- New code capability: a read-only string cache. Start of solution for
[make.git] / doc / make.texi
blob499fd2eb5a71685c34f1c962549ddab9786c1186
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.texi,v 1.42 2006/02/10 05:29:00 psmith Exp $
11 @set EDITION 0.70
12 @set VERSION 3.81
13 @set UPDATED 5 Feb 2006
14 @set UPDATE-MONTH Feb 2006
15 @c ISBN provided by Lisa M. Opus Goldstein <opus@gnu.org>, 5 May 2004
16 @set ISBN 1-882114-83-5
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 @ifnottex
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 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
42 1998, 1999, 2000, 2002, 2003, 2004, 2005, 2006
43 Free Software Foundation, Inc.
45 Permission is granted to copy, distribute and/or modify this document
46 under the terms of the GNU Free Documentation License, Version 1.1 or
47 any later version published by the Free Software Foundation; with no
48 Invariant Sections, with no Front-Cover Texts, and with no Back-Cover
49 Texts.  A copy of the license is included in the section entitled
50 ``GNU Free Documentation License''.
51 @end ifnottex
53 @iftex
54 @shorttitlepage GNU Make
55 @end iftex
56 @titlepage
57 @title GNU Make
58 @subtitle A Program for Directing Recompilation
59 @subtitle GNU @code{make} Version @value{VERSION}
60 @subtitle @value{UPDATE-MONTH}
61 @author Richard M. Stallman, Roland McGrath, Paul D. Smith
62 @page
63 @vskip 0pt plus 1filll
64 Copyright @copyright{} 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
65 1996, 1997, 1998, 1999, 2000, 2002, 2003, 2004 Free Software Foundation, Inc.
66 @sp 2
67 Published by the Free Software Foundation @*
68 59 Temple Place -- Suite 330, @*
69 Boston, MA 02111-1307 USA @*
70 ISBN @value{ISBN} @*
72 Permission is granted to copy, distribute and/or modify this document
73 under the terms of the GNU Free Documentation License, Version 1.1 or
74 any later version published by the Free Software Foundation; with the
75 Invariant Sections being ``GNU General Public License'', the Front-Cover
76 Texts being ``A GNU Manual'', and with the Back-Cover Texts being as in
77 (a) below.  A copy of the license is included in the section entitled
78 ``GNU Free Documentation License''.
80 (a) The FSF's Back-Cover Text is:
82 @quotation
83       You have freedom to copy and modify this GNU Manual, like GNU
84       software.  Copies published by the Free Software Foundation raise
85       funds for GNU development.
86 @end quotation
87 @sp 2
88 Cover art by Etienne Suvasa.
89 @end titlepage
90 @page
92 @ifnottex
93 @node Top, Overview, (dir), (dir)
94 @top Make
96 The GNU @code{make} utility automatically determines which pieces of a
97 large program need to be recompiled, and issues the commands to
98 recompile them.@refill
100 This edition of the @cite{GNU Make Manual},
101 last updated @value{UPDATED},
102 documents GNU @code{make} Version @value{VERSION}.@refill
104 This manual describes @code{make} and contains the following chapters:@refill
105 @end ifnottex
107 @menu
108 * Overview::                    Overview of @code{make}.
109 * Introduction::                An introduction to @code{make}.
110 * Makefiles::                   Makefiles tell @code{make} what to do.
111 * Rules::                       Rules describe when a file must be remade.
112 * Commands::                    Commands say how to remake a file.
113 * Using Variables::             You can use variables to avoid repetition.
114 * Conditionals::                Use or ignore parts of the makefile based
115                                   on the values of variables.
116 * Functions::                   Many powerful ways to manipulate text.
117 * Invoking make: Running.       How to invoke @code{make} on the command line.
118 * Implicit Rules::              Use implicit rules to treat many files alike,
119                                   based on their file names.
120 * Archives::                    How @code{make} can update library archives.
121 * Features::                    Features GNU @code{make} has over other @code{make}s.
122 * Missing::                     What GNU @code{make} lacks from other @code{make}s.
123 * Makefile Conventions::        Conventions for writing makefiles for
124                                   GNU programs.
125 * Quick Reference::             A quick reference for experienced users.
126 * Error Messages::              A list of common errors generated by @code{make}.
127 * Complex Makefile::            A real example of a straightforward,
128                                   but nontrivial, makefile.
130 * GNU Free Documentation License::  License for copying this manual
131 * Concept Index::               Index of Concepts
132 * Name Index::                  Index of Functions, Variables, & Directives
134 @detailmenu
135  --- The Detailed Node Listing ---
137 Overview of @code{make}
139 * Preparing::                   Preparing and Running Make
140 * Reading::                     On Reading this Text
141 * Bugs::                        Problems and Bugs
143 An Introduction to Makefiles
145 * Rule Introduction::           What a rule looks like.
146 * Simple Makefile::             A Simple Makefile
147 * How Make Works::              How @code{make} Processes This Makefile
148 * Variables Simplify::          Variables Make Makefiles Simpler
149 * make Deduces::                Letting @code{make} Deduce the Commands
150 * Combine By Prerequisite::     Another Style of Makefile
151 * Cleanup::                     Rules for Cleaning the Directory
153 Writing Makefiles
155 * Makefile Contents::           What makefiles contain.
156 * Makefile Names::              How to name your makefile.
157 * Include::                     How one makefile can use another makefile.
158 * MAKEFILES Variable::          The environment can specify extra makefiles.
159 * MAKEFILE_LIST Variable::      Discover which makefiles have been read.
160 * Special Variables::           Other special variables.
161 * Remaking Makefiles::          How makefiles get remade.
162 * Overriding Makefiles::        How to override part of one makefile
163                                   with another makefile.
164 * Reading Makefiles::           How makefiles are parsed.
165 * Secondary Expansion::         How and when secondary expansion is performed.
167 Writing Rules
169 * Rule Example::                An example explained.
170 * Rule Syntax::                 General syntax explained.
171 * Prerequisite Types::          There are two types of prerequisites.
172 * Wildcards::                   Using wildcard characters such as `*'.
173 * Directory Search::            Searching other directories for source files.
174 * Phony Targets::               Using a target that is not a real file's name.
175 * Force Targets::               You can use a target without commands
176                                   or prerequisites to mark other targets
177                                   as phony.
178 * Empty Targets::               When only the date matters and the
179                                   files are empty.
180 * Special Targets::             Targets with special built-in meanings.
181 * Multiple Targets::            When to make use of several targets in a rule.
182 * Multiple Rules::              How to use several rules with the same target.
183 * Static Pattern::              Static pattern rules apply to multiple targets
184                                   and can vary the prerequisites according to
185                                   the target name.
186 * Double-Colon::                How to use a special kind of rule to allow
187                                   several independent rules for one target.
188 * Automatic Prerequisites::     How to automatically generate rules giving
189                                   prerequisites from source files themselves.
191 Using Wildcard Characters in File Names
193 * Wildcard Examples::           Several examples
194 * Wildcard Pitfall::            Problems to avoid.
195 * Wildcard Function::           How to cause wildcard expansion where
196                                   it does not normally take place.
198 Searching Directories for Prerequisites
200 * General Search::              Specifying a search path that applies
201                                   to every prerequisite.
202 * Selective Search::            Specifying a search path
203                                   for a specified class of names.
204 * Search Algorithm::            When and how search paths are applied.
205 * Commands/Search::             How to write shell commands that work together
206                                   with search paths.
207 * Implicit/Search::             How search paths affect implicit rules.
208 * Libraries/Search::            Directory search for link libraries.
210 Static Pattern Rules
212 * Static Usage::                The syntax of static pattern rules.
213 * Static versus Implicit::      When are they better than implicit rules?
215 Writing the Commands in Rules
217 * Command Syntax::              Command syntax features and pitfalls.
218 * Echoing::                     How to control when commands are echoed.
219 * Execution::                   How commands are executed.
220 * Parallel::                    How commands can be executed in parallel.
221 * Errors::                      What happens after a command execution error.
222 * Interrupts::                  What happens when a command is interrupted.
223 * Recursion::                   Invoking @code{make} from makefiles.
224 * Sequences::                   Defining canned sequences of commands.
225 * Empty Commands::              Defining useful, do-nothing commands.
227 Command Syntax
229 * Splitting Lines::             Breaking long command lines for readability.
230 * Variables in Commands::       Using @code{make} variables in commands.
232 Command Execution
234 * Choosing the Shell::          How @code{make} chooses the shell used
235                                   to run commands.
237 Recursive Use of @code{make}
239 * MAKE Variable::               The special effects of using @samp{$(MAKE)}.
240 * Variables/Recursion::         How to communicate variables to a sub-@code{make}.
241 * Options/Recursion::           How to communicate options to a sub-@code{make}.
242 * -w Option::                   How the @samp{-w} or @samp{--print-directory} option
243                                   helps debug use of recursive @code{make} commands.
245 How to Use Variables
247 * Reference::                   How to use the value of a variable.
248 * Flavors::                     Variables come in two flavors.
249 * Advanced::                    Advanced features for referencing a variable.
250 * Values::                      All the ways variables get their values.
251 * Setting::                     How to set a variable in the makefile.
252 * Appending::                   How to append more text to the old value
253                                   of a variable.
254 * Override Directive::          How to set a variable in the makefile even if
255                                   the user has set it with a command argument.
256 * Defining::                    An alternate way to set a variable
257                                   to a verbatim string.
258 * Environment::                 Variable values can come from the environment.
259 * Target-specific::             Variable values can be defined on a per-target
260                                   basis.
261 * Pattern-specific::            Target-specific variable values can be applied
262                                   to a group of targets that match a pattern.
264 Advanced Features for Reference to Variables
266 * Substitution Refs::           Referencing a variable with
267                                   substitutions on the value.
268 * Computed Names::              Computing the name of the variable to refer to.
270 Conditional Parts of Makefiles
272 * Conditional Example::         Example of a conditional
273 * Conditional Syntax::          The syntax of conditionals.
274 * Testing Flags::               Conditionals that test flags.
276 Functions for Transforming Text
278 * Syntax of Functions::         How to write a function call.
279 * Text Functions::              General-purpose text manipulation functions.
280 * File Name Functions::         Functions for manipulating file names.
281 * Conditional Functions::       Functions that implement conditions.
282 * Foreach Function::            Repeat some text with controlled variation.
283 * Call Function::               Expand a user-defined function.
284 * Value Function::              Return the un-expanded value of a variable.
285 * Eval Function::               Evaluate the arguments as makefile syntax.
286 * Origin Function::             Find where a variable got its value.
287 * Flavor Function::             Find out the flavor of a variable.
288 * Shell Function::              Substitute the output of a shell command.
289 * Make Control Functions::      Functions that control how make runs.
291 How to Run @code{make}
293 * Makefile Arguments::          How to specify which makefile to use.
294 * Goals::                       How to use goal arguments to specify which
295                                   parts of the makefile to use.
296 * Instead of Execution::        How to use mode flags to specify what
297                                   kind of thing to do with the commands
298                                   in the makefile other than simply
299                                   execute them.
300 * Avoiding Compilation::        How to avoid recompiling certain files.
301 * Overriding::                  How to override a variable to specify
302                                   an alternate compiler and other things.
303 * Testing::                     How to proceed past some errors, to
304                                   test compilation.
305 * Options Summary::             Summary of Options
307 Using Implicit Rules
309 * Using Implicit::              How to use an existing implicit rule
310                                   to get the commands for updating a file.
311 * Catalogue of Rules::          A list of built-in implicit rules.
312 * Implicit Variables::          How to change what predefined rules do.
313 * Chained Rules::               How to use a chain of implicit rules.
314 * Pattern Rules::               How to define new implicit rules.
315 * Last Resort::                 How to define commands for rules which
316                                   cannot find any.
317 * Suffix Rules::                The old-fashioned style of implicit rule.
318 * Implicit Rule Search::        The precise algorithm for applying
319                                   implicit rules.
321 Defining and Redefining Pattern Rules
323 * Pattern Intro::               An introduction to pattern rules.
324 * Pattern Examples::            Examples of pattern rules.
325 * Automatic Variables::         How to use automatic variables in the
326                                   commands of implicit rules.
327 * Pattern Match::               How patterns match.
328 * Match-Anything Rules::        Precautions you should take prior to
329                                   defining rules that can match any
330                                   target file whatever.
331 * Canceling Rules::             How to override or cancel built-in rules.
333 Using @code{make} to Update Archive Files
335 * Archive Members::             Archive members as targets.
336 * Archive Update::              The implicit rule for archive member targets.
337 * Archive Pitfalls::            Dangers to watch out for when using archives.
338 * Archive Suffix Rules::        You can write a special kind of suffix rule
339                                   for updating archives.
341 Implicit Rule for Archive Member Targets
343 * Archive Symbols::             How to update archive symbol directories.
345 @end detailmenu
346 @end menu
348 @node Overview, Introduction, Top, Top
349 @comment  node-name,  next,  previous,  up
350 @chapter Overview of @code{make}
352 The @code{make} utility automatically determines which pieces of a large
353 program need to be recompiled, and issues commands to recompile them.
354 This manual describes GNU @code{make}, which was implemented by Richard
355 Stallman and Roland McGrath.  Development since Version 3.76 has been
356 handled by Paul D. Smith.
358 GNU @code{make} conforms to section 6.2 of @cite{IEEE Standard
359 1003.2-1992} (POSIX.2).
360 @cindex POSIX
361 @cindex IEEE Standard 1003.2
362 @cindex standards conformance
364 Our examples show C programs, since they are most common, but you can use
365 @code{make} with any programming language whose compiler can be run with a
366 shell command.  Indeed, @code{make} is not limited to programs.  You can
367 use it to describe any task where some files must be updated automatically
368 from others whenever the others change.
370 @menu
371 * Preparing::                   Preparing and Running Make
372 * Reading::                     On Reading this Text
373 * Bugs::                        Problems and Bugs
374 @end menu
376 @node Preparing, Reading, Overview, Overview
377 @ifnottex
378 @heading Preparing and Running Make
379 @end ifnottex
381 To prepare to use @code{make}, you must write a file called
382 the @dfn{makefile} that describes the relationships among files
383 in your program and provides commands for updating each file.
384 In a program, typically, the executable file is updated from object
385 files, which are in turn made by compiling source files.@refill
387 Once a suitable makefile exists, each time you change some source files,
388 this simple shell command:
390 @example
391 make
392 @end example
394 @noindent
395 suffices to perform all necessary recompilations.  The @code{make} program
396 uses the makefile data base and the last-modification times of the files to
397 decide which of the files need to be updated.  For each of those files, it
398 issues the commands recorded in the data base.
400 You can provide command line arguments to @code{make} to control which
401 files should be recompiled, or how.  @xref{Running, ,How to Run
402 @code{make}}.
404 @node Reading, Bugs, Preparing, Overview
405 @section How to Read This Manual
407 If you are new to @code{make}, or are looking for a general
408 introduction, read the first few sections of each chapter, skipping the
409 later sections.  In each chapter, the first few sections contain
410 introductory or general information and the later sections contain
411 specialized or technical information.
412 @ifnottex
413 The exception is the second chapter, @ref{Introduction, ,An
414 Introduction to Makefiles}, all of which is introductory.
415 @end ifnottex
416 @iftex
417 The exception is @ref{Introduction, ,An Introduction to Makefiles},
418 all of which is introductory.
419 @end iftex
421 If you are familiar with other @code{make} programs, see @ref{Features,
422 ,Features of GNU @code{make}}, which lists the enhancements GNU
423 @code{make} has, and @ref{Missing, ,Incompatibilities and Missing
424 Features}, which explains the few things GNU @code{make} lacks that
425 others have.
427 For a quick summary, see @ref{Options Summary}, @ref{Quick Reference},
428 and @ref{Special Targets}.
430 @node Bugs,  , Reading, Overview
431 @section Problems and Bugs
432 @cindex reporting bugs
433 @cindex bugs, reporting
434 @cindex problems and bugs, reporting
436 If you have problems with GNU @code{make} or think you've found a bug,
437 please report it to the developers; we cannot promise to do anything but
438 we might well want to fix it.
440 Before reporting a bug, make sure you've actually found a real bug.
441 Carefully reread the documentation and see if it really says you can do
442 what you're trying to do.  If it's not clear whether you should be able
443 to do something or not, report that too; it's a bug in the
444 documentation!
446 Before reporting a bug or trying to fix it yourself, try to isolate it
447 to the smallest possible makefile that reproduces the problem.  Then
448 send us the makefile and the exact results @code{make} gave you,
449 including any error or warning messages.  Please don't paraphrase
450 these messages: it's best to cut and paste them into your report.
451 When generating this small makefile, be sure to not use any non-free
452 or unusual tools in your commands: you can almost always emulate what
453 such a tool would do with simple shell commands.  Finally, be sure to
454 explain what you expected to occur; this will help us decide whether
455 the problem was really in the documentation.
457 Once you have a precise problem you can report it in one of two ways.
458 Either send electronic mail to:
460 @example
461     bug-make@@gnu.org
462 @end example
464 @noindent
465 or use our Web-based project management tool, at:
467 @example
468     http://savannah.gnu.org/projects/make/
469 @end example
471 @noindent
472 In addition to the information above, please be careful to include the
473 version number of @code{make} you are using.  You can get this
474 information with the command @samp{make --version}.  Be sure also to
475 include the type of machine and operating system you are using.  One
476 way to obtain this information is by looking at the final lines of
477 output from the command @samp{make --help}.
479 @node Introduction, Makefiles, Overview, Top
480 @comment  node-name,  next,  previous,  up
481 @chapter An Introduction to Makefiles
483 You need a file called a @dfn{makefile} to tell @code{make} what to do.
484 Most often, the makefile tells @code{make} how to compile and link a
485 program.
486 @cindex makefile
488 In this chapter, we will discuss a simple makefile that describes how to
489 compile and link a text editor which consists of eight C source files
490 and three header files.  The makefile can also tell @code{make} how to
491 run miscellaneous commands when explicitly asked (for example, to remove
492 certain files as a clean-up operation).  To see a more complex example
493 of a makefile, see @ref{Complex Makefile}.
495 When @code{make} recompiles the editor, each changed C source file
496 must be recompiled.  If a header file has changed, each C source file
497 that includes the header file must be recompiled to be safe.  Each
498 compilation produces an object file corresponding to the source file.
499 Finally, if any source file has been recompiled, all the object files,
500 whether newly made or saved from previous compilations, must be linked
501 together to produce the new executable editor.
502 @cindex recompilation
503 @cindex editor
505 @menu
506 * Rule Introduction::           What a rule looks like.
507 * Simple Makefile::             A Simple Makefile
508 * How Make Works::              How @code{make} Processes This Makefile
509 * Variables Simplify::          Variables Make Makefiles Simpler
510 * make Deduces::                Letting @code{make} Deduce the Commands
511 * Combine By Prerequisite::     Another Style of Makefile
512 * Cleanup::                     Rules for Cleaning the Directory
513 @end menu
515 @node Rule Introduction, Simple Makefile, Introduction, Introduction
516 @comment  node-name,  next,  previous,  up
517 @section What a Rule Looks Like
518 @cindex rule, introduction to
519 @cindex makefile rule parts
520 @cindex parts of makefile rule
522 A simple makefile consists of ``rules'' with the following shape:
524 @cindex targets, introduction to
525 @cindex prerequisites, introduction to
526 @cindex commands, introduction to
527 @example
528 @group
529 @var{target} @dots{} : @var{prerequisites} @dots{}
530         @var{command}
531         @dots{}
532         @dots{}
533 @end group
534 @end example
536 A @dfn{target} is usually the name of a file that is generated by a
537 program; examples of targets are executable or object files.  A target
538 can also be the name of an action to carry out, such as @samp{clean}
539 (@pxref{Phony Targets}).
541 A @dfn{prerequisite} is a file that is used as input to create the
542 target.  A target often depends on several files.
544 @cindex tabs in rules
545 A @dfn{command} is an action that @code{make} carries out.
546 A rule may have more than one command, each on its own line.
547 @strong{Please note:} you need to put a tab character at the beginning of
548 every command line!  This is an obscurity that catches the unwary.
550 Usually a command is in a rule with prerequisites and serves to create a
551 target file if any of the prerequisites change.  However, the rule that
552 specifies commands for the target need not have prerequisites.  For
553 example, the rule containing the delete command associated with the
554 target @samp{clean} does not have prerequisites.
556 A @dfn{rule}, then, explains how and when to remake certain files
557 which are the targets of the particular rule.  @code{make} carries out
558 the commands on the prerequisites to create or update the target.  A
559 rule can also explain how and when to carry out an action.
560 @xref{Rules, , Writing Rules}.
562 A makefile may contain other text besides rules, but a simple makefile
563 need only contain rules.  Rules may look somewhat more complicated
564 than shown in this template, but all fit the pattern more or less.
566 @node Simple Makefile, How Make Works, Rule Introduction, Introduction
567 @section A Simple Makefile
568 @cindex simple makefile
569 @cindex makefile, simple
571 Here is a straightforward makefile that describes the way an
572 executable file called @code{edit} depends on eight object files
573 which, in turn, depend on eight C source and three header files.
575 In this example, all the C files include @file{defs.h}, but only those
576 defining editing commands include @file{command.h}, and only low
577 level files that change the editor buffer include @file{buffer.h}.
579 @example
580 @group
581 edit : main.o kbd.o command.o display.o \
582        insert.o search.o files.o utils.o
583         cc -o edit main.o kbd.o command.o display.o \
584                    insert.o search.o files.o utils.o
586 main.o : main.c defs.h
587         cc -c main.c
588 kbd.o : kbd.c defs.h command.h
589         cc -c kbd.c
590 command.o : command.c defs.h command.h
591         cc -c command.c
592 display.o : display.c defs.h buffer.h
593         cc -c display.c
594 insert.o : insert.c defs.h buffer.h
595         cc -c insert.c
596 search.o : search.c defs.h buffer.h
597         cc -c search.c
598 files.o : files.c defs.h buffer.h command.h
599         cc -c files.c
600 utils.o : utils.c defs.h
601         cc -c utils.c
602 clean :
603         rm edit main.o kbd.o command.o display.o \
604            insert.o search.o files.o utils.o
605 @end group
606 @end example
608 @noindent
609 We split each long line into two lines using backslash-newline; this is
610 like using one long line, but is easier to read.
611 @cindex continuation lines
612 @cindex @code{\} (backslash), for continuation lines
613 @cindex backslash (@code{\}), for continuation lines
614 @cindex quoting newline, in makefile
615 @cindex newline, quoting, in makefile
617 To use this makefile to create the executable file called @file{edit},
618 type:
620 @example
621 make
622 @end example
624 To use this makefile to delete the executable file and all the object
625 files from the directory, type:
627 @example
628 make clean
629 @end example
631 In the example makefile, the targets include the executable file
632 @samp{edit}, and the object files @samp{main.o} and @samp{kbd.o}.  The
633 prerequisites are files such as @samp{main.c} and @samp{defs.h}.
634 In fact, each @samp{.o} file is both a target and a prerequisite.
635 Commands include @w{@samp{cc -c main.c}} and @w{@samp{cc -c kbd.c}}.
637 When a target is a file, it needs to be recompiled or relinked if any
638 of its prerequisites change.  In addition, any prerequisites that are
639 themselves automatically generated should be updated first.  In this
640 example, @file{edit} depends on each of the eight object files; the
641 object file @file{main.o} depends on the source file @file{main.c} and
642 on the header file @file{defs.h}.
644 A shell command follows each line that contains a target and
645 prerequisites.  These shell commands say how to update the target file.
646 A tab character must come at the beginning of every command line to
647 distinguish command lines from other lines in the makefile.  (Bear in
648 mind that @code{make} does not know anything about how the commands
649 work.  It is up to you to supply commands that will update the target
650 file properly.  All @code{make} does is execute the commands in the rule
651 you have specified when the target file needs to be updated.)
652 @cindex shell command
654 The target @samp{clean} is not a file, but merely the name of an
655 action.  Since you
656 normally
657 do not want to carry out the actions in this rule, @samp{clean} is not a prerequisite of any other rule.
658 Consequently, @code{make} never does anything with it unless you tell
659 it specifically.  Note that this rule not only is not a prerequisite, it
660 also does not have any prerequisites, so the only purpose of the rule
661 is to run the specified commands.  Targets that do not refer to files
662 but are just actions are called @dfn{phony targets}.  @xref{Phony
663 Targets}, for information about this kind of target.  @xref{Errors, ,
664 Errors in Commands}, to see how to cause @code{make} to ignore errors
665 from @code{rm} or any other command.
666 @cindex @code{clean} target
667 @cindex @code{rm} (shell command)
669 @node How Make Works, Variables Simplify, Simple Makefile, Introduction
670 @comment  node-name,  next,  previous,  up
671 @section How @code{make} Processes a Makefile
672 @cindex processing a makefile
673 @cindex makefile, how @code{make} processes
675 By default, @code{make} starts with the first target (not targets whose
676 names start with @samp{.}).  This is called the @dfn{default goal}.
677 (@dfn{Goals} are the targets that @code{make} strives ultimately to
678 update.    You can override this behavior using the command line
679 (@pxref{Goals, , Arguments to Specify the Goals}) or with the
680 @code{.DEFAULT_GOAL} special variable (@pxref{Special Variables, ,
681 Other Special Variables}).
682 @cindex default goal
683 @cindex goal, default
684 @cindex goal
686 In the simple example of the previous section, the default goal is to
687 update the executable program @file{edit}; therefore, we put that rule
688 first.
690 Thus, when you give the command:
692 @example
693 make
694 @end example
696 @noindent
697 @code{make} reads the makefile in the current directory and begins by
698 processing the first rule.  In the example, this rule is for relinking
699 @file{edit}; but before @code{make} can fully process this rule, it
700 must process the rules for the files that @file{edit} depends on,
701 which in this case are the object files.  Each of these files is
702 processed according to its own rule.  These rules say to update each
703 @samp{.o} file by compiling its source file.  The recompilation must
704 be done if the source file, or any of the header files named as
705 prerequisites, is more recent than the object file, or if the object
706 file does not exist.
708 The other rules are processed because their targets appear as
709 prerequisites of the goal.  If some other rule is not depended on by the
710 goal (or anything it depends on, etc.), that rule is not processed,
711 unless you tell @code{make} to do so (with a command such as
712 @w{@code{make clean}}).
714 Before recompiling an object file, @code{make} considers updating its
715 prerequisites, the source file and header files.  This makefile does not
716 specify anything to be done for them---the @samp{.c} and @samp{.h} files
717 are not the targets of any rules---so @code{make} does nothing for these
718 files.  But @code{make} would update automatically generated C programs,
719 such as those made by Bison or Yacc, by their own rules at this time.
721 After recompiling whichever object files need it, @code{make} decides
722 whether to relink @file{edit}.  This must be done if the file
723 @file{edit} does not exist, or if any of the object files are newer than
724 it.  If an object file was just recompiled, it is now newer than
725 @file{edit}, so @file{edit} is relinked.
726 @cindex relinking
728 Thus, if we change the file @file{insert.c} and run @code{make},
729 @code{make} will compile that file to update @file{insert.o}, and then
730 link @file{edit}.  If we change the file @file{command.h} and run
731 @code{make}, @code{make} will recompile the object files @file{kbd.o},
732 @file{command.o} and @file{files.o} and then link the file @file{edit}.
734 @node Variables Simplify, make Deduces, How Make Works, Introduction
735 @section Variables Make Makefiles Simpler
736 @cindex variables
737 @cindex simplifying with variables
739 In our example, we had to list all the object files twice in the rule for
740 @file{edit} (repeated here):
742 @example
743 @group
744 edit : main.o kbd.o command.o display.o \
745               insert.o search.o files.o utils.o
746         cc -o edit main.o kbd.o command.o display.o \
747                    insert.o search.o files.o utils.o
748 @end group
749 @end example
751 @cindex @code{objects}
752 Such duplication is error-prone; if a new object file is added to the
753 system, we might add it to one list and forget the other.  We can eliminate
754 the risk and simplify the makefile by using a variable.  @dfn{Variables}
755 allow a text string to be defined once and substituted in multiple places
756 later (@pxref{Using Variables, ,How to Use Variables}).
758 @cindex @code{OBJECTS}
759 @cindex @code{objs}
760 @cindex @code{OBJS}
761 @cindex @code{obj}
762 @cindex @code{OBJ}
763 It is standard practice for every makefile to have a variable named
764 @code{objects}, @code{OBJECTS}, @code{objs}, @code{OBJS}, @code{obj},
765 or @code{OBJ} which is a list of all object file names.  We would
766 define such a variable @code{objects} with a line like this in the
767 makefile:@refill
769 @example
770 @group
771 objects = main.o kbd.o command.o display.o \
772           insert.o search.o files.o utils.o
773 @end group
774 @end example
776 @noindent
777 Then, each place we want to put a list of the object file names, we can
778 substitute the variable's value by writing @samp{$(objects)}
779 (@pxref{Using Variables, ,How to Use Variables}).
781 Here is how the complete simple makefile looks when you use a variable
782 for the object files:
784 @example
785 @group
786 objects = main.o kbd.o command.o display.o \
787           insert.o search.o files.o utils.o
789 edit : $(objects)
790         cc -o edit $(objects)
791 main.o : main.c defs.h
792         cc -c main.c
793 kbd.o : kbd.c defs.h command.h
794         cc -c kbd.c
795 command.o : command.c defs.h command.h
796         cc -c command.c
797 display.o : display.c defs.h buffer.h
798         cc -c display.c
799 insert.o : insert.c defs.h buffer.h
800         cc -c insert.c
801 search.o : search.c defs.h buffer.h
802         cc -c search.c
803 files.o : files.c defs.h buffer.h command.h
804         cc -c files.c
805 utils.o : utils.c defs.h
806         cc -c utils.c
807 clean :
808         rm edit $(objects)
809 @end group
810 @end example
812 @node make Deduces, Combine By Prerequisite, Variables Simplify, Introduction
813 @section Letting @code{make} Deduce the Commands
814 @cindex deducing commands (implicit rules)
815 @cindex implicit rule, introduction to
816 @cindex rule, implicit, introduction to
818 It is not necessary to spell out the commands for compiling the individual
819 C source files, because @code{make} can figure them out: it has an
820 @dfn{implicit rule} for updating a @samp{.o} file from a correspondingly
821 named @samp{.c} file using a @samp{cc -c} command.  For example, it will
822 use the command @samp{cc -c main.c -o main.o} to compile @file{main.c} into
823 @file{main.o}.  We can therefore omit the commands from the rules for the
824 object files.  @xref{Implicit Rules, ,Using Implicit Rules}.@refill
826 When a @samp{.c} file is used automatically in this way, it is also
827 automatically added to the list of prerequisites.  We can therefore omit
828 the @samp{.c} files from the prerequisites, provided we omit the commands.
830 Here is the entire example, with both of these changes, and a variable
831 @code{objects} as suggested above:
833 @example
834 @group
835 objects = main.o kbd.o command.o display.o \
836           insert.o search.o files.o utils.o
838 edit : $(objects)
839         cc -o edit $(objects)
841 main.o : defs.h
842 kbd.o : defs.h command.h
843 command.o : defs.h command.h
844 display.o : defs.h buffer.h
845 insert.o : defs.h buffer.h
846 search.o : defs.h buffer.h
847 files.o : defs.h buffer.h command.h
848 utils.o : defs.h
850 .PHONY : clean
851 clean :
852         rm edit $(objects)
853 @end group
854 @end example
856 @noindent
857 This is how we would write the makefile in actual practice.  (The
858 complications associated with @samp{clean} are described elsewhere.
859 See @ref{Phony Targets}, and @ref{Errors, ,Errors in Commands}.)
861 Because implicit rules are so convenient, they are important.  You
862 will see them used frequently.@refill
864 @node Combine By Prerequisite, Cleanup, make Deduces, Introduction
865 @section Another Style of Makefile
866 @cindex combining rules by prerequisite
868 When the objects of a makefile are created only by implicit rules, an
869 alternative style of makefile is possible.  In this style of makefile,
870 you group entries by their prerequisites instead of by their targets.
871 Here is what one looks like:
873 @example
874 @group
875 objects = main.o kbd.o command.o display.o \
876           insert.o search.o files.o utils.o
878 edit : $(objects)
879         cc -o edit $(objects)
881 $(objects) : defs.h
882 kbd.o command.o files.o : command.h
883 display.o insert.o search.o files.o : buffer.h
884 @end group
885 @end example
887 @noindent
888 Here @file{defs.h} is given as a prerequisite of all the object files;
889 @file{command.h} and @file{buffer.h} are prerequisites of the specific
890 object files listed for them.
892 Whether this is better is a matter of taste: it is more compact, but some
893 people dislike it because they find it clearer to put all the information
894 about each target in one place.
896 @node Cleanup,  , Combine By Prerequisite, Introduction
897 @section Rules for Cleaning the Directory
898 @cindex cleaning up
899 @cindex removing, to clean up
901 Compiling a program is not the only thing you might want to write rules
902 for.  Makefiles commonly tell how to do a few other things besides
903 compiling a program: for example, how to delete all the object files
904 and executables so that the directory is @samp{clean}.
906 @cindex @code{clean} target
907 Here is how we
908 could write a @code{make} rule for cleaning our example editor:
910 @example
911 @group
912 clean:
913         rm edit $(objects)
914 @end group
915 @end example
917 In practice, we might want to write the rule in a somewhat more
918 complicated manner to handle unanticipated situations.  We would do this:
920 @example
921 @group
922 .PHONY : clean
923 clean :
924         -rm edit $(objects)
925 @end group
926 @end example
928 @noindent
929 This prevents @code{make} from getting confused by an actual file
930 called @file{clean} and causes it to continue in spite of errors from
931 @code{rm}.  (See @ref{Phony Targets}, and @ref{Errors, ,Errors in
932 Commands}.)
934 @noindent
935 A rule such as this should not be placed at the beginning of the
936 makefile, because we do not want it to run by default!  Thus, in the
937 example makefile, we want the rule for @code{edit}, which recompiles
938 the editor, to remain the default goal.
940 Since @code{clean} is not a prerequisite of @code{edit}, this rule will not
941 run at all if we give the command @samp{make} with no arguments.  In
942 order to make the rule run, we have to type @samp{make clean}.
943 @xref{Running, ,How to Run @code{make}}.
945 @node Makefiles, Rules, Introduction, Top
946 @chapter Writing Makefiles
948 @cindex makefile, how to write
949 The information that tells @code{make} how to recompile a system comes from
950 reading a data base called the @dfn{makefile}.
952 @menu
953 * Makefile Contents::           What makefiles contain.
954 * Makefile Names::              How to name your makefile.
955 * Include::                     How one makefile can use another makefile.
956 * MAKEFILES Variable::          The environment can specify extra makefiles.
957 * MAKEFILE_LIST Variable::      Discover which makefiles have been read.
958 * Special Variables::           Other special variables.
959 * Remaking Makefiles::          How makefiles get remade.
960 * Overriding Makefiles::        How to override part of one makefile
961                                   with another makefile.
962 * Reading Makefiles::           How makefiles are parsed.
963 * Secondary Expansion::         How and when secondary expansion is performed.
964 @end menu
966 @node Makefile Contents, Makefile Names, Makefiles, Makefiles
967 @section What Makefiles Contain
969 Makefiles contain five kinds of things: @dfn{explicit rules},
970 @dfn{implicit rules}, @dfn{variable definitions}, @dfn{directives},
971 and @dfn{comments}.  Rules, variables, and directives are described at
972 length in later chapters.@refill
974 @itemize @bullet
975 @cindex rule, explicit, definition of
976 @cindex explicit rule, definition of
977 @item
978 An @dfn{explicit rule} says when and how to remake one or more files,
979 called the rule's @dfn{targets}.  It lists the other files that the
980 targets depend on, called the @dfn{prerequisites} of the target, and
981 may also give commands to use to create or update the targets.
982 @xref{Rules, ,Writing Rules}.
984 @cindex rule, implicit, definition of
985 @cindex implicit rule, definition of
986 @item
987 An @dfn{implicit rule} says when and how to remake a class of files
988 based on their names.  It describes how a target may depend on a file
989 with a name similar to the target and gives commands to create or
990 update such a target.  @xref{Implicit Rules, ,Using Implicit Rules}.
992 @cindex variable definition
993 @item
994 A @dfn{variable definition} is a line that specifies a text string
995 value for a variable that can be substituted into the text later.  The
996 simple makefile example shows a variable definition for @code{objects}
997 as a list of all object files (@pxref{Variables Simplify, , Variables
998 Make Makefiles Simpler}).
1000 @cindex directive
1001 @item
1002 A @dfn{directive} is a command for @code{make} to do something special while
1003 reading the makefile.  These include:
1005 @itemize @bullet
1006 @item
1007 Reading another makefile (@pxref{Include, ,Including Other Makefiles}).
1009 @item
1010 Deciding (based on the values of variables) whether to use or
1011 ignore a part of the makefile (@pxref{Conditionals, ,Conditional Parts of Makefiles}).
1013 @item
1014 Defining a variable from a verbatim string containing multiple lines
1015 (@pxref{Defining, ,Defining Variables Verbatim}).
1016 @end itemize
1018 @cindex comments, in makefile
1019 @cindex @code{#} (comments), in makefile
1020 @item
1021 @samp{#} in a line of a makefile starts a @dfn{comment}.  It and the
1022 rest of the line are ignored, except that a trailing backslash not
1023 escaped by another backslash will continue the comment across multiple
1024 lines.  A line containing just a comment (with perhaps spaces before
1025 it) is effectively blank, and is ignored.  If you want a literal
1026 @code{#}, escape it with a backslash (e.g., @code{\#}).  Comments may
1027 appear on any line in the makefile, although they are treated
1028 specially in certain situations.
1030 Within a command script (if the line begins with a TAB character) the
1031 entire line is passed to the shell, just as with any other line that
1032 begins with a TAB.  The shell decides how to interpret the text:
1033 whether or not this is a comment is up to the shell.
1035 Within a @code{define} directive, comments are not ignored during the
1036 definition of the variable, but rather kept intact in the value of the
1037 variable.  When the variable is expanded they will either be treated
1038 as @code{make} comments or as command script text, depending on the
1039 context in which the variable is evaluated.
1040 @end itemize
1042 @node Makefile Names, Include, Makefile Contents, Makefiles
1043 @section What Name to Give Your Makefile
1044 @cindex makefile name
1045 @cindex name of makefile
1046 @cindex default makefile name
1047 @cindex file name of makefile
1049 @c following paragraph rewritten to avoid overfull hbox
1050 By default, when @code{make} looks for the makefile, it tries the
1051 following names, in order: @file{GNUmakefile}, @file{makefile}
1052 and @file{Makefile}.@refill
1053 @findex Makefile
1054 @findex GNUmakefile
1055 @findex makefile
1057 @cindex @code{README}
1058 Normally you should call your makefile either @file{makefile} or
1059 @file{Makefile}.  (We recommend @file{Makefile} because it appears
1060 prominently near the beginning of a directory listing, right near other
1061 important files such as @file{README}.)  The first name checked,
1062 @file{GNUmakefile}, is not recommended for most makefiles.  You should
1063 use this name if you have a makefile that is specific to GNU
1064 @code{make}, and will not be understood by other versions of
1065 @code{make}.  Other @code{make} programs look for @file{makefile} and
1066 @file{Makefile}, but not @file{GNUmakefile}.
1068 If @code{make} finds none of these names, it does not use any makefile.
1069 Then you must specify a goal with a command argument, and @code{make}
1070 will attempt to figure out how to remake it using only its built-in
1071 implicit rules.  @xref{Implicit Rules, ,Using Implicit Rules}.
1073 @cindex @code{-f}
1074 @cindex @code{--file}
1075 @cindex @code{--makefile}
1076 If you want to use a nonstandard name for your makefile, you can specify
1077 the makefile name with the @samp{-f} or @samp{--file} option.  The
1078 arguments @w{@samp{-f @var{name}}} or @w{@samp{--file=@var{name}}} tell
1079 @code{make} to read the file @var{name} as the makefile.  If you use
1080 more than one @samp{-f} or @samp{--file} option, you can specify several
1081 makefiles.  All the makefiles are effectively concatenated in the order
1082 specified.  The default makefile names @file{GNUmakefile},
1083 @file{makefile} and @file{Makefile} are not checked automatically if you
1084 specify @samp{-f} or @samp{--file}.@refill
1085 @cindex specifying makefile name
1086 @cindex makefile name, how to specify
1087 @cindex name of makefile, how to specify
1088 @cindex file name of makefile, how to specify
1090 @node Include, MAKEFILES Variable, Makefile Names, Makefiles
1091 @section Including Other Makefiles
1092 @cindex including other makefiles
1093 @cindex makefile, including
1095 @findex include
1096 The @code{include} directive tells @code{make} to suspend reading the
1097 current makefile and read one or more other makefiles before continuing.
1098 The directive is a line in the makefile that looks like this:
1100 @example
1101 include @var{filenames}@dots{}
1102 @end example
1104 @noindent
1105 @var{filenames} can contain shell file name patterns.  If
1106 @var{filenames} is empty, nothing is included and no error is printed.
1107 @cindex shell file name pattern (in @code{include})
1108 @cindex shell wildcards (in @code{include})
1109 @cindex wildcard, in @code{include}
1111 Extra spaces are allowed and ignored at the beginning of the line, but
1112 a tab is not allowed.  (If the line begins with a tab, it will be
1113 considered a command line.)  Whitespace is required between
1114 @code{include} and the file names, and between file names; extra
1115 whitespace is ignored there and at the end of the directive.  A
1116 comment starting with @samp{#} is allowed at the end of the line.  If
1117 the file names contain any variable or function references, they are
1118 expanded.  @xref{Using Variables, ,How to Use Variables}.
1120 For example, if you have three @file{.mk} files, @file{a.mk},
1121 @file{b.mk}, and @file{c.mk}, and @code{$(bar)} expands to
1122 @code{bish bash}, then the following expression
1124 @example
1125 include foo *.mk $(bar)
1126 @end example
1128 is equivalent to
1130 @example
1131 include foo a.mk b.mk c.mk bish bash
1132 @end example
1134 When @code{make} processes an @code{include} directive, it suspends
1135 reading of the containing makefile and reads from each listed file in
1136 turn.  When that is finished, @code{make} resumes reading the
1137 makefile in which the directive appears.
1139 One occasion for using @code{include} directives is when several programs,
1140 handled by individual makefiles in various directories, need to use a
1141 common set of variable definitions
1142 (@pxref{Setting, ,Setting Variables}) or pattern rules
1143 (@pxref{Pattern Rules, ,Defining and Redefining Pattern Rules}).
1145 Another such occasion is when you want to generate prerequisites from
1146 source files automatically; the prerequisites can be put in a file that
1147 is included by the main makefile.  This practice is generally cleaner
1148 than that of somehow appending the prerequisites to the end of the main
1149 makefile as has been traditionally done with other versions of
1150 @code{make}.  @xref{Automatic Prerequisites}.
1151 @cindex prerequisites, automatic generation
1152 @cindex automatic generation of prerequisites
1153 @cindex generating prerequisites automatically
1155 @cindex @code{-I}
1156 @cindex @code{--include-dir}
1157 @cindex included makefiles, default directories
1158 @cindex default directories for included makefiles
1159 @findex /usr/gnu/include
1160 @findex /usr/local/include
1161 @findex /usr/include
1162 If the specified name does not start with a slash, and the file is not
1163 found in the current directory, several other directories are searched.
1164 First, any directories you have specified with the @samp{-I} or
1165 @samp{--include-dir} option are searched
1166 (@pxref{Options Summary, ,Summary of Options}).
1167 Then the following directories (if they exist)
1168 are searched, in this order:
1169 @file{@var{prefix}/include} (normally @file{/usr/local/include}
1170 @footnote{GNU Make compiled for MS-DOS and MS-Windows behaves as if
1171 @var{prefix} has been defined to be the root of the DJGPP tree
1172 hierarchy.})
1173 @file{/usr/gnu/include},
1174 @file{/usr/local/include}, @file{/usr/include}.
1176 If an included makefile cannot be found in any of these directories, a
1177 warning message is generated, but it is not an immediately fatal error;
1178 processing of the makefile containing the @code{include} continues.
1179 Once it has finished reading makefiles, @code{make} will try to remake
1180 any that are out of date or don't exist.
1181 @xref{Remaking Makefiles, ,How Makefiles Are Remade}.
1182 Only after it has tried to find a way to remake a makefile and failed,
1183 will @code{make} diagnose the missing makefile as a fatal error.
1185 If you want @code{make} to simply ignore a makefile which does not exist
1186 and cannot be remade, with no error message, use the @w{@code{-include}}
1187 directive instead of @code{include}, like this:
1189 @example
1190 -include @var{filenames}@dots{}
1191 @end example
1193 This acts like @code{include} in every way except that there is no
1194 error (not even a warning) if any of the @var{filenames} do not exist.
1195 For compatibility with some other @code{make} implementations,
1196 @code{sinclude} is another name for @w{@code{-include}}.
1198 @node MAKEFILES Variable, MAKEFILE_LIST Variable, Include, Makefiles
1199 @section The Variable @code{MAKEFILES}
1200 @cindex makefile, and @code{MAKEFILES} variable
1201 @cindex including (@code{MAKEFILES} variable)
1203 @vindex MAKEFILES
1204 If the environment variable @code{MAKEFILES} is defined, @code{make}
1205 considers its value as a list of names (separated by whitespace) of
1206 additional makefiles to be read before the others.  This works much like
1207 the @code{include} directive: various directories are searched for those
1208 files (@pxref{Include, ,Including Other Makefiles}).  In addition, the
1209 default goal is never taken from one of these makefiles and it is not an
1210 error if the files listed in @code{MAKEFILES} are not found.@refill
1212 @cindex recursion, and @code{MAKEFILES} variable
1213 The main use of @code{MAKEFILES} is in communication between recursive
1214 invocations of @code{make} (@pxref{Recursion, ,Recursive Use of
1215 @code{make}}).  It usually is not desirable to set the environment
1216 variable before a top-level invocation of @code{make}, because it is
1217 usually better not to mess with a makefile from outside.  However, if
1218 you are running @code{make} without a specific makefile, a makefile in
1219 @code{MAKEFILES} can do useful things to help the built-in implicit
1220 rules work better, such as defining search paths (@pxref{Directory Search}).
1222 Some users are tempted to set @code{MAKEFILES} in the environment
1223 automatically on login, and program makefiles to expect this to be done.
1224 This is a very bad idea, because such makefiles will fail to work if run by
1225 anyone else.  It is much better to write explicit @code{include} directives
1226 in the makefiles.  @xref{Include, , Including Other Makefiles}.
1228 @node MAKEFILE_LIST Variable, Special Variables, MAKEFILES Variable, Makefiles
1229 @comment  node-name,  next,  previous,  up
1230 @section The Variable @code{MAKEFILE_LIST}
1231 @cindex makefiles, and @code{MAKEFILE_LIST} variable
1232 @cindex including (@code{MAKEFILE_LIST} variable)
1233 @vindex MAKEFILE_LIST
1235 As @code{make} reads various makefiles, including any obtained from the
1236 @code{MAKEFILES} variable, the command line, the default files, or
1237 from @code{include} directives, their names will be automatically
1238 appended to the @code{MAKEFILE_LIST} variable.  They are added right
1239 before @code{make} begins to parse them.
1241 This means that if the first thing a makefile does is examine the last
1242 word in this variable, it will be the name of the current makefile.
1243 Once the current makefile has used @code{include}, however, the last
1244 word will be the just-included makefile.
1246 If a makefile named @code{Makefile} has this content:
1248 @example
1249 @group
1250 name1 := $(lastword $(MAKEFILE_LIST))
1252 include inc.mk
1254 name2 := $(lastword $(MAKEFILE_LIST))
1256 all:
1257         @@echo name1 = $(name1)
1258         @@echo name2 = $(name2)
1259 @end group
1260 @end example
1262 @noindent
1263 then you would expect to see this output:
1265 @example
1266 @group
1267 name1 = Makefile
1268 name2 = inc.mk
1269 @end group
1270 @end example
1272 @xref{Text Functions}, for more information on the @code{word} and
1273 @code{words} functions used above.  @xref{Flavors, The Two Flavors of
1274 Variables}, for more information on simply-expanded (@code{:=})
1275 variable definitions.
1277 @node Special Variables, Remaking Makefiles, MAKEFILE_LIST Variable, Makefiles
1278 @comment  node-name,  next,  previous,  up
1279 @section Other Special Variables
1280 @cindex makefiles, and special variables
1281 @cindex special variables
1283 GNU @code{make} also supports other special variables.  Unless
1284 otherwise documented here, these values lose their special properties
1285 if they are set by a makefile or on the command line.
1287 @table @code
1289 @vindex .DEFAULT_GOAL @r{(define default goal)}
1290 @item .DEFAULT_GOAL
1291 Sets the default goal to be used if no targets were specified on the
1292 command line (@pxref{Goals, , Arguments to Specify the Goals}).  The
1293 @code{.DEFAULT_GOAL} variable allows you to discover the current
1294 default goal, restart the default goal selection algorithm by clearing
1295 its value, or to explicitly set the default goal.  The following
1296 example illustrates these cases:
1298 @example
1299 @group
1300 # Query the default goal.
1301 ifeq ($(.DEFAULT_GOAL),)
1302   $(warning no default goal is set)
1303 endif
1305 .PHONY: foo
1306 foo: ; @@echo $@@
1308 $(warning default goal is $(.DEFAULT_GOAL))
1310 # Reset the default goal.
1311 .DEFAULT_GOAL :=
1313 .PHONY: bar
1314 bar: ; @@echo $@@
1316 $(warning default goal is $(.DEFAULT_GOAL))
1318 # Set our own.
1319 .DEFAULT_GOAL := foo
1320 @end group
1321 @end example
1323 This makefile prints:
1325 @example
1326 @group
1327 no default goal is set
1328 default goal is foo
1329 default goal is bar
1331 @end group
1332 @end example
1334 Note that assigning more than one target name to @code{.DEFAULT_GOAL} is
1335 illegal and will result in an error.
1337 @vindex MAKE_RESTARTS @r{(number of times @code{make} has restarted)}
1338 @item MAKE_RESTARTS
1339 This variable is set only if this instance of @code{make} has
1340 restarted (@pxref{Remaking Makefiles, , How Makefiles Are Remade}): it
1341 will contain the number of times this instance has restarted.  Note
1342 this is not the same as recursion (counted by the @code{MAKELEVEL}
1343 variable).  You should not set, modify, or export this variable.
1345 @vindex .VARIABLES @r{(list of variables)}
1346 @item .VARIABLES
1347 Expands to a list of the @emph{names} of all global variables defined
1348 so far.  This includes variables which have empty values, as well as
1349 built-in variables (@pxref{Implicit Variables, , Variables Used by
1350 Implicit Rules}), but does not include any variables which are only
1351 defined in a target-specific context.  Note that any value you assign
1352 to this variable will be ignored; it will always return its special
1353 value.
1355 @c @vindex .TARGETS @r{(list of targets)}
1356 @c @item .TARGETS
1357 @c The second special variable is @code{.TARGETS}.  When expanded, the
1358 @c value consists of a list of all targets defined in all makefiles read
1359 @c up until that point.  Note it's not enough for a file to be simply
1360 @c mentioned in the makefile to be listed in this variable, even if it
1361 @c would match an implicit rule and become an ``implicit target''.  The
1362 @c file must appear as a target, on the left-hand side of a ``:'', to be
1363 @c considered a target for the purposes of this variable.
1365 @vindex .FEATURES @r{(list of supported features)}
1366 @item .FEATURES
1367 Expands to a list of special features supported by this version of
1368 @code{make}.  Possible values include:
1370 @table @samp
1371 @item target-specific
1372 Supports target-specific and pattern-specific variable assignments.
1373 @xref{Target-specific, ,Target-specific Variable Values}.
1375 @item order-only
1376 Supports order-only prerequisites.  @xref{Prerequisite Types, ,Types
1377 of Prerequisites}.
1379 @item second-expansion
1380 Supports secondary expansion of prerequisite lists.
1382 @item jobserver
1383 Supports ``job server'' enhanced parallel builds.  @xref{Parallel,
1384 ,Parallel Execution}.
1386 @item else-if
1387 Supports ``else if'' non-nested conditionals.  @xref{Conditional
1388 Syntax, ,Syntax of Conditionals}.
1390 @item check-symlink
1391 Supports the @code{-L} (@code{--check-symlink-times}) flag.
1392 @xref{Options Summary, ,Summary of Options}.
1394 @end table
1396 @vindex .INCLUDE_DIRS @r{(list of include directories)}
1397 @item .INCLUDE_DIRS
1398 Expands to a list of directories that @code{make} searches for
1399 included makefiles (@pxref{Include, , Including Other Makefiles}).
1401 @end table
1403 @node Remaking Makefiles, Overriding Makefiles, Special Variables, Makefiles
1404 @section How Makefiles Are Remade
1406 @cindex updating makefiles
1407 @cindex remaking makefiles
1408 @cindex makefile, remaking of
1409 Sometimes makefiles can be remade from other files, such as RCS or SCCS
1410 files.  If a makefile can be remade from other files, you probably want
1411 @code{make} to get an up-to-date version of the makefile to read in.
1413 To this end, after reading in all makefiles, @code{make} will consider
1414 each as a goal target and attempt to update it.  If a makefile has a
1415 rule which says how to update it (found either in that very makefile or
1416 in another one) or if an implicit rule applies to it (@pxref{Implicit
1417 Rules, ,Using Implicit Rules}), it will be updated if necessary.  After
1418 all makefiles have been checked, if any have actually been changed,
1419 @code{make} starts with a clean slate and reads all the makefiles over
1420 again.  (It will also attempt to update each of them over again, but
1421 normally this will not change them again, since they are already up to
1422 date.)@refill
1424 If you know that one or more of your makefiles cannot be remade and you
1425 want to keep @code{make} from performing an implicit rule search on
1426 them, perhaps for efficiency reasons, you can use any normal method of
1427 preventing implicit rule lookup to do so.  For example, you can write an
1428 explicit rule with the makefile as the target, and an empty command
1429 string (@pxref{Empty Commands, ,Using Empty Commands}).
1431 If the makefiles specify a double-colon rule to remake a file with
1432 commands but no prerequisites, that file will always be remade
1433 (@pxref{Double-Colon}).  In the case of makefiles, a makefile that has a
1434 double-colon rule with commands but no prerequisites will be remade every
1435 time @code{make} is run, and then again after @code{make} starts over
1436 and reads the makefiles in again.  This would cause an infinite loop:
1437 @code{make} would constantly remake the makefile, and never do anything
1438 else.  So, to avoid this, @code{make} will @strong{not} attempt to
1439 remake makefiles which are specified as targets of a double-colon rule
1440 with commands but no prerequisites.@refill
1442 If you do not specify any makefiles to be read with @samp{-f} or
1443 @samp{--file} options, @code{make} will try the default makefile names;
1444 @pxref{Makefile Names, ,What Name to Give Your Makefile}.  Unlike
1445 makefiles explicitly requested with @samp{-f} or @samp{--file} options,
1446 @code{make} is not certain that these makefiles should exist.  However,
1447 if a default makefile does not exist but can be created by running
1448 @code{make} rules, you probably want the rules to be run so that the
1449 makefile can be used.
1451 Therefore, if none of the default makefiles exists, @code{make} will try
1452 to make each of them in the same order in which they are searched for
1453 (@pxref{Makefile Names, ,What Name to Give Your Makefile})
1454 until it succeeds in making one, or it runs out of names to try.  Note
1455 that it is not an error if @code{make} cannot find or make any makefile;
1456 a makefile is not always necessary.@refill
1458 When you use the @samp{-t} or @samp{--touch} option
1459 (@pxref{Instead of Execution, ,Instead of Executing the Commands}),
1460 you would not want to use an out-of-date makefile to decide which
1461 targets to touch.  So the @samp{-t} option has no effect on updating
1462 makefiles; they are really updated even if @samp{-t} is specified.
1463 Likewise, @samp{-q} (or @samp{--question}) and @samp{-n} (or
1464 @samp{--just-print}) do not prevent updating of makefiles, because an
1465 out-of-date makefile would result in the wrong output for other targets.
1466 Thus, @samp{make -f mfile -n foo} will update @file{mfile}, read it in,
1467 and then print the commands to update @file{foo} and its prerequisites
1468 without running them.  The commands printed for @file{foo} will be those
1469 specified in the updated contents of @file{mfile}.
1471 However, on occasion you might actually wish to prevent updating of even
1472 the makefiles.  You can do this by specifying the makefiles as goals in
1473 the command line as well as specifying them as makefiles.  When the
1474 makefile name is specified explicitly as a goal, the options @samp{-t}
1475 and so on do apply to them.
1477 Thus, @samp{make -f mfile -n mfile foo} would read the makefile
1478 @file{mfile}, print the commands needed to update it without actually
1479 running them, and then print the commands needed to update @file{foo}
1480 without running them.  The commands for @file{foo} will be those
1481 specified by the existing contents of @file{mfile}.
1483 @node Overriding Makefiles, Reading Makefiles, Remaking Makefiles, Makefiles
1484 @section Overriding Part of Another Makefile
1486 @cindex overriding makefiles
1487 @cindex makefile, overriding
1488 Sometimes it is useful to have a makefile that is mostly just like
1489 another makefile.  You can often use the @samp{include} directive to
1490 include one in the other, and add more targets or variable definitions.
1491 However, if the two makefiles give different commands for the same
1492 target, @code{make} will not let you just do this.  But there is another way.
1494 @cindex match-anything rule, used to override
1495 In the containing makefile (the one that wants to include the other),
1496 you can use a match-anything pattern rule to say that to remake any
1497 target that cannot be made from the information in the containing
1498 makefile, @code{make} should look in another makefile.
1499 @xref{Pattern Rules}, for more information on pattern rules.
1501 For example, if you have a makefile called @file{Makefile} that says how
1502 to make the target @samp{foo} (and other targets), you can write a
1503 makefile called @file{GNUmakefile} that contains:
1505 @example
1506 foo:
1507         frobnicate > foo
1509 %: force
1510         @@$(MAKE) -f Makefile $@@
1511 force: ;
1512 @end example
1514 If you say @samp{make foo}, @code{make} will find @file{GNUmakefile},
1515 read it, and see that to make @file{foo}, it needs to run the command
1516 @samp{frobnicate > foo}.  If you say @samp{make bar}, @code{make} will
1517 find no way to make @file{bar} in @file{GNUmakefile}, so it will use the
1518 commands from the pattern rule: @samp{make -f Makefile bar}.  If
1519 @file{Makefile} provides a rule for updating @file{bar}, @code{make}
1520 will apply the rule.  And likewise for any other target that
1521 @file{GNUmakefile} does not say how to make.
1523 The way this works is that the pattern rule has a pattern of just
1524 @samp{%}, so it matches any target whatever.  The rule specifies a
1525 prerequisite @file{force}, to guarantee that the commands will be run even
1526 if the target file already exists.  We give @file{force} target empty
1527 commands to prevent @code{make} from searching for an implicit rule to
1528 build it---otherwise it would apply the same match-anything rule to
1529 @file{force} itself and create a prerequisite loop!
1531 @node Reading Makefiles,  Secondary Expansion, Overriding Makefiles, Makefiles
1532 @section How @code{make} Reads a Makefile
1533 @cindex reading makefiles
1534 @cindex makefile, parsing
1536 GNU @code{make} does its work in two distinct phases.  During the first
1537 phase it reads all the makefiles, included makefiles, etc. and
1538 internalizes all the variables and their values, implicit and explicit
1539 rules, and constructs a dependency graph of all the targets and their
1540 prerequisites.  During the second phase, @code{make} uses these internal
1541 structures to determine what targets will need to be rebuilt and to
1542 invoke the rules necessary to do so.
1544 It's important to understand this two-phase approach because it has a
1545 direct impact on how variable and function expansion happens; this is
1546 often a source of some confusion when writing makefiles.  Here we will
1547 present a summary of the phases in which expansion happens for different
1548 constructs within the makefile.  We say that expansion is
1549 @dfn{immediate} if it happens during the first phase: in this case
1550 @code{make} will expand any variables or functions in that section of a
1551 construct as the makefile is parsed.  We say that expansion is
1552 @dfn{deferred} if expansion is not performed immediately.  Expansion of
1553 deferred construct is not performed until either the construct appears
1554 later in an immediate context, or until the second phase.
1556 You may not be familiar with some of these constructs yet.  You can
1557 reference this section as you become familiar with them, in later
1558 chapters.
1560 @subheading Variable Assignment
1561 @cindex +=, expansion
1562 @cindex =, expansion
1563 @cindex ?=, expansion
1564 @cindex +=, expansion
1565 @cindex define, expansion
1567 Variable definitions are parsed as follows:
1569 @example
1570 @var{immediate} = @var{deferred}
1571 @var{immediate} ?= @var{deferred}
1572 @var{immediate} := @var{immediate}
1573 @var{immediate} += @var{deferred} or @var{immediate}
1575 define @var{immediate}
1576   @var{deferred}
1577 endef
1578 @end example
1580 For the append operator, @samp{+=}, the right-hand side is considered
1581 immediate if the variable was previously set as a simple variable
1582 (@samp{:=}), and deferred otherwise.
1584 @subheading Conditional Statements
1585 @cindex ifdef, expansion
1586 @cindex ifeq, expansion
1587 @cindex ifndef, expansion
1588 @cindex ifneq, expansion
1590 All instances of conditional syntax are parsed immediately, in their
1591 entirety; this includes the @code{ifdef}, @code{ifeq}, @code{ifndef},
1592 and @code{ifneq} forms.  Of course this means that automatic variables
1593 cannot be used in conditional statements, as automatic variables are
1594 not set until the command script for that rule is invoked.  If you
1595 need to use automatic variables in a conditional you @emph{must} use
1596 shell conditional syntax, in your command script proper, for these
1597 tests, not @code{make} conditionals.
1599 @subheading Rule Definition
1600 @cindex target, expansion
1601 @cindex prerequisite, expansion
1602 @cindex implicit rule, expansion
1603 @cindex pattern rule, expansion
1604 @cindex explicit rule, expansion
1606 A rule is always expanded the same way, regardless of the form:
1608 @example
1609 @var{immediate} : @var{immediate} ; @var{deferred}
1610         @var{deferred}
1611 @end example
1613 That is, the target and prerequisite sections are expanded immediately,
1614 and the commands used to construct the target are always deferred.  This
1615 general rule is true for explicit rules, pattern rules, suffix rules,
1616 static pattern rules, and simple prerequisite definitions.
1618 @node Secondary Expansion, , Reading Makefiles, Makefiles
1619 @section Secondary Expansion
1620 @cindex secondary expansion
1621 @cindex expansion, secondary
1623 @findex .SECONDEXPANSION
1624 In the previous section we learned that GNU @code{make} works in two
1625 distinct phases: a read-in phase and a target-update phase
1626 (@pxref{Reading Makefiles, , How @code{make} Reads a Makefile}).  GNU
1627 make also has the ability to enable a @emph{second expansion} of the
1628 prerequisites (only) for some or all targets defined in the makefile.
1629 In order for this second expansion to occur, the special target
1630 @code{.SECONDEXPANSION} must be defined before the first prerequisite
1631 list that makes use of this feature.
1633 If that special target is defined then in between the two phases
1634 mentioned above, right at the end of the read-in phase, all the
1635 prerequisites of the targets defined after the special target are
1636 expanded a @emph{second time}.  In most circumstances this secondary
1637 expansion will have no effect, since all variable and function
1638 references will have been expanded during the initial parsing of the
1639 makefiles.  In order to take advantage of the secondary expansion
1640 phase of the parser, then, it's necessary to @emph{escape} the
1641 variable or function reference in the makefile.  In this case the
1642 first expansion merely un-escapes the reference but doesn't expand it,
1643 and expansion is left to the secondary expansion phase.  For example,
1644 consider this makefile:
1646 @example
1647 .SECONDEXPANSION:
1648 ONEVAR = onefile
1649 TWOVAR = twofile
1650 myfile: $(ONEVAR) $$(TWOVAR)
1651 @end example
1653 After the first expansion phase the prerequisites list of the
1654 @file{myfile} target will be @code{onefile} and @code{$(TWOVAR)}; the
1655 first (unescaped) variable reference to @var{ONEVAR} is expanded,
1656 while the second (escaped) variable reference is simply unescaped,
1657 without being recognized as a variable reference.  Now during the
1658 secondary expansion the first word is expanded again but since it
1659 contains no variable or function references it remains the static
1660 value @file{onefile}, while the second word is now a normal reference
1661 to the variable @var{TWOVAR}, which is expanded to the value
1662 @file{twofile}.  The final result is that there are two prerequisites,
1663 @file{onefile} and @file{twofile}.
1665 Obviously, this is not a very interesting case since the same result
1666 could more easily have been achieved simply by having both variables
1667 appear, unescaped, in the prerequisites list.  One difference becomes
1668 apparent if the variables are reset; consider this example:
1670 @example
1671 .SECONDEXPANSION:
1672 AVAR = top
1673 onefile: $(AVAR)
1674 twofile: $$(AVAR)
1675 AVAR = bottom
1676 @end example
1678 Here the prerequisite of @file{onefile} will be expanded immediately,
1679 and resolve to the value @file{top}, while the prerequisite of
1680 @file{twofile} will not be full expanded until the secondary expansion
1681 and yield a value of @file{bottom}.
1683 This is marginally more exciting, but the true power of this feature
1684 only becomes apparent when you discover that secondary expansions
1685 always take place within the scope of the automatic variables for that
1686 target.  This means that you can use variables such as @code{$@@},
1687 @code{$*}, etc. during the second expansion and they will have their
1688 expected values, just as in the command script.  All you have to do is
1689 defer the expansion by escaping the @code{$}.  Also, secondary
1690 expansion occurs for both explicit and implicit (pattern) rules.
1691 Knowing this, the possible uses for this feature increase
1692 dramatically.  For example:
1694 @example
1695 .SECONDEXPANSION:
1696 main_OBJS := main.o try.o test.o
1697 lib_OBJS := lib.o api.o
1699 main lib: $$($$@@_OBJS)
1700 @end example
1702 Here, after the initial expansion the prerequisites of both the
1703 @file{main} and @file{lib} targets will be @code{$($@@_OBJS)}.  During
1704 the secondary expansion, the @code{$@@} variable is set to the name of
1705 the target and so the expansion for the @file{main} target will yield
1706 @code{$(main_OBJS)}, or @code{main.o try.o test.o}, while the
1707 secondary expansion for the @file{lib} target will yield
1708 @code{$(lib_OBJS)}, or @code{lib.o api.o}.
1710 You can also mix functions here, as long as they are properly escaped:
1712 @example
1713 main_SRCS := main.c try.c test.c
1714 lib_SRCS := lib.c api.c
1716 .SECONDEXPANSION:
1717 main lib: $$(patsubst %.c,%.o,$$($$@@_SRCS))
1718 @end example
1720 This version allows users to specify source files rather than object
1721 files, but gives the same resulting prerequisites list as the previous
1722 example.
1724 Evaluation of automatic variables during the secondary expansion
1725 phase, especially of the target name variable @code{$$@@}, behaves
1726 similarly to evaluation within command scripts.  However, there are
1727 some subtle differences and ``corner cases'' which come into play for
1728 the different types of rule definitions that @code{make} understands.
1729 The subtleties of using the different automatic variables are
1730 described below.
1732 @subheading Secondary Expansion of Explicit Rules
1733 @cindex secondary expansion and explicit rules
1734 @cindex explicit rules, secondary expansion of
1736 During the secondary expansion of explicit rules, @code{$$@@} and
1737 @code{$$%} evaluate, respectively, to the file name of the target and,
1738 when the target is an archive member, the target member name.  The
1739 @code{$$<} variable evaluates to the first prerequisite in the first
1740 rule for this target.  @code{$$^} and @code{$$+} evaluate to the list
1741 of all prerequisites of rules @emph{that have already appeared} for
1742 the same target (@code{$$+} with repetitions and @code{$$^}
1743 without).  The following example will help illustrate these behaviors:
1745 @example
1746 .SECONDEXPANSION:
1748 foo: foo.1 bar.1 $$< $$^ $$+    # line #1
1750 foo: foo.2 bar.2 $$< $$^ $$+    # line #2
1752 foo: foo.3 bar.3 $$< $$^ $$+    # line #3
1753 @end example
1755 In the first prerequisite list, all three variables (@code{$$<},
1756 @code{$$^}, and @code{$$+}) expand to the empty string.  In the
1757 second, they will have values @code{foo.1}, @code{foo.1 bar.1}, and
1758 @code{foo.1 bar.1} respectively.  In the third they will have values
1759 @code{foo.1}, @code{foo.1 bar.1 foo.2 bar.2}, and @code{foo.1 bar.1
1760 foo.2 bar.2} respectively.
1762 Rules undergo secondary expansion in makefile order, except that
1763 the rule with the command script is always evaluated last.
1765 The variables @code{$$?} and @code{$$*} are not available and expand
1766 to the empty string.
1768 @subheading Secondary Expansion of Static Pattern Rules
1769 @cindex secondary expansion and static pattern rules
1770 @cindex static pattern rules, secondary expansion of
1772 Rules for secondary expansion of static pattern rules are identical to
1773 those for explicit rules, above, with one exception: for static
1774 pattern rules the @code{$$*} variable is set to the pattern stem.  As
1775 with explicit rules, @code{$$?} is not available and expands to the
1776 empty string.
1778 @subheading Secondary Expansion of Implicit Rules
1779 @cindex secondary expansion and implicit rules
1780 @cindex implicit rules, secondary expansion of
1782 As @code{make} searches for an implicit rule, it substitutes the stem
1783 and then performs secondary expansion for every rule with a matching
1784 target pattern.  The value of the automatic variables is derived in
1785 the same fashion as for static pattern rules.  As an example:
1787 @example
1788 .SECONDEXPANSION:
1790 foo: bar
1792 foo foz: fo%: bo%
1794 %oo: $$< $$^ $$+ $$*
1795 @end example
1797 When the implicit rule is tried for target @file{foo}, @code{$$<}
1798 expands to @file{bar}, @code{$$^} expands to @file{bar boo},
1799 @code{$$+} also expands to @file{bar boo}, and @code{$$*} expands to
1800 @file{f}.
1802 Note that the directory prefix (D), as described in @ref{Implicit Rule
1803 Search, ,Implicit Rule Search Algorithm}, is appended (after
1804 expansion) to all the patterns in the prerequisites list.  As an
1805 example:
1807 @example
1808 .SECONDEXPANSION:
1810 /tmp/foo.o:
1812 %.o: $$(addsuffix /%.c,foo bar) foo.h
1813 @end example
1815 The prerequisite list after the secondary expansion and directory
1816 prefix reconstruction will be @file{/tmp/foo/foo.c /tmp/var/bar/foo.c
1817 foo.h}.  If you are not interested in this reconstruction, you can use
1818 @code{$$*} instead of @code{%} in the prerequisites list.
1820 @node Rules, Commands, Makefiles, Top
1821 @chapter Writing Rules
1822 @cindex writing rules
1823 @cindex rule, how to write
1824 @cindex target
1825 @cindex prerequisite
1827 A @dfn{rule} appears in the makefile and says when and how to remake
1828 certain files, called the rule's @dfn{targets} (most often only one per rule).
1829 It lists the other files that are the @dfn{prerequisites} of the target, and
1830 @dfn{commands} to use to create or update the target.
1832 @cindex default goal
1833 @cindex goal, default
1834 The order of rules is not significant, except for determining the
1835 @dfn{default goal}: the target for @code{make} to consider, if you do
1836 not otherwise specify one.  The default goal is the target of the first
1837 rule in the first makefile.  If the first rule has multiple targets,
1838 only the first target is taken as the default.  There are two
1839 exceptions: a target starting with a period is not a default unless it
1840 contains one or more slashes, @samp{/}, as well; and, a target that
1841 defines a pattern rule has no effect on the default goal.
1842 (@xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.)
1844 Therefore, we usually write the makefile so that the first rule is the
1845 one for compiling the entire program or all the programs described by
1846 the makefile (often with a target called @samp{all}).
1847 @xref{Goals, ,Arguments to Specify the Goals}.
1849 @menu
1850 * Rule Example::                An example explained.
1851 * Rule Syntax::                 General syntax explained.
1852 * Prerequisite Types::          There are two types of prerequisites.
1853 * Wildcards::                   Using wildcard characters such as `*'.
1854 * Directory Search::            Searching other directories for source files.
1855 * Phony Targets::               Using a target that is not a real file's name.
1856 * Force Targets::               You can use a target without commands
1857                                   or prerequisites to mark other targets
1858                                   as phony.
1859 * Empty Targets::               When only the date matters and the
1860                                   files are empty.
1861 * Special Targets::             Targets with special built-in meanings.
1862 * Multiple Targets::            When to make use of several targets in a rule.
1863 * Multiple Rules::              How to use several rules with the same target.
1864 * Static Pattern::              Static pattern rules apply to multiple targets
1865                                   and can vary the prerequisites according to
1866                                   the target name.
1867 * Double-Colon::                How to use a special kind of rule to allow
1868                                   several independent rules for one target.
1869 * Automatic Prerequisites::     How to automatically generate rules giving
1870                                   prerequisites from source files themselves.
1871 @end menu
1873 @ifnottex
1874 @node Rule Example, Rule Syntax, Rules, Rules
1875 @section Rule Example
1877 Here is an example of a rule:
1879 @example
1880 foo.o : foo.c defs.h       # module for twiddling the frobs
1881         cc -c -g foo.c
1882 @end example
1884 Its target is @file{foo.o} and its prerequisites are @file{foo.c} and
1885 @file{defs.h}.  It has one command, which is @samp{cc -c -g foo.c}.
1886 The command line starts with a tab to identify it as a command.
1888 This rule says two things:
1890 @itemize @bullet
1891 @item
1892 How to decide whether @file{foo.o} is out of date: it is out of date
1893 if it does not exist, or if either @file{foo.c} or @file{defs.h} is
1894 more recent than it.
1896 @item
1897 How to update the file @file{foo.o}: by running @code{cc} as stated.
1898 The command does not explicitly mention @file{defs.h}, but we presume
1899 that @file{foo.c} includes it, and that that is why @file{defs.h} was
1900 added to the prerequisites.
1901 @end itemize
1902 @end ifnottex
1904 @node Rule Syntax, Prerequisite Types, Rule Example, Rules
1905 @section Rule Syntax
1907 @cindex rule syntax
1908 @cindex syntax of rules
1909 In general, a rule looks like this:
1911 @example
1912 @var{targets} : @var{prerequisites}
1913         @var{command}
1914         @dots{}
1915 @end example
1917 @noindent
1918 or like this:
1920 @example
1921 @var{targets} : @var{prerequisites} ; @var{command}
1922         @var{command}
1923         @dots{}
1924 @end example
1926 @cindex targets
1927 @cindex rule targets
1928 The @var{targets} are file names, separated by spaces.  Wildcard
1929 characters may be used (@pxref{Wildcards, ,Using Wildcard Characters
1930 in File Names}) and a name of the form @file{@var{a}(@var{m})}
1931 represents member @var{m} in archive file @var{a}
1932 (@pxref{Archive Members, ,Archive Members as Targets}).
1933 Usually there is only one
1934 target per rule, but occasionally there is a reason to have more
1935 (@pxref{Multiple Targets, , Multiple Targets in a Rule}).@refill
1937 @cindex commands
1938 @cindex tab character (in commands)
1939 The @var{command} lines start with a tab character.  The first command may
1940 appear on the line after the prerequisites, with a tab character, or may
1941 appear on the same line, with a semicolon.  Either way, the effect is the
1942 same.  There are other differences in the syntax of command lines.
1943 @xref{Commands, ,Writing the Commands in Rules}.
1945 @cindex dollar sign (@code{$}), in rules
1946 @cindex @code{$}, in rules
1947 @cindex rules, and @code{$}
1948 Because dollar signs are used to start @code{make} variable
1949 references, if you really want a dollar sign in a target or
1950 prerequisite you must write two of them, @samp{$$} (@pxref{Using
1951 Variables, ,How to Use Variables}).  If you have enabled secondary
1952 expansion (@pxref{Secondary Expansion}) and you want a literal dollar
1953 sign in the prerequisites lise, you must actually write @emph{four}
1954 dollar signs (@samp{$$$$}).
1956 You may split a long line by inserting a backslash followed by a
1957 newline, but this is not required, as @code{make} places no limit on
1958 the length of a line in a makefile.
1960 A rule tells @code{make} two things: when the targets are out of date,
1961 and how to update them when necessary.
1963 @cindex prerequisites
1964 @cindex rule prerequisites
1965 The criterion for being out of date is specified in terms of the
1966 @var{prerequisites}, which consist of file names separated by spaces.
1967 (Wildcards and archive members (@pxref{Archives}) are allowed here too.)
1968 A target is out of date if it does not exist or if it is older than any
1969 of the prerequisites (by comparison of last-modification times).  The
1970 idea is that the contents of the target file are computed based on
1971 information in the prerequisites, so if any of the prerequisites changes,
1972 the contents of the existing target file are no longer necessarily
1973 valid.
1975 How to update is specified by @var{commands}.  These are lines to be
1976 executed by the shell (normally @samp{sh}), but with some extra features
1977 (@pxref{Commands, ,Writing the Commands in Rules}).
1979 @node Prerequisite Types, Wildcards, Rule Syntax, Rules
1980 @comment  node-name,  next,  previous,  up
1981 @section Types of Prerequisites
1982 @cindex prerequisite types
1983 @cindex types of prerequisites
1985 @cindex prerequisites, normal
1986 @cindex normal prerequisites
1987 @cindex prerequisites, order-only
1988 @cindex order-only prerequisites
1989 There are actually two different types of prerequisites understood by
1990 GNU @code{make}: normal prerequisites such as described in the
1991 previous section, and @dfn{order-only} prerequisites.  A normal
1992 prerequisite makes two statements: first, it imposes an order of
1993 execution of build commands: any commands necessary to build any of a
1994 target's prerequisites will be fully executed before any commands
1995 necessary to build the target.  Second, it imposes a dependency
1996 relationship: if any prerequisite is newer than the target, then the
1997 target is considered out-of-date and must be rebuilt.
1999 Normally, this is exactly what you want: if a target's prerequisite is
2000 updated, then the target should also be updated.
2002 Occasionally, however, you have a situation where you want to impose a
2003 specific ordering on the rules to be invoked @emph{without} forcing
2004 the target to be updated if one of those rules is executed.  In that
2005 case, you want to define @dfn{order-only} prerequisites.  Order-only
2006 prerequisites can be specified by placing a pipe symbol (@code{|})
2007 in the prerequisites list: any prerequisites to the left of the pipe
2008 symbol are normal; any prerequisites to the right are order-only:
2010 @example
2011 @var{targets} : @var{normal-prerequisites} | @var{order-only-prerequisites}
2012 @end example
2014 The normal prerequisites section may of course be empty.  Also, you
2015 may still declare multiple lines of prerequisites for the same target:
2016 they are appended appropriately.  Note that if you declare the same
2017 file to be both a normal and an order-only prerequisite, the normal
2018 prerequisite takes precedence (since they are a strict superset of the
2019 behavior of an order-only prerequisite).
2021 @node Wildcards, Directory Search, Prerequisite Types, Rules
2022 @section Using Wildcard Characters in File Names
2023 @cindex wildcard
2024 @cindex file name with wildcards
2025 @cindex globbing (wildcards)
2027 @cindex @code{*} (wildcard character)
2028 @cindex @code{?} (wildcard character)
2029 @cindex @code{[@dots{}]} (wildcard characters)
2030 A single file name can specify many files using @dfn{wildcard characters}.
2031 The wildcard characters in @code{make} are @samp{*}, @samp{?} and
2032 @samp{[@dots{}]}, the same as in the Bourne shell.  For example, @file{*.c}
2033 specifies a list of all the files (in the working directory) whose names
2034 end in @samp{.c}.@refill
2036 @cindex @code{~} (tilde)
2037 @cindex tilde (@code{~})
2038 @cindex home directory
2039 The character @samp{~} at the beginning of a file name also has special
2040 significance.  If alone, or followed by a slash, it represents your home
2041 directory.  For example @file{~/bin} expands to @file{/home/you/bin}.
2042 If the @samp{~} is followed by a word, the string represents the home
2043 directory of the user named by that word.  For example @file{~john/bin}
2044 expands to @file{/home/john/bin}.  On systems which don't have a home
2045 directory for each user (such as MS-DOS or MS-Windows), this
2046 functionality can be simulated by setting the environment variable
2047 @var{HOME}.@refill
2049 Wildcard expansion happens automatically in targets, in prerequisites,
2050 and in commands (where the shell does the expansion).  In other
2051 contexts, wildcard expansion happens only if you request it explicitly
2052 with the @code{wildcard} function.
2054 The special significance of a wildcard character can be turned off by
2055 preceding it with a backslash.  Thus, @file{foo\*bar} would refer to a
2056 specific file whose name consists of @samp{foo}, an asterisk, and
2057 @samp{bar}.@refill
2059 @menu
2060 * Wildcard Examples::           Several examples
2061 * Wildcard Pitfall::            Problems to avoid.
2062 * Wildcard Function::           How to cause wildcard expansion where
2063                                   it does not normally take place.
2064 @end menu
2066 @node Wildcard Examples, Wildcard Pitfall, Wildcards, Wildcards
2067 @subsection Wildcard Examples
2069 Wildcards can be used in the commands of a rule, where they are expanded
2070 by the shell.  For example, here is a rule to delete all the object files:
2072 @example
2073 @group
2074 clean:
2075         rm -f *.o
2076 @end group
2077 @end example
2078 @cindex @code{rm} (shell command)
2080 Wildcards are also useful in the prerequisites of a rule.  With the
2081 following rule in the makefile, @samp{make print} will print all the
2082 @samp{.c} files that have changed since the last time you printed them:
2084 @example
2085 print: *.c
2086         lpr -p $?
2087         touch print
2088 @end example
2090 @cindex @code{print} target
2091 @cindex @code{lpr} (shell command)
2092 @cindex @code{touch} (shell command)
2093 @noindent
2094 This rule uses @file{print} as an empty target file; see @ref{Empty
2095 Targets, ,Empty Target Files to Record Events}.  (The automatic variable
2096 @samp{$?} is used to print only those files that have changed; see
2097 @ref{Automatic Variables}.)@refill
2099 Wildcard expansion does not happen when you define a variable.  Thus, if
2100 you write this:
2102 @example
2103 objects = *.o
2104 @end example
2106 @noindent
2107 then the value of the variable @code{objects} is the actual string
2108 @samp{*.o}.  However, if you use the value of @code{objects} in a target,
2109 prerequisite or command, wildcard expansion will take place at that time.
2110 To set @code{objects} to the expansion, instead use:
2112 @example
2113 objects := $(wildcard *.o)
2114 @end example
2116 @noindent
2117 @xref{Wildcard Function}.
2119 @node Wildcard Pitfall, Wildcard Function, Wildcard Examples, Wildcards
2120 @subsection Pitfalls of Using Wildcards
2121 @cindex wildcard pitfalls
2122 @cindex pitfalls of wildcards
2123 @cindex mistakes with wildcards
2124 @cindex errors with wildcards
2125 @cindex problems with wildcards
2127 Now here is an example of a naive way of using wildcard expansion, that
2128 does not do what you would intend.  Suppose you would like to say that the
2129 executable file @file{foo} is made from all the object files in the
2130 directory, and you write this:
2132 @example
2133 objects = *.o
2135 foo : $(objects)
2136         cc -o foo $(CFLAGS) $(objects)
2137 @end example
2139 @noindent
2140 The value of @code{objects} is the actual string @samp{*.o}.  Wildcard
2141 expansion happens in the rule for @file{foo}, so that each @emph{existing}
2142 @samp{.o} file becomes a prerequisite of @file{foo} and will be recompiled if
2143 necessary.
2145 But what if you delete all the @samp{.o} files?  When a wildcard matches
2146 no files, it is left as it is, so then @file{foo} will depend on the
2147 oddly-named file @file{*.o}.  Since no such file is likely to exist,
2148 @code{make} will give you an error saying it cannot figure out how to
2149 make @file{*.o}.  This is not what you want!
2151 Actually it is possible to obtain the desired result with wildcard
2152 expansion, but you need more sophisticated techniques, including the
2153 @code{wildcard} function and string substitution.
2154 @ifnottex
2155 @xref{Wildcard Function, ,The Function @code{wildcard}}.
2156 @end ifnottex
2157 @iftex
2158 These are described in the following section.
2159 @end iftex
2161 @cindex wildcards and MS-DOS/MS-Windows backslashes
2162 @cindex backslashes in pathnames and wildcard expansion
2164 Microsoft operating systems (MS-DOS and MS-Windows) use backslashes to
2165 separate directories in pathnames, like so:
2167 @example
2168   c:\foo\bar\baz.c
2169 @end example
2171 This is equivalent to the Unix-style @file{c:/foo/bar/baz.c} (the
2172 @file{c:} part is the so-called drive letter).  When @code{make} runs on
2173 these systems, it supports backslashes as well as the Unix-style forward
2174 slashes in pathnames.  However, this support does @emph{not} include the
2175 wildcard expansion, where backslash is a quote character.  Therefore,
2176 you @emph{must} use Unix-style slashes in these cases.
2179 @node Wildcard Function,  , Wildcard Pitfall, Wildcards
2180 @subsection The Function @code{wildcard}
2181 @findex wildcard
2183 Wildcard expansion happens automatically in rules.  But wildcard expansion
2184 does not normally take place when a variable is set, or inside the
2185 arguments of a function.  If you want to do wildcard expansion in such
2186 places, you need to use the @code{wildcard} function, like this:
2188 @example
2189 $(wildcard @var{pattern}@dots{})
2190 @end example
2192 @noindent
2193 This string, used anywhere in a makefile, is replaced by a
2194 space-separated list of names of existing files that match one of the
2195 given file name patterns.  If no existing file name matches a pattern,
2196 then that pattern is omitted from the output of the @code{wildcard}
2197 function.  Note that this is different from how unmatched wildcards
2198 behave in rules, where they are used verbatim rather than ignored
2199 (@pxref{Wildcard Pitfall}).
2201 One use of the @code{wildcard} function is to get a list of all the C source
2202 files in a directory, like this:
2204 @example
2205 $(wildcard *.c)
2206 @end example
2208 We can change the list of C source files into a list of object files by
2209 replacing the @samp{.c} suffix with @samp{.o} in the result, like this:
2211 @example
2212 $(patsubst %.c,%.o,$(wildcard *.c))
2213 @end example
2215 @noindent
2216 (Here we have used another function, @code{patsubst}.
2217 @xref{Text Functions, ,Functions for String Substitution and Analysis}.)@refill
2219 Thus, a makefile to compile all C source files in the directory and then
2220 link them together could be written as follows:
2222 @example
2223 objects := $(patsubst %.c,%.o,$(wildcard *.c))
2225 foo : $(objects)
2226         cc -o foo $(objects)
2227 @end example
2229 @noindent
2230 (This takes advantage of the implicit rule for compiling C programs, so
2231 there is no need to write explicit rules for compiling the files.
2232 @xref{Flavors, ,The Two Flavors of Variables}, for an explanation of
2233 @samp{:=}, which is a variant of @samp{=}.)
2235 @node Directory Search, Phony Targets, Wildcards, Rules
2236 @section Searching Directories for Prerequisites
2237 @vindex VPATH
2238 @findex vpath
2239 @cindex vpath
2240 @cindex search path for prerequisites (@code{VPATH})
2241 @cindex directory search (@code{VPATH})
2243 For large systems, it is often desirable to put sources in a separate
2244 directory from the binaries.  The @dfn{directory search} features of
2245 @code{make} facilitate this by searching several directories
2246 automatically to find a prerequisite.  When you redistribute the files
2247 among directories, you do not need to change the individual rules,
2248 just the search paths.
2250 @menu
2251 * General Search::              Specifying a search path that applies
2252                                   to every prerequisite.
2253 * Selective Search::            Specifying a search path
2254                                   for a specified class of names.
2255 * Search Algorithm::            When and how search paths are applied.
2256 * Commands/Search::             How to write shell commands that work together
2257                                   with search paths.
2258 * Implicit/Search::             How search paths affect implicit rules.
2259 * Libraries/Search::            Directory search for link libraries.
2260 @end menu
2262 @node General Search, Selective Search, Directory Search, Directory Search
2263 @subsection @code{VPATH}: Search Path for All Prerequisites
2264 @vindex VPATH
2266 The value of the @code{make} variable @code{VPATH} specifies a list of
2267 directories that @code{make} should search.  Most often, the
2268 directories are expected to contain prerequisite files that are not in the
2269 current directory; however, @code{make} uses @code{VPATH} as a search
2270 list for both prerequisites and targets of rules.
2272 Thus, if a file that is listed as a target or prerequisite does not exist
2273 in the current directory, @code{make} searches the directories listed in
2274 @code{VPATH} for a file with that name.  If a file is found in one of
2275 them, that file may become the prerequisite (see below).  Rules may then
2276 specify the names of files in the prerequisite list as if they all
2277 existed in the current directory.  @xref{Commands/Search, ,Writing Shell
2278 Commands with Directory Search}.
2280 In the @code{VPATH} variable, directory names are separated by colons or
2281 blanks.  The order in which directories are listed is the order followed
2282 by @code{make} in its search.  (On MS-DOS and MS-Windows, semi-colons
2283 are used as separators of directory names in @code{VPATH}, since the
2284 colon can be used in the pathname itself, after the drive letter.)
2286 For example,
2288 @example
2289 VPATH = src:../headers
2290 @end example
2292 @noindent
2293 specifies a path containing two directories, @file{src} and
2294 @file{../headers}, which @code{make} searches in that order.
2296 With this value of @code{VPATH}, the following rule,
2298 @example
2299 foo.o : foo.c
2300 @end example
2302 @noindent
2303 is interpreted as if it were written like this:
2305 @example
2306 foo.o : src/foo.c
2307 @end example
2309 @noindent
2310 assuming the file @file{foo.c} does not exist in the current directory but
2311 is found in the directory @file{src}.
2313 @node Selective Search, Search Algorithm, General Search, Directory Search
2314 @subsection The @code{vpath} Directive
2315 @findex vpath
2317 Similar to the @code{VPATH} variable, but more selective, is the
2318 @code{vpath} directive (note lower case), which allows you to specify a
2319 search path for a particular class of file names: those that match a
2320 particular pattern.  Thus you can supply certain search directories for
2321 one class of file names and other directories (or none) for other file
2322 names.
2324 There are three forms of the @code{vpath} directive:
2326 @table @code
2327 @item vpath @var{pattern} @var{directories}
2328 Specify the search path @var{directories} for file names that match
2329 @var{pattern}.
2331 The search path, @var{directories}, is a list of directories to be
2332 searched, separated by colons (semi-colons on MS-DOS and MS-Windows) or
2333 blanks, just like the search path used in the @code{VPATH} variable.
2335 @item vpath @var{pattern}
2336 Clear out the search path associated with @var{pattern}.
2338 @c Extra blank line makes sure this gets two lines.
2339 @item vpath
2341 Clear all search paths previously specified with @code{vpath} directives.
2342 @end table
2344 A @code{vpath} pattern is a string containing a @samp{%} character.  The
2345 string must match the file name of a prerequisite that is being searched
2346 for, the @samp{%} character matching any sequence of zero or more
2347 characters (as in pattern rules; @pxref{Pattern Rules, ,Defining and
2348 Redefining Pattern Rules}).  For example, @code{%.h} matches files that
2349 end in @code{.h}.  (If there is no @samp{%}, the pattern must match the
2350 prerequisite exactly, which is not useful very often.)
2352 @cindex @code{%}, quoting in @code{vpath}
2353 @cindex @code{%}, quoting with @code{\} (backslash)
2354 @cindex @code{\} (backslash), to quote @code{%}
2355 @cindex backslash (@code{\}), to quote @code{%}
2356 @cindex quoting @code{%}, in @code{vpath}
2357 @samp{%} characters in a @code{vpath} directive's pattern can be quoted
2358 with preceding backslashes (@samp{\}).  Backslashes that would otherwise
2359 quote @samp{%} characters can be quoted with more backslashes.
2360 Backslashes that quote @samp{%} characters or other backslashes are
2361 removed from the pattern before it is compared to file names.  Backslashes
2362 that are not in danger of quoting @samp{%} characters go unmolested.@refill
2364 When a prerequisite fails to exist in the current directory, if the
2365 @var{pattern} in a @code{vpath} directive matches the name of the
2366 prerequisite file, then the @var{directories} in that directive are searched
2367 just like (and before) the directories in the @code{VPATH} variable.
2369 For example,
2371 @example
2372 vpath %.h ../headers
2373 @end example
2375 @noindent
2376 tells @code{make} to look for any prerequisite whose name ends in @file{.h}
2377 in the directory @file{../headers} if the file is not found in the current
2378 directory.
2380 If several @code{vpath} patterns match the prerequisite file's name, then
2381 @code{make} processes each matching @code{vpath} directive one by one,
2382 searching all the directories mentioned in each directive.  @code{make}
2383 handles multiple @code{vpath} directives in the order in which they
2384 appear in the makefile; multiple directives with the same pattern are
2385 independent of each other.
2387 @need 750
2388 Thus,
2390 @example
2391 @group
2392 vpath %.c foo
2393 vpath %   blish
2394 vpath %.c bar
2395 @end group
2396 @end example
2398 @noindent
2399 will look for a file ending in @samp{.c} in @file{foo}, then
2400 @file{blish}, then @file{bar}, while
2402 @example
2403 @group
2404 vpath %.c foo:bar
2405 vpath %   blish
2406 @end group
2407 @end example
2409 @noindent
2410 will look for a file ending in @samp{.c} in @file{foo}, then
2411 @file{bar}, then @file{blish}.
2413 @node Search Algorithm, Commands/Search, Selective Search, Directory Search
2414 @subsection How Directory Searches are Performed
2415 @cindex algorithm for directory search
2416 @cindex directory search algorithm
2418 When a prerequisite is found through directory search, regardless of type
2419 (general or selective), the pathname located may not be the one that
2420 @code{make} actually provides you in the prerequisite list.  Sometimes
2421 the path discovered through directory search is thrown away.
2423 The algorithm @code{make} uses to decide whether to keep or abandon a
2424 path found via directory search is as follows:
2426 @enumerate
2427 @item
2428 If a target file does not exist at the path specified in the makefile,
2429 directory search is performed.
2431 @item
2432 If the directory search is successful, that path is kept and this file
2433 is tentatively stored as the target.
2435 @item
2436 All prerequisites of this target are examined using this same method.
2438 @item
2439 After processing the prerequisites, the target may or may not need to be
2440 rebuilt:
2442 @enumerate a
2443 @item
2444 If the target does @emph{not} need to be rebuilt, the path to the file
2445 found during directory search is used for any prerequisite lists which
2446 contain this target.  In short, if @code{make} doesn't need to rebuild
2447 the target then you use the path found via directory search.
2449 @item
2450 If the target @emph{does} need to be rebuilt (is out-of-date), the
2451 pathname found during directory search is @emph{thrown away}, and the
2452 target is rebuilt using the file name specified in the makefile.  In
2453 short, if @code{make} must rebuild, then the target is rebuilt locally,
2454 not in the directory found via directory search.
2455 @end enumerate
2456 @end enumerate
2458 This algorithm may seem complex, but in practice it is quite often
2459 exactly what you want.
2461 @cindex traditional directory search (GPATH)
2462 @cindex directory search, traditional (GPATH)
2463 Other versions of @code{make} use a simpler algorithm: if the file does
2464 not exist, and it is found via directory search, then that pathname is
2465 always used whether or not the target needs to be built.  Thus, if the
2466 target is rebuilt it is created at the pathname discovered during
2467 directory search.
2469 @vindex GPATH
2470 If, in fact, this is the behavior you want for some or all of your
2471 directories, you can use the @code{GPATH} variable to indicate this to
2472 @code{make}.
2474 @code{GPATH} has the same syntax and format as @code{VPATH} (that is, a
2475 space- or colon-delimited list of pathnames).  If an out-of-date target
2476 is found by directory search in a directory that also appears in
2477 @code{GPATH}, then that pathname is not thrown away.  The target is
2478 rebuilt using the expanded path.
2480 @node Commands/Search, Implicit/Search, Search Algorithm, Directory Search
2481 @subsection Writing Shell Commands with Directory Search
2482 @cindex shell command, and directory search
2483 @cindex directory search (@code{VPATH}), and shell commands
2485 When a prerequisite is found in another directory through directory search,
2486 this cannot change the commands of the rule; they will execute as written.
2487 Therefore, you must write the commands with care so that they will look for
2488 the prerequisite in the directory where @code{make} finds it.
2490 This is done with the @dfn{automatic variables} such as @samp{$^}
2491 (@pxref{Automatic Variables}).
2492 For instance, the value of @samp{$^} is a
2493 list of all the prerequisites of the rule, including the names of
2494 the directories in which they were found, and the value of
2495 @samp{$@@} is the target.  Thus:@refill
2497 @example
2498 foo.o : foo.c
2499         cc -c $(CFLAGS) $^ -o $@@
2500 @end example
2502 @noindent
2503 (The variable @code{CFLAGS} exists so you can specify flags for C
2504 compilation by implicit rules; we use it here for consistency so it will
2505 affect all C compilations uniformly;
2506 @pxref{Implicit Variables, ,Variables Used by Implicit Rules}.)
2508 Often the prerequisites include header files as well, which you do not
2509 want to mention in the commands.  The automatic variable @samp{$<} is
2510 just the first prerequisite:
2512 @example
2513 VPATH = src:../headers
2514 foo.o : foo.c defs.h hack.h
2515         cc -c $(CFLAGS) $< -o $@@
2516 @end example
2518 @node Implicit/Search, Libraries/Search, Commands/Search, Directory Search
2519 @subsection Directory Search and Implicit Rules
2520 @cindex @code{VPATH}, and implicit rules
2521 @cindex directory search (@code{VPATH}), and implicit rules
2522 @cindex search path for prerequisites (@code{VPATH}), and implicit rules
2523 @cindex implicit rule, and directory search
2524 @cindex implicit rule, and @code{VPATH}
2525 @cindex rule, implicit, and directory search
2526 @cindex rule, implicit, and @code{VPATH}
2528 The search through the directories specified in @code{VPATH} or with
2529 @code{vpath} also happens during consideration of implicit rules
2530 (@pxref{Implicit Rules, ,Using Implicit Rules}).
2532 For example, when a file @file{foo.o} has no explicit rule, @code{make}
2533 considers implicit rules, such as the built-in rule to compile
2534 @file{foo.c} if that file exists.  If such a file is lacking in the
2535 current directory, the appropriate directories are searched for it.  If
2536 @file{foo.c} exists (or is mentioned in the makefile) in any of the
2537 directories, the implicit rule for C compilation is applied.
2539 The commands of implicit rules normally use automatic variables as a
2540 matter of necessity; consequently they will use the file names found by
2541 directory search with no extra effort.
2543 @node Libraries/Search,  , Implicit/Search, Directory Search
2544 @subsection Directory Search for Link Libraries
2545 @cindex link libraries, and directory search
2546 @cindex libraries for linking, directory search
2547 @cindex directory search (@code{VPATH}), and link libraries
2548 @cindex @code{VPATH}, and link libraries
2549 @cindex search path for prerequisites (@code{VPATH}), and link libraries
2550 @cindex @code{-l} (library search)
2551 @cindex link libraries, patterns matching
2552 @cindex @code{.LIBPATTERNS}, and link libraries
2553 @vindex .LIBPATTERNS
2555 Directory search applies in a special way to libraries used with the
2556 linker.  This special feature comes into play when you write a prerequisite
2557 whose name is of the form @samp{-l@var{name}}.  (You can tell something
2558 strange is going on here because the prerequisite is normally the name of a
2559 file, and the @emph{file name} of a library generally looks like
2560 @file{lib@var{name}.a}, not like @samp{-l@var{name}}.)@refill
2562 When a prerequisite's name has the form @samp{-l@var{name}}, @code{make}
2563 handles it specially by searching for the file @file{lib@var{name}.so} in
2564 the current directory, in directories specified by matching @code{vpath}
2565 search paths and the @code{VPATH} search path, and then in the
2566 directories @file{/lib}, @file{/usr/lib}, and @file{@var{prefix}/lib}
2567 (normally @file{/usr/local/lib}, but MS-DOS/MS-Windows versions of
2568 @code{make} behave as if @var{prefix} is defined to be the root of the
2569 DJGPP installation tree).
2571 If that file is not found, then the file @file{lib@var{name}.a} is
2572 searched for, in the same directories as above.
2574 For example, if there is a @file{/usr/lib/libcurses.a} library on your
2575 system (and no @file{/usr/lib/libcurses.so} file), then
2577 @example
2578 @group
2579 foo : foo.c -lcurses
2580         cc $^ -o $@@
2581 @end group
2582 @end example
2584 @noindent
2585 would cause the command @samp{cc foo.c /usr/lib/libcurses.a -o foo} to
2586 be executed when @file{foo} is older than @file{foo.c} or than
2587 @file{/usr/lib/libcurses.a}.@refill
2589 Although the default set of files to be searched for is
2590 @file{lib@var{name}.so} and @file{lib@var{name}.a}, this is customizable
2591 via the @code{.LIBPATTERNS} variable.  Each word in the value of this
2592 variable is a pattern string.  When a prerequisite like
2593 @samp{-l@var{name}} is seen, @code{make} will replace the percent in
2594 each pattern in the list with @var{name} and perform the above directory
2595 searches using that library filename.  If no library is found, the next
2596 word in the list will be used.
2598 The default value for @code{.LIBPATTERNS} is @samp{lib%.so lib%.a},
2599 which provides the default behavior described above.
2601 You can turn off link library expansion completely by setting this
2602 variable to an empty value.
2604 @node Phony Targets, Force Targets, Directory Search, Rules
2605 @section Phony Targets
2606 @cindex phony targets
2607 @cindex targets, phony
2608 @cindex targets without a file
2610 A phony target is one that is not really the name of a file.  It is just a
2611 name for some commands to be executed when you make an explicit request.
2612 There are two reasons to use a phony target: to avoid a conflict with
2613 a file of the same name, and to improve performance.
2615 If you write a rule whose commands will not create the target file, the
2616 commands will be executed every time the target comes up for remaking.
2617 Here is an example:
2619 @example
2620 @group
2621 clean:
2622         rm *.o temp
2623 @end group
2624 @end example
2626 @noindent
2627 Because the @code{rm} command does not create a file named @file{clean},
2628 probably no such file will ever exist.  Therefore, the @code{rm} command
2629 will be executed every time you say @samp{make clean}.
2630 @cindex @code{rm} (shell command)
2632 @findex .PHONY
2633 The phony target will cease to work if anything ever does create a file
2634 named @file{clean} in this directory.  Since it has no prerequisites, the
2635 file @file{clean} would inevitably be considered up to date, and its
2636 commands would not be executed.  To avoid this problem, you can explicitly
2637 declare the target to be phony, using the special target @code{.PHONY}
2638 (@pxref{Special Targets, ,Special Built-in Target Names}) as follows:
2640 @example
2641 .PHONY : clean
2642 @end example
2644 @noindent
2645 Once this is done, @samp{make clean} will run the commands regardless of
2646 whether there is a file named @file{clean}.
2648 Since it knows that phony targets do not name actual files that could be
2649 remade from other files, @code{make} skips the implicit rule search for
2650 phony targets (@pxref{Implicit Rules}).  This is why declaring a target
2651 phony is good for performance, even if you are not worried about the
2652 actual file existing.
2654 Thus, you first write the line that states that @code{clean} is a
2655 phony target, then you write the rule, like this:
2657 @example
2658 @group
2659 .PHONY: clean
2660 clean:
2661         rm *.o temp
2662 @end group
2663 @end example
2665 Another example of the usefulness of phony targets is in conjunction
2666 with recursive invocations of @code{make} (for more information, see
2667 @ref{Recursion, ,Recursive Use of @code{make}}).  In this case the
2668 makefile will often contain a variable which lists a number of
2669 subdirectories to be built.  One way to handle this is with one rule
2670 whose command is a shell loop over the subdirectories, like this:
2672 @example
2673 @group
2674 SUBDIRS = foo bar baz
2676 subdirs:
2677         for dir in $(SUBDIRS); do \
2678           $(MAKE) -C $$dir; \
2679         done
2680 @end group
2681 @end example
2683 There are a few problems with this method, however.  First, any error
2684 detected in a submake is not noted by this rule, so it will continue to
2685 build the rest of the directories even when one fails.  This can be
2686 overcome by adding shell commands to note the error and exit, but then
2687 it will do so even if @code{make} is invoked with the @code{-k} option,
2688 which is unfortunate.  Second, and perhaps more importantly, you cannot
2689 take advantage of @code{make}'s ability to build targets in parallel
2690 (@pxref{Parallel, ,Parallel Execution}), since there is only one rule.
2692 By declaring the subdirectories as phony targets (you must do this as
2693 the subdirectory obviously always exists; otherwise it won't be built)
2694 you can remove these problems:
2696 @example
2697 @group
2698 SUBDIRS = foo bar baz
2700 .PHONY: subdirs $(SUBDIRS)
2702 subdirs: $(SUBDIRS)
2704 $(SUBDIRS):
2705         $(MAKE) -C $@@
2707 foo: baz
2708 @end group
2709 @end example
2711 Here we've also declared that the @file{foo} subdirectory cannot be
2712 built until after the @file{baz} subdirectory is complete; this kind of
2713 relationship declaration is particularly important when attempting
2714 parallel builds.
2716 A phony target should not be a prerequisite of a real target file; if it
2717 is, its commands are run every time @code{make} goes to update that
2718 file.  As long as a phony target is never a prerequisite of a real
2719 target, the phony target commands will be executed only when the phony
2720 target is a specified goal (@pxref{Goals, ,Arguments to Specify the
2721 Goals}).
2723 Phony targets can have prerequisites.  When one directory contains multiple
2724 programs, it is most convenient to describe all of the programs in one
2725 makefile @file{./Makefile}.  Since the target remade by default will be the
2726 first one in the makefile, it is common to make this a phony target named
2727 @samp{all} and give it, as prerequisites, all the individual programs.  For
2728 example:
2730 @example
2731 all : prog1 prog2 prog3
2732 .PHONY : all
2734 prog1 : prog1.o utils.o
2735         cc -o prog1 prog1.o utils.o
2737 prog2 : prog2.o
2738         cc -o prog2 prog2.o
2740 prog3 : prog3.o sort.o utils.o
2741         cc -o prog3 prog3.o sort.o utils.o
2742 @end example
2744 @noindent
2745 Now you can say just @samp{make} to remake all three programs, or
2746 specify as arguments the ones to remake (as in @samp{make prog1
2747 prog3}).  Phoniness is not inherited: the prerequisites of a phony
2748 target are not themselves phony, unless explicitly declared to be so.
2750 When one phony target is a prerequisite of another, it serves as a subroutine
2751 of the other.  For example, here @samp{make cleanall} will delete the
2752 object files, the difference files, and the file @file{program}:
2754 @example
2755 .PHONY: cleanall cleanobj cleandiff
2757 cleanall : cleanobj cleandiff
2758         rm program
2760 cleanobj :
2761         rm *.o
2763 cleandiff :
2764         rm *.diff
2765 @end example
2767 @node Force Targets, Empty Targets, Phony Targets, Rules
2768 @section Rules without Commands or Prerequisites
2769 @cindex force targets
2770 @cindex targets, force
2771 @cindex @code{FORCE}
2772 @cindex rule, no commands or prerequisites
2774 If a rule has no prerequisites or commands, and the target of the rule
2775 is a nonexistent file, then @code{make} imagines this target to have
2776 been updated whenever its rule is run.  This implies that all targets
2777 depending on this one will always have their commands run.
2779 An example will illustrate this:
2781 @example
2782 @group
2783 clean: FORCE
2784         rm $(objects)
2785 FORCE:
2786 @end group
2787 @end example
2789 Here the target @samp{FORCE} satisfies the special conditions, so the
2790 target @file{clean} that depends on it is forced to run its commands.
2791 There is nothing special about the name @samp{FORCE}, but that is one name
2792 commonly used this way.
2794 As you can see, using @samp{FORCE} this way has the same results as using
2795 @samp{.PHONY: clean}.
2797 Using @samp{.PHONY} is more explicit and more efficient.  However,
2798 other versions of @code{make} do not support @samp{.PHONY}; thus
2799 @samp{FORCE} appears in many makefiles.  @xref{Phony Targets}.
2801 @node Empty Targets, Special Targets, Force Targets, Rules
2802 @section Empty Target Files to Record Events
2803 @cindex empty targets
2804 @cindex targets, empty
2805 @cindex recording events with empty targets
2807 The @dfn{empty target} is a variant of the phony target; it is used to hold
2808 commands for an action that you request explicitly from time to time.
2809 Unlike a phony target, this target file can really exist; but the file's
2810 contents do not matter, and usually are empty.
2812 The purpose of the empty target file is to record, with its
2813 last-modification time, when the rule's commands were last executed.  It
2814 does so because one of the commands is a @code{touch} command to update the
2815 target file.
2817 The empty target file should have some prerequisites (otherwise it
2818 doesn't make sense).  When you ask to remake the empty target, the
2819 commands are executed if any prerequisite is more recent than the target;
2820 in other words, if a prerequisite has changed since the last time you
2821 remade the target.  Here is an example:
2823 @example
2824 print: foo.c bar.c
2825         lpr -p $?
2826         touch print
2827 @end example
2828 @cindex @code{print} target
2829 @cindex @code{lpr} (shell command)
2830 @cindex @code{touch} (shell command)
2832 @noindent
2833 With this rule, @samp{make print} will execute the @code{lpr} command if
2834 either source file has changed since the last @samp{make print}.  The
2835 automatic variable @samp{$?} is used to print only those files that have
2836 changed (@pxref{Automatic Variables}).
2838 @node Special Targets, Multiple Targets, Empty Targets, Rules
2839 @section Special Built-in Target Names
2840 @cindex special targets
2841 @cindex built-in special targets
2842 @cindex targets, built-in special
2844 Certain names have special meanings if they appear as targets.
2846 @table @code
2847 @findex .PHONY
2848 @item .PHONY
2850 The prerequisites of the special target @code{.PHONY} are considered to
2851 be phony targets.  When it is time to consider such a target,
2852 @code{make} will run its commands unconditionally, regardless of
2853 whether a file with that name exists or what its last-modification
2854 time is.  @xref{Phony Targets, ,Phony Targets}.
2856 @findex .SUFFIXES
2857 @item .SUFFIXES
2859 The prerequisites of the special target @code{.SUFFIXES} are the list
2860 of suffixes to be used in checking for suffix rules.
2861 @xref{Suffix Rules, , Old-Fashioned Suffix Rules}.
2863 @findex .DEFAULT
2864 @item .DEFAULT
2866 The commands specified for @code{.DEFAULT} are used for any target for
2867 which no rules are found (either explicit rules or implicit rules).
2868 @xref{Last Resort}.  If @code{.DEFAULT} commands are specified, every
2869 file mentioned as a prerequisite, but not as a target in a rule, will have
2870 these commands executed on its behalf.  @xref{Implicit Rule Search,
2871 ,Implicit Rule Search Algorithm}.
2873 @findex .PRECIOUS
2874 @item .PRECIOUS
2875 @cindex precious targets
2876 @cindex preserving with @code{.PRECIOUS}
2878 The targets which @code{.PRECIOUS} depends on are given the following
2879 special treatment: if @code{make} is killed or interrupted during the
2880 execution of their commands, the target is not deleted.
2881 @xref{Interrupts, ,Interrupting or Killing @code{make}}.  Also, if the
2882 target is an intermediate file, it will not be deleted after it is no
2883 longer needed, as is normally done.  @xref{Chained Rules, ,Chains of
2884 Implicit Rules}.  In this latter respect it overlaps with the
2885 @code{.SECONDARY} special target.
2887 You can also list the target pattern of an implicit rule (such as
2888 @samp{%.o}) as a prerequisite file of the special target @code{.PRECIOUS}
2889 to preserve intermediate files created by rules whose target patterns
2890 match that file's name.
2892 @findex .INTERMEDIATE
2893 @item .INTERMEDIATE
2894 @cindex intermediate targets, explicit
2896 The targets which @code{.INTERMEDIATE} depends on are treated as
2897 intermediate files.  @xref{Chained Rules, ,Chains of Implicit Rules}.
2898 @code{.INTERMEDIATE} with no prerequisites has no effect.
2900 @findex .SECONDARY
2901 @item .SECONDARY
2902 @cindex secondary targets
2903 @cindex preserving with @code{.SECONDARY}
2905 The targets which @code{.SECONDARY} depends on are treated as
2906 intermediate files, except that they are never automatically deleted.
2907 @xref{Chained Rules, ,Chains of Implicit Rules}.
2909 @code{.SECONDARY} with no prerequisites causes all targets to be treated
2910 as secondary (i.e., no target is removed because it is considered
2911 intermediate).
2913 @findex .SECONDEXPANSION
2914 @item .SECONDEXPANSION
2916 If @code{.SECONDEXPANSION} is mentioned as a target anywhere in the
2917 makefile, then all prerequisite lists defined @emph{after} it appears
2918 will be expanded a second time after all makefiles have been read in.
2919 @xref{Secondary Expansion, ,Secondary Expansion}.
2921 The prerequisites of the special target @code{.SUFFIXES} are the list
2922 of suffixes to be used in checking for suffix rules.
2923 @xref{Suffix Rules, , Old-Fashioned Suffix Rules}.
2925 @findex .DELETE_ON_ERROR
2926 @item .DELETE_ON_ERROR
2927 @cindex removing targets on failure
2929 If @code{.DELETE_ON_ERROR} is mentioned as a target anywhere in the
2930 makefile, then @code{make} will delete the target of a rule if it has
2931 changed and its commands exit with a nonzero exit status, just as it
2932 does when it receives a signal.  @xref{Errors, ,Errors in Commands}.
2934 @findex .IGNORE
2935 @item .IGNORE
2937 If you specify prerequisites for @code{.IGNORE}, then @code{make} will
2938 ignore errors in execution of the commands run for those particular
2939 files.  The commands for @code{.IGNORE} are not meaningful.
2941 If mentioned as a target with no prerequisites, @code{.IGNORE} says to
2942 ignore errors in execution of commands for all files.  This usage of
2943 @samp{.IGNORE} is supported only for historical compatibility.  Since
2944 this affects every command in the makefile, it is not very useful; we
2945 recommend you use the more selective ways to ignore errors in specific
2946 commands.  @xref{Errors, ,Errors in Commands}.
2948 @findex .LOW_RESOLUTION_TIME
2949 @item .LOW_RESOLUTION_TIME
2951 If you specify prerequisites for @code{.LOW_RESOLUTION_TIME},
2952 @command{make} assumes that these files are created by commands that
2953 generate low resolution time stamps.  The commands for
2954 @code{.LOW_RESOLUTION_TIME} are not meaningful.
2956 The high resolution file time stamps of many modern hosts lessen the
2957 chance of @command{make} incorrectly concluding that a file is up to
2958 date.  Unfortunately, these hosts provide no way to set a high
2959 resolution file time stamp, so commands like @samp{cp -p} that
2960 explicitly set a file's time stamp must discard its subsecond part.  If
2961 a file is created by such a command, you should list it as a
2962 prerequisite of @code{.LOW_RESOLUTION_TIME} so that @command{make} does
2963 not mistakenly conclude that the file is out of date.  For example:
2965 @example
2966 @group
2967 .LOW_RESOLUTION_TIME: dst
2968 dst: src
2969         cp -p src dst
2970 @end group
2971 @end example
2973 Since @samp{cp -p} discards the subsecond part of @file{src}'s time
2974 stamp, @file{dst} is typically slightly older than @file{src} even when
2975 it is up to date.  The @code{.LOW_RESOLUTION_TIME} line causes
2976 @command{make} to consider @file{dst} to be up to date if its time stamp
2977 is at the start of the same second that @file{src}'s time stamp is in.
2979 Due to a limitation of the archive format, archive member time stamps
2980 are always low resolution.  You need not list archive members as
2981 prerequisites of @code{.LOW_RESOLUTION_TIME}, as @command{make} does this
2982 automatically.
2984 @findex .SILENT
2985 @item .SILENT
2987 If you specify prerequisites for @code{.SILENT}, then @code{make} will
2988 not print the commands to remake those particular files before executing
2989 them.  The commands for @code{.SILENT} are not meaningful.
2991 If mentioned as a target with no prerequisites, @code{.SILENT} says not
2992 to print any commands before executing them.  This usage of
2993 @samp{.SILENT} is supported only for historical compatibility.  We
2994 recommend you use the more selective ways to silence specific commands.
2995 @xref{Echoing, ,Command Echoing}.  If you want to silence all commands
2996 for a particular run of @code{make}, use the @samp{-s} or
2997 @w{@samp{--silent}} option (@pxref{Options Summary}).
2999 @findex .EXPORT_ALL_VARIABLES
3000 @item .EXPORT_ALL_VARIABLES
3002 Simply by being mentioned as a target, this tells @code{make} to
3003 export all variables to child processes by default.
3004 @xref{Variables/Recursion, ,Communicating Variables to a
3005 Sub-@code{make}}.
3007 @findex .NOTPARALLEL
3008 @item .NOTPARALLEL
3009 @cindex parallel execution, overriding
3011 If @code{.NOTPARALLEL} is mentioned as a target, then this invocation of
3012 @code{make} will be run serially, even if the @samp{-j} option is
3013 given.  Any recursively invoked @code{make} command will still be run in
3014 parallel (unless its makefile contains this target).  Any prerequisites
3015 on this target are ignored.
3016 @end table
3018 Any defined implicit rule suffix also counts as a special target if it
3019 appears as a target, and so does the concatenation of two suffixes, such
3020 as @samp{.c.o}.  These targets are suffix rules, an obsolete way of
3021 defining implicit rules (but a way still widely used).  In principle, any
3022 target name could be special in this way if you break it in two and add
3023 both pieces to the suffix list.  In practice, suffixes normally begin with
3024 @samp{.}, so these special target names also begin with @samp{.}.
3025 @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
3027 @node Multiple Targets, Multiple Rules, Special Targets, Rules
3028 @section Multiple Targets in a Rule
3029 @cindex multiple targets
3030 @cindex several targets in a rule
3031 @cindex targets, multiple
3032 @cindex rule, with multiple targets
3034 A rule with multiple targets is equivalent to writing many rules, each with
3035 one target, and all identical aside from that.  The same commands apply to
3036 all the targets, but their effects may vary because you can substitute the
3037 actual target name into the command using @samp{$@@}.  The rule contributes
3038 the same prerequisites to all the targets also.
3040 This is useful in two cases.
3042 @itemize @bullet
3043 @item
3044 You want just prerequisites, no commands.  For example:
3046 @example
3047 kbd.o command.o files.o: command.h
3048 @end example
3050 @noindent
3051 gives an additional prerequisite to each of the three object files
3052 mentioned.
3054 @item
3055 Similar commands work for all the targets.  The commands do not need
3056 to be absolutely identical, since the automatic variable @samp{$@@}
3057 can be used to substitute the particular target to be remade into the
3058 commands (@pxref{Automatic Variables}).  For example:
3060 @example
3061 @group
3062 bigoutput littleoutput : text.g
3063         generate text.g -$(subst output,,$@@) > $@@
3064 @end group
3065 @end example
3066 @findex subst
3068 @noindent
3069 is equivalent to
3071 @example
3072 bigoutput : text.g
3073         generate text.g -big > bigoutput
3074 littleoutput : text.g
3075         generate text.g -little > littleoutput
3076 @end example
3078 @noindent
3079 Here we assume the hypothetical program @code{generate} makes two
3080 types of output, one if given @samp{-big} and one if given
3081 @samp{-little}.
3082 @xref{Text Functions, ,Functions for String Substitution and Analysis},
3083 for an explanation of the @code{subst} function.
3084 @end itemize
3086 Suppose you would like to vary the prerequisites according to the target,
3087 much as the variable @samp{$@@} allows you to vary the commands.
3088 You cannot do this with multiple targets in an ordinary rule, but you can
3089 do it with a @dfn{static pattern rule}.
3090 @xref{Static Pattern, ,Static Pattern Rules}.
3092 @node Multiple Rules, Static Pattern, Multiple Targets, Rules
3093 @section Multiple Rules for One Target
3094 @cindex multiple rules for one target
3095 @cindex several rules for one target
3096 @cindex rule, multiple for one target
3097 @cindex target, multiple rules for one
3099 One file can be the target of several rules.  All the prerequisites
3100 mentioned in all the rules are merged into one list of prerequisites for
3101 the target.  If the target is older than any prerequisite from any rule,
3102 the commands are executed.
3104 There can only be one set of commands to be executed for a file.  If
3105 more than one rule gives commands for the same file, @code{make} uses
3106 the last set given and prints an error message.  (As a special case,
3107 if the file's name begins with a dot, no error message is printed.
3108 This odd behavior is only for compatibility with other implementations
3109 of @code{make}... you should avoid using it).  Occasionally it is
3110 useful to have the same target invoke multiple commands which are
3111 defined in different parts of your makefile; you can use
3112 @dfn{double-colon rules} (@pxref{Double-Colon}) for this.
3114 An extra rule with just prerequisites can be used to give a few extra
3115 prerequisites to many files at once.  For example, makefiles often
3116 have a variable, such as @code{objects}, containing a list of all the
3117 compiler output files in the system being made.  An easy way to say
3118 that all of them must be recompiled if @file{config.h} changes is to
3119 write the following:
3121 @example
3122 objects = foo.o bar.o
3123 foo.o : defs.h
3124 bar.o : defs.h test.h
3125 $(objects) : config.h
3126 @end example
3128 This could be inserted or taken out without changing the rules that really
3129 specify how to make the object files, making it a convenient form to use if
3130 you wish to add the additional prerequisite intermittently.
3132 Another wrinkle is that the additional prerequisites could be specified with
3133 a variable that you set with a command argument to @code{make}
3134 (@pxref{Overriding, ,Overriding Variables}).  For example,
3136 @example
3137 @group
3138 extradeps=
3139 $(objects) : $(extradeps)
3140 @end group
3141 @end example
3143 @noindent
3144 means that the command @samp{make extradeps=foo.h} will consider
3145 @file{foo.h} as a prerequisite of each object file, but plain @samp{make}
3146 will not.
3148 If none of the explicit rules for a target has commands, then @code{make}
3149 searches for an applicable implicit rule to find some commands
3150 @pxref{Implicit Rules, ,Using Implicit Rules}).
3152 @node Static Pattern, Double-Colon, Multiple Rules, Rules
3153 @section Static Pattern Rules
3154 @cindex static pattern rule
3155 @cindex rule, static pattern
3156 @cindex pattern rules, static (not implicit)
3157 @cindex varying prerequisites
3158 @cindex prerequisites, varying (static pattern)
3160 @dfn{Static pattern rules} are rules which specify multiple targets and
3161 construct the prerequisite names for each target based on the target name.
3162 They are more general than ordinary rules with multiple targets because the
3163 targets do not have to have identical prerequisites.  Their prerequisites must
3164 be @emph{analogous}, but not necessarily @emph{identical}.
3166 @menu
3167 * Static Usage::                The syntax of static pattern rules.
3168 * Static versus Implicit::      When are they better than implicit rules?
3169 @end menu
3171 @node Static Usage, Static versus Implicit, Static Pattern, Static Pattern
3172 @subsection Syntax of Static Pattern Rules
3173 @cindex static pattern rule, syntax of
3174 @cindex pattern rules, static, syntax of
3176 Here is the syntax of a static pattern rule:
3178 @example
3179 @var{targets} @dots{}: @var{target-pattern}: @var{prereq-patterns} @dots{}
3180         @var{commands}
3181         @dots{}
3182 @end example
3184 @noindent
3185 The @var{targets} list specifies the targets that the rule applies to.
3186 The targets can contain wildcard characters, just like the targets of
3187 ordinary rules (@pxref{Wildcards, ,Using Wildcard Characters in File
3188 Names}).
3190 @cindex target pattern, static (not implicit)
3191 @cindex stem
3192 The @var{target-pattern} and @var{prereq-patterns} say how to compute the
3193 prerequisites of each target.  Each target is matched against the
3194 @var{target-pattern} to extract a part of the target name, called the
3195 @dfn{stem}.  This stem is substituted into each of the @var{prereq-patterns}
3196 to make the prerequisite names (one from each @var{prereq-pattern}).
3198 Each pattern normally contains the character @samp{%} just once.  When the
3199 @var{target-pattern} matches a target, the @samp{%} can match any part of
3200 the target name; this part is called the @dfn{stem}.  The rest of the
3201 pattern must match exactly.  For example, the target @file{foo.o} matches
3202 the pattern @samp{%.o}, with @samp{foo} as the stem.  The targets
3203 @file{foo.c} and @file{foo.out} do not match that pattern.@refill
3205 @cindex prerequisite pattern, static (not implicit)
3206 The prerequisite names for each target are made by substituting the stem
3207 for the @samp{%} in each prerequisite pattern.  For example, if one
3208 prerequisite pattern is @file{%.c}, then substitution of the stem
3209 @samp{foo} gives the prerequisite name @file{foo.c}.  It is legitimate
3210 to write a prerequisite pattern that does not contain @samp{%}; then this
3211 prerequisite is the same for all targets.
3213 @cindex @code{%}, quoting in static pattern
3214 @cindex @code{%}, quoting with @code{\} (backslash)
3215 @cindex @code{\} (backslash), to quote @code{%}
3216 @cindex backslash (@code{\}), to quote @code{%}
3217 @cindex quoting @code{%}, in static pattern
3218 @samp{%} characters in pattern rules can be quoted with preceding
3219 backslashes (@samp{\}).  Backslashes that would otherwise quote @samp{%}
3220 characters can be quoted with more backslashes.  Backslashes that quote
3221 @samp{%} characters or other backslashes are removed from the pattern
3222 before it is compared to file names or has a stem substituted into it.
3223 Backslashes that are not in danger of quoting @samp{%} characters go
3224 unmolested.  For example, the pattern @file{the\%weird\\%pattern\\} has
3225 @samp{the%weird\} preceding the operative @samp{%} character, and
3226 @samp{pattern\\} following it.  The final two backslashes are left alone
3227 because they cannot affect any @samp{%} character.@refill
3229 Here is an example, which compiles each of @file{foo.o} and @file{bar.o}
3230 from the corresponding @file{.c} file:
3232 @example
3233 @group
3234 objects = foo.o bar.o
3236 all: $(objects)
3238 $(objects): %.o: %.c
3239         $(CC) -c $(CFLAGS) $< -o $@@
3240 @end group
3241 @end example
3243 @noindent
3244 Here @samp{$<} is the automatic variable that holds the name of the
3245 prerequisite and @samp{$@@} is the automatic variable that holds the name
3246 of the target; see @ref{Automatic Variables}.
3248 Each target specified must match the target pattern; a warning is issued
3249 for each target that does not.  If you have a list of files, only some of
3250 which will match the pattern, you can use the @code{filter} function to
3251 remove nonmatching file names (@pxref{Text Functions, ,Functions for String Substitution and Analysis}):
3253 @example
3254 files = foo.elc bar.o lose.o
3256 $(filter %.o,$(files)): %.o: %.c
3257         $(CC) -c $(CFLAGS) $< -o $@@
3258 $(filter %.elc,$(files)): %.elc: %.el
3259         emacs -f batch-byte-compile $<
3260 @end example
3262 @noindent
3263 In this example the result of @samp{$(filter %.o,$(files))} is
3264 @file{bar.o lose.o}, and the first static pattern rule causes each of
3265 these object files to be updated by compiling the corresponding C source
3266 file.  The result of @w{@samp{$(filter %.elc,$(files))}} is
3267 @file{foo.elc}, so that file is made from @file{foo.el}.@refill
3269 Another example shows how to use @code{$*} in static pattern rules:
3270 @vindex $*@r{, and static pattern}
3272 @example
3273 @group
3274 bigoutput littleoutput : %output : text.g
3275         generate text.g -$* > $@@
3276 @end group
3277 @end example
3279 @noindent
3280 When the @code{generate} command is run, @code{$*} will expand to the
3281 stem, either @samp{big} or @samp{little}.
3283 @node Static versus Implicit,  , Static Usage, Static Pattern
3284 @subsection Static Pattern Rules versus Implicit Rules
3285 @cindex rule, static pattern versus implicit
3286 @cindex static pattern rule, versus implicit
3288 A static pattern rule has much in common with an implicit rule defined as a
3289 pattern rule (@pxref{Pattern Rules, ,Defining and Redefining Pattern Rules}).
3290 Both have a pattern for the target and patterns for constructing the
3291 names of prerequisites.  The difference is in how @code{make} decides
3292 @emph{when} the rule applies.
3294 An implicit rule @emph{can} apply to any target that matches its pattern,
3295 but it @emph{does} apply only when the target has no commands otherwise
3296 specified, and only when the prerequisites can be found.  If more than one
3297 implicit rule appears applicable, only one applies; the choice depends on
3298 the order of rules.
3300 By contrast, a static pattern rule applies to the precise list of targets
3301 that you specify in the rule.  It cannot apply to any other target and it
3302 invariably does apply to each of the targets specified.  If two conflicting
3303 rules apply, and both have commands, that's an error.
3305 The static pattern rule can be better than an implicit rule for these
3306 reasons:
3308 @itemize @bullet
3309 @item
3310 You may wish to override the usual implicit rule for a few
3311 files whose names cannot be categorized syntactically but
3312 can be given in an explicit list.
3314 @item
3315 If you cannot be sure of the precise contents of the directories
3316 you are using, you may not be sure which other irrelevant files
3317 might lead @code{make} to use the wrong implicit rule.  The choice
3318 might depend on the order in which the implicit rule search is done.
3319 With static pattern rules, there is no uncertainty: each rule applies
3320 to precisely the targets specified.
3321 @end itemize
3323 @node Double-Colon, Automatic Prerequisites, Static Pattern, Rules
3324 @section Double-Colon Rules
3325 @cindex double-colon rules
3326 @cindex rule, double-colon (@code{::})
3327 @cindex multiple rules for one target (@code{::})
3328 @cindex @code{::} rules (double-colon)
3330 @dfn{Double-colon} rules are rules written with @samp{::} instead of
3331 @samp{:} after the target names.  They are handled differently from
3332 ordinary rules when the same target appears in more than one rule.
3334 When a target appears in multiple rules, all the rules must be the same
3335 type: all ordinary, or all double-colon.  If they are double-colon, each
3336 of them is independent of the others.  Each double-colon rule's commands
3337 are executed if the target is older than any prerequisites of that rule.
3338 If there are no prerequisites for that rule, its commands are always
3339 executed (even if the target already exists).  This can result in
3340 executing none, any, or all of the double-colon rules.
3342 Double-colon rules with the same target are in fact completely separate
3343 from one another.  Each double-colon rule is processed individually, just
3344 as rules with different targets are processed.
3346 The double-colon rules for a target are executed in the order they appear
3347 in the makefile.  However, the cases where double-colon rules really make
3348 sense are those where the order of executing the commands would not matter.
3350 Double-colon rules are somewhat obscure and not often very useful; they
3351 provide a mechanism for cases in which the method used to update a target
3352 differs depending on which prerequisite files caused the update, and such
3353 cases are rare.
3355 Each double-colon rule should specify commands; if it does not, an
3356 implicit rule will be used if one applies.
3357 @xref{Implicit Rules, ,Using Implicit Rules}.
3359 @node Automatic Prerequisites,  , Double-Colon, Rules
3360 @section Generating Prerequisites Automatically
3361 @cindex prerequisites, automatic generation
3362 @cindex automatic generation of prerequisites
3363 @cindex generating prerequisites automatically
3365 In the makefile for a program, many of the rules you need to write often
3366 say only that some object file depends on some header
3367 file.  For example, if @file{main.c} uses @file{defs.h} via an
3368 @code{#include}, you would write:
3370 @example
3371 main.o: defs.h
3372 @end example
3374 @noindent
3375 You need this rule so that @code{make} knows that it must remake
3376 @file{main.o} whenever @file{defs.h} changes.  You can see that for a
3377 large program you would have to write dozens of such rules in your
3378 makefile.  And, you must always be very careful to update the makefile
3379 every time you add or remove an @code{#include}.
3380 @cindex @code{#include}
3382 @cindex @code{-M} (to compiler)
3383 To avoid this hassle, most modern C compilers can write these rules for
3384 you, by looking at the @code{#include} lines in the source files.
3385 Usually this is done with the @samp{-M} option to the compiler.
3386 For example, the command:
3388 @example
3389 cc -M main.c
3390 @end example
3392 @noindent
3393 generates the output:
3395 @example
3396 main.o : main.c defs.h
3397 @end example
3399 @noindent
3400 Thus you no longer have to write all those rules yourself.
3401 The compiler will do it for you.
3403 Note that such a prerequisite constitutes mentioning @file{main.o} in a
3404 makefile, so it can never be considered an intermediate file by implicit
3405 rule search.  This means that @code{make} won't ever remove the file
3406 after using it; @pxref{Chained Rules, ,Chains of Implicit Rules}.
3408 @cindex @code{make depend}
3409 With old @code{make} programs, it was traditional practice to use this
3410 compiler feature to generate prerequisites on demand with a command like
3411 @samp{make depend}.  That command would create a file @file{depend}
3412 containing all the automatically-generated prerequisites; then the
3413 makefile could use @code{include} to read them in (@pxref{Include}).
3415 In GNU @code{make}, the feature of remaking makefiles makes this
3416 practice obsolete---you need never tell @code{make} explicitly to
3417 regenerate the prerequisites, because it always regenerates any makefile
3418 that is out of date.  @xref{Remaking Makefiles}.
3420 The practice we recommend for automatic prerequisite generation is to have
3421 one makefile corresponding to each source file.  For each source file
3422 @file{@var{name}.c} there is a makefile @file{@var{name}.d} which lists
3423 what files the object file @file{@var{name}.o} depends on.  That way
3424 only the source files that have changed need to be rescanned to produce
3425 the new prerequisites.
3427 Here is the pattern rule to generate a file of prerequisites (i.e., a makefile)
3428 called @file{@var{name}.d} from a C source file called @file{@var{name}.c}:
3430 @smallexample
3431 @group
3432 %.d: %.c
3433         @@set -e; rm -f $@@; \
3434          $(CC) -M $(CPPFLAGS) $< > $@@.$$$$; \
3435          sed 's,\($*\)\.o[ :]*,\1.o $@@ : ,g' < $@@.$$$$ > $@@; \
3436          rm -f $@@.$$$$
3437 @end group
3438 @end smallexample
3440 @noindent
3441 @xref{Pattern Rules}, for information on defining pattern rules.  The
3442 @samp{-e} flag to the shell causes it to exit immediately if the
3443 @code{$(CC)} command (or any other command) fails (exits with a
3444 nonzero status).
3445 @cindex @code{-e} (shell flag)
3447 @cindex @code{-MM} (to GNU compiler)
3448 With the GNU C compiler, you may wish to use the @samp{-MM} flag instead
3449 of @samp{-M}.  This omits prerequisites on system header files.
3450 @xref{Preprocessor Options, , Options Controlling the Preprocessor,
3451 gcc.info, Using GNU CC}, for details.
3453 @cindex @code{sed} (shell command)
3454 The purpose of the @code{sed} command is to translate (for example):
3456 @example
3457 main.o : main.c defs.h
3458 @end example
3460 @noindent
3461 into:
3463 @example
3464 main.o main.d : main.c defs.h
3465 @end example
3467 @noindent
3468 @cindex @code{.d}
3469 This makes each @samp{.d} file depend on all the source and header files
3470 that the corresponding @samp{.o} file depends on.  @code{make} then
3471 knows it must regenerate the prerequisites whenever any of the source or
3472 header files changes.
3474 Once you've defined the rule to remake the @samp{.d} files,
3475 you then use the @code{include} directive to read them all in.
3476 @xref{Include}.  For example:
3478 @example
3479 @group
3480 sources = foo.c bar.c
3482 include $(sources:.c=.d)
3483 @end group
3484 @end example
3486 @noindent
3487 (This example uses a substitution variable reference to translate the
3488 list of source files @samp{foo.c bar.c} into a list of prerequisite
3489 makefiles, @samp{foo.d bar.d}.  @xref{Substitution Refs}, for full
3490 information on substitution references.)  Since the @samp{.d} files are
3491 makefiles like any others, @code{make} will remake them as necessary
3492 with no further work from you.  @xref{Remaking Makefiles}.
3494 Note that the @samp{.d} files contain target definitions; you should
3495 be sure to place the @code{include} directive @emph{after} the first,
3496 default goal in your makefiles or run the risk of having a random
3497 object file become the default goal.
3498 @xref{How Make Works}.
3500 @node Commands, Using Variables, Rules, Top
3501 @chapter Writing the Commands in Rules
3502 @cindex commands, how to write
3503 @cindex rule commands
3504 @cindex writing rule commands
3506 The commands of a rule consist of one or more shell command lines to
3507 be executed, one at a time, in the order they appear.  Typically, the
3508 result of executing these commands is that the target of the rule is
3509 brought up to date.
3511 Users use many different shell programs, but commands in makefiles are
3512 always interpreted by @file{/bin/sh} unless the makefile specifies
3513 otherwise.  @xref{Execution, ,Command Execution}.
3515 @menu
3516 * Command Syntax::              Command syntax features and pitfalls.
3517 * Echoing::                     How to control when commands are echoed.
3518 * Execution::                   How commands are executed.
3519 * Parallel::                    How commands can be executed in parallel.
3520 * Errors::                      What happens after a command execution error.
3521 * Interrupts::                  What happens when a command is interrupted.
3522 * Recursion::                   Invoking @code{make} from makefiles.
3523 * Sequences::                   Defining canned sequences of commands.
3524 * Empty Commands::              Defining useful, do-nothing commands.
3525 @end menu
3527 @node Command Syntax, Echoing, Commands, Commands
3528 @section Command Syntax
3529 @cindex command syntax
3530 @cindex syntax of commands
3532 Makefiles have the unusual property that there are really two distinct
3533 syntaxes in one file.  Most of the makefile uses @code{make} syntax
3534 (@pxref{Makefiles, ,Writing Makefiles}).  However, commands are meant to be
3535 interpreted by the shell and so they are written using shell syntax.
3536 The @code{make} program does not try to understand shell syntax: it
3537 performs only a very few specific translations on the content of the
3538 command before handing it to the shell.
3540 Each command line must start with a tab, except that the first command
3541 line may be attached to the target-and-prerequisites line with a
3542 semicolon in between.  @emph{Any} line in the makefile that begins
3543 with a tab and appears in a ``rule context'' (that is, after a rule
3544 has been started until another rule or variable definition) will be
3545 considered a command line for that rule.  Blank lines and lines of
3546 just comments may appear among the command lines; they are ignored.
3548 Some consequences of these rules include:
3550 @itemize @bullet
3551 @item
3552 A blank line that begins with a tab is not blank: it's an empty
3553 command (@pxref{Empty Commands}).
3555 @cindex comments, in commands
3556 @cindex commands, comments in
3557 @cindex @code{#} (comments), in commands
3558 @item
3559 A comment in a command line is not a @code{make} comment; it will be
3560 passed to the shell as-is.  Whether the shell treats it as a comment
3561 or not depends on your shell.
3563 @item
3564 A variable definition in a ``rule context'' which is indented by a tab
3565 as the first character on the line, will be considered a command line,
3566 not a @code{make} variable definition, and passed to the shell.
3568 @item
3569 A conditional expression (@code{ifdef}, @code{ifeq},
3570 etc. @pxref{Conditional Syntax, ,Syntax of Conditionals}) in a ``rule
3571 context'' which is indented by a tab as the first character on the
3572 line, will be considered a command line and be passed to the shell.
3574 @end itemize
3576 @menu
3577 * Splitting Lines::             Breaking long command lines for readability.
3578 * Variables in Commands::       Using @code{make} variables in commands.
3579 @end menu
3581 @node Splitting Lines, Variables in Commands, Command Syntax, Command Syntax
3582 @subsection Splitting Command Lines
3583 @cindex commands, splitting
3584 @cindex splitting commands
3585 @cindex commands, backslash (@code{\}) in
3586 @cindex commands, quoting newlines in
3587 @cindex backslash (@code{\}), in commands
3588 @cindex @code{\} (backslash), in commands
3589 @cindex quoting newline, in commands
3590 @cindex newline, quoting, in commands
3592 One of the few ways in which @code{make} does interpret command lines
3593 is checking for a backslash just before the newline.  As in normal
3594 makefile syntax, a single command can be split into multiple lines in
3595 the makefile by placing a backslash before each newline.  A sequence
3596 of lines like this is considered a single command, and one instance of
3597 the shell will be invoked to run it.
3599 However, in contrast to how they are treated in other places in a
3600 makefile, backslash-newline pairs are @emph{not} removed from the
3601 command.  Both the backslash and the newline characters are preserved
3602 and passed to the shell.  How the backslash-newline is interpreted
3603 depends on your shell.  If the first character of the next line
3604 after the backslash-newline is a tab, then that tab (and only that
3605 tab) is removed.  Whitespace is never added to the command.
3607 For example, this makefile:
3609 @example
3610 @group
3611 all :
3612         @@echo no\
3613 space
3614         @@echo no\
3615         space
3616         @@echo one \
3617         space
3618         @@echo one\
3619          space
3620 @end group
3621 @end example
3623 @noindent
3624 consists of four separate shell commands where the output is:
3626 @example
3627 @group
3628 nospace
3629 nospace
3630 one space
3631 one space
3632 @end group
3633 @end example
3635 As a more complex example, this makefile:
3637 @example
3638 @group
3639 all : ; @@echo 'hello \
3640         world' ; echo "hello \
3641     world"
3642 @end group
3643 @end example
3645 @noindent
3646 will run one shell with a command script of:
3648 @example
3649 @group
3650 echo 'hello \
3651 world' ; echo "hello \
3652     world"
3653 @end group
3654 @end example
3656 @noindent
3657 which, according to shell quoting rules, will yield the following output:
3659 @example
3660 @group
3661 hello \
3662 world
3663 hello     world
3664 @end group
3665 @end example
3667 @noindent
3668 Notice how the backslash/newline pair was removed inside the string quoted
3669 with double quotes (@code{"..."}), but not from the string quoted with single
3670 quotes (@code{'...'}).  This is the way the default shell (@file{/bin/sh})
3671 handles backslash/newline pairs.  If you specify a different shell in your
3672 makefiles it may treat them differently.
3674 Sometimes you want to split a long line inside of single quotes, but
3675 you don't want the backslash-newline to appear in the quoted content.
3676 This is often the case when passing scripts to languages such as Perl,
3677 where extraneous backslashes inside the script can change its meaning
3678 or even be a syntax error.  One simple way of handling this is to
3679 place the quoted string, or even the entire command, into a
3680 @code{make} variable then use the variable in the command.  In this
3681 situation the newline quoting rules for makefiles will be used, and
3682 the backslash-newline will be removed.  If we rewrite our example
3683 above using this method:
3685 @example
3686 @group
3687 HELLO = 'hello \
3688 world'
3690 all : ; @@echo $(HELLO)
3691 @end group
3692 @end example
3694 @noindent
3695 we will get output like this:
3697 @example
3698 @group
3699 hello world
3700 @end group
3701 @end example
3703 If you like, you can also use target-specific variables
3704 (@pxref{Target-specific, ,Target-specific Variable Values}) to obtain
3705 a tighter correspondence between the variable and the command that
3706 uses it.
3708 @node Variables in Commands,  , Splitting Lines, Command Syntax
3709 @subsection Using Variables in Commands
3710 @cindex variable references in commands
3711 @cindex commands, using variables in
3713 The other way in which @code{make} processes commands is by expanding
3714 any variable references in them (@pxref{Reference,Basics of Variable
3715 References}).  This occurs after make has finished reading all the
3716 makefiles and the target is determined to be out of date; so, the
3717 commands for targets which are not rebuilt are never expanded.
3719 Variable and function references in commands have identical syntax and
3720 semantics to references elsewhere in the makefile.  They also have the
3721 same quoting rules: if you want a dollar sign to appear in your
3722 command, you must double it (@samp{$$}).  For shells like the default
3723 shell, that use dollar signs to introduce variables, it's important to
3724 keep clear in your mind whether the variable you want to reference is
3725 a @code{make} variable (use a single dollar sign) or a shell variable
3726 (use two dollar signs).  For example:
3728 @example
3729 @group
3730 LIST = one two three
3731 all:
3732         for i in $(LIST); do \
3733             echo $$i; \
3734         done
3735 @end group
3736 @end example
3738 @noindent
3739 results in the following command being passed to the shell:
3741 @example
3742 @group
3743 for i in one two three; do \
3744     echo $i; \
3745 done
3746 @end group
3747 @end example
3749 @noindent
3750 which generates the expected result:
3752 @example
3753 @group
3756 three
3757 @end group
3758 @end example
3760 @node Echoing, Execution, Command Syntax, Commands
3761 @section Command Echoing
3762 @cindex echoing of commands
3763 @cindex silent operation
3764 @cindex @code{@@} (in commands)
3765 @cindex commands, echoing
3766 @cindex printing of commands
3768 Normally @code{make} prints each command line before it is executed.
3769 We call this @dfn{echoing} because it gives the appearance that you
3770 are typing the commands yourself.
3772 When a line starts with @samp{@@}, the echoing of that line is suppressed.
3773 The @samp{@@} is discarded before the command is passed to the shell.
3774 Typically you would use this for a command whose only effect is to print
3775 something, such as an @code{echo} command to indicate progress through
3776 the makefile:
3778 @example
3779 @@echo About to make distribution files
3780 @end example
3782 @cindex @code{-n}
3783 @cindex @code{--just-print}
3784 @cindex @code{--dry-run}
3785 @cindex @code{--recon}
3786 When @code{make} is given the flag @samp{-n} or @samp{--just-print}
3787 it only echoes commands, it won't execute them.  @xref{Options Summary,
3788 ,Summary of Options}.  In this case and only this case, even the
3789 commands starting with @samp{@@} are printed.  This flag is useful for
3790 finding out which commands @code{make} thinks are necessary without
3791 actually doing them.
3793 @cindex @code{-s}
3794 @cindex @code{--silent}
3795 @cindex @code{--quiet}
3796 @findex .SILENT
3797 The @samp{-s} or @samp{--silent}
3798 flag to @code{make} prevents all echoing, as if all commands
3799 started with @samp{@@}.  A rule in the makefile for the special target
3800 @code{.SILENT} without prerequisites has the same effect
3801 (@pxref{Special Targets, ,Special Built-in Target Names}).
3802 @code{.SILENT} is essentially obsolete since @samp{@@} is more flexible.@refill
3804 @node Execution, Parallel, Echoing, Commands
3805 @section Command Execution
3806 @cindex commands, execution
3807 @cindex execution, of commands
3808 @cindex shell command, execution
3809 @vindex @code{SHELL} @r{(command execution)}
3811 When it is time to execute commands to update a target, they are
3812 executed by invoking a new subshell for each command line.  (In
3813 practice, @code{make} may take shortcuts that do not affect the
3814 results.)
3816 @cindex @code{cd} (shell command)
3817 @cindex shell variables, setting in commands
3818 @cindex commands setting shell variables
3819 @strong{Please note:} this implies that setting shell variables and
3820 invoking shell commands such as @code{cd} that set a context local to
3821 each process will not affect the following command lines.@footnote{On
3822 MS-DOS, the value of current working directory is @strong{global}, so
3823 changing it @emph{will} affect the following command lines on those
3824 systems.}  If you want to use @code{cd} to affect the next statement,
3825 put both statements in a single command line.  Then @code{make} will
3826 invoke one shell to run the entire line, and the shell will execute
3827 the statements in sequence.  For example:
3829 @example
3830 foo : bar/lose
3831         cd $(@@D) && gobble $(@@F) > ../$@@
3832 @end example
3834 @noindent
3835 Here we use the shell AND operator (@code{&&}) so that if the
3836 @code{cd} command fails, the script will fail without trying to invoke
3837 the @code{gobble} command in the wrong directory, which could cause
3838 problems (in this case it would certainly cause @file{../foo} to be
3839 truncated, at least).
3841 @menu
3842 * Choosing the Shell::          How @code{make} chooses the shell used
3843                                   to run commands.
3844 @end menu
3846 @node Choosing the Shell,  , Execution, Execution
3847 @subsection Choosing the Shell
3848 @cindex shell, choosing the
3849 @cindex @code{SHELL}, value of
3851 @vindex SHELL
3852 The program used as the shell is taken from the variable @code{SHELL}.
3853 If this variable is not set in your makefile, the program
3854 @file{/bin/sh} is used as the shell.
3856 @cindex environment, @code{SHELL} in
3857 Unlike most variables, the variable @code{SHELL} is never set from the
3858 environment.  This is because the @code{SHELL} environment variable is
3859 used to specify your personal choice of shell program for interactive
3860 use.  It would be very bad for personal choices like this to affect the
3861 functioning of makefiles.  @xref{Environment, ,Variables from the
3862 Environment}.
3864 Furthermore, when you do set @code{SHELL} in your makefile that value
3865 is @emph{not} exported in the environment to commands that @code{make}
3866 invokes.  Instead, the value inherited from the user's environment, if
3867 any, is exported.  You can override this behavior by explicitly
3868 exporting @code{SHELL} (@pxref{Variables/Recursion, ,Communicating
3869 Variables to a Sub-@code{make}}), forcing it to be passed in the
3870 environment to commands.
3872 @vindex @code{MAKESHELL} @r{(MS-DOS alternative to @code{SHELL})}
3873 However, on MS-DOS and MS-Windows the value of @code{SHELL} in the
3874 environment @strong{is} used, since on those systems most users do not
3875 set this variable, and therefore it is most likely set specifically to
3876 be used by @code{make}.  On MS-DOS, if the setting of @code{SHELL} is
3877 not suitable for @code{make}, you can set the variable
3878 @code{MAKESHELL} to the shell that @code{make} should use; if set it
3879 will be used as the shell instead of the value of @code{SHELL}.
3881 @subsubheading Choosing a Shell in DOS and Windows
3882 @cindex shell, in DOS and Windows
3883 @cindex DOS, choosing a shell in
3884 @cindex Windows, choosing a shell in
3886 Choosing a shell in MS-DOS and MS-Windows is much more complex than on
3887 other systems.
3889 @vindex COMSPEC
3890 On MS-DOS, if @code{SHELL} is not set, the value of the variable
3891 @code{COMSPEC} (which is always set) is used instead.
3893 @cindex @code{SHELL}, MS-DOS specifics
3894 The processing of lines that set the variable @code{SHELL} in Makefiles
3895 is different on MS-DOS.  The stock shell, @file{command.com}, is
3896 ridiculously limited in its functionality and many users of @code{make}
3897 tend to install a replacement shell.  Therefore, on MS-DOS, @code{make}
3898 examines the value of @code{SHELL}, and changes its behavior based on
3899 whether it points to a Unix-style or DOS-style shell.  This allows
3900 reasonable functionality even if @code{SHELL} points to
3901 @file{command.com}.
3903 If @code{SHELL} points to a Unix-style shell, @code{make} on MS-DOS
3904 additionally checks whether that shell can indeed be found; if not, it
3905 ignores the line that sets @code{SHELL}.  In MS-DOS, GNU @code{make}
3906 searches for the shell in the following places:
3908 @enumerate
3909 @item
3910 In the precise place pointed to by the value of @code{SHELL}.  For
3911 example, if the makefile specifies @samp{SHELL = /bin/sh}, @code{make}
3912 will look in the directory @file{/bin} on the current drive.
3914 @item
3915 In the current directory.
3917 @item
3918 In each of the directories in the @code{PATH} variable, in order.
3920 @end enumerate
3922 In every directory it examines, @code{make} will first look for the
3923 specific file (@file{sh} in the example above).  If this is not found,
3924 it will also look in that directory for that file with one of the known
3925 extensions which identify executable files.  For example @file{.exe},
3926 @file{.com}, @file{.bat}, @file{.btm}, @file{.sh}, and some others.
3928 If any of these attempts is successful, the value of @code{SHELL} will
3929 be set to the full pathname of the shell as found.  However, if none of
3930 these is found, the value of @code{SHELL} will not be changed, and thus
3931 the line that sets it will be effectively ignored.  This is so
3932 @code{make} will only support features specific to a Unix-style shell if
3933 such a shell is actually installed on the system where @code{make} runs.
3935 Note that this extended search for the shell is limited to the cases
3936 where @code{SHELL} is set from the Makefile; if it is set in the
3937 environment or command line, you are expected to set it to the full
3938 pathname of the shell, exactly as things are on Unix.
3940 The effect of the above DOS-specific processing is that a Makefile that
3941 contains @samp{SHELL = /bin/sh} (as many Unix makefiles do), will work
3942 on MS-DOS unaltered if you have e.g.@: @file{sh.exe} installed in some
3943 directory along your @code{PATH}.
3945 @node Parallel, Errors, Execution, Commands
3946 @section Parallel Execution
3947 @cindex commands, execution in parallel
3948 @cindex parallel execution
3949 @cindex execution, in parallel
3950 @cindex job slots
3951 @cindex @code{-j}
3952 @cindex @code{--jobs}
3954 GNU @code{make} knows how to execute several commands at once.
3955 Normally, @code{make} will execute only one command at a time, waiting
3956 for it to finish before executing the next.  However, the @samp{-j} or
3957 @samp{--jobs} option tells @code{make} to execute many commands
3958 simultaneously.@refill
3960 On MS-DOS, the @samp{-j} option has no effect, since that system doesn't
3961 support multi-processing.
3963 If the @samp{-j} option is followed by an integer, this is the number of
3964 commands to execute at once; this is called the number of @dfn{job slots}.
3965 If there is nothing looking like an integer after the @samp{-j} option,
3966 there is no limit on the number of job slots.  The default number of job
3967 slots is one, which means serial execution (one thing at a time).
3969 One unpleasant consequence of running several commands simultaneously is
3970 that output generated by the commands appears whenever each command
3971 sends it, so messages from different commands may be interspersed.
3973 Another problem is that two processes cannot both take input from the
3974 same device; so to make sure that only one command tries to take input
3975 from the terminal at once, @code{make} will invalidate the standard
3976 input streams of all but one running command.  This means that
3977 attempting to read from standard input will usually be a fatal error (a
3978 @samp{Broken pipe} signal) for most child processes if there are
3979 several.
3980 @cindex broken pipe
3981 @cindex standard input
3983 It is unpredictable which command will have a valid standard input stream
3984 (which will come from the terminal, or wherever you redirect the standard
3985 input of @code{make}).  The first command run will always get it first, and
3986 the first command started after that one finishes will get it next, and so
3989 We will change how this aspect of @code{make} works if we find a better
3990 alternative.  In the mean time, you should not rely on any command using
3991 standard input at all if you are using the parallel execution feature; but
3992 if you are not using this feature, then standard input works normally in
3993 all commands.
3995 Finally, handling recursive @code{make} invocations raises issues.  For
3996 more information on this, see
3997 @ref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
3999 If a command fails (is killed by a signal or exits with a nonzero
4000 status), and errors are not ignored for that command
4001 (@pxref{Errors, ,Errors in Commands}),
4002 the remaining command lines to remake the same target will not be run.
4003 If a command fails and the @samp{-k} or @samp{--keep-going}
4004 option was not given
4005 (@pxref{Options Summary, ,Summary of Options}),
4006 @code{make} aborts execution.  If make
4007 terminates for any reason (including a signal) with child processes
4008 running, it waits for them to finish before actually exiting.@refill
4010 @cindex load average
4011 @cindex limiting jobs based on load
4012 @cindex jobs, limiting based on load
4013 @cindex @code{-l} (load average)
4014 @cindex @code{--max-load}
4015 @cindex @code{--load-average}
4016 When the system is heavily loaded, you will probably want to run fewer jobs
4017 than when it is lightly loaded.  You can use the @samp{-l} option to tell
4018 @code{make} to limit the number of jobs to run at once, based on the load
4019 average.  The @samp{-l} or @samp{--max-load}
4020 option is followed by a floating-point number.  For
4021 example,
4023 @example
4024 -l 2.5
4025 @end example
4027 @noindent
4028 will not let @code{make} start more than one job if the load average is
4029 above 2.5.  The @samp{-l} option with no following number removes the
4030 load limit, if one was given with a previous @samp{-l} option.@refill
4032 More precisely, when @code{make} goes to start up a job, and it already has
4033 at least one job running, it checks the current load average; if it is not
4034 lower than the limit given with @samp{-l}, @code{make} waits until the load
4035 average goes below that limit, or until all the other jobs finish.
4037 By default, there is no load limit.
4039 @node Errors, Interrupts, Parallel, Commands
4040 @section Errors in Commands
4041 @cindex errors (in commands)
4042 @cindex commands, errors in
4043 @cindex exit status (errors)
4045 After each shell command returns, @code{make} looks at its exit status.
4046 If the command completed successfully, the next command line is executed
4047 in a new shell; after the last command line is finished, the rule is
4048 finished.
4050 If there is an error (the exit status is nonzero), @code{make} gives up on
4051 the current rule, and perhaps on all rules.
4053 Sometimes the failure of a certain command does not indicate a problem.
4054 For example, you may use the @code{mkdir} command to ensure that a
4055 directory exists.  If the directory already exists, @code{mkdir} will
4056 report an error, but you probably want @code{make} to continue regardless.
4058 @cindex @code{-} (in commands)
4059 To ignore errors in a command line, write a @samp{-} at the beginning of
4060 the line's text (after the initial tab).  The @samp{-} is discarded before
4061 the command is passed to the shell for execution.
4063 For example,
4065 @example
4066 @group
4067 clean:
4068         -rm -f *.o
4069 @end group
4070 @end example
4071 @cindex @code{rm} (shell command)
4073 @noindent
4074 This causes @code{rm} to continue even if it is unable to remove a file.
4076 @cindex @code{-i}
4077 @cindex @code{--ignore-errors}
4078 @findex .IGNORE
4079 When you run @code{make} with the @samp{-i} or @samp{--ignore-errors}
4080 flag, errors are ignored in all commands of all rules.  A rule in the
4081 makefile for the special target @code{.IGNORE} has the same effect, if
4082 there are no prerequisites.  These ways of ignoring errors are obsolete
4083 because @samp{-} is more flexible.
4085 When errors are to be ignored, because of either a @samp{-} or the
4086 @samp{-i} flag, @code{make} treats an error return just like success,
4087 except that it prints out a message that tells you the status code
4088 the command exited with, and says that the error has been ignored.
4090 When an error happens that @code{make} has not been told to ignore,
4091 it implies that the current target cannot be correctly remade, and neither
4092 can any other that depends on it either directly or indirectly.  No further
4093 commands will be executed for these targets, since their preconditions
4094 have not been achieved.
4097 @cindex @code{-k}
4098 @cindex @code{--keep-going}
4099 Normally @code{make} gives up immediately in this circumstance, returning a
4100 nonzero status.  However, if the @samp{-k} or @samp{--keep-going}
4101 flag is specified, @code{make}
4102 continues to consider the other prerequisites of the pending targets,
4103 remaking them if necessary, before it gives up and returns nonzero status.
4104 For example, after an error in compiling one object file, @samp{make -k}
4105 will continue compiling other object files even though it already knows
4106 that linking them will be impossible.  @xref{Options Summary, ,Summary of Options}.
4108 The usual behavior assumes that your purpose is to get the specified
4109 targets up to date; once @code{make} learns that this is impossible, it
4110 might as well report the failure immediately.  The @samp{-k} option says
4111 that the real purpose is to test as many of the changes made in the
4112 program as possible, perhaps to find several independent problems so
4113 that you can correct them all before the next attempt to compile.  This
4114 is why Emacs' @code{compile} command passes the @samp{-k} flag by
4115 default.
4116 @cindex Emacs (@code{M-x compile})
4118 @findex .DELETE_ON_ERROR
4119 @cindex deletion of target files
4120 @cindex removal of target files
4121 @cindex target, deleting on error
4122 Usually when a command fails, if it has changed the target file at all,
4123 the file is corrupted and cannot be used---or at least it is not
4124 completely updated.  Yet the file's time stamp says that it is now up to
4125 date, so the next time @code{make} runs, it will not try to update that
4126 file.  The situation is just the same as when the command is killed by a
4127 signal; @pxref{Interrupts}.  So generally the right thing to do is to
4128 delete the target file if the command fails after beginning to change
4129 the file.  @code{make} will do this if @code{.DELETE_ON_ERROR} appears
4130 as a target.  This is almost always what you want @code{make} to do, but
4131 it is not historical practice; so for compatibility, you must explicitly
4132 request it.
4134 @node Interrupts, Recursion, Errors, Commands
4135 @section Interrupting or Killing @code{make}
4136 @cindex interrupt
4137 @cindex signal
4138 @cindex deletion of target files
4139 @cindex removal of target files
4140 @cindex target, deleting on interrupt
4141 @cindex killing (interruption)
4143 If @code{make} gets a fatal signal while a command is executing, it may
4144 delete the target file that the command was supposed to update.  This is
4145 done if the target file's last-modification time has changed since
4146 @code{make} first checked it.
4148 The purpose of deleting the target is to make sure that it is remade from
4149 scratch when @code{make} is next run.  Why is this?  Suppose you type
4150 @kbd{Ctrl-c} while a compiler is running, and it has begun to write an
4151 object file @file{foo.o}.  The @kbd{Ctrl-c} kills the compiler, resulting
4152 in an incomplete file whose last-modification time is newer than the source
4153 file @file{foo.c}.  But @code{make} also receives the @kbd{Ctrl-c} signal
4154 and deletes this incomplete file.  If @code{make} did not do this, the next
4155 invocation of @code{make} would think that @file{foo.o} did not require
4156 updating---resulting in a strange error message from the linker when it
4157 tries to link an object file half of which is missing.
4159 @findex .PRECIOUS
4160 You can prevent the deletion of a target file in this way by making the
4161 special target @code{.PRECIOUS} depend on it.  Before remaking a target,
4162 @code{make} checks to see whether it appears on the prerequisites of
4163 @code{.PRECIOUS}, and thereby decides whether the target should be deleted
4164 if a signal happens.  Some reasons why you might do this are that the
4165 target is updated in some atomic fashion, or exists only to record a
4166 modification-time (its contents do not matter), or must exist at all
4167 times to prevent other sorts of trouble.
4169 @node Recursion, Sequences, Interrupts, Commands
4170 @section Recursive Use of @code{make}
4171 @cindex recursion
4172 @cindex subdirectories, recursion for
4174 Recursive use of @code{make} means using @code{make} as a command in a
4175 makefile.  This technique is useful when you want separate makefiles for
4176 various subsystems that compose a larger system.  For example, suppose you
4177 have a subdirectory @file{subdir} which has its own makefile, and you would
4178 like the containing directory's makefile to run @code{make} on the
4179 subdirectory.  You can do it by writing this:
4181 @example
4182 subsystem:
4183         cd subdir && $(MAKE)
4184 @end example
4186 @noindent
4187 or, equivalently, this (@pxref{Options Summary, ,Summary of Options}):
4189 @example
4190 subsystem:
4191         $(MAKE) -C subdir
4192 @end example
4193 @cindex @code{-C}
4194 @cindex @code{--directory}
4196 You can write recursive @code{make} commands just by copying this example,
4197 but there are many things to know about how they work and why, and about
4198 how the sub-@code{make} relates to the top-level @code{make}.  You may
4199 also find it useful to declare targets that invoke recursive
4200 @code{make} commands as @samp{.PHONY} (for more discussion on when
4201 this is useful, see @ref{Phony Targets}).
4203 @vindex @code{CURDIR}
4204 For your convenience, when GNU @code{make} starts (after it has
4205 processed any @code{-C} options) it sets the variable @code{CURDIR} to
4206 the pathname of the current working directory.  This value is never
4207 touched by @code{make} again: in particular note that if you include
4208 files from other directories the value of @code{CURDIR} does not
4209 change.  The value has the same precedence it would have if it were
4210 set in the makefile (by default, an environment variable @code{CURDIR}
4211 will not override this value).  Note that setting this variable has no
4212 impact on the operation of @code{make} (it does not cause @code{make}
4213 to change its working directory, for example).
4215 @menu
4216 * MAKE Variable::               The special effects of using @samp{$(MAKE)}.
4217 * Variables/Recursion::         How to communicate variables to a sub-@code{make}.
4218 * Options/Recursion::           How to communicate options to a sub-@code{make}.
4219 * -w Option::                   How the @samp{-w} or @samp{--print-directory} option
4220                                   helps debug use of recursive @code{make} commands.
4221 @end menu
4223 @node MAKE Variable, Variables/Recursion, Recursion, Recursion
4224 @subsection How the @code{MAKE} Variable Works
4225 @vindex MAKE
4226 @cindex recursion, and @code{MAKE} variable
4228 Recursive @code{make} commands should always use the variable @code{MAKE},
4229 not the explicit command name @samp{make}, as shown here:
4231 @example
4232 @group
4233 subsystem:
4234         cd subdir && $(MAKE)
4235 @end group
4236 @end example
4238 The value of this variable is the file name with which @code{make} was
4239 invoked.  If this file name was @file{/bin/make}, then the command executed
4240 is @samp{cd subdir && /bin/make}.  If you use a special version of
4241 @code{make} to run the top-level makefile, the same special version will be
4242 executed for recursive invocations.
4243 @cindex @code{cd} (shell command)
4245 @cindex +, and commands
4246 As a special feature, using the variable @code{MAKE} in the commands of
4247 a rule alters the effects of the @samp{-t} (@samp{--touch}), @samp{-n}
4248 (@samp{--just-print}), or @samp{-q} (@w{@samp{--question}}) option.
4249 Using the @code{MAKE} variable has the same effect as using a @samp{+}
4250 character at the beginning of the command line.  @xref{Instead of
4251 Execution, ,Instead of Executing the Commands}.  This special feature
4252 is only enabled if the @code{MAKE} variable appears directly in the
4253 command script: it does not apply if the @code{MAKE} variable is
4254 referenced through expansion of another variable.  In the latter case
4255 you must use the @samp{+} token to get these special effects.@refill
4257 Consider the command @samp{make -t} in the above example.  (The
4258 @samp{-t} option marks targets as up to date without actually running
4259 any commands; see @ref{Instead of Execution}.)  Following the usual
4260 definition of @samp{-t}, a @samp{make -t} command in the example would
4261 create a file named @file{subsystem} and do nothing else.  What you
4262 really want it to do is run @samp{@w{cd subdir &&} @w{make -t}}; but that would
4263 require executing the command, and @samp{-t} says not to execute
4264 commands.@refill
4265 @cindex @code{-t}, and recursion
4266 @cindex recursion, and @code{-t}
4267 @cindex @code{--touch}, and recursion
4269 The special feature makes this do what you want: whenever a command
4270 line of a rule contains the variable @code{MAKE}, the flags @samp{-t},
4271 @samp{-n} and @samp{-q} do not apply to that line.  Command lines
4272 containing @code{MAKE} are executed normally despite the presence of a
4273 flag that causes most commands not to be run.  The usual
4274 @code{MAKEFLAGS} mechanism passes the flags to the sub-@code{make}
4275 (@pxref{Options/Recursion, ,Communicating Options to a
4276 Sub-@code{make}}), so your request to touch the files, or print the
4277 commands, is propagated to the subsystem.@refill
4279 @node Variables/Recursion, Options/Recursion, MAKE Variable, Recursion
4280 @subsection Communicating Variables to a Sub-@code{make}
4281 @cindex sub-@code{make}
4282 @cindex environment, and recursion
4283 @cindex exporting variables
4284 @cindex variables, environment
4285 @cindex variables, exporting
4286 @cindex recursion, and environment
4287 @cindex recursion, and variables
4289 Variable values of the top-level @code{make} can be passed to the
4290 sub-@code{make} through the environment by explicit request.  These
4291 variables are defined in the sub-@code{make} as defaults, but do not
4292 override what is specified in the makefile used by the sub-@code{make}
4293 makefile unless you use the @samp{-e} switch (@pxref{Options Summary,
4294 ,Summary of Options}).@refill
4296 To pass down, or @dfn{export}, a variable, @code{make} adds the variable
4297 and its value to the environment for running each command.  The
4298 sub-@code{make}, in turn, uses the environment to initialize its table
4299 of variable values.  @xref{Environment, ,Variables from the
4300 Environment}.
4302 Except by explicit request, @code{make} exports a variable only if it
4303 is either defined in the environment initially or set on the command
4304 line, and if its name consists only of letters, numbers, and underscores.
4305 Some shells cannot cope with environment variable names consisting of
4306 characters other than letters, numbers, and underscores.
4308 @cindex SHELL, exported value
4309 The value of the @code{make} variable @code{SHELL} is not exported.
4310 Instead, the value of the @code{SHELL} variable from the invoking
4311 environment is passed to the sub-@code{make}.  You can force
4312 @code{make} to export its value for @code{SHELL} by using the
4313 @code{export} directive, described below.  @xref{Choosing the Shell}.
4315 The special variable @code{MAKEFLAGS} is always exported (unless you
4316 unexport it).  @code{MAKEFILES} is exported if you set it to anything.
4318 @code{make} automatically passes down variable values that were defined
4319 on the command line, by putting them in the @code{MAKEFLAGS} variable.
4320 @iftex
4321 See the next section.
4322 @end iftex
4323 @ifnottex
4324 @xref{Options/Recursion}.
4325 @end ifnottex
4327 Variables are @emph{not} normally passed down if they were created by
4328 default by @code{make} (@pxref{Implicit Variables, ,Variables Used by
4329 Implicit Rules}).  The sub-@code{make} will define these for
4330 itself.@refill
4332 @findex export
4333 If you want to export specific variables to a sub-@code{make}, use the
4334 @code{export} directive, like this:
4336 @example
4337 export @var{variable} @dots{}
4338 @end example
4340 @noindent
4341 @findex unexport
4342 If you want to @emph{prevent} a variable from being exported, use the
4343 @code{unexport} directive, like this:
4345 @example
4346 unexport @var{variable} @dots{}
4347 @end example
4349 @noindent
4350 In both of these forms, the arguments to @code{export} and
4351 @code{unexport} are expanded, and so could be variables or functions
4352 which expand to a (list of) variable names to be (un)exported.
4354 As a convenience, you can define a variable and export it at the same
4355 time by doing:
4357 @example
4358 export @var{variable} = value
4359 @end example
4361 @noindent
4362 has the same result as:
4364 @example
4365 @var{variable} = value
4366 export @var{variable}
4367 @end example
4369 @noindent
4372 @example
4373 export @var{variable} := value
4374 @end example
4376 @noindent
4377 has the same result as:
4379 @example
4380 @var{variable} := value
4381 export @var{variable}
4382 @end example
4384 Likewise,
4386 @example
4387 export @var{variable} += value
4388 @end example
4390 @noindent
4391 is just like:
4393 @example
4394 @var{variable} += value
4395 export @var{variable}
4396 @end example
4398 @noindent
4399 @xref{Appending, ,Appending More Text to Variables}.
4401 You may notice that the @code{export} and @code{unexport} directives
4402 work in @code{make} in the same way they work in the shell, @code{sh}.
4404 If you want all variables to be exported by default, you can use
4405 @code{export} by itself:
4407 @example
4408 export
4409 @end example
4411 @noindent
4412 This tells @code{make} that variables which are not explicitly mentioned
4413 in an @code{export} or @code{unexport} directive should be exported.
4414 Any variable given in an @code{unexport} directive will still @emph{not}
4415 be exported.  If you use @code{export} by itself to export variables by
4416 default, variables whose names contain characters other than
4417 alphanumerics and underscores will not be exported unless specifically
4418 mentioned in an @code{export} directive.@refill
4420 @findex .EXPORT_ALL_VARIABLES
4421 The behavior elicited by an @code{export} directive by itself was the
4422 default in older versions of GNU @code{make}.  If your makefiles depend
4423 on this behavior and you want to be compatible with old versions of
4424 @code{make}, you can write a rule for the special target
4425 @code{.EXPORT_ALL_VARIABLES} instead of using the @code{export} directive.
4426 This will be ignored by old @code{make}s, while the @code{export}
4427 directive will cause a syntax error.@refill
4428 @cindex compatibility in exporting
4430 Likewise, you can use @code{unexport} by itself to tell @code{make}
4431 @emph{not} to export variables by default.  Since this is the default
4432 behavior, you would only need to do this if @code{export} had been used
4433 by itself earlier (in an included makefile, perhaps).  You
4434 @strong{cannot} use @code{export} and @code{unexport} by themselves to
4435 have variables exported for some commands and not for others.  The last
4436 @code{export} or @code{unexport} directive that appears by itself
4437 determines the behavior for the entire run of @code{make}.@refill
4439 @vindex MAKELEVEL
4440 @cindex recursion, level of
4441 As a special feature, the variable @code{MAKELEVEL} is changed when it
4442 is passed down from level to level.  This variable's value is a string
4443 which is the depth of the level as a decimal number.  The value is
4444 @samp{0} for the top-level @code{make}; @samp{1} for a sub-@code{make},
4445 @samp{2} for a sub-sub-@code{make}, and so on.  The incrementation
4446 happens when @code{make} sets up the environment for a command.@refill
4448 The main use of @code{MAKELEVEL} is to test it in a conditional
4449 directive (@pxref{Conditionals, ,Conditional Parts of Makefiles}); this
4450 way you can write a makefile that behaves one way if run recursively and
4451 another way if run directly by you.@refill
4453 @vindex MAKEFILES
4454 You can use the variable @code{MAKEFILES} to cause all sub-@code{make}
4455 commands to use additional makefiles.  The value of @code{MAKEFILES} is
4456 a whitespace-separated list of file names.  This variable, if defined in
4457 the outer-level makefile, is passed down through the environment; then
4458 it serves as a list of extra makefiles for the sub-@code{make} to read
4459 before the usual or specified ones.  @xref{MAKEFILES Variable, ,The
4460 Variable @code{MAKEFILES}}.@refill
4462 @node Options/Recursion, -w Option, Variables/Recursion, Recursion
4463 @subsection Communicating Options to a Sub-@code{make}
4464 @cindex options, and recursion
4465 @cindex recursion, and options
4467 @vindex MAKEFLAGS
4468 Flags such as @samp{-s} and @samp{-k} are passed automatically to the
4469 sub-@code{make} through the variable @code{MAKEFLAGS}.  This variable is
4470 set up automatically by @code{make} to contain the flag letters that
4471 @code{make} received.  Thus, if you do @w{@samp{make -ks}} then
4472 @code{MAKEFLAGS} gets the value @samp{ks}.@refill
4474 As a consequence, every sub-@code{make} gets a value for @code{MAKEFLAGS}
4475 in its environment.  In response, it takes the flags from that value and
4476 processes them as if they had been given as arguments.
4477 @xref{Options Summary, ,Summary of Options}.
4479 @cindex command line variable definitions, and recursion
4480 @cindex variables, command line, and recursion
4481 @cindex recursion, and command line variable definitions
4482 Likewise variables defined on the command line are passed to the
4483 sub-@code{make} through @code{MAKEFLAGS}.  Words in the value of
4484 @code{MAKEFLAGS} that contain @samp{=}, @code{make} treats as variable
4485 definitions just as if they appeared on the command line.
4486 @xref{Overriding, ,Overriding Variables}.
4488 @cindex @code{-C}, and recursion
4489 @cindex @code{-f}, and recursion
4490 @cindex @code{-o}, and recursion
4491 @cindex @code{-W}, and recursion
4492 @cindex @code{--directory}, and recursion
4493 @cindex @code{--file}, and recursion
4494 @cindex @code{--old-file}, and recursion
4495 @cindex @code{--assume-old}, and recursion
4496 @cindex @code{--assume-new}, and recursion
4497 @cindex @code{--new-file}, and recursion
4498 @cindex recursion, and @code{-C}
4499 @cindex recursion, and @code{-f}
4500 @cindex recursion, and @code{-o}
4501 @cindex recursion, and @code{-W}
4502 The options @samp{-C}, @samp{-f}, @samp{-o}, and @samp{-W} are not put
4503 into @code{MAKEFLAGS}; these options are not passed down.@refill
4505 @cindex @code{-j}, and recursion
4506 @cindex @code{--jobs}, and recursion
4507 @cindex recursion, and @code{-j}
4508 @cindex job slots, and recursion
4509 The @samp{-j} option is a special case (@pxref{Parallel, ,Parallel Execution}).
4510 If you set it to some numeric value @samp{N} and your operating system
4511 supports it (most any UNIX system will; others typically won't), the
4512 parent @code{make} and all the sub-@code{make}s will communicate to
4513 ensure that there are only @samp{N} jobs running at the same time
4514 between them all.  Note that any job that is marked recursive
4515 (@pxref{Instead of Execution, ,Instead of Executing the Commands})
4516 doesn't count against the total jobs (otherwise we could get @samp{N}
4517 sub-@code{make}s running and have no slots left over for any real work!)
4519 If your operating system doesn't support the above communication, then
4520 @samp{-j 1} is always put into @code{MAKEFLAGS} instead of the value you
4521 specified.  This is because if the @w{@samp{-j}} option were passed down
4522 to sub-@code{make}s, you would get many more jobs running in parallel
4523 than you asked for.  If you give @samp{-j} with no numeric argument,
4524 meaning to run as many jobs as possible in parallel, this is passed
4525 down, since multiple infinities are no more than one.@refill
4527 If you do not want to pass the other flags down, you must change the
4528 value of @code{MAKEFLAGS}, like this:
4530 @example
4531 subsystem:
4532         cd subdir && $(MAKE) MAKEFLAGS=
4533 @end example
4535 @vindex MAKEOVERRIDES
4536 The command line variable definitions really appear in the variable
4537 @code{MAKEOVERRIDES}, and @code{MAKEFLAGS} contains a reference to this
4538 variable.  If you do want to pass flags down normally, but don't want to
4539 pass down the command line variable definitions, you can reset
4540 @code{MAKEOVERRIDES} to empty, like this:
4542 @example
4543 MAKEOVERRIDES =
4544 @end example
4546 @noindent
4547 @cindex Arg list too long
4548 @cindex E2BIG
4549 This is not usually useful to do.  However, some systems have a small
4550 fixed limit on the size of the environment, and putting so much
4551 information into the value of @code{MAKEFLAGS} can exceed it.  If you
4552 see the error message @samp{Arg list too long}, this may be the problem.
4553 @findex .POSIX
4554 @cindex POSIX.2
4555 (For strict compliance with POSIX.2, changing @code{MAKEOVERRIDES} does
4556 not affect @code{MAKEFLAGS} if the special target @samp{.POSIX} appears
4557 in the makefile.  You probably do not care about this.)
4559 @vindex MFLAGS
4560 A similar variable @code{MFLAGS} exists also, for historical
4561 compatibility.  It has the same value as @code{MAKEFLAGS} except that it
4562 does not contain the command line variable definitions, and it always
4563 begins with a hyphen unless it is empty (@code{MAKEFLAGS} begins with a
4564 hyphen only when it begins with an option that has no single-letter
4565 version, such as @samp{--warn-undefined-variables}).  @code{MFLAGS} was
4566 traditionally used explicitly in the recursive @code{make} command, like
4567 this:
4569 @example
4570 subsystem:
4571         cd subdir && $(MAKE) $(MFLAGS)
4572 @end example
4574 @noindent
4575 but now @code{MAKEFLAGS} makes this usage redundant.  If you want your
4576 makefiles to be compatible with old @code{make} programs, use this
4577 technique; it will work fine with more modern @code{make} versions too.
4579 @cindex setting options from environment
4580 @cindex options, setting from environment
4581 @cindex setting options in makefiles
4582 @cindex options, setting in makefiles
4583 The @code{MAKEFLAGS} variable can also be useful if you want to have
4584 certain options, such as @samp{-k} (@pxref{Options Summary, ,Summary of
4585 Options}), set each time you run @code{make}.  You simply put a value for
4586 @code{MAKEFLAGS} in your environment.  You can also set @code{MAKEFLAGS} in
4587 a makefile, to specify additional flags that should also be in effect for
4588 that makefile.  (Note that you cannot use @code{MFLAGS} this way.  That
4589 variable is set only for compatibility; @code{make} does not interpret a
4590 value you set for it in any way.)
4592 When @code{make} interprets the value of @code{MAKEFLAGS} (either from the
4593 environment or from a makefile), it first prepends a hyphen if the value
4594 does not already begin with one.  Then it chops the value into words
4595 separated by blanks, and parses these words as if they were options given
4596 on the command line (except that @samp{-C}, @samp{-f}, @samp{-h},
4597 @samp{-o}, @samp{-W}, and their long-named versions are ignored; and there
4598 is no error for an invalid option).
4600 If you do put @code{MAKEFLAGS} in your environment, you should be sure not
4601 to include any options that will drastically affect the actions of
4602 @code{make} and undermine the purpose of makefiles and of @code{make}
4603 itself.  For instance, the @samp{-t}, @samp{-n}, and @samp{-q} options, if
4604 put in one of these variables, could have disastrous consequences and would
4605 certainly have at least surprising and probably annoying effects.@refill
4607 @node -w Option,  , Options/Recursion, Recursion
4608 @subsection The @samp{--print-directory} Option
4609 @cindex directories, printing them
4610 @cindex printing directories
4611 @cindex recursion, and printing directories
4613 If you use several levels of recursive @code{make} invocations, the
4614 @samp{-w} or @w{@samp{--print-directory}} option can make the output a
4615 lot easier to understand by showing each directory as @code{make}
4616 starts processing it and as @code{make} finishes processing it.  For
4617 example, if @samp{make -w} is run in the directory @file{/u/gnu/make},
4618 @code{make} will print a line of the form:@refill
4620 @example
4621 make: Entering directory `/u/gnu/make'.
4622 @end example
4624 @noindent
4625 before doing anything else, and a line of the form:
4627 @example
4628 make: Leaving directory `/u/gnu/make'.
4629 @end example
4631 @noindent
4632 when processing is completed.
4634 @cindex @code{-C}, and @code{-w}
4635 @cindex @code{--directory}, and @code{--print-directory}
4636 @cindex recursion, and @code{-w}
4637 @cindex @code{-w}, and @code{-C}
4638 @cindex @code{-w}, and recursion
4639 @cindex @code{--print-directory}, and @code{--directory}
4640 @cindex @code{--print-directory}, and recursion
4641 @cindex @code{--no-print-directory}
4642 @cindex @code{--print-directory}, disabling
4643 @cindex @code{-w}, disabling
4644 Normally, you do not need to specify this option because @samp{make}
4645 does it for you: @samp{-w} is turned on automatically when you use the
4646 @samp{-C} option, and in sub-@code{make}s.  @code{make} will not
4647 automatically turn on @samp{-w} if you also use @samp{-s}, which says to
4648 be silent, or if you use @samp{--no-print-directory} to explicitly
4649 disable it.
4651 @node Sequences, Empty Commands, Recursion, Commands
4652 @section Defining Canned Command Sequences
4653 @cindex sequences of commands
4654 @cindex commands, sequences of
4656 When the same sequence of commands is useful in making various targets, you
4657 can define it as a canned sequence with the @code{define} directive, and
4658 refer to the canned sequence from the rules for those targets.  The canned
4659 sequence is actually a variable, so the name must not conflict with other
4660 variable names.
4662 Here is an example of defining a canned sequence of commands:
4664 @example
4665 define run-yacc
4666 yacc $(firstword $^)
4667 mv y.tab.c $@@
4668 endef
4669 @end example
4670 @cindex @code{yacc}
4672 @noindent
4673 Here @code{run-yacc} is the name of the variable being defined;
4674 @code{endef} marks the end of the definition; the lines in between are the
4675 commands.  The @code{define} directive does not expand variable references
4676 and function calls in the canned sequence; the @samp{$} characters,
4677 parentheses, variable names, and so on, all become part of the value of the
4678 variable you are defining.
4679 @xref{Defining, ,Defining Variables Verbatim},
4680 for a complete explanation of @code{define}.
4682 The first command in this example runs Yacc on the first prerequisite of
4683 whichever rule uses the canned sequence.  The output file from Yacc is
4684 always named @file{y.tab.c}.  The second command moves the output to the
4685 rule's target file name.
4687 To use the canned sequence, substitute the variable into the commands of a
4688 rule.  You can substitute it like any other variable
4689 (@pxref{Reference, ,Basics of Variable References}).
4690 Because variables defined by @code{define} are recursively expanded
4691 variables, all the variable references you wrote inside the @code{define}
4692 are expanded now.  For example:
4694 @example
4695 foo.c : foo.y
4696         $(run-yacc)
4697 @end example
4699 @noindent
4700 @samp{foo.y} will be substituted for the variable @samp{$^} when it occurs in
4701 @code{run-yacc}'s value, and @samp{foo.c} for @samp{$@@}.@refill
4703 This is a realistic example, but this particular one is not needed in
4704 practice because @code{make} has an implicit rule to figure out these
4705 commands based on the file names involved
4706 (@pxref{Implicit Rules, ,Using Implicit Rules}).
4708 @cindex @@, and @code{define}
4709 @cindex -, and @code{define}
4710 @cindex +, and @code{define}
4711 In command execution, each line of a canned sequence is treated just as
4712 if the line appeared on its own in the rule, preceded by a tab.  In
4713 particular, @code{make} invokes a separate subshell for each line.  You
4714 can use the special prefix characters that affect command lines
4715 (@samp{@@}, @samp{-}, and @samp{+}) on each line of a canned sequence.
4716 @xref{Commands, ,Writing the Commands in Rules}.
4717 For example, using this canned sequence:
4719 @example
4720 define frobnicate
4721 @@echo "frobnicating target $@@"
4722 frob-step-1 $< -o $@@-step-1
4723 frob-step-2 $@@-step-1 -o $@@
4724 endef
4725 @end example
4727 @noindent
4728 @code{make} will not echo the first line, the @code{echo} command.
4729 But it @emph{will} echo the following two command lines.
4731 On the other hand, prefix characters on the command line that refers to
4732 a canned sequence apply to every line in the sequence.  So the rule:
4734 @example
4735 frob.out: frob.in
4736         @@$(frobnicate)
4737 @end example
4739 @noindent
4740 does not echo @emph{any} commands.
4741 (@xref{Echoing, ,Command Echoing}, for a full explanation of @samp{@@}.)
4743 @node Empty Commands,  , Sequences, Commands
4744 @section Using Empty Commands
4745 @cindex empty commands
4746 @cindex commands, empty
4748 It is sometimes useful to define commands which do nothing.  This is done
4749 simply by giving a command that consists of nothing but whitespace.  For
4750 example:
4752 @example
4753 target: ;
4754 @end example
4756 @noindent
4757 defines an empty command string for @file{target}.  You could also use a
4758 line beginning with a tab character to define an empty command string,
4759 but this would be confusing because such a line looks empty.
4761 @findex .DEFAULT@r{, and empty commands}
4762 You may be wondering why you would want to define a command string that
4763 does nothing.  The only reason this is useful is to prevent a target
4764 from getting implicit commands (from implicit rules or the
4765 @code{.DEFAULT} special target; @pxref{Implicit Rules} and
4766 @pxref{Last Resort, ,Defining Last-Resort Default Rules}).@refill
4768 @c !!! another reason is for canonical stamp files:
4769 @ignore
4770 @example
4771 foo: stamp-foo ;
4772 stamp-foo: foo.in
4773         create foo frm foo.in
4774         touch $@
4775 @end example
4776 @end ignore
4778 You may be inclined to define empty command strings for targets that are
4779 not actual files, but only exist so that their prerequisites can be
4780 remade.  However, this is not the best way to do that, because the
4781 prerequisites may not be remade properly if the target file actually does exist.
4782 @xref{Phony Targets, ,Phony Targets}, for a better way to do this.
4784 @node Using Variables, Conditionals, Commands, Top
4785 @chapter How to Use Variables
4786 @cindex variable
4787 @cindex value
4788 @cindex recursive variable expansion
4789 @cindex simple variable expansion
4791 A @dfn{variable} is a name defined in a makefile to represent a string
4792 of text, called the variable's @dfn{value}.  These values are
4793 substituted by explicit request into targets, prerequisites, commands,
4794 and other parts of the makefile.  (In some other versions of @code{make},
4795 variables are called @dfn{macros}.)
4796 @cindex macro
4798 Variables and functions in all parts of a makefile are expanded when
4799 read, except for the shell commands in rules, the right-hand sides of
4800 variable definitions using @samp{=}, and the bodies of variable
4801 definitions using the @code{define} directive.@refill
4803 Variables can represent lists of file names, options to pass to compilers,
4804 programs to run, directories to look in for source files, directories to
4805 write output in, or anything else you can imagine.
4807 A variable name may be any sequence of characters not containing @samp{:},
4808 @samp{#}, @samp{=}, or leading or trailing whitespace.  However,
4809 variable names containing characters other than letters, numbers, and
4810 underscores should be avoided, as they may be given special meanings in the
4811 future, and with some shells they cannot be passed through the environment to a
4812 sub-@code{make}
4813 (@pxref{Variables/Recursion, ,Communicating Variables to a Sub-@code{make}}).
4815 Variable names are case-sensitive.  The names @samp{foo}, @samp{FOO},
4816 and @samp{Foo} all refer to different variables.
4818 It is traditional to use upper case letters in variable names, but we
4819 recommend using lower case letters for variable names that serve internal
4820 purposes in the makefile, and reserving upper case for parameters that
4821 control implicit rules or for parameters that the user should override with
4822 command options (@pxref{Overriding, ,Overriding Variables}).
4824 A few variables have names that are a single punctuation character or
4825 just a few characters.  These are the @dfn{automatic variables}, and
4826 they have particular specialized uses.  @xref{Automatic Variables}.
4828 @menu
4829 * Reference::                   How to use the value of a variable.
4830 * Flavors::                     Variables come in two flavors.
4831 * Advanced::                    Advanced features for referencing a variable.
4832 * Values::                      All the ways variables get their values.
4833 * Setting::                     How to set a variable in the makefile.
4834 * Appending::                   How to append more text to the old value
4835                                   of a variable.
4836 * Override Directive::          How to set a variable in the makefile even if
4837                                   the user has set it with a command argument.
4838 * Defining::                    An alternate way to set a variable
4839                                   to a verbatim string.
4840 * Environment::                 Variable values can come from the environment.
4841 * Target-specific::             Variable values can be defined on a per-target
4842                                   basis.
4843 * Pattern-specific::            Target-specific variable values can be applied
4844                                   to a group of targets that match a pattern.
4845 @end menu
4847 @node Reference, Flavors, Using Variables, Using Variables
4848 @section Basics of Variable References
4849 @cindex variables, how to reference
4850 @cindex reference to variables
4851 @cindex @code{$}, in variable reference
4852 @cindex dollar sign (@code{$}), in variable reference
4854 To substitute a variable's value, write a dollar sign followed by the name
4855 of the variable in parentheses or braces: either @samp{$(foo)} or
4856 @samp{$@{foo@}} is a valid reference to the variable @code{foo}.  This
4857 special significance of @samp{$} is why you must write @samp{$$} to have
4858 the effect of a single dollar sign in a file name or command.
4860 Variable references can be used in any context: targets, prerequisites,
4861 commands, most directives, and new variable values.  Here is an
4862 example of a common case, where a variable holds the names of all the
4863 object files in a program:
4865 @example
4866 @group
4867 objects = program.o foo.o utils.o
4868 program : $(objects)
4869         cc -o program $(objects)
4871 $(objects) : defs.h
4872 @end group
4873 @end example
4875 Variable references work by strict textual substitution.  Thus, the rule
4877 @example
4878 @group
4879 foo = c
4880 prog.o : prog.$(foo)
4881         $(foo)$(foo) -$(foo) prog.$(foo)
4882 @end group
4883 @end example
4885 @noindent
4886 could be used to compile a C program @file{prog.c}.  Since spaces before
4887 the variable value are ignored in variable assignments, the value of
4888 @code{foo} is precisely @samp{c}.  (Don't actually write your makefiles
4889 this way!)
4891 A dollar sign followed by a character other than a dollar sign,
4892 open-parenthesis or open-brace treats that single character as the
4893 variable name.  Thus, you could reference the variable @code{x} with
4894 @samp{$x}.  However, this practice is strongly discouraged, except in
4895 the case of the automatic variables (@pxref{Automatic Variables}).
4897 @node Flavors, Advanced, Reference, Using Variables
4898 @section The Two Flavors of Variables
4899 @cindex flavors of variables
4900 @cindex recursive variable expansion
4901 @cindex variables, flavors
4902 @cindex recursively expanded variables
4903 @cindex variables, recursively expanded
4905 There are two ways that a variable in GNU @code{make} can have a value;
4906 we call them the two @dfn{flavors} of variables.  The two flavors are
4907 distinguished in how they are defined and in what they do when expanded.
4909 @cindex =
4910 The first flavor of variable is a @dfn{recursively expanded} variable.
4911 Variables of this sort are defined by lines using @samp{=}
4912 (@pxref{Setting, ,Setting Variables}) or by the @code{define} directive
4913 (@pxref{Defining, ,Defining Variables Verbatim}).  The value you specify
4914 is installed verbatim; if it contains references to other variables,
4915 these references are expanded whenever this variable is substituted (in
4916 the course of expanding some other string).  When this happens, it is
4917 called @dfn{recursive expansion}.@refill
4919 For example,
4921 @example
4922 foo = $(bar)
4923 bar = $(ugh)
4924 ugh = Huh?
4926 all:;echo $(foo)
4927 @end example
4929 @noindent
4930 will echo @samp{Huh?}: @samp{$(foo)} expands to @samp{$(bar)} which
4931 expands to @samp{$(ugh)} which finally expands to @samp{Huh?}.@refill
4933 This flavor of variable is the only sort supported by other versions of
4934 @code{make}.  It has its advantages and its disadvantages.  An advantage
4935 (most would say) is that:
4937 @example
4938 CFLAGS = $(include_dirs) -O
4939 include_dirs = -Ifoo -Ibar
4940 @end example
4942 @noindent
4943 will do what was intended: when @samp{CFLAGS} is expanded in a command,
4944 it will expand to @samp{-Ifoo -Ibar -O}.  A major disadvantage is that you
4945 cannot append something on the end of a variable, as in
4947 @example
4948 CFLAGS = $(CFLAGS) -O
4949 @end example
4951 @noindent
4952 because it will cause an infinite loop in the variable expansion.
4953 (Actually @code{make} detects the infinite loop and reports an error.)
4954 @cindex loops in variable expansion
4955 @cindex variables, loops in expansion
4957 Another disadvantage is that any functions
4958 (@pxref{Functions, ,Functions for Transforming Text})
4959 referenced in the definition will be executed every time the variable is
4960 expanded.  This makes @code{make} run slower; worse, it causes the
4961 @code{wildcard} and @code{shell} functions to give unpredictable results
4962 because you cannot easily control when they are called, or even how many
4963 times.
4965 To avoid all the problems and inconveniences of recursively expanded
4966 variables, there is another flavor: simply expanded variables.
4968 @cindex simply expanded variables
4969 @cindex variables, simply expanded
4970 @cindex :=
4971 @dfn{Simply expanded variables} are defined by lines using @samp{:=}
4972 (@pxref{Setting, ,Setting Variables}).
4973 The value of a simply expanded variable is scanned
4974 once and for all, expanding any references to other variables and
4975 functions, when the variable is defined.  The actual value of the simply
4976 expanded variable is the result of expanding the text that you write.
4977 It does not contain any references to other variables; it contains their
4978 values @emph{as of the time this variable was defined}.  Therefore,
4980 @example
4981 x := foo
4982 y := $(x) bar
4983 x := later
4984 @end example
4986 @noindent
4987 is equivalent to
4989 @example
4990 y := foo bar
4991 x := later
4992 @end example
4994 When a simply expanded variable is referenced, its value is substituted
4995 verbatim.
4997 Here is a somewhat more complicated example, illustrating the use of
4998 @samp{:=} in conjunction with the @code{shell} function.
4999 (@xref{Shell Function, , The @code{shell} Function}.)  This example
5000 also shows use of the variable @code{MAKELEVEL}, which is changed
5001 when it is passed down from level to level.
5002 (@xref{Variables/Recursion, , Communicating Variables to a
5003 Sub-@code{make}}, for information about @code{MAKELEVEL}.)
5005 @vindex MAKELEVEL
5006 @vindex MAKE
5007 @example
5008 @group
5009 ifeq (0,$@{MAKELEVEL@})
5010 whoami    := $(shell whoami)
5011 host-type := $(shell arch)
5012 MAKE := $@{MAKE@} host-type=$@{host-type@} whoami=$@{whoami@}
5013 endif
5014 @end group
5015 @end example
5017 @noindent
5018 An advantage of this use of @samp{:=} is that a typical
5019 `descend into a directory' command then looks like this:
5021 @example
5022 @group
5023 $@{subdirs@}:
5024         $@{MAKE@} -C $@@ all
5025 @end group
5026 @end example
5028 Simply expanded variables generally make complicated makefile programming
5029 more predictable because they work like variables in most programming
5030 languages.  They allow you to redefine a variable using its own value (or
5031 its value processed in some way by one of the expansion functions) and to
5032 use the expansion functions much more efficiently
5033 (@pxref{Functions, ,Functions for Transforming Text}).
5035 @cindex spaces, in variable values
5036 @cindex whitespace, in variable values
5037 @cindex variables, spaces in values
5038 You can also use them to introduce controlled leading whitespace into
5039 variable values.  Leading whitespace characters are discarded from your
5040 input before substitution of variable references and function calls;
5041 this means you can include leading spaces in a variable value by
5042 protecting them with variable references, like this:
5044 @example
5045 nullstring :=
5046 space := $(nullstring) # end of the line
5047 @end example
5049 @noindent
5050 Here the value of the variable @code{space} is precisely one space.  The
5051 comment @w{@samp{# end of the line}} is included here just for clarity.
5052 Since trailing space characters are @emph{not} stripped from variable
5053 values, just a space at the end of the line would have the same effect
5054 (but be rather hard to read).  If you put whitespace at the end of a
5055 variable value, it is a good idea to put a comment like that at the end
5056 of the line to make your intent clear.  Conversely, if you do @emph{not}
5057 want any whitespace characters at the end of your variable value, you
5058 must remember not to put a random comment on the end of the line after
5059 some whitespace, such as this:
5061 @example
5062 dir := /foo/bar    # directory to put the frobs in
5063 @end example
5065 @noindent
5066 Here the value of the variable @code{dir} is @w{@samp{/foo/bar    }}
5067 (with four trailing spaces), which was probably not the intention.
5068 (Imagine something like @w{@samp{$(dir)/file}} with this definition!)
5070 @cindex conditional variable assignment
5071 @cindex variables, conditional assignment
5072 @cindex ?=
5073 There is another assignment operator for variables, @samp{?=}.  This
5074 is called a conditional variable assignment operator, because it only
5075 has an effect if the variable is not yet defined.  This statement:
5077 @example
5078 FOO ?= bar
5079 @end example
5081 @noindent
5082 is exactly equivalent to this
5083 (@pxref{Origin Function, ,The @code{origin} Function}):
5085 @example
5086 ifeq ($(origin FOO), undefined)
5087   FOO = bar
5088 endif
5089 @end example
5091 Note that a variable set to an empty value is still defined, so
5092 @samp{?=} will not set that variable.
5094 @node Advanced, Values, Flavors, Using Variables
5095 @section Advanced Features for Reference to Variables
5096 @cindex reference to variables
5098 This section describes some advanced features you can use to reference
5099 variables in more flexible ways.
5101 @menu
5102 * Substitution Refs::           Referencing a variable with
5103                                   substitutions on the value.
5104 * Computed Names::              Computing the name of the variable to refer to.
5105 @end menu
5107 @node Substitution Refs, Computed Names, Advanced, Advanced
5108 @subsection Substitution References
5109 @cindex modified variable reference
5110 @cindex substitution variable reference
5111 @cindex variables, modified reference
5112 @cindex variables, substitution reference
5114 @cindex variables, substituting suffix in
5115 @cindex suffix, substituting in variables
5116 A @dfn{substitution reference} substitutes the value of a variable with
5117 alterations that you specify.  It has the form
5118 @samp{$(@var{var}:@var{a}=@var{b})} (or
5119 @samp{$@{@var{var}:@var{a}=@var{b}@}}) and its meaning is to take the value
5120 of the variable @var{var}, replace every @var{a} at the end of a word with
5121 @var{b} in that value, and substitute the resulting string.
5123 When we say ``at the end of a word'', we mean that @var{a} must appear
5124 either followed by whitespace or at the end of the value in order to be
5125 replaced; other occurrences of @var{a} in the value are unaltered.  For
5126 example:@refill
5128 @example
5129 foo := a.o b.o c.o
5130 bar := $(foo:.o=.c)
5131 @end example
5133 @noindent
5134 sets @samp{bar} to @samp{a.c b.c c.c}.  @xref{Setting, ,Setting Variables}.
5136 A substitution reference is actually an abbreviation for use of the
5137 @code{patsubst} expansion function (@pxref{Text Functions, ,Functions for String Substitution and Analysis}).  We provide
5138 substitution references as well as @code{patsubst} for compatibility with
5139 other implementations of @code{make}.
5141 @findex patsubst
5142 Another type of substitution reference lets you use the full power of
5143 the @code{patsubst} function.  It has the same form
5144 @samp{$(@var{var}:@var{a}=@var{b})} described above, except that now
5145 @var{a} must contain a single @samp{%} character.  This case is
5146 equivalent to @samp{$(patsubst @var{a},@var{b},$(@var{var}))}.
5147 @xref{Text Functions, ,Functions for String Substitution and Analysis},
5148 for a description of the @code{patsubst} function.@refill
5150 @example
5151 @group
5152 @exdent For example:
5154 foo := a.o b.o c.o
5155 bar := $(foo:%.o=%.c)
5156 @end group
5157 @end example
5159 @noindent
5160 sets @samp{bar} to @samp{a.c b.c c.c}.
5162 @node Computed Names,  , Substitution Refs, Advanced
5163 @subsection Computed Variable Names
5164 @cindex nested variable reference
5165 @cindex computed variable name
5166 @cindex variables, computed names
5167 @cindex variables, nested references
5168 @cindex variables, @samp{$} in name
5169 @cindex @code{$}, in variable name
5170 @cindex dollar sign (@code{$}), in variable name
5172 Computed variable names are a complicated concept needed only for
5173 sophisticated makefile programming.  For most purposes you need not
5174 consider them, except to know that making a variable with a dollar sign
5175 in its name might have strange results.  However, if you are the type
5176 that wants to understand everything, or you are actually interested in
5177 what they do, read on.
5179 Variables may be referenced inside the name of a variable.  This is
5180 called a @dfn{computed variable name} or a @dfn{nested variable
5181 reference}.  For example,
5183 @example
5184 x = y
5185 y = z
5186 a := $($(x))
5187 @end example
5189 @noindent
5190 defines @code{a} as @samp{z}: the @samp{$(x)} inside @samp{$($(x))} expands
5191 to @samp{y}, so @samp{$($(x))} expands to @samp{$(y)} which in turn expands
5192 to @samp{z}.  Here the name of the variable to reference is not stated
5193 explicitly; it is computed by expansion of @samp{$(x)}.  The reference
5194 @samp{$(x)} here is nested within the outer variable reference.
5196 The previous example shows two levels of nesting, but any number of levels
5197 is possible.  For example, here are three levels:
5199 @example
5200 x = y
5201 y = z
5202 z = u
5203 a := $($($(x)))
5204 @end example
5206 @noindent
5207 Here the innermost @samp{$(x)} expands to @samp{y}, so @samp{$($(x))}
5208 expands to @samp{$(y)} which in turn expands to @samp{z}; now we have
5209 @samp{$(z)}, which becomes @samp{u}.
5211 References to recursively-expanded variables within a variable name are
5212 reexpanded in the usual fashion.  For example:
5214 @example
5215 x = $(y)
5216 y = z
5217 z = Hello
5218 a := $($(x))
5219 @end example
5221 @noindent
5222 defines @code{a} as @samp{Hello}: @samp{$($(x))} becomes @samp{$($(y))}
5223 which becomes @samp{$(z)} which becomes @samp{Hello}.
5225 Nested variable references can also contain modified references and
5226 function invocations (@pxref{Functions, ,Functions for Transforming Text}),
5227 just like any other reference.
5228 For example, using the @code{subst} function
5229 (@pxref{Text Functions, ,Functions for String Substitution and Analysis}):
5231 @example
5232 @group
5233 x = variable1
5234 variable2 := Hello
5235 y = $(subst 1,2,$(x))
5236 z = y
5237 a := $($($(z)))
5238 @end group
5239 @end example
5241 @noindent
5242 eventually defines @code{a} as @samp{Hello}.  It is doubtful that anyone
5243 would ever want to write a nested reference as convoluted as this one, but
5244 it works: @samp{$($($(z)))} expands to @samp{$($(y))} which becomes
5245 @samp{$($(subst 1,2,$(x)))}.  This gets the value @samp{variable1} from
5246 @code{x} and changes it by substitution to @samp{variable2}, so that the
5247 entire string becomes @samp{$(variable2)}, a simple variable reference
5248 whose value is @samp{Hello}.@refill
5250 A computed variable name need not consist entirely of a single variable
5251 reference.  It can contain several variable references, as well as some
5252 invariant text.  For example,
5254 @example
5255 @group
5256 a_dirs := dira dirb
5257 1_dirs := dir1 dir2
5258 @end group
5260 @group
5261 a_files := filea fileb
5262 1_files := file1 file2
5263 @end group
5265 @group
5266 ifeq "$(use_a)" "yes"
5267 a1 := a
5268 else
5269 a1 := 1
5270 endif
5271 @end group
5273 @group
5274 ifeq "$(use_dirs)" "yes"
5275 df := dirs
5276 else
5277 df := files
5278 endif
5280 dirs := $($(a1)_$(df))
5281 @end group
5282 @end example
5284 @noindent
5285 will give @code{dirs} the same value as @code{a_dirs}, @code{1_dirs},
5286 @code{a_files} or @code{1_files} depending on the settings of @code{use_a}
5287 and @code{use_dirs}.@refill
5289 Computed variable names can also be used in substitution references:
5291 @example
5292 @group
5293 a_objects := a.o b.o c.o
5294 1_objects := 1.o 2.o 3.o
5296 sources := $($(a1)_objects:.o=.c)
5297 @end group
5298 @end example
5300 @noindent
5301 defines @code{sources} as either @samp{a.c b.c c.c} or @samp{1.c 2.c 3.c},
5302 depending on the value of @code{a1}.
5304 The only restriction on this sort of use of nested variable references
5305 is that they cannot specify part of the name of a function to be called.
5306 This is because the test for a recognized function name is done before
5307 the expansion of nested references.  For example,
5309 @example
5310 @group
5311 ifdef do_sort
5312 func := sort
5313 else
5314 func := strip
5315 endif
5316 @end group
5318 @group
5319 bar := a d b g q c
5320 @end group
5322 @group
5323 foo := $($(func) $(bar))
5324 @end group
5325 @end example
5327 @noindent
5328 attempts to give @samp{foo} the value of the variable @samp{sort a d b g
5329 q c} or @samp{strip a d b g q c}, rather than giving @samp{a d b g q c}
5330 as the argument to either the @code{sort} or the @code{strip} function.
5331 This restriction could be removed in the future if that change is shown
5332 to be a good idea.
5334 You can also use computed variable names in the left-hand side of a
5335 variable assignment, or in a @code{define} directive, as in:
5337 @example
5338 dir = foo
5339 $(dir)_sources := $(wildcard $(dir)/*.c)
5340 define $(dir)_print
5341 lpr $($(dir)_sources)
5342 endef
5343 @end example
5345 @noindent
5346 This example defines the variables @samp{dir}, @samp{foo_sources}, and
5347 @samp{foo_print}.
5349 Note that @dfn{nested variable references} are quite different from
5350 @dfn{recursively expanded variables}
5351 (@pxref{Flavors, ,The Two Flavors of Variables}), though both are
5352 used together in complex ways when doing makefile programming.@refill
5354 @node Values, Setting, Advanced, Using Variables
5355 @section How Variables Get Their Values
5356 @cindex variables, how they get their values
5357 @cindex value, how a variable gets it
5359 Variables can get values in several different ways:
5361 @itemize @bullet
5362 @item
5363 You can specify an overriding value when you run @code{make}.
5364 @xref{Overriding, ,Overriding Variables}.
5366 @item
5367 You can specify a value in the makefile, either
5368 with an assignment (@pxref{Setting, ,Setting Variables}) or with a
5369 verbatim definition (@pxref{Defining, ,Defining Variables Verbatim}).@refill
5371 @item
5372 Variables in the environment become @code{make} variables.
5373 @xref{Environment, ,Variables from the Environment}.
5375 @item
5376 Several @dfn{automatic} variables are given new values for each rule.
5377 Each of these has a single conventional use.
5378 @xref{Automatic Variables}.
5380 @item
5381 Several variables have constant initial values.
5382 @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
5383 @end itemize
5385 @node Setting, Appending, Values, Using Variables
5386 @section Setting Variables
5387 @cindex setting variables
5388 @cindex variables, setting
5389 @cindex =
5390 @cindex :=
5391 @cindex ?=
5393 To set a variable from the makefile, write a line starting with the
5394 variable name followed by @samp{=} or @samp{:=}.  Whatever follows the
5395 @samp{=} or @samp{:=} on the line becomes the value.  For example,
5397 @example
5398 objects = main.o foo.o bar.o utils.o
5399 @end example
5401 @noindent
5402 defines a variable named @code{objects}.  Whitespace around the variable
5403 name and immediately after the @samp{=} is ignored.
5405 Variables defined with @samp{=} are @dfn{recursively expanded} variables.
5406 Variables defined with @samp{:=} are @dfn{simply expanded} variables; these
5407 definitions can contain variable references which will be expanded before
5408 the definition is made.  @xref{Flavors, ,The Two Flavors of Variables}.
5410 The variable name may contain function and variable references, which
5411 are expanded when the line is read to find the actual variable name to use.
5413 There is no limit on the length of the value of a variable except the
5414 amount of swapping space on the computer.  When a variable definition is
5415 long, it is a good idea to break it into several lines by inserting
5416 backslash-newline at convenient places in the definition.  This will not
5417 affect the functioning of @code{make}, but it will make the makefile easier
5418 to read.
5420 Most variable names are considered to have the empty string as a value if
5421 you have never set them.  Several variables have built-in initial values
5422 that are not empty, but you can set them in the usual ways
5423 (@pxref{Implicit Variables, ,Variables Used by Implicit Rules}).
5424 Several special variables are set
5425 automatically to a new value for each rule; these are called the
5426 @dfn{automatic} variables (@pxref{Automatic Variables}).
5428 If you'd like a variable to be set to a value only if it's not already
5429 set, then you can use the shorthand operator @samp{?=} instead of
5430 @samp{=}.  These two settings of the variable @samp{FOO} are identical
5431 (@pxref{Origin Function, ,The @code{origin} Function}):
5433 @example
5434 FOO ?= bar
5435 @end example
5437 @noindent
5440 @example
5441 ifeq ($(origin FOO), undefined)
5442 FOO = bar
5443 endif
5444 @end example
5446 @node Appending, Override Directive, Setting, Using Variables
5447 @section Appending More Text to Variables
5448 @cindex +=
5449 @cindex appending to variables
5450 @cindex variables, appending to
5452 Often it is useful to add more text to the value of a variable already defined.
5453 You do this with a line containing @samp{+=}, like this:
5455 @example
5456 objects += another.o
5457 @end example
5459 @noindent
5460 This takes the value of the variable @code{objects}, and adds the text
5461 @samp{another.o} to it (preceded by a single space).  Thus:
5463 @example
5464 objects = main.o foo.o bar.o utils.o
5465 objects += another.o
5466 @end example
5468 @noindent
5469 sets @code{objects} to @samp{main.o foo.o bar.o utils.o another.o}.
5471 Using @samp{+=} is similar to:
5473 @example
5474 objects = main.o foo.o bar.o utils.o
5475 objects := $(objects) another.o
5476 @end example
5478 @noindent
5479 but differs in ways that become important when you use more complex values.
5481 When the variable in question has not been defined before, @samp{+=}
5482 acts just like normal @samp{=}: it defines a recursively-expanded
5483 variable.  However, when there @emph{is} a previous definition, exactly
5484 what @samp{+=} does depends on what flavor of variable you defined
5485 originally.  @xref{Flavors, ,The Two Flavors of Variables}, for an
5486 explanation of the two flavors of variables.
5488 When you add to a variable's value with @samp{+=}, @code{make} acts
5489 essentially as if you had included the extra text in the initial
5490 definition of the variable.  If you defined it first with @samp{:=},
5491 making it a simply-expanded variable, @samp{+=} adds to that
5492 simply-expanded definition, and expands the new text before appending it
5493 to the old value just as @samp{:=} does
5494 (see @ref{Setting, ,Setting Variables}, for a full explanation of @samp{:=}).
5495 In fact,
5497 @example
5498 variable := value
5499 variable += more
5500 @end example
5502 @noindent
5503 is exactly equivalent to:
5505 @noindent
5506 @example
5507 variable := value
5508 variable := $(variable) more
5509 @end example
5511 On the other hand, when you use @samp{+=} with a variable that you defined
5512 first to be recursively-expanded using plain @samp{=}, @code{make} does
5513 something a bit different.  Recall that when you define a
5514 recursively-expanded variable, @code{make} does not expand the value you set
5515 for variable and function references immediately.  Instead it stores the text
5516 verbatim, and saves these variable and function references to be expanded
5517 later, when you refer to the new variable (@pxref{Flavors, ,The Two Flavors
5518 of Variables}).  When you use @samp{+=} on a recursively-expanded variable,
5519 it is this unexpanded text to which @code{make} appends the new text you
5520 specify.
5522 @example
5523 @group
5524 variable = value
5525 variable += more
5526 @end group
5527 @end example
5529 @noindent
5530 is roughly equivalent to:
5532 @example
5533 @group
5534 temp = value
5535 variable = $(temp) more
5536 @end group
5537 @end example
5539 @noindent
5540 except that of course it never defines a variable called @code{temp}.
5541 The importance of this comes when the variable's old value contains
5542 variable references.  Take this common example:
5544 @example
5545 CFLAGS = $(includes) -O
5546 @dots{}
5547 CFLAGS += -pg # enable profiling
5548 @end example
5550 @noindent
5551 The first line defines the @code{CFLAGS} variable with a reference to another
5552 variable, @code{includes}.  (@code{CFLAGS} is used by the rules for C
5553 compilation; @pxref{Catalogue of Rules, ,Catalogue of Implicit Rules}.)
5554 Using @samp{=} for the definition makes @code{CFLAGS} a recursively-expanded
5555 variable, meaning @w{@samp{$(includes) -O}} is @emph{not} expanded when
5556 @code{make} processes the definition of @code{CFLAGS}.  Thus, @code{includes}
5557 need not be defined yet for its value to take effect.  It only has to be
5558 defined before any reference to @code{CFLAGS}.  If we tried to append to the
5559 value of @code{CFLAGS} without using @samp{+=}, we might do it like this:
5561 @example
5562 CFLAGS := $(CFLAGS) -pg # enable profiling
5563 @end example
5565 @noindent
5566 This is pretty close, but not quite what we want.  Using @samp{:=}
5567 redefines @code{CFLAGS} as a simply-expanded variable; this means
5568 @code{make} expands the text @w{@samp{$(CFLAGS) -pg}} before setting the
5569 variable.  If @code{includes} is not yet defined, we get @w{@samp{ -O
5570 -pg}}, and a later definition of @code{includes} will have no effect.
5571 Conversely, by using @samp{+=} we set @code{CFLAGS} to the
5572 @emph{unexpanded} value @w{@samp{$(includes) -O -pg}}.  Thus we preserve
5573 the reference to @code{includes}, so if that variable gets defined at
5574 any later point, a reference like @samp{$(CFLAGS)} still uses its
5575 value.
5577 @node Override Directive, Defining, Appending, Using Variables
5578 @section The @code{override} Directive
5579 @findex override
5580 @cindex overriding with @code{override}
5581 @cindex variables, overriding
5583 If a variable has been set with a command argument
5584 (@pxref{Overriding, ,Overriding Variables}),
5585 then ordinary assignments in the makefile are ignored.  If you want to set
5586 the variable in the makefile even though it was set with a command
5587 argument, you can use an @code{override} directive, which is a line that
5588 looks like this:@refill
5590 @example
5591 override @var{variable} = @var{value}
5592 @end example
5594 @noindent
5597 @example
5598 override @var{variable} := @var{value}
5599 @end example
5601 To append more text to a variable defined on the command line, use:
5603 @example
5604 override @var{variable} += @var{more text}
5605 @end example
5607 @noindent
5608 @xref{Appending, ,Appending More Text to Variables}.
5610 The @code{override} directive was not invented for escalation in the war
5611 between makefiles and command arguments.  It was invented so you can alter
5612 and add to values that the user specifies with command arguments.
5614 For example, suppose you always want the @samp{-g} switch when you run the
5615 C compiler, but you would like to allow the user to specify the other
5616 switches with a command argument just as usual.  You could use this
5617 @code{override} directive:
5619 @example
5620 override CFLAGS += -g
5621 @end example
5623 You can also use @code{override} directives with @code{define} directives.
5624 This is done as you might expect:
5626 @example
5627 override define foo
5629 endef
5630 @end example
5632 @noindent
5633 @iftex
5634 See the next section for information about @code{define}.
5635 @end iftex
5636 @ifnottex
5637 @xref{Defining, ,Defining Variables Verbatim}.
5638 @end ifnottex
5640 @node Defining, Environment, Override Directive, Using Variables
5641 @section Defining Variables Verbatim
5642 @findex define
5643 @findex endef
5644 @cindex verbatim variable definition
5645 @cindex defining variables verbatim
5646 @cindex variables, defining verbatim
5648 Another way to set the value of a variable is to use the @code{define}
5649 directive.  This directive has an unusual syntax which allows newline
5650 characters to be included in the value, which is convenient for defining
5651 both canned sequences of commands
5652 (@pxref{Sequences, ,Defining Canned Command Sequences}), and also
5653 sections of makefile syntax to use with @code{eval} (@pxref{Eval Function}).
5655 The @code{define} directive is followed on the same line by the name of the
5656 variable and nothing more.  The value to give the variable appears on the
5657 following lines.  The end of the value is marked by a line containing just
5658 the word @code{endef}.  Aside from this difference in syntax, @code{define}
5659 works just like @samp{=}: it creates a recursively-expanded variable
5660 (@pxref{Flavors, ,The Two Flavors of Variables}).
5661 The variable name may contain function and variable references, which
5662 are expanded when the directive is read to find the actual variable name
5663 to use.
5665 You may nest @code{define} directives: @code{make} will keep track of
5666 nested directives and report an error if they are not all properly
5667 closed with @code{endef}.  Note that lines beginning with tab
5668 characters are considered part of a command script, so any
5669 @code{define} or @code{endef} strings appearing on such a line will
5670 not be considered @code{make} operators.
5672 @example
5673 define two-lines
5674 echo foo
5675 echo $(bar)
5676 endef
5677 @end example
5679 The value in an ordinary assignment cannot contain a newline; but the
5680 newlines that separate the lines of the value in a @code{define} become
5681 part of the variable's value (except for the final newline which precedes
5682 the @code{endef} and is not considered part of the value).@refill
5684 @need 800
5685 When used in a command script, the previous example is functionally
5686 equivalent to this:
5688 @example
5689 two-lines = echo foo; echo $(bar)
5690 @end example
5692 @noindent
5693 since two commands separated by semicolon behave much like two separate
5694 shell commands.  However, note that using two separate lines means
5695 @code{make} will invoke the shell twice, running an independent subshell
5696 for each line.  @xref{Execution, ,Command Execution}.
5698 If you want variable definitions made with @code{define} to take
5699 precedence over command-line variable definitions, you can use the
5700 @code{override} directive together with @code{define}:
5702 @example
5703 override define two-lines
5705 $(bar)
5706 endef
5707 @end example
5709 @noindent
5710 @xref{Override Directive, ,The @code{override} Directive}.
5712 @node Environment, Target-specific, Defining, Using Variables
5713 @section Variables from the Environment
5715 @cindex variables, environment
5716 @cindex environment
5717 Variables in @code{make} can come from the environment in which
5718 @code{make} is run.  Every environment variable that @code{make} sees
5719 when it starts up is transformed into a @code{make} variable with the
5720 same name and value.  However, an explicit assignment in the makefile,
5721 or with a command argument, overrides the environment.  (If the
5722 @samp{-e} flag is specified, then values from the environment override
5723 assignments in the makefile.  @xref{Options Summary, ,Summary of
5724 Options}.  But this is not recommended practice.)
5726 Thus, by setting the variable @code{CFLAGS} in your environment, you can
5727 cause all C compilations in most makefiles to use the compiler switches you
5728 prefer.  This is safe for variables with standard or conventional meanings
5729 because you know that no makefile will use them for other things.  (Note
5730 this is not totally reliable; some makefiles set @code{CFLAGS} explicitly
5731 and therefore are not affected by the value in the environment.)
5733 When @code{make} runs a command script, variables defined in the
5734 makefile are placed into the environment of that command.  This allows
5735 you to pass values to sub-@code{make} invocations (@pxref{Recursion,
5736 ,Recursive Use of @code{make}}).  By default, only variables that came
5737 from the environment or the command line are passed to recursive
5738 invocations.  You can use the @code{export} directive to pass other
5739 variables.  @xref{Variables/Recursion, , Communicating Variables to a
5740 Sub-@code{make}}, for full details.
5742 Other use of variables from the environment is not recommended.  It is not
5743 wise for makefiles to depend for their functioning on environment variables
5744 set up outside their control, since this would cause different users to get
5745 different results from the same makefile.  This is against the whole
5746 purpose of most makefiles.
5748 @cindex SHELL, import from environment
5749 Such problems would be especially likely with the variable
5750 @code{SHELL}, which is normally present in the environment to specify
5751 the user's choice of interactive shell.  It would be very undesirable
5752 for this choice to affect @code{make}; so, @code{make} handles the
5753 @code{SHELL} environment variable in a special way; see @ref{Choosing
5754 the Shell}.@refill
5756 @node Target-specific, Pattern-specific, Environment, Using Variables
5757 @section Target-specific Variable Values
5758 @cindex target-specific variables
5759 @cindex variables, target-specific
5761 Variable values in @code{make} are usually global; that is, they are the
5762 same regardless of where they are evaluated (unless they're reset, of
5763 course).  One exception to that is automatic variables
5764 (@pxref{Automatic Variables}).
5766 The other exception is @dfn{target-specific variable values}.  This
5767 feature allows you to define different values for the same variable,
5768 based on the target that @code{make} is currently building.  As with
5769 automatic variables, these values are only available within the context
5770 of a target's command script (and in other target-specific assignments).
5772 Set a target-specific variable value like this:
5774 @example
5775 @var{target} @dots{} : @var{variable-assignment}
5776 @end example
5778 @noindent
5779 or like this:
5781 @example
5782 @var{target} @dots{} : override @var{variable-assignment}
5783 @end example
5785 @noindent
5786 or like this:
5788 @example
5789 @var{target} @dots{} : export @var{variable-assignment}
5790 @end example
5792 Multiple @var{target} values create a target-specific variable value for
5793 each member of the target list individually.
5795 The @var{variable-assignment} can be any valid form of assignment;
5796 recursive (@samp{=}), static (@samp{:=}), appending (@samp{+=}), or
5797 conditional (@samp{?=}).  All variables that appear within the
5798 @var{variable-assignment} are evaluated within the context of the
5799 target: thus, any previously-defined target-specific variable values
5800 will be in effect.  Note that this variable is actually distinct from
5801 any ``global'' value: the two variables do not have to have the same
5802 flavor (recursive vs.@: static).
5804 Target-specific variables have the same priority as any other makefile
5805 variable.  Variables provided on the command-line (and in the
5806 environment if the @samp{-e} option is in force) will take precedence.
5807 Specifying the @code{override} directive will allow the target-specific
5808 variable value to be preferred.
5810 There is one more special feature of target-specific variables: when
5811 you define a target-specific variable that variable value is also in
5812 effect for all prerequisites of this target, and all their
5813 prerequisites, etc.@: (unless those prerequisites override that variable
5814 with their own target-specific variable value).  So, for example, a
5815 statement like this:
5817 @example
5818 prog : CFLAGS = -g
5819 prog : prog.o foo.o bar.o
5820 @end example
5822 @noindent
5823 will set @code{CFLAGS} to @samp{-g} in the command script for
5824 @file{prog}, but it will also set @code{CFLAGS} to @samp{-g} in the
5825 command scripts that create @file{prog.o}, @file{foo.o}, and
5826 @file{bar.o}, and any command scripts which create their
5827 prerequisites.
5829 Be aware that a given prerequisite will only be built once per
5830 invocation of make, at most.  If the same file is a prerequisite of
5831 multiple targets, and each of those targets has a different value for
5832 the same target-specific variable, then the first target to be built
5833 will cause that prerequisite to be built and the prerequisite will
5834 inherit the target-specific value from the first target.  It will
5835 ignore the target-specific values from any other targets.
5837 @node Pattern-specific,  , Target-specific, Using Variables
5838 @section Pattern-specific Variable Values
5839 @cindex pattern-specific variables
5840 @cindex variables, pattern-specific
5842 In addition to target-specific variable values
5843 (@pxref{Target-specific, ,Target-specific Variable Values}), GNU
5844 @code{make} supports pattern-specific variable values.  In this form,
5845 the variable is defined for any target that matches the pattern
5846 specified.  If a target matches more than one pattern, all the
5847 matching pattern-specific variables are interpreted in the order in
5848 which they were defined in the makefile, and collected together into
5849 one set.  Variables defined in this way are searched after any
5850 target-specific variables defined explicitly for that target, and
5851 before target-specific variables defined for the parent target.
5853 Set a pattern-specific variable value like this:
5855 @example
5856 @var{pattern} @dots{} : @var{variable-assignment}
5857 @end example
5859 @noindent
5860 or like this:
5862 @example
5863 @var{pattern} @dots{} : override @var{variable-assignment}
5864 @end example
5866 @noindent
5867 where @var{pattern} is a %-pattern.  As with target-specific variable
5868 values, multiple @var{pattern} values create a pattern-specific variable
5869 value for each pattern individually.  The @var{variable-assignment} can
5870 be any valid form of assignment.  Any command-line variable setting will
5871 take precedence, unless @code{override} is specified.
5873 For example:
5875 @example
5876 %.o : CFLAGS = -O
5877 @end example
5879 @noindent
5880 will assign @code{CFLAGS} the value of @samp{-O} for all targets
5881 matching the pattern @code{%.o}.
5883 @node Conditionals, Functions, Using Variables, Top
5884 @chapter Conditional Parts of Makefiles
5886 @cindex conditionals
5887 A @dfn{conditional} causes part of a makefile to be obeyed or ignored
5888 depending on the values of variables.  Conditionals can compare the
5889 value of one variable to another, or the value of a variable to
5890 a constant string.  Conditionals control what @code{make} actually
5891 ``sees'' in the makefile, so they @emph{cannot} be used to control shell
5892 commands at the time of execution.@refill
5894 @menu
5895 * Conditional Example::         Example of a conditional
5896 * Conditional Syntax::          The syntax of conditionals.
5897 * Testing Flags::               Conditionals that test flags.
5898 @end menu
5900 @node Conditional Example, Conditional Syntax, Conditionals, Conditionals
5901 @section Example of a Conditional
5903 The following example of a conditional tells @code{make} to use one set
5904 of libraries if the @code{CC} variable is @samp{gcc}, and a different
5905 set of libraries otherwise.  It works by controlling which of two
5906 command lines will be used as the command for a rule.  The result is
5907 that @samp{CC=gcc} as an argument to @code{make} changes not only which
5908 compiler is used but also which libraries are linked.
5910 @example
5911 libs_for_gcc = -lgnu
5912 normal_libs =
5914 foo: $(objects)
5915 ifeq ($(CC),gcc)
5916         $(CC) -o foo $(objects) $(libs_for_gcc)
5917 else
5918         $(CC) -o foo $(objects) $(normal_libs)
5919 endif
5920 @end example
5922 This conditional uses three directives: one @code{ifeq}, one @code{else}
5923 and one @code{endif}.
5925 The @code{ifeq} directive begins the conditional, and specifies the
5926 condition.  It contains two arguments, separated by a comma and surrounded
5927 by parentheses.  Variable substitution is performed on both arguments and
5928 then they are compared.  The lines of the makefile following the
5929 @code{ifeq} are obeyed if the two arguments match; otherwise they are
5930 ignored.
5932 The @code{else} directive causes the following lines to be obeyed if the
5933 previous conditional failed.  In the example above, this means that the
5934 second alternative linking command is used whenever the first alternative
5935 is not used.  It is optional to have an @code{else} in a conditional.
5937 The @code{endif} directive ends the conditional.  Every conditional must
5938 end with an @code{endif}.  Unconditional makefile text follows.
5940 As this example illustrates, conditionals work at the textual level:
5941 the lines of the conditional are treated as part of the makefile, or
5942 ignored, according to the condition.  This is why the larger syntactic
5943 units of the makefile, such as rules, may cross the beginning or the
5944 end of the conditional.
5946 When the variable @code{CC} has the value @samp{gcc}, the above example has
5947 this effect:
5949 @example
5950 foo: $(objects)
5951         $(CC) -o foo $(objects) $(libs_for_gcc)
5952 @end example
5954 @noindent
5955 When the variable @code{CC} has any other value, the effect is this:
5957 @example
5958 foo: $(objects)
5959         $(CC) -o foo $(objects) $(normal_libs)
5960 @end example
5962 Equivalent results can be obtained in another way by conditionalizing a
5963 variable assignment and then using the variable unconditionally:
5965 @example
5966 libs_for_gcc = -lgnu
5967 normal_libs =
5969 ifeq ($(CC),gcc)
5970   libs=$(libs_for_gcc)
5971 else
5972   libs=$(normal_libs)
5973 endif
5975 foo: $(objects)
5976         $(CC) -o foo $(objects) $(libs)
5977 @end example
5979 @node Conditional Syntax, Testing Flags, Conditional Example, Conditionals
5980 @section Syntax of Conditionals
5981 @findex ifdef
5982 @findex ifeq
5983 @findex ifndef
5984 @findex ifneq
5985 @findex else
5986 @findex endif
5988 The syntax of a simple conditional with no @code{else} is as follows:
5990 @example
5991 @var{conditional-directive}
5992 @var{text-if-true}
5993 endif
5994 @end example
5996 @noindent
5997 The @var{text-if-true} may be any lines of text, to be considered as part
5998 of the makefile if the condition is true.  If the condition is false, no
5999 text is used instead.
6001 The syntax of a complex conditional is as follows:
6003 @example
6004 @var{conditional-directive}
6005 @var{text-if-true}
6006 else
6007 @var{text-if-false}
6008 endif
6009 @end example
6013 @example
6014 @var{conditional-directive}
6015 @var{text-if-one-is-true}
6016 else @var{conditional-directive}
6017 @var{text-if-true}
6018 else
6019 @var{text-if-false}
6020 endif
6021 @end example
6023 @noindent
6024 There can be as many ``@code{else} @var{conditional-directive}''
6025 clauses as necessary.  Once a given condition is true,
6026 @var{text-if-true} is used and no other clause is used; if no
6027 condition is true then @var{text-if-false} is used.  The
6028 @var{text-if-true} and @var{text-if-false} can be any number of lines
6029 of text.
6031 The syntax of the @var{conditional-directive} is the same whether the
6032 conditional is simple or complex; after an @code{else} or not.  There
6033 are four different directives that test different conditions.  Here is
6034 a table of them:
6036 @table @code
6037 @item ifeq (@var{arg1}, @var{arg2})
6038 @itemx ifeq '@var{arg1}' '@var{arg2}'
6039 @itemx ifeq "@var{arg1}" "@var{arg2}"
6040 @itemx ifeq "@var{arg1}" '@var{arg2}'
6041 @itemx ifeq '@var{arg1}' "@var{arg2}"
6042 Expand all variable references in @var{arg1} and @var{arg2} and
6043 compare them.  If they are identical, the @var{text-if-true} is
6044 effective; otherwise, the @var{text-if-false}, if any, is effective.
6046 Often you want to test if a variable has a non-empty value.  When the
6047 value results from complex expansions of variables and functions,
6048 expansions you would consider empty may actually contain whitespace
6049 characters and thus are not seen as empty.  However, you can use the
6050 @code{strip} function (@pxref{Text Functions}) to avoid interpreting
6051 whitespace as a non-empty value.  For example:
6053 @example
6054 @group
6055 ifeq ($(strip $(foo)),)
6056 @var{text-if-empty}
6057 endif
6058 @end group
6059 @end example
6061 @noindent
6062 will evaluate @var{text-if-empty} even if the expansion of
6063 @code{$(foo)} contains whitespace characters.
6065 @item ifneq (@var{arg1}, @var{arg2})
6066 @itemx ifneq '@var{arg1}' '@var{arg2}'
6067 @itemx ifneq "@var{arg1}" "@var{arg2}"
6068 @itemx ifneq "@var{arg1}" '@var{arg2}'
6069 @itemx ifneq '@var{arg1}' "@var{arg2}"
6070 Expand all variable references in @var{arg1} and @var{arg2} and
6071 compare them.  If they are different, the @var{text-if-true} is
6072 effective; otherwise, the @var{text-if-false}, if any, is effective.
6074 @item ifdef @var{variable-name}
6075 The @code{ifdef} form takes the @emph{name} of a variable as its
6076 argument, not a reference to a variable.  The value of that variable
6077 has a non-empty value, the @var{text-if-true} is effective; otherwise,
6078 the @var{text-if-false}, if any, is effective.  Variables that have
6079 never been defined have an empty value.  The text @var{variable-name}
6080 is expanded, so it could be a variable or function that expands
6081 to the name of a variable.  For example:
6083 @example
6084 bar = true
6085 foo = bar
6086 ifdef $(foo)
6087 frobozz = yes
6088 endif
6089 @end example
6091 The variable reference @code{$(foo)} is expanded, yielding @code{bar},
6092 which is considered to be the name of a variable.  The variable
6093 @code{bar} is not expanded, but its value is examined to determine if
6094 it is non-empty.
6096 Note that @code{ifdef} only tests whether a variable has a value.  It
6097 does not expand the variable to see if that value is nonempty.
6098 Consequently, tests using @code{ifdef} return true for all definitions
6099 except those like @code{foo =}.  To test for an empty value, use
6100 @w{@code{ifeq ($(foo),)}}.  For example,
6102 @example
6103 bar =
6104 foo = $(bar)
6105 ifdef foo
6106 frobozz = yes
6107 else
6108 frobozz = no
6109 endif
6110 @end example
6112 @noindent
6113 sets @samp{frobozz} to @samp{yes}, while:
6115 @example
6116 foo =
6117 ifdef foo
6118 frobozz = yes
6119 else
6120 frobozz = no
6121 endif
6122 @end example
6124 @noindent
6125 sets @samp{frobozz} to @samp{no}.
6127 @item ifndef @var{variable-name}
6128 If the variable @var{variable-name} has an empty value, the
6129 @var{text-if-true} is effective; otherwise, the @var{text-if-false},
6130 if any, is effective.  The rules for expansion and testing of
6131 @var{variable-name} are identical to the @code{ifdef} directive.
6132 @end table
6134 Extra spaces are allowed and ignored at the beginning of the conditional
6135 directive line, but a tab is not allowed.  (If the line begins with a tab,
6136 it will be considered a command for a rule.)  Aside from this, extra spaces
6137 or tabs may be inserted with no effect anywhere except within the directive
6138 name or within an argument.  A comment starting with @samp{#} may appear at
6139 the end of the line.
6141 The other two directives that play a part in a conditional are @code{else}
6142 and @code{endif}.  Each of these directives is written as one word, with no
6143 arguments.  Extra spaces are allowed and ignored at the beginning of the
6144 line, and spaces or tabs at the end.  A comment starting with @samp{#} may
6145 appear at the end of the line.
6147 Conditionals affect which lines of the makefile @code{make} uses.  If
6148 the condition is true, @code{make} reads the lines of the
6149 @var{text-if-true} as part of the makefile; if the condition is false,
6150 @code{make} ignores those lines completely.  It follows that syntactic
6151 units of the makefile, such as rules, may safely be split across the
6152 beginning or the end of the conditional.@refill
6154 @code{make} evaluates conditionals when it reads a makefile.
6155 Consequently, you cannot use automatic variables in the tests of
6156 conditionals because they are not defined until commands are run
6157 (@pxref{Automatic Variables}).
6159 To prevent intolerable confusion, it is not permitted to start a
6160 conditional in one makefile and end it in another.  However, you may
6161 write an @code{include} directive within a conditional, provided you do
6162 not attempt to terminate the conditional inside the included file.
6164 @node Testing Flags,  , Conditional Syntax, Conditionals
6165 @section Conditionals that Test Flags
6167 You can write a conditional that tests @code{make} command flags such as
6168 @samp{-t} by using the variable @code{MAKEFLAGS} together with the
6169 @code{findstring} function
6170 (@pxref{Text Functions, , Functions for String Substitution and Analysis}).
6171 This is useful when @code{touch} is not enough to make a file appear up
6172 to date.
6174 The @code{findstring} function determines whether one string appears as a
6175 substring of another.  If you want to test for the @samp{-t} flag,
6176 use @samp{t} as the first string and the value of @code{MAKEFLAGS} as
6177 the other.
6179 For example, here is how to arrange to use @samp{ranlib -t} to finish
6180 marking an archive file up to date:
6182 @example
6183 archive.a: @dots{}
6184 ifneq (,$(findstring t,$(MAKEFLAGS)))
6185         +touch archive.a
6186         +ranlib -t archive.a
6187 else
6188         ranlib archive.a
6189 endif
6190 @end example
6192 @noindent
6193 The @samp{+} prefix marks those command lines as ``recursive'' so
6194 that they will be executed despite use of the @samp{-t} flag.
6195 @xref{Recursion, ,Recursive Use of @code{make}}.
6197 @node Functions, Running, Conditionals, Top
6198 @chapter Functions for Transforming Text
6199 @cindex functions
6201 @dfn{Functions} allow you to do text processing in the makefile to compute
6202 the files to operate on or the commands to use.  You use a function in a
6203 @dfn{function call}, where you give the name of the function and some text
6204 (the @dfn{arguments}) for the function to operate on.  The result of the
6205 function's processing is substituted into the makefile at the point of the
6206 call, just as a variable might be substituted.
6208 @menu
6209 * Syntax of Functions::         How to write a function call.
6210 * Text Functions::              General-purpose text manipulation functions.
6211 * File Name Functions::         Functions for manipulating file names.
6212 * Conditional Functions::       Functions that implement conditions.
6213 * Foreach Function::            Repeat some text with controlled variation.
6214 * Call Function::               Expand a user-defined function.
6215 * Value Function::              Return the un-expanded value of a variable.
6216 * Eval Function::               Evaluate the arguments as makefile syntax.
6217 * Origin Function::             Find where a variable got its value.
6218 * Flavor Function::             Find out the flavor of a variable.
6219 * Shell Function::              Substitute the output of a shell command.
6220 * Make Control Functions::      Functions that control how make runs.
6221 @end menu
6223 @node Syntax of Functions, Text Functions, Functions, Functions
6224 @section Function Call Syntax
6225 @cindex @code{$}, in function call
6226 @cindex dollar sign (@code{$}), in function call
6227 @cindex arguments of functions
6228 @cindex functions, syntax of
6230 A function call resembles a variable reference.  It looks like this:
6232 @example
6233 $(@var{function} @var{arguments})
6234 @end example
6236 @noindent
6237 or like this:
6239 @example
6240 $@{@var{function} @var{arguments}@}
6241 @end example
6243 Here @var{function} is a function name; one of a short list of names
6244 that are part of @code{make}.  You can also essentially create your own
6245 functions by using the @code{call} builtin function.
6247 The @var{arguments} are the arguments of the function.  They are
6248 separated from the function name by one or more spaces or tabs, and if
6249 there is more than one argument, then they are separated by commas.
6250 Such whitespace and commas are not part of an argument's value.  The
6251 delimiters which you use to surround the function call, whether
6252 parentheses or braces, can appear in an argument only in matching pairs;
6253 the other kind of delimiters may appear singly.  If the arguments
6254 themselves contain other function calls or variable references, it is
6255 wisest to use the same kind of delimiters for all the references; write
6256 @w{@samp{$(subst a,b,$(x))}}, not @w{@samp{$(subst a,b,$@{x@})}}.  This
6257 is because it is clearer, and because only one type of delimiter is
6258 matched to find the end of the reference.
6260 The text written for each argument is processed by substitution of
6261 variables and function calls to produce the argument value, which
6262 is the text on which the function acts.  The substitution is done in the
6263 order in which the arguments appear.
6265 Commas and unmatched parentheses or braces cannot appear in the text of an
6266 argument as written; leading spaces cannot appear in the text of the first
6267 argument as written.  These characters can be put into the argument value
6268 by variable substitution.  First define variables @code{comma} and
6269 @code{space} whose values are isolated comma and space characters, then
6270 substitute these variables where such characters are wanted, like this:
6272 @example
6273 @group
6274 comma:= ,
6275 empty:=
6276 space:= $(empty) $(empty)
6277 foo:= a b c
6278 bar:= $(subst $(space),$(comma),$(foo))
6279 # @r{bar is now `a,b,c'.}
6280 @end group
6281 @end example
6283 @noindent
6284 Here the @code{subst} function replaces each space with a comma, through
6285 the value of @code{foo}, and substitutes the result.
6287 @node Text Functions, File Name Functions, Syntax of Functions, Functions
6288 @section Functions for String Substitution and Analysis
6289 @cindex functions, for text
6291 Here are some functions that operate on strings:
6293 @table @code
6294 @item $(subst @var{from},@var{to},@var{text})
6295 @findex subst
6296 Performs a textual replacement on the text @var{text}: each occurrence
6297 of @var{from} is replaced by @var{to}.  The result is substituted for
6298 the function call.  For example,
6300 @example
6301 $(subst ee,EE,feet on the street)
6302 @end example
6304 substitutes the string @samp{fEEt on the strEEt}.
6306 @item $(patsubst @var{pattern},@var{replacement},@var{text})
6307 @findex patsubst
6308 Finds whitespace-separated words in @var{text} that match
6309 @var{pattern} and replaces them with @var{replacement}.  Here
6310 @var{pattern} may contain a @samp{%} which acts as a wildcard,
6311 matching any number of any characters within a word.  If
6312 @var{replacement} also contains a @samp{%}, the @samp{%} is replaced
6313 by the text that matched the @samp{%} in @var{pattern}.  Only the first
6314 @samp{%} in the @var{pattern} and @var{replacement} is treated this
6315 way; any subsequent @samp{%} is unchanged.@refill
6317 @cindex @code{%}, quoting in @code{patsubst}
6318 @cindex @code{%}, quoting with @code{\} (backslash)
6319 @cindex @code{\} (backslash), to quote @code{%}
6320 @cindex backslash (@code{\}), to quote @code{%}
6321 @cindex quoting @code{%}, in @code{patsubst}
6322 @samp{%} characters in @code{patsubst} function invocations can be
6323 quoted with preceding backslashes (@samp{\}).  Backslashes that would
6324 otherwise quote @samp{%} characters can be quoted with more backslashes.
6325 Backslashes that quote @samp{%} characters or other backslashes are
6326 removed from the pattern before it is compared file names or has a stem
6327 substituted into it.  Backslashes that are not in danger of quoting
6328 @samp{%} characters go unmolested.  For example, the pattern
6329 @file{the\%weird\\%pattern\\} has @samp{the%weird\} preceding the
6330 operative @samp{%} character, and @samp{pattern\\} following it.  The
6331 final two backslashes are left alone because they cannot affect any
6332 @samp{%} character.@refill
6334 Whitespace between words is folded into single space characters;
6335 leading and trailing whitespace is discarded.
6337 For example,
6339 @example
6340 $(patsubst %.c,%.o,x.c.c bar.c)
6341 @end example
6343 @noindent
6344 produces the value @samp{x.c.o bar.o}.
6346 Substitution references (@pxref{Substitution Refs, ,Substitution
6347 References}) are a simpler way to get the effect of the @code{patsubst}
6348 function:
6350 @example
6351 $(@var{var}:@var{pattern}=@var{replacement})
6352 @end example
6354 @noindent
6355 is equivalent to
6357 @example
6358 $(patsubst @var{pattern},@var{replacement},$(@var{var}))
6359 @end example
6361 The second shorthand simplifies one of the most common uses of
6362 @code{patsubst}: replacing the suffix at the end of file names.
6364 @example
6365 $(@var{var}:@var{suffix}=@var{replacement})
6366 @end example
6368 @noindent
6369 is equivalent to
6371 @example
6372 $(patsubst %@var{suffix},%@var{replacement},$(@var{var}))
6373 @end example
6375 @noindent
6376 For example, you might have a list of object files:
6378 @example
6379 objects = foo.o bar.o baz.o
6380 @end example
6382 @noindent
6383 To get the list of corresponding source files, you could simply write:
6385 @example
6386 $(objects:.o=.c)
6387 @end example
6389 @noindent
6390 instead of using the general form:
6392 @example
6393 $(patsubst %.o,%.c,$(objects))
6394 @end example
6396 @item $(strip @var{string})
6397 @cindex stripping whitespace
6398 @cindex whitespace, stripping
6399 @cindex spaces, stripping
6400 @findex strip
6401 Removes leading and trailing whitespace from @var{string} and replaces
6402 each internal sequence of one or more whitespace characters with a
6403 single space.  Thus, @samp{$(strip a b  c )} results in @w{@samp{a b c}}.
6405 The function @code{strip} can be very useful when used in conjunction
6406 with conditionals.  When comparing something with the empty string
6407 @samp{} using @code{ifeq} or @code{ifneq}, you usually want a string of
6408 just whitespace to match the empty string (@pxref{Conditionals}).
6410 Thus, the following may fail to have the desired results:
6412 @example
6413 .PHONY: all
6414 ifneq   "$(needs_made)" ""
6415 all: $(needs_made)
6416 else
6417 all:;@@echo 'Nothing to make!'
6418 endif
6419 @end example
6421 @noindent
6422 Replacing the variable reference @w{@samp{$(needs_made)}} with the
6423 function call @w{@samp{$(strip $(needs_made))}} in the @code{ifneq}
6424 directive would make it more robust.@refill
6426 @item $(findstring @var{find},@var{in})
6427 @findex findstring
6428 @cindex searching for strings
6429 @cindex finding strings
6430 @cindex strings, searching for
6431 Searches @var{in} for an occurrence of @var{find}.  If it occurs, the
6432 value is @var{find}; otherwise, the value is empty.  You can use this
6433 function in a conditional to test for the presence of a specific
6434 substring in a given string.  Thus, the two examples,
6436 @example
6437 $(findstring a,a b c)
6438 $(findstring a,b c)
6439 @end example
6441 @noindent
6442 produce the values @samp{a} and @samp{} (the empty string),
6443 respectively.  @xref{Testing Flags}, for a practical application of
6444 @code{findstring}.@refill
6446 @need 750
6447 @findex filter
6448 @cindex filtering words
6449 @cindex words, filtering
6450 @item $(filter @var{pattern}@dots{},@var{text})
6451 Returns all whitespace-separated words in @var{text} that @emph{do} match
6452 any of the @var{pattern} words, removing any words that @emph{do not}
6453 match.  The patterns are written using @samp{%}, just like the patterns
6454 used in the @code{patsubst} function above.@refill
6456 The @code{filter} function can be used to separate out different types
6457 of strings (such as file names) in a variable.  For example:
6459 @example
6460 sources := foo.c bar.c baz.s ugh.h
6461 foo: $(sources)
6462         cc $(filter %.c %.s,$(sources)) -o foo
6463 @end example
6465 @noindent
6466 says that @file{foo} depends of @file{foo.c}, @file{bar.c},
6467 @file{baz.s} and @file{ugh.h} but only @file{foo.c}, @file{bar.c} and
6468 @file{baz.s} should be specified in the command to the
6469 compiler.@refill
6471 @item $(filter-out @var{pattern}@dots{},@var{text})
6472 @findex filter-out
6473 @cindex filtering out words
6474 @cindex words, filtering out
6475 Returns all whitespace-separated words in @var{text} that @emph{do not}
6476 match any of the @var{pattern} words, removing the words that @emph{do}
6477 match one or more.  This is the exact opposite of the @code{filter}
6478 function.@refill
6480 For example, given:
6482 @example
6483 @group
6484 objects=main1.o foo.o main2.o bar.o
6485 mains=main1.o main2.o
6486 @end group
6487 @end example
6489 @noindent
6490 the following generates a list which contains all the object files not
6491 in @samp{mains}:
6493 @example
6494 $(filter-out $(mains),$(objects))
6495 @end example
6497 @need 1500
6498 @findex sort
6499 @cindex sorting words
6500 @item $(sort @var{list})
6501 Sorts the words of @var{list} in lexical order, removing duplicate
6502 words.  The output is a list of words separated by single spaces.
6503 Thus,
6505 @example
6506 $(sort foo bar lose)
6507 @end example
6509 @noindent
6510 returns the value @samp{bar foo lose}.
6512 @cindex removing duplicate words
6513 @cindex duplicate words, removing
6514 @cindex words, removing duplicates
6515 Incidentally, since @code{sort} removes duplicate words, you can use
6516 it for this purpose even if you don't care about the sort order.
6518 @item $(word @var{n},@var{text})
6519 @findex word
6520 @cindex word, selecting a
6521 @cindex selecting a word
6522 Returns the @var{n}th word of @var{text}.  The legitimate values of
6523 @var{n} start from 1.  If @var{n} is bigger than the number of words
6524 in @var{text}, the value is empty.  For example,
6526 @example
6527 $(word 2, foo bar baz)
6528 @end example
6530 @noindent
6531 returns @samp{bar}.
6533 @item $(wordlist @var{s},@var{e},@var{text})
6534 @findex wordlist
6535 @cindex words, selecting lists of
6536 @cindex selecting word lists
6537 Returns the list of words in @var{text} starting with word @var{s} and
6538 ending with word @var{e} (inclusive).  The legitimate values of @var{s}
6539 start from 1; @var{e} may start from 0.  If @var{s} is bigger than the
6540 number of words in @var{text}, the value is empty.  If @var{e} is
6541 bigger than the number of words in @var{text}, words up to the end of
6542 @var{text} are returned.  If @var{s} is greater than @var{e}, nothing
6543 is returned.  For example,
6545 @example
6546 $(wordlist 2, 3, foo bar baz)
6547 @end example
6549 @noindent
6550 returns @samp{bar baz}.
6552 @c Following item phrased to prevent overfull hbox.  --RJC 17 Jul 92
6553 @item $(words @var{text})
6554 @findex words
6555 @cindex words, finding number
6556 Returns the number of words in @var{text}.
6557 Thus, the last word of @var{text} is
6558 @w{@code{$(word $(words @var{text}),@var{text})}}.@refill
6560 @item $(firstword @var{names}@dots{})
6561 @findex firstword
6562 @cindex words, extracting first
6563 The argument @var{names} is regarded as a series of names, separated
6564 by whitespace.  The value is the first name in the series.  The rest
6565 of the names are ignored.
6567 For example,
6569 @example
6570 $(firstword foo bar)
6571 @end example
6573 @noindent
6574 produces the result @samp{foo}.  Although @code{$(firstword
6575 @var{text})} is the same as @code{$(word 1,@var{text})}, the
6576 @code{firstword} function is retained for its simplicity.@refill
6579 @item $(lastword @var{names}@dots{})
6580 @findex lastword
6581 @cindex words, extracting last
6582 The argument @var{names} is regarded as a series of names, separated
6583 by whitespace.  The value is the last name in the series.
6585 For example,
6587 @example
6588 $(lastword foo bar)
6589 @end example
6591 @noindent
6592 produces the result @samp{bar}.  Although @code{$(lastword
6593 @var{text})} is the same as @code{$(word $(words @var{text}),@var{text})},
6594 the @code{lastword} function was added for its simplicity and better
6595 performance.@refill
6596 @end table
6599 Here is a realistic example of the use of @code{subst} and
6600 @code{patsubst}.  Suppose that a makefile uses the @code{VPATH} variable
6601 to specify a list of directories that @code{make} should search for
6602 prerequisite files
6603 (@pxref{General Search, , @code{VPATH} Search Path for All Prerequisites}).
6604 This example shows how to
6605 tell the C compiler to search for header files in the same list of
6606 directories.@refill
6608 The value of @code{VPATH} is a list of directories separated by colons,
6609 such as @samp{src:../headers}.  First, the @code{subst} function is used to
6610 change the colons to spaces:
6612 @example
6613 $(subst :, ,$(VPATH))
6614 @end example
6616 @noindent
6617 This produces @samp{src ../headers}.  Then @code{patsubst} is used to turn
6618 each directory name into a @samp{-I} flag.  These can be added to the
6619 value of the variable @code{CFLAGS}, which is passed automatically to the C
6620 compiler, like this:
6622 @example
6623 override CFLAGS += $(patsubst %,-I%,$(subst :, ,$(VPATH)))
6624 @end example
6626 @noindent
6627 The effect is to append the text @samp{-Isrc -I../headers} to the
6628 previously given value of @code{CFLAGS}.  The @code{override} directive is
6629 used so that the new value is assigned even if the previous value of
6630 @code{CFLAGS} was specified with a command argument (@pxref{Override
6631 Directive, , The @code{override} Directive}).
6633 @node File Name Functions, Conditional Functions, Text Functions, Functions
6634 @section Functions for File Names
6635 @cindex functions, for file names
6636 @cindex file name functions
6638 Several of the built-in expansion functions relate specifically to
6639 taking apart file names or lists of file names.
6641 Each of the following functions performs a specific transformation on a
6642 file name.  The argument of the function is regarded as a series of file
6643 names, separated by whitespace.  (Leading and trailing whitespace is
6644 ignored.)  Each file name in the series is transformed in the same way and
6645 the results are concatenated with single spaces between them.
6647 @table @code
6648 @item $(dir @var{names}@dots{})
6649 @findex dir
6650 @cindex directory part
6651 @cindex file name, directory part
6652 Extracts the directory-part of each file name in @var{names}.  The
6653 directory-part of the file name is everything up through (and
6654 including) the last slash in it.  If the file name contains no slash,
6655 the directory part is the string @samp{./}.  For example,
6657 @example
6658 $(dir src/foo.c hacks)
6659 @end example
6661 @noindent
6662 produces the result @samp{src/ ./}.
6664 @item $(notdir @var{names}@dots{})
6665 @findex notdir
6666 @cindex file name, nondirectory part
6667 @cindex nondirectory part
6668 Extracts all but the directory-part of each file name in @var{names}.
6669 If the file name contains no slash, it is left unchanged.  Otherwise,
6670 everything through the last slash is removed from it.
6672 A file name that ends with a slash becomes an empty string.  This is
6673 unfortunate, because it means that the result does not always have the
6674 same number of whitespace-separated file names as the argument had;
6675 but we do not see any other valid alternative.
6677 For example,
6679 @example
6680 $(notdir src/foo.c hacks)
6681 @end example
6683 @noindent
6684 produces the result @samp{foo.c hacks}.
6686 @item $(suffix @var{names}@dots{})
6687 @findex suffix
6688 @cindex suffix, function to find
6689 @cindex file name suffix
6690 Extracts the suffix of each file name in @var{names}.  If the file name
6691 contains a period, the suffix is everything starting with the last
6692 period.  Otherwise, the suffix is the empty string.  This frequently
6693 means that the result will be empty when @var{names} is not, and if
6694 @var{names} contains multiple file names, the result may contain fewer
6695 file names.
6697 For example,
6699 @example
6700 $(suffix src/foo.c src-1.0/bar.c hacks)
6701 @end example
6703 @noindent
6704 produces the result @samp{.c .c}.
6706 @item $(basename @var{names}@dots{})
6707 @findex basename
6708 @cindex basename
6709 @cindex file name, basename of
6710 Extracts all but the suffix of each file name in @var{names}.  If the
6711 file name contains a period, the basename is everything starting up to
6712 (and not including) the last period.  Periods in the directory part are
6713 ignored.  If there is no period, the basename is the entire file name.
6714 For example,
6716 @example
6717 $(basename src/foo.c src-1.0/bar hacks)
6718 @end example
6720 @noindent
6721 produces the result @samp{src/foo src-1.0/bar hacks}.
6723 @c plural convention with dots (be consistent)
6724 @item $(addsuffix @var{suffix},@var{names}@dots{})
6725 @findex addsuffix
6726 @cindex suffix, adding
6727 @cindex file name suffix, adding
6728 The argument @var{names} is regarded as a series of names, separated
6729 by whitespace; @var{suffix} is used as a unit.  The value of
6730 @var{suffix} is appended to the end of each individual name and the
6731 resulting larger names are concatenated with single spaces between
6732 them.  For example,
6734 @example
6735 $(addsuffix .c,foo bar)
6736 @end example
6738 @noindent
6739 produces the result @samp{foo.c bar.c}.
6741 @item $(addprefix @var{prefix},@var{names}@dots{})
6742 @findex addprefix
6743 @cindex prefix, adding
6744 @cindex file name prefix, adding
6745 The argument @var{names} is regarded as a series of names, separated
6746 by whitespace; @var{prefix} is used as a unit.  The value of
6747 @var{prefix} is prepended to the front of each individual name and the
6748 resulting larger names are concatenated with single spaces between
6749 them.  For example,
6751 @example
6752 $(addprefix src/,foo bar)
6753 @end example
6755 @noindent
6756 produces the result @samp{src/foo src/bar}.
6758 @item $(join @var{list1},@var{list2})
6759 @findex join
6760 @cindex joining lists of words
6761 @cindex words, joining lists
6762 Concatenates the two arguments word by word: the two first words (one
6763 from each argument) concatenated form the first word of the result, the
6764 two second words form the second word of the result, and so on.  So the
6765 @var{n}th word of the result comes from the @var{n}th word of each
6766 argument.  If one argument has more words that the other, the extra
6767 words are copied unchanged into the result.
6769 For example, @samp{$(join a b,.c .o)} produces @samp{a.c b.o}.
6771 Whitespace between the words in the lists is not preserved; it is
6772 replaced with a single space.
6774 This function can merge the results of the @code{dir} and
6775 @code{notdir} functions, to produce the original list of files which
6776 was given to those two functions.@refill
6778 @item $(wildcard @var{pattern})
6779 @findex wildcard
6780 @cindex wildcard, function
6781 The argument @var{pattern} is a file name pattern, typically containing
6782 wildcard characters (as in shell file name patterns).  The result of
6783 @code{wildcard} is a space-separated list of the names of existing files
6784 that match the pattern.
6785 @xref{Wildcards, ,Using Wildcard Characters in File Names}.
6787 @item $(realpath @var{names}@dots{})
6788 @findex realpath
6789 @cindex realpath
6790 @cindex file name, realpath of
6791 For each file name in @var{names} return the canonical absolute name.
6792 A canonical name does not contain any @code{.} or @code{..} components,
6793 nor any repeated path separators (@code{/}) or symlinks.  In case of a
6794 failure the empty string is returned.  Consult the @code{realpath(3)}
6795 documentation for a list of possible failure causes.
6797 @item $(abspath @var{names}@dots{})
6798 @findex abspath
6799 @cindex abspath
6800 @cindex file name, abspath of
6801 For each file name in @var{names} return an absolute name that does
6802 not contain any @code{.} or @code{..} components, nor any repeated path
6803 separators (@code{/}).  Note that, in contrast to @code{realpath}
6804 function, @code{abspath} does not resolve symlinks and does not require
6805 the file names to refer to an existing file or directory.  Use the
6806 @code{wildcard} function to test for existence.
6807 @end table
6809 @node Conditional Functions, Foreach Function, File Name Functions, Functions
6810 @section Functions for Conditionals
6811 @findex if
6812 @cindex conditional expansion
6813 There are three functions that provide conditional expansion.  A key
6814 aspect of these functions is that not all of the arguments are
6815 expanded initially.  Only those arguments which need to be expanded,
6816 will be expanded.
6818 @table @code
6819 @item $(if @var{condition},@var{then-part}[,@var{else-part}])
6820 @findex if
6821 The @code{if} function provides support for conditional expansion in a
6822 functional context (as opposed to the GNU @code{make} makefile
6823 conditionals such as @code{ifeq} (@pxref{Conditional Syntax, ,Syntax of
6824 Conditionals}).
6826 The first argument, @var{condition}, first has all preceding and
6827 trailing whitespace stripped, then is expanded.  If it expands to any
6828 non-empty string, then the condition is considered to be true.  If it
6829 expands to an empty string, the condition is considered to be false.
6831 If the condition is true then the second argument, @var{then-part}, is
6832 evaluated and this is used as the result of the evaluation of the entire
6833 @code{if} function.
6835 If the condition is false then the third argument, @var{else-part}, is
6836 evaluated and this is the result of the @code{if} function.  If there is
6837 no third argument, the @code{if} function evaluates to nothing (the
6838 empty string).
6840 Note that only one of the @var{then-part} or the @var{else-part} will be
6841 evaluated, never both.  Thus, either can contain side-effects (such as
6842 @code{shell} function calls, etc.)
6844 @item $(or @var{condition1}[,@var{condition2}[,@var{condition3}@dots{}]])
6845 @findex or
6846 The @code{or} function provides a ``short-circuiting'' OR operation.
6847 Each argument is expanded, in order.  If an argument expands to a
6848 non-empty string the processing stops and the result of the expansion
6849 is that string.  If, after all arguments are expanded, all of them are
6850 false (empty), then the result of the expansion is the empty string.
6852 @item $(and @var{condition1}[,@var{condition2}[,@var{condition3}@dots{}]])
6853 @findex and
6854 The @code{and} function provides a ``short-circuiting'' AND operation.
6855 Each argument is expanded, in order.  If an argument expands to an
6856 empty string the processing stops and the result of the expansion is
6857 the empty string.  If all arguments expand to a non-empty string then
6858 the result of the expansion is the expansion of the last argument.
6860 @end table
6862 @node Foreach Function, Call Function, Conditional Functions, Functions
6863 @section The @code{foreach} Function
6864 @findex foreach
6865 @cindex words, iterating over
6867 The @code{foreach} function is very different from other functions.  It
6868 causes one piece of text to be used repeatedly, each time with a different
6869 substitution performed on it.  It resembles the @code{for} command in the
6870 shell @code{sh} and the @code{foreach} command in the C-shell @code{csh}.
6872 The syntax of the @code{foreach} function is:
6874 @example
6875 $(foreach @var{var},@var{list},@var{text})
6876 @end example
6878 @noindent
6879 The first two arguments, @var{var} and @var{list}, are expanded before
6880 anything else is done; note that the last argument, @var{text}, is
6881 @strong{not} expanded at the same time.  Then for each word of the expanded
6882 value of @var{list}, the variable named by the expanded value of @var{var}
6883 is set to that word, and @var{text} is expanded.  Presumably @var{text}
6884 contains references to that variable, so its expansion will be different
6885 each time.
6887 The result is that @var{text} is expanded as many times as there are
6888 whitespace-separated words in @var{list}.  The multiple expansions of
6889 @var{text} are concatenated, with spaces between them, to make the result
6890 of @code{foreach}.
6892 This simple example sets the variable @samp{files} to the list of all files
6893 in the directories in the list @samp{dirs}:
6895 @example
6896 dirs := a b c d
6897 files := $(foreach dir,$(dirs),$(wildcard $(dir)/*))
6898 @end example
6900 Here @var{text} is @samp{$(wildcard $(dir)/*)}.  The first repetition
6901 finds the value @samp{a} for @code{dir}, so it produces the same result
6902 as @samp{$(wildcard a/*)}; the second repetition produces the result
6903 of @samp{$(wildcard b/*)}; and the third, that of @samp{$(wildcard c/*)}.
6905 This example has the same result (except for setting @samp{dirs}) as
6906 the following example:
6908 @example
6909 files := $(wildcard a/* b/* c/* d/*)
6910 @end example
6912 When @var{text} is complicated, you can improve readability by giving it
6913 a name, with an additional variable:
6915 @example
6916 find_files = $(wildcard $(dir)/*)
6917 dirs := a b c d
6918 files := $(foreach dir,$(dirs),$(find_files))
6919 @end example
6921 @noindent
6922 Here we use the variable @code{find_files} this way.  We use plain @samp{=}
6923 to define a recursively-expanding variable, so that its value contains an
6924 actual function call to be reexpanded under the control of @code{foreach};
6925 a simply-expanded variable would not do, since @code{wildcard} would be
6926 called only once at the time of defining @code{find_files}.
6928 The @code{foreach} function has no permanent effect on the variable
6929 @var{var}; its value and flavor after the @code{foreach} function call are
6930 the same as they were beforehand.  The other values which are taken from
6931 @var{list} are in effect only temporarily, during the execution of
6932 @code{foreach}.  The variable @var{var} is a simply-expanded variable
6933 during the execution of @code{foreach}.  If @var{var} was undefined
6934 before the @code{foreach} function call, it is undefined after the call.
6935 @xref{Flavors, ,The Two Flavors of Variables}.@refill
6937 You must take care when using complex variable expressions that result in
6938 variable names because many strange things are valid variable names, but
6939 are probably not what you intended.  For example,
6941 @smallexample
6942 files := $(foreach Esta escrito en espanol!,b c ch,$(find_files))
6943 @end smallexample
6945 @noindent
6946 might be useful if the value of @code{find_files} references the variable
6947 whose name is @samp{Esta escrito en espanol!} (es un nombre bastante largo,
6948 no?), but it is more likely to be a mistake.
6950 @node Call Function, Value Function, Foreach Function, Functions
6951 @section The @code{call} Function
6952 @findex call
6953 @cindex functions, user defined
6954 @cindex user defined functions
6956 The @code{call} function is unique in that it can be used to create new
6957 parameterized functions.  You can write a complex expression as the
6958 value of a variable, then use @code{call} to expand it with different
6959 values.
6961 The syntax of the @code{call} function is:
6963 @example
6964 $(call @var{variable},@var{param},@var{param},@dots{})
6965 @end example
6967 When @code{make} expands this function, it assigns each @var{param} to
6968 temporary variables @code{$(1)}, @code{$(2)}, etc.  The variable
6969 @code{$(0)} will contain @var{variable}.  There is no maximum number of
6970 parameter arguments.  There is no minimum, either, but it doesn't make
6971 sense to use @code{call} with no parameters.
6973 Then @var{variable} is expanded as a @code{make} variable in the context
6974 of these temporary assignments.  Thus, any reference to @code{$(1)} in
6975 the value of @var{variable} will resolve to the first @var{param} in the
6976 invocation of @code{call}.
6978 Note that @var{variable} is the @emph{name} of a variable, not a
6979 @emph{reference} to that variable.  Therefore you would not normally use
6980 a @samp{$} or parentheses when writing it.  (You can, however, use a
6981 variable reference in the name if you want the name not to be a
6982 constant.)
6984 If @var{variable} is the name of a builtin function, the builtin function
6985 is always invoked (even if a @code{make} variable by that name also
6986 exists).
6988 The @code{call} function expands the @var{param} arguments before
6989 assigning them to temporary variables.  This means that @var{variable}
6990 values containing references to builtin functions that have special
6991 expansion rules, like @code{foreach} or @code{if}, may not work as you
6992 expect.
6994 Some examples may make this clearer.
6996 This macro simply reverses its arguments:
6998 @smallexample
6999 reverse = $(2) $(1)
7001 foo = $(call reverse,a,b)
7002 @end smallexample
7004 @noindent
7005 Here @var{foo} will contain @samp{b a}.
7007 This one is slightly more interesting: it defines a macro to search for
7008 the first instance of a program in @code{PATH}:
7010 @smallexample
7011 pathsearch = $(firstword $(wildcard $(addsuffix /$(1),$(subst :, ,$(PATH)))))
7013 LS := $(call pathsearch,ls)
7014 @end smallexample
7016 @noindent
7017 Now the variable LS contains @code{/bin/ls} or similar.
7019 The @code{call} function can be nested.  Each recursive invocation gets
7020 its own local values for @code{$(1)}, etc.@: that mask the values of
7021 higher-level @code{call}.  For example, here is an implementation of a
7022 @dfn{map} function:
7024 @smallexample
7025 map = $(foreach a,$(2),$(call $(1),$(a)))
7026 @end smallexample
7028 Now you can @var{map} a function that normally takes only one argument,
7029 such as @code{origin}, to multiple values in one step:
7031 @smallexample
7032 o = $(call map,origin,o map MAKE)
7033 @end smallexample
7035 and end up with @var{o} containing something like @samp{file file default}.
7037 A final caution: be careful when adding whitespace to the arguments to
7038 @code{call}.  As with other functions, any whitespace contained in the
7039 second and subsequent arguments is kept; this can cause strange
7040 effects.  It's generally safest to remove all extraneous whitespace when
7041 providing parameters to @code{call}.
7043 @node Value Function, Eval Function, Call Function, Functions
7044 @comment  node-name,  next,  previous,  up
7045 @section The @code{value} Function
7046 @findex value
7047 @cindex variables, unexpanded value
7049 The @code{value} function provides a way for you to use the value of a
7050 variable @emph{without} having it expanded.  Please note that this
7051 does not undo expansions which have already occurred; for example if
7052 you create a simply expanded variable its value is expanded during the
7053 definition; in that case the @code{value} function will return the
7054 same result as using the variable directly.
7056 The syntax of the @code{value} function is:
7058 @example
7059 $(value @var{variable})
7060 @end example
7062 Note that @var{variable} is the @emph{name} of a variable; not a
7063 @emph{reference} to that variable.  Therefore you would not normally
7064 use a @samp{$} or parentheses when writing it.  (You can, however, use
7065 a variable reference in the name if you want the name not to be a
7066 constant.)
7068 The result of this function is a string containing the value of
7069 @var{variable}, without any expansion occurring.  For example, in this
7070 makefile:
7072 @example
7073 @group
7074 FOO = $PATH
7076 all:
7077         @@echo $(FOO)
7078         @@echo $(value FOO)
7079 @end group
7080 @end example
7082 @noindent
7083 The first output line would be @code{ATH}, since the ``$P'' would be
7084 expanded as a @code{make} variable, while the second output line would
7085 be the current value of your @code{$PATH} environment variable, since
7086 the @code{value} function avoided the expansion.
7088 The @code{value} function is most often used in conjunction with the
7089 @code{eval} function (@pxref{Eval Function}).
7091 @node Eval Function, Origin Function, Value Function, Functions
7092 @comment  node-name,  next,  previous,  up
7093 @section The @code{eval} Function
7094 @findex eval
7095 @cindex evaluating makefile syntax
7096 @cindex makefile syntax, evaluating
7098 The @code{eval} function is very special: it allows you to define new
7099 makefile constructs that are not constant; which are the result of
7100 evaluating other variables and functions.  The argument to the
7101 @code{eval} function is expanded, then the results of that expansion
7102 are parsed as makefile syntax.  The expanded results can define new
7103 @code{make} variables, targets, implicit or explicit rules, etc.
7105 The result of the @code{eval} function is always the empty string;
7106 thus, it can be placed virtually anywhere in a makefile without
7107 causing syntax errors.
7109 It's important to realize that the @code{eval} argument is expanded
7110 @emph{twice}; first by the @code{eval} function, then the results of
7111 that expansion are expanded again when they are parsed as makefile
7112 syntax.  This means you may need to provide extra levels of escaping
7113 for ``$'' characters when using @code{eval}.  The @code{value}
7114 function (@pxref{Value Function}) can sometimes be useful in these
7115 situations, to circumvent unwanted expansions.
7117 Here is an example of how @code{eval} can be used; this example
7118 combines a number of concepts and other functions.  Although it might
7119 seem overly complex to use @code{eval} in this example, rather than
7120 just writing out the rules, consider two things: first, the template
7121 definition (in @code{PROGRAM_template}) could need to be much more
7122 complex than it is here; and second, you might put the complex,
7123 ``generic'' part of this example into another makefile, then include
7124 it in all the individual makefiles.  Now your individual makefiles are
7125 quite straightforward.
7127 @example
7128 @group
7129 PROGRAMS    = server client
7131 server_OBJS = server.o server_priv.o server_access.o
7132 server_LIBS = priv protocol
7134 client_OBJS = client.o client_api.o client_mem.o
7135 client_LIBS = protocol
7137 # Everything after this is generic
7139 .PHONY: all
7140 all: $(PROGRAMS)
7142 define PROGRAM_template
7143  $(1): $$($(1)_OBJS) $$($(1)_LIBS:%=-l%)
7144  ALL_OBJS   += $$($(1)_OBJS)
7145 endef
7147 $(foreach prog,$(PROGRAMS),$(eval $(call PROGRAM_template,$(prog))))
7149 $(PROGRAMS):
7150         $(LINK.o) $^ $(LDLIBS) -o $@@
7152 clean:
7153         rm -f $(ALL_OBJS) $(PROGRAMS)
7154 @end group
7155 @end example
7157 @node Origin Function, Flavor Function, Eval Function, Functions
7158 @section The @code{origin} Function
7159 @findex origin
7160 @cindex variables, origin of
7161 @cindex origin of variable
7163 The @code{origin} function is unlike most other functions in that it does
7164 not operate on the values of variables; it tells you something @emph{about}
7165 a variable.  Specifically, it tells you where it came from.
7167 The syntax of the @code{origin} function is:
7169 @example
7170 $(origin @var{variable})
7171 @end example
7173 Note that @var{variable} is the @emph{name} of a variable to inquire about;
7174 not a @emph{reference} to that variable.  Therefore you would not normally
7175 use a @samp{$} or parentheses when writing it.  (You can, however, use a
7176 variable reference in the name if you want the name not to be a constant.)
7178 The result of this function is a string telling you how the variable
7179 @var{variable} was defined:
7181 @table @samp
7182 @item undefined
7184 if @var{variable} was never defined.
7186 @item default
7188 if @var{variable} has a default definition, as is usual with @code{CC}
7189 and so on.  @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
7190 Note that if you have redefined a default variable, the @code{origin}
7191 function will return the origin of the later definition.
7193 @item environment
7195 if @var{variable} was defined as an environment variable and the
7196 @samp{-e} option is @emph{not} turned on (@pxref{Options Summary, ,Summary of Options}).
7198 @item environment override
7200 if @var{variable} was defined as an environment variable and the
7201 @w{@samp{-e}} option @emph{is} turned on (@pxref{Options Summary,
7202 ,Summary of Options}).@refill
7204 @item file
7206 if @var{variable} was defined in a makefile.
7208 @item command line
7210 if @var{variable} was defined on the command line.
7212 @item override
7214 if @var{variable} was defined with an @code{override} directive in a
7215 makefile (@pxref{Override Directive, ,The @code{override} Directive}).
7217 @item automatic
7219 if @var{variable} is an automatic variable defined for the
7220 execution of the commands for each rule
7221 (@pxref{Automatic Variables}).
7222 @end table
7224 This information is primarily useful (other than for your curiosity) to
7225 determine if you want to believe the value of a variable.  For example,
7226 suppose you have a makefile @file{foo} that includes another makefile
7227 @file{bar}.  You want a variable @code{bletch} to be defined in @file{bar}
7228 if you run the command @w{@samp{make -f bar}}, even if the environment contains
7229 a definition of @code{bletch}.  However, if @file{foo} defined
7230 @code{bletch} before including @file{bar}, you do not want to override that
7231 definition.  This could be done by using an @code{override} directive in
7232 @file{foo}, giving that definition precedence over the later definition in
7233 @file{bar}; unfortunately, the @code{override} directive would also
7234 override any command line definitions.  So, @file{bar} could
7235 include:@refill
7237 @example
7238 @group
7239 ifdef bletch
7240 ifeq "$(origin bletch)" "environment"
7241 bletch = barf, gag, etc.
7242 endif
7243 endif
7244 @end group
7245 @end example
7247 @noindent
7248 If @code{bletch} has been defined from the environment, this will redefine
7251 If you want to override a previous definition of @code{bletch} if it came
7252 from the environment, even under @samp{-e}, you could instead write:
7254 @example
7255 @group
7256 ifneq "$(findstring environment,$(origin bletch))" ""
7257 bletch = barf, gag, etc.
7258 endif
7259 @end group
7260 @end example
7262 Here the redefinition takes place if @samp{$(origin bletch)} returns either
7263 @samp{environment} or @samp{environment override}.
7264 @xref{Text Functions, , Functions for String Substitution and Analysis}.
7266 @node Flavor Function, Shell Function, Origin Function, Functions
7267 @section The @code{flavor} Function
7268 @findex flavor
7269 @cindex variables, flavor of
7270 @cindex flavor of variable
7272 The @code{flavor} function is unlike most other functions (and like
7273 @code{origin} function) in that it does not operate on the values of
7274 variables; it tells you something @emph{about} a variable.
7275 Specifically, it tells you the flavor of a variable (@pxref{Flavors,
7276 ,The Two Flavors of Variables}).
7278 The syntax of the @code{flavor} function is:
7280 @example
7281 $(flavor @var{variable})
7282 @end example
7284 Note that @var{variable} is the @emph{name} of a variable to inquire about;
7285 not a @emph{reference} to that variable.  Therefore you would not normally
7286 use a @samp{$} or parentheses when writing it.  (You can, however, use a
7287 variable reference in the name if you want the name not to be a constant.)
7289 The result of this function is a string that identifies the flavor of the
7290 variable @var{variable}:
7292 @table @samp
7293 @item undefined
7295 if @var{variable} was never defined.
7297 @item recursive
7299 if @var{variable} is a recursively expanded variable.
7301 @item simple
7303 if @var{variable} is a simply expanded variable.
7305 @end table
7308 @node Shell Function, Make Control Functions, Flavor Function, Functions
7309 @section The @code{shell} Function
7310 @findex shell
7311 @cindex commands, expansion
7312 @cindex backquotes
7313 @cindex shell command, function for
7315 The @code{shell} function is unlike any other function other than the
7316 @code{wildcard} function
7317 (@pxref{Wildcard Function, ,The Function @code{wildcard}}) in that it
7318 communicates with the world outside of @code{make}.
7320 The @code{shell} function performs the same function that backquotes
7321 (@samp{`}) perform in most shells: it does @dfn{command expansion}.
7322 This means that it takes as an argument a shell command and evaluates
7323 to the output of the command.  The only processing @code{make} does on
7324 the result is to convert each newline (or carriage-return / newline
7325 pair) to a single space.  If there is a trailing (carriage-return
7326 and) newline it will simply be removed.@refill
7328 The commands run by calls to the @code{shell} function are run when the
7329 function calls are expanded (@pxref{Reading Makefiles, , How
7330 @code{make} Reads a Makefile}).  Because this function involves
7331 spawning a new shell, you should carefully consider the performance
7332 implications of using the @code{shell} function within recursively
7333 expanded variables vs.@: simply expanded variables (@pxref{Flavors, ,The
7334 Two Flavors of Variables}).
7336 Here are some examples of the use of the @code{shell} function:
7338 @example
7339 contents := $(shell cat foo)
7340 @end example
7342 @noindent
7343 sets @code{contents} to the contents of the file @file{foo}, with a space
7344 (rather than a newline) separating each line.
7346 @example
7347 files := $(shell echo *.c)
7348 @end example
7350 @noindent
7351 sets @code{files} to the expansion of @samp{*.c}.  Unless @code{make} is
7352 using a very strange shell, this has the same result as
7353 @w{@samp{$(wildcard *.c)}} (as long as at least one @samp{.c} file
7354 exists).@refill
7356 @node Make Control Functions,  , Shell Function, Functions
7357 @section Functions That Control Make
7358 @cindex functions, for controlling make
7359 @cindex controlling make
7361 These functions control the way make runs.  Generally, they are used to
7362 provide information to the user of the makefile or to cause make to stop
7363 if some sort of environmental error is detected.
7365 @table @code
7366 @item $(error @var{text}@dots{})
7367 @findex error
7368 @cindex error, stopping on
7369 @cindex stopping make
7370 Generates a fatal error where the message is @var{text}.  Note that the
7371 error is generated whenever this function is evaluated.  So, if you put
7372 it inside a command script or on the right side of a recursive variable
7373 assignment, it won't be evaluated until later.  The @var{text} will be
7374 expanded before the error is generated.
7376 For example,
7378 @example
7379 ifdef ERROR1
7380 $(error error is $(ERROR1))
7381 endif
7382 @end example
7384 @noindent
7385 will generate a fatal error during the read of the makefile if the
7386 @code{make} variable @code{ERROR1} is defined.  Or,
7388 @example
7389 ERR = $(error found an error!)
7391 .PHONY: err
7392 err: ; $(ERR)
7393 @end example
7395 @noindent
7396 will generate a fatal error while @code{make} is running, if the
7397 @code{err} target is invoked.
7399 @item $(warning @var{text}@dots{})
7400 @findex warning
7401 @cindex warnings, printing
7402 @cindex printing user warnings
7403 This function works similarly to the @code{error} function, above,
7404 except that @code{make} doesn't exit.  Instead, @var{text} is expanded
7405 and the resulting message is displayed, but processing of the makefile
7406 continues.
7408 The result of the expansion of this function is the empty string.
7410 @item $(info @var{text}@dots{})
7411 @findex info
7412 @cindex printing messages
7413 This function does nothing more than print its (expanded) argument(s)
7414 to standard output.  No makefile name or line number is added.  The
7415 result of the expansion of this function is the empty string.
7416 @end table
7418 @node Running, Implicit Rules, Functions, Top
7419 @chapter How to Run @code{make}
7421 A makefile that says how to recompile a program can be used in more
7422 than one way.  The simplest use is to recompile every file that is out
7423 of date.  Usually, makefiles are written so that if you run
7424 @code{make} with no arguments, it does just that.
7426 But you might want to update only some of the files; you might want to use
7427 a different compiler or different compiler options; you might want just to
7428 find out which files are out of date without changing them.
7430 By giving arguments when you run @code{make}, you can do any of these
7431 things and many others.
7433 @cindex exit status of make
7434 The exit status of @code{make} is always one of three values:
7435 @table @code
7436 @item 0
7437 The exit status is zero if @code{make} is successful.
7438 @item 2
7439 The exit status is two if @code{make} encounters any errors.
7440 It will print messages describing the particular errors.
7441 @item 1
7442 The exit status is one if you use the @samp{-q} flag and @code{make}
7443 determines that some target is not already up to date.
7444 @xref{Instead of Execution, ,Instead of Executing the Commands}.
7445 @end table
7447 @menu
7448 * Makefile Arguments::          How to specify which makefile to use.
7449 * Goals::                       How to use goal arguments to specify which
7450                                   parts of the makefile to use.
7451 * Instead of Execution::        How to use mode flags to specify what
7452                                   kind of thing to do with the commands
7453                                   in the makefile other than simply
7454                                   execute them.
7455 * Avoiding Compilation::        How to avoid recompiling certain files.
7456 * Overriding::                  How to override a variable to specify
7457                                   an alternate compiler and other things.
7458 * Testing::                     How to proceed past some errors, to
7459                                   test compilation.
7460 * Options Summary::             Summary of Options
7461 @end menu
7463 @node Makefile Arguments, Goals, Running, Running
7464 @section Arguments to Specify the Makefile
7465 @cindex @code{--file}
7466 @cindex @code{--makefile}
7467 @cindex @code{-f}
7469 The way to specify the name of the makefile is with the @samp{-f} or
7470 @samp{--file} option (@samp{--makefile} also works).  For example,
7471 @samp{-f altmake} says to use the file @file{altmake} as the makefile.
7473 If you use the @samp{-f} flag several times and follow each @samp{-f}
7474 with an argument, all the specified files are used jointly as
7475 makefiles.
7477 If you do not use the @samp{-f} or @samp{--file} flag, the default is
7478 to try @file{GNUmakefile}, @file{makefile}, and @file{Makefile}, in
7479 that order, and use the first of these three which exists or can be made
7480 (@pxref{Makefiles, ,Writing Makefiles}).@refill
7482 @node Goals, Instead of Execution, Makefile Arguments, Running
7483 @section Arguments to Specify the Goals
7484 @cindex goal, how to specify
7486 The @dfn{goals} are the targets that @code{make} should strive ultimately
7487 to update.  Other targets are updated as well if they appear as
7488 prerequisites of goals, or prerequisites of prerequisites of goals, etc.
7490 By default, the goal is the first target in the makefile (not counting
7491 targets that start with a period).  Therefore, makefiles are usually
7492 written so that the first target is for compiling the entire program or
7493 programs they describe.  If the first rule in the makefile has several
7494 targets, only the first target in the rule becomes the default goal, not
7495 the whole list.  You can manage the selection of the default goal from
7496 within your makefile using the @code{.DEFAULT_GOAL} variable
7497 (@pxref{Special Variables, , Other Special Variables}).
7499 You can also specify a different goal or goals with command-line
7500 arguments to @code{make}.  Use the name of the goal as an argument.
7501 If you specify several goals, @code{make} processes each of them in
7502 turn, in the order you name them.
7504 Any target in the makefile may be specified as a goal (unless it
7505 starts with @samp{-} or contains an @samp{=}, in which case it will be
7506 parsed as a switch or variable definition, respectively).  Even
7507 targets not in the makefile may be specified, if @code{make} can find
7508 implicit rules that say how to make them.
7510 @vindex MAKECMDGOALS
7511 @code{Make} will set the special variable @code{MAKECMDGOALS} to the
7512 list of goals you specified on the command line.  If no goals were given
7513 on the command line, this variable is empty.  Note that this variable
7514 should be used only in special circumstances.
7516 An example of appropriate use is to avoid including @file{.d} files
7517 during @code{clean} rules (@pxref{Automatic Prerequisites}), so
7518 @code{make} won't create them only to immediately remove them
7519 again:@refill
7521 @example
7522 @group
7523 sources = foo.c bar.c
7525 ifneq ($(MAKECMDGOALS),clean)
7526 include $(sources:.c=.d)
7527 endif
7528 @end group
7529 @end example
7531 One use of specifying a goal is if you want to compile only a part of
7532 the program, or only one of several programs.  Specify as a goal each
7533 file that you wish to remake.  For example, consider a directory containing
7534 several programs, with a makefile that starts like this:
7536 @example
7537 .PHONY: all
7538 all: size nm ld ar as
7539 @end example
7541 If you are working on the program @code{size}, you might want to say
7542 @w{@samp{make size}} so that only the files of that program are recompiled.
7544 Another use of specifying a goal is to make files that are not normally
7545 made.  For example, there may be a file of debugging output, or a
7546 version of the program that is compiled specially for testing, which has
7547 a rule in the makefile but is not a prerequisite of the default goal.
7549 Another use of specifying a goal is to run the commands associated with
7550 a phony target (@pxref{Phony Targets}) or empty target (@pxref{Empty
7551 Targets, ,Empty Target Files to Record Events}).  Many makefiles contain
7552 a phony target named @file{clean} which deletes everything except source
7553 files.  Naturally, this is done only if you request it explicitly with
7554 @w{@samp{make clean}}.  Following is a list of typical phony and empty
7555 target names.  @xref{Standard Targets}, for a detailed list of all the
7556 standard target names which GNU software packages use.
7558 @table @file
7559 @item all
7560 @cindex @code{all} @r{(standard target)}
7561 Make all the top-level targets the makefile knows about.
7563 @item clean
7564 @cindex @code{clean} @r{(standard target)}
7565 Delete all files that are normally created by running @code{make}.
7567 @item mostlyclean
7568 @cindex @code{mostlyclean} @r{(standard target)}
7569 Like @samp{clean}, but may refrain from deleting a few files that people
7570 normally don't want to recompile.  For example, the @samp{mostlyclean}
7571 target for GCC does not delete @file{libgcc.a}, because recompiling it
7572 is rarely necessary and takes a lot of time.
7574 @item distclean
7575 @cindex @code{distclean} @r{(standard target)}
7576 @itemx realclean
7577 @cindex @code{realclean} @r{(standard target)}
7578 @itemx clobber
7579 @cindex @code{clobber} @r{(standard target)}
7580 Any of these targets might be defined to delete @emph{more} files than
7581 @samp{clean} does.  For example, this would delete configuration files
7582 or links that you would normally create as preparation for compilation,
7583 even if the makefile itself cannot create these files.
7585 @item install
7586 @cindex @code{install} @r{(standard target)}
7587 Copy the executable file into a directory that users typically search
7588 for commands; copy any auxiliary files that the executable uses into
7589 the directories where it will look for them.
7591 @item print
7592 @cindex @code{print} @r{(standard target)}
7593 Print listings of the source files that have changed.
7595 @item tar
7596 @cindex @code{tar} @r{(standard target)}
7597 Create a tar file of the source files.
7599 @item shar
7600 @cindex @code{shar} @r{(standard target)}
7601 Create a shell archive (shar file) of the source files.
7603 @item dist
7604 @cindex @code{dist} @r{(standard target)}
7605 Create a distribution file of the source files.  This might
7606 be a tar file, or a shar file, or a compressed version of one of the
7607 above, or even more than one of the above.
7609 @item TAGS
7610 @cindex @code{TAGS} @r{(standard target)}
7611 Update a tags table for this program.
7613 @item check
7614 @cindex @code{check} @r{(standard target)}
7615 @itemx test
7616 @cindex @code{test} @r{(standard target)}
7617 Perform self tests on the program this makefile builds.
7618 @end table
7620 @node Instead of Execution, Avoiding Compilation, Goals, Running
7621 @section Instead of Executing the Commands
7622 @cindex execution, instead of
7623 @cindex commands, instead of executing
7625 The makefile tells @code{make} how to tell whether a target is up to date,
7626 and how to update each target.  But updating the targets is not always
7627 what you want.  Certain options specify other activities for @code{make}.
7629 @comment Extra blank lines make it print better.
7630 @table @samp
7631 @item -n
7632 @itemx --just-print
7633 @itemx --dry-run
7634 @itemx --recon
7635 @cindex @code{--just-print}
7636 @cindex @code{--dry-run}
7637 @cindex @code{--recon}
7638 @cindex @code{-n}
7640 ``No-op''.  The activity is to print what commands would be used to make
7641 the targets up to date, but not actually execute them.
7643 @item -t
7644 @itemx --touch
7645 @cindex @code{--touch}
7646 @cindex touching files
7647 @cindex target, touching
7648 @cindex @code{-t}
7650 ``Touch''.  The activity is to mark the targets as up to date without
7651 actually changing them.  In other words, @code{make} pretends to compile
7652 the targets but does not really change their contents.
7654 @item -q
7655 @itemx --question
7656 @cindex @code{--question}
7657 @cindex @code{-q}
7658 @cindex question mode
7660 ``Question''.  The activity is to find out silently whether the targets
7661 are up to date already; but execute no commands in either case.  In other
7662 words, neither compilation nor output will occur.
7664 @item -W @var{file}
7665 @itemx --what-if=@var{file}
7666 @itemx --assume-new=@var{file}
7667 @itemx --new-file=@var{file}
7668 @cindex @code{--what-if}
7669 @cindex @code{-W}
7670 @cindex @code{--assume-new}
7671 @cindex @code{--new-file}
7672 @cindex what if
7673 @cindex files, assuming new
7675 ``What if''.  Each @samp{-W} flag is followed by a file name.  The given
7676 files' modification times are recorded by @code{make} as being the present
7677 time, although the actual modification times remain the same.
7678 You can use the @samp{-W} flag in conjunction with the @samp{-n} flag
7679 to see what would happen if you were to modify specific files.@refill
7680 @end table
7682 With the @samp{-n} flag, @code{make} prints the commands that it would
7683 normally execute but does not execute them.
7685 With the @samp{-t} flag, @code{make} ignores the commands in the rules
7686 and uses (in effect) the command @code{touch} for each target that needs to
7687 be remade.  The @code{touch} command is also printed, unless @samp{-s} or
7688 @code{.SILENT} is used.  For speed, @code{make} does not actually invoke
7689 the program @code{touch}.  It does the work directly.
7691 With the @samp{-q} flag, @code{make} prints nothing and executes no
7692 commands, but the exit status code it returns is zero if and only if the
7693 targets to be considered are already up to date.  If the exit status is
7694 one, then some updating needs to be done.  If @code{make} encounters an
7695 error, the exit status is two, so you can distinguish an error from a
7696 target that is not up to date.
7698 It is an error to use more than one of these three flags in the same
7699 invocation of @code{make}.
7701 @cindex +, and command execution
7702 The @samp{-n}, @samp{-t}, and @samp{-q} options do not affect command
7703 lines that begin with @samp{+} characters or contain the strings
7704 @samp{$(MAKE)} or @samp{$@{MAKE@}}.  Note that only the line containing
7705 the @samp{+} character or the strings @samp{$(MAKE)} or @samp{$@{MAKE@}}
7706 is run regardless of these options.  Other lines in the same rule are
7707 not run unless they too begin with @samp{+} or contain @samp{$(MAKE)} or
7708 @samp{$@{MAKE@}} (@xref{MAKE Variable, ,How the @code{MAKE} Variable Works}.)
7710 The @samp{-W} flag provides two features:
7712 @itemize @bullet
7713 @item
7714 If you also use the @samp{-n} or @samp{-q} flag, you can see what
7715 @code{make} would do if you were to modify some files.
7717 @item
7718 Without the @samp{-n} or @samp{-q} flag, when @code{make} is actually
7719 executing commands, the @samp{-W} flag can direct @code{make} to act
7720 as if some files had been modified, without actually modifying the
7721 files.@refill
7722 @end itemize
7724 Note that the options @samp{-p} and @samp{-v} allow you to obtain other
7725 information about @code{make} or about the makefiles in use
7726 (@pxref{Options Summary, ,Summary of Options}).@refill
7728 @node Avoiding Compilation, Overriding, Instead of Execution, Running
7729 @section Avoiding Recompilation of Some Files
7730 @cindex @code{-o}
7731 @cindex @code{--old-file}
7732 @cindex @code{--assume-old}
7733 @cindex files, assuming old
7734 @cindex files, avoiding recompilation of
7735 @cindex recompilation, avoiding
7737 Sometimes you may have changed a source file but you do not want to
7738 recompile all the files that depend on it.  For example, suppose you add
7739 a macro or a declaration to a header file that many other files depend
7740 on.  Being conservative, @code{make} assumes that any change in the
7741 header file requires recompilation of all dependent files, but you know
7742 that they do not need to be recompiled and you would rather not waste
7743 the time waiting for them to compile.
7745 If you anticipate the problem before changing the header file, you can
7746 use the @samp{-t} flag.  This flag tells @code{make} not to run the
7747 commands in the rules, but rather to mark the target up to date by
7748 changing its last-modification date.  You would follow this procedure:
7750 @enumerate
7751 @item
7752 Use the command @samp{make} to recompile the source files that really
7753 need recompilation, ensuring that the object files are up-to-date
7754 before you begin.
7756 @item
7757 Make the changes in the header files.
7759 @item
7760 Use the command @samp{make -t} to mark all the object files as
7761 up to date.  The next time you run @code{make}, the changes in the
7762 header files will not cause any recompilation.
7763 @end enumerate
7765 If you have already changed the header file at a time when some files
7766 do need recompilation, it is too late to do this.  Instead, you can
7767 use the @w{@samp{-o @var{file}}} flag, which marks a specified file as
7768 ``old'' (@pxref{Options Summary, ,Summary of Options}).  This means
7769 that the file itself will not be remade, and nothing else will be
7770 remade on its account.  Follow this procedure:
7772 @enumerate
7773 @item
7774 Recompile the source files that need compilation for reasons independent
7775 of the particular header file, with @samp{make -o @var{headerfile}}.
7776 If several header files are involved, use a separate @samp{-o} option
7777 for each header file.
7779 @item
7780 Touch all the object files with @samp{make -t}.
7781 @end enumerate
7783 @node Overriding, Testing, Avoiding Compilation, Running
7784 @section Overriding Variables
7785 @cindex overriding variables with arguments
7786 @cindex variables, overriding with arguments
7787 @cindex command line variables
7788 @cindex variables, command line
7790 An argument that contains @samp{=} specifies the value of a variable:
7791 @samp{@var{v}=@var{x}} sets the value of the variable @var{v} to @var{x}.
7792 If you specify a value in this way, all ordinary assignments of the same
7793 variable in the makefile are ignored; we say they have been
7794 @dfn{overridden} by the command line argument.
7796 The most common way to use this facility is to pass extra flags to
7797 compilers.  For example, in a properly written makefile, the variable
7798 @code{CFLAGS} is included in each command that runs the C compiler, so a
7799 file @file{foo.c} would be compiled something like this:
7801 @example
7802 cc -c $(CFLAGS) foo.c
7803 @end example
7805 Thus, whatever value you set for @code{CFLAGS} affects each compilation
7806 that occurs.  The makefile probably specifies the usual value for
7807 @code{CFLAGS}, like this:
7809 @example
7810 CFLAGS=-g
7811 @end example
7813 Each time you run @code{make}, you can override this value if you
7814 wish.  For example, if you say @samp{make CFLAGS='-g -O'}, each C
7815 compilation will be done with @samp{cc -c -g -O}.  (This also
7816 illustrates how you can use quoting in the shell to enclose spaces and
7817 other special characters in the value of a variable when you override
7818 it.)
7820 The variable @code{CFLAGS} is only one of many standard variables that
7821 exist just so that you can change them this way.  @xref{Implicit
7822 Variables, , Variables Used by Implicit Rules}, for a complete list.
7824 You can also program the makefile to look at additional variables of your
7825 own, giving the user the ability to control other aspects of how the
7826 makefile works by changing the variables.
7828 When you override a variable with a command argument, you can define either
7829 a recursively-expanded variable or a simply-expanded variable.  The
7830 examples shown above make a recursively-expanded variable; to make a
7831 simply-expanded variable, write @samp{:=} instead of @samp{=}.  But, unless
7832 you want to include a variable reference or function call in the
7833 @emph{value} that you specify, it makes no difference which kind of
7834 variable you create.
7836 There is one way that the makefile can change a variable that you have
7837 overridden.  This is to use the @code{override} directive, which is a line
7838 that looks like this: @samp{override @var{variable} = @var{value}}
7839 (@pxref{Override Directive, ,The @code{override} Directive}).
7841 @node Testing, Options Summary, Overriding, Running
7842 @section Testing the Compilation of a Program
7843 @cindex testing compilation
7844 @cindex compilation, testing
7846 Normally, when an error happens in executing a shell command, @code{make}
7847 gives up immediately, returning a nonzero status.  No further commands are
7848 executed for any target.  The error implies that the goal cannot be
7849 correctly remade, and @code{make} reports this as soon as it knows.
7851 When you are compiling a program that you have just changed, this is not
7852 what you want.  Instead, you would rather that @code{make} try compiling
7853 every file that can be tried, to show you as many compilation errors
7854 as possible.
7856 @cindex @code{-k}
7857 @cindex @code{--keep-going}
7858 On these occasions, you should use the @samp{-k} or
7859 @samp{--keep-going} flag.  This tells @code{make} to continue to
7860 consider the other prerequisites of the pending targets, remaking them
7861 if necessary, before it gives up and returns nonzero status.  For
7862 example, after an error in compiling one object file, @samp{make -k}
7863 will continue compiling other object files even though it already
7864 knows that linking them will be impossible.  In addition to continuing
7865 after failed shell commands, @samp{make -k} will continue as much as
7866 possible after discovering that it does not know how to make a target
7867 or prerequisite file.  This will always cause an error message, but
7868 without @samp{-k}, it is a fatal error (@pxref{Options Summary,
7869 ,Summary of Options}).@refill
7871 The usual behavior of @code{make} assumes that your purpose is to get the
7872 goals up to date; once @code{make} learns that this is impossible, it might
7873 as well report the failure immediately.  The @samp{-k} flag says that the
7874 real purpose is to test as much as possible of the changes made in the
7875 program, perhaps to find several independent problems so that you can
7876 correct them all before the next attempt to compile.  This is why Emacs'
7877 @kbd{M-x compile} command passes the @samp{-k} flag by default.
7879 @node Options Summary,  , Testing, Running
7880 @section Summary of Options
7881 @cindex options
7882 @cindex flags
7883 @cindex switches
7885 Here is a table of all the options @code{make} understands:
7887 @table @samp
7888 @item -b
7889 @cindex @code{-b}
7890 @itemx -m
7891 @cindex @code{-m}
7892 These options are ignored for compatibility with other versions of @code{make}.
7894 @item -B
7895 @cindex @code{-B}
7896 @itemx --always-make
7897 @cindex @code{--always-make}
7898 Consider all targets out-of-date.  GNU @code{make} proceeds to
7899 consider targets and their prerequisites using the normal algorithms;
7900 however, all targets so considered are always remade regardless of the
7901 status of their prerequisites.  To avoid infinite recursion, if
7902 @code{MAKE_RESTARTS} (@pxref{Special Variables, , Other Special
7903 Variables}) is set to a number greater than 0 this option is disabled
7904 when considering whether to remake makefiles (@pxref{Remaking
7905 Makefiles, , How Makefiles Are Remade}).
7907 @item -C @var{dir}
7908 @cindex @code{-C}
7909 @itemx --directory=@var{dir}
7910 @cindex @code{--directory}
7911 Change to directory @var{dir} before reading the makefiles.  If multiple
7912 @samp{-C} options are specified, each is interpreted relative to the
7913 previous one: @samp{-C / -C etc} is equivalent to @samp{-C /etc}.
7914 This is typically used with recursive invocations of @code{make}
7915 (@pxref{Recursion, ,Recursive Use of @code{make}}).
7917 @item -d
7918 @cindex @code{-d}
7919 @c Extra blank line here makes the table look better.
7921 Print debugging information in addition to normal processing.  The
7922 debugging information says which files are being considered for
7923 remaking, which file-times are being compared and with what results,
7924 which files actually need to be remade, which implicit rules are
7925 considered and which are applied---everything interesting about how
7926 @code{make} decides what to do.  The @code{-d} option is equivalent to
7927 @samp{--debug=a} (see below).
7929 @item --debug[=@var{options}]
7930 @cindex @code{--debug}
7931 @c Extra blank line here makes the table look better.
7933 Print debugging information in addition to normal processing.  Various
7934 levels and types of output can be chosen.  With no arguments, print the
7935 ``basic'' level of debugging.  Possible arguments are below; only the
7936 first character is considered, and values must be comma- or
7937 space-separated.
7939 @table @code
7940 @item a (@i{all})
7941 All types of debugging output are enabled.  This is equivalent to using
7942 @samp{-d}.
7944 @item b (@i{basic})
7945 Basic debugging prints each target that was found to be out-of-date, and
7946 whether the build was successful or not.
7948 @item v (@i{verbose})
7949 A level above @samp{basic}; includes messages about which makefiles were
7950 parsed, prerequisites that did not need to be rebuilt, etc.  This option
7951 also enables @samp{basic} messages.
7953 @item i (@i{implicit})
7954 Prints messages describing the implicit rule searches for each target.
7955 This option also enables @samp{basic} messages.
7957 @item j (@i{jobs})
7958 Prints messages giving details on the invocation of specific subcommands.
7960 @item m (@i{makefile})
7961 By default, the above messages are not enabled while trying to remake
7962 the makefiles.  This option enables messages while rebuilding makefiles,
7963 too.  Note that the @samp{all} option does enable this option.  This
7964 option also enables @samp{basic} messages.
7965 @end table
7967 @item -e
7968 @cindex @code{-e}
7969 @itemx --environment-overrides
7970 @cindex @code{--environment-overrides}
7971 Give variables taken from the environment precedence
7972 over variables from makefiles.
7973 @xref{Environment, ,Variables from the Environment}.
7975 @item -f @var{file}
7976 @cindex @code{-f}
7977 @itemx --file=@var{file}
7978 @cindex @code{--file}
7979 @itemx --makefile=@var{file}
7980 @cindex @code{--makefile}
7981 Read the file named @var{file} as a makefile.
7982 @xref{Makefiles, ,Writing Makefiles}.
7984 @item -h
7985 @cindex @code{-h}
7986 @itemx --help
7987 @cindex @code{--help}
7988 @c Extra blank line here makes the table look better.
7990 Remind you of the options that @code{make} understands and then exit.
7992 @item -i
7993 @cindex @code{-i}
7994 @itemx --ignore-errors
7995 @cindex @code{--ignore-errors}
7996 Ignore all errors in commands executed to remake files.
7997 @xref{Errors, ,Errors in Commands}.
7999 @item -I @var{dir}
8000 @cindex @code{-I}
8001 @itemx --include-dir=@var{dir}
8002 @cindex @code{--include-dir}
8003 Specifies a directory @var{dir} to search for included makefiles.
8004 @xref{Include, ,Including Other Makefiles}.  If several @samp{-I}
8005 options are used to specify several directories, the directories are
8006 searched in the order specified.
8008 @item -j [@var{jobs}]
8009 @cindex @code{-j}
8010 @itemx --jobs[=@var{jobs}]
8011 @cindex @code{--jobs}
8012 Specifies the number of jobs (commands) to run simultaneously.  With no
8013 argument, @code{make} runs as many jobs simultaneously as possible.  If
8014 there is more than one @samp{-j} option, the last one is effective.
8015 @xref{Parallel, ,Parallel Execution},
8016 for more information on how commands are run.
8017 Note that this option is ignored on MS-DOS.
8019 @item -k
8020 @cindex @code{-k}
8021 @itemx --keep-going
8022 @cindex @code{--keep-going}
8023 Continue as much as possible after an error.  While the target that
8024 failed, and those that depend on it, cannot be remade, the other
8025 prerequisites of these targets can be processed all the same.
8026 @xref{Testing, ,Testing the Compilation of a Program}.
8028 @item -l [@var{load}]
8029 @cindex @code{-l}
8030 @itemx --load-average[=@var{load}]
8031 @cindex @code{--load-average}
8032 @itemx --max-load[=@var{load}]
8033 @cindex @code{--max-load}
8034 Specifies that no new jobs (commands) should be started if there are
8035 other jobs running and the load average is at least @var{load} (a
8036 floating-point number).  With no argument, removes a previous load
8037 limit.  @xref{Parallel, ,Parallel Execution}.
8039 @item -L
8040 @cindex @code{-L}
8041 @itemx --check-symlink-times
8042 @cindex @code{--check-symlink-times}
8043 On systems that support symbolic links, this option causes @code{make}
8044 to consider the timestamps on any symbolic links in addition to the
8045 timestamp on the file referenced by those links.  When this option is
8046 provided, the most recent timestamp among the file and the symbolic
8047 links is taken as the modification time for this target file.
8049 @item -n
8050 @cindex @code{-n}
8051 @itemx --just-print
8052 @cindex @code{--just-print}
8053 @itemx --dry-run
8054 @cindex @code{--dry-run}
8055 @itemx --recon
8056 @cindex @code{--recon}
8057 @c Extra blank line here makes the table look better.
8059 Print the commands that would be executed, but do not execute them.
8060 @xref{Instead of Execution, ,Instead of Executing the Commands}.
8062 @item -o @var{file}
8063 @cindex @code{-o}
8064 @itemx --old-file=@var{file}
8065 @cindex @code{--old-file}
8066 @itemx --assume-old=@var{file}
8067 @cindex @code{--assume-old}
8068 Do not remake the file @var{file} even if it is older than its
8069 prerequisites, and do not remake anything on account of changes in
8070 @var{file}.  Essentially the file is treated as very old and its rules
8071 are ignored.  @xref{Avoiding Compilation, ,Avoiding Recompilation of
8072 Some Files}.@refill
8074 @item -p
8075 @cindex @code{-p}
8076 @itemx --print-data-base
8077 @cindex @code{--print-data-base}
8078 @cindex data base of @code{make} rules
8079 @cindex predefined rules and variables, printing
8080 Print the data base (rules and variable values) that results from
8081 reading the makefiles; then execute as usual or as otherwise specified.
8082 This also prints the version information given by the @samp{-v} switch
8083 (see below).  To print the data base without trying to remake any files,
8084 use @w{@samp{make -qp}}.  To print the data base of predefined rules and
8085 variables, use @w{@samp{make -p -f /dev/null}}.  The data base output
8086 contains filename and linenumber information for command and variable
8087 definitions, so it can be a useful debugging tool in complex environments.
8089 @item -q
8090 @cindex @code{-q}
8091 @itemx --question
8092 @cindex @code{--question}
8093 ``Question mode''.  Do not run any commands, or print anything; just
8094 return an exit status that is zero if the specified targets are already
8095 up to date, one if any remaking is required, or two if an error is
8096 encountered.  @xref{Instead of Execution, ,Instead of Executing the
8097 Commands}.@refill
8099 @item -r
8100 @cindex @code{-r}
8101 @itemx --no-builtin-rules
8102 @cindex @code{--no-builtin-rules}
8103 Eliminate use of the built-in implicit rules (@pxref{Implicit Rules,
8104 ,Using Implicit Rules}).  You can still define your own by writing
8105 pattern rules (@pxref{Pattern Rules, ,Defining and Redefining Pattern
8106 Rules}).  The @samp{-r} option also clears out the default list of
8107 suffixes for suffix rules (@pxref{Suffix Rules, ,Old-Fashioned Suffix
8108 Rules}).  But you can still define your own suffixes with a rule for
8109 @code{.SUFFIXES}, and then define your own suffix rules.  Note that only
8110 @emph{rules} are affected by the @code{-r} option; default variables
8111 remain in effect (@pxref{Implicit Variables, ,Variables Used by Implicit
8112 Rules}); see the @samp{-R} option below.
8114 @item -R
8115 @cindex @code{-R}
8116 @itemx --no-builtin-variables
8117 @cindex @code{--no-builtin-variables}
8118 Eliminate use of the built-in rule-specific variables (@pxref{Implicit
8119 Variables, ,Variables Used by Implicit Rules}).  You can still define
8120 your own, of course.  The @samp{-R} option also automatically enables
8121 the @samp{-r} option (see above), since it doesn't make sense to have
8122 implicit rules without any definitions for the variables that they use.
8124 @item -s
8125 @cindex @code{-s}
8126 @itemx --silent
8127 @cindex @code{--silent}
8128 @itemx --quiet
8129 @cindex @code{--quiet}
8130 @c Extra blank line here makes the table look better.
8132 Silent operation; do not print the commands as they are executed.
8133 @xref{Echoing, ,Command Echoing}.
8135 @item -S
8136 @cindex @code{-S}
8137 @itemx --no-keep-going
8138 @cindex @code{--no-keep-going}
8139 @itemx --stop
8140 @cindex @code{--stop}
8141 @c Extra blank line here makes the table look better.
8143 Cancel the effect of the @samp{-k} option.  This is never necessary
8144 except in a recursive @code{make} where @samp{-k} might be inherited
8145 from the top-level @code{make} via @code{MAKEFLAGS}
8146 (@pxref{Recursion, ,Recursive Use of @code{make}})
8147 or if you set @samp{-k} in @code{MAKEFLAGS} in your environment.@refill
8149 @item -t
8150 @cindex @code{-t}
8151 @itemx --touch
8152 @cindex @code{--touch}
8153 @c Extra blank line here makes the table look better.
8155 Touch files (mark them up to date without really changing them)
8156 instead of running their commands.  This is used to pretend that the
8157 commands were done, in order to fool future invocations of
8158 @code{make}.  @xref{Instead of Execution, ,Instead of Executing the Commands}.
8160 @item -v
8161 @cindex @code{-v}
8162 @itemx --version
8163 @cindex @code{--version}
8164 Print the version of the @code{make} program plus a copyright, a list
8165 of authors, and a notice that there is no warranty; then exit.
8167 @item -w
8168 @cindex @code{-w}
8169 @itemx --print-directory
8170 @cindex @code{--print-directory}
8171 Print a message containing the working directory both before and after
8172 executing the makefile.  This may be useful for tracking down errors
8173 from complicated nests of recursive @code{make} commands.
8174 @xref{Recursion, ,Recursive Use of @code{make}}.  (In practice, you
8175 rarely need to specify this option since @samp{make} does it for you;
8176 see @ref{-w Option, ,The @samp{--print-directory} Option}.)
8178 @itemx --no-print-directory
8179 @cindex @code{--no-print-directory}
8180 Disable printing of the working directory under @code{-w}.
8181 This option is useful when @code{-w} is turned on automatically,
8182 but you do not want to see the extra messages.
8183 @xref{-w Option, ,The @samp{--print-directory} Option}.
8185 @item -W @var{file}
8186 @cindex @code{-W}
8187 @itemx --what-if=@var{file}
8188 @cindex @code{--what-if}
8189 @itemx --new-file=@var{file}
8190 @cindex @code{--new-file}
8191 @itemx --assume-new=@var{file}
8192 @cindex @code{--assume-new}
8193 Pretend that the target @var{file} has just been modified.  When used
8194 with the @samp{-n} flag, this shows you what would happen if you were
8195 to modify that file.  Without @samp{-n}, it is almost the same as
8196 running a @code{touch} command on the given file before running
8197 @code{make}, except that the modification time is changed only in the
8198 imagination of @code{make}.
8199 @xref{Instead of Execution, ,Instead of Executing the Commands}.
8201 @item --warn-undefined-variables
8202 @cindex @code{--warn-undefined-variables}
8203 @cindex variables, warning for undefined
8204 @cindex undefined variables, warning message
8205 Issue a warning message whenever @code{make} sees a reference to an
8206 undefined variable.  This can be helpful when you are trying to debug
8207 makefiles which use variables in complex ways.
8208 @end table
8210 @node Implicit Rules, Archives, Running, Top
8211 @chapter Using Implicit Rules
8212 @cindex implicit rule
8213 @cindex rule, implicit
8215 Certain standard ways of remaking target files are used very often.  For
8216 example, one customary way to make an object file is from a C source file
8217 using the C compiler, @code{cc}.
8219 @dfn{Implicit rules} tell @code{make} how to use customary techniques so
8220 that you do not have to specify them in detail when you want to use
8221 them.  For example, there is an implicit rule for C compilation.  File
8222 names determine which implicit rules are run.  For example, C
8223 compilation typically takes a @file{.c} file and makes a @file{.o} file.
8224 So @code{make} applies the implicit rule for C compilation when it sees
8225 this combination of file name endings.@refill
8227 A chain of implicit rules can apply in sequence; for example, @code{make}
8228 will remake a @file{.o} file from a @file{.y} file by way of a @file{.c} file.
8229 @iftex
8230 @xref{Chained Rules, ,Chains of Implicit Rules}.
8231 @end iftex
8233 The built-in implicit rules use several variables in their commands so
8234 that, by changing the values of the variables, you can change the way the
8235 implicit rule works.  For example, the variable @code{CFLAGS} controls the
8236 flags given to the C compiler by the implicit rule for C compilation.
8237 @iftex
8238 @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
8239 @end iftex
8241 You can define your own implicit rules by writing @dfn{pattern rules}.
8242 @iftex
8243 @xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.
8244 @end iftex
8246 @dfn{Suffix rules} are a more limited way to define implicit rules.
8247 Pattern rules are more general and clearer, but suffix rules are
8248 retained for compatibility.
8249 @iftex
8250 @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
8251 @end iftex
8253 @menu
8254 * Using Implicit::              How to use an existing implicit rule
8255                                   to get the commands for updating a file.
8256 * Catalogue of Rules::          A list of built-in implicit rules.
8257 * Implicit Variables::          How to change what predefined rules do.
8258 * Chained Rules::               How to use a chain of implicit rules.
8259 * Pattern Rules::               How to define new implicit rules.
8260 * Last Resort::                 How to define commands for rules which
8261                                   cannot find any.
8262 * Suffix Rules::                The old-fashioned style of implicit rule.
8263 * Implicit Rule Search::        The precise algorithm for applying
8264                                   implicit rules.
8265 @end menu
8267 @node Using Implicit, Catalogue of Rules, Implicit Rules, Implicit Rules
8268 @section Using Implicit Rules
8269 @cindex implicit rule, how to use
8270 @cindex rule, implicit, how to use
8272 To allow @code{make} to find a customary method for updating a target file,
8273 all you have to do is refrain from specifying commands yourself.  Either
8274 write a rule with no command lines, or don't write a rule at all.  Then
8275 @code{make} will figure out which implicit rule to use based on which
8276 kind of source file exists or can be made.
8278 For example, suppose the makefile looks like this:
8280 @example
8281 foo : foo.o bar.o
8282         cc -o foo foo.o bar.o $(CFLAGS) $(LDFLAGS)
8283 @end example
8285 @noindent
8286 Because you mention @file{foo.o} but do not give a rule for it, @code{make}
8287 will automatically look for an implicit rule that tells how to update it.
8288 This happens whether or not the file @file{foo.o} currently exists.
8290 If an implicit rule is found, it can supply both commands and one or
8291 more prerequisites (the source files).  You would want to write a rule
8292 for @file{foo.o} with no command lines if you need to specify additional
8293 prerequisites, such as header files, that the implicit rule cannot
8294 supply.
8296 Each implicit rule has a target pattern and prerequisite patterns.  There may
8297 be many implicit rules with the same target pattern.  For example, numerous
8298 rules make @samp{.o} files: one, from a @samp{.c} file with the C compiler;
8299 another, from a @samp{.p} file with the Pascal compiler; and so on.  The rule
8300 that actually applies is the one whose prerequisites exist or can be made.
8301 So, if you have a file @file{foo.c}, @code{make} will run the C compiler;
8302 otherwise, if you have a file @file{foo.p}, @code{make} will run the Pascal
8303 compiler; and so on.
8305 Of course, when you write the makefile, you know which implicit rule you
8306 want @code{make} to use, and you know it will choose that one because you
8307 know which possible prerequisite files are supposed to exist.
8308 @xref{Catalogue of Rules, ,Catalogue of Implicit Rules},
8309 for a catalogue of all the predefined implicit rules.
8311 Above, we said an implicit rule applies if the required prerequisites ``exist
8312 or can be made''.  A file ``can be made'' if it is mentioned explicitly in
8313 the makefile as a target or a prerequisite, or if an implicit rule can be
8314 recursively found for how to make it.  When an implicit prerequisite is the
8315 result of another implicit rule, we say that @dfn{chaining} is occurring.
8316 @xref{Chained Rules, ,Chains of Implicit Rules}.
8318 In general, @code{make} searches for an implicit rule for each target, and
8319 for each double-colon rule, that has no commands.  A file that is mentioned
8320 only as a prerequisite is considered a target whose rule specifies nothing,
8321 so implicit rule search happens for it.  @xref{Implicit Rule Search, ,Implicit Rule Search Algorithm}, for the
8322 details of how the search is done.
8324 Note that explicit prerequisites do not influence implicit rule search.
8325 For example, consider this explicit rule:
8327 @example
8328 foo.o: foo.p
8329 @end example
8331 @noindent
8332 The prerequisite on @file{foo.p} does not necessarily mean that
8333 @code{make} will remake @file{foo.o} according to the implicit rule to
8334 make an object file, a @file{.o} file, from a Pascal source file, a
8335 @file{.p} file.  For example, if @file{foo.c} also exists, the implicit
8336 rule to make an object file from a C source file is used instead,
8337 because it appears before the Pascal rule in the list of predefined
8338 implicit rules (@pxref{Catalogue of Rules, , Catalogue of Implicit
8339 Rules}).
8341 If you do not want an implicit rule to be used for a target that has no
8342 commands, you can give that target empty commands by writing a semicolon
8343 (@pxref{Empty Commands, ,Defining Empty Commands}).
8345 @node Catalogue of Rules, Implicit Variables, Using Implicit, Implicit Rules
8346 @section Catalogue of Implicit Rules
8347 @cindex implicit rule, predefined
8348 @cindex rule, implicit, predefined
8350 Here is a catalogue of predefined implicit rules which are always
8351 available unless the makefile explicitly overrides or cancels them.
8352 @xref{Canceling Rules, ,Canceling Implicit Rules}, for information on
8353 canceling or overriding an implicit rule.  The @samp{-r} or
8354 @samp{--no-builtin-rules} option cancels all predefined rules.
8356 This manual only documents the default rules available on POSIX-based
8357 operating systems.  Other operating systems, such as VMS, Windows,
8358 OS/2, etc. may have different sets of default rules.  To see the full
8359 list of default rules and variables available in your version of GNU
8360 @code{make}, run @samp{make -p} in a directory with no makefile.
8362 Not all of these rules will always be defined, even when the @samp{-r}
8363 option is not given.  Many of the predefined implicit rules are
8364 implemented in @code{make} as suffix rules, so which ones will be
8365 defined depends on the @dfn{suffix list} (the list of prerequisites of
8366 the special target @code{.SUFFIXES}).  The default suffix list is:
8367 @code{.out}, @code{.a}, @code{.ln}, @code{.o}, @code{.c}, @code{.cc},
8368 @code{.C}, @code{.cpp}, @code{.p}, @code{.f}, @code{.F}, @code{.r}, @code{.y},
8369 @code{.l}, @code{.s}, @code{.S}, @code{.mod}, @code{.sym}, @code{.def},
8370 @code{.h}, @code{.info}, @code{.dvi}, @code{.tex}, @code{.texinfo},
8371 @code{.texi}, @code{.txinfo}, @code{.w}, @code{.ch} @code{.web},
8372 @code{.sh}, @code{.elc}, @code{.el}.  All of the implicit rules
8373 described below whose prerequisites have one of these suffixes are
8374 actually suffix rules.  If you modify the suffix list, the only
8375 predefined suffix rules in effect will be those named by one or two of
8376 the suffixes that are on the list you specify; rules whose suffixes fail
8377 to be on the list are disabled.  @xref{Suffix Rules, ,Old-Fashioned
8378 Suffix Rules}, for full details on suffix rules.
8380 @table @asis
8381 @item Compiling C programs
8382 @cindex C, rule to compile
8383 @pindex cc
8384 @pindex gcc
8385 @pindex .o
8386 @pindex .c
8387 @file{@var{n}.o} is made automatically from @file{@var{n}.c} with
8388 a command of the form @samp{$(CC) -c $(CPPFLAGS) $(CFLAGS)}.@refill
8390 @item Compiling C++ programs
8391 @cindex C++, rule to compile
8392 @pindex g++
8393 @pindex .cc
8394 @pindex .cpp
8395 @pindex .C
8396 @file{@var{n}.o} is made automatically from @file{@var{n}.cc},
8397 @file{@var{n}.cpp}, or @file{@var{n}.C} with a command of the form
8398 @samp{$(CXX) -c $(CPPFLAGS) $(CXXFLAGS)}.  We encourage you to use the
8399 suffix @samp{.cc} for C++ source files instead of @samp{.C}.@refill
8401 @item Compiling Pascal programs
8402 @cindex Pascal, rule to compile
8403 @pindex pc
8404 @pindex .p
8405 @file{@var{n}.o} is made automatically from @file{@var{n}.p}
8406 with the command @samp{$(PC) -c $(PFLAGS)}.@refill
8408 @item Compiling Fortran and Ratfor programs
8409 @cindex Fortran, rule to compile
8410 @cindex Ratfor, rule to compile
8411 @pindex f77
8412 @pindex .f
8413 @pindex .r
8414 @pindex .F
8415 @file{@var{n}.o} is made automatically from @file{@var{n}.r},
8416 @file{@var{n}.F} or @file{@var{n}.f} by running the
8417 Fortran compiler.  The precise command used is as follows:@refill
8419 @table @samp
8420 @item .f
8421 @samp{$(FC) -c $(FFLAGS)}.
8422 @item .F
8423 @samp{$(FC) -c $(FFLAGS) $(CPPFLAGS)}.
8424 @item .r
8425 @samp{$(FC) -c $(FFLAGS) $(RFLAGS)}.
8426 @end table
8428 @item Preprocessing Fortran and Ratfor programs
8429 @file{@var{n}.f} is made automatically from @file{@var{n}.r} or
8430 @file{@var{n}.F}.  This rule runs just the preprocessor to convert a
8431 Ratfor or preprocessable Fortran program into a strict Fortran
8432 program.  The precise command used is as follows:@refill
8434 @table @samp
8435 @item .F
8436 @samp{$(FC) -F $(CPPFLAGS) $(FFLAGS)}.
8437 @item .r
8438 @samp{$(FC) -F $(FFLAGS) $(RFLAGS)}.
8439 @end table
8441 @item Compiling Modula-2 programs
8442 @cindex Modula-2, rule to compile
8443 @pindex m2c
8444 @pindex .sym
8445 @pindex .def
8446 @pindex .mod
8447 @file{@var{n}.sym} is made from @file{@var{n}.def} with a command
8448 of the form @samp{$(M2C) $(M2FLAGS) $(DEFFLAGS)}.  @file{@var{n}.o}
8449 is made from @file{@var{n}.mod}; the form is:
8450 @w{@samp{$(M2C) $(M2FLAGS) $(MODFLAGS)}}.@refill
8452 @need 1200
8453 @item Assembling and preprocessing assembler programs
8454 @cindex assembly, rule to compile
8455 @pindex as
8456 @pindex .s
8457 @file{@var{n}.o} is made automatically from @file{@var{n}.s} by
8458 running the assembler, @code{as}.  The precise command is
8459 @samp{$(AS) $(ASFLAGS)}.@refill
8461 @pindex .S
8462 @file{@var{n}.s} is made automatically from @file{@var{n}.S} by
8463 running the C preprocessor, @code{cpp}.  The precise command is
8464 @w{@samp{$(CPP) $(CPPFLAGS)}}.
8466 @item Linking a single object file
8467 @cindex linking, predefined rule for
8468 @pindex ld
8469 @pindex .o
8470 @file{@var{n}} is made automatically from @file{@var{n}.o} by running
8471 the linker (usually called @code{ld}) via the C compiler.  The precise
8472 command used is @w{@samp{$(CC) $(LDFLAGS) @var{n}.o $(LOADLIBES) $(LDLIBS)}}.
8474 This rule does the right thing for a simple program with only one
8475 source file.  It will also do the right thing if there are multiple
8476 object files (presumably coming from various other source files), one
8477 of which has a name matching that of the executable file.  Thus,
8479 @example
8480 x: y.o z.o
8481 @end example
8483 @noindent
8484 when @file{x.c}, @file{y.c} and @file{z.c} all exist will execute:
8486 @example
8487 @group
8488 cc -c x.c -o x.o
8489 cc -c y.c -o y.o
8490 cc -c z.c -o z.o
8491 cc x.o y.o z.o -o x
8492 rm -f x.o
8493 rm -f y.o
8494 rm -f z.o
8495 @end group
8496 @end example
8498 @noindent
8499 In more complicated cases, such as when there is no object file whose
8500 name derives from the executable file name, you must write an explicit
8501 command for linking.
8503 Each kind of file automatically made into @samp{.o} object files will
8504 be automatically linked by using the compiler (@samp{$(CC)},
8505 @samp{$(FC)} or @samp{$(PC)}; the C compiler @samp{$(CC)} is used to
8506 assemble @samp{.s} files) without the @samp{-c} option.  This could be
8507 done by using the @samp{.o} object files as intermediates, but it is
8508 faster to do the compiling and linking in one step, so that's how it's
8509 done.@refill
8511 @item Yacc for C programs
8512 @pindex yacc
8513 @cindex Yacc, rule to run
8514 @pindex .y
8515 @file{@var{n}.c} is made automatically from @file{@var{n}.y} by
8516 running Yacc with the command @samp{$(YACC) $(YFLAGS)}.
8518 @item Lex for C programs
8519 @pindex lex
8520 @cindex Lex, rule to run
8521 @pindex .l
8522 @file{@var{n}.c} is made automatically from @file{@var{n}.l} by
8523 running Lex.  The actual command is @samp{$(LEX) $(LFLAGS)}.
8525 @item Lex for Ratfor programs
8526 @file{@var{n}.r} is made automatically from @file{@var{n}.l} by
8527 running Lex.  The actual command is @samp{$(LEX) $(LFLAGS)}.
8529 The convention of using the same suffix @samp{.l} for all Lex files
8530 regardless of whether they produce C code or Ratfor code makes it
8531 impossible for @code{make} to determine automatically which of the two
8532 languages you are using in any particular case.  If @code{make} is
8533 called upon to remake an object file from a @samp{.l} file, it must
8534 guess which compiler to use.  It will guess the C compiler, because
8535 that is more common.  If you are using Ratfor, make sure @code{make}
8536 knows this by mentioning @file{@var{n}.r} in the makefile.  Or, if you
8537 are using Ratfor exclusively, with no C files, remove @samp{.c} from
8538 the list of implicit rule suffixes with:@refill
8540 @example
8541 @group
8542 .SUFFIXES:
8543 .SUFFIXES: .o .r .f .l @dots{}
8544 @end group
8545 @end example
8547 @item Making Lint Libraries from C, Yacc, or Lex programs
8548 @pindex lint
8549 @cindex @code{lint}, rule to run
8550 @pindex .ln
8551 @file{@var{n}.ln} is made from @file{@var{n}.c} by running @code{lint}.
8552 The precise command is @w{@samp{$(LINT) $(LINTFLAGS) $(CPPFLAGS) -i}}.
8553 The same command is used on the C code produced from
8554 @file{@var{n}.y} or @file{@var{n}.l}.@refill
8556 @item @TeX{} and Web
8557 @cindex @TeX{}, rule to run
8558 @cindex Web, rule to run
8559 @pindex tex
8560 @pindex cweave
8561 @pindex weave
8562 @pindex tangle
8563 @pindex ctangle
8564 @pindex .dvi
8565 @pindex .tex
8566 @pindex .web
8567 @pindex .w
8568 @pindex .ch
8569 @file{@var{n}.dvi} is made from @file{@var{n}.tex} with the command
8570 @samp{$(TEX)}.  @file{@var{n}.tex} is made from @file{@var{n}.web} with
8571 @samp{$(WEAVE)}, or from @file{@var{n}.w} (and from @file{@var{n}.ch} if
8572 it exists or can be made) with @samp{$(CWEAVE)}.  @file{@var{n}.p} is
8573 made from @file{@var{n}.web} with @samp{$(TANGLE)} and @file{@var{n}.c}
8574 is made from @file{@var{n}.w} (and from @file{@var{n}.ch} if it exists
8575 or can be made) with @samp{$(CTANGLE)}.@refill
8577 @item Texinfo and Info
8578 @cindex Texinfo, rule to format
8579 @cindex Info, rule to format
8580 @pindex texi2dvi
8581 @pindex makeinfo
8582 @pindex .texinfo
8583 @pindex .info
8584 @pindex .texi
8585 @pindex .txinfo
8586 @file{@var{n}.dvi} is made from @file{@var{n}.texinfo},
8587 @file{@var{n}.texi}, or @file{@var{n}.txinfo}, with the command
8588 @w{@samp{$(TEXI2DVI) $(TEXI2DVI_FLAGS)}}.  @file{@var{n}.info} is made from
8589 @file{@var{n}.texinfo}, @file{@var{n}.texi}, or @file{@var{n}.txinfo}, with
8590 the command @w{@samp{$(MAKEINFO) $(MAKEINFO_FLAGS)}}.
8592 @item RCS
8593 @cindex RCS, rule to extract from
8594 @pindex co
8595 @pindex ,v @r{(RCS file extension)}
8596 Any file @file{@var{n}} is extracted if necessary from an RCS file
8597 named either @file{@var{n},v} or @file{RCS/@var{n},v}.  The precise
8598 command used is @w{@samp{$(CO) $(COFLAGS)}}.  @file{@var{n}} will not be
8599 extracted from RCS if it already exists, even if the RCS file is
8600 newer.  The rules for RCS are terminal
8601 (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}),
8602 so RCS files cannot be generated from another source; they must
8603 actually exist.@refill
8605 @item SCCS
8606 @cindex SCCS, rule to extract from
8607 @pindex get
8608 @pindex s. @r{(SCCS file prefix)}
8609 Any file @file{@var{n}} is extracted if necessary from an SCCS file
8610 named either @file{s.@var{n}} or @file{SCCS/s.@var{n}}.  The precise
8611 command used is @w{@samp{$(GET) $(GFLAGS)}}.  The rules for SCCS are
8612 terminal (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}),
8613 so SCCS files cannot be generated from another source; they must
8614 actually exist.@refill
8616 @pindex .sh
8617 For the benefit of SCCS, a file @file{@var{n}} is copied from
8618 @file{@var{n}.sh} and made executable (by everyone).  This is for
8619 shell scripts that are checked into SCCS.  Since RCS preserves the
8620 execution permission of a file, you do not need to use this feature
8621 with RCS.@refill
8623 We recommend that you avoid using of SCCS.  RCS is widely held to be
8624 superior, and is also free.  By choosing free software in place of
8625 comparable (or inferior) proprietary software, you support the free
8626 software movement.
8627 @end table
8629 Usually, you want to change only the variables listed in the table
8630 above, which are documented in the following section.
8632 However, the commands in built-in implicit rules actually use
8633 variables such as @code{COMPILE.c}, @code{LINK.p}, and
8634 @code{PREPROCESS.S}, whose values contain the commands listed above.
8636 @code{make} follows the convention that the rule to compile a
8637 @file{.@var{x}} source file uses the variable @code{COMPILE.@var{x}}.
8638 Similarly, the rule to produce an executable from a @file{.@var{x}}
8639 file uses @code{LINK.@var{x}}; and the rule to preprocess a
8640 @file{.@var{x}} file uses @code{PREPROCESS.@var{x}}.
8642 @vindex OUTPUT_OPTION
8643 Every rule that produces an object file uses the variable
8644 @code{OUTPUT_OPTION}.  @code{make} defines this variable either to
8645 contain @samp{-o $@@}, or to be empty, depending on a compile-time
8646 option.  You need the @samp{-o} option to ensure that the output goes
8647 into the right file when the source file is in a different directory,
8648 as when using @code{VPATH} (@pxref{Directory Search}).  However,
8649 compilers on some systems do not accept a @samp{-o} switch for object
8650 files.  If you use such a system, and use @code{VPATH}, some
8651 compilations will put their output in the wrong place.
8652 A possible workaround for this problem is to give @code{OUTPUT_OPTION}
8653 the value @w{@samp{; mv $*.o $@@}}.
8655 @node Implicit Variables, Chained Rules, Catalogue of Rules, Implicit Rules
8656 @section Variables Used by Implicit Rules
8657 @cindex flags for compilers
8659 The commands in built-in implicit rules make liberal use of certain
8660 predefined variables.  You can alter the values of these variables in
8661 the makefile, with arguments to @code{make}, or in the environment to
8662 alter how the implicit rules work without redefining the rules
8663 themselves.  You can cancel all variables used by implicit rules with
8664 the @samp{-R} or @samp{--no-builtin-variables} option.
8666 For example, the command used to compile a C source file actually says
8667 @samp{$(CC) -c $(CFLAGS) $(CPPFLAGS)}.  The default values of the variables
8668 used are @samp{cc} and nothing, resulting in the command @samp{cc -c}.  By
8669 redefining @samp{CC} to @samp{ncc}, you could cause @samp{ncc} to be
8670 used for all C compilations performed by the implicit rule.  By redefining
8671 @samp{CFLAGS} to be @samp{-g}, you could pass the @samp{-g} option to
8672 each compilation.  @emph{All} implicit rules that do C compilation use
8673 @samp{$(CC)} to get the program name for the compiler and @emph{all}
8674 include @samp{$(CFLAGS)} among the arguments given to the compiler.@refill
8676 The variables used in implicit rules fall into two classes: those that are
8677 names of programs (like @code{CC}) and those that contain arguments for the
8678 programs (like @code{CFLAGS}).  (The ``name of a program'' may also contain
8679 some command arguments, but it must start with an actual executable program
8680 name.)  If a variable value contains more than one argument, separate them
8681 with spaces.
8683 The following tables describe of some of the more commonly-used predefined
8684 variables.  This list is not exhaustive, and the default values shown here may
8685 not be what are selected by @code{make} for your environment.  To see the
8686 complete list of predefined variables for your instance of GNU @code{make} you
8687 can run @samp{make -p} in a directory with no makefiles.
8689 Here is a table of some of the more common variables used as names of
8690 programs in built-in rules:
8691 makefiles.
8693 @table @code
8694 @item AR
8695 @vindex AR
8696 Archive-maintaining program; default @samp{ar}.
8697 @pindex ar
8699 @item AS
8700 @vindex AS
8701 Program for compiling assembly files; default @samp{as}.
8702 @pindex as
8704 @item CC
8705 @vindex CC
8706 Program for compiling C programs; default @samp{cc}.
8707 @pindex cc
8709 @item CO
8710 @vindex CO
8711 Program for checking out files from RCS; default @samp{co}.
8712 @pindex cc
8714 @item CXX
8715 @vindex CXX
8716 Program for compiling C++ programs; default @samp{g++}.
8717 @pindex g++
8719 @item CO
8720 @vindex CO
8721 Program for extracting a file from RCS; default @samp{co}.
8722 @pindex co
8724 @item CPP
8725 @vindex CPP
8726 Program for running the C preprocessor, with results to standard output;
8727 default @samp{$(CC) -E}.
8729 @item FC
8730 @vindex FC
8731 Program for compiling or preprocessing Fortran and Ratfor programs;
8732 default @samp{f77}.
8733 @pindex f77
8735 @item GET
8736 @vindex GET
8737 Program for extracting a file from SCCS; default @samp{get}.
8738 @pindex get
8740 @item LEX
8741 @vindex LEX
8742 Program to use to turn Lex grammars into source code; default @samp{lex}.
8743 @pindex lex
8745 @item YACC
8746 @vindex YACC
8747 Program to use to turn Yacc grammars into source code; default @samp{yacc}.
8748 @pindex yacc
8750 @item LINT
8751 @vindex LINT
8752 Program to use to run lint on source code; default @samp{lint}.
8753 @pindex lint
8755 @item M2C
8756 @vindex M2C
8757 Program to use to compile Modula-2 source code; default @samp{m2c}.
8758 @pindex m2c
8760 @item PC
8761 @vindex PC
8762 Program for compiling Pascal programs; default @samp{pc}.
8763 @pindex pc
8765 @item MAKEINFO
8766 @vindex MAKEINFO
8767 Program to convert a Texinfo source file into an Info file; default
8768 @samp{makeinfo}.
8769 @pindex makeinfo
8771 @item TEX
8772 @vindex TEX
8773 Program to make @TeX{} @sc{dvi} files from @TeX{} source;
8774 default @samp{tex}.
8775 @pindex tex
8777 @item TEXI2DVI
8778 @vindex TEXI2DVI
8779 Program to make @TeX{} @sc{dvi} files from Texinfo source;
8780 default @samp{texi2dvi}.
8781 @pindex texi2dvi
8783 @item WEAVE
8784 @vindex WEAVE
8785 Program to translate Web into @TeX{}; default @samp{weave}.
8786 @pindex weave
8788 @item CWEAVE
8789 @vindex CWEAVE
8790 Program to translate C Web into @TeX{}; default @samp{cweave}.
8791 @pindex cweave
8793 @item TANGLE
8794 @vindex TANGLE
8795 Program to translate Web into Pascal; default @samp{tangle}.
8796 @pindex tangle
8798 @item CTANGLE
8799 @vindex CTANGLE
8800 Program to translate C Web into C; default @samp{ctangle}.
8801 @pindex ctangle
8803 @item RM
8804 @vindex RM
8805 Command to remove a file; default @samp{rm -f}.
8806 @pindex rm
8807 @end table
8809 Here is a table of variables whose values are additional arguments for the
8810 programs above.  The default values for all of these is the empty
8811 string, unless otherwise noted.
8813 @table @code
8814 @item ARFLAGS
8815 @vindex ARFLAGS
8816 Flags to give the archive-maintaining program; default @samp{rv}.
8818 @item ASFLAGS
8819 @vindex ASFLAGS
8820 Extra flags to give to the assembler (when explicitly
8821 invoked on a @samp{.s} or @samp{.S} file).
8823 @item CFLAGS
8824 @vindex CFLAGS
8825 Extra flags to give to the C compiler.
8827 @item CXXFLAGS
8828 @vindex CXXFLAGS
8829 Extra flags to give to the C++ compiler.
8831 @item COFLAGS
8832 @vindex COFLAGS
8833 Extra flags to give to the RCS @code{co} program.
8835 @item CPPFLAGS
8836 @vindex CPPFLAGS
8837 Extra flags to give to the C preprocessor and programs
8838 that use it (the C and Fortran compilers).
8840 @item FFLAGS
8841 @vindex FFLAGS
8842 Extra flags to give to the Fortran compiler.
8844 @item GFLAGS
8845 @vindex GFLAGS
8846 Extra flags to give to the SCCS @code{get} program.
8848 @item LDFLAGS
8849 @vindex LDFLAGS
8850 Extra flags to give to compilers when they are supposed to invoke the linker,
8851 @samp{ld}.
8853 @item LFLAGS
8854 @vindex LFLAGS
8855 Extra flags to give to Lex.
8857 @item YFLAGS
8858 @vindex YFLAGS
8859 Extra flags to give to Yacc.
8861 @item PFLAGS
8862 @vindex PFLAGS
8863 Extra flags to give to the Pascal compiler.
8865 @item RFLAGS
8866 @vindex RFLAGS
8867 Extra flags to give to the Fortran compiler for Ratfor programs.
8869 @item LINTFLAGS
8870 @vindex LINTFLAGS
8871 Extra flags to give to lint.
8872 @end table
8874 @node Chained Rules, Pattern Rules, Implicit Variables, Implicit Rules
8875 @section Chains of Implicit Rules
8877 @cindex chains of rules
8878 @cindex rule, implicit, chains of
8879 Sometimes a file can be made by a sequence of implicit rules.  For example,
8880 a file @file{@var{n}.o} could be made from @file{@var{n}.y} by running
8881 first Yacc and then @code{cc}.  Such a sequence is called a @dfn{chain}.
8883 If the file @file{@var{n}.c} exists, or is mentioned in the makefile, no
8884 special searching is required: @code{make} finds that the object file can
8885 be made by C compilation from @file{@var{n}.c}; later on, when considering
8886 how to make @file{@var{n}.c}, the rule for running Yacc is
8887 used.  Ultimately both @file{@var{n}.c} and @file{@var{n}.o} are
8888 updated.@refill
8890 @cindex intermediate files
8891 @cindex files, intermediate
8892 However, even if @file{@var{n}.c} does not exist and is not mentioned,
8893 @code{make} knows how to envision it as the missing link between
8894 @file{@var{n}.o} and @file{@var{n}.y}!  In this case, @file{@var{n}.c} is
8895 called an @dfn{intermediate file}.  Once @code{make} has decided to use the
8896 intermediate file, it is entered in the data base as if it had been
8897 mentioned in the makefile, along with the implicit rule that says how to
8898 create it.@refill
8900 Intermediate files are remade using their rules just like all other
8901 files.  But intermediate files are treated differently in two ways.
8903 The first difference is what happens if the intermediate file does not
8904 exist.  If an ordinary file @var{b} does not exist, and @code{make}
8905 considers a target that depends on @var{b}, it invariably creates
8906 @var{b} and then updates the target from @var{b}.  But if @var{b} is an
8907 intermediate file, then @code{make} can leave well enough alone.  It
8908 won't bother updating @var{b}, or the ultimate target, unless some
8909 prerequisite of @var{b} is newer than that target or there is some other
8910 reason to update that target.
8912 The second difference is that if @code{make} @emph{does} create @var{b}
8913 in order to update something else, it deletes @var{b} later on after it
8914 is no longer needed.  Therefore, an intermediate file which did not
8915 exist before @code{make} also does not exist after @code{make}.
8916 @code{make} reports the deletion to you by printing a @samp{rm -f}
8917 command showing which file it is deleting.
8919 Ordinarily, a file cannot be intermediate if it is mentioned in the
8920 makefile as a target or prerequisite.  However, you can explicitly mark a
8921 file as intermediate by listing it as a prerequisite of the special target
8922 @code{.INTERMEDIATE}.  This takes effect even if the file is mentioned
8923 explicitly in some other way.
8925 @cindex intermediate files, preserving
8926 @cindex preserving intermediate files
8927 @cindex secondary files
8928 You can prevent automatic deletion of an intermediate file by marking it
8929 as a @dfn{secondary} file.  To do this, list it as a prerequisite of the
8930 special target @code{.SECONDARY}.  When a file is secondary, @code{make}
8931 will not create the file merely because it does not already exist, but
8932 @code{make} does not automatically delete the file.  Marking a file as
8933 secondary also marks it as intermediate.
8935 You can list the target pattern of an implicit rule (such as @samp{%.o})
8936 as a prerequisite of the special target @code{.PRECIOUS} to preserve
8937 intermediate files made by implicit rules whose target patterns match
8938 that file's name; see @ref{Interrupts}.@refill
8939 @cindex preserving with @code{.PRECIOUS}
8940 @cindex @code{.PRECIOUS} intermediate files
8942 A chain can involve more than two implicit rules.  For example, it is
8943 possible to make a file @file{foo} from @file{RCS/foo.y,v} by running RCS,
8944 Yacc and @code{cc}.  Then both @file{foo.y} and @file{foo.c} are
8945 intermediate files that are deleted at the end.@refill
8947 No single implicit rule can appear more than once in a chain.  This means
8948 that @code{make} will not even consider such a ridiculous thing as making
8949 @file{foo} from @file{foo.o.o} by running the linker twice.  This
8950 constraint has the added benefit of preventing any infinite loop in the
8951 search for an implicit rule chain.
8953 There are some special implicit rules to optimize certain cases that would
8954 otherwise be handled by rule chains.  For example, making @file{foo} from
8955 @file{foo.c} could be handled by compiling and linking with separate
8956 chained rules, using @file{foo.o} as an intermediate file.  But what
8957 actually happens is that a special rule for this case does the compilation
8958 and linking with a single @code{cc} command.  The optimized rule is used in
8959 preference to the step-by-step chain because it comes earlier in the
8960 ordering of rules.
8962 @node Pattern Rules, Last Resort, Chained Rules, Implicit Rules
8963 @section Defining and Redefining Pattern Rules
8965 You define an implicit rule by writing a @dfn{pattern rule}.  A pattern
8966 rule looks like an ordinary rule, except that its target contains the
8967 character @samp{%} (exactly one of them).  The target is considered a
8968 pattern for matching file names; the @samp{%} can match any nonempty
8969 substring, while other characters match only themselves.  The prerequisites
8970 likewise use @samp{%} to show how their names relate to the target name.
8972 Thus, a pattern rule @samp{%.o : %.c} says how to make any file
8973 @file{@var{stem}.o} from another file @file{@var{stem}.c}.@refill
8975 Note that expansion using @samp{%} in pattern rules occurs
8976 @strong{after} any variable or function expansions, which take place
8977 when the makefile is read.  @xref{Using Variables, , How to Use
8978 Variables}, and @ref{Functions, ,Functions for Transforming Text}.
8980 @menu
8981 * Pattern Intro::               An introduction to pattern rules.
8982 * Pattern Examples::            Examples of pattern rules.
8983 * Automatic Variables::         How to use automatic variables in the
8984                                   commands of implicit rules.
8985 * Pattern Match::               How patterns match.
8986 * Match-Anything Rules::        Precautions you should take prior to
8987                                   defining rules that can match any
8988                                   target file whatever.
8989 * Canceling Rules::             How to override or cancel built-in rules.
8990 @end menu
8992 @node Pattern Intro, Pattern Examples, Pattern Rules, Pattern Rules
8993 @subsection Introduction to Pattern Rules
8994 @cindex pattern rule
8995 @cindex rule, pattern
8997 A pattern rule contains the character @samp{%} (exactly one of them)
8998 in the target; otherwise, it looks exactly like an ordinary rule.  The
8999 target is a pattern for matching file names; the @samp{%} matches any
9000 nonempty substring, while other characters match only themselves.
9001 @cindex target pattern, implicit
9002 @cindex @code{%}, in pattern rules
9004 For example, @samp{%.c} as a pattern matches any file name that ends in
9005 @samp{.c}.  @samp{s.%.c} as a pattern matches any file name that starts
9006 with @samp{s.}, ends in @samp{.c} and is at least five characters long.
9007 (There must be at least one character to match the @samp{%}.)  The substring
9008 that the @samp{%} matches is called the @dfn{stem}.@refill
9010 @samp{%} in a prerequisite of a pattern rule stands for the same stem
9011 that was matched by the @samp{%} in the target.  In order for the
9012 pattern rule to apply, its target pattern must match the file name
9013 under consideration and all of its prerequisites (after pattern
9014 substitution) must name files that exist or can be made.  These files
9015 become prerequisites of the target.
9016 @cindex prerequisite pattern, implicit
9018 Thus, a rule of the form
9020 @example
9021 %.o : %.c ; @var{command}@dots{}
9022 @end example
9024 @noindent
9025 specifies how to make a file @file{@var{n}.o}, with another file
9026 @file{@var{n}.c} as its prerequisite, provided that @file{@var{n}.c}
9027 exists or can be made.
9029 There may also be prerequisites that do not use @samp{%}; such a prerequisite
9030 attaches to every file made by this pattern rule.  These unvarying
9031 prerequisites are useful occasionally.
9033 A pattern rule need not have any prerequisites that contain @samp{%}, or
9034 in fact any prerequisites at all.  Such a rule is effectively a general
9035 wildcard.  It provides a way to make any file that matches the target
9036 pattern.  @xref{Last Resort}.
9038 @c !!! The end of of this paragraph should be rewritten.  --bob
9039 Pattern rules may have more than one target.  Unlike normal rules, this
9040 does not act as many different rules with the same prerequisites and
9041 commands.  If a pattern rule has multiple targets, @code{make} knows that
9042 the rule's commands are responsible for making all of the targets.  The
9043 commands are executed only once to make all the targets.  When searching
9044 for a pattern rule to match a target, the target patterns of a rule other
9045 than the one that matches the target in need of a rule are incidental:
9046 @code{make} worries only about giving commands and prerequisites to the file
9047 presently in question.  However, when this file's commands are run, the
9048 other targets are marked as having been updated themselves.
9049 @cindex multiple targets, in pattern rule
9050 @cindex target, multiple in pattern rule
9052 The order in which pattern rules appear in the makefile is important
9053 since this is the order in which they are considered.
9054 Of equally applicable
9055 rules, only the first one found is used.  The rules you write take precedence
9056 over those that are built in.  Note however, that a rule whose
9057 prerequisites actually exist or are mentioned always takes priority over a
9058 rule with prerequisites that must be made by chaining other implicit rules.
9059 @cindex pattern rules, order of
9060 @cindex order of pattern rules
9062 @node Pattern Examples, Automatic Variables, Pattern Intro, Pattern Rules
9063 @subsection Pattern Rule Examples
9065 Here are some examples of pattern rules actually predefined in
9066 @code{make}.  First, the rule that compiles @samp{.c} files into @samp{.o}
9067 files:@refill
9069 @example
9070 %.o : %.c
9071         $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@@
9072 @end example
9074 @noindent
9075 defines a rule that can make any file @file{@var{x}.o} from
9076 @file{@var{x}.c}.  The command uses the automatic variables @samp{$@@} and
9077 @samp{$<} to substitute the names of the target file and the source file
9078 in each case where the rule applies (@pxref{Automatic Variables}).@refill
9080 Here is a second built-in rule:
9082 @example
9083 % :: RCS/%,v
9084         $(CO) $(COFLAGS) $<
9085 @end example
9087 @noindent
9088 defines a rule that can make any file @file{@var{x}} whatsoever from a
9089 corresponding file @file{@var{x},v} in the subdirectory @file{RCS}.  Since
9090 the target is @samp{%}, this rule will apply to any file whatever, provided
9091 the appropriate prerequisite file exists.  The double colon makes the rule
9092 @dfn{terminal}, which means that its prerequisite may not be an intermediate
9093 file (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}).@refill
9095 @need 500
9096 This pattern rule has two targets:
9098 @example
9099 @group
9100 %.tab.c %.tab.h: %.y
9101         bison -d $<
9102 @end group
9103 @end example
9105 @noindent
9106 @c The following paragraph is rewritten to avoid overfull hboxes
9107 This tells @code{make} that the command @samp{bison -d @var{x}.y} will
9108 make both @file{@var{x}.tab.c} and @file{@var{x}.tab.h}.  If the file
9109 @file{foo} depends on the files @file{parse.tab.o} and @file{scan.o}
9110 and the file @file{scan.o} depends on the file @file{parse.tab.h},
9111 when @file{parse.y} is changed, the command @samp{bison -d parse.y}
9112 will be executed only once, and the prerequisites of both
9113 @file{parse.tab.o} and @file{scan.o} will be satisfied.  (Presumably
9114 the file @file{parse.tab.o} will be recompiled from @file{parse.tab.c}
9115 and the file @file{scan.o} from @file{scan.c}, while @file{foo} is
9116 linked from @file{parse.tab.o}, @file{scan.o}, and its other
9117 prerequisites, and it will execute happily ever after.)@refill
9119 @node Automatic Variables, Pattern Match, Pattern Examples, Pattern Rules
9120 @subsection Automatic Variables
9121 @cindex automatic variables
9122 @cindex variables, automatic
9123 @cindex variables, and implicit rule
9125 Suppose you are writing a pattern rule to compile a @samp{.c} file into a
9126 @samp{.o} file: how do you write the @samp{cc} command so that it operates
9127 on the right source file name?  You cannot write the name in the command,
9128 because the name is different each time the implicit rule is applied.
9130 What you do is use a special feature of @code{make}, the @dfn{automatic
9131 variables}.  These variables have values computed afresh for each rule that
9132 is executed, based on the target and prerequisites of the rule.  In this
9133 example, you would use @samp{$@@} for the object file name and @samp{$<}
9134 for the source file name.
9136 @cindex automatic variables in prerequisites
9137 @cindex prerequisites, and automatic variables
9138 It's very important that you recognize the limited scope in which
9139 automatic variable values are available: they only have values within
9140 the command script.  In particular, you cannot use them anywhere
9141 within the target list of a rule; they have no value there and will
9142 expand to the empty string.  Also, they cannot be accessed directly
9143 within the prerequisite list of a rule.  A common mistake is
9144 attempting to use @code{$@@} within the prerequisites list; this will
9145 not work.  However, there is a special feature of GNU @code{make},
9146 secondary expansion (@pxref{Secondary Expansion}), which will allow
9147 automatic variable values to be used in prerequisite lists.
9149 Here is a table of automatic variables:
9151 @table @code
9152 @vindex $@@
9153 @vindex @@ @r{(automatic variable)}
9154 @item $@@
9155 The file name of the target of the rule.  If the target is an archive
9156 member, then @samp{$@@} is the name of the archive file.  In a pattern
9157 rule that has multiple targets (@pxref{Pattern Intro, ,Introduction to
9158 Pattern Rules}), @samp{$@@} is the name of whichever target caused the
9159 rule's commands to be run.
9161 @vindex $%
9162 @vindex % @r{(automatic variable)}
9163 @item $%
9164 The target member name, when the target is an archive member.
9165 @xref{Archives}.  For example, if the target is @file{foo.a(bar.o)} then
9166 @samp{$%} is @file{bar.o} and @samp{$@@} is @file{foo.a}.  @samp{$%} is
9167 empty when the target is not an archive member.
9169 @vindex $<
9170 @vindex < @r{(automatic variable)}
9171 @item $<
9172 The name of the first prerequisite.  If the target got its commands from
9173 an implicit rule, this will be the first prerequisite added by the
9174 implicit rule (@pxref{Implicit Rules}).
9176 @vindex $?
9177 @vindex ? @r{(automatic variable)}
9178 @item $?
9179 The names of all the prerequisites that are newer than the target, with
9180 spaces between them.  For prerequisites which are archive members, only
9181 the member named is used (@pxref{Archives}).
9182 @cindex prerequisites, list of changed
9183 @cindex list of changed prerequisites
9185 @vindex $^
9186 @vindex ^ @r{(automatic variable)}
9187 @item $^
9188 The names of all the prerequisites, with spaces between them.  For
9189 prerequisites which are archive members, only the member named is used
9190 (@pxref{Archives}).  A target has only one prerequisite on each other file
9191 it depends on, no matter how many times each file is listed as a
9192 prerequisite.  So if you list a prerequisite more than once for a target,
9193 the value of @code{$^} contains just one copy of the name.  This list
9194 does @strong{not} contain any of the order-only prerequisites; for those
9195 see the @samp{$|} variable, below.
9196 @cindex prerequisites, list of all
9197 @cindex list of all prerequisites
9199 @vindex $+
9200 @vindex + @r{(automatic variable)}
9201 @item $+
9202 This is like @samp{$^}, but prerequisites listed more than once are
9203 duplicated in the order they were listed in the makefile.  This is
9204 primarily useful for use in linking commands where it is meaningful to
9205 repeat library file names in a particular order.
9207 @vindex $|
9208 @vindex | @r{(automatic variable)}
9209 @item $|
9210 The names of all the order-only prerequisites, with spaces between
9211 them.
9213 @vindex $*
9214 @vindex * @r{(automatic variable)}
9215 @item $*
9216 The stem with which an implicit rule matches (@pxref{Pattern Match, ,How
9217 Patterns Match}).  If the target is @file{dir/a.foo.b} and the target
9218 pattern is @file{a.%.b} then the stem is @file{dir/foo}.  The stem is
9219 useful for constructing names of related files.@refill
9220 @cindex stem, variable for
9222 In a static pattern rule, the stem is part of the file name that matched
9223 the @samp{%} in the target pattern.
9225 In an explicit rule, there is no stem; so @samp{$*} cannot be determined
9226 in that way.  Instead, if the target name ends with a recognized suffix
9227 (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}), @samp{$*} is set to
9228 the target name minus the suffix.  For example, if the target name is
9229 @samp{foo.c}, then @samp{$*} is set to @samp{foo}, since @samp{.c} is a
9230 suffix.  GNU @code{make} does this bizarre thing only for compatibility
9231 with other implementations of @code{make}.  You should generally avoid
9232 using @samp{$*} except in implicit rules or static pattern rules.@refill
9234 If the target name in an explicit rule does not end with a recognized
9235 suffix, @samp{$*} is set to the empty string for that rule.
9236 @end table
9238 @samp{$?} is useful even in explicit rules when you wish to operate on only
9239 the prerequisites that have changed.  For example, suppose that an archive
9240 named @file{lib} is supposed to contain copies of several object files.
9241 This rule copies just the changed object files into the archive:
9243 @example
9244 @group
9245 lib: foo.o bar.o lose.o win.o
9246         ar r lib $?
9247 @end group
9248 @end example
9250 Of the variables listed above, four have values that are single file
9251 names, and three have values that are lists of file names.  These seven
9252 have variants that get just the file's directory name or just the file
9253 name within the directory.  The variant variables' names are formed by
9254 appending @samp{D} or @samp{F}, respectively.  These variants are
9255 semi-obsolete in GNU @code{make} since the functions @code{dir} and
9256 @code{notdir} can be used to get a similar effect (@pxref{File Name
9257 Functions, , Functions for File Names}).  Note, however, that the
9258 @samp{D} variants all omit the trailing slash which always appears in
9259 the output of the @code{dir} function.  Here is a table of the variants:
9261 @table @samp
9262 @vindex $(@@D)
9263 @vindex @@D @r{(automatic variable)}
9264 @item $(@@D)
9265 The directory part of the file name of the target, with the trailing
9266 slash removed.  If the value of @samp{$@@} is @file{dir/foo.o} then
9267 @samp{$(@@D)} is @file{dir}.  This value is @file{.} if @samp{$@@} does
9268 not contain a slash.
9270 @vindex $(@@F)
9271 @vindex @@F @r{(automatic variable)}
9272 @item $(@@F)
9273 The file-within-directory part of the file name of the target.  If the
9274 value of @samp{$@@} is @file{dir/foo.o} then @samp{$(@@F)} is
9275 @file{foo.o}.  @samp{$(@@F)} is equivalent to @samp{$(notdir $@@)}.
9277 @vindex $(*D)
9278 @vindex *D @r{(automatic variable)}
9279 @item $(*D)
9280 @vindex $(*F)
9281 @vindex *F @r{(automatic variable)}
9282 @itemx $(*F)
9283 The directory part and the file-within-directory
9284 part of the stem; @file{dir} and @file{foo} in this example.
9286 @vindex $(%D)
9287 @vindex %D @r{(automatic variable)}
9288 @item $(%D)
9289 @vindex $(%F)
9290 @vindex %F @r{(automatic variable)}
9291 @itemx $(%F)
9292 The directory part and the file-within-directory part of the target
9293 archive member name.  This makes sense only for archive member targets
9294 of the form @file{@var{archive}(@var{member})} and is useful only when
9295 @var{member} may contain a directory name.  (@xref{Archive Members,
9296 ,Archive Members as Targets}.)
9298 @vindex $(<D)
9299 @vindex <D @r{(automatic variable)}
9300 @item $(<D)
9301 @vindex $(<F)
9302 @vindex <F @r{(automatic variable)}
9303 @itemx $(<F)
9304 The directory part and the file-within-directory
9305 part of the first prerequisite.
9307 @vindex $(^D)
9308 @vindex ^D @r{(automatic variable)}
9309 @item $(^D)
9310 @vindex $(^F)
9311 @vindex ^F @r{(automatic variable)}
9312 @itemx $(^F)
9313 Lists of the directory parts and the file-within-directory
9314 parts of all prerequisites.
9316 @vindex $(+D)
9317 @vindex +D @r{(automatic variable)}
9318 @item $(+D)
9319 @vindex $(+F)
9320 @vindex +F @r{(automatic variable)}
9321 @itemx $(+F)
9322 Lists of the directory parts and the file-within-directory
9323 parts of all prerequisites, including multiple instances of duplicated
9324 prerequisites.
9326 @vindex $(?D)
9327 @vindex ?D @r{(automatic variable)}
9328 @item $(?D)
9329 @vindex $(?F)
9330 @vindex ?F @r{(automatic variable)}
9331 @itemx $(?F)
9332 Lists of the directory parts and the file-within-directory parts of
9333 all prerequisites that are newer than the target.
9334 @end table
9336 Note that we use a special stylistic convention when we talk about these
9337 automatic variables; we write ``the value of @samp{$<}'', rather than
9338 @w{``the variable @code{<}''} as we would write for ordinary variables
9339 such as @code{objects} and @code{CFLAGS}.  We think this convention
9340 looks more natural in this special case.  Please do not assume it has a
9341 deep significance; @samp{$<} refers to the variable named @code{<} just
9342 as @samp{$(CFLAGS)} refers to the variable named @code{CFLAGS}.
9343 You could just as well use @samp{$(<)} in place of @samp{$<}.
9345 @node Pattern Match, Match-Anything Rules, Automatic Variables, Pattern Rules
9346 @subsection How Patterns Match
9348 @cindex stem
9349 A target pattern is composed of a @samp{%} between a prefix and a suffix,
9350 either or both of which may be empty.  The pattern matches a file name only
9351 if the file name starts with the prefix and ends with the suffix, without
9352 overlap.  The text between the prefix and the suffix is called the
9353 @dfn{stem}.  Thus, when the pattern @samp{%.o} matches the file name
9354 @file{test.o}, the stem is @samp{test}.  The pattern rule prerequisites are
9355 turned into actual file names by substituting the stem for the character
9356 @samp{%}.  Thus, if in the same example one of the prerequisites is written
9357 as @samp{%.c}, it expands to @samp{test.c}.@refill
9359 When the target pattern does not contain a slash (and it usually does
9360 not), directory names in the file names are removed from the file name
9361 before it is compared with the target prefix and suffix.  After the
9362 comparison of the file name to the target pattern, the directory
9363 names, along with the slash that ends them, are added on to the
9364 prerequisite file names generated from the pattern rule's prerequisite
9365 patterns and the file name.  The directories are ignored only for the
9366 purpose of finding an implicit rule to use, not in the application of
9367 that rule.  Thus, @samp{e%t} matches the file name @file{src/eat},
9368 with @samp{src/a} as the stem.  When prerequisites are turned into file
9369 names, the directories from the stem are added at the front, while the
9370 rest of the stem is substituted for the @samp{%}.  The stem
9371 @samp{src/a} with a prerequisite pattern @samp{c%r} gives the file name
9372 @file{src/car}.@refill
9374 @node Match-Anything Rules, Canceling Rules, Pattern Match, Pattern Rules
9375 @subsection Match-Anything Pattern Rules
9377 @cindex match-anything rule
9378 @cindex terminal rule
9379 When a pattern rule's target is just @samp{%}, it matches any file name
9380 whatever.  We call these rules @dfn{match-anything} rules.  They are very
9381 useful, but it can take a lot of time for @code{make} to think about them,
9382 because it must consider every such rule for each file name listed either
9383 as a target or as a prerequisite.
9385 Suppose the makefile mentions @file{foo.c}.  For this target, @code{make}
9386 would have to consider making it by linking an object file @file{foo.c.o},
9387 or by C compilation-and-linking in one step from @file{foo.c.c}, or by
9388 Pascal compilation-and-linking from @file{foo.c.p}, and many other
9389 possibilities.
9391 We know these possibilities are ridiculous since @file{foo.c} is a C source
9392 file, not an executable.  If @code{make} did consider these possibilities,
9393 it would ultimately reject them, because files such as @file{foo.c.o} and
9394 @file{foo.c.p} would not exist.  But these possibilities are so
9395 numerous that @code{make} would run very slowly if it had to consider
9396 them.@refill
9398 To gain speed, we have put various constraints on the way @code{make}
9399 considers match-anything rules.  There are two different constraints that
9400 can be applied, and each time you define a match-anything rule you must
9401 choose one or the other for that rule.
9403 One choice is to mark the match-anything rule as @dfn{terminal} by defining
9404 it with a double colon.  When a rule is terminal, it does not apply unless
9405 its prerequisites actually exist.  Prerequisites that could be made with
9406 other implicit rules are not good enough.  In other words, no further
9407 chaining is allowed beyond a terminal rule.
9409 For example, the built-in implicit rules for extracting sources from RCS
9410 and SCCS files are terminal; as a result, if the file @file{foo.c,v} does
9411 not exist, @code{make} will not even consider trying to make it as an
9412 intermediate file from @file{foo.c,v.o} or from @file{RCS/SCCS/s.foo.c,v}.
9413 RCS and SCCS files are generally ultimate source files, which should not be
9414 remade from any other files; therefore, @code{make} can save time by not
9415 looking for ways to remake them.@refill
9417 If you do not mark the match-anything rule as terminal, then it is
9418 nonterminal.  A nonterminal match-anything rule cannot apply to a file name
9419 that indicates a specific type of data.  A file name indicates a specific
9420 type of data if some non-match-anything implicit rule target matches it.
9422 For example, the file name @file{foo.c} matches the target for the pattern
9423 rule @samp{%.c : %.y} (the rule to run Yacc).  Regardless of whether this
9424 rule is actually applicable (which happens only if there is a file
9425 @file{foo.y}), the fact that its target matches is enough to prevent
9426 consideration of any nonterminal match-anything rules for the file
9427 @file{foo.c}.  Thus, @code{make} will not even consider trying to make
9428 @file{foo.c} as an executable file from @file{foo.c.o}, @file{foo.c.c},
9429 @file{foo.c.p}, etc.@refill
9431 The motivation for this constraint is that nonterminal match-anything
9432 rules are used for making files containing specific types of data (such as
9433 executable files) and a file name with a recognized suffix indicates some
9434 other specific type of data (such as a C source file).
9436 Special built-in dummy pattern rules are provided solely to recognize
9437 certain file names so that nonterminal match-anything rules will not be
9438 considered.  These dummy rules have no prerequisites and no commands, and
9439 they are ignored for all other purposes.  For example, the built-in
9440 implicit rule
9442 @example
9443 %.p :
9444 @end example
9446 @noindent
9447 exists to make sure that Pascal source files such as @file{foo.p} match a
9448 specific target pattern and thereby prevent time from being wasted looking
9449 for @file{foo.p.o} or @file{foo.p.c}.
9451 Dummy pattern rules such as the one for @samp{%.p} are made for every
9452 suffix listed as valid for use in suffix rules (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}).
9454 @node Canceling Rules,  , Match-Anything Rules, Pattern Rules
9455 @subsection Canceling Implicit Rules
9457 You can override a built-in implicit rule (or one you have defined
9458 yourself) by defining a new pattern rule with the same target and
9459 prerequisites, but different commands.  When the new rule is defined, the
9460 built-in one is replaced.  The new rule's position in the sequence of
9461 implicit rules is determined by where you write the new rule.
9463 You can cancel a built-in implicit rule by defining a pattern rule with the
9464 same target and prerequisites, but no commands.  For example, the following
9465 would cancel the rule that runs the assembler:
9467 @example
9468 %.o : %.s
9469 @end example
9471 @node Last Resort, Suffix Rules, Pattern Rules, Implicit Rules
9472 @section Defining Last-Resort Default Rules
9473 @cindex last-resort default rules
9474 @cindex default rules, last-resort
9476 You can define a last-resort implicit rule by writing a terminal
9477 match-anything pattern rule with no prerequisites (@pxref{Match-Anything
9478 Rules}).  This is just like any other pattern rule; the only thing
9479 special about it is that it will match any target.  So such a rule's
9480 commands are used for all targets and prerequisites that have no commands
9481 of their own and for which no other implicit rule applies.
9483 For example, when testing a makefile, you might not care if the source
9484 files contain real data, only that they exist.  Then you might do this:
9486 @example
9488         touch $@@
9489 @end example
9491 @noindent
9492 to cause all the source files needed (as prerequisites) to be created
9493 automatically.
9495 @findex .DEFAULT
9496 You can instead define commands to be used for targets for which there
9497 are no rules at all, even ones which don't specify commands.  You do
9498 this by writing a rule for the target @code{.DEFAULT}.  Such a rule's
9499 commands are used for all prerequisites which do not appear as targets in
9500 any explicit rule, and for which no implicit rule applies.  Naturally,
9501 there is no @code{.DEFAULT} rule unless you write one.
9503 If you use @code{.DEFAULT} with no commands or prerequisites:
9505 @example
9506 .DEFAULT:
9507 @end example
9509 @noindent
9510 the commands previously stored for @code{.DEFAULT} are cleared.
9511 Then @code{make} acts as if you had never defined @code{.DEFAULT} at all.
9513 If you do not want a target to get the commands from a match-anything
9514 pattern rule or @code{.DEFAULT}, but you also do not want any commands
9515 to be run for the target, you can give it empty commands (@pxref{Empty
9516 Commands, ,Defining Empty Commands}).@refill
9518 You can use a last-resort rule to override part of another makefile.
9519 @xref{Overriding Makefiles, , Overriding Part of Another Makefile}.
9521 @node Suffix Rules, Implicit Rule Search, Last Resort, Implicit Rules
9522 @section Old-Fashioned Suffix Rules
9523 @cindex old-fashioned suffix rules
9524 @cindex suffix rule
9526 @dfn{Suffix rules} are the old-fashioned way of defining implicit rules for
9527 @code{make}.  Suffix rules are obsolete because pattern rules are more
9528 general and clearer.  They are supported in GNU @code{make} for
9529 compatibility with old makefiles.  They come in two kinds:
9530 @dfn{double-suffix} and @dfn{single-suffix}.@refill
9532 A double-suffix rule is defined by a pair of suffixes: the target suffix
9533 and the source suffix.  It matches any file whose name ends with the
9534 target suffix.  The corresponding implicit prerequisite is made by
9535 replacing the target suffix with the source suffix in the file name.  A
9536 two-suffix rule whose target and source suffixes are @samp{.o} and
9537 @samp{.c} is equivalent to the pattern rule @samp{%.o : %.c}.
9539 A single-suffix rule is defined by a single suffix, which is the source
9540 suffix.  It matches any file name, and the corresponding implicit
9541 prerequisite name is made by appending the source suffix.  A single-suffix
9542 rule whose source suffix is @samp{.c} is equivalent to the pattern rule
9543 @samp{% : %.c}.
9545 Suffix rule definitions are recognized by comparing each rule's target
9546 against a defined list of known suffixes.  When @code{make} sees a rule
9547 whose target is a known suffix, this rule is considered a single-suffix
9548 rule.  When @code{make} sees a rule whose target is two known suffixes
9549 concatenated, this rule is taken as a double-suffix rule.
9551 For example, @samp{.c} and @samp{.o} are both on the default list of
9552 known suffixes.  Therefore, if you define a rule whose target is
9553 @samp{.c.o}, @code{make} takes it to be a double-suffix rule with source
9554 suffix @samp{.c} and target suffix @samp{.o}.  Here is the old-fashioned
9555 way to define the rule for compiling a C source file:@refill
9557 @example
9558 .c.o:
9559         $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
9560 @end example
9562 Suffix rules cannot have any prerequisites of their own.  If they have any,
9563 they are treated as normal files with funny names, not as suffix rules.
9564 Thus, the rule:
9566 @example
9567 .c.o: foo.h
9568         $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
9569 @end example
9571 @noindent
9572 tells how to make the file @file{.c.o} from the prerequisite file
9573 @file{foo.h}, and is not at all like the pattern rule:
9575 @example
9576 %.o: %.c foo.h
9577         $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
9578 @end example
9580 @noindent
9581 which tells how to make @samp{.o} files from @samp{.c} files, and makes all
9582 @samp{.o} files using this pattern rule also depend on @file{foo.h}.
9584 Suffix rules with no commands are also meaningless.  They do not remove
9585 previous rules as do pattern rules with no commands (@pxref{Canceling
9586 Rules, , Canceling Implicit Rules}).  They simply enter the suffix or pair of suffixes concatenated as
9587 a target in the data base.@refill
9589 @findex .SUFFIXES
9590 The known suffixes are simply the names of the prerequisites of the special
9591 target @code{.SUFFIXES}.  You can add your own suffixes by writing a rule
9592 for @code{.SUFFIXES} that adds more prerequisites, as in:
9594 @example
9595 .SUFFIXES: .hack .win
9596 @end example
9598 @noindent
9599 which adds @samp{.hack} and @samp{.win} to the end of the list of suffixes.
9601 If you wish to eliminate the default known suffixes instead of just adding
9602 to them, write a rule for @code{.SUFFIXES} with no prerequisites.  By
9603 special dispensation, this eliminates all existing prerequisites of
9604 @code{.SUFFIXES}.  You can then write another rule to add the suffixes you
9605 want.  For example,
9607 @example
9608 @group
9609 .SUFFIXES:            # @r{Delete the default suffixes}
9610 .SUFFIXES: .c .o .h   # @r{Define our suffix list}
9611 @end group
9612 @end example
9614 The @samp{-r} or @samp{--no-builtin-rules} flag causes the default
9615 list of suffixes to be empty.
9617 @vindex SUFFIXES
9618 The variable @code{SUFFIXES} is defined to the default list of suffixes
9619 before @code{make} reads any makefiles.  You can change the list of suffixes
9620 with a rule for the special target @code{.SUFFIXES}, but that does not alter
9621 this variable.
9623 @node Implicit Rule Search,  , Suffix Rules, Implicit Rules
9624 @section Implicit Rule Search Algorithm
9625 @cindex implicit rule, search algorithm
9626 @cindex search algorithm, implicit rule
9628 Here is the procedure @code{make} uses for searching for an implicit rule
9629 for a target @var{t}.  This procedure is followed for each double-colon
9630 rule with no commands, for each target of ordinary rules none of which have
9631 commands, and for each prerequisite that is not the target of any rule.  It
9632 is also followed recursively for prerequisites that come from implicit
9633 rules, in the search for a chain of rules.
9635 Suffix rules are not mentioned in this algorithm because suffix rules are
9636 converted to equivalent pattern rules once the makefiles have been read in.
9638 For an archive member target of the form
9639 @samp{@var{archive}(@var{member})}, the following algorithm is run
9640 twice, first using the entire target name @var{t}, and second using
9641 @samp{(@var{member})} as the target @var{t} if the first run found no
9642 rule.@refill
9644 @enumerate
9645 @item
9646 Split @var{t} into a directory part, called @var{d}, and the rest,
9647 called @var{n}.  For example, if @var{t} is @samp{src/foo.o}, then
9648 @var{d} is @samp{src/} and @var{n} is @samp{foo.o}.@refill
9650 @item
9651 Make a list of all the pattern rules one of whose targets matches
9652 @var{t} or @var{n}.  If the target pattern contains a slash, it is
9653 matched against @var{t}; otherwise, against @var{n}.
9655 @item
9656 If any rule in that list is @emph{not} a match-anything rule, then
9657 remove all nonterminal match-anything rules from the list.
9659 @item
9660 Remove from the list all rules with no commands.
9662 @item
9663 For each pattern rule in the list:
9665 @enumerate a
9666 @item
9667 Find the stem @var{s}, which is the nonempty part of @var{t} or @var{n}
9668 matched by the @samp{%} in the target pattern.@refill
9670 @item
9671 Compute the prerequisite names by substituting @var{s} for @samp{%}; if
9672 the target pattern does not contain a slash, append @var{d} to
9673 the front of each prerequisite name.@refill
9675 @item
9676 Test whether all the prerequisites exist or ought to exist.  (If a
9677 file name is mentioned in the makefile as a target or as an explicit
9678 prerequisite, then we say it ought to exist.)
9680 If all prerequisites exist or ought to exist, or there are no prerequisites,
9681 then this rule applies.
9682 @end enumerate
9684 @item
9685 If no pattern rule has been found so far, try harder.
9686 For each pattern rule in the list:
9688 @enumerate a
9689 @item
9690 If the rule is terminal, ignore it and go on to the next rule.
9692 @item
9693 Compute the prerequisite names as before.
9695 @item
9696 Test whether all the prerequisites exist or ought to exist.
9698 @item
9699 For each prerequisite that does not exist, follow this algorithm
9700 recursively to see if the prerequisite can be made by an implicit
9701 rule.
9703 @item
9704 If all prerequisites exist, ought to exist, or can be
9705 made by implicit rules, then this rule applies.
9706 @end enumerate
9708 @item
9709 If no implicit rule applies, the rule for @code{.DEFAULT}, if any,
9710 applies.  In that case, give @var{t} the same commands that
9711 @code{.DEFAULT} has.  Otherwise, there are no commands for @var{t}.
9712 @end enumerate
9714 Once a rule that applies has been found, for each target pattern of the
9715 rule other than the one that matched @var{t} or @var{n}, the @samp{%} in
9716 the pattern is replaced with @var{s} and the resultant file name is stored
9717 until the commands to remake the target file @var{t} are executed.  After
9718 these commands are executed, each of these stored file names are entered
9719 into the data base and marked as having been updated and having the same
9720 update status as the file @var{t}.
9722 When the commands of a pattern rule are executed for @var{t}, the automatic
9723 variables are set corresponding to the target and prerequisites.
9724 @xref{Automatic Variables}.
9726 @node Archives, Features, Implicit Rules, Top
9727 @chapter Using @code{make} to Update Archive Files
9728 @cindex archive
9730 @dfn{Archive files} are files containing named subfiles called
9731 @dfn{members}; they are maintained with the program @code{ar} and their
9732 main use is as subroutine libraries for linking.
9734 @menu
9735 * Archive Members::             Archive members as targets.
9736 * Archive Update::              The implicit rule for archive member targets.
9737 * Archive Pitfalls::            Dangers to watch out for when using archives.
9738 * Archive Suffix Rules::        You can write a special kind of suffix rule
9739                                   for updating archives.
9740 @end menu
9742 @node Archive Members, Archive Update, Archives, Archives
9743 @section Archive Members as Targets
9744 @cindex archive member targets
9746 An individual member of an archive file can be used as a target or
9747 prerequisite in @code{make}.  You specify the member named @var{member} in
9748 archive file @var{archive} as follows:
9750 @example
9751 @var{archive}(@var{member})
9752 @end example
9754 @noindent
9755 This construct is available only in targets and prerequisites, not in
9756 commands!  Most programs that you might use in commands do not support this
9757 syntax and cannot act directly on archive members.  Only @code{ar} and
9758 other programs specifically designed to operate on archives can do so.
9759 Therefore, valid commands to update an archive member target probably must
9760 use @code{ar}.  For example, this rule says to create a member
9761 @file{hack.o} in archive @file{foolib} by copying the file @file{hack.o}:
9763 @example
9764 foolib(hack.o) : hack.o
9765         ar cr foolib hack.o
9766 @end example
9768 In fact, nearly all archive member targets are updated in just this way
9769 and there is an implicit rule to do it for you.  @strong{Please note:} The
9770 @samp{c} flag to @code{ar} is required if the archive file does not
9771 already exist.
9773 To specify several members in the same archive, you can write all the
9774 member names together between the parentheses.  For example:
9776 @example
9777 foolib(hack.o kludge.o)
9778 @end example
9780 @noindent
9781 is equivalent to:
9783 @example
9784 foolib(hack.o) foolib(kludge.o)
9785 @end example
9787 @cindex wildcard, in archive member
9788 You can also use shell-style wildcards in an archive member reference.
9789 @xref{Wildcards, ,Using Wildcard Characters in File Names}.  For
9790 example, @w{@samp{foolib(*.o)}} expands to all existing members of the
9791 @file{foolib} archive whose names end in @samp{.o}; perhaps
9792 @samp{@w{foolib(hack.o)} @w{foolib(kludge.o)}}.
9794 @node Archive Update, Archive Pitfalls, Archive Members, Archives
9795 @section Implicit Rule for Archive Member Targets
9797 Recall that a target that looks like @file{@var{a}(@var{m})} stands for the
9798 member named @var{m} in the archive file @var{a}.
9800 When @code{make} looks for an implicit rule for such a target, as a special
9801 feature it considers implicit rules that match @file{(@var{m})}, as well as
9802 those that match the actual target @file{@var{a}(@var{m})}.
9804 This causes one special rule whose target is @file{(%)} to match.  This
9805 rule updates the target @file{@var{a}(@var{m})} by copying the file @var{m}
9806 into the archive.  For example, it will update the archive member target
9807 @file{foo.a(bar.o)} by copying the @emph{file} @file{bar.o} into the
9808 archive @file{foo.a} as a @emph{member} named @file{bar.o}.
9810 When this rule is chained with others, the result is very powerful.
9811 Thus, @samp{make "foo.a(bar.o)"} (the quotes are needed to protect the
9812 @samp{(} and @samp{)} from being interpreted specially by the shell) in
9813 the presence of a file @file{bar.c} is enough to cause the following
9814 commands to be run, even without a makefile:
9816 @example
9817 cc -c bar.c -o bar.o
9818 ar r foo.a bar.o
9819 rm -f bar.o
9820 @end example
9822 @noindent
9823 Here @code{make} has envisioned the file @file{bar.o} as an intermediate
9824 file.  @xref{Chained Rules, ,Chains of Implicit Rules}.
9826 Implicit rules such as this one are written using the automatic variable
9827 @samp{$%}.  @xref{Automatic Variables}.
9829 An archive member name in an archive cannot contain a directory name, but
9830 it may be useful in a makefile to pretend that it does.  If you write an
9831 archive member target @file{foo.a(dir/file.o)}, @code{make} will perform
9832 automatic updating with this command:
9834 @example
9835 ar r foo.a dir/file.o
9836 @end example
9838 @noindent
9839 which has the effect of copying the file @file{dir/file.o} into a member
9840 named @file{file.o}.  In connection with such usage, the automatic variables
9841 @code{%D} and @code{%F} may be useful.
9843 @menu
9844 * Archive Symbols::             How to update archive symbol directories.
9845 @end menu
9847 @node Archive Symbols,  , Archive Update, Archive Update
9848 @subsection Updating Archive Symbol Directories
9849 @cindex @code{__.SYMDEF}
9850 @cindex updating archive symbol directories
9851 @cindex archive symbol directory updating
9852 @cindex symbol directories, updating archive
9853 @cindex directories, updating archive symbol
9855 An archive file that is used as a library usually contains a special member
9856 named @file{__.SYMDEF} that contains a directory of the external symbol
9857 names defined by all the other members.  After you update any other
9858 members, you need to update @file{__.SYMDEF} so that it will summarize the
9859 other members properly.  This is done by running the @code{ranlib} program:
9861 @example
9862 ranlib @var{archivefile}
9863 @end example
9865 Normally you would put this command in the rule for the archive file,
9866 and make all the members of the archive file prerequisites of that rule.
9867 For example,
9869 @example
9870 libfoo.a: libfoo.a(x.o) libfoo.a(y.o) @dots{}
9871         ranlib libfoo.a
9872 @end example
9874 @noindent
9875 The effect of this is to update archive members @file{x.o}, @file{y.o},
9876 etc., and then update the symbol directory member @file{__.SYMDEF} by
9877 running @code{ranlib}.  The rules for updating the members are not shown
9878 here; most likely you can omit them and use the implicit rule which copies
9879 files into the archive, as described in the preceding section.
9881 This is not necessary when using the GNU @code{ar} program, which
9882 updates the @file{__.SYMDEF} member automatically.
9884 @node Archive Pitfalls, Archive Suffix Rules, Archive Update, Archives
9885 @section Dangers When Using Archives
9886 @cindex archive, and parallel execution
9887 @cindex parallel execution, and archive update
9888 @cindex archive, and @code{-j}
9889 @cindex @code{-j}, and archive update
9891 It is important to be careful when using parallel execution (the
9892 @code{-j} switch; @pxref{Parallel, ,Parallel Execution}) and archives.
9893 If multiple @code{ar} commands run at the same time on the same archive
9894 file, they will not know about each other and can corrupt the file.
9896 Possibly a future version of @code{make} will provide a mechanism to
9897 circumvent this problem by serializing all commands that operate on the
9898 same archive file.  But for the time being, you must either write your
9899 makefiles to avoid this problem in some other way, or not use @code{-j}.
9901 @node Archive Suffix Rules,  , Archive Pitfalls, Archives
9902 @section Suffix Rules for Archive Files
9903 @cindex suffix rule, for archive
9904 @cindex archive, suffix rule for
9905 @cindex library archive, suffix rule for
9906 @cindex @code{.a} (archives)
9908 You can write a special kind of suffix rule for dealing with archive
9909 files.  @xref{Suffix Rules}, for a full explanation of suffix rules.
9910 Archive suffix rules are obsolete in GNU @code{make}, because pattern
9911 rules for archives are a more general mechanism (@pxref{Archive
9912 Update}).  But they are retained for compatibility with other
9913 @code{make}s.
9915 To write a suffix rule for archives, you simply write a suffix rule
9916 using the target suffix @samp{.a} (the usual suffix for archive files).
9917 For example, here is the old-fashioned suffix rule to update a library
9918 archive from C source files:
9920 @example
9921 @group
9922 .c.a:
9923         $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o
9924         $(AR) r $@@ $*.o
9925         $(RM) $*.o
9926 @end group
9927 @end example
9929 @noindent
9930 This works just as if you had written the pattern rule:
9932 @example
9933 @group
9934 (%.o): %.c
9935         $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o
9936         $(AR) r $@@ $*.o
9937         $(RM) $*.o
9938 @end group
9939 @end example
9941 In fact, this is just what @code{make} does when it sees a suffix rule
9942 with @samp{.a} as the target suffix.  Any double-suffix rule
9943 @w{@samp{.@var{x}.a}} is converted to a pattern rule with the target
9944 pattern @samp{(%.o)} and a prerequisite pattern of @samp{%.@var{x}}.
9946 Since you might want to use @samp{.a} as the suffix for some other kind
9947 of file, @code{make} also converts archive suffix rules to pattern rules
9948 in the normal way (@pxref{Suffix Rules}).  Thus a double-suffix rule
9949 @w{@samp{.@var{x}.a}} produces two pattern rules: @samp{@w{(%.o):}
9950 @w{%.@var{x}}} and @samp{@w{%.a}: @w{%.@var{x}}}.@refill
9952 @node Features, Missing, Archives, Top
9953 @chapter Features of GNU @code{make}
9954 @cindex features of GNU @code{make}
9955 @cindex portability
9956 @cindex compatibility
9958 Here is a summary of the features of GNU @code{make}, for comparison
9959 with and credit to other versions of @code{make}.  We consider the
9960 features of @code{make} in 4.2 BSD systems as a baseline.  If you are
9961 concerned with writing portable makefiles, you should not use the
9962 features of @code{make} listed here, nor the ones in @ref{Missing}.
9964 Many features come from the version of @code{make} in System V.
9966 @itemize @bullet
9967 @item
9968 The @code{VPATH} variable and its special meaning.
9969 @xref{Directory Search, , Searching Directories for Prerequisites}.
9970 This feature exists in System V @code{make}, but is undocumented.
9971 It is documented in 4.3 BSD @code{make} (which says it mimics System V's
9972 @code{VPATH} feature).@refill
9974 @item
9975 Included makefiles.  @xref{Include, ,Including Other Makefiles}.
9976 Allowing multiple files to be included with a single directive is a GNU
9977 extension.
9979 @item
9980 Variables are read from and communicated via the environment.
9981 @xref{Environment, ,Variables from the Environment}.
9983 @item
9984 Options passed through the variable @code{MAKEFLAGS} to recursive
9985 invocations of @code{make}.
9986 @xref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
9988 @item
9989 The automatic variable @code{$%} is set to the member name
9990 in an archive reference.  @xref{Automatic Variables}.
9992 @item
9993 The automatic variables @code{$@@}, @code{$*}, @code{$<}, @code{$%},
9994 and @code{$?} have corresponding forms like @code{$(@@F)} and
9995 @code{$(@@D)}.  We have generalized this to @code{$^} as an obvious
9996 extension.  @xref{Automatic Variables}.@refill
9998 @item
9999 Substitution variable references.
10000 @xref{Reference, ,Basics of Variable References}.
10002 @item
10003 The command-line options @samp{-b} and @samp{-m}, accepted and
10004 ignored.  In System V @code{make}, these options actually do something.
10006 @item
10007 Execution of recursive commands to run @code{make} via the variable
10008 @code{MAKE} even if @samp{-n}, @samp{-q} or @samp{-t} is specified.
10009 @xref{Recursion, ,Recursive Use of @code{make}}.
10011 @item
10012 Support for suffix @samp{.a} in suffix rules.  @xref{Archive Suffix
10013 Rules}.  This feature is obsolete in GNU @code{make}, because the
10014 general feature of rule chaining (@pxref{Chained Rules, ,Chains of
10015 Implicit Rules}) allows one pattern rule for installing members in an
10016 archive (@pxref{Archive Update}) to be sufficient.
10018 @item
10019 The arrangement of lines and backslash-newline combinations in
10020 commands is retained when the commands are printed, so they appear as
10021 they do in the makefile, except for the stripping of initial
10022 whitespace.
10023 @end itemize
10025 The following features were inspired by various other versions of
10026 @code{make}.  In some cases it is unclear exactly which versions inspired
10027 which others.
10029 @itemize @bullet
10030 @item
10031 Pattern rules using @samp{%}.
10032 This has been implemented in several versions of @code{make}.
10033 We're not sure who invented it first, but it's been spread around a bit.
10034 @xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.@refill
10036 @item
10037 Rule chaining and implicit intermediate files.
10038 This was implemented by Stu Feldman in his version of @code{make}
10039 for AT&T Eighth Edition Research Unix, and later by Andrew Hume of
10040 AT&T Bell Labs in his @code{mk} program (where he terms it
10041 ``transitive closure'').  We do not really know if
10042 we got this from either of them or thought it up ourselves at the
10043 same time.  @xref{Chained Rules, ,Chains of Implicit Rules}.
10045 @item
10046 The automatic variable @code{$^} containing a list of all prerequisites
10047 of the current target.  We did not invent this, but we have no idea who
10048 did.  @xref{Automatic Variables}.  The automatic variable
10049 @code{$+} is a simple extension of @code{$^}.
10051 @item
10052 The ``what if'' flag (@samp{-W} in GNU @code{make}) was (as far as we know)
10053 invented by Andrew Hume in @code{mk}.
10054 @xref{Instead of Execution, ,Instead of Executing the Commands}.
10056 @item
10057 The concept of doing several things at once (parallelism) exists in
10058 many incarnations of @code{make} and similar programs, though not in the
10059 System V or BSD implementations.  @xref{Execution, ,Command Execution}.
10061 @item
10062 Modified variable references using pattern substitution come from
10063 SunOS 4.  @xref{Reference, ,Basics of Variable References}.
10064 This functionality was provided in GNU @code{make} by the
10065 @code{patsubst} function before the alternate syntax was implemented
10066 for compatibility with SunOS 4.  It is not altogether clear who
10067 inspired whom, since GNU @code{make} had @code{patsubst} before SunOS
10068 4 was released.@refill
10070 @item
10071 The special significance of @samp{+} characters preceding command lines
10072 (@pxref{Instead of Execution, ,Instead of Executing the Commands}) is
10073 mandated by
10074 @cite{IEEE Standard 1003.2-1992} (POSIX.2).
10076 @item
10077 The @samp{+=} syntax to append to the value of a variable comes from SunOS
10078 4 @code{make}.  @xref{Appending, , Appending More Text to Variables}.
10080 @item
10081 The syntax @w{@samp{@var{archive}(@var{mem1} @var{mem2}@dots{})}} to list
10082 multiple members in a single archive file comes from SunOS 4 @code{make}.
10083 @xref{Archive Members}.
10085 @item
10086 The @code{-include} directive to include makefiles with no error for a
10087 nonexistent file comes from SunOS 4 @code{make}.  (But note that SunOS 4
10088 @code{make} does not allow multiple makefiles to be specified in one
10089 @code{-include} directive.)  The same feature appears with the name
10090 @code{sinclude} in SGI @code{make} and perhaps others.
10091 @end itemize
10093 The remaining features are inventions new in GNU @code{make}:
10095 @itemize @bullet
10096 @item
10097 Use the @samp{-v} or @samp{--version} option to print version and
10098 copyright information.
10100 @item
10101 Use the @samp{-h} or @samp{--help} option to summarize the options to
10102 @code{make}.
10104 @item
10105 Simply-expanded variables.  @xref{Flavors, ,The Two Flavors of Variables}.
10107 @item
10108 Pass command-line variable assignments automatically through the
10109 variable @code{MAKE} to recursive @code{make} invocations.
10110 @xref{Recursion, ,Recursive Use of @code{make}}.
10112 @item
10113 Use the @samp{-C} or @samp{--directory} command option to change
10114 directory.  @xref{Options Summary, ,Summary of Options}.
10116 @item
10117 Make verbatim variable definitions with @code{define}.
10118 @xref{Defining, ,Defining Variables Verbatim}.
10120 @item
10121 Declare phony targets with the special target @code{.PHONY}.
10123 Andrew Hume of AT&T Bell Labs implemented a similar feature with a
10124 different syntax in his @code{mk} program.  This seems to be a case of
10125 parallel discovery.  @xref{Phony Targets, ,Phony Targets}.
10127 @item
10128 Manipulate text by calling functions.
10129 @xref{Functions, ,Functions for Transforming Text}.
10131 @item
10132 Use the @samp{-o} or @samp{--old-file}
10133 option to pretend a file's modification-time is old.
10134 @xref{Avoiding Compilation, ,Avoiding Recompilation of Some Files}.
10136 @item
10137 Conditional execution.
10139 This feature has been implemented numerous times in various versions
10140 of @code{make}; it seems a natural extension derived from the features
10141 of the C preprocessor and similar macro languages and is not a
10142 revolutionary concept.  @xref{Conditionals, ,Conditional Parts of Makefiles}.
10144 @item
10145 Specify a search path for included makefiles.
10146 @xref{Include, ,Including Other Makefiles}.
10148 @item
10149 Specify extra makefiles to read with an environment variable.
10150 @xref{MAKEFILES Variable, ,The Variable @code{MAKEFILES}}.
10152 @item
10153 Strip leading sequences of @samp{./} from file names, so that
10154 @file{./@var{file}} and @file{@var{file}} are considered to be the
10155 same file.@refill
10157 @item
10158 Use a special search method for library prerequisites written in the
10159 form @samp{-l@var{name}}.
10160 @xref{Libraries/Search, ,Directory Search for Link Libraries}.
10162 @item
10163 Allow suffixes for suffix rules
10164 (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}) to contain any
10165 characters.  In other versions of @code{make}, they must begin with
10166 @samp{.} and not contain any @samp{/} characters.
10168 @item
10169 Keep track of the current level of @code{make} recursion using the
10170 variable @code{MAKELEVEL}.  @xref{Recursion, ,Recursive Use of @code{make}}.
10172 @item
10173 Provide any goals given on the command line in the variable
10174 @code{MAKECMDGOALS}.  @xref{Goals, ,Arguments to Specify the Goals}.
10176 @item
10177 Specify static pattern rules.  @xref{Static Pattern, ,Static Pattern Rules}.
10179 @item
10180 Provide selective @code{vpath} search.
10181 @xref{Directory Search, ,Searching Directories for Prerequisites}.
10183 @item
10184 Provide computed variable references.
10185 @xref{Reference, ,Basics of Variable References}.
10187 @item
10188 Update makefiles.  @xref{Remaking Makefiles, ,How Makefiles Are Remade}.
10189 System V @code{make} has a very, very limited form of this
10190 functionality in that it will check out SCCS files for makefiles.
10192 @item
10193 Various new built-in implicit rules.
10194 @xref{Catalogue of Rules, ,Catalogue of Implicit Rules}.
10196 @item
10197 The built-in variable @samp{MAKE_VERSION} gives the version number of
10198 @code{make}.
10199 @vindex MAKE_VERSION
10200 @end itemize
10202 @node Missing, Makefile Conventions, Features, Top
10203 @chapter Incompatibilities and Missing Features
10204 @cindex incompatibilities
10205 @cindex missing features
10206 @cindex features, missing
10208 The @code{make} programs in various other systems support a few features
10209 that are not implemented in GNU @code{make}.  The POSIX.2 standard
10210 (@cite{IEEE Standard 1003.2-1992}) which specifies @code{make} does not
10211 require any of these features.@refill
10213 @itemize @bullet
10214 @item
10215 A target of the form @samp{@var{file}((@var{entry}))} stands for a member
10216 of archive file @var{file}.  The member is chosen, not by name, but by
10217 being an object file which defines the linker symbol @var{entry}.@refill
10219 This feature was not put into GNU @code{make} because of the
10220 nonmodularity of putting knowledge into @code{make} of the internal
10221 format of archive file symbol tables.
10222 @xref{Archive Symbols, ,Updating Archive Symbol Directories}.
10224 @item
10225 Suffixes (used in suffix rules) that end with the character @samp{~}
10226 have a special meaning to System V @code{make};
10227 they refer to the SCCS file that corresponds
10228 to the file one would get without the @samp{~}.  For example, the
10229 suffix rule @samp{.c~.o} would make the file @file{@var{n}.o} from
10230 the SCCS file @file{s.@var{n}.c}.  For complete coverage, a whole
10231 series of such suffix rules is required.
10232 @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
10234 In GNU @code{make}, this entire series of cases is handled by two
10235 pattern rules for extraction from SCCS, in combination with the
10236 general feature of rule chaining.
10237 @xref{Chained Rules, ,Chains of Implicit Rules}.
10239 @item
10240 In System V and 4.3 BSD @code{make}, files found by @code{VPATH} search
10241 (@pxref{Directory Search, ,Searching Directories for Prerequisites}) have their names changed inside command
10242 strings.  We feel it is much cleaner to always use automatic variables
10243 and thus make this feature obsolete.@refill
10245 @item
10246 In some Unix @code{make}s, the automatic variable @code{$*} appearing in
10247 the prerequisites of a rule has the amazingly strange ``feature'' of
10248 expanding to the full name of the @emph{target of that rule}.  We cannot
10249 imagine what went on in the minds of Unix @code{make} developers to do
10250 this; it is utterly inconsistent with the normal definition of @code{$*}.
10251 @vindex * @r{(automatic variable), unsupported bizarre usage}
10253 @item
10254 In some Unix @code{make}s, implicit rule search
10255 (@pxref{Implicit Rules, ,Using Implicit Rules}) is apparently done for
10256 @emph{all} targets, not just those without commands.  This means you can
10257 do:@refill
10259 @example
10260 @group
10261 foo.o:
10262         cc -c foo.c
10263 @end group
10264 @end example
10266 @noindent
10267 and Unix @code{make} will intuit that @file{foo.o} depends on
10268 @file{foo.c}.@refill
10270 We feel that such usage is broken.  The prerequisite properties of
10271 @code{make} are well-defined (for GNU @code{make}, at least),
10272 and doing such a thing simply does not fit the model.@refill
10274 @item
10275 GNU @code{make} does not include any built-in implicit rules for
10276 compiling or preprocessing EFL programs.  If we hear of anyone who is
10277 using EFL, we will gladly add them.
10279 @item
10280 It appears that in SVR4 @code{make}, a suffix rule can be specified with
10281 no commands, and it is treated as if it had empty commands
10282 (@pxref{Empty Commands}).  For example:
10284 @example
10285 .c.a:
10286 @end example
10288 @noindent
10289 will override the built-in @file{.c.a} suffix rule.
10291 We feel that it is cleaner for a rule without commands to always simply
10292 add to the prerequisite list for the target.  The above example can be
10293 easily rewritten to get the desired behavior in GNU @code{make}:
10295 @example
10296 .c.a: ;
10297 @end example
10299 @item
10300 Some versions of @code{make} invoke the shell with the @samp{-e} flag,
10301 except under @samp{-k} (@pxref{Testing, ,Testing the Compilation of a
10302 Program}).  The @samp{-e} flag tells the shell to exit as soon as any
10303 program it runs returns a nonzero status.  We feel it is cleaner to
10304 write each shell command line to stand on its own and not require this
10305 special treatment.
10306 @end itemize
10308 @comment The makefile standards are in a separate file that is also
10309 @comment included by standards.texi.
10310 @include make-stds.texi
10312 @node Quick Reference, Error Messages, Makefile Conventions, Top
10313 @appendix Quick Reference
10315 This appendix summarizes the directives, text manipulation functions,
10316 and special variables which GNU @code{make} understands.
10317 @xref{Special Targets}, @ref{Catalogue of Rules, ,Catalogue of Implicit Rules},
10318 and @ref{Options Summary, ,Summary of Options},
10319 for other summaries.
10321 Here is a summary of the directives GNU @code{make} recognizes:
10323 @table @code
10324 @item define @var{variable}
10325 @itemx endef
10327 Define a multi-line, recursively-expanded variable.@*
10328 @xref{Sequences}.
10330 @item ifdef @var{variable}
10331 @itemx ifndef @var{variable}
10332 @itemx ifeq (@var{a},@var{b})
10333 @itemx ifeq "@var{a}" "@var{b}"
10334 @itemx ifeq '@var{a}' '@var{b}'
10335 @itemx ifneq (@var{a},@var{b})
10336 @itemx ifneq "@var{a}" "@var{b}"
10337 @itemx ifneq '@var{a}' '@var{b}'
10338 @itemx else
10339 @itemx endif
10341 Conditionally evaluate part of the makefile.@*
10342 @xref{Conditionals}.
10344 @item include @var{file}
10345 @itemx -include @var{file}
10346 @itemx sinclude @var{file}
10348 Include another makefile.@*
10349 @xref{Include, ,Including Other Makefiles}.
10351 @item override @var{variable} = @var{value}
10352 @itemx override @var{variable} := @var{value}
10353 @itemx override @var{variable} += @var{value}
10354 @itemx override @var{variable} ?= @var{value}
10355 @itemx override define @var{variable}
10356 @itemx endef
10358 Define a variable, overriding any previous definition, even one from
10359 the command line.@*
10360 @xref{Override Directive, ,The @code{override} Directive}.
10362 @item export
10364 Tell @code{make} to export all variables to child processes by default.@*
10365 @xref{Variables/Recursion, , Communicating Variables to a Sub-@code{make}}.
10367 @item export @var{variable}
10368 @itemx export @var{variable} = @var{value}
10369 @itemx export @var{variable} := @var{value}
10370 @itemx export @var{variable} += @var{value}
10371 @itemx export @var{variable} ?= @var{value}
10372 @itemx unexport @var{variable}
10373 Tell @code{make} whether or not to export a particular variable to child
10374 processes.@*
10375 @xref{Variables/Recursion, , Communicating Variables to a Sub-@code{make}}.
10377 @item vpath @var{pattern} @var{path}
10378 Specify a search path for files matching a @samp{%} pattern.@*
10379 @xref{Selective Search, , The @code{vpath} Directive}.
10381 @item vpath @var{pattern}
10382 Remove all search paths previously specified for @var{pattern}.
10384 @item vpath
10385 Remove all search paths previously specified in any @code{vpath}
10386 directive.
10387 @end table
10389 Here is a summary of the built-in functions (@pxref{Functions}):
10391 @table @code
10392 @item $(subst @var{from},@var{to},@var{text})
10393 Replace @var{from} with @var{to} in @var{text}.@*
10394 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10396 @item $(patsubst @var{pattern},@var{replacement},@var{text})
10397 Replace words matching @var{pattern} with @var{replacement} in @var{text}.@*
10398 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10400 @item $(strip @var{string})
10401 Remove excess whitespace characters from @var{string}.@*
10402 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10404 @item $(findstring @var{find},@var{text})
10405 Locate @var{find} in @var{text}.@*
10406 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10408 @item $(filter @var{pattern}@dots{},@var{text})
10409 Select words in @var{text} that match one of the @var{pattern} words.@*
10410 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10412 @item $(filter-out @var{pattern}@dots{},@var{text})
10413 Select words in @var{text} that @emph{do not} match any of the @var{pattern} words.@*
10414 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10416 @item $(sort @var{list})
10417 Sort the words in @var{list} lexicographically, removing duplicates.@*
10418 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10420 @item $(word @var{n},@var{text})
10421 Extract the @var{n}th word (one-origin) of @var{text}.@*
10422 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10424 @item $(words @var{text})
10425 Count the number of words in @var{text}.@*
10426 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10428 @item $(wordlist @var{s},@var{e},@var{text})
10429 Returns the list of words in @var{text} from @var{s} to @var{e}.@*
10430 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10432 @item $(firstword @var{names}@dots{})
10433 Extract the first word of @var{names}.@*
10434 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10436 @item $(lastword @var{names}@dots{})
10437 Extract the last word of @var{names}.@*
10438 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10440 @item $(dir @var{names}@dots{})
10441 Extract the directory part of each file name.@*
10442 @xref{File Name Functions, ,Functions for File Names}.
10444 @item $(notdir @var{names}@dots{})
10445 Extract the non-directory part of each file name.@*
10446 @xref{File Name Functions, ,Functions for File Names}.
10448 @item $(suffix @var{names}@dots{})
10449 Extract the suffix (the last @samp{.} and following characters) of each file name.@*
10450 @xref{File Name Functions, ,Functions for File Names}.
10452 @item $(basename @var{names}@dots{})
10453 Extract the base name (name without suffix) of each file name.@*
10454 @xref{File Name Functions, ,Functions for File Names}.
10456 @item $(addsuffix @var{suffix},@var{names}@dots{})
10457 Append @var{suffix} to each word in @var{names}.@*
10458 @xref{File Name Functions, ,Functions for File Names}.
10460 @item $(addprefix @var{prefix},@var{names}@dots{})
10461 Prepend @var{prefix} to each word in @var{names}.@*
10462 @xref{File Name Functions, ,Functions for File Names}.
10464 @item $(join @var{list1},@var{list2})
10465 Join two parallel lists of words.@*
10466 @xref{File Name Functions, ,Functions for File Names}.
10468 @item $(wildcard @var{pattern}@dots{})
10469 Find file names matching a shell file name pattern (@emph{not} a
10470 @samp{%} pattern).@*
10471 @xref{Wildcard Function, ,The Function @code{wildcard}}.
10473 @item $(realpath @var{names}@dots{})
10474 For each file name in @var{names}, expand to an absolute name that
10475 does not contain any @code{.}, @code{..}, nor symlinks.@*
10476 @xref{File Name Functions, ,Functions for File Names}.
10478 @item $(abspath @var{names}@dots{})
10479 For each file name in @var{names}, expand to an absolute name that
10480 does not contain any @code{.} or @code{..} components, but preserves
10481 symlinks.@*
10482 @xref{File Name Functions, ,Functions for File Names}.
10484 @item $(error @var{text}@dots{})
10486 When this function is evaluated, @code{make} generates a fatal error
10487 with the message @var{text}.@*
10488 @xref{Make Control Functions, ,Functions That Control Make}.
10490 @item $(warning @var{text}@dots{})
10492 When this function is evaluated, @code{make} generates a warning with
10493 the message @var{text}.@*
10494 @xref{Make Control Functions, ,Functions That Control Make}.
10496 @item $(shell @var{command})
10498 Execute a shell command and return its output.@*
10499 @xref{Shell Function, , The @code{shell} Function}.
10501 @item $(origin @var{variable})
10503 Return a string describing how the @code{make} variable @var{variable} was
10504 defined.@*
10505 @xref{Origin Function, , The @code{origin} Function}.
10507 @item $(flavor @var{variable})
10509 Return a string describing the flavor of the @code{make} variable
10510 @var{variable}.@*
10511 @xref{Flavor Function, , The @code{flavor} Function}.
10513 @item $(foreach @var{var},@var{words},@var{text})
10515 Evaluate @var{text} with @var{var} bound to each word in @var{words},
10516 and concatenate the results.@*
10517 @xref{Foreach Function, ,The @code{foreach} Function}.
10519 @item $(call @var{var},@var{param},@dots{})
10521 Evaluate the variable @var{var} replacing any references to @code{$(1)},
10522 @code{$(2)} with the first, second, etc.@: @var{param} values.@*
10523 @xref{Call Function, ,The @code{call} Function}.
10525 @item $(eval @var{text})
10527 Evaluate @var{text} then read the results as makefile commands.
10528 Expands to the empty string.@*
10529 @xref{Eval Function, ,The @code{eval} Function}.
10531 @item $(value @var{var})
10533 Evaluates to the contents of the variable @var{var}, with no expansion
10534 performed on it.@*
10535 @xref{Value Function, ,The @code{value} Function}.
10536 @end table
10538 Here is a summary of the automatic variables.
10539 @xref{Automatic Variables},
10540 for full information.
10542 @table @code
10543 @item $@@
10544 The file name of the target.
10546 @item $%
10547 The target member name, when the target is an archive member.
10549 @item $<
10550 The name of the first prerequisite.
10552 @item $?
10553 The names of all the prerequisites that are
10554 newer than the target, with spaces between them.
10555 For prerequisites which are archive members, only
10556 the member named is used (@pxref{Archives}).
10558 @item $^
10559 @itemx $+
10560 The names of all the prerequisites, with spaces between them.  For
10561 prerequisites which are archive members, only the member named is used
10562 (@pxref{Archives}).  The value of @code{$^} omits duplicate
10563 prerequisites, while @code{$+} retains them and preserves their order.
10565 @item $*
10566 The stem with which an implicit rule matches
10567 (@pxref{Pattern Match, ,How Patterns Match}).
10569 @item $(@@D)
10570 @itemx $(@@F)
10571 The directory part and the file-within-directory part of @code{$@@}.
10573 @item $(*D)
10574 @itemx $(*F)
10575 The directory part and the file-within-directory part of @code{$*}.
10577 @item $(%D)
10578 @itemx $(%F)
10579 The directory part and the file-within-directory part of @code{$%}.
10581 @item $(<D)
10582 @itemx $(<F)
10583 The directory part and the file-within-directory part of @code{$<}.
10585 @item $(^D)
10586 @itemx $(^F)
10587 The directory part and the file-within-directory part of @code{$^}.
10589 @item $(+D)
10590 @itemx $(+F)
10591 The directory part and the file-within-directory part of @code{$+}.
10593 @item $(?D)
10594 @itemx $(?F)
10595 The directory part and the file-within-directory part of @code{$?}.
10596 @end table
10598 These variables are used specially by GNU @code{make}:
10600 @table @code
10601 @item MAKEFILES
10603 Makefiles to be read on every invocation of @code{make}.@*
10604 @xref{MAKEFILES Variable, ,The Variable @code{MAKEFILES}}.
10606 @item VPATH
10608 Directory search path for files not found in the current directory.@*
10609 @xref{General Search, , @code{VPATH} Search Path for All Prerequisites}.
10611 @item SHELL
10613 The name of the system default command interpreter, usually @file{/bin/sh}.
10614 You can set @code{SHELL} in the makefile to change the shell used to run
10615 commands.  @xref{Execution, ,Command Execution}.  The @code{SHELL}
10616 variable is handled specially when importing from and exporting to the
10617 environment.  @xref{Choosing the Shell}.
10619 @item MAKESHELL
10621 On MS-DOS only, the name of the command interpreter that is to be used
10622 by @code{make}.  This value takes precedence over the value of
10623 @code{SHELL}.  @xref{Execution, ,MAKESHELL variable}.
10625 @item MAKE
10627 The name with which @code{make} was invoked.
10628 Using this variable in commands has special meaning.
10629 @xref{MAKE Variable, ,How the @code{MAKE} Variable Works}.
10631 @item MAKELEVEL
10633 The number of levels of recursion (sub-@code{make}s).@*
10634 @xref{Variables/Recursion}.
10636 @item MAKEFLAGS
10638 The flags given to @code{make}.  You can set this in the environment or
10639 a makefile to set flags.@*
10640 @xref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
10642 It is @emph{never} appropriate to use @code{MAKEFLAGS} directly on a
10643 command line: its contents may not be quoted correctly for use in the
10644 shell.  Always allow recursive @code{make}'s to obtain these values
10645 through the environment from its parent.
10647 @item MAKECMDGOALS
10649 The targets given to @code{make} on the command line.  Setting this
10650 variable has no effect on the operation of @code{make}.@*
10651 @xref{Goals, ,Arguments to Specify the Goals}.
10653 @item CURDIR
10655 Set to the pathname of the current working directory (after all
10656 @code{-C} options are processed, if any).  Setting this variable has no
10657 effect on the operation of @code{make}.@*
10658 @xref{Recursion, ,Recursive Use of @code{make}}.
10660 @item SUFFIXES
10662 The default list of suffixes before @code{make} reads any makefiles.
10664 @item .LIBPATTERNS
10665 Defines the naming of the libraries @code{make} searches for, and their
10666 order.@*
10667 @xref{Libraries/Search, ,Directory Search for Link Libraries}.
10668 @end table
10670 @node Error Messages, Complex Makefile, Quick Reference, Top
10671 @comment  node-name,  next,  previous,  up
10672 @appendix Errors Generated by Make
10674 Here is a list of the more common errors you might see generated by
10675 @code{make}, and some information about what they mean and how to fix
10676 them.
10678 Sometimes @code{make} errors are not fatal, especially in the presence
10679 of a @code{-} prefix on a command script line, or the @code{-k} command
10680 line option.  Errors that are fatal are prefixed with the string
10681 @code{***}.
10683 Error messages are all either prefixed with the name of the program
10684 (usually @samp{make}), or, if the error is found in a makefile, the name
10685 of the file and linenumber containing the problem.
10687 In the table below, these common prefixes are left off.
10689 @table @samp
10691 @item [@var{foo}] Error @var{NN}
10692 @itemx [@var{foo}] @var{signal description}
10693 These errors are not really @code{make} errors at all.  They mean that a
10694 program that @code{make} invoked as part of a command script returned a
10695 non-0 error code (@samp{Error @var{NN}}), which @code{make} interprets
10696 as failure, or it exited in some other abnormal fashion (with a
10697 signal of some type).  @xref{Errors, ,Errors in Commands}.
10699 If no @code{***} is attached to the message, then the subprocess failed
10700 but the rule in the makefile was prefixed with the @code{-} special
10701 character, so @code{make} ignored the error.
10703 @item missing separator.  Stop.
10704 @itemx missing separator (did you mean TAB instead of 8 spaces?).  Stop.
10705 This means that @code{make} could not understand much of anything about
10706 the command line it just read.  GNU @code{make} looks for various kinds
10707 of separators (@code{:}, @code{=}, TAB characters, etc.) to help it
10708 decide what kind of commandline it's seeing.  This means it couldn't
10709 find a valid one.
10711 One of the most common reasons for this message is that you (or perhaps
10712 your oh-so-helpful editor, as is the case with many MS-Windows editors)
10713 have attempted to indent your command scripts with spaces instead of a
10714 TAB character.  In this case, @code{make} will use the second form of
10715 the error above.  Remember that every line in the command script must
10716 begin with a TAB character.  Eight spaces do not count.  @xref{Rule
10717 Syntax}.
10719 @item commands commence before first target.  Stop.
10720 @itemx missing rule before commands.  Stop.
10721 This means the first thing in the makefile seems to be part of a command
10722 script: it begins with a TAB character and doesn't appear to be a legal
10723 @code{make} command (such as a variable assignment).  Command scripts
10724 must always be associated with a target.
10726 The second form is generated if the line has a semicolon as the first
10727 non-whitespace character; @code{make} interprets this to mean you left
10728 out the "target: prerequisite" section of a rule.  @xref{Rule Syntax}.
10730 @item No rule to make target `@var{xxx}'.
10731 @itemx No rule to make target `@var{xxx}', needed by `@var{yyy}'.
10732 This means that @code{make} decided it needed to build a target, but
10733 then couldn't find any instructions in the makefile on how to do that,
10734 either explicit or implicit (including in the default rules database).
10736 If you want that file to be built, you will need to add a rule to your
10737 makefile describing how that target can be built.  Other possible
10738 sources of this problem are typos in the makefile (if that filename is
10739 wrong) or a corrupted source tree (if that file is not supposed to be
10740 built, but rather only a prerequisite).
10742 @item No targets specified and no makefile found.  Stop.
10743 @itemx No targets.  Stop.
10744 The former means that you didn't provide any targets to be built on the
10745 command line, and @code{make} couldn't find any makefiles to read in.
10746 The latter means that some makefile was found, but it didn't contain any
10747 default goal and none was given on the command line.  GNU @code{make}
10748 has nothing to do in these situations.
10749 @xref{Makefile Arguments, ,Arguments to Specify the Makefile}.@refill
10751 @item Makefile `@var{xxx}' was not found.
10752 @itemx Included makefile `@var{xxx}' was not found.
10753 A makefile specified on the command line (first form) or included
10754 (second form) was not found.
10756 @item warning: overriding commands for target `@var{xxx}'
10757 @itemx warning: ignoring old commands for target `@var{xxx}'
10758 GNU @code{make} allows commands to be specified only once per target
10759 (except for double-colon rules).  If you give commands for a target
10760 which already has been defined to have commands, this warning is issued
10761 and the second set of commands will overwrite the first set.
10762 @xref{Multiple Rules, ,Multiple Rules for One Target}.
10764 @item Circular @var{xxx} <- @var{yyy} dependency dropped.
10765 This means that @code{make} detected a loop in the dependency graph:
10766 after tracing the prerequisite @var{yyy} of target @var{xxx}, and its
10767 prerequisites, etc., one of them depended on @var{xxx} again.
10769 @item Recursive variable `@var{xxx}' references itself (eventually).  Stop.
10770 This means you've defined a normal (recursive) @code{make} variable
10771 @var{xxx} that, when it's expanded, will refer to itself (@var{xxx}).
10772 This is not allowed; either use simply-expanded variables (@code{:=}) or
10773 use the append operator (@code{+=}).  @xref{Using Variables, ,How to Use
10774 Variables}.
10776 @item Unterminated variable reference.  Stop.
10777 This means you forgot to provide the proper closing parenthesis
10778 or brace in your variable or function reference.
10780 @item insufficient arguments to function `@var{xxx}'.  Stop.
10781 This means you haven't provided the requisite number of arguments for
10782 this function.  See the documentation of the function for a description
10783 of its arguments.  @xref{Functions, ,Functions for Transforming Text}.
10785 @item missing target pattern.  Stop.
10786 @itemx multiple target patterns.  Stop.
10787 @itemx target pattern contains no `%'.  Stop.
10788 @itemx mixed implicit and static pattern rules.  Stop.
10789 These are generated for malformed static pattern rules.  The first means
10790 there's no pattern in the target section of the rule; the second means
10791 there are multiple patterns in the target section; the third means
10792 the target doesn't contain a pattern character (@code{%}); and the
10793 fourth means that all three parts of the static pattern rule contain
10794 pattern characters (@code{%})--only the first two parts should.
10795 @xref{Static Usage, ,Syntax of Static Pattern Rules}.
10797 @item warning: -jN forced in submake: disabling jobserver mode.
10798 This warning and the next are generated if @code{make} detects error
10799 conditions related to parallel processing on systems where
10800 sub-@code{make}s can communicate (@pxref{Options/Recursion,
10801 ,Communicating Options to a Sub-@code{make}}).  This warning is
10802 generated if a recursive invocation of a @code{make} process is forced
10803 to have @samp{-j@var{N}} in its argument list (where @var{N} is greater
10804 than one).  This could happen, for example, if you set the @code{MAKE}
10805 environment variable to @samp{make -j2}.  In this case, the
10806 sub-@code{make} doesn't communicate with other @code{make} processes and
10807 will simply pretend it has two jobs of its own.
10809 @item warning: jobserver unavailable: using -j1.  Add `+' to parent make rule.
10810 In order for @code{make} processes to communicate, the parent will pass
10811 information to the child.  Since this could result in problems if the
10812 child process isn't actually a @code{make}, the parent will only do this
10813 if it thinks the child is a @code{make}.  The parent uses the normal
10814 algorithms to determine this (@pxref{MAKE Variable, ,How the @code{MAKE}
10815 Variable Works}).  If the makefile is constructed such that the parent
10816 doesn't know the child is a @code{make} process, then the child will
10817 receive only part of the information necessary.  In this case, the child
10818 will generate this warning message and proceed with its build in a
10819 sequential manner.
10821 @end table
10823 @node Complex Makefile, GNU Free Documentation License, Error Messages, Top
10824 @appendix Complex Makefile Example
10826 Here is the makefile for the GNU @code{tar} program.  This is a
10827 moderately complex makefile.
10829 Because it is the first target, the default goal is @samp{all}.  An
10830 interesting feature of this makefile is that @file{testpad.h} is a
10831 source file automatically created by the @code{testpad} program,
10832 itself compiled from @file{testpad.c}.
10834 If you type @samp{make} or @samp{make all}, then @code{make} creates
10835 the @file{tar} executable, the @file{rmt} daemon that provides
10836 remote tape access, and the @file{tar.info} Info file.
10838 If you type @samp{make install}, then @code{make} not only creates
10839 @file{tar}, @file{rmt}, and @file{tar.info}, but also installs
10840 them.
10842 If you type @samp{make clean}, then @code{make} removes the @samp{.o}
10843 files, and the @file{tar}, @file{rmt}, @file{testpad},
10844 @file{testpad.h}, and @file{core} files.
10846 If you type @samp{make distclean}, then @code{make} not only removes
10847 the same files as does @samp{make clean} but also the
10848 @file{TAGS}, @file{Makefile}, and @file{config.status} files.
10849 (Although it is not evident, this makefile (and
10850 @file{config.status}) is generated by the user with the
10851 @code{configure} program, which is provided in the @code{tar}
10852 distribution, but is not shown here.)
10854 If you type @samp{make realclean}, then @code{make} removes the same
10855 files as does @samp{make distclean} and also removes the Info files
10856 generated from @file{tar.texinfo}.
10858 In addition, there are targets @code{shar} and @code{dist} that create
10859 distribution kits.
10861 @example
10862 @group
10863 # Generated automatically from Makefile.in by configure.
10864 # Un*x Makefile for GNU tar program.
10865 # Copyright (C) 1991 Free Software Foundation, Inc.
10866 @end group
10868 @group
10869 # This program is free software; you can redistribute
10870 # it and/or modify it under the terms of the GNU
10871 # General Public License @dots{}
10872 @dots{}
10873 @dots{}
10874 @end group
10876 SHELL = /bin/sh
10878 #### Start of system configuration section. ####
10880 srcdir = .
10882 @group
10883 # If you use gcc, you should either run the
10884 # fixincludes script that comes with it or else use
10885 # gcc with the -traditional option.  Otherwise ioctl
10886 # calls will be compiled incorrectly on some systems.
10887 CC = gcc -O
10888 YACC = bison -y
10889 INSTALL = /usr/local/bin/install -c
10890 INSTALLDATA = /usr/local/bin/install -c -m 644
10891 @end group
10893 # Things you might add to DEFS:
10894 # -DSTDC_HEADERS        If you have ANSI C headers and
10895 #                       libraries.
10896 # -DPOSIX               If you have POSIX.1 headers and
10897 #                       libraries.
10898 # -DBSD42               If you have sys/dir.h (unless
10899 #                       you use -DPOSIX), sys/file.h,
10900 #                       and st_blocks in `struct stat'.
10901 # -DUSG                 If you have System V/ANSI C
10902 #                       string and memory functions
10903 #                       and headers, sys/sysmacros.h,
10904 #                       fcntl.h, getcwd, no valloc,
10905 #                       and ndir.h (unless
10906 #                       you use -DDIRENT).
10907 # -DNO_MEMORY_H         If USG or STDC_HEADERS but do not
10908 #                       include memory.h.
10909 # -DDIRENT              If USG and you have dirent.h
10910 #                       instead of ndir.h.
10911 # -DSIGTYPE=int         If your signal handlers
10912 #                       return int, not void.
10913 # -DNO_MTIO             If you lack sys/mtio.h
10914 #                       (magtape ioctls).
10915 # -DNO_REMOTE           If you do not have a remote shell
10916 #                       or rexec.
10917 # -DUSE_REXEC           To use rexec for remote tape
10918 #                       operations instead of
10919 #                       forking rsh or remsh.
10920 # -DVPRINTF_MISSING     If you lack vprintf function
10921 #                       (but have _doprnt).
10922 # -DDOPRNT_MISSING      If you lack _doprnt function.
10923 #                       Also need to define
10924 #                       -DVPRINTF_MISSING.
10925 # -DFTIME_MISSING       If you lack ftime system call.
10926 # -DSTRSTR_MISSING      If you lack strstr function.
10927 # -DVALLOC_MISSING      If you lack valloc function.
10928 # -DMKDIR_MISSING       If you lack mkdir and
10929 #                       rmdir system calls.
10930 # -DRENAME_MISSING      If you lack rename system call.
10931 # -DFTRUNCATE_MISSING   If you lack ftruncate
10932 #                       system call.
10933 # -DV7                  On Version 7 Unix (not
10934 #                       tested in a long time).
10935 # -DEMUL_OPEN3          If you lack a 3-argument version
10936 #                       of open, and want to emulate it
10937 #                       with system calls you do have.
10938 # -DNO_OPEN3            If you lack the 3-argument open
10939 #                       and want to disable the tar -k
10940 #                       option instead of emulating open.
10941 # -DXENIX               If you have sys/inode.h
10942 #                       and need it 94 to be included.
10944 DEFS =  -DSIGTYPE=int -DDIRENT -DSTRSTR_MISSING \
10945         -DVPRINTF_MISSING -DBSD42
10946 # Set this to rtapelib.o unless you defined NO_REMOTE,
10947 # in which case make it empty.
10948 RTAPELIB = rtapelib.o
10949 LIBS =
10950 DEF_AR_FILE = /dev/rmt8
10951 DEFBLOCKING = 20
10953 @group
10954 CDEBUG = -g
10955 CFLAGS = $(CDEBUG) -I. -I$(srcdir) $(DEFS) \
10956         -DDEF_AR_FILE=\"$(DEF_AR_FILE)\" \
10957         -DDEFBLOCKING=$(DEFBLOCKING)
10958 LDFLAGS = -g
10959 @end group
10961 @group
10962 prefix = /usr/local
10963 # Prefix for each installed program,
10964 # normally empty or `g'.
10965 binprefix =
10967 # The directory to install tar in.
10968 bindir = $(prefix)/bin
10970 # The directory to install the info files in.
10971 infodir = $(prefix)/info
10972 @end group
10974 #### End of system configuration section. ####
10976 SRC1 =  tar.c create.c extract.c buffer.c \
10977         getoldopt.c update.c gnu.c mangle.c
10978 SRC2 =  version.c list.c names.c diffarch.c \
10979         port.c wildmat.c getopt.c
10980 SRC3 =  getopt1.c regex.c getdate.y
10981 SRCS =  $(SRC1) $(SRC2) $(SRC3)
10982 OBJ1 =  tar.o create.o extract.o buffer.o \
10983         getoldopt.o update.o gnu.o mangle.o
10984 OBJ2 =  version.o list.o names.o diffarch.o \
10985         port.o wildmat.o getopt.o
10986 OBJ3 =  getopt1.o regex.o getdate.o $(RTAPELIB)
10987 OBJS =  $(OBJ1) $(OBJ2) $(OBJ3)
10988 @group
10989 AUX =   README COPYING ChangeLog Makefile.in  \
10990         makefile.pc configure configure.in \
10991         tar.texinfo tar.info* texinfo.tex \
10992         tar.h port.h open3.h getopt.h regex.h \
10993         rmt.h rmt.c rtapelib.c alloca.c \
10994         msd_dir.h msd_dir.c tcexparg.c \
10995         level-0 level-1 backup-specs testpad.c
10996 @end group
10998 .PHONY: all
10999 all:    tar rmt tar.info
11001 @group
11002 .PHONY: tar
11003 tar:    $(OBJS)
11004         $(CC) $(LDFLAGS) -o $@@ $(OBJS) $(LIBS)
11005 @end group
11007 @group
11008 rmt:    rmt.c
11009         $(CC) $(CFLAGS) $(LDFLAGS) -o $@@ rmt.c
11010 @end group
11012 @group
11013 tar.info: tar.texinfo
11014         makeinfo tar.texinfo
11015 @end group
11017 @group
11018 .PHONY: install
11019 install: all
11020         $(INSTALL) tar $(bindir)/$(binprefix)tar
11021         -test ! -f rmt || $(INSTALL) rmt /etc/rmt
11022         $(INSTALLDATA) $(srcdir)/tar.info* $(infodir)
11023 @end group
11025 @group
11026 $(OBJS): tar.h port.h testpad.h
11027 regex.o buffer.o tar.o: regex.h
11028 # getdate.y has 8 shift/reduce conflicts.
11029 @end group
11031 @group
11032 testpad.h: testpad
11033         ./testpad
11034 @end group
11036 @group
11037 testpad: testpad.o
11038         $(CC) -o $@@ testpad.o
11039 @end group
11041 @group
11042 TAGS:   $(SRCS)
11043         etags $(SRCS)
11044 @end group
11046 @group
11047 .PHONY: clean
11048 clean:
11049         rm -f *.o tar rmt testpad testpad.h core
11050 @end group
11052 @group
11053 .PHONY: distclean
11054 distclean: clean
11055         rm -f TAGS Makefile config.status
11056 @end group
11058 @group
11059 .PHONY: realclean
11060 realclean: distclean
11061         rm -f tar.info*
11062 @end group
11064 @group
11065 .PHONY: shar
11066 shar: $(SRCS) $(AUX)
11067         shar $(SRCS) $(AUX) | compress \
11068           > tar-`sed -e '/version_string/!d' \
11069                      -e 's/[^0-9.]*\([0-9.]*\).*/\1/' \
11070                      -e q
11071                      version.c`.shar.Z
11072 @end group
11074 @group
11075 .PHONY: dist
11076 dist: $(SRCS) $(AUX)
11077         echo tar-`sed \
11078              -e '/version_string/!d' \
11079              -e 's/[^0-9.]*\([0-9.]*\).*/\1/' \
11080              -e q
11081              version.c` > .fname
11082         -rm -rf `cat .fname`
11083         mkdir `cat .fname`
11084         ln $(SRCS) $(AUX) `cat .fname`
11085         tar chZf `cat .fname`.tar.Z `cat .fname`
11086         -rm -rf `cat .fname` .fname
11087 @end group
11089 @group
11090 tar.zoo: $(SRCS) $(AUX)
11091         -rm -rf tmp.dir
11092         -mkdir tmp.dir
11093         -rm tar.zoo
11094         for X in $(SRCS) $(AUX) ; do \
11095             echo $$X ; \
11096             sed 's/$$/^M/' $$X \
11097             > tmp.dir/$$X ; done
11098         cd tmp.dir ; zoo aM ../tar.zoo *
11099         -rm -rf tmp.dir
11100 @end group
11101 @end example
11103 @raisesections
11104 @include fdl.texi
11105 @lowersections
11107 @node Concept Index, Name Index, GNU Free Documentation License, Top
11108 @unnumbered Index of Concepts
11110 @printindex cp
11112 @node Name Index,  , Concept Index, Top
11113 @unnumbered Index of Functions, Variables, & Directives
11115 @printindex fn
11117 @summarycontents
11118 @contents
11119 @bye