make: Conditional Syntax
1
1 7.2 Syntax of Conditionals
1 ==========================
1
1 The syntax of a simple conditional with no 'else' is as follows:
1
1 CONDITIONAL-DIRECTIVE
1 TEXT-IF-TRUE
1 endif
1
1 The TEXT-IF-TRUE may be any lines of text, to be considered as part of
1 the makefile if the condition is true. If the condition is false, no
1 text is used instead.
1
1 The syntax of a complex conditional is as follows:
1
1 CONDITIONAL-DIRECTIVE
1 TEXT-IF-TRUE
1 else
1 TEXT-IF-FALSE
1 endif
1
1 or:
1
1 CONDITIONAL-DIRECTIVE-ONE
1 TEXT-IF-ONE-IS-TRUE
1 else CONDITIONAL-DIRECTIVE-TWO
1 TEXT-IF-TWO-IS-TRUE
1 else
1 TEXT-IF-ONE-AND-TWO-ARE-FALSE
1 endif
1
1 There can be as many "'else' CONDITIONAL-DIRECTIVE" clauses as
1 necessary. Once a given condition is true, TEXT-IF-TRUE is used and no
1 other clause is used; if no condition is true then TEXT-IF-FALSE is
1 used. The TEXT-IF-TRUE and TEXT-IF-FALSE can be any number of lines of
1 text.
1
1 The syntax of the CONDITIONAL-DIRECTIVE is the same whether the
1 conditional is simple or complex; after an 'else' or not. There are
1 four different directives that test different conditions. Here is a
1 table of them:
1
1 'ifeq (ARG1, ARG2)'
1 'ifeq 'ARG1' 'ARG2''
1 'ifeq "ARG1" "ARG2"'
1 'ifeq "ARG1" 'ARG2''
1 'ifeq 'ARG1' "ARG2"'
1 Expand all variable references in ARG1 and ARG2 and compare them.
1 If they are identical, the TEXT-IF-TRUE is effective; otherwise,
1 the TEXT-IF-FALSE, if any, is effective.
1
1 Often you want to test if a variable has a non-empty value. When
1 the value results from complex expansions of variables and
1 functions, expansions you would consider empty may actually contain
1 whitespace characters and thus are not seen as empty. However, you
1 can use the 'strip' function (⇒Text Functions) to avoid
1 interpreting whitespace as a non-empty value. For example:
1
1 ifeq ($(strip $(foo)),)
1 TEXT-IF-EMPTY
1 endif
1
1 will evaluate TEXT-IF-EMPTY even if the expansion of '$(foo)'
1 contains whitespace characters.
1
1 'ifneq (ARG1, ARG2)'
1 'ifneq 'ARG1' 'ARG2''
1 'ifneq "ARG1" "ARG2"'
1 'ifneq "ARG1" 'ARG2''
1 'ifneq 'ARG1' "ARG2"'
1 Expand all variable references in ARG1 and ARG2 and compare them.
1 If they are different, the TEXT-IF-TRUE is effective; otherwise,
1 the TEXT-IF-FALSE, if any, is effective.
1
1 'ifdef VARIABLE-NAME'
1 The 'ifdef' form takes the _name_ of a variable as its argument,
1 not a reference to a variable. If the value of that variable has a
1 non-empty value, the TEXT-IF-TRUE is effective; otherwise, the
1 TEXT-IF-FALSE, if any, is effective. Variables that have never
1 been defined have an empty value. The text VARIABLE-NAME is
1 expanded, so it could be a variable or function that expands to the
1 name of a variable. For example:
1
1 bar = true
1 foo = bar
1 ifdef $(foo)
1 frobozz = yes
1 endif
1
1 The variable reference '$(foo)' is expanded, yielding 'bar', which
1 is considered to be the name of a variable. The variable 'bar' is
1 not expanded, but its value is examined to determine if it is
1 non-empty.
1
1 Note that 'ifdef' only tests whether a variable has a value. It
1 does not expand the variable to see if that value is nonempty.
1 Consequently, tests using 'ifdef' return true for all definitions
1 except those like 'foo ='. To test for an empty value, use
1 'ifeq ($(foo),)'. For example,
1
1 bar =
1 foo = $(bar)
1 ifdef foo
1 frobozz = yes
1 else
1 frobozz = no
1 endif
1
1 sets 'frobozz' to 'yes', while:
1
1 foo =
1 ifdef foo
1 frobozz = yes
1 else
1 frobozz = no
1 endif
1
1 sets 'frobozz' to 'no'.
1
1 'ifndef VARIABLE-NAME'
1 If the variable VARIABLE-NAME has an empty value, the TEXT-IF-TRUE
1 is effective; otherwise, the TEXT-IF-FALSE, if any, is effective.
1 The rules for expansion and testing of VARIABLE-NAME are identical
1 to the 'ifdef' directive.
1
1 Extra spaces are allowed and ignored at the beginning of the
1 conditional directive line, but a tab is not allowed. (If the line
1 begins with a tab, it will be considered part of a recipe for a rule.)
1 Aside from this, extra spaces or tabs may be inserted with no effect
1 anywhere except within the directive name or within an argument. A
1 comment starting with '#' may appear at the end of the line.
1
1 The other two directives that play a part in a conditional are 'else'
1 and 'endif'. Each of these directives is written as one word, with no
1 arguments. Extra spaces are allowed and ignored at the beginning of the
1 line, and spaces or tabs at the end. A comment starting with '#' may
1 appear at the end of the line.
1
1 Conditionals affect which lines of the makefile 'make' uses. If the
1 condition is true, 'make' reads the lines of the TEXT-IF-TRUE as part of
1 the makefile; if the condition is false, 'make' ignores those lines
1 completely. It follows that syntactic units of the makefile, such as
1 rules, may safely be split across the beginning or the end of the
1 conditional.
1
1 'make' evaluates conditionals when it reads a makefile.
1 Consequently, you cannot use automatic variables in the tests of
11 conditionals because they are not defined until recipes are run (⇒
Automatic Variables).
1
1 To prevent intolerable confusion, it is not permitted to start a
1 conditional in one makefile and end it in another. However, you may
1 write an 'include' directive within a conditional, provided you do not
1 attempt to terminate the conditional inside the included file.
1