1 This is make.info, produced by makeinfo version 4.2 from make.texi.
3 INFO-DIR-SECTION GNU Packages
5 * Make: (make). Remake files automatically.
8 This file documents the GNU Make utility, which determines
9 automatically which pieces of a large program need to be recompiled,
10 and issues the commands to recompile them.
12 This is Edition 0.60, last updated 08 July 2002, of `The GNU Make
13 Manual', for `make', Version 3.80.
15 Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
16 1997, 1998, 1999, 2000, 2002 Free Software Foundation, Inc.
18 Permission is granted to copy, distribute and/or modify this document
19 under the terms of the GNU Free Documentation License, Version 1.1 or
20 any later version published by the Free Software Foundation; with no
21 Invariant Sections, with no Front-Cover Texts, and with no Back-Cover
22 Texts. A copy of the license is included in the section entitled "GNU
23 Free Documentation License".
26 File: make.info, Node: Sequences, Next: Empty Commands, Prev: Recursion, Up: Commands
28 Defining Canned Command Sequences
29 =================================
31 When the same sequence of commands is useful in making various
32 targets, you can define it as a canned sequence with the `define'
33 directive, and refer to the canned sequence from the rules for those
34 targets. The canned sequence is actually a variable, so the name must
35 not conflict with other variable names.
37 Here is an example of defining a canned sequence of commands:
44 Here `run-yacc' is the name of the variable being defined; `endef'
45 marks the end of the definition; the lines in between are the commands.
46 The `define' directive does not expand variable references and
47 function calls in the canned sequence; the `$' characters, parentheses,
48 variable names, and so on, all become part of the value of the variable
49 you are defining. *Note Defining Variables Verbatim: Defining, for a
50 complete explanation of `define'.
52 The first command in this example runs Yacc on the first
53 prerequisite of whichever rule uses the canned sequence. The output
54 file from Yacc is always named `y.tab.c'. The second command moves the
55 output to the rule's target file name.
57 To use the canned sequence, substitute the variable into the
58 commands of a rule. You can substitute it like any other variable
59 (*note Basics of Variable References: Reference.). Because variables
60 defined by `define' are recursively expanded variables, all the
61 variable references you wrote inside the `define' are expanded now.
67 `foo.y' will be substituted for the variable `$^' when it occurs in
68 `run-yacc''s value, and `foo.c' for `$@'.
70 This is a realistic example, but this particular one is not needed in
71 practice because `make' has an implicit rule to figure out these
72 commands based on the file names involved (*note Using Implicit Rules:
75 In command execution, each line of a canned sequence is treated just
76 as if the line appeared on its own in the rule, preceded by a tab. In
77 particular, `make' invokes a separate subshell for each line. You can
78 use the special prefix characters that affect command lines (`@', `-',
79 and `+') on each line of a canned sequence. *Note Writing the Commands
80 in Rules: Commands. For example, using this canned sequence:
83 @echo "frobnicating target $@"
84 frob-step-1 $< -o $@-step-1
85 frob-step-2 $@-step-1 -o $@
88 `make' will not echo the first line, the `echo' command. But it _will_
89 echo the following two command lines.
91 On the other hand, prefix characters on the command line that refers
92 to a canned sequence apply to every line in the sequence. So the rule:
97 does not echo _any_ commands. (*Note Command Echoing: Echoing, for a
98 full explanation of `@'.)
101 File: make.info, Node: Empty Commands, Prev: Sequences, Up: Commands
106 It is sometimes useful to define commands which do nothing. This is
107 done simply by giving a command that consists of nothing but
108 whitespace. For example:
112 defines an empty command string for `target'. You could also use a
113 line beginning with a tab character to define an empty command string,
114 but this would be confusing because such a line looks empty.
116 You may be wondering why you would want to define a command string
117 that does nothing. The only reason this is useful is to prevent a
118 target from getting implicit commands (from implicit rules or the
119 `.DEFAULT' special target; *note Implicit Rules:: and *note Defining
120 Last-Resort Default Rules: Last Resort.).
122 You may be inclined to define empty command strings for targets that
123 are not actual files, but only exist so that their prerequisites can be
124 remade. However, this is not the best way to do that, because the
125 prerequisites may not be remade properly if the target file actually
126 does exist. *Note Phony Targets: Phony Targets, for a better way to do
130 File: make.info, Node: Using Variables, Next: Conditionals, Prev: Commands, Up: Top
135 A "variable" is a name defined in a makefile to represent a string
136 of text, called the variable's "value". These values are substituted
137 by explicit request into targets, prerequisites, commands, and other
138 parts of the makefile. (In some other versions of `make', variables
139 are called "macros".)
141 Variables and functions in all parts of a makefile are expanded when
142 read, except for the shell commands in rules, the right-hand sides of
143 variable definitions using `=', and the bodies of variable definitions
144 using the `define' directive.
146 Variables can represent lists of file names, options to pass to
147 compilers, programs to run, directories to look in for source files,
148 directories to write output in, or anything else you can imagine.
150 A variable name may be any sequence of characters not containing `:',
151 `#', `=', or leading or trailing whitespace. However, variable names
152 containing characters other than letters, numbers, and underscores
153 should be avoided, as they may be given special meanings in the future,
154 and with some shells they cannot be passed through the environment to a
155 sub-`make' (*note Communicating Variables to a Sub-`make':
156 Variables/Recursion.).
158 Variable names are case-sensitive. The names `foo', `FOO', and
159 `Foo' all refer to different variables.
161 It is traditional to use upper case letters in variable names, but we
162 recommend using lower case letters for variable names that serve
163 internal purposes in the makefile, and reserving upper case for
164 parameters that control implicit rules or for parameters that the user
165 should override with command options (*note Overriding Variables:
168 A few variables have names that are a single punctuation character or
169 just a few characters. These are the "automatic variables", and they
170 have particular specialized uses. *Note Automatic Variables: Automatic.
174 * Reference:: How to use the value of a variable.
175 * Flavors:: Variables come in two flavors.
176 * Advanced:: Advanced features for referencing a variable.
177 * Values:: All the ways variables get their values.
178 * Setting:: How to set a variable in the makefile.
179 * Appending:: How to append more text to the old value
181 * Override Directive:: How to set a variable in the makefile even if
182 the user has set it with a command argument.
183 * Defining:: An alternate way to set a variable
184 to a verbatim string.
185 * Environment:: Variable values can come from the environment.
186 * Target-specific:: Variable values can be defined on a per-target
188 * Pattern-specific:: Target-specific variable values can be applied
189 to a group of targets that match a pattern.
192 File: make.info, Node: Reference, Next: Flavors, Prev: Using Variables, Up: Using Variables
194 Basics of Variable References
195 =============================
197 To substitute a variable's value, write a dollar sign followed by
198 the name of the variable in parentheses or braces: either `$(foo)' or
199 `${foo}' is a valid reference to the variable `foo'. This special
200 significance of `$' is why you must write `$$' to have the effect of a
201 single dollar sign in a file name or command.
203 Variable references can be used in any context: targets,
204 prerequisites, commands, most directives, and new variable values.
205 Here is an example of a common case, where a variable holds the names
206 of all the object files in a program:
208 objects = program.o foo.o utils.o
210 cc -o program $(objects)
214 Variable references work by strict textual substitution. Thus, the
219 $(foo)$(foo) -$(foo) prog.$(foo)
221 could be used to compile a C program `prog.c'. Since spaces before the
222 variable value are ignored in variable assignments, the value of `foo'
223 is precisely `c'. (Don't actually write your makefiles this way!)
225 A dollar sign followed by a character other than a dollar sign,
226 open-parenthesis or open-brace treats that single character as the
227 variable name. Thus, you could reference the variable `x' with `$x'.
228 However, this practice is strongly discouraged, except in the case of
229 the automatic variables (*note Automatic Variables: Automatic.).
232 File: make.info, Node: Flavors, Next: Advanced, Prev: Reference, Up: Using Variables
234 The Two Flavors of Variables
235 ============================
237 There are two ways that a variable in GNU `make' can have a value;
238 we call them the two "flavors" of variables. The two flavors are
239 distinguished in how they are defined and in what they do when expanded.
241 The first flavor of variable is a "recursively expanded" variable.
242 Variables of this sort are defined by lines using `=' (*note Setting
243 Variables: Setting.) or by the `define' directive (*note Defining
244 Variables Verbatim: Defining.). The value you specify is installed
245 verbatim; if it contains references to other variables, these
246 references are expanded whenever this variable is substituted (in the
247 course of expanding some other string). When this happens, it is
248 called "recursive expansion".
258 will echo `Huh?': `$(foo)' expands to `$(bar)' which expands to
259 `$(ugh)' which finally expands to `Huh?'.
261 This flavor of variable is the only sort supported by other versions
262 of `make'. It has its advantages and its disadvantages. An advantage
263 (most would say) is that:
265 CFLAGS = $(include_dirs) -O
266 include_dirs = -Ifoo -Ibar
268 will do what was intended: when `CFLAGS' is expanded in a command, it
269 will expand to `-Ifoo -Ibar -O'. A major disadvantage is that you
270 cannot append something on the end of a variable, as in
272 CFLAGS = $(CFLAGS) -O
274 because it will cause an infinite loop in the variable expansion.
275 (Actually `make' detects the infinite loop and reports an error.)
277 Another disadvantage is that any functions (*note Functions for
278 Transforming Text: Functions.) referenced in the definition will be
279 executed every time the variable is expanded. This makes `make' run
280 slower; worse, it causes the `wildcard' and `shell' functions to give
281 unpredictable results because you cannot easily control when they are
282 called, or even how many times.
284 To avoid all the problems and inconveniences of recursively expanded
285 variables, there is another flavor: simply expanded variables.
287 "Simply expanded variables" are defined by lines using `:=' (*note
288 Setting Variables: Setting.). The value of a simply expanded variable
289 is scanned once and for all, expanding any references to other
290 variables and functions, when the variable is defined. The actual
291 value of the simply expanded variable is the result of expanding the
292 text that you write. It does not contain any references to other
293 variables; it contains their values _as of the time this variable was
305 When a simply expanded variable is referenced, its value is
306 substituted verbatim.
308 Here is a somewhat more complicated example, illustrating the use of
309 `:=' in conjunction with the `shell' function. (*Note The `shell'
310 Function: Shell Function.) This example also shows use of the variable
311 `MAKELEVEL', which is changed when it is passed down from level to
312 level. (*Note Communicating Variables to a Sub-`make':
313 Variables/Recursion, for information about `MAKELEVEL'.)
315 ifeq (0,${MAKELEVEL})
316 cur-dir := $(shell pwd)
317 whoami := $(shell whoami)
318 host-type := $(shell arch)
319 MAKE := ${MAKE} host-type=${host-type} whoami=${whoami}
322 An advantage of this use of `:=' is that a typical `descend into a
323 directory' command then looks like this:
326 ${MAKE} cur-dir=${cur-dir}/$@ -C $@ all
328 Simply expanded variables generally make complicated makefile
329 programming more predictable because they work like variables in most
330 programming languages. They allow you to redefine a variable using its
331 own value (or its value processed in some way by one of the expansion
332 functions) and to use the expansion functions much more efficiently
333 (*note Functions for Transforming Text: Functions.).
335 You can also use them to introduce controlled leading whitespace into
336 variable values. Leading whitespace characters are discarded from your
337 input before substitution of variable references and function calls;
338 this means you can include leading spaces in a variable value by
339 protecting them with variable references, like this:
342 space := $(nullstring) # end of the line
344 Here the value of the variable `space' is precisely one space. The
345 comment `# end of the line' is included here just for clarity. Since
346 trailing space characters are _not_ stripped from variable values, just
347 a space at the end of the line would have the same effect (but be
348 rather hard to read). If you put whitespace at the end of a variable
349 value, it is a good idea to put a comment like that at the end of the
350 line to make your intent clear. Conversely, if you do _not_ want any
351 whitespace characters at the end of your variable value, you must
352 remember not to put a random comment on the end of the line after some
353 whitespace, such as this:
355 dir := /foo/bar # directory to put the frobs in
357 Here the value of the variable `dir' is `/foo/bar ' (with four
358 trailing spaces), which was probably not the intention. (Imagine
359 something like `$(dir)/file' with this definition!)
361 There is another assignment operator for variables, `?='. This is
362 called a conditional variable assignment operator, because it only has
363 an effect if the variable is not yet defined. This statement:
367 is exactly equivalent to this (*note The `origin' Function: Origin
370 ifeq ($(origin FOO), undefined)
374 Note that a variable set to an empty value is still defined, so `?='
375 will not set that variable.
378 File: make.info, Node: Advanced, Next: Values, Prev: Flavors, Up: Using Variables
380 Advanced Features for Reference to Variables
381 ============================================
383 This section describes some advanced features you can use to
384 reference variables in more flexible ways.
388 * Substitution Refs:: Referencing a variable with
389 substitutions on the value.
390 * Computed Names:: Computing the name of the variable to refer to.
393 File: make.info, Node: Substitution Refs, Next: Computed Names, Prev: Advanced, Up: Advanced
395 Substitution References
396 -----------------------
398 A "substitution reference" substitutes the value of a variable with
399 alterations that you specify. It has the form `$(VAR:A=B)' (or
400 `${VAR:A=B}') and its meaning is to take the value of the variable VAR,
401 replace every A at the end of a word with B in that value, and
402 substitute the resulting string.
404 When we say "at the end of a word", we mean that A must appear
405 either followed by whitespace or at the end of the value in order to be
406 replaced; other occurrences of A in the value are unaltered. For
412 sets `bar' to `a.c b.c c.c'. *Note Setting Variables: Setting.
414 A substitution reference is actually an abbreviation for use of the
415 `patsubst' expansion function (*note Functions for String Substitution
416 and Analysis: Text Functions.). We provide substitution references as
417 well as `patsubst' for compatibility with other implementations of
420 Another type of substitution reference lets you use the full power of
421 the `patsubst' function. It has the same form `$(VAR:A=B)' described
422 above, except that now A must contain a single `%' character. This
423 case is equivalent to `$(patsubst A,B,$(VAR))'. *Note Functions for
424 String Substitution and Analysis: Text Functions, for a description of
425 the `patsubst' function.
430 bar := $(foo:%.o=%.c)
432 sets `bar' to `a.c b.c c.c'.
435 File: make.info, Node: Computed Names, Prev: Substitution Refs, Up: Advanced
437 Computed Variable Names
438 -----------------------
440 Computed variable names are a complicated concept needed only for
441 sophisticated makefile programming. For most purposes you need not
442 consider them, except to know that making a variable with a dollar sign
443 in its name might have strange results. However, if you are the type
444 that wants to understand everything, or you are actually interested in
445 what they do, read on.
447 Variables may be referenced inside the name of a variable. This is
448 called a "computed variable name" or a "nested variable reference".
455 defines `a' as `z': the `$(x)' inside `$($(x))' expands to `y', so
456 `$($(x))' expands to `$(y)' which in turn expands to `z'. Here the
457 name of the variable to reference is not stated explicitly; it is
458 computed by expansion of `$(x)'. The reference `$(x)' here is nested
459 within the outer variable reference.
461 The previous example shows two levels of nesting, but any number of
462 levels is possible. For example, here are three levels:
469 Here the innermost `$(x)' expands to `y', so `$($(x))' expands to
470 `$(y)' which in turn expands to `z'; now we have `$(z)', which becomes
473 References to recursively-expanded variables within a variable name
474 are reexpanded in the usual fashion. For example:
481 defines `a' as `Hello': `$($(x))' becomes `$($(y))' which becomes
482 `$(z)' which becomes `Hello'.
484 Nested variable references can also contain modified references and
485 function invocations (*note Functions for Transforming Text:
486 Functions.), just like any other reference. For example, using the
487 `subst' function (*note Functions for String Substitution and Analysis:
492 y = $(subst 1,2,$(x))
496 eventually defines `a' as `Hello'. It is doubtful that anyone would
497 ever want to write a nested reference as convoluted as this one, but it
498 works: `$($($(z)))' expands to `$($(y))' which becomes `$($(subst
499 1,2,$(x)))'. This gets the value `variable1' from `x' and changes it
500 by substitution to `variable2', so that the entire string becomes
501 `$(variable2)', a simple variable reference whose value is `Hello'.
503 A computed variable name need not consist entirely of a single
504 variable reference. It can contain several variable references, as
505 well as some invariant text. For example,
510 a_files := filea fileb
511 1_files := file1 file2
513 ifeq "$(use_a)" "yes"
519 ifeq "$(use_dirs)" "yes"
525 dirs := $($(a1)_$(df))
527 will give `dirs' the same value as `a_dirs', `1_dirs', `a_files' or
528 `1_files' depending on the settings of `use_a' and `use_dirs'.
530 Computed variable names can also be used in substitution references:
532 a_objects := a.o b.o c.o
533 1_objects := 1.o 2.o 3.o
535 sources := $($(a1)_objects:.o=.c)
537 defines `sources' as either `a.c b.c c.c' or `1.c 2.c 3.c', depending
538 on the value of `a1'.
540 The only restriction on this sort of use of nested variable
541 references is that they cannot specify part of the name of a function
542 to be called. This is because the test for a recognized function name
543 is done before the expansion of nested references. For example,
553 foo := $($(func) $(bar))
555 attempts to give `foo' the value of the variable `sort a d b g q c' or
556 `strip a d b g q c', rather than giving `a d b g q c' as the argument
557 to either the `sort' or the `strip' function. This restriction could
558 be removed in the future if that change is shown to be a good idea.
560 You can also use computed variable names in the left-hand side of a
561 variable assignment, or in a `define' directive, as in:
564 $(dir)_sources := $(wildcard $(dir)/*.c)
566 lpr $($(dir)_sources)
569 This example defines the variables `dir', `foo_sources', and
572 Note that "nested variable references" are quite different from
573 "recursively expanded variables" (*note The Two Flavors of Variables:
574 Flavors.), though both are used together in complex ways when doing
575 makefile programming.
578 File: make.info, Node: Values, Next: Setting, Prev: Advanced, Up: Using Variables
580 How Variables Get Their Values
581 ==============================
583 Variables can get values in several different ways:
585 * You can specify an overriding value when you run `make'. *Note
586 Overriding Variables: Overriding.
588 * You can specify a value in the makefile, either with an assignment
589 (*note Setting Variables: Setting.) or with a verbatim definition
590 (*note Defining Variables Verbatim: Defining.).
592 * Variables in the environment become `make' variables. *Note
593 Variables from the Environment: Environment.
595 * Several "automatic" variables are given new values for each rule.
596 Each of these has a single conventional use. *Note Automatic
597 Variables: Automatic.
599 * Several variables have constant initial values. *Note Variables
600 Used by Implicit Rules: Implicit Variables.
603 File: make.info, Node: Setting, Next: Appending, Prev: Values, Up: Using Variables
608 To set a variable from the makefile, write a line starting with the
609 variable name followed by `=' or `:='. Whatever follows the `=' or
610 `:=' on the line becomes the value. For example,
612 objects = main.o foo.o bar.o utils.o
614 defines a variable named `objects'. Whitespace around the variable
615 name and immediately after the `=' is ignored.
617 Variables defined with `=' are "recursively expanded" variables.
618 Variables defined with `:=' are "simply expanded" variables; these
619 definitions can contain variable references which will be expanded
620 before the definition is made. *Note The Two Flavors of Variables:
623 The variable name may contain function and variable references, which
624 are expanded when the line is read to find the actual variable name to
627 There is no limit on the length of the value of a variable except the
628 amount of swapping space on the computer. When a variable definition is
629 long, it is a good idea to break it into several lines by inserting
630 backslash-newline at convenient places in the definition. This will not
631 affect the functioning of `make', but it will make the makefile easier
634 Most variable names are considered to have the empty string as a
635 value if you have never set them. Several variables have built-in
636 initial values that are not empty, but you can set them in the usual
637 ways (*note Variables Used by Implicit Rules: Implicit Variables.).
638 Several special variables are set automatically to a new value for each
639 rule; these are called the "automatic" variables (*note Automatic
640 Variables: Automatic.).
642 If you'd like a variable to be set to a value only if it's not
643 already set, then you can use the shorthand operator `?=' instead of
644 `='. These two settings of the variable `FOO' are identical (*note The
645 `origin' Function: Origin Function.):
651 ifeq ($(origin FOO), undefined)
656 File: make.info, Node: Appending, Next: Override Directive, Prev: Setting, Up: Using Variables
658 Appending More Text to Variables
659 ================================
661 Often it is useful to add more text to the value of a variable
662 already defined. You do this with a line containing `+=', like this:
666 This takes the value of the variable `objects', and adds the text
667 `another.o' to it (preceded by a single space). Thus:
669 objects = main.o foo.o bar.o utils.o
672 sets `objects' to `main.o foo.o bar.o utils.o another.o'.
674 Using `+=' is similar to:
676 objects = main.o foo.o bar.o utils.o
677 objects := $(objects) another.o
679 but differs in ways that become important when you use more complex
682 When the variable in question has not been defined before, `+=' acts
683 just like normal `=': it defines a recursively-expanded variable.
684 However, when there _is_ a previous definition, exactly what `+=' does
685 depends on what flavor of variable you defined originally. *Note The
686 Two Flavors of Variables: Flavors, for an explanation of the two
687 flavors of variables.
689 When you add to a variable's value with `+=', `make' acts
690 essentially as if you had included the extra text in the initial
691 definition of the variable. If you defined it first with `:=', making
692 it a simply-expanded variable, `+=' adds to that simply-expanded
693 definition, and expands the new text before appending it to the old
694 value just as `:=' does (*note Setting Variables: Setting., for a full
695 explanation of `:='). In fact,
700 is exactly equivalent to:
703 variable := $(variable) more
705 On the other hand, when you use `+=' with a variable that you defined
706 first to be recursively-expanded using plain `=', `make' does something
707 a bit different. Recall that when you define a recursively-expanded
708 variable, `make' does not expand the value you set for variable and
709 function references immediately. Instead it stores the text verbatim,
710 and saves these variable and function references to be expanded later,
711 when you refer to the new variable (*note The Two Flavors of Variables:
712 Flavors.). When you use `+=' on a recursively-expanded variable, it is
713 this unexpanded text to which `make' appends the new text you specify.
718 is roughly equivalent to:
721 variable = $(temp) more
723 except that of course it never defines a variable called `temp'. The
724 importance of this comes when the variable's old value contains
725 variable references. Take this common example:
727 CFLAGS = $(includes) -O
729 CFLAGS += -pg # enable profiling
731 The first line defines the `CFLAGS' variable with a reference to another
732 variable, `includes'. (`CFLAGS' is used by the rules for C
733 compilation; *note Catalogue of Implicit Rules: Catalogue of Rules..)
734 Using `=' for the definition makes `CFLAGS' a recursively-expanded
735 variable, meaning `$(includes) -O' is _not_ expanded when `make'
736 processes the definition of `CFLAGS'. Thus, `includes' need not be
737 defined yet for its value to take effect. It only has to be defined
738 before any reference to `CFLAGS'. If we tried to append to the value
739 of `CFLAGS' without using `+=', we might do it like this:
741 CFLAGS := $(CFLAGS) -pg # enable profiling
743 This is pretty close, but not quite what we want. Using `:=' redefines
744 `CFLAGS' as a simply-expanded variable; this means `make' expands the
745 text `$(CFLAGS) -pg' before setting the variable. If `includes' is not
746 yet defined, we get ` -O -pg', and a later definition of `includes'
747 will have no effect. Conversely, by using `+=' we set `CFLAGS' to the
748 _unexpanded_ value `$(includes) -O -pg'. Thus we preserve the
749 reference to `includes', so if that variable gets defined at any later
750 point, a reference like `$(CFLAGS)' still uses its value.
753 File: make.info, Node: Override Directive, Next: Defining, Prev: Appending, Up: Using Variables
755 The `override' Directive
756 ========================
758 If a variable has been set with a command argument (*note Overriding
759 Variables: Overriding.), then ordinary assignments in the makefile are
760 ignored. If you want to set the variable in the makefile even though
761 it was set with a command argument, you can use an `override'
762 directive, which is a line that looks like this:
764 override VARIABLE = VALUE
768 override VARIABLE := VALUE
770 To append more text to a variable defined on the command line, use:
772 override VARIABLE += MORE TEXT
774 *Note Appending More Text to Variables: Appending.
776 The `override' directive was not invented for escalation in the war
777 between makefiles and command arguments. It was invented so you can
778 alter and add to values that the user specifies with command arguments.
780 For example, suppose you always want the `-g' switch when you run the
781 C compiler, but you would like to allow the user to specify the other
782 switches with a command argument just as usual. You could use this
783 `override' directive:
785 override CFLAGS += -g
787 You can also use `override' directives with `define' directives.
788 This is done as you might expect:
794 *Note Defining Variables Verbatim: Defining.
797 File: make.info, Node: Defining, Next: Environment, Prev: Override Directive, Up: Using Variables
799 Defining Variables Verbatim
800 ===========================
802 Another way to set the value of a variable is to use the `define'
803 directive. This directive has an unusual syntax which allows newline
804 characters to be included in the value, which is convenient for defining
805 both canned sequences of commands (*note Defining Canned Command
806 Sequences: Sequences.), and also sections of makefile syntax to use
807 with `eval' (*note Eval Function::).
809 The `define' directive is followed on the same line by the name of
810 the variable and nothing more. The value to give the variable appears
811 on the following lines. The end of the value is marked by a line
812 containing just the word `endef'. Aside from this difference in
813 syntax, `define' works just like `=': it creates a recursively-expanded
814 variable (*note The Two Flavors of Variables: Flavors.). The variable
815 name may contain function and variable references, which are expanded
816 when the directive is read to find the actual variable name to use.
818 You may nest `define' directives: `make' will keep track of nested
819 directives and report an error if they are not all properly closed with
820 `endef'. Note that lines beginning with tab characters are considered
821 part of a command script, so any `define' or `endef' strings appearing
822 on such a line will not be considered `make' operators.
829 The value in an ordinary assignment cannot contain a newline; but the
830 newlines that separate the lines of the value in a `define' become part
831 of the variable's value (except for the final newline which precedes
832 the `endef' and is not considered part of the value).
834 When used in a command script, the previous example is functionally
837 two-lines = echo foo; echo $(bar)
839 since two commands separated by semicolon behave much like two separate
840 shell commands. However, note that using two separate lines means
841 `make' will invoke the shell twice, running an independent subshell for
842 each line. *Note Command Execution: Execution.
844 If you want variable definitions made with `define' to take
845 precedence over command-line variable definitions, you can use the
846 `override' directive together with `define':
848 override define two-lines
853 *Note The `override' Directive: Override Directive.
856 File: make.info, Node: Environment, Next: Target-specific, Prev: Defining, Up: Using Variables
858 Variables from the Environment
859 ==============================
861 Variables in `make' can come from the environment in which `make' is
862 run. Every environment variable that `make' sees when it starts up is
863 transformed into a `make' variable with the same name and value. But
864 an explicit assignment in the makefile, or with a command argument,
865 overrides the environment. (If the `-e' flag is specified, then values
866 from the environment override assignments in the makefile. *Note
867 Summary of Options: Options Summary. But this is not recommended
870 Thus, by setting the variable `CFLAGS' in your environment, you can
871 cause all C compilations in most makefiles to use the compiler switches
872 you prefer. This is safe for variables with standard or conventional
873 meanings because you know that no makefile will use them for other
874 things. (But this is not totally reliable; some makefiles set `CFLAGS'
875 explicitly and therefore are not affected by the value in the
878 When `make' is invoked recursively, variables defined in the outer
879 invocation can be passed to inner invocations through the environment
880 (*note Recursive Use of `make': Recursion.). By default, only
881 variables that came from the environment or the command line are passed
882 to recursive invocations. You can use the `export' directive to pass
883 other variables. *Note Communicating Variables to a Sub-`make':
884 Variables/Recursion, for full details.
886 Other use of variables from the environment is not recommended. It
887 is not wise for makefiles to depend for their functioning on
888 environment variables set up outside their control, since this would
889 cause different users to get different results from the same makefile.
890 This is against the whole purpose of most makefiles.
892 Such problems would be especially likely with the variable `SHELL',
893 which is normally present in the environment to specify the user's
894 choice of interactive shell. It would be very undesirable for this
895 choice to affect `make'. So `make' ignores the environment value of
896 `SHELL' (except on MS-DOS and MS-Windows, where `SHELL' is usually not
897 set. *Note Special handling of SHELL on MS-DOS: Execution.)
900 File: make.info, Node: Target-specific, Next: Pattern-specific, Prev: Environment, Up: Using Variables
902 Target-specific Variable Values
903 ===============================
905 Variable values in `make' are usually global; that is, they are the
906 same regardless of where they are evaluated (unless they're reset, of
907 course). One exception to that is automatic variables (*note Automatic
908 Variables: Automatic.).
910 The other exception is "target-specific variable values". This
911 feature allows you to define different values for the same variable,
912 based on the target that `make' is currently building. As with
913 automatic variables, these values are only available within the context
914 of a target's command script (and in other target-specific assignments).
916 Set a target-specific variable value like this:
918 TARGET ... : VARIABLE-ASSIGNMENT
922 TARGET ... : override VARIABLE-ASSIGNMENT
924 Multiple TARGET values create a target-specific variable value for
925 each member of the target list individually.
927 The VARIABLE-ASSIGNMENT can be any valid form of assignment;
928 recursive (`='), static (`:='), appending (`+='), or conditional
929 (`?='). All variables that appear within the VARIABLE-ASSIGNMENT are
930 evaluated within the context of the target: thus, any
931 previously-defined target-specific variable values will be in effect.
932 Note that this variable is actually distinct from any "global" value:
933 the two variables do not have to have the same flavor (recursive vs.
936 Target-specific variables have the same priority as any other
937 makefile variable. Variables provided on the command-line (and in the
938 environment if the `-e' option is in force) will take precedence.
939 Specifying the `override' directive will allow the target-specific
940 variable value to be preferred.
942 There is one more special feature of target-specific variables: when
943 you define a target-specific variable, that variable value is also in
944 effect for all prerequisites of this target (unless those prerequisites
945 override it with their own target-specific variable value). So, for
946 example, a statement like this:
949 prog : prog.o foo.o bar.o
951 will set `CFLAGS' to `-g' in the command script for `prog', but it will
952 also set `CFLAGS' to `-g' in the command scripts that create `prog.o',
953 `foo.o', and `bar.o', and any command scripts which create their
957 File: make.info, Node: Pattern-specific, Prev: Target-specific, Up: Using Variables
959 Pattern-specific Variable Values
960 ================================
962 In addition to target-specific variable values (*note
963 Target-specific Variable Values: Target-specific.), GNU `make' supports
964 pattern-specific variable values. In this form, a variable is defined
965 for any target that matches the pattern specified. Variables defined in
966 this way are searched after any target-specific variables defined
967 explicitly for that target, and before target-specific variables defined
968 for the parent target.
970 Set a pattern-specific variable value like this:
972 PATTERN ... : VARIABLE-ASSIGNMENT
976 PATTERN ... : override VARIABLE-ASSIGNMENT
978 where PATTERN is a %-pattern. As with target-specific variable values,
979 multiple PATTERN values create a pattern-specific variable value for
980 each pattern individually. The VARIABLE-ASSIGNMENT can be any valid
981 form of assignment. Any command-line variable setting will take
982 precedence, unless `override' is specified.
988 will assign `CFLAGS' the value of `-O' for all targets matching the
992 File: make.info, Node: Conditionals, Next: Functions, Prev: Using Variables, Up: Top
994 Conditional Parts of Makefiles
995 ******************************
997 A "conditional" causes part of a makefile to be obeyed or ignored
998 depending on the values of variables. Conditionals can compare the
999 value of one variable to another, or the value of a variable to a
1000 constant string. Conditionals control what `make' actually "sees" in
1001 the makefile, so they _cannot_ be used to control shell commands at the
1006 * Conditional Example:: Example of a conditional
1007 * Conditional Syntax:: The syntax of conditionals.
1008 * Testing Flags:: Conditionals that test flags.
1011 File: make.info, Node: Conditional Example, Next: Conditional Syntax, Prev: Conditionals, Up: Conditionals
1013 Example of a Conditional
1014 ========================
1016 The following example of a conditional tells `make' to use one set
1017 of libraries if the `CC' variable is `gcc', and a different set of
1018 libraries otherwise. It works by controlling which of two command
1019 lines will be used as the command for a rule. The result is that
1020 `CC=gcc' as an argument to `make' changes not only which compiler is
1021 used but also which libraries are linked.
1023 libs_for_gcc = -lgnu
1028 $(CC) -o foo $(objects) $(libs_for_gcc)
1030 $(CC) -o foo $(objects) $(normal_libs)
1033 This conditional uses three directives: one `ifeq', one `else' and
1036 The `ifeq' directive begins the conditional, and specifies the
1037 condition. It contains two arguments, separated by a comma and
1038 surrounded by parentheses. Variable substitution is performed on both
1039 arguments and then they are compared. The lines of the makefile
1040 following the `ifeq' are obeyed if the two arguments match; otherwise
1043 The `else' directive causes the following lines to be obeyed if the
1044 previous conditional failed. In the example above, this means that the
1045 second alternative linking command is used whenever the first
1046 alternative is not used. It is optional to have an `else' in a
1049 The `endif' directive ends the conditional. Every conditional must
1050 end with an `endif'. Unconditional makefile text follows.
1052 As this example illustrates, conditionals work at the textual level:
1053 the lines of the conditional are treated as part of the makefile, or
1054 ignored, according to the condition. This is why the larger syntactic
1055 units of the makefile, such as rules, may cross the beginning or the
1056 end of the conditional.
1058 When the variable `CC' has the value `gcc', the above example has
1062 $(CC) -o foo $(objects) $(libs_for_gcc)
1064 When the variable `CC' has any other value, the effect is this:
1067 $(CC) -o foo $(objects) $(normal_libs)
1069 Equivalent results can be obtained in another way by
1070 conditionalizing a variable assignment and then using the variable
1073 libs_for_gcc = -lgnu
1077 libs=$(libs_for_gcc)
1083 $(CC) -o foo $(objects) $(libs)
1086 File: make.info, Node: Conditional Syntax, Next: Testing Flags, Prev: Conditional Example, Up: Conditionals
1088 Syntax of Conditionals
1089 ======================
1091 The syntax of a simple conditional with no `else' is as follows:
1093 CONDITIONAL-DIRECTIVE
1097 The TEXT-IF-TRUE may be any lines of text, to be considered as part of
1098 the makefile if the condition is true. If the condition is false, no
1099 text is used instead.
1101 The syntax of a complex conditional is as follows:
1103 CONDITIONAL-DIRECTIVE
1109 If the condition is true, TEXT-IF-TRUE is used; otherwise,
1110 TEXT-IF-FALSE is used instead. The TEXT-IF-FALSE can be any number of
1113 The syntax of the CONDITIONAL-DIRECTIVE is the same whether the
1114 conditional is simple or complex. There are four different directives
1115 that test different conditions. Here is a table of them:
1118 `ifeq 'ARG1' 'ARG2''
1119 `ifeq "ARG1" "ARG2"'
1120 `ifeq "ARG1" 'ARG2''
1121 `ifeq 'ARG1' "ARG2"'
1122 Expand all variable references in ARG1 and ARG2 and compare them.
1123 If they are identical, the TEXT-IF-TRUE is effective; otherwise,
1124 the TEXT-IF-FALSE, if any, is effective.
1126 Often you want to test if a variable has a non-empty value. When
1127 the value results from complex expansions of variables and
1128 functions, expansions you would consider empty may actually
1129 contain whitespace characters and thus are not seen as empty.
1130 However, you can use the `strip' function (*note Text Functions::)
1131 to avoid interpreting whitespace as a non-empty value. For
1134 ifeq ($(strip $(foo)),)
1138 will evaluate TEXT-IF-EMPTY even if the expansion of `$(foo)'
1139 contains whitespace characters.
1141 `ifneq (ARG1, ARG2)'
1142 `ifneq 'ARG1' 'ARG2''
1143 `ifneq "ARG1" "ARG2"'
1144 `ifneq "ARG1" 'ARG2''
1145 `ifneq 'ARG1' "ARG2"'
1146 Expand all variable references in ARG1 and ARG2 and compare them.
1147 If they are different, the TEXT-IF-TRUE is effective; otherwise,
1148 the TEXT-IF-FALSE, if any, is effective.
1150 `ifdef VARIABLE-NAME'
1151 If the variable VARIABLE-NAME has a non-empty value, the
1152 TEXT-IF-TRUE is effective; otherwise, the TEXT-IF-FALSE, if any,
1153 is effective. Variables that have never been defined have an
1154 empty value. The variable VARIABLE-NAME is itself expanded, so it
1155 could be a variable or function that expands to the name of a
1158 Note that `ifdef' only tests whether a variable has a value. It
1159 does not expand the variable to see if that value is nonempty.
1160 Consequently, tests using `ifdef' return true for all definitions
1161 except those like `foo ='. To test for an empty value, use
1162 `ifeq ($(foo),)'. For example,
1172 sets `frobozz' to `yes', while:
1181 sets `frobozz' to `no'.
1183 `ifndef VARIABLE-NAME'
1184 If the variable VARIABLE-NAME has an empty value, the TEXT-IF-TRUE
1185 is effective; otherwise, the TEXT-IF-FALSE, if any, is effective.
1187 Extra spaces are allowed and ignored at the beginning of the
1188 conditional directive line, but a tab is not allowed. (If the line
1189 begins with a tab, it will be considered a command for a rule.) Aside
1190 from this, extra spaces or tabs may be inserted with no effect anywhere
1191 except within the directive name or within an argument. A comment
1192 starting with `#' may appear at the end of the line.
1194 The other two directives that play a part in a conditional are `else'
1195 and `endif'. Each of these directives is written as one word, with no
1196 arguments. Extra spaces are allowed and ignored at the beginning of the
1197 line, and spaces or tabs at the end. A comment starting with `#' may
1198 appear at the end of the line.
1200 Conditionals affect which lines of the makefile `make' uses. If the
1201 condition is true, `make' reads the lines of the TEXT-IF-TRUE as part
1202 of the makefile; if the condition is false, `make' ignores those lines
1203 completely. It follows that syntactic units of the makefile, such as
1204 rules, may safely be split across the beginning or the end of the
1207 `make' evaluates conditionals when it reads a makefile.
1208 Consequently, you cannot use automatic variables in the tests of
1209 conditionals because they are not defined until commands are run (*note
1210 Automatic Variables: Automatic.).
1212 To prevent intolerable confusion, it is not permitted to start a
1213 conditional in one makefile and end it in another. However, you may
1214 write an `include' directive within a conditional, provided you do not
1215 attempt to terminate the conditional inside the included file.
1218 File: make.info, Node: Testing Flags, Prev: Conditional Syntax, Up: Conditionals
1220 Conditionals that Test Flags
1221 ============================
1223 You can write a conditional that tests `make' command flags such as
1224 `-t' by using the variable `MAKEFLAGS' together with the `findstring'
1225 function (*note Functions for String Substitution and Analysis: Text
1226 Functions.). This is useful when `touch' is not enough to make a file
1229 The `findstring' function determines whether one string appears as a
1230 substring of another. If you want to test for the `-t' flag, use `t'
1231 as the first string and the value of `MAKEFLAGS' as the other.
1233 For example, here is how to arrange to use `ranlib -t' to finish
1234 marking an archive file up to date:
1237 ifneq (,$(findstring t,$(MAKEFLAGS)))
1239 +ranlib -t archive.a
1244 The `+' prefix marks those command lines as "recursive" so that they
1245 will be executed despite use of the `-t' flag. *Note Recursive Use of
1249 File: make.info, Node: Functions, Next: Running, Prev: Conditionals, Up: Top
1251 Functions for Transforming Text
1252 *******************************
1254 "Functions" allow you to do text processing in the makefile to
1255 compute the files to operate on or the commands to use. You use a
1256 function in a "function call", where you give the name of the function
1257 and some text (the "arguments") for the function to operate on. The
1258 result of the function's processing is substituted into the makefile at
1259 the point of the call, just as a variable might be substituted.
1263 * Syntax of Functions:: How to write a function call.
1264 * Text Functions:: General-purpose text manipulation functions.
1265 * File Name Functions:: Functions for manipulating file names.
1266 * Foreach Function:: Repeat some text with controlled variation.
1267 * If Function:: Conditionally expand a value.
1268 * Call Function:: Expand a user-defined function.
1269 * Value Function:: Return the un-expanded value of a variable.
1270 * Eval Function:: Evaluate the arguments as makefile syntax.
1271 * Origin Function:: Find where a variable got its value.
1272 * Shell Function:: Substitute the output of a shell command.
1273 * Make Control Functions:: Functions that control how make runs.