60

Can someone explain how to use if-then statements and for loops in Makefiles? I can't seem to find any good documentation with examples.

Jeff Atwood
  • 63,320
  • 48
  • 150
  • 153
GavinR
  • 6,094
  • 7
  • 33
  • 44
  • 1
    I add "with simple examples". The documenatation's ok, but man are the examples abstract! To me, this is still a valid question. – Quickredfox Jun 12 '12 at 23:23

4 Answers4

69

Conditional Forms

Simple

conditional-directive
text-if-true
endif

Moderately Complex

conditional-directive
text-if-true
else
text-if-false
endif

More Complex

conditional-directive
text-if-one-is-true
else
conditional-directive
text-if-true
else
text-if-false
endif
endif

Conditional Directives

If Equal Syntax

ifeq (arg1, arg2)
ifeq 'arg1' 'arg2'
ifeq "arg1" "arg2"
ifeq "arg1" 'arg2'
ifeq 'arg1' "arg2"

If Not Equal Syntax

ifneq (arg1, arg2)
ifneq 'arg1' 'arg2'
ifneq "arg1" "arg2"
ifneq "arg1" 'arg2'
ifneq 'arg1' "arg2"

If Defined Syntax

ifdef variable-name

If Not Defined Syntax

ifndef variable-name  

foreach Function

foreach Function Syntax

$(foreach var, list, text)  

foreach Semantics
For each whitespace separated word in "list", the variable named by "var" is set to that word and text is executed.

tjhei
  • 133
  • 1
  • 5
John Mulder
  • 9,765
  • 7
  • 33
  • 37
20

Here's an example if:

ifeq ($(strip $(OS)),Linux)
        PYTHON = /usr/bin/python
        FIND = /usr/bin/find
endif

Note that this comes with a word of warning that different versions of Make have slightly different syntax, none of which seems to be documented very well.

Mark Roddy
  • 27,122
  • 19
  • 67
  • 71
  • 1
    Is `$(OS)` a predefined GNU make variable? If so, then maybe you should add it [here](http://stackoverflow.com/q/714100/912144). – Shahbaz Nov 16 '12 at 10:08
8

Have you tried the GNU make documentation? It has a whole section about conditionals with examples.

Paige Ruten
  • 172,675
  • 36
  • 177
  • 197
  • 6
    It is interesting that they waste a lot of words explaining several combinations, but don't mention the most basic thing, that the `ifeq` cannot be indented. – Rafael Eyng Jun 12 '20 at 16:50
  • 1
    I wasted couple of hours pulling my hair just to find ifeq cannot be indented... Thankyou for your valuable comment @RafaelEyng – Rinav Apr 22 '22 at 06:05
  • if `ifeq` cannot be indented, does this imply conditionals cannot be used inside a target? – JamesWilson Jun 01 '22 at 00:47
  • Alternate solution here https://stackoverflow.com/a/58602879/413538 that works inside make targets – JamesWilson Jun 01 '22 at 00:58
  • "The recipe lines start with a tab character" [ref](https://www.gnu.org/software/make/manual/html_node/Rule-Syntax.html#Rule-Syntax), given Conditionals are not Recipe lines, they don't - shrug – Efren Jun 17 '22 at 01:26
5

You do see for loops alot of the time, but they are usually not needed. Here is an example of how one might perform a for loop without resorting to the shell

LIST_OF_THINGS_TO_DO = do_this do_that 
$(LIST_OF_THINGS_TO_DO): 
       run $@ > $@.out

SUBDIRS = snafu fubar
$(SUBDIRS):
     cd $@ && $(MAKE)
Kramer
  • 174
  • 3
  • 5