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: Double-Colon, Next: Automatic Prerequisites, Prev: Static Pattern, Up: Rules |
---|
34 | |
---|
35 | Double-Colon Rules |
---|
36 | ================== |
---|
37 | |
---|
38 | "Double-colon" rules are rules written with `::' instead of `:' |
---|
39 | after the target names. They are handled differently from ordinary |
---|
40 | rules when the same target appears in more than one rule. |
---|
41 | |
---|
42 | When a target appears in multiple rules, all the rules must be the |
---|
43 | same type: all ordinary, or all double-colon. If they are |
---|
44 | double-colon, each of them is independent of the others. Each |
---|
45 | double-colon rule's commands are executed if the target is older than |
---|
46 | any prerequisites of that rule. This can result in executing none, |
---|
47 | any, or all of the double-colon rules. |
---|
48 | |
---|
49 | Double-colon rules with the same target are in fact completely |
---|
50 | separate from one another. Each double-colon rule is processed |
---|
51 | individually, just as rules with different targets are processed. |
---|
52 | |
---|
53 | The double-colon rules for a target are executed in the order they |
---|
54 | appear in the makefile. However, the cases where double-colon rules |
---|
55 | really make sense are those where the order of executing the commands |
---|
56 | would not matter. |
---|
57 | |
---|
58 | Double-colon rules are somewhat obscure and not often very useful; |
---|
59 | they provide a mechanism for cases in which the method used to update a |
---|
60 | target differs depending on which prerequisite files caused the update, |
---|
61 | and such cases are rare. |
---|
62 | |
---|
63 | Each double-colon rule should specify commands; if it does not, an |
---|
64 | implicit rule will be used if one applies. *Note Using Implicit Rules: |
---|
65 | Implicit Rules. |
---|
66 | |
---|
67 | |
---|
68 | File: make.info, Node: Automatic Prerequisites, Prev: Double-Colon, Up: Rules |
---|
69 | |
---|
70 | Generating Prerequisites Automatically |
---|
71 | ====================================== |
---|
72 | |
---|
73 | In the makefile for a program, many of the rules you need to write |
---|
74 | often say only that some object file depends on some header file. For |
---|
75 | example, if `main.c' uses `defs.h' via an `#include', you would write: |
---|
76 | |
---|
77 | main.o: defs.h |
---|
78 | |
---|
79 | You need this rule so that `make' knows that it must remake `main.o' |
---|
80 | whenever `defs.h' changes. You can see that for a large program you |
---|
81 | would have to write dozens of such rules in your makefile. And, you |
---|
82 | must always be very careful to update the makefile every time you add |
---|
83 | or remove an `#include'. |
---|
84 | |
---|
85 | To avoid this hassle, most modern C compilers can write these rules |
---|
86 | for you, by looking at the `#include' lines in the source files. |
---|
87 | Usually this is done with the `-M' option to the compiler. For |
---|
88 | example, the command: |
---|
89 | |
---|
90 | cc -M main.c |
---|
91 | |
---|
92 | generates the output: |
---|
93 | |
---|
94 | main.o : main.c defs.h |
---|
95 | |
---|
96 | Thus you no longer have to write all those rules yourself. The |
---|
97 | compiler will do it for you. |
---|
98 | |
---|
99 | Note that such a prerequisite constitutes mentioning `main.o' in a |
---|
100 | makefile, so it can never be considered an intermediate file by implicit |
---|
101 | rule search. This means that `make' won't ever remove the file after |
---|
102 | using it; *note Chains of Implicit Rules: Chained Rules.. |
---|
103 | |
---|
104 | With old `make' programs, it was traditional practice to use this |
---|
105 | compiler feature to generate prerequisites on demand with a command like |
---|
106 | `make depend'. That command would create a file `depend' containing |
---|
107 | all the automatically-generated prerequisites; then the makefile could |
---|
108 | use `include' to read them in (*note Include::). |
---|
109 | |
---|
110 | In GNU `make', the feature of remaking makefiles makes this practice |
---|
111 | obsolete--you need never tell `make' explicitly to regenerate the |
---|
112 | prerequisites, because it always regenerates any makefile that is out |
---|
113 | of date. *Note Remaking Makefiles::. |
---|
114 | |
---|
115 | The practice we recommend for automatic prerequisite generation is |
---|
116 | to have one makefile corresponding to each source file. For each |
---|
117 | source file `NAME.c' there is a makefile `NAME.d' which lists what |
---|
118 | files the object file `NAME.o' depends on. That way only the source |
---|
119 | files that have changed need to be rescanned to produce the new |
---|
120 | prerequisites. |
---|
121 | |
---|
122 | Here is the pattern rule to generate a file of prerequisites (i.e., |
---|
123 | a makefile) called `NAME.d' from a C source file called `NAME.c': |
---|
124 | |
---|
125 | %.d: %.c |
---|
126 | set -e; $(CC) -M $(CPPFLAGS) $< \ |
---|
127 | | sed 's/\($*\)\.o[ :]*/\1.o $@ : /g' > $@; \ |
---|
128 | [ -s $@ ] || rm -f $@ |
---|
129 | |
---|
130 | *Note Pattern Rules::, for information on defining pattern rules. The |
---|
131 | `-e' flag to the shell makes it exit immediately if the `$(CC)' command |
---|
132 | fails (exits with a nonzero status). Normally the shell exits with the |
---|
133 | status of the last command in the pipeline (`sed' in this case), so |
---|
134 | `make' would not notice a nonzero status from the compiler. |
---|
135 | |
---|
136 | With the GNU C compiler, you may wish to use the `-MM' flag instead |
---|
137 | of `-M'. This omits prerequisites on system header files. *Note |
---|
138 | Options Controlling the Preprocessor: (gcc.info)Preprocessor Options, |
---|
139 | for details. |
---|
140 | |
---|
141 | The purpose of the `sed' command is to translate (for example): |
---|
142 | |
---|
143 | main.o : main.c defs.h |
---|
144 | |
---|
145 | into: |
---|
146 | |
---|
147 | main.o main.d : main.c defs.h |
---|
148 | |
---|
149 | This makes each `.d' file depend on all the source and header files |
---|
150 | that the corresponding `.o' file depends on. `make' then knows it must |
---|
151 | regenerate the prerequisites whenever any of the source or header files |
---|
152 | changes. |
---|
153 | |
---|
154 | Once you've defined the rule to remake the `.d' files, you then use |
---|
155 | the `include' directive to read them all in. *Note Include::. For |
---|
156 | example: |
---|
157 | |
---|
158 | sources = foo.c bar.c |
---|
159 | |
---|
160 | include $(sources:.c=.d) |
---|
161 | |
---|
162 | (This example uses a substitution variable reference to translate the |
---|
163 | list of source files `foo.c bar.c' into a list of prerequisite |
---|
164 | makefiles, `foo.d bar.d'. *Note Substitution Refs::, for full |
---|
165 | information on substitution references.) Since the `.d' files are |
---|
166 | makefiles like any others, `make' will remake them as necessary with no |
---|
167 | further work from you. *Note Remaking Makefiles::. |
---|
168 | |
---|
169 | |
---|
170 | File: make.info, Node: Commands, Next: Using Variables, Prev: Rules, Up: Top |
---|
171 | |
---|
172 | Writing the Commands in Rules |
---|
173 | ***************************** |
---|
174 | |
---|
175 | The commands of a rule consist of shell command lines to be executed |
---|
176 | one by one. Each command line must start with a tab, except that the |
---|
177 | first command line may be attached to the target-and-prerequisites line |
---|
178 | with a semicolon in between. Blank lines and lines of just comments |
---|
179 | may appear among the command lines; they are ignored. (But beware, an |
---|
180 | apparently "blank" line that begins with a tab is _not_ blank! It is an |
---|
181 | empty command; *note Empty Commands::.) |
---|
182 | |
---|
183 | Users use many different shell programs, but commands in makefiles |
---|
184 | are always interpreted by `/bin/sh' unless the makefile specifies |
---|
185 | otherwise. *Note Command Execution: Execution. |
---|
186 | |
---|
187 | The shell that is in use determines whether comments can be written |
---|
188 | on command lines, and what syntax they use. When the shell is |
---|
189 | `/bin/sh', a `#' starts a comment that extends to the end of the line. |
---|
190 | The `#' does not have to be at the beginning of a line. Text on a line |
---|
191 | before a `#' is not part of the comment. |
---|
192 | |
---|
193 | * Menu: |
---|
194 | |
---|
195 | * Echoing:: How to control when commands are echoed. |
---|
196 | * Execution:: How commands are executed. |
---|
197 | * Parallel:: How commands can be executed in parallel. |
---|
198 | * Errors:: What happens after a command execution error. |
---|
199 | * Interrupts:: What happens when a command is interrupted. |
---|
200 | * Recursion:: Invoking `make' from makefiles. |
---|
201 | * Sequences:: Defining canned sequences of commands. |
---|
202 | * Empty Commands:: Defining useful, do-nothing commands. |
---|
203 | |
---|
204 | |
---|
205 | File: make.info, Node: Echoing, Next: Execution, Up: Commands |
---|
206 | |
---|
207 | Command Echoing |
---|
208 | =============== |
---|
209 | |
---|
210 | Normally `make' prints each command line before it is executed. We |
---|
211 | call this "echoing" because it gives the appearance that you are typing |
---|
212 | the commands yourself. |
---|
213 | |
---|
214 | When a line starts with `@', the echoing of that line is suppressed. |
---|
215 | The `@' is discarded before the command is passed to the shell. |
---|
216 | Typically you would use this for a command whose only effect is to print |
---|
217 | something, such as an `echo' command to indicate progress through the |
---|
218 | makefile: |
---|
219 | |
---|
220 | @echo About to make distribution files |
---|
221 | |
---|
222 | When `make' is given the flag `-n' or `--just-print' it only echoes |
---|
223 | commands, it won't execute them. *Note Summary of Options: Options |
---|
224 | Summary. In this case and only this case, even the commands starting |
---|
225 | with `@' are printed. This flag is useful for finding out which |
---|
226 | commands `make' thinks are necessary without actually doing them. |
---|
227 | |
---|
228 | The `-s' or `--silent' flag to `make' prevents all echoing, as if |
---|
229 | all commands started with `@'. A rule in the makefile for the special |
---|
230 | target `.SILENT' without prerequisites has the same effect (*note |
---|
231 | Special Built-in Target Names: Special Targets.). `.SILENT' is |
---|
232 | essentially obsolete since `@' is more flexible. |
---|
233 | |
---|
234 | |
---|
235 | File: make.info, Node: Execution, Next: Parallel, Prev: Echoing, Up: Commands |
---|
236 | |
---|
237 | Command Execution |
---|
238 | ================= |
---|
239 | |
---|
240 | When it is time to execute commands to update a target, they are |
---|
241 | executed by making a new subshell for each line. (In practice, `make' |
---|
242 | may take shortcuts that do not affect the results.) |
---|
243 | |
---|
244 | *Please note:* this implies that shell commands such as `cd' that |
---|
245 | set variables local to each process will not affect the following |
---|
246 | command lines. (1) If you want to use `cd' to affect the next command, |
---|
247 | put the two on a single line with a semicolon between them. Then |
---|
248 | `make' will consider them a single command and pass them, together, to |
---|
249 | a shell which will execute them in sequence. For example: |
---|
250 | |
---|
251 | foo : bar/lose |
---|
252 | cd bar; gobble lose > ../foo |
---|
253 | |
---|
254 | If you would like to split a single shell command into multiple |
---|
255 | lines of text, you must use a backslash at the end of all but the last |
---|
256 | subline. Such a sequence of lines is combined into a single line, by |
---|
257 | deleting the backslash-newline sequences, before passing it to the |
---|
258 | shell. Thus, the following is equivalent to the preceding example: |
---|
259 | |
---|
260 | foo : bar/lose |
---|
261 | cd bar; \ |
---|
262 | gobble lose > ../foo |
---|
263 | |
---|
264 | The program used as the shell is taken from the variable `SHELL'. |
---|
265 | By default, the program `/bin/sh' is used. |
---|
266 | |
---|
267 | On MS-DOS, if `SHELL' is not set, the value of the variable |
---|
268 | `COMSPEC' (which is always set) is used instead. |
---|
269 | |
---|
270 | The processing of lines that set the variable `SHELL' in Makefiles |
---|
271 | is different on MS-DOS. The stock shell, `command.com', is |
---|
272 | ridiculously limited in its functionality and many users of `make' tend |
---|
273 | to install a replacement shell. Therefore, on MS-DOS, `make' examines |
---|
274 | the value of `SHELL', and changes its behavior based on whether it |
---|
275 | points to a Unix-style or DOS-style shell. This allows reasonable |
---|
276 | functionality even if `SHELL' points to `command.com'. |
---|
277 | |
---|
278 | If `SHELL' points to a Unix-style shell, `make' on MS-DOS |
---|
279 | additionally checks whether that shell can indeed be found; if not, it |
---|
280 | ignores the line that sets `SHELL'. In MS-DOS, GNU `make' searches for |
---|
281 | the shell in the following places: |
---|
282 | |
---|
283 | 1. In the precise place pointed to by the value of `SHELL'. For |
---|
284 | example, if the makefile specifies `SHELL = /bin/sh', `make' will |
---|
285 | look in the directory `/bin' on the current drive. |
---|
286 | |
---|
287 | 2. In the current directory. |
---|
288 | |
---|
289 | 3. In each of the directories in the `PATH' variable, in order. |
---|
290 | |
---|
291 | |
---|
292 | In every directory it examines, `make' will first look for the |
---|
293 | specific file (`sh' in the example above). If this is not found, it |
---|
294 | will also look in that directory for that file with one of the known |
---|
295 | extensions which identify executable files. For example `.exe', |
---|
296 | `.com', `.bat', `.btm', `.sh', and some others. |
---|
297 | |
---|
298 | If any of these attempts is successful, the value of `SHELL' will be |
---|
299 | set to the full pathname of the shell as found. However, if none of |
---|
300 | these is found, the value of `SHELL' will not be changed, and thus the |
---|
301 | line that sets it will be effectively ignored. This is so `make' will |
---|
302 | only support features specific to a Unix-style shell if such a shell is |
---|
303 | actually installed on the system where `make' runs. |
---|
304 | |
---|
305 | Note that this extended search for the shell is limited to the cases |
---|
306 | where `SHELL' is set from the Makefile; if it is set in the environment |
---|
307 | or command line, you are expected to set it to the full pathname of the |
---|
308 | shell, exactly as things are on Unix. |
---|
309 | |
---|
310 | The effect of the above DOS-specific processing is that a Makefile |
---|
311 | that says `SHELL = /bin/sh' (as many Unix makefiles do), will work on |
---|
312 | MS-DOS unaltered if you have e.g. `sh.exe' installed in some directory |
---|
313 | along your `PATH'. |
---|
314 | |
---|
315 | Unlike most variables, the variable `SHELL' is never set from the |
---|
316 | environment. This is because the `SHELL' environment variable is used |
---|
317 | to specify your personal choice of shell program for interactive use. |
---|
318 | It would be very bad for personal choices like this to affect the |
---|
319 | functioning of makefiles. *Note Variables from the Environment: |
---|
320 | Environment. However, on MS-DOS and MS-Windows the value of `SHELL' in |
---|
321 | the environment *is* used, since on those systems most users do not set |
---|
322 | this variable, and therefore it is most likely set specifically to be |
---|
323 | used by `make'. On MS-DOS, if the setting of `SHELL' is not suitable |
---|
324 | for `make', you can set the variable `MAKESHELL' to the shell that |
---|
325 | `make' should use; this will override the value of `SHELL'. |
---|
326 | |
---|
327 | ---------- Footnotes ---------- |
---|
328 | |
---|
329 | (1) On MS-DOS, the value of current working directory is *global*, |
---|
330 | so changing it _will_ affect the following command lines on those |
---|
331 | systems. |
---|
332 | |
---|
333 | |
---|
334 | File: make.info, Node: Parallel, Next: Errors, Prev: Execution, Up: Commands |
---|
335 | |
---|
336 | Parallel Execution |
---|
337 | ================== |
---|
338 | |
---|
339 | GNU `make' knows how to execute several commands at once. Normally, |
---|
340 | `make' will execute only one command at a time, waiting for it to |
---|
341 | finish before executing the next. However, the `-j' or `--jobs' option |
---|
342 | tells `make' to execute many commands simultaneously. |
---|
343 | |
---|
344 | On MS-DOS, the `-j' option has no effect, since that system doesn't |
---|
345 | support multi-processing. |
---|
346 | |
---|
347 | If the `-j' option is followed by an integer, this is the number of |
---|
348 | commands to execute at once; this is called the number of "job slots". |
---|
349 | If there is nothing looking like an integer after the `-j' option, |
---|
350 | there is no limit on the number of job slots. The default number of job |
---|
351 | slots is one, which means serial execution (one thing at a time). |
---|
352 | |
---|
353 | One unpleasant consequence of running several commands |
---|
354 | simultaneously is that output generated by the commands appears |
---|
355 | whenever each command sends it, so messages from different commands may |
---|
356 | be interspersed. |
---|
357 | |
---|
358 | Another problem is that two processes cannot both take input from the |
---|
359 | same device; so to make sure that only one command tries to take input |
---|
360 | from the terminal at once, `make' will invalidate the standard input |
---|
361 | streams of all but one running command. This means that attempting to |
---|
362 | read from standard input will usually be a fatal error (a `Broken pipe' |
---|
363 | signal) for most child processes if there are several. |
---|
364 | |
---|
365 | It is unpredictable which command will have a valid standard input |
---|
366 | stream (which will come from the terminal, or wherever you redirect the |
---|
367 | standard input of `make'). The first command run will always get it |
---|
368 | first, and the first command started after that one finishes will get |
---|
369 | it next, and so on. |
---|
370 | |
---|
371 | We will change how this aspect of `make' works if we find a better |
---|
372 | alternative. In the mean time, you should not rely on any command using |
---|
373 | standard input at all if you are using the parallel execution feature; |
---|
374 | but if you are not using this feature, then standard input works |
---|
375 | normally in all commands. |
---|
376 | |
---|
377 | Finally, handling recursive `make' invocations raises issues. For |
---|
378 | more information on this, see *Note Communicating Options to a |
---|
379 | Sub-`make': Options/Recursion. |
---|
380 | |
---|
381 | If a command fails (is killed by a signal or exits with a nonzero |
---|
382 | status), and errors are not ignored for that command (*note Errors in |
---|
383 | Commands: Errors.), the remaining command lines to remake the same |
---|
384 | target will not be run. If a command fails and the `-k' or |
---|
385 | `--keep-going' option was not given (*note Summary of Options: Options |
---|
386 | Summary.), `make' aborts execution. If make terminates for any reason |
---|
387 | (including a signal) with child processes running, it waits for them to |
---|
388 | finish before actually exiting. |
---|
389 | |
---|
390 | When the system is heavily loaded, you will probably want to run |
---|
391 | fewer jobs than when it is lightly loaded. You can use the `-l' option |
---|
392 | to tell `make' to limit the number of jobs to run at once, based on the |
---|
393 | load average. The `-l' or `--max-load' option is followed by a |
---|
394 | floating-point number. For example, |
---|
395 | |
---|
396 | -l 2.5 |
---|
397 | |
---|
398 | will not let `make' start more than one job if the load average is |
---|
399 | above 2.5. The `-l' option with no following number removes the load |
---|
400 | limit, if one was given with a previous `-l' option. |
---|
401 | |
---|
402 | More precisely, when `make' goes to start up a job, and it already |
---|
403 | has at least one job running, it checks the current load average; if it |
---|
404 | is not lower than the limit given with `-l', `make' waits until the load |
---|
405 | average goes below that limit, or until all the other jobs finish. |
---|
406 | |
---|
407 | By default, there is no load limit. |
---|
408 | |
---|
409 | |
---|
410 | File: make.info, Node: Errors, Next: Interrupts, Prev: Parallel, Up: Commands |
---|
411 | |
---|
412 | Errors in Commands |
---|
413 | ================== |
---|
414 | |
---|
415 | After each shell command returns, `make' looks at its exit status. |
---|
416 | If the command completed successfully, the next command line is executed |
---|
417 | in a new shell; after the last command line is finished, the rule is |
---|
418 | finished. |
---|
419 | |
---|
420 | If there is an error (the exit status is nonzero), `make' gives up on |
---|
421 | the current rule, and perhaps on all rules. |
---|
422 | |
---|
423 | Sometimes the failure of a certain command does not indicate a |
---|
424 | problem. For example, you may use the `mkdir' command to ensure that a |
---|
425 | directory exists. If the directory already exists, `mkdir' will report |
---|
426 | an error, but you probably want `make' to continue regardless. |
---|
427 | |
---|
428 | To ignore errors in a command line, write a `-' at the beginning of |
---|
429 | the line's text (after the initial tab). The `-' is discarded before |
---|
430 | the command is passed to the shell for execution. |
---|
431 | |
---|
432 | For example, |
---|
433 | |
---|
434 | clean: |
---|
435 | -rm -f *.o |
---|
436 | |
---|
437 | This causes `rm' to continue even if it is unable to remove a file. |
---|
438 | |
---|
439 | When you run `make' with the `-i' or `--ignore-errors' flag, errors |
---|
440 | are ignored in all commands of all rules. A rule in the makefile for |
---|
441 | the special target `.IGNORE' has the same effect, if there are no |
---|
442 | prerequisites. These ways of ignoring errors are obsolete because `-' |
---|
443 | is more flexible. |
---|
444 | |
---|
445 | When errors are to be ignored, because of either a `-' or the `-i' |
---|
446 | flag, `make' treats an error return just like success, except that it |
---|
447 | prints out a message that tells you the status code the command exited |
---|
448 | with, and says that the error has been ignored. |
---|
449 | |
---|
450 | When an error happens that `make' has not been told to ignore, it |
---|
451 | implies that the current target cannot be correctly remade, and neither |
---|
452 | can any other that depends on it either directly or indirectly. No |
---|
453 | further commands will be executed for these targets, since their |
---|
454 | preconditions have not been achieved. |
---|
455 | |
---|
456 | Normally `make' gives up immediately in this circumstance, returning |
---|
457 | a nonzero status. However, if the `-k' or `--keep-going' flag is |
---|
458 | specified, `make' continues to consider the other prerequisites of the |
---|
459 | pending targets, remaking them if necessary, before it gives up and |
---|
460 | returns nonzero status. For example, after an error in compiling one |
---|
461 | object file, `make -k' will continue compiling other object files even |
---|
462 | though it already knows that linking them will be impossible. *Note |
---|
463 | Summary of Options: Options Summary. |
---|
464 | |
---|
465 | The usual behavior assumes that your purpose is to get the specified |
---|
466 | targets up to date; once `make' learns that this is impossible, it |
---|
467 | might as well report the failure immediately. The `-k' option says |
---|
468 | that the real purpose is to test as many of the changes made in the |
---|
469 | program as possible, perhaps to find several independent problems so |
---|
470 | that you can correct them all before the next attempt to compile. This |
---|
471 | is why Emacs' `compile' command passes the `-k' flag by default. |
---|
472 | |
---|
473 | Usually when a command fails, if it has changed the target file at |
---|
474 | all, the file is corrupted and cannot be used--or at least it is not |
---|
475 | completely updated. Yet the file's timestamp says that it is now up to |
---|
476 | date, so the next time `make' runs, it will not try to update that |
---|
477 | file. The situation is just the same as when the command is killed by a |
---|
478 | signal; *note Interrupts::. So generally the right thing to do is to |
---|
479 | delete the target file if the command fails after beginning to change |
---|
480 | the file. `make' will do this if `.DELETE_ON_ERROR' appears as a |
---|
481 | target. This is almost always what you want `make' to do, but it is |
---|
482 | not historical practice; so for compatibility, you must explicitly |
---|
483 | request it. |
---|
484 | |
---|
485 | |
---|
486 | File: make.info, Node: Interrupts, Next: Recursion, Prev: Errors, Up: Commands |
---|
487 | |
---|
488 | Interrupting or Killing `make' |
---|
489 | ============================== |
---|
490 | |
---|
491 | If `make' gets a fatal signal while a command is executing, it may |
---|
492 | delete the target file that the command was supposed to update. This is |
---|
493 | done if the target file's last-modification time has changed since |
---|
494 | `make' first checked it. |
---|
495 | |
---|
496 | The purpose of deleting the target is to make sure that it is remade |
---|
497 | from scratch when `make' is next run. Why is this? Suppose you type |
---|
498 | `Ctrl-c' while a compiler is running, and it has begun to write an |
---|
499 | object file `foo.o'. The `Ctrl-c' kills the compiler, resulting in an |
---|
500 | incomplete file whose last-modification time is newer than the source |
---|
501 | file `foo.c'. But `make' also receives the `Ctrl-c' signal and deletes |
---|
502 | this incomplete file. If `make' did not do this, the next invocation |
---|
503 | of `make' would think that `foo.o' did not require updating--resulting |
---|
504 | in a strange error message from the linker when it tries to link an |
---|
505 | object file half of which is missing. |
---|
506 | |
---|
507 | You can prevent the deletion of a target file in this way by making |
---|
508 | the special target `.PRECIOUS' depend on it. Before remaking a target, |
---|
509 | `make' checks to see whether it appears on the prerequisites of |
---|
510 | `.PRECIOUS', and thereby decides whether the target should be deleted |
---|
511 | if a signal happens. Some reasons why you might do this are that the |
---|
512 | target is updated in some atomic fashion, or exists only to record a |
---|
513 | modification-time (its contents do not matter), or must exist at all |
---|
514 | times to prevent other sorts of trouble. |
---|
515 | |
---|
516 | |
---|
517 | File: make.info, Node: Recursion, Next: Sequences, Prev: Interrupts, Up: Commands |
---|
518 | |
---|
519 | Recursive Use of `make' |
---|
520 | ======================= |
---|
521 | |
---|
522 | Recursive use of `make' means using `make' as a command in a |
---|
523 | makefile. This technique is useful when you want separate makefiles for |
---|
524 | various subsystems that compose a larger system. For example, suppose |
---|
525 | you have a subdirectory `subdir' which has its own makefile, and you |
---|
526 | would like the containing directory's makefile to run `make' on the |
---|
527 | subdirectory. You can do it by writing this: |
---|
528 | |
---|
529 | subsystem: |
---|
530 | cd subdir && $(MAKE) |
---|
531 | |
---|
532 | or, equivalently, this (*note Summary of Options: Options Summary.): |
---|
533 | |
---|
534 | subsystem: |
---|
535 | $(MAKE) -C subdir |
---|
536 | |
---|
537 | You can write recursive `make' commands just by copying this example, |
---|
538 | but there are many things to know about how they work and why, and about |
---|
539 | how the sub-`make' relates to the top-level `make'. |
---|
540 | |
---|
541 | For your convenience, GNU `make' sets the variable `CURDIR' to the |
---|
542 | pathname of the current working directory for you. If `-C' is in |
---|
543 | effect, it will contain the path of the new directory, not the |
---|
544 | original. The value has the same precedence it would have if it were |
---|
545 | set in the makefile (by default, an environment variable `CURDIR' will |
---|
546 | not override this value). Note that setting this variable has no |
---|
547 | effect on the operation of `make' |
---|
548 | |
---|
549 | * Menu: |
---|
550 | |
---|
551 | * MAKE Variable:: The special effects of using `$(MAKE)'. |
---|
552 | * Variables/Recursion:: How to communicate variables to a sub-`make'. |
---|
553 | * Options/Recursion:: How to communicate options to a sub-`make'. |
---|
554 | * -w Option:: How the `-w' or `--print-directory' option |
---|
555 | helps debug use of recursive `make' commands. |
---|
556 | |
---|
557 | |
---|
558 | File: make.info, Node: MAKE Variable, Next: Variables/Recursion, Up: Recursion |
---|
559 | |
---|
560 | How the `MAKE' Variable Works |
---|
561 | ----------------------------- |
---|
562 | |
---|
563 | Recursive `make' commands should always use the variable `MAKE', not |
---|
564 | the explicit command name `make', as shown here: |
---|
565 | |
---|
566 | subsystem: |
---|
567 | cd subdir && $(MAKE) |
---|
568 | |
---|
569 | The value of this variable is the file name with which `make' was |
---|
570 | invoked. If this file name was `/bin/make', then the command executed |
---|
571 | is `cd subdir && /bin/make'. If you use a special version of `make' to |
---|
572 | run the top-level makefile, the same special version will be executed |
---|
573 | for recursive invocations. |
---|
574 | |
---|
575 | As a special feature, using the variable `MAKE' in the commands of a |
---|
576 | rule alters the effects of the `-t' (`--touch'), `-n' (`--just-print'), |
---|
577 | or `-q' (`--question') option. Using the `MAKE' variable has the same |
---|
578 | effect as using a `+' character at the beginning of the command line. |
---|
579 | *Note Instead of Executing the Commands: Instead of Execution. |
---|
580 | |
---|
581 | Consider the command `make -t' in the above example. (The `-t' |
---|
582 | option marks targets as up to date without actually running any |
---|
583 | commands; see *Note Instead of Execution::.) Following the usual |
---|
584 | definition of `-t', a `make -t' command in the example would create a |
---|
585 | file named `subsystem' and do nothing else. What you really want it to |
---|
586 | do is run `cd subdir && make -t'; but that would require executing the |
---|
587 | command, and `-t' says not to execute commands. |
---|
588 | |
---|
589 | The special feature makes this do what you want: whenever a command |
---|
590 | line of a rule contains the variable `MAKE', the flags `-t', `-n' and |
---|
591 | `-q' do not apply to that line. Command lines containing `MAKE' are |
---|
592 | executed normally despite the presence of a flag that causes most |
---|
593 | commands not to be run. The usual `MAKEFLAGS' mechanism passes the |
---|
594 | flags to the sub-`make' (*note Communicating Options to a Sub-`make': |
---|
595 | Options/Recursion.), so your request to touch the files, or print the |
---|
596 | commands, is propagated to the subsystem. |
---|
597 | |
---|
598 | |
---|
599 | File: make.info, Node: Variables/Recursion, Next: Options/Recursion, Prev: MAKE Variable, Up: Recursion |
---|
600 | |
---|
601 | Communicating Variables to a Sub-`make' |
---|
602 | --------------------------------------- |
---|
603 | |
---|
604 | Variable values of the top-level `make' can be passed to the |
---|
605 | sub-`make' through the environment by explicit request. These |
---|
606 | variables are defined in the sub-`make' as defaults, but do not |
---|
607 | override what is specified in the makefile used by the sub-`make' |
---|
608 | makefile unless you use the `-e' switch (*note Summary of Options: |
---|
609 | Options Summary.). |
---|
610 | |
---|
611 | To pass down, or "export", a variable, `make' adds the variable and |
---|
612 | its value to the environment for running each command. The sub-`make', |
---|
613 | in turn, uses the environment to initialize its table of variable |
---|
614 | values. *Note Variables from the Environment: Environment. |
---|
615 | |
---|
616 | Except by explicit request, `make' exports a variable only if it is |
---|
617 | either defined in the environment initially or set on the command line, |
---|
618 | and if its name consists only of letters, numbers, and underscores. |
---|
619 | Some shells cannot cope with environment variable names consisting of |
---|
620 | characters other than letters, numbers, and underscores. |
---|
621 | |
---|
622 | The special variables `SHELL' and `MAKEFLAGS' are always exported |
---|
623 | (unless you unexport them). `MAKEFILES' is exported if you set it to |
---|
624 | anything. |
---|
625 | |
---|
626 | `make' automatically passes down variable values that were defined |
---|
627 | on the command line, by putting them in the `MAKEFLAGS' variable. |
---|
628 | *Note Options/Recursion::. |
---|
629 | |
---|
630 | Variables are _not_ normally passed down if they were created by |
---|
631 | default by `make' (*note Variables Used by Implicit Rules: Implicit |
---|
632 | Variables.). The sub-`make' will define these for itself. |
---|
633 | |
---|
634 | If you want to export specific variables to a sub-`make', use the |
---|
635 | `export' directive, like this: |
---|
636 | |
---|
637 | export VARIABLE ... |
---|
638 | |
---|
639 | If you want to _prevent_ a variable from being exported, use the |
---|
640 | `unexport' directive, like this: |
---|
641 | |
---|
642 | unexport VARIABLE ... |
---|
643 | |
---|
644 | As a convenience, you can define a variable and export it at the same |
---|
645 | time by doing: |
---|
646 | |
---|
647 | export VARIABLE = value |
---|
648 | |
---|
649 | has the same result as: |
---|
650 | |
---|
651 | VARIABLE = value |
---|
652 | export VARIABLE |
---|
653 | |
---|
654 | and |
---|
655 | |
---|
656 | export VARIABLE := value |
---|
657 | |
---|
658 | has the same result as: |
---|
659 | |
---|
660 | VARIABLE := value |
---|
661 | export VARIABLE |
---|
662 | |
---|
663 | Likewise, |
---|
664 | |
---|
665 | export VARIABLE += value |
---|
666 | |
---|
667 | is just like: |
---|
668 | |
---|
669 | VARIABLE += value |
---|
670 | export VARIABLE |
---|
671 | |
---|
672 | *Note Appending More Text to Variables: Appending. |
---|
673 | |
---|
674 | You may notice that the `export' and `unexport' directives work in |
---|
675 | `make' in the same way they work in the shell, `sh'. |
---|
676 | |
---|
677 | If you want all variables to be exported by default, you can use |
---|
678 | `export' by itself: |
---|
679 | |
---|
680 | export |
---|
681 | |
---|
682 | This tells `make' that variables which are not explicitly mentioned in |
---|
683 | an `export' or `unexport' directive should be exported. Any variable |
---|
684 | given in an `unexport' directive will still _not_ be exported. If you |
---|
685 | use `export' by itself to export variables by default, variables whose |
---|
686 | names contain characters other than alphanumerics and underscores will |
---|
687 | not be exported unless specifically mentioned in an `export' directive. |
---|
688 | |
---|
689 | The behavior elicited by an `export' directive by itself was the |
---|
690 | default in older versions of GNU `make'. If your makefiles depend on |
---|
691 | this behavior and you want to be compatible with old versions of |
---|
692 | `make', you can write a rule for the special target |
---|
693 | `.EXPORT_ALL_VARIABLES' instead of using the `export' directive. This |
---|
694 | will be ignored by old `make's, while the `export' directive will cause |
---|
695 | a syntax error. |
---|
696 | |
---|
697 | Likewise, you can use `unexport' by itself to tell `make' _not_ to |
---|
698 | export variables by default. Since this is the default behavior, you |
---|
699 | would only need to do this if `export' had been used by itself earlier |
---|
700 | (in an included makefile, perhaps). You *cannot* use `export' and |
---|
701 | `unexport' by themselves to have variables exported for some commands |
---|
702 | and not for others. The last `export' or `unexport' directive that |
---|
703 | appears by itself determines the behavior for the entire run of `make'. |
---|
704 | |
---|
705 | As a special feature, the variable `MAKELEVEL' is changed when it is |
---|
706 | passed down from level to level. This variable's value is a string |
---|
707 | which is the depth of the level as a decimal number. The value is `0' |
---|
708 | for the top-level `make'; `1' for a sub-`make', `2' for a |
---|
709 | sub-sub-`make', and so on. The incrementation happens when `make' sets |
---|
710 | up the environment for a command. |
---|
711 | |
---|
712 | The main use of `MAKELEVEL' is to test it in a conditional directive |
---|
713 | (*note Conditional Parts of Makefiles: Conditionals.); this way you can |
---|
714 | write a makefile that behaves one way if run recursively and another |
---|
715 | way if run directly by you. |
---|
716 | |
---|
717 | You can use the variable `MAKEFILES' to cause all sub-`make' |
---|
718 | commands to use additional makefiles. The value of `MAKEFILES' is a |
---|
719 | whitespace-separated list of file names. This variable, if defined in |
---|
720 | the outer-level makefile, is passed down through the environment; then |
---|
721 | it serves as a list of extra makefiles for the sub-`make' to read |
---|
722 | before the usual or specified ones. *Note The Variable `MAKEFILES': |
---|
723 | MAKEFILES Variable. |
---|
724 | |
---|
725 | |
---|
726 | File: make.info, Node: Options/Recursion, Next: -w Option, Prev: Variables/Recursion, Up: Recursion |
---|
727 | |
---|
728 | Communicating Options to a Sub-`make' |
---|
729 | ------------------------------------- |
---|
730 | |
---|
731 | Flags such as `-s' and `-k' are passed automatically to the |
---|
732 | sub-`make' through the variable `MAKEFLAGS'. This variable is set up |
---|
733 | automatically by `make' to contain the flag letters that `make' |
---|
734 | received. Thus, if you do `make -ks' then `MAKEFLAGS' gets the value |
---|
735 | `ks'. |
---|
736 | |
---|
737 | As a consequence, every sub-`make' gets a value for `MAKEFLAGS' in |
---|
738 | its environment. In response, it takes the flags from that value and |
---|
739 | processes them as if they had been given as arguments. *Note Summary |
---|
740 | of Options: Options Summary. |
---|
741 | |
---|
742 | Likewise variables defined on the command line are passed to the |
---|
743 | sub-`make' through `MAKEFLAGS'. Words in the value of `MAKEFLAGS' that |
---|
744 | contain `=', `make' treats as variable definitions just as if they |
---|
745 | appeared on the command line. *Note Overriding Variables: Overriding. |
---|
746 | |
---|
747 | The options `-C', `-f', `-o', and `-W' are not put into `MAKEFLAGS'; |
---|
748 | these options are not passed down. |
---|
749 | |
---|
750 | The `-j' option is a special case (*note Parallel Execution: |
---|
751 | Parallel.). If you set it to some numeric value `N' and your operating |
---|
752 | system supports it (most any UNIX system will; others typically won't), |
---|
753 | the parent `make' and all the sub-`make's will communicate to ensure |
---|
754 | that there are only `N' jobs running at the same time between them all. |
---|
755 | Note that any job that is marked recursive (*note Instead of Executing |
---|
756 | the Commands: Instead of Execution.) doesn't count against the total |
---|
757 | jobs (otherwise we could get `N' sub-`make's running and have no slots |
---|
758 | left over for any real work!) |
---|
759 | |
---|
760 | If your operating system doesn't support the above communication, |
---|
761 | then `-j 1' is always put into `MAKEFLAGS' instead of the value you |
---|
762 | specified. This is because if the `-j' option were passed down to |
---|
763 | sub-`make's, you would get many more jobs running in parallel than you |
---|
764 | asked for. If you give `-j' with no numeric argument, meaning to run |
---|
765 | as many jobs as possible in parallel, this is passed down, since |
---|
766 | multiple infinities are no more than one. |
---|
767 | |
---|
768 | If you do not want to pass the other flags down, you must change the |
---|
769 | value of `MAKEFLAGS', like this: |
---|
770 | |
---|
771 | subsystem: |
---|
772 | cd subdir && $(MAKE) MAKEFLAGS= |
---|
773 | |
---|
774 | The command line variable definitions really appear in the variable |
---|
775 | `MAKEOVERRIDES', and `MAKEFLAGS' contains a reference to this variable. |
---|
776 | If you do want to pass flags down normally, but don't want to pass |
---|
777 | down the command line variable definitions, you can reset |
---|
778 | `MAKEOVERRIDES' to empty, like this: |
---|
779 | |
---|
780 | MAKEOVERRIDES = |
---|
781 | |
---|
782 | This is not usually useful to do. However, some systems have a small |
---|
783 | fixed limit on the size of the environment, and putting so much |
---|
784 | information into the value of `MAKEFLAGS' can exceed it. If you see |
---|
785 | the error message `Arg list too long', this may be the problem. (For |
---|
786 | strict compliance with POSIX.2, changing `MAKEOVERRIDES' does not |
---|
787 | affect `MAKEFLAGS' if the special target `.POSIX' appears in the |
---|
788 | makefile. You probably do not care about this.) |
---|
789 | |
---|
790 | A similar variable `MFLAGS' exists also, for historical |
---|
791 | compatibility. It has the same value as `MAKEFLAGS' except that it |
---|
792 | does not contain the command line variable definitions, and it always |
---|
793 | begins with a hyphen unless it is empty (`MAKEFLAGS' begins with a |
---|
794 | hyphen only when it begins with an option that has no single-letter |
---|
795 | version, such as `--warn-undefined-variables'). `MFLAGS' was |
---|
796 | traditionally used explicitly in the recursive `make' command, like |
---|
797 | this: |
---|
798 | |
---|
799 | subsystem: |
---|
800 | cd subdir && $(MAKE) $(MFLAGS) |
---|
801 | |
---|
802 | but now `MAKEFLAGS' makes this usage redundant. If you want your |
---|
803 | makefiles to be compatible with old `make' programs, use this |
---|
804 | technique; it will work fine with more modern `make' versions too. |
---|
805 | |
---|
806 | The `MAKEFLAGS' variable can also be useful if you want to have |
---|
807 | certain options, such as `-k' (*note Summary of Options: Options |
---|
808 | Summary.), set each time you run `make'. You simply put a value for |
---|
809 | `MAKEFLAGS' in your environment. You can also set `MAKEFLAGS' in a |
---|
810 | makefile, to specify additional flags that should also be in effect for |
---|
811 | that makefile. (Note that you cannot use `MFLAGS' this way. That |
---|
812 | variable is set only for compatibility; `make' does not interpret a |
---|
813 | value you set for it in any way.) |
---|
814 | |
---|
815 | When `make' interprets the value of `MAKEFLAGS' (either from the |
---|
816 | environment or from a makefile), it first prepends a hyphen if the value |
---|
817 | does not already begin with one. Then it chops the value into words |
---|
818 | separated by blanks, and parses these words as if they were options |
---|
819 | given on the command line (except that `-C', `-f', `-h', `-o', `-W', |
---|
820 | and their long-named versions are ignored; and there is no error for an |
---|
821 | invalid option). |
---|
822 | |
---|
823 | If you do put `MAKEFLAGS' in your environment, you should be sure not |
---|
824 | to include any options that will drastically affect the actions of |
---|
825 | `make' and undermine the purpose of makefiles and of `make' itself. |
---|
826 | For instance, the `-t', `-n', and `-q' options, if put in one of these |
---|
827 | variables, could have disastrous consequences and would certainly have |
---|
828 | at least surprising and probably annoying effects. |
---|
829 | |
---|
830 | |
---|
831 | File: make.info, Node: -w Option, Prev: Options/Recursion, Up: Recursion |
---|
832 | |
---|
833 | The `--print-directory' Option |
---|
834 | ------------------------------ |
---|
835 | |
---|
836 | If you use several levels of recursive `make' invocations, the `-w' |
---|
837 | or `--print-directory' option can make the output a lot easier to |
---|
838 | understand by showing each directory as `make' starts processing it and |
---|
839 | as `make' finishes processing it. For example, if `make -w' is run in |
---|
840 | the directory `/u/gnu/make', `make' will print a line of the form: |
---|
841 | |
---|
842 | make: Entering directory `/u/gnu/make'. |
---|
843 | |
---|
844 | before doing anything else, and a line of the form: |
---|
845 | |
---|
846 | make: Leaving directory `/u/gnu/make'. |
---|
847 | |
---|
848 | when processing is completed. |
---|
849 | |
---|
850 | Normally, you do not need to specify this option because `make' does |
---|
851 | it for you: `-w' is turned on automatically when you use the `-C' |
---|
852 | option, and in sub-`make's. `make' will not automatically turn on `-w' |
---|
853 | if you also use `-s', which says to be silent, or if you use |
---|
854 | `--no-print-directory' to explicitly disable it. |
---|
855 | |
---|
856 | |
---|
857 | File: make.info, Node: Sequences, Next: Empty Commands, Prev: Recursion, Up: Commands |
---|
858 | |
---|
859 | Defining Canned Command Sequences |
---|
860 | ================================= |
---|
861 | |
---|
862 | When the same sequence of commands is useful in making various |
---|
863 | targets, you can define it as a canned sequence with the `define' |
---|
864 | directive, and refer to the canned sequence from the rules for those |
---|
865 | targets. The canned sequence is actually a variable, so the name must |
---|
866 | not conflict with other variable names. |
---|
867 | |
---|
868 | Here is an example of defining a canned sequence of commands: |
---|
869 | |
---|
870 | define run-yacc |
---|
871 | yacc $(firstword $^) |
---|
872 | mv y.tab.c $@ |
---|
873 | endef |
---|
874 | |
---|
875 | Here `run-yacc' is the name of the variable being defined; `endef' |
---|
876 | marks the end of the definition; the lines in between are the commands. |
---|
877 | The `define' directive does not expand variable references and |
---|
878 | function calls in the canned sequence; the `$' characters, parentheses, |
---|
879 | variable names, and so on, all become part of the value of the variable |
---|
880 | you are defining. *Note Defining Variables Verbatim: Defining, for a |
---|
881 | complete explanation of `define'. |
---|
882 | |
---|
883 | The first command in this example runs Yacc on the first |
---|
884 | prerequisite of whichever rule uses the canned sequence. The output |
---|
885 | file from Yacc is always named `y.tab.c'. The second command moves the |
---|
886 | output to the rule's target file name. |
---|
887 | |
---|
888 | To use the canned sequence, substitute the variable into the |
---|
889 | commands of a rule. You can substitute it like any other variable |
---|
890 | (*note Basics of Variable References: Reference.). Because variables |
---|
891 | defined by `define' are recursively expanded variables, all the |
---|
892 | variable references you wrote inside the `define' are expanded now. |
---|
893 | For example: |
---|
894 | |
---|
895 | foo.c : foo.y |
---|
896 | $(run-yacc) |
---|
897 | |
---|
898 | `foo.y' will be substituted for the variable `$^' when it occurs in |
---|
899 | `run-yacc''s value, and `foo.c' for `$@'. |
---|
900 | |
---|
901 | This is a realistic example, but this particular one is not needed in |
---|
902 | practice because `make' has an implicit rule to figure out these |
---|
903 | commands based on the file names involved (*note Using Implicit Rules: |
---|
904 | Implicit Rules.). |
---|
905 | |
---|
906 | In command execution, each line of a canned sequence is treated just |
---|
907 | as if the line appeared on its own in the rule, preceded by a tab. In |
---|
908 | particular, `make' invokes a separate subshell for each line. You can |
---|
909 | use the special prefix characters that affect command lines (`@', `-', |
---|
910 | and `+') on each line of a canned sequence. *Note Writing the Commands |
---|
911 | in Rules: Commands. For example, using this canned sequence: |
---|
912 | |
---|
913 | define frobnicate |
---|
914 | @echo "frobnicating target $@" |
---|
915 | frob-step-1 $< -o $@-step-1 |
---|
916 | frob-step-2 $@-step-1 -o $@ |
---|
917 | endef |
---|
918 | |
---|
919 | `make' will not echo the first line, the `echo' command. But it _will_ |
---|
920 | echo the following two command lines. |
---|
921 | |
---|
922 | On the other hand, prefix characters on the command line that refers |
---|
923 | to a canned sequence apply to every line in the sequence. So the rule: |
---|
924 | |
---|
925 | frob.out: frob.in |
---|
926 | @$(frobnicate) |
---|
927 | |
---|
928 | does not echo _any_ commands. (*Note Command Echoing: Echoing, for a |
---|
929 | full explanation of `@'.) |
---|
930 | |
---|
931 | |
---|
932 | File: make.info, Node: Empty Commands, Prev: Sequences, Up: Commands |
---|
933 | |
---|
934 | Using Empty Commands |
---|
935 | ==================== |
---|
936 | |
---|
937 | It is sometimes useful to define commands which do nothing. This is |
---|
938 | done simply by giving a command that consists of nothing but |
---|
939 | whitespace. For example: |
---|
940 | |
---|
941 | target: ; |
---|
942 | |
---|
943 | defines an empty command string for `target'. You could also use a |
---|
944 | line beginning with a tab character to define an empty command string, |
---|
945 | but this would be confusing because such a line looks empty. |
---|
946 | |
---|
947 | You may be wondering why you would want to define a command string |
---|
948 | that does nothing. The only reason this is useful is to prevent a |
---|
949 | target from getting implicit commands (from implicit rules or the |
---|
950 | `.DEFAULT' special target; *note Implicit Rules:: and *note Defining |
---|
951 | Last-Resort Default Rules: Last Resort.). |
---|
952 | |
---|
953 | You may be inclined to define empty command strings for targets that |
---|
954 | are not actual files, but only exist so that their prerequisites can be |
---|
955 | remade. However, this is not the best way to do that, because the |
---|
956 | prerequisites may not be remade properly if the target file actually |
---|
957 | does exist. *Note Phony Targets: Phony Targets, for a better way to do |
---|
958 | this. |
---|
959 | |
---|
960 | |
---|
961 | File: make.info, Node: Using Variables, Next: Conditionals, Prev: Commands, Up: Top |
---|
962 | |
---|
963 | How to Use Variables |
---|
964 | ******************** |
---|
965 | |
---|
966 | A "variable" is a name defined in a makefile to represent a string |
---|
967 | of text, called the variable's "value". These values are substituted |
---|
968 | by explicit request into targets, prerequisites, commands, and other |
---|
969 | parts of the makefile. (In some other versions of `make', variables |
---|
970 | are called "macros".) |
---|
971 | |
---|
972 | Variables and functions in all parts of a makefile are expanded when |
---|
973 | read, except for the shell commands in rules, the right-hand sides of |
---|
974 | variable definitions using `=', and the bodies of variable definitions |
---|
975 | using the `define' directive. |
---|
976 | |
---|
977 | Variables can represent lists of file names, options to pass to |
---|
978 | compilers, programs to run, directories to look in for source files, |
---|
979 | directories to write output in, or anything else you can imagine. |
---|
980 | |
---|
981 | A variable name may be any sequence of characters not containing `:', |
---|
982 | `#', `=', or leading or trailing whitespace. However, variable names |
---|
983 | containing characters other than letters, numbers, and underscores |
---|
984 | should be avoided, as they may be given special meanings in the future, |
---|
985 | and with some shells they cannot be passed through the environment to a |
---|
986 | sub-`make' (*note Communicating Variables to a Sub-`make': |
---|
987 | Variables/Recursion.). |
---|
988 | |
---|
989 | Variable names are case-sensitive. The names `foo', `FOO', and |
---|
990 | `Foo' all refer to different variables. |
---|
991 | |
---|
992 | It is traditional to use upper case letters in variable names, but we |
---|
993 | recommend using lower case letters for variable names that serve |
---|
994 | internal purposes in the makefile, and reserving upper case for |
---|
995 | parameters that control implicit rules or for parameters that the user |
---|
996 | should override with command options (*note Overriding Variables: |
---|
997 | Overriding.). |
---|
998 | |
---|
999 | A few variables have names that are a single punctuation character or |
---|
1000 | just a few characters. These are the "automatic variables", and they |
---|
1001 | have particular specialized uses. *Note Automatic Variables: Automatic. |
---|
1002 | |
---|
1003 | * Menu: |
---|
1004 | |
---|
1005 | * Reference:: How to use the value of a variable. |
---|
1006 | * Flavors:: Variables come in two flavors. |
---|
1007 | * Advanced:: Advanced features for referencing a variable. |
---|
1008 | * Values:: All the ways variables get their values. |
---|
1009 | * Setting:: How to set a variable in the makefile. |
---|
1010 | * Appending:: How to append more text to the old value |
---|
1011 | of a variable. |
---|
1012 | * Override Directive:: How to set a variable in the makefile even if |
---|
1013 | the user has set it with a command argument. |
---|
1014 | * Defining:: An alternate way to set a variable |
---|
1015 | to a verbatim string. |
---|
1016 | * Environment:: Variable values can come from the environment. |
---|
1017 | * Target-specific:: Variable values can be defined on a per-target |
---|
1018 | basis. |
---|
1019 | * Pattern-specific:: Target-specific variable values can be applied |
---|
1020 | to a group of targets that match a pattern. |
---|
1021 | * Automatic:: Some special variables have predefined |
---|
1022 | meanings for use with implicit rules. |
---|
1023 | |
---|
1024 | |
---|
1025 | File: make.info, Node: Reference, Next: Flavors, Up: Using Variables |
---|
1026 | |
---|
1027 | Basics of Variable References |
---|
1028 | ============================= |
---|
1029 | |
---|
1030 | To substitute a variable's value, write a dollar sign followed by |
---|
1031 | the name of the variable in parentheses or braces: either `$(foo)' or |
---|
1032 | `${foo}' is a valid reference to the variable `foo'. This special |
---|
1033 | significance of `$' is why you must write `$$' to have the effect of a |
---|
1034 | single dollar sign in a file name or command. |
---|
1035 | |
---|
1036 | Variable references can be used in any context: targets, |
---|
1037 | prerequisites, commands, most directives, and new variable values. |
---|
1038 | Here is an example of a common case, where a variable holds the names |
---|
1039 | of all the object files in a program: |
---|
1040 | |
---|
1041 | objects = program.o foo.o utils.o |
---|
1042 | program : $(objects) |
---|
1043 | cc -o program $(objects) |
---|
1044 | |
---|
1045 | $(objects) : defs.h |
---|
1046 | |
---|
1047 | Variable references work by strict textual substitution. Thus, the |
---|
1048 | rule |
---|
1049 | |
---|
1050 | foo = c |
---|
1051 | prog.o : prog.$(foo) |
---|
1052 | $(foo)$(foo) -$(foo) prog.$(foo) |
---|
1053 | |
---|
1054 | could be used to compile a C program `prog.c'. Since spaces before the |
---|
1055 | variable value are ignored in variable assignments, the value of `foo' |
---|
1056 | is precisely `c'. (Don't actually write your makefiles this way!) |
---|
1057 | |
---|
1058 | A dollar sign followed by a character other than a dollar sign, |
---|
1059 | open-parenthesis or open-brace treats that single character as the |
---|
1060 | variable name. Thus, you could reference the variable `x' with `$x'. |
---|
1061 | However, this practice is strongly discouraged, except in the case of |
---|
1062 | the automatic variables (*note Automatic Variables: Automatic.). |
---|
1063 | |
---|