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

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