1 | This is make.info, produced by makeinfo version 4.0 from make.texinfo. |
---|
2 | |
---|
3 | INFO-DIR-SECTION GNU Packages |
---|
4 | START-INFO-DIR-ENTRY |
---|
5 | * Make: (make). Remake files automatically. |
---|
6 | END-INFO-DIR-ENTRY |
---|
7 | |
---|
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. |
---|
11 | |
---|
12 | This is Edition 0.55, last updated 04 April 2000, of `The GNU Make |
---|
13 | Manual', 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 |
---|
19 | manual provided the copyright notice and this permission notice are |
---|
20 | preserved on all copies. |
---|
21 | |
---|
22 | Permission is granted to copy and distribute modified versions of |
---|
23 | this manual under the conditions for verbatim copying, provided that |
---|
24 | the entire resulting derived work is distributed under the terms of a |
---|
25 | permission notice identical to this one. |
---|
26 | |
---|
27 | Permission is granted to copy and distribute translations of this |
---|
28 | manual into another language, under the above conditions for modified |
---|
29 | versions, except that this permission notice may be stated in a |
---|
30 | translation approved by the Free Software Foundation. |
---|
31 | |
---|
32 | |
---|
33 | File: make.info, Node: Flavors, Next: Advanced, Prev: Reference, Up: Using Variables |
---|
34 | |
---|
35 | The Two Flavors of Variables |
---|
36 | ============================ |
---|
37 | |
---|
38 | There are two ways that a variable in GNU `make' can have a value; |
---|
39 | we call them the two "flavors" of variables. The two flavors are |
---|
40 | distinguished in how they are defined and in what they do when expanded. |
---|
41 | |
---|
42 | The first flavor of variable is a "recursively expanded" variable. |
---|
43 | Variables of this sort are defined by lines using `=' (*note Setting |
---|
44 | Variables: Setting.) or by the `define' directive (*note Defining |
---|
45 | Variables Verbatim: Defining.). The value you specify is installed |
---|
46 | verbatim; if it contains references to other variables, these |
---|
47 | references are expanded whenever this variable is substituted (in the |
---|
48 | course of expanding some other string). When this happens, it is |
---|
49 | called "recursive expansion". |
---|
50 | |
---|
51 | For example, |
---|
52 | |
---|
53 | foo = $(bar) |
---|
54 | bar = $(ugh) |
---|
55 | ugh = Huh? |
---|
56 | |
---|
57 | all:;echo $(foo) |
---|
58 | |
---|
59 | will 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 |
---|
63 | of `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 | |
---|
69 | will do what was intended: when `CFLAGS' is expanded in a command, it |
---|
70 | will expand to `-Ifoo -Ibar -O'. A major disadvantage is that you |
---|
71 | cannot append something on the end of a variable, as in |
---|
72 | |
---|
73 | CFLAGS = $(CFLAGS) -O |
---|
74 | |
---|
75 | because 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 |
---|
79 | Transforming Text: Functions.) referenced in the definition will be |
---|
80 | executed every time the variable is expanded. This makes `make' run |
---|
81 | slower; worse, it causes the `wildcard' and `shell' functions to give |
---|
82 | unpredictable results because you cannot easily control when they are |
---|
83 | called, or even how many times. |
---|
84 | |
---|
85 | To avoid all the problems and inconveniences of recursively expanded |
---|
86 | variables, there is another flavor: simply expanded variables. |
---|
87 | |
---|
88 | "Simply expanded variables" are defined by lines using `:=' (*note |
---|
89 | Setting Variables: Setting.). The value of a simply expanded variable |
---|
90 | is scanned once and for all, expanding any references to other |
---|
91 | variables and functions, when the variable is defined. The actual |
---|
92 | value of the simply expanded variable is the result of expanding the |
---|
93 | text that you write. It does not contain any references to other |
---|
94 | variables; it contains their values _as of the time this variable was |
---|
95 | defined_. Therefore, |
---|
96 | |
---|
97 | x := foo |
---|
98 | y := $(x) bar |
---|
99 | x := later |
---|
100 | |
---|
101 | is equivalent to |
---|
102 | |
---|
103 | y := foo bar |
---|
104 | x := later |
---|
105 | |
---|
106 | When a simply expanded variable is referenced, its value is |
---|
107 | substituted verbatim. |
---|
108 | |
---|
109 | Here is a somewhat more complicated example, illustrating the use of |
---|
110 | `:=' in conjunction with the `shell' function. (*Note The `shell' |
---|
111 | Function: Shell Function.) This example also shows use of the variable |
---|
112 | `MAKELEVEL', which is changed when it is passed down from level to |
---|
113 | level. (*Note Communicating Variables to a Sub-`make': |
---|
114 | Variables/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 | |
---|
123 | An advantage of this use of `:=' is that a typical `descend into a |
---|
124 | directory' 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 |
---|
130 | programming more predictable because they work like variables in most |
---|
131 | programming languages. They allow you to redefine a variable using its |
---|
132 | own value (or its value processed in some way by one of the expansion |
---|
133 | functions) 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 |
---|
137 | variable values. Leading whitespace characters are discarded from your |
---|
138 | input before substitution of variable references and function calls; |
---|
139 | this means you can include leading spaces in a variable value by |
---|
140 | protecting them with variable references, like this: |
---|
141 | |
---|
142 | nullstring := |
---|
143 | space := $(nullstring) # end of the line |
---|
144 | |
---|
145 | Here the value of the variable `space' is precisely one space. The |
---|
146 | comment `# end of the line' is included here just for clarity. Since |
---|
147 | trailing space characters are _not_ stripped from variable values, just |
---|
148 | a space at the end of the line would have the same effect (but be |
---|
149 | rather hard to read). If you put whitespace at the end of a variable |
---|
150 | value, it is a good idea to put a comment like that at the end of the |
---|
151 | line to make your intent clear. Conversely, if you do _not_ want any |
---|
152 | whitespace characters at the end of your variable value, you must |
---|
153 | remember not to put a random comment on the end of the line after some |
---|
154 | whitespace, such as this: |
---|
155 | |
---|
156 | dir := /foo/bar # directory to put the frobs in |
---|
157 | |
---|
158 | Here the value of the variable `dir' is `/foo/bar ' (with four |
---|
159 | trailing spaces), which was probably not the intention. (Imagine |
---|
160 | something like `$(dir)/file' with this definition!) |
---|
161 | |
---|
162 | There is another assignment operator for variables, `?='. This is |
---|
163 | called a conditional variable assignment operator, because it only has |
---|
164 | an effect if the variable is not yet defined. This statement: |
---|
165 | |
---|
166 | FOO ?= bar |
---|
167 | |
---|
168 | is exactly equivalent to this (*note The `origin' Function: Origin |
---|
169 | Function.): |
---|
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 `?=' |
---|
176 | will not set that variable. |
---|
177 | |
---|
178 | |
---|
179 | File: make.info, Node: Advanced, Next: Values, Prev: Flavors, Up: Using Variables |
---|
180 | |
---|
181 | Advanced Features for Reference to Variables |
---|
182 | ============================================ |
---|
183 | |
---|
184 | This section describes some advanced features you can use to |
---|
185 | reference 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 | |
---|
194 | File: make.info, Node: Substitution Refs, Next: Computed Names, Up: Advanced |
---|
195 | |
---|
196 | Substitution References |
---|
197 | ----------------------- |
---|
198 | |
---|
199 | A "substitution reference" substitutes the value of a variable with |
---|
200 | alterations 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, |
---|
202 | replace every A at the end of a word with B in that value, and |
---|
203 | substitute the resulting string. |
---|
204 | |
---|
205 | When we say "at the end of a word", we mean that A must appear |
---|
206 | either followed by whitespace or at the end of the value in order to be |
---|
207 | replaced; other occurrences of A in the value are unaltered. For |
---|
208 | example: |
---|
209 | |
---|
210 | foo := a.o b.o c.o |
---|
211 | bar := $(foo:.o=.c) |
---|
212 | |
---|
213 | sets `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 |
---|
217 | and Analysis: Text Functions.). We provide substitution references as |
---|
218 | well as `patsubst' for compatibility with other implementations of |
---|
219 | `make'. |
---|
220 | |
---|
221 | Another type of substitution reference lets you use the full power of |
---|
222 | the `patsubst' function. It has the same form `$(VAR:A=B)' described |
---|
223 | above, except that now A must contain a single `%' character. This |
---|
224 | case is equivalent to `$(patsubst A,B,$(VAR))'. *Note Functions for |
---|
225 | String Substitution and Analysis: Text Functions, for a description of |
---|
226 | the `patsubst' function. |
---|
227 | |
---|
228 | For example: |
---|
229 | |
---|
230 | foo := a.o b.o c.o |
---|
231 | bar := $(foo:%.o=%.c) |
---|
232 | |
---|
233 | sets `bar' to `a.c b.c c.c'. |
---|
234 | |
---|
235 | |
---|
236 | File: make.info, Node: Computed Names, Prev: Substitution Refs, Up: Advanced |
---|
237 | |
---|
238 | Computed Variable Names |
---|
239 | ----------------------- |
---|
240 | |
---|
241 | Computed variable names are a complicated concept needed only for |
---|
242 | sophisticated makefile programming. For most purposes you need not |
---|
243 | consider them, except to know that making a variable with a dollar sign |
---|
244 | in its name might have strange results. However, if you are the type |
---|
245 | that wants to understand everything, or you are actually interested in |
---|
246 | what they do, read on. |
---|
247 | |
---|
248 | Variables may be referenced inside the name of a variable. This is |
---|
249 | called a "computed variable name" or a "nested variable reference". |
---|
250 | For example, |
---|
251 | |
---|
252 | x = y |
---|
253 | y = z |
---|
254 | a := $($(x)) |
---|
255 | |
---|
256 | defines `a' as `z': the `$(x)' inside `$($(x))' expands to `y', so |
---|
257 | `$($(x))' expands to `$(y)' which in turn expands to `z'. Here the |
---|
258 | name of the variable to reference is not stated explicitly; it is |
---|
259 | computed by expansion of `$(x)'. The reference `$(x)' here is nested |
---|
260 | within the outer variable reference. |
---|
261 | |
---|
262 | The previous example shows two levels of nesting, but any number of |
---|
263 | levels is possible. For example, here are three levels: |
---|
264 | |
---|
265 | x = y |
---|
266 | y = z |
---|
267 | z = u |
---|
268 | a := $($($(x))) |
---|
269 | |
---|
270 | Here 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 |
---|
275 | are reexpanded in the usual fashion. For example: |
---|
276 | |
---|
277 | x = $(y) |
---|
278 | y = z |
---|
279 | z = Hello |
---|
280 | a := $($(x)) |
---|
281 | |
---|
282 | defines `a' as `Hello': `$($(x))' becomes `$($(y))' which becomes |
---|
283 | `$(z)' which becomes `Hello'. |
---|
284 | |
---|
285 | Nested variable references can also contain modified references and |
---|
286 | function invocations (*note Functions for Transforming Text: |
---|
287 | Functions.), just like any other reference. For example, using the |
---|
288 | `subst' function (*note Functions for String Substitution and Analysis: |
---|
289 | Text Functions.): |
---|
290 | |
---|
291 | x = variable1 |
---|
292 | variable2 := Hello |
---|
293 | y = $(subst 1,2,$(x)) |
---|
294 | z = y |
---|
295 | a := $($($(z))) |
---|
296 | |
---|
297 | eventually defines `a' as `Hello'. It is doubtful that anyone would |
---|
298 | ever want to write a nested reference as convoluted as this one, but it |
---|
299 | works: `$($($(z)))' expands to `$($(y))' which becomes `$($(subst |
---|
300 | 1,2,$(x)))'. This gets the value `variable1' from `x' and changes it |
---|
301 | by 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 |
---|
305 | variable reference. It can contain several variable references, as |
---|
306 | well 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 | |
---|
328 | will 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 | |
---|
338 | defines `sources' as either `a.c b.c c.c' or `1.c 2.c 3.c', depending |
---|
339 | on the value of `a1'. |
---|
340 | |
---|
341 | The only restriction on this sort of use of nested variable |
---|
342 | references is that they cannot specify part of the name of a function |
---|
343 | to be called. This is because the test for a recognized function name |
---|
344 | is 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 | |
---|
356 | attempts 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 |
---|
358 | to either the `sort' or the `strip' function. This restriction could |
---|
359 | be 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 |
---|
362 | variable 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 | |
---|
370 | This 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: |
---|
375 | Flavors.), though both are used together in complex ways when doing |
---|
376 | makefile programming. |
---|
377 | |
---|
378 | |
---|
379 | File: make.info, Node: Values, Next: Setting, Prev: Advanced, Up: Using Variables |
---|
380 | |
---|
381 | How 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 | |
---|
404 | File: make.info, Node: Setting, Next: Appending, Prev: Values, Up: Using Variables |
---|
405 | |
---|
406 | Setting Variables |
---|
407 | ================= |
---|
408 | |
---|
409 | To set a variable from the makefile, write a line starting with the |
---|
410 | variable 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 | |
---|
415 | defines a variable named `objects'. Whitespace around the variable |
---|
416 | name and immediately after the `=' is ignored. |
---|
417 | |
---|
418 | Variables defined with `=' are "recursively expanded" variables. |
---|
419 | Variables defined with `:=' are "simply expanded" variables; these |
---|
420 | definitions can contain variable references which will be expanded |
---|
421 | before the definition is made. *Note The Two Flavors of Variables: |
---|
422 | Flavors. |
---|
423 | |
---|
424 | The variable name may contain function and variable references, which |
---|
425 | are expanded when the line is read to find the actual variable name to |
---|
426 | use. |
---|
427 | |
---|
428 | There is no limit on the length of the value of a variable except the |
---|
429 | amount of swapping space on the computer. When a variable definition is |
---|
430 | long, it is a good idea to break it into several lines by inserting |
---|
431 | backslash-newline at convenient places in the definition. This will not |
---|
432 | affect the functioning of `make', but it will make the makefile easier |
---|
433 | to read. |
---|
434 | |
---|
435 | Most variable names are considered to have the empty string as a |
---|
436 | value if you have never set them. Several variables have built-in |
---|
437 | initial values that are not empty, but you can set them in the usual |
---|
438 | ways (*note Variables Used by Implicit Rules: Implicit Variables.). |
---|
439 | Several special variables are set automatically to a new value for each |
---|
440 | rule; these are called the "automatic" variables (*note Automatic |
---|
441 | Variables: Automatic.). |
---|
442 | |
---|
443 | If you'd like a variable to be set to a value only if it's not |
---|
444 | already 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 | |
---|
450 | and |
---|
451 | |
---|
452 | ifeq ($(origin FOO), undefined) |
---|
453 | FOO = bar |
---|
454 | endif |
---|
455 | |
---|
456 | |
---|
457 | File: make.info, Node: Appending, Next: Override Directive, Prev: Setting, Up: Using Variables |
---|
458 | |
---|
459 | Appending More Text to Variables |
---|
460 | ================================ |
---|
461 | |
---|
462 | Often it is useful to add more text to the value of a variable |
---|
463 | already defined. You do this with a line containing `+=', like this: |
---|
464 | |
---|
465 | objects += another.o |
---|
466 | |
---|
467 | This 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 | |
---|
473 | sets `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 | |
---|
480 | but differs in ways that become important when you use more complex |
---|
481 | values. |
---|
482 | |
---|
483 | When the variable in question has not been defined before, `+=' acts |
---|
484 | just like normal `=': it defines a recursively-expanded variable. |
---|
485 | However, when there _is_ a previous definition, exactly what `+=' does |
---|
486 | depends on what flavor of variable you defined originally. *Note The |
---|
487 | Two Flavors of Variables: Flavors, for an explanation of the two |
---|
488 | flavors of variables. |
---|
489 | |
---|
490 | When you add to a variable's value with `+=', `make' acts |
---|
491 | essentially as if you had included the extra text in the initial |
---|
492 | definition of the variable. If you defined it first with `:=', making |
---|
493 | it a simply-expanded variable, `+=' adds to that simply-expanded |
---|
494 | definition, and expands the new text before appending it to the old |
---|
495 | value just as `:=' does (*note Setting Variables: Setting., for a full |
---|
496 | explanation of `:='). In fact, |
---|
497 | |
---|
498 | variable := value |
---|
499 | variable += more |
---|
500 | |
---|
501 | is 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 |
---|
507 | first to be recursively-expanded using plain `=', `make' does something |
---|
508 | a bit different. Recall that when you define a recursively-expanded |
---|
509 | variable, `make' does not expand the value you set for variable and |
---|
510 | function references immediately. Instead it stores the text verbatim, |
---|
511 | and saves these variable and function references to be expanded later, |
---|
512 | when you refer to the new variable (*note The Two Flavors of Variables: |
---|
513 | Flavors.). When you use `+=' on a recursively-expanded variable, it is |
---|
514 | this unexpanded text to which `make' appends the new text you specify. |
---|
515 | |
---|
516 | variable = value |
---|
517 | variable += more |
---|
518 | |
---|
519 | is roughly equivalent to: |
---|
520 | |
---|
521 | temp = value |
---|
522 | variable = $(temp) more |
---|
523 | |
---|
524 | except that of course it never defines a variable called `temp'. The |
---|
525 | importance of this comes when the variable's old value contains |
---|
526 | variable references. Take this common example: |
---|
527 | |
---|
528 | CFLAGS = $(includes) -O |
---|
529 | ... |
---|
530 | CFLAGS += -pg # enable profiling |
---|
531 | |
---|
532 | The first line defines the `CFLAGS' variable with a reference to another |
---|
533 | variable, `includes'. (`CFLAGS' is used by the rules for C |
---|
534 | compilation; *note Catalogue of Implicit Rules: Catalogue of Rules..) |
---|
535 | Using `=' for the definition makes `CFLAGS' a recursively-expanded |
---|
536 | variable, meaning `$(includes) -O' is _not_ expanded when `make' |
---|
537 | processes the definition of `CFLAGS'. Thus, `includes' need not be |
---|
538 | defined yet for its value to take effect. It only has to be defined |
---|
539 | before any reference to `CFLAGS'. If we tried to append to the value |
---|
540 | of `CFLAGS' without using `+=', we might do it like this: |
---|
541 | |
---|
542 | CFLAGS := $(CFLAGS) -pg # enable profiling |
---|
543 | |
---|
544 | This is pretty close, but not quite what we want. Using `:=' redefines |
---|
545 | `CFLAGS' as a simply-expanded variable; this means `make' expands the |
---|
546 | text `$(CFLAGS) -pg' before setting the variable. If `includes' is not |
---|
547 | yet defined, we get ` -O -pg', and a later definition of `includes' |
---|
548 | will have no effect. Conversely, by using `+=' we set `CFLAGS' to the |
---|
549 | _unexpanded_ value `$(includes) -O -pg'. Thus we preserve the |
---|
550 | reference to `includes', so if that variable gets defined at any later |
---|
551 | point, a reference like `$(CFLAGS)' still uses its value. |
---|
552 | |
---|
553 | |
---|
554 | File: make.info, Node: Override Directive, Next: Defining, Prev: Appending, Up: Using Variables |
---|
555 | |
---|
556 | The `override' Directive |
---|
557 | ======================== |
---|
558 | |
---|
559 | If a variable has been set with a command argument (*note Overriding |
---|
560 | Variables: Overriding.), then ordinary assignments in the makefile are |
---|
561 | ignored. If you want to set the variable in the makefile even though |
---|
562 | it was set with a command argument, you can use an `override' |
---|
563 | directive, which is a line that looks like this: |
---|
564 | |
---|
565 | override VARIABLE = VALUE |
---|
566 | |
---|
567 | or |
---|
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 |
---|
578 | between makefiles and command arguments. It was invented so you can |
---|
579 | alter 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 |
---|
582 | C compiler, but you would like to allow the user to specify the other |
---|
583 | switches 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. |
---|
589 | This is done as you might expect: |
---|
590 | |
---|
591 | override define foo |
---|
592 | bar |
---|
593 | endef |
---|
594 | |
---|
595 | *Note Defining Variables Verbatim: Defining. |
---|
596 | |
---|
597 | |
---|
598 | File: make.info, Node: Defining, Next: Environment, Prev: Override Directive, Up: Using Variables |
---|
599 | |
---|
600 | Defining Variables Verbatim |
---|
601 | =========================== |
---|
602 | |
---|
603 | Another way to set the value of a variable is to use the `define' |
---|
604 | directive. This directive has an unusual syntax which allows newline |
---|
605 | characters to be included in the value, which is convenient for defining |
---|
606 | canned sequences of commands (*note Defining Canned Command Sequences: |
---|
607 | Sequences.). |
---|
608 | |
---|
609 | The `define' directive is followed on the same line by the name of |
---|
610 | the variable and nothing more. The value to give the variable appears |
---|
611 | on the following lines. The end of the value is marked by a line |
---|
612 | containing just the word `endef'. Aside from this difference in |
---|
613 | syntax, `define' works just like `=': it creates a recursively-expanded |
---|
614 | variable (*note The Two Flavors of Variables: Flavors.). The variable |
---|
615 | name may contain function and variable references, which are expanded |
---|
616 | when 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 |
---|
624 | newlines that separate the lines of the value in a `define' become part |
---|
625 | of the variable's value (except for the final newline which precedes |
---|
626 | the `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 | |
---|
632 | since two commands separated by semicolon behave much like two separate |
---|
633 | shell commands. However, note that using two separate lines means |
---|
634 | `make' will invoke the shell twice, running an independent subshell for |
---|
635 | each line. *Note Command Execution: Execution. |
---|
636 | |
---|
637 | If you want variable definitions made with `define' to take |
---|
638 | precedence 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 | |
---|
649 | File: make.info, Node: Environment, Next: Target-specific, Prev: Defining, Up: Using Variables |
---|
650 | |
---|
651 | Variables from the Environment |
---|
652 | ============================== |
---|
653 | |
---|
654 | Variables in `make' can come from the environment in which `make' is |
---|
655 | run. Every environment variable that `make' sees when it starts up is |
---|
656 | transformed into a `make' variable with the same name and value. But |
---|
657 | an explicit assignment in the makefile, or with a command argument, |
---|
658 | overrides the environment. (If the `-e' flag is specified, then values |
---|
659 | from the environment override assignments in the makefile. *Note |
---|
660 | Summary of Options: Options Summary. But this is not recommended |
---|
661 | practice.) |
---|
662 | |
---|
663 | Thus, by setting the variable `CFLAGS' in your environment, you can |
---|
664 | cause all C compilations in most makefiles to use the compiler switches |
---|
665 | you prefer. This is safe for variables with standard or conventional |
---|
666 | meanings because you know that no makefile will use them for other |
---|
667 | things. (But this is not totally reliable; some makefiles set `CFLAGS' |
---|
668 | explicitly and therefore are not affected by the value in the |
---|
669 | environment.) |
---|
670 | |
---|
671 | When `make' is invoked recursively, variables defined in the outer |
---|
672 | invocation can be passed to inner invocations through the environment |
---|
673 | (*note Recursive Use of `make': Recursion.). By default, only |
---|
674 | variables that came from the environment or the command line are passed |
---|
675 | to recursive invocations. You can use the `export' directive to pass |
---|
676 | other variables. *Note Communicating Variables to a Sub-`make': |
---|
677 | Variables/Recursion, for full details. |
---|
678 | |
---|
679 | Other use of variables from the environment is not recommended. It |
---|
680 | is not wise for makefiles to depend for their functioning on |
---|
681 | environment variables set up outside their control, since this would |
---|
682 | cause different users to get different results from the same makefile. |
---|
683 | This is against the whole purpose of most makefiles. |
---|
684 | |
---|
685 | Such problems would be especially likely with the variable `SHELL', |
---|
686 | which is normally present in the environment to specify the user's |
---|
687 | choice of interactive shell. It would be very undesirable for this |
---|
688 | choice to affect `make'. So `make' ignores the environment value of |
---|
689 | `SHELL' (except on MS-DOS and MS-Windows, where `SHELL' is usually not |
---|
690 | set. *Note Special handling of SHELL on MS-DOS: Execution.) |
---|
691 | |
---|
692 | |
---|
693 | File: make.info, Node: Target-specific, Next: Pattern-specific, Prev: Environment, Up: Using Variables |
---|
694 | |
---|
695 | Target-specific Variable Values |
---|
696 | =============================== |
---|
697 | |
---|
698 | Variable values in `make' are usually global; that is, they are the |
---|
699 | same regardless of where they are evaluated (unless they're reset, of |
---|
700 | course). One exception to that is automatic variables (*note Automatic |
---|
701 | Variables: Automatic.). |
---|
702 | |
---|
703 | The other exception is "target-specific variable values". This |
---|
704 | feature allows you to define different values for the same variable, |
---|
705 | based on the target that `make' is currently building. As with |
---|
706 | automatic variables, these values are only available within the context |
---|
707 | of 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 | |
---|
713 | or like this: |
---|
714 | |
---|
715 | TARGET ... : override VARIABLE-ASSIGNMENT |
---|
716 | |
---|
717 | Multiple TARGET values create a target-specific variable value for |
---|
718 | each member of the target list individually. |
---|
719 | |
---|
720 | The VARIABLE-ASSIGNMENT can be any valid form of assignment; |
---|
721 | recursive (`='), static (`:='), appending (`+='), or conditional |
---|
722 | (`?='). All variables that appear within the VARIABLE-ASSIGNMENT are |
---|
723 | evaluated within the context of the target: thus, any |
---|
724 | previously-defined target-specific variable values will be in effect. |
---|
725 | Note that this variable is actually distinct from any "global" value: |
---|
726 | the two variables do not have to have the same flavor (recursive vs. |
---|
727 | static). |
---|
728 | |
---|
729 | Target-specific variables have the same priority as any other |
---|
730 | makefile variable. Variables provided on the command-line (and in the |
---|
731 | environment if the `-e' option is in force) will take precedence. |
---|
732 | Specifying the `override' directive will allow the target-specific |
---|
733 | variable value to be preferred. |
---|
734 | |
---|
735 | There is one more special feature of target-specific variables: when |
---|
736 | you define a target-specific variable, that variable value is also in |
---|
737 | effect for all prerequisites of this target (unless those prerequisites |
---|
738 | override it with their own target-specific variable value). So, for |
---|
739 | example, a statement like this: |
---|
740 | |
---|
741 | prog : CFLAGS = -g |
---|
742 | prog : prog.o foo.o bar.o |
---|
743 | |
---|
744 | will set `CFLAGS' to `-g' in the command script for `prog', but it will |
---|
745 | also 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 |
---|
747 | prerequisites. |
---|
748 | |
---|
749 | |
---|
750 | File: make.info, Node: Pattern-specific, Prev: Target-specific, Up: Using Variables |
---|
751 | |
---|
752 | Pattern-specific Variable Values |
---|
753 | ================================ |
---|
754 | |
---|
755 | In addition to target-specific variable values (*note |
---|
756 | Target-specific Variable Values: Target-specific.), GNU `make' supports |
---|
757 | pattern-specific variable values. In this form, a variable is defined |
---|
758 | for any target that matches the pattern specified. Variables defined in |
---|
759 | this way are searched after any target-specific variables defined |
---|
760 | explicitly for that target, and before target-specific variables defined |
---|
761 | for the parent target. |
---|
762 | |
---|
763 | Set a pattern-specific variable value like this: |
---|
764 | |
---|
765 | PATTERN ... : VARIABLE-ASSIGNMENT |
---|
766 | |
---|
767 | or like this: |
---|
768 | |
---|
769 | PATTERN ... : override VARIABLE-ASSIGNMENT |
---|
770 | |
---|
771 | where PATTERN is a %-pattern. As with target-specific variable values, |
---|
772 | multiple PATTERN values create a pattern-specific variable value for |
---|
773 | each pattern individually. The VARIABLE-ASSIGNMENT can be any valid |
---|
774 | form of assignment. Any command-line variable setting will take |
---|
775 | precedence, unless `override' is specified. |
---|
776 | |
---|
777 | For example: |
---|
778 | |
---|
779 | %.o : CFLAGS = -O |
---|
780 | |
---|
781 | will assign `CFLAGS' the value of `-O' for all targets matching the |
---|
782 | pattern `%.o'. |
---|
783 | |
---|
784 | |
---|
785 | File: make.info, Node: Conditionals, Next: Functions, Prev: Using Variables, Up: Top |
---|
786 | |
---|
787 | Conditional Parts of Makefiles |
---|
788 | ****************************** |
---|
789 | |
---|
790 | A "conditional" causes part of a makefile to be obeyed or ignored |
---|
791 | depending on the values of variables. Conditionals can compare the |
---|
792 | value of one variable to another, or the value of a variable to a |
---|
793 | constant string. Conditionals control what `make' actually "sees" in |
---|
794 | the makefile, so they _cannot_ be used to control shell commands at the |
---|
795 | time 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 | |
---|
804 | File: make.info, Node: Conditional Example, Next: Conditional Syntax, Up: Conditionals |
---|
805 | |
---|
806 | Example of a Conditional |
---|
807 | ======================== |
---|
808 | |
---|
809 | The following example of a conditional tells `make' to use one set |
---|
810 | of libraries if the `CC' variable is `gcc', and a different set of |
---|
811 | libraries otherwise. It works by controlling which of two command |
---|
812 | lines 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 |
---|
814 | used 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 |
---|
827 | one `endif'. |
---|
828 | |
---|
829 | The `ifeq' directive begins the conditional, and specifies the |
---|
830 | condition. It contains two arguments, separated by a comma and |
---|
831 | surrounded by parentheses. Variable substitution is performed on both |
---|
832 | arguments and then they are compared. The lines of the makefile |
---|
833 | following the `ifeq' are obeyed if the two arguments match; otherwise |
---|
834 | they are ignored. |
---|
835 | |
---|
836 | The `else' directive causes the following lines to be obeyed if the |
---|
837 | previous conditional failed. In the example above, this means that the |
---|
838 | second alternative linking command is used whenever the first |
---|
839 | alternative is not used. It is optional to have an `else' in a |
---|
840 | conditional. |
---|
841 | |
---|
842 | The `endif' directive ends the conditional. Every conditional must |
---|
843 | end with an `endif'. Unconditional makefile text follows. |
---|
844 | |
---|
845 | As this example illustrates, conditionals work at the textual level: |
---|
846 | the lines of the conditional are treated as part of the makefile, or |
---|
847 | ignored, according to the condition. This is why the larger syntactic |
---|
848 | units of the makefile, such as rules, may cross the beginning or the |
---|
849 | end of the conditional. |
---|
850 | |
---|
851 | When the variable `CC' has the value `gcc', the above example has |
---|
852 | this effect: |
---|
853 | |
---|
854 | foo: $(objects) |
---|
855 | $(CC) -o foo $(objects) $(libs_for_gcc) |
---|
856 | |
---|
857 | When 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 |
---|
863 | conditionalizing a variable assignment and then using the variable |
---|
864 | unconditionally: |
---|
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 | |
---|
879 | File: make.info, Node: Conditional Syntax, Next: Testing Flags, Prev: Conditional Example, Up: Conditionals |
---|
880 | |
---|
881 | Syntax 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 | |
---|
890 | The TEXT-IF-TRUE may be any lines of text, to be considered as part of |
---|
891 | the makefile if the condition is true. If the condition is false, no |
---|
892 | text 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 | |
---|
902 | If the condition is true, TEXT-IF-TRUE is used; otherwise, |
---|
903 | TEXT-IF-FALSE is used instead. The TEXT-IF-FALSE can be any number of |
---|
904 | lines of text. |
---|
905 | |
---|
906 | The syntax of the CONDITIONAL-DIRECTIVE is the same whether the |
---|
907 | conditional is simple or complex. There are four different directives |
---|
908 | that 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 |
---|
979 | conditional directive line, but a tab is not allowed. (If the line |
---|
980 | begins with a tab, it will be considered a command for a rule.) Aside |
---|
981 | from this, extra spaces or tabs may be inserted with no effect anywhere |
---|
982 | except within the directive name or within an argument. A comment |
---|
983 | starting with `#' may appear at the end of the line. |
---|
984 | |
---|
985 | The other two directives that play a part in a conditional are `else' |
---|
986 | and `endif'. Each of these directives is written as one word, with no |
---|
987 | arguments. Extra spaces are allowed and ignored at the beginning of the |
---|
988 | line, and spaces or tabs at the end. A comment starting with `#' may |
---|
989 | appear at the end of the line. |
---|
990 | |
---|
991 | Conditionals affect which lines of the makefile `make' uses. If the |
---|
992 | condition is true, `make' reads the lines of the TEXT-IF-TRUE as part |
---|
993 | of the makefile; if the condition is false, `make' ignores those lines |
---|
994 | completely. It follows that syntactic units of the makefile, such as |
---|
995 | rules, may safely be split across the beginning or the end of the |
---|
996 | conditional. |
---|
997 | |
---|
998 | `make' evaluates conditionals when it reads a makefile. |
---|
999 | Consequently, you cannot use automatic variables in the tests of |
---|
1000 | conditionals because they are not defined until commands are run (*note |
---|
1001 | Automatic Variables: Automatic.). |
---|
1002 | |
---|
1003 | To prevent intolerable confusion, it is not permitted to start a |
---|
1004 | conditional in one makefile and end it in another. However, you may |
---|
1005 | write an `include' directive within a conditional, provided you do not |
---|
1006 | attempt to terminate the conditional inside the included file. |
---|
1007 | |
---|
1008 | |
---|
1009 | File: make.info, Node: Testing Flags, Prev: Conditional Syntax, Up: Conditionals |
---|
1010 | |
---|
1011 | Conditionals 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' |
---|
1016 | function (*note Functions for String Substitution and Analysis: Text |
---|
1017 | Functions.). This is useful when `touch' is not enough to make a file |
---|
1018 | appear up to date. |
---|
1019 | |
---|
1020 | The `findstring' function determines whether one string appears as a |
---|
1021 | substring of another. If you want to test for the `-t' flag, use `t' |
---|
1022 | as 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 |
---|
1025 | marking 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 | |
---|
1035 | The `+' prefix marks those command lines as "recursive" so that they |
---|
1036 | will be executed despite use of the `-t' flag. *Note Recursive Use of |
---|
1037 | `make': Recursion. |
---|
1038 | |
---|
1039 | |
---|
1040 | File: make.info, Node: Functions, Next: Running, Prev: Conditionals, Up: Top |
---|
1041 | |
---|
1042 | Functions for Transforming Text |
---|
1043 | ******************************* |
---|
1044 | |
---|
1045 | "Functions" allow you to do text processing in the makefile to |
---|
1046 | compute the files to operate on or the commands to use. You use a |
---|
1047 | function in a "function call", where you give the name of the function |
---|
1048 | and some text (the "arguments") for the function to operate on. The |
---|
1049 | result of the function's processing is substituted into the makefile at |
---|
1050 | the 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 | |
---|
1065 | File: make.info, Node: Syntax of Functions, Next: Text Functions, Up: Functions |
---|
1066 | |
---|
1067 | Function Call Syntax |
---|
1068 | ==================== |
---|
1069 | |
---|
1070 | A function call resembles a variable reference. It looks like this: |
---|
1071 | |
---|
1072 | $(FUNCTION ARGUMENTS) |
---|
1073 | |
---|
1074 | or like this: |
---|
1075 | |
---|
1076 | ${FUNCTION ARGUMENTS} |
---|
1077 | |
---|
1078 | Here FUNCTION is a function name; one of a short list of names that |
---|
1079 | are part of `make'. You can also essentially create your own functions |
---|
1080 | by using the `call' builtin function. |
---|
1081 | |
---|
1082 | The ARGUMENTS are the arguments of the function. They are separated |
---|
1083 | from the function name by one or more spaces or tabs, and if there is |
---|
1084 | more than one argument, then they are separated by commas. Such |
---|
1085 | whitespace and commas are not part of an argument's value. The |
---|
1086 | delimiters which you use to surround the function call, whether |
---|
1087 | parentheses or braces, can appear in an argument only in matching pairs; |
---|
1088 | the other kind of delimiters may appear singly. If the arguments |
---|
1089 | themselves contain other function calls or variable references, it is |
---|
1090 | wisest 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 |
---|
1092 | clearer, and because only one type of delimiter is matched to find the |
---|
1093 | end of the reference. |
---|
1094 | |
---|
1095 | The text written for each argument is processed by substitution of |
---|
1096 | variables and function calls to produce the argument value, which is |
---|
1097 | the text on which the function acts. The substitution is done in the |
---|
1098 | order in which the arguments appear. |
---|
1099 | |
---|
1100 | Commas and unmatched parentheses or braces cannot appear in the text |
---|
1101 | of an argument as written; leading spaces cannot appear in the text of |
---|
1102 | the first argument as written. These characters can be put into the |
---|
1103 | argument value by variable substitution. First define variables |
---|
1104 | `comma' and `space' whose values are isolated comma and space |
---|
1105 | characters, then substitute these variables where such characters are |
---|
1106 | wanted, 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 | |
---|
1115 | Here the `subst' function replaces each space with a comma, through the |
---|
1116 | value of `foo', and substitutes the result. |
---|
1117 | |
---|
1118 | |
---|
1119 | File: make.info, Node: Text Functions, Next: File Name Functions, Prev: Syntax of Functions, Up: Functions |
---|
1120 | |
---|
1121 | Functions 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'. |
---|
1279 | Suppose that a makefile uses the `VPATH' variable to specify a list of |
---|
1280 | directories that `make' should search for prerequisite files (*note |
---|
1281 | `VPATH' Search Path for All Prerequisites: General Search.). This |
---|
1282 | example shows how to tell the C compiler to search for header files in |
---|
1283 | the same list of directories. |
---|
1284 | |
---|
1285 | The value of `VPATH' is a list of directories separated by colons, |
---|
1286 | such as `src:../headers'. First, the `subst' function is used to |
---|
1287 | change the colons to spaces: |
---|
1288 | |
---|
1289 | $(subst :, ,$(VPATH)) |
---|
1290 | |
---|
1291 | This produces `src ../headers'. Then `patsubst' is used to turn each |
---|
1292 | directory name into a `-I' flag. These can be added to the value of |
---|
1293 | the variable `CFLAGS', which is passed automatically to the C compiler, |
---|
1294 | like this: |
---|
1295 | |
---|
1296 | override CFLAGS += $(patsubst %,-I%,$(subst :, ,$(VPATH))) |
---|
1297 | |
---|
1298 | The effect is to append the text `-Isrc -I../headers' to the previously |
---|
1299 | given value of `CFLAGS'. The `override' directive is used so that the |
---|
1300 | new value is assigned even if the previous value of `CFLAGS' was |
---|
1301 | specified with a command argument (*note The `override' Directive: |
---|
1302 | Override Directive.). |
---|
1303 | |
---|