* Fixed a minor grammaticaly error.
[platform/upstream/make.git] / make.texinfo
1 \input texinfo  @c -*- Texinfo -*-
2 @setfilename make.info
3 @synindex vr fn
4
5 @c $Header$
6
7 @ifinfo
8 This file documents the GNU Make utility.
9
10 Copyright (C) 1988 Free Software Foundation, Inc.
11
12 Permission is granted to make and distribute verbatim copies of
13 this manual provided the copyright notice and this permission notice
14 are preserved on all copies.
15
16 @ignore
17 Permission is granted to process this file through TeX and print the
18 results, provided the printed document carries copying permission
19 notice identical to this one except for the removal of this paragraph
20 (this paragraph not being relevant to the printed manual).
21
22 @end ignore
23 Permission is granted to copy and distribute modified versions of this
24 manual under the conditions for verbatim copying, provided that the entire
25 resulting derived work is distributed under the terms of a permission
26 notice identical to this one.
27
28 Permission is granted to copy and distribute translations of this manual
29 into another language, under the above conditions for modified versions.
30 @end ifinfo
31 @c
32 @setchapternewpage odd
33 @settitle Make
34
35 @titlepage
36 @sp 6
37 @center @titlefont{GNU Make}
38 @sp 1
39 @center A Program for Directing Recompilation
40 @sp 2
41 @center April 1988
42 @sp 5
43 @center Richard M. Stallman, Roland McGrath
44 @page
45 @vskip 0pt plus 1filll
46 Copyright @copyright{} 1988 Free Software Foundation, Inc.
47
48 Permission is granted to make and distribute verbatim copies of
49 this manual provided the copyright notice and this permission notice
50 are preserved on all copies.
51
52 Permission is granted to copy and distribute modified versions of this
53 manual under the conditions for verbatim copying, provided that the entire
54 resulting derived work is distributed under the terms of a permission
55 notice identical to this one.
56
57 Permission is granted to copy and distribute translations of this manual
58 into another language, under the above conditions for modified versions.
59 @end titlepage
60 @page
61
62 @node Top, Bugs,, (DIR)
63 @chapter Overview of @code{make}
64
65 The purpose of the @code{make} utility is to determine automatically which
66 pieces of a large program need to be recompiled, and issue the commands to
67 recompile them.  This manual describes the GNU implementation of
68 @code{make}, which was implemented by Richard Stallman and Roland McGrath.
69
70 Our examples show C programs, since they are most common, but you can use
71 @code{make} with any programming language whose compiler can be run with a
72 shell command.  In fact, @code{make} is not limited to programs.  You can
73 use it to describe any task where some files must be updated automatically
74 from others whenever the others change.
75
76 To prepare to use @code{make}, you must write a file called
77 the @dfn{makefile} that describes the relationships among files
78 in your program, and the states the commands for updating each file.
79 In a program, typically the executable file is updated from object
80 files, which are in turn made by compiling source files.@refill
81
82 Once a suitable makefile exists, each time you change some source files,
83 this simple shell command:
84
85 @example
86 make
87 @end example
88
89 @noindent
90 suffices to perform all necessary recompilations.  The @code{make} program
91 uses the makefile data base and the last-modification times of the files to
92 decide which of the files need to be updated.  For each of those files, it
93 issues the commands recorded in the data base.
94
95 @iftex
96 Command arguments to @code{make} can be used to control which files should
97 be recompiled, or how.  @xref{Running}.
98 @end iftex
99
100 @menu
101 * Bugs::        If you have problems, or think you've found a bug.
102 * Simple::      A simple example explained.
103 * Makefiles::   The data base contains rules and variable definitions.
104 * Rules::       A rule says how and when to remake one file.
105 * Commands::    A rule contains shell commands that say how to remake.
106 * Variables::   A variable holds a text string for substitution into rules.
107 * Conditionals::Makefiles that do one thing or another depending on
108                  variable values.
109 * Functions::   Functions can do text-processing within @code{make}.
110
111 * Running::     How to run @code{make}; how you can adjust the way
112                  @code{make} uses the makefile.
113
114 * Implicit::    Implicit rules take over if the makefile doesn't say
115                  how a file is to be remade.
116 * Archives::    How to use @code{make} to update archive files.
117
118 * Features::    GNU @code{make}'s advanced features and how GNU @code{make}
119                  relates to other versions of @code{make}.
120 * Missing::     Features of other @code{make}s not supported
121                  by GNU @code{make}.
122
123 * Concept Index::Index of cross-references to where concepts are discussed.
124 * Name Index::  Index of cross-references for names of @code{make}'s
125                  variables, functions, special targets and directives.
126 @end menu
127
128 @node Bugs, Simple, Top, Top
129 @section Problems and Bugs
130
131 If you have problems with GNU @code{make} or think you've found a bug,
132 please report it to Roland McGrath; he doesn't promise to do anything
133 but he might well want to fix it.
134
135 Before reporting a bug, make sure you've actually found a real bug.
136 Carefully re-read the documentation and see if it really says you can do
137 what you're trying to do.  If it's not clear whether you should be able
138 to do something or not, report that too; it's a bug in the documentation!
139
140 Before reporting a bug or trying to fix it yourself, try to isolate it to
141 the smallest possible makefile that reproduces the problem.  Then send
142 us the makefile and the exact results @code{make} gave you.  Also say what
143 you expected to occur; this will help us decide whether the problem
144 was really in the documentation.
145
146 Once you've got a precise problem, send email to (Internet)
147 @samp{bug-gnu-utils@@prep.ai.mit.edu} or (UUCP)
148 @samp{mit-eddie!prep.ai.mit.edu!bug-gnu-utils}.  Please include the version
149 number of @code{make} you are using.  You can get this information with the
150 command @samp{make -v -f /dev/null}.@refill
151
152 Non-bug suggestions are always welcome as well.
153 If you have questions about things that are unclear in the documentation
154 or are just obscure features, ask Roland McGrath; he'll be happy to help
155 you out (but no promises).  You can send him electronic mail at Internet
156 address @samp{roland@@wheaties.ai.mit.edu} or UUCP path
157 @samp{mit-eddie!wheaties.ai.mit.edu!roland}.
158
159 @node Simple, Makefiles, Bugs, Top
160 @section Simple Example of @code{make}
161
162 Suppose we have a text editor consisting of eight C source files and three
163 header files.  We need a makefile to tell @code{make} how to compile and
164 link the editor.  Assume that all the C files include @file{defs.h}, but
165 only those defining editing commands include @file{commands.h} and only low
166 level files that change the editor buffer include @file{buffer.h}.
167
168 To recompile the editor, each changed C source file must be recompiled.  If
169 a header file has changed, to be safe each C source file that
170 includes the header file must be recompiled.  Each compilation produces an
171 object file corresponding to the source file.  Finally, if any source file
172 has been recompiled, all the object files, whether newly made or saved from
173 previous compilations, must be linked together to produce the new
174 executable editor.
175
176 Here is a straightforward makefile that describes these criteria and says
177 how to compile and link when the time comes:
178
179 @example
180 edit : main.o kbd.o commands.o display.o \
181        insert.o search.o files.o utils.o
182         cc -o edit main.o kbd.o commands.o display.o \
183                    insert.o search.o files.o utils.o
184
185 main.o : main.c defs.h
186         cc -c main.c
187 kbd.o : kbd.c defs.h command.h
188         cc -c kbd.c
189 commands.o : command.c defs.h command.h
190         cc -c commands.c
191 display.o : display.c defs.h buffer.h
192         cc -c display.c
193 insert.o : insert.c defs.h buffer.h
194         cc -c insert.c
195 search.o : search.c defs.h buffer.h
196         cc -c search.c
197 files.o : files.c defs.h buffer.h command.h
198         cc -c files.c
199 utils.o : utils.c defs.h
200         cc -c utils.c
201 @end example
202
203 We split each long line into two lines using a backslash-newline; this is
204 like using one long line, but is easier to read.
205
206 Each file that is generated by a program---that is to say, each file except
207 for source files---is the @dfn{target} of a @dfn{rule} (@pxref{Rules}).
208 (In this example, these are the object files such as @file{main.o},
209 @file{kbd.o}, etc., and the executable file @file{edit}.)  The target
210 appears at the beginning of a line, followed by a colon.
211
212 After the colon come the target's @dfn{dependencies}: all the files that
213 are used as input when the target file is updated.  A target file needs to
214 be recompiled or relinked if any of its dependencies changes.  In addition,
215 any dependencies that are themselves automatically generated should be
216 updated first.  In this example, @file{edit} depends on each of the eight
217 object files; the object file @file{main.o} depends on the source file
218 @file{main.c} and on the header file @file{defs.h}.
219
220 By default, @code{make} starts with the first rule (not counting rules
221 whose target names start with @samp{.}).  This is called the @dfn{default
222 goal}.  Therefore, we put the rule for the executable program @file{edit}
223 first.  The other rules are processed because their targets appear as
224 dependencies of the goal.
225
226 After each line containing a target and dependencies come one or more lines
227 of shell commands that say how to update the target file.  These lines
228 start with a tab to tell @code{make} that they are command lines.  But
229 @code{make} does not know anything about how the commands work.  It is up
230 to you to supply commands that will update the target file properly.
231 All @code{make} does is execute the commands you have specified when the
232 target file needs to be updated.
233
234 @subsection How @code{make} Processes This Makefile
235
236 After reading the makefile, @code{make} begins its real work by processing
237 the first rule, the one for relinking @file{edit}; but before it can fully
238 process this rule, it must process the rules for the files @file{edit}
239 depends on: all the object files.  Each of these files is processed
240 according to its own rule.  These rules say to update the @samp{.o} file by
241 compiling its source file.  The recompilation must be done if the source
242 file, or any of the header files named as dependencies, is more recent than
243 the object file, or if the object file does not exist.
244
245 Before recompiling an object file, @code{make} considers updating its
246 dependencies, the source file and header files.  This makefile does not
247 specify anything to be done for them---the @samp{.c} and @samp{.h} files
248 are not the targets of any rules---so nothing needs to be done.  But
249 automatically generated C programs, such as made by Yacc (or Bison), would
250 be updated by their own rules at this time.
251
252 After recompiling whichever object files need it, @code{make} can now
253 decide whether to relink @file{edit}.  This must be done if the file
254 @file{edit} does not exist, or if any of the object files are newer than
255 it.  If an object file was just recompiled, it is now newer than
256 @file{edit}, so @file{edit} will be relinked.
257
258 @subsection Variables Make Makefiles Simpler
259
260 In our example, we had to list all the object files twice in the rule for
261 @file{edit} (repeated here):
262
263 @example
264 edit : main.o kbd.o commands.o display.o \
265               insert.o search.o files.o utils.o
266         cc -o edit main.o kbd.o commands.o display.o \
267                    insert.o search.o files.o utils.o
268 @end example
269
270 @vindex objects
271 Such duplication is error-prone; if a new object file is added to the
272 system, we might add it to one list and forget the other.  We can eliminate
273 the risk and simplify the makefile by using a @dfn{variable}.  Variables
274 allow a text string to be defined once and substituted in multiple places
275 later (@pxref{Variables}).
276
277 It's standard practice for every makefile to have a variable named
278 @code{objects}, @code{OBJECTS}, @code{objs}, @code{OBJS}, @code{obj} or
279 @code{OBJ} which is a list of all object file names.  We would define
280 such a variable @code{objects} with a line like this in the makefile:@refill
281
282 @example
283 objects = main.o kbd.o commands.o display.o \
284               insert.o search.o files.o utils.o
285 @end example
286
287 @noindent
288 Then, each place we want to put a list of the object file names, we can
289 substitute the variable's value by writing @samp{$(objects)}
290 (@pxref{Variables}).  Here is how the rule for @code{edit} looks as a
291 result:
292
293 @example
294 edit : $(objects)
295         cc -o edit $(objects)
296 @end example
297
298 @subsection Letting @code{make} Deduce the Commands
299
300 It is not necessary to spell out the commands for compiling the individual
301 C source files, because @code{make} can figure them out: it has an
302 @dfn{implicit rule} for updating a @samp{.o} file from a correspondingly
303 named @samp{.c} file using a @samp{cc -c} command.  For example, it will
304 use the command @samp{cc -c main.c -o main.o} to compile @file{main.c} into
305 @file{main.o}.  We can therefore omit the commands from the rules for the
306 object files.  @xref{Implicit}.@refill
307
308 When a @samp{.c} file is used automatically in this way, it is also
309 automatically added to the list of dependencies.  We can therefore omit
310 the @samp{.c} files from the dependencies, provided we omit the commands.
311
312 Here is the entire example, with both of these changes, and a variable
313 @code{objects} as suggested above:
314
315 @example
316 objects =  main.o kbd.o commands.o display.o \
317  insert.o search.o files.o utils.o
318
319 edit : $(objects)
320         cc -o edit $(objects)
321
322 main.o : defs.h
323 kbd.o : defs.h command.h
324 commands.o : defs.h command.h
325 display.o : defs.h buffer.h
326 insert.o : defs.h buffer.h
327 search.o : defs.h buffer.h
328 files.o : defs.h buffer.h command.h
329 utils.o : defs.h
330 @end example
331
332 @noindent
333 This is how we would write the makefile in actual practice.
334
335 @subsection Another Style of Makefile
336
337 Since the rules for the object files specify only dependencies, no
338 commands, one can alternatively combine them by dependency instead of by
339 target.  Here is what it looks like:
340
341 @example
342 objects =  main.o kbd.o commands.o display.o \
343  insert.o search.o files.o utils.o
344
345 edit : $(objects)
346         cc -o edit $(objects)
347
348 $(objects) : defs.h
349 kbd.o commands.o files.o : command.h
350 display.o insert.o search.o files.o : buffer.h
351 @end example
352
353 @noindent
354 Here @file{defs.h} is given as a dependency of all the object files;
355 @file{commands.h} and @file{buffer.h} are dependencies of the specific
356 object files listed for them.
357
358 Whether this is better is a matter of taste: it is more compact, but some
359 people dislike it because they find it clearer to put all the information
360 about each target in one place.
361
362 @node Makefiles, Rules, Simple, Top
363 @chapter Writing Makefiles
364
365 @cindex makefile
366 The information that tells @code{make} how to recompile a system comes from
367 reading a data base called the @dfn{makefile}.
368
369 @menu
370 * Contents: Makefile Contents.   Overview of what you put in a makefile.
371 * Names: Makefile Names.         Where @code{make} finds the makefile.
372 * MAKEFILES Variable::           The environment can specify extra makefiles.
373 * Include::                      How one makefile can use another makefile.
374 @end menu
375
376 @node Makefile Contents, Makefile Names, Makefiles, Makefiles
377 @section What Makefiles Contain
378
379 Makefiles contain four kinds of things: @dfn{rules}, @dfn{variable
380 definitions}, @dfn{directives} and @dfn{comments}.  Rules, variables and
381 directives are described at length in later chapters.@refill
382
383 @itemize @bullet
384 @item
385 A rule says when and how to remake one or more files, called the rule's
386 @dfn{targets}.  It lists the other files that the targets @dfn{depend on},
387 and may also give commands to use to create or update the targets.
388 @xref{Rules}.
389
390 @item
391 A variable definition is a line that specifies a text string value for
392 a @dfn{variable} that can be substituted into the text later.  The
393 simple makefile example (@pxref{Simple}) shows a variable definition
394 for @code{objects} as a list of all object files.  @xref{Variables},
395 for full details.
396
397 @item
398 A directive is a command for @code{make} to do something special while
399 reading the makefile.  These include:
400
401 @itemize @bullet
402 @item
403 Reading another makefile (@pxref{Include}).
404
405 @item
406 Deciding (based on the values of variables) whether to use or
407 ignore a part of the makefile (@pxref{Conditionals}).
408
409 @item
410 Defining a variable from a verbatim string containing multiple lines
411 (@pxref{Defining}).
412 @end itemize
413
414 @item
415 @cindex comments
416 @samp{#} in a line of a makefile starts a comment.  It and the rest of
417 the line are ignored.  Comments may appear on any of the lines in the
418 makefile, except within a @code{define} directive, and perhaps within
419 commands (where the shell decides what is a comment).  A line
420 containing just a comment (with perhaps spaces before it) is
421 effectively blank, and is ignored.
422 @end itemize
423
424 @node Makefile Names, MAKEFILES Variable, Makefile Contents, Makefiles
425 @section What Name to Give Your Makefile
426
427 By default, when @code{make} looks for the makefile, it tries the names
428 @file{./makefile} and @file{./Makefile} in that order.  So normally you call
429 your makefile by one of these two names, and @code{make} finds it
430 automatically.  We recommend @file{Makefile} because it appears prominently
431 near the beginning of a directory listing (right near other important
432 files such as @file{README}).@refill
433
434 If @code{make} finds neither of these two names, it does not use any
435 makefile.  Then you must specify a goal with a command argument, and
436 @code{make} will attempt to figure out how to remake it using only its
437 built-in implicit rules.  @xref{Implicit}.
438
439 If you want to use a nonstandard name for your makefile, you can specify
440 the makefile name with the @samp{-f} option.  The arguments @samp{-f
441 @var{name}} tell @code{make} to read the file @var{name} as the makefile.
442 If you use more than one @samp{-f} option, you can specify several
443 makefiles.  All the makefiles are effectively concatenated in the order
444 specified.  The default makefile names @file{./makefile} and
445 @file{./Makefile} are not used if you specify @samp{-f}.@refill
446
447 @node MAKEFILES Variable, Include, Makefile Names, Makefiles
448 @section The Variable @code{MAKEFILES}
449
450 @vindex MAKEFILES
451 If the environment variable @code{MAKEFILES} is defined, @code{make}
452 considers its value as a list of names (separated by whitespace) of
453 additional makefiles to be read before the others.  This works much like
454 the @code{include} directive: various directories are searched for those
455 files and the default goal is never taken from them.  @xref{Include}.  In
456 addition, it is not an error if the files listed in @code{MAKEFILES} are
457 not found.@refill
458
459 The main use of @code{MAKEFILES} is in communication between recursive
460 invocations of @code{make} (@pxref{Recursion}).  It usually isn't
461 desirable to set the environment variable before a top-level invocation
462 of @code{make}, because it is usually better not to mess with a makefile
463 from outside.  However, if you are running @code{make} without a specific
464 makefile, a makefile in @code{MAKEFILES} can do useful things to help the
465 built-in implicit rules work better, such as defining search paths.
466
467 Some users are tempted to set @code{MAKEFILES} in the environment
468 automatically on login, and program makefiles to expect this to be done.
469 This is a very bad idea, because such makefiles will fail to work if run by
470 anyone else.  It is much better to write explicit @code{include} directives
471 in the makefiles.
472
473 @node Include,, MAKEFILES Variable, Makefiles
474 @section Including Other Makefiles
475
476 @findex include
477 The @code{include} directive tells @code{make} to suspend reading the
478 current makefile and read another makefile before continuing.  The
479 directive is a line in the makefile that looks like this:
480
481 @example
482 include @var{filename}
483 @end example
484
485 Extra spaces are allowed and ignored at the beginning of the line, but a
486 tab is not allowed.  (If the line begins with a tab, it will be considered
487 a command line.)  Whitespace is required between @code{include} and
488 @var{filename}; extra whitespace is ignored there and at the end of the
489 directive.  A comment starting with @samp{#} is allowed at the end of the
490 line.
491
492 Reading of the containing makefile is temporarily suspended while the file
493 @var{filename} is read as a makefile.  When that is finished, @code{make}
494 goes on with reading the makefile in which the directive appears.
495
496 The default goal target is never taken from an included makefile
497 (@pxref{Goals}).
498
499 One occasion for using @code{include} directives is when several programs,
500 handled by individual makefiles in various directories, need to use a
501 common set of variable definitions (@pxref{Setting}) or pattern rules
502 (@pxref{Pattern Rules}).
503
504 Another such occasion is when you want to automatically generate
505 dependencies from source files; the dependencies can be put in a file that
506 is included by the main makefile.  This practice is generally cleaner than
507 that of somehow appending the dependencies to the end of the main makefile
508 as has been traditionally done with other versions of @code{make}.
509
510 If the specified name does not start with a slash, and the file is not
511 found in the current directory, several other directories are searched.
512 First, any directories you have specified with the @samp{-I} option are
513 searched (@pxref{Options}).  Then the following directories (if they
514 exist) are searched, in this order: @file{/usr/gnu/include},
515 @file{/usr/local/include}, @file{/usr/include}.
516 If an included makefile cannot be found in any of these directories, a
517 warning message is generated, but it is not a fatal error.@refill
518
519 @node Rules, Commands, Makefiles, Top
520 @chapter Writing Rules
521
522 @cindex rule
523 @cindex target
524 @cindex dependency
525 A @dfn{rule} appears in the makefile and says when and how to remake
526 certain files, called the rule's @dfn{targets} (usually only one per rule).
527 It lists the other files that are the @dfn{dependencies} of the target, and
528 @dfn{commands} to use to create or update the target.
529
530 The order of rules is not significant, except for determining the
531 @dfn{default goal}: the target for @code{make} to consider, if you do not
532 otherwise specify one.  The default goal comes from the first rule (not
533 counting included makefiles) whose target does not start with a period.
534 Therefore, the first rule is normally one for compiling the entire program
535 or all the programs described by the makefile.  @xref{Goals}.
536
537 @menu
538 * Rule Example::        An explained example of a rule.
539 * Rule Syntax::         General syntax of rules, with explanation.
540
541 * Wildcards::           Using wildcard characters like `*' in file names.
542 * Directory Search::    Searching other directories for source files.
543
544 * Phony Targets::       Using a target that isn't a real file's name.
545 * Special Targets::     Targets with special built-in meanings.
546 * Empty Targets::       Real files that are empty--only the date matters.
547 * Multiple Targets::    When it is useful to have several targets in a rule.
548 * Static Pattern::      Static pattern rules apply to multiple targets
549                          and can vary the dependencies according to the
550                          target name.
551 * Multiple Rules::      Using several rules with the same target.
552 * Double-Colon::        Special kind of rule allowing
553                           several independent rules for one target.
554 * Commands::            Special features and details of how commands
555                          in a rule are executed.
556 @end menu
557
558 @ifinfo
559 @node Rule Example, Rule Syntax, Rules, Rules
560 @isubsection Rule Example
561
562 Here is an example of a rule:
563
564 @example
565 foo.o : foo.c defs.h       # module for twiddling the frobs
566         cc -c -g foo.c
567 @end example
568
569 Its target is @file{foo.o} and its dependencies are @file{foo.c} and
570 @file{defs.h}.  It has one command, which is @samp{cc -c -g foo.c}.
571 The command line starts with a tab to identify it as a command.
572
573 This rule says two things:
574
575 @itemize @bullet
576 @item
577 How to decide whether @file{foo.o} is out of date: it is out of date
578 if it does not exist, or if either @file{foo.c} or @file{defs.h} is
579 more recent than it.
580
581 @item
582 How to update the file @file{foo.o}: by running @code{cc} as stated.
583 The command does not explicitly mention @file{defs.h}, but we presume
584 that @file{foo.c} includes it, and that that is why @file{defs.h} was
585 added to the dependencies.
586 @end itemize
587 @end ifinfo
588
589 @node Rule Syntax, Wildcards, Rule Example, Rules
590 @section Rule Syntax
591
592 In general, a rule looks like this:
593
594 @example
595 @var{targets} : @var{dependencies}
596         @var{command}
597         @var{command}
598         ...
599 @end example
600
601 @noindent
602 or like this:
603
604 @example
605 @var{targets} : @var{dependencies} ; @var{command}
606         @var{command}
607         @var{command}
608         ...
609 @end example
610
611 The @var{targets} are file names, separated by spaces.  Wild card
612 characters may be used (@pxref{Wildcards}) and a name of the form
613 @file{@var{a}(@var{m})} represents member @var{m} in archive file @var{a}
614 (@pxref{Archive Members}).  Usually there is only one target per rule, but
615 occasionally there is a reason to have more (@pxref{Multiple Targets}).
616
617 The @var{command} lines start with a tab character.  The first command may
618 appear on the line after the dependencies, with a tab character, or may
619 appear on the same line, with a semicolon.  Either way, the effect is the
620 same.  @xref{Commands}.
621
622 Because dollar signs are used to start variable references, if you really
623 want a dollar sign in the rule you must write two of them (@samp{$$}).
624 @xref{Variables}.  A long line may be split by inserting a backslash
625 followed by a newline, but this is not required, as there is no limit on
626 the length of a line.
627
628 A rule tells @code{make} two things: when the targets are out of date,
629 and how to update them when necessary.
630
631 The criterion for being out of date is specified in terms of the
632 @var{dependencies}, which consist of file names separated by spaces.
633 (Wildcards and archive members are allowed here too.)  A target is out of
634 date if it does not exist or if it is older than any of the dependencies
635 (by comparison of last-modification times).  The idea is that the contents
636 of the target file are computed based on information in the dependencies,
637 so if any of the dependencies changes, the contents of the existing target
638 file are no longer necessarily valid.
639
640 How to update is specified by @var{commands}.  These are lines to be
641 executed by the shell (normally @samp{sh}), but with some extra features
642 (@pxref{Commands}).
643
644 @node Wildcards, Directory Search, Rule Syntax, Rules
645 @section Using Wildcards Characters in File Names
646 @cindex wildcard
647 @cindex file name
648
649 A single file name can specify many files using @dfn{wildcard characters}.
650 The wildcard characters in @code{make} are @samp{*}, @samp{?} and
651 @samp{[@dots{}]}, the same as in the Bourne shell.  For example, @file{*.c}
652 specifies a list of all the files (in the working directory) whose names
653 end in @samp{.c}.@refill
654
655 Wildcard expansion happens automatically in targets, in dependencies, and
656 in commands.  In other contexts, wildcard expansion happens only if you
657 request it explicitly with the @code{wildcard} function.
658
659 The special significance of a wildcard character can be turned off by
660 preceding it with a backslash.  Thus, @file{foo\*bar} would refer to a
661 specific file whose name consists of @samp{foo}, an asterisk, and
662 @samp{bar}.@refill
663
664 @menu
665 * Examples: Wildcard Examples.    Some simple examples.
666 * Pitfall: Wildcard Pitfall.      @code{*.o} won't do what you want!
667 * Function: Wildcard Function.
668        How to do wildcard expansion when defining a variable
669        using the function @code{wildcard}.
670 @end menu
671
672 @node Wildcard Examples, Wildcard Function, Wildcards, Wildcards
673 @subsection Wildcard Examples
674
675 Wildcards can be used in the commands of a rule.  For example, here is a
676 rule to delete all the object files:
677
678 @example
679 clean:
680         rm -f *.o
681 @end example
682
683 Wildcards are also useful in the dependencies of a rule.  With the
684 following rule in the makefile, @samp{make print} will print all the
685 @samp{.c} files that have changed since the last time you printed them:
686
687 @example
688 print: *.c
689         lpr -p $?
690         touch print
691 @end example
692
693 @noindent
694 This rule uses @file{print} as an empty target file; @pxref{Empty Targets}.
695
696 Wildcard expansion does not happen when you define a variable.  Thus, if
697 you write this:
698
699 @example
700 objects=*.o
701 @end example
702
703 @noindent
704 then the value of the variable @code{objects} is the actual string
705 @samp{*.o}.  However, if you use the value of @code{objects} in a target,
706 dependency or command, wildcard expansion will take place at that time.
707
708 @node Wildcard Pitfall, Wildcard Function, Wildcard Examples, Wildcards
709 @subsection Pitfalls of Using Wildcards
710
711 Now here is an example of a naive way of using wildcard expansion, that
712 does not do what you would intend.  Suppose you would like to say that the
713 executable file @file{foo} is made from all the object files in the
714 directory, and you write this:
715
716 @example
717 objects=*.o
718
719 foo : $(objects)
720         cc -o foo $(CFLAGS) $(objects)
721 @end example
722
723 @noindent
724 The value of @code{objects} is the actual string @samp{*.o}.  Wildcard
725 expansion happens in the rule for @file{foo}, so that each @emph{existing}
726 @samp{.o} file becomes a dependency of @file{foo} and will be recompiled if
727 necessary.
728
729 But what if you delete all the @samp{.o} files?  Then @samp{*.o} will
730 expand into @emph{nothing}.  The target @file{foo} will have no
731 dependencies and would be remade by linking no object files.  This is not
732 what you want!
733
734 Actually you can use wildcard expansion for this purpose, but you need more
735 sophisticated techniques, including the @code{wildcard} function and string
736 substitution.
737 @ifinfo
738 @xref{Wildcard Function}.
739 @end ifinfo
740 @iftex
741 These are described in the following section.
742 @end iftex
743
744 @node Wildcard Function,, Wildcard Pitfall, Wildcards
745 @subsection The Function @code{wildcard}
746 @findex wildcard
747
748 Wildcard expansion happens automatically in rules.  But wildcard expansion
749 does not normally take place when a variable is set, or inside the
750 arguments of a function.  If you want to do wildcard expansion in such
751 places, you need to use the @code{wildcard} function, like this:
752
753 @example
754 $(wildcard @var{pattern})
755 @end example
756
757 This string, used anywhere in a makefile, is replaced by a space-separated
758 list of names of existing files that match the pattern @var{pattern}.
759
760 One use of the @code{wildcard} function is to get a list of all the C source
761 files in a directory, like this:
762
763 @example
764 $(wildcard *.c)
765 @end example
766
767 We can change the list of C source files into a list of object files by
768 substituting @samp{.o} for @samp{.c} in the result, like this:
769
770 @example
771 $(subst .c,.o,$(wildcard *.c))
772 @end example
773
774 Here we have used another function, @code{subst} (@pxref{Text Functions}).
775
776 Thus, a makefile to compile all C source files in the directory and then
777 link them together could be written as follows:
778
779 @example
780 objects:=$(subst .c,.o,$(wildcard *.c))
781
782 foo : $(objects)
783         cc -o foo $(LDFLAGS) $(objects)
784 @end example
785
786 @noindent
787 (This takes advantage of the implicit rule for compiling C programs, so
788 there is no need to write explicit rules for compiling the files.)
789
790 @node Directory Search, Phony Targets, Wildcards, Rules
791 @section Searching Directories for Dependencies
792 @vindex VPATH
793 @findex vpath
794 @cindex vpath
795 @cindex search path for dependencies
796 @cindex directory search
797
798 For large systems, it is often desirable to put sources in a separate
799 directory from the binaries.  The @dfn{directory search} features of
800 @code{make} facilitate this by searching several directories automatically
801 to find a dependency.  When you redistribute the files among directories,
802 you do not need to change the individual rules, just the search paths.
803
804 @menu
805 * General Search::    The @code{VPATH} variable specifies a search path
806                         that applies to every dependency.
807 * Selective Search::  The @code{vpath} directive specifies a search path
808                         for a specified class of names.
809 * Commands/Search::   How to write shell commands that work together
810                         with search paths.
811 * Implicit/Search::   How search paths affect implicit rules.
812 * Libraries/Search::  Directory search for link libraries.
813 @end menu
814
815 @node General Search, Selective Search, Directory Search, Directory Search
816 @subsection @code{VPATH}: Search Path for All Dependencies
817
818 The value of the variable @code{VPATH} is a list of directories which
819 @code{make} should search (in the order specified) for dependency files.
820 The directory names are separated by colons.  For example:
821
822 @example
823 VPATH = src:../headers
824 @end example
825
826 @noindent
827 specifies a path containing two directories, @file{src} and @file{../headers}.
828
829 Whenever a file listed as a dependency does not exist in the current
830 directory, the directories listed in @code{VPATH} are searched for a file
831 with that name.  If a file is found in one of them, that file becomes the
832 dependency.  Rules may then specify the names of source files as if they
833 all existed in the current directory.
834
835 Using the value of @code{VPATH} set in the previous example, a rule like this:
836
837 @example
838 foo.o : foo.c
839 @end example
840
841 @noindent
842 is interpreted as if it were written like this:
843
844 @example
845 foo.o : src/foo.c
846 @end example
847
848 @noindent
849 assuming the file @file{foo.c} does not exist in the current directory but
850 is found in the directory @file{src}.
851
852 @node Selective Search, Commands/Search, General Search, Directory Search
853 @subsection The @code{vpath} Directive
854
855 Similar to the @code{VPATH} variable but more selective is the @code{vpath}
856 directive, which allows you to specify a search path for a particular class
857 of filenames, those that match a particular pattern.  Thus you can supply
858 certain search directories for one class of filenames and other directories
859 (or none) for other filenames.
860
861 There are three forms of the @code{vpath} directive:
862
863 @table @code
864 @item vpath @var{pattern} @var{directories}
865 Specify the search path @var{directories} for filenames that match
866 @code{pattern}.  If another path was previously specified for the same
867 pattern, the new path replaces it.  Note that it does @emph{not} add
868 to the old path for this pattern.@refill
869
870 The search path, @var{directories}, is a colon-separated list of
871 directories to be searched, just like the search path used in the
872 @code{VPATH} variable.
873
874 @item vpath @var{pattern}
875 Clear out the search path associated with @var{pattern}.
876
877 @item vpath
878 Clear all search paths previously specified with @code{vpath} directives.
879 @end table
880
881 A @code{vpath} pattern is a string containing a @samp{%} character.  The
882 string must match the filename of a dependency that is being searched for,
883 the @samp{%} character matching any sequence of zero or more characters (as
884 in pattern rules; @pxref{Pattern Rules}).  (It is valid to omit the
885 @samp{%}, but then the pattern must match the dependency exactly, which may
886 not be very useful.)
887
888 When a dependency fails to exist in the current directory, if the
889 @var{pattern} in a @code{vpath} directive matches the name of the
890 dependency file, then the @var{directories} in that directive are searched
891 just like (and before) the directories in the @code{VPATH} variable.@refill
892
893 If several @code{vpath} patterns match the dependency file's name, then
894 @code{make} processes each matching @code{vpath} directive one by one,
895 searching all the directories mentioned in each directive.  The @code{vpath}
896 directives are processed in the order in which they appear in the makefiles.
897
898 @node Commands/Search, Implicit/Search, Selective Search, Directory Search
899 @subsection Writing Shell-Commands with Directory Search
900
901 When a dependency is found in another directory through directory search,
902 this cannot change the commands of the rule; they will execute as written.
903 Therefore, you must write the commands with care so that they will look for
904 the dependency in the directory where @code{make} finds it.
905
906 This is done with the @dfn{automatic variables} such as @samp{$^}
907 (@pxref{Automatic}).  For instance, the value of @samp{$^} is a list of all
908 the dependencies of the rule, including the names of the directories in
909 which they were found, and the value of @samp{$@@} is the target.  Thus:
910
911 @example
912 foo.o : foo.c
913         cc -c $(CFLAGS) $^ -o $@@
914 @end example
915
916 @noindent
917 The variable @code{CFLAGS} exists so you can specify flags for C
918 compilation by implicit rule; we use it here for consistency so it will
919 affect all C compilations uniformly (@pxref{Implicit Variables}).
920
921 Often the dependencies include header files as well, which you don't want
922 to mention in the commands.  The function @code{firstword} can be used to
923 extract just the first dependency from the entire list, as shown here
924 (@pxref{Filename Functions}):
925
926 @example
927 VPATH = src:../headers
928 foo.o : foo.c defs.h hack.h
929         cc -c $(CFLAGS) $(firstword $^) -o $@@
930 @end example
931
932 @noindent
933 Here the value of @samp{$^} would be something like @samp{src/foo.c
934 ../headers/defs.h hack.h}, from which @samp{$(firstword $^)} extracts just
935 @samp{src/foo.c}.@refill
936
937 @node Implicit/Search, Libraries/Search, Commands/Search, Directory Search
938 @subsection Directory Search and Implicit Rules
939
940 The search through the directories specified in @code{VPATH} or with
941 @code{vpath} happens also during consideration of implicit rules
942 (@pxref{Implicit}).
943
944 For example, when a file @file{foo.o} has no explicit rule, @code{make}
945 considers implicit rules, such as to compile @file{foo.c} if that file
946 exists.  If such a file is lacking in the current directory, the
947 appropriate directories are searched for it.  If @file{foo.c} exists (or is
948 mentioned in the makefile) in any of the directories, the implicit rule for
949 C compilation is applicable.
950
951 The commands of all the built-in implicit rules normally use automatic
952 variables as a matter of necessity; consequently they will use the file
953 names found by directory search with no extra effort.
954
955 @node Libraries/Search,, Implicit/Search, Directory Search
956 @subsection Directory Search for Link Libraries
957
958 Directory search applies in a special way to libraries used with the
959 linker.  This special feature comes into play when you write a dependency
960 whose name is of the form @samp{-l@var{name}}.  (You can tell something
961 funny is going on here because the dependency is normally the name of a
962 file, and the @emph{file name} of the library looks like
963 @file{lib@var{name}.a}, not like @samp{-l@var{name}}.)@refill
964
965 When a dependency's name has the form @samp{-l@var{name}}, @code{make}
966 handles it specially by searching for the file @samp{lib@var{name}.a} in
967 the directories @samp{/lib} and @samp{/usr/lib}, and then using matching
968 @code{vpath} search paths and the @code{VPATH} search path.@refill
969
970 For example,
971
972 @example
973 foo : foo.c -lcurses
974         cc $^ -o $@@
975 @end example
976
977 @noindent
978 would cause the command @samp{cc foo.c -lcurses -o foo} to be executed when
979 @file{foo} is older than @file{foo.c} or than @file{libcurses.a} (which has
980 probably been found by directory search in the file
981 @file{/usr/lib/libcurses.a}).@refill
982
983 As shown by the example above, the file name found by directory search is
984 used only for comparing the file time with the target file's time.  It
985 does not replace the file's name in later usage (such as in automatic
986 variables like @code{$^}); the name remains unchanged, still starting
987 with @samp{-l}.  This leads to the correct results because the linker
988 will repeat the appropriate search when it processes its arguments.@refill
989
990 @node Phony Targets, Empty Targets, Directory Search, Rules
991 @section Phony Targets
992 @cindex phony targets
993
994 A phony target is one that is not really the name of a file.
995 It is only a name for some commands to be executed when explicitly
996 requested.
997
998 If you write a rule whose commands will not create the target file, the
999 commands will be executed every time the target comes up for remaking.
1000 Here is an example:
1001
1002 @example
1003 clean:
1004         rm *.o temp
1005 @end example
1006
1007 @noindent
1008 Because the @code{rm} command does not create a file named @file{clean},
1009 probably no such file will ever exist.  Therefore, the @code{rm} command
1010 will be executed every time you say @samp{make clean}.
1011
1012 @findex .PHONY
1013 The phony target will cease to work if anything ever does create a file
1014 named @file{clean} in this directory.  Since there are no dependencies, the
1015 @file{clean} would be considered up to date and its commands would not be
1016 executed.  To avoid this problem, you can explicitly declare the target to
1017 be phony, using the special target @code{.PHONY} (@pxref{Special Targets})
1018 as follows:
1019
1020 @example
1021 .PHONY : clean
1022 @end example
1023
1024 @noindent
1025 Once this is done, @code{make} will run the commands regardless of whether
1026 there is a file named @file{clean}.
1027
1028 A phony target should not be a dependency of a real target file; strange
1029 things can result from that.  As long as you don't do that, the phony
1030 target commands will be executed only when the phony target is a goal
1031 (@pxref{Goals}).
1032
1033 Phony targets can have dependencies.  When one directory contains multiple
1034 programs, it is most convenient to describe all of the programs in one
1035 makefile @file{./Makefile}.  Since the target remade by default will be the
1036 first one in the makefile, it is common to make this a phony target named
1037 @samp{all} and give it, as dependencies, all the individual programs.  For
1038 example:
1039
1040 @example
1041 all : prog1 prog2 prog3
1042 .PHONY : all
1043
1044 prog1 : prog1.o utils.o
1045         cc -o prog1 prog1.o utils.o
1046
1047 prog2 : prog2.o
1048         cc -o prog2 prog2.o
1049
1050 prog3 : prog3.o sort.o utils.o
1051         cc -o prog3 prog3.o sort.o utils.o
1052 @end example
1053
1054 @noindent
1055 Now you can say just @samp{make} to remake all three programs, or specify
1056 as arguments the ones to remake (as in @samp{make prog1 prog3}).
1057
1058 When one phony target is a dependency of another, it serves as a subroutine
1059 of the other.  For example, here @samp{make cleanall} will delete the
1060 object files, the difference files, and the file @file{program}:
1061
1062 @example
1063 cleanall : cleanobj cleandiff
1064         rm program
1065
1066 cleanobj :
1067         rm *.o
1068
1069 cleandiff :
1070         rm *.diff
1071 @end example
1072
1073 @node Empty Targets, Special Targets, Phony Targets, Rules
1074 @section Empty Target Files to Record Events
1075 @cindex empty targets
1076
1077 The @dfn{empty target} is a variant of the phony target; it is used to hold
1078 commands for an action that you request explicitly from time to time.
1079 Unlike a phony target, this target file can really exist; but the file's
1080 contents do not matter, and usually are empty.
1081
1082 The purpose of the empty target file is to record, with its
1083 last-modification-time, when the rule's commands were last executed.  It
1084 does so because one of the commands is a @code{touch} command to update the
1085 target file.
1086
1087 The empty target file must have some dependencies.  When you ask to remake
1088 the empty target, the commands are executed if any dependency is more
1089 recent than the target; in other words, if a dependency has changed since
1090 the last time you remade the target.  Here is an example:
1091
1092 @example
1093 print: foo.c bar.c
1094         lpr -p $?
1095         touch print
1096 @end example
1097
1098 @noindent
1099 With this rule, @samp{make print} will execute the @code{lpr} command if
1100 either source file has changed since the last @samp{make print}.  The
1101 automatic variable @samp{$?} is used to print only those files that have
1102 changed (@pxref{Automatic}).
1103
1104 @node Special Targets, Multiple Targets, Empty Targets, Rules
1105 @section Special Built-in Target Names
1106 @cindex special targets
1107
1108 Certain names have special meanings if they appear as targets.
1109
1110 @table @code
1111 @item .PHONY
1112 The dependencies of the special target @code{.PHONY} are considered to
1113 be phony targets.  When it is time to consider such a target,
1114 @code{make} will run its commands unconditionally, regardless of
1115 whether a file with that name exists or what its last-modification
1116 time is.  @xref{Phony Targets}.
1117
1118 @item .SUFFIXES
1119 The dependencies of the special target @code{.SUFFIXES} are the list
1120 of suffixes to be used in checking for suffix rules (@pxref{Suffix
1121 Rules}).
1122
1123 @item .DEFAULT
1124 The commands specified for @code{.DEFAULT} are used for any target for
1125 which no other commands are known (either explicitly or through an
1126 implicit rule).  If @code{.DEFAULT} commands are specified, every
1127 nonexistent file mentioned as a dependency will have these commands
1128 executed on its behalf.  @xref{Search Algorithm}.
1129
1130 @item .PRECIOUS
1131 @cindex precious targets
1132 The targets which @code{.PRECIOUS} depends on are given this special
1133 treatment: if @code{make} is killed or interrupted during the
1134 execution of their commands, the target is not deleted.
1135 @xref{Interrupts}.
1136
1137 @item .IGNORE
1138 Simply by being mentioned as a target, @code{.IGNORE} says to ignore
1139 errors in execution of commands.  The dependencies and commands for
1140 @code{.IGNORE} are not meaningful.
1141
1142 @samp{.IGNORE} exists for historical compatibility.  Since
1143 @code{.IGNORE} affects every command in the makefile, it is not very
1144 useful; we recommend you use the more selective ways to ignore errors
1145 in specific commands (@pxref{Errors}).
1146
1147 @item .SILENT
1148 Simply by being mentioned as a target, @code{.SILENT} says not to
1149 print commands before executing them.  The dependencies and commands
1150 for @code{.SILENT} are not meaningful.
1151
1152 @samp{.SILENT} exists for historical compatibility.  We recommend you
1153 use the more selective ways to silence specific commands
1154 (@pxref{Echoing}).
1155 @end table
1156
1157 An entire class of special targets have names made of the concatenation of
1158 two implicit rule suffixes (two members of the list of dependencies of
1159 @code{.SUFFIXES}).  Such special targets are suffix rules, an obsolete way
1160 of defining implicit rules (but a way still widely used).  In principle,
1161 any target name could be special in this way if you break it in two and add
1162 both pieces to the suffix list.  In practice, suffixes normally begin with
1163 @samp{.}, so these special target names also begin with @samp{.}.
1164 @xref{Suffix Rules}.
1165
1166 @node Multiple Targets, Multiple Rules, Special Targets, Rules
1167 @section Multiple Targets in a Rule
1168
1169 A rule with multiple targets is equivalent to writing many rules, each with
1170 one target, and all identical aside from that.  The same commands apply to
1171 all the targets, but their effects may vary because you can substitute the
1172 actual target name into the command using @samp{$@@}.  The rule contributes
1173 the same dependencies to all the targets also.
1174
1175 This is useful in two cases.
1176
1177 @itemize @bullet
1178 @item
1179 You want just dependencies, no commands.  For example:
1180
1181 @example
1182 kbd.o commands.o files.o: command.h
1183 @end example
1184
1185 @noindent
1186 gives an additional dependency to each of the three object files
1187 mentioned.
1188
1189 @item
1190 Similar commands work for all the targets.  The commands do not need
1191 to be absolutely identical, since the automatic variable @samp{$@@}
1192 can be used to substitute the particular target to be remade into the
1193 commands (@pxref{Automatic}).  For example:
1194
1195 @group
1196 @example
1197 bigoutput littleoutput : text.g
1198         generate text.g -$(subst output,,$@@) > $@@
1199 @end example
1200 @end group
1201
1202 @noindent
1203 is equivalent to
1204
1205 @example
1206 bigoutput : text.g
1207         generate text.g -big > bigoutput
1208 littleoutput : text.g
1209         generate text.g -little > littleoutput
1210 @end example
1211
1212 @noindent
1213 Here we assume the hypothetical program @code{generate} makes two
1214 types of output, one if given @samp{-big} and one if given
1215 @samp{-little}.@refill
1216 @end itemize
1217
1218 @ifinfo
1219 Suppose you would like to vary the dependencies according to the target,
1220 much as the variable @samp{$@@} allows you to vary the commands.
1221 You cannot do this with multiple targets in an ordinary rule, but you can
1222 do it with a @dfn{static pattern rule}.  @xref{Static Pattern}.
1223 @end ifinfo
1224
1225 @node Static Pattern, Multiple Rules, Multiple Targets, Rules
1226 @section Static Pattern Rules
1227 @cindex static pattern rules
1228 @cindex varying dependencies
1229
1230 @dfn{Static pattern rules} are rules which specify multiple targets and
1231 construct the dependency names for each target based on the target name.
1232 They are more general than ordinary rules with multiple targets because the
1233 targets don't have to have identical dependencies.  Their dependencies must
1234 be @emph{analogous}, but not necessarily @emph{identical}.
1235
1236 @menu
1237 * Usage: Static Usage.  How to use static pattern rules.
1238 * Static vs Implicit::  When are they better than implicit rules?
1239 @end menu
1240
1241 @node Static Usage, Static vs Implicit, Static Pattern, Static Pattern
1242 @subsection Syntax of Static Pattern Rules
1243
1244 Here is the syntax of a static pattern rule:
1245
1246 @example
1247 @var{targets}: @var{target-pattern}: @var{dep-patterns} @dots{}
1248         @var{commands}
1249         @dots{}
1250 @end example
1251
1252 @noindent
1253 The @var{targets} gives the list of targets that the rule applies to.  The
1254 targets can contain wildcard characters, just like the targets of ordinary
1255 rules (@pxref{Wildcards}).
1256
1257 The @var{target-pattern} and @var{dep-patterns} say how to compute the
1258 dependencies of each target.  Each target is matched against the
1259 @var{target-pattern} to extract a part of the target name, called the
1260 @dfn{stem}.  This stem is substituted into each of the @var{dep-patterns}
1261 to make the dependency names (one from each @var{dep-pattern}).
1262
1263 Each pattern normally contains the character @samp{%} just once.  When the
1264 @var{target-pattern} matches a target, the @samp{%} can match any part of
1265 the target name; this part is called the @dfn{stem}.  The rest of the
1266 pattern must match exactly.  For example, the target @file{foo.o} matches
1267 the pattern @samp{%.o}, with @samp{foo} as the stem.  The targets
1268 @file{foo.c} and @file{foo.out} don't match that pattern.@refill
1269
1270 The dependency names for each target are made by substituting the stem for
1271 the @samp{%} in each dependency pattern.  For example, if one dependency
1272 pattern is @file{%.c}, then substitution of the stem @samp{foo} gives the
1273 dependency name @file{foo.c}.  It is fine to write a dependency pattern that
1274 doesn't contain @samp{%}; then this dependency is the same for all targets.
1275
1276 Here is an example, which compiles each of @file{foo.o} and @file{bar.o}
1277 from the corresponding @file{.c} file:
1278
1279 @example
1280 objects := foo.o bar.o
1281
1282 $(objects): %.o: %.c
1283         $(CC) -c $(CFLAGS) $< -o $@@
1284 @end example
1285
1286 Each target specified must match the target pattern; a warning is issued
1287 for each that does not.  If you have a list of files, only some of which
1288 will match the pattern, you can use the @code{filter} function to remove
1289 nonmatching filenames (@pxref{Text Functions}):
1290
1291 @example
1292 files := foo.elc bar.o
1293
1294 $(filter %.o,$(files)): %.o: %.c
1295         $(CC) -c $(CFLAGS) $< -o $@@
1296 $(filter %.elc,$(files)): %.elc: %.el
1297         emacs -f batch-byte-compile $<
1298 @end example
1299
1300 @noindent
1301 Here the result of @samp{$(filter %.o,$(files))} is just @file{bar.o}, and
1302 the first static pattern rule causes it to be made from @file{bar.c}.  The
1303 result of @samp{$(filter %.elc,$(files))} is @file{foo.elc}, which is
1304 made from @file{foo.el}.@refill
1305
1306 @node Static vs Implicit,, Static Usage, Static Pattern
1307 @subsection Static Pattern Rules versus Implicit Rules
1308
1309 A static pattern rule has much in common with an implicit rule defined as a
1310 pattern rule (@pxref{Pattern Rules}).  Both have a pattern for the target
1311 and patterns for constructing the names of dependencies.  The difference is
1312 in how @code{make} decides @emph{when} the rule applies.
1313
1314 An implicit rule @emph{can} apply to any target that matches its pattern,
1315 but it @emph{does} apply only when the target has no commands otherwise
1316 specified, and only when the dependencies can be found.  If more than one
1317 implicit rule appears applicable, only one applies; the choice depends on
1318 the order of rules.
1319
1320 By contrast, a static pattern rule applies to the precise list of targets
1321 that you specify in the rule.  It cannot apply to any other target and it
1322 invariably does apply to each of the targets specified.  If two conflicting
1323 rules apply, and both have commands, that's an error.
1324
1325 The static pattern rule can be better than an implicit rule for these
1326 reasons:
1327
1328 @itemize @bullet
1329 @item
1330 You may wish to override the usual implicit rule for a few files whose
1331 names cannot be categorized syntactically but can be given in an
1332 explicit list.
1333
1334 @item
1335 If you cannot be sure of the precise contents of the directories you
1336 are using, you may not be sure which other irrelevant files might lead
1337 @code{make} to use the wrong implicit rule.  The choice might depend
1338 on the order in which the implicit rule search is done.  With static
1339 pattern rules, there is no uncertainty: each rule applies to precisely
1340 the targets specified.
1341 @end itemize
1342
1343 @node Multiple Rules, Double-Colon, Static Pattern, Rules
1344 @section Multiple Rules for One Target
1345
1346 One file can be the target of several rules if at most one rule has commands.
1347 The other rules can only have dependencies.  All the dependencies mentioned
1348 in all the rules are merged into one list of dependencies for the target.
1349 If the target is older than any dependency from any rule, the commands are
1350 executed.
1351
1352 An extra rule with just dependencies can be used to give a few extra
1353 dependencies to many files at once.  For example, one usually has a
1354 variable named @code{objects} containing a list of all the compiler output
1355 files in the system being made.  An easy way to say that all of them must
1356 be recompiled if @file{config.h} changes is to write
1357
1358 @example
1359 objects = foo.o bar.o
1360 foo.o : defs.h
1361 bar.o : defs.h test.h
1362 $(objects) : config.h
1363 @end example
1364
1365 This could be inserted or taken out without changing the rules that really
1366 say how to make the object files, making it a convenient form to use if
1367 you wish to add the additional dependency intermittently.
1368
1369 Another wrinkle is that the additional dependencies could be specified with
1370 a variable that you could set with a command argument to @code{make}
1371 (@pxref{Overriding}).  For example,
1372
1373 @example
1374 extradeps=
1375 $(objects) : $(extradeps)
1376 @end example
1377
1378 @noindent
1379 means that the command @samp{make extradeps=foo.h} will consider
1380 @file{foo.h} as a dependency of each object file, but plain @samp{make}
1381 will not.
1382
1383 If none of the explicit rules for a target has commands, then @code{make}
1384 searches for an applicable implicit rule to find some commands.
1385 @xref{Implicit}.
1386
1387 @node Double-Colon,, Multiple Rules, Rules
1388 @section Double-Colon Rules
1389 @cindex double-colon rule
1390
1391 @dfn{Double-colon} rules are rules written with @samp{::} instead of
1392 @samp{:} after the target names.  They are handled differently from
1393 ordinary rules when the same target appears in more than one rule.
1394
1395 When a target appears in multiple rules, all the rules must be the same
1396 type: all ordinary, or all double-colon.  If they are double-colon, each of
1397 them is independent of the others.  Each double-colon rule's commands are
1398 executed if the target is older than any dependencies of that rule.  This
1399 can result in executing none, any or all of the double-colon rules.
1400
1401 The double-colon rules for a target are executed in the order they appear
1402 in the makefile.  However, the cases where double-colon rules really make
1403 sense are those where the order of executing the commands would not matter.
1404
1405 Each double-colon rule should specify commands; if it does not, an
1406 implicit rule will be used if one applies.  @xref{Implicit}.
1407
1408 @node Commands, Variables, Rules, Top
1409 @chapter Writing the Commands in Rules
1410 @cindex commands
1411
1412 The commands of a rule consist of shell command lines to be executed one by
1413 one.  Each command line must start with a tab, except that the first
1414 command line may be attached to the target-and-dependencies line with a
1415 semicolon in between.  Blank lines and lines of just comments may appear
1416 among the command lines; they are ignored.
1417
1418 Users use many different shell programs, but commands in makefiles are
1419 always interpreted by @file{/bin/sh} unless the makefile specifies otherwise.
1420
1421 Whether comments can be written on command lines, and what syntax they use,
1422 is under the control of the shell that is in use.  If it is @file{/bin/sh},
1423 a @samp{#} at the start of a word starts a comment.
1424
1425 @menu
1426 * Echoing::       Normally commands are echoed before execution,
1427                     but you can control this in several ways.
1428 * Execution::     How commands are executed.
1429 * Errors::        What happens after an error in command execution.
1430                    How to ignore errors in certain commands.
1431 * Interrupts::    If a command is interrupted or killed,
1432                    the target may be deleted.
1433 * Recursion::     Invoking @code{make} from commands in makefiles.
1434 * Sequences::     Defining canned sequences of commands.
1435 @end menu
1436
1437 @node Echoing, Execution, Commands, Commands
1438 @section Command Echoing
1439
1440 @cindex echoing (of commands)
1441 @cindex silent operation
1442 @cindex @@ (in commands)
1443 @cindex -n
1444 Normally @code{make} prints each command line before it is executed.  We
1445 call this @dfn{echoing} because it gives the appearance that you are typing
1446 the commands yourself.
1447
1448 When a line starts with @samp{@@}, it is normally not echoed.  The
1449 @samp{@@} is discarded before the command is passed to the shell.  Typically
1450 you would use this for a command whose only effect is to print something,
1451 such as an @code{echo} command.
1452
1453 When @code{make} is given the flag @samp{-n}, echoing is all that happens,
1454 no execution.  @xref{Options}.  In this case and only this case, even the
1455 commands starting with @samp{@@} are printed.  This flag is useful for
1456 finding out which commands @code{make} thinks are necessary without
1457 actually doing them.
1458
1459 @cindex -s
1460 @findex .SILENT
1461 The @samp{-s} flag to @code{make} prevents all echoing, as if all commands
1462 started with @samp{@@}.  A rule in the makefile for the special target
1463 @code{.SILENT} has the same effect (@pxref{Special Targets}).
1464 @code{.SILENT} is essentially obsolete since @samp{@@} is more
1465 general.@refill
1466
1467 @node Execution, Errors, Echoing, Commands
1468 @section Command Execution
1469 @cindex execution
1470 @cindex shell
1471
1472 When it is time to execute commands to update a target, they are executed
1473 one at a time by making a new subshell for each line.  (In practice,
1474 @code{make} may take shortcuts that do not affect the results.)
1475
1476 This implies that shell commands such as @code{cd} that set variables local
1477 to each process will not affect the following command lines.  If you want
1478 to use @code{cd} to affect the next command, put the two on a single line
1479 with a semicolon between them.  Then @code{make} will consider them a
1480 single command and pass them, together, to a shell which will execute them
1481 in sequence.  For example:
1482
1483 @example
1484 foo : bar/lose
1485         cd bar; gobble lose > ../foo
1486 @end example
1487
1488 If you would like to split a single shell command into multiple lines of
1489 text, you must use a backslash at the end of all but the last subline.
1490 Such a sequence of lines is combined into a single line, by deleting the
1491 backslash-newline sequences, before passing it to the shell.  Thus, the
1492 following is equivalent to the preceding example:
1493
1494 @group
1495 @example
1496 foo : bar/lose
1497         cd bar;  \
1498         gobble lose > ../foo
1499 @end example
1500 @end group
1501
1502 @vindex SHELL
1503 The program used as the shell is taken from the variable @code{SHELL}.  By
1504 default, the program @file{/bin/sh} is used.
1505
1506 Unlike most variables, the variable @code{SHELL} will not be set from the
1507 environment, except in a recursive @code{make}.  This is because the
1508 environment variable @code{SHELL} is used to specify your personal choice
1509 of shell program for interactive use.  It would be very bad for personal
1510 choices like this to affect the functioning of makefiles.
1511 @xref{Environment}.
1512
1513 @node Errors, Interrupts, Execution, Commands
1514 @section Errors in Commands
1515
1516 @cindex error (in commands)
1517 After each shell command returns, @code{make} looks at its exit status.
1518 If the command completed successfully, the next command line is executed in
1519 a new shell, or after the last command line the rule is finished.
1520
1521 If there is an error (the exit status is nonzero), @code{make} gives up on
1522 the current rule, and perhaps on all rules.
1523
1524 Sometimes it does not matter whether a command fails.  For example, you
1525 may use the @code{mkdir} command to insure that a directory exists.  If
1526 the directory already exists, @code{mkdir} will report an error, but you
1527 probably want @code{make} to continue regardless.
1528
1529 @cindex - (in commands)
1530 To ignore errors in a command line, write a @samp{-} at the beginning of
1531 the line's text (after the initial tab).  The @samp{-} is discarded before
1532 the command is passed to the shell for execution.
1533
1534 @cindex -i
1535 @findex .IGNORE
1536 When @code{make} is run with the @samp{-i} flag, errors are ignored in
1537 all commands of all rules.  A rule in the makefile for the special target
1538 @code{.IGNORE} has the same effect.  These ways of ignoring errors are
1539 obsolete because @samp{-} is more general.
1540
1541 When errors are to be ignored, because of either a @samp{-} or the
1542 @samp{-i} flag, @code{make} treats an error return just like success.
1543
1544 @cindex -k
1545 When an error happens that @code{make} has not been told to ignore,
1546 it implies that the current target cannot be correctly remade, and neither
1547 can any other that depends on it either directly or indirectly.  No further
1548 commands will be executed for these targets, since their preconditions
1549 have not been achieved.
1550
1551 Normally @code{make} gives up immediately in this circumstance, returning a
1552 nonzero status.  However, if the @samp{-k} flag is specified, @code{make}
1553 continues to consider the other dependencies of the pending targets,
1554 remaking them if necessary, before it gives up and returns nonzero status.
1555 For example, after an error in compiling one object file, @samp{make -k}
1556 will continue compiling other object files even though it already knows
1557 that linking them will be impossible.  @xref{Options}.
1558
1559 The usual behavior assumes that your purpose is to get the specified
1560 targets up to date; once @code{make} learns that this is impossible, it
1561 might as well report the failure immediately.  The @samp{-k} option says
1562 that the real purpose is to test as much as possible of the changes made in
1563 the program, perhaps to find several independent problems so that you can
1564 correct them all before the next attempt to compile.  This is why Emacs's
1565 @code{compile} command passes the @samp{-k} flag by default.
1566
1567 @node Interrupts, Recursion, Errors, Commands
1568 @section Interrupting or Killing @code{make}
1569 @cindex interrupt
1570 @cindex signal
1571 @cindex deletion of target files
1572
1573 If @code{make} gets a fatal signal while a command is executing, it may
1574 delete the target file that the command was supposed to update.  This is
1575 done if the target file's last-modification time has changed since
1576 @code{make} first checked it.
1577
1578 The purpose of deleting the target is to make sure that it is remade from
1579 scratch when @code{make} is next run.  Otherwise, a partially written file
1580 could appear to be valid, since it is more recent than the dependencies.
1581
1582 @findex .PRECIOUS
1583 You can prevent the deletion of a target file in this way by making the
1584 special target @code{.PRECIOUS} depend on it.  Before remaking a target,
1585 @code{make} checks to see whether it appears on the dependencies of
1586 @code{.PRECIOUS}, and thereby decides whether the target should be deleted
1587 if a signal happens.  Some reasons why you might do this are that the
1588 target is updated in some atomic fashion or exists only to record a
1589 modification-time (its contents do not matter) or will cause trouble if it
1590 ever fails to exist.
1591
1592 @node Recursion, Sequences, Interrupts, Commands
1593 @section Recursive Use of @code{make}
1594 @cindex recursion
1595
1596 Recursive use of @code{make} means using @code{make} as a command in a
1597 makefile.  This technique is useful when you want separate makefiles for
1598 various subsystems that compose a larger system.  For example, suppose you
1599 have a subdirectory @file{subdir} which has its own makefile, and you would
1600 like the containing directory's makefile to run @code{make} on the
1601 subdirectory.  You can do it by writing this:
1602
1603 @example
1604 subsystem:
1605         cd subdir; $(MAKE)
1606 @end example
1607
1608 @noindent
1609 or, equivalently, this (@pxref{Options}):
1610
1611 @example
1612 subsystem:
1613         $(MAKE) -c subdir
1614 @end example
1615
1616 You can write recursive @code{make} commands just by copying this example,
1617 but there are many things to know about how they work and why, and about
1618 how the sub-@code{make} relates to the top-level @code{make}.
1619
1620 @menu
1621 * MAKE Variable::        Special effects of using @samp{$(MAKE)}.
1622 * Variables/Recursion::  How variables are communicated to a sub-@code{make}.
1623 * Options/Recursion::    How options are communicated to a sub-@code{make}.
1624 * -w Option::            The @samp{-w} option facilitates debugging
1625                            makefiles with recursive @code{make} commands.
1626 @end menu
1627
1628 @node MAKE Variable, Variables/Recursion, Recursion, Recursion
1629 @subsection How the @code{MAKE} Variable Works
1630 @vindex MAKE
1631
1632 Recursive @code{make} commands should always use the variable @code{MAKE},
1633 not the explicit command name @samp{make}, as shown here:
1634
1635 @example
1636 subsystem:
1637         cd subdir; $(MAKE)
1638 @end example
1639
1640 The value of this variable is the file name with which @code{make} was
1641 invoked.  If this file name was @file{/bin/make}, then the command executed
1642 is @samp{cd subdir; /bin/make}.  If you use a special version of
1643 @code{make} to run the top-level makefile, the same special version will be
1644 executed for recursive invocations.
1645
1646 Also, any arguments that define variable values are added to @code{MAKE},
1647 so the sub-@code{make} gets them too.  Thus, if you do @samp{make
1648 CFLAGS=-O}, so that all C-compilations will be optimized, the
1649 sub-@code{make} is run with @samp{cd subdir; /bin/make CFLAGS=-O}.@refill
1650
1651 As a special feature, using the variable @code{MAKE} in the commands of a
1652 rule alters the effects of the @samp{-t}, @samp{-n} or @samp{-q} option.
1653 (@xref{Instead of Execution}.)@refill
1654
1655 Consider the command @samp{make -t} in the above example.  Following the
1656 usual definition of @samp{-t}, this would create a file named
1657 @file{subsystem} and do nothing else.  What you really want it to do is run
1658 @samp{cd subdir; make -t}; but that would require executing the command,
1659 and @samp{-t} says not to execute commands.@refill
1660
1661 The special feature makes this do what you want: whenever a rule's commands
1662 use the variable @code{MAKE}, the flags @samp{-t}, @samp{-n} or @samp{-q}
1663 do not apply to that rule.  The commands of that rule are executed normally
1664 despite the presence of a flag that causes most commands not to be run.
1665 The usual @code{MAKEFLAGS} mechanism passes the flags to the
1666 sub-@code{make} (@pxref{Options/Recursion}), so your request to touch the
1667 files, or print the commands, is propagated to the subsystem.@refill
1668
1669 @node Variables/Recursion, Options/Recursion, MAKE Variable, Recursion
1670 @subsection Communicating Variables to a Sub-@code{make}
1671 @cindex environment and recursion
1672
1673 Most variable values of the top-level @code{make} are passed to the
1674 sub-@code{make} through the environment.  These variables are defined in
1675 the sub-@code{make} as defaults, but do not override what is specified in
1676 the sub-@code{make}'s makefile.  (Variables are passed down if their names
1677 consist of letters, numbers and underscores.)
1678
1679 The way this works is that @code{make} adds each variable and its value to
1680 the environment for running each command.  (Variables whose names start
1681 with non-alphanumeric characters are left out.)  The sub-@code{make}, in
1682 turn, uses the environment to initialize its table of variable values.
1683 @xref{Environment}.
1684
1685 @vindex MAKELEVEL
1686 As a special feature, the variable @code{MAKELEVEL} is changed when it is
1687 passed down from level to level.  This variable's value is a string which
1688 is the depth of the level as a decimal number.  The value is @samp{0} for
1689 the top-level @code{make}; @samp{1} for a sub-@code{make}, @samp{2} for a
1690 sub-sub-@code{make}, and so on.  The incrementation happens when
1691 @code{make} sets up the environment for a command.@refill
1692
1693 The main use of @code{MAKELEVEL} is to test it in a conditional directive
1694 (@pxref{Conditionals}); this way you can write a makefile that behaves one
1695 way if run recursively and another way if run directly by you.
1696
1697 @vindex MAKEFILES
1698 You can use the variable @code{MAKEFILES} to cause all sub-@code{make}
1699 commands to use additional makefiles.  The value of @code{MAKEFILES} is a
1700 whitespace-separated list of filenames.  This variable, if defined in the
1701 outer-level makefile, is passed down through the environment as usual; then
1702 it serves as a list of extra makefiles for the sub-@code{make} to read
1703 before the usual or specified ones.  @xref{MAKEFILES Variable}.
1704
1705 @node Options/Recursion, -w Option, Variables/Recursion, Recursion
1706 @subsection Communicating Options to a Sub-@code{make}
1707 @cindex options and recursion
1708
1709 @vindex MAKEFLAGS
1710 Flags such as @samp{-s} and @samp{-k} are passed automatically to the
1711 sub-@code{make} through the variable @code{MAKEFLAGS}.  This variable is
1712 set up automatically by @code{make} to contain the flag letters that
1713 @code{make} received.  Thus, if you do @samp{make -ks} then
1714 @code{MAKEFLAGS} gets the value @samp{ks}.@refill
1715
1716 As a consequence, every sub-@code{make} gets a value for @code{MAKEFLAGS}
1717 in its environment.  In response, it takes the flags from that value and
1718 processes them as if they had been given as arguments.  @xref{Options}.
1719
1720 The options @samp{-c}, @samp{-d}, @samp{-f}, @samp{-I}, @samp{-o}, and
1721 @samp{-p} are not put into @code{MAKEFLAGS}; these options are not passed
1722 down.@refill
1723
1724 If you don't want to pass the other the flags down, you must change the
1725 value of @code{MAKEFLAGS}, like this:
1726
1727 @example
1728 subsystem:
1729         cd subdir; $(MAKE) MAKEFLAGS=
1730 @end example
1731
1732 @vindex MFLAGS
1733 A similar variable @code{MFLAGS} exists also, for historical compatibility.
1734 It has the same value as @code{MAKEFLAGS} except that a hyphen is added at
1735 the beginning if it is not empty.  @code{MFLAGS} was traditionally used
1736 explicitly in the recursive @code{make} command, like this:
1737
1738 @example
1739 subsystem:
1740         cd subdir; $(MAKE) $(MFLAGS)
1741 @end example
1742
1743 @noindent
1744 but now @code{MAKEFLAGS} makes this usage redundant.
1745
1746 @node -w Option,, Options/Recursion, Recursion
1747 @subsection The @samp{-w} Option
1748
1749 If you are running @code{make} over a large directory tree, the @samp{-w}
1750 option can make the output a lot easier to understand by showing each
1751 directory as it is entered and exited.  For example, if @samp{make -w} is
1752 run in the directory @file{/u/gnu/make}, @code{make} will print a line of
1753 the form:@refill
1754
1755 @example
1756 make: Entering directory `/u/gnu/make'.
1757 @end example
1758
1759 @noindent
1760 before doing anything else, and a line of this form:
1761
1762 @example
1763 make: Leaving directory `/u/gnu/make'.
1764 @end example
1765
1766 @noindent
1767 when processing is completed.
1768
1769 @node Sequences,, Recursion, Commands
1770 @section Defining Canned Command Sequences
1771 @cindex sequences of commands
1772
1773 When the same sequence of commands is useful in making various targets, you
1774 can define it as a canned sequence with the @code{define} directive, and
1775 refer to the canned sequence from the rules for those targets.  The canned
1776 sequence is actually a variable, so the name must not conflict with other
1777 variable names.
1778
1779 Here is an example of defining a canned sequence of commands:
1780
1781 @example
1782 define run-yacc
1783 yacc $(firstword $^)
1784 mv y.tab.c $@@
1785 endef
1786 @end example
1787
1788 @noindent
1789 Here @code{run-yacc} is the name of the variable being defined;
1790 @code{endef} marks the end of the definition; the lines in between are the
1791 commands.  The @code{define} directive does not expand variable references
1792 and function calls in the canned sequence; the @samp{$} characters,
1793 parentheses, variable names, and so on, all become part of the value of the
1794 variable you are defining.  @xref{Defining}, for a complete explanation of
1795 @code{define}.
1796
1797 The first command in this example runs Yacc on the first dependency (of
1798 whichever rule uses the canned sequence).  The output file from Yacc is
1799 always named @file{y.tab.c}.  The second command moves the output to the
1800 rule's target file name.
1801
1802 To use the canned sequence, substitute the variable into the commands of a
1803 rule.  You can substitute it like any other variable (@pxref{Reference}).
1804 Because variables defined by @code{define} are recursively expanded
1805 variables, all the variable references you wrote inside the @code{define}
1806 are expanded now.  For example:
1807
1808 @example
1809 foo.c : foo.y
1810         $(run-yacc)
1811 @end example
1812
1813 @noindent
1814 @samp{foo.y} will substituted for the variable @samp{$^} when it occurs in
1815 @code{run-yacc}'s value, and @samp{foo.c} for @samp{$@@}.@refill
1816
1817 This is a realistic example, but this particular one is not needed in
1818 practice because @code{make} has an implicit rule to figure out these
1819 commands based on the file names involved.  @xref{Implicit}.
1820
1821 @node Variables, Conditionals, Commands, Top
1822 @chapter How to Use Variables
1823 @cindex variable
1824 @cindex value
1825 @cindex recursive variable expansion
1826 @cindex simple variable expansion
1827
1828 A @dfn{variable} is a name defined within @code{make} to represent a string
1829 of text, called the variable's @dfn{value}.  These values can be
1830 substituted by explicit request into targets, dependencies, commands and
1831 other parts of the makefile.
1832
1833 Variables can represent lists of file names, options to pass to compilers,
1834 programs to run, directories to look in for source files, directories to
1835 write output in, or anything else you can imagine.
1836
1837 A variable name may be any sequence characters not containing @samp{:},
1838 @samp{#}, @samp{=}, leading or trailing whitespace.  However,
1839 variable names containing characters other than letters, numbers and
1840 underscores should be avoided, as they may be given special meanings in the
1841 future, and they are not passed through the environment to a
1842 sub-@code{make} (@pxref{Variables/Recursion}).
1843
1844 It is traditional to use upper case letters in variable names, but we
1845 recommend using lower case letters for variable names that serve internal
1846 purposes in the makefile, and reserving upper case for parameters that
1847 control implicit rules or for parameters that the user should override with
1848 command options (@pxref{Overriding}).
1849
1850 @menu
1851 * Reference::   How to use the value of a variable.
1852 * Values::      All the ways variables get their values.
1853 * Flavors::     Variables come in two flavors.
1854 * Setting::     How to set a variable in the makefile.
1855 * Override Directive:: Setting a variable in the makefile
1856                  even if the user has set it with a command argument.
1857 * Defining::    An alternate way to set a variable to a verbatim string.
1858 * Environment:: Variable values can come from the environment.
1859 @end menu
1860
1861 @node Reference, Values, Variables, Variables
1862 @section Reference to Variables
1863 @cindex reference to variables
1864 @cindex $ (variable reference)
1865
1866 To substitute a variable's value, write a dollar sign followed by the name
1867 of the variable in parentheses or braces: either @samp{$(foo)} or
1868 @samp{$@{foo@}} is a valid reference to the variable @code{foo}.  This
1869 special significance of @samp{$} is why you must write @samp{$$} to have
1870 the effect of a single dollar sign in a file name or command.
1871
1872 Variable references can be used in any context: targets, dependencies,
1873 commands, most directives, and new variable values.  Here is a common kind
1874 of example, where a variable holds the names of all the object files in a
1875 program:
1876
1877 @example
1878 objects = program.o foo.o utils.o
1879 program : $(objects)
1880         cc -o program $(objects)
1881
1882 $(objects) : defs.h
1883 @end example
1884
1885 Variable references work by strict textual substitution.  Thus, the rule
1886
1887 @example
1888 foo = c
1889 prog.o : prog.c
1890         $(foo)$(foo) prog.c
1891 @end example
1892
1893 @noindent
1894 could be used to compile a C program @file{prog.c}.  (Since spaces around
1895 the variable value are ignored in variable assignments, the value of
1896 @code{foo} is precisely @samp{c}.)
1897
1898 A dollar sign followed by a character other than a dollar sign,
1899 open-parenthesis or open-brace treats that single character as the variable
1900 name.  Thus, you could reference the variable @code{x} with @samp{$x}.
1901 However, this practice is strongly discouraged, except with the automatic
1902 variables (@pxref{Automatic}).
1903
1904
1905 @subsection Modified References
1906 @cindex modified variable reference
1907 @cindex substitution variable reference
1908
1909 In addition to simple references, variables can be referenced in a manner
1910 which modifies the value of the reference but do not modify the value of
1911 the variable referenced.  Such a reference is a @dfn{substitution reference}.
1912
1913 A @dfn{substitution reference} is really a simplified form of the
1914 @code{patsubst} expansion function (@pxref{Functions}).  It has the form
1915 @samp{$(var:a=b)} (or @samp{$@{var:a=b@}}) and is equivalent to
1916 @samp{$(patsubst %a,%b,$(var))}.  This means that it replaces every
1917 @samp{a} at the end of a whitespace-separated word with a @samp{b}.
1918 For example:@refill
1919
1920 @example
1921 foo := a.o b.o c.o
1922 bar := $(foo:.o=.c)
1923 @end example
1924
1925 @noindent
1926 sets @samp{bar} to @samp{a.c b.c c.c}.  @xref{Setting}.
1927
1928 @subsection Recursive References
1929 @cindex recursive variable reference
1930
1931 Variables may be referenced inside a variable reference.  This is called
1932 a @dfn{recursive variable reference}.  For example,
1933
1934 @example
1935 x = y
1936 y = z
1937 a := $($(x))
1938 @end example
1939
1940 @noindent
1941 defines @samp{a} as @samp{z}: the @samp{$(x)} inside @samp{$($(x))} expands
1942 to @samp{y}, so @samp{$($(x))} expands to @samp{$(y)} which in turn expands
1943 to @samp{z}.@refill
1944
1945 Recursive variable references can get yet more recursive.  For example,
1946
1947 @example
1948 x = $(y)
1949 y = z
1950 z = Hello
1951 a := $($(x))
1952 @end example
1953
1954 @noindent
1955 defines @samp{a} as @samp{Hello}: @samp{$($(x))} becomes @samp{$($(y))}
1956 which becomes @samp{$(z)} which becomes @samp{Hello}.  This sort of
1957 recursion can go on for as many levels as you like (and can comprehend),
1958 though it's not clear that very many levels of recursion are useful.@refill
1959
1960 Recursive variable references can also contain modified references and
1961 function invokations (@pxref{Functions}), just like any other reference.
1962 For example, using the @code{subst} function (@pxref{Text Functions}):
1963
1964 @example
1965 x = variable1
1966 variable2 := Hello
1967 y = $(subst 1,2,$(x))
1968 z = y
1969 a := $($($(z)))
1970 @end example
1971
1972 @noindent
1973 eventually defines @samp{a} as @samp{Hello}.  It is doubtful that anyone
1974 would ever want to write a recursive reference as convoluted as this one,
1975 but it works: @samp{$($($(z)))} expands to @samp{$($(y))} which becomes
1976 @samp{$($(subst 1,2,$(x)))} which changes @samp{variable1} to
1977 @samp{variable2} in @samp{$(x)} and finally expands to @samp{$(variable2)},
1978 a simple variable reference that becomes @samp{Hello}.@refill
1979
1980 Recursive variable references are a complicated concept needed only for
1981 very complex makefile programming.  You need not worry about them in
1982 general, except to know that making a variable with a dollar sign in its
1983 name might have strange results.  Note also that @dfn{recursive variable
1984 references} are quite different from @dfn{recursive variables}
1985 (@pxref{Flavors}), though both are used together in complex ways when
1986 doing makefile programming.@refill
1987
1988 @node Values, Flavors, Reference, Variables
1989 @section How Variables Get Their Values
1990
1991 Variables can get values in several different ways:
1992
1993 @itemize @bullet
1994 @item
1995 You can specify an overriding value when you run @code{make}.
1996 @xref{Overriding}.
1997
1998 @item
1999 You can specify a value in the makefile, either with an assignment
2000 (@pxref{Setting}) or with a verbatim definition (@pxref{Defining}).
2001
2002 @item
2003 Values are inherited from the environment.  @xref{Environment}.
2004
2005 @item
2006 Several @dfn{automatic} variables are given new values for each rule.
2007 @xref{Automatic}.
2008
2009 @item
2010 Several variables have constant initial values.  @xref{Implicit Variables}.
2011 @end itemize
2012
2013 @node Flavors, Setting, Values, Variables
2014 @section The Two Flavors of Variables
2015 @cindex flavors (of variables)
2016 @cindex recursive variable expansion
2017
2018 There are two kinds of variables in GNU @code{make}.  They are
2019 distinguished by two things: how they are defined and how they are expanded.
2020
2021 The first flavor of variable is a @dfn{recursively expanded} variable.
2022 Variables of this sort are defined by lines using @samp{=}
2023 @ifinfo
2024 (@pxref{Setting}).
2025 @end ifinfo
2026 @iftex
2027 (see the next section).
2028 @end iftex
2029 The value you specify is installed verbatim; if it contains references to
2030 other variables, these references are expanded whenever this variable is
2031 substituted (in the course of expanding some other string).  When this
2032 happens, it is recursive expansion.
2033
2034 For example,
2035
2036 @example
2037 foo = $(bar)
2038 bar = $(ugh)
2039 ugh = Huh?
2040
2041 all:;echo $(foo)
2042 @end example
2043
2044 @noindent
2045 will echo @samp{Huh?}: @samp{$(foo)} expands to @samp{$(bar)} which
2046 expands to @samp{$(ugh)} which finally expands to @samp{Huh?}.@refill
2047
2048 This flavor of variable is the only sort supported by other versions of
2049 @code{make}.  It has its advantages and its disadvantages.  An advantage
2050 (most would say) is that:
2051
2052 @example
2053 CFLAGS = $(include_dirs) -O
2054 include_dirs = -Ifoo -Ibar
2055 @end example
2056
2057 @noindent
2058 will do what was intended: when @samp{CFLAGS} is expanded in a command,
2059 it will expand to @samp{-Ifoo -Ibar}.  A major disadvantage is that you
2060 can't append something on the end of a variable, as in
2061
2062 @example
2063 CFLAGS = $(CFLAGS) -O
2064 @end example
2065
2066 @noindent
2067 because it will cause an infinite loop in the variable expansion.
2068 (Actually @code{make} detects the infinite loop and reports an error.)
2069
2070 Another disadvantage is that any functions (@pxref{Functions}) referenced
2071 in the definition will be executed every time the variable is expanded.
2072 This makes @code{make} run slower; worse, it causes the @code{wildcard}
2073 function to give unpredictable results.
2074
2075 To avoid all the problems and inconveniences of recursively expanded
2076 variables, there is another flavor: @dfn{simply expanded} variables.
2077 Simply expanded variables are defined by lines using @samp{:=}
2078 @ifinfo
2079 (@pxref{Setting}).
2080 @end ifinfo
2081 @iftex
2082 (see the next section).
2083 @end iftex
2084 The value of a simply expanded variable is scanned once and for all,
2085 expanding any references to other variables and functions, when the
2086 variable is defined.  The actual value of the simply expanded variable is
2087 the result of expanding the value you write.  It does not contain any
2088 references to other variables; it contains their values @emph{as of the
2089 time this variable was defined}.  Therefore,
2090
2091 @example
2092 x := foo
2093 y := $(x) bar
2094 x := later
2095 @end example
2096
2097 @noindent
2098 is equivalent to
2099
2100 @example
2101 y := foo bar
2102 x := later
2103 @end example
2104
2105 When a simply expanded variable is referenced, its value is substituted
2106 verbatim.
2107
2108 Simply expanded variables generally make complicated makefile programming
2109 more predictable because they work like variables in most programming
2110 languages.  They allow you to redefine a variable using its own value (or
2111 its value processed in some way by one of the expansion functions) and to
2112 use the expansion functions much more efficiently (@pxref{Functions}).
2113
2114 You can also use them to introduce controlled leading or trailing spaces
2115 into variable values.  Such spaces are discarded from your input before
2116 substitution of variable references and function calls; this means you can
2117 include leading or trailing spaces in a variable value by protecting them
2118 with variable references, like this:
2119
2120 @example
2121 nullstring :=
2122 space := $(nullstring) $(nullstring)
2123 @end example
2124
2125 @noindent
2126 Here the value of the variable @code{space} is precisely one space.
2127
2128 @node Setting, Override Directive, Flavors, Variables
2129 @section Setting Variables
2130 @cindex setting variables
2131 @cindex =
2132 @cindex :=
2133
2134 To set a variable from the makefile, write a line starting with the
2135 variable name followed by @samp{=} or @samp{:=}.  Whatever follows the
2136 @samp{=} or @samp{:=} on the line becomes the value.  For example,
2137
2138 @example
2139 objects = main.o foo.o bar.o utils.o
2140 @end example
2141
2142 @noindent
2143 defines a variable named @code{objects}.  Spaces around the variable name
2144 are ignored, and so are spaces after the @samp{=} or at the end of the
2145 line.
2146
2147 Variables defined with @samp{=} are @dfn{recursively expanded} variables.
2148 Variables defined with @samp{:=} are @dfn{simply expanded} variables; these
2149 definitions can contain variable references which will be expanded before
2150 the definition is made.  @xref{Flavors}. 
2151
2152 There is no limit on the length of the value of a variable except the
2153 amount of swapping space on the computer.  When a variable definition is
2154 long, it is a good idea to break it into several lines by inserting
2155 backslash-newline at convenient places in the definition.  This will not
2156 affect the functioning of @code{make}, but it will make the makefile easier
2157 to read.
2158
2159 Most variable names are considered to have the empty string as a value if
2160 you have never set them.  Several variables have built-in initial values
2161 that are not empty, but can be set by you in the usual ways
2162 (@pxref{Implicit Variables}).  Several special variables are set
2163 automatically to a new value for each rule; these are called the
2164 @dfn{automatic} variables (@pxref{Automatic}).
2165
2166 @node Override Directive, Defining, Setting, Variables
2167 @section The @code{override} Directive
2168 @findex override
2169
2170 If a variable has been set with a command argument (@pxref{Overriding}),
2171 then ordinary assignments in the makefile are ignored.  If you want to set
2172 the variable in the makefile even though it was set with a command
2173 argument, you can use an @code{override} directive, which is a line that
2174 looks like this:
2175
2176 @example
2177 override @var{variable} = @var{value}
2178 @end example
2179
2180 or
2181
2182 @example
2183 override @var{variable} := @var{value}
2184 @end example
2185
2186 The @code{override} directive was not invented for escalation in the war
2187 between makefiles and command arguments.  It was invented so you can alter
2188 and add to values that the user specifies with command arguments.
2189
2190 For example, suppose you always want the @samp{-g} switch when you run the
2191 C compiler, but you would like to allow the user to specify the other
2192 switches with a command argument just as usual.  You could use this
2193 @code{override} directive:
2194
2195 @example
2196 override CFLAGS := $(CFLAGS) -g
2197 @end example
2198
2199 @node Defining, Environment, Override Directive, Variables
2200 @section Defining Variables Verbatim
2201 @findex define
2202 @findex endef
2203
2204 Another way to set the value of a variable is to use the @code{define}
2205 directive.  This directive has a different syntax which allows newline
2206 characters to be included in the value, which is convenient for defining
2207 canned sequences of commands (@pxref{Sequences}).
2208
2209 The @code{define} directive is followed on the same line the name of the
2210 variable and nothing more.  The value to give the variable appears on the
2211 following lines.  The end of the value is marked by a line containing just
2212 the word @code{endef}.  Aside from this difference in syntax, @code{define}
2213 works just like @samp{=}; it creates a recursively-expanded variable
2214 (@pxref{Flavors}).
2215
2216 @example
2217 define two-lines
2218 echo foo
2219 echo $(bar)
2220 endef
2221 @end example
2222
2223 The value in an ordinary assignment cannot contain a newline; but the
2224 newlines that separate the lines of the value in a @code{define} become
2225 part of the variable's value (except for the final newline which precedes
2226 the @code{endef} and is not considered part of the value).@refill
2227
2228 The previous example is functionally equivalent to this:
2229
2230 @example
2231 two-lines = echo foo; echo $(bar)
2232 @end example
2233
2234 @noindent
2235 since the shell will interpret the semicolon and the newline identically.
2236
2237 @node Environment,, Defining, Variables
2238 @section Variables from the Environment
2239
2240 @cindex environment
2241 Variables in @code{make} can come from the environment with which
2242 @code{make} is run.  Every environment variable that @code{make} sees when
2243 it starts up is transformed into a @code{make} variable with the same name
2244 and value.  But an explicit assignment in the makefile, or with a command
2245 argument, overrides the environment.  (If the @samp{-e} flag is specified,
2246 then values from the environment override assignments in the makefile.
2247 @xref{Options}.  But this is not recommended practice.)
2248
2249 By setting the variable @code{CFLAGS} in your environment, you can cause
2250 all C compilations in most makefiles to use the compiler switches you
2251 prefer.  This is safe for variables with standard or conventional meanings
2252 because you know that no makefile will use them for other things.  (But
2253 this is not totally reliable; some makefiles set @code{CFLAGS} explicitly
2254 and therefore are not affected by the value in the environment.)
2255
2256 When @code{make} is invoked recursively, variables defined in the outer
2257 invocation are automatically passed to inner invocations through the
2258 environment (@pxref{Recursion}).  This is the main purpose of turning
2259 environment variables into @code{make} variables, and it requires no
2260 attention from you.
2261
2262 Other use of variables from the environment is not recommended.  It is not
2263 wise for makefiles to depend for their functioning on environment variables
2264 set up outside their control, since this would cause different users to get
2265 different results from the same makefile.  This is against the whole
2266 purpose of most makefiles.
2267
2268 Such problems would be especially likely with the variable @code{SHELL},
2269 which is normally present in the environment to specify the user's choice
2270 of interactive shell.  It would be very undesirable for this choice to
2271 affect @code{make}.  So @code{make} ignores the environment value of
2272 @code{SHELL} if the value of @code{MAKELEVEL} is zero (which is normally
2273 true except in recursive invocations of @code{make}).@refill
2274
2275 @node Conditionals, Functions, Variables, Top
2276 @chapter Conditional Parts of Makefiles
2277
2278 @cindex conditionals
2279 A @dfn{conditional} causes part of a makefile to be obeyed or ignored
2280 depending on the values of variables.  Conditionals can compare the value
2281 of one variable with another, or the value of a variable with a constant
2282 string.  Conditionals control what @code{make} actually ``sees'' in the
2283 makefile, so they @emph{cannot} be used to control shell commands at the
2284 time of execution.@refill
2285
2286 @menu
2287 * Example: Conditional Example.   An annotated example.
2288 * Syntax: Conditional Syntax.     Precise rules for syntax of conditionals.
2289 * Flags: Testing Flags.           Conditionals testing flags such as @samp{-t}.
2290 @end menu
2291
2292 @node Conditional Example, Conditional Syntax, Conditionals, Conditionals
2293 @section Example of a Conditional
2294
2295 This conditional tells @code{make} to use one set of libraries if the
2296 @code{CC} variable is @samp{gcc}, and a different set of libraries
2297 otherwise.  It works by controlling which of two command lines will be used
2298 as the command for a rule.  The result is that @samp{CC=gcc} as an argument
2299 to @code{make} not only changes which compiler is used but also which
2300 libraries are linked.
2301
2302 @example
2303 libs_for_gcc = -lgnu
2304 normal_libs =
2305
2306 foo: $(objects)
2307 ifeq ($(CC),gcc)
2308         $(CC) -o foo $(objects) $(libs_for_gcc)
2309 else
2310         $(CC) -o foo $(objects) $(normal_libs)
2311 endif
2312 @end example
2313
2314 @noindent
2315 This conditional uses three directives: one @code{ifeq}, one @code{else}
2316 and one @code{endif}.
2317
2318 The @code{ifeq} directive contains two arguments, separated by a comma and
2319 surrounded by parentheses.  Variable substitution is performed on both
2320 arguments and then they are compared.  The lines of the makefile following
2321 the @code{ifeq} are obeyed if the two arguments match; otherwise they are
2322 ignored.
2323
2324 The @code{else} directive causes the following lines to be obeyed if the
2325 previous conditional failed.  In the example above, this means that the
2326 second alternative linking command is used whenever the first alternative
2327 is not used.  It is optional to have an @code{else} in a conditional.
2328
2329 The @code{endif} directive ends the conditional.  Every conditional must
2330 end with an @code{endif}.  Unconditional makefile text follows.
2331
2332 When the variable @code{CC} has the value @samp{gcc}, the above example has
2333 this effect:
2334
2335 @example
2336 foo: $(objects)
2337         $(CC) -o foo $(objects) $(libs_for_gcc)
2338 @end example
2339
2340 @noindent
2341 When the variable @code{CC} has any other value, this effect is this:
2342
2343 @example
2344 foo: $(objects)
2345         $(CC) -o foo $(objects) $(normal_libs)
2346 @end example
2347
2348 Equivalent results can be obtained in another way by conditionalizing a
2349 variable assignment and then using the variable unconditionally:
2350
2351 @example
2352 libs_for_gcc = -lgnu
2353 normal_libs =
2354
2355 ifeq ($(CC),gcc)
2356   libs=$(libs_for_gcc)
2357 else
2358   libs=$(normal_libs)
2359 endif
2360
2361 foo: $(objects)
2362         $(CC) -o foo $(objects) $(libs)
2363 @end example
2364
2365 @node Conditional Syntax, Testing Flags, Conditional Example, Conditionals
2366 @section Syntax of Conditionals
2367 @findex ifdef
2368 @findex ifeq
2369 @findex else
2370 @findex endif
2371
2372 The syntax of a simple conditional with no @code{else} is as follows:
2373
2374 @example
2375 @var{conditional-directive}
2376 @var{text-if-true}
2377 endif
2378 @end example
2379
2380 @noindent
2381 The @var{text-if-true} may be any lines of text, to be considered as part
2382 of the makefile if the condition is true.  If the condition is false, no
2383 text is used instead.
2384
2385 The syntax of a complex conditional is as follows:
2386
2387 @example
2388 @var{conditional-directive}
2389 @var{text-if-true}
2390 else
2391 @var{text-if-false}
2392 endif
2393 @end example
2394
2395 @noindent
2396 If the condition is true, @var{text-if-true} is used; otherwise,
2397 @var{text-if-false} is used instead.  The @var{text-if-false} can be any
2398 number of lines of text.
2399
2400 Conditionals work at the textual level.  The lines of the
2401 @var{text-if-true} are read as part of the makefile if the condition is
2402 true; if the condition is false, those lines are ignored completely.  It
2403 follows that syntactic units of the makefile, such as rules, may safely be
2404 split across the beginning or the end of the conditional.@refill
2405
2406 You may use an @code{include} directive within a conditional, but you may
2407 not start a conditional in one file and end it in another.
2408
2409 The syntax of the @var{conditional-directive} is the same whether the
2410 conditional is simple or complex.  There are four different directives that
2411 test different conditions.  Here is a table of them:
2412
2413 @table @code
2414 @item ifeq (@var{arg1}, @var{arg2})
2415 Expand all variable references in @var{arg1} and @var{arg2} and
2416 compare them.  If they are identical, the @var{text-if-true} is
2417 effective; otherwise, the @var{text-if-false}, if any, is effective.
2418
2419 @item ifneq (@var{arg1}, @var{arg2})
2420 Expand all variable references in @var{arg1} and @var{arg2} and
2421 compare them.  If they are different, the @var{text-if-true} is
2422 effective; otherwise, the @var{text-if-false}, if any, is effective.
2423
2424 @item ifdef @var{variable-name}
2425 If the variable @var{variable-name} has a non-empty value, the
2426 @var{text-if-true} is effective; otherwise, the @var{text-if-false},
2427 if any, is effective.  Variables that have never been defined have an
2428 empty value.
2429
2430 @item ifndef @var{variable-name}
2431 If the variable @var{variable-name} has an empty value, the
2432 @var{text-if-true} is effective; otherwise, the @var{text-if-false},
2433 if any, is effective.
2434 @end table
2435
2436 Extra spaces are allowed and ignored at the beginning of the conditional
2437 directive line, but a tab is not allowed.  (If the line begins with a tab,
2438 it will be considered a command for a rule.)  Aside from this, extra spaces
2439 or tabs may be inserted with no effect anywhere except within the directive
2440 name or within an argument.  A comment starting with @samp{#} may appear at
2441 the end of the line.
2442
2443 The other two directives that play a part in a conditional are @code{else}
2444 and @code{endif}.  Each of these directives is written as one word, with no
2445 arguments.  Extra spaces are allowed and ignored at the beginning of the
2446 line, and spaces or tabs at the end.  A comment starting with @samp{#} may
2447 appear at the end of the line.
2448
2449 @node Testing Flags,, Conditional Syntax, Conditionals
2450 @section Conditionals that Test Flags
2451
2452 You can write a conditional that tests @code{make} command flags such as
2453 @samp{-t} by using the variable @code{MAKEFLAGS} together with the
2454 @code{findstring} function.  This is useful when @code{touch} is not
2455 enough to make a file appear up to date.
2456
2457 The @code{findstring} function determines whether one string appears as a
2458 substring of another.  If you want to test for the @samp{-t} flag,
2459 use @samp{t} as the first string and the value of @code{MAKEFLAGS} as
2460 the other.
2461
2462 For example, here is how to arrange to use @samp{ranlib -t} to finish
2463 marking an archive file up to date:
2464
2465 @example
2466 archive.a: @dots{}
2467 ifneq (,$(findstring t,$(MAKEFLAGS)))
2468         @@echo $(MAKE) > /dev/null
2469         touch archive.a
2470         ranlib -t archive.a
2471 else
2472         ranlib archive.a
2473 endif
2474 @end example
2475
2476 @noindent
2477 The @code{echo} command does nothing when executed; but its presence, with
2478 a reference to the variable @code{MAKE}, marks the rule as ``recursive'' so
2479 that its commands will be executed despite use of the @samp{-t} flag.
2480 @xref{Recursion}.
2481
2482 @node Functions, Running, Conditionals, Top
2483 @chapter Functions for Transforming Text
2484 @cindex function
2485
2486 @dfn{Functions} allow you to do text processing in the makefile to compute
2487 the files to operate on or the commands to use.  You use a function in a
2488 @dfn{function call}, where you give the name of the function and some text
2489 (the @dfn{arguments}) for the function to operate on.  The result of the
2490 function's processing is substituted into the makefile at the point of the
2491 call, just as a variable might be substituted.
2492
2493 @menu
2494 * Syntax: Function Syntax.  Syntax of function calls in general.
2495 * Text Functions::          Text manipulation functions.
2496 * Foreach Function::        The @code{foreach} function.
2497 * Filename Functions::      Functions for manipulating file names.
2498 @end menu
2499
2500 @node Function Syntax, Text Functions, Functions, Functions
2501 @section Function Call Syntax
2502 @cindex $ (function call)
2503 @cindex arguments
2504
2505 A function call resembles a variable reference.  It looks like this:
2506
2507 @example
2508 $(@var{function} @var{arguments})
2509 @end example
2510
2511 @noindent
2512 or like this:
2513
2514 @example
2515 $@{@var{function} @var{arguments}@}
2516 @end example
2517
2518 Here @var{function} is a function name; one of a short list of names that
2519 are part of @code{make}.  There is no provision for defining new functions.
2520
2521 The @var{arguments} are the arguments of the function.  They are separated
2522 from the function name by one or more spaces and/or tabs, and if there is
2523 more than one argument they are separated by commas.  Such whitespace and
2524 commas are not part of any argument's value.  Parentheses or braces,
2525 whichever you use to surround the function call, can appear in an argument
2526 only in matching pairs; the ones that were not used to surround the
2527 function call can appear freely.  If the arguments contain other function
2528 calls or variable references, it is wisest to surround them with the same
2529 delimiters used for the containing function call.
2530
2531 The text written for each argument is processed by substitution of
2532 variables and function calls in order to produce the argument value, which
2533 is the text on which the function acts.
2534
2535 Commas and unmatched parentheses or braces cannot appear in the text of an
2536 argument as written; leading spaces cannot appear in the text of the first
2537 argument as written.  These characters can be put into the argument value
2538 by variable substitution.  First define variables @code{comma} and
2539 @code{space} whose values are isolated comma and space characters, then
2540 substitute those variables where such characters are wanted, like this:
2541
2542 @example
2543 comma:= ,
2544 space:= $(empty) $(empty)
2545 foo:= a b c
2546 bar:= $(subst $(space),$(comma),$(foo))
2547 # @r{bar is now `a,b,c'.}
2548 @end example
2549
2550 @noindent
2551 Here the @code{subst} function replaces each space with a comma, through
2552 the value of @code{foo}, and substitutes the result.
2553
2554 @node Text Functions, Foreach Function, Function Syntax, Functions
2555 @section Functions for String Substitution and Analysis
2556
2557 Here are some functions that operate on substrings of a string:
2558
2559 @table @code
2560 @item $(subst @var{from},@var{to},@var{text})
2561 @findex subst
2562 Performs a textual replacement on the text @var{text}: each occurrence
2563 of @var{from} is replaced by @var{to}.  The result is substituted for
2564 the function call.  For example,
2565
2566 @example
2567 $(subst ee,EE,feet on the street)
2568 @end example
2569
2570 substitutes the string @samp{fEEt on the strEEt}.
2571
2572 @item $(patsubst @var{pattern},@var{replacement},@var{text})
2573 @findex patsubst
2574 Finds whitespace-separated words in @var{text} that match
2575 @var{pattern} and replaces them with @var{replacement}.  Here
2576 @var{pattern} may contain a @samp{%} which acts as a wildcard,
2577 matching any number of any characters within a word.  If
2578 @var{replacement} also contains a @samp{%}, the @samp{%} is replaced
2579 by the text that matched the @samp{%} in @var{pattern}.
2580
2581 Whitespace between words is folded into single space characters;
2582 leading and trailing whitespace is discarded.
2583
2584 @item $(strip @var{string})
2585 @findex strip
2586 Removes leading and trailing whitespace from @var{string} and replaces
2587 each internal sequence of one or more whitespace characters with a
2588 single space.
2589
2590 @item $(findstring @var{find},@var{in})
2591 @findex findstring
2592 Searches @var{in} for an occurrence of @var{find}.  If it occurs, the
2593 value is @var{find}; otherwise, the value is empty.  You can use this
2594 function in a conditional to test for the presence of a specific
2595 substring in a given string.  @xref{Testing Flags}, for a practical
2596 application of @code{findstring}.
2597
2598 @item $(filter @var{pattern},@var{text})
2599 @findex filter
2600 Removes all whitespace-separated words in @var{text} that do @emph{not}
2601 match @var{pattern}, returning only matching words.  The pattern is one
2602 using @samp{%} as used in the @code{patsubst} function.  This can be used
2603 to separate out different types of strings (such as filenames) in a variable.
2604 For example:@refill
2605
2606 @example
2607 sources := foo.c bar.c ugh.h
2608 foo: $(sources)
2609         cc $(filter %.c,$(sources)) -o foo
2610 @end example
2611
2612 @noindent
2613 says that @file{foo} depends of @file{foo.c}, @file{bar.c} and
2614 @file{ugh.h} but only @file{foo.c} and @file{bar.c} should be specified
2615 in the command to the compiler.@refill
2616
2617 @item $(filter-out @var{pattern},@var{text})
2618 @findex filter-out
2619 Removes all whitespace-separated words in @var{text} that @emph{do}
2620 match @var{pattern}, returning only matching words.  This is the exact
2621 opposite of the @code{filter} function.@refill
2622
2623 @item $(sort @var{list})
2624 @findex sort
2625 Sorts the words of @var{list} in lexical order, removing duplicate
2626 words.  The output is a list of words separated by single spaces.
2627 @end table
2628
2629 Here is a realistic example of the use of @code{subst}.  Suppose that a
2630 makefile uses the @code{VPATH} variable to specify a list of directories
2631 that @code{make} should search for dependency files.  This example shows
2632 how to tell the C compiler to search for header files in the same list of
2633 directories.
2634
2635 The value of @code{VPATH} is a list of directories separated by colons,
2636 such as @samp{src:../headers}.  First, the @code{subst} function is used to
2637 change the colons to spaces:
2638
2639 @example
2640 $(subst :, ,$(VPATH))
2641 @end example
2642
2643 @noindent
2644 This produces @samp{src ../headers}.  Then another function,
2645 @code{addprefix}, can turn each directory name into an @samp{-I} flag.
2646 These can be added to the value of the variable @code{CFLAGS}, which is
2647 passed automatically to the C compiler, like this:
2648
2649 @example
2650 override CFLAGS:= $(CFLAGS) $(addprefix -I,$(subst :, ,$(VPATH)))
2651 @end example
2652
2653 @noindent
2654 The effect is to append the text @samp{-Isrc -I../headers} to the
2655 previously given value of @code{CFLAGS}.  The @code{override} directive is
2656 used so that the new value is assigned even if the previous value of
2657 @code{CFLAGS} was specified with a command argument (@pxref{Override
2658 Directive}).
2659
2660 The function @code{strip} can be very useful when used in conjunction
2661 with conditionals.  When comparing something with the null string
2662 @samp{""} using @code{ifeq} or @code{ifneq}, you usually want a string
2663 of just whitespace to match the null string.  Thus,
2664
2665 @example
2666 .PHONY: all
2667 ifneq   "$(needs_made)" ""
2668 all: $(needs_made)
2669 else
2670 all:;@@echo 'Nothing to make!'
2671 endif
2672 @end example
2673
2674 @noindent
2675 might fail to have the desired results.  Replacing the variable reference
2676 @samp{"$(needs_made)"} with the function call @samp{"$(strip
2677 $(needs_made))"} in the @code{ifneq} directive would make it more robust.
2678
2679 @node Foreach Function, Filename Functions, Text Functions, Functions
2680 @section The @code{foreach} Function
2681 @findex foreach
2682
2683 The @code{foreach} function is very different from other functions.  It
2684 causes one piece of text to be used repeatedly, each time with a different
2685 substitution performed on it.  It resembles the @code{for} command in the
2686 shell @code{sh} and the @code{foreach} command in the C-shell @code{csh}.
2687
2688 The syntax of the @code{foreach} function is:
2689
2690 @example
2691 $(foreach @var{var},@var{list},@var{text})
2692 @end example
2693
2694 @noindent
2695 The first two arguments, @var{var} and @var{list}, are expanded before
2696 anything else is done; note that the last argument, @var{text}, is
2697 @emph{not} expanded at the same time.  Then for each word of the expanded
2698 value of @var{list}, the variable named by the expanded value of @var{var}
2699 is set to that word, and @var{text} is expanded.  Presumably @var{text}
2700 contains references to that variable, so the expansions will be different
2701 each time.
2702
2703 The result is that @var{text} is expanded as many times as there are
2704 whitespace-separated words in @var{list}.  The multiple expansions of
2705 @var{text} are concatenated, with spaces between them, to make the result
2706 of @code{foreach}.
2707
2708 This simple example sets the variable @samp{files} to the list of all files
2709 in the directories in the list @samp{dirs}:
2710
2711 @example
2712 find_files = $(wildcard $(dir)/*)
2713 dirs := a b c d
2714 files := $(foreach dir,$(dirs),$(find_files))
2715 @end example
2716
2717 For readability, it is a good idea to put the @var{text} part of a
2718 @code{foreach} function invokation into a variable.  Here we use the
2719 variable @code{find_file} this way.  We make this a recursively-expanding
2720 variable by using @samp{=} to define it.  This is necessary so that when
2721 its value is substituted by @code{foreach} the value is rescanned for
2722 variable references and function calls.  This is what causes the
2723 @code{wildcard} function actually to be called.
2724
2725 This example has the same result (except for setting @samp{find_files},
2726 @samp{dirs} and @samp{dir}) as the following example:
2727
2728 @example
2729 files := $(wildcard a/* b/* c/* d/*)
2730 @end example
2731
2732 The value of the variable @var{var} after the @code{foreach} function call
2733 is the same as the value beforehand.  Other values taken from @var{list}
2734 are in effect only temporarily, during the execution of @code{foreach}.
2735 The variable @var{var} is a simply-expanded variable during the execution
2736 of @code{foreach} but is returned to the same flavor it was before the
2737 @code{foreach} when it is done.  @xref{Flavors}.
2738
2739 If @var{var} was previously undefined, then it is defined as a recursively
2740 expanded variable (@samp{=}, not @samp{:=}) during the @code{foreach} and
2741 remains so (with a null value) afterward.
2742
2743 Because it is expanded before @code{foreach} runs, the @var{var} argument
2744 to @code{foreach} need not be a literal variable name.  It can instead be a
2745 variable expression resulting in the name.  Thus,
2746
2747 @example
2748 dir := a
2749 var = $(whatsthevar)
2750 foo := dar
2751 whatsthevar := $(subst a,i,$(foo))
2752 files := $(foreach $(var),$(dirs),$(find_files))
2753 @end example
2754
2755 @noindent
2756 is ultimately equivalent to the first example.  You must take care when
2757 using complex variable expressions that result in variable names because
2758 many strange things are legal variable names, and these might not be what
2759 you intended.  For example,
2760
2761 @example
2762 files := $(foreach Es escrito en espanol!,b c ch,$(find_files))
2763 @end example
2764
2765 @noindent
2766 might be useful if @samp{find_files} references the variable @samp{Es
2767 escrito en espanol!} (es un nombre bastante largo, no?), but it is more
2768 likely to be a mistake.
2769
2770 @node Filename Functions,, Foreach Function, Functions
2771 @section Functions for File Names
2772
2773 Several of the built-in expansion functions relate specifically to
2774 taking apart file names or lists of file names.
2775
2776 Each of these functions performs a specific transformation on a file name.
2777 The argument of the function is regarded as a series of file names,
2778 separated by whitespace.  (Leading and trailing whitespace is ignored.)
2779 Each file name in the series is transformed in the same way and the results
2780 are concatenated with single spaces between them.
2781
2782 @table @code
2783 @item $(dir @var{names})
2784 @findex dir
2785 Extracts the directory-part of each file name in @var{names}.  The
2786 directory-part of the file name is everything up through (and
2787 including) the last slash in it.  If the file name contains no slash,
2788 the directory part is the string @samp{./}.  For example,
2789
2790 @example
2791 $(dir src/foo.c hacks)
2792 @end example
2793
2794 @noindent
2795 produces the result @samp{src/ ./}.
2796
2797 @item $(notdir @var{names})
2798 @findex notdir
2799 Extracts all but the directory-part of each file name in @var{names}.
2800 If the file name contains no slash, it is left unchanged.  Otherwise,
2801 everything through the last slash is removed from it.  A file name
2802 that ends with a slash becomes an empty string.  This is unfortunate,
2803 because it means that the result does not always have the same number
2804 of whitespace-separated file names as the argument had; but we do not
2805 see any other valid alternative.
2806
2807 For example,
2808
2809 @example
2810 $(notdir src/foo.c hacks)
2811 @end example
2812
2813 @noindent
2814 produces the result @samp{foo.c hacks}.
2815
2816 @item $(suffix @var{names})
2817 @findex suffix
2818 Extracts the suffix of each file name in @var{names}.  If the file name
2819 contains a period, the suffix is everything starting with the last
2820 period.  Otherwise, the suffix is the empty string.  This frequently
2821 means that the result will be empty when @var{names} is not, and if
2822 @var{names} contains multiple file names, the result may contain fewer
2823 file names.
2824
2825 For example,
2826
2827 @example
2828 $(suffix src/foo.c hacks)
2829 @end example
2830
2831 @noindent
2832 produces the result @samp{.c}.
2833
2834 @item $(basename @var{names})
2835 @findex basename
2836 Extracts all but the suffix of each file name in @var{names}.  If the
2837 file name contains a period, the basename is everything starting up to
2838 (and not including) the last period.  Otherwise, the basename is the
2839 entire file name.  For example,
2840
2841 @example
2842 $(basename src/foo.c hacks)
2843 @end example
2844
2845 @noindent
2846 produces the result @samp{src/foo hacks}.
2847
2848 @item $(addsuffix @var{suffix},@var{names})
2849 @findex addsuffix
2850 The argument @var{names} is regarded as a series of names, separated
2851 by whitespace; @var{suffix} is used as a unit.  The value of
2852 @var{suffix} is appended to the end of each individual name and the
2853 resulting larger names are concatenated with single spaces between
2854 them.  For example,
2855
2856 @example
2857 $(addsuffix .c,foo bar)
2858 @end example
2859
2860 @noindent
2861 produces the result @samp{foo.c bar.c}.
2862
2863 @item $(addprefix @var{prefix},@var{names})
2864 @findex addprefix
2865 The argument @var{names} is regarded as a series of names, separated
2866 by whitespace; @var{prefix} is used as a unit.  The value of
2867 @var{prefix} is appended to the front of each individual name and the
2868 resulting larger names are concatenated with single spaces between
2869 them.  For example,
2870
2871 @example
2872 $(addprefix src/,foo bar)
2873 @end example
2874
2875 @noindent
2876 produces the result @samp{src/foo src/bar}.
2877
2878 @item $(join @var{list1},@var{list2})
2879 @findex join
2880 Concatenates the two arguments word by word: the two first words (one
2881 from each argument) concatenated form the first word of the result, the
2882 two second words form the second word of the result, and so on.  So the
2883 @var{n}th word of the result comes from the @var{n}th word of each
2884 argument.  If one argument has more words that the other, the extra
2885 words are copied unchanged into the result.
2886
2887 For example, @samp{$(join a b,.c .o)} produces @samp{a.c b.o}.
2888
2889 Whitespace between the words in the lists is not preserved; it is
2890 replaced with a single space.
2891
2892 This function can reverse the effect of the @code{dir} and @code{notdir}
2893 functions, after other processing has been done on the separated lists
2894 of directories and files.@refill
2895
2896 @item $(firstword @var{names})
2897 @findex firstword
2898 The argument @var{names} is regarded as a series of names, separated
2899 by whitespace.  The value is the first name in the series.  The rest
2900 of the names are ignored.  For example,
2901
2902 @example
2903 $(firstword foo bar)
2904 @end example
2905
2906 @noindent
2907 produces the result @samp{foo}.
2908
2909 @item $(wildcard @var{pattern})
2910 @findex wildcard
2911 The argument @var{pattern} is a file name pattern, typically containing
2912 wildcard characters.  The result of @code{wildcard} is a space-separated
2913 list of the names of existing files that match the pattern.
2914
2915 Wildcards are expanded automatically in rules (@pxref{Wildcards}).
2916 But they are not normally expanded when a variable is set, or inside the
2917 arguments of other functions.  Those occasions are when the @code{wildcard}
2918 function is useful.
2919 @end table
2920
2921 @node Running, Implicit, Functions, Top
2922 @chapter How to Run @code{make}
2923
2924 A makefile that says how to recompile a program can be used in more than
2925 one way.  The simplest use is to recompile every file that is out of date.
2926 This is what @code{make} will do if run with no arguments.
2927
2928 But you might want to update only some of the files; you might want to use
2929 a different compiler or different compiler options; you might want just to
2930 find out which files are out of date without changing them.
2931
2932 By specifying arguments when you run @code{make}, you can do any of these
2933 things or many others.
2934
2935 @menu
2936 * Makefile Arguments::    Arguments to specify which makefile to use.
2937
2938 * Goals::                 Goal arguments specify which parts of the makefile
2939                            should be used.
2940
2941 * Avoid Compilation::     How to avoid recompiling certain files.
2942
2943 * Instead of Execution::  Mode flags specify what kind of thing to do
2944                            with the commands in the makefile
2945                            other than simply execute them.
2946
2947 * Overriding::            Overriding a variable can specify an alternate
2948                            compiler, or alternate flags for the compiler,
2949                            or whatever else you program into the makefile.
2950
2951 * Testing::               How to proceed past some errors, to test compilation.
2952
2953 * Options::               Summary of all options @code{make} accepts.
2954 @end menu
2955
2956 @node Makefile Arguments, Goals, Running, Running
2957 @section Arguments to Specify the Makefile
2958
2959 The way to specify the name of the makefile is with the @samp{-f} option.
2960 For example, @samp{-f altmake} says to use the file @file{altmake} as
2961 the makefile.
2962
2963 If you use the @samp{-f} flag several times (each time with a
2964 following argument), all the specified files are used jointly as
2965 makefiles.
2966
2967 If you do not use the @samp{-f} flag, the default is to use
2968 @file{./makefile}, or, if that does not exist, @file{./Makefile}.
2969 @xref{Makefiles}.@refill
2970
2971 @node Goals, Avoid Compilation, Makefile Arguments, Running
2972 @section Goals
2973 @cindex goal
2974
2975 The @dfn{goals} are the targets that @code{make} should strive ultimately
2976 to update.  Other targets are updated as well if they appear as
2977 dependencies of goals, or dependencies of dependencies of goals, etc.
2978
2979 By default, the goal is the first target in the makefile (not counting
2980 targets that start with a period or that appear in included makefiles).
2981 Therefore, makefiles are usually written so that the first target is for
2982 compiling the entire program or programs they describe.
2983
2984 You can specify a different goal or goal with arguments to @code{make}.
2985 Use the name of the goal as an argument.  If you specify several goals,
2986 @code{make} processes each of them in turn, in the order you name them.
2987
2988 Any target in the makefile may be specified as a goal (unless it starts
2989 with @samp{-} or contains an @samp{=}).  Even targets not in the makefile
2990 may be specified, if @code{make} can find implicit rules that say how to
2991 make them.
2992
2993 One use of specifying a goal is if you want to compile only a part of
2994 the program, or only one of several programs.  Specify as a goal each
2995 file that you wish to remake.  For example, consider a directory containing
2996 a several programs, with a makefile that starts like this:
2997
2998 @example
2999 .PHONY: all
3000 all: size nm ld ar as
3001 @end example
3002
3003 If you are working on the program @code{size}, you might want to say
3004 @samp{make size} so that only the files of that program are recompiled.
3005
3006 Another use of specifying a goal is to make files that aren't normally
3007 made.  For example, there may be a file of debugging output, or a version
3008 of the program that is compiled specially for testing, which has a rule
3009 in the makefile but isn't a dependency of the default goal.
3010
3011 Another use of specifying a goal is to run the commands associated with a
3012 phony target (@pxref{Phony Targets}) or empty target (@pxref{Empty Targets}).
3013 Many makefiles contain a phony target named @file{clean} which deletes
3014 everything except source files.  Naturally, this is done only if you
3015 request it explicitly with @samp{make clean}.  Here is a list of typical
3016 phony and empty target names:
3017
3018 @table @file
3019 @item all
3020 Make all the top-level targets the makefile knows about.
3021
3022 @item clean
3023 Delete all files that the makefile could remake.
3024
3025 @item clobber
3026 Delete absolutely everything the makefile could remake (whereas
3027 @samp{make clean} often leaves intact some files that might take a
3028 long time to remake).
3029
3030 @item install
3031 Copy the executable file into a directory that users typically search
3032 for commands; copy any auxiliary files that the executable uses into
3033 the directories where it will look for them.
3034
3035 @item print
3036 Print listings of the source files that have changed.
3037
3038 @item tar
3039 Create a tar file of the source files.
3040
3041 @item shar
3042 Create a shell archive (shar file) of the source files.
3043 @end table
3044
3045 @node Avoid Compilation, Instead of Execution, Goals, Running
3046 @section Avoiding Recompilation of Some Files
3047 @cindex -o
3048
3049 Sometimes you may have changed a source file but you don't want to
3050 recompile all the files that depend on it.  For example, suppose you add a
3051 macro or a declaration to a header file that many other files depend on.
3052 Being conservative, @code{make} assumes that any change in the header file
3053 requires recompilation of all dependent files, but you know that they don't
3054 need to be recompiled and you would rather not waste the time waiting.
3055
3056 If you anticipate the problem before making the change, you can use the
3057 @samp{-t} flag.  This flag tells @code{make} not to run the commands in the
3058 rules, but rather to mark the target up to date by changing its
3059 last-modification date.  You would follow this procedure:
3060
3061 @enumerate
3062 @item
3063 Use the command @samp{make} to recompile the source files that really
3064 need recompilation.
3065
3066 @item
3067 Make the changes in the header files.
3068
3069 @item
3070 Use the command @samp{make -t} to mark all the object files as
3071 up to date.  The next time you run @code{make}, the changes in the
3072 header files will not cause any recompilation.
3073 @end enumerate
3074
3075 If you have already changed the header file at a time when some files do
3076 need recompilation, it is too late to do this.  Instead, you can use the
3077 @samp{-o @var{file}} flag, which marks a specified file as ``old''
3078 (@pxref{Options}).  This means that the file itself won't be remade,
3079 and nothing else will be remade on its account.  Follow this procedure:
3080
3081 @enumerate
3082 @item
3083 Recompile the source files that need compilation for reasons independent
3084 of the particular header file, with @samp{make -o @var{headerfile}}.
3085 If several header files are involved, use a separate @samp{-o} option
3086 for each header file.
3087
3088 @item
3089 Touch all the object files with @samp{make -t}.
3090 @end enumerate
3091
3092 @node Instead of Execution, Overriding, Avoid Compilation, Running
3093 @section Instead of Executing the Commands
3094 @cindex -t
3095 @cindex touch
3096 @cindex -q
3097 @cindex -n
3098
3099 The makefile tells @code{make} how to tell whether a target is up to date,
3100 and how to update each target.  But updating the targets is not always
3101 what you want.  Certain options specify other activities for @code{make}.
3102
3103 @table @samp
3104 @item -t
3105 ``Touch''.  The activity is to mark the targets as up to date without
3106 actually changing them.  In other words, @code{make} pretends to compile
3107 the targets but does not really change their contents.
3108
3109 @item -n
3110 ``No-op''.  The activity is to print what commands would be used to make
3111 the targets up to date, but not actually execute them.
3112
3113 @item -q
3114 ``Question''.  The activity is to find out silently whether the targets
3115 are up to date already; but execute no commands in either case.  In other
3116 words, neither compilation nor output will occur.
3117 @end table
3118
3119 With the @samp{-n} flag, @code{make} prints without execution the commands
3120 that it would normally execute.
3121
3122 With the @samp{-t} flag, @code{make} ignores the commands in the rules
3123 and uses (in effect) the command @code{touch} for each target that needs to
3124 be remade.  The @code{touch} command is also printed, unless @samp{-s} or
3125 @code{.SILENT} is used.  For speed, @code{make} does not actually invoke
3126 the program @code{touch}.  It does the work directly.
3127
3128 With the @samp{-q} flag, @code{make} prints nothing and executes no
3129 commands, but the exit status code it returns is zero if and only if the
3130 targets to be considered are already up to date.
3131
3132 It is an error to use more than one of these three flags in the same
3133 invocation of @code{make}.
3134
3135 If you are not at all interested in what @code{make} @emph{would} do,
3136 but rather in some other information about @code{make}, there are two
3137 options: the command line @samp{make -p -f /dev/null} will print the
3138 information in @code{make}'s database of variables, rules, directories
3139 and files and @samp{make -v -f /dev/null} will print information about
3140 what version of GNU @code{make} you are using.  @xref{Options}.@refill
3141
3142 @node Overriding, Testing, Instead of Execution, Running
3143 @section Overriding Variables
3144
3145 You can override the value of a variable using an argument to @code{make}
3146 that contains a @samp{=}.  The argument @samp{@var{v}=@var{x}} (or
3147 @samp{@var{v}:=@var{x}}; @pxref{Flavors}) sets the value of the variable
3148 @var{v} to @var{x}.
3149
3150 Values specified this way override all values specified in the makefile
3151 itself; once you have set a variable with a command argument, any ordinary
3152 attempt in the makefile to change that variable is simply ignored.
3153
3154 One way to use this facility is to pass extra flags to compilers.
3155 For example, in a properly written makefile, the variable @code{CFLAGS}
3156 is included in each command that runs the C compiler, so a file
3157 @file{foo.c} would be compiled like this:
3158
3159 @example
3160 cc -c $(CFLAGS) foo.c
3161 @end example
3162
3163 Thus, whatever value you set for @code{CFLAGS} affects each compilation
3164 that occurs.  The makefile probably specifies the usual value for
3165 @code{CFLAGS}, like this:
3166
3167 @example
3168 CFLAGS=-g
3169 @end example
3170
3171 Each time you run @code{make}, you can override this value and specify a
3172 different value.  For example, if you say @samp{make CFLAGS='-g -O'}, each
3173 C compilation will be done with @samp{cc -c -g -O}.  (This illustrates how
3174 you can enclose spaces and other special characters in the value of a
3175 variable when you override it.)
3176
3177 The variable @code{CFLAGS} is only one of many standard variables that
3178 exist just so that you can change them this way.
3179 @xref{Implicit Variables}, for a complete list.
3180
3181 You can also program the makefile to look at additional variables of your
3182 own, giving the user ability to control other aspects of how the makefile
3183 works by changing the variables.
3184
3185 There is one way that the makefile can change a variable that you have
3186 overridden.  This is to use the @code{override} directive, which is a line
3187 that looks like this: @samp{override @var{variable} = @var{value}}.
3188 @xref{Override Directive}.
3189
3190 @node Testing, Options, Overriding, Running
3191 @section Testing the Compilation of a Program
3192
3193 Normally, when an error happens in executing a shell command, @code{make}
3194 gives up immediately, returning a nonzero status.  No further commands are
3195 executed for any target.  The error implies that the goal cannot be
3196 correctly remade, and @code{make} reports this as soon as it knows.
3197
3198 When you are compiling a program that you have just changed, this is not
3199 what you want.  Instead, you would rather that @code{make} try compiling
3200 every file that can be tried, to show you all the compilation errors.
3201
3202 @cindex -k
3203 On these occasions, you should use the @samp{-k} flag.  This tells
3204 @code{make} to continue to consider the other dependencies of the pending
3205 targets, remaking them if necessary, before it gives up and returns nonzero
3206 status.  For example, after an error in compiling one object file,
3207 @samp{make -k} will continue compiling other object files even though it
3208 already knows that linking them will be impossible.  In addition to
3209 contuing after failing shell commands, @samp{make -k} will continue as much
3210 as possible after discovering that it doesn't know how to make a target or
3211 dependency file.  This will always cause an error message, but without
3212 @samp{-k}, it is a fatal error.  @xref{Options}.
3213
3214 The usual behavior of @code{make} assumes that your purpose is to get the
3215 goals up to date; once @code{make} learns that this is impossible, it might
3216 as well report the failure immediately.  The @samp{-k} flag says that the
3217 real purpose is to test as much as possible of the changes made in the
3218 program, perhaps to find several independent problems so that you can
3219 correct them all before the next attempt to compile.  This is why Emacs's
3220 @code{compile} command passes the @samp{-k} flag by default.
3221
3222 @node Options,, Testing, Running
3223 @section Summary of Options
3224 @cindex options
3225 @cindex flags
3226
3227 Here is a table of all the options @code{make} understands:
3228
3229 @table @samp
3230 @item -b
3231 @itemx -m
3232 These options are ignored for compatibility with other versions of @code{make}.
3233
3234 @item -c @var{dir}
3235 Change to directory @var{dir} before executing the rules.  If multiple
3236 @samp{-c} options are specified, each is interpreted relative to the
3237 previous one: @samp{-c / -c etc} is equivalent to @samp{-c /etc}.
3238 This is typically used with recursive invocations of @code{make}
3239 (@pxref{Recursion}).
3240
3241 @item -d
3242 Print debugging information in addition to normal processing.  The
3243 debugging information says which files are being considered for
3244 remaking, which file-times are being compared and with what results,
3245 which files actually need to be remade, which implicit rules are
3246 considered and which are applied---everything interesting about how
3247 @code{make} decides what to do.
3248
3249 @item -f @var{file}
3250 Use file @var{file} as a makefile.  @xref{Makefiles}.
3251
3252 @item -i
3253 Ignore all errors in commands executed to remake files.
3254 @xref{Errors}.
3255
3256 @item -I @var{dir}
3257 Specifies a directory @var{dir} to search for included makefiles.
3258 @xref{Include}.  If several @samp{-I} options are used to specify
3259 several directories, the directories are searched in the order
3260 specified.
3261
3262 @item -k
3263 Continue as much as possible after an error.  While the target that
3264 failed, and those that depend on it, cannot be remade, the other
3265 dependencies of these targets can be processed all the same.
3266 @xref{Testing}.
3267
3268 @item -n
3269 Print the commands that would be executed, but do not execute them.
3270 @xref{Instead of Execution}.
3271
3272 @item -o @var{file}
3273 Do not remake the file @var{file} even if it is older than its
3274 dependencies, and do not remake anything on account of changes in
3275 @var{file}.  Essentially the file is treated as very old and its rules
3276 are ignored.  @xref{Avoid Compilation}.
3277
3278 @item -p
3279 Print the data base (rules and variable values) that results from
3280 reading the makefiles; then execute as usual or as otherwise
3281 specified.  This also prints the version information given by
3282 the @samp{-v} switch (see below).  To print the data base without
3283 trying to remake any files, use @samp{make -p -f /dev/null}.
3284
3285 @item -q
3286 ``Question mode''.  Do not run any commands, or print anything; just
3287 return an exit status that is zero if the specified targets are
3288 already up to date, nonzero otherwise.  @xref{Instead of Execution}.
3289
3290 @item -r
3291 Eliminate use of the built-in implicit rules (@pxref{Implicit}).
3292 Also clear out the default list of suffixes for suffix rules
3293 (@pxref{Suffix Rules}).
3294
3295 @item -s
3296 Silent operation; do not print the commands as they are executed.
3297 @xref{Echoing}.
3298
3299 @item -S
3300 Cancel the effect of the @samp{-k} option.  This is never necessary
3301 except in a recursive @code{make} where @samp{-k} might be inherited
3302 from the top-level @code{make} via @code{MAKEFLAGS} (@pxref{Recursion})
3303 or if you set @samp{-k} in @code{MAKEFLAGS} in your environment.@refill
3304
3305 @item -t
3306 Touch files (mark them up to date without really changing them)
3307 instead of running their commands.  This is used to pretend (to fool
3308 future invocations of @code{make}) that the commands were done.
3309 @xref{Instead of Execution}.
3310
3311 @item -v
3312 Print the version of the @code{make} program plus a copyright,
3313 list of authors and notice of (non)warranty (short).
3314 After this information is printed, processing continues normally.
3315 To get the version information without doing anything else,
3316 use @samp{make -v -f /dev/null}.
3317
3318 @item -w
3319 Print a message containing the working directory both before and after
3320 executing the makefile; this is useful for tracking down errors from
3321 builds of large directory trees.  @xref{Recursion}.
3322 @end table
3323
3324 @node Implicit, Archives, Running, Top
3325 @chapter Using Implicit Rules
3326 @cindex implicit rule
3327
3328 Certain standard ways of remaking target files are used very often.  For
3329 example, one customary way to make an object file is from a C source file
3330 using the C compiler, @code{cc}.
3331
3332 @dfn{Implicit rules} tell @code{make} how to use customary techniques so
3333 that you don't have to specify them in detail when you want to use them.
3334 For example, there is an implicit rule for C compilation.
3335
3336 Implicit rules work based on file names.  For example, C compilation typically
3337 takes a @file{.c} file and makes a @file{.o} file.  So @code{make} applies
3338 the implicit rule when it sees this combination of file-name endings.
3339
3340 A chain of implicit rules can apply in sequence; for example, @code{make}
3341 will remake a @file{.o} file from a @file{.y} file by way of a @file{.c} file.
3342 @iftex
3343 @xref{Chained Rules}.
3344 @end iftex
3345
3346 The built-in implicit rules use several variables in their commands so
3347 that, by changing the values of the variables, you can change the way the
3348 implicit rule works.  For example, the variable @code{CFLAGS} controls the
3349 flags given to the C compiler by the implicit rule for C compilation.
3350 @iftex
3351 @xref{Implicit Variables}.
3352 @end iftex
3353
3354 You can define your own implicit rules by writing @dfn{pattern rules}.
3355 @iftex
3356 @xref{Pattern Rules}.
3357 @end iftex
3358
3359 @menu
3360 * Using Implicit::       How to use an existing implicit rule
3361                           to get the commands for updating a file.
3362
3363 * Catalogue of Rules::   Catalogue of built-in implicit rules.
3364
3365 * Implicit Variables::   By changing certain variables, you can
3366                           change what the predefined implicit rules do.
3367
3368 * Chained Rules::        Using a chain of implicit rules.
3369
3370 * Pattern Rules::        Defining new implicit rules.
3371
3372 * Search Algorithm::     Precise algorithm for applying implicit rules.
3373 @end menu
3374
3375 @node Using Implicit, Catalogue of Rules, Implicit, Implicit
3376 @section Using Implicit Rules
3377
3378 To allow @code{make} to find a customary method for updating a target file,
3379 all you have to do is refrain from specifying commands yourself.  Either
3380 write a rule with no command lines, or don't write a rule at all.  Then
3381 @code{make} will figure out which implicit rule to use based on which
3382 kind of source file exists.
3383
3384 For example, suppose the makefile looks like this:
3385
3386 @example
3387 foo : foo.o bar.o
3388         cc -o foo foo.o bar.o $(CFLAGS) $(LDFLAGS)
3389 @end example
3390
3391 @noindent
3392 Because you mention @file{foo.o} but do not give a rule for it, @code{make}
3393 will automatically look for an implicit rule that tells how to update it.
3394 This happens whether or not the file @file{foo.o} currently exists.
3395
3396 If an implicit rule is found, it supplies both commands and a dependency
3397 (the source file).  You would want to write a rule for @file{foo.o} with no
3398 command lines if you need to specify additional dependencies, such as
3399 header files, that the implicit rule cannot supply.
3400
3401 Each implicit rule has a target pattern and dependency patterns.  There may
3402 be many implicit rules with the same target pattern.  For example, numerous
3403 rules make @samp{.o} files: one, from a @samp{.c} file with the C compiler;
3404 another, from a @samp{.p} file with the Pascal compiler; and so on.  The rule
3405 that actually applies is the one whose dependency exists or can be made.
3406
3407 So, if you have a file @file{foo.c}, @code{make} will run the C compiler;
3408 otherwise, if you have a file @file{foo.p}, @code{make} will run the Pascal
3409 compiler; and so on.
3410
3411 Of course, when you write the makefile, you know which implicit rule you
3412 want @code{make} to use, and you know it will choose that one because you
3413 know which other files are supposed to exist.  @xref{Catalogue of Rules},
3414 for a catalogue of all the predefined implicit rules.
3415
3416 Above, we said an implicit rule applies if the required dependency ``exists
3417 or can be made''.  A file ``can be made'' if it is mentioned explicitly in
3418 the makefile as a target or a dependency, or if an implicit rule can be
3419 recursively found for how to make it.  When the implicit dependency is the
3420 result of another implicit rule, we say that @dfn{chaining} is occurring.
3421 @xref{Chained Rules}.
3422
3423 In general, @code{make} searches for an implicit rule for each target, and
3424 for each double-colon rule, that has no commands.  A file that is mentioned
3425 only as a dependency is considered a target whose rule specifies nothing,
3426 so implicit rule search happens for it.  @xref{Search Algorithm}, for the
3427 details of how the search is done.
3428
3429 @node Catalogue of Rules, Implicit Variables, Using Implicit, Implicit
3430 @section Catalogue of Implicit Rules
3431
3432 Here is a catalogue of predefined implicit rules which are always available
3433 unless the makefile explicitly overrides or cancels them.
3434 (@xref{Canceling Rules}, for information on canceling or overriding an
3435 implicit rule.  The @samp{-r} option cancels all predefined rules.)
3436
3437 @table @asis
3438 @item Compiling C programs
3439 @file{@var{n}.o} will be made automatically from @file{@var{n}.c} 
3440 with the command @samp{$(CC) -c $(CFLAGS)}.@refill
3441
3442 @item Compiling Pascal programs
3443 @file{@var{n}.o} will be made automatically from @file{@var{n}.p}
3444 with the command @samp{$(PC) -c $(PFLAGS)}.@refill
3445
3446 @item Compiling Fortran, EFL and Ratfor programs
3447 @file{@var{n}.o} will be made automatically from @file{@var{n}.e},
3448 @file{@var{n}.r}, @file{@var{n}.F} or @file{@var{n}.f} by running the
3449 Fortran compiler.  The precise command used is as follows:@refill
3450
3451 @table @code
3452 @item .e
3453 @samp{$(FC) -c $(EFLAGS)}.
3454 @item .f
3455 @samp{$(FC) -c $(FFLAGS)}.
3456 @item .F
3457 @samp{$(FC) -c $(FFLAGS)}.
3458 @item .r
3459 @samp{$(FC) -c $(RFLAGS)}.
3460 @end table
3461
3462 @item Preprocessing Fortran, EFL and Ratfor programs
3463 @file{@var{n}.f} will be made automatically from @file{@var{n}.e},
3464 @file{@var{n}.r} or @file{@var{n}.F}.  This rule runs just the
3465 preprocessor to convert a Ratfor, EFL or preprocessable Fortran
3466 program into a strict Fortran program.  The precise command used is as
3467 follows:@refill
3468
3469 @table @code
3470 @item .e
3471 @samp{$(FC) -F $(EFLAGS)}.
3472 @item .F
3473 @samp{$(FC) -F $(FFLAGS)}.
3474 @item .r
3475 @samp{$(FC) -F $(RFLAGS)}.
3476 @end table
3477
3478 @item Assembling assembler programs
3479 @file{@var{n}.o} will be made automatically from @file{@var{n}.s} by
3480 running the assembler @code{as}.  The precise command used is
3481 @samp{$(AS) $(ASFLAGS)}.@refill
3482
3483 @item Linking a single object file
3484 @file{@var{n}} will be made automatically from @file{@var{n}.o} by
3485 running the linker @code{ld} via the C compiler.  The precise command
3486 used is @samp{$(CC) $(LDFLAGS) @dots{} $(LOADLIBES)}.@refill
3487
3488 This rule does the right thing for a simple program with only one source
3489 file.  It will also do the right thing if there are multiple object files
3490 (presumably coming from various other source files), the first of which has
3491 a name matching that of the executable file.  Thus,
3492
3493 @example
3494 x: y.o z.o
3495 @end example
3496
3497 @noindent
3498 when @file{x.c}, @file{y.c} and @file{z.c} all exist will execute:
3499
3500 @example
3501 cc -c x.c -o x.o
3502 cc -c y.c -o y.o
3503 cc -c z.c -o z.o
3504 cc x.o y.o z.o -o x
3505 rm -f x.o
3506 rm -f y.o
3507 rm -f z.o
3508 @end example
3509
3510 @noindent
3511 In more complicated cases, you must write an explicit command for linking.
3512
3513 @item Yacc for C programs
3514 @file{@var{n}.c} will be made automatically from @file{@var{n}.y} by
3515 running Yacc with the command @samp{$(YACC) $(YFLAGS)}.
3516
3517 @item Yacc for Ratfor programs
3518 @file{@var{n}.r} will be made automatically from @file{@var{n}.yr} by
3519 running Yacc with the command @samp{$(YACCR) $(YFLAGS)}.
3520
3521 @item Yacc for EFL programs
3522 @file{@var{n}.e} will be made automatically from @file{@var{n}.ye} by
3523 running Yacc with the command @samp{$(YACCE) $(YFLAGS)}.
3524
3525 @item Lex for C programs
3526 @file{@var{n}.c} will be made automatically from @file{@var{n}.l} by
3527 by running Lex.  The actual command is @samp{$(LEX) $(LFLAGS)}.
3528
3529 @item Lex for Ratfor programs
3530 @file{@var{n}.r} will be made automatically from @file{@var{n}.l} by
3531 by running Lex.  The actual command is @samp{$(LEX) $(LFLAGS)}.
3532
3533 The traditional custom of using the same suffix @samp{.l} for all Lex
3534 files regardless of whether they produce C code or Ratfor code makes
3535 it impossible for @code{make} to determine automatically which of the
3536 two languages you are using in any particular case.  If @code{make} is
3537 called upon to remake an object file from a @samp{.l} file, it must
3538 guess which compiler to use.  It will guess the C compiler, because
3539 that is more common.  If you are using Ratfor, make sure @code{make}
3540 knows this by mentioning @file{@var{n}.r} in the makefile.
3541
3542 @item RCS
3543 Any file @file{@var{n}} will be extracted if necessary from an RCS
3544 file named either @file{@var{n},v} or @file{RCS/@var{n},v}.  The
3545 precise command used is @samp{$(CO) $(COFLAGS)}.  The variable
3546 @code{CO} has default value @samp{co}.
3547
3548 @item SCCS
3549 Any file @file{@var{n}} will be extracted if necessary from an SCCS
3550 file named either @file{s.@var{n}} or @file{SCCS/s.@var{n}}.  The
3551 precise command used is @samp{$(GET) $(GFLAGS)}.
3552
3553 We recommend that you avoid the use of SCCS.  RCS is widely held to be
3554 superior, and is also free.  By choosing free software in place of
3555 comparable (or lesser) proprietary software, you support the free software
3556 movement.
3557 @end table
3558
3559 @node Implicit Variables, Chained Rules, Catalogue of Rules, Implicit
3560 @section Variables Used by Implicit Rules
3561 @cindex flags for compilers
3562
3563 The commands in built-in implicit rules make liberal use of certain
3564 predefined variables.  You can redefine these variables, either in the
3565 makefile or with arguments to @code{make}, to alter how the implicit rules
3566 work without actually redefining them.
3567
3568 For example, the command used to compile a C source file actually says
3569 @samp{$(CC) -c $(CFLAGS)}.  The default values of the variables used
3570 are @samp{cc} and nothing, resulting in the command @samp{cc -c}.  By
3571 redefining @samp{$(CC)} to @samp{ncc}, you could cause @samp{ncc} to
3572 be used for all C compilations performed by the implicit rule.  By
3573 redefining @samp{$(CFLAGS)} to be @samp{-g}, you could pass the
3574 @samp{-g} option to each compilation.  @emph{All} implicit rules that
3575 do C compilation use @samp{$(CC)} to get the program name for the
3576 compiler and @emph{all} include @samp{$(CFLAGS)} among the arguments
3577 given to the compiler.@refill
3578
3579 The variables used in implicit rules fall into two classes: those that are
3580 names of programs (like @code{CC}) and those that contain arguments for the
3581 programs (like @code{CFLAGS}).  (The ``name of a program'' may also contain
3582 some command arguments, but it must start with an actual executable program
3583 name.)  If a variable value contains more than one argument, separate them
3584 with spaces.
3585
3586 Here is a table of variables used as names of programs:
3587
3588 @table @code
3589 @item AS
3590 @vindex AS
3591 Program for doing assembly; default @samp{as}.
3592
3593 @item CC
3594 @vindex CC
3595 Program for compiling C programs; default @samp{cc}.
3596
3597 @item CO
3598 @vindex CO
3599 Program for extracting a file from RCS; default @samp{co}.
3600
3601 @item FC
3602 @vindex FC
3603 Program for compiling or preprocessing Fortran, Ratfor,
3604 and EFL programs; default @samp{f77}.
3605
3606 @item GET
3607 @vindex GET
3608 Program for extracting a file from SCCS; default @samp{get}.
3609
3610 @item LEX
3611 @vindex LEX
3612 Program to use to turn Lex grammars into C programs or Ratfor programs;
3613 default @samp{lex}.
3614
3615 @item PC
3616 @vindex PC
3617 Program for compiling Pascal programs; default @samp{pc}.
3618
3619 @item YACC
3620 @vindex YACC
3621 Program to use to turn Yacc grammars into C programs; default @samp{yacc}.
3622
3623 @item YACCR
3624 @vindex YACCR
3625 Program to use to turn Yacc grammars into Ratfor
3626 programs; default @samp{yacc -r}.
3627
3628 @item YACCE
3629 @vindex YACCE
3630 Program to use to turn Yacc grammars into EFL
3631 programs; default @samp{yacc -e}.
3632
3633 @item RANLIB
3634 @vindex RANLIB
3635 Program to use to update the symbol-directory of an archive
3636 (the @file{__.SYMDEF} member); default @samp{ranlib}.
3637 @end table
3638
3639 Here is a table of variables whose values are additional arguments for the
3640 programs above.  The default values for all of these is the empty string.
3641
3642 @table @code
3643 @item ASFLAGS
3644 @vindex ASFLAGS
3645 Extra flags to give to the assembler (when explicitly
3646 invoked on a @samp{.s} file).
3647
3648 @item CFLAGS
3649 @vindex CFLAGS
3650 Extra flags to give to the C compiler.
3651
3652 @item EFLAGS
3653 @vindex EFLAGS
3654 Extra flags to give to the Fortran compiler for EFL programs.
3655
3656 @item FFLAGS
3657 @vindex FFLAGS
3658 Extra flags to give to the Fortran compiler.
3659
3660 @item LFLAGS
3661 @vindex LFLAGS
3662 Extra flags to give to Lex.
3663
3664 @item LDFLAGS
3665 @vindex LDFLAGS
3666 Extra flags to give to compilers when they are supposed to invoke the
3667 linker, @samp{ld} (actually the value of the variable @code{LD}).
3668
3669 @item PFLAGS
3670 @vindex PFLAGS
3671 Extra flags to give to the Pascal compiler.
3672
3673 @item RFLAGS
3674 @vindex RFLAGS
3675 Extra flags to give to the Fortran compiler for Ratfor programs.
3676
3677 @item YFLAGS
3678 @vindex YFLAGS
3679 Extra flags to give to Yacc.
3680 @end table
3681
3682 @node Chained Rules, Pattern Rules, Implicit Variables, Implicit
3683 @section Chains of Implicit Rules
3684
3685 @cindex chains of rules
3686 Sometimes a file can be made by a sequence of implicit rules.  For example,
3687 a file @file{@var{n}.o} could be made from @file{@var{n}.y} by running
3688 first Yacc and then @code{cc}.  Such a sequence is called a @dfn{chain}.
3689
3690 If the file @file{@var{n}.c} exists, or is mentioned in the makefile, no
3691 special searching is required: @code{make} finds that the object file can
3692 be made by C compilation from @file{@var{n}.c}; later on, when considering
3693 how to make @file{@var{n}.c}, the rule for running Yacc will be
3694 used.  Ultimately both @file{@var{n}.c} and @file{@var{n}.o} are
3695 updated.@refill
3696
3697 @cindex intermediate file
3698 However, even if @file{@var{n}.c} does not exist and is not mentioned,
3699 @code{make} knows how to envision it as the missing link between
3700 @file{@var{n}.o} and @file{@var{n}.y}!  In this case, @file{@var{n}.c} is
3701 called an @dfn{intermediate file}.  Once @code{make} has decided to use the
3702 intermediate file, it is entered in the data base as if it had been
3703 mentioned in the makefile, along with the implicit rule that says how to
3704 create it.@refill
3705
3706 Intermediate files are remade using their rules just like all other
3707 files.  The difference is that the intermediate file is deleted when
3708 @code{make} is finished.  Therefore, the intermediate file which did
3709 not exist before @code{make} also does not exist after @code{make}.
3710 The deletion is reported to you by printing a @samp{rm -f} command
3711 that shows what @code{make} is doing.  (You can optionally define an
3712 implicit rule so as to preserve certain intermediate files.  You can also
3713 list the target pattern of an implicit rule (such as @samp{%.o}) as a
3714 dependency file of the special target @code{.PRECIOUS} to preserve intermediate
3715 files whose target patterns match that file's name.)@refill
3716
3717 A chain can involve more than two implicit rules.  For example, it is
3718 possible to make a file @file{foo} from @file{RCS/foo.y,v} by running RCS,
3719 Yacc and @code{cc}.  Then both @file{foo.y} and @file{foo.c} are
3720 intermediate files that are deleted at the end.@refill
3721
3722 No single implicit rule can appear more than once in a chain.  This means
3723 that @code{make} will not even consider such a ridiculous thing as making
3724 @file{foo} from @file{foo.o.o} by running the linker twice.  This
3725 constraint has the added benefit of preventing any infinite loop in the
3726 search for an implicit rule chain.
3727
3728 There are some special implicit rules to optimize certain cases that would
3729 otherwise by handled by rule chains.  For example, making @file{foo} from
3730 @file{foo.c} could be handled by compiling and linking with separate rules,
3731 using @file{foo.o} as an intermediate file.  But what actually happens is
3732 that a special rule for this case does the compilation and linking with a
3733 single @code{cc} command.  The optimized rule is used in preference to the
3734 step-by-step chain because it comes earlier in the ordering of rules.
3735
3736 @node Pattern Rules, Last Resort, Chained Rules, Implicit
3737 @section Defining and Redefining Pattern Rules
3738
3739 @cindex pattern rule
3740 You define an implicit rule by writing a @dfn{pattern rule}.  A pattern
3741 rule looks like an ordinary rule, except that its target contains the
3742 character @samp{%} (exactly one of them).  The target is considered a
3743 pattern for matching file names; the @samp{%} can match any substring,
3744 while other characters match only themselves.
3745
3746 For example, @samp{%.c} as a pattern matches any file name that ends in
3747 @samp{.c}.  @samp{s.%.c} as a pattern matches any file name that starts
3748 with @samp{s.}, ends in @samp{.c} and is at least five characters long.
3749 (There must be at least one character to match the @samp{%}.)  The substring
3750 that the @samp{%} matches is called the @dfn{stem}.@refill
3751
3752 A pattern rule must have at least one dependency that uses @samp{%}.
3753 @samp{%} in a dependency of a pattern rule stands for the same stem
3754 that was matched by the @samp{%} in the target.  In order for
3755 the pattern rule to apply, its target pattern must match the file name
3756 under consideration, and its dependency patterns must name files that
3757 exist or can be made.  These files become dependencies of the target.
3758
3759 There may also be dependencies that do not use @samp{%}; such a dependency
3760 attaches to every file made by this pattern rule.  These unvarying
3761 dependencies are rarely useful.
3762
3763 The order in which pattern rules appear in the makefile is important
3764 because the rules are considered in that order.  Of equally applicable
3765 rules, the first one found is used.  The rules you write take precedence
3766 over those that are built in.  Note, however, that a rule whose
3767 dependencies actually exist or are mentioned always takes priority over a
3768 rule with dependencies that must be made by chaining other implicit rules.
3769
3770 @menu
3771 * Examples: Pattern Examples.  Real examples of pattern rule definitions.
3772
3773 * Vars: Automatic.             The automatic variables enable the commands
3774                                 in pattern rules to act on the right files.
3775
3776 * Matching: Pattern Match.     Details of how patterns match.
3777
3778 * Match-Anything Rules::       Precautions in defining a rules that can
3779                                 match any target file whatever.
3780
3781 * Canceling Rules::           Overriding or canceling built-in rules.
3782
3783 * Last Resort::                How to define a last-resort rule
3784                                 that applies to any target that no other
3785                                 rule applies to.
3786
3787 * Suffix Rules::               The old-fashioned way to define implicit rules.
3788 @end menu
3789
3790 @node Pattern Examples, Automatic, Pattern Rules, Pattern Rules
3791 @subsection Pattern Rule Examples
3792
3793 Here are some examples of pattern rules actually predefined in
3794 @code{make}.  First, the rule that compiles @samp{.c} files into @samp{.o}
3795 files:@refill
3796
3797 @example
3798 %.o : %.c
3799         $(CC) -c $(CFLAGS) $< -o $@@
3800 @end example
3801
3802 @noindent
3803 defines a rule that can make any file @file{@var{x}.o} from
3804 @file{@var{x}.c}.  The command uses the automatic variables @samp{$@@} and
3805 @samp{$<} to substitute the names of the target file and the source file
3806 as they are in each case where the rule apply (@pxref{Automatic}).@refill
3807
3808 Here is a second built-in rule:
3809
3810 @example
3811 % :: RCS/%,v
3812         $(CO) $(COFLAGS) $<
3813 @end example
3814
3815 @noindent
3816 defines a rule that can make any file @file{@var{x}} whatever from a
3817 corresponding file @file{@var{x},v} in the subdirectory @file{RCS}.  Since
3818 the target is @samp{%}, this rule will apply to any file whatever, provided
3819 the appropriate dependency file exists.  The double colon makes the rule
3820 @dfn{terminal}, which means that its dependency may not be an intermediate
3821 file (@pxref{Match-Anything Rules}).@refill
3822
3823 @node Automatic, Pattern Match, Pattern Examples, Pattern Rules
3824 @subsection Automatic Variables
3825 @cindex automatic variables
3826
3827 Suppose you are writing a pattern rule to compile a @samp{.c} file into a
3828 @samp{.o} file: how do you write the @samp{cc} command so that it operates
3829 on the right source file name?  You can't write the name in the command,
3830 because the name is different each time the implicit rule is applied.
3831
3832 What you do is use a special feature of @code{make}, the @dfn{automatic
3833 variables}.  These variables have values computed afresh for each rule that
3834 is executed, based on the target and dependencies of the rule.  In this
3835 example, you would use @samp{$@@} for the object file name and @samp{$<}
3836 for the source file name.
3837
3838 Here is a table of automatic variables:
3839
3840 @table @code
3841 @item $@@
3842 The file name of the target of the rule.  If the target is an archive
3843 member, then @samp{$@@} is the name of the archive file.
3844
3845 @item $%
3846 The target member name, when the target is an archive member.  For
3847 example, if the target is @file{foo.a(bar.o)} then @samp{$%} is
3848 @file{bar.o} and @samp{$@@} is @file{foo.a}.  @samp{$%} is empty
3849 when the target is not an archive member.
3850
3851 @item $<
3852 The name of the first dependency.
3853
3854 @item $?
3855 The names of all the dependencies that are newer than the target, with
3856 spaces between them.
3857
3858 @item $^
3859 The names of all the dependencies, with spaces between them.
3860
3861 @item $*
3862 The stem with which an implicit rule matches (@pxref{Pattern Match}).
3863 If the target is @file{dir/a.foo.b} and the target pattern is
3864 @file{a.%.b} then the stem is @file{dir/foo}.  The stem is useful for
3865 constructing names of related files.@refill
3866 @end table
3867
3868 @samp{$?} is useful even in explicit rules when you wish to operate on only
3869 the dependencies that have changed.  For example, suppose that an archive
3870 named @file{lib} is supposed to contain copies of several object files.
3871 This rule copies just the changed object files into the archive:
3872
3873 @example
3874 lib: foo.o bar.o lose.o win.o
3875         ar c lib $?
3876 @end example
3877
3878 Of the variables listed above, four have values that are single file names.
3879 These four have variants that get just the file's directory name or just
3880 the file name within the directory.  The variant variables' names are
3881 formed by appending @samp{D} or @samp{F}, respectively.  These variants are
3882 semi-obsolete in GNU @code{make} since the functions @code{dir} and
3883 @code{notdir} can be used to get an equivalent effect (@pxref{Filename
3884 Functions}).  Here is a table of the variants:@refill
3885
3886 @table @samp
3887 @item $(@@D)
3888 The directory part of the file name of the target.  If the value of
3889 @samp{$@@} is @file{dir/foo.o} then @samp{$(@@D)} is @file{dir/}.
3890 This value is @file{./} if @samp{$@@} does not contain a slash.
3891 @samp{$(@@D)} is equivalent to @samp{$(dir $@@)}.@refill
3892
3893 @item $(@@F)
3894 The file-within-directory part of the file name of the target.  If the
3895 value of @samp{$@@} is @file{dir/foo.o} then @samp{$(@@F)} is
3896 @file{foo.o}.  @samp{$(@@F)} is equivalent to @samp{$(notdir $@@)}.
3897
3898 @item $($/)
3899 The same as @code{$(@@F)}, for compatibility with some other versions
3900 of @code{make}.
3901
3902 @item $(%D)
3903 @itemx $(%F)
3904 The directory part and the file-within-directory part of the archive
3905 member name.
3906
3907 @item $(*D)
3908 @itemx $(*F)
3909 The directory part and the file-within-directory part of the stem;
3910 @file{dir/} in this example.
3911
3912 @item $(<D)
3913 @itemx $(<F)
3914 The directory part and the file-within-directory part of the first
3915 implicit dependency.
3916 @end table
3917
3918 @node Pattern Match, Match-Anything Rules, Automatic, Pattern Rules
3919 @subsection How Patterns Match
3920
3921 @cindex stem
3922 A target pattern is composed of a @samp{%} between a prefix and a suffix,
3923 either of which may be empty.  The pattern matches a file name only if the
3924 file name starts with the prefix and ends with the suffix, without overlap.
3925 The text between the prefix and the suffix is called the @dfn{stem}.  Thus,
3926 when the pattern @samp{%.o} matches the file name @file{test.o}, the stem
3927 is @samp{test}.  The pattern rule dependencies are turned into actual file
3928 names by substituting the stem for the character @samp{%}.  Thus, if in the
3929 same example one of the dependencies is written as @samp{%.c}, it expands
3930 to @samp{test.c}.@refill
3931
3932 When the target pattern does not contain a slash (and usually it does not),
3933 directory names in the file names are removed from the file name before it
3934 is compared with the target prefix and suffix.  The directory names, along
3935 with the slash that ends them, are added back to the stem.  Thus,
3936 @samp{e%t} does match the file name @file{src/eat}, with @samp{src/a} as
3937 the stem.  When dependencies are turned into file names, the directories
3938 from the stem are added at the front, while the rest of the stem is
3939 substituted for the @samp{%}.  The stem @samp{src/a} with a dependency
3940 pattern @samp{c%r} gives the file name @file{src/car}.@refill
3941
3942 @node Match-Anything Rules, Canceling Rules, Pattern Match, Pattern Rules
3943 @subsection Match-Anything Pattern Rules
3944
3945 @cindex match-anything rule
3946 @cindex terminal rule
3947 When a pattern rule's target is just @samp{%}, it matches any filename
3948 whatever.  We call these rules @dfn{match-anything} rules.  They are very
3949 useful, but it can take a lot of time for @code{make} to think about them,
3950 because it must consider every such rule for each file name listed either
3951 as a target or as a dependency.
3952
3953 Suppose the makefile mentions @file{foo.c}.  For this target, @code{make}
3954 would have to consider making it by linking an object file @file{foo.c.o},
3955 or by C compilation-and-linking in one step from @file{foo.c.c}, or by
3956 Pascal compilation-and-linking from @file{foo.c.p}, and many other
3957 possibilities.  We know these possibilities are ridiculous since
3958 @file{foo.c} is a C source file, not an executable.@refill
3959
3960 If @code{make} did consider these possibilities, it would ultimately reject
3961 them, because files such as @file{foo.c.o}, @file{foo.c.p}, etc. would not
3962 exist.  But these possibilities are so numerous that @code{make} would run
3963 very slowly if it had to consider them.@refill
3964
3965 To gain speed, we have put various constraints on the way @code{make}
3966 considers match-anything rules.  There are two different constraints that
3967 can be applied, and each time you define a match-anything rule you must
3968 choose one or the other for that rule.
3969
3970 One choice is to mark the match-anything rule as @dfn{terminal} by defining
3971 it with a double colon.  When a rule is terminal, it does not apply
3972 unless its dependencies actually exist.  Dependencies that could be made
3973 with other implicit rules are not good enough.
3974
3975 For example, the built-in implicit rules for extracting sources from RCS
3976 and SCCS files are terminal; as a result, if the file @file{foo.c,v} does
3977 not exist, @code{make} will not even consider trying to make it as an
3978 intermediate file from @file{foo.c,v.o} or from @file{RCS/SCCS/s.foo.c,v}.
3979 RCS and SCCS files are generally ultimate source files, which should not be
3980 remade from any other files; therefore, @code{make} can save time by not
3981 looking for ways to remake them.@refill
3982
3983 If you do not mark the match-anything rule as terminal, then it is
3984 nonterminal.  A nonterminal match-anything rule cannot apply to a file name
3985 that indicates a specific type of data.  A file name indicates a specific
3986 type of data if some non-match-anything implicit rule target matches it.
3987
3988 For example, the file name @file{foo.c} matches the target for the pattern
3989 rule @samp{%.c : %.y} (the rule to run Yacc).  Regardless of whether this
3990 rule is actually applicable (which happens only if there is a file
3991 @file{foo.y}), the fact that its target matches is enough to prevent
3992 consideration of any nonterminal match-everything rules for the file
3993 @file{foo.c}.  Thus, @code{make} will not even consider trying to make
3994 @file{foo.c} as an executable file from @file{foo.c.o}, @file{foo.c.c},
3995 @file{foo.c.p}, etc.@refill
3996
3997 The motivation for this constraint is that nonterminal match-everything
3998 rules are used for making files containing specific types of data (such as
3999 executable files) and a file name with a recognized suffix indicates a
4000 specific different type of data (such as a C source file).
4001
4002 Special built-in dummy pattern rules are provided solely to recognize
4003 certain file names so that nonterminal match-everything rules won't be
4004 considered.  These dummy rules have no dependencies and no commands, and
4005 they are ignored for all other purposes.  For example, the built-in
4006 implicit rule
4007
4008 @example
4009 %.p :
4010 @end example
4011
4012 @noindent
4013 exists to make sure that Pascal source files such as @file{foo.p} match a
4014 specific target pattern and thereby prevent time from being wasted looking
4015 for @file{foo.p.o} or @file{foo.p.c}.
4016
4017 @node Canceling Rules,, Match-Anything Rules, Pattern Rules
4018 @subsection Canceling Implicit Rules
4019
4020 You can override a built-in implicit rule by defining a new pattern rule
4021 with the same target and dependencies, but different commands.  When the
4022 new rule is defined, the built-in one is replaced.  The new rule's position
4023 in the sequence of implicit rules is determined by where you write the new
4024 rule.
4025
4026 You can cancel a built-in implicit rule by defining a pattern rule with the
4027 same target and dependencies, but no commands.  For example, the following
4028 would cancel the rule that runs the assembler:
4029
4030 @example
4031 %.o : %.s
4032 @end example
4033
4034 @node Last Resort, Suffix Rules, Pattern Rules, Implicit
4035 @section Defining Last-Resort Default Rules
4036
4037 @findex .DEFAULT
4038 You can define a last-resort implicit rule by writing a rule for the target
4039 @code{.DEFAULT}.  Such a rule's commands are used for all targets and
4040 dependencies that have no commands of their own and for which no other
4041 implicit rule applies.  Naturally, there is no @code{.DEFAULT} rule unless
4042 you write one.
4043
4044 For example, when testing a makefile, you might not care if the source
4045 files contain real data, only that they exist.  Then you might do this:
4046
4047 @example
4048 .DEFAULT:
4049         touch $@@
4050 @end example
4051
4052 @noindent
4053 to cause all the source files needed (as dependencies) to be created
4054 silently.
4055
4056 @node Suffix Rules, Search Algorithm, Last Resort, Implicit
4057 @section Old-Fashioned Suffix Rules
4058 @cindex suffix rules
4059
4060 @dfn{Suffix rules} are the old-fashioned way of defining implicit rules for
4061 @code{make}.  Suffix rules are obsolete because pattern rules are more
4062 general and clearer.  They are supported in GNU @code{make} for
4063 compatibility with old makefiles.  They come in two kinds:
4064 @dfn{double-suffix} and @dfn{single-suffix}.@refill
4065
4066 A double-suffix rule is defined by a pair of suffixes: the target suffix
4067 and the source suffix.  It matches any file whose name ends with the target
4068 suffix.  The corresponding implicit dependency is to the file name made by
4069 replacing the target suffix with the source suffix.  A two-suffix rule
4070 whose target and source suffixes are @samp{.o} and @samp{.c} is equivalent
4071 to the pattern rule @samp{%.o : %.c}.
4072
4073 A single-suffix rule is defined by a single suffix, which is the source
4074 suffix.  It matches any file name, and the corresponding implicit
4075 dependency name is made by appending the source suffix.  A single-suffix
4076 rule whose source suffix is @samp{.c} is equivalent to the pattern rule
4077 @samp{% : %.c}.
4078
4079 Suffix rule definitions are recognized by comparing each rule's target
4080 against a defined list of known suffixes.  When @code{make} sees a rule
4081 whose target is a known suffix, this rule is considered a single-suffix
4082 rule.  When @code{make} sees a rule whose target is two known suffixes
4083 concatenated, this rule is taken as a double-suffix rule.
4084
4085 For example, @samp{.c} and @samp{.o} are both on the default list of known
4086 suffixes.  Therefore, if you define a rule whose target is @samp{.c.o},
4087 @code{make} takes it to be a double-suffix rule with source suffix
4088 @samp{.c} and target suffix @samp{.o}.  For example, here is the old
4089 fashioned way to define the rule for compiling a C source:@refill
4090
4091 @example
4092 .c.o:
4093         $(CC) -c $(CFLAGS) -o $@@ $<
4094 @end example
4095
4096 @findex .SUFFIXES
4097 The known suffixes are simply the names of the dependencies of the special
4098 target @code{.SUFFIXES}.  You can add your own suffixes by writing a rule
4099 for @code{.SUFFIXES} that adds more dependencies, as in
4100
4101 @example
4102 .SUFFIXES: .hack .win
4103 @end example
4104
4105 @noindent
4106 which adds @samp{.hack} and @samp{.win} to the end of the list of suffixes.
4107
4108 If you wish to eliminate the default known suffixes instead of just adding
4109 to them, write a rule for @code{.SUFFIXES} with no dependencies.  By
4110 special dispensation, this eliminates all existing dependencies of
4111 @code{.SUFFIXES}.  You can then write another rule to add the suffixes you
4112 want.  For example,
4113
4114 @example
4115 .SUFFIXES:    # @r{Delete the default suffixes}
4116 .SUFFIXES: .c .o .h   # @r{Define our suffix list}
4117 @end example
4118
4119 The @samp{-r} flag causes the default list of suffixes to be empty.
4120
4121 @node Search Algorithm,, Last Resort, Implicit
4122 @section Implicit Rule Search Algorithm
4123
4124 Here is the procedure @code{make} uses for searching for an implicit rule
4125 for a target @var{t}.  This procedure is followed for each double-colon
4126 rule with no commands, for each target of ordinary rules none of which have
4127 commands, and for each dependency that is not the target of any rule.  It
4128 is also followed recursively for dependencies that come from implicit
4129 rules, in the search for a chain of rules.
4130
4131 Suffix rules are not mentioned in this algorithm because suffix rules are
4132 converted to equivalent pattern rules after the makefiles have been read
4133 in.
4134
4135 For an archive member target of the form
4136 @samp{@var{archive}(@var{member})}, the following algorithm is run twice,
4137 first using @samp{(@var{member})} as the target @var{t}, and second using
4138 the entire target if the first run found no rule.@refill
4139
4140 @enumerate
4141 @item
4142 Split @var{t} into a directory part, called @var{d}, and the rest,
4143 called @var{n}.  For example, if @var{t} is @samp{src/foo.o}, then
4144 @var{d} is @samp{src/} and @var{n} is @samp{foo.o}.@refill
4145
4146 @item
4147 Make a list of the pattern rules whose target matches @var{t} or
4148 @var{n}.  If the target pattern contains a slash, it is matched
4149 against @var{t}; otherwise, against @var{n}.
4150
4151 @item
4152 If any rule in that list is @emph{not} a match-anything rule, then
4153 remove all nonterminal match-anything rules from the list.
4154
4155 @item
4156 Remove any rules with no dependencies from the list.
4157
4158 @item
4159 For each pattern rule in the list:
4160
4161 @enumerate
4162 @item
4163 Find the stem @var{s}: the part of @var{t} or @var{n} that the
4164 @samp{%} in the target pattern matches.@refill
4165
4166 @item
4167 Compute the dependency names by substituting @var{s} for @samp{%}; if
4168 the target pattern does not contain a slash, @var{d} is appended to
4169 the front of each dependency name.
4170
4171 @item
4172 Test whether all the dependencies exist or ought to exist.  (If a
4173 file name mentioned in the makefile as a target or as an explicit
4174 dependency then we say it ought to exist.)
4175
4176 If all dependencies exist or ought to exist, then this rule applies.
4177 @end enumerate
4178
4179 @item
4180 If no pattern rule has been found so far, try harder.  For
4181 each pattern rule in the list:
4182
4183 @enumerate
4184 @item
4185 If the rule is a terminal match-anything rule, ignore it and go
4186 on to the next rule.
4187
4188 @item
4189 Compute the dependency names as before.
4190
4191 @item
4192 Test whether all the dependencies exist or ought to exist.
4193
4194 @item
4195 For each dependency that does not exist, follow this algorithm
4196 recursively to see if the dependency can be made by an implicit
4197 rule.
4198
4199 @item
4200 If all dependencies exist, ought to exist, or can be made by
4201 implicit rules, then this rule applies.
4202 @end enumerate
4203
4204 @item
4205 If no rule has been found so far, this target cannot be made by an
4206 implicit rule.  Return failure.
4207
4208 @item
4209 If no implicit rule applies, the rule for @code{.DEFAULT}, if any,
4210 applies.  In that case, give @var{t} the same commands that
4211 @code{.DEFAULT} has.  Otherwise, there are no commands for @var{t}.
4212 @end enumerate
4213
4214 When the commands of a pattern rule are executed for @var{t}, the automatic
4215 variables @samp{$@@}, @samp{$*} and @samp{$<} are set as follows:
4216
4217 @table @samp
4218 @item $@@
4219 @var{t}
4220 @item $*
4221 If the target pattern contains a slash, this is @var{s}; otherwise, it
4222 is @var{d}@var{s}.
4223 @item $<
4224 The name of the first dependency that came via the implicit rule.
4225 @end table
4226
4227 For @code{.DEFAULT} commands, as for non-implicit commands, @samp{$*}
4228 and @samp{$<} are empty.  @samp{$@@} is @var{t}, as always.
4229
4230 @node Archives, Features, Implicit, Top
4231 @chapter Using @code{make} to Update Archive Files
4232 @cindex archive
4233
4234 @dfn{Archive files} are files containing named subfiles called
4235 @dfn{members}; they are maintained with the program @code{ar} and their
4236 main use is as subroutine libraries for linking.
4237
4238 @menu
4239 * Members: Archive Members.    How to name an archive member
4240                                 as a target or dependency.
4241 * Update: Archive Update.      An implicit rule can update
4242                                 most archive member targets just right.
4243 * Symbols: Archive Symbols.    Another implicit rule runs @code{ranlib}
4244                                 to update the special member @file{__.SYMDEF}.
4245 @end menu
4246
4247 @node Archive Members, Archive Update, Archives, Archives
4248 @section Archive Members as Targets
4249 @cindex archive member targets
4250
4251 An individual member of an archive file can be used as a target or
4252 dependency in @code{make}.  The archive file must already exist, but the
4253 member need not exist.  You specify the member named @var{member} in
4254 archive file @var{archive} as follows:
4255
4256 @example
4257 @var{archive}(@var{member})
4258 @end example
4259
4260 @noindent
4261 This construct is available only in targets and dependencies, not in
4262 commands!  Most programs that you might use in commands do not support this
4263 syntax and cannot act directly on archive members.  Only @code{ar} and
4264 other programs specifically designed to operate on archives can do so.
4265 Therefore, valid commands to update an archive member target probably must
4266 use @code{ar}.  For example, this rule says to create a member
4267 @file{hack.o} in archive @file{foolib} by copying the file @file{hack.o}:
4268
4269 @example
4270 foolib(hack.o) : hack.o
4271         ar r foolib hack.o
4272 @end example
4273
4274 In fact, nearly all archive member targets are updated in just this way
4275 and there is an implicit rule to do it for you.
4276
4277 @node Archive Update, Archive Symbols, Archive Members, Archives
4278 @section Implicit Rule for Archive Member Targets
4279
4280 Recall that a target that looks like @file{@var{a}(@var{m})} stands for the
4281 member named @var{m} in the archive file @var{a}.
4282
4283 When @code{make} looks for an implicit rule for such a target, as a special
4284 feature it considers implicit rules that match @file{(@var{m})}, as well as
4285 those that match the actual target @file{@var{a}(@var{m})}.
4286
4287 This causes one special rule whose target is @file{(%)} to match.  This
4288 rule updates the target @file{@var{a}(@var{m})} by copying the file @var{m}
4289 into the archive.  For example, it will update the archive member target
4290 @file{foo.a(bar.o)} by copying the @emph{file} @file{bar.o} into the
4291 archive @file{foo.a} as a member named @file{bar.o}.
4292
4293 When this rule is chained with others, the result is very powerful.  Thus,
4294 @samp{make "foo.a(bar.o)"} in the presence of a file @file{bar.c} is enough
4295 to cause the following commands to be run, even without a makefile:
4296
4297 @example
4298 cc -c bar.c -o bar.o
4299 ar r foo.a bar.o
4300 rm -f bar.o
4301 @end example
4302
4303 @noindent
4304 Here the file @file{bar.o} has been envisioned as an intermediate file.
4305
4306 @node Archive Symbols,, Archive Update, Archives
4307 @subsection Updating Archive Symbol Directories
4308 @cindex __.SYMDEF
4309
4310 An archive file that is used as a library usually contains a special member
4311 named @file{__.SYMDEF} that contains a directory of the external symbol
4312 names defined by all the other members.  After you update any other
4313 members, you need to update @file{__.SYMDEF} so that it will summarize the
4314 other members properly.  This is done by running the @code{ranlib} program:
4315
4316 @example
4317 ranlib @var{archivefile}
4318 @end example
4319
4320 Normally you would put this command in the rule for the archive file,
4321 and make all the members of the archive file dependents of that rule.
4322 For example,
4323
4324 @example
4325 libfoo.a: libfoo.a(x.o) libfoo.a(y.o) @dots{}
4326         ranlib libfoo.a
4327 @end example
4328
4329 @noindent
4330 The effect of this is to update archive members @file{x.o}, @file{y.o},
4331 etc., and then update the symbol directory member @file{__.SYMDEF} by
4332 running @code{ranlib}.  The rules for updating the members are not shown
4333 here; most likely you can omit them and use the implicit rule which copies
4334 files into the archive, as described in the preceding section.
4335
4336 @node Features, Missing, Archives, Top
4337 @chapter Features of GNU @code{make}
4338
4339 GNU @code{make} contains many features not in any other @code{make}
4340 program.  Some of them are taken from other versions of @code{make}, while
4341 most are new inventions of the implementors of GNU @code{make}.
4342
4343 The standard for comparison among versions of @code{make} in this chapter
4344 will be the version found in standard 4.2 BSD Unix systems.  Many additions
4345 come from the versions of @code{make} found in other Unix systems.  This
4346 chapter lists the features of GNU @code{make} beyond the 4.2 BSD version.
4347 They are presented as a simple list of small items with terse descriptions.
4348 All of these features (as well as those found in 4.2 BSD @code{make}) are
4349 documented in this manual, so it would be redundant to repeat that
4350 documentation here.
4351
4352 The @code{VPATH} variable and its special meaning come from the @code{make}
4353 in 4.3 BSD Unix.  @xref{Directory Search}.
4354
4355 Many features come from the version of @code{make} in Sun Unix systems.  I
4356 believe that these in turn are adopted from the @code{make} in Unix System V.
4357
4358 @itemize @bullet
4359 @item
4360 Included makefiles.  @xref{Include}.
4361
4362 @item
4363 Variables are read in from the environment.
4364 All variables are placed into the environment of child processes (running
4365 commands).  @xref{Environment}.  Note also the @samp{-e} option.
4366
4367 @item
4368 The environment variable @samp{MAKEFLAGS} is scanned for command-line
4369 options to @code{make}.  The options @samp{-f}, @samp{-p}, @samp{-d} and
4370 @samp{-c} are not accepted.  The @code{make} variable @samp{MAKEFLAGS} is
4371 set to a list of the options @code{make} was invoked with, except those
4372 noted above.  @xref{Options/Recursion}.@refill
4373
4374 @item
4375 The automatic variable @code{$%} is set to the member name
4376 in an archive reference.  @xref{Automatic}.
4377
4378 @item
4379 The automatic variables @code{$@@}, @code{$*}, @code{$<} and @code{$%} have
4380 corresponding forms such as @code{$(@@F)} and @code{$(@@D)} which with only
4381 the filename and only the directory.  @xref{Automatic}.@refill
4382
4383 @item
4384 Substitution variable references.  @xref{Reference}.
4385
4386 @item
4387 The command-line options @samp{-b} and @samp{-m} are accepted and ignored.
4388
4389 @item
4390 Targets whose commands contain a reference to the variable @code{MAKE} have
4391 their commands executed even if the @samp{-n}, @samp{-q} or @samp{-t}
4392 options are specified.  @xref{Recursion}.  I'm told that Unix System V
4393 @code{make} does this only in the case of @samp{-n}.@refill
4394
4395 @item
4396 An implicit suffix rule @samp{@var{x}.a:} makes
4397 @file{@var{lib}(@var{name}.o)} from @file{@var{name}.@var{x}}.  In GNU
4398 @code{make}, this is actually implemented by using one pattern rule for
4399 making library-archive files and rule-chaining.  @xref{Chained Rules}.@refill
4400 @end itemize
4401
4402 The Sun Unix (and probably System V) version of @code{make} fails to
4403 support variable references using braces (@samp{@{} and @samp{@}}) rather
4404 than parantheses (@pxref{Reference}), and to set the @code{MFLAGS} variable
4405 to the list of options (the same list as in @code{MAKEFLAGS}).@refill
4406
4407 The remaining new features are inventions new to GNU @code{make}.
4408
4409 @itemize @bullet
4410 @item
4411 The arrangement of lines and backslash-newline combinations in
4412 commands is retained when the commands are printed, so they
4413 appear as they do in the makefile, except for the stripping of
4414 initial whitespace.
4415 This has been implemented in several versions of @code{make}.
4416 We're not sure who invented it first, but it's been spread around a bit.
4417 The @samp{-v} option prints version and copyright information.
4418 Rule chaining and implicit intermediate files.
4419 we got this from either of them or thought it up ourselves at the
4420 The automatic variable @code{$^} containing a list of all dependencies
4421 of the current target.  @xref{Automatic}.
4422 @end itemize
4423 The variable @code{MAKEOVERRIDES} is defined to contain all
4424 the variable definitions given on the command line, and is
4425 appended to the @code{MAKE} variable which contains the name
4426 by which @code{make} was invoked.  Thus, sub-makes will always
4427 get variable definitions from the command line that cannot
4428 come from the environment since environment variable
4429 definitions do not override those in makefiles.
4430 @itemize @bullet
4431 @item
4432 The @samp{-c} to change directory.  @xref{Options}.
4433
4434 @item
4435 Simply-expanded variables.  @xref{Flavors}.
4436
4437 @item
4438 Phony targets.  @xref{Phony Targets}.
4439 @xref{Recursion}.
4440
4441 Variable expansion functions.  @xref{Functions}.
4442 The @samp{-c} command option to change directory.  @xref{Options}.
4443
4444 The @samp{-o} option makes files seem artificially old.
4445 Verbatim variable definitions made with @code{define}.  @xref{Defining}.
4446
4447 @item
4448 Conditional lines.  @xref{Conditionals}.
4449 A similar feature with a different syntax was implemented by
4450 Andrew Hume of AT&T Bell Labs in his @code{mk} program.  This
4451 Included makefiles never determine the default target.
4452
4453 @item
4454 There is an include file search path.  @xref{Include}.
4455
4456 @item
4457 @xref{MAKEFILES Variable}.
4458 @xref{Avoid Compilation}.
4459
4460 @xref{Pattern Rules}.
4461 @code{make}; it seems a natural extension derived from the features
4462 of the C preprocessor and similar macro languages and is not a
4463 The automatic variable @code{$^} contains a list of all
4464 dependencies of the current target.  @xref{Automatic}.
4465 @item
4466 Included makefiles never determine the default goal.
4467 There is a fully complete default set of implicit rules using
4468 file with suffixes @samp{.out}, @samp{.a}, @samp{.o},
4469 @samp{.s}, @samp{.c}, @samp{.f}, @samp{.p}, @samp{.F},
4470 @samp{.e}, @samp{.r}, @samp{.y}, @samp{.ye}, @samp{.yr} and
4471 @samp{.l}.  @xref{Catalogue of Rules}.@refill
4472
4473 @item
4474 Leading sequences of @samp{./} are stripped from file names,
4475 so that @file{./@var{file}} and @file{@var{file}} are
4476 considered to be the same file.
4477 @item
4478 Stripping leading sequences of @samp{./} from file names, so that
4479 Dependencies of the form @samp{-l@var{name}} are searched for
4480 as library archives.  @xref{Libraries/Search}.
4481 same file.
4482
4483 Suffixes for suffix rules (@pxref{Suffix Rules}) may contain
4484 any characters.  In other version of @code{make}, they must
4485 begin with @samp{.} and not contain any @samp{/} characters.
4486
4487 @item
4488 The variable @code{MAKELEVEL} keeps track of the current level
4489 of @code{make} recursion.  At the top level (@code{MAKELEVEL}
4490 is zero), the @code{SHELL} environment variable is not used to
4491 execute commands.  It is reset to @samp{/bin/sh}.@refill
4492
4493 @item
4494 Intermediate implicit files.  @xref{Chained Rules}.
4495 Special search method for library dependencies written in the form
4496 @samp{-l@var{name}}.  @xref{Libraries/Search}.
4497
4498 @samp{.} and not contain any @samp{/} characters.
4499
4500 @item
4501 The variable @code{MAKELEVEL} which keeps track of the current level
4502 of @code{make} recursion.  @xref{Recursion}.
4503 The @code{make} programs in various other systems support three features
4504 @item
4505 Static pattern rules.  @xref{Static Pattern}.
4506
4507 @item
4508 Selective @code{vpath} search.  @xref{Directory Search}.
4509
4510 @item
4511 Recursive variable references.  @xref{Reference}.
4512 @end itemize
4513
4514 @node Missing, Concept Index, Features, Top
4515 @chapter Missing Features in GNU @code{make}
4516
4517 The @code{make} programs in various other systems support a few features
4518 that are not implemented in GNU @code{make}.
4519
4520 @itemize @bullet
4521 @item
4522 A target of the form @samp{@var{file}((@var{entry}))} stands for a member
4523 of archive file @var{file}.  The member is chosen, not by name, but by
4524 being an object file which defines the linker symbol @var{entry}.@refill
4525
4526 This feature was not put into GNU @code{make} because of the
4527 nonmodularity of putting knowledge into @code{make} of the internal
4528 format of archive file symbol directories.  @xref{Archive Symbols}.
4529
4530 @item
4531 Suffixes (used in suffix rules) that end with the character @samp{~}
4532 have a special meaning; they refer to the SCCS file that corresponds
4533 to the file one would get without the @samp{~}.  For example, the
4534 suffix rule @samp{.c~.o} would make the file @file{@var{n}.o} file from
4535 the SCCS file @file{s.@var{n}.c}.  For complete coverage, a whole
4536 series of such suffix rules is required.  @xref{Suffix Rules}.@refill
4537 rules.  The System V @code{make} rule:
4538 In GNU @code{make}, this entire series of cases is handled by two
4539 pattern rules for extraction from SCCS, in combination with the
4540 general feature of rule chaining.  @xref{Chained Rules}.
4541
4542 @item
4543 In System V @code{make}, the string @samp{$$@@} has the strange meaning
4544 that, in the dependencies of a rule with multiple targets, it stands
4545 for the particular target that is being processed.
4546
4547 This is not defined in GNU @code{make} because @samp{$$} should always
4548 stand for an ordinary @samp{$}.
4549 @noindent
4550 can be replaced with the GNU @code{make} static pattern rule:
4551
4552 @example
4553 $(targets): %: %.o lib.a
4554 @end example
4555
4556 @item
4557 In System V and 4.3 BSD @code{make}, files found by @code{VPATH} search
4558 (@pxref{Directory Search}) have their names changed inside command strings.
4559 We feel it is much cleaner to always use automatic variables and thus
4560 obviate the need for this feature.  We are still debating whether to
4561 implement this to be compatible or to leave it out to avoid such ugliness.
4562 @end itemize
4563
4564 @node Concept Index, Name Index, Missing, Top
4565 @unnumbered Index of Concepts
4566
4567 @printindex cp
4568
4569 @node Name Index,, Concept Index, Top
4570 @unnumbered Index of Functions, Directives and Variables
4571
4572 @printindex fn
4573
4574 @contents
4575 @bye