* Some final cleanups, and release 3.79.
[platform/upstream/make.git] / make.texinfo
1 \input texinfo                @c -*- Texinfo -*-
2 @c %**start of header
3 @setfilename make.info
4 @settitle GNU @code{make}
5 @setchapternewpage odd
6 @c %**end of header
7
8 @c FSF publishers: format makebook.texi instead of using this file directly.
9
10 @set RCSID $Id$
11 @set EDITION 0.55
12 @set VERSION 3.79
13 @set UPDATED 04 April 2000
14 @set UPDATE-MONTH April 2000
15 @comment The ISBN number might need to change on next publication.
16 @set ISBN 1-882114-80-9 @c CHANGE THIS BEFORE PRINTING AGAIN! --psmith 16jul98
17
18 @c finalout
19
20 @c ISPELL CHECK: done, 10 June 1993 --roland
21
22 @c Combine the variable and function indices:
23 @syncodeindex vr fn
24 @c Combine the program and concept indices:
25 @syncodeindex pg cp
26
27 @dircategory GNU Packages
28 @direntry
29 * Make: (make).            Remake files automatically.
30 @end direntry
31
32 @ifinfo
33 This file documents the GNU Make utility, which determines
34 automatically which pieces of a large program need to be recompiled,
35 and issues the commands to recompile them.
36
37 This is Edition @value{EDITION}, last updated @value{UPDATED},
38 of @cite{The GNU Make Manual}, for @code{make}, Version @value{VERSION}.
39
40 Copyright (C) 1988, '89, '90, '91, '92, '93, '94, '95, '96, '97, '98, '99, 2000
41         Free Software Foundation, Inc.
42
43 Permission is granted to make and distribute verbatim copies of
44 this manual provided the copyright notice and this permission notice
45 are preserved on all copies.
46
47 @ignore
48 Permission is granted to process this file through TeX and print the
49 results, provided the printed document carries copying permission
50 notice identical to this one except for the removal of this paragraph
51 (this paragraph not being relevant to the printed manual).
52
53 @end ignore
54 Permission is granted to copy and distribute modified versions of this
55 manual under the conditions for verbatim copying, provided that the entire
56 resulting derived work is distributed under the terms of a permission
57 notice identical to this one.
58
59 Permission is granted to copy and distribute translations of this manual
60 into another language, under the above conditions for modified versions,
61 except that this permission notice may be stated in a translation approved
62 by the Free Software Foundation.
63 @end ifinfo
64
65 @iftex
66 @shorttitlepage GNU Make
67 @end iftex
68 @titlepage
69 @title GNU Make
70 @subtitle A Program for Directing Recompilation
71 @subtitle GNU @code{make} Version @value{VERSION}
72 @subtitle @value{UPDATE-MONTH}
73 @author Richard M. Stallman and Roland McGrath
74 @page
75 @vskip 0pt plus 1filll
76 Copyright @copyright{} 1988, '89, '90, '91, '92, '93, '94, '95, '96, '97, '98, '99, 2000
77 Free Software Foundation, Inc.
78 @sp 2
79 Published by the Free Software Foundation @*
80 59 Temple Place -- Suite 330, @*
81 Boston, MA 02111-1307 USA @*
82 ISBN @value{ISBN} @*
83
84 Maintenance and updates since Version 3.76 by Paul D. Smith.
85
86 Permission is granted to make and distribute verbatim copies of
87 this manual provided the copyright notice and this permission notice
88 are preserved on all copies.
89
90 Permission is granted to copy and distribute modified versions of this
91 manual under the conditions for verbatim copying, provided that the entire
92 resulting derived work is distributed under the terms of a permission
93 notice identical to this one.
94
95 Permission is granted to copy and distribute translations of this manual
96 into another language, under the above conditions for modified versions,
97 except that this permission notice may be stated in a translation approved
98 by the Free Software Foundation.
99 @sp 2
100 Cover art by Etienne Suvasa.
101 @end titlepage
102 @page
103
104 @ifinfo
105 @node Top, Overview, , (dir)
106 @top Make
107
108 The GNU @code{make} utility automatically determines which pieces of a
109 large program need to be recompiled, and issues the commands to
110 recompile them.@refill
111
112 This edition of the @cite{GNU Make Manual},
113 last updated @value{UPDATED},
114 documents GNU @code{make} Version @value{VERSION}.@refill
115
116 This manual describes @code{make} and contains the following chapters:@refill
117 @end ifinfo
118
119 @menu
120 * Overview::                    Overview of @code{make}.
121 * Introduction::                An introduction to @code{make}.
122 * Makefiles::                   Makefiles tell @code{make} what to do.
123 * Rules::                       Rules describe when a file must be remade.
124 * Commands::                    Commands say how to remake a file.
125 * Using Variables::             You can use variables to avoid repetition.
126 * Conditionals::                Use or ignore parts of the makefile based
127                                  on the values of variables.
128 * Functions::                   Many powerful ways to manipulate text.
129 * Invoking make: Running.       How to invoke @code{make} on the command line.
130 * Implicit Rules::              Use implicit rules to treat many files alike,
131                                  based on their file names.
132 * Archives::                    How @code{make} can update library archives.
133 * Features::                    Features GNU @code{make} has over other @code{make}s.
134 * Missing::                     What GNU @code{make} lacks from other @code{make}s.
135 * Makefile Conventions::        Conventions for makefiles in GNU programs.
136 * Quick Reference::             A quick reference for experienced users.
137 * Error Messages::              A list of common errors generated by @code{make}.
138 * Complex Makefile::            A real example of a straightforward,
139                                  but nontrivial, makefile.
140 * Concept Index::               Index of Concepts
141 * Name Index::                  Index of Functions, Variables, & Directives
142
143  --- The Detailed Node Listing ---
144
145 Overview of @code{make}
146
147 * Preparing::                   Preparing and Running Make
148 * Reading::                     On Reading this Text
149 * Bugs::                        Problems and Bugs
150
151 An Introduction to Makefiles
152
153 * Rule Introduction::           What a rule looks like.
154 * Simple Makefile::             A Simple Makefile
155 * How Make Works::              How @code{make} Processes This Makefile
156 * Variables Simplify::          Variables Make Makefiles Simpler
157 * make Deduces::                Letting @code{make} Deduce the Commands
158 * Combine By Prerequisite::     Another Style of Makefile
159 * Cleanup::                     Rules for Cleaning the Directory
160
161 Writing Makefiles
162
163 * Makefile Contents::           What makefiles contain.
164 * Makefile Names::              How to name your makefile.
165 * Include::                     How one makefile can use another makefile.
166 * MAKEFILES Variable::          The environment can specify extra makefiles.
167 * Remaking Makefiles::          How makefiles get remade.
168 * Overriding Makefiles::        How to override part of one makefile
169                                  with another makefile.
170 * Reading Makefiles::           How makefiles are parsed.
171
172 Writing Rules
173
174 * Rule Example::                An example explained.
175 * Rule Syntax::                 General syntax explained.
176 * Wildcards::                   Using wildcard characters such as `*'.
177 * Directory Search::            Searching other directories for source files.
178 * Phony Targets::               Using a target that is not a real file's name.
179 * Force Targets::               You can use a target without commands
180                                   or prerequisites to mark other
181                                   targets as phony.
182 * Empty Targets::               When only the date matters and the
183                                   files are empty.
184 * Special Targets::             Targets with special built-in meanings.
185 * Multiple Targets::            When to make use of several targets in a rule.
186 * Multiple Rules::              How to use several rules with the same target.
187 * Static Pattern::              Static pattern rules apply to multiple targets
188                                   and can vary the prerequisites according to
189                                   the target name.
190 * Double-Colon::                How to use a special kind of rule to allow
191                                   several independent rules for one target.
192 * Automatic Prerequisites::     How to automatically generate rules giving
193                                  prerequisites from source files themselves.
194
195 Using Wildcard Characters in File Names
196
197 * Wildcard Examples::           Several examples
198 * Wildcard Pitfall::            Problems to avoid.
199 * Wildcard Function::           How to cause wildcard expansion where
200                                   it does not normally take place.
201
202 Searching Directories for Prerequisites
203
204 * General Search::              Specifying a search path that applies
205                                   to every prerequisite.
206 * Selective Search::            Specifying a search path
207                                   for a specified class of names.
208 * Search Algorithm::            When and how search paths are applied.
209 * Commands/Search::             How to write shell commands that work together
210                                   with search paths.
211 * Implicit/Search::             How search paths affect implicit rules.
212 * Libraries/Search::            Directory search for link libraries.
213
214 Static Pattern Rules
215
216 * Static Usage::                The syntax of static pattern rules.
217 * Static versus Implicit::      When are they better than implicit rules?
218
219 Writing the Commands in Rules
220
221 * Echoing::                     How to control when commands are echoed.
222 * Execution::                   How commands are executed.
223 * Parallel::                    How commands can be executed in parallel.
224 * Errors::                      What happens after a command execution error.
225 * Interrupts::                  What happens when a command is interrupted.
226 * Recursion::                   Invoking @code{make} from makefiles.
227 * Sequences::                   Defining canned sequences of commands.
228 * Empty Commands::              Defining useful, do-nothing commands.
229
230 Recursive Use of @code{make}
231
232 * MAKE Variable::               The special effects of using @samp{$(MAKE)}.
233 * Variables/Recursion::         How to communicate variables to a sub-@code{make}.
234 * Options/Recursion::           How to communicate options to a sub-@code{make}.
235 * -w Option::                   How the @samp{-w} or @samp{--print-directory} option
236                                  helps debug use of recursive @code{make} commands.
237
238 How to Use Variables
239
240 * Reference::                   How to use the value of a variable.
241 * Flavors::                     Variables come in two flavors.
242 * Advanced::                    Advanced features for referencing a variable.
243 * Values::                      All the ways variables get their values.
244 * Setting::                     How to set a variable in the makefile.
245 * Appending::                   How to append more text to the old value
246                                   of a variable.
247 * Override Directive::          How to set a variable in the makefile even if
248                                   the user has set it with a command argument.
249 * Defining::                    An alternate way to set a variable
250                                   to a verbatim string.
251 * Environment::                 Variable values can come from the environment.
252 * Automatic::                   Some special variables have predefined
253                                   meanings for use with implicit rules.
254
255 Advanced Features for Reference to Variables
256
257 * Substitution Refs::           Referencing a variable with
258                                   substitutions on the value.
259 * Computed Names::              Computing the name of the variable to refer to.
260
261 Conditional Parts of Makefiles
262
263 * Conditional Example::         Example of a conditional
264 * Conditional Syntax::          The syntax of conditionals.
265 * Testing Flags::               Conditionals that test flags.
266
267 Functions for Transforming Text
268
269 * Syntax of Functions::         How to write a function call.
270 * Text Functions::              General-purpose text manipulation functions.
271 * File Name Functions::         Functions for manipulating file names.
272 * Foreach Function::            Repeat some text with controlled variation.
273 * Call Function::               Expand a user-defined function.
274 * Origin Function::             Find where a variable got its value.
275 * Shell Function::              Substitute the output of a shell command.
276
277 How to Run @code{make}
278
279 * Makefile Arguments::          How to specify which makefile to use.
280 * Goals::                       How to use goal arguments to specify which
281                                   parts of the makefile to use.
282 * Instead of Execution::        How to use mode flags to specify what
283                                   kind of thing to do with the commands
284                                   in the makefile other than simply
285                                   execute them.
286 * Avoiding Compilation::        How to avoid recompiling certain files.
287 * Overriding::                  How to override a variable to specify
288                                   an alternate compiler and other things.
289 * Testing::                     How to proceed past some errors, to
290                                   test compilation.
291 * Options Summary::             Summary of Options
292
293 Using Implicit Rules
294
295 * Using Implicit::              How to use an existing implicit rule
296                                   to get the commands for updating a file.
297 * Catalogue of Rules::          A list of built-in implicit rules.
298 * Implicit Variables::          How to change what predefined rules do.
299 * Chained Rules::               How to use a chain of implicit rules.
300 * Pattern Rules::               How to define new implicit rules.
301 * Last Resort::                 How to defining commands for rules
302                                   which cannot find any.
303 * Suffix Rules::                The old-fashioned style of implicit rule.
304 * Implicit Rule Search::        The precise algorithm for applying
305                                   implicit rules.
306
307 Defining and Redefining Pattern Rules
308
309 * Pattern Intro::               An introduction to pattern rules.
310 * Pattern Examples::            Examples of pattern rules.
311 * Automatic::                   How to use automatic variables in the
312                                   commands of implicit rules.
313 * Pattern Match::               How patterns match.
314 * Match-Anything Rules::        Precautions you should take prior to
315                                   defining rules that can match any
316                                   target file whatever.
317 * Canceling Rules::             How to override or cancel built-in rules.
318
319 Using @code{make} to Update Archive Files
320
321 * Archive Members::             Archive members as targets.
322 * Archive Update::              The implicit rule for archive member targets.
323 * Archive Pitfalls::            Dangers to watch out for when using archives.
324 * Archive Suffix Rules::        You can write a special kind of suffix rule
325                                   for updating archives.
326
327 Implicit Rule for Archive Member Targets
328
329 * Archive Symbols::             How to update archive symbol directories.
330 @end menu
331
332 @node Overview, Introduction, Top, Top
333 @comment  node-name,  next,  previous,  up
334 @chapter Overview of @code{make}
335
336 The @code{make} utility automatically determines which pieces of a large
337 program need to be recompiled, and issues commands to recompile them.
338 This manual describes GNU @code{make}, which was implemented by Richard
339 Stallman and Roland McGrath.  Development since Version 3.76 has been
340 handled by Paul D. Smith.
341
342 GNU @code{make} conforms to section 6.2 of @cite{IEEE Standard
343 1003.2-1992} (POSIX.2).
344 @cindex POSIX
345 @cindex IEEE Standard 1003.2
346 @cindex standards conformance
347
348 Our examples show C programs, since they are most common, but you can use
349 @code{make} with any programming language whose compiler can be run with a
350 shell command.  Indeed, @code{make} is not limited to programs.  You can
351 use it to describe any task where some files must be updated automatically
352 from others whenever the others change.
353
354 @menu
355 * Preparing::                   Preparing and Running Make
356 * Reading::                     On Reading this Text
357 * Bugs::                        Problems and Bugs
358 @end menu
359
360 @node Preparing, Reading,  , Overview
361 @ifinfo
362 @heading Preparing and Running Make
363 @end ifinfo
364
365 To prepare to use @code{make}, you must write a file called
366 the @dfn{makefile} that describes the relationships among files
367 in your program and provides commands for updating each file.
368 In a program, typically, the executable file is updated from object
369 files, which are in turn made by compiling source files.@refill
370
371 Once a suitable makefile exists, each time you change some source files,
372 this simple shell command:
373
374 @example
375 make
376 @end example
377
378 @noindent
379 suffices to perform all necessary recompilations.  The @code{make} program
380 uses the makefile data base and the last-modification times of the files to
381 decide which of the files need to be updated.  For each of those files, it
382 issues the commands recorded in the data base.
383
384 You can provide command line arguments to @code{make} to control which
385 files should be recompiled, or how.  @xref{Running, ,How to Run
386 @code{make}}.
387
388 @node Reading, Bugs, Preparing, Overview
389 @section How to Read This Manual
390
391 If you are new to @code{make}, or are looking for a general
392 introduction, read the first few sections of each chapter, skipping the
393 later sections.  In each chapter, the first few sections contain
394 introductory or general information and the later sections contain
395 specialized or technical information.
396 @ifinfo
397 The exception is the second chapter, @ref{Introduction, ,An
398 Introduction to Makefiles}, all of which is introductory.
399 @end ifinfo
400 @iftex
401 The exception is @ref{Introduction, ,An Introduction to Makefiles},
402 all of which is introductory.
403 @end iftex
404
405 If you are familiar with other @code{make} programs, see @ref{Features,
406 ,Features of GNU @code{make}}, which lists the enhancements GNU
407 @code{make} has, and @ref{Missing, ,Incompatibilities and Missing
408 Features}, which explains the few things GNU @code{make} lacks that
409 others have.
410
411 For a quick summary, see @ref{Options Summary}, @ref{Quick Reference},
412 and @ref{Special Targets}.
413
414 @node Bugs,  , Reading, Overview
415 @section Problems and Bugs
416 @cindex reporting bugs
417 @cindex bugs, reporting
418 @cindex problems and bugs, reporting
419
420 If you have problems with GNU @code{make} or think you've found a bug,
421 please report it to the developers; we cannot promise to do anything but
422 we might well want to fix it.
423
424 Before reporting a bug, make sure you've actually found a real bug.
425 Carefully reread the documentation and see if it really says you can do
426 what you're trying to do.  If it's not clear whether you should be able
427 to do something or not, report that too; it's a bug in the
428 documentation!
429
430 Before reporting a bug or trying to fix it yourself, try to isolate it
431 to the smallest possible makefile that reproduces the problem.  Then
432 send us the makefile and the exact results @code{make} gave you.  Also
433 say what you expected to occur; this will help us decide whether the
434 problem was really in the documentation.
435
436 Once you've got a precise problem, please send electronic mail to:
437
438 @example
439     bug-make@@gnu.org
440 @end example
441
442 @noindent
443 Please include the version number of @code{make} you are using.  You can
444 get this information with the command @samp{make --version}.
445 Be sure also to include the type of machine and operating system you are
446 using.  If possible, include the contents of the file @file{config.h}
447 that is generated by the configuration process.
448
449 @node Introduction, Makefiles, Overview, Top
450 @comment  node-name,  next,  previous,  up
451 @chapter An Introduction to Makefiles
452
453 You need a file called a @dfn{makefile} to tell @code{make} what to do.
454 Most often, the makefile tells @code{make} how to compile and link a
455 program.
456 @cindex makefile
457
458 In this chapter, we will discuss a simple makefile that describes how to
459 compile and link a text editor which consists of eight C source files
460 and three header files.  The makefile can also tell @code{make} how to
461 run miscellaneous commands when explicitly asked (for example, to remove
462 certain files as a clean-up operation).  To see a more complex example
463 of a makefile, see @ref{Complex Makefile}.
464
465 When @code{make} recompiles the editor, each changed C source file
466 must be recompiled.  If a header file has changed, each C source file
467 that includes the header file must be recompiled to be safe.  Each
468 compilation produces an object file corresponding to the source file.
469 Finally, if any source file has been recompiled, all the object files,
470 whether newly made or saved from previous compilations, must be linked
471 together to produce the new executable editor.
472 @cindex recompilation
473 @cindex editor
474
475 @menu
476 * Rule Introduction::           What a rule looks like.
477 * Simple Makefile::             A Simple Makefile
478 * How Make Works::              How @code{make} Processes This Makefile
479 * Variables Simplify::          Variables Make Makefiles Simpler
480 * make Deduces::                Letting @code{make} Deduce the Commands
481 * Combine By Prerequisite::     Another Style of Makefile
482 * Cleanup::                     Rules for Cleaning the Directory
483 @end menu
484
485 @node Rule Introduction, Simple Makefile,  , Introduction
486 @comment  node-name,  next,  previous,  up
487 @section What a Rule Looks Like
488 @cindex rule, introduction to
489 @cindex makefile rule parts
490 @cindex parts of makefile rule
491
492 A simple makefile consists of ``rules'' with the following shape:
493
494 @cindex targets, introduction to
495 @cindex prerequisites, introduction to
496 @cindex commands, introduction to
497 @example
498 @group
499 @var{target} @dots{} : @var{prerequisites} @dots{}
500         @var{command}
501         @dots{}
502         @dots{}
503 @end group
504 @end example
505
506 A @dfn{target} is usually the name of a file that is generated by a
507 program; examples of targets are executable or object files.  A target
508 can also be the name of an action to carry out, such as @samp{clean}
509 (@pxref{Phony Targets}).
510
511 A @dfn{prerequisite} is a file that is used as input to create the
512 target.  A target often depends on several files.
513
514 @cindex tabs in rules
515 A @dfn{command} is an action that @code{make} carries out.
516 A rule may have more than one command, each on its own line.
517 @strong{Please note:} you need to put a tab character at the beginning of
518 every command line!  This is an obscurity that catches the unwary.
519
520 Usually a command is in a rule with prerequisites and serves to create a
521 target file if any of the prerequisites change.  However, the rule that
522 specifies commands for the target need not have prerequisites.  For
523 example, the rule containing the delete command associated with the
524 target @samp{clean} does not have prerequisites.
525
526 A @dfn{rule}, then, explains how and when to remake certain files
527 which are the targets of the particular rule.  @code{make} carries out
528 the commands on the prerequisites to create or update the target.  A
529 rule can also explain how and when to carry out an action.
530 @xref{Rules, , Writing Rules}.
531
532 A makefile may contain other text besides rules, but a simple makefile
533 need only contain rules.  Rules may look somewhat more complicated
534 than shown in this template, but all fit the pattern more or less.
535
536 @node Simple Makefile, How Make Works, Rule Introduction, Introduction
537 @section A Simple Makefile
538 @cindex simple makefile
539 @cindex makefile, simple
540
541 Here is a straightforward makefile that describes the way an
542 executable file called @code{edit} depends on eight object files
543 which, in turn, depend on eight C source and three header files.
544
545 In this example, all the C files include @file{defs.h}, but only those
546 defining editing commands include @file{command.h}, and only low
547 level files that change the editor buffer include @file{buffer.h}.
548
549 @example
550 @group
551 edit : main.o kbd.o command.o display.o \
552        insert.o search.o files.o utils.o
553         cc -o edit main.o kbd.o command.o display.o \
554                    insert.o search.o files.o utils.o
555
556 main.o : main.c defs.h
557         cc -c main.c
558 kbd.o : kbd.c defs.h command.h
559         cc -c kbd.c
560 command.o : command.c defs.h command.h
561         cc -c command.c
562 display.o : display.c defs.h buffer.h
563         cc -c display.c
564 insert.o : insert.c defs.h buffer.h
565         cc -c insert.c
566 search.o : search.c defs.h buffer.h
567         cc -c search.c
568 files.o : files.c defs.h buffer.h command.h
569         cc -c files.c
570 utils.o : utils.c defs.h
571         cc -c utils.c
572 clean :
573         rm edit main.o kbd.o command.o display.o \
574            insert.o search.o files.o utils.o
575 @end group
576 @end example
577
578 @noindent
579 We split each long line into two lines using backslash-newline; this is
580 like using one long line, but is easier to read.
581 @cindex continuation lines
582 @cindex @code{\} (backslash), for continuation lines
583 @cindex backslash (@code{\}), for continuation lines
584 @cindex quoting newline, in makefile
585 @cindex newline, quoting, in makefile
586
587 To use this makefile to create the executable file called @file{edit},
588 type:
589
590 @example
591 make
592 @end example
593
594 To use this makefile to delete the executable file and all the object
595 files from the directory, type:
596
597 @example
598 make clean
599 @end example
600
601 In the example makefile, the targets include the executable file
602 @samp{edit}, and the object files @samp{main.o} and @samp{kbd.o}.  The
603 prerequisites are files such as @samp{main.c} and @samp{defs.h}.
604 In fact, each @samp{.o} file is both a target and a prerequisite.
605 Commands include @w{@samp{cc -c main.c}} and @w{@samp{cc -c kbd.c}}.
606
607 When a target is a file, it needs to be recompiled or relinked if any
608 of its prerequisites change.  In addition, any prerequisites that are
609 themselves automatically generated should be updated first.  In this
610 example, @file{edit} depends on each of the eight object files; the
611 object file @file{main.o} depends on the source file @file{main.c} and
612 on the header file @file{defs.h}.
613
614 A shell command follows each line that contains a target and
615 prerequisites.  These shell commands say how to update the target file.
616 A tab character must come at the beginning of every command line to
617 distinguish commands lines from other lines in the makefile.  (Bear in
618 mind that @code{make} does not know anything about how the commands
619 work.  It is up to you to supply commands that will update the target
620 file properly.  All @code{make} does is execute the commands in the rule
621 you have specified when the target file needs to be updated.)
622 @cindex shell command
623
624 The target @samp{clean} is not a file, but merely the name of an
625 action.  Since you
626 normally
627 do not want to carry out the actions in this rule, @samp{clean} is not a prerequisite of any other rule.
628 Consequently, @code{make} never does anything with it unless you tell
629 it specifically.  Note that this rule not only is not a prerequisite, it
630 also does not have any prerequisites, so the only purpose of the rule
631 is to run the specified commands.  Targets that do not refer to files
632 but are just actions are called @dfn{phony targets}.  @xref{Phony
633 Targets}, for information about this kind of target.  @xref{Errors, ,
634 Errors in Commands}, to see how to cause @code{make} to ignore errors
635 from @code{rm} or any other command.
636 @cindex @code{clean} target
637 @cindex @code{rm} (shell command)
638
639 @node How Make Works, Variables Simplify, Simple Makefile, Introduction
640 @comment  node-name,  next,  previous,  up
641 @section How @code{make} Processes a Makefile
642 @cindex processing a makefile
643 @cindex makefile, how @code{make} processes
644
645 By default, @code{make} starts with the first target (not targets whose
646 names start with @samp{.}).  This is called the @dfn{default goal}.
647 (@dfn{Goals} are the targets that @code{make} strives ultimately to
648 update.  @xref{Goals, , Arguments to Specify the Goals}.)
649 @cindex default goal
650 @cindex goal, default
651 @cindex goal
652
653 In the simple example of the previous section, the default goal is to
654 update the executable program @file{edit}; therefore, we put that rule
655 first.
656
657 Thus, when you give the command:
658
659 @example
660 make
661 @end example
662
663 @noindent
664 @code{make} reads the makefile in the current directory and begins by
665 processing the first rule.  In the example, this rule is for relinking
666 @file{edit}; but before @code{make} can fully process this rule, it
667 must process the rules for the files that @file{edit} depends on,
668 which in this case are the object files.  Each of these files is
669 processed according to its own rule.  These rules say to update each
670 @samp{.o} file by compiling its source file.  The recompilation must
671 be done if the source file, or any of the header files named as
672 prerequisites, is more recent than the object file, or if the object
673 file does not exist.
674
675 The other rules are processed because their targets appear as
676 prerequisites of the goal.  If some other rule is not depended on by the
677 goal (or anything it depends on, etc.), that rule is not processed,
678 unless you tell @code{make} to do so (with a command such as
679 @w{@code{make clean}}).
680
681 Before recompiling an object file, @code{make} considers updating its
682 prerequisites, the source file and header files.  This makefile does not
683 specify anything to be done for them---the @samp{.c} and @samp{.h} files
684 are not the targets of any rules---so @code{make} does nothing for these
685 files.  But @code{make} would update automatically generated C programs,
686 such as those made by Bison or Yacc, by their own rules at this time.
687
688 After recompiling whichever object files need it, @code{make} decides
689 whether to relink @file{edit}.  This must be done if the file
690 @file{edit} does not exist, or if any of the object files are newer than
691 it.  If an object file was just recompiled, it is now newer than
692 @file{edit}, so @file{edit} is relinked.
693 @cindex relinking
694
695 Thus, if we change the file @file{insert.c} and run @code{make},
696 @code{make} will compile that file to update @file{insert.o}, and then
697 link @file{edit}.  If we change the file @file{command.h} and run
698 @code{make}, @code{make} will recompile the object files @file{kbd.o},
699 @file{command.o} and @file{files.o} and then link the file @file{edit}.
700
701 @node Variables Simplify, make Deduces, How Make Works, Introduction
702 @section Variables Make Makefiles Simpler
703 @cindex variables
704 @cindex simplifying with variables
705
706 In our example, we had to list all the object files twice in the rule for
707 @file{edit} (repeated here):
708
709 @example
710 @group
711 edit : main.o kbd.o command.o display.o \
712               insert.o search.o files.o utils.o
713         cc -o edit main.o kbd.o command.o display.o \
714                    insert.o search.o files.o utils.o
715 @end group
716 @end example
717
718 @cindex @code{objects}
719 Such duplication is error-prone; if a new object file is added to the
720 system, we might add it to one list and forget the other.  We can eliminate
721 the risk and simplify the makefile by using a variable.  @dfn{Variables}
722 allow a text string to be defined once and substituted in multiple places
723 later (@pxref{Using Variables, ,How to Use Variables}).
724
725 @cindex @code{OBJECTS}
726 @cindex @code{objs}
727 @cindex @code{OBJS}
728 @cindex @code{obj}
729 @cindex @code{OBJ}
730 It is standard practice for every makefile to have a variable named
731 @code{objects}, @code{OBJECTS}, @code{objs}, @code{OBJS}, @code{obj},
732 or @code{OBJ} which is a list of all object file names.  We would
733 define such a variable @code{objects} with a line like this in the
734 makefile:@refill
735
736 @example
737 @group
738 objects = main.o kbd.o command.o display.o \
739           insert.o search.o files.o utils.o
740 @end group
741 @end example
742
743 @noindent
744 Then, each place we want to put a list of the object file names, we can
745 substitute the variable's value by writing @samp{$(objects)}
746 (@pxref{Using Variables, ,How to Use Variables}).
747
748 Here is how the complete simple makefile looks when you use a variable
749 for the object files:
750
751 @example
752 @group
753 objects = main.o kbd.o command.o display.o \
754           insert.o search.o files.o utils.o
755
756 edit : $(objects)
757         cc -o edit $(objects)
758 main.o : main.c defs.h
759         cc -c main.c
760 kbd.o : kbd.c defs.h command.h
761         cc -c kbd.c
762 command.o : command.c defs.h command.h
763         cc -c command.c
764 display.o : display.c defs.h buffer.h
765         cc -c display.c
766 insert.o : insert.c defs.h buffer.h
767         cc -c insert.c
768 search.o : search.c defs.h buffer.h
769         cc -c search.c
770 files.o : files.c defs.h buffer.h command.h
771         cc -c files.c
772 utils.o : utils.c defs.h
773         cc -c utils.c
774 clean :
775         rm edit $(objects)
776 @end group
777 @end example
778
779 @node make Deduces, Combine By Prerequisite, Variables Simplify, Introduction
780 @section Letting @code{make} Deduce the Commands
781 @cindex deducing commands (implicit rules)
782 @cindex implicit rule, introduction to
783 @cindex rule, implicit, introduction to
784
785 It is not necessary to spell out the commands for compiling the individual
786 C source files, because @code{make} can figure them out: it has an
787 @dfn{implicit rule} for updating a @samp{.o} file from a correspondingly
788 named @samp{.c} file using a @samp{cc -c} command.  For example, it will
789 use the command @samp{cc -c main.c -o main.o} to compile @file{main.c} into
790 @file{main.o}.  We can therefore omit the commands from the rules for the
791 object files.  @xref{Implicit Rules, ,Using Implicit Rules}.@refill
792
793 When a @samp{.c} file is used automatically in this way, it is also
794 automatically added to the list of prerequisites.  We can therefore omit
795 the @samp{.c} files from the prerequisites, provided we omit the commands.
796
797 Here is the entire example, with both of these changes, and a variable
798 @code{objects} as suggested above:
799
800 @example
801 @group
802 objects = main.o kbd.o command.o display.o \
803           insert.o search.o files.o utils.o
804
805 edit : $(objects)
806         cc -o edit $(objects)
807
808 main.o : defs.h
809 kbd.o : defs.h command.h
810 command.o : defs.h command.h
811 display.o : defs.h buffer.h
812 insert.o : defs.h buffer.h
813 search.o : defs.h buffer.h
814 files.o : defs.h buffer.h command.h
815 utils.o : defs.h
816
817 .PHONY : clean
818 clean :
819         -rm edit $(objects)
820 @end group
821 @end example
822
823 @noindent
824 This is how we would write the makefile in actual practice.  (The
825 complications associated with @samp{clean} are described elsewhere.
826 See @ref{Phony Targets}, and @ref{Errors, ,Errors in Commands}.)
827
828 Because implicit rules are so convenient, they are important.  You
829 will see them used frequently.@refill
830
831 @node Combine By Prerequisite, Cleanup, make Deduces, Introduction
832 @section Another Style of Makefile
833 @cindex combining rules by prerequisite
834
835 When the objects of a makefile are created only by implicit rules, an
836 alternative style of makefile is possible.  In this style of makefile,
837 you group entries by their prerequisites instead of by their targets.
838 Here is what one looks like:
839
840 @example
841 @group
842 objects = main.o kbd.o command.o display.o \
843           insert.o search.o files.o utils.o
844
845 edit : $(objects)
846         cc -o edit $(objects)
847
848 $(objects) : defs.h
849 kbd.o command.o files.o : command.h
850 display.o insert.o search.o files.o : buffer.h
851 @end group
852 @end example
853
854 @noindent
855 Here @file{defs.h} is given as a prerequisite of all the object files;
856 @file{command.h} and @file{buffer.h} are prerequisites of the specific
857 object files listed for them.
858
859 Whether this is better is a matter of taste: it is more compact, but some
860 people dislike it because they find it clearer to put all the information
861 about each target in one place.
862
863 @node Cleanup,  , Combine By Prerequisite, Introduction
864 @section Rules for Cleaning the Directory
865 @cindex cleaning up
866 @cindex removing, to clean up
867
868 Compiling a program is not the only thing you might want to write rules
869 for.  Makefiles commonly tell how to do a few other things besides
870 compiling a program: for example, how to delete all the object files
871 and executables so that the directory is @samp{clean}.
872
873 @cindex @code{clean} target
874 Here is how we
875 could write a @code{make} rule for cleaning our example editor:
876
877 @example
878 @group
879 clean:
880         rm edit $(objects)
881 @end group
882 @end example
883
884 In practice, we might want to write the rule in a somewhat more
885 complicated manner to handle unanticipated situations.  We would do this:
886
887 @example
888 @group
889 .PHONY : clean
890 clean :
891         -rm edit $(objects)
892 @end group
893 @end example
894
895 @noindent
896 This prevents @code{make} from getting confused by an actual file
897 called @file{clean} and causes it to continue in spite of errors from
898 @code{rm}.  (See @ref{Phony Targets}, and @ref{Errors, ,Errors in
899 Commands}.)
900
901 @noindent
902 A rule such as this should not be placed at the beginning of the
903 makefile, because we do not want it to run by default!  Thus, in the
904 example makefile, we want the rule for @code{edit}, which recompiles
905 the editor, to remain the default goal.
906
907 Since @code{clean} is not a prerequisite of @code{edit}, this rule will not
908 run at all if we give the command @samp{make} with no arguments.  In
909 order to make the rule run, we have to type @samp{make clean}.
910 @xref{Running, ,How to Run @code{make}}.
911
912 @node Makefiles, Rules, Introduction, Top
913 @chapter Writing Makefiles
914
915 @cindex makefile, how to write
916 The information that tells @code{make} how to recompile a system comes from
917 reading a data base called the @dfn{makefile}.
918
919 @menu
920 * Makefile Contents::           What makefiles contain.
921 * Makefile Names::              How to name your makefile.
922 * Include::                     How one makefile can use another makefile.
923 * MAKEFILES Variable::          The environment can specify extra makefiles.
924 * Remaking Makefiles::          How makefiles get remade.
925 * Overriding Makefiles::        How to override part of one makefile
926                                  with another makefile.
927 * Reading Makefiles::           How makefiles are parsed.
928 @end menu
929
930 @node Makefile Contents, Makefile Names,  , Makefiles
931 @section What Makefiles Contain
932
933 Makefiles contain five kinds of things: @dfn{explicit rules},
934 @dfn{implicit rules}, @dfn{variable definitions}, @dfn{directives},
935 and @dfn{comments}.  Rules, variables, and directives are described at
936 length in later chapters.@refill
937
938 @itemize @bullet
939 @cindex rule, explicit, definition of
940 @cindex explicit rule, definition of
941 @item
942 An @dfn{explicit rule} says when and how to remake one or more files,
943 called the rule's targets.  It lists the other files that the targets
944 depend on, call the @dfn{prerequisites} of the target, and may also give
945 commands to use to create or update the targets.  @xref{Rules, ,Writing
946 Rules}.
947
948 @cindex rule, implicit, definition of
949 @cindex implicit rule, definition of
950 @item
951 An @dfn{implicit rule} says when and how to remake a class of files
952 based on their names.  It describes how a target may depend on a file
953 with a name similar to the target and gives commands to create or
954 update such a target.  @xref{Implicit Rules, ,Using Implicit Rules}.
955
956 @cindex variable definition
957 @item
958 A @dfn{variable definition} is a line that specifies a text string
959 value for a variable that can be substituted into the text later.  The
960 simple makefile example shows a variable definition for @code{objects}
961 as a list of all object files (@pxref{Variables Simplify, , Variables
962 Make Makefiles Simpler}).
963
964 @cindex directive
965 @item
966 A @dfn{directive} is a command for @code{make} to do something special while
967 reading the makefile.  These include:
968
969 @itemize @bullet
970 @item
971 Reading another makefile (@pxref{Include, ,Including Other Makefiles}).
972
973 @item
974 Deciding (based on the values of variables) whether to use or
975 ignore a part of the makefile (@pxref{Conditionals, ,Conditional Parts of Makefiles}).
976
977 @item
978 Defining a variable from a verbatim string containing multiple lines
979 (@pxref{Defining, ,Defining Variables Verbatim}).
980 @end itemize
981
982 @cindex comments, in makefile
983 @cindex @code{#} (comments), in makefile
984 @item
985 @samp{#} in a line of a makefile starts a @dfn{comment}.  It and the rest of
986 the line are ignored, except that a trailing backslash not escaped by
987 another backslash will continue the comment across multiple lines.
988 Comments may appear on any of the lines in the makefile, except within a
989 @code{define} directive, and perhaps within commands (where the shell
990 decides what is a comment).  A line containing just a comment (with
991 perhaps spaces before it) is effectively blank, and is ignored.@refill
992 @end itemize
993
994 @node Makefile Names, Include, Makefile Contents, Makefiles
995 @section What Name to Give Your Makefile
996 @cindex makefile name
997 @cindex name of makefile
998 @cindex default makefile name
999 @cindex file name of makefile
1000
1001 @c following paragraph rewritten to avoid overfull hbox
1002 By default, when @code{make} looks for the makefile, it tries the
1003 following names, in order: @file{GNUmakefile}, @file{makefile}
1004 and @file{Makefile}.@refill
1005 @findex Makefile
1006 @findex GNUmakefile
1007 @findex makefile
1008
1009 @cindex @code{README}
1010 Normally you should call your makefile either @file{makefile} or
1011 @file{Makefile}.  (We recommend @file{Makefile} because it appears
1012 prominently near the beginning of a directory listing, right near other
1013 important files such as @file{README}.)  The first name checked,
1014 @file{GNUmakefile}, is not recommended for most makefiles.  You should
1015 use this name if you have a makefile that is specific to GNU
1016 @code{make}, and will not be understood by other versions of
1017 @code{make}.  Other @code{make} programs look for @file{makefile} and
1018 @file{Makefile}, but not @file{GNUmakefile}.
1019
1020 If @code{make} finds none of these names, it does not use any makefile.
1021 Then you must specify a goal with a command argument, and @code{make}
1022 will attempt to figure out how to remake it using only its built-in
1023 implicit rules.  @xref{Implicit Rules, ,Using Implicit Rules}.
1024
1025 @cindex @code{-f}
1026 @cindex @code{--file}
1027 @cindex @code{--makefile}
1028 If you want to use a nonstandard name for your makefile, you can specify
1029 the makefile name with the @samp{-f} or @samp{--file} option.  The
1030 arguments @w{@samp{-f @var{name}}} or @w{@samp{--file=@var{name}}} tell
1031 @code{make} to read the file @var{name} as the makefile.  If you use
1032 more than one @samp{-f} or @samp{--file} option, you can specify several
1033 makefiles.  All the makefiles are effectively concatenated in the order
1034 specified.  The default makefile names @file{GNUmakefile},
1035 @file{makefile} and @file{Makefile} are not checked automatically if you
1036 specify @samp{-f} or @samp{--file}.@refill
1037 @cindex specifying makefile name
1038 @cindex makefile name, how to specify
1039 @cindex name of makefile, how to specify
1040 @cindex file name of makefile, how to specify
1041
1042 @node Include, MAKEFILES Variable, Makefile Names, Makefiles
1043 @section Including Other Makefiles
1044 @cindex including other makefiles
1045 @cindex makefile, including
1046
1047 @findex include
1048 The @code{include} directive tells @code{make} to suspend reading the
1049 current makefile and read one or more other makefiles before continuing.
1050 The directive is a line in the makefile that looks like this:
1051
1052 @example
1053 include @var{filenames}@dots{}
1054 @end example
1055
1056 @noindent
1057 @var{filenames} can contain shell file name patterns.
1058 @cindex shell file name pattern (in @code{include})
1059 @cindex shell wildcards (in @code{include})
1060 @cindex wildcard, in @code{include}
1061
1062 Extra spaces are allowed and ignored at the beginning of the line, but
1063 a tab is not allowed.  (If the line begins with a tab, it will be
1064 considered a command line.)  Whitespace is required between
1065 @code{include} and the file names, and between file names; extra
1066 whitespace is ignored there and at the end of the directive.  A
1067 comment starting with @samp{#} is allowed at the end of the line.  If
1068 the file names contain any variable or function references, they are
1069 expanded.  @xref{Using Variables, ,How to Use Variables}.
1070
1071 For example, if you have three @file{.mk} files, @file{a.mk},
1072 @file{b.mk}, and @file{c.mk}, and @code{$(bar)} expands to
1073 @code{bish bash}, then the following expression
1074
1075 @example
1076 include foo *.mk $(bar)
1077 @end example
1078
1079 is equivalent to
1080
1081 @example
1082 include foo a.mk b.mk c.mk bish bash
1083 @end example
1084
1085 When @code{make} processes an @code{include} directive, it suspends
1086 reading of the containing makefile and reads from each listed file in
1087 turn.  When that is finished, @code{make} resumes reading the
1088 makefile in which the directive appears.
1089
1090 One occasion for using @code{include} directives is when several programs,
1091 handled by individual makefiles in various directories, need to use a
1092 common set of variable definitions
1093 (@pxref{Setting, ,Setting Variables}) or pattern rules
1094 (@pxref{Pattern Rules, ,Defining and Redefining Pattern Rules}).
1095
1096 Another such occasion is when you want to generate prerequisites from
1097 source files automatically; the prerequisites can be put in a file that
1098 is included by the main makefile.  This practice is generally cleaner
1099 than that of somehow appending the prerequisites to the end of the main
1100 makefile as has been traditionally done with other versions of
1101 @code{make}.  @xref{Automatic Prerequisites}.
1102 @cindex prerequisites, automatic generation
1103 @cindex automatic generation of prerequisites
1104 @cindex generating prerequisites automatically
1105
1106 @cindex @code{-I}
1107 @cindex @code{--include-dir}
1108 @cindex included makefiles, default directries
1109 @cindex default directries for included makefiles
1110 @findex /usr/gnu/include
1111 @findex /usr/local/include
1112 @findex /usr/include
1113 If the specified name does not start with a slash, and the file is not
1114 found in the current directory, several other directories are searched.
1115 First, any directories you have specified with the @samp{-I} or
1116 @samp{--include-dir} option are searched
1117 (@pxref{Options Summary, ,Summary of Options}).
1118 Then the following directories (if they exist)
1119 are searched, in this order:
1120 @file{@var{prefix}/include} (normally @file{/usr/local/include}
1121 @footnote{GNU Make compiled for MS-DOS and MS-Windows behaves as if
1122 @var{prefix} has been defined to be the root of the DJGPP tree
1123 hierarchy.})
1124 @file{/usr/gnu/include},
1125 @file{/usr/local/include}, @file{/usr/include}.
1126
1127 If an included makefile cannot be found in any of these directories, a
1128 warning message is generated, but it is not an immediately fatal error;
1129 processing of the makefile containing the @code{include} continues.
1130 Once it has finished reading makefiles, @code{make} will try to remake
1131 any that are out of date or don't exist.
1132 @xref{Remaking Makefiles, ,How Makefiles Are Remade}.
1133 Only after it has tried to find a way to remake a makefile and failed,
1134 will @code{make} diagnose the missing makefile as a fatal error.
1135
1136 If you want @code{make} to simply ignore a makefile which does not exist
1137 and cannot be remade, with no error message, use the @w{@code{-include}}
1138 directive instead of @code{include}, like this:
1139
1140 @example
1141 -include @var{filenames}@dots{}
1142 @end example
1143
1144 This is acts like @code{include} in every way except that there is no
1145 error (not even a warning) if any of the @var{filenames} do not exist.
1146 For compatibility with some other @code{make} implementations,
1147 @code{sinclude} is another name for @w{@code{-include}}.
1148
1149 @node MAKEFILES Variable, Remaking Makefiles, Include, Makefiles
1150 @section The Variable @code{MAKEFILES}
1151 @cindex makefile, and @code{MAKEFILES} variable
1152 @cindex including (@code{MAKEFILES} variable)
1153
1154 @vindex MAKEFILES
1155 If the environment variable @code{MAKEFILES} is defined, @code{make}
1156 considers its value as a list of names (separated by whitespace) of
1157 additional makefiles to be read before the others.  This works much like
1158 the @code{include} directive: various directories are searched for those
1159 files (@pxref{Include, ,Including Other Makefiles}).  In addition, the
1160 default goal is never taken from one of these makefiles and it is not an
1161 error if the files listed in @code{MAKEFILES} are not found.@refill
1162
1163 @cindex recursion, and @code{MAKEFILES} variable
1164 The main use of @code{MAKEFILES} is in communication between recursive
1165 invocations of @code{make} (@pxref{Recursion, ,Recursive Use of
1166 @code{make}}).  It usually is not desirable to set the environment
1167 variable before a top-level invocation of @code{make}, because it is
1168 usually better not to mess with a makefile from outside.  However, if
1169 you are running @code{make} without a specific makefile, a makefile in
1170 @code{MAKEFILES} can do useful things to help the built-in implicit
1171 rules work better, such as defining search paths (@pxref{Directory Search}).
1172
1173 Some users are tempted to set @code{MAKEFILES} in the environment
1174 automatically on login, and program makefiles to expect this to be done.
1175 This is a very bad idea, because such makefiles will fail to work if run by
1176 anyone else.  It is much better to write explicit @code{include} directives
1177 in the makefiles.  @xref{Include, , Including Other Makefiles}.
1178
1179 @node Remaking Makefiles, Overriding Makefiles, MAKEFILES Variable, Makefiles
1180 @section How Makefiles Are Remade
1181
1182 @cindex updating makefiles
1183 @cindex remaking makefiles
1184 @cindex makefile, remaking of
1185 Sometimes makefiles can be remade from other files, such as RCS or SCCS
1186 files.  If a makefile can be remade from other files, you probably want
1187 @code{make} to get an up-to-date version of the makefile to read in.
1188
1189 To this end, after reading in all makefiles, @code{make} will consider
1190 each as a goal target and attempt to update it.  If a makefile has a
1191 rule which says how to update it (found either in that very makefile or
1192 in another one) or if an implicit rule applies to it (@pxref{Implicit
1193 Rules, ,Using Implicit Rules}), it will be updated if necessary.  After
1194 all makefiles have been checked, if any have actually been changed,
1195 @code{make} starts with a clean slate and reads all the makefiles over
1196 again.  (It will also attempt to update each of them over again, but
1197 normally this will not change them again, since they are already up to
1198 date.)@refill
1199
1200 If you know that one or more of your makefiles cannot be remade and you
1201 want to keep @code{make} from performing an implicit rule search on
1202 them, perhaps for efficiency reasons, you can use any normal method of
1203 preventing implicit rule lookup to do so.  For example, you can write an
1204 explicit rule with the makefile as the target, and an empty command
1205 string (@pxref{Empty Commands, ,Using Empty Commands}).
1206
1207 If the makefiles specify a double-colon rule to remake a file with
1208 commands but no prerequisites, that file will always be remade
1209 (@pxref{Double-Colon}).  In the case of makefiles, a makefile that has a
1210 double-colon rule with commands but no prerequisites will be remade every
1211 time @code{make} is run, and then again after @code{make} starts over
1212 and reads the makefiles in again.  This would cause an infinite loop:
1213 @code{make} would constantly remake the makefile, and never do anything
1214 else.  So, to avoid this, @code{make} will @strong{not} attempt to
1215 remake makefiles which are specified as targets of a double-colon rule
1216 with commands but no prerequisites.@refill
1217
1218 If you do not specify any makefiles to be read with @samp{-f} or
1219 @samp{--file} options, @code{make} will try the default makefile names;
1220 @pxref{Makefile Names, ,What Name to Give Your Makefile}.  Unlike
1221 makefiles explicitly requested with @samp{-f} or @samp{--file} options,
1222 @code{make} is not certain that these makefiles should exist.  However,
1223 if a default makefile does not exist but can be created by running
1224 @code{make} rules, you probably want the rules to be run so that the
1225 makefile can be used.
1226
1227 Therefore, if none of the default makefiles exists, @code{make} will try
1228 to make each of them in the same order in which they are searched for
1229 (@pxref{Makefile Names, ,What Name to Give Your Makefile})
1230 until it succeeds in making one, or it runs out of names to try.  Note
1231 that it is not an error if @code{make} cannot find or make any makefile;
1232 a makefile is not always necessary.@refill
1233
1234 When you use the @samp{-t} or @samp{--touch} option
1235 (@pxref{Instead of Execution, ,Instead of Executing the Commands}),
1236 you would not want to use an out-of-date makefile to decide which
1237 targets to touch.  So the @samp{-t} option has no effect on updating
1238 makefiles; they are really updated even if @samp{-t} is specified.
1239 Likewise, @samp{-q} (or @samp{--question}) and @samp{-n} (or
1240 @samp{--just-print}) do not prevent updating of makefiles, because an
1241 out-of-date makefile would result in the wrong output for other targets.
1242 Thus, @samp{make -f mfile -n foo} will update @file{mfile}, read it in,
1243 and then print the commands to update @file{foo} and its prerequisites
1244 without running them.  The commands printed for @file{foo} will be those
1245 specified in the updated contents of @file{mfile}.
1246
1247 However, on occasion you might actually wish to prevent updating of even
1248 the makefiles.  You can do this by specifying the makefiles as goals in
1249 the command line as well as specifying them as makefiles.  When the
1250 makefile name is specified explicitly as a goal, the options @samp{-t}
1251 and so on do apply to them.
1252
1253 Thus, @samp{make -f mfile -n mfile foo} would read the makefile
1254 @file{mfile}, print the commands needed to update it without actually
1255 running them, and then print the commands needed to update @file{foo}
1256 without running them.  The commands for @file{foo} will be those
1257 specified by the existing contents of @file{mfile}.
1258
1259 @node Overriding Makefiles, Reading Makefiles, Remaking Makefiles, Makefiles
1260 @section Overriding Part of Another Makefile
1261
1262 @cindex overriding makefiles
1263 @cindex makefile, overriding
1264 Sometimes it is useful to have a makefile that is mostly just like
1265 another makefile.  You can often use the @samp{include} directive to
1266 include one in the other, and add more targets or variable definitions.
1267 However, if the two makefiles give different commands for the same
1268 target, @code{make} will not let you just do this.  But there is another way.
1269
1270 @cindex match-anything rule, used to override
1271 In the containing makefile (the one that wants to include the other),
1272 you can use a match-anything pattern rule to say that to remake any
1273 target that cannot be made from the information in the containing
1274 makefile, @code{make} should look in another makefile.
1275 @xref{Pattern Rules}, for more information on pattern rules.
1276
1277 For example, if you have a makefile called @file{Makefile} that says how
1278 to make the target @samp{foo} (and other targets), you can write a
1279 makefile called @file{GNUmakefile} that contains:
1280
1281 @example
1282 foo:
1283         frobnicate > foo
1284
1285 %: force
1286         @@$(MAKE) -f Makefile $@@
1287 force: ;
1288 @end example
1289
1290 If you say @samp{make foo}, @code{make} will find @file{GNUmakefile},
1291 read it, and see that to make @file{foo}, it needs to run the command
1292 @samp{frobnicate > foo}.  If you say @samp{make bar}, @code{make} will
1293 find no way to make @file{bar} in @file{GNUmakefile}, so it will use the
1294 commands from the pattern rule: @samp{make -f Makefile bar}.  If
1295 @file{Makefile} provides a rule for updating @file{bar}, @code{make}
1296 will apply the rule.  And likewise for any other target that
1297 @file{GNUmakefile} does not say how to make.
1298
1299 The way this works is that the pattern rule has a pattern of just
1300 @samp{%}, so it matches any target whatever.  The rule specifies a
1301 prerequisite @file{force}, to guarantee that the commands will be run even
1302 if the target file already exists.  We give @file{force} target empty
1303 commands to prevent @code{make} from searching for an implicit rule to
1304 build it---otherwise it would apply the same match-anything rule to
1305 @file{force} itself and create a prerequisite loop!
1306
1307 @node Reading Makefiles,  , Overriding Makefiles, Makefiles
1308 @section How @code{make} Reads a Makefile
1309 @cindex reading makefiles
1310 @cindex makefile, parsing
1311
1312 GNU @code{make} does its work in two distinct phases.  During the first
1313 phase it reads all the makefiles, included makefiles, etc. and
1314 internalizes all the variables and their values, implicit and explicit
1315 rules, and constructs a dependency graph of all the targets and their
1316 prerequisites.  During the second phase, @code{make} uses these internal
1317 structures to determine what targets will need to be rebuilt and to
1318 invoke the rules necessary to do so.
1319
1320 It's important to understand this two-phase approach because it has a
1321 direct impact on how variable and function expansion happens; this is
1322 often a source of some confusion when writing makefiles.  Here we will
1323 present a summary of the phases in which expansion happens for different
1324 constructs within the makefile.  We say that expansion is
1325 @dfn{immediate} if it happens during the first phase: in this case
1326 @code{make} will expand any variables or functions in that section of a
1327 construct as the makefile is parsed.  We say that expansion is
1328 @dfn{deferred} if expansion is not performed immediately.  Expansion of
1329 deferred construct is not performed until either the construct appears
1330 later in an immediate context, or until the second phase.
1331
1332 You may not be familiar with some of these constructs yet.  You can
1333 reference this section as you become familiar with them, in later
1334 chapters.
1335
1336 @subheading Variable Assignment
1337 @cindex +=, expansion
1338 @cindex =, expansion
1339 @cindex ?=, expansion
1340 @cindex +=, expansion
1341 @cindex define, expansion
1342
1343 Variable definitions are parsed as follows:
1344
1345 @example
1346 @var{immediate} = @var{deferred}
1347 @var{immediate} ?= @var{deferred}
1348 @var{immediate} := @var{immediate}
1349 @var{immediate} += @var{deferred} or @var{immediate}
1350
1351 define @var{immediate}
1352   @var{deferred}
1353 endef
1354 @end example
1355
1356 For the append operator, @samp{+=}, the right-hand side is considered
1357 immediate if the variable was previously set as a simple variable
1358 (@samp{:=}), and deferred otherwise.
1359
1360 @subheading Conditional Syntax
1361 @cindex ifdef, expansion
1362 @cindex ifeq, expansion
1363 @cindex ifndef, expansion
1364 @cindex ifneq, expansion
1365
1366 All instances of conditional syntax are parsed immediately, in their
1367 entirety; this includes the @code{ifdef}, @code{ifeq}, @code{ifndef},
1368 and @code{ifneq} forms.
1369
1370 @subheading Rule Definition
1371 @cindex target, expansion
1372 @cindex prerequisite, expansion
1373 @cindex implicit rule, expansion
1374 @cindex pattern rule, expansion
1375 @cindex explicit rule, expansion
1376
1377 A rule is always expanded the same way, regardless of the form:
1378
1379 @example
1380 @var{immediate} : @var{immediate} ; @var{deferred}
1381         @var{deferred}
1382 @end example
1383
1384 That is, the target and prerequisite sections are expanded immediately,
1385 and the commands used to construct the target are always deferred.  This
1386 general rule is true for explicit rules, pattern rules, suffix rules,
1387 static pattern rules, and simple prerequisite definitions.
1388
1389 @node Rules, Commands, Makefiles, Top
1390 @chapter Writing Rules
1391 @cindex writing rules
1392 @cindex rule, how to write
1393 @cindex target
1394 @cindex prerequisite
1395
1396 A @dfn{rule} appears in the makefile and says when and how to remake
1397 certain files, called the rule's @dfn{targets} (most often only one per rule).
1398 It lists the other files that are the @dfn{prerequisites} of the target, and
1399 @dfn{commands} to use to create or update the target.
1400
1401 @cindex default goal
1402 @cindex goal, default
1403 The order of rules is not significant, except for determining the
1404 @dfn{default goal}: the target for @code{make} to consider, if you do
1405 not otherwise specify one.  The default goal is the target of the first
1406 rule in the first makefile.  If the first rule has multiple targets,
1407 only the first target is taken as the default.  There are two
1408 exceptions: a target starting with a period is not a default unless it
1409 contains one or more slashes, @samp{/}, as well; and, a target that
1410 defines a pattern rule has no effect on the default goal.
1411 (@xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.)
1412
1413 Therefore, we usually write the makefile so that the first rule is the
1414 one for compiling the entire program or all the programs described by
1415 the makefile (often with a target called @samp{all}).
1416 @xref{Goals, ,Arguments to Specify the Goals}.
1417
1418 @menu
1419 * Rule Example::                An example explained.
1420 * Rule Syntax::                 General syntax explained.
1421 * Wildcards::                   Using wildcard characters such as `*'.
1422 * Directory Search::            Searching other directories for source files.
1423 * Phony Targets::               Using a target that is not a real file's name.
1424 * Force Targets::               You can use a target without commands
1425                                   or prerequisites to mark other
1426                                   targets as phony.
1427 * Empty Targets::               When only the date matters and the
1428                                   files are empty.
1429 * Special Targets::             Targets with special built-in meanings.
1430 * Multiple Targets::            When to make use of several targets in a rule.
1431 * Multiple Rules::              How to use several rules with the same target.
1432 * Static Pattern::              Static pattern rules apply to multiple targets
1433                                   and can vary the prerequisites according to
1434                                   the target name.
1435 * Double-Colon::                How to use a special kind of rule to allow
1436                                   several independent rules for one target.
1437 * Automatic Prerequisites::     How to automatically generate rules giving
1438                                   prerequisites from source files themselves.
1439 @end menu
1440
1441 @ifinfo
1442 @node Rule Example, Rule Syntax,  , Rules
1443 @section Rule Example
1444
1445 Here is an example of a rule:
1446
1447 @example
1448 foo.o : foo.c defs.h       # module for twiddling the frobs
1449         cc -c -g foo.c
1450 @end example
1451
1452 Its target is @file{foo.o} and its prerequisites are @file{foo.c} and
1453 @file{defs.h}.  It has one command, which is @samp{cc -c -g foo.c}.
1454 The command line starts with a tab to identify it as a command.
1455
1456 This rule says two things:
1457
1458 @itemize @bullet
1459 @item
1460 How to decide whether @file{foo.o} is out of date: it is out of date
1461 if it does not exist, or if either @file{foo.c} or @file{defs.h} is
1462 more recent than it.
1463
1464 @item
1465 How to update the file @file{foo.o}: by running @code{cc} as stated.
1466 The command does not explicitly mention @file{defs.h}, but we presume
1467 that @file{foo.c} includes it, and that that is why @file{defs.h} was
1468 added to the prerequisites.
1469 @end itemize
1470 @end ifinfo
1471
1472 @node Rule Syntax, Wildcards, Rule Example, Rules
1473 @section Rule Syntax
1474
1475 @cindex rule syntax
1476 @cindex syntax of rules
1477 In general, a rule looks like this:
1478
1479 @example
1480 @var{targets} : @var{prerequisites}
1481         @var{command}
1482         @dots{}
1483 @end example
1484
1485 @noindent
1486 or like this:
1487
1488 @example
1489 @var{targets} : @var{prerequisites} ; @var{command}
1490         @var{command}
1491         @dots{}
1492 @end example
1493
1494 @cindex targets
1495 @cindex rule targets
1496 The @var{targets} are file names, separated by spaces.  Wildcard
1497 characters may be used (@pxref{Wildcards, ,Using Wildcard Characters
1498 in File Names}) and a name of the form @file{@var{a}(@var{m})}
1499 represents member @var{m} in archive file @var{a}
1500 (@pxref{Archive Members, ,Archive Members as Targets}).
1501 Usually there is only one
1502 target per rule, but occasionally there is a reason to have more
1503 (@pxref{Multiple Targets, , Multiple Targets in a Rule}).@refill
1504
1505 @cindex commands
1506 @cindex tab character (in commands)
1507 The @var{command} lines start with a tab character.  The first command may
1508 appear on the line after the prerequisites, with a tab character, or may
1509 appear on the same line, with a semicolon.  Either way, the effect is the
1510 same.  @xref{Commands, ,Writing the Commands in Rules}.
1511
1512 @cindex dollar sign (@code{$}), in rules
1513 @cindex @code{$}, in rules
1514 @cindex rule, and @code{$}
1515 Because dollar signs are used to start variable references, if you really
1516 want a dollar sign in a rule you must write two of them, @samp{$$}
1517 (@pxref{Using Variables, ,How to Use Variables}).
1518 You may split a long line by inserting a backslash
1519 followed by a newline, but this is not required, as @code{make} places no
1520 limit on the length of a line in a makefile.
1521
1522 A rule tells @code{make} two things: when the targets are out of date,
1523 and how to update them when necessary.
1524
1525 @cindex prerequisites
1526 @cindex rule prerequisites
1527 The criterion for being out of date is specified in terms of the
1528 @var{prerequisites}, which consist of file names separated by spaces.
1529 (Wildcards and archive members (@pxref{Archives}) are allowed here too.)
1530 A target is out of date if it does not exist or if it is older than any
1531 of the prerequisites (by comparison of last-modification times).  The
1532 idea is that the contents of the target file are computed based on
1533 information in the prerequisites, so if any of the prerequisites changes,
1534 the contents of the existing target file are no longer necessarily
1535 valid.
1536
1537 How to update is specified by @var{commands}.  These are lines to be
1538 executed by the shell (normally @samp{sh}), but with some extra features
1539 (@pxref{Commands, ,Writing the Commands in Rules}).
1540
1541 @node Wildcards, Directory Search, Rule Syntax, Rules
1542 @section Using Wildcard Characters in File Names
1543 @cindex wildcard
1544 @cindex file name with wildcards
1545 @cindex globbing (wildcards)
1546
1547 @cindex @code{*} (wildcard character)
1548 @cindex @code{?} (wildcard character)
1549 @cindex @code{[@dots{}]} (wildcard characters)
1550 A single file name can specify many files using @dfn{wildcard characters}.
1551 The wildcard characters in @code{make} are @samp{*}, @samp{?} and
1552 @samp{[@dots{}]}, the same as in the Bourne shell.  For example, @file{*.c}
1553 specifies a list of all the files (in the working directory) whose names
1554 end in @samp{.c}.@refill
1555
1556 @cindex @code{~} (tilde)
1557 @cindex tilde (@code{~})
1558 @cindex home directory
1559 The character @samp{~} at the beginning of a file name also has special
1560 significance.  If alone, or followed by a slash, it represents your home
1561 directory.  For example @file{~/bin} expands to @file{/home/you/bin}.
1562 If the @samp{~} is followed by a word, the string represents the home
1563 directory of the user named by that word.  For example @file{~john/bin}
1564 expands to @file{/home/john/bin}.  On systems which don't have a home
1565 directory for each user (such as MS-DOS or MS-Windows), this
1566 functionality can be simulated by setting the environment variable
1567 @var{HOME}.@refill
1568
1569 Wildcard expansion happens automatically in targets, in prerequisites,
1570 and in commands (where the shell does the expansion).  In other
1571 contexts, wildcard expansion happens only if you request it explicitly
1572 with the @code{wildcard} function.
1573
1574 The special significance of a wildcard character can be turned off by
1575 preceding it with a backslash.  Thus, @file{foo\*bar} would refer to a
1576 specific file whose name consists of @samp{foo}, an asterisk, and
1577 @samp{bar}.@refill
1578
1579 @menu
1580 * Wildcard Examples::           Several examples
1581 * Wildcard Pitfall::            Problems to avoid.
1582 * Wildcard Function::           How to cause wildcard expansion where
1583                                   it does not normally take place.
1584 @end menu
1585
1586 @node Wildcard Examples, Wildcard Pitfall,  , Wildcards
1587 @subsection Wildcard Examples
1588
1589 Wildcards can be used in the commands of a rule, where they are expanded
1590 by the shell.  For example, here is a rule to delete all the object files:
1591
1592 @example
1593 @group
1594 clean:
1595         rm -f *.o
1596 @end group
1597 @end example
1598 @cindex @code{rm} (shell command)
1599
1600 Wildcards are also useful in the prerequisites of a rule.  With the
1601 following rule in the makefile, @samp{make print} will print all the
1602 @samp{.c} files that have changed since the last time you printed them:
1603
1604 @example
1605 print: *.c
1606         lpr -p $?
1607         touch print
1608 @end example
1609
1610 @cindex @code{print} target
1611 @cindex @code{lpr} (shell command)
1612 @cindex @code{touch} (shell command)
1613 @noindent
1614 This rule uses @file{print} as an empty target file; see @ref{Empty
1615 Targets, ,Empty Target Files to Record Events}.  (The automatic variable
1616 @samp{$?} is used to print only those files that have changed; see
1617 @ref{Automatic, ,Automatic Variables}.)@refill
1618
1619 Wildcard expansion does not happen when you define a variable.  Thus, if
1620 you write this:
1621
1622 @example
1623 objects = *.o
1624 @end example
1625
1626 @noindent
1627 then the value of the variable @code{objects} is the actual string
1628 @samp{*.o}.  However, if you use the value of @code{objects} in a target,
1629 prerequisite or command, wildcard expansion will take place at that time.
1630 To set @code{objects} to the expansion, instead use:
1631
1632 @example
1633 objects := $(wildcard *.o)
1634 @end example
1635
1636 @noindent
1637 @xref{Wildcard Function}.
1638
1639 @node Wildcard Pitfall, Wildcard Function, Wildcard Examples, Wildcards
1640 @subsection Pitfalls of Using Wildcards
1641 @cindex wildcard pitfalls
1642 @cindex pitfalls of wildcards
1643 @cindex mistakes with wildcards
1644 @cindex errors with wildcards
1645 @cindex problems with wildcards
1646
1647 Now here is an example of a naive way of using wildcard expansion, that
1648 does not do what you would intend.  Suppose you would like to say that the
1649 executable file @file{foo} is made from all the object files in the
1650 directory, and you write this:
1651
1652 @example
1653 objects = *.o
1654
1655 foo : $(objects)
1656         cc -o foo $(CFLAGS) $(objects)
1657 @end example
1658
1659 @noindent
1660 The value of @code{objects} is the actual string @samp{*.o}.  Wildcard
1661 expansion happens in the rule for @file{foo}, so that each @emph{existing}
1662 @samp{.o} file becomes a prerequisite of @file{foo} and will be recompiled if
1663 necessary.
1664
1665 But what if you delete all the @samp{.o} files?  When a wildcard matches
1666 no files, it is left as it is, so then @file{foo} will depend on the
1667 oddly-named file @file{*.o}.  Since no such file is likely to exist,
1668 @code{make} will give you an error saying it cannot figure out how to
1669 make @file{*.o}.  This is not what you want!
1670
1671 Actually it is possible to obtain the desired result with wildcard
1672 expansion, but you need more sophisticated techniques, including the
1673 @code{wildcard} function and string substitution.
1674 @ifinfo
1675 @xref{Wildcard Function, ,The Function @code{wildcard}}.
1676 @end ifinfo
1677 @iftex
1678 These are described in the following section.
1679 @end iftex
1680
1681 @cindex wildcards and MS-DOS/MS-Windows backslashes
1682 @cindex backslashes in pathnames and wildcard expansion
1683
1684 Microsoft operating systems (MS-DOS and MS-Windows) use backslashes to
1685 separate directories in pathnames, like so:
1686
1687 @example
1688   c:\foo\bar\baz.c
1689 @end example
1690
1691 This is equivalent to the Unix-style @file{c:/foo/bar/baz.c} (the
1692 @file{c:} part is the so-called drive letter).  When @code{make} runs on
1693 these systems, it supports backslashes as well as the Unix-style forward
1694 slashes in pathnames.  However, this support does @emph{not} include the
1695 wildcard expansion, where backslash is a quote character.  Therefore,
1696 you @emph{must} use Unix-style slashes in these cases.
1697
1698
1699 @node Wildcard Function,  , Wildcard Pitfall, Wildcards
1700 @subsection The Function @code{wildcard}
1701 @findex wildcard
1702
1703 Wildcard expansion happens automatically in rules.  But wildcard expansion
1704 does not normally take place when a variable is set, or inside the
1705 arguments of a function.  If you want to do wildcard expansion in such
1706 places, you need to use the @code{wildcard} function, like this:
1707
1708 @example
1709 $(wildcard @var{pattern}@dots{})
1710 @end example
1711
1712 @noindent
1713 This string, used anywhere in a makefile, is replaced by a
1714 space-separated list of names of existing files that match one of the
1715 given file name patterns.  If no existing file name matches a pattern,
1716 then that pattern is omitted from the output of the @code{wildcard}
1717 function.  Note that this is different from how unmatched wildcards
1718 behave in rules, where they are used verbatim rather than ignored
1719 (@pxref{Wildcard Pitfall}).
1720
1721 One use of the @code{wildcard} function is to get a list of all the C source
1722 files in a directory, like this:
1723
1724 @example
1725 $(wildcard *.c)
1726 @end example
1727
1728 We can change the list of C source files into a list of object files by
1729 replacing the @samp{.c} suffix with @samp{.o} in the result, like this:
1730
1731 @example
1732 $(patsubst %.c,%.o,$(wildcard *.c))
1733 @end example
1734
1735 @noindent
1736 (Here we have used another function, @code{patsubst}.
1737 @xref{Text Functions, ,Functions for String Substitution and Analysis}.)@refill
1738
1739 Thus, a makefile to compile all C source files in the directory and then
1740 link them together could be written as follows:
1741
1742 @example
1743 objects := $(patsubst %.c,%.o,$(wildcard *.c))
1744
1745 foo : $(objects)
1746         cc -o foo $(objects)
1747 @end example
1748
1749 @noindent
1750 (This takes advantage of the implicit rule for compiling C programs, so
1751 there is no need to write explicit rules for compiling the files.
1752 @xref{Flavors, ,The Two Flavors of Variables}, for an explanation of
1753 @samp{:=}, which is a variant of @samp{=}.)
1754
1755 @node Directory Search, Phony Targets, Wildcards, Rules
1756 @section Searching Directories for Prerequisites
1757 @vindex VPATH
1758 @findex vpath
1759 @cindex vpath
1760 @cindex search path for prerequisites (@code{VPATH})
1761 @cindex directory search (@code{VPATH})
1762
1763 For large systems, it is often desirable to put sources in a separate
1764 directory from the binaries.  The @dfn{directory search} features of
1765 @code{make} facilitate this by searching several directories
1766 automatically to find a prerequisite.  When you redistribute the files
1767 among directories, you do not need to change the individual rules,
1768 just the search paths.
1769
1770 @menu
1771 * General Search::              Specifying a search path that applies
1772                                   to every prerequisite.
1773 * Selective Search::            Specifying a search path
1774                                   for a specified class of names.
1775 * Search Algorithm::            When and how search paths are applied.
1776 * Commands/Search::             How to write shell commands that work together
1777                                   with search paths.
1778 * Implicit/Search::             How search paths affect implicit rules.
1779 * Libraries/Search::            Directory search for link libraries.
1780 @end menu
1781
1782 @node General Search, Selective Search,  , Directory Search
1783 @subsection @code{VPATH}: Search Path for All Prerequisites
1784 @vindex VPATH
1785
1786 The value of the @code{make} variable @code{VPATH} specifies a list of
1787 directories that @code{make} should search.  Most often, the
1788 directories are expected to contain prerequisite files that are not in the
1789 current directory; however, @code{VPATH} specifies a search list that
1790 @code{make} applies for all files, including files which are targets of
1791 rules.
1792
1793 Thus, if a file that is listed as a target or prerequisite does not exist
1794 in the current directory, @code{make} searches the directories listed in
1795 @code{VPATH} for a file with that name.  If a file is found in one of
1796 them, that file may become the prerequisite (see below).  Rules may then
1797 specify the names of files in the prerequisite list as if they all
1798 existed in the current directory.  @xref{Commands/Search, ,Writing Shell
1799 Commands with Directory Search}.
1800
1801 In the @code{VPATH} variable, directory names are separated by colons or
1802 blanks.  The order in which directories are listed is the order followed
1803 by @code{make} in its search.  (On MS-DOS and MS-Windows, semi-colons
1804 are used as separators of directory names in @code{VPATH}, since the
1805 colon can be used in the pathname itself, after the drive letter.)
1806
1807 For example,
1808
1809 @example
1810 VPATH = src:../headers
1811 @end example
1812
1813 @noindent
1814 specifies a path containing two directories, @file{src} and
1815 @file{../headers}, which @code{make} searches in that order.
1816
1817 With this value of @code{VPATH}, the following rule,
1818
1819 @example
1820 foo.o : foo.c
1821 @end example
1822
1823 @noindent
1824 is interpreted as if it were written like this:
1825
1826 @example
1827 foo.o : src/foo.c
1828 @end example
1829
1830 @noindent
1831 assuming the file @file{foo.c} does not exist in the current directory but
1832 is found in the directory @file{src}.
1833
1834 @node Selective Search, Search Algorithm, General Search, Directory Search
1835 @subsection The @code{vpath} Directive
1836 @findex vpath
1837
1838 Similar to the @code{VPATH} variable, but more selective, is the
1839 @code{vpath} directive (note lower case), which allows you to specify a
1840 search path for a particular class of file names: those that match a
1841 particular pattern.  Thus you can supply certain search directories for
1842 one class of file names and other directories (or none) for other file
1843 names.
1844
1845 There are three forms of the @code{vpath} directive:
1846
1847 @table @code
1848 @item vpath @var{pattern} @var{directories}
1849 Specify the search path @var{directories} for file names that match
1850 @var{pattern}.
1851
1852 The search path, @var{directories}, is a list of directories to be
1853 searched, separated by colons (semi-colons on MS-DOS and MS-Windows) or
1854 blanks, just like the search path used in the @code{VPATH} variable.
1855
1856 @item vpath @var{pattern}
1857 Clear out the search path associated with @var{pattern}.
1858
1859 @c Extra blank line makes sure this gets two lines.
1860 @item vpath
1861
1862 Clear all search paths previously specified with @code{vpath} directives.
1863 @end table
1864
1865 A @code{vpath} pattern is a string containing a @samp{%} character.  The
1866 string must match the file name of a prerequisite that is being searched
1867 for, the @samp{%} character matching any sequence of zero or more
1868 characters (as in pattern rules; @pxref{Pattern Rules, ,Defining and
1869 Redefining Pattern Rules}).  For example, @code{%.h} matches files that
1870 end in @code{.h}.  (If there is no @samp{%}, the pattern must match the
1871 prerequisite exactly, which is not useful very often.)
1872
1873 @cindex @code{%}, quoting in @code{vpath}
1874 @cindex @code{%}, quoting with @code{\} (backslash)
1875 @cindex @code{\} (backslash), to quote @code{%}
1876 @cindex backslash (@code{\}), to quote @code{%}
1877 @cindex quoting @code{%}, in @code{vpath}
1878 @samp{%} characters in a @code{vpath} directive's pattern can be quoted
1879 with preceding backslashes (@samp{\}).  Backslashes that would otherwise
1880 quote @samp{%} characters can be quoted with more backslashes.
1881 Backslashes that quote @samp{%} characters or other backslashes are
1882 removed from the pattern before it is compared to file names.  Backslashes
1883 that are not in danger of quoting @samp{%} characters go unmolested.@refill
1884
1885 When a prerequisite fails to exist in the current directory, if the
1886 @var{pattern} in a @code{vpath} directive matches the name of the
1887 prerequisite file, then the @var{directories} in that directive are searched
1888 just like (and before) the directories in the @code{VPATH} variable.
1889
1890 For example,
1891
1892 @example
1893 vpath %.h ../headers
1894 @end example
1895
1896 @noindent
1897 tells @code{make} to look for any prerequisite whose name ends in @file{.h}
1898 in the directory @file{../headers} if the file is not found in the current
1899 directory.
1900
1901 If several @code{vpath} patterns match the prerequisite file's name, then
1902 @code{make} processes each matching @code{vpath} directive one by one,
1903 searching all the directories mentioned in each directive.  @code{make}
1904 handles multiple @code{vpath} directives in the order in which they
1905 appear in the makefile; multiple directives with the same pattern are
1906 independent of each other.
1907
1908 @need 750
1909 Thus,
1910
1911 @example
1912 @group
1913 vpath %.c foo
1914 vpath %   blish
1915 vpath %.c bar
1916 @end group
1917 @end example
1918
1919 @noindent
1920 will look for a file ending in @samp{.c} in @file{foo}, then
1921 @file{blish}, then @file{bar}, while
1922
1923 @example
1924 @group
1925 vpath %.c foo:bar
1926 vpath %   blish
1927 @end group
1928 @end example
1929
1930 @noindent
1931 will look for a file ending in @samp{.c} in @file{foo}, then
1932 @file{bar}, then @file{blish}.
1933
1934 @node Search Algorithm, Commands/Search, Selective Search, Directory Search
1935 @subsection How Directory Searches are Performed
1936 @cindex algorithm for directory search
1937 @cindex directory search algorithm
1938
1939 When a prerequisite is found through directory search, regardless of type
1940 (general or selective), the pathname located may not be the one that
1941 @code{make} actually provides you in the prerequisite list.  Sometimes
1942 the path discovered through directory search is thrown away.
1943
1944 The algorithm @code{make} uses to decide whether to keep or abandon a
1945 path found via directory search is as follows:
1946
1947 @enumerate
1948 @item
1949 If a target file does not exist at the path specified in the makefile,
1950 directory search is performed.
1951
1952 @item
1953 If the directory search is successful, that path is kept and this file
1954 is tentatively stored as the target.
1955
1956 @item
1957 All prerequisites of this target are examined using this same method.
1958
1959 @item
1960 After processing the prerequisites, the target may or may not need to be
1961 rebuilt:
1962
1963 @enumerate a
1964 @item
1965 If the target does @emph{not} need to be rebuilt, the path to the file
1966 found during directory search is used for any prerequisite lists which
1967 contain this target.  In short, if @code{make} doesn't need to rebuild
1968 the target then you use the path found via directory search.
1969
1970 @item
1971 If the target @emph{does} need to be rebuilt (is out-of-date), the
1972 pathname found during directory search is @emph{thrown away}, and the
1973 target is rebuilt using the file name specified in the makefile.  In
1974 short, if @code{make} must rebuild, then the target is rebuilt locally,
1975 not in the directory found via directory search.
1976 @end enumerate
1977 @end enumerate
1978
1979 This algorithm may seem complex, but in practice it is quite often
1980 exactly what you want.
1981
1982 @cindex traditional directory search
1983 @cindex directory search, traditional
1984 Other versions of @code{make} use a simpler algorithm: if the file does
1985 not exist, and it is found via directory search, then that pathname is
1986 always used whether or not the target needs to be built.  Thus, if the
1987 target is rebuilt it is created at the pathname discovered during
1988 directory search.
1989
1990 @vindex GPATH
1991 If, in fact, this is the behavior you want for some or all of your
1992 directories, you can use the @code{GPATH} variable to indicate this to
1993 @code{make}.
1994
1995 @code{GPATH} has the same syntax and format as @code{VPATH} (that is, a
1996 space- or colon-delimited list of pathnames).  If an out-of-date target
1997 is found by directory search in a directory that also appears in
1998 @code{GPATH}, then that pathname is not thrown away.  The target is
1999 rebuilt using the expanded path.
2000
2001 @node Commands/Search, Implicit/Search, Search Algorithm, Directory Search
2002 @subsection Writing Shell Commands with Directory Search
2003 @cindex shell command, and directory search
2004 @cindex directory search (@code{VPATH}), and shell commands
2005
2006 When a prerequisite is found in another directory through directory search,
2007 this cannot change the commands of the rule; they will execute as written.
2008 Therefore, you must write the commands with care so that they will look for
2009 the prerequisite in the directory where @code{make} finds it.
2010
2011 This is done with the @dfn{automatic variables} such as @samp{$^}
2012 (@pxref{Automatic, ,Automatic Variables}).
2013 For instance, the value of @samp{$^} is a
2014 list of all the prerequisites of the rule, including the names of
2015 the directories in which they were found, and the value of
2016 @samp{$@@} is the target.  Thus:@refill
2017
2018 @example
2019 foo.o : foo.c
2020         cc -c $(CFLAGS) $^ -o $@@
2021 @end example
2022
2023 @noindent
2024 (The variable @code{CFLAGS} exists so you can specify flags for C
2025 compilation by implicit rules; we use it here for consistency so it will
2026 affect all C compilations uniformly;
2027 @pxref{Implicit Variables, ,Variables Used by Implicit Rules}.)
2028
2029 Often the prerequisites include header files as well, which you do not
2030 want to mention in the commands.  The automatic variable @samp{$<} is
2031 just the first prerequisite:
2032
2033 @example
2034 VPATH = src:../headers
2035 foo.o : foo.c defs.h hack.h
2036         cc -c $(CFLAGS) $< -o $@@
2037 @end example
2038
2039 @node Implicit/Search, Libraries/Search, Commands/Search, Directory Search
2040 @subsection Directory Search and Implicit Rules
2041 @cindex @code{VPATH}, and implicit rules
2042 @cindex directory search (@code{VPATH}), and implicit rules
2043 @cindex search path for prerequisites (@code{VPATH}), and implicit rules
2044 @cindex implicit rule, and directory search
2045 @cindex implicit rule, and @code{VPATH}
2046 @cindex rule, implicit, and directory search
2047 @cindex rule, implicit, and @code{VPATH}
2048
2049 The search through the directories specified in @code{VPATH} or with
2050 @code{vpath} also happens during consideration of implicit rules
2051 (@pxref{Implicit Rules, ,Using Implicit Rules}).
2052
2053 For example, when a file @file{foo.o} has no explicit rule, @code{make}
2054 considers implicit rules, such as the built-in rule to compile
2055 @file{foo.c} if that file exists.  If such a file is lacking in the
2056 current directory, the appropriate directories are searched for it.  If
2057 @file{foo.c} exists (or is mentioned in the makefile) in any of the
2058 directories, the implicit rule for C compilation is applied.
2059
2060 The commands of implicit rules normally use automatic variables as a
2061 matter of necessity; consequently they will use the file names found by
2062 directory search with no extra effort.
2063
2064 @node Libraries/Search,  , Implicit/Search, Directory Search
2065 @subsection Directory Search for Link Libraries
2066 @cindex link libraries, and directory search
2067 @cindex libraries for linking, directory search
2068 @cindex directory search (@code{VPATH}), and link libraries
2069 @cindex @code{VPATH}, and link libraries
2070 @cindex search path for prerequisites (@code{VPATH}), and link libraries
2071 @cindex @code{-l} (library search)
2072 @cindex link libraries, patterns matching
2073 @cindex @code{.LIBPATTERNS}, and link libraries
2074 @vindex .LIBPATTERNS
2075
2076 Directory search applies in a special way to libraries used with the
2077 linker.  This special feature comes into play when you write a prerequisite
2078 whose name is of the form @samp{-l@var{name}}.  (You can tell something
2079 strange is going on here because the prerequisite is normally the name of a
2080 file, and the @emph{file name} of a library generally looks like
2081 @file{lib@var{name}.a}, not like @samp{-l@var{name}}.)@refill
2082
2083 When a prerequisite's name has the form @samp{-l@var{name}}, @code{make}
2084 handles it specially by searching for the file @file{lib@var{name}.so} in
2085 the current directory, in directories specified by matching @code{vpath}
2086 search paths and the @code{VPATH} search path, and then in the
2087 directories @file{/lib}, @file{/usr/lib}, and @file{@var{prefix}/lib}
2088 (normally @file{/usr/local/lib}, but MS-DOS/MS-Windows versions of
2089 @code{make} behave as if @var{prefix} is defined to be the root of the
2090 DJGPP installation tree).
2091
2092 If that file is not found, then the file @file{lib@var{name}.a} is
2093 searched for, in the same directories as above.
2094
2095 For example, if there is a @file{/usr/lib/libcurses.a} library on your
2096 system (and no @file{/usr/lib/libcurses.so} file), then
2097
2098 @example
2099 @group
2100 foo : foo.c -lcurses
2101         cc $^ -o $@@
2102 @end group
2103 @end example
2104
2105 @noindent
2106 would cause the command @samp{cc foo.c /usr/lib/libcurses.a -o foo} to
2107 be executed when @file{foo} is older than @file{foo.c} or than
2108 @file{/usr/lib/libcurses.a}.@refill
2109
2110 Although the default set of files to be searched for is
2111 @file{lib@var{name}.so} and @file{lib@var{name}.a}, this is customizable
2112 via the @code{.LIBPATTERNS} variable.  Each word in the value of this
2113 variable is a pattern string.  When a prerequisite like
2114 @samp{-l@var{name}} is seen, @code{make} will replace the percent in
2115 each pattern in the list with @var{name} and perform the above directory
2116 searches using that library filename.  If no library is found, the next
2117 word in the list will be used.
2118
2119 The default value for @code{.LIBPATTERNS} is ``@samp{lib%.so lib%.a}'',
2120 which provides the default behavior described above.
2121
2122 You can turn off link library expansion completely by setting this
2123 variable to an empty value.
2124
2125 @node Phony Targets, Force Targets, Directory Search, Rules
2126 @section Phony Targets
2127 @cindex phony targets
2128 @cindex targets, phony
2129 @cindex targets without a file
2130
2131 A phony target is one that is not really the name of a file.  It is just a
2132 name for some commands to be executed when you make an explicit request.
2133 There are two reasons to use a phony target: to avoid a conflict with
2134 a file of the same name, and to improve performance.
2135
2136 If you write a rule whose commands will not create the target file, the
2137 commands will be executed every time the target comes up for remaking.
2138 Here is an example:
2139
2140 @example
2141 @group
2142 clean:
2143         rm *.o temp
2144 @end group
2145 @end example
2146
2147 @noindent
2148 Because the @code{rm} command does not create a file named @file{clean},
2149 probably no such file will ever exist.  Therefore, the @code{rm} command
2150 will be executed every time you say @samp{make clean}.
2151 @cindex @code{rm} (shell command)
2152
2153 @findex .PHONY
2154 The phony target will cease to work if anything ever does create a file
2155 named @file{clean} in this directory.  Since it has no prerequisites, the
2156 file @file{clean} would inevitably be considered up to date, and its
2157 commands would not be executed.  To avoid this problem, you can explicitly
2158 declare the target to be phony, using the special target @code{.PHONY}
2159 (@pxref{Special Targets, ,Special Built-in Target Names}) as follows:
2160
2161 @example
2162 .PHONY : clean
2163 @end example
2164
2165 @noindent
2166 Once this is done, @samp{make clean} will run the commands regardless of
2167 whether there is a file named @file{clean}.
2168
2169 Since it knows that phony targets do not name actual files that could be
2170 remade from other files, @code{make} skips the implicit rule search for
2171 phony targets (@pxref{Implicit Rules}).  This is why declaring a target
2172 phony is good for performance, even if you are not worried about the
2173 actual file existing.
2174
2175 Thus, you first write the line that states that @code{clean} is a
2176 phony target, then you write the rule, like this:
2177
2178 @example
2179 @group
2180 .PHONY: clean
2181 clean:
2182         rm *.o temp
2183 @end group
2184 @end example
2185
2186 Another example of the usefulness of phony targets is in conjunction
2187 with recursive invocations of @code{make}.  In this case the makefile
2188 will often contain a variable which lists a number of subdirectories to
2189 be built.  One way to handle this is with one rule whose command is a
2190 shell loop over the subdirectories, like this:
2191
2192 @example
2193 @group
2194 SUBDIRS = foo bar baz
2195
2196 subdirs:
2197         for dir in $(SUBDIRS); do \
2198           $(MAKE) -C $$dir; \
2199         done
2200 @end group
2201 @end example
2202
2203 There are a few of problems with this method, however.  First, any error
2204 detected in a submake is not noted by this rule, so it will continue to
2205 build the rest of the directories even when one fails.  This can be
2206 overcome by adding shell commands to note the error and exit, but then
2207 it will do so even if @code{make} is invoked with the @code{-k} option,
2208 which is unfortunate.  Second, and perhaps more importantly, you cannot
2209 take advantage of the parallel build capabilities of make using this
2210 method, since there is only one rule.
2211
2212 By declaring the subdirectories as phony targets (you must do this as
2213 the subdirectory obviously always exists; otherwise it won't be built)
2214 you can remove these problems:
2215
2216 @example
2217 @group
2218 SUBDIRS = foo bar baz
2219
2220 .PHONY: subdirs $(SUBDIRS)
2221
2222 subdirs: $(SUBDIRS)
2223
2224 $(SUBDIRS):
2225         $(MAKE) -C $@
2226
2227 foo: baz
2228 @end group
2229 @end example
2230
2231 Here we've also declared that the @file{foo} subdirectory cannot be
2232 built until after the @file{baz} subdirectory is complete; this kind of
2233 relationship declaration is particularly important when attempting
2234 parallel builds.
2235
2236 A phony target should not be a prerequisite of a real target file; if it
2237 is, its commands are run every time @code{make} goes to update that
2238 file.  As long as a phony target is never a prerequisite of a real
2239 target, the phony target commands will be executed only when the phony
2240 target is a specified goal (@pxref{Goals, ,Arguments to Specify the
2241 Goals}).
2242
2243 Phony targets can have prerequisites.  When one directory contains multiple
2244 programs, it is most convenient to describe all of the programs in one
2245 makefile @file{./Makefile}.  Since the target remade by default will be the
2246 first one in the makefile, it is common to make this a phony target named
2247 @samp{all} and give it, as prerequisites, all the individual programs.  For
2248 example:
2249
2250 @example
2251 all : prog1 prog2 prog3
2252 .PHONY : all
2253
2254 prog1 : prog1.o utils.o
2255         cc -o prog1 prog1.o utils.o
2256
2257 prog2 : prog2.o
2258         cc -o prog2 prog2.o
2259
2260 prog3 : prog3.o sort.o utils.o
2261         cc -o prog3 prog3.o sort.o utils.o
2262 @end example
2263
2264 @noindent
2265 Now you can say just @samp{make} to remake all three programs, or specify
2266 as arguments the ones to remake (as in @samp{make prog1 prog3}).
2267
2268 When one phony target is a prerequisite of another, it serves as a subroutine
2269 of the other.  For example, here @samp{make cleanall} will delete the
2270 object files, the difference files, and the file @file{program}:
2271
2272 @example
2273 .PHONY: cleanall cleanobj cleandiff
2274
2275 cleanall : cleanobj cleandiff
2276         rm program
2277
2278 cleanobj :
2279         rm *.o
2280
2281 cleandiff :
2282         rm *.diff
2283 @end example
2284
2285 @node Force Targets, Empty Targets, Phony Targets, Rules
2286 @section Rules without Commands or Prerequisites
2287 @cindex force targets
2288 @cindex targets, force
2289 @cindex @code{FORCE}
2290 @cindex rule, no commands or prerequisites
2291
2292 If a rule has no prerequisites or commands, and the target of the rule
2293 is a nonexistent file, then @code{make} imagines this target to have
2294 been updated whenever its rule is run.  This implies that all targets
2295 depending on this one will always have their commands run.
2296
2297 An example will illustrate this:
2298
2299 @example
2300 @group
2301 clean: FORCE
2302         rm $(objects)
2303 FORCE:
2304 @end group
2305 @end example
2306
2307 Here the target @samp{FORCE} satisfies the special conditions, so the
2308 target @file{clean} that depends on it is forced to run its commands.
2309 There is nothing special about the name @samp{FORCE}, but that is one name
2310 commonly used this way.
2311
2312 As you can see, using @samp{FORCE} this way has the same results as using
2313 @samp{.PHONY: clean}.
2314
2315 Using @samp{.PHONY} is more explicit and more efficient.  However,
2316 other versions of @code{make} do not support @samp{.PHONY}; thus
2317 @samp{FORCE} appears in many makefiles.  @xref{Phony Targets}.
2318
2319 @node Empty Targets, Special Targets, Force Targets, Rules
2320 @section Empty Target Files to Record Events
2321 @cindex empty targets
2322 @cindex targets, empty
2323 @cindex recording events with empty targets
2324
2325 The @dfn{empty target} is a variant of the phony target; it is used to hold
2326 commands for an action that you request explicitly from time to time.
2327 Unlike a phony target, this target file can really exist; but the file's
2328 contents do not matter, and usually are empty.
2329
2330 The purpose of the empty target file is to record, with its
2331 last-modification time, when the rule's commands were last executed.  It
2332 does so because one of the commands is a @code{touch} command to update the
2333 target file.
2334
2335 The empty target file should have some prerequisites (otherwise it
2336 doesn't make sense).  When you ask to remake the empty target, the
2337 commands are executed if any prerequisite is more recent than the target;
2338 in other words, if a prerequisite has changed since the last time you
2339 remade the target.  Here is an example:
2340
2341 @example
2342 print: foo.c bar.c
2343         lpr -p $?
2344         touch print
2345 @end example
2346 @cindex @code{print} target
2347 @cindex @code{lpr} (shell command)
2348 @cindex @code{touch} (shell command)
2349
2350 @noindent
2351 With this rule, @samp{make print} will execute the @code{lpr} command if
2352 either source file has changed since the last @samp{make print}.  The
2353 automatic variable @samp{$?} is used to print only those files that have
2354 changed (@pxref{Automatic, ,Automatic Variables}).
2355
2356 @node Special Targets, Multiple Targets, Empty Targets, Rules
2357 @section Special Built-in Target Names
2358 @cindex special targets
2359 @cindex built-in special targets
2360 @cindex targets, built-in special
2361
2362 Certain names have special meanings if they appear as targets.
2363
2364 @table @code
2365 @findex .PHONY
2366 @item .PHONY
2367
2368 The prerequisites of the special target @code{.PHONY} are considered to
2369 be phony targets.  When it is time to consider such a target,
2370 @code{make} will run its commands unconditionally, regardless of
2371 whether a file with that name exists or what its last-modification
2372 time is.  @xref{Phony Targets, ,Phony Targets}.
2373
2374 @findex .SUFFIXES
2375 @item .SUFFIXES
2376
2377 The prerequisites of the special target @code{.SUFFIXES} are the list
2378 of suffixes to be used in checking for suffix rules.
2379 @xref{Suffix Rules, , Old-Fashioned Suffix Rules}.
2380
2381 @findex .DEFAULT
2382 @item .DEFAULT
2383
2384 The commands specified for @code{.DEFAULT} are used for any target for
2385 which no rules are found (either explicit rules or implicit rules).
2386 @xref{Last Resort}.  If @code{.DEFAULT} commands are specified, every
2387 file mentioned as a prerequisite, but not as a target in a rule, will have
2388 these commands executed on its behalf.  @xref{Implicit Rule Search,
2389 ,Implicit Rule Search Algorithm}.
2390
2391 @findex .PRECIOUS
2392 @item .PRECIOUS
2393 @cindex precious targets
2394 @cindex preserving with @code{.PRECIOUS}
2395
2396 The targets which @code{.PRECIOUS} depends on are given the following
2397 special treatment: if @code{make} is killed or interrupted during the
2398 execution of their commands, the target is not deleted.
2399 @xref{Interrupts, ,Interrupting or Killing @code{make}}.
2400 Also, if the target is an intermediate file, it will not be deleted
2401 after it is no longer needed, as is normally done.
2402 @xref{Chained Rules, ,Chains of Implicit Rules}.
2403
2404 You can also list the target pattern of an implicit rule (such as
2405 @samp{%.o}) as a prerequisite file of the special target @code{.PRECIOUS}
2406 to preserve intermediate files created by rules whose target patterns
2407 match that file's name.
2408
2409 @findex .INTERMEDIATE
2410 @item .INTERMEDIATE
2411 @cindex intermediate targets, explicit
2412
2413 The targets which @code{.INTERMEDIATE} depends on are treated as
2414 intermediate files.  @xref{Chained Rules, ,Chains of Implicit Rules}.
2415 @code{.INTERMEDIATE} with no prerequisites has no effect.
2416
2417 @findex .SECONDARY
2418 @item .SECONDARY
2419 @cindex secondary targets
2420 @cindex preserving with @code{.SECONDARY}
2421
2422 The targets which @code{.SECONDARY} depends on are treated as
2423 intermediate files, except that they are never automatically deleted.
2424 @xref{Chained Rules, ,Chains of Implicit Rules}.
2425
2426 @code{.SECONDARY} with no prerequisites marks all file targets mentioned
2427 in the makefile as secondary.
2428
2429 @findex .DELETE_ON_ERROR
2430 @item .DELETE_ON_ERROR
2431 @cindex removing targets on failure
2432
2433 If @code{.DELETE_ON_ERROR} is mentioned as a target anywhere in the
2434 makefile, then @code{make} will delete the target of a rule if it has
2435 changed and its commands exit with a nonzero exit status, just as it
2436 does when it receives a signal.  @xref{Errors, ,Errors in Commands}.
2437
2438 @findex .IGNORE
2439 @item .IGNORE
2440
2441 If you specify prerequisites for @code{.IGNORE}, then @code{make} will
2442 ignore errors in execution of the commands run for those particular
2443 files.  The commands for @code{.IGNORE} are not meaningful.
2444
2445 If mentioned as a target with no prerequisites, @code{.IGNORE} says to
2446 ignore errors in execution of commands for all files.  This usage of
2447 @samp{.IGNORE} is supported only for historical compatibility.  Since
2448 this affects every command in the makefile, it is not very useful; we
2449 recommend you use the more selective ways to ignore errors in specific
2450 commands.  @xref{Errors, ,Errors in Commands}.
2451
2452 @findex .SILENT
2453 @item .SILENT
2454
2455 If you specify prerequisites for @code{.SILENT}, then @code{make} will
2456 not print the commands to remake those particular files before executing
2457 them.  The commands for @code{.SILENT} are not meaningful.
2458
2459 If mentioned as a target with no prerequisites, @code{.SILENT} says not
2460 to print any commands before executing them.  This usage of
2461 @samp{.SILENT} is supported only for historical compatibility.  We
2462 recommend you use the more selective ways to silence specific commands.
2463 @xref{Echoing, ,Command Echoing}.  If you want to silence all commands
2464 for a particular run of @code{make}, use the @samp{-s} or
2465 @w{@samp{--silent}} option (@pxref{Options Summary}).
2466
2467 @findex .EXPORT_ALL_VARIABLES
2468 @item .EXPORT_ALL_VARIABLES
2469
2470 Simply by being mentioned as a target, this tells @code{make} to
2471 export all variables to child processes by default.
2472 @xref{Variables/Recursion, ,Communicating Variables to a
2473 Sub-@code{make}}.
2474
2475 @findex .NOTPARALLEL
2476 @item .NOTPARALLEL
2477 @cindex parallel execution, overriding
2478
2479 If @code{.NOTPARALLEL} is mentioned as a target, then this invocation of
2480 @code{make} will be run serially, even if the @samp{-j} option is
2481 given.  Any recursively invoked @code{make} command will still be run in
2482 parallel (unless its makefile contains this target).  Any prerequisites
2483 on this target are ignored.
2484 @end table
2485
2486 Any defined implicit rule suffix also counts as a special target if it
2487 appears as a target, and so does the concatenation of two suffixes, such
2488 as @samp{.c.o}.  These targets are suffix rules, an obsolete way of
2489 defining implicit rules (but a way still widely used).  In principle, any
2490 target name could be special in this way if you break it in two and add
2491 both pieces to the suffix list.  In practice, suffixes normally begin with
2492 @samp{.}, so these special target names also begin with @samp{.}.
2493 @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
2494
2495 @node Multiple Targets, Multiple Rules, Special Targets, Rules
2496 @section Multiple Targets in a Rule
2497 @cindex multiple targets
2498 @cindex several targets in a rule
2499 @cindex targets, multiple
2500 @cindex rule, with multiple targets
2501
2502 A rule with multiple targets is equivalent to writing many rules, each with
2503 one target, and all identical aside from that.  The same commands apply to
2504 all the targets, but their effects may vary because you can substitute the
2505 actual target name into the command using @samp{$@@}.  The rule contributes
2506 the same prerequisites to all the targets also.
2507
2508 This is useful in two cases.
2509
2510 @itemize @bullet
2511 @item
2512 You want just prerequisites, no commands.  For example:
2513
2514 @example
2515 kbd.o command.o files.o: command.h
2516 @end example
2517
2518 @noindent
2519 gives an additional prerequisite to each of the three object files
2520 mentioned.
2521
2522 @item
2523 Similar commands work for all the targets.  The commands do not need
2524 to be absolutely identical, since the automatic variable @samp{$@@}
2525 can be used to substitute the particular target to be remade into the
2526 commands (@pxref{Automatic, ,Automatic Variables}).  For example:
2527
2528 @example
2529 @group
2530 bigoutput littleoutput : text.g
2531         generate text.g -$(subst output,,$@@) > $@@
2532 @end group
2533 @end example
2534 @findex subst
2535
2536 @noindent
2537 is equivalent to
2538
2539 @example
2540 bigoutput : text.g
2541         generate text.g -big > bigoutput
2542 littleoutput : text.g
2543         generate text.g -little > littleoutput
2544 @end example
2545
2546 @noindent
2547 Here we assume the hypothetical program @code{generate} makes two
2548 types of output, one if given @samp{-big} and one if given
2549 @samp{-little}.
2550 @xref{Text Functions, ,Functions for String Substitution and Analysis},
2551 for an explanation of the @code{subst} function.
2552 @end itemize
2553
2554 Suppose you would like to vary the prerequisites according to the target,
2555 much as the variable @samp{$@@} allows you to vary the commands.
2556 You cannot do this with multiple targets in an ordinary rule, but you can
2557 do it with a @dfn{static pattern rule}.
2558 @xref{Static Pattern, ,Static Pattern Rules}.
2559
2560 @node Multiple Rules, Static Pattern, Multiple Targets, Rules
2561 @section Multiple Rules for One Target
2562 @cindex multiple rules for one target
2563 @cindex several rules for one target
2564 @cindex rule, multiple for one target
2565 @cindex target, multiple rules for one
2566
2567 One file can be the target of several rules.  All the prerequisites
2568 mentioned in all the rules are merged into one list of prerequisites for
2569 the target.  If the target is older than any prerequisite from any rule,
2570 the commands are executed.
2571
2572 There can only be one set of commands to be executed for a file.
2573 If more than one rule gives commands for the same file,
2574 @code{make} uses the last set given and prints an error message.
2575 (As a special case, if the file's name begins with a dot, no
2576 error message is printed.  This odd behavior is only for
2577 compatibility with other implementations of @code{make}.)
2578 There is no reason to
2579 write your makefiles this way; that is why @code{make} gives you
2580 an error message.@refill
2581
2582 An extra rule with just prerequisites can be used to give a few extra
2583 prerequisites to many files at once.  For example, one usually has a
2584 variable named @code{objects} containing a list of all the compiler output
2585 files in the system being made.  An easy way to say that all of them must
2586 be recompiled if @file{config.h} changes is to write the following:
2587
2588 @example
2589 objects = foo.o bar.o
2590 foo.o : defs.h
2591 bar.o : defs.h test.h
2592 $(objects) : config.h
2593 @end example
2594
2595 This could be inserted or taken out without changing the rules that really
2596 specify how to make the object files, making it a convenient form to use if
2597 you wish to add the additional prerequisite intermittently.
2598
2599 Another wrinkle is that the additional prerequisites could be specified with
2600 a variable that you set with a command argument to @code{make}
2601 (@pxref{Overriding, ,Overriding Variables}).  For example,
2602
2603 @example
2604 @group
2605 extradeps=
2606 $(objects) : $(extradeps)
2607 @end group
2608 @end example
2609
2610 @noindent
2611 means that the command @samp{make extradeps=foo.h} will consider
2612 @file{foo.h} as a prerequisite of each object file, but plain @samp{make}
2613 will not.
2614
2615 If none of the explicit rules for a target has commands, then @code{make}
2616 searches for an applicable implicit rule to find some commands
2617 @pxref{Implicit Rules, ,Using Implicit Rules}).
2618
2619 @node Static Pattern, Double-Colon, Multiple Rules, Rules
2620 @section Static Pattern Rules
2621 @cindex static pattern rule
2622 @cindex rule, static pattern
2623 @cindex pattern rules, static (not implicit)
2624 @cindex varying prerequisites
2625 @cindex prerequisites, varying (static pattern)
2626
2627 @dfn{Static pattern rules} are rules which specify multiple targets and
2628 construct the prerequisite names for each target based on the target name.
2629 They are more general than ordinary rules with multiple targets because the
2630 targets do not have to have identical prerequisites.  Their prerequisites must
2631 be @emph{analogous}, but not necessarily @emph{identical}.
2632
2633 @menu
2634 * Static Usage::                The syntax of static pattern rules.
2635 * Static versus Implicit::      When are they better than implicit rules?
2636 @end menu
2637
2638 @node Static Usage, Static versus Implicit,  , Static Pattern
2639 @subsection Syntax of Static Pattern Rules
2640 @cindex static pattern rule, syntax of
2641 @cindex pattern rules, static, syntax of
2642
2643 Here is the syntax of a static pattern rule:
2644
2645 @example
2646 @var{targets} @dots{}: @var{target-pattern}: @var{dep-patterns} @dots{}
2647         @var{commands}
2648         @dots{}
2649 @end example
2650
2651 @noindent
2652 The @var{targets} list specifies the targets that the rule applies to.
2653 The targets can contain wildcard characters, just like the targets of
2654 ordinary rules (@pxref{Wildcards, ,Using Wildcard Characters in File
2655 Names}).
2656
2657 @cindex target pattern, static (not implicit)
2658 @cindex stem
2659 The @var{target-pattern} and @var{dep-patterns} say how to compute the
2660 prerequisites of each target.  Each target is matched against the
2661 @var{target-pattern} to extract a part of the target name, called the
2662 @dfn{stem}.  This stem is substituted into each of the @var{dep-patterns}
2663 to make the prerequisite names (one from each @var{dep-pattern}).
2664
2665 Each pattern normally contains the character @samp{%} just once.  When the
2666 @var{target-pattern} matches a target, the @samp{%} can match any part of
2667 the target name; this part is called the @dfn{stem}.  The rest of the
2668 pattern must match exactly.  For example, the target @file{foo.o} matches
2669 the pattern @samp{%.o}, with @samp{foo} as the stem.  The targets
2670 @file{foo.c} and @file{foo.out} do not match that pattern.@refill
2671
2672 @cindex prerequisite pattern, static (not implicit)
2673 The prerequisite names for each target are made by substituting the stem
2674 for the @samp{%} in each prerequisite pattern.  For example, if one
2675 prerequisite pattern is @file{%.c}, then substitution of the stem
2676 @samp{foo} gives the prerequisite name @file{foo.c}.  It is legitimate
2677 to write a prerequisite pattern that does not contain @samp{%}; then this
2678 prerequisite is the same for all targets.
2679
2680 @cindex @code{%}, quoting in static pattern
2681 @cindex @code{%}, quoting with @code{\} (backslash)
2682 @cindex @code{\} (backslash), to quote @code{%}
2683 @cindex backslash (@code{\}), to quote @code{%}
2684 @cindex quoting @code{%}, in static pattern
2685 @samp{%} characters in pattern rules can be quoted with preceding
2686 backslashes (@samp{\}).  Backslashes that would otherwise quote @samp{%}
2687 characters can be quoted with more backslashes.  Backslashes that quote
2688 @samp{%} characters or other backslashes are removed from the pattern
2689 before it is compared to file names or has a stem substituted into it.
2690 Backslashes that are not in danger of quoting @samp{%} characters go
2691 unmolested.  For example, the pattern @file{the\%weird\\%pattern\\} has
2692 @samp{the%weird\} preceding the operative @samp{%} character, and
2693 @samp{pattern\\} following it.  The final two backslashes are left alone
2694 because they cannot affect any @samp{%} character.@refill
2695
2696 Here is an example, which compiles each of @file{foo.o} and @file{bar.o}
2697 from the corresponding @file{.c} file:
2698
2699 @example
2700 @group
2701 objects = foo.o bar.o
2702
2703 all: $(objects)
2704
2705 $(objects): %.o: %.c
2706         $(CC) -c $(CFLAGS) $< -o $@@
2707 @end group
2708 @end example
2709
2710 @noindent
2711 Here @samp{$<} is the automatic variable that holds the name of the
2712 prerequisite and @samp{$@@} is the automatic variable that holds the name
2713 of the target; see @ref{Automatic, , Automatic Variables}.
2714
2715 Each target specified must match the target pattern; a warning is issued
2716 for each target that does not.  If you have a list of files, only some of
2717 which will match the pattern, you can use the @code{filter} function to
2718 remove nonmatching file names (@pxref{Text Functions, ,Functions for String Substitution and Analysis}):
2719
2720 @example
2721 files = foo.elc bar.o lose.o
2722
2723 $(filter %.o,$(files)): %.o: %.c
2724         $(CC) -c $(CFLAGS) $< -o $@@
2725 $(filter %.elc,$(files)): %.elc: %.el
2726         emacs -f batch-byte-compile $<
2727 @end example
2728
2729 @noindent
2730 In this example the result of @samp{$(filter %.o,$(files))} is
2731 @file{bar.o lose.o}, and the first static pattern rule causes each of
2732 these object files to be updated by compiling the corresponding C source
2733 file.  The result of @w{@samp{$(filter %.elc,$(files))}} is
2734 @file{foo.elc}, so that file is made from @file{foo.el}.@refill
2735
2736 Another example shows how to use @code{$*} in static pattern rules:
2737 @vindex $*@r{, and static pattern}
2738
2739 @example
2740 @group
2741 bigoutput littleoutput : %output : text.g
2742         generate text.g -$* > $@@
2743 @end group
2744 @end example
2745
2746 @noindent
2747 When the @code{generate} command is run, @code{$*} will expand to the
2748 stem, either @samp{big} or @samp{little}.
2749
2750 @node Static versus Implicit,  , Static Usage, Static Pattern
2751 @subsection Static Pattern Rules versus Implicit Rules
2752 @cindex rule, static pattern versus implicit
2753 @cindex static pattern rule, versus implicit
2754
2755 A static pattern rule has much in common with an implicit rule defined as a
2756 pattern rule (@pxref{Pattern Rules, ,Defining and Redefining Pattern Rules}).
2757 Both have a pattern for the target and patterns for constructing the
2758 names of prerequisites.  The difference is in how @code{make} decides
2759 @emph{when} the rule applies.
2760
2761 An implicit rule @emph{can} apply to any target that matches its pattern,
2762 but it @emph{does} apply only when the target has no commands otherwise
2763 specified, and only when the prerequisites can be found.  If more than one
2764 implicit rule appears applicable, only one applies; the choice depends on
2765 the order of rules.
2766
2767 By contrast, a static pattern rule applies to the precise list of targets
2768 that you specify in the rule.  It cannot apply to any other target and it
2769 invariably does apply to each of the targets specified.  If two conflicting
2770 rules apply, and both have commands, that's an error.
2771
2772 The static pattern rule can be better than an implicit rule for these
2773 reasons:
2774
2775 @itemize @bullet
2776 @item
2777 You may wish to override the usual implicit rule for a few
2778 files whose names cannot be categorized syntactically but
2779 can be given in an explicit list.
2780
2781 @item
2782 If you cannot be sure of the precise contents of the directories
2783 you are using, you may not be sure which other irrelevant files
2784 might lead @code{make} to use the wrong implicit rule.  The choice
2785 might depend on the order in which the implicit rule search is done.
2786 With static pattern rules, there is no uncertainty: each rule applies
2787 to precisely the targets specified.
2788 @end itemize
2789
2790 @node Double-Colon, Automatic Prerequisites, Static Pattern, Rules
2791 @section Double-Colon Rules
2792 @cindex double-colon rules
2793 @cindex rule, double-colon (@code{::})
2794 @cindex multiple rules for one target (@code{::})
2795 @cindex @code{::} rules (double-colon)
2796
2797 @dfn{Double-colon} rules are rules written with @samp{::} instead of
2798 @samp{:} after the target names.  They are handled differently from
2799 ordinary rules when the same target appears in more than one rule.
2800
2801 When a target appears in multiple rules, all the rules must be the same
2802 type: all ordinary, or all double-colon.  If they are double-colon, each of
2803 them is independent of the others.  Each double-colon rule's commands are
2804 executed if the target is older than any prerequisites of that rule.  This
2805 can result in executing none, any, or all of the double-colon rules.
2806
2807 Double-colon rules with the same target are in fact completely separate
2808 from one another.  Each double-colon rule is processed individually, just
2809 as rules with different targets are processed.
2810
2811 The double-colon rules for a target are executed in the order they appear
2812 in the makefile.  However, the cases where double-colon rules really make
2813 sense are those where the order of executing the commands would not matter.
2814
2815 Double-colon rules are somewhat obscure and not often very useful; they
2816 provide a mechanism for cases in which the method used to update a target
2817 differs depending on which prerequisite files caused the update, and such
2818 cases are rare.
2819
2820 Each double-colon rule should specify commands; if it does not, an
2821 implicit rule will be used if one applies.
2822 @xref{Implicit Rules, ,Using Implicit Rules}.
2823
2824 @node Automatic Prerequisites,  , Double-Colon, Rules
2825 @section Generating Prerequisites Automatically
2826 @cindex prerequisites, automatic generation
2827 @cindex automatic generation of prerequisites
2828 @cindex generating prerequisites automatically
2829
2830 In the makefile for a program, many of the rules you need to write often
2831 say only that some object file depends on some header
2832 file.  For example, if @file{main.c} uses @file{defs.h} via an
2833 @code{#include}, you would write:
2834
2835 @example
2836 main.o: defs.h
2837 @end example
2838
2839 @noindent
2840 You need this rule so that @code{make} knows that it must remake
2841 @file{main.o} whenever @file{defs.h} changes.  You can see that for a
2842 large program you would have to write dozens of such rules in your
2843 makefile.  And, you must always be very careful to update the makefile
2844 every time you add or remove an @code{#include}.
2845 @cindex @code{#include}
2846
2847 @cindex @code{-M} (to compiler)
2848 To avoid this hassle, most modern C compilers can write these rules for
2849 you, by looking at the @code{#include} lines in the source files.
2850 Usually this is done with the @samp{-M} option to the compiler.
2851 For example, the command:
2852
2853 @example
2854 cc -M main.c
2855 @end example
2856
2857 @noindent
2858 generates the output:
2859
2860 @example
2861 main.o : main.c defs.h
2862 @end example
2863
2864 @noindent
2865 Thus you no longer have to write all those rules yourself.
2866 The compiler will do it for you.
2867
2868 Note that such a prerequisite constitutes mentioning @file{main.o} in a
2869 makefile, so it can never be considered an intermediate file by implicit
2870 rule search.  This means that @code{make} won't ever remove the file
2871 after using it; @pxref{Chained Rules, ,Chains of Implicit Rules}.
2872
2873 @cindex @code{make depend}
2874 With old @code{make} programs, it was traditional practice to use this
2875 compiler feature to generate prerequisites on demand with a command like
2876 @samp{make depend}.  That command would create a file @file{depend}
2877 containing all the automatically-generated prerequisites; then the
2878 makefile could use @code{include} to read them in (@pxref{Include}).
2879
2880 In GNU @code{make}, the feature of remaking makefiles makes this
2881 practice obsolete---you need never tell @code{make} explicitly to
2882 regenerate the prerequisites, because it always regenerates any makefile
2883 that is out of date.  @xref{Remaking Makefiles}.
2884
2885 The practice we recommend for automatic prerequisite generation is to have
2886 one makefile corresponding to each source file.  For each source file
2887 @file{@var{name}.c} there is a makefile @file{@var{name}.d} which lists
2888 what files the object file @file{@var{name}.o} depends on.  That way
2889 only the source files that have changed need to be rescanned to produce
2890 the new prerequisites.
2891
2892 Here is the pattern rule to generate a file of prerequisites (i.e., a makefile)
2893 called @file{@var{name}.d} from a C source file called @file{@var{name}.c}:
2894
2895 @smallexample
2896 @group
2897 %.d: %.c
2898         set -e; $(CC) -M $(CPPFLAGS) $< \
2899                   | sed 's/\($*\)\.o[ :]*/\1.o $@@ : /g' > $@@; \
2900                 [ -s $@@ ] || rm -f $@@
2901 @end group
2902 @end smallexample
2903
2904 @noindent
2905 @xref{Pattern Rules}, for information on defining pattern rules.  The
2906 @samp{-e} flag to the shell makes it exit immediately if the
2907 @code{$(CC)} command fails (exits with a nonzero status).  Normally the
2908 shell exits with the status of the last command in the pipeline
2909 (@code{sed} in this case), so @code{make} would not notice a nonzero
2910 status from the compiler.
2911 @cindex @code{-e} (shell flag)
2912
2913 @cindex @code{-MM} (to GNU compiler)
2914 With the GNU C compiler, you may wish to use the @samp{-MM} flag instead
2915 of @samp{-M}.  This omits prerequisites on system header files.
2916 @xref{Preprocessor Options, , Options Controlling the Preprocessor,
2917 gcc.info, Using GNU CC}, for details.
2918
2919 @cindex @code{sed} (shell command)
2920 The purpose of the @code{sed} command is to translate (for example):
2921
2922 @example
2923 main.o : main.c defs.h
2924 @end example
2925
2926 @noindent
2927 into:
2928
2929 @example
2930 main.o main.d : main.c defs.h
2931 @end example
2932
2933 @noindent
2934 @cindex @code{.d}
2935 This makes each @samp{.d} file depend on all the source and header files
2936 that the corresponding @samp{.o} file depends on.  @code{make} then
2937 knows it must regenerate the prerequisites whenever any of the source or
2938 header files changes.
2939
2940 Once you've defined the rule to remake the @samp{.d} files,
2941 you then use the @code{include} directive to read them all in.
2942 @xref{Include}.  For example:
2943
2944 @example
2945 @group
2946 sources = foo.c bar.c
2947
2948 include $(sources:.c=.d)
2949 @end group
2950 @end example
2951
2952 @noindent
2953 (This example uses a substitution variable reference to translate the
2954 list of source files @samp{foo.c bar.c} into a list of prerequisite
2955 makefiles, @samp{foo.d bar.d}.  @xref{Substitution Refs}, for full
2956 information on substitution references.)  Since the @samp{.d} files are
2957 makefiles like any others, @code{make} will remake them as necessary
2958 with no further work from you.  @xref{Remaking Makefiles}.
2959
2960 @node Commands, Using Variables, Rules, Top
2961 @chapter Writing the Commands in Rules
2962 @cindex commands, how to write
2963 @cindex rule commands
2964 @cindex writing rule commands
2965
2966 The commands of a rule consist of shell command lines to be executed one
2967 by one.  Each command line must start with a tab, except that the first
2968 command line may be attached to the target-and-prerequisites line with a
2969 semicolon in between.  Blank lines and lines of just comments may appear
2970 among the command lines; they are ignored.  (But beware, an apparently
2971 ``blank'' line that begins with a tab is @emph{not} blank!  It is an
2972 empty command; @pxref{Empty Commands}.)
2973
2974 Users use many different shell programs, but commands in makefiles are
2975 always interpreted by @file{/bin/sh} unless the makefile specifies
2976 otherwise.  @xref{Execution, ,Command Execution}.
2977
2978 @cindex comments, in commands
2979 @cindex commands, comments in
2980 @cindex @code{#} (comments), in commands
2981 The shell that is in use determines whether comments can be written on
2982 command lines, and what syntax they use.  When the shell is
2983 @file{/bin/sh}, a @samp{#} starts a comment that extends to the end of
2984 the line.  The @samp{#} does not have to be at the beginning of a line.
2985 Text on a line before a @samp{#} is not part of the comment.
2986
2987 @menu
2988 * Echoing::                     How to control when commands are echoed.
2989 * Execution::                   How commands are executed.
2990 * Parallel::                    How commands can be executed in parallel.
2991 * Errors::                      What happens after a command execution error.
2992 * Interrupts::                  What happens when a command is interrupted.
2993 * Recursion::                   Invoking @code{make} from makefiles.
2994 * Sequences::                   Defining canned sequences of commands.
2995 * Empty Commands::              Defining useful, do-nothing commands.
2996 @end menu
2997
2998 @node Echoing, Execution,  , Commands
2999 @section Command Echoing
3000 @cindex echoing of commands
3001 @cindex silent operation
3002 @cindex @code{@@} (in commands)
3003 @cindex commands, echoing
3004 @cindex printing of commands
3005
3006 Normally @code{make} prints each command line before it is executed.
3007 We call this @dfn{echoing} because it gives the appearance that you
3008 are typing the commands yourself.
3009
3010 When a line starts with @samp{@@}, the echoing of that line is suppressed.
3011 The @samp{@@} is discarded before the command is passed to the shell.
3012 Typically you would use this for a command whose only effect is to print
3013 something, such as an @code{echo} command to indicate progress through
3014 the makefile:
3015
3016 @example
3017 @@echo About to make distribution files
3018 @end example
3019
3020 @cindex @code{-n}
3021 @cindex @code{--just-print}
3022 @cindex @code{--dry-run}
3023 @cindex @code{--recon}
3024 When @code{make} is given the flag @samp{-n} or @samp{--just-print}
3025 it only echoes commands, it won't execute them.  @xref{Options Summary,
3026 ,Summary of Options}.  In this case and only this case, even the
3027 commands starting with @samp{@@} are printed.  This flag is useful for
3028 finding out which commands @code{make} thinks are necessary without
3029 actually doing them.
3030
3031 @cindex @code{-s}
3032 @cindex @code{--silent}
3033 @cindex @code{--quiet}
3034 @findex .SILENT
3035 The @samp{-s} or @samp{--silent}
3036 flag to @code{make} prevents all echoing, as if all commands
3037 started with @samp{@@}.  A rule in the makefile for the special target
3038 @code{.SILENT} without prerequisites has the same effect
3039 (@pxref{Special Targets, ,Special Built-in Target Names}).
3040 @code{.SILENT} is essentially obsolete since @samp{@@} is more flexible.@refill
3041
3042 @node Execution, Parallel, Echoing, Commands
3043 @section Command Execution
3044 @cindex commands, execution
3045 @cindex execution, of commands
3046 @cindex shell command, execution
3047 @vindex SHELL @r{(command execution)}
3048
3049 When it is time to execute commands to update a target, they are executed
3050 by making a new subshell for each line.  (In practice, @code{make} may
3051 take shortcuts that do not affect the results.)
3052
3053 @cindex @code{cd} (shell command)
3054 @strong{Please note:} this implies that shell commands such as @code{cd}
3055 that set variables local to each process will not affect the following
3056 command lines. @footnote{On MS-DOS, the value of current working
3057 directory is @strong{global}, so changing it @emph{will} affect the
3058 following command lines on those systems.}  If you want to use @code{cd}
3059 to affect the next command, put the two on a single line with a
3060 semicolon between them.  Then @code{make} will consider them a single
3061 command and pass them, together, to a shell which will execute them in
3062 sequence.  For example:
3063
3064 @example
3065 foo : bar/lose
3066         cd bar; gobble lose > ../foo
3067 @end example
3068
3069 @cindex commands, backslash (@code{\}) in
3070 @cindex commands, quoting newlines in
3071 @cindex backslash (@code{\}), in commands
3072 @cindex @code{\} (backslash), in commands
3073 @cindex quoting newline, in commands
3074 @cindex newline, quoting, in commands
3075 If you would like to split a single shell command into multiple lines of
3076 text, you must use a backslash at the end of all but the last subline.
3077 Such a sequence of lines is combined into a single line, by deleting the
3078 backslash-newline sequences, before passing it to the shell.  Thus, the
3079 following is equivalent to the preceding example:
3080
3081 @example
3082 @group
3083 foo : bar/lose
3084         cd bar;  \
3085         gobble lose > ../foo
3086 @end group
3087 @end example
3088
3089 @vindex SHELL
3090 The program used as the shell is taken from the variable @code{SHELL}.
3091 By default, the program @file{/bin/sh} is used.
3092
3093 @vindex COMSPEC
3094 On MS-DOS, if @code{SHELL} is not set, the value of the variable
3095 @code{COMSPEC} (which is always set) is used instead.
3096
3097 @cindex @code{SHELL}, MS-DOS specifics
3098 The processing of lines that set the variable @code{SHELL} in Makefiles
3099 is different on MS-DOS.  The stock shell, @file{command.com}, is
3100 ridiculously limited in its functionality and many users of @code{make}
3101 tend to install a replacement shell.  Therefore, on MS-DOS, @code{make}
3102 examines the value of @code{SHELL}, and changes its behavior based on
3103 whether it points to a Unix-style or DOS-style shell.  This allows
3104 reasonable functionality even if @code{SHELL} points to
3105 @file{command.com}.
3106
3107 If @code{SHELL} points to a Unix-style shell, @code{make} on MS-DOS
3108 additionally checks whether that shell can indeed be found; if not, it
3109 ignores the line that sets @code{SHELL}.  In MS-DOS, GNU @code{make}
3110 searches for the shell in the following places:
3111
3112 @enumerate
3113 @item
3114 In the precise place pointed to by the value of @code{SHELL}.  For
3115 example, if the makefile specifies @samp{SHELL = /bin/sh}, @code{make}
3116 will look in the directory @file{/bin} on the current drive.
3117
3118 @item
3119 In the current directory.
3120
3121 @item
3122 In each of the directories in the @code{PATH} variable, in order.
3123
3124 @end enumerate
3125
3126 In every directory it examines, @code{make} will first look for the
3127 specific file (@file{sh} in the example above).  If this is not found,
3128 it will also look in that directory for that file with one of the known
3129 extensions which identify executable files.  For example @file{.exe},
3130 @file{.com}, @file{.bat}, @file{.btm}, @file{.sh}, and some others.
3131
3132 If any of these attempts is successful, the value of @code{SHELL} will
3133 be set to the full pathname of the shell as found.  However, if none of
3134 these is found, the value of @code{SHELL} will not be changed, and thus
3135 the line that sets it will be effectively ignored.  This is so
3136 @code{make} will only support features specific to a Unix-style shell if
3137 such a shell is actually installed on the system where @code{make} runs.
3138
3139 Note that this extended search for the shell is limited to the cases
3140 where @code{SHELL} is set from the Makefile; if it is set in the
3141 environment or command line, you are expected to set it to the full
3142 pathname of the shell, exactly as things are on Unix.
3143
3144 The effect of the above DOS-specific processing is that a Makefile that
3145 says @samp{SHELL = /bin/sh} (as many Unix makefiles do), will work
3146 on MS-DOS unaltered if you have e.g. @file{sh.exe} installed in some
3147 directory along your @code{PATH}.
3148
3149 @cindex environment, @code{SHELL} in
3150 Unlike most variables, the variable @code{SHELL} is never set from the
3151 environment.  This is because the @code{SHELL} environment variable is
3152 used to specify your personal choice of shell program for interactive
3153 use.  It would be very bad for personal choices like this to affect the
3154 functioning of makefiles.  @xref{Environment, ,Variables from the
3155 Environment}.  However, on MS-DOS and MS-Windows the value of
3156 @code{SHELL} in the environment @strong{is} used, since on those systems
3157 most users do not set this variable, and therefore it is most likely set
3158 specifically to be used by @code{make}.  On MS-DOS, if the setting of
3159 @code{SHELL} is not suitable for @code{make}, you can set the variable
3160 @code{MAKESHELL} to the shell that @code{make} should use; this will
3161 override the value of @code{SHELL}.
3162
3163 @node Parallel, Errors, Execution, Commands
3164 @section Parallel Execution
3165 @cindex commands, execution in parallel
3166 @cindex parallel execution
3167 @cindex execution, in parallel
3168 @cindex job slots
3169 @cindex @code{-j}
3170 @cindex @code{--jobs}
3171
3172 GNU @code{make} knows how to execute several commands at once.
3173 Normally, @code{make} will execute only one command at a time, waiting
3174 for it to finish before executing the next.  However, the @samp{-j} or
3175 @samp{--jobs} option tells @code{make} to execute many commands
3176 simultaneously.@refill
3177
3178 On MS-DOS, the @samp{-j} option has no effect, since that system doesn't
3179 support multi-processing.
3180
3181 If the @samp{-j} option is followed by an integer, this is the number of
3182 commands to execute at once; this is called the number of @dfn{job slots}.
3183 If there is nothing looking like an integer after the @samp{-j} option,
3184 there is no limit on the number of job slots.  The default number of job
3185 slots is one, which means serial execution (one thing at a time).
3186
3187 One unpleasant consequence of running several commands simultaneously is
3188 that output generated by the commands appears whenever each command
3189 sends it, so messages from different commands may be interspersed.
3190
3191 Another problem is that two processes cannot both take input from the
3192 same device; so to make sure that only one command tries to take input
3193 from the terminal at once, @code{make} will invalidate the standard
3194 input streams of all but one running command.  This means that
3195 attempting to read from standard input will usually be a fatal error (a
3196 @samp{Broken pipe} signal) for most child processes if there are
3197 several.
3198 @cindex broken pipe
3199 @cindex standard input
3200
3201 It is unpredictable which command will have a valid standard input stream
3202 (which will come from the terminal, or wherever you redirect the standard
3203 input of @code{make}).  The first command run will always get it first, and
3204 the first command started after that one finishes will get it next, and so
3205 on.
3206
3207 We will change how this aspect of @code{make} works if we find a better
3208 alternative.  In the mean time, you should not rely on any command using
3209 standard input at all if you are using the parallel execution feature; but
3210 if you are not using this feature, then standard input works normally in
3211 all commands.
3212
3213 Finally, handling recursive @code{make} invocations raises issues.  For
3214 more information on this, see
3215 @ref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
3216
3217 If a command fails (is killed by a signal or exits with a nonzero
3218 status), and errors are not ignored for that command
3219 (@pxref{Errors, ,Errors in Commands}),
3220 the remaining command lines to remake the same target will not be run.
3221 If a command fails and the @samp{-k} or @samp{--keep-going}
3222 option was not given
3223 (@pxref{Options Summary, ,Summary of Options}),
3224 @code{make} aborts execution.  If make
3225 terminates for any reason (including a signal) with child processes
3226 running, it waits for them to finish before actually exiting.@refill
3227
3228 @cindex load average
3229 @cindex limiting jobs based on load
3230 @cindex jobs, limiting based on load
3231 @cindex @code{-l} (load average)
3232 @cindex @code{--max-load}
3233 @cindex @code{--load-average}
3234 When the system is heavily loaded, you will probably want to run fewer jobs
3235 than when it is lightly loaded.  You can use the @samp{-l} option to tell
3236 @code{make} to limit the number of jobs to run at once, based on the load
3237 average.  The @samp{-l} or @samp{--max-load}
3238 option is followed by a floating-point number.  For
3239 example,
3240
3241 @example
3242 -l 2.5
3243 @end example
3244
3245 @noindent
3246 will not let @code{make} start more than one job if the load average is
3247 above 2.5.  The @samp{-l} option with no following number removes the
3248 load limit, if one was given with a previous @samp{-l} option.@refill
3249
3250 More precisely, when @code{make} goes to start up a job, and it already has
3251 at least one job running, it checks the current load average; if it is not
3252 lower than the limit given with @samp{-l}, @code{make} waits until the load
3253 average goes below that limit, or until all the other jobs finish.
3254
3255 By default, there is no load limit.
3256
3257 @node Errors, Interrupts, Parallel, Commands
3258 @section Errors in Commands
3259 @cindex errors (in commands)
3260 @cindex commands, errors in
3261 @cindex exit status (errors)
3262
3263 After each shell command returns, @code{make} looks at its exit status.
3264 If the command completed successfully, the next command line is executed
3265 in a new shell; after the last command line is finished, the rule is
3266 finished.
3267
3268 If there is an error (the exit status is nonzero), @code{make} gives up on
3269 the current rule, and perhaps on all rules.
3270
3271 Sometimes the failure of a certain command does not indicate a problem.
3272 For example, you may use the @code{mkdir} command to ensure that a
3273 directory exists.  If the directory already exists, @code{mkdir} will
3274 report an error, but you probably want @code{make} to continue regardless.
3275
3276 @cindex @code{-} (in commands)
3277 To ignore errors in a command line, write a @samp{-} at the beginning of
3278 the line's text (after the initial tab).  The @samp{-} is discarded before
3279 the command is passed to the shell for execution.
3280
3281 For example,
3282
3283 @example
3284 @group
3285 clean:
3286         -rm -f *.o
3287 @end group
3288 @end example
3289 @cindex @code{rm} (shell command)
3290
3291 @noindent
3292 This causes @code{rm} to continue even if it is unable to remove a file.
3293
3294 @cindex @code{-i}
3295 @cindex @code{--ignore-errors}
3296 @findex .IGNORE
3297 When you run @code{make} with the @samp{-i} or @samp{--ignore-errors}
3298 flag, errors are ignored in all commands of all rules.  A rule in the
3299 makefile for the special target @code{.IGNORE} has the same effect, if
3300 there are no prerequisites.  These ways of ignoring errors are obsolete
3301 because @samp{-} is more flexible.
3302
3303 When errors are to be ignored, because of either a @samp{-} or the
3304 @samp{-i} flag, @code{make} treats an error return just like success,
3305 except that it prints out a message that tells you the status code
3306 the command exited with, and says that the error has been ignored.
3307
3308 When an error happens that @code{make} has not been told to ignore,
3309 it implies that the current target cannot be correctly remade, and neither
3310 can any other that depends on it either directly or indirectly.  No further
3311 commands will be executed for these targets, since their preconditions
3312 have not been achieved.
3313
3314
3315 @cindex @code{-k}
3316 @cindex @code{--keep-going}
3317 Normally @code{make} gives up immediately in this circumstance, returning a
3318 nonzero status.  However, if the @samp{-k} or @samp{--keep-going}
3319 flag is specified, @code{make}
3320 continues to consider the other prerequisites of the pending targets,
3321 remaking them if necessary, before it gives up and returns nonzero status.
3322 For example, after an error in compiling one object file, @samp{make -k}
3323 will continue compiling other object files even though it already knows
3324 that linking them will be impossible.  @xref{Options Summary, ,Summary of Options}.
3325
3326 The usual behavior assumes that your purpose is to get the specified
3327 targets up to date; once @code{make} learns that this is impossible, it
3328 might as well report the failure immediately.  The @samp{-k} option says
3329 that the real purpose is to test as many of the changes made in the
3330 program as possible, perhaps to find several independent problems so
3331 that you can correct them all before the next attempt to compile.  This
3332 is why Emacs' @code{compile} command passes the @samp{-k} flag by
3333 default.
3334 @cindex Emacs (@code{M-x compile})
3335
3336 @findex .DELETE_ON_ERROR
3337 @cindex deletion of target files
3338 @cindex removal of target files
3339 @cindex target, deleting on error
3340 Usually when a command fails, if it has changed the target file at all,
3341 the file is corrupted and cannot be used---or at least it is not
3342 completely updated.  Yet the file's timestamp says that it is now up to
3343 date, so the next time @code{make} runs, it will not try to update that
3344 file.  The situation is just the same as when the command is killed by a
3345 signal; @pxref{Interrupts}.  So generally the right thing to do is to
3346 delete the target file if the command fails after beginning to change
3347 the file.  @code{make} will do this if @code{.DELETE_ON_ERROR} appears
3348 as a target.  This is almost always what you want @code{make} to do, but
3349 it is not historical practice; so for compatibility, you must explicitly
3350 request it.
3351
3352 @node Interrupts, Recursion, Errors, Commands
3353 @section Interrupting or Killing @code{make}
3354 @cindex interrupt
3355 @cindex signal
3356 @cindex deletion of target files
3357 @cindex removal of target files
3358 @cindex target, deleting on interrupt
3359 @cindex killing (interruption)
3360
3361 If @code{make} gets a fatal signal while a command is executing, it may
3362 delete the target file that the command was supposed to update.  This is
3363 done if the target file's last-modification time has changed since
3364 @code{make} first checked it.
3365
3366 The purpose of deleting the target is to make sure that it is remade from
3367 scratch when @code{make} is next run.  Why is this?  Suppose you type
3368 @kbd{Ctrl-c} while a compiler is running, and it has begun to write an
3369 object file @file{foo.o}.  The @kbd{Ctrl-c} kills the compiler, resulting
3370 in an incomplete file whose last-modification time is newer than the source
3371 file @file{foo.c}.  But @code{make} also receives the @kbd{Ctrl-c} signal
3372 and deletes this incomplete file.  If @code{make} did not do this, the next
3373 invocation of @code{make} would think that @file{foo.o} did not require
3374 updating---resulting in a strange error message from the linker when it
3375 tries to link an object file half of which is missing.
3376
3377 @findex .PRECIOUS
3378 You can prevent the deletion of a target file in this way by making the
3379 special target @code{.PRECIOUS} depend on it.  Before remaking a target,
3380 @code{make} checks to see whether it appears on the prerequisites of
3381 @code{.PRECIOUS}, and thereby decides whether the target should be deleted
3382 if a signal happens.  Some reasons why you might do this are that the
3383 target is updated in some atomic fashion, or exists only to record a
3384 modification-time (its contents do not matter), or must exist at all
3385 times to prevent other sorts of trouble.
3386
3387 @node Recursion, Sequences, Interrupts, Commands
3388 @section Recursive Use of @code{make}
3389 @cindex recursion
3390 @cindex subdirectories, recursion for
3391
3392 Recursive use of @code{make} means using @code{make} as a command in a
3393 makefile.  This technique is useful when you want separate makefiles for
3394 various subsystems that compose a larger system.  For example, suppose you
3395 have a subdirectory @file{subdir} which has its own makefile, and you would
3396 like the containing directory's makefile to run @code{make} on the
3397 subdirectory.  You can do it by writing this:
3398
3399 @example
3400 subsystem:
3401         cd subdir && $(MAKE)
3402 @end example
3403
3404 @noindent
3405 or, equivalently, this (@pxref{Options Summary, ,Summary of Options}):
3406
3407 @example
3408 subsystem:
3409         $(MAKE) -C subdir
3410 @end example
3411 @cindex @code{-C}
3412 @cindex @code{--directory}
3413
3414 You can write recursive @code{make} commands just by copying this example,
3415 but there are many things to know about how they work and why, and about
3416 how the sub-@code{make} relates to the top-level @code{make}.
3417
3418 For your convenience, GNU @code{make} sets the variable @code{CURDIR} to
3419 the pathname of the current working directory for you.  If @code{-C} is
3420 in effect, it will contain the path of the new directory, not the
3421 original.  The value has the same precedence it would have if it were
3422 set in the makefile (by default, an environment variable @code{CURDIR}
3423 will not override this value).  Note that setting this variable has no
3424 effect on the operation of @code{make}
3425
3426 @menu
3427 * MAKE Variable::               The special effects of using @samp{$(MAKE)}.
3428 * Variables/Recursion::         How to communicate variables to a sub-@code{make}.
3429 * Options/Recursion::           How to communicate options to a sub-@code{make}.
3430 * -w Option::                   How the @samp{-w} or @samp{--print-directory} option
3431                                  helps debug use of recursive @code{make} commands.
3432 @end menu
3433
3434 @node MAKE Variable, Variables/Recursion,  , Recursion
3435 @subsection How the @code{MAKE} Variable Works
3436 @vindex MAKE
3437 @cindex recursion, and @code{MAKE} variable
3438
3439 Recursive @code{make} commands should always use the variable @code{MAKE},
3440 not the explicit command name @samp{make}, as shown here:
3441
3442 @example
3443 @group
3444 subsystem:
3445         cd subdir && $(MAKE)
3446 @end group
3447 @end example
3448
3449 The value of this variable is the file name with which @code{make} was
3450 invoked.  If this file name was @file{/bin/make}, then the command executed
3451 is @samp{cd subdir && /bin/make}.  If you use a special version of
3452 @code{make} to run the top-level makefile, the same special version will be
3453 executed for recursive invocations.
3454 @cindex @code{cd} (shell command)
3455
3456 As a special feature, using the variable @code{MAKE} in the commands of
3457 a rule alters the effects of the @samp{-t} (@samp{--touch}), @samp{-n}
3458 (@samp{--just-print}), or @samp{-q} (@w{@samp{--question}}) option.
3459 Using the @code{MAKE} variable has the same effect as using a @samp{+}
3460 character at the beginning of the command line.  @xref{Instead of
3461 Execution, ,Instead of Executing the Commands}.@refill
3462
3463 Consider the command @samp{make -t} in the above example.  (The
3464 @samp{-t} option marks targets as up to date without actually running
3465 any commands; see @ref{Instead of Execution}.)  Following the usual
3466 definition of @samp{-t}, a @samp{make -t} command in the example would
3467 create a file named @file{subsystem} and do nothing else.  What you
3468 really want it to do is run @samp{@w{cd subdir &&} @w{make -t}}; but that would
3469 require executing the command, and @samp{-t} says not to execute
3470 commands.@refill
3471 @cindex @code{-t}, and recursion
3472 @cindex recursion, and @code{-t}
3473 @cindex @code{--touch}, and recursion
3474
3475 The special feature makes this do what you want: whenever a command
3476 line of a rule contains the variable @code{MAKE}, the flags @samp{-t},
3477 @samp{-n} and @samp{-q} do not apply to that line.  Command lines
3478 containing @code{MAKE} are executed normally despite the presence of a
3479 flag that causes most commands not to be run.  The usual
3480 @code{MAKEFLAGS} mechanism passes the flags to the sub-@code{make}
3481 (@pxref{Options/Recursion, ,Communicating Options to a
3482 Sub-@code{make}}), so your request to touch the files, or print the
3483 commands, is propagated to the subsystem.@refill
3484
3485 @node Variables/Recursion, Options/Recursion, MAKE Variable, Recursion
3486 @subsection Communicating Variables to a Sub-@code{make}
3487 @cindex sub-@code{make}
3488 @cindex environment, and recursion
3489 @cindex exporting variables
3490 @cindex variables, environment
3491 @cindex variables, exporting
3492 @cindex recursion, and environment
3493 @cindex recursion, and variables
3494
3495 Variable values of the top-level @code{make} can be passed to the
3496 sub-@code{make} through the environment by explicit request.  These
3497 variables are defined in the sub-@code{make} as defaults, but do not
3498 override what is specified in the makefile used by the sub-@code{make}
3499 makefile unless you use the @samp{-e} switch (@pxref{Options Summary,
3500 ,Summary of Options}).@refill
3501
3502 To pass down, or @dfn{export}, a variable, @code{make} adds the variable
3503 and its value to the environment for running each command.  The
3504 sub-@code{make}, in turn, uses the environment to initialize its table
3505 of variable values.  @xref{Environment, ,Variables from the
3506 Environment}.
3507
3508 Except by explicit request, @code{make} exports a variable only if it
3509 is either defined in the environment initially or set on the command
3510 line, and if its name consists only of letters, numbers, and underscores.
3511 Some shells cannot cope with environment variable names consisting of
3512 characters other than letters, numbers, and underscores.
3513
3514 The special variables @code{SHELL} and @code{MAKEFLAGS} are always
3515 exported (unless you unexport them).
3516 @code{MAKEFILES} is exported if you set it to anything.
3517
3518 @code{make} automatically passes down variable values that were defined
3519 on the command line, by putting them in the @code{MAKEFLAGS} variable.
3520 @iftex
3521 See the next section.
3522 @end iftex
3523 @ifinfo
3524 @xref{Options/Recursion}.
3525 @end ifinfo
3526
3527 Variables are @emph{not} normally passed down if they were created by
3528 default by @code{make} (@pxref{Implicit Variables, ,Variables Used by
3529 Implicit Rules}).  The sub-@code{make} will define these for
3530 itself.@refill
3531
3532 @findex export
3533 If you want to export specific variables to a sub-@code{make}, use the
3534 @code{export} directive, like this:
3535
3536 @example
3537 export @var{variable} @dots{}
3538 @end example
3539
3540 @noindent
3541 @findex unexport
3542 If you want to @emph{prevent} a variable from being exported, use the
3543 @code{unexport} directive, like this:
3544
3545 @example
3546 unexport @var{variable} @dots{}
3547 @end example
3548
3549 @noindent
3550 As a convenience, you can define a variable and export it at the same
3551 time by doing:
3552
3553 @example
3554 export @var{variable} = value
3555 @end example
3556
3557 @noindent
3558 has the same result as:
3559
3560 @example
3561 @var{variable} = value
3562 export @var{variable}
3563 @end example
3564
3565 @noindent
3566 and
3567
3568 @example
3569 export @var{variable} := value
3570 @end example
3571
3572 @noindent
3573 has the same result as:
3574
3575 @example
3576 @var{variable} := value
3577 export @var{variable}
3578 @end example
3579
3580 Likewise,
3581
3582 @example
3583 export @var{variable} += value
3584 @end example
3585
3586 @noindent
3587 is just like:
3588
3589 @example
3590 @var{variable} += value
3591 export @var{variable}
3592 @end example
3593
3594 @noindent
3595 @xref{Appending, ,Appending More Text to Variables}.
3596
3597 You may notice that the @code{export} and @code{unexport} directives
3598 work in @code{make} in the same way they work in the shell, @code{sh}.
3599
3600 If you want all variables to be exported by default, you can use
3601 @code{export} by itself:
3602
3603 @example
3604 export
3605 @end example
3606
3607 @noindent
3608 This tells @code{make} that variables which are not explicitly mentioned
3609 in an @code{export} or @code{unexport} directive should be exported.
3610 Any variable given in an @code{unexport} directive will still @emph{not}
3611 be exported.  If you use @code{export} by itself to export variables by
3612 default, variables whose names contain characters other than
3613 alphanumerics and underscores will not be exported unless specifically
3614 mentioned in an @code{export} directive.@refill
3615
3616 @findex .EXPORT_ALL_VARIABLES
3617 The behavior elicited by an @code{export} directive by itself was the
3618 default in older versions of GNU @code{make}.  If your makefiles depend
3619 on this behavior and you want to be compatible with old versions of
3620 @code{make}, you can write a rule for the special target
3621 @code{.EXPORT_ALL_VARIABLES} instead of using the @code{export} directive.
3622 This will be ignored by old @code{make}s, while the @code{export}
3623 directive will cause a syntax error.@refill
3624 @cindex compatibility in exporting
3625
3626 Likewise, you can use @code{unexport} by itself to tell @code{make}
3627 @emph{not} to export variables by default.  Since this is the default
3628 behavior, you would only need to do this if @code{export} had been used
3629 by itself earlier (in an included makefile, perhaps).  You
3630 @strong{cannot} use @code{export} and @code{unexport} by themselves to
3631 have variables exported for some commands and not for others.  The last
3632 @code{export} or @code{unexport} directive that appears by itself
3633 determines the behavior for the entire run of @code{make}.@refill
3634
3635 @vindex MAKELEVEL
3636 @cindex recursion, level of
3637 As a special feature, the variable @code{MAKELEVEL} is changed when it
3638 is passed down from level to level.  This variable's value is a string
3639 which is the depth of the level as a decimal number.  The value is
3640 @samp{0} for the top-level @code{make}; @samp{1} for a sub-@code{make},
3641 @samp{2} for a sub-sub-@code{make}, and so on.  The incrementation
3642 happens when @code{make} sets up the environment for a command.@refill
3643
3644 The main use of @code{MAKELEVEL} is to test it in a conditional
3645 directive (@pxref{Conditionals, ,Conditional Parts of Makefiles}); this
3646 way you can write a makefile that behaves one way if run recursively and
3647 another way if run directly by you.@refill
3648
3649 @vindex MAKEFILES
3650 You can use the variable @code{MAKEFILES} to cause all sub-@code{make}
3651 commands to use additional makefiles.  The value of @code{MAKEFILES} is
3652 a whitespace-separated list of file names.  This variable, if defined in
3653 the outer-level makefile, is passed down through the environment; then
3654 it serves as a list of extra makefiles for the sub-@code{make} to read
3655 before the usual or specified ones.  @xref{MAKEFILES Variable, ,The
3656 Variable @code{MAKEFILES}}.@refill
3657
3658 @node Options/Recursion, -w Option, Variables/Recursion, Recursion
3659 @subsection Communicating Options to a Sub-@code{make}
3660 @cindex options, and recursion
3661 @cindex recursion, and options
3662
3663 @vindex MAKEFLAGS
3664 Flags such as @samp{-s} and @samp{-k} are passed automatically to the
3665 sub-@code{make} through the variable @code{MAKEFLAGS}.  This variable is
3666 set up automatically by @code{make} to contain the flag letters that
3667 @code{make} received.  Thus, if you do @w{@samp{make -ks}} then
3668 @code{MAKEFLAGS} gets the value @samp{ks}.@refill
3669
3670 As a consequence, every sub-@code{make} gets a value for @code{MAKEFLAGS}
3671 in its environment.  In response, it takes the flags from that value and
3672 processes them as if they had been given as arguments.
3673 @xref{Options Summary, ,Summary of Options}.
3674
3675 @cindex command line variable definitions, and recursion
3676 @cindex variables, command line, and recursion
3677 @cindex recursion, and command line variable definitions
3678 Likewise variables defined on the command line are passed to the
3679 sub-@code{make} through @code{MAKEFLAGS}.  Words in the value of
3680 @code{MAKEFLAGS} that contain @samp{=}, @code{make} treats as variable
3681 definitions just as if they appeared on the command line.
3682 @xref{Overriding, ,Overriding Variables}.
3683
3684 @cindex @code{-C}, and recursion
3685 @cindex @code{-f}, and recursion
3686 @cindex @code{-o}, and recursion
3687 @cindex @code{-W}, and recursion
3688 @cindex @code{--directory}, and recursion
3689 @cindex @code{--file}, and recursion
3690 @cindex @code{--old-file}, and recursion
3691 @cindex @code{--assume-old}, and recursion
3692 @cindex @code{--assume-new}, and recursion
3693 @cindex @code{--new-file}, and recursion
3694 @cindex recursion, and @code{-C}
3695 @cindex recursion, and @code{-f}
3696 @cindex recursion, and @code{-o}
3697 @cindex recursion, and @code{-W}
3698 The options @samp{-C}, @samp{-f}, @samp{-o}, and @samp{-W} are not put
3699 into @code{MAKEFLAGS}; these options are not passed down.@refill
3700
3701 @cindex @code{-j}, and recursion
3702 @cindex @code{--jobs}, and recursion
3703 @cindex recursion, and @code{-j}
3704 @cindex job slots, and recursion
3705 The @samp{-j} option is a special case (@pxref{Parallel, ,Parallel Execution}).
3706 If you set it to some numeric value @samp{N} and your operating system
3707 supports it (most any UNIX system will; others typically won't), the
3708 parent @code{make} and all the sub-@code{make}s will communicate to
3709 ensure that there are only @samp{N} jobs running at the same time
3710 between them all.  Note that any job that is marked recursive
3711 (@pxref{Instead of Execution, ,Instead of Executing the Commands})
3712 doesn't count against the total jobs (otherwise we could get @samp{N}
3713 sub-@code{make}s running and have no slots left over for any real work!)
3714
3715 If your operating system doesn't support the above communication, then
3716 @samp{-j 1} is always put into @code{MAKEFLAGS} instead of the value you
3717 specified.  This is because if the @w{@samp{-j}} option were passed down
3718 to sub-@code{make}s, you would get many more jobs running in parallel
3719 than you asked for.  If you give @samp{-j} with no numeric argument,
3720 meaning to run as many jobs as possible in parallel, this is passed
3721 down, since multiple infinities are no more than one.@refill
3722
3723 If you do not want to pass the other flags down, you must change the
3724 value of @code{MAKEFLAGS}, like this:
3725
3726 @example
3727 subsystem:
3728         cd subdir && $(MAKE) MAKEFLAGS=
3729 @end example
3730
3731 @vindex MAKEOVERRIDES
3732 The command line variable definitions really appear in the variable
3733 @code{MAKEOVERRIDES}, and @code{MAKEFLAGS} contains a reference to this
3734 variable.  If you do want to pass flags down normally, but don't want to
3735 pass down the command line variable definitions, you can reset
3736 @code{MAKEOVERRIDES} to empty, like this:
3737
3738 @example
3739 MAKEOVERRIDES =
3740 @end example
3741
3742 @noindent
3743 @cindex Arg list too long
3744 @cindex E2BIG
3745 This is not usually useful to do.  However, some systems have a small
3746 fixed limit on the size of the environment, and putting so much
3747 information into the value of @code{MAKEFLAGS} can exceed it.  If you
3748 see the error message @samp{Arg list too long}, this may be the problem.
3749 @findex .POSIX
3750 @cindex POSIX.2
3751 (For strict compliance with POSIX.2, changing @code{MAKEOVERRIDES} does
3752 not affect @code{MAKEFLAGS} if the special target @samp{.POSIX} appears
3753 in the makefile.  You probably do not care about this.)
3754
3755 @vindex MFLAGS
3756 A similar variable @code{MFLAGS} exists also, for historical
3757 compatibility.  It has the same value as @code{MAKEFLAGS} except that it
3758 does not contain the command line variable definitions, and it always
3759 begins with a hyphen unless it is empty (@code{MAKEFLAGS} begins with a
3760 hyphen only when it begins with an option that has no single-letter
3761 version, such as @samp{--warn-undefined-variables}).  @code{MFLAGS} was
3762 traditionally used explicitly in the recursive @code{make} command, like
3763 this:
3764
3765 @example
3766 subsystem:
3767         cd subdir && $(MAKE) $(MFLAGS)
3768 @end example
3769
3770 @noindent
3771 but now @code{MAKEFLAGS} makes this usage redundant.  If you want your
3772 makefiles to be compatible with old @code{make} programs, use this
3773 technique; it will work fine with more modern @code{make} versions too.
3774
3775 @cindex setting options from environment
3776 @cindex options, setting from environment
3777 @cindex setting options in makefiles
3778 @cindex options, setting in makefiles
3779 The @code{MAKEFLAGS} variable can also be useful if you want to have
3780 certain options, such as @samp{-k} (@pxref{Options Summary, ,Summary of
3781 Options}), set each time you run @code{make}.  You simply put a value for
3782 @code{MAKEFLAGS} in your environment.  You can also set @code{MAKEFLAGS} in
3783 a makefile, to specify additional flags that should also be in effect for
3784 that makefile.  (Note that you cannot use @code{MFLAGS} this way.  That
3785 variable is set only for compatibility; @code{make} does not interpret a
3786 value you set for it in any way.)
3787
3788 When @code{make} interprets the value of @code{MAKEFLAGS} (either from the
3789 environment or from a makefile), it first prepends a hyphen if the value
3790 does not already begin with one.  Then it chops the value into words
3791 separated by blanks, and parses these words as if they were options given
3792 on the command line (except that @samp{-C}, @samp{-f}, @samp{-h},
3793 @samp{-o}, @samp{-W}, and their long-named versions are ignored; and there
3794 is no error for an invalid option).
3795
3796 If you do put @code{MAKEFLAGS} in your environment, you should be sure not
3797 to include any options that will drastically affect the actions of
3798 @code{make} and undermine the purpose of makefiles and of @code{make}
3799 itself.  For instance, the @samp{-t}, @samp{-n}, and @samp{-q} options, if
3800 put in one of these variables, could have disastrous consequences and would
3801 certainly have at least surprising and probably annoying effects.@refill
3802
3803 @node -w Option,  , Options/Recursion, Recursion
3804 @subsection The @samp{--print-directory} Option
3805 @cindex directories, printing them
3806 @cindex printing directories
3807 @cindex recursion, and printing directories
3808
3809 If you use several levels of recursive @code{make} invocations, the
3810 @samp{-w} or @w{@samp{--print-directory}} option can make the output a
3811 lot easier to understand by showing each directory as @code{make}
3812 starts processing it and as @code{make} finishes processing it.  For
3813 example, if @samp{make -w} is run in the directory @file{/u/gnu/make},
3814 @code{make} will print a line of the form:@refill
3815
3816 @example
3817 make: Entering directory `/u/gnu/make'.
3818 @end example
3819
3820 @noindent
3821 before doing anything else, and a line of the form:
3822
3823 @example
3824 make: Leaving directory `/u/gnu/make'.
3825 @end example
3826
3827 @noindent
3828 when processing is completed.
3829
3830 @cindex @code{-C}, and @code{-w}
3831 @cindex @code{--directory}, and @code{--print-directory}
3832 @cindex recursion, and @code{-w}
3833 @cindex @code{-w}, and @code{-C}
3834 @cindex @code{-w}, and recursion
3835 @cindex @code{--print-directory}, and @code{--directory}
3836 @cindex @code{--print-directory}, and recursion
3837 @cindex @code{--no-print-directory}
3838 @cindex @code{--print-directory}, disabling
3839 @cindex @code{-w}, disabling
3840 Normally, you do not need to specify this option because @samp{make}
3841 does it for you: @samp{-w} is turned on automatically when you use the
3842 @samp{-C} option, and in sub-@code{make}s.  @code{make} will not
3843 automatically turn on @samp{-w} if you also use @samp{-s}, which says to
3844 be silent, or if you use @samp{--no-print-directory} to explicitly
3845 disable it.
3846
3847 @node Sequences, Empty Commands, Recursion, Commands
3848 @section Defining Canned Command Sequences
3849 @cindex sequences of commands
3850 @cindex commands, sequences of
3851
3852 When the same sequence of commands is useful in making various targets, you
3853 can define it as a canned sequence with the @code{define} directive, and
3854 refer to the canned sequence from the rules for those targets.  The canned
3855 sequence is actually a variable, so the name must not conflict with other
3856 variable names.
3857
3858 Here is an example of defining a canned sequence of commands:
3859
3860 @example
3861 define run-yacc
3862 yacc $(firstword $^)
3863 mv y.tab.c $@@
3864 endef
3865 @end example
3866 @cindex @code{yacc}
3867
3868 @noindent
3869 Here @code{run-yacc} is the name of the variable being defined;
3870 @code{endef} marks the end of the definition; the lines in between are the
3871 commands.  The @code{define} directive does not expand variable references
3872 and function calls in the canned sequence; the @samp{$} characters,
3873 parentheses, variable names, and so on, all become part of the value of the
3874 variable you are defining.
3875 @xref{Defining, ,Defining Variables Verbatim},
3876 for a complete explanation of @code{define}.
3877
3878 The first command in this example runs Yacc on the first prerequisite of
3879 whichever rule uses the canned sequence.  The output file from Yacc is
3880 always named @file{y.tab.c}.  The second command moves the output to the
3881 rule's target file name.
3882
3883 To use the canned sequence, substitute the variable into the commands of a
3884 rule.  You can substitute it like any other variable
3885 (@pxref{Reference, ,Basics of Variable References}).
3886 Because variables defined by @code{define} are recursively expanded
3887 variables, all the variable references you wrote inside the @code{define}
3888 are expanded now.  For example:
3889
3890 @example
3891 foo.c : foo.y
3892         $(run-yacc)
3893 @end example
3894
3895 @noindent
3896 @samp{foo.y} will be substituted for the variable @samp{$^} when it occurs in
3897 @code{run-yacc}'s value, and @samp{foo.c} for @samp{$@@}.@refill
3898
3899 This is a realistic example, but this particular one is not needed in
3900 practice because @code{make} has an implicit rule to figure out these
3901 commands based on the file names involved
3902 (@pxref{Implicit Rules, ,Using Implicit Rules}).
3903
3904 @cindex @@, and @code{define}
3905 @cindex -, and @code{define}
3906 @cindex +, and @code{define}
3907 In command execution, each line of a canned sequence is treated just as
3908 if the line appeared on its own in the rule, preceded by a tab.  In
3909 particular, @code{make} invokes a separate subshell for each line.  You
3910 can use the special prefix characters that affect command lines
3911 (@samp{@@}, @samp{-}, and @samp{+}) on each line of a canned sequence.
3912 @xref{Commands, ,Writing the Commands in Rules}.
3913 For example, using this canned sequence:
3914
3915 @example
3916 define frobnicate
3917 @@echo "frobnicating target $@@"
3918 frob-step-1 $< -o $@@-step-1
3919 frob-step-2 $@@-step-1 -o $@@
3920 endef
3921 @end example
3922
3923 @noindent
3924 @code{make} will not echo the first line, the @code{echo} command.
3925 But it @emph{will} echo the following two command lines.
3926
3927 On the other hand, prefix characters on the command line that refers to
3928 a canned sequence apply to every line in the sequence.  So the rule:
3929
3930 @example
3931 frob.out: frob.in
3932         @@$(frobnicate)
3933 @end example
3934
3935 @noindent
3936 does not echo @emph{any} commands.
3937 (@xref{Echoing, ,Command Echoing}, for a full explanation of @samp{@@}.)
3938
3939 @node Empty Commands,  , Sequences, Commands
3940 @section Using Empty Commands
3941 @cindex empty commands
3942 @cindex commands, empty
3943
3944 It is sometimes useful to define commands which do nothing.  This is done
3945 simply by giving a command that consists of nothing but whitespace.  For
3946 example:
3947
3948 @example
3949 target: ;
3950 @end example
3951
3952 @noindent
3953 defines an empty command string for @file{target}.  You could also use a
3954 line beginning with a tab character to define an empty command string,
3955 but this would be confusing because such a line looks empty.
3956
3957 @findex .DEFAULT@r{, and empty commands}
3958 You may be wondering why you would want to define a command string that
3959 does nothing.  The only reason this is useful is to prevent a target
3960 from getting implicit commands (from implicit rules or the
3961 @code{.DEFAULT} special target; @pxref{Implicit Rules} and
3962 @pxref{Last Resort, ,Defining Last-Resort Default Rules}).@refill
3963
3964 @c !!! another reason is for canonical stamp files:
3965 @ignore
3966 foo: stamp-foo ;
3967 stamp-foo: foo.in
3968         create foo frm foo.in
3969         touch $@
3970 @end ignore
3971
3972 You may be inclined to define empty command strings for targets that are
3973 not actual files, but only exist so that their prerequisites can be
3974 remade.  However, this is not the best way to do that, because the
3975 prerequisites may not be remade properly if the target file actually does exist.
3976 @xref{Phony Targets, ,Phony Targets}, for a better way to do this.
3977
3978 @node Using Variables, Conditionals, Commands, Top
3979 @chapter How to Use Variables
3980 @cindex variable
3981 @cindex value
3982 @cindex recursive variable expansion
3983 @cindex simple variable expansion
3984
3985 A @dfn{variable} is a name defined in a makefile to represent a string
3986 of text, called the variable's @dfn{value}.  These values are
3987 substituted by explicit request into targets, prerequisites, commands,
3988 and other parts of the makefile.  (In some other versions of @code{make},
3989 variables are called @dfn{macros}.)
3990 @cindex macro
3991
3992 Variables and functions in all parts of a makefile are expanded when
3993 read, except for the shell commands in rules, the right-hand sides of
3994 variable definitions using @samp{=}, and the bodies of variable
3995 definitions using the @code{define} directive.@refill
3996
3997 Variables can represent lists of file names, options to pass to compilers,
3998 programs to run, directories to look in for source files, directories to
3999 write output in, or anything else you can imagine.
4000
4001 A variable name may be any sequence of characters not containing @samp{:},
4002 @samp{#}, @samp{=}, or leading or trailing whitespace.  However,
4003 variable names containing characters other than letters, numbers, and
4004 underscores should be avoided, as they may be given special meanings in the
4005 future, and with some shells they cannot be passed through the environment to a
4006 sub-@code{make}
4007 (@pxref{Variables/Recursion, ,Communicating Variables to a Sub-@code{make}}).
4008
4009 Variable names are case-sensitive.  The names @samp{foo}, @samp{FOO},
4010 and @samp{Foo} all refer to different variables.
4011
4012 It is traditional to use upper case letters in variable names, but we
4013 recommend using lower case letters for variable names that serve internal
4014 purposes in the makefile, and reserving upper case for parameters that
4015 control implicit rules or for parameters that the user should override with
4016 command options (@pxref{Overriding, ,Overriding Variables}).
4017
4018 A few variables have names that are a single punctuation character or
4019 just a few characters.  These are the @dfn{automatic variables}, and
4020 they have particular specialized uses.  @xref{Automatic, ,Automatic Variables}.
4021
4022 @menu
4023 * Reference::                   How to use the value of a variable.
4024 * Flavors::                     Variables come in two flavors.
4025 * Advanced::                    Advanced features for referencing a variable.
4026 * Values::                      All the ways variables get their values.
4027 * Setting::                     How to set a variable in the makefile.
4028 * Appending::                   How to append more text to the old value
4029                                   of a variable.
4030 * Override Directive::          How to set a variable in the makefile even if
4031                                   the user has set it with a command argument.
4032 * Defining::                    An alternate way to set a variable
4033                                   to a verbatim string.
4034 * Environment::                 Variable values can come from the environment.
4035 * Target-specific::             Variable values can be defined on a per-target
4036                                   basis.
4037 * Pattern-specific::            Target-specific variable values can be applied
4038                                   to a group of targets that match a pattern.
4039 * Automatic::                   Some special variables have predefined
4040                                   meanings for use with implicit rules.
4041 @end menu
4042
4043 @node Reference, Flavors,  , Using Variables
4044 @section Basics of Variable References
4045 @cindex variables, how to reference
4046 @cindex reference to variables
4047 @cindex @code{$}, in variable reference
4048 @cindex dollar sign (@code{$}), in variable reference
4049
4050 To substitute a variable's value, write a dollar sign followed by the name
4051 of the variable in parentheses or braces: either @samp{$(foo)} or
4052 @samp{$@{foo@}} is a valid reference to the variable @code{foo}.  This
4053 special significance of @samp{$} is why you must write @samp{$$} to have
4054 the effect of a single dollar sign in a file name or command.
4055
4056 Variable references can be used in any context: targets, prerequisites,
4057 commands, most directives, and new variable values.  Here is an
4058 example of a common case, where a variable holds the names of all the
4059 object files in a program:
4060
4061 @example
4062 @group
4063 objects = program.o foo.o utils.o
4064 program : $(objects)
4065         cc -o program $(objects)
4066
4067 $(objects) : defs.h
4068 @end group
4069 @end example
4070
4071 Variable references work by strict textual substitution.  Thus, the rule
4072
4073 @example
4074 @group
4075 foo = c
4076 prog.o : prog.$(foo)
4077         $(foo)$(foo) -$(foo) prog.$(foo)
4078 @end group
4079 @end example
4080
4081 @noindent
4082 could be used to compile a C program @file{prog.c}.  Since spaces before
4083 the variable value are ignored in variable assignments, the value of
4084 @code{foo} is precisely @samp{c}.  (Don't actually write your makefiles
4085 this way!)
4086
4087 A dollar sign followed by a character other than a dollar sign,
4088 open-parenthesis or open-brace treats that single character as the
4089 variable name.  Thus, you could reference the variable @code{x} with
4090 @samp{$x}.  However, this practice is strongly discouraged, except in
4091 the case of the automatic variables (@pxref{Automatic, ,Automatic Variables}).
4092
4093 @node Flavors, Advanced, Reference, Using Variables
4094 @section The Two Flavors of Variables
4095 @cindex flavors of variables
4096 @cindex recursive variable expansion
4097 @cindex variables, flavors
4098 @cindex recursively expanded variables
4099 @cindex variables, recursively expanded
4100
4101 There are two ways that a variable in GNU @code{make} can have a value;
4102 we call them the two @dfn{flavors} of variables.  The two flavors are
4103 distinguished in how they are defined and in what they do when expanded.
4104
4105 @cindex =
4106 The first flavor of variable is a @dfn{recursively expanded} variable.
4107 Variables of this sort are defined by lines using @samp{=}
4108 (@pxref{Setting, ,Setting Variables}) or by the @code{define} directive
4109 (@pxref{Defining, ,Defining Variables Verbatim}).  The value you specify
4110 is installed verbatim; if it contains references to other variables,
4111 these references are expanded whenever this variable is substituted (in
4112 the course of expanding some other string).  When this happens, it is
4113 called @dfn{recursive expansion}.@refill
4114
4115 For example,
4116
4117 @example
4118 foo = $(bar)
4119 bar = $(ugh)
4120 ugh = Huh?
4121
4122 all:;echo $(foo)
4123 @end example
4124
4125 @noindent
4126 will echo @samp{Huh?}: @samp{$(foo)} expands to @samp{$(bar)} which
4127 expands to @samp{$(ugh)} which finally expands to @samp{Huh?}.@refill
4128
4129 This flavor of variable is the only sort supported by other versions of
4130 @code{make}.  It has its advantages and its disadvantages.  An advantage
4131 (most would say) is that:
4132
4133 @example
4134 CFLAGS = $(include_dirs) -O
4135 include_dirs = -Ifoo -Ibar
4136 @end example
4137
4138 @noindent
4139 will do what was intended: when @samp{CFLAGS} is expanded in a command,
4140 it will expand to @samp{-Ifoo -Ibar -O}.  A major disadvantage is that you
4141 cannot append something on the end of a variable, as in
4142
4143 @example
4144 CFLAGS = $(CFLAGS) -O
4145 @end example
4146
4147 @noindent
4148 because it will cause an infinite loop in the variable expansion.
4149 (Actually @code{make} detects the infinite loop and reports an error.)
4150 @cindex loops in variable expansion
4151 @cindex variables, loops in expansion
4152
4153 Another disadvantage is that any functions
4154 (@pxref{Functions, ,Functions for Transforming Text})
4155 referenced in the definition will be executed every time the variable is
4156 expanded.  This makes @code{make} run slower; worse, it causes the
4157 @code{wildcard} and @code{shell} functions to give unpredictable results
4158 because you cannot easily control when they are called, or even how many
4159 times.
4160
4161 To avoid all the problems and inconveniences of recursively expanded
4162 variables, there is another flavor: simply expanded variables.
4163
4164 @cindex simply expanded variables
4165 @cindex variables, simply expanded
4166 @cindex :=
4167 @dfn{Simply expanded variables} are defined by lines using @samp{:=}
4168 (@pxref{Setting, ,Setting Variables}).
4169 The value of a simply expanded variable is scanned
4170 once and for all, expanding any references to other variables and
4171 functions, when the variable is defined.  The actual value of the simply
4172 expanded variable is the result of expanding the text that you write.
4173 It does not contain any references to other variables; it contains their
4174 values @emph{as of the time this variable was defined}.  Therefore,
4175
4176 @example
4177 x := foo
4178 y := $(x) bar
4179 x := later
4180 @end example
4181
4182 @noindent
4183 is equivalent to
4184
4185 @example
4186 y := foo bar
4187 x := later
4188 @end example
4189
4190 When a simply expanded variable is referenced, its value is substituted
4191 verbatim.
4192
4193 Here is a somewhat more complicated example, illustrating the use of
4194 @samp{:=} in conjunction with the @code{shell} function.
4195 (@xref{Shell Function, , The @code{shell} Function}.)  This example
4196 also shows use of the variable @code{MAKELEVEL}, which is changed
4197 when it is passed down from level to level.
4198 (@xref{Variables/Recursion, , Communicating Variables to a
4199 Sub-@code{make}}, for information about @code{MAKELEVEL}.)
4200
4201 @vindex MAKELEVEL
4202 @vindex MAKE
4203 @example
4204 @group
4205 ifeq (0,$@{MAKELEVEL@})
4206 cur-dir   := $(shell pwd)
4207 whoami    := $(shell whoami)
4208 host-type := $(shell arch)
4209 MAKE := $@{MAKE@} host-type=$@{host-type@} whoami=$@{whoami@}
4210 endif
4211 @end group
4212 @end example
4213
4214 @noindent
4215 An advantage of this use of @samp{:=} is that a typical
4216 `descend into a directory' command then looks like this:
4217
4218 @example
4219 @group
4220 $@{subdirs@}:
4221       $@{MAKE@} cur-dir=$@{cur-dir@}/$@@ -C $@@ all
4222 @end group
4223 @end example
4224
4225 Simply expanded variables generally make complicated makefile programming
4226 more predictable because they work like variables in most programming
4227 languages.  They allow you to redefine a variable using its own value (or
4228 its value processed in some way by one of the expansion functions) and to
4229 use the expansion functions much more efficiently
4230 (@pxref{Functions, ,Functions for Transforming Text}).
4231
4232 @cindex spaces, in variable values
4233 @cindex whitespace, in variable values
4234 @cindex variables, spaces in values
4235 You can also use them to introduce controlled leading whitespace into
4236 variable values.  Leading whitespace characters are discarded from your
4237 input before substitution of variable references and function calls;
4238 this means you can include leading spaces in a variable value by
4239 protecting them with variable references, like this:
4240
4241 @example
4242 nullstring :=
4243 space := $(nullstring) # end of the line
4244 @end example
4245
4246 @noindent
4247 Here the value of the variable @code{space} is precisely one space.  The
4248 comment @w{@samp{# end of the line}} is included here just for clarity.
4249 Since trailing space characters are @emph{not} stripped from variable
4250 values, just a space at the end of the line would have the same effect
4251 (but be rather hard to read).  If you put whitespace at the end of a
4252 variable value, it is a good idea to put a comment like that at the end
4253 of the line to make your intent clear.  Conversely, if you do @emph{not}
4254 want any whitespace characters at the end of your variable value, you
4255 must remember not to put a random comment on the end of the line after
4256 some whitespace, such as this:
4257
4258 @example
4259 dir := /foo/bar    # directory to put the frobs in
4260 @end example
4261
4262 @noindent
4263 Here the value of the variable @code{dir} is @w{@samp{/foo/bar    }}
4264 (with four trailing spaces), which was probably not the intention.
4265 (Imagine something like @w{@samp{$(dir)/file}} with this definition!)
4266
4267 @cindex conditional variable assignment
4268 @cindex variables, conditional assignment
4269 @cindex ?=
4270 There is another assignment operator for variables, @samp{?=}.  This
4271 is called a conditional variable assignment operator, because it only
4272 has an effect if the variable is not yet defined.  This statement:
4273
4274 @example
4275 FOO ?= bar
4276 @end example
4277
4278 @noindent
4279 is exactly equivalent to this
4280 (@pxref{Origin Function, ,The @code{origin} Function}):
4281
4282 @example
4283 ifeq ($(origin FOO), undefined)
4284   FOO = bar
4285 endif
4286 @end example
4287
4288 Note that a variable set to an empty value is still defined, so
4289 @samp{?=} will not set that variable.
4290
4291 @node Advanced, Values, Flavors, Using Variables
4292 @section Advanced Features for Reference to Variables
4293 @cindex reference to variables
4294
4295 This section describes some advanced features you can use to reference
4296 variables in more flexible ways.
4297
4298 @menu
4299 * Substitution Refs::           Referencing a variable with
4300                                   substitutions on the value.
4301 * Computed Names::              Computing the name of the variable to refer to.
4302 @end menu
4303
4304 @node Substitution Refs, Computed Names,  , Advanced
4305 @subsection Substitution References
4306 @cindex modified variable reference
4307 @cindex substitution variable reference
4308 @cindex variables, modified reference
4309 @cindex variables, substitution reference
4310
4311 @cindex variables, substituting suffix in
4312 @cindex suffix, substituting in variables
4313 A @dfn{substitution reference} substitutes the value of a variable with
4314 alterations that you specify.  It has the form
4315 @samp{$(@var{var}:@var{a}=@var{b})} (or
4316 @samp{$@{@var{var}:@var{a}=@var{b}@}}) and its meaning is to take the value
4317 of the variable @var{var}, replace every @var{a} at the end of a word with
4318 @var{b} in that value, and substitute the resulting string.
4319
4320 When we say ``at the end of a word'', we mean that @var{a} must appear
4321 either followed by whitespace or at the end of the value in order to be
4322 replaced; other occurrences of @var{a} in the value are unaltered.  For
4323 example:@refill
4324
4325 @example
4326 foo := a.o b.o c.o
4327 bar := $(foo:.o=.c)
4328 @end example
4329
4330 @noindent
4331 sets @samp{bar} to @samp{a.c b.c c.c}.  @xref{Setting, ,Setting Variables}.
4332
4333 A substitution reference is actually an abbreviation for use of the
4334 @code{patsubst} expansion function (@pxref{Text Functions, ,Functions for String Substitution and Analysis}).  We provide
4335 substitution references as well as @code{patsubst} for compatibility with
4336 other implementations of @code{make}.
4337
4338 @findex patsubst
4339 Another type of substitution reference lets you use the full power of
4340 the @code{patsubst} function.  It has the same form
4341 @samp{$(@var{var}:@var{a}=@var{b})} described above, except that now
4342 @var{a} must contain a single @samp{%} character.  This case is
4343 equivalent to @samp{$(patsubst @var{a},@var{b},$(@var{var}))}.
4344 @xref{Text Functions, ,Functions for String Substitution and Analysis},
4345 for a description of the @code{patsubst} function.@refill
4346
4347 @example
4348 @group
4349 @exdent For example:
4350
4351 foo := a.o b.o c.o
4352 bar := $(foo:%.o=%.c)
4353 @end group
4354 @end example
4355
4356 @noindent
4357 sets @samp{bar} to @samp{a.c b.c c.c}.
4358
4359 @node Computed Names,  , Substitution Refs, Advanced
4360 @subsection Computed Variable Names
4361 @cindex nested variable reference
4362 @cindex computed variable name
4363 @cindex variables, computed names
4364 @cindex variables, nested references
4365 @cindex variables, @samp{$} in name
4366 @cindex @code{$}, in variable name
4367 @cindex dollar sign (@code{$}), in variable name
4368
4369 Computed variable names are a complicated concept needed only for
4370 sophisticated makefile programming.  For most purposes you need not
4371 consider them, except to know that making a variable with a dollar sign
4372 in its name might have strange results.  However, if you are the type
4373 that wants to understand everything, or you are actually interested in
4374 what they do, read on.
4375
4376 Variables may be referenced inside the name of a variable.  This is
4377 called a @dfn{computed variable name} or a @dfn{nested variable
4378 reference}.  For example,
4379
4380 @example
4381 x = y
4382 y = z
4383 a := $($(x))
4384 @end example
4385
4386 @noindent
4387 defines @code{a} as @samp{z}: the @samp{$(x)} inside @samp{$($(x))} expands
4388 to @samp{y}, so @samp{$($(x))} expands to @samp{$(y)} which in turn expands
4389 to @samp{z}.  Here the name of the variable to reference is not stated
4390 explicitly; it is computed by expansion of @samp{$(x)}.  The reference
4391 @samp{$(x)} here is nested within the outer variable reference.
4392
4393 The previous example shows two levels of nesting, but any number of levels
4394 is possible.  For example, here are three levels:
4395
4396 @example
4397 x = y
4398 y = z
4399 z = u
4400 a := $($($(x)))
4401 @end example
4402
4403 @noindent
4404 Here the innermost @samp{$(x)} expands to @samp{y}, so @samp{$($(x))}
4405 expands to @samp{$(y)} which in turn expands to @samp{z}; now we have
4406 @samp{$(z)}, which becomes @samp{u}.
4407
4408 References to recursively-expanded variables within a variable name are
4409 reexpanded in the usual fashion.  For example:
4410
4411 @example
4412 x = $(y)
4413 y = z
4414 z = Hello
4415 a := $($(x))
4416 @end example
4417
4418 @noindent
4419 defines @code{a} as @samp{Hello}: @samp{$($(x))} becomes @samp{$($(y))}
4420 which becomes @samp{$(z)} which becomes @samp{Hello}.
4421
4422 Nested variable references can also contain modified references and
4423 function invocations (@pxref{Functions, ,Functions for Transforming Text}),
4424 just like any other reference.
4425 For example, using the @code{subst} function
4426 (@pxref{Text Functions, ,Functions for String Substitution and Analysis}):
4427
4428 @example
4429 @group
4430 x = variable1
4431 variable2 := Hello
4432 y = $(subst 1,2,$(x))
4433 z = y
4434 a := $($($(z)))
4435 @end group
4436 @end example
4437
4438 @noindent
4439 eventually defines @code{a} as @samp{Hello}.  It is doubtful that anyone
4440 would ever want to write a nested reference as convoluted as this one, but
4441 it works: @samp{$($($(z)))} expands to @samp{$($(y))} which becomes
4442 @samp{$($(subst 1,2,$(x)))}.  This gets the value @samp{variable1} from
4443 @code{x} and changes it by substitution to @samp{variable2}, so that the
4444 entire string becomes @samp{$(variable2)}, a simple variable reference
4445 whose value is @samp{Hello}.@refill
4446
4447 A computed variable name need not consist entirely of a single variable
4448 reference.  It can contain several variable references, as well as some
4449 invariant text.  For example,
4450
4451 @example
4452 @group
4453 a_dirs := dira dirb
4454 1_dirs := dir1 dir2
4455 @end group
4456
4457 @group
4458 a_files := filea fileb
4459 1_files := file1 file2
4460 @end group
4461
4462 @group
4463 ifeq "$(use_a)" "yes"
4464 a1 := a
4465 else
4466 a1 := 1
4467 endif
4468 @end group
4469
4470 @group
4471 ifeq "$(use_dirs)" "yes"
4472 df := dirs
4473 else
4474 df := files
4475 endif
4476
4477 dirs := $($(a1)_$(df))
4478 @end group
4479 @end example
4480
4481 @noindent
4482 will give @code{dirs} the same value as @code{a_dirs}, @code{1_dirs},
4483 @code{a_files} or @code{1_files} depending on the settings of @code{use_a}
4484 and @code{use_dirs}.@refill
4485
4486 Computed variable names can also be used in substitution references:
4487
4488 @example
4489 @group
4490 a_objects := a.o b.o c.o
4491 1_objects := 1.o 2.o 3.o
4492
4493 sources := $($(a1)_objects:.o=.c)
4494 @end group
4495 @end example
4496
4497 @noindent
4498 defines @code{sources} as either @samp{a.c b.c c.c} or @samp{1.c 2.c 3.c},
4499 depending on the value of @code{a1}.
4500
4501 The only restriction on this sort of use of nested variable references
4502 is that they cannot specify part of the name of a function to be called.
4503 This is because the test for a recognized function name is done before
4504 the expansion of nested references.  For example,
4505
4506 @example
4507 @group
4508 ifdef do_sort
4509 func := sort
4510 else
4511 func := strip
4512 endif
4513 @end group
4514
4515 @group
4516 bar := a d b g q c
4517 @end group
4518
4519 @group
4520 foo := $($(func) $(bar))
4521 @end group
4522 @end example
4523
4524 @noindent
4525 attempts to give @samp{foo} the value of the variable @samp{sort a d b g
4526 q c} or @samp{strip a d b g q c}, rather than giving @samp{a d b g q c}
4527 as the argument to either the @code{sort} or the @code{strip} function.
4528 This restriction could be removed in the future if that change is shown
4529 to be a good idea.
4530
4531 You can also use computed variable names in the left-hand side of a
4532 variable assignment, or in a @code{define} directive, as in:
4533
4534 @example
4535 dir = foo
4536 $(dir)_sources := $(wildcard $(dir)/*.c)
4537 define $(dir)_print
4538 lpr $($(dir)_sources)
4539 endef
4540 @end example
4541
4542 @noindent
4543 This example defines the variables @samp{dir}, @samp{foo_sources}, and
4544 @samp{foo_print}.
4545
4546 Note that @dfn{nested variable references} are quite different from
4547 @dfn{recursively expanded variables}
4548 (@pxref{Flavors, ,The Two Flavors of Variables}), though both are
4549 used together in complex ways when doing makefile programming.@refill
4550
4551 @node Values, Setting, Advanced, Using Variables
4552 @section How Variables Get Their Values
4553 @cindex variables, how they get their values
4554 @cindex value, how a variable gets it
4555
4556 Variables can get values in several different ways:
4557
4558 @itemize @bullet
4559 @item
4560 You can specify an overriding value when you run @code{make}.
4561 @xref{Overriding, ,Overriding Variables}.
4562
4563 @item
4564 You can specify a value in the makefile, either
4565 with an assignment (@pxref{Setting, ,Setting Variables}) or with a
4566 verbatim definition (@pxref{Defining, ,Defining Variables Verbatim}).@refill
4567
4568 @item
4569 Variables in the environment become @code{make} variables.
4570 @xref{Environment, ,Variables from the Environment}.
4571
4572 @item
4573 Several @dfn{automatic} variables are given new values for each rule.
4574 Each of these has a single conventional use.
4575 @xref{Automatic, ,Automatic Variables}.
4576
4577 @item
4578 Several variables have constant initial values.
4579 @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
4580 @end itemize
4581
4582 @node Setting, Appending, Values, Using Variables
4583 @section Setting Variables
4584 @cindex setting variables
4585 @cindex variables, setting
4586 @cindex =
4587 @cindex :=
4588 @cindex ?=
4589
4590 To set a variable from the makefile, write a line starting with the
4591 variable name followed by @samp{=} or @samp{:=}.  Whatever follows the
4592 @samp{=} or @samp{:=} on the line becomes the value.  For example,
4593
4594 @example
4595 objects = main.o foo.o bar.o utils.o
4596 @end example
4597
4598 @noindent
4599 defines a variable named @code{objects}.  Whitespace around the variable
4600 name and immediately after the @samp{=} is ignored.
4601
4602 Variables defined with @samp{=} are @dfn{recursively expanded} variables.
4603 Variables defined with @samp{:=} are @dfn{simply expanded} variables; these
4604 definitions can contain variable references which will be expanded before
4605 the definition is made.  @xref{Flavors, ,The Two Flavors of Variables}.
4606
4607 The variable name may contain function and variable references, which
4608 are expanded when the line is read to find the actual variable name to use.
4609
4610 There is no limit on the length of the value of a variable except the
4611 amount of swapping space on the computer.  When a variable definition is
4612 long, it is a good idea to break it into several lines by inserting
4613 backslash-newline at convenient places in the definition.  This will not
4614 affect the functioning of @code{make}, but it will make the makefile easier
4615 to read.
4616
4617 Most variable names are considered to have the empty string as a value if
4618 you have never set them.  Several variables have built-in initial values
4619 that are not empty, but you can set them in the usual ways
4620 (@pxref{Implicit Variables, ,Variables Used by Implicit Rules}).
4621 Several special variables are set
4622 automatically to a new value for each rule; these are called the
4623 @dfn{automatic} variables (@pxref{Automatic, ,Automatic Variables}).
4624
4625 If you'd like a variable to be set to a value only if it's not already
4626 set, then you can use the shorthand operator @samp{?=} instead of
4627 @samp{=}.  These two settings of the variable @samp{FOO} are identical
4628 (@pxref{Origin Function, ,The @code{origin} Function}):
4629
4630 @example
4631 FOO ?= bar
4632 @end example
4633
4634 @noindent
4635 and
4636
4637 @example
4638 ifeq ($(origin FOO), undefined)
4639 FOO = bar
4640 endif
4641 @end example
4642
4643 @node Appending, Override Directive, Setting, Using Variables
4644 @section Appending More Text to Variables
4645 @cindex +=
4646 @cindex appending to variables
4647 @cindex variables, appending to
4648
4649 Often it is useful to add more text to the value of a variable already defined.
4650 You do this with a line containing @samp{+=}, like this:
4651
4652 @example
4653 objects += another.o
4654 @end example
4655
4656 @noindent
4657 This takes the value of the variable @code{objects}, and adds the text
4658 @samp{another.o} to it (preceded by a single space).  Thus:
4659
4660 @example
4661 objects = main.o foo.o bar.o utils.o
4662 objects += another.o
4663 @end example
4664
4665 @noindent
4666 sets @code{objects} to @samp{main.o foo.o bar.o utils.o another.o}.
4667
4668 Using @samp{+=} is similar to:
4669
4670 @example
4671 objects = main.o foo.o bar.o utils.o
4672 objects := $(objects) another.o
4673 @end example
4674
4675 @noindent
4676 but differs in ways that become important when you use more complex values.
4677
4678 When the variable in question has not been defined before, @samp{+=}
4679 acts just like normal @samp{=}: it defines a recursively-expanded
4680 variable.  However, when there @emph{is} a previous definition, exactly
4681 what @samp{+=} does depends on what flavor of variable you defined
4682 originally.  @xref{Flavors, ,The Two Flavors of Variables}, for an
4683 explanation of the two flavors of variables.
4684
4685 When you add to a variable's value with @samp{+=}, @code{make} acts
4686 essentially as if you had included the extra text in the initial
4687 definition of the variable.  If you defined it first with @samp{:=},
4688 making it a simply-expanded variable, @samp{+=} adds to that
4689 simply-expanded definition, and expands the new text before appending it
4690 to the old value just as @samp{:=} does
4691 (@pxref{Setting, ,Setting Variables}, for a full explanation of @samp{:=}).
4692 In fact,
4693
4694 @example
4695 variable := value
4696 variable += more
4697 @end example
4698
4699 @noindent
4700 is exactly equivalent to:
4701
4702 @noindent
4703 @example
4704 variable := value
4705 variable := $(variable) more
4706 @end example
4707
4708 On the other hand, when you use @samp{+=} with a variable that you defined
4709 first to be recursively-expanded using plain @samp{=}, @code{make} does
4710 something a bit different.  Recall that when you define a
4711 recursively-expanded variable, @code{make} does not expand the value you set
4712 for variable and function references immediately.  Instead it stores the text
4713 verbatim, and saves these variable and function references to be expanded
4714 later, when you refer to the new variable (@pxref{Flavors, ,The Two Flavors
4715 of Variables}).  When you use @samp{+=} on a recursively-expanded variable,
4716 it is this unexpanded text to which @code{make} appends the new text you
4717 specify.
4718
4719 @example
4720 @group
4721 variable = value
4722 variable += more
4723 @end group
4724 @end example
4725
4726 @noindent
4727 is roughly equivalent to:
4728
4729 @example
4730 @group
4731 temp = value
4732 variable = $(temp) more
4733 @end group
4734 @end example
4735
4736 @noindent
4737 except that of course it never defines a variable called @code{temp}.
4738 The importance of this comes when the variable's old value contains
4739 variable references.  Take this common example:
4740
4741 @example
4742 CFLAGS = $(includes) -O
4743 @dots{}
4744 CFLAGS += -pg # enable profiling
4745 @end example
4746
4747 @noindent
4748 The first line defines the @code{CFLAGS} variable with a reference to another
4749 variable, @code{includes}.  (@code{CFLAGS} is used by the rules for C
4750 compilation; @pxref{Catalogue of Rules, ,Catalogue of Implicit Rules}.)
4751 Using @samp{=} for the definition makes @code{CFLAGS} a recursively-expanded
4752 variable, meaning @w{@samp{$(includes) -O}} is @emph{not} expanded when
4753 @code{make} processes the definition of @code{CFLAGS}.  Thus, @code{includes}
4754 need not be defined yet for its value to take effect.  It only has to be
4755 defined before any reference to @code{CFLAGS}.  If we tried to append to the
4756 value of @code{CFLAGS} without using @samp{+=}, we might do it like this:
4757
4758 @example
4759 CFLAGS := $(CFLAGS) -pg # enable profiling
4760 @end example
4761
4762 @noindent
4763 This is pretty close, but not quite what we want.  Using @samp{:=}
4764 redefines @code{CFLAGS} as a simply-expanded variable; this means
4765 @code{make} expands the text @w{@samp{$(CFLAGS) -pg}} before setting the
4766 variable.  If @code{includes} is not yet defined, we get @w{@samp{ -O
4767 -pg}}, and a later definition of @code{includes} will have no effect.
4768 Conversely, by using @samp{+=} we set @code{CFLAGS} to the
4769 @emph{unexpanded} value @w{@samp{$(includes) -O -pg}}.  Thus we preserve
4770 the reference to @code{includes}, so if that variable gets defined at
4771 any later point, a reference like @samp{$(CFLAGS)} still uses its
4772 value.
4773
4774 @node Override Directive, Defining, Appending, Using Variables
4775 @section The @code{override} Directive
4776 @findex override
4777 @cindex overriding with @code{override}
4778 @cindex variables, overriding
4779
4780 If a variable has been set with a command argument
4781 (@pxref{Overriding, ,Overriding Variables}),
4782 then ordinary assignments in the makefile are ignored.  If you want to set
4783 the variable in the makefile even though it was set with a command
4784 argument, you can use an @code{override} directive, which is a line that
4785 looks like this:@refill
4786
4787 @example
4788 override @var{variable} = @var{value}
4789 @end example
4790
4791 @noindent
4792 or
4793
4794 @example
4795 override @var{variable} := @var{value}
4796 @end example
4797
4798 To append more text to a variable defined on the command line, use:
4799
4800 @example
4801 override @var{variable} += @var{more text}
4802 @end example
4803
4804 @noindent
4805 @xref{Appending, ,Appending More Text to Variables}.
4806
4807 The @code{override} directive was not invented for escalation in the war
4808 between makefiles and command arguments.  It was invented so you can alter
4809 and add to values that the user specifies with command arguments.
4810
4811 For example, suppose you always want the @samp{-g} switch when you run the
4812 C compiler, but you would like to allow the user to specify the other
4813 switches with a command argument just as usual.  You could use this
4814 @code{override} directive:
4815
4816 @example
4817 override CFLAGS += -g
4818 @end example
4819
4820 You can also use @code{override} directives with @code{define} directives.
4821 This is done as you might expect:
4822
4823 @example
4824 override define foo
4825 bar
4826 endef
4827 @end example
4828
4829 @noindent
4830 @iftex
4831 See the next section for information about @code{define}.
4832 @end iftex
4833 @ifinfo
4834 @xref{Defining, ,Defining Variables Verbatim}.
4835 @end ifinfo
4836
4837 @node Defining, Environment, Override Directive, Using Variables
4838 @section Defining Variables Verbatim
4839 @findex define
4840 @findex endef
4841 @cindex verbatim variable definition
4842 @cindex defining variables verbatim
4843 @cindex variables, defining verbatim
4844
4845 Another way to set the value of a variable is to use the @code{define}
4846 directive.  This directive has an unusual syntax which allows newline
4847 characters to be included in the value, which is convenient for defining
4848 canned sequences of commands
4849 (@pxref{Sequences, ,Defining Canned Command Sequences}).
4850
4851 The @code{define} directive is followed on the same line by the name of the
4852 variable and nothing more.  The value to give the variable appears on the
4853 following lines.  The end of the value is marked by a line containing just
4854 the word @code{endef}.  Aside from this difference in syntax, @code{define}
4855 works just like @samp{=}: it creates a recursively-expanded variable
4856 (@pxref{Flavors, ,The Two Flavors of Variables}).
4857 The variable name may contain function and variable references, which
4858 are expanded when the directive is read to find the actual variable name
4859 to use.
4860
4861 @example
4862 define two-lines
4863 echo foo
4864 echo $(bar)
4865 endef
4866 @end example
4867
4868 The value in an ordinary assignment cannot contain a newline; but the
4869 newlines that separate the lines of the value in a @code{define} become
4870 part of the variable's value (except for the final newline which precedes
4871 the @code{endef} and is not considered part of the value).@refill
4872
4873 @need 800
4874 The previous example is functionally equivalent to this:
4875
4876 @example
4877 two-lines = echo foo; echo $(bar)
4878 @end example
4879
4880 @noindent
4881 since two commands separated by semicolon behave much like two separate
4882 shell commands.  However, note that using two separate lines means
4883 @code{make} will invoke the shell twice, running an independent subshell
4884 for each line.  @xref{Execution, ,Command Execution}.
4885
4886 If you want variable definitions made with @code{define} to take
4887 precedence over command-line variable definitions, you can use the
4888 @code{override} directive together with @code{define}:
4889
4890 @example
4891 override define two-lines
4892 foo
4893 $(bar)
4894 endef
4895 @end example
4896
4897 @noindent
4898 @xref{Override Directive, ,The @code{override} Directive}.
4899
4900 @node Environment, Target-specific, Defining, Using Variables
4901 @section Variables from the Environment
4902
4903 @cindex variables, environment
4904 @cindex environment
4905 Variables in @code{make} can come from the environment in which
4906 @code{make} is run.  Every environment variable that @code{make} sees when
4907 it starts up is transformed into a @code{make} variable with the same name
4908 and value.  But an explicit assignment in the makefile, or with a command
4909 argument, overrides the environment.  (If the @samp{-e} flag is specified,
4910 then values from the environment override assignments in the makefile.
4911 @xref{Options Summary, ,Summary of Options}.
4912 But this is not recommended practice.)
4913
4914 Thus, by setting the variable @code{CFLAGS} in your environment, you can
4915 cause all C compilations in most makefiles to use the compiler switches you
4916 prefer.  This is safe for variables with standard or conventional meanings
4917 because you know that no makefile will use them for other things.  (But
4918 this is not totally reliable; some makefiles set @code{CFLAGS} explicitly
4919 and therefore are not affected by the value in the environment.)
4920
4921 When @code{make} is invoked recursively, variables defined in the
4922 outer invocation can be passed to inner invocations through the
4923 environment (@pxref{Recursion, ,Recursive Use of @code{make}}).  By
4924 default, only variables that came from the environment or the command
4925 line are passed to recursive invocations.  You can use the
4926 @code{export} directive to pass other variables.
4927 @xref{Variables/Recursion, , Communicating Variables to a
4928 Sub-@code{make}}, for full details.
4929
4930 Other use of variables from the environment is not recommended.  It is not
4931 wise for makefiles to depend for their functioning on environment variables
4932 set up outside their control, since this would cause different users to get
4933 different results from the same makefile.  This is against the whole
4934 purpose of most makefiles.
4935
4936 Such problems would be especially likely with the variable @code{SHELL},
4937 which is normally present in the environment to specify the user's choice
4938 of interactive shell.  It would be very undesirable for this choice to
4939 affect @code{make}.  So @code{make} ignores the environment value of
4940 @code{SHELL} (except on MS-DOS and MS-Windows, where @code{SHELL} is
4941 usually not set.  @xref{Execution, ,Special handling of SHELL on
4942 MS-DOS}.)@refill
4943
4944 @node Target-specific, Pattern-specific, Environment, Using Variables
4945 @section Target-specific Variable Values
4946 @cindex target-specific variables
4947 @cindex variables, target-specific
4948
4949 Variable values in @code{make} are usually global; that is, they are the
4950 same regardless of where they are evaluated (unless they're reset, of
4951 course).  One exception to that is automatic variables
4952 (@pxref{Automatic, ,Automatic Variables}).
4953
4954 The other exception is @dfn{target-specific variable values}.  This
4955 feature allows you to define different values for the same variable,
4956 based on the target that @code{make} is currently building.  As with
4957 automatic variables, these values are only available within the context
4958 of a target's command script (and in other target-specific assignments).
4959
4960 Set a target-specific variable value like this:
4961
4962 @example
4963 @var{target} @dots{} : @var{variable-assignment}
4964 @end example
4965
4966 @noindent
4967 or like this:
4968
4969 @example
4970 @var{target} @dots{} : override @var{variable-assignment}
4971 @end example
4972
4973 Multiple @var{target} values create a target-specific variable value for
4974 each member of the target list individually.
4975
4976 The @var{variable-assignment} can be any valid form of assignment;
4977 recursive (@samp{=}), static (@samp{:=}), appending (@samp{+=}), or
4978 conditional (@samp{?=}).  All variables that appear within the
4979 @var{variable-assignment} are evaluated within the context of the
4980 target: thus, any previously-defined target-specific variable values
4981 will be in effect.  Note that this variable is actually distinct from
4982 any ``global'' value: the two variables do not have to have the same
4983 flavor (recursive vs. static).
4984
4985 Target-specific variables have the same priority as any other makefile
4986 variable.  Variables provided on the command-line (and in the
4987 environment if the @samp{-e} option is in force) will take precedence.
4988 Specifying the @code{override} directive will allow the target-specific
4989 variable value to be preferred.
4990
4991 There is one more special feature of target-specific variables: when you
4992 define a target-specific variable, that variable value is also in effect
4993 for all prerequisites of this target (unless those prerequisites override
4994 it with their own target-specific variable value).  So, for example, a
4995 statement like this:
4996
4997 @example
4998 prog : CFLAGS = -g
4999 prog : prog.o foo.o bar.o
5000 @end example
5001
5002 @noindent
5003 will set @code{CFLAGS} to @samp{-g} in the command script for
5004 @file{prog}, but it will also set @code{CFLAGS} to @samp{-g} in the
5005 command scripts that create @file{prog.o}, @file{foo.o}, and
5006 @file{bar.o}, and any command scripts which create their prerequisites.
5007
5008 @node Pattern-specific,  , Target-specific, Using Variables
5009 @section Pattern-specific Variable Values
5010 @cindex pattern-specific variables
5011 @cindex variables, pattern-specific
5012
5013 In addition to target-specific variable values (@pxref{Target-specific,
5014 ,Target-specific Variable Values}), GNU @code{make} supports
5015 pattern-specific variable values.  In this form, a variable is defined
5016 for any target that matches the pattern specified.  Variables defined in
5017 this way are searched after any target-specific variables defined
5018 explicitly for that target, and before target-specific variables defined
5019 for the parent target.
5020
5021 Set a pattern-specific variable value like this:
5022
5023 @example
5024 @var{pattern} @dots{} : @var{variable-assignment}
5025 @end example
5026
5027 @noindent
5028 or like this:
5029
5030 @example
5031 @var{pattern} @dots{} : override @var{variable-assignment}
5032 @end example
5033
5034 @noindent
5035 where @var{pattern} is a %-pattern.  As with target-specific variable
5036 values, multiple @var{pattern} values create a pattern-specific variable
5037 value for each pattern individually.  The @var{variable-assignment} can
5038 be any valid form of assignment.  Any command-line variable setting will
5039 take precedence, unless @code{override} is specified.
5040
5041 For example:
5042
5043 @example
5044 %.o : CFLAGS = -O
5045 @end example
5046
5047 @noindent
5048 will assign @code{CFLAGS} the value of @samp{-O} for all targets
5049 matching the pattern @code{%.o}.
5050
5051 @node Conditionals, Functions, Using Variables, Top
5052 @chapter Conditional Parts of Makefiles
5053
5054 @cindex conditionals
5055 A @dfn{conditional} causes part of a makefile to be obeyed or ignored
5056 depending on the values of variables.  Conditionals can compare the
5057 value of one variable to another, or the value of a variable to
5058 a constant string.  Conditionals control what @code{make} actually
5059 ``sees'' in the makefile, so they @emph{cannot} be used to control shell
5060 commands at the time of execution.@refill
5061
5062 @menu
5063 * Conditional Example::         Example of a conditional
5064 * Conditional Syntax::          The syntax of conditionals.
5065 * Testing Flags::               Conditionals that test flags.
5066 @end menu
5067
5068 @node Conditional Example, Conditional Syntax,  , Conditionals
5069 @section Example of a Conditional
5070
5071 The following example of a conditional tells @code{make} to use one set
5072 of libraries if the @code{CC} variable is @samp{gcc}, and a different
5073 set of libraries otherwise.  It works by controlling which of two
5074 command lines will be used as the command for a rule.  The result is
5075 that @samp{CC=gcc} as an argument to @code{make} changes not only which
5076 compiler is used but also which libraries are linked.
5077
5078 @example
5079 libs_for_gcc = -lgnu
5080 normal_libs =
5081
5082 foo: $(objects)
5083 ifeq ($(CC),gcc)
5084         $(CC) -o foo $(objects) $(libs_for_gcc)
5085 else
5086         $(CC) -o foo $(objects) $(normal_libs)
5087 endif
5088 @end example
5089
5090 This conditional uses three directives: one @code{ifeq}, one @code{else}
5091 and one @code{endif}.
5092
5093 The @code{ifeq} directive begins the conditional, and specifies the
5094 condition.  It contains two arguments, separated by a comma and surrounded
5095 by parentheses.  Variable substitution is performed on both arguments and
5096 then they are compared.  The lines of the makefile following the
5097 @code{ifeq} are obeyed if the two arguments match; otherwise they are
5098 ignored.
5099
5100 The @code{else} directive causes the following lines to be obeyed if the
5101 previous conditional failed.  In the example above, this means that the
5102 second alternative linking command is used whenever the first alternative
5103 is not used.  It is optional to have an @code{else} in a conditional.
5104
5105 The @code{endif} directive ends the conditional.  Every conditional must
5106 end with an @code{endif}.  Unconditional makefile text follows.
5107
5108 As this example illustrates, conditionals work at the textual level:
5109 the lines of the conditional are treated as part of the makefile, or
5110 ignored, according to the condition.  This is why the larger syntactic
5111 units of the makefile, such as rules, may cross the beginning or the
5112 end of the conditional.
5113
5114 When the variable @code{CC} has the value @samp{gcc}, the above example has
5115 this effect:
5116
5117 @example
5118 foo: $(objects)
5119         $(CC) -o foo $(objects) $(libs_for_gcc)
5120 @end example
5121
5122 @noindent
5123 When the variable @code{CC} has any other value, the effect is this:
5124
5125 @example
5126 foo: $(objects)
5127         $(CC) -o foo $(objects) $(normal_libs)
5128 @end example
5129
5130 Equivalent results can be obtained in another way by conditionalizing a
5131 variable assignment and then using the variable unconditionally:
5132
5133 @example
5134 libs_for_gcc = -lgnu
5135 normal_libs =
5136
5137 ifeq ($(CC),gcc)
5138   libs=$(libs_for_gcc)
5139 else
5140   libs=$(normal_libs)
5141 endif
5142
5143 foo: $(objects)
5144         $(CC) -o foo $(objects) $(libs)
5145 @end example
5146
5147 @node Conditional Syntax, Testing Flags, Conditional Example, Conditionals
5148 @section Syntax of Conditionals
5149 @findex ifdef
5150 @findex ifeq
5151 @findex ifndef
5152 @findex ifneq
5153 @findex else
5154 @findex endif
5155
5156 The syntax of a simple conditional with no @code{else} is as follows:
5157
5158 @example
5159 @var{conditional-directive}
5160 @var{text-if-true}
5161 endif
5162 @end example
5163
5164 @noindent
5165 The @var{text-if-true} may be any lines of text, to be considered as part
5166 of the makefile if the condition is true.  If the condition is false, no
5167 text is used instead.
5168
5169 The syntax of a complex conditional is as follows:
5170
5171 @example
5172 @var{conditional-directive}
5173 @var{text-if-true}
5174 else
5175 @var{text-if-false}
5176 endif
5177 @end example
5178
5179 @noindent
5180 If the condition is true, @var{text-if-true} is used; otherwise,
5181 @var{text-if-false} is used instead.  The @var{text-if-false} can be any
5182 number of lines of text.
5183
5184 The syntax of the @var{conditional-directive} is the same whether the
5185 conditional is simple or complex.  There are four different directives that
5186 test different conditions.  Here is a table of them:
5187
5188 @table @code
5189 @item ifeq (@var{arg1}, @var{arg2})
5190 @itemx ifeq '@var{arg1}' '@var{arg2}'
5191 @itemx ifeq "@var{arg1}" "@var{arg2}"
5192 @itemx ifeq "@var{arg1}" '@var{arg2}'
5193 @itemx ifeq '@var{arg1}' "@var{arg2}"
5194 Expand all variable references in @var{arg1} and @var{arg2} and
5195 compare them.  If they are identical, the @var{text-if-true} is
5196 effective; otherwise, the @var{text-if-false}, if any, is effective.
5197
5198 Often you want to test if a variable has a non-empty value.  When the
5199 value results from complex expansions of variables and functions,
5200 expansions you would consider empty may actually contain whitespace
5201 characters and thus are not seen as empty.  However, you can use the
5202 @code{strip} function (@pxref{Text Functions}) to avoid interpreting
5203 whitespace as a non-empty value.  For example:
5204
5205 @example
5206 @group
5207 ifeq ($(strip $(foo)),)
5208 @var{text-if-empty}
5209 endif
5210 @end group
5211 @end example
5212
5213 @noindent
5214 will evaluate @var{text-if-empty} even if the expansion of
5215 @code{$(foo)} contains whitespace characters.
5216
5217 @item ifneq (@var{arg1}, @var{arg2})
5218 @itemx ifneq '@var{arg1}' '@var{arg2}'
5219 @itemx ifneq "@var{arg1}" "@var{arg2}"
5220 @itemx ifneq "@var{arg1}" '@var{arg2}'
5221 @itemx ifneq '@var{arg1}' "@var{arg2}"
5222 Expand all variable references in @var{arg1} and @var{arg2} and
5223 compare them.  If they are different, the @var{text-if-true} is
5224 effective; otherwise, the @var{text-if-false}, if any, is effective.
5225
5226 @item ifdef @var{variable-name}
5227 If the variable @var{variable-name} has a non-empty value, the
5228 @var{text-if-true} is effective; otherwise, the @var{text-if-false},
5229 if any, is effective.  Variables that have never been defined have an
5230 empty value.
5231
5232 Note that @code{ifdef} only tests whether a variable has a value.  It
5233 does not expand the variable to see if that value is nonempty.
5234 Consequently, tests using @code{ifdef} return true for all definitions
5235 except those like @code{foo =}.  To test for an empty value, use
5236 @w{@code{ifeq ($(foo),)}}.  For example,
5237
5238 @example
5239 bar =
5240 foo = $(bar)
5241 ifdef foo
5242 frobozz = yes
5243 else
5244 frobozz = no
5245 endif
5246 @end example
5247
5248 @noindent
5249 sets @samp{frobozz} to @samp{yes}, while:
5250
5251 @example
5252 foo =
5253 ifdef foo
5254 frobozz = yes
5255 else
5256 frobozz = no
5257 endif
5258 @end example
5259
5260 @noindent
5261 sets @samp{frobozz} to @samp{no}.
5262
5263 @item ifndef @var{variable-name}
5264 If the variable @var{variable-name} has an empty value, the
5265 @var{text-if-true} is effective; otherwise, the @var{text-if-false},
5266 if any, is effective.
5267 @end table
5268
5269 Extra spaces are allowed and ignored at the beginning of the conditional
5270 directive line, but a tab is not allowed.  (If the line begins with a tab,
5271 it will be considered a command for a rule.)  Aside from this, extra spaces
5272 or tabs may be inserted with no effect anywhere except within the directive
5273 name or within an argument.  A comment starting with @samp{#} may appear at
5274 the end of the line.
5275
5276 The other two directives that play a part in a conditional are @code{else}
5277 and @code{endif}.  Each of these directives is written as one word, with no
5278 arguments.  Extra spaces are allowed and ignored at the beginning of the
5279 line, and spaces or tabs at the end.  A comment starting with @samp{#} may
5280 appear at the end of the line.
5281
5282 Conditionals affect which lines of the makefile @code{make} uses.  If
5283 the condition is true, @code{make} reads the lines of the
5284 @var{text-if-true} as part of the makefile; if the condition is false,
5285 @code{make} ignores those lines completely.  It follows that syntactic
5286 units of the makefile, such as rules, may safely be split across the
5287 beginning or the end of the conditional.@refill
5288
5289 @code{make} evaluates conditionals when it reads a makefile.
5290 Consequently, you cannot use automatic variables in the tests of
5291 conditionals because they are not defined until commands are run
5292 (@pxref{Automatic, , Automatic Variables}).
5293
5294 To prevent intolerable confusion, it is not permitted to start a
5295 conditional in one makefile and end it in another.  However, you may
5296 write an @code{include} directive within a conditional, provided you do
5297 not attempt to terminate the conditional inside the included file.
5298
5299 @node Testing Flags,  , Conditional Syntax, Conditionals
5300 @section Conditionals that Test Flags
5301
5302 You can write a conditional that tests @code{make} command flags such as
5303 @samp{-t} by using the variable @code{MAKEFLAGS} together with the
5304 @code{findstring} function
5305 (@pxref{Text Functions, , Functions for String Substitution and Analysis}).
5306 This is useful when @code{touch} is not enough to make a file appear up
5307 to date.
5308
5309 The @code{findstring} function determines whether one string appears as a
5310 substring of another.  If you want to test for the @samp{-t} flag,
5311 use @samp{t} as the first string and the value of @code{MAKEFLAGS} as
5312 the other.
5313
5314 For example, here is how to arrange to use @samp{ranlib -t} to finish
5315 marking an archive file up to date:
5316
5317 @example
5318 archive.a: @dots{}
5319 ifneq (,$(findstring t,$(MAKEFLAGS)))
5320         +touch archive.a
5321         +ranlib -t archive.a
5322 else
5323         ranlib archive.a
5324 endif
5325 @end example
5326
5327 @noindent
5328 The @samp{+} prefix marks those command lines as ``recursive'' so
5329 that they will be executed despite use of the @samp{-t} flag.
5330 @xref{Recursion, ,Recursive Use of @code{make}}.
5331
5332 @node Functions, Running, Conditionals, Top
5333 @chapter Functions for Transforming Text
5334 @cindex functions
5335
5336 @dfn{Functions} allow you to do text processing in the makefile to compute
5337 the files to operate on or the commands to use.  You use a function in a
5338 @dfn{function call}, where you give the name of the function and some text
5339 (the @dfn{arguments}) for the function to operate on.  The result of the
5340 function's processing is substituted into the makefile at the point of the
5341 call, just as a variable might be substituted.
5342
5343 @menu
5344 * Syntax of Functions::         How to write a function call.
5345 * Text Functions::              General-purpose text manipulation functions.
5346 * File Name Functions::         Functions for manipulating file names.
5347 * Foreach Function::            Repeat some text with controlled variation.
5348 * If Function::                 Conditionally expand a value.
5349 * Call Function::               Expand a user-defined function.
5350 * Origin Function::             Find where a variable got its value.
5351 * Shell Function::              Substitute the output of a shell command.
5352 * Make Control Functions::      Functions that control how make runs.
5353 @end menu
5354
5355 @node Syntax of Functions, Text Functions,  , Functions
5356 @section Function Call Syntax
5357 @cindex @code{$}, in function call
5358 @cindex dollar sign (@code{$}), in function call
5359 @cindex arguments of functions
5360 @cindex functions, syntax of
5361
5362 A function call resembles a variable reference.  It looks like this:
5363
5364 @example
5365 $(@var{function} @var{arguments})
5366 @end example
5367
5368 @noindent
5369 or like this:
5370
5371 @example
5372 $@{@var{function} @var{arguments}@}
5373 @end example
5374
5375 Here @var{function} is a function name; one of a short list of names
5376 that are part of @code{make}.  You can also essentially create your own
5377 functions by using the @code{call} builtin function.
5378
5379 The @var{arguments} are the arguments of the function.  They are
5380 separated from the function name by one or more spaces or tabs, and if
5381 there is more than one argument, then they are separated by commas.
5382 Such whitespace and commas are not part of an argument's value.  The
5383 delimiters which you use to surround the function call, whether
5384 parentheses or braces, can appear in an argument only in matching pairs;
5385 the other kind of delimiters may appear singly.  If the arguments
5386 themselves contain other function calls or variable references, it is
5387 wisest to use the same kind of delimiters for all the references; write
5388 @w{@samp{$(subst a,b,$(x))}}, not @w{@samp{$(subst a,b,$@{x@})}}.  This
5389 is because it is clearer, and because only one type of delimiter is
5390 matched to find the end of the reference.
5391
5392 The text written for each argument is processed by substitution of
5393 variables and function calls to produce the argument value, which
5394 is the text on which the function acts.  The substitution is done in the
5395 order in which the arguments appear.
5396
5397 Commas and unmatched parentheses or braces cannot appear in the text of an
5398 argument as written; leading spaces cannot appear in the text of the first
5399 argument as written.  These characters can be put into the argument value
5400 by variable substitution.  First define variables @code{comma} and
5401 @code{space} whose values are isolated comma and space characters, then
5402 substitute these variables where such characters are wanted, like this:
5403
5404 @example
5405 @group
5406 comma:= ,
5407 empty:=
5408 space:= $(empty) $(empty)
5409 foo:= a b c
5410 bar:= $(subst $(space),$(comma),$(foo))
5411 # @r{bar is now `a,b,c'.}
5412 @end group
5413 @end example
5414
5415 @noindent
5416 Here the @code{subst} function replaces each space with a comma, through
5417 the value of @code{foo}, and substitutes the result.
5418
5419 @node Text Functions, File Name Functions, Syntax of Functions, Functions
5420 @section Functions for String Substitution and Analysis
5421 @cindex functions, for text
5422
5423 Here are some functions that operate on strings:
5424
5425 @table @code
5426 @item $(subst @var{from},@var{to},@var{text})
5427 @findex subst
5428 Performs a textual replacement on the text @var{text}: each occurrence
5429 of @var{from} is replaced by @var{to}.  The result is substituted for
5430 the function call.  For example,
5431
5432 @example
5433 $(subst ee,EE,feet on the street)
5434 @end example
5435
5436 substitutes the string @samp{fEEt on the strEEt}.
5437
5438 @item $(patsubst @var{pattern},@var{replacement},@var{text})
5439 @findex patsubst
5440 Finds whitespace-separated words in @var{text} that match
5441 @var{pattern} and replaces them with @var{replacement}.  Here
5442 @var{pattern} may contain a @samp{%} which acts as a wildcard,
5443 matching any number of any characters within a word.  If
5444 @var{replacement} also contains a @samp{%}, the @samp{%} is replaced
5445 by the text that matched the @samp{%} in @var{pattern}.@refill
5446
5447 @cindex @code{%}, quoting in @code{patsubst}
5448 @cindex @code{%}, quoting with @code{\} (backslash)
5449 @cindex @code{\} (backslash), to quote @code{%}
5450 @cindex backslash (@code{\}), to quote @code{%}
5451 @cindex quoting @code{%}, in @code{patsubst}
5452 @samp{%} characters in @code{patsubst} function invocations can be
5453 quoted with preceding backslashes (@samp{\}).  Backslashes that would
5454 otherwise quote @samp{%} characters can be quoted with more backslashes.
5455 Backslashes that quote @samp{%} characters or other backslashes are
5456 removed from the pattern before it is compared file names or has a stem
5457 substituted into it.  Backslashes that are not in danger of quoting
5458 @samp{%} characters go unmolested.  For example, the pattern
5459 @file{the\%weird\\%pattern\\} has @samp{the%weird\} preceding the
5460 operative @samp{%} character, and @samp{pattern\\} following it.  The
5461 final two backslashes are left alone because they cannot affect any
5462 @samp{%} character.@refill
5463
5464 Whitespace between words is folded into single space characters;
5465 leading and trailing whitespace is discarded.
5466
5467 For example,
5468
5469 @example
5470 $(patsubst %.c,%.o,x.c.c bar.c)
5471 @end example
5472
5473 @noindent
5474 produces the value @samp{x.c.o bar.o}.
5475
5476 Substitution references (@pxref{Substitution Refs, ,Substitution
5477 References}) are a simpler way to get the effect of the @code{patsubst}
5478 function:
5479
5480 @example
5481 $(@var{var}:@var{pattern}=@var{replacement})
5482 @end example
5483
5484 @noindent
5485 is equivalent to
5486
5487 @example
5488 $(patsubst @var{pattern},@var{replacement},$(@var{var}))
5489 @end example
5490
5491 The second shorthand simplifies one of the most common uses of
5492 @code{patsubst}: replacing the suffix at the end of file names.
5493
5494 @example
5495 $(@var{var}:@var{suffix}=@var{replacement})
5496 @end example
5497
5498 @noindent
5499 is equivalent to
5500
5501 @example
5502 $(patsubst %@var{suffix},%@var{replacement},$(@var{var}))
5503 @end example
5504
5505 @noindent
5506 For example, you might have a list of object files:
5507
5508 @example
5509 objects = foo.o bar.o baz.o
5510 @end example
5511
5512 @noindent
5513 To get the list of corresponding source files, you could simply write:
5514
5515 @example
5516 $(objects:.o=.c)
5517 @end example
5518
5519 @noindent
5520 instead of using the general form:
5521
5522 @example
5523 $(patsubst %.o,%.c,$(objects))
5524 @end example
5525
5526 @item $(strip @var{string})
5527 @cindex stripping whitespace
5528 @cindex whitespace, stripping
5529 @cindex spaces, stripping
5530 @findex strip
5531 Removes leading and trailing whitespace from @var{string} and replaces
5532 each internal sequence of one or more whitespace characters with a
5533 single space.  Thus, @samp{$(strip a b  c )} results in @w{@samp{a b c}}.
5534
5535 The function @code{strip} can be very useful when used in conjunction
5536 with conditionals.  When comparing something with the empty string
5537 @samp{} using @code{ifeq} or @code{ifneq}, you usually want a string of
5538 just whitespace to match the empty string (@pxref{Conditionals}).
5539
5540 Thus, the following may fail to have the desired results:
5541
5542 @example
5543 .PHONY: all
5544 ifneq   "$(needs_made)" ""
5545 all: $(needs_made)
5546 else
5547 all:;@@echo 'Nothing to make!'
5548 endif
5549 @end example
5550
5551 @noindent
5552 Replacing the variable reference @w{@samp{$(needs_made)}} with the
5553 function call @w{@samp{$(strip $(needs_made))}} in the @code{ifneq}
5554 directive would make it more robust.@refill
5555
5556 @item $(findstring @var{find},@var{in})
5557 @findex findstring
5558 @cindex searching for strings
5559 @cindex finding strings
5560 @cindex strings, searching for
5561 Searches @var{in} for an occurrence of @var{find}.  If it occurs, the
5562 value is @var{find}; otherwise, the value is empty.  You can use this
5563 function in a conditional to test for the presence of a specific
5564 substring in a given string.  Thus, the two examples,
5565
5566 @example
5567 $(findstring a,a b c)
5568 $(findstring a,b c)
5569 @end example
5570
5571 @noindent
5572 produce the values @samp{a} and @samp{} (the empty string),
5573 respectively.  @xref{Testing Flags}, for a practical application of
5574 @code{findstring}.@refill
5575
5576 @need 750
5577 @findex filter
5578 @cindex filtering words
5579 @cindex words, filtering
5580 @item $(filter @var{pattern}@dots{},@var{text})
5581 Returns all whitespace-separated words in @var{text} that @emph{do} match
5582 any of the @var{pattern} words, removing any words that @emph{do not}
5583 match.  The patterns are written using @samp{%}, just like the patterns
5584 used in the @code{patsubst} function above.@refill
5585
5586 The @code{filter} function can be used to separate out different types
5587 of strings (such as file names) in a variable.  For example:
5588
5589 @example
5590 sources := foo.c bar.c baz.s ugh.h
5591 foo: $(sources)
5592         cc $(filter %.c %.s,$(sources)) -o foo
5593 @end example
5594
5595 @noindent
5596 says that @file{foo} depends of @file{foo.c}, @file{bar.c},
5597 @file{baz.s} and @file{ugh.h} but only @file{foo.c}, @file{bar.c} and
5598 @file{baz.s} should be specified in the command to the
5599 compiler.@refill
5600
5601 @item $(filter-out @var{pattern}@dots{},@var{text})
5602 @findex filter-out
5603 @cindex filtering out words
5604 @cindex words, filtering out
5605 Returns all whitespace-separated words in @var{text} that @emph{do not}
5606 match any of the @var{pattern} words, removing the words that @emph{do}
5607 match one or more.  This is the exact opposite of the @code{filter}
5608 function.@refill
5609
5610 Removes all whitespace-separated words in @var{text} that @emph{do}
5611 match the @var{pattern} words, returning only the words that @emph{do
5612 not} match.  This is the exact opposite of the @code{filter}
5613 function.@refill
5614
5615 For example, given:
5616
5617 @example
5618 @group
5619 objects=main1.o foo.o main2.o bar.o
5620 mains=main1.o main2.o
5621 @end group
5622 @end example
5623
5624 @noindent
5625 the following generates a list which contains all the object files not
5626 in @samp{mains}:
5627
5628 @example
5629 $(filter-out $(mains),$(objects))
5630 @end example
5631
5632 @need 1500
5633 @findex sort
5634 @cindex sorting words
5635 @item $(sort @var{list})
5636 Sorts the words of @var{list} in lexical order, removing duplicate
5637 words.  The output is a list of words separated by single spaces.
5638 Thus,
5639
5640 @example
5641 $(sort foo bar lose)
5642 @end example
5643
5644 @noindent
5645 returns the value @samp{bar foo lose}.
5646
5647 @cindex removing duplicate words
5648 @cindex duplicate words, removing
5649 @cindex words, removing duplicates
5650 Incidentally, since @code{sort} removes duplicate words, you can use
5651 it for this purpose even if you don't care about the sort order.
5652 @end table
5653
5654 Here is a realistic example of the use of @code{subst} and
5655 @code{patsubst}.  Suppose that a makefile uses the @code{VPATH} variable
5656 to specify a list of directories that @code{make} should search for
5657 prerequisite files
5658 (@pxref{General Search, , @code{VPATH} Search Path for All Prerequisites}).
5659 This example shows how to
5660 tell the C compiler to search for header files in the same list of
5661 directories.@refill
5662
5663 The value of @code{VPATH} is a list of directories separated by colons,
5664 such as @samp{src:../headers}.  First, the @code{subst} function is used to
5665 change the colons to spaces:
5666
5667 @example
5668 $(subst :, ,$(VPATH))
5669 @end example
5670
5671 @noindent
5672 This produces @samp{src ../headers}.  Then @code{patsubst} is used to turn
5673 each directory name into a @samp{-I} flag.  These can be added to the
5674 value of the variable @code{CFLAGS}, which is passed automatically to the C
5675 compiler, like this:
5676
5677 @example
5678 override CFLAGS += $(patsubst %,-I%,$(subst :, ,$(VPATH)))
5679 @end example
5680
5681 @noindent
5682 The effect is to append the text @samp{-Isrc -I../headers} to the
5683 previously given value of @code{CFLAGS}.  The @code{override} directive is
5684 used so that the new value is assigned even if the previous value of
5685 @code{CFLAGS} was specified with a command argument (@pxref{Override
5686 Directive, , The @code{override} Directive}).
5687
5688 @node File Name Functions, Foreach Function, Text Functions, Functions
5689 @section Functions for File Names
5690 @cindex functions, for file names
5691 @cindex file name functions
5692
5693 Several of the built-in expansion functions relate specifically to
5694 taking apart file names or lists of file names.
5695
5696 Each of the following functions performs a specific transformation on a
5697 file name.  The argument of the function is regarded as a series of file
5698 names, separated by whitespace.  (Leading and trailing whitespace is
5699 ignored.)  Each file name in the series is transformed in the same way and
5700 the results are concatenated with single spaces between them.
5701
5702 @table @code
5703 @item $(dir @var{names}@dots{})
5704 @findex dir
5705 @cindex directory part
5706 @cindex file name, directory part
5707 Extracts the directory-part of each file name in @var{names}.  The
5708 directory-part of the file name is everything up through (and
5709 including) the last slash in it.  If the file name contains no slash,
5710 the directory part is the string @samp{./}.  For example,
5711
5712 @example
5713 $(dir src/foo.c hacks)
5714 @end example
5715
5716 @noindent
5717 produces the result @samp{src/ ./}.
5718
5719 @item $(notdir @var{names}@dots{})
5720 @findex notdir
5721 @cindex file name, nondirectory part
5722 @cindex nondirectory part
5723 Extracts all but the directory-part of each file name in @var{names}.
5724 If the file name contains no slash, it is left unchanged.  Otherwise,
5725 everything through the last slash is removed from it.
5726
5727 A file name that ends with a slash becomes an empty string.  This is
5728 unfortunate, because it means that the result does not always have the
5729 same number of whitespace-separated file names as the argument had;
5730 but we do not see any other valid alternative.
5731
5732 For example,
5733
5734 @example
5735 $(notdir src/foo.c hacks)
5736 @end example
5737
5738 @noindent
5739 produces the result @samp{foo.c hacks}.
5740
5741 @item $(suffix @var{names}@dots{})
5742 @findex suffix
5743 @cindex suffix, function to find
5744 @cindex file name suffix
5745 Extracts the suffix of each file name in @var{names}.  If the file name
5746 contains a period, the suffix is everything starting with the last
5747 period.  Otherwise, the suffix is the empty string.  This frequently
5748 means that the result will be empty when @var{names} is not, and if
5749 @var{names} contains multiple file names, the result may contain fewer
5750 file names.
5751
5752 For example,
5753
5754 @example
5755 $(suffix src/foo.c src-1.0/bar.c hacks)
5756 @end example
5757
5758 @noindent
5759 produces the result @samp{.c .c}.
5760
5761 @item $(basename @var{names}@dots{})
5762 @findex basename
5763 @cindex basename
5764 @cindex file name, basename of
5765 Extracts all but the suffix of each file name in @var{names}.  If the
5766 file name contains a period, the basename is everything starting up to
5767 (and not including) the last period.  Periods in the directory part are
5768 ignored.  If there is no period, the basename is the entire file name.
5769 For example,
5770
5771 @example
5772 $(basename src/foo.c src-1.0/bar hacks)
5773 @end example
5774
5775 @noindent
5776 produces the result @samp{src/foo src-1.0/bar hacks}.
5777
5778 @c plural convention with dots (be consistent)
5779 @item $(addsuffix @var{suffix},@var{names}@dots{})
5780 @findex addsuffix
5781 @cindex suffix, adding
5782 @cindex file name suffix, adding
5783 The argument @var{names} is regarded as a series of names, separated
5784 by whitespace; @var{suffix} is used as a unit.  The value of
5785 @var{suffix} is appended to the end of each individual name and the
5786 resulting larger names are concatenated with single spaces between
5787 them.  For example,
5788
5789 @example
5790 $(addsuffix .c,foo bar)
5791 @end example
5792
5793 @noindent
5794 produces the result @samp{foo.c bar.c}.
5795
5796 @item $(addprefix @var{prefix},@var{names}@dots{})
5797 @findex addprefix
5798 @cindex prefix, adding
5799 @cindex file name prefix, adding
5800 The argument @var{names} is regarded as a series of names, separated
5801 by whitespace; @var{prefix} is used as a unit.  The value of
5802 @var{prefix} is prepended to the front of each individual name and the
5803 resulting larger names are concatenated with single spaces between
5804 them.  For example,
5805
5806 @example
5807 $(addprefix src/,foo bar)
5808 @end example
5809
5810 @noindent
5811 produces the result @samp{src/foo src/bar}.
5812
5813 @item $(join @var{list1},@var{list2})
5814 @findex join
5815 @cindex joining lists of words
5816 @cindex words, joining lists
5817 Concatenates the two arguments word by word: the two first words (one
5818 from each argument) concatenated form the first word of the result, the
5819 two second words form the second word of the result, and so on.  So the
5820 @var{n}th word of the result comes from the @var{n}th word of each
5821 argument.  If one argument has more words that the other, the extra
5822 words are copied unchanged into the result.
5823
5824 For example, @samp{$(join a b,.c .o)} produces @samp{a.c b.o}.
5825
5826 Whitespace between the words in the lists is not preserved; it is
5827 replaced with a single space.
5828
5829 This function can merge the results of the @code{dir} and
5830 @code{notdir} functions, to produce the original list of files which
5831 was given to those two functions.@refill
5832
5833 @item $(word @var{n},@var{text})
5834 @findex word
5835 @cindex word, selecting a
5836 @cindex selecting a word
5837 Returns the @var{n}th word of @var{text}.  The legitimate values of
5838 @var{n} start from 1.  If @var{n} is bigger than the number of words
5839 in @var{text}, the value is empty.  For example,
5840
5841 @example
5842 $(word 2, foo bar baz)
5843 @end example
5844
5845 @noindent
5846 returns @samp{bar}.
5847
5848 @item $(wordlist @var{s},@var{e},@var{text})
5849 @findex wordlist
5850 @cindex words, selecting lists of
5851 @cindex selecting word lists
5852 Returns the list of words in @var{text} starting with word @var{s} and
5853 ending with word @var{e} (inclusive).  The legitimate values of @var{s}
5854 and @var{e} start from 1.  If @var{s} is bigger than the number of words
5855 in @var{text}, the value is empty.  If @var{e} is bigger than the number
5856 of words in @var{text}, words up to the end of @var{text} are returned.
5857 If @var{s} is greater than @var{e}, nothing is returned.  For example,
5858
5859 @example
5860 $(wordlist 2, 3, foo bar baz)
5861 @end example
5862
5863 @noindent
5864 returns @samp{bar baz}.
5865
5866 @c Following item phrased to prevent overfull hbox.  --RJC 17 Jul 92
5867 @item $(words @var{text})
5868 @findex words
5869 @cindex words, finding number
5870 Returns the number of words in @var{text}.
5871 Thus, the last word of @var{text} is
5872 @w{@code{$(word $(words @var{text}),@var{text})}}.@refill
5873
5874 @item $(firstword @var{names}@dots{})
5875 @findex firstword
5876 @cindex words, extracting first
5877 The argument @var{names} is regarded as a series of names, separated
5878 by whitespace.  The value is the first name in the series.  The rest
5879 of the names are ignored.
5880
5881 For example,
5882
5883 @example
5884 $(firstword foo bar)
5885 @end example
5886
5887 @noindent
5888 produces the result @samp{foo}.  Although @code{$(firstword
5889 @var{text})} is the same as @code{$(word 1,@var{text})}, the
5890 @code{firstword} function is retained for its simplicity.@refill
5891
5892 @item $(wildcard @var{pattern})
5893 @findex wildcard
5894 @cindex wildcard, function
5895 The argument @var{pattern} is a file name pattern, typically containing
5896 wildcard characters (as in shell file name patterns).  The result of
5897 @code{wildcard} is a space-separated list of the names of existing files
5898 that match the pattern.
5899 @xref{Wildcards, ,Using Wildcard Characters in File Names}.
5900 @end table
5901
5902 @node Foreach Function, If Function, File Name Functions, Functions
5903 @section The @code{foreach} Function
5904 @findex foreach
5905 @cindex words, iterating over
5906
5907 The @code{foreach} function is very different from other functions.  It
5908 causes one piece of text to be used repeatedly, each time with a different
5909 substitution performed on it.  It resembles the @code{for} command in the
5910 shell @code{sh} and the @code{foreach} command in the C-shell @code{csh}.
5911
5912 The syntax of the @code{foreach} function is:
5913
5914 @example
5915 $(foreach @var{var},@var{list},@var{text})
5916 @end example
5917
5918 @noindent
5919 The first two arguments, @var{var} and @var{list}, are expanded before
5920 anything else is done; note that the last argument, @var{text}, is
5921 @strong{not} expanded at the same time.  Then for each word of the expanded
5922 value of @var{list}, the variable named by the expanded value of @var{var}
5923 is set to that word, and @var{text} is expanded.  Presumably @var{text}
5924 contains references to that variable, so its expansion will be different
5925 each time.
5926
5927 The result is that @var{text} is expanded as many times as there are
5928 whitespace-separated words in @var{list}.  The multiple expansions of
5929 @var{text} are concatenated, with spaces between them, to make the result
5930 of @code{foreach}.
5931
5932 This simple example sets the variable @samp{files} to the list of all files
5933 in the directories in the list @samp{dirs}:
5934
5935 @example
5936 dirs := a b c d
5937 files := $(foreach dir,$(dirs),$(wildcard $(dir)/*))
5938 @end example
5939
5940 Here @var{text} is @samp{$(wildcard $(dir)/*)}.  The first repetition
5941 finds the value @samp{a} for @code{dir}, so it produces the same result
5942 as @samp{$(wildcard a/*)}; the second repetition produces the result
5943 of @samp{$(wildcard b/*)}; and the third, that of @samp{$(wildcard c/*)}.
5944
5945 This example has the same result (except for setting @samp{dirs}) as
5946 the following example:
5947
5948 @example
5949 files := $(wildcard a/* b/* c/* d/*)
5950 @end example
5951
5952 When @var{text} is complicated, you can improve readability by giving it
5953 a name, with an additional variable:
5954
5955 @example
5956 find_files = $(wildcard $(dir)/*)
5957 dirs := a b c d
5958 files := $(foreach dir,$(dirs),$(find_files))
5959 @end example
5960
5961 @noindent
5962 Here we use the variable @code{find_files} this way.  We use plain @samp{=}
5963 to define a recursively-expanding variable, so that its value contains an
5964 actual function call to be reexpanded under the control of @code{foreach};
5965 a simply-expanded variable would not do, since @code{wildcard} would be
5966 called only once at the time of defining @code{find_files}.
5967
5968 The @code{foreach} function has no permanent effect on the variable
5969 @var{var}; its value and flavor after the @code{foreach} function call are
5970 the same as they were beforehand.  The other values which are taken from
5971 @var{list} are in effect only temporarily, during the execution of
5972 @code{foreach}.  The variable @var{var} is a simply-expanded variable
5973 during the execution of @code{foreach}.  If @var{var} was undefined
5974 before the @code{foreach} function call, it is undefined after the call.
5975 @xref{Flavors, ,The Two Flavors of Variables}.@refill
5976
5977 You must take care when using complex variable expressions that result in
5978 variable names because many strange things are valid variable names, but
5979 are probably not what you intended.  For example,
5980
5981 @smallexample
5982 files := $(foreach Esta escrito en espanol!,b c ch,$(find_files))
5983 @end smallexample
5984
5985 @noindent
5986 might be useful if the value of @code{find_files} references the variable
5987 whose name is @samp{Esta escrito en espanol!} (es un nombre bastante largo,
5988 no?), but it is more likely to be a mistake.
5989
5990 @node If Function, Call Function, Foreach Function, Functions
5991 @section The @code{if} Function
5992 @findex if
5993 @cindex conditional expansion
5994
5995 The @code{if} function provides support for conditional expansion in a
5996 functional context (as opposed to the GNU @code{make} makefile
5997 conditionals such as @code{ifeq} (@pxref{Conditional Syntax, ,Syntax of
5998 Conditionals}).
5999
6000 An @code{if} function call can contain either two or three arguments:
6001
6002 @example
6003 $(if @var{condition},@var{then-part}[,@var{else-part}])
6004 @end example
6005
6006 The first argument, @var{condition}, first has all preceding and
6007 trailing whitespace stripped, then is expanded.  If it expands to any
6008 non-empty string, then the condition is considered to be true.  If it
6009 expands to an empty string, the condition is considered to be false.
6010
6011 If the condition is true then the second argument, @var{then-part}, is
6012 evaluated and this is used as the result of the evaluation of the entire
6013 @code{if} function.
6014
6015 If the condition is false then the third argument, @var{else-part}, is
6016 evaluated and this is the result of the @code{if} function.  If there is
6017 no third argument, the @code{if} function evaluates to nothing (the
6018 empty string).
6019
6020 Note that only one of the @var{then-part} or the @var{else-part} will be
6021 evaluated, never both.  Thus, either can contain side-effects (such as
6022 @code{shell} function calls, etc.)
6023
6024 @node Call Function, Origin Function, If Function, Functions
6025 @section The @code{call} Function
6026 @findex call
6027 @cindex functions, user defined
6028 @cindex user defined functions
6029
6030 The @code{call} function is unique in that it can be used to create new
6031 parameterized functions.  You can write a complex expression as the
6032 value of a variable, then use @code{call} to expand it with different
6033 values.
6034
6035 The syntax of the @code{call} function is:
6036
6037 @example
6038 $(call @var{variable},@var{param},@var{param},@dots{})
6039 @end example
6040
6041 When @code{make} expands this function, it assigns each @var{param} to
6042 temporary variables @code{$(1)}, @code{$(2)}, etc.  The variable
6043 @code{$(0)} will contain @var{variable}.  There is no maximum number of
6044 parameter arguments.  There is no minimum, either, but it doesn't make
6045 sense to use @code{call} with no parameters.
6046
6047 Then @var{variable} is expanded as a @code{make} variable in the context
6048 of these temporary assignments.  Thus, any reference to @code{$(1)} in
6049 the value of @var{variable} will resolve to the first @var{param} in the
6050 invocation of @code{call}.
6051
6052 Note that @var{variable} is the @emph{name} of a variable, not a
6053 @emph{reference} to that variable.  Therefore you would not normally use
6054 a @samp{$} or parentheses when writing it.  (You can, however, use a
6055 variable reference in the name if you want the name not to be a
6056 constant.)
6057
6058 If @var{variable} is the name of a builtin function, the builtin function
6059 is always invoked (even if a @code{make} variable by that name also
6060 exists).
6061
6062 The @code{call} function expands the @var{param} arguments before
6063 assigning them to temporary variables.  This means that @var{variable}
6064 values containing references to builtin functions that have special
6065 expansion rules, like @code{foreach} or @code{if}, may not work as you
6066 expect.
6067
6068 Some examples may make this clearer.
6069
6070 This macro simply reverses its arguments:
6071
6072 @smallexample
6073 reverse = $(2) $(1)
6074
6075 foo = $(call reverse,a,b)
6076 @end smallexample
6077
6078 @noindent
6079 Here @var{foo} will contain @samp{b a}.
6080
6081 This one is slightly more interesting: it defines a macro to search for
6082 the first instance of a program in @code{PATH}:
6083
6084 @smallexample
6085 pathsearch = $(firstword $(wildcard $(addsufix /$(1),$(subst :, ,$(PATH)))))
6086
6087 LS := $(call pathsearch,ls)
6088 @end smallexample
6089
6090 @noindent
6091 Now the variable LS contains @code{/bin/ls} or similar.
6092
6093 The @code{call} function can be nested.  Each recursive invocation gets
6094 its own local values for @code{$(1)}, etc. that mask the values of
6095 higher-level @code{call}.  For example, here is an implementation of a
6096 @dfn{map} function:
6097
6098 @smallexample
6099 map = $(foreach a,$(2),$(call $(1),$(a)))
6100 @end smallexample
6101
6102 Now you can @var{map} a function that normally takes only one argument,
6103 such as @code{origin}, to multiple values in one step:
6104
6105 @smallexample
6106 o = $(call map,origin,o map MAKE)
6107 @end smallexample
6108
6109 and end up with @var{o} containing something like @samp{file file default}.
6110
6111 A final caution: be careful when adding whitespace to the arguments to
6112 @code{call}.  As with other functions, any whitespace contained in the
6113 second and subsequent arguments is kept; this can cause strange
6114 effects.  It's generally safest to remove all extraneous whitespace when
6115 providing parameters to @code{call}.
6116
6117
6118 @node Origin Function, Shell Function, Call Function, Functions
6119 @section The @code{origin} Function
6120 @findex origin
6121 @cindex variables, origin of
6122 @cindex origin of variable
6123
6124 The @code{origin} function is unlike most other functions in that it does
6125 not operate on the values of variables; it tells you something @emph{about}
6126 a variable.  Specifically, it tells you where it came from.
6127
6128 The syntax of the @code{origin} function is:
6129
6130 @example
6131 $(origin @var{variable})
6132 @end example
6133
6134 Note that @var{variable} is the @emph{name} of a variable to inquire about;
6135 not a @emph{reference} to that variable.  Therefore you would not normally
6136 use a @samp{$} or parentheses when writing it.  (You can, however, use a
6137 variable reference in the name if you want the name not to be a constant.)
6138
6139 The result of this function is a string telling you how the variable
6140 @var{variable} was defined:
6141
6142 @table @samp
6143 @item undefined
6144
6145 if @var{variable} was never defined.
6146
6147 @item default
6148
6149 if @var{variable} has a default definition, as is usual with @code{CC}
6150 and so on.  @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
6151 Note that if you have redefined a default variable, the @code{origin}
6152 function will return the origin of the later definition.
6153
6154 @item environment
6155
6156 if @var{variable} was defined as an environment variable and the
6157 @samp{-e} option is @emph{not} turned on (@pxref{Options Summary, ,Summary of Options}).
6158
6159 @item environment override
6160
6161 if @var{variable} was defined as an environment variable and the
6162 @w{@samp{-e}} option @emph{is} turned on (@pxref{Options Summary,
6163 ,Summary of Options}).@refill
6164
6165 @item file
6166
6167 if @var{variable} was defined in a makefile.
6168
6169 @item command line
6170
6171 if @var{variable} was defined on the command line.
6172
6173 @item override
6174
6175 if @var{variable} was defined with an @code{override} directive in a
6176 makefile (@pxref{Override Directive, ,The @code{override} Directive}).
6177
6178 @item automatic
6179
6180 if @var{variable} is an automatic variable defined for the
6181 execution of the commands for each rule
6182 (@pxref{Automatic, , Automatic Variables}).
6183 @end table
6184
6185 This information is primarily useful (other than for your curiosity) to
6186 determine if you want to believe the value of a variable.  For example,
6187 suppose you have a makefile @file{foo} that includes another makefile
6188 @file{bar}.  You want a variable @code{bletch} to be defined in @file{bar}
6189 if you run the command @w{@samp{make -f bar}}, even if the environment contains
6190 a definition of @code{bletch}.  However, if @file{foo} defined
6191 @code{bletch} before including @file{bar}, you do not want to override that
6192 definition.  This could be done by using an @code{override} directive in
6193 @file{foo}, giving that definition precedence over the later definition in
6194 @file{bar}; unfortunately, the @code{override} directive would also
6195 override any command line definitions.  So, @file{bar} could
6196 include:@refill
6197
6198 @example
6199 @group
6200 ifdef bletch
6201 ifeq "$(origin bletch)" "environment"
6202 bletch = barf, gag, etc.
6203 endif
6204 endif
6205 @end group
6206 @end example
6207
6208 @noindent
6209 If @code{bletch} has been defined from the environment, this will redefine
6210 it.
6211
6212 If you want to override a previous definition of @code{bletch} if it came
6213 from the environment, even under @samp{-e}, you could instead write:
6214
6215 @example
6216 @group
6217 ifneq "$(findstring environment,$(origin bletch))" ""
6218 bletch = barf, gag, etc.
6219 endif
6220 @end group
6221 @end example
6222
6223 Here the redefinition takes place if @samp{$(origin bletch)} returns either
6224 @samp{environment} or @samp{environment override}.
6225 @xref{Text Functions, , Functions for String Substitution and Analysis}.
6226
6227 @node Shell Function, Make Control Functions, Origin Function, Functions
6228 @section The @code{shell} Function
6229 @findex shell
6230 @cindex commands, expansion
6231 @cindex backquotes
6232 @cindex shell command, function for
6233
6234 The @code{shell} function is unlike any other function except the
6235 @code{wildcard} function
6236 (@pxref{Wildcard Function, ,The Function @code{wildcard}}) in that it
6237 communicates with the world outside of @code{make}.
6238
6239 The @code{shell} function performs the same function that backquotes
6240 (@samp{`}) perform in most shells: it does @dfn{command expansion}.  This
6241 means that it takes an argument that is a shell command and returns the
6242 output of the command.  The only processing @code{make} does on the result,
6243 before substituting it into the surrounding text, is to convert each
6244 newline or carriage-return / newline pair to a single space.  It also
6245 removes the trailing (carriage-return and) newline, if it's the last
6246 thing in the result.@refill
6247
6248 The commands run by calls to the @code{shell} function are run when the
6249 function calls are expanded.  In most cases, this is when the makefile is
6250 read in.  The exception is that function calls in the commands of the rules
6251 are expanded when the commands are run, and this applies to @code{shell}
6252 function calls like all others.
6253
6254 Here are some examples of the use of the @code{shell} function:
6255
6256 @example
6257 contents := $(shell cat foo)
6258 @end example
6259
6260 @noindent
6261 sets @code{contents} to the contents of the file @file{foo}, with a space
6262 (rather than a newline) separating each line.
6263
6264 @example
6265 files := $(shell echo *.c)
6266 @end example
6267
6268 @noindent
6269 sets @code{files} to the expansion of @samp{*.c}.  Unless @code{make} is
6270 using a very strange shell, this has the same result as
6271 @w{@samp{$(wildcard *.c)}}.@refill
6272
6273 @node Make Control Functions,  , Shell Function, Functions
6274 @section Functions That Control Make
6275 @cindex functions, for controlling make
6276 @cindex controlling make
6277
6278 These functions control the way make runs.  Generally, they are used to
6279 provide information to the user of the makefile or to cause make to stop
6280 if some sort of environmental error is detected.
6281
6282 @table @code
6283 @item $(error @var{text}@dots{})
6284 @findex error
6285 @cindex error, stopping on
6286 @cindex stopping make
6287 Generates a fatal error where the message is @var{text}.  Note that the
6288 error is generated whenever this function is evaluated.  So, if you put
6289 it inside a command script or on the right side of a recursive variable
6290 assignment, it won't be evaluated until later.  The @var{text} will be
6291 expanded before the error is generated.
6292
6293 For example,
6294
6295 @example
6296 ifdef ERROR1
6297 $(error error is $(ERROR1))
6298 endif
6299 @end example
6300
6301 @noindent
6302 will generate a fatal error during the read of the makefile if the
6303 @code{make} variable @code{ERROR1} is defined.  Or,
6304
6305 @example
6306 ERR = $(error found an error!)
6307
6308 .PHONY: err
6309 err: ; $(ERR)
6310 @end example
6311
6312 @noindent
6313 will generate a fatal error while @code{make} is running, if the
6314 @code{err} target is invoked.
6315
6316 @item $(warning @var{text}@dots{})
6317 @findex warning
6318 @cindex warnings, printing
6319 @cindex printing user warnings
6320 This function works similarly to the @code{error} function, above,
6321 except that @code{make} doesn't exit.  Instead, @var{text} is expanded
6322 and the resulting message is displayed, but processing of the makefile
6323 continues.
6324
6325 The result of the expansion of this function is the empty string.
6326 @end table
6327
6328 @node Running, Implicit Rules, Functions, Top
6329 @chapter How to Run @code{make}
6330
6331 A makefile that says how to recompile a program can be used in more
6332 than one way.  The simplest use is to recompile every file that is out
6333 of date.  Usually, makefiles are written so that if you run
6334 @code{make} with no arguments, it does just that.
6335
6336 But you might want to update only some of the files; you might want to use
6337 a different compiler or different compiler options; you might want just to
6338 find out which files are out of date without changing them.
6339
6340 By giving arguments when you run @code{make}, you can do any of these
6341 things and many others.
6342
6343 The exit status of @code{make} is always one of three values:
6344 @table @code
6345 @item 0
6346 The exit status is zero if @code{make} is successful.
6347 @item 2
6348 The exit status is two if @code{make} encounters any errors.
6349 It will print messages describing the particular errors.
6350 @item 1
6351 The exit status is one if you use the @samp{-q} flag and @code{make}
6352 determines that some target is not already up to date.
6353 @xref{Instead of Execution, ,Instead of Executing the Commands}.
6354 @end table
6355
6356 @menu
6357 * Makefile Arguments::          How to specify which makefile to use.
6358 * Goals::                       How to use goal arguments to specify which
6359                                   parts of the makefile to use.
6360 * Instead of Execution::        How to use mode flags to specify what
6361                                   kind of thing to do with the commands
6362                                   in the makefile other than simply
6363                                   execute them.
6364 * Avoiding Compilation::        How to avoid recompiling certain files.
6365 * Overriding::                  How to override a variable to specify
6366                                   an alternate compiler and other things.
6367 * Testing::                     How to proceed past some errors, to
6368                                   test compilation.
6369 * Options Summary::             Summary of Options
6370 @end menu
6371
6372 @node Makefile Arguments, Goals,  , Running
6373 @section Arguments to Specify the Makefile
6374 @cindex @code{--file}
6375 @cindex @code{--makefile}
6376 @cindex @code{-f}
6377
6378 The way to specify the name of the makefile is with the @samp{-f} or
6379 @samp{--file} option (@samp{--makefile} also works).  For example,
6380 @samp{-f altmake} says to use the file @file{altmake} as the makefile.
6381
6382 If you use the @samp{-f} flag several times and follow each @samp{-f}
6383 with an argument, all the specified files are used jointly as
6384 makefiles.
6385
6386 If you do not use the @samp{-f} or @samp{--file} flag, the default is
6387 to try @file{GNUmakefile}, @file{makefile}, and @file{Makefile}, in
6388 that order, and use the first of these three which exists or can be made
6389 (@pxref{Makefiles, ,Writing Makefiles}).@refill
6390
6391 @node Goals, Instead of Execution, Makefile Arguments, Running
6392 @section Arguments to Specify the Goals
6393 @cindex goal, how to specify
6394
6395 The @dfn{goals} are the targets that @code{make} should strive ultimately
6396 to update.  Other targets are updated as well if they appear as
6397 prerequisites of goals, or prerequisites of prerequisites of goals, etc.
6398
6399 By default, the goal is the first target in the makefile (not counting
6400 targets that start with a period).  Therefore, makefiles are usually
6401 written so that the first target is for compiling the entire program or
6402 programs they describe.  If the first rule in the makefile has several
6403 targets, only the first target in the rule becomes the default goal, not
6404 the whole list.
6405
6406 You can specify a different goal or goals with arguments to @code{make}.
6407 Use the name of the goal as an argument.  If you specify several goals,
6408 @code{make} processes each of them in turn, in the order you name them.
6409
6410 Any target in the makefile may be specified as a goal (unless it
6411 starts with @samp{-} or contains an @samp{=}, in which case it will be
6412 parsed as a switch or variable definition, respectively).  Even
6413 targets not in the makefile may be specified, if @code{make} can find
6414 implicit rules that say how to make them.
6415
6416 @cindex @code{MAKECMDGOALS}
6417 @vindex MAKECMDGOALS
6418 @code{Make} will set the special variable @code{MAKECMDGOALS} to the
6419 list of goals you specified on the command line.  If no goals were given
6420 on the command line, this variable is empty.  Note that this variable
6421 should be used only in special circumstances.
6422
6423 An example of appropriate use is to avoid including @file{.d} files
6424 during @code{clean} rules (@pxref{Automatic Prerequisites}), so
6425 @code{make} won't create them only to immediately remove them
6426 again:@refill
6427
6428 @example
6429 @group
6430 sources = foo.c bar.c
6431
6432 ifneq ($(MAKECMDGOALS),clean)
6433 include $(sources:.c=.d)
6434 endif
6435 @end group
6436 @end example
6437
6438 One use of specifying a goal is if you want to compile only a part of
6439 the program, or only one of several programs.  Specify as a goal each
6440 file that you wish to remake.  For example, consider a directory containing
6441 several programs, with a makefile that starts like this:
6442
6443 @example
6444 .PHONY: all
6445 all: size nm ld ar as
6446 @end example
6447
6448 If you are working on the program @code{size}, you might want to say
6449 @w{@samp{make size}} so that only the files of that program are recompiled.
6450
6451 Another use of specifying a goal is to make files that are not normally
6452 made.  For example, there may be a file of debugging output, or a
6453 version of the program that is compiled specially for testing, which has
6454 a rule in the makefile but is not a prerequisite of the default goal.
6455
6456 Another use of specifying a goal is to run the commands associated with
6457 a phony target (@pxref{Phony Targets}) or empty target (@pxref{Empty
6458 Targets, ,Empty Target Files to Record Events}).  Many makefiles contain
6459 a phony target named @file{clean} which deletes everything except source
6460 files.  Naturally, this is done only if you request it explicitly with
6461 @w{@samp{make clean}}.  Following is a list of typical phony and empty
6462 target names.  @xref{Standard Targets}, for a detailed list of all the
6463 standard target names which GNU software packages use.
6464
6465 @table @file
6466 @item all
6467 @cindex @code{all} @r{(standard target)}
6468 Make all the top-level targets the makefile knows about.
6469
6470 @item clean
6471 @cindex @code{clean} @r{(standard target)}
6472 Delete all files that are normally created by running @code{make}.
6473
6474 @item mostlyclean
6475 @cindex @code{mostlyclean} @r{(standard target)}
6476 Like @samp{clean}, but may refrain from deleting a few files that people
6477 normally don't want to recompile.  For example, the @samp{mostlyclean}
6478 target for GCC does not delete @file{libgcc.a}, because recompiling it
6479 is rarely necessary and takes a lot of time.
6480
6481 @item distclean
6482 @cindex @code{distclean} @r{(standard target)}
6483 @itemx realclean
6484 @cindex @code{realclean} @r{(standard target)}
6485 @itemx clobber
6486 @cindex @code{clobber} @r{(standard target)}
6487 Any of these targets might be defined to delete @emph{more} files than
6488 @samp{clean} does.  For example, this would delete configuration files
6489 or links that you would normally create as preparation for compilation,
6490 even if the makefile itself cannot create these files.
6491
6492 @item install
6493 @cindex @code{install} @r{(standard target)}
6494 Copy the executable file into a directory that users typically search
6495 for commands; copy any auxiliary files that the executable uses into
6496 the directories where it will look for them.
6497
6498 @item print
6499 @cindex @code{print} @r{(standard target)}
6500 Print listings of the source files that have changed.
6501
6502 @item tar
6503 @cindex @code{tar} @r{(standard target)}
6504 Create a tar file of the source files.
6505
6506 @item shar
6507 @cindex @code{shar} @r{(standard target)}
6508 Create a shell archive (shar file) of the source files.
6509
6510 @item dist
6511 @cindex @code{dist} @r{(standard target)}
6512 Create a distribution file of the source files.  This might
6513 be a tar file, or a shar file, or a compressed version of one of the
6514 above, or even more than one of the above.
6515
6516 @item TAGS
6517 @cindex @code{TAGS} @r{(standard target)}
6518 Update a tags table for this program.
6519
6520 @item check
6521 @cindex @code{check} @r{(standard target)}
6522 @itemx test
6523 @cindex @code{test} @r{(standard target)}
6524 Perform self tests on the program this makefile builds.
6525 @end table
6526
6527 @node Instead of Execution, Avoiding Compilation, Goals, Running
6528 @section Instead of Executing the Commands
6529 @cindex execution, instead of
6530 @cindex commands, instead of executing
6531
6532 The makefile tells @code{make} how to tell whether a target is up to date,
6533 and how to update each target.  But updating the targets is not always
6534 what you want.  Certain options specify other activities for @code{make}.
6535
6536 @comment Extra blank lines make it print better.
6537 @table @samp
6538 @item -n
6539 @itemx --just-print
6540 @itemx --dry-run
6541 @itemx --recon
6542 @cindex @code{--just-print}
6543 @cindex @code{--dry-run}
6544 @cindex @code{--recon}
6545 @cindex @code{-n}
6546
6547 ``No-op''.  The activity is to print what commands would be used to make
6548 the targets up to date, but not actually execute them.
6549
6550 @item -t
6551 @itemx --touch
6552 @cindex @code{--touch}
6553 @cindex touching files
6554 @cindex target, touching
6555 @cindex @code{-t}
6556
6557 ``Touch''.  The activity is to mark the targets as up to date without
6558 actually changing them.  In other words, @code{make} pretends to compile
6559 the targets but does not really change their contents.
6560
6561 @item -q
6562 @itemx --question
6563 @cindex @code{--question}
6564 @cindex @code{-q}
6565 @cindex question mode
6566
6567 ``Question''.  The activity is to find out silently whether the targets
6568 are up to date already; but execute no commands in either case.  In other
6569 words, neither compilation nor output will occur.
6570
6571 @item -W @var{file}
6572 @itemx --what-if=@var{file}
6573 @itemx --assume-new=@var{file}
6574 @itemx --new-file=@var{file}
6575 @cindex @code{--what-if}
6576 @cindex @code{-W}
6577 @cindex @code{--assume-new}
6578 @cindex @code{--new-file}
6579 @cindex what if
6580 @cindex files, assuming new
6581
6582 ``What if''.  Each @samp{-W} flag is followed by a file name.  The given
6583 files' modification times are recorded by @code{make} as being the present
6584 time, although the actual modification times remain the same.
6585 You can use the @samp{-W} flag in conjunction with the @samp{-n} flag
6586 to see what would happen if you were to modify specific files.@refill
6587 @end table
6588
6589 With the @samp{-n} flag, @code{make} prints the commands that it would
6590 normally execute but does not execute them.
6591
6592 With the @samp{-t} flag, @code{make} ignores the commands in the rules
6593 and uses (in effect) the command @code{touch} for each target that needs to
6594 be remade.  The @code{touch} command is also printed, unless @samp{-s} or
6595 @code{.SILENT} is used.  For speed, @code{make} does not actually invoke
6596 the program @code{touch}.  It does the work directly.
6597
6598 With the @samp{-q} flag, @code{make} prints nothing and executes no
6599 commands, but the exit status code it returns is zero if and only if the
6600 targets to be considered are already up to date.  If the exit status is
6601 one, then some updating needs to be done.  If @code{make} encounters an
6602 error, the exit status is two, so you can distinguish an error from a
6603 target that is not up to date.
6604
6605 It is an error to use more than one of these three flags in the same
6606 invocation of @code{make}.
6607
6608 The @samp{-n}, @samp{-t}, and @samp{-q} options do not affect command
6609 lines that begin with @samp{+} characters or contain the strings
6610 @samp{$(MAKE)} or @samp{$@{MAKE@}}.  Note that only the line containing
6611 the @samp{+} character or the strings @samp{$(MAKE)} or @samp{$@{MAKE@}}
6612 is run regardless of these options.  Other lines in the same rule are
6613 not run unless they too begin with @samp{+} or contain @samp{$(MAKE)} or
6614 @samp{$@{MAKE@}} (@xref{MAKE Variable, ,How the @code{MAKE} Variable Works}.)
6615
6616 The @samp{-W} flag provides two features:
6617
6618 @itemize @bullet
6619 @item
6620 If you also use the @samp{-n} or @samp{-q} flag, you can see what
6621 @code{make} would do if you were to modify some files.
6622
6623 @item
6624 Without the @samp{-n} or @samp{-q} flag, when @code{make} is actually
6625 executing commands, the @samp{-W} flag can direct @code{make} to act
6626 as if some files had been modified, without actually modifying the
6627 files.@refill
6628 @end itemize
6629
6630 Note that the options @samp{-p} and @samp{-v} allow you to obtain other
6631 information about @code{make} or about the makefiles in use
6632 (@pxref{Options Summary, ,Summary of Options}).@refill
6633
6634 @node Avoiding Compilation, Overriding, Instead of Execution, Running
6635 @section Avoiding Recompilation of Some Files
6636 @cindex @code{-o}
6637 @cindex @code{--old-file}
6638 @cindex @code{--assume-old}
6639 @cindex files, assuming old
6640 @cindex files, avoiding recompilation of
6641 @cindex recompilation, avoiding
6642
6643 Sometimes you may have changed a source file but you do not want to
6644 recompile all the files that depend on it.  For example, suppose you add
6645 a macro or a declaration to a header file that many other files depend
6646 on.  Being conservative, @code{make} assumes that any change in the
6647 header file requires recompilation of all dependent files, but you know
6648 that they do not need to be recompiled and you would rather not waste
6649 the time waiting for them to compile.
6650
6651 If you anticipate the problem before changing the header file, you can
6652 use the @samp{-t} flag.  This flag tells @code{make} not to run the
6653 commands in the rules, but rather to mark the target up to date by
6654 changing its last-modification date.  You would follow this procedure:
6655
6656 @enumerate
6657 @item
6658 Use the command @samp{make} to recompile the source files that really
6659 need recompilation.
6660
6661 @item
6662 Make the changes in the header files.
6663
6664 @item
6665 Use the command @samp{make -t} to mark all the object files as
6666 up to date.  The next time you run @code{make}, the changes in the
6667 header files will not cause any recompilation.
6668 @end enumerate
6669
6670 If you have already changed the header file at a time when some files
6671 do need recompilation, it is too late to do this.  Instead, you can
6672 use the @w{@samp{-o @var{file}}} flag, which marks a specified file as
6673 ``old'' (@pxref{Options Summary, ,Summary of Options}).  This means
6674 that the file itself will not be remade, and nothing else will be
6675 remade on its account.  Follow this procedure:
6676
6677 @enumerate
6678 @item
6679 Recompile the source files that need compilation for reasons independent
6680 of the particular header file, with @samp{make -o @var{headerfile}}.
6681 If several header files are involved, use a separate @samp{-o} option
6682 for each header file.
6683
6684 @item
6685 Touch all the object files with @samp{make -t}.
6686 @end enumerate
6687
6688 @node Overriding, Testing, Avoiding Compilation, Running
6689 @section Overriding Variables
6690 @cindex overriding variables with arguments
6691 @cindex variables, overriding with arguments
6692 @cindex command line variables
6693 @cindex variables, command line
6694
6695 An argument that contains @samp{=} specifies the value of a variable:
6696 @samp{@var{v}=@var{x}} sets the value of the variable @var{v} to @var{x}.
6697 If you specify a value in this way, all ordinary assignments of the same
6698 variable in the makefile are ignored; we say they have been
6699 @dfn{overridden} by the command line argument.
6700
6701 The most common way to use this facility is to pass extra flags to
6702 compilers.  For example, in a properly written makefile, the variable
6703 @code{CFLAGS} is included in each command that runs the C compiler, so a
6704 file @file{foo.c} would be compiled something like this:
6705
6706 @example
6707 cc -c $(CFLAGS) foo.c
6708 @end example
6709
6710 Thus, whatever value you set for @code{CFLAGS} affects each compilation
6711 that occurs.  The makefile probably specifies the usual value for
6712 @code{CFLAGS}, like this:
6713
6714 @example
6715 CFLAGS=-g
6716 @end example
6717
6718 Each time you run @code{make}, you can override this value if you
6719 wish.  For example, if you say @samp{make CFLAGS='-g -O'}, each C
6720 compilation will be done with @samp{cc -c -g -O}.  (This illustrates
6721 how you can use quoting in the shell to enclose spaces and other
6722 special characters in the value of a variable when you override it.)
6723
6724 The variable @code{CFLAGS} is only one of many standard variables that
6725 exist just so that you can change them this way.  @xref{Implicit
6726 Variables, , Variables Used by Implicit Rules}, for a complete list.
6727
6728 You can also program the makefile to look at additional variables of your
6729 own, giving the user the ability to control other aspects of how the
6730 makefile works by changing the variables.
6731
6732 When you override a variable with a command argument, you can define either
6733 a recursively-expanded variable or a simply-expanded variable.  The
6734 examples shown above make a recursively-expanded variable; to make a
6735 simply-expanded variable, write @samp{:=} instead of @samp{=}.  But, unless
6736 you want to include a variable reference or function call in the
6737 @emph{value} that you specify, it makes no difference which kind of
6738 variable you create.
6739
6740 There is one way that the makefile can change a variable that you have
6741 overridden.  This is to use the @code{override} directive, which is a line
6742 that looks like this: @samp{override @var{variable} = @var{value}}
6743 (@pxref{Override Directive, ,The @code{override} Directive}).
6744
6745 @node Testing, Options Summary, Overriding, Running
6746 @section Testing the Compilation of a Program
6747 @cindex testing compilation
6748 @cindex compilation, testing
6749
6750 Normally, when an error happens in executing a shell command, @code{make}
6751 gives up immediately, returning a nonzero status.  No further commands are
6752 executed for any target.  The error implies that the goal cannot be
6753 correctly remade, and @code{make} reports this as soon as it knows.
6754
6755 When you are compiling a program that you have just changed, this is not
6756 what you want.  Instead, you would rather that @code{make} try compiling
6757 every file that can be tried, to show you as many compilation errors
6758 as possible.
6759
6760 @cindex @code{-k}
6761 @cindex @code{--keep-going}
6762 On these occasions, you should use the @samp{-k} or
6763 @samp{--keep-going} flag.  This tells @code{make} to continue to
6764 consider the other prerequisites of the pending targets, remaking them
6765 if necessary, before it gives up and returns nonzero status.  For
6766 example, after an error in compiling one object file, @samp{make -k}
6767 will continue compiling other object files even though it already
6768 knows that linking them will be impossible.  In addition to continuing
6769 after failed shell commands, @samp{make -k} will continue as much as
6770 possible after discovering that it does not know how to make a target
6771 or prerequisite file.  This will always cause an error message, but
6772 without @samp{-k}, it is a fatal error (@pxref{Options Summary,
6773 ,Summary of Options}).@refill
6774
6775 The usual behavior of @code{make} assumes that your purpose is to get the
6776 goals up to date; once @code{make} learns that this is impossible, it might
6777 as well report the failure immediately.  The @samp{-k} flag says that the
6778 real purpose is to test as much as possible of the changes made in the
6779 program, perhaps to find several independent problems so that you can
6780 correct them all before the next attempt to compile.  This is why Emacs'
6781 @kbd{M-x compile} command passes the @samp{-k} flag by default.
6782
6783 @node Options Summary,  , Testing, Running
6784 @section Summary of Options
6785 @cindex options
6786 @cindex flags
6787 @cindex switches
6788
6789 Here is a table of all the options @code{make} understands:
6790
6791 @table @samp
6792 @item -b
6793 @cindex @code{-b}
6794 @itemx -m
6795 @cindex @code{-m}
6796 These options are ignored for compatibility with other versions of @code{make}.
6797
6798 @item -C @var{dir}
6799 @cindex @code{-C}
6800 @itemx --directory=@var{dir}
6801 @cindex @code{--directory}
6802 Change to directory @var{dir} before reading the makefiles.  If multiple
6803 @samp{-C} options are specified, each is interpreted relative to the
6804 previous one: @samp{-C / -C etc} is equivalent to @samp{-C /etc}.
6805 This is typically used with recursive invocations of @code{make}
6806 (@pxref{Recursion, ,Recursive Use of @code{make}}).
6807
6808 @item -d
6809 @cindex @code{-d}
6810 @c Extra blank line here makes the table look better.
6811
6812 Print debugging information in addition to normal processing.  The
6813 debugging information says which files are being considered for
6814 remaking, which file-times are being compared and with what results,
6815 which files actually need to be remade, which implicit rules are
6816 considered and which are applied---everything interesting about how
6817 @code{make} decides what to do.  The @code{-d} option is equivalent to
6818 @samp{--debug=a} (see below).
6819
6820 @item --debug[=@var{options}]
6821 @cindex @code{--debug}
6822 @c Extra blank line here makes the table look better.
6823
6824 Print debugging information in addition to normal processing.  Various
6825 levels and types of output can be chosen.  With no arguments, print the
6826 ``basic'' level of debugging.  Possible arguments are below; only the
6827 first character is considered, and values must be comma- or
6828 space-separated.
6829
6830 @table @code
6831 @item a@var{ll}
6832 All types of debugging output is enabled.  This is equivalent to using
6833 @samp{-d}.
6834
6835 @item b@var{asic}
6836 Basic debugging prints each target that was found to be out-of-date, and
6837 whether the build was successful or not.
6838
6839 @item v@var{erbose}
6840 A level above @samp{basic}; includes messages about which makefiles were
6841 parsed, prerequisites that did not need to be rebuilt, etc.  This option
6842 also enables @samp{basic} messages.
6843
6844 @item i@var{mplicit}
6845 Prints messages describing the implicit rule searches for each target.
6846 This option also enables @samp{basic} messages.
6847
6848 @item j@var{obs}
6849 Prints messages giving details on the invocation of specific subcommands.
6850
6851 @item m@var{akefile}
6852 By default, the above messages are not enabled while trying to remake
6853 the makefiles.  This option enables messages while rebuilding makefiles,
6854 too.  Note that the @samp{all} option does enable this option.  This
6855 option also enables @samp{basic} messages.
6856 @end table
6857
6858 @item -e
6859 @cindex @code{-e}
6860 @itemx --environment-overrides
6861 @cindex @code{--environment-overrides}
6862 Give variables taken from the environment precedence
6863 over variables from makefiles.
6864 @xref{Environment, ,Variables from the Environment}.
6865
6866 @item -f @var{file}
6867 @cindex @code{-f}
6868 @itemx --file=@var{file}
6869 @cindex @code{--file}
6870 @itemx --makefile=@var{file}
6871 @cindex @code{--makefile}
6872 Read the file named @var{file} as a makefile.
6873 @xref{Makefiles, ,Writing Makefiles}.
6874
6875 @item -h
6876 @cindex @code{-h}
6877 @itemx --help
6878 @cindex @code{--help}
6879 @c Extra blank line here makes the table look better.
6880
6881 Remind you of the options that @code{make} understands and then exit.
6882
6883 @item -i
6884 @cindex @code{-i}
6885 @itemx --ignore-errors
6886 @cindex @code{--ignore-errors}
6887 Ignore all errors in commands executed to remake files.
6888 @xref{Errors, ,Errors in Commands}.
6889
6890 @item -I @var{dir}
6891 @cindex @code{-I}
6892 @itemx --include-dir=@var{dir}
6893 @cindex @code{--include-dir}
6894 Specifies a directory @var{dir} to search for included makefiles.
6895 @xref{Include, ,Including Other Makefiles}.  If several @samp{-I}
6896 options are used to specify several directories, the directories are
6897 searched in the order specified.
6898
6899 @item -j [@var{jobs}]
6900 @cindex @code{-j}
6901 @itemx --jobs[=@var{jobs}]
6902 @cindex @code{--jobs}
6903 Specifies the number of jobs (commands) to run simultaneously.  With no
6904 argument, @code{make} runs as many jobs simultaneously as possible.  If
6905 there is more than one @samp{-j} option, the last one is effective.
6906 @xref{Parallel, ,Parallel Execution},
6907 for more information on how commands are run.
6908 Note that this option is ignored on MS-DOS.
6909
6910 @item -k
6911 @cindex @code{-k}
6912 @itemx --keep-going
6913 @cindex @code{--keep-going}
6914 Continue as much as possible after an error.  While the target that
6915 failed, and those that depend on it, cannot be remade, the other
6916 prerequisites of these targets can be processed all the same.
6917 @xref{Testing, ,Testing the Compilation of a Program}.
6918
6919 @item -l [@var{load}]
6920 @cindex @code{-l}
6921 @itemx --load-average[=@var{load}]
6922 @cindex @code{--load-average}
6923 @itemx --max-load[=@var{load}]
6924 @cindex @code{--max-load}
6925 Specifies that no new jobs (commands) should be started if there are
6926 other jobs running and the load average is at least @var{load} (a
6927 floating-point number).  With no argument, removes a previous load
6928 limit.  @xref{Parallel, ,Parallel Execution}.
6929
6930 @item -n
6931 @cindex @code{-n}
6932 @itemx --just-print
6933 @cindex @code{--just-print}
6934 @itemx --dry-run
6935 @cindex @code{--dry-run}
6936 @itemx --recon
6937 @cindex @code{--recon}
6938 @c Extra blank line here makes the table look better.
6939
6940 Print the commands that would be executed, but do not execute them.
6941 @xref{Instead of Execution, ,Instead of Executing the Commands}.
6942
6943 @item -o @var{file}
6944 @cindex @code{-o}
6945 @itemx --old-file=@var{file}
6946 @cindex @code{--old-file}
6947 @itemx --assume-old=@var{file}
6948 @cindex @code{--assume-old}
6949 Do not remake the file @var{file} even if it is older than its
6950 prerequisites, and do not remake anything on account of changes in
6951 @var{file}.  Essentially the file is treated as very old and its rules
6952 are ignored.  @xref{Avoiding Compilation, ,Avoiding Recompilation of
6953 Some Files}.@refill
6954
6955 @item -p
6956 @cindex @code{-p}
6957 @itemx --print-data-base
6958 @cindex @code{--print-data-base}
6959 Print the data base (rules and variable values) that results from
6960 reading the makefiles; then execute as usual or as otherwise specified.
6961 This also prints the version information given by the @samp{-v} switch
6962 (see below).  To print the data base without trying to remake any files,
6963 use @w{@samp{make -qp}}.  To print the data base of predefined rules and
6964 variables, use @w{@samp{make -p -f /dev/null}}.  The data base output
6965 contains filename and linenumber information for command and variable
6966 definitions, so it can be a useful debugging tool in complex environments.
6967
6968 @item -q
6969 @cindex @code{-q}
6970 @itemx --question
6971 @cindex @code{--question}
6972 ``Question mode''.  Do not run any commands, or print anything; just
6973 return an exit status that is zero if the specified targets are already
6974 up to date, one if any remaking is required, or two if an error is
6975 encountered.  @xref{Instead of Execution, ,Instead of Executing the
6976 Commands}.@refill
6977
6978 @item -r
6979 @cindex @code{-r}
6980 @itemx --no-builtin-rules
6981 @cindex @code{--no-builtin-rules}
6982 Eliminate use of the built-in implicit rules (@pxref{Implicit Rules,
6983 ,Using Implicit Rules}).  You can still define your own by writing
6984 pattern rules (@pxref{Pattern Rules, ,Defining and Redefining Pattern
6985 Rules}).  The @samp{-r} option also clears out the default list of
6986 suffixes for suffix rules (@pxref{Suffix Rules, ,Old-Fashioned Suffix
6987 Rules}).  But you can still define your own suffixes with a rule for
6988 @code{.SUFFIXES}, and then define your own suffix rules.  Note that only
6989 @emph{rules} are affected by the @code{-r} option; default variables
6990 remain in effect (@pxref{Implicit Variables, ,Variables Used by Implicit
6991 Rules}); see the @samp{-R} option below.
6992
6993 @item -R
6994 @cindex @code{-R}
6995 @itemx --no-builtin-variables
6996 @cindex @code{--no-builtin-variables}
6997 Eliminate use of the built-in rule-specific variables (@pxref{Implicit
6998 Variables, ,Variables Used by Implicit Rules}).  You can still define
6999 your own, of course.  The @samp{-R} option also automatically enables
7000 the @samp{-r} option (see above), since it doesn't make sense to have
7001 implicit rules without any definitions for the variables that they use.
7002
7003 @item -s
7004 @cindex @code{-s}
7005 @itemx --silent
7006 @cindex @code{--silent}
7007 @itemx --quiet
7008 @cindex @code{--quiet}
7009 @c Extra blank line here makes the table look better.
7010
7011 Silent operation; do not print the commands as they are executed.
7012 @xref{Echoing, ,Command Echoing}.
7013
7014 @item -S
7015 @cindex @code{-S}
7016 @itemx --no-keep-going
7017 @cindex @code{--no-keep-going}
7018 @itemx --stop
7019 @cindex @code{--stop}
7020 @c Extra blank line here makes the table look better.
7021
7022 Cancel the effect of the @samp{-k} option.  This is never necessary
7023 except in a recursive @code{make} where @samp{-k} might be inherited
7024 from the top-level @code{make} via @code{MAKEFLAGS}
7025 (@pxref{Recursion, ,Recursive Use of @code{make}})
7026 or if you set @samp{-k} in @code{MAKEFLAGS} in your environment.@refill
7027
7028 @item -t
7029 @cindex @code{-t}
7030 @itemx --touch
7031 @cindex @code{--touch}
7032 @c Extra blank line here makes the table look better.
7033
7034 Touch files (mark them up to date without really changing them)
7035 instead of running their commands.  This is used to pretend that the
7036 commands were done, in order to fool future invocations of
7037 @code{make}.  @xref{Instead of Execution, ,Instead of Executing the Commands}.
7038
7039 @item -v
7040 @cindex @code{-v}
7041 @itemx --version
7042 @cindex @code{--version}
7043 Print the version of the @code{make} program plus a copyright, a list
7044 of authors, and a notice that there is no warranty; then exit.
7045
7046 @item -w
7047 @cindex @code{-w}
7048 @itemx --print-directory
7049 @cindex @code{--print-directory}
7050 Print a message containing the working directory both before and after
7051 executing the makefile.  This may be useful for tracking down errors
7052 from complicated nests of recursive @code{make} commands.
7053 @xref{Recursion, ,Recursive Use of @code{make}}.  (In practice, you
7054 rarely need to specify this option since @samp{make} does it for you;
7055 see @ref{-w Option, ,The @samp{--print-directory} Option}.)
7056
7057 @itemx --no-print-directory
7058 @cindex @code{--no-print-directory}
7059 Disable printing of the working directory under @code{-w}.
7060 This option is useful when @code{-w} is turned on automatically,
7061 but you do not want to see the extra messages.
7062 @xref{-w Option, ,The @samp{--print-directory} Option}.
7063
7064 @item -W @var{file}
7065 @cindex @code{-W}
7066 @itemx --what-if=@var{file}
7067 @cindex @code{--what-if}
7068 @itemx --new-file=@var{file}
7069 @cindex @code{--new-file}
7070 @itemx --assume-new=@var{file}
7071 @cindex @code{--assume-new}
7072 Pretend that the target @var{file} has just been modified.  When used
7073 with the @samp{-n} flag, this shows you what would happen if you were
7074 to modify that file.  Without @samp{-n}, it is almost the same as
7075 running a @code{touch} command on the given file before running
7076 @code{make}, except that the modification time is changed only in the
7077 imagination of @code{make}.
7078 @xref{Instead of Execution, ,Instead of Executing the Commands}.
7079
7080 @item --warn-undefined-variables
7081 @cindex @code{--warn-undefined-variables}
7082 @cindex variables, warning for undefined
7083 @cindex undefined variables, warning message
7084 Issue a warning message whenever @code{make} sees a reference to an
7085 undefined variable.  This can be helpful when you are trying to debug
7086 makefiles which use variables in complex ways.
7087 @end table
7088
7089 @node Implicit Rules, Archives, Running, Top
7090 @chapter Using Implicit Rules
7091 @cindex implicit rule
7092 @cindex rule, implicit
7093
7094 Certain standard ways of remaking target files are used very often.  For
7095 example, one customary way to make an object file is from a C source file
7096 using the C compiler, @code{cc}.
7097
7098 @dfn{Implicit rules} tell @code{make} how to use customary techniques so
7099 that you do not have to specify them in detail when you want to use
7100 them.  For example, there is an implicit rule for C compilation.  File
7101 names determine which implicit rules are run.  For example, C
7102 compilation typically takes a @file{.c} file and makes a @file{.o} file.
7103 So @code{make} applies the implicit rule for C compilation when it sees
7104 this combination of file name endings.@refill
7105
7106 A chain of implicit rules can apply in sequence; for example, @code{make}
7107 will remake a @file{.o} file from a @file{.y} file by way of a @file{.c} file.
7108 @iftex
7109 @xref{Chained Rules, ,Chains of Implicit Rules}.
7110 @end iftex
7111
7112 The built-in implicit rules use several variables in their commands so
7113 that, by changing the values of the variables, you can change the way the
7114 implicit rule works.  For example, the variable @code{CFLAGS} controls the
7115 flags given to the C compiler by the implicit rule for C compilation.
7116 @iftex
7117 @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
7118 @end iftex
7119
7120 You can define your own implicit rules by writing @dfn{pattern rules}.
7121 @iftex
7122 @xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.
7123 @end iftex
7124
7125 @dfn{Suffix rules} are a more limited way to define implicit rules.
7126 Pattern rules are more general and clearer, but suffix rules are
7127 retained for compatibility.
7128 @iftex
7129 @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
7130 @end iftex
7131
7132 @menu
7133 * Using Implicit::              How to use an existing implicit rule
7134                                   to get the commands for updating a file.
7135 * Catalogue of Rules::          A list of built-in implicit rules.
7136 * Implicit Variables::          How to change what predefined rules do.
7137 * Chained Rules::               How to use a chain of implicit rules.
7138 * Pattern Rules::               How to define new implicit rules.
7139 * Last Resort::                 How to defining commands for rules
7140                                   which cannot find any.
7141 * Suffix Rules::                The old-fashioned style of implicit rule.
7142 * Implicit Rule Search::        The precise algorithm for applying
7143                                   implicit rules.
7144 @end menu
7145
7146 @node Using Implicit, Catalogue of Rules,  , Implicit Rules
7147 @section Using Implicit Rules
7148 @cindex implicit rule, how to use
7149 @cindex rule, implicit, how to use
7150
7151 To allow @code{make} to find a customary method for updating a target file,
7152 all you have to do is refrain from specifying commands yourself.  Either
7153 write a rule with no command lines, or don't write a rule at all.  Then
7154 @code{make} will figure out which implicit rule to use based on which
7155 kind of source file exists or can be made.
7156
7157 For example, suppose the makefile looks like this:
7158
7159 @example
7160 foo : foo.o bar.o
7161         cc -o foo foo.o bar.o $(CFLAGS) $(LDFLAGS)
7162 @end example
7163
7164 @noindent
7165 Because you mention @file{foo.o} but do not give a rule for it, @code{make}
7166 will automatically look for an implicit rule that tells how to update it.
7167 This happens whether or not the file @file{foo.o} currently exists.
7168
7169 If an implicit rule is found, it can supply both commands and one or
7170 more prerequisites (the source files).  You would want to write a rule
7171 for @file{foo.o} with no command lines if you need to specify additional
7172 prerequisites, such as header files, that the implicit rule cannot
7173 supply.
7174
7175 Each implicit rule has a target pattern and prerequisite patterns.  There may
7176 be many implicit rules with the same target pattern.  For example, numerous
7177 rules make @samp{.o} files: one, from a @samp{.c} file with the C compiler;
7178 another, from a @samp{.p} file with the Pascal compiler; and so on.  The rule
7179 that actually applies is the one whose prerequisites exist or can be made.
7180 So, if you have a file @file{foo.c}, @code{make} will run the C compiler;
7181 otherwise, if you have a file @file{foo.p}, @code{make} will run the Pascal
7182 compiler; and so on.
7183
7184 Of course, when you write the makefile, you know which implicit rule you
7185 want @code{make} to use, and you know it will choose that one because you
7186 know which possible prerequisite files are supposed to exist.
7187 @xref{Catalogue of Rules, ,Catalogue of Implicit Rules},
7188 for a catalogue of all the predefined implicit rules.
7189
7190 Above, we said an implicit rule applies if the required prerequisites ``exist
7191 or can be made''.  A file ``can be made'' if it is mentioned explicitly in
7192 the makefile as a target or a prerequisite, or if an implicit rule can be
7193 recursively found for how to make it.  When an implicit prerequisite is the
7194 result of another implicit rule, we say that @dfn{chaining} is occurring.
7195 @xref{Chained Rules, ,Chains of Implicit Rules}.
7196
7197 In general, @code{make} searches for an implicit rule for each target, and
7198 for each double-colon rule, that has no commands.  A file that is mentioned
7199 only as a prerequisite is considered a target whose rule specifies nothing,
7200 so implicit rule search happens for it.  @xref{Implicit Rule Search, ,Implicit Rule Search Algorithm}, for the
7201 details of how the search is done.
7202
7203 Note that explicit prerequisites do not influence implicit rule search.
7204 For example, consider this explicit rule:
7205
7206 @example
7207 foo.o: foo.p
7208 @end example
7209
7210 @noindent
7211 The prerequisite on @file{foo.p} does not necessarily mean that
7212 @code{make} will remake @file{foo.o} according to the implicit rule to
7213 make an object file, a @file{.o} file, from a Pascal source file, a
7214 @file{.p} file.  For example, if @file{foo.c} also exists, the implicit
7215 rule to make an object file from a C source file is used instead,
7216 because it appears before the Pascal rule in the list of predefined
7217 implicit rules (@pxref{Catalogue of Rules, , Catalogue of Implicit
7218 Rules}).
7219
7220 If you do not want an implicit rule to be used for a target that has no
7221 commands, you can give that target empty commands by writing a semicolon
7222 (@pxref{Empty Commands, ,Defining Empty Commands}).
7223
7224 @node Catalogue of Rules, Implicit Variables, Using Implicit, Implicit Rules
7225 @section Catalogue of Implicit Rules
7226 @cindex implicit rule, predefined
7227 @cindex rule, implicit, predefined
7228
7229 Here is a catalogue of predefined implicit rules which are always
7230 available unless the makefile explicitly overrides or cancels them.
7231 @xref{Canceling Rules, ,Canceling Implicit Rules}, for information on
7232 canceling or overriding an implicit rule.  The @samp{-r} or
7233 @samp{--no-builtin-rules} option cancels all predefined rules.
7234
7235 Not all of these rules will always be defined, even when the @samp{-r}
7236 option is not given.  Many of the predefined implicit rules are
7237 implemented in @code{make} as suffix rules, so which ones will be
7238 defined depends on the @dfn{suffix list} (the list of prerequisites of
7239 the special target @code{.SUFFIXES}).  The default suffix list is:
7240 @code{.out}, @code{.a}, @code{.ln}, @code{.o}, @code{.c}, @code{.cc},
7241 @code{.C}, @code{.p}, @code{.f}, @code{.F}, @code{.r}, @code{.y},
7242 @code{.l}, @code{.s}, @code{.S}, @code{.mod}, @code{.sym}, @code{.def},
7243 @code{.h}, @code{.info}, @code{.dvi}, @code{.tex}, @code{.texinfo},
7244 @code{.texi}, @code{.txinfo}, @code{.w}, @code{.ch} @code{.web},
7245 @code{.sh}, @code{.elc}, @code{.el}.  All of the implicit rules
7246 described below whose prerequisites have one of these suffixes are
7247 actually suffix rules.  If you modify the suffix list, the only
7248 predefined suffix rules in effect will be those named by one or two of
7249 the suffixes that are on the list you specify; rules whose suffixes fail
7250 to be on the list are disabled.  @xref{Suffix Rules, ,Old-Fashioned
7251 Suffix Rules}, for full details on suffix rules.
7252
7253 @table @asis
7254 @item Compiling C programs
7255 @cindex C, rule to compile
7256 @pindex cc
7257 @pindex gcc
7258 @pindex .o
7259 @pindex .c
7260 @file{@var{n}.o} is made automatically from @file{@var{n}.c} with
7261 a command of the form @samp{$(CC) -c $(CPPFLAGS) $(CFLAGS)}.@refill
7262
7263 @item Compiling C++ programs
7264 @cindex C++, rule to compile
7265 @pindex g++
7266 @pindex .C
7267 @pindex .cc
7268 @file{@var{n}.o} is made automatically from @file{@var{n}.cc} or
7269 @file{@var{n}.C} with a command of the form @samp{$(CXX) -c $(CPPFLAGS)
7270 $(CXXFLAGS)}.  We encourage you to use the suffix @samp{.cc} for C++
7271 source files instead of @samp{.C}.@refill
7272
7273 @item Compiling Pascal programs
7274 @cindex Pascal, rule to compile
7275 @pindex pc
7276 @pindex .p
7277 @file{@var{n}.o} is made automatically from @file{@var{n}.p}
7278 with the command @samp{$(PC) -c $(PFLAGS)}.@refill
7279
7280 @item Compiling Fortran and Ratfor programs
7281 @cindex Fortran, rule to compile
7282 @cindex Ratfor, rule to compile
7283 @pindex f77
7284 @pindex .f
7285 @pindex .r
7286 @pindex .F
7287 @file{@var{n}.o} is made automatically from @file{@var{n}.r},
7288 @file{@var{n}.F} or @file{@var{n}.f} by running the
7289 Fortran compiler.  The precise command used is as follows:@refill
7290
7291 @table @samp
7292 @item .f
7293 @samp{$(FC) -c $(FFLAGS)}.
7294 @item .F
7295 @samp{$(FC) -c $(FFLAGS) $(CPPFLAGS)}.
7296 @item .r
7297 @samp{$(FC) -c $(FFLAGS) $(RFLAGS)}.
7298 @end table
7299
7300 @item Preprocessing Fortran and Ratfor programs
7301 @file{@var{n}.f} is made automatically from @file{@var{n}.r} or
7302 @file{@var{n}.F}.  This rule runs just the preprocessor to convert a
7303 Ratfor or preprocessable Fortran program into a strict Fortran
7304 program.  The precise command used is as follows:@refill
7305
7306 @table @samp
7307 @item .F
7308 @samp{$(FC) -F $(CPPFLAGS) $(FFLAGS)}.
7309 @item .r
7310 @samp{$(FC) -F $(FFLAGS) $(RFLAGS)}.
7311 @end table
7312
7313 @item Compiling Modula-2 programs
7314 @cindex Modula-2, rule to compile
7315 @pindex m2c
7316 @pindex .sym
7317 @pindex .def
7318 @pindex .mod
7319 @file{@var{n}.sym} is made from @file{@var{n}.def} with a command
7320 of the form @samp{$(M2C) $(M2FLAGS) $(DEFFLAGS)}.  @file{@var{n}.o}
7321 is made from @file{@var{n}.mod}; the form is:
7322 @w{@samp{$(M2C) $(M2FLAGS) $(MODFLAGS)}}.@refill
7323
7324 @need 1200
7325 @item Assembling and preprocessing assembler programs
7326 @cindex assembly, rule to compile
7327 @pindex as
7328 @pindex .s
7329 @file{@var{n}.o} is made automatically from @file{@var{n}.s} by
7330 running the assembler, @code{as}.  The precise command is
7331 @samp{$(AS) $(ASFLAGS)}.@refill
7332
7333 @pindex .S
7334 @file{@var{n}.s} is made automatically from @file{@var{n}.S} by
7335 running the C preprocessor, @code{cpp}.  The precise command is
7336 @w{@samp{$(CPP) $(CPPFLAGS)}}.
7337
7338 @item Linking a single object file
7339 @cindex linking, predefined rule for
7340 @pindex ld
7341 @pindex .o
7342 @file{@var{n}} is made automatically from @file{@var{n}.o} by running
7343 the linker (usually called @code{ld}) via the C compiler.  The precise
7344 command used is @w{@samp{$(CC) $(LDFLAGS) @var{n}.o $(LOADLIBES) $(LDLIBS)}}.
7345
7346 This rule does the right thing for a simple program with only one
7347 source file.  It will also do the right thing if there are multiple
7348 object files (presumably coming from various other source files), one
7349 of which has a name matching that of the executable file.  Thus,
7350
7351 @example
7352 x: y.o z.o
7353 @end example
7354
7355 @noindent
7356 when @file{x.c}, @file{y.c} and @file{z.c} all exist will execute:
7357
7358 @example
7359 @group
7360 cc -c x.c -o x.o
7361 cc -c y.c -o y.o
7362 cc -c z.c -o z.o
7363 cc x.o y.o z.o -o x
7364 rm -f x.o
7365 rm -f y.o
7366 rm -f z.o
7367 @end group
7368 @end example
7369
7370 @noindent
7371 In more complicated cases, such as when there is no object file whose
7372 name derives from the executable file name, you must write an explicit
7373 command for linking.
7374
7375 Each kind of file automatically made into @samp{.o} object files will
7376 be automatically linked by using the compiler (@samp{$(CC)},
7377 @samp{$(FC)} or @samp{$(PC)}; the C compiler @samp{$(CC)} is used to
7378 assemble @samp{.s} files) without the @samp{-c} option.  This could be
7379 done by using the @samp{.o} object files as intermediates, but it is
7380 faster to do the compiling and linking in one step, so that's how it's
7381 done.@refill
7382
7383 @item Yacc for C programs
7384 @pindex yacc
7385 @cindex Yacc, rule to run
7386 @pindex .y
7387 @file{@var{n}.c} is made automatically from @file{@var{n}.y} by
7388 running Yacc with the command @samp{$(YACC) $(YFLAGS)}.
7389
7390 @item Lex for C programs
7391 @pindex lex
7392 @cindex Lex, rule to run
7393 @pindex .l
7394 @file{@var{n}.c} is made automatically from @file{@var{n}.l} by
7395 by running Lex.  The actual command is @samp{$(LEX) $(LFLAGS)}.
7396
7397 @item Lex for Ratfor programs
7398 @file{@var{n}.r} is made automatically from @file{@var{n}.l} by
7399 by running Lex.  The actual command is @samp{$(LEX) $(LFLAGS)}.
7400
7401 The convention of using the same suffix @samp{.l} for all Lex files
7402 regardless of whether they produce C code or Ratfor code makes it
7403 impossible for @code{make} to determine automatically which of the two
7404 languages you are using in any particular case.  If @code{make} is
7405 called upon to remake an object file from a @samp{.l} file, it must
7406 guess which compiler to use.  It will guess the C compiler, because
7407 that is more common.  If you are using Ratfor, make sure @code{make}
7408 knows this by mentioning @file{@var{n}.r} in the makefile.  Or, if you
7409 are using Ratfor exclusively, with no C files, remove @samp{.c} from
7410 the list of implicit rule suffixes with:@refill
7411
7412 @example
7413 @group
7414 .SUFFIXES:
7415 .SUFFIXES: .o .r .f .l @dots{}
7416 @end group
7417 @end example
7418
7419 @item Making Lint Libraries from C, Yacc, or Lex programs
7420 @pindex lint
7421 @cindex @code{lint}, rule to run
7422 @pindex .ln
7423 @file{@var{n}.ln} is made from @file{@var{n}.c} by running @code{lint}.
7424 The precise command is @w{@samp{$(LINT) $(LINTFLAGS) $(CPPFLAGS) -i}}.
7425 The same command is used on the C code produced from
7426 @file{@var{n}.y} or @file{@var{n}.l}.@refill
7427
7428 @item @TeX{} and Web
7429 @cindex @TeX{}, rule to run
7430 @cindex Web, rule to run
7431 @pindex tex
7432 @pindex cweave
7433 @pindex weave
7434 @pindex tangle
7435 @pindex ctangle
7436 @pindex .dvi
7437 @pindex .tex
7438 @pindex .web
7439 @pindex .w
7440 @pindex .ch
7441 @file{@var{n}.dvi} is made from @file{@var{n}.tex} with the command
7442 @samp{$(TEX)}.  @file{@var{n}.tex} is made from @file{@var{n}.web} with
7443 @samp{$(WEAVE)}, or from @file{@var{n}.w} (and from @file{@var{n}.ch} if
7444 it exists or can be made) with @samp{$(CWEAVE)}.  @file{@var{n}.p} is
7445 made from @file{@var{n}.web} with @samp{$(TANGLE)} and @file{@var{n}.c}
7446 is made from @file{@var{n}.w} (and from @file{@var{n}.ch} if it exists
7447 or can be made) with @samp{$(CTANGLE)}.@refill
7448
7449 @item Texinfo and Info
7450 @cindex Texinfo, rule to format
7451 @cindex Info, rule to format
7452 @pindex texi2dvi
7453 @pindex makeinfo
7454 @pindex .texinfo
7455 @pindex .info
7456 @pindex .texi
7457 @pindex .txinfo
7458 @file{@var{n}.dvi} is made from @file{@var{n}.texinfo},
7459 @file{@var{n}.texi}, or @file{@var{n}.txinfo}, with the command
7460 @w{@samp{$(TEXI2DVI) $(TEXI2DVI_FLAGS)}}.  @file{@var{n}.info} is made from
7461 @file{@var{n}.texinfo}, @file{@var{n}.texi}, or @file{@var{n}.txinfo}, with
7462 the command @w{@samp{$(MAKEINFO) $(MAKEINFO_FLAGS)}}.
7463
7464 @item RCS
7465 @cindex RCS, rule to extract from
7466 @pindex co
7467 @pindex ,v @r{(RCS file extension)}
7468 Any file @file{@var{n}} is extracted if necessary from an RCS file
7469 named either @file{@var{n},v} or @file{RCS/@var{n},v}.  The precise
7470 command used is @w{@samp{$(CO) $(COFLAGS)}}.  @file{@var{n}} will not be
7471 extracted from RCS if it already exists, even if the RCS file is
7472 newer.  The rules for RCS are terminal
7473 (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}),
7474 so RCS files cannot be generated from another source; they must
7475 actually exist.@refill
7476
7477 @item SCCS
7478 @cindex SCCS, rule to extract from
7479 @pindex get
7480 @pindex s. @r{(SCCS file prefix)}
7481 Any file @file{@var{n}} is extracted if necessary from an SCCS file
7482 named either @file{s.@var{n}} or @file{SCCS/s.@var{n}}.  The precise
7483 command used is @w{@samp{$(GET) $(GFLAGS)}}.  The rules for SCCS are
7484 terminal (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}),
7485 so SCCS files cannot be generated from another source; they must
7486 actually exist.@refill
7487
7488 @pindex .sh
7489 For the benefit of SCCS, a file @file{@var{n}} is copied from
7490 @file{@var{n}.sh} and made executable (by everyone).  This is for
7491 shell scripts that are checked into SCCS.  Since RCS preserves the
7492 execution permission of a file, you do not need to use this feature
7493 with RCS.@refill
7494
7495 We recommend that you avoid using of SCCS.  RCS is widely held to be
7496 superior, and is also free.  By choosing free software in place of
7497 comparable (or inferior) proprietary software, you support the free
7498 software movement.
7499 @end table
7500
7501 Usually, you want to change only the variables listed in the table
7502 above, which are documented in the following section.
7503
7504 However, the commands in built-in implicit rules actually use
7505 variables such as @code{COMPILE.c}, @code{LINK.p}, and
7506 @code{PREPROCESS.S}, whose values contain the commands listed above.
7507
7508 @code{make} follows the convention that the rule to compile a
7509 @file{.@var{x}} source file uses the variable @code{COMPILE.@var{x}}.
7510 Similarly, the rule to produce an executable from a @file{.@var{x}}
7511 file uses @code{LINK.@var{x}}; and the rule to preprocess a
7512 @file{.@var{x}} file uses @code{PREPROCESS.@var{x}}.
7513
7514 @vindex OUTPUT_OPTION
7515 Every rule that produces an object file uses the variable
7516 @code{OUTPUT_OPTION}.  @code{make} defines this variable either to
7517 contain @samp{-o $@@}, or to be empty, depending on a compile-time
7518 option.  You need the @samp{-o} option to ensure that the output goes
7519 into the right file when the source file is in a different directory,
7520 as when using @code{VPATH} (@pxref{Directory Search}).  However,
7521 compilers on some systems do not accept a @samp{-o} switch for object
7522 files.  If you use such a system, and use @code{VPATH}, some
7523 compilations will put their output in the wrong place.
7524 A possible workaround for this problem is to give @code{OUTPUT_OPTION}
7525 the value @w{@samp{; mv $*.o $@@}}.
7526
7527 @node Implicit Variables, Chained Rules, Catalogue of Rules, Implicit Rules
7528 @section Variables Used by Implicit Rules
7529 @cindex flags for compilers
7530
7531 The commands in built-in implicit rules make liberal use of certain
7532 predefined variables.  You can alter these variables in the makefile,
7533 with arguments to @code{make}, or in the environment to alter how the
7534 implicit rules work without redefining the rules themselves.  You can
7535 cancel all variables used by implicit rules with the @samp{-R} or
7536 @samp{--no-builtin-variables} option.
7537
7538 For example, the command used to compile a C source file actually says
7539 @samp{$(CC) -c $(CFLAGS) $(CPPFLAGS)}.  The default values of the variables
7540 used are @samp{cc} and nothing, resulting in the command @samp{cc -c}.  By
7541 redefining @samp{CC} to @samp{ncc}, you could cause @samp{ncc} to be
7542 used for all C compilations performed by the implicit rule.  By redefining
7543 @samp{CFLAGS} to be @samp{-g}, you could pass the @samp{-g} option to
7544 each compilation.  @emph{All} implicit rules that do C compilation use
7545 @samp{$(CC)} to get the program name for the compiler and @emph{all}
7546 include @samp{$(CFLAGS)} among the arguments given to the compiler.@refill
7547
7548 The variables used in implicit rules fall into two classes: those that are
7549 names of programs (like @code{CC}) and those that contain arguments for the
7550 programs (like @code{CFLAGS}).  (The ``name of a program'' may also contain
7551 some command arguments, but it must start with an actual executable program
7552 name.)  If a variable value contains more than one argument, separate them
7553 with spaces.
7554
7555 Here is a table of variables used as names of programs in built-in rules:
7556
7557 @table @code
7558 @item AR
7559 @vindex AR
7560 Archive-maintaining program; default @samp{ar}.
7561 @pindex ar
7562
7563 @item AS
7564 @vindex AS
7565 Program for doing assembly; default @samp{as}.
7566 @pindex as
7567
7568 @item CC
7569 @vindex CC
7570 Program for compiling C programs; default @samp{cc}.
7571 @pindex cc
7572
7573 @item CXX
7574 @vindex CXX
7575 Program for compiling C++ programs; default @samp{g++}.
7576 @pindex g++
7577
7578 @item CO
7579 @vindex CO
7580 Program for extracting a file from RCS; default @samp{co}.
7581 @pindex co
7582
7583 @item CPP
7584 @vindex CPP
7585 Program for running the C preprocessor, with results to standard output;
7586 default @samp{$(CC) -E}.
7587
7588 @item FC
7589 @vindex FC
7590 Program for compiling or preprocessing Fortran and Ratfor programs;
7591 default @samp{f77}.
7592 @pindex f77
7593
7594 @item GET
7595 @vindex GET
7596 Program for extracting a file from SCCS; default @samp{get}.
7597 @pindex get
7598
7599 @item LEX
7600 @vindex LEX
7601 Program to use to turn Lex grammars into C programs or Ratfor programs;
7602 default @samp{lex}.
7603 @pindex lex
7604
7605 @item PC
7606 @vindex PC
7607 Program for compiling Pascal programs; default @samp{pc}.
7608 @pindex pc
7609
7610 @item YACC
7611 @vindex YACC
7612 Program to use to turn Yacc grammars into C programs; default @samp{yacc}.
7613 @pindex yacc
7614
7615 @item YACCR
7616 @vindex YACCR
7617 Program to use to turn Yacc grammars into Ratfor
7618 programs; default @samp{yacc -r}.
7619
7620 @item MAKEINFO
7621 @vindex MAKEINFO
7622 Program to convert a Texinfo source file into an Info file; default
7623 @samp{makeinfo}.
7624 @pindex makeinfo
7625
7626 @item TEX
7627 @vindex TEX
7628 Program to make @TeX{} @sc{dvi} files from @TeX{} source;
7629 default @samp{tex}.
7630 @pindex tex
7631
7632 @item TEXI2DVI
7633 @vindex TEXI2DVI
7634 Program to make @TeX{} @sc{dvi} files from Texinfo source;
7635 default @samp{texi2dvi}.
7636 @pindex texi2dvi
7637
7638 @item WEAVE
7639 @vindex WEAVE
7640 Program to translate Web into @TeX{}; default @samp{weave}.
7641 @pindex weave
7642
7643 @item CWEAVE
7644 @vindex CWEAVE
7645 Program to translate C Web into @TeX{}; default @samp{cweave}.
7646 @pindex cweave
7647
7648 @item TANGLE
7649 @vindex TANGLE
7650 Program to translate Web into Pascal; default @samp{tangle}.
7651 @pindex tangle
7652
7653 @item CTANGLE
7654 @vindex CTANGLE
7655 Program to translate C Web into C; default @samp{ctangle}.
7656 @pindex ctangle
7657
7658 @item RM
7659 @vindex RM
7660 Command to remove a file; default @samp{rm -f}.
7661 @pindex rm
7662 @end table
7663
7664 Here is a table of variables whose values are additional arguments for the
7665 programs above.  The default values for all of these is the empty
7666 string, unless otherwise noted.
7667
7668 @table @code
7669 @item ARFLAGS
7670 @vindex ARFLAGS
7671 Flags to give the archive-maintaining program; default @samp{rv}.
7672
7673 @item ASFLAGS
7674 @vindex ASFLAGS
7675 Extra flags to give to the assembler (when explicitly
7676 invoked on a @samp{.s} or @samp{.S} file).
7677
7678 @item CFLAGS
7679 @vindex CFLAGS
7680 Extra flags to give to the C compiler.
7681
7682 @item CXXFLAGS
7683 @vindex CXXFLAGS
7684 Extra flags to give to the C++ compiler.
7685
7686 @item COFLAGS
7687 @vindex COFLAGS
7688 Extra flags to give to the RCS @code{co} program.
7689
7690 @item CPPFLAGS
7691 @vindex CPPFLAGS
7692 Extra flags to give to the C preprocessor and programs
7693 that use it (the C and Fortran compilers).
7694
7695 @item FFLAGS
7696 @vindex FFLAGS
7697 Extra flags to give to the Fortran compiler.
7698
7699 @item GFLAGS
7700 @vindex GFLAGS
7701 Extra flags to give to the SCCS @code{get} program.
7702
7703 @item LDFLAGS
7704 @vindex LDFLAGS
7705 Extra flags to give to compilers when they are
7706 supposed to invoke the linker, @samp{ld}.
7707
7708 @item LFLAGS
7709 @vindex LFLAGS
7710 Extra flags to give to Lex.
7711
7712 @item PFLAGS
7713 @vindex PFLAGS
7714 Extra flags to give to the Pascal compiler.
7715
7716 @item RFLAGS
7717 @vindex RFLAGS
7718 Extra flags to give to the Fortran compiler for Ratfor programs.
7719
7720 @item YFLAGS
7721 @vindex YFLAGS
7722 Extra flags to give to Yacc.
7723 @end table
7724
7725 @node Chained Rules, Pattern Rules, Implicit Variables, Implicit Rules
7726 @section Chains of Implicit Rules
7727
7728 @cindex chains of rules
7729 @cindex rule, implicit, chains of
7730 Sometimes a file can be made by a sequence of implicit rules.  For example,
7731 a file @file{@var{n}.o} could be made from @file{@var{n}.y} by running
7732 first Yacc and then @code{cc}.  Such a sequence is called a @dfn{chain}.
7733
7734 If the file @file{@var{n}.c} exists, or is mentioned in the makefile, no
7735 special searching is required: @code{make} finds that the object file can
7736 be made by C compilation from @file{@var{n}.c}; later on, when considering
7737 how to make @file{@var{n}.c}, the rule for running Yacc is
7738 used.  Ultimately both @file{@var{n}.c} and @file{@var{n}.o} are
7739 updated.@refill
7740
7741 @cindex intermediate files
7742 @cindex files, intermediate
7743 However, even if @file{@var{n}.c} does not exist and is not mentioned,
7744 @code{make} knows how to envision it as the missing link between
7745 @file{@var{n}.o} and @file{@var{n}.y}!  In this case, @file{@var{n}.c} is
7746 called an @dfn{intermediate file}.  Once @code{make} has decided to use the
7747 intermediate file, it is entered in the data base as if it had been
7748 mentioned in the makefile, along with the implicit rule that says how to
7749 create it.@refill
7750
7751 Intermediate files are remade using their rules just like all other
7752 files.  But intermediate files are treated differently in two ways.
7753
7754 The first difference is what happens if the intermediate file does not
7755 exist.  If an ordinary file @var{b} does not exist, and @code{make}
7756 considers a target that depends on @var{b}, it invariably creates
7757 @var{b} and then updates the target from @var{b}.  But if @var{b} is an
7758 intermediate file, then @code{make} can leave well enough alone.  It
7759 won't bother updating @var{b}, or the ultimate target, unless some
7760 prerequisite of @var{b} is newer than that target or there is some other
7761 reason to update that target.
7762
7763 The second difference is that if @code{make} @emph{does} create @var{b}
7764 in order to update something else, it deletes @var{b} later on after it
7765 is no longer needed.  Therefore, an intermediate file which did not
7766 exist before @code{make} also does not exist after @code{make}.
7767 @code{make} reports the deletion to you by printing a @samp{rm -f}
7768 command showing which file it is deleting.
7769
7770 Ordinarily, a file cannot be intermediate if it is mentioned in the
7771 makefile as a target or prerequisite.  However, you can explicitly mark a
7772 file as intermediate by listing it as a prerequisite of the special target
7773 @code{.INTERMEDIATE}.  This takes effect even if the file is mentioned
7774 explicitly in some other way.
7775
7776 @cindex intermediate files, preserving
7777 @cindex preserving intermediate files
7778 @cindex secondary files
7779 You can prevent automatic deletion of an intermediate file by marking it
7780 as a @dfn{secondary} file.  To do this, list it as a prerequisite of the
7781 special target @code{.SECONDARY}.  When a file is secondary, @code{make}
7782 will not create the file merely because it does not already exist, but
7783 @code{make} does not automatically delete the file.  Marking a file as
7784 secondary also marks it as intermediate.
7785
7786 You can list the target pattern of an implicit rule (such as @samp{%.o})
7787 as a prerequisite of the special target @code{.PRECIOUS} to preserve
7788 intermediate files made by implicit rules whose target patterns match
7789 that file's name; see @ref{Interrupts}.@refill
7790 @cindex preserving with @code{.PRECIOUS}
7791 @cindex @code{.PRECIOUS} intermediate files
7792
7793 A chain can involve more than two implicit rules.  For example, it is
7794 possible to make a file @file{foo} from @file{RCS/foo.y,v} by running RCS,
7795 Yacc and @code{cc}.  Then both @file{foo.y} and @file{foo.c} are
7796 intermediate files that are deleted at the end.@refill
7797
7798 No single implicit rule can appear more than once in a chain.  This means
7799 that @code{make} will not even consider such a ridiculous thing as making
7800 @file{foo} from @file{foo.o.o} by running the linker twice.  This
7801 constraint has the added benefit of preventing any infinite loop in the
7802 search for an implicit rule chain.
7803
7804 There are some special implicit rules to optimize certain cases that would
7805 otherwise be handled by rule chains.  For example, making @file{foo} from
7806 @file{foo.c} could be handled by compiling and linking with separate
7807 chained rules, using @file{foo.o} as an intermediate file.  But what
7808 actually happens is that a special rule for this case does the compilation
7809 and linking with a single @code{cc} command.  The optimized rule is used in
7810 preference to the step-by-step chain because it comes earlier in the
7811 ordering of rules.
7812
7813 @node Pattern Rules, Last Resort, Chained Rules, Implicit Rules
7814 @section Defining and Redefining Pattern Rules
7815
7816 You define an implicit rule by writing a @dfn{pattern rule}.  A pattern
7817 rule looks like an ordinary rule, except that its target contains the
7818 character @samp{%} (exactly one of them).  The target is considered a
7819 pattern for matching file names; the @samp{%} can match any nonempty
7820 substring, while other characters match only themselves.  The prerequisites
7821 likewise use @samp{%} to show how their names relate to the target name.
7822
7823 Thus, a pattern rule @samp{%.o : %.c} says how to make any file
7824 @file{@var{stem}.o} from another file @file{@var{stem}.c}.@refill
7825
7826 Note that expansion using @samp{%} in pattern rules occurs
7827 @strong{after} any variable or function expansions, which take place
7828 when the makefile is read.  @xref{Using Variables, , How to Use
7829 Variables}, and @ref{Functions, ,Functions for Transforming Text}.
7830
7831 @menu
7832 * Pattern Intro::               An introduction to pattern rules.
7833 * Pattern Examples::            Examples of pattern rules.
7834 * Automatic::                   How to use automatic variables in the
7835                                   commands of implicit rules.
7836 * Pattern Match::               How patterns match.
7837 * Match-Anything Rules::        Precautions you should take prior to
7838                                   defining rules that can match any
7839                                   target file whatever.
7840 * Canceling Rules::             How to override or cancel built-in rules.
7841 @end menu
7842
7843 @node Pattern Intro, Pattern Examples,  , Pattern Rules
7844 @subsection Introduction to Pattern Rules
7845 @cindex pattern rule
7846 @cindex rule, pattern
7847
7848 A pattern rule contains the character @samp{%} (exactly one of them)
7849 in the target; otherwise, it looks exactly like an ordinary rule.  The
7850 target is a pattern for matching file names; the @samp{%} matches any
7851 nonempty substring, while other characters match only themselves.
7852 @cindex target pattern, implicit
7853 @cindex @code{%}, in pattern rules
7854
7855 For example, @samp{%.c} as a pattern matches any file name that ends in
7856 @samp{.c}.  @samp{s.%.c} as a pattern matches any file name that starts
7857 with @samp{s.}, ends in @samp{.c} and is at least five characters long.
7858 (There must be at least one character to match the @samp{%}.)  The substring
7859 that the @samp{%} matches is called the @dfn{stem}.@refill
7860
7861 @samp{%} in a prerequisite of a pattern rule stands for the same stem
7862 that was matched by the @samp{%} in the target.  In order for
7863 the pattern rule to apply, its target pattern must match the file name
7864 under consideration, and its prerequisite patterns must name files that
7865 exist or can be made.  These files become prerequisites of the target.
7866 @cindex prerequisite pattern, implicit
7867
7868 Thus, a rule of the form
7869
7870 @example
7871 %.o : %.c ; @var{command}@dots{}
7872 @end example
7873
7874 @noindent
7875 specifies how to make a file @file{@var{n}.o}, with another file
7876 @file{@var{n}.c} as its prerequisite, provided that @file{@var{n}.c}
7877 exists or can be made.
7878
7879 There may also be prerequisites that do not use @samp{%}; such a prerequisite
7880 attaches to every file made by this pattern rule.  These unvarying
7881 prerequisites are useful occasionally.
7882
7883 A pattern rule need not have any prerequisites that contain @samp{%}, or
7884 in fact any prerequisites at all.  Such a rule is effectively a general
7885 wildcard.  It provides a way to make any file that matches the target
7886 pattern.  @xref{Last Resort}.
7887
7888 @c !!! The end of of this paragraph should be rewritten.  --bob
7889 Pattern rules may have more than one target.  Unlike normal rules, this
7890 does not act as many different rules with the same prerequisites and
7891 commands.  If a pattern rule has multiple targets, @code{make} knows that
7892 the rule's commands are responsible for making all of the targets.  The
7893 commands are executed only once to make all the targets.  When searching
7894 for a pattern rule to match a target, the target patterns of a rule other
7895 than the one that matches the target in need of a rule are incidental:
7896 @code{make} worries only about giving commands and prerequisites to the file
7897 presently in question.  However, when this file's commands are run, the
7898 other targets are marked as having been updated themselves.
7899 @cindex multiple targets, in pattern rule
7900 @cindex target, multiple in pattern rule
7901
7902 The order in which pattern rules appear in the makefile is important
7903 since this is the order in which they are considered.
7904 Of equally applicable
7905 rules, only the first one found is used.  The rules you write take precedence
7906 over those that are built in.  Note however, that a rule whose
7907 prerequisites actually exist or are mentioned always takes priority over a
7908 rule with prerequisites that must be made by chaining other implicit rules.
7909 @cindex pattern rules, order of
7910 @cindex order of pattern rules
7911
7912 @node Pattern Examples, Automatic, Pattern Intro, Pattern Rules
7913 @subsection Pattern Rule Examples
7914
7915 Here are some examples of pattern rules actually predefined in
7916 @code{make}.  First, the rule that compiles @samp{.c} files into @samp{.o}
7917 files:@refill
7918
7919 @example
7920 %.o : %.c
7921         $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@@
7922 @end example
7923
7924 @noindent
7925 defines a rule that can make any file @file{@var{x}.o} from
7926 @file{@var{x}.c}.  The command uses the automatic variables @samp{$@@} and
7927 @samp{$<} to substitute the names of the target file and the source file
7928 in each case where the rule applies (@pxref{Automatic, ,Automatic Variables}).@refill
7929
7930 Here is a second built-in rule:
7931
7932 @example
7933 % :: RCS/%,v
7934         $(CO) $(COFLAGS) $<
7935 @end example
7936
7937 @noindent
7938 defines a rule that can make any file @file{@var{x}} whatsoever from a
7939 corresponding file @file{@var{x},v} in the subdirectory @file{RCS}.  Since
7940 the target is @samp{%}, this rule will apply to any file whatever, provided
7941 the appropriate prerequisite file exists.  The double colon makes the rule
7942 @dfn{terminal}, which means that its prerequisite may not be an intermediate
7943 file (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}).@refill
7944
7945 @need 500
7946 This pattern rule has two targets:
7947
7948 @example
7949 @group
7950 %.tab.c %.tab.h: %.y
7951         bison -d $<
7952 @end group
7953 @end example
7954
7955 @noindent
7956 @c The following paragraph is rewritten to avoid overfull hboxes
7957 This tells @code{make} that the command @samp{bison -d @var{x}.y} will
7958 make both @file{@var{x}.tab.c} and @file{@var{x}.tab.h}.  If the file
7959 @file{foo} depends on the files @file{parse.tab.o} and @file{scan.o}
7960 and the file @file{scan.o} depends on the file @file{parse.tab.h},
7961 when @file{parse.y} is changed, the command @samp{bison -d parse.y}
7962 will be executed only once, and the prerequisites of both
7963 @file{parse.tab.o} and @file{scan.o} will be satisfied.  (Presumably
7964 the file @file{parse.tab.o} will be recompiled from @file{parse.tab.c}
7965 and the file @file{scan.o} from @file{scan.c}, while @file{foo} is
7966 linked from @file{parse.tab.o}, @file{scan.o}, and its other
7967 prerequisites, and it will execute happily ever after.)@refill
7968
7969 @node Automatic, Pattern Match, Pattern Examples, Pattern Rules
7970 @subsection Automatic Variables
7971 @cindex automatic variables
7972 @cindex variables, automatic
7973 @cindex variables, and implicit rule
7974
7975 Suppose you are writing a pattern rule to compile a @samp{.c} file into a
7976 @samp{.o} file: how do you write the @samp{cc} command so that it operates
7977 on the right source file name?  You cannot write the name in the command,
7978 because the name is different each time the implicit rule is applied.
7979
7980 What you do is use a special feature of @code{make}, the @dfn{automatic
7981 variables}.  These variables have values computed afresh for each rule that
7982 is executed, based on the target and prerequisites of the rule.  In this
7983 example, you would use @samp{$@@} for the object file name and @samp{$<}
7984 for the source file name.
7985
7986 Here is a table of automatic variables:
7987
7988 @table @code
7989 @vindex $@@
7990 @vindex @@ @r{(automatic variable)}
7991 @item $@@
7992 The file name of the target of the rule.  If the target is an archive
7993 member, then @samp{$@@} is the name of the archive file.  In a pattern
7994 rule that has multiple targets (@pxref{Pattern Intro, ,Introduction to
7995 Pattern Rules}), @samp{$@@} is the name of whichever target caused the
7996 rule's commands to be run.
7997
7998 @vindex $%
7999 @vindex % @r{(automatic variable)}
8000 @item $%
8001 The target member name, when the target is an archive member.
8002 @xref{Archives}.  For example, if the target is @file{foo.a(bar.o)} then
8003 @samp{$%} is @file{bar.o} and @samp{$@@} is @file{foo.a}.  @samp{$%} is
8004 empty when the target is not an archive member.
8005
8006 @vindex $<
8007 @vindex < @r{(automatic variable)}
8008 @item $<
8009 The name of the first prerequisite.  If the target got its commands from
8010 an implicit rule, this will be the first prerequisite added by the
8011 implicit rule (@pxref{Implicit Rules}).
8012
8013 @vindex $?
8014 @vindex ? @r{(automatic variable)}
8015 @item $?
8016 The names of all the prerequisites that are newer than the target, with
8017 spaces between them.  For prerequisites which are archive members, only
8018 the member named is used (@pxref{Archives}).
8019 @cindex prerequisites, list of changed
8020 @cindex list of changed prerequisites
8021
8022 @vindex $^
8023 @vindex ^ @r{(automatic variable)}
8024 @item $^
8025 The names of all the prerequisites, with spaces between them.  For
8026 prerequisites which are archive members, only the member named is used
8027 (@pxref{Archives}).  A target has only one prerequisite on each other file
8028 it depends on, no matter how many times each file is listed as a
8029 prerequisite.  So if you list a prerequisite more than once for a target,
8030 the value of @code{$^} contains just one copy of the name.
8031 @cindex prerequisites, list of all
8032 @cindex list of all prerequisites
8033
8034 @vindex $+
8035 @vindex + @r{(automatic variable)}
8036 @item $+
8037 This is like @samp{$^}, but prerequisites listed more than once are
8038 duplicated in the order they were listed in the makefile.  This is
8039 primarily useful for use in linking commands where it is meaningful to
8040 repeat library file names in a particular order.
8041
8042 @vindex $*
8043 @vindex * @r{(automatic variable)}
8044 @item $*
8045 The stem with which an implicit rule matches (@pxref{Pattern Match, ,How
8046 Patterns Match}).  If the target is @file{dir/a.foo.b} and the target
8047 pattern is @file{a.%.b} then the stem is @file{dir/foo}.  The stem is
8048 useful for constructing names of related files.@refill
8049 @cindex stem, variable for
8050
8051 In a static pattern rule, the stem is part of the file name that matched
8052 the @samp{%} in the target pattern.
8053
8054 In an explicit rule, there is no stem; so @samp{$*} cannot be determined
8055 in that way.  Instead, if the target name ends with a recognized suffix
8056 (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}), @samp{$*} is set to
8057 the target name minus the suffix.  For example, if the target name is
8058 @samp{foo.c}, then @samp{$*} is set to @samp{foo}, since @samp{.c} is a
8059 suffix.  GNU @code{make} does this bizarre thing only for compatibility
8060 with other implementations of @code{make}.  You should generally avoid
8061 using @samp{$*} except in implicit rules or static pattern rules.@refill
8062
8063 If the target name in an explicit rule does not end with a recognized
8064 suffix, @samp{$*} is set to the empty string for that rule.
8065 @end table
8066
8067 @samp{$?} is useful even in explicit rules when you wish to operate on only
8068 the prerequisites that have changed.  For example, suppose that an archive
8069 named @file{lib} is supposed to contain copies of several object files.
8070 This rule copies just the changed object files into the archive:
8071
8072 @example
8073 @group
8074 lib: foo.o bar.o lose.o win.o
8075         ar r lib $?
8076 @end group
8077 @end example
8078
8079 Of the variables listed above, four have values that are single file
8080 names, and three have values that are lists of file names.  These seven
8081 have variants that get just the file's directory name or just the file
8082 name within the directory.  The variant variables' names are formed by
8083 appending @samp{D} or @samp{F}, respectively.  These variants are
8084 semi-obsolete in GNU @code{make} since the functions @code{dir} and
8085 @code{notdir} can be used to get a similar effect (@pxref{File Name
8086 Functions, , Functions for File Names}).  Note, however, that the
8087 @samp{F} variants all omit the trailing slash which always appears in
8088 the output of the @code{dir} function.  Here is a table of the variants:
8089
8090 @table @samp
8091 @vindex $(@@D)
8092 @vindex @@D @r{(automatic variable)}
8093 @item $(@@D)
8094 The directory part of the file name of the target, with the trailing
8095 slash removed.  If the value of @samp{$@@} is @file{dir/foo.o} then
8096 @samp{$(@@D)} is @file{dir}.  This value is @file{.} if @samp{$@@} does
8097 not contain a slash.
8098
8099 @vindex $(@@F)
8100 @vindex @@F @r{(automatic variable)}
8101 @item $(@@F)
8102 The file-within-directory part of the file name of the target.  If the
8103 value of @samp{$@@} is @file{dir/foo.o} then @samp{$(@@F)} is
8104 @file{foo.o}.  @samp{$(@@F)} is equivalent to @samp{$(notdir $@@)}.
8105
8106 @vindex $(*D)
8107 @vindex *D @r{(automatic variable)}
8108 @item $(*D)
8109 @vindex $(*F)
8110 @vindex *F @r{(automatic variable)}
8111 @itemx $(*F)
8112 The directory part and the file-within-directory
8113 part of the stem; @file{dir} and @file{foo} in this example.
8114
8115 @vindex $(%D)
8116 @vindex %D @r{(automatic variable)}
8117 @item $(%D)
8118 @vindex $(%F)
8119 @vindex %F @r{(automatic variable)}
8120 @itemx $(%F)
8121 The directory part and the file-within-directory part of the target
8122 archive member name.  This makes sense only for archive member targets
8123 of the form @file{@var{archive}(@var{member})} and is useful only when
8124 @var{member} may contain a directory name.  (@xref{Archive Members,
8125 ,Archive Members as Targets}.)
8126
8127 @vindex $(<D)
8128 @vindex <D @r{(automatic variable)}
8129 @item $(<D)
8130 @vindex $(<F)
8131 @vindex <F @r{(automatic variable)}
8132 @itemx $(<F)
8133 The directory part and the file-within-directory
8134 part of the first prerequisite.
8135
8136 @vindex $(^D)
8137 @vindex ^D @r{(automatic variable)}
8138 @item $(^D)
8139 @vindex $(^F)
8140 @vindex ^F @r{(automatic variable)}
8141 @itemx $(^F)
8142 Lists of the directory parts and the file-within-directory
8143 parts of all prerequisites.
8144
8145 @vindex $(?D)
8146 @vindex ?D @r{(automatic variable)}
8147 @item $(?D)
8148 @vindex $(?F)
8149 @vindex ?F @r{(automatic variable)}
8150 @itemx $(?F)
8151 Lists of the directory parts and the file-within-directory parts of
8152 all prerequisites that are newer than the target.
8153 @end table
8154
8155 Note that we use a special stylistic convention when we talk about these
8156 automatic variables; we write ``the value of @samp{$<}'', rather than
8157 @w{``the variable @code{<}''} as we would write for ordinary variables
8158 such as @code{objects} and @code{CFLAGS}.  We think this convention
8159 looks more natural in this special case.  Please do not assume it has a
8160 deep significance; @samp{$<} refers to the variable named @code{<} just
8161 as @samp{$(CFLAGS)} refers to the variable named @code{CFLAGS}.
8162 You could just as well use @samp{$(<)} in place of @samp{$<}.
8163
8164 @node Pattern Match, Match-Anything Rules, Automatic, Pattern Rules
8165 @subsection How Patterns Match
8166
8167 @cindex stem
8168 A target pattern is composed of a @samp{%} between a prefix and a suffix,
8169 either or both of which may be empty.  The pattern matches a file name only
8170 if the file name starts with the prefix and ends with the suffix, without
8171 overlap.  The text between the prefix and the suffix is called the
8172 @dfn{stem}.  Thus, when the pattern @samp{%.o} matches the file name
8173 @file{test.o}, the stem is @samp{test}.  The pattern rule prerequisites are
8174 turned into actual file names by substituting the stem for the character
8175 @samp{%}.  Thus, if in the same example one of the prerequisites is written
8176 as @samp{%.c}, it expands to @samp{test.c}.@refill
8177
8178 When the target pattern does not contain a slash (and it usually does
8179 not), directory names in the file names are removed from the file name
8180 before it is compared with the target prefix and suffix.  After the
8181 comparison of the file name to the target pattern, the directory
8182 names, along with the slash that ends them, are added on to the
8183 prerequisite file names generated from the pattern rule's prerequisite
8184 patterns and the file name. The directories are ignored only for the
8185 purpose of finding an implicit rule to use, not in the application of
8186 that rule.  Thus, @samp{e%t} matches the file name @file{src/eat},
8187 with @samp{src/a} as the stem.  When prerequisites are turned into file
8188 names, the directories from the stem are added at the front, while the
8189 rest of the stem is substituted for the @samp{%}.  The stem
8190 @samp{src/a} with a prerequisite pattern @samp{c%r} gives the file name
8191 @file{src/car}.@refill
8192
8193 @node Match-Anything Rules, Canceling Rules, Pattern Match, Pattern Rules
8194 @subsection Match-Anything Pattern Rules
8195
8196 @cindex match-anything rule
8197 @cindex terminal rule
8198 When a pattern rule's target is just @samp{%}, it matches any file name
8199 whatever.  We call these rules @dfn{match-anything} rules.  They are very
8200 useful, but it can take a lot of time for @code{make} to think about them,
8201 because it must consider every such rule for each file name listed either
8202 as a target or as a prerequisite.
8203
8204 Suppose the makefile mentions @file{foo.c}.  For this target, @code{make}
8205 would have to consider making it by linking an object file @file{foo.c.o},
8206 or by C compilation-and-linking in one step from @file{foo.c.c}, or by
8207 Pascal compilation-and-linking from @file{foo.c.p}, and many other
8208 possibilities.
8209
8210 We know these possibilities are ridiculous since @file{foo.c} is a C source
8211 file, not an executable.  If @code{make} did consider these possibilities,
8212 it would ultimately reject them, because files such as @file{foo.c.o} and
8213 @file{foo.c.p} would not exist.  But these possibilities are so
8214 numerous that @code{make} would run very slowly if it had to consider
8215 them.@refill
8216
8217 To gain speed, we have put various constraints on the way @code{make}
8218 considers match-anything rules.  There are two different constraints that
8219 can be applied, and each time you define a match-anything rule you must
8220 choose one or the other for that rule.
8221
8222 One choice is to mark the match-anything rule as @dfn{terminal} by defining
8223 it with a double colon.  When a rule is terminal, it does not apply unless
8224 its prerequisites actually exist.  Prerequisites that could be made with
8225 other implicit rules are not good enough.  In other words, no further
8226 chaining is allowed beyond a terminal rule.
8227
8228 For example, the built-in implicit rules for extracting sources from RCS
8229 and SCCS files are terminal; as a result, if the file @file{foo.c,v} does
8230 not exist, @code{make} will not even consider trying to make it as an
8231 intermediate file from @file{foo.c,v.o} or from @file{RCS/SCCS/s.foo.c,v}.
8232 RCS and SCCS files are generally ultimate source files, which should not be
8233 remade from any other files; therefore, @code{make} can save time by not
8234 looking for ways to remake them.@refill
8235
8236 If you do not mark the match-anything rule as terminal, then it is
8237 nonterminal.  A nonterminal match-anything rule cannot apply to a file name
8238 that indicates a specific type of data.  A file name indicates a specific
8239 type of data if some non-match-anything implicit rule target matches it.
8240
8241 For example, the file name @file{foo.c} matches the target for the pattern
8242 rule @samp{%.c : %.y} (the rule to run Yacc).  Regardless of whether this
8243 rule is actually applicable (which happens only if there is a file
8244 @file{foo.y}), the fact that its target matches is enough to prevent
8245 consideration of any nonterminal match-anything rules for the file
8246 @file{foo.c}.  Thus, @code{make} will not even consider trying to make
8247 @file{foo.c} as an executable file from @file{foo.c.o}, @file{foo.c.c},
8248 @file{foo.c.p}, etc.@refill
8249
8250 The motivation for this constraint is that nonterminal match-anything
8251 rules are used for making files containing specific types of data (such as
8252 executable files) and a file name with a recognized suffix indicates some
8253 other specific type of data (such as a C source file).
8254
8255 Special built-in dummy pattern rules are provided solely to recognize
8256 certain file names so that nonterminal match-anything rules will not be
8257 considered.  These dummy rules have no prerequisites and no commands, and
8258 they are ignored for all other purposes.  For example, the built-in
8259 implicit rule
8260
8261 @example
8262 %.p :
8263 @end example
8264
8265 @noindent
8266 exists to make sure that Pascal source files such as @file{foo.p} match a
8267 specific target pattern and thereby prevent time from being wasted looking
8268 for @file{foo.p.o} or @file{foo.p.c}.
8269
8270 Dummy pattern rules such as the one for @samp{%.p} are made for every
8271 suffix listed as valid for use in suffix rules (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}).
8272
8273 @node Canceling Rules,  , Match-Anything Rules, Pattern Rules
8274 @subsection Canceling Implicit Rules
8275
8276 You can override a built-in implicit rule (or one you have defined
8277 yourself) by defining a new pattern rule with the same target and
8278 prerequisites, but different commands.  When the new rule is defined, the
8279 built-in one is replaced.  The new rule's position in the sequence of
8280 implicit rules is determined by where you write the new rule.
8281
8282 You can cancel a built-in implicit rule by defining a pattern rule with the
8283 same target and prerequisites, but no commands.  For example, the following
8284 would cancel the rule that runs the assembler:
8285
8286 @example
8287 %.o : %.s
8288 @end example
8289
8290 @node Last Resort, Suffix Rules, Pattern Rules, Implicit Rules
8291 @section Defining Last-Resort Default Rules
8292 @cindex last-resort default rules
8293 @cindex default rules, last-resort
8294
8295 You can define a last-resort implicit rule by writing a terminal
8296 match-anything pattern rule with no prerequisites (@pxref{Match-Anything
8297 Rules}).  This is just like any other pattern rule; the only thing
8298 special about it is that it will match any target.  So such a rule's
8299 commands are used for all targets and prerequisites that have no commands
8300 of their own and for which no other implicit rule applies.
8301
8302 For example, when testing a makefile, you might not care if the source
8303 files contain real data, only that they exist.  Then you might do this:
8304
8305 @example
8306 %::
8307         touch $@@
8308 @end example
8309
8310 @noindent
8311 to cause all the source files needed (as prerequisites) to be created
8312 automatically.
8313
8314 @findex .DEFAULT
8315 You can instead define commands to be used for targets for which there
8316 are no rules at all, even ones which don't specify commands.  You do
8317 this by writing a rule for the target @code{.DEFAULT}.  Such a rule's
8318 commands are used for all prerequisites which do not appear as targets in
8319 any explicit rule, and for which no implicit rule applies.  Naturally,
8320 there is no @code{.DEFAULT} rule unless you write one.
8321
8322 If you use @code{.DEFAULT} with no commands or prerequisites:
8323
8324 @example
8325 .DEFAULT:
8326 @end example
8327
8328 @noindent
8329 the commands previously stored for @code{.DEFAULT} are cleared.
8330 Then @code{make} acts as if you had never defined @code{.DEFAULT} at all.
8331
8332 If you do not want a target to get the commands from a match-anything
8333 pattern rule or @code{.DEFAULT}, but you also do not want any commands
8334 to be run for the target, you can give it empty commands (@pxref{Empty
8335 Commands, ,Defining Empty Commands}).@refill
8336
8337 You can use a last-resort rule to override part of another makefile.
8338 @xref{Overriding Makefiles, , Overriding Part of Another Makefile}.
8339
8340 @node Suffix Rules, Implicit Rule Search, Last Resort, Implicit Rules
8341 @section Old-Fashioned Suffix Rules
8342 @cindex old-fashioned suffix rules
8343 @cindex suffix rule
8344
8345 @dfn{Suffix rules} are the old-fashioned way of defining implicit rules for
8346 @code{make}.  Suffix rules are obsolete because pattern rules are more
8347 general and clearer.  They are supported in GNU @code{make} for
8348 compatibility with old makefiles.  They come in two kinds:
8349 @dfn{double-suffix} and @dfn{single-suffix}.@refill
8350
8351 A double-suffix rule is defined by a pair of suffixes: the target suffix
8352 and the source suffix.  It matches any file whose name ends with the
8353 target suffix.  The corresponding implicit prerequisite is made by
8354 replacing the target suffix with the source suffix in the file name.  A
8355 two-suffix rule whose target and source suffixes are @samp{.o} and
8356 @samp{.c} is equivalent to the pattern rule @samp{%.o : %.c}.
8357
8358 A single-suffix rule is defined by a single suffix, which is the source
8359 suffix.  It matches any file name, and the corresponding implicit
8360 prerequisite name is made by appending the source suffix.  A single-suffix
8361 rule whose source suffix is @samp{.c} is equivalent to the pattern rule
8362 @samp{% : %.c}.
8363
8364 Suffix rule definitions are recognized by comparing each rule's target
8365 against a defined list of known suffixes.  When @code{make} sees a rule
8366 whose target is a known suffix, this rule is considered a single-suffix
8367 rule.  When @code{make} sees a rule whose target is two known suffixes
8368 concatenated, this rule is taken as a double-suffix rule.
8369
8370 For example, @samp{.c} and @samp{.o} are both on the default list of
8371 known suffixes.  Therefore, if you define a rule whose target is
8372 @samp{.c.o}, @code{make} takes it to be a double-suffix rule with source
8373 suffix @samp{.c} and target suffix @samp{.o}.  Here is the old-fashioned
8374 way to define the rule for compiling a C source file:@refill
8375
8376 @example
8377 .c.o:
8378         $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
8379 @end example
8380
8381 Suffix rules cannot have any prerequisites of their own.  If they have any,
8382 they are treated as normal files with funny names, not as suffix rules.
8383 Thus, the rule:
8384
8385 @example
8386 .c.o: foo.h
8387         $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
8388 @end example
8389
8390 @noindent
8391 tells how to make the file @file{.c.o} from the prerequisite file
8392 @file{foo.h}, and is not at all like the pattern rule:
8393
8394 @example
8395 %.o: %.c foo.h
8396         $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
8397 @end example
8398
8399 @noindent
8400 which tells how to make @samp{.o} files from @samp{.c} files, and makes all
8401 @samp{.o} files using this pattern rule also depend on @file{foo.h}.
8402
8403 Suffix rules with no commands are also meaningless.  They do not remove
8404 previous rules as do pattern rules with no commands (@pxref{Canceling
8405 Rules, , Canceling Implicit Rules}).  They simply enter the suffix or pair of suffixes concatenated as
8406 a target in the data base.@refill
8407
8408 @findex .SUFFIXES
8409 The known suffixes are simply the names of the prerequisites of the special
8410 target @code{.SUFFIXES}.  You can add your own suffixes by writing a rule
8411 for @code{.SUFFIXES} that adds more prerequisites, as in:
8412
8413 @example
8414 .SUFFIXES: .hack .win
8415 @end example
8416
8417 @noindent
8418 which adds @samp{.hack} and @samp{.win} to the end of the list of suffixes.
8419
8420 If you wish to eliminate the default known suffixes instead of just adding
8421 to them, write a rule for @code{.SUFFIXES} with no prerequisites.  By
8422 special dispensation, this eliminates all existing prerequisites of
8423 @code{.SUFFIXES}.  You can then write another rule to add the suffixes you
8424 want.  For example,
8425
8426 @example
8427 @group
8428 .SUFFIXES:            # @r{Delete the default suffixes}
8429 .SUFFIXES: .c .o .h   # @r{Define our suffix list}
8430 @end group
8431 @end example
8432
8433 The @samp{-r} or @samp{--no-builtin-rules} flag causes the default
8434 list of suffixes to be empty.
8435
8436 @vindex SUFFIXES
8437 The variable @code{SUFFIXES} is defined to the default list of suffixes
8438 before @code{make} reads any makefiles.  You can change the list of suffixes
8439 with a rule for the special target @code{.SUFFIXES}, but that does not alter
8440 this variable.
8441
8442 @node Implicit Rule Search,  , Suffix Rules, Implicit Rules
8443 @section Implicit Rule Search Algorithm
8444 @cindex implicit rule, search algorithm
8445 @cindex search algorithm, implicit rule
8446
8447 Here is the procedure @code{make} uses for searching for an implicit rule
8448 for a target @var{t}.  This procedure is followed for each double-colon
8449 rule with no commands, for each target of ordinary rules none of which have
8450 commands, and for each prerequisite that is not the target of any rule.  It
8451 is also followed recursively for prerequisites that come from implicit
8452 rules, in the search for a chain of rules.
8453
8454 Suffix rules are not mentioned in this algorithm because suffix rules are
8455 converted to equivalent pattern rules once the makefiles have been read in.
8456
8457 For an archive member target of the form
8458 @samp{@var{archive}(@var{member})}, the following algorithm is run
8459 twice, first using the entire target name @var{t}, and second using
8460 @samp{(@var{member})} as the target @var{t} if the first run found no
8461 rule.@refill
8462
8463 @enumerate
8464 @item
8465 Split @var{t} into a directory part, called @var{d}, and the rest,
8466 called @var{n}.  For example, if @var{t} is @samp{src/foo.o}, then
8467 @var{d} is @samp{src/} and @var{n} is @samp{foo.o}.@refill
8468
8469 @item
8470 Make a list of all the pattern rules one of whose targets matches
8471 @var{t} or @var{n}.  If the target pattern contains a slash, it is
8472 matched against @var{t}; otherwise, against @var{n}.
8473
8474 @item
8475 If any rule in that list is @emph{not} a match-anything rule, then
8476 remove all nonterminal match-anything rules from the list.
8477
8478 @item
8479 Remove from the list all rules with no commands.
8480
8481 @item
8482 For each pattern rule in the list:
8483
8484 @enumerate a
8485 @item
8486 Find the stem @var{s}, which is the nonempty part of @var{t} or @var{n}
8487 matched by the @samp{%} in the target pattern.@refill
8488
8489 @item
8490 Compute the prerequisite names by substituting @var{s} for @samp{%}; if
8491 the target pattern does not contain a slash, append @var{d} to
8492 the front of each prerequisite name.@refill
8493
8494 @item
8495 Test whether all the prerequisites exist or ought to exist.  (If a
8496 file name is mentioned in the makefile as a target or as an explicit
8497 prerequisite, then we say it ought to exist.)
8498
8499 If all prerequisites exist or ought to exist, or there are no prerequisites,
8500 then this rule applies.
8501 @end enumerate
8502
8503 @item
8504 If no pattern rule has been found so far, try harder.
8505 For each pattern rule in the list:
8506
8507 @enumerate a
8508 @item
8509 If the rule is terminal, ignore it and go on to the next rule.
8510
8511 @item
8512 Compute the prerequisite names as before.
8513
8514 @item
8515 Test whether all the prerequisites exist or ought to exist.
8516
8517 @item
8518 For each prerequisite that does not exist, follow this algorithm
8519 recursively to see if the prerequisite can be made by an implicit
8520 rule.
8521
8522 @item
8523 If all prerequisites exist, ought to exist, or can be
8524 made by implicit rules, then this rule applies.
8525 @end enumerate
8526
8527 @item
8528 If no implicit rule applies, the rule for @code{.DEFAULT}, if any,
8529 applies.  In that case, give @var{t} the same commands that
8530 @code{.DEFAULT} has.  Otherwise, there are no commands for @var{t}.
8531 @end enumerate
8532
8533 Once a rule that applies has been found, for each target pattern of the
8534 rule other than the one that matched @var{t} or @var{n}, the @samp{%} in
8535 the pattern is replaced with @var{s} and the resultant file name is stored
8536 until the commands to remake the target file @var{t} are executed.  After
8537 these commands are executed, each of these stored file names are entered
8538 into the data base and marked as having been updated and having the same
8539 update status as the file @var{t}.
8540
8541 When the commands of a pattern rule are executed for @var{t}, the automatic
8542 variables are set corresponding to the target and prerequisites.
8543 @xref{Automatic, ,Automatic Variables}.
8544
8545 @node Archives, Features, Implicit Rules, Top
8546 @chapter Using @code{make} to Update Archive Files
8547 @cindex archive
8548
8549 @dfn{Archive files} are files containing named subfiles called
8550 @dfn{members}; they are maintained with the program @code{ar} and their
8551 main use is as subroutine libraries for linking.
8552
8553 @menu
8554 * Archive Members::             Archive members as targets.
8555 * Archive Update::              The implicit rule for archive member targets.
8556 * Archive Pitfalls::            Dangers to watch out for when using archives.
8557 * Archive Suffix Rules::        You can write a special kind of suffix rule
8558                                   for updating archives.
8559 @end menu
8560
8561 @node Archive Members, Archive Update,  , Archives
8562 @section Archive Members as Targets
8563 @cindex archive member targets
8564
8565 An individual member of an archive file can be used as a target or
8566 prerequisite in @code{make}.  You specify the member named @var{member} in
8567 archive file @var{archive} as follows:
8568
8569 @example
8570 @var{archive}(@var{member})
8571 @end example
8572
8573 @noindent
8574 This construct is available only in targets and prerequisites, not in
8575 commands!  Most programs that you might use in commands do not support this
8576 syntax and cannot act directly on archive members.  Only @code{ar} and
8577 other programs specifically designed to operate on archives can do so.
8578 Therefore, valid commands to update an archive member target probably must
8579 use @code{ar}.  For example, this rule says to create a member
8580 @file{hack.o} in archive @file{foolib} by copying the file @file{hack.o}:
8581
8582 @example
8583 foolib(hack.o) : hack.o
8584         ar cr foolib hack.o
8585 @end example
8586
8587 In fact, nearly all archive member targets are updated in just this way
8588 and there is an implicit rule to do it for you.  @strong{Note:} The
8589 @samp{c} flag to @code{ar} is required if the archive file does not
8590 already exist.
8591
8592 To specify several members in the same archive, you can write all the
8593 member names together between the parentheses.  For example:
8594
8595 @example
8596 foolib(hack.o kludge.o)
8597 @end example
8598
8599 @noindent
8600 is equivalent to:
8601
8602 @example
8603 foolib(hack.o) foolib(kludge.o)
8604 @end example
8605
8606 @cindex wildcard, in archive member
8607 You can also use shell-style wildcards in an archive member reference.
8608 @xref{Wildcards, ,Using Wildcard Characters in File Names}.  For
8609 example, @w{@samp{foolib(*.o)}} expands to all existing members of the
8610 @file{foolib} archive whose names end in @samp{.o}; perhaps
8611 @samp{@w{foolib(hack.o)} @w{foolib(kludge.o)}}.
8612
8613 @node Archive Update
8614 @section Implicit Rule for Archive Member Targets
8615
8616 Recall that a target that looks like @file{@var{a}(@var{m})} stands for the
8617 member named @var{m} in the archive file @var{a}.
8618
8619 When @code{make} looks for an implicit rule for such a target, as a special
8620 feature it considers implicit rules that match @file{(@var{m})}, as well as
8621 those that match the actual target @file{@var{a}(@var{m})}.
8622
8623 This causes one special rule whose target is @file{(%)} to match.  This
8624 rule updates the target @file{@var{a}(@var{m})} by copying the file @var{m}
8625 into the archive.  For example, it will update the archive member target
8626 @file{foo.a(bar.o)} by copying the @emph{file} @file{bar.o} into the
8627 archive @file{foo.a} as a @emph{member} named @file{bar.o}.
8628
8629 When this rule is chained with others, the result is very powerful.
8630 Thus, @samp{make "foo.a(bar.o)"} (the quotes are needed to protect the
8631 @samp{(} and @samp{)} from being interpreted specially by the shell) in
8632 the presence of a file @file{bar.c} is enough to cause the following
8633 commands to be run, even without a makefile:
8634
8635 @example
8636 cc -c bar.c -o bar.o
8637 ar r foo.a bar.o
8638 rm -f bar.o
8639 @end example
8640
8641 @noindent
8642 Here @code{make} has envisioned the file @file{bar.o} as an intermediate
8643 file.  @xref{Chained Rules, ,Chains of Implicit Rules}.
8644
8645 Implicit rules such as this one are written using the automatic variable
8646 @samp{$%}.  @xref{Automatic, ,Automatic Variables}.
8647
8648 An archive member name in an archive cannot contain a directory name, but
8649 it may be useful in a makefile to pretend that it does.  If you write an
8650 archive member target @file{foo.a(dir/file.o)}, @code{make} will perform
8651 automatic updating with this command:
8652
8653 @example
8654 ar r foo.a dir/file.o
8655 @end example
8656
8657 @noindent
8658 which has the effect of copying the file @file{dir/file.o} into a member
8659 named @file{file.o}.  In connection with such usage, the automatic variables
8660 @code{%D} and @code{%F} may be useful.
8661
8662 @menu
8663 * Archive Symbols::             How to update archive symbol directories.
8664 @end menu
8665
8666 @node Archive Symbols,  ,  , Archive Update
8667 @subsection Updating Archive Symbol Directories
8668 @cindex @code{__.SYMDEF}
8669 @cindex updating archive symbol directories
8670 @cindex archive symbol directory updating
8671 @cindex symbol directories, updating archive
8672 @cindex directories, updating archive symbol
8673
8674 An archive file that is used as a library usually contains a special member
8675 named @file{__.SYMDEF} that contains a directory of the external symbol
8676 names defined by all the other members.  After you update any other
8677 members, you need to update @file{__.SYMDEF} so that it will summarize the
8678 other members properly.  This is done by running the @code{ranlib} program:
8679
8680 @example
8681 ranlib @var{archivefile}
8682 @end example
8683
8684 Normally you would put this command in the rule for the archive file,
8685 and make all the members of the archive file prerequisites of that rule.
8686 For example,
8687
8688 @example
8689 libfoo.a: libfoo.a(x.o) libfoo.a(y.o) @dots{}
8690         ranlib libfoo.a
8691 @end example
8692
8693 @noindent
8694 The effect of this is to update archive members @file{x.o}, @file{y.o},
8695 etc., and then update the symbol directory member @file{__.SYMDEF} by
8696 running @code{ranlib}.  The rules for updating the members are not shown
8697 here; most likely you can omit them and use the implicit rule which copies
8698 files into the archive, as described in the preceding section.
8699
8700 This is not necessary when using the GNU @code{ar} program, which
8701 updates the @file{__.SYMDEF} member automatically.
8702
8703 @node Archive Pitfalls
8704 @section Dangers When Using Archives
8705 @cindex archive, and parallel execution
8706 @cindex parallel execution, and archive update
8707 @cindex archive, and @code{-j}
8708 @cindex @code{-j}, and archive update
8709
8710 It is important to be careful when using parallel execution (the
8711 @code{-j} switch; @pxref{Parallel, ,Parallel Execution}) and archives.
8712 If multiple @code{ar} commands run at the same time on the same archive
8713 file, they will not know about each other and can corrupt the file.
8714
8715 Possibly a future version of @code{make} will provide a mechanism to
8716 circumvent this problem by serializing all commands that operate on the
8717 same archive file.  But for the time being, you must either write your
8718 makefiles to avoid this problem in some other way, or not use @code{-j}.
8719
8720 @node Archive Suffix Rules, , Archive Pitfalls, Archives
8721 @section Suffix Rules for Archive Files
8722 @cindex suffix rule, for archive
8723 @cindex archive, suffix rule for
8724 @cindex library archive, suffix rule for
8725 @cindex @code{.a} (archives)
8726
8727 You can write a special kind of suffix rule for dealing with archive
8728 files.  @xref{Suffix Rules}, for a full explanation of suffix rules.
8729 Archive suffix rules are obsolete in GNU @code{make}, because pattern
8730 rules for archives are a more general mechanism (@pxref{Archive
8731 Update}).  But they are retained for compatibility with other
8732 @code{make}s.
8733
8734 To write a suffix rule for archives, you simply write a suffix rule
8735 using the target suffix @samp{.a} (the usual suffix for archive files).
8736 For example, here is the old-fashioned suffix rule to update a library
8737 archive from C source files:
8738
8739 @example
8740 @group
8741 .c.a:
8742         $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o
8743         $(AR) r $@@ $*.o
8744         $(RM) $*.o
8745 @end group
8746 @end example
8747
8748 @noindent
8749 This works just as if you had written the pattern rule:
8750
8751 @example
8752 @group
8753 (%.o): %.c
8754         $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o
8755         $(AR) r $@@ $*.o
8756         $(RM) $*.o
8757 @end group
8758 @end example
8759
8760 In fact, this is just what @code{make} does when it sees a suffix rule
8761 with @samp{.a} as the target suffix.  Any double-suffix rule
8762 @w{@samp{.@var{x}.a}} is converted to a pattern rule with the target
8763 pattern @samp{(%.o)} and a prerequisite pattern of @samp{%.@var{x}}.
8764
8765 Since you might want to use @samp{.a} as the suffix for some other kind
8766 of file, @code{make} also converts archive suffix rules to pattern rules
8767 in the normal way (@pxref{Suffix Rules}).  Thus a double-suffix rule
8768 @w{@samp{.@var{x}.a}} produces two pattern rules: @samp{@w{(%.o):}
8769 @w{%.@var{x}}} and @samp{@w{%.a}: @w{%.@var{x}}}.@refill
8770
8771 @node Features, Missing, Archives, Top
8772 @chapter Features of GNU @code{make}
8773 @cindex features of GNU @code{make}
8774 @cindex portability
8775 @cindex compatibility
8776
8777 Here is a summary of the features of GNU @code{make}, for comparison
8778 with and credit to other versions of @code{make}.  We consider the
8779 features of @code{make} in 4.2 BSD systems as a baseline.  If you are
8780 concerned with writing portable makefiles, you should not use the
8781 features of @code{make} listed here, nor the ones in @ref{Missing}.
8782
8783 Many features come from the version of @code{make} in System V.
8784
8785 @itemize @bullet
8786 @item
8787 The @code{VPATH} variable and its special meaning.
8788 @xref{Directory Search, , Searching Directories for Prerequisites}.
8789 This feature exists in System V @code{make}, but is undocumented.
8790 It is documented in 4.3 BSD @code{make} (which says it mimics System V's
8791 @code{VPATH} feature).@refill
8792
8793 @item
8794 Included makefiles.  @xref{Include, ,Including Other Makefiles}.
8795 Allowing multiple files to be included with a single directive is a GNU
8796 extension.
8797
8798 @item
8799 Variables are read from and communicated via the environment.
8800 @xref{Environment, ,Variables from the Environment}.
8801
8802 @item
8803 Options passed through the variable @code{MAKEFLAGS} to recursive
8804 invocations of @code{make}.
8805 @xref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
8806
8807 @item
8808 The automatic variable @code{$%} is set to the member name
8809 in an archive reference.  @xref{Automatic, ,Automatic Variables}.
8810
8811 @item
8812 The automatic variables @code{$@@}, @code{$*}, @code{$<}, @code{$%},
8813 and @code{$?} have corresponding forms like @code{$(@@F)} and
8814 @code{$(@@D)}.  We have generalized this to @code{$^} as an obvious
8815 extension.  @xref{Automatic, ,Automatic Variables}.@refill
8816
8817 @item
8818 Substitution variable references.
8819 @xref{Reference, ,Basics of Variable References}.
8820
8821 @item
8822 The command-line options @samp{-b} and @samp{-m}, accepted and
8823 ignored.  In System V @code{make}, these options actually do something.
8824
8825 @item
8826 Execution of recursive commands to run @code{make} via the variable
8827 @code{MAKE} even if @samp{-n}, @samp{-q} or @samp{-t} is specified.
8828 @xref{Recursion, ,Recursive Use of @code{make}}.
8829
8830 @item
8831 Support for suffix @samp{.a} in suffix rules.  @xref{Archive Suffix
8832 Rules}.  This feature is obsolete in GNU @code{make}, because the
8833 general feature of rule chaining (@pxref{Chained Rules, ,Chains of
8834 Implicit Rules}) allows one pattern rule for installing members in an
8835 archive (@pxref{Archive Update}) to be sufficient.
8836
8837 @item
8838 The arrangement of lines and backslash-newline combinations in
8839 commands is retained when the commands are printed, so they appear as
8840 they do in the makefile, except for the stripping of initial
8841 whitespace.
8842 @end itemize
8843
8844 The following features were inspired by various other versions of
8845 @code{make}.  In some cases it is unclear exactly which versions inspired
8846 which others.
8847
8848 @itemize @bullet
8849 @item
8850 Pattern rules using @samp{%}.
8851 This has been implemented in several versions of @code{make}.
8852 We're not sure who invented it first, but it's been spread around a bit.
8853 @xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.@refill
8854
8855 @item
8856 Rule chaining and implicit intermediate files.
8857 This was implemented by Stu Feldman in his version of @code{make}
8858 for AT&T Eighth Edition Research Unix, and later by Andrew Hume of
8859 AT&T Bell Labs in his @code{mk} program (where he terms it
8860 ``transitive closure'').  We do not really know if
8861 we got this from either of them or thought it up ourselves at the
8862 same time.  @xref{Chained Rules, ,Chains of Implicit Rules}.
8863
8864 @item
8865 The automatic variable @code{$^} containing a list of all prerequisites
8866 of the current target.  We did not invent this, but we have no idea who
8867 did.  @xref{Automatic, ,Automatic Variables}.  The automatic variable
8868 @code{$+} is a simple extension of @code{$^}.
8869
8870 @item
8871 The ``what if'' flag (@samp{-W} in GNU @code{make}) was (as far as we know)
8872 invented by Andrew Hume in @code{mk}.
8873 @xref{Instead of Execution, ,Instead of Executing the Commands}.
8874
8875 @item
8876 The concept of doing several things at once (parallelism) exists in
8877 many incarnations of @code{make} and similar programs, though not in the
8878 System V or BSD implementations.  @xref{Execution, ,Command Execution}.
8879
8880 @item
8881 Modified variable references using pattern substitution come from
8882 SunOS 4.  @xref{Reference, ,Basics of Variable References}.
8883 This functionality was provided in GNU @code{make} by the
8884 @code{patsubst} function before the alternate syntax was implemented
8885 for compatibility with SunOS 4.  It is not altogether clear who
8886 inspired whom, since GNU @code{make} had @code{patsubst} before SunOS
8887 4 was released.@refill
8888
8889 @item
8890 The special significance of @samp{+} characters preceding command lines
8891 (@pxref{Instead of Execution, ,Instead of Executing the Commands}) is
8892 mandated by
8893 @cite{IEEE Standard 1003.2-1992} (POSIX.2).
8894
8895 @item
8896 The @samp{+=} syntax to append to the value of a variable comes from SunOS
8897 4 @code{make}.  @xref{Appending, , Appending More Text to Variables}.
8898
8899 @item
8900 The syntax @w{@samp{@var{archive}(@var{mem1} @var{mem2}@dots{})}} to list
8901 multiple members in a single archive file comes from SunOS 4 @code{make}.
8902 @xref{Archive Members}.
8903
8904 @item
8905 The @code{-include} directive to include makefiles with no error for a
8906 nonexistent file comes from SunOS 4 @code{make}.  (But note that SunOS 4
8907 @code{make} does not allow multiple makefiles to be specified in one
8908 @code{-include} directive.)  The same feature appears with the name
8909 @code{sinclude} in SGI @code{make} and perhaps others.
8910 @end itemize
8911
8912 The remaining features are inventions new in GNU @code{make}:
8913
8914 @itemize @bullet
8915 @item
8916 Use the @samp{-v} or @samp{--version} option to print version and
8917 copyright information.
8918
8919 @item
8920 Use the @samp{-h} or @samp{--help} option to summarize the options to
8921 @code{make}.
8922
8923 @item
8924 Simply-expanded variables.  @xref{Flavors, ,The Two Flavors of Variables}.
8925
8926 @item
8927 Pass command-line variable assignments automatically through the
8928 variable @code{MAKE} to recursive @code{make} invocations.
8929 @xref{Recursion, ,Recursive Use of @code{make}}.
8930
8931 @item
8932 Use the @samp{-C} or @samp{--directory} command option to change
8933 directory.  @xref{Options Summary, ,Summary of Options}.
8934
8935 @item
8936 Make verbatim variable definitions with @code{define}.
8937 @xref{Defining, ,Defining Variables Verbatim}.
8938
8939 @item
8940 Declare phony targets with the special target @code{.PHONY}.
8941
8942 Andrew Hume of AT&T Bell Labs implemented a similar feature with a
8943 different syntax in his @code{mk} program.  This seems to be a case of
8944 parallel discovery.  @xref{Phony Targets, ,Phony Targets}.
8945
8946 @item
8947 Manipulate text by calling functions.
8948 @xref{Functions, ,Functions for Transforming Text}.
8949
8950 @item
8951 Use the @samp{-o} or @samp{--old-file}
8952 option to pretend a file's modification-time is old.
8953 @xref{Avoiding Compilation, ,Avoiding Recompilation of Some Files}.
8954
8955 @item
8956 Conditional execution.
8957
8958 This feature has been implemented numerous times in various versions
8959 of @code{make}; it seems a natural extension derived from the features
8960 of the C preprocessor and similar macro languages and is not a
8961 revolutionary concept.  @xref{Conditionals, ,Conditional Parts of Makefiles}.
8962
8963 @item
8964 Specify a search path for included makefiles.
8965 @xref{Include, ,Including Other Makefiles}.
8966
8967 @item
8968 Specify extra makefiles to read with an environment variable.
8969 @xref{MAKEFILES Variable, ,The Variable @code{MAKEFILES}}.
8970
8971 @item
8972 Strip leading sequences of @samp{./} from file names, so that
8973 @file{./@var{file}} and @file{@var{file}} are considered to be the
8974 same file.@refill
8975
8976 @item
8977 Use a special search method for library prerequisites written in the
8978 form @samp{-l@var{name}}.
8979 @xref{Libraries/Search, ,Directory Search for Link Libraries}.
8980
8981 @item
8982 Allow suffixes for suffix rules
8983 (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}) to contain any
8984 characters.  In other versions of @code{make}, they must begin with
8985 @samp{.} and not contain any @samp{/} characters.
8986
8987 @item
8988 Keep track of the current level of @code{make} recursion using the
8989 variable @code{MAKELEVEL}.  @xref{Recursion, ,Recursive Use of @code{make}}.
8990
8991 @item
8992 Provide any goals given on the command line in the variable
8993 @code{MAKECMDGOALS}.  @xref{Goals, ,Arguments to Specify the Goals}.
8994
8995 @item
8996 Specify static pattern rules.  @xref{Static Pattern, ,Static Pattern Rules}.
8997
8998 @item
8999 Provide selective @code{vpath} search.
9000 @xref{Directory Search, ,Searching Directories for Prerequisites}.
9001
9002 @item
9003 Provide computed variable references.
9004 @xref{Reference, ,Basics of Variable References}.
9005
9006 @item
9007 Update makefiles.  @xref{Remaking Makefiles, ,How Makefiles Are Remade}.
9008 System V @code{make} has a very, very limited form of this
9009 functionality in that it will check out SCCS files for makefiles.
9010
9011 @item
9012 Various new built-in implicit rules.
9013 @xref{Catalogue of Rules, ,Catalogue of Implicit Rules}.
9014
9015 @item
9016 The built-in variable @samp{MAKE_VERSION} gives the version number of
9017 @code{make}.
9018 @end itemize
9019
9020 @node Missing, Makefile Conventions, Features, Top
9021 @chapter Incompatibilities and Missing Features
9022 @cindex incompatibilities
9023 @cindex missing features
9024 @cindex features, missing
9025
9026 The @code{make} programs in various other systems support a few features
9027 that are not implemented in GNU @code{make}.  The POSIX.2 standard
9028 (@cite{IEEE Standard 1003.2-1992}) which specifies @code{make} does not
9029 require any of these features.@refill
9030
9031 @itemize @bullet
9032 @item
9033 A target of the form @samp{@var{file}((@var{entry}))} stands for a member
9034 of archive file @var{file}.  The member is chosen, not by name, but by
9035 being an object file which defines the linker symbol @var{entry}.@refill
9036
9037 This feature was not put into GNU @code{make} because of the
9038 nonmodularity of putting knowledge into @code{make} of the internal
9039 format of archive file symbol tables.
9040 @xref{Archive Symbols, ,Updating Archive Symbol Directories}.
9041
9042 @item
9043 Suffixes (used in suffix rules) that end with the character @samp{~}
9044 have a special meaning to System V @code{make};
9045 they refer to the SCCS file that corresponds
9046 to the file one would get without the @samp{~}.  For example, the
9047 suffix rule @samp{.c~.o} would make the file @file{@var{n}.o} from
9048 the SCCS file @file{s.@var{n}.c}.  For complete coverage, a whole
9049 series of such suffix rules is required.
9050 @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
9051
9052 In GNU @code{make}, this entire series of cases is handled by two
9053 pattern rules for extraction from SCCS, in combination with the
9054 general feature of rule chaining.
9055 @xref{Chained Rules, ,Chains of Implicit Rules}.
9056
9057 @item
9058 In System V @code{make}, the string @samp{$$@@} has the strange meaning
9059 that, in the prerequisites of a rule with multiple targets, it stands
9060 for the particular target that is being processed.
9061
9062 This is not defined in GNU @code{make} because @samp{$$} should always
9063 stand for an ordinary @samp{$}.
9064
9065 It is possible to get portions of this functionality through the use of
9066 static pattern rules (@pxref{Static Pattern, ,Static Pattern Rules}).
9067 The System V @code{make} rule:
9068
9069 @example
9070 $(targets): $$@@.o lib.a
9071 @end example
9072
9073 @noindent
9074 can be replaced with the GNU @code{make} static pattern rule:
9075
9076 @example
9077 $(targets): %: %.o lib.a
9078 @end example
9079
9080 @item
9081 In System V and 4.3 BSD @code{make}, files found by @code{VPATH} search
9082 (@pxref{Directory Search, ,Searching Directories for Prerequisites}) have their names changed inside command
9083 strings.  We feel it is much cleaner to always use automatic variables
9084 and thus make this feature obsolete.@refill
9085
9086 @item
9087 In some Unix @code{make}s, the automatic variable @code{$*} appearing in
9088 the prerequisites of a rule has the amazingly strange ``feature'' of
9089 expanding to the full name of the @emph{target of that rule}.  We cannot
9090 imagine what went on in the minds of Unix @code{make} developers to do
9091 this; it is utterly inconsistent with the normal definition of @code{$*}.
9092 @vindex * @r{(automatic variable), unsupported bizarre usage}
9093
9094 @item
9095 In some Unix @code{make}s, implicit rule search
9096 (@pxref{Implicit Rules, ,Using Implicit Rules}) is apparently done for
9097 @emph{all} targets, not just those without commands.  This means you can
9098 do:@refill
9099
9100 @example
9101 @group
9102 foo.o:
9103         cc -c foo.c
9104 @end group
9105 @end example
9106
9107 @noindent
9108 and Unix @code{make} will intuit that @file{foo.o} depends on
9109 @file{foo.c}.@refill
9110
9111 We feel that such usage is broken.  The prerequisite properties of
9112 @code{make} are well-defined (for GNU @code{make}, at least),
9113 and doing such a thing simply does not fit the model.@refill
9114
9115 @item
9116 GNU @code{make} does not include any built-in implicit rules for
9117 compiling or preprocessing EFL programs.  If we hear of anyone who is
9118 using EFL, we will gladly add them.
9119
9120 @item
9121 It appears that in SVR4 @code{make}, a suffix rule can be specified with
9122 no commands, and it is treated as if it had empty commands
9123 (@pxref{Empty Commands}).  For example:
9124
9125 @example
9126 .c.a:
9127 @end example
9128
9129 @noindent
9130 will override the built-in @file{.c.a} suffix rule.
9131
9132 We feel that it is cleaner for a rule without commands to always simply
9133 add to the prerequisite list for the target.  The above example can be
9134 easily rewritten to get the desired behavior in GNU @code{make}:
9135
9136 @example
9137 .c.a: ;
9138 @end example
9139
9140 @item
9141 Some versions of @code{make} invoke the shell with the @samp{-e} flag,
9142 except under @samp{-k} (@pxref{Testing, ,Testing the Compilation of a
9143 Program}).  The @samp{-e} flag tells the shell to exit as soon as any
9144 program it runs returns a nonzero status.  We feel it is cleaner to
9145 write each shell command line to stand on its own and not require this
9146 special treatment.
9147 @end itemize
9148
9149 @comment The makefile standards are in a separate file that is also
9150 @comment included by standards.texi.
9151 @include make-stds.texi
9152
9153 @node Quick Reference, Error Messages, Makefile Conventions, Top
9154 @appendix Quick Reference
9155
9156 This appendix summarizes the directives, text manipulation functions,
9157 and special variables which GNU @code{make} understands.
9158 @xref{Special Targets}, @ref{Catalogue of Rules, ,Catalogue of Implicit Rules},
9159 and @ref{Options Summary, ,Summary of Options},
9160 for other summaries.
9161
9162 Here is a summary of the directives GNU @code{make} recognizes:
9163
9164 @table @code
9165 @item define @var{variable}
9166 @itemx endef
9167
9168 Define a multi-line, recursively-expanded variable.@*
9169 @xref{Sequences}.
9170
9171 @item ifdef @var{variable}
9172 @itemx ifndef @var{variable}
9173 @itemx ifeq (@var{a},@var{b})
9174 @itemx ifeq "@var{a}" "@var{b}"
9175 @itemx ifeq '@var{a}' '@var{b}'
9176 @itemx ifneq (@var{a},@var{b})
9177 @itemx ifneq "@var{a}" "@var{b}"
9178 @itemx ifneq '@var{a}' '@var{b}'
9179 @itemx else
9180 @itemx endif
9181
9182 Conditionally evaluate part of the makefile.@*
9183 @xref{Conditionals}.
9184
9185 @item include @var{file}
9186 @itemx -include @var{file}
9187 @itemx sinclude @var{file}
9188
9189 Include another makefile.@*
9190 @xref{Include, ,Including Other Makefiles}.
9191
9192 @item override @var{variable} = @var{value}
9193 @itemx override @var{variable} := @var{value}
9194 @itemx override @var{variable} += @var{value}
9195 @itemx override @var{variable} ?= @var{value}
9196 @itemx override define @var{variable}
9197 @itemx endef
9198
9199 Define a variable, overriding any previous definition, even one from
9200 the command line.@*
9201 @xref{Override Directive, ,The @code{override} Directive}.
9202
9203 @item export
9204
9205 Tell @code{make} to export all variables to child processes by default.@*
9206 @xref{Variables/Recursion, , Communicating Variables to a Sub-@code{make}}.
9207
9208 @item export @var{variable}
9209 @itemx export @var{variable} = @var{value}
9210 @itemx export @var{variable} := @var{value}
9211 @itemx export @var{variable} += @var{value}
9212 @itemx export @var{variable} ?= @var{value}
9213 @itemx unexport @var{variable}
9214 Tell @code{make} whether or not to export a particular variable to child
9215 processes.@*
9216 @xref{Variables/Recursion, , Communicating Variables to a Sub-@code{make}}.
9217
9218 @item vpath @var{pattern} @var{path}
9219 Specify a search path for files matching a @samp{%} pattern.@*
9220 @xref{Selective Search, , The @code{vpath} Directive}.
9221
9222 @item vpath @var{pattern}
9223 Remove all search paths previously specified for @var{pattern}.
9224
9225 @item vpath
9226 Remove all search paths previously specified in any @code{vpath}
9227 directive.
9228 @end table
9229
9230 Here is a summary of the text manipulation functions (@pxref{Functions}):
9231
9232 @table @code
9233 @item $(subst @var{from},@var{to},@var{text})
9234 Replace @var{from} with @var{to} in @var{text}.@*
9235 @xref{Text Functions, , Functions for String Substitution and Analysis}.
9236
9237 @item $(patsubst @var{pattern},@var{replacement},@var{text})
9238 Replace words matching @var{pattern} with @var{replacement} in @var{text}.@*
9239 @xref{Text Functions, , Functions for String Substitution and Analysis}.
9240
9241 @item $(strip @var{string})
9242 Remove excess whitespace characters from @var{string}.@*
9243 @xref{Text Functions, , Functions for String Substitution and Analysis}.
9244
9245 @item $(findstring @var{find},@var{text})
9246 Locate @var{find} in @var{text}.@*
9247 @xref{Text Functions, , Functions for String Substitution and Analysis}.
9248
9249 @item $(filter @var{pattern}@dots{},@var{text})
9250 Select words in @var{text} that match one of the @var{pattern} words.@*
9251 @xref{Text Functions, , Functions for String Substitution and Analysis}.
9252
9253 @item $(filter-out @var{pattern}@dots{},@var{text})
9254 Select words in @var{text} that @emph{do not} match any of the @var{pattern} words.@*
9255 @xref{Text Functions, , Functions for String Substitution and Analysis}.
9256
9257 @item $(sort @var{list})
9258 Sort the words in @var{list} lexicographically, removing duplicates.@*
9259 @xref{Text Functions, , Functions for String Substitution and Analysis}.
9260
9261 @item $(dir @var{names}@dots{})
9262 Extract the directory part of each file name.@*
9263 @xref{File Name Functions, ,Functions for File Names}.
9264
9265 @item $(notdir @var{names}@dots{})
9266 Extract the non-directory part of each file name.@*
9267 @xref{File Name Functions, ,Functions for File Names}.
9268
9269 @item $(suffix @var{names}@dots{})
9270 Extract the suffix (the last @samp{.} and following characters) of each file name.@*
9271 @xref{File Name Functions, ,Functions for File Names}.
9272
9273 @item $(basename @var{names}@dots{})
9274 Extract the base name (name without suffix) of each file name.@*
9275 @xref{File Name Functions, ,Functions for File Names}.
9276
9277 @item $(addsuffix @var{suffix},@var{names}@dots{})
9278 Append @var{suffix} to each word in @var{names}.@*
9279 @xref{File Name Functions, ,Functions for File Names}.
9280
9281 @item $(addprefix @var{prefix},@var{names}@dots{})
9282 Prepend @var{prefix} to each word in @var{names}.@*
9283 @xref{File Name Functions, ,Functions for File Names}.
9284
9285 @item $(join @var{list1},@var{list2})
9286 Join two parallel lists of words.@*
9287 @xref{File Name Functions, ,Functions for File Names}.
9288
9289 @item $(word @var{n},@var{text})
9290 Extract the @var{n}th word (one-origin) of @var{text}.@*
9291 @xref{File Name Functions, ,Functions for File Names}.
9292
9293 @item $(words @var{text})
9294 Count the number of words in @var{text}.@*
9295 @xref{File Name Functions, ,Functions for File Names}.
9296
9297 @item $(wordlist @var{s},@var{e},@var{text})
9298 Returns the list of words in @var{text} from @var{s} to @var{e}.@*
9299 @xref{File Name Functions, ,Functions for File Names}.
9300
9301 @item $(firstword @var{names}@dots{})
9302 Extract the first word of @var{names}.@*
9303 @xref{File Name Functions, ,Functions for File Names}.
9304
9305 @item $(wildcard @var{pattern}@dots{})
9306 Find file names matching a shell file name pattern (@emph{not} a
9307 @samp{%} pattern).@*
9308 @xref{Wildcard Function, ,The Function @code{wildcard}}.
9309
9310 @item $(error @var{text}@dots{})
9311
9312 When this function is evaluated, @code{make} generates a fatal error
9313 with the message @var{text}.@*
9314 @xref{Make Control Functions, ,Functions That Control Make}.
9315
9316 @item $(warning @var{text}@dots{})
9317
9318 When this function is evaluated, @code{make} generates a warning with
9319 the message @var{text}.@*
9320 @xref{Make Control Functions, ,Functions That Control Make}.
9321
9322 @item $(shell @var{command})
9323
9324 Execute a shell command and return its output.@*
9325 @xref{Shell Function, , The @code{shell} Function}.
9326
9327 @item $(origin @var{variable})
9328
9329 Return a string describing how the @code{make} variable @var{variable} was
9330 defined.@*
9331 @xref{Origin Function, , The @code{origin} Function}.
9332
9333 @item $(foreach @var{var},@var{words},@var{text})
9334
9335 Evaluate @var{text} with @var{var} bound to each word in @var{words},
9336 and concatenate the results.@*
9337 @xref{Foreach Function, ,The @code{foreach} Function}.
9338
9339 @item $(call @var{var},@var{param},@dots{})
9340
9341 Evaluate the variable @var{var} replacing any references to @code{$(1)},
9342 @code{$(2)} with the first, second, etc. @var{param} values.@*
9343 @xref{Call Function, ,The @code{call} Function}.
9344 @end table
9345
9346 Here is a summary of the automatic variables.
9347 @xref{Automatic, ,Automatic Variables},
9348 for full information.
9349
9350 @table @code
9351 @item $@@
9352 The file name of the target.
9353
9354 @item $%
9355 The target member name, when the target is an archive member.
9356
9357 @item $<
9358 The name of the first prerequisite.
9359
9360 @item $?
9361 The names of all the prerequisites that are
9362 newer than the target, with spaces between them.
9363 For prerequisites which are archive members, only
9364 the member named is used (@pxref{Archives}).
9365
9366 @item $^
9367 @itemx $+
9368 The names of all the prerequisites, with spaces between them.  For
9369 prerequisites which are archive members, only the member named is used
9370 (@pxref{Archives}).  The value of @code{$^} omits duplicate
9371 prerequisites, while @code{$+} retains them and preserves their order.
9372
9373 @item $*
9374 The stem with which an implicit rule matches
9375 (@pxref{Pattern Match, ,How Patterns Match}).
9376
9377 @item $(@@D)
9378 @itemx $(@@F)
9379 The directory part and the file-within-directory part of @code{$@@}.
9380
9381 @item $(*D)
9382 @itemx $(*F)
9383 The directory part and the file-within-directory part of @code{$*}.
9384
9385 @item $(%D)
9386 @itemx $(%F)
9387 The directory part and the file-within-directory part of @code{$%}.
9388
9389 @item $(<D)
9390 @itemx $(<F)
9391 The directory part and the file-within-directory part of @code{$<}.
9392
9393 @item $(^D)
9394 @itemx $(^F)
9395 The directory part and the file-within-directory part of @code{$^}.
9396
9397 @item $(+D)
9398 @itemx $(+F)
9399 The directory part and the file-within-directory part of @code{$+}.
9400
9401 @item $(?D)
9402 @itemx $(?F)
9403 The directory part and the file-within-directory part of @code{$?}.
9404 @end table
9405
9406 These variables are used specially by GNU @code{make}:
9407
9408 @table @code
9409 @item MAKEFILES
9410
9411 Makefiles to be read on every invocation of @code{make}.@*
9412 @xref{MAKEFILES Variable, ,The Variable @code{MAKEFILES}}.
9413
9414 @item VPATH
9415
9416 Directory search path for files not found in the current directory.@*
9417 @xref{General Search, , @code{VPATH} Search Path for All Prerequisites}.
9418
9419 @item SHELL
9420
9421 The name of the system default command interpreter, usually @file{/bin/sh}.
9422 You can set @code{SHELL} in the makefile to change the shell used to run
9423 commands.  @xref{Execution, ,Command Execution}.
9424
9425 @item MAKESHELL
9426
9427 On MS-DOS only, the name of the command interpreter that is to be used
9428 by @code{make}. This value takes precedence over the value of
9429 @code{SHELL}.  @xref{Execution, ,MAKESHELL variable}.
9430
9431 @item MAKE
9432
9433 The name with which @code{make} was invoked.
9434 Using this variable in commands has special meaning.
9435 @xref{MAKE Variable, ,How the @code{MAKE} Variable Works}.
9436
9437 @item MAKELEVEL
9438
9439 The number of levels of recursion (sub-@code{make}s).@*
9440 @xref{Variables/Recursion}.
9441
9442 @item MAKEFLAGS
9443
9444 The flags given to @code{make}.  You can set this in the environment or
9445 a makefile to set flags.@*
9446 @xref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
9447
9448 It is @emph{never} appropriate to use @code{MAKEFLAGS} directly on a
9449 command line: its contents may not be quoted correctly for use in the
9450 shell.  Always allow recursive @code{make}'s to obtain these values
9451 through the environment from its parent.
9452
9453 @item MAKECMDGOALS
9454
9455 The targets given to @code{make} on the command line.  Setting this
9456 variable has no effect on the operation of @code{make}.@*
9457 @xref{Goals, ,Arguments to Specify the Goals}.
9458
9459 @item CURDIR
9460
9461 Set to the pathname of the current working directory (after all
9462 @code{-C} options are processed, if any).  Setting this variable has no
9463 effect on the operation of @code{make}.@*
9464 @xref{Recursion, ,Recursive Use of @code{make}}.
9465
9466 @item SUFFIXES
9467
9468 The default list of suffixes before @code{make} reads any makefiles.
9469
9470 @item .LIBPATTERNS
9471 Defines the naming of the libraries @code{make} searches for, and their
9472 order.@*
9473 @xref{Libraries/Search, ,Directory Search for Link Libraries}.
9474 @end table
9475
9476 @node Error Messages, Complex Makefile, Quick Reference, Top
9477 @comment  node-name,  next,  previous,  up
9478 @appendix Errors Generated by Make
9479
9480 Here is a list of the more common errors you might see generated by
9481 @code{make}, and some information about what they mean and how to fix
9482 them.
9483
9484 Sometimes @code{make} errors are not fatal, especially in the presence
9485 of a @code{-} prefix on a command script line, or the @code{-k} command
9486 line option.  Errors that are fatal are prefixed with the string
9487 @code{***}.
9488
9489 Error messages are all either prefixed with the name of the program
9490 (usually @samp{make}), or, if the error is found in a makefile, the name
9491 of the file and linenumber containing the problem.
9492
9493 In the table below, these common prefixes are left off.
9494
9495 @table @samp
9496
9497 @item [@var{foo}] Error @var{NN}
9498 @itemx [@var{foo}] @var{signal description}
9499 These errors are not really @code{make} errors at all.  They mean that a
9500 program that @code{make} invoked as part of a command script returned a
9501 non-0 error code (@samp{Error @var{NN}}), which @code{make} interprets
9502 as failure, or it exited in some other abnormal fashion (with a
9503 signal of some type).  @xref{Errors, ,Errors in Commands}.
9504
9505 If no @code{***} is attached to the message, then the subprocess failed
9506 but the rule in the makefile was prefixed with the @code{-} special
9507 character, so @code{make} ignored the error.
9508
9509 @item missing separator.  Stop.
9510 @itemx missing separator (did you mean TAB instead of 8 spaces?).  Stop.
9511 This means that @code{make} could not understand much of anything about
9512 the command line it just read.  GNU @code{make} looks for various kinds
9513 of separators (@code{:}, @code{=}, TAB characters, etc.) to help it
9514 decide what kind of commandline it's seeing.  This means it couldn't
9515 find a valid one.
9516
9517 One of the most common reasons for this message is that you (or perhaps
9518 your oh-so-helpful editor, as is the case with many MS-Windows editors)
9519 have attempted to indent your command scripts with spaces instead of a
9520 TAB character.  In this case, @code{make} will use the second form of
9521 the error above.  Remember that every line in the command script must
9522 begin with a TAB character.  Eight spaces do not count.  @xref{Rule
9523 Syntax}.
9524
9525 @item commands commence before first target.  Stop.
9526 @itemx missing rule before commands.  Stop.
9527 This means the first thing in the makefile seems to be part of a command
9528 script: it begins with a TAB character and doesn't appear to be a legal
9529 @code{make} command (such as a variable assignment).  Command scripts
9530 must always be associated with a target.
9531
9532 The second form is generated if the line has a semicolon as the first
9533 non-whitespace character; @code{make} interprets this to mean you left
9534 out the "target: prerequisite" section of a rule.  @xref{Rule Syntax}.
9535
9536 @item No rule to make target `@var{xxx}'.
9537 @itemx No rule to make target `@var{xxx}', needed by `@var{yyy}'.
9538 This means that @code{make} decided it needed to build a target, but
9539 then couldn't find any instructions in the makefile on how to do that,
9540 either explicit or implicit (including in the default rules database).
9541
9542 If you want that file to be built, you will need to add a rule to your
9543 makefile describing how that target can be built.  Other possible
9544 sources of this problem are typos in the makefile (if that filename is
9545 wrong) or a corrupted source tree (if that file is not supposed to be
9546 built, but rather only a prerequisite).
9547
9548 @item No targets specified and no makefile found.  Stop.
9549 @itemx No targets.  Stop.
9550 The former means that you didn't provide any targets to be built on the
9551 command line, and @code{make} couldn't find any makefiles to read in.
9552 The latter means that some makefile was found, but it didn't contain any
9553 default target and none was given on the command line.  GNU @code{make}
9554 has nothing to do in these situations.
9555 @xref{Makefile Arguments, ,Arguments to Specify the Makefile}.@refill
9556
9557 @item Makefile `@var{xxx}' was not found.
9558 @itemx Included makefile `@var{xxx}' was not found.
9559 A makefile specified on the command line (first form) or included
9560 (second form) was not found.
9561
9562 @item warning: overriding commands for target `@var{xxx}'
9563 @itemx warning: ignoring old commands for target `@var{xxx}'
9564 GNU @code{make} allows commands to be specified only once per target
9565 (except for double-colon rules).  If you give commands for a target
9566 which already has been defined to have commands, this warning is issued
9567 and the second set of commands will overwrite the first set.
9568 @xref{Multiple Rules, ,Multiple Rules for One Target}.
9569
9570 @item Circular @var{xxx} <- @var{yyy} dependency dropped.
9571 This means that @code{make} detected a loop in the dependency graph:
9572 after tracing the prerequisite @var{yyy} of target @var{xxx}, and its
9573 prerequisites, etc., one of them depended on @var{xxx} again.
9574
9575 @item Recursive variable `@var{xxx}' references itself (eventually).  Stop.
9576 This means you've defined a normal (recursive) @code{make} variable
9577 @var{xxx} that, when it's expanded, will refer to itself (@var{xxx}).
9578 This is not allowed; either use simply-expanded variables (@code{:=}) or
9579 use the append operator (@code{+=}).  @xref{Using Variables, ,How to Use
9580 Variables}.
9581
9582 @item Unterminated variable reference.  Stop.
9583 This means you forgot to provide the proper closing parenthesis
9584 or brace in your variable or function reference.
9585
9586 @item insufficient arguments to function `@var{xxx}'.  Stop.
9587 This means you haven't provided the requisite number of arguments for
9588 this function.  See the documentation of the function for a description
9589 of its arguments.  @xref{Functions, ,Functions for Transforming Text}.
9590
9591 @item missing target pattern.  Stop.
9592 @itemx multiple target patterns.  Stop.
9593 @itemx target pattern contains no `%'.  Stop.
9594 These are generated for malformed static pattern rules.  The first means
9595 there's no pattern in the target section of the rule, the second means
9596 there are multiple patterns in the target section, and the third means
9597 the target doesn't contain a pattern character (@code{%}).  @xref{Static
9598 Usage, ,Syntax of Static Pattern Rules}.
9599
9600 @item warning: -jN forced in submake: disabling jobserver mode.
9601 This warning and the next are generated if @code{make} detects error
9602 conditions related to parallel processing on systems where
9603 sub-@code{make}s can communicate (@pxref{Options/Recursion,
9604 ,Communicating Options to a Sub-@code{make}}).  This warning is
9605 generated if a recursive invocation of a @code{make} process is forced
9606 to have @samp{-j@var{N}} in its argument list (where @var{N} is greater
9607 than one).  This could happen, for example, if you set the @code{MAKE}
9608 environment variable to @samp{make -j2}.  In this case, the
9609 sub-@code{make} doesn't communicate with other @code{make} processes and
9610 will simply pretend it has two jobs of its own.
9611
9612 @item warning: jobserver unavailable: using -j1.  Add `+' to parent make rule.
9613 In order for @code{make} processes to communicate, the parent will pass
9614 information to the child.  Since this could result in problems if the
9615 child process isn't actually a @code{make}, the parent will only do this
9616 if it thinks the child is a @code{make}.  The parent uses the normal
9617 algorithms to determine this (@pxref{MAKE Variable, ,How the @code{MAKE}
9618 Variable Works}).  If the makefile is constructed such that the parent
9619 doesn't know the child is a @code{make} process, then the child will
9620 receive only part of the information necessary.  In this case, the child
9621 will generate this warning message and proceed with its build in a
9622 sequential manner.
9623
9624 @end table
9625
9626 @node Complex Makefile, Concept Index, Error Messages, Top
9627 @appendix Complex Makefile Example
9628
9629 Here is the makefile for the GNU @code{tar} program.  This is a
9630 moderately complex makefile.
9631
9632 Because it is the first target, the default goal is @samp{all}.  An
9633 interesting feature of this makefile is that @file{testpad.h} is a
9634 source file automatically created by the @code{testpad} program,
9635 itself compiled from @file{testpad.c}.
9636
9637 If you type @samp{make} or @samp{make all}, then @code{make} creates
9638 the @file{tar} executable, the @file{rmt} daemon that provides
9639 remote tape access, and the @file{tar.info} Info file.
9640
9641 If you type @samp{make install}, then @code{make} not only creates
9642 @file{tar}, @file{rmt}, and @file{tar.info}, but also installs
9643 them.
9644
9645 If you type @samp{make clean}, then @code{make} removes the @samp{.o}
9646 files, and the @file{tar}, @file{rmt}, @file{testpad},
9647 @file{testpad.h}, and @file{core} files.
9648
9649 If you type @samp{make distclean}, then @code{make} not only removes
9650 the same files as does @samp{make clean} but also the
9651 @file{TAGS}, @file{Makefile}, and @file{config.status} files.
9652 (Although it is not evident, this makefile (and
9653 @file{config.status}) is generated by the user with the
9654 @code{configure} program, which is provided in the @code{tar}
9655 distribution, but is not shown here.)
9656
9657 If you type @samp{make realclean}, then @code{make} removes the same
9658 files as does @samp{make distclean} and also removes the Info files
9659 generated from @file{tar.texinfo}.
9660
9661 In addition, there are targets @code{shar} and @code{dist} that create
9662 distribution kits.
9663
9664 @example
9665 @group
9666 # Generated automatically from Makefile.in by configure.
9667 # Un*x Makefile for GNU tar program.
9668 # Copyright (C) 1991 Free Software Foundation, Inc.
9669 @end group
9670
9671 @group
9672 # This program is free software; you can redistribute
9673 # it and/or modify it under the terms of the GNU
9674 # General Public License @dots{}
9675 @dots{}
9676 @dots{}
9677 @end group
9678
9679 SHELL = /bin/sh
9680
9681 #### Start of system configuration section. ####
9682
9683 srcdir = .
9684
9685 @group
9686 # If you use gcc, you should either run the
9687 # fixincludes script that comes with it or else use
9688 # gcc with the -traditional option.  Otherwise ioctl
9689 # calls will be compiled incorrectly on some systems.
9690 CC = gcc -O
9691 YACC = bison -y
9692 INSTALL = /usr/local/bin/install -c
9693 INSTALLDATA = /usr/local/bin/install -c -m 644
9694 @end group
9695
9696 # Things you might add to DEFS:
9697 # -DSTDC_HEADERS        If you have ANSI C headers and
9698 #                       libraries.
9699 # -DPOSIX               If you have POSIX.1 headers and
9700 #                       libraries.
9701 # -DBSD42               If you have sys/dir.h (unless
9702 #                       you use -DPOSIX), sys/file.h,
9703 #                       and st_blocks in `struct stat'.
9704 # -DUSG                 If you have System V/ANSI C
9705 #                       string and memory functions
9706 #                       and headers, sys/sysmacros.h,
9707 #                       fcntl.h, getcwd, no valloc,
9708 #                       and ndir.h (unless
9709 #                       you use -DDIRENT).
9710 # -DNO_MEMORY_H         If USG or STDC_HEADERS but do not
9711 #                       include memory.h.
9712 # -DDIRENT              If USG and you have dirent.h
9713 #                       instead of ndir.h.
9714 # -DSIGTYPE=int         If your signal handlers
9715 #                       return int, not void.
9716 # -DNO_MTIO             If you lack sys/mtio.h
9717 #                       (magtape ioctls).
9718 # -DNO_REMOTE           If you do not have a remote shell
9719 #                       or rexec.
9720 # -DUSE_REXEC           To use rexec for remote tape
9721 #                       operations instead of
9722 #                       forking rsh or remsh.
9723 # -DVPRINTF_MISSING     If you lack vprintf function
9724 #                       (but have _doprnt).
9725 # -DDOPRNT_MISSING      If you lack _doprnt function.
9726 #                       Also need to define
9727 #                       -DVPRINTF_MISSING.
9728 # -DFTIME_MISSING       If you lack ftime system call.
9729 # -DSTRSTR_MISSING      If you lack strstr function.
9730 # -DVALLOC_MISSING      If you lack valloc function.
9731 # -DMKDIR_MISSING       If you lack mkdir and
9732 #                       rmdir system calls.
9733 # -DRENAME_MISSING      If you lack rename system call.
9734 # -DFTRUNCATE_MISSING   If you lack ftruncate
9735 #                       system call.
9736 # -DV7                  On Version 7 Unix (not
9737 #                       tested in a long time).
9738 # -DEMUL_OPEN3          If you lack a 3-argument version
9739 #                       of open, and want to emulate it
9740 #                       with system calls you do have.
9741 # -DNO_OPEN3            If you lack the 3-argument open
9742 #                       and want to disable the tar -k
9743 #                       option instead of emulating open.
9744 # -DXENIX               If you have sys/inode.h
9745 #                       and need it 94 to be included.
9746
9747 DEFS =  -DSIGTYPE=int -DDIRENT -DSTRSTR_MISSING \
9748         -DVPRINTF_MISSING -DBSD42
9749 # Set this to rtapelib.o unless you defined NO_REMOTE,
9750 # in which case make it empty.
9751 RTAPELIB = rtapelib.o
9752 LIBS =
9753 DEF_AR_FILE = /dev/rmt8
9754 DEFBLOCKING = 20
9755
9756 @group
9757 CDEBUG = -g
9758 CFLAGS = $(CDEBUG) -I. -I$(srcdir) $(DEFS) \
9759         -DDEF_AR_FILE=\"$(DEF_AR_FILE)\" \
9760         -DDEFBLOCKING=$(DEFBLOCKING)
9761 LDFLAGS = -g
9762 @end group
9763
9764 @group
9765 prefix = /usr/local
9766 # Prefix for each installed program,
9767 # normally empty or `g'.
9768 binprefix =
9769
9770 # The directory to install tar in.
9771 bindir = $(prefix)/bin
9772
9773 # The directory to install the info files in.
9774 infodir = $(prefix)/info
9775 @end group
9776
9777 #### End of system configuration section. ####
9778
9779 SRC1 =  tar.c create.c extract.c buffer.c \
9780         getoldopt.c update.c gnu.c mangle.c
9781 SRC2 =  version.c list.c names.c diffarch.c \
9782         port.c wildmat.c getopt.c
9783 SRC3 =  getopt1.c regex.c getdate.y
9784 SRCS =  $(SRC1) $(SRC2) $(SRC3)
9785 OBJ1 =  tar.o create.o extract.o buffer.o \
9786         getoldopt.o update.o gnu.o mangle.o
9787 OBJ2 =  version.o list.o names.o diffarch.o \
9788         port.o wildmat.o getopt.o
9789 OBJ3 =  getopt1.o regex.o getdate.o $(RTAPELIB)
9790 OBJS =  $(OBJ1) $(OBJ2) $(OBJ3)
9791 @group
9792 AUX =   README COPYING ChangeLog Makefile.in  \
9793         makefile.pc configure configure.in \
9794         tar.texinfo tar.info* texinfo.tex \
9795         tar.h port.h open3.h getopt.h regex.h \
9796         rmt.h rmt.c rtapelib.c alloca.c \
9797         msd_dir.h msd_dir.c tcexparg.c \
9798         level-0 level-1 backup-specs testpad.c
9799 @end group
9800
9801 all:    tar rmt tar.info
9802
9803 @group
9804 tar:    $(OBJS)
9805         $(CC) $(LDFLAGS) -o $@@ $(OBJS) $(LIBS)
9806 @end group
9807
9808 @group
9809 rmt:    rmt.c
9810         $(CC) $(CFLAGS) $(LDFLAGS) -o $@@ rmt.c
9811 @end group
9812
9813 @group
9814 tar.info: tar.texinfo
9815         makeinfo tar.texinfo
9816 @end group
9817
9818 @group
9819 install: all
9820         $(INSTALL) tar $(bindir)/$(binprefix)tar
9821         -test ! -f rmt || $(INSTALL) rmt /etc/rmt
9822         $(INSTALLDATA) $(srcdir)/tar.info* $(infodir)
9823 @end group
9824
9825 @group
9826 $(OBJS): tar.h port.h testpad.h
9827 regex.o buffer.o tar.o: regex.h
9828 # getdate.y has 8 shift/reduce conflicts.
9829 @end group
9830
9831 @group
9832 testpad.h: testpad
9833         ./testpad
9834 @end group
9835
9836 @group
9837 testpad: testpad.o
9838         $(CC) -o $@@ testpad.o
9839 @end group
9840
9841 @group
9842 TAGS:   $(SRCS)
9843         etags $(SRCS)
9844 @end group
9845
9846 @group
9847 clean:
9848         rm -f *.o tar rmt testpad testpad.h core
9849 @end group
9850
9851 @group
9852 distclean: clean
9853         rm -f TAGS Makefile config.status
9854 @end group
9855
9856 @group
9857 realclean: distclean
9858         rm -f tar.info*
9859 @end group
9860
9861 @group
9862 shar: $(SRCS) $(AUX)
9863         shar $(SRCS) $(AUX) | compress \
9864           > tar-`sed -e '/version_string/!d' \
9865                      -e 's/[^0-9.]*\([0-9.]*\).*/\1/' \
9866                      -e q
9867                      version.c`.shar.Z
9868 @end group
9869
9870 @group
9871 dist: $(SRCS) $(AUX)
9872         echo tar-`sed \
9873              -e '/version_string/!d' \
9874              -e 's/[^0-9.]*\([0-9.]*\).*/\1/' \
9875              -e q
9876              version.c` > .fname
9877         -rm -rf `cat .fname`
9878         mkdir `cat .fname`
9879         ln $(SRCS) $(AUX) `cat .fname`
9880         tar chZf `cat .fname`.tar.Z `cat .fname`
9881         -rm -rf `cat .fname` .fname
9882 @end group
9883
9884 @group
9885 tar.zoo: $(SRCS) $(AUX)
9886         -rm -rf tmp.dir
9887         -mkdir tmp.dir
9888         -rm tar.zoo
9889         for X in $(SRCS) $(AUX) ; do \
9890             echo $$X ; \
9891             sed 's/$$/^M/' $$X \
9892             > tmp.dir/$$X ; done
9893         cd tmp.dir ; zoo aM ../tar.zoo *
9894         -rm -rf tmp.dir
9895 @end group
9896 @end example
9897
9898 @node Concept Index, Name Index, Complex Makefile, Top
9899 @unnumbered Index of Concepts
9900
9901 @printindex cp
9902
9903 @node Name Index,  , Concept Index, Top
9904 @unnumbered Index of Functions, Variables, & Directives
9905
9906 @printindex fn
9907
9908 @summarycontents
9909 @contents
9910 @bye