A conditional causes part of a makefile to be obeyed or ignored depending on the values of variables. Conditionals can compare the value of one variable to another, or the value of a variable to a constant string. Conditionals control what
makeactually "sees" in the makefile, so they cannot be used to control shell commands at the time of execution.
maketo use one set of libraries if the
CCvariable is `gcc', and a different set of libraries otherwise. It works by controlling which of two command lines will be used as the command for a rule. The result is that `CC=gcc' as an argument to
makechanges not only which compiler is used but also which libraries are linked.
libs_for_gcc = -lgnu normal_libs = foo: $(objects) ifeq ($(CC),gcc) $(CC) -o foo $(objects) $(libs_for_gcc) else $(CC) -o foo $(objects) $(normal_libs) endifThis conditional uses three directives: one
ifeqdirective begins the conditional, and specifies the condition. It contains two arguments, separated by a comma and surrounded by parentheses. Variable substitution is performed on both arguments and then they are compared. The lines of the makefile following the
ifeqare obeyed if the two arguments match; otherwise they are ignored.
elsedirective causes the following lines to be obeyed if the previous conditional failed. In the example above, this means that the second alternative linking command is used whenever the first alternative is not used. It is optional to have an
elsein a conditional.
endifdirective ends the conditional. Every conditional must end with an
endif. Unconditional makefile text follows.
As this example illustrates, conditionals work at the textual level: the lines of the conditional are treated as part of the makefile, or ignored, according to the condition. This is why the larger syntactic units of the makefile, such as rules, may cross the beginning or the end of the conditional.
When the variable
CChas the value `gcc', the above example has this effect:
foo: $(objects) $(CC) -o foo $(objects) $(libs_for_gcc)When the variable
CChas any other value, the effect is this:
foo: $(objects) $(CC) -o foo $(objects) $(normal_libs)Equivalent results can be obtained in another way by conditionalizing a variable assignment and then using the variable unconditionally:
libs_for_gcc = -lgnu normal_libs = ifeq ($(CC),gcc) libs=$(libs_for_gcc) else libs=$(normal_libs) endif foo: $(objects) $(CC) -o foo $(objects) $(libs)
elseis as follows:
conditional-directive text-if-true endifThe 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 endifIf the condition is true, text-if-true is used; otherwise, text-if-false is used instead. The 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. 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
stripfunction (see section Functions for String Substitution and Analysis) to avoid interpreting whitespace as a non-empty value. For example:
ifeq ($(strip $(foo)),) text-if-empty endifwill 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.
- If the variable variable-name 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.Note that
ifdefonly tests whether a variable has a value. It does not expand the variable to see if that value is nonempty. Consequently, tests using
ifdefreturn 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 endifsets `frobozz' to `yes', while:
foo = ifdef foo frobozz = yes else frobozz = no endifsets `frobozz' to `no'.
- 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 other two directives that play a part in a conditional are
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
makeuses. If the condition is true,
makereads the lines of the text-if-true as part of the makefile; if the condition is false,
makeignores 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.
makeevaluates conditionals when it reads a makefile. Consequently, you cannot use automatic variables in the tests of conditionals because they are not defined until commands are run (see section 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
includedirective within a conditional, provided you do not attempt to terminate the conditional inside the included file.
makecommand flags such as `-t' by using the variable
MAKEFLAGStogether with the
findstringfunction (see section Functions for String Substitution and Analysis). This is useful when
touchis not enough to make a file appear up to date.
findstringfunction determines whether one string appears as a substring of another. If you want to test for the `-t' flag, use `t' as the first string and the value of
MAKEFLAGSas the other.
For example, here is how to arrange to use `ranlib -t' to finish marking an archive file up to date:
archive.a: ... ifneq (,$(findstring t,$(MAKEFLAGS))) +touch archive.a +ranlib -t archive.a else ranlib archive.a endifThe `+' prefix marks those command lines as "recursive" so that they will be executed despite use of the `-t' flag. See section Recursive Use of
GNU Make - Conditional Parts of Makefiles
'via Blog this'