Add back initial "\" which was accidentally removed a year ago! :-/.
[make.git] / make.texinfo
blobe31c900b45ee985a84ae44e806351611b142c82a
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
8 @c FSF publishers: format makebook.texi instead of using this file directly.
10 @set RCSID $Id: make.texinfo,v 2.194 2002/04/24 21:36:38 psmith Exp $
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
18 @c finalout
20 @c ISPELL CHECK: done, 10 June 1993 --roland
21 @c ISPELL CHECK: done, 2000-06-25 --Martin Buchholz
23 @c Combine the variable and function indices:
24 @syncodeindex vr fn
25 @c Combine the program and concept indices:
26 @syncodeindex pg cp
28 @dircategory GNU Packages
29 @direntry
30 * Make: (make).            Remake files automatically.
31 @end direntry
33 @ifinfo
34 This file documents the GNU Make utility, which determines
35 automatically which pieces of a large program need to be recompiled,
36 and issues the commands to recompile them.
38 This is Edition @value{EDITION}, last updated @value{UPDATED},
39 of @cite{The GNU Make Manual}, for @code{make}, Version @value{VERSION}.
41 Copyright (C) 1988, '89, '90, '91, '92, '93, '94, '95, '96, '97, '98, '99, 2000
42         Free Software Foundation, Inc.
44 Permission is granted to make and distribute verbatim copies of
45 this manual provided the copyright notice and this permission notice
46 are preserved on all copies.
48 @ignore
49 Permission is granted to process this file through TeX and print the
50 results, provided the printed document carries copying permission
51 notice identical to this one except for the removal of this paragraph
52 (this paragraph not being relevant to the printed manual).
54 @end ignore
55 Permission is granted to copy and distribute modified versions of this
56 manual under the conditions for verbatim copying, provided that the entire
57 resulting derived work is distributed under the terms of a permission
58 notice identical to this one.
60 Permission is granted to copy and distribute translations of this manual
61 into another language, under the above conditions for modified versions,
62 except that this permission notice may be stated in a translation approved
63 by the Free Software Foundation.
64 @end ifinfo
66 @iftex
67 @shorttitlepage GNU Make
68 @end iftex
69 @titlepage
70 @title GNU Make
71 @subtitle A Program for Directing Recompilation
72 @subtitle GNU @code{make} Version @value{VERSION}
73 @subtitle @value{UPDATE-MONTH}
74 @author Richard M. Stallman and Roland McGrath
75 @page
76 @vskip 0pt plus 1filll
77 Copyright @copyright{} 1988, '89, '90, '91, '92, '93, '94, '95, '96, '97, '98, '99, 2000
78 Free Software Foundation, Inc.
79 @sp 2
80 Published by the Free Software Foundation @*
81 59 Temple Place -- Suite 330, @*
82 Boston, MA 02111-1307 USA @*
83 ISBN @value{ISBN} @*
85 Maintenance and updates since Version 3.76 by Paul D. Smith.
87 Permission is granted to make and distribute verbatim copies of
88 this manual provided the copyright notice and this permission notice
89 are preserved on all copies.
91 Permission is granted to copy and distribute modified versions of this
92 manual under the conditions for verbatim copying, provided that the entire
93 resulting derived work is distributed under the terms of a permission
94 notice identical to this one.
96 Permission is granted to copy and distribute translations of this manual
97 into another language, under the above conditions for modified versions,
98 except that this permission notice may be stated in a translation approved
99 by the Free Software Foundation.
100 @sp 2
101 Cover art by Etienne Suvasa.
102 @end titlepage
103 @page
105 @ifinfo
106 @node Top, Overview, , (dir)
107 @top Make
109 The GNU @code{make} utility automatically determines which pieces of a
110 large program need to be recompiled, and issues the commands to
111 recompile them.@refill
113 This edition of the @cite{GNU Make Manual},
114 last updated @value{UPDATED},
115 documents GNU @code{make} Version @value{VERSION}.@refill
117 This manual describes @code{make} and contains the following chapters:@refill
118 @end ifinfo
120 @menu
121 * Overview::                    Overview of @code{make}.
122 * Introduction::                An introduction to @code{make}.
123 * Makefiles::                   Makefiles tell @code{make} what to do.
124 * Rules::                       Rules describe when a file must be remade.
125 * Commands::                    Commands say how to remake a file.
126 * Using Variables::             You can use variables to avoid repetition.
127 * Conditionals::                Use or ignore parts of the makefile based
128                                  on the values of variables.
129 * Functions::                   Many powerful ways to manipulate text.
130 * Invoking make: Running.       How to invoke @code{make} on the command line.
131 * Implicit Rules::              Use implicit rules to treat many files alike,
132                                  based on their file names.
133 * Archives::                    How @code{make} can update library archives.
134 * Features::                    Features GNU @code{make} has over other @code{make}s.
135 * Missing::                     What GNU @code{make} lacks from other @code{make}s.
136 * Makefile Conventions::        Conventions for makefiles in GNU programs.
137 * Quick Reference::             A quick reference for experienced users.
138 * Error Messages::              A list of common errors generated by @code{make}.
139 * Complex Makefile::            A real example of a straightforward,
140                                  but nontrivial, makefile.
141 * Concept Index::               Index of Concepts
142 * Name Index::                  Index of Functions, Variables, & Directives
144  --- The Detailed Node Listing ---
146 Overview of @code{make}
148 * Preparing::                   Preparing and Running Make
149 * Reading::                     On Reading this Text
150 * Bugs::                        Problems and Bugs
152 An Introduction to Makefiles
154 * Rule Introduction::           What a rule looks like.
155 * Simple Makefile::             A Simple Makefile
156 * How Make Works::              How @code{make} Processes This Makefile
157 * Variables Simplify::          Variables Make Makefiles Simpler
158 * make Deduces::                Letting @code{make} Deduce the Commands
159 * Combine By Prerequisite::     Another Style of Makefile
160 * Cleanup::                     Rules for Cleaning the Directory
162 Writing Makefiles
164 * Makefile Contents::           What makefiles contain.
165 * Makefile Names::              How to name your makefile.
166 * Include::                     How one makefile can use another makefile.
167 * MAKEFILES Variable::          The environment can specify extra makefiles.
168 * Remaking Makefiles::          How makefiles get remade.
169 * Overriding Makefiles::        How to override part of one makefile
170                                  with another makefile.
171 * Reading Makefiles::           How makefiles are parsed.
173 Writing Rules
175 * Rule Example::                An example explained.
176 * Rule Syntax::                 General syntax explained.
177 * Wildcards::                   Using wildcard characters such as `*'.
178 * Directory Search::            Searching other directories for source files.
179 * Phony Targets::               Using a target that is not a real file's name.
180 * Force Targets::               You can use a target without commands
181                                   or prerequisites to mark other
182                                   targets as phony.
183 * Empty Targets::               When only the date matters and the
184                                   files are empty.
185 * Special Targets::             Targets with special built-in meanings.
186 * Multiple Targets::            When to make use of several targets in a rule.
187 * Multiple Rules::              How to use several rules with the same target.
188 * Static Pattern::              Static pattern rules apply to multiple targets
189                                   and can vary the prerequisites according to
190                                   the target name.
191 * Double-Colon::                How to use a special kind of rule to allow
192                                   several independent rules for one target.
193 * Automatic Prerequisites::     How to automatically generate rules giving
194                                  prerequisites from source files themselves.
196 Using Wildcard Characters in File Names
198 * Wildcard Examples::           Several examples
199 * Wildcard Pitfall::            Problems to avoid.
200 * Wildcard Function::           How to cause wildcard expansion where
201                                   it does not normally take place.
203 Searching Directories for Prerequisites
205 * General Search::              Specifying a search path that applies
206                                   to every prerequisite.
207 * Selective Search::            Specifying a search path
208                                   for a specified class of names.
209 * Search Algorithm::            When and how search paths are applied.
210 * Commands/Search::             How to write shell commands that work together
211                                   with search paths.
212 * Implicit/Search::             How search paths affect implicit rules.
213 * Libraries/Search::            Directory search for link libraries.
215 Static Pattern Rules
217 * Static Usage::                The syntax of static pattern rules.
218 * Static versus Implicit::      When are they better than implicit rules?
220 Writing the Commands in Rules
222 * Echoing::                     How to control when commands are echoed.
223 * Execution::                   How commands are executed.
224 * Parallel::                    How commands can be executed in parallel.
225 * Errors::                      What happens after a command execution error.
226 * Interrupts::                  What happens when a command is interrupted.
227 * Recursion::                   Invoking @code{make} from makefiles.
228 * Sequences::                   Defining canned sequences of commands.
229 * Empty Commands::              Defining useful, do-nothing commands.
231 Recursive Use of @code{make}
233 * MAKE Variable::               The special effects of using @samp{$(MAKE)}.
234 * Variables/Recursion::         How to communicate variables to a sub-@code{make}.
235 * Options/Recursion::           How to communicate options to a sub-@code{make}.
236 * -w Option::                   How the @samp{-w} or @samp{--print-directory} option
237                                  helps debug use of recursive @code{make} commands.
239 How to Use Variables
241 * Reference::                   How to use the value of a variable.
242 * Flavors::                     Variables come in two flavors.
243 * Advanced::                    Advanced features for referencing a variable.
244 * Values::                      All the ways variables get their values.
245 * Setting::                     How to set a variable in the makefile.
246 * Appending::                   How to append more text to the old value
247                                   of a variable.
248 * Override Directive::          How to set a variable in the makefile even if
249                                   the user has set it with a command argument.
250 * Defining::                    An alternate way to set a variable
251                                   to a verbatim string.
252 * Environment::                 Variable values can come from the environment.
253 * Automatic::                   Some special variables have predefined
254                                   meanings for use with implicit rules.
256 Advanced Features for Reference to Variables
258 * Substitution Refs::           Referencing a variable with
259                                   substitutions on the value.
260 * Computed Names::              Computing the name of the variable to refer to.
262 Conditional Parts of Makefiles
264 * Conditional Example::         Example of a conditional
265 * Conditional Syntax::          The syntax of conditionals.
266 * Testing Flags::               Conditionals that test flags.
268 Functions for Transforming Text
270 * Syntax of Functions::         How to write a function call.
271 * Text Functions::              General-purpose text manipulation functions.
272 * File Name Functions::         Functions for manipulating file names.
273 * Foreach Function::            Repeat some text with controlled variation.
274 * Call Function::               Expand a user-defined function.
275 * Origin Function::             Find where a variable got its value.
276 * Shell Function::              Substitute the output of a shell command.
278 How to Run @code{make}
280 * Makefile Arguments::          How to specify which makefile to use.
281 * Goals::                       How to use goal arguments to specify which
282                                   parts of the makefile to use.
283 * Instead of Execution::        How to use mode flags to specify what
284                                   kind of thing to do with the commands
285                                   in the makefile other than simply
286                                   execute them.
287 * Avoiding Compilation::        How to avoid recompiling certain files.
288 * Overriding::                  How to override a variable to specify
289                                   an alternate compiler and other things.
290 * Testing::                     How to proceed past some errors, to
291                                   test compilation.
292 * Options Summary::             Summary of Options
294 Using Implicit Rules
296 * Using Implicit::              How to use an existing implicit rule
297                                   to get the commands for updating a file.
298 * Catalogue of Rules::          A list of built-in implicit rules.
299 * Implicit Variables::          How to change what predefined rules do.
300 * Chained Rules::               How to use a chain of implicit rules.
301 * Pattern Rules::               How to define new implicit rules.
302 * Last Resort::                 How to defining commands for rules
303                                   which cannot find any.
304 * Suffix Rules::                The old-fashioned style of implicit rule.
305 * Implicit Rule Search::        The precise algorithm for applying
306                                   implicit rules.
308 Defining and Redefining Pattern Rules
310 * Pattern Intro::               An introduction to pattern rules.
311 * Pattern Examples::            Examples of pattern rules.
312 * Automatic::                   How to use automatic variables in the
313                                   commands of implicit rules.
314 * Pattern Match::               How patterns match.
315 * Match-Anything Rules::        Precautions you should take prior to
316                                   defining rules that can match any
317                                   target file whatever.
318 * Canceling Rules::             How to override or cancel built-in rules.
320 Using @code{make} to Update Archive Files
322 * Archive Members::             Archive members as targets.
323 * Archive Update::              The implicit rule for archive member targets.
324 * Archive Pitfalls::            Dangers to watch out for when using archives.
325 * Archive Suffix Rules::        You can write a special kind of suffix rule
326                                   for updating archives.
328 Implicit Rule for Archive Member Targets
330 * Archive Symbols::             How to update archive symbol directories.
331 @end menu
333 @node Overview, Introduction, Top, Top
334 @comment  node-name,  next,  previous,  up
335 @chapter Overview of @code{make}
337 The @code{make} utility automatically determines which pieces of a large
338 program need to be recompiled, and issues commands to recompile them.
339 This manual describes GNU @code{make}, which was implemented by Richard
340 Stallman and Roland McGrath.  Development since Version 3.76 has been
341 handled by Paul D. Smith.
343 GNU @code{make} conforms to section 6.2 of @cite{IEEE Standard
344 1003.2-1992} (POSIX.2).
345 @cindex POSIX
346 @cindex IEEE Standard 1003.2
347 @cindex standards conformance
349 Our examples show C programs, since they are most common, but you can use
350 @code{make} with any programming language whose compiler can be run with a
351 shell command.  Indeed, @code{make} is not limited to programs.  You can
352 use it to describe any task where some files must be updated automatically
353 from others whenever the others change.
355 @menu
356 * Preparing::                   Preparing and Running Make
357 * Reading::                     On Reading this Text
358 * Bugs::                        Problems and Bugs
359 @end menu
361 @node Preparing, Reading,  , Overview
362 @ifinfo
363 @heading Preparing and Running Make
364 @end ifinfo
366 To prepare to use @code{make}, you must write a file called
367 the @dfn{makefile} that describes the relationships among files
368 in your program and provides commands for updating each file.
369 In a program, typically, the executable file is updated from object
370 files, which are in turn made by compiling source files.@refill
372 Once a suitable makefile exists, each time you change some source files,
373 this simple shell command:
375 @example
376 make
377 @end example
379 @noindent
380 suffices to perform all necessary recompilations.  The @code{make} program
381 uses the makefile data base and the last-modification times of the files to
382 decide which of the files need to be updated.  For each of those files, it
383 issues the commands recorded in the data base.
385 You can provide command line arguments to @code{make} to control which
386 files should be recompiled, or how.  @xref{Running, ,How to Run
387 @code{make}}.
389 @node Reading, Bugs, Preparing, Overview
390 @section How to Read This Manual
392 If you are new to @code{make}, or are looking for a general
393 introduction, read the first few sections of each chapter, skipping the
394 later sections.  In each chapter, the first few sections contain
395 introductory or general information and the later sections contain
396 specialized or technical information.
397 @ifinfo
398 The exception is the second chapter, @ref{Introduction, ,An
399 Introduction to Makefiles}, all of which is introductory.
400 @end ifinfo
401 @iftex
402 The exception is @ref{Introduction, ,An Introduction to Makefiles},
403 all of which is introductory.
404 @end iftex
406 If you are familiar with other @code{make} programs, see @ref{Features,
407 ,Features of GNU @code{make}}, which lists the enhancements GNU
408 @code{make} has, and @ref{Missing, ,Incompatibilities and Missing
409 Features}, which explains the few things GNU @code{make} lacks that
410 others have.
412 For a quick summary, see @ref{Options Summary}, @ref{Quick Reference},
413 and @ref{Special Targets}.
415 @node Bugs,  , Reading, Overview
416 @section Problems and Bugs
417 @cindex reporting bugs
418 @cindex bugs, reporting
419 @cindex problems and bugs, reporting
421 If you have problems with GNU @code{make} or think you've found a bug,
422 please report it to the developers; we cannot promise to do anything but
423 we might well want to fix it.
425 Before reporting a bug, make sure you've actually found a real bug.
426 Carefully reread the documentation and see if it really says you can do
427 what you're trying to do.  If it's not clear whether you should be able
428 to do something or not, report that too; it's a bug in the
429 documentation!
431 Before reporting a bug or trying to fix it yourself, try to isolate it
432 to the smallest possible makefile that reproduces the problem.  Then
433 send us the makefile and the exact results @code{make} gave you.  Also
434 say what you expected to occur; this will help us decide whether the
435 problem was really in the documentation.
437 Once you've got a precise problem, please send electronic mail to:
439 @example
440     bug-make@@gnu.org
441 @end example
443 @noindent
444 Please include the version number of @code{make} you are using.  You can
445 get this information with the command @samp{make --version}.
446 Be sure also to include the type of machine and operating system you are
447 using.  If possible, include the contents of the file @file{config.h}
448 that is generated by the configuration process.
450 @node Introduction, Makefiles, Overview, Top
451 @comment  node-name,  next,  previous,  up
452 @chapter An Introduction to Makefiles
454 You need a file called a @dfn{makefile} to tell @code{make} what to do.
455 Most often, the makefile tells @code{make} how to compile and link a
456 program.
457 @cindex makefile
459 In this chapter, we will discuss a simple makefile that describes how to
460 compile and link a text editor which consists of eight C source files
461 and three header files.  The makefile can also tell @code{make} how to
462 run miscellaneous commands when explicitly asked (for example, to remove
463 certain files as a clean-up operation).  To see a more complex example
464 of a makefile, see @ref{Complex Makefile}.
466 When @code{make} recompiles the editor, each changed C source file
467 must be recompiled.  If a header file has changed, each C source file
468 that includes the header file must be recompiled to be safe.  Each
469 compilation produces an object file corresponding to the source file.
470 Finally, if any source file has been recompiled, all the object files,
471 whether newly made or saved from previous compilations, must be linked
472 together to produce the new executable editor.
473 @cindex recompilation
474 @cindex editor
476 @menu
477 * Rule Introduction::           What a rule looks like.
478 * Simple Makefile::             A Simple Makefile
479 * How Make Works::              How @code{make} Processes This Makefile
480 * Variables Simplify::          Variables Make Makefiles Simpler
481 * make Deduces::                Letting @code{make} Deduce the Commands
482 * Combine By Prerequisite::     Another Style of Makefile
483 * Cleanup::                     Rules for Cleaning the Directory
484 @end menu
486 @node Rule Introduction, Simple Makefile,  , Introduction
487 @comment  node-name,  next,  previous,  up
488 @section What a Rule Looks Like
489 @cindex rule, introduction to
490 @cindex makefile rule parts
491 @cindex parts of makefile rule
493 A simple makefile consists of ``rules'' with the following shape:
495 @cindex targets, introduction to
496 @cindex prerequisites, introduction to
497 @cindex commands, introduction to
498 @example
499 @group
500 @var{target} @dots{} : @var{prerequisites} @dots{}
501         @var{command}
502         @dots{}
503         @dots{}
504 @end group
505 @end example
507 A @dfn{target} is usually the name of a file that is generated by a
508 program; examples of targets are executable or object files.  A target
509 can also be the name of an action to carry out, such as @samp{clean}
510 (@pxref{Phony Targets}).
512 A @dfn{prerequisite} is a file that is used as input to create the
513 target.  A target often depends on several files.
515 @cindex tabs in rules
516 A @dfn{command} is an action that @code{make} carries out.
517 A rule may have more than one command, each on its own line.
518 @strong{Please note:} you need to put a tab character at the beginning of
519 every command line!  This is an obscurity that catches the unwary.
521 Usually a command is in a rule with prerequisites and serves to create a
522 target file if any of the prerequisites change.  However, the rule that
523 specifies commands for the target need not have prerequisites.  For
524 example, the rule containing the delete command associated with the
525 target @samp{clean} does not have prerequisites.
527 A @dfn{rule}, then, explains how and when to remake certain files
528 which are the targets of the particular rule.  @code{make} carries out
529 the commands on the prerequisites to create or update the target.  A
530 rule can also explain how and when to carry out an action.
531 @xref{Rules, , Writing Rules}.
533 A makefile may contain other text besides rules, but a simple makefile
534 need only contain rules.  Rules may look somewhat more complicated
535 than shown in this template, but all fit the pattern more or less.
537 @node Simple Makefile, How Make Works, Rule Introduction, Introduction
538 @section A Simple Makefile
539 @cindex simple makefile
540 @cindex makefile, simple
542 Here is a straightforward makefile that describes the way an
543 executable file called @code{edit} depends on eight object files
544 which, in turn, depend on eight C source and three header files.
546 In this example, all the C files include @file{defs.h}, but only those
547 defining editing commands include @file{command.h}, and only low
548 level files that change the editor buffer include @file{buffer.h}.
550 @example
551 @group
552 edit : main.o kbd.o command.o display.o \
553        insert.o search.o files.o utils.o
554         cc -o edit main.o kbd.o command.o display.o \
555                    insert.o search.o files.o utils.o
557 main.o : main.c defs.h
558         cc -c main.c
559 kbd.o : kbd.c defs.h command.h
560         cc -c kbd.c
561 command.o : command.c defs.h command.h
562         cc -c command.c
563 display.o : display.c defs.h buffer.h
564         cc -c display.c
565 insert.o : insert.c defs.h buffer.h
566         cc -c insert.c
567 search.o : search.c defs.h buffer.h
568         cc -c search.c
569 files.o : files.c defs.h buffer.h command.h
570         cc -c files.c
571 utils.o : utils.c defs.h
572         cc -c utils.c
573 clean :
574         rm edit main.o kbd.o command.o display.o \
575            insert.o search.o files.o utils.o
576 @end group
577 @end example
579 @noindent
580 We split each long line into two lines using backslash-newline; this is
581 like using one long line, but is easier to read.
582 @cindex continuation lines
583 @cindex @code{\} (backslash), for continuation lines
584 @cindex backslash (@code{\}), for continuation lines
585 @cindex quoting newline, in makefile
586 @cindex newline, quoting, in makefile
588 To use this makefile to create the executable file called @file{edit},
589 type:
591 @example
592 make
593 @end example
595 To use this makefile to delete the executable file and all the object
596 files from the directory, type:
598 @example
599 make clean
600 @end example
602 In the example makefile, the targets include the executable file
603 @samp{edit}, and the object files @samp{main.o} and @samp{kbd.o}.  The
604 prerequisites are files such as @samp{main.c} and @samp{defs.h}.
605 In fact, each @samp{.o} file is both a target and a prerequisite.
606 Commands include @w{@samp{cc -c main.c}} and @w{@samp{cc -c kbd.c}}.
608 When a target is a file, it needs to be recompiled or relinked if any
609 of its prerequisites change.  In addition, any prerequisites that are
610 themselves automatically generated should be updated first.  In this
611 example, @file{edit} depends on each of the eight object files; the
612 object file @file{main.o} depends on the source file @file{main.c} and
613 on the header file @file{defs.h}.
615 A shell command follows each line that contains a target and
616 prerequisites.  These shell commands say how to update the target file.
617 A tab character must come at the beginning of every command line to
618 distinguish commands lines from other lines in the makefile.  (Bear in
619 mind that @code{make} does not know anything about how the commands
620 work.  It is up to you to supply commands that will update the target
621 file properly.  All @code{make} does is execute the commands in the rule
622 you have specified when the target file needs to be updated.)
623 @cindex shell command
625 The target @samp{clean} is not a file, but merely the name of an
626 action.  Since you
627 normally
628 do not want to carry out the actions in this rule, @samp{clean} is not a prerequisite of any other rule.
629 Consequently, @code{make} never does anything with it unless you tell
630 it specifically.  Note that this rule not only is not a prerequisite, it
631 also does not have any prerequisites, so the only purpose of the rule
632 is to run the specified commands.  Targets that do not refer to files
633 but are just actions are called @dfn{phony targets}.  @xref{Phony
634 Targets}, for information about this kind of target.  @xref{Errors, ,
635 Errors in Commands}, to see how to cause @code{make} to ignore errors
636 from @code{rm} or any other command.
637 @cindex @code{clean} target
638 @cindex @code{rm} (shell command)
640 @node How Make Works, Variables Simplify, Simple Makefile, Introduction
641 @comment  node-name,  next,  previous,  up
642 @section How @code{make} Processes a Makefile
643 @cindex processing a makefile
644 @cindex makefile, how @code{make} processes
646 By default, @code{make} starts with the first target (not targets whose
647 names start with @samp{.}).  This is called the @dfn{default goal}.
648 (@dfn{Goals} are the targets that @code{make} strives ultimately to
649 update.  @xref{Goals, , Arguments to Specify the Goals}.)
650 @cindex default goal
651 @cindex goal, default
652 @cindex goal
654 In the simple example of the previous section, the default goal is to
655 update the executable program @file{edit}; therefore, we put that rule
656 first.
658 Thus, when you give the command:
660 @example
661 make
662 @end example
664 @noindent
665 @code{make} reads the makefile in the current directory and begins by
666 processing the first rule.  In the example, this rule is for relinking
667 @file{edit}; but before @code{make} can fully process this rule, it
668 must process the rules for the files that @file{edit} depends on,
669 which in this case are the object files.  Each of these files is
670 processed according to its own rule.  These rules say to update each
671 @samp{.o} file by compiling its source file.  The recompilation must
672 be done if the source file, or any of the header files named as
673 prerequisites, is more recent than the object file, or if the object
674 file does not exist.
676 The other rules are processed because their targets appear as
677 prerequisites of the goal.  If some other rule is not depended on by the
678 goal (or anything it depends on, etc.), that rule is not processed,
679 unless you tell @code{make} to do so (with a command such as
680 @w{@code{make clean}}).
682 Before recompiling an object file, @code{make} considers updating its
683 prerequisites, the source file and header files.  This makefile does not
684 specify anything to be done for them---the @samp{.c} and @samp{.h} files
685 are not the targets of any rules---so @code{make} does nothing for these
686 files.  But @code{make} would update automatically generated C programs,
687 such as those made by Bison or Yacc, by their own rules at this time.
689 After recompiling whichever object files need it, @code{make} decides
690 whether to relink @file{edit}.  This must be done if the file
691 @file{edit} does not exist, or if any of the object files are newer than
692 it.  If an object file was just recompiled, it is now newer than
693 @file{edit}, so @file{edit} is relinked.
694 @cindex relinking
696 Thus, if we change the file @file{insert.c} and run @code{make},
697 @code{make} will compile that file to update @file{insert.o}, and then
698 link @file{edit}.  If we change the file @file{command.h} and run
699 @code{make}, @code{make} will recompile the object files @file{kbd.o},
700 @file{command.o} and @file{files.o} and then link the file @file{edit}.
702 @node Variables Simplify, make Deduces, How Make Works, Introduction
703 @section Variables Make Makefiles Simpler
704 @cindex variables
705 @cindex simplifying with variables
707 In our example, we had to list all the object files twice in the rule for
708 @file{edit} (repeated here):
710 @example
711 @group
712 edit : main.o kbd.o command.o display.o \
713               insert.o search.o files.o utils.o
714         cc -o edit main.o kbd.o command.o display.o \
715                    insert.o search.o files.o utils.o
716 @end group
717 @end example
719 @cindex @code{objects}
720 Such duplication is error-prone; if a new object file is added to the
721 system, we might add it to one list and forget the other.  We can eliminate
722 the risk and simplify the makefile by using a variable.  @dfn{Variables}
723 allow a text string to be defined once and substituted in multiple places
724 later (@pxref{Using Variables, ,How to Use Variables}).
726 @cindex @code{OBJECTS}
727 @cindex @code{objs}
728 @cindex @code{OBJS}
729 @cindex @code{obj}
730 @cindex @code{OBJ}
731 It is standard practice for every makefile to have a variable named
732 @code{objects}, @code{OBJECTS}, @code{objs}, @code{OBJS}, @code{obj},
733 or @code{OBJ} which is a list of all object file names.  We would
734 define such a variable @code{objects} with a line like this in the
735 makefile:@refill
737 @example
738 @group
739 objects = main.o kbd.o command.o display.o \
740           insert.o search.o files.o utils.o
741 @end group
742 @end example
744 @noindent
745 Then, each place we want to put a list of the object file names, we can
746 substitute the variable's value by writing @samp{$(objects)}
747 (@pxref{Using Variables, ,How to Use Variables}).
749 Here is how the complete simple makefile looks when you use a variable
750 for the object files:
752 @example
753 @group
754 objects = main.o kbd.o command.o display.o \
755           insert.o search.o files.o utils.o
757 edit : $(objects)
758         cc -o edit $(objects)
759 main.o : main.c defs.h
760         cc -c main.c
761 kbd.o : kbd.c defs.h command.h
762         cc -c kbd.c
763 command.o : command.c defs.h command.h
764         cc -c command.c
765 display.o : display.c defs.h buffer.h
766         cc -c display.c
767 insert.o : insert.c defs.h buffer.h
768         cc -c insert.c
769 search.o : search.c defs.h buffer.h
770         cc -c search.c
771 files.o : files.c defs.h buffer.h command.h
772         cc -c files.c
773 utils.o : utils.c defs.h
774         cc -c utils.c
775 clean :
776         rm edit $(objects)
777 @end group
778 @end example
780 @node make Deduces, Combine By Prerequisite, Variables Simplify, Introduction
781 @section Letting @code{make} Deduce the Commands
782 @cindex deducing commands (implicit rules)
783 @cindex implicit rule, introduction to
784 @cindex rule, implicit, introduction to
786 It is not necessary to spell out the commands for compiling the individual
787 C source files, because @code{make} can figure them out: it has an
788 @dfn{implicit rule} for updating a @samp{.o} file from a correspondingly
789 named @samp{.c} file using a @samp{cc -c} command.  For example, it will
790 use the command @samp{cc -c main.c -o main.o} to compile @file{main.c} into
791 @file{main.o}.  We can therefore omit the commands from the rules for the
792 object files.  @xref{Implicit Rules, ,Using Implicit Rules}.@refill
794 When a @samp{.c} file is used automatically in this way, it is also
795 automatically added to the list of prerequisites.  We can therefore omit
796 the @samp{.c} files from the prerequisites, provided we omit the commands.
798 Here is the entire example, with both of these changes, and a variable
799 @code{objects} as suggested above:
801 @example
802 @group
803 objects = main.o kbd.o command.o display.o \
804           insert.o search.o files.o utils.o
806 edit : $(objects)
807         cc -o edit $(objects)
809 main.o : defs.h
810 kbd.o : defs.h command.h
811 command.o : defs.h command.h
812 display.o : defs.h buffer.h
813 insert.o : defs.h buffer.h
814 search.o : defs.h buffer.h
815 files.o : defs.h buffer.h command.h
816 utils.o : defs.h
818 .PHONY : clean
819 clean :
820         -rm edit $(objects)
821 @end group
822 @end example
824 @noindent
825 This is how we would write the makefile in actual practice.  (The
826 complications associated with @samp{clean} are described elsewhere.
827 See @ref{Phony Targets}, and @ref{Errors, ,Errors in Commands}.)
829 Because implicit rules are so convenient, they are important.  You
830 will see them used frequently.@refill
832 @node Combine By Prerequisite, Cleanup, make Deduces, Introduction
833 @section Another Style of Makefile
834 @cindex combining rules by prerequisite
836 When the objects of a makefile are created only by implicit rules, an
837 alternative style of makefile is possible.  In this style of makefile,
838 you group entries by their prerequisites instead of by their targets.
839 Here is what one looks like:
841 @example
842 @group
843 objects = main.o kbd.o command.o display.o \
844           insert.o search.o files.o utils.o
846 edit : $(objects)
847         cc -o edit $(objects)
849 $(objects) : defs.h
850 kbd.o command.o files.o : command.h
851 display.o insert.o search.o files.o : buffer.h
852 @end group
853 @end example
855 @noindent
856 Here @file{defs.h} is given as a prerequisite of all the object files;
857 @file{command.h} and @file{buffer.h} are prerequisites of the specific
858 object files listed for them.
860 Whether this is better is a matter of taste: it is more compact, but some
861 people dislike it because they find it clearer to put all the information
862 about each target in one place.
864 @node Cleanup,  , Combine By Prerequisite, Introduction
865 @section Rules for Cleaning the Directory
866 @cindex cleaning up
867 @cindex removing, to clean up
869 Compiling a program is not the only thing you might want to write rules
870 for.  Makefiles commonly tell how to do a few other things besides
871 compiling a program: for example, how to delete all the object files
872 and executables so that the directory is @samp{clean}.
874 @cindex @code{clean} target
875 Here is how we
876 could write a @code{make} rule for cleaning our example editor:
878 @example
879 @group
880 clean:
881         rm edit $(objects)
882 @end group
883 @end example
885 In practice, we might want to write the rule in a somewhat more
886 complicated manner to handle unanticipated situations.  We would do this:
888 @example
889 @group
890 .PHONY : clean
891 clean :
892         -rm edit $(objects)
893 @end group
894 @end example
896 @noindent
897 This prevents @code{make} from getting confused by an actual file
898 called @file{clean} and causes it to continue in spite of errors from
899 @code{rm}.  (See @ref{Phony Targets}, and @ref{Errors, ,Errors in
900 Commands}.)
902 @noindent
903 A rule such as this should not be placed at the beginning of the
904 makefile, because we do not want it to run by default!  Thus, in the
905 example makefile, we want the rule for @code{edit}, which recompiles
906 the editor, to remain the default goal.
908 Since @code{clean} is not a prerequisite of @code{edit}, this rule will not
909 run at all if we give the command @samp{make} with no arguments.  In
910 order to make the rule run, we have to type @samp{make clean}.
911 @xref{Running, ,How to Run @code{make}}.
913 @node Makefiles, Rules, Introduction, Top
914 @chapter Writing Makefiles
916 @cindex makefile, how to write
917 The information that tells @code{make} how to recompile a system comes from
918 reading a data base called the @dfn{makefile}.
920 @menu
921 * Makefile Contents::           What makefiles contain.
922 * Makefile Names::              How to name your makefile.
923 * Include::                     How one makefile can use another makefile.
924 * MAKEFILES Variable::          The environment can specify extra makefiles.
925 * Remaking Makefiles::          How makefiles get remade.
926 * Overriding Makefiles::        How to override part of one makefile
927                                  with another makefile.
928 * Reading Makefiles::           How makefiles are parsed.
929 @end menu
931 @node Makefile Contents, Makefile Names,  , Makefiles
932 @section What Makefiles Contain
934 Makefiles contain five kinds of things: @dfn{explicit rules},
935 @dfn{implicit rules}, @dfn{variable definitions}, @dfn{directives},
936 and @dfn{comments}.  Rules, variables, and directives are described at
937 length in later chapters.@refill
939 @itemize @bullet
940 @cindex rule, explicit, definition of
941 @cindex explicit rule, definition of
942 @item
943 An @dfn{explicit rule} says when and how to remake one or more files,
944 called the rule's targets.  It lists the other files that the targets
945 depend on, call the @dfn{prerequisites} of the target, and may also give
946 commands to use to create or update the targets.  @xref{Rules, ,Writing
947 Rules}.
949 @cindex rule, implicit, definition of
950 @cindex implicit rule, definition of
951 @item
952 An @dfn{implicit rule} says when and how to remake a class of files
953 based on their names.  It describes how a target may depend on a file
954 with a name similar to the target and gives commands to create or
955 update such a target.  @xref{Implicit Rules, ,Using Implicit Rules}.
957 @cindex variable definition
958 @item
959 A @dfn{variable definition} is a line that specifies a text string
960 value for a variable that can be substituted into the text later.  The
961 simple makefile example shows a variable definition for @code{objects}
962 as a list of all object files (@pxref{Variables Simplify, , Variables
963 Make Makefiles Simpler}).
965 @cindex directive
966 @item
967 A @dfn{directive} is a command for @code{make} to do something special while
968 reading the makefile.  These include:
970 @itemize @bullet
971 @item
972 Reading another makefile (@pxref{Include, ,Including Other Makefiles}).
974 @item
975 Deciding (based on the values of variables) whether to use or
976 ignore a part of the makefile (@pxref{Conditionals, ,Conditional Parts of Makefiles}).
978 @item
979 Defining a variable from a verbatim string containing multiple lines
980 (@pxref{Defining, ,Defining Variables Verbatim}).
981 @end itemize
983 @cindex comments, in makefile
984 @cindex @code{#} (comments), in makefile
985 @item
986 @samp{#} in a line of a makefile starts a @dfn{comment}.  It and the rest of
987 the line are ignored, except that a trailing backslash not escaped by
988 another backslash will continue the comment across multiple lines.
989 Comments may appear on any of the lines in the makefile, except within a
990 @code{define} directive, and perhaps within commands (where the shell
991 decides what is a comment).  A line containing just a comment (with
992 perhaps spaces before it) is effectively blank, and is ignored.@refill
993 @end itemize
995 @node Makefile Names, Include, Makefile Contents, Makefiles
996 @section What Name to Give Your Makefile
997 @cindex makefile name
998 @cindex name of makefile
999 @cindex default makefile name
1000 @cindex file name of makefile
1002 @c following paragraph rewritten to avoid overfull hbox
1003 By default, when @code{make} looks for the makefile, it tries the
1004 following names, in order: @file{GNUmakefile}, @file{makefile}
1005 and @file{Makefile}.@refill
1006 @findex Makefile
1007 @findex GNUmakefile
1008 @findex makefile
1010 @cindex @code{README}
1011 Normally you should call your makefile either @file{makefile} or
1012 @file{Makefile}.  (We recommend @file{Makefile} because it appears
1013 prominently near the beginning of a directory listing, right near other
1014 important files such as @file{README}.)  The first name checked,
1015 @file{GNUmakefile}, is not recommended for most makefiles.  You should
1016 use this name if you have a makefile that is specific to GNU
1017 @code{make}, and will not be understood by other versions of
1018 @code{make}.  Other @code{make} programs look for @file{makefile} and
1019 @file{Makefile}, but not @file{GNUmakefile}.
1021 If @code{make} finds none of these names, it does not use any makefile.
1022 Then you must specify a goal with a command argument, and @code{make}
1023 will attempt to figure out how to remake it using only its built-in
1024 implicit rules.  @xref{Implicit Rules, ,Using Implicit Rules}.
1026 @cindex @code{-f}
1027 @cindex @code{--file}
1028 @cindex @code{--makefile}
1029 If you want to use a nonstandard name for your makefile, you can specify
1030 the makefile name with the @samp{-f} or @samp{--file} option.  The
1031 arguments @w{@samp{-f @var{name}}} or @w{@samp{--file=@var{name}}} tell
1032 @code{make} to read the file @var{name} as the makefile.  If you use
1033 more than one @samp{-f} or @samp{--file} option, you can specify several
1034 makefiles.  All the makefiles are effectively concatenated in the order
1035 specified.  The default makefile names @file{GNUmakefile},
1036 @file{makefile} and @file{Makefile} are not checked automatically if you
1037 specify @samp{-f} or @samp{--file}.@refill
1038 @cindex specifying makefile name
1039 @cindex makefile name, how to specify
1040 @cindex name of makefile, how to specify
1041 @cindex file name of makefile, how to specify
1043 @node Include, MAKEFILES Variable, Makefile Names, Makefiles
1044 @section Including Other Makefiles
1045 @cindex including other makefiles
1046 @cindex makefile, including
1048 @findex include
1049 The @code{include} directive tells @code{make} to suspend reading the
1050 current makefile and read one or more other makefiles before continuing.
1051 The directive is a line in the makefile that looks like this:
1053 @example
1054 include @var{filenames}@dots{}
1055 @end example
1057 @noindent
1058 @var{filenames} can contain shell file name patterns.
1059 @cindex shell file name pattern (in @code{include})
1060 @cindex shell wildcards (in @code{include})
1061 @cindex wildcard, in @code{include}
1063 Extra spaces are allowed and ignored at the beginning of the line, but
1064 a tab is not allowed.  (If the line begins with a tab, it will be
1065 considered a command line.)  Whitespace is required between
1066 @code{include} and the file names, and between file names; extra
1067 whitespace is ignored there and at the end of the directive.  A
1068 comment starting with @samp{#} is allowed at the end of the line.  If
1069 the file names contain any variable or function references, they are
1070 expanded.  @xref{Using Variables, ,How to Use Variables}.
1072 For example, if you have three @file{.mk} files, @file{a.mk},
1073 @file{b.mk}, and @file{c.mk}, and @code{$(bar)} expands to
1074 @code{bish bash}, then the following expression
1076 @example
1077 include foo *.mk $(bar)
1078 @end example
1080 is equivalent to
1082 @example
1083 include foo a.mk b.mk c.mk bish bash
1084 @end example
1086 When @code{make} processes an @code{include} directive, it suspends
1087 reading of the containing makefile and reads from each listed file in
1088 turn.  When that is finished, @code{make} resumes reading the
1089 makefile in which the directive appears.
1091 One occasion for using @code{include} directives is when several programs,
1092 handled by individual makefiles in various directories, need to use a
1093 common set of variable definitions
1094 (@pxref{Setting, ,Setting Variables}) or pattern rules
1095 (@pxref{Pattern Rules, ,Defining and Redefining Pattern Rules}).
1097 Another such occasion is when you want to generate prerequisites from
1098 source files automatically; the prerequisites can be put in a file that
1099 is included by the main makefile.  This practice is generally cleaner
1100 than that of somehow appending the prerequisites to the end of the main
1101 makefile as has been traditionally done with other versions of
1102 @code{make}.  @xref{Automatic Prerequisites}.
1103 @cindex prerequisites, automatic generation
1104 @cindex automatic generation of prerequisites
1105 @cindex generating prerequisites automatically
1107 @cindex @code{-I}
1108 @cindex @code{--include-dir}
1109 @cindex included makefiles, default directories
1110 @cindex default directories for included makefiles
1111 @findex /usr/gnu/include
1112 @findex /usr/local/include
1113 @findex /usr/include
1114 If the specified name does not start with a slash, and the file is not
1115 found in the current directory, several other directories are searched.
1116 First, any directories you have specified with the @samp{-I} or
1117 @samp{--include-dir} option are searched
1118 (@pxref{Options Summary, ,Summary of Options}).
1119 Then the following directories (if they exist)
1120 are searched, in this order:
1121 @file{@var{prefix}/include} (normally @file{/usr/local/include}
1122 @footnote{GNU Make compiled for MS-DOS and MS-Windows behaves as if
1123 @var{prefix} has been defined to be the root of the DJGPP tree
1124 hierarchy.})
1125 @file{/usr/gnu/include},
1126 @file{/usr/local/include}, @file{/usr/include}.
1128 If an included makefile cannot be found in any of these directories, a
1129 warning message is generated, but it is not an immediately fatal error;
1130 processing of the makefile containing the @code{include} continues.
1131 Once it has finished reading makefiles, @code{make} will try to remake
1132 any that are out of date or don't exist.
1133 @xref{Remaking Makefiles, ,How Makefiles Are Remade}.
1134 Only after it has tried to find a way to remake a makefile and failed,
1135 will @code{make} diagnose the missing makefile as a fatal error.
1137 If you want @code{make} to simply ignore a makefile which does not exist
1138 and cannot be remade, with no error message, use the @w{@code{-include}}
1139 directive instead of @code{include}, like this:
1141 @example
1142 -include @var{filenames}@dots{}
1143 @end example
1145 This is acts like @code{include} in every way except that there is no
1146 error (not even a warning) if any of the @var{filenames} do not exist.
1147 For compatibility with some other @code{make} implementations,
1148 @code{sinclude} is another name for @w{@code{-include}}.
1150 @node MAKEFILES Variable, Remaking Makefiles, Include, Makefiles
1151 @section The Variable @code{MAKEFILES}
1152 @cindex makefile, and @code{MAKEFILES} variable
1153 @cindex including (@code{MAKEFILES} variable)
1155 @vindex MAKEFILES
1156 If the environment variable @code{MAKEFILES} is defined, @code{make}
1157 considers its value as a list of names (separated by whitespace) of
1158 additional makefiles to be read before the others.  This works much like
1159 the @code{include} directive: various directories are searched for those
1160 files (@pxref{Include, ,Including Other Makefiles}).  In addition, the
1161 default goal is never taken from one of these makefiles and it is not an
1162 error if the files listed in @code{MAKEFILES} are not found.@refill
1164 @cindex recursion, and @code{MAKEFILES} variable
1165 The main use of @code{MAKEFILES} is in communication between recursive
1166 invocations of @code{make} (@pxref{Recursion, ,Recursive Use of
1167 @code{make}}).  It usually is not desirable to set the environment
1168 variable before a top-level invocation of @code{make}, because it is
1169 usually better not to mess with a makefile from outside.  However, if
1170 you are running @code{make} without a specific makefile, a makefile in
1171 @code{MAKEFILES} can do useful things to help the built-in implicit
1172 rules work better, such as defining search paths (@pxref{Directory Search}).
1174 Some users are tempted to set @code{MAKEFILES} in the environment
1175 automatically on login, and program makefiles to expect this to be done.
1176 This is a very bad idea, because such makefiles will fail to work if run by
1177 anyone else.  It is much better to write explicit @code{include} directives
1178 in the makefiles.  @xref{Include, , Including Other Makefiles}.
1180 @node Remaking Makefiles, Overriding Makefiles, MAKEFILES Variable, Makefiles
1181 @section How Makefiles Are Remade
1183 @cindex updating makefiles
1184 @cindex remaking makefiles
1185 @cindex makefile, remaking of
1186 Sometimes makefiles can be remade from other files, such as RCS or SCCS
1187 files.  If a makefile can be remade from other files, you probably want
1188 @code{make} to get an up-to-date version of the makefile to read in.
1190 To this end, after reading in all makefiles, @code{make} will consider
1191 each as a goal target and attempt to update it.  If a makefile has a
1192 rule which says how to update it (found either in that very makefile or
1193 in another one) or if an implicit rule applies to it (@pxref{Implicit
1194 Rules, ,Using Implicit Rules}), it will be updated if necessary.  After
1195 all makefiles have been checked, if any have actually been changed,
1196 @code{make} starts with a clean slate and reads all the makefiles over
1197 again.  (It will also attempt to update each of them over again, but
1198 normally this will not change them again, since they are already up to
1199 date.)@refill
1201 If you know that one or more of your makefiles cannot be remade and you
1202 want to keep @code{make} from performing an implicit rule search on
1203 them, perhaps for efficiency reasons, you can use any normal method of
1204 preventing implicit rule lookup to do so.  For example, you can write an
1205 explicit rule with the makefile as the target, and an empty command
1206 string (@pxref{Empty Commands, ,Using Empty Commands}).
1208 If the makefiles specify a double-colon rule to remake a file with
1209 commands but no prerequisites, that file will always be remade
1210 (@pxref{Double-Colon}).  In the case of makefiles, a makefile that has a
1211 double-colon rule with commands but no prerequisites will be remade every
1212 time @code{make} is run, and then again after @code{make} starts over
1213 and reads the makefiles in again.  This would cause an infinite loop:
1214 @code{make} would constantly remake the makefile, and never do anything
1215 else.  So, to avoid this, @code{make} will @strong{not} attempt to
1216 remake makefiles which are specified as targets of a double-colon rule
1217 with commands but no prerequisites.@refill
1219 If you do not specify any makefiles to be read with @samp{-f} or
1220 @samp{--file} options, @code{make} will try the default makefile names;
1221 @pxref{Makefile Names, ,What Name to Give Your Makefile}.  Unlike
1222 makefiles explicitly requested with @samp{-f} or @samp{--file} options,
1223 @code{make} is not certain that these makefiles should exist.  However,
1224 if a default makefile does not exist but can be created by running
1225 @code{make} rules, you probably want the rules to be run so that the
1226 makefile can be used.
1228 Therefore, if none of the default makefiles exists, @code{make} will try
1229 to make each of them in the same order in which they are searched for
1230 (@pxref{Makefile Names, ,What Name to Give Your Makefile})
1231 until it succeeds in making one, or it runs out of names to try.  Note
1232 that it is not an error if @code{make} cannot find or make any makefile;
1233 a makefile is not always necessary.@refill
1235 When you use the @samp{-t} or @samp{--touch} option
1236 (@pxref{Instead of Execution, ,Instead of Executing the Commands}),
1237 you would not want to use an out-of-date makefile to decide which
1238 targets to touch.  So the @samp{-t} option has no effect on updating
1239 makefiles; they are really updated even if @samp{-t} is specified.
1240 Likewise, @samp{-q} (or @samp{--question}) and @samp{-n} (or
1241 @samp{--just-print}) do not prevent updating of makefiles, because an
1242 out-of-date makefile would result in the wrong output for other targets.
1243 Thus, @samp{make -f mfile -n foo} will update @file{mfile}, read it in,
1244 and then print the commands to update @file{foo} and its prerequisites
1245 without running them.  The commands printed for @file{foo} will be those
1246 specified in the updated contents of @file{mfile}.
1248 However, on occasion you might actually wish to prevent updating of even
1249 the makefiles.  You can do this by specifying the makefiles as goals in
1250 the command line as well as specifying them as makefiles.  When the
1251 makefile name is specified explicitly as a goal, the options @samp{-t}
1252 and so on do apply to them.
1254 Thus, @samp{make -f mfile -n mfile foo} would read the makefile
1255 @file{mfile}, print the commands needed to update it without actually
1256 running them, and then print the commands needed to update @file{foo}
1257 without running them.  The commands for @file{foo} will be those
1258 specified by the existing contents of @file{mfile}.
1260 @node Overriding Makefiles, Reading Makefiles, Remaking Makefiles, Makefiles
1261 @section Overriding Part of Another Makefile
1263 @cindex overriding makefiles
1264 @cindex makefile, overriding
1265 Sometimes it is useful to have a makefile that is mostly just like
1266 another makefile.  You can often use the @samp{include} directive to
1267 include one in the other, and add more targets or variable definitions.
1268 However, if the two makefiles give different commands for the same
1269 target, @code{make} will not let you just do this.  But there is another way.
1271 @cindex match-anything rule, used to override
1272 In the containing makefile (the one that wants to include the other),
1273 you can use a match-anything pattern rule to say that to remake any
1274 target that cannot be made from the information in the containing
1275 makefile, @code{make} should look in another makefile.
1276 @xref{Pattern Rules}, for more information on pattern rules.
1278 For example, if you have a makefile called @file{Makefile} that says how
1279 to make the target @samp{foo} (and other targets), you can write a
1280 makefile called @file{GNUmakefile} that contains:
1282 @example
1283 foo:
1284         frobnicate > foo
1286 %: force
1287         @@$(MAKE) -f Makefile $@@
1288 force: ;
1289 @end example
1291 If you say @samp{make foo}, @code{make} will find @file{GNUmakefile},
1292 read it, and see that to make @file{foo}, it needs to run the command
1293 @samp{frobnicate > foo}.  If you say @samp{make bar}, @code{make} will
1294 find no way to make @file{bar} in @file{GNUmakefile}, so it will use the
1295 commands from the pattern rule: @samp{make -f Makefile bar}.  If
1296 @file{Makefile} provides a rule for updating @file{bar}, @code{make}
1297 will apply the rule.  And likewise for any other target that
1298 @file{GNUmakefile} does not say how to make.
1300 The way this works is that the pattern rule has a pattern of just
1301 @samp{%}, so it matches any target whatever.  The rule specifies a
1302 prerequisite @file{force}, to guarantee that the commands will be run even
1303 if the target file already exists.  We give @file{force} target empty
1304 commands to prevent @code{make} from searching for an implicit rule to
1305 build it---otherwise it would apply the same match-anything rule to
1306 @file{force} itself and create a prerequisite loop!
1308 @node Reading Makefiles,  , Overriding Makefiles, Makefiles
1309 @section How @code{make} Reads a Makefile
1310 @cindex reading makefiles
1311 @cindex makefile, parsing
1313 GNU @code{make} does its work in two distinct phases.  During the first
1314 phase it reads all the makefiles, included makefiles, etc. and
1315 internalizes all the variables and their values, implicit and explicit
1316 rules, and constructs a dependency graph of all the targets and their
1317 prerequisites.  During the second phase, @code{make} uses these internal
1318 structures to determine what targets will need to be rebuilt and to
1319 invoke the rules necessary to do so.
1321 It's important to understand this two-phase approach because it has a
1322 direct impact on how variable and function expansion happens; this is
1323 often a source of some confusion when writing makefiles.  Here we will
1324 present a summary of the phases in which expansion happens for different
1325 constructs within the makefile.  We say that expansion is
1326 @dfn{immediate} if it happens during the first phase: in this case
1327 @code{make} will expand any variables or functions in that section of a
1328 construct as the makefile is parsed.  We say that expansion is
1329 @dfn{deferred} if expansion is not performed immediately.  Expansion of
1330 deferred construct is not performed until either the construct appears
1331 later in an immediate context, or until the second phase.
1333 You may not be familiar with some of these constructs yet.  You can
1334 reference this section as you become familiar with them, in later
1335 chapters.
1337 @subheading Variable Assignment
1338 @cindex +=, expansion
1339 @cindex =, expansion
1340 @cindex ?=, expansion
1341 @cindex +=, expansion
1342 @cindex define, expansion
1344 Variable definitions are parsed as follows:
1346 @example
1347 @var{immediate} = @var{deferred}
1348 @var{immediate} ?= @var{deferred}
1349 @var{immediate} := @var{immediate}
1350 @var{immediate} += @var{deferred} or @var{immediate}
1352 define @var{immediate}
1353   @var{deferred}
1354 endef
1355 @end example
1357 For the append operator, @samp{+=}, the right-hand side is considered
1358 immediate if the variable was previously set as a simple variable
1359 (@samp{:=}), and deferred otherwise.
1361 @subheading Conditional Syntax
1362 @cindex ifdef, expansion
1363 @cindex ifeq, expansion
1364 @cindex ifndef, expansion
1365 @cindex ifneq, expansion
1367 All instances of conditional syntax are parsed immediately, in their
1368 entirety; this includes the @code{ifdef}, @code{ifeq}, @code{ifndef},
1369 and @code{ifneq} forms.
1371 @subheading Rule Definition
1372 @cindex target, expansion
1373 @cindex prerequisite, expansion
1374 @cindex implicit rule, expansion
1375 @cindex pattern rule, expansion
1376 @cindex explicit rule, expansion
1378 A rule is always expanded the same way, regardless of the form:
1380 @example
1381 @var{immediate} : @var{immediate} ; @var{deferred}
1382         @var{deferred}
1383 @end example
1385 That is, the target and prerequisite sections are expanded immediately,
1386 and the commands used to construct the target are always deferred.  This
1387 general rule is true for explicit rules, pattern rules, suffix rules,
1388 static pattern rules, and simple prerequisite definitions.
1390 @node Rules, Commands, Makefiles, Top
1391 @chapter Writing Rules
1392 @cindex writing rules
1393 @cindex rule, how to write
1394 @cindex target
1395 @cindex prerequisite
1397 A @dfn{rule} appears in the makefile and says when and how to remake
1398 certain files, called the rule's @dfn{targets} (most often only one per rule).
1399 It lists the other files that are the @dfn{prerequisites} of the target, and
1400 @dfn{commands} to use to create or update the target.
1402 @cindex default goal
1403 @cindex goal, default
1404 The order of rules is not significant, except for determining the
1405 @dfn{default goal}: the target for @code{make} to consider, if you do
1406 not otherwise specify one.  The default goal is the target of the first
1407 rule in the first makefile.  If the first rule has multiple targets,
1408 only the first target is taken as the default.  There are two
1409 exceptions: a target starting with a period is not a default unless it
1410 contains one or more slashes, @samp{/}, as well; and, a target that
1411 defines a pattern rule has no effect on the default goal.
1412 (@xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.)
1414 Therefore, we usually write the makefile so that the first rule is the
1415 one for compiling the entire program or all the programs described by
1416 the makefile (often with a target called @samp{all}).
1417 @xref{Goals, ,Arguments to Specify the Goals}.
1419 @menu
1420 * Rule Example::                An example explained.
1421 * Rule Syntax::                 General syntax explained.
1422 * Wildcards::                   Using wildcard characters such as `*'.
1423 * Directory Search::            Searching other directories for source files.
1424 * Phony Targets::               Using a target that is not a real file's name.
1425 * Force Targets::               You can use a target without commands
1426                                   or prerequisites to mark other
1427                                   targets as phony.
1428 * Empty Targets::               When only the date matters and the
1429                                   files are empty.
1430 * Special Targets::             Targets with special built-in meanings.
1431 * Multiple Targets::            When to make use of several targets in a rule.
1432 * Multiple Rules::              How to use several rules with the same target.
1433 * Static Pattern::              Static pattern rules apply to multiple targets
1434                                   and can vary the prerequisites according to
1435                                   the target name.
1436 * Double-Colon::                How to use a special kind of rule to allow
1437                                   several independent rules for one target.
1438 * Automatic Prerequisites::     How to automatically generate rules giving
1439                                   prerequisites from source files themselves.
1440 @end menu
1442 @ifinfo
1443 @node Rule Example, Rule Syntax,  , Rules
1444 @section Rule Example
1446 Here is an example of a rule:
1448 @example
1449 foo.o : foo.c defs.h       # module for twiddling the frobs
1450         cc -c -g foo.c
1451 @end example
1453 Its target is @file{foo.o} and its prerequisites are @file{foo.c} and
1454 @file{defs.h}.  It has one command, which is @samp{cc -c -g foo.c}.
1455 The command line starts with a tab to identify it as a command.
1457 This rule says two things:
1459 @itemize @bullet
1460 @item
1461 How to decide whether @file{foo.o} is out of date: it is out of date
1462 if it does not exist, or if either @file{foo.c} or @file{defs.h} is
1463 more recent than it.
1465 @item
1466 How to update the file @file{foo.o}: by running @code{cc} as stated.
1467 The command does not explicitly mention @file{defs.h}, but we presume
1468 that @file{foo.c} includes it, and that that is why @file{defs.h} was
1469 added to the prerequisites.
1470 @end itemize
1471 @end ifinfo
1473 @node Rule Syntax, Wildcards, Rule Example, Rules
1474 @section Rule Syntax
1476 @cindex rule syntax
1477 @cindex syntax of rules
1478 In general, a rule looks like this:
1480 @example
1481 @var{targets} : @var{prerequisites}
1482         @var{command}
1483         @dots{}
1484 @end example
1486 @noindent
1487 or like this:
1489 @example
1490 @var{targets} : @var{prerequisites} ; @var{command}
1491         @var{command}
1492         @dots{}
1493 @end example
1495 @cindex targets
1496 @cindex rule targets
1497 The @var{targets} are file names, separated by spaces.  Wildcard
1498 characters may be used (@pxref{Wildcards, ,Using Wildcard Characters
1499 in File Names}) and a name of the form @file{@var{a}(@var{m})}
1500 represents member @var{m} in archive file @var{a}
1501 (@pxref{Archive Members, ,Archive Members as Targets}).
1502 Usually there is only one
1503 target per rule, but occasionally there is a reason to have more
1504 (@pxref{Multiple Targets, , Multiple Targets in a Rule}).@refill
1506 @cindex commands
1507 @cindex tab character (in commands)
1508 The @var{command} lines start with a tab character.  The first command may
1509 appear on the line after the prerequisites, with a tab character, or may
1510 appear on the same line, with a semicolon.  Either way, the effect is the
1511 same.  @xref{Commands, ,Writing the Commands in Rules}.
1513 @cindex dollar sign (@code{$}), in rules
1514 @cindex @code{$}, in rules
1515 @cindex rule, and @code{$}
1516 Because dollar signs are used to start variable references, if you really
1517 want a dollar sign in a rule you must write two of them, @samp{$$}
1518 (@pxref{Using Variables, ,How to Use Variables}).
1519 You may split a long line by inserting a backslash
1520 followed by a newline, but this is not required, as @code{make} places no
1521 limit on the length of a line in a makefile.
1523 A rule tells @code{make} two things: when the targets are out of date,
1524 and how to update them when necessary.
1526 @cindex prerequisites
1527 @cindex rule prerequisites
1528 The criterion for being out of date is specified in terms of the
1529 @var{prerequisites}, which consist of file names separated by spaces.
1530 (Wildcards and archive members (@pxref{Archives}) are allowed here too.)
1531 A target is out of date if it does not exist or if it is older than any
1532 of the prerequisites (by comparison of last-modification times).  The
1533 idea is that the contents of the target file are computed based on
1534 information in the prerequisites, so if any of the prerequisites changes,
1535 the contents of the existing target file are no longer necessarily
1536 valid.
1538 How to update is specified by @var{commands}.  These are lines to be
1539 executed by the shell (normally @samp{sh}), but with some extra features
1540 (@pxref{Commands, ,Writing the Commands in Rules}).
1542 @node Wildcards, Directory Search, Rule Syntax, Rules
1543 @section Using Wildcard Characters in File Names
1544 @cindex wildcard
1545 @cindex file name with wildcards
1546 @cindex globbing (wildcards)
1548 @cindex @code{*} (wildcard character)
1549 @cindex @code{?} (wildcard character)
1550 @cindex @code{[@dots{}]} (wildcard characters)
1551 A single file name can specify many files using @dfn{wildcard characters}.
1552 The wildcard characters in @code{make} are @samp{*}, @samp{?} and
1553 @samp{[@dots{}]}, the same as in the Bourne shell.  For example, @file{*.c}
1554 specifies a list of all the files (in the working directory) whose names
1555 end in @samp{.c}.@refill
1557 @cindex @code{~} (tilde)
1558 @cindex tilde (@code{~})
1559 @cindex home directory
1560 The character @samp{~} at the beginning of a file name also has special
1561 significance.  If alone, or followed by a slash, it represents your home
1562 directory.  For example @file{~/bin} expands to @file{/home/you/bin}.
1563 If the @samp{~} is followed by a word, the string represents the home
1564 directory of the user named by that word.  For example @file{~john/bin}
1565 expands to @file{/home/john/bin}.  On systems which don't have a home
1566 directory for each user (such as MS-DOS or MS-Windows), this
1567 functionality can be simulated by setting the environment variable
1568 @var{HOME}.@refill
1570 Wildcard expansion happens automatically in targets, in prerequisites,
1571 and in commands (where the shell does the expansion).  In other
1572 contexts, wildcard expansion happens only if you request it explicitly
1573 with the @code{wildcard} function.
1575 The special significance of a wildcard character can be turned off by
1576 preceding it with a backslash.  Thus, @file{foo\*bar} would refer to a
1577 specific file whose name consists of @samp{foo}, an asterisk, and
1578 @samp{bar}.@refill
1580 @menu
1581 * Wildcard Examples::           Several examples
1582 * Wildcard Pitfall::            Problems to avoid.
1583 * Wildcard Function::           How to cause wildcard expansion where
1584                                   it does not normally take place.
1585 @end menu
1587 @node Wildcard Examples, Wildcard Pitfall,  , Wildcards
1588 @subsection Wildcard Examples
1590 Wildcards can be used in the commands of a rule, where they are expanded
1591 by the shell.  For example, here is a rule to delete all the object files:
1593 @example
1594 @group
1595 clean:
1596         rm -f *.o
1597 @end group
1598 @end example
1599 @cindex @code{rm} (shell command)
1601 Wildcards are also useful in the prerequisites of a rule.  With the
1602 following rule in the makefile, @samp{make print} will print all the
1603 @samp{.c} files that have changed since the last time you printed them:
1605 @example
1606 print: *.c
1607         lpr -p $?
1608         touch print
1609 @end example
1611 @cindex @code{print} target
1612 @cindex @code{lpr} (shell command)
1613 @cindex @code{touch} (shell command)
1614 @noindent
1615 This rule uses @file{print} as an empty target file; see @ref{Empty
1616 Targets, ,Empty Target Files to Record Events}.  (The automatic variable
1617 @samp{$?} is used to print only those files that have changed; see
1618 @ref{Automatic, ,Automatic Variables}.)@refill
1620 Wildcard expansion does not happen when you define a variable.  Thus, if
1621 you write this:
1623 @example
1624 objects = *.o
1625 @end example
1627 @noindent
1628 then the value of the variable @code{objects} is the actual string
1629 @samp{*.o}.  However, if you use the value of @code{objects} in a target,
1630 prerequisite or command, wildcard expansion will take place at that time.
1631 To set @code{objects} to the expansion, instead use:
1633 @example
1634 objects := $(wildcard *.o)
1635 @end example
1637 @noindent
1638 @xref{Wildcard Function}.
1640 @node Wildcard Pitfall, Wildcard Function, Wildcard Examples, Wildcards
1641 @subsection Pitfalls of Using Wildcards
1642 @cindex wildcard pitfalls
1643 @cindex pitfalls of wildcards
1644 @cindex mistakes with wildcards
1645 @cindex errors with wildcards
1646 @cindex problems with wildcards
1648 Now here is an example of a naive way of using wildcard expansion, that
1649 does not do what you would intend.  Suppose you would like to say that the
1650 executable file @file{foo} is made from all the object files in the
1651 directory, and you write this:
1653 @example
1654 objects = *.o
1656 foo : $(objects)
1657         cc -o foo $(CFLAGS) $(objects)
1658 @end example
1660 @noindent
1661 The value of @code{objects} is the actual string @samp{*.o}.  Wildcard
1662 expansion happens in the rule for @file{foo}, so that each @emph{existing}
1663 @samp{.o} file becomes a prerequisite of @file{foo} and will be recompiled if
1664 necessary.
1666 But what if you delete all the @samp{.o} files?  When a wildcard matches
1667 no files, it is left as it is, so then @file{foo} will depend on the
1668 oddly-named file @file{*.o}.  Since no such file is likely to exist,
1669 @code{make} will give you an error saying it cannot figure out how to
1670 make @file{*.o}.  This is not what you want!
1672 Actually it is possible to obtain the desired result with wildcard
1673 expansion, but you need more sophisticated techniques, including the
1674 @code{wildcard} function and string substitution.
1675 @ifinfo
1676 @xref{Wildcard Function, ,The Function @code{wildcard}}.
1677 @end ifinfo
1678 @iftex
1679 These are described in the following section.
1680 @end iftex
1682 @cindex wildcards and MS-DOS/MS-Windows backslashes
1683 @cindex backslashes in pathnames and wildcard expansion
1685 Microsoft operating systems (MS-DOS and MS-Windows) use backslashes to
1686 separate directories in pathnames, like so:
1688 @example
1689   c:\foo\bar\baz.c
1690 @end example
1692 This is equivalent to the Unix-style @file{c:/foo/bar/baz.c} (the
1693 @file{c:} part is the so-called drive letter).  When @code{make} runs on
1694 these systems, it supports backslashes as well as the Unix-style forward
1695 slashes in pathnames.  However, this support does @emph{not} include the
1696 wildcard expansion, where backslash is a quote character.  Therefore,
1697 you @emph{must} use Unix-style slashes in these cases.
1700 @node Wildcard Function,  , Wildcard Pitfall, Wildcards
1701 @subsection The Function @code{wildcard}
1702 @findex wildcard
1704 Wildcard expansion happens automatically in rules.  But wildcard expansion
1705 does not normally take place when a variable is set, or inside the
1706 arguments of a function.  If you want to do wildcard expansion in such
1707 places, you need to use the @code{wildcard} function, like this:
1709 @example
1710 $(wildcard @var{pattern}@dots{})
1711 @end example
1713 @noindent
1714 This string, used anywhere in a makefile, is replaced by a
1715 space-separated list of names of existing files that match one of the
1716 given file name patterns.  If no existing file name matches a pattern,
1717 then that pattern is omitted from the output of the @code{wildcard}
1718 function.  Note that this is different from how unmatched wildcards
1719 behave in rules, where they are used verbatim rather than ignored
1720 (@pxref{Wildcard Pitfall}).
1722 One use of the @code{wildcard} function is to get a list of all the C source
1723 files in a directory, like this:
1725 @example
1726 $(wildcard *.c)
1727 @end example
1729 We can change the list of C source files into a list of object files by
1730 replacing the @samp{.c} suffix with @samp{.o} in the result, like this:
1732 @example
1733 $(patsubst %.c,%.o,$(wildcard *.c))
1734 @end example
1736 @noindent
1737 (Here we have used another function, @code{patsubst}.
1738 @xref{Text Functions, ,Functions for String Substitution and Analysis}.)@refill
1740 Thus, a makefile to compile all C source files in the directory and then
1741 link them together could be written as follows:
1743 @example
1744 objects := $(patsubst %.c,%.o,$(wildcard *.c))
1746 foo : $(objects)
1747         cc -o foo $(objects)
1748 @end example
1750 @noindent
1751 (This takes advantage of the implicit rule for compiling C programs, so
1752 there is no need to write explicit rules for compiling the files.
1753 @xref{Flavors, ,The Two Flavors of Variables}, for an explanation of
1754 @samp{:=}, which is a variant of @samp{=}.)
1756 @node Directory Search, Phony Targets, Wildcards, Rules
1757 @section Searching Directories for Prerequisites
1758 @vindex VPATH
1759 @findex vpath
1760 @cindex vpath
1761 @cindex search path for prerequisites (@code{VPATH})
1762 @cindex directory search (@code{VPATH})
1764 For large systems, it is often desirable to put sources in a separate
1765 directory from the binaries.  The @dfn{directory search} features of
1766 @code{make} facilitate this by searching several directories
1767 automatically to find a prerequisite.  When you redistribute the files
1768 among directories, you do not need to change the individual rules,
1769 just the search paths.
1771 @menu
1772 * General Search::              Specifying a search path that applies
1773                                   to every prerequisite.
1774 * Selective Search::            Specifying a search path
1775                                   for a specified class of names.
1776 * Search Algorithm::            When and how search paths are applied.
1777 * Commands/Search::             How to write shell commands that work together
1778                                   with search paths.
1779 * Implicit/Search::             How search paths affect implicit rules.
1780 * Libraries/Search::            Directory search for link libraries.
1781 @end menu
1783 @node General Search, Selective Search,  , Directory Search
1784 @subsection @code{VPATH}: Search Path for All Prerequisites
1785 @vindex VPATH
1787 The value of the @code{make} variable @code{VPATH} specifies a list of
1788 directories that @code{make} should search.  Most often, the
1789 directories are expected to contain prerequisite files that are not in the
1790 current directory; however, @code{VPATH} specifies a search list that
1791 @code{make} applies for all files, including files which are targets of
1792 rules.
1794 Thus, if a file that is listed as a target or prerequisite does not exist
1795 in the current directory, @code{make} searches the directories listed in
1796 @code{VPATH} for a file with that name.  If a file is found in one of
1797 them, that file may become the prerequisite (see below).  Rules may then
1798 specify the names of files in the prerequisite list as if they all
1799 existed in the current directory.  @xref{Commands/Search, ,Writing Shell
1800 Commands with Directory Search}.
1802 In the @code{VPATH} variable, directory names are separated by colons or
1803 blanks.  The order in which directories are listed is the order followed
1804 by @code{make} in its search.  (On MS-DOS and MS-Windows, semi-colons
1805 are used as separators of directory names in @code{VPATH}, since the
1806 colon can be used in the pathname itself, after the drive letter.)
1808 For example,
1810 @example
1811 VPATH = src:../headers
1812 @end example
1814 @noindent
1815 specifies a path containing two directories, @file{src} and
1816 @file{../headers}, which @code{make} searches in that order.
1818 With this value of @code{VPATH}, the following rule,
1820 @example
1821 foo.o : foo.c
1822 @end example
1824 @noindent
1825 is interpreted as if it were written like this:
1827 @example
1828 foo.o : src/foo.c
1829 @end example
1831 @noindent
1832 assuming the file @file{foo.c} does not exist in the current directory but
1833 is found in the directory @file{src}.
1835 @node Selective Search, Search Algorithm, General Search, Directory Search
1836 @subsection The @code{vpath} Directive
1837 @findex vpath
1839 Similar to the @code{VPATH} variable, but more selective, is the
1840 @code{vpath} directive (note lower case), which allows you to specify a
1841 search path for a particular class of file names: those that match a
1842 particular pattern.  Thus you can supply certain search directories for
1843 one class of file names and other directories (or none) for other file
1844 names.
1846 There are three forms of the @code{vpath} directive:
1848 @table @code
1849 @item vpath @var{pattern} @var{directories}
1850 Specify the search path @var{directories} for file names that match
1851 @var{pattern}.
1853 The search path, @var{directories}, is a list of directories to be
1854 searched, separated by colons (semi-colons on MS-DOS and MS-Windows) or
1855 blanks, just like the search path used in the @code{VPATH} variable.
1857 @item vpath @var{pattern}
1858 Clear out the search path associated with @var{pattern}.
1860 @c Extra blank line makes sure this gets two lines.
1861 @item vpath
1863 Clear all search paths previously specified with @code{vpath} directives.
1864 @end table
1866 A @code{vpath} pattern is a string containing a @samp{%} character.  The
1867 string must match the file name of a prerequisite that is being searched
1868 for, the @samp{%} character matching any sequence of zero or more
1869 characters (as in pattern rules; @pxref{Pattern Rules, ,Defining and
1870 Redefining Pattern Rules}).  For example, @code{%.h} matches files that
1871 end in @code{.h}.  (If there is no @samp{%}, the pattern must match the
1872 prerequisite exactly, which is not useful very often.)
1874 @cindex @code{%}, quoting in @code{vpath}
1875 @cindex @code{%}, quoting with @code{\} (backslash)
1876 @cindex @code{\} (backslash), to quote @code{%}
1877 @cindex backslash (@code{\}), to quote @code{%}
1878 @cindex quoting @code{%}, in @code{vpath}
1879 @samp{%} characters in a @code{vpath} directive's pattern can be quoted
1880 with preceding backslashes (@samp{\}).  Backslashes that would otherwise
1881 quote @samp{%} characters can be quoted with more backslashes.
1882 Backslashes that quote @samp{%} characters or other backslashes are
1883 removed from the pattern before it is compared to file names.  Backslashes
1884 that are not in danger of quoting @samp{%} characters go unmolested.@refill
1886 When a prerequisite fails to exist in the current directory, if the
1887 @var{pattern} in a @code{vpath} directive matches the name of the
1888 prerequisite file, then the @var{directories} in that directive are searched
1889 just like (and before) the directories in the @code{VPATH} variable.
1891 For example,
1893 @example
1894 vpath %.h ../headers
1895 @end example
1897 @noindent
1898 tells @code{make} to look for any prerequisite whose name ends in @file{.h}
1899 in the directory @file{../headers} if the file is not found in the current
1900 directory.
1902 If several @code{vpath} patterns match the prerequisite file's name, then
1903 @code{make} processes each matching @code{vpath} directive one by one,
1904 searching all the directories mentioned in each directive.  @code{make}
1905 handles multiple @code{vpath} directives in the order in which they
1906 appear in the makefile; multiple directives with the same pattern are
1907 independent of each other.
1909 @need 750
1910 Thus,
1912 @example
1913 @group
1914 vpath %.c foo
1915 vpath %   blish
1916 vpath %.c bar
1917 @end group
1918 @end example
1920 @noindent
1921 will look for a file ending in @samp{.c} in @file{foo}, then
1922 @file{blish}, then @file{bar}, while
1924 @example
1925 @group
1926 vpath %.c foo:bar
1927 vpath %   blish
1928 @end group
1929 @end example
1931 @noindent
1932 will look for a file ending in @samp{.c} in @file{foo}, then
1933 @file{bar}, then @file{blish}.
1935 @node Search Algorithm, Commands/Search, Selective Search, Directory Search
1936 @subsection How Directory Searches are Performed
1937 @cindex algorithm for directory search
1938 @cindex directory search algorithm
1940 When a prerequisite is found through directory search, regardless of type
1941 (general or selective), the pathname located may not be the one that
1942 @code{make} actually provides you in the prerequisite list.  Sometimes
1943 the path discovered through directory search is thrown away.
1945 The algorithm @code{make} uses to decide whether to keep or abandon a
1946 path found via directory search is as follows:
1948 @enumerate
1949 @item
1950 If a target file does not exist at the path specified in the makefile,
1951 directory search is performed.
1953 @item
1954 If the directory search is successful, that path is kept and this file
1955 is tentatively stored as the target.
1957 @item
1958 All prerequisites of this target are examined using this same method.
1960 @item
1961 After processing the prerequisites, the target may or may not need to be
1962 rebuilt:
1964 @enumerate a
1965 @item
1966 If the target does @emph{not} need to be rebuilt, the path to the file
1967 found during directory search is used for any prerequisite lists which
1968 contain this target.  In short, if @code{make} doesn't need to rebuild
1969 the target then you use the path found via directory search.
1971 @item
1972 If the target @emph{does} need to be rebuilt (is out-of-date), the
1973 pathname found during directory search is @emph{thrown away}, and the
1974 target is rebuilt using the file name specified in the makefile.  In
1975 short, if @code{make} must rebuild, then the target is rebuilt locally,
1976 not in the directory found via directory search.
1977 @end enumerate
1978 @end enumerate
1980 This algorithm may seem complex, but in practice it is quite often
1981 exactly what you want.
1983 @cindex traditional directory search (GPATH)
1984 @cindex directory search, traditional (GPATH)
1985 Other versions of @code{make} use a simpler algorithm: if the file does
1986 not exist, and it is found via directory search, then that pathname is
1987 always used whether or not the target needs to be built.  Thus, if the
1988 target is rebuilt it is created at the pathname discovered during
1989 directory search.
1991 @vindex GPATH
1992 If, in fact, this is the behavior you want for some or all of your
1993 directories, you can use the @code{GPATH} variable to indicate this to
1994 @code{make}.
1996 @code{GPATH} has the same syntax and format as @code{VPATH} (that is, a
1997 space- or colon-delimited list of pathnames).  If an out-of-date target
1998 is found by directory search in a directory that also appears in
1999 @code{GPATH}, then that pathname is not thrown away.  The target is
2000 rebuilt using the expanded path.
2002 @node Commands/Search, Implicit/Search, Search Algorithm, Directory Search
2003 @subsection Writing Shell Commands with Directory Search
2004 @cindex shell command, and directory search
2005 @cindex directory search (@code{VPATH}), and shell commands
2007 When a prerequisite is found in another directory through directory search,
2008 this cannot change the commands of the rule; they will execute as written.
2009 Therefore, you must write the commands with care so that they will look for
2010 the prerequisite in the directory where @code{make} finds it.
2012 This is done with the @dfn{automatic variables} such as @samp{$^}
2013 (@pxref{Automatic, ,Automatic Variables}).
2014 For instance, the value of @samp{$^} is a
2015 list of all the prerequisites of the rule, including the names of
2016 the directories in which they were found, and the value of
2017 @samp{$@@} is the target.  Thus:@refill
2019 @example
2020 foo.o : foo.c
2021         cc -c $(CFLAGS) $^ -o $@@
2022 @end example
2024 @noindent
2025 (The variable @code{CFLAGS} exists so you can specify flags for C
2026 compilation by implicit rules; we use it here for consistency so it will
2027 affect all C compilations uniformly;
2028 @pxref{Implicit Variables, ,Variables Used by Implicit Rules}.)
2030 Often the prerequisites include header files as well, which you do not
2031 want to mention in the commands.  The automatic variable @samp{$<} is
2032 just the first prerequisite:
2034 @example
2035 VPATH = src:../headers
2036 foo.o : foo.c defs.h hack.h
2037         cc -c $(CFLAGS) $< -o $@@
2038 @end example
2040 @node Implicit/Search, Libraries/Search, Commands/Search, Directory Search
2041 @subsection Directory Search and Implicit Rules
2042 @cindex @code{VPATH}, and implicit rules
2043 @cindex directory search (@code{VPATH}), and implicit rules
2044 @cindex search path for prerequisites (@code{VPATH}), and implicit rules
2045 @cindex implicit rule, and directory search
2046 @cindex implicit rule, and @code{VPATH}
2047 @cindex rule, implicit, and directory search
2048 @cindex rule, implicit, and @code{VPATH}
2050 The search through the directories specified in @code{VPATH} or with
2051 @code{vpath} also happens during consideration of implicit rules
2052 (@pxref{Implicit Rules, ,Using Implicit Rules}).
2054 For example, when a file @file{foo.o} has no explicit rule, @code{make}
2055 considers implicit rules, such as the built-in rule to compile
2056 @file{foo.c} if that file exists.  If such a file is lacking in the
2057 current directory, the appropriate directories are searched for it.  If
2058 @file{foo.c} exists (or is mentioned in the makefile) in any of the
2059 directories, the implicit rule for C compilation is applied.
2061 The commands of implicit rules normally use automatic variables as a
2062 matter of necessity; consequently they will use the file names found by
2063 directory search with no extra effort.
2065 @node Libraries/Search,  , Implicit/Search, Directory Search
2066 @subsection Directory Search for Link Libraries
2067 @cindex link libraries, and directory search
2068 @cindex libraries for linking, directory search
2069 @cindex directory search (@code{VPATH}), and link libraries
2070 @cindex @code{VPATH}, and link libraries
2071 @cindex search path for prerequisites (@code{VPATH}), and link libraries
2072 @cindex @code{-l} (library search)
2073 @cindex link libraries, patterns matching
2074 @cindex @code{.LIBPATTERNS}, and link libraries
2075 @vindex .LIBPATTERNS
2077 Directory search applies in a special way to libraries used with the
2078 linker.  This special feature comes into play when you write a prerequisite
2079 whose name is of the form @samp{-l@var{name}}.  (You can tell something
2080 strange is going on here because the prerequisite is normally the name of a
2081 file, and the @emph{file name} of a library generally looks like
2082 @file{lib@var{name}.a}, not like @samp{-l@var{name}}.)@refill
2084 When a prerequisite's name has the form @samp{-l@var{name}}, @code{make}
2085 handles it specially by searching for the file @file{lib@var{name}.so} in
2086 the current directory, in directories specified by matching @code{vpath}
2087 search paths and the @code{VPATH} search path, and then in the
2088 directories @file{/lib}, @file{/usr/lib}, and @file{@var{prefix}/lib}
2089 (normally @file{/usr/local/lib}, but MS-DOS/MS-Windows versions of
2090 @code{make} behave as if @var{prefix} is defined to be the root of the
2091 DJGPP installation tree).
2093 If that file is not found, then the file @file{lib@var{name}.a} is
2094 searched for, in the same directories as above.
2096 For example, if there is a @file{/usr/lib/libcurses.a} library on your
2097 system (and no @file{/usr/lib/libcurses.so} file), then
2099 @example
2100 @group
2101 foo : foo.c -lcurses
2102         cc $^ -o $@@
2103 @end group
2104 @end example
2106 @noindent
2107 would cause the command @samp{cc foo.c /usr/lib/libcurses.a -o foo} to
2108 be executed when @file{foo} is older than @file{foo.c} or than
2109 @file{/usr/lib/libcurses.a}.@refill
2111 Although the default set of files to be searched for is
2112 @file{lib@var{name}.so} and @file{lib@var{name}.a}, this is customizable
2113 via the @code{.LIBPATTERNS} variable.  Each word in the value of this
2114 variable is a pattern string.  When a prerequisite like
2115 @samp{-l@var{name}} is seen, @code{make} will replace the percent in
2116 each pattern in the list with @var{name} and perform the above directory
2117 searches using that library filename.  If no library is found, the next
2118 word in the list will be used.
2120 The default value for @code{.LIBPATTERNS} is ``@samp{lib%.so lib%.a}'',
2121 which provides the default behavior described above.
2123 You can turn off link library expansion completely by setting this
2124 variable to an empty value.
2126 @node Phony Targets, Force Targets, Directory Search, Rules
2127 @section Phony Targets
2128 @cindex phony targets
2129 @cindex targets, phony
2130 @cindex targets without a file
2132 A phony target is one that is not really the name of a file.  It is just a
2133 name for some commands to be executed when you make an explicit request.
2134 There are two reasons to use a phony target: to avoid a conflict with
2135 a file of the same name, and to improve performance.
2137 If you write a rule whose commands will not create the target file, the
2138 commands will be executed every time the target comes up for remaking.
2139 Here is an example:
2141 @example
2142 @group
2143 clean:
2144         rm *.o temp
2145 @end group
2146 @end example
2148 @noindent
2149 Because the @code{rm} command does not create a file named @file{clean},
2150 probably no such file will ever exist.  Therefore, the @code{rm} command
2151 will be executed every time you say @samp{make clean}.
2152 @cindex @code{rm} (shell command)
2154 @findex .PHONY
2155 The phony target will cease to work if anything ever does create a file
2156 named @file{clean} in this directory.  Since it has no prerequisites, the
2157 file @file{clean} would inevitably be considered up to date, and its
2158 commands would not be executed.  To avoid this problem, you can explicitly
2159 declare the target to be phony, using the special target @code{.PHONY}
2160 (@pxref{Special Targets, ,Special Built-in Target Names}) as follows:
2162 @example
2163 .PHONY : clean
2164 @end example
2166 @noindent
2167 Once this is done, @samp{make clean} will run the commands regardless of
2168 whether there is a file named @file{clean}.
2170 Since it knows that phony targets do not name actual files that could be
2171 remade from other files, @code{make} skips the implicit rule search for
2172 phony targets (@pxref{Implicit Rules}).  This is why declaring a target
2173 phony is good for performance, even if you are not worried about the
2174 actual file existing.
2176 Thus, you first write the line that states that @code{clean} is a
2177 phony target, then you write the rule, like this:
2179 @example
2180 @group
2181 .PHONY: clean
2182 clean:
2183         rm *.o temp
2184 @end group
2185 @end example
2187 Another example of the usefulness of phony targets is in conjunction
2188 with recursive invocations of @code{make}.  In this case the makefile
2189 will often contain a variable which lists a number of subdirectories to
2190 be built.  One way to handle this is with one rule whose command is a
2191 shell loop over the subdirectories, like this:
2193 @example
2194 @group
2195 SUBDIRS = foo bar baz
2197 subdirs:
2198         for dir in $(SUBDIRS); do \
2199           $(MAKE) -C $$dir; \
2200         done
2201 @end group
2202 @end example
2204 There are a few problems with this method, however.  First, any error
2205 detected in a submake is not noted by this rule, so it will continue to
2206 build the rest of the directories even when one fails.  This can be
2207 overcome by adding shell commands to note the error and exit, but then
2208 it will do so even if @code{make} is invoked with the @code{-k} option,
2209 which is unfortunate.  Second, and perhaps more importantly, you cannot
2210 take advantage of the parallel build capabilities of make using this
2211 method, since there is only one rule.
2213 By declaring the subdirectories as phony targets (you must do this as
2214 the subdirectory obviously always exists; otherwise it won't be built)
2215 you can remove these problems:
2217 @example
2218 @group
2219 SUBDIRS = foo bar baz
2221 .PHONY: subdirs $(SUBDIRS)
2223 subdirs: $(SUBDIRS)
2225 $(SUBDIRS):
2226         $(MAKE) -C $@@
2228 foo: baz
2229 @end group
2230 @end example
2232 Here we've also declared that the @file{foo} subdirectory cannot be
2233 built until after the @file{baz} subdirectory is complete; this kind of
2234 relationship declaration is particularly important when attempting
2235 parallel builds.
2237 A phony target should not be a prerequisite of a real target file; if it
2238 is, its commands are run every time @code{make} goes to update that
2239 file.  As long as a phony target is never a prerequisite of a real
2240 target, the phony target commands will be executed only when the phony
2241 target is a specified goal (@pxref{Goals, ,Arguments to Specify the
2242 Goals}).
2244 Phony targets can have prerequisites.  When one directory contains multiple
2245 programs, it is most convenient to describe all of the programs in one
2246 makefile @file{./Makefile}.  Since the target remade by default will be the
2247 first one in the makefile, it is common to make this a phony target named
2248 @samp{all} and give it, as prerequisites, all the individual programs.  For
2249 example:
2251 @example
2252 all : prog1 prog2 prog3
2253 .PHONY : all
2255 prog1 : prog1.o utils.o
2256         cc -o prog1 prog1.o utils.o
2258 prog2 : prog2.o
2259         cc -o prog2 prog2.o
2261 prog3 : prog3.o sort.o utils.o
2262         cc -o prog3 prog3.o sort.o utils.o
2263 @end example
2265 @noindent
2266 Now you can say just @samp{make} to remake all three programs, or specify
2267 as arguments the ones to remake (as in @samp{make prog1 prog3}).
2269 When one phony target is a prerequisite of another, it serves as a subroutine
2270 of the other.  For example, here @samp{make cleanall} will delete the
2271 object files, the difference files, and the file @file{program}:
2273 @example
2274 .PHONY: cleanall cleanobj cleandiff
2276 cleanall : cleanobj cleandiff
2277         rm program
2279 cleanobj :
2280         rm *.o
2282 cleandiff :
2283         rm *.diff
2284 @end example
2286 @node Force Targets, Empty Targets, Phony Targets, Rules
2287 @section Rules without Commands or Prerequisites
2288 @cindex force targets
2289 @cindex targets, force
2290 @cindex @code{FORCE}
2291 @cindex rule, no commands or prerequisites
2293 If a rule has no prerequisites or commands, and the target of the rule
2294 is a nonexistent file, then @code{make} imagines this target to have
2295 been updated whenever its rule is run.  This implies that all targets
2296 depending on this one will always have their commands run.
2298 An example will illustrate this:
2300 @example
2301 @group
2302 clean: FORCE
2303         rm $(objects)
2304 FORCE:
2305 @end group
2306 @end example
2308 Here the target @samp{FORCE} satisfies the special conditions, so the
2309 target @file{clean} that depends on it is forced to run its commands.
2310 There is nothing special about the name @samp{FORCE}, but that is one name
2311 commonly used this way.
2313 As you can see, using @samp{FORCE} this way has the same results as using
2314 @samp{.PHONY: clean}.
2316 Using @samp{.PHONY} is more explicit and more efficient.  However,
2317 other versions of @code{make} do not support @samp{.PHONY}; thus
2318 @samp{FORCE} appears in many makefiles.  @xref{Phony Targets}.
2320 @node Empty Targets, Special Targets, Force Targets, Rules
2321 @section Empty Target Files to Record Events
2322 @cindex empty targets
2323 @cindex targets, empty
2324 @cindex recording events with empty targets
2326 The @dfn{empty target} is a variant of the phony target; it is used to hold
2327 commands for an action that you request explicitly from time to time.
2328 Unlike a phony target, this target file can really exist; but the file's
2329 contents do not matter, and usually are empty.
2331 The purpose of the empty target file is to record, with its
2332 last-modification time, when the rule's commands were last executed.  It
2333 does so because one of the commands is a @code{touch} command to update the
2334 target file.
2336 The empty target file should have some prerequisites (otherwise it
2337 doesn't make sense).  When you ask to remake the empty target, the
2338 commands are executed if any prerequisite is more recent than the target;
2339 in other words, if a prerequisite has changed since the last time you
2340 remade the target.  Here is an example:
2342 @example
2343 print: foo.c bar.c
2344         lpr -p $?
2345         touch print
2346 @end example
2347 @cindex @code{print} target
2348 @cindex @code{lpr} (shell command)
2349 @cindex @code{touch} (shell command)
2351 @noindent
2352 With this rule, @samp{make print} will execute the @code{lpr} command if
2353 either source file has changed since the last @samp{make print}.  The
2354 automatic variable @samp{$?} is used to print only those files that have
2355 changed (@pxref{Automatic, ,Automatic Variables}).
2357 @node Special Targets, Multiple Targets, Empty Targets, Rules
2358 @section Special Built-in Target Names
2359 @cindex special targets
2360 @cindex built-in special targets
2361 @cindex targets, built-in special
2363 Certain names have special meanings if they appear as targets.
2365 @table @code
2366 @findex .PHONY
2367 @item .PHONY
2369 The prerequisites of the special target @code{.PHONY} are considered to
2370 be phony targets.  When it is time to consider such a target,
2371 @code{make} will run its commands unconditionally, regardless of
2372 whether a file with that name exists or what its last-modification
2373 time is.  @xref{Phony Targets, ,Phony Targets}.
2375 @findex .SUFFIXES
2376 @item .SUFFIXES
2378 The prerequisites of the special target @code{.SUFFIXES} are the list
2379 of suffixes to be used in checking for suffix rules.
2380 @xref{Suffix Rules, , Old-Fashioned Suffix Rules}.
2382 @findex .DEFAULT
2383 @item .DEFAULT
2385 The commands specified for @code{.DEFAULT} are used for any target for
2386 which no rules are found (either explicit rules or implicit rules).
2387 @xref{Last Resort}.  If @code{.DEFAULT} commands are specified, every
2388 file mentioned as a prerequisite, but not as a target in a rule, will have
2389 these commands executed on its behalf.  @xref{Implicit Rule Search,
2390 ,Implicit Rule Search Algorithm}.
2392 @findex .PRECIOUS
2393 @item .PRECIOUS
2394 @cindex precious targets
2395 @cindex preserving with @code{.PRECIOUS}
2397 The targets which @code{.PRECIOUS} depends on are given the following
2398 special treatment: if @code{make} is killed or interrupted during the
2399 execution of their commands, the target is not deleted.
2400 @xref{Interrupts, ,Interrupting or Killing @code{make}}.  Also, if the
2401 target is an intermediate file, it will not be deleted after it is no
2402 longer needed, as is normally done.  @xref{Chained Rules, ,Chains of
2403 Implicit Rules}.  In this latter respect it overlaps with the
2404 @code{.SECONDARY} special target.
2406 You can also list the target pattern of an implicit rule (such as
2407 @samp{%.o}) as a prerequisite file of the special target @code{.PRECIOUS}
2408 to preserve intermediate files created by rules whose target patterns
2409 match that file's name.
2411 @findex .INTERMEDIATE
2412 @item .INTERMEDIATE
2413 @cindex intermediate targets, explicit
2415 The targets which @code{.INTERMEDIATE} depends on are treated as
2416 intermediate files.  @xref{Chained Rules, ,Chains of Implicit Rules}.
2417 @code{.INTERMEDIATE} with no prerequisites has no effect.
2419 @findex .SECONDARY
2420 @item .SECONDARY
2421 @cindex secondary targets
2422 @cindex preserving with @code{.SECONDARY}
2424 The targets which @code{.SECONDARY} depends on are treated as
2425 intermediate files, except that they are never automatically deleted.
2426 @xref{Chained Rules, ,Chains of Implicit Rules}.
2428 @code{.SECONDARY} with no prerequisites causes all targets to be treated
2429 as secondary (i.e., no target is removed because it is considered
2430 intermediate).
2432 @findex .DELETE_ON_ERROR
2433 @item .DELETE_ON_ERROR
2434 @cindex removing targets on failure
2436 If @code{.DELETE_ON_ERROR} is mentioned as a target anywhere in the
2437 makefile, then @code{make} will delete the target of a rule if it has
2438 changed and its commands exit with a nonzero exit status, just as it
2439 does when it receives a signal.  @xref{Errors, ,Errors in Commands}.
2441 @findex .IGNORE
2442 @item .IGNORE
2444 If you specify prerequisites for @code{.IGNORE}, then @code{make} will
2445 ignore errors in execution of the commands run for those particular
2446 files.  The commands for @code{.IGNORE} are not meaningful.
2448 If mentioned as a target with no prerequisites, @code{.IGNORE} says to
2449 ignore errors in execution of commands for all files.  This usage of
2450 @samp{.IGNORE} is supported only for historical compatibility.  Since
2451 this affects every command in the makefile, it is not very useful; we
2452 recommend you use the more selective ways to ignore errors in specific
2453 commands.  @xref{Errors, ,Errors in Commands}.
2455 @findex .LOW_RESOLUTION_TIME
2456 @item .LOW_RESOLUTION_TIME
2458 If you specify prerequisites for @code{.LOW_RESOLUTION_TIME},
2459 @command{make} assumes that these files are created by commands that
2460 generate low resolution time stamps.  The commands for
2461 @code{.LOW_RESOLUTION_TIME} are not meaningful.
2463 The high resolution file time stamps of many modern hosts lessen the
2464 chance of @command{make} incorrectly concluding that a file is up to
2465 date.  Unfortunately, these hosts provide no way to set a high
2466 resolution file time stamp, so commands like @samp{cp -p} that
2467 explicitly set a file's time stamp must discard its subsecond part.  If
2468 a file is created by such a command, you should list it as a
2469 prerequisite of @code{.LOW_RESOLUTION_TIME} so that @command{make} does
2470 not mistakenly conclude that the file is out of date.  For example:
2472 @example
2473 @group
2474 .LOW_RESOLUTION_TIME: dst
2475 dst: src
2476         cp -p src dst
2477 @end group
2478 @end example
2480 Since @samp{cp -p} discards the subsecond part of @file{src}'s time
2481 stamp, @file{dst} is typically slightly older than @file{src} even when
2482 it is up to date.  The @code{.LOW_RESOLUTION_TIME} line causes
2483 @command{make} to consider @file{dst} to be up to date if its time stamp
2484 is at the start of the same second that @file{src}'s time stamp is in.
2486 Due to a limitation of the archive format, archive member time stamps
2487 are always low resolution.  You need not list archive members as
2488 prerequisites of @code{.LOW_RESOLUTION_TIME}, as @command{make} does this
2489 automatically.
2491 @findex .SILENT
2492 @item .SILENT
2494 If you specify prerequisites for @code{.SILENT}, then @code{make} will
2495 not print the commands to remake those particular files before executing
2496 them.  The commands for @code{.SILENT} are not meaningful.
2498 If mentioned as a target with no prerequisites, @code{.SILENT} says not
2499 to print any commands before executing them.  This usage of
2500 @samp{.SILENT} is supported only for historical compatibility.  We
2501 recommend you use the more selective ways to silence specific commands.
2502 @xref{Echoing, ,Command Echoing}.  If you want to silence all commands
2503 for a particular run of @code{make}, use the @samp{-s} or
2504 @w{@samp{--silent}} option (@pxref{Options Summary}).
2506 @findex .EXPORT_ALL_VARIABLES
2507 @item .EXPORT_ALL_VARIABLES
2509 Simply by being mentioned as a target, this tells @code{make} to
2510 export all variables to child processes by default.
2511 @xref{Variables/Recursion, ,Communicating Variables to a
2512 Sub-@code{make}}.
2514 @findex .NOTPARALLEL
2515 @item .NOTPARALLEL
2516 @cindex parallel execution, overriding
2518 If @code{.NOTPARALLEL} is mentioned as a target, then this invocation of
2519 @code{make} will be run serially, even if the @samp{-j} option is
2520 given.  Any recursively invoked @code{make} command will still be run in
2521 parallel (unless its makefile contains this target).  Any prerequisites
2522 on this target are ignored.
2523 @end table
2525 Any defined implicit rule suffix also counts as a special target if it
2526 appears as a target, and so does the concatenation of two suffixes, such
2527 as @samp{.c.o}.  These targets are suffix rules, an obsolete way of
2528 defining implicit rules (but a way still widely used).  In principle, any
2529 target name could be special in this way if you break it in two and add
2530 both pieces to the suffix list.  In practice, suffixes normally begin with
2531 @samp{.}, so these special target names also begin with @samp{.}.
2532 @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
2534 @node Multiple Targets, Multiple Rules, Special Targets, Rules
2535 @section Multiple Targets in a Rule
2536 @cindex multiple targets
2537 @cindex several targets in a rule
2538 @cindex targets, multiple
2539 @cindex rule, with multiple targets
2541 A rule with multiple targets is equivalent to writing many rules, each with
2542 one target, and all identical aside from that.  The same commands apply to
2543 all the targets, but their effects may vary because you can substitute the
2544 actual target name into the command using @samp{$@@}.  The rule contributes
2545 the same prerequisites to all the targets also.
2547 This is useful in two cases.
2549 @itemize @bullet
2550 @item
2551 You want just prerequisites, no commands.  For example:
2553 @example
2554 kbd.o command.o files.o: command.h
2555 @end example
2557 @noindent
2558 gives an additional prerequisite to each of the three object files
2559 mentioned.
2561 @item
2562 Similar commands work for all the targets.  The commands do not need
2563 to be absolutely identical, since the automatic variable @samp{$@@}
2564 can be used to substitute the particular target to be remade into the
2565 commands (@pxref{Automatic, ,Automatic Variables}).  For example:
2567 @example
2568 @group
2569 bigoutput littleoutput : text.g
2570         generate text.g -$(subst output,,$@@) > $@@
2571 @end group
2572 @end example
2573 @findex subst
2575 @noindent
2576 is equivalent to
2578 @example
2579 bigoutput : text.g
2580         generate text.g -big > bigoutput
2581 littleoutput : text.g
2582         generate text.g -little > littleoutput
2583 @end example
2585 @noindent
2586 Here we assume the hypothetical program @code{generate} makes two
2587 types of output, one if given @samp{-big} and one if given
2588 @samp{-little}.
2589 @xref{Text Functions, ,Functions for String Substitution and Analysis},
2590 for an explanation of the @code{subst} function.
2591 @end itemize
2593 Suppose you would like to vary the prerequisites according to the target,
2594 much as the variable @samp{$@@} allows you to vary the commands.
2595 You cannot do this with multiple targets in an ordinary rule, but you can
2596 do it with a @dfn{static pattern rule}.
2597 @xref{Static Pattern, ,Static Pattern Rules}.
2599 @node Multiple Rules, Static Pattern, Multiple Targets, Rules
2600 @section Multiple Rules for One Target
2601 @cindex multiple rules for one target
2602 @cindex several rules for one target
2603 @cindex rule, multiple for one target
2604 @cindex target, multiple rules for one
2606 One file can be the target of several rules.  All the prerequisites
2607 mentioned in all the rules are merged into one list of prerequisites for
2608 the target.  If the target is older than any prerequisite from any rule,
2609 the commands are executed.
2611 There can only be one set of commands to be executed for a file.
2612 If more than one rule gives commands for the same file,
2613 @code{make} uses the last set given and prints an error message.
2614 (As a special case, if the file's name begins with a dot, no
2615 error message is printed.  This odd behavior is only for
2616 compatibility with other implementations of @code{make}.)
2617 There is no reason to
2618 write your makefiles this way; that is why @code{make} gives you
2619 an error message.@refill
2621 An extra rule with just prerequisites can be used to give a few extra
2622 prerequisites to many files at once.  For example, one usually has a
2623 variable named @code{objects} containing a list of all the compiler output
2624 files in the system being made.  An easy way to say that all of them must
2625 be recompiled if @file{config.h} changes is to write the following:
2627 @example
2628 objects = foo.o bar.o
2629 foo.o : defs.h
2630 bar.o : defs.h test.h
2631 $(objects) : config.h
2632 @end example
2634 This could be inserted or taken out without changing the rules that really
2635 specify how to make the object files, making it a convenient form to use if
2636 you wish to add the additional prerequisite intermittently.
2638 Another wrinkle is that the additional prerequisites could be specified with
2639 a variable that you set with a command argument to @code{make}
2640 (@pxref{Overriding, ,Overriding Variables}).  For example,
2642 @example
2643 @group
2644 extradeps=
2645 $(objects) : $(extradeps)
2646 @end group
2647 @end example
2649 @noindent
2650 means that the command @samp{make extradeps=foo.h} will consider
2651 @file{foo.h} as a prerequisite of each object file, but plain @samp{make}
2652 will not.
2654 If none of the explicit rules for a target has commands, then @code{make}
2655 searches for an applicable implicit rule to find some commands
2656 @pxref{Implicit Rules, ,Using Implicit Rules}).
2658 @node Static Pattern, Double-Colon, Multiple Rules, Rules
2659 @section Static Pattern Rules
2660 @cindex static pattern rule
2661 @cindex rule, static pattern
2662 @cindex pattern rules, static (not implicit)
2663 @cindex varying prerequisites
2664 @cindex prerequisites, varying (static pattern)
2666 @dfn{Static pattern rules} are rules which specify multiple targets and
2667 construct the prerequisite names for each target based on the target name.
2668 They are more general than ordinary rules with multiple targets because the
2669 targets do not have to have identical prerequisites.  Their prerequisites must
2670 be @emph{analogous}, but not necessarily @emph{identical}.
2672 @menu
2673 * Static Usage::                The syntax of static pattern rules.
2674 * Static versus Implicit::      When are they better than implicit rules?
2675 @end menu
2677 @node Static Usage, Static versus Implicit,  , Static Pattern
2678 @subsection Syntax of Static Pattern Rules
2679 @cindex static pattern rule, syntax of
2680 @cindex pattern rules, static, syntax of
2682 Here is the syntax of a static pattern rule:
2684 @example
2685 @var{targets} @dots{}: @var{target-pattern}: @var{prereq-patterns} @dots{}
2686         @var{commands}
2687         @dots{}
2688 @end example
2690 @noindent
2691 The @var{targets} list specifies the targets that the rule applies to.
2692 The targets can contain wildcard characters, just like the targets of
2693 ordinary rules (@pxref{Wildcards, ,Using Wildcard Characters in File
2694 Names}).
2696 @cindex target pattern, static (not implicit)
2697 @cindex stem
2698 The @var{target-pattern} and @var{prereq-patterns} say how to compute the
2699 prerequisites of each target.  Each target is matched against the
2700 @var{target-pattern} to extract a part of the target name, called the
2701 @dfn{stem}.  This stem is substituted into each of the @var{prereq-patterns}
2702 to make the prerequisite names (one from each @var{prereq-pattern}).
2704 Each pattern normally contains the character @samp{%} just once.  When the
2705 @var{target-pattern} matches a target, the @samp{%} can match any part of
2706 the target name; this part is called the @dfn{stem}.  The rest of the
2707 pattern must match exactly.  For example, the target @file{foo.o} matches
2708 the pattern @samp{%.o}, with @samp{foo} as the stem.  The targets
2709 @file{foo.c} and @file{foo.out} do not match that pattern.@refill
2711 @cindex prerequisite pattern, static (not implicit)
2712 The prerequisite names for each target are made by substituting the stem
2713 for the @samp{%} in each prerequisite pattern.  For example, if one
2714 prerequisite pattern is @file{%.c}, then substitution of the stem
2715 @samp{foo} gives the prerequisite name @file{foo.c}.  It is legitimate
2716 to write a prerequisite pattern that does not contain @samp{%}; then this
2717 prerequisite is the same for all targets.
2719 @cindex @code{%}, quoting in static pattern
2720 @cindex @code{%}, quoting with @code{\} (backslash)
2721 @cindex @code{\} (backslash), to quote @code{%}
2722 @cindex backslash (@code{\}), to quote @code{%}
2723 @cindex quoting @code{%}, in static pattern
2724 @samp{%} characters in pattern rules can be quoted with preceding
2725 backslashes (@samp{\}).  Backslashes that would otherwise quote @samp{%}
2726 characters can be quoted with more backslashes.  Backslashes that quote
2727 @samp{%} characters or other backslashes are removed from the pattern
2728 before it is compared to file names or has a stem substituted into it.
2729 Backslashes that are not in danger of quoting @samp{%} characters go
2730 unmolested.  For example, the pattern @file{the\%weird\\%pattern\\} has
2731 @samp{the%weird\} preceding the operative @samp{%} character, and
2732 @samp{pattern\\} following it.  The final two backslashes are left alone
2733 because they cannot affect any @samp{%} character.@refill
2735 Here is an example, which compiles each of @file{foo.o} and @file{bar.o}
2736 from the corresponding @file{.c} file:
2738 @example
2739 @group
2740 objects = foo.o bar.o
2742 all: $(objects)
2744 $(objects): %.o: %.c
2745         $(CC) -c $(CFLAGS) $< -o $@@
2746 @end group
2747 @end example
2749 @noindent
2750 Here @samp{$<} is the automatic variable that holds the name of the
2751 prerequisite and @samp{$@@} is the automatic variable that holds the name
2752 of the target; see @ref{Automatic, , Automatic Variables}.
2754 Each target specified must match the target pattern; a warning is issued
2755 for each target that does not.  If you have a list of files, only some of
2756 which will match the pattern, you can use the @code{filter} function to
2757 remove nonmatching file names (@pxref{Text Functions, ,Functions for String Substitution and Analysis}):
2759 @example
2760 files = foo.elc bar.o lose.o
2762 $(filter %.o,$(files)): %.o: %.c
2763         $(CC) -c $(CFLAGS) $< -o $@@
2764 $(filter %.elc,$(files)): %.elc: %.el
2765         emacs -f batch-byte-compile $<
2766 @end example
2768 @noindent
2769 In this example the result of @samp{$(filter %.o,$(files))} is
2770 @file{bar.o lose.o}, and the first static pattern rule causes each of
2771 these object files to be updated by compiling the corresponding C source
2772 file.  The result of @w{@samp{$(filter %.elc,$(files))}} is
2773 @file{foo.elc}, so that file is made from @file{foo.el}.@refill
2775 Another example shows how to use @code{$*} in static pattern rules:
2776 @vindex $*@r{, and static pattern}
2778 @example
2779 @group
2780 bigoutput littleoutput : %output : text.g
2781         generate text.g -$* > $@@
2782 @end group
2783 @end example
2785 @noindent
2786 When the @code{generate} command is run, @code{$*} will expand to the
2787 stem, either @samp{big} or @samp{little}.
2789 @node Static versus Implicit,  , Static Usage, Static Pattern
2790 @subsection Static Pattern Rules versus Implicit Rules
2791 @cindex rule, static pattern versus implicit
2792 @cindex static pattern rule, versus implicit
2794 A static pattern rule has much in common with an implicit rule defined as a
2795 pattern rule (@pxref{Pattern Rules, ,Defining and Redefining Pattern Rules}).
2796 Both have a pattern for the target and patterns for constructing the
2797 names of prerequisites.  The difference is in how @code{make} decides
2798 @emph{when} the rule applies.
2800 An implicit rule @emph{can} apply to any target that matches its pattern,
2801 but it @emph{does} apply only when the target has no commands otherwise
2802 specified, and only when the prerequisites can be found.  If more than one
2803 implicit rule appears applicable, only one applies; the choice depends on
2804 the order of rules.
2806 By contrast, a static pattern rule applies to the precise list of targets
2807 that you specify in the rule.  It cannot apply to any other target and it
2808 invariably does apply to each of the targets specified.  If two conflicting
2809 rules apply, and both have commands, that's an error.
2811 The static pattern rule can be better than an implicit rule for these
2812 reasons:
2814 @itemize @bullet
2815 @item
2816 You may wish to override the usual implicit rule for a few
2817 files whose names cannot be categorized syntactically but
2818 can be given in an explicit list.
2820 @item
2821 If you cannot be sure of the precise contents of the directories
2822 you are using, you may not be sure which other irrelevant files
2823 might lead @code{make} to use the wrong implicit rule.  The choice
2824 might depend on the order in which the implicit rule search is done.
2825 With static pattern rules, there is no uncertainty: each rule applies
2826 to precisely the targets specified.
2827 @end itemize
2829 @node Double-Colon, Automatic Prerequisites, Static Pattern, Rules
2830 @section Double-Colon Rules
2831 @cindex double-colon rules
2832 @cindex rule, double-colon (@code{::})
2833 @cindex multiple rules for one target (@code{::})
2834 @cindex @code{::} rules (double-colon)
2836 @dfn{Double-colon} rules are rules written with @samp{::} instead of
2837 @samp{:} after the target names.  They are handled differently from
2838 ordinary rules when the same target appears in more than one rule.
2840 When a target appears in multiple rules, all the rules must be the same
2841 type: all ordinary, or all double-colon.  If they are double-colon, each
2842 of them is independent of the others.  Each double-colon rule's commands
2843 are executed if the target is older than any prerequisites of that rule.
2844 If there are no prerequisites for that rule, its commands are always
2845 executed (even if the target already exists).  This can result in
2846 executing none, any, or all of the double-colon rules.
2848 Double-colon rules with the same target are in fact completely separate
2849 from one another.  Each double-colon rule is processed individually, just
2850 as rules with different targets are processed.
2852 The double-colon rules for a target are executed in the order they appear
2853 in the makefile.  However, the cases where double-colon rules really make
2854 sense are those where the order of executing the commands would not matter.
2856 Double-colon rules are somewhat obscure and not often very useful; they
2857 provide a mechanism for cases in which the method used to update a target
2858 differs depending on which prerequisite files caused the update, and such
2859 cases are rare.
2861 Each double-colon rule should specify commands; if it does not, an
2862 implicit rule will be used if one applies.
2863 @xref{Implicit Rules, ,Using Implicit Rules}.
2865 @node Automatic Prerequisites,  , Double-Colon, Rules
2866 @section Generating Prerequisites Automatically
2867 @cindex prerequisites, automatic generation
2868 @cindex automatic generation of prerequisites
2869 @cindex generating prerequisites automatically
2871 In the makefile for a program, many of the rules you need to write often
2872 say only that some object file depends on some header
2873 file.  For example, if @file{main.c} uses @file{defs.h} via an
2874 @code{#include}, you would write:
2876 @example
2877 main.o: defs.h
2878 @end example
2880 @noindent
2881 You need this rule so that @code{make} knows that it must remake
2882 @file{main.o} whenever @file{defs.h} changes.  You can see that for a
2883 large program you would have to write dozens of such rules in your
2884 makefile.  And, you must always be very careful to update the makefile
2885 every time you add or remove an @code{#include}.
2886 @cindex @code{#include}
2888 @cindex @code{-M} (to compiler)
2889 To avoid this hassle, most modern C compilers can write these rules for
2890 you, by looking at the @code{#include} lines in the source files.
2891 Usually this is done with the @samp{-M} option to the compiler.
2892 For example, the command:
2894 @example
2895 cc -M main.c
2896 @end example
2898 @noindent
2899 generates the output:
2901 @example
2902 main.o : main.c defs.h
2903 @end example
2905 @noindent
2906 Thus you no longer have to write all those rules yourself.
2907 The compiler will do it for you.
2909 Note that such a prerequisite constitutes mentioning @file{main.o} in a
2910 makefile, so it can never be considered an intermediate file by implicit
2911 rule search.  This means that @code{make} won't ever remove the file
2912 after using it; @pxref{Chained Rules, ,Chains of Implicit Rules}.
2914 @cindex @code{make depend}
2915 With old @code{make} programs, it was traditional practice to use this
2916 compiler feature to generate prerequisites on demand with a command like
2917 @samp{make depend}.  That command would create a file @file{depend}
2918 containing all the automatically-generated prerequisites; then the
2919 makefile could use @code{include} to read them in (@pxref{Include}).
2921 In GNU @code{make}, the feature of remaking makefiles makes this
2922 practice obsolete---you need never tell @code{make} explicitly to
2923 regenerate the prerequisites, because it always regenerates any makefile
2924 that is out of date.  @xref{Remaking Makefiles}.
2926 The practice we recommend for automatic prerequisite generation is to have
2927 one makefile corresponding to each source file.  For each source file
2928 @file{@var{name}.c} there is a makefile @file{@var{name}.d} which lists
2929 what files the object file @file{@var{name}.o} depends on.  That way
2930 only the source files that have changed need to be rescanned to produce
2931 the new prerequisites.
2933 Here is the pattern rule to generate a file of prerequisites (i.e., a makefile)
2934 called @file{@var{name}.d} from a C source file called @file{@var{name}.c}:
2936 @smallexample
2937 @group
2938 %.d: %.c
2939         set -e; $(CC) -M $(CPPFLAGS) $< \
2940                   | sed 's/\($*\)\.o[ :]*/\1.o $@@ : /g' > $@@; \
2941                 [ -s $@@ ] || rm -f $@@
2942 @end group
2943 @end smallexample
2945 @noindent
2946 @xref{Pattern Rules}, for information on defining pattern rules.  The
2947 @samp{-e} flag to the shell makes it exit immediately if the
2948 @code{$(CC)} command fails (exits with a nonzero status).  Normally the
2949 shell exits with the status of the last command in the pipeline
2950 (@code{sed} in this case), so @code{make} would not notice a nonzero
2951 status from the compiler.
2952 @cindex @code{-e} (shell flag)
2954 @cindex @code{-MM} (to GNU compiler)
2955 With the GNU C compiler, you may wish to use the @samp{-MM} flag instead
2956 of @samp{-M}.  This omits prerequisites on system header files.
2957 @xref{Preprocessor Options, , Options Controlling the Preprocessor,
2958 gcc.info, Using GNU CC}, for details.
2960 @cindex @code{sed} (shell command)
2961 The purpose of the @code{sed} command is to translate (for example):
2963 @example
2964 main.o : main.c defs.h
2965 @end example
2967 @noindent
2968 into:
2970 @example
2971 main.o main.d : main.c defs.h
2972 @end example
2974 @noindent
2975 @cindex @code{.d}
2976 This makes each @samp{.d} file depend on all the source and header files
2977 that the corresponding @samp{.o} file depends on.  @code{make} then
2978 knows it must regenerate the prerequisites whenever any of the source or
2979 header files changes.
2981 Once you've defined the rule to remake the @samp{.d} files,
2982 you then use the @code{include} directive to read them all in.
2983 @xref{Include}.  For example:
2985 @example
2986 @group
2987 sources = foo.c bar.c
2989 include $(sources:.c=.d)
2990 @end group
2991 @end example
2993 @noindent
2994 (This example uses a substitution variable reference to translate the
2995 list of source files @samp{foo.c bar.c} into a list of prerequisite
2996 makefiles, @samp{foo.d bar.d}.  @xref{Substitution Refs}, for full
2997 information on substitution references.)  Since the @samp{.d} files are
2998 makefiles like any others, @code{make} will remake them as necessary
2999 with no further work from you.  @xref{Remaking Makefiles}.
3001 @node Commands, Using Variables, Rules, Top
3002 @chapter Writing the Commands in Rules
3003 @cindex commands, how to write
3004 @cindex rule commands
3005 @cindex writing rule commands
3007 The commands of a rule consist of shell command lines to be executed one
3008 by one.  Each command line must start with a tab, except that the first
3009 command line may be attached to the target-and-prerequisites line with a
3010 semicolon in between.  Blank lines and lines of just comments may appear
3011 among the command lines; they are ignored.  (But beware, an apparently
3012 ``blank'' line that begins with a tab is @emph{not} blank!  It is an
3013 empty command; @pxref{Empty Commands}.)
3015 Users use many different shell programs, but commands in makefiles are
3016 always interpreted by @file{/bin/sh} unless the makefile specifies
3017 otherwise.  @xref{Execution, ,Command Execution}.
3019 @cindex comments, in commands
3020 @cindex commands, comments in
3021 @cindex @code{#} (comments), in commands
3022 The shell that is in use determines whether comments can be written on
3023 command lines, and what syntax they use.  When the shell is
3024 @file{/bin/sh}, a @samp{#} starts a comment that extends to the end of
3025 the line.  The @samp{#} does not have to be at the beginning of a line.
3026 Text on a line before a @samp{#} is not part of the comment.
3028 @menu
3029 * Echoing::                     How to control when commands are echoed.
3030 * Execution::                   How commands are executed.
3031 * Parallel::                    How commands can be executed in parallel.
3032 * Errors::                      What happens after a command execution error.
3033 * Interrupts::                  What happens when a command is interrupted.
3034 * Recursion::                   Invoking @code{make} from makefiles.
3035 * Sequences::                   Defining canned sequences of commands.
3036 * Empty Commands::              Defining useful, do-nothing commands.
3037 @end menu
3039 @node Echoing, Execution,  , Commands
3040 @section Command Echoing
3041 @cindex echoing of commands
3042 @cindex silent operation
3043 @cindex @code{@@} (in commands)
3044 @cindex commands, echoing
3045 @cindex printing of commands
3047 Normally @code{make} prints each command line before it is executed.
3048 We call this @dfn{echoing} because it gives the appearance that you
3049 are typing the commands yourself.
3051 When a line starts with @samp{@@}, the echoing of that line is suppressed.
3052 The @samp{@@} is discarded before the command is passed to the shell.
3053 Typically you would use this for a command whose only effect is to print
3054 something, such as an @code{echo} command to indicate progress through
3055 the makefile:
3057 @example
3058 @@echo About to make distribution files
3059 @end example
3061 @cindex @code{-n}
3062 @cindex @code{--just-print}
3063 @cindex @code{--dry-run}
3064 @cindex @code{--recon}
3065 When @code{make} is given the flag @samp{-n} or @samp{--just-print}
3066 it only echoes commands, it won't execute them.  @xref{Options Summary,
3067 ,Summary of Options}.  In this case and only this case, even the
3068 commands starting with @samp{@@} are printed.  This flag is useful for
3069 finding out which commands @code{make} thinks are necessary without
3070 actually doing them.
3072 @cindex @code{-s}
3073 @cindex @code{--silent}
3074 @cindex @code{--quiet}
3075 @findex .SILENT
3076 The @samp{-s} or @samp{--silent}
3077 flag to @code{make} prevents all echoing, as if all commands
3078 started with @samp{@@}.  A rule in the makefile for the special target
3079 @code{.SILENT} without prerequisites has the same effect
3080 (@pxref{Special Targets, ,Special Built-in Target Names}).
3081 @code{.SILENT} is essentially obsolete since @samp{@@} is more flexible.@refill
3083 @node Execution, Parallel, Echoing, Commands
3084 @section Command Execution
3085 @cindex commands, execution
3086 @cindex execution, of commands
3087 @cindex shell command, execution
3088 @vindex SHELL @r{(command execution)}
3090 When it is time to execute commands to update a target, they are executed
3091 by making a new subshell for each line.  (In practice, @code{make} may
3092 take shortcuts that do not affect the results.)
3094 @cindex @code{cd} (shell command)
3095 @strong{Please note:} this implies that shell commands such as @code{cd}
3096 that set variables local to each process will not affect the following
3097 command lines. @footnote{On MS-DOS, the value of current working
3098 directory is @strong{global}, so changing it @emph{will} affect the
3099 following command lines on those systems.}  If you want to use @code{cd}
3100 to affect the next command, put the two on a single line with a
3101 semicolon between them.  Then @code{make} will consider them a single
3102 command and pass them, together, to a shell which will execute them in
3103 sequence.  For example:
3105 @example
3106 foo : bar/lose
3107         cd bar; gobble lose > ../foo
3108 @end example
3110 @cindex commands, backslash (@code{\}) in
3111 @cindex commands, quoting newlines in
3112 @cindex backslash (@code{\}), in commands
3113 @cindex @code{\} (backslash), in commands
3114 @cindex quoting newline, in commands
3115 @cindex newline, quoting, in commands
3116 If you would like to split a single shell command into multiple lines of
3117 text, you must use a backslash at the end of all but the last subline.
3118 Such a sequence of lines is combined into a single line, by deleting the
3119 backslash-newline sequences, before passing it to the shell.  Thus, the
3120 following is equivalent to the preceding example:
3122 @example
3123 @group
3124 foo : bar/lose
3125         cd bar;  \
3126         gobble lose > ../foo
3127 @end group
3128 @end example
3130 @vindex SHELL
3131 The program used as the shell is taken from the variable @code{SHELL}.
3132 By default, the program @file{/bin/sh} is used.
3134 @vindex COMSPEC
3135 On MS-DOS, if @code{SHELL} is not set, the value of the variable
3136 @code{COMSPEC} (which is always set) is used instead.
3138 @cindex @code{SHELL}, MS-DOS specifics
3139 The processing of lines that set the variable @code{SHELL} in Makefiles
3140 is different on MS-DOS.  The stock shell, @file{command.com}, is
3141 ridiculously limited in its functionality and many users of @code{make}
3142 tend to install a replacement shell.  Therefore, on MS-DOS, @code{make}
3143 examines the value of @code{SHELL}, and changes its behavior based on
3144 whether it points to a Unix-style or DOS-style shell.  This allows
3145 reasonable functionality even if @code{SHELL} points to
3146 @file{command.com}.
3148 If @code{SHELL} points to a Unix-style shell, @code{make} on MS-DOS
3149 additionally checks whether that shell can indeed be found; if not, it
3150 ignores the line that sets @code{SHELL}.  In MS-DOS, GNU @code{make}
3151 searches for the shell in the following places:
3153 @enumerate
3154 @item
3155 In the precise place pointed to by the value of @code{SHELL}.  For
3156 example, if the makefile specifies @samp{SHELL = /bin/sh}, @code{make}
3157 will look in the directory @file{/bin} on the current drive.
3159 @item
3160 In the current directory.
3162 @item
3163 In each of the directories in the @code{PATH} variable, in order.
3165 @end enumerate
3167 In every directory it examines, @code{make} will first look for the
3168 specific file (@file{sh} in the example above).  If this is not found,
3169 it will also look in that directory for that file with one of the known
3170 extensions which identify executable files.  For example @file{.exe},
3171 @file{.com}, @file{.bat}, @file{.btm}, @file{.sh}, and some others.
3173 If any of these attempts is successful, the value of @code{SHELL} will
3174 be set to the full pathname of the shell as found.  However, if none of
3175 these is found, the value of @code{SHELL} will not be changed, and thus
3176 the line that sets it will be effectively ignored.  This is so
3177 @code{make} will only support features specific to a Unix-style shell if
3178 such a shell is actually installed on the system where @code{make} runs.
3180 Note that this extended search for the shell is limited to the cases
3181 where @code{SHELL} is set from the Makefile; if it is set in the
3182 environment or command line, you are expected to set it to the full
3183 pathname of the shell, exactly as things are on Unix.
3185 The effect of the above DOS-specific processing is that a Makefile that
3186 says @samp{SHELL = /bin/sh} (as many Unix makefiles do), will work
3187 on MS-DOS unaltered if you have e.g. @file{sh.exe} installed in some
3188 directory along your @code{PATH}.
3190 @cindex environment, @code{SHELL} in
3191 Unlike most variables, the variable @code{SHELL} is never set from the
3192 environment.  This is because the @code{SHELL} environment variable is
3193 used to specify your personal choice of shell program for interactive
3194 use.  It would be very bad for personal choices like this to affect the
3195 functioning of makefiles.  @xref{Environment, ,Variables from the
3196 Environment}.  However, on MS-DOS and MS-Windows the value of
3197 @code{SHELL} in the environment @strong{is} used, since on those systems
3198 most users do not set this variable, and therefore it is most likely set
3199 specifically to be used by @code{make}.  On MS-DOS, if the setting of
3200 @code{SHELL} is not suitable for @code{make}, you can set the variable
3201 @code{MAKESHELL} to the shell that @code{make} should use; this will
3202 override the value of @code{SHELL}.
3204 @node Parallel, Errors, Execution, Commands
3205 @section Parallel Execution
3206 @cindex commands, execution in parallel
3207 @cindex parallel execution
3208 @cindex execution, in parallel
3209 @cindex job slots
3210 @cindex @code{-j}
3211 @cindex @code{--jobs}
3213 GNU @code{make} knows how to execute several commands at once.
3214 Normally, @code{make} will execute only one command at a time, waiting
3215 for it to finish before executing the next.  However, the @samp{-j} or
3216 @samp{--jobs} option tells @code{make} to execute many commands
3217 simultaneously.@refill
3219 On MS-DOS, the @samp{-j} option has no effect, since that system doesn't
3220 support multi-processing.
3222 If the @samp{-j} option is followed by an integer, this is the number of
3223 commands to execute at once; this is called the number of @dfn{job slots}.
3224 If there is nothing looking like an integer after the @samp{-j} option,
3225 there is no limit on the number of job slots.  The default number of job
3226 slots is one, which means serial execution (one thing at a time).
3228 One unpleasant consequence of running several commands simultaneously is
3229 that output generated by the commands appears whenever each command
3230 sends it, so messages from different commands may be interspersed.
3232 Another problem is that two processes cannot both take input from the
3233 same device; so to make sure that only one command tries to take input
3234 from the terminal at once, @code{make} will invalidate the standard
3235 input streams of all but one running command.  This means that
3236 attempting to read from standard input will usually be a fatal error (a
3237 @samp{Broken pipe} signal) for most child processes if there are
3238 several.
3239 @cindex broken pipe
3240 @cindex standard input
3242 It is unpredictable which command will have a valid standard input stream
3243 (which will come from the terminal, or wherever you redirect the standard
3244 input of @code{make}).  The first command run will always get it first, and
3245 the first command started after that one finishes will get it next, and so
3248 We will change how this aspect of @code{make} works if we find a better
3249 alternative.  In the mean time, you should not rely on any command using
3250 standard input at all if you are using the parallel execution feature; but
3251 if you are not using this feature, then standard input works normally in
3252 all commands.
3254 Finally, handling recursive @code{make} invocations raises issues.  For
3255 more information on this, see
3256 @ref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
3258 If a command fails (is killed by a signal or exits with a nonzero
3259 status), and errors are not ignored for that command
3260 (@pxref{Errors, ,Errors in Commands}),
3261 the remaining command lines to remake the same target will not be run.
3262 If a command fails and the @samp{-k} or @samp{--keep-going}
3263 option was not given
3264 (@pxref{Options Summary, ,Summary of Options}),
3265 @code{make} aborts execution.  If make
3266 terminates for any reason (including a signal) with child processes
3267 running, it waits for them to finish before actually exiting.@refill
3269 @cindex load average
3270 @cindex limiting jobs based on load
3271 @cindex jobs, limiting based on load
3272 @cindex @code{-l} (load average)
3273 @cindex @code{--max-load}
3274 @cindex @code{--load-average}
3275 When the system is heavily loaded, you will probably want to run fewer jobs
3276 than when it is lightly loaded.  You can use the @samp{-l} option to tell
3277 @code{make} to limit the number of jobs to run at once, based on the load
3278 average.  The @samp{-l} or @samp{--max-load}
3279 option is followed by a floating-point number.  For
3280 example,
3282 @example
3283 -l 2.5
3284 @end example
3286 @noindent
3287 will not let @code{make} start more than one job if the load average is
3288 above 2.5.  The @samp{-l} option with no following number removes the
3289 load limit, if one was given with a previous @samp{-l} option.@refill
3291 More precisely, when @code{make} goes to start up a job, and it already has
3292 at least one job running, it checks the current load average; if it is not
3293 lower than the limit given with @samp{-l}, @code{make} waits until the load
3294 average goes below that limit, or until all the other jobs finish.
3296 By default, there is no load limit.
3298 @node Errors, Interrupts, Parallel, Commands
3299 @section Errors in Commands
3300 @cindex errors (in commands)
3301 @cindex commands, errors in
3302 @cindex exit status (errors)
3304 After each shell command returns, @code{make} looks at its exit status.
3305 If the command completed successfully, the next command line is executed
3306 in a new shell; after the last command line is finished, the rule is
3307 finished.
3309 If there is an error (the exit status is nonzero), @code{make} gives up on
3310 the current rule, and perhaps on all rules.
3312 Sometimes the failure of a certain command does not indicate a problem.
3313 For example, you may use the @code{mkdir} command to ensure that a
3314 directory exists.  If the directory already exists, @code{mkdir} will
3315 report an error, but you probably want @code{make} to continue regardless.
3317 @cindex @code{-} (in commands)
3318 To ignore errors in a command line, write a @samp{-} at the beginning of
3319 the line's text (after the initial tab).  The @samp{-} is discarded before
3320 the command is passed to the shell for execution.
3322 For example,
3324 @example
3325 @group
3326 clean:
3327         -rm -f *.o
3328 @end group
3329 @end example
3330 @cindex @code{rm} (shell command)
3332 @noindent
3333 This causes @code{rm} to continue even if it is unable to remove a file.
3335 @cindex @code{-i}
3336 @cindex @code{--ignore-errors}
3337 @findex .IGNORE
3338 When you run @code{make} with the @samp{-i} or @samp{--ignore-errors}
3339 flag, errors are ignored in all commands of all rules.  A rule in the
3340 makefile for the special target @code{.IGNORE} has the same effect, if
3341 there are no prerequisites.  These ways of ignoring errors are obsolete
3342 because @samp{-} is more flexible.
3344 When errors are to be ignored, because of either a @samp{-} or the
3345 @samp{-i} flag, @code{make} treats an error return just like success,
3346 except that it prints out a message that tells you the status code
3347 the command exited with, and says that the error has been ignored.
3349 When an error happens that @code{make} has not been told to ignore,
3350 it implies that the current target cannot be correctly remade, and neither
3351 can any other that depends on it either directly or indirectly.  No further
3352 commands will be executed for these targets, since their preconditions
3353 have not been achieved.
3356 @cindex @code{-k}
3357 @cindex @code{--keep-going}
3358 Normally @code{make} gives up immediately in this circumstance, returning a
3359 nonzero status.  However, if the @samp{-k} or @samp{--keep-going}
3360 flag is specified, @code{make}
3361 continues to consider the other prerequisites of the pending targets,
3362 remaking them if necessary, before it gives up and returns nonzero status.
3363 For example, after an error in compiling one object file, @samp{make -k}
3364 will continue compiling other object files even though it already knows
3365 that linking them will be impossible.  @xref{Options Summary, ,Summary of Options}.
3367 The usual behavior assumes that your purpose is to get the specified
3368 targets up to date; once @code{make} learns that this is impossible, it
3369 might as well report the failure immediately.  The @samp{-k} option says
3370 that the real purpose is to test as many of the changes made in the
3371 program as possible, perhaps to find several independent problems so
3372 that you can correct them all before the next attempt to compile.  This
3373 is why Emacs' @code{compile} command passes the @samp{-k} flag by
3374 default.
3375 @cindex Emacs (@code{M-x compile})
3377 @findex .DELETE_ON_ERROR
3378 @cindex deletion of target files
3379 @cindex removal of target files
3380 @cindex target, deleting on error
3381 Usually when a command fails, if it has changed the target file at all,
3382 the file is corrupted and cannot be used---or at least it is not
3383 completely updated.  Yet the file's time stamp says that it is now up to
3384 date, so the next time @code{make} runs, it will not try to update that
3385 file.  The situation is just the same as when the command is killed by a
3386 signal; @pxref{Interrupts}.  So generally the right thing to do is to
3387 delete the target file if the command fails after beginning to change
3388 the file.  @code{make} will do this if @code{.DELETE_ON_ERROR} appears
3389 as a target.  This is almost always what you want @code{make} to do, but
3390 it is not historical practice; so for compatibility, you must explicitly
3391 request it.
3393 @node Interrupts, Recursion, Errors, Commands
3394 @section Interrupting or Killing @code{make}
3395 @cindex interrupt
3396 @cindex signal
3397 @cindex deletion of target files
3398 @cindex removal of target files
3399 @cindex target, deleting on interrupt
3400 @cindex killing (interruption)
3402 If @code{make} gets a fatal signal while a command is executing, it may
3403 delete the target file that the command was supposed to update.  This is
3404 done if the target file's last-modification time has changed since
3405 @code{make} first checked it.
3407 The purpose of deleting the target is to make sure that it is remade from
3408 scratch when @code{make} is next run.  Why is this?  Suppose you type
3409 @kbd{Ctrl-c} while a compiler is running, and it has begun to write an
3410 object file @file{foo.o}.  The @kbd{Ctrl-c} kills the compiler, resulting
3411 in an incomplete file whose last-modification time is newer than the source
3412 file @file{foo.c}.  But @code{make} also receives the @kbd{Ctrl-c} signal
3413 and deletes this incomplete file.  If @code{make} did not do this, the next
3414 invocation of @code{make} would think that @file{foo.o} did not require
3415 updating---resulting in a strange error message from the linker when it
3416 tries to link an object file half of which is missing.
3418 @findex .PRECIOUS
3419 You can prevent the deletion of a target file in this way by making the
3420 special target @code{.PRECIOUS} depend on it.  Before remaking a target,
3421 @code{make} checks to see whether it appears on the prerequisites of
3422 @code{.PRECIOUS}, and thereby decides whether the target should be deleted
3423 if a signal happens.  Some reasons why you might do this are that the
3424 target is updated in some atomic fashion, or exists only to record a
3425 modification-time (its contents do not matter), or must exist at all
3426 times to prevent other sorts of trouble.
3428 @node Recursion, Sequences, Interrupts, Commands
3429 @section Recursive Use of @code{make}
3430 @cindex recursion
3431 @cindex subdirectories, recursion for
3433 Recursive use of @code{make} means using @code{make} as a command in a
3434 makefile.  This technique is useful when you want separate makefiles for
3435 various subsystems that compose a larger system.  For example, suppose you
3436 have a subdirectory @file{subdir} which has its own makefile, and you would
3437 like the containing directory's makefile to run @code{make} on the
3438 subdirectory.  You can do it by writing this:
3440 @example
3441 subsystem:
3442         cd subdir && $(MAKE)
3443 @end example
3445 @noindent
3446 or, equivalently, this (@pxref{Options Summary, ,Summary of Options}):
3448 @example
3449 subsystem:
3450         $(MAKE) -C subdir
3451 @end example
3452 @cindex @code{-C}
3453 @cindex @code{--directory}
3455 You can write recursive @code{make} commands just by copying this example,
3456 but there are many things to know about how they work and why, and about
3457 how the sub-@code{make} relates to the top-level @code{make}.
3459 For your convenience, GNU @code{make} sets the variable @code{CURDIR} to
3460 the pathname of the current working directory for you.  If @code{-C} is
3461 in effect, it will contain the path of the new directory, not the
3462 original.  The value has the same precedence it would have if it were
3463 set in the makefile (by default, an environment variable @code{CURDIR}
3464 will not override this value).  Note that setting this variable has no
3465 effect on the operation of @code{make}
3467 @menu
3468 * MAKE Variable::               The special effects of using @samp{$(MAKE)}.
3469 * Variables/Recursion::         How to communicate variables to a sub-@code{make}.
3470 * Options/Recursion::           How to communicate options to a sub-@code{make}.
3471 * -w Option::                   How the @samp{-w} or @samp{--print-directory} option
3472                                  helps debug use of recursive @code{make} commands.
3473 @end menu
3475 @node MAKE Variable, Variables/Recursion,  , Recursion
3476 @subsection How the @code{MAKE} Variable Works
3477 @vindex MAKE
3478 @cindex recursion, and @code{MAKE} variable
3480 Recursive @code{make} commands should always use the variable @code{MAKE},
3481 not the explicit command name @samp{make}, as shown here:
3483 @example
3484 @group
3485 subsystem:
3486         cd subdir && $(MAKE)
3487 @end group
3488 @end example
3490 The value of this variable is the file name with which @code{make} was
3491 invoked.  If this file name was @file{/bin/make}, then the command executed
3492 is @samp{cd subdir && /bin/make}.  If you use a special version of
3493 @code{make} to run the top-level makefile, the same special version will be
3494 executed for recursive invocations.
3495 @cindex @code{cd} (shell command)
3497 As a special feature, using the variable @code{MAKE} in the commands of
3498 a rule alters the effects of the @samp{-t} (@samp{--touch}), @samp{-n}
3499 (@samp{--just-print}), or @samp{-q} (@w{@samp{--question}}) option.
3500 Using the @code{MAKE} variable has the same effect as using a @samp{+}
3501 character at the beginning of the command line.  @xref{Instead of
3502 Execution, ,Instead of Executing the Commands}.@refill
3504 Consider the command @samp{make -t} in the above example.  (The
3505 @samp{-t} option marks targets as up to date without actually running
3506 any commands; see @ref{Instead of Execution}.)  Following the usual
3507 definition of @samp{-t}, a @samp{make -t} command in the example would
3508 create a file named @file{subsystem} and do nothing else.  What you
3509 really want it to do is run @samp{@w{cd subdir &&} @w{make -t}}; but that would
3510 require executing the command, and @samp{-t} says not to execute
3511 commands.@refill
3512 @cindex @code{-t}, and recursion
3513 @cindex recursion, and @code{-t}
3514 @cindex @code{--touch}, and recursion
3516 The special feature makes this do what you want: whenever a command
3517 line of a rule contains the variable @code{MAKE}, the flags @samp{-t},
3518 @samp{-n} and @samp{-q} do not apply to that line.  Command lines
3519 containing @code{MAKE} are executed normally despite the presence of a
3520 flag that causes most commands not to be run.  The usual
3521 @code{MAKEFLAGS} mechanism passes the flags to the sub-@code{make}
3522 (@pxref{Options/Recursion, ,Communicating Options to a
3523 Sub-@code{make}}), so your request to touch the files, or print the
3524 commands, is propagated to the subsystem.@refill
3526 @node Variables/Recursion, Options/Recursion, MAKE Variable, Recursion
3527 @subsection Communicating Variables to a Sub-@code{make}
3528 @cindex sub-@code{make}
3529 @cindex environment, and recursion
3530 @cindex exporting variables
3531 @cindex variables, environment
3532 @cindex variables, exporting
3533 @cindex recursion, and environment
3534 @cindex recursion, and variables
3536 Variable values of the top-level @code{make} can be passed to the
3537 sub-@code{make} through the environment by explicit request.  These
3538 variables are defined in the sub-@code{make} as defaults, but do not
3539 override what is specified in the makefile used by the sub-@code{make}
3540 makefile unless you use the @samp{-e} switch (@pxref{Options Summary,
3541 ,Summary of Options}).@refill
3543 To pass down, or @dfn{export}, a variable, @code{make} adds the variable
3544 and its value to the environment for running each command.  The
3545 sub-@code{make}, in turn, uses the environment to initialize its table
3546 of variable values.  @xref{Environment, ,Variables from the
3547 Environment}.
3549 Except by explicit request, @code{make} exports a variable only if it
3550 is either defined in the environment initially or set on the command
3551 line, and if its name consists only of letters, numbers, and underscores.
3552 Some shells cannot cope with environment variable names consisting of
3553 characters other than letters, numbers, and underscores.
3555 The special variables @code{SHELL} and @code{MAKEFLAGS} are always
3556 exported (unless you unexport them).
3557 @code{MAKEFILES} is exported if you set it to anything.
3559 @code{make} automatically passes down variable values that were defined
3560 on the command line, by putting them in the @code{MAKEFLAGS} variable.
3561 @iftex
3562 See the next section.
3563 @end iftex
3564 @ifinfo
3565 @xref{Options/Recursion}.
3566 @end ifinfo
3568 Variables are @emph{not} normally passed down if they were created by
3569 default by @code{make} (@pxref{Implicit Variables, ,Variables Used by
3570 Implicit Rules}).  The sub-@code{make} will define these for
3571 itself.@refill
3573 @findex export
3574 If you want to export specific variables to a sub-@code{make}, use the
3575 @code{export} directive, like this:
3577 @example
3578 export @var{variable} @dots{}
3579 @end example
3581 @noindent
3582 @findex unexport
3583 If you want to @emph{prevent} a variable from being exported, use the
3584 @code{unexport} directive, like this:
3586 @example
3587 unexport @var{variable} @dots{}
3588 @end example
3590 @noindent
3591 As a convenience, you can define a variable and export it at the same
3592 time by doing:
3594 @example
3595 export @var{variable} = value
3596 @end example
3598 @noindent
3599 has the same result as:
3601 @example
3602 @var{variable} = value
3603 export @var{variable}
3604 @end example
3606 @noindent
3609 @example
3610 export @var{variable} := value
3611 @end example
3613 @noindent
3614 has the same result as:
3616 @example
3617 @var{variable} := value
3618 export @var{variable}
3619 @end example
3621 Likewise,
3623 @example
3624 export @var{variable} += value
3625 @end example
3627 @noindent
3628 is just like:
3630 @example
3631 @var{variable} += value
3632 export @var{variable}
3633 @end example
3635 @noindent
3636 @xref{Appending, ,Appending More Text to Variables}.
3638 You may notice that the @code{export} and @code{unexport} directives
3639 work in @code{make} in the same way they work in the shell, @code{sh}.
3641 If you want all variables to be exported by default, you can use
3642 @code{export} by itself:
3644 @example
3645 export
3646 @end example
3648 @noindent
3649 This tells @code{make} that variables which are not explicitly mentioned
3650 in an @code{export} or @code{unexport} directive should be exported.
3651 Any variable given in an @code{unexport} directive will still @emph{not}
3652 be exported.  If you use @code{export} by itself to export variables by
3653 default, variables whose names contain characters other than
3654 alphanumerics and underscores will not be exported unless specifically
3655 mentioned in an @code{export} directive.@refill
3657 @findex .EXPORT_ALL_VARIABLES
3658 The behavior elicited by an @code{export} directive by itself was the
3659 default in older versions of GNU @code{make}.  If your makefiles depend
3660 on this behavior and you want to be compatible with old versions of
3661 @code{make}, you can write a rule for the special target
3662 @code{.EXPORT_ALL_VARIABLES} instead of using the @code{export} directive.
3663 This will be ignored by old @code{make}s, while the @code{export}
3664 directive will cause a syntax error.@refill
3665 @cindex compatibility in exporting
3667 Likewise, you can use @code{unexport} by itself to tell @code{make}
3668 @emph{not} to export variables by default.  Since this is the default
3669 behavior, you would only need to do this if @code{export} had been used
3670 by itself earlier (in an included makefile, perhaps).  You
3671 @strong{cannot} use @code{export} and @code{unexport} by themselves to
3672 have variables exported for some commands and not for others.  The last
3673 @code{export} or @code{unexport} directive that appears by itself
3674 determines the behavior for the entire run of @code{make}.@refill
3676 @vindex MAKELEVEL
3677 @cindex recursion, level of
3678 As a special feature, the variable @code{MAKELEVEL} is changed when it
3679 is passed down from level to level.  This variable's value is a string
3680 which is the depth of the level as a decimal number.  The value is
3681 @samp{0} for the top-level @code{make}; @samp{1} for a sub-@code{make},
3682 @samp{2} for a sub-sub-@code{make}, and so on.  The incrementation
3683 happens when @code{make} sets up the environment for a command.@refill
3685 The main use of @code{MAKELEVEL} is to test it in a conditional
3686 directive (@pxref{Conditionals, ,Conditional Parts of Makefiles}); this
3687 way you can write a makefile that behaves one way if run recursively and
3688 another way if run directly by you.@refill
3690 @vindex MAKEFILES
3691 You can use the variable @code{MAKEFILES} to cause all sub-@code{make}
3692 commands to use additional makefiles.  The value of @code{MAKEFILES} is
3693 a whitespace-separated list of file names.  This variable, if defined in
3694 the outer-level makefile, is passed down through the environment; then
3695 it serves as a list of extra makefiles for the sub-@code{make} to read
3696 before the usual or specified ones.  @xref{MAKEFILES Variable, ,The
3697 Variable @code{MAKEFILES}}.@refill
3699 @node Options/Recursion, -w Option, Variables/Recursion, Recursion
3700 @subsection Communicating Options to a Sub-@code{make}
3701 @cindex options, and recursion
3702 @cindex recursion, and options
3704 @vindex MAKEFLAGS
3705 Flags such as @samp{-s} and @samp{-k} are passed automatically to the
3706 sub-@code{make} through the variable @code{MAKEFLAGS}.  This variable is
3707 set up automatically by @code{make} to contain the flag letters that
3708 @code{make} received.  Thus, if you do @w{@samp{make -ks}} then
3709 @code{MAKEFLAGS} gets the value @samp{ks}.@refill
3711 As a consequence, every sub-@code{make} gets a value for @code{MAKEFLAGS}
3712 in its environment.  In response, it takes the flags from that value and
3713 processes them as if they had been given as arguments.
3714 @xref{Options Summary, ,Summary of Options}.
3716 @cindex command line variable definitions, and recursion
3717 @cindex variables, command line, and recursion
3718 @cindex recursion, and command line variable definitions
3719 Likewise variables defined on the command line are passed to the
3720 sub-@code{make} through @code{MAKEFLAGS}.  Words in the value of
3721 @code{MAKEFLAGS} that contain @samp{=}, @code{make} treats as variable
3722 definitions just as if they appeared on the command line.
3723 @xref{Overriding, ,Overriding Variables}.
3725 @cindex @code{-C}, and recursion
3726 @cindex @code{-f}, and recursion
3727 @cindex @code{-o}, and recursion
3728 @cindex @code{-W}, and recursion
3729 @cindex @code{--directory}, and recursion
3730 @cindex @code{--file}, and recursion
3731 @cindex @code{--old-file}, and recursion
3732 @cindex @code{--assume-old}, and recursion
3733 @cindex @code{--assume-new}, and recursion
3734 @cindex @code{--new-file}, and recursion
3735 @cindex recursion, and @code{-C}
3736 @cindex recursion, and @code{-f}
3737 @cindex recursion, and @code{-o}
3738 @cindex recursion, and @code{-W}
3739 The options @samp{-C}, @samp{-f}, @samp{-o}, and @samp{-W} are not put
3740 into @code{MAKEFLAGS}; these options are not passed down.@refill
3742 @cindex @code{-j}, and recursion
3743 @cindex @code{--jobs}, and recursion
3744 @cindex recursion, and @code{-j}
3745 @cindex job slots, and recursion
3746 The @samp{-j} option is a special case (@pxref{Parallel, ,Parallel Execution}).
3747 If you set it to some numeric value @samp{N} and your operating system
3748 supports it (most any UNIX system will; others typically won't), the
3749 parent @code{make} and all the sub-@code{make}s will communicate to
3750 ensure that there are only @samp{N} jobs running at the same time
3751 between them all.  Note that any job that is marked recursive
3752 (@pxref{Instead of Execution, ,Instead of Executing the Commands})
3753 doesn't count against the total jobs (otherwise we could get @samp{N}
3754 sub-@code{make}s running and have no slots left over for any real work!)
3756 If your operating system doesn't support the above communication, then
3757 @samp{-j 1} is always put into @code{MAKEFLAGS} instead of the value you
3758 specified.  This is because if the @w{@samp{-j}} option were passed down
3759 to sub-@code{make}s, you would get many more jobs running in parallel
3760 than you asked for.  If you give @samp{-j} with no numeric argument,
3761 meaning to run as many jobs as possible in parallel, this is passed
3762 down, since multiple infinities are no more than one.@refill
3764 If you do not want to pass the other flags down, you must change the
3765 value of @code{MAKEFLAGS}, like this:
3767 @example
3768 subsystem:
3769         cd subdir && $(MAKE) MAKEFLAGS=
3770 @end example
3772 @vindex MAKEOVERRIDES
3773 The command line variable definitions really appear in the variable
3774 @code{MAKEOVERRIDES}, and @code{MAKEFLAGS} contains a reference to this
3775 variable.  If you do want to pass flags down normally, but don't want to
3776 pass down the command line variable definitions, you can reset
3777 @code{MAKEOVERRIDES} to empty, like this:
3779 @example
3780 MAKEOVERRIDES =
3781 @end example
3783 @noindent
3784 @cindex Arg list too long
3785 @cindex E2BIG
3786 This is not usually useful to do.  However, some systems have a small
3787 fixed limit on the size of the environment, and putting so much
3788 information into the value of @code{MAKEFLAGS} can exceed it.  If you
3789 see the error message @samp{Arg list too long}, this may be the problem.
3790 @findex .POSIX
3791 @cindex POSIX.2
3792 (For strict compliance with POSIX.2, changing @code{MAKEOVERRIDES} does
3793 not affect @code{MAKEFLAGS} if the special target @samp{.POSIX} appears
3794 in the makefile.  You probably do not care about this.)
3796 @vindex MFLAGS
3797 A similar variable @code{MFLAGS} exists also, for historical
3798 compatibility.  It has the same value as @code{MAKEFLAGS} except that it
3799 does not contain the command line variable definitions, and it always
3800 begins with a hyphen unless it is empty (@code{MAKEFLAGS} begins with a
3801 hyphen only when it begins with an option that has no single-letter
3802 version, such as @samp{--warn-undefined-variables}).  @code{MFLAGS} was
3803 traditionally used explicitly in the recursive @code{make} command, like
3804 this:
3806 @example
3807 subsystem:
3808         cd subdir && $(MAKE) $(MFLAGS)
3809 @end example
3811 @noindent
3812 but now @code{MAKEFLAGS} makes this usage redundant.  If you want your
3813 makefiles to be compatible with old @code{make} programs, use this
3814 technique; it will work fine with more modern @code{make} versions too.
3816 @cindex setting options from environment
3817 @cindex options, setting from environment
3818 @cindex setting options in makefiles
3819 @cindex options, setting in makefiles
3820 The @code{MAKEFLAGS} variable can also be useful if you want to have
3821 certain options, such as @samp{-k} (@pxref{Options Summary, ,Summary of
3822 Options}), set each time you run @code{make}.  You simply put a value for
3823 @code{MAKEFLAGS} in your environment.  You can also set @code{MAKEFLAGS} in
3824 a makefile, to specify additional flags that should also be in effect for
3825 that makefile.  (Note that you cannot use @code{MFLAGS} this way.  That
3826 variable is set only for compatibility; @code{make} does not interpret a
3827 value you set for it in any way.)
3829 When @code{make} interprets the value of @code{MAKEFLAGS} (either from the
3830 environment or from a makefile), it first prepends a hyphen if the value
3831 does not already begin with one.  Then it chops the value into words
3832 separated by blanks, and parses these words as if they were options given
3833 on the command line (except that @samp{-C}, @samp{-f}, @samp{-h},
3834 @samp{-o}, @samp{-W}, and their long-named versions are ignored; and there
3835 is no error for an invalid option).
3837 If you do put @code{MAKEFLAGS} in your environment, you should be sure not
3838 to include any options that will drastically affect the actions of
3839 @code{make} and undermine the purpose of makefiles and of @code{make}
3840 itself.  For instance, the @samp{-t}, @samp{-n}, and @samp{-q} options, if
3841 put in one of these variables, could have disastrous consequences and would
3842 certainly have at least surprising and probably annoying effects.@refill
3844 @node -w Option,  , Options/Recursion, Recursion
3845 @subsection The @samp{--print-directory} Option
3846 @cindex directories, printing them
3847 @cindex printing directories
3848 @cindex recursion, and printing directories
3850 If you use several levels of recursive @code{make} invocations, the
3851 @samp{-w} or @w{@samp{--print-directory}} option can make the output a
3852 lot easier to understand by showing each directory as @code{make}
3853 starts processing it and as @code{make} finishes processing it.  For
3854 example, if @samp{make -w} is run in the directory @file{/u/gnu/make},
3855 @code{make} will print a line of the form:@refill
3857 @example
3858 make: Entering directory `/u/gnu/make'.
3859 @end example
3861 @noindent
3862 before doing anything else, and a line of the form:
3864 @example
3865 make: Leaving directory `/u/gnu/make'.
3866 @end example
3868 @noindent
3869 when processing is completed.
3871 @cindex @code{-C}, and @code{-w}
3872 @cindex @code{--directory}, and @code{--print-directory}
3873 @cindex recursion, and @code{-w}
3874 @cindex @code{-w}, and @code{-C}
3875 @cindex @code{-w}, and recursion
3876 @cindex @code{--print-directory}, and @code{--directory}
3877 @cindex @code{--print-directory}, and recursion
3878 @cindex @code{--no-print-directory}
3879 @cindex @code{--print-directory}, disabling
3880 @cindex @code{-w}, disabling
3881 Normally, you do not need to specify this option because @samp{make}
3882 does it for you: @samp{-w} is turned on automatically when you use the
3883 @samp{-C} option, and in sub-@code{make}s.  @code{make} will not
3884 automatically turn on @samp{-w} if you also use @samp{-s}, which says to
3885 be silent, or if you use @samp{--no-print-directory} to explicitly
3886 disable it.
3888 @node Sequences, Empty Commands, Recursion, Commands
3889 @section Defining Canned Command Sequences
3890 @cindex sequences of commands
3891 @cindex commands, sequences of
3893 When the same sequence of commands is useful in making various targets, you
3894 can define it as a canned sequence with the @code{define} directive, and
3895 refer to the canned sequence from the rules for those targets.  The canned
3896 sequence is actually a variable, so the name must not conflict with other
3897 variable names.
3899 Here is an example of defining a canned sequence of commands:
3901 @example
3902 define run-yacc
3903 yacc $(firstword $^)
3904 mv y.tab.c $@@
3905 endef
3906 @end example
3907 @cindex @code{yacc}
3909 @noindent
3910 Here @code{run-yacc} is the name of the variable being defined;
3911 @code{endef} marks the end of the definition; the lines in between are the
3912 commands.  The @code{define} directive does not expand variable references
3913 and function calls in the canned sequence; the @samp{$} characters,
3914 parentheses, variable names, and so on, all become part of the value of the
3915 variable you are defining.
3916 @xref{Defining, ,Defining Variables Verbatim},
3917 for a complete explanation of @code{define}.
3919 The first command in this example runs Yacc on the first prerequisite of
3920 whichever rule uses the canned sequence.  The output file from Yacc is
3921 always named @file{y.tab.c}.  The second command moves the output to the
3922 rule's target file name.
3924 To use the canned sequence, substitute the variable into the commands of a
3925 rule.  You can substitute it like any other variable
3926 (@pxref{Reference, ,Basics of Variable References}).
3927 Because variables defined by @code{define} are recursively expanded
3928 variables, all the variable references you wrote inside the @code{define}
3929 are expanded now.  For example:
3931 @example
3932 foo.c : foo.y
3933         $(run-yacc)
3934 @end example
3936 @noindent
3937 @samp{foo.y} will be substituted for the variable @samp{$^} when it occurs in
3938 @code{run-yacc}'s value, and @samp{foo.c} for @samp{$@@}.@refill
3940 This is a realistic example, but this particular one is not needed in
3941 practice because @code{make} has an implicit rule to figure out these
3942 commands based on the file names involved
3943 (@pxref{Implicit Rules, ,Using Implicit Rules}).
3945 @cindex @@, and @code{define}
3946 @cindex -, and @code{define}
3947 @cindex +, and @code{define}
3948 In command execution, each line of a canned sequence is treated just as
3949 if the line appeared on its own in the rule, preceded by a tab.  In
3950 particular, @code{make} invokes a separate subshell for each line.  You
3951 can use the special prefix characters that affect command lines
3952 (@samp{@@}, @samp{-}, and @samp{+}) on each line of a canned sequence.
3953 @xref{Commands, ,Writing the Commands in Rules}.
3954 For example, using this canned sequence:
3956 @example
3957 define frobnicate
3958 @@echo "frobnicating target $@@"
3959 frob-step-1 $< -o $@@-step-1
3960 frob-step-2 $@@-step-1 -o $@@
3961 endef
3962 @end example
3964 @noindent
3965 @code{make} will not echo the first line, the @code{echo} command.
3966 But it @emph{will} echo the following two command lines.
3968 On the other hand, prefix characters on the command line that refers to
3969 a canned sequence apply to every line in the sequence.  So the rule:
3971 @example
3972 frob.out: frob.in
3973         @@$(frobnicate)
3974 @end example
3976 @noindent
3977 does not echo @emph{any} commands.
3978 (@xref{Echoing, ,Command Echoing}, for a full explanation of @samp{@@}.)
3980 @node Empty Commands,  , Sequences, Commands
3981 @section Using Empty Commands
3982 @cindex empty commands
3983 @cindex commands, empty
3985 It is sometimes useful to define commands which do nothing.  This is done
3986 simply by giving a command that consists of nothing but whitespace.  For
3987 example:
3989 @example
3990 target: ;
3991 @end example
3993 @noindent
3994 defines an empty command string for @file{target}.  You could also use a
3995 line beginning with a tab character to define an empty command string,
3996 but this would be confusing because such a line looks empty.
3998 @findex .DEFAULT@r{, and empty commands}
3999 You may be wondering why you would want to define a command string that
4000 does nothing.  The only reason this is useful is to prevent a target
4001 from getting implicit commands (from implicit rules or the
4002 @code{.DEFAULT} special target; @pxref{Implicit Rules} and
4003 @pxref{Last Resort, ,Defining Last-Resort Default Rules}).@refill
4005 @c !!! another reason is for canonical stamp files:
4006 @ignore
4007 foo: stamp-foo ;
4008 stamp-foo: foo.in
4009         create foo frm foo.in
4010         touch $@
4011 @end ignore
4013 You may be inclined to define empty command strings for targets that are
4014 not actual files, but only exist so that their prerequisites can be
4015 remade.  However, this is not the best way to do that, because the
4016 prerequisites may not be remade properly if the target file actually does exist.
4017 @xref{Phony Targets, ,Phony Targets}, for a better way to do this.
4019 @node Using Variables, Conditionals, Commands, Top
4020 @chapter How to Use Variables
4021 @cindex variable
4022 @cindex value
4023 @cindex recursive variable expansion
4024 @cindex simple variable expansion
4026 A @dfn{variable} is a name defined in a makefile to represent a string
4027 of text, called the variable's @dfn{value}.  These values are
4028 substituted by explicit request into targets, prerequisites, commands,
4029 and other parts of the makefile.  (In some other versions of @code{make},
4030 variables are called @dfn{macros}.)
4031 @cindex macro
4033 Variables and functions in all parts of a makefile are expanded when
4034 read, except for the shell commands in rules, the right-hand sides of
4035 variable definitions using @samp{=}, and the bodies of variable
4036 definitions using the @code{define} directive.@refill
4038 Variables can represent lists of file names, options to pass to compilers,
4039 programs to run, directories to look in for source files, directories to
4040 write output in, or anything else you can imagine.
4042 A variable name may be any sequence of characters not containing @samp{:},
4043 @samp{#}, @samp{=}, or leading or trailing whitespace.  However,
4044 variable names containing characters other than letters, numbers, and
4045 underscores should be avoided, as they may be given special meanings in the
4046 future, and with some shells they cannot be passed through the environment to a
4047 sub-@code{make}
4048 (@pxref{Variables/Recursion, ,Communicating Variables to a Sub-@code{make}}).
4050 Variable names are case-sensitive.  The names @samp{foo}, @samp{FOO},
4051 and @samp{Foo} all refer to different variables.
4053 It is traditional to use upper case letters in variable names, but we
4054 recommend using lower case letters for variable names that serve internal
4055 purposes in the makefile, and reserving upper case for parameters that
4056 control implicit rules or for parameters that the user should override with
4057 command options (@pxref{Overriding, ,Overriding Variables}).
4059 A few variables have names that are a single punctuation character or
4060 just a few characters.  These are the @dfn{automatic variables}, and
4061 they have particular specialized uses.  @xref{Automatic, ,Automatic Variables}.
4063 @menu
4064 * Reference::                   How to use the value of a variable.
4065 * Flavors::                     Variables come in two flavors.
4066 * Advanced::                    Advanced features for referencing a variable.
4067 * Values::                      All the ways variables get their values.
4068 * Setting::                     How to set a variable in the makefile.
4069 * Appending::                   How to append more text to the old value
4070                                   of a variable.
4071 * Override Directive::          How to set a variable in the makefile even if
4072                                   the user has set it with a command argument.
4073 * Defining::                    An alternate way to set a variable
4074                                   to a verbatim string.
4075 * Environment::                 Variable values can come from the environment.
4076 * Target-specific::             Variable values can be defined on a per-target
4077                                   basis.
4078 * Pattern-specific::            Target-specific variable values can be applied
4079                                   to a group of targets that match a pattern.
4080 * Automatic::                   Some special variables have predefined
4081                                   meanings for use with implicit rules.
4082 @end menu
4084 @node Reference, Flavors,  , Using Variables
4085 @section Basics of Variable References
4086 @cindex variables, how to reference
4087 @cindex reference to variables
4088 @cindex @code{$}, in variable reference
4089 @cindex dollar sign (@code{$}), in variable reference
4091 To substitute a variable's value, write a dollar sign followed by the name
4092 of the variable in parentheses or braces: either @samp{$(foo)} or
4093 @samp{$@{foo@}} is a valid reference to the variable @code{foo}.  This
4094 special significance of @samp{$} is why you must write @samp{$$} to have
4095 the effect of a single dollar sign in a file name or command.
4097 Variable references can be used in any context: targets, prerequisites,
4098 commands, most directives, and new variable values.  Here is an
4099 example of a common case, where a variable holds the names of all the
4100 object files in a program:
4102 @example
4103 @group
4104 objects = program.o foo.o utils.o
4105 program : $(objects)
4106         cc -o program $(objects)
4108 $(objects) : defs.h
4109 @end group
4110 @end example
4112 Variable references work by strict textual substitution.  Thus, the rule
4114 @example
4115 @group
4116 foo = c
4117 prog.o : prog.$(foo)
4118         $(foo)$(foo) -$(foo) prog.$(foo)
4119 @end group
4120 @end example
4122 @noindent
4123 could be used to compile a C program @file{prog.c}.  Since spaces before
4124 the variable value are ignored in variable assignments, the value of
4125 @code{foo} is precisely @samp{c}.  (Don't actually write your makefiles
4126 this way!)
4128 A dollar sign followed by a character other than a dollar sign,
4129 open-parenthesis or open-brace treats that single character as the
4130 variable name.  Thus, you could reference the variable @code{x} with
4131 @samp{$x}.  However, this practice is strongly discouraged, except in
4132 the case of the automatic variables (@pxref{Automatic, ,Automatic Variables}).
4134 @node Flavors, Advanced, Reference, Using Variables
4135 @section The Two Flavors of Variables
4136 @cindex flavors of variables
4137 @cindex recursive variable expansion
4138 @cindex variables, flavors
4139 @cindex recursively expanded variables
4140 @cindex variables, recursively expanded
4142 There are two ways that a variable in GNU @code{make} can have a value;
4143 we call them the two @dfn{flavors} of variables.  The two flavors are
4144 distinguished in how they are defined and in what they do when expanded.
4146 @cindex =
4147 The first flavor of variable is a @dfn{recursively expanded} variable.
4148 Variables of this sort are defined by lines using @samp{=}
4149 (@pxref{Setting, ,Setting Variables}) or by the @code{define} directive
4150 (@pxref{Defining, ,Defining Variables Verbatim}).  The value you specify
4151 is installed verbatim; if it contains references to other variables,
4152 these references are expanded whenever this variable is substituted (in
4153 the course of expanding some other string).  When this happens, it is
4154 called @dfn{recursive expansion}.@refill
4156 For example,
4158 @example
4159 foo = $(bar)
4160 bar = $(ugh)
4161 ugh = Huh?
4163 all:;echo $(foo)
4164 @end example
4166 @noindent
4167 will echo @samp{Huh?}: @samp{$(foo)} expands to @samp{$(bar)} which
4168 expands to @samp{$(ugh)} which finally expands to @samp{Huh?}.@refill
4170 This flavor of variable is the only sort supported by other versions of
4171 @code{make}.  It has its advantages and its disadvantages.  An advantage
4172 (most would say) is that:
4174 @example
4175 CFLAGS = $(include_dirs) -O
4176 include_dirs = -Ifoo -Ibar
4177 @end example
4179 @noindent
4180 will do what was intended: when @samp{CFLAGS} is expanded in a command,
4181 it will expand to @samp{-Ifoo -Ibar -O}.  A major disadvantage is that you
4182 cannot append something on the end of a variable, as in
4184 @example
4185 CFLAGS = $(CFLAGS) -O
4186 @end example
4188 @noindent
4189 because it will cause an infinite loop in the variable expansion.
4190 (Actually @code{make} detects the infinite loop and reports an error.)
4191 @cindex loops in variable expansion
4192 @cindex variables, loops in expansion
4194 Another disadvantage is that any functions
4195 (@pxref{Functions, ,Functions for Transforming Text})
4196 referenced in the definition will be executed every time the variable is
4197 expanded.  This makes @code{make} run slower; worse, it causes the
4198 @code{wildcard} and @code{shell} functions to give unpredictable results
4199 because you cannot easily control when they are called, or even how many
4200 times.
4202 To avoid all the problems and inconveniences of recursively expanded
4203 variables, there is another flavor: simply expanded variables.
4205 @cindex simply expanded variables
4206 @cindex variables, simply expanded
4207 @cindex :=
4208 @dfn{Simply expanded variables} are defined by lines using @samp{:=}
4209 (@pxref{Setting, ,Setting Variables}).
4210 The value of a simply expanded variable is scanned
4211 once and for all, expanding any references to other variables and
4212 functions, when the variable is defined.  The actual value of the simply
4213 expanded variable is the result of expanding the text that you write.
4214 It does not contain any references to other variables; it contains their
4215 values @emph{as of the time this variable was defined}.  Therefore,
4217 @example
4218 x := foo
4219 y := $(x) bar
4220 x := later
4221 @end example
4223 @noindent
4224 is equivalent to
4226 @example
4227 y := foo bar
4228 x := later
4229 @end example
4231 When a simply expanded variable is referenced, its value is substituted
4232 verbatim.
4234 Here is a somewhat more complicated example, illustrating the use of
4235 @samp{:=} in conjunction with the @code{shell} function.
4236 (@xref{Shell Function, , The @code{shell} Function}.)  This example
4237 also shows use of the variable @code{MAKELEVEL}, which is changed
4238 when it is passed down from level to level.
4239 (@xref{Variables/Recursion, , Communicating Variables to a
4240 Sub-@code{make}}, for information about @code{MAKELEVEL}.)
4242 @vindex MAKELEVEL
4243 @vindex MAKE
4244 @example
4245 @group
4246 ifeq (0,$@{MAKELEVEL@})
4247 cur-dir   := $(shell pwd)
4248 whoami    := $(shell whoami)
4249 host-type := $(shell arch)
4250 MAKE := $@{MAKE@} host-type=$@{host-type@} whoami=$@{whoami@}
4251 endif
4252 @end group
4253 @end example
4255 @noindent
4256 An advantage of this use of @samp{:=} is that a typical
4257 `descend into a directory' command then looks like this:
4259 @example
4260 @group
4261 $@{subdirs@}:
4262       $@{MAKE@} cur-dir=$@{cur-dir@}/$@@ -C $@@ all
4263 @end group
4264 @end example
4266 Simply expanded variables generally make complicated makefile programming
4267 more predictable because they work like variables in most programming
4268 languages.  They allow you to redefine a variable using its own value (or
4269 its value processed in some way by one of the expansion functions) and to
4270 use the expansion functions much more efficiently
4271 (@pxref{Functions, ,Functions for Transforming Text}).
4273 @cindex spaces, in variable values
4274 @cindex whitespace, in variable values
4275 @cindex variables, spaces in values
4276 You can also use them to introduce controlled leading whitespace into
4277 variable values.  Leading whitespace characters are discarded from your
4278 input before substitution of variable references and function calls;
4279 this means you can include leading spaces in a variable value by
4280 protecting them with variable references, like this:
4282 @example
4283 nullstring :=
4284 space := $(nullstring) # end of the line
4285 @end example
4287 @noindent
4288 Here the value of the variable @code{space} is precisely one space.  The
4289 comment @w{@samp{# end of the line}} is included here just for clarity.
4290 Since trailing space characters are @emph{not} stripped from variable
4291 values, just a space at the end of the line would have the same effect
4292 (but be rather hard to read).  If you put whitespace at the end of a
4293 variable value, it is a good idea to put a comment like that at the end
4294 of the line to make your intent clear.  Conversely, if you do @emph{not}
4295 want any whitespace characters at the end of your variable value, you
4296 must remember not to put a random comment on the end of the line after
4297 some whitespace, such as this:
4299 @example
4300 dir := /foo/bar    # directory to put the frobs in
4301 @end example
4303 @noindent
4304 Here the value of the variable @code{dir} is @w{@samp{/foo/bar    }}
4305 (with four trailing spaces), which was probably not the intention.
4306 (Imagine something like @w{@samp{$(dir)/file}} with this definition!)
4308 @cindex conditional variable assignment
4309 @cindex variables, conditional assignment
4310 @cindex ?=
4311 There is another assignment operator for variables, @samp{?=}.  This
4312 is called a conditional variable assignment operator, because it only
4313 has an effect if the variable is not yet defined.  This statement:
4315 @example
4316 FOO ?= bar
4317 @end example
4319 @noindent
4320 is exactly equivalent to this
4321 (@pxref{Origin Function, ,The @code{origin} Function}):
4323 @example
4324 ifeq ($(origin FOO), undefined)
4325   FOO = bar
4326 endif
4327 @end example
4329 Note that a variable set to an empty value is still defined, so
4330 @samp{?=} will not set that variable.
4332 @node Advanced, Values, Flavors, Using Variables
4333 @section Advanced Features for Reference to Variables
4334 @cindex reference to variables
4336 This section describes some advanced features you can use to reference
4337 variables in more flexible ways.
4339 @menu
4340 * Substitution Refs::           Referencing a variable with
4341                                   substitutions on the value.
4342 * Computed Names::              Computing the name of the variable to refer to.
4343 @end menu
4345 @node Substitution Refs, Computed Names,  , Advanced
4346 @subsection Substitution References
4347 @cindex modified variable reference
4348 @cindex substitution variable reference
4349 @cindex variables, modified reference
4350 @cindex variables, substitution reference
4352 @cindex variables, substituting suffix in
4353 @cindex suffix, substituting in variables
4354 A @dfn{substitution reference} substitutes the value of a variable with
4355 alterations that you specify.  It has the form
4356 @samp{$(@var{var}:@var{a}=@var{b})} (or
4357 @samp{$@{@var{var}:@var{a}=@var{b}@}}) and its meaning is to take the value
4358 of the variable @var{var}, replace every @var{a} at the end of a word with
4359 @var{b} in that value, and substitute the resulting string.
4361 When we say ``at the end of a word'', we mean that @var{a} must appear
4362 either followed by whitespace or at the end of the value in order to be
4363 replaced; other occurrences of @var{a} in the value are unaltered.  For
4364 example:@refill
4366 @example
4367 foo := a.o b.o c.o
4368 bar := $(foo:.o=.c)
4369 @end example
4371 @noindent
4372 sets @samp{bar} to @samp{a.c b.c c.c}.  @xref{Setting, ,Setting Variables}.
4374 A substitution reference is actually an abbreviation for use of the
4375 @code{patsubst} expansion function (@pxref{Text Functions, ,Functions for String Substitution and Analysis}).  We provide
4376 substitution references as well as @code{patsubst} for compatibility with
4377 other implementations of @code{make}.
4379 @findex patsubst
4380 Another type of substitution reference lets you use the full power of
4381 the @code{patsubst} function.  It has the same form
4382 @samp{$(@var{var}:@var{a}=@var{b})} described above, except that now
4383 @var{a} must contain a single @samp{%} character.  This case is
4384 equivalent to @samp{$(patsubst @var{a},@var{b},$(@var{var}))}.
4385 @xref{Text Functions, ,Functions for String Substitution and Analysis},
4386 for a description of the @code{patsubst} function.@refill
4388 @example
4389 @group
4390 @exdent For example:
4392 foo := a.o b.o c.o
4393 bar := $(foo:%.o=%.c)
4394 @end group
4395 @end example
4397 @noindent
4398 sets @samp{bar} to @samp{a.c b.c c.c}.
4400 @node Computed Names,  , Substitution Refs, Advanced
4401 @subsection Computed Variable Names
4402 @cindex nested variable reference
4403 @cindex computed variable name
4404 @cindex variables, computed names
4405 @cindex variables, nested references
4406 @cindex variables, @samp{$} in name
4407 @cindex @code{$}, in variable name
4408 @cindex dollar sign (@code{$}), in variable name
4410 Computed variable names are a complicated concept needed only for
4411 sophisticated makefile programming.  For most purposes you need not
4412 consider them, except to know that making a variable with a dollar sign
4413 in its name might have strange results.  However, if you are the type
4414 that wants to understand everything, or you are actually interested in
4415 what they do, read on.
4417 Variables may be referenced inside the name of a variable.  This is
4418 called a @dfn{computed variable name} or a @dfn{nested variable
4419 reference}.  For example,
4421 @example
4422 x = y
4423 y = z
4424 a := $($(x))
4425 @end example
4427 @noindent
4428 defines @code{a} as @samp{z}: the @samp{$(x)} inside @samp{$($(x))} expands
4429 to @samp{y}, so @samp{$($(x))} expands to @samp{$(y)} which in turn expands
4430 to @samp{z}.  Here the name of the variable to reference is not stated
4431 explicitly; it is computed by expansion of @samp{$(x)}.  The reference
4432 @samp{$(x)} here is nested within the outer variable reference.
4434 The previous example shows two levels of nesting, but any number of levels
4435 is possible.  For example, here are three levels:
4437 @example
4438 x = y
4439 y = z
4440 z = u
4441 a := $($($(x)))
4442 @end example
4444 @noindent
4445 Here the innermost @samp{$(x)} expands to @samp{y}, so @samp{$($(x))}
4446 expands to @samp{$(y)} which in turn expands to @samp{z}; now we have
4447 @samp{$(z)}, which becomes @samp{u}.
4449 References to recursively-expanded variables within a variable name are
4450 reexpanded in the usual fashion.  For example:
4452 @example
4453 x = $(y)
4454 y = z
4455 z = Hello
4456 a := $($(x))
4457 @end example
4459 @noindent
4460 defines @code{a} as @samp{Hello}: @samp{$($(x))} becomes @samp{$($(y))}
4461 which becomes @samp{$(z)} which becomes @samp{Hello}.
4463 Nested variable references can also contain modified references and
4464 function invocations (@pxref{Functions, ,Functions for Transforming Text}),
4465 just like any other reference.
4466 For example, using the @code{subst} function
4467 (@pxref{Text Functions, ,Functions for String Substitution and Analysis}):
4469 @example
4470 @group
4471 x = variable1
4472 variable2 := Hello
4473 y = $(subst 1,2,$(x))
4474 z = y
4475 a := $($($(z)))
4476 @end group
4477 @end example
4479 @noindent
4480 eventually defines @code{a} as @samp{Hello}.  It is doubtful that anyone
4481 would ever want to write a nested reference as convoluted as this one, but
4482 it works: @samp{$($($(z)))} expands to @samp{$($(y))} which becomes
4483 @samp{$($(subst 1,2,$(x)))}.  This gets the value @samp{variable1} from
4484 @code{x} and changes it by substitution to @samp{variable2}, so that the
4485 entire string becomes @samp{$(variable2)}, a simple variable reference
4486 whose value is @samp{Hello}.@refill
4488 A computed variable name need not consist entirely of a single variable
4489 reference.  It can contain several variable references, as well as some
4490 invariant text.  For example,
4492 @example
4493 @group
4494 a_dirs := dira dirb
4495 1_dirs := dir1 dir2
4496 @end group
4498 @group
4499 a_files := filea fileb
4500 1_files := file1 file2
4501 @end group
4503 @group
4504 ifeq "$(use_a)" "yes"
4505 a1 := a
4506 else
4507 a1 := 1
4508 endif
4509 @end group
4511 @group
4512 ifeq "$(use_dirs)" "yes"
4513 df := dirs
4514 else
4515 df := files
4516 endif
4518 dirs := $($(a1)_$(df))
4519 @end group
4520 @end example
4522 @noindent
4523 will give @code{dirs} the same value as @code{a_dirs}, @code{1_dirs},
4524 @code{a_files} or @code{1_files} depending on the settings of @code{use_a}
4525 and @code{use_dirs}.@refill
4527 Computed variable names can also be used in substitution references:
4529 @example
4530 @group
4531 a_objects := a.o b.o c.o
4532 1_objects := 1.o 2.o 3.o
4534 sources := $($(a1)_objects:.o=.c)
4535 @end group
4536 @end example
4538 @noindent
4539 defines @code{sources} as either @samp{a.c b.c c.c} or @samp{1.c 2.c 3.c},
4540 depending on the value of @code{a1}.
4542 The only restriction on this sort of use of nested variable references
4543 is that they cannot specify part of the name of a function to be called.
4544 This is because the test for a recognized function name is done before
4545 the expansion of nested references.  For example,
4547 @example
4548 @group
4549 ifdef do_sort
4550 func := sort
4551 else
4552 func := strip
4553 endif
4554 @end group
4556 @group
4557 bar := a d b g q c
4558 @end group
4560 @group
4561 foo := $($(func) $(bar))
4562 @end group
4563 @end example
4565 @noindent
4566 attempts to give @samp{foo} the value of the variable @samp{sort a d b g
4567 q c} or @samp{strip a d b g q c}, rather than giving @samp{a d b g q c}
4568 as the argument to either the @code{sort} or the @code{strip} function.
4569 This restriction could be removed in the future if that change is shown
4570 to be a good idea.
4572 You can also use computed variable names in the left-hand side of a
4573 variable assignment, or in a @code{define} directive, as in:
4575 @example
4576 dir = foo
4577 $(dir)_sources := $(wildcard $(dir)/*.c)
4578 define $(dir)_print
4579 lpr $($(dir)_sources)
4580 endef
4581 @end example
4583 @noindent
4584 This example defines the variables @samp{dir}, @samp{foo_sources}, and
4585 @samp{foo_print}.
4587 Note that @dfn{nested variable references} are quite different from
4588 @dfn{recursively expanded variables}
4589 (@pxref{Flavors, ,The Two Flavors of Variables}), though both are
4590 used together in complex ways when doing makefile programming.@refill
4592 @node Values, Setting, Advanced, Using Variables
4593 @section How Variables Get Their Values
4594 @cindex variables, how they get their values
4595 @cindex value, how a variable gets it
4597 Variables can get values in several different ways:
4599 @itemize @bullet
4600 @item
4601 You can specify an overriding value when you run @code{make}.
4602 @xref{Overriding, ,Overriding Variables}.
4604 @item
4605 You can specify a value in the makefile, either
4606 with an assignment (@pxref{Setting, ,Setting Variables}) or with a
4607 verbatim definition (@pxref{Defining, ,Defining Variables Verbatim}).@refill
4609 @item
4610 Variables in the environment become @code{make} variables.
4611 @xref{Environment, ,Variables from the Environment}.
4613 @item
4614 Several @dfn{automatic} variables are given new values for each rule.
4615 Each of these has a single conventional use.
4616 @xref{Automatic, ,Automatic Variables}.
4618 @item
4619 Several variables have constant initial values.
4620 @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
4621 @end itemize
4623 @node Setting, Appending, Values, Using Variables
4624 @section Setting Variables
4625 @cindex setting variables
4626 @cindex variables, setting
4627 @cindex =
4628 @cindex :=
4629 @cindex ?=
4631 To set a variable from the makefile, write a line starting with the
4632 variable name followed by @samp{=} or @samp{:=}.  Whatever follows the
4633 @samp{=} or @samp{:=} on the line becomes the value.  For example,
4635 @example
4636 objects = main.o foo.o bar.o utils.o
4637 @end example
4639 @noindent
4640 defines a variable named @code{objects}.  Whitespace around the variable
4641 name and immediately after the @samp{=} is ignored.
4643 Variables defined with @samp{=} are @dfn{recursively expanded} variables.
4644 Variables defined with @samp{:=} are @dfn{simply expanded} variables; these
4645 definitions can contain variable references which will be expanded before
4646 the definition is made.  @xref{Flavors, ,The Two Flavors of Variables}.
4648 The variable name may contain function and variable references, which
4649 are expanded when the line is read to find the actual variable name to use.
4651 There is no limit on the length of the value of a variable except the
4652 amount of swapping space on the computer.  When a variable definition is
4653 long, it is a good idea to break it into several lines by inserting
4654 backslash-newline at convenient places in the definition.  This will not
4655 affect the functioning of @code{make}, but it will make the makefile easier
4656 to read.
4658 Most variable names are considered to have the empty string as a value if
4659 you have never set them.  Several variables have built-in initial values
4660 that are not empty, but you can set them in the usual ways
4661 (@pxref{Implicit Variables, ,Variables Used by Implicit Rules}).
4662 Several special variables are set
4663 automatically to a new value for each rule; these are called the
4664 @dfn{automatic} variables (@pxref{Automatic, ,Automatic Variables}).
4666 If you'd like a variable to be set to a value only if it's not already
4667 set, then you can use the shorthand operator @samp{?=} instead of
4668 @samp{=}.  These two settings of the variable @samp{FOO} are identical
4669 (@pxref{Origin Function, ,The @code{origin} Function}):
4671 @example
4672 FOO ?= bar
4673 @end example
4675 @noindent
4678 @example
4679 ifeq ($(origin FOO), undefined)
4680 FOO = bar
4681 endif
4682 @end example
4684 @node Appending, Override Directive, Setting, Using Variables
4685 @section Appending More Text to Variables
4686 @cindex +=
4687 @cindex appending to variables
4688 @cindex variables, appending to
4690 Often it is useful to add more text to the value of a variable already defined.
4691 You do this with a line containing @samp{+=}, like this:
4693 @example
4694 objects += another.o
4695 @end example
4697 @noindent
4698 This takes the value of the variable @code{objects}, and adds the text
4699 @samp{another.o} to it (preceded by a single space).  Thus:
4701 @example
4702 objects = main.o foo.o bar.o utils.o
4703 objects += another.o
4704 @end example
4706 @noindent
4707 sets @code{objects} to @samp{main.o foo.o bar.o utils.o another.o}.
4709 Using @samp{+=} is similar to:
4711 @example
4712 objects = main.o foo.o bar.o utils.o
4713 objects := $(objects) another.o
4714 @end example
4716 @noindent
4717 but differs in ways that become important when you use more complex values.
4719 When the variable in question has not been defined before, @samp{+=}
4720 acts just like normal @samp{=}: it defines a recursively-expanded
4721 variable.  However, when there @emph{is} a previous definition, exactly
4722 what @samp{+=} does depends on what flavor of variable you defined
4723 originally.  @xref{Flavors, ,The Two Flavors of Variables}, for an
4724 explanation of the two flavors of variables.
4726 When you add to a variable's value with @samp{+=}, @code{make} acts
4727 essentially as if you had included the extra text in the initial
4728 definition of the variable.  If you defined it first with @samp{:=},
4729 making it a simply-expanded variable, @samp{+=} adds to that
4730 simply-expanded definition, and expands the new text before appending it
4731 to the old value just as @samp{:=} does
4732 (@pxref{Setting, ,Setting Variables}, for a full explanation of @samp{:=}).
4733 In fact,
4735 @example
4736 variable := value
4737 variable += more
4738 @end example
4740 @noindent
4741 is exactly equivalent to:
4743 @noindent
4744 @example
4745 variable := value
4746 variable := $(variable) more
4747 @end example
4749 On the other hand, when you use @samp{+=} with a variable that you defined
4750 first to be recursively-expanded using plain @samp{=}, @code{make} does
4751 something a bit different.  Recall that when you define a
4752 recursively-expanded variable, @code{make} does not expand the value you set
4753 for variable and function references immediately.  Instead it stores the text
4754 verbatim, and saves these variable and function references to be expanded
4755 later, when you refer to the new variable (@pxref{Flavors, ,The Two Flavors
4756 of Variables}).  When you use @samp{+=} on a recursively-expanded variable,
4757 it is this unexpanded text to which @code{make} appends the new text you
4758 specify.
4760 @example
4761 @group
4762 variable = value
4763 variable += more
4764 @end group
4765 @end example
4767 @noindent
4768 is roughly equivalent to:
4770 @example
4771 @group
4772 temp = value
4773 variable = $(temp) more
4774 @end group
4775 @end example
4777 @noindent
4778 except that of course it never defines a variable called @code{temp}.
4779 The importance of this comes when the variable's old value contains
4780 variable references.  Take this common example:
4782 @example
4783 CFLAGS = $(includes) -O
4784 @dots{}
4785 CFLAGS += -pg # enable profiling
4786 @end example
4788 @noindent
4789 The first line defines the @code{CFLAGS} variable with a reference to another
4790 variable, @code{includes}.  (@code{CFLAGS} is used by the rules for C
4791 compilation; @pxref{Catalogue of Rules, ,Catalogue of Implicit Rules}.)
4792 Using @samp{=} for the definition makes @code{CFLAGS} a recursively-expanded
4793 variable, meaning @w{@samp{$(includes) -O}} is @emph{not} expanded when
4794 @code{make} processes the definition of @code{CFLAGS}.  Thus, @code{includes}
4795 need not be defined yet for its value to take effect.  It only has to be
4796 defined before any reference to @code{CFLAGS}.  If we tried to append to the
4797 value of @code{CFLAGS} without using @samp{+=}, we might do it like this:
4799 @example
4800 CFLAGS := $(CFLAGS) -pg # enable profiling
4801 @end example
4803 @noindent
4804 This is pretty close, but not quite what we want.  Using @samp{:=}
4805 redefines @code{CFLAGS} as a simply-expanded variable; this means
4806 @code{make} expands the text @w{@samp{$(CFLAGS) -pg}} before setting the
4807 variable.  If @code{includes} is not yet defined, we get @w{@samp{ -O
4808 -pg}}, and a later definition of @code{includes} will have no effect.
4809 Conversely, by using @samp{+=} we set @code{CFLAGS} to the
4810 @emph{unexpanded} value @w{@samp{$(includes) -O -pg}}.  Thus we preserve
4811 the reference to @code{includes}, so if that variable gets defined at
4812 any later point, a reference like @samp{$(CFLAGS)} still uses its
4813 value.
4815 @node Override Directive, Defining, Appending, Using Variables
4816 @section The @code{override} Directive
4817 @findex override
4818 @cindex overriding with @code{override}
4819 @cindex variables, overriding
4821 If a variable has been set with a command argument
4822 (@pxref{Overriding, ,Overriding Variables}),
4823 then ordinary assignments in the makefile are ignored.  If you want to set
4824 the variable in the makefile even though it was set with a command
4825 argument, you can use an @code{override} directive, which is a line that
4826 looks like this:@refill
4828 @example
4829 override @var{variable} = @var{value}
4830 @end example
4832 @noindent
4835 @example
4836 override @var{variable} := @var{value}
4837 @end example
4839 To append more text to a variable defined on the command line, use:
4841 @example
4842 override @var{variable} += @var{more text}
4843 @end example
4845 @noindent
4846 @xref{Appending, ,Appending More Text to Variables}.
4848 The @code{override} directive was not invented for escalation in the war
4849 between makefiles and command arguments.  It was invented so you can alter
4850 and add to values that the user specifies with command arguments.
4852 For example, suppose you always want the @samp{-g} switch when you run the
4853 C compiler, but you would like to allow the user to specify the other
4854 switches with a command argument just as usual.  You could use this
4855 @code{override} directive:
4857 @example
4858 override CFLAGS += -g
4859 @end example
4861 You can also use @code{override} directives with @code{define} directives.
4862 This is done as you might expect:
4864 @example
4865 override define foo
4867 endef
4868 @end example
4870 @noindent
4871 @iftex
4872 See the next section for information about @code{define}.
4873 @end iftex
4874 @ifinfo
4875 @xref{Defining, ,Defining Variables Verbatim}.
4876 @end ifinfo
4878 @node Defining, Environment, Override Directive, Using Variables
4879 @section Defining Variables Verbatim
4880 @findex define
4881 @findex endef
4882 @cindex verbatim variable definition
4883 @cindex defining variables verbatim
4884 @cindex variables, defining verbatim
4886 Another way to set the value of a variable is to use the @code{define}
4887 directive.  This directive has an unusual syntax which allows newline
4888 characters to be included in the value, which is convenient for defining
4889 canned sequences of commands
4890 (@pxref{Sequences, ,Defining Canned Command Sequences}).
4892 The @code{define} directive is followed on the same line by the name of the
4893 variable and nothing more.  The value to give the variable appears on the
4894 following lines.  The end of the value is marked by a line containing just
4895 the word @code{endef}.  Aside from this difference in syntax, @code{define}
4896 works just like @samp{=}: it creates a recursively-expanded variable
4897 (@pxref{Flavors, ,The Two Flavors of Variables}).
4898 The variable name may contain function and variable references, which
4899 are expanded when the directive is read to find the actual variable name
4900 to use.
4902 @example
4903 define two-lines
4904 echo foo
4905 echo $(bar)
4906 endef
4907 @end example
4909 The value in an ordinary assignment cannot contain a newline; but the
4910 newlines that separate the lines of the value in a @code{define} become
4911 part of the variable's value (except for the final newline which precedes
4912 the @code{endef} and is not considered part of the value).@refill
4914 @need 800
4915 The previous example is functionally equivalent to this:
4917 @example
4918 two-lines = echo foo; echo $(bar)
4919 @end example
4921 @noindent
4922 since two commands separated by semicolon behave much like two separate
4923 shell commands.  However, note that using two separate lines means
4924 @code{make} will invoke the shell twice, running an independent subshell
4925 for each line.  @xref{Execution, ,Command Execution}.
4927 If you want variable definitions made with @code{define} to take
4928 precedence over command-line variable definitions, you can use the
4929 @code{override} directive together with @code{define}:
4931 @example
4932 override define two-lines
4934 $(bar)
4935 endef
4936 @end example
4938 @noindent
4939 @xref{Override Directive, ,The @code{override} Directive}.
4941 @node Environment, Target-specific, Defining, Using Variables
4942 @section Variables from the Environment
4944 @cindex variables, environment
4945 @cindex environment
4946 Variables in @code{make} can come from the environment in which
4947 @code{make} is run.  Every environment variable that @code{make} sees when
4948 it starts up is transformed into a @code{make} variable with the same name
4949 and value.  But an explicit assignment in the makefile, or with a command
4950 argument, overrides the environment.  (If the @samp{-e} flag is specified,
4951 then values from the environment override assignments in the makefile.
4952 @xref{Options Summary, ,Summary of Options}.
4953 But this is not recommended practice.)
4955 Thus, by setting the variable @code{CFLAGS} in your environment, you can
4956 cause all C compilations in most makefiles to use the compiler switches you
4957 prefer.  This is safe for variables with standard or conventional meanings
4958 because you know that no makefile will use them for other things.  (But
4959 this is not totally reliable; some makefiles set @code{CFLAGS} explicitly
4960 and therefore are not affected by the value in the environment.)
4962 When @code{make} is invoked recursively, variables defined in the
4963 outer invocation can be passed to inner invocations through the
4964 environment (@pxref{Recursion, ,Recursive Use of @code{make}}).  By
4965 default, only variables that came from the environment or the command
4966 line are passed to recursive invocations.  You can use the
4967 @code{export} directive to pass other variables.
4968 @xref{Variables/Recursion, , Communicating Variables to a
4969 Sub-@code{make}}, for full details.
4971 Other use of variables from the environment is not recommended.  It is not
4972 wise for makefiles to depend for their functioning on environment variables
4973 set up outside their control, since this would cause different users to get
4974 different results from the same makefile.  This is against the whole
4975 purpose of most makefiles.
4977 Such problems would be especially likely with the variable @code{SHELL},
4978 which is normally present in the environment to specify the user's choice
4979 of interactive shell.  It would be very undesirable for this choice to
4980 affect @code{make}.  So @code{make} ignores the environment value of
4981 @code{SHELL} (except on MS-DOS and MS-Windows, where @code{SHELL} is
4982 usually not set.  @xref{Execution, ,Special handling of SHELL on
4983 MS-DOS}.)@refill
4985 @node Target-specific, Pattern-specific, Environment, Using Variables
4986 @section Target-specific Variable Values
4987 @cindex target-specific variables
4988 @cindex variables, target-specific
4990 Variable values in @code{make} are usually global; that is, they are the
4991 same regardless of where they are evaluated (unless they're reset, of
4992 course).  One exception to that is automatic variables
4993 (@pxref{Automatic, ,Automatic Variables}).
4995 The other exception is @dfn{target-specific variable values}.  This
4996 feature allows you to define different values for the same variable,
4997 based on the target that @code{make} is currently building.  As with
4998 automatic variables, these values are only available within the context
4999 of a target's command script (and in other target-specific assignments).
5001 Set a target-specific variable value like this:
5003 @example
5004 @var{target} @dots{} : @var{variable-assignment}
5005 @end example
5007 @noindent
5008 or like this:
5010 @example
5011 @var{target} @dots{} : override @var{variable-assignment}
5012 @end example
5014 Multiple @var{target} values create a target-specific variable value for
5015 each member of the target list individually.
5017 The @var{variable-assignment} can be any valid form of assignment;
5018 recursive (@samp{=}), static (@samp{:=}), appending (@samp{+=}), or
5019 conditional (@samp{?=}).  All variables that appear within the
5020 @var{variable-assignment} are evaluated within the context of the
5021 target: thus, any previously-defined target-specific variable values
5022 will be in effect.  Note that this variable is actually distinct from
5023 any ``global'' value: the two variables do not have to have the same
5024 flavor (recursive vs. static).
5026 Target-specific variables have the same priority as any other makefile
5027 variable.  Variables provided on the command-line (and in the
5028 environment if the @samp{-e} option is in force) will take precedence.
5029 Specifying the @code{override} directive will allow the target-specific
5030 variable value to be preferred.
5032 There is one more special feature of target-specific variables: when you
5033 define a target-specific variable, that variable value is also in effect
5034 for all prerequisites of this target (unless those prerequisites override
5035 it with their own target-specific variable value).  So, for example, a
5036 statement like this:
5038 @example
5039 prog : CFLAGS = -g
5040 prog : prog.o foo.o bar.o
5041 @end example
5043 @noindent
5044 will set @code{CFLAGS} to @samp{-g} in the command script for
5045 @file{prog}, but it will also set @code{CFLAGS} to @samp{-g} in the
5046 command scripts that create @file{prog.o}, @file{foo.o}, and
5047 @file{bar.o}, and any command scripts which create their prerequisites.
5049 @node Pattern-specific,  , Target-specific, Using Variables
5050 @section Pattern-specific Variable Values
5051 @cindex pattern-specific variables
5052 @cindex variables, pattern-specific
5054 In addition to target-specific variable values (@pxref{Target-specific,
5055 ,Target-specific Variable Values}), GNU @code{make} supports
5056 pattern-specific variable values.  In this form, a variable is defined
5057 for any target that matches the pattern specified.  Variables defined in
5058 this way are searched after any target-specific variables defined
5059 explicitly for that target, and before target-specific variables defined
5060 for the parent target.
5062 Set a pattern-specific variable value like this:
5064 @example
5065 @var{pattern} @dots{} : @var{variable-assignment}
5066 @end example
5068 @noindent
5069 or like this:
5071 @example
5072 @var{pattern} @dots{} : override @var{variable-assignment}
5073 @end example
5075 @noindent
5076 where @var{pattern} is a %-pattern.  As with target-specific variable
5077 values, multiple @var{pattern} values create a pattern-specific variable
5078 value for each pattern individually.  The @var{variable-assignment} can
5079 be any valid form of assignment.  Any command-line variable setting will
5080 take precedence, unless @code{override} is specified.
5082 For example:
5084 @example
5085 %.o : CFLAGS = -O
5086 @end example
5088 @noindent
5089 will assign @code{CFLAGS} the value of @samp{-O} for all targets
5090 matching the pattern @code{%.o}.
5092 @node Conditionals, Functions, Using Variables, Top
5093 @chapter Conditional Parts of Makefiles
5095 @cindex conditionals
5096 A @dfn{conditional} causes part of a makefile to be obeyed or ignored
5097 depending on the values of variables.  Conditionals can compare the
5098 value of one variable to another, or the value of a variable to
5099 a constant string.  Conditionals control what @code{make} actually
5100 ``sees'' in the makefile, so they @emph{cannot} be used to control shell
5101 commands at the time of execution.@refill
5103 @menu
5104 * Conditional Example::         Example of a conditional
5105 * Conditional Syntax::          The syntax of conditionals.
5106 * Testing Flags::               Conditionals that test flags.
5107 @end menu
5109 @node Conditional Example, Conditional Syntax,  , Conditionals
5110 @section Example of a Conditional
5112 The following example of a conditional tells @code{make} to use one set
5113 of libraries if the @code{CC} variable is @samp{gcc}, and a different
5114 set of libraries otherwise.  It works by controlling which of two
5115 command lines will be used as the command for a rule.  The result is
5116 that @samp{CC=gcc} as an argument to @code{make} changes not only which
5117 compiler is used but also which libraries are linked.
5119 @example
5120 libs_for_gcc = -lgnu
5121 normal_libs =
5123 foo: $(objects)
5124 ifeq ($(CC),gcc)
5125         $(CC) -o foo $(objects) $(libs_for_gcc)
5126 else
5127         $(CC) -o foo $(objects) $(normal_libs)
5128 endif
5129 @end example
5131 This conditional uses three directives: one @code{ifeq}, one @code{else}
5132 and one @code{endif}.
5134 The @code{ifeq} directive begins the conditional, and specifies the
5135 condition.  It contains two arguments, separated by a comma and surrounded
5136 by parentheses.  Variable substitution is performed on both arguments and
5137 then they are compared.  The lines of the makefile following the
5138 @code{ifeq} are obeyed if the two arguments match; otherwise they are
5139 ignored.
5141 The @code{else} directive causes the following lines to be obeyed if the
5142 previous conditional failed.  In the example above, this means that the
5143 second alternative linking command is used whenever the first alternative
5144 is not used.  It is optional to have an @code{else} in a conditional.
5146 The @code{endif} directive ends the conditional.  Every conditional must
5147 end with an @code{endif}.  Unconditional makefile text follows.
5149 As this example illustrates, conditionals work at the textual level:
5150 the lines of the conditional are treated as part of the makefile, or
5151 ignored, according to the condition.  This is why the larger syntactic
5152 units of the makefile, such as rules, may cross the beginning or the
5153 end of the conditional.
5155 When the variable @code{CC} has the value @samp{gcc}, the above example has
5156 this effect:
5158 @example
5159 foo: $(objects)
5160         $(CC) -o foo $(objects) $(libs_for_gcc)
5161 @end example
5163 @noindent
5164 When the variable @code{CC} has any other value, the effect is this:
5166 @example
5167 foo: $(objects)
5168         $(CC) -o foo $(objects) $(normal_libs)
5169 @end example
5171 Equivalent results can be obtained in another way by conditionalizing a
5172 variable assignment and then using the variable unconditionally:
5174 @example
5175 libs_for_gcc = -lgnu
5176 normal_libs =
5178 ifeq ($(CC),gcc)
5179   libs=$(libs_for_gcc)
5180 else
5181   libs=$(normal_libs)
5182 endif
5184 foo: $(objects)
5185         $(CC) -o foo $(objects) $(libs)
5186 @end example
5188 @node Conditional Syntax, Testing Flags, Conditional Example, Conditionals
5189 @section Syntax of Conditionals
5190 @findex ifdef
5191 @findex ifeq
5192 @findex ifndef
5193 @findex ifneq
5194 @findex else
5195 @findex endif
5197 The syntax of a simple conditional with no @code{else} is as follows:
5199 @example
5200 @var{conditional-directive}
5201 @var{text-if-true}
5202 endif
5203 @end example
5205 @noindent
5206 The @var{text-if-true} may be any lines of text, to be considered as part
5207 of the makefile if the condition is true.  If the condition is false, no
5208 text is used instead.
5210 The syntax of a complex conditional is as follows:
5212 @example
5213 @var{conditional-directive}
5214 @var{text-if-true}
5215 else
5216 @var{text-if-false}
5217 endif
5218 @end example
5220 @noindent
5221 If the condition is true, @var{text-if-true} is used; otherwise,
5222 @var{text-if-false} is used instead.  The @var{text-if-false} can be any
5223 number of lines of text.
5225 The syntax of the @var{conditional-directive} is the same whether the
5226 conditional is simple or complex.  There are four different directives that
5227 test different conditions.  Here is a table of them:
5229 @table @code
5230 @item ifeq (@var{arg1}, @var{arg2})
5231 @itemx ifeq '@var{arg1}' '@var{arg2}'
5232 @itemx ifeq "@var{arg1}" "@var{arg2}"
5233 @itemx ifeq "@var{arg1}" '@var{arg2}'
5234 @itemx ifeq '@var{arg1}' "@var{arg2}"
5235 Expand all variable references in @var{arg1} and @var{arg2} and
5236 compare them.  If they are identical, the @var{text-if-true} is
5237 effective; otherwise, the @var{text-if-false}, if any, is effective.
5239 Often you want to test if a variable has a non-empty value.  When the
5240 value results from complex expansions of variables and functions,
5241 expansions you would consider empty may actually contain whitespace
5242 characters and thus are not seen as empty.  However, you can use the
5243 @code{strip} function (@pxref{Text Functions}) to avoid interpreting
5244 whitespace as a non-empty value.  For example:
5246 @example
5247 @group
5248 ifeq ($(strip $(foo)),)
5249 @var{text-if-empty}
5250 endif
5251 @end group
5252 @end example
5254 @noindent
5255 will evaluate @var{text-if-empty} even if the expansion of
5256 @code{$(foo)} contains whitespace characters.
5258 @item ifneq (@var{arg1}, @var{arg2})
5259 @itemx ifneq '@var{arg1}' '@var{arg2}'
5260 @itemx ifneq "@var{arg1}" "@var{arg2}"
5261 @itemx ifneq "@var{arg1}" '@var{arg2}'
5262 @itemx ifneq '@var{arg1}' "@var{arg2}"
5263 Expand all variable references in @var{arg1} and @var{arg2} and
5264 compare them.  If they are different, the @var{text-if-true} is
5265 effective; otherwise, the @var{text-if-false}, if any, is effective.
5267 @item ifdef @var{variable-name}
5268 If the variable @var{variable-name} has a non-empty value, the
5269 @var{text-if-true} is effective; otherwise, the @var{text-if-false},
5270 if any, is effective.  Variables that have never been defined have an
5271 empty value.
5273 Note that @code{ifdef} only tests whether a variable has a value.  It
5274 does not expand the variable to see if that value is nonempty.
5275 Consequently, tests using @code{ifdef} return true for all definitions
5276 except those like @code{foo =}.  To test for an empty value, use
5277 @w{@code{ifeq ($(foo),)}}.  For example,
5279 @example
5280 bar =
5281 foo = $(bar)
5282 ifdef foo
5283 frobozz = yes
5284 else
5285 frobozz = no
5286 endif
5287 @end example
5289 @noindent
5290 sets @samp{frobozz} to @samp{yes}, while:
5292 @example
5293 foo =
5294 ifdef foo
5295 frobozz = yes
5296 else
5297 frobozz = no
5298 endif
5299 @end example
5301 @noindent
5302 sets @samp{frobozz} to @samp{no}.
5304 @item ifndef @var{variable-name}
5305 If the variable @var{variable-name} has an empty value, the
5306 @var{text-if-true} is effective; otherwise, the @var{text-if-false},
5307 if any, is effective.
5308 @end table
5310 Extra spaces are allowed and ignored at the beginning of the conditional
5311 directive line, but a tab is not allowed.  (If the line begins with a tab,
5312 it will be considered a command for a rule.)  Aside from this, extra spaces
5313 or tabs may be inserted with no effect anywhere except within the directive
5314 name or within an argument.  A comment starting with @samp{#} may appear at
5315 the end of the line.
5317 The other two directives that play a part in a conditional are @code{else}
5318 and @code{endif}.  Each of these directives is written as one word, with no
5319 arguments.  Extra spaces are allowed and ignored at the beginning of the
5320 line, and spaces or tabs at the end.  A comment starting with @samp{#} may
5321 appear at the end of the line.
5323 Conditionals affect which lines of the makefile @code{make} uses.  If
5324 the condition is true, @code{make} reads the lines of the
5325 @var{text-if-true} as part of the makefile; if the condition is false,
5326 @code{make} ignores those lines completely.  It follows that syntactic
5327 units of the makefile, such as rules, may safely be split across the
5328 beginning or the end of the conditional.@refill
5330 @code{make} evaluates conditionals when it reads a makefile.
5331 Consequently, you cannot use automatic variables in the tests of
5332 conditionals because they are not defined until commands are run
5333 (@pxref{Automatic, , Automatic Variables}).
5335 To prevent intolerable confusion, it is not permitted to start a
5336 conditional in one makefile and end it in another.  However, you may
5337 write an @code{include} directive within a conditional, provided you do
5338 not attempt to terminate the conditional inside the included file.
5340 @node Testing Flags,  , Conditional Syntax, Conditionals
5341 @section Conditionals that Test Flags
5343 You can write a conditional that tests @code{make} command flags such as
5344 @samp{-t} by using the variable @code{MAKEFLAGS} together with the
5345 @code{findstring} function
5346 (@pxref{Text Functions, , Functions for String Substitution and Analysis}).
5347 This is useful when @code{touch} is not enough to make a file appear up
5348 to date.
5350 The @code{findstring} function determines whether one string appears as a
5351 substring of another.  If you want to test for the @samp{-t} flag,
5352 use @samp{t} as the first string and the value of @code{MAKEFLAGS} as
5353 the other.
5355 For example, here is how to arrange to use @samp{ranlib -t} to finish
5356 marking an archive file up to date:
5358 @example
5359 archive.a: @dots{}
5360 ifneq (,$(findstring t,$(MAKEFLAGS)))
5361         +touch archive.a
5362         +ranlib -t archive.a
5363 else
5364         ranlib archive.a
5365 endif
5366 @end example
5368 @noindent
5369 The @samp{+} prefix marks those command lines as ``recursive'' so
5370 that they will be executed despite use of the @samp{-t} flag.
5371 @xref{Recursion, ,Recursive Use of @code{make}}.
5373 @node Functions, Running, Conditionals, Top
5374 @chapter Functions for Transforming Text
5375 @cindex functions
5377 @dfn{Functions} allow you to do text processing in the makefile to compute
5378 the files to operate on or the commands to use.  You use a function in a
5379 @dfn{function call}, where you give the name of the function and some text
5380 (the @dfn{arguments}) for the function to operate on.  The result of the
5381 function's processing is substituted into the makefile at the point of the
5382 call, just as a variable might be substituted.
5384 @menu
5385 * Syntax of Functions::         How to write a function call.
5386 * Text Functions::              General-purpose text manipulation functions.
5387 * File Name Functions::         Functions for manipulating file names.
5388 * Foreach Function::            Repeat some text with controlled variation.
5389 * If Function::                 Conditionally expand a value.
5390 * Call Function::               Expand a user-defined function.
5391 * Origin Function::             Find where a variable got its value.
5392 * Shell Function::              Substitute the output of a shell command.
5393 * Make Control Functions::      Functions that control how make runs.
5394 @end menu
5396 @node Syntax of Functions, Text Functions,  , Functions
5397 @section Function Call Syntax
5398 @cindex @code{$}, in function call
5399 @cindex dollar sign (@code{$}), in function call
5400 @cindex arguments of functions
5401 @cindex functions, syntax of
5403 A function call resembles a variable reference.  It looks like this:
5405 @example
5406 $(@var{function} @var{arguments})
5407 @end example
5409 @noindent
5410 or like this:
5412 @example
5413 $@{@var{function} @var{arguments}@}
5414 @end example
5416 Here @var{function} is a function name; one of a short list of names
5417 that are part of @code{make}.  You can also essentially create your own
5418 functions by using the @code{call} builtin function.
5420 The @var{arguments} are the arguments of the function.  They are
5421 separated from the function name by one or more spaces or tabs, and if
5422 there is more than one argument, then they are separated by commas.
5423 Such whitespace and commas are not part of an argument's value.  The
5424 delimiters which you use to surround the function call, whether
5425 parentheses or braces, can appear in an argument only in matching pairs;
5426 the other kind of delimiters may appear singly.  If the arguments
5427 themselves contain other function calls or variable references, it is
5428 wisest to use the same kind of delimiters for all the references; write
5429 @w{@samp{$(subst a,b,$(x))}}, not @w{@samp{$(subst a,b,$@{x@})}}.  This
5430 is because it is clearer, and because only one type of delimiter is
5431 matched to find the end of the reference.
5433 The text written for each argument is processed by substitution of
5434 variables and function calls to produce the argument value, which
5435 is the text on which the function acts.  The substitution is done in the
5436 order in which the arguments appear.
5438 Commas and unmatched parentheses or braces cannot appear in the text of an
5439 argument as written; leading spaces cannot appear in the text of the first
5440 argument as written.  These characters can be put into the argument value
5441 by variable substitution.  First define variables @code{comma} and
5442 @code{space} whose values are isolated comma and space characters, then
5443 substitute these variables where such characters are wanted, like this:
5445 @example
5446 @group
5447 comma:= ,
5448 empty:=
5449 space:= $(empty) $(empty)
5450 foo:= a b c
5451 bar:= $(subst $(space),$(comma),$(foo))
5452 # @r{bar is now `a,b,c'.}
5453 @end group
5454 @end example
5456 @noindent
5457 Here the @code{subst} function replaces each space with a comma, through
5458 the value of @code{foo}, and substitutes the result.
5460 @node Text Functions, File Name Functions, Syntax of Functions, Functions
5461 @section Functions for String Substitution and Analysis
5462 @cindex functions, for text
5464 Here are some functions that operate on strings:
5466 @table @code
5467 @item $(subst @var{from},@var{to},@var{text})
5468 @findex subst
5469 Performs a textual replacement on the text @var{text}: each occurrence
5470 of @var{from} is replaced by @var{to}.  The result is substituted for
5471 the function call.  For example,
5473 @example
5474 $(subst ee,EE,feet on the street)
5475 @end example
5477 substitutes the string @samp{fEEt on the strEEt}.
5479 @item $(patsubst @var{pattern},@var{replacement},@var{text})
5480 @findex patsubst
5481 Finds whitespace-separated words in @var{text} that match
5482 @var{pattern} and replaces them with @var{replacement}.  Here
5483 @var{pattern} may contain a @samp{%} which acts as a wildcard,
5484 matching any number of any characters within a word.  If
5485 @var{replacement} also contains a @samp{%}, the @samp{%} is replaced
5486 by the text that matched the @samp{%} in @var{pattern}.  Only the first
5487 @samp{%} in the @var{pattern} and @var{replacement} is treated this
5488 way; any subsequent @samp{%} is unchanged.@refill
5490 @cindex @code{%}, quoting in @code{patsubst}
5491 @cindex @code{%}, quoting with @code{\} (backslash)
5492 @cindex @code{\} (backslash), to quote @code{%}
5493 @cindex backslash (@code{\}), to quote @code{%}
5494 @cindex quoting @code{%}, in @code{patsubst}
5495 @samp{%} characters in @code{patsubst} function invocations can be
5496 quoted with preceding backslashes (@samp{\}).  Backslashes that would
5497 otherwise quote @samp{%} characters can be quoted with more backslashes.
5498 Backslashes that quote @samp{%} characters or other backslashes are
5499 removed from the pattern before it is compared file names or has a stem
5500 substituted into it.  Backslashes that are not in danger of quoting
5501 @samp{%} characters go unmolested.  For example, the pattern
5502 @file{the\%weird\\%pattern\\} has @samp{the%weird\} preceding the
5503 operative @samp{%} character, and @samp{pattern\\} following it.  The
5504 final two backslashes are left alone because they cannot affect any
5505 @samp{%} character.@refill
5507 Whitespace between words is folded into single space characters;
5508 leading and trailing whitespace is discarded.
5510 For example,
5512 @example
5513 $(patsubst %.c,%.o,x.c.c bar.c)
5514 @end example
5516 @noindent
5517 produces the value @samp{x.c.o bar.o}.
5519 Substitution references (@pxref{Substitution Refs, ,Substitution
5520 References}) are a simpler way to get the effect of the @code{patsubst}
5521 function:
5523 @example
5524 $(@var{var}:@var{pattern}=@var{replacement})
5525 @end example
5527 @noindent
5528 is equivalent to
5530 @example
5531 $(patsubst @var{pattern},@var{replacement},$(@var{var}))
5532 @end example
5534 The second shorthand simplifies one of the most common uses of
5535 @code{patsubst}: replacing the suffix at the end of file names.
5537 @example
5538 $(@var{var}:@var{suffix}=@var{replacement})
5539 @end example
5541 @noindent
5542 is equivalent to
5544 @example
5545 $(patsubst %@var{suffix},%@var{replacement},$(@var{var}))
5546 @end example
5548 @noindent
5549 For example, you might have a list of object files:
5551 @example
5552 objects = foo.o bar.o baz.o
5553 @end example
5555 @noindent
5556 To get the list of corresponding source files, you could simply write:
5558 @example
5559 $(objects:.o=.c)
5560 @end example
5562 @noindent
5563 instead of using the general form:
5565 @example
5566 $(patsubst %.o,%.c,$(objects))
5567 @end example
5569 @item $(strip @var{string})
5570 @cindex stripping whitespace
5571 @cindex whitespace, stripping
5572 @cindex spaces, stripping
5573 @findex strip
5574 Removes leading and trailing whitespace from @var{string} and replaces
5575 each internal sequence of one or more whitespace characters with a
5576 single space.  Thus, @samp{$(strip a b  c )} results in @w{@samp{a b c}}.
5578 The function @code{strip} can be very useful when used in conjunction
5579 with conditionals.  When comparing something with the empty string
5580 @samp{} using @code{ifeq} or @code{ifneq}, you usually want a string of
5581 just whitespace to match the empty string (@pxref{Conditionals}).
5583 Thus, the following may fail to have the desired results:
5585 @example
5586 .PHONY: all
5587 ifneq   "$(needs_made)" ""
5588 all: $(needs_made)
5589 else
5590 all:;@@echo 'Nothing to make!'
5591 endif
5592 @end example
5594 @noindent
5595 Replacing the variable reference @w{@samp{$(needs_made)}} with the
5596 function call @w{@samp{$(strip $(needs_made))}} in the @code{ifneq}
5597 directive would make it more robust.@refill
5599 @item $(findstring @var{find},@var{in})
5600 @findex findstring
5601 @cindex searching for strings
5602 @cindex finding strings
5603 @cindex strings, searching for
5604 Searches @var{in} for an occurrence of @var{find}.  If it occurs, the
5605 value is @var{find}; otherwise, the value is empty.  You can use this
5606 function in a conditional to test for the presence of a specific
5607 substring in a given string.  Thus, the two examples,
5609 @example
5610 $(findstring a,a b c)
5611 $(findstring a,b c)
5612 @end example
5614 @noindent
5615 produce the values @samp{a} and @samp{} (the empty string),
5616 respectively.  @xref{Testing Flags}, for a practical application of
5617 @code{findstring}.@refill
5619 @need 750
5620 @findex filter
5621 @cindex filtering words
5622 @cindex words, filtering
5623 @item $(filter @var{pattern}@dots{},@var{text})
5624 Returns all whitespace-separated words in @var{text} that @emph{do} match
5625 any of the @var{pattern} words, removing any words that @emph{do not}
5626 match.  The patterns are written using @samp{%}, just like the patterns
5627 used in the @code{patsubst} function above.@refill
5629 The @code{filter} function can be used to separate out different types
5630 of strings (such as file names) in a variable.  For example:
5632 @example
5633 sources := foo.c bar.c baz.s ugh.h
5634 foo: $(sources)
5635         cc $(filter %.c %.s,$(sources)) -o foo
5636 @end example
5638 @noindent
5639 says that @file{foo} depends of @file{foo.c}, @file{bar.c},
5640 @file{baz.s} and @file{ugh.h} but only @file{foo.c}, @file{bar.c} and
5641 @file{baz.s} should be specified in the command to the
5642 compiler.@refill
5644 @item $(filter-out @var{pattern}@dots{},@var{text})
5645 @findex filter-out
5646 @cindex filtering out words
5647 @cindex words, filtering out
5648 Returns all whitespace-separated words in @var{text} that @emph{do not}
5649 match any of the @var{pattern} words, removing the words that @emph{do}
5650 match one or more.  This is the exact opposite of the @code{filter}
5651 function.@refill
5653 Removes all whitespace-separated words in @var{text} that @emph{do}
5654 match the @var{pattern} words, returning only the words that @emph{do
5655 not} match.  This is the exact opposite of the @code{filter}
5656 function.@refill
5658 For example, given:
5660 @example
5661 @group
5662 objects=main1.o foo.o main2.o bar.o
5663 mains=main1.o main2.o
5664 @end group
5665 @end example
5667 @noindent
5668 the following generates a list which contains all the object files not
5669 in @samp{mains}:
5671 @example
5672 $(filter-out $(mains),$(objects))
5673 @end example
5675 @need 1500
5676 @findex sort
5677 @cindex sorting words
5678 @item $(sort @var{list})
5679 Sorts the words of @var{list} in lexical order, removing duplicate
5680 words.  The output is a list of words separated by single spaces.
5681 Thus,
5683 @example
5684 $(sort foo bar lose)
5685 @end example
5687 @noindent
5688 returns the value @samp{bar foo lose}.
5690 @cindex removing duplicate words
5691 @cindex duplicate words, removing
5692 @cindex words, removing duplicates
5693 Incidentally, since @code{sort} removes duplicate words, you can use
5694 it for this purpose even if you don't care about the sort order.
5695 @end table
5697 Here is a realistic example of the use of @code{subst} and
5698 @code{patsubst}.  Suppose that a makefile uses the @code{VPATH} variable
5699 to specify a list of directories that @code{make} should search for
5700 prerequisite files
5701 (@pxref{General Search, , @code{VPATH} Search Path for All Prerequisites}).
5702 This example shows how to
5703 tell the C compiler to search for header files in the same list of
5704 directories.@refill
5706 The value of @code{VPATH} is a list of directories separated by colons,
5707 such as @samp{src:../headers}.  First, the @code{subst} function is used to
5708 change the colons to spaces:
5710 @example
5711 $(subst :, ,$(VPATH))
5712 @end example
5714 @noindent
5715 This produces @samp{src ../headers}.  Then @code{patsubst} is used to turn
5716 each directory name into a @samp{-I} flag.  These can be added to the
5717 value of the variable @code{CFLAGS}, which is passed automatically to the C
5718 compiler, like this:
5720 @example
5721 override CFLAGS += $(patsubst %,-I%,$(subst :, ,$(VPATH)))
5722 @end example
5724 @noindent
5725 The effect is to append the text @samp{-Isrc -I../headers} to the
5726 previously given value of @code{CFLAGS}.  The @code{override} directive is
5727 used so that the new value is assigned even if the previous value of
5728 @code{CFLAGS} was specified with a command argument (@pxref{Override
5729 Directive, , The @code{override} Directive}).
5731 @node File Name Functions, Foreach Function, Text Functions, Functions
5732 @section Functions for File Names
5733 @cindex functions, for file names
5734 @cindex file name functions
5736 Several of the built-in expansion functions relate specifically to
5737 taking apart file names or lists of file names.
5739 Each of the following functions performs a specific transformation on a
5740 file name.  The argument of the function is regarded as a series of file
5741 names, separated by whitespace.  (Leading and trailing whitespace is
5742 ignored.)  Each file name in the series is transformed in the same way and
5743 the results are concatenated with single spaces between them.
5745 @table @code
5746 @item $(dir @var{names}@dots{})
5747 @findex dir
5748 @cindex directory part
5749 @cindex file name, directory part
5750 Extracts the directory-part of each file name in @var{names}.  The
5751 directory-part of the file name is everything up through (and
5752 including) the last slash in it.  If the file name contains no slash,
5753 the directory part is the string @samp{./}.  For example,
5755 @example
5756 $(dir src/foo.c hacks)
5757 @end example
5759 @noindent
5760 produces the result @samp{src/ ./}.
5762 @item $(notdir @var{names}@dots{})
5763 @findex notdir
5764 @cindex file name, nondirectory part
5765 @cindex nondirectory part
5766 Extracts all but the directory-part of each file name in @var{names}.
5767 If the file name contains no slash, it is left unchanged.  Otherwise,
5768 everything through the last slash is removed from it.
5770 A file name that ends with a slash becomes an empty string.  This is
5771 unfortunate, because it means that the result does not always have the
5772 same number of whitespace-separated file names as the argument had;
5773 but we do not see any other valid alternative.
5775 For example,
5777 @example
5778 $(notdir src/foo.c hacks)
5779 @end example
5781 @noindent
5782 produces the result @samp{foo.c hacks}.
5784 @item $(suffix @var{names}@dots{})
5785 @findex suffix
5786 @cindex suffix, function to find
5787 @cindex file name suffix
5788 Extracts the suffix of each file name in @var{names}.  If the file name
5789 contains a period, the suffix is everything starting with the last
5790 period.  Otherwise, the suffix is the empty string.  This frequently
5791 means that the result will be empty when @var{names} is not, and if
5792 @var{names} contains multiple file names, the result may contain fewer
5793 file names.
5795 For example,
5797 @example
5798 $(suffix src/foo.c src-1.0/bar.c hacks)
5799 @end example
5801 @noindent
5802 produces the result @samp{.c .c}.
5804 @item $(basename @var{names}@dots{})
5805 @findex basename
5806 @cindex basename
5807 @cindex file name, basename of
5808 Extracts all but the suffix of each file name in @var{names}.  If the
5809 file name contains a period, the basename is everything starting up to
5810 (and not including) the last period.  Periods in the directory part are
5811 ignored.  If there is no period, the basename is the entire file name.
5812 For example,
5814 @example
5815 $(basename src/foo.c src-1.0/bar hacks)
5816 @end example
5818 @noindent
5819 produces the result @samp{src/foo src-1.0/bar hacks}.
5821 @c plural convention with dots (be consistent)
5822 @item $(addsuffix @var{suffix},@var{names}@dots{})
5823 @findex addsuffix
5824 @cindex suffix, adding
5825 @cindex file name suffix, adding
5826 The argument @var{names} is regarded as a series of names, separated
5827 by whitespace; @var{suffix} is used as a unit.  The value of
5828 @var{suffix} is appended to the end of each individual name and the
5829 resulting larger names are concatenated with single spaces between
5830 them.  For example,
5832 @example
5833 $(addsuffix .c,foo bar)
5834 @end example
5836 @noindent
5837 produces the result @samp{foo.c bar.c}.
5839 @item $(addprefix @var{prefix},@var{names}@dots{})
5840 @findex addprefix
5841 @cindex prefix, adding
5842 @cindex file name prefix, adding
5843 The argument @var{names} is regarded as a series of names, separated
5844 by whitespace; @var{prefix} is used as a unit.  The value of
5845 @var{prefix} is prepended to the front of each individual name and the
5846 resulting larger names are concatenated with single spaces between
5847 them.  For example,
5849 @example
5850 $(addprefix src/,foo bar)
5851 @end example
5853 @noindent
5854 produces the result @samp{src/foo src/bar}.
5856 @item $(join @var{list1},@var{list2})
5857 @findex join
5858 @cindex joining lists of words
5859 @cindex words, joining lists
5860 Concatenates the two arguments word by word: the two first words (one
5861 from each argument) concatenated form the first word of the result, the
5862 two second words form the second word of the result, and so on.  So the
5863 @var{n}th word of the result comes from the @var{n}th word of each
5864 argument.  If one argument has more words that the other, the extra
5865 words are copied unchanged into the result.
5867 For example, @samp{$(join a b,.c .o)} produces @samp{a.c b.o}.
5869 Whitespace between the words in the lists is not preserved; it is
5870 replaced with a single space.
5872 This function can merge the results of the @code{dir} and
5873 @code{notdir} functions, to produce the original list of files which
5874 was given to those two functions.@refill
5876 @item $(word @var{n},@var{text})
5877 @findex word
5878 @cindex word, selecting a
5879 @cindex selecting a word
5880 Returns the @var{n}th word of @var{text}.  The legitimate values of
5881 @var{n} start from 1.  If @var{n} is bigger than the number of words
5882 in @var{text}, the value is empty.  For example,
5884 @example
5885 $(word 2, foo bar baz)
5886 @end example
5888 @noindent
5889 returns @samp{bar}.
5891 @item $(wordlist @var{s},@var{e},@var{text})
5892 @findex wordlist
5893 @cindex words, selecting lists of
5894 @cindex selecting word lists
5895 Returns the list of words in @var{text} starting with word @var{s} and
5896 ending with word @var{e} (inclusive).  The legitimate values of @var{s}
5897 and @var{e} start from 1.  If @var{s} is bigger than the number of words
5898 in @var{text}, the value is empty.  If @var{e} is bigger than the number
5899 of words in @var{text}, words up to the end of @var{text} are returned.
5900 If @var{s} is greater than @var{e}, nothing is returned.  For example,
5902 @example
5903 $(wordlist 2, 3, foo bar baz)
5904 @end example
5906 @noindent
5907 returns @samp{bar baz}.
5909 @c Following item phrased to prevent overfull hbox.  --RJC 17 Jul 92
5910 @item $(words @var{text})
5911 @findex words
5912 @cindex words, finding number
5913 Returns the number of words in @var{text}.
5914 Thus, the last word of @var{text} is
5915 @w{@code{$(word $(words @var{text}),@var{text})}}.@refill
5917 @item $(firstword @var{names}@dots{})
5918 @findex firstword
5919 @cindex words, extracting first
5920 The argument @var{names} is regarded as a series of names, separated
5921 by whitespace.  The value is the first name in the series.  The rest
5922 of the names are ignored.
5924 For example,
5926 @example
5927 $(firstword foo bar)
5928 @end example
5930 @noindent
5931 produces the result @samp{foo}.  Although @code{$(firstword
5932 @var{text})} is the same as @code{$(word 1,@var{text})}, the
5933 @code{firstword} function is retained for its simplicity.@refill
5935 @item $(wildcard @var{pattern})
5936 @findex wildcard
5937 @cindex wildcard, function
5938 The argument @var{pattern} is a file name pattern, typically containing
5939 wildcard characters (as in shell file name patterns).  The result of
5940 @code{wildcard} is a space-separated list of the names of existing files
5941 that match the pattern.
5942 @xref{Wildcards, ,Using Wildcard Characters in File Names}.
5943 @end table
5945 @node Foreach Function, If Function, File Name Functions, Functions
5946 @section The @code{foreach} Function
5947 @findex foreach
5948 @cindex words, iterating over
5950 The @code{foreach} function is very different from other functions.  It
5951 causes one piece of text to be used repeatedly, each time with a different
5952 substitution performed on it.  It resembles the @code{for} command in the
5953 shell @code{sh} and the @code{foreach} command in the C-shell @code{csh}.
5955 The syntax of the @code{foreach} function is:
5957 @example
5958 $(foreach @var{var},@var{list},@var{text})
5959 @end example
5961 @noindent
5962 The first two arguments, @var{var} and @var{list}, are expanded before
5963 anything else is done; note that the last argument, @var{text}, is
5964 @strong{not} expanded at the same time.  Then for each word of the expanded
5965 value of @var{list}, the variable named by the expanded value of @var{var}
5966 is set to that word, and @var{text} is expanded.  Presumably @var{text}
5967 contains references to that variable, so its expansion will be different
5968 each time.
5970 The result is that @var{text} is expanded as many times as there are
5971 whitespace-separated words in @var{list}.  The multiple expansions of
5972 @var{text} are concatenated, with spaces between them, to make the result
5973 of @code{foreach}.
5975 This simple example sets the variable @samp{files} to the list of all files
5976 in the directories in the list @samp{dirs}:
5978 @example
5979 dirs := a b c d
5980 files := $(foreach dir,$(dirs),$(wildcard $(dir)/*))
5981 @end example
5983 Here @var{text} is @samp{$(wildcard $(dir)/*)}.  The first repetition
5984 finds the value @samp{a} for @code{dir}, so it produces the same result
5985 as @samp{$(wildcard a/*)}; the second repetition produces the result
5986 of @samp{$(wildcard b/*)}; and the third, that of @samp{$(wildcard c/*)}.
5988 This example has the same result (except for setting @samp{dirs}) as
5989 the following example:
5991 @example
5992 files := $(wildcard a/* b/* c/* d/*)
5993 @end example
5995 When @var{text} is complicated, you can improve readability by giving it
5996 a name, with an additional variable:
5998 @example
5999 find_files = $(wildcard $(dir)/*)
6000 dirs := a b c d
6001 files := $(foreach dir,$(dirs),$(find_files))
6002 @end example
6004 @noindent
6005 Here we use the variable @code{find_files} this way.  We use plain @samp{=}
6006 to define a recursively-expanding variable, so that its value contains an
6007 actual function call to be reexpanded under the control of @code{foreach};
6008 a simply-expanded variable would not do, since @code{wildcard} would be
6009 called only once at the time of defining @code{find_files}.
6011 The @code{foreach} function has no permanent effect on the variable
6012 @var{var}; its value and flavor after the @code{foreach} function call are
6013 the same as they were beforehand.  The other values which are taken from
6014 @var{list} are in effect only temporarily, during the execution of
6015 @code{foreach}.  The variable @var{var} is a simply-expanded variable
6016 during the execution of @code{foreach}.  If @var{var} was undefined
6017 before the @code{foreach} function call, it is undefined after the call.
6018 @xref{Flavors, ,The Two Flavors of Variables}.@refill
6020 You must take care when using complex variable expressions that result in
6021 variable names because many strange things are valid variable names, but
6022 are probably not what you intended.  For example,
6024 @smallexample
6025 files := $(foreach Esta escrito en espanol!,b c ch,$(find_files))
6026 @end smallexample
6028 @noindent
6029 might be useful if the value of @code{find_files} references the variable
6030 whose name is @samp{Esta escrito en espanol!} (es un nombre bastante largo,
6031 no?), but it is more likely to be a mistake.
6033 @node If Function, Call Function, Foreach Function, Functions
6034 @section The @code{if} Function
6035 @findex if
6036 @cindex conditional expansion
6038 The @code{if} function provides support for conditional expansion in a
6039 functional context (as opposed to the GNU @code{make} makefile
6040 conditionals such as @code{ifeq} (@pxref{Conditional Syntax, ,Syntax of
6041 Conditionals}).
6043 An @code{if} function call can contain either two or three arguments:
6045 @example
6046 $(if @var{condition},@var{then-part}[,@var{else-part}])
6047 @end example
6049 The first argument, @var{condition}, first has all preceding and
6050 trailing whitespace stripped, then is expanded.  If it expands to any
6051 non-empty string, then the condition is considered to be true.  If it
6052 expands to an empty string, the condition is considered to be false.
6054 If the condition is true then the second argument, @var{then-part}, is
6055 evaluated and this is used as the result of the evaluation of the entire
6056 @code{if} function.
6058 If the condition is false then the third argument, @var{else-part}, is
6059 evaluated and this is the result of the @code{if} function.  If there is
6060 no third argument, the @code{if} function evaluates to nothing (the
6061 empty string).
6063 Note that only one of the @var{then-part} or the @var{else-part} will be
6064 evaluated, never both.  Thus, either can contain side-effects (such as
6065 @code{shell} function calls, etc.)
6067 @node Call Function, Origin Function, If Function, Functions
6068 @section The @code{call} Function
6069 @findex call
6070 @cindex functions, user defined
6071 @cindex user defined functions
6073 The @code{call} function is unique in that it can be used to create new
6074 parameterized functions.  You can write a complex expression as the
6075 value of a variable, then use @code{call} to expand it with different
6076 values.
6078 The syntax of the @code{call} function is:
6080 @example
6081 $(call @var{variable},@var{param},@var{param},@dots{})
6082 @end example
6084 When @code{make} expands this function, it assigns each @var{param} to
6085 temporary variables @code{$(1)}, @code{$(2)}, etc.  The variable
6086 @code{$(0)} will contain @var{variable}.  There is no maximum number of
6087 parameter arguments.  There is no minimum, either, but it doesn't make
6088 sense to use @code{call} with no parameters.
6090 Then @var{variable} is expanded as a @code{make} variable in the context
6091 of these temporary assignments.  Thus, any reference to @code{$(1)} in
6092 the value of @var{variable} will resolve to the first @var{param} in the
6093 invocation of @code{call}.
6095 Note that @var{variable} is the @emph{name} of a variable, not a
6096 @emph{reference} to that variable.  Therefore you would not normally use
6097 a @samp{$} or parentheses when writing it.  (You can, however, use a
6098 variable reference in the name if you want the name not to be a
6099 constant.)
6101 If @var{variable} is the name of a builtin function, the builtin function
6102 is always invoked (even if a @code{make} variable by that name also
6103 exists).
6105 The @code{call} function expands the @var{param} arguments before
6106 assigning them to temporary variables.  This means that @var{variable}
6107 values containing references to builtin functions that have special
6108 expansion rules, like @code{foreach} or @code{if}, may not work as you
6109 expect.
6111 Some examples may make this clearer.
6113 This macro simply reverses its arguments:
6115 @smallexample
6116 reverse = $(2) $(1)
6118 foo = $(call reverse,a,b)
6119 @end smallexample
6121 @noindent
6122 Here @var{foo} will contain @samp{b a}.
6124 This one is slightly more interesting: it defines a macro to search for
6125 the first instance of a program in @code{PATH}:
6127 @smallexample
6128 pathsearch = $(firstword $(wildcard $(addsuffix /$(1),$(subst :, ,$(PATH)))))
6130 LS := $(call pathsearch,ls)
6131 @end smallexample
6133 @noindent
6134 Now the variable LS contains @code{/bin/ls} or similar.
6136 The @code{call} function can be nested.  Each recursive invocation gets
6137 its own local values for @code{$(1)}, etc. that mask the values of
6138 higher-level @code{call}.  For example, here is an implementation of a
6139 @dfn{map} function:
6141 @smallexample
6142 map = $(foreach a,$(2),$(call $(1),$(a)))
6143 @end smallexample
6145 Now you can @var{map} a function that normally takes only one argument,
6146 such as @code{origin}, to multiple values in one step:
6148 @smallexample
6149 o = $(call map,origin,o map MAKE)
6150 @end smallexample
6152 and end up with @var{o} containing something like @samp{file file default}.
6154 A final caution: be careful when adding whitespace to the arguments to
6155 @code{call}.  As with other functions, any whitespace contained in the
6156 second and subsequent arguments is kept; this can cause strange
6157 effects.  It's generally safest to remove all extraneous whitespace when
6158 providing parameters to @code{call}.
6161 @node Origin Function, Shell Function, Call Function, Functions
6162 @section The @code{origin} Function
6163 @findex origin
6164 @cindex variables, origin of
6165 @cindex origin of variable
6167 The @code{origin} function is unlike most other functions in that it does
6168 not operate on the values of variables; it tells you something @emph{about}
6169 a variable.  Specifically, it tells you where it came from.
6171 The syntax of the @code{origin} function is:
6173 @example
6174 $(origin @var{variable})
6175 @end example
6177 Note that @var{variable} is the @emph{name} of a variable to inquire about;
6178 not a @emph{reference} to that variable.  Therefore you would not normally
6179 use a @samp{$} or parentheses when writing it.  (You can, however, use a
6180 variable reference in the name if you want the name not to be a constant.)
6182 The result of this function is a string telling you how the variable
6183 @var{variable} was defined:
6185 @table @samp
6186 @item undefined
6188 if @var{variable} was never defined.
6190 @item default
6192 if @var{variable} has a default definition, as is usual with @code{CC}
6193 and so on.  @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
6194 Note that if you have redefined a default variable, the @code{origin}
6195 function will return the origin of the later definition.
6197 @item environment
6199 if @var{variable} was defined as an environment variable and the
6200 @samp{-e} option is @emph{not} turned on (@pxref{Options Summary, ,Summary of Options}).
6202 @item environment override
6204 if @var{variable} was defined as an environment variable and the
6205 @w{@samp{-e}} option @emph{is} turned on (@pxref{Options Summary,
6206 ,Summary of Options}).@refill
6208 @item file
6210 if @var{variable} was defined in a makefile.
6212 @item command line
6214 if @var{variable} was defined on the command line.
6216 @item override
6218 if @var{variable} was defined with an @code{override} directive in a
6219 makefile (@pxref{Override Directive, ,The @code{override} Directive}).
6221 @item automatic
6223 if @var{variable} is an automatic variable defined for the
6224 execution of the commands for each rule
6225 (@pxref{Automatic, , Automatic Variables}).
6226 @end table
6228 This information is primarily useful (other than for your curiosity) to
6229 determine if you want to believe the value of a variable.  For example,
6230 suppose you have a makefile @file{foo} that includes another makefile
6231 @file{bar}.  You want a variable @code{bletch} to be defined in @file{bar}
6232 if you run the command @w{@samp{make -f bar}}, even if the environment contains
6233 a definition of @code{bletch}.  However, if @file{foo} defined
6234 @code{bletch} before including @file{bar}, you do not want to override that
6235 definition.  This could be done by using an @code{override} directive in
6236 @file{foo}, giving that definition precedence over the later definition in
6237 @file{bar}; unfortunately, the @code{override} directive would also
6238 override any command line definitions.  So, @file{bar} could
6239 include:@refill
6241 @example
6242 @group
6243 ifdef bletch
6244 ifeq "$(origin bletch)" "environment"
6245 bletch = barf, gag, etc.
6246 endif
6247 endif
6248 @end group
6249 @end example
6251 @noindent
6252 If @code{bletch} has been defined from the environment, this will redefine
6255 If you want to override a previous definition of @code{bletch} if it came
6256 from the environment, even under @samp{-e}, you could instead write:
6258 @example
6259 @group
6260 ifneq "$(findstring environment,$(origin bletch))" ""
6261 bletch = barf, gag, etc.
6262 endif
6263 @end group
6264 @end example
6266 Here the redefinition takes place if @samp{$(origin bletch)} returns either
6267 @samp{environment} or @samp{environment override}.
6268 @xref{Text Functions, , Functions for String Substitution and Analysis}.
6270 @node Shell Function, Make Control Functions, Origin Function, Functions
6271 @section The @code{shell} Function
6272 @findex shell
6273 @cindex commands, expansion
6274 @cindex backquotes
6275 @cindex shell command, function for
6277 The @code{shell} function is unlike any other function except the
6278 @code{wildcard} function
6279 (@pxref{Wildcard Function, ,The Function @code{wildcard}}) in that it
6280 communicates with the world outside of @code{make}.
6282 The @code{shell} function performs the same function that backquotes
6283 (@samp{`}) perform in most shells: it does @dfn{command expansion}.  This
6284 means that it takes an argument that is a shell command and returns the
6285 output of the command.  The only processing @code{make} does on the result,
6286 before substituting it into the surrounding text, is to convert each
6287 newline or carriage-return / newline pair to a single space.  It also
6288 removes the trailing (carriage-return and) newline, if it's the last
6289 thing in the result.@refill
6291 The commands run by calls to the @code{shell} function are run when the
6292 function calls are expanded.  In most cases, this is when the makefile is
6293 read in.  The exception is that function calls in the commands of the rules
6294 are expanded when the commands are run, and this applies to @code{shell}
6295 function calls like all others.
6297 Here are some examples of the use of the @code{shell} function:
6299 @example
6300 contents := $(shell cat foo)
6301 @end example
6303 @noindent
6304 sets @code{contents} to the contents of the file @file{foo}, with a space
6305 (rather than a newline) separating each line.
6307 @example
6308 files := $(shell echo *.c)
6309 @end example
6311 @noindent
6312 sets @code{files} to the expansion of @samp{*.c}.  Unless @code{make} is
6313 using a very strange shell, this has the same result as
6314 @w{@samp{$(wildcard *.c)}}.@refill
6316 @node Make Control Functions,  , Shell Function, Functions
6317 @section Functions That Control Make
6318 @cindex functions, for controlling make
6319 @cindex controlling make
6321 These functions control the way make runs.  Generally, they are used to
6322 provide information to the user of the makefile or to cause make to stop
6323 if some sort of environmental error is detected.
6325 @table @code
6326 @item $(error @var{text}@dots{})
6327 @findex error
6328 @cindex error, stopping on
6329 @cindex stopping make
6330 Generates a fatal error where the message is @var{text}.  Note that the
6331 error is generated whenever this function is evaluated.  So, if you put
6332 it inside a command script or on the right side of a recursive variable
6333 assignment, it won't be evaluated until later.  The @var{text} will be
6334 expanded before the error is generated.
6336 For example,
6338 @example
6339 ifdef ERROR1
6340 $(error error is $(ERROR1))
6341 endif
6342 @end example
6344 @noindent
6345 will generate a fatal error during the read of the makefile if the
6346 @code{make} variable @code{ERROR1} is defined.  Or,
6348 @example
6349 ERR = $(error found an error!)
6351 .PHONY: err
6352 err: ; $(ERR)
6353 @end example
6355 @noindent
6356 will generate a fatal error while @code{make} is running, if the
6357 @code{err} target is invoked.
6359 @item $(warning @var{text}@dots{})
6360 @findex warning
6361 @cindex warnings, printing
6362 @cindex printing user warnings
6363 This function works similarly to the @code{error} function, above,
6364 except that @code{make} doesn't exit.  Instead, @var{text} is expanded
6365 and the resulting message is displayed, but processing of the makefile
6366 continues.
6368 The result of the expansion of this function is the empty string.
6369 @end table
6371 @node Running, Implicit Rules, Functions, Top
6372 @chapter How to Run @code{make}
6374 A makefile that says how to recompile a program can be used in more
6375 than one way.  The simplest use is to recompile every file that is out
6376 of date.  Usually, makefiles are written so that if you run
6377 @code{make} with no arguments, it does just that.
6379 But you might want to update only some of the files; you might want to use
6380 a different compiler or different compiler options; you might want just to
6381 find out which files are out of date without changing them.
6383 By giving arguments when you run @code{make}, you can do any of these
6384 things and many others.
6386 The exit status of @code{make} is always one of three values:
6387 @table @code
6388 @item 0
6389 The exit status is zero if @code{make} is successful.
6390 @item 2
6391 The exit status is two if @code{make} encounters any errors.
6392 It will print messages describing the particular errors.
6393 @item 1
6394 The exit status is one if you use the @samp{-q} flag and @code{make}
6395 determines that some target is not already up to date.
6396 @xref{Instead of Execution, ,Instead of Executing the Commands}.
6397 @end table
6399 @menu
6400 * Makefile Arguments::          How to specify which makefile to use.
6401 * Goals::                       How to use goal arguments to specify which
6402                                   parts of the makefile to use.
6403 * Instead of Execution::        How to use mode flags to specify what
6404                                   kind of thing to do with the commands
6405                                   in the makefile other than simply
6406                                   execute them.
6407 * Avoiding Compilation::        How to avoid recompiling certain files.
6408 * Overriding::                  How to override a variable to specify
6409                                   an alternate compiler and other things.
6410 * Testing::                     How to proceed past some errors, to
6411                                   test compilation.
6412 * Options Summary::             Summary of Options
6413 @end menu
6415 @node Makefile Arguments, Goals,  , Running
6416 @section Arguments to Specify the Makefile
6417 @cindex @code{--file}
6418 @cindex @code{--makefile}
6419 @cindex @code{-f}
6421 The way to specify the name of the makefile is with the @samp{-f} or
6422 @samp{--file} option (@samp{--makefile} also works).  For example,
6423 @samp{-f altmake} says to use the file @file{altmake} as the makefile.
6425 If you use the @samp{-f} flag several times and follow each @samp{-f}
6426 with an argument, all the specified files are used jointly as
6427 makefiles.
6429 If you do not use the @samp{-f} or @samp{--file} flag, the default is
6430 to try @file{GNUmakefile}, @file{makefile}, and @file{Makefile}, in
6431 that order, and use the first of these three which exists or can be made
6432 (@pxref{Makefiles, ,Writing Makefiles}).@refill
6434 @node Goals, Instead of Execution, Makefile Arguments, Running
6435 @section Arguments to Specify the Goals
6436 @cindex goal, how to specify
6438 The @dfn{goals} are the targets that @code{make} should strive ultimately
6439 to update.  Other targets are updated as well if they appear as
6440 prerequisites of goals, or prerequisites of prerequisites of goals, etc.
6442 By default, the goal is the first target in the makefile (not counting
6443 targets that start with a period).  Therefore, makefiles are usually
6444 written so that the first target is for compiling the entire program or
6445 programs they describe.  If the first rule in the makefile has several
6446 targets, only the first target in the rule becomes the default goal, not
6447 the whole list.
6449 You can specify a different goal or goals with arguments to @code{make}.
6450 Use the name of the goal as an argument.  If you specify several goals,
6451 @code{make} processes each of them in turn, in the order you name them.
6453 Any target in the makefile may be specified as a goal (unless it
6454 starts with @samp{-} or contains an @samp{=}, in which case it will be
6455 parsed as a switch or variable definition, respectively).  Even
6456 targets not in the makefile may be specified, if @code{make} can find
6457 implicit rules that say how to make them.
6459 @cindex @code{MAKECMDGOALS}
6460 @vindex MAKECMDGOALS
6461 @code{Make} will set the special variable @code{MAKECMDGOALS} to the
6462 list of goals you specified on the command line.  If no goals were given
6463 on the command line, this variable is empty.  Note that this variable
6464 should be used only in special circumstances.
6466 An example of appropriate use is to avoid including @file{.d} files
6467 during @code{clean} rules (@pxref{Automatic Prerequisites}), so
6468 @code{make} won't create them only to immediately remove them
6469 again:@refill
6471 @example
6472 @group
6473 sources = foo.c bar.c
6475 ifneq ($(MAKECMDGOALS),clean)
6476 include $(sources:.c=.d)
6477 endif
6478 @end group
6479 @end example
6481 One use of specifying a goal is if you want to compile only a part of
6482 the program, or only one of several programs.  Specify as a goal each
6483 file that you wish to remake.  For example, consider a directory containing
6484 several programs, with a makefile that starts like this:
6486 @example
6487 .PHONY: all
6488 all: size nm ld ar as
6489 @end example
6491 If you are working on the program @code{size}, you might want to say
6492 @w{@samp{make size}} so that only the files of that program are recompiled.
6494 Another use of specifying a goal is to make files that are not normally
6495 made.  For example, there may be a file of debugging output, or a
6496 version of the program that is compiled specially for testing, which has
6497 a rule in the makefile but is not a prerequisite of the default goal.
6499 Another use of specifying a goal is to run the commands associated with
6500 a phony target (@pxref{Phony Targets}) or empty target (@pxref{Empty
6501 Targets, ,Empty Target Files to Record Events}).  Many makefiles contain
6502 a phony target named @file{clean} which deletes everything except source
6503 files.  Naturally, this is done only if you request it explicitly with
6504 @w{@samp{make clean}}.  Following is a list of typical phony and empty
6505 target names.  @xref{Standard Targets}, for a detailed list of all the
6506 standard target names which GNU software packages use.
6508 @table @file
6509 @item all
6510 @cindex @code{all} @r{(standard target)}
6511 Make all the top-level targets the makefile knows about.
6513 @item clean
6514 @cindex @code{clean} @r{(standard target)}
6515 Delete all files that are normally created by running @code{make}.
6517 @item mostlyclean
6518 @cindex @code{mostlyclean} @r{(standard target)}
6519 Like @samp{clean}, but may refrain from deleting a few files that people
6520 normally don't want to recompile.  For example, the @samp{mostlyclean}
6521 target for GCC does not delete @file{libgcc.a}, because recompiling it
6522 is rarely necessary and takes a lot of time.
6524 @item distclean
6525 @cindex @code{distclean} @r{(standard target)}
6526 @itemx realclean
6527 @cindex @code{realclean} @r{(standard target)}
6528 @itemx clobber
6529 @cindex @code{clobber} @r{(standard target)}
6530 Any of these targets might be defined to delete @emph{more} files than
6531 @samp{clean} does.  For example, this would delete configuration files
6532 or links that you would normally create as preparation for compilation,
6533 even if the makefile itself cannot create these files.
6535 @item install
6536 @cindex @code{install} @r{(standard target)}
6537 Copy the executable file into a directory that users typically search
6538 for commands; copy any auxiliary files that the executable uses into
6539 the directories where it will look for them.
6541 @item print
6542 @cindex @code{print} @r{(standard target)}
6543 Print listings of the source files that have changed.
6545 @item tar
6546 @cindex @code{tar} @r{(standard target)}
6547 Create a tar file of the source files.
6549 @item shar
6550 @cindex @code{shar} @r{(standard target)}
6551 Create a shell archive (shar file) of the source files.
6553 @item dist
6554 @cindex @code{dist} @r{(standard target)}
6555 Create a distribution file of the source files.  This might
6556 be a tar file, or a shar file, or a compressed version of one of the
6557 above, or even more than one of the above.
6559 @item TAGS
6560 @cindex @code{TAGS} @r{(standard target)}
6561 Update a tags table for this program.
6563 @item check
6564 @cindex @code{check} @r{(standard target)}
6565 @itemx test
6566 @cindex @code{test} @r{(standard target)}
6567 Perform self tests on the program this makefile builds.
6568 @end table
6570 @node Instead of Execution, Avoiding Compilation, Goals, Running
6571 @section Instead of Executing the Commands
6572 @cindex execution, instead of
6573 @cindex commands, instead of executing
6575 The makefile tells @code{make} how to tell whether a target is up to date,
6576 and how to update each target.  But updating the targets is not always
6577 what you want.  Certain options specify other activities for @code{make}.
6579 @comment Extra blank lines make it print better.
6580 @table @samp
6581 @item -n
6582 @itemx --just-print
6583 @itemx --dry-run
6584 @itemx --recon
6585 @cindex @code{--just-print}
6586 @cindex @code{--dry-run}
6587 @cindex @code{--recon}
6588 @cindex @code{-n}
6590 ``No-op''.  The activity is to print what commands would be used to make
6591 the targets up to date, but not actually execute them.
6593 @item -t
6594 @itemx --touch
6595 @cindex @code{--touch}
6596 @cindex touching files
6597 @cindex target, touching
6598 @cindex @code{-t}
6600 ``Touch''.  The activity is to mark the targets as up to date without
6601 actually changing them.  In other words, @code{make} pretends to compile
6602 the targets but does not really change their contents.
6604 @item -q
6605 @itemx --question
6606 @cindex @code{--question}
6607 @cindex @code{-q}
6608 @cindex question mode
6610 ``Question''.  The activity is to find out silently whether the targets
6611 are up to date already; but execute no commands in either case.  In other
6612 words, neither compilation nor output will occur.
6614 @item -W @var{file}
6615 @itemx --what-if=@var{file}
6616 @itemx --assume-new=@var{file}
6617 @itemx --new-file=@var{file}
6618 @cindex @code{--what-if}
6619 @cindex @code{-W}
6620 @cindex @code{--assume-new}
6621 @cindex @code{--new-file}
6622 @cindex what if
6623 @cindex files, assuming new
6625 ``What if''.  Each @samp{-W} flag is followed by a file name.  The given
6626 files' modification times are recorded by @code{make} as being the present
6627 time, although the actual modification times remain the same.
6628 You can use the @samp{-W} flag in conjunction with the @samp{-n} flag
6629 to see what would happen if you were to modify specific files.@refill
6630 @end table
6632 With the @samp{-n} flag, @code{make} prints the commands that it would
6633 normally execute but does not execute them.
6635 With the @samp{-t} flag, @code{make} ignores the commands in the rules
6636 and uses (in effect) the command @code{touch} for each target that needs to
6637 be remade.  The @code{touch} command is also printed, unless @samp{-s} or
6638 @code{.SILENT} is used.  For speed, @code{make} does not actually invoke
6639 the program @code{touch}.  It does the work directly.
6641 With the @samp{-q} flag, @code{make} prints nothing and executes no
6642 commands, but the exit status code it returns is zero if and only if the
6643 targets to be considered are already up to date.  If the exit status is
6644 one, then some updating needs to be done.  If @code{make} encounters an
6645 error, the exit status is two, so you can distinguish an error from a
6646 target that is not up to date.
6648 It is an error to use more than one of these three flags in the same
6649 invocation of @code{make}.
6651 The @samp{-n}, @samp{-t}, and @samp{-q} options do not affect command
6652 lines that begin with @samp{+} characters or contain the strings
6653 @samp{$(MAKE)} or @samp{$@{MAKE@}}.  Note that only the line containing
6654 the @samp{+} character or the strings @samp{$(MAKE)} or @samp{$@{MAKE@}}
6655 is run regardless of these options.  Other lines in the same rule are
6656 not run unless they too begin with @samp{+} or contain @samp{$(MAKE)} or
6657 @samp{$@{MAKE@}} (@xref{MAKE Variable, ,How the @code{MAKE} Variable Works}.)
6659 The @samp{-W} flag provides two features:
6661 @itemize @bullet
6662 @item
6663 If you also use the @samp{-n} or @samp{-q} flag, you can see what
6664 @code{make} would do if you were to modify some files.
6666 @item
6667 Without the @samp{-n} or @samp{-q} flag, when @code{make} is actually
6668 executing commands, the @samp{-W} flag can direct @code{make} to act
6669 as if some files had been modified, without actually modifying the
6670 files.@refill
6671 @end itemize
6673 Note that the options @samp{-p} and @samp{-v} allow you to obtain other
6674 information about @code{make} or about the makefiles in use
6675 (@pxref{Options Summary, ,Summary of Options}).@refill
6677 @node Avoiding Compilation, Overriding, Instead of Execution, Running
6678 @section Avoiding Recompilation of Some Files
6679 @cindex @code{-o}
6680 @cindex @code{--old-file}
6681 @cindex @code{--assume-old}
6682 @cindex files, assuming old
6683 @cindex files, avoiding recompilation of
6684 @cindex recompilation, avoiding
6686 Sometimes you may have changed a source file but you do not want to
6687 recompile all the files that depend on it.  For example, suppose you add
6688 a macro or a declaration to a header file that many other files depend
6689 on.  Being conservative, @code{make} assumes that any change in the
6690 header file requires recompilation of all dependent files, but you know
6691 that they do not need to be recompiled and you would rather not waste
6692 the time waiting for them to compile.
6694 If you anticipate the problem before changing the header file, you can
6695 use the @samp{-t} flag.  This flag tells @code{make} not to run the
6696 commands in the rules, but rather to mark the target up to date by
6697 changing its last-modification date.  You would follow this procedure:
6699 @enumerate
6700 @item
6701 Use the command @samp{make} to recompile the source files that really
6702 need recompilation.
6704 @item
6705 Make the changes in the header files.
6707 @item
6708 Use the command @samp{make -t} to mark all the object files as
6709 up to date.  The next time you run @code{make}, the changes in the
6710 header files will not cause any recompilation.
6711 @end enumerate
6713 If you have already changed the header file at a time when some files
6714 do need recompilation, it is too late to do this.  Instead, you can
6715 use the @w{@samp{-o @var{file}}} flag, which marks a specified file as
6716 ``old'' (@pxref{Options Summary, ,Summary of Options}).  This means
6717 that the file itself will not be remade, and nothing else will be
6718 remade on its account.  Follow this procedure:
6720 @enumerate
6721 @item
6722 Recompile the source files that need compilation for reasons independent
6723 of the particular header file, with @samp{make -o @var{headerfile}}.
6724 If several header files are involved, use a separate @samp{-o} option
6725 for each header file.
6727 @item
6728 Touch all the object files with @samp{make -t}.
6729 @end enumerate
6731 @node Overriding, Testing, Avoiding Compilation, Running
6732 @section Overriding Variables
6733 @cindex overriding variables with arguments
6734 @cindex variables, overriding with arguments
6735 @cindex command line variables
6736 @cindex variables, command line
6738 An argument that contains @samp{=} specifies the value of a variable:
6739 @samp{@var{v}=@var{x}} sets the value of the variable @var{v} to @var{x}.
6740 If you specify a value in this way, all ordinary assignments of the same
6741 variable in the makefile are ignored; we say they have been
6742 @dfn{overridden} by the command line argument.
6744 The most common way to use this facility is to pass extra flags to
6745 compilers.  For example, in a properly written makefile, the variable
6746 @code{CFLAGS} is included in each command that runs the C compiler, so a
6747 file @file{foo.c} would be compiled something like this:
6749 @example
6750 cc -c $(CFLAGS) foo.c
6751 @end example
6753 Thus, whatever value you set for @code{CFLAGS} affects each compilation
6754 that occurs.  The makefile probably specifies the usual value for
6755 @code{CFLAGS}, like this:
6757 @example
6758 CFLAGS=-g
6759 @end example
6761 Each time you run @code{make}, you can override this value if you
6762 wish.  For example, if you say @samp{make CFLAGS='-g -O'}, each C
6763 compilation will be done with @samp{cc -c -g -O}.  (This illustrates
6764 how you can use quoting in the shell to enclose spaces and other
6765 special characters in the value of a variable when you override it.)
6767 The variable @code{CFLAGS} is only one of many standard variables that
6768 exist just so that you can change them this way.  @xref{Implicit
6769 Variables, , Variables Used by Implicit Rules}, for a complete list.
6771 You can also program the makefile to look at additional variables of your
6772 own, giving the user the ability to control other aspects of how the
6773 makefile works by changing the variables.
6775 When you override a variable with a command argument, you can define either
6776 a recursively-expanded variable or a simply-expanded variable.  The
6777 examples shown above make a recursively-expanded variable; to make a
6778 simply-expanded variable, write @samp{:=} instead of @samp{=}.  But, unless
6779 you want to include a variable reference or function call in the
6780 @emph{value} that you specify, it makes no difference which kind of
6781 variable you create.
6783 There is one way that the makefile can change a variable that you have
6784 overridden.  This is to use the @code{override} directive, which is a line
6785 that looks like this: @samp{override @var{variable} = @var{value}}
6786 (@pxref{Override Directive, ,The @code{override} Directive}).
6788 @node Testing, Options Summary, Overriding, Running
6789 @section Testing the Compilation of a Program
6790 @cindex testing compilation
6791 @cindex compilation, testing
6793 Normally, when an error happens in executing a shell command, @code{make}
6794 gives up immediately, returning a nonzero status.  No further commands are
6795 executed for any target.  The error implies that the goal cannot be
6796 correctly remade, and @code{make} reports this as soon as it knows.
6798 When you are compiling a program that you have just changed, this is not
6799 what you want.  Instead, you would rather that @code{make} try compiling
6800 every file that can be tried, to show you as many compilation errors
6801 as possible.
6803 @cindex @code{-k}
6804 @cindex @code{--keep-going}
6805 On these occasions, you should use the @samp{-k} or
6806 @samp{--keep-going} flag.  This tells @code{make} to continue to
6807 consider the other prerequisites of the pending targets, remaking them
6808 if necessary, before it gives up and returns nonzero status.  For
6809 example, after an error in compiling one object file, @samp{make -k}
6810 will continue compiling other object files even though it already
6811 knows that linking them will be impossible.  In addition to continuing
6812 after failed shell commands, @samp{make -k} will continue as much as
6813 possible after discovering that it does not know how to make a target
6814 or prerequisite file.  This will always cause an error message, but
6815 without @samp{-k}, it is a fatal error (@pxref{Options Summary,
6816 ,Summary of Options}).@refill
6818 The usual behavior of @code{make} assumes that your purpose is to get the
6819 goals up to date; once @code{make} learns that this is impossible, it might
6820 as well report the failure immediately.  The @samp{-k} flag says that the
6821 real purpose is to test as much as possible of the changes made in the
6822 program, perhaps to find several independent problems so that you can
6823 correct them all before the next attempt to compile.  This is why Emacs'
6824 @kbd{M-x compile} command passes the @samp{-k} flag by default.
6826 @node Options Summary,  , Testing, Running
6827 @section Summary of Options
6828 @cindex options
6829 @cindex flags
6830 @cindex switches
6832 Here is a table of all the options @code{make} understands:
6834 @table @samp
6835 @item -b
6836 @cindex @code{-b}
6837 @itemx -m
6838 @cindex @code{-m}
6839 These options are ignored for compatibility with other versions of @code{make}.
6841 @item -C @var{dir}
6842 @cindex @code{-C}
6843 @itemx --directory=@var{dir}
6844 @cindex @code{--directory}
6845 Change to directory @var{dir} before reading the makefiles.  If multiple
6846 @samp{-C} options are specified, each is interpreted relative to the
6847 previous one: @samp{-C / -C etc} is equivalent to @samp{-C /etc}.
6848 This is typically used with recursive invocations of @code{make}
6849 (@pxref{Recursion, ,Recursive Use of @code{make}}).
6851 @item -d
6852 @cindex @code{-d}
6853 @c Extra blank line here makes the table look better.
6855 Print debugging information in addition to normal processing.  The
6856 debugging information says which files are being considered for
6857 remaking, which file-times are being compared and with what results,
6858 which files actually need to be remade, which implicit rules are
6859 considered and which are applied---everything interesting about how
6860 @code{make} decides what to do.  The @code{-d} option is equivalent to
6861 @samp{--debug=a} (see below).
6863 @item --debug[=@var{options}]
6864 @cindex @code{--debug}
6865 @c Extra blank line here makes the table look better.
6867 Print debugging information in addition to normal processing.  Various
6868 levels and types of output can be chosen.  With no arguments, print the
6869 ``basic'' level of debugging.  Possible arguments are below; only the
6870 first character is considered, and values must be comma- or
6871 space-separated.
6873 @table @code
6874 @item a (@i{all})
6875 All types of debugging output are enabled.  This is equivalent to using
6876 @samp{-d}.
6878 @item b (@i{basic})
6879 Basic debugging prints each target that was found to be out-of-date, and
6880 whether the build was successful or not.
6882 @item v (@i{verbose})
6883 A level above @samp{basic}; includes messages about which makefiles were
6884 parsed, prerequisites that did not need to be rebuilt, etc.  This option
6885 also enables @samp{basic} messages.
6887 @item i (@i{implicit})
6888 Prints messages describing the implicit rule searches for each target.
6889 This option also enables @samp{basic} messages.
6891 @item j (@i{jobs})
6892 Prints messages giving details on the invocation of specific subcommands.
6894 @item m (@i{makefile})
6895 By default, the above messages are not enabled while trying to remake
6896 the makefiles.  This option enables messages while rebuilding makefiles,
6897 too.  Note that the @samp{all} option does enable this option.  This
6898 option also enables @samp{basic} messages.
6899 @end table
6901 @item -e
6902 @cindex @code{-e}
6903 @itemx --environment-overrides
6904 @cindex @code{--environment-overrides}
6905 Give variables taken from the environment precedence
6906 over variables from makefiles.
6907 @xref{Environment, ,Variables from the Environment}.
6909 @item -f @var{file}
6910 @cindex @code{-f}
6911 @itemx --file=@var{file}
6912 @cindex @code{--file}
6913 @itemx --makefile=@var{file}
6914 @cindex @code{--makefile}
6915 Read the file named @var{file} as a makefile.
6916 @xref{Makefiles, ,Writing Makefiles}.
6918 @item -h
6919 @cindex @code{-h}
6920 @itemx --help
6921 @cindex @code{--help}
6922 @c Extra blank line here makes the table look better.
6924 Remind you of the options that @code{make} understands and then exit.
6926 @item -i
6927 @cindex @code{-i}
6928 @itemx --ignore-errors
6929 @cindex @code{--ignore-errors}
6930 Ignore all errors in commands executed to remake files.
6931 @xref{Errors, ,Errors in Commands}.
6933 @item -I @var{dir}
6934 @cindex @code{-I}
6935 @itemx --include-dir=@var{dir}
6936 @cindex @code{--include-dir}
6937 Specifies a directory @var{dir} to search for included makefiles.
6938 @xref{Include, ,Including Other Makefiles}.  If several @samp{-I}
6939 options are used to specify several directories, the directories are
6940 searched in the order specified.
6942 @item -j [@var{jobs}]
6943 @cindex @code{-j}
6944 @itemx --jobs[=@var{jobs}]
6945 @cindex @code{--jobs}
6946 Specifies the number of jobs (commands) to run simultaneously.  With no
6947 argument, @code{make} runs as many jobs simultaneously as possible.  If
6948 there is more than one @samp{-j} option, the last one is effective.
6949 @xref{Parallel, ,Parallel Execution},
6950 for more information on how commands are run.
6951 Note that this option is ignored on MS-DOS.
6953 @item -k
6954 @cindex @code{-k}
6955 @itemx --keep-going
6956 @cindex @code{--keep-going}
6957 Continue as much as possible after an error.  While the target that
6958 failed, and those that depend on it, cannot be remade, the other
6959 prerequisites of these targets can be processed all the same.
6960 @xref{Testing, ,Testing the Compilation of a Program}.
6962 @item -l [@var{load}]
6963 @cindex @code{-l}
6964 @itemx --load-average[=@var{load}]
6965 @cindex @code{--load-average}
6966 @itemx --max-load[=@var{load}]
6967 @cindex @code{--max-load}
6968 Specifies that no new jobs (commands) should be started if there are
6969 other jobs running and the load average is at least @var{load} (a
6970 floating-point number).  With no argument, removes a previous load
6971 limit.  @xref{Parallel, ,Parallel Execution}.
6973 @item -n
6974 @cindex @code{-n}
6975 @itemx --just-print
6976 @cindex @code{--just-print}
6977 @itemx --dry-run
6978 @cindex @code{--dry-run}
6979 @itemx --recon
6980 @cindex @code{--recon}
6981 @c Extra blank line here makes the table look better.
6983 Print the commands that would be executed, but do not execute them.
6984 @xref{Instead of Execution, ,Instead of Executing the Commands}.
6986 @item -o @var{file}
6987 @cindex @code{-o}
6988 @itemx --old-file=@var{file}
6989 @cindex @code{--old-file}
6990 @itemx --assume-old=@var{file}
6991 @cindex @code{--assume-old}
6992 Do not remake the file @var{file} even if it is older than its
6993 prerequisites, and do not remake anything on account of changes in
6994 @var{file}.  Essentially the file is treated as very old and its rules
6995 are ignored.  @xref{Avoiding Compilation, ,Avoiding Recompilation of
6996 Some Files}.@refill
6998 @item -p
6999 @cindex @code{-p}
7000 @itemx --print-data-base
7001 @cindex @code{--print-data-base}
7002 @cindex data base of @code{make} rules
7003 @cindex predefined rules and variables, printing
7004 Print the data base (rules and variable values) that results from
7005 reading the makefiles; then execute as usual or as otherwise specified.
7006 This also prints the version information given by the @samp{-v} switch
7007 (see below).  To print the data base without trying to remake any files,
7008 use @w{@samp{make -qp}}.  To print the data base of predefined rules and
7009 variables, use @w{@samp{make -p -f /dev/null}}.  The data base output
7010 contains filename and linenumber information for command and variable
7011 definitions, so it can be a useful debugging tool in complex environments.
7013 @item -q
7014 @cindex @code{-q}
7015 @itemx --question
7016 @cindex @code{--question}
7017 ``Question mode''.  Do not run any commands, or print anything; just
7018 return an exit status that is zero if the specified targets are already
7019 up to date, one if any remaking is required, or two if an error is
7020 encountered.  @xref{Instead of Execution, ,Instead of Executing the
7021 Commands}.@refill
7023 @item -r
7024 @cindex @code{-r}
7025 @itemx --no-builtin-rules
7026 @cindex @code{--no-builtin-rules}
7027 Eliminate use of the built-in implicit rules (@pxref{Implicit Rules,
7028 ,Using Implicit Rules}).  You can still define your own by writing
7029 pattern rules (@pxref{Pattern Rules, ,Defining and Redefining Pattern
7030 Rules}).  The @samp{-r} option also clears out the default list of
7031 suffixes for suffix rules (@pxref{Suffix Rules, ,Old-Fashioned Suffix
7032 Rules}).  But you can still define your own suffixes with a rule for
7033 @code{.SUFFIXES}, and then define your own suffix rules.  Note that only
7034 @emph{rules} are affected by the @code{-r} option; default variables
7035 remain in effect (@pxref{Implicit Variables, ,Variables Used by Implicit
7036 Rules}); see the @samp{-R} option below.
7038 @item -R
7039 @cindex @code{-R}
7040 @itemx --no-builtin-variables
7041 @cindex @code{--no-builtin-variables}
7042 Eliminate use of the built-in rule-specific variables (@pxref{Implicit
7043 Variables, ,Variables Used by Implicit Rules}).  You can still define
7044 your own, of course.  The @samp{-R} option also automatically enables
7045 the @samp{-r} option (see above), since it doesn't make sense to have
7046 implicit rules without any definitions for the variables that they use.
7048 @item -s
7049 @cindex @code{-s}
7050 @itemx --silent
7051 @cindex @code{--silent}
7052 @itemx --quiet
7053 @cindex @code{--quiet}
7054 @c Extra blank line here makes the table look better.
7056 Silent operation; do not print the commands as they are executed.
7057 @xref{Echoing, ,Command Echoing}.
7059 @item -S
7060 @cindex @code{-S}
7061 @itemx --no-keep-going
7062 @cindex @code{--no-keep-going}
7063 @itemx --stop
7064 @cindex @code{--stop}
7065 @c Extra blank line here makes the table look better.
7067 Cancel the effect of the @samp{-k} option.  This is never necessary
7068 except in a recursive @code{make} where @samp{-k} might be inherited
7069 from the top-level @code{make} via @code{MAKEFLAGS}
7070 (@pxref{Recursion, ,Recursive Use of @code{make}})
7071 or if you set @samp{-k} in @code{MAKEFLAGS} in your environment.@refill
7073 @item -t
7074 @cindex @code{-t}
7075 @itemx --touch
7076 @cindex @code{--touch}
7077 @c Extra blank line here makes the table look better.
7079 Touch files (mark them up to date without really changing them)
7080 instead of running their commands.  This is used to pretend that the
7081 commands were done, in order to fool future invocations of
7082 @code{make}.  @xref{Instead of Execution, ,Instead of Executing the Commands}.
7084 @item -v
7085 @cindex @code{-v}
7086 @itemx --version
7087 @cindex @code{--version}
7088 Print the version of the @code{make} program plus a copyright, a list
7089 of authors, and a notice that there is no warranty; then exit.
7091 @item -w
7092 @cindex @code{-w}
7093 @itemx --print-directory
7094 @cindex @code{--print-directory}
7095 Print a message containing the working directory both before and after
7096 executing the makefile.  This may be useful for tracking down errors
7097 from complicated nests of recursive @code{make} commands.
7098 @xref{Recursion, ,Recursive Use of @code{make}}.  (In practice, you
7099 rarely need to specify this option since @samp{make} does it for you;
7100 see @ref{-w Option, ,The @samp{--print-directory} Option}.)
7102 @itemx --no-print-directory
7103 @cindex @code{--no-print-directory}
7104 Disable printing of the working directory under @code{-w}.
7105 This option is useful when @code{-w} is turned on automatically,
7106 but you do not want to see the extra messages.
7107 @xref{-w Option, ,The @samp{--print-directory} Option}.
7109 @item -W @var{file}
7110 @cindex @code{-W}
7111 @itemx --what-if=@var{file}
7112 @cindex @code{--what-if}
7113 @itemx --new-file=@var{file}
7114 @cindex @code{--new-file}
7115 @itemx --assume-new=@var{file}
7116 @cindex @code{--assume-new}
7117 Pretend that the target @var{file} has just been modified.  When used
7118 with the @samp{-n} flag, this shows you what would happen if you were
7119 to modify that file.  Without @samp{-n}, it is almost the same as
7120 running a @code{touch} command on the given file before running
7121 @code{make}, except that the modification time is changed only in the
7122 imagination of @code{make}.
7123 @xref{Instead of Execution, ,Instead of Executing the Commands}.
7125 @item --warn-undefined-variables
7126 @cindex @code{--warn-undefined-variables}
7127 @cindex variables, warning for undefined
7128 @cindex undefined variables, warning message
7129 Issue a warning message whenever @code{make} sees a reference to an
7130 undefined variable.  This can be helpful when you are trying to debug
7131 makefiles which use variables in complex ways.
7132 @end table
7134 @node Implicit Rules, Archives, Running, Top
7135 @chapter Using Implicit Rules
7136 @cindex implicit rule
7137 @cindex rule, implicit
7139 Certain standard ways of remaking target files are used very often.  For
7140 example, one customary way to make an object file is from a C source file
7141 using the C compiler, @code{cc}.
7143 @dfn{Implicit rules} tell @code{make} how to use customary techniques so
7144 that you do not have to specify them in detail when you want to use
7145 them.  For example, there is an implicit rule for C compilation.  File
7146 names determine which implicit rules are run.  For example, C
7147 compilation typically takes a @file{.c} file and makes a @file{.o} file.
7148 So @code{make} applies the implicit rule for C compilation when it sees
7149 this combination of file name endings.@refill
7151 A chain of implicit rules can apply in sequence; for example, @code{make}
7152 will remake a @file{.o} file from a @file{.y} file by way of a @file{.c} file.
7153 @iftex
7154 @xref{Chained Rules, ,Chains of Implicit Rules}.
7155 @end iftex
7157 The built-in implicit rules use several variables in their commands so
7158 that, by changing the values of the variables, you can change the way the
7159 implicit rule works.  For example, the variable @code{CFLAGS} controls the
7160 flags given to the C compiler by the implicit rule for C compilation.
7161 @iftex
7162 @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
7163 @end iftex
7165 You can define your own implicit rules by writing @dfn{pattern rules}.
7166 @iftex
7167 @xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.
7168 @end iftex
7170 @dfn{Suffix rules} are a more limited way to define implicit rules.
7171 Pattern rules are more general and clearer, but suffix rules are
7172 retained for compatibility.
7173 @iftex
7174 @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
7175 @end iftex
7177 @menu
7178 * Using Implicit::              How to use an existing implicit rule
7179                                   to get the commands for updating a file.
7180 * Catalogue of Rules::          A list of built-in implicit rules.
7181 * Implicit Variables::          How to change what predefined rules do.
7182 * Chained Rules::               How to use a chain of implicit rules.
7183 * Pattern Rules::               How to define new implicit rules.
7184 * Last Resort::                 How to defining commands for rules
7185                                   which cannot find any.
7186 * Suffix Rules::                The old-fashioned style of implicit rule.
7187 * Implicit Rule Search::        The precise algorithm for applying
7188                                   implicit rules.
7189 @end menu
7191 @node Using Implicit, Catalogue of Rules,  , Implicit Rules
7192 @section Using Implicit Rules
7193 @cindex implicit rule, how to use
7194 @cindex rule, implicit, how to use
7196 To allow @code{make} to find a customary method for updating a target file,
7197 all you have to do is refrain from specifying commands yourself.  Either
7198 write a rule with no command lines, or don't write a rule at all.  Then
7199 @code{make} will figure out which implicit rule to use based on which
7200 kind of source file exists or can be made.
7202 For example, suppose the makefile looks like this:
7204 @example
7205 foo : foo.o bar.o
7206         cc -o foo foo.o bar.o $(CFLAGS) $(LDFLAGS)
7207 @end example
7209 @noindent
7210 Because you mention @file{foo.o} but do not give a rule for it, @code{make}
7211 will automatically look for an implicit rule that tells how to update it.
7212 This happens whether or not the file @file{foo.o} currently exists.
7214 If an implicit rule is found, it can supply both commands and one or
7215 more prerequisites (the source files).  You would want to write a rule
7216 for @file{foo.o} with no command lines if you need to specify additional
7217 prerequisites, such as header files, that the implicit rule cannot
7218 supply.
7220 Each implicit rule has a target pattern and prerequisite patterns.  There may
7221 be many implicit rules with the same target pattern.  For example, numerous
7222 rules make @samp{.o} files: one, from a @samp{.c} file with the C compiler;
7223 another, from a @samp{.p} file with the Pascal compiler; and so on.  The rule
7224 that actually applies is the one whose prerequisites exist or can be made.
7225 So, if you have a file @file{foo.c}, @code{make} will run the C compiler;
7226 otherwise, if you have a file @file{foo.p}, @code{make} will run the Pascal
7227 compiler; and so on.
7229 Of course, when you write the makefile, you know which implicit rule you
7230 want @code{make} to use, and you know it will choose that one because you
7231 know which possible prerequisite files are supposed to exist.
7232 @xref{Catalogue of Rules, ,Catalogue of Implicit Rules},
7233 for a catalogue of all the predefined implicit rules.
7235 Above, we said an implicit rule applies if the required prerequisites ``exist
7236 or can be made''.  A file ``can be made'' if it is mentioned explicitly in
7237 the makefile as a target or a prerequisite, or if an implicit rule can be
7238 recursively found for how to make it.  When an implicit prerequisite is the
7239 result of another implicit rule, we say that @dfn{chaining} is occurring.
7240 @xref{Chained Rules, ,Chains of Implicit Rules}.
7242 In general, @code{make} searches for an implicit rule for each target, and
7243 for each double-colon rule, that has no commands.  A file that is mentioned
7244 only as a prerequisite is considered a target whose rule specifies nothing,
7245 so implicit rule search happens for it.  @xref{Implicit Rule Search, ,Implicit Rule Search Algorithm}, for the
7246 details of how the search is done.
7248 Note that explicit prerequisites do not influence implicit rule search.
7249 For example, consider this explicit rule:
7251 @example
7252 foo.o: foo.p
7253 @end example
7255 @noindent
7256 The prerequisite on @file{foo.p} does not necessarily mean that
7257 @code{make} will remake @file{foo.o} according to the implicit rule to
7258 make an object file, a @file{.o} file, from a Pascal source file, a
7259 @file{.p} file.  For example, if @file{foo.c} also exists, the implicit
7260 rule to make an object file from a C source file is used instead,
7261 because it appears before the Pascal rule in the list of predefined
7262 implicit rules (@pxref{Catalogue of Rules, , Catalogue of Implicit
7263 Rules}).
7265 If you do not want an implicit rule to be used for a target that has no
7266 commands, you can give that target empty commands by writing a semicolon
7267 (@pxref{Empty Commands, ,Defining Empty Commands}).
7269 @node Catalogue of Rules, Implicit Variables, Using Implicit, Implicit Rules
7270 @section Catalogue of Implicit Rules
7271 @cindex implicit rule, predefined
7272 @cindex rule, implicit, predefined
7274 Here is a catalogue of predefined implicit rules which are always
7275 available unless the makefile explicitly overrides or cancels them.
7276 @xref{Canceling Rules, ,Canceling Implicit Rules}, for information on
7277 canceling or overriding an implicit rule.  The @samp{-r} or
7278 @samp{--no-builtin-rules} option cancels all predefined rules.
7280 Not all of these rules will always be defined, even when the @samp{-r}
7281 option is not given.  Many of the predefined implicit rules are
7282 implemented in @code{make} as suffix rules, so which ones will be
7283 defined depends on the @dfn{suffix list} (the list of prerequisites of
7284 the special target @code{.SUFFIXES}).  The default suffix list is:
7285 @code{.out}, @code{.a}, @code{.ln}, @code{.o}, @code{.c}, @code{.cc},
7286 @code{.C}, @code{.p}, @code{.f}, @code{.F}, @code{.r}, @code{.y},
7287 @code{.l}, @code{.s}, @code{.S}, @code{.mod}, @code{.sym}, @code{.def},
7288 @code{.h}, @code{.info}, @code{.dvi}, @code{.tex}, @code{.texinfo},
7289 @code{.texi}, @code{.txinfo}, @code{.w}, @code{.ch} @code{.web},
7290 @code{.sh}, @code{.elc}, @code{.el}.  All of the implicit rules
7291 described below whose prerequisites have one of these suffixes are
7292 actually suffix rules.  If you modify the suffix list, the only
7293 predefined suffix rules in effect will be those named by one or two of
7294 the suffixes that are on the list you specify; rules whose suffixes fail
7295 to be on the list are disabled.  @xref{Suffix Rules, ,Old-Fashioned
7296 Suffix Rules}, for full details on suffix rules.
7298 @table @asis
7299 @item Compiling C programs
7300 @cindex C, rule to compile
7301 @pindex cc
7302 @pindex gcc
7303 @pindex .o
7304 @pindex .c
7305 @file{@var{n}.o} is made automatically from @file{@var{n}.c} with
7306 a command of the form @samp{$(CC) -c $(CPPFLAGS) $(CFLAGS)}.@refill
7308 @item Compiling C++ programs
7309 @cindex C++, rule to compile
7310 @pindex g++
7311 @pindex .C
7312 @pindex .cc
7313 @file{@var{n}.o} is made automatically from @file{@var{n}.cc} or
7314 @file{@var{n}.C} with a command of the form @samp{$(CXX) -c $(CPPFLAGS)
7315 $(CXXFLAGS)}.  We encourage you to use the suffix @samp{.cc} for C++
7316 source files instead of @samp{.C}.@refill
7318 @item Compiling Pascal programs
7319 @cindex Pascal, rule to compile
7320 @pindex pc
7321 @pindex .p
7322 @file{@var{n}.o} is made automatically from @file{@var{n}.p}
7323 with the command @samp{$(PC) -c $(PFLAGS)}.@refill
7325 @item Compiling Fortran and Ratfor programs
7326 @cindex Fortran, rule to compile
7327 @cindex Ratfor, rule to compile
7328 @pindex f77
7329 @pindex .f
7330 @pindex .r
7331 @pindex .F
7332 @file{@var{n}.o} is made automatically from @file{@var{n}.r},
7333 @file{@var{n}.F} or @file{@var{n}.f} by running the
7334 Fortran compiler.  The precise command used is as follows:@refill
7336 @table @samp
7337 @item .f
7338 @samp{$(FC) -c $(FFLAGS)}.
7339 @item .F
7340 @samp{$(FC) -c $(FFLAGS) $(CPPFLAGS)}.
7341 @item .r
7342 @samp{$(FC) -c $(FFLAGS) $(RFLAGS)}.
7343 @end table
7345 @item Preprocessing Fortran and Ratfor programs
7346 @file{@var{n}.f} is made automatically from @file{@var{n}.r} or
7347 @file{@var{n}.F}.  This rule runs just the preprocessor to convert a
7348 Ratfor or preprocessable Fortran program into a strict Fortran
7349 program.  The precise command used is as follows:@refill
7351 @table @samp
7352 @item .F
7353 @samp{$(FC) -F $(CPPFLAGS) $(FFLAGS)}.
7354 @item .r
7355 @samp{$(FC) -F $(FFLAGS) $(RFLAGS)}.
7356 @end table
7358 @item Compiling Modula-2 programs
7359 @cindex Modula-2, rule to compile
7360 @pindex m2c
7361 @pindex .sym
7362 @pindex .def
7363 @pindex .mod
7364 @file{@var{n}.sym} is made from @file{@var{n}.def} with a command
7365 of the form @samp{$(M2C) $(M2FLAGS) $(DEFFLAGS)}.  @file{@var{n}.o}
7366 is made from @file{@var{n}.mod}; the form is:
7367 @w{@samp{$(M2C) $(M2FLAGS) $(MODFLAGS)}}.@refill
7369 @need 1200
7370 @item Assembling and preprocessing assembler programs
7371 @cindex assembly, rule to compile
7372 @pindex as
7373 @pindex .s
7374 @file{@var{n}.o} is made automatically from @file{@var{n}.s} by
7375 running the assembler, @code{as}.  The precise command is
7376 @samp{$(AS) $(ASFLAGS)}.@refill
7378 @pindex .S
7379 @file{@var{n}.s} is made automatically from @file{@var{n}.S} by
7380 running the C preprocessor, @code{cpp}.  The precise command is
7381 @w{@samp{$(CPP) $(CPPFLAGS)}}.
7383 @item Linking a single object file
7384 @cindex linking, predefined rule for
7385 @pindex ld
7386 @pindex .o
7387 @file{@var{n}} is made automatically from @file{@var{n}.o} by running
7388 the linker (usually called @code{ld}) via the C compiler.  The precise
7389 command used is @w{@samp{$(CC) $(LDFLAGS) @var{n}.o $(LOADLIBES) $(LDLIBS)}}.
7391 This rule does the right thing for a simple program with only one
7392 source file.  It will also do the right thing if there are multiple
7393 object files (presumably coming from various other source files), one
7394 of which has a name matching that of the executable file.  Thus,
7396 @example
7397 x: y.o z.o
7398 @end example
7400 @noindent
7401 when @file{x.c}, @file{y.c} and @file{z.c} all exist will execute:
7403 @example
7404 @group
7405 cc -c x.c -o x.o
7406 cc -c y.c -o y.o
7407 cc -c z.c -o z.o
7408 cc x.o y.o z.o -o x
7409 rm -f x.o
7410 rm -f y.o
7411 rm -f z.o
7412 @end group
7413 @end example
7415 @noindent
7416 In more complicated cases, such as when there is no object file whose
7417 name derives from the executable file name, you must write an explicit
7418 command for linking.
7420 Each kind of file automatically made into @samp{.o} object files will
7421 be automatically linked by using the compiler (@samp{$(CC)},
7422 @samp{$(FC)} or @samp{$(PC)}; the C compiler @samp{$(CC)} is used to
7423 assemble @samp{.s} files) without the @samp{-c} option.  This could be
7424 done by using the @samp{.o} object files as intermediates, but it is
7425 faster to do the compiling and linking in one step, so that's how it's
7426 done.@refill
7428 @item Yacc for C programs
7429 @pindex yacc
7430 @cindex Yacc, rule to run
7431 @pindex .y
7432 @file{@var{n}.c} is made automatically from @file{@var{n}.y} by
7433 running Yacc with the command @samp{$(YACC) $(YFLAGS)}.
7435 @item Lex for C programs
7436 @pindex lex
7437 @cindex Lex, rule to run
7438 @pindex .l
7439 @file{@var{n}.c} is made automatically from @file{@var{n}.l} by
7440 by running Lex.  The actual command is @samp{$(LEX) $(LFLAGS)}.
7442 @item Lex for Ratfor programs
7443 @file{@var{n}.r} is made automatically from @file{@var{n}.l} by
7444 by running Lex.  The actual command is @samp{$(LEX) $(LFLAGS)}.
7446 The convention of using the same suffix @samp{.l} for all Lex files
7447 regardless of whether they produce C code or Ratfor code makes it
7448 impossible for @code{make} to determine automatically which of the two
7449 languages you are using in any particular case.  If @code{make} is
7450 called upon to remake an object file from a @samp{.l} file, it must
7451 guess which compiler to use.  It will guess the C compiler, because
7452 that is more common.  If you are using Ratfor, make sure @code{make}
7453 knows this by mentioning @file{@var{n}.r} in the makefile.  Or, if you
7454 are using Ratfor exclusively, with no C files, remove @samp{.c} from
7455 the list of implicit rule suffixes with:@refill
7457 @example
7458 @group
7459 .SUFFIXES:
7460 .SUFFIXES: .o .r .f .l @dots{}
7461 @end group
7462 @end example
7464 @item Making Lint Libraries from C, Yacc, or Lex programs
7465 @pindex lint
7466 @cindex @code{lint}, rule to run
7467 @pindex .ln
7468 @file{@var{n}.ln} is made from @file{@var{n}.c} by running @code{lint}.
7469 The precise command is @w{@samp{$(LINT) $(LINTFLAGS) $(CPPFLAGS) -i}}.
7470 The same command is used on the C code produced from
7471 @file{@var{n}.y} or @file{@var{n}.l}.@refill
7473 @item @TeX{} and Web
7474 @cindex @TeX{}, rule to run
7475 @cindex Web, rule to run
7476 @pindex tex
7477 @pindex cweave
7478 @pindex weave
7479 @pindex tangle
7480 @pindex ctangle
7481 @pindex .dvi
7482 @pindex .tex
7483 @pindex .web
7484 @pindex .w
7485 @pindex .ch
7486 @file{@var{n}.dvi} is made from @file{@var{n}.tex} with the command
7487 @samp{$(TEX)}.  @file{@var{n}.tex} is made from @file{@var{n}.web} with
7488 @samp{$(WEAVE)}, or from @file{@var{n}.w} (and from @file{@var{n}.ch} if
7489 it exists or can be made) with @samp{$(CWEAVE)}.  @file{@var{n}.p} is
7490 made from @file{@var{n}.web} with @samp{$(TANGLE)} and @file{@var{n}.c}
7491 is made from @file{@var{n}.w} (and from @file{@var{n}.ch} if it exists
7492 or can be made) with @samp{$(CTANGLE)}.@refill
7494 @item Texinfo and Info
7495 @cindex Texinfo, rule to format
7496 @cindex Info, rule to format
7497 @pindex texi2dvi
7498 @pindex makeinfo
7499 @pindex .texinfo
7500 @pindex .info
7501 @pindex .texi
7502 @pindex .txinfo
7503 @file{@var{n}.dvi} is made from @file{@var{n}.texinfo},
7504 @file{@var{n}.texi}, or @file{@var{n}.txinfo}, with the command
7505 @w{@samp{$(TEXI2DVI) $(TEXI2DVI_FLAGS)}}.  @file{@var{n}.info} is made from
7506 @file{@var{n}.texinfo}, @file{@var{n}.texi}, or @file{@var{n}.txinfo}, with
7507 the command @w{@samp{$(MAKEINFO) $(MAKEINFO_FLAGS)}}.
7509 @item RCS
7510 @cindex RCS, rule to extract from
7511 @pindex co
7512 @pindex ,v @r{(RCS file extension)}
7513 Any file @file{@var{n}} is extracted if necessary from an RCS file
7514 named either @file{@var{n},v} or @file{RCS/@var{n},v}.  The precise
7515 command used is @w{@samp{$(CO) $(COFLAGS)}}.  @file{@var{n}} will not be
7516 extracted from RCS if it already exists, even if the RCS file is
7517 newer.  The rules for RCS are terminal
7518 (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}),
7519 so RCS files cannot be generated from another source; they must
7520 actually exist.@refill
7522 @item SCCS
7523 @cindex SCCS, rule to extract from
7524 @pindex get
7525 @pindex s. @r{(SCCS file prefix)}
7526 Any file @file{@var{n}} is extracted if necessary from an SCCS file
7527 named either @file{s.@var{n}} or @file{SCCS/s.@var{n}}.  The precise
7528 command used is @w{@samp{$(GET) $(GFLAGS)}}.  The rules for SCCS are
7529 terminal (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}),
7530 so SCCS files cannot be generated from another source; they must
7531 actually exist.@refill
7533 @pindex .sh
7534 For the benefit of SCCS, a file @file{@var{n}} is copied from
7535 @file{@var{n}.sh} and made executable (by everyone).  This is for
7536 shell scripts that are checked into SCCS.  Since RCS preserves the
7537 execution permission of a file, you do not need to use this feature
7538 with RCS.@refill
7540 We recommend that you avoid using of SCCS.  RCS is widely held to be
7541 superior, and is also free.  By choosing free software in place of
7542 comparable (or inferior) proprietary software, you support the free
7543 software movement.
7544 @end table
7546 Usually, you want to change only the variables listed in the table
7547 above, which are documented in the following section.
7549 However, the commands in built-in implicit rules actually use
7550 variables such as @code{COMPILE.c}, @code{LINK.p}, and
7551 @code{PREPROCESS.S}, whose values contain the commands listed above.
7553 @code{make} follows the convention that the rule to compile a
7554 @file{.@var{x}} source file uses the variable @code{COMPILE.@var{x}}.
7555 Similarly, the rule to produce an executable from a @file{.@var{x}}
7556 file uses @code{LINK.@var{x}}; and the rule to preprocess a
7557 @file{.@var{x}} file uses @code{PREPROCESS.@var{x}}.
7559 @vindex OUTPUT_OPTION
7560 Every rule that produces an object file uses the variable
7561 @code{OUTPUT_OPTION}.  @code{make} defines this variable either to
7562 contain @samp{-o $@@}, or to be empty, depending on a compile-time
7563 option.  You need the @samp{-o} option to ensure that the output goes
7564 into the right file when the source file is in a different directory,
7565 as when using @code{VPATH} (@pxref{Directory Search}).  However,
7566 compilers on some systems do not accept a @samp{-o} switch for object
7567 files.  If you use such a system, and use @code{VPATH}, some
7568 compilations will put their output in the wrong place.
7569 A possible workaround for this problem is to give @code{OUTPUT_OPTION}
7570 the value @w{@samp{; mv $*.o $@@}}.
7572 @node Implicit Variables, Chained Rules, Catalogue of Rules, Implicit Rules
7573 @section Variables Used by Implicit Rules
7574 @cindex flags for compilers
7576 The commands in built-in implicit rules make liberal use of certain
7577 predefined variables.  You can alter these variables in the makefile,
7578 with arguments to @code{make}, or in the environment to alter how the
7579 implicit rules work without redefining the rules themselves.  You can
7580 cancel all variables used by implicit rules with the @samp{-R} or
7581 @samp{--no-builtin-variables} option.
7583 For example, the command used to compile a C source file actually says
7584 @samp{$(CC) -c $(CFLAGS) $(CPPFLAGS)}.  The default values of the variables
7585 used are @samp{cc} and nothing, resulting in the command @samp{cc -c}.  By
7586 redefining @samp{CC} to @samp{ncc}, you could cause @samp{ncc} to be
7587 used for all C compilations performed by the implicit rule.  By redefining
7588 @samp{CFLAGS} to be @samp{-g}, you could pass the @samp{-g} option to
7589 each compilation.  @emph{All} implicit rules that do C compilation use
7590 @samp{$(CC)} to get the program name for the compiler and @emph{all}
7591 include @samp{$(CFLAGS)} among the arguments given to the compiler.@refill
7593 The variables used in implicit rules fall into two classes: those that are
7594 names of programs (like @code{CC}) and those that contain arguments for the
7595 programs (like @code{CFLAGS}).  (The ``name of a program'' may also contain
7596 some command arguments, but it must start with an actual executable program
7597 name.)  If a variable value contains more than one argument, separate them
7598 with spaces.
7600 Here is a table of variables used as names of programs in built-in rules:
7602 @table @code
7603 @item AR
7604 @vindex AR
7605 Archive-maintaining program; default @samp{ar}.
7606 @pindex ar
7608 @item AS
7609 @vindex AS
7610 Program for doing assembly; default @samp{as}.
7611 @pindex as
7613 @item CC
7614 @vindex CC
7615 Program for compiling C programs; default @samp{cc}.
7616 @pindex cc
7618 @item CXX
7619 @vindex CXX
7620 Program for compiling C++ programs; default @samp{g++}.
7621 @pindex g++
7623 @item CO
7624 @vindex CO
7625 Program for extracting a file from RCS; default @samp{co}.
7626 @pindex co
7628 @item CPP
7629 @vindex CPP
7630 Program for running the C preprocessor, with results to standard output;
7631 default @samp{$(CC) -E}.
7633 @item FC
7634 @vindex FC
7635 Program for compiling or preprocessing Fortran and Ratfor programs;
7636 default @samp{f77}.
7637 @pindex f77
7639 @item GET
7640 @vindex GET
7641 Program for extracting a file from SCCS; default @samp{get}.
7642 @pindex get
7644 @item LEX
7645 @vindex LEX
7646 Program to use to turn Lex grammars into C programs or Ratfor programs;
7647 default @samp{lex}.
7648 @pindex lex
7650 @item PC
7651 @vindex PC
7652 Program for compiling Pascal programs; default @samp{pc}.
7653 @pindex pc
7655 @item YACC
7656 @vindex YACC
7657 Program to use to turn Yacc grammars into C programs; default @samp{yacc}.
7658 @pindex yacc
7660 @item YACCR
7661 @vindex YACCR
7662 Program to use to turn Yacc grammars into Ratfor
7663 programs; default @samp{yacc -r}.
7665 @item MAKEINFO
7666 @vindex MAKEINFO
7667 Program to convert a Texinfo source file into an Info file; default
7668 @samp{makeinfo}.
7669 @pindex makeinfo
7671 @item TEX
7672 @vindex TEX
7673 Program to make @TeX{} @sc{dvi} files from @TeX{} source;
7674 default @samp{tex}.
7675 @pindex tex
7677 @item TEXI2DVI
7678 @vindex TEXI2DVI
7679 Program to make @TeX{} @sc{dvi} files from Texinfo source;
7680 default @samp{texi2dvi}.
7681 @pindex texi2dvi
7683 @item WEAVE
7684 @vindex WEAVE
7685 Program to translate Web into @TeX{}; default @samp{weave}.
7686 @pindex weave
7688 @item CWEAVE
7689 @vindex CWEAVE
7690 Program to translate C Web into @TeX{}; default @samp{cweave}.
7691 @pindex cweave
7693 @item TANGLE
7694 @vindex TANGLE
7695 Program to translate Web into Pascal; default @samp{tangle}.
7696 @pindex tangle
7698 @item CTANGLE
7699 @vindex CTANGLE
7700 Program to translate C Web into C; default @samp{ctangle}.
7701 @pindex ctangle
7703 @item RM
7704 @vindex RM
7705 Command to remove a file; default @samp{rm -f}.
7706 @pindex rm
7707 @end table
7709 Here is a table of variables whose values are additional arguments for the
7710 programs above.  The default values for all of these is the empty
7711 string, unless otherwise noted.
7713 @table @code
7714 @item ARFLAGS
7715 @vindex ARFLAGS
7716 Flags to give the archive-maintaining program; default @samp{rv}.
7718 @item ASFLAGS
7719 @vindex ASFLAGS
7720 Extra flags to give to the assembler (when explicitly
7721 invoked on a @samp{.s} or @samp{.S} file).
7723 @item CFLAGS
7724 @vindex CFLAGS
7725 Extra flags to give to the C compiler.
7727 @item CXXFLAGS
7728 @vindex CXXFLAGS
7729 Extra flags to give to the C++ compiler.
7731 @item COFLAGS
7732 @vindex COFLAGS
7733 Extra flags to give to the RCS @code{co} program.
7735 @item CPPFLAGS
7736 @vindex CPPFLAGS
7737 Extra flags to give to the C preprocessor and programs
7738 that use it (the C and Fortran compilers).
7740 @item FFLAGS
7741 @vindex FFLAGS
7742 Extra flags to give to the Fortran compiler.
7744 @item GFLAGS
7745 @vindex GFLAGS
7746 Extra flags to give to the SCCS @code{get} program.
7748 @item LDFLAGS
7749 @vindex LDFLAGS
7750 Extra flags to give to compilers when they are
7751 supposed to invoke the linker, @samp{ld}.
7753 @item LFLAGS
7754 @vindex LFLAGS
7755 Extra flags to give to Lex.
7757 @item PFLAGS
7758 @vindex PFLAGS
7759 Extra flags to give to the Pascal compiler.
7761 @item RFLAGS
7762 @vindex RFLAGS
7763 Extra flags to give to the Fortran compiler for Ratfor programs.
7765 @item YFLAGS
7766 @vindex YFLAGS
7767 Extra flags to give to Yacc.
7768 @end table
7770 @node Chained Rules, Pattern Rules, Implicit Variables, Implicit Rules
7771 @section Chains of Implicit Rules
7773 @cindex chains of rules
7774 @cindex rule, implicit, chains of
7775 Sometimes a file can be made by a sequence of implicit rules.  For example,
7776 a file @file{@var{n}.o} could be made from @file{@var{n}.y} by running
7777 first Yacc and then @code{cc}.  Such a sequence is called a @dfn{chain}.
7779 If the file @file{@var{n}.c} exists, or is mentioned in the makefile, no
7780 special searching is required: @code{make} finds that the object file can
7781 be made by C compilation from @file{@var{n}.c}; later on, when considering
7782 how to make @file{@var{n}.c}, the rule for running Yacc is
7783 used.  Ultimately both @file{@var{n}.c} and @file{@var{n}.o} are
7784 updated.@refill
7786 @cindex intermediate files
7787 @cindex files, intermediate
7788 However, even if @file{@var{n}.c} does not exist and is not mentioned,
7789 @code{make} knows how to envision it as the missing link between
7790 @file{@var{n}.o} and @file{@var{n}.y}!  In this case, @file{@var{n}.c} is
7791 called an @dfn{intermediate file}.  Once @code{make} has decided to use the
7792 intermediate file, it is entered in the data base as if it had been
7793 mentioned in the makefile, along with the implicit rule that says how to
7794 create it.@refill
7796 Intermediate files are remade using their rules just like all other
7797 files.  But intermediate files are treated differently in two ways.
7799 The first difference is what happens if the intermediate file does not
7800 exist.  If an ordinary file @var{b} does not exist, and @code{make}
7801 considers a target that depends on @var{b}, it invariably creates
7802 @var{b} and then updates the target from @var{b}.  But if @var{b} is an
7803 intermediate file, then @code{make} can leave well enough alone.  It
7804 won't bother updating @var{b}, or the ultimate target, unless some
7805 prerequisite of @var{b} is newer than that target or there is some other
7806 reason to update that target.
7808 The second difference is that if @code{make} @emph{does} create @var{b}
7809 in order to update something else, it deletes @var{b} later on after it
7810 is no longer needed.  Therefore, an intermediate file which did not
7811 exist before @code{make} also does not exist after @code{make}.
7812 @code{make} reports the deletion to you by printing a @samp{rm -f}
7813 command showing which file it is deleting.
7815 Ordinarily, a file cannot be intermediate if it is mentioned in the
7816 makefile as a target or prerequisite.  However, you can explicitly mark a
7817 file as intermediate by listing it as a prerequisite of the special target
7818 @code{.INTERMEDIATE}.  This takes effect even if the file is mentioned
7819 explicitly in some other way.
7821 @cindex intermediate files, preserving
7822 @cindex preserving intermediate files
7823 @cindex secondary files
7824 You can prevent automatic deletion of an intermediate file by marking it
7825 as a @dfn{secondary} file.  To do this, list it as a prerequisite of the
7826 special target @code{.SECONDARY}.  When a file is secondary, @code{make}
7827 will not create the file merely because it does not already exist, but
7828 @code{make} does not automatically delete the file.  Marking a file as
7829 secondary also marks it as intermediate.
7831 You can list the target pattern of an implicit rule (such as @samp{%.o})
7832 as a prerequisite of the special target @code{.PRECIOUS} to preserve
7833 intermediate files made by implicit rules whose target patterns match
7834 that file's name; see @ref{Interrupts}.@refill
7835 @cindex preserving with @code{.PRECIOUS}
7836 @cindex @code{.PRECIOUS} intermediate files
7838 A chain can involve more than two implicit rules.  For example, it is
7839 possible to make a file @file{foo} from @file{RCS/foo.y,v} by running RCS,
7840 Yacc and @code{cc}.  Then both @file{foo.y} and @file{foo.c} are
7841 intermediate files that are deleted at the end.@refill
7843 No single implicit rule can appear more than once in a chain.  This means
7844 that @code{make} will not even consider such a ridiculous thing as making
7845 @file{foo} from @file{foo.o.o} by running the linker twice.  This
7846 constraint has the added benefit of preventing any infinite loop in the
7847 search for an implicit rule chain.
7849 There are some special implicit rules to optimize certain cases that would
7850 otherwise be handled by rule chains.  For example, making @file{foo} from
7851 @file{foo.c} could be handled by compiling and linking with separate
7852 chained rules, using @file{foo.o} as an intermediate file.  But what
7853 actually happens is that a special rule for this case does the compilation
7854 and linking with a single @code{cc} command.  The optimized rule is used in
7855 preference to the step-by-step chain because it comes earlier in the
7856 ordering of rules.
7858 @node Pattern Rules, Last Resort, Chained Rules, Implicit Rules
7859 @section Defining and Redefining Pattern Rules
7861 You define an implicit rule by writing a @dfn{pattern rule}.  A pattern
7862 rule looks like an ordinary rule, except that its target contains the
7863 character @samp{%} (exactly one of them).  The target is considered a
7864 pattern for matching file names; the @samp{%} can match any nonempty
7865 substring, while other characters match only themselves.  The prerequisites
7866 likewise use @samp{%} to show how their names relate to the target name.
7868 Thus, a pattern rule @samp{%.o : %.c} says how to make any file
7869 @file{@var{stem}.o} from another file @file{@var{stem}.c}.@refill
7871 Note that expansion using @samp{%} in pattern rules occurs
7872 @strong{after} any variable or function expansions, which take place
7873 when the makefile is read.  @xref{Using Variables, , How to Use
7874 Variables}, and @ref{Functions, ,Functions for Transforming Text}.
7876 @menu
7877 * Pattern Intro::               An introduction to pattern rules.
7878 * Pattern Examples::            Examples of pattern rules.
7879 * Automatic::                   How to use automatic variables in the
7880                                   commands of implicit rules.
7881 * Pattern Match::               How patterns match.
7882 * Match-Anything Rules::        Precautions you should take prior to
7883                                   defining rules that can match any
7884                                   target file whatever.
7885 * Canceling Rules::             How to override or cancel built-in rules.
7886 @end menu
7888 @node Pattern Intro, Pattern Examples,  , Pattern Rules
7889 @subsection Introduction to Pattern Rules
7890 @cindex pattern rule
7891 @cindex rule, pattern
7893 A pattern rule contains the character @samp{%} (exactly one of them)
7894 in the target; otherwise, it looks exactly like an ordinary rule.  The
7895 target is a pattern for matching file names; the @samp{%} matches any
7896 nonempty substring, while other characters match only themselves.
7897 @cindex target pattern, implicit
7898 @cindex @code{%}, in pattern rules
7900 For example, @samp{%.c} as a pattern matches any file name that ends in
7901 @samp{.c}.  @samp{s.%.c} as a pattern matches any file name that starts
7902 with @samp{s.}, ends in @samp{.c} and is at least five characters long.
7903 (There must be at least one character to match the @samp{%}.)  The substring
7904 that the @samp{%} matches is called the @dfn{stem}.@refill
7906 @samp{%} in a prerequisite of a pattern rule stands for the same stem
7907 that was matched by the @samp{%} in the target.  In order for
7908 the pattern rule to apply, its target pattern must match the file name
7909 under consideration, and its prerequisite patterns must name files that
7910 exist or can be made.  These files become prerequisites of the target.
7911 @cindex prerequisite pattern, implicit
7913 Thus, a rule of the form
7915 @example
7916 %.o : %.c ; @var{command}@dots{}
7917 @end example
7919 @noindent
7920 specifies how to make a file @file{@var{n}.o}, with another file
7921 @file{@var{n}.c} as its prerequisite, provided that @file{@var{n}.c}
7922 exists or can be made.
7924 There may also be prerequisites that do not use @samp{%}; such a prerequisite
7925 attaches to every file made by this pattern rule.  These unvarying
7926 prerequisites are useful occasionally.
7928 A pattern rule need not have any prerequisites that contain @samp{%}, or
7929 in fact any prerequisites at all.  Such a rule is effectively a general
7930 wildcard.  It provides a way to make any file that matches the target
7931 pattern.  @xref{Last Resort}.
7933 @c !!! The end of of this paragraph should be rewritten.  --bob
7934 Pattern rules may have more than one target.  Unlike normal rules, this
7935 does not act as many different rules with the same prerequisites and
7936 commands.  If a pattern rule has multiple targets, @code{make} knows that
7937 the rule's commands are responsible for making all of the targets.  The
7938 commands are executed only once to make all the targets.  When searching
7939 for a pattern rule to match a target, the target patterns of a rule other
7940 than the one that matches the target in need of a rule are incidental:
7941 @code{make} worries only about giving commands and prerequisites to the file
7942 presently in question.  However, when this file's commands are run, the
7943 other targets are marked as having been updated themselves.
7944 @cindex multiple targets, in pattern rule
7945 @cindex target, multiple in pattern rule
7947 The order in which pattern rules appear in the makefile is important
7948 since this is the order in which they are considered.
7949 Of equally applicable
7950 rules, only the first one found is used.  The rules you write take precedence
7951 over those that are built in.  Note however, that a rule whose
7952 prerequisites actually exist or are mentioned always takes priority over a
7953 rule with prerequisites that must be made by chaining other implicit rules.
7954 @cindex pattern rules, order of
7955 @cindex order of pattern rules
7957 @node Pattern Examples, Automatic, Pattern Intro, Pattern Rules
7958 @subsection Pattern Rule Examples
7960 Here are some examples of pattern rules actually predefined in
7961 @code{make}.  First, the rule that compiles @samp{.c} files into @samp{.o}
7962 files:@refill
7964 @example
7965 %.o : %.c
7966         $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@@
7967 @end example
7969 @noindent
7970 defines a rule that can make any file @file{@var{x}.o} from
7971 @file{@var{x}.c}.  The command uses the automatic variables @samp{$@@} and
7972 @samp{$<} to substitute the names of the target file and the source file
7973 in each case where the rule applies (@pxref{Automatic, ,Automatic Variables}).@refill
7975 Here is a second built-in rule:
7977 @example
7978 % :: RCS/%,v
7979         $(CO) $(COFLAGS) $<
7980 @end example
7982 @noindent
7983 defines a rule that can make any file @file{@var{x}} whatsoever from a
7984 corresponding file @file{@var{x},v} in the subdirectory @file{RCS}.  Since
7985 the target is @samp{%}, this rule will apply to any file whatever, provided
7986 the appropriate prerequisite file exists.  The double colon makes the rule
7987 @dfn{terminal}, which means that its prerequisite may not be an intermediate
7988 file (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}).@refill
7990 @need 500
7991 This pattern rule has two targets:
7993 @example
7994 @group
7995 %.tab.c %.tab.h: %.y
7996         bison -d $<
7997 @end group
7998 @end example
8000 @noindent
8001 @c The following paragraph is rewritten to avoid overfull hboxes
8002 This tells @code{make} that the command @samp{bison -d @var{x}.y} will
8003 make both @file{@var{x}.tab.c} and @file{@var{x}.tab.h}.  If the file
8004 @file{foo} depends on the files @file{parse.tab.o} and @file{scan.o}
8005 and the file @file{scan.o} depends on the file @file{parse.tab.h},
8006 when @file{parse.y} is changed, the command @samp{bison -d parse.y}
8007 will be executed only once, and the prerequisites of both
8008 @file{parse.tab.o} and @file{scan.o} will be satisfied.  (Presumably
8009 the file @file{parse.tab.o} will be recompiled from @file{parse.tab.c}
8010 and the file @file{scan.o} from @file{scan.c}, while @file{foo} is
8011 linked from @file{parse.tab.o}, @file{scan.o}, and its other
8012 prerequisites, and it will execute happily ever after.)@refill
8014 @node Automatic, Pattern Match, Pattern Examples, Pattern Rules
8015 @subsection Automatic Variables
8016 @cindex automatic variables
8017 @cindex variables, automatic
8018 @cindex variables, and implicit rule
8020 Suppose you are writing a pattern rule to compile a @samp{.c} file into a
8021 @samp{.o} file: how do you write the @samp{cc} command so that it operates
8022 on the right source file name?  You cannot write the name in the command,
8023 because the name is different each time the implicit rule is applied.
8025 What you do is use a special feature of @code{make}, the @dfn{automatic
8026 variables}.  These variables have values computed afresh for each rule that
8027 is executed, based on the target and prerequisites of the rule.  In this
8028 example, you would use @samp{$@@} for the object file name and @samp{$<}
8029 for the source file name.
8031 Here is a table of automatic variables:
8033 @table @code
8034 @vindex $@@
8035 @vindex @@ @r{(automatic variable)}
8036 @item $@@
8037 The file name of the target of the rule.  If the target is an archive
8038 member, then @samp{$@@} is the name of the archive file.  In a pattern
8039 rule that has multiple targets (@pxref{Pattern Intro, ,Introduction to
8040 Pattern Rules}), @samp{$@@} is the name of whichever target caused the
8041 rule's commands to be run.
8043 @vindex $%
8044 @vindex % @r{(automatic variable)}
8045 @item $%
8046 The target member name, when the target is an archive member.
8047 @xref{Archives}.  For example, if the target is @file{foo.a(bar.o)} then
8048 @samp{$%} is @file{bar.o} and @samp{$@@} is @file{foo.a}.  @samp{$%} is
8049 empty when the target is not an archive member.
8051 @vindex $<
8052 @vindex < @r{(automatic variable)}
8053 @item $<
8054 The name of the first prerequisite.  If the target got its commands from
8055 an implicit rule, this will be the first prerequisite added by the
8056 implicit rule (@pxref{Implicit Rules}).
8058 @vindex $?
8059 @vindex ? @r{(automatic variable)}
8060 @item $?
8061 The names of all the prerequisites that are newer than the target, with
8062 spaces between them.  For prerequisites which are archive members, only
8063 the member named is used (@pxref{Archives}).
8064 @cindex prerequisites, list of changed
8065 @cindex list of changed prerequisites
8067 @vindex $^
8068 @vindex ^ @r{(automatic variable)}
8069 @item $^
8070 The names of all the prerequisites, with spaces between them.  For
8071 prerequisites which are archive members, only the member named is used
8072 (@pxref{Archives}).  A target has only one prerequisite on each other file
8073 it depends on, no matter how many times each file is listed as a
8074 prerequisite.  So if you list a prerequisite more than once for a target,
8075 the value of @code{$^} contains just one copy of the name.
8076 @cindex prerequisites, list of all
8077 @cindex list of all prerequisites
8079 @vindex $+
8080 @vindex + @r{(automatic variable)}
8081 @item $+
8082 This is like @samp{$^}, but prerequisites listed more than once are
8083 duplicated in the order they were listed in the makefile.  This is
8084 primarily useful for use in linking commands where it is meaningful to
8085 repeat library file names in a particular order.
8087 @vindex $*
8088 @vindex * @r{(automatic variable)}
8089 @item $*
8090 The stem with which an implicit rule matches (@pxref{Pattern Match, ,How
8091 Patterns Match}).  If the target is @file{dir/a.foo.b} and the target
8092 pattern is @file{a.%.b} then the stem is @file{dir/foo}.  The stem is
8093 useful for constructing names of related files.@refill
8094 @cindex stem, variable for
8096 In a static pattern rule, the stem is part of the file name that matched
8097 the @samp{%} in the target pattern.
8099 In an explicit rule, there is no stem; so @samp{$*} cannot be determined
8100 in that way.  Instead, if the target name ends with a recognized suffix
8101 (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}), @samp{$*} is set to
8102 the target name minus the suffix.  For example, if the target name is
8103 @samp{foo.c}, then @samp{$*} is set to @samp{foo}, since @samp{.c} is a
8104 suffix.  GNU @code{make} does this bizarre thing only for compatibility
8105 with other implementations of @code{make}.  You should generally avoid
8106 using @samp{$*} except in implicit rules or static pattern rules.@refill
8108 If the target name in an explicit rule does not end with a recognized
8109 suffix, @samp{$*} is set to the empty string for that rule.
8110 @end table
8112 @samp{$?} is useful even in explicit rules when you wish to operate on only
8113 the prerequisites that have changed.  For example, suppose that an archive
8114 named @file{lib} is supposed to contain copies of several object files.
8115 This rule copies just the changed object files into the archive:
8117 @example
8118 @group
8119 lib: foo.o bar.o lose.o win.o
8120         ar r lib $?
8121 @end group
8122 @end example
8124 Of the variables listed above, four have values that are single file
8125 names, and three have values that are lists of file names.  These seven
8126 have variants that get just the file's directory name or just the file
8127 name within the directory.  The variant variables' names are formed by
8128 appending @samp{D} or @samp{F}, respectively.  These variants are
8129 semi-obsolete in GNU @code{make} since the functions @code{dir} and
8130 @code{notdir} can be used to get a similar effect (@pxref{File Name
8131 Functions, , Functions for File Names}).  Note, however, that the
8132 @samp{D} variants all omit the trailing slash which always appears in
8133 the output of the @code{dir} function.  Here is a table of the variants:
8135 @table @samp
8136 @vindex $(@@D)
8137 @vindex @@D @r{(automatic variable)}
8138 @item $(@@D)
8139 The directory part of the file name of the target, with the trailing
8140 slash removed.  If the value of @samp{$@@} is @file{dir/foo.o} then
8141 @samp{$(@@D)} is @file{dir}.  This value is @file{.} if @samp{$@@} does
8142 not contain a slash.
8144 @vindex $(@@F)
8145 @vindex @@F @r{(automatic variable)}
8146 @item $(@@F)
8147 The file-within-directory part of the file name of the target.  If the
8148 value of @samp{$@@} is @file{dir/foo.o} then @samp{$(@@F)} is
8149 @file{foo.o}.  @samp{$(@@F)} is equivalent to @samp{$(notdir $@@)}.
8151 @vindex $(*D)
8152 @vindex *D @r{(automatic variable)}
8153 @item $(*D)
8154 @vindex $(*F)
8155 @vindex *F @r{(automatic variable)}
8156 @itemx $(*F)
8157 The directory part and the file-within-directory
8158 part of the stem; @file{dir} and @file{foo} in this example.
8160 @vindex $(%D)
8161 @vindex %D @r{(automatic variable)}
8162 @item $(%D)
8163 @vindex $(%F)
8164 @vindex %F @r{(automatic variable)}
8165 @itemx $(%F)
8166 The directory part and the file-within-directory part of the target
8167 archive member name.  This makes sense only for archive member targets
8168 of the form @file{@var{archive}(@var{member})} and is useful only when
8169 @var{member} may contain a directory name.  (@xref{Archive Members,
8170 ,Archive Members as Targets}.)
8172 @vindex $(<D)
8173 @vindex <D @r{(automatic variable)}
8174 @item $(<D)
8175 @vindex $(<F)
8176 @vindex <F @r{(automatic variable)}
8177 @itemx $(<F)
8178 The directory part and the file-within-directory
8179 part of the first prerequisite.
8181 @vindex $(^D)
8182 @vindex ^D @r{(automatic variable)}
8183 @item $(^D)
8184 @vindex $(^F)
8185 @vindex ^F @r{(automatic variable)}
8186 @itemx $(^F)
8187 Lists of the directory parts and the file-within-directory
8188 parts of all prerequisites.
8190 @vindex $(?D)
8191 @vindex ?D @r{(automatic variable)}
8192 @item $(?D)
8193 @vindex $(?F)
8194 @vindex ?F @r{(automatic variable)}
8195 @itemx $(?F)
8196 Lists of the directory parts and the file-within-directory parts of
8197 all prerequisites that are newer than the target.
8198 @end table
8200 Note that we use a special stylistic convention when we talk about these
8201 automatic variables; we write ``the value of @samp{$<}'', rather than
8202 @w{``the variable @code{<}''} as we would write for ordinary variables
8203 such as @code{objects} and @code{CFLAGS}.  We think this convention
8204 looks more natural in this special case.  Please do not assume it has a
8205 deep significance; @samp{$<} refers to the variable named @code{<} just
8206 as @samp{$(CFLAGS)} refers to the variable named @code{CFLAGS}.
8207 You could just as well use @samp{$(<)} in place of @samp{$<}.
8209 @node Pattern Match, Match-Anything Rules, Automatic, Pattern Rules
8210 @subsection How Patterns Match
8212 @cindex stem
8213 A target pattern is composed of a @samp{%} between a prefix and a suffix,
8214 either or both of which may be empty.  The pattern matches a file name only
8215 if the file name starts with the prefix and ends with the suffix, without
8216 overlap.  The text between the prefix and the suffix is called the
8217 @dfn{stem}.  Thus, when the pattern @samp{%.o} matches the file name
8218 @file{test.o}, the stem is @samp{test}.  The pattern rule prerequisites are
8219 turned into actual file names by substituting the stem for the character
8220 @samp{%}.  Thus, if in the same example one of the prerequisites is written
8221 as @samp{%.c}, it expands to @samp{test.c}.@refill
8223 When the target pattern does not contain a slash (and it usually does
8224 not), directory names in the file names are removed from the file name
8225 before it is compared with the target prefix and suffix.  After the
8226 comparison of the file name to the target pattern, the directory
8227 names, along with the slash that ends them, are added on to the
8228 prerequisite file names generated from the pattern rule's prerequisite
8229 patterns and the file name. The directories are ignored only for the
8230 purpose of finding an implicit rule to use, not in the application of
8231 that rule.  Thus, @samp{e%t} matches the file name @file{src/eat},
8232 with @samp{src/a} as the stem.  When prerequisites are turned into file
8233 names, the directories from the stem are added at the front, while the
8234 rest of the stem is substituted for the @samp{%}.  The stem
8235 @samp{src/a} with a prerequisite pattern @samp{c%r} gives the file name
8236 @file{src/car}.@refill
8238 @node Match-Anything Rules, Canceling Rules, Pattern Match, Pattern Rules
8239 @subsection Match-Anything Pattern Rules
8241 @cindex match-anything rule
8242 @cindex terminal rule
8243 When a pattern rule's target is just @samp{%}, it matches any file name
8244 whatever.  We call these rules @dfn{match-anything} rules.  They are very
8245 useful, but it can take a lot of time for @code{make} to think about them,
8246 because it must consider every such rule for each file name listed either
8247 as a target or as a prerequisite.
8249 Suppose the makefile mentions @file{foo.c}.  For this target, @code{make}
8250 would have to consider making it by linking an object file @file{foo.c.o},
8251 or by C compilation-and-linking in one step from @file{foo.c.c}, or by
8252 Pascal compilation-and-linking from @file{foo.c.p}, and many other
8253 possibilities.
8255 We know these possibilities are ridiculous since @file{foo.c} is a C source
8256 file, not an executable.  If @code{make} did consider these possibilities,
8257 it would ultimately reject them, because files such as @file{foo.c.o} and
8258 @file{foo.c.p} would not exist.  But these possibilities are so
8259 numerous that @code{make} would run very slowly if it had to consider
8260 them.@refill
8262 To gain speed, we have put various constraints on the way @code{make}
8263 considers match-anything rules.  There are two different constraints that
8264 can be applied, and each time you define a match-anything rule you must
8265 choose one or the other for that rule.
8267 One choice is to mark the match-anything rule as @dfn{terminal} by defining
8268 it with a double colon.  When a rule is terminal, it does not apply unless
8269 its prerequisites actually exist.  Prerequisites that could be made with
8270 other implicit rules are not good enough.  In other words, no further
8271 chaining is allowed beyond a terminal rule.
8273 For example, the built-in implicit rules for extracting sources from RCS
8274 and SCCS files are terminal; as a result, if the file @file{foo.c,v} does
8275 not exist, @code{make} will not even consider trying to make it as an
8276 intermediate file from @file{foo.c,v.o} or from @file{RCS/SCCS/s.foo.c,v}.
8277 RCS and SCCS files are generally ultimate source files, which should not be
8278 remade from any other files; therefore, @code{make} can save time by not
8279 looking for ways to remake them.@refill
8281 If you do not mark the match-anything rule as terminal, then it is
8282 nonterminal.  A nonterminal match-anything rule cannot apply to a file name
8283 that indicates a specific type of data.  A file name indicates a specific
8284 type of data if some non-match-anything implicit rule target matches it.
8286 For example, the file name @file{foo.c} matches the target for the pattern
8287 rule @samp{%.c : %.y} (the rule to run Yacc).  Regardless of whether this
8288 rule is actually applicable (which happens only if there is a file
8289 @file{foo.y}), the fact that its target matches is enough to prevent
8290 consideration of any nonterminal match-anything rules for the file
8291 @file{foo.c}.  Thus, @code{make} will not even consider trying to make
8292 @file{foo.c} as an executable file from @file{foo.c.o}, @file{foo.c.c},
8293 @file{foo.c.p}, etc.@refill
8295 The motivation for this constraint is that nonterminal match-anything
8296 rules are used for making files containing specific types of data (such as
8297 executable files) and a file name with a recognized suffix indicates some
8298 other specific type of data (such as a C source file).
8300 Special built-in dummy pattern rules are provided solely to recognize
8301 certain file names so that nonterminal match-anything rules will not be
8302 considered.  These dummy rules have no prerequisites and no commands, and
8303 they are ignored for all other purposes.  For example, the built-in
8304 implicit rule
8306 @example
8307 %.p :
8308 @end example
8310 @noindent
8311 exists to make sure that Pascal source files such as @file{foo.p} match a
8312 specific target pattern and thereby prevent time from being wasted looking
8313 for @file{foo.p.o} or @file{foo.p.c}.
8315 Dummy pattern rules such as the one for @samp{%.p} are made for every
8316 suffix listed as valid for use in suffix rules (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}).
8318 @node Canceling Rules,  , Match-Anything Rules, Pattern Rules
8319 @subsection Canceling Implicit Rules
8321 You can override a built-in implicit rule (or one you have defined
8322 yourself) by defining a new pattern rule with the same target and
8323 prerequisites, but different commands.  When the new rule is defined, the
8324 built-in one is replaced.  The new rule's position in the sequence of
8325 implicit rules is determined by where you write the new rule.
8327 You can cancel a built-in implicit rule by defining a pattern rule with the
8328 same target and prerequisites, but no commands.  For example, the following
8329 would cancel the rule that runs the assembler:
8331 @example
8332 %.o : %.s
8333 @end example
8335 @node Last Resort, Suffix Rules, Pattern Rules, Implicit Rules
8336 @section Defining Last-Resort Default Rules
8337 @cindex last-resort default rules
8338 @cindex default rules, last-resort
8340 You can define a last-resort implicit rule by writing a terminal
8341 match-anything pattern rule with no prerequisites (@pxref{Match-Anything
8342 Rules}).  This is just like any other pattern rule; the only thing
8343 special about it is that it will match any target.  So such a rule's
8344 commands are used for all targets and prerequisites that have no commands
8345 of their own and for which no other implicit rule applies.
8347 For example, when testing a makefile, you might not care if the source
8348 files contain real data, only that they exist.  Then you might do this:
8350 @example
8352         touch $@@
8353 @end example
8355 @noindent
8356 to cause all the source files needed (as prerequisites) to be created
8357 automatically.
8359 @findex .DEFAULT
8360 You can instead define commands to be used for targets for which there
8361 are no rules at all, even ones which don't specify commands.  You do
8362 this by writing a rule for the target @code{.DEFAULT}.  Such a rule's
8363 commands are used for all prerequisites which do not appear as targets in
8364 any explicit rule, and for which no implicit rule applies.  Naturally,
8365 there is no @code{.DEFAULT} rule unless you write one.
8367 If you use @code{.DEFAULT} with no commands or prerequisites:
8369 @example
8370 .DEFAULT:
8371 @end example
8373 @noindent
8374 the commands previously stored for @code{.DEFAULT} are cleared.
8375 Then @code{make} acts as if you had never defined @code{.DEFAULT} at all.
8377 If you do not want a target to get the commands from a match-anything
8378 pattern rule or @code{.DEFAULT}, but you also do not want any commands
8379 to be run for the target, you can give it empty commands (@pxref{Empty
8380 Commands, ,Defining Empty Commands}).@refill
8382 You can use a last-resort rule to override part of another makefile.
8383 @xref{Overriding Makefiles, , Overriding Part of Another Makefile}.
8385 @node Suffix Rules, Implicit Rule Search, Last Resort, Implicit Rules
8386 @section Old-Fashioned Suffix Rules
8387 @cindex old-fashioned suffix rules
8388 @cindex suffix rule
8390 @dfn{Suffix rules} are the old-fashioned way of defining implicit rules for
8391 @code{make}.  Suffix rules are obsolete because pattern rules are more
8392 general and clearer.  They are supported in GNU @code{make} for
8393 compatibility with old makefiles.  They come in two kinds:
8394 @dfn{double-suffix} and @dfn{single-suffix}.@refill
8396 A double-suffix rule is defined by a pair of suffixes: the target suffix
8397 and the source suffix.  It matches any file whose name ends with the
8398 target suffix.  The corresponding implicit prerequisite is made by
8399 replacing the target suffix with the source suffix in the file name.  A
8400 two-suffix rule whose target and source suffixes are @samp{.o} and
8401 @samp{.c} is equivalent to the pattern rule @samp{%.o : %.c}.
8403 A single-suffix rule is defined by a single suffix, which is the source
8404 suffix.  It matches any file name, and the corresponding implicit
8405 prerequisite name is made by appending the source suffix.  A single-suffix
8406 rule whose source suffix is @samp{.c} is equivalent to the pattern rule
8407 @samp{% : %.c}.
8409 Suffix rule definitions are recognized by comparing each rule's target
8410 against a defined list of known suffixes.  When @code{make} sees a rule
8411 whose target is a known suffix, this rule is considered a single-suffix
8412 rule.  When @code{make} sees a rule whose target is two known suffixes
8413 concatenated, this rule is taken as a double-suffix rule.
8415 For example, @samp{.c} and @samp{.o} are both on the default list of
8416 known suffixes.  Therefore, if you define a rule whose target is
8417 @samp{.c.o}, @code{make} takes it to be a double-suffix rule with source
8418 suffix @samp{.c} and target suffix @samp{.o}.  Here is the old-fashioned
8419 way to define the rule for compiling a C source file:@refill
8421 @example
8422 .c.o:
8423         $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
8424 @end example
8426 Suffix rules cannot have any prerequisites of their own.  If they have any,
8427 they are treated as normal files with funny names, not as suffix rules.
8428 Thus, the rule:
8430 @example
8431 .c.o: foo.h
8432         $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
8433 @end example
8435 @noindent
8436 tells how to make the file @file{.c.o} from the prerequisite file
8437 @file{foo.h}, and is not at all like the pattern rule:
8439 @example
8440 %.o: %.c foo.h
8441         $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
8442 @end example
8444 @noindent
8445 which tells how to make @samp{.o} files from @samp{.c} files, and makes all
8446 @samp{.o} files using this pattern rule also depend on @file{foo.h}.
8448 Suffix rules with no commands are also meaningless.  They do not remove
8449 previous rules as do pattern rules with no commands (@pxref{Canceling
8450 Rules, , Canceling Implicit Rules}).  They simply enter the suffix or pair of suffixes concatenated as
8451 a target in the data base.@refill
8453 @findex .SUFFIXES
8454 The known suffixes are simply the names of the prerequisites of the special
8455 target @code{.SUFFIXES}.  You can add your own suffixes by writing a rule
8456 for @code{.SUFFIXES} that adds more prerequisites, as in:
8458 @example
8459 .SUFFIXES: .hack .win
8460 @end example
8462 @noindent
8463 which adds @samp{.hack} and @samp{.win} to the end of the list of suffixes.
8465 If you wish to eliminate the default known suffixes instead of just adding
8466 to them, write a rule for @code{.SUFFIXES} with no prerequisites.  By
8467 special dispensation, this eliminates all existing prerequisites of
8468 @code{.SUFFIXES}.  You can then write another rule to add the suffixes you
8469 want.  For example,
8471 @example
8472 @group
8473 .SUFFIXES:            # @r{Delete the default suffixes}
8474 .SUFFIXES: .c .o .h   # @r{Define our suffix list}
8475 @end group
8476 @end example
8478 The @samp{-r} or @samp{--no-builtin-rules} flag causes the default
8479 list of suffixes to be empty.
8481 @vindex SUFFIXES
8482 The variable @code{SUFFIXES} is defined to the default list of suffixes
8483 before @code{make} reads any makefiles.  You can change the list of suffixes
8484 with a rule for the special target @code{.SUFFIXES}, but that does not alter
8485 this variable.
8487 @node Implicit Rule Search,  , Suffix Rules, Implicit Rules
8488 @section Implicit Rule Search Algorithm
8489 @cindex implicit rule, search algorithm
8490 @cindex search algorithm, implicit rule
8492 Here is the procedure @code{make} uses for searching for an implicit rule
8493 for a target @var{t}.  This procedure is followed for each double-colon
8494 rule with no commands, for each target of ordinary rules none of which have
8495 commands, and for each prerequisite that is not the target of any rule.  It
8496 is also followed recursively for prerequisites that come from implicit
8497 rules, in the search for a chain of rules.
8499 Suffix rules are not mentioned in this algorithm because suffix rules are
8500 converted to equivalent pattern rules once the makefiles have been read in.
8502 For an archive member target of the form
8503 @samp{@var{archive}(@var{member})}, the following algorithm is run
8504 twice, first using the entire target name @var{t}, and second using
8505 @samp{(@var{member})} as the target @var{t} if the first run found no
8506 rule.@refill
8508 @enumerate
8509 @item
8510 Split @var{t} into a directory part, called @var{d}, and the rest,
8511 called @var{n}.  For example, if @var{t} is @samp{src/foo.o}, then
8512 @var{d} is @samp{src/} and @var{n} is @samp{foo.o}.@refill
8514 @item
8515 Make a list of all the pattern rules one of whose targets matches
8516 @var{t} or @var{n}.  If the target pattern contains a slash, it is
8517 matched against @var{t}; otherwise, against @var{n}.
8519 @item
8520 If any rule in that list is @emph{not} a match-anything rule, then
8521 remove all nonterminal match-anything rules from the list.
8523 @item
8524 Remove from the list all rules with no commands.
8526 @item
8527 For each pattern rule in the list:
8529 @enumerate a
8530 @item
8531 Find the stem @var{s}, which is the nonempty part of @var{t} or @var{n}
8532 matched by the @samp{%} in the target pattern.@refill
8534 @item
8535 Compute the prerequisite names by substituting @var{s} for @samp{%}; if
8536 the target pattern does not contain a slash, append @var{d} to
8537 the front of each prerequisite name.@refill
8539 @item
8540 Test whether all the prerequisites exist or ought to exist.  (If a
8541 file name is mentioned in the makefile as a target or as an explicit
8542 prerequisite, then we say it ought to exist.)
8544 If all prerequisites exist or ought to exist, or there are no prerequisites,
8545 then this rule applies.
8546 @end enumerate
8548 @item
8549 If no pattern rule has been found so far, try harder.
8550 For each pattern rule in the list:
8552 @enumerate a
8553 @item
8554 If the rule is terminal, ignore it and go on to the next rule.
8556 @item
8557 Compute the prerequisite names as before.
8559 @item
8560 Test whether all the prerequisites exist or ought to exist.
8562 @item
8563 For each prerequisite that does not exist, follow this algorithm
8564 recursively to see if the prerequisite can be made by an implicit
8565 rule.
8567 @item
8568 If all prerequisites exist, ought to exist, or can be
8569 made by implicit rules, then this rule applies.
8570 @end enumerate
8572 @item
8573 If no implicit rule applies, the rule for @code{.DEFAULT}, if any,
8574 applies.  In that case, give @var{t} the same commands that
8575 @code{.DEFAULT} has.  Otherwise, there are no commands for @var{t}.
8576 @end enumerate
8578 Once a rule that applies has been found, for each target pattern of the
8579 rule other than the one that matched @var{t} or @var{n}, the @samp{%} in
8580 the pattern is replaced with @var{s} and the resultant file name is stored
8581 until the commands to remake the target file @var{t} are executed.  After
8582 these commands are executed, each of these stored file names are entered
8583 into the data base and marked as having been updated and having the same
8584 update status as the file @var{t}.
8586 When the commands of a pattern rule are executed for @var{t}, the automatic
8587 variables are set corresponding to the target and prerequisites.
8588 @xref{Automatic, ,Automatic Variables}.
8590 @node Archives, Features, Implicit Rules, Top
8591 @chapter Using @code{make} to Update Archive Files
8592 @cindex archive
8594 @dfn{Archive files} are files containing named subfiles called
8595 @dfn{members}; they are maintained with the program @code{ar} and their
8596 main use is as subroutine libraries for linking.
8598 @menu
8599 * Archive Members::             Archive members as targets.
8600 * Archive Update::              The implicit rule for archive member targets.
8601 * Archive Pitfalls::            Dangers to watch out for when using archives.
8602 * Archive Suffix Rules::        You can write a special kind of suffix rule
8603                                   for updating archives.
8604 @end menu
8606 @node Archive Members, Archive Update,  , Archives
8607 @section Archive Members as Targets
8608 @cindex archive member targets
8610 An individual member of an archive file can be used as a target or
8611 prerequisite in @code{make}.  You specify the member named @var{member} in
8612 archive file @var{archive} as follows:
8614 @example
8615 @var{archive}(@var{member})
8616 @end example
8618 @noindent
8619 This construct is available only in targets and prerequisites, not in
8620 commands!  Most programs that you might use in commands do not support this
8621 syntax and cannot act directly on archive members.  Only @code{ar} and
8622 other programs specifically designed to operate on archives can do so.
8623 Therefore, valid commands to update an archive member target probably must
8624 use @code{ar}.  For example, this rule says to create a member
8625 @file{hack.o} in archive @file{foolib} by copying the file @file{hack.o}:
8627 @example
8628 foolib(hack.o) : hack.o
8629         ar cr foolib hack.o
8630 @end example
8632 In fact, nearly all archive member targets are updated in just this way
8633 and there is an implicit rule to do it for you.  @strong{Note:} The
8634 @samp{c} flag to @code{ar} is required if the archive file does not
8635 already exist.
8637 To specify several members in the same archive, you can write all the
8638 member names together between the parentheses.  For example:
8640 @example
8641 foolib(hack.o kludge.o)
8642 @end example
8644 @noindent
8645 is equivalent to:
8647 @example
8648 foolib(hack.o) foolib(kludge.o)
8649 @end example
8651 @cindex wildcard, in archive member
8652 You can also use shell-style wildcards in an archive member reference.
8653 @xref{Wildcards, ,Using Wildcard Characters in File Names}.  For
8654 example, @w{@samp{foolib(*.o)}} expands to all existing members of the
8655 @file{foolib} archive whose names end in @samp{.o}; perhaps
8656 @samp{@w{foolib(hack.o)} @w{foolib(kludge.o)}}.
8658 @node Archive Update
8659 @section Implicit Rule for Archive Member Targets
8661 Recall that a target that looks like @file{@var{a}(@var{m})} stands for the
8662 member named @var{m} in the archive file @var{a}.
8664 When @code{make} looks for an implicit rule for such a target, as a special
8665 feature it considers implicit rules that match @file{(@var{m})}, as well as
8666 those that match the actual target @file{@var{a}(@var{m})}.
8668 This causes one special rule whose target is @file{(%)} to match.  This
8669 rule updates the target @file{@var{a}(@var{m})} by copying the file @var{m}
8670 into the archive.  For example, it will update the archive member target
8671 @file{foo.a(bar.o)} by copying the @emph{file} @file{bar.o} into the
8672 archive @file{foo.a} as a @emph{member} named @file{bar.o}.
8674 When this rule is chained with others, the result is very powerful.
8675 Thus, @samp{make "foo.a(bar.o)"} (the quotes are needed to protect the
8676 @samp{(} and @samp{)} from being interpreted specially by the shell) in
8677 the presence of a file @file{bar.c} is enough to cause the following
8678 commands to be run, even without a makefile:
8680 @example
8681 cc -c bar.c -o bar.o
8682 ar r foo.a bar.o
8683 rm -f bar.o
8684 @end example
8686 @noindent
8687 Here @code{make} has envisioned the file @file{bar.o} as an intermediate
8688 file.  @xref{Chained Rules, ,Chains of Implicit Rules}.
8690 Implicit rules such as this one are written using the automatic variable
8691 @samp{$%}.  @xref{Automatic, ,Automatic Variables}.
8693 An archive member name in an archive cannot contain a directory name, but
8694 it may be useful in a makefile to pretend that it does.  If you write an
8695 archive member target @file{foo.a(dir/file.o)}, @code{make} will perform
8696 automatic updating with this command:
8698 @example
8699 ar r foo.a dir/file.o
8700 @end example
8702 @noindent
8703 which has the effect of copying the file @file{dir/file.o} into a member
8704 named @file{file.o}.  In connection with such usage, the automatic variables
8705 @code{%D} and @code{%F} may be useful.
8707 @menu
8708 * Archive Symbols::             How to update archive symbol directories.
8709 @end menu
8711 @node Archive Symbols,  ,  , Archive Update
8712 @subsection Updating Archive Symbol Directories
8713 @cindex @code{__.SYMDEF}
8714 @cindex updating archive symbol directories
8715 @cindex archive symbol directory updating
8716 @cindex symbol directories, updating archive
8717 @cindex directories, updating archive symbol
8719 An archive file that is used as a library usually contains a special member
8720 named @file{__.SYMDEF} that contains a directory of the external symbol
8721 names defined by all the other members.  After you update any other
8722 members, you need to update @file{__.SYMDEF} so that it will summarize the
8723 other members properly.  This is done by running the @code{ranlib} program:
8725 @example
8726 ranlib @var{archivefile}
8727 @end example
8729 Normally you would put this command in the rule for the archive file,
8730 and make all the members of the archive file prerequisites of that rule.
8731 For example,
8733 @example
8734 libfoo.a: libfoo.a(x.o) libfoo.a(y.o) @dots{}
8735         ranlib libfoo.a
8736 @end example
8738 @noindent
8739 The effect of this is to update archive members @file{x.o}, @file{y.o},
8740 etc., and then update the symbol directory member @file{__.SYMDEF} by
8741 running @code{ranlib}.  The rules for updating the members are not shown
8742 here; most likely you can omit them and use the implicit rule which copies
8743 files into the archive, as described in the preceding section.
8745 This is not necessary when using the GNU @code{ar} program, which
8746 updates the @file{__.SYMDEF} member automatically.
8748 @node Archive Pitfalls
8749 @section Dangers When Using Archives
8750 @cindex archive, and parallel execution
8751 @cindex parallel execution, and archive update
8752 @cindex archive, and @code{-j}
8753 @cindex @code{-j}, and archive update
8755 It is important to be careful when using parallel execution (the
8756 @code{-j} switch; @pxref{Parallel, ,Parallel Execution}) and archives.
8757 If multiple @code{ar} commands run at the same time on the same archive
8758 file, they will not know about each other and can corrupt the file.
8760 Possibly a future version of @code{make} will provide a mechanism to
8761 circumvent this problem by serializing all commands that operate on the
8762 same archive file.  But for the time being, you must either write your
8763 makefiles to avoid this problem in some other way, or not use @code{-j}.
8765 @node Archive Suffix Rules, , Archive Pitfalls, Archives
8766 @section Suffix Rules for Archive Files
8767 @cindex suffix rule, for archive
8768 @cindex archive, suffix rule for
8769 @cindex library archive, suffix rule for
8770 @cindex @code{.a} (archives)
8772 You can write a special kind of suffix rule for dealing with archive
8773 files.  @xref{Suffix Rules}, for a full explanation of suffix rules.
8774 Archive suffix rules are obsolete in GNU @code{make}, because pattern
8775 rules for archives are a more general mechanism (@pxref{Archive
8776 Update}).  But they are retained for compatibility with other
8777 @code{make}s.
8779 To write a suffix rule for archives, you simply write a suffix rule
8780 using the target suffix @samp{.a} (the usual suffix for archive files).
8781 For example, here is the old-fashioned suffix rule to update a library
8782 archive from C source files:
8784 @example
8785 @group
8786 .c.a:
8787         $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o
8788         $(AR) r $@@ $*.o
8789         $(RM) $*.o
8790 @end group
8791 @end example
8793 @noindent
8794 This works just as if you had written the pattern rule:
8796 @example
8797 @group
8798 (%.o): %.c
8799         $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o
8800         $(AR) r $@@ $*.o
8801         $(RM) $*.o
8802 @end group
8803 @end example
8805 In fact, this is just what @code{make} does when it sees a suffix rule
8806 with @samp{.a} as the target suffix.  Any double-suffix rule
8807 @w{@samp{.@var{x}.a}} is converted to a pattern rule with the target
8808 pattern @samp{(%.o)} and a prerequisite pattern of @samp{%.@var{x}}.
8810 Since you might want to use @samp{.a} as the suffix for some other kind
8811 of file, @code{make} also converts archive suffix rules to pattern rules
8812 in the normal way (@pxref{Suffix Rules}).  Thus a double-suffix rule
8813 @w{@samp{.@var{x}.a}} produces two pattern rules: @samp{@w{(%.o):}
8814 @w{%.@var{x}}} and @samp{@w{%.a}: @w{%.@var{x}}}.@refill
8816 @node Features, Missing, Archives, Top
8817 @chapter Features of GNU @code{make}
8818 @cindex features of GNU @code{make}
8819 @cindex portability
8820 @cindex compatibility
8822 Here is a summary of the features of GNU @code{make}, for comparison
8823 with and credit to other versions of @code{make}.  We consider the
8824 features of @code{make} in 4.2 BSD systems as a baseline.  If you are
8825 concerned with writing portable makefiles, you should not use the
8826 features of @code{make} listed here, nor the ones in @ref{Missing}.
8828 Many features come from the version of @code{make} in System V.
8830 @itemize @bullet
8831 @item
8832 The @code{VPATH} variable and its special meaning.
8833 @xref{Directory Search, , Searching Directories for Prerequisites}.
8834 This feature exists in System V @code{make}, but is undocumented.
8835 It is documented in 4.3 BSD @code{make} (which says it mimics System V's
8836 @code{VPATH} feature).@refill
8838 @item
8839 Included makefiles.  @xref{Include, ,Including Other Makefiles}.
8840 Allowing multiple files to be included with a single directive is a GNU
8841 extension.
8843 @item
8844 Variables are read from and communicated via the environment.
8845 @xref{Environment, ,Variables from the Environment}.
8847 @item
8848 Options passed through the variable @code{MAKEFLAGS} to recursive
8849 invocations of @code{make}.
8850 @xref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
8852 @item
8853 The automatic variable @code{$%} is set to the member name
8854 in an archive reference.  @xref{Automatic, ,Automatic Variables}.
8856 @item
8857 The automatic variables @code{$@@}, @code{$*}, @code{$<}, @code{$%},
8858 and @code{$?} have corresponding forms like @code{$(@@F)} and
8859 @code{$(@@D)}.  We have generalized this to @code{$^} as an obvious
8860 extension.  @xref{Automatic, ,Automatic Variables}.@refill
8862 @item
8863 Substitution variable references.
8864 @xref{Reference, ,Basics of Variable References}.
8866 @item
8867 The command-line options @samp{-b} and @samp{-m}, accepted and
8868 ignored.  In System V @code{make}, these options actually do something.
8870 @item
8871 Execution of recursive commands to run @code{make} via the variable
8872 @code{MAKE} even if @samp{-n}, @samp{-q} or @samp{-t} is specified.
8873 @xref{Recursion, ,Recursive Use of @code{make}}.
8875 @item
8876 Support for suffix @samp{.a} in suffix rules.  @xref{Archive Suffix
8877 Rules}.  This feature is obsolete in GNU @code{make}, because the
8878 general feature of rule chaining (@pxref{Chained Rules, ,Chains of
8879 Implicit Rules}) allows one pattern rule for installing members in an
8880 archive (@pxref{Archive Update}) to be sufficient.
8882 @item
8883 The arrangement of lines and backslash-newline combinations in
8884 commands is retained when the commands are printed, so they appear as
8885 they do in the makefile, except for the stripping of initial
8886 whitespace.
8887 @end itemize
8889 The following features were inspired by various other versions of
8890 @code{make}.  In some cases it is unclear exactly which versions inspired
8891 which others.
8893 @itemize @bullet
8894 @item
8895 Pattern rules using @samp{%}.
8896 This has been implemented in several versions of @code{make}.
8897 We're not sure who invented it first, but it's been spread around a bit.
8898 @xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.@refill
8900 @item
8901 Rule chaining and implicit intermediate files.
8902 This was implemented by Stu Feldman in his version of @code{make}
8903 for AT&T Eighth Edition Research Unix, and later by Andrew Hume of
8904 AT&T Bell Labs in his @code{mk} program (where he terms it
8905 ``transitive closure'').  We do not really know if
8906 we got this from either of them or thought it up ourselves at the
8907 same time.  @xref{Chained Rules, ,Chains of Implicit Rules}.
8909 @item
8910 The automatic variable @code{$^} containing a list of all prerequisites
8911 of the current target.  We did not invent this, but we have no idea who
8912 did.  @xref{Automatic, ,Automatic Variables}.  The automatic variable
8913 @code{$+} is a simple extension of @code{$^}.
8915 @item
8916 The ``what if'' flag (@samp{-W} in GNU @code{make}) was (as far as we know)
8917 invented by Andrew Hume in @code{mk}.
8918 @xref{Instead of Execution, ,Instead of Executing the Commands}.
8920 @item
8921 The concept of doing several things at once (parallelism) exists in
8922 many incarnations of @code{make} and similar programs, though not in the
8923 System V or BSD implementations.  @xref{Execution, ,Command Execution}.
8925 @item
8926 Modified variable references using pattern substitution come from
8927 SunOS 4.  @xref{Reference, ,Basics of Variable References}.
8928 This functionality was provided in GNU @code{make} by the
8929 @code{patsubst} function before the alternate syntax was implemented
8930 for compatibility with SunOS 4.  It is not altogether clear who
8931 inspired whom, since GNU @code{make} had @code{patsubst} before SunOS
8932 4 was released.@refill
8934 @item
8935 The special significance of @samp{+} characters preceding command lines
8936 (@pxref{Instead of Execution, ,Instead of Executing the Commands}) is
8937 mandated by
8938 @cite{IEEE Standard 1003.2-1992} (POSIX.2).
8940 @item
8941 The @samp{+=} syntax to append to the value of a variable comes from SunOS
8942 4 @code{make}.  @xref{Appending, , Appending More Text to Variables}.
8944 @item
8945 The syntax @w{@samp{@var{archive}(@var{mem1} @var{mem2}@dots{})}} to list
8946 multiple members in a single archive file comes from SunOS 4 @code{make}.
8947 @xref{Archive Members}.
8949 @item
8950 The @code{-include} directive to include makefiles with no error for a
8951 nonexistent file comes from SunOS 4 @code{make}.  (But note that SunOS 4
8952 @code{make} does not allow multiple makefiles to be specified in one
8953 @code{-include} directive.)  The same feature appears with the name
8954 @code{sinclude} in SGI @code{make} and perhaps others.
8955 @end itemize
8957 The remaining features are inventions new in GNU @code{make}:
8959 @itemize @bullet
8960 @item
8961 Use the @samp{-v} or @samp{--version} option to print version and
8962 copyright information.
8964 @item
8965 Use the @samp{-h} or @samp{--help} option to summarize the options to
8966 @code{make}.
8968 @item
8969 Simply-expanded variables.  @xref{Flavors, ,The Two Flavors of Variables}.
8971 @item
8972 Pass command-line variable assignments automatically through the
8973 variable @code{MAKE} to recursive @code{make} invocations.
8974 @xref{Recursion, ,Recursive Use of @code{make}}.
8976 @item
8977 Use the @samp{-C} or @samp{--directory} command option to change
8978 directory.  @xref{Options Summary, ,Summary of Options}.
8980 @item
8981 Make verbatim variable definitions with @code{define}.
8982 @xref{Defining, ,Defining Variables Verbatim}.
8984 @item
8985 Declare phony targets with the special target @code{.PHONY}.
8987 Andrew Hume of AT&T Bell Labs implemented a similar feature with a
8988 different syntax in his @code{mk} program.  This seems to be a case of
8989 parallel discovery.  @xref{Phony Targets, ,Phony Targets}.
8991 @item
8992 Manipulate text by calling functions.
8993 @xref{Functions, ,Functions for Transforming Text}.
8995 @item
8996 Use the @samp{-o} or @samp{--old-file}
8997 option to pretend a file's modification-time is old.
8998 @xref{Avoiding Compilation, ,Avoiding Recompilation of Some Files}.
9000 @item
9001 Conditional execution.
9003 This feature has been implemented numerous times in various versions
9004 of @code{make}; it seems a natural extension derived from the features
9005 of the C preprocessor and similar macro languages and is not a
9006 revolutionary concept.  @xref{Conditionals, ,Conditional Parts of Makefiles}.
9008 @item
9009 Specify a search path for included makefiles.
9010 @xref{Include, ,Including Other Makefiles}.
9012 @item
9013 Specify extra makefiles to read with an environment variable.
9014 @xref{MAKEFILES Variable, ,The Variable @code{MAKEFILES}}.
9016 @item
9017 Strip leading sequences of @samp{./} from file names, so that
9018 @file{./@var{file}} and @file{@var{file}} are considered to be the
9019 same file.@refill
9021 @item
9022 Use a special search method for library prerequisites written in the
9023 form @samp{-l@var{name}}.
9024 @xref{Libraries/Search, ,Directory Search for Link Libraries}.
9026 @item
9027 Allow suffixes for suffix rules
9028 (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}) to contain any
9029 characters.  In other versions of @code{make}, they must begin with
9030 @samp{.} and not contain any @samp{/} characters.
9032 @item
9033 Keep track of the current level of @code{make} recursion using the
9034 variable @code{MAKELEVEL}.  @xref{Recursion, ,Recursive Use of @code{make}}.
9036 @item
9037 Provide any goals given on the command line in the variable
9038 @code{MAKECMDGOALS}.  @xref{Goals, ,Arguments to Specify the Goals}.
9040 @item
9041 Specify static pattern rules.  @xref{Static Pattern, ,Static Pattern Rules}.
9043 @item
9044 Provide selective @code{vpath} search.
9045 @xref{Directory Search, ,Searching Directories for Prerequisites}.
9047 @item
9048 Provide computed variable references.
9049 @xref{Reference, ,Basics of Variable References}.
9051 @item
9052 Update makefiles.  @xref{Remaking Makefiles, ,How Makefiles Are Remade}.
9053 System V @code{make} has a very, very limited form of this
9054 functionality in that it will check out SCCS files for makefiles.
9056 @item
9057 Various new built-in implicit rules.
9058 @xref{Catalogue of Rules, ,Catalogue of Implicit Rules}.
9060 @item
9061 The built-in variable @samp{MAKE_VERSION} gives the version number of
9062 @code{make}.
9063 @end itemize
9065 @node Missing, Makefile Conventions, Features, Top
9066 @chapter Incompatibilities and Missing Features
9067 @cindex incompatibilities
9068 @cindex missing features
9069 @cindex features, missing
9071 The @code{make} programs in various other systems support a few features
9072 that are not implemented in GNU @code{make}.  The POSIX.2 standard
9073 (@cite{IEEE Standard 1003.2-1992}) which specifies @code{make} does not
9074 require any of these features.@refill
9076 @itemize @bullet
9077 @item
9078 A target of the form @samp{@var{file}((@var{entry}))} stands for a member
9079 of archive file @var{file}.  The member is chosen, not by name, but by
9080 being an object file which defines the linker symbol @var{entry}.@refill
9082 This feature was not put into GNU @code{make} because of the
9083 nonmodularity of putting knowledge into @code{make} of the internal
9084 format of archive file symbol tables.
9085 @xref{Archive Symbols, ,Updating Archive Symbol Directories}.
9087 @item
9088 Suffixes (used in suffix rules) that end with the character @samp{~}
9089 have a special meaning to System V @code{make};
9090 they refer to the SCCS file that corresponds
9091 to the file one would get without the @samp{~}.  For example, the
9092 suffix rule @samp{.c~.o} would make the file @file{@var{n}.o} from
9093 the SCCS file @file{s.@var{n}.c}.  For complete coverage, a whole
9094 series of such suffix rules is required.
9095 @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
9097 In GNU @code{make}, this entire series of cases is handled by two
9098 pattern rules for extraction from SCCS, in combination with the
9099 general feature of rule chaining.
9100 @xref{Chained Rules, ,Chains of Implicit Rules}.
9102 @item
9103 In System V @code{make}, the string @samp{$$@@} has the strange meaning
9104 that, in the prerequisites of a rule with multiple targets, it stands
9105 for the particular target that is being processed.
9107 This is not defined in GNU @code{make} because @samp{$$} should always
9108 stand for an ordinary @samp{$}.
9110 It is possible to get portions of this functionality through the use of
9111 static pattern rules (@pxref{Static Pattern, ,Static Pattern Rules}).
9112 The System V @code{make} rule:
9114 @example
9115 $(targets): $$@@.o lib.a
9116 @end example
9118 @noindent
9119 can be replaced with the GNU @code{make} static pattern rule:
9121 @example
9122 $(targets): %: %.o lib.a
9123 @end example
9125 @item
9126 In System V and 4.3 BSD @code{make}, files found by @code{VPATH} search
9127 (@pxref{Directory Search, ,Searching Directories for Prerequisites}) have their names changed inside command
9128 strings.  We feel it is much cleaner to always use automatic variables
9129 and thus make this feature obsolete.@refill
9131 @item
9132 In some Unix @code{make}s, the automatic variable @code{$*} appearing in
9133 the prerequisites of a rule has the amazingly strange ``feature'' of
9134 expanding to the full name of the @emph{target of that rule}.  We cannot
9135 imagine what went on in the minds of Unix @code{make} developers to do
9136 this; it is utterly inconsistent with the normal definition of @code{$*}.
9137 @vindex * @r{(automatic variable), unsupported bizarre usage}
9139 @item
9140 In some Unix @code{make}s, implicit rule search
9141 (@pxref{Implicit Rules, ,Using Implicit Rules}) is apparently done for
9142 @emph{all} targets, not just those without commands.  This means you can
9143 do:@refill
9145 @example
9146 @group
9147 foo.o:
9148         cc -c foo.c
9149 @end group
9150 @end example
9152 @noindent
9153 and Unix @code{make} will intuit that @file{foo.o} depends on
9154 @file{foo.c}.@refill
9156 We feel that such usage is broken.  The prerequisite properties of
9157 @code{make} are well-defined (for GNU @code{make}, at least),
9158 and doing such a thing simply does not fit the model.@refill
9160 @item
9161 GNU @code{make} does not include any built-in implicit rules for
9162 compiling or preprocessing EFL programs.  If we hear of anyone who is
9163 using EFL, we will gladly add them.
9165 @item
9166 It appears that in SVR4 @code{make}, a suffix rule can be specified with
9167 no commands, and it is treated as if it had empty commands
9168 (@pxref{Empty Commands}).  For example:
9170 @example
9171 .c.a:
9172 @end example
9174 @noindent
9175 will override the built-in @file{.c.a} suffix rule.
9177 We feel that it is cleaner for a rule without commands to always simply
9178 add to the prerequisite list for the target.  The above example can be
9179 easily rewritten to get the desired behavior in GNU @code{make}:
9181 @example
9182 .c.a: ;
9183 @end example
9185 @item
9186 Some versions of @code{make} invoke the shell with the @samp{-e} flag,
9187 except under @samp{-k} (@pxref{Testing, ,Testing the Compilation of a
9188 Program}).  The @samp{-e} flag tells the shell to exit as soon as any
9189 program it runs returns a nonzero status.  We feel it is cleaner to
9190 write each shell command line to stand on its own and not require this
9191 special treatment.
9192 @end itemize
9194 @comment The makefile standards are in a separate file that is also
9195 @comment included by standards.texi.
9196 @include make-stds.texi
9198 @node Quick Reference, Error Messages, Makefile Conventions, Top
9199 @appendix Quick Reference
9201 This appendix summarizes the directives, text manipulation functions,
9202 and special variables which GNU @code{make} understands.
9203 @xref{Special Targets}, @ref{Catalogue of Rules, ,Catalogue of Implicit Rules},
9204 and @ref{Options Summary, ,Summary of Options},
9205 for other summaries.
9207 Here is a summary of the directives GNU @code{make} recognizes:
9209 @table @code
9210 @item define @var{variable}
9211 @itemx endef
9213 Define a multi-line, recursively-expanded variable.@*
9214 @xref{Sequences}.
9216 @item ifdef @var{variable}
9217 @itemx ifndef @var{variable}
9218 @itemx ifeq (@var{a},@var{b})
9219 @itemx ifeq "@var{a}" "@var{b}"
9220 @itemx ifeq '@var{a}' '@var{b}'
9221 @itemx ifneq (@var{a},@var{b})
9222 @itemx ifneq "@var{a}" "@var{b}"
9223 @itemx ifneq '@var{a}' '@var{b}'
9224 @itemx else
9225 @itemx endif
9227 Conditionally evaluate part of the makefile.@*
9228 @xref{Conditionals}.
9230 @item include @var{file}
9231 @itemx -include @var{file}
9232 @itemx sinclude @var{file}
9234 Include another makefile.@*
9235 @xref{Include, ,Including Other Makefiles}.
9237 @item override @var{variable} = @var{value}
9238 @itemx override @var{variable} := @var{value}
9239 @itemx override @var{variable} += @var{value}
9240 @itemx override @var{variable} ?= @var{value}
9241 @itemx override define @var{variable}
9242 @itemx endef
9244 Define a variable, overriding any previous definition, even one from
9245 the command line.@*
9246 @xref{Override Directive, ,The @code{override} Directive}.
9248 @item export
9250 Tell @code{make} to export all variables to child processes by default.@*
9251 @xref{Variables/Recursion, , Communicating Variables to a Sub-@code{make}}.
9253 @item export @var{variable}
9254 @itemx export @var{variable} = @var{value}
9255 @itemx export @var{variable} := @var{value}
9256 @itemx export @var{variable} += @var{value}
9257 @itemx export @var{variable} ?= @var{value}
9258 @itemx unexport @var{variable}
9259 Tell @code{make} whether or not to export a particular variable to child
9260 processes.@*
9261 @xref{Variables/Recursion, , Communicating Variables to a Sub-@code{make}}.
9263 @item vpath @var{pattern} @var{path}
9264 Specify a search path for files matching a @samp{%} pattern.@*
9265 @xref{Selective Search, , The @code{vpath} Directive}.
9267 @item vpath @var{pattern}
9268 Remove all search paths previously specified for @var{pattern}.
9270 @item vpath
9271 Remove all search paths previously specified in any @code{vpath}
9272 directive.
9273 @end table
9275 Here is a summary of the text manipulation functions (@pxref{Functions}):
9277 @table @code
9278 @item $(subst @var{from},@var{to},@var{text})
9279 Replace @var{from} with @var{to} in @var{text}.@*
9280 @xref{Text Functions, , Functions for String Substitution and Analysis}.
9282 @item $(patsubst @var{pattern},@var{replacement},@var{text})
9283 Replace words matching @var{pattern} with @var{replacement} in @var{text}.@*
9284 @xref{Text Functions, , Functions for String Substitution and Analysis}.
9286 @item $(strip @var{string})
9287 Remove excess whitespace characters from @var{string}.@*
9288 @xref{Text Functions, , Functions for String Substitution and Analysis}.
9290 @item $(findstring @var{find},@var{text})
9291 Locate @var{find} in @var{text}.@*
9292 @xref{Text Functions, , Functions for String Substitution and Analysis}.
9294 @item $(filter @var{pattern}@dots{},@var{text})
9295 Select words in @var{text} that match one of the @var{pattern} words.@*
9296 @xref{Text Functions, , Functions for String Substitution and Analysis}.
9298 @item $(filter-out @var{pattern}@dots{},@var{text})
9299 Select words in @var{text} that @emph{do not} match any of the @var{pattern} words.@*
9300 @xref{Text Functions, , Functions for String Substitution and Analysis}.
9302 @item $(sort @var{list})
9303 Sort the words in @var{list} lexicographically, removing duplicates.@*
9304 @xref{Text Functions, , Functions for String Substitution and Analysis}.
9306 @item $(dir @var{names}@dots{})
9307 Extract the directory part of each file name.@*
9308 @xref{File Name Functions, ,Functions for File Names}.
9310 @item $(notdir @var{names}@dots{})
9311 Extract the non-directory part of each file name.@*
9312 @xref{File Name Functions, ,Functions for File Names}.
9314 @item $(suffix @var{names}@dots{})
9315 Extract the suffix (the last @samp{.} and following characters) of each file name.@*
9316 @xref{File Name Functions, ,Functions for File Names}.
9318 @item $(basename @var{names}@dots{})
9319 Extract the base name (name without suffix) of each file name.@*
9320 @xref{File Name Functions, ,Functions for File Names}.
9322 @item $(addsuffix @var{suffix},@var{names}@dots{})
9323 Append @var{suffix} to each word in @var{names}.@*
9324 @xref{File Name Functions, ,Functions for File Names}.
9326 @item $(addprefix @var{prefix},@var{names}@dots{})
9327 Prepend @var{prefix} to each word in @var{names}.@*
9328 @xref{File Name Functions, ,Functions for File Names}.
9330 @item $(join @var{list1},@var{list2})
9331 Join two parallel lists of words.@*
9332 @xref{File Name Functions, ,Functions for File Names}.
9334 @item $(word @var{n},@var{text})
9335 Extract the @var{n}th word (one-origin) of @var{text}.@*
9336 @xref{File Name Functions, ,Functions for File Names}.
9338 @item $(words @var{text})
9339 Count the number of words in @var{text}.@*
9340 @xref{File Name Functions, ,Functions for File Names}.
9342 @item $(wordlist @var{s},@var{e},@var{text})
9343 Returns the list of words in @var{text} from @var{s} to @var{e}.@*
9344 @xref{File Name Functions, ,Functions for File Names}.
9346 @item $(firstword @var{names}@dots{})
9347 Extract the first word of @var{names}.@*
9348 @xref{File Name Functions, ,Functions for File Names}.
9350 @item $(wildcard @var{pattern}@dots{})
9351 Find file names matching a shell file name pattern (@emph{not} a
9352 @samp{%} pattern).@*
9353 @xref{Wildcard Function, ,The Function @code{wildcard}}.
9355 @item $(error @var{text}@dots{})
9357 When this function is evaluated, @code{make} generates a fatal error
9358 with the message @var{text}.@*
9359 @xref{Make Control Functions, ,Functions That Control Make}.
9361 @item $(warning @var{text}@dots{})
9363 When this function is evaluated, @code{make} generates a warning with
9364 the message @var{text}.@*
9365 @xref{Make Control Functions, ,Functions That Control Make}.
9367 @item $(shell @var{command})
9369 Execute a shell command and return its output.@*
9370 @xref{Shell Function, , The @code{shell} Function}.
9372 @item $(origin @var{variable})
9374 Return a string describing how the @code{make} variable @var{variable} was
9375 defined.@*
9376 @xref{Origin Function, , The @code{origin} Function}.
9378 @item $(foreach @var{var},@var{words},@var{text})
9380 Evaluate @var{text} with @var{var} bound to each word in @var{words},
9381 and concatenate the results.@*
9382 @xref{Foreach Function, ,The @code{foreach} Function}.
9384 @item $(call @var{var},@var{param},@dots{})
9386 Evaluate the variable @var{var} replacing any references to @code{$(1)},
9387 @code{$(2)} with the first, second, etc. @var{param} values.@*
9388 @xref{Call Function, ,The @code{call} Function}.
9389 @end table
9391 Here is a summary of the automatic variables.
9392 @xref{Automatic, ,Automatic Variables},
9393 for full information.
9395 @table @code
9396 @item $@@
9397 The file name of the target.
9399 @item $%
9400 The target member name, when the target is an archive member.
9402 @item $<
9403 The name of the first prerequisite.
9405 @item $?
9406 The names of all the prerequisites that are
9407 newer than the target, with spaces between them.
9408 For prerequisites which are archive members, only
9409 the member named is used (@pxref{Archives}).
9411 @item $^
9412 @itemx $+
9413 The names of all the prerequisites, with spaces between them.  For
9414 prerequisites which are archive members, only the member named is used
9415 (@pxref{Archives}).  The value of @code{$^} omits duplicate
9416 prerequisites, while @code{$+} retains them and preserves their order.
9418 @item $*
9419 The stem with which an implicit rule matches
9420 (@pxref{Pattern Match, ,How Patterns Match}).
9422 @item $(@@D)
9423 @itemx $(@@F)
9424 The directory part and the file-within-directory part of @code{$@@}.
9426 @item $(*D)
9427 @itemx $(*F)
9428 The directory part and the file-within-directory part of @code{$*}.
9430 @item $(%D)
9431 @itemx $(%F)
9432 The directory part and the file-within-directory part of @code{$%}.
9434 @item $(<D)
9435 @itemx $(<F)
9436 The directory part and the file-within-directory part of @code{$<}.
9438 @item $(^D)
9439 @itemx $(^F)
9440 The directory part and the file-within-directory part of @code{$^}.
9442 @item $(+D)
9443 @itemx $(+F)
9444 The directory part and the file-within-directory part of @code{$+}.
9446 @item $(?D)
9447 @itemx $(?F)
9448 The directory part and the file-within-directory part of @code{$?}.
9449 @end table
9451 These variables are used specially by GNU @code{make}:
9453 @table @code
9454 @item MAKEFILES
9456 Makefiles to be read on every invocation of @code{make}.@*
9457 @xref{MAKEFILES Variable, ,The Variable @code{MAKEFILES}}.
9459 @item VPATH
9461 Directory search path for files not found in the current directory.@*
9462 @xref{General Search, , @code{VPATH} Search Path for All Prerequisites}.
9464 @item SHELL
9466 The name of the system default command interpreter, usually @file{/bin/sh}.
9467 You can set @code{SHELL} in the makefile to change the shell used to run
9468 commands.  @xref{Execution, ,Command Execution}.
9470 @item MAKESHELL
9472 On MS-DOS only, the name of the command interpreter that is to be used
9473 by @code{make}. This value takes precedence over the value of
9474 @code{SHELL}.  @xref{Execution, ,MAKESHELL variable}.
9476 @item MAKE
9478 The name with which @code{make} was invoked.
9479 Using this variable in commands has special meaning.
9480 @xref{MAKE Variable, ,How the @code{MAKE} Variable Works}.
9482 @item MAKELEVEL
9484 The number of levels of recursion (sub-@code{make}s).@*
9485 @xref{Variables/Recursion}.
9487 @item MAKEFLAGS
9489 The flags given to @code{make}.  You can set this in the environment or
9490 a makefile to set flags.@*
9491 @xref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
9493 It is @emph{never} appropriate to use @code{MAKEFLAGS} directly on a
9494 command line: its contents may not be quoted correctly for use in the
9495 shell.  Always allow recursive @code{make}'s to obtain these values
9496 through the environment from its parent.
9498 @item MAKECMDGOALS
9500 The targets given to @code{make} on the command line.  Setting this
9501 variable has no effect on the operation of @code{make}.@*
9502 @xref{Goals, ,Arguments to Specify the Goals}.
9504 @item CURDIR
9506 Set to the pathname of the current working directory (after all
9507 @code{-C} options are processed, if any).  Setting this variable has no
9508 effect on the operation of @code{make}.@*
9509 @xref{Recursion, ,Recursive Use of @code{make}}.
9511 @item SUFFIXES
9513 The default list of suffixes before @code{make} reads any makefiles.
9515 @item .LIBPATTERNS
9516 Defines the naming of the libraries @code{make} searches for, and their
9517 order.@*
9518 @xref{Libraries/Search, ,Directory Search for Link Libraries}.
9519 @end table
9521 @node Error Messages, Complex Makefile, Quick Reference, Top
9522 @comment  node-name,  next,  previous,  up
9523 @appendix Errors Generated by Make
9525 Here is a list of the more common errors you might see generated by
9526 @code{make}, and some information about what they mean and how to fix
9527 them.
9529 Sometimes @code{make} errors are not fatal, especially in the presence
9530 of a @code{-} prefix on a command script line, or the @code{-k} command
9531 line option.  Errors that are fatal are prefixed with the string
9532 @code{***}.
9534 Error messages are all either prefixed with the name of the program
9535 (usually @samp{make}), or, if the error is found in a makefile, the name
9536 of the file and linenumber containing the problem.
9538 In the table below, these common prefixes are left off.
9540 @table @samp
9542 @item [@var{foo}] Error @var{NN}
9543 @itemx [@var{foo}] @var{signal description}
9544 These errors are not really @code{make} errors at all.  They mean that a
9545 program that @code{make} invoked as part of a command script returned a
9546 non-0 error code (@samp{Error @var{NN}}), which @code{make} interprets
9547 as failure, or it exited in some other abnormal fashion (with a
9548 signal of some type).  @xref{Errors, ,Errors in Commands}.
9550 If no @code{***} is attached to the message, then the subprocess failed
9551 but the rule in the makefile was prefixed with the @code{-} special
9552 character, so @code{make} ignored the error.
9554 @item missing separator.  Stop.
9555 @itemx missing separator (did you mean TAB instead of 8 spaces?).  Stop.
9556 This means that @code{make} could not understand much of anything about
9557 the command line it just read.  GNU @code{make} looks for various kinds
9558 of separators (@code{:}, @code{=}, TAB characters, etc.) to help it
9559 decide what kind of commandline it's seeing.  This means it couldn't
9560 find a valid one.
9562 One of the most common reasons for this message is that you (or perhaps
9563 your oh-so-helpful editor, as is the case with many MS-Windows editors)
9564 have attempted to indent your command scripts with spaces instead of a
9565 TAB character.  In this case, @code{make} will use the second form of
9566 the error above.  Remember that every line in the command script must
9567 begin with a TAB character.  Eight spaces do not count.  @xref{Rule
9568 Syntax}.
9570 @item commands commence before first target.  Stop.
9571 @itemx missing rule before commands.  Stop.
9572 This means the first thing in the makefile seems to be part of a command
9573 script: it begins with a TAB character and doesn't appear to be a legal
9574 @code{make} command (such as a variable assignment).  Command scripts
9575 must always be associated with a target.
9577 The second form is generated if the line has a semicolon as the first
9578 non-whitespace character; @code{make} interprets this to mean you left
9579 out the "target: prerequisite" section of a rule.  @xref{Rule Syntax}.
9581 @item No rule to make target `@var{xxx}'.
9582 @itemx No rule to make target `@var{xxx}', needed by `@var{yyy}'.
9583 This means that @code{make} decided it needed to build a target, but
9584 then couldn't find any instructions in the makefile on how to do that,
9585 either explicit or implicit (including in the default rules database).
9587 If you want that file to be built, you will need to add a rule to your
9588 makefile describing how that target can be built.  Other possible
9589 sources of this problem are typos in the makefile (if that filename is
9590 wrong) or a corrupted source tree (if that file is not supposed to be
9591 built, but rather only a prerequisite).
9593 @item No targets specified and no makefile found.  Stop.
9594 @itemx No targets.  Stop.
9595 The former means that you didn't provide any targets to be built on the
9596 command line, and @code{make} couldn't find any makefiles to read in.
9597 The latter means that some makefile was found, but it didn't contain any
9598 default target and none was given on the command line.  GNU @code{make}
9599 has nothing to do in these situations.
9600 @xref{Makefile Arguments, ,Arguments to Specify the Makefile}.@refill
9602 @item Makefile `@var{xxx}' was not found.
9603 @itemx Included makefile `@var{xxx}' was not found.
9604 A makefile specified on the command line (first form) or included
9605 (second form) was not found.
9607 @item warning: overriding commands for target `@var{xxx}'
9608 @itemx warning: ignoring old commands for target `@var{xxx}'
9609 GNU @code{make} allows commands to be specified only once per target
9610 (except for double-colon rules).  If you give commands for a target
9611 which already has been defined to have commands, this warning is issued
9612 and the second set of commands will overwrite the first set.
9613 @xref{Multiple Rules, ,Multiple Rules for One Target}.
9615 @item Circular @var{xxx} <- @var{yyy} dependency dropped.
9616 This means that @code{make} detected a loop in the dependency graph:
9617 after tracing the prerequisite @var{yyy} of target @var{xxx}, and its
9618 prerequisites, etc., one of them depended on @var{xxx} again.
9620 @item Recursive variable `@var{xxx}' references itself (eventually).  Stop.
9621 This means you've defined a normal (recursive) @code{make} variable
9622 @var{xxx} that, when it's expanded, will refer to itself (@var{xxx}).
9623 This is not allowed; either use simply-expanded variables (@code{:=}) or
9624 use the append operator (@code{+=}).  @xref{Using Variables, ,How to Use
9625 Variables}.
9627 @item Unterminated variable reference.  Stop.
9628 This means you forgot to provide the proper closing parenthesis
9629 or brace in your variable or function reference.
9631 @item insufficient arguments to function `@var{xxx}'.  Stop.
9632 This means you haven't provided the requisite number of arguments for
9633 this function.  See the documentation of the function for a description
9634 of its arguments.  @xref{Functions, ,Functions for Transforming Text}.
9636 @item missing target pattern.  Stop.
9637 @itemx multiple target patterns.  Stop.
9638 @itemx target pattern contains no `%'.  Stop.
9639 These are generated for malformed static pattern rules.  The first means
9640 there's no pattern in the target section of the rule, the second means
9641 there are multiple patterns in the target section, and the third means
9642 the target doesn't contain a pattern character (@code{%}).  @xref{Static
9643 Usage, ,Syntax of Static Pattern Rules}.
9645 @item warning: -jN forced in submake: disabling jobserver mode.
9646 This warning and the next are generated if @code{make} detects error
9647 conditions related to parallel processing on systems where
9648 sub-@code{make}s can communicate (@pxref{Options/Recursion,
9649 ,Communicating Options to a Sub-@code{make}}).  This warning is
9650 generated if a recursive invocation of a @code{make} process is forced
9651 to have @samp{-j@var{N}} in its argument list (where @var{N} is greater
9652 than one).  This could happen, for example, if you set the @code{MAKE}
9653 environment variable to @samp{make -j2}.  In this case, the
9654 sub-@code{make} doesn't communicate with other @code{make} processes and
9655 will simply pretend it has two jobs of its own.
9657 @item warning: jobserver unavailable: using -j1.  Add `+' to parent make rule.
9658 In order for @code{make} processes to communicate, the parent will pass
9659 information to the child.  Since this could result in problems if the
9660 child process isn't actually a @code{make}, the parent will only do this
9661 if it thinks the child is a @code{make}.  The parent uses the normal
9662 algorithms to determine this (@pxref{MAKE Variable, ,How the @code{MAKE}
9663 Variable Works}).  If the makefile is constructed such that the parent
9664 doesn't know the child is a @code{make} process, then the child will
9665 receive only part of the information necessary.  In this case, the child
9666 will generate this warning message and proceed with its build in a
9667 sequential manner.
9669 @end table
9671 @node Complex Makefile, Concept Index, Error Messages, Top
9672 @appendix Complex Makefile Example
9674 Here is the makefile for the GNU @code{tar} program.  This is a
9675 moderately complex makefile.
9677 Because it is the first target, the default goal is @samp{all}.  An
9678 interesting feature of this makefile is that @file{testpad.h} is a
9679 source file automatically created by the @code{testpad} program,
9680 itself compiled from @file{testpad.c}.
9682 If you type @samp{make} or @samp{make all}, then @code{make} creates
9683 the @file{tar} executable, the @file{rmt} daemon that provides
9684 remote tape access, and the @file{tar.info} Info file.
9686 If you type @samp{make install}, then @code{make} not only creates
9687 @file{tar}, @file{rmt}, and @file{tar.info}, but also installs
9688 them.
9690 If you type @samp{make clean}, then @code{make} removes the @samp{.o}
9691 files, and the @file{tar}, @file{rmt}, @file{testpad},
9692 @file{testpad.h}, and @file{core} files.
9694 If you type @samp{make distclean}, then @code{make} not only removes
9695 the same files as does @samp{make clean} but also the
9696 @file{TAGS}, @file{Makefile}, and @file{config.status} files.
9697 (Although it is not evident, this makefile (and
9698 @file{config.status}) is generated by the user with the
9699 @code{configure} program, which is provided in the @code{tar}
9700 distribution, but is not shown here.)
9702 If you type @samp{make realclean}, then @code{make} removes the same
9703 files as does @samp{make distclean} and also removes the Info files
9704 generated from @file{tar.texinfo}.
9706 In addition, there are targets @code{shar} and @code{dist} that create
9707 distribution kits.
9709 @example
9710 @group
9711 # Generated automatically from Makefile.in by configure.
9712 # Un*x Makefile for GNU tar program.
9713 # Copyright (C) 1991 Free Software Foundation, Inc.
9714 @end group
9716 @group
9717 # This program is free software; you can redistribute
9718 # it and/or modify it under the terms of the GNU
9719 # General Public License @dots{}
9720 @dots{}
9721 @dots{}
9722 @end group
9724 SHELL = /bin/sh
9726 #### Start of system configuration section. ####
9728 srcdir = .
9730 @group
9731 # If you use gcc, you should either run the
9732 # fixincludes script that comes with it or else use
9733 # gcc with the -traditional option.  Otherwise ioctl
9734 # calls will be compiled incorrectly on some systems.
9735 CC = gcc -O
9736 YACC = bison -y
9737 INSTALL = /usr/local/bin/install -c
9738 INSTALLDATA = /usr/local/bin/install -c -m 644
9739 @end group
9741 # Things you might add to DEFS:
9742 # -DSTDC_HEADERS        If you have ANSI C headers and
9743 #                       libraries.
9744 # -DPOSIX               If you have POSIX.1 headers and
9745 #                       libraries.
9746 # -DBSD42               If you have sys/dir.h (unless
9747 #                       you use -DPOSIX), sys/file.h,
9748 #                       and st_blocks in `struct stat'.
9749 # -DUSG                 If you have System V/ANSI C
9750 #                       string and memory functions
9751 #                       and headers, sys/sysmacros.h,
9752 #                       fcntl.h, getcwd, no valloc,
9753 #                       and ndir.h (unless
9754 #                       you use -DDIRENT).
9755 # -DNO_MEMORY_H         If USG or STDC_HEADERS but do not
9756 #                       include memory.h.
9757 # -DDIRENT              If USG and you have dirent.h
9758 #                       instead of ndir.h.
9759 # -DSIGTYPE=int         If your signal handlers
9760 #                       return int, not void.
9761 # -DNO_MTIO             If you lack sys/mtio.h
9762 #                       (magtape ioctls).
9763 # -DNO_REMOTE           If you do not have a remote shell
9764 #                       or rexec.
9765 # -DUSE_REXEC           To use rexec for remote tape
9766 #                       operations instead of
9767 #                       forking rsh or remsh.
9768 # -DVPRINTF_MISSING     If you lack vprintf function
9769 #                       (but have _doprnt).
9770 # -DDOPRNT_MISSING      If you lack _doprnt function.
9771 #                       Also need to define
9772 #                       -DVPRINTF_MISSING.
9773 # -DFTIME_MISSING       If you lack ftime system call.
9774 # -DSTRSTR_MISSING      If you lack strstr function.
9775 # -DVALLOC_MISSING      If you lack valloc function.
9776 # -DMKDIR_MISSING       If you lack mkdir and
9777 #                       rmdir system calls.
9778 # -DRENAME_MISSING      If you lack rename system call.
9779 # -DFTRUNCATE_MISSING   If you lack ftruncate
9780 #                       system call.
9781 # -DV7                  On Version 7 Unix (not
9782 #                       tested in a long time).
9783 # -DEMUL_OPEN3          If you lack a 3-argument version
9784 #                       of open, and want to emulate it
9785 #                       with system calls you do have.
9786 # -DNO_OPEN3            If you lack the 3-argument open
9787 #                       and want to disable the tar -k
9788 #                       option instead of emulating open.
9789 # -DXENIX               If you have sys/inode.h
9790 #                       and need it 94 to be included.
9792 DEFS =  -DSIGTYPE=int -DDIRENT -DSTRSTR_MISSING \
9793         -DVPRINTF_MISSING -DBSD42
9794 # Set this to rtapelib.o unless you defined NO_REMOTE,
9795 # in which case make it empty.
9796 RTAPELIB = rtapelib.o
9797 LIBS =
9798 DEF_AR_FILE = /dev/rmt8
9799 DEFBLOCKING = 20
9801 @group
9802 CDEBUG = -g
9803 CFLAGS = $(CDEBUG) -I. -I$(srcdir) $(DEFS) \
9804         -DDEF_AR_FILE=\"$(DEF_AR_FILE)\" \
9805         -DDEFBLOCKING=$(DEFBLOCKING)
9806 LDFLAGS = -g
9807 @end group
9809 @group
9810 prefix = /usr/local
9811 # Prefix for each installed program,
9812 # normally empty or `g'.
9813 binprefix =
9815 # The directory to install tar in.
9816 bindir = $(prefix)/bin
9818 # The directory to install the info files in.
9819 infodir = $(prefix)/info
9820 @end group
9822 #### End of system configuration section. ####
9824 SRC1 =  tar.c create.c extract.c buffer.c \
9825         getoldopt.c update.c gnu.c mangle.c
9826 SRC2 =  version.c list.c names.c diffarch.c \
9827         port.c wildmat.c getopt.c
9828 SRC3 =  getopt1.c regex.c getdate.y
9829 SRCS =  $(SRC1) $(SRC2) $(SRC3)
9830 OBJ1 =  tar.o create.o extract.o buffer.o \
9831         getoldopt.o update.o gnu.o mangle.o
9832 OBJ2 =  version.o list.o names.o diffarch.o \
9833         port.o wildmat.o getopt.o
9834 OBJ3 =  getopt1.o regex.o getdate.o $(RTAPELIB)
9835 OBJS =  $(OBJ1) $(OBJ2) $(OBJ3)
9836 @group
9837 AUX =   README COPYING ChangeLog Makefile.in  \
9838         makefile.pc configure configure.in \
9839         tar.texinfo tar.info* texinfo.tex \
9840         tar.h port.h open3.h getopt.h regex.h \
9841         rmt.h rmt.c rtapelib.c alloca.c \
9842         msd_dir.h msd_dir.c tcexparg.c \
9843         level-0 level-1 backup-specs testpad.c
9844 @end group
9846 all:    tar rmt tar.info
9848 @group
9849 tar:    $(OBJS)
9850         $(CC) $(LDFLAGS) -o $@@ $(OBJS) $(LIBS)
9851 @end group
9853 @group
9854 rmt:    rmt.c
9855         $(CC) $(CFLAGS) $(LDFLAGS) -o $@@ rmt.c
9856 @end group
9858 @group
9859 tar.info: tar.texinfo
9860         makeinfo tar.texinfo
9861 @end group
9863 @group
9864 install: all
9865         $(INSTALL) tar $(bindir)/$(binprefix)tar
9866         -test ! -f rmt || $(INSTALL) rmt /etc/rmt
9867         $(INSTALLDATA) $(srcdir)/tar.info* $(infodir)
9868 @end group
9870 @group
9871 $(OBJS): tar.h port.h testpad.h
9872 regex.o buffer.o tar.o: regex.h
9873 # getdate.y has 8 shift/reduce conflicts.
9874 @end group
9876 @group
9877 testpad.h: testpad
9878         ./testpad
9879 @end group
9881 @group
9882 testpad: testpad.o
9883         $(CC) -o $@@ testpad.o
9884 @end group
9886 @group
9887 TAGS:   $(SRCS)
9888         etags $(SRCS)
9889 @end group
9891 @group
9892 clean:
9893         rm -f *.o tar rmt testpad testpad.h core
9894 @end group
9896 @group
9897 distclean: clean
9898         rm -f TAGS Makefile config.status
9899 @end group
9901 @group
9902 realclean: distclean
9903         rm -f tar.info*
9904 @end group
9906 @group
9907 shar: $(SRCS) $(AUX)
9908         shar $(SRCS) $(AUX) | compress \
9909           > tar-`sed -e '/version_string/!d' \
9910                      -e 's/[^0-9.]*\([0-9.]*\).*/\1/' \
9911                      -e q
9912                      version.c`.shar.Z
9913 @end group
9915 @group
9916 dist: $(SRCS) $(AUX)
9917         echo tar-`sed \
9918              -e '/version_string/!d' \
9919              -e 's/[^0-9.]*\([0-9.]*\).*/\1/' \
9920              -e q
9921              version.c` > .fname
9922         -rm -rf `cat .fname`
9923         mkdir `cat .fname`
9924         ln $(SRCS) $(AUX) `cat .fname`
9925         tar chZf `cat .fname`.tar.Z `cat .fname`
9926         -rm -rf `cat .fname` .fname
9927 @end group
9929 @group
9930 tar.zoo: $(SRCS) $(AUX)
9931         -rm -rf tmp.dir
9932         -mkdir tmp.dir
9933         -rm tar.zoo
9934         for X in $(SRCS) $(AUX) ; do \
9935             echo $$X ; \
9936             sed 's/$$/^M/' $$X \
9937             > tmp.dir/$$X ; done
9938         cd tmp.dir ; zoo aM ../tar.zoo *
9939         -rm -rf tmp.dir
9940 @end group
9941 @end example
9943 @node Concept Index, Name Index, Complex Makefile, Top
9944 @unnumbered Index of Concepts
9946 @printindex cp
9948 @node Name Index,  , Concept Index, Top
9949 @unnumbered Index of Functions, Variables, & Directives
9951 @printindex fn
9953 @summarycontents
9954 @contents
9955 @bye