make: Setting
1
1 6.5 Setting Variables
1 =====================
1
1 To set a variable from the makefile, write a line starting with the
1 variable name followed by '=', ':=', or '::='. Whatever follows the
1 '=', ':=', or '::=' on the line becomes the value. For example,
1
1 objects = main.o foo.o bar.o utils.o
1
1 defines a variable named 'objects'. Whitespace around the variable name
1 and immediately after the '=' is ignored.
1
1 Variables defined with '=' are "recursively expanded" variables.
1 Variables defined with ':=' or '::=' are "simply expanded" variables;
1 these definitions can contain variable references which will be expanded
11 before the definition is made. ⇒The Two Flavors of Variables
Flavors.
1
1 The variable name may contain function and variable references, which
1 are expanded when the line is read to find the actual variable name to
1 use.
1
1 There is no limit on the length of the value of a variable except the
1 amount of memory on the computer. You can split the value of a variable
11 into multiple physical lines for readability (⇒Splitting Long
Lines Splitting Lines.).
1
1 Most variable names are considered to have the empty string as a
1 value if you have never set them. Several variables have built-in
1 initial values that are not empty, but you can set them in the usual
1 ways (⇒Variables Used by Implicit Rules Implicit Variables.).
1 Several special variables are set automatically to a new value for each
11 rule; these are called the "automatic" variables (⇒Automatic
Variables).
1
1 If you'd like a variable to be set to a value only if it's not
1 already set, then you can use the shorthand operator '?=' instead of
11 '='. These two settings of the variable 'FOO' are identical (⇒The
'origin' Function Origin Function.):
1
1 FOO ?= bar
1
1 and
1
1 ifeq ($(origin FOO), undefined)
1 FOO = bar
1 endif
1
1 The shell assignment operator '!=' can be used to execute a shell
1 script and set a variable to its output. This operator first evaluates
1 the right-hand side, then passes that result to the shell for execution.
1 If the result of the execution ends in a newline, that one newline is
1 removed; all other newlines are replaced by spaces. The resulting
1 string is then placed into the named recursively-expanded variable. For
1 example:
1
1 hash != printf '\043'
1 file_list != find . -name '*.c'
1
1 If the result of the execution could produce a '$', and you don't
1 intend what follows that to be interpreted as a make variable or
1 function reference, then you must replace every '$' with '$$' as part of
1 the execution. Alternatively, you can set a simply expanded variable to
11 the result of running a program using the 'shell' function call. ⇒
The 'shell' Function Shell Function. For example:
1
1 hash := $(shell printf '\043')
1 var := $(shell find . -name "*.c")
1
1 As with the 'shell' function, the exit status of the just-invoked
1 shell script is stored in the '.SHELLSTATUS' variable.
1