- Work around a bug in glibc glob(3), by avoiding GLOB_NOCHECK.
[make.git] / doc / make.texi
blob640391afe3b879afcb9c16d4173ac2eafe3bb5df
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.55 2009/06/04 06:30:27 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 confirmed by Jasimin Huang <jasimin@fsf.org> on 25 Mar 2009
17 @set ISBN 1-882114-83-3
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, 1996,
29 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
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 have the freedom to copy and
42 modify this GNU manual.  Buying copies from the FSF supports it 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 * Canned Recipes::              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 * Multi-Line::                  An alternate way to set a variable
237                                   to a multi-line 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 * Suppressing Inheritance::     Suppress inheritance of variables.
244 * Special Variables::           Variables with special meaning or behavior.
246 Advanced Features for Reference to Variables
248 * Substitution Refs::           Referencing a variable with
249                                   substitutions on the value.
250 * Computed Names::              Computing the name of the variable to refer to.
252 Conditional Parts of Makefiles
254 * Conditional Example::         Example of a conditional
255 * Conditional Syntax::          The syntax of conditionals.
256 * Testing Flags::               Conditionals that test flags.
258 Functions for Transforming Text
260 * Syntax of Functions::         How to write a function call.
261 * Text Functions::              General-purpose text manipulation functions.
262 * File Name Functions::         Functions for manipulating file names.
263 * Conditional Functions::       Functions that implement conditions.
264 * Foreach Function::            Repeat some text with controlled variation.
265 * Call Function::               Expand a user-defined function.
266 * Value Function::              Return the un-expanded value of a variable.
267 * Eval Function::               Evaluate the arguments as makefile syntax.
268 * Origin Function::             Find where a variable got its value.
269 * Flavor Function::             Find out the flavor of a variable.
270 * Shell Function::              Substitute the output of a shell command.
271 * Make Control Functions::      Functions that control how make runs.
273 How to Run @code{make}
275 * Makefile Arguments::          How to specify which makefile to use.
276 * Goals::                       How to use goal arguments to specify which
277                                   parts of the makefile to use.
278 * Instead of Execution::        How to use mode flags to specify what
279                                   kind of thing to do with the recipes
280                                   in the makefile other than simply
281                                   execute them.
282 * Avoiding Compilation::        How to avoid recompiling certain files.
283 * Overriding::                  How to override a variable to specify
284                                   an alternate compiler and other things.
285 * Testing::                     How to proceed past some errors, to
286                                   test compilation.
287 * Options Summary::             Summary of Options
289 Using Implicit Rules
291 * Using Implicit::              How to use an existing implicit rule
292                                   to get the recipe for updating a file.
293 * Catalogue of Rules::          A list of built-in implicit rules.
294 * Implicit Variables::          How to change what predefined rules do.
295 * Chained Rules::               How to use a chain of implicit rules.
296 * Pattern Rules::               How to define new implicit rules.
297 * Last Resort::                 How to define a recipe for rules which
298                                   cannot find any.
299 * Suffix Rules::                The old-fashioned style of implicit rule.
300 * Implicit Rule Search::        The precise algorithm for applying
301                                   implicit rules.
303 Defining and Redefining Pattern Rules
305 * Pattern Intro::               An introduction to pattern rules.
306 * Pattern Examples::            Examples of pattern rules.
307 * Automatic Variables::         How to use automatic variables in the
308                                   recipe of implicit rules.
309 * Pattern Match::               How patterns match.
310 * Match-Anything Rules::        Precautions you should take prior to
311                                   defining rules that can match any
312                                   target file whatever.
313 * Canceling Rules::             How to override or cancel built-in rules.
315 Using @code{make} to Update Archive Files
317 * Archive Members::             Archive members as targets.
318 * Archive Update::              The implicit rule for archive member targets.
319 * Archive Pitfalls::            Dangers to watch out for when using archives.
320 * Archive Suffix Rules::        You can write a special kind of suffix rule
321                                   for updating archives.
323 Implicit Rule for Archive Member Targets
325 * Archive Symbols::             How to update archive symbol directories.
327 @end detailmenu
328 @end menu
330 @node Overview, Introduction, Top, Top
331 @comment  node-name,  next,  previous,  up
332 @chapter Overview of @code{make}
334 The @code{make} utility automatically determines which pieces of a large
335 program need to be recompiled, and issues commands to recompile them.
336 This manual describes GNU @code{make}, which was implemented by Richard
337 Stallman and Roland McGrath.  Development since Version 3.76 has been
338 handled by Paul D. Smith.
340 GNU @code{make} conforms to section 6.2 of @cite{IEEE Standard
341 1003.2-1992} (POSIX.2).
342 @cindex POSIX
343 @cindex IEEE Standard 1003.2
344 @cindex standards conformance
346 Our examples show C programs, since they are most common, but you can use
347 @code{make} with any programming language whose compiler can be run with a
348 shell command.  Indeed, @code{make} is not limited to programs.  You can
349 use it to describe any task where some files must be updated automatically
350 from others whenever the others change.
352 @menu
353 * Preparing::                   Preparing and Running Make
354 * Reading::                     On Reading this Text
355 * Bugs::                        Problems and Bugs
356 @end menu
358 @node Preparing, Reading, Overview, Overview
359 @ifnottex
360 @heading Preparing and Running Make
361 @end ifnottex
363 To prepare to use @code{make}, you must write a file called
364 the @dfn{makefile} that describes the relationships among files
365 in your program and provides commands for updating each file.
366 In a program, typically, the executable file is updated from object
367 files, which are in turn made by compiling source files.@refill
369 Once a suitable makefile exists, each time you change some source files,
370 this simple shell command:
372 @example
373 make
374 @end example
376 @noindent
377 suffices to perform all necessary recompilations.  The @code{make} program
378 uses the makefile data base and the last-modification times of the files to
379 decide which of the files need to be updated.  For each of those files, it
380 issues the recipes recorded in the data base.
382 You can provide command line arguments to @code{make} to control which
383 files should be recompiled, or how.  @xref{Running, ,How to Run
384 @code{make}}.
386 @node Reading, Bugs, Preparing, Overview
387 @section How to Read This Manual
389 If you are new to @code{make}, or are looking for a general
390 introduction, read the first few sections of each chapter, skipping the
391 later sections.  In each chapter, the first few sections contain
392 introductory or general information and the later sections contain
393 specialized or technical information.
394 @ifnottex
395 The exception is the second chapter, @ref{Introduction, ,An
396 Introduction to Makefiles}, all of which is introductory.
397 @end ifnottex
398 @iftex
399 The exception is @ref{Introduction, ,An Introduction to Makefiles},
400 all of which is introductory.
401 @end iftex
403 If you are familiar with other @code{make} programs, see @ref{Features,
404 ,Features of GNU @code{make}}, which lists the enhancements GNU
405 @code{make} has, and @ref{Missing, ,Incompatibilities and Missing
406 Features}, which explains the few things GNU @code{make} lacks that
407 others have.
409 For a quick summary, see @ref{Options Summary}, @ref{Quick Reference},
410 and @ref{Special Targets}.
412 @node Bugs,  , Reading, Overview
413 @section Problems and Bugs
414 @cindex reporting bugs
415 @cindex bugs, reporting
416 @cindex problems and bugs, reporting
418 If you have problems with GNU @code{make} or think you've found a bug,
419 please report it to the developers; we cannot promise to do anything but
420 we might well want to fix it.
422 Before reporting a bug, make sure you've actually found a real bug.
423 Carefully reread the documentation and see if it really says you can do
424 what you're trying to do.  If it's not clear whether you should be able
425 to do something or not, report that too; it's a bug in the
426 documentation!
428 Before reporting a bug or trying to fix it yourself, try to isolate it
429 to the smallest possible makefile that reproduces the problem.  Then
430 send us the makefile and the exact results @code{make} gave you,
431 including any error or warning messages.  Please don't paraphrase
432 these messages: it's best to cut and paste them into your report.
433 When generating this small makefile, be sure to not use any non-free
434 or unusual tools in your recipes: you can almost always emulate what
435 such a tool would do with simple shell commands.  Finally, be sure to
436 explain what you expected to occur; this will help us decide whether
437 the problem was really in the documentation.
439 Once you have a precise problem you can report it in one of two ways.
440 Either send electronic mail to:
442 @example
443     bug-make@@gnu.org
444 @end example
446 @noindent
447 or use our Web-based project management tool, at:
449 @example
450     http://savannah.gnu.org/projects/make/
451 @end example
453 @noindent
454 In addition to the information above, please be careful to include the
455 version number of @code{make} you are using.  You can get this
456 information with the command @samp{make --version}.  Be sure also to
457 include the type of machine and operating system you are using.  One
458 way to obtain this information is by looking at the final lines of
459 output from the command @samp{make --help}.
461 @node Introduction, Makefiles, Overview, Top
462 @comment  node-name,  next,  previous,  up
463 @chapter An Introduction to Makefiles
465 You need a file called a @dfn{makefile} to tell @code{make} what to do.
466 Most often, the makefile tells @code{make} how to compile and link a
467 program.
468 @cindex makefile
470 In this chapter, we will discuss a simple makefile that describes how to
471 compile and link a text editor which consists of eight C source files
472 and three header files.  The makefile can also tell @code{make} how to
473 run miscellaneous commands when explicitly asked (for example, to remove
474 certain files as a clean-up operation).  To see a more complex example
475 of a makefile, see @ref{Complex Makefile}.
477 When @code{make} recompiles the editor, each changed C source file
478 must be recompiled.  If a header file has changed, each C source file
479 that includes the header file must be recompiled to be safe.  Each
480 compilation produces an object file corresponding to the source file.
481 Finally, if any source file has been recompiled, all the object files,
482 whether newly made or saved from previous compilations, must be linked
483 together to produce the new executable editor.
484 @cindex recompilation
485 @cindex editor
487 @menu
488 * Rule Introduction::           What a rule looks like.
489 * Simple Makefile::             A Simple Makefile
490 * How Make Works::              How @code{make} Processes This Makefile
491 * Variables Simplify::          Variables Make Makefiles Simpler
492 * make Deduces::                Letting @code{make} Deduce the Recipes
493 * Combine By Prerequisite::     Another Style of Makefile
494 * Cleanup::                     Rules for Cleaning the Directory
495 @end menu
497 @node Rule Introduction, Simple Makefile, Introduction, Introduction
498 @comment  node-name,  next,  previous,  up
499 @section What a Rule Looks Like
500 @cindex rule, introduction to
501 @cindex makefile rule parts
502 @cindex parts of makefile rule
504 A simple makefile consists of ``rules'' with the following shape:
506 @cindex targets, introduction to
507 @cindex prerequisites, introduction to
508 @cindex recipes, introduction to
509 @example
510 @group
511 @var{target} @dots{} : @var{prerequisites} @dots{}
512         @var{recipe}
513         @dots{}
514         @dots{}
515 @end group
516 @end example
518 A @dfn{target} is usually the name of a file that is generated by a
519 program; examples of targets are executable or object files.  A target
520 can also be the name of an action to carry out, such as @samp{clean}
521 (@pxref{Phony Targets}).
523 A @dfn{prerequisite} is a file that is used as input to create the
524 target.  A target often depends on several files.
526 @cindex tabs in rules
527 A @dfn{recipe} is an action that @code{make} carries out.
528 A recipe may have more than one command, each on its own line.
529 @strong{Please note:} you need to put a tab character at the beginning of
530 every command line!  This is an obscurity that catches the unwary.  If
531 you prefer to prefix your recipes with a character other than tab,
532 you can set the @code{.CMDPREFIX} variable to an alternate character
533 (@pxref{Special Variables}).
535 Usually a recipe is in a rule with prerequisites and serves to create a
536 target file if any of the prerequisites change.  However, the rule that
537 specifies a recipe for the target need not have prerequisites.  For
538 example, the rule containing the delete command associated with the
539 target @samp{clean} does not have prerequisites.
541 A @dfn{rule}, then, explains how and when to remake certain files
542 which are the targets of the particular rule.  @code{make} carries out
543 the recipe on the prerequisites to create or update the target.  A
544 rule can also explain how and when to carry out an action.
545 @xref{Rules, , Writing Rules}.
547 A makefile may contain other text besides rules, but a simple makefile
548 need only contain rules.  Rules may look somewhat more complicated
549 than shown in this template, but all fit the pattern more or less.
551 @node Simple Makefile, How Make Works, Rule Introduction, Introduction
552 @section A Simple Makefile
553 @cindex simple makefile
554 @cindex makefile, simple
556 Here is a straightforward makefile that describes the way an
557 executable file called @code{edit} depends on eight object files
558 which, in turn, depend on eight C source and three header files.
560 In this example, all the C files include @file{defs.h}, but only those
561 defining editing commands include @file{command.h}, and only low
562 level files that change the editor buffer include @file{buffer.h}.
564 @example
565 @group
566 edit : main.o kbd.o command.o display.o \
567        insert.o search.o files.o utils.o
568         cc -o edit main.o kbd.o command.o display.o \
569                    insert.o search.o files.o utils.o
571 main.o : main.c defs.h
572         cc -c main.c
573 kbd.o : kbd.c defs.h command.h
574         cc -c kbd.c
575 command.o : command.c defs.h command.h
576         cc -c command.c
577 display.o : display.c defs.h buffer.h
578         cc -c display.c
579 insert.o : insert.c defs.h buffer.h
580         cc -c insert.c
581 search.o : search.c defs.h buffer.h
582         cc -c search.c
583 files.o : files.c defs.h buffer.h command.h
584         cc -c files.c
585 utils.o : utils.c defs.h
586         cc -c utils.c
587 clean :
588         rm edit main.o kbd.o command.o display.o \
589            insert.o search.o files.o utils.o
590 @end group
591 @end example
593 @noindent
594 We split each long line into two lines using backslash-newline; this is
595 like using one long line, but is easier to read.
596 @cindex continuation lines
597 @cindex @code{\} (backslash), for continuation lines
598 @cindex backslash (@code{\}), for continuation lines
599 @cindex quoting newline, in makefile
600 @cindex newline, quoting, in makefile
602 To use this makefile to create the executable file called @file{edit},
603 type:
605 @example
606 make
607 @end example
609 To use this makefile to delete the executable file and all the object
610 files from the directory, type:
612 @example
613 make clean
614 @end example
616 In the example makefile, the targets include the executable file
617 @samp{edit}, and the object files @samp{main.o} and @samp{kbd.o}.  The
618 prerequisites are files such as @samp{main.c} and @samp{defs.h}.
619 In fact, each @samp{.o} file is both a target and a prerequisite.
620 Recipes include @w{@samp{cc -c main.c}} and @w{@samp{cc -c kbd.c}}.
622 When a target is a file, it needs to be recompiled or relinked if any
623 of its prerequisites change.  In addition, any prerequisites that are
624 themselves automatically generated should be updated first.  In this
625 example, @file{edit} depends on each of the eight object files; the
626 object file @file{main.o} depends on the source file @file{main.c} and
627 on the header file @file{defs.h}.
629 A recipe may follow each line that contains a target and
630 prerequisites.  These recipes say how to update the target file.  A
631 tab character (or whatever character is specified by the
632 @code{.CMDPREFIX} variable; @pxref{Special Variables}) must come at
633 the beginning of every line in the recipe to distinguish recipes from
634 other lines in the makefile.  (Bear in mind that @code{make} does not
635 know anything about how the recipes work.  It is up to you to supply
636 recipes that will update the target file properly.  All @code{make}
637 does is execute the commands in the recipe you have specified when the
638 target file needs to be updated.)@refill
639 @cindex recipe
641 The target @samp{clean} is not a file, but merely the name of an
642 action.  Since you normally do not want to carry out the actions in
643 this rule, @samp{clean} is not a prerequisite of any other rule.
644 Consequently, @code{make} never does anything with it unless you tell
645 it specifically.  Note that this rule not only is not a prerequisite,
646 it also does not have any prerequisites, so the only purpose of the
647 rule is to run the specified recipe.  Targets that do not refer to
648 files but are just actions are called @dfn{phony targets}.
649 @xref{Phony Targets}, for information about this kind of target.
650 @xref{Errors, , Errors in Recipes}, to see how to cause @code{make}
651 to ignore errors from @code{rm} or any other command.
652 @cindex @code{clean} target
653 @cindex @code{rm} (shell command)
655 @node How Make Works, Variables Simplify, Simple Makefile, Introduction
656 @comment  node-name,  next,  previous,  up
657 @section How @code{make} Processes a Makefile
658 @cindex processing a makefile
659 @cindex makefile, how @code{make} processes
661 By default, @code{make} starts with the first target (not targets whose
662 names start with @samp{.}).  This is called the @dfn{default goal}.
663 (@dfn{Goals} are the targets that @code{make} strives ultimately to
664 update.    You can override this behavior using the command line
665 (@pxref{Goals, , Arguments to Specify the Goals}) or with the
666 @code{.DEFAULT_GOAL} special variable (@pxref{Special Variables, ,
667 Other Special Variables}).
668 @cindex default goal
669 @cindex goal, default
670 @cindex goal
672 In the simple example of the previous section, the default goal is to
673 update the executable program @file{edit}; therefore, we put that rule
674 first.
676 Thus, when you give the command:
678 @example
679 make
680 @end example
682 @noindent
683 @code{make} reads the makefile in the current directory and begins by
684 processing the first rule.  In the example, this rule is for relinking
685 @file{edit}; but before @code{make} can fully process this rule, it
686 must process the rules for the files that @file{edit} depends on,
687 which in this case are the object files.  Each of these files is
688 processed according to its own rule.  These rules say to update each
689 @samp{.o} file by compiling its source file.  The recompilation must
690 be done if the source file, or any of the header files named as
691 prerequisites, is more recent than the object file, or if the object
692 file does not exist.
694 The other rules are processed because their targets appear as
695 prerequisites of the goal.  If some other rule is not depended on by the
696 goal (or anything it depends on, etc.), that rule is not processed,
697 unless you tell @code{make} to do so (with a command such as
698 @w{@code{make clean}}).
700 Before recompiling an object file, @code{make} considers updating its
701 prerequisites, the source file and header files.  This makefile does not
702 specify anything to be done for them---the @samp{.c} and @samp{.h} files
703 are not the targets of any rules---so @code{make} does nothing for these
704 files.  But @code{make} would update automatically generated C programs,
705 such as those made by Bison or Yacc, by their own rules at this time.
707 After recompiling whichever object files need it, @code{make} decides
708 whether to relink @file{edit}.  This must be done if the file
709 @file{edit} does not exist, or if any of the object files are newer than
710 it.  If an object file was just recompiled, it is now newer than
711 @file{edit}, so @file{edit} is relinked.
712 @cindex relinking
714 Thus, if we change the file @file{insert.c} and run @code{make},
715 @code{make} will compile that file to update @file{insert.o}, and then
716 link @file{edit}.  If we change the file @file{command.h} and run
717 @code{make}, @code{make} will recompile the object files @file{kbd.o},
718 @file{command.o} and @file{files.o} and then link the file @file{edit}.
720 @node Variables Simplify, make Deduces, How Make Works, Introduction
721 @section Variables Make Makefiles Simpler
722 @cindex variables
723 @cindex simplifying with variables
725 In our example, we had to list all the object files twice in the rule for
726 @file{edit} (repeated here):
728 @example
729 @group
730 edit : main.o kbd.o command.o display.o \
731               insert.o search.o files.o utils.o
732         cc -o edit main.o kbd.o command.o display.o \
733                    insert.o search.o files.o utils.o
734 @end group
735 @end example
737 @cindex @code{objects}
738 Such duplication is error-prone; if a new object file is added to the
739 system, we might add it to one list and forget the other.  We can eliminate
740 the risk and simplify the makefile by using a variable.  @dfn{Variables}
741 allow a text string to be defined once and substituted in multiple places
742 later (@pxref{Using Variables, ,How to Use Variables}).
744 @cindex @code{OBJECTS}
745 @cindex @code{objs}
746 @cindex @code{OBJS}
747 @cindex @code{obj}
748 @cindex @code{OBJ}
749 It is standard practice for every makefile to have a variable named
750 @code{objects}, @code{OBJECTS}, @code{objs}, @code{OBJS}, @code{obj},
751 or @code{OBJ} which is a list of all object file names.  We would
752 define such a variable @code{objects} with a line like this in the
753 makefile:@refill
755 @example
756 @group
757 objects = main.o kbd.o command.o display.o \
758           insert.o search.o files.o utils.o
759 @end group
760 @end example
762 @noindent
763 Then, each place we want to put a list of the object file names, we can
764 substitute the variable's value by writing @samp{$(objects)}
765 (@pxref{Using Variables, ,How to Use Variables}).
767 Here is how the complete simple makefile looks when you use a variable
768 for the object files:
770 @example
771 @group
772 objects = main.o kbd.o command.o display.o \
773           insert.o search.o files.o utils.o
775 edit : $(objects)
776         cc -o edit $(objects)
777 main.o : main.c defs.h
778         cc -c main.c
779 kbd.o : kbd.c defs.h command.h
780         cc -c kbd.c
781 command.o : command.c defs.h command.h
782         cc -c command.c
783 display.o : display.c defs.h buffer.h
784         cc -c display.c
785 insert.o : insert.c defs.h buffer.h
786         cc -c insert.c
787 search.o : search.c defs.h buffer.h
788         cc -c search.c
789 files.o : files.c defs.h buffer.h command.h
790         cc -c files.c
791 utils.o : utils.c defs.h
792         cc -c utils.c
793 clean :
794         rm edit $(objects)
795 @end group
796 @end example
798 @node make Deduces, Combine By Prerequisite, Variables Simplify, Introduction
799 @section Letting @code{make} Deduce the Recipes
800 @cindex deducing recipes (implicit rules)
801 @cindex implicit rule, introduction to
802 @cindex rule, implicit, introduction to
804 It is not necessary to spell out the recipes for compiling the individual
805 C source files, because @code{make} can figure them out: it has an
806 @dfn{implicit rule} for updating a @samp{.o} file from a correspondingly
807 named @samp{.c} file using a @samp{cc -c} command.  For example, it will
808 use the recipe @samp{cc -c main.c -o main.o} to compile @file{main.c} into
809 @file{main.o}.  We can therefore omit the recipes from the rules for the
810 object files.  @xref{Implicit Rules, ,Using Implicit Rules}.@refill
812 When a @samp{.c} file is used automatically in this way, it is also
813 automatically added to the list of prerequisites.  We can therefore omit
814 the @samp{.c} files from the prerequisites, provided we omit the recipe.
816 Here is the entire example, with both of these changes, and a variable
817 @code{objects} as suggested above:
819 @example
820 @group
821 objects = main.o kbd.o command.o display.o \
822           insert.o search.o files.o utils.o
824 edit : $(objects)
825         cc -o edit $(objects)
827 main.o : defs.h
828 kbd.o : defs.h command.h
829 command.o : defs.h command.h
830 display.o : defs.h buffer.h
831 insert.o : defs.h buffer.h
832 search.o : defs.h buffer.h
833 files.o : defs.h buffer.h command.h
834 utils.o : defs.h
836 .PHONY : clean
837 clean :
838         rm edit $(objects)
839 @end group
840 @end example
842 @noindent
843 This is how we would write the makefile in actual practice.  (The
844 complications associated with @samp{clean} are described elsewhere.
845 See @ref{Phony Targets}, and @ref{Errors, ,Errors in Recipes}.)
847 Because implicit rules are so convenient, they are important.  You
848 will see them used frequently.@refill
850 @node Combine By Prerequisite, Cleanup, make Deduces, Introduction
851 @section Another Style of Makefile
852 @cindex combining rules by prerequisite
854 When the objects of a makefile are created only by implicit rules, an
855 alternative style of makefile is possible.  In this style of makefile,
856 you group entries by their prerequisites instead of by their targets.
857 Here is what one looks like:
859 @example
860 @group
861 objects = main.o kbd.o command.o display.o \
862           insert.o search.o files.o utils.o
864 edit : $(objects)
865         cc -o edit $(objects)
867 $(objects) : defs.h
868 kbd.o command.o files.o : command.h
869 display.o insert.o search.o files.o : buffer.h
870 @end group
871 @end example
873 @noindent
874 Here @file{defs.h} is given as a prerequisite of all the object files;
875 @file{command.h} and @file{buffer.h} are prerequisites of the specific
876 object files listed for them.
878 Whether this is better is a matter of taste: it is more compact, but some
879 people dislike it because they find it clearer to put all the information
880 about each target in one place.
882 @node Cleanup,  , Combine By Prerequisite, Introduction
883 @section Rules for Cleaning the Directory
884 @cindex cleaning up
885 @cindex removing, to clean up
887 Compiling a program is not the only thing you might want to write rules
888 for.  Makefiles commonly tell how to do a few other things besides
889 compiling a program: for example, how to delete all the object files
890 and executables so that the directory is @samp{clean}.
892 @cindex @code{clean} target
893 Here is how we
894 could write a @code{make} rule for cleaning our example editor:
896 @example
897 @group
898 clean:
899         rm edit $(objects)
900 @end group
901 @end example
903 In practice, we might want to write the rule in a somewhat more
904 complicated manner to handle unanticipated situations.  We would do this:
906 @example
907 @group
908 .PHONY : clean
909 clean :
910         -rm edit $(objects)
911 @end group
912 @end example
914 @noindent
915 This prevents @code{make} from getting confused by an actual file
916 called @file{clean} and causes it to continue in spite of errors from
917 @code{rm}.  (See @ref{Phony Targets}, and @ref{Errors, ,Errors in
918 Recipes}.)
920 @noindent
921 A rule such as this should not be placed at the beginning of the
922 makefile, because we do not want it to run by default!  Thus, in the
923 example makefile, we want the rule for @code{edit}, which recompiles
924 the editor, to remain the default goal.
926 Since @code{clean} is not a prerequisite of @code{edit}, this rule will not
927 run at all if we give the command @samp{make} with no arguments.  In
928 order to make the rule run, we have to type @samp{make clean}.
929 @xref{Running, ,How to Run @code{make}}.
931 @node Makefiles, Rules, Introduction, Top
932 @chapter Writing Makefiles
934 @cindex makefile, how to write
935 The information that tells @code{make} how to recompile a system comes from
936 reading a data base called the @dfn{makefile}.
938 @menu
939 * Makefile Contents::           What makefiles contain.
940 * Makefile Names::              How to name your makefile.
941 * Include::                     How one makefile can use another makefile.
942 * MAKEFILES Variable::          The environment can specify extra makefiles.
943 * Remaking Makefiles::          How makefiles get remade.
944 * Overriding Makefiles::        How to override part of one makefile
945                                   with another makefile.
946 * Reading Makefiles::           How makefiles are parsed.
947 * Secondary Expansion::         How and when secondary expansion is performed.
948 @end menu
950 @node Makefile Contents, Makefile Names, Makefiles, Makefiles
951 @section What Makefiles Contain
953 Makefiles contain five kinds of things: @dfn{explicit rules},
954 @dfn{implicit rules}, @dfn{variable definitions}, @dfn{directives},
955 and @dfn{comments}.  Rules, variables, and directives are described at
956 length in later chapters.@refill
958 @itemize @bullet
959 @cindex rule, explicit, definition of
960 @cindex explicit rule, definition of
961 @item
962 An @dfn{explicit rule} says when and how to remake one or more files,
963 called the rule's @dfn{targets}.  It lists the other files that the
964 targets depend on, called the @dfn{prerequisites} of the target, and
965 may also give a recipe to use to create or update the targets.
966 @xref{Rules, ,Writing Rules}.
968 @cindex rule, implicit, definition of
969 @cindex implicit rule, definition of
970 @item
971 An @dfn{implicit rule} says when and how to remake a class of files
972 based on their names.  It describes how a target may depend on a file
973 with a name similar to the target and gives a recipe to create or
974 update such a target.  @xref{Implicit Rules, ,Using Implicit Rules}.
976 @cindex variable definition
977 @item
978 A @dfn{variable definition} is a line that specifies a text string
979 value for a variable that can be substituted into the text later.  The
980 simple makefile example shows a variable definition for @code{objects}
981 as a list of all object files (@pxref{Variables Simplify, , Variables
982 Make Makefiles Simpler}).
984 @cindex directive
985 @item
986 A @dfn{directive} is an instruction for @code{make} to do something
987 special while reading the makefile.  These include:
989 @itemize @bullet
990 @item
991 Reading another makefile (@pxref{Include, ,Including Other Makefiles}).
993 @item
994 Deciding (based on the values of variables) whether to use or
995 ignore a part of the makefile (@pxref{Conditionals, ,Conditional Parts of Makefiles}).
997 @item
998 Defining a variable from a verbatim string containing multiple lines
999 (@pxref{Multi-Line, ,Defining Multi-Line Variables}).
1000 @end itemize
1002 @cindex comments, in makefile
1003 @cindex @code{#} (comments), in makefile
1004 @item
1005 @samp{#} in a line of a makefile starts a @dfn{comment}.  It and the
1006 rest of the line are ignored, except that a trailing backslash not
1007 escaped by another backslash will continue the comment across multiple
1008 lines.  A line containing just a comment (with perhaps spaces before
1009 it) is effectively blank, and is ignored.  If you want a literal
1010 @code{#}, escape it with a backslash (e.g., @code{\#}).  Comments may
1011 appear on any line in the makefile, although they are treated
1012 specially in certain situations.
1014 You cannot use comments within variable references or function calls:
1015 any instance of @code{#} will be treated literally (rather than as the
1016 start of a comment) inside a variable reference or function call.
1018 Comments within a recipe are passed to the shell, just as with any
1019 other recipe text.  The shell decides how to interpret it: whether or
1020 not this is a comment is up to the shell.
1022 Within a @code{define} directive, comments are not ignored during the
1023 definition of the variable, but rather kept intact in the value of the
1024 variable.  When the variable is expanded they will either be treated
1025 as @code{make} comments or as recipe text, depending on the context in
1026 which the variable is evaluated.
1027 @end itemize
1029 @node Makefile Names, Include, Makefile Contents, Makefiles
1030 @section What Name to Give Your Makefile
1031 @cindex makefile name
1032 @cindex name of makefile
1033 @cindex default makefile name
1034 @cindex file name of makefile
1036 @c following paragraph rewritten to avoid overfull hbox
1037 By default, when @code{make} looks for the makefile, it tries the
1038 following names, in order: @file{GNUmakefile}, @file{makefile}
1039 and @file{Makefile}.@refill
1040 @findex Makefile
1041 @findex GNUmakefile
1042 @findex makefile
1044 @cindex @code{README}
1045 Normally you should call your makefile either @file{makefile} or
1046 @file{Makefile}.  (We recommend @file{Makefile} because it appears
1047 prominently near the beginning of a directory listing, right near other
1048 important files such as @file{README}.)  The first name checked,
1049 @file{GNUmakefile}, is not recommended for most makefiles.  You should
1050 use this name if you have a makefile that is specific to GNU
1051 @code{make}, and will not be understood by other versions of
1052 @code{make}.  Other @code{make} programs look for @file{makefile} and
1053 @file{Makefile}, but not @file{GNUmakefile}.
1055 If @code{make} finds none of these names, it does not use any makefile.
1056 Then you must specify a goal with a command argument, and @code{make}
1057 will attempt to figure out how to remake it using only its built-in
1058 implicit rules.  @xref{Implicit Rules, ,Using Implicit Rules}.
1060 @cindex @code{-f}
1061 @cindex @code{--file}
1062 @cindex @code{--makefile}
1063 If you want to use a nonstandard name for your makefile, you can specify
1064 the makefile name with the @samp{-f} or @samp{--file} option.  The
1065 arguments @w{@samp{-f @var{name}}} or @w{@samp{--file=@var{name}}} tell
1066 @code{make} to read the file @var{name} as the makefile.  If you use
1067 more than one @samp{-f} or @samp{--file} option, you can specify several
1068 makefiles.  All the makefiles are effectively concatenated in the order
1069 specified.  The default makefile names @file{GNUmakefile},
1070 @file{makefile} and @file{Makefile} are not checked automatically if you
1071 specify @samp{-f} or @samp{--file}.@refill
1072 @cindex specifying makefile name
1073 @cindex makefile name, how to specify
1074 @cindex name of makefile, how to specify
1075 @cindex file name of makefile, how to specify
1077 @node Include, MAKEFILES Variable, Makefile Names, Makefiles
1078 @section Including Other Makefiles
1079 @cindex including other makefiles
1080 @cindex makefile, including
1082 @findex include
1083 The @code{include} directive tells @code{make} to suspend reading the
1084 current makefile and read one or more other makefiles before continuing.
1085 The directive is a line in the makefile that looks like this:
1087 @example
1088 include @var{filenames}@dots{}
1089 @end example
1091 @noindent
1092 @var{filenames} can contain shell file name patterns.  If
1093 @var{filenames} is empty, nothing is included and no error is printed.
1094 @cindex shell file name pattern (in @code{include})
1095 @cindex shell wildcards (in @code{include})
1096 @cindex wildcard, in @code{include}
1098 Extra spaces are allowed and ignored at the beginning of the line, but
1099 the first character must not be a tab (or the value of
1100 @code{.CMDPREFIX})---if the line begins with a tab, it will be
1101 considered a recipe line.  Whitespace is required between
1102 @code{include} and the file names, and between file names; extra
1103 whitespace is ignored there and at the end of the directive.  A
1104 comment starting with @samp{#} is allowed at the end of the line.  If
1105 the file names contain any variable or function references, they are
1106 expanded.  @xref{Using Variables, ,How to Use Variables}.
1108 For example, if you have three @file{.mk} files, @file{a.mk},
1109 @file{b.mk}, and @file{c.mk}, and @code{$(bar)} expands to
1110 @code{bish bash}, then the following expression
1112 @example
1113 include foo *.mk $(bar)
1114 @end example
1116 is equivalent to
1118 @example
1119 include foo a.mk b.mk c.mk bish bash
1120 @end example
1122 When @code{make} processes an @code{include} directive, it suspends
1123 reading of the containing makefile and reads from each listed file in
1124 turn.  When that is finished, @code{make} resumes reading the
1125 makefile in which the directive appears.
1127 One occasion for using @code{include} directives is when several programs,
1128 handled by individual makefiles in various directories, need to use a
1129 common set of variable definitions
1130 (@pxref{Setting, ,Setting Variables}) or pattern rules
1131 (@pxref{Pattern Rules, ,Defining and Redefining Pattern Rules}).
1133 Another such occasion is when you want to generate prerequisites from
1134 source files automatically; the prerequisites can be put in a file that
1135 is included by the main makefile.  This practice is generally cleaner
1136 than that of somehow appending the prerequisites to the end of the main
1137 makefile as has been traditionally done with other versions of
1138 @code{make}.  @xref{Automatic Prerequisites}.
1139 @cindex prerequisites, automatic generation
1140 @cindex automatic generation of prerequisites
1141 @cindex generating prerequisites automatically
1143 @cindex @code{-I}
1144 @cindex @code{--include-dir}
1145 @cindex included makefiles, default directories
1146 @cindex default directories for included makefiles
1147 @findex /usr/gnu/include
1148 @findex /usr/local/include
1149 @findex /usr/include
1150 If the specified name does not start with a slash, and the file is not
1151 found in the current directory, several other directories are searched.
1152 First, any directories you have specified with the @samp{-I} or
1153 @samp{--include-dir} option are searched
1154 (@pxref{Options Summary, ,Summary of Options}).
1155 Then the following directories (if they exist)
1156 are searched, in this order:
1157 @file{@var{prefix}/include} (normally @file{/usr/local/include}
1158 @footnote{GNU Make compiled for MS-DOS and MS-Windows behaves as if
1159 @var{prefix} has been defined to be the root of the DJGPP tree
1160 hierarchy.})
1161 @file{/usr/gnu/include},
1162 @file{/usr/local/include}, @file{/usr/include}.
1164 If an included makefile cannot be found in any of these directories, a
1165 warning message is generated, but it is not an immediately fatal error;
1166 processing of the makefile containing the @code{include} continues.
1167 Once it has finished reading makefiles, @code{make} will try to remake
1168 any that are out of date or don't exist.
1169 @xref{Remaking Makefiles, ,How Makefiles Are Remade}.
1170 Only after it has tried to find a way to remake a makefile and failed,
1171 will @code{make} diagnose the missing makefile as a fatal error.
1173 If you want @code{make} to simply ignore a makefile which does not exist
1174 and cannot be remade, with no error message, use the @w{@code{-include}}
1175 directive instead of @code{include}, like this:
1177 @example
1178 -include @var{filenames}@dots{}
1179 @end example
1181 This acts like @code{include} in every way except that there is no
1182 error (not even a warning) if any of the @var{filenames} do not exist.
1183 For compatibility with some other @code{make} implementations,
1184 @code{sinclude} is another name for @w{@code{-include}}.
1186 @node MAKEFILES Variable, Remaking Makefiles, Include, Makefiles
1187 @section The Variable @code{MAKEFILES}
1188 @cindex makefile, and @code{MAKEFILES} variable
1189 @cindex including (@code{MAKEFILES} variable)
1191 @vindex MAKEFILES
1192 If the environment variable @code{MAKEFILES} is defined, @code{make}
1193 considers its value as a list of names (separated by whitespace) of
1194 additional makefiles to be read before the others.  This works much like
1195 the @code{include} directive: various directories are searched for those
1196 files (@pxref{Include, ,Including Other Makefiles}).  In addition, the
1197 default goal is never taken from one of these makefiles and it is not an
1198 error if the files listed in @code{MAKEFILES} are not found.@refill
1200 @cindex recursion, and @code{MAKEFILES} variable
1201 The main use of @code{MAKEFILES} is in communication between recursive
1202 invocations of @code{make} (@pxref{Recursion, ,Recursive Use of
1203 @code{make}}).  It usually is not desirable to set the environment
1204 variable before a top-level invocation of @code{make}, because it is
1205 usually better not to mess with a makefile from outside.  However, if
1206 you are running @code{make} without a specific makefile, a makefile in
1207 @code{MAKEFILES} can do useful things to help the built-in implicit
1208 rules work better, such as defining search paths (@pxref{Directory Search}).
1210 Some users are tempted to set @code{MAKEFILES} in the environment
1211 automatically on login, and program makefiles to expect this to be done.
1212 This is a very bad idea, because such makefiles will fail to work if run by
1213 anyone else.  It is much better to write explicit @code{include} directives
1214 in the makefiles.  @xref{Include, , Including Other Makefiles}.
1216 @node Remaking Makefiles, Overriding Makefiles, MAKEFILES Variable, Makefiles
1217 @section How Makefiles Are Remade
1219 @cindex updating makefiles
1220 @cindex remaking makefiles
1221 @cindex makefile, remaking of
1222 Sometimes makefiles can be remade from other files, such as RCS or SCCS
1223 files.  If a makefile can be remade from other files, you probably want
1224 @code{make} to get an up-to-date version of the makefile to read in.
1226 To this end, after reading in all makefiles, @code{make} will consider
1227 each as a goal target and attempt to update it.  If a makefile has a
1228 rule which says how to update it (found either in that very makefile or
1229 in another one) or if an implicit rule applies to it (@pxref{Implicit
1230 Rules, ,Using Implicit Rules}), it will be updated if necessary.  After
1231 all makefiles have been checked, if any have actually been changed,
1232 @code{make} starts with a clean slate and reads all the makefiles over
1233 again.  (It will also attempt to update each of them over again, but
1234 normally this will not change them again, since they are already up to
1235 date.)@refill
1237 If you know that one or more of your makefiles cannot be remade and
1238 you want to keep @code{make} from performing an implicit rule search
1239 on them, perhaps for efficiency reasons, you can use any normal method
1240 of preventing implicit rule lookup to do so.  For example, you can
1241 write an explicit rule with the makefile as the target, and an empty
1242 recipe (@pxref{Empty Recipes, ,Using Empty Recipes}).
1244 If the makefiles specify a double-colon rule to remake a file with
1245 a recipe but no prerequisites, that file will always be remade
1246 (@pxref{Double-Colon}).  In the case of makefiles, a makefile that has a
1247 double-colon rule with a recipe but no prerequisites will be remade every
1248 time @code{make} is run, and then again after @code{make} starts over
1249 and reads the makefiles in again.  This would cause an infinite loop:
1250 @code{make} would constantly remake the makefile, and never do anything
1251 else.  So, to avoid this, @code{make} will @strong{not} attempt to
1252 remake makefiles which are specified as targets of a double-colon rule
1253 with a recipe but no prerequisites.@refill
1255 If you do not specify any makefiles to be read with @samp{-f} or
1256 @samp{--file} options, @code{make} will try the default makefile names;
1257 @pxref{Makefile Names, ,What Name to Give Your Makefile}.  Unlike
1258 makefiles explicitly requested with @samp{-f} or @samp{--file} options,
1259 @code{make} is not certain that these makefiles should exist.  However,
1260 if a default makefile does not exist but can be created by running
1261 @code{make} rules, you probably want the rules to be run so that the
1262 makefile can be used.
1264 Therefore, if none of the default makefiles exists, @code{make} will try
1265 to make each of them in the same order in which they are searched for
1266 (@pxref{Makefile Names, ,What Name to Give Your Makefile})
1267 until it succeeds in making one, or it runs out of names to try.  Note
1268 that it is not an error if @code{make} cannot find or make any makefile;
1269 a makefile is not always necessary.@refill
1271 When you use the @samp{-t} or @samp{--touch} option
1272 (@pxref{Instead of Execution, ,Instead of Executing Recipes}),
1273 you would not want to use an out-of-date makefile to decide which
1274 targets to touch.  So the @samp{-t} option has no effect on updating
1275 makefiles; they are really updated even if @samp{-t} is specified.
1276 Likewise, @samp{-q} (or @samp{--question}) and @samp{-n} (or
1277 @samp{--just-print}) do not prevent updating of makefiles, because an
1278 out-of-date makefile would result in the wrong output for other targets.
1279 Thus, @samp{make -f mfile -n foo} will update @file{mfile}, read it in,
1280 and then print the recipe to update @file{foo} and its prerequisites
1281 without running it.  The recipe printed for @file{foo} will be the one
1282 specified in the updated contents of @file{mfile}.
1284 However, on occasion you might actually wish to prevent updating of even
1285 the makefiles.  You can do this by specifying the makefiles as goals in
1286 the command line as well as specifying them as makefiles.  When the
1287 makefile name is specified explicitly as a goal, the options @samp{-t}
1288 and so on do apply to them.
1290 Thus, @samp{make -f mfile -n mfile foo} would read the makefile
1291 @file{mfile}, print the recipe needed to update it without actually
1292 running it, and then print the recipe needed to update @file{foo}
1293 without running that.  The recipe for @file{foo} will be the one
1294 specified by the existing contents of @file{mfile}.
1296 @node Overriding Makefiles, Reading Makefiles, Remaking Makefiles, Makefiles
1297 @section Overriding Part of Another Makefile
1299 @cindex overriding makefiles
1300 @cindex makefile, overriding
1301 Sometimes it is useful to have a makefile that is mostly just like
1302 another makefile.  You can often use the @samp{include} directive to
1303 include one in the other, and add more targets or variable definitions.
1304 However, it is illegal for two makefiles to give different recipes for
1305 the same target.  But there is another way.
1307 @cindex match-anything rule, used to override
1308 In the containing makefile (the one that wants to include the other),
1309 you can use a match-anything pattern rule to say that to remake any
1310 target that cannot be made from the information in the containing
1311 makefile, @code{make} should look in another makefile.
1312 @xref{Pattern Rules}, for more information on pattern rules.
1314 For example, if you have a makefile called @file{Makefile} that says how
1315 to make the target @samp{foo} (and other targets), you can write a
1316 makefile called @file{GNUmakefile} that contains:
1318 @example
1319 foo:
1320         frobnicate > foo
1322 %: force
1323         @@$(MAKE) -f Makefile $@@
1324 force: ;
1325 @end example
1327 If you say @samp{make foo}, @code{make} will find @file{GNUmakefile},
1328 read it, and see that to make @file{foo}, it needs to run the recipe
1329 @samp{frobnicate > foo}.  If you say @samp{make bar}, @code{make} will
1330 find no way to make @file{bar} in @file{GNUmakefile}, so it will use the
1331 recipe from the pattern rule: @samp{make -f Makefile bar}.  If
1332 @file{Makefile} provides a rule for updating @file{bar}, @code{make}
1333 will apply the rule.  And likewise for any other target that
1334 @file{GNUmakefile} does not say how to make.
1336 The way this works is that the pattern rule has a pattern of just
1337 @samp{%}, so it matches any target whatever.  The rule specifies a
1338 prerequisite @file{force}, to guarantee that the recipe will be run even
1339 if the target file already exists.  We give the @file{force} target an
1340 empty recipe to prevent @code{make} from searching for an implicit rule to
1341 build it---otherwise it would apply the same match-anything rule to
1342 @file{force} itself and create a prerequisite loop!
1344 @node Reading Makefiles,  Secondary Expansion, Overriding Makefiles, Makefiles
1345 @section How @code{make} Reads a Makefile
1346 @cindex reading makefiles
1347 @cindex makefile, parsing
1349 GNU @code{make} does its work in two distinct phases.  During the first
1350 phase it reads all the makefiles, included makefiles, etc. and
1351 internalizes all the variables and their values, implicit and explicit
1352 rules, and constructs a dependency graph of all the targets and their
1353 prerequisites.  During the second phase, @code{make} uses these internal
1354 structures to determine what targets will need to be rebuilt and to
1355 invoke the rules necessary to do so.
1357 It's important to understand this two-phase approach because it has a
1358 direct impact on how variable and function expansion happens; this is
1359 often a source of some confusion when writing makefiles.  Here we will
1360 present a summary of the phases in which expansion happens for different
1361 constructs within the makefile.  We say that expansion is
1362 @dfn{immediate} if it happens during the first phase: in this case
1363 @code{make} will expand any variables or functions in that section of a
1364 construct as the makefile is parsed.  We say that expansion is
1365 @dfn{deferred} if expansion is not performed immediately.  Expansion of
1366 a deferred construct is not performed until either the construct appears
1367 later in an immediate context, or until the second phase.
1369 You may not be familiar with some of these constructs yet.  You can
1370 reference this section as you become familiar with them, in later
1371 chapters.
1373 @subheading Variable Assignment
1374 @cindex +=, expansion
1375 @cindex =, expansion
1376 @cindex ?=, expansion
1377 @cindex +=, expansion
1378 @cindex define, expansion
1380 Variable definitions are parsed as follows:
1382 @example
1383 @var{immediate} = @var{deferred}
1384 @var{immediate} ?= @var{deferred}
1385 @var{immediate} := @var{immediate}
1386 @var{immediate} += @var{deferred} or @var{immediate}
1388 define @var{immediate}
1389   @var{deferred}
1390 endef
1392 define @var{immediate} =
1393   @var{deferred}
1394 endef
1396 define @var{immediate} ?=
1397   @var{deferred}
1398 endef
1400 define @var{immediate} :=
1401   @var{immediate}
1402 endef
1404 define @var{immediate} +=
1405   @var{deferred} or @var{immediate}
1406 endef
1407 @end example
1409 For the append operator, @samp{+=}, the right-hand side is considered
1410 immediate if the variable was previously set as a simple variable
1411 (@samp{:=}), and deferred otherwise.
1413 @subheading Conditional Directives
1414 @cindex ifdef, expansion
1415 @cindex ifeq, expansion
1416 @cindex ifndef, expansion
1417 @cindex ifneq, expansion
1419 Conditional directives are parsed immediately.  This means, for
1420 example, that automatic variables cannot be used in conditional
1421 directives, as automatic variables are not set until the recipe for
1422 that rule is invoked.  If you need to use automatic variables in a
1423 conditional directive you @emph{must} move the condition into the
1424 recipe and use shell conditional syntax instead.
1426 @subheading Rule Definition
1427 @cindex target, expansion
1428 @cindex prerequisite, expansion
1429 @cindex implicit rule, expansion
1430 @cindex pattern rule, expansion
1431 @cindex explicit rule, expansion
1433 A rule is always expanded the same way, regardless of the form:
1435 @example
1436 @var{immediate} : @var{immediate} ; @var{deferred}
1437         @var{deferred}
1438 @end example
1440 That is, the target and prerequisite sections are expanded immediately,
1441 and the recipe used to construct the target is always deferred.  This
1442 general rule is true for explicit rules, pattern rules, suffix rules,
1443 static pattern rules, and simple prerequisite definitions.
1445 @node Secondary Expansion, , Reading Makefiles, Makefiles
1446 @section Secondary Expansion
1447 @cindex secondary expansion
1448 @cindex expansion, secondary
1450 @findex .SECONDEXPANSION
1451 In the previous section we learned that GNU @code{make} works in two
1452 distinct phases: a read-in phase and a target-update phase
1453 (@pxref{Reading Makefiles, , How @code{make} Reads a Makefile}).  GNU
1454 make also has the ability to enable a @emph{second expansion} of the
1455 prerequisites (only) for some or all targets defined in the makefile.
1456 In order for this second expansion to occur, the special target
1457 @code{.SECONDEXPANSION} must be defined before the first prerequisite
1458 list that makes use of this feature.
1460 If that special target is defined then in between the two phases
1461 mentioned above, right at the end of the read-in phase, all the
1462 prerequisites of the targets defined after the special target are
1463 expanded a @emph{second time}.  In most circumstances this secondary
1464 expansion will have no effect, since all variable and function
1465 references will have been expanded during the initial parsing of the
1466 makefiles.  In order to take advantage of the secondary expansion
1467 phase of the parser, then, it's necessary to @emph{escape} the
1468 variable or function reference in the makefile.  In this case the
1469 first expansion merely un-escapes the reference but doesn't expand it,
1470 and expansion is left to the secondary expansion phase.  For example,
1471 consider this makefile:
1473 @example
1474 .SECONDEXPANSION:
1475 ONEVAR = onefile
1476 TWOVAR = twofile
1477 myfile: $(ONEVAR) $$(TWOVAR)
1478 @end example
1480 After the first expansion phase the prerequisites list of the
1481 @file{myfile} target will be @code{onefile} and @code{$(TWOVAR)}; the
1482 first (unescaped) variable reference to @var{ONEVAR} is expanded,
1483 while the second (escaped) variable reference is simply unescaped,
1484 without being recognized as a variable reference.  Now during the
1485 secondary expansion the first word is expanded again but since it
1486 contains no variable or function references it remains the static
1487 value @file{onefile}, while the second word is now a normal reference
1488 to the variable @var{TWOVAR}, which is expanded to the value
1489 @file{twofile}.  The final result is that there are two prerequisites,
1490 @file{onefile} and @file{twofile}.
1492 Obviously, this is not a very interesting case since the same result
1493 could more easily have been achieved simply by having both variables
1494 appear, unescaped, in the prerequisites list.  One difference becomes
1495 apparent if the variables are reset; consider this example:
1497 @example
1498 .SECONDEXPANSION:
1499 AVAR = top
1500 onefile: $(AVAR)
1501 twofile: $$(AVAR)
1502 AVAR = bottom
1503 @end example
1505 Here the prerequisite of @file{onefile} will be expanded immediately,
1506 and resolve to the value @file{top}, while the prerequisite of
1507 @file{twofile} will not be full expanded until the secondary expansion
1508 and yield a value of @file{bottom}.
1510 This is marginally more exciting, but the true power of this feature
1511 only becomes apparent when you discover that secondary expansions
1512 always take place within the scope of the automatic variables for that
1513 target.  This means that you can use variables such as @code{$@@},
1514 @code{$*}, etc. during the second expansion and they will have their
1515 expected values, just as in the recipe.  All you have to do is defer
1516 the expansion by escaping the @code{$}.  Also, secondary expansion
1517 occurs for both explicit and implicit (pattern) rules.  Knowing this,
1518 the possible uses for this feature increase dramatically.  For
1519 example:
1521 @example
1522 .SECONDEXPANSION:
1523 main_OBJS := main.o try.o test.o
1524 lib_OBJS := lib.o api.o
1526 main lib: $$($$@@_OBJS)
1527 @end example
1529 Here, after the initial expansion the prerequisites of both the
1530 @file{main} and @file{lib} targets will be @code{$($@@_OBJS)}.  During
1531 the secondary expansion, the @code{$@@} variable is set to the name of
1532 the target and so the expansion for the @file{main} target will yield
1533 @code{$(main_OBJS)}, or @code{main.o try.o test.o}, while the
1534 secondary expansion for the @file{lib} target will yield
1535 @code{$(lib_OBJS)}, or @code{lib.o api.o}.
1537 You can also mix in functions here, as long as they are properly escaped:
1539 @example
1540 main_SRCS := main.c try.c test.c
1541 lib_SRCS := lib.c api.c
1543 .SECONDEXPANSION:
1544 main lib: $$(patsubst %.c,%.o,$$($$@@_SRCS))
1545 @end example
1547 This version allows users to specify source files rather than object
1548 files, but gives the same resulting prerequisites list as the previous
1549 example.
1551 Evaluation of automatic variables during the secondary expansion
1552 phase, especially of the target name variable @code{$$@@}, behaves
1553 similarly to evaluation within recipes.  However, there are some
1554 subtle differences and ``corner cases'' which come into play for the
1555 different types of rule definitions that @code{make} understands.  The
1556 subtleties of using the different automatic variables are described
1557 below.
1559 @subheading Secondary Expansion of Explicit Rules
1560 @cindex secondary expansion and explicit rules
1561 @cindex explicit rules, secondary expansion of
1563 During the secondary expansion of explicit rules, @code{$$@@} and
1564 @code{$$%} evaluate, respectively, to the file name of the target and,
1565 when the target is an archive member, the target member name.  The
1566 @code{$$<} variable evaluates to the first prerequisite in the first
1567 rule for this target.  @code{$$^} and @code{$$+} evaluate to the list
1568 of all prerequisites of rules @emph{that have already appeared} for
1569 the same target (@code{$$+} with repetitions and @code{$$^}
1570 without).  The following example will help illustrate these behaviors:
1572 @example
1573 .SECONDEXPANSION:
1575 foo: foo.1 bar.1 $$< $$^ $$+    # line #1
1577 foo: foo.2 bar.2 $$< $$^ $$+    # line #2
1579 foo: foo.3 bar.3 $$< $$^ $$+    # line #3
1580 @end example
1582 In the first prerequisite list, all three variables (@code{$$<},
1583 @code{$$^}, and @code{$$+}) expand to the empty string.  In the
1584 second, they will have values @code{foo.1}, @code{foo.1 bar.1}, and
1585 @code{foo.1 bar.1} respectively.  In the third they will have values
1586 @code{foo.1}, @code{foo.1 bar.1 foo.2 bar.2}, and @code{foo.1 bar.1
1587 foo.2 bar.2 foo.1 foo.1 bar.1 foo.1 bar.1} respectively.
1589 Rules undergo secondary expansion in makefile order, except that
1590 the rule with the recipe is always evaluated last.
1592 The variables @code{$$?} and @code{$$*} are not available and expand
1593 to the empty string.
1595 @subheading Secondary Expansion of Static Pattern Rules
1596 @cindex secondary expansion and static pattern rules
1597 @cindex static pattern rules, secondary expansion of
1599 Rules for secondary expansion of static pattern rules are identical to
1600 those for explicit rules, above, with one exception: for static
1601 pattern rules the @code{$$*} variable is set to the pattern stem.  As
1602 with explicit rules, @code{$$?} is not available and expands to the
1603 empty string.
1605 @subheading Secondary Expansion of Implicit Rules
1606 @cindex secondary expansion and implicit rules
1607 @cindex implicit rules, secondary expansion of
1609 As @code{make} searches for an implicit rule, it substitutes the stem
1610 and then performs secondary expansion for every rule with a matching
1611 target pattern.  The value of the automatic variables is derived in
1612 the same fashion as for static pattern rules.  As an example:
1614 @example
1615 .SECONDEXPANSION:
1617 foo: bar
1619 foo foz: fo%: bo%
1621 %oo: $$< $$^ $$+ $$*
1622 @end example
1624 When the implicit rule is tried for target @file{foo}, @code{$$<}
1625 expands to @file{bar}, @code{$$^} expands to @file{bar boo},
1626 @code{$$+} also expands to @file{bar boo}, and @code{$$*} expands to
1627 @file{f}.
1629 Note that the directory prefix (D), as described in @ref{Implicit Rule
1630 Search, ,Implicit Rule Search Algorithm}, is appended (after
1631 expansion) to all the patterns in the prerequisites list.  As an
1632 example:
1634 @example
1635 .SECONDEXPANSION:
1637 /tmp/foo.o:
1639 %.o: $$(addsuffix /%.c,foo bar) foo.h
1640 @end example
1642 The prerequisite list after the secondary expansion and directory
1643 prefix reconstruction will be @file{/tmp/foo/foo.c /tmp/var/bar/foo.c
1644 foo.h}.  If you are not interested in this reconstruction, you can use
1645 @code{$$*} instead of @code{%} in the prerequisites list.
1647 @node Rules, Recipes, Makefiles, Top
1648 @chapter Writing Rules
1649 @cindex writing rules
1650 @cindex rule, how to write
1651 @cindex target
1652 @cindex prerequisite
1654 A @dfn{rule} appears in the makefile and says when and how to remake
1655 certain files, called the rule's @dfn{targets} (most often only one per rule).
1656 It lists the other files that are the @dfn{prerequisites} of the target, and
1657 the @dfn{recipe} to use to create or update the target.
1659 @cindex default goal
1660 @cindex goal, default
1661 The order of rules is not significant, except for determining the
1662 @dfn{default goal}: the target for @code{make} to consider, if you do
1663 not otherwise specify one.  The default goal is the target of the first
1664 rule in the first makefile.  If the first rule has multiple targets,
1665 only the first target is taken as the default.  There are two
1666 exceptions: a target starting with a period is not a default unless it
1667 contains one or more slashes, @samp{/}, as well; and, a target that
1668 defines a pattern rule has no effect on the default goal.
1669 (@xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.)
1671 Therefore, we usually write the makefile so that the first rule is the
1672 one for compiling the entire program or all the programs described by
1673 the makefile (often with a target called @samp{all}).
1674 @xref{Goals, ,Arguments to Specify the Goals}.
1676 @menu
1677 * Rule Example::                An example explained.
1678 * Rule Syntax::                 General syntax explained.
1679 * Prerequisite Types::          There are two types of prerequisites.
1680 * Wildcards::                   Using wildcard characters such as `*'.
1681 * Directory Search::            Searching other directories for source files.
1682 * Phony Targets::               Using a target that is not a real file's name.
1683 * Force Targets::               You can use a target without recipes
1684                                   or prerequisites to mark other targets
1685                                   as phony.
1686 * Empty Targets::               When only the date matters and the
1687                                   files are empty.
1688 * Special Targets::             Targets with special built-in meanings.
1689 * Multiple Targets::            When to make use of several targets in a rule.
1690 * Multiple Rules::              How to use several rules with the same target.
1691 * Static Pattern::              Static pattern rules apply to multiple targets
1692                                   and can vary the prerequisites according to
1693                                   the target name.
1694 * Double-Colon::                How to use a special kind of rule to allow
1695                                   several independent rules for one target.
1696 * Automatic Prerequisites::     How to automatically generate rules giving
1697                                   prerequisites from source files themselves.
1698 @end menu
1700 @ifnottex
1701 @node Rule Example, Rule Syntax, Rules, Rules
1702 @section Rule Example
1704 Here is an example of a rule:
1706 @example
1707 foo.o : foo.c defs.h       # module for twiddling the frobs
1708         cc -c -g foo.c
1709 @end example
1711 Its target is @file{foo.o} and its prerequisites are @file{foo.c} and
1712 @file{defs.h}.  It has one command in the recipe: @samp{cc -c -g foo.c}.
1713 The recipe starts with a tab to identify it as a recipe.
1715 This rule says two things:
1717 @itemize @bullet
1718 @item
1719 How to decide whether @file{foo.o} is out of date: it is out of date
1720 if it does not exist, or if either @file{foo.c} or @file{defs.h} is
1721 more recent than it.
1723 @item
1724 How to update the file @file{foo.o}: by running @code{cc} as stated.
1725 The recipe does not explicitly mention @file{defs.h}, but we presume
1726 that @file{foo.c} includes it, and that that is why @file{defs.h} was
1727 added to the prerequisites.
1728 @end itemize
1729 @end ifnottex
1731 @node Rule Syntax, Prerequisite Types, Rule Example, Rules
1732 @section Rule Syntax
1734 @cindex rule syntax
1735 @cindex syntax of rules
1736 In general, a rule looks like this:
1738 @example
1739 @var{targets} : @var{prerequisites}
1740         @var{recipe}
1741         @dots{}
1742 @end example
1744 @noindent
1745 or like this:
1747 @example
1748 @var{targets} : @var{prerequisites} ; @var{recipe}
1749         @var{recipe}
1750         @dots{}
1751 @end example
1753 @cindex targets
1754 @cindex rule targets
1755 The @var{targets} are file names, separated by spaces.  Wildcard
1756 characters may be used (@pxref{Wildcards, ,Using Wildcard Characters
1757 in File Names}) and a name of the form @file{@var{a}(@var{m})}
1758 represents member @var{m} in archive file @var{a}
1759 (@pxref{Archive Members, ,Archive Members as Targets}).
1760 Usually there is only one
1761 target per rule, but occasionally there is a reason to have more
1762 (@pxref{Multiple Targets, , Multiple Targets in a Rule}).@refill
1764 @cindex recipes
1765 @cindex tab character (in commands)
1766 The @var{recipe} lines start with a tab character (or the first
1767 character in the value of the @code{.CMDPREFIX} variable;
1768 @pxref{Special Variables}).  The first recipe line may appear on the line
1769 after the prerequisites, with a tab character, or may appear on the
1770 same line, with a semicolon.  Either way, the effect is the same.
1771 There are other differences in the syntax of recipes.
1772 @xref{Recipes, ,Writing Recipes in Rules}.
1774 @cindex dollar sign (@code{$}), in rules
1775 @cindex @code{$}, in rules
1776 @cindex rules, and @code{$}
1777 Because dollar signs are used to start @code{make} variable
1778 references, if you really want a dollar sign in a target or
1779 prerequisite you must write two of them, @samp{$$} (@pxref{Using
1780 Variables, ,How to Use Variables}).  If you have enabled secondary
1781 expansion (@pxref{Secondary Expansion}) and you want a literal dollar
1782 sign in the prerequisites list, you must actually write @emph{four}
1783 dollar signs (@samp{$$$$}).
1785 You may split a long line by inserting a backslash followed by a
1786 newline, but this is not required, as @code{make} places no limit on
1787 the length of a line in a makefile.
1789 A rule tells @code{make} two things: when the targets are out of date,
1790 and how to update them when necessary.
1792 @cindex prerequisites
1793 @cindex rule prerequisites
1794 The criterion for being out of date is specified in terms of the
1795 @var{prerequisites}, which consist of file names separated by spaces.
1796 (Wildcards and archive members (@pxref{Archives}) are allowed here too.)
1797 A target is out of date if it does not exist or if it is older than any
1798 of the prerequisites (by comparison of last-modification times).  The
1799 idea is that the contents of the target file are computed based on
1800 information in the prerequisites, so if any of the prerequisites changes,
1801 the contents of the existing target file are no longer necessarily
1802 valid.
1804 How to update is specified by a @var{recipe}.  This is one or more
1805 lines to be executed by the shell (normally @samp{sh}), but with some
1806 extra features (@pxref{Recipes, ,Writing Recipes in Rules}).
1808 @node Prerequisite Types, Wildcards, Rule Syntax, Rules
1809 @comment  node-name,  next,  previous,  up
1810 @section Types of Prerequisites
1811 @cindex prerequisite types
1812 @cindex types of prerequisites
1814 @cindex prerequisites, normal
1815 @cindex normal prerequisites
1816 @cindex prerequisites, order-only
1817 @cindex order-only prerequisites
1818 There are actually two different types of prerequisites understood by
1819 GNU @code{make}: normal prerequisites such as described in the
1820 previous section, and @dfn{order-only} prerequisites.  A normal
1821 prerequisite makes two statements: first, it imposes an order of
1822 execution of recipes: any recipes necessary to build any of a
1823 target's prerequisites will be fully executed before any recipe
1824 necessary to build the target.  Second, it imposes a dependency
1825 relationship: if any prerequisite is newer than the target, then the
1826 target is considered out-of-date and must be rebuilt.
1828 Normally, this is exactly what you want: if a target's prerequisite is
1829 updated, then the target should also be updated.
1831 Occasionally, however, you have a situation where you want to impose a
1832 specific ordering on the rules to be invoked @emph{without} forcing
1833 the target to be updated if one of those rules is executed.  In that
1834 case, you want to define @dfn{order-only} prerequisites.  Order-only
1835 prerequisites can be specified by placing a pipe symbol (@code{|})
1836 in the prerequisites list: any prerequisites to the left of the pipe
1837 symbol are normal; any prerequisites to the right are order-only:
1839 @example
1840 @var{targets} : @var{normal-prerequisites} | @var{order-only-prerequisites}
1841 @end example
1843 The normal prerequisites section may of course be empty.  Also, you
1844 may still declare multiple lines of prerequisites for the same target:
1845 they are appended appropriately (normal prerequisites are appended to
1846 the list of normal prerequisites; order-only prerequisites are
1847 appended to the list of order-only prerequisites).  Note that if you
1848 declare the same file to be both a normal and an order-only
1849 prerequisite, the normal prerequisite takes precedence (since they
1850 have a strict superset of the behavior of an order-only prerequisite).
1852 Consider an example where your targets are to be placed in a separate
1853 directory, and that directory might not exist before @code{make} is
1854 run.  In this situation, you want the directory to be created before
1855 any targets are placed into it but, because the timestamps on
1856 directories change whenever a file is added, removed, or renamed, we
1857 certainly don't want to rebuild all the targets whenever the
1858 directory's timestamp changes.  One way to manage this is with
1859 order-only prerequisites: make the directory an order-only
1860 prerequisite on all the targets:
1862 @example
1863 OBJDIR := objdir
1864 OBJS := $(addprefix $(OBJDIR)/,foo.o bar.o baz.o)
1866 $(OBJDIR)/%.o : %.c
1867         $(COMPILE.c) $(OUTPUT_OPTION) $<
1869 all: $(OBJS)
1871 $(OBJS): | $(OBJDIR)
1873 $(OBJDIR):
1874         mkdir $(OBJDIR)
1875 @end example
1877 Now the rule to create the @file{objdir} directory will be run, if
1878 needed, before any @samp{.o} is built, but no @samp{.o} will be built
1879 because the @file{objdir} directory timestamp changed.
1881 @node Wildcards, Directory Search, Prerequisite Types, Rules
1882 @section Using Wildcard Characters in File Names
1883 @cindex wildcard
1884 @cindex file name with wildcards
1885 @cindex globbing (wildcards)
1887 @cindex @code{*} (wildcard character)
1888 @cindex @code{?} (wildcard character)
1889 @cindex @code{[@dots{}]} (wildcard characters)
1890 A single file name can specify many files using @dfn{wildcard characters}.
1891 The wildcard characters in @code{make} are @samp{*}, @samp{?} and
1892 @samp{[@dots{}]}, the same as in the Bourne shell.  For example, @file{*.c}
1893 specifies a list of all the files (in the working directory) whose names
1894 end in @samp{.c}.@refill
1896 @cindex @code{~} (tilde)
1897 @cindex tilde (@code{~})
1898 @cindex home directory
1899 The character @samp{~} at the beginning of a file name also has special
1900 significance.  If alone, or followed by a slash, it represents your home
1901 directory.  For example @file{~/bin} expands to @file{/home/you/bin}.
1902 If the @samp{~} is followed by a word, the string represents the home
1903 directory of the user named by that word.  For example @file{~john/bin}
1904 expands to @file{/home/john/bin}.  On systems which don't have a home
1905 directory for each user (such as MS-DOS or MS-Windows), this
1906 functionality can be simulated by setting the environment variable
1907 @var{HOME}.@refill
1909 Wildcard expansion is performed by @code{make} automatically in
1910 targets and in prerequisites.  In recipes, the shell is responsible
1911 for wildcard expansion.  In other contexts, wildcard expansion happens
1912 only if you request it explicitly with the @code{wildcard} function.
1914 The special significance of a wildcard character can be turned off by
1915 preceding it with a backslash.  Thus, @file{foo\*bar} would refer to a
1916 specific file whose name consists of @samp{foo}, an asterisk, and
1917 @samp{bar}.@refill
1919 @menu
1920 * Wildcard Examples::           Several examples
1921 * Wildcard Pitfall::            Problems to avoid.
1922 * Wildcard Function::           How to cause wildcard expansion where
1923                                   it does not normally take place.
1924 @end menu
1926 @node Wildcard Examples, Wildcard Pitfall, Wildcards, Wildcards
1927 @subsection Wildcard Examples
1929 Wildcards can be used in the recipe of a rule, where they are expanded
1930 by the shell.  For example, here is a rule to delete all the object files:
1932 @example
1933 @group
1934 clean:
1935         rm -f *.o
1936 @end group
1937 @end example
1938 @cindex @code{rm} (shell command)
1940 Wildcards are also useful in the prerequisites of a rule.  With the
1941 following rule in the makefile, @samp{make print} will print all the
1942 @samp{.c} files that have changed since the last time you printed them:
1944 @example
1945 print: *.c
1946         lpr -p $?
1947         touch print
1948 @end example
1950 @cindex @code{print} target
1951 @cindex @code{lpr} (shell command)
1952 @cindex @code{touch} (shell command)
1953 @noindent
1954 This rule uses @file{print} as an empty target file; see @ref{Empty
1955 Targets, ,Empty Target Files to Record Events}.  (The automatic variable
1956 @samp{$?} is used to print only those files that have changed; see
1957 @ref{Automatic Variables}.)@refill
1959 Wildcard expansion does not happen when you define a variable.  Thus, if
1960 you write this:
1962 @example
1963 objects = *.o
1964 @end example
1966 @noindent
1967 then the value of the variable @code{objects} is the actual string
1968 @samp{*.o}.  However, if you use the value of @code{objects} in a target,
1969 prerequisite, or recipe, wildcard expansion will take place at that time.
1970 To set @code{objects} to the expansion, instead use:
1972 @example
1973 objects := $(wildcard *.o)
1974 @end example
1976 @noindent
1977 @xref{Wildcard Function}.
1979 @node Wildcard Pitfall, Wildcard Function, Wildcard Examples, Wildcards
1980 @subsection Pitfalls of Using Wildcards
1981 @cindex wildcard pitfalls
1982 @cindex pitfalls of wildcards
1983 @cindex mistakes with wildcards
1984 @cindex errors with wildcards
1985 @cindex problems with wildcards
1987 Now here is an example of a naive way of using wildcard expansion, that
1988 does not do what you would intend.  Suppose you would like to say that the
1989 executable file @file{foo} is made from all the object files in the
1990 directory, and you write this:
1992 @example
1993 objects = *.o
1995 foo : $(objects)
1996         cc -o foo $(CFLAGS) $(objects)
1997 @end example
1999 @noindent
2000 The value of @code{objects} is the actual string @samp{*.o}.  Wildcard
2001 expansion happens in the rule for @file{foo}, so that each @emph{existing}
2002 @samp{.o} file becomes a prerequisite of @file{foo} and will be recompiled if
2003 necessary.
2005 But what if you delete all the @samp{.o} files?  When a wildcard matches
2006 no files, it is left as it is, so then @file{foo} will depend on the
2007 oddly-named file @file{*.o}.  Since no such file is likely to exist,
2008 @code{make} will give you an error saying it cannot figure out how to
2009 make @file{*.o}.  This is not what you want!
2011 Actually it is possible to obtain the desired result with wildcard
2012 expansion, but you need more sophisticated techniques, including the
2013 @code{wildcard} function and string substitution.
2014 @ifnottex
2015 @xref{Wildcard Function, ,The Function @code{wildcard}}.
2016 @end ifnottex
2017 @iftex
2018 These are described in the following section.
2019 @end iftex
2021 @cindex wildcards and MS-DOS/MS-Windows backslashes
2022 @cindex backslashes in pathnames and wildcard expansion
2024 Microsoft operating systems (MS-DOS and MS-Windows) use backslashes to
2025 separate directories in pathnames, like so:
2027 @example
2028   c:\foo\bar\baz.c
2029 @end example
2031 This is equivalent to the Unix-style @file{c:/foo/bar/baz.c} (the
2032 @file{c:} part is the so-called drive letter).  When @code{make} runs on
2033 these systems, it supports backslashes as well as the Unix-style forward
2034 slashes in pathnames.  However, this support does @emph{not} include the
2035 wildcard expansion, where backslash is a quote character.  Therefore,
2036 you @emph{must} use Unix-style slashes in these cases.
2039 @node Wildcard Function,  , Wildcard Pitfall, Wildcards
2040 @subsection The Function @code{wildcard}
2041 @findex wildcard
2043 Wildcard expansion happens automatically in rules.  But wildcard expansion
2044 does not normally take place when a variable is set, or inside the
2045 arguments of a function.  If you want to do wildcard expansion in such
2046 places, you need to use the @code{wildcard} function, like this:
2048 @example
2049 $(wildcard @var{pattern}@dots{})
2050 @end example
2052 @noindent
2053 This string, used anywhere in a makefile, is replaced by a
2054 space-separated list of names of existing files that match one of the
2055 given file name patterns.  If no existing file name matches a pattern,
2056 then that pattern is omitted from the output of the @code{wildcard}
2057 function.  Note that this is different from how unmatched wildcards
2058 behave in rules, where they are used verbatim rather than ignored
2059 (@pxref{Wildcard Pitfall}).
2061 One use of the @code{wildcard} function is to get a list of all the C source
2062 files in a directory, like this:
2064 @example
2065 $(wildcard *.c)
2066 @end example
2068 We can change the list of C source files into a list of object files by
2069 replacing the @samp{.c} suffix with @samp{.o} in the result, like this:
2071 @example
2072 $(patsubst %.c,%.o,$(wildcard *.c))
2073 @end example
2075 @noindent
2076 (Here we have used another function, @code{patsubst}.
2077 @xref{Text Functions, ,Functions for String Substitution and Analysis}.)@refill
2079 Thus, a makefile to compile all C source files in the directory and then
2080 link them together could be written as follows:
2082 @example
2083 objects := $(patsubst %.c,%.o,$(wildcard *.c))
2085 foo : $(objects)
2086         cc -o foo $(objects)
2087 @end example
2089 @noindent
2090 (This takes advantage of the implicit rule for compiling C programs, so
2091 there is no need to write explicit rules for compiling the files.
2092 @xref{Flavors, ,The Two Flavors of Variables}, for an explanation of
2093 @samp{:=}, which is a variant of @samp{=}.)
2095 @node Directory Search, Phony Targets, Wildcards, Rules
2096 @section Searching Directories for Prerequisites
2097 @vindex VPATH
2098 @findex vpath
2099 @cindex vpath
2100 @cindex search path for prerequisites (@code{VPATH})
2101 @cindex directory search (@code{VPATH})
2103 For large systems, it is often desirable to put sources in a separate
2104 directory from the binaries.  The @dfn{directory search} features of
2105 @code{make} facilitate this by searching several directories
2106 automatically to find a prerequisite.  When you redistribute the files
2107 among directories, you do not need to change the individual rules,
2108 just the search paths.
2110 @menu
2111 * General Search::              Specifying a search path that applies
2112                                   to every prerequisite.
2113 * Selective Search::            Specifying a search path
2114                                   for a specified class of names.
2115 * Search Algorithm::            When and how search paths are applied.
2116 * Recipes/Search::             How to write recipes that work together
2117                                   with search paths.
2118 * Implicit/Search::             How search paths affect implicit rules.
2119 * Libraries/Search::            Directory search for link libraries.
2120 @end menu
2122 @node General Search, Selective Search, Directory Search, Directory Search
2123 @subsection @code{VPATH}: Search Path for All Prerequisites
2124 @vindex VPATH
2126 The value of the @code{make} variable @code{VPATH} specifies a list of
2127 directories that @code{make} should search.  Most often, the
2128 directories are expected to contain prerequisite files that are not in the
2129 current directory; however, @code{make} uses @code{VPATH} as a search
2130 list for both prerequisites and targets of rules.
2132 Thus, if a file that is listed as a target or prerequisite does not exist
2133 in the current directory, @code{make} searches the directories listed in
2134 @code{VPATH} for a file with that name.  If a file is found in one of
2135 them, that file may become the prerequisite (see below).  Rules may then
2136 specify the names of files in the prerequisite list as if they all
2137 existed in the current directory.  @xref{Recipes/Search, ,Writing Recipes with Directory Search}.
2139 In the @code{VPATH} variable, directory names are separated by colons or
2140 blanks.  The order in which directories are listed is the order followed
2141 by @code{make} in its search.  (On MS-DOS and MS-Windows, semi-colons
2142 are used as separators of directory names in @code{VPATH}, since the
2143 colon can be used in the pathname itself, after the drive letter.)
2145 For example,
2147 @example
2148 VPATH = src:../headers
2149 @end example
2151 @noindent
2152 specifies a path containing two directories, @file{src} and
2153 @file{../headers}, which @code{make} searches in that order.
2155 With this value of @code{VPATH}, the following rule,
2157 @example
2158 foo.o : foo.c
2159 @end example
2161 @noindent
2162 is interpreted as if it were written like this:
2164 @example
2165 foo.o : src/foo.c
2166 @end example
2168 @noindent
2169 assuming the file @file{foo.c} does not exist in the current directory but
2170 is found in the directory @file{src}.
2172 @node Selective Search, Search Algorithm, General Search, Directory Search
2173 @subsection The @code{vpath} Directive
2174 @findex vpath
2176 Similar to the @code{VPATH} variable, but more selective, is the
2177 @code{vpath} directive (note lower case), which allows you to specify a
2178 search path for a particular class of file names: those that match a
2179 particular pattern.  Thus you can supply certain search directories for
2180 one class of file names and other directories (or none) for other file
2181 names.
2183 There are three forms of the @code{vpath} directive:
2185 @table @code
2186 @item vpath @var{pattern} @var{directories}
2187 Specify the search path @var{directories} for file names that match
2188 @var{pattern}.
2190 The search path, @var{directories}, is a list of directories to be
2191 searched, separated by colons (semi-colons on MS-DOS and MS-Windows) or
2192 blanks, just like the search path used in the @code{VPATH} variable.
2194 @item vpath @var{pattern}
2195 Clear out the search path associated with @var{pattern}.
2197 @c Extra blank line makes sure this gets two lines.
2198 @item vpath
2200 Clear all search paths previously specified with @code{vpath} directives.
2201 @end table
2203 A @code{vpath} pattern is a string containing a @samp{%} character.  The
2204 string must match the file name of a prerequisite that is being searched
2205 for, the @samp{%} character matching any sequence of zero or more
2206 characters (as in pattern rules; @pxref{Pattern Rules, ,Defining and
2207 Redefining Pattern Rules}).  For example, @code{%.h} matches files that
2208 end in @code{.h}.  (If there is no @samp{%}, the pattern must match the
2209 prerequisite exactly, which is not useful very often.)
2211 @cindex @code{%}, quoting in @code{vpath}
2212 @cindex @code{%}, quoting with @code{\} (backslash)
2213 @cindex @code{\} (backslash), to quote @code{%}
2214 @cindex backslash (@code{\}), to quote @code{%}
2215 @cindex quoting @code{%}, in @code{vpath}
2216 @samp{%} characters in a @code{vpath} directive's pattern can be quoted
2217 with preceding backslashes (@samp{\}).  Backslashes that would otherwise
2218 quote @samp{%} characters can be quoted with more backslashes.
2219 Backslashes that quote @samp{%} characters or other backslashes are
2220 removed from the pattern before it is compared to file names.  Backslashes
2221 that are not in danger of quoting @samp{%} characters go unmolested.@refill
2223 When a prerequisite fails to exist in the current directory, if the
2224 @var{pattern} in a @code{vpath} directive matches the name of the
2225 prerequisite file, then the @var{directories} in that directive are searched
2226 just like (and before) the directories in the @code{VPATH} variable.
2228 For example,
2230 @example
2231 vpath %.h ../headers
2232 @end example
2234 @noindent
2235 tells @code{make} to look for any prerequisite whose name ends in @file{.h}
2236 in the directory @file{../headers} if the file is not found in the current
2237 directory.
2239 If several @code{vpath} patterns match the prerequisite file's name, then
2240 @code{make} processes each matching @code{vpath} directive one by one,
2241 searching all the directories mentioned in each directive.  @code{make}
2242 handles multiple @code{vpath} directives in the order in which they
2243 appear in the makefile; multiple directives with the same pattern are
2244 independent of each other.
2246 @need 750
2247 Thus,
2249 @example
2250 @group
2251 vpath %.c foo
2252 vpath %   blish
2253 vpath %.c bar
2254 @end group
2255 @end example
2257 @noindent
2258 will look for a file ending in @samp{.c} in @file{foo}, then
2259 @file{blish}, then @file{bar}, while
2261 @example
2262 @group
2263 vpath %.c foo:bar
2264 vpath %   blish
2265 @end group
2266 @end example
2268 @noindent
2269 will look for a file ending in @samp{.c} in @file{foo}, then
2270 @file{bar}, then @file{blish}.
2272 @node Search Algorithm, Recipes/Search, Selective Search, Directory Search
2273 @subsection How Directory Searches are Performed
2274 @cindex algorithm for directory search
2275 @cindex directory search algorithm
2277 When a prerequisite is found through directory search, regardless of type
2278 (general or selective), the pathname located may not be the one that
2279 @code{make} actually provides you in the prerequisite list.  Sometimes
2280 the path discovered through directory search is thrown away.
2282 The algorithm @code{make} uses to decide whether to keep or abandon a
2283 path found via directory search is as follows:
2285 @enumerate
2286 @item
2287 If a target file does not exist at the path specified in the makefile,
2288 directory search is performed.
2290 @item
2291 If the directory search is successful, that path is kept and this file
2292 is tentatively stored as the target.
2294 @item
2295 All prerequisites of this target are examined using this same method.
2297 @item
2298 After processing the prerequisites, the target may or may not need to be
2299 rebuilt:
2301 @enumerate a
2302 @item
2303 If the target does @emph{not} need to be rebuilt, the path to the file
2304 found during directory search is used for any prerequisite lists which
2305 contain this target.  In short, if @code{make} doesn't need to rebuild
2306 the target then you use the path found via directory search.
2308 @item
2309 If the target @emph{does} need to be rebuilt (is out-of-date), the
2310 pathname found during directory search is @emph{thrown away}, and the
2311 target is rebuilt using the file name specified in the makefile.  In
2312 short, if @code{make} must rebuild, then the target is rebuilt locally,
2313 not in the directory found via directory search.
2314 @end enumerate
2315 @end enumerate
2317 This algorithm may seem complex, but in practice it is quite often
2318 exactly what you want.
2320 @cindex traditional directory search (GPATH)
2321 @cindex directory search, traditional (GPATH)
2322 Other versions of @code{make} use a simpler algorithm: if the file does
2323 not exist, and it is found via directory search, then that pathname is
2324 always used whether or not the target needs to be built.  Thus, if the
2325 target is rebuilt it is created at the pathname discovered during
2326 directory search.
2328 @vindex GPATH
2329 If, in fact, this is the behavior you want for some or all of your
2330 directories, you can use the @code{GPATH} variable to indicate this to
2331 @code{make}.
2333 @code{GPATH} has the same syntax and format as @code{VPATH} (that is, a
2334 space- or colon-delimited list of pathnames).  If an out-of-date target
2335 is found by directory search in a directory that also appears in
2336 @code{GPATH}, then that pathname is not thrown away.  The target is
2337 rebuilt using the expanded path.
2339 @node Recipes/Search, Implicit/Search, Search Algorithm, Directory Search
2340 @subsection Writing Recipes with Directory Search
2341 @cindex recipes, and directory search
2342 @cindex directory search (@code{VPATH}), and recipes
2344 When a prerequisite is found in another directory through directory search,
2345 this cannot change the recipe of the rule; they will execute as written.
2346 Therefore, you must write the recipe with care so that it will look for
2347 the prerequisite in the directory where @code{make} finds it.
2349 This is done with the @dfn{automatic variables} such as @samp{$^}
2350 (@pxref{Automatic Variables}).
2351 For instance, the value of @samp{$^} is a
2352 list of all the prerequisites of the rule, including the names of
2353 the directories in which they were found, and the value of
2354 @samp{$@@} is the target.  Thus:@refill
2356 @example
2357 foo.o : foo.c
2358         cc -c $(CFLAGS) $^ -o $@@
2359 @end example
2361 @noindent
2362 (The variable @code{CFLAGS} exists so you can specify flags for C
2363 compilation by implicit rules; we use it here for consistency so it will
2364 affect all C compilations uniformly;
2365 @pxref{Implicit Variables, ,Variables Used by Implicit Rules}.)
2367 Often the prerequisites include header files as well, which you do not
2368 want to mention in the recipe.  The automatic variable @samp{$<} is
2369 just the first prerequisite:
2371 @example
2372 VPATH = src:../headers
2373 foo.o : foo.c defs.h hack.h
2374         cc -c $(CFLAGS) $< -o $@@
2375 @end example
2377 @node Implicit/Search, Libraries/Search, Recipes/Search, Directory Search
2378 @subsection Directory Search and Implicit Rules
2379 @cindex @code{VPATH}, and implicit rules
2380 @cindex directory search (@code{VPATH}), and implicit rules
2381 @cindex search path for prerequisites (@code{VPATH}), and implicit rules
2382 @cindex implicit rule, and directory search
2383 @cindex implicit rule, and @code{VPATH}
2384 @cindex rule, implicit, and directory search
2385 @cindex rule, implicit, and @code{VPATH}
2387 The search through the directories specified in @code{VPATH} or with
2388 @code{vpath} also happens during consideration of implicit rules
2389 (@pxref{Implicit Rules, ,Using Implicit Rules}).
2391 For example, when a file @file{foo.o} has no explicit rule, @code{make}
2392 considers implicit rules, such as the built-in rule to compile
2393 @file{foo.c} if that file exists.  If such a file is lacking in the
2394 current directory, the appropriate directories are searched for it.  If
2395 @file{foo.c} exists (or is mentioned in the makefile) in any of the
2396 directories, the implicit rule for C compilation is applied.
2398 The recipes of implicit rules normally use automatic variables as a
2399 matter of necessity; consequently they will use the file names found by
2400 directory search with no extra effort.
2402 @node Libraries/Search,  , Implicit/Search, Directory Search
2403 @subsection Directory Search for Link Libraries
2404 @cindex link libraries, and directory search
2405 @cindex libraries for linking, directory search
2406 @cindex directory search (@code{VPATH}), and link libraries
2407 @cindex @code{VPATH}, and link libraries
2408 @cindex search path for prerequisites (@code{VPATH}), and link libraries
2409 @cindex @code{-l} (library search)
2410 @cindex link libraries, patterns matching
2411 @cindex @code{.LIBPATTERNS}, and link libraries
2412 @vindex .LIBPATTERNS
2414 Directory search applies in a special way to libraries used with the
2415 linker.  This special feature comes into play when you write a prerequisite
2416 whose name is of the form @samp{-l@var{name}}.  (You can tell something
2417 strange is going on here because the prerequisite is normally the name of a
2418 file, and the @emph{file name} of a library generally looks like
2419 @file{lib@var{name}.a}, not like @samp{-l@var{name}}.)@refill
2421 When a prerequisite's name has the form @samp{-l@var{name}}, @code{make}
2422 handles it specially by searching for the file @file{lib@var{name}.so} in
2423 the current directory, in directories specified by matching @code{vpath}
2424 search paths and the @code{VPATH} search path, and then in the
2425 directories @file{/lib}, @file{/usr/lib}, and @file{@var{prefix}/lib}
2426 (normally @file{/usr/local/lib}, but MS-DOS/MS-Windows versions of
2427 @code{make} behave as if @var{prefix} is defined to be the root of the
2428 DJGPP installation tree).
2430 If that file is not found, then the file @file{lib@var{name}.a} is
2431 searched for, in the same directories as above.
2433 For example, if there is a @file{/usr/lib/libcurses.a} library on your
2434 system (and no @file{/usr/lib/libcurses.so} file), then
2436 @example
2437 @group
2438 foo : foo.c -lcurses
2439         cc $^ -o $@@
2440 @end group
2441 @end example
2443 @noindent
2444 would cause the command @samp{cc foo.c /usr/lib/libcurses.a -o foo} to
2445 be executed when @file{foo} is older than @file{foo.c} or than
2446 @file{/usr/lib/libcurses.a}.@refill
2448 Although the default set of files to be searched for is
2449 @file{lib@var{name}.so} and @file{lib@var{name}.a}, this is customizable
2450 via the @code{.LIBPATTERNS} variable.  Each word in the value of this
2451 variable is a pattern string.  When a prerequisite like
2452 @samp{-l@var{name}} is seen, @code{make} will replace the percent in
2453 each pattern in the list with @var{name} and perform the above directory
2454 searches using that library filename.  If no library is found, the next
2455 word in the list will be used.
2457 The default value for @code{.LIBPATTERNS} is @samp{lib%.so lib%.a},
2458 which provides the default behavior described above.
2460 You can turn off link library expansion completely by setting this
2461 variable to an empty value.
2463 @node Phony Targets, Force Targets, Directory Search, Rules
2464 @section Phony Targets
2465 @cindex phony targets
2466 @cindex targets, phony
2467 @cindex targets without a file
2469 A phony target is one that is not really the name of a file; rather it
2470 is just a name for a recipe to be executed when you make an explicit
2471 request.  There are two reasons to use a phony target: to avoid a
2472 conflict with a file of the same name, and to improve performance.
2474 If you write a rule whose recipe will not create the target file, the
2475 recipe will be executed every time the target comes up for remaking.
2476 Here is an example:
2478 @example
2479 @group
2480 clean:
2481         rm *.o temp
2482 @end group
2483 @end example
2485 @noindent
2486 Because the @code{rm} command does not create a file named @file{clean},
2487 probably no such file will ever exist.  Therefore, the @code{rm} command
2488 will be executed every time you say @samp{make clean}.
2489 @cindex @code{rm} (shell command)
2491 @findex .PHONY
2492 The phony target will cease to work if anything ever does create a file
2493 named @file{clean} in this directory.  Since it has no prerequisites, the
2494 file @file{clean} would inevitably be considered up to date, and its
2495 recipe would not be executed.  To avoid this problem, you can explicitly
2496 declare the target to be phony, using the special target @code{.PHONY}
2497 (@pxref{Special Targets, ,Special Built-in Target Names}) as follows:
2499 @example
2500 .PHONY : clean
2501 @end example
2503 @noindent
2504 Once this is done, @samp{make clean} will run the recipe regardless of
2505 whether there is a file named @file{clean}.
2507 Since it knows that phony targets do not name actual files that could be
2508 remade from other files, @code{make} skips the implicit rule search for
2509 phony targets (@pxref{Implicit Rules}).  This is why declaring a target
2510 phony is good for performance, even if you are not worried about the
2511 actual file existing.
2513 Thus, you first write the line that states that @code{clean} is a
2514 phony target, then you write the rule, like this:
2516 @example
2517 @group
2518 .PHONY: clean
2519 clean:
2520         rm *.o temp
2521 @end group
2522 @end example
2524 Another example of the usefulness of phony targets is in conjunction
2525 with recursive invocations of @code{make} (for more information, see
2526 @ref{Recursion, ,Recursive Use of @code{make}}).  In this case the
2527 makefile will often contain a variable which lists a number of
2528 subdirectories to be built.  One way to handle this is with one rule
2529 whose recipe is a shell loop over the subdirectories, like this:
2531 @example
2532 @group
2533 SUBDIRS = foo bar baz
2535 subdirs:
2536         for dir in $(SUBDIRS); do \
2537           $(MAKE) -C $$dir; \
2538         done
2539 @end group
2540 @end example
2542 There are problems with this method, however.  First, any error
2543 detected in a submake is ignored by this rule, so it will continue
2544 to build the rest of the directories even when one fails.  This can be
2545 overcome by adding shell commands to note the error and exit, but then
2546 it will do so even if @code{make} is invoked with the @code{-k}
2547 option, which is unfortunate.  Second, and perhaps more importantly,
2548 you cannot take advantage of @code{make}'s ability to build targets in
2549 parallel (@pxref{Parallel, ,Parallel Execution}), since there is only
2550 one rule.
2552 By declaring the subdirectories as phony targets (you must do this as
2553 the subdirectory obviously always exists; otherwise it won't be built)
2554 you can remove these problems:
2556 @example
2557 @group
2558 SUBDIRS = foo bar baz
2560 .PHONY: subdirs $(SUBDIRS)
2562 subdirs: $(SUBDIRS)
2564 $(SUBDIRS):
2565         $(MAKE) -C $@@
2567 foo: baz
2568 @end group
2569 @end example
2571 Here we've also declared that the @file{foo} subdirectory cannot be
2572 built until after the @file{baz} subdirectory is complete; this kind of
2573 relationship declaration is particularly important when attempting
2574 parallel builds.
2576 A phony target should not be a prerequisite of a real target file; if it
2577 is, its recipe will be run every time @code{make} goes to update that
2578 file.  As long as a phony target is never a prerequisite of a real
2579 target, the phony target recipe will be executed only when the phony
2580 target is a specified goal (@pxref{Goals, ,Arguments to Specify the
2581 Goals}).
2583 Phony targets can have prerequisites.  When one directory contains multiple
2584 programs, it is most convenient to describe all of the programs in one
2585 makefile @file{./Makefile}.  Since the target remade by default will be the
2586 first one in the makefile, it is common to make this a phony target named
2587 @samp{all} and give it, as prerequisites, all the individual programs.  For
2588 example:
2590 @example
2591 all : prog1 prog2 prog3
2592 .PHONY : all
2594 prog1 : prog1.o utils.o
2595         cc -o prog1 prog1.o utils.o
2597 prog2 : prog2.o
2598         cc -o prog2 prog2.o
2600 prog3 : prog3.o sort.o utils.o
2601         cc -o prog3 prog3.o sort.o utils.o
2602 @end example
2604 @noindent
2605 Now you can say just @samp{make} to remake all three programs, or
2606 specify as arguments the ones to remake (as in @samp{make prog1
2607 prog3}).  Phoniness is not inherited: the prerequisites of a phony
2608 target are not themselves phony, unless explicitly declared to be so.
2610 When one phony target is a prerequisite of another, it serves as a subroutine
2611 of the other.  For example, here @samp{make cleanall} will delete the
2612 object files, the difference files, and the file @file{program}:
2614 @example
2615 .PHONY: cleanall cleanobj cleandiff
2617 cleanall : cleanobj cleandiff
2618         rm program
2620 cleanobj :
2621         rm *.o
2623 cleandiff :
2624         rm *.diff
2625 @end example
2627 @node Force Targets, Empty Targets, Phony Targets, Rules
2628 @section Rules without Recipes or Prerequisites
2629 @cindex force targets
2630 @cindex targets, force
2631 @cindex @code{FORCE}
2632 @cindex rule, no recipe or prerequisites
2634 If a rule has no prerequisites or recipe, and the target of the rule
2635 is a nonexistent file, then @code{make} imagines this target to have
2636 been updated whenever its rule is run.  This implies that all targets
2637 depending on this one will always have their recipe run.
2639 An example will illustrate this:
2641 @example
2642 @group
2643 clean: FORCE
2644         rm $(objects)
2645 FORCE:
2646 @end group
2647 @end example
2649 Here the target @samp{FORCE} satisfies the special conditions, so the
2650 target @file{clean} that depends on it is forced to run its recipe.
2651 There is nothing special about the name @samp{FORCE}, but that is one
2652 name commonly used this way.
2654 As you can see, using @samp{FORCE} this way has the same results as using
2655 @samp{.PHONY: clean}.
2657 Using @samp{.PHONY} is more explicit and more efficient.  However,
2658 other versions of @code{make} do not support @samp{.PHONY}; thus
2659 @samp{FORCE} appears in many makefiles.  @xref{Phony Targets}.
2661 @node Empty Targets, Special Targets, Force Targets, Rules
2662 @section Empty Target Files to Record Events
2663 @cindex empty targets
2664 @cindex targets, empty
2665 @cindex recording events with empty targets
2667 The @dfn{empty target} is a variant of the phony target; it is used to hold
2668 recipes for an action that you request explicitly from time to time.
2669 Unlike a phony target, this target file can really exist; but the file's
2670 contents do not matter, and usually are empty.
2672 The purpose of the empty target file is to record, with its
2673 last-modification time, when the rule's recipe was last executed.  It
2674 does so because one of the commands in the recipe is a @code{touch}
2675 command to update the target file.
2677 The empty target file should have some prerequisites (otherwise it
2678 doesn't make sense).  When you ask to remake the empty target, the
2679 recipe is executed if any prerequisite is more recent than the target;
2680 in other words, if a prerequisite has changed since the last time you
2681 remade the target.  Here is an example:
2683 @example
2684 print: foo.c bar.c
2685         lpr -p $?
2686         touch print
2687 @end example
2688 @cindex @code{print} target
2689 @cindex @code{lpr} (shell command)
2690 @cindex @code{touch} (shell command)
2692 @noindent
2693 With this rule, @samp{make print} will execute the @code{lpr} command if
2694 either source file has changed since the last @samp{make print}.  The
2695 automatic variable @samp{$?} is used to print only those files that have
2696 changed (@pxref{Automatic Variables}).
2698 @node Special Targets, Multiple Targets, Empty Targets, Rules
2699 @section Special Built-in Target Names
2700 @cindex special targets
2701 @cindex built-in special targets
2702 @cindex targets, built-in special
2704 Certain names have special meanings if they appear as targets.
2706 @table @code
2707 @findex .PHONY
2708 @item .PHONY
2710 The prerequisites of the special target @code{.PHONY} are considered to
2711 be phony targets.  When it is time to consider such a target,
2712 @code{make} will run its recipe unconditionally, regardless of
2713 whether a file with that name exists or what its last-modification
2714 time is.  @xref{Phony Targets, ,Phony Targets}.
2716 @findex .SUFFIXES
2717 @item .SUFFIXES
2719 The prerequisites of the special target @code{.SUFFIXES} are the list
2720 of suffixes to be used in checking for suffix rules.
2721 @xref{Suffix Rules, , Old-Fashioned Suffix Rules}.
2723 @findex .DEFAULT
2724 @item .DEFAULT
2726 The recipe specified for @code{.DEFAULT} is used for any target for
2727 which no rules are found (either explicit rules or implicit rules).
2728 @xref{Last Resort}.  If a @code{.DEFAULT} recipe is specified, every
2729 file mentioned as a prerequisite, but not as a target in a rule, will have
2730 that recipe executed on its behalf.  @xref{Implicit Rule Search,
2731 ,Implicit Rule Search Algorithm}.
2733 @findex .PRECIOUS
2734 @item .PRECIOUS
2735 @cindex precious targets
2736 @cindex preserving with @code{.PRECIOUS}
2738 The targets which @code{.PRECIOUS} depends on are given the following
2739 special treatment: if @code{make} is killed or interrupted during the
2740 execution of their recipes, the target is not deleted.
2741 @xref{Interrupts, ,Interrupting or Killing @code{make}}.  Also, if the
2742 target is an intermediate file, it will not be deleted after it is no
2743 longer needed, as is normally done.  @xref{Chained Rules, ,Chains of
2744 Implicit Rules}.  In this latter respect it overlaps with the
2745 @code{.SECONDARY} special target.
2747 You can also list the target pattern of an implicit rule (such as
2748 @samp{%.o}) as a prerequisite file of the special target @code{.PRECIOUS}
2749 to preserve intermediate files created by rules whose target patterns
2750 match that file's name.
2752 @findex .INTERMEDIATE
2753 @item .INTERMEDIATE
2754 @cindex intermediate targets, explicit
2756 The targets which @code{.INTERMEDIATE} depends on are treated as
2757 intermediate files.  @xref{Chained Rules, ,Chains of Implicit Rules}.
2758 @code{.INTERMEDIATE} with no prerequisites has no effect.
2760 @findex .SECONDARY
2761 @item .SECONDARY
2762 @cindex secondary targets
2763 @cindex preserving with @code{.SECONDARY}
2765 The targets which @code{.SECONDARY} depends on are treated as
2766 intermediate files, except that they are never automatically deleted.
2767 @xref{Chained Rules, ,Chains of Implicit Rules}.
2769 @code{.SECONDARY} with no prerequisites causes all targets to be treated
2770 as secondary (i.e., no target is removed because it is considered
2771 intermediate).
2773 @findex .SECONDEXPANSION
2774 @item .SECONDEXPANSION
2776 If @code{.SECONDEXPANSION} is mentioned as a target anywhere in the
2777 makefile, then all prerequisite lists defined @emph{after} it appears
2778 will be expanded a second time after all makefiles have been read in.
2779 @xref{Secondary Expansion, ,Secondary Expansion}.
2781 @findex .DELETE_ON_ERROR
2782 @item .DELETE_ON_ERROR
2783 @cindex removing targets on failure
2785 If @code{.DELETE_ON_ERROR} is mentioned as a target anywhere in the
2786 makefile, then @code{make} will delete the target of a rule if it has
2787 changed and its recipe exits with a nonzero exit status, just as it
2788 does when it receives a signal.  @xref{Errors, ,Errors in Recipes}.
2790 @findex .IGNORE
2791 @item .IGNORE
2793 If you specify prerequisites for @code{.IGNORE}, then @code{make} will
2794 ignore errors in execution of the recipe for those particular files.
2795 The recipe for @code{.IGNORE} (if any) is ignored.
2797 If mentioned as a target with no prerequisites, @code{.IGNORE} says to
2798 ignore errors in execution of recipes for all files.  This usage of
2799 @samp{.IGNORE} is supported only for historical compatibility.  Since
2800 this affects every recipe in the makefile, it is not very useful; we
2801 recommend you use the more selective ways to ignore errors in specific
2802 recipes.  @xref{Errors, ,Errors in Recipes}.
2804 @findex .LOW_RESOLUTION_TIME
2805 @item .LOW_RESOLUTION_TIME
2807 If you specify prerequisites for @code{.LOW_RESOLUTION_TIME},
2808 @command{make} assumes that these files are created by commands that
2809 generate low resolution time stamps.  The recipe for the
2810 @code{.LOW_RESOLUTION_TIME} target are ignored.
2812 The high resolution file time stamps of many modern file systems
2813 lessen the chance of @command{make} incorrectly concluding that a file
2814 is up to date.  Unfortunately, some hosts do not provide a way to set a
2815 high resolution file time stamp, so commands like @samp{cp -p} that
2816 explicitly set a file's time stamp must discard its subsecond part.
2817 If a file is created by such a command, you should list it as a
2818 prerequisite of @code{.LOW_RESOLUTION_TIME} so that @command{make}
2819 does not mistakenly conclude that the file is out of date.  For
2820 example:
2822 @example
2823 @group
2824 .LOW_RESOLUTION_TIME: dst
2825 dst: src
2826         cp -p src dst
2827 @end group
2828 @end example
2830 Since @samp{cp -p} discards the subsecond part of @file{src}'s time
2831 stamp, @file{dst} is typically slightly older than @file{src} even when
2832 it is up to date.  The @code{.LOW_RESOLUTION_TIME} line causes
2833 @command{make} to consider @file{dst} to be up to date if its time stamp
2834 is at the start of the same second that @file{src}'s time stamp is in.
2836 Due to a limitation of the archive format, archive member time stamps
2837 are always low resolution.  You need not list archive members as
2838 prerequisites of @code{.LOW_RESOLUTION_TIME}, as @command{make} does this
2839 automatically.
2841 @findex .SILENT
2842 @item .SILENT
2844 If you specify prerequisites for @code{.SILENT}, then @code{make} will
2845 not print the recipe used to remake those particular files before
2846 executing them.  The recipe for @code{.SILENT} is ignored.
2848 If mentioned as a target with no prerequisites, @code{.SILENT} says not
2849 to print any recipes before executing them.  This usage of
2850 @samp{.SILENT} is supported only for historical compatibility.  We
2851 recommend you use the more selective ways to silence specific recipes.
2852 @xref{Echoing, ,Recipe Echoing}.  If you want to silence all recipes
2853 for a particular run of @code{make}, use the @samp{-s} or
2854 @w{@samp{--silent}} option (@pxref{Options Summary}).
2856 @findex .EXPORT_ALL_VARIABLES
2857 @item .EXPORT_ALL_VARIABLES
2859 Simply by being mentioned as a target, this tells @code{make} to
2860 export all variables to child processes by default.
2861 @xref{Variables/Recursion, ,Communicating Variables to a
2862 Sub-@code{make}}.
2864 @findex .NOTPARALLEL
2865 @item .NOTPARALLEL
2866 @cindex parallel execution, overriding
2868 If @code{.NOTPARALLEL} is mentioned as a target, then this invocation
2869 of @code{make} will be run serially, even if the @samp{-j} option is
2870 given.  Any recursively invoked @code{make} command will still run
2871 recipes in parallel (unless its makefile also contains this target).
2872 Any prerequisites on this target are ignored.
2873 @end table
2875 Any defined implicit rule suffix also counts as a special target if it
2876 appears as a target, and so does the concatenation of two suffixes, such
2877 as @samp{.c.o}.  These targets are suffix rules, an obsolete way of
2878 defining implicit rules (but a way still widely used).  In principle, any
2879 target name could be special in this way if you break it in two and add
2880 both pieces to the suffix list.  In practice, suffixes normally begin with
2881 @samp{.}, so these special target names also begin with @samp{.}.
2882 @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
2884 @node Multiple Targets, Multiple Rules, Special Targets, Rules
2885 @section Multiple Targets in a Rule
2886 @cindex multiple targets
2887 @cindex several targets in a rule
2888 @cindex targets, multiple
2889 @cindex rule, with multiple targets
2891 A rule with multiple targets is equivalent to writing many rules, each with
2892 one target, and all identical aside from that.  The same recipe applies to
2893 all the targets, but its effect may vary because you can substitute the
2894 actual target name into the recipe using @samp{$@@}.  The rule contributes
2895 the same prerequisites to all the targets also.
2897 This is useful in two cases.
2899 @itemize @bullet
2900 @item
2901 You want just prerequisites, no recipe.  For example:
2903 @example
2904 kbd.o command.o files.o: command.h
2905 @end example
2907 @noindent
2908 gives an additional prerequisite to each of the three object files
2909 mentioned.
2911 @item
2912 Similar recipes work for all the targets.  The recipes do not need
2913 to be absolutely identical, since the automatic variable @samp{$@@}
2914 can be used to substitute the particular target to be remade into the
2915 commands (@pxref{Automatic Variables}).  For example:
2917 @example
2918 @group
2919 bigoutput littleoutput : text.g
2920         generate text.g -$(subst output,,$@@) > $@@
2921 @end group
2922 @end example
2923 @findex subst
2925 @noindent
2926 is equivalent to
2928 @example
2929 bigoutput : text.g
2930         generate text.g -big > bigoutput
2931 littleoutput : text.g
2932         generate text.g -little > littleoutput
2933 @end example
2935 @noindent
2936 Here we assume the hypothetical program @code{generate} makes two
2937 types of output, one if given @samp{-big} and one if given
2938 @samp{-little}.
2939 @xref{Text Functions, ,Functions for String Substitution and Analysis},
2940 for an explanation of the @code{subst} function.
2941 @end itemize
2943 Suppose you would like to vary the prerequisites according to the
2944 target, much as the variable @samp{$@@} allows you to vary the recipe.
2945 You cannot do this with multiple targets in an ordinary rule, but you
2946 can do it with a @dfn{static pattern rule}.  @xref{Static Pattern,
2947 ,Static Pattern Rules}.
2949 @node Multiple Rules, Static Pattern, Multiple Targets, Rules
2950 @section Multiple Rules for One Target
2951 @cindex multiple rules for one target
2952 @cindex several rules for one target
2953 @cindex rule, multiple for one target
2954 @cindex target, multiple rules for one
2956 One file can be the target of several rules.  All the prerequisites
2957 mentioned in all the rules are merged into one list of prerequisites for
2958 the target.  If the target is older than any prerequisite from any rule,
2959 the recipe is executed.
2961 There can only be one recipe to be executed for a file.  If more than
2962 one rule gives a recipe for the same file, @code{make} uses the last
2963 one given and prints an error message.  (As a special case, if the
2964 file's name begins with a dot, no error message is printed.  This odd
2965 behavior is only for compatibility with other implementations of
2966 @code{make}@dots{} you should avoid using it).  Occasionally it is
2967 useful to have the same target invoke multiple recipes which are
2968 defined in different parts of your makefile; you can use
2969 @dfn{double-colon rules} (@pxref{Double-Colon}) for this.
2971 An extra rule with just prerequisites can be used to give a few extra
2972 prerequisites to many files at once.  For example, makefiles often
2973 have a variable, such as @code{objects}, containing a list of all the
2974 compiler output files in the system being made.  An easy way to say
2975 that all of them must be recompiled if @file{config.h} changes is to
2976 write the following:
2978 @example
2979 objects = foo.o bar.o
2980 foo.o : defs.h
2981 bar.o : defs.h test.h
2982 $(objects) : config.h
2983 @end example
2985 This could be inserted or taken out without changing the rules that really
2986 specify how to make the object files, making it a convenient form to use if
2987 you wish to add the additional prerequisite intermittently.
2989 Another wrinkle is that the additional prerequisites could be
2990 specified with a variable that you set with a command line argument to
2991 @code{make} (@pxref{Overriding, ,Overriding Variables}).  For example,
2993 @example
2994 @group
2995 extradeps=
2996 $(objects) : $(extradeps)
2997 @end group
2998 @end example
3000 @noindent
3001 means that the command @samp{make extradeps=foo.h} will consider
3002 @file{foo.h} as a prerequisite of each object file, but plain @samp{make}
3003 will not.
3005 If none of the explicit rules for a target has a recipe, then @code{make}
3006 searches for an applicable implicit rule to find one
3007 @pxref{Implicit Rules, ,Using Implicit Rules}).
3009 @node Static Pattern, Double-Colon, Multiple Rules, Rules
3010 @section Static Pattern Rules
3011 @cindex static pattern rule
3012 @cindex rule, static pattern
3013 @cindex pattern rules, static (not implicit)
3014 @cindex varying prerequisites
3015 @cindex prerequisites, varying (static pattern)
3017 @dfn{Static pattern rules} are rules which specify multiple targets and
3018 construct the prerequisite names for each target based on the target name.
3019 They are more general than ordinary rules with multiple targets because the
3020 targets do not have to have identical prerequisites.  Their prerequisites must
3021 be @emph{analogous}, but not necessarily @emph{identical}.
3023 @menu
3024 * Static Usage::                The syntax of static pattern rules.
3025 * Static versus Implicit::      When are they better than implicit rules?
3026 @end menu
3028 @node Static Usage, Static versus Implicit, Static Pattern, Static Pattern
3029 @subsection Syntax of Static Pattern Rules
3030 @cindex static pattern rule, syntax of
3031 @cindex pattern rules, static, syntax of
3033 Here is the syntax of a static pattern rule:
3035 @example
3036 @var{targets} @dots{}: @var{target-pattern}: @var{prereq-patterns} @dots{}
3037         @var{recipe}
3038         @dots{}
3039 @end example
3041 @noindent
3042 The @var{targets} list specifies the targets that the rule applies to.
3043 The targets can contain wildcard characters, just like the targets of
3044 ordinary rules (@pxref{Wildcards, ,Using Wildcard Characters in File
3045 Names}).
3047 @cindex target pattern, static (not implicit)
3048 @cindex stem
3049 The @var{target-pattern} and @var{prereq-patterns} say how to compute the
3050 prerequisites of each target.  Each target is matched against the
3051 @var{target-pattern} to extract a part of the target name, called the
3052 @dfn{stem}.  This stem is substituted into each of the @var{prereq-patterns}
3053 to make the prerequisite names (one from each @var{prereq-pattern}).
3055 Each pattern normally contains the character @samp{%} just once.  When the
3056 @var{target-pattern} matches a target, the @samp{%} can match any part of
3057 the target name; this part is called the @dfn{stem}.  The rest of the
3058 pattern must match exactly.  For example, the target @file{foo.o} matches
3059 the pattern @samp{%.o}, with @samp{foo} as the stem.  The targets
3060 @file{foo.c} and @file{foo.out} do not match that pattern.@refill
3062 @cindex prerequisite pattern, static (not implicit)
3063 The prerequisite names for each target are made by substituting the stem
3064 for the @samp{%} in each prerequisite pattern.  For example, if one
3065 prerequisite pattern is @file{%.c}, then substitution of the stem
3066 @samp{foo} gives the prerequisite name @file{foo.c}.  It is legitimate
3067 to write a prerequisite pattern that does not contain @samp{%}; then this
3068 prerequisite is the same for all targets.
3070 @cindex @code{%}, quoting in static pattern
3071 @cindex @code{%}, quoting with @code{\} (backslash)
3072 @cindex @code{\} (backslash), to quote @code{%}
3073 @cindex backslash (@code{\}), to quote @code{%}
3074 @cindex quoting @code{%}, in static pattern
3075 @samp{%} characters in pattern rules can be quoted with preceding
3076 backslashes (@samp{\}).  Backslashes that would otherwise quote @samp{%}
3077 characters can be quoted with more backslashes.  Backslashes that quote
3078 @samp{%} characters or other backslashes are removed from the pattern
3079 before it is compared to file names or has a stem substituted into it.
3080 Backslashes that are not in danger of quoting @samp{%} characters go
3081 unmolested.  For example, the pattern @file{the\%weird\\%pattern\\} has
3082 @samp{the%weird\} preceding the operative @samp{%} character, and
3083 @samp{pattern\\} following it.  The final two backslashes are left alone
3084 because they cannot affect any @samp{%} character.@refill
3086 Here is an example, which compiles each of @file{foo.o} and @file{bar.o}
3087 from the corresponding @file{.c} file:
3089 @example
3090 @group
3091 objects = foo.o bar.o
3093 all: $(objects)
3095 $(objects): %.o: %.c
3096         $(CC) -c $(CFLAGS) $< -o $@@
3097 @end group
3098 @end example
3100 @noindent
3101 Here @samp{$<} is the automatic variable that holds the name of the
3102 prerequisite and @samp{$@@} is the automatic variable that holds the name
3103 of the target; see @ref{Automatic Variables}.
3105 Each target specified must match the target pattern; a warning is issued
3106 for each target that does not.  If you have a list of files, only some of
3107 which will match the pattern, you can use the @code{filter} function to
3108 remove nonmatching file names (@pxref{Text Functions, ,Functions for String Substitution and Analysis}):
3110 @example
3111 files = foo.elc bar.o lose.o
3113 $(filter %.o,$(files)): %.o: %.c
3114         $(CC) -c $(CFLAGS) $< -o $@@
3115 $(filter %.elc,$(files)): %.elc: %.el
3116         emacs -f batch-byte-compile $<
3117 @end example
3119 @noindent
3120 In this example the result of @samp{$(filter %.o,$(files))} is
3121 @file{bar.o lose.o}, and the first static pattern rule causes each of
3122 these object files to be updated by compiling the corresponding C source
3123 file.  The result of @w{@samp{$(filter %.elc,$(files))}} is
3124 @file{foo.elc}, so that file is made from @file{foo.el}.@refill
3126 Another example shows how to use @code{$*} in static pattern rules:
3127 @vindex $*@r{, and static pattern}
3129 @example
3130 @group
3131 bigoutput littleoutput : %output : text.g
3132         generate text.g -$* > $@@
3133 @end group
3134 @end example
3136 @noindent
3137 When the @code{generate} command is run, @code{$*} will expand to the
3138 stem, either @samp{big} or @samp{little}.
3140 @node Static versus Implicit,  , Static Usage, Static Pattern
3141 @subsection Static Pattern Rules versus Implicit Rules
3142 @cindex rule, static pattern versus implicit
3143 @cindex static pattern rule, versus implicit
3145 A static pattern rule has much in common with an implicit rule defined as a
3146 pattern rule (@pxref{Pattern Rules, ,Defining and Redefining Pattern Rules}).
3147 Both have a pattern for the target and patterns for constructing the
3148 names of prerequisites.  The difference is in how @code{make} decides
3149 @emph{when} the rule applies.
3151 An implicit rule @emph{can} apply to any target that matches its pattern,
3152 but it @emph{does} apply only when the target has no recipe otherwise
3153 specified, and only when the prerequisites can be found.  If more than one
3154 implicit rule appears applicable, only one applies; the choice depends on
3155 the order of rules.
3157 By contrast, a static pattern rule applies to the precise list of targets
3158 that you specify in the rule.  It cannot apply to any other target and it
3159 invariably does apply to each of the targets specified.  If two conflicting
3160 rules apply, and both have recipes, that's an error.
3162 The static pattern rule can be better than an implicit rule for these
3163 reasons:
3165 @itemize @bullet
3166 @item
3167 You may wish to override the usual implicit rule for a few
3168 files whose names cannot be categorized syntactically but
3169 can be given in an explicit list.
3171 @item
3172 If you cannot be sure of the precise contents of the directories
3173 you are using, you may not be sure which other irrelevant files
3174 might lead @code{make} to use the wrong implicit rule.  The choice
3175 might depend on the order in which the implicit rule search is done.
3176 With static pattern rules, there is no uncertainty: each rule applies
3177 to precisely the targets specified.
3178 @end itemize
3180 @node Double-Colon, Automatic Prerequisites, Static Pattern, Rules
3181 @section Double-Colon Rules
3182 @cindex double-colon rules
3183 @cindex rule, double-colon (@code{::})
3184 @cindex multiple rules for one target (@code{::})
3185 @cindex @code{::} rules (double-colon)
3187 @dfn{Double-colon} rules are rules written with @samp{::} instead of
3188 @samp{:} after the target names.  They are handled differently from
3189 ordinary rules when the same target appears in more than one rule.
3191 When a target appears in multiple rules, all the rules must be the same
3192 type: all ordinary, or all double-colon.  If they are double-colon, each
3193 of them is independent of the others.  Each double-colon rule's recipe
3194 is executed if the target is older than any prerequisites of that rule.
3195 If there are no prerequisites for that rule, its recipe is always
3196 executed (even if the target already exists).  This can result in
3197 executing none, any, or all of the double-colon rules.
3199 Double-colon rules with the same target are in fact completely separate
3200 from one another.  Each double-colon rule is processed individually, just
3201 as rules with different targets are processed.
3203 The double-colon rules for a target are executed in the order they appear
3204 in the makefile.  However, the cases where double-colon rules really make
3205 sense are those where the order of executing the recipes would not matter.
3207 Double-colon rules are somewhat obscure and not often very useful; they
3208 provide a mechanism for cases in which the method used to update a target
3209 differs depending on which prerequisite files caused the update, and such
3210 cases are rare.
3212 Each double-colon rule should specify a recipe; if it does not, an
3213 implicit rule will be used if one applies.
3214 @xref{Implicit Rules, ,Using Implicit Rules}.
3216 @node Automatic Prerequisites,  , Double-Colon, Rules
3217 @section Generating Prerequisites Automatically
3218 @cindex prerequisites, automatic generation
3219 @cindex automatic generation of prerequisites
3220 @cindex generating prerequisites automatically
3222 In the makefile for a program, many of the rules you need to write often
3223 say only that some object file depends on some header
3224 file.  For example, if @file{main.c} uses @file{defs.h} via an
3225 @code{#include}, you would write:
3227 @example
3228 main.o: defs.h
3229 @end example
3231 @noindent
3232 You need this rule so that @code{make} knows that it must remake
3233 @file{main.o} whenever @file{defs.h} changes.  You can see that for a
3234 large program you would have to write dozens of such rules in your
3235 makefile.  And, you must always be very careful to update the makefile
3236 every time you add or remove an @code{#include}.
3237 @cindex @code{#include}
3239 @cindex @code{-M} (to compiler)
3240 To avoid this hassle, most modern C compilers can write these rules for
3241 you, by looking at the @code{#include} lines in the source files.
3242 Usually this is done with the @samp{-M} option to the compiler.
3243 For example, the command:
3245 @example
3246 cc -M main.c
3247 @end example
3249 @noindent
3250 generates the output:
3252 @example
3253 main.o : main.c defs.h
3254 @end example
3256 @noindent
3257 Thus you no longer have to write all those rules yourself.
3258 The compiler will do it for you.
3260 Note that such a prerequisite constitutes mentioning @file{main.o} in a
3261 makefile, so it can never be considered an intermediate file by implicit
3262 rule search.  This means that @code{make} won't ever remove the file
3263 after using it; @pxref{Chained Rules, ,Chains of Implicit Rules}.
3265 @cindex @code{make depend}
3266 With old @code{make} programs, it was traditional practice to use this
3267 compiler feature to generate prerequisites on demand with a command like
3268 @samp{make depend}.  That command would create a file @file{depend}
3269 containing all the automatically-generated prerequisites; then the
3270 makefile could use @code{include} to read them in (@pxref{Include}).
3272 In GNU @code{make}, the feature of remaking makefiles makes this
3273 practice obsolete---you need never tell @code{make} explicitly to
3274 regenerate the prerequisites, because it always regenerates any makefile
3275 that is out of date.  @xref{Remaking Makefiles}.
3277 The practice we recommend for automatic prerequisite generation is to have
3278 one makefile corresponding to each source file.  For each source file
3279 @file{@var{name}.c} there is a makefile @file{@var{name}.d} which lists
3280 what files the object file @file{@var{name}.o} depends on.  That way
3281 only the source files that have changed need to be rescanned to produce
3282 the new prerequisites.
3284 Here is the pattern rule to generate a file of prerequisites (i.e., a makefile)
3285 called @file{@var{name}.d} from a C source file called @file{@var{name}.c}:
3287 @smallexample
3288 @group
3289 %.d: %.c
3290         @@set -e; rm -f $@@; \
3291          $(CC) -M $(CPPFLAGS) $< > $@@.$$$$; \
3292          sed 's,\($*\)\.o[ :]*,\1.o $@@ : ,g' < $@@.$$$$ > $@@; \
3293          rm -f $@@.$$$$
3294 @end group
3295 @end smallexample
3297 @noindent
3298 @xref{Pattern Rules}, for information on defining pattern rules.  The
3299 @samp{-e} flag to the shell causes it to exit immediately if the
3300 @code{$(CC)} command (or any other command) fails (exits with a
3301 nonzero status).
3302 @cindex @code{-e} (shell flag)
3304 @cindex @code{-MM} (to GNU compiler)
3305 With the GNU C compiler, you may wish to use the @samp{-MM} flag instead
3306 of @samp{-M}.  This omits prerequisites on system header files.
3307 @xref{Preprocessor Options, , Options Controlling the Preprocessor,
3308 gcc.info, Using GNU CC}, for details.
3310 @cindex @code{sed} (shell command)
3311 The purpose of the @code{sed} command is to translate (for example):
3313 @example
3314 main.o : main.c defs.h
3315 @end example
3317 @noindent
3318 into:
3320 @example
3321 main.o main.d : main.c defs.h
3322 @end example
3324 @noindent
3325 @cindex @code{.d}
3326 This makes each @samp{.d} file depend on all the source and header files
3327 that the corresponding @samp{.o} file depends on.  @code{make} then
3328 knows it must regenerate the prerequisites whenever any of the source or
3329 header files changes.
3331 Once you've defined the rule to remake the @samp{.d} files,
3332 you then use the @code{include} directive to read them all in.
3333 @xref{Include}.  For example:
3335 @example
3336 @group
3337 sources = foo.c bar.c
3339 include $(sources:.c=.d)
3340 @end group
3341 @end example
3343 @noindent
3344 (This example uses a substitution variable reference to translate the
3345 list of source files @samp{foo.c bar.c} into a list of prerequisite
3346 makefiles, @samp{foo.d bar.d}.  @xref{Substitution Refs}, for full
3347 information on substitution references.)  Since the @samp{.d} files are
3348 makefiles like any others, @code{make} will remake them as necessary
3349 with no further work from you.  @xref{Remaking Makefiles}.
3351 Note that the @samp{.d} files contain target definitions; you should
3352 be sure to place the @code{include} directive @emph{after} the first,
3353 default goal in your makefiles or run the risk of having a random
3354 object file become the default goal.
3355 @xref{How Make Works}.
3357 @node Recipes, Using Variables, Rules, Top
3358 @chapter Writing Recipes in Rules
3359 @cindex recipes
3360 @cindex recipes, how to write
3361 @cindex writing recipes
3363 The recipe of a rule consists of one or more shell command lines to
3364 be executed, one at a time, in the order they appear.  Typically, the
3365 result of executing these commands is that the target of the rule is
3366 brought up to date.
3368 Users use many different shell programs, but recipes in makefiles are
3369 always interpreted by @file{/bin/sh} unless the makefile specifies
3370 otherwise.  @xref{Execution, ,Command Execution}.
3372 @menu
3373 * Recipe Syntax::               Recipe syntax features and pitfalls.
3374 * Echoing::                     How to control when recipes are echoed.
3375 * Execution::                   How recipes are executed.
3376 * Parallel::                    How recipes can be executed in parallel.
3377 * Errors::                      What happens after a recipe execution error.
3378 * Interrupts::                  What happens when a recipe is interrupted.
3379 * Recursion::                   Invoking @code{make} from makefiles.
3380 * Canned Recipes::              Defining canned recipes.
3381 * Empty Recipes::               Defining useful, do-nothing recipes.
3382 @end menu
3384 @node Recipe Syntax, Echoing, Recipes, Recipes
3385 @section Recipe Syntax
3386 @cindex recipe syntax
3387 @cindex syntax of recipe
3389 Makefiles have the unusual property that there are really two distinct
3390 syntaxes in one file.  Most of the makefile uses @code{make} syntax
3391 (@pxref{Makefiles, ,Writing Makefiles}).  However, recipes are meant
3392 to be interpreted by the shell and so they are written using shell
3393 syntax.  The @code{make} program does not try to understand shell
3394 syntax: it performs only a very few specific translations on the
3395 content of the recipe before handing it to the shell.
3397 Each line in the recipe must start with a tab (or the first character
3398 in the value of the @code{.CMDPREFIX} variable; @pxref{Special
3399 Variables}), except that the first recipe line may be attached to the
3400 target-and-prerequisites line with a semicolon in between.  @emph{Any}
3401 line in the makefile that begins with a tab and appears in a ``rule
3402 context'' (that is, after a rule has been started until another rule
3403 or variable definition) will be considered part of a recipe for that
3404 rule.  Blank lines and lines of just comments may appear among the
3405 recipe lines; they are ignored.
3407 Some consequences of these rules include:
3409 @itemize @bullet
3410 @item
3411 A blank line that begins with a tab is not blank: it's an empty
3412 recipe (@pxref{Empty Recipes}).
3414 @cindex comments, in recipes
3415 @cindex recipes, comments in
3416 @cindex @code{#} (comments), in recipes
3417 @item
3418 A comment in a recipe is not a @code{make} comment; it will be
3419 passed to the shell as-is.  Whether the shell treats it as a comment
3420 or not depends on your shell.
3422 @item
3423 A variable definition in a ``rule context'' which is indented by a tab
3424 as the first character on the line, will be considered part of a
3425 recipe, not a @code{make} variable definition, and passed to the
3426 shell.
3428 @item
3429 A conditional expression (@code{ifdef}, @code{ifeq},
3430 etc. @pxref{Conditional Syntax, ,Syntax of Conditionals}) in a ``rule
3431 context'' which is indented by a tab as the first character on the
3432 line, will be considered part of a recipe and be passed to the shell.
3434 @end itemize
3436 @menu
3437 * Splitting Lines::             Breaking long recipe lines for readability.
3438 * Variables in Recipes::        Using @code{make} variables in recipes.
3439 @end menu
3441 @node Splitting Lines, Variables in Recipes, Recipe Syntax, Recipe Syntax
3442 @subsection Splitting Recipe Lines
3443 @cindex recipes, splitting
3444 @cindex splitting recipes
3445 @cindex recipes, backslash (@code{\}) in
3446 @cindex recipes, quoting newlines in
3447 @cindex backslash (@code{\}), in recipes
3448 @cindex @code{\} (backslash), in recipes
3449 @cindex quoting newline, in recipes
3450 @cindex newline, quoting, in recipes
3452 One of the few ways in which @code{make} does interpret recipes is
3453 checking for a backslash just before the newline.  As in normal
3454 makefile syntax, a single logical recipe line can be split into
3455 multiple physical lines in the makefile by placing a backslash before
3456 each newline.  A sequence of lines like this is considered a single
3457 recipe line, and one instance of the shell will be invoked to run it.
3459 However, in contrast to how they are treated in other places in a
3460 makefile, backslash-newline pairs are @emph{not} removed from the
3461 recipe.  Both the backslash and the newline characters are preserved
3462 and passed to the shell.  How the backslash-newline is interpreted
3463 depends on your shell.  If the first character of the next line after
3464 the backslash-newline is the recipe prefix character (a tab by
3465 default; @pxref{Special Variables}), then that character (and only
3466 that character) is removed.  Whitespace is never added to the recipe.
3468 For example, the recipe for the all target in this makefile:
3470 @example
3471 @group
3472 all :
3473         @@echo no\
3474 space
3475         @@echo no\
3476         space
3477         @@echo one \
3478         space
3479         @@echo one\
3480          space
3481 @end group
3482 @end example
3484 @noindent
3485 consists of four separate shell commands where the output is:
3487 @example
3488 @group
3489 nospace
3490 nospace
3491 one space
3492 one space
3493 @end group
3494 @end example
3496 As a more complex example, this makefile:
3498 @example
3499 @group
3500 all : ; @@echo 'hello \
3501         world' ; echo "hello \
3502     world"
3503 @end group
3504 @end example
3506 @noindent
3507 will invoke one shell with a command of:
3509 @example
3510 @group
3511 echo 'hello \
3512 world' ; echo "hello \
3513     world"
3514 @end group
3515 @end example
3517 @noindent
3518 which, according to shell quoting rules, will yield the following output:
3520 @example
3521 @group
3522 hello \
3523 world
3524 hello     world
3525 @end group
3526 @end example
3528 @noindent
3529 Notice how the backslash/newline pair was removed inside the string
3530 quoted with double quotes (@code{"@dots{}"}), but not from the string
3531 quoted with single quotes (@code{'@dots{}'}).  This is the way the
3532 default shell (@file{/bin/sh}) handles backslash/newline pairs.  If
3533 you specify a different shell in your makefiles it may treat them
3534 differently.
3536 Sometimes you want to split a long line inside of single quotes, but
3537 you don't want the backslash-newline to appear in the quoted content.
3538 This is often the case when passing scripts to languages such as Perl,
3539 where extraneous backslashes inside the script can change its meaning
3540 or even be a syntax error.  One simple way of handling this is to
3541 place the quoted string, or even the entire command, into a
3542 @code{make} variable then use the variable in the recipe.  In this
3543 situation the newline quoting rules for makefiles will be used, and
3544 the backslash-newline will be removed.  If we rewrite our example
3545 above using this method:
3547 @example
3548 @group
3549 HELLO = 'hello \
3550 world'
3552 all : ; @@echo $(HELLO)
3553 @end group
3554 @end example
3556 @noindent
3557 we will get output like this:
3559 @example
3560 @group
3561 hello world
3562 @end group
3563 @end example
3565 If you like, you can also use target-specific variables
3566 (@pxref{Target-specific, ,Target-specific Variable Values}) to obtain
3567 a tighter correspondence between the variable and the recipe that
3568 uses it.
3570 @node Variables in Recipes,  , Splitting Lines, Recipe Syntax
3571 @subsection Using Variables in Recipes
3572 @cindex variable references in recipes
3573 @cindex recipes, using variables in
3575 The other way in which @code{make} processes recipes is by expanding
3576 any variable references in them (@pxref{Reference,Basics of Variable
3577 References}).  This occurs after make has finished reading all the
3578 makefiles and the target is determined to be out of date; so, the
3579 recipes for targets which are not rebuilt are never expanded.
3581 Variable and function references in recipes have identical syntax and
3582 semantics to references elsewhere in the makefile.  They also have the
3583 same quoting rules: if you want a dollar sign to appear in your
3584 recipe, you must double it (@samp{$$}).  For shells like the default
3585 shell, that use dollar signs to introduce variables, it's important to
3586 keep clear in your mind whether the variable you want to reference is
3587 a @code{make} variable (use a single dollar sign) or a shell variable
3588 (use two dollar signs).  For example:
3590 @example
3591 @group
3592 LIST = one two three
3593 all:
3594         for i in $(LIST); do \
3595             echo $$i; \
3596         done
3597 @end group
3598 @end example
3600 @noindent
3601 results in the following command being passed to the shell:
3603 @example
3604 @group
3605 for i in one two three; do \
3606     echo $i; \
3607 done
3608 @end group
3609 @end example
3611 @noindent
3612 which generates the expected result:
3614 @example
3615 @group
3618 three
3619 @end group
3620 @end example
3622 @node Echoing, Execution, Recipe Syntax, Recipes
3623 @section Recipe Echoing
3624 @cindex echoing of recipes
3625 @cindex silent operation
3626 @cindex @code{@@} (in recipes)
3627 @cindex recipes, echoing
3628 @cindex printing of recipes
3630 Normally @code{make} prints each line of the recipe before it is
3631 executed.  We call this @dfn{echoing} because it gives the appearance
3632 that you are typing the lines yourself.
3634 When a line starts with @samp{@@}, the echoing of that line is suppressed.
3635 The @samp{@@} is discarded before the line is passed to the shell.
3636 Typically you would use this for a command whose only effect is to print
3637 something, such as an @code{echo} command to indicate progress through
3638 the makefile:
3640 @example
3641 @@echo About to make distribution files
3642 @end example
3644 @cindex @code{-n}
3645 @cindex @code{--just-print}
3646 @cindex @code{--dry-run}
3647 @cindex @code{--recon}
3648 When @code{make} is given the flag @samp{-n} or @samp{--just-print} it
3649 only echoes most recipes, without executing them.  @xref{Options
3650 Summary, ,Summary of Options}.  In this case even the recipe lines
3651 starting with @samp{@@} are printed.  This flag is useful for finding
3652 out which recipes @code{make} thinks are necessary without actually
3653 doing them.
3655 @cindex @code{-s}
3656 @cindex @code{--silent}
3657 @cindex @code{--quiet}
3658 @findex .SILENT
3659 The @samp{-s} or @samp{--silent}
3660 flag to @code{make} prevents all echoing, as if all recipes
3661 started with @samp{@@}.  A rule in the makefile for the special target
3662 @code{.SILENT} without prerequisites has the same effect
3663 (@pxref{Special Targets, ,Special Built-in Target Names}).
3664 @code{.SILENT} is essentially obsolete since @samp{@@} is more flexible.@refill
3666 @node Execution, Parallel, Echoing, Recipes
3667 @section Recipe Execution
3668 @cindex recipe, execution
3669 @cindex execution, of recipes
3670 @vindex @code{SHELL} @r{(recipe execution)}
3672 When it is time to execute recipes to update a target, they are
3673 executed by invoking a new subshell for each line of the recipe.  (In
3674 practice, @code{make} may take shortcuts that do not affect the
3675 results.)
3677 @cindex @code{cd} (shell command)
3678 @cindex shell variables, setting in recipes
3679 @cindex recipes setting shell variables
3680 @strong{Please note:} this implies that setting shell variables and
3681 invoking shell commands such as @code{cd} that set a context local to
3682 each process will not affect the following lines in the recipe.@footnote{On
3683 MS-DOS, the value of current working directory is @strong{global}, so
3684 changing it @emph{will} affect the following recipe lines on those
3685 systems.}  If you want to use @code{cd} to affect the next statement,
3686 put both statements in a single recipe line.  Then @code{make} will
3687 invoke one shell to run the entire line, and the shell will execute
3688 the statements in sequence.  For example:
3690 @example
3691 foo : bar/lose
3692         cd $(@@D) && gobble $(@@F) > ../$@@
3693 @end example
3695 @noindent
3696 Here we use the shell AND operator (@code{&&}) so that if the
3697 @code{cd} command fails, the script will fail without trying to invoke
3698 the @code{gobble} command in the wrong directory, which could cause
3699 problems (in this case it would certainly cause @file{../foo} to be
3700 truncated, at least).
3702 @menu
3703 * Choosing the Shell::          How @code{make} chooses the shell used
3704                                   to run recipes.
3705 @end menu
3707 @node Choosing the Shell,  , Execution, Execution
3708 @subsection Choosing the Shell
3709 @cindex shell, choosing the
3710 @cindex @code{SHELL}, value of
3712 @vindex SHELL
3713 The program used as the shell is taken from the variable @code{SHELL}.
3714 If this variable is not set in your makefile, the program
3715 @file{/bin/sh} is used as the shell.
3717 @cindex environment, @code{SHELL} in
3718 Unlike most variables, the variable @code{SHELL} is never set from the
3719 environment.  This is because the @code{SHELL} environment variable is
3720 used to specify your personal choice of shell program for interactive
3721 use.  It would be very bad for personal choices like this to affect the
3722 functioning of makefiles.  @xref{Environment, ,Variables from the
3723 Environment}.
3725 Furthermore, when you do set @code{SHELL} in your makefile that value
3726 is @emph{not} exported in the environment to recipe lines that
3727 @code{make} invokes.  Instead, the value inherited from the user's
3728 environment, if any, is exported.  You can override this behavior by
3729 explicitly exporting @code{SHELL} (@pxref{Variables/Recursion,
3730 ,Communicating Variables to a Sub-@code{make}}), forcing it to be
3731 passed in the environment to recipe lines.
3733 @vindex @code{MAKESHELL} @r{(MS-DOS alternative to @code{SHELL})}
3734 However, on MS-DOS and MS-Windows the value of @code{SHELL} in the
3735 environment @strong{is} used, since on those systems most users do not
3736 set this variable, and therefore it is most likely set specifically to
3737 be used by @code{make}.  On MS-DOS, if the setting of @code{SHELL} is
3738 not suitable for @code{make}, you can set the variable
3739 @code{MAKESHELL} to the shell that @code{make} should use; if set it
3740 will be used as the shell instead of the value of @code{SHELL}.
3742 @subsubheading Choosing a Shell in DOS and Windows
3743 @cindex shell, in DOS and Windows
3744 @cindex DOS, choosing a shell in
3745 @cindex Windows, choosing a shell in
3747 Choosing a shell in MS-DOS and MS-Windows is much more complex than on
3748 other systems.
3750 @vindex COMSPEC
3751 On MS-DOS, if @code{SHELL} is not set, the value of the variable
3752 @code{COMSPEC} (which is always set) is used instead.
3754 @cindex @code{SHELL}, MS-DOS specifics
3755 The processing of lines that set the variable @code{SHELL} in Makefiles
3756 is different on MS-DOS.  The stock shell, @file{command.com}, is
3757 ridiculously limited in its functionality and many users of @code{make}
3758 tend to install a replacement shell.  Therefore, on MS-DOS, @code{make}
3759 examines the value of @code{SHELL}, and changes its behavior based on
3760 whether it points to a Unix-style or DOS-style shell.  This allows
3761 reasonable functionality even if @code{SHELL} points to
3762 @file{command.com}.
3764 If @code{SHELL} points to a Unix-style shell, @code{make} on MS-DOS
3765 additionally checks whether that shell can indeed be found; if not, it
3766 ignores the line that sets @code{SHELL}.  In MS-DOS, GNU @code{make}
3767 searches for the shell in the following places:
3769 @enumerate
3770 @item
3771 In the precise place pointed to by the value of @code{SHELL}.  For
3772 example, if the makefile specifies @samp{SHELL = /bin/sh}, @code{make}
3773 will look in the directory @file{/bin} on the current drive.
3775 @item
3776 In the current directory.
3778 @item
3779 In each of the directories in the @code{PATH} variable, in order.
3781 @end enumerate
3783 In every directory it examines, @code{make} will first look for the
3784 specific file (@file{sh} in the example above).  If this is not found,
3785 it will also look in that directory for that file with one of the known
3786 extensions which identify executable files.  For example @file{.exe},
3787 @file{.com}, @file{.bat}, @file{.btm}, @file{.sh}, and some others.
3789 If any of these attempts is successful, the value of @code{SHELL} will
3790 be set to the full pathname of the shell as found.  However, if none of
3791 these is found, the value of @code{SHELL} will not be changed, and thus
3792 the line that sets it will be effectively ignored.  This is so
3793 @code{make} will only support features specific to a Unix-style shell if
3794 such a shell is actually installed on the system where @code{make} runs.
3796 Note that this extended search for the shell is limited to the cases
3797 where @code{SHELL} is set from the Makefile; if it is set in the
3798 environment or command line, you are expected to set it to the full
3799 pathname of the shell, exactly as things are on Unix.
3801 The effect of the above DOS-specific processing is that a Makefile that
3802 contains @samp{SHELL = /bin/sh} (as many Unix makefiles do), will work
3803 on MS-DOS unaltered if you have e.g.@: @file{sh.exe} installed in some
3804 directory along your @code{PATH}.
3806 @node Parallel, Errors, Execution, Recipes
3807 @section Parallel Execution
3808 @cindex recipes, execution in parallel
3809 @cindex parallel execution
3810 @cindex execution, in parallel
3811 @cindex job slots
3812 @cindex @code{-j}
3813 @cindex @code{--jobs}
3815 GNU @code{make} knows how to execute several recipes at once.
3816 Normally, @code{make} will execute only one recipe at a time, waiting
3817 for it to finish before executing the next.  However, the @samp{-j} or
3818 @samp{--jobs} option tells @code{make} to execute many recipes
3819 simultaneously.  You can inhibit parallelism in a particular makefile
3820 with the @code{.NOTPARALLEL} pseudo-target (@pxref{Special
3821 Targets,Special Built-in Target Names}).@refill
3823 On MS-DOS, the @samp{-j} option has no effect, since that system doesn't
3824 support multi-processing.
3826 If the @samp{-j} option is followed by an integer, this is the number of
3827 recipes to execute at once; this is called the number of @dfn{job slots}.
3828 If there is nothing looking like an integer after the @samp{-j} option,
3829 there is no limit on the number of job slots.  The default number of job
3830 slots is one, which means serial execution (one thing at a time).
3832 One unpleasant consequence of running several recipes simultaneously is
3833 that output generated by the recipes appears whenever each recipe
3834 sends it, so messages from different recipes may be interspersed.
3836 Another problem is that two processes cannot both take input from the
3837 same device; so to make sure that only one recipe tries to take input
3838 from the terminal at once, @code{make} will invalidate the standard
3839 input streams of all but one running recipe.  This means that
3840 attempting to read from standard input will usually be a fatal error (a
3841 @samp{Broken pipe} signal) for most child processes if there are
3842 several.
3843 @cindex broken pipe
3844 @cindex standard input
3846 It is unpredictable which recipe will have a valid standard input stream
3847 (which will come from the terminal, or wherever you redirect the standard
3848 input of @code{make}).  The first recipe run will always get it first, and
3849 the first recipe started after that one finishes will get it next, and so
3852 We will change how this aspect of @code{make} works if we find a better
3853 alternative.  In the mean time, you should not rely on any recipe using
3854 standard input at all if you are using the parallel execution feature; but
3855 if you are not using this feature, then standard input works normally in
3856 all recipes.
3858 Finally, handling recursive @code{make} invocations raises issues.  For
3859 more information on this, see
3860 @ref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
3862 If a recipe fails (is killed by a signal or exits with a nonzero
3863 status), and errors are not ignored for that recipe
3864 (@pxref{Errors, ,Errors in Recipes}),
3865 the remaining recipe lines to remake the same target will not be run.
3866 If a recipe fails and the @samp{-k} or @samp{--keep-going}
3867 option was not given
3868 (@pxref{Options Summary, ,Summary of Options}),
3869 @code{make} aborts execution.  If make
3870 terminates for any reason (including a signal) with child processes
3871 running, it waits for them to finish before actually exiting.@refill
3873 @cindex load average
3874 @cindex limiting jobs based on load
3875 @cindex jobs, limiting based on load
3876 @cindex @code{-l} (load average)
3877 @cindex @code{--max-load}
3878 @cindex @code{--load-average}
3879 When the system is heavily loaded, you will probably want to run fewer jobs
3880 than when it is lightly loaded.  You can use the @samp{-l} option to tell
3881 @code{make} to limit the number of jobs to run at once, based on the load
3882 average.  The @samp{-l} or @samp{--max-load}
3883 option is followed by a floating-point number.  For
3884 example,
3886 @example
3887 -l 2.5
3888 @end example
3890 @noindent
3891 will not let @code{make} start more than one job if the load average is
3892 above 2.5.  The @samp{-l} option with no following number removes the
3893 load limit, if one was given with a previous @samp{-l} option.@refill
3895 More precisely, when @code{make} goes to start up a job, and it already has
3896 at least one job running, it checks the current load average; if it is not
3897 lower than the limit given with @samp{-l}, @code{make} waits until the load
3898 average goes below that limit, or until all the other jobs finish.
3900 By default, there is no load limit.
3902 @node Errors, Interrupts, Parallel, Recipes
3903 @section Errors in Recipes
3904 @cindex errors (in recipes)
3905 @cindex recipes, errors in
3906 @cindex exit status (errors)
3908 After each shell invocation returns, @code{make} looks at its exit
3909 status.  If the shell completed successfully (the exit status is
3910 zero), the next line in the recipe is executed in a new shell; after
3911 the last line is finished, the rule is finished.
3913 If there is an error (the exit status is nonzero), @code{make} gives up on
3914 the current rule, and perhaps on all rules.
3916 Sometimes the failure of a certain recipe line does not indicate a problem.
3917 For example, you may use the @code{mkdir} command to ensure that a
3918 directory exists.  If the directory already exists, @code{mkdir} will
3919 report an error, but you probably want @code{make} to continue regardless.
3921 @cindex @code{-} (in recipes)
3922 To ignore errors in a recipe line, write a @samp{-} at the beginning
3923 of the line's text (after the initial tab).  The @samp{-} is discarded
3924 before the line is passed to the shell for execution.
3926 For example,
3928 @example
3929 @group
3930 clean:
3931         -rm -f *.o
3932 @end group
3933 @end example
3934 @cindex @code{rm} (shell command)
3936 @noindent
3937 This causes @code{make} to continue even if @code{rm} is unable to
3938 remove a file.
3940 @cindex @code{-i}
3941 @cindex @code{--ignore-errors}
3942 @findex .IGNORE
3943 When you run @code{make} with the @samp{-i} or @samp{--ignore-errors}
3944 flag, errors are ignored in all recipes of all rules.  A rule in the
3945 makefile for the special target @code{.IGNORE} has the same effect, if
3946 there are no prerequisites.  These ways of ignoring errors are obsolete
3947 because @samp{-} is more flexible.
3949 When errors are to be ignored, because of either a @samp{-} or the
3950 @samp{-i} flag, @code{make} treats an error return just like success,
3951 except that it prints out a message that tells you the status code
3952 the shell exited with, and says that the error has been ignored.
3954 When an error happens that @code{make} has not been told to ignore,
3955 it implies that the current target cannot be correctly remade, and neither
3956 can any other that depends on it either directly or indirectly.  No further
3957 recipes will be executed for these targets, since their preconditions
3958 have not been achieved.
3961 @cindex @code{-k}
3962 @cindex @code{--keep-going}
3963 Normally @code{make} gives up immediately in this circumstance, returning a
3964 nonzero status.  However, if the @samp{-k} or @samp{--keep-going}
3965 flag is specified, @code{make}
3966 continues to consider the other prerequisites of the pending targets,
3967 remaking them if necessary, before it gives up and returns nonzero status.
3968 For example, after an error in compiling one object file, @samp{make -k}
3969 will continue compiling other object files even though it already knows
3970 that linking them will be impossible.  @xref{Options Summary, ,Summary of Options}.
3972 The usual behavior assumes that your purpose is to get the specified
3973 targets up to date; once @code{make} learns that this is impossible, it
3974 might as well report the failure immediately.  The @samp{-k} option says
3975 that the real purpose is to test as many of the changes made in the
3976 program as possible, perhaps to find several independent problems so
3977 that you can correct them all before the next attempt to compile.  This
3978 is why Emacs' @code{compile} command passes the @samp{-k} flag by
3979 default.
3980 @cindex Emacs (@code{M-x compile})
3982 @findex .DELETE_ON_ERROR
3983 @cindex deletion of target files
3984 @cindex removal of target files
3985 @cindex target, deleting on error
3986 Usually when a recipe line fails, if it has changed the target file at all,
3987 the file is corrupted and cannot be used---or at least it is not
3988 completely updated.  Yet the file's time stamp says that it is now up to
3989 date, so the next time @code{make} runs, it will not try to update that
3990 file.  The situation is just the same as when the shell is killed by a
3991 signal; @pxref{Interrupts}.  So generally the right thing to do is to
3992 delete the target file if the recipe fails after beginning to change
3993 the file.  @code{make} will do this if @code{.DELETE_ON_ERROR} appears
3994 as a target.  This is almost always what you want @code{make} to do, but
3995 it is not historical practice; so for compatibility, you must explicitly
3996 request it.
3998 @node Interrupts, Recursion, Errors, Recipes
3999 @section Interrupting or Killing @code{make}
4000 @cindex interrupt
4001 @cindex signal
4002 @cindex deletion of target files
4003 @cindex removal of target files
4004 @cindex target, deleting on interrupt
4005 @cindex killing (interruption)
4007 If @code{make} gets a fatal signal while a shell is executing, it may
4008 delete the target file that the recipe was supposed to update.  This is
4009 done if the target file's last-modification time has changed since
4010 @code{make} first checked it.
4012 The purpose of deleting the target is to make sure that it is remade from
4013 scratch when @code{make} is next run.  Why is this?  Suppose you type
4014 @kbd{Ctrl-c} while a compiler is running, and it has begun to write an
4015 object file @file{foo.o}.  The @kbd{Ctrl-c} kills the compiler, resulting
4016 in an incomplete file whose last-modification time is newer than the source
4017 file @file{foo.c}.  But @code{make} also receives the @kbd{Ctrl-c} signal
4018 and deletes this incomplete file.  If @code{make} did not do this, the next
4019 invocation of @code{make} would think that @file{foo.o} did not require
4020 updating---resulting in a strange error message from the linker when it
4021 tries to link an object file half of which is missing.
4023 @findex .PRECIOUS
4024 You can prevent the deletion of a target file in this way by making the
4025 special target @code{.PRECIOUS} depend on it.  Before remaking a target,
4026 @code{make} checks to see whether it appears on the prerequisites of
4027 @code{.PRECIOUS}, and thereby decides whether the target should be deleted
4028 if a signal happens.  Some reasons why you might do this are that the
4029 target is updated in some atomic fashion, or exists only to record a
4030 modification-time (its contents do not matter), or must exist at all
4031 times to prevent other sorts of trouble.
4033 @node Recursion, Canned Recipes, Interrupts, Recipes
4034 @section Recursive Use of @code{make}
4035 @cindex recursion
4036 @cindex subdirectories, recursion for
4038 Recursive use of @code{make} means using @code{make} as a command in a
4039 makefile.  This technique is useful when you want separate makefiles for
4040 various subsystems that compose a larger system.  For example, suppose you
4041 have a subdirectory @file{subdir} which has its own makefile, and you would
4042 like the containing directory's makefile to run @code{make} on the
4043 subdirectory.  You can do it by writing this:
4045 @example
4046 subsystem:
4047         cd subdir && $(MAKE)
4048 @end example
4050 @noindent
4051 or, equivalently, this (@pxref{Options Summary, ,Summary of Options}):
4053 @example
4054 subsystem:
4055         $(MAKE) -C subdir
4056 @end example
4057 @cindex @code{-C}
4058 @cindex @code{--directory}
4060 You can write recursive @code{make} commands just by copying this example,
4061 but there are many things to know about how they work and why, and about
4062 how the sub-@code{make} relates to the top-level @code{make}.  You may
4063 also find it useful to declare targets that invoke recursive
4064 @code{make} commands as @samp{.PHONY} (for more discussion on when
4065 this is useful, see @ref{Phony Targets}).
4067 @vindex @code{CURDIR}
4068 For your convenience, when GNU @code{make} starts (after it has
4069 processed any @code{-C} options) it sets the variable @code{CURDIR} to
4070 the pathname of the current working directory.  This value is never
4071 touched by @code{make} again: in particular note that if you include
4072 files from other directories the value of @code{CURDIR} does not
4073 change.  The value has the same precedence it would have if it were
4074 set in the makefile (by default, an environment variable @code{CURDIR}
4075 will not override this value).  Note that setting this variable has no
4076 impact on the operation of @code{make} (it does not cause @code{make}
4077 to change its working directory, for example).
4079 @menu
4080 * MAKE Variable::               The special effects of using @samp{$(MAKE)}.
4081 * Variables/Recursion::         How to communicate variables to a sub-@code{make}.
4082 * Options/Recursion::           How to communicate options to a sub-@code{make}.
4083 * -w Option::                   How the @samp{-w} or @samp{--print-directory} option
4084                                   helps debug use of recursive @code{make} commands.
4085 @end menu
4087 @node MAKE Variable, Variables/Recursion, Recursion, Recursion
4088 @subsection How the @code{MAKE} Variable Works
4089 @vindex MAKE
4090 @cindex recursion, and @code{MAKE} variable
4092 Recursive @code{make} commands should always use the variable @code{MAKE},
4093 not the explicit command name @samp{make}, as shown here:
4095 @example
4096 @group
4097 subsystem:
4098         cd subdir && $(MAKE)
4099 @end group
4100 @end example
4102 The value of this variable is the file name with which @code{make} was
4103 invoked.  If this file name was @file{/bin/make}, then the recipe executed
4104 is @samp{cd subdir && /bin/make}.  If you use a special version of
4105 @code{make} to run the top-level makefile, the same special version will be
4106 executed for recursive invocations.
4107 @cindex @code{cd} (shell command)
4109 @cindex +, and recipes
4110 As a special feature, using the variable @code{MAKE} in the recipe of
4111 a rule alters the effects of the @samp{-t} (@samp{--touch}), @samp{-n}
4112 (@samp{--just-print}), or @samp{-q} (@w{@samp{--question}}) option.
4113 Using the @code{MAKE} variable has the same effect as using a @samp{+}
4114 character at the beginning of the recipe line.  @xref{Instead of
4115 Execution, ,Instead of Executing the Recipes}.  This special feature
4116 is only enabled if the @code{MAKE} variable appears directly in the
4117 recipe: it does not apply if the @code{MAKE} variable is referenced
4118 through expansion of another variable.  In the latter case you must
4119 use the @samp{+} token to get these special effects.@refill
4121 Consider the command @samp{make -t} in the above example.  (The
4122 @samp{-t} option marks targets as up to date without actually running
4123 any recipes; see @ref{Instead of Execution}.)  Following the usual
4124 definition of @samp{-t}, a @samp{make -t} command in the example would
4125 create a file named @file{subsystem} and do nothing else.  What you
4126 really want it to do is run @samp{@w{cd subdir &&} @w{make -t}}; but
4127 that would require executing the recipe, and @samp{-t} says not to
4128 execute recipes.@refill
4129 @cindex @code{-t}, and recursion
4130 @cindex recursion, and @code{-t}
4131 @cindex @code{--touch}, and recursion
4133 The special feature makes this do what you want: whenever a recipe
4134 line of a rule contains the variable @code{MAKE}, the flags @samp{-t},
4135 @samp{-n} and @samp{-q} do not apply to that line.  Recipe lines
4136 containing @code{MAKE} are executed normally despite the presence of a
4137 flag that causes most recipes not to be run.  The usual
4138 @code{MAKEFLAGS} mechanism passes the flags to the sub-@code{make}
4139 (@pxref{Options/Recursion, ,Communicating Options to a
4140 Sub-@code{make}}), so your request to touch the files, or print the
4141 recipes, is propagated to the subsystem.@refill
4143 @node Variables/Recursion, Options/Recursion, MAKE Variable, Recursion
4144 @subsection Communicating Variables to a Sub-@code{make}
4145 @cindex sub-@code{make}
4146 @cindex environment, and recursion
4147 @cindex exporting variables
4148 @cindex variables, environment
4149 @cindex variables, exporting
4150 @cindex recursion, and environment
4151 @cindex recursion, and variables
4153 Variable values of the top-level @code{make} can be passed to the
4154 sub-@code{make} through the environment by explicit request.  These
4155 variables are defined in the sub-@code{make} as defaults, but do not
4156 override what is specified in the makefile used by the sub-@code{make}
4157 makefile unless you use the @samp{-e} switch (@pxref{Options Summary,
4158 ,Summary of Options}).@refill
4160 To pass down, or @dfn{export}, a variable, @code{make} adds the
4161 variable and its value to the environment for running each line of the
4162 recipe.  The sub-@code{make}, in turn, uses the environment to
4163 initialize its table of variable values.  @xref{Environment,
4164 ,Variables from the Environment}.
4166 Except by explicit request, @code{make} exports a variable only if it
4167 is either defined in the environment initially or set on the command
4168 line, and if its name consists only of letters, numbers, and underscores.
4169 Some shells cannot cope with environment variable names consisting of
4170 characters other than letters, numbers, and underscores.
4172 @cindex SHELL, exported value
4173 The value of the @code{make} variable @code{SHELL} is not exported.
4174 Instead, the value of the @code{SHELL} variable from the invoking
4175 environment is passed to the sub-@code{make}.  You can force
4176 @code{make} to export its value for @code{SHELL} by using the
4177 @code{export} directive, described below.  @xref{Choosing the Shell}.
4179 The special variable @code{MAKEFLAGS} is always exported (unless you
4180 unexport it).  @code{MAKEFILES} is exported if you set it to anything.
4182 @code{make} automatically passes down variable values that were defined
4183 on the command line, by putting them in the @code{MAKEFLAGS} variable.
4184 @iftex
4185 See the next section.
4186 @end iftex
4187 @ifnottex
4188 @xref{Options/Recursion}.
4189 @end ifnottex
4191 Variables are @emph{not} normally passed down if they were created by
4192 default by @code{make} (@pxref{Implicit Variables, ,Variables Used by
4193 Implicit Rules}).  The sub-@code{make} will define these for
4194 itself.@refill
4196 @findex export
4197 If you want to export specific variables to a sub-@code{make}, use the
4198 @code{export} directive, like this:
4200 @example
4201 export @var{variable} @dots{}
4202 @end example
4204 @noindent
4205 @findex unexport
4206 If you want to @emph{prevent} a variable from being exported, use the
4207 @code{unexport} directive, like this:
4209 @example
4210 unexport @var{variable} @dots{}
4211 @end example
4213 @noindent
4214 In both of these forms, the arguments to @code{export} and
4215 @code{unexport} are expanded, and so could be variables or functions
4216 which expand to a (list of) variable names to be (un)exported.
4218 As a convenience, you can define a variable and export it at the same
4219 time by doing:
4221 @example
4222 export @var{variable} = value
4223 @end example
4225 @noindent
4226 has the same result as:
4228 @example
4229 @var{variable} = value
4230 export @var{variable}
4231 @end example
4233 @noindent
4236 @example
4237 export @var{variable} := value
4238 @end example
4240 @noindent
4241 has the same result as:
4243 @example
4244 @var{variable} := value
4245 export @var{variable}
4246 @end example
4248 Likewise,
4250 @example
4251 export @var{variable} += value
4252 @end example
4254 @noindent
4255 is just like:
4257 @example
4258 @var{variable} += value
4259 export @var{variable}
4260 @end example
4262 @noindent
4263 @xref{Appending, ,Appending More Text to Variables}.
4265 You may notice that the @code{export} and @code{unexport} directives
4266 work in @code{make} in the same way they work in the shell, @code{sh}.
4268 If you want all variables to be exported by default, you can use
4269 @code{export} by itself:
4271 @example
4272 export
4273 @end example
4275 @noindent
4276 This tells @code{make} that variables which are not explicitly mentioned
4277 in an @code{export} or @code{unexport} directive should be exported.
4278 Any variable given in an @code{unexport} directive will still @emph{not}
4279 be exported.  If you use @code{export} by itself to export variables by
4280 default, variables whose names contain characters other than
4281 alphanumerics and underscores will not be exported unless specifically
4282 mentioned in an @code{export} directive.@refill
4284 @findex .EXPORT_ALL_VARIABLES
4285 The behavior elicited by an @code{export} directive by itself was the
4286 default in older versions of GNU @code{make}.  If your makefiles depend
4287 on this behavior and you want to be compatible with old versions of
4288 @code{make}, you can write a rule for the special target
4289 @code{.EXPORT_ALL_VARIABLES} instead of using the @code{export} directive.
4290 This will be ignored by old @code{make}s, while the @code{export}
4291 directive will cause a syntax error.@refill
4292 @cindex compatibility in exporting
4294 Likewise, you can use @code{unexport} by itself to tell @code{make}
4295 @emph{not} to export variables by default.  Since this is the default
4296 behavior, you would only need to do this if @code{export} had been used
4297 by itself earlier (in an included makefile, perhaps).  You
4298 @strong{cannot} use @code{export} and @code{unexport} by themselves to
4299 have variables exported for some recipes and not for others.  The last
4300 @code{export} or @code{unexport} directive that appears by itself
4301 determines the behavior for the entire run of @code{make}.@refill
4303 @vindex MAKELEVEL
4304 @cindex recursion, level of
4305 As a special feature, the variable @code{MAKELEVEL} is changed when it
4306 is passed down from level to level.  This variable's value is a string
4307 which is the depth of the level as a decimal number.  The value is
4308 @samp{0} for the top-level @code{make}; @samp{1} for a sub-@code{make},
4309 @samp{2} for a sub-sub-@code{make}, and so on.  The incrementation
4310 happens when @code{make} sets up the environment for a recipe.@refill
4312 The main use of @code{MAKELEVEL} is to test it in a conditional
4313 directive (@pxref{Conditionals, ,Conditional Parts of Makefiles}); this
4314 way you can write a makefile that behaves one way if run recursively and
4315 another way if run directly by you.@refill
4317 @vindex MAKEFILES
4318 You can use the variable @code{MAKEFILES} to cause all sub-@code{make}
4319 commands to use additional makefiles.  The value of @code{MAKEFILES} is
4320 a whitespace-separated list of file names.  This variable, if defined in
4321 the outer-level makefile, is passed down through the environment; then
4322 it serves as a list of extra makefiles for the sub-@code{make} to read
4323 before the usual or specified ones.  @xref{MAKEFILES Variable, ,The
4324 Variable @code{MAKEFILES}}.@refill
4326 @node Options/Recursion, -w Option, Variables/Recursion, Recursion
4327 @subsection Communicating Options to a Sub-@code{make}
4328 @cindex options, and recursion
4329 @cindex recursion, and options
4331 @vindex MAKEFLAGS
4332 Flags such as @samp{-s} and @samp{-k} are passed automatically to the
4333 sub-@code{make} through the variable @code{MAKEFLAGS}.  This variable is
4334 set up automatically by @code{make} to contain the flag letters that
4335 @code{make} received.  Thus, if you do @w{@samp{make -ks}} then
4336 @code{MAKEFLAGS} gets the value @samp{ks}.@refill
4338 As a consequence, every sub-@code{make} gets a value for @code{MAKEFLAGS}
4339 in its environment.  In response, it takes the flags from that value and
4340 processes them as if they had been given as arguments.
4341 @xref{Options Summary, ,Summary of Options}.
4343 @cindex command line variable definitions, and recursion
4344 @cindex variables, command line, and recursion
4345 @cindex recursion, and command line variable definitions
4346 Likewise variables defined on the command line are passed to the
4347 sub-@code{make} through @code{MAKEFLAGS}.  Words in the value of
4348 @code{MAKEFLAGS} that contain @samp{=}, @code{make} treats as variable
4349 definitions just as if they appeared on the command line.
4350 @xref{Overriding, ,Overriding Variables}.
4352 @cindex @code{-C}, and recursion
4353 @cindex @code{-f}, and recursion
4354 @cindex @code{-o}, and recursion
4355 @cindex @code{-W}, and recursion
4356 @cindex @code{--directory}, and recursion
4357 @cindex @code{--file}, and recursion
4358 @cindex @code{--old-file}, and recursion
4359 @cindex @code{--assume-old}, and recursion
4360 @cindex @code{--assume-new}, and recursion
4361 @cindex @code{--new-file}, and recursion
4362 @cindex recursion, and @code{-C}
4363 @cindex recursion, and @code{-f}
4364 @cindex recursion, and @code{-o}
4365 @cindex recursion, and @code{-W}
4366 The options @samp{-C}, @samp{-f}, @samp{-o}, and @samp{-W} are not put
4367 into @code{MAKEFLAGS}; these options are not passed down.@refill
4369 @cindex @code{-j}, and recursion
4370 @cindex @code{--jobs}, and recursion
4371 @cindex recursion, and @code{-j}
4372 @cindex job slots, and recursion
4373 The @samp{-j} option is a special case (@pxref{Parallel, ,Parallel Execution}).
4374 If you set it to some numeric value @samp{N} and your operating system
4375 supports it (most any UNIX system will; others typically won't), the
4376 parent @code{make} and all the sub-@code{make}s will communicate to
4377 ensure that there are only @samp{N} jobs running at the same time
4378 between them all.  Note that any job that is marked recursive
4379 (@pxref{Instead of Execution, ,Instead of Executing Recipes})
4380 doesn't count against the total jobs (otherwise we could get @samp{N}
4381 sub-@code{make}s running and have no slots left over for any real work!)
4383 If your operating system doesn't support the above communication, then
4384 @samp{-j 1} is always put into @code{MAKEFLAGS} instead of the value you
4385 specified.  This is because if the @w{@samp{-j}} option were passed down
4386 to sub-@code{make}s, you would get many more jobs running in parallel
4387 than you asked for.  If you give @samp{-j} with no numeric argument,
4388 meaning to run as many jobs as possible in parallel, this is passed
4389 down, since multiple infinities are no more than one.@refill
4391 If you do not want to pass the other flags down, you must change the
4392 value of @code{MAKEFLAGS}, like this:
4394 @example
4395 subsystem:
4396         cd subdir && $(MAKE) MAKEFLAGS=
4397 @end example
4399 @vindex MAKEOVERRIDES
4400 The command line variable definitions really appear in the variable
4401 @code{MAKEOVERRIDES}, and @code{MAKEFLAGS} contains a reference to this
4402 variable.  If you do want to pass flags down normally, but don't want to
4403 pass down the command line variable definitions, you can reset
4404 @code{MAKEOVERRIDES} to empty, like this:
4406 @example
4407 MAKEOVERRIDES =
4408 @end example
4410 @noindent
4411 @cindex Arg list too long
4412 @cindex E2BIG
4413 This is not usually useful to do.  However, some systems have a small
4414 fixed limit on the size of the environment, and putting so much
4415 information into the value of @code{MAKEFLAGS} can exceed it.  If you
4416 see the error message @samp{Arg list too long}, this may be the problem.
4417 @findex .POSIX
4418 @cindex POSIX.2
4419 (For strict compliance with POSIX.2, changing @code{MAKEOVERRIDES} does
4420 not affect @code{MAKEFLAGS} if the special target @samp{.POSIX} appears
4421 in the makefile.  You probably do not care about this.)
4423 @vindex MFLAGS
4424 A similar variable @code{MFLAGS} exists also, for historical
4425 compatibility.  It has the same value as @code{MAKEFLAGS} except that it
4426 does not contain the command line variable definitions, and it always
4427 begins with a hyphen unless it is empty (@code{MAKEFLAGS} begins with a
4428 hyphen only when it begins with an option that has no single-letter
4429 version, such as @samp{--warn-undefined-variables}).  @code{MFLAGS} was
4430 traditionally used explicitly in the recursive @code{make} command, like
4431 this:
4433 @example
4434 subsystem:
4435         cd subdir && $(MAKE) $(MFLAGS)
4436 @end example
4438 @noindent
4439 but now @code{MAKEFLAGS} makes this usage redundant.  If you want your
4440 makefiles to be compatible with old @code{make} programs, use this
4441 technique; it will work fine with more modern @code{make} versions too.
4443 @cindex setting options from environment
4444 @cindex options, setting from environment
4445 @cindex setting options in makefiles
4446 @cindex options, setting in makefiles
4447 The @code{MAKEFLAGS} variable can also be useful if you want to have
4448 certain options, such as @samp{-k} (@pxref{Options Summary, ,Summary of
4449 Options}), set each time you run @code{make}.  You simply put a value for
4450 @code{MAKEFLAGS} in your environment.  You can also set @code{MAKEFLAGS} in
4451 a makefile, to specify additional flags that should also be in effect for
4452 that makefile.  (Note that you cannot use @code{MFLAGS} this way.  That
4453 variable is set only for compatibility; @code{make} does not interpret a
4454 value you set for it in any way.)
4456 When @code{make} interprets the value of @code{MAKEFLAGS} (either from the
4457 environment or from a makefile), it first prepends a hyphen if the value
4458 does not already begin with one.  Then it chops the value into words
4459 separated by blanks, and parses these words as if they were options given
4460 on the command line (except that @samp{-C}, @samp{-f}, @samp{-h},
4461 @samp{-o}, @samp{-W}, and their long-named versions are ignored; and there
4462 is no error for an invalid option).
4464 If you do put @code{MAKEFLAGS} in your environment, you should be sure not
4465 to include any options that will drastically affect the actions of
4466 @code{make} and undermine the purpose of makefiles and of @code{make}
4467 itself.  For instance, the @samp{-t}, @samp{-n}, and @samp{-q} options, if
4468 put in one of these variables, could have disastrous consequences and would
4469 certainly have at least surprising and probably annoying effects.@refill
4471 @node -w Option,  , Options/Recursion, Recursion
4472 @subsection The @samp{--print-directory} Option
4473 @cindex directories, printing them
4474 @cindex printing directories
4475 @cindex recursion, and printing directories
4477 If you use several levels of recursive @code{make} invocations, the
4478 @samp{-w} or @w{@samp{--print-directory}} option can make the output a
4479 lot easier to understand by showing each directory as @code{make}
4480 starts processing it and as @code{make} finishes processing it.  For
4481 example, if @samp{make -w} is run in the directory @file{/u/gnu/make},
4482 @code{make} will print a line of the form:@refill
4484 @example
4485 make: Entering directory `/u/gnu/make'.
4486 @end example
4488 @noindent
4489 before doing anything else, and a line of the form:
4491 @example
4492 make: Leaving directory `/u/gnu/make'.
4493 @end example
4495 @noindent
4496 when processing is completed.
4498 @cindex @code{-C}, and @code{-w}
4499 @cindex @code{--directory}, and @code{--print-directory}
4500 @cindex recursion, and @code{-w}
4501 @cindex @code{-w}, and @code{-C}
4502 @cindex @code{-w}, and recursion
4503 @cindex @code{--print-directory}, and @code{--directory}
4504 @cindex @code{--print-directory}, and recursion
4505 @cindex @code{--no-print-directory}
4506 @cindex @code{--print-directory}, disabling
4507 @cindex @code{-w}, disabling
4508 Normally, you do not need to specify this option because @samp{make}
4509 does it for you: @samp{-w} is turned on automatically when you use the
4510 @samp{-C} option, and in sub-@code{make}s.  @code{make} will not
4511 automatically turn on @samp{-w} if you also use @samp{-s}, which says to
4512 be silent, or if you use @samp{--no-print-directory} to explicitly
4513 disable it.
4515 @node Canned Recipes, Empty Recipes, Recursion, Recipes
4516 @section Defining Canned Recipes
4517 @cindex canned recipes
4518 @cindex recipes, canned
4519 @cindex sequences of commands
4520 @cindex commands, sequences of
4522 When the same sequence of commands is useful in making various
4523 targets, you can define it as a canned sequence with the @code{define}
4524 directive, and refer to the canned sequence from the recipes for those
4525 targets.  The canned sequence is actually a variable, so the name must
4526 not conflict with other variable names.
4528 Here is an example of defining a canned recipe:
4530 @example
4531 define run-yacc =
4532 yacc $(firstword $^)
4533 mv y.tab.c $@@
4534 endef
4535 @end example
4536 @cindex @code{yacc}
4538 @noindent
4539 Here @code{run-yacc} is the name of the variable being defined;
4540 @code{endef} marks the end of the definition; the lines in between are the
4541 commands.  The @code{define} directive does not expand variable references
4542 and function calls in the canned sequence; the @samp{$} characters,
4543 parentheses, variable names, and so on, all become part of the value of the
4544 variable you are defining.
4545 @xref{Multi-Line, ,Defining Multi-Line Variables},
4546 for a complete explanation of @code{define}.
4548 The first command in this example runs Yacc on the first prerequisite of
4549 whichever rule uses the canned sequence.  The output file from Yacc is
4550 always named @file{y.tab.c}.  The second command moves the output to the
4551 rule's target file name.
4553 To use the canned sequence, substitute the variable into the recipe of a
4554 rule.  You can substitute it like any other variable
4555 (@pxref{Reference, ,Basics of Variable References}).
4556 Because variables defined by @code{define} are recursively expanded
4557 variables, all the variable references you wrote inside the @code{define}
4558 are expanded now.  For example:
4560 @example
4561 foo.c : foo.y
4562         $(run-yacc)
4563 @end example
4565 @noindent
4566 @samp{foo.y} will be substituted for the variable @samp{$^} when it occurs in
4567 @code{run-yacc}'s value, and @samp{foo.c} for @samp{$@@}.@refill
4569 This is a realistic example, but this particular one is not needed in
4570 practice because @code{make} has an implicit rule to figure out these
4571 commands based on the file names involved
4572 (@pxref{Implicit Rules, ,Using Implicit Rules}).
4574 @cindex @@, and @code{define}
4575 @cindex -, and @code{define}
4576 @cindex +, and @code{define}
4577 In recipe execution, each line of a canned sequence is treated just as
4578 if the line appeared on its own in the rule, preceded by a tab.  In
4579 particular, @code{make} invokes a separate subshell for each line.  You
4580 can use the special prefix characters that affect command lines
4581 (@samp{@@}, @samp{-}, and @samp{+}) on each line of a canned sequence.
4582 @xref{Recipes, ,Writing Recipes in Rules}.
4583 For example, using this canned sequence:
4585 @example
4586 define frobnicate =
4587 @@echo "frobnicating target $@@"
4588 frob-step-1 $< -o $@@-step-1
4589 frob-step-2 $@@-step-1 -o $@@
4590 endef
4591 @end example
4593 @noindent
4594 @code{make} will not echo the first line, the @code{echo} command.
4595 But it @emph{will} echo the following two recipe lines.
4597 On the other hand, prefix characters on the recipe line that refers to
4598 a canned sequence apply to every line in the sequence.  So the rule:
4600 @example
4601 frob.out: frob.in
4602         @@$(frobnicate)
4603 @end example
4605 @noindent
4606 does not echo @emph{any} recipe lines.
4607 (@xref{Echoing, ,Recipe Echoing}, for a full explanation of @samp{@@}.)
4609 @node Empty Recipes,  , Canned Recipes, Recipes
4610 @section Using Empty Recipes
4611 @cindex empty recipes
4612 @cindex recipes, empty
4614 It is sometimes useful to define recipes which do nothing.  This is done
4615 simply by giving a recipe that consists of nothing but whitespace.  For
4616 example:
4618 @example
4619 target: ;
4620 @end example
4622 @noindent
4623 defines an empty recipe for @file{target}.  You could also use a line
4624 beginning with a recipe prefix character to define an empty recipe,
4625 but this would be confusing because such a line looks empty.
4627 @findex .DEFAULT@r{, and empty recipes}
4628 You may be wondering why you would want to define a recipe that
4629 does nothing.  The only reason this is useful is to prevent a target
4630 from getting implicit recipes (from implicit rules or the
4631 @code{.DEFAULT} special target; @pxref{Implicit Rules} and
4632 @pxref{Last Resort, ,Defining Last-Resort Default Rules}).@refill
4634 @c !!! another reason is for canonical stamp files:
4635 @ignore
4636 @example
4637 foo: stamp-foo ;
4638 stamp-foo: foo.in
4639         create foo frm foo.in
4640         touch $@
4641 @end example
4642 @end ignore
4644 You may be inclined to define empty recipes for targets that are
4645 not actual files, but only exist so that their prerequisites can be
4646 remade.  However, this is not the best way to do that, because the
4647 prerequisites may not be remade properly if the target file actually does exist.
4648 @xref{Phony Targets, ,Phony Targets}, for a better way to do this.
4650 @node Using Variables, Conditionals, Recipes, Top
4651 @chapter How to Use Variables
4652 @cindex variable
4653 @cindex value
4654 @cindex recursive variable expansion
4655 @cindex simple variable expansion
4657 A @dfn{variable} is a name defined in a makefile to represent a string
4658 of text, called the variable's @dfn{value}.  These values are
4659 substituted by explicit request into targets, prerequisites, recipes,
4660 and other parts of the makefile.  (In some other versions of @code{make},
4661 variables are called @dfn{macros}.)
4662 @cindex macro
4664 Variables and functions in all parts of a makefile are expanded when
4665 read, except for in recipes, the right-hand sides of variable
4666 definitions using @samp{=}, and the bodies of variable definitions
4667 using the @code{define} directive.@refill
4669 Variables can represent lists of file names, options to pass to compilers,
4670 programs to run, directories to look in for source files, directories to
4671 write output in, or anything else you can imagine.
4673 A variable name may be any sequence of characters not containing @samp{:},
4674 @samp{#}, @samp{=}, or leading or trailing whitespace.  However,
4675 variable names containing characters other than letters, numbers, and
4676 underscores should be avoided, as they may be given special meanings in the
4677 future, and with some shells they cannot be passed through the environment to a
4678 sub-@code{make}
4679 (@pxref{Variables/Recursion, ,Communicating Variables to a Sub-@code{make}}).
4681 Variable names are case-sensitive.  The names @samp{foo}, @samp{FOO},
4682 and @samp{Foo} all refer to different variables.
4684 It is traditional to use upper case letters in variable names, but we
4685 recommend using lower case letters for variable names that serve internal
4686 purposes in the makefile, and reserving upper case for parameters that
4687 control implicit rules or for parameters that the user should override with
4688 command options (@pxref{Overriding, ,Overriding Variables}).
4690 A few variables have names that are a single punctuation character or
4691 just a few characters.  These are the @dfn{automatic variables}, and
4692 they have particular specialized uses.  @xref{Automatic Variables}.
4694 @menu
4695 * Reference::                   How to use the value of a variable.
4696 * Flavors::                     Variables come in two flavors.
4697 * Advanced::                    Advanced features for referencing a variable.
4698 * Values::                      All the ways variables get their values.
4699 * Setting::                     How to set a variable in the makefile.
4700 * Appending::                   How to append more text to the old value
4701                                   of a variable.
4702 * Override Directive::          How to set a variable in the makefile even if
4703                                   the user has set it with a command argument.
4704 * Multi-Line::                  An alternate way to set a variable
4705                                   to a multi-line string.
4706 * Environment::                 Variable values can come from the environment.
4707 * Target-specific::             Variable values can be defined on a per-target
4708                                   basis.
4709 * Pattern-specific::            Target-specific variable values can be applied
4710                                   to a group of targets that match a pattern.
4711 * Suppressing Inheritance::     Suppress inheritance of variables.
4712 * Special Variables::           Variables with special meaning or behavior.
4713 @end menu
4715 @node Reference, Flavors, Using Variables, Using Variables
4716 @section Basics of Variable References
4717 @cindex variables, how to reference
4718 @cindex reference to variables
4719 @cindex @code{$}, in variable reference
4720 @cindex dollar sign (@code{$}), in variable reference
4722 To substitute a variable's value, write a dollar sign followed by the name
4723 of the variable in parentheses or braces: either @samp{$(foo)} or
4724 @samp{$@{foo@}} is a valid reference to the variable @code{foo}.  This
4725 special significance of @samp{$} is why you must write @samp{$$} to have
4726 the effect of a single dollar sign in a file name or recipe.
4728 Variable references can be used in any context: targets, prerequisites,
4729 recipes, most directives, and new variable values.  Here is an
4730 example of a common case, where a variable holds the names of all the
4731 object files in a program:
4733 @example
4734 @group
4735 objects = program.o foo.o utils.o
4736 program : $(objects)
4737         cc -o program $(objects)
4739 $(objects) : defs.h
4740 @end group
4741 @end example
4743 Variable references work by strict textual substitution.  Thus, the rule
4745 @example
4746 @group
4747 foo = c
4748 prog.o : prog.$(foo)
4749         $(foo)$(foo) -$(foo) prog.$(foo)
4750 @end group
4751 @end example
4753 @noindent
4754 could be used to compile a C program @file{prog.c}.  Since spaces before
4755 the variable value are ignored in variable assignments, the value of
4756 @code{foo} is precisely @samp{c}.  (Don't actually write your makefiles
4757 this way!)
4759 A dollar sign followed by a character other than a dollar sign,
4760 open-parenthesis or open-brace treats that single character as the
4761 variable name.  Thus, you could reference the variable @code{x} with
4762 @samp{$x}.  However, this practice is strongly discouraged, except in
4763 the case of the automatic variables (@pxref{Automatic Variables}).
4765 @node Flavors, Advanced, Reference, Using Variables
4766 @section The Two Flavors of Variables
4767 @cindex flavors of variables
4768 @cindex recursive variable expansion
4769 @cindex variables, flavors
4770 @cindex recursively expanded variables
4771 @cindex variables, recursively expanded
4773 There are two ways that a variable in GNU @code{make} can have a value;
4774 we call them the two @dfn{flavors} of variables.  The two flavors are
4775 distinguished in how they are defined and in what they do when expanded.
4777 @cindex =
4778 The first flavor of variable is a @dfn{recursively expanded} variable.
4779 Variables of this sort are defined by lines using @samp{=}
4780 (@pxref{Setting, ,Setting Variables}) or by the @code{define} directive
4781 (@pxref{Multi-Line, ,Defining Multi-Line Variables}).  The value you specify
4782 is installed verbatim; if it contains references to other variables,
4783 these references are expanded whenever this variable is substituted (in
4784 the course of expanding some other string).  When this happens, it is
4785 called @dfn{recursive expansion}.@refill
4787 For example,
4789 @example
4790 foo = $(bar)
4791 bar = $(ugh)
4792 ugh = Huh?
4794 all:;echo $(foo)
4795 @end example
4797 @noindent
4798 will echo @samp{Huh?}: @samp{$(foo)} expands to @samp{$(bar)} which
4799 expands to @samp{$(ugh)} which finally expands to @samp{Huh?}.@refill
4801 This flavor of variable is the only sort supported by other versions of
4802 @code{make}.  It has its advantages and its disadvantages.  An advantage
4803 (most would say) is that:
4805 @example
4806 CFLAGS = $(include_dirs) -O
4807 include_dirs = -Ifoo -Ibar
4808 @end example
4810 @noindent
4811 will do what was intended: when @samp{CFLAGS} is expanded in a recipe,
4812 it will expand to @samp{-Ifoo -Ibar -O}.  A major disadvantage is that you
4813 cannot append something on the end of a variable, as in
4815 @example
4816 CFLAGS = $(CFLAGS) -O
4817 @end example
4819 @noindent
4820 because it will cause an infinite loop in the variable expansion.
4821 (Actually @code{make} detects the infinite loop and reports an error.)
4822 @cindex loops in variable expansion
4823 @cindex variables, loops in expansion
4825 Another disadvantage is that any functions
4826 (@pxref{Functions, ,Functions for Transforming Text})
4827 referenced in the definition will be executed every time the variable is
4828 expanded.  This makes @code{make} run slower; worse, it causes the
4829 @code{wildcard} and @code{shell} functions to give unpredictable results
4830 because you cannot easily control when they are called, or even how many
4831 times.
4833 To avoid all the problems and inconveniences of recursively expanded
4834 variables, there is another flavor: simply expanded variables.
4836 @cindex simply expanded variables
4837 @cindex variables, simply expanded
4838 @cindex :=
4839 @dfn{Simply expanded variables} are defined by lines using @samp{:=}
4840 (@pxref{Setting, ,Setting Variables}).
4841 The value of a simply expanded variable is scanned
4842 once and for all, expanding any references to other variables and
4843 functions, when the variable is defined.  The actual value of the simply
4844 expanded variable is the result of expanding the text that you write.
4845 It does not contain any references to other variables; it contains their
4846 values @emph{as of the time this variable was defined}.  Therefore,
4848 @example
4849 x := foo
4850 y := $(x) bar
4851 x := later
4852 @end example
4854 @noindent
4855 is equivalent to
4857 @example
4858 y := foo bar
4859 x := later
4860 @end example
4862 When a simply expanded variable is referenced, its value is substituted
4863 verbatim.
4865 Here is a somewhat more complicated example, illustrating the use of
4866 @samp{:=} in conjunction with the @code{shell} function.
4867 (@xref{Shell Function, , The @code{shell} Function}.)  This example
4868 also shows use of the variable @code{MAKELEVEL}, which is changed
4869 when it is passed down from level to level.
4870 (@xref{Variables/Recursion, , Communicating Variables to a
4871 Sub-@code{make}}, for information about @code{MAKELEVEL}.)
4873 @vindex MAKELEVEL
4874 @vindex MAKE
4875 @example
4876 @group
4877 ifeq (0,$@{MAKELEVEL@})
4878 whoami    := $(shell whoami)
4879 host-type := $(shell arch)
4880 MAKE := $@{MAKE@} host-type=$@{host-type@} whoami=$@{whoami@}
4881 endif
4882 @end group
4883 @end example
4885 @noindent
4886 An advantage of this use of @samp{:=} is that a typical
4887 `descend into a directory' recipe then looks like this:
4889 @example
4890 @group
4891 $@{subdirs@}:
4892         $@{MAKE@} -C $@@ all
4893 @end group
4894 @end example
4896 Simply expanded variables generally make complicated makefile programming
4897 more predictable because they work like variables in most programming
4898 languages.  They allow you to redefine a variable using its own value (or
4899 its value processed in some way by one of the expansion functions) and to
4900 use the expansion functions much more efficiently
4901 (@pxref{Functions, ,Functions for Transforming Text}).
4903 @cindex spaces, in variable values
4904 @cindex whitespace, in variable values
4905 @cindex variables, spaces in values
4906 You can also use them to introduce controlled leading whitespace into
4907 variable values.  Leading whitespace characters are discarded from your
4908 input before substitution of variable references and function calls;
4909 this means you can include leading spaces in a variable value by
4910 protecting them with variable references, like this:
4912 @example
4913 nullstring :=
4914 space := $(nullstring) # end of the line
4915 @end example
4917 @noindent
4918 Here the value of the variable @code{space} is precisely one space.  The
4919 comment @w{@samp{# end of the line}} is included here just for clarity.
4920 Since trailing space characters are @emph{not} stripped from variable
4921 values, just a space at the end of the line would have the same effect
4922 (but be rather hard to read).  If you put whitespace at the end of a
4923 variable value, it is a good idea to put a comment like that at the end
4924 of the line to make your intent clear.  Conversely, if you do @emph{not}
4925 want any whitespace characters at the end of your variable value, you
4926 must remember not to put a random comment on the end of the line after
4927 some whitespace, such as this:
4929 @example
4930 dir := /foo/bar    # directory to put the frobs in
4931 @end example
4933 @noindent
4934 Here the value of the variable @code{dir} is @w{@samp{/foo/bar    }}
4935 (with four trailing spaces), which was probably not the intention.
4936 (Imagine something like @w{@samp{$(dir)/file}} with this definition!)
4938 @cindex conditional variable assignment
4939 @cindex variables, conditional assignment
4940 @cindex ?=
4941 There is another assignment operator for variables, @samp{?=}.  This
4942 is called a conditional variable assignment operator, because it only
4943 has an effect if the variable is not yet defined.  This statement:
4945 @example
4946 FOO ?= bar
4947 @end example
4949 @noindent
4950 is exactly equivalent to this
4951 (@pxref{Origin Function, ,The @code{origin} Function}):
4953 @example
4954 ifeq ($(origin FOO), undefined)
4955   FOO = bar
4956 endif
4957 @end example
4959 Note that a variable set to an empty value is still defined, so
4960 @samp{?=} will not set that variable.
4962 @node Advanced, Values, Flavors, Using Variables
4963 @section Advanced Features for Reference to Variables
4964 @cindex reference to variables
4966 This section describes some advanced features you can use to reference
4967 variables in more flexible ways.
4969 @menu
4970 * Substitution Refs::           Referencing a variable with
4971                                   substitutions on the value.
4972 * Computed Names::              Computing the name of the variable to refer to.
4973 @end menu
4975 @node Substitution Refs, Computed Names, Advanced, Advanced
4976 @subsection Substitution References
4977 @cindex modified variable reference
4978 @cindex substitution variable reference
4979 @cindex variables, modified reference
4980 @cindex variables, substitution reference
4982 @cindex variables, substituting suffix in
4983 @cindex suffix, substituting in variables
4984 A @dfn{substitution reference} substitutes the value of a variable with
4985 alterations that you specify.  It has the form
4986 @samp{$(@var{var}:@var{a}=@var{b})} (or
4987 @samp{$@{@var{var}:@var{a}=@var{b}@}}) and its meaning is to take the value
4988 of the variable @var{var}, replace every @var{a} at the end of a word with
4989 @var{b} in that value, and substitute the resulting string.
4991 When we say ``at the end of a word'', we mean that @var{a} must appear
4992 either followed by whitespace or at the end of the value in order to be
4993 replaced; other occurrences of @var{a} in the value are unaltered.  For
4994 example:@refill
4996 @example
4997 foo := a.o b.o c.o
4998 bar := $(foo:.o=.c)
4999 @end example
5001 @noindent
5002 sets @samp{bar} to @samp{a.c b.c c.c}.  @xref{Setting, ,Setting Variables}.
5004 A substitution reference is actually an abbreviation for use of the
5005 @code{patsubst} expansion function (@pxref{Text Functions, ,Functions for String Substitution and Analysis}).  We provide
5006 substitution references as well as @code{patsubst} for compatibility with
5007 other implementations of @code{make}.
5009 @findex patsubst
5010 Another type of substitution reference lets you use the full power of
5011 the @code{patsubst} function.  It has the same form
5012 @samp{$(@var{var}:@var{a}=@var{b})} described above, except that now
5013 @var{a} must contain a single @samp{%} character.  This case is
5014 equivalent to @samp{$(patsubst @var{a},@var{b},$(@var{var}))}.
5015 @xref{Text Functions, ,Functions for String Substitution and Analysis},
5016 for a description of the @code{patsubst} function.@refill
5018 @example
5019 @group
5020 @exdent For example:
5022 foo := a.o b.o c.o
5023 bar := $(foo:%.o=%.c)
5024 @end group
5025 @end example
5027 @noindent
5028 sets @samp{bar} to @samp{a.c b.c c.c}.
5030 @node Computed Names,  , Substitution Refs, Advanced
5031 @subsection Computed Variable Names
5032 @cindex nested variable reference
5033 @cindex computed variable name
5034 @cindex variables, computed names
5035 @cindex variables, nested references
5036 @cindex variables, @samp{$} in name
5037 @cindex @code{$}, in variable name
5038 @cindex dollar sign (@code{$}), in variable name
5040 Computed variable names are a complicated concept needed only for
5041 sophisticated makefile programming.  For most purposes you need not
5042 consider them, except to know that making a variable with a dollar sign
5043 in its name might have strange results.  However, if you are the type
5044 that wants to understand everything, or you are actually interested in
5045 what they do, read on.
5047 Variables may be referenced inside the name of a variable.  This is
5048 called a @dfn{computed variable name} or a @dfn{nested variable
5049 reference}.  For example,
5051 @example
5052 x = y
5053 y = z
5054 a := $($(x))
5055 @end example
5057 @noindent
5058 defines @code{a} as @samp{z}: the @samp{$(x)} inside @samp{$($(x))} expands
5059 to @samp{y}, so @samp{$($(x))} expands to @samp{$(y)} which in turn expands
5060 to @samp{z}.  Here the name of the variable to reference is not stated
5061 explicitly; it is computed by expansion of @samp{$(x)}.  The reference
5062 @samp{$(x)} here is nested within the outer variable reference.
5064 The previous example shows two levels of nesting, but any number of levels
5065 is possible.  For example, here are three levels:
5067 @example
5068 x = y
5069 y = z
5070 z = u
5071 a := $($($(x)))
5072 @end example
5074 @noindent
5075 Here the innermost @samp{$(x)} expands to @samp{y}, so @samp{$($(x))}
5076 expands to @samp{$(y)} which in turn expands to @samp{z}; now we have
5077 @samp{$(z)}, which becomes @samp{u}.
5079 References to recursively-expanded variables within a variable name are
5080 reexpanded in the usual fashion.  For example:
5082 @example
5083 x = $(y)
5084 y = z
5085 z = Hello
5086 a := $($(x))
5087 @end example
5089 @noindent
5090 defines @code{a} as @samp{Hello}: @samp{$($(x))} becomes @samp{$($(y))}
5091 which becomes @samp{$(z)} which becomes @samp{Hello}.
5093 Nested variable references can also contain modified references and
5094 function invocations (@pxref{Functions, ,Functions for Transforming Text}),
5095 just like any other reference.
5096 For example, using the @code{subst} function
5097 (@pxref{Text Functions, ,Functions for String Substitution and Analysis}):
5099 @example
5100 @group
5101 x = variable1
5102 variable2 := Hello
5103 y = $(subst 1,2,$(x))
5104 z = y
5105 a := $($($(z)))
5106 @end group
5107 @end example
5109 @noindent
5110 eventually defines @code{a} as @samp{Hello}.  It is doubtful that anyone
5111 would ever want to write a nested reference as convoluted as this one, but
5112 it works: @samp{$($($(z)))} expands to @samp{$($(y))} which becomes
5113 @samp{$($(subst 1,2,$(x)))}.  This gets the value @samp{variable1} from
5114 @code{x} and changes it by substitution to @samp{variable2}, so that the
5115 entire string becomes @samp{$(variable2)}, a simple variable reference
5116 whose value is @samp{Hello}.@refill
5118 A computed variable name need not consist entirely of a single variable
5119 reference.  It can contain several variable references, as well as some
5120 invariant text.  For example,
5122 @example
5123 @group
5124 a_dirs := dira dirb
5125 1_dirs := dir1 dir2
5126 @end group
5128 @group
5129 a_files := filea fileb
5130 1_files := file1 file2
5131 @end group
5133 @group
5134 ifeq "$(use_a)" "yes"
5135 a1 := a
5136 else
5137 a1 := 1
5138 endif
5139 @end group
5141 @group
5142 ifeq "$(use_dirs)" "yes"
5143 df := dirs
5144 else
5145 df := files
5146 endif
5148 dirs := $($(a1)_$(df))
5149 @end group
5150 @end example
5152 @noindent
5153 will give @code{dirs} the same value as @code{a_dirs}, @code{1_dirs},
5154 @code{a_files} or @code{1_files} depending on the settings of @code{use_a}
5155 and @code{use_dirs}.@refill
5157 Computed variable names can also be used in substitution references:
5159 @example
5160 @group
5161 a_objects := a.o b.o c.o
5162 1_objects := 1.o 2.o 3.o
5164 sources := $($(a1)_objects:.o=.c)
5165 @end group
5166 @end example
5168 @noindent
5169 defines @code{sources} as either @samp{a.c b.c c.c} or @samp{1.c 2.c 3.c},
5170 depending on the value of @code{a1}.
5172 The only restriction on this sort of use of nested variable references
5173 is that they cannot specify part of the name of a function to be called.
5174 This is because the test for a recognized function name is done before
5175 the expansion of nested references.  For example,
5177 @example
5178 @group
5179 ifdef do_sort
5180 func := sort
5181 else
5182 func := strip
5183 endif
5184 @end group
5186 @group
5187 bar := a d b g q c
5188 @end group
5190 @group
5191 foo := $($(func) $(bar))
5192 @end group
5193 @end example
5195 @noindent
5196 attempts to give @samp{foo} the value of the variable @samp{sort a d b g
5197 q c} or @samp{strip a d b g q c}, rather than giving @samp{a d b g q c}
5198 as the argument to either the @code{sort} or the @code{strip} function.
5199 This restriction could be removed in the future if that change is shown
5200 to be a good idea.
5202 You can also use computed variable names in the left-hand side of a
5203 variable assignment, or in a @code{define} directive, as in:
5205 @example
5206 dir = foo
5207 $(dir)_sources := $(wildcard $(dir)/*.c)
5208 define $(dir)_print =
5209 lpr $($(dir)_sources)
5210 endef
5211 @end example
5213 @noindent
5214 This example defines the variables @samp{dir}, @samp{foo_sources}, and
5215 @samp{foo_print}.
5217 Note that @dfn{nested variable references} are quite different from
5218 @dfn{recursively expanded variables}
5219 (@pxref{Flavors, ,The Two Flavors of Variables}), though both are
5220 used together in complex ways when doing makefile programming.@refill
5222 @node Values, Setting, Advanced, Using Variables
5223 @section How Variables Get Their Values
5224 @cindex variables, how they get their values
5225 @cindex value, how a variable gets it
5227 Variables can get values in several different ways:
5229 @itemize @bullet
5230 @item
5231 You can specify an overriding value when you run @code{make}.
5232 @xref{Overriding, ,Overriding Variables}.
5234 @item
5235 You can specify a value in the makefile, either
5236 with an assignment (@pxref{Setting, ,Setting Variables}) or with a
5237 verbatim definition (@pxref{Multi-Line, ,Defining Multi-Line Variables}).@refill
5239 @item
5240 Variables in the environment become @code{make} variables.
5241 @xref{Environment, ,Variables from the Environment}.
5243 @item
5244 Several @dfn{automatic} variables are given new values for each rule.
5245 Each of these has a single conventional use.
5246 @xref{Automatic Variables}.
5248 @item
5249 Several variables have constant initial values.
5250 @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
5251 @end itemize
5253 @node Setting, Appending, Values, Using Variables
5254 @section Setting Variables
5255 @cindex setting variables
5256 @cindex variables, setting
5257 @cindex =
5258 @cindex :=
5259 @cindex ?=
5261 To set a variable from the makefile, write a line starting with the
5262 variable name followed by @samp{=} or @samp{:=}.  Whatever follows the
5263 @samp{=} or @samp{:=} on the line becomes the value.  For example,
5265 @example
5266 objects = main.o foo.o bar.o utils.o
5267 @end example
5269 @noindent
5270 defines a variable named @code{objects}.  Whitespace around the variable
5271 name and immediately after the @samp{=} is ignored.
5273 Variables defined with @samp{=} are @dfn{recursively expanded} variables.
5274 Variables defined with @samp{:=} are @dfn{simply expanded} variables; these
5275 definitions can contain variable references which will be expanded before
5276 the definition is made.  @xref{Flavors, ,The Two Flavors of Variables}.
5278 The variable name may contain function and variable references, which
5279 are expanded when the line is read to find the actual variable name to use.
5281 There is no limit on the length of the value of a variable except the
5282 amount of swapping space on the computer.  When a variable definition is
5283 long, it is a good idea to break it into several lines by inserting
5284 backslash-newline at convenient places in the definition.  This will not
5285 affect the functioning of @code{make}, but it will make the makefile easier
5286 to read.
5288 Most variable names are considered to have the empty string as a value if
5289 you have never set them.  Several variables have built-in initial values
5290 that are not empty, but you can set them in the usual ways
5291 (@pxref{Implicit Variables, ,Variables Used by Implicit Rules}).
5292 Several special variables are set
5293 automatically to a new value for each rule; these are called the
5294 @dfn{automatic} variables (@pxref{Automatic Variables}).
5296 If you'd like a variable to be set to a value only if it's not already
5297 set, then you can use the shorthand operator @samp{?=} instead of
5298 @samp{=}.  These two settings of the variable @samp{FOO} are identical
5299 (@pxref{Origin Function, ,The @code{origin} Function}):
5301 @example
5302 FOO ?= bar
5303 @end example
5305 @noindent
5308 @example
5309 ifeq ($(origin FOO), undefined)
5310 FOO = bar
5311 endif
5312 @end example
5314 @node Appending, Override Directive, Setting, Using Variables
5315 @section Appending More Text to Variables
5316 @cindex +=
5317 @cindex appending to variables
5318 @cindex variables, appending to
5320 Often it is useful to add more text to the value of a variable already defined.
5321 You do this with a line containing @samp{+=}, like this:
5323 @example
5324 objects += another.o
5325 @end example
5327 @noindent
5328 This takes the value of the variable @code{objects}, and adds the text
5329 @samp{another.o} to it (preceded by a single space).  Thus:
5331 @example
5332 objects = main.o foo.o bar.o utils.o
5333 objects += another.o
5334 @end example
5336 @noindent
5337 sets @code{objects} to @samp{main.o foo.o bar.o utils.o another.o}.
5339 Using @samp{+=} is similar to:
5341 @example
5342 objects = main.o foo.o bar.o utils.o
5343 objects := $(objects) another.o
5344 @end example
5346 @noindent
5347 but differs in ways that become important when you use more complex values.
5349 When the variable in question has not been defined before, @samp{+=}
5350 acts just like normal @samp{=}: it defines a recursively-expanded
5351 variable.  However, when there @emph{is} a previous definition, exactly
5352 what @samp{+=} does depends on what flavor of variable you defined
5353 originally.  @xref{Flavors, ,The Two Flavors of Variables}, for an
5354 explanation of the two flavors of variables.
5356 When you add to a variable's value with @samp{+=}, @code{make} acts
5357 essentially as if you had included the extra text in the initial
5358 definition of the variable.  If you defined it first with @samp{:=},
5359 making it a simply-expanded variable, @samp{+=} adds to that
5360 simply-expanded definition, and expands the new text before appending it
5361 to the old value just as @samp{:=} does
5362 (see @ref{Setting, ,Setting Variables}, for a full explanation of @samp{:=}).
5363 In fact,
5365 @example
5366 variable := value
5367 variable += more
5368 @end example
5370 @noindent
5371 is exactly equivalent to:
5373 @noindent
5374 @example
5375 variable := value
5376 variable := $(variable) more
5377 @end example
5379 On the other hand, when you use @samp{+=} with a variable that you defined
5380 first to be recursively-expanded using plain @samp{=}, @code{make} does
5381 something a bit different.  Recall that when you define a
5382 recursively-expanded variable, @code{make} does not expand the value you set
5383 for variable and function references immediately.  Instead it stores the text
5384 verbatim, and saves these variable and function references to be expanded
5385 later, when you refer to the new variable (@pxref{Flavors, ,The Two Flavors
5386 of Variables}).  When you use @samp{+=} on a recursively-expanded variable,
5387 it is this unexpanded text to which @code{make} appends the new text you
5388 specify.
5390 @example
5391 @group
5392 variable = value
5393 variable += more
5394 @end group
5395 @end example
5397 @noindent
5398 is roughly equivalent to:
5400 @example
5401 @group
5402 temp = value
5403 variable = $(temp) more
5404 @end group
5405 @end example
5407 @noindent
5408 except that of course it never defines a variable called @code{temp}.
5409 The importance of this comes when the variable's old value contains
5410 variable references.  Take this common example:
5412 @example
5413 CFLAGS = $(includes) -O
5414 @dots{}
5415 CFLAGS += -pg # enable profiling
5416 @end example
5418 @noindent
5419 The first line defines the @code{CFLAGS} variable with a reference to another
5420 variable, @code{includes}.  (@code{CFLAGS} is used by the rules for C
5421 compilation; @pxref{Catalogue of Rules, ,Catalogue of Implicit Rules}.)
5422 Using @samp{=} for the definition makes @code{CFLAGS} a recursively-expanded
5423 variable, meaning @w{@samp{$(includes) -O}} is @emph{not} expanded when
5424 @code{make} processes the definition of @code{CFLAGS}.  Thus, @code{includes}
5425 need not be defined yet for its value to take effect.  It only has to be
5426 defined before any reference to @code{CFLAGS}.  If we tried to append to the
5427 value of @code{CFLAGS} without using @samp{+=}, we might do it like this:
5429 @example
5430 CFLAGS := $(CFLAGS) -pg # enable profiling
5431 @end example
5433 @noindent
5434 This is pretty close, but not quite what we want.  Using @samp{:=}
5435 redefines @code{CFLAGS} as a simply-expanded variable; this means
5436 @code{make} expands the text @w{@samp{$(CFLAGS) -pg}} before setting the
5437 variable.  If @code{includes} is not yet defined, we get @w{@samp{ -O
5438 -pg}}, and a later definition of @code{includes} will have no effect.
5439 Conversely, by using @samp{+=} we set @code{CFLAGS} to the
5440 @emph{unexpanded} value @w{@samp{$(includes) -O -pg}}.  Thus we preserve
5441 the reference to @code{includes}, so if that variable gets defined at
5442 any later point, a reference like @samp{$(CFLAGS)} still uses its
5443 value.
5445 @node Override Directive, Multi-Line, Appending, Using Variables
5446 @section The @code{override} Directive
5447 @findex override
5448 @cindex overriding with @code{override}
5449 @cindex variables, overriding
5451 If a variable has been set with a command argument
5452 (@pxref{Overriding, ,Overriding Variables}),
5453 then ordinary assignments in the makefile are ignored.  If you want to set
5454 the variable in the makefile even though it was set with a command
5455 argument, you can use an @code{override} directive, which is a line that
5456 looks like this:@refill
5458 @example
5459 override @var{variable} = @var{value}
5460 @end example
5462 @noindent
5465 @example
5466 override @var{variable} := @var{value}
5467 @end example
5469 To append more text to a variable defined on the command line, use:
5471 @example
5472 override @var{variable} += @var{more text}
5473 @end example
5475 @noindent
5476 @xref{Appending, ,Appending More Text to Variables}.
5478 Variable assignments marked with the @code{override} flag have a
5479 higher priority than all other assignments, except another
5480 @code{override}.  Subsequent assignments or appends to this variable
5481 which are not marked @code{override} will be ignored.
5483 The @code{override} directive was not invented for escalation in the war
5484 between makefiles and command arguments.  It was invented so you can alter
5485 and add to values that the user specifies with command arguments.
5487 For example, suppose you always want the @samp{-g} switch when you run the
5488 C compiler, but you would like to allow the user to specify the other
5489 switches with a command argument just as usual.  You could use this
5490 @code{override} directive:
5492 @example
5493 override CFLAGS += -g
5494 @end example
5496 You can also use @code{override} directives with @code{define} directives.
5497 This is done as you might expect:
5499 @example
5500 override define foo =
5502 endef
5503 @end example
5505 @noindent
5506 @iftex
5507 See the next section for information about @code{define}.
5508 @end iftex
5509 @ifnottex
5510 @xref{Multi-Line, ,Defining Multi-Line Variables}.
5511 @end ifnottex
5513 @node Multi-Line, Environment, Override Directive, Using Variables
5514 @section Defining Multi-Line Variables
5515 @findex define
5516 @findex endef
5517 @cindex multi-line variable definition
5518 @cindex variables, multi-line
5519 @cindex verbatim variable definition
5520 @cindex defining variables verbatim
5521 @cindex variables, defining verbatim
5523 Another way to set the value of a variable is to use the @code{define}
5524 directive.  This directive has an unusual syntax which allows newline
5525 characters to be included in the value, which is convenient for
5526 defining both canned sequences of commands (@pxref{Canned Recipes,
5527 ,Defining Canned Recipes}), and also sections of makefile syntax to
5528 use with @code{eval} (@pxref{Eval Function}).@refill
5530 The @code{define} directive is followed on the same line by the name
5531 of the variable being defined and an (optional) assignment operator,
5532 and nothing more.  The value to give the variable appears on the
5533 following lines.  The end of the value is marked by a line containing
5534 just the word @code{endef}.  Aside from this difference in syntax,
5535 @code{define} works just like any other variable definition.  The
5536 variable name may contain function and variable references, which are
5537 expanded when the directive is read to find the actual variable name
5538 to use.
5540 You may omit the variable assignment operator if you prefer.  If
5541 omitted, @code{make} assumes it to be @samp{=} and creates a
5542 recursively-expanded variable (@pxref{Flavors, ,The Two Flavors of Variables}).
5543 When using a @samp{+=} operator, the value is appended to the previous
5544 value as with any other append operation: with a single space
5545 separating the old and new values.
5547 You may nest @code{define} directives: @code{make} will keep track of
5548 nested directives and report an error if they are not all properly
5549 closed with @code{endef}.  Note that lines beginning with the recipe
5550 prefix character are considered part of a recipe, so any @code{define}
5551 or @code{endef} strings appearing on such a line will not be
5552 considered @code{make} directives.
5554 @example
5555 define two-lines =
5556 echo foo
5557 echo $(bar)
5558 endef
5559 @end example
5561 The value in an ordinary assignment cannot contain a newline; but the
5562 newlines that separate the lines of the value in a @code{define} become
5563 part of the variable's value (except for the final newline which precedes
5564 the @code{endef} and is not considered part of the value).@refill
5566 @need 800
5567 When used in a recipe, the previous example is functionally equivalent
5568 to this:
5570 @example
5571 two-lines = echo foo; echo $(bar)
5572 @end example
5574 @noindent
5575 since two commands separated by semicolon behave much like two separate
5576 shell commands.  However, note that using two separate lines means
5577 @code{make} will invoke the shell twice, running an independent subshell
5578 for each line.  @xref{Execution, ,Recipe Execution}.
5580 If you want variable definitions made with @code{define} to take
5581 precedence over command-line variable definitions, you can use the
5582 @code{override} directive together with @code{define}:
5584 @example
5585 override define two-lines =
5587 $(bar)
5588 endef
5589 @end example
5591 @noindent
5592 @xref{Override Directive, ,The @code{override} Directive}.
5594 @node Environment, Target-specific, Multi-Line, Using Variables
5595 @section Variables from the Environment
5597 @cindex variables, environment
5598 @cindex environment
5599 Variables in @code{make} can come from the environment in which
5600 @code{make} is run.  Every environment variable that @code{make} sees
5601 when it starts up is transformed into a @code{make} variable with the
5602 same name and value.  However, an explicit assignment in the makefile,
5603 or with a command argument, overrides the environment.  (If the
5604 @samp{-e} flag is specified, then values from the environment override
5605 assignments in the makefile.  @xref{Options Summary, ,Summary of
5606 Options}.  But this is not recommended practice.)
5608 Thus, by setting the variable @code{CFLAGS} in your environment, you can
5609 cause all C compilations in most makefiles to use the compiler switches you
5610 prefer.  This is safe for variables with standard or conventional meanings
5611 because you know that no makefile will use them for other things.  (Note
5612 this is not totally reliable; some makefiles set @code{CFLAGS} explicitly
5613 and therefore are not affected by the value in the environment.)
5615 When @code{make} runs a recipe, variables defined in the
5616 makefile are placed into the environment of each shell.  This allows
5617 you to pass values to sub-@code{make} invocations (@pxref{Recursion,
5618 ,Recursive Use of @code{make}}).  By default, only variables that came
5619 from the environment or the command line are passed to recursive
5620 invocations.  You can use the @code{export} directive to pass other
5621 variables.  @xref{Variables/Recursion, , Communicating Variables to a
5622 Sub-@code{make}}, for full details.
5624 Other use of variables from the environment is not recommended.  It is not
5625 wise for makefiles to depend for their functioning on environment variables
5626 set up outside their control, since this would cause different users to get
5627 different results from the same makefile.  This is against the whole
5628 purpose of most makefiles.
5630 @cindex SHELL, import from environment
5631 Such problems would be especially likely with the variable
5632 @code{SHELL}, which is normally present in the environment to specify
5633 the user's choice of interactive shell.  It would be very undesirable
5634 for this choice to affect @code{make}; so, @code{make} handles the
5635 @code{SHELL} environment variable in a special way; see @ref{Choosing
5636 the Shell}.@refill
5638 @node Target-specific, Pattern-specific, Environment, Using Variables
5639 @section Target-specific Variable Values
5640 @cindex target-specific variables
5641 @cindex variables, target-specific
5643 Variable values in @code{make} are usually global; that is, they are the
5644 same regardless of where they are evaluated (unless they're reset, of
5645 course).  One exception to that is automatic variables
5646 (@pxref{Automatic Variables}).
5648 The other exception is @dfn{target-specific variable values}.  This
5649 feature allows you to define different values for the same variable,
5650 based on the target that @code{make} is currently building.  As with
5651 automatic variables, these values are only available within the context
5652 of a target's recipe (and in other target-specific assignments).
5654 Set a target-specific variable value like this:
5656 @example
5657 @var{target} @dots{} : @var{variable-assignment}
5658 @end example
5660 Target-specific variable assignments can be prefixed with any or all of the
5661 special keywords @code{export}, @code{override}, or @code{private};
5662 these apply their normal behavior to this instance of the variable only.
5664 Multiple @var{target} values create a target-specific variable value for
5665 each member of the target list individually.
5667 The @var{variable-assignment} can be any valid form of assignment;
5668 recursive (@samp{=}), static (@samp{:=}), appending (@samp{+=}), or
5669 conditional (@samp{?=}).  All variables that appear within the
5670 @var{variable-assignment} are evaluated within the context of the
5671 target: thus, any previously-defined target-specific variable values
5672 will be in effect.  Note that this variable is actually distinct from
5673 any ``global'' value: the two variables do not have to have the same
5674 flavor (recursive vs.@: static).
5676 Target-specific variables have the same priority as any other makefile
5677 variable.  Variables provided on the command line (and in the
5678 environment if the @samp{-e} option is in force) will take precedence.
5679 Specifying the @code{override} directive will allow the target-specific
5680 variable value to be preferred.
5682 There is one more special feature of target-specific variables: when
5683 you define a target-specific variable that variable value is also in
5684 effect for all prerequisites of this target, and all their
5685 prerequisites, etc.@: (unless those prerequisites override that variable
5686 with their own target-specific variable value).  So, for example, a
5687 statement like this:
5689 @example
5690 prog : CFLAGS = -g
5691 prog : prog.o foo.o bar.o
5692 @end example
5694 @noindent
5695 will set @code{CFLAGS} to @samp{-g} in the recipe for @file{prog}, but
5696 it will also set @code{CFLAGS} to @samp{-g} in the recipes that create
5697 @file{prog.o}, @file{foo.o}, and @file{bar.o}, and any recipes which
5698 create their prerequisites.
5700 Be aware that a given prerequisite will only be built once per
5701 invocation of make, at most.  If the same file is a prerequisite of
5702 multiple targets, and each of those targets has a different value for
5703 the same target-specific variable, then the first target to be built
5704 will cause that prerequisite to be built and the prerequisite will
5705 inherit the target-specific value from the first target.  It will
5706 ignore the target-specific values from any other targets.
5708 @node Pattern-specific, Suppressing Inheritance, Target-specific, Using Variables
5709 @section Pattern-specific Variable Values
5710 @cindex pattern-specific variables
5711 @cindex variables, pattern-specific
5713 In addition to target-specific variable values
5714 (@pxref{Target-specific, ,Target-specific Variable Values}), GNU
5715 @code{make} supports pattern-specific variable values.  In this form,
5716 the variable is defined for any target that matches the pattern
5717 specified.  If a target matches more than one pattern, all the
5718 matching pattern-specific variables are interpreted in the order in
5719 which they were defined in the makefile, and collected together into
5720 one set.  Variables defined in this way are searched after any
5721 target-specific variables defined explicitly for that target, and
5722 before target-specific variables defined for the parent target.
5724 Set a pattern-specific variable value like this:
5726 @example
5727 @var{pattern} @dots{} : @var{variable-assignment}
5728 @end example
5729 where @var{pattern} is a %-pattern.  As with target-specific variable
5730 values, multiple @var{pattern} values create a pattern-specific variable
5731 value for each pattern individually.  The @var{variable-assignment} can
5732 be any valid form of assignment.  Any command line variable setting will
5733 take precedence, unless @code{override} is specified.
5735 For example:
5737 @example
5738 %.o : CFLAGS = -O
5739 @end example
5741 @noindent
5742 will assign @code{CFLAGS} the value of @samp{-O} for all targets
5743 matching the pattern @code{%.o}.
5745 @node Suppressing Inheritance, Special Variables, Pattern-specific, Using Variables
5746 @section Suppressing Inheritance
5747 @findex private
5748 @cindex suppressing inheritance
5749 @cindex inheritance, suppressing
5751 As described in previous sections, @code{make} variables are inherited
5752 by prerequisites.  This capability allows you to modify the behavior
5753 of a prerequisite based on which targets caused it to be rebuilt.  For
5754 example, you might set a target-specific variable on a @code{debug}
5755 target, then running @samp{make debug} will cause that variable to be
5756 inherited by all prerequisites of @code{debug}, while just running
5757 @samp{make all} (for example) would not have that assignment.
5759 Sometimes, however, you may not want a variable to be inherited.  For
5760 these situations, @code{make} provides the @code{private} modifier.
5761 Although this modifier can be used with any variable assignment, it
5762 makes the most sense with target- and pattern-specific variables.  Any
5763 variable marked @code{private} will be visible to its local target but
5764 will not be inherited by prerequisites of that target.  A global
5765 variable marked @code{private} will be visible in the global scope but
5766 will not be inherited by any target, and hence will not be visible
5767 in any recipe.
5769 As an example, consider this makefile:
5770 @example
5771 EXTRA_CFLAGS =
5773 prog: private EXTRA_CFLAGS = -L/usr/local/lib
5774 prog: a.o b.o
5775 @end example
5777 Due to the @code{private} modifier, @code{a.o} and @code{b.o} will not
5778 inherit the @code{EXTRA_CFLAGS} variable assignment from the
5779 @code{progs} target.
5781 @node Special Variables,  , Suppressing Inheritance, Using Variables
5782 @comment  node-name,  next,  previous,  up
5783 @section Other Special Variables
5784 @cindex makefiles, and special variables
5785 @cindex special variables
5787 GNU @code{make} supports some variables that have special properties.
5789 @table @code
5791 @vindex MAKEFILE_LIST @r{(list of parsed makefiles)}
5792 @cindex makefiles, and @code{MAKEFILE_LIST} variable
5793 @cindex including (@code{MAKEFILE_LIST} variable)
5794 @item MAKEFILE_LIST
5795 Contains the name of each makefile that is parsed by @code{make}, in
5796 the order in which it was parsed.  The name is appended just
5797 before @code{make} begins to parse the makefile.  Thus, if the first
5798 thing a makefile does is examine the last word in this variable, it
5799 will be the name of the current makefile.  Once the current makefile
5800 has used @code{include}, however, the last word will be the
5801 just-included makefile.
5803 If a makefile named @code{Makefile} has this content:
5805 @example
5806 @group
5807 name1 := $(lastword $(MAKEFILE_LIST))
5809 include inc.mk
5811 name2 := $(lastword $(MAKEFILE_LIST))
5813 all:
5814         @@echo name1 = $(name1)
5815         @@echo name2 = $(name2)
5816 @end group
5817 @end example
5819 @noindent
5820 then you would expect to see this output:
5822 @example
5823 @group
5824 name1 = Makefile
5825 name2 = inc.mk
5826 @end group
5827 @end example
5829 @vindex .DEFAULT_GOAL @r{(define default goal)}
5830 @item .DEFAULT_GOAL
5831 Sets the default goal to be used if no targets were specified on the
5832 command line (@pxref{Goals, , Arguments to Specify the Goals}).  The
5833 @code{.DEFAULT_GOAL} variable allows you to discover the current
5834 default goal, restart the default goal selection algorithm by clearing
5835 its value, or to explicitly set the default goal.  The following
5836 example illustrates these cases:
5838 @example
5839 @group
5840 # Query the default goal.
5841 ifeq ($(.DEFAULT_GOAL),)
5842   $(warning no default goal is set)
5843 endif
5845 .PHONY: foo
5846 foo: ; @@echo $@@
5848 $(warning default goal is $(.DEFAULT_GOAL))
5850 # Reset the default goal.
5851 .DEFAULT_GOAL :=
5853 .PHONY: bar
5854 bar: ; @@echo $@@
5856 $(warning default goal is $(.DEFAULT_GOAL))
5858 # Set our own.
5859 .DEFAULT_GOAL := foo
5860 @end group
5861 @end example
5863 This makefile prints:
5865 @example
5866 @group
5867 no default goal is set
5868 default goal is foo
5869 default goal is bar
5871 @end group
5872 @end example
5874 Note that assigning more than one target name to @code{.DEFAULT_GOAL} is
5875 illegal and will result in an error.
5877 @vindex MAKE_RESTARTS @r{(number of times @code{make} has restarted)}
5878 @item MAKE_RESTARTS
5879 This variable is set only if this instance of @code{make} has
5880 restarted (@pxref{Remaking Makefiles, , How Makefiles Are Remade}): it
5881 will contain the number of times this instance has restarted.  Note
5882 this is not the same as recursion (counted by the @code{MAKELEVEL}
5883 variable).  You should not set, modify, or export this variable.
5885 @vindex .CMDPREFIX @r{(change the recipe prefix character)}
5886 @item .CMDPREFIX
5887 The first character of the value of this variable is used as the
5888 character make assumes is introducing a recipe line.  If the variable
5889 is empty (as it is by default) that character is the standard tab
5890 character.  For example, this is a valid makefile:
5892 @example
5893 @group
5894 .CMDPREFIX = >
5895 all:
5896 > @@echo Hello, world
5897 @end group
5898 @end example
5900 The value of @code{.CMDPREFIX} can be changed multiple times; once set
5901 it stays in effect for all rules parsed until it is modified.
5903 @vindex .VARIABLES @r{(list of variables)}
5904 @item .VARIABLES
5905 Expands to a list of the @emph{names} of all global variables defined
5906 so far.  This includes variables which have empty values, as well as
5907 built-in variables (@pxref{Implicit Variables, , Variables Used by
5908 Implicit Rules}), but does not include any variables which are only
5909 defined in a target-specific context.  Note that any value you assign
5910 to this variable will be ignored; it will always return its special
5911 value.
5913 @c @vindex .TARGETS @r{(list of targets)}
5914 @c @item .TARGETS
5915 @c The second special variable is @code{.TARGETS}.  When expanded, the
5916 @c value consists of a list of all targets defined in all makefiles read
5917 @c up until that point.  Note it's not enough for a file to be simply
5918 @c mentioned in the makefile to be listed in this variable, even if it
5919 @c would match an implicit rule and become an ``implicit target''.  The
5920 @c file must appear as a target, on the left-hand side of a ``:'', to be
5921 @c considered a target for the purposes of this variable.
5923 @vindex .FEATURES @r{(list of supported features)}
5924 @item .FEATURES
5925 Expands to a list of special features supported by this version of
5926 @code{make}.  Possible values include:
5928 @table @samp
5930 @item archives
5931 Supports @code{ar} (archive) files using special filename syntax.
5932 @xref{Archives, ,Using @code{make} to Update Archive Files}.
5934 @item check-symlink
5935 Supports the @code{-L} (@code{--check-symlink-times}) flag.
5936 @xref{Options Summary, ,Summary of Options}.
5938 @item else-if
5939 Supports ``else if'' non-nested conditionals.  @xref{Conditional
5940 Syntax, ,Syntax of Conditionals}.
5942 @item jobserver
5943 Supports ``job server'' enhanced parallel builds.  @xref{Parallel,
5944 ,Parallel Execution}.
5946 @item second-expansion
5947 Supports secondary expansion of prerequisite lists.
5949 @item order-only
5950 Supports order-only prerequisites.  @xref{Prerequisite Types, ,Types
5951 of Prerequisites}.
5953 @item target-specific
5954 Supports target-specific and pattern-specific variable assignments.
5955 @xref{Target-specific, ,Target-specific Variable Values}.
5957 @end table
5959 @vindex .INCLUDE_DIRS @r{(list of include directories)}
5960 @item .INCLUDE_DIRS
5961 Expands to a list of directories that @code{make} searches for
5962 included makefiles (@pxref{Include, , Including Other Makefiles}).
5964 @end table
5966 @node Conditionals, Functions, Using Variables, Top
5967 @chapter Conditional Parts of Makefiles
5969 @cindex conditionals
5970 A @dfn{conditional} directive causes part of a makefile to be obeyed
5971 or ignored depending on the values of variables.  Conditionals can
5972 compare the value of one variable to another, or the value of a
5973 variable to a constant string.  Conditionals control what @code{make}
5974 actually ``sees'' in the makefile, so they @emph{cannot} be used to
5975 control recipes at the time of execution.@refill
5977 @menu
5978 * Conditional Example::         Example of a conditional
5979 * Conditional Syntax::          The syntax of conditionals.
5980 * Testing Flags::               Conditionals that test flags.
5981 @end menu
5983 @node Conditional Example, Conditional Syntax, Conditionals, Conditionals
5984 @section Example of a Conditional
5986 The following example of a conditional tells @code{make} to use one
5987 set of libraries if the @code{CC} variable is @samp{gcc}, and a
5988 different set of libraries otherwise.  It works by controlling which
5989 of two recipe lines will be used for the rule.  The result is that
5990 @samp{CC=gcc} as an argument to @code{make} changes not only which
5991 compiler is used but also which libraries are linked.
5993 @example
5994 libs_for_gcc = -lgnu
5995 normal_libs =
5997 foo: $(objects)
5998 ifeq ($(CC),gcc)
5999         $(CC) -o foo $(objects) $(libs_for_gcc)
6000 else
6001         $(CC) -o foo $(objects) $(normal_libs)
6002 endif
6003 @end example
6005 This conditional uses three directives: one @code{ifeq}, one @code{else}
6006 and one @code{endif}.
6008 The @code{ifeq} directive begins the conditional, and specifies the
6009 condition.  It contains two arguments, separated by a comma and surrounded
6010 by parentheses.  Variable substitution is performed on both arguments and
6011 then they are compared.  The lines of the makefile following the
6012 @code{ifeq} are obeyed if the two arguments match; otherwise they are
6013 ignored.
6015 The @code{else} directive causes the following lines to be obeyed if the
6016 previous conditional failed.  In the example above, this means that the
6017 second alternative linking command is used whenever the first alternative
6018 is not used.  It is optional to have an @code{else} in a conditional.
6020 The @code{endif} directive ends the conditional.  Every conditional must
6021 end with an @code{endif}.  Unconditional makefile text follows.
6023 As this example illustrates, conditionals work at the textual level:
6024 the lines of the conditional are treated as part of the makefile, or
6025 ignored, according to the condition.  This is why the larger syntactic
6026 units of the makefile, such as rules, may cross the beginning or the
6027 end of the conditional.
6029 When the variable @code{CC} has the value @samp{gcc}, the above example has
6030 this effect:
6032 @example
6033 foo: $(objects)
6034         $(CC) -o foo $(objects) $(libs_for_gcc)
6035 @end example
6037 @noindent
6038 When the variable @code{CC} has any other value, the effect is this:
6040 @example
6041 foo: $(objects)
6042         $(CC) -o foo $(objects) $(normal_libs)
6043 @end example
6045 Equivalent results can be obtained in another way by conditionalizing a
6046 variable assignment and then using the variable unconditionally:
6048 @example
6049 libs_for_gcc = -lgnu
6050 normal_libs =
6052 ifeq ($(CC),gcc)
6053   libs=$(libs_for_gcc)
6054 else
6055   libs=$(normal_libs)
6056 endif
6058 foo: $(objects)
6059         $(CC) -o foo $(objects) $(libs)
6060 @end example
6062 @node Conditional Syntax, Testing Flags, Conditional Example, Conditionals
6063 @section Syntax of Conditionals
6064 @findex ifdef
6065 @findex ifeq
6066 @findex ifndef
6067 @findex ifneq
6068 @findex else
6069 @findex endif
6071 The syntax of a simple conditional with no @code{else} is as follows:
6073 @example
6074 @var{conditional-directive}
6075 @var{text-if-true}
6076 endif
6077 @end example
6079 @noindent
6080 The @var{text-if-true} may be any lines of text, to be considered as part
6081 of the makefile if the condition is true.  If the condition is false, no
6082 text is used instead.
6084 The syntax of a complex conditional is as follows:
6086 @example
6087 @var{conditional-directive}
6088 @var{text-if-true}
6089 else
6090 @var{text-if-false}
6091 endif
6092 @end example
6096 @example
6097 @var{conditional-directive}
6098 @var{text-if-one-is-true}
6099 else @var{conditional-directive}
6100 @var{text-if-true}
6101 else
6102 @var{text-if-false}
6103 endif
6104 @end example
6106 @noindent
6107 There can be as many ``@code{else} @var{conditional-directive}''
6108 clauses as necessary.  Once a given condition is true,
6109 @var{text-if-true} is used and no other clause is used; if no
6110 condition is true then @var{text-if-false} is used.  The
6111 @var{text-if-true} and @var{text-if-false} can be any number of lines
6112 of text.
6114 The syntax of the @var{conditional-directive} is the same whether the
6115 conditional is simple or complex; after an @code{else} or not.  There
6116 are four different directives that test different conditions.  Here is
6117 a table of them:
6119 @table @code
6120 @item ifeq (@var{arg1}, @var{arg2})
6121 @itemx ifeq '@var{arg1}' '@var{arg2}'
6122 @itemx ifeq "@var{arg1}" "@var{arg2}"
6123 @itemx ifeq "@var{arg1}" '@var{arg2}'
6124 @itemx ifeq '@var{arg1}' "@var{arg2}"
6125 Expand all variable references in @var{arg1} and @var{arg2} and
6126 compare them.  If they are identical, the @var{text-if-true} is
6127 effective; otherwise, the @var{text-if-false}, if any, is effective.
6129 Often you want to test if a variable has a non-empty value.  When the
6130 value results from complex expansions of variables and functions,
6131 expansions you would consider empty may actually contain whitespace
6132 characters and thus are not seen as empty.  However, you can use the
6133 @code{strip} function (@pxref{Text Functions}) to avoid interpreting
6134 whitespace as a non-empty value.  For example:
6136 @example
6137 @group
6138 ifeq ($(strip $(foo)),)
6139 @var{text-if-empty}
6140 endif
6141 @end group
6142 @end example
6144 @noindent
6145 will evaluate @var{text-if-empty} even if the expansion of
6146 @code{$(foo)} contains whitespace characters.
6148 @item ifneq (@var{arg1}, @var{arg2})
6149 @itemx ifneq '@var{arg1}' '@var{arg2}'
6150 @itemx ifneq "@var{arg1}" "@var{arg2}"
6151 @itemx ifneq "@var{arg1}" '@var{arg2}'
6152 @itemx ifneq '@var{arg1}' "@var{arg2}"
6153 Expand all variable references in @var{arg1} and @var{arg2} and
6154 compare them.  If they are different, the @var{text-if-true} is
6155 effective; otherwise, the @var{text-if-false}, if any, is effective.
6157 @item ifdef @var{variable-name}
6158 The @code{ifdef} form takes the @emph{name} of a variable as its
6159 argument, not a reference to a variable.  The value of that variable
6160 has a non-empty value, the @var{text-if-true} is effective; otherwise,
6161 the @var{text-if-false}, if any, is effective.  Variables that have
6162 never been defined have an empty value.  The text @var{variable-name}
6163 is expanded, so it could be a variable or function that expands
6164 to the name of a variable.  For example:
6166 @example
6167 bar = true
6168 foo = bar
6169 ifdef $(foo)
6170 frobozz = yes
6171 endif
6172 @end example
6174 The variable reference @code{$(foo)} is expanded, yielding @code{bar},
6175 which is considered to be the name of a variable.  The variable
6176 @code{bar} is not expanded, but its value is examined to determine if
6177 it is non-empty.
6179 Note that @code{ifdef} only tests whether a variable has a value.  It
6180 does not expand the variable to see if that value is nonempty.
6181 Consequently, tests using @code{ifdef} return true for all definitions
6182 except those like @code{foo =}.  To test for an empty value, use
6183 @w{@code{ifeq ($(foo),)}}.  For example,
6185 @example
6186 bar =
6187 foo = $(bar)
6188 ifdef foo
6189 frobozz = yes
6190 else
6191 frobozz = no
6192 endif
6193 @end example
6195 @noindent
6196 sets @samp{frobozz} to @samp{yes}, while:
6198 @example
6199 foo =
6200 ifdef foo
6201 frobozz = yes
6202 else
6203 frobozz = no
6204 endif
6205 @end example
6207 @noindent
6208 sets @samp{frobozz} to @samp{no}.
6210 @item ifndef @var{variable-name}
6211 If the variable @var{variable-name} has an empty value, the
6212 @var{text-if-true} is effective; otherwise, the @var{text-if-false},
6213 if any, is effective.  The rules for expansion and testing of
6214 @var{variable-name} are identical to the @code{ifdef} directive.
6215 @end table
6217 Extra spaces are allowed and ignored at the beginning of the
6218 conditional directive line, but a tab is not allowed.  (If the line
6219 begins with a tab, it will be considered part of a recipe for a rule.)
6220 Aside from this, extra spaces or tabs may be inserted with no effect
6221 anywhere except within the directive name or within an argument.  A
6222 comment starting with @samp{#} may appear at the end of the line.
6224 The other two directives that play a part in a conditional are @code{else}
6225 and @code{endif}.  Each of these directives is written as one word, with no
6226 arguments.  Extra spaces are allowed and ignored at the beginning of the
6227 line, and spaces or tabs at the end.  A comment starting with @samp{#} may
6228 appear at the end of the line.
6230 Conditionals affect which lines of the makefile @code{make} uses.  If
6231 the condition is true, @code{make} reads the lines of the
6232 @var{text-if-true} as part of the makefile; if the condition is false,
6233 @code{make} ignores those lines completely.  It follows that syntactic
6234 units of the makefile, such as rules, may safely be split across the
6235 beginning or the end of the conditional.@refill
6237 @code{make} evaluates conditionals when it reads a makefile.
6238 Consequently, you cannot use automatic variables in the tests of
6239 conditionals because they are not defined until recipes are run
6240 (@pxref{Automatic Variables}).
6242 To prevent intolerable confusion, it is not permitted to start a
6243 conditional in one makefile and end it in another.  However, you may
6244 write an @code{include} directive within a conditional, provided you do
6245 not attempt to terminate the conditional inside the included file.
6247 @node Testing Flags,  , Conditional Syntax, Conditionals
6248 @section Conditionals that Test Flags
6250 You can write a conditional that tests @code{make} command flags such as
6251 @samp{-t} by using the variable @code{MAKEFLAGS} together with the
6252 @code{findstring} function
6253 (@pxref{Text Functions, , Functions for String Substitution and Analysis}).
6254 This is useful when @code{touch} is not enough to make a file appear up
6255 to date.
6257 The @code{findstring} function determines whether one string appears as a
6258 substring of another.  If you want to test for the @samp{-t} flag,
6259 use @samp{t} as the first string and the value of @code{MAKEFLAGS} as
6260 the other.
6262 For example, here is how to arrange to use @samp{ranlib -t} to finish
6263 marking an archive file up to date:
6265 @example
6266 archive.a: @dots{}
6267 ifneq (,$(findstring t,$(MAKEFLAGS)))
6268         +touch archive.a
6269         +ranlib -t archive.a
6270 else
6271         ranlib archive.a
6272 endif
6273 @end example
6275 @noindent
6276 The @samp{+} prefix marks those recipe lines as ``recursive'' so that
6277 they will be executed despite use of the @samp{-t} flag.
6278 @xref{Recursion, ,Recursive Use of @code{make}}.
6280 @node Functions, Running, Conditionals, Top
6281 @chapter Functions for Transforming Text
6282 @cindex functions
6284 @dfn{Functions} allow you to do text processing in the makefile to
6285 compute the files to operate on or the commands to use in recipes.
6286 You use a function in a @dfn{function call}, where you give the name
6287 of the function and some text (the @dfn{arguments}) for the function
6288 to operate on.  The result of the function's processing is substituted
6289 into the makefile at the point of the call, just as a variable might
6290 be substituted.
6292 @menu
6293 * Syntax of Functions::         How to write a function call.
6294 * Text Functions::              General-purpose text manipulation functions.
6295 * File Name Functions::         Functions for manipulating file names.
6296 * Conditional Functions::       Functions that implement conditions.
6297 * Foreach Function::            Repeat some text with controlled variation.
6298 * Call Function::               Expand a user-defined function.
6299 * Value Function::              Return the un-expanded value of a variable.
6300 * Eval Function::               Evaluate the arguments as makefile syntax.
6301 * Origin Function::             Find where a variable got its value.
6302 * Flavor Function::             Find out the flavor of a variable.
6303 * Shell Function::              Substitute the output of a shell command.
6304 * Make Control Functions::      Functions that control how make runs.
6305 @end menu
6307 @node Syntax of Functions, Text Functions, Functions, Functions
6308 @section Function Call Syntax
6309 @cindex @code{$}, in function call
6310 @cindex dollar sign (@code{$}), in function call
6311 @cindex arguments of functions
6312 @cindex functions, syntax of
6314 A function call resembles a variable reference.  It looks like this:
6316 @example
6317 $(@var{function} @var{arguments})
6318 @end example
6320 @noindent
6321 or like this:
6323 @example
6324 $@{@var{function} @var{arguments}@}
6325 @end example
6327 Here @var{function} is a function name; one of a short list of names
6328 that are part of @code{make}.  You can also essentially create your own
6329 functions by using the @code{call} builtin function.
6331 The @var{arguments} are the arguments of the function.  They are
6332 separated from the function name by one or more spaces or tabs, and if
6333 there is more than one argument, then they are separated by commas.
6334 Such whitespace and commas are not part of an argument's value.  The
6335 delimiters which you use to surround the function call, whether
6336 parentheses or braces, can appear in an argument only in matching pairs;
6337 the other kind of delimiters may appear singly.  If the arguments
6338 themselves contain other function calls or variable references, it is
6339 wisest to use the same kind of delimiters for all the references; write
6340 @w{@samp{$(subst a,b,$(x))}}, not @w{@samp{$(subst a,b,$@{x@})}}.  This
6341 is because it is clearer, and because only one type of delimiter is
6342 matched to find the end of the reference.
6344 The text written for each argument is processed by substitution of
6345 variables and function calls to produce the argument value, which
6346 is the text on which the function acts.  The substitution is done in the
6347 order in which the arguments appear.
6349 Commas and unmatched parentheses or braces cannot appear in the text of an
6350 argument as written; leading spaces cannot appear in the text of the first
6351 argument as written.  These characters can be put into the argument value
6352 by variable substitution.  First define variables @code{comma} and
6353 @code{space} whose values are isolated comma and space characters, then
6354 substitute these variables where such characters are wanted, like this:
6356 @example
6357 @group
6358 comma:= ,
6359 empty:=
6360 space:= $(empty) $(empty)
6361 foo:= a b c
6362 bar:= $(subst $(space),$(comma),$(foo))
6363 # @r{bar is now `a,b,c'.}
6364 @end group
6365 @end example
6367 @noindent
6368 Here the @code{subst} function replaces each space with a comma, through
6369 the value of @code{foo}, and substitutes the result.
6371 @node Text Functions, File Name Functions, Syntax of Functions, Functions
6372 @section Functions for String Substitution and Analysis
6373 @cindex functions, for text
6375 Here are some functions that operate on strings:
6377 @table @code
6378 @item $(subst @var{from},@var{to},@var{text})
6379 @findex subst
6380 Performs a textual replacement on the text @var{text}: each occurrence
6381 of @var{from} is replaced by @var{to}.  The result is substituted for
6382 the function call.  For example,
6384 @example
6385 $(subst ee,EE,feet on the street)
6386 @end example
6388 substitutes the string @samp{fEEt on the strEEt}.
6390 @item $(patsubst @var{pattern},@var{replacement},@var{text})
6391 @findex patsubst
6392 Finds whitespace-separated words in @var{text} that match
6393 @var{pattern} and replaces them with @var{replacement}.  Here
6394 @var{pattern} may contain a @samp{%} which acts as a wildcard,
6395 matching any number of any characters within a word.  If
6396 @var{replacement} also contains a @samp{%}, the @samp{%} is replaced
6397 by the text that matched the @samp{%} in @var{pattern}.  Only the first
6398 @samp{%} in the @var{pattern} and @var{replacement} is treated this
6399 way; any subsequent @samp{%} is unchanged.@refill
6401 @cindex @code{%}, quoting in @code{patsubst}
6402 @cindex @code{%}, quoting with @code{\} (backslash)
6403 @cindex @code{\} (backslash), to quote @code{%}
6404 @cindex backslash (@code{\}), to quote @code{%}
6405 @cindex quoting @code{%}, in @code{patsubst}
6406 @samp{%} characters in @code{patsubst} function invocations can be
6407 quoted with preceding backslashes (@samp{\}).  Backslashes that would
6408 otherwise quote @samp{%} characters can be quoted with more backslashes.
6409 Backslashes that quote @samp{%} characters or other backslashes are
6410 removed from the pattern before it is compared file names or has a stem
6411 substituted into it.  Backslashes that are not in danger of quoting
6412 @samp{%} characters go unmolested.  For example, the pattern
6413 @file{the\%weird\\%pattern\\} has @samp{the%weird\} preceding the
6414 operative @samp{%} character, and @samp{pattern\\} following it.  The
6415 final two backslashes are left alone because they cannot affect any
6416 @samp{%} character.@refill
6418 Whitespace between words is folded into single space characters;
6419 leading and trailing whitespace is discarded.
6421 For example,
6423 @example
6424 $(patsubst %.c,%.o,x.c.c bar.c)
6425 @end example
6427 @noindent
6428 produces the value @samp{x.c.o bar.o}.
6430 Substitution references (@pxref{Substitution Refs, ,Substitution
6431 References}) are a simpler way to get the effect of the @code{patsubst}
6432 function:
6434 @example
6435 $(@var{var}:@var{pattern}=@var{replacement})
6436 @end example
6438 @noindent
6439 is equivalent to
6441 @example
6442 $(patsubst @var{pattern},@var{replacement},$(@var{var}))
6443 @end example
6445 The second shorthand simplifies one of the most common uses of
6446 @code{patsubst}: replacing the suffix at the end of file names.
6448 @example
6449 $(@var{var}:@var{suffix}=@var{replacement})
6450 @end example
6452 @noindent
6453 is equivalent to
6455 @example
6456 $(patsubst %@var{suffix},%@var{replacement},$(@var{var}))
6457 @end example
6459 @noindent
6460 For example, you might have a list of object files:
6462 @example
6463 objects = foo.o bar.o baz.o
6464 @end example
6466 @noindent
6467 To get the list of corresponding source files, you could simply write:
6469 @example
6470 $(objects:.o=.c)
6471 @end example
6473 @noindent
6474 instead of using the general form:
6476 @example
6477 $(patsubst %.o,%.c,$(objects))
6478 @end example
6480 @item $(strip @var{string})
6481 @cindex stripping whitespace
6482 @cindex whitespace, stripping
6483 @cindex spaces, stripping
6484 @findex strip
6485 Removes leading and trailing whitespace from @var{string} and replaces
6486 each internal sequence of one or more whitespace characters with a
6487 single space.  Thus, @samp{$(strip a b  c )} results in @w{@samp{a b c}}.
6489 The function @code{strip} can be very useful when used in conjunction
6490 with conditionals.  When comparing something with the empty string
6491 @samp{} using @code{ifeq} or @code{ifneq}, you usually want a string of
6492 just whitespace to match the empty string (@pxref{Conditionals}).
6494 Thus, the following may fail to have the desired results:
6496 @example
6497 .PHONY: all
6498 ifneq   "$(needs_made)" ""
6499 all: $(needs_made)
6500 else
6501 all:;@@echo 'Nothing to make!'
6502 endif
6503 @end example
6505 @noindent
6506 Replacing the variable reference @w{@samp{$(needs_made)}} with the
6507 function call @w{@samp{$(strip $(needs_made))}} in the @code{ifneq}
6508 directive would make it more robust.@refill
6510 @item $(findstring @var{find},@var{in})
6511 @findex findstring
6512 @cindex searching for strings
6513 @cindex finding strings
6514 @cindex strings, searching for
6515 Searches @var{in} for an occurrence of @var{find}.  If it occurs, the
6516 value is @var{find}; otherwise, the value is empty.  You can use this
6517 function in a conditional to test for the presence of a specific
6518 substring in a given string.  Thus, the two examples,
6520 @example
6521 $(findstring a,a b c)
6522 $(findstring a,b c)
6523 @end example
6525 @noindent
6526 produce the values @samp{a} and @samp{} (the empty string),
6527 respectively.  @xref{Testing Flags}, for a practical application of
6528 @code{findstring}.@refill
6530 @need 750
6531 @findex filter
6532 @cindex filtering words
6533 @cindex words, filtering
6534 @item $(filter @var{pattern}@dots{},@var{text})
6535 Returns all whitespace-separated words in @var{text} that @emph{do} match
6536 any of the @var{pattern} words, removing any words that @emph{do not}
6537 match.  The patterns are written using @samp{%}, just like the patterns
6538 used in the @code{patsubst} function above.@refill
6540 The @code{filter} function can be used to separate out different types
6541 of strings (such as file names) in a variable.  For example:
6543 @example
6544 sources := foo.c bar.c baz.s ugh.h
6545 foo: $(sources)
6546         cc $(filter %.c %.s,$(sources)) -o foo
6547 @end example
6549 @noindent
6550 says that @file{foo} depends of @file{foo.c}, @file{bar.c},
6551 @file{baz.s} and @file{ugh.h} but only @file{foo.c}, @file{bar.c} and
6552 @file{baz.s} should be specified in the command to the
6553 compiler.@refill
6555 @item $(filter-out @var{pattern}@dots{},@var{text})
6556 @findex filter-out
6557 @cindex filtering out words
6558 @cindex words, filtering out
6559 Returns all whitespace-separated words in @var{text} that @emph{do not}
6560 match any of the @var{pattern} words, removing the words that @emph{do}
6561 match one or more.  This is the exact opposite of the @code{filter}
6562 function.@refill
6564 For example, given:
6566 @example
6567 @group
6568 objects=main1.o foo.o main2.o bar.o
6569 mains=main1.o main2.o
6570 @end group
6571 @end example
6573 @noindent
6574 the following generates a list which contains all the object files not
6575 in @samp{mains}:
6577 @example
6578 $(filter-out $(mains),$(objects))
6579 @end example
6581 @need 1500
6582 @findex sort
6583 @cindex sorting words
6584 @item $(sort @var{list})
6585 Sorts the words of @var{list} in lexical order, removing duplicate
6586 words.  The output is a list of words separated by single spaces.
6587 Thus,
6589 @example
6590 $(sort foo bar lose)
6591 @end example
6593 @noindent
6594 returns the value @samp{bar foo lose}.
6596 @cindex removing duplicate words
6597 @cindex duplicate words, removing
6598 @cindex words, removing duplicates
6599 Incidentally, since @code{sort} removes duplicate words, you can use
6600 it for this purpose even if you don't care about the sort order.
6602 @item $(word @var{n},@var{text})
6603 @findex word
6604 @cindex word, selecting a
6605 @cindex selecting a word
6606 Returns the @var{n}th word of @var{text}.  The legitimate values of
6607 @var{n} start from 1.  If @var{n} is bigger than the number of words
6608 in @var{text}, the value is empty.  For example,
6610 @example
6611 $(word 2, foo bar baz)
6612 @end example
6614 @noindent
6615 returns @samp{bar}.
6617 @item $(wordlist @var{s},@var{e},@var{text})
6618 @findex wordlist
6619 @cindex words, selecting lists of
6620 @cindex selecting word lists
6621 Returns the list of words in @var{text} starting with word @var{s} and
6622 ending with word @var{e} (inclusive).  The legitimate values of @var{s}
6623 start from 1; @var{e} may start from 0.  If @var{s} is bigger than the
6624 number of words in @var{text}, the value is empty.  If @var{e} is
6625 bigger than the number of words in @var{text}, words up to the end of
6626 @var{text} are returned.  If @var{s} is greater than @var{e}, nothing
6627 is returned.  For example,
6629 @example
6630 $(wordlist 2, 3, foo bar baz)
6631 @end example
6633 @noindent
6634 returns @samp{bar baz}.
6636 @c Following item phrased to prevent overfull hbox.  --RJC 17 Jul 92
6637 @item $(words @var{text})
6638 @findex words
6639 @cindex words, finding number
6640 Returns the number of words in @var{text}.
6641 Thus, the last word of @var{text} is
6642 @w{@code{$(word $(words @var{text}),@var{text})}}.@refill
6644 @item $(firstword @var{names}@dots{})
6645 @findex firstword
6646 @cindex words, extracting first
6647 The argument @var{names} is regarded as a series of names, separated
6648 by whitespace.  The value is the first name in the series.  The rest
6649 of the names are ignored.
6651 For example,
6653 @example
6654 $(firstword foo bar)
6655 @end example
6657 @noindent
6658 produces the result @samp{foo}.  Although @code{$(firstword
6659 @var{text})} is the same as @code{$(word 1,@var{text})}, the
6660 @code{firstword} function is retained for its simplicity.@refill
6663 @item $(lastword @var{names}@dots{})
6664 @findex lastword
6665 @cindex words, extracting last
6666 The argument @var{names} is regarded as a series of names, separated
6667 by whitespace.  The value is the last name in the series.
6669 For example,
6671 @example
6672 $(lastword foo bar)
6673 @end example
6675 @noindent
6676 produces the result @samp{bar}.  Although @code{$(lastword
6677 @var{text})} is the same as @code{$(word $(words @var{text}),@var{text})},
6678 the @code{lastword} function was added for its simplicity and better
6679 performance.@refill
6680 @end table
6683 Here is a realistic example of the use of @code{subst} and
6684 @code{patsubst}.  Suppose that a makefile uses the @code{VPATH} variable
6685 to specify a list of directories that @code{make} should search for
6686 prerequisite files
6687 (@pxref{General Search, , @code{VPATH} Search Path for All Prerequisites}).
6688 This example shows how to
6689 tell the C compiler to search for header files in the same list of
6690 directories.@refill
6692 The value of @code{VPATH} is a list of directories separated by colons,
6693 such as @samp{src:../headers}.  First, the @code{subst} function is used to
6694 change the colons to spaces:
6696 @example
6697 $(subst :, ,$(VPATH))
6698 @end example
6700 @noindent
6701 This produces @samp{src ../headers}.  Then @code{patsubst} is used to turn
6702 each directory name into a @samp{-I} flag.  These can be added to the
6703 value of the variable @code{CFLAGS}, which is passed automatically to the C
6704 compiler, like this:
6706 @example
6707 override CFLAGS += $(patsubst %,-I%,$(subst :, ,$(VPATH)))
6708 @end example
6710 @noindent
6711 The effect is to append the text @samp{-Isrc -I../headers} to the
6712 previously given value of @code{CFLAGS}.  The @code{override} directive is
6713 used so that the new value is assigned even if the previous value of
6714 @code{CFLAGS} was specified with a command argument (@pxref{Override
6715 Directive, , The @code{override} Directive}).
6717 @node File Name Functions, Conditional Functions, Text Functions, Functions
6718 @section Functions for File Names
6719 @cindex functions, for file names
6720 @cindex file name functions
6722 Several of the built-in expansion functions relate specifically to
6723 taking apart file names or lists of file names.
6725 Each of the following functions performs a specific transformation on a
6726 file name.  The argument of the function is regarded as a series of file
6727 names, separated by whitespace.  (Leading and trailing whitespace is
6728 ignored.)  Each file name in the series is transformed in the same way and
6729 the results are concatenated with single spaces between them.
6731 @table @code
6732 @item $(dir @var{names}@dots{})
6733 @findex dir
6734 @cindex directory part
6735 @cindex file name, directory part
6736 Extracts the directory-part of each file name in @var{names}.  The
6737 directory-part of the file name is everything up through (and
6738 including) the last slash in it.  If the file name contains no slash,
6739 the directory part is the string @samp{./}.  For example,
6741 @example
6742 $(dir src/foo.c hacks)
6743 @end example
6745 @noindent
6746 produces the result @samp{src/ ./}.
6748 @item $(notdir @var{names}@dots{})
6749 @findex notdir
6750 @cindex file name, nondirectory part
6751 @cindex nondirectory part
6752 Extracts all but the directory-part of each file name in @var{names}.
6753 If the file name contains no slash, it is left unchanged.  Otherwise,
6754 everything through the last slash is removed from it.
6756 A file name that ends with a slash becomes an empty string.  This is
6757 unfortunate, because it means that the result does not always have the
6758 same number of whitespace-separated file names as the argument had;
6759 but we do not see any other valid alternative.
6761 For example,
6763 @example
6764 $(notdir src/foo.c hacks)
6765 @end example
6767 @noindent
6768 produces the result @samp{foo.c hacks}.
6770 @item $(suffix @var{names}@dots{})
6771 @findex suffix
6772 @cindex suffix, function to find
6773 @cindex file name suffix
6774 Extracts the suffix of each file name in @var{names}.  If the file name
6775 contains a period, the suffix is everything starting with the last
6776 period.  Otherwise, the suffix is the empty string.  This frequently
6777 means that the result will be empty when @var{names} is not, and if
6778 @var{names} contains multiple file names, the result may contain fewer
6779 file names.
6781 For example,
6783 @example
6784 $(suffix src/foo.c src-1.0/bar.c hacks)
6785 @end example
6787 @noindent
6788 produces the result @samp{.c .c}.
6790 @item $(basename @var{names}@dots{})
6791 @findex basename
6792 @cindex basename
6793 @cindex file name, basename of
6794 Extracts all but the suffix of each file name in @var{names}.  If the
6795 file name contains a period, the basename is everything starting up to
6796 (and not including) the last period.  Periods in the directory part are
6797 ignored.  If there is no period, the basename is the entire file name.
6798 For example,
6800 @example
6801 $(basename src/foo.c src-1.0/bar hacks)
6802 @end example
6804 @noindent
6805 produces the result @samp{src/foo src-1.0/bar hacks}.
6807 @c plural convention with dots (be consistent)
6808 @item $(addsuffix @var{suffix},@var{names}@dots{})
6809 @findex addsuffix
6810 @cindex suffix, adding
6811 @cindex file name suffix, adding
6812 The argument @var{names} is regarded as a series of names, separated
6813 by whitespace; @var{suffix} is used as a unit.  The value of
6814 @var{suffix} is appended to the end of each individual name and the
6815 resulting larger names are concatenated with single spaces between
6816 them.  For example,
6818 @example
6819 $(addsuffix .c,foo bar)
6820 @end example
6822 @noindent
6823 produces the result @samp{foo.c bar.c}.
6825 @item $(addprefix @var{prefix},@var{names}@dots{})
6826 @findex addprefix
6827 @cindex prefix, adding
6828 @cindex file name prefix, adding
6829 The argument @var{names} is regarded as a series of names, separated
6830 by whitespace; @var{prefix} is used as a unit.  The value of
6831 @var{prefix} is prepended to the front of each individual name and the
6832 resulting larger names are concatenated with single spaces between
6833 them.  For example,
6835 @example
6836 $(addprefix src/,foo bar)
6837 @end example
6839 @noindent
6840 produces the result @samp{src/foo src/bar}.
6842 @item $(join @var{list1},@var{list2})
6843 @findex join
6844 @cindex joining lists of words
6845 @cindex words, joining lists
6846 Concatenates the two arguments word by word: the two first words (one
6847 from each argument) concatenated form the first word of the result, the
6848 two second words form the second word of the result, and so on.  So the
6849 @var{n}th word of the result comes from the @var{n}th word of each
6850 argument.  If one argument has more words that the other, the extra
6851 words are copied unchanged into the result.
6853 For example, @samp{$(join a b,.c .o)} produces @samp{a.c b.o}.
6855 Whitespace between the words in the lists is not preserved; it is
6856 replaced with a single space.
6858 This function can merge the results of the @code{dir} and
6859 @code{notdir} functions, to produce the original list of files which
6860 was given to those two functions.@refill
6862 @item $(wildcard @var{pattern})
6863 @findex wildcard
6864 @cindex wildcard, function
6865 The argument @var{pattern} is a file name pattern, typically containing
6866 wildcard characters (as in shell file name patterns).  The result of
6867 @code{wildcard} is a space-separated list of the names of existing files
6868 that match the pattern.
6869 @xref{Wildcards, ,Using Wildcard Characters in File Names}.
6871 @item $(realpath @var{names}@dots{})
6872 @findex realpath
6873 @cindex realpath
6874 @cindex file name, realpath of
6875 For each file name in @var{names} return the canonical absolute name.
6876 A canonical name does not contain any @code{.} or @code{..} components,
6877 nor any repeated path separators (@code{/}) or symlinks.  In case of a
6878 failure the empty string is returned.  Consult the @code{realpath(3)}
6879 documentation for a list of possible failure causes.
6881 @item $(abspath @var{names}@dots{})
6882 @findex abspath
6883 @cindex abspath
6884 @cindex file name, abspath of
6885 For each file name in @var{names} return an absolute name that does
6886 not contain any @code{.} or @code{..} components, nor any repeated path
6887 separators (@code{/}).  Note that, in contrast to @code{realpath}
6888 function, @code{abspath} does not resolve symlinks and does not require
6889 the file names to refer to an existing file or directory.  Use the
6890 @code{wildcard} function to test for existence.
6891 @end table
6893 @node Conditional Functions, Foreach Function, File Name Functions, Functions
6894 @section Functions for Conditionals
6895 @findex if
6896 @cindex conditional expansion
6897 There are three functions that provide conditional expansion.  A key
6898 aspect of these functions is that not all of the arguments are
6899 expanded initially.  Only those arguments which need to be expanded,
6900 will be expanded.
6902 @table @code
6903 @item $(if @var{condition},@var{then-part}[,@var{else-part}])
6904 @findex if
6905 The @code{if} function provides support for conditional expansion in a
6906 functional context (as opposed to the GNU @code{make} makefile
6907 conditionals such as @code{ifeq} (@pxref{Conditional Syntax, ,Syntax of
6908 Conditionals}).
6910 The first argument, @var{condition}, first has all preceding and
6911 trailing whitespace stripped, then is expanded.  If it expands to any
6912 non-empty string, then the condition is considered to be true.  If it
6913 expands to an empty string, the condition is considered to be false.
6915 If the condition is true then the second argument, @var{then-part}, is
6916 evaluated and this is used as the result of the evaluation of the entire
6917 @code{if} function.
6919 If the condition is false then the third argument, @var{else-part}, is
6920 evaluated and this is the result of the @code{if} function.  If there is
6921 no third argument, the @code{if} function evaluates to nothing (the
6922 empty string).
6924 Note that only one of the @var{then-part} or the @var{else-part} will be
6925 evaluated, never both.  Thus, either can contain side-effects (such as
6926 @code{shell} function calls, etc.)
6928 @item $(or @var{condition1}[,@var{condition2}[,@var{condition3}@dots{}]])
6929 @findex or
6930 The @code{or} function provides a ``short-circuiting'' OR operation.
6931 Each argument is expanded, in order.  If an argument expands to a
6932 non-empty string the processing stops and the result of the expansion
6933 is that string.  If, after all arguments are expanded, all of them are
6934 false (empty), then the result of the expansion is the empty string.
6936 @item $(and @var{condition1}[,@var{condition2}[,@var{condition3}@dots{}]])
6937 @findex and
6938 The @code{and} function provides a ``short-circuiting'' AND operation.
6939 Each argument is expanded, in order.  If an argument expands to an
6940 empty string the processing stops and the result of the expansion is
6941 the empty string.  If all arguments expand to a non-empty string then
6942 the result of the expansion is the expansion of the last argument.
6944 @end table
6946 @node Foreach Function, Call Function, Conditional Functions, Functions
6947 @section The @code{foreach} Function
6948 @findex foreach
6949 @cindex words, iterating over
6951 The @code{foreach} function is very different from other functions.  It
6952 causes one piece of text to be used repeatedly, each time with a different
6953 substitution performed on it.  It resembles the @code{for} command in the
6954 shell @code{sh} and the @code{foreach} command in the C-shell @code{csh}.
6956 The syntax of the @code{foreach} function is:
6958 @example
6959 $(foreach @var{var},@var{list},@var{text})
6960 @end example
6962 @noindent
6963 The first two arguments, @var{var} and @var{list}, are expanded before
6964 anything else is done; note that the last argument, @var{text}, is
6965 @strong{not} expanded at the same time.  Then for each word of the expanded
6966 value of @var{list}, the variable named by the expanded value of @var{var}
6967 is set to that word, and @var{text} is expanded.  Presumably @var{text}
6968 contains references to that variable, so its expansion will be different
6969 each time.
6971 The result is that @var{text} is expanded as many times as there are
6972 whitespace-separated words in @var{list}.  The multiple expansions of
6973 @var{text} are concatenated, with spaces between them, to make the result
6974 of @code{foreach}.
6976 This simple example sets the variable @samp{files} to the list of all files
6977 in the directories in the list @samp{dirs}:
6979 @example
6980 dirs := a b c d
6981 files := $(foreach dir,$(dirs),$(wildcard $(dir)/*))
6982 @end example
6984 Here @var{text} is @samp{$(wildcard $(dir)/*)}.  The first repetition
6985 finds the value @samp{a} for @code{dir}, so it produces the same result
6986 as @samp{$(wildcard a/*)}; the second repetition produces the result
6987 of @samp{$(wildcard b/*)}; and the third, that of @samp{$(wildcard c/*)}.
6989 This example has the same result (except for setting @samp{dirs}) as
6990 the following example:
6992 @example
6993 files := $(wildcard a/* b/* c/* d/*)
6994 @end example
6996 When @var{text} is complicated, you can improve readability by giving it
6997 a name, with an additional variable:
6999 @example
7000 find_files = $(wildcard $(dir)/*)
7001 dirs := a b c d
7002 files := $(foreach dir,$(dirs),$(find_files))
7003 @end example
7005 @noindent
7006 Here we use the variable @code{find_files} this way.  We use plain @samp{=}
7007 to define a recursively-expanding variable, so that its value contains an
7008 actual function call to be reexpanded under the control of @code{foreach};
7009 a simply-expanded variable would not do, since @code{wildcard} would be
7010 called only once at the time of defining @code{find_files}.
7012 The @code{foreach} function has no permanent effect on the variable
7013 @var{var}; its value and flavor after the @code{foreach} function call are
7014 the same as they were beforehand.  The other values which are taken from
7015 @var{list} are in effect only temporarily, during the execution of
7016 @code{foreach}.  The variable @var{var} is a simply-expanded variable
7017 during the execution of @code{foreach}.  If @var{var} was undefined
7018 before the @code{foreach} function call, it is undefined after the call.
7019 @xref{Flavors, ,The Two Flavors of Variables}.@refill
7021 You must take care when using complex variable expressions that result in
7022 variable names because many strange things are valid variable names, but
7023 are probably not what you intended.  For example,
7025 @smallexample
7026 files := $(foreach Esta escrito en espanol!,b c ch,$(find_files))
7027 @end smallexample
7029 @noindent
7030 might be useful if the value of @code{find_files} references the variable
7031 whose name is @samp{Esta escrito en espanol!} (es un nombre bastante largo,
7032 no?), but it is more likely to be a mistake.
7034 @node Call Function, Value Function, Foreach Function, Functions
7035 @section The @code{call} Function
7036 @findex call
7037 @cindex functions, user defined
7038 @cindex user defined functions
7040 The @code{call} function is unique in that it can be used to create new
7041 parameterized functions.  You can write a complex expression as the
7042 value of a variable, then use @code{call} to expand it with different
7043 values.
7045 The syntax of the @code{call} function is:
7047 @example
7048 $(call @var{variable},@var{param},@var{param},@dots{})
7049 @end example
7051 When @code{make} expands this function, it assigns each @var{param} to
7052 temporary variables @code{$(1)}, @code{$(2)}, etc.  The variable
7053 @code{$(0)} will contain @var{variable}.  There is no maximum number of
7054 parameter arguments.  There is no minimum, either, but it doesn't make
7055 sense to use @code{call} with no parameters.
7057 Then @var{variable} is expanded as a @code{make} variable in the context
7058 of these temporary assignments.  Thus, any reference to @code{$(1)} in
7059 the value of @var{variable} will resolve to the first @var{param} in the
7060 invocation of @code{call}.
7062 Note that @var{variable} is the @emph{name} of a variable, not a
7063 @emph{reference} to that variable.  Therefore you would not normally use
7064 a @samp{$} or parentheses when writing it.  (You can, however, use a
7065 variable reference in the name if you want the name not to be a
7066 constant.)
7068 If @var{variable} is the name of a builtin function, the builtin function
7069 is always invoked (even if a @code{make} variable by that name also
7070 exists).
7072 The @code{call} function expands the @var{param} arguments before
7073 assigning them to temporary variables.  This means that @var{variable}
7074 values containing references to builtin functions that have special
7075 expansion rules, like @code{foreach} or @code{if}, may not work as you
7076 expect.
7078 Some examples may make this clearer.
7080 This macro simply reverses its arguments:
7082 @smallexample
7083 reverse = $(2) $(1)
7085 foo = $(call reverse,a,b)
7086 @end smallexample
7088 @noindent
7089 Here @var{foo} will contain @samp{b a}.
7091 This one is slightly more interesting: it defines a macro to search for
7092 the first instance of a program in @code{PATH}:
7094 @smallexample
7095 pathsearch = $(firstword $(wildcard $(addsuffix /$(1),$(subst :, ,$(PATH)))))
7097 LS := $(call pathsearch,ls)
7098 @end smallexample
7100 @noindent
7101 Now the variable LS contains @code{/bin/ls} or similar.
7103 The @code{call} function can be nested.  Each recursive invocation gets
7104 its own local values for @code{$(1)}, etc.@: that mask the values of
7105 higher-level @code{call}.  For example, here is an implementation of a
7106 @dfn{map} function:
7108 @smallexample
7109 map = $(foreach a,$(2),$(call $(1),$(a)))
7110 @end smallexample
7112 Now you can @var{map} a function that normally takes only one argument,
7113 such as @code{origin}, to multiple values in one step:
7115 @smallexample
7116 o = $(call map,origin,o map MAKE)
7117 @end smallexample
7119 and end up with @var{o} containing something like @samp{file file default}.
7121 A final caution: be careful when adding whitespace to the arguments to
7122 @code{call}.  As with other functions, any whitespace contained in the
7123 second and subsequent arguments is kept; this can cause strange
7124 effects.  It's generally safest to remove all extraneous whitespace when
7125 providing parameters to @code{call}.
7127 @node Value Function, Eval Function, Call Function, Functions
7128 @comment  node-name,  next,  previous,  up
7129 @section The @code{value} Function
7130 @findex value
7131 @cindex variables, unexpanded value
7133 The @code{value} function provides a way for you to use the value of a
7134 variable @emph{without} having it expanded.  Please note that this
7135 does not undo expansions which have already occurred; for example if
7136 you create a simply expanded variable its value is expanded during the
7137 definition; in that case the @code{value} function will return the
7138 same result as using the variable directly.
7140 The syntax of the @code{value} function is:
7142 @example
7143 $(value @var{variable})
7144 @end example
7146 Note that @var{variable} is the @emph{name} of a variable; not a
7147 @emph{reference} to that variable.  Therefore you would not normally
7148 use a @samp{$} or parentheses when writing it.  (You can, however, use
7149 a variable reference in the name if you want the name not to be a
7150 constant.)
7152 The result of this function is a string containing the value of
7153 @var{variable}, without any expansion occurring.  For example, in this
7154 makefile:
7156 @example
7157 @group
7158 FOO = $PATH
7160 all:
7161         @@echo $(FOO)
7162         @@echo $(value FOO)
7163 @end group
7164 @end example
7166 @noindent
7167 The first output line would be @code{ATH}, since the ``$P'' would be
7168 expanded as a @code{make} variable, while the second output line would
7169 be the current value of your @code{$PATH} environment variable, since
7170 the @code{value} function avoided the expansion.
7172 The @code{value} function is most often used in conjunction with the
7173 @code{eval} function (@pxref{Eval Function}).
7175 @node Eval Function, Origin Function, Value Function, Functions
7176 @comment  node-name,  next,  previous,  up
7177 @section The @code{eval} Function
7178 @findex eval
7179 @cindex evaluating makefile syntax
7180 @cindex makefile syntax, evaluating
7182 The @code{eval} function is very special: it allows you to define new
7183 makefile constructs that are not constant; which are the result of
7184 evaluating other variables and functions.  The argument to the
7185 @code{eval} function is expanded, then the results of that expansion
7186 are parsed as makefile syntax.  The expanded results can define new
7187 @code{make} variables, targets, implicit or explicit rules, etc.
7189 The result of the @code{eval} function is always the empty string;
7190 thus, it can be placed virtually anywhere in a makefile without
7191 causing syntax errors.
7193 It's important to realize that the @code{eval} argument is expanded
7194 @emph{twice}; first by the @code{eval} function, then the results of
7195 that expansion are expanded again when they are parsed as makefile
7196 syntax.  This means you may need to provide extra levels of escaping
7197 for ``$'' characters when using @code{eval}.  The @code{value}
7198 function (@pxref{Value Function}) can sometimes be useful in these
7199 situations, to circumvent unwanted expansions.
7201 Here is an example of how @code{eval} can be used; this example
7202 combines a number of concepts and other functions.  Although it might
7203 seem overly complex to use @code{eval} in this example, rather than
7204 just writing out the rules, consider two things: first, the template
7205 definition (in @code{PROGRAM_template}) could need to be much more
7206 complex than it is here; and second, you might put the complex,
7207 ``generic'' part of this example into another makefile, then include
7208 it in all the individual makefiles.  Now your individual makefiles are
7209 quite straightforward.
7211 @example
7212 @group
7213 PROGRAMS    = server client
7215 server_OBJS = server.o server_priv.o server_access.o
7216 server_LIBS = priv protocol
7218 client_OBJS = client.o client_api.o client_mem.o
7219 client_LIBS = protocol
7221 # Everything after this is generic
7223 .PHONY: all
7224 all: $(PROGRAMS)
7226 define PROGRAM_template =
7227  $(1): $$($(1)_OBJS) $$($(1)_LIBS:%=-l%)
7228  ALL_OBJS   += $$($(1)_OBJS)
7229 endef
7231 $(foreach prog,$(PROGRAMS),$(eval $(call PROGRAM_template,$(prog))))
7233 $(PROGRAMS):
7234         $(LINK.o) $^ $(LDLIBS) -o $@@
7236 clean:
7237         rm -f $(ALL_OBJS) $(PROGRAMS)
7238 @end group
7239 @end example
7241 @node Origin Function, Flavor Function, Eval Function, Functions
7242 @section The @code{origin} Function
7243 @findex origin
7244 @cindex variables, origin of
7245 @cindex origin of variable
7247 The @code{origin} function is unlike most other functions in that it does
7248 not operate on the values of variables; it tells you something @emph{about}
7249 a variable.  Specifically, it tells you where it came from.
7251 The syntax of the @code{origin} function is:
7253 @example
7254 $(origin @var{variable})
7255 @end example
7257 Note that @var{variable} is the @emph{name} of a variable to inquire about;
7258 not a @emph{reference} to that variable.  Therefore you would not normally
7259 use a @samp{$} or parentheses when writing it.  (You can, however, use a
7260 variable reference in the name if you want the name not to be a constant.)
7262 The result of this function is a string telling you how the variable
7263 @var{variable} was defined:
7265 @table @samp
7266 @item undefined
7268 if @var{variable} was never defined.
7270 @item default
7272 if @var{variable} has a default definition, as is usual with @code{CC}
7273 and so on.  @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
7274 Note that if you have redefined a default variable, the @code{origin}
7275 function will return the origin of the later definition.
7277 @item environment
7279 if @var{variable} was inherited from the environment provided to
7280 @code{make}.
7282 @item environment override
7284 if @var{variable} was inherited from the environment provided to
7285 @code{make}, and is overriding a setting for @var{variable} in the
7286 makefile as a result of the @w{@samp{-e}} option (@pxref{Options
7287 Summary, ,Summary of Options}).@refill
7289 @item file
7291 if @var{variable} was defined in a makefile.
7293 @item command line
7295 if @var{variable} was defined on the command line.
7297 @item override
7299 if @var{variable} was defined with an @code{override} directive in a
7300 makefile (@pxref{Override Directive, ,The @code{override} Directive}).
7302 @item automatic
7304 if @var{variable} is an automatic variable defined for the execution
7305 of the recipe for each rule (@pxref{Automatic Variables}).
7306 @end table
7308 This information is primarily useful (other than for your curiosity) to
7309 determine if you want to believe the value of a variable.  For example,
7310 suppose you have a makefile @file{foo} that includes another makefile
7311 @file{bar}.  You want a variable @code{bletch} to be defined in @file{bar}
7312 if you run the command @w{@samp{make -f bar}}, even if the environment contains
7313 a definition of @code{bletch}.  However, if @file{foo} defined
7314 @code{bletch} before including @file{bar}, you do not want to override that
7315 definition.  This could be done by using an @code{override} directive in
7316 @file{foo}, giving that definition precedence over the later definition in
7317 @file{bar}; unfortunately, the @code{override} directive would also
7318 override any command line definitions.  So, @file{bar} could
7319 include:@refill
7321 @example
7322 @group
7323 ifdef bletch
7324 ifeq "$(origin bletch)" "environment"
7325 bletch = barf, gag, etc.
7326 endif
7327 endif
7328 @end group
7329 @end example
7331 @noindent
7332 If @code{bletch} has been defined from the environment, this will redefine
7335 If you want to override a previous definition of @code{bletch} if it came
7336 from the environment, even under @samp{-e}, you could instead write:
7338 @example
7339 @group
7340 ifneq "$(findstring environment,$(origin bletch))" ""
7341 bletch = barf, gag, etc.
7342 endif
7343 @end group
7344 @end example
7346 Here the redefinition takes place if @samp{$(origin bletch)} returns either
7347 @samp{environment} or @samp{environment override}.
7348 @xref{Text Functions, , Functions for String Substitution and Analysis}.
7350 @node Flavor Function, Shell Function, Origin Function, Functions
7351 @section The @code{flavor} Function
7352 @findex flavor
7353 @cindex variables, flavor of
7354 @cindex flavor of variable
7356 The @code{flavor} function is unlike most other functions (and like
7357 @code{origin} function) in that it does not operate on the values of
7358 variables; it tells you something @emph{about} a variable.
7359 Specifically, it tells you the flavor of a variable (@pxref{Flavors,
7360 ,The Two Flavors of Variables}).
7362 The syntax of the @code{flavor} function is:
7364 @example
7365 $(flavor @var{variable})
7366 @end example
7368 Note that @var{variable} is the @emph{name} of a variable to inquire about;
7369 not a @emph{reference} to that variable.  Therefore you would not normally
7370 use a @samp{$} or parentheses when writing it.  (You can, however, use a
7371 variable reference in the name if you want the name not to be a constant.)
7373 The result of this function is a string that identifies the flavor of the
7374 variable @var{variable}:
7376 @table @samp
7377 @item undefined
7379 if @var{variable} was never defined.
7381 @item recursive
7383 if @var{variable} is a recursively expanded variable.
7385 @item simple
7387 if @var{variable} is a simply expanded variable.
7389 @end table
7392 @node Shell Function, Make Control Functions, Flavor Function, Functions
7393 @section The @code{shell} Function
7394 @findex shell
7395 @cindex command expansion
7396 @cindex backquotes
7397 @cindex shell command, function for
7399 The @code{shell} function is unlike any other function other than the
7400 @code{wildcard} function
7401 (@pxref{Wildcard Function, ,The Function @code{wildcard}}) in that it
7402 communicates with the world outside of @code{make}.
7404 The @code{shell} function performs the same function that backquotes
7405 (@samp{`}) perform in most shells: it does @dfn{command expansion}.
7406 This means that it takes as an argument a shell command and evaluates
7407 to the output of the command.  The only processing @code{make} does on
7408 the result is to convert each newline (or carriage-return / newline
7409 pair) to a single space.  If there is a trailing (carriage-return
7410 and) newline it will simply be removed.@refill
7412 The commands run by calls to the @code{shell} function are run when the
7413 function calls are expanded (@pxref{Reading Makefiles, , How
7414 @code{make} Reads a Makefile}).  Because this function involves
7415 spawning a new shell, you should carefully consider the performance
7416 implications of using the @code{shell} function within recursively
7417 expanded variables vs.@: simply expanded variables (@pxref{Flavors, ,The
7418 Two Flavors of Variables}).
7420 Here are some examples of the use of the @code{shell} function:
7422 @example
7423 contents := $(shell cat foo)
7424 @end example
7426 @noindent
7427 sets @code{contents} to the contents of the file @file{foo}, with a space
7428 (rather than a newline) separating each line.
7430 @example
7431 files := $(shell echo *.c)
7432 @end example
7434 @noindent
7435 sets @code{files} to the expansion of @samp{*.c}.  Unless @code{make} is
7436 using a very strange shell, this has the same result as
7437 @w{@samp{$(wildcard *.c)}} (as long as at least one @samp{.c} file
7438 exists).@refill
7440 @node Make Control Functions,  , Shell Function, Functions
7441 @section Functions That Control Make
7442 @cindex functions, for controlling make
7443 @cindex controlling make
7445 These functions control the way make runs.  Generally, they are used to
7446 provide information to the user of the makefile or to cause make to stop
7447 if some sort of environmental error is detected.
7449 @table @code
7450 @item $(error @var{text}@dots{})
7451 @findex error
7452 @cindex error, stopping on
7453 @cindex stopping make
7454 Generates a fatal error where the message is @var{text}.  Note that
7455 the error is generated whenever this function is evaluated.  So, if
7456 you put it inside a recipe or on the right side of a recursive
7457 variable assignment, it won't be evaluated until later.  The
7458 @var{text} will be expanded before the error is generated.
7460 For example,
7462 @example
7463 ifdef ERROR1
7464 $(error error is $(ERROR1))
7465 endif
7466 @end example
7468 @noindent
7469 will generate a fatal error during the read of the makefile if the
7470 @code{make} variable @code{ERROR1} is defined.  Or,
7472 @example
7473 ERR = $(error found an error!)
7475 .PHONY: err
7476 err: ; $(ERR)
7477 @end example
7479 @noindent
7480 will generate a fatal error while @code{make} is running, if the
7481 @code{err} target is invoked.
7483 @item $(warning @var{text}@dots{})
7484 @findex warning
7485 @cindex warnings, printing
7486 @cindex printing user warnings
7487 This function works similarly to the @code{error} function, above,
7488 except that @code{make} doesn't exit.  Instead, @var{text} is expanded
7489 and the resulting message is displayed, but processing of the makefile
7490 continues.
7492 The result of the expansion of this function is the empty string.
7494 @item $(info @var{text}@dots{})
7495 @findex info
7496 @cindex printing messages
7497 This function does nothing more than print its (expanded) argument(s)
7498 to standard output.  No makefile name or line number is added.  The
7499 result of the expansion of this function is the empty string.
7500 @end table
7502 @node Running, Implicit Rules, Functions, Top
7503 @chapter How to Run @code{make}
7505 A makefile that says how to recompile a program can be used in more
7506 than one way.  The simplest use is to recompile every file that is out
7507 of date.  Usually, makefiles are written so that if you run
7508 @code{make} with no arguments, it does just that.
7510 But you might want to update only some of the files; you might want to use
7511 a different compiler or different compiler options; you might want just to
7512 find out which files are out of date without changing them.
7514 By giving arguments when you run @code{make}, you can do any of these
7515 things and many others.
7517 @cindex exit status of make
7518 The exit status of @code{make} is always one of three values:
7519 @table @code
7520 @item 0
7521 The exit status is zero if @code{make} is successful.
7522 @item 2
7523 The exit status is two if @code{make} encounters any errors.
7524 It will print messages describing the particular errors.
7525 @item 1
7526 The exit status is one if you use the @samp{-q} flag and @code{make}
7527 determines that some target is not already up to date.
7528 @xref{Instead of Execution, ,Instead of Executing Recipes}.
7529 @end table
7531 @menu
7532 * Makefile Arguments::          How to specify which makefile to use.
7533 * Goals::                       How to use goal arguments to specify which
7534                                   parts of the makefile to use.
7535 * Instead of Execution::        How to use mode flags to specify what
7536                                   kind of thing to do with the recipes
7537                                   in the makefile other than simply
7538                                   execute them.
7539 * Avoiding Compilation::        How to avoid recompiling certain files.
7540 * Overriding::                  How to override a variable to specify
7541                                   an alternate compiler and other things.
7542 * Testing::                     How to proceed past some errors, to
7543                                   test compilation.
7544 * Options Summary::             Summary of Options
7545 @end menu
7547 @node Makefile Arguments, Goals, Running, Running
7548 @section Arguments to Specify the Makefile
7549 @cindex @code{--file}
7550 @cindex @code{--makefile}
7551 @cindex @code{-f}
7553 The way to specify the name of the makefile is with the @samp{-f} or
7554 @samp{--file} option (@samp{--makefile} also works).  For example,
7555 @samp{-f altmake} says to use the file @file{altmake} as the makefile.
7557 If you use the @samp{-f} flag several times and follow each @samp{-f}
7558 with an argument, all the specified files are used jointly as
7559 makefiles.
7561 If you do not use the @samp{-f} or @samp{--file} flag, the default is
7562 to try @file{GNUmakefile}, @file{makefile}, and @file{Makefile}, in
7563 that order, and use the first of these three which exists or can be made
7564 (@pxref{Makefiles, ,Writing Makefiles}).@refill
7566 @node Goals, Instead of Execution, Makefile Arguments, Running
7567 @section Arguments to Specify the Goals
7568 @cindex goal, how to specify
7570 The @dfn{goals} are the targets that @code{make} should strive ultimately
7571 to update.  Other targets are updated as well if they appear as
7572 prerequisites of goals, or prerequisites of prerequisites of goals, etc.
7574 By default, the goal is the first target in the makefile (not counting
7575 targets that start with a period).  Therefore, makefiles are usually
7576 written so that the first target is for compiling the entire program or
7577 programs they describe.  If the first rule in the makefile has several
7578 targets, only the first target in the rule becomes the default goal, not
7579 the whole list.  You can manage the selection of the default goal from
7580 within your makefile using the @code{.DEFAULT_GOAL} variable
7581 (@pxref{Special Variables, , Other Special Variables}).
7583 You can also specify a different goal or goals with command line
7584 arguments to @code{make}.  Use the name of the goal as an argument.
7585 If you specify several goals, @code{make} processes each of them in
7586 turn, in the order you name them.
7588 Any target in the makefile may be specified as a goal (unless it
7589 starts with @samp{-} or contains an @samp{=}, in which case it will be
7590 parsed as a switch or variable definition, respectively).  Even
7591 targets not in the makefile may be specified, if @code{make} can find
7592 implicit rules that say how to make them.
7594 @vindex MAKECMDGOALS
7595 @code{Make} will set the special variable @code{MAKECMDGOALS} to the
7596 list of goals you specified on the command line.  If no goals were given
7597 on the command line, this variable is empty.  Note that this variable
7598 should be used only in special circumstances.
7600 An example of appropriate use is to avoid including @file{.d} files
7601 during @code{clean} rules (@pxref{Automatic Prerequisites}), so
7602 @code{make} won't create them only to immediately remove them
7603 again:@refill
7605 @example
7606 @group
7607 sources = foo.c bar.c
7609 ifneq ($(MAKECMDGOALS),clean)
7610 include $(sources:.c=.d)
7611 endif
7612 @end group
7613 @end example
7615 One use of specifying a goal is if you want to compile only a part of
7616 the program, or only one of several programs.  Specify as a goal each
7617 file that you wish to remake.  For example, consider a directory containing
7618 several programs, with a makefile that starts like this:
7620 @example
7621 .PHONY: all
7622 all: size nm ld ar as
7623 @end example
7625 If you are working on the program @code{size}, you might want to say
7626 @w{@samp{make size}} so that only the files of that program are recompiled.
7628 Another use of specifying a goal is to make files that are not normally
7629 made.  For example, there may be a file of debugging output, or a
7630 version of the program that is compiled specially for testing, which has
7631 a rule in the makefile but is not a prerequisite of the default goal.
7633 Another use of specifying a goal is to run the recipe associated with
7634 a phony target (@pxref{Phony Targets}) or empty target (@pxref{Empty
7635 Targets, ,Empty Target Files to Record Events}).  Many makefiles contain
7636 a phony target named @file{clean} which deletes everything except source
7637 files.  Naturally, this is done only if you request it explicitly with
7638 @w{@samp{make clean}}.  Following is a list of typical phony and empty
7639 target names.  @xref{Standard Targets}, for a detailed list of all the
7640 standard target names which GNU software packages use.
7642 @table @file
7643 @item all
7644 @cindex @code{all} @r{(standard target)}
7645 Make all the top-level targets the makefile knows about.
7647 @item clean
7648 @cindex @code{clean} @r{(standard target)}
7649 Delete all files that are normally created by running @code{make}.
7651 @item mostlyclean
7652 @cindex @code{mostlyclean} @r{(standard target)}
7653 Like @samp{clean}, but may refrain from deleting a few files that people
7654 normally don't want to recompile.  For example, the @samp{mostlyclean}
7655 target for GCC does not delete @file{libgcc.a}, because recompiling it
7656 is rarely necessary and takes a lot of time.
7658 @item distclean
7659 @cindex @code{distclean} @r{(standard target)}
7660 @itemx realclean
7661 @cindex @code{realclean} @r{(standard target)}
7662 @itemx clobber
7663 @cindex @code{clobber} @r{(standard target)}
7664 Any of these targets might be defined to delete @emph{more} files than
7665 @samp{clean} does.  For example, this would delete configuration files
7666 or links that you would normally create as preparation for compilation,
7667 even if the makefile itself cannot create these files.
7669 @item install
7670 @cindex @code{install} @r{(standard target)}
7671 Copy the executable file into a directory that users typically search
7672 for commands; copy any auxiliary files that the executable uses into
7673 the directories where it will look for them.
7675 @item print
7676 @cindex @code{print} @r{(standard target)}
7677 Print listings of the source files that have changed.
7679 @item tar
7680 @cindex @code{tar} @r{(standard target)}
7681 Create a tar file of the source files.
7683 @item shar
7684 @cindex @code{shar} @r{(standard target)}
7685 Create a shell archive (shar file) of the source files.
7687 @item dist
7688 @cindex @code{dist} @r{(standard target)}
7689 Create a distribution file of the source files.  This might
7690 be a tar file, or a shar file, or a compressed version of one of the
7691 above, or even more than one of the above.
7693 @item TAGS
7694 @cindex @code{TAGS} @r{(standard target)}
7695 Update a tags table for this program.
7697 @item check
7698 @cindex @code{check} @r{(standard target)}
7699 @itemx test
7700 @cindex @code{test} @r{(standard target)}
7701 Perform self tests on the program this makefile builds.
7702 @end table
7704 @node Instead of Execution, Avoiding Compilation, Goals, Running
7705 @section Instead of Executing Recipes
7706 @cindex execution, instead of
7707 @cindex recipes, instead of executing
7709 The makefile tells @code{make} how to tell whether a target is up to date,
7710 and how to update each target.  But updating the targets is not always
7711 what you want.  Certain options specify other activities for @code{make}.
7713 @comment Extra blank lines make it print better.
7714 @table @samp
7715 @item -n
7716 @itemx --just-print
7717 @itemx --dry-run
7718 @itemx --recon
7719 @cindex @code{--just-print}
7720 @cindex @code{--dry-run}
7721 @cindex @code{--recon}
7722 @cindex @code{-n}
7724 ``No-op''.  The activity is to print what recipe would be used to make
7725 the targets up to date, but not actually execute it.  Some recipes are
7726 still executed, even with this flag (@pxref{MAKE Variable, ,How the @code{MAKE} Variable Works}).
7728 @item -t
7729 @itemx --touch
7730 @cindex @code{--touch}
7731 @cindex touching files
7732 @cindex target, touching
7733 @cindex @code{-t}
7735 ``Touch''.  The activity is to mark the targets as up to date without
7736 actually changing them.  In other words, @code{make} pretends to compile
7737 the targets but does not really change their contents.
7739 @item -q
7740 @itemx --question
7741 @cindex @code{--question}
7742 @cindex @code{-q}
7743 @cindex question mode
7745 ``Question''.  The activity is to find out silently whether the targets
7746 are up to date already; but execute no recipe in either case.  In other
7747 words, neither compilation nor output will occur.
7749 @item -W @var{file}
7750 @itemx --what-if=@var{file}
7751 @itemx --assume-new=@var{file}
7752 @itemx --new-file=@var{file}
7753 @cindex @code{--what-if}
7754 @cindex @code{-W}
7755 @cindex @code{--assume-new}
7756 @cindex @code{--new-file}
7757 @cindex what if
7758 @cindex files, assuming new
7760 ``What if''.  Each @samp{-W} flag is followed by a file name.  The given
7761 files' modification times are recorded by @code{make} as being the present
7762 time, although the actual modification times remain the same.
7763 You can use the @samp{-W} flag in conjunction with the @samp{-n} flag
7764 to see what would happen if you were to modify specific files.@refill
7765 @end table
7767 With the @samp{-n} flag, @code{make} prints the recipe that it would
7768 normally execute but usually does not execute it.
7770 With the @samp{-t} flag, @code{make} ignores the recipes in the rules
7771 and uses (in effect) the command @code{touch} for each target that needs to
7772 be remade.  The @code{touch} command is also printed, unless @samp{-s} or
7773 @code{.SILENT} is used.  For speed, @code{make} does not actually invoke
7774 the program @code{touch}.  It does the work directly.
7776 With the @samp{-q} flag, @code{make} prints nothing and executes no
7777 recipes, but the exit status code it returns is zero if and only if the
7778 targets to be considered are already up to date.  If the exit status is
7779 one, then some updating needs to be done.  If @code{make} encounters an
7780 error, the exit status is two, so you can distinguish an error from a
7781 target that is not up to date.
7783 It is an error to use more than one of these three flags in the same
7784 invocation of @code{make}.
7786 @cindex +, and recipe execution
7787 The @samp{-n}, @samp{-t}, and @samp{-q} options do not affect recipe
7788 lines that begin with @samp{+} characters or contain the strings
7789 @samp{$(MAKE)} or @samp{$@{MAKE@}}.  Note that only the line containing
7790 the @samp{+} character or the strings @samp{$(MAKE)} or @samp{$@{MAKE@}}
7791 is run regardless of these options.  Other lines in the same rule are
7792 not run unless they too begin with @samp{+} or contain @samp{$(MAKE)} or
7793 @samp{$@{MAKE@}} (@xref{MAKE Variable, ,How the @code{MAKE} Variable Works}.)
7795 The @samp{-W} flag provides two features:
7797 @itemize @bullet
7798 @item
7799 If you also use the @samp{-n} or @samp{-q} flag, you can see what
7800 @code{make} would do if you were to modify some files.
7802 @item
7803 Without the @samp{-n} or @samp{-q} flag, when @code{make} is actually
7804 executing recipes, the @samp{-W} flag can direct @code{make} to act as
7805 if some files had been modified, without actually running the recipes
7806 for those files.@refill
7807 @end itemize
7809 Note that the options @samp{-p} and @samp{-v} allow you to obtain other
7810 information about @code{make} or about the makefiles in use
7811 (@pxref{Options Summary, ,Summary of Options}).@refill
7813 @node Avoiding Compilation, Overriding, Instead of Execution, Running
7814 @section Avoiding Recompilation of Some Files
7815 @cindex @code{-o}
7816 @cindex @code{--old-file}
7817 @cindex @code{--assume-old}
7818 @cindex files, assuming old
7819 @cindex files, avoiding recompilation of
7820 @cindex recompilation, avoiding
7822 Sometimes you may have changed a source file but you do not want to
7823 recompile all the files that depend on it.  For example, suppose you add
7824 a macro or a declaration to a header file that many other files depend
7825 on.  Being conservative, @code{make} assumes that any change in the
7826 header file requires recompilation of all dependent files, but you know
7827 that they do not need to be recompiled and you would rather not waste
7828 the time waiting for them to compile.
7830 If you anticipate the problem before changing the header file, you can
7831 use the @samp{-t} flag.  This flag tells @code{make} not to run the
7832 recipes in the rules, but rather to mark the target up to date by
7833 changing its last-modification date.  You would follow this procedure:
7835 @enumerate
7836 @item
7837 Use the command @samp{make} to recompile the source files that really
7838 need recompilation, ensuring that the object files are up-to-date
7839 before you begin.
7841 @item
7842 Make the changes in the header files.
7844 @item
7845 Use the command @samp{make -t} to mark all the object files as
7846 up to date.  The next time you run @code{make}, the changes in the
7847 header files will not cause any recompilation.
7848 @end enumerate
7850 If you have already changed the header file at a time when some files
7851 do need recompilation, it is too late to do this.  Instead, you can
7852 use the @w{@samp{-o @var{file}}} flag, which marks a specified file as
7853 ``old'' (@pxref{Options Summary, ,Summary of Options}).  This means
7854 that the file itself will not be remade, and nothing else will be
7855 remade on its account.  Follow this procedure:
7857 @enumerate
7858 @item
7859 Recompile the source files that need compilation for reasons independent
7860 of the particular header file, with @samp{make -o @var{headerfile}}.
7861 If several header files are involved, use a separate @samp{-o} option
7862 for each header file.
7864 @item
7865 Touch all the object files with @samp{make -t}.
7866 @end enumerate
7868 @node Overriding, Testing, Avoiding Compilation, Running
7869 @section Overriding Variables
7870 @cindex overriding variables with arguments
7871 @cindex variables, overriding with arguments
7872 @cindex command line variables
7873 @cindex variables, command line
7875 An argument that contains @samp{=} specifies the value of a variable:
7876 @samp{@var{v}=@var{x}} sets the value of the variable @var{v} to @var{x}.
7877 If you specify a value in this way, all ordinary assignments of the same
7878 variable in the makefile are ignored; we say they have been
7879 @dfn{overridden} by the command line argument.
7881 The most common way to use this facility is to pass extra flags to
7882 compilers.  For example, in a properly written makefile, the variable
7883 @code{CFLAGS} is included in each recipe that runs the C compiler, so a
7884 file @file{foo.c} would be compiled something like this:
7886 @example
7887 cc -c $(CFLAGS) foo.c
7888 @end example
7890 Thus, whatever value you set for @code{CFLAGS} affects each compilation
7891 that occurs.  The makefile probably specifies the usual value for
7892 @code{CFLAGS}, like this:
7894 @example
7895 CFLAGS=-g
7896 @end example
7898 Each time you run @code{make}, you can override this value if you
7899 wish.  For example, if you say @samp{make CFLAGS='-g -O'}, each C
7900 compilation will be done with @samp{cc -c -g -O}.  (This also
7901 illustrates how you can use quoting in the shell to enclose spaces and
7902 other special characters in the value of a variable when you override
7903 it.)
7905 The variable @code{CFLAGS} is only one of many standard variables that
7906 exist just so that you can change them this way.  @xref{Implicit
7907 Variables, , Variables Used by Implicit Rules}, for a complete list.
7909 You can also program the makefile to look at additional variables of your
7910 own, giving the user the ability to control other aspects of how the
7911 makefile works by changing the variables.
7913 When you override a variable with a command line argument, you can
7914 define either a recursively-expanded variable or a simply-expanded
7915 variable.  The examples shown above make a recursively-expanded
7916 variable; to make a simply-expanded variable, write @samp{:=} instead
7917 of @samp{=}.  But, unless you want to include a variable reference or
7918 function call in the @emph{value} that you specify, it makes no
7919 difference which kind of variable you create.
7921 There is one way that the makefile can change a variable that you have
7922 overridden.  This is to use the @code{override} directive, which is a line
7923 that looks like this: @samp{override @var{variable} = @var{value}}
7924 (@pxref{Override Directive, ,The @code{override} Directive}).
7926 @node Testing, Options Summary, Overriding, Running
7927 @section Testing the Compilation of a Program
7928 @cindex testing compilation
7929 @cindex compilation, testing
7931 Normally, when an error happens in executing a shell command, @code{make}
7932 gives up immediately, returning a nonzero status.  No further recipes are
7933 executed for any target.  The error implies that the goal cannot be
7934 correctly remade, and @code{make} reports this as soon as it knows.
7936 When you are compiling a program that you have just changed, this is not
7937 what you want.  Instead, you would rather that @code{make} try compiling
7938 every file that can be tried, to show you as many compilation errors
7939 as possible.
7941 @cindex @code{-k}
7942 @cindex @code{--keep-going}
7943 On these occasions, you should use the @samp{-k} or
7944 @samp{--keep-going} flag.  This tells @code{make} to continue to
7945 consider the other prerequisites of the pending targets, remaking them
7946 if necessary, before it gives up and returns nonzero status.  For
7947 example, after an error in compiling one object file, @samp{make -k}
7948 will continue compiling other object files even though it already
7949 knows that linking them will be impossible.  In addition to continuing
7950 after failed shell commands, @samp{make -k} will continue as much as
7951 possible after discovering that it does not know how to make a target
7952 or prerequisite file.  This will always cause an error message, but
7953 without @samp{-k}, it is a fatal error (@pxref{Options Summary,
7954 ,Summary of Options}).@refill
7956 The usual behavior of @code{make} assumes that your purpose is to get the
7957 goals up to date; once @code{make} learns that this is impossible, it might
7958 as well report the failure immediately.  The @samp{-k} flag says that the
7959 real purpose is to test as much as possible of the changes made in the
7960 program, perhaps to find several independent problems so that you can
7961 correct them all before the next attempt to compile.  This is why Emacs'
7962 @kbd{M-x compile} command passes the @samp{-k} flag by default.
7964 @node Options Summary,  , Testing, Running
7965 @section Summary of Options
7966 @cindex options
7967 @cindex flags
7968 @cindex switches
7970 Here is a table of all the options @code{make} understands:
7972 @table @samp
7973 @item -b
7974 @cindex @code{-b}
7975 @itemx -m
7976 @cindex @code{-m}
7977 These options are ignored for compatibility with other versions of @code{make}.
7979 @item -B
7980 @cindex @code{-B}
7981 @itemx --always-make
7982 @cindex @code{--always-make}
7983 Consider all targets out-of-date.  GNU @code{make} proceeds to
7984 consider targets and their prerequisites using the normal algorithms;
7985 however, all targets so considered are always remade regardless of the
7986 status of their prerequisites.  To avoid infinite recursion, if
7987 @code{MAKE_RESTARTS} (@pxref{Special Variables, , Other Special
7988 Variables}) is set to a number greater than 0 this option is disabled
7989 when considering whether to remake makefiles (@pxref{Remaking
7990 Makefiles, , How Makefiles Are Remade}).
7992 @item -C @var{dir}
7993 @cindex @code{-C}
7994 @itemx --directory=@var{dir}
7995 @cindex @code{--directory}
7996 Change to directory @var{dir} before reading the makefiles.  If multiple
7997 @samp{-C} options are specified, each is interpreted relative to the
7998 previous one: @samp{-C / -C etc} is equivalent to @samp{-C /etc}.
7999 This is typically used with recursive invocations of @code{make}
8000 (@pxref{Recursion, ,Recursive Use of @code{make}}).
8002 @item -d
8003 @cindex @code{-d}
8004 @c Extra blank line here makes the table look better.
8006 Print debugging information in addition to normal processing.  The
8007 debugging information says which files are being considered for
8008 remaking, which file-times are being compared and with what results,
8009 which files actually need to be remade, which implicit rules are
8010 considered and which are applied---everything interesting about how
8011 @code{make} decides what to do.  The @code{-d} option is equivalent to
8012 @samp{--debug=a} (see below).
8014 @item --debug[=@var{options}]
8015 @cindex @code{--debug}
8016 @c Extra blank line here makes the table look better.
8018 Print debugging information in addition to normal processing.  Various
8019 levels and types of output can be chosen.  With no arguments, print the
8020 ``basic'' level of debugging.  Possible arguments are below; only the
8021 first character is considered, and values must be comma- or
8022 space-separated.
8024 @table @code
8025 @item a (@i{all})
8026 All types of debugging output are enabled.  This is equivalent to using
8027 @samp{-d}.
8029 @item b (@i{basic})
8030 Basic debugging prints each target that was found to be out-of-date, and
8031 whether the build was successful or not.
8033 @item v (@i{verbose})
8034 A level above @samp{basic}; includes messages about which makefiles were
8035 parsed, prerequisites that did not need to be rebuilt, etc.  This option
8036 also enables @samp{basic} messages.
8038 @item i (@i{implicit})
8039 Prints messages describing the implicit rule searches for each target.
8040 This option also enables @samp{basic} messages.
8042 @item j (@i{jobs})
8043 Prints messages giving details on the invocation of specific subcommands.
8045 @item m (@i{makefile})
8046 By default, the above messages are not enabled while trying to remake
8047 the makefiles.  This option enables messages while rebuilding makefiles,
8048 too.  Note that the @samp{all} option does enable this option.  This
8049 option also enables @samp{basic} messages.
8050 @end table
8052 @item -e
8053 @cindex @code{-e}
8054 @itemx --environment-overrides
8055 @cindex @code{--environment-overrides}
8056 Give variables taken from the environment precedence
8057 over variables from makefiles.
8058 @xref{Environment, ,Variables from the Environment}.
8060 @item -f @var{file}
8061 @cindex @code{-f}
8062 @itemx --file=@var{file}
8063 @cindex @code{--file}
8064 @itemx --makefile=@var{file}
8065 @cindex @code{--makefile}
8066 Read the file named @var{file} as a makefile.
8067 @xref{Makefiles, ,Writing Makefiles}.
8069 @item -h
8070 @cindex @code{-h}
8071 @itemx --help
8072 @cindex @code{--help}
8073 @c Extra blank line here makes the table look better.
8075 Remind you of the options that @code{make} understands and then exit.
8077 @item -i
8078 @cindex @code{-i}
8079 @itemx --ignore-errors
8080 @cindex @code{--ignore-errors}
8081 Ignore all errors in recipes executed to remake files.
8082 @xref{Errors, ,Errors in Recipes}.
8084 @item -I @var{dir}
8085 @cindex @code{-I}
8086 @itemx --include-dir=@var{dir}
8087 @cindex @code{--include-dir}
8088 Specifies a directory @var{dir} to search for included makefiles.
8089 @xref{Include, ,Including Other Makefiles}.  If several @samp{-I}
8090 options are used to specify several directories, the directories are
8091 searched in the order specified.
8093 @item -j [@var{jobs}]
8094 @cindex @code{-j}
8095 @itemx --jobs[=@var{jobs}]
8096 @cindex @code{--jobs}
8097 Specifies the number of recipes (jobs) to run simultaneously.  With no
8098 argument, @code{make} runs as many recipes simultaneously as possible.
8099 If there is more than one @samp{-j} option, the last one is effective.
8100 @xref{Parallel, ,Parallel Execution}, for more information on how
8101 recipes are run.  Note that this option is ignored on MS-DOS.
8103 @item -k
8104 @cindex @code{-k}
8105 @itemx --keep-going
8106 @cindex @code{--keep-going}
8107 Continue as much as possible after an error.  While the target that
8108 failed, and those that depend on it, cannot be remade, the other
8109 prerequisites of these targets can be processed all the same.
8110 @xref{Testing, ,Testing the Compilation of a Program}.
8112 @item -l [@var{load}]
8113 @cindex @code{-l}
8114 @itemx --load-average[=@var{load}]
8115 @cindex @code{--load-average}
8116 @itemx --max-load[=@var{load}]
8117 @cindex @code{--max-load}
8118 Specifies that no new recipes should be started if there are other
8119 recipes running and the load average is at least @var{load} (a
8120 floating-point number).  With no argument, removes a previous load
8121 limit.  @xref{Parallel, ,Parallel Execution}.
8123 @item -L
8124 @cindex @code{-L}
8125 @itemx --check-symlink-times
8126 @cindex @code{--check-symlink-times}
8127 On systems that support symbolic links, this option causes @code{make}
8128 to consider the timestamps on any symbolic links in addition to the
8129 timestamp on the file referenced by those links.  When this option is
8130 provided, the most recent timestamp among the file and the symbolic
8131 links is taken as the modification time for this target file.
8133 @item -n
8134 @cindex @code{-n}
8135 @itemx --just-print
8136 @cindex @code{--just-print}
8137 @itemx --dry-run
8138 @cindex @code{--dry-run}
8139 @itemx --recon
8140 @cindex @code{--recon}
8141 @c Extra blank line here makes the table look better.
8143 Print the recipe that would be executed, but do not execute it (except
8144 in certain circumstances).
8145 @xref{Instead of Execution, ,Instead of Executing Recipes}.
8147 @item -o @var{file}
8148 @cindex @code{-o}
8149 @itemx --old-file=@var{file}
8150 @cindex @code{--old-file}
8151 @itemx --assume-old=@var{file}
8152 @cindex @code{--assume-old}
8153 Do not remake the file @var{file} even if it is older than its
8154 prerequisites, and do not remake anything on account of changes in
8155 @var{file}.  Essentially the file is treated as very old and its rules
8156 are ignored.  @xref{Avoiding Compilation, ,Avoiding Recompilation of
8157 Some Files}.@refill
8159 @item -p
8160 @cindex @code{-p}
8161 @itemx --print-data-base
8162 @cindex @code{--print-data-base}
8163 @cindex data base of @code{make} rules
8164 @cindex predefined rules and variables, printing
8165 Print the data base (rules and variable values) that results from
8166 reading the makefiles; then execute as usual or as otherwise
8167 specified.  This also prints the version information given by the
8168 @samp{-v} switch (see below).  To print the data base without trying
8169 to remake any files, use @w{@samp{make -qp}}.  To print the data base
8170 of predefined rules and variables, use @w{@samp{make -p -f /dev/null}}.
8171 The data base output contains filename and linenumber information for
8172 recipe and variable definitions, so it can be a useful debugging tool
8173 in complex environments.
8175 @item -q
8176 @cindex @code{-q}
8177 @itemx --question
8178 @cindex @code{--question}
8179 ``Question mode''.  Do not run any recipes, or print anything; just
8180 return an exit status that is zero if the specified targets are already
8181 up to date, one if any remaking is required, or two if an error is
8182 encountered.  @xref{Instead of Execution, ,Instead of Executing
8183 Recipes}.@refill
8185 @item -r
8186 @cindex @code{-r}
8187 @itemx --no-builtin-rules
8188 @cindex @code{--no-builtin-rules}
8189 Eliminate use of the built-in implicit rules (@pxref{Implicit Rules,
8190 ,Using Implicit Rules}).  You can still define your own by writing
8191 pattern rules (@pxref{Pattern Rules, ,Defining and Redefining Pattern
8192 Rules}).  The @samp{-r} option also clears out the default list of
8193 suffixes for suffix rules (@pxref{Suffix Rules, ,Old-Fashioned Suffix
8194 Rules}).  But you can still define your own suffixes with a rule for
8195 @code{.SUFFIXES}, and then define your own suffix rules.  Note that only
8196 @emph{rules} are affected by the @code{-r} option; default variables
8197 remain in effect (@pxref{Implicit Variables, ,Variables Used by Implicit
8198 Rules}); see the @samp{-R} option below.
8200 @item -R
8201 @cindex @code{-R}
8202 @itemx --no-builtin-variables
8203 @cindex @code{--no-builtin-variables}
8204 Eliminate use of the built-in rule-specific variables (@pxref{Implicit
8205 Variables, ,Variables Used by Implicit Rules}).  You can still define
8206 your own, of course.  The @samp{-R} option also automatically enables
8207 the @samp{-r} option (see above), since it doesn't make sense to have
8208 implicit rules without any definitions for the variables that they use.
8210 @item -s
8211 @cindex @code{-s}
8212 @itemx --silent
8213 @cindex @code{--silent}
8214 @itemx --quiet
8215 @cindex @code{--quiet}
8216 @c Extra blank line here makes the table look better.
8218 Silent operation; do not print the recipes as they are executed.
8219 @xref{Echoing, ,Recipe Echoing}.
8221 @item -S
8222 @cindex @code{-S}
8223 @itemx --no-keep-going
8224 @cindex @code{--no-keep-going}
8225 @itemx --stop
8226 @cindex @code{--stop}
8227 @c Extra blank line here makes the table look better.
8229 Cancel the effect of the @samp{-k} option.  This is never necessary
8230 except in a recursive @code{make} where @samp{-k} might be inherited
8231 from the top-level @code{make} via @code{MAKEFLAGS}
8232 (@pxref{Recursion, ,Recursive Use of @code{make}})
8233 or if you set @samp{-k} in @code{MAKEFLAGS} in your environment.@refill
8235 @item -t
8236 @cindex @code{-t}
8237 @itemx --touch
8238 @cindex @code{--touch}
8239 @c Extra blank line here makes the table look better.
8241 Touch files (mark them up to date without really changing them)
8242 instead of running their recipes.  This is used to pretend that the
8243 recipes were done, in order to fool future invocations of
8244 @code{make}.  @xref{Instead of Execution, ,Instead of Executing Recipes}.
8246 @item -v
8247 @cindex @code{-v}
8248 @itemx --version
8249 @cindex @code{--version}
8250 Print the version of the @code{make} program plus a copyright, a list
8251 of authors, and a notice that there is no warranty; then exit.
8253 @item -w
8254 @cindex @code{-w}
8255 @itemx --print-directory
8256 @cindex @code{--print-directory}
8257 Print a message containing the working directory both before and after
8258 executing the makefile.  This may be useful for tracking down errors
8259 from complicated nests of recursive @code{make} commands.
8260 @xref{Recursion, ,Recursive Use of @code{make}}.  (In practice, you
8261 rarely need to specify this option since @samp{make} does it for you;
8262 see @ref{-w Option, ,The @samp{--print-directory} Option}.)
8264 @itemx --no-print-directory
8265 @cindex @code{--no-print-directory}
8266 Disable printing of the working directory under @code{-w}.
8267 This option is useful when @code{-w} is turned on automatically,
8268 but you do not want to see the extra messages.
8269 @xref{-w Option, ,The @samp{--print-directory} Option}.
8271 @item -W @var{file}
8272 @cindex @code{-W}
8273 @itemx --what-if=@var{file}
8274 @cindex @code{--what-if}
8275 @itemx --new-file=@var{file}
8276 @cindex @code{--new-file}
8277 @itemx --assume-new=@var{file}
8278 @cindex @code{--assume-new}
8279 Pretend that the target @var{file} has just been modified.  When used
8280 with the @samp{-n} flag, this shows you what would happen if you were
8281 to modify that file.  Without @samp{-n}, it is almost the same as
8282 running a @code{touch} command on the given file before running
8283 @code{make}, except that the modification time is changed only in the
8284 imagination of @code{make}.
8285 @xref{Instead of Execution, ,Instead of Executing Recipes}.
8287 @item --warn-undefined-variables
8288 @cindex @code{--warn-undefined-variables}
8289 @cindex variables, warning for undefined
8290 @cindex undefined variables, warning message
8291 Issue a warning message whenever @code{make} sees a reference to an
8292 undefined variable.  This can be helpful when you are trying to debug
8293 makefiles which use variables in complex ways.
8294 @end table
8296 @node Implicit Rules, Archives, Running, Top
8297 @chapter Using Implicit Rules
8298 @cindex implicit rule
8299 @cindex rule, implicit
8301 Certain standard ways of remaking target files are used very often.  For
8302 example, one customary way to make an object file is from a C source file
8303 using the C compiler, @code{cc}.
8305 @dfn{Implicit rules} tell @code{make} how to use customary techniques so
8306 that you do not have to specify them in detail when you want to use
8307 them.  For example, there is an implicit rule for C compilation.  File
8308 names determine which implicit rules are run.  For example, C
8309 compilation typically takes a @file{.c} file and makes a @file{.o} file.
8310 So @code{make} applies the implicit rule for C compilation when it sees
8311 this combination of file name endings.@refill
8313 A chain of implicit rules can apply in sequence; for example, @code{make}
8314 will remake a @file{.o} file from a @file{.y} file by way of a @file{.c} file.
8315 @iftex
8316 @xref{Chained Rules, ,Chains of Implicit Rules}.
8317 @end iftex
8319 The built-in implicit rules use several variables in their recipes so
8320 that, by changing the values of the variables, you can change the way the
8321 implicit rule works.  For example, the variable @code{CFLAGS} controls the
8322 flags given to the C compiler by the implicit rule for C compilation.
8323 @iftex
8324 @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
8325 @end iftex
8327 You can define your own implicit rules by writing @dfn{pattern rules}.
8328 @iftex
8329 @xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.
8330 @end iftex
8332 @dfn{Suffix rules} are a more limited way to define implicit rules.
8333 Pattern rules are more general and clearer, but suffix rules are
8334 retained for compatibility.
8335 @iftex
8336 @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
8337 @end iftex
8339 @menu
8340 * Using Implicit::              How to use an existing implicit rule
8341                                   to get the recipes for updating a file.
8342 * Catalogue of Rules::          A list of built-in implicit rules.
8343 * Implicit Variables::          How to change what predefined rules do.
8344 * Chained Rules::               How to use a chain of implicit rules.
8345 * Pattern Rules::               How to define new implicit rules.
8346 * Last Resort::                 How to define recipes for rules which
8347                                   cannot find any.
8348 * Suffix Rules::                The old-fashioned style of implicit rule.
8349 * Implicit Rule Search::        The precise algorithm for applying
8350                                   implicit rules.
8351 @end menu
8353 @node Using Implicit, Catalogue of Rules, Implicit Rules, Implicit Rules
8354 @section Using Implicit Rules
8355 @cindex implicit rule, how to use
8356 @cindex rule, implicit, how to use
8358 To allow @code{make} to find a customary method for updating a target
8359 file, all you have to do is refrain from specifying recipes yourself.
8360 Either write a rule with no recipe, or don't write a rule at all.
8361 Then @code{make} will figure out which implicit rule to use based on
8362 which kind of source file exists or can be made.
8364 For example, suppose the makefile looks like this:
8366 @example
8367 foo : foo.o bar.o
8368         cc -o foo foo.o bar.o $(CFLAGS) $(LDFLAGS)
8369 @end example
8371 @noindent
8372 Because you mention @file{foo.o} but do not give a rule for it, @code{make}
8373 will automatically look for an implicit rule that tells how to update it.
8374 This happens whether or not the file @file{foo.o} currently exists.
8376 If an implicit rule is found, it can supply both a recipe and one or
8377 more prerequisites (the source files).  You would want to write a rule
8378 for @file{foo.o} with no recipe if you need to specify additional
8379 prerequisites, such as header files, that the implicit rule cannot
8380 supply.
8382 Each implicit rule has a target pattern and prerequisite patterns.  There may
8383 be many implicit rules with the same target pattern.  For example, numerous
8384 rules make @samp{.o} files: one, from a @samp{.c} file with the C compiler;
8385 another, from a @samp{.p} file with the Pascal compiler; and so on.  The rule
8386 that actually applies is the one whose prerequisites exist or can be made.
8387 So, if you have a file @file{foo.c}, @code{make} will run the C compiler;
8388 otherwise, if you have a file @file{foo.p}, @code{make} will run the Pascal
8389 compiler; and so on.
8391 Of course, when you write the makefile, you know which implicit rule you
8392 want @code{make} to use, and you know it will choose that one because you
8393 know which possible prerequisite files are supposed to exist.
8394 @xref{Catalogue of Rules, ,Catalogue of Implicit Rules},
8395 for a catalogue of all the predefined implicit rules.
8397 Above, we said an implicit rule applies if the required prerequisites ``exist
8398 or can be made''.  A file ``can be made'' if it is mentioned explicitly in
8399 the makefile as a target or a prerequisite, or if an implicit rule can be
8400 recursively found for how to make it.  When an implicit prerequisite is the
8401 result of another implicit rule, we say that @dfn{chaining} is occurring.
8402 @xref{Chained Rules, ,Chains of Implicit Rules}.
8404 In general, @code{make} searches for an implicit rule for each target, and
8405 for each double-colon rule, that has no recipe.  A file that is mentioned
8406 only as a prerequisite is considered a target whose rule specifies nothing,
8407 so implicit rule search happens for it.  @xref{Implicit Rule Search, ,Implicit Rule Search Algorithm}, for the
8408 details of how the search is done.
8410 Note that explicit prerequisites do not influence implicit rule search.
8411 For example, consider this explicit rule:
8413 @example
8414 foo.o: foo.p
8415 @end example
8417 @noindent
8418 The prerequisite on @file{foo.p} does not necessarily mean that
8419 @code{make} will remake @file{foo.o} according to the implicit rule to
8420 make an object file, a @file{.o} file, from a Pascal source file, a
8421 @file{.p} file.  For example, if @file{foo.c} also exists, the implicit
8422 rule to make an object file from a C source file is used instead,
8423 because it appears before the Pascal rule in the list of predefined
8424 implicit rules (@pxref{Catalogue of Rules, , Catalogue of Implicit
8425 Rules}).
8427 If you do not want an implicit rule to be used for a target that has no
8428 recipe, you can give that target an empty recipe by writing a semicolon
8429 (@pxref{Empty Recipes, ,Defining Empty Recipes}).
8431 @node Catalogue of Rules, Implicit Variables, Using Implicit, Implicit Rules
8432 @section Catalogue of Implicit Rules
8433 @cindex implicit rule, predefined
8434 @cindex rule, implicit, predefined
8436 Here is a catalogue of predefined implicit rules which are always
8437 available unless the makefile explicitly overrides or cancels them.
8438 @xref{Canceling Rules, ,Canceling Implicit Rules}, for information on
8439 canceling or overriding an implicit rule.  The @samp{-r} or
8440 @samp{--no-builtin-rules} option cancels all predefined rules.
8442 This manual only documents the default rules available on POSIX-based
8443 operating systems.  Other operating systems, such as VMS, Windows,
8444 OS/2, etc. may have different sets of default rules.  To see the full
8445 list of default rules and variables available in your version of GNU
8446 @code{make}, run @samp{make -p} in a directory with no makefile.
8448 Not all of these rules will always be defined, even when the @samp{-r}
8449 option is not given.  Many of the predefined implicit rules are
8450 implemented in @code{make} as suffix rules, so which ones will be
8451 defined depends on the @dfn{suffix list} (the list of prerequisites of
8452 the special target @code{.SUFFIXES}).  The default suffix list is:
8453 @code{.out}, @code{.a}, @code{.ln}, @code{.o}, @code{.c}, @code{.cc},
8454 @code{.C}, @code{.cpp}, @code{.p}, @code{.f}, @code{.F}, @code{.m},
8455 @code{.r}, @code{.y}, @code{.l}, @code{.ym}, @code{.lm}, @code{.s},
8456 @code{.S}, @code{.mod}, @code{.sym}, @code{.def}, @code{.h},
8457 @code{.info}, @code{.dvi}, @code{.tex}, @code{.texinfo}, @code{.texi},
8458 @code{.txinfo}, @code{.w}, @code{.ch} @code{.web}, @code{.sh},
8459 @code{.elc}, @code{.el}.  All of the implicit rules described below
8460 whose prerequisites have one of these suffixes are actually suffix
8461 rules.  If you modify the suffix list, the only predefined suffix
8462 rules in effect will be those named by one or two of the suffixes that
8463 are on the list you specify; rules whose suffixes fail to be on the
8464 list are disabled.  @xref{Suffix Rules, ,Old-Fashioned Suffix Rules},
8465 for full details on suffix rules.
8467 @table @asis
8468 @item Compiling C programs
8469 @cindex C, rule to compile
8470 @pindex cc
8471 @pindex gcc
8472 @pindex .o
8473 @pindex .c
8474 @file{@var{n}.o} is made automatically from @file{@var{n}.c} with
8475 a recipe of the form @samp{$(CC) -c $(CPPFLAGS) $(CFLAGS)}.@refill
8477 @item Compiling C++ programs
8478 @cindex C++, rule to compile
8479 @pindex g++
8480 @pindex .cc
8481 @pindex .cpp
8482 @pindex .C
8483 @file{@var{n}.o} is made automatically from @file{@var{n}.cc},
8484 @file{@var{n}.cpp}, or @file{@var{n}.C} with a recipe of the form
8485 @samp{$(CXX) -c $(CPPFLAGS) $(CXXFLAGS)}.  We encourage you to use the
8486 suffix @samp{.cc} for C++ source files instead of @samp{.C}.@refill
8488 @item Compiling Pascal programs
8489 @cindex Pascal, rule to compile
8490 @pindex pc
8491 @pindex .p
8492 @file{@var{n}.o} is made automatically from @file{@var{n}.p}
8493 with the recipe @samp{$(PC) -c $(PFLAGS)}.@refill
8495 @item Compiling Fortran and Ratfor programs
8496 @cindex Fortran, rule to compile
8497 @cindex Ratfor, rule to compile
8498 @pindex f77
8499 @pindex .f
8500 @pindex .r
8501 @pindex .F
8502 @file{@var{n}.o} is made automatically from @file{@var{n}.r},
8503 @file{@var{n}.F} or @file{@var{n}.f} by running the
8504 Fortran compiler.  The precise recipe used is as follows:@refill
8506 @table @samp
8507 @item .f
8508 @samp{$(FC) -c $(FFLAGS)}.
8509 @item .F
8510 @samp{$(FC) -c $(FFLAGS) $(CPPFLAGS)}.
8511 @item .r
8512 @samp{$(FC) -c $(FFLAGS) $(RFLAGS)}.
8513 @end table
8515 @item Preprocessing Fortran and Ratfor programs
8516 @file{@var{n}.f} is made automatically from @file{@var{n}.r} or
8517 @file{@var{n}.F}.  This rule runs just the preprocessor to convert a
8518 Ratfor or preprocessable Fortran program into a strict Fortran
8519 program.  The precise recipe used is as follows:@refill
8521 @table @samp
8522 @item .F
8523 @samp{$(FC) -F $(CPPFLAGS) $(FFLAGS)}.
8524 @item .r
8525 @samp{$(FC) -F $(FFLAGS) $(RFLAGS)}.
8526 @end table
8528 @item Compiling Modula-2 programs
8529 @cindex Modula-2, rule to compile
8530 @pindex m2c
8531 @pindex .sym
8532 @pindex .def
8533 @pindex .mod
8534 @file{@var{n}.sym} is made from @file{@var{n}.def} with a recipe
8535 of the form @samp{$(M2C) $(M2FLAGS) $(DEFFLAGS)}.  @file{@var{n}.o}
8536 is made from @file{@var{n}.mod}; the form is:
8537 @w{@samp{$(M2C) $(M2FLAGS) $(MODFLAGS)}}.@refill
8539 @need 1200
8540 @item Assembling and preprocessing assembler programs
8541 @cindex assembly, rule to compile
8542 @pindex as
8543 @pindex .s
8544 @file{@var{n}.o} is made automatically from @file{@var{n}.s} by
8545 running the assembler, @code{as}.  The precise recipe is
8546 @samp{$(AS) $(ASFLAGS)}.@refill
8548 @pindex .S
8549 @file{@var{n}.s} is made automatically from @file{@var{n}.S} by
8550 running the C preprocessor, @code{cpp}.  The precise recipe is
8551 @w{@samp{$(CPP) $(CPPFLAGS)}}.
8553 @item Linking a single object file
8554 @cindex linking, predefined rule for
8555 @pindex ld
8556 @pindex .o
8557 @file{@var{n}} is made automatically from @file{@var{n}.o} by running
8558 the linker (usually called @code{ld}) via the C compiler.  The precise
8559 recipe used is @w{@samp{$(CC) $(LDFLAGS) @var{n}.o $(LOADLIBES) $(LDLIBS)}}.
8561 This rule does the right thing for a simple program with only one
8562 source file.  It will also do the right thing if there are multiple
8563 object files (presumably coming from various other source files), one
8564 of which has a name matching that of the executable file.  Thus,
8566 @example
8567 x: y.o z.o
8568 @end example
8570 @noindent
8571 when @file{x.c}, @file{y.c} and @file{z.c} all exist will execute:
8573 @example
8574 @group
8575 cc -c x.c -o x.o
8576 cc -c y.c -o y.o
8577 cc -c z.c -o z.o
8578 cc x.o y.o z.o -o x
8579 rm -f x.o
8580 rm -f y.o
8581 rm -f z.o
8582 @end group
8583 @end example
8585 @noindent
8586 In more complicated cases, such as when there is no object file whose
8587 name derives from the executable file name, you must write an explicit
8588 recipe for linking.
8590 Each kind of file automatically made into @samp{.o} object files will
8591 be automatically linked by using the compiler (@samp{$(CC)},
8592 @samp{$(FC)} or @samp{$(PC)}; the C compiler @samp{$(CC)} is used to
8593 assemble @samp{.s} files) without the @samp{-c} option.  This could be
8594 done by using the @samp{.o} object files as intermediates, but it is
8595 faster to do the compiling and linking in one step, so that's how it's
8596 done.@refill
8598 @item Yacc for C programs
8599 @pindex yacc
8600 @cindex Yacc, rule to run
8601 @pindex .y
8602 @file{@var{n}.c} is made automatically from @file{@var{n}.y} by
8603 running Yacc with the recipe @samp{$(YACC) $(YFLAGS)}.
8605 @item Lex for C programs
8606 @pindex lex
8607 @cindex Lex, rule to run
8608 @pindex .l
8609 @file{@var{n}.c} is made automatically from @file{@var{n}.l} by
8610 running Lex.  The actual recipe is @samp{$(LEX) $(LFLAGS)}.
8612 @item Lex for Ratfor programs
8613 @file{@var{n}.r} is made automatically from @file{@var{n}.l} by
8614 running Lex.  The actual recipe is @samp{$(LEX) $(LFLAGS)}.
8616 The convention of using the same suffix @samp{.l} for all Lex files
8617 regardless of whether they produce C code or Ratfor code makes it
8618 impossible for @code{make} to determine automatically which of the two
8619 languages you are using in any particular case.  If @code{make} is
8620 called upon to remake an object file from a @samp{.l} file, it must
8621 guess which compiler to use.  It will guess the C compiler, because
8622 that is more common.  If you are using Ratfor, make sure @code{make}
8623 knows this by mentioning @file{@var{n}.r} in the makefile.  Or, if you
8624 are using Ratfor exclusively, with no C files, remove @samp{.c} from
8625 the list of implicit rule suffixes with:@refill
8627 @example
8628 @group
8629 .SUFFIXES:
8630 .SUFFIXES: .o .r .f .l @dots{}
8631 @end group
8632 @end example
8634 @item Making Lint Libraries from C, Yacc, or Lex programs
8635 @pindex lint
8636 @cindex @code{lint}, rule to run
8637 @pindex .ln
8638 @file{@var{n}.ln} is made from @file{@var{n}.c} by running @code{lint}.
8639 The precise recipe is @w{@samp{$(LINT) $(LINTFLAGS) $(CPPFLAGS) -i}}.
8640 The same recipe is used on the C code produced from
8641 @file{@var{n}.y} or @file{@var{n}.l}.@refill
8643 @item @TeX{} and Web
8644 @cindex @TeX{}, rule to run
8645 @cindex Web, rule to run
8646 @pindex tex
8647 @pindex cweave
8648 @pindex weave
8649 @pindex tangle
8650 @pindex ctangle
8651 @pindex .dvi
8652 @pindex .tex
8653 @pindex .web
8654 @pindex .w
8655 @pindex .ch
8656 @file{@var{n}.dvi} is made from @file{@var{n}.tex} with the recipe
8657 @samp{$(TEX)}.  @file{@var{n}.tex} is made from @file{@var{n}.web} with
8658 @samp{$(WEAVE)}, or from @file{@var{n}.w} (and from @file{@var{n}.ch} if
8659 it exists or can be made) with @samp{$(CWEAVE)}.  @file{@var{n}.p} is
8660 made from @file{@var{n}.web} with @samp{$(TANGLE)} and @file{@var{n}.c}
8661 is made from @file{@var{n}.w} (and from @file{@var{n}.ch} if it exists
8662 or can be made) with @samp{$(CTANGLE)}.@refill
8664 @item Texinfo and Info
8665 @cindex Texinfo, rule to format
8666 @cindex Info, rule to format
8667 @pindex texi2dvi
8668 @pindex makeinfo
8669 @pindex .texinfo
8670 @pindex .info
8671 @pindex .texi
8672 @pindex .txinfo
8673 @file{@var{n}.dvi} is made from @file{@var{n}.texinfo},
8674 @file{@var{n}.texi}, or @file{@var{n}.txinfo}, with the recipe
8675 @w{@samp{$(TEXI2DVI) $(TEXI2DVI_FLAGS)}}.  @file{@var{n}.info} is made from
8676 @file{@var{n}.texinfo}, @file{@var{n}.texi}, or @file{@var{n}.txinfo}, with
8677 the recipe @w{@samp{$(MAKEINFO) $(MAKEINFO_FLAGS)}}.
8679 @item RCS
8680 @cindex RCS, rule to extract from
8681 @pindex co
8682 @pindex ,v @r{(RCS file extension)}
8683 Any file @file{@var{n}} is extracted if necessary from an RCS file
8684 named either @file{@var{n},v} or @file{RCS/@var{n},v}.  The precise
8685 recipe used is @w{@samp{$(CO) $(COFLAGS)}}.  @file{@var{n}} will not be
8686 extracted from RCS if it already exists, even if the RCS file is
8687 newer.  The rules for RCS are terminal
8688 (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}),
8689 so RCS files cannot be generated from another source; they must
8690 actually exist.@refill
8692 @item SCCS
8693 @cindex SCCS, rule to extract from
8694 @pindex get
8695 @pindex s. @r{(SCCS file prefix)}
8696 Any file @file{@var{n}} is extracted if necessary from an SCCS file
8697 named either @file{s.@var{n}} or @file{SCCS/s.@var{n}}.  The precise
8698 recipe used is @w{@samp{$(GET) $(GFLAGS)}}.  The rules for SCCS are
8699 terminal (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}),
8700 so SCCS files cannot be generated from another source; they must
8701 actually exist.@refill
8703 @pindex .sh
8704 For the benefit of SCCS, a file @file{@var{n}} is copied from
8705 @file{@var{n}.sh} and made executable (by everyone).  This is for
8706 shell scripts that are checked into SCCS.  Since RCS preserves the
8707 execution permission of a file, you do not need to use this feature
8708 with RCS.@refill
8710 We recommend that you avoid using of SCCS.  RCS is widely held to be
8711 superior, and is also free.  By choosing free software in place of
8712 comparable (or inferior) proprietary software, you support the free
8713 software movement.
8714 @end table
8716 Usually, you want to change only the variables listed in the table
8717 above, which are documented in the following section.
8719 However, the recipes in built-in implicit rules actually use
8720 variables such as @code{COMPILE.c}, @code{LINK.p}, and
8721 @code{PREPROCESS.S}, whose values contain the recipes listed above.
8723 @code{make} follows the convention that the rule to compile a
8724 @file{.@var{x}} source file uses the variable @code{COMPILE.@var{x}}.
8725 Similarly, the rule to produce an executable from a @file{.@var{x}}
8726 file uses @code{LINK.@var{x}}; and the rule to preprocess a
8727 @file{.@var{x}} file uses @code{PREPROCESS.@var{x}}.
8729 @vindex OUTPUT_OPTION
8730 Every rule that produces an object file uses the variable
8731 @code{OUTPUT_OPTION}.  @code{make} defines this variable either to
8732 contain @samp{-o $@@}, or to be empty, depending on a compile-time
8733 option.  You need the @samp{-o} option to ensure that the output goes
8734 into the right file when the source file is in a different directory,
8735 as when using @code{VPATH} (@pxref{Directory Search}).  However,
8736 compilers on some systems do not accept a @samp{-o} switch for object
8737 files.  If you use such a system, and use @code{VPATH}, some
8738 compilations will put their output in the wrong place.
8739 A possible workaround for this problem is to give @code{OUTPUT_OPTION}
8740 the value @w{@samp{; mv $*.o $@@}}.
8742 @node Implicit Variables, Chained Rules, Catalogue of Rules, Implicit Rules
8743 @section Variables Used by Implicit Rules
8744 @cindex flags for compilers
8746 The recipes in built-in implicit rules make liberal use of certain
8747 predefined variables.  You can alter the values of these variables in
8748 the makefile, with arguments to @code{make}, or in the environment to
8749 alter how the implicit rules work without redefining the rules
8750 themselves.  You can cancel all variables used by implicit rules with
8751 the @samp{-R} or @samp{--no-builtin-variables} option.
8753 For example, the recipe used to compile a C source file actually says
8754 @samp{$(CC) -c $(CFLAGS) $(CPPFLAGS)}.  The default values of the variables
8755 used are @samp{cc} and nothing, resulting in the command @samp{cc -c}.  By
8756 redefining @samp{CC} to @samp{ncc}, you could cause @samp{ncc} to be
8757 used for all C compilations performed by the implicit rule.  By redefining
8758 @samp{CFLAGS} to be @samp{-g}, you could pass the @samp{-g} option to
8759 each compilation.  @emph{All} implicit rules that do C compilation use
8760 @samp{$(CC)} to get the program name for the compiler and @emph{all}
8761 include @samp{$(CFLAGS)} among the arguments given to the compiler.@refill
8763 The variables used in implicit rules fall into two classes: those that are
8764 names of programs (like @code{CC}) and those that contain arguments for the
8765 programs (like @code{CFLAGS}).  (The ``name of a program'' may also contain
8766 some command arguments, but it must start with an actual executable program
8767 name.)  If a variable value contains more than one argument, separate them
8768 with spaces.
8770 The following tables describe of some of the more commonly-used predefined
8771 variables.  This list is not exhaustive, and the default values shown here may
8772 not be what @code{make} selects for your environment.  To see the
8773 complete list of predefined variables for your instance of GNU @code{make} you
8774 can run @samp{make -p} in a directory with no makefiles.
8776 Here is a table of some of the more common variables used as names of
8777 programs in built-in rules:
8778 makefiles.
8780 @table @code
8781 @item AR
8782 @vindex AR
8783 Archive-maintaining program; default @samp{ar}.
8784 @pindex ar
8786 @item AS
8787 @vindex AS
8788 Program for compiling assembly files; default @samp{as}.
8789 @pindex as
8791 @item CC
8792 @vindex CC
8793 Program for compiling C programs; default @samp{cc}.
8794 @pindex cc
8796 @item CXX
8797 @vindex CXX
8798 Program for compiling C++ programs; default @samp{g++}.
8799 @pindex g++
8801 @item CPP
8802 @vindex CPP
8803 Program for running the C preprocessor, with results to standard output;
8804 default @samp{$(CC) -E}.
8806 @item FC
8807 @vindex FC
8808 Program for compiling or preprocessing Fortran and Ratfor programs;
8809 default @samp{f77}.
8810 @pindex f77
8812 @item M2C
8813 @vindex M2C
8814 Program to use to compile Modula-2 source code; default @samp{m2c}.
8815 @pindex m2c
8817 @item PC
8818 @vindex PC
8819 Program for compiling Pascal programs; default @samp{pc}.
8820 @pindex pc
8822 @item CO
8823 @vindex CO
8824 Program for extracting a file from RCS; default @samp{co}.
8825 @pindex co
8827 @item GET
8828 @vindex GET
8829 Program for extracting a file from SCCS; default @samp{get}.
8830 @pindex get
8832 @item LEX
8833 @vindex LEX
8834 Program to use to turn Lex grammars into source code; default @samp{lex}.
8835 @pindex lex
8837 @item YACC
8838 @vindex YACC
8839 Program to use to turn Yacc grammars into source code; default @samp{yacc}.
8840 @pindex yacc
8842 @item LINT
8843 @vindex LINT
8844 Program to use to run lint on source code; default @samp{lint}.
8845 @pindex lint
8847 @item MAKEINFO
8848 @vindex MAKEINFO
8849 Program to convert a Texinfo source file into an Info file; default
8850 @samp{makeinfo}.
8851 @pindex makeinfo
8853 @item TEX
8854 @vindex TEX
8855 Program to make @TeX{} @sc{dvi} files from @TeX{} source;
8856 default @samp{tex}.
8857 @pindex tex
8859 @item TEXI2DVI
8860 @vindex TEXI2DVI
8861 Program to make @TeX{} @sc{dvi} files from Texinfo source;
8862 default @samp{texi2dvi}.
8863 @pindex texi2dvi
8865 @item WEAVE
8866 @vindex WEAVE
8867 Program to translate Web into @TeX{}; default @samp{weave}.
8868 @pindex weave
8870 @item CWEAVE
8871 @vindex CWEAVE
8872 Program to translate C Web into @TeX{}; default @samp{cweave}.
8873 @pindex cweave
8875 @item TANGLE
8876 @vindex TANGLE
8877 Program to translate Web into Pascal; default @samp{tangle}.
8878 @pindex tangle
8880 @item CTANGLE
8881 @vindex CTANGLE
8882 Program to translate C Web into C; default @samp{ctangle}.
8883 @pindex ctangle
8885 @item RM
8886 @vindex RM
8887 Command to remove a file; default @samp{rm -f}.
8888 @pindex rm
8889 @end table
8891 Here is a table of variables whose values are additional arguments for the
8892 programs above.  The default values for all of these is the empty
8893 string, unless otherwise noted.
8895 @table @code
8896 @item ARFLAGS
8897 @vindex ARFLAGS
8898 Flags to give the archive-maintaining program; default @samp{rv}.
8900 @item ASFLAGS
8901 @vindex ASFLAGS
8902 Extra flags to give to the assembler (when explicitly
8903 invoked on a @samp{.s} or @samp{.S} file).
8905 @item CFLAGS
8906 @vindex CFLAGS
8907 Extra flags to give to the C compiler.
8909 @item CXXFLAGS
8910 @vindex CXXFLAGS
8911 Extra flags to give to the C++ compiler.
8913 @item COFLAGS
8914 @vindex COFLAGS
8915 Extra flags to give to the RCS @code{co} program.
8917 @item CPPFLAGS
8918 @vindex CPPFLAGS
8919 Extra flags to give to the C preprocessor and programs
8920 that use it (the C and Fortran compilers).
8922 @item FFLAGS
8923 @vindex FFLAGS
8924 Extra flags to give to the Fortran compiler.
8926 @item GFLAGS
8927 @vindex GFLAGS
8928 Extra flags to give to the SCCS @code{get} program.
8930 @item LDFLAGS
8931 @vindex LDFLAGS
8932 Extra flags to give to compilers when they are supposed to invoke the linker,
8933 @samp{ld}.
8935 @item LFLAGS
8936 @vindex LFLAGS
8937 Extra flags to give to Lex.
8939 @item YFLAGS
8940 @vindex YFLAGS
8941 Extra flags to give to Yacc.
8943 @item PFLAGS
8944 @vindex PFLAGS
8945 Extra flags to give to the Pascal compiler.
8947 @item RFLAGS
8948 @vindex RFLAGS
8949 Extra flags to give to the Fortran compiler for Ratfor programs.
8951 @item LINTFLAGS
8952 @vindex LINTFLAGS
8953 Extra flags to give to lint.
8954 @end table
8956 @node Chained Rules, Pattern Rules, Implicit Variables, Implicit Rules
8957 @section Chains of Implicit Rules
8959 @cindex chains of rules
8960 @cindex rule, implicit, chains of
8961 Sometimes a file can be made by a sequence of implicit rules.  For example,
8962 a file @file{@var{n}.o} could be made from @file{@var{n}.y} by running
8963 first Yacc and then @code{cc}.  Such a sequence is called a @dfn{chain}.
8965 If the file @file{@var{n}.c} exists, or is mentioned in the makefile, no
8966 special searching is required: @code{make} finds that the object file can
8967 be made by C compilation from @file{@var{n}.c}; later on, when considering
8968 how to make @file{@var{n}.c}, the rule for running Yacc is
8969 used.  Ultimately both @file{@var{n}.c} and @file{@var{n}.o} are
8970 updated.@refill
8972 @cindex intermediate files
8973 @cindex files, intermediate
8974 However, even if @file{@var{n}.c} does not exist and is not mentioned,
8975 @code{make} knows how to envision it as the missing link between
8976 @file{@var{n}.o} and @file{@var{n}.y}!  In this case, @file{@var{n}.c} is
8977 called an @dfn{intermediate file}.  Once @code{make} has decided to use the
8978 intermediate file, it is entered in the data base as if it had been
8979 mentioned in the makefile, along with the implicit rule that says how to
8980 create it.@refill
8982 Intermediate files are remade using their rules just like all other
8983 files.  But intermediate files are treated differently in two ways.
8985 The first difference is what happens if the intermediate file does not
8986 exist.  If an ordinary file @var{b} does not exist, and @code{make}
8987 considers a target that depends on @var{b}, it invariably creates
8988 @var{b} and then updates the target from @var{b}.  But if @var{b} is an
8989 intermediate file, then @code{make} can leave well enough alone.  It
8990 won't bother updating @var{b}, or the ultimate target, unless some
8991 prerequisite of @var{b} is newer than that target or there is some other
8992 reason to update that target.
8994 The second difference is that if @code{make} @emph{does} create @var{b}
8995 in order to update something else, it deletes @var{b} later on after it
8996 is no longer needed.  Therefore, an intermediate file which did not
8997 exist before @code{make} also does not exist after @code{make}.
8998 @code{make} reports the deletion to you by printing a @samp{rm -f}
8999 command showing which file it is deleting.
9001 Ordinarily, a file cannot be intermediate if it is mentioned in the
9002 makefile as a target or prerequisite.  However, you can explicitly mark a
9003 file as intermediate by listing it as a prerequisite of the special target
9004 @code{.INTERMEDIATE}.  This takes effect even if the file is mentioned
9005 explicitly in some other way.
9007 @cindex intermediate files, preserving
9008 @cindex preserving intermediate files
9009 @cindex secondary files
9010 You can prevent automatic deletion of an intermediate file by marking it
9011 as a @dfn{secondary} file.  To do this, list it as a prerequisite of the
9012 special target @code{.SECONDARY}.  When a file is secondary, @code{make}
9013 will not create the file merely because it does not already exist, but
9014 @code{make} does not automatically delete the file.  Marking a file as
9015 secondary also marks it as intermediate.
9017 You can list the target pattern of an implicit rule (such as @samp{%.o})
9018 as a prerequisite of the special target @code{.PRECIOUS} to preserve
9019 intermediate files made by implicit rules whose target patterns match
9020 that file's name; see @ref{Interrupts}.@refill
9021 @cindex preserving with @code{.PRECIOUS}
9022 @cindex @code{.PRECIOUS} intermediate files
9024 A chain can involve more than two implicit rules.  For example, it is
9025 possible to make a file @file{foo} from @file{RCS/foo.y,v} by running RCS,
9026 Yacc and @code{cc}.  Then both @file{foo.y} and @file{foo.c} are
9027 intermediate files that are deleted at the end.@refill
9029 No single implicit rule can appear more than once in a chain.  This means
9030 that @code{make} will not even consider such a ridiculous thing as making
9031 @file{foo} from @file{foo.o.o} by running the linker twice.  This
9032 constraint has the added benefit of preventing any infinite loop in the
9033 search for an implicit rule chain.
9035 There are some special implicit rules to optimize certain cases that would
9036 otherwise be handled by rule chains.  For example, making @file{foo} from
9037 @file{foo.c} could be handled by compiling and linking with separate
9038 chained rules, using @file{foo.o} as an intermediate file.  But what
9039 actually happens is that a special rule for this case does the compilation
9040 and linking with a single @code{cc} command.  The optimized rule is used in
9041 preference to the step-by-step chain because it comes earlier in the
9042 ordering of rules.
9044 @node Pattern Rules, Last Resort, Chained Rules, Implicit Rules
9045 @section Defining and Redefining Pattern Rules
9047 You define an implicit rule by writing a @dfn{pattern rule}.  A pattern
9048 rule looks like an ordinary rule, except that its target contains the
9049 character @samp{%} (exactly one of them).  The target is considered a
9050 pattern for matching file names; the @samp{%} can match any nonempty
9051 substring, while other characters match only themselves.  The prerequisites
9052 likewise use @samp{%} to show how their names relate to the target name.
9054 Thus, a pattern rule @samp{%.o : %.c} says how to make any file
9055 @file{@var{stem}.o} from another file @file{@var{stem}.c}.@refill
9057 Note that expansion using @samp{%} in pattern rules occurs
9058 @strong{after} any variable or function expansions, which take place
9059 when the makefile is read.  @xref{Using Variables, , How to Use
9060 Variables}, and @ref{Functions, ,Functions for Transforming Text}.
9062 @menu
9063 * Pattern Intro::               An introduction to pattern rules.
9064 * Pattern Examples::            Examples of pattern rules.
9065 * Automatic Variables::         How to use automatic variables in the
9066                                   recipes of implicit rules.
9067 * Pattern Match::               How patterns match.
9068 * Match-Anything Rules::        Precautions you should take prior to
9069                                   defining rules that can match any
9070                                   target file whatever.
9071 * Canceling Rules::             How to override or cancel built-in rules.
9072 @end menu
9074 @node Pattern Intro, Pattern Examples, Pattern Rules, Pattern Rules
9075 @subsection Introduction to Pattern Rules
9076 @cindex pattern rule
9077 @cindex rule, pattern
9079 A pattern rule contains the character @samp{%} (exactly one of them)
9080 in the target; otherwise, it looks exactly like an ordinary rule.  The
9081 target is a pattern for matching file names; the @samp{%} matches any
9082 nonempty substring, while other characters match only themselves.
9083 @cindex target pattern, implicit
9084 @cindex @code{%}, in pattern rules
9086 For example, @samp{%.c} as a pattern matches any file name that ends in
9087 @samp{.c}.  @samp{s.%.c} as a pattern matches any file name that starts
9088 with @samp{s.}, ends in @samp{.c} and is at least five characters long.
9089 (There must be at least one character to match the @samp{%}.)  The substring
9090 that the @samp{%} matches is called the @dfn{stem}.@refill
9092 @samp{%} in a prerequisite of a pattern rule stands for the same stem
9093 that was matched by the @samp{%} in the target.  In order for the
9094 pattern rule to apply, its target pattern must match the file name
9095 under consideration and all of its prerequisites (after pattern
9096 substitution) must name files that exist or can be made.  These files
9097 become prerequisites of the target.
9098 @cindex prerequisite pattern, implicit
9100 Thus, a rule of the form
9102 @example
9103 %.o : %.c ; @var{recipe}@dots{}
9104 @end example
9106 @noindent
9107 specifies how to make a file @file{@var{n}.o}, with another file
9108 @file{@var{n}.c} as its prerequisite, provided that @file{@var{n}.c}
9109 exists or can be made.
9111 There may also be prerequisites that do not use @samp{%}; such a prerequisite
9112 attaches to every file made by this pattern rule.  These unvarying
9113 prerequisites are useful occasionally.
9115 A pattern rule need not have any prerequisites that contain @samp{%}, or
9116 in fact any prerequisites at all.  Such a rule is effectively a general
9117 wildcard.  It provides a way to make any file that matches the target
9118 pattern.  @xref{Last Resort}.
9120 @c !!! The end of of this paragraph should be rewritten.  --bob
9121 Pattern rules may have more than one target.  Unlike normal rules,
9122 this does not act as many different rules with the same prerequisites
9123 and recipe.  If a pattern rule has multiple targets, @code{make} knows
9124 that the rule's recipe is responsible for making all of the targets.
9125 The recipe is executed only once to make all the targets.  When
9126 searching for a pattern rule to match a target, the target patterns of
9127 a rule other than the one that matches the target in need of a rule
9128 are incidental: @code{make} worries only about giving a recipe and
9129 prerequisites to the file presently in question.  However, when this
9130 file's recipe is run, the other targets are marked as having been
9131 updated themselves.
9132 @cindex multiple targets, in pattern rule
9133 @cindex target, multiple in pattern rule
9135 The order in which pattern rules appear in the makefile is important
9136 since this is the order in which they are considered.
9137 Of equally applicable
9138 rules, only the first one found is used.  The rules you write take precedence
9139 over those that are built in.  Note however, that a rule whose
9140 prerequisites actually exist or are mentioned always takes priority over a
9141 rule with prerequisites that must be made by chaining other implicit rules.
9142 @cindex pattern rules, order of
9143 @cindex order of pattern rules
9145 @node Pattern Examples, Automatic Variables, Pattern Intro, Pattern Rules
9146 @subsection Pattern Rule Examples
9148 Here are some examples of pattern rules actually predefined in
9149 @code{make}.  First, the rule that compiles @samp{.c} files into @samp{.o}
9150 files:@refill
9152 @example
9153 %.o : %.c
9154         $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@@
9155 @end example
9157 @noindent
9158 defines a rule that can make any file @file{@var{x}.o} from
9159 @file{@var{x}.c}.  The recipe uses the automatic variables @samp{$@@} and
9160 @samp{$<} to substitute the names of the target file and the source file
9161 in each case where the rule applies (@pxref{Automatic Variables}).@refill
9163 Here is a second built-in rule:
9165 @example
9166 % :: RCS/%,v
9167         $(CO) $(COFLAGS) $<
9168 @end example
9170 @noindent
9171 defines a rule that can make any file @file{@var{x}} whatsoever from a
9172 corresponding file @file{@var{x},v} in the subdirectory @file{RCS}.  Since
9173 the target is @samp{%}, this rule will apply to any file whatever, provided
9174 the appropriate prerequisite file exists.  The double colon makes the rule
9175 @dfn{terminal}, which means that its prerequisite may not be an intermediate
9176 file (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}).@refill
9178 @need 500
9179 This pattern rule has two targets:
9181 @example
9182 @group
9183 %.tab.c %.tab.h: %.y
9184         bison -d $<
9185 @end group
9186 @end example
9188 @noindent
9189 @c The following paragraph is rewritten to avoid overfull hboxes
9190 This tells @code{make} that the recipe @samp{bison -d @var{x}.y} will
9191 make both @file{@var{x}.tab.c} and @file{@var{x}.tab.h}.  If the file
9192 @file{foo} depends on the files @file{parse.tab.o} and @file{scan.o}
9193 and the file @file{scan.o} depends on the file @file{parse.tab.h},
9194 when @file{parse.y} is changed, the recipe @samp{bison -d parse.y}
9195 will be executed only once, and the prerequisites of both
9196 @file{parse.tab.o} and @file{scan.o} will be satisfied.  (Presumably
9197 the file @file{parse.tab.o} will be recompiled from @file{parse.tab.c}
9198 and the file @file{scan.o} from @file{scan.c}, while @file{foo} is
9199 linked from @file{parse.tab.o}, @file{scan.o}, and its other
9200 prerequisites, and it will execute happily ever after.)@refill
9202 @node Automatic Variables, Pattern Match, Pattern Examples, Pattern Rules
9203 @subsection Automatic Variables
9204 @cindex automatic variables
9205 @cindex variables, automatic
9206 @cindex variables, and implicit rule
9208 Suppose you are writing a pattern rule to compile a @samp{.c} file into a
9209 @samp{.o} file: how do you write the @samp{cc} command so that it operates
9210 on the right source file name?  You cannot write the name in the recipe,
9211 because the name is different each time the implicit rule is applied.
9213 What you do is use a special feature of @code{make}, the @dfn{automatic
9214 variables}.  These variables have values computed afresh for each rule that
9215 is executed, based on the target and prerequisites of the rule.  In this
9216 example, you would use @samp{$@@} for the object file name and @samp{$<}
9217 for the source file name.
9219 @cindex automatic variables in prerequisites
9220 @cindex prerequisites, and automatic variables
9221 It's very important that you recognize the limited scope in which
9222 automatic variable values are available: they only have values within
9223 the recipe.  In particular, you cannot use them anywhere
9224 within the target list of a rule; they have no value there and will
9225 expand to the empty string.  Also, they cannot be accessed directly
9226 within the prerequisite list of a rule.  A common mistake is
9227 attempting to use @code{$@@} within the prerequisites list; this will
9228 not work.  However, there is a special feature of GNU @code{make},
9229 secondary expansion (@pxref{Secondary Expansion}), which will allow
9230 automatic variable values to be used in prerequisite lists.
9232 Here is a table of automatic variables:
9234 @table @code
9235 @vindex $@@
9236 @vindex @@ @r{(automatic variable)}
9237 @item $@@
9238 The file name of the target of the rule.  If the target is an archive
9239 member, then @samp{$@@} is the name of the archive file.  In a pattern
9240 rule that has multiple targets (@pxref{Pattern Intro, ,Introduction to
9241 Pattern Rules}), @samp{$@@} is the name of whichever target caused the
9242 rule's recipe to be run.
9244 @vindex $%
9245 @vindex % @r{(automatic variable)}
9246 @item $%
9247 The target member name, when the target is an archive member.
9248 @xref{Archives}.  For example, if the target is @file{foo.a(bar.o)} then
9249 @samp{$%} is @file{bar.o} and @samp{$@@} is @file{foo.a}.  @samp{$%} is
9250 empty when the target is not an archive member.
9252 @vindex $<
9253 @vindex < @r{(automatic variable)}
9254 @item $<
9255 The name of the first prerequisite.  If the target got its recipe from
9256 an implicit rule, this will be the first prerequisite added by the
9257 implicit rule (@pxref{Implicit Rules}).
9259 @vindex $?
9260 @vindex ? @r{(automatic variable)}
9261 @item $?
9262 The names of all the prerequisites that are newer than the target, with
9263 spaces between them.  For prerequisites which are archive members, only
9264 the named member is used (@pxref{Archives}).
9265 @cindex prerequisites, list of changed
9266 @cindex list of changed prerequisites
9268 @vindex $^
9269 @vindex ^ @r{(automatic variable)}
9270 @item $^
9271 The names of all the prerequisites, with spaces between them.  For
9272 prerequisites which are archive members, only the named member is used
9273 (@pxref{Archives}).  A target has only one prerequisite on each other file
9274 it depends on, no matter how many times each file is listed as a
9275 prerequisite.  So if you list a prerequisite more than once for a target,
9276 the value of @code{$^} contains just one copy of the name.  This list
9277 does @strong{not} contain any of the order-only prerequisites; for those
9278 see the @samp{$|} variable, below.
9279 @cindex prerequisites, list of all
9280 @cindex list of all prerequisites
9282 @vindex $+
9283 @vindex + @r{(automatic variable)}
9284 @item $+
9285 This is like @samp{$^}, but prerequisites listed more than once are
9286 duplicated in the order they were listed in the makefile.  This is
9287 primarily useful for use in linking commands where it is meaningful to
9288 repeat library file names in a particular order.
9290 @vindex $|
9291 @vindex | @r{(automatic variable)}
9292 @item $|
9293 The names of all the order-only prerequisites, with spaces between
9294 them.
9296 @vindex $*
9297 @vindex * @r{(automatic variable)}
9298 @item $*
9299 The stem with which an implicit rule matches (@pxref{Pattern Match, ,How
9300 Patterns Match}).  If the target is @file{dir/a.foo.b} and the target
9301 pattern is @file{a.%.b} then the stem is @file{dir/foo}.  The stem is
9302 useful for constructing names of related files.@refill
9303 @cindex stem, variable for
9305 In a static pattern rule, the stem is part of the file name that matched
9306 the @samp{%} in the target pattern.
9308 In an explicit rule, there is no stem; so @samp{$*} cannot be determined
9309 in that way.  Instead, if the target name ends with a recognized suffix
9310 (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}), @samp{$*} is set to
9311 the target name minus the suffix.  For example, if the target name is
9312 @samp{foo.c}, then @samp{$*} is set to @samp{foo}, since @samp{.c} is a
9313 suffix.  GNU @code{make} does this bizarre thing only for compatibility
9314 with other implementations of @code{make}.  You should generally avoid
9315 using @samp{$*} except in implicit rules or static pattern rules.@refill
9317 If the target name in an explicit rule does not end with a recognized
9318 suffix, @samp{$*} is set to the empty string for that rule.
9319 @end table
9321 @samp{$?} is useful even in explicit rules when you wish to operate on only
9322 the prerequisites that have changed.  For example, suppose that an archive
9323 named @file{lib} is supposed to contain copies of several object files.
9324 This rule copies just the changed object files into the archive:
9326 @example
9327 @group
9328 lib: foo.o bar.o lose.o win.o
9329         ar r lib $?
9330 @end group
9331 @end example
9333 Of the variables listed above, four have values that are single file
9334 names, and three have values that are lists of file names.  These seven
9335 have variants that get just the file's directory name or just the file
9336 name within the directory.  The variant variables' names are formed by
9337 appending @samp{D} or @samp{F}, respectively.  These variants are
9338 semi-obsolete in GNU @code{make} since the functions @code{dir} and
9339 @code{notdir} can be used to get a similar effect (@pxref{File Name
9340 Functions, , Functions for File Names}).  Note, however, that the
9341 @samp{D} variants all omit the trailing slash which always appears in
9342 the output of the @code{dir} function.  Here is a table of the variants:
9344 @table @samp
9345 @vindex $(@@D)
9346 @vindex @@D @r{(automatic variable)}
9347 @item $(@@D)
9348 The directory part of the file name of the target, with the trailing
9349 slash removed.  If the value of @samp{$@@} is @file{dir/foo.o} then
9350 @samp{$(@@D)} is @file{dir}.  This value is @file{.} if @samp{$@@} does
9351 not contain a slash.
9353 @vindex $(@@F)
9354 @vindex @@F @r{(automatic variable)}
9355 @item $(@@F)
9356 The file-within-directory part of the file name of the target.  If the
9357 value of @samp{$@@} is @file{dir/foo.o} then @samp{$(@@F)} is
9358 @file{foo.o}.  @samp{$(@@F)} is equivalent to @samp{$(notdir $@@)}.
9360 @vindex $(*D)
9361 @vindex *D @r{(automatic variable)}
9362 @item $(*D)
9363 @vindex $(*F)
9364 @vindex *F @r{(automatic variable)}
9365 @itemx $(*F)
9366 The directory part and the file-within-directory
9367 part of the stem; @file{dir} and @file{foo} in this example.
9369 @vindex $(%D)
9370 @vindex %D @r{(automatic variable)}
9371 @item $(%D)
9372 @vindex $(%F)
9373 @vindex %F @r{(automatic variable)}
9374 @itemx $(%F)
9375 The directory part and the file-within-directory part of the target
9376 archive member name.  This makes sense only for archive member targets
9377 of the form @file{@var{archive}(@var{member})} and is useful only when
9378 @var{member} may contain a directory name.  (@xref{Archive Members,
9379 ,Archive Members as Targets}.)
9381 @vindex $(<D)
9382 @vindex <D @r{(automatic variable)}
9383 @item $(<D)
9384 @vindex $(<F)
9385 @vindex <F @r{(automatic variable)}
9386 @itemx $(<F)
9387 The directory part and the file-within-directory
9388 part of the first prerequisite.
9390 @vindex $(^D)
9391 @vindex ^D @r{(automatic variable)}
9392 @item $(^D)
9393 @vindex $(^F)
9394 @vindex ^F @r{(automatic variable)}
9395 @itemx $(^F)
9396 Lists of the directory parts and the file-within-directory
9397 parts of all prerequisites.
9399 @vindex $(+D)
9400 @vindex +D @r{(automatic variable)}
9401 @item $(+D)
9402 @vindex $(+F)
9403 @vindex +F @r{(automatic variable)}
9404 @itemx $(+F)
9405 Lists of the directory parts and the file-within-directory
9406 parts of all prerequisites, including multiple instances of duplicated
9407 prerequisites.
9409 @vindex $(?D)
9410 @vindex ?D @r{(automatic variable)}
9411 @item $(?D)
9412 @vindex $(?F)
9413 @vindex ?F @r{(automatic variable)}
9414 @itemx $(?F)
9415 Lists of the directory parts and the file-within-directory parts of
9416 all prerequisites that are newer than the target.
9417 @end table
9419 Note that we use a special stylistic convention when we talk about these
9420 automatic variables; we write ``the value of @samp{$<}'', rather than
9421 @w{``the variable @code{<}''} as we would write for ordinary variables
9422 such as @code{objects} and @code{CFLAGS}.  We think this convention
9423 looks more natural in this special case.  Please do not assume it has a
9424 deep significance; @samp{$<} refers to the variable named @code{<} just
9425 as @samp{$(CFLAGS)} refers to the variable named @code{CFLAGS}.
9426 You could just as well use @samp{$(<)} in place of @samp{$<}.
9428 @node Pattern Match, Match-Anything Rules, Automatic Variables, Pattern Rules
9429 @subsection How Patterns Match
9431 @cindex stem
9432 A target pattern is composed of a @samp{%} between a prefix and a suffix,
9433 either or both of which may be empty.  The pattern matches a file name only
9434 if the file name starts with the prefix and ends with the suffix, without
9435 overlap.  The text between the prefix and the suffix is called the
9436 @dfn{stem}.  Thus, when the pattern @samp{%.o} matches the file name
9437 @file{test.o}, the stem is @samp{test}.  The pattern rule prerequisites are
9438 turned into actual file names by substituting the stem for the character
9439 @samp{%}.  Thus, if in the same example one of the prerequisites is written
9440 as @samp{%.c}, it expands to @samp{test.c}.@refill
9442 When the target pattern does not contain a slash (and it usually does
9443 not), directory names in the file names are removed from the file name
9444 before it is compared with the target prefix and suffix.  After the
9445 comparison of the file name to the target pattern, the directory
9446 names, along with the slash that ends them, are added on to the
9447 prerequisite file names generated from the pattern rule's prerequisite
9448 patterns and the file name.  The directories are ignored only for the
9449 purpose of finding an implicit rule to use, not in the application of
9450 that rule.  Thus, @samp{e%t} matches the file name @file{src/eat},
9451 with @samp{src/a} as the stem.  When prerequisites are turned into file
9452 names, the directories from the stem are added at the front, while the
9453 rest of the stem is substituted for the @samp{%}.  The stem
9454 @samp{src/a} with a prerequisite pattern @samp{c%r} gives the file name
9455 @file{src/car}.@refill
9457 @node Match-Anything Rules, Canceling Rules, Pattern Match, Pattern Rules
9458 @subsection Match-Anything Pattern Rules
9460 @cindex match-anything rule
9461 @cindex terminal rule
9462 When a pattern rule's target is just @samp{%}, it matches any file name
9463 whatever.  We call these rules @dfn{match-anything} rules.  They are very
9464 useful, but it can take a lot of time for @code{make} to think about them,
9465 because it must consider every such rule for each file name listed either
9466 as a target or as a prerequisite.
9468 Suppose the makefile mentions @file{foo.c}.  For this target, @code{make}
9469 would have to consider making it by linking an object file @file{foo.c.o},
9470 or by C compilation-and-linking in one step from @file{foo.c.c}, or by
9471 Pascal compilation-and-linking from @file{foo.c.p}, and many other
9472 possibilities.
9474 We know these possibilities are ridiculous since @file{foo.c} is a C source
9475 file, not an executable.  If @code{make} did consider these possibilities,
9476 it would ultimately reject them, because files such as @file{foo.c.o} and
9477 @file{foo.c.p} would not exist.  But these possibilities are so
9478 numerous that @code{make} would run very slowly if it had to consider
9479 them.@refill
9481 To gain speed, we have put various constraints on the way @code{make}
9482 considers match-anything rules.  There are two different constraints that
9483 can be applied, and each time you define a match-anything rule you must
9484 choose one or the other for that rule.
9486 One choice is to mark the match-anything rule as @dfn{terminal} by defining
9487 it with a double colon.  When a rule is terminal, it does not apply unless
9488 its prerequisites actually exist.  Prerequisites that could be made with
9489 other implicit rules are not good enough.  In other words, no further
9490 chaining is allowed beyond a terminal rule.
9492 For example, the built-in implicit rules for extracting sources from RCS
9493 and SCCS files are terminal; as a result, if the file @file{foo.c,v} does
9494 not exist, @code{make} will not even consider trying to make it as an
9495 intermediate file from @file{foo.c,v.o} or from @file{RCS/SCCS/s.foo.c,v}.
9496 RCS and SCCS files are generally ultimate source files, which should not be
9497 remade from any other files; therefore, @code{make} can save time by not
9498 looking for ways to remake them.@refill
9500 If you do not mark the match-anything rule as terminal, then it is
9501 nonterminal.  A nonterminal match-anything rule cannot apply to a file name
9502 that indicates a specific type of data.  A file name indicates a specific
9503 type of data if some non-match-anything implicit rule target matches it.
9505 For example, the file name @file{foo.c} matches the target for the pattern
9506 rule @samp{%.c : %.y} (the rule to run Yacc).  Regardless of whether this
9507 rule is actually applicable (which happens only if there is a file
9508 @file{foo.y}), the fact that its target matches is enough to prevent
9509 consideration of any nonterminal match-anything rules for the file
9510 @file{foo.c}.  Thus, @code{make} will not even consider trying to make
9511 @file{foo.c} as an executable file from @file{foo.c.o}, @file{foo.c.c},
9512 @file{foo.c.p}, etc.@refill
9514 The motivation for this constraint is that nonterminal match-anything
9515 rules are used for making files containing specific types of data (such as
9516 executable files) and a file name with a recognized suffix indicates some
9517 other specific type of data (such as a C source file).
9519 Special built-in dummy pattern rules are provided solely to recognize
9520 certain file names so that nonterminal match-anything rules will not be
9521 considered.  These dummy rules have no prerequisites and no recipes, and
9522 they are ignored for all other purposes.  For example, the built-in
9523 implicit rule
9525 @example
9526 %.p :
9527 @end example
9529 @noindent
9530 exists to make sure that Pascal source files such as @file{foo.p} match a
9531 specific target pattern and thereby prevent time from being wasted looking
9532 for @file{foo.p.o} or @file{foo.p.c}.
9534 Dummy pattern rules such as the one for @samp{%.p} are made for every
9535 suffix listed as valid for use in suffix rules (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}).
9537 @node Canceling Rules,  , Match-Anything Rules, Pattern Rules
9538 @subsection Canceling Implicit Rules
9540 You can override a built-in implicit rule (or one you have defined
9541 yourself) by defining a new pattern rule with the same target and
9542 prerequisites, but a different recipe.  When the new rule is defined, the
9543 built-in one is replaced.  The new rule's position in the sequence of
9544 implicit rules is determined by where you write the new rule.
9546 You can cancel a built-in implicit rule by defining a pattern rule with the
9547 same target and prerequisites, but no recipe.  For example, the following
9548 would cancel the rule that runs the assembler:
9550 @example
9551 %.o : %.s
9552 @end example
9554 @node Last Resort, Suffix Rules, Pattern Rules, Implicit Rules
9555 @section Defining Last-Resort Default Rules
9556 @cindex last-resort default rules
9557 @cindex default rules, last-resort
9559 You can define a last-resort implicit rule by writing a terminal
9560 match-anything pattern rule with no prerequisites (@pxref{Match-Anything
9561 Rules}).  This is just like any other pattern rule; the only thing
9562 special about it is that it will match any target.  So such a rule's
9563 recipe is used for all targets and prerequisites that have no recipe
9564 of their own and for which no other implicit rule applies.
9566 For example, when testing a makefile, you might not care if the source
9567 files contain real data, only that they exist.  Then you might do this:
9569 @example
9571         touch $@@
9572 @end example
9574 @noindent
9575 to cause all the source files needed (as prerequisites) to be created
9576 automatically.
9578 @findex .DEFAULT
9579 You can instead define a recipe to be used for targets for which there
9580 are no rules at all, even ones which don't specify recipes.  You do
9581 this by writing a rule for the target @code{.DEFAULT}.  Such a rule's
9582 recipe is used for all prerequisites which do not appear as targets in
9583 any explicit rule, and for which no implicit rule applies.  Naturally,
9584 there is no @code{.DEFAULT} rule unless you write one.
9586 If you use @code{.DEFAULT} with no recipe or prerequisites:
9588 @example
9589 .DEFAULT:
9590 @end example
9592 @noindent
9593 the recipe previously stored for @code{.DEFAULT} is cleared.  Then
9594 @code{make} acts as if you had never defined @code{.DEFAULT} at all.
9596 If you do not want a target to get the recipe from a match-anything
9597 pattern rule or @code{.DEFAULT}, but you also do not want any recipe
9598 to be run for the target, you can give it an empty recipe
9599 (@pxref{Empty Recipes, ,Defining Empty Recipes}).@refill
9601 You can use a last-resort rule to override part of another makefile.
9602 @xref{Overriding Makefiles, , Overriding Part of Another Makefile}.
9604 @node Suffix Rules, Implicit Rule Search, Last Resort, Implicit Rules
9605 @section Old-Fashioned Suffix Rules
9606 @cindex old-fashioned suffix rules
9607 @cindex suffix rule
9609 @dfn{Suffix rules} are the old-fashioned way of defining implicit rules for
9610 @code{make}.  Suffix rules are obsolete because pattern rules are more
9611 general and clearer.  They are supported in GNU @code{make} for
9612 compatibility with old makefiles.  They come in two kinds:
9613 @dfn{double-suffix} and @dfn{single-suffix}.@refill
9615 A double-suffix rule is defined by a pair of suffixes: the target suffix
9616 and the source suffix.  It matches any file whose name ends with the
9617 target suffix.  The corresponding implicit prerequisite is made by
9618 replacing the target suffix with the source suffix in the file name.  A
9619 two-suffix rule whose target and source suffixes are @samp{.o} and
9620 @samp{.c} is equivalent to the pattern rule @samp{%.o : %.c}.
9622 A single-suffix rule is defined by a single suffix, which is the source
9623 suffix.  It matches any file name, and the corresponding implicit
9624 prerequisite name is made by appending the source suffix.  A single-suffix
9625 rule whose source suffix is @samp{.c} is equivalent to the pattern rule
9626 @samp{% : %.c}.
9628 Suffix rule definitions are recognized by comparing each rule's target
9629 against a defined list of known suffixes.  When @code{make} sees a rule
9630 whose target is a known suffix, this rule is considered a single-suffix
9631 rule.  When @code{make} sees a rule whose target is two known suffixes
9632 concatenated, this rule is taken as a double-suffix rule.
9634 For example, @samp{.c} and @samp{.o} are both on the default list of
9635 known suffixes.  Therefore, if you define a rule whose target is
9636 @samp{.c.o}, @code{make} takes it to be a double-suffix rule with source
9637 suffix @samp{.c} and target suffix @samp{.o}.  Here is the old-fashioned
9638 way to define the rule for compiling a C source file:@refill
9640 @example
9641 .c.o:
9642         $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
9643 @end example
9645 Suffix rules cannot have any prerequisites of their own.  If they have any,
9646 they are treated as normal files with funny names, not as suffix rules.
9647 Thus, the rule:
9649 @example
9650 .c.o: foo.h
9651         $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
9652 @end example
9654 @noindent
9655 tells how to make the file @file{.c.o} from the prerequisite file
9656 @file{foo.h}, and is not at all like the pattern rule:
9658 @example
9659 %.o: %.c foo.h
9660         $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
9661 @end example
9663 @noindent
9664 which tells how to make @samp{.o} files from @samp{.c} files, and makes all
9665 @samp{.o} files using this pattern rule also depend on @file{foo.h}.
9667 Suffix rules with no recipe are also meaningless.  They do not remove
9668 previous rules as do pattern rules with no recipe (@pxref{Canceling
9669 Rules, , Canceling Implicit Rules}).  They simply enter the suffix or
9670 pair of suffixes concatenated as a target in the data base.@refill
9672 @findex .SUFFIXES
9673 The known suffixes are simply the names of the prerequisites of the special
9674 target @code{.SUFFIXES}.  You can add your own suffixes by writing a rule
9675 for @code{.SUFFIXES} that adds more prerequisites, as in:
9677 @example
9678 .SUFFIXES: .hack .win
9679 @end example
9681 @noindent
9682 which adds @samp{.hack} and @samp{.win} to the end of the list of suffixes.
9684 If you wish to eliminate the default known suffixes instead of just adding
9685 to them, write a rule for @code{.SUFFIXES} with no prerequisites.  By
9686 special dispensation, this eliminates all existing prerequisites of
9687 @code{.SUFFIXES}.  You can then write another rule to add the suffixes you
9688 want.  For example,
9690 @example
9691 @group
9692 .SUFFIXES:            # @r{Delete the default suffixes}
9693 .SUFFIXES: .c .o .h   # @r{Define our suffix list}
9694 @end group
9695 @end example
9697 The @samp{-r} or @samp{--no-builtin-rules} flag causes the default
9698 list of suffixes to be empty.
9700 @vindex SUFFIXES
9701 The variable @code{SUFFIXES} is defined to the default list of suffixes
9702 before @code{make} reads any makefiles.  You can change the list of suffixes
9703 with a rule for the special target @code{.SUFFIXES}, but that does not alter
9704 this variable.
9706 @node Implicit Rule Search,  , Suffix Rules, Implicit Rules
9707 @section Implicit Rule Search Algorithm
9708 @cindex implicit rule, search algorithm
9709 @cindex search algorithm, implicit rule
9711 Here is the procedure @code{make} uses for searching for an implicit rule
9712 for a target @var{t}.  This procedure is followed for each double-colon
9713 rule with no recipe, for each target of ordinary rules none of which have
9714 a recipe, and for each prerequisite that is not the target of any rule.  It
9715 is also followed recursively for prerequisites that come from implicit
9716 rules, in the search for a chain of rules.
9718 Suffix rules are not mentioned in this algorithm because suffix rules are
9719 converted to equivalent pattern rules once the makefiles have been read in.
9721 For an archive member target of the form
9722 @samp{@var{archive}(@var{member})}, the following algorithm is run
9723 twice, first using the entire target name @var{t}, and second using
9724 @samp{(@var{member})} as the target @var{t} if the first run found no
9725 rule.@refill
9727 @enumerate
9728 @item
9729 Split @var{t} into a directory part, called @var{d}, and the rest,
9730 called @var{n}.  For example, if @var{t} is @samp{src/foo.o}, then
9731 @var{d} is @samp{src/} and @var{n} is @samp{foo.o}.@refill
9733 @item
9734 Make a list of all the pattern rules one of whose targets matches
9735 @var{t} or @var{n}.  If the target pattern contains a slash, it is
9736 matched against @var{t}; otherwise, against @var{n}.
9738 @item
9739 If any rule in that list is @emph{not} a match-anything rule, then
9740 remove all nonterminal match-anything rules from the list.
9742 @item
9743 Remove from the list all rules with no recipe.
9745 @item
9746 For each pattern rule in the list:
9748 @enumerate a
9749 @item
9750 Find the stem @var{s}, which is the nonempty part of @var{t} or @var{n}
9751 matched by the @samp{%} in the target pattern.@refill
9753 @item
9754 Compute the prerequisite names by substituting @var{s} for @samp{%}; if
9755 the target pattern does not contain a slash, append @var{d} to
9756 the front of each prerequisite name.@refill
9758 @item
9759 Test whether all the prerequisites exist or ought to exist.  (If a
9760 file name is mentioned in the makefile as a target or as an explicit
9761 prerequisite, then we say it ought to exist.)
9763 If all prerequisites exist or ought to exist, or there are no prerequisites,
9764 then this rule applies.
9765 @end enumerate
9767 @item
9768 If no pattern rule has been found so far, try harder.
9769 For each pattern rule in the list:
9771 @enumerate a
9772 @item
9773 If the rule is terminal, ignore it and go on to the next rule.
9775 @item
9776 Compute the prerequisite names as before.
9778 @item
9779 Test whether all the prerequisites exist or ought to exist.
9781 @item
9782 For each prerequisite that does not exist, follow this algorithm
9783 recursively to see if the prerequisite can be made by an implicit
9784 rule.
9786 @item
9787 If all prerequisites exist, ought to exist, or can be
9788 made by implicit rules, then this rule applies.
9789 @end enumerate
9791 @item
9792 If no implicit rule applies, the rule for @code{.DEFAULT}, if any,
9793 applies.  In that case, give @var{t} the same recipe that
9794 @code{.DEFAULT} has.  Otherwise, there is no recipe for @var{t}.
9795 @end enumerate
9797 Once a rule that applies has been found, for each target pattern of
9798 the rule other than the one that matched @var{t} or @var{n}, the
9799 @samp{%} in the pattern is replaced with @var{s} and the resultant
9800 file name is stored until the recipe to remake the target file @var{t}
9801 is executed.  After the recipe is executed, each of these stored file
9802 names are entered into the data base and marked as having been updated
9803 and having the same update status as the file @var{t}.
9805 When the recipe of a pattern rule is executed for @var{t}, the
9806 automatic variables are set corresponding to the target and
9807 prerequisites.  @xref{Automatic Variables}.
9809 @node Archives, Features, Implicit Rules, Top
9810 @chapter Using @code{make} to Update Archive Files
9811 @cindex archive
9813 @dfn{Archive files} are files containing named subfiles called
9814 @dfn{members}; they are maintained with the program @code{ar} and their
9815 main use is as subroutine libraries for linking.
9817 @menu
9818 * Archive Members::             Archive members as targets.
9819 * Archive Update::              The implicit rule for archive member targets.
9820 * Archive Pitfalls::            Dangers to watch out for when using archives.
9821 * Archive Suffix Rules::        You can write a special kind of suffix rule
9822                                   for updating archives.
9823 @end menu
9825 @node Archive Members, Archive Update, Archives, Archives
9826 @section Archive Members as Targets
9827 @cindex archive member targets
9829 An individual member of an archive file can be used as a target or
9830 prerequisite in @code{make}.  You specify the member named @var{member} in
9831 archive file @var{archive} as follows:
9833 @example
9834 @var{archive}(@var{member})
9835 @end example
9837 @noindent
9838 This construct is available only in targets and prerequisites, not in
9839 recipes!  Most programs that you might use in recipes do not support
9840 this syntax and cannot act directly on archive members.  Only
9841 @code{ar} and other programs specifically designed to operate on
9842 archives can do so.  Therefore, valid recipes to update an archive
9843 member target probably must use @code{ar}.  For example, this rule
9844 says to create a member @file{hack.o} in archive @file{foolib} by
9845 copying the file @file{hack.o}:
9847 @example
9848 foolib(hack.o) : hack.o
9849         ar cr foolib hack.o
9850 @end example
9852 In fact, nearly all archive member targets are updated in just this way
9853 and there is an implicit rule to do it for you.  @strong{Please note:} The
9854 @samp{c} flag to @code{ar} is required if the archive file does not
9855 already exist.
9857 To specify several members in the same archive, you can write all the
9858 member names together between the parentheses.  For example:
9860 @example
9861 foolib(hack.o kludge.o)
9862 @end example
9864 @noindent
9865 is equivalent to:
9867 @example
9868 foolib(hack.o) foolib(kludge.o)
9869 @end example
9871 @cindex wildcard, in archive member
9872 You can also use shell-style wildcards in an archive member reference.
9873 @xref{Wildcards, ,Using Wildcard Characters in File Names}.  For
9874 example, @w{@samp{foolib(*.o)}} expands to all existing members of the
9875 @file{foolib} archive whose names end in @samp{.o}; perhaps
9876 @samp{@w{foolib(hack.o)} @w{foolib(kludge.o)}}.
9878 @node Archive Update, Archive Pitfalls, Archive Members, Archives
9879 @section Implicit Rule for Archive Member Targets
9881 Recall that a target that looks like @file{@var{a}(@var{m})} stands for the
9882 member named @var{m} in the archive file @var{a}.
9884 When @code{make} looks for an implicit rule for such a target, as a special
9885 feature it considers implicit rules that match @file{(@var{m})}, as well as
9886 those that match the actual target @file{@var{a}(@var{m})}.
9888 This causes one special rule whose target is @file{(%)} to match.  This
9889 rule updates the target @file{@var{a}(@var{m})} by copying the file @var{m}
9890 into the archive.  For example, it will update the archive member target
9891 @file{foo.a(bar.o)} by copying the @emph{file} @file{bar.o} into the
9892 archive @file{foo.a} as a @emph{member} named @file{bar.o}.
9894 When this rule is chained with others, the result is very powerful.
9895 Thus, @samp{make "foo.a(bar.o)"} (the quotes are needed to protect the
9896 @samp{(} and @samp{)} from being interpreted specially by the shell) in
9897 the presence of a file @file{bar.c} is enough to cause the following
9898 recipe to be run, even without a makefile:
9900 @example
9901 cc -c bar.c -o bar.o
9902 ar r foo.a bar.o
9903 rm -f bar.o
9904 @end example
9906 @noindent
9907 Here @code{make} has envisioned the file @file{bar.o} as an intermediate
9908 file.  @xref{Chained Rules, ,Chains of Implicit Rules}.
9910 Implicit rules such as this one are written using the automatic variable
9911 @samp{$%}.  @xref{Automatic Variables}.
9913 An archive member name in an archive cannot contain a directory name, but
9914 it may be useful in a makefile to pretend that it does.  If you write an
9915 archive member target @file{foo.a(dir/file.o)}, @code{make} will perform
9916 automatic updating with this recipe:
9918 @example
9919 ar r foo.a dir/file.o
9920 @end example
9922 @noindent
9923 which has the effect of copying the file @file{dir/file.o} into a member
9924 named @file{file.o}.  In connection with such usage, the automatic variables
9925 @code{%D} and @code{%F} may be useful.
9927 @menu
9928 * Archive Symbols::             How to update archive symbol directories.
9929 @end menu
9931 @node Archive Symbols,  , Archive Update, Archive Update
9932 @subsection Updating Archive Symbol Directories
9933 @cindex @code{__.SYMDEF}
9934 @cindex updating archive symbol directories
9935 @cindex archive symbol directory updating
9936 @cindex symbol directories, updating archive
9937 @cindex directories, updating archive symbol
9939 An archive file that is used as a library usually contains a special member
9940 named @file{__.SYMDEF} that contains a directory of the external symbol
9941 names defined by all the other members.  After you update any other
9942 members, you need to update @file{__.SYMDEF} so that it will summarize the
9943 other members properly.  This is done by running the @code{ranlib} program:
9945 @example
9946 ranlib @var{archivefile}
9947 @end example
9949 Normally you would put this command in the rule for the archive file,
9950 and make all the members of the archive file prerequisites of that rule.
9951 For example,
9953 @example
9954 libfoo.a: libfoo.a(x.o) libfoo.a(y.o) @dots{}
9955         ranlib libfoo.a
9956 @end example
9958 @noindent
9959 The effect of this is to update archive members @file{x.o}, @file{y.o},
9960 etc., and then update the symbol directory member @file{__.SYMDEF} by
9961 running @code{ranlib}.  The rules for updating the members are not shown
9962 here; most likely you can omit them and use the implicit rule which copies
9963 files into the archive, as described in the preceding section.
9965 This is not necessary when using the GNU @code{ar} program, which
9966 updates the @file{__.SYMDEF} member automatically.
9968 @node Archive Pitfalls, Archive Suffix Rules, Archive Update, Archives
9969 @section Dangers When Using Archives
9970 @cindex archive, and parallel execution
9971 @cindex parallel execution, and archive update
9972 @cindex archive, and @code{-j}
9973 @cindex @code{-j}, and archive update
9975 It is important to be careful when using parallel execution (the
9976 @code{-j} switch; @pxref{Parallel, ,Parallel Execution}) and archives.
9977 If multiple @code{ar} commands run at the same time on the same archive
9978 file, they will not know about each other and can corrupt the file.
9980 Possibly a future version of @code{make} will provide a mechanism to
9981 circumvent this problem by serializing all recipes that operate on the
9982 same archive file.  But for the time being, you must either write your
9983 makefiles to avoid this problem in some other way, or not use @code{-j}.
9985 @node Archive Suffix Rules,  , Archive Pitfalls, Archives
9986 @section Suffix Rules for Archive Files
9987 @cindex suffix rule, for archive
9988 @cindex archive, suffix rule for
9989 @cindex library archive, suffix rule for
9990 @cindex @code{.a} (archives)
9992 You can write a special kind of suffix rule for dealing with archive
9993 files.  @xref{Suffix Rules}, for a full explanation of suffix rules.
9994 Archive suffix rules are obsolete in GNU @code{make}, because pattern
9995 rules for archives are a more general mechanism (@pxref{Archive
9996 Update}).  But they are retained for compatibility with other
9997 @code{make}s.
9999 To write a suffix rule for archives, you simply write a suffix rule
10000 using the target suffix @samp{.a} (the usual suffix for archive files).
10001 For example, here is the old-fashioned suffix rule to update a library
10002 archive from C source files:
10004 @example
10005 @group
10006 .c.a:
10007         $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o
10008         $(AR) r $@@ $*.o
10009         $(RM) $*.o
10010 @end group
10011 @end example
10013 @noindent
10014 This works just as if you had written the pattern rule:
10016 @example
10017 @group
10018 (%.o): %.c
10019         $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o
10020         $(AR) r $@@ $*.o
10021         $(RM) $*.o
10022 @end group
10023 @end example
10025 In fact, this is just what @code{make} does when it sees a suffix rule
10026 with @samp{.a} as the target suffix.  Any double-suffix rule
10027 @w{@samp{.@var{x}.a}} is converted to a pattern rule with the target
10028 pattern @samp{(%.o)} and a prerequisite pattern of @samp{%.@var{x}}.
10030 Since you might want to use @samp{.a} as the suffix for some other kind
10031 of file, @code{make} also converts archive suffix rules to pattern rules
10032 in the normal way (@pxref{Suffix Rules}).  Thus a double-suffix rule
10033 @w{@samp{.@var{x}.a}} produces two pattern rules: @samp{@w{(%.o):}
10034 @w{%.@var{x}}} and @samp{@w{%.a}: @w{%.@var{x}}}.@refill
10036 @node Features, Missing, Archives, Top
10037 @chapter Features of GNU @code{make}
10038 @cindex features of GNU @code{make}
10039 @cindex portability
10040 @cindex compatibility
10042 Here is a summary of the features of GNU @code{make}, for comparison
10043 with and credit to other versions of @code{make}.  We consider the
10044 features of @code{make} in 4.2 BSD systems as a baseline.  If you are
10045 concerned with writing portable makefiles, you should not use the
10046 features of @code{make} listed here, nor the ones in @ref{Missing}.
10048 Many features come from the version of @code{make} in System V.
10050 @itemize @bullet
10051 @item
10052 The @code{VPATH} variable and its special meaning.
10053 @xref{Directory Search, , Searching Directories for Prerequisites}.
10054 This feature exists in System V @code{make}, but is undocumented.
10055 It is documented in 4.3 BSD @code{make} (which says it mimics System V's
10056 @code{VPATH} feature).@refill
10058 @item
10059 Included makefiles.  @xref{Include, ,Including Other Makefiles}.
10060 Allowing multiple files to be included with a single directive is a GNU
10061 extension.
10063 @item
10064 Variables are read from and communicated via the environment.
10065 @xref{Environment, ,Variables from the Environment}.
10067 @item
10068 Options passed through the variable @code{MAKEFLAGS} to recursive
10069 invocations of @code{make}.
10070 @xref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
10072 @item
10073 The automatic variable @code{$%} is set to the member name
10074 in an archive reference.  @xref{Automatic Variables}.
10076 @item
10077 The automatic variables @code{$@@}, @code{$*}, @code{$<}, @code{$%},
10078 and @code{$?} have corresponding forms like @code{$(@@F)} and
10079 @code{$(@@D)}.  We have generalized this to @code{$^} as an obvious
10080 extension.  @xref{Automatic Variables}.@refill
10082 @item
10083 Substitution variable references.
10084 @xref{Reference, ,Basics of Variable References}.
10086 @item
10087 The command line options @samp{-b} and @samp{-m}, accepted and
10088 ignored.  In System V @code{make}, these options actually do something.
10090 @item
10091 Execution of recursive commands to run @code{make} via the variable
10092 @code{MAKE} even if @samp{-n}, @samp{-q} or @samp{-t} is specified.
10093 @xref{Recursion, ,Recursive Use of @code{make}}.
10095 @item
10096 Support for suffix @samp{.a} in suffix rules.  @xref{Archive Suffix
10097 Rules}.  This feature is obsolete in GNU @code{make}, because the
10098 general feature of rule chaining (@pxref{Chained Rules, ,Chains of
10099 Implicit Rules}) allows one pattern rule for installing members in an
10100 archive (@pxref{Archive Update}) to be sufficient.
10102 @item
10103 The arrangement of lines and backslash-newline combinations in
10104 recipes is retained when the recipes are printed, so they appear as
10105 they do in the makefile, except for the stripping of initial
10106 whitespace.
10107 @end itemize
10109 The following features were inspired by various other versions of
10110 @code{make}.  In some cases it is unclear exactly which versions inspired
10111 which others.
10113 @itemize @bullet
10114 @item
10115 Pattern rules using @samp{%}.
10116 This has been implemented in several versions of @code{make}.
10117 We're not sure who invented it first, but it's been spread around a bit.
10118 @xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.@refill
10120 @item
10121 Rule chaining and implicit intermediate files.
10122 This was implemented by Stu Feldman in his version of @code{make}
10123 for AT&T Eighth Edition Research Unix, and later by Andrew Hume of
10124 AT&T Bell Labs in his @code{mk} program (where he terms it
10125 ``transitive closure'').  We do not really know if
10126 we got this from either of them or thought it up ourselves at the
10127 same time.  @xref{Chained Rules, ,Chains of Implicit Rules}.
10129 @item
10130 The automatic variable @code{$^} containing a list of all prerequisites
10131 of the current target.  We did not invent this, but we have no idea who
10132 did.  @xref{Automatic Variables}.  The automatic variable
10133 @code{$+} is a simple extension of @code{$^}.
10135 @item
10136 The ``what if'' flag (@samp{-W} in GNU @code{make}) was (as far as we know)
10137 invented by Andrew Hume in @code{mk}.
10138 @xref{Instead of Execution, ,Instead of Executing Recipes}.
10140 @item
10141 The concept of doing several things at once (parallelism) exists in
10142 many incarnations of @code{make} and similar programs, though not in the
10143 System V or BSD implementations.  @xref{Execution, ,Recipe Execution}.
10145 @item
10146 Modified variable references using pattern substitution come from
10147 SunOS 4.  @xref{Reference, ,Basics of Variable References}.
10148 This functionality was provided in GNU @code{make} by the
10149 @code{patsubst} function before the alternate syntax was implemented
10150 for compatibility with SunOS 4.  It is not altogether clear who
10151 inspired whom, since GNU @code{make} had @code{patsubst} before SunOS
10152 4 was released.@refill
10154 @item
10155 The special significance of @samp{+} characters preceding recipe lines
10156 (@pxref{Instead of Execution, ,Instead of Executing Recipes}) is
10157 mandated by @cite{IEEE Standard 1003.2-1992} (POSIX.2).
10159 @item
10160 The @samp{+=} syntax to append to the value of a variable comes from SunOS
10161 4 @code{make}.  @xref{Appending, , Appending More Text to Variables}.
10163 @item
10164 The syntax @w{@samp{@var{archive}(@var{mem1} @var{mem2}@dots{})}} to list
10165 multiple members in a single archive file comes from SunOS 4 @code{make}.
10166 @xref{Archive Members}.
10168 @item
10169 The @code{-include} directive to include makefiles with no error for a
10170 nonexistent file comes from SunOS 4 @code{make}.  (But note that SunOS 4
10171 @code{make} does not allow multiple makefiles to be specified in one
10172 @code{-include} directive.)  The same feature appears with the name
10173 @code{sinclude} in SGI @code{make} and perhaps others.
10174 @end itemize
10176 The remaining features are inventions new in GNU @code{make}:
10178 @itemize @bullet
10179 @item
10180 Use the @samp{-v} or @samp{--version} option to print version and
10181 copyright information.
10183 @item
10184 Use the @samp{-h} or @samp{--help} option to summarize the options to
10185 @code{make}.
10187 @item
10188 Simply-expanded variables.  @xref{Flavors, ,The Two Flavors of Variables}.
10190 @item
10191 Pass command line variable assignments automatically through the
10192 variable @code{MAKE} to recursive @code{make} invocations.
10193 @xref{Recursion, ,Recursive Use of @code{make}}.
10195 @item
10196 Use the @samp{-C} or @samp{--directory} command option to change
10197 directory.  @xref{Options Summary, ,Summary of Options}.
10199 @item
10200 Make verbatim variable definitions with @code{define}.
10201 @xref{Multi-Line, ,Defining Multi-Line Variables}.
10203 @item
10204 Declare phony targets with the special target @code{.PHONY}.
10206 Andrew Hume of AT&T Bell Labs implemented a similar feature with a
10207 different syntax in his @code{mk} program.  This seems to be a case of
10208 parallel discovery.  @xref{Phony Targets, ,Phony Targets}.
10210 @item
10211 Manipulate text by calling functions.
10212 @xref{Functions, ,Functions for Transforming Text}.
10214 @item
10215 Use the @samp{-o} or @samp{--old-file}
10216 option to pretend a file's modification-time is old.
10217 @xref{Avoiding Compilation, ,Avoiding Recompilation of Some Files}.
10219 @item
10220 Conditional execution.
10222 This feature has been implemented numerous times in various versions
10223 of @code{make}; it seems a natural extension derived from the features
10224 of the C preprocessor and similar macro languages and is not a
10225 revolutionary concept.  @xref{Conditionals, ,Conditional Parts of Makefiles}.
10227 @item
10228 Specify a search path for included makefiles.
10229 @xref{Include, ,Including Other Makefiles}.
10231 @item
10232 Specify extra makefiles to read with an environment variable.
10233 @xref{MAKEFILES Variable, ,The Variable @code{MAKEFILES}}.
10235 @item
10236 Strip leading sequences of @samp{./} from file names, so that
10237 @file{./@var{file}} and @file{@var{file}} are considered to be the
10238 same file.@refill
10240 @item
10241 Use a special search method for library prerequisites written in the
10242 form @samp{-l@var{name}}.
10243 @xref{Libraries/Search, ,Directory Search for Link Libraries}.
10245 @item
10246 Allow suffixes for suffix rules
10247 (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}) to contain any
10248 characters.  In other versions of @code{make}, they must begin with
10249 @samp{.} and not contain any @samp{/} characters.
10251 @item
10252 Keep track of the current level of @code{make} recursion using the
10253 variable @code{MAKELEVEL}.  @xref{Recursion, ,Recursive Use of @code{make}}.
10255 @item
10256 Provide any goals given on the command line in the variable
10257 @code{MAKECMDGOALS}.  @xref{Goals, ,Arguments to Specify the Goals}.
10259 @item
10260 Specify static pattern rules.  @xref{Static Pattern, ,Static Pattern Rules}.
10262 @item
10263 Provide selective @code{vpath} search.
10264 @xref{Directory Search, ,Searching Directories for Prerequisites}.
10266 @item
10267 Provide computed variable references.
10268 @xref{Reference, ,Basics of Variable References}.
10270 @item
10271 Update makefiles.  @xref{Remaking Makefiles, ,How Makefiles Are Remade}.
10272 System V @code{make} has a very, very limited form of this
10273 functionality in that it will check out SCCS files for makefiles.
10275 @item
10276 Various new built-in implicit rules.
10277 @xref{Catalogue of Rules, ,Catalogue of Implicit Rules}.
10279 @item
10280 The built-in variable @samp{MAKE_VERSION} gives the version number of
10281 @code{make}.
10282 @vindex MAKE_VERSION
10283 @end itemize
10285 @node Missing, Makefile Conventions, Features, Top
10286 @chapter Incompatibilities and Missing Features
10287 @cindex incompatibilities
10288 @cindex missing features
10289 @cindex features, missing
10291 The @code{make} programs in various other systems support a few features
10292 that are not implemented in GNU @code{make}.  The POSIX.2 standard
10293 (@cite{IEEE Standard 1003.2-1992}) which specifies @code{make} does not
10294 require any of these features.@refill
10296 @itemize @bullet
10297 @item
10298 A target of the form @samp{@var{file}((@var{entry}))} stands for a member
10299 of archive file @var{file}.  The member is chosen, not by name, but by
10300 being an object file which defines the linker symbol @var{entry}.@refill
10302 This feature was not put into GNU @code{make} because of the
10303 nonmodularity of putting knowledge into @code{make} of the internal
10304 format of archive file symbol tables.
10305 @xref{Archive Symbols, ,Updating Archive Symbol Directories}.
10307 @item
10308 Suffixes (used in suffix rules) that end with the character @samp{~}
10309 have a special meaning to System V @code{make};
10310 they refer to the SCCS file that corresponds
10311 to the file one would get without the @samp{~}.  For example, the
10312 suffix rule @samp{.c~.o} would make the file @file{@var{n}.o} from
10313 the SCCS file @file{s.@var{n}.c}.  For complete coverage, a whole
10314 series of such suffix rules is required.
10315 @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
10317 In GNU @code{make}, this entire series of cases is handled by two
10318 pattern rules for extraction from SCCS, in combination with the
10319 general feature of rule chaining.
10320 @xref{Chained Rules, ,Chains of Implicit Rules}.
10322 @item
10323 In System V and 4.3 BSD @code{make}, files found by @code{VPATH}
10324 search (@pxref{Directory Search, ,Searching Directories for
10325 Prerequisites}) have their names changed inside recipes.  We feel it
10326 is much cleaner to always use automatic variables and thus make this
10327 feature obsolete.@refill
10329 @item
10330 In some Unix @code{make}s, the automatic variable @code{$*} appearing in
10331 the prerequisites of a rule has the amazingly strange ``feature'' of
10332 expanding to the full name of the @emph{target of that rule}.  We cannot
10333 imagine what went on in the minds of Unix @code{make} developers to do
10334 this; it is utterly inconsistent with the normal definition of @code{$*}.
10335 @vindex * @r{(automatic variable), unsupported bizarre usage}
10337 @item
10338 In some Unix @code{make}s, implicit rule search (@pxref{Implicit
10339 Rules, ,Using Implicit Rules}) is apparently done for @emph{all}
10340 targets, not just those without recipes.  This means you can
10341 do:@refill
10343 @example
10344 @group
10345 foo.o:
10346         cc -c foo.c
10347 @end group
10348 @end example
10350 @noindent
10351 and Unix @code{make} will intuit that @file{foo.o} depends on
10352 @file{foo.c}.@refill
10354 We feel that such usage is broken.  The prerequisite properties of
10355 @code{make} are well-defined (for GNU @code{make}, at least),
10356 and doing such a thing simply does not fit the model.@refill
10358 @item
10359 GNU @code{make} does not include any built-in implicit rules for
10360 compiling or preprocessing EFL programs.  If we hear of anyone who is
10361 using EFL, we will gladly add them.
10363 @item
10364 It appears that in SVR4 @code{make}, a suffix rule can be specified
10365 with no recipe, and it is treated as if it had an empty recipe
10366 (@pxref{Empty Recipes}).  For example:
10368 @example
10369 .c.a:
10370 @end example
10372 @noindent
10373 will override the built-in @file{.c.a} suffix rule.
10375 We feel that it is cleaner for a rule without a recipe to always simply
10376 add to the prerequisite list for the target.  The above example can be
10377 easily rewritten to get the desired behavior in GNU @code{make}:
10379 @example
10380 .c.a: ;
10381 @end example
10383 @item
10384 Some versions of @code{make} invoke the shell with the @samp{-e} flag,
10385 except under @samp{-k} (@pxref{Testing, ,Testing the Compilation of a
10386 Program}).  The @samp{-e} flag tells the shell to exit as soon as any
10387 program it runs returns a nonzero status.  We feel it is cleaner to
10388 write each line of the recipe to stand on its own and not require this
10389 special treatment.
10390 @end itemize
10392 @comment The makefile standards are in a separate file that is also
10393 @comment included by standards.texi.
10394 @include make-stds.texi
10396 @node Quick Reference, Error Messages, Makefile Conventions, Top
10397 @appendix Quick Reference
10399 This appendix summarizes the directives, text manipulation functions,
10400 and special variables which GNU @code{make} understands.
10401 @xref{Special Targets}, @ref{Catalogue of Rules, ,Catalogue of Implicit Rules},
10402 and @ref{Options Summary, ,Summary of Options},
10403 for other summaries.
10405 Here is a summary of the directives GNU @code{make} recognizes:
10407 @table @code
10408 @item define @var{variable}
10409 @itemx define @var{variable} =
10410 @itemx define @var{variable} :=
10411 @itemx define @var{variable} +=
10412 @itemx define @var{variable} ?=
10413 @itemx endef
10414 Define multi-line variables.@*
10415 @xref{Multi-Line}.
10417 @item ifdef @var{variable}
10418 @itemx ifndef @var{variable}
10419 @itemx ifeq (@var{a},@var{b})
10420 @itemx ifeq "@var{a}" "@var{b}"
10421 @itemx ifeq '@var{a}' '@var{b}'
10422 @itemx ifneq (@var{a},@var{b})
10423 @itemx ifneq "@var{a}" "@var{b}"
10424 @itemx ifneq '@var{a}' '@var{b}'
10425 @itemx else
10426 @itemx endif
10427 Conditionally evaluate part of the makefile.@*
10428 @xref{Conditionals}.
10430 @item include @var{file}
10431 @itemx -include @var{file}
10432 @itemx sinclude @var{file}
10433 Include another makefile.@*
10434 @xref{Include, ,Including Other Makefiles}.
10436 @item override @var{variable-assignment}
10437 Define a variable, overriding any previous definition, even one from
10438 the command line.@*
10439 @xref{Override Directive, ,The @code{override} Directive}.
10441 @item export
10442 Tell @code{make} to export all variables to child processes by default.@*
10443 @xref{Variables/Recursion, , Communicating Variables to a Sub-@code{make}}.
10445 @item export @var{variable}
10446 @itemx export @var{variable-assignment}
10447 @itemx unexport @var{variable}
10448 Tell @code{make} whether or not to export a particular variable to child
10449 processes.@*
10450 @xref{Variables/Recursion, , Communicating Variables to a Sub-@code{make}}.
10452 @item private @var{variable-assignment}
10453 Do not allow this variable assignment to be inherited by prerequisites.@*
10454 @xref{Suppressing Inheritance}.
10456 @item vpath @var{pattern} @var{path}
10457 Specify a search path for files matching a @samp{%} pattern.@*
10458 @xref{Selective Search, , The @code{vpath} Directive}.
10460 @item vpath @var{pattern}
10461 Remove all search paths previously specified for @var{pattern}.
10463 @item vpath
10464 Remove all search paths previously specified in any @code{vpath}
10465 directive.
10466 @end table
10468 Here is a summary of the built-in functions (@pxref{Functions}):
10470 @table @code
10471 @item $(subst @var{from},@var{to},@var{text})
10472 Replace @var{from} with @var{to} in @var{text}.@*
10473 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10475 @item $(patsubst @var{pattern},@var{replacement},@var{text})
10476 Replace words matching @var{pattern} with @var{replacement} in @var{text}.@*
10477 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10479 @item $(strip @var{string})
10480 Remove excess whitespace characters from @var{string}.@*
10481 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10483 @item $(findstring @var{find},@var{text})
10484 Locate @var{find} in @var{text}.@*
10485 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10487 @item $(filter @var{pattern}@dots{},@var{text})
10488 Select words in @var{text} that match one of the @var{pattern} words.@*
10489 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10491 @item $(filter-out @var{pattern}@dots{},@var{text})
10492 Select words in @var{text} that @emph{do not} match any of the @var{pattern} words.@*
10493 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10495 @item $(sort @var{list})
10496 Sort the words in @var{list} lexicographically, removing duplicates.@*
10497 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10499 @item $(word @var{n},@var{text})
10500 Extract the @var{n}th word (one-origin) of @var{text}.@*
10501 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10503 @item $(words @var{text})
10504 Count the number of words in @var{text}.@*
10505 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10507 @item $(wordlist @var{s},@var{e},@var{text})
10508 Returns the list of words in @var{text} from @var{s} to @var{e}.@*
10509 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10511 @item $(firstword @var{names}@dots{})
10512 Extract the first word of @var{names}.@*
10513 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10515 @item $(lastword @var{names}@dots{})
10516 Extract the last word of @var{names}.@*
10517 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10519 @item $(dir @var{names}@dots{})
10520 Extract the directory part of each file name.@*
10521 @xref{File Name Functions, ,Functions for File Names}.
10523 @item $(notdir @var{names}@dots{})
10524 Extract the non-directory part of each file name.@*
10525 @xref{File Name Functions, ,Functions for File Names}.
10527 @item $(suffix @var{names}@dots{})
10528 Extract the suffix (the last @samp{.} and following characters) of each file name.@*
10529 @xref{File Name Functions, ,Functions for File Names}.
10531 @item $(basename @var{names}@dots{})
10532 Extract the base name (name without suffix) of each file name.@*
10533 @xref{File Name Functions, ,Functions for File Names}.
10535 @item $(addsuffix @var{suffix},@var{names}@dots{})
10536 Append @var{suffix} to each word in @var{names}.@*
10537 @xref{File Name Functions, ,Functions for File Names}.
10539 @item $(addprefix @var{prefix},@var{names}@dots{})
10540 Prepend @var{prefix} to each word in @var{names}.@*
10541 @xref{File Name Functions, ,Functions for File Names}.
10543 @item $(join @var{list1},@var{list2})
10544 Join two parallel lists of words.@*
10545 @xref{File Name Functions, ,Functions for File Names}.
10547 @item $(wildcard @var{pattern}@dots{})
10548 Find file names matching a shell file name pattern (@emph{not} a
10549 @samp{%} pattern).@*
10550 @xref{Wildcard Function, ,The Function @code{wildcard}}.
10552 @item $(realpath @var{names}@dots{})
10553 For each file name in @var{names}, expand to an absolute name that
10554 does not contain any @code{.}, @code{..}, nor symlinks.@*
10555 @xref{File Name Functions, ,Functions for File Names}.
10557 @item $(abspath @var{names}@dots{})
10558 For each file name in @var{names}, expand to an absolute name that
10559 does not contain any @code{.} or @code{..} components, but preserves
10560 symlinks.@*
10561 @xref{File Name Functions, ,Functions for File Names}.
10563 @item $(error @var{text}@dots{})
10564 When this function is evaluated, @code{make} generates a fatal error
10565 with the message @var{text}.@*
10566 @xref{Make Control Functions, ,Functions That Control Make}.
10568 @item $(warning @var{text}@dots{})
10569 When this function is evaluated, @code{make} generates a warning with
10570 the message @var{text}.@*
10571 @xref{Make Control Functions, ,Functions That Control Make}.
10573 @item $(shell @var{command})
10574 Execute a shell command and return its output.@*
10575 @xref{Shell Function, , The @code{shell} Function}.
10577 @item $(origin @var{variable})
10578 Return a string describing how the @code{make} variable @var{variable} was
10579 defined.@*
10580 @xref{Origin Function, , The @code{origin} Function}.
10582 @item $(flavor @var{variable})
10583 Return a string describing the flavor of the @code{make} variable
10584 @var{variable}.@*
10585 @xref{Flavor Function, , The @code{flavor} Function}.
10587 @item $(foreach @var{var},@var{words},@var{text})
10588 Evaluate @var{text} with @var{var} bound to each word in @var{words},
10589 and concatenate the results.@*
10590 @xref{Foreach Function, ,The @code{foreach} Function}.
10592 @item $(if @var{condition},@var{then-part}[,@var{else-part}])
10593 Evaluate the condition @var{condition}; if it's non-empty substitute
10594 the expansion of the @var{then-part} otherwise substitute the
10595 expansion of the @var{else-part}.@*
10596 @xref{Conditional Functions, ,Functions for Conditionals}.
10598 @item $(or @var{condition1}[,@var{condition2}[,@var{condition3}@dots{}]])
10599 Evaluate each condition @var{conditionN} one at a time; substitute the
10600 first non-empty expansion.  If all expansions are empty, substitute
10601 the empty string.@*
10602 @xref{Conditional Functions, ,Functions for Conditionals}.
10604 @item $(and @var{condition1}[,@var{condition2}[,@var{condition3}@dots{}]])
10605 Evaluate each condition @var{conditionN} one at a time; if any
10606 expansion results in the empty string substitute the empty string.  If
10607 all expansions result in a non-empty string, substitute the expansion
10608 of the last @var{condition}.@*
10609 @xref{Conditional Functions, ,Functions for Conditionals}.
10611 @item $(call @var{var},@var{param},@dots{})
10612 Evaluate the variable @var{var} replacing any references to @code{$(1)},
10613 @code{$(2)} with the first, second, etc.@: @var{param} values.@*
10614 @xref{Call Function, ,The @code{call} Function}.
10616 @item $(eval @var{text})
10617 Evaluate @var{text} then read the results as makefile commands.
10618 Expands to the empty string.@*
10619 @xref{Eval Function, ,The @code{eval} Function}.
10621 @item $(value @var{var})
10622 Evaluates to the contents of the variable @var{var}, with no expansion
10623 performed on it.@*
10624 @xref{Value Function, ,The @code{value} Function}.
10625 @end table
10627 Here is a summary of the automatic variables.
10628 @xref{Automatic Variables},
10629 for full information.
10631 @table @code
10632 @item $@@
10633 The file name of the target.
10635 @item $%
10636 The target member name, when the target is an archive member.
10638 @item $<
10639 The name of the first prerequisite.
10641 @item $?
10642 The names of all the prerequisites that are
10643 newer than the target, with spaces between them.
10644 For prerequisites which are archive members, only
10645 the named member is used (@pxref{Archives}).
10647 @item $^
10648 @itemx $+
10649 The names of all the prerequisites, with spaces between them.  For
10650 prerequisites which are archive members, only the named member is used
10651 (@pxref{Archives}).  The value of @code{$^} omits duplicate
10652 prerequisites, while @code{$+} retains them and preserves their order.
10654 @item $*
10655 The stem with which an implicit rule matches
10656 (@pxref{Pattern Match, ,How Patterns Match}).
10658 @item $(@@D)
10659 @itemx $(@@F)
10660 The directory part and the file-within-directory part of @code{$@@}.
10662 @item $(*D)
10663 @itemx $(*F)
10664 The directory part and the file-within-directory part of @code{$*}.
10666 @item $(%D)
10667 @itemx $(%F)
10668 The directory part and the file-within-directory part of @code{$%}.
10670 @item $(<D)
10671 @itemx $(<F)
10672 The directory part and the file-within-directory part of @code{$<}.
10674 @item $(^D)
10675 @itemx $(^F)
10676 The directory part and the file-within-directory part of @code{$^}.
10678 @item $(+D)
10679 @itemx $(+F)
10680 The directory part and the file-within-directory part of @code{$+}.
10682 @item $(?D)
10683 @itemx $(?F)
10684 The directory part and the file-within-directory part of @code{$?}.
10685 @end table
10687 These variables are used specially by GNU @code{make}:
10689 @table @code
10690 @item MAKEFILES
10692 Makefiles to be read on every invocation of @code{make}.@*
10693 @xref{MAKEFILES Variable, ,The Variable @code{MAKEFILES}}.
10695 @item VPATH
10697 Directory search path for files not found in the current directory.@*
10698 @xref{General Search, , @code{VPATH} Search Path for All Prerequisites}.
10700 @item SHELL
10702 The name of the system default command interpreter, usually @file{/bin/sh}.
10703 You can set @code{SHELL} in the makefile to change the shell used to run
10704 recipes.  @xref{Execution, ,Recipe Execution}.  The @code{SHELL}
10705 variable is handled specially when importing from and exporting to the
10706 environment.  @xref{Choosing the Shell}.
10708 @item MAKESHELL
10710 On MS-DOS only, the name of the command interpreter that is to be used
10711 by @code{make}.  This value takes precedence over the value of
10712 @code{SHELL}.  @xref{Execution, ,MAKESHELL variable}.
10714 @item MAKE
10716 The name with which @code{make} was invoked.  Using this variable in
10717 recipes has special meaning.  @xref{MAKE Variable, ,How the
10718 @code{MAKE} Variable Works}.
10720 @item MAKELEVEL
10722 The number of levels of recursion (sub-@code{make}s).@*
10723 @xref{Variables/Recursion}.
10725 @item MAKEFLAGS
10727 The flags given to @code{make}.  You can set this in the environment or
10728 a makefile to set flags.@*
10729 @xref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
10731 It is @emph{never} appropriate to use @code{MAKEFLAGS} directly in a
10732 recipe line: its contents may not be quoted correctly for use in the
10733 shell.  Always allow recursive @code{make}'s to obtain these values
10734 through the environment from its parent.
10736 @item MAKECMDGOALS
10738 The targets given to @code{make} on the command line.  Setting this
10739 variable has no effect on the operation of @code{make}.@*
10740 @xref{Goals, ,Arguments to Specify the Goals}.
10742 @item CURDIR
10744 Set to the pathname of the current working directory (after all
10745 @code{-C} options are processed, if any).  Setting this variable has no
10746 effect on the operation of @code{make}.@*
10747 @xref{Recursion, ,Recursive Use of @code{make}}.
10749 @item SUFFIXES
10751 The default list of suffixes before @code{make} reads any makefiles.
10753 @item .LIBPATTERNS
10754 Defines the naming of the libraries @code{make} searches for, and their
10755 order.@*
10756 @xref{Libraries/Search, ,Directory Search for Link Libraries}.
10757 @end table
10759 @node Error Messages, Complex Makefile, Quick Reference, Top
10760 @comment  node-name,  next,  previous,  up
10761 @appendix Errors Generated by Make
10763 Here is a list of the more common errors you might see generated by
10764 @code{make}, and some information about what they mean and how to fix
10765 them.
10767 Sometimes @code{make} errors are not fatal, especially in the presence
10768 of a @code{-} prefix on a recipe line, or the @code{-k} command line
10769 option.  Errors that are fatal are prefixed with the string
10770 @code{***}.
10772 Error messages are all either prefixed with the name of the program
10773 (usually @samp{make}), or, if the error is found in a makefile, the name
10774 of the file and linenumber containing the problem.
10776 In the table below, these common prefixes are left off.
10778 @table @samp
10780 @item [@var{foo}] Error @var{NN}
10781 @itemx [@var{foo}] @var{signal description}
10782 These errors are not really @code{make} errors at all.  They mean that a
10783 program that @code{make} invoked as part of a recipe returned a
10784 non-0 error code (@samp{Error @var{NN}}), which @code{make} interprets
10785 as failure, or it exited in some other abnormal fashion (with a
10786 signal of some type).  @xref{Errors, ,Errors in Recipes}.
10788 If no @code{***} is attached to the message, then the subprocess failed
10789 but the rule in the makefile was prefixed with the @code{-} special
10790 character, so @code{make} ignored the error.
10792 @item missing separator.  Stop.
10793 @itemx missing separator (did you mean TAB instead of 8 spaces?).  Stop.
10794 This means that @code{make} could not understand much of anything
10795 about the makefile line it just read.  GNU @code{make} looks for
10796 various separators (@code{:}, @code{=}, recipe prefix characters,
10797 etc.) to indicate what kind of line it's parsing.  This message means
10798 it couldn't find a valid one.
10800 One of the most common reasons for this message is that you (or
10801 perhaps your oh-so-helpful editor, as is the case with many MS-Windows
10802 editors) have attempted to indent your recipe lines with spaces
10803 instead of a tab character.  In this case, @code{make} will use the
10804 second form of the error above.  Remember that every line in the
10805 recipe must begin with a tab character (unless you set
10806 @code{.CMDPREFIX}; @pxref{Special Variables}).  Eight spaces do not
10807 count.  @xref{Rule Syntax}.
10809 @item recipe commences before first target.  Stop.
10810 @itemx missing rule before recipe.  Stop.
10811 This means the first thing in the makefile seems to be part of a
10812 recipe: it begins with a recipe prefix character and doesn't appear to
10813 be a legal @code{make} directive (such as a variable assignment).
10814 Recipes must always be associated with a target.
10816 The second form is generated if the line has a semicolon as the first
10817 non-whitespace character; @code{make} interprets this to mean you left
10818 out the "target: prerequisite" section of a rule.  @xref{Rule Syntax}.
10820 @item No rule to make target `@var{xxx}'.
10821 @itemx No rule to make target `@var{xxx}', needed by `@var{yyy}'.
10822 This means that @code{make} decided it needed to build a target, but
10823 then couldn't find any instructions in the makefile on how to do that,
10824 either explicit or implicit (including in the default rules database).
10826 If you want that file to be built, you will need to add a rule to your
10827 makefile describing how that target can be built.  Other possible
10828 sources of this problem are typos in the makefile (if that filename is
10829 wrong) or a corrupted source tree (if that file is not supposed to be
10830 built, but rather only a prerequisite).
10832 @item No targets specified and no makefile found.  Stop.
10833 @itemx No targets.  Stop.
10834 The former means that you didn't provide any targets to be built on the
10835 command line, and @code{make} couldn't find any makefiles to read in.
10836 The latter means that some makefile was found, but it didn't contain any
10837 default goal and none was given on the command line.  GNU @code{make}
10838 has nothing to do in these situations.
10839 @xref{Makefile Arguments, ,Arguments to Specify the Makefile}.@refill
10841 @item Makefile `@var{xxx}' was not found.
10842 @itemx Included makefile `@var{xxx}' was not found.
10843 A makefile specified on the command line (first form) or included
10844 (second form) was not found.
10846 @item warning: overriding recipe for target `@var{xxx}'
10847 @itemx warning: ignoring old recipe for target `@var{xxx}'
10848 GNU @code{make} allows only one recipe to be specified per target
10849 (except for double-colon rules).  If you give a recipe for a target
10850 which already has been defined to have one, this warning is issued and
10851 the second recipe will overwrite the first.  @xref{Multiple Rules,
10852 ,Multiple Rules for One Target}.
10854 @item Circular @var{xxx} <- @var{yyy} dependency dropped.
10855 This means that @code{make} detected a loop in the dependency graph:
10856 after tracing the prerequisite @var{yyy} of target @var{xxx}, and its
10857 prerequisites, etc., one of them depended on @var{xxx} again.
10859 @item Recursive variable `@var{xxx}' references itself (eventually).  Stop.
10860 This means you've defined a normal (recursive) @code{make} variable
10861 @var{xxx} that, when it's expanded, will refer to itself (@var{xxx}).
10862 This is not allowed; either use simply-expanded variables (@code{:=}) or
10863 use the append operator (@code{+=}).  @xref{Using Variables, ,How to Use
10864 Variables}.
10866 @item Unterminated variable reference.  Stop.
10867 This means you forgot to provide the proper closing parenthesis
10868 or brace in your variable or function reference.
10870 @item insufficient arguments to function `@var{xxx}'.  Stop.
10871 This means you haven't provided the requisite number of arguments for
10872 this function.  See the documentation of the function for a description
10873 of its arguments.  @xref{Functions, ,Functions for Transforming Text}.
10875 @item missing target pattern.  Stop.
10876 @itemx multiple target patterns.  Stop.
10877 @itemx target pattern contains no `%'.  Stop.
10878 @itemx mixed implicit and static pattern rules.  Stop.
10879 These are generated for malformed static pattern rules.  The first means
10880 there's no pattern in the target section of the rule; the second means
10881 there are multiple patterns in the target section; the third means
10882 the target doesn't contain a pattern character (@code{%}); and the
10883 fourth means that all three parts of the static pattern rule contain
10884 pattern characters (@code{%})--only the first two parts should.
10885 @xref{Static Usage, ,Syntax of Static Pattern Rules}.
10887 @item warning: -jN forced in submake: disabling jobserver mode.
10888 This warning and the next are generated if @code{make} detects error
10889 conditions related to parallel processing on systems where
10890 sub-@code{make}s can communicate (@pxref{Options/Recursion,
10891 ,Communicating Options to a Sub-@code{make}}).  This warning is
10892 generated if a recursive invocation of a @code{make} process is forced
10893 to have @samp{-j@var{N}} in its argument list (where @var{N} is greater
10894 than one).  This could happen, for example, if you set the @code{MAKE}
10895 environment variable to @samp{make -j2}.  In this case, the
10896 sub-@code{make} doesn't communicate with other @code{make} processes and
10897 will simply pretend it has two jobs of its own.
10899 @item warning: jobserver unavailable: using -j1.  Add `+' to parent make rule.
10900 In order for @code{make} processes to communicate, the parent will pass
10901 information to the child.  Since this could result in problems if the
10902 child process isn't actually a @code{make}, the parent will only do this
10903 if it thinks the child is a @code{make}.  The parent uses the normal
10904 algorithms to determine this (@pxref{MAKE Variable, ,How the @code{MAKE}
10905 Variable Works}).  If the makefile is constructed such that the parent
10906 doesn't know the child is a @code{make} process, then the child will
10907 receive only part of the information necessary.  In this case, the child
10908 will generate this warning message and proceed with its build in a
10909 sequential manner.
10911 @end table
10913 @node Complex Makefile, GNU Free Documentation License, Error Messages, Top
10914 @appendix Complex Makefile Example
10916 Here is the makefile for the GNU @code{tar} program.  This is a
10917 moderately complex makefile.
10919 Because it is the first target, the default goal is @samp{all}.  An
10920 interesting feature of this makefile is that @file{testpad.h} is a
10921 source file automatically created by the @code{testpad} program,
10922 itself compiled from @file{testpad.c}.
10924 If you type @samp{make} or @samp{make all}, then @code{make} creates
10925 the @file{tar} executable, the @file{rmt} daemon that provides
10926 remote tape access, and the @file{tar.info} Info file.
10928 If you type @samp{make install}, then @code{make} not only creates
10929 @file{tar}, @file{rmt}, and @file{tar.info}, but also installs
10930 them.
10932 If you type @samp{make clean}, then @code{make} removes the @samp{.o}
10933 files, and the @file{tar}, @file{rmt}, @file{testpad},
10934 @file{testpad.h}, and @file{core} files.
10936 If you type @samp{make distclean}, then @code{make} not only removes
10937 the same files as does @samp{make clean} but also the
10938 @file{TAGS}, @file{Makefile}, and @file{config.status} files.
10939 (Although it is not evident, this makefile (and
10940 @file{config.status}) is generated by the user with the
10941 @code{configure} program, which is provided in the @code{tar}
10942 distribution, but is not shown here.)
10944 If you type @samp{make realclean}, then @code{make} removes the same
10945 files as does @samp{make distclean} and also removes the Info files
10946 generated from @file{tar.texinfo}.
10948 In addition, there are targets @code{shar} and @code{dist} that create
10949 distribution kits.
10951 @example
10952 @group
10953 # Generated automatically from Makefile.in by configure.
10954 # Un*x Makefile for GNU tar program.
10955 # Copyright (C) 1991 Free Software Foundation, Inc.
10956 @end group
10958 @group
10959 # This program is free software; you can redistribute
10960 # it and/or modify it under the terms of the GNU
10961 # General Public License @dots{}
10962 @dots{}
10963 @dots{}
10964 @end group
10966 SHELL = /bin/sh
10968 #### Start of system configuration section. ####
10970 srcdir = .
10972 @group
10973 # If you use gcc, you should either run the
10974 # fixincludes script that comes with it or else use
10975 # gcc with the -traditional option.  Otherwise ioctl
10976 # calls will be compiled incorrectly on some systems.
10977 CC = gcc -O
10978 YACC = bison -y
10979 INSTALL = /usr/local/bin/install -c
10980 INSTALLDATA = /usr/local/bin/install -c -m 644
10981 @end group
10983 # Things you might add to DEFS:
10984 # -DSTDC_HEADERS        If you have ANSI C headers and
10985 #                       libraries.
10986 # -DPOSIX               If you have POSIX.1 headers and
10987 #                       libraries.
10988 # -DBSD42               If you have sys/dir.h (unless
10989 #                       you use -DPOSIX), sys/file.h,
10990 #                       and st_blocks in `struct stat'.
10991 # -DUSG                 If you have System V/ANSI C
10992 #                       string and memory functions
10993 #                       and headers, sys/sysmacros.h,
10994 #                       fcntl.h, getcwd, no valloc,
10995 #                       and ndir.h (unless
10996 #                       you use -DDIRENT).
10997 # -DNO_MEMORY_H         If USG or STDC_HEADERS but do not
10998 #                       include memory.h.
10999 # -DDIRENT              If USG and you have dirent.h
11000 #                       instead of ndir.h.
11001 # -DSIGTYPE=int         If your signal handlers
11002 #                       return int, not void.
11003 # -DNO_MTIO             If you lack sys/mtio.h
11004 #                       (magtape ioctls).
11005 # -DNO_REMOTE           If you do not have a remote shell
11006 #                       or rexec.
11007 # -DUSE_REXEC           To use rexec for remote tape
11008 #                       operations instead of
11009 #                       forking rsh or remsh.
11010 # -DVPRINTF_MISSING     If you lack vprintf function
11011 #                       (but have _doprnt).
11012 # -DDOPRNT_MISSING      If you lack _doprnt function.
11013 #                       Also need to define
11014 #                       -DVPRINTF_MISSING.
11015 # -DFTIME_MISSING       If you lack ftime system call.
11016 # -DSTRSTR_MISSING      If you lack strstr function.
11017 # -DVALLOC_MISSING      If you lack valloc function.
11018 # -DMKDIR_MISSING       If you lack mkdir and
11019 #                       rmdir system calls.
11020 # -DRENAME_MISSING      If you lack rename system call.
11021 # -DFTRUNCATE_MISSING   If you lack ftruncate
11022 #                       system call.
11023 # -DV7                  On Version 7 Unix (not
11024 #                       tested in a long time).
11025 # -DEMUL_OPEN3          If you lack a 3-argument version
11026 #                       of open, and want to emulate it
11027 #                       with system calls you do have.
11028 # -DNO_OPEN3            If you lack the 3-argument open
11029 #                       and want to disable the tar -k
11030 #                       option instead of emulating open.
11031 # -DXENIX               If you have sys/inode.h
11032 #                       and need it 94 to be included.
11034 DEFS =  -DSIGTYPE=int -DDIRENT -DSTRSTR_MISSING \
11035         -DVPRINTF_MISSING -DBSD42
11036 # Set this to rtapelib.o unless you defined NO_REMOTE,
11037 # in which case make it empty.
11038 RTAPELIB = rtapelib.o
11039 LIBS =
11040 DEF_AR_FILE = /dev/rmt8
11041 DEFBLOCKING = 20
11043 @group
11044 CDEBUG = -g
11045 CFLAGS = $(CDEBUG) -I. -I$(srcdir) $(DEFS) \
11046         -DDEF_AR_FILE=\"$(DEF_AR_FILE)\" \
11047         -DDEFBLOCKING=$(DEFBLOCKING)
11048 LDFLAGS = -g
11049 @end group
11051 @group
11052 prefix = /usr/local
11053 # Prefix for each installed program,
11054 # normally empty or `g'.
11055 binprefix =
11057 # The directory to install tar in.
11058 bindir = $(prefix)/bin
11060 # The directory to install the info files in.
11061 infodir = $(prefix)/info
11062 @end group
11064 #### End of system configuration section. ####
11066 SRC1 =  tar.c create.c extract.c buffer.c \
11067         getoldopt.c update.c gnu.c mangle.c
11068 SRC2 =  version.c list.c names.c diffarch.c \
11069         port.c wildmat.c getopt.c
11070 SRC3 =  getopt1.c regex.c getdate.y
11071 SRCS =  $(SRC1) $(SRC2) $(SRC3)
11072 OBJ1 =  tar.o create.o extract.o buffer.o \
11073         getoldopt.o update.o gnu.o mangle.o
11074 OBJ2 =  version.o list.o names.o diffarch.o \
11075         port.o wildmat.o getopt.o
11076 OBJ3 =  getopt1.o regex.o getdate.o $(RTAPELIB)
11077 OBJS =  $(OBJ1) $(OBJ2) $(OBJ3)
11078 @group
11079 AUX =   README COPYING ChangeLog Makefile.in  \
11080         makefile.pc configure configure.in \
11081         tar.texinfo tar.info* texinfo.tex \
11082         tar.h port.h open3.h getopt.h regex.h \
11083         rmt.h rmt.c rtapelib.c alloca.c \
11084         msd_dir.h msd_dir.c tcexparg.c \
11085         level-0 level-1 backup-specs testpad.c
11086 @end group
11088 .PHONY: all
11089 all:    tar rmt tar.info
11091 @group
11092 tar:    $(OBJS)
11093         $(CC) $(LDFLAGS) -o $@@ $(OBJS) $(LIBS)
11094 @end group
11096 @group
11097 rmt:    rmt.c
11098         $(CC) $(CFLAGS) $(LDFLAGS) -o $@@ rmt.c
11099 @end group
11101 @group
11102 tar.info: tar.texinfo
11103         makeinfo tar.texinfo
11104 @end group
11106 @group
11107 .PHONY: install
11108 install: all
11109         $(INSTALL) tar $(bindir)/$(binprefix)tar
11110         -test ! -f rmt || $(INSTALL) rmt /etc/rmt
11111         $(INSTALLDATA) $(srcdir)/tar.info* $(infodir)
11112 @end group
11114 @group
11115 $(OBJS): tar.h port.h testpad.h
11116 regex.o buffer.o tar.o: regex.h
11117 # getdate.y has 8 shift/reduce conflicts.
11118 @end group
11120 @group
11121 testpad.h: testpad
11122         ./testpad
11123 @end group
11125 @group
11126 testpad: testpad.o
11127         $(CC) -o $@@ testpad.o
11128 @end group
11130 @group
11131 TAGS:   $(SRCS)
11132         etags $(SRCS)
11133 @end group
11135 @group
11136 .PHONY: clean
11137 clean:
11138         rm -f *.o tar rmt testpad testpad.h core
11139 @end group
11141 @group
11142 .PHONY: distclean
11143 distclean: clean
11144         rm -f TAGS Makefile config.status
11145 @end group
11147 @group
11148 .PHONY: realclean
11149 realclean: distclean
11150         rm -f tar.info*
11151 @end group
11153 @group
11154 .PHONY: shar
11155 shar: $(SRCS) $(AUX)
11156         shar $(SRCS) $(AUX) | compress \
11157           > tar-`sed -e '/version_string/!d' \
11158                      -e 's/[^0-9.]*\([0-9.]*\).*/\1/' \
11159                      -e q
11160                      version.c`.shar.Z
11161 @end group
11163 @group
11164 .PHONY: dist
11165 dist: $(SRCS) $(AUX)
11166         echo tar-`sed \
11167              -e '/version_string/!d' \
11168              -e 's/[^0-9.]*\([0-9.]*\).*/\1/' \
11169              -e q
11170              version.c` > .fname
11171         -rm -rf `cat .fname`
11172         mkdir `cat .fname`
11173         ln $(SRCS) $(AUX) `cat .fname`
11174         tar chZf `cat .fname`.tar.Z `cat .fname`
11175         -rm -rf `cat .fname` .fname
11176 @end group
11178 @group
11179 tar.zoo: $(SRCS) $(AUX)
11180         -rm -rf tmp.dir
11181         -mkdir tmp.dir
11182         -rm tar.zoo
11183         for X in $(SRCS) $(AUX) ; do \
11184             echo $$X ; \
11185             sed 's/$$/^M/' $$X \
11186             > tmp.dir/$$X ; done
11187         cd tmp.dir ; zoo aM ../tar.zoo *
11188         -rm -rf tmp.dir
11189 @end group
11190 @end example
11192 @node GNU Free Documentation License, Concept Index, Complex Makefile, Top
11193 @appendixsec GNU Free Documentation License
11194 @cindex FDL, GNU Free Documentation License
11195 @include fdl.texi
11197 @node Concept Index, Name Index, GNU Free Documentation License, Top
11198 @unnumbered Index of Concepts
11200 @printindex cp
11202 @node Name Index,  , Concept Index, Top
11203 @unnumbered Index of Functions, Variables, & Directives
11205 @printindex fn
11207 @bye