Manual typo.
[make.git] / doc / make.texi
blob67df5841ac0a62862aaab1af690e1912a325d3e3
1 \input texinfo                @c -*- Texinfo -*-
2 @c %**start of header
3 @setfilename make.info
5 @include version.texi
6 @set EDITION 0.70
7 @set RCSID $Id: make.texi,v 1.52 2008/05/18 15:11:40 psmith Exp $
9 @settitle GNU @code{make}
10 @setchapternewpage odd
11 @c Combine the variable and function indices:
12 @syncodeindex vr fn
13 @c Combine the program and concept indices:
14 @syncodeindex pg cp
15 @c FSF publishers: format makebook.texi instead of using this file directly.
16 @c ISBN provided by Lisa M. Opus Goldstein <opus@gnu.org>, 5 May 2004
17 @set ISBN 1-882114-83-5
18 @c %**end of header
20 @copying
21 This file documents the GNU @code{make} utility, which determines
22 automatically which pieces of a large program need to be recompiled,
23 and issues the commands to recompile them.
25 This is Edition @value{EDITION}, last updated @value{UPDATED},
26 of @cite{The GNU Make Manual}, for GNU @code{make} version @value{VERSION}.
28 Copyright @copyright{} 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
29 1996, 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005, 2006, 2007
30 Free Software Foundation, Inc.
32 @quotation
33 Permission is granted to copy, distribute and/or modify this document
34 under the terms of the GNU Free Documentation License, Version 1.2 or
35 any later version published by the Free Software Foundation; with no
36 Invariant Sections, with the Front-Cover Texts being ``A GNU Manual,''
37 and with the Back-Cover Texts as in (a) below.  A copy of the
38 license is included in the section entitled ``GNU Free Documentation
39 License.''
41 (a) The FSF's Back-Cover Text is: ``You are free to copy and modify
42 this GNU Manual. Buying copies from GNU Press supports the FSF in
43 developing GNU and promoting software freedom.''
44 @end quotation
45 @end copying
47 @c finalout
49 @c ISPELL CHECK: done, 10 June 1993 --roland
50 @c ISPELL CHECK: done, 2000-06-25 --Martin Buchholz
53 @dircategory Software development
54 @direntry
55 * Make: (make).            Remake files automatically.
56 @end direntry
58 @iftex
59 @shorttitlepage GNU Make
60 @end iftex
61 @titlepage
62 @title GNU Make
63 @subtitle A Program for Directing Recompilation
64 @subtitle GNU @code{make} Version @value{VERSION}
65 @subtitle @value{UPDATED-MONTH}
66 @author Richard M. Stallman, Roland McGrath, Paul D. Smith
67 @page
68 @vskip 0pt plus 1filll
69 @insertcopying
70 @sp 2
71 Published by the Free Software Foundation @*
72 51 Franklin St. -- Fifth Floor @*
73 Boston, MA 02110-1301 USA @*
74 ISBN @value{ISBN} @*
75 @sp 2
76 Cover art by Etienne Suvasa.
77 @end titlepage
79 @summarycontents
80 @contents
82 @ifnottex
83 @node Top, Overview, (dir), (dir)
84 @top GNU @code{make}
86 @insertcopying
87 @end ifnottex
89 @menu
90 * Overview::                    Overview of @code{make}.
91 * Introduction::                An introduction to @code{make}.
92 * Makefiles::                   Makefiles tell @code{make} what to do.
93 * Rules::                       Rules describe when a file must be remade.
94 * Recipes::                     Recipes say how to remake a file.
95 * Using Variables::             You can use variables to avoid repetition.
96 * Conditionals::                Use or ignore parts of the makefile based
97                                   on the values of variables.
98 * Functions::                   Many powerful ways to manipulate text.
99 * Invoking make: Running.       How to invoke @code{make} on the command line.
100 * Implicit Rules::              Use implicit rules to treat many files alike,
101                                   based on their file names.
102 * Archives::                    How @code{make} can update library archives.
103 * Features::                    Features GNU @code{make} has over other @code{make}s.
104 * Missing::                     What GNU @code{make} lacks from other @code{make}s.
105 * Makefile Conventions::        Conventions for writing makefiles for
106                                   GNU programs.
107 * Quick Reference::             A quick reference for experienced users.
108 * Error Messages::              A list of common errors generated by @code{make}.
109 * Complex Makefile::            A real example of a straightforward,
110                                   but nontrivial, makefile.
112 * GNU Free Documentation License::  License for copying this manual
113 * Concept Index::               Index of Concepts
114 * Name Index::                  Index of Functions, Variables, & Directives
116 @detailmenu
117  --- The Detailed Node Listing ---
119 Overview of @code{make}
121 * Preparing::                   Preparing and running make
122 * Reading::                     On reading this text
123 * Bugs::                        Problems and bugs
125 An Introduction to Makefiles
127 * Rule Introduction::           What a rule looks like.
128 * Simple Makefile::             A simple makefile
129 * How Make Works::              How @code{make} processes this makefile
130 * Variables Simplify::          Variables make makefiles simpler
131 * make Deduces::                Letting @code{make} deduce the recipe
132 * Combine By Prerequisite::     Another style of makefile
133 * Cleanup::                     Rules for cleaning the directory
135 Writing Makefiles
137 * Makefile Contents::           What makefiles contain.
138 * Makefile Names::              How to name your makefile.
139 * Include::                     How one makefile can use another makefile.
140 * MAKEFILES Variable::          The environment can specify extra makefiles.
141 * Remaking Makefiles::          How makefiles get remade.
142 * Overriding Makefiles::        How to override part of one makefile
143                                   with another makefile.
144 * Reading Makefiles::           How makefiles are parsed.
145 * Secondary Expansion::         How and when secondary expansion is performed.
147 Writing Rules
149 * Rule Example::                An example explained.
150 * Rule Syntax::                 General syntax explained.
151 * Prerequisite Types::          There are two types of prerequisites.
152 * Wildcards::                   Using wildcard characters such as `*'.
153 * Directory Search::            Searching other directories for source files.
154 * Phony Targets::               Using a target that is not a real file's name.
155 * Force Targets::               You can use a target without a recipe
156                                   or prerequisites to mark other targets
157                                   as phony.
158 * Empty Targets::               When only the date matters and the
159                                   files are empty.
160 * Special Targets::             Targets with special built-in meanings.
161 * Multiple Targets::            When to make use of several targets in a rule.
162 * Multiple Rules::              How to use several rules with the same target.
163 * Static Pattern::              Static pattern rules apply to multiple targets
164                                   and can vary the prerequisites according to
165                                   the target name.
166 * Double-Colon::                How to use a special kind of rule to allow
167                                   several independent rules for one target.
168 * Automatic Prerequisites::     How to automatically generate rules giving
169                                   prerequisites from source files themselves.
171 Using Wildcard Characters in File Names
173 * Wildcard Examples::           Several examples
174 * Wildcard Pitfall::            Problems to avoid.
175 * Wildcard Function::           How to cause wildcard expansion where
176                                   it does not normally take place.
178 Searching Directories for Prerequisites
180 * General Search::              Specifying a search path that applies
181                                   to every prerequisite.
182 * Selective Search::            Specifying a search path
183                                   for a specified class of names.
184 * Search Algorithm::            When and how search paths are applied.
185 * Recipes/Search::              How to write recipes that work together
186                                   with search paths.
187 * Implicit/Search::             How search paths affect implicit rules.
188 * Libraries/Search::            Directory search for link libraries.
190 Static Pattern Rules
192 * Static Usage::                The syntax of static pattern rules.
193 * Static versus Implicit::      When are they better than implicit rules?
195 Writing Recipes in Rules
197 * Recipe Syntax::               Recipe syntax features and pitfalls.
198 * Echoing::                     How to control when recipes are echoed.
199 * Execution::                   How recipes are executed.
200 * Parallel::                    How recipes can be executed in parallel.
201 * Errors::                      What happens after a recipe execution error.
202 * Interrupts::                  What happens when a recipe is interrupted.
203 * Recursion::                   Invoking @code{make} from makefiles.
204 * Sequences::                   Defining canned recipes.
205 * Empty Recipes::               Defining useful, do-nothing recipes.
207 Recipe Syntax
209 * Splitting Lines::             Breaking long recipe lines for readability.
210 * Variables in Recipes::        Using @code{make} variables in recipes.
212 Recipe Execution
214 * Choosing the Shell::          How @code{make} chooses the shell used
215                                   to run recipes.
217 Recursive Use of @code{make}
219 * MAKE Variable::               The special effects of using @samp{$(MAKE)}.
220 * Variables/Recursion::         How to communicate variables to a sub-@code{make}.
221 * Options/Recursion::           How to communicate options to a sub-@code{make}.
222 * -w Option::                   How the @samp{-w} or @samp{--print-directory} option
223                                   helps debug use of recursive @code{make} commands.
225 How to Use Variables
227 * Reference::                   How to use the value of a variable.
228 * Flavors::                     Variables come in two flavors.
229 * Advanced::                    Advanced features for referencing a variable.
230 * Values::                      All the ways variables get their values.
231 * Setting::                     How to set a variable in the makefile.
232 * Appending::                   How to append more text to the old value
233                                   of a variable.
234 * Override Directive::          How to set a variable in the makefile even if
235                                   the user has set it with a command argument.
236 * Defining::                    An alternate way to set a variable
237                                   to a verbatim string.
238 * Environment::                 Variable values can come from the environment.
239 * Target-specific::             Variable values can be defined on a per-target
240                                   basis.
241 * Pattern-specific::            Target-specific variable values can be applied
242                                   to a group of targets that match a pattern.
243 * Special Variables::           Variables with special meaning or behavior.
245 Advanced Features for Reference to Variables
247 * Substitution Refs::           Referencing a variable with
248                                   substitutions on the value.
249 * Computed Names::              Computing the name of the variable to refer to.
251 Conditional Parts of Makefiles
253 * Conditional Example::         Example of a conditional
254 * Conditional Syntax::          The syntax of conditionals.
255 * Testing Flags::               Conditionals that test flags.
257 Functions for Transforming Text
259 * Syntax of Functions::         How to write a function call.
260 * Text Functions::              General-purpose text manipulation functions.
261 * File Name Functions::         Functions for manipulating file names.
262 * Conditional Functions::       Functions that implement conditions.
263 * Foreach Function::            Repeat some text with controlled variation.
264 * Call Function::               Expand a user-defined function.
265 * Value Function::              Return the un-expanded value of a variable.
266 * Eval Function::               Evaluate the arguments as makefile syntax.
267 * Origin Function::             Find where a variable got its value.
268 * Flavor Function::             Find out the flavor of a variable.
269 * Shell Function::              Substitute the output of a shell command.
270 * Make Control Functions::      Functions that control how make runs.
272 How to Run @code{make}
274 * Makefile Arguments::          How to specify which makefile to use.
275 * Goals::                       How to use goal arguments to specify which
276                                   parts of the makefile to use.
277 * Instead of Execution::        How to use mode flags to specify what
278                                   kind of thing to do with the recipes
279                                   in the makefile other than simply
280                                   execute them.
281 * Avoiding Compilation::        How to avoid recompiling certain files.
282 * Overriding::                  How to override a variable to specify
283                                   an alternate compiler and other things.
284 * Testing::                     How to proceed past some errors, to
285                                   test compilation.
286 * Options Summary::             Summary of Options
288 Using Implicit Rules
290 * Using Implicit::              How to use an existing implicit rule
291                                   to get the recipe for updating a file.
292 * Catalogue of Rules::          A list of built-in implicit rules.
293 * Implicit Variables::          How to change what predefined rules do.
294 * Chained Rules::               How to use a chain of implicit rules.
295 * Pattern Rules::               How to define new implicit rules.
296 * Last Resort::                 How to define a recipe for rules which
297                                   cannot find any.
298 * Suffix Rules::                The old-fashioned style of implicit rule.
299 * Implicit Rule Search::        The precise algorithm for applying
300                                   implicit rules.
302 Defining and Redefining Pattern Rules
304 * Pattern Intro::               An introduction to pattern rules.
305 * Pattern Examples::            Examples of pattern rules.
306 * Automatic Variables::         How to use automatic variables in the
307                                   recipe of implicit rules.
308 * Pattern Match::               How patterns match.
309 * Match-Anything Rules::        Precautions you should take prior to
310                                   defining rules that can match any
311                                   target file whatever.
312 * Canceling Rules::             How to override or cancel built-in rules.
314 Using @code{make} to Update Archive Files
316 * Archive Members::             Archive members as targets.
317 * Archive Update::              The implicit rule for archive member targets.
318 * Archive Pitfalls::            Dangers to watch out for when using archives.
319 * Archive Suffix Rules::        You can write a special kind of suffix rule
320                                   for updating archives.
322 Implicit Rule for Archive Member Targets
324 * Archive Symbols::             How to update archive symbol directories.
326 @end detailmenu
327 @end menu
329 @node Overview, Introduction, Top, Top
330 @comment  node-name,  next,  previous,  up
331 @chapter Overview of @code{make}
333 The @code{make} utility automatically determines which pieces of a large
334 program need to be recompiled, and issues commands to recompile them.
335 This manual describes GNU @code{make}, which was implemented by Richard
336 Stallman and Roland McGrath.  Development since Version 3.76 has been
337 handled by Paul D. Smith.
339 GNU @code{make} conforms to section 6.2 of @cite{IEEE Standard
340 1003.2-1992} (POSIX.2).
341 @cindex POSIX
342 @cindex IEEE Standard 1003.2
343 @cindex standards conformance
345 Our examples show C programs, since they are most common, but you can use
346 @code{make} with any programming language whose compiler can be run with a
347 shell command.  Indeed, @code{make} is not limited to programs.  You can
348 use it to describe any task where some files must be updated automatically
349 from others whenever the others change.
351 @menu
352 * Preparing::                   Preparing and Running Make
353 * Reading::                     On Reading this Text
354 * Bugs::                        Problems and Bugs
355 @end menu
357 @node Preparing, Reading, Overview, Overview
358 @ifnottex
359 @heading Preparing and Running Make
360 @end ifnottex
362 To prepare to use @code{make}, you must write a file called
363 the @dfn{makefile} that describes the relationships among files
364 in your program and provides commands for updating each file.
365 In a program, typically, the executable file is updated from object
366 files, which are in turn made by compiling source files.@refill
368 Once a suitable makefile exists, each time you change some source files,
369 this simple shell command:
371 @example
372 make
373 @end example
375 @noindent
376 suffices to perform all necessary recompilations.  The @code{make} program
377 uses the makefile data base and the last-modification times of the files to
378 decide which of the files need to be updated.  For each of those files, it
379 issues the recipes recorded in the data base.
381 You can provide command line arguments to @code{make} to control which
382 files should be recompiled, or how.  @xref{Running, ,How to Run
383 @code{make}}.
385 @node Reading, Bugs, Preparing, Overview
386 @section How to Read This Manual
388 If you are new to @code{make}, or are looking for a general
389 introduction, read the first few sections of each chapter, skipping the
390 later sections.  In each chapter, the first few sections contain
391 introductory or general information and the later sections contain
392 specialized or technical information.
393 @ifnottex
394 The exception is the second chapter, @ref{Introduction, ,An
395 Introduction to Makefiles}, all of which is introductory.
396 @end ifnottex
397 @iftex
398 The exception is @ref{Introduction, ,An Introduction to Makefiles},
399 all of which is introductory.
400 @end iftex
402 If you are familiar with other @code{make} programs, see @ref{Features,
403 ,Features of GNU @code{make}}, which lists the enhancements GNU
404 @code{make} has, and @ref{Missing, ,Incompatibilities and Missing
405 Features}, which explains the few things GNU @code{make} lacks that
406 others have.
408 For a quick summary, see @ref{Options Summary}, @ref{Quick Reference},
409 and @ref{Special Targets}.
411 @node Bugs,  , Reading, Overview
412 @section Problems and Bugs
413 @cindex reporting bugs
414 @cindex bugs, reporting
415 @cindex problems and bugs, reporting
417 If you have problems with GNU @code{make} or think you've found a bug,
418 please report it to the developers; we cannot promise to do anything but
419 we might well want to fix it.
421 Before reporting a bug, make sure you've actually found a real bug.
422 Carefully reread the documentation and see if it really says you can do
423 what you're trying to do.  If it's not clear whether you should be able
424 to do something or not, report that too; it's a bug in the
425 documentation!
427 Before reporting a bug or trying to fix it yourself, try to isolate it
428 to the smallest possible makefile that reproduces the problem.  Then
429 send us the makefile and the exact results @code{make} gave you,
430 including any error or warning messages.  Please don't paraphrase
431 these messages: it's best to cut and paste them into your report.
432 When generating this small makefile, be sure to not use any non-free
433 or unusual tools in your recipes: you can almost always emulate what
434 such a tool would do with simple shell commands.  Finally, be sure to
435 explain what you expected to occur; this will help us decide whether
436 the problem was really in the documentation.
438 Once you have a precise problem you can report it in one of two ways.
439 Either send electronic mail to:
441 @example
442     bug-make@@gnu.org
443 @end example
445 @noindent
446 or use our Web-based project management tool, at:
448 @example
449     http://savannah.gnu.org/projects/make/
450 @end example
452 @noindent
453 In addition to the information above, please be careful to include the
454 version number of @code{make} you are using.  You can get this
455 information with the command @samp{make --version}.  Be sure also to
456 include the type of machine and operating system you are using.  One
457 way to obtain this information is by looking at the final lines of
458 output from the command @samp{make --help}.
460 @node Introduction, Makefiles, Overview, Top
461 @comment  node-name,  next,  previous,  up
462 @chapter An Introduction to Makefiles
464 You need a file called a @dfn{makefile} to tell @code{make} what to do.
465 Most often, the makefile tells @code{make} how to compile and link a
466 program.
467 @cindex makefile
469 In this chapter, we will discuss a simple makefile that describes how to
470 compile and link a text editor which consists of eight C source files
471 and three header files.  The makefile can also tell @code{make} how to
472 run miscellaneous commands when explicitly asked (for example, to remove
473 certain files as a clean-up operation).  To see a more complex example
474 of a makefile, see @ref{Complex Makefile}.
476 When @code{make} recompiles the editor, each changed C source file
477 must be recompiled.  If a header file has changed, each C source file
478 that includes the header file must be recompiled to be safe.  Each
479 compilation produces an object file corresponding to the source file.
480 Finally, if any source file has been recompiled, all the object files,
481 whether newly made or saved from previous compilations, must be linked
482 together to produce the new executable editor.
483 @cindex recompilation
484 @cindex editor
486 @menu
487 * Rule Introduction::           What a rule looks like.
488 * Simple Makefile::             A Simple Makefile
489 * How Make Works::              How @code{make} Processes This Makefile
490 * Variables Simplify::          Variables Make Makefiles Simpler
491 * make Deduces::                Letting @code{make} Deduce the Recipes
492 * Combine By Prerequisite::     Another Style of Makefile
493 * Cleanup::                     Rules for Cleaning the Directory
494 @end menu
496 @node Rule Introduction, Simple Makefile, Introduction, Introduction
497 @comment  node-name,  next,  previous,  up
498 @section What a Rule Looks Like
499 @cindex rule, introduction to
500 @cindex makefile rule parts
501 @cindex parts of makefile rule
503 A simple makefile consists of ``rules'' with the following shape:
505 @cindex targets, introduction to
506 @cindex prerequisites, introduction to
507 @cindex recipes, introduction to
508 @example
509 @group
510 @var{target} @dots{} : @var{prerequisites} @dots{}
511         @var{recipe}
512         @dots{}
513         @dots{}
514 @end group
515 @end example
517 A @dfn{target} is usually the name of a file that is generated by a
518 program; examples of targets are executable or object files.  A target
519 can also be the name of an action to carry out, such as @samp{clean}
520 (@pxref{Phony Targets}).
522 A @dfn{prerequisite} is a file that is used as input to create the
523 target.  A target often depends on several files.
525 @cindex tabs in rules
526 A @dfn{recipe} is an action that @code{make} carries out.
527 A recipe may have more than one command, each on its own line.
528 @strong{Please note:} you need to put a tab character at the beginning of
529 every command line!  This is an obscurity that catches the unwary.  If
530 you prefer to prefix your recipes with a character other than tab,
531 you can set the @code{.CMDPREFIX} variable to an alternate character
532 (@pxref{Special Variables}).
534 Usually a recipe is in a rule with prerequisites and serves to create a
535 target file if any of the prerequisites change.  However, the rule that
536 specifies a recipe for the target need not have prerequisites.  For
537 example, the rule containing the delete command associated with the
538 target @samp{clean} does not have prerequisites.
540 A @dfn{rule}, then, explains how and when to remake certain files
541 which are the targets of the particular rule.  @code{make} carries out
542 the recipe on the prerequisites to create or update the target.  A
543 rule can also explain how and when to carry out an action.
544 @xref{Rules, , Writing Rules}.
546 A makefile may contain other text besides rules, but a simple makefile
547 need only contain rules.  Rules may look somewhat more complicated
548 than shown in this template, but all fit the pattern more or less.
550 @node Simple Makefile, How Make Works, Rule Introduction, Introduction
551 @section A Simple Makefile
552 @cindex simple makefile
553 @cindex makefile, simple
555 Here is a straightforward makefile that describes the way an
556 executable file called @code{edit} depends on eight object files
557 which, in turn, depend on eight C source and three header files.
559 In this example, all the C files include @file{defs.h}, but only those
560 defining editing commands include @file{command.h}, and only low
561 level files that change the editor buffer include @file{buffer.h}.
563 @example
564 @group
565 edit : main.o kbd.o command.o display.o \
566        insert.o search.o files.o utils.o
567         cc -o edit main.o kbd.o command.o display.o \
568                    insert.o search.o files.o utils.o
570 main.o : main.c defs.h
571         cc -c main.c
572 kbd.o : kbd.c defs.h command.h
573         cc -c kbd.c
574 command.o : command.c defs.h command.h
575         cc -c command.c
576 display.o : display.c defs.h buffer.h
577         cc -c display.c
578 insert.o : insert.c defs.h buffer.h
579         cc -c insert.c
580 search.o : search.c defs.h buffer.h
581         cc -c search.c
582 files.o : files.c defs.h buffer.h command.h
583         cc -c files.c
584 utils.o : utils.c defs.h
585         cc -c utils.c
586 clean :
587         rm edit main.o kbd.o command.o display.o \
588            insert.o search.o files.o utils.o
589 @end group
590 @end example
592 @noindent
593 We split each long line into two lines using backslash-newline; this is
594 like using one long line, but is easier to read.
595 @cindex continuation lines
596 @cindex @code{\} (backslash), for continuation lines
597 @cindex backslash (@code{\}), for continuation lines
598 @cindex quoting newline, in makefile
599 @cindex newline, quoting, in makefile
601 To use this makefile to create the executable file called @file{edit},
602 type:
604 @example
605 make
606 @end example
608 To use this makefile to delete the executable file and all the object
609 files from the directory, type:
611 @example
612 make clean
613 @end example
615 In the example makefile, the targets include the executable file
616 @samp{edit}, and the object files @samp{main.o} and @samp{kbd.o}.  The
617 prerequisites are files such as @samp{main.c} and @samp{defs.h}.
618 In fact, each @samp{.o} file is both a target and a prerequisite.
619 Recipes include @w{@samp{cc -c main.c}} and @w{@samp{cc -c kbd.c}}.
621 When a target is a file, it needs to be recompiled or relinked if any
622 of its prerequisites change.  In addition, any prerequisites that are
623 themselves automatically generated should be updated first.  In this
624 example, @file{edit} depends on each of the eight object files; the
625 object file @file{main.o} depends on the source file @file{main.c} and
626 on the header file @file{defs.h}.
628 A recipe may follow each line that contains a target and
629 prerequisites.  These recipes say how to update the target file.  A
630 tab character (or whatever character is specified by the
631 @code{.CMDPREFIX} variable; @pxref{Special Variables}) must come at
632 the beginning of every line in the recipe to distinguish recipes from
633 other lines in the makefile.  (Bear in mind that @code{make} does not
634 know anything about how the recipes work.  It is up to you to supply
635 recipes that will update the target file properly.  All @code{make}
636 does is execute the commands in the recipe you have specified when the
637 target file needs to be updated.)@refill
638 @cindex recipe
640 The target @samp{clean} is not a file, but merely the name of an
641 action.  Since you normally do not want to carry out the actions in
642 this rule, @samp{clean} is not a prerequisite of any other rule.
643 Consequently, @code{make} never does anything with it unless you tell
644 it specifically.  Note that this rule not only is not a prerequisite,
645 it also does not have any prerequisites, so the only purpose of the
646 rule is to run the specified recipe.  Targets that do not refer to
647 files but are just actions are called @dfn{phony targets}.
648 @xref{Phony Targets}, for information about this kind of target.
649 @xref{Errors, , Errors in Recipes}, to see how to cause @code{make}
650 to ignore errors from @code{rm} or any other command.
651 @cindex @code{clean} target
652 @cindex @code{rm} (shell command)
654 @node How Make Works, Variables Simplify, Simple Makefile, Introduction
655 @comment  node-name,  next,  previous,  up
656 @section How @code{make} Processes a Makefile
657 @cindex processing a makefile
658 @cindex makefile, how @code{make} processes
660 By default, @code{make} starts with the first target (not targets whose
661 names start with @samp{.}).  This is called the @dfn{default goal}.
662 (@dfn{Goals} are the targets that @code{make} strives ultimately to
663 update.    You can override this behavior using the command line
664 (@pxref{Goals, , Arguments to Specify the Goals}) or with the
665 @code{.DEFAULT_GOAL} special variable (@pxref{Special Variables, ,
666 Other Special Variables}).
667 @cindex default goal
668 @cindex goal, default
669 @cindex goal
671 In the simple example of the previous section, the default goal is to
672 update the executable program @file{edit}; therefore, we put that rule
673 first.
675 Thus, when you give the command:
677 @example
678 make
679 @end example
681 @noindent
682 @code{make} reads the makefile in the current directory and begins by
683 processing the first rule.  In the example, this rule is for relinking
684 @file{edit}; but before @code{make} can fully process this rule, it
685 must process the rules for the files that @file{edit} depends on,
686 which in this case are the object files.  Each of these files is
687 processed according to its own rule.  These rules say to update each
688 @samp{.o} file by compiling its source file.  The recompilation must
689 be done if the source file, or any of the header files named as
690 prerequisites, is more recent than the object file, or if the object
691 file does not exist.
693 The other rules are processed because their targets appear as
694 prerequisites of the goal.  If some other rule is not depended on by the
695 goal (or anything it depends on, etc.), that rule is not processed,
696 unless you tell @code{make} to do so (with a command such as
697 @w{@code{make clean}}).
699 Before recompiling an object file, @code{make} considers updating its
700 prerequisites, the source file and header files.  This makefile does not
701 specify anything to be done for them---the @samp{.c} and @samp{.h} files
702 are not the targets of any rules---so @code{make} does nothing for these
703 files.  But @code{make} would update automatically generated C programs,
704 such as those made by Bison or Yacc, by their own rules at this time.
706 After recompiling whichever object files need it, @code{make} decides
707 whether to relink @file{edit}.  This must be done if the file
708 @file{edit} does not exist, or if any of the object files are newer than
709 it.  If an object file was just recompiled, it is now newer than
710 @file{edit}, so @file{edit} is relinked.
711 @cindex relinking
713 Thus, if we change the file @file{insert.c} and run @code{make},
714 @code{make} will compile that file to update @file{insert.o}, and then
715 link @file{edit}.  If we change the file @file{command.h} and run
716 @code{make}, @code{make} will recompile the object files @file{kbd.o},
717 @file{command.o} and @file{files.o} and then link the file @file{edit}.
719 @node Variables Simplify, make Deduces, How Make Works, Introduction
720 @section Variables Make Makefiles Simpler
721 @cindex variables
722 @cindex simplifying with variables
724 In our example, we had to list all the object files twice in the rule for
725 @file{edit} (repeated here):
727 @example
728 @group
729 edit : main.o kbd.o command.o display.o \
730               insert.o search.o files.o utils.o
731         cc -o edit main.o kbd.o command.o display.o \
732                    insert.o search.o files.o utils.o
733 @end group
734 @end example
736 @cindex @code{objects}
737 Such duplication is error-prone; if a new object file is added to the
738 system, we might add it to one list and forget the other.  We can eliminate
739 the risk and simplify the makefile by using a variable.  @dfn{Variables}
740 allow a text string to be defined once and substituted in multiple places
741 later (@pxref{Using Variables, ,How to Use Variables}).
743 @cindex @code{OBJECTS}
744 @cindex @code{objs}
745 @cindex @code{OBJS}
746 @cindex @code{obj}
747 @cindex @code{OBJ}
748 It is standard practice for every makefile to have a variable named
749 @code{objects}, @code{OBJECTS}, @code{objs}, @code{OBJS}, @code{obj},
750 or @code{OBJ} which is a list of all object file names.  We would
751 define such a variable @code{objects} with a line like this in the
752 makefile:@refill
754 @example
755 @group
756 objects = main.o kbd.o command.o display.o \
757           insert.o search.o files.o utils.o
758 @end group
759 @end example
761 @noindent
762 Then, each place we want to put a list of the object file names, we can
763 substitute the variable's value by writing @samp{$(objects)}
764 (@pxref{Using Variables, ,How to Use Variables}).
766 Here is how the complete simple makefile looks when you use a variable
767 for the object files:
769 @example
770 @group
771 objects = main.o kbd.o command.o display.o \
772           insert.o search.o files.o utils.o
774 edit : $(objects)
775         cc -o edit $(objects)
776 main.o : main.c defs.h
777         cc -c main.c
778 kbd.o : kbd.c defs.h command.h
779         cc -c kbd.c
780 command.o : command.c defs.h command.h
781         cc -c command.c
782 display.o : display.c defs.h buffer.h
783         cc -c display.c
784 insert.o : insert.c defs.h buffer.h
785         cc -c insert.c
786 search.o : search.c defs.h buffer.h
787         cc -c search.c
788 files.o : files.c defs.h buffer.h command.h
789         cc -c files.c
790 utils.o : utils.c defs.h
791         cc -c utils.c
792 clean :
793         rm edit $(objects)
794 @end group
795 @end example
797 @node make Deduces, Combine By Prerequisite, Variables Simplify, Introduction
798 @section Letting @code{make} Deduce the Recipes
799 @cindex deducing recipes (implicit rules)
800 @cindex implicit rule, introduction to
801 @cindex rule, implicit, introduction to
803 It is not necessary to spell out the recipes for compiling the individual
804 C source files, because @code{make} can figure them out: it has an
805 @dfn{implicit rule} for updating a @samp{.o} file from a correspondingly
806 named @samp{.c} file using a @samp{cc -c} command.  For example, it will
807 use the recipe @samp{cc -c main.c -o main.o} to compile @file{main.c} into
808 @file{main.o}.  We can therefore omit the recipes from the rules for the
809 object files.  @xref{Implicit Rules, ,Using Implicit Rules}.@refill
811 When a @samp{.c} file is used automatically in this way, it is also
812 automatically added to the list of prerequisites.  We can therefore omit
813 the @samp{.c} files from the prerequisites, provided we omit the recipe.
815 Here is the entire example, with both of these changes, and a variable
816 @code{objects} as suggested above:
818 @example
819 @group
820 objects = main.o kbd.o command.o display.o \
821           insert.o search.o files.o utils.o
823 edit : $(objects)
824         cc -o edit $(objects)
826 main.o : defs.h
827 kbd.o : defs.h command.h
828 command.o : defs.h command.h
829 display.o : defs.h buffer.h
830 insert.o : defs.h buffer.h
831 search.o : defs.h buffer.h
832 files.o : defs.h buffer.h command.h
833 utils.o : defs.h
835 .PHONY : clean
836 clean :
837         rm edit $(objects)
838 @end group
839 @end example
841 @noindent
842 This is how we would write the makefile in actual practice.  (The
843 complications associated with @samp{clean} are described elsewhere.
844 See @ref{Phony Targets}, and @ref{Errors, ,Errors in Recipes}.)
846 Because implicit rules are so convenient, they are important.  You
847 will see them used frequently.@refill
849 @node Combine By Prerequisite, Cleanup, make Deduces, Introduction
850 @section Another Style of Makefile
851 @cindex combining rules by prerequisite
853 When the objects of a makefile are created only by implicit rules, an
854 alternative style of makefile is possible.  In this style of makefile,
855 you group entries by their prerequisites instead of by their targets.
856 Here is what one looks like:
858 @example
859 @group
860 objects = main.o kbd.o command.o display.o \
861           insert.o search.o files.o utils.o
863 edit : $(objects)
864         cc -o edit $(objects)
866 $(objects) : defs.h
867 kbd.o command.o files.o : command.h
868 display.o insert.o search.o files.o : buffer.h
869 @end group
870 @end example
872 @noindent
873 Here @file{defs.h} is given as a prerequisite of all the object files;
874 @file{command.h} and @file{buffer.h} are prerequisites of the specific
875 object files listed for them.
877 Whether this is better is a matter of taste: it is more compact, but some
878 people dislike it because they find it clearer to put all the information
879 about each target in one place.
881 @node Cleanup,  , Combine By Prerequisite, Introduction
882 @section Rules for Cleaning the Directory
883 @cindex cleaning up
884 @cindex removing, to clean up
886 Compiling a program is not the only thing you might want to write rules
887 for.  Makefiles commonly tell how to do a few other things besides
888 compiling a program: for example, how to delete all the object files
889 and executables so that the directory is @samp{clean}.
891 @cindex @code{clean} target
892 Here is how we
893 could write a @code{make} rule for cleaning our example editor:
895 @example
896 @group
897 clean:
898         rm edit $(objects)
899 @end group
900 @end example
902 In practice, we might want to write the rule in a somewhat more
903 complicated manner to handle unanticipated situations.  We would do this:
905 @example
906 @group
907 .PHONY : clean
908 clean :
909         -rm edit $(objects)
910 @end group
911 @end example
913 @noindent
914 This prevents @code{make} from getting confused by an actual file
915 called @file{clean} and causes it to continue in spite of errors from
916 @code{rm}.  (See @ref{Phony Targets}, and @ref{Errors, ,Errors in
917 Recipes}.)
919 @noindent
920 A rule such as this should not be placed at the beginning of the
921 makefile, because we do not want it to run by default!  Thus, in the
922 example makefile, we want the rule for @code{edit}, which recompiles
923 the editor, to remain the default goal.
925 Since @code{clean} is not a prerequisite of @code{edit}, this rule will not
926 run at all if we give the command @samp{make} with no arguments.  In
927 order to make the rule run, we have to type @samp{make clean}.
928 @xref{Running, ,How to Run @code{make}}.
930 @node Makefiles, Rules, Introduction, Top
931 @chapter Writing Makefiles
933 @cindex makefile, how to write
934 The information that tells @code{make} how to recompile a system comes from
935 reading a data base called the @dfn{makefile}.
937 @menu
938 * Makefile Contents::           What makefiles contain.
939 * Makefile Names::              How to name your makefile.
940 * Include::                     How one makefile can use another makefile.
941 * MAKEFILES Variable::          The environment can specify extra makefiles.
942 * Remaking Makefiles::          How makefiles get remade.
943 * Overriding Makefiles::        How to override part of one makefile
944                                   with another makefile.
945 * Reading Makefiles::           How makefiles are parsed.
946 * Secondary Expansion::         How and when secondary expansion is performed.
947 @end menu
949 @node Makefile Contents, Makefile Names, Makefiles, Makefiles
950 @section What Makefiles Contain
952 Makefiles contain five kinds of things: @dfn{explicit rules},
953 @dfn{implicit rules}, @dfn{variable definitions}, @dfn{directives},
954 and @dfn{comments}.  Rules, variables, and directives are described at
955 length in later chapters.@refill
957 @itemize @bullet
958 @cindex rule, explicit, definition of
959 @cindex explicit rule, definition of
960 @item
961 An @dfn{explicit rule} says when and how to remake one or more files,
962 called the rule's @dfn{targets}.  It lists the other files that the
963 targets depend on, called the @dfn{prerequisites} of the target, and
964 may also give a recipe to use to create or update the targets.
965 @xref{Rules, ,Writing Rules}.
967 @cindex rule, implicit, definition of
968 @cindex implicit rule, definition of
969 @item
970 An @dfn{implicit rule} says when and how to remake a class of files
971 based on their names.  It describes how a target may depend on a file
972 with a name similar to the target and gives a recipe to create or
973 update such a target.  @xref{Implicit Rules, ,Using Implicit Rules}.
975 @cindex variable definition
976 @item
977 A @dfn{variable definition} is a line that specifies a text string
978 value for a variable that can be substituted into the text later.  The
979 simple makefile example shows a variable definition for @code{objects}
980 as a list of all object files (@pxref{Variables Simplify, , Variables
981 Make Makefiles Simpler}).
983 @cindex directive
984 @item
985 A @dfn{directive} is an instruction for @code{make} to do something
986 special while reading the makefile.  These include:
988 @itemize @bullet
989 @item
990 Reading another makefile (@pxref{Include, ,Including Other Makefiles}).
992 @item
993 Deciding (based on the values of variables) whether to use or
994 ignore a part of the makefile (@pxref{Conditionals, ,Conditional Parts of Makefiles}).
996 @item
997 Defining a variable from a verbatim string containing multiple lines
998 (@pxref{Defining, ,Defining Variables Verbatim}).
999 @end itemize
1001 @cindex comments, in makefile
1002 @cindex @code{#} (comments), in makefile
1003 @item
1004 @samp{#} in a line of a makefile starts a @dfn{comment}.  It and the
1005 rest of the line are ignored, except that a trailing backslash not
1006 escaped by another backslash will continue the comment across multiple
1007 lines.  A line containing just a comment (with perhaps spaces before
1008 it) is effectively blank, and is ignored.  If you want a literal
1009 @code{#}, escape it with a backslash (e.g., @code{\#}).  Comments may
1010 appear on any line in the makefile, although they are treated
1011 specially in certain situations.
1013 You cannot use comments within variable references or function calls:
1014 any instance of @code{#} will be treated literally (rather than as the
1015 start of a comment) inside a variable reference or function call.
1017 Comments within a recipe are passed to the shell, just as with any
1018 other recipe text.  The shell decides how to interpret it: whether or
1019 not this is a comment is up to the shell.
1021 Within a @code{define} directive, comments are not ignored during the
1022 definition of the variable, but rather kept intact in the value of the
1023 variable.  When the variable is expanded they will either be treated
1024 as @code{make} comments or as recipe text, depending on the context in
1025 which the variable is evaluated.
1026 @end itemize
1028 @node Makefile Names, Include, Makefile Contents, Makefiles
1029 @section What Name to Give Your Makefile
1030 @cindex makefile name
1031 @cindex name of makefile
1032 @cindex default makefile name
1033 @cindex file name of makefile
1035 @c following paragraph rewritten to avoid overfull hbox
1036 By default, when @code{make} looks for the makefile, it tries the
1037 following names, in order: @file{GNUmakefile}, @file{makefile}
1038 and @file{Makefile}.@refill
1039 @findex Makefile
1040 @findex GNUmakefile
1041 @findex makefile
1043 @cindex @code{README}
1044 Normally you should call your makefile either @file{makefile} or
1045 @file{Makefile}.  (We recommend @file{Makefile} because it appears
1046 prominently near the beginning of a directory listing, right near other
1047 important files such as @file{README}.)  The first name checked,
1048 @file{GNUmakefile}, is not recommended for most makefiles.  You should
1049 use this name if you have a makefile that is specific to GNU
1050 @code{make}, and will not be understood by other versions of
1051 @code{make}.  Other @code{make} programs look for @file{makefile} and
1052 @file{Makefile}, but not @file{GNUmakefile}.
1054 If @code{make} finds none of these names, it does not use any makefile.
1055 Then you must specify a goal with a command argument, and @code{make}
1056 will attempt to figure out how to remake it using only its built-in
1057 implicit rules.  @xref{Implicit Rules, ,Using Implicit Rules}.
1059 @cindex @code{-f}
1060 @cindex @code{--file}
1061 @cindex @code{--makefile}
1062 If you want to use a nonstandard name for your makefile, you can specify
1063 the makefile name with the @samp{-f} or @samp{--file} option.  The
1064 arguments @w{@samp{-f @var{name}}} or @w{@samp{--file=@var{name}}} tell
1065 @code{make} to read the file @var{name} as the makefile.  If you use
1066 more than one @samp{-f} or @samp{--file} option, you can specify several
1067 makefiles.  All the makefiles are effectively concatenated in the order
1068 specified.  The default makefile names @file{GNUmakefile},
1069 @file{makefile} and @file{Makefile} are not checked automatically if you
1070 specify @samp{-f} or @samp{--file}.@refill
1071 @cindex specifying makefile name
1072 @cindex makefile name, how to specify
1073 @cindex name of makefile, how to specify
1074 @cindex file name of makefile, how to specify
1076 @node Include, MAKEFILES Variable, Makefile Names, Makefiles
1077 @section Including Other Makefiles
1078 @cindex including other makefiles
1079 @cindex makefile, including
1081 @findex include
1082 The @code{include} directive tells @code{make} to suspend reading the
1083 current makefile and read one or more other makefiles before continuing.
1084 The directive is a line in the makefile that looks like this:
1086 @example
1087 include @var{filenames}@dots{}
1088 @end example
1090 @noindent
1091 @var{filenames} can contain shell file name patterns.  If
1092 @var{filenames} is empty, nothing is included and no error is printed.
1093 @cindex shell file name pattern (in @code{include})
1094 @cindex shell wildcards (in @code{include})
1095 @cindex wildcard, in @code{include}
1097 Extra spaces are allowed and ignored at the beginning of the line, but
1098 the first character must not be a tab (or the value of
1099 @code{.CMDPREFIX})---if the line begins with a tab, it will be
1100 considered a recipe line.  Whitespace is required between
1101 @code{include} and the file names, and between file names; extra
1102 whitespace is ignored there and at the end of the directive.  A
1103 comment starting with @samp{#} is allowed at the end of the line.  If
1104 the file names contain any variable or function references, they are
1105 expanded.  @xref{Using Variables, ,How to Use Variables}.
1107 For example, if you have three @file{.mk} files, @file{a.mk},
1108 @file{b.mk}, and @file{c.mk}, and @code{$(bar)} expands to
1109 @code{bish bash}, then the following expression
1111 @example
1112 include foo *.mk $(bar)
1113 @end example
1115 is equivalent to
1117 @example
1118 include foo a.mk b.mk c.mk bish bash
1119 @end example
1121 When @code{make} processes an @code{include} directive, it suspends
1122 reading of the containing makefile and reads from each listed file in
1123 turn.  When that is finished, @code{make} resumes reading the
1124 makefile in which the directive appears.
1126 One occasion for using @code{include} directives is when several programs,
1127 handled by individual makefiles in various directories, need to use a
1128 common set of variable definitions
1129 (@pxref{Setting, ,Setting Variables}) or pattern rules
1130 (@pxref{Pattern Rules, ,Defining and Redefining Pattern Rules}).
1132 Another such occasion is when you want to generate prerequisites from
1133 source files automatically; the prerequisites can be put in a file that
1134 is included by the main makefile.  This practice is generally cleaner
1135 than that of somehow appending the prerequisites to the end of the main
1136 makefile as has been traditionally done with other versions of
1137 @code{make}.  @xref{Automatic Prerequisites}.
1138 @cindex prerequisites, automatic generation
1139 @cindex automatic generation of prerequisites
1140 @cindex generating prerequisites automatically
1142 @cindex @code{-I}
1143 @cindex @code{--include-dir}
1144 @cindex included makefiles, default directories
1145 @cindex default directories for included makefiles
1146 @findex /usr/gnu/include
1147 @findex /usr/local/include
1148 @findex /usr/include
1149 If the specified name does not start with a slash, and the file is not
1150 found in the current directory, several other directories are searched.
1151 First, any directories you have specified with the @samp{-I} or
1152 @samp{--include-dir} option are searched
1153 (@pxref{Options Summary, ,Summary of Options}).
1154 Then the following directories (if they exist)
1155 are searched, in this order:
1156 @file{@var{prefix}/include} (normally @file{/usr/local/include}
1157 @footnote{GNU Make compiled for MS-DOS and MS-Windows behaves as if
1158 @var{prefix} has been defined to be the root of the DJGPP tree
1159 hierarchy.})
1160 @file{/usr/gnu/include},
1161 @file{/usr/local/include}, @file{/usr/include}.
1163 If an included makefile cannot be found in any of these directories, a
1164 warning message is generated, but it is not an immediately fatal error;
1165 processing of the makefile containing the @code{include} continues.
1166 Once it has finished reading makefiles, @code{make} will try to remake
1167 any that are out of date or don't exist.
1168 @xref{Remaking Makefiles, ,How Makefiles Are Remade}.
1169 Only after it has tried to find a way to remake a makefile and failed,
1170 will @code{make} diagnose the missing makefile as a fatal error.
1172 If you want @code{make} to simply ignore a makefile which does not exist
1173 and cannot be remade, with no error message, use the @w{@code{-include}}
1174 directive instead of @code{include}, like this:
1176 @example
1177 -include @var{filenames}@dots{}
1178 @end example
1180 This acts like @code{include} in every way except that there is no
1181 error (not even a warning) if any of the @var{filenames} do not exist.
1182 For compatibility with some other @code{make} implementations,
1183 @code{sinclude} is another name for @w{@code{-include}}.
1185 @node MAKEFILES Variable, Remaking Makefiles, Include, Makefiles
1186 @section The Variable @code{MAKEFILES}
1187 @cindex makefile, and @code{MAKEFILES} variable
1188 @cindex including (@code{MAKEFILES} variable)
1190 @vindex MAKEFILES
1191 If the environment variable @code{MAKEFILES} is defined, @code{make}
1192 considers its value as a list of names (separated by whitespace) of
1193 additional makefiles to be read before the others.  This works much like
1194 the @code{include} directive: various directories are searched for those
1195 files (@pxref{Include, ,Including Other Makefiles}).  In addition, the
1196 default goal is never taken from one of these makefiles and it is not an
1197 error if the files listed in @code{MAKEFILES} are not found.@refill
1199 @cindex recursion, and @code{MAKEFILES} variable
1200 The main use of @code{MAKEFILES} is in communication between recursive
1201 invocations of @code{make} (@pxref{Recursion, ,Recursive Use of
1202 @code{make}}).  It usually is not desirable to set the environment
1203 variable before a top-level invocation of @code{make}, because it is
1204 usually better not to mess with a makefile from outside.  However, if
1205 you are running @code{make} without a specific makefile, a makefile in
1206 @code{MAKEFILES} can do useful things to help the built-in implicit
1207 rules work better, such as defining search paths (@pxref{Directory Search}).
1209 Some users are tempted to set @code{MAKEFILES} in the environment
1210 automatically on login, and program makefiles to expect this to be done.
1211 This is a very bad idea, because such makefiles will fail to work if run by
1212 anyone else.  It is much better to write explicit @code{include} directives
1213 in the makefiles.  @xref{Include, , Including Other Makefiles}.
1215 @node Remaking Makefiles, Overriding Makefiles, MAKEFILES Variable, Makefiles
1216 @section How Makefiles Are Remade
1218 @cindex updating makefiles
1219 @cindex remaking makefiles
1220 @cindex makefile, remaking of
1221 Sometimes makefiles can be remade from other files, such as RCS or SCCS
1222 files.  If a makefile can be remade from other files, you probably want
1223 @code{make} to get an up-to-date version of the makefile to read in.
1225 To this end, after reading in all makefiles, @code{make} will consider
1226 each as a goal target and attempt to update it.  If a makefile has a
1227 rule which says how to update it (found either in that very makefile or
1228 in another one) or if an implicit rule applies to it (@pxref{Implicit
1229 Rules, ,Using Implicit Rules}), it will be updated if necessary.  After
1230 all makefiles have been checked, if any have actually been changed,
1231 @code{make} starts with a clean slate and reads all the makefiles over
1232 again.  (It will also attempt to update each of them over again, but
1233 normally this will not change them again, since they are already up to
1234 date.)@refill
1236 If you know that one or more of your makefiles cannot be remade and
1237 you want to keep @code{make} from performing an implicit rule search
1238 on them, perhaps for efficiency reasons, you can use any normal method
1239 of preventing implicit rule lookup to do so.  For example, you can
1240 write an explicit rule with the makefile as the target, and an empty
1241 recipe (@pxref{Empty Recipes, ,Using Empty Recipes}).
1243 If the makefiles specify a double-colon rule to remake a file with
1244 a recipe but no prerequisites, that file will always be remade
1245 (@pxref{Double-Colon}).  In the case of makefiles, a makefile that has a
1246 double-colon rule with a recipe but no prerequisites will be remade every
1247 time @code{make} is run, and then again after @code{make} starts over
1248 and reads the makefiles in again.  This would cause an infinite loop:
1249 @code{make} would constantly remake the makefile, and never do anything
1250 else.  So, to avoid this, @code{make} will @strong{not} attempt to
1251 remake makefiles which are specified as targets of a double-colon rule
1252 with a recipe but no prerequisites.@refill
1254 If you do not specify any makefiles to be read with @samp{-f} or
1255 @samp{--file} options, @code{make} will try the default makefile names;
1256 @pxref{Makefile Names, ,What Name to Give Your Makefile}.  Unlike
1257 makefiles explicitly requested with @samp{-f} or @samp{--file} options,
1258 @code{make} is not certain that these makefiles should exist.  However,
1259 if a default makefile does not exist but can be created by running
1260 @code{make} rules, you probably want the rules to be run so that the
1261 makefile can be used.
1263 Therefore, if none of the default makefiles exists, @code{make} will try
1264 to make each of them in the same order in which they are searched for
1265 (@pxref{Makefile Names, ,What Name to Give Your Makefile})
1266 until it succeeds in making one, or it runs out of names to try.  Note
1267 that it is not an error if @code{make} cannot find or make any makefile;
1268 a makefile is not always necessary.@refill
1270 When you use the @samp{-t} or @samp{--touch} option
1271 (@pxref{Instead of Execution, ,Instead of Executing the Recipe}),
1272 you would not want to use an out-of-date makefile to decide which
1273 targets to touch.  So the @samp{-t} option has no effect on updating
1274 makefiles; they are really updated even if @samp{-t} is specified.
1275 Likewise, @samp{-q} (or @samp{--question}) and @samp{-n} (or
1276 @samp{--just-print}) do not prevent updating of makefiles, because an
1277 out-of-date makefile would result in the wrong output for other targets.
1278 Thus, @samp{make -f mfile -n foo} will update @file{mfile}, read it in,
1279 and then print the recipe to update @file{foo} and its prerequisites
1280 without running it.  The recipe printed for @file{foo} will be the one
1281 specified in the updated contents of @file{mfile}.
1283 However, on occasion you might actually wish to prevent updating of even
1284 the makefiles.  You can do this by specifying the makefiles as goals in
1285 the command line as well as specifying them as makefiles.  When the
1286 makefile name is specified explicitly as a goal, the options @samp{-t}
1287 and so on do apply to them.
1289 Thus, @samp{make -f mfile -n mfile foo} would read the makefile
1290 @file{mfile}, print the recipe needed to update it without actually
1291 running it, and then print the recipe needed to update @file{foo}
1292 without running that.  The recipe for @file{foo} will be the one
1293 specified by the existing contents of @file{mfile}.
1295 @node Overriding Makefiles, Reading Makefiles, Remaking Makefiles, Makefiles
1296 @section Overriding Part of Another Makefile
1298 @cindex overriding makefiles
1299 @cindex makefile, overriding
1300 Sometimes it is useful to have a makefile that is mostly just like
1301 another makefile.  You can often use the @samp{include} directive to
1302 include one in the other, and add more targets or variable definitions.
1303 However, it is illegal for two makefiles to give different recipes for
1304 the same target.  But there is another way.
1306 @cindex match-anything rule, used to override
1307 In the containing makefile (the one that wants to include the other),
1308 you can use a match-anything pattern rule to say that to remake any
1309 target that cannot be made from the information in the containing
1310 makefile, @code{make} should look in another makefile.
1311 @xref{Pattern Rules}, for more information on pattern rules.
1313 For example, if you have a makefile called @file{Makefile} that says how
1314 to make the target @samp{foo} (and other targets), you can write a
1315 makefile called @file{GNUmakefile} that contains:
1317 @example
1318 foo:
1319         frobnicate > foo
1321 %: force
1322         @@$(MAKE) -f Makefile $@@
1323 force: ;
1324 @end example
1326 If you say @samp{make foo}, @code{make} will find @file{GNUmakefile},
1327 read it, and see that to make @file{foo}, it needs to run the recipe
1328 @samp{frobnicate > foo}.  If you say @samp{make bar}, @code{make} will
1329 find no way to make @file{bar} in @file{GNUmakefile}, so it will use the
1330 recipe from the pattern rule: @samp{make -f Makefile bar}.  If
1331 @file{Makefile} provides a rule for updating @file{bar}, @code{make}
1332 will apply the rule.  And likewise for any other target that
1333 @file{GNUmakefile} does not say how to make.
1335 The way this works is that the pattern rule has a pattern of just
1336 @samp{%}, so it matches any target whatever.  The rule specifies a
1337 prerequisite @file{force}, to guarantee that the recipe will be run even
1338 if the target file already exists.  We give the @file{force} target an
1339 empty recipe to prevent @code{make} from searching for an implicit rule to
1340 build it---otherwise it would apply the same match-anything rule to
1341 @file{force} itself and create a prerequisite loop!
1343 @node Reading Makefiles,  Secondary Expansion, Overriding Makefiles, Makefiles
1344 @section How @code{make} Reads a Makefile
1345 @cindex reading makefiles
1346 @cindex makefile, parsing
1348 GNU @code{make} does its work in two distinct phases.  During the first
1349 phase it reads all the makefiles, included makefiles, etc. and
1350 internalizes all the variables and their values, implicit and explicit
1351 rules, and constructs a dependency graph of all the targets and their
1352 prerequisites.  During the second phase, @code{make} uses these internal
1353 structures to determine what targets will need to be rebuilt and to
1354 invoke the rules necessary to do so.
1356 It's important to understand this two-phase approach because it has a
1357 direct impact on how variable and function expansion happens; this is
1358 often a source of some confusion when writing makefiles.  Here we will
1359 present a summary of the phases in which expansion happens for different
1360 constructs within the makefile.  We say that expansion is
1361 @dfn{immediate} if it happens during the first phase: in this case
1362 @code{make} will expand any variables or functions in that section of a
1363 construct as the makefile is parsed.  We say that expansion is
1364 @dfn{deferred} if expansion is not performed immediately.  Expansion of
1365 a deferred construct is not performed until either the construct appears
1366 later in an immediate context, or until the second phase.
1368 You may not be familiar with some of these constructs yet.  You can
1369 reference this section as you become familiar with them, in later
1370 chapters.
1372 @subheading Variable Assignment
1373 @cindex +=, expansion
1374 @cindex =, expansion
1375 @cindex ?=, expansion
1376 @cindex +=, expansion
1377 @cindex define, expansion
1379 Variable definitions are parsed as follows:
1381 @example
1382 @var{immediate} = @var{deferred}
1383 @var{immediate} ?= @var{deferred}
1384 @var{immediate} := @var{immediate}
1385 @var{immediate} += @var{deferred} or @var{immediate}
1387 define @var{immediate}
1388   @var{deferred}
1389 endef
1390 @end example
1392 For the append operator, @samp{+=}, the right-hand side is considered
1393 immediate if the variable was previously set as a simple variable
1394 (@samp{:=}), and deferred otherwise.
1396 @subheading Conditional Directives
1397 @cindex ifdef, expansion
1398 @cindex ifeq, expansion
1399 @cindex ifndef, expansion
1400 @cindex ifneq, expansion
1402 Conditional directives are parsed immediately.  This means, for
1403 example, that automatic variables cannot be used in conditional
1404 directives, as automatic variables are not set until the recipe for
1405 that rule is invoked.  If you need to use automatic variables in a
1406 conditional directive you @emph{must} move the condition into the
1407 recipe and use shell conditional syntax instead.
1409 @subheading Rule Definition
1410 @cindex target, expansion
1411 @cindex prerequisite, expansion
1412 @cindex implicit rule, expansion
1413 @cindex pattern rule, expansion
1414 @cindex explicit rule, expansion
1416 A rule is always expanded the same way, regardless of the form:
1418 @example
1419 @var{immediate} : @var{immediate} ; @var{deferred}
1420         @var{deferred}
1421 @end example
1423 That is, the target and prerequisite sections are expanded immediately,
1424 and the recipe used to construct the target is always deferred.  This
1425 general rule is true for explicit rules, pattern rules, suffix rules,
1426 static pattern rules, and simple prerequisite definitions.
1428 @node Secondary Expansion, , Reading Makefiles, Makefiles
1429 @section Secondary Expansion
1430 @cindex secondary expansion
1431 @cindex expansion, secondary
1433 @findex .SECONDEXPANSION
1434 In the previous section we learned that GNU @code{make} works in two
1435 distinct phases: a read-in phase and a target-update phase
1436 (@pxref{Reading Makefiles, , How @code{make} Reads a Makefile}).  GNU
1437 make also has the ability to enable a @emph{second expansion} of the
1438 prerequisites (only) for some or all targets defined in the makefile.
1439 In order for this second expansion to occur, the special target
1440 @code{.SECONDEXPANSION} must be defined before the first prerequisite
1441 list that makes use of this feature.
1443 If that special target is defined then in between the two phases
1444 mentioned above, right at the end of the read-in phase, all the
1445 prerequisites of the targets defined after the special target are
1446 expanded a @emph{second time}.  In most circumstances this secondary
1447 expansion will have no effect, since all variable and function
1448 references will have been expanded during the initial parsing of the
1449 makefiles.  In order to take advantage of the secondary expansion
1450 phase of the parser, then, it's necessary to @emph{escape} the
1451 variable or function reference in the makefile.  In this case the
1452 first expansion merely un-escapes the reference but doesn't expand it,
1453 and expansion is left to the secondary expansion phase.  For example,
1454 consider this makefile:
1456 @example
1457 .SECONDEXPANSION:
1458 ONEVAR = onefile
1459 TWOVAR = twofile
1460 myfile: $(ONEVAR) $$(TWOVAR)
1461 @end example
1463 After the first expansion phase the prerequisites list of the
1464 @file{myfile} target will be @code{onefile} and @code{$(TWOVAR)}; the
1465 first (unescaped) variable reference to @var{ONEVAR} is expanded,
1466 while the second (escaped) variable reference is simply unescaped,
1467 without being recognized as a variable reference.  Now during the
1468 secondary expansion the first word is expanded again but since it
1469 contains no variable or function references it remains the static
1470 value @file{onefile}, while the second word is now a normal reference
1471 to the variable @var{TWOVAR}, which is expanded to the value
1472 @file{twofile}.  The final result is that there are two prerequisites,
1473 @file{onefile} and @file{twofile}.
1475 Obviously, this is not a very interesting case since the same result
1476 could more easily have been achieved simply by having both variables
1477 appear, unescaped, in the prerequisites list.  One difference becomes
1478 apparent if the variables are reset; consider this example:
1480 @example
1481 .SECONDEXPANSION:
1482 AVAR = top
1483 onefile: $(AVAR)
1484 twofile: $$(AVAR)
1485 AVAR = bottom
1486 @end example
1488 Here the prerequisite of @file{onefile} will be expanded immediately,
1489 and resolve to the value @file{top}, while the prerequisite of
1490 @file{twofile} will not be full expanded until the secondary expansion
1491 and yield a value of @file{bottom}.
1493 This is marginally more exciting, but the true power of this feature
1494 only becomes apparent when you discover that secondary expansions
1495 always take place within the scope of the automatic variables for that
1496 target.  This means that you can use variables such as @code{$@@},
1497 @code{$*}, etc. during the second expansion and they will have their
1498 expected values, just as in the recipe.  All you have to do is defer
1499 the expansion by escaping the @code{$}.  Also, secondary expansion
1500 occurs for both explicit and implicit (pattern) rules.  Knowing this,
1501 the possible uses for this feature increase dramatically.  For
1502 example:
1504 @example
1505 .SECONDEXPANSION:
1506 main_OBJS := main.o try.o test.o
1507 lib_OBJS := lib.o api.o
1509 main lib: $$($$@@_OBJS)
1510 @end example
1512 Here, after the initial expansion the prerequisites of both the
1513 @file{main} and @file{lib} targets will be @code{$($@@_OBJS)}.  During
1514 the secondary expansion, the @code{$@@} variable is set to the name of
1515 the target and so the expansion for the @file{main} target will yield
1516 @code{$(main_OBJS)}, or @code{main.o try.o test.o}, while the
1517 secondary expansion for the @file{lib} target will yield
1518 @code{$(lib_OBJS)}, or @code{lib.o api.o}.
1520 You can also mix in functions here, as long as they are properly escaped:
1522 @example
1523 main_SRCS := main.c try.c test.c
1524 lib_SRCS := lib.c api.c
1526 .SECONDEXPANSION:
1527 main lib: $$(patsubst %.c,%.o,$$($$@@_SRCS))
1528 @end example
1530 This version allows users to specify source files rather than object
1531 files, but gives the same resulting prerequisites list as the previous
1532 example.
1534 Evaluation of automatic variables during the secondary expansion
1535 phase, especially of the target name variable @code{$$@@}, behaves
1536 similarly to evaluation within recipes.  However, there are some
1537 subtle differences and ``corner cases'' which come into play for the
1538 different types of rule definitions that @code{make} understands.  The
1539 subtleties of using the different automatic variables are described
1540 below.
1542 @subheading Secondary Expansion of Explicit Rules
1543 @cindex secondary expansion and explicit rules
1544 @cindex explicit rules, secondary expansion of
1546 During the secondary expansion of explicit rules, @code{$$@@} and
1547 @code{$$%} evaluate, respectively, to the file name of the target and,
1548 when the target is an archive member, the target member name.  The
1549 @code{$$<} variable evaluates to the first prerequisite in the first
1550 rule for this target.  @code{$$^} and @code{$$+} evaluate to the list
1551 of all prerequisites of rules @emph{that have already appeared} for
1552 the same target (@code{$$+} with repetitions and @code{$$^}
1553 without).  The following example will help illustrate these behaviors:
1555 @example
1556 .SECONDEXPANSION:
1558 foo: foo.1 bar.1 $$< $$^ $$+    # line #1
1560 foo: foo.2 bar.2 $$< $$^ $$+    # line #2
1562 foo: foo.3 bar.3 $$< $$^ $$+    # line #3
1563 @end example
1565 In the first prerequisite list, all three variables (@code{$$<},
1566 @code{$$^}, and @code{$$+}) expand to the empty string.  In the
1567 second, they will have values @code{foo.1}, @code{foo.1 bar.1}, and
1568 @code{foo.1 bar.1} respectively.  In the third they will have values
1569 @code{foo.1}, @code{foo.1 bar.1 foo.2 bar.2}, and @code{foo.1 bar.1
1570 foo.2 bar.2 foo.1 foo.1 bar.1 foo.1 bar.1} respectively.
1572 Rules undergo secondary expansion in makefile order, except that
1573 the rule with the recipe is always evaluated last.
1575 The variables @code{$$?} and @code{$$*} are not available and expand
1576 to the empty string.
1578 @subheading Secondary Expansion of Static Pattern Rules
1579 @cindex secondary expansion and static pattern rules
1580 @cindex static pattern rules, secondary expansion of
1582 Rules for secondary expansion of static pattern rules are identical to
1583 those for explicit rules, above, with one exception: for static
1584 pattern rules the @code{$$*} variable is set to the pattern stem.  As
1585 with explicit rules, @code{$$?} is not available and expands to the
1586 empty string.
1588 @subheading Secondary Expansion of Implicit Rules
1589 @cindex secondary expansion and implicit rules
1590 @cindex implicit rules, secondary expansion of
1592 As @code{make} searches for an implicit rule, it substitutes the stem
1593 and then performs secondary expansion for every rule with a matching
1594 target pattern.  The value of the automatic variables is derived in
1595 the same fashion as for static pattern rules.  As an example:
1597 @example
1598 .SECONDEXPANSION:
1600 foo: bar
1602 foo foz: fo%: bo%
1604 %oo: $$< $$^ $$+ $$*
1605 @end example
1607 When the implicit rule is tried for target @file{foo}, @code{$$<}
1608 expands to @file{bar}, @code{$$^} expands to @file{bar boo},
1609 @code{$$+} also expands to @file{bar boo}, and @code{$$*} expands to
1610 @file{f}.
1612 Note that the directory prefix (D), as described in @ref{Implicit Rule
1613 Search, ,Implicit Rule Search Algorithm}, is appended (after
1614 expansion) to all the patterns in the prerequisites list.  As an
1615 example:
1617 @example
1618 .SECONDEXPANSION:
1620 /tmp/foo.o:
1622 %.o: $$(addsuffix /%.c,foo bar) foo.h
1623 @end example
1625 The prerequisite list after the secondary expansion and directory
1626 prefix reconstruction will be @file{/tmp/foo/foo.c /tmp/var/bar/foo.c
1627 foo.h}.  If you are not interested in this reconstruction, you can use
1628 @code{$$*} instead of @code{%} in the prerequisites list.
1630 @node Rules, Recipes, Makefiles, Top
1631 @chapter Writing Rules
1632 @cindex writing rules
1633 @cindex rule, how to write
1634 @cindex target
1635 @cindex prerequisite
1637 A @dfn{rule} appears in the makefile and says when and how to remake
1638 certain files, called the rule's @dfn{targets} (most often only one per rule).
1639 It lists the other files that are the @dfn{prerequisites} of the target, and
1640 the @dfn{recipe} to use to create or update the target.
1642 @cindex default goal
1643 @cindex goal, default
1644 The order of rules is not significant, except for determining the
1645 @dfn{default goal}: the target for @code{make} to consider, if you do
1646 not otherwise specify one.  The default goal is the target of the first
1647 rule in the first makefile.  If the first rule has multiple targets,
1648 only the first target is taken as the default.  There are two
1649 exceptions: a target starting with a period is not a default unless it
1650 contains one or more slashes, @samp{/}, as well; and, a target that
1651 defines a pattern rule has no effect on the default goal.
1652 (@xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.)
1654 Therefore, we usually write the makefile so that the first rule is the
1655 one for compiling the entire program or all the programs described by
1656 the makefile (often with a target called @samp{all}).
1657 @xref{Goals, ,Arguments to Specify the Goals}.
1659 @menu
1660 * Rule Example::                An example explained.
1661 * Rule Syntax::                 General syntax explained.
1662 * Prerequisite Types::          There are two types of prerequisites.
1663 * Wildcards::                   Using wildcard characters such as `*'.
1664 * Directory Search::            Searching other directories for source files.
1665 * Phony Targets::               Using a target that is not a real file's name.
1666 * Force Targets::               You can use a target without recipes
1667                                   or prerequisites to mark other targets
1668                                   as phony.
1669 * Empty Targets::               When only the date matters and the
1670                                   files are empty.
1671 * Special Targets::             Targets with special built-in meanings.
1672 * Multiple Targets::            When to make use of several targets in a rule.
1673 * Multiple Rules::              How to use several rules with the same target.
1674 * Static Pattern::              Static pattern rules apply to multiple targets
1675                                   and can vary the prerequisites according to
1676                                   the target name.
1677 * Double-Colon::                How to use a special kind of rule to allow
1678                                   several independent rules for one target.
1679 * Automatic Prerequisites::     How to automatically generate rules giving
1680                                   prerequisites from source files themselves.
1681 @end menu
1683 @ifnottex
1684 @node Rule Example, Rule Syntax, Rules, Rules
1685 @section Rule Example
1687 Here is an example of a rule:
1689 @example
1690 foo.o : foo.c defs.h       # module for twiddling the frobs
1691         cc -c -g foo.c
1692 @end example
1694 Its target is @file{foo.o} and its prerequisites are @file{foo.c} and
1695 @file{defs.h}.  It has one command in the recipe: @samp{cc -c -g foo.c}.
1696 The recipe starts with a tab to identify it as a recipe.
1698 This rule says two things:
1700 @itemize @bullet
1701 @item
1702 How to decide whether @file{foo.o} is out of date: it is out of date
1703 if it does not exist, or if either @file{foo.c} or @file{defs.h} is
1704 more recent than it.
1706 @item
1707 How to update the file @file{foo.o}: by running @code{cc} as stated.
1708 The recipe does not explicitly mention @file{defs.h}, but we presume
1709 that @file{foo.c} includes it, and that that is why @file{defs.h} was
1710 added to the prerequisites.
1711 @end itemize
1712 @end ifnottex
1714 @node Rule Syntax, Prerequisite Types, Rule Example, Rules
1715 @section Rule Syntax
1717 @cindex rule syntax
1718 @cindex syntax of rules
1719 In general, a rule looks like this:
1721 @example
1722 @var{targets} : @var{prerequisites}
1723         @var{recipe}
1724         @dots{}
1725 @end example
1727 @noindent
1728 or like this:
1730 @example
1731 @var{targets} : @var{prerequisites} ; @var{recipe}
1732         @var{recipe}
1733         @dots{}
1734 @end example
1736 @cindex targets
1737 @cindex rule targets
1738 The @var{targets} are file names, separated by spaces.  Wildcard
1739 characters may be used (@pxref{Wildcards, ,Using Wildcard Characters
1740 in File Names}) and a name of the form @file{@var{a}(@var{m})}
1741 represents member @var{m} in archive file @var{a}
1742 (@pxref{Archive Members, ,Archive Members as Targets}).
1743 Usually there is only one
1744 target per rule, but occasionally there is a reason to have more
1745 (@pxref{Multiple Targets, , Multiple Targets in a Rule}).@refill
1747 @cindex recipes
1748 @cindex tab character (in commands)
1749 The @var{recipe} lines start with a tab character (or the first
1750 character in the value of the @code{.CMDPREFIX} variable;
1751 @pxref{Special Variables}).  The first recipe line may appear on the line
1752 after the prerequisites, with a tab character, or may appear on the
1753 same line, with a semicolon.  Either way, the effect is the same.
1754 There are other differences in the syntax of recipes.
1755 @xref{Recipes, ,Writing Recipes in Rules}.
1757 @cindex dollar sign (@code{$}), in rules
1758 @cindex @code{$}, in rules
1759 @cindex rules, and @code{$}
1760 Because dollar signs are used to start @code{make} variable
1761 references, if you really want a dollar sign in a target or
1762 prerequisite you must write two of them, @samp{$$} (@pxref{Using
1763 Variables, ,How to Use Variables}).  If you have enabled secondary
1764 expansion (@pxref{Secondary Expansion}) and you want a literal dollar
1765 sign in the prerequisites list, you must actually write @emph{four}
1766 dollar signs (@samp{$$$$}).
1768 You may split a long line by inserting a backslash followed by a
1769 newline, but this is not required, as @code{make} places no limit on
1770 the length of a line in a makefile.
1772 A rule tells @code{make} two things: when the targets are out of date,
1773 and how to update them when necessary.
1775 @cindex prerequisites
1776 @cindex rule prerequisites
1777 The criterion for being out of date is specified in terms of the
1778 @var{prerequisites}, which consist of file names separated by spaces.
1779 (Wildcards and archive members (@pxref{Archives}) are allowed here too.)
1780 A target is out of date if it does not exist or if it is older than any
1781 of the prerequisites (by comparison of last-modification times).  The
1782 idea is that the contents of the target file are computed based on
1783 information in the prerequisites, so if any of the prerequisites changes,
1784 the contents of the existing target file are no longer necessarily
1785 valid.
1787 How to update is specified by a @var{recipe}.  This is one or more
1788 lines to be executed by the shell (normally @samp{sh}), but with some
1789 extra features (@pxref{Recipes, ,Writing Recipes in Rules}).
1791 @node Prerequisite Types, Wildcards, Rule Syntax, Rules
1792 @comment  node-name,  next,  previous,  up
1793 @section Types of Prerequisites
1794 @cindex prerequisite types
1795 @cindex types of prerequisites
1797 @cindex prerequisites, normal
1798 @cindex normal prerequisites
1799 @cindex prerequisites, order-only
1800 @cindex order-only prerequisites
1801 There are actually two different types of prerequisites understood by
1802 GNU @code{make}: normal prerequisites such as described in the
1803 previous section, and @dfn{order-only} prerequisites.  A normal
1804 prerequisite makes two statements: first, it imposes an order of
1805 execution of recipes: any recipes necessary to build any of a
1806 target's prerequisites will be fully executed before any recipe
1807 necessary to build the target.  Second, it imposes a dependency
1808 relationship: if any prerequisite is newer than the target, then the
1809 target is considered out-of-date and must be rebuilt.
1811 Normally, this is exactly what you want: if a target's prerequisite is
1812 updated, then the target should also be updated.
1814 Occasionally, however, you have a situation where you want to impose a
1815 specific ordering on the rules to be invoked @emph{without} forcing
1816 the target to be updated if one of those rules is executed.  In that
1817 case, you want to define @dfn{order-only} prerequisites.  Order-only
1818 prerequisites can be specified by placing a pipe symbol (@code{|})
1819 in the prerequisites list: any prerequisites to the left of the pipe
1820 symbol are normal; any prerequisites to the right are order-only:
1822 @example
1823 @var{targets} : @var{normal-prerequisites} | @var{order-only-prerequisites}
1824 @end example
1826 The normal prerequisites section may of course be empty.  Also, you
1827 may still declare multiple lines of prerequisites for the same target:
1828 they are appended appropriately (normal prerequisites are appended to
1829 the list of normal prerequisites; order-only prerequisites are
1830 appended to the list of order-only prerequisites).  Note that if you
1831 declare the same file to be both a normal and an order-only
1832 prerequisite, the normal prerequisite takes precedence (since they
1833 have a strict superset of the behavior of an order-only prerequisite).
1835 Consider an example where your targets are to be placed in a separate
1836 directory, and that directory might not exist before @code{make} is
1837 run.  In this situation, you want the directory to be created before
1838 any targets are placed into it but, because the timestamps on
1839 directories change whenever a file is added, removed, or renamed, we
1840 certainly don't want to rebuild all the targets whenever the
1841 directory's timestamp changes.  One way to manage this is with
1842 order-only prerequisites: make the directory an order-only
1843 prerequisite on all the targets:
1845 @example
1846 OBJDIR := objdir
1847 OBJS := $(addprefix $(OBJDIR)/,foo.o bar.o baz.o)
1849 $(OBJDIR)/%.o : %.c
1850         $(COMPILE.c) $(OUTPUT_OPTION) $<
1852 all: $(OBJS)
1854 $(OBJS): | $(OBJDIR)
1856 $(OBJDIR):
1857         mkdir $(OBJDIR)
1858 @end example
1860 Now the rule to create the @file{objdir} directory will be run, if
1861 needed, before any @samp{.o} is built, but no @samp{.o} will be built
1862 because the @file{objdir} directory timestamp changed.
1864 @node Wildcards, Directory Search, Prerequisite Types, Rules
1865 @section Using Wildcard Characters in File Names
1866 @cindex wildcard
1867 @cindex file name with wildcards
1868 @cindex globbing (wildcards)
1870 @cindex @code{*} (wildcard character)
1871 @cindex @code{?} (wildcard character)
1872 @cindex @code{[@dots{}]} (wildcard characters)
1873 A single file name can specify many files using @dfn{wildcard characters}.
1874 The wildcard characters in @code{make} are @samp{*}, @samp{?} and
1875 @samp{[@dots{}]}, the same as in the Bourne shell.  For example, @file{*.c}
1876 specifies a list of all the files (in the working directory) whose names
1877 end in @samp{.c}.@refill
1879 @cindex @code{~} (tilde)
1880 @cindex tilde (@code{~})
1881 @cindex home directory
1882 The character @samp{~} at the beginning of a file name also has special
1883 significance.  If alone, or followed by a slash, it represents your home
1884 directory.  For example @file{~/bin} expands to @file{/home/you/bin}.
1885 If the @samp{~} is followed by a word, the string represents the home
1886 directory of the user named by that word.  For example @file{~john/bin}
1887 expands to @file{/home/john/bin}.  On systems which don't have a home
1888 directory for each user (such as MS-DOS or MS-Windows), this
1889 functionality can be simulated by setting the environment variable
1890 @var{HOME}.@refill
1892 Wildcard expansion is performed by @code{make} automatically in
1893 targets and in prerequisites.  In recipes, the shell is responsible
1894 for wildcard expansion.  In other contexts, wildcard expansion happens
1895 only if you request it explicitly with the @code{wildcard} function.
1897 The special significance of a wildcard character can be turned off by
1898 preceding it with a backslash.  Thus, @file{foo\*bar} would refer to a
1899 specific file whose name consists of @samp{foo}, an asterisk, and
1900 @samp{bar}.@refill
1902 @menu
1903 * Wildcard Examples::           Several examples
1904 * Wildcard Pitfall::            Problems to avoid.
1905 * Wildcard Function::           How to cause wildcard expansion where
1906                                   it does not normally take place.
1907 @end menu
1909 @node Wildcard Examples, Wildcard Pitfall, Wildcards, Wildcards
1910 @subsection Wildcard Examples
1912 Wildcards can be used in the recipe of a rule, where they are expanded
1913 by the shell.  For example, here is a rule to delete all the object files:
1915 @example
1916 @group
1917 clean:
1918         rm -f *.o
1919 @end group
1920 @end example
1921 @cindex @code{rm} (shell command)
1923 Wildcards are also useful in the prerequisites of a rule.  With the
1924 following rule in the makefile, @samp{make print} will print all the
1925 @samp{.c} files that have changed since the last time you printed them:
1927 @example
1928 print: *.c
1929         lpr -p $?
1930         touch print
1931 @end example
1933 @cindex @code{print} target
1934 @cindex @code{lpr} (shell command)
1935 @cindex @code{touch} (shell command)
1936 @noindent
1937 This rule uses @file{print} as an empty target file; see @ref{Empty
1938 Targets, ,Empty Target Files to Record Events}.  (The automatic variable
1939 @samp{$?} is used to print only those files that have changed; see
1940 @ref{Automatic Variables}.)@refill
1942 Wildcard expansion does not happen when you define a variable.  Thus, if
1943 you write this:
1945 @example
1946 objects = *.o
1947 @end example
1949 @noindent
1950 then the value of the variable @code{objects} is the actual string
1951 @samp{*.o}.  However, if you use the value of @code{objects} in a target,
1952 prerequisite, or recipe, wildcard expansion will take place at that time.
1953 To set @code{objects} to the expansion, instead use:
1955 @example
1956 objects := $(wildcard *.o)
1957 @end example
1959 @noindent
1960 @xref{Wildcard Function}.
1962 @node Wildcard Pitfall, Wildcard Function, Wildcard Examples, Wildcards
1963 @subsection Pitfalls of Using Wildcards
1964 @cindex wildcard pitfalls
1965 @cindex pitfalls of wildcards
1966 @cindex mistakes with wildcards
1967 @cindex errors with wildcards
1968 @cindex problems with wildcards
1970 Now here is an example of a naive way of using wildcard expansion, that
1971 does not do what you would intend.  Suppose you would like to say that the
1972 executable file @file{foo} is made from all the object files in the
1973 directory, and you write this:
1975 @example
1976 objects = *.o
1978 foo : $(objects)
1979         cc -o foo $(CFLAGS) $(objects)
1980 @end example
1982 @noindent
1983 The value of @code{objects} is the actual string @samp{*.o}.  Wildcard
1984 expansion happens in the rule for @file{foo}, so that each @emph{existing}
1985 @samp{.o} file becomes a prerequisite of @file{foo} and will be recompiled if
1986 necessary.
1988 But what if you delete all the @samp{.o} files?  When a wildcard matches
1989 no files, it is left as it is, so then @file{foo} will depend on the
1990 oddly-named file @file{*.o}.  Since no such file is likely to exist,
1991 @code{make} will give you an error saying it cannot figure out how to
1992 make @file{*.o}.  This is not what you want!
1994 Actually it is possible to obtain the desired result with wildcard
1995 expansion, but you need more sophisticated techniques, including the
1996 @code{wildcard} function and string substitution.
1997 @ifnottex
1998 @xref{Wildcard Function, ,The Function @code{wildcard}}.
1999 @end ifnottex
2000 @iftex
2001 These are described in the following section.
2002 @end iftex
2004 @cindex wildcards and MS-DOS/MS-Windows backslashes
2005 @cindex backslashes in pathnames and wildcard expansion
2007 Microsoft operating systems (MS-DOS and MS-Windows) use backslashes to
2008 separate directories in pathnames, like so:
2010 @example
2011   c:\foo\bar\baz.c
2012 @end example
2014 This is equivalent to the Unix-style @file{c:/foo/bar/baz.c} (the
2015 @file{c:} part is the so-called drive letter).  When @code{make} runs on
2016 these systems, it supports backslashes as well as the Unix-style forward
2017 slashes in pathnames.  However, this support does @emph{not} include the
2018 wildcard expansion, where backslash is a quote character.  Therefore,
2019 you @emph{must} use Unix-style slashes in these cases.
2022 @node Wildcard Function,  , Wildcard Pitfall, Wildcards
2023 @subsection The Function @code{wildcard}
2024 @findex wildcard
2026 Wildcard expansion happens automatically in rules.  But wildcard expansion
2027 does not normally take place when a variable is set, or inside the
2028 arguments of a function.  If you want to do wildcard expansion in such
2029 places, you need to use the @code{wildcard} function, like this:
2031 @example
2032 $(wildcard @var{pattern}@dots{})
2033 @end example
2035 @noindent
2036 This string, used anywhere in a makefile, is replaced by a
2037 space-separated list of names of existing files that match one of the
2038 given file name patterns.  If no existing file name matches a pattern,
2039 then that pattern is omitted from the output of the @code{wildcard}
2040 function.  Note that this is different from how unmatched wildcards
2041 behave in rules, where they are used verbatim rather than ignored
2042 (@pxref{Wildcard Pitfall}).
2044 One use of the @code{wildcard} function is to get a list of all the C source
2045 files in a directory, like this:
2047 @example
2048 $(wildcard *.c)
2049 @end example
2051 We can change the list of C source files into a list of object files by
2052 replacing the @samp{.c} suffix with @samp{.o} in the result, like this:
2054 @example
2055 $(patsubst %.c,%.o,$(wildcard *.c))
2056 @end example
2058 @noindent
2059 (Here we have used another function, @code{patsubst}.
2060 @xref{Text Functions, ,Functions for String Substitution and Analysis}.)@refill
2062 Thus, a makefile to compile all C source files in the directory and then
2063 link them together could be written as follows:
2065 @example
2066 objects := $(patsubst %.c,%.o,$(wildcard *.c))
2068 foo : $(objects)
2069         cc -o foo $(objects)
2070 @end example
2072 @noindent
2073 (This takes advantage of the implicit rule for compiling C programs, so
2074 there is no need to write explicit rules for compiling the files.
2075 @xref{Flavors, ,The Two Flavors of Variables}, for an explanation of
2076 @samp{:=}, which is a variant of @samp{=}.)
2078 @node Directory Search, Phony Targets, Wildcards, Rules
2079 @section Searching Directories for Prerequisites
2080 @vindex VPATH
2081 @findex vpath
2082 @cindex vpath
2083 @cindex search path for prerequisites (@code{VPATH})
2084 @cindex directory search (@code{VPATH})
2086 For large systems, it is often desirable to put sources in a separate
2087 directory from the binaries.  The @dfn{directory search} features of
2088 @code{make} facilitate this by searching several directories
2089 automatically to find a prerequisite.  When you redistribute the files
2090 among directories, you do not need to change the individual rules,
2091 just the search paths.
2093 @menu
2094 * General Search::              Specifying a search path that applies
2095                                   to every prerequisite.
2096 * Selective Search::            Specifying a search path
2097                                   for a specified class of names.
2098 * Search Algorithm::            When and how search paths are applied.
2099 * Recipes/Search::             How to write recipes that work together
2100                                   with search paths.
2101 * Implicit/Search::             How search paths affect implicit rules.
2102 * Libraries/Search::            Directory search for link libraries.
2103 @end menu
2105 @node General Search, Selective Search, Directory Search, Directory Search
2106 @subsection @code{VPATH}: Search Path for All Prerequisites
2107 @vindex VPATH
2109 The value of the @code{make} variable @code{VPATH} specifies a list of
2110 directories that @code{make} should search.  Most often, the
2111 directories are expected to contain prerequisite files that are not in the
2112 current directory; however, @code{make} uses @code{VPATH} as a search
2113 list for both prerequisites and targets of rules.
2115 Thus, if a file that is listed as a target or prerequisite does not exist
2116 in the current directory, @code{make} searches the directories listed in
2117 @code{VPATH} for a file with that name.  If a file is found in one of
2118 them, that file may become the prerequisite (see below).  Rules may then
2119 specify the names of files in the prerequisite list as if they all
2120 existed in the current directory.  @xref{Recipes/Search, ,Writing Recipes with Directory Search}.
2122 In the @code{VPATH} variable, directory names are separated by colons or
2123 blanks.  The order in which directories are listed is the order followed
2124 by @code{make} in its search.  (On MS-DOS and MS-Windows, semi-colons
2125 are used as separators of directory names in @code{VPATH}, since the
2126 colon can be used in the pathname itself, after the drive letter.)
2128 For example,
2130 @example
2131 VPATH = src:../headers
2132 @end example
2134 @noindent
2135 specifies a path containing two directories, @file{src} and
2136 @file{../headers}, which @code{make} searches in that order.
2138 With this value of @code{VPATH}, the following rule,
2140 @example
2141 foo.o : foo.c
2142 @end example
2144 @noindent
2145 is interpreted as if it were written like this:
2147 @example
2148 foo.o : src/foo.c
2149 @end example
2151 @noindent
2152 assuming the file @file{foo.c} does not exist in the current directory but
2153 is found in the directory @file{src}.
2155 @node Selective Search, Search Algorithm, General Search, Directory Search
2156 @subsection The @code{vpath} Directive
2157 @findex vpath
2159 Similar to the @code{VPATH} variable, but more selective, is the
2160 @code{vpath} directive (note lower case), which allows you to specify a
2161 search path for a particular class of file names: those that match a
2162 particular pattern.  Thus you can supply certain search directories for
2163 one class of file names and other directories (or none) for other file
2164 names.
2166 There are three forms of the @code{vpath} directive:
2168 @table @code
2169 @item vpath @var{pattern} @var{directories}
2170 Specify the search path @var{directories} for file names that match
2171 @var{pattern}.
2173 The search path, @var{directories}, is a list of directories to be
2174 searched, separated by colons (semi-colons on MS-DOS and MS-Windows) or
2175 blanks, just like the search path used in the @code{VPATH} variable.
2177 @item vpath @var{pattern}
2178 Clear out the search path associated with @var{pattern}.
2180 @c Extra blank line makes sure this gets two lines.
2181 @item vpath
2183 Clear all search paths previously specified with @code{vpath} directives.
2184 @end table
2186 A @code{vpath} pattern is a string containing a @samp{%} character.  The
2187 string must match the file name of a prerequisite that is being searched
2188 for, the @samp{%} character matching any sequence of zero or more
2189 characters (as in pattern rules; @pxref{Pattern Rules, ,Defining and
2190 Redefining Pattern Rules}).  For example, @code{%.h} matches files that
2191 end in @code{.h}.  (If there is no @samp{%}, the pattern must match the
2192 prerequisite exactly, which is not useful very often.)
2194 @cindex @code{%}, quoting in @code{vpath}
2195 @cindex @code{%}, quoting with @code{\} (backslash)
2196 @cindex @code{\} (backslash), to quote @code{%}
2197 @cindex backslash (@code{\}), to quote @code{%}
2198 @cindex quoting @code{%}, in @code{vpath}
2199 @samp{%} characters in a @code{vpath} directive's pattern can be quoted
2200 with preceding backslashes (@samp{\}).  Backslashes that would otherwise
2201 quote @samp{%} characters can be quoted with more backslashes.
2202 Backslashes that quote @samp{%} characters or other backslashes are
2203 removed from the pattern before it is compared to file names.  Backslashes
2204 that are not in danger of quoting @samp{%} characters go unmolested.@refill
2206 When a prerequisite fails to exist in the current directory, if the
2207 @var{pattern} in a @code{vpath} directive matches the name of the
2208 prerequisite file, then the @var{directories} in that directive are searched
2209 just like (and before) the directories in the @code{VPATH} variable.
2211 For example,
2213 @example
2214 vpath %.h ../headers
2215 @end example
2217 @noindent
2218 tells @code{make} to look for any prerequisite whose name ends in @file{.h}
2219 in the directory @file{../headers} if the file is not found in the current
2220 directory.
2222 If several @code{vpath} patterns match the prerequisite file's name, then
2223 @code{make} processes each matching @code{vpath} directive one by one,
2224 searching all the directories mentioned in each directive.  @code{make}
2225 handles multiple @code{vpath} directives in the order in which they
2226 appear in the makefile; multiple directives with the same pattern are
2227 independent of each other.
2229 @need 750
2230 Thus,
2232 @example
2233 @group
2234 vpath %.c foo
2235 vpath %   blish
2236 vpath %.c bar
2237 @end group
2238 @end example
2240 @noindent
2241 will look for a file ending in @samp{.c} in @file{foo}, then
2242 @file{blish}, then @file{bar}, while
2244 @example
2245 @group
2246 vpath %.c foo:bar
2247 vpath %   blish
2248 @end group
2249 @end example
2251 @noindent
2252 will look for a file ending in @samp{.c} in @file{foo}, then
2253 @file{bar}, then @file{blish}.
2255 @node Search Algorithm, Recipes/Search, Selective Search, Directory Search
2256 @subsection How Directory Searches are Performed
2257 @cindex algorithm for directory search
2258 @cindex directory search algorithm
2260 When a prerequisite is found through directory search, regardless of type
2261 (general or selective), the pathname located may not be the one that
2262 @code{make} actually provides you in the prerequisite list.  Sometimes
2263 the path discovered through directory search is thrown away.
2265 The algorithm @code{make} uses to decide whether to keep or abandon a
2266 path found via directory search is as follows:
2268 @enumerate
2269 @item
2270 If a target file does not exist at the path specified in the makefile,
2271 directory search is performed.
2273 @item
2274 If the directory search is successful, that path is kept and this file
2275 is tentatively stored as the target.
2277 @item
2278 All prerequisites of this target are examined using this same method.
2280 @item
2281 After processing the prerequisites, the target may or may not need to be
2282 rebuilt:
2284 @enumerate a
2285 @item
2286 If the target does @emph{not} need to be rebuilt, the path to the file
2287 found during directory search is used for any prerequisite lists which
2288 contain this target.  In short, if @code{make} doesn't need to rebuild
2289 the target then you use the path found via directory search.
2291 @item
2292 If the target @emph{does} need to be rebuilt (is out-of-date), the
2293 pathname found during directory search is @emph{thrown away}, and the
2294 target is rebuilt using the file name specified in the makefile.  In
2295 short, if @code{make} must rebuild, then the target is rebuilt locally,
2296 not in the directory found via directory search.
2297 @end enumerate
2298 @end enumerate
2300 This algorithm may seem complex, but in practice it is quite often
2301 exactly what you want.
2303 @cindex traditional directory search (GPATH)
2304 @cindex directory search, traditional (GPATH)
2305 Other versions of @code{make} use a simpler algorithm: if the file does
2306 not exist, and it is found via directory search, then that pathname is
2307 always used whether or not the target needs to be built.  Thus, if the
2308 target is rebuilt it is created at the pathname discovered during
2309 directory search.
2311 @vindex GPATH
2312 If, in fact, this is the behavior you want for some or all of your
2313 directories, you can use the @code{GPATH} variable to indicate this to
2314 @code{make}.
2316 @code{GPATH} has the same syntax and format as @code{VPATH} (that is, a
2317 space- or colon-delimited list of pathnames).  If an out-of-date target
2318 is found by directory search in a directory that also appears in
2319 @code{GPATH}, then that pathname is not thrown away.  The target is
2320 rebuilt using the expanded path.
2322 @node Recipes/Search, Implicit/Search, Search Algorithm, Directory Search
2323 @subsection Writing Recipes with Directory Search
2324 @cindex recipes, and directory search
2325 @cindex directory search (@code{VPATH}), and recipes
2327 When a prerequisite is found in another directory through directory search,
2328 this cannot change the recipe of the rule; they will execute as written.
2329 Therefore, you must write the recipe with care so that it will look for
2330 the prerequisite in the directory where @code{make} finds it.
2332 This is done with the @dfn{automatic variables} such as @samp{$^}
2333 (@pxref{Automatic Variables}).
2334 For instance, the value of @samp{$^} is a
2335 list of all the prerequisites of the rule, including the names of
2336 the directories in which they were found, and the value of
2337 @samp{$@@} is the target.  Thus:@refill
2339 @example
2340 foo.o : foo.c
2341         cc -c $(CFLAGS) $^ -o $@@
2342 @end example
2344 @noindent
2345 (The variable @code{CFLAGS} exists so you can specify flags for C
2346 compilation by implicit rules; we use it here for consistency so it will
2347 affect all C compilations uniformly;
2348 @pxref{Implicit Variables, ,Variables Used by Implicit Rules}.)
2350 Often the prerequisites include header files as well, which you do not
2351 want to mention in the recipe.  The automatic variable @samp{$<} is
2352 just the first prerequisite:
2354 @example
2355 VPATH = src:../headers
2356 foo.o : foo.c defs.h hack.h
2357         cc -c $(CFLAGS) $< -o $@@
2358 @end example
2360 @node Implicit/Search, Libraries/Search, Recipes/Search, Directory Search
2361 @subsection Directory Search and Implicit Rules
2362 @cindex @code{VPATH}, and implicit rules
2363 @cindex directory search (@code{VPATH}), and implicit rules
2364 @cindex search path for prerequisites (@code{VPATH}), and implicit rules
2365 @cindex implicit rule, and directory search
2366 @cindex implicit rule, and @code{VPATH}
2367 @cindex rule, implicit, and directory search
2368 @cindex rule, implicit, and @code{VPATH}
2370 The search through the directories specified in @code{VPATH} or with
2371 @code{vpath} also happens during consideration of implicit rules
2372 (@pxref{Implicit Rules, ,Using Implicit Rules}).
2374 For example, when a file @file{foo.o} has no explicit rule, @code{make}
2375 considers implicit rules, such as the built-in rule to compile
2376 @file{foo.c} if that file exists.  If such a file is lacking in the
2377 current directory, the appropriate directories are searched for it.  If
2378 @file{foo.c} exists (or is mentioned in the makefile) in any of the
2379 directories, the implicit rule for C compilation is applied.
2381 The recipes of implicit rules normally use automatic variables as a
2382 matter of necessity; consequently they will use the file names found by
2383 directory search with no extra effort.
2385 @node Libraries/Search,  , Implicit/Search, Directory Search
2386 @subsection Directory Search for Link Libraries
2387 @cindex link libraries, and directory search
2388 @cindex libraries for linking, directory search
2389 @cindex directory search (@code{VPATH}), and link libraries
2390 @cindex @code{VPATH}, and link libraries
2391 @cindex search path for prerequisites (@code{VPATH}), and link libraries
2392 @cindex @code{-l} (library search)
2393 @cindex link libraries, patterns matching
2394 @cindex @code{.LIBPATTERNS}, and link libraries
2395 @vindex .LIBPATTERNS
2397 Directory search applies in a special way to libraries used with the
2398 linker.  This special feature comes into play when you write a prerequisite
2399 whose name is of the form @samp{-l@var{name}}.  (You can tell something
2400 strange is going on here because the prerequisite is normally the name of a
2401 file, and the @emph{file name} of a library generally looks like
2402 @file{lib@var{name}.a}, not like @samp{-l@var{name}}.)@refill
2404 When a prerequisite's name has the form @samp{-l@var{name}}, @code{make}
2405 handles it specially by searching for the file @file{lib@var{name}.so} in
2406 the current directory, in directories specified by matching @code{vpath}
2407 search paths and the @code{VPATH} search path, and then in the
2408 directories @file{/lib}, @file{/usr/lib}, and @file{@var{prefix}/lib}
2409 (normally @file{/usr/local/lib}, but MS-DOS/MS-Windows versions of
2410 @code{make} behave as if @var{prefix} is defined to be the root of the
2411 DJGPP installation tree).
2413 If that file is not found, then the file @file{lib@var{name}.a} is
2414 searched for, in the same directories as above.
2416 For example, if there is a @file{/usr/lib/libcurses.a} library on your
2417 system (and no @file{/usr/lib/libcurses.so} file), then
2419 @example
2420 @group
2421 foo : foo.c -lcurses
2422         cc $^ -o $@@
2423 @end group
2424 @end example
2426 @noindent
2427 would cause the command @samp{cc foo.c /usr/lib/libcurses.a -o foo} to
2428 be executed when @file{foo} is older than @file{foo.c} or than
2429 @file{/usr/lib/libcurses.a}.@refill
2431 Although the default set of files to be searched for is
2432 @file{lib@var{name}.so} and @file{lib@var{name}.a}, this is customizable
2433 via the @code{.LIBPATTERNS} variable.  Each word in the value of this
2434 variable is a pattern string.  When a prerequisite like
2435 @samp{-l@var{name}} is seen, @code{make} will replace the percent in
2436 each pattern in the list with @var{name} and perform the above directory
2437 searches using that library filename.  If no library is found, the next
2438 word in the list will be used.
2440 The default value for @code{.LIBPATTERNS} is @samp{lib%.so lib%.a},
2441 which provides the default behavior described above.
2443 You can turn off link library expansion completely by setting this
2444 variable to an empty value.
2446 @node Phony Targets, Force Targets, Directory Search, Rules
2447 @section Phony Targets
2448 @cindex phony targets
2449 @cindex targets, phony
2450 @cindex targets without a file
2452 A phony target is one that is not really the name of a file; rather it
2453 is just a name for a recipe to be executed when you make an explicit
2454 request.  There are two reasons to use a phony target: to avoid a
2455 conflict with a file of the same name, and to improve performance.
2457 If you write a rule whose recipe will not create the target file, the
2458 recipe will be executed every time the target comes up for remaking.
2459 Here is an example:
2461 @example
2462 @group
2463 clean:
2464         rm *.o temp
2465 @end group
2466 @end example
2468 @noindent
2469 Because the @code{rm} command does not create a file named @file{clean},
2470 probably no such file will ever exist.  Therefore, the @code{rm} command
2471 will be executed every time you say @samp{make clean}.
2472 @cindex @code{rm} (shell command)
2474 @findex .PHONY
2475 The phony target will cease to work if anything ever does create a file
2476 named @file{clean} in this directory.  Since it has no prerequisites, the
2477 file @file{clean} would inevitably be considered up to date, and its
2478 recipe would not be executed.  To avoid this problem, you can explicitly
2479 declare the target to be phony, using the special target @code{.PHONY}
2480 (@pxref{Special Targets, ,Special Built-in Target Names}) as follows:
2482 @example
2483 .PHONY : clean
2484 @end example
2486 @noindent
2487 Once this is done, @samp{make clean} will run the recipe regardless of
2488 whether there is a file named @file{clean}.
2490 Since it knows that phony targets do not name actual files that could be
2491 remade from other files, @code{make} skips the implicit rule search for
2492 phony targets (@pxref{Implicit Rules}).  This is why declaring a target
2493 phony is good for performance, even if you are not worried about the
2494 actual file existing.
2496 Thus, you first write the line that states that @code{clean} is a
2497 phony target, then you write the rule, like this:
2499 @example
2500 @group
2501 .PHONY: clean
2502 clean:
2503         rm *.o temp
2504 @end group
2505 @end example
2507 Another example of the usefulness of phony targets is in conjunction
2508 with recursive invocations of @code{make} (for more information, see
2509 @ref{Recursion, ,Recursive Use of @code{make}}).  In this case the
2510 makefile will often contain a variable which lists a number of
2511 subdirectories to be built.  One way to handle this is with one rule
2512 whose recipe is a shell loop over the subdirectories, like this:
2514 @example
2515 @group
2516 SUBDIRS = foo bar baz
2518 subdirs:
2519         for dir in $(SUBDIRS); do \
2520           $(MAKE) -C $$dir; \
2521         done
2522 @end group
2523 @end example
2525 There are problems with this method, however.  First, any error
2526 detected in a submake is ignored by this rule, so it will continue
2527 to build the rest of the directories even when one fails.  This can be
2528 overcome by adding shell commands to note the error and exit, but then
2529 it will do so even if @code{make} is invoked with the @code{-k}
2530 option, which is unfortunate.  Second, and perhaps more importantly,
2531 you cannot take advantage of @code{make}'s ability to build targets in
2532 parallel (@pxref{Parallel, ,Parallel Execution}), since there is only
2533 one rule.
2535 By declaring the subdirectories as phony targets (you must do this as
2536 the subdirectory obviously always exists; otherwise it won't be built)
2537 you can remove these problems:
2539 @example
2540 @group
2541 SUBDIRS = foo bar baz
2543 .PHONY: subdirs $(SUBDIRS)
2545 subdirs: $(SUBDIRS)
2547 $(SUBDIRS):
2548         $(MAKE) -C $@@
2550 foo: baz
2551 @end group
2552 @end example
2554 Here we've also declared that the @file{foo} subdirectory cannot be
2555 built until after the @file{baz} subdirectory is complete; this kind of
2556 relationship declaration is particularly important when attempting
2557 parallel builds.
2559 A phony target should not be a prerequisite of a real target file; if it
2560 is, its recipe will be run every time @code{make} goes to update that
2561 file.  As long as a phony target is never a prerequisite of a real
2562 target, the phony target recipe will be executed only when the phony
2563 target is a specified goal (@pxref{Goals, ,Arguments to Specify the
2564 Goals}).
2566 Phony targets can have prerequisites.  When one directory contains multiple
2567 programs, it is most convenient to describe all of the programs in one
2568 makefile @file{./Makefile}.  Since the target remade by default will be the
2569 first one in the makefile, it is common to make this a phony target named
2570 @samp{all} and give it, as prerequisites, all the individual programs.  For
2571 example:
2573 @example
2574 all : prog1 prog2 prog3
2575 .PHONY : all
2577 prog1 : prog1.o utils.o
2578         cc -o prog1 prog1.o utils.o
2580 prog2 : prog2.o
2581         cc -o prog2 prog2.o
2583 prog3 : prog3.o sort.o utils.o
2584         cc -o prog3 prog3.o sort.o utils.o
2585 @end example
2587 @noindent
2588 Now you can say just @samp{make} to remake all three programs, or
2589 specify as arguments the ones to remake (as in @samp{make prog1
2590 prog3}).  Phoniness is not inherited: the prerequisites of a phony
2591 target are not themselves phony, unless explicitly declared to be so.
2593 When one phony target is a prerequisite of another, it serves as a subroutine
2594 of the other.  For example, here @samp{make cleanall} will delete the
2595 object files, the difference files, and the file @file{program}:
2597 @example
2598 .PHONY: cleanall cleanobj cleandiff
2600 cleanall : cleanobj cleandiff
2601         rm program
2603 cleanobj :
2604         rm *.o
2606 cleandiff :
2607         rm *.diff
2608 @end example
2610 @node Force Targets, Empty Targets, Phony Targets, Rules
2611 @section Rules without Recipes or Prerequisites
2612 @cindex force targets
2613 @cindex targets, force
2614 @cindex @code{FORCE}
2615 @cindex rule, no recipe or prerequisites
2617 If a rule has no prerequisites or recipe, and the target of the rule
2618 is a nonexistent file, then @code{make} imagines this target to have
2619 been updated whenever its rule is run.  This implies that all targets
2620 depending on this one will always have their recipe run.
2622 An example will illustrate this:
2624 @example
2625 @group
2626 clean: FORCE
2627         rm $(objects)
2628 FORCE:
2629 @end group
2630 @end example
2632 Here the target @samp{FORCE} satisfies the special conditions, so the
2633 target @file{clean} that depends on it is forced to run its recipe.
2634 There is nothing special about the name @samp{FORCE}, but that is one
2635 name commonly used this way.
2637 As you can see, using @samp{FORCE} this way has the same results as using
2638 @samp{.PHONY: clean}.
2640 Using @samp{.PHONY} is more explicit and more efficient.  However,
2641 other versions of @code{make} do not support @samp{.PHONY}; thus
2642 @samp{FORCE} appears in many makefiles.  @xref{Phony Targets}.
2644 @node Empty Targets, Special Targets, Force Targets, Rules
2645 @section Empty Target Files to Record Events
2646 @cindex empty targets
2647 @cindex targets, empty
2648 @cindex recording events with empty targets
2650 The @dfn{empty target} is a variant of the phony target; it is used to hold
2651 recipes for an action that you request explicitly from time to time.
2652 Unlike a phony target, this target file can really exist; but the file's
2653 contents do not matter, and usually are empty.
2655 The purpose of the empty target file is to record, with its
2656 last-modification time, when the rule's recipe was last executed.  It
2657 does so because one of the commands in the recipe is a @code{touch}
2658 command to update the target file.
2660 The empty target file should have some prerequisites (otherwise it
2661 doesn't make sense).  When you ask to remake the empty target, the
2662 recipe is executed if any prerequisite is more recent than the target;
2663 in other words, if a prerequisite has changed since the last time you
2664 remade the target.  Here is an example:
2666 @example
2667 print: foo.c bar.c
2668         lpr -p $?
2669         touch print
2670 @end example
2671 @cindex @code{print} target
2672 @cindex @code{lpr} (shell command)
2673 @cindex @code{touch} (shell command)
2675 @noindent
2676 With this rule, @samp{make print} will execute the @code{lpr} command if
2677 either source file has changed since the last @samp{make print}.  The
2678 automatic variable @samp{$?} is used to print only those files that have
2679 changed (@pxref{Automatic Variables}).
2681 @node Special Targets, Multiple Targets, Empty Targets, Rules
2682 @section Special Built-in Target Names
2683 @cindex special targets
2684 @cindex built-in special targets
2685 @cindex targets, built-in special
2687 Certain names have special meanings if they appear as targets.
2689 @table @code
2690 @findex .PHONY
2691 @item .PHONY
2693 The prerequisites of the special target @code{.PHONY} are considered to
2694 be phony targets.  When it is time to consider such a target,
2695 @code{make} will run its recipe unconditionally, regardless of
2696 whether a file with that name exists or what its last-modification
2697 time is.  @xref{Phony Targets, ,Phony Targets}.
2699 @findex .SUFFIXES
2700 @item .SUFFIXES
2702 The prerequisites of the special target @code{.SUFFIXES} are the list
2703 of suffixes to be used in checking for suffix rules.
2704 @xref{Suffix Rules, , Old-Fashioned Suffix Rules}.
2706 @findex .DEFAULT
2707 @item .DEFAULT
2709 The recipe specified for @code{.DEFAULT} is used for any target for
2710 which no rules are found (either explicit rules or implicit rules).
2711 @xref{Last Resort}.  If a @code{.DEFAULT} recipe is specified, every
2712 file mentioned as a prerequisite, but not as a target in a rule, will have
2713 that recipe executed on its behalf.  @xref{Implicit Rule Search,
2714 ,Implicit Rule Search Algorithm}.
2716 @findex .PRECIOUS
2717 @item .PRECIOUS
2718 @cindex precious targets
2719 @cindex preserving with @code{.PRECIOUS}
2721 The targets which @code{.PRECIOUS} depends on are given the following
2722 special treatment: if @code{make} is killed or interrupted during the
2723 execution of their recipes, the target is not deleted.
2724 @xref{Interrupts, ,Interrupting or Killing @code{make}}.  Also, if the
2725 target is an intermediate file, it will not be deleted after it is no
2726 longer needed, as is normally done.  @xref{Chained Rules, ,Chains of
2727 Implicit Rules}.  In this latter respect it overlaps with the
2728 @code{.SECONDARY} special target.
2730 You can also list the target pattern of an implicit rule (such as
2731 @samp{%.o}) as a prerequisite file of the special target @code{.PRECIOUS}
2732 to preserve intermediate files created by rules whose target patterns
2733 match that file's name.
2735 @findex .INTERMEDIATE
2736 @item .INTERMEDIATE
2737 @cindex intermediate targets, explicit
2739 The targets which @code{.INTERMEDIATE} depends on are treated as
2740 intermediate files.  @xref{Chained Rules, ,Chains of Implicit Rules}.
2741 @code{.INTERMEDIATE} with no prerequisites has no effect.
2743 @findex .SECONDARY
2744 @item .SECONDARY
2745 @cindex secondary targets
2746 @cindex preserving with @code{.SECONDARY}
2748 The targets which @code{.SECONDARY} depends on are treated as
2749 intermediate files, except that they are never automatically deleted.
2750 @xref{Chained Rules, ,Chains of Implicit Rules}.
2752 @code{.SECONDARY} with no prerequisites causes all targets to be treated
2753 as secondary (i.e., no target is removed because it is considered
2754 intermediate).
2756 @findex .SECONDEXPANSION
2757 @item .SECONDEXPANSION
2759 If @code{.SECONDEXPANSION} is mentioned as a target anywhere in the
2760 makefile, then all prerequisite lists defined @emph{after} it appears
2761 will be expanded a second time after all makefiles have been read in.
2762 @xref{Secondary Expansion, ,Secondary Expansion}.
2764 @findex .DELETE_ON_ERROR
2765 @item .DELETE_ON_ERROR
2766 @cindex removing targets on failure
2768 If @code{.DELETE_ON_ERROR} is mentioned as a target anywhere in the
2769 makefile, then @code{make} will delete the target of a rule if it has
2770 changed and its recipe exits with a nonzero exit status, just as it
2771 does when it receives a signal.  @xref{Errors, ,Errors in Recipes}.
2773 @findex .IGNORE
2774 @item .IGNORE
2776 If you specify prerequisites for @code{.IGNORE}, then @code{make} will
2777 ignore errors in execution of the recipe for those particular files.
2778 The recipe for @code{.IGNORE} (if any) is ignored.
2780 If mentioned as a target with no prerequisites, @code{.IGNORE} says to
2781 ignore errors in execution of recipes for all files.  This usage of
2782 @samp{.IGNORE} is supported only for historical compatibility.  Since
2783 this affects every recipe in the makefile, it is not very useful; we
2784 recommend you use the more selective ways to ignore errors in specific
2785 recipes.  @xref{Errors, ,Errors in Recipes}.
2787 @findex .LOW_RESOLUTION_TIME
2788 @item .LOW_RESOLUTION_TIME
2790 If you specify prerequisites for @code{.LOW_RESOLUTION_TIME},
2791 @command{make} assumes that these files are created by commands that
2792 generate low resolution time stamps.  The recipe for the
2793 @code{.LOW_RESOLUTION_TIME} target are ignored.
2795 The high resolution file time stamps of many modern file systems
2796 lessen the chance of @command{make} incorrectly concluding that a file
2797 is up to date.  Unfortunately, some hosts do not provide a way to set a
2798 high resolution file time stamp, so commands like @samp{cp -p} that
2799 explicitly set a file's time stamp must discard its subsecond part.
2800 If a file is created by such a command, you should list it as a
2801 prerequisite of @code{.LOW_RESOLUTION_TIME} so that @command{make}
2802 does not mistakenly conclude that the file is out of date.  For
2803 example:
2805 @example
2806 @group
2807 .LOW_RESOLUTION_TIME: dst
2808 dst: src
2809         cp -p src dst
2810 @end group
2811 @end example
2813 Since @samp{cp -p} discards the subsecond part of @file{src}'s time
2814 stamp, @file{dst} is typically slightly older than @file{src} even when
2815 it is up to date.  The @code{.LOW_RESOLUTION_TIME} line causes
2816 @command{make} to consider @file{dst} to be up to date if its time stamp
2817 is at the start of the same second that @file{src}'s time stamp is in.
2819 Due to a limitation of the archive format, archive member time stamps
2820 are always low resolution.  You need not list archive members as
2821 prerequisites of @code{.LOW_RESOLUTION_TIME}, as @command{make} does this
2822 automatically.
2824 @findex .SILENT
2825 @item .SILENT
2827 If you specify prerequisites for @code{.SILENT}, then @code{make} will
2828 not print the recipe used to remake those particular files before
2829 executing them.  The recipe for @code{.SILENT} is ignored.
2831 If mentioned as a target with no prerequisites, @code{.SILENT} says not
2832 to print any recipes before executing them.  This usage of
2833 @samp{.SILENT} is supported only for historical compatibility.  We
2834 recommend you use the more selective ways to silence specific recipes.
2835 @xref{Echoing, ,Recipe Echoing}.  If you want to silence all recipes
2836 for a particular run of @code{make}, use the @samp{-s} or
2837 @w{@samp{--silent}} option (@pxref{Options Summary}).
2839 @findex .EXPORT_ALL_VARIABLES
2840 @item .EXPORT_ALL_VARIABLES
2842 Simply by being mentioned as a target, this tells @code{make} to
2843 export all variables to child processes by default.
2844 @xref{Variables/Recursion, ,Communicating Variables to a
2845 Sub-@code{make}}.
2847 @findex .NOTPARALLEL
2848 @item .NOTPARALLEL
2849 @cindex parallel execution, overriding
2851 If @code{.NOTPARALLEL} is mentioned as a target, then this invocation
2852 of @code{make} will be run serially, even if the @samp{-j} option is
2853 given.  Any recursively invoked @code{make} command will still run
2854 recipes in parallel (unless its makefile also contains this target).
2855 Any prerequisites on this target are ignored.
2856 @end table
2858 Any defined implicit rule suffix also counts as a special target if it
2859 appears as a target, and so does the concatenation of two suffixes, such
2860 as @samp{.c.o}.  These targets are suffix rules, an obsolete way of
2861 defining implicit rules (but a way still widely used).  In principle, any
2862 target name could be special in this way if you break it in two and add
2863 both pieces to the suffix list.  In practice, suffixes normally begin with
2864 @samp{.}, so these special target names also begin with @samp{.}.
2865 @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
2867 @node Multiple Targets, Multiple Rules, Special Targets, Rules
2868 @section Multiple Targets in a Rule
2869 @cindex multiple targets
2870 @cindex several targets in a rule
2871 @cindex targets, multiple
2872 @cindex rule, with multiple targets
2874 A rule with multiple targets is equivalent to writing many rules, each with
2875 one target, and all identical aside from that.  The same recipe applies to
2876 all the targets, but its effect may vary because you can substitute the
2877 actual target name into the recipe using @samp{$@@}.  The rule contributes
2878 the same prerequisites to all the targets also.
2880 This is useful in two cases.
2882 @itemize @bullet
2883 @item
2884 You want just prerequisites, no recipe.  For example:
2886 @example
2887 kbd.o command.o files.o: command.h
2888 @end example
2890 @noindent
2891 gives an additional prerequisite to each of the three object files
2892 mentioned.
2894 @item
2895 Similar recipes work for all the targets.  The recipes do not need
2896 to be absolutely identical, since the automatic variable @samp{$@@}
2897 can be used to substitute the particular target to be remade into the
2898 commands (@pxref{Automatic Variables}).  For example:
2900 @example
2901 @group
2902 bigoutput littleoutput : text.g
2903         generate text.g -$(subst output,,$@@) > $@@
2904 @end group
2905 @end example
2906 @findex subst
2908 @noindent
2909 is equivalent to
2911 @example
2912 bigoutput : text.g
2913         generate text.g -big > bigoutput
2914 littleoutput : text.g
2915         generate text.g -little > littleoutput
2916 @end example
2918 @noindent
2919 Here we assume the hypothetical program @code{generate} makes two
2920 types of output, one if given @samp{-big} and one if given
2921 @samp{-little}.
2922 @xref{Text Functions, ,Functions for String Substitution and Analysis},
2923 for an explanation of the @code{subst} function.
2924 @end itemize
2926 Suppose you would like to vary the prerequisites according to the
2927 target, much as the variable @samp{$@@} allows you to vary the recipe.
2928 You cannot do this with multiple targets in an ordinary rule, but you
2929 can do it with a @dfn{static pattern rule}.  @xref{Static Pattern,
2930 ,Static Pattern Rules}.
2932 @node Multiple Rules, Static Pattern, Multiple Targets, Rules
2933 @section Multiple Rules for One Target
2934 @cindex multiple rules for one target
2935 @cindex several rules for one target
2936 @cindex rule, multiple for one target
2937 @cindex target, multiple rules for one
2939 One file can be the target of several rules.  All the prerequisites
2940 mentioned in all the rules are merged into one list of prerequisites for
2941 the target.  If the target is older than any prerequisite from any rule,
2942 the recipe is executed.
2944 There can only be one recipe to be executed for a file.  If more than
2945 one rule gives a recipe for the same file, @code{make} uses the last
2946 one given and prints an error message.  (As a special case, if the
2947 file's name begins with a dot, no error message is printed.  This odd
2948 behavior is only for compatibility with other implementations of
2949 @code{make}@dots{} you should avoid using it).  Occasionally it is
2950 useful to have the same target invoke multiple recipes which are
2951 defined in different parts of your makefile; you can use
2952 @dfn{double-colon rules} (@pxref{Double-Colon}) for this.
2954 An extra rule with just prerequisites can be used to give a few extra
2955 prerequisites to many files at once.  For example, makefiles often
2956 have a variable, such as @code{objects}, containing a list of all the
2957 compiler output files in the system being made.  An easy way to say
2958 that all of them must be recompiled if @file{config.h} changes is to
2959 write the following:
2961 @example
2962 objects = foo.o bar.o
2963 foo.o : defs.h
2964 bar.o : defs.h test.h
2965 $(objects) : config.h
2966 @end example
2968 This could be inserted or taken out without changing the rules that really
2969 specify how to make the object files, making it a convenient form to use if
2970 you wish to add the additional prerequisite intermittently.
2972 Another wrinkle is that the additional prerequisites could be
2973 specified with a variable that you set with a command line argument to
2974 @code{make} (@pxref{Overriding, ,Overriding Variables}).  For example,
2976 @example
2977 @group
2978 extradeps=
2979 $(objects) : $(extradeps)
2980 @end group
2981 @end example
2983 @noindent
2984 means that the command @samp{make extradeps=foo.h} will consider
2985 @file{foo.h} as a prerequisite of each object file, but plain @samp{make}
2986 will not.
2988 If none of the explicit rules for a target has a recipe, then @code{make}
2989 searches for an applicable implicit rule to find one
2990 @pxref{Implicit Rules, ,Using Implicit Rules}).
2992 @node Static Pattern, Double-Colon, Multiple Rules, Rules
2993 @section Static Pattern Rules
2994 @cindex static pattern rule
2995 @cindex rule, static pattern
2996 @cindex pattern rules, static (not implicit)
2997 @cindex varying prerequisites
2998 @cindex prerequisites, varying (static pattern)
3000 @dfn{Static pattern rules} are rules which specify multiple targets and
3001 construct the prerequisite names for each target based on the target name.
3002 They are more general than ordinary rules with multiple targets because the
3003 targets do not have to have identical prerequisites.  Their prerequisites must
3004 be @emph{analogous}, but not necessarily @emph{identical}.
3006 @menu
3007 * Static Usage::                The syntax of static pattern rules.
3008 * Static versus Implicit::      When are they better than implicit rules?
3009 @end menu
3011 @node Static Usage, Static versus Implicit, Static Pattern, Static Pattern
3012 @subsection Syntax of Static Pattern Rules
3013 @cindex static pattern rule, syntax of
3014 @cindex pattern rules, static, syntax of
3016 Here is the syntax of a static pattern rule:
3018 @example
3019 @var{targets} @dots{}: @var{target-pattern}: @var{prereq-patterns} @dots{}
3020         @var{recipe}
3021         @dots{}
3022 @end example
3024 @noindent
3025 The @var{targets} list specifies the targets that the rule applies to.
3026 The targets can contain wildcard characters, just like the targets of
3027 ordinary rules (@pxref{Wildcards, ,Using Wildcard Characters in File
3028 Names}).
3030 @cindex target pattern, static (not implicit)
3031 @cindex stem
3032 The @var{target-pattern} and @var{prereq-patterns} say how to compute the
3033 prerequisites of each target.  Each target is matched against the
3034 @var{target-pattern} to extract a part of the target name, called the
3035 @dfn{stem}.  This stem is substituted into each of the @var{prereq-patterns}
3036 to make the prerequisite names (one from each @var{prereq-pattern}).
3038 Each pattern normally contains the character @samp{%} just once.  When the
3039 @var{target-pattern} matches a target, the @samp{%} can match any part of
3040 the target name; this part is called the @dfn{stem}.  The rest of the
3041 pattern must match exactly.  For example, the target @file{foo.o} matches
3042 the pattern @samp{%.o}, with @samp{foo} as the stem.  The targets
3043 @file{foo.c} and @file{foo.out} do not match that pattern.@refill
3045 @cindex prerequisite pattern, static (not implicit)
3046 The prerequisite names for each target are made by substituting the stem
3047 for the @samp{%} in each prerequisite pattern.  For example, if one
3048 prerequisite pattern is @file{%.c}, then substitution of the stem
3049 @samp{foo} gives the prerequisite name @file{foo.c}.  It is legitimate
3050 to write a prerequisite pattern that does not contain @samp{%}; then this
3051 prerequisite is the same for all targets.
3053 @cindex @code{%}, quoting in static pattern
3054 @cindex @code{%}, quoting with @code{\} (backslash)
3055 @cindex @code{\} (backslash), to quote @code{%}
3056 @cindex backslash (@code{\}), to quote @code{%}
3057 @cindex quoting @code{%}, in static pattern
3058 @samp{%} characters in pattern rules can be quoted with preceding
3059 backslashes (@samp{\}).  Backslashes that would otherwise quote @samp{%}
3060 characters can be quoted with more backslashes.  Backslashes that quote
3061 @samp{%} characters or other backslashes are removed from the pattern
3062 before it is compared to file names or has a stem substituted into it.
3063 Backslashes that are not in danger of quoting @samp{%} characters go
3064 unmolested.  For example, the pattern @file{the\%weird\\%pattern\\} has
3065 @samp{the%weird\} preceding the operative @samp{%} character, and
3066 @samp{pattern\\} following it.  The final two backslashes are left alone
3067 because they cannot affect any @samp{%} character.@refill
3069 Here is an example, which compiles each of @file{foo.o} and @file{bar.o}
3070 from the corresponding @file{.c} file:
3072 @example
3073 @group
3074 objects = foo.o bar.o
3076 all: $(objects)
3078 $(objects): %.o: %.c
3079         $(CC) -c $(CFLAGS) $< -o $@@
3080 @end group
3081 @end example
3083 @noindent
3084 Here @samp{$<} is the automatic variable that holds the name of the
3085 prerequisite and @samp{$@@} is the automatic variable that holds the name
3086 of the target; see @ref{Automatic Variables}.
3088 Each target specified must match the target pattern; a warning is issued
3089 for each target that does not.  If you have a list of files, only some of
3090 which will match the pattern, you can use the @code{filter} function to
3091 remove nonmatching file names (@pxref{Text Functions, ,Functions for String Substitution and Analysis}):
3093 @example
3094 files = foo.elc bar.o lose.o
3096 $(filter %.o,$(files)): %.o: %.c
3097         $(CC) -c $(CFLAGS) $< -o $@@
3098 $(filter %.elc,$(files)): %.elc: %.el
3099         emacs -f batch-byte-compile $<
3100 @end example
3102 @noindent
3103 In this example the result of @samp{$(filter %.o,$(files))} is
3104 @file{bar.o lose.o}, and the first static pattern rule causes each of
3105 these object files to be updated by compiling the corresponding C source
3106 file.  The result of @w{@samp{$(filter %.elc,$(files))}} is
3107 @file{foo.elc}, so that file is made from @file{foo.el}.@refill
3109 Another example shows how to use @code{$*} in static pattern rules:
3110 @vindex $*@r{, and static pattern}
3112 @example
3113 @group
3114 bigoutput littleoutput : %output : text.g
3115         generate text.g -$* > $@@
3116 @end group
3117 @end example
3119 @noindent
3120 When the @code{generate} command is run, @code{$*} will expand to the
3121 stem, either @samp{big} or @samp{little}.
3123 @node Static versus Implicit,  , Static Usage, Static Pattern
3124 @subsection Static Pattern Rules versus Implicit Rules
3125 @cindex rule, static pattern versus implicit
3126 @cindex static pattern rule, versus implicit
3128 A static pattern rule has much in common with an implicit rule defined as a
3129 pattern rule (@pxref{Pattern Rules, ,Defining and Redefining Pattern Rules}).
3130 Both have a pattern for the target and patterns for constructing the
3131 names of prerequisites.  The difference is in how @code{make} decides
3132 @emph{when} the rule applies.
3134 An implicit rule @emph{can} apply to any target that matches its pattern,
3135 but it @emph{does} apply only when the target has no recipe otherwise
3136 specified, and only when the prerequisites can be found.  If more than one
3137 implicit rule appears applicable, only one applies; the choice depends on
3138 the order of rules.
3140 By contrast, a static pattern rule applies to the precise list of targets
3141 that you specify in the rule.  It cannot apply to any other target and it
3142 invariably does apply to each of the targets specified.  If two conflicting
3143 rules apply, and both have recipes, that's an error.
3145 The static pattern rule can be better than an implicit rule for these
3146 reasons:
3148 @itemize @bullet
3149 @item
3150 You may wish to override the usual implicit rule for a few
3151 files whose names cannot be categorized syntactically but
3152 can be given in an explicit list.
3154 @item
3155 If you cannot be sure of the precise contents of the directories
3156 you are using, you may not be sure which other irrelevant files
3157 might lead @code{make} to use the wrong implicit rule.  The choice
3158 might depend on the order in which the implicit rule search is done.
3159 With static pattern rules, there is no uncertainty: each rule applies
3160 to precisely the targets specified.
3161 @end itemize
3163 @node Double-Colon, Automatic Prerequisites, Static Pattern, Rules
3164 @section Double-Colon Rules
3165 @cindex double-colon rules
3166 @cindex rule, double-colon (@code{::})
3167 @cindex multiple rules for one target (@code{::})
3168 @cindex @code{::} rules (double-colon)
3170 @dfn{Double-colon} rules are rules written with @samp{::} instead of
3171 @samp{:} after the target names.  They are handled differently from
3172 ordinary rules when the same target appears in more than one rule.
3174 When a target appears in multiple rules, all the rules must be the same
3175 type: all ordinary, or all double-colon.  If they are double-colon, each
3176 of them is independent of the others.  Each double-colon rule's recipe
3177 is executed if the target is older than any prerequisites of that rule.
3178 If there are no prerequisites for that rule, its recipe is always
3179 executed (even if the target already exists).  This can result in
3180 executing none, any, or all of the double-colon rules.
3182 Double-colon rules with the same target are in fact completely separate
3183 from one another.  Each double-colon rule is processed individually, just
3184 as rules with different targets are processed.
3186 The double-colon rules for a target are executed in the order they appear
3187 in the makefile.  However, the cases where double-colon rules really make
3188 sense are those where the order of executing the recipes would not matter.
3190 Double-colon rules are somewhat obscure and not often very useful; they
3191 provide a mechanism for cases in which the method used to update a target
3192 differs depending on which prerequisite files caused the update, and such
3193 cases are rare.
3195 Each double-colon rule should specify a recipe; if it does not, an
3196 implicit rule will be used if one applies.
3197 @xref{Implicit Rules, ,Using Implicit Rules}.
3199 @node Automatic Prerequisites,  , Double-Colon, Rules
3200 @section Generating Prerequisites Automatically
3201 @cindex prerequisites, automatic generation
3202 @cindex automatic generation of prerequisites
3203 @cindex generating prerequisites automatically
3205 In the makefile for a program, many of the rules you need to write often
3206 say only that some object file depends on some header
3207 file.  For example, if @file{main.c} uses @file{defs.h} via an
3208 @code{#include}, you would write:
3210 @example
3211 main.o: defs.h
3212 @end example
3214 @noindent
3215 You need this rule so that @code{make} knows that it must remake
3216 @file{main.o} whenever @file{defs.h} changes.  You can see that for a
3217 large program you would have to write dozens of such rules in your
3218 makefile.  And, you must always be very careful to update the makefile
3219 every time you add or remove an @code{#include}.
3220 @cindex @code{#include}
3222 @cindex @code{-M} (to compiler)
3223 To avoid this hassle, most modern C compilers can write these rules for
3224 you, by looking at the @code{#include} lines in the source files.
3225 Usually this is done with the @samp{-M} option to the compiler.
3226 For example, the command:
3228 @example
3229 cc -M main.c
3230 @end example
3232 @noindent
3233 generates the output:
3235 @example
3236 main.o : main.c defs.h
3237 @end example
3239 @noindent
3240 Thus you no longer have to write all those rules yourself.
3241 The compiler will do it for you.
3243 Note that such a prerequisite constitutes mentioning @file{main.o} in a
3244 makefile, so it can never be considered an intermediate file by implicit
3245 rule search.  This means that @code{make} won't ever remove the file
3246 after using it; @pxref{Chained Rules, ,Chains of Implicit Rules}.
3248 @cindex @code{make depend}
3249 With old @code{make} programs, it was traditional practice to use this
3250 compiler feature to generate prerequisites on demand with a command like
3251 @samp{make depend}.  That command would create a file @file{depend}
3252 containing all the automatically-generated prerequisites; then the
3253 makefile could use @code{include} to read them in (@pxref{Include}).
3255 In GNU @code{make}, the feature of remaking makefiles makes this
3256 practice obsolete---you need never tell @code{make} explicitly to
3257 regenerate the prerequisites, because it always regenerates any makefile
3258 that is out of date.  @xref{Remaking Makefiles}.
3260 The practice we recommend for automatic prerequisite generation is to have
3261 one makefile corresponding to each source file.  For each source file
3262 @file{@var{name}.c} there is a makefile @file{@var{name}.d} which lists
3263 what files the object file @file{@var{name}.o} depends on.  That way
3264 only the source files that have changed need to be rescanned to produce
3265 the new prerequisites.
3267 Here is the pattern rule to generate a file of prerequisites (i.e., a makefile)
3268 called @file{@var{name}.d} from a C source file called @file{@var{name}.c}:
3270 @smallexample
3271 @group
3272 %.d: %.c
3273         @@set -e; rm -f $@@; \
3274          $(CC) -M $(CPPFLAGS) $< > $@@.$$$$; \
3275          sed 's,\($*\)\.o[ :]*,\1.o $@@ : ,g' < $@@.$$$$ > $@@; \
3276          rm -f $@@.$$$$
3277 @end group
3278 @end smallexample
3280 @noindent
3281 @xref{Pattern Rules}, for information on defining pattern rules.  The
3282 @samp{-e} flag to the shell causes it to exit immediately if the
3283 @code{$(CC)} command (or any other command) fails (exits with a
3284 nonzero status).
3285 @cindex @code{-e} (shell flag)
3287 @cindex @code{-MM} (to GNU compiler)
3288 With the GNU C compiler, you may wish to use the @samp{-MM} flag instead
3289 of @samp{-M}.  This omits prerequisites on system header files.
3290 @xref{Preprocessor Options, , Options Controlling the Preprocessor,
3291 gcc.info, Using GNU CC}, for details.
3293 @cindex @code{sed} (shell command)
3294 The purpose of the @code{sed} command is to translate (for example):
3296 @example
3297 main.o : main.c defs.h
3298 @end example
3300 @noindent
3301 into:
3303 @example
3304 main.o main.d : main.c defs.h
3305 @end example
3307 @noindent
3308 @cindex @code{.d}
3309 This makes each @samp{.d} file depend on all the source and header files
3310 that the corresponding @samp{.o} file depends on.  @code{make} then
3311 knows it must regenerate the prerequisites whenever any of the source or
3312 header files changes.
3314 Once you've defined the rule to remake the @samp{.d} files,
3315 you then use the @code{include} directive to read them all in.
3316 @xref{Include}.  For example:
3318 @example
3319 @group
3320 sources = foo.c bar.c
3322 include $(sources:.c=.d)
3323 @end group
3324 @end example
3326 @noindent
3327 (This example uses a substitution variable reference to translate the
3328 list of source files @samp{foo.c bar.c} into a list of prerequisite
3329 makefiles, @samp{foo.d bar.d}.  @xref{Substitution Refs}, for full
3330 information on substitution references.)  Since the @samp{.d} files are
3331 makefiles like any others, @code{make} will remake them as necessary
3332 with no further work from you.  @xref{Remaking Makefiles}.
3334 Note that the @samp{.d} files contain target definitions; you should
3335 be sure to place the @code{include} directive @emph{after} the first,
3336 default goal in your makefiles or run the risk of having a random
3337 object file become the default goal.
3338 @xref{How Make Works}.
3340 @node Recipes, Using Variables, Rules, Top
3341 @chapter Writing Recipes in Rules
3342 @cindex recipes
3343 @cindex recipes, how to write
3344 @cindex writing recipes
3346 The recipe of a rule consists of one or more shell command lines to
3347 be executed, one at a time, in the order they appear.  Typically, the
3348 result of executing these commands is that the target of the rule is
3349 brought up to date.
3351 Users use many different shell programs, but recipes in makefiles are
3352 always interpreted by @file{/bin/sh} unless the makefile specifies
3353 otherwise.  @xref{Execution, ,Command Execution}.
3355 @menu
3356 * Recipe Syntax::               Recipe syntax features and pitfalls.
3357 * Echoing::                     How to control when recipes are echoed.
3358 * Execution::                   How recipes are executed.
3359 * Parallel::                    How recipes can be executed in parallel.
3360 * Errors::                      What happens after a recipe execution error.
3361 * Interrupts::                  What happens when a recipe is interrupted.
3362 * Recursion::                   Invoking @code{make} from makefiles.
3363 * Sequences::                   Defining canned recipes.
3364 * Empty Recipes::               Defining useful, do-nothing recipes.
3365 @end menu
3367 @node Recipe Syntax, Echoing, Recipes, Recipes
3368 @section Recipe Syntax
3369 @cindex recipe syntax
3370 @cindex syntax of recipe
3372 Makefiles have the unusual property that there are really two distinct
3373 syntaxes in one file.  Most of the makefile uses @code{make} syntax
3374 (@pxref{Makefiles, ,Writing Makefiles}).  However, recipes are meant
3375 to be interpreted by the shell and so they are written using shell
3376 syntax.  The @code{make} program does not try to understand shell
3377 syntax: it performs only a very few specific translations on the
3378 content of the recipe before handing it to the shell.
3380 Each line in the recipe must start with a tab (or the first character
3381 in the value of the @code{.CMDPREFIX} variable; @pxref{Special
3382 Variables}), except that the first recipe line may be attached to the
3383 target-and-prerequisites line with a semicolon in between.  @emph{Any}
3384 line in the makefile that begins with a tab and appears in a ``rule
3385 context'' (that is, after a rule has been started until another rule
3386 or variable definition) will be considered part of a recipe for that
3387 rule.  Blank lines and lines of just comments may appear among the
3388 recipe lines; they are ignored.
3390 Some consequences of these rules include:
3392 @itemize @bullet
3393 @item
3394 A blank line that begins with a tab is not blank: it's an empty
3395 recipe (@pxref{Empty Recipes}).
3397 @cindex comments, in recipes
3398 @cindex recipes, comments in
3399 @cindex @code{#} (comments), in recipes
3400 @item
3401 A comment in a recipe is not a @code{make} comment; it will be
3402 passed to the shell as-is.  Whether the shell treats it as a comment
3403 or not depends on your shell.
3405 @item
3406 A variable definition in a ``rule context'' which is indented by a tab
3407 as the first character on the line, will be considered part of a
3408 recipe, not a @code{make} variable definition, and passed to the
3409 shell.
3411 @item
3412 A conditional expression (@code{ifdef}, @code{ifeq},
3413 etc. @pxref{Conditional Syntax, ,Syntax of Conditionals}) in a ``rule
3414 context'' which is indented by a tab as the first character on the
3415 line, will be considered part of a recipe and be passed to the shell.
3417 @end itemize
3419 @menu
3420 * Splitting Lines::             Breaking long recipe lines for readability.
3421 * Variables in Recipes::        Using @code{make} variables in recipes.
3422 @end menu
3424 @node Splitting Lines, Variables in Recipes, Recipe Syntax, Recipe Syntax
3425 @subsection Splitting Recipe Lines
3426 @cindex recipes, splitting
3427 @cindex splitting recipes
3428 @cindex recipes, backslash (@code{\}) in
3429 @cindex recipes, quoting newlines in
3430 @cindex backslash (@code{\}), in recipes
3431 @cindex @code{\} (backslash), in recipes
3432 @cindex quoting newline, in recipes
3433 @cindex newline, quoting, in recipes
3435 One of the few ways in which @code{make} does interpret recipes is
3436 checking for a backslash just before the newline.  As in normal
3437 makefile syntax, a single logical recipe line can be split into
3438 multiple physical lines in the makefile by placing a backslash before
3439 each newline.  A sequence of lines like this is considered a single
3440 recipe line, and one instance of the shell will be invoked to run it.
3442 However, in contrast to how they are treated in other places in a
3443 makefile, backslash-newline pairs are @emph{not} removed from the
3444 recipe.  Both the backslash and the newline characters are preserved
3445 and passed to the shell.  How the backslash-newline is interpreted
3446 depends on your shell.  If the first character of the next line after
3447 the backslash-newline is the recipe prefix character (a tab by
3448 default; @pxref{Special Variables}), then that character (and only
3449 that character) is removed.  Whitespace is never added to the recipe.
3451 For example, the recipe for the all target in this makefile:
3453 @example
3454 @group
3455 all :
3456         @@echo no\
3457 space
3458         @@echo no\
3459         space
3460         @@echo one \
3461         space
3462         @@echo one\
3463          space
3464 @end group
3465 @end example
3467 @noindent
3468 consists of four separate shell commands where the output is:
3470 @example
3471 @group
3472 nospace
3473 nospace
3474 one space
3475 one space
3476 @end group
3477 @end example
3479 As a more complex example, this makefile:
3481 @example
3482 @group
3483 all : ; @@echo 'hello \
3484         world' ; echo "hello \
3485     world"
3486 @end group
3487 @end example
3489 @noindent
3490 will invoke one shell with a command of:
3492 @example
3493 @group
3494 echo 'hello \
3495 world' ; echo "hello \
3496     world"
3497 @end group
3498 @end example
3500 @noindent
3501 which, according to shell quoting rules, will yield the following output:
3503 @example
3504 @group
3505 hello \
3506 world
3507 hello     world
3508 @end group
3509 @end example
3511 @noindent
3512 Notice how the backslash/newline pair was removed inside the string
3513 quoted with double quotes (@code{"@dots{}"}), but not from the string
3514 quoted with single quotes (@code{'@dots{}'}).  This is the way the
3515 default shell (@file{/bin/sh}) handles backslash/newline pairs.  If
3516 you specify a different shell in your makefiles it may treat them
3517 differently.
3519 Sometimes you want to split a long line inside of single quotes, but
3520 you don't want the backslash-newline to appear in the quoted content.
3521 This is often the case when passing scripts to languages such as Perl,
3522 where extraneous backslashes inside the script can change its meaning
3523 or even be a syntax error.  One simple way of handling this is to
3524 place the quoted string, or even the entire command, into a
3525 @code{make} variable then use the variable in the recipe.  In this
3526 situation the newline quoting rules for makefiles will be used, and
3527 the backslash-newline will be removed.  If we rewrite our example
3528 above using this method:
3530 @example
3531 @group
3532 HELLO = 'hello \
3533 world'
3535 all : ; @@echo $(HELLO)
3536 @end group
3537 @end example
3539 @noindent
3540 we will get output like this:
3542 @example
3543 @group
3544 hello world
3545 @end group
3546 @end example
3548 If you like, you can also use target-specific variables
3549 (@pxref{Target-specific, ,Target-specific Variable Values}) to obtain
3550 a tighter correspondence between the variable and the recipe that
3551 uses it.
3553 @node Variables in Recipes,  , Splitting Lines, Recipe Syntax
3554 @subsection Using Variables in Recipes
3555 @cindex variable references in recipes
3556 @cindex recipes, using variables in
3558 The other way in which @code{make} processes recipes is by expanding
3559 any variable references in them (@pxref{Reference,Basics of Variable
3560 References}).  This occurs after make has finished reading all the
3561 makefiles and the target is determined to be out of date; so, the
3562 recipes for targets which are not rebuilt are never expanded.
3564 Variable and function references in recipes have identical syntax and
3565 semantics to references elsewhere in the makefile.  They also have the
3566 same quoting rules: if you want a dollar sign to appear in your
3567 recipe, you must double it (@samp{$$}).  For shells like the default
3568 shell, that use dollar signs to introduce variables, it's important to
3569 keep clear in your mind whether the variable you want to reference is
3570 a @code{make} variable (use a single dollar sign) or a shell variable
3571 (use two dollar signs).  For example:
3573 @example
3574 @group
3575 LIST = one two three
3576 all:
3577         for i in $(LIST); do \
3578             echo $$i; \
3579         done
3580 @end group
3581 @end example
3583 @noindent
3584 results in the following command being passed to the shell:
3586 @example
3587 @group
3588 for i in one two three; do \
3589     echo $i; \
3590 done
3591 @end group
3592 @end example
3594 @noindent
3595 which generates the expected result:
3597 @example
3598 @group
3601 three
3602 @end group
3603 @end example
3605 @node Echoing, Execution, Recipe Syntax, Recipes
3606 @section Recipe Echoing
3607 @cindex echoing of recipes
3608 @cindex silent operation
3609 @cindex @code{@@} (in recipes)
3610 @cindex recipes, echoing
3611 @cindex printing of recipes
3613 Normally @code{make} prints each line of the recipe before it is
3614 executed.  We call this @dfn{echoing} because it gives the appearance
3615 that you are typing the lines yourself.
3617 When a line starts with @samp{@@}, the echoing of that line is suppressed.
3618 The @samp{@@} is discarded before the line is passed to the shell.
3619 Typically you would use this for a command whose only effect is to print
3620 something, such as an @code{echo} command to indicate progress through
3621 the makefile:
3623 @example
3624 @@echo About to make distribution files
3625 @end example
3627 @cindex @code{-n}
3628 @cindex @code{--just-print}
3629 @cindex @code{--dry-run}
3630 @cindex @code{--recon}
3631 When @code{make} is given the flag @samp{-n} or @samp{--just-print}
3632 it only echoes recipes, it won't execute them.  @xref{Options Summary,
3633 ,Summary of Options}.  In this case and only this case, even the
3634 recipe lines starting with @samp{@@} are printed.  This flag is useful for
3635 finding out which recipes @code{make} thinks are necessary without
3636 actually doing them.
3638 @cindex @code{-s}
3639 @cindex @code{--silent}
3640 @cindex @code{--quiet}
3641 @findex .SILENT
3642 The @samp{-s} or @samp{--silent}
3643 flag to @code{make} prevents all echoing, as if all recipes
3644 started with @samp{@@}.  A rule in the makefile for the special target
3645 @code{.SILENT} without prerequisites has the same effect
3646 (@pxref{Special Targets, ,Special Built-in Target Names}).
3647 @code{.SILENT} is essentially obsolete since @samp{@@} is more flexible.@refill
3649 @node Execution, Parallel, Echoing, Recipes
3650 @section Recipe Execution
3651 @cindex recipe, execution
3652 @cindex execution, of recipes
3653 @vindex @code{SHELL} @r{(recipe execution)}
3655 When it is time to execute recipes to update a target, they are
3656 executed by invoking a new subshell for each line of the recipe.  (In
3657 practice, @code{make} may take shortcuts that do not affect the
3658 results.)
3660 @cindex @code{cd} (shell command)
3661 @cindex shell variables, setting in recipes
3662 @cindex recipes setting shell variables
3663 @strong{Please note:} this implies that setting shell variables and
3664 invoking shell commands such as @code{cd} that set a context local to
3665 each process will not affect the following lines in the recipe.@footnote{On
3666 MS-DOS, the value of current working directory is @strong{global}, so
3667 changing it @emph{will} affect the following recipe lines on those
3668 systems.}  If you want to use @code{cd} to affect the next statement,
3669 put both statements in a single recipe line.  Then @code{make} will
3670 invoke one shell to run the entire line, and the shell will execute
3671 the statements in sequence.  For example:
3673 @example
3674 foo : bar/lose
3675         cd $(@@D) && gobble $(@@F) > ../$@@
3676 @end example
3678 @noindent
3679 Here we use the shell AND operator (@code{&&}) so that if the
3680 @code{cd} command fails, the script will fail without trying to invoke
3681 the @code{gobble} command in the wrong directory, which could cause
3682 problems (in this case it would certainly cause @file{../foo} to be
3683 truncated, at least).
3685 @menu
3686 * Choosing the Shell::          How @code{make} chooses the shell used
3687                                   to run recipes.
3688 @end menu
3690 @node Choosing the Shell,  , Execution, Execution
3691 @subsection Choosing the Shell
3692 @cindex shell, choosing the
3693 @cindex @code{SHELL}, value of
3695 @vindex SHELL
3696 The program used as the shell is taken from the variable @code{SHELL}.
3697 If this variable is not set in your makefile, the program
3698 @file{/bin/sh} is used as the shell.
3700 @cindex environment, @code{SHELL} in
3701 Unlike most variables, the variable @code{SHELL} is never set from the
3702 environment.  This is because the @code{SHELL} environment variable is
3703 used to specify your personal choice of shell program for interactive
3704 use.  It would be very bad for personal choices like this to affect the
3705 functioning of makefiles.  @xref{Environment, ,Variables from the
3706 Environment}.
3708 Furthermore, when you do set @code{SHELL} in your makefile that value
3709 is @emph{not} exported in the environment to recipe lines that
3710 @code{make} invokes.  Instead, the value inherited from the user's
3711 environment, if any, is exported.  You can override this behavior by
3712 explicitly exporting @code{SHELL} (@pxref{Variables/Recursion,
3713 ,Communicating Variables to a Sub-@code{make}}), forcing it to be
3714 passed in the environment to recipe lines.
3716 @vindex @code{MAKESHELL} @r{(MS-DOS alternative to @code{SHELL})}
3717 However, on MS-DOS and MS-Windows the value of @code{SHELL} in the
3718 environment @strong{is} used, since on those systems most users do not
3719 set this variable, and therefore it is most likely set specifically to
3720 be used by @code{make}.  On MS-DOS, if the setting of @code{SHELL} is
3721 not suitable for @code{make}, you can set the variable
3722 @code{MAKESHELL} to the shell that @code{make} should use; if set it
3723 will be used as the shell instead of the value of @code{SHELL}.
3725 @subsubheading Choosing a Shell in DOS and Windows
3726 @cindex shell, in DOS and Windows
3727 @cindex DOS, choosing a shell in
3728 @cindex Windows, choosing a shell in
3730 Choosing a shell in MS-DOS and MS-Windows is much more complex than on
3731 other systems.
3733 @vindex COMSPEC
3734 On MS-DOS, if @code{SHELL} is not set, the value of the variable
3735 @code{COMSPEC} (which is always set) is used instead.
3737 @cindex @code{SHELL}, MS-DOS specifics
3738 The processing of lines that set the variable @code{SHELL} in Makefiles
3739 is different on MS-DOS.  The stock shell, @file{command.com}, is
3740 ridiculously limited in its functionality and many users of @code{make}
3741 tend to install a replacement shell.  Therefore, on MS-DOS, @code{make}
3742 examines the value of @code{SHELL}, and changes its behavior based on
3743 whether it points to a Unix-style or DOS-style shell.  This allows
3744 reasonable functionality even if @code{SHELL} points to
3745 @file{command.com}.
3747 If @code{SHELL} points to a Unix-style shell, @code{make} on MS-DOS
3748 additionally checks whether that shell can indeed be found; if not, it
3749 ignores the line that sets @code{SHELL}.  In MS-DOS, GNU @code{make}
3750 searches for the shell in the following places:
3752 @enumerate
3753 @item
3754 In the precise place pointed to by the value of @code{SHELL}.  For
3755 example, if the makefile specifies @samp{SHELL = /bin/sh}, @code{make}
3756 will look in the directory @file{/bin} on the current drive.
3758 @item
3759 In the current directory.
3761 @item
3762 In each of the directories in the @code{PATH} variable, in order.
3764 @end enumerate
3766 In every directory it examines, @code{make} will first look for the
3767 specific file (@file{sh} in the example above).  If this is not found,
3768 it will also look in that directory for that file with one of the known
3769 extensions which identify executable files.  For example @file{.exe},
3770 @file{.com}, @file{.bat}, @file{.btm}, @file{.sh}, and some others.
3772 If any of these attempts is successful, the value of @code{SHELL} will
3773 be set to the full pathname of the shell as found.  However, if none of
3774 these is found, the value of @code{SHELL} will not be changed, and thus
3775 the line that sets it will be effectively ignored.  This is so
3776 @code{make} will only support features specific to a Unix-style shell if
3777 such a shell is actually installed on the system where @code{make} runs.
3779 Note that this extended search for the shell is limited to the cases
3780 where @code{SHELL} is set from the Makefile; if it is set in the
3781 environment or command line, you are expected to set it to the full
3782 pathname of the shell, exactly as things are on Unix.
3784 The effect of the above DOS-specific processing is that a Makefile that
3785 contains @samp{SHELL = /bin/sh} (as many Unix makefiles do), will work
3786 on MS-DOS unaltered if you have e.g.@: @file{sh.exe} installed in some
3787 directory along your @code{PATH}.
3789 @node Parallel, Errors, Execution, Recipes
3790 @section Parallel Execution
3791 @cindex recipes, execution in parallel
3792 @cindex parallel execution
3793 @cindex execution, in parallel
3794 @cindex job slots
3795 @cindex @code{-j}
3796 @cindex @code{--jobs}
3798 GNU @code{make} knows how to execute several recipes at once.
3799 Normally, @code{make} will execute only one recipe at a time, waiting
3800 for it to finish before executing the next.  However, the @samp{-j} or
3801 @samp{--jobs} option tells @code{make} to execute many recipes
3802 simultaneously.  You can inhibit parallelism in a particular makefile
3803 with the @code{.NOTPARALLEL} pseudo-target (@pxref{Special
3804 Targets,Special Built-in Target Names}).@refill
3806 On MS-DOS, the @samp{-j} option has no effect, since that system doesn't
3807 support multi-processing.
3809 If the @samp{-j} option is followed by an integer, this is the number of
3810 recipes to execute at once; this is called the number of @dfn{job slots}.
3811 If there is nothing looking like an integer after the @samp{-j} option,
3812 there is no limit on the number of job slots.  The default number of job
3813 slots is one, which means serial execution (one thing at a time).
3815 One unpleasant consequence of running several recipes simultaneously is
3816 that output generated by the recipes appears whenever each recipe
3817 sends it, so messages from different recipes may be interspersed.
3819 Another problem is that two processes cannot both take input from the
3820 same device; so to make sure that only one recipe tries to take input
3821 from the terminal at once, @code{make} will invalidate the standard
3822 input streams of all but one running recipe.  This means that
3823 attempting to read from standard input will usually be a fatal error (a
3824 @samp{Broken pipe} signal) for most child processes if there are
3825 several.
3826 @cindex broken pipe
3827 @cindex standard input
3829 It is unpredictable which recipe will have a valid standard input stream
3830 (which will come from the terminal, or wherever you redirect the standard
3831 input of @code{make}).  The first recipe run will always get it first, and
3832 the first recipe started after that one finishes will get it next, and so
3835 We will change how this aspect of @code{make} works if we find a better
3836 alternative.  In the mean time, you should not rely on any recipe using
3837 standard input at all if you are using the parallel execution feature; but
3838 if you are not using this feature, then standard input works normally in
3839 all recipes.
3841 Finally, handling recursive @code{make} invocations raises issues.  For
3842 more information on this, see
3843 @ref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
3845 If a recipe fails (is killed by a signal or exits with a nonzero
3846 status), and errors are not ignored for that recipe
3847 (@pxref{Errors, ,Errors in Recipes}),
3848 the remaining recipe lines to remake the same target will not be run.
3849 If a recipe fails and the @samp{-k} or @samp{--keep-going}
3850 option was not given
3851 (@pxref{Options Summary, ,Summary of Options}),
3852 @code{make} aborts execution.  If make
3853 terminates for any reason (including a signal) with child processes
3854 running, it waits for them to finish before actually exiting.@refill
3856 @cindex load average
3857 @cindex limiting jobs based on load
3858 @cindex jobs, limiting based on load
3859 @cindex @code{-l} (load average)
3860 @cindex @code{--max-load}
3861 @cindex @code{--load-average}
3862 When the system is heavily loaded, you will probably want to run fewer jobs
3863 than when it is lightly loaded.  You can use the @samp{-l} option to tell
3864 @code{make} to limit the number of jobs to run at once, based on the load
3865 average.  The @samp{-l} or @samp{--max-load}
3866 option is followed by a floating-point number.  For
3867 example,
3869 @example
3870 -l 2.5
3871 @end example
3873 @noindent
3874 will not let @code{make} start more than one job if the load average is
3875 above 2.5.  The @samp{-l} option with no following number removes the
3876 load limit, if one was given with a previous @samp{-l} option.@refill
3878 More precisely, when @code{make} goes to start up a job, and it already has
3879 at least one job running, it checks the current load average; if it is not
3880 lower than the limit given with @samp{-l}, @code{make} waits until the load
3881 average goes below that limit, or until all the other jobs finish.
3883 By default, there is no load limit.
3885 @node Errors, Interrupts, Parallel, Recipes
3886 @section Errors in Recipes
3887 @cindex errors (in recipes)
3888 @cindex recipes, errors in
3889 @cindex exit status (errors)
3891 After each shell invocation returns, @code{make} looks at its exit
3892 status.  If the shell completed successfully (the exit status is
3893 zero), the next line in the recipe is executed in a new shell; after
3894 the last line is finished, the rule is finished.
3896 If there is an error (the exit status is nonzero), @code{make} gives up on
3897 the current rule, and perhaps on all rules.
3899 Sometimes the failure of a certain recipe line does not indicate a problem.
3900 For example, you may use the @code{mkdir} command to ensure that a
3901 directory exists.  If the directory already exists, @code{mkdir} will
3902 report an error, but you probably want @code{make} to continue regardless.
3904 @cindex @code{-} (in recipes)
3905 To ignore errors in a recipe line, write a @samp{-} at the beginning
3906 of the line's text (after the initial tab).  The @samp{-} is discarded
3907 before the line is passed to the shell for execution.
3909 For example,
3911 @example
3912 @group
3913 clean:
3914         -rm -f *.o
3915 @end group
3916 @end example
3917 @cindex @code{rm} (shell command)
3919 @noindent
3920 This causes @code{make} to continue even if @code{rm} is unable to
3921 remove a file.
3923 @cindex @code{-i}
3924 @cindex @code{--ignore-errors}
3925 @findex .IGNORE
3926 When you run @code{make} with the @samp{-i} or @samp{--ignore-errors}
3927 flag, errors are ignored in all recipes of all rules.  A rule in the
3928 makefile for the special target @code{.IGNORE} has the same effect, if
3929 there are no prerequisites.  These ways of ignoring errors are obsolete
3930 because @samp{-} is more flexible.
3932 When errors are to be ignored, because of either a @samp{-} or the
3933 @samp{-i} flag, @code{make} treats an error return just like success,
3934 except that it prints out a message that tells you the status code
3935 the shell exited with, and says that the error has been ignored.
3937 When an error happens that @code{make} has not been told to ignore,
3938 it implies that the current target cannot be correctly remade, and neither
3939 can any other that depends on it either directly or indirectly.  No further
3940 recipes will be executed for these targets, since their preconditions
3941 have not been achieved.
3944 @cindex @code{-k}
3945 @cindex @code{--keep-going}
3946 Normally @code{make} gives up immediately in this circumstance, returning a
3947 nonzero status.  However, if the @samp{-k} or @samp{--keep-going}
3948 flag is specified, @code{make}
3949 continues to consider the other prerequisites of the pending targets,
3950 remaking them if necessary, before it gives up and returns nonzero status.
3951 For example, after an error in compiling one object file, @samp{make -k}
3952 will continue compiling other object files even though it already knows
3953 that linking them will be impossible.  @xref{Options Summary, ,Summary of Options}.
3955 The usual behavior assumes that your purpose is to get the specified
3956 targets up to date; once @code{make} learns that this is impossible, it
3957 might as well report the failure immediately.  The @samp{-k} option says
3958 that the real purpose is to test as many of the changes made in the
3959 program as possible, perhaps to find several independent problems so
3960 that you can correct them all before the next attempt to compile.  This
3961 is why Emacs' @code{compile} command passes the @samp{-k} flag by
3962 default.
3963 @cindex Emacs (@code{M-x compile})
3965 @findex .DELETE_ON_ERROR
3966 @cindex deletion of target files
3967 @cindex removal of target files
3968 @cindex target, deleting on error
3969 Usually when a recipe line fails, if it has changed the target file at all,
3970 the file is corrupted and cannot be used---or at least it is not
3971 completely updated.  Yet the file's time stamp says that it is now up to
3972 date, so the next time @code{make} runs, it will not try to update that
3973 file.  The situation is just the same as when the shell is killed by a
3974 signal; @pxref{Interrupts}.  So generally the right thing to do is to
3975 delete the target file if the recipe fails after beginning to change
3976 the file.  @code{make} will do this if @code{.DELETE_ON_ERROR} appears
3977 as a target.  This is almost always what you want @code{make} to do, but
3978 it is not historical practice; so for compatibility, you must explicitly
3979 request it.
3981 @node Interrupts, Recursion, Errors, Recipes
3982 @section Interrupting or Killing @code{make}
3983 @cindex interrupt
3984 @cindex signal
3985 @cindex deletion of target files
3986 @cindex removal of target files
3987 @cindex target, deleting on interrupt
3988 @cindex killing (interruption)
3990 If @code{make} gets a fatal signal while a shell is executing, it may
3991 delete the target file that the recipe was supposed to update.  This is
3992 done if the target file's last-modification time has changed since
3993 @code{make} first checked it.
3995 The purpose of deleting the target is to make sure that it is remade from
3996 scratch when @code{make} is next run.  Why is this?  Suppose you type
3997 @kbd{Ctrl-c} while a compiler is running, and it has begun to write an
3998 object file @file{foo.o}.  The @kbd{Ctrl-c} kills the compiler, resulting
3999 in an incomplete file whose last-modification time is newer than the source
4000 file @file{foo.c}.  But @code{make} also receives the @kbd{Ctrl-c} signal
4001 and deletes this incomplete file.  If @code{make} did not do this, the next
4002 invocation of @code{make} would think that @file{foo.o} did not require
4003 updating---resulting in a strange error message from the linker when it
4004 tries to link an object file half of which is missing.
4006 @findex .PRECIOUS
4007 You can prevent the deletion of a target file in this way by making the
4008 special target @code{.PRECIOUS} depend on it.  Before remaking a target,
4009 @code{make} checks to see whether it appears on the prerequisites of
4010 @code{.PRECIOUS}, and thereby decides whether the target should be deleted
4011 if a signal happens.  Some reasons why you might do this are that the
4012 target is updated in some atomic fashion, or exists only to record a
4013 modification-time (its contents do not matter), or must exist at all
4014 times to prevent other sorts of trouble.
4016 @node Recursion, Sequences, Interrupts, Recipes
4017 @section Recursive Use of @code{make}
4018 @cindex recursion
4019 @cindex subdirectories, recursion for
4021 Recursive use of @code{make} means using @code{make} as a command in a
4022 makefile.  This technique is useful when you want separate makefiles for
4023 various subsystems that compose a larger system.  For example, suppose you
4024 have a subdirectory @file{subdir} which has its own makefile, and you would
4025 like the containing directory's makefile to run @code{make} on the
4026 subdirectory.  You can do it by writing this:
4028 @example
4029 subsystem:
4030         cd subdir && $(MAKE)
4031 @end example
4033 @noindent
4034 or, equivalently, this (@pxref{Options Summary, ,Summary of Options}):
4036 @example
4037 subsystem:
4038         $(MAKE) -C subdir
4039 @end example
4040 @cindex @code{-C}
4041 @cindex @code{--directory}
4043 You can write recursive @code{make} commands just by copying this example,
4044 but there are many things to know about how they work and why, and about
4045 how the sub-@code{make} relates to the top-level @code{make}.  You may
4046 also find it useful to declare targets that invoke recursive
4047 @code{make} commands as @samp{.PHONY} (for more discussion on when
4048 this is useful, see @ref{Phony Targets}).
4050 @vindex @code{CURDIR}
4051 For your convenience, when GNU @code{make} starts (after it has
4052 processed any @code{-C} options) it sets the variable @code{CURDIR} to
4053 the pathname of the current working directory.  This value is never
4054 touched by @code{make} again: in particular note that if you include
4055 files from other directories the value of @code{CURDIR} does not
4056 change.  The value has the same precedence it would have if it were
4057 set in the makefile (by default, an environment variable @code{CURDIR}
4058 will not override this value).  Note that setting this variable has no
4059 impact on the operation of @code{make} (it does not cause @code{make}
4060 to change its working directory, for example).
4062 @menu
4063 * MAKE Variable::               The special effects of using @samp{$(MAKE)}.
4064 * Variables/Recursion::         How to communicate variables to a sub-@code{make}.
4065 * Options/Recursion::           How to communicate options to a sub-@code{make}.
4066 * -w Option::                   How the @samp{-w} or @samp{--print-directory} option
4067                                   helps debug use of recursive @code{make} commands.
4068 @end menu
4070 @node MAKE Variable, Variables/Recursion, Recursion, Recursion
4071 @subsection How the @code{MAKE} Variable Works
4072 @vindex MAKE
4073 @cindex recursion, and @code{MAKE} variable
4075 Recursive @code{make} commands should always use the variable @code{MAKE},
4076 not the explicit command name @samp{make}, as shown here:
4078 @example
4079 @group
4080 subsystem:
4081         cd subdir && $(MAKE)
4082 @end group
4083 @end example
4085 The value of this variable is the file name with which @code{make} was
4086 invoked.  If this file name was @file{/bin/make}, then the recipe executed
4087 is @samp{cd subdir && /bin/make}.  If you use a special version of
4088 @code{make} to run the top-level makefile, the same special version will be
4089 executed for recursive invocations.
4090 @cindex @code{cd} (shell command)
4092 @cindex +, and recipes
4093 As a special feature, using the variable @code{MAKE} in the recipe of
4094 a rule alters the effects of the @samp{-t} (@samp{--touch}), @samp{-n}
4095 (@samp{--just-print}), or @samp{-q} (@w{@samp{--question}}) option.
4096 Using the @code{MAKE} variable has the same effect as using a @samp{+}
4097 character at the beginning of the recipe line.  @xref{Instead of
4098 Execution, ,Instead of Executing the Recipes}.  This special feature
4099 is only enabled if the @code{MAKE} variable appears directly in the
4100 recipe: it does not apply if the @code{MAKE} variable is referenced
4101 through expansion of another variable.  In the latter case you must
4102 use the @samp{+} token to get these special effects.@refill
4104 Consider the command @samp{make -t} in the above example.  (The
4105 @samp{-t} option marks targets as up to date without actually running
4106 any recipes; see @ref{Instead of Execution}.)  Following the usual
4107 definition of @samp{-t}, a @samp{make -t} command in the example would
4108 create a file named @file{subsystem} and do nothing else.  What you
4109 really want it to do is run @samp{@w{cd subdir &&} @w{make -t}}; but
4110 that would require executing the recipe, and @samp{-t} says not to
4111 execute recipes.@refill
4112 @cindex @code{-t}, and recursion
4113 @cindex recursion, and @code{-t}
4114 @cindex @code{--touch}, and recursion
4116 The special feature makes this do what you want: whenever a recipe
4117 line of a rule contains the variable @code{MAKE}, the flags @samp{-t},
4118 @samp{-n} and @samp{-q} do not apply to that line.  Recipe lines
4119 containing @code{MAKE} are executed normally despite the presence of a
4120 flag that causes most recipes not to be run.  The usual
4121 @code{MAKEFLAGS} mechanism passes the flags to the sub-@code{make}
4122 (@pxref{Options/Recursion, ,Communicating Options to a
4123 Sub-@code{make}}), so your request to touch the files, or print the
4124 recipes, is propagated to the subsystem.@refill
4126 @node Variables/Recursion, Options/Recursion, MAKE Variable, Recursion
4127 @subsection Communicating Variables to a Sub-@code{make}
4128 @cindex sub-@code{make}
4129 @cindex environment, and recursion
4130 @cindex exporting variables
4131 @cindex variables, environment
4132 @cindex variables, exporting
4133 @cindex recursion, and environment
4134 @cindex recursion, and variables
4136 Variable values of the top-level @code{make} can be passed to the
4137 sub-@code{make} through the environment by explicit request.  These
4138 variables are defined in the sub-@code{make} as defaults, but do not
4139 override what is specified in the makefile used by the sub-@code{make}
4140 makefile unless you use the @samp{-e} switch (@pxref{Options Summary,
4141 ,Summary of Options}).@refill
4143 To pass down, or @dfn{export}, a variable, @code{make} adds the
4144 variable and its value to the environment for running each line of the
4145 recipe.  The sub-@code{make}, in turn, uses the environment to
4146 initialize its table of variable values.  @xref{Environment,
4147 ,Variables from the Environment}.
4149 Except by explicit request, @code{make} exports a variable only if it
4150 is either defined in the environment initially or set on the command
4151 line, and if its name consists only of letters, numbers, and underscores.
4152 Some shells cannot cope with environment variable names consisting of
4153 characters other than letters, numbers, and underscores.
4155 @cindex SHELL, exported value
4156 The value of the @code{make} variable @code{SHELL} is not exported.
4157 Instead, the value of the @code{SHELL} variable from the invoking
4158 environment is passed to the sub-@code{make}.  You can force
4159 @code{make} to export its value for @code{SHELL} by using the
4160 @code{export} directive, described below.  @xref{Choosing the Shell}.
4162 The special variable @code{MAKEFLAGS} is always exported (unless you
4163 unexport it).  @code{MAKEFILES} is exported if you set it to anything.
4165 @code{make} automatically passes down variable values that were defined
4166 on the command line, by putting them in the @code{MAKEFLAGS} variable.
4167 @iftex
4168 See the next section.
4169 @end iftex
4170 @ifnottex
4171 @xref{Options/Recursion}.
4172 @end ifnottex
4174 Variables are @emph{not} normally passed down if they were created by
4175 default by @code{make} (@pxref{Implicit Variables, ,Variables Used by
4176 Implicit Rules}).  The sub-@code{make} will define these for
4177 itself.@refill
4179 @findex export
4180 If you want to export specific variables to a sub-@code{make}, use the
4181 @code{export} directive, like this:
4183 @example
4184 export @var{variable} @dots{}
4185 @end example
4187 @noindent
4188 @findex unexport
4189 If you want to @emph{prevent} a variable from being exported, use the
4190 @code{unexport} directive, like this:
4192 @example
4193 unexport @var{variable} @dots{}
4194 @end example
4196 @noindent
4197 In both of these forms, the arguments to @code{export} and
4198 @code{unexport} are expanded, and so could be variables or functions
4199 which expand to a (list of) variable names to be (un)exported.
4201 As a convenience, you can define a variable and export it at the same
4202 time by doing:
4204 @example
4205 export @var{variable} = value
4206 @end example
4208 @noindent
4209 has the same result as:
4211 @example
4212 @var{variable} = value
4213 export @var{variable}
4214 @end example
4216 @noindent
4219 @example
4220 export @var{variable} := value
4221 @end example
4223 @noindent
4224 has the same result as:
4226 @example
4227 @var{variable} := value
4228 export @var{variable}
4229 @end example
4231 Likewise,
4233 @example
4234 export @var{variable} += value
4235 @end example
4237 @noindent
4238 is just like:
4240 @example
4241 @var{variable} += value
4242 export @var{variable}
4243 @end example
4245 @noindent
4246 @xref{Appending, ,Appending More Text to Variables}.
4248 You may notice that the @code{export} and @code{unexport} directives
4249 work in @code{make} in the same way they work in the shell, @code{sh}.
4251 If you want all variables to be exported by default, you can use
4252 @code{export} by itself:
4254 @example
4255 export
4256 @end example
4258 @noindent
4259 This tells @code{make} that variables which are not explicitly mentioned
4260 in an @code{export} or @code{unexport} directive should be exported.
4261 Any variable given in an @code{unexport} directive will still @emph{not}
4262 be exported.  If you use @code{export} by itself to export variables by
4263 default, variables whose names contain characters other than
4264 alphanumerics and underscores will not be exported unless specifically
4265 mentioned in an @code{export} directive.@refill
4267 @findex .EXPORT_ALL_VARIABLES
4268 The behavior elicited by an @code{export} directive by itself was the
4269 default in older versions of GNU @code{make}.  If your makefiles depend
4270 on this behavior and you want to be compatible with old versions of
4271 @code{make}, you can write a rule for the special target
4272 @code{.EXPORT_ALL_VARIABLES} instead of using the @code{export} directive.
4273 This will be ignored by old @code{make}s, while the @code{export}
4274 directive will cause a syntax error.@refill
4275 @cindex compatibility in exporting
4277 Likewise, you can use @code{unexport} by itself to tell @code{make}
4278 @emph{not} to export variables by default.  Since this is the default
4279 behavior, you would only need to do this if @code{export} had been used
4280 by itself earlier (in an included makefile, perhaps).  You
4281 @strong{cannot} use @code{export} and @code{unexport} by themselves to
4282 have variables exported for some recipes and not for others.  The last
4283 @code{export} or @code{unexport} directive that appears by itself
4284 determines the behavior for the entire run of @code{make}.@refill
4286 @vindex MAKELEVEL
4287 @cindex recursion, level of
4288 As a special feature, the variable @code{MAKELEVEL} is changed when it
4289 is passed down from level to level.  This variable's value is a string
4290 which is the depth of the level as a decimal number.  The value is
4291 @samp{0} for the top-level @code{make}; @samp{1} for a sub-@code{make},
4292 @samp{2} for a sub-sub-@code{make}, and so on.  The incrementation
4293 happens when @code{make} sets up the environment for a recipe.@refill
4295 The main use of @code{MAKELEVEL} is to test it in a conditional
4296 directive (@pxref{Conditionals, ,Conditional Parts of Makefiles}); this
4297 way you can write a makefile that behaves one way if run recursively and
4298 another way if run directly by you.@refill
4300 @vindex MAKEFILES
4301 You can use the variable @code{MAKEFILES} to cause all sub-@code{make}
4302 commands to use additional makefiles.  The value of @code{MAKEFILES} is
4303 a whitespace-separated list of file names.  This variable, if defined in
4304 the outer-level makefile, is passed down through the environment; then
4305 it serves as a list of extra makefiles for the sub-@code{make} to read
4306 before the usual or specified ones.  @xref{MAKEFILES Variable, ,The
4307 Variable @code{MAKEFILES}}.@refill
4309 @node Options/Recursion, -w Option, Variables/Recursion, Recursion
4310 @subsection Communicating Options to a Sub-@code{make}
4311 @cindex options, and recursion
4312 @cindex recursion, and options
4314 @vindex MAKEFLAGS
4315 Flags such as @samp{-s} and @samp{-k} are passed automatically to the
4316 sub-@code{make} through the variable @code{MAKEFLAGS}.  This variable is
4317 set up automatically by @code{make} to contain the flag letters that
4318 @code{make} received.  Thus, if you do @w{@samp{make -ks}} then
4319 @code{MAKEFLAGS} gets the value @samp{ks}.@refill
4321 As a consequence, every sub-@code{make} gets a value for @code{MAKEFLAGS}
4322 in its environment.  In response, it takes the flags from that value and
4323 processes them as if they had been given as arguments.
4324 @xref{Options Summary, ,Summary of Options}.
4326 @cindex command line variable definitions, and recursion
4327 @cindex variables, command line, and recursion
4328 @cindex recursion, and command line variable definitions
4329 Likewise variables defined on the command line are passed to the
4330 sub-@code{make} through @code{MAKEFLAGS}.  Words in the value of
4331 @code{MAKEFLAGS} that contain @samp{=}, @code{make} treats as variable
4332 definitions just as if they appeared on the command line.
4333 @xref{Overriding, ,Overriding Variables}.
4335 @cindex @code{-C}, and recursion
4336 @cindex @code{-f}, and recursion
4337 @cindex @code{-o}, and recursion
4338 @cindex @code{-W}, and recursion
4339 @cindex @code{--directory}, and recursion
4340 @cindex @code{--file}, and recursion
4341 @cindex @code{--old-file}, and recursion
4342 @cindex @code{--assume-old}, and recursion
4343 @cindex @code{--assume-new}, and recursion
4344 @cindex @code{--new-file}, and recursion
4345 @cindex recursion, and @code{-C}
4346 @cindex recursion, and @code{-f}
4347 @cindex recursion, and @code{-o}
4348 @cindex recursion, and @code{-W}
4349 The options @samp{-C}, @samp{-f}, @samp{-o}, and @samp{-W} are not put
4350 into @code{MAKEFLAGS}; these options are not passed down.@refill
4352 @cindex @code{-j}, and recursion
4353 @cindex @code{--jobs}, and recursion
4354 @cindex recursion, and @code{-j}
4355 @cindex job slots, and recursion
4356 The @samp{-j} option is a special case (@pxref{Parallel, ,Parallel Execution}).
4357 If you set it to some numeric value @samp{N} and your operating system
4358 supports it (most any UNIX system will; others typically won't), the
4359 parent @code{make} and all the sub-@code{make}s will communicate to
4360 ensure that there are only @samp{N} jobs running at the same time
4361 between them all.  Note that any job that is marked recursive
4362 (@pxref{Instead of Execution, ,Instead of Executing the Recipes})
4363 doesn't count against the total jobs (otherwise we could get @samp{N}
4364 sub-@code{make}s running and have no slots left over for any real work!)
4366 If your operating system doesn't support the above communication, then
4367 @samp{-j 1} is always put into @code{MAKEFLAGS} instead of the value you
4368 specified.  This is because if the @w{@samp{-j}} option were passed down
4369 to sub-@code{make}s, you would get many more jobs running in parallel
4370 than you asked for.  If you give @samp{-j} with no numeric argument,
4371 meaning to run as many jobs as possible in parallel, this is passed
4372 down, since multiple infinities are no more than one.@refill
4374 If you do not want to pass the other flags down, you must change the
4375 value of @code{MAKEFLAGS}, like this:
4377 @example
4378 subsystem:
4379         cd subdir && $(MAKE) MAKEFLAGS=
4380 @end example
4382 @vindex MAKEOVERRIDES
4383 The command line variable definitions really appear in the variable
4384 @code{MAKEOVERRIDES}, and @code{MAKEFLAGS} contains a reference to this
4385 variable.  If you do want to pass flags down normally, but don't want to
4386 pass down the command line variable definitions, you can reset
4387 @code{MAKEOVERRIDES} to empty, like this:
4389 @example
4390 MAKEOVERRIDES =
4391 @end example
4393 @noindent
4394 @cindex Arg list too long
4395 @cindex E2BIG
4396 This is not usually useful to do.  However, some systems have a small
4397 fixed limit on the size of the environment, and putting so much
4398 information into the value of @code{MAKEFLAGS} can exceed it.  If you
4399 see the error message @samp{Arg list too long}, this may be the problem.
4400 @findex .POSIX
4401 @cindex POSIX.2
4402 (For strict compliance with POSIX.2, changing @code{MAKEOVERRIDES} does
4403 not affect @code{MAKEFLAGS} if the special target @samp{.POSIX} appears
4404 in the makefile.  You probably do not care about this.)
4406 @vindex MFLAGS
4407 A similar variable @code{MFLAGS} exists also, for historical
4408 compatibility.  It has the same value as @code{MAKEFLAGS} except that it
4409 does not contain the command line variable definitions, and it always
4410 begins with a hyphen unless it is empty (@code{MAKEFLAGS} begins with a
4411 hyphen only when it begins with an option that has no single-letter
4412 version, such as @samp{--warn-undefined-variables}).  @code{MFLAGS} was
4413 traditionally used explicitly in the recursive @code{make} command, like
4414 this:
4416 @example
4417 subsystem:
4418         cd subdir && $(MAKE) $(MFLAGS)
4419 @end example
4421 @noindent
4422 but now @code{MAKEFLAGS} makes this usage redundant.  If you want your
4423 makefiles to be compatible with old @code{make} programs, use this
4424 technique; it will work fine with more modern @code{make} versions too.
4426 @cindex setting options from environment
4427 @cindex options, setting from environment
4428 @cindex setting options in makefiles
4429 @cindex options, setting in makefiles
4430 The @code{MAKEFLAGS} variable can also be useful if you want to have
4431 certain options, such as @samp{-k} (@pxref{Options Summary, ,Summary of
4432 Options}), set each time you run @code{make}.  You simply put a value for
4433 @code{MAKEFLAGS} in your environment.  You can also set @code{MAKEFLAGS} in
4434 a makefile, to specify additional flags that should also be in effect for
4435 that makefile.  (Note that you cannot use @code{MFLAGS} this way.  That
4436 variable is set only for compatibility; @code{make} does not interpret a
4437 value you set for it in any way.)
4439 When @code{make} interprets the value of @code{MAKEFLAGS} (either from the
4440 environment or from a makefile), it first prepends a hyphen if the value
4441 does not already begin with one.  Then it chops the value into words
4442 separated by blanks, and parses these words as if they were options given
4443 on the command line (except that @samp{-C}, @samp{-f}, @samp{-h},
4444 @samp{-o}, @samp{-W}, and their long-named versions are ignored; and there
4445 is no error for an invalid option).
4447 If you do put @code{MAKEFLAGS} in your environment, you should be sure not
4448 to include any options that will drastically affect the actions of
4449 @code{make} and undermine the purpose of makefiles and of @code{make}
4450 itself.  For instance, the @samp{-t}, @samp{-n}, and @samp{-q} options, if
4451 put in one of these variables, could have disastrous consequences and would
4452 certainly have at least surprising and probably annoying effects.@refill
4454 @node -w Option,  , Options/Recursion, Recursion
4455 @subsection The @samp{--print-directory} Option
4456 @cindex directories, printing them
4457 @cindex printing directories
4458 @cindex recursion, and printing directories
4460 If you use several levels of recursive @code{make} invocations, the
4461 @samp{-w} or @w{@samp{--print-directory}} option can make the output a
4462 lot easier to understand by showing each directory as @code{make}
4463 starts processing it and as @code{make} finishes processing it.  For
4464 example, if @samp{make -w} is run in the directory @file{/u/gnu/make},
4465 @code{make} will print a line of the form:@refill
4467 @example
4468 make: Entering directory `/u/gnu/make'.
4469 @end example
4471 @noindent
4472 before doing anything else, and a line of the form:
4474 @example
4475 make: Leaving directory `/u/gnu/make'.
4476 @end example
4478 @noindent
4479 when processing is completed.
4481 @cindex @code{-C}, and @code{-w}
4482 @cindex @code{--directory}, and @code{--print-directory}
4483 @cindex recursion, and @code{-w}
4484 @cindex @code{-w}, and @code{-C}
4485 @cindex @code{-w}, and recursion
4486 @cindex @code{--print-directory}, and @code{--directory}
4487 @cindex @code{--print-directory}, and recursion
4488 @cindex @code{--no-print-directory}
4489 @cindex @code{--print-directory}, disabling
4490 @cindex @code{-w}, disabling
4491 Normally, you do not need to specify this option because @samp{make}
4492 does it for you: @samp{-w} is turned on automatically when you use the
4493 @samp{-C} option, and in sub-@code{make}s.  @code{make} will not
4494 automatically turn on @samp{-w} if you also use @samp{-s}, which says to
4495 be silent, or if you use @samp{--no-print-directory} to explicitly
4496 disable it.
4498 @node Sequences, Empty Recipes, Recursion, Recipes
4499 @section Defining Canned Recipes
4500 @cindex canned recipes
4501 @cindex recipes, canned
4502 @cindex sequences of commands
4503 @cindex commands, sequences of
4505 When the same sequence of commands is useful in making various
4506 targets, you can define it as a canned sequence with the @code{define}
4507 directive, and refer to the canned sequence from the recipes for those
4508 targets.  The canned sequence is actually a variable, so the name must
4509 not conflict with other variable names.
4511 Here is an example of defining a canned recipes:
4513 @example
4514 define run-yacc
4515 yacc $(firstword $^)
4516 mv y.tab.c $@@
4517 endef
4518 @end example
4519 @cindex @code{yacc}
4521 @noindent
4522 Here @code{run-yacc} is the name of the variable being defined;
4523 @code{endef} marks the end of the definition; the lines in between are the
4524 commands.  The @code{define} directive does not expand variable references
4525 and function calls in the canned sequence; the @samp{$} characters,
4526 parentheses, variable names, and so on, all become part of the value of the
4527 variable you are defining.
4528 @xref{Defining, ,Defining Variables Verbatim},
4529 for a complete explanation of @code{define}.
4531 The first command in this example runs Yacc on the first prerequisite of
4532 whichever rule uses the canned sequence.  The output file from Yacc is
4533 always named @file{y.tab.c}.  The second command moves the output to the
4534 rule's target file name.
4536 To use the canned sequence, substitute the variable into the recipe of a
4537 rule.  You can substitute it like any other variable
4538 (@pxref{Reference, ,Basics of Variable References}).
4539 Because variables defined by @code{define} are recursively expanded
4540 variables, all the variable references you wrote inside the @code{define}
4541 are expanded now.  For example:
4543 @example
4544 foo.c : foo.y
4545         $(run-yacc)
4546 @end example
4548 @noindent
4549 @samp{foo.y} will be substituted for the variable @samp{$^} when it occurs in
4550 @code{run-yacc}'s value, and @samp{foo.c} for @samp{$@@}.@refill
4552 This is a realistic example, but this particular one is not needed in
4553 practice because @code{make} has an implicit rule to figure out these
4554 commands based on the file names involved
4555 (@pxref{Implicit Rules, ,Using Implicit Rules}).
4557 @cindex @@, and @code{define}
4558 @cindex -, and @code{define}
4559 @cindex +, and @code{define}
4560 In recipe execution, each line of a canned sequence is treated just as
4561 if the line appeared on its own in the rule, preceded by a tab.  In
4562 particular, @code{make} invokes a separate subshell for each line.  You
4563 can use the special prefix characters that affect command lines
4564 (@samp{@@}, @samp{-}, and @samp{+}) on each line of a canned sequence.
4565 @xref{Recipes, ,Writing Recipes in Rules}.
4566 For example, using this canned sequence:
4568 @example
4569 define frobnicate
4570 @@echo "frobnicating target $@@"
4571 frob-step-1 $< -o $@@-step-1
4572 frob-step-2 $@@-step-1 -o $@@
4573 endef
4574 @end example
4576 @noindent
4577 @code{make} will not echo the first line, the @code{echo} command.
4578 But it @emph{will} echo the following two recipe lines.
4580 On the other hand, prefix characters on the recipe line that refers to
4581 a canned sequence apply to every line in the sequence.  So the rule:
4583 @example
4584 frob.out: frob.in
4585         @@$(frobnicate)
4586 @end example
4588 @noindent
4589 does not echo @emph{any} recipe lines.
4590 (@xref{Echoing, ,Recipe Echoing}, for a full explanation of @samp{@@}.)
4592 @node Empty Recipes,  , Sequences, Recipes
4593 @section Using Empty Recipes
4594 @cindex empty recipes
4595 @cindex recipes, empty
4597 It is sometimes useful to define recipes which do nothing.  This is done
4598 simply by giving a recipe that consists of nothing but whitespace.  For
4599 example:
4601 @example
4602 target: ;
4603 @end example
4605 @noindent
4606 defines an empty recipe for @file{target}.  You could also use a line
4607 beginning with a recipe prefix character to define an empty recipe,
4608 but this would be confusing because such a line looks empty.
4610 @findex .DEFAULT@r{, and empty recipes}
4611 You may be wondering why you would want to define a recipe that
4612 does nothing.  The only reason this is useful is to prevent a target
4613 from getting implicit recipes (from implicit rules or the
4614 @code{.DEFAULT} special target; @pxref{Implicit Rules} and
4615 @pxref{Last Resort, ,Defining Last-Resort Default Rules}).@refill
4617 @c !!! another reason is for canonical stamp files:
4618 @ignore
4619 @example
4620 foo: stamp-foo ;
4621 stamp-foo: foo.in
4622         create foo frm foo.in
4623         touch $@
4624 @end example
4625 @end ignore
4627 You may be inclined to define empty recipes for targets that are
4628 not actual files, but only exist so that their prerequisites can be
4629 remade.  However, this is not the best way to do that, because the
4630 prerequisites may not be remade properly if the target file actually does exist.
4631 @xref{Phony Targets, ,Phony Targets}, for a better way to do this.
4633 @node Using Variables, Conditionals, Recipes, Top
4634 @chapter How to Use Variables
4635 @cindex variable
4636 @cindex value
4637 @cindex recursive variable expansion
4638 @cindex simple variable expansion
4640 A @dfn{variable} is a name defined in a makefile to represent a string
4641 of text, called the variable's @dfn{value}.  These values are
4642 substituted by explicit request into targets, prerequisites, recipes,
4643 and other parts of the makefile.  (In some other versions of @code{make},
4644 variables are called @dfn{macros}.)
4645 @cindex macro
4647 Variables and functions in all parts of a makefile are expanded when
4648 read, except for in recipes, the right-hand sides of variable
4649 definitions using @samp{=}, and the bodies of variable definitions
4650 using the @code{define} directive.@refill
4652 Variables can represent lists of file names, options to pass to compilers,
4653 programs to run, directories to look in for source files, directories to
4654 write output in, or anything else you can imagine.
4656 A variable name may be any sequence of characters not containing @samp{:},
4657 @samp{#}, @samp{=}, or leading or trailing whitespace.  However,
4658 variable names containing characters other than letters, numbers, and
4659 underscores should be avoided, as they may be given special meanings in the
4660 future, and with some shells they cannot be passed through the environment to a
4661 sub-@code{make}
4662 (@pxref{Variables/Recursion, ,Communicating Variables to a Sub-@code{make}}).
4664 Variable names are case-sensitive.  The names @samp{foo}, @samp{FOO},
4665 and @samp{Foo} all refer to different variables.
4667 It is traditional to use upper case letters in variable names, but we
4668 recommend using lower case letters for variable names that serve internal
4669 purposes in the makefile, and reserving upper case for parameters that
4670 control implicit rules or for parameters that the user should override with
4671 command options (@pxref{Overriding, ,Overriding Variables}).
4673 A few variables have names that are a single punctuation character or
4674 just a few characters.  These are the @dfn{automatic variables}, and
4675 they have particular specialized uses.  @xref{Automatic Variables}.
4677 @menu
4678 * Reference::                   How to use the value of a variable.
4679 * Flavors::                     Variables come in two flavors.
4680 * Advanced::                    Advanced features for referencing a variable.
4681 * Values::                      All the ways variables get their values.
4682 * Setting::                     How to set a variable in the makefile.
4683 * Appending::                   How to append more text to the old value
4684                                   of a variable.
4685 * Override Directive::          How to set a variable in the makefile even if
4686                                   the user has set it with a command argument.
4687 * Defining::                    An alternate way to set a variable
4688                                   to a verbatim string.
4689 * Environment::                 Variable values can come from the environment.
4690 * Target-specific::             Variable values can be defined on a per-target
4691                                   basis.
4692 * Pattern-specific::            Target-specific variable values can be applied
4693                                   to a group of targets that match a pattern.
4694 * Special Variables::           Variables with special meaning or behavior.
4695 @end menu
4697 @node Reference, Flavors, Using Variables, Using Variables
4698 @section Basics of Variable References
4699 @cindex variables, how to reference
4700 @cindex reference to variables
4701 @cindex @code{$}, in variable reference
4702 @cindex dollar sign (@code{$}), in variable reference
4704 To substitute a variable's value, write a dollar sign followed by the name
4705 of the variable in parentheses or braces: either @samp{$(foo)} or
4706 @samp{$@{foo@}} is a valid reference to the variable @code{foo}.  This
4707 special significance of @samp{$} is why you must write @samp{$$} to have
4708 the effect of a single dollar sign in a file name or recipe.
4710 Variable references can be used in any context: targets, prerequisites,
4711 recipes, most directives, and new variable values.  Here is an
4712 example of a common case, where a variable holds the names of all the
4713 object files in a program:
4715 @example
4716 @group
4717 objects = program.o foo.o utils.o
4718 program : $(objects)
4719         cc -o program $(objects)
4721 $(objects) : defs.h
4722 @end group
4723 @end example
4725 Variable references work by strict textual substitution.  Thus, the rule
4727 @example
4728 @group
4729 foo = c
4730 prog.o : prog.$(foo)
4731         $(foo)$(foo) -$(foo) prog.$(foo)
4732 @end group
4733 @end example
4735 @noindent
4736 could be used to compile a C program @file{prog.c}.  Since spaces before
4737 the variable value are ignored in variable assignments, the value of
4738 @code{foo} is precisely @samp{c}.  (Don't actually write your makefiles
4739 this way!)
4741 A dollar sign followed by a character other than a dollar sign,
4742 open-parenthesis or open-brace treats that single character as the
4743 variable name.  Thus, you could reference the variable @code{x} with
4744 @samp{$x}.  However, this practice is strongly discouraged, except in
4745 the case of the automatic variables (@pxref{Automatic Variables}).
4747 @node Flavors, Advanced, Reference, Using Variables
4748 @section The Two Flavors of Variables
4749 @cindex flavors of variables
4750 @cindex recursive variable expansion
4751 @cindex variables, flavors
4752 @cindex recursively expanded variables
4753 @cindex variables, recursively expanded
4755 There are two ways that a variable in GNU @code{make} can have a value;
4756 we call them the two @dfn{flavors} of variables.  The two flavors are
4757 distinguished in how they are defined and in what they do when expanded.
4759 @cindex =
4760 The first flavor of variable is a @dfn{recursively expanded} variable.
4761 Variables of this sort are defined by lines using @samp{=}
4762 (@pxref{Setting, ,Setting Variables}) or by the @code{define} directive
4763 (@pxref{Defining, ,Defining Variables Verbatim}).  The value you specify
4764 is installed verbatim; if it contains references to other variables,
4765 these references are expanded whenever this variable is substituted (in
4766 the course of expanding some other string).  When this happens, it is
4767 called @dfn{recursive expansion}.@refill
4769 For example,
4771 @example
4772 foo = $(bar)
4773 bar = $(ugh)
4774 ugh = Huh?
4776 all:;echo $(foo)
4777 @end example
4779 @noindent
4780 will echo @samp{Huh?}: @samp{$(foo)} expands to @samp{$(bar)} which
4781 expands to @samp{$(ugh)} which finally expands to @samp{Huh?}.@refill
4783 This flavor of variable is the only sort supported by other versions of
4784 @code{make}.  It has its advantages and its disadvantages.  An advantage
4785 (most would say) is that:
4787 @example
4788 CFLAGS = $(include_dirs) -O
4789 include_dirs = -Ifoo -Ibar
4790 @end example
4792 @noindent
4793 will do what was intended: when @samp{CFLAGS} is expanded in a recipe,
4794 it will expand to @samp{-Ifoo -Ibar -O}.  A major disadvantage is that you
4795 cannot append something on the end of a variable, as in
4797 @example
4798 CFLAGS = $(CFLAGS) -O
4799 @end example
4801 @noindent
4802 because it will cause an infinite loop in the variable expansion.
4803 (Actually @code{make} detects the infinite loop and reports an error.)
4804 @cindex loops in variable expansion
4805 @cindex variables, loops in expansion
4807 Another disadvantage is that any functions
4808 (@pxref{Functions, ,Functions for Transforming Text})
4809 referenced in the definition will be executed every time the variable is
4810 expanded.  This makes @code{make} run slower; worse, it causes the
4811 @code{wildcard} and @code{shell} functions to give unpredictable results
4812 because you cannot easily control when they are called, or even how many
4813 times.
4815 To avoid all the problems and inconveniences of recursively expanded
4816 variables, there is another flavor: simply expanded variables.
4818 @cindex simply expanded variables
4819 @cindex variables, simply expanded
4820 @cindex :=
4821 @dfn{Simply expanded variables} are defined by lines using @samp{:=}
4822 (@pxref{Setting, ,Setting Variables}).
4823 The value of a simply expanded variable is scanned
4824 once and for all, expanding any references to other variables and
4825 functions, when the variable is defined.  The actual value of the simply
4826 expanded variable is the result of expanding the text that you write.
4827 It does not contain any references to other variables; it contains their
4828 values @emph{as of the time this variable was defined}.  Therefore,
4830 @example
4831 x := foo
4832 y := $(x) bar
4833 x := later
4834 @end example
4836 @noindent
4837 is equivalent to
4839 @example
4840 y := foo bar
4841 x := later
4842 @end example
4844 When a simply expanded variable is referenced, its value is substituted
4845 verbatim.
4847 Here is a somewhat more complicated example, illustrating the use of
4848 @samp{:=} in conjunction with the @code{shell} function.
4849 (@xref{Shell Function, , The @code{shell} Function}.)  This example
4850 also shows use of the variable @code{MAKELEVEL}, which is changed
4851 when it is passed down from level to level.
4852 (@xref{Variables/Recursion, , Communicating Variables to a
4853 Sub-@code{make}}, for information about @code{MAKELEVEL}.)
4855 @vindex MAKELEVEL
4856 @vindex MAKE
4857 @example
4858 @group
4859 ifeq (0,$@{MAKELEVEL@})
4860 whoami    := $(shell whoami)
4861 host-type := $(shell arch)
4862 MAKE := $@{MAKE@} host-type=$@{host-type@} whoami=$@{whoami@}
4863 endif
4864 @end group
4865 @end example
4867 @noindent
4868 An advantage of this use of @samp{:=} is that a typical
4869 `descend into a directory' recipe then looks like this:
4871 @example
4872 @group
4873 $@{subdirs@}:
4874         $@{MAKE@} -C $@@ all
4875 @end group
4876 @end example
4878 Simply expanded variables generally make complicated makefile programming
4879 more predictable because they work like variables in most programming
4880 languages.  They allow you to redefine a variable using its own value (or
4881 its value processed in some way by one of the expansion functions) and to
4882 use the expansion functions much more efficiently
4883 (@pxref{Functions, ,Functions for Transforming Text}).
4885 @cindex spaces, in variable values
4886 @cindex whitespace, in variable values
4887 @cindex variables, spaces in values
4888 You can also use them to introduce controlled leading whitespace into
4889 variable values.  Leading whitespace characters are discarded from your
4890 input before substitution of variable references and function calls;
4891 this means you can include leading spaces in a variable value by
4892 protecting them with variable references, like this:
4894 @example
4895 nullstring :=
4896 space := $(nullstring) # end of the line
4897 @end example
4899 @noindent
4900 Here the value of the variable @code{space} is precisely one space.  The
4901 comment @w{@samp{# end of the line}} is included here just for clarity.
4902 Since trailing space characters are @emph{not} stripped from variable
4903 values, just a space at the end of the line would have the same effect
4904 (but be rather hard to read).  If you put whitespace at the end of a
4905 variable value, it is a good idea to put a comment like that at the end
4906 of the line to make your intent clear.  Conversely, if you do @emph{not}
4907 want any whitespace characters at the end of your variable value, you
4908 must remember not to put a random comment on the end of the line after
4909 some whitespace, such as this:
4911 @example
4912 dir := /foo/bar    # directory to put the frobs in
4913 @end example
4915 @noindent
4916 Here the value of the variable @code{dir} is @w{@samp{/foo/bar    }}
4917 (with four trailing spaces), which was probably not the intention.
4918 (Imagine something like @w{@samp{$(dir)/file}} with this definition!)
4920 @cindex conditional variable assignment
4921 @cindex variables, conditional assignment
4922 @cindex ?=
4923 There is another assignment operator for variables, @samp{?=}.  This
4924 is called a conditional variable assignment operator, because it only
4925 has an effect if the variable is not yet defined.  This statement:
4927 @example
4928 FOO ?= bar
4929 @end example
4931 @noindent
4932 is exactly equivalent to this
4933 (@pxref{Origin Function, ,The @code{origin} Function}):
4935 @example
4936 ifeq ($(origin FOO), undefined)
4937   FOO = bar
4938 endif
4939 @end example
4941 Note that a variable set to an empty value is still defined, so
4942 @samp{?=} will not set that variable.
4944 @node Advanced, Values, Flavors, Using Variables
4945 @section Advanced Features for Reference to Variables
4946 @cindex reference to variables
4948 This section describes some advanced features you can use to reference
4949 variables in more flexible ways.
4951 @menu
4952 * Substitution Refs::           Referencing a variable with
4953                                   substitutions on the value.
4954 * Computed Names::              Computing the name of the variable to refer to.
4955 @end menu
4957 @node Substitution Refs, Computed Names, Advanced, Advanced
4958 @subsection Substitution References
4959 @cindex modified variable reference
4960 @cindex substitution variable reference
4961 @cindex variables, modified reference
4962 @cindex variables, substitution reference
4964 @cindex variables, substituting suffix in
4965 @cindex suffix, substituting in variables
4966 A @dfn{substitution reference} substitutes the value of a variable with
4967 alterations that you specify.  It has the form
4968 @samp{$(@var{var}:@var{a}=@var{b})} (or
4969 @samp{$@{@var{var}:@var{a}=@var{b}@}}) and its meaning is to take the value
4970 of the variable @var{var}, replace every @var{a} at the end of a word with
4971 @var{b} in that value, and substitute the resulting string.
4973 When we say ``at the end of a word'', we mean that @var{a} must appear
4974 either followed by whitespace or at the end of the value in order to be
4975 replaced; other occurrences of @var{a} in the value are unaltered.  For
4976 example:@refill
4978 @example
4979 foo := a.o b.o c.o
4980 bar := $(foo:.o=.c)
4981 @end example
4983 @noindent
4984 sets @samp{bar} to @samp{a.c b.c c.c}.  @xref{Setting, ,Setting Variables}.
4986 A substitution reference is actually an abbreviation for use of the
4987 @code{patsubst} expansion function (@pxref{Text Functions, ,Functions for String Substitution and Analysis}).  We provide
4988 substitution references as well as @code{patsubst} for compatibility with
4989 other implementations of @code{make}.
4991 @findex patsubst
4992 Another type of substitution reference lets you use the full power of
4993 the @code{patsubst} function.  It has the same form
4994 @samp{$(@var{var}:@var{a}=@var{b})} described above, except that now
4995 @var{a} must contain a single @samp{%} character.  This case is
4996 equivalent to @samp{$(patsubst @var{a},@var{b},$(@var{var}))}.
4997 @xref{Text Functions, ,Functions for String Substitution and Analysis},
4998 for a description of the @code{patsubst} function.@refill
5000 @example
5001 @group
5002 @exdent For example:
5004 foo := a.o b.o c.o
5005 bar := $(foo:%.o=%.c)
5006 @end group
5007 @end example
5009 @noindent
5010 sets @samp{bar} to @samp{a.c b.c c.c}.
5012 @node Computed Names,  , Substitution Refs, Advanced
5013 @subsection Computed Variable Names
5014 @cindex nested variable reference
5015 @cindex computed variable name
5016 @cindex variables, computed names
5017 @cindex variables, nested references
5018 @cindex variables, @samp{$} in name
5019 @cindex @code{$}, in variable name
5020 @cindex dollar sign (@code{$}), in variable name
5022 Computed variable names are a complicated concept needed only for
5023 sophisticated makefile programming.  For most purposes you need not
5024 consider them, except to know that making a variable with a dollar sign
5025 in its name might have strange results.  However, if you are the type
5026 that wants to understand everything, or you are actually interested in
5027 what they do, read on.
5029 Variables may be referenced inside the name of a variable.  This is
5030 called a @dfn{computed variable name} or a @dfn{nested variable
5031 reference}.  For example,
5033 @example
5034 x = y
5035 y = z
5036 a := $($(x))
5037 @end example
5039 @noindent
5040 defines @code{a} as @samp{z}: the @samp{$(x)} inside @samp{$($(x))} expands
5041 to @samp{y}, so @samp{$($(x))} expands to @samp{$(y)} which in turn expands
5042 to @samp{z}.  Here the name of the variable to reference is not stated
5043 explicitly; it is computed by expansion of @samp{$(x)}.  The reference
5044 @samp{$(x)} here is nested within the outer variable reference.
5046 The previous example shows two levels of nesting, but any number of levels
5047 is possible.  For example, here are three levels:
5049 @example
5050 x = y
5051 y = z
5052 z = u
5053 a := $($($(x)))
5054 @end example
5056 @noindent
5057 Here the innermost @samp{$(x)} expands to @samp{y}, so @samp{$($(x))}
5058 expands to @samp{$(y)} which in turn expands to @samp{z}; now we have
5059 @samp{$(z)}, which becomes @samp{u}.
5061 References to recursively-expanded variables within a variable name are
5062 reexpanded in the usual fashion.  For example:
5064 @example
5065 x = $(y)
5066 y = z
5067 z = Hello
5068 a := $($(x))
5069 @end example
5071 @noindent
5072 defines @code{a} as @samp{Hello}: @samp{$($(x))} becomes @samp{$($(y))}
5073 which becomes @samp{$(z)} which becomes @samp{Hello}.
5075 Nested variable references can also contain modified references and
5076 function invocations (@pxref{Functions, ,Functions for Transforming Text}),
5077 just like any other reference.
5078 For example, using the @code{subst} function
5079 (@pxref{Text Functions, ,Functions for String Substitution and Analysis}):
5081 @example
5082 @group
5083 x = variable1
5084 variable2 := Hello
5085 y = $(subst 1,2,$(x))
5086 z = y
5087 a := $($($(z)))
5088 @end group
5089 @end example
5091 @noindent
5092 eventually defines @code{a} as @samp{Hello}.  It is doubtful that anyone
5093 would ever want to write a nested reference as convoluted as this one, but
5094 it works: @samp{$($($(z)))} expands to @samp{$($(y))} which becomes
5095 @samp{$($(subst 1,2,$(x)))}.  This gets the value @samp{variable1} from
5096 @code{x} and changes it by substitution to @samp{variable2}, so that the
5097 entire string becomes @samp{$(variable2)}, a simple variable reference
5098 whose value is @samp{Hello}.@refill
5100 A computed variable name need not consist entirely of a single variable
5101 reference.  It can contain several variable references, as well as some
5102 invariant text.  For example,
5104 @example
5105 @group
5106 a_dirs := dira dirb
5107 1_dirs := dir1 dir2
5108 @end group
5110 @group
5111 a_files := filea fileb
5112 1_files := file1 file2
5113 @end group
5115 @group
5116 ifeq "$(use_a)" "yes"
5117 a1 := a
5118 else
5119 a1 := 1
5120 endif
5121 @end group
5123 @group
5124 ifeq "$(use_dirs)" "yes"
5125 df := dirs
5126 else
5127 df := files
5128 endif
5130 dirs := $($(a1)_$(df))
5131 @end group
5132 @end example
5134 @noindent
5135 will give @code{dirs} the same value as @code{a_dirs}, @code{1_dirs},
5136 @code{a_files} or @code{1_files} depending on the settings of @code{use_a}
5137 and @code{use_dirs}.@refill
5139 Computed variable names can also be used in substitution references:
5141 @example
5142 @group
5143 a_objects := a.o b.o c.o
5144 1_objects := 1.o 2.o 3.o
5146 sources := $($(a1)_objects:.o=.c)
5147 @end group
5148 @end example
5150 @noindent
5151 defines @code{sources} as either @samp{a.c b.c c.c} or @samp{1.c 2.c 3.c},
5152 depending on the value of @code{a1}.
5154 The only restriction on this sort of use of nested variable references
5155 is that they cannot specify part of the name of a function to be called.
5156 This is because the test for a recognized function name is done before
5157 the expansion of nested references.  For example,
5159 @example
5160 @group
5161 ifdef do_sort
5162 func := sort
5163 else
5164 func := strip
5165 endif
5166 @end group
5168 @group
5169 bar := a d b g q c
5170 @end group
5172 @group
5173 foo := $($(func) $(bar))
5174 @end group
5175 @end example
5177 @noindent
5178 attempts to give @samp{foo} the value of the variable @samp{sort a d b g
5179 q c} or @samp{strip a d b g q c}, rather than giving @samp{a d b g q c}
5180 as the argument to either the @code{sort} or the @code{strip} function.
5181 This restriction could be removed in the future if that change is shown
5182 to be a good idea.
5184 You can also use computed variable names in the left-hand side of a
5185 variable assignment, or in a @code{define} directive, as in:
5187 @example
5188 dir = foo
5189 $(dir)_sources := $(wildcard $(dir)/*.c)
5190 define $(dir)_print
5191 lpr $($(dir)_sources)
5192 endef
5193 @end example
5195 @noindent
5196 This example defines the variables @samp{dir}, @samp{foo_sources}, and
5197 @samp{foo_print}.
5199 Note that @dfn{nested variable references} are quite different from
5200 @dfn{recursively expanded variables}
5201 (@pxref{Flavors, ,The Two Flavors of Variables}), though both are
5202 used together in complex ways when doing makefile programming.@refill
5204 @node Values, Setting, Advanced, Using Variables
5205 @section How Variables Get Their Values
5206 @cindex variables, how they get their values
5207 @cindex value, how a variable gets it
5209 Variables can get values in several different ways:
5211 @itemize @bullet
5212 @item
5213 You can specify an overriding value when you run @code{make}.
5214 @xref{Overriding, ,Overriding Variables}.
5216 @item
5217 You can specify a value in the makefile, either
5218 with an assignment (@pxref{Setting, ,Setting Variables}) or with a
5219 verbatim definition (@pxref{Defining, ,Defining Variables Verbatim}).@refill
5221 @item
5222 Variables in the environment become @code{make} variables.
5223 @xref{Environment, ,Variables from the Environment}.
5225 @item
5226 Several @dfn{automatic} variables are given new values for each rule.
5227 Each of these has a single conventional use.
5228 @xref{Automatic Variables}.
5230 @item
5231 Several variables have constant initial values.
5232 @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
5233 @end itemize
5235 @node Setting, Appending, Values, Using Variables
5236 @section Setting Variables
5237 @cindex setting variables
5238 @cindex variables, setting
5239 @cindex =
5240 @cindex :=
5241 @cindex ?=
5243 To set a variable from the makefile, write a line starting with the
5244 variable name followed by @samp{=} or @samp{:=}.  Whatever follows the
5245 @samp{=} or @samp{:=} on the line becomes the value.  For example,
5247 @example
5248 objects = main.o foo.o bar.o utils.o
5249 @end example
5251 @noindent
5252 defines a variable named @code{objects}.  Whitespace around the variable
5253 name and immediately after the @samp{=} is ignored.
5255 Variables defined with @samp{=} are @dfn{recursively expanded} variables.
5256 Variables defined with @samp{:=} are @dfn{simply expanded} variables; these
5257 definitions can contain variable references which will be expanded before
5258 the definition is made.  @xref{Flavors, ,The Two Flavors of Variables}.
5260 The variable name may contain function and variable references, which
5261 are expanded when the line is read to find the actual variable name to use.
5263 There is no limit on the length of the value of a variable except the
5264 amount of swapping space on the computer.  When a variable definition is
5265 long, it is a good idea to break it into several lines by inserting
5266 backslash-newline at convenient places in the definition.  This will not
5267 affect the functioning of @code{make}, but it will make the makefile easier
5268 to read.
5270 Most variable names are considered to have the empty string as a value if
5271 you have never set them.  Several variables have built-in initial values
5272 that are not empty, but you can set them in the usual ways
5273 (@pxref{Implicit Variables, ,Variables Used by Implicit Rules}).
5274 Several special variables are set
5275 automatically to a new value for each rule; these are called the
5276 @dfn{automatic} variables (@pxref{Automatic Variables}).
5278 If you'd like a variable to be set to a value only if it's not already
5279 set, then you can use the shorthand operator @samp{?=} instead of
5280 @samp{=}.  These two settings of the variable @samp{FOO} are identical
5281 (@pxref{Origin Function, ,The @code{origin} Function}):
5283 @example
5284 FOO ?= bar
5285 @end example
5287 @noindent
5290 @example
5291 ifeq ($(origin FOO), undefined)
5292 FOO = bar
5293 endif
5294 @end example
5296 @node Appending, Override Directive, Setting, Using Variables
5297 @section Appending More Text to Variables
5298 @cindex +=
5299 @cindex appending to variables
5300 @cindex variables, appending to
5302 Often it is useful to add more text to the value of a variable already defined.
5303 You do this with a line containing @samp{+=}, like this:
5305 @example
5306 objects += another.o
5307 @end example
5309 @noindent
5310 This takes the value of the variable @code{objects}, and adds the text
5311 @samp{another.o} to it (preceded by a single space).  Thus:
5313 @example
5314 objects = main.o foo.o bar.o utils.o
5315 objects += another.o
5316 @end example
5318 @noindent
5319 sets @code{objects} to @samp{main.o foo.o bar.o utils.o another.o}.
5321 Using @samp{+=} is similar to:
5323 @example
5324 objects = main.o foo.o bar.o utils.o
5325 objects := $(objects) another.o
5326 @end example
5328 @noindent
5329 but differs in ways that become important when you use more complex values.
5331 When the variable in question has not been defined before, @samp{+=}
5332 acts just like normal @samp{=}: it defines a recursively-expanded
5333 variable.  However, when there @emph{is} a previous definition, exactly
5334 what @samp{+=} does depends on what flavor of variable you defined
5335 originally.  @xref{Flavors, ,The Two Flavors of Variables}, for an
5336 explanation of the two flavors of variables.
5338 When you add to a variable's value with @samp{+=}, @code{make} acts
5339 essentially as if you had included the extra text in the initial
5340 definition of the variable.  If you defined it first with @samp{:=},
5341 making it a simply-expanded variable, @samp{+=} adds to that
5342 simply-expanded definition, and expands the new text before appending it
5343 to the old value just as @samp{:=} does
5344 (see @ref{Setting, ,Setting Variables}, for a full explanation of @samp{:=}).
5345 In fact,
5347 @example
5348 variable := value
5349 variable += more
5350 @end example
5352 @noindent
5353 is exactly equivalent to:
5355 @noindent
5356 @example
5357 variable := value
5358 variable := $(variable) more
5359 @end example
5361 On the other hand, when you use @samp{+=} with a variable that you defined
5362 first to be recursively-expanded using plain @samp{=}, @code{make} does
5363 something a bit different.  Recall that when you define a
5364 recursively-expanded variable, @code{make} does not expand the value you set
5365 for variable and function references immediately.  Instead it stores the text
5366 verbatim, and saves these variable and function references to be expanded
5367 later, when you refer to the new variable (@pxref{Flavors, ,The Two Flavors
5368 of Variables}).  When you use @samp{+=} on a recursively-expanded variable,
5369 it is this unexpanded text to which @code{make} appends the new text you
5370 specify.
5372 @example
5373 @group
5374 variable = value
5375 variable += more
5376 @end group
5377 @end example
5379 @noindent
5380 is roughly equivalent to:
5382 @example
5383 @group
5384 temp = value
5385 variable = $(temp) more
5386 @end group
5387 @end example
5389 @noindent
5390 except that of course it never defines a variable called @code{temp}.
5391 The importance of this comes when the variable's old value contains
5392 variable references.  Take this common example:
5394 @example
5395 CFLAGS = $(includes) -O
5396 @dots{}
5397 CFLAGS += -pg # enable profiling
5398 @end example
5400 @noindent
5401 The first line defines the @code{CFLAGS} variable with a reference to another
5402 variable, @code{includes}.  (@code{CFLAGS} is used by the rules for C
5403 compilation; @pxref{Catalogue of Rules, ,Catalogue of Implicit Rules}.)
5404 Using @samp{=} for the definition makes @code{CFLAGS} a recursively-expanded
5405 variable, meaning @w{@samp{$(includes) -O}} is @emph{not} expanded when
5406 @code{make} processes the definition of @code{CFLAGS}.  Thus, @code{includes}
5407 need not be defined yet for its value to take effect.  It only has to be
5408 defined before any reference to @code{CFLAGS}.  If we tried to append to the
5409 value of @code{CFLAGS} without using @samp{+=}, we might do it like this:
5411 @example
5412 CFLAGS := $(CFLAGS) -pg # enable profiling
5413 @end example
5415 @noindent
5416 This is pretty close, but not quite what we want.  Using @samp{:=}
5417 redefines @code{CFLAGS} as a simply-expanded variable; this means
5418 @code{make} expands the text @w{@samp{$(CFLAGS) -pg}} before setting the
5419 variable.  If @code{includes} is not yet defined, we get @w{@samp{ -O
5420 -pg}}, and a later definition of @code{includes} will have no effect.
5421 Conversely, by using @samp{+=} we set @code{CFLAGS} to the
5422 @emph{unexpanded} value @w{@samp{$(includes) -O -pg}}.  Thus we preserve
5423 the reference to @code{includes}, so if that variable gets defined at
5424 any later point, a reference like @samp{$(CFLAGS)} still uses its
5425 value.
5427 @node Override Directive, Defining, Appending, Using Variables
5428 @section The @code{override} Directive
5429 @findex override
5430 @cindex overriding with @code{override}
5431 @cindex variables, overriding
5433 If a variable has been set with a command argument
5434 (@pxref{Overriding, ,Overriding Variables}),
5435 then ordinary assignments in the makefile are ignored.  If you want to set
5436 the variable in the makefile even though it was set with a command
5437 argument, you can use an @code{override} directive, which is a line that
5438 looks like this:@refill
5440 @example
5441 override @var{variable} = @var{value}
5442 @end example
5444 @noindent
5447 @example
5448 override @var{variable} := @var{value}
5449 @end example
5451 To append more text to a variable defined on the command line, use:
5453 @example
5454 override @var{variable} += @var{more text}
5455 @end example
5457 @noindent
5458 @xref{Appending, ,Appending More Text to Variables}.
5460 The @code{override} directive was not invented for escalation in the war
5461 between makefiles and command arguments.  It was invented so you can alter
5462 and add to values that the user specifies with command arguments.
5464 For example, suppose you always want the @samp{-g} switch when you run the
5465 C compiler, but you would like to allow the user to specify the other
5466 switches with a command argument just as usual.  You could use this
5467 @code{override} directive:
5469 @example
5470 override CFLAGS += -g
5471 @end example
5473 You can also use @code{override} directives with @code{define} directives.
5474 This is done as you might expect:
5476 @example
5477 override define foo
5479 endef
5480 @end example
5482 @noindent
5483 @iftex
5484 See the next section for information about @code{define}.
5485 @end iftex
5486 @ifnottex
5487 @xref{Defining, ,Defining Variables Verbatim}.
5488 @end ifnottex
5490 @node Defining, Environment, Override Directive, Using Variables
5491 @section Defining Variables Verbatim
5492 @findex define
5493 @findex endef
5494 @cindex verbatim variable definition
5495 @cindex defining variables verbatim
5496 @cindex variables, defining verbatim
5498 Another way to set the value of a variable is to use the @code{define}
5499 directive.  This directive has an unusual syntax which allows newline
5500 characters to be included in the value, which is convenient for defining
5501 both canned sequences of commands
5502 (@pxref{Sequences, ,Defining Canned Command Sequences}), and also
5503 sections of makefile syntax to use with @code{eval} (@pxref{Eval Function}).
5505 The @code{define} directive is followed on the same line by the name of the
5506 variable and nothing more.  The value to give the variable appears on the
5507 following lines.  The end of the value is marked by a line containing just
5508 the word @code{endef}.  Aside from this difference in syntax, @code{define}
5509 works just like @samp{=}: it creates a recursively-expanded variable
5510 (@pxref{Flavors, ,The Two Flavors of Variables}).
5511 The variable name may contain function and variable references, which
5512 are expanded when the directive is read to find the actual variable name
5513 to use.
5515 You may nest @code{define} directives: @code{make} will keep track of
5516 nested directives and report an error if they are not all properly
5517 closed with @code{endef}.  Note that lines beginning with the recipe
5518 prefix character are considered part of a recipe, so any @code{define}
5519 or @code{endef} strings appearing on such a line will not be
5520 considered @code{make} operators.
5522 @example
5523 define two-lines
5524 echo foo
5525 echo $(bar)
5526 endef
5527 @end example
5529 The value in an ordinary assignment cannot contain a newline; but the
5530 newlines that separate the lines of the value in a @code{define} become
5531 part of the variable's value (except for the final newline which precedes
5532 the @code{endef} and is not considered part of the value).@refill
5534 @need 800
5535 When used in a recipe, the previous example is functionally equivalent
5536 to this:
5538 @example
5539 two-lines = echo foo; echo $(bar)
5540 @end example
5542 @noindent
5543 since two commands separated by semicolon behave much like two separate
5544 shell commands.  However, note that using two separate lines means
5545 @code{make} will invoke the shell twice, running an independent subshell
5546 for each line.  @xref{Execution, ,Recipe Execution}.
5548 If you want variable definitions made with @code{define} to take
5549 precedence over command-line variable definitions, you can use the
5550 @code{override} directive together with @code{define}:
5552 @example
5553 override define two-lines
5555 $(bar)
5556 endef
5557 @end example
5559 @noindent
5560 @xref{Override Directive, ,The @code{override} Directive}.
5562 @node Environment, Target-specific, Defining, Using Variables
5563 @section Variables from the Environment
5565 @cindex variables, environment
5566 @cindex environment
5567 Variables in @code{make} can come from the environment in which
5568 @code{make} is run.  Every environment variable that @code{make} sees
5569 when it starts up is transformed into a @code{make} variable with the
5570 same name and value.  However, an explicit assignment in the makefile,
5571 or with a command argument, overrides the environment.  (If the
5572 @samp{-e} flag is specified, then values from the environment override
5573 assignments in the makefile.  @xref{Options Summary, ,Summary of
5574 Options}.  But this is not recommended practice.)
5576 Thus, by setting the variable @code{CFLAGS} in your environment, you can
5577 cause all C compilations in most makefiles to use the compiler switches you
5578 prefer.  This is safe for variables with standard or conventional meanings
5579 because you know that no makefile will use them for other things.  (Note
5580 this is not totally reliable; some makefiles set @code{CFLAGS} explicitly
5581 and therefore are not affected by the value in the environment.)
5583 When @code{make} runs a recipe, variables defined in the
5584 makefile are placed into the environment of each shell.  This allows
5585 you to pass values to sub-@code{make} invocations (@pxref{Recursion,
5586 ,Recursive Use of @code{make}}).  By default, only variables that came
5587 from the environment or the command line are passed to recursive
5588 invocations.  You can use the @code{export} directive to pass other
5589 variables.  @xref{Variables/Recursion, , Communicating Variables to a
5590 Sub-@code{make}}, for full details.
5592 Other use of variables from the environment is not recommended.  It is not
5593 wise for makefiles to depend for their functioning on environment variables
5594 set up outside their control, since this would cause different users to get
5595 different results from the same makefile.  This is against the whole
5596 purpose of most makefiles.
5598 @cindex SHELL, import from environment
5599 Such problems would be especially likely with the variable
5600 @code{SHELL}, which is normally present in the environment to specify
5601 the user's choice of interactive shell.  It would be very undesirable
5602 for this choice to affect @code{make}; so, @code{make} handles the
5603 @code{SHELL} environment variable in a special way; see @ref{Choosing
5604 the Shell}.@refill
5606 @node Target-specific, Pattern-specific, Environment, Using Variables
5607 @section Target-specific Variable Values
5608 @cindex target-specific variables
5609 @cindex variables, target-specific
5611 Variable values in @code{make} are usually global; that is, they are the
5612 same regardless of where they are evaluated (unless they're reset, of
5613 course).  One exception to that is automatic variables
5614 (@pxref{Automatic Variables}).
5616 The other exception is @dfn{target-specific variable values}.  This
5617 feature allows you to define different values for the same variable,
5618 based on the target that @code{make} is currently building.  As with
5619 automatic variables, these values are only available within the context
5620 of a target's recipe (and in other target-specific assignments).
5622 Set a target-specific variable value like this:
5624 @example
5625 @var{target} @dots{} : @var{variable-assignment}
5626 @end example
5628 @noindent
5629 or like this:
5631 @example
5632 @var{target} @dots{} : override @var{variable-assignment}
5633 @end example
5635 @noindent
5636 or like this:
5638 @example
5639 @var{target} @dots{} : export @var{variable-assignment}
5640 @end example
5642 Multiple @var{target} values create a target-specific variable value for
5643 each member of the target list individually.
5645 The @var{variable-assignment} can be any valid form of assignment;
5646 recursive (@samp{=}), static (@samp{:=}), appending (@samp{+=}), or
5647 conditional (@samp{?=}).  All variables that appear within the
5648 @var{variable-assignment} are evaluated within the context of the
5649 target: thus, any previously-defined target-specific variable values
5650 will be in effect.  Note that this variable is actually distinct from
5651 any ``global'' value: the two variables do not have to have the same
5652 flavor (recursive vs.@: static).
5654 Target-specific variables have the same priority as any other makefile
5655 variable.  Variables provided on the command line (and in the
5656 environment if the @samp{-e} option is in force) will take precedence.
5657 Specifying the @code{override} directive will allow the target-specific
5658 variable value to be preferred.
5660 There is one more special feature of target-specific variables: when
5661 you define a target-specific variable that variable value is also in
5662 effect for all prerequisites of this target, and all their
5663 prerequisites, etc.@: (unless those prerequisites override that variable
5664 with their own target-specific variable value).  So, for example, a
5665 statement like this:
5667 @example
5668 prog : CFLAGS = -g
5669 prog : prog.o foo.o bar.o
5670 @end example
5672 @noindent
5673 will set @code{CFLAGS} to @samp{-g} in the recipe for @file{prog}, but
5674 it will also set @code{CFLAGS} to @samp{-g} in the recipes that create
5675 @file{prog.o}, @file{foo.o}, and @file{bar.o}, and any recipes which
5676 create their prerequisites.
5678 Be aware that a given prerequisite will only be built once per
5679 invocation of make, at most.  If the same file is a prerequisite of
5680 multiple targets, and each of those targets has a different value for
5681 the same target-specific variable, then the first target to be built
5682 will cause that prerequisite to be built and the prerequisite will
5683 inherit the target-specific value from the first target.  It will
5684 ignore the target-specific values from any other targets.
5686 @node Pattern-specific, Special Variables, Target-specific, Using Variables
5687 @section Pattern-specific Variable Values
5688 @cindex pattern-specific variables
5689 @cindex variables, pattern-specific
5691 In addition to target-specific variable values
5692 (@pxref{Target-specific, ,Target-specific Variable Values}), GNU
5693 @code{make} supports pattern-specific variable values.  In this form,
5694 the variable is defined for any target that matches the pattern
5695 specified.  If a target matches more than one pattern, all the
5696 matching pattern-specific variables are interpreted in the order in
5697 which they were defined in the makefile, and collected together into
5698 one set.  Variables defined in this way are searched after any
5699 target-specific variables defined explicitly for that target, and
5700 before target-specific variables defined for the parent target.
5702 Set a pattern-specific variable value like this:
5704 @example
5705 @var{pattern} @dots{} : @var{variable-assignment}
5706 @end example
5708 @noindent
5709 or like this:
5711 @example
5712 @var{pattern} @dots{} : override @var{variable-assignment}
5713 @end example
5715 @noindent
5716 where @var{pattern} is a %-pattern.  As with target-specific variable
5717 values, multiple @var{pattern} values create a pattern-specific variable
5718 value for each pattern individually.  The @var{variable-assignment} can
5719 be any valid form of assignment.  Any command line variable setting will
5720 take precedence, unless @code{override} is specified.
5722 For example:
5724 @example
5725 %.o : CFLAGS = -O
5726 @end example
5728 @noindent
5729 will assign @code{CFLAGS} the value of @samp{-O} for all targets
5730 matching the pattern @code{%.o}.
5732 @node Special Variables,  , Pattern-specific, Using Variables
5733 @comment  node-name,  next,  previous,  up
5734 @section Other Special Variables
5735 @cindex makefiles, and special variables
5736 @cindex special variables
5738 GNU @code{make} supports some variables that have special properties.
5740 @table @code
5742 @vindex MAKEFILE_LIST @r{(list of parsed makefiles)}
5743 @cindex makefiles, and @code{MAKEFILE_LIST} variable
5744 @cindex including (@code{MAKEFILE_LIST} variable)
5745 @item MAKEFILE_LIST
5746 Contains the name of each makefile that is parsed by @code{make}, in
5747 the order in which it was parsed.  The name is appended just
5748 before @code{make} begins to parse the makefile.  Thus, if the first
5749 thing a makefile does is examine the last word in this variable, it
5750 will be the name of the current makefile.  Once the current makefile
5751 has used @code{include}, however, the last word will be the
5752 just-included makefile.
5754 If a makefile named @code{Makefile} has this content:
5756 @example
5757 @group
5758 name1 := $(lastword $(MAKEFILE_LIST))
5760 include inc.mk
5762 name2 := $(lastword $(MAKEFILE_LIST))
5764 all:
5765         @@echo name1 = $(name1)
5766         @@echo name2 = $(name2)
5767 @end group
5768 @end example
5770 @noindent
5771 then you would expect to see this output:
5773 @example
5774 @group
5775 name1 = Makefile
5776 name2 = inc.mk
5777 @end group
5778 @end example
5780 @vindex .DEFAULT_GOAL @r{(define default goal)}
5781 @item .DEFAULT_GOAL
5782 Sets the default goal to be used if no targets were specified on the
5783 command line (@pxref{Goals, , Arguments to Specify the Goals}).  The
5784 @code{.DEFAULT_GOAL} variable allows you to discover the current
5785 default goal, restart the default goal selection algorithm by clearing
5786 its value, or to explicitly set the default goal.  The following
5787 example illustrates these cases:
5789 @example
5790 @group
5791 # Query the default goal.
5792 ifeq ($(.DEFAULT_GOAL),)
5793   $(warning no default goal is set)
5794 endif
5796 .PHONY: foo
5797 foo: ; @@echo $@@
5799 $(warning default goal is $(.DEFAULT_GOAL))
5801 # Reset the default goal.
5802 .DEFAULT_GOAL :=
5804 .PHONY: bar
5805 bar: ; @@echo $@@
5807 $(warning default goal is $(.DEFAULT_GOAL))
5809 # Set our own.
5810 .DEFAULT_GOAL := foo
5811 @end group
5812 @end example
5814 This makefile prints:
5816 @example
5817 @group
5818 no default goal is set
5819 default goal is foo
5820 default goal is bar
5822 @end group
5823 @end example
5825 Note that assigning more than one target name to @code{.DEFAULT_GOAL} is
5826 illegal and will result in an error.
5828 @vindex MAKE_RESTARTS @r{(number of times @code{make} has restarted)}
5829 @item MAKE_RESTARTS
5830 This variable is set only if this instance of @code{make} has
5831 restarted (@pxref{Remaking Makefiles, , How Makefiles Are Remade}): it
5832 will contain the number of times this instance has restarted.  Note
5833 this is not the same as recursion (counted by the @code{MAKELEVEL}
5834 variable).  You should not set, modify, or export this variable.
5836 @vindex .CMDPREFIX @r{(change the recipe prefix character)}
5837 @item .CMDPREFIX
5838 The first character of the value of this variable is used as the
5839 character make assumes is introducing a recipe line.  If the variable
5840 is empty (as it is by default) that character is the standard tab
5841 character.  For example, this is a valid makefile:
5843 @example
5844 @group
5845 .CMDPREFIX = >
5846 all:
5847 > @@echo Hello, world
5848 @end group
5849 @end example
5851 The value of @code{.CMDPREFIX} can be changed multiple times; once set
5852 it stays in effect for all rules parsed until it is modified.
5854 @vindex .VARIABLES @r{(list of variables)}
5855 @item .VARIABLES
5856 Expands to a list of the @emph{names} of all global variables defined
5857 so far.  This includes variables which have empty values, as well as
5858 built-in variables (@pxref{Implicit Variables, , Variables Used by
5859 Implicit Rules}), but does not include any variables which are only
5860 defined in a target-specific context.  Note that any value you assign
5861 to this variable will be ignored; it will always return its special
5862 value.
5864 @c @vindex .TARGETS @r{(list of targets)}
5865 @c @item .TARGETS
5866 @c The second special variable is @code{.TARGETS}.  When expanded, the
5867 @c value consists of a list of all targets defined in all makefiles read
5868 @c up until that point.  Note it's not enough for a file to be simply
5869 @c mentioned in the makefile to be listed in this variable, even if it
5870 @c would match an implicit rule and become an ``implicit target''.  The
5871 @c file must appear as a target, on the left-hand side of a ``:'', to be
5872 @c considered a target for the purposes of this variable.
5874 @vindex .FEATURES @r{(list of supported features)}
5875 @item .FEATURES
5876 Expands to a list of special features supported by this version of
5877 @code{make}.  Possible values include:
5879 @table @samp
5881 @item archives
5882 Supports @code{ar} (archive) files using special filename syntax.
5883 @xref{Archives, ,Using @code{make} to Update Archive Files}.
5885 @item check-symlink
5886 Supports the @code{-L} (@code{--check-symlink-times}) flag.
5887 @xref{Options Summary, ,Summary of Options}.
5889 @item else-if
5890 Supports ``else if'' non-nested conditionals.  @xref{Conditional
5891 Syntax, ,Syntax of Conditionals}.
5893 @item jobserver
5894 Supports ``job server'' enhanced parallel builds.  @xref{Parallel,
5895 ,Parallel Execution}.
5897 @item second-expansion
5898 Supports secondary expansion of prerequisite lists.
5900 @item order-only
5901 Supports order-only prerequisites.  @xref{Prerequisite Types, ,Types
5902 of Prerequisites}.
5904 @item target-specific
5905 Supports target-specific and pattern-specific variable assignments.
5906 @xref{Target-specific, ,Target-specific Variable Values}.
5908 @end table
5910 @vindex .INCLUDE_DIRS @r{(list of include directories)}
5911 @item .INCLUDE_DIRS
5912 Expands to a list of directories that @code{make} searches for
5913 included makefiles (@pxref{Include, , Including Other Makefiles}).
5915 @end table
5917 @node Conditionals, Functions, Using Variables, Top
5918 @chapter Conditional Parts of Makefiles
5920 @cindex conditionals
5921 A @dfn{conditional} directive causes part of a makefile to be obeyed
5922 or ignored depending on the values of variables.  Conditionals can
5923 compare the value of one variable to another, or the value of a
5924 variable to a constant string.  Conditionals control what @code{make}
5925 actually ``sees'' in the makefile, so they @emph{cannot} be used to
5926 control recipes at the time of execution.@refill
5928 @menu
5929 * Conditional Example::         Example of a conditional
5930 * Conditional Syntax::          The syntax of conditionals.
5931 * Testing Flags::               Conditionals that test flags.
5932 @end menu
5934 @node Conditional Example, Conditional Syntax, Conditionals, Conditionals
5935 @section Example of a Conditional
5937 The following example of a conditional tells @code{make} to use one
5938 set of libraries if the @code{CC} variable is @samp{gcc}, and a
5939 different set of libraries otherwise.  It works by controlling which
5940 of two recipe lines will be used for the rule.  The result is that
5941 @samp{CC=gcc} as an argument to @code{make} changes not only which
5942 compiler is used but also which libraries are linked.
5944 @example
5945 libs_for_gcc = -lgnu
5946 normal_libs =
5948 foo: $(objects)
5949 ifeq ($(CC),gcc)
5950         $(CC) -o foo $(objects) $(libs_for_gcc)
5951 else
5952         $(CC) -o foo $(objects) $(normal_libs)
5953 endif
5954 @end example
5956 This conditional uses three directives: one @code{ifeq}, one @code{else}
5957 and one @code{endif}.
5959 The @code{ifeq} directive begins the conditional, and specifies the
5960 condition.  It contains two arguments, separated by a comma and surrounded
5961 by parentheses.  Variable substitution is performed on both arguments and
5962 then they are compared.  The lines of the makefile following the
5963 @code{ifeq} are obeyed if the two arguments match; otherwise they are
5964 ignored.
5966 The @code{else} directive causes the following lines to be obeyed if the
5967 previous conditional failed.  In the example above, this means that the
5968 second alternative linking command is used whenever the first alternative
5969 is not used.  It is optional to have an @code{else} in a conditional.
5971 The @code{endif} directive ends the conditional.  Every conditional must
5972 end with an @code{endif}.  Unconditional makefile text follows.
5974 As this example illustrates, conditionals work at the textual level:
5975 the lines of the conditional are treated as part of the makefile, or
5976 ignored, according to the condition.  This is why the larger syntactic
5977 units of the makefile, such as rules, may cross the beginning or the
5978 end of the conditional.
5980 When the variable @code{CC} has the value @samp{gcc}, the above example has
5981 this effect:
5983 @example
5984 foo: $(objects)
5985         $(CC) -o foo $(objects) $(libs_for_gcc)
5986 @end example
5988 @noindent
5989 When the variable @code{CC} has any other value, the effect is this:
5991 @example
5992 foo: $(objects)
5993         $(CC) -o foo $(objects) $(normal_libs)
5994 @end example
5996 Equivalent results can be obtained in another way by conditionalizing a
5997 variable assignment and then using the variable unconditionally:
5999 @example
6000 libs_for_gcc = -lgnu
6001 normal_libs =
6003 ifeq ($(CC),gcc)
6004   libs=$(libs_for_gcc)
6005 else
6006   libs=$(normal_libs)
6007 endif
6009 foo: $(objects)
6010         $(CC) -o foo $(objects) $(libs)
6011 @end example
6013 @node Conditional Syntax, Testing Flags, Conditional Example, Conditionals
6014 @section Syntax of Conditionals
6015 @findex ifdef
6016 @findex ifeq
6017 @findex ifndef
6018 @findex ifneq
6019 @findex else
6020 @findex endif
6022 The syntax of a simple conditional with no @code{else} is as follows:
6024 @example
6025 @var{conditional-directive}
6026 @var{text-if-true}
6027 endif
6028 @end example
6030 @noindent
6031 The @var{text-if-true} may be any lines of text, to be considered as part
6032 of the makefile if the condition is true.  If the condition is false, no
6033 text is used instead.
6035 The syntax of a complex conditional is as follows:
6037 @example
6038 @var{conditional-directive}
6039 @var{text-if-true}
6040 else
6041 @var{text-if-false}
6042 endif
6043 @end example
6047 @example
6048 @var{conditional-directive}
6049 @var{text-if-one-is-true}
6050 else @var{conditional-directive}
6051 @var{text-if-true}
6052 else
6053 @var{text-if-false}
6054 endif
6055 @end example
6057 @noindent
6058 There can be as many ``@code{else} @var{conditional-directive}''
6059 clauses as necessary.  Once a given condition is true,
6060 @var{text-if-true} is used and no other clause is used; if no
6061 condition is true then @var{text-if-false} is used.  The
6062 @var{text-if-true} and @var{text-if-false} can be any number of lines
6063 of text.
6065 The syntax of the @var{conditional-directive} is the same whether the
6066 conditional is simple or complex; after an @code{else} or not.  There
6067 are four different directives that test different conditions.  Here is
6068 a table of them:
6070 @table @code
6071 @item ifeq (@var{arg1}, @var{arg2})
6072 @itemx ifeq '@var{arg1}' '@var{arg2}'
6073 @itemx ifeq "@var{arg1}" "@var{arg2}"
6074 @itemx ifeq "@var{arg1}" '@var{arg2}'
6075 @itemx ifeq '@var{arg1}' "@var{arg2}"
6076 Expand all variable references in @var{arg1} and @var{arg2} and
6077 compare them.  If they are identical, the @var{text-if-true} is
6078 effective; otherwise, the @var{text-if-false}, if any, is effective.
6080 Often you want to test if a variable has a non-empty value.  When the
6081 value results from complex expansions of variables and functions,
6082 expansions you would consider empty may actually contain whitespace
6083 characters and thus are not seen as empty.  However, you can use the
6084 @code{strip} function (@pxref{Text Functions}) to avoid interpreting
6085 whitespace as a non-empty value.  For example:
6087 @example
6088 @group
6089 ifeq ($(strip $(foo)),)
6090 @var{text-if-empty}
6091 endif
6092 @end group
6093 @end example
6095 @noindent
6096 will evaluate @var{text-if-empty} even if the expansion of
6097 @code{$(foo)} contains whitespace characters.
6099 @item ifneq (@var{arg1}, @var{arg2})
6100 @itemx ifneq '@var{arg1}' '@var{arg2}'
6101 @itemx ifneq "@var{arg1}" "@var{arg2}"
6102 @itemx ifneq "@var{arg1}" '@var{arg2}'
6103 @itemx ifneq '@var{arg1}' "@var{arg2}"
6104 Expand all variable references in @var{arg1} and @var{arg2} and
6105 compare them.  If they are different, the @var{text-if-true} is
6106 effective; otherwise, the @var{text-if-false}, if any, is effective.
6108 @item ifdef @var{variable-name}
6109 The @code{ifdef} form takes the @emph{name} of a variable as its
6110 argument, not a reference to a variable.  The value of that variable
6111 has a non-empty value, the @var{text-if-true} is effective; otherwise,
6112 the @var{text-if-false}, if any, is effective.  Variables that have
6113 never been defined have an empty value.  The text @var{variable-name}
6114 is expanded, so it could be a variable or function that expands
6115 to the name of a variable.  For example:
6117 @example
6118 bar = true
6119 foo = bar
6120 ifdef $(foo)
6121 frobozz = yes
6122 endif
6123 @end example
6125 The variable reference @code{$(foo)} is expanded, yielding @code{bar},
6126 which is considered to be the name of a variable.  The variable
6127 @code{bar} is not expanded, but its value is examined to determine if
6128 it is non-empty.
6130 Note that @code{ifdef} only tests whether a variable has a value.  It
6131 does not expand the variable to see if that value is nonempty.
6132 Consequently, tests using @code{ifdef} return true for all definitions
6133 except those like @code{foo =}.  To test for an empty value, use
6134 @w{@code{ifeq ($(foo),)}}.  For example,
6136 @example
6137 bar =
6138 foo = $(bar)
6139 ifdef foo
6140 frobozz = yes
6141 else
6142 frobozz = no
6143 endif
6144 @end example
6146 @noindent
6147 sets @samp{frobozz} to @samp{yes}, while:
6149 @example
6150 foo =
6151 ifdef foo
6152 frobozz = yes
6153 else
6154 frobozz = no
6155 endif
6156 @end example
6158 @noindent
6159 sets @samp{frobozz} to @samp{no}.
6161 @item ifndef @var{variable-name}
6162 If the variable @var{variable-name} has an empty value, the
6163 @var{text-if-true} is effective; otherwise, the @var{text-if-false},
6164 if any, is effective.  The rules for expansion and testing of
6165 @var{variable-name} are identical to the @code{ifdef} directive.
6166 @end table
6168 Extra spaces are allowed and ignored at the beginning of the
6169 conditional directive line, but a tab is not allowed.  (If the line
6170 begins with a tab, it will be considered part of a recipe for a rule.)
6171 Aside from this, extra spaces or tabs may be inserted with no effect
6172 anywhere except within the directive name or within an argument.  A
6173 comment starting with @samp{#} may appear at the end of the line.
6175 The other two directives that play a part in a conditional are @code{else}
6176 and @code{endif}.  Each of these directives is written as one word, with no
6177 arguments.  Extra spaces are allowed and ignored at the beginning of the
6178 line, and spaces or tabs at the end.  A comment starting with @samp{#} may
6179 appear at the end of the line.
6181 Conditionals affect which lines of the makefile @code{make} uses.  If
6182 the condition is true, @code{make} reads the lines of the
6183 @var{text-if-true} as part of the makefile; if the condition is false,
6184 @code{make} ignores those lines completely.  It follows that syntactic
6185 units of the makefile, such as rules, may safely be split across the
6186 beginning or the end of the conditional.@refill
6188 @code{make} evaluates conditionals when it reads a makefile.
6189 Consequently, you cannot use automatic variables in the tests of
6190 conditionals because they are not defined until recipes are run
6191 (@pxref{Automatic Variables}).
6193 To prevent intolerable confusion, it is not permitted to start a
6194 conditional in one makefile and end it in another.  However, you may
6195 write an @code{include} directive within a conditional, provided you do
6196 not attempt to terminate the conditional inside the included file.
6198 @node Testing Flags,  , Conditional Syntax, Conditionals
6199 @section Conditionals that Test Flags
6201 You can write a conditional that tests @code{make} command flags such as
6202 @samp{-t} by using the variable @code{MAKEFLAGS} together with the
6203 @code{findstring} function
6204 (@pxref{Text Functions, , Functions for String Substitution and Analysis}).
6205 This is useful when @code{touch} is not enough to make a file appear up
6206 to date.
6208 The @code{findstring} function determines whether one string appears as a
6209 substring of another.  If you want to test for the @samp{-t} flag,
6210 use @samp{t} as the first string and the value of @code{MAKEFLAGS} as
6211 the other.
6213 For example, here is how to arrange to use @samp{ranlib -t} to finish
6214 marking an archive file up to date:
6216 @example
6217 archive.a: @dots{}
6218 ifneq (,$(findstring t,$(MAKEFLAGS)))
6219         +touch archive.a
6220         +ranlib -t archive.a
6221 else
6222         ranlib archive.a
6223 endif
6224 @end example
6226 @noindent
6227 The @samp{+} prefix marks those recipe lines as ``recursive'' so that
6228 they will be executed despite use of the @samp{-t} flag.
6229 @xref{Recursion, ,Recursive Use of @code{make}}.
6231 @node Functions, Running, Conditionals, Top
6232 @chapter Functions for Transforming Text
6233 @cindex functions
6235 @dfn{Functions} allow you to do text processing in the makefile to
6236 compute the files to operate on or the commands to use in recipes.
6237 You use a function in a @dfn{function call}, where you give the name
6238 of the function and some text (the @dfn{arguments}) for the function
6239 to operate on.  The result of the function's processing is substituted
6240 into the makefile at the point of the call, just as a variable might
6241 be substituted.
6243 @menu
6244 * Syntax of Functions::         How to write a function call.
6245 * Text Functions::              General-purpose text manipulation functions.
6246 * File Name Functions::         Functions for manipulating file names.
6247 * Conditional Functions::       Functions that implement conditions.
6248 * Foreach Function::            Repeat some text with controlled variation.
6249 * Call Function::               Expand a user-defined function.
6250 * Value Function::              Return the un-expanded value of a variable.
6251 * Eval Function::               Evaluate the arguments as makefile syntax.
6252 * Origin Function::             Find where a variable got its value.
6253 * Flavor Function::             Find out the flavor of a variable.
6254 * Shell Function::              Substitute the output of a shell command.
6255 * Make Control Functions::      Functions that control how make runs.
6256 @end menu
6258 @node Syntax of Functions, Text Functions, Functions, Functions
6259 @section Function Call Syntax
6260 @cindex @code{$}, in function call
6261 @cindex dollar sign (@code{$}), in function call
6262 @cindex arguments of functions
6263 @cindex functions, syntax of
6265 A function call resembles a variable reference.  It looks like this:
6267 @example
6268 $(@var{function} @var{arguments})
6269 @end example
6271 @noindent
6272 or like this:
6274 @example
6275 $@{@var{function} @var{arguments}@}
6276 @end example
6278 Here @var{function} is a function name; one of a short list of names
6279 that are part of @code{make}.  You can also essentially create your own
6280 functions by using the @code{call} builtin function.
6282 The @var{arguments} are the arguments of the function.  They are
6283 separated from the function name by one or more spaces or tabs, and if
6284 there is more than one argument, then they are separated by commas.
6285 Such whitespace and commas are not part of an argument's value.  The
6286 delimiters which you use to surround the function call, whether
6287 parentheses or braces, can appear in an argument only in matching pairs;
6288 the other kind of delimiters may appear singly.  If the arguments
6289 themselves contain other function calls or variable references, it is
6290 wisest to use the same kind of delimiters for all the references; write
6291 @w{@samp{$(subst a,b,$(x))}}, not @w{@samp{$(subst a,b,$@{x@})}}.  This
6292 is because it is clearer, and because only one type of delimiter is
6293 matched to find the end of the reference.
6295 The text written for each argument is processed by substitution of
6296 variables and function calls to produce the argument value, which
6297 is the text on which the function acts.  The substitution is done in the
6298 order in which the arguments appear.
6300 Commas and unmatched parentheses or braces cannot appear in the text of an
6301 argument as written; leading spaces cannot appear in the text of the first
6302 argument as written.  These characters can be put into the argument value
6303 by variable substitution.  First define variables @code{comma} and
6304 @code{space} whose values are isolated comma and space characters, then
6305 substitute these variables where such characters are wanted, like this:
6307 @example
6308 @group
6309 comma:= ,
6310 empty:=
6311 space:= $(empty) $(empty)
6312 foo:= a b c
6313 bar:= $(subst $(space),$(comma),$(foo))
6314 # @r{bar is now `a,b,c'.}
6315 @end group
6316 @end example
6318 @noindent
6319 Here the @code{subst} function replaces each space with a comma, through
6320 the value of @code{foo}, and substitutes the result.
6322 @node Text Functions, File Name Functions, Syntax of Functions, Functions
6323 @section Functions for String Substitution and Analysis
6324 @cindex functions, for text
6326 Here are some functions that operate on strings:
6328 @table @code
6329 @item $(subst @var{from},@var{to},@var{text})
6330 @findex subst
6331 Performs a textual replacement on the text @var{text}: each occurrence
6332 of @var{from} is replaced by @var{to}.  The result is substituted for
6333 the function call.  For example,
6335 @example
6336 $(subst ee,EE,feet on the street)
6337 @end example
6339 substitutes the string @samp{fEEt on the strEEt}.
6341 @item $(patsubst @var{pattern},@var{replacement},@var{text})
6342 @findex patsubst
6343 Finds whitespace-separated words in @var{text} that match
6344 @var{pattern} and replaces them with @var{replacement}.  Here
6345 @var{pattern} may contain a @samp{%} which acts as a wildcard,
6346 matching any number of any characters within a word.  If
6347 @var{replacement} also contains a @samp{%}, the @samp{%} is replaced
6348 by the text that matched the @samp{%} in @var{pattern}.  Only the first
6349 @samp{%} in the @var{pattern} and @var{replacement} is treated this
6350 way; any subsequent @samp{%} is unchanged.@refill
6352 @cindex @code{%}, quoting in @code{patsubst}
6353 @cindex @code{%}, quoting with @code{\} (backslash)
6354 @cindex @code{\} (backslash), to quote @code{%}
6355 @cindex backslash (@code{\}), to quote @code{%}
6356 @cindex quoting @code{%}, in @code{patsubst}
6357 @samp{%} characters in @code{patsubst} function invocations can be
6358 quoted with preceding backslashes (@samp{\}).  Backslashes that would
6359 otherwise quote @samp{%} characters can be quoted with more backslashes.
6360 Backslashes that quote @samp{%} characters or other backslashes are
6361 removed from the pattern before it is compared file names or has a stem
6362 substituted into it.  Backslashes that are not in danger of quoting
6363 @samp{%} characters go unmolested.  For example, the pattern
6364 @file{the\%weird\\%pattern\\} has @samp{the%weird\} preceding the
6365 operative @samp{%} character, and @samp{pattern\\} following it.  The
6366 final two backslashes are left alone because they cannot affect any
6367 @samp{%} character.@refill
6369 Whitespace between words is folded into single space characters;
6370 leading and trailing whitespace is discarded.
6372 For example,
6374 @example
6375 $(patsubst %.c,%.o,x.c.c bar.c)
6376 @end example
6378 @noindent
6379 produces the value @samp{x.c.o bar.o}.
6381 Substitution references (@pxref{Substitution Refs, ,Substitution
6382 References}) are a simpler way to get the effect of the @code{patsubst}
6383 function:
6385 @example
6386 $(@var{var}:@var{pattern}=@var{replacement})
6387 @end example
6389 @noindent
6390 is equivalent to
6392 @example
6393 $(patsubst @var{pattern},@var{replacement},$(@var{var}))
6394 @end example
6396 The second shorthand simplifies one of the most common uses of
6397 @code{patsubst}: replacing the suffix at the end of file names.
6399 @example
6400 $(@var{var}:@var{suffix}=@var{replacement})
6401 @end example
6403 @noindent
6404 is equivalent to
6406 @example
6407 $(patsubst %@var{suffix},%@var{replacement},$(@var{var}))
6408 @end example
6410 @noindent
6411 For example, you might have a list of object files:
6413 @example
6414 objects = foo.o bar.o baz.o
6415 @end example
6417 @noindent
6418 To get the list of corresponding source files, you could simply write:
6420 @example
6421 $(objects:.o=.c)
6422 @end example
6424 @noindent
6425 instead of using the general form:
6427 @example
6428 $(patsubst %.o,%.c,$(objects))
6429 @end example
6431 @item $(strip @var{string})
6432 @cindex stripping whitespace
6433 @cindex whitespace, stripping
6434 @cindex spaces, stripping
6435 @findex strip
6436 Removes leading and trailing whitespace from @var{string} and replaces
6437 each internal sequence of one or more whitespace characters with a
6438 single space.  Thus, @samp{$(strip a b  c )} results in @w{@samp{a b c}}.
6440 The function @code{strip} can be very useful when used in conjunction
6441 with conditionals.  When comparing something with the empty string
6442 @samp{} using @code{ifeq} or @code{ifneq}, you usually want a string of
6443 just whitespace to match the empty string (@pxref{Conditionals}).
6445 Thus, the following may fail to have the desired results:
6447 @example
6448 .PHONY: all
6449 ifneq   "$(needs_made)" ""
6450 all: $(needs_made)
6451 else
6452 all:;@@echo 'Nothing to make!'
6453 endif
6454 @end example
6456 @noindent
6457 Replacing the variable reference @w{@samp{$(needs_made)}} with the
6458 function call @w{@samp{$(strip $(needs_made))}} in the @code{ifneq}
6459 directive would make it more robust.@refill
6461 @item $(findstring @var{find},@var{in})
6462 @findex findstring
6463 @cindex searching for strings
6464 @cindex finding strings
6465 @cindex strings, searching for
6466 Searches @var{in} for an occurrence of @var{find}.  If it occurs, the
6467 value is @var{find}; otherwise, the value is empty.  You can use this
6468 function in a conditional to test for the presence of a specific
6469 substring in a given string.  Thus, the two examples,
6471 @example
6472 $(findstring a,a b c)
6473 $(findstring a,b c)
6474 @end example
6476 @noindent
6477 produce the values @samp{a} and @samp{} (the empty string),
6478 respectively.  @xref{Testing Flags}, for a practical application of
6479 @code{findstring}.@refill
6481 @need 750
6482 @findex filter
6483 @cindex filtering words
6484 @cindex words, filtering
6485 @item $(filter @var{pattern}@dots{},@var{text})
6486 Returns all whitespace-separated words in @var{text} that @emph{do} match
6487 any of the @var{pattern} words, removing any words that @emph{do not}
6488 match.  The patterns are written using @samp{%}, just like the patterns
6489 used in the @code{patsubst} function above.@refill
6491 The @code{filter} function can be used to separate out different types
6492 of strings (such as file names) in a variable.  For example:
6494 @example
6495 sources := foo.c bar.c baz.s ugh.h
6496 foo: $(sources)
6497         cc $(filter %.c %.s,$(sources)) -o foo
6498 @end example
6500 @noindent
6501 says that @file{foo} depends of @file{foo.c}, @file{bar.c},
6502 @file{baz.s} and @file{ugh.h} but only @file{foo.c}, @file{bar.c} and
6503 @file{baz.s} should be specified in the command to the
6504 compiler.@refill
6506 @item $(filter-out @var{pattern}@dots{},@var{text})
6507 @findex filter-out
6508 @cindex filtering out words
6509 @cindex words, filtering out
6510 Returns all whitespace-separated words in @var{text} that @emph{do not}
6511 match any of the @var{pattern} words, removing the words that @emph{do}
6512 match one or more.  This is the exact opposite of the @code{filter}
6513 function.@refill
6515 For example, given:
6517 @example
6518 @group
6519 objects=main1.o foo.o main2.o bar.o
6520 mains=main1.o main2.o
6521 @end group
6522 @end example
6524 @noindent
6525 the following generates a list which contains all the object files not
6526 in @samp{mains}:
6528 @example
6529 $(filter-out $(mains),$(objects))
6530 @end example
6532 @need 1500
6533 @findex sort
6534 @cindex sorting words
6535 @item $(sort @var{list})
6536 Sorts the words of @var{list} in lexical order, removing duplicate
6537 words.  The output is a list of words separated by single spaces.
6538 Thus,
6540 @example
6541 $(sort foo bar lose)
6542 @end example
6544 @noindent
6545 returns the value @samp{bar foo lose}.
6547 @cindex removing duplicate words
6548 @cindex duplicate words, removing
6549 @cindex words, removing duplicates
6550 Incidentally, since @code{sort} removes duplicate words, you can use
6551 it for this purpose even if you don't care about the sort order.
6553 @item $(word @var{n},@var{text})
6554 @findex word
6555 @cindex word, selecting a
6556 @cindex selecting a word
6557 Returns the @var{n}th word of @var{text}.  The legitimate values of
6558 @var{n} start from 1.  If @var{n} is bigger than the number of words
6559 in @var{text}, the value is empty.  For example,
6561 @example
6562 $(word 2, foo bar baz)
6563 @end example
6565 @noindent
6566 returns @samp{bar}.
6568 @item $(wordlist @var{s},@var{e},@var{text})
6569 @findex wordlist
6570 @cindex words, selecting lists of
6571 @cindex selecting word lists
6572 Returns the list of words in @var{text} starting with word @var{s} and
6573 ending with word @var{e} (inclusive).  The legitimate values of @var{s}
6574 start from 1; @var{e} may start from 0.  If @var{s} is bigger than the
6575 number of words in @var{text}, the value is empty.  If @var{e} is
6576 bigger than the number of words in @var{text}, words up to the end of
6577 @var{text} are returned.  If @var{s} is greater than @var{e}, nothing
6578 is returned.  For example,
6580 @example
6581 $(wordlist 2, 3, foo bar baz)
6582 @end example
6584 @noindent
6585 returns @samp{bar baz}.
6587 @c Following item phrased to prevent overfull hbox.  --RJC 17 Jul 92
6588 @item $(words @var{text})
6589 @findex words
6590 @cindex words, finding number
6591 Returns the number of words in @var{text}.
6592 Thus, the last word of @var{text} is
6593 @w{@code{$(word $(words @var{text}),@var{text})}}.@refill
6595 @item $(firstword @var{names}@dots{})
6596 @findex firstword
6597 @cindex words, extracting first
6598 The argument @var{names} is regarded as a series of names, separated
6599 by whitespace.  The value is the first name in the series.  The rest
6600 of the names are ignored.
6602 For example,
6604 @example
6605 $(firstword foo bar)
6606 @end example
6608 @noindent
6609 produces the result @samp{foo}.  Although @code{$(firstword
6610 @var{text})} is the same as @code{$(word 1,@var{text})}, the
6611 @code{firstword} function is retained for its simplicity.@refill
6614 @item $(lastword @var{names}@dots{})
6615 @findex lastword
6616 @cindex words, extracting last
6617 The argument @var{names} is regarded as a series of names, separated
6618 by whitespace.  The value is the last name in the series.
6620 For example,
6622 @example
6623 $(lastword foo bar)
6624 @end example
6626 @noindent
6627 produces the result @samp{bar}.  Although @code{$(lastword
6628 @var{text})} is the same as @code{$(word $(words @var{text}),@var{text})},
6629 the @code{lastword} function was added for its simplicity and better
6630 performance.@refill
6631 @end table
6634 Here is a realistic example of the use of @code{subst} and
6635 @code{patsubst}.  Suppose that a makefile uses the @code{VPATH} variable
6636 to specify a list of directories that @code{make} should search for
6637 prerequisite files
6638 (@pxref{General Search, , @code{VPATH} Search Path for All Prerequisites}).
6639 This example shows how to
6640 tell the C compiler to search for header files in the same list of
6641 directories.@refill
6643 The value of @code{VPATH} is a list of directories separated by colons,
6644 such as @samp{src:../headers}.  First, the @code{subst} function is used to
6645 change the colons to spaces:
6647 @example
6648 $(subst :, ,$(VPATH))
6649 @end example
6651 @noindent
6652 This produces @samp{src ../headers}.  Then @code{patsubst} is used to turn
6653 each directory name into a @samp{-I} flag.  These can be added to the
6654 value of the variable @code{CFLAGS}, which is passed automatically to the C
6655 compiler, like this:
6657 @example
6658 override CFLAGS += $(patsubst %,-I%,$(subst :, ,$(VPATH)))
6659 @end example
6661 @noindent
6662 The effect is to append the text @samp{-Isrc -I../headers} to the
6663 previously given value of @code{CFLAGS}.  The @code{override} directive is
6664 used so that the new value is assigned even if the previous value of
6665 @code{CFLAGS} was specified with a command argument (@pxref{Override
6666 Directive, , The @code{override} Directive}).
6668 @node File Name Functions, Conditional Functions, Text Functions, Functions
6669 @section Functions for File Names
6670 @cindex functions, for file names
6671 @cindex file name functions
6673 Several of the built-in expansion functions relate specifically to
6674 taking apart file names or lists of file names.
6676 Each of the following functions performs a specific transformation on a
6677 file name.  The argument of the function is regarded as a series of file
6678 names, separated by whitespace.  (Leading and trailing whitespace is
6679 ignored.)  Each file name in the series is transformed in the same way and
6680 the results are concatenated with single spaces between them.
6682 @table @code
6683 @item $(dir @var{names}@dots{})
6684 @findex dir
6685 @cindex directory part
6686 @cindex file name, directory part
6687 Extracts the directory-part of each file name in @var{names}.  The
6688 directory-part of the file name is everything up through (and
6689 including) the last slash in it.  If the file name contains no slash,
6690 the directory part is the string @samp{./}.  For example,
6692 @example
6693 $(dir src/foo.c hacks)
6694 @end example
6696 @noindent
6697 produces the result @samp{src/ ./}.
6699 @item $(notdir @var{names}@dots{})
6700 @findex notdir
6701 @cindex file name, nondirectory part
6702 @cindex nondirectory part
6703 Extracts all but the directory-part of each file name in @var{names}.
6704 If the file name contains no slash, it is left unchanged.  Otherwise,
6705 everything through the last slash is removed from it.
6707 A file name that ends with a slash becomes an empty string.  This is
6708 unfortunate, because it means that the result does not always have the
6709 same number of whitespace-separated file names as the argument had;
6710 but we do not see any other valid alternative.
6712 For example,
6714 @example
6715 $(notdir src/foo.c hacks)
6716 @end example
6718 @noindent
6719 produces the result @samp{foo.c hacks}.
6721 @item $(suffix @var{names}@dots{})
6722 @findex suffix
6723 @cindex suffix, function to find
6724 @cindex file name suffix
6725 Extracts the suffix of each file name in @var{names}.  If the file name
6726 contains a period, the suffix is everything starting with the last
6727 period.  Otherwise, the suffix is the empty string.  This frequently
6728 means that the result will be empty when @var{names} is not, and if
6729 @var{names} contains multiple file names, the result may contain fewer
6730 file names.
6732 For example,
6734 @example
6735 $(suffix src/foo.c src-1.0/bar.c hacks)
6736 @end example
6738 @noindent
6739 produces the result @samp{.c .c}.
6741 @item $(basename @var{names}@dots{})
6742 @findex basename
6743 @cindex basename
6744 @cindex file name, basename of
6745 Extracts all but the suffix of each file name in @var{names}.  If the
6746 file name contains a period, the basename is everything starting up to
6747 (and not including) the last period.  Periods in the directory part are
6748 ignored.  If there is no period, the basename is the entire file name.
6749 For example,
6751 @example
6752 $(basename src/foo.c src-1.0/bar hacks)
6753 @end example
6755 @noindent
6756 produces the result @samp{src/foo src-1.0/bar hacks}.
6758 @c plural convention with dots (be consistent)
6759 @item $(addsuffix @var{suffix},@var{names}@dots{})
6760 @findex addsuffix
6761 @cindex suffix, adding
6762 @cindex file name suffix, adding
6763 The argument @var{names} is regarded as a series of names, separated
6764 by whitespace; @var{suffix} is used as a unit.  The value of
6765 @var{suffix} is appended to the end of each individual name and the
6766 resulting larger names are concatenated with single spaces between
6767 them.  For example,
6769 @example
6770 $(addsuffix .c,foo bar)
6771 @end example
6773 @noindent
6774 produces the result @samp{foo.c bar.c}.
6776 @item $(addprefix @var{prefix},@var{names}@dots{})
6777 @findex addprefix
6778 @cindex prefix, adding
6779 @cindex file name prefix, adding
6780 The argument @var{names} is regarded as a series of names, separated
6781 by whitespace; @var{prefix} is used as a unit.  The value of
6782 @var{prefix} is prepended to the front of each individual name and the
6783 resulting larger names are concatenated with single spaces between
6784 them.  For example,
6786 @example
6787 $(addprefix src/,foo bar)
6788 @end example
6790 @noindent
6791 produces the result @samp{src/foo src/bar}.
6793 @item $(join @var{list1},@var{list2})
6794 @findex join
6795 @cindex joining lists of words
6796 @cindex words, joining lists
6797 Concatenates the two arguments word by word: the two first words (one
6798 from each argument) concatenated form the first word of the result, the
6799 two second words form the second word of the result, and so on.  So the
6800 @var{n}th word of the result comes from the @var{n}th word of each
6801 argument.  If one argument has more words that the other, the extra
6802 words are copied unchanged into the result.
6804 For example, @samp{$(join a b,.c .o)} produces @samp{a.c b.o}.
6806 Whitespace between the words in the lists is not preserved; it is
6807 replaced with a single space.
6809 This function can merge the results of the @code{dir} and
6810 @code{notdir} functions, to produce the original list of files which
6811 was given to those two functions.@refill
6813 @item $(wildcard @var{pattern})
6814 @findex wildcard
6815 @cindex wildcard, function
6816 The argument @var{pattern} is a file name pattern, typically containing
6817 wildcard characters (as in shell file name patterns).  The result of
6818 @code{wildcard} is a space-separated list of the names of existing files
6819 that match the pattern.
6820 @xref{Wildcards, ,Using Wildcard Characters in File Names}.
6822 @item $(realpath @var{names}@dots{})
6823 @findex realpath
6824 @cindex realpath
6825 @cindex file name, realpath of
6826 For each file name in @var{names} return the canonical absolute name.
6827 A canonical name does not contain any @code{.} or @code{..} components,
6828 nor any repeated path separators (@code{/}) or symlinks.  In case of a
6829 failure the empty string is returned.  Consult the @code{realpath(3)}
6830 documentation for a list of possible failure causes.
6832 @item $(abspath @var{names}@dots{})
6833 @findex abspath
6834 @cindex abspath
6835 @cindex file name, abspath of
6836 For each file name in @var{names} return an absolute name that does
6837 not contain any @code{.} or @code{..} components, nor any repeated path
6838 separators (@code{/}).  Note that, in contrast to @code{realpath}
6839 function, @code{abspath} does not resolve symlinks and does not require
6840 the file names to refer to an existing file or directory.  Use the
6841 @code{wildcard} function to test for existence.
6842 @end table
6844 @node Conditional Functions, Foreach Function, File Name Functions, Functions
6845 @section Functions for Conditionals
6846 @findex if
6847 @cindex conditional expansion
6848 There are three functions that provide conditional expansion.  A key
6849 aspect of these functions is that not all of the arguments are
6850 expanded initially.  Only those arguments which need to be expanded,
6851 will be expanded.
6853 @table @code
6854 @item $(if @var{condition},@var{then-part}[,@var{else-part}])
6855 @findex if
6856 The @code{if} function provides support for conditional expansion in a
6857 functional context (as opposed to the GNU @code{make} makefile
6858 conditionals such as @code{ifeq} (@pxref{Conditional Syntax, ,Syntax of
6859 Conditionals}).
6861 The first argument, @var{condition}, first has all preceding and
6862 trailing whitespace stripped, then is expanded.  If it expands to any
6863 non-empty string, then the condition is considered to be true.  If it
6864 expands to an empty string, the condition is considered to be false.
6866 If the condition is true then the second argument, @var{then-part}, is
6867 evaluated and this is used as the result of the evaluation of the entire
6868 @code{if} function.
6870 If the condition is false then the third argument, @var{else-part}, is
6871 evaluated and this is the result of the @code{if} function.  If there is
6872 no third argument, the @code{if} function evaluates to nothing (the
6873 empty string).
6875 Note that only one of the @var{then-part} or the @var{else-part} will be
6876 evaluated, never both.  Thus, either can contain side-effects (such as
6877 @code{shell} function calls, etc.)
6879 @item $(or @var{condition1}[,@var{condition2}[,@var{condition3}@dots{}]])
6880 @findex or
6881 The @code{or} function provides a ``short-circuiting'' OR operation.
6882 Each argument is expanded, in order.  If an argument expands to a
6883 non-empty string the processing stops and the result of the expansion
6884 is that string.  If, after all arguments are expanded, all of them are
6885 false (empty), then the result of the expansion is the empty string.
6887 @item $(and @var{condition1}[,@var{condition2}[,@var{condition3}@dots{}]])
6888 @findex and
6889 The @code{and} function provides a ``short-circuiting'' AND operation.
6890 Each argument is expanded, in order.  If an argument expands to an
6891 empty string the processing stops and the result of the expansion is
6892 the empty string.  If all arguments expand to a non-empty string then
6893 the result of the expansion is the expansion of the last argument.
6895 @end table
6897 @node Foreach Function, Call Function, Conditional Functions, Functions
6898 @section The @code{foreach} Function
6899 @findex foreach
6900 @cindex words, iterating over
6902 The @code{foreach} function is very different from other functions.  It
6903 causes one piece of text to be used repeatedly, each time with a different
6904 substitution performed on it.  It resembles the @code{for} command in the
6905 shell @code{sh} and the @code{foreach} command in the C-shell @code{csh}.
6907 The syntax of the @code{foreach} function is:
6909 @example
6910 $(foreach @var{var},@var{list},@var{text})
6911 @end example
6913 @noindent
6914 The first two arguments, @var{var} and @var{list}, are expanded before
6915 anything else is done; note that the last argument, @var{text}, is
6916 @strong{not} expanded at the same time.  Then for each word of the expanded
6917 value of @var{list}, the variable named by the expanded value of @var{var}
6918 is set to that word, and @var{text} is expanded.  Presumably @var{text}
6919 contains references to that variable, so its expansion will be different
6920 each time.
6922 The result is that @var{text} is expanded as many times as there are
6923 whitespace-separated words in @var{list}.  The multiple expansions of
6924 @var{text} are concatenated, with spaces between them, to make the result
6925 of @code{foreach}.
6927 This simple example sets the variable @samp{files} to the list of all files
6928 in the directories in the list @samp{dirs}:
6930 @example
6931 dirs := a b c d
6932 files := $(foreach dir,$(dirs),$(wildcard $(dir)/*))
6933 @end example
6935 Here @var{text} is @samp{$(wildcard $(dir)/*)}.  The first repetition
6936 finds the value @samp{a} for @code{dir}, so it produces the same result
6937 as @samp{$(wildcard a/*)}; the second repetition produces the result
6938 of @samp{$(wildcard b/*)}; and the third, that of @samp{$(wildcard c/*)}.
6940 This example has the same result (except for setting @samp{dirs}) as
6941 the following example:
6943 @example
6944 files := $(wildcard a/* b/* c/* d/*)
6945 @end example
6947 When @var{text} is complicated, you can improve readability by giving it
6948 a name, with an additional variable:
6950 @example
6951 find_files = $(wildcard $(dir)/*)
6952 dirs := a b c d
6953 files := $(foreach dir,$(dirs),$(find_files))
6954 @end example
6956 @noindent
6957 Here we use the variable @code{find_files} this way.  We use plain @samp{=}
6958 to define a recursively-expanding variable, so that its value contains an
6959 actual function call to be reexpanded under the control of @code{foreach};
6960 a simply-expanded variable would not do, since @code{wildcard} would be
6961 called only once at the time of defining @code{find_files}.
6963 The @code{foreach} function has no permanent effect on the variable
6964 @var{var}; its value and flavor after the @code{foreach} function call are
6965 the same as they were beforehand.  The other values which are taken from
6966 @var{list} are in effect only temporarily, during the execution of
6967 @code{foreach}.  The variable @var{var} is a simply-expanded variable
6968 during the execution of @code{foreach}.  If @var{var} was undefined
6969 before the @code{foreach} function call, it is undefined after the call.
6970 @xref{Flavors, ,The Two Flavors of Variables}.@refill
6972 You must take care when using complex variable expressions that result in
6973 variable names because many strange things are valid variable names, but
6974 are probably not what you intended.  For example,
6976 @smallexample
6977 files := $(foreach Esta escrito en espanol!,b c ch,$(find_files))
6978 @end smallexample
6980 @noindent
6981 might be useful if the value of @code{find_files} references the variable
6982 whose name is @samp{Esta escrito en espanol!} (es un nombre bastante largo,
6983 no?), but it is more likely to be a mistake.
6985 @node Call Function, Value Function, Foreach Function, Functions
6986 @section The @code{call} Function
6987 @findex call
6988 @cindex functions, user defined
6989 @cindex user defined functions
6991 The @code{call} function is unique in that it can be used to create new
6992 parameterized functions.  You can write a complex expression as the
6993 value of a variable, then use @code{call} to expand it with different
6994 values.
6996 The syntax of the @code{call} function is:
6998 @example
6999 $(call @var{variable},@var{param},@var{param},@dots{})
7000 @end example
7002 When @code{make} expands this function, it assigns each @var{param} to
7003 temporary variables @code{$(1)}, @code{$(2)}, etc.  The variable
7004 @code{$(0)} will contain @var{variable}.  There is no maximum number of
7005 parameter arguments.  There is no minimum, either, but it doesn't make
7006 sense to use @code{call} with no parameters.
7008 Then @var{variable} is expanded as a @code{make} variable in the context
7009 of these temporary assignments.  Thus, any reference to @code{$(1)} in
7010 the value of @var{variable} will resolve to the first @var{param} in the
7011 invocation of @code{call}.
7013 Note that @var{variable} is the @emph{name} of a variable, not a
7014 @emph{reference} to that variable.  Therefore you would not normally use
7015 a @samp{$} or parentheses when writing it.  (You can, however, use a
7016 variable reference in the name if you want the name not to be a
7017 constant.)
7019 If @var{variable} is the name of a builtin function, the builtin function
7020 is always invoked (even if a @code{make} variable by that name also
7021 exists).
7023 The @code{call} function expands the @var{param} arguments before
7024 assigning them to temporary variables.  This means that @var{variable}
7025 values containing references to builtin functions that have special
7026 expansion rules, like @code{foreach} or @code{if}, may not work as you
7027 expect.
7029 Some examples may make this clearer.
7031 This macro simply reverses its arguments:
7033 @smallexample
7034 reverse = $(2) $(1)
7036 foo = $(call reverse,a,b)
7037 @end smallexample
7039 @noindent
7040 Here @var{foo} will contain @samp{b a}.
7042 This one is slightly more interesting: it defines a macro to search for
7043 the first instance of a program in @code{PATH}:
7045 @smallexample
7046 pathsearch = $(firstword $(wildcard $(addsuffix /$(1),$(subst :, ,$(PATH)))))
7048 LS := $(call pathsearch,ls)
7049 @end smallexample
7051 @noindent
7052 Now the variable LS contains @code{/bin/ls} or similar.
7054 The @code{call} function can be nested.  Each recursive invocation gets
7055 its own local values for @code{$(1)}, etc.@: that mask the values of
7056 higher-level @code{call}.  For example, here is an implementation of a
7057 @dfn{map} function:
7059 @smallexample
7060 map = $(foreach a,$(2),$(call $(1),$(a)))
7061 @end smallexample
7063 Now you can @var{map} a function that normally takes only one argument,
7064 such as @code{origin}, to multiple values in one step:
7066 @smallexample
7067 o = $(call map,origin,o map MAKE)
7068 @end smallexample
7070 and end up with @var{o} containing something like @samp{file file default}.
7072 A final caution: be careful when adding whitespace to the arguments to
7073 @code{call}.  As with other functions, any whitespace contained in the
7074 second and subsequent arguments is kept; this can cause strange
7075 effects.  It's generally safest to remove all extraneous whitespace when
7076 providing parameters to @code{call}.
7078 @node Value Function, Eval Function, Call Function, Functions
7079 @comment  node-name,  next,  previous,  up
7080 @section The @code{value} Function
7081 @findex value
7082 @cindex variables, unexpanded value
7084 The @code{value} function provides a way for you to use the value of a
7085 variable @emph{without} having it expanded.  Please note that this
7086 does not undo expansions which have already occurred; for example if
7087 you create a simply expanded variable its value is expanded during the
7088 definition; in that case the @code{value} function will return the
7089 same result as using the variable directly.
7091 The syntax of the @code{value} function is:
7093 @example
7094 $(value @var{variable})
7095 @end example
7097 Note that @var{variable} is the @emph{name} of a variable; not a
7098 @emph{reference} to that variable.  Therefore you would not normally
7099 use a @samp{$} or parentheses when writing it.  (You can, however, use
7100 a variable reference in the name if you want the name not to be a
7101 constant.)
7103 The result of this function is a string containing the value of
7104 @var{variable}, without any expansion occurring.  For example, in this
7105 makefile:
7107 @example
7108 @group
7109 FOO = $PATH
7111 all:
7112         @@echo $(FOO)
7113         @@echo $(value FOO)
7114 @end group
7115 @end example
7117 @noindent
7118 The first output line would be @code{ATH}, since the ``$P'' would be
7119 expanded as a @code{make} variable, while the second output line would
7120 be the current value of your @code{$PATH} environment variable, since
7121 the @code{value} function avoided the expansion.
7123 The @code{value} function is most often used in conjunction with the
7124 @code{eval} function (@pxref{Eval Function}).
7126 @node Eval Function, Origin Function, Value Function, Functions
7127 @comment  node-name,  next,  previous,  up
7128 @section The @code{eval} Function
7129 @findex eval
7130 @cindex evaluating makefile syntax
7131 @cindex makefile syntax, evaluating
7133 The @code{eval} function is very special: it allows you to define new
7134 makefile constructs that are not constant; which are the result of
7135 evaluating other variables and functions.  The argument to the
7136 @code{eval} function is expanded, then the results of that expansion
7137 are parsed as makefile syntax.  The expanded results can define new
7138 @code{make} variables, targets, implicit or explicit rules, etc.
7140 The result of the @code{eval} function is always the empty string;
7141 thus, it can be placed virtually anywhere in a makefile without
7142 causing syntax errors.
7144 It's important to realize that the @code{eval} argument is expanded
7145 @emph{twice}; first by the @code{eval} function, then the results of
7146 that expansion are expanded again when they are parsed as makefile
7147 syntax.  This means you may need to provide extra levels of escaping
7148 for ``$'' characters when using @code{eval}.  The @code{value}
7149 function (@pxref{Value Function}) can sometimes be useful in these
7150 situations, to circumvent unwanted expansions.
7152 Here is an example of how @code{eval} can be used; this example
7153 combines a number of concepts and other functions.  Although it might
7154 seem overly complex to use @code{eval} in this example, rather than
7155 just writing out the rules, consider two things: first, the template
7156 definition (in @code{PROGRAM_template}) could need to be much more
7157 complex than it is here; and second, you might put the complex,
7158 ``generic'' part of this example into another makefile, then include
7159 it in all the individual makefiles.  Now your individual makefiles are
7160 quite straightforward.
7162 @example
7163 @group
7164 PROGRAMS    = server client
7166 server_OBJS = server.o server_priv.o server_access.o
7167 server_LIBS = priv protocol
7169 client_OBJS = client.o client_api.o client_mem.o
7170 client_LIBS = protocol
7172 # Everything after this is generic
7174 .PHONY: all
7175 all: $(PROGRAMS)
7177 define PROGRAM_template
7178  $(1): $$($(1)_OBJS) $$($(1)_LIBS:%=-l%)
7179  ALL_OBJS   += $$($(1)_OBJS)
7180 endef
7182 $(foreach prog,$(PROGRAMS),$(eval $(call PROGRAM_template,$(prog))))
7184 $(PROGRAMS):
7185         $(LINK.o) $^ $(LDLIBS) -o $@@
7187 clean:
7188         rm -f $(ALL_OBJS) $(PROGRAMS)
7189 @end group
7190 @end example
7192 @node Origin Function, Flavor Function, Eval Function, Functions
7193 @section The @code{origin} Function
7194 @findex origin
7195 @cindex variables, origin of
7196 @cindex origin of variable
7198 The @code{origin} function is unlike most other functions in that it does
7199 not operate on the values of variables; it tells you something @emph{about}
7200 a variable.  Specifically, it tells you where it came from.
7202 The syntax of the @code{origin} function is:
7204 @example
7205 $(origin @var{variable})
7206 @end example
7208 Note that @var{variable} is the @emph{name} of a variable to inquire about;
7209 not a @emph{reference} to that variable.  Therefore you would not normally
7210 use a @samp{$} or parentheses when writing it.  (You can, however, use a
7211 variable reference in the name if you want the name not to be a constant.)
7213 The result of this function is a string telling you how the variable
7214 @var{variable} was defined:
7216 @table @samp
7217 @item undefined
7219 if @var{variable} was never defined.
7221 @item default
7223 if @var{variable} has a default definition, as is usual with @code{CC}
7224 and so on.  @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
7225 Note that if you have redefined a default variable, the @code{origin}
7226 function will return the origin of the later definition.
7228 @item environment
7230 if @var{variable} was defined as an environment variable and the
7231 @samp{-e} option is @emph{not} turned on (@pxref{Options Summary, ,Summary of Options}).
7233 @item environment override
7235 if @var{variable} was defined as an environment variable and the
7236 @w{@samp{-e}} option @emph{is} turned on (@pxref{Options Summary,
7237 ,Summary of Options}).@refill
7239 @item file
7241 if @var{variable} was defined in a makefile.
7243 @item command line
7245 if @var{variable} was defined on the command line.
7247 @item override
7249 if @var{variable} was defined with an @code{override} directive in a
7250 makefile (@pxref{Override Directive, ,The @code{override} Directive}).
7252 @item automatic
7254 if @var{variable} is an automatic variable defined for the execution
7255 of the recipe for each rule (@pxref{Automatic Variables}).
7256 @end table
7258 This information is primarily useful (other than for your curiosity) to
7259 determine if you want to believe the value of a variable.  For example,
7260 suppose you have a makefile @file{foo} that includes another makefile
7261 @file{bar}.  You want a variable @code{bletch} to be defined in @file{bar}
7262 if you run the command @w{@samp{make -f bar}}, even if the environment contains
7263 a definition of @code{bletch}.  However, if @file{foo} defined
7264 @code{bletch} before including @file{bar}, you do not want to override that
7265 definition.  This could be done by using an @code{override} directive in
7266 @file{foo}, giving that definition precedence over the later definition in
7267 @file{bar}; unfortunately, the @code{override} directive would also
7268 override any command line definitions.  So, @file{bar} could
7269 include:@refill
7271 @example
7272 @group
7273 ifdef bletch
7274 ifeq "$(origin bletch)" "environment"
7275 bletch = barf, gag, etc.
7276 endif
7277 endif
7278 @end group
7279 @end example
7281 @noindent
7282 If @code{bletch} has been defined from the environment, this will redefine
7285 If you want to override a previous definition of @code{bletch} if it came
7286 from the environment, even under @samp{-e}, you could instead write:
7288 @example
7289 @group
7290 ifneq "$(findstring environment,$(origin bletch))" ""
7291 bletch = barf, gag, etc.
7292 endif
7293 @end group
7294 @end example
7296 Here the redefinition takes place if @samp{$(origin bletch)} returns either
7297 @samp{environment} or @samp{environment override}.
7298 @xref{Text Functions, , Functions for String Substitution and Analysis}.
7300 @node Flavor Function, Shell Function, Origin Function, Functions
7301 @section The @code{flavor} Function
7302 @findex flavor
7303 @cindex variables, flavor of
7304 @cindex flavor of variable
7306 The @code{flavor} function is unlike most other functions (and like
7307 @code{origin} function) in that it does not operate on the values of
7308 variables; it tells you something @emph{about} a variable.
7309 Specifically, it tells you the flavor of a variable (@pxref{Flavors,
7310 ,The Two Flavors of Variables}).
7312 The syntax of the @code{flavor} function is:
7314 @example
7315 $(flavor @var{variable})
7316 @end example
7318 Note that @var{variable} is the @emph{name} of a variable to inquire about;
7319 not a @emph{reference} to that variable.  Therefore you would not normally
7320 use a @samp{$} or parentheses when writing it.  (You can, however, use a
7321 variable reference in the name if you want the name not to be a constant.)
7323 The result of this function is a string that identifies the flavor of the
7324 variable @var{variable}:
7326 @table @samp
7327 @item undefined
7329 if @var{variable} was never defined.
7331 @item recursive
7333 if @var{variable} is a recursively expanded variable.
7335 @item simple
7337 if @var{variable} is a simply expanded variable.
7339 @end table
7342 @node Shell Function, Make Control Functions, Flavor Function, Functions
7343 @section The @code{shell} Function
7344 @findex shell
7345 @cindex command expansion
7346 @cindex backquotes
7347 @cindex shell command, function for
7349 The @code{shell} function is unlike any other function other than the
7350 @code{wildcard} function
7351 (@pxref{Wildcard Function, ,The Function @code{wildcard}}) in that it
7352 communicates with the world outside of @code{make}.
7354 The @code{shell} function performs the same function that backquotes
7355 (@samp{`}) perform in most shells: it does @dfn{command expansion}.
7356 This means that it takes as an argument a shell command and evaluates
7357 to the output of the command.  The only processing @code{make} does on
7358 the result is to convert each newline (or carriage-return / newline
7359 pair) to a single space.  If there is a trailing (carriage-return
7360 and) newline it will simply be removed.@refill
7362 The commands run by calls to the @code{shell} function are run when the
7363 function calls are expanded (@pxref{Reading Makefiles, , How
7364 @code{make} Reads a Makefile}).  Because this function involves
7365 spawning a new shell, you should carefully consider the performance
7366 implications of using the @code{shell} function within recursively
7367 expanded variables vs.@: simply expanded variables (@pxref{Flavors, ,The
7368 Two Flavors of Variables}).
7370 Here are some examples of the use of the @code{shell} function:
7372 @example
7373 contents := $(shell cat foo)
7374 @end example
7376 @noindent
7377 sets @code{contents} to the contents of the file @file{foo}, with a space
7378 (rather than a newline) separating each line.
7380 @example
7381 files := $(shell echo *.c)
7382 @end example
7384 @noindent
7385 sets @code{files} to the expansion of @samp{*.c}.  Unless @code{make} is
7386 using a very strange shell, this has the same result as
7387 @w{@samp{$(wildcard *.c)}} (as long as at least one @samp{.c} file
7388 exists).@refill
7390 @node Make Control Functions,  , Shell Function, Functions
7391 @section Functions That Control Make
7392 @cindex functions, for controlling make
7393 @cindex controlling make
7395 These functions control the way make runs.  Generally, they are used to
7396 provide information to the user of the makefile or to cause make to stop
7397 if some sort of environmental error is detected.
7399 @table @code
7400 @item $(error @var{text}@dots{})
7401 @findex error
7402 @cindex error, stopping on
7403 @cindex stopping make
7404 Generates a fatal error where the message is @var{text}.  Note that
7405 the error is generated whenever this function is evaluated.  So, if
7406 you put it inside a recipe or on the right side of a recursive
7407 variable assignment, it won't be evaluated until later.  The
7408 @var{text} will be expanded before the error is generated.
7410 For example,
7412 @example
7413 ifdef ERROR1
7414 $(error error is $(ERROR1))
7415 endif
7416 @end example
7418 @noindent
7419 will generate a fatal error during the read of the makefile if the
7420 @code{make} variable @code{ERROR1} is defined.  Or,
7422 @example
7423 ERR = $(error found an error!)
7425 .PHONY: err
7426 err: ; $(ERR)
7427 @end example
7429 @noindent
7430 will generate a fatal error while @code{make} is running, if the
7431 @code{err} target is invoked.
7433 @item $(warning @var{text}@dots{})
7434 @findex warning
7435 @cindex warnings, printing
7436 @cindex printing user warnings
7437 This function works similarly to the @code{error} function, above,
7438 except that @code{make} doesn't exit.  Instead, @var{text} is expanded
7439 and the resulting message is displayed, but processing of the makefile
7440 continues.
7442 The result of the expansion of this function is the empty string.
7444 @item $(info @var{text}@dots{})
7445 @findex info
7446 @cindex printing messages
7447 This function does nothing more than print its (expanded) argument(s)
7448 to standard output.  No makefile name or line number is added.  The
7449 result of the expansion of this function is the empty string.
7450 @end table
7452 @node Running, Implicit Rules, Functions, Top
7453 @chapter How to Run @code{make}
7455 A makefile that says how to recompile a program can be used in more
7456 than one way.  The simplest use is to recompile every file that is out
7457 of date.  Usually, makefiles are written so that if you run
7458 @code{make} with no arguments, it does just that.
7460 But you might want to update only some of the files; you might want to use
7461 a different compiler or different compiler options; you might want just to
7462 find out which files are out of date without changing them.
7464 By giving arguments when you run @code{make}, you can do any of these
7465 things and many others.
7467 @cindex exit status of make
7468 The exit status of @code{make} is always one of three values:
7469 @table @code
7470 @item 0
7471 The exit status is zero if @code{make} is successful.
7472 @item 2
7473 The exit status is two if @code{make} encounters any errors.
7474 It will print messages describing the particular errors.
7475 @item 1
7476 The exit status is one if you use the @samp{-q} flag and @code{make}
7477 determines that some target is not already up to date.
7478 @xref{Instead of Execution, ,Instead of Executing the Recipes}.
7479 @end table
7481 @menu
7482 * Makefile Arguments::          How to specify which makefile to use.
7483 * Goals::                       How to use goal arguments to specify which
7484                                   parts of the makefile to use.
7485 * Instead of Execution::        How to use mode flags to specify what
7486                                   kind of thing to do with the recipes
7487                                   in the makefile other than simply
7488                                   execute them.
7489 * Avoiding Compilation::        How to avoid recompiling certain files.
7490 * Overriding::                  How to override a variable to specify
7491                                   an alternate compiler and other things.
7492 * Testing::                     How to proceed past some errors, to
7493                                   test compilation.
7494 * Options Summary::             Summary of Options
7495 @end menu
7497 @node Makefile Arguments, Goals, Running, Running
7498 @section Arguments to Specify the Makefile
7499 @cindex @code{--file}
7500 @cindex @code{--makefile}
7501 @cindex @code{-f}
7503 The way to specify the name of the makefile is with the @samp{-f} or
7504 @samp{--file} option (@samp{--makefile} also works).  For example,
7505 @samp{-f altmake} says to use the file @file{altmake} as the makefile.
7507 If you use the @samp{-f} flag several times and follow each @samp{-f}
7508 with an argument, all the specified files are used jointly as
7509 makefiles.
7511 If you do not use the @samp{-f} or @samp{--file} flag, the default is
7512 to try @file{GNUmakefile}, @file{makefile}, and @file{Makefile}, in
7513 that order, and use the first of these three which exists or can be made
7514 (@pxref{Makefiles, ,Writing Makefiles}).@refill
7516 @node Goals, Instead of Execution, Makefile Arguments, Running
7517 @section Arguments to Specify the Goals
7518 @cindex goal, how to specify
7520 The @dfn{goals} are the targets that @code{make} should strive ultimately
7521 to update.  Other targets are updated as well if they appear as
7522 prerequisites of goals, or prerequisites of prerequisites of goals, etc.
7524 By default, the goal is the first target in the makefile (not counting
7525 targets that start with a period).  Therefore, makefiles are usually
7526 written so that the first target is for compiling the entire program or
7527 programs they describe.  If the first rule in the makefile has several
7528 targets, only the first target in the rule becomes the default goal, not
7529 the whole list.  You can manage the selection of the default goal from
7530 within your makefile using the @code{.DEFAULT_GOAL} variable
7531 (@pxref{Special Variables, , Other Special Variables}).
7533 You can also specify a different goal or goals with command line
7534 arguments to @code{make}.  Use the name of the goal as an argument.
7535 If you specify several goals, @code{make} processes each of them in
7536 turn, in the order you name them.
7538 Any target in the makefile may be specified as a goal (unless it
7539 starts with @samp{-} or contains an @samp{=}, in which case it will be
7540 parsed as a switch or variable definition, respectively).  Even
7541 targets not in the makefile may be specified, if @code{make} can find
7542 implicit rules that say how to make them.
7544 @vindex MAKECMDGOALS
7545 @code{Make} will set the special variable @code{MAKECMDGOALS} to the
7546 list of goals you specified on the command line.  If no goals were given
7547 on the command line, this variable is empty.  Note that this variable
7548 should be used only in special circumstances.
7550 An example of appropriate use is to avoid including @file{.d} files
7551 during @code{clean} rules (@pxref{Automatic Prerequisites}), so
7552 @code{make} won't create them only to immediately remove them
7553 again:@refill
7555 @example
7556 @group
7557 sources = foo.c bar.c
7559 ifneq ($(MAKECMDGOALS),clean)
7560 include $(sources:.c=.d)
7561 endif
7562 @end group
7563 @end example
7565 One use of specifying a goal is if you want to compile only a part of
7566 the program, or only one of several programs.  Specify as a goal each
7567 file that you wish to remake.  For example, consider a directory containing
7568 several programs, with a makefile that starts like this:
7570 @example
7571 .PHONY: all
7572 all: size nm ld ar as
7573 @end example
7575 If you are working on the program @code{size}, you might want to say
7576 @w{@samp{make size}} so that only the files of that program are recompiled.
7578 Another use of specifying a goal is to make files that are not normally
7579 made.  For example, there may be a file of debugging output, or a
7580 version of the program that is compiled specially for testing, which has
7581 a rule in the makefile but is not a prerequisite of the default goal.
7583 Another use of specifying a goal is to run the recipe associated with
7584 a phony target (@pxref{Phony Targets}) or empty target (@pxref{Empty
7585 Targets, ,Empty Target Files to Record Events}).  Many makefiles contain
7586 a phony target named @file{clean} which deletes everything except source
7587 files.  Naturally, this is done only if you request it explicitly with
7588 @w{@samp{make clean}}.  Following is a list of typical phony and empty
7589 target names.  @xref{Standard Targets}, for a detailed list of all the
7590 standard target names which GNU software packages use.
7592 @table @file
7593 @item all
7594 @cindex @code{all} @r{(standard target)}
7595 Make all the top-level targets the makefile knows about.
7597 @item clean
7598 @cindex @code{clean} @r{(standard target)}
7599 Delete all files that are normally created by running @code{make}.
7601 @item mostlyclean
7602 @cindex @code{mostlyclean} @r{(standard target)}
7603 Like @samp{clean}, but may refrain from deleting a few files that people
7604 normally don't want to recompile.  For example, the @samp{mostlyclean}
7605 target for GCC does not delete @file{libgcc.a}, because recompiling it
7606 is rarely necessary and takes a lot of time.
7608 @item distclean
7609 @cindex @code{distclean} @r{(standard target)}
7610 @itemx realclean
7611 @cindex @code{realclean} @r{(standard target)}
7612 @itemx clobber
7613 @cindex @code{clobber} @r{(standard target)}
7614 Any of these targets might be defined to delete @emph{more} files than
7615 @samp{clean} does.  For example, this would delete configuration files
7616 or links that you would normally create as preparation for compilation,
7617 even if the makefile itself cannot create these files.
7619 @item install
7620 @cindex @code{install} @r{(standard target)}
7621 Copy the executable file into a directory that users typically search
7622 for commands; copy any auxiliary files that the executable uses into
7623 the directories where it will look for them.
7625 @item print
7626 @cindex @code{print} @r{(standard target)}
7627 Print listings of the source files that have changed.
7629 @item tar
7630 @cindex @code{tar} @r{(standard target)}
7631 Create a tar file of the source files.
7633 @item shar
7634 @cindex @code{shar} @r{(standard target)}
7635 Create a shell archive (shar file) of the source files.
7637 @item dist
7638 @cindex @code{dist} @r{(standard target)}
7639 Create a distribution file of the source files.  This might
7640 be a tar file, or a shar file, or a compressed version of one of the
7641 above, or even more than one of the above.
7643 @item TAGS
7644 @cindex @code{TAGS} @r{(standard target)}
7645 Update a tags table for this program.
7647 @item check
7648 @cindex @code{check} @r{(standard target)}
7649 @itemx test
7650 @cindex @code{test} @r{(standard target)}
7651 Perform self tests on the program this makefile builds.
7652 @end table
7654 @node Instead of Execution, Avoiding Compilation, Goals, Running
7655 @section Instead of Executing the Recipes
7656 @cindex execution, instead of
7657 @cindex recipes, instead of executing
7659 The makefile tells @code{make} how to tell whether a target is up to date,
7660 and how to update each target.  But updating the targets is not always
7661 what you want.  Certain options specify other activities for @code{make}.
7663 @comment Extra blank lines make it print better.
7664 @table @samp
7665 @item -n
7666 @itemx --just-print
7667 @itemx --dry-run
7668 @itemx --recon
7669 @cindex @code{--just-print}
7670 @cindex @code{--dry-run}
7671 @cindex @code{--recon}
7672 @cindex @code{-n}
7674 ``No-op''.  The activity is to print what recipe would be used to make
7675 the targets up to date, but not actually execute it.
7677 @item -t
7678 @itemx --touch
7679 @cindex @code{--touch}
7680 @cindex touching files
7681 @cindex target, touching
7682 @cindex @code{-t}
7684 ``Touch''.  The activity is to mark the targets as up to date without
7685 actually changing them.  In other words, @code{make} pretends to compile
7686 the targets but does not really change their contents.
7688 @item -q
7689 @itemx --question
7690 @cindex @code{--question}
7691 @cindex @code{-q}
7692 @cindex question mode
7694 ``Question''.  The activity is to find out silently whether the targets
7695 are up to date already; but execute no recipe in either case.  In other
7696 words, neither compilation nor output will occur.
7698 @item -W @var{file}
7699 @itemx --what-if=@var{file}
7700 @itemx --assume-new=@var{file}
7701 @itemx --new-file=@var{file}
7702 @cindex @code{--what-if}
7703 @cindex @code{-W}
7704 @cindex @code{--assume-new}
7705 @cindex @code{--new-file}
7706 @cindex what if
7707 @cindex files, assuming new
7709 ``What if''.  Each @samp{-W} flag is followed by a file name.  The given
7710 files' modification times are recorded by @code{make} as being the present
7711 time, although the actual modification times remain the same.
7712 You can use the @samp{-W} flag in conjunction with the @samp{-n} flag
7713 to see what would happen if you were to modify specific files.@refill
7714 @end table
7716 With the @samp{-n} flag, @code{make} prints the recipe that it would
7717 normally execute but does not execute it.
7719 With the @samp{-t} flag, @code{make} ignores the recipes in the rules
7720 and uses (in effect) the command @code{touch} for each target that needs to
7721 be remade.  The @code{touch} command is also printed, unless @samp{-s} or
7722 @code{.SILENT} is used.  For speed, @code{make} does not actually invoke
7723 the program @code{touch}.  It does the work directly.
7725 With the @samp{-q} flag, @code{make} prints nothing and executes no
7726 recipes, but the exit status code it returns is zero if and only if the
7727 targets to be considered are already up to date.  If the exit status is
7728 one, then some updating needs to be done.  If @code{make} encounters an
7729 error, the exit status is two, so you can distinguish an error from a
7730 target that is not up to date.
7732 It is an error to use more than one of these three flags in the same
7733 invocation of @code{make}.
7735 @cindex +, and recipe execution
7736 The @samp{-n}, @samp{-t}, and @samp{-q} options do not affect recipe
7737 lines that begin with @samp{+} characters or contain the strings
7738 @samp{$(MAKE)} or @samp{$@{MAKE@}}.  Note that only the line containing
7739 the @samp{+} character or the strings @samp{$(MAKE)} or @samp{$@{MAKE@}}
7740 is run regardless of these options.  Other lines in the same rule are
7741 not run unless they too begin with @samp{+} or contain @samp{$(MAKE)} or
7742 @samp{$@{MAKE@}} (@xref{MAKE Variable, ,How the @code{MAKE} Variable Works}.)
7744 The @samp{-W} flag provides two features:
7746 @itemize @bullet
7747 @item
7748 If you also use the @samp{-n} or @samp{-q} flag, you can see what
7749 @code{make} would do if you were to modify some files.
7751 @item
7752 Without the @samp{-n} or @samp{-q} flag, when @code{make} is actually
7753 executing recipes, the @samp{-W} flag can direct @code{make} to act as
7754 if some files had been modified, without actually running the recipes
7755 for those files.@refill
7756 @end itemize
7758 Note that the options @samp{-p} and @samp{-v} allow you to obtain other
7759 information about @code{make} or about the makefiles in use
7760 (@pxref{Options Summary, ,Summary of Options}).@refill
7762 @node Avoiding Compilation, Overriding, Instead of Execution, Running
7763 @section Avoiding Recompilation of Some Files
7764 @cindex @code{-o}
7765 @cindex @code{--old-file}
7766 @cindex @code{--assume-old}
7767 @cindex files, assuming old
7768 @cindex files, avoiding recompilation of
7769 @cindex recompilation, avoiding
7771 Sometimes you may have changed a source file but you do not want to
7772 recompile all the files that depend on it.  For example, suppose you add
7773 a macro or a declaration to a header file that many other files depend
7774 on.  Being conservative, @code{make} assumes that any change in the
7775 header file requires recompilation of all dependent files, but you know
7776 that they do not need to be recompiled and you would rather not waste
7777 the time waiting for them to compile.
7779 If you anticipate the problem before changing the header file, you can
7780 use the @samp{-t} flag.  This flag tells @code{make} not to run the
7781 recipes in the rules, but rather to mark the target up to date by
7782 changing its last-modification date.  You would follow this procedure:
7784 @enumerate
7785 @item
7786 Use the command @samp{make} to recompile the source files that really
7787 need recompilation, ensuring that the object files are up-to-date
7788 before you begin.
7790 @item
7791 Make the changes in the header files.
7793 @item
7794 Use the command @samp{make -t} to mark all the object files as
7795 up to date.  The next time you run @code{make}, the changes in the
7796 header files will not cause any recompilation.
7797 @end enumerate
7799 If you have already changed the header file at a time when some files
7800 do need recompilation, it is too late to do this.  Instead, you can
7801 use the @w{@samp{-o @var{file}}} flag, which marks a specified file as
7802 ``old'' (@pxref{Options Summary, ,Summary of Options}).  This means
7803 that the file itself will not be remade, and nothing else will be
7804 remade on its account.  Follow this procedure:
7806 @enumerate
7807 @item
7808 Recompile the source files that need compilation for reasons independent
7809 of the particular header file, with @samp{make -o @var{headerfile}}.
7810 If several header files are involved, use a separate @samp{-o} option
7811 for each header file.
7813 @item
7814 Touch all the object files with @samp{make -t}.
7815 @end enumerate
7817 @node Overriding, Testing, Avoiding Compilation, Running
7818 @section Overriding Variables
7819 @cindex overriding variables with arguments
7820 @cindex variables, overriding with arguments
7821 @cindex command line variables
7822 @cindex variables, command line
7824 An argument that contains @samp{=} specifies the value of a variable:
7825 @samp{@var{v}=@var{x}} sets the value of the variable @var{v} to @var{x}.
7826 If you specify a value in this way, all ordinary assignments of the same
7827 variable in the makefile are ignored; we say they have been
7828 @dfn{overridden} by the command line argument.
7830 The most common way to use this facility is to pass extra flags to
7831 compilers.  For example, in a properly written makefile, the variable
7832 @code{CFLAGS} is included in each recipe that runs the C compiler, so a
7833 file @file{foo.c} would be compiled something like this:
7835 @example
7836 cc -c $(CFLAGS) foo.c
7837 @end example
7839 Thus, whatever value you set for @code{CFLAGS} affects each compilation
7840 that occurs.  The makefile probably specifies the usual value for
7841 @code{CFLAGS}, like this:
7843 @example
7844 CFLAGS=-g
7845 @end example
7847 Each time you run @code{make}, you can override this value if you
7848 wish.  For example, if you say @samp{make CFLAGS='-g -O'}, each C
7849 compilation will be done with @samp{cc -c -g -O}.  (This also
7850 illustrates how you can use quoting in the shell to enclose spaces and
7851 other special characters in the value of a variable when you override
7852 it.)
7854 The variable @code{CFLAGS} is only one of many standard variables that
7855 exist just so that you can change them this way.  @xref{Implicit
7856 Variables, , Variables Used by Implicit Rules}, for a complete list.
7858 You can also program the makefile to look at additional variables of your
7859 own, giving the user the ability to control other aspects of how the
7860 makefile works by changing the variables.
7862 When you override a variable with a command line argument, you can
7863 define either a recursively-expanded variable or a simply-expanded
7864 variable.  The examples shown above make a recursively-expanded
7865 variable; to make a simply-expanded variable, write @samp{:=} instead
7866 of @samp{=}.  But, unless you want to include a variable reference or
7867 function call in the @emph{value} that you specify, it makes no
7868 difference which kind of variable you create.
7870 There is one way that the makefile can change a variable that you have
7871 overridden.  This is to use the @code{override} directive, which is a line
7872 that looks like this: @samp{override @var{variable} = @var{value}}
7873 (@pxref{Override Directive, ,The @code{override} Directive}).
7875 @node Testing, Options Summary, Overriding, Running
7876 @section Testing the Compilation of a Program
7877 @cindex testing compilation
7878 @cindex compilation, testing
7880 Normally, when an error happens in executing a shell command, @code{make}
7881 gives up immediately, returning a nonzero status.  No further recipes are
7882 executed for any target.  The error implies that the goal cannot be
7883 correctly remade, and @code{make} reports this as soon as it knows.
7885 When you are compiling a program that you have just changed, this is not
7886 what you want.  Instead, you would rather that @code{make} try compiling
7887 every file that can be tried, to show you as many compilation errors
7888 as possible.
7890 @cindex @code{-k}
7891 @cindex @code{--keep-going}
7892 On these occasions, you should use the @samp{-k} or
7893 @samp{--keep-going} flag.  This tells @code{make} to continue to
7894 consider the other prerequisites of the pending targets, remaking them
7895 if necessary, before it gives up and returns nonzero status.  For
7896 example, after an error in compiling one object file, @samp{make -k}
7897 will continue compiling other object files even though it already
7898 knows that linking them will be impossible.  In addition to continuing
7899 after failed shell commands, @samp{make -k} will continue as much as
7900 possible after discovering that it does not know how to make a target
7901 or prerequisite file.  This will always cause an error message, but
7902 without @samp{-k}, it is a fatal error (@pxref{Options Summary,
7903 ,Summary of Options}).@refill
7905 The usual behavior of @code{make} assumes that your purpose is to get the
7906 goals up to date; once @code{make} learns that this is impossible, it might
7907 as well report the failure immediately.  The @samp{-k} flag says that the
7908 real purpose is to test as much as possible of the changes made in the
7909 program, perhaps to find several independent problems so that you can
7910 correct them all before the next attempt to compile.  This is why Emacs'
7911 @kbd{M-x compile} command passes the @samp{-k} flag by default.
7913 @node Options Summary,  , Testing, Running
7914 @section Summary of Options
7915 @cindex options
7916 @cindex flags
7917 @cindex switches
7919 Here is a table of all the options @code{make} understands:
7921 @table @samp
7922 @item -b
7923 @cindex @code{-b}
7924 @itemx -m
7925 @cindex @code{-m}
7926 These options are ignored for compatibility with other versions of @code{make}.
7928 @item -B
7929 @cindex @code{-B}
7930 @itemx --always-make
7931 @cindex @code{--always-make}
7932 Consider all targets out-of-date.  GNU @code{make} proceeds to
7933 consider targets and their prerequisites using the normal algorithms;
7934 however, all targets so considered are always remade regardless of the
7935 status of their prerequisites.  To avoid infinite recursion, if
7936 @code{MAKE_RESTARTS} (@pxref{Special Variables, , Other Special
7937 Variables}) is set to a number greater than 0 this option is disabled
7938 when considering whether to remake makefiles (@pxref{Remaking
7939 Makefiles, , How Makefiles Are Remade}).
7941 @item -C @var{dir}
7942 @cindex @code{-C}
7943 @itemx --directory=@var{dir}
7944 @cindex @code{--directory}
7945 Change to directory @var{dir} before reading the makefiles.  If multiple
7946 @samp{-C} options are specified, each is interpreted relative to the
7947 previous one: @samp{-C / -C etc} is equivalent to @samp{-C /etc}.
7948 This is typically used with recursive invocations of @code{make}
7949 (@pxref{Recursion, ,Recursive Use of @code{make}}).
7951 @item -d
7952 @cindex @code{-d}
7953 @c Extra blank line here makes the table look better.
7955 Print debugging information in addition to normal processing.  The
7956 debugging information says which files are being considered for
7957 remaking, which file-times are being compared and with what results,
7958 which files actually need to be remade, which implicit rules are
7959 considered and which are applied---everything interesting about how
7960 @code{make} decides what to do.  The @code{-d} option is equivalent to
7961 @samp{--debug=a} (see below).
7963 @item --debug[=@var{options}]
7964 @cindex @code{--debug}
7965 @c Extra blank line here makes the table look better.
7967 Print debugging information in addition to normal processing.  Various
7968 levels and types of output can be chosen.  With no arguments, print the
7969 ``basic'' level of debugging.  Possible arguments are below; only the
7970 first character is considered, and values must be comma- or
7971 space-separated.
7973 @table @code
7974 @item a (@i{all})
7975 All types of debugging output are enabled.  This is equivalent to using
7976 @samp{-d}.
7978 @item b (@i{basic})
7979 Basic debugging prints each target that was found to be out-of-date, and
7980 whether the build was successful or not.
7982 @item v (@i{verbose})
7983 A level above @samp{basic}; includes messages about which makefiles were
7984 parsed, prerequisites that did not need to be rebuilt, etc.  This option
7985 also enables @samp{basic} messages.
7987 @item i (@i{implicit})
7988 Prints messages describing the implicit rule searches for each target.
7989 This option also enables @samp{basic} messages.
7991 @item j (@i{jobs})
7992 Prints messages giving details on the invocation of specific subcommands.
7994 @item m (@i{makefile})
7995 By default, the above messages are not enabled while trying to remake
7996 the makefiles.  This option enables messages while rebuilding makefiles,
7997 too.  Note that the @samp{all} option does enable this option.  This
7998 option also enables @samp{basic} messages.
7999 @end table
8001 @item -e
8002 @cindex @code{-e}
8003 @itemx --environment-overrides
8004 @cindex @code{--environment-overrides}
8005 Give variables taken from the environment precedence
8006 over variables from makefiles.
8007 @xref{Environment, ,Variables from the Environment}.
8009 @item -f @var{file}
8010 @cindex @code{-f}
8011 @itemx --file=@var{file}
8012 @cindex @code{--file}
8013 @itemx --makefile=@var{file}
8014 @cindex @code{--makefile}
8015 Read the file named @var{file} as a makefile.
8016 @xref{Makefiles, ,Writing Makefiles}.
8018 @item -h
8019 @cindex @code{-h}
8020 @itemx --help
8021 @cindex @code{--help}
8022 @c Extra blank line here makes the table look better.
8024 Remind you of the options that @code{make} understands and then exit.
8026 @item -i
8027 @cindex @code{-i}
8028 @itemx --ignore-errors
8029 @cindex @code{--ignore-errors}
8030 Ignore all errors in recipes executed to remake files.
8031 @xref{Errors, ,Errors in Recipes}.
8033 @item -I @var{dir}
8034 @cindex @code{-I}
8035 @itemx --include-dir=@var{dir}
8036 @cindex @code{--include-dir}
8037 Specifies a directory @var{dir} to search for included makefiles.
8038 @xref{Include, ,Including Other Makefiles}.  If several @samp{-I}
8039 options are used to specify several directories, the directories are
8040 searched in the order specified.
8042 @item -j [@var{jobs}]
8043 @cindex @code{-j}
8044 @itemx --jobs[=@var{jobs}]
8045 @cindex @code{--jobs}
8046 Specifies the number of recipes (jobs) to run simultaneously.  With no
8047 argument, @code{make} runs as many recipes simultaneously as possible.
8048 If there is more than one @samp{-j} option, the last one is effective.
8049 @xref{Parallel, ,Parallel Execution}, for more information on how
8050 recipes are run.  Note that this option is ignored on MS-DOS.
8052 @item -k
8053 @cindex @code{-k}
8054 @itemx --keep-going
8055 @cindex @code{--keep-going}
8056 Continue as much as possible after an error.  While the target that
8057 failed, and those that depend on it, cannot be remade, the other
8058 prerequisites of these targets can be processed all the same.
8059 @xref{Testing, ,Testing the Compilation of a Program}.
8061 @item -l [@var{load}]
8062 @cindex @code{-l}
8063 @itemx --load-average[=@var{load}]
8064 @cindex @code{--load-average}
8065 @itemx --max-load[=@var{load}]
8066 @cindex @code{--max-load}
8067 Specifies that no new recipes should be started if there are other
8068 recipes running and the load average is at least @var{load} (a
8069 floating-point number).  With no argument, removes a previous load
8070 limit.  @xref{Parallel, ,Parallel Execution}.
8072 @item -L
8073 @cindex @code{-L}
8074 @itemx --check-symlink-times
8075 @cindex @code{--check-symlink-times}
8076 On systems that support symbolic links, this option causes @code{make}
8077 to consider the timestamps on any symbolic links in addition to the
8078 timestamp on the file referenced by those links.  When this option is
8079 provided, the most recent timestamp among the file and the symbolic
8080 links is taken as the modification time for this target file.
8082 @item -n
8083 @cindex @code{-n}
8084 @itemx --just-print
8085 @cindex @code{--just-print}
8086 @itemx --dry-run
8087 @cindex @code{--dry-run}
8088 @itemx --recon
8089 @cindex @code{--recon}
8090 @c Extra blank line here makes the table look better.
8092 Print the recipe that would be executed, but do not execute it.
8093 @xref{Instead of Execution, ,Instead of Executing the Recipes}.
8095 @item -o @var{file}
8096 @cindex @code{-o}
8097 @itemx --old-file=@var{file}
8098 @cindex @code{--old-file}
8099 @itemx --assume-old=@var{file}
8100 @cindex @code{--assume-old}
8101 Do not remake the file @var{file} even if it is older than its
8102 prerequisites, and do not remake anything on account of changes in
8103 @var{file}.  Essentially the file is treated as very old and its rules
8104 are ignored.  @xref{Avoiding Compilation, ,Avoiding Recompilation of
8105 Some Files}.@refill
8107 @item -p
8108 @cindex @code{-p}
8109 @itemx --print-data-base
8110 @cindex @code{--print-data-base}
8111 @cindex data base of @code{make} rules
8112 @cindex predefined rules and variables, printing
8113 Print the data base (rules and variable values) that results from
8114 reading the makefiles; then execute as usual or as otherwise
8115 specified.  This also prints the version information given by the
8116 @samp{-v} switch (see below).  To print the data base without trying
8117 to remake any files, use @w{@samp{make -qp}}.  To print the data base
8118 of predefined rules and variables, use @w{@samp{make -p -f /dev/null}}.
8119 The data base output contains filename and linenumber information for
8120 recipe and variable definitions, so it can be a useful debugging tool
8121 in complex environments.
8123 @item -q
8124 @cindex @code{-q}
8125 @itemx --question
8126 @cindex @code{--question}
8127 ``Question mode''.  Do not run any recipes, or print anything; just
8128 return an exit status that is zero if the specified targets are already
8129 up to date, one if any remaking is required, or two if an error is
8130 encountered.  @xref{Instead of Execution, ,Instead of Executing the
8131 Recipes}.@refill
8133 @item -r
8134 @cindex @code{-r}
8135 @itemx --no-builtin-rules
8136 @cindex @code{--no-builtin-rules}
8137 Eliminate use of the built-in implicit rules (@pxref{Implicit Rules,
8138 ,Using Implicit Rules}).  You can still define your own by writing
8139 pattern rules (@pxref{Pattern Rules, ,Defining and Redefining Pattern
8140 Rules}).  The @samp{-r} option also clears out the default list of
8141 suffixes for suffix rules (@pxref{Suffix Rules, ,Old-Fashioned Suffix
8142 Rules}).  But you can still define your own suffixes with a rule for
8143 @code{.SUFFIXES}, and then define your own suffix rules.  Note that only
8144 @emph{rules} are affected by the @code{-r} option; default variables
8145 remain in effect (@pxref{Implicit Variables, ,Variables Used by Implicit
8146 Rules}); see the @samp{-R} option below.
8148 @item -R
8149 @cindex @code{-R}
8150 @itemx --no-builtin-variables
8151 @cindex @code{--no-builtin-variables}
8152 Eliminate use of the built-in rule-specific variables (@pxref{Implicit
8153 Variables, ,Variables Used by Implicit Rules}).  You can still define
8154 your own, of course.  The @samp{-R} option also automatically enables
8155 the @samp{-r} option (see above), since it doesn't make sense to have
8156 implicit rules without any definitions for the variables that they use.
8158 @item -s
8159 @cindex @code{-s}
8160 @itemx --silent
8161 @cindex @code{--silent}
8162 @itemx --quiet
8163 @cindex @code{--quiet}
8164 @c Extra blank line here makes the table look better.
8166 Silent operation; do not print the recipes as they are executed.
8167 @xref{Echoing, ,Recipe Echoing}.
8169 @item -S
8170 @cindex @code{-S}
8171 @itemx --no-keep-going
8172 @cindex @code{--no-keep-going}
8173 @itemx --stop
8174 @cindex @code{--stop}
8175 @c Extra blank line here makes the table look better.
8177 Cancel the effect of the @samp{-k} option.  This is never necessary
8178 except in a recursive @code{make} where @samp{-k} might be inherited
8179 from the top-level @code{make} via @code{MAKEFLAGS}
8180 (@pxref{Recursion, ,Recursive Use of @code{make}})
8181 or if you set @samp{-k} in @code{MAKEFLAGS} in your environment.@refill
8183 @item -t
8184 @cindex @code{-t}
8185 @itemx --touch
8186 @cindex @code{--touch}
8187 @c Extra blank line here makes the table look better.
8189 Touch files (mark them up to date without really changing them)
8190 instead of running their recipes.  This is used to pretend that the
8191 recipes were done, in order to fool future invocations of
8192 @code{make}.  @xref{Instead of Execution, ,Instead of Executing the Recipes}.
8194 @item -v
8195 @cindex @code{-v}
8196 @itemx --version
8197 @cindex @code{--version}
8198 Print the version of the @code{make} program plus a copyright, a list
8199 of authors, and a notice that there is no warranty; then exit.
8201 @item -w
8202 @cindex @code{-w}
8203 @itemx --print-directory
8204 @cindex @code{--print-directory}
8205 Print a message containing the working directory both before and after
8206 executing the makefile.  This may be useful for tracking down errors
8207 from complicated nests of recursive @code{make} commands.
8208 @xref{Recursion, ,Recursive Use of @code{make}}.  (In practice, you
8209 rarely need to specify this option since @samp{make} does it for you;
8210 see @ref{-w Option, ,The @samp{--print-directory} Option}.)
8212 @itemx --no-print-directory
8213 @cindex @code{--no-print-directory}
8214 Disable printing of the working directory under @code{-w}.
8215 This option is useful when @code{-w} is turned on automatically,
8216 but you do not want to see the extra messages.
8217 @xref{-w Option, ,The @samp{--print-directory} Option}.
8219 @item -W @var{file}
8220 @cindex @code{-W}
8221 @itemx --what-if=@var{file}
8222 @cindex @code{--what-if}
8223 @itemx --new-file=@var{file}
8224 @cindex @code{--new-file}
8225 @itemx --assume-new=@var{file}
8226 @cindex @code{--assume-new}
8227 Pretend that the target @var{file} has just been modified.  When used
8228 with the @samp{-n} flag, this shows you what would happen if you were
8229 to modify that file.  Without @samp{-n}, it is almost the same as
8230 running a @code{touch} command on the given file before running
8231 @code{make}, except that the modification time is changed only in the
8232 imagination of @code{make}.
8233 @xref{Instead of Execution, ,Instead of Executing the Recipes}.
8235 @item --warn-undefined-variables
8236 @cindex @code{--warn-undefined-variables}
8237 @cindex variables, warning for undefined
8238 @cindex undefined variables, warning message
8239 Issue a warning message whenever @code{make} sees a reference to an
8240 undefined variable.  This can be helpful when you are trying to debug
8241 makefiles which use variables in complex ways.
8242 @end table
8244 @node Implicit Rules, Archives, Running, Top
8245 @chapter Using Implicit Rules
8246 @cindex implicit rule
8247 @cindex rule, implicit
8249 Certain standard ways of remaking target files are used very often.  For
8250 example, one customary way to make an object file is from a C source file
8251 using the C compiler, @code{cc}.
8253 @dfn{Implicit rules} tell @code{make} how to use customary techniques so
8254 that you do not have to specify them in detail when you want to use
8255 them.  For example, there is an implicit rule for C compilation.  File
8256 names determine which implicit rules are run.  For example, C
8257 compilation typically takes a @file{.c} file and makes a @file{.o} file.
8258 So @code{make} applies the implicit rule for C compilation when it sees
8259 this combination of file name endings.@refill
8261 A chain of implicit rules can apply in sequence; for example, @code{make}
8262 will remake a @file{.o} file from a @file{.y} file by way of a @file{.c} file.
8263 @iftex
8264 @xref{Chained Rules, ,Chains of Implicit Rules}.
8265 @end iftex
8267 The built-in implicit rules use several variables in their recipes so
8268 that, by changing the values of the variables, you can change the way the
8269 implicit rule works.  For example, the variable @code{CFLAGS} controls the
8270 flags given to the C compiler by the implicit rule for C compilation.
8271 @iftex
8272 @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
8273 @end iftex
8275 You can define your own implicit rules by writing @dfn{pattern rules}.
8276 @iftex
8277 @xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.
8278 @end iftex
8280 @dfn{Suffix rules} are a more limited way to define implicit rules.
8281 Pattern rules are more general and clearer, but suffix rules are
8282 retained for compatibility.
8283 @iftex
8284 @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
8285 @end iftex
8287 @menu
8288 * Using Implicit::              How to use an existing implicit rule
8289                                   to get the recipes for updating a file.
8290 * Catalogue of Rules::          A list of built-in implicit rules.
8291 * Implicit Variables::          How to change what predefined rules do.
8292 * Chained Rules::               How to use a chain of implicit rules.
8293 * Pattern Rules::               How to define new implicit rules.
8294 * Last Resort::                 How to define recipes for rules which
8295                                   cannot find any.
8296 * Suffix Rules::                The old-fashioned style of implicit rule.
8297 * Implicit Rule Search::        The precise algorithm for applying
8298                                   implicit rules.
8299 @end menu
8301 @node Using Implicit, Catalogue of Rules, Implicit Rules, Implicit Rules
8302 @section Using Implicit Rules
8303 @cindex implicit rule, how to use
8304 @cindex rule, implicit, how to use
8306 To allow @code{make} to find a customary method for updating a target
8307 file, all you have to do is refrain from specifying recipes yourself.
8308 Either write a rule with no recipe, or don't write a rule at all.
8309 Then @code{make} will figure out which implicit rule to use based on
8310 which kind of source file exists or can be made.
8312 For example, suppose the makefile looks like this:
8314 @example
8315 foo : foo.o bar.o
8316         cc -o foo foo.o bar.o $(CFLAGS) $(LDFLAGS)
8317 @end example
8319 @noindent
8320 Because you mention @file{foo.o} but do not give a rule for it, @code{make}
8321 will automatically look for an implicit rule that tells how to update it.
8322 This happens whether or not the file @file{foo.o} currently exists.
8324 If an implicit rule is found, it can supply both a recipe and one or
8325 more prerequisites (the source files).  You would want to write a rule
8326 for @file{foo.o} with no recipe if you need to specify additional
8327 prerequisites, such as header files, that the implicit rule cannot
8328 supply.
8330 Each implicit rule has a target pattern and prerequisite patterns.  There may
8331 be many implicit rules with the same target pattern.  For example, numerous
8332 rules make @samp{.o} files: one, from a @samp{.c} file with the C compiler;
8333 another, from a @samp{.p} file with the Pascal compiler; and so on.  The rule
8334 that actually applies is the one whose prerequisites exist or can be made.
8335 So, if you have a file @file{foo.c}, @code{make} will run the C compiler;
8336 otherwise, if you have a file @file{foo.p}, @code{make} will run the Pascal
8337 compiler; and so on.
8339 Of course, when you write the makefile, you know which implicit rule you
8340 want @code{make} to use, and you know it will choose that one because you
8341 know which possible prerequisite files are supposed to exist.
8342 @xref{Catalogue of Rules, ,Catalogue of Implicit Rules},
8343 for a catalogue of all the predefined implicit rules.
8345 Above, we said an implicit rule applies if the required prerequisites ``exist
8346 or can be made''.  A file ``can be made'' if it is mentioned explicitly in
8347 the makefile as a target or a prerequisite, or if an implicit rule can be
8348 recursively found for how to make it.  When an implicit prerequisite is the
8349 result of another implicit rule, we say that @dfn{chaining} is occurring.
8350 @xref{Chained Rules, ,Chains of Implicit Rules}.
8352 In general, @code{make} searches for an implicit rule for each target, and
8353 for each double-colon rule, that has no recipe.  A file that is mentioned
8354 only as a prerequisite is considered a target whose rule specifies nothing,
8355 so implicit rule search happens for it.  @xref{Implicit Rule Search, ,Implicit Rule Search Algorithm}, for the
8356 details of how the search is done.
8358 Note that explicit prerequisites do not influence implicit rule search.
8359 For example, consider this explicit rule:
8361 @example
8362 foo.o: foo.p
8363 @end example
8365 @noindent
8366 The prerequisite on @file{foo.p} does not necessarily mean that
8367 @code{make} will remake @file{foo.o} according to the implicit rule to
8368 make an object file, a @file{.o} file, from a Pascal source file, a
8369 @file{.p} file.  For example, if @file{foo.c} also exists, the implicit
8370 rule to make an object file from a C source file is used instead,
8371 because it appears before the Pascal rule in the list of predefined
8372 implicit rules (@pxref{Catalogue of Rules, , Catalogue of Implicit
8373 Rules}).
8375 If you do not want an implicit rule to be used for a target that has no
8376 recipe, you can give that target an empty recipe by writing a semicolon
8377 (@pxref{Empty Recipes, ,Defining Empty Recipes}).
8379 @node Catalogue of Rules, Implicit Variables, Using Implicit, Implicit Rules
8380 @section Catalogue of Implicit Rules
8381 @cindex implicit rule, predefined
8382 @cindex rule, implicit, predefined
8384 Here is a catalogue of predefined implicit rules which are always
8385 available unless the makefile explicitly overrides or cancels them.
8386 @xref{Canceling Rules, ,Canceling Implicit Rules}, for information on
8387 canceling or overriding an implicit rule.  The @samp{-r} or
8388 @samp{--no-builtin-rules} option cancels all predefined rules.
8390 This manual only documents the default rules available on POSIX-based
8391 operating systems.  Other operating systems, such as VMS, Windows,
8392 OS/2, etc. may have different sets of default rules.  To see the full
8393 list of default rules and variables available in your version of GNU
8394 @code{make}, run @samp{make -p} in a directory with no makefile.
8396 Not all of these rules will always be defined, even when the @samp{-r}
8397 option is not given.  Many of the predefined implicit rules are
8398 implemented in @code{make} as suffix rules, so which ones will be
8399 defined depends on the @dfn{suffix list} (the list of prerequisites of
8400 the special target @code{.SUFFIXES}).  The default suffix list is:
8401 @code{.out}, @code{.a}, @code{.ln}, @code{.o}, @code{.c}, @code{.cc},
8402 @code{.C}, @code{.cpp}, @code{.p}, @code{.f}, @code{.F}, @code{.m},
8403 @code{.r}, @code{.y}, @code{.l}, @code{.ym}, @code{.lm}, @code{.s},
8404 @code{.S}, @code{.mod}, @code{.sym}, @code{.def}, @code{.h},
8405 @code{.info}, @code{.dvi}, @code{.tex}, @code{.texinfo}, @code{.texi},
8406 @code{.txinfo}, @code{.w}, @code{.ch} @code{.web}, @code{.sh},
8407 @code{.elc}, @code{.el}.  All of the implicit rules described below
8408 whose prerequisites have one of these suffixes are actually suffix
8409 rules.  If you modify the suffix list, the only predefined suffix
8410 rules in effect will be those named by one or two of the suffixes that
8411 are on the list you specify; rules whose suffixes fail to be on the
8412 list are disabled.  @xref{Suffix Rules, ,Old-Fashioned Suffix Rules},
8413 for full details on suffix rules.
8415 @table @asis
8416 @item Compiling C programs
8417 @cindex C, rule to compile
8418 @pindex cc
8419 @pindex gcc
8420 @pindex .o
8421 @pindex .c
8422 @file{@var{n}.o} is made automatically from @file{@var{n}.c} with
8423 a recipe of the form @samp{$(CC) -c $(CPPFLAGS) $(CFLAGS)}.@refill
8425 @item Compiling C++ programs
8426 @cindex C++, rule to compile
8427 @pindex g++
8428 @pindex .cc
8429 @pindex .cpp
8430 @pindex .C
8431 @file{@var{n}.o} is made automatically from @file{@var{n}.cc},
8432 @file{@var{n}.cpp}, or @file{@var{n}.C} with a recipe of the form
8433 @samp{$(CXX) -c $(CPPFLAGS) $(CXXFLAGS)}.  We encourage you to use the
8434 suffix @samp{.cc} for C++ source files instead of @samp{.C}.@refill
8436 @item Compiling Pascal programs
8437 @cindex Pascal, rule to compile
8438 @pindex pc
8439 @pindex .p
8440 @file{@var{n}.o} is made automatically from @file{@var{n}.p}
8441 with the recipe @samp{$(PC) -c $(PFLAGS)}.@refill
8443 @item Compiling Fortran and Ratfor programs
8444 @cindex Fortran, rule to compile
8445 @cindex Ratfor, rule to compile
8446 @pindex f77
8447 @pindex .f
8448 @pindex .r
8449 @pindex .F
8450 @file{@var{n}.o} is made automatically from @file{@var{n}.r},
8451 @file{@var{n}.F} or @file{@var{n}.f} by running the
8452 Fortran compiler.  The precise recipe used is as follows:@refill
8454 @table @samp
8455 @item .f
8456 @samp{$(FC) -c $(FFLAGS)}.
8457 @item .F
8458 @samp{$(FC) -c $(FFLAGS) $(CPPFLAGS)}.
8459 @item .r
8460 @samp{$(FC) -c $(FFLAGS) $(RFLAGS)}.
8461 @end table
8463 @item Preprocessing Fortran and Ratfor programs
8464 @file{@var{n}.f} is made automatically from @file{@var{n}.r} or
8465 @file{@var{n}.F}.  This rule runs just the preprocessor to convert a
8466 Ratfor or preprocessable Fortran program into a strict Fortran
8467 program.  The precise recipe used is as follows:@refill
8469 @table @samp
8470 @item .F
8471 @samp{$(FC) -F $(CPPFLAGS) $(FFLAGS)}.
8472 @item .r
8473 @samp{$(FC) -F $(FFLAGS) $(RFLAGS)}.
8474 @end table
8476 @item Compiling Modula-2 programs
8477 @cindex Modula-2, rule to compile
8478 @pindex m2c
8479 @pindex .sym
8480 @pindex .def
8481 @pindex .mod
8482 @file{@var{n}.sym} is made from @file{@var{n}.def} with a recipe
8483 of the form @samp{$(M2C) $(M2FLAGS) $(DEFFLAGS)}.  @file{@var{n}.o}
8484 is made from @file{@var{n}.mod}; the form is:
8485 @w{@samp{$(M2C) $(M2FLAGS) $(MODFLAGS)}}.@refill
8487 @need 1200
8488 @item Assembling and preprocessing assembler programs
8489 @cindex assembly, rule to compile
8490 @pindex as
8491 @pindex .s
8492 @file{@var{n}.o} is made automatically from @file{@var{n}.s} by
8493 running the assembler, @code{as}.  The precise recipe is
8494 @samp{$(AS) $(ASFLAGS)}.@refill
8496 @pindex .S
8497 @file{@var{n}.s} is made automatically from @file{@var{n}.S} by
8498 running the C preprocessor, @code{cpp}.  The precise recipe is
8499 @w{@samp{$(CPP) $(CPPFLAGS)}}.
8501 @item Linking a single object file
8502 @cindex linking, predefined rule for
8503 @pindex ld
8504 @pindex .o
8505 @file{@var{n}} is made automatically from @file{@var{n}.o} by running
8506 the linker (usually called @code{ld}) via the C compiler.  The precise
8507 recipe used is @w{@samp{$(CC) $(LDFLAGS) @var{n}.o $(LOADLIBES) $(LDLIBS)}}.
8509 This rule does the right thing for a simple program with only one
8510 source file.  It will also do the right thing if there are multiple
8511 object files (presumably coming from various other source files), one
8512 of which has a name matching that of the executable file.  Thus,
8514 @example
8515 x: y.o z.o
8516 @end example
8518 @noindent
8519 when @file{x.c}, @file{y.c} and @file{z.c} all exist will execute:
8521 @example
8522 @group
8523 cc -c x.c -o x.o
8524 cc -c y.c -o y.o
8525 cc -c z.c -o z.o
8526 cc x.o y.o z.o -o x
8527 rm -f x.o
8528 rm -f y.o
8529 rm -f z.o
8530 @end group
8531 @end example
8533 @noindent
8534 In more complicated cases, such as when there is no object file whose
8535 name derives from the executable file name, you must write an explicit
8536 recipe for linking.
8538 Each kind of file automatically made into @samp{.o} object files will
8539 be automatically linked by using the compiler (@samp{$(CC)},
8540 @samp{$(FC)} or @samp{$(PC)}; the C compiler @samp{$(CC)} is used to
8541 assemble @samp{.s} files) without the @samp{-c} option.  This could be
8542 done by using the @samp{.o} object files as intermediates, but it is
8543 faster to do the compiling and linking in one step, so that's how it's
8544 done.@refill
8546 @item Yacc for C programs
8547 @pindex yacc
8548 @cindex Yacc, rule to run
8549 @pindex .y
8550 @file{@var{n}.c} is made automatically from @file{@var{n}.y} by
8551 running Yacc with the recipe @samp{$(YACC) $(YFLAGS)}.
8553 @item Lex for C programs
8554 @pindex lex
8555 @cindex Lex, rule to run
8556 @pindex .l
8557 @file{@var{n}.c} is made automatically from @file{@var{n}.l} by
8558 running Lex.  The actual recipe is @samp{$(LEX) $(LFLAGS)}.
8560 @item Lex for Ratfor programs
8561 @file{@var{n}.r} is made automatically from @file{@var{n}.l} by
8562 running Lex.  The actual recipe is @samp{$(LEX) $(LFLAGS)}.
8564 The convention of using the same suffix @samp{.l} for all Lex files
8565 regardless of whether they produce C code or Ratfor code makes it
8566 impossible for @code{make} to determine automatically which of the two
8567 languages you are using in any particular case.  If @code{make} is
8568 called upon to remake an object file from a @samp{.l} file, it must
8569 guess which compiler to use.  It will guess the C compiler, because
8570 that is more common.  If you are using Ratfor, make sure @code{make}
8571 knows this by mentioning @file{@var{n}.r} in the makefile.  Or, if you
8572 are using Ratfor exclusively, with no C files, remove @samp{.c} from
8573 the list of implicit rule suffixes with:@refill
8575 @example
8576 @group
8577 .SUFFIXES:
8578 .SUFFIXES: .o .r .f .l @dots{}
8579 @end group
8580 @end example
8582 @item Making Lint Libraries from C, Yacc, or Lex programs
8583 @pindex lint
8584 @cindex @code{lint}, rule to run
8585 @pindex .ln
8586 @file{@var{n}.ln} is made from @file{@var{n}.c} by running @code{lint}.
8587 The precise recipe is @w{@samp{$(LINT) $(LINTFLAGS) $(CPPFLAGS) -i}}.
8588 The same recipe is used on the C code produced from
8589 @file{@var{n}.y} or @file{@var{n}.l}.@refill
8591 @item @TeX{} and Web
8592 @cindex @TeX{}, rule to run
8593 @cindex Web, rule to run
8594 @pindex tex
8595 @pindex cweave
8596 @pindex weave
8597 @pindex tangle
8598 @pindex ctangle
8599 @pindex .dvi
8600 @pindex .tex
8601 @pindex .web
8602 @pindex .w
8603 @pindex .ch
8604 @file{@var{n}.dvi} is made from @file{@var{n}.tex} with the recipe
8605 @samp{$(TEX)}.  @file{@var{n}.tex} is made from @file{@var{n}.web} with
8606 @samp{$(WEAVE)}, or from @file{@var{n}.w} (and from @file{@var{n}.ch} if
8607 it exists or can be made) with @samp{$(CWEAVE)}.  @file{@var{n}.p} is
8608 made from @file{@var{n}.web} with @samp{$(TANGLE)} and @file{@var{n}.c}
8609 is made from @file{@var{n}.w} (and from @file{@var{n}.ch} if it exists
8610 or can be made) with @samp{$(CTANGLE)}.@refill
8612 @item Texinfo and Info
8613 @cindex Texinfo, rule to format
8614 @cindex Info, rule to format
8615 @pindex texi2dvi
8616 @pindex makeinfo
8617 @pindex .texinfo
8618 @pindex .info
8619 @pindex .texi
8620 @pindex .txinfo
8621 @file{@var{n}.dvi} is made from @file{@var{n}.texinfo},
8622 @file{@var{n}.texi}, or @file{@var{n}.txinfo}, with the recipe
8623 @w{@samp{$(TEXI2DVI) $(TEXI2DVI_FLAGS)}}.  @file{@var{n}.info} is made from
8624 @file{@var{n}.texinfo}, @file{@var{n}.texi}, or @file{@var{n}.txinfo}, with
8625 the recipe @w{@samp{$(MAKEINFO) $(MAKEINFO_FLAGS)}}.
8627 @item RCS
8628 @cindex RCS, rule to extract from
8629 @pindex co
8630 @pindex ,v @r{(RCS file extension)}
8631 Any file @file{@var{n}} is extracted if necessary from an RCS file
8632 named either @file{@var{n},v} or @file{RCS/@var{n},v}.  The precise
8633 recipe used is @w{@samp{$(CO) $(COFLAGS)}}.  @file{@var{n}} will not be
8634 extracted from RCS if it already exists, even if the RCS file is
8635 newer.  The rules for RCS are terminal
8636 (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}),
8637 so RCS files cannot be generated from another source; they must
8638 actually exist.@refill
8640 @item SCCS
8641 @cindex SCCS, rule to extract from
8642 @pindex get
8643 @pindex s. @r{(SCCS file prefix)}
8644 Any file @file{@var{n}} is extracted if necessary from an SCCS file
8645 named either @file{s.@var{n}} or @file{SCCS/s.@var{n}}.  The precise
8646 recipe used is @w{@samp{$(GET) $(GFLAGS)}}.  The rules for SCCS are
8647 terminal (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}),
8648 so SCCS files cannot be generated from another source; they must
8649 actually exist.@refill
8651 @pindex .sh
8652 For the benefit of SCCS, a file @file{@var{n}} is copied from
8653 @file{@var{n}.sh} and made executable (by everyone).  This is for
8654 shell scripts that are checked into SCCS.  Since RCS preserves the
8655 execution permission of a file, you do not need to use this feature
8656 with RCS.@refill
8658 We recommend that you avoid using of SCCS.  RCS is widely held to be
8659 superior, and is also free.  By choosing free software in place of
8660 comparable (or inferior) proprietary software, you support the free
8661 software movement.
8662 @end table
8664 Usually, you want to change only the variables listed in the table
8665 above, which are documented in the following section.
8667 However, the recipes in built-in implicit rules actually use
8668 variables such as @code{COMPILE.c}, @code{LINK.p}, and
8669 @code{PREPROCESS.S}, whose values contain the recipes listed above.
8671 @code{make} follows the convention that the rule to compile a
8672 @file{.@var{x}} source file uses the variable @code{COMPILE.@var{x}}.
8673 Similarly, the rule to produce an executable from a @file{.@var{x}}
8674 file uses @code{LINK.@var{x}}; and the rule to preprocess a
8675 @file{.@var{x}} file uses @code{PREPROCESS.@var{x}}.
8677 @vindex OUTPUT_OPTION
8678 Every rule that produces an object file uses the variable
8679 @code{OUTPUT_OPTION}.  @code{make} defines this variable either to
8680 contain @samp{-o $@@}, or to be empty, depending on a compile-time
8681 option.  You need the @samp{-o} option to ensure that the output goes
8682 into the right file when the source file is in a different directory,
8683 as when using @code{VPATH} (@pxref{Directory Search}).  However,
8684 compilers on some systems do not accept a @samp{-o} switch for object
8685 files.  If you use such a system, and use @code{VPATH}, some
8686 compilations will put their output in the wrong place.
8687 A possible workaround for this problem is to give @code{OUTPUT_OPTION}
8688 the value @w{@samp{; mv $*.o $@@}}.
8690 @node Implicit Variables, Chained Rules, Catalogue of Rules, Implicit Rules
8691 @section Variables Used by Implicit Rules
8692 @cindex flags for compilers
8694 The recipes in built-in implicit rules make liberal use of certain
8695 predefined variables.  You can alter the values of these variables in
8696 the makefile, with arguments to @code{make}, or in the environment to
8697 alter how the implicit rules work without redefining the rules
8698 themselves.  You can cancel all variables used by implicit rules with
8699 the @samp{-R} or @samp{--no-builtin-variables} option.
8701 For example, the recipe used to compile a C source file actually says
8702 @samp{$(CC) -c $(CFLAGS) $(CPPFLAGS)}.  The default values of the variables
8703 used are @samp{cc} and nothing, resulting in the command @samp{cc -c}.  By
8704 redefining @samp{CC} to @samp{ncc}, you could cause @samp{ncc} to be
8705 used for all C compilations performed by the implicit rule.  By redefining
8706 @samp{CFLAGS} to be @samp{-g}, you could pass the @samp{-g} option to
8707 each compilation.  @emph{All} implicit rules that do C compilation use
8708 @samp{$(CC)} to get the program name for the compiler and @emph{all}
8709 include @samp{$(CFLAGS)} among the arguments given to the compiler.@refill
8711 The variables used in implicit rules fall into two classes: those that are
8712 names of programs (like @code{CC}) and those that contain arguments for the
8713 programs (like @code{CFLAGS}).  (The ``name of a program'' may also contain
8714 some command arguments, but it must start with an actual executable program
8715 name.)  If a variable value contains more than one argument, separate them
8716 with spaces.
8718 The following tables describe of some of the more commonly-used predefined
8719 variables.  This list is not exhaustive, and the default values shown here may
8720 not be what @code{make} selects for your environment.  To see the
8721 complete list of predefined variables for your instance of GNU @code{make} you
8722 can run @samp{make -p} in a directory with no makefiles.
8724 Here is a table of some of the more common variables used as names of
8725 programs in built-in rules:
8726 makefiles.
8728 @table @code
8729 @item AR
8730 @vindex AR
8731 Archive-maintaining program; default @samp{ar}.
8732 @pindex ar
8734 @item AS
8735 @vindex AS
8736 Program for compiling assembly files; default @samp{as}.
8737 @pindex as
8739 @item CC
8740 @vindex CC
8741 Program for compiling C programs; default @samp{cc}.
8742 @pindex cc
8744 @item CXX
8745 @vindex CXX
8746 Program for compiling C++ programs; default @samp{g++}.
8747 @pindex g++
8749 @item CPP
8750 @vindex CPP
8751 Program for running the C preprocessor, with results to standard output;
8752 default @samp{$(CC) -E}.
8754 @item FC
8755 @vindex FC
8756 Program for compiling or preprocessing Fortran and Ratfor programs;
8757 default @samp{f77}.
8758 @pindex f77
8760 @item M2C
8761 @vindex M2C
8762 Program to use to compile Modula-2 source code; default @samp{m2c}.
8763 @pindex m2c
8765 @item PC
8766 @vindex PC
8767 Program for compiling Pascal programs; default @samp{pc}.
8768 @pindex pc
8770 @item CO
8771 @vindex CO
8772 Program for extracting a file from RCS; default @samp{co}.
8773 @pindex co
8775 @item GET
8776 @vindex GET
8777 Program for extracting a file from SCCS; default @samp{get}.
8778 @pindex get
8780 @item LEX
8781 @vindex LEX
8782 Program to use to turn Lex grammars into source code; default @samp{lex}.
8783 @pindex lex
8785 @item YACC
8786 @vindex YACC
8787 Program to use to turn Yacc grammars into source code; default @samp{yacc}.
8788 @pindex yacc
8790 @item LINT
8791 @vindex LINT
8792 Program to use to run lint on source code; default @samp{lint}.
8793 @pindex lint
8795 @item MAKEINFO
8796 @vindex MAKEINFO
8797 Program to convert a Texinfo source file into an Info file; default
8798 @samp{makeinfo}.
8799 @pindex makeinfo
8801 @item TEX
8802 @vindex TEX
8803 Program to make @TeX{} @sc{dvi} files from @TeX{} source;
8804 default @samp{tex}.
8805 @pindex tex
8807 @item TEXI2DVI
8808 @vindex TEXI2DVI
8809 Program to make @TeX{} @sc{dvi} files from Texinfo source;
8810 default @samp{texi2dvi}.
8811 @pindex texi2dvi
8813 @item WEAVE
8814 @vindex WEAVE
8815 Program to translate Web into @TeX{}; default @samp{weave}.
8816 @pindex weave
8818 @item CWEAVE
8819 @vindex CWEAVE
8820 Program to translate C Web into @TeX{}; default @samp{cweave}.
8821 @pindex cweave
8823 @item TANGLE
8824 @vindex TANGLE
8825 Program to translate Web into Pascal; default @samp{tangle}.
8826 @pindex tangle
8828 @item CTANGLE
8829 @vindex CTANGLE
8830 Program to translate C Web into C; default @samp{ctangle}.
8831 @pindex ctangle
8833 @item RM
8834 @vindex RM
8835 Command to remove a file; default @samp{rm -f}.
8836 @pindex rm
8837 @end table
8839 Here is a table of variables whose values are additional arguments for the
8840 programs above.  The default values for all of these is the empty
8841 string, unless otherwise noted.
8843 @table @code
8844 @item ARFLAGS
8845 @vindex ARFLAGS
8846 Flags to give the archive-maintaining program; default @samp{rv}.
8848 @item ASFLAGS
8849 @vindex ASFLAGS
8850 Extra flags to give to the assembler (when explicitly
8851 invoked on a @samp{.s} or @samp{.S} file).
8853 @item CFLAGS
8854 @vindex CFLAGS
8855 Extra flags to give to the C compiler.
8857 @item CXXFLAGS
8858 @vindex CXXFLAGS
8859 Extra flags to give to the C++ compiler.
8861 @item COFLAGS
8862 @vindex COFLAGS
8863 Extra flags to give to the RCS @code{co} program.
8865 @item CPPFLAGS
8866 @vindex CPPFLAGS
8867 Extra flags to give to the C preprocessor and programs
8868 that use it (the C and Fortran compilers).
8870 @item FFLAGS
8871 @vindex FFLAGS
8872 Extra flags to give to the Fortran compiler.
8874 @item GFLAGS
8875 @vindex GFLAGS
8876 Extra flags to give to the SCCS @code{get} program.
8878 @item LDFLAGS
8879 @vindex LDFLAGS
8880 Extra flags to give to compilers when they are supposed to invoke the linker,
8881 @samp{ld}.
8883 @item LFLAGS
8884 @vindex LFLAGS
8885 Extra flags to give to Lex.
8887 @item YFLAGS
8888 @vindex YFLAGS
8889 Extra flags to give to Yacc.
8891 @item PFLAGS
8892 @vindex PFLAGS
8893 Extra flags to give to the Pascal compiler.
8895 @item RFLAGS
8896 @vindex RFLAGS
8897 Extra flags to give to the Fortran compiler for Ratfor programs.
8899 @item LINTFLAGS
8900 @vindex LINTFLAGS
8901 Extra flags to give to lint.
8902 @end table
8904 @node Chained Rules, Pattern Rules, Implicit Variables, Implicit Rules
8905 @section Chains of Implicit Rules
8907 @cindex chains of rules
8908 @cindex rule, implicit, chains of
8909 Sometimes a file can be made by a sequence of implicit rules.  For example,
8910 a file @file{@var{n}.o} could be made from @file{@var{n}.y} by running
8911 first Yacc and then @code{cc}.  Such a sequence is called a @dfn{chain}.
8913 If the file @file{@var{n}.c} exists, or is mentioned in the makefile, no
8914 special searching is required: @code{make} finds that the object file can
8915 be made by C compilation from @file{@var{n}.c}; later on, when considering
8916 how to make @file{@var{n}.c}, the rule for running Yacc is
8917 used.  Ultimately both @file{@var{n}.c} and @file{@var{n}.o} are
8918 updated.@refill
8920 @cindex intermediate files
8921 @cindex files, intermediate
8922 However, even if @file{@var{n}.c} does not exist and is not mentioned,
8923 @code{make} knows how to envision it as the missing link between
8924 @file{@var{n}.o} and @file{@var{n}.y}!  In this case, @file{@var{n}.c} is
8925 called an @dfn{intermediate file}.  Once @code{make} has decided to use the
8926 intermediate file, it is entered in the data base as if it had been
8927 mentioned in the makefile, along with the implicit rule that says how to
8928 create it.@refill
8930 Intermediate files are remade using their rules just like all other
8931 files.  But intermediate files are treated differently in two ways.
8933 The first difference is what happens if the intermediate file does not
8934 exist.  If an ordinary file @var{b} does not exist, and @code{make}
8935 considers a target that depends on @var{b}, it invariably creates
8936 @var{b} and then updates the target from @var{b}.  But if @var{b} is an
8937 intermediate file, then @code{make} can leave well enough alone.  It
8938 won't bother updating @var{b}, or the ultimate target, unless some
8939 prerequisite of @var{b} is newer than that target or there is some other
8940 reason to update that target.
8942 The second difference is that if @code{make} @emph{does} create @var{b}
8943 in order to update something else, it deletes @var{b} later on after it
8944 is no longer needed.  Therefore, an intermediate file which did not
8945 exist before @code{make} also does not exist after @code{make}.
8946 @code{make} reports the deletion to you by printing a @samp{rm -f}
8947 command showing which file it is deleting.
8949 Ordinarily, a file cannot be intermediate if it is mentioned in the
8950 makefile as a target or prerequisite.  However, you can explicitly mark a
8951 file as intermediate by listing it as a prerequisite of the special target
8952 @code{.INTERMEDIATE}.  This takes effect even if the file is mentioned
8953 explicitly in some other way.
8955 @cindex intermediate files, preserving
8956 @cindex preserving intermediate files
8957 @cindex secondary files
8958 You can prevent automatic deletion of an intermediate file by marking it
8959 as a @dfn{secondary} file.  To do this, list it as a prerequisite of the
8960 special target @code{.SECONDARY}.  When a file is secondary, @code{make}
8961 will not create the file merely because it does not already exist, but
8962 @code{make} does not automatically delete the file.  Marking a file as
8963 secondary also marks it as intermediate.
8965 You can list the target pattern of an implicit rule (such as @samp{%.o})
8966 as a prerequisite of the special target @code{.PRECIOUS} to preserve
8967 intermediate files made by implicit rules whose target patterns match
8968 that file's name; see @ref{Interrupts}.@refill
8969 @cindex preserving with @code{.PRECIOUS}
8970 @cindex @code{.PRECIOUS} intermediate files
8972 A chain can involve more than two implicit rules.  For example, it is
8973 possible to make a file @file{foo} from @file{RCS/foo.y,v} by running RCS,
8974 Yacc and @code{cc}.  Then both @file{foo.y} and @file{foo.c} are
8975 intermediate files that are deleted at the end.@refill
8977 No single implicit rule can appear more than once in a chain.  This means
8978 that @code{make} will not even consider such a ridiculous thing as making
8979 @file{foo} from @file{foo.o.o} by running the linker twice.  This
8980 constraint has the added benefit of preventing any infinite loop in the
8981 search for an implicit rule chain.
8983 There are some special implicit rules to optimize certain cases that would
8984 otherwise be handled by rule chains.  For example, making @file{foo} from
8985 @file{foo.c} could be handled by compiling and linking with separate
8986 chained rules, using @file{foo.o} as an intermediate file.  But what
8987 actually happens is that a special rule for this case does the compilation
8988 and linking with a single @code{cc} command.  The optimized rule is used in
8989 preference to the step-by-step chain because it comes earlier in the
8990 ordering of rules.
8992 @node Pattern Rules, Last Resort, Chained Rules, Implicit Rules
8993 @section Defining and Redefining Pattern Rules
8995 You define an implicit rule by writing a @dfn{pattern rule}.  A pattern
8996 rule looks like an ordinary rule, except that its target contains the
8997 character @samp{%} (exactly one of them).  The target is considered a
8998 pattern for matching file names; the @samp{%} can match any nonempty
8999 substring, while other characters match only themselves.  The prerequisites
9000 likewise use @samp{%} to show how their names relate to the target name.
9002 Thus, a pattern rule @samp{%.o : %.c} says how to make any file
9003 @file{@var{stem}.o} from another file @file{@var{stem}.c}.@refill
9005 Note that expansion using @samp{%} in pattern rules occurs
9006 @strong{after} any variable or function expansions, which take place
9007 when the makefile is read.  @xref{Using Variables, , How to Use
9008 Variables}, and @ref{Functions, ,Functions for Transforming Text}.
9010 @menu
9011 * Pattern Intro::               An introduction to pattern rules.
9012 * Pattern Examples::            Examples of pattern rules.
9013 * Automatic Variables::         How to use automatic variables in the
9014                                   recipes of implicit rules.
9015 * Pattern Match::               How patterns match.
9016 * Match-Anything Rules::        Precautions you should take prior to
9017                                   defining rules that can match any
9018                                   target file whatever.
9019 * Canceling Rules::             How to override or cancel built-in rules.
9020 @end menu
9022 @node Pattern Intro, Pattern Examples, Pattern Rules, Pattern Rules
9023 @subsection Introduction to Pattern Rules
9024 @cindex pattern rule
9025 @cindex rule, pattern
9027 A pattern rule contains the character @samp{%} (exactly one of them)
9028 in the target; otherwise, it looks exactly like an ordinary rule.  The
9029 target is a pattern for matching file names; the @samp{%} matches any
9030 nonempty substring, while other characters match only themselves.
9031 @cindex target pattern, implicit
9032 @cindex @code{%}, in pattern rules
9034 For example, @samp{%.c} as a pattern matches any file name that ends in
9035 @samp{.c}.  @samp{s.%.c} as a pattern matches any file name that starts
9036 with @samp{s.}, ends in @samp{.c} and is at least five characters long.
9037 (There must be at least one character to match the @samp{%}.)  The substring
9038 that the @samp{%} matches is called the @dfn{stem}.@refill
9040 @samp{%} in a prerequisite of a pattern rule stands for the same stem
9041 that was matched by the @samp{%} in the target.  In order for the
9042 pattern rule to apply, its target pattern must match the file name
9043 under consideration and all of its prerequisites (after pattern
9044 substitution) must name files that exist or can be made.  These files
9045 become prerequisites of the target.
9046 @cindex prerequisite pattern, implicit
9048 Thus, a rule of the form
9050 @example
9051 %.o : %.c ; @var{recipe}@dots{}
9052 @end example
9054 @noindent
9055 specifies how to make a file @file{@var{n}.o}, with another file
9056 @file{@var{n}.c} as its prerequisite, provided that @file{@var{n}.c}
9057 exists or can be made.
9059 There may also be prerequisites that do not use @samp{%}; such a prerequisite
9060 attaches to every file made by this pattern rule.  These unvarying
9061 prerequisites are useful occasionally.
9063 A pattern rule need not have any prerequisites that contain @samp{%}, or
9064 in fact any prerequisites at all.  Such a rule is effectively a general
9065 wildcard.  It provides a way to make any file that matches the target
9066 pattern.  @xref{Last Resort}.
9068 @c !!! The end of of this paragraph should be rewritten.  --bob
9069 Pattern rules may have more than one target.  Unlike normal rules,
9070 this does not act as many different rules with the same prerequisites
9071 and recipe.  If a pattern rule has multiple targets, @code{make} knows
9072 that the rule's recipe is responsible for making all of the targets.
9073 The recipe is executed only once to make all the targets.  When
9074 searching for a pattern rule to match a target, the target patterns of
9075 a rule other than the one that matches the target in need of a rule
9076 are incidental: @code{make} worries only about giving a recipe and
9077 prerequisites to the file presently in question.  However, when this
9078 file's recipe is run, the other targets are marked as having been
9079 updated themselves.
9080 @cindex multiple targets, in pattern rule
9081 @cindex target, multiple in pattern rule
9083 The order in which pattern rules appear in the makefile is important
9084 since this is the order in which they are considered.
9085 Of equally applicable
9086 rules, only the first one found is used.  The rules you write take precedence
9087 over those that are built in.  Note however, that a rule whose
9088 prerequisites actually exist or are mentioned always takes priority over a
9089 rule with prerequisites that must be made by chaining other implicit rules.
9090 @cindex pattern rules, order of
9091 @cindex order of pattern rules
9093 @node Pattern Examples, Automatic Variables, Pattern Intro, Pattern Rules
9094 @subsection Pattern Rule Examples
9096 Here are some examples of pattern rules actually predefined in
9097 @code{make}.  First, the rule that compiles @samp{.c} files into @samp{.o}
9098 files:@refill
9100 @example
9101 %.o : %.c
9102         $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@@
9103 @end example
9105 @noindent
9106 defines a rule that can make any file @file{@var{x}.o} from
9107 @file{@var{x}.c}.  The recipe uses the automatic variables @samp{$@@} and
9108 @samp{$<} to substitute the names of the target file and the source file
9109 in each case where the rule applies (@pxref{Automatic Variables}).@refill
9111 Here is a second built-in rule:
9113 @example
9114 % :: RCS/%,v
9115         $(CO) $(COFLAGS) $<
9116 @end example
9118 @noindent
9119 defines a rule that can make any file @file{@var{x}} whatsoever from a
9120 corresponding file @file{@var{x},v} in the subdirectory @file{RCS}.  Since
9121 the target is @samp{%}, this rule will apply to any file whatever, provided
9122 the appropriate prerequisite file exists.  The double colon makes the rule
9123 @dfn{terminal}, which means that its prerequisite may not be an intermediate
9124 file (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}).@refill
9126 @need 500
9127 This pattern rule has two targets:
9129 @example
9130 @group
9131 %.tab.c %.tab.h: %.y
9132         bison -d $<
9133 @end group
9134 @end example
9136 @noindent
9137 @c The following paragraph is rewritten to avoid overfull hboxes
9138 This tells @code{make} that the recipe @samp{bison -d @var{x}.y} will
9139 make both @file{@var{x}.tab.c} and @file{@var{x}.tab.h}.  If the file
9140 @file{foo} depends on the files @file{parse.tab.o} and @file{scan.o}
9141 and the file @file{scan.o} depends on the file @file{parse.tab.h},
9142 when @file{parse.y} is changed, the recipe @samp{bison -d parse.y}
9143 will be executed only once, and the prerequisites of both
9144 @file{parse.tab.o} and @file{scan.o} will be satisfied.  (Presumably
9145 the file @file{parse.tab.o} will be recompiled from @file{parse.tab.c}
9146 and the file @file{scan.o} from @file{scan.c}, while @file{foo} is
9147 linked from @file{parse.tab.o}, @file{scan.o}, and its other
9148 prerequisites, and it will execute happily ever after.)@refill
9150 @node Automatic Variables, Pattern Match, Pattern Examples, Pattern Rules
9151 @subsection Automatic Variables
9152 @cindex automatic variables
9153 @cindex variables, automatic
9154 @cindex variables, and implicit rule
9156 Suppose you are writing a pattern rule to compile a @samp{.c} file into a
9157 @samp{.o} file: how do you write the @samp{cc} command so that it operates
9158 on the right source file name?  You cannot write the name in the recipe,
9159 because the name is different each time the implicit rule is applied.
9161 What you do is use a special feature of @code{make}, the @dfn{automatic
9162 variables}.  These variables have values computed afresh for each rule that
9163 is executed, based on the target and prerequisites of the rule.  In this
9164 example, you would use @samp{$@@} for the object file name and @samp{$<}
9165 for the source file name.
9167 @cindex automatic variables in prerequisites
9168 @cindex prerequisites, and automatic variables
9169 It's very important that you recognize the limited scope in which
9170 automatic variable values are available: they only have values within
9171 the recipe.  In particular, you cannot use them anywhere
9172 within the target list of a rule; they have no value there and will
9173 expand to the empty string.  Also, they cannot be accessed directly
9174 within the prerequisite list of a rule.  A common mistake is
9175 attempting to use @code{$@@} within the prerequisites list; this will
9176 not work.  However, there is a special feature of GNU @code{make},
9177 secondary expansion (@pxref{Secondary Expansion}), which will allow
9178 automatic variable values to be used in prerequisite lists.
9180 Here is a table of automatic variables:
9182 @table @code
9183 @vindex $@@
9184 @vindex @@ @r{(automatic variable)}
9185 @item $@@
9186 The file name of the target of the rule.  If the target is an archive
9187 member, then @samp{$@@} is the name of the archive file.  In a pattern
9188 rule that has multiple targets (@pxref{Pattern Intro, ,Introduction to
9189 Pattern Rules}), @samp{$@@} is the name of whichever target caused the
9190 rule's recipe to be run.
9192 @vindex $%
9193 @vindex % @r{(automatic variable)}
9194 @item $%
9195 The target member name, when the target is an archive member.
9196 @xref{Archives}.  For example, if the target is @file{foo.a(bar.o)} then
9197 @samp{$%} is @file{bar.o} and @samp{$@@} is @file{foo.a}.  @samp{$%} is
9198 empty when the target is not an archive member.
9200 @vindex $<
9201 @vindex < @r{(automatic variable)}
9202 @item $<
9203 The name of the first prerequisite.  If the target got its recipe from
9204 an implicit rule, this will be the first prerequisite added by the
9205 implicit rule (@pxref{Implicit Rules}).
9207 @vindex $?
9208 @vindex ? @r{(automatic variable)}
9209 @item $?
9210 The names of all the prerequisites that are newer than the target, with
9211 spaces between them.  For prerequisites which are archive members, only
9212 the named member is used (@pxref{Archives}).
9213 @cindex prerequisites, list of changed
9214 @cindex list of changed prerequisites
9216 @vindex $^
9217 @vindex ^ @r{(automatic variable)}
9218 @item $^
9219 The names of all the prerequisites, with spaces between them.  For
9220 prerequisites which are archive members, only the named member is used
9221 (@pxref{Archives}).  A target has only one prerequisite on each other file
9222 it depends on, no matter how many times each file is listed as a
9223 prerequisite.  So if you list a prerequisite more than once for a target,
9224 the value of @code{$^} contains just one copy of the name.  This list
9225 does @strong{not} contain any of the order-only prerequisites; for those
9226 see the @samp{$|} variable, below.
9227 @cindex prerequisites, list of all
9228 @cindex list of all prerequisites
9230 @vindex $+
9231 @vindex + @r{(automatic variable)}
9232 @item $+
9233 This is like @samp{$^}, but prerequisites listed more than once are
9234 duplicated in the order they were listed in the makefile.  This is
9235 primarily useful for use in linking commands where it is meaningful to
9236 repeat library file names in a particular order.
9238 @vindex $|
9239 @vindex | @r{(automatic variable)}
9240 @item $|
9241 The names of all the order-only prerequisites, with spaces between
9242 them.
9244 @vindex $*
9245 @vindex * @r{(automatic variable)}
9246 @item $*
9247 The stem with which an implicit rule matches (@pxref{Pattern Match, ,How
9248 Patterns Match}).  If the target is @file{dir/a.foo.b} and the target
9249 pattern is @file{a.%.b} then the stem is @file{dir/foo}.  The stem is
9250 useful for constructing names of related files.@refill
9251 @cindex stem, variable for
9253 In a static pattern rule, the stem is part of the file name that matched
9254 the @samp{%} in the target pattern.
9256 In an explicit rule, there is no stem; so @samp{$*} cannot be determined
9257 in that way.  Instead, if the target name ends with a recognized suffix
9258 (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}), @samp{$*} is set to
9259 the target name minus the suffix.  For example, if the target name is
9260 @samp{foo.c}, then @samp{$*} is set to @samp{foo}, since @samp{.c} is a
9261 suffix.  GNU @code{make} does this bizarre thing only for compatibility
9262 with other implementations of @code{make}.  You should generally avoid
9263 using @samp{$*} except in implicit rules or static pattern rules.@refill
9265 If the target name in an explicit rule does not end with a recognized
9266 suffix, @samp{$*} is set to the empty string for that rule.
9267 @end table
9269 @samp{$?} is useful even in explicit rules when you wish to operate on only
9270 the prerequisites that have changed.  For example, suppose that an archive
9271 named @file{lib} is supposed to contain copies of several object files.
9272 This rule copies just the changed object files into the archive:
9274 @example
9275 @group
9276 lib: foo.o bar.o lose.o win.o
9277         ar r lib $?
9278 @end group
9279 @end example
9281 Of the variables listed above, four have values that are single file
9282 names, and three have values that are lists of file names.  These seven
9283 have variants that get just the file's directory name or just the file
9284 name within the directory.  The variant variables' names are formed by
9285 appending @samp{D} or @samp{F}, respectively.  These variants are
9286 semi-obsolete in GNU @code{make} since the functions @code{dir} and
9287 @code{notdir} can be used to get a similar effect (@pxref{File Name
9288 Functions, , Functions for File Names}).  Note, however, that the
9289 @samp{D} variants all omit the trailing slash which always appears in
9290 the output of the @code{dir} function.  Here is a table of the variants:
9292 @table @samp
9293 @vindex $(@@D)
9294 @vindex @@D @r{(automatic variable)}
9295 @item $(@@D)
9296 The directory part of the file name of the target, with the trailing
9297 slash removed.  If the value of @samp{$@@} is @file{dir/foo.o} then
9298 @samp{$(@@D)} is @file{dir}.  This value is @file{.} if @samp{$@@} does
9299 not contain a slash.
9301 @vindex $(@@F)
9302 @vindex @@F @r{(automatic variable)}
9303 @item $(@@F)
9304 The file-within-directory part of the file name of the target.  If the
9305 value of @samp{$@@} is @file{dir/foo.o} then @samp{$(@@F)} is
9306 @file{foo.o}.  @samp{$(@@F)} is equivalent to @samp{$(notdir $@@)}.
9308 @vindex $(*D)
9309 @vindex *D @r{(automatic variable)}
9310 @item $(*D)
9311 @vindex $(*F)
9312 @vindex *F @r{(automatic variable)}
9313 @itemx $(*F)
9314 The directory part and the file-within-directory
9315 part of the stem; @file{dir} and @file{foo} in this example.
9317 @vindex $(%D)
9318 @vindex %D @r{(automatic variable)}
9319 @item $(%D)
9320 @vindex $(%F)
9321 @vindex %F @r{(automatic variable)}
9322 @itemx $(%F)
9323 The directory part and the file-within-directory part of the target
9324 archive member name.  This makes sense only for archive member targets
9325 of the form @file{@var{archive}(@var{member})} and is useful only when
9326 @var{member} may contain a directory name.  (@xref{Archive Members,
9327 ,Archive Members as Targets}.)
9329 @vindex $(<D)
9330 @vindex <D @r{(automatic variable)}
9331 @item $(<D)
9332 @vindex $(<F)
9333 @vindex <F @r{(automatic variable)}
9334 @itemx $(<F)
9335 The directory part and the file-within-directory
9336 part of the first prerequisite.
9338 @vindex $(^D)
9339 @vindex ^D @r{(automatic variable)}
9340 @item $(^D)
9341 @vindex $(^F)
9342 @vindex ^F @r{(automatic variable)}
9343 @itemx $(^F)
9344 Lists of the directory parts and the file-within-directory
9345 parts of all prerequisites.
9347 @vindex $(+D)
9348 @vindex +D @r{(automatic variable)}
9349 @item $(+D)
9350 @vindex $(+F)
9351 @vindex +F @r{(automatic variable)}
9352 @itemx $(+F)
9353 Lists of the directory parts and the file-within-directory
9354 parts of all prerequisites, including multiple instances of duplicated
9355 prerequisites.
9357 @vindex $(?D)
9358 @vindex ?D @r{(automatic variable)}
9359 @item $(?D)
9360 @vindex $(?F)
9361 @vindex ?F @r{(automatic variable)}
9362 @itemx $(?F)
9363 Lists of the directory parts and the file-within-directory parts of
9364 all prerequisites that are newer than the target.
9365 @end table
9367 Note that we use a special stylistic convention when we talk about these
9368 automatic variables; we write ``the value of @samp{$<}'', rather than
9369 @w{``the variable @code{<}''} as we would write for ordinary variables
9370 such as @code{objects} and @code{CFLAGS}.  We think this convention
9371 looks more natural in this special case.  Please do not assume it has a
9372 deep significance; @samp{$<} refers to the variable named @code{<} just
9373 as @samp{$(CFLAGS)} refers to the variable named @code{CFLAGS}.
9374 You could just as well use @samp{$(<)} in place of @samp{$<}.
9376 @node Pattern Match, Match-Anything Rules, Automatic Variables, Pattern Rules
9377 @subsection How Patterns Match
9379 @cindex stem
9380 A target pattern is composed of a @samp{%} between a prefix and a suffix,
9381 either or both of which may be empty.  The pattern matches a file name only
9382 if the file name starts with the prefix and ends with the suffix, without
9383 overlap.  The text between the prefix and the suffix is called the
9384 @dfn{stem}.  Thus, when the pattern @samp{%.o} matches the file name
9385 @file{test.o}, the stem is @samp{test}.  The pattern rule prerequisites are
9386 turned into actual file names by substituting the stem for the character
9387 @samp{%}.  Thus, if in the same example one of the prerequisites is written
9388 as @samp{%.c}, it expands to @samp{test.c}.@refill
9390 When the target pattern does not contain a slash (and it usually does
9391 not), directory names in the file names are removed from the file name
9392 before it is compared with the target prefix and suffix.  After the
9393 comparison of the file name to the target pattern, the directory
9394 names, along with the slash that ends them, are added on to the
9395 prerequisite file names generated from the pattern rule's prerequisite
9396 patterns and the file name.  The directories are ignored only for the
9397 purpose of finding an implicit rule to use, not in the application of
9398 that rule.  Thus, @samp{e%t} matches the file name @file{src/eat},
9399 with @samp{src/a} as the stem.  When prerequisites are turned into file
9400 names, the directories from the stem are added at the front, while the
9401 rest of the stem is substituted for the @samp{%}.  The stem
9402 @samp{src/a} with a prerequisite pattern @samp{c%r} gives the file name
9403 @file{src/car}.@refill
9405 @node Match-Anything Rules, Canceling Rules, Pattern Match, Pattern Rules
9406 @subsection Match-Anything Pattern Rules
9408 @cindex match-anything rule
9409 @cindex terminal rule
9410 When a pattern rule's target is just @samp{%}, it matches any file name
9411 whatever.  We call these rules @dfn{match-anything} rules.  They are very
9412 useful, but it can take a lot of time for @code{make} to think about them,
9413 because it must consider every such rule for each file name listed either
9414 as a target or as a prerequisite.
9416 Suppose the makefile mentions @file{foo.c}.  For this target, @code{make}
9417 would have to consider making it by linking an object file @file{foo.c.o},
9418 or by C compilation-and-linking in one step from @file{foo.c.c}, or by
9419 Pascal compilation-and-linking from @file{foo.c.p}, and many other
9420 possibilities.
9422 We know these possibilities are ridiculous since @file{foo.c} is a C source
9423 file, not an executable.  If @code{make} did consider these possibilities,
9424 it would ultimately reject them, because files such as @file{foo.c.o} and
9425 @file{foo.c.p} would not exist.  But these possibilities are so
9426 numerous that @code{make} would run very slowly if it had to consider
9427 them.@refill
9429 To gain speed, we have put various constraints on the way @code{make}
9430 considers match-anything rules.  There are two different constraints that
9431 can be applied, and each time you define a match-anything rule you must
9432 choose one or the other for that rule.
9434 One choice is to mark the match-anything rule as @dfn{terminal} by defining
9435 it with a double colon.  When a rule is terminal, it does not apply unless
9436 its prerequisites actually exist.  Prerequisites that could be made with
9437 other implicit rules are not good enough.  In other words, no further
9438 chaining is allowed beyond a terminal rule.
9440 For example, the built-in implicit rules for extracting sources from RCS
9441 and SCCS files are terminal; as a result, if the file @file{foo.c,v} does
9442 not exist, @code{make} will not even consider trying to make it as an
9443 intermediate file from @file{foo.c,v.o} or from @file{RCS/SCCS/s.foo.c,v}.
9444 RCS and SCCS files are generally ultimate source files, which should not be
9445 remade from any other files; therefore, @code{make} can save time by not
9446 looking for ways to remake them.@refill
9448 If you do not mark the match-anything rule as terminal, then it is
9449 nonterminal.  A nonterminal match-anything rule cannot apply to a file name
9450 that indicates a specific type of data.  A file name indicates a specific
9451 type of data if some non-match-anything implicit rule target matches it.
9453 For example, the file name @file{foo.c} matches the target for the pattern
9454 rule @samp{%.c : %.y} (the rule to run Yacc).  Regardless of whether this
9455 rule is actually applicable (which happens only if there is a file
9456 @file{foo.y}), the fact that its target matches is enough to prevent
9457 consideration of any nonterminal match-anything rules for the file
9458 @file{foo.c}.  Thus, @code{make} will not even consider trying to make
9459 @file{foo.c} as an executable file from @file{foo.c.o}, @file{foo.c.c},
9460 @file{foo.c.p}, etc.@refill
9462 The motivation for this constraint is that nonterminal match-anything
9463 rules are used for making files containing specific types of data (such as
9464 executable files) and a file name with a recognized suffix indicates some
9465 other specific type of data (such as a C source file).
9467 Special built-in dummy pattern rules are provided solely to recognize
9468 certain file names so that nonterminal match-anything rules will not be
9469 considered.  These dummy rules have no prerequisites and no recipes, and
9470 they are ignored for all other purposes.  For example, the built-in
9471 implicit rule
9473 @example
9474 %.p :
9475 @end example
9477 @noindent
9478 exists to make sure that Pascal source files such as @file{foo.p} match a
9479 specific target pattern and thereby prevent time from being wasted looking
9480 for @file{foo.p.o} or @file{foo.p.c}.
9482 Dummy pattern rules such as the one for @samp{%.p} are made for every
9483 suffix listed as valid for use in suffix rules (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}).
9485 @node Canceling Rules,  , Match-Anything Rules, Pattern Rules
9486 @subsection Canceling Implicit Rules
9488 You can override a built-in implicit rule (or one you have defined
9489 yourself) by defining a new pattern rule with the same target and
9490 prerequisites, but a different recipe.  When the new rule is defined, the
9491 built-in one is replaced.  The new rule's position in the sequence of
9492 implicit rules is determined by where you write the new rule.
9494 You can cancel a built-in implicit rule by defining a pattern rule with the
9495 same target and prerequisites, but no recipe.  For example, the following
9496 would cancel the rule that runs the assembler:
9498 @example
9499 %.o : %.s
9500 @end example
9502 @node Last Resort, Suffix Rules, Pattern Rules, Implicit Rules
9503 @section Defining Last-Resort Default Rules
9504 @cindex last-resort default rules
9505 @cindex default rules, last-resort
9507 You can define a last-resort implicit rule by writing a terminal
9508 match-anything pattern rule with no prerequisites (@pxref{Match-Anything
9509 Rules}).  This is just like any other pattern rule; the only thing
9510 special about it is that it will match any target.  So such a rule's
9511 recipe is used for all targets and prerequisites that have no recipe
9512 of their own and for which no other implicit rule applies.
9514 For example, when testing a makefile, you might not care if the source
9515 files contain real data, only that they exist.  Then you might do this:
9517 @example
9519         touch $@@
9520 @end example
9522 @noindent
9523 to cause all the source files needed (as prerequisites) to be created
9524 automatically.
9526 @findex .DEFAULT
9527 You can instead define a recipe to be used for targets for which there
9528 are no rules at all, even ones which don't specify recipes.  You do
9529 this by writing a rule for the target @code{.DEFAULT}.  Such a rule's
9530 recipe is used for all prerequisites which do not appear as targets in
9531 any explicit rule, and for which no implicit rule applies.  Naturally,
9532 there is no @code{.DEFAULT} rule unless you write one.
9534 If you use @code{.DEFAULT} with no recipe or prerequisites:
9536 @example
9537 .DEFAULT:
9538 @end example
9540 @noindent
9541 the recipe previously stored for @code{.DEFAULT} is cleared.  Then
9542 @code{make} acts as if you had never defined @code{.DEFAULT} at all.
9544 If you do not want a target to get the recipe from a match-anything
9545 pattern rule or @code{.DEFAULT}, but you also do not want any recipe
9546 to be run for the target, you can give it an empty recipe
9547 (@pxref{Empty Recipes, ,Defining Empty Recipes}).@refill
9549 You can use a last-resort rule to override part of another makefile.
9550 @xref{Overriding Makefiles, , Overriding Part of Another Makefile}.
9552 @node Suffix Rules, Implicit Rule Search, Last Resort, Implicit Rules
9553 @section Old-Fashioned Suffix Rules
9554 @cindex old-fashioned suffix rules
9555 @cindex suffix rule
9557 @dfn{Suffix rules} are the old-fashioned way of defining implicit rules for
9558 @code{make}.  Suffix rules are obsolete because pattern rules are more
9559 general and clearer.  They are supported in GNU @code{make} for
9560 compatibility with old makefiles.  They come in two kinds:
9561 @dfn{double-suffix} and @dfn{single-suffix}.@refill
9563 A double-suffix rule is defined by a pair of suffixes: the target suffix
9564 and the source suffix.  It matches any file whose name ends with the
9565 target suffix.  The corresponding implicit prerequisite is made by
9566 replacing the target suffix with the source suffix in the file name.  A
9567 two-suffix rule whose target and source suffixes are @samp{.o} and
9568 @samp{.c} is equivalent to the pattern rule @samp{%.o : %.c}.
9570 A single-suffix rule is defined by a single suffix, which is the source
9571 suffix.  It matches any file name, and the corresponding implicit
9572 prerequisite name is made by appending the source suffix.  A single-suffix
9573 rule whose source suffix is @samp{.c} is equivalent to the pattern rule
9574 @samp{% : %.c}.
9576 Suffix rule definitions are recognized by comparing each rule's target
9577 against a defined list of known suffixes.  When @code{make} sees a rule
9578 whose target is a known suffix, this rule is considered a single-suffix
9579 rule.  When @code{make} sees a rule whose target is two known suffixes
9580 concatenated, this rule is taken as a double-suffix rule.
9582 For example, @samp{.c} and @samp{.o} are both on the default list of
9583 known suffixes.  Therefore, if you define a rule whose target is
9584 @samp{.c.o}, @code{make} takes it to be a double-suffix rule with source
9585 suffix @samp{.c} and target suffix @samp{.o}.  Here is the old-fashioned
9586 way to define the rule for compiling a C source file:@refill
9588 @example
9589 .c.o:
9590         $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
9591 @end example
9593 Suffix rules cannot have any prerequisites of their own.  If they have any,
9594 they are treated as normal files with funny names, not as suffix rules.
9595 Thus, the rule:
9597 @example
9598 .c.o: foo.h
9599         $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
9600 @end example
9602 @noindent
9603 tells how to make the file @file{.c.o} from the prerequisite file
9604 @file{foo.h}, and is not at all like the pattern rule:
9606 @example
9607 %.o: %.c foo.h
9608         $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
9609 @end example
9611 @noindent
9612 which tells how to make @samp{.o} files from @samp{.c} files, and makes all
9613 @samp{.o} files using this pattern rule also depend on @file{foo.h}.
9615 Suffix rules with no recipe are also meaningless.  They do not remove
9616 previous rules as do pattern rules with no recipe (@pxref{Canceling
9617 Rules, , Canceling Implicit Rules}).  They simply enter the suffix or
9618 pair of suffixes concatenated as a target in the data base.@refill
9620 @findex .SUFFIXES
9621 The known suffixes are simply the names of the prerequisites of the special
9622 target @code{.SUFFIXES}.  You can add your own suffixes by writing a rule
9623 for @code{.SUFFIXES} that adds more prerequisites, as in:
9625 @example
9626 .SUFFIXES: .hack .win
9627 @end example
9629 @noindent
9630 which adds @samp{.hack} and @samp{.win} to the end of the list of suffixes.
9632 If you wish to eliminate the default known suffixes instead of just adding
9633 to them, write a rule for @code{.SUFFIXES} with no prerequisites.  By
9634 special dispensation, this eliminates all existing prerequisites of
9635 @code{.SUFFIXES}.  You can then write another rule to add the suffixes you
9636 want.  For example,
9638 @example
9639 @group
9640 .SUFFIXES:            # @r{Delete the default suffixes}
9641 .SUFFIXES: .c .o .h   # @r{Define our suffix list}
9642 @end group
9643 @end example
9645 The @samp{-r} or @samp{--no-builtin-rules} flag causes the default
9646 list of suffixes to be empty.
9648 @vindex SUFFIXES
9649 The variable @code{SUFFIXES} is defined to the default list of suffixes
9650 before @code{make} reads any makefiles.  You can change the list of suffixes
9651 with a rule for the special target @code{.SUFFIXES}, but that does not alter
9652 this variable.
9654 @node Implicit Rule Search,  , Suffix Rules, Implicit Rules
9655 @section Implicit Rule Search Algorithm
9656 @cindex implicit rule, search algorithm
9657 @cindex search algorithm, implicit rule
9659 Here is the procedure @code{make} uses for searching for an implicit rule
9660 for a target @var{t}.  This procedure is followed for each double-colon
9661 rule with no recipe, for each target of ordinary rules none of which have
9662 a recipe, and for each prerequisite that is not the target of any rule.  It
9663 is also followed recursively for prerequisites that come from implicit
9664 rules, in the search for a chain of rules.
9666 Suffix rules are not mentioned in this algorithm because suffix rules are
9667 converted to equivalent pattern rules once the makefiles have been read in.
9669 For an archive member target of the form
9670 @samp{@var{archive}(@var{member})}, the following algorithm is run
9671 twice, first using the entire target name @var{t}, and second using
9672 @samp{(@var{member})} as the target @var{t} if the first run found no
9673 rule.@refill
9675 @enumerate
9676 @item
9677 Split @var{t} into a directory part, called @var{d}, and the rest,
9678 called @var{n}.  For example, if @var{t} is @samp{src/foo.o}, then
9679 @var{d} is @samp{src/} and @var{n} is @samp{foo.o}.@refill
9681 @item
9682 Make a list of all the pattern rules one of whose targets matches
9683 @var{t} or @var{n}.  If the target pattern contains a slash, it is
9684 matched against @var{t}; otherwise, against @var{n}.
9686 @item
9687 If any rule in that list is @emph{not} a match-anything rule, then
9688 remove all nonterminal match-anything rules from the list.
9690 @item
9691 Remove from the list all rules with no recipe.
9693 @item
9694 For each pattern rule in the list:
9696 @enumerate a
9697 @item
9698 Find the stem @var{s}, which is the nonempty part of @var{t} or @var{n}
9699 matched by the @samp{%} in the target pattern.@refill
9701 @item
9702 Compute the prerequisite names by substituting @var{s} for @samp{%}; if
9703 the target pattern does not contain a slash, append @var{d} to
9704 the front of each prerequisite name.@refill
9706 @item
9707 Test whether all the prerequisites exist or ought to exist.  (If a
9708 file name is mentioned in the makefile as a target or as an explicit
9709 prerequisite, then we say it ought to exist.)
9711 If all prerequisites exist or ought to exist, or there are no prerequisites,
9712 then this rule applies.
9713 @end enumerate
9715 @item
9716 If no pattern rule has been found so far, try harder.
9717 For each pattern rule in the list:
9719 @enumerate a
9720 @item
9721 If the rule is terminal, ignore it and go on to the next rule.
9723 @item
9724 Compute the prerequisite names as before.
9726 @item
9727 Test whether all the prerequisites exist or ought to exist.
9729 @item
9730 For each prerequisite that does not exist, follow this algorithm
9731 recursively to see if the prerequisite can be made by an implicit
9732 rule.
9734 @item
9735 If all prerequisites exist, ought to exist, or can be
9736 made by implicit rules, then this rule applies.
9737 @end enumerate
9739 @item
9740 If no implicit rule applies, the rule for @code{.DEFAULT}, if any,
9741 applies.  In that case, give @var{t} the same recipe that
9742 @code{.DEFAULT} has.  Otherwise, there is no recipe for @var{t}.
9743 @end enumerate
9745 Once a rule that applies has been found, for each target pattern of
9746 the rule other than the one that matched @var{t} or @var{n}, the
9747 @samp{%} in the pattern is replaced with @var{s} and the resultant
9748 file name is stored until the recipe to remake the target file @var{t}
9749 is executed.  After the recipe is executed, each of these stored file
9750 names are entered into the data base and marked as having been updated
9751 and having the same update status as the file @var{t}.
9753 When the recipe of a pattern rule is executed for @var{t}, the
9754 automatic variables are set corresponding to the target and
9755 prerequisites.  @xref{Automatic Variables}.
9757 @node Archives, Features, Implicit Rules, Top
9758 @chapter Using @code{make} to Update Archive Files
9759 @cindex archive
9761 @dfn{Archive files} are files containing named subfiles called
9762 @dfn{members}; they are maintained with the program @code{ar} and their
9763 main use is as subroutine libraries for linking.
9765 @menu
9766 * Archive Members::             Archive members as targets.
9767 * Archive Update::              The implicit rule for archive member targets.
9768 * Archive Pitfalls::            Dangers to watch out for when using archives.
9769 * Archive Suffix Rules::        You can write a special kind of suffix rule
9770                                   for updating archives.
9771 @end menu
9773 @node Archive Members, Archive Update, Archives, Archives
9774 @section Archive Members as Targets
9775 @cindex archive member targets
9777 An individual member of an archive file can be used as a target or
9778 prerequisite in @code{make}.  You specify the member named @var{member} in
9779 archive file @var{archive} as follows:
9781 @example
9782 @var{archive}(@var{member})
9783 @end example
9785 @noindent
9786 This construct is available only in targets and prerequisites, not in
9787 recipes!  Most programs that you might use in recipes do not support
9788 this syntax and cannot act directly on archive members.  Only
9789 @code{ar} and other programs specifically designed to operate on
9790 archives can do so.  Therefore, valid recipes to update an archive
9791 member target probably must use @code{ar}.  For example, this rule
9792 says to create a member @file{hack.o} in archive @file{foolib} by
9793 copying the file @file{hack.o}:
9795 @example
9796 foolib(hack.o) : hack.o
9797         ar cr foolib hack.o
9798 @end example
9800 In fact, nearly all archive member targets are updated in just this way
9801 and there is an implicit rule to do it for you.  @strong{Please note:} The
9802 @samp{c} flag to @code{ar} is required if the archive file does not
9803 already exist.
9805 To specify several members in the same archive, you can write all the
9806 member names together between the parentheses.  For example:
9808 @example
9809 foolib(hack.o kludge.o)
9810 @end example
9812 @noindent
9813 is equivalent to:
9815 @example
9816 foolib(hack.o) foolib(kludge.o)
9817 @end example
9819 @cindex wildcard, in archive member
9820 You can also use shell-style wildcards in an archive member reference.
9821 @xref{Wildcards, ,Using Wildcard Characters in File Names}.  For
9822 example, @w{@samp{foolib(*.o)}} expands to all existing members of the
9823 @file{foolib} archive whose names end in @samp{.o}; perhaps
9824 @samp{@w{foolib(hack.o)} @w{foolib(kludge.o)}}.
9826 @node Archive Update, Archive Pitfalls, Archive Members, Archives
9827 @section Implicit Rule for Archive Member Targets
9829 Recall that a target that looks like @file{@var{a}(@var{m})} stands for the
9830 member named @var{m} in the archive file @var{a}.
9832 When @code{make} looks for an implicit rule for such a target, as a special
9833 feature it considers implicit rules that match @file{(@var{m})}, as well as
9834 those that match the actual target @file{@var{a}(@var{m})}.
9836 This causes one special rule whose target is @file{(%)} to match.  This
9837 rule updates the target @file{@var{a}(@var{m})} by copying the file @var{m}
9838 into the archive.  For example, it will update the archive member target
9839 @file{foo.a(bar.o)} by copying the @emph{file} @file{bar.o} into the
9840 archive @file{foo.a} as a @emph{member} named @file{bar.o}.
9842 When this rule is chained with others, the result is very powerful.
9843 Thus, @samp{make "foo.a(bar.o)"} (the quotes are needed to protect the
9844 @samp{(} and @samp{)} from being interpreted specially by the shell) in
9845 the presence of a file @file{bar.c} is enough to cause the following
9846 recipe to be run, even without a makefile:
9848 @example
9849 cc -c bar.c -o bar.o
9850 ar r foo.a bar.o
9851 rm -f bar.o
9852 @end example
9854 @noindent
9855 Here @code{make} has envisioned the file @file{bar.o} as an intermediate
9856 file.  @xref{Chained Rules, ,Chains of Implicit Rules}.
9858 Implicit rules such as this one are written using the automatic variable
9859 @samp{$%}.  @xref{Automatic Variables}.
9861 An archive member name in an archive cannot contain a directory name, but
9862 it may be useful in a makefile to pretend that it does.  If you write an
9863 archive member target @file{foo.a(dir/file.o)}, @code{make} will perform
9864 automatic updating with this recipe:
9866 @example
9867 ar r foo.a dir/file.o
9868 @end example
9870 @noindent
9871 which has the effect of copying the file @file{dir/file.o} into a member
9872 named @file{file.o}.  In connection with such usage, the automatic variables
9873 @code{%D} and @code{%F} may be useful.
9875 @menu
9876 * Archive Symbols::             How to update archive symbol directories.
9877 @end menu
9879 @node Archive Symbols,  , Archive Update, Archive Update
9880 @subsection Updating Archive Symbol Directories
9881 @cindex @code{__.SYMDEF}
9882 @cindex updating archive symbol directories
9883 @cindex archive symbol directory updating
9884 @cindex symbol directories, updating archive
9885 @cindex directories, updating archive symbol
9887 An archive file that is used as a library usually contains a special member
9888 named @file{__.SYMDEF} that contains a directory of the external symbol
9889 names defined by all the other members.  After you update any other
9890 members, you need to update @file{__.SYMDEF} so that it will summarize the
9891 other members properly.  This is done by running the @code{ranlib} program:
9893 @example
9894 ranlib @var{archivefile}
9895 @end example
9897 Normally you would put this command in the rule for the archive file,
9898 and make all the members of the archive file prerequisites of that rule.
9899 For example,
9901 @example
9902 libfoo.a: libfoo.a(x.o) libfoo.a(y.o) @dots{}
9903         ranlib libfoo.a
9904 @end example
9906 @noindent
9907 The effect of this is to update archive members @file{x.o}, @file{y.o},
9908 etc., and then update the symbol directory member @file{__.SYMDEF} by
9909 running @code{ranlib}.  The rules for updating the members are not shown
9910 here; most likely you can omit them and use the implicit rule which copies
9911 files into the archive, as described in the preceding section.
9913 This is not necessary when using the GNU @code{ar} program, which
9914 updates the @file{__.SYMDEF} member automatically.
9916 @node Archive Pitfalls, Archive Suffix Rules, Archive Update, Archives
9917 @section Dangers When Using Archives
9918 @cindex archive, and parallel execution
9919 @cindex parallel execution, and archive update
9920 @cindex archive, and @code{-j}
9921 @cindex @code{-j}, and archive update
9923 It is important to be careful when using parallel execution (the
9924 @code{-j} switch; @pxref{Parallel, ,Parallel Execution}) and archives.
9925 If multiple @code{ar} commands run at the same time on the same archive
9926 file, they will not know about each other and can corrupt the file.
9928 Possibly a future version of @code{make} will provide a mechanism to
9929 circumvent this problem by serializing all recipes that operate on the
9930 same archive file.  But for the time being, you must either write your
9931 makefiles to avoid this problem in some other way, or not use @code{-j}.
9933 @node Archive Suffix Rules,  , Archive Pitfalls, Archives
9934 @section Suffix Rules for Archive Files
9935 @cindex suffix rule, for archive
9936 @cindex archive, suffix rule for
9937 @cindex library archive, suffix rule for
9938 @cindex @code{.a} (archives)
9940 You can write a special kind of suffix rule for dealing with archive
9941 files.  @xref{Suffix Rules}, for a full explanation of suffix rules.
9942 Archive suffix rules are obsolete in GNU @code{make}, because pattern
9943 rules for archives are a more general mechanism (@pxref{Archive
9944 Update}).  But they are retained for compatibility with other
9945 @code{make}s.
9947 To write a suffix rule for archives, you simply write a suffix rule
9948 using the target suffix @samp{.a} (the usual suffix for archive files).
9949 For example, here is the old-fashioned suffix rule to update a library
9950 archive from C source files:
9952 @example
9953 @group
9954 .c.a:
9955         $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o
9956         $(AR) r $@@ $*.o
9957         $(RM) $*.o
9958 @end group
9959 @end example
9961 @noindent
9962 This works just as if you had written the pattern rule:
9964 @example
9965 @group
9966 (%.o): %.c
9967         $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o
9968         $(AR) r $@@ $*.o
9969         $(RM) $*.o
9970 @end group
9971 @end example
9973 In fact, this is just what @code{make} does when it sees a suffix rule
9974 with @samp{.a} as the target suffix.  Any double-suffix rule
9975 @w{@samp{.@var{x}.a}} is converted to a pattern rule with the target
9976 pattern @samp{(%.o)} and a prerequisite pattern of @samp{%.@var{x}}.
9978 Since you might want to use @samp{.a} as the suffix for some other kind
9979 of file, @code{make} also converts archive suffix rules to pattern rules
9980 in the normal way (@pxref{Suffix Rules}).  Thus a double-suffix rule
9981 @w{@samp{.@var{x}.a}} produces two pattern rules: @samp{@w{(%.o):}
9982 @w{%.@var{x}}} and @samp{@w{%.a}: @w{%.@var{x}}}.@refill
9984 @node Features, Missing, Archives, Top
9985 @chapter Features of GNU @code{make}
9986 @cindex features of GNU @code{make}
9987 @cindex portability
9988 @cindex compatibility
9990 Here is a summary of the features of GNU @code{make}, for comparison
9991 with and credit to other versions of @code{make}.  We consider the
9992 features of @code{make} in 4.2 BSD systems as a baseline.  If you are
9993 concerned with writing portable makefiles, you should not use the
9994 features of @code{make} listed here, nor the ones in @ref{Missing}.
9996 Many features come from the version of @code{make} in System V.
9998 @itemize @bullet
9999 @item
10000 The @code{VPATH} variable and its special meaning.
10001 @xref{Directory Search, , Searching Directories for Prerequisites}.
10002 This feature exists in System V @code{make}, but is undocumented.
10003 It is documented in 4.3 BSD @code{make} (which says it mimics System V's
10004 @code{VPATH} feature).@refill
10006 @item
10007 Included makefiles.  @xref{Include, ,Including Other Makefiles}.
10008 Allowing multiple files to be included with a single directive is a GNU
10009 extension.
10011 @item
10012 Variables are read from and communicated via the environment.
10013 @xref{Environment, ,Variables from the Environment}.
10015 @item
10016 Options passed through the variable @code{MAKEFLAGS} to recursive
10017 invocations of @code{make}.
10018 @xref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
10020 @item
10021 The automatic variable @code{$%} is set to the member name
10022 in an archive reference.  @xref{Automatic Variables}.
10024 @item
10025 The automatic variables @code{$@@}, @code{$*}, @code{$<}, @code{$%},
10026 and @code{$?} have corresponding forms like @code{$(@@F)} and
10027 @code{$(@@D)}.  We have generalized this to @code{$^} as an obvious
10028 extension.  @xref{Automatic Variables}.@refill
10030 @item
10031 Substitution variable references.
10032 @xref{Reference, ,Basics of Variable References}.
10034 @item
10035 The command line options @samp{-b} and @samp{-m}, accepted and
10036 ignored.  In System V @code{make}, these options actually do something.
10038 @item
10039 Execution of recursive commands to run @code{make} via the variable
10040 @code{MAKE} even if @samp{-n}, @samp{-q} or @samp{-t} is specified.
10041 @xref{Recursion, ,Recursive Use of @code{make}}.
10043 @item
10044 Support for suffix @samp{.a} in suffix rules.  @xref{Archive Suffix
10045 Rules}.  This feature is obsolete in GNU @code{make}, because the
10046 general feature of rule chaining (@pxref{Chained Rules, ,Chains of
10047 Implicit Rules}) allows one pattern rule for installing members in an
10048 archive (@pxref{Archive Update}) to be sufficient.
10050 @item
10051 The arrangement of lines and backslash-newline combinations in
10052 recipes is retained when the recipes are printed, so they appear as
10053 they do in the makefile, except for the stripping of initial
10054 whitespace.
10055 @end itemize
10057 The following features were inspired by various other versions of
10058 @code{make}.  In some cases it is unclear exactly which versions inspired
10059 which others.
10061 @itemize @bullet
10062 @item
10063 Pattern rules using @samp{%}.
10064 This has been implemented in several versions of @code{make}.
10065 We're not sure who invented it first, but it's been spread around a bit.
10066 @xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.@refill
10068 @item
10069 Rule chaining and implicit intermediate files.
10070 This was implemented by Stu Feldman in his version of @code{make}
10071 for AT&T Eighth Edition Research Unix, and later by Andrew Hume of
10072 AT&T Bell Labs in his @code{mk} program (where he terms it
10073 ``transitive closure'').  We do not really know if
10074 we got this from either of them or thought it up ourselves at the
10075 same time.  @xref{Chained Rules, ,Chains of Implicit Rules}.
10077 @item
10078 The automatic variable @code{$^} containing a list of all prerequisites
10079 of the current target.  We did not invent this, but we have no idea who
10080 did.  @xref{Automatic Variables}.  The automatic variable
10081 @code{$+} is a simple extension of @code{$^}.
10083 @item
10084 The ``what if'' flag (@samp{-W} in GNU @code{make}) was (as far as we know)
10085 invented by Andrew Hume in @code{mk}.
10086 @xref{Instead of Execution, ,Instead of Executing the Recipes}.
10088 @item
10089 The concept of doing several things at once (parallelism) exists in
10090 many incarnations of @code{make} and similar programs, though not in the
10091 System V or BSD implementations.  @xref{Execution, ,Recipe Execution}.
10093 @item
10094 Modified variable references using pattern substitution come from
10095 SunOS 4.  @xref{Reference, ,Basics of Variable References}.
10096 This functionality was provided in GNU @code{make} by the
10097 @code{patsubst} function before the alternate syntax was implemented
10098 for compatibility with SunOS 4.  It is not altogether clear who
10099 inspired whom, since GNU @code{make} had @code{patsubst} before SunOS
10100 4 was released.@refill
10102 @item
10103 The special significance of @samp{+} characters preceding recipe lines
10104 (@pxref{Instead of Execution, ,Instead of Executing the Recipes}) is
10105 mandated by @cite{IEEE Standard 1003.2-1992} (POSIX.2).
10107 @item
10108 The @samp{+=} syntax to append to the value of a variable comes from SunOS
10109 4 @code{make}.  @xref{Appending, , Appending More Text to Variables}.
10111 @item
10112 The syntax @w{@samp{@var{archive}(@var{mem1} @var{mem2}@dots{})}} to list
10113 multiple members in a single archive file comes from SunOS 4 @code{make}.
10114 @xref{Archive Members}.
10116 @item
10117 The @code{-include} directive to include makefiles with no error for a
10118 nonexistent file comes from SunOS 4 @code{make}.  (But note that SunOS 4
10119 @code{make} does not allow multiple makefiles to be specified in one
10120 @code{-include} directive.)  The same feature appears with the name
10121 @code{sinclude} in SGI @code{make} and perhaps others.
10122 @end itemize
10124 The remaining features are inventions new in GNU @code{make}:
10126 @itemize @bullet
10127 @item
10128 Use the @samp{-v} or @samp{--version} option to print version and
10129 copyright information.
10131 @item
10132 Use the @samp{-h} or @samp{--help} option to summarize the options to
10133 @code{make}.
10135 @item
10136 Simply-expanded variables.  @xref{Flavors, ,The Two Flavors of Variables}.
10138 @item
10139 Pass command line variable assignments automatically through the
10140 variable @code{MAKE} to recursive @code{make} invocations.
10141 @xref{Recursion, ,Recursive Use of @code{make}}.
10143 @item
10144 Use the @samp{-C} or @samp{--directory} command option to change
10145 directory.  @xref{Options Summary, ,Summary of Options}.
10147 @item
10148 Make verbatim variable definitions with @code{define}.
10149 @xref{Defining, ,Defining Variables Verbatim}.
10151 @item
10152 Declare phony targets with the special target @code{.PHONY}.
10154 Andrew Hume of AT&T Bell Labs implemented a similar feature with a
10155 different syntax in his @code{mk} program.  This seems to be a case of
10156 parallel discovery.  @xref{Phony Targets, ,Phony Targets}.
10158 @item
10159 Manipulate text by calling functions.
10160 @xref{Functions, ,Functions for Transforming Text}.
10162 @item
10163 Use the @samp{-o} or @samp{--old-file}
10164 option to pretend a file's modification-time is old.
10165 @xref{Avoiding Compilation, ,Avoiding Recompilation of Some Files}.
10167 @item
10168 Conditional execution.
10170 This feature has been implemented numerous times in various versions
10171 of @code{make}; it seems a natural extension derived from the features
10172 of the C preprocessor and similar macro languages and is not a
10173 revolutionary concept.  @xref{Conditionals, ,Conditional Parts of Makefiles}.
10175 @item
10176 Specify a search path for included makefiles.
10177 @xref{Include, ,Including Other Makefiles}.
10179 @item
10180 Specify extra makefiles to read with an environment variable.
10181 @xref{MAKEFILES Variable, ,The Variable @code{MAKEFILES}}.
10183 @item
10184 Strip leading sequences of @samp{./} from file names, so that
10185 @file{./@var{file}} and @file{@var{file}} are considered to be the
10186 same file.@refill
10188 @item
10189 Use a special search method for library prerequisites written in the
10190 form @samp{-l@var{name}}.
10191 @xref{Libraries/Search, ,Directory Search for Link Libraries}.
10193 @item
10194 Allow suffixes for suffix rules
10195 (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}) to contain any
10196 characters.  In other versions of @code{make}, they must begin with
10197 @samp{.} and not contain any @samp{/} characters.
10199 @item
10200 Keep track of the current level of @code{make} recursion using the
10201 variable @code{MAKELEVEL}.  @xref{Recursion, ,Recursive Use of @code{make}}.
10203 @item
10204 Provide any goals given on the command line in the variable
10205 @code{MAKECMDGOALS}.  @xref{Goals, ,Arguments to Specify the Goals}.
10207 @item
10208 Specify static pattern rules.  @xref{Static Pattern, ,Static Pattern Rules}.
10210 @item
10211 Provide selective @code{vpath} search.
10212 @xref{Directory Search, ,Searching Directories for Prerequisites}.
10214 @item
10215 Provide computed variable references.
10216 @xref{Reference, ,Basics of Variable References}.
10218 @item
10219 Update makefiles.  @xref{Remaking Makefiles, ,How Makefiles Are Remade}.
10220 System V @code{make} has a very, very limited form of this
10221 functionality in that it will check out SCCS files for makefiles.
10223 @item
10224 Various new built-in implicit rules.
10225 @xref{Catalogue of Rules, ,Catalogue of Implicit Rules}.
10227 @item
10228 The built-in variable @samp{MAKE_VERSION} gives the version number of
10229 @code{make}.
10230 @vindex MAKE_VERSION
10231 @end itemize
10233 @node Missing, Makefile Conventions, Features, Top
10234 @chapter Incompatibilities and Missing Features
10235 @cindex incompatibilities
10236 @cindex missing features
10237 @cindex features, missing
10239 The @code{make} programs in various other systems support a few features
10240 that are not implemented in GNU @code{make}.  The POSIX.2 standard
10241 (@cite{IEEE Standard 1003.2-1992}) which specifies @code{make} does not
10242 require any of these features.@refill
10244 @itemize @bullet
10245 @item
10246 A target of the form @samp{@var{file}((@var{entry}))} stands for a member
10247 of archive file @var{file}.  The member is chosen, not by name, but by
10248 being an object file which defines the linker symbol @var{entry}.@refill
10250 This feature was not put into GNU @code{make} because of the
10251 nonmodularity of putting knowledge into @code{make} of the internal
10252 format of archive file symbol tables.
10253 @xref{Archive Symbols, ,Updating Archive Symbol Directories}.
10255 @item
10256 Suffixes (used in suffix rules) that end with the character @samp{~}
10257 have a special meaning to System V @code{make};
10258 they refer to the SCCS file that corresponds
10259 to the file one would get without the @samp{~}.  For example, the
10260 suffix rule @samp{.c~.o} would make the file @file{@var{n}.o} from
10261 the SCCS file @file{s.@var{n}.c}.  For complete coverage, a whole
10262 series of such suffix rules is required.
10263 @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
10265 In GNU @code{make}, this entire series of cases is handled by two
10266 pattern rules for extraction from SCCS, in combination with the
10267 general feature of rule chaining.
10268 @xref{Chained Rules, ,Chains of Implicit Rules}.
10270 @item
10271 In System V and 4.3 BSD @code{make}, files found by @code{VPATH}
10272 search (@pxref{Directory Search, ,Searching Directories for
10273 Prerequisites}) have their names changed inside recipes.  We feel it
10274 is much cleaner to always use automatic variables and thus make this
10275 feature obsolete.@refill
10277 @item
10278 In some Unix @code{make}s, the automatic variable @code{$*} appearing in
10279 the prerequisites of a rule has the amazingly strange ``feature'' of
10280 expanding to the full name of the @emph{target of that rule}.  We cannot
10281 imagine what went on in the minds of Unix @code{make} developers to do
10282 this; it is utterly inconsistent with the normal definition of @code{$*}.
10283 @vindex * @r{(automatic variable), unsupported bizarre usage}
10285 @item
10286 In some Unix @code{make}s, implicit rule search (@pxref{Implicit
10287 Rules, ,Using Implicit Rules}) is apparently done for @emph{all}
10288 targets, not just those without recipes.  This means you can
10289 do:@refill
10291 @example
10292 @group
10293 foo.o:
10294         cc -c foo.c
10295 @end group
10296 @end example
10298 @noindent
10299 and Unix @code{make} will intuit that @file{foo.o} depends on
10300 @file{foo.c}.@refill
10302 We feel that such usage is broken.  The prerequisite properties of
10303 @code{make} are well-defined (for GNU @code{make}, at least),
10304 and doing such a thing simply does not fit the model.@refill
10306 @item
10307 GNU @code{make} does not include any built-in implicit rules for
10308 compiling or preprocessing EFL programs.  If we hear of anyone who is
10309 using EFL, we will gladly add them.
10311 @item
10312 It appears that in SVR4 @code{make}, a suffix rule can be specified
10313 with no recipe, and it is treated as if it had an empty recipe
10314 (@pxref{Empty Recipes}).  For example:
10316 @example
10317 .c.a:
10318 @end example
10320 @noindent
10321 will override the built-in @file{.c.a} suffix rule.
10323 We feel that it is cleaner for a rule without a recipe to always simply
10324 add to the prerequisite list for the target.  The above example can be
10325 easily rewritten to get the desired behavior in GNU @code{make}:
10327 @example
10328 .c.a: ;
10329 @end example
10331 @item
10332 Some versions of @code{make} invoke the shell with the @samp{-e} flag,
10333 except under @samp{-k} (@pxref{Testing, ,Testing the Compilation of a
10334 Program}).  The @samp{-e} flag tells the shell to exit as soon as any
10335 program it runs returns a nonzero status.  We feel it is cleaner to
10336 write each line of the recipe to stand on its own and not require this
10337 special treatment.
10338 @end itemize
10340 @comment The makefile standards are in a separate file that is also
10341 @comment included by standards.texi.
10342 @include make-stds.texi
10344 @node Quick Reference, Error Messages, Makefile Conventions, Top
10345 @appendix Quick Reference
10347 This appendix summarizes the directives, text manipulation functions,
10348 and special variables which GNU @code{make} understands.
10349 @xref{Special Targets}, @ref{Catalogue of Rules, ,Catalogue of Implicit Rules},
10350 and @ref{Options Summary, ,Summary of Options},
10351 for other summaries.
10353 Here is a summary of the directives GNU @code{make} recognizes:
10355 @table @code
10356 @item define @var{variable}
10357 @itemx endef
10359 Define a multi-line, recursively-expanded variable.@*
10360 @xref{Sequences}.
10362 @item ifdef @var{variable}
10363 @itemx ifndef @var{variable}
10364 @itemx ifeq (@var{a},@var{b})
10365 @itemx ifeq "@var{a}" "@var{b}"
10366 @itemx ifeq '@var{a}' '@var{b}'
10367 @itemx ifneq (@var{a},@var{b})
10368 @itemx ifneq "@var{a}" "@var{b}"
10369 @itemx ifneq '@var{a}' '@var{b}'
10370 @itemx else
10371 @itemx endif
10373 Conditionally evaluate part of the makefile.@*
10374 @xref{Conditionals}.
10376 @item include @var{file}
10377 @itemx -include @var{file}
10378 @itemx sinclude @var{file}
10380 Include another makefile.@*
10381 @xref{Include, ,Including Other Makefiles}.
10383 @item override @var{variable} = @var{value}
10384 @itemx override @var{variable} := @var{value}
10385 @itemx override @var{variable} += @var{value}
10386 @itemx override @var{variable} ?= @var{value}
10387 @itemx override define @var{variable}
10388 @itemx endef
10390 Define a variable, overriding any previous definition, even one from
10391 the command line.@*
10392 @xref{Override Directive, ,The @code{override} Directive}.
10394 @item export
10396 Tell @code{make} to export all variables to child processes by default.@*
10397 @xref{Variables/Recursion, , Communicating Variables to a Sub-@code{make}}.
10399 @item export @var{variable}
10400 @itemx export @var{variable} = @var{value}
10401 @itemx export @var{variable} := @var{value}
10402 @itemx export @var{variable} += @var{value}
10403 @itemx export @var{variable} ?= @var{value}
10404 @itemx unexport @var{variable}
10405 Tell @code{make} whether or not to export a particular variable to child
10406 processes.@*
10407 @xref{Variables/Recursion, , Communicating Variables to a Sub-@code{make}}.
10409 @item vpath @var{pattern} @var{path}
10410 Specify a search path for files matching a @samp{%} pattern.@*
10411 @xref{Selective Search, , The @code{vpath} Directive}.
10413 @item vpath @var{pattern}
10414 Remove all search paths previously specified for @var{pattern}.
10416 @item vpath
10417 Remove all search paths previously specified in any @code{vpath}
10418 directive.
10419 @end table
10421 Here is a summary of the built-in functions (@pxref{Functions}):
10423 @table @code
10424 @item $(subst @var{from},@var{to},@var{text})
10425 Replace @var{from} with @var{to} in @var{text}.@*
10426 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10428 @item $(patsubst @var{pattern},@var{replacement},@var{text})
10429 Replace words matching @var{pattern} with @var{replacement} in @var{text}.@*
10430 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10432 @item $(strip @var{string})
10433 Remove excess whitespace characters from @var{string}.@*
10434 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10436 @item $(findstring @var{find},@var{text})
10437 Locate @var{find} in @var{text}.@*
10438 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10440 @item $(filter @var{pattern}@dots{},@var{text})
10441 Select words in @var{text} that match one of the @var{pattern} words.@*
10442 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10444 @item $(filter-out @var{pattern}@dots{},@var{text})
10445 Select words in @var{text} that @emph{do not} match any of the @var{pattern} words.@*
10446 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10448 @item $(sort @var{list})
10449 Sort the words in @var{list} lexicographically, removing duplicates.@*
10450 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10452 @item $(word @var{n},@var{text})
10453 Extract the @var{n}th word (one-origin) of @var{text}.@*
10454 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10456 @item $(words @var{text})
10457 Count the number of words in @var{text}.@*
10458 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10460 @item $(wordlist @var{s},@var{e},@var{text})
10461 Returns the list of words in @var{text} from @var{s} to @var{e}.@*
10462 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10464 @item $(firstword @var{names}@dots{})
10465 Extract the first word of @var{names}.@*
10466 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10468 @item $(lastword @var{names}@dots{})
10469 Extract the last word of @var{names}.@*
10470 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10472 @item $(dir @var{names}@dots{})
10473 Extract the directory part of each file name.@*
10474 @xref{File Name Functions, ,Functions for File Names}.
10476 @item $(notdir @var{names}@dots{})
10477 Extract the non-directory part of each file name.@*
10478 @xref{File Name Functions, ,Functions for File Names}.
10480 @item $(suffix @var{names}@dots{})
10481 Extract the suffix (the last @samp{.} and following characters) of each file name.@*
10482 @xref{File Name Functions, ,Functions for File Names}.
10484 @item $(basename @var{names}@dots{})
10485 Extract the base name (name without suffix) of each file name.@*
10486 @xref{File Name Functions, ,Functions for File Names}.
10488 @item $(addsuffix @var{suffix},@var{names}@dots{})
10489 Append @var{suffix} to each word in @var{names}.@*
10490 @xref{File Name Functions, ,Functions for File Names}.
10492 @item $(addprefix @var{prefix},@var{names}@dots{})
10493 Prepend @var{prefix} to each word in @var{names}.@*
10494 @xref{File Name Functions, ,Functions for File Names}.
10496 @item $(join @var{list1},@var{list2})
10497 Join two parallel lists of words.@*
10498 @xref{File Name Functions, ,Functions for File Names}.
10500 @item $(wildcard @var{pattern}@dots{})
10501 Find file names matching a shell file name pattern (@emph{not} a
10502 @samp{%} pattern).@*
10503 @xref{Wildcard Function, ,The Function @code{wildcard}}.
10505 @item $(realpath @var{names}@dots{})
10506 For each file name in @var{names}, expand to an absolute name that
10507 does not contain any @code{.}, @code{..}, nor symlinks.@*
10508 @xref{File Name Functions, ,Functions for File Names}.
10510 @item $(abspath @var{names}@dots{})
10511 For each file name in @var{names}, expand to an absolute name that
10512 does not contain any @code{.} or @code{..} components, but preserves
10513 symlinks.@*
10514 @xref{File Name Functions, ,Functions for File Names}.
10516 @item $(error @var{text}@dots{})
10518 When this function is evaluated, @code{make} generates a fatal error
10519 with the message @var{text}.@*
10520 @xref{Make Control Functions, ,Functions That Control Make}.
10522 @item $(warning @var{text}@dots{})
10524 When this function is evaluated, @code{make} generates a warning with
10525 the message @var{text}.@*
10526 @xref{Make Control Functions, ,Functions That Control Make}.
10528 @item $(shell @var{command})
10530 Execute a shell command and return its output.@*
10531 @xref{Shell Function, , The @code{shell} Function}.
10533 @item $(origin @var{variable})
10535 Return a string describing how the @code{make} variable @var{variable} was
10536 defined.@*
10537 @xref{Origin Function, , The @code{origin} Function}.
10539 @item $(flavor @var{variable})
10541 Return a string describing the flavor of the @code{make} variable
10542 @var{variable}.@*
10543 @xref{Flavor Function, , The @code{flavor} Function}.
10545 @item $(foreach @var{var},@var{words},@var{text})
10547 Evaluate @var{text} with @var{var} bound to each word in @var{words},
10548 and concatenate the results.@*
10549 @xref{Foreach Function, ,The @code{foreach} Function}.
10551 @item $(call @var{var},@var{param},@dots{})
10553 Evaluate the variable @var{var} replacing any references to @code{$(1)},
10554 @code{$(2)} with the first, second, etc.@: @var{param} values.@*
10555 @xref{Call Function, ,The @code{call} Function}.
10557 @item $(eval @var{text})
10559 Evaluate @var{text} then read the results as makefile commands.
10560 Expands to the empty string.@*
10561 @xref{Eval Function, ,The @code{eval} Function}.
10563 @item $(value @var{var})
10565 Evaluates to the contents of the variable @var{var}, with no expansion
10566 performed on it.@*
10567 @xref{Value Function, ,The @code{value} Function}.
10568 @end table
10570 Here is a summary of the automatic variables.
10571 @xref{Automatic Variables},
10572 for full information.
10574 @table @code
10575 @item $@@
10576 The file name of the target.
10578 @item $%
10579 The target member name, when the target is an archive member.
10581 @item $<
10582 The name of the first prerequisite.
10584 @item $?
10585 The names of all the prerequisites that are
10586 newer than the target, with spaces between them.
10587 For prerequisites which are archive members, only
10588 the named member is used (@pxref{Archives}).
10590 @item $^
10591 @itemx $+
10592 The names of all the prerequisites, with spaces between them.  For
10593 prerequisites which are archive members, only the named member is used
10594 (@pxref{Archives}).  The value of @code{$^} omits duplicate
10595 prerequisites, while @code{$+} retains them and preserves their order.
10597 @item $*
10598 The stem with which an implicit rule matches
10599 (@pxref{Pattern Match, ,How Patterns Match}).
10601 @item $(@@D)
10602 @itemx $(@@F)
10603 The directory part and the file-within-directory part of @code{$@@}.
10605 @item $(*D)
10606 @itemx $(*F)
10607 The directory part and the file-within-directory part of @code{$*}.
10609 @item $(%D)
10610 @itemx $(%F)
10611 The directory part and the file-within-directory part of @code{$%}.
10613 @item $(<D)
10614 @itemx $(<F)
10615 The directory part and the file-within-directory part of @code{$<}.
10617 @item $(^D)
10618 @itemx $(^F)
10619 The directory part and the file-within-directory part of @code{$^}.
10621 @item $(+D)
10622 @itemx $(+F)
10623 The directory part and the file-within-directory part of @code{$+}.
10625 @item $(?D)
10626 @itemx $(?F)
10627 The directory part and the file-within-directory part of @code{$?}.
10628 @end table
10630 These variables are used specially by GNU @code{make}:
10632 @table @code
10633 @item MAKEFILES
10635 Makefiles to be read on every invocation of @code{make}.@*
10636 @xref{MAKEFILES Variable, ,The Variable @code{MAKEFILES}}.
10638 @item VPATH
10640 Directory search path for files not found in the current directory.@*
10641 @xref{General Search, , @code{VPATH} Search Path for All Prerequisites}.
10643 @item SHELL
10645 The name of the system default command interpreter, usually @file{/bin/sh}.
10646 You can set @code{SHELL} in the makefile to change the shell used to run
10647 recipes.  @xref{Execution, ,Recipe Execution}.  The @code{SHELL}
10648 variable is handled specially when importing from and exporting to the
10649 environment.  @xref{Choosing the Shell}.
10651 @item MAKESHELL
10653 On MS-DOS only, the name of the command interpreter that is to be used
10654 by @code{make}.  This value takes precedence over the value of
10655 @code{SHELL}.  @xref{Execution, ,MAKESHELL variable}.
10657 @item MAKE
10659 The name with which @code{make} was invoked.  Using this variable in
10660 recipes has special meaning.  @xref{MAKE Variable, ,How the
10661 @code{MAKE} Variable Works}.
10663 @item MAKELEVEL
10665 The number of levels of recursion (sub-@code{make}s).@*
10666 @xref{Variables/Recursion}.
10668 @item MAKEFLAGS
10670 The flags given to @code{make}.  You can set this in the environment or
10671 a makefile to set flags.@*
10672 @xref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
10674 It is @emph{never} appropriate to use @code{MAKEFLAGS} directly in a
10675 recipe line: its contents may not be quoted correctly for use in the
10676 shell.  Always allow recursive @code{make}'s to obtain these values
10677 through the environment from its parent.
10679 @item MAKECMDGOALS
10681 The targets given to @code{make} on the command line.  Setting this
10682 variable has no effect on the operation of @code{make}.@*
10683 @xref{Goals, ,Arguments to Specify the Goals}.
10685 @item CURDIR
10687 Set to the pathname of the current working directory (after all
10688 @code{-C} options are processed, if any).  Setting this variable has no
10689 effect on the operation of @code{make}.@*
10690 @xref{Recursion, ,Recursive Use of @code{make}}.
10692 @item SUFFIXES
10694 The default list of suffixes before @code{make} reads any makefiles.
10696 @item .LIBPATTERNS
10697 Defines the naming of the libraries @code{make} searches for, and their
10698 order.@*
10699 @xref{Libraries/Search, ,Directory Search for Link Libraries}.
10700 @end table
10702 @node Error Messages, Complex Makefile, Quick Reference, Top
10703 @comment  node-name,  next,  previous,  up
10704 @appendix Errors Generated by Make
10706 Here is a list of the more common errors you might see generated by
10707 @code{make}, and some information about what they mean and how to fix
10708 them.
10710 Sometimes @code{make} errors are not fatal, especially in the presence
10711 of a @code{-} prefix on a recipe line, or the @code{-k} command line
10712 option.  Errors that are fatal are prefixed with the string
10713 @code{***}.
10715 Error messages are all either prefixed with the name of the program
10716 (usually @samp{make}), or, if the error is found in a makefile, the name
10717 of the file and linenumber containing the problem.
10719 In the table below, these common prefixes are left off.
10721 @table @samp
10723 @item [@var{foo}] Error @var{NN}
10724 @itemx [@var{foo}] @var{signal description}
10725 These errors are not really @code{make} errors at all.  They mean that a
10726 program that @code{make} invoked as part of a recipe returned a
10727 non-0 error code (@samp{Error @var{NN}}), which @code{make} interprets
10728 as failure, or it exited in some other abnormal fashion (with a
10729 signal of some type).  @xref{Errors, ,Errors in Recipes}.
10731 If no @code{***} is attached to the message, then the subprocess failed
10732 but the rule in the makefile was prefixed with the @code{-} special
10733 character, so @code{make} ignored the error.
10735 @item missing separator.  Stop.
10736 @itemx missing separator (did you mean TAB instead of 8 spaces?).  Stop.
10737 This means that @code{make} could not understand much of anything
10738 about the makefile line it just read.  GNU @code{make} looks for
10739 various separators (@code{:}, @code{=}, recipe prefix characters,
10740 etc.) to indicate what kind of line it's parsing.  This message means
10741 it couldn't find a valid one.
10743 One of the most common reasons for this message is that you (or
10744 perhaps your oh-so-helpful editor, as is the case with many MS-Windows
10745 editors) have attempted to indent your recipe lines with spaces
10746 instead of a tab character.  In this case, @code{make} will use the
10747 second form of the error above.  Remember that every line in the
10748 recipe must begin with a tab character (unless you set
10749 @code{.CMDPREFIX}; @pxref{Special Variables}).  Eight spaces do not
10750 count.  @xref{Rule Syntax}.
10752 @item recipe commences before first target.  Stop.
10753 @itemx missing rule before recipe.  Stop.
10754 This means the first thing in the makefile seems to be part of a
10755 recipe: it begins with a recipe prefix character and doesn't appear to
10756 be a legal @code{make} directive (such as a variable assignment).
10757 Recipes must always be associated with a target.
10759 The second form is generated if the line has a semicolon as the first
10760 non-whitespace character; @code{make} interprets this to mean you left
10761 out the "target: prerequisite" section of a rule.  @xref{Rule Syntax}.
10763 @item No rule to make target `@var{xxx}'.
10764 @itemx No rule to make target `@var{xxx}', needed by `@var{yyy}'.
10765 This means that @code{make} decided it needed to build a target, but
10766 then couldn't find any instructions in the makefile on how to do that,
10767 either explicit or implicit (including in the default rules database).
10769 If you want that file to be built, you will need to add a rule to your
10770 makefile describing how that target can be built.  Other possible
10771 sources of this problem are typos in the makefile (if that filename is
10772 wrong) or a corrupted source tree (if that file is not supposed to be
10773 built, but rather only a prerequisite).
10775 @item No targets specified and no makefile found.  Stop.
10776 @itemx No targets.  Stop.
10777 The former means that you didn't provide any targets to be built on the
10778 command line, and @code{make} couldn't find any makefiles to read in.
10779 The latter means that some makefile was found, but it didn't contain any
10780 default goal and none was given on the command line.  GNU @code{make}
10781 has nothing to do in these situations.
10782 @xref{Makefile Arguments, ,Arguments to Specify the Makefile}.@refill
10784 @item Makefile `@var{xxx}' was not found.
10785 @itemx Included makefile `@var{xxx}' was not found.
10786 A makefile specified on the command line (first form) or included
10787 (second form) was not found.
10789 @item warning: overriding recipe for target `@var{xxx}'
10790 @itemx warning: ignoring old recipe for target `@var{xxx}'
10791 GNU @code{make} allows only one recipe to be specified per target
10792 (except for double-colon rules).  If you give a recipe for a target
10793 which already has been defined to have one, this warning is issued and
10794 the second recipe will overwrite the first.  @xref{Multiple Rules,
10795 ,Multiple Rules for One Target}.
10797 @item Circular @var{xxx} <- @var{yyy} dependency dropped.
10798 This means that @code{make} detected a loop in the dependency graph:
10799 after tracing the prerequisite @var{yyy} of target @var{xxx}, and its
10800 prerequisites, etc., one of them depended on @var{xxx} again.
10802 @item Recursive variable `@var{xxx}' references itself (eventually).  Stop.
10803 This means you've defined a normal (recursive) @code{make} variable
10804 @var{xxx} that, when it's expanded, will refer to itself (@var{xxx}).
10805 This is not allowed; either use simply-expanded variables (@code{:=}) or
10806 use the append operator (@code{+=}).  @xref{Using Variables, ,How to Use
10807 Variables}.
10809 @item Unterminated variable reference.  Stop.
10810 This means you forgot to provide the proper closing parenthesis
10811 or brace in your variable or function reference.
10813 @item insufficient arguments to function `@var{xxx}'.  Stop.
10814 This means you haven't provided the requisite number of arguments for
10815 this function.  See the documentation of the function for a description
10816 of its arguments.  @xref{Functions, ,Functions for Transforming Text}.
10818 @item missing target pattern.  Stop.
10819 @itemx multiple target patterns.  Stop.
10820 @itemx target pattern contains no `%'.  Stop.
10821 @itemx mixed implicit and static pattern rules.  Stop.
10822 These are generated for malformed static pattern rules.  The first means
10823 there's no pattern in the target section of the rule; the second means
10824 there are multiple patterns in the target section; the third means
10825 the target doesn't contain a pattern character (@code{%}); and the
10826 fourth means that all three parts of the static pattern rule contain
10827 pattern characters (@code{%})--only the first two parts should.
10828 @xref{Static Usage, ,Syntax of Static Pattern Rules}.
10830 @item warning: -jN forced in submake: disabling jobserver mode.
10831 This warning and the next are generated if @code{make} detects error
10832 conditions related to parallel processing on systems where
10833 sub-@code{make}s can communicate (@pxref{Options/Recursion,
10834 ,Communicating Options to a Sub-@code{make}}).  This warning is
10835 generated if a recursive invocation of a @code{make} process is forced
10836 to have @samp{-j@var{N}} in its argument list (where @var{N} is greater
10837 than one).  This could happen, for example, if you set the @code{MAKE}
10838 environment variable to @samp{make -j2}.  In this case, the
10839 sub-@code{make} doesn't communicate with other @code{make} processes and
10840 will simply pretend it has two jobs of its own.
10842 @item warning: jobserver unavailable: using -j1.  Add `+' to parent make rule.
10843 In order for @code{make} processes to communicate, the parent will pass
10844 information to the child.  Since this could result in problems if the
10845 child process isn't actually a @code{make}, the parent will only do this
10846 if it thinks the child is a @code{make}.  The parent uses the normal
10847 algorithms to determine this (@pxref{MAKE Variable, ,How the @code{MAKE}
10848 Variable Works}).  If the makefile is constructed such that the parent
10849 doesn't know the child is a @code{make} process, then the child will
10850 receive only part of the information necessary.  In this case, the child
10851 will generate this warning message and proceed with its build in a
10852 sequential manner.
10854 @end table
10856 @node Complex Makefile, GNU Free Documentation License, Error Messages, Top
10857 @appendix Complex Makefile Example
10859 Here is the makefile for the GNU @code{tar} program.  This is a
10860 moderately complex makefile.
10862 Because it is the first target, the default goal is @samp{all}.  An
10863 interesting feature of this makefile is that @file{testpad.h} is a
10864 source file automatically created by the @code{testpad} program,
10865 itself compiled from @file{testpad.c}.
10867 If you type @samp{make} or @samp{make all}, then @code{make} creates
10868 the @file{tar} executable, the @file{rmt} daemon that provides
10869 remote tape access, and the @file{tar.info} Info file.
10871 If you type @samp{make install}, then @code{make} not only creates
10872 @file{tar}, @file{rmt}, and @file{tar.info}, but also installs
10873 them.
10875 If you type @samp{make clean}, then @code{make} removes the @samp{.o}
10876 files, and the @file{tar}, @file{rmt}, @file{testpad},
10877 @file{testpad.h}, and @file{core} files.
10879 If you type @samp{make distclean}, then @code{make} not only removes
10880 the same files as does @samp{make clean} but also the
10881 @file{TAGS}, @file{Makefile}, and @file{config.status} files.
10882 (Although it is not evident, this makefile (and
10883 @file{config.status}) is generated by the user with the
10884 @code{configure} program, which is provided in the @code{tar}
10885 distribution, but is not shown here.)
10887 If you type @samp{make realclean}, then @code{make} removes the same
10888 files as does @samp{make distclean} and also removes the Info files
10889 generated from @file{tar.texinfo}.
10891 In addition, there are targets @code{shar} and @code{dist} that create
10892 distribution kits.
10894 @example
10895 @group
10896 # Generated automatically from Makefile.in by configure.
10897 # Un*x Makefile for GNU tar program.
10898 # Copyright (C) 1991 Free Software Foundation, Inc.
10899 @end group
10901 @group
10902 # This program is free software; you can redistribute
10903 # it and/or modify it under the terms of the GNU
10904 # General Public License @dots{}
10905 @dots{}
10906 @dots{}
10907 @end group
10909 SHELL = /bin/sh
10911 #### Start of system configuration section. ####
10913 srcdir = .
10915 @group
10916 # If you use gcc, you should either run the
10917 # fixincludes script that comes with it or else use
10918 # gcc with the -traditional option.  Otherwise ioctl
10919 # calls will be compiled incorrectly on some systems.
10920 CC = gcc -O
10921 YACC = bison -y
10922 INSTALL = /usr/local/bin/install -c
10923 INSTALLDATA = /usr/local/bin/install -c -m 644
10924 @end group
10926 # Things you might add to DEFS:
10927 # -DSTDC_HEADERS        If you have ANSI C headers and
10928 #                       libraries.
10929 # -DPOSIX               If you have POSIX.1 headers and
10930 #                       libraries.
10931 # -DBSD42               If you have sys/dir.h (unless
10932 #                       you use -DPOSIX), sys/file.h,
10933 #                       and st_blocks in `struct stat'.
10934 # -DUSG                 If you have System V/ANSI C
10935 #                       string and memory functions
10936 #                       and headers, sys/sysmacros.h,
10937 #                       fcntl.h, getcwd, no valloc,
10938 #                       and ndir.h (unless
10939 #                       you use -DDIRENT).
10940 # -DNO_MEMORY_H         If USG or STDC_HEADERS but do not
10941 #                       include memory.h.
10942 # -DDIRENT              If USG and you have dirent.h
10943 #                       instead of ndir.h.
10944 # -DSIGTYPE=int         If your signal handlers
10945 #                       return int, not void.
10946 # -DNO_MTIO             If you lack sys/mtio.h
10947 #                       (magtape ioctls).
10948 # -DNO_REMOTE           If you do not have a remote shell
10949 #                       or rexec.
10950 # -DUSE_REXEC           To use rexec for remote tape
10951 #                       operations instead of
10952 #                       forking rsh or remsh.
10953 # -DVPRINTF_MISSING     If you lack vprintf function
10954 #                       (but have _doprnt).
10955 # -DDOPRNT_MISSING      If you lack _doprnt function.
10956 #                       Also need to define
10957 #                       -DVPRINTF_MISSING.
10958 # -DFTIME_MISSING       If you lack ftime system call.
10959 # -DSTRSTR_MISSING      If you lack strstr function.
10960 # -DVALLOC_MISSING      If you lack valloc function.
10961 # -DMKDIR_MISSING       If you lack mkdir and
10962 #                       rmdir system calls.
10963 # -DRENAME_MISSING      If you lack rename system call.
10964 # -DFTRUNCATE_MISSING   If you lack ftruncate
10965 #                       system call.
10966 # -DV7                  On Version 7 Unix (not
10967 #                       tested in a long time).
10968 # -DEMUL_OPEN3          If you lack a 3-argument version
10969 #                       of open, and want to emulate it
10970 #                       with system calls you do have.
10971 # -DNO_OPEN3            If you lack the 3-argument open
10972 #                       and want to disable the tar -k
10973 #                       option instead of emulating open.
10974 # -DXENIX               If you have sys/inode.h
10975 #                       and need it 94 to be included.
10977 DEFS =  -DSIGTYPE=int -DDIRENT -DSTRSTR_MISSING \
10978         -DVPRINTF_MISSING -DBSD42
10979 # Set this to rtapelib.o unless you defined NO_REMOTE,
10980 # in which case make it empty.
10981 RTAPELIB = rtapelib.o
10982 LIBS =
10983 DEF_AR_FILE = /dev/rmt8
10984 DEFBLOCKING = 20
10986 @group
10987 CDEBUG = -g
10988 CFLAGS = $(CDEBUG) -I. -I$(srcdir) $(DEFS) \
10989         -DDEF_AR_FILE=\"$(DEF_AR_FILE)\" \
10990         -DDEFBLOCKING=$(DEFBLOCKING)
10991 LDFLAGS = -g
10992 @end group
10994 @group
10995 prefix = /usr/local
10996 # Prefix for each installed program,
10997 # normally empty or `g'.
10998 binprefix =
11000 # The directory to install tar in.
11001 bindir = $(prefix)/bin
11003 # The directory to install the info files in.
11004 infodir = $(prefix)/info
11005 @end group
11007 #### End of system configuration section. ####
11009 SRC1 =  tar.c create.c extract.c buffer.c \
11010         getoldopt.c update.c gnu.c mangle.c
11011 SRC2 =  version.c list.c names.c diffarch.c \
11012         port.c wildmat.c getopt.c
11013 SRC3 =  getopt1.c regex.c getdate.y
11014 SRCS =  $(SRC1) $(SRC2) $(SRC3)
11015 OBJ1 =  tar.o create.o extract.o buffer.o \
11016         getoldopt.o update.o gnu.o mangle.o
11017 OBJ2 =  version.o list.o names.o diffarch.o \
11018         port.o wildmat.o getopt.o
11019 OBJ3 =  getopt1.o regex.o getdate.o $(RTAPELIB)
11020 OBJS =  $(OBJ1) $(OBJ2) $(OBJ3)
11021 @group
11022 AUX =   README COPYING ChangeLog Makefile.in  \
11023         makefile.pc configure configure.in \
11024         tar.texinfo tar.info* texinfo.tex \
11025         tar.h port.h open3.h getopt.h regex.h \
11026         rmt.h rmt.c rtapelib.c alloca.c \
11027         msd_dir.h msd_dir.c tcexparg.c \
11028         level-0 level-1 backup-specs testpad.c
11029 @end group
11031 .PHONY: all
11032 all:    tar rmt tar.info
11034 @group
11035 tar:    $(OBJS)
11036         $(CC) $(LDFLAGS) -o $@@ $(OBJS) $(LIBS)
11037 @end group
11039 @group
11040 rmt:    rmt.c
11041         $(CC) $(CFLAGS) $(LDFLAGS) -o $@@ rmt.c
11042 @end group
11044 @group
11045 tar.info: tar.texinfo
11046         makeinfo tar.texinfo
11047 @end group
11049 @group
11050 .PHONY: install
11051 install: all
11052         $(INSTALL) tar $(bindir)/$(binprefix)tar
11053         -test ! -f rmt || $(INSTALL) rmt /etc/rmt
11054         $(INSTALLDATA) $(srcdir)/tar.info* $(infodir)
11055 @end group
11057 @group
11058 $(OBJS): tar.h port.h testpad.h
11059 regex.o buffer.o tar.o: regex.h
11060 # getdate.y has 8 shift/reduce conflicts.
11061 @end group
11063 @group
11064 testpad.h: testpad
11065         ./testpad
11066 @end group
11068 @group
11069 testpad: testpad.o
11070         $(CC) -o $@@ testpad.o
11071 @end group
11073 @group
11074 TAGS:   $(SRCS)
11075         etags $(SRCS)
11076 @end group
11078 @group
11079 .PHONY: clean
11080 clean:
11081         rm -f *.o tar rmt testpad testpad.h core
11082 @end group
11084 @group
11085 .PHONY: distclean
11086 distclean: clean
11087         rm -f TAGS Makefile config.status
11088 @end group
11090 @group
11091 .PHONY: realclean
11092 realclean: distclean
11093         rm -f tar.info*
11094 @end group
11096 @group
11097 .PHONY: shar
11098 shar: $(SRCS) $(AUX)
11099         shar $(SRCS) $(AUX) | compress \
11100           > tar-`sed -e '/version_string/!d' \
11101                      -e 's/[^0-9.]*\([0-9.]*\).*/\1/' \
11102                      -e q
11103                      version.c`.shar.Z
11104 @end group
11106 @group
11107 .PHONY: dist
11108 dist: $(SRCS) $(AUX)
11109         echo tar-`sed \
11110              -e '/version_string/!d' \
11111              -e 's/[^0-9.]*\([0-9.]*\).*/\1/' \
11112              -e q
11113              version.c` > .fname
11114         -rm -rf `cat .fname`
11115         mkdir `cat .fname`
11116         ln $(SRCS) $(AUX) `cat .fname`
11117         tar chZf `cat .fname`.tar.Z `cat .fname`
11118         -rm -rf `cat .fname` .fname
11119 @end group
11121 @group
11122 tar.zoo: $(SRCS) $(AUX)
11123         -rm -rf tmp.dir
11124         -mkdir tmp.dir
11125         -rm tar.zoo
11126         for X in $(SRCS) $(AUX) ; do \
11127             echo $$X ; \
11128             sed 's/$$/^M/' $$X \
11129             > tmp.dir/$$X ; done
11130         cd tmp.dir ; zoo aM ../tar.zoo *
11131         -rm -rf tmp.dir
11132 @end group
11133 @end example
11135 @node GNU Free Documentation License, Concept Index, Complex Makefile, Top
11136 @appendixsec GNU Free Documentation License
11137 @cindex FDL, GNU Free Documentation License
11138 @include fdl.texi
11140 @node Concept Index, Name Index, GNU Free Documentation License, Top
11141 @unnumbered Index of Concepts
11143 @printindex cp
11145 @node Name Index,  , Concept Index, Top
11146 @unnumbered Index of Functions, Variables, & Directives
11148 @printindex fn
11150 @bye