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