source: trunk/third/gmake/make.info-4 @ 15972

Revision 15972, 48.1 KB checked in by ghudson, 24 years ago (diff)
This commit was generated by cvs2svn to compensate for changes in r15971, which included commits to RCS files with non-trunk default branches.
Line 
1This is make.info, produced by makeinfo version 4.0 from make.texinfo.
2
3INFO-DIR-SECTION GNU Packages
4START-INFO-DIR-ENTRY
5* Make: (make).            Remake files automatically.
6END-INFO-DIR-ENTRY
7
8   This file documents the GNU Make utility, which determines
9automatically which pieces of a large program need to be recompiled,
10and issues the commands to recompile them.
11
12   This is Edition 0.55, last updated 04 April 2000, of `The GNU Make
13Manual', for `make', Version 3.79.
14
15   Copyright (C) 1988, '89, '90, '91, '92, '93, '94, '95, '96, '97,
16'98, '99, 2000         Free Software Foundation, Inc.
17
18   Permission is granted to make and distribute verbatim copies of this
19manual provided the copyright notice and this permission notice are
20preserved on all copies.
21
22   Permission is granted to copy and distribute modified versions of
23this manual under the conditions for verbatim copying, provided that
24the entire resulting derived work is distributed under the terms of a
25permission notice identical to this one.
26
27   Permission is granted to copy and distribute translations of this
28manual into another language, under the above conditions for modified
29versions, except that this permission notice may be stated in a
30translation approved by the Free Software Foundation.
31
32
33File: make.info,  Node: Flavors,  Next: Advanced,  Prev: Reference,  Up: Using Variables
34
35The Two Flavors of Variables
36============================
37
38   There are two ways that a variable in GNU `make' can have a value;
39we call them the two "flavors" of variables.  The two flavors are
40distinguished in how they are defined and in what they do when expanded.
41
42   The first flavor of variable is a "recursively expanded" variable.
43Variables of this sort are defined by lines using `=' (*note Setting
44Variables: Setting.) or by the `define' directive (*note Defining
45Variables Verbatim: Defining.).  The value you specify is installed
46verbatim; if it contains references to other variables, these
47references are expanded whenever this variable is substituted (in the
48course of expanding some other string).  When this happens, it is
49called "recursive expansion".
50
51   For example,
52
53     foo = $(bar)
54     bar = $(ugh)
55     ugh = Huh?
56     
57     all:;echo $(foo)
58
59will echo `Huh?': `$(foo)' expands to `$(bar)' which expands to
60`$(ugh)' which finally expands to `Huh?'.
61
62   This flavor of variable is the only sort supported by other versions
63of `make'.  It has its advantages and its disadvantages.  An advantage
64(most would say) is that:
65
66     CFLAGS = $(include_dirs) -O
67     include_dirs = -Ifoo -Ibar
68
69will do what was intended: when `CFLAGS' is expanded in a command, it
70will expand to `-Ifoo -Ibar -O'.  A major disadvantage is that you
71cannot append something on the end of a variable, as in
72
73     CFLAGS = $(CFLAGS) -O
74
75because it will cause an infinite loop in the variable expansion.
76(Actually `make' detects the infinite loop and reports an error.)
77
78   Another disadvantage is that any functions (*note Functions for
79Transforming Text: Functions.)  referenced in the definition will be
80executed every time the variable is expanded.  This makes `make' run
81slower; worse, it causes the `wildcard' and `shell' functions to give
82unpredictable results because you cannot easily control when they are
83called, or even how many times.
84
85   To avoid all the problems and inconveniences of recursively expanded
86variables, there is another flavor: simply expanded variables.
87
88   "Simply expanded variables" are defined by lines using `:=' (*note
89Setting Variables: Setting.).  The value of a simply expanded variable
90is scanned once and for all, expanding any references to other
91variables and functions, when the variable is defined.  The actual
92value of the simply expanded variable is the result of expanding the
93text that you write.  It does not contain any references to other
94variables; it contains their values _as of the time this variable was
95defined_.  Therefore,
96
97     x := foo
98     y := $(x) bar
99     x := later
100
101is equivalent to
102
103     y := foo bar
104     x := later
105
106   When a simply expanded variable is referenced, its value is
107substituted verbatim.
108
109   Here is a somewhat more complicated example, illustrating the use of
110`:=' in conjunction with the `shell' function.  (*Note The `shell'
111Function: Shell Function.)  This example also shows use of the variable
112`MAKELEVEL', which is changed when it is passed down from level to
113level.  (*Note Communicating Variables to a Sub-`make':
114Variables/Recursion, for information about `MAKELEVEL'.)
115
116     ifeq (0,${MAKELEVEL})
117     cur-dir   := $(shell pwd)
118     whoami    := $(shell whoami)
119     host-type := $(shell arch)
120     MAKE := ${MAKE} host-type=${host-type} whoami=${whoami}
121     endif
122
123An advantage of this use of `:=' is that a typical `descend into a
124directory' command then looks like this:
125
126     ${subdirs}:
127           ${MAKE} cur-dir=${cur-dir}/$@ -C $@ all
128
129   Simply expanded variables generally make complicated makefile
130programming more predictable because they work like variables in most
131programming languages.  They allow you to redefine a variable using its
132own value (or its value processed in some way by one of the expansion
133functions) and to use the expansion functions much more efficiently
134(*note Functions for Transforming Text: Functions.).
135
136   You can also use them to introduce controlled leading whitespace into
137variable values.  Leading whitespace characters are discarded from your
138input before substitution of variable references and function calls;
139this means you can include leading spaces in a variable value by
140protecting them with variable references, like this:
141
142     nullstring :=
143     space := $(nullstring) # end of the line
144
145Here the value of the variable `space' is precisely one space.  The
146comment `# end of the line' is included here just for clarity.  Since
147trailing space characters are _not_ stripped from variable values, just
148a space at the end of the line would have the same effect (but be
149rather hard to read).  If you put whitespace at the end of a variable
150value, it is a good idea to put a comment like that at the end of the
151line to make your intent clear.  Conversely, if you do _not_ want any
152whitespace characters at the end of your variable value, you must
153remember not to put a random comment on the end of the line after some
154whitespace, such as this:
155
156     dir := /foo/bar    # directory to put the frobs in
157
158Here the value of the variable `dir' is `/foo/bar    ' (with four
159trailing spaces), which was probably not the intention.  (Imagine
160something like `$(dir)/file' with this definition!)
161
162   There is another assignment operator for variables, `?='.  This is
163called a conditional variable assignment operator, because it only has
164an effect if the variable is not yet defined.  This statement:
165
166     FOO ?= bar
167
168is exactly equivalent to this (*note The `origin' Function: Origin
169Function.):
170
171     ifeq ($(origin FOO), undefined)
172       FOO = bar
173     endif
174
175   Note that a variable set to an empty value is still defined, so `?='
176will not set that variable.
177
178
179File: make.info,  Node: Advanced,  Next: Values,  Prev: Flavors,  Up: Using Variables
180
181Advanced Features for Reference to Variables
182============================================
183
184   This section describes some advanced features you can use to
185reference variables in more flexible ways.
186
187* Menu:
188
189* Substitution Refs::           Referencing a variable with
190                                  substitutions on the value.
191* Computed Names::              Computing the name of the variable to refer to.
192
193
194File: make.info,  Node: Substitution Refs,  Next: Computed Names,  Up: Advanced
195
196Substitution References
197-----------------------
198
199   A "substitution reference" substitutes the value of a variable with
200alterations that you specify.  It has the form `$(VAR:A=B)' (or
201`${VAR:A=B}') and its meaning is to take the value of the variable VAR,
202replace every A at the end of a word with B in that value, and
203substitute the resulting string.
204
205   When we say "at the end of a word", we mean that A must appear
206either followed by whitespace or at the end of the value in order to be
207replaced; other occurrences of A in the value are unaltered.  For
208example:
209
210     foo := a.o b.o c.o
211     bar := $(foo:.o=.c)
212
213sets `bar' to `a.c b.c c.c'.  *Note Setting Variables: Setting.
214
215   A substitution reference is actually an abbreviation for use of the
216`patsubst' expansion function (*note Functions for String Substitution
217and Analysis: Text Functions.).  We provide substitution references as
218well as `patsubst' for compatibility with other implementations of
219`make'.
220
221   Another type of substitution reference lets you use the full power of
222the `patsubst' function.  It has the same form `$(VAR:A=B)' described
223above, except that now A must contain a single `%' character.  This
224case is equivalent to `$(patsubst A,B,$(VAR))'.  *Note Functions for
225String Substitution and Analysis: Text Functions, for a description of
226the `patsubst' function.
227
228For example:
229
230     foo := a.o b.o c.o
231     bar := $(foo:%.o=%.c)
232
233sets `bar' to `a.c b.c c.c'.
234
235
236File: make.info,  Node: Computed Names,  Prev: Substitution Refs,  Up: Advanced
237
238Computed Variable Names
239-----------------------
240
241   Computed variable names are a complicated concept needed only for
242sophisticated makefile programming.  For most purposes you need not
243consider them, except to know that making a variable with a dollar sign
244in its name might have strange results.  However, if you are the type
245that wants to understand everything, or you are actually interested in
246what they do, read on.
247
248   Variables may be referenced inside the name of a variable.  This is
249called a "computed variable name" or a "nested variable reference".
250For example,
251
252     x = y
253     y = z
254     a := $($(x))
255
256defines `a' as `z': the `$(x)' inside `$($(x))' expands to `y', so
257`$($(x))' expands to `$(y)' which in turn expands to `z'.  Here the
258name of the variable to reference is not stated explicitly; it is
259computed by expansion of `$(x)'.  The reference `$(x)' here is nested
260within the outer variable reference.
261
262   The previous example shows two levels of nesting, but any number of
263levels is possible.  For example, here are three levels:
264
265     x = y
266     y = z
267     z = u
268     a := $($($(x)))
269
270Here the innermost `$(x)' expands to `y', so `$($(x))' expands to
271`$(y)' which in turn expands to `z'; now we have `$(z)', which becomes
272`u'.
273
274   References to recursively-expanded variables within a variable name
275are reexpanded in the usual fashion.  For example:
276
277     x = $(y)
278     y = z
279     z = Hello
280     a := $($(x))
281
282defines `a' as `Hello': `$($(x))' becomes `$($(y))' which becomes
283`$(z)' which becomes `Hello'.
284
285   Nested variable references can also contain modified references and
286function invocations (*note Functions for Transforming Text:
287Functions.), just like any other reference.  For example, using the
288`subst' function (*note Functions for String Substitution and Analysis:
289Text Functions.):
290
291     x = variable1
292     variable2 := Hello
293     y = $(subst 1,2,$(x))
294     z = y
295     a := $($($(z)))
296
297eventually defines `a' as `Hello'.  It is doubtful that anyone would
298ever want to write a nested reference as convoluted as this one, but it
299works: `$($($(z)))' expands to `$($(y))' which becomes `$($(subst
3001,2,$(x)))'.  This gets the value `variable1' from `x' and changes it
301by substitution to `variable2', so that the entire string becomes
302`$(variable2)', a simple variable reference whose value is `Hello'.
303
304   A computed variable name need not consist entirely of a single
305variable reference.  It can contain several variable references, as
306well as some invariant text.  For example,
307
308     a_dirs := dira dirb
309     1_dirs := dir1 dir2
310     
311     a_files := filea fileb
312     1_files := file1 file2
313     
314     ifeq "$(use_a)" "yes"
315     a1 := a
316     else
317     a1 := 1
318     endif
319     
320     ifeq "$(use_dirs)" "yes"
321     df := dirs
322     else
323     df := files
324     endif
325     
326     dirs := $($(a1)_$(df))
327
328will give `dirs' the same value as `a_dirs', `1_dirs', `a_files' or
329`1_files' depending on the settings of `use_a' and `use_dirs'.
330
331   Computed variable names can also be used in substitution references:
332
333     a_objects := a.o b.o c.o
334     1_objects := 1.o 2.o 3.o
335     
336     sources := $($(a1)_objects:.o=.c)
337
338defines `sources' as either `a.c b.c c.c' or `1.c 2.c 3.c', depending
339on the value of `a1'.
340
341   The only restriction on this sort of use of nested variable
342references is that they cannot specify part of the name of a function
343to be called.  This is because the test for a recognized function name
344is done before the expansion of nested references.  For example,
345
346     ifdef do_sort
347     func := sort
348     else
349     func := strip
350     endif
351     
352     bar := a d b g q c
353     
354     foo := $($(func) $(bar))
355
356attempts to give `foo' the value of the variable `sort a d b g q c' or
357`strip a d b g q c', rather than giving `a d b g q c' as the argument
358to either the `sort' or the `strip' function.  This restriction could
359be removed in the future if that change is shown to be a good idea.
360
361   You can also use computed variable names in the left-hand side of a
362variable assignment, or in a `define' directive, as in:
363
364     dir = foo
365     $(dir)_sources := $(wildcard $(dir)/*.c)
366     define $(dir)_print
367     lpr $($(dir)_sources)
368     endef
369
370This example defines the variables `dir', `foo_sources', and
371`foo_print'.
372
373   Note that "nested variable references" are quite different from
374"recursively expanded variables" (*note The Two Flavors of Variables:
375Flavors.), though both are used together in complex ways when doing
376makefile programming.
377
378
379File: make.info,  Node: Values,  Next: Setting,  Prev: Advanced,  Up: Using Variables
380
381How Variables Get Their Values
382==============================
383
384   Variables can get values in several different ways:
385
386   * You can specify an overriding value when you run `make'.  *Note
387     Overriding Variables: Overriding.
388
389   * You can specify a value in the makefile, either with an assignment
390     (*note Setting Variables: Setting.) or with a verbatim definition
391     (*note Defining Variables Verbatim: Defining.).
392
393   * Variables in the environment become `make' variables.  *Note
394     Variables from the Environment: Environment.
395
396   * Several "automatic" variables are given new values for each rule.
397     Each of these has a single conventional use.  *Note Automatic
398     Variables: Automatic.
399
400   * Several variables have constant initial values.  *Note Variables
401     Used by Implicit Rules: Implicit Variables.
402
403
404File: make.info,  Node: Setting,  Next: Appending,  Prev: Values,  Up: Using Variables
405
406Setting Variables
407=================
408
409   To set a variable from the makefile, write a line starting with the
410variable name followed by `=' or `:='.  Whatever follows the `=' or
411`:=' on the line becomes the value.  For example,
412
413     objects = main.o foo.o bar.o utils.o
414
415defines a variable named `objects'.  Whitespace around the variable
416name and immediately after the `=' is ignored.
417
418   Variables defined with `=' are "recursively expanded" variables.
419Variables defined with `:=' are "simply expanded" variables; these
420definitions can contain variable references which will be expanded
421before the definition is made.  *Note The Two Flavors of Variables:
422Flavors.
423
424   The variable name may contain function and variable references, which
425are expanded when the line is read to find the actual variable name to
426use.
427
428   There is no limit on the length of the value of a variable except the
429amount of swapping space on the computer.  When a variable definition is
430long, it is a good idea to break it into several lines by inserting
431backslash-newline at convenient places in the definition.  This will not
432affect the functioning of `make', but it will make the makefile easier
433to read.
434
435   Most variable names are considered to have the empty string as a
436value if you have never set them.  Several variables have built-in
437initial values that are not empty, but you can set them in the usual
438ways (*note Variables Used by Implicit Rules: Implicit Variables.).
439Several special variables are set automatically to a new value for each
440rule; these are called the "automatic" variables (*note Automatic
441Variables: Automatic.).
442
443   If you'd like a variable to be set to a value only if it's not
444already set, then you can use the shorthand operator `?=' instead of
445`='.  These two settings of the variable `FOO' are identical (*note The
446`origin' Function: Origin Function.):
447
448     FOO ?= bar
449
450and
451
452     ifeq ($(origin FOO), undefined)
453     FOO = bar
454     endif
455
456
457File: make.info,  Node: Appending,  Next: Override Directive,  Prev: Setting,  Up: Using Variables
458
459Appending More Text to Variables
460================================
461
462   Often it is useful to add more text to the value of a variable
463already defined.  You do this with a line containing `+=', like this:
464
465     objects += another.o
466
467This takes the value of the variable `objects', and adds the text
468`another.o' to it (preceded by a single space).  Thus:
469
470     objects = main.o foo.o bar.o utils.o
471     objects += another.o
472
473sets `objects' to `main.o foo.o bar.o utils.o another.o'.
474
475   Using `+=' is similar to:
476
477     objects = main.o foo.o bar.o utils.o
478     objects := $(objects) another.o
479
480but differs in ways that become important when you use more complex
481values.
482
483   When the variable in question has not been defined before, `+=' acts
484just like normal `=': it defines a recursively-expanded variable.
485However, when there _is_ a previous definition, exactly what `+=' does
486depends on what flavor of variable you defined originally.  *Note The
487Two Flavors of Variables: Flavors, for an explanation of the two
488flavors of variables.
489
490   When you add to a variable's value with `+=', `make' acts
491essentially as if you had included the extra text in the initial
492definition of the variable.  If you defined it first with `:=', making
493it a simply-expanded variable, `+=' adds to that simply-expanded
494definition, and expands the new text before appending it to the old
495value just as `:=' does (*note Setting Variables: Setting., for a full
496explanation of `:=').  In fact,
497
498     variable := value
499     variable += more
500
501is exactly equivalent to:
502
503     variable := value
504     variable := $(variable) more
505
506   On the other hand, when you use `+=' with a variable that you defined
507first to be recursively-expanded using plain `=', `make' does something
508a bit different.  Recall that when you define a recursively-expanded
509variable, `make' does not expand the value you set for variable and
510function references immediately.  Instead it stores the text verbatim,
511and saves these variable and function references to be expanded later,
512when you refer to the new variable (*note The Two Flavors of Variables:
513Flavors.).  When you use `+=' on a recursively-expanded variable, it is
514this unexpanded text to which `make' appends the new text you specify.
515
516     variable = value
517     variable += more
518
519is roughly equivalent to:
520
521     temp = value
522     variable = $(temp) more
523
524except that of course it never defines a variable called `temp'.  The
525importance of this comes when the variable's old value contains
526variable references.  Take this common example:
527
528     CFLAGS = $(includes) -O
529     ...
530     CFLAGS += -pg # enable profiling
531
532The first line defines the `CFLAGS' variable with a reference to another
533variable, `includes'.  (`CFLAGS' is used by the rules for C
534compilation; *note Catalogue of Implicit Rules: Catalogue of Rules..)
535Using `=' for the definition makes `CFLAGS' a recursively-expanded
536variable, meaning `$(includes) -O' is _not_ expanded when `make'
537processes the definition of `CFLAGS'.  Thus, `includes' need not be
538defined yet for its value to take effect.  It only has to be defined
539before any reference to `CFLAGS'.  If we tried to append to the value
540of `CFLAGS' without using `+=', we might do it like this:
541
542     CFLAGS := $(CFLAGS) -pg # enable profiling
543
544This is pretty close, but not quite what we want.  Using `:=' redefines
545`CFLAGS' as a simply-expanded variable; this means `make' expands the
546text `$(CFLAGS) -pg' before setting the variable.  If `includes' is not
547yet defined, we get ` -O -pg', and a later definition of `includes'
548will have no effect.  Conversely, by using `+=' we set `CFLAGS' to the
549_unexpanded_ value `$(includes) -O -pg'.  Thus we preserve the
550reference to `includes', so if that variable gets defined at any later
551point, a reference like `$(CFLAGS)' still uses its value.
552
553
554File: make.info,  Node: Override Directive,  Next: Defining,  Prev: Appending,  Up: Using Variables
555
556The `override' Directive
557========================
558
559   If a variable has been set with a command argument (*note Overriding
560Variables: Overriding.), then ordinary assignments in the makefile are
561ignored.  If you want to set the variable in the makefile even though
562it was set with a command argument, you can use an `override'
563directive, which is a line that looks like this:
564
565     override VARIABLE = VALUE
566
567or
568
569     override VARIABLE := VALUE
570
571   To append more text to a variable defined on the command line, use:
572
573     override VARIABLE += MORE TEXT
574
575*Note Appending More Text to Variables: Appending.
576
577   The `override' directive was not invented for escalation in the war
578between makefiles and command arguments.  It was invented so you can
579alter and add to values that the user specifies with command arguments.
580
581   For example, suppose you always want the `-g' switch when you run the
582C compiler, but you would like to allow the user to specify the other
583switches with a command argument just as usual.  You could use this
584`override' directive:
585
586     override CFLAGS += -g
587
588   You can also use `override' directives with `define' directives.
589This is done as you might expect:
590
591     override define foo
592     bar
593     endef
594
595*Note Defining Variables Verbatim: Defining.
596
597
598File: make.info,  Node: Defining,  Next: Environment,  Prev: Override Directive,  Up: Using Variables
599
600Defining Variables Verbatim
601===========================
602
603Another way to set the value of a variable is to use the `define'
604directive.  This directive has an unusual syntax which allows newline
605characters to be included in the value, which is convenient for defining
606canned sequences of commands (*note Defining Canned Command Sequences:
607Sequences.).
608
609   The `define' directive is followed on the same line by the name of
610the variable and nothing more.  The value to give the variable appears
611on the following lines.  The end of the value is marked by a line
612containing just the word `endef'.  Aside from this difference in
613syntax, `define' works just like `=': it creates a recursively-expanded
614variable (*note The Two Flavors of Variables: Flavors.).  The variable
615name may contain function and variable references, which are expanded
616when the directive is read to find the actual variable name to use.
617
618     define two-lines
619     echo foo
620     echo $(bar)
621     endef
622
623   The value in an ordinary assignment cannot contain a newline; but the
624newlines that separate the lines of the value in a `define' become part
625of the variable's value (except for the final newline which precedes
626the `endef' and is not considered part of the value).
627
628   The previous example is functionally equivalent to this:
629
630     two-lines = echo foo; echo $(bar)
631
632since two commands separated by semicolon behave much like two separate
633shell commands.  However, note that using two separate lines means
634`make' will invoke the shell twice, running an independent subshell for
635each line.  *Note Command Execution: Execution.
636
637   If you want variable definitions made with `define' to take
638precedence over command-line variable definitions, you can use the
639`override' directive together with `define':
640
641     override define two-lines
642     foo
643     $(bar)
644     endef
645
646*Note The `override' Directive: Override Directive.
647
648
649File: make.info,  Node: Environment,  Next: Target-specific,  Prev: Defining,  Up: Using Variables
650
651Variables from the Environment
652==============================
653
654   Variables in `make' can come from the environment in which `make' is
655run.  Every environment variable that `make' sees when it starts up is
656transformed into a `make' variable with the same name and value.  But
657an explicit assignment in the makefile, or with a command argument,
658overrides the environment.  (If the `-e' flag is specified, then values
659from the environment override assignments in the makefile.  *Note
660Summary of Options: Options Summary.  But this is not recommended
661practice.)
662
663   Thus, by setting the variable `CFLAGS' in your environment, you can
664cause all C compilations in most makefiles to use the compiler switches
665you prefer.  This is safe for variables with standard or conventional
666meanings because you know that no makefile will use them for other
667things.  (But this is not totally reliable; some makefiles set `CFLAGS'
668explicitly and therefore are not affected by the value in the
669environment.)
670
671   When `make' is invoked recursively, variables defined in the outer
672invocation can be passed to inner invocations through the environment
673(*note Recursive Use of `make': Recursion.).  By default, only
674variables that came from the environment or the command line are passed
675to recursive invocations.  You can use the `export' directive to pass
676other variables.  *Note Communicating Variables to a Sub-`make':
677Variables/Recursion, for full details.
678
679   Other use of variables from the environment is not recommended.  It
680is not wise for makefiles to depend for their functioning on
681environment variables set up outside their control, since this would
682cause different users to get different results from the same makefile.
683This is against the whole purpose of most makefiles.
684
685   Such problems would be especially likely with the variable `SHELL',
686which is normally present in the environment to specify the user's
687choice of interactive shell.  It would be very undesirable for this
688choice to affect `make'.  So `make' ignores the environment value of
689`SHELL' (except on MS-DOS and MS-Windows, where `SHELL' is usually not
690set.  *Note Special handling of SHELL on MS-DOS: Execution.)
691
692
693File: make.info,  Node: Target-specific,  Next: Pattern-specific,  Prev: Environment,  Up: Using Variables
694
695Target-specific Variable Values
696===============================
697
698   Variable values in `make' are usually global; that is, they are the
699same regardless of where they are evaluated (unless they're reset, of
700course).  One exception to that is automatic variables (*note Automatic
701Variables: Automatic.).
702
703   The other exception is "target-specific variable values".  This
704feature allows you to define different values for the same variable,
705based on the target that `make' is currently building.  As with
706automatic variables, these values are only available within the context
707of a target's command script (and in other target-specific assignments).
708
709   Set a target-specific variable value like this:
710
711     TARGET ... : VARIABLE-ASSIGNMENT
712
713or like this:
714
715     TARGET ... : override VARIABLE-ASSIGNMENT
716
717   Multiple TARGET values create a target-specific variable value for
718each member of the target list individually.
719
720   The VARIABLE-ASSIGNMENT can be any valid form of assignment;
721recursive (`='), static (`:='), appending (`+='), or conditional
722(`?=').  All variables that appear within the VARIABLE-ASSIGNMENT are
723evaluated within the context of the target: thus, any
724previously-defined target-specific variable values will be in effect.
725Note that this variable is actually distinct from any "global" value:
726the two variables do not have to have the same flavor (recursive vs.
727static).
728
729   Target-specific variables have the same priority as any other
730makefile variable.  Variables provided on the command-line (and in the
731environment if the `-e' option is in force) will take precedence.
732Specifying the `override' directive will allow the target-specific
733variable value to be preferred.
734
735   There is one more special feature of target-specific variables: when
736you define a target-specific variable, that variable value is also in
737effect for all prerequisites of this target (unless those prerequisites
738override it with their own target-specific variable value).  So, for
739example, a statement like this:
740
741     prog : CFLAGS = -g
742     prog : prog.o foo.o bar.o
743
744will set `CFLAGS' to `-g' in the command script for `prog', but it will
745also set `CFLAGS' to `-g' in the command scripts that create `prog.o',
746`foo.o', and `bar.o', and any command scripts which create their
747prerequisites.
748
749
750File: make.info,  Node: Pattern-specific,  Prev: Target-specific,  Up: Using Variables
751
752Pattern-specific Variable Values
753================================
754
755   In addition to target-specific variable values (*note
756Target-specific Variable Values: Target-specific.), GNU `make' supports
757pattern-specific variable values.  In this form, a variable is defined
758for any target that matches the pattern specified.  Variables defined in
759this way are searched after any target-specific variables defined
760explicitly for that target, and before target-specific variables defined
761for the parent target.
762
763   Set a pattern-specific variable value like this:
764
765     PATTERN ... : VARIABLE-ASSIGNMENT
766
767or like this:
768
769     PATTERN ... : override VARIABLE-ASSIGNMENT
770
771where PATTERN is a %-pattern.  As with target-specific variable values,
772multiple PATTERN values create a pattern-specific variable value for
773each pattern individually.  The VARIABLE-ASSIGNMENT can be any valid
774form of assignment.  Any command-line variable setting will take
775precedence, unless `override' is specified.
776
777   For example:
778
779     %.o : CFLAGS = -O
780
781will assign `CFLAGS' the value of `-O' for all targets matching the
782pattern `%.o'.
783
784
785File: make.info,  Node: Conditionals,  Next: Functions,  Prev: Using Variables,  Up: Top
786
787Conditional Parts of Makefiles
788******************************
789
790   A "conditional" causes part of a makefile to be obeyed or ignored
791depending on the values of variables.  Conditionals can compare the
792value of one variable to another, or the value of a variable to a
793constant string.  Conditionals control what `make' actually "sees" in
794the makefile, so they _cannot_ be used to control shell commands at the
795time of execution.
796
797* Menu:
798
799* Conditional Example::         Example of a conditional
800* Conditional Syntax::          The syntax of conditionals.
801* Testing Flags::               Conditionals that test flags.
802
803
804File: make.info,  Node: Conditional Example,  Next: Conditional Syntax,  Up: Conditionals
805
806Example of a Conditional
807========================
808
809   The following example of a conditional tells `make' to use one set
810of libraries if the `CC' variable is `gcc', and a different set of
811libraries otherwise.  It works by controlling which of two command
812lines will be used as the command for a rule.  The result is that
813`CC=gcc' as an argument to `make' changes not only which compiler is
814used but also which libraries are linked.
815
816     libs_for_gcc = -lgnu
817     normal_libs =
818     
819     foo: $(objects)
820     ifeq ($(CC),gcc)
821             $(CC) -o foo $(objects) $(libs_for_gcc)
822     else
823             $(CC) -o foo $(objects) $(normal_libs)
824     endif
825
826   This conditional uses three directives: one `ifeq', one `else' and
827one `endif'.
828
829   The `ifeq' directive begins the conditional, and specifies the
830condition.  It contains two arguments, separated by a comma and
831surrounded by parentheses.  Variable substitution is performed on both
832arguments and then they are compared.  The lines of the makefile
833following the `ifeq' are obeyed if the two arguments match; otherwise
834they are ignored.
835
836   The `else' directive causes the following lines to be obeyed if the
837previous conditional failed.  In the example above, this means that the
838second alternative linking command is used whenever the first
839alternative is not used.  It is optional to have an `else' in a
840conditional.
841
842   The `endif' directive ends the conditional.  Every conditional must
843end with an `endif'.  Unconditional makefile text follows.
844
845   As this example illustrates, conditionals work at the textual level:
846the lines of the conditional are treated as part of the makefile, or
847ignored, according to the condition.  This is why the larger syntactic
848units of the makefile, such as rules, may cross the beginning or the
849end of the conditional.
850
851   When the variable `CC' has the value `gcc', the above example has
852this effect:
853
854     foo: $(objects)
855             $(CC) -o foo $(objects) $(libs_for_gcc)
856
857When the variable `CC' has any other value, the effect is this:
858
859     foo: $(objects)
860             $(CC) -o foo $(objects) $(normal_libs)
861
862   Equivalent results can be obtained in another way by
863conditionalizing a variable assignment and then using the variable
864unconditionally:
865
866     libs_for_gcc = -lgnu
867     normal_libs =
868     
869     ifeq ($(CC),gcc)
870       libs=$(libs_for_gcc)
871     else
872       libs=$(normal_libs)
873     endif
874     
875     foo: $(objects)
876             $(CC) -o foo $(objects) $(libs)
877
878
879File: make.info,  Node: Conditional Syntax,  Next: Testing Flags,  Prev: Conditional Example,  Up: Conditionals
880
881Syntax of Conditionals
882======================
883
884   The syntax of a simple conditional with no `else' is as follows:
885
886     CONDITIONAL-DIRECTIVE
887     TEXT-IF-TRUE
888     endif
889
890The TEXT-IF-TRUE may be any lines of text, to be considered as part of
891the makefile if the condition is true.  If the condition is false, no
892text is used instead.
893
894   The syntax of a complex conditional is as follows:
895
896     CONDITIONAL-DIRECTIVE
897     TEXT-IF-TRUE
898     else
899     TEXT-IF-FALSE
900     endif
901
902If the condition is true, TEXT-IF-TRUE is used; otherwise,
903TEXT-IF-FALSE is used instead.  The TEXT-IF-FALSE can be any number of
904lines of text.
905
906   The syntax of the CONDITIONAL-DIRECTIVE is the same whether the
907conditional is simple or complex.  There are four different directives
908that test different conditions.  Here is a table of them:
909
910`ifeq (ARG1, ARG2)'
911`ifeq 'ARG1' 'ARG2''
912`ifeq "ARG1" "ARG2"'
913`ifeq "ARG1" 'ARG2''
914`ifeq 'ARG1' "ARG2"'
915     Expand all variable references in ARG1 and ARG2 and compare them.
916     If they are identical, the TEXT-IF-TRUE is effective; otherwise,
917     the TEXT-IF-FALSE, if any, is effective.
918
919     Often you want to test if a variable has a non-empty value.  When
920     the value results from complex expansions of variables and
921     functions, expansions you would consider empty may actually
922     contain whitespace characters and thus are not seen as empty.
923     However, you can use the `strip' function (*note Text Functions::)
924     to avoid interpreting whitespace as a non-empty value.  For
925     example:
926
927          ifeq ($(strip $(foo)),)
928          TEXT-IF-EMPTY
929          endif
930
931     will evaluate TEXT-IF-EMPTY even if the expansion of `$(foo)'
932     contains whitespace characters.
933
934`ifneq (ARG1, ARG2)'
935`ifneq 'ARG1' 'ARG2''
936`ifneq "ARG1" "ARG2"'
937`ifneq "ARG1" 'ARG2''
938`ifneq 'ARG1' "ARG2"'
939     Expand all variable references in ARG1 and ARG2 and compare them.
940     If they are different, the TEXT-IF-TRUE is effective; otherwise,
941     the TEXT-IF-FALSE, if any, is effective.
942
943`ifdef VARIABLE-NAME'
944     If the variable VARIABLE-NAME has a non-empty value, the
945     TEXT-IF-TRUE is effective; otherwise, the TEXT-IF-FALSE, if any,
946     is effective.  Variables that have never been defined have an
947     empty value.
948
949     Note that `ifdef' only tests whether a variable has a value.  It
950     does not expand the variable to see if that value is nonempty.
951     Consequently, tests using `ifdef' return true for all definitions
952     except those like `foo ='.  To test for an empty value, use
953     `ifeq ($(foo),)'.  For example,
954
955          bar =
956          foo = $(bar)
957          ifdef foo
958          frobozz = yes
959          else
960          frobozz = no
961          endif
962
963     sets `frobozz' to `yes', while:
964
965          foo =
966          ifdef foo
967          frobozz = yes
968          else
969          frobozz = no
970          endif
971
972     sets `frobozz' to `no'.
973
974`ifndef VARIABLE-NAME'
975     If the variable VARIABLE-NAME has an empty value, the TEXT-IF-TRUE
976     is effective; otherwise, the TEXT-IF-FALSE, if any, is effective.
977
978   Extra spaces are allowed and ignored at the beginning of the
979conditional directive line, but a tab is not allowed.  (If the line
980begins with a tab, it will be considered a command for a rule.)  Aside
981from this, extra spaces or tabs may be inserted with no effect anywhere
982except within the directive name or within an argument.  A comment
983starting with `#' may appear at the end of the line.
984
985   The other two directives that play a part in a conditional are `else'
986and `endif'.  Each of these directives is written as one word, with no
987arguments.  Extra spaces are allowed and ignored at the beginning of the
988line, and spaces or tabs at the end.  A comment starting with `#' may
989appear at the end of the line.
990
991   Conditionals affect which lines of the makefile `make' uses.  If the
992condition is true, `make' reads the lines of the TEXT-IF-TRUE as part
993of the makefile; if the condition is false, `make' ignores those lines
994completely.  It follows that syntactic units of the makefile, such as
995rules, may safely be split across the beginning or the end of the
996conditional.
997
998   `make' evaluates conditionals when it reads a makefile.
999Consequently, you cannot use automatic variables in the tests of
1000conditionals because they are not defined until commands are run (*note
1001Automatic Variables: Automatic.).
1002
1003   To prevent intolerable confusion, it is not permitted to start a
1004conditional in one makefile and end it in another.  However, you may
1005write an `include' directive within a conditional, provided you do not
1006attempt to terminate the conditional inside the included file.
1007
1008
1009File: make.info,  Node: Testing Flags,  Prev: Conditional Syntax,  Up: Conditionals
1010
1011Conditionals that Test Flags
1012============================
1013
1014   You can write a conditional that tests `make' command flags such as
1015`-t' by using the variable `MAKEFLAGS' together with the `findstring'
1016function (*note Functions for String Substitution and Analysis: Text
1017Functions.).  This is useful when `touch' is not enough to make a file
1018appear up to date.
1019
1020   The `findstring' function determines whether one string appears as a
1021substring of another.  If you want to test for the `-t' flag, use `t'
1022as the first string and the value of `MAKEFLAGS' as the other.
1023
1024   For example, here is how to arrange to use `ranlib -t' to finish
1025marking an archive file up to date:
1026
1027     archive.a: ...
1028     ifneq (,$(findstring t,$(MAKEFLAGS)))
1029             +touch archive.a
1030             +ranlib -t archive.a
1031     else
1032             ranlib archive.a
1033     endif
1034
1035The `+' prefix marks those command lines as "recursive" so that they
1036will be executed despite use of the `-t' flag.  *Note Recursive Use of
1037`make': Recursion.
1038
1039
1040File: make.info,  Node: Functions,  Next: Running,  Prev: Conditionals,  Up: Top
1041
1042Functions for Transforming Text
1043*******************************
1044
1045   "Functions" allow you to do text processing in the makefile to
1046compute the files to operate on or the commands to use.  You use a
1047function in a "function call", where you give the name of the function
1048and some text (the "arguments") for the function to operate on.  The
1049result of the function's processing is substituted into the makefile at
1050the point of the call, just as a variable might be substituted.
1051
1052* Menu:
1053
1054* Syntax of Functions::         How to write a function call.
1055* Text Functions::              General-purpose text manipulation functions.
1056* File Name Functions::         Functions for manipulating file names.
1057* Foreach Function::            Repeat some text with controlled variation.
1058* If Function::                 Conditionally expand a value.
1059* Call Function::               Expand a user-defined function.
1060* Origin Function::             Find where a variable got its value.
1061* Shell Function::              Substitute the output of a shell command.
1062* Make Control Functions::      Functions that control how make runs.
1063
1064
1065File: make.info,  Node: Syntax of Functions,  Next: Text Functions,  Up: Functions
1066
1067Function Call Syntax
1068====================
1069
1070   A function call resembles a variable reference.  It looks like this:
1071
1072     $(FUNCTION ARGUMENTS)
1073
1074or like this:
1075
1076     ${FUNCTION ARGUMENTS}
1077
1078   Here FUNCTION is a function name; one of a short list of names that
1079are part of `make'.  You can also essentially create your own functions
1080by using the `call' builtin function.
1081
1082   The ARGUMENTS are the arguments of the function.  They are separated
1083from the function name by one or more spaces or tabs, and if there is
1084more than one argument, then they are separated by commas.  Such
1085whitespace and commas are not part of an argument's value.  The
1086delimiters which you use to surround the function call, whether
1087parentheses or braces, can appear in an argument only in matching pairs;
1088the other kind of delimiters may appear singly.  If the arguments
1089themselves contain other function calls or variable references, it is
1090wisest to use the same kind of delimiters for all the references; write
1091`$(subst a,b,$(x))', not `$(subst a,b,${x})'.  This is because it is
1092clearer, and because only one type of delimiter is matched to find the
1093end of the reference.
1094
1095   The text written for each argument is processed by substitution of
1096variables and function calls to produce the argument value, which is
1097the text on which the function acts.  The substitution is done in the
1098order in which the arguments appear.
1099
1100   Commas and unmatched parentheses or braces cannot appear in the text
1101of an argument as written; leading spaces cannot appear in the text of
1102the first argument as written.  These characters can be put into the
1103argument value by variable substitution.  First define variables
1104`comma' and `space' whose values are isolated comma and space
1105characters, then substitute these variables where such characters are
1106wanted, like this:
1107
1108     comma:= ,
1109     empty:=
1110     space:= $(empty) $(empty)
1111     foo:= a b c
1112     bar:= $(subst $(space),$(comma),$(foo))
1113     # bar is now `a,b,c'.
1114
1115Here the `subst' function replaces each space with a comma, through the
1116value of `foo', and substitutes the result.
1117
1118
1119File: make.info,  Node: Text Functions,  Next: File Name Functions,  Prev: Syntax of Functions,  Up: Functions
1120
1121Functions for String Substitution and Analysis
1122==============================================
1123
1124   Here are some functions that operate on strings:
1125
1126`$(subst FROM,TO,TEXT)'
1127     Performs a textual replacement on the text TEXT: each occurrence
1128     of FROM is replaced by TO.  The result is substituted for the
1129     function call.  For example,
1130
1131          $(subst ee,EE,feet on the street)
1132
1133     substitutes the string `fEEt on the strEEt'.
1134
1135`$(patsubst PATTERN,REPLACEMENT,TEXT)'
1136     Finds whitespace-separated words in TEXT that match PATTERN and
1137     replaces them with REPLACEMENT.  Here PATTERN may contain a `%'
1138     which acts as a wildcard, matching any number of any characters
1139     within a word.  If REPLACEMENT also contains a `%', the `%' is
1140     replaced by the text that matched the `%' in PATTERN.
1141
1142     `%' characters in `patsubst' function invocations can be quoted
1143     with preceding backslashes (`\').  Backslashes that would
1144     otherwise quote `%' characters can be quoted with more backslashes.
1145     Backslashes that quote `%' characters or other backslashes are
1146     removed from the pattern before it is compared file names or has a
1147     stem substituted into it.  Backslashes that are not in danger of
1148     quoting `%' characters go unmolested.  For example, the pattern
1149     `the\%weird\\%pattern\\' has `the%weird\' preceding the operative
1150     `%' character, and `pattern\\' following it.  The final two
1151     backslashes are left alone because they cannot affect any `%'
1152     character.
1153
1154     Whitespace between words is folded into single space characters;
1155     leading and trailing whitespace is discarded.
1156
1157     For example,
1158
1159          $(patsubst %.c,%.o,x.c.c bar.c)
1160
1161     produces the value `x.c.o bar.o'.
1162
1163     Substitution references (*note Substitution References:
1164     Substitution Refs.) are a simpler way to get the effect of the
1165     `patsubst' function:
1166
1167          $(VAR:PATTERN=REPLACEMENT)
1168
1169     is equivalent to
1170
1171          $(patsubst PATTERN,REPLACEMENT,$(VAR))
1172
1173     The second shorthand simplifies one of the most common uses of
1174     `patsubst': replacing the suffix at the end of file names.
1175
1176          $(VAR:SUFFIX=REPLACEMENT)
1177
1178     is equivalent to
1179
1180          $(patsubst %SUFFIX,%REPLACEMENT,$(VAR))
1181
1182     For example, you might have a list of object files:
1183
1184          objects = foo.o bar.o baz.o
1185
1186     To get the list of corresponding source files, you could simply
1187     write:
1188
1189          $(objects:.o=.c)
1190
1191     instead of using the general form:
1192
1193          $(patsubst %.o,%.c,$(objects))
1194
1195`$(strip STRING)'
1196     Removes leading and trailing whitespace from STRING and replaces
1197     each internal sequence of one or more whitespace characters with a
1198     single space.  Thus, `$(strip a b  c )' results in `a b c'.
1199
1200     The function `strip' can be very useful when used in conjunction
1201     with conditionals.  When comparing something with the empty string
1202     `' using `ifeq' or `ifneq', you usually want a string of just
1203     whitespace to match the empty string (*note Conditionals::).
1204
1205     Thus, the following may fail to have the desired results:
1206
1207          .PHONY: all
1208          ifneq   "$(needs_made)" ""
1209          all: $(needs_made)
1210          else
1211          all:;@echo 'Nothing to make!'
1212          endif
1213
1214     Replacing the variable reference `$(needs_made)' with the function
1215     call `$(strip $(needs_made))' in the `ifneq' directive would make
1216     it more robust.
1217
1218`$(findstring FIND,IN)'
1219     Searches IN for an occurrence of FIND.  If it occurs, the value is
1220     FIND; otherwise, the value is empty.  You can use this function in
1221     a conditional to test for the presence of a specific substring in
1222     a given string.  Thus, the two examples,
1223
1224          $(findstring a,a b c)
1225          $(findstring a,b c)
1226
1227     produce the values `a' and `' (the empty string), respectively.
1228     *Note Testing Flags::, for a practical application of `findstring'.
1229
1230`$(filter PATTERN...,TEXT)'
1231     Returns all whitespace-separated words in TEXT that _do_ match any
1232     of the PATTERN words, removing any words that _do not_ match.  The
1233     patterns are written using `%', just like the patterns used in the
1234     `patsubst' function above.
1235
1236     The `filter' function can be used to separate out different types
1237     of strings (such as file names) in a variable.  For example:
1238
1239          sources := foo.c bar.c baz.s ugh.h
1240          foo: $(sources)
1241                  cc $(filter %.c %.s,$(sources)) -o foo
1242
1243     says that `foo' depends of `foo.c', `bar.c', `baz.s' and `ugh.h'
1244     but only `foo.c', `bar.c' and `baz.s' should be specified in the
1245     command to the compiler.
1246
1247`$(filter-out PATTERN...,TEXT)'
1248     Returns all whitespace-separated words in TEXT that _do not_ match
1249     any of the PATTERN words, removing the words that _do_ match one
1250     or more.  This is the exact opposite of the `filter' function.
1251
1252     Removes all whitespace-separated words in TEXT that _do_ match the
1253     PATTERN words, returning only the words that _do not_ match.  This
1254     is the exact opposite of the `filter' function.
1255
1256     For example, given:
1257
1258          objects=main1.o foo.o main2.o bar.o
1259          mains=main1.o main2.o
1260
1261     the following generates a list which contains all the object files
1262     not in `mains':
1263
1264          $(filter-out $(mains),$(objects))
1265
1266`$(sort LIST)'
1267     Sorts the words of LIST in lexical order, removing duplicate
1268     words.  The output is a list of words separated by single spaces.
1269     Thus,
1270
1271          $(sort foo bar lose)
1272
1273     returns the value `bar foo lose'.
1274
1275     Incidentally, since `sort' removes duplicate words, you can use it
1276     for this purpose even if you don't care about the sort order.
1277
1278   Here is a realistic example of the use of `subst' and `patsubst'.
1279Suppose that a makefile uses the `VPATH' variable to specify a list of
1280directories that `make' should search for prerequisite files (*note
1281`VPATH' Search Path for All Prerequisites: General Search.).  This
1282example shows how to tell the C compiler to search for header files in
1283the same list of directories.
1284
1285   The value of `VPATH' is a list of directories separated by colons,
1286such as `src:../headers'.  First, the `subst' function is used to
1287change the colons to spaces:
1288
1289     $(subst :, ,$(VPATH))
1290
1291This produces `src ../headers'.  Then `patsubst' is used to turn each
1292directory name into a `-I' flag.  These can be added to the value of
1293the variable `CFLAGS', which is passed automatically to the C compiler,
1294like this:
1295
1296     override CFLAGS += $(patsubst %,-I%,$(subst :, ,$(VPATH)))
1297
1298The effect is to append the text `-Isrc -I../headers' to the previously
1299given value of `CFLAGS'.  The `override' directive is used so that the
1300new value is assigned even if the previous value of `CFLAGS' was
1301specified with a command argument (*note The `override' Directive:
1302Override Directive.).
1303
Note: See TracBrowser for help on using the repository browser.