* Fixups to the make man page
[make.git] / doc / make.texi
blob2fb79b404c2ac74f2667757419601c3505a4df31
1 \input texinfo                @c -*- Texinfo -*-
2 @c %**start of header
3 @setfilename make.info
5 @include version.texi
6 @set EDITION 0.71
7 @set RCSID $Id: make.texi,v 1.70 2011/02/21 07:30:12 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,
29 1996, 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005, 2006, 2007,
30 2008, 2009, 2010 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.3 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.  A recipe
528 may have more than one command, either on the same line or each on its
529 own line.  @strong{Please note:} you need to put a tab character at
530 the beginning of every recipe line!  This is an obscurity that catches
531 the unwary.  If you prefer to prefix your recipes with a character
532 other than tab, you can set the @code{.RECIPEPREFIX} variable to an
533 alternate character (@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{.RECIPEPREFIX} 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 recipe you have specified when the target file
638 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{.RECIPEPREFIX})---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 or 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} (or any
1183 prerequisites of any of the @var{filenames}) do not exist or cannot be
1184 remade.
1186 For compatibility with some other @code{make} implementations,
1187 @code{sinclude} is another name for @w{@code{-include}}.
1189 @node MAKEFILES Variable, Remaking Makefiles, Include, Makefiles
1190 @section The Variable @code{MAKEFILES}
1191 @cindex makefile, and @code{MAKEFILES} variable
1192 @cindex including (@code{MAKEFILES} variable)
1194 @vindex MAKEFILES
1195 If the environment variable @code{MAKEFILES} is defined, @code{make}
1196 considers its value as a list of names (separated by whitespace) of
1197 additional makefiles to be read before the others.  This works much
1198 like the @code{include} directive: various directories are searched
1199 for those files (@pxref{Include, ,Including Other Makefiles}).  In
1200 addition, the default goal is never taken from one of these makefiles
1201 (or any makefile included by them) and it is not an error if the files
1202 listed in @code{MAKEFILES} are not found.@refill
1204 @cindex recursion, and @code{MAKEFILES} variable
1205 The main use of @code{MAKEFILES} is in communication between recursive
1206 invocations of @code{make} (@pxref{Recursion, ,Recursive Use of
1207 @code{make}}).  It usually is not desirable to set the environment
1208 variable before a top-level invocation of @code{make}, because it is
1209 usually better not to mess with a makefile from outside.  However, if
1210 you are running @code{make} without a specific makefile, a makefile in
1211 @code{MAKEFILES} can do useful things to help the built-in implicit
1212 rules work better, such as defining search paths (@pxref{Directory Search}).
1214 Some users are tempted to set @code{MAKEFILES} in the environment
1215 automatically on login, and program makefiles to expect this to be done.
1216 This is a very bad idea, because such makefiles will fail to work if run by
1217 anyone else.  It is much better to write explicit @code{include} directives
1218 in the makefiles.  @xref{Include, , Including Other Makefiles}.
1220 @node Remaking Makefiles, Overriding Makefiles, MAKEFILES Variable, Makefiles
1221 @section How Makefiles Are Remade
1223 @cindex updating makefiles
1224 @cindex remaking makefiles
1225 @cindex makefile, remaking of
1226 Sometimes makefiles can be remade from other files, such as RCS or SCCS
1227 files.  If a makefile can be remade from other files, you probably want
1228 @code{make} to get an up-to-date version of the makefile to read in.
1230 To this end, after reading in all makefiles, @code{make} will consider
1231 each as a goal target and attempt to update it.  If a makefile has a
1232 rule which says how to update it (found either in that very makefile or
1233 in another one) or if an implicit rule applies to it (@pxref{Implicit
1234 Rules, ,Using Implicit Rules}), it will be updated if necessary.  After
1235 all makefiles have been checked, if any have actually been changed,
1236 @code{make} starts with a clean slate and reads all the makefiles over
1237 again.  (It will also attempt to update each of them over again, but
1238 normally this will not change them again, since they are already up to
1239 date.)@refill
1241 If you know that one or more of your makefiles cannot be remade and
1242 you want to keep @code{make} from performing an implicit rule search
1243 on them, perhaps for efficiency reasons, you can use any normal method
1244 of preventing implicit rule lookup to do so.  For example, you can
1245 write an explicit rule with the makefile as the target, and an empty
1246 recipe (@pxref{Empty Recipes, ,Using Empty Recipes}).
1248 If the makefiles specify a double-colon rule to remake a file with
1249 a recipe but no prerequisites, that file will always be remade
1250 (@pxref{Double-Colon}).  In the case of makefiles, a makefile that has a
1251 double-colon rule with a recipe but no prerequisites will be remade every
1252 time @code{make} is run, and then again after @code{make} starts over
1253 and reads the makefiles in again.  This would cause an infinite loop:
1254 @code{make} would constantly remake the makefile, and never do anything
1255 else.  So, to avoid this, @code{make} will @strong{not} attempt to
1256 remake makefiles which are specified as targets of a double-colon rule
1257 with a recipe but no prerequisites.@refill
1259 If you do not specify any makefiles to be read with @samp{-f} or
1260 @samp{--file} options, @code{make} will try the default makefile names;
1261 @pxref{Makefile Names, ,What Name to Give Your Makefile}.  Unlike
1262 makefiles explicitly requested with @samp{-f} or @samp{--file} options,
1263 @code{make} is not certain that these makefiles should exist.  However,
1264 if a default makefile does not exist but can be created by running
1265 @code{make} rules, you probably want the rules to be run so that the
1266 makefile can be used.
1268 Therefore, if none of the default makefiles exists, @code{make} will try
1269 to make each of them in the same order in which they are searched for
1270 (@pxref{Makefile Names, ,What Name to Give Your Makefile})
1271 until it succeeds in making one, or it runs out of names to try.  Note
1272 that it is not an error if @code{make} cannot find or make any makefile;
1273 a makefile is not always necessary.@refill
1275 When you use the @samp{-t} or @samp{--touch} option
1276 (@pxref{Instead of Execution, ,Instead of Executing Recipes}),
1277 you would not want to use an out-of-date makefile to decide which
1278 targets to touch.  So the @samp{-t} option has no effect on updating
1279 makefiles; they are really updated even if @samp{-t} is specified.
1280 Likewise, @samp{-q} (or @samp{--question}) and @samp{-n} (or
1281 @samp{--just-print}) do not prevent updating of makefiles, because an
1282 out-of-date makefile would result in the wrong output for other targets.
1283 Thus, @samp{make -f mfile -n foo} will update @file{mfile}, read it in,
1284 and then print the recipe to update @file{foo} and its prerequisites
1285 without running it.  The recipe printed for @file{foo} will be the one
1286 specified in the updated contents of @file{mfile}.
1288 However, on occasion you might actually wish to prevent updating of even
1289 the makefiles.  You can do this by specifying the makefiles as goals in
1290 the command line as well as specifying them as makefiles.  When the
1291 makefile name is specified explicitly as a goal, the options @samp{-t}
1292 and so on do apply to them.
1294 Thus, @samp{make -f mfile -n mfile foo} would read the makefile
1295 @file{mfile}, print the recipe needed to update it without actually
1296 running it, and then print the recipe needed to update @file{foo}
1297 without running that.  The recipe for @file{foo} will be the one
1298 specified by the existing contents of @file{mfile}.
1300 @node Overriding Makefiles, Reading Makefiles, Remaking Makefiles, Makefiles
1301 @section Overriding Part of Another Makefile
1303 @cindex overriding makefiles
1304 @cindex makefile, overriding
1305 Sometimes it is useful to have a makefile that is mostly just like
1306 another makefile.  You can often use the @samp{include} directive to
1307 include one in the other, and add more targets or variable definitions.
1308 However, it is illegal for two makefiles to give different recipes for
1309 the same target.  But there is another way.
1311 @cindex match-anything rule, used to override
1312 In the containing makefile (the one that wants to include the other),
1313 you can use a match-anything pattern rule to say that to remake any
1314 target that cannot be made from the information in the containing
1315 makefile, @code{make} should look in another makefile.
1316 @xref{Pattern Rules}, for more information on pattern rules.
1318 For example, if you have a makefile called @file{Makefile} that says how
1319 to make the target @samp{foo} (and other targets), you can write a
1320 makefile called @file{GNUmakefile} that contains:
1322 @example
1323 foo:
1324         frobnicate > foo
1326 %: force
1327         @@$(MAKE) -f Makefile $@@
1328 force: ;
1329 @end example
1331 If you say @samp{make foo}, @code{make} will find @file{GNUmakefile},
1332 read it, and see that to make @file{foo}, it needs to run the recipe
1333 @samp{frobnicate > foo}.  If you say @samp{make bar}, @code{make} will
1334 find no way to make @file{bar} in @file{GNUmakefile}, so it will use the
1335 recipe from the pattern rule: @samp{make -f Makefile bar}.  If
1336 @file{Makefile} provides a rule for updating @file{bar}, @code{make}
1337 will apply the rule.  And likewise for any other target that
1338 @file{GNUmakefile} does not say how to make.
1340 The way this works is that the pattern rule has a pattern of just
1341 @samp{%}, so it matches any target whatever.  The rule specifies a
1342 prerequisite @file{force}, to guarantee that the recipe will be run even
1343 if the target file already exists.  We give the @file{force} target an
1344 empty recipe to prevent @code{make} from searching for an implicit rule to
1345 build it---otherwise it would apply the same match-anything rule to
1346 @file{force} itself and create a prerequisite loop!
1348 @node Reading Makefiles,  Secondary Expansion, Overriding Makefiles, Makefiles
1349 @section How @code{make} Reads a Makefile
1350 @cindex reading makefiles
1351 @cindex makefile, parsing
1353 GNU @code{make} does its work in two distinct phases.  During the first
1354 phase it reads all the makefiles, included makefiles, etc. and
1355 internalizes all the variables and their values, implicit and explicit
1356 rules, and constructs a dependency graph of all the targets and their
1357 prerequisites.  During the second phase, @code{make} uses these internal
1358 structures to determine what targets will need to be rebuilt and to
1359 invoke the rules necessary to do so.
1361 It's important to understand this two-phase approach because it has a
1362 direct impact on how variable and function expansion happens; this is
1363 often a source of some confusion when writing makefiles.  Here we will
1364 present a summary of the phases in which expansion happens for different
1365 constructs within the makefile.  We say that expansion is
1366 @dfn{immediate} if it happens during the first phase: in this case
1367 @code{make} will expand any variables or functions in that section of a
1368 construct as the makefile is parsed.  We say that expansion is
1369 @dfn{deferred} if expansion is not performed immediately.  Expansion of
1370 a deferred construct is not performed until either the construct appears
1371 later in an immediate context, or until the second phase.
1373 You may not be familiar with some of these constructs yet.  You can
1374 reference this section as you become familiar with them, in later
1375 chapters.
1377 @subheading Variable Assignment
1378 @cindex +=, expansion
1379 @cindex =, expansion
1380 @cindex ?=, expansion
1381 @cindex +=, expansion
1382 @cindex define, expansion
1384 Variable definitions are parsed as follows:
1386 @example
1387 @var{immediate} = @var{deferred}
1388 @var{immediate} ?= @var{deferred}
1389 @var{immediate} := @var{immediate}
1390 @var{immediate} += @var{deferred} or @var{immediate}
1392 define @var{immediate}
1393   @var{deferred}
1394 endef
1396 define @var{immediate} =
1397   @var{deferred}
1398 endef
1400 define @var{immediate} ?=
1401   @var{deferred}
1402 endef
1404 define @var{immediate} :=
1405   @var{immediate}
1406 endef
1408 define @var{immediate} +=
1409   @var{deferred} or @var{immediate}
1410 endef
1411 @end example
1413 For the append operator, @samp{+=}, the right-hand side is considered
1414 immediate if the variable was previously set as a simple variable
1415 (@samp{:=}), and deferred otherwise.
1417 @subheading Conditional Directives
1418 @cindex ifdef, expansion
1419 @cindex ifeq, expansion
1420 @cindex ifndef, expansion
1421 @cindex ifneq, expansion
1423 Conditional directives are parsed immediately.  This means, for
1424 example, that automatic variables cannot be used in conditional
1425 directives, as automatic variables are not set until the recipe for
1426 that rule is invoked.  If you need to use automatic variables in a
1427 conditional directive you @emph{must} move the condition into the
1428 recipe and use shell conditional syntax instead.
1430 @subheading Rule Definition
1431 @cindex target, expansion
1432 @cindex prerequisite, expansion
1433 @cindex implicit rule, expansion
1434 @cindex pattern rule, expansion
1435 @cindex explicit rule, expansion
1437 A rule is always expanded the same way, regardless of the form:
1439 @example
1440 @var{immediate} : @var{immediate} ; @var{deferred}
1441         @var{deferred}
1442 @end example
1444 That is, the target and prerequisite sections are expanded immediately,
1445 and the recipe used to construct the target is always deferred.  This
1446 general rule is true for explicit rules, pattern rules, suffix rules,
1447 static pattern rules, and simple prerequisite definitions.
1449 @node Secondary Expansion, , Reading Makefiles, Makefiles
1450 @section Secondary Expansion
1451 @cindex secondary expansion
1452 @cindex expansion, secondary
1454 @findex .SECONDEXPANSION
1455 In the previous section we learned that GNU @code{make} works in two
1456 distinct phases: a read-in phase and a target-update phase
1457 (@pxref{Reading Makefiles, , How @code{make} Reads a Makefile}).  GNU
1458 make also has the ability to enable a @emph{second expansion} of the
1459 prerequisites (only) for some or all targets defined in the makefile.
1460 In order for this second expansion to occur, the special target
1461 @code{.SECONDEXPANSION} must be defined before the first prerequisite
1462 list that makes use of this feature.
1464 If that special target is defined then in between the two phases
1465 mentioned above, right at the end of the read-in phase, all the
1466 prerequisites of the targets defined after the special target are
1467 expanded a @emph{second time}.  In most circumstances this secondary
1468 expansion will have no effect, since all variable and function
1469 references will have been expanded during the initial parsing of the
1470 makefiles.  In order to take advantage of the secondary expansion
1471 phase of the parser, then, it's necessary to @emph{escape} the
1472 variable or function reference in the makefile.  In this case the
1473 first expansion merely un-escapes the reference but doesn't expand it,
1474 and expansion is left to the secondary expansion phase.  For example,
1475 consider this makefile:
1477 @example
1478 .SECONDEXPANSION:
1479 ONEVAR = onefile
1480 TWOVAR = twofile
1481 myfile: $(ONEVAR) $$(TWOVAR)
1482 @end example
1484 After the first expansion phase the prerequisites list of the
1485 @file{myfile} target will be @code{onefile} and @code{$(TWOVAR)}; the
1486 first (unescaped) variable reference to @var{ONEVAR} is expanded,
1487 while the second (escaped) variable reference is simply unescaped,
1488 without being recognized as a variable reference.  Now during the
1489 secondary expansion the first word is expanded again but since it
1490 contains no variable or function references it remains the value
1491 @file{onefile}, while the second word is now a normal reference to the
1492 variable @var{TWOVAR}, which is expanded to the value @file{twofile}.
1493 The final result is that there are two prerequisites, @file{onefile}
1494 and @file{twofile}.
1496 Obviously, this is not a very interesting case since the same result
1497 could more easily have been achieved simply by having both variables
1498 appear, unescaped, in the prerequisites list.  One difference becomes
1499 apparent if the variables are reset; consider this example:
1501 @example
1502 .SECONDEXPANSION:
1503 AVAR = top
1504 onefile: $(AVAR)
1505 twofile: $$(AVAR)
1506 AVAR = bottom
1507 @end example
1509 Here the prerequisite of @file{onefile} will be expanded immediately,
1510 and resolve to the value @file{top}, while the prerequisite of
1511 @file{twofile} will not be full expanded until the secondary expansion
1512 and yield a value of @file{bottom}.
1514 This is marginally more exciting, but the true power of this feature
1515 only becomes apparent when you discover that secondary expansions
1516 always take place within the scope of the automatic variables for that
1517 target.  This means that you can use variables such as @code{$@@},
1518 @code{$*}, etc. during the second expansion and they will have their
1519 expected values, just as in the recipe.  All you have to do is defer
1520 the expansion by escaping the @code{$}.  Also, secondary expansion
1521 occurs for both explicit and implicit (pattern) rules.  Knowing this,
1522 the possible uses for this feature increase dramatically.  For
1523 example:
1525 @example
1526 .SECONDEXPANSION:
1527 main_OBJS := main.o try.o test.o
1528 lib_OBJS := lib.o api.o
1530 main lib: $$($$@@_OBJS)
1531 @end example
1533 Here, after the initial expansion the prerequisites of both the
1534 @file{main} and @file{lib} targets will be @code{$($@@_OBJS)}.  During
1535 the secondary expansion, the @code{$@@} variable is set to the name of
1536 the target and so the expansion for the @file{main} target will yield
1537 @code{$(main_OBJS)}, or @code{main.o try.o test.o}, while the
1538 secondary expansion for the @file{lib} target will yield
1539 @code{$(lib_OBJS)}, or @code{lib.o api.o}.
1541 You can also mix in functions here, as long as they are properly escaped:
1543 @example
1544 main_SRCS := main.c try.c test.c
1545 lib_SRCS := lib.c api.c
1547 .SECONDEXPANSION:
1548 main lib: $$(patsubst %.c,%.o,$$($$@@_SRCS))
1549 @end example
1551 This version allows users to specify source files rather than object
1552 files, but gives the same resulting prerequisites list as the previous
1553 example.
1555 Evaluation of automatic variables during the secondary expansion
1556 phase, especially of the target name variable @code{$$@@}, behaves
1557 similarly to evaluation within recipes.  However, there are some
1558 subtle differences and ``corner cases'' which come into play for the
1559 different types of rule definitions that @code{make} understands.  The
1560 subtleties of using the different automatic variables are described
1561 below.
1563 @subheading Secondary Expansion of Explicit Rules
1564 @cindex secondary expansion and explicit rules
1565 @cindex explicit rules, secondary expansion of
1567 During the secondary expansion of explicit rules, @code{$$@@} and
1568 @code{$$%} evaluate, respectively, to the file name of the target and,
1569 when the target is an archive member, the target member name.  The
1570 @code{$$<} variable evaluates to the first prerequisite in the first
1571 rule for this target.  @code{$$^} and @code{$$+} evaluate to the list
1572 of all prerequisites of rules @emph{that have already appeared} for
1573 the same target (@code{$$+} with repetitions and @code{$$^}
1574 without).  The following example will help illustrate these behaviors:
1576 @example
1577 .SECONDEXPANSION:
1579 foo: foo.1 bar.1 $$< $$^ $$+    # line #1
1581 foo: foo.2 bar.2 $$< $$^ $$+    # line #2
1583 foo: foo.3 bar.3 $$< $$^ $$+    # line #3
1584 @end example
1586 In the first prerequisite list, all three variables (@code{$$<},
1587 @code{$$^}, and @code{$$+}) expand to the empty string.  In the
1588 second, they will have values @code{foo.1}, @code{foo.1 bar.1}, and
1589 @code{foo.1 bar.1} respectively.  In the third they will have values
1590 @code{foo.1}, @code{foo.1 bar.1 foo.2 bar.2}, and @code{foo.1 bar.1
1591 foo.2 bar.2 foo.1 foo.1 bar.1 foo.1 bar.1} respectively.
1593 Rules undergo secondary expansion in makefile order, except that
1594 the rule with the recipe is always evaluated last.
1596 The variables @code{$$?} and @code{$$*} are not available and expand
1597 to the empty string.
1599 @subheading Secondary Expansion of Static Pattern Rules
1600 @cindex secondary expansion and static pattern rules
1601 @cindex static pattern rules, secondary expansion of
1603 Rules for secondary expansion of static pattern rules are identical to
1604 those for explicit rules, above, with one exception: for static
1605 pattern rules the @code{$$*} variable is set to the pattern stem.  As
1606 with explicit rules, @code{$$?} is not available and expands to the
1607 empty string.
1609 @subheading Secondary Expansion of Implicit Rules
1610 @cindex secondary expansion and implicit rules
1611 @cindex implicit rules, secondary expansion of
1613 As @code{make} searches for an implicit rule, it substitutes the stem
1614 and then performs secondary expansion for every rule with a matching
1615 target pattern.  The value of the automatic variables is derived in
1616 the same fashion as for static pattern rules.  As an example:
1618 @example
1619 .SECONDEXPANSION:
1621 foo: bar
1623 foo foz: fo%: bo%
1625 %oo: $$< $$^ $$+ $$*
1626 @end example
1628 When the implicit rule is tried for target @file{foo}, @code{$$<}
1629 expands to @file{bar}, @code{$$^} expands to @file{bar boo},
1630 @code{$$+} also expands to @file{bar boo}, and @code{$$*} expands to
1631 @file{f}.
1633 Note that the directory prefix (D), as described in @ref{Implicit Rule
1634 Search, ,Implicit Rule Search Algorithm}, is appended (after
1635 expansion) to all the patterns in the prerequisites list.  As an
1636 example:@refill
1638 @example
1639 .SECONDEXPANSION:
1641 /tmp/foo.o:
1643 %.o: $$(addsuffix /%.c,foo bar) foo.h
1644         @@echo $^
1645 @end example
1647 The prerequisite list printed, after the secondary expansion and
1648 directory prefix reconstruction, will be @file{/tmp/foo/foo.c
1649 /tmp/bar/foo.c foo.h}.  If you are not interested in this
1650 reconstruction, you can use @code{$$*} instead of @code{%} in the
1651 prerequisites list.
1653 @node Rules, Recipes, Makefiles, Top
1654 @chapter Writing Rules
1655 @cindex writing rules
1656 @cindex rule, how to write
1657 @cindex target
1658 @cindex prerequisite
1660 A @dfn{rule} appears in the makefile and says when and how to remake
1661 certain files, called the rule's @dfn{targets} (most often only one per rule).
1662 It lists the other files that are the @dfn{prerequisites} of the target, and
1663 the @dfn{recipe} to use to create or update the target.
1665 @cindex default goal
1666 @cindex goal, default
1667 The order of rules is not significant, except for determining the
1668 @dfn{default goal}: the target for @code{make} to consider, if you do
1669 not otherwise specify one.  The default goal is the target of the first
1670 rule in the first makefile.  If the first rule has multiple targets,
1671 only the first target is taken as the default.  There are two
1672 exceptions: a target starting with a period is not a default unless it
1673 contains one or more slashes, @samp{/}, as well; and, a target that
1674 defines a pattern rule has no effect on the default goal.
1675 (@xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.)
1677 Therefore, we usually write the makefile so that the first rule is the
1678 one for compiling the entire program or all the programs described by
1679 the makefile (often with a target called @samp{all}).
1680 @xref{Goals, ,Arguments to Specify the Goals}.
1682 @menu
1683 * Rule Example::                An example explained.
1684 * Rule Syntax::                 General syntax explained.
1685 * Prerequisite Types::          There are two types of prerequisites.
1686 * Wildcards::                   Using wildcard characters such as `*'.
1687 * Directory Search::            Searching other directories for source files.
1688 * Phony Targets::               Using a target that is not a real file's name.
1689 * Force Targets::               You can use a target without recipes
1690                                   or prerequisites to mark other targets
1691                                   as phony.
1692 * Empty Targets::               When only the date matters and the
1693                                   files are empty.
1694 * Special Targets::             Targets with special built-in meanings.
1695 * Multiple Targets::            When to make use of several targets in a rule.
1696 * Multiple Rules::              How to use several rules with the same target.
1697 * Static Pattern::              Static pattern rules apply to multiple targets
1698                                   and can vary the prerequisites according to
1699                                   the target name.
1700 * Double-Colon::                How to use a special kind of rule to allow
1701                                   several independent rules for one target.
1702 * Automatic Prerequisites::     How to automatically generate rules giving
1703                                   prerequisites from source files themselves.
1704 @end menu
1706 @ifnottex
1707 @node Rule Example, Rule Syntax, Rules, Rules
1708 @section Rule Example
1710 Here is an example of a rule:
1712 @example
1713 foo.o : foo.c defs.h       # module for twiddling the frobs
1714         cc -c -g foo.c
1715 @end example
1717 Its target is @file{foo.o} and its prerequisites are @file{foo.c} and
1718 @file{defs.h}.  It has one command in the recipe: @samp{cc -c -g foo.c}.
1719 The recipe starts with a tab to identify it as a recipe.
1721 This rule says two things:
1723 @itemize @bullet
1724 @item
1725 How to decide whether @file{foo.o} is out of date: it is out of date
1726 if it does not exist, or if either @file{foo.c} or @file{defs.h} is
1727 more recent than it.
1729 @item
1730 How to update the file @file{foo.o}: by running @code{cc} as stated.
1731 The recipe does not explicitly mention @file{defs.h}, but we presume
1732 that @file{foo.c} includes it, and that that is why @file{defs.h} was
1733 added to the prerequisites.
1734 @end itemize
1735 @end ifnottex
1737 @node Rule Syntax, Prerequisite Types, Rule Example, Rules
1738 @section Rule Syntax
1740 @cindex rule syntax
1741 @cindex syntax of rules
1742 In general, a rule looks like this:
1744 @example
1745 @var{targets} : @var{prerequisites}
1746         @var{recipe}
1747         @dots{}
1748 @end example
1750 @noindent
1751 or like this:
1753 @example
1754 @var{targets} : @var{prerequisites} ; @var{recipe}
1755         @var{recipe}
1756         @dots{}
1757 @end example
1759 @cindex targets
1760 @cindex rule targets
1761 The @var{targets} are file names, separated by spaces.  Wildcard
1762 characters may be used (@pxref{Wildcards, ,Using Wildcard Characters
1763 in File Names}) and a name of the form @file{@var{a}(@var{m})}
1764 represents member @var{m} in archive file @var{a}
1765 (@pxref{Archive Members, ,Archive Members as Targets}).
1766 Usually there is only one
1767 target per rule, but occasionally there is a reason to have more
1768 (@pxref{Multiple Targets, , Multiple Targets in a Rule}).@refill
1770 @cindex recipes
1771 @cindex tab character (in commands)
1772 The @var{recipe} lines start with a tab character (or the first
1773 character in the value of the @code{.RECIPEPREFIX} variable;
1774 @pxref{Special Variables}).  The first recipe line may appear on the line
1775 after the prerequisites, with a tab character, or may appear on the
1776 same line, with a semicolon.  Either way, the effect is the same.
1777 There are other differences in the syntax of recipes.
1778 @xref{Recipes, ,Writing Recipes in Rules}.
1780 @cindex dollar sign (@code{$}), in rules
1781 @cindex @code{$}, in rules
1782 @cindex rules, and @code{$}
1783 Because dollar signs are used to start @code{make} variable
1784 references, if you really want a dollar sign in a target or
1785 prerequisite you must write two of them, @samp{$$} (@pxref{Using
1786 Variables, ,How to Use Variables}).  If you have enabled secondary
1787 expansion (@pxref{Secondary Expansion}) and you want a literal dollar
1788 sign in the prerequisites list, you must actually write @emph{four}
1789 dollar signs (@samp{$$$$}).
1791 You may split a long line by inserting a backslash followed by a
1792 newline, but this is not required, as @code{make} places no limit on
1793 the length of a line in a makefile.
1795 A rule tells @code{make} two things: when the targets are out of date,
1796 and how to update them when necessary.
1798 @cindex prerequisites
1799 @cindex rule prerequisites
1800 The criterion for being out of date is specified in terms of the
1801 @var{prerequisites}, which consist of file names separated by spaces.
1802 (Wildcards and archive members (@pxref{Archives}) are allowed here too.)
1803 A target is out of date if it does not exist or if it is older than any
1804 of the prerequisites (by comparison of last-modification times).  The
1805 idea is that the contents of the target file are computed based on
1806 information in the prerequisites, so if any of the prerequisites changes,
1807 the contents of the existing target file are no longer necessarily
1808 valid.
1810 How to update is specified by a @var{recipe}.  This is one or more
1811 lines to be executed by the shell (normally @samp{sh}), but with some
1812 extra features (@pxref{Recipes, ,Writing Recipes in Rules}).
1814 @node Prerequisite Types, Wildcards, Rule Syntax, Rules
1815 @comment  node-name,  next,  previous,  up
1816 @section Types of Prerequisites
1817 @cindex prerequisite types
1818 @cindex types of prerequisites
1820 @cindex prerequisites, normal
1821 @cindex normal prerequisites
1822 @cindex prerequisites, order-only
1823 @cindex order-only prerequisites
1824 There are actually two different types of prerequisites understood by
1825 GNU @code{make}: normal prerequisites such as described in the
1826 previous section, and @dfn{order-only} prerequisites.  A normal
1827 prerequisite makes two statements: first, it imposes an order in which
1828 recipes will be invoked: the recipes for all prerequisites of a target
1829 will be completed before the recipe for the target is run.  Second, it
1830 imposes a dependency relationship: if any prerequisite is newer than
1831 the target, then the target is considered out-of-date and must be
1832 rebuilt.
1834 Normally, this is exactly what you want: if a target's prerequisite is
1835 updated, then the target should also be updated.
1837 Occasionally, however, you have a situation where you want to impose a
1838 specific ordering on the rules to be invoked @emph{without} forcing
1839 the target to be updated if one of those rules is executed.  In that
1840 case, you want to define @dfn{order-only} prerequisites.  Order-only
1841 prerequisites can be specified by placing a pipe symbol (@code{|})
1842 in the prerequisites list: any prerequisites to the left of the pipe
1843 symbol are normal; any prerequisites to the right are order-only:
1845 @example
1846 @var{targets} : @var{normal-prerequisites} | @var{order-only-prerequisites}
1847 @end example
1849 The normal prerequisites section may of course be empty.  Also, you
1850 may still declare multiple lines of prerequisites for the same target:
1851 they are appended appropriately (normal prerequisites are appended to
1852 the list of normal prerequisites; order-only prerequisites are
1853 appended to the list of order-only prerequisites).  Note that if you
1854 declare the same file to be both a normal and an order-only
1855 prerequisite, the normal prerequisite takes precedence (since they
1856 have a strict superset of the behavior of an order-only prerequisite).
1858 Consider an example where your targets are to be placed in a separate
1859 directory, and that directory might not exist before @code{make} is
1860 run.  In this situation, you want the directory to be created before
1861 any targets are placed into it but, because the timestamps on
1862 directories change whenever a file is added, removed, or renamed, we
1863 certainly don't want to rebuild all the targets whenever the
1864 directory's timestamp changes.  One way to manage this is with
1865 order-only prerequisites: make the directory an order-only
1866 prerequisite on all the targets:
1868 @example
1869 OBJDIR := objdir
1870 OBJS := $(addprefix $(OBJDIR)/,foo.o bar.o baz.o)
1872 $(OBJDIR)/%.o : %.c
1873         $(COMPILE.c) $(OUTPUT_OPTION) $<
1875 all: $(OBJS)
1877 $(OBJS): | $(OBJDIR)
1879 $(OBJDIR):
1880         mkdir $(OBJDIR)
1881 @end example
1883 Now the rule to create the @file{objdir} directory will be run, if
1884 needed, before any @samp{.o} is built, but no @samp{.o} will be built
1885 because the @file{objdir} directory timestamp changed.
1887 @node Wildcards, Directory Search, Prerequisite Types, Rules
1888 @section Using Wildcard Characters in File Names
1889 @cindex wildcard
1890 @cindex file name with wildcards
1891 @cindex globbing (wildcards)
1893 @cindex @code{*} (wildcard character)
1894 @cindex @code{?} (wildcard character)
1895 @cindex @code{[@dots{}]} (wildcard characters)
1896 A single file name can specify many files using @dfn{wildcard characters}.
1897 The wildcard characters in @code{make} are @samp{*}, @samp{?} and
1898 @samp{[@dots{}]}, the same as in the Bourne shell.  For example, @file{*.c}
1899 specifies a list of all the files (in the working directory) whose names
1900 end in @samp{.c}.@refill
1902 @cindex @code{~} (tilde)
1903 @cindex tilde (@code{~})
1904 @cindex home directory
1905 The character @samp{~} at the beginning of a file name also has special
1906 significance.  If alone, or followed by a slash, it represents your home
1907 directory.  For example @file{~/bin} expands to @file{/home/you/bin}.
1908 If the @samp{~} is followed by a word, the string represents the home
1909 directory of the user named by that word.  For example @file{~john/bin}
1910 expands to @file{/home/john/bin}.  On systems which don't have a home
1911 directory for each user (such as MS-DOS or MS-Windows), this
1912 functionality can be simulated by setting the environment variable
1913 @var{HOME}.@refill
1915 Wildcard expansion is performed by @code{make} automatically in
1916 targets and in prerequisites.  In recipes, the shell is responsible
1917 for wildcard expansion.  In other contexts, wildcard expansion happens
1918 only if you request it explicitly with the @code{wildcard} function.
1920 The special significance of a wildcard character can be turned off by
1921 preceding it with a backslash.  Thus, @file{foo\*bar} would refer to a
1922 specific file whose name consists of @samp{foo}, an asterisk, and
1923 @samp{bar}.@refill
1925 @menu
1926 * Wildcard Examples::           Several examples
1927 * Wildcard Pitfall::            Problems to avoid.
1928 * Wildcard Function::           How to cause wildcard expansion where
1929                                   it does not normally take place.
1930 @end menu
1932 @node Wildcard Examples, Wildcard Pitfall, Wildcards, Wildcards
1933 @subsection Wildcard Examples
1935 Wildcards can be used in the recipe of a rule, where they are expanded
1936 by the shell.  For example, here is a rule to delete all the object files:
1938 @example
1939 @group
1940 clean:
1941         rm -f *.o
1942 @end group
1943 @end example
1944 @cindex @code{rm} (shell command)
1946 Wildcards are also useful in the prerequisites of a rule.  With the
1947 following rule in the makefile, @samp{make print} will print all the
1948 @samp{.c} files that have changed since the last time you printed them:
1950 @example
1951 print: *.c
1952         lpr -p $?
1953         touch print
1954 @end example
1956 @cindex @code{print} target
1957 @cindex @code{lpr} (shell command)
1958 @cindex @code{touch} (shell command)
1959 @noindent
1960 This rule uses @file{print} as an empty target file; see @ref{Empty
1961 Targets, ,Empty Target Files to Record Events}.  (The automatic variable
1962 @samp{$?} is used to print only those files that have changed; see
1963 @ref{Automatic Variables}.)@refill
1965 Wildcard expansion does not happen when you define a variable.  Thus, if
1966 you write this:
1968 @example
1969 objects = *.o
1970 @end example
1972 @noindent
1973 then the value of the variable @code{objects} is the actual string
1974 @samp{*.o}.  However, if you use the value of @code{objects} in a
1975 target or prerequisite, wildcard expansion will take place there.  If
1976 you use the value of @code{objects} in a recipe, the shell may perform
1977 wildcard expansion when the recipe runs.  To set @code{objects} to the
1978 expansion, instead use:
1980 @example
1981 objects := $(wildcard *.o)
1982 @end example
1984 @noindent
1985 @xref{Wildcard Function}.
1987 @node Wildcard Pitfall, Wildcard Function, Wildcard Examples, Wildcards
1988 @subsection Pitfalls of Using Wildcards
1989 @cindex wildcard pitfalls
1990 @cindex pitfalls of wildcards
1991 @cindex mistakes with wildcards
1992 @cindex errors with wildcards
1993 @cindex problems with wildcards
1995 Now here is an example of a naive way of using wildcard expansion, that
1996 does not do what you would intend.  Suppose you would like to say that the
1997 executable file @file{foo} is made from all the object files in the
1998 directory, and you write this:
2000 @example
2001 objects = *.o
2003 foo : $(objects)
2004         cc -o foo $(CFLAGS) $(objects)
2005 @end example
2007 @noindent
2008 The value of @code{objects} is the actual string @samp{*.o}.  Wildcard
2009 expansion happens in the rule for @file{foo}, so that each @emph{existing}
2010 @samp{.o} file becomes a prerequisite of @file{foo} and will be recompiled if
2011 necessary.
2013 But what if you delete all the @samp{.o} files?  When a wildcard matches
2014 no files, it is left as it is, so then @file{foo} will depend on the
2015 oddly-named file @file{*.o}.  Since no such file is likely to exist,
2016 @code{make} will give you an error saying it cannot figure out how to
2017 make @file{*.o}.  This is not what you want!
2019 Actually it is possible to obtain the desired result with wildcard
2020 expansion, but you need more sophisticated techniques, including the
2021 @code{wildcard} function and string substitution.
2022 @ifnottex
2023 @xref{Wildcard Function, ,The Function @code{wildcard}}.
2024 @end ifnottex
2025 @iftex
2026 These are described in the following section.
2027 @end iftex
2029 @cindex wildcards and MS-DOS/MS-Windows backslashes
2030 @cindex backslashes in pathnames and wildcard expansion
2032 Microsoft operating systems (MS-DOS and MS-Windows) use backslashes to
2033 separate directories in pathnames, like so:
2035 @example
2036   c:\foo\bar\baz.c
2037 @end example
2039 This is equivalent to the Unix-style @file{c:/foo/bar/baz.c} (the
2040 @file{c:} part is the so-called drive letter).  When @code{make} runs on
2041 these systems, it supports backslashes as well as the Unix-style forward
2042 slashes in pathnames.  However, this support does @emph{not} include the
2043 wildcard expansion, where backslash is a quote character.  Therefore,
2044 you @emph{must} use Unix-style slashes in these cases.
2047 @node Wildcard Function,  , Wildcard Pitfall, Wildcards
2048 @subsection The Function @code{wildcard}
2049 @findex wildcard
2051 Wildcard expansion happens automatically in rules.  But wildcard expansion
2052 does not normally take place when a variable is set, or inside the
2053 arguments of a function.  If you want to do wildcard expansion in such
2054 places, you need to use the @code{wildcard} function, like this:
2056 @example
2057 $(wildcard @var{pattern}@dots{})
2058 @end example
2060 @noindent
2061 This string, used anywhere in a makefile, is replaced by a
2062 space-separated list of names of existing files that match one of the
2063 given file name patterns.  If no existing file name matches a pattern,
2064 then that pattern is omitted from the output of the @code{wildcard}
2065 function.  Note that this is different from how unmatched wildcards
2066 behave in rules, where they are used verbatim rather than ignored
2067 (@pxref{Wildcard Pitfall}).
2069 One use of the @code{wildcard} function is to get a list of all the C source
2070 files in a directory, like this:
2072 @example
2073 $(wildcard *.c)
2074 @end example
2076 We can change the list of C source files into a list of object files by
2077 replacing the @samp{.c} suffix with @samp{.o} in the result, like this:
2079 @example
2080 $(patsubst %.c,%.o,$(wildcard *.c))
2081 @end example
2083 @noindent
2084 (Here we have used another function, @code{patsubst}.
2085 @xref{Text Functions, ,Functions for String Substitution and Analysis}.)@refill
2087 Thus, a makefile to compile all C source files in the directory and then
2088 link them together could be written as follows:
2090 @example
2091 objects := $(patsubst %.c,%.o,$(wildcard *.c))
2093 foo : $(objects)
2094         cc -o foo $(objects)
2095 @end example
2097 @noindent
2098 (This takes advantage of the implicit rule for compiling C programs, so
2099 there is no need to write explicit rules for compiling the files.
2100 @xref{Flavors, ,The Two Flavors of Variables}, for an explanation of
2101 @samp{:=}, which is a variant of @samp{=}.)
2103 @node Directory Search, Phony Targets, Wildcards, Rules
2104 @section Searching Directories for Prerequisites
2105 @vindex VPATH
2106 @findex vpath
2107 @cindex vpath
2108 @cindex search path for prerequisites (@code{VPATH})
2109 @cindex directory search (@code{VPATH})
2111 For large systems, it is often desirable to put sources in a separate
2112 directory from the binaries.  The @dfn{directory search} features of
2113 @code{make} facilitate this by searching several directories
2114 automatically to find a prerequisite.  When you redistribute the files
2115 among directories, you do not need to change the individual rules,
2116 just the search paths.
2118 @menu
2119 * General Search::              Specifying a search path that applies
2120                                   to every prerequisite.
2121 * Selective Search::            Specifying a search path
2122                                   for a specified class of names.
2123 * Search Algorithm::            When and how search paths are applied.
2124 * Recipes/Search::             How to write recipes that work together
2125                                   with search paths.
2126 * Implicit/Search::             How search paths affect implicit rules.
2127 * Libraries/Search::            Directory search for link libraries.
2128 @end menu
2130 @node General Search, Selective Search, Directory Search, Directory Search
2131 @subsection @code{VPATH}: Search Path for All Prerequisites
2132 @vindex VPATH
2134 The value of the @code{make} variable @code{VPATH} specifies a list of
2135 directories that @code{make} should search.  Most often, the
2136 directories are expected to contain prerequisite files that are not in the
2137 current directory; however, @code{make} uses @code{VPATH} as a search
2138 list for both prerequisites and targets of rules.
2140 Thus, if a file that is listed as a target or prerequisite does not exist
2141 in the current directory, @code{make} searches the directories listed in
2142 @code{VPATH} for a file with that name.  If a file is found in one of
2143 them, that file may become the prerequisite (see below).  Rules may then
2144 specify the names of files in the prerequisite list as if they all
2145 existed in the current directory.  @xref{Recipes/Search, ,Writing Recipes with Directory Search}.
2147 In the @code{VPATH} variable, directory names are separated by colons or
2148 blanks.  The order in which directories are listed is the order followed
2149 by @code{make} in its search.  (On MS-DOS and MS-Windows, semi-colons
2150 are used as separators of directory names in @code{VPATH}, since the
2151 colon can be used in the pathname itself, after the drive letter.)
2153 For example,
2155 @example
2156 VPATH = src:../headers
2157 @end example
2159 @noindent
2160 specifies a path containing two directories, @file{src} and
2161 @file{../headers}, which @code{make} searches in that order.
2163 With this value of @code{VPATH}, the following rule,
2165 @example
2166 foo.o : foo.c
2167 @end example
2169 @noindent
2170 is interpreted as if it were written like this:
2172 @example
2173 foo.o : src/foo.c
2174 @end example
2176 @noindent
2177 assuming the file @file{foo.c} does not exist in the current directory but
2178 is found in the directory @file{src}.
2180 @node Selective Search, Search Algorithm, General Search, Directory Search
2181 @subsection The @code{vpath} Directive
2182 @findex vpath
2184 Similar to the @code{VPATH} variable, but more selective, is the
2185 @code{vpath} directive (note lower case), which allows you to specify a
2186 search path for a particular class of file names: those that match a
2187 particular pattern.  Thus you can supply certain search directories for
2188 one class of file names and other directories (or none) for other file
2189 names.
2191 There are three forms of the @code{vpath} directive:
2193 @table @code
2194 @item vpath @var{pattern} @var{directories}
2195 Specify the search path @var{directories} for file names that match
2196 @var{pattern}.
2198 The search path, @var{directories}, is a list of directories to be
2199 searched, separated by colons (semi-colons on MS-DOS and MS-Windows) or
2200 blanks, just like the search path used in the @code{VPATH} variable.
2202 @item vpath @var{pattern}
2203 Clear out the search path associated with @var{pattern}.
2205 @c Extra blank line makes sure this gets two lines.
2206 @item vpath
2208 Clear all search paths previously specified with @code{vpath} directives.
2209 @end table
2211 A @code{vpath} pattern is a string containing a @samp{%} character.  The
2212 string must match the file name of a prerequisite that is being searched
2213 for, the @samp{%} character matching any sequence of zero or more
2214 characters (as in pattern rules; @pxref{Pattern Rules, ,Defining and
2215 Redefining Pattern Rules}).  For example, @code{%.h} matches files that
2216 end in @code{.h}.  (If there is no @samp{%}, the pattern must match the
2217 prerequisite exactly, which is not useful very often.)
2219 @cindex @code{%}, quoting in @code{vpath}
2220 @cindex @code{%}, quoting with @code{\} (backslash)
2221 @cindex @code{\} (backslash), to quote @code{%}
2222 @cindex backslash (@code{\}), to quote @code{%}
2223 @cindex quoting @code{%}, in @code{vpath}
2224 @samp{%} characters in a @code{vpath} directive's pattern can be quoted
2225 with preceding backslashes (@samp{\}).  Backslashes that would otherwise
2226 quote @samp{%} characters can be quoted with more backslashes.
2227 Backslashes that quote @samp{%} characters or other backslashes are
2228 removed from the pattern before it is compared to file names.  Backslashes
2229 that are not in danger of quoting @samp{%} characters go unmolested.@refill
2231 When a prerequisite fails to exist in the current directory, if the
2232 @var{pattern} in a @code{vpath} directive matches the name of the
2233 prerequisite file, then the @var{directories} in that directive are searched
2234 just like (and before) the directories in the @code{VPATH} variable.
2236 For example,
2238 @example
2239 vpath %.h ../headers
2240 @end example
2242 @noindent
2243 tells @code{make} to look for any prerequisite whose name ends in @file{.h}
2244 in the directory @file{../headers} if the file is not found in the current
2245 directory.
2247 If several @code{vpath} patterns match the prerequisite file's name, then
2248 @code{make} processes each matching @code{vpath} directive one by one,
2249 searching all the directories mentioned in each directive.  @code{make}
2250 handles multiple @code{vpath} directives in the order in which they
2251 appear in the makefile; multiple directives with the same pattern are
2252 independent of each other.
2254 @need 750
2255 Thus,
2257 @example
2258 @group
2259 vpath %.c foo
2260 vpath %   blish
2261 vpath %.c bar
2262 @end group
2263 @end example
2265 @noindent
2266 will look for a file ending in @samp{.c} in @file{foo}, then
2267 @file{blish}, then @file{bar}, while
2269 @example
2270 @group
2271 vpath %.c foo:bar
2272 vpath %   blish
2273 @end group
2274 @end example
2276 @noindent
2277 will look for a file ending in @samp{.c} in @file{foo}, then
2278 @file{bar}, then @file{blish}.
2280 @node Search Algorithm, Recipes/Search, Selective Search, Directory Search
2281 @subsection How Directory Searches are Performed
2282 @cindex algorithm for directory search
2283 @cindex directory search algorithm
2285 When a prerequisite is found through directory search, regardless of type
2286 (general or selective), the pathname located may not be the one that
2287 @code{make} actually provides you in the prerequisite list.  Sometimes
2288 the path discovered through directory search is thrown away.
2290 The algorithm @code{make} uses to decide whether to keep or abandon a
2291 path found via directory search is as follows:
2293 @enumerate
2294 @item
2295 If a target file does not exist at the path specified in the makefile,
2296 directory search is performed.
2298 @item
2299 If the directory search is successful, that path is kept and this file
2300 is tentatively stored as the target.
2302 @item
2303 All prerequisites of this target are examined using this same method.
2305 @item
2306 After processing the prerequisites, the target may or may not need to be
2307 rebuilt:
2309 @enumerate a
2310 @item
2311 If the target does @emph{not} need to be rebuilt, the path to the file
2312 found during directory search is used for any prerequisite lists which
2313 contain this target.  In short, if @code{make} doesn't need to rebuild
2314 the target then you use the path found via directory search.
2316 @item
2317 If the target @emph{does} need to be rebuilt (is out-of-date), the
2318 pathname found during directory search is @emph{thrown away}, and the
2319 target is rebuilt using the file name specified in the makefile.  In
2320 short, if @code{make} must rebuild, then the target is rebuilt locally,
2321 not in the directory found via directory search.
2322 @end enumerate
2323 @end enumerate
2325 This algorithm may seem complex, but in practice it is quite often
2326 exactly what you want.
2328 @cindex traditional directory search (GPATH)
2329 @cindex directory search, traditional (GPATH)
2330 Other versions of @code{make} use a simpler algorithm: if the file does
2331 not exist, and it is found via directory search, then that pathname is
2332 always used whether or not the target needs to be built.  Thus, if the
2333 target is rebuilt it is created at the pathname discovered during
2334 directory search.
2336 @vindex GPATH
2337 If, in fact, this is the behavior you want for some or all of your
2338 directories, you can use the @code{GPATH} variable to indicate this to
2339 @code{make}.
2341 @code{GPATH} has the same syntax and format as @code{VPATH} (that is, a
2342 space- or colon-delimited list of pathnames).  If an out-of-date target
2343 is found by directory search in a directory that also appears in
2344 @code{GPATH}, then that pathname is not thrown away.  The target is
2345 rebuilt using the expanded path.
2347 @node Recipes/Search, Implicit/Search, Search Algorithm, Directory Search
2348 @subsection Writing Recipes with Directory Search
2349 @cindex recipes, and directory search
2350 @cindex directory search (@code{VPATH}), and recipes
2352 When a prerequisite is found in another directory through directory search,
2353 this cannot change the recipe of the rule; they will execute as written.
2354 Therefore, you must write the recipe with care so that it will look for
2355 the prerequisite in the directory where @code{make} finds it.
2357 This is done with the @dfn{automatic variables} such as @samp{$^}
2358 (@pxref{Automatic Variables}).
2359 For instance, the value of @samp{$^} is a
2360 list of all the prerequisites of the rule, including the names of
2361 the directories in which they were found, and the value of
2362 @samp{$@@} is the target.  Thus:@refill
2364 @example
2365 foo.o : foo.c
2366         cc -c $(CFLAGS) $^ -o $@@
2367 @end example
2369 @noindent
2370 (The variable @code{CFLAGS} exists so you can specify flags for C
2371 compilation by implicit rules; we use it here for consistency so it will
2372 affect all C compilations uniformly;
2373 @pxref{Implicit Variables, ,Variables Used by Implicit Rules}.)
2375 Often the prerequisites include header files as well, which you do not
2376 want to mention in the recipe.  The automatic variable @samp{$<} is
2377 just the first prerequisite:
2379 @example
2380 VPATH = src:../headers
2381 foo.o : foo.c defs.h hack.h
2382         cc -c $(CFLAGS) $< -o $@@
2383 @end example
2385 @node Implicit/Search, Libraries/Search, Recipes/Search, Directory Search
2386 @subsection Directory Search and Implicit Rules
2387 @cindex @code{VPATH}, and implicit rules
2388 @cindex directory search (@code{VPATH}), and implicit rules
2389 @cindex search path for prerequisites (@code{VPATH}), and implicit rules
2390 @cindex implicit rule, and directory search
2391 @cindex implicit rule, and @code{VPATH}
2392 @cindex rule, implicit, and directory search
2393 @cindex rule, implicit, and @code{VPATH}
2395 The search through the directories specified in @code{VPATH} or with
2396 @code{vpath} also happens during consideration of implicit rules
2397 (@pxref{Implicit Rules, ,Using Implicit Rules}).
2399 For example, when a file @file{foo.o} has no explicit rule, @code{make}
2400 considers implicit rules, such as the built-in rule to compile
2401 @file{foo.c} if that file exists.  If such a file is lacking in the
2402 current directory, the appropriate directories are searched for it.  If
2403 @file{foo.c} exists (or is mentioned in the makefile) in any of the
2404 directories, the implicit rule for C compilation is applied.
2406 The recipes of implicit rules normally use automatic variables as a
2407 matter of necessity; consequently they will use the file names found by
2408 directory search with no extra effort.
2410 @node Libraries/Search,  , Implicit/Search, Directory Search
2411 @subsection Directory Search for Link Libraries
2412 @cindex link libraries, and directory search
2413 @cindex libraries for linking, directory search
2414 @cindex directory search (@code{VPATH}), and link libraries
2415 @cindex @code{VPATH}, and link libraries
2416 @cindex search path for prerequisites (@code{VPATH}), and link libraries
2417 @cindex @code{-l} (library search)
2418 @cindex link libraries, patterns matching
2419 @cindex @code{.LIBPATTERNS}, and link libraries
2420 @vindex .LIBPATTERNS
2422 Directory search applies in a special way to libraries used with the
2423 linker.  This special feature comes into play when you write a prerequisite
2424 whose name is of the form @samp{-l@var{name}}.  (You can tell something
2425 strange is going on here because the prerequisite is normally the name of a
2426 file, and the @emph{file name} of a library generally looks like
2427 @file{lib@var{name}.a}, not like @samp{-l@var{name}}.)@refill
2429 When a prerequisite's name has the form @samp{-l@var{name}}, @code{make}
2430 handles it specially by searching for the file @file{lib@var{name}.so},
2431 and, if it is not found, for the file @file{lib@var{name}.a} in the current
2432 directory, in directories specified by matching @code{vpath}
2433 search paths and the @code{VPATH} search path, and then in the
2434 directories @file{/lib}, @file{/usr/lib}, and @file{@var{prefix}/lib}
2435 (normally @file{/usr/local/lib}, but MS-DOS/MS-Windows versions of
2436 @code{make} behave as if @var{prefix} is defined to be the root of the
2437 DJGPP installation tree).
2439 For example, if there is a @file{/usr/lib/libcurses.a} library on your
2440 system (and no @file{/usr/lib/libcurses.so} file), then
2442 @example
2443 @group
2444 foo : foo.c -lcurses
2445         cc $^ -o $@@
2446 @end group
2447 @end example
2449 @noindent
2450 would cause the command @samp{cc foo.c /usr/lib/libcurses.a -o foo} to
2451 be executed when @file{foo} is older than @file{foo.c} or than
2452 @file{/usr/lib/libcurses.a}.@refill
2454 Although the default set of files to be searched for is
2455 @file{lib@var{name}.so} and @file{lib@var{name}.a}, this is customizable
2456 via the @code{.LIBPATTERNS} variable.  Each word in the value of this
2457 variable is a pattern string.  When a prerequisite like
2458 @samp{-l@var{name}} is seen, @code{make} will replace the percent in
2459 each pattern in the list with @var{name} and perform the above directory
2460 searches using each library filename.
2462 The default value for @code{.LIBPATTERNS} is @samp{lib%.so lib%.a},
2463 which provides the default behavior described above.
2465 You can turn off link library expansion completely by setting this
2466 variable to an empty value.
2468 @node Phony Targets, Force Targets, Directory Search, Rules
2469 @section Phony Targets
2470 @cindex phony targets
2471 @cindex targets, phony
2472 @cindex targets without a file
2474 A phony target is one that is not really the name of a file; rather it
2475 is just a name for a recipe to be executed when you make an explicit
2476 request.  There are two reasons to use a phony target: to avoid a
2477 conflict with a file of the same name, and to improve performance.
2479 If you write a rule whose recipe will not create the target file, the
2480 recipe will be executed every time the target comes up for remaking.
2481 Here is an example:
2483 @example
2484 @group
2485 clean:
2486         rm *.o temp
2487 @end group
2488 @end example
2490 @noindent
2491 Because the @code{rm} command does not create a file named @file{clean},
2492 probably no such file will ever exist.  Therefore, the @code{rm} command
2493 will be executed every time you say @samp{make clean}.
2494 @cindex @code{rm} (shell command)
2496 @findex .PHONY
2497 The phony target will cease to work if anything ever does create a file
2498 named @file{clean} in this directory.  Since it has no prerequisites, the
2499 file @file{clean} would inevitably be considered up to date, and its
2500 recipe would not be executed.  To avoid this problem, you can explicitly
2501 declare the target to be phony, using the special target @code{.PHONY}
2502 (@pxref{Special Targets, ,Special Built-in Target Names}) as follows:
2504 @example
2505 .PHONY : clean
2506 @end example
2508 @noindent
2509 Once this is done, @samp{make clean} will run the recipe regardless of
2510 whether there is a file named @file{clean}.
2512 Since it knows that phony targets do not name actual files that could be
2513 remade from other files, @code{make} skips the implicit rule search for
2514 phony targets (@pxref{Implicit Rules}).  This is why declaring a target
2515 phony is good for performance, even if you are not worried about the
2516 actual file existing.
2518 Thus, you first write the line that states that @code{clean} is a
2519 phony target, then you write the rule, like this:
2521 @example
2522 @group
2523 .PHONY: clean
2524 clean:
2525         rm *.o temp
2526 @end group
2527 @end example
2529 Another example of the usefulness of phony targets is in conjunction
2530 with recursive invocations of @code{make} (for more information, see
2531 @ref{Recursion, ,Recursive Use of @code{make}}).  In this case the
2532 makefile will often contain a variable which lists a number of
2533 subdirectories to be built.  One way to handle this is with one rule
2534 whose recipe is a shell loop over the subdirectories, like this:
2536 @example
2537 @group
2538 SUBDIRS = foo bar baz
2540 subdirs:
2541         for dir in $(SUBDIRS); do \
2542           $(MAKE) -C $$dir; \
2543         done
2544 @end group
2545 @end example
2547 There are problems with this method, however.  First, any error
2548 detected in a submake is ignored by this rule, so it will continue
2549 to build the rest of the directories even when one fails.  This can be
2550 overcome by adding shell commands to note the error and exit, but then
2551 it will do so even if @code{make} is invoked with the @code{-k}
2552 option, which is unfortunate.  Second, and perhaps more importantly,
2553 you cannot take advantage of @code{make}'s ability to build targets in
2554 parallel (@pxref{Parallel, ,Parallel Execution}), since there is only
2555 one rule.
2557 By declaring the subdirectories as phony targets (you must do this as
2558 the subdirectory obviously always exists; otherwise it won't be built)
2559 you can remove these problems:
2561 @example
2562 @group
2563 SUBDIRS = foo bar baz
2565 .PHONY: subdirs $(SUBDIRS)
2567 subdirs: $(SUBDIRS)
2569 $(SUBDIRS):
2570         $(MAKE) -C $@@
2572 foo: baz
2573 @end group
2574 @end example
2576 Here we've also declared that the @file{foo} subdirectory cannot be
2577 built until after the @file{baz} subdirectory is complete; this kind of
2578 relationship declaration is particularly important when attempting
2579 parallel builds.
2581 A phony target should not be a prerequisite of a real target file; if it
2582 is, its recipe will be run every time @code{make} goes to update that
2583 file.  As long as a phony target is never a prerequisite of a real
2584 target, the phony target recipe will be executed only when the phony
2585 target is a specified goal (@pxref{Goals, ,Arguments to Specify the
2586 Goals}).
2588 Phony targets can have prerequisites.  When one directory contains multiple
2589 programs, it is most convenient to describe all of the programs in one
2590 makefile @file{./Makefile}.  Since the target remade by default will be the
2591 first one in the makefile, it is common to make this a phony target named
2592 @samp{all} and give it, as prerequisites, all the individual programs.  For
2593 example:
2595 @example
2596 all : prog1 prog2 prog3
2597 .PHONY : all
2599 prog1 : prog1.o utils.o
2600         cc -o prog1 prog1.o utils.o
2602 prog2 : prog2.o
2603         cc -o prog2 prog2.o
2605 prog3 : prog3.o sort.o utils.o
2606         cc -o prog3 prog3.o sort.o utils.o
2607 @end example
2609 @noindent
2610 Now you can say just @samp{make} to remake all three programs, or
2611 specify as arguments the ones to remake (as in @samp{make prog1
2612 prog3}).  Phoniness is not inherited: the prerequisites of a phony
2613 target are not themselves phony, unless explicitly declared to be so.
2615 When one phony target is a prerequisite of another, it serves as a subroutine
2616 of the other.  For example, here @samp{make cleanall} will delete the
2617 object files, the difference files, and the file @file{program}:
2619 @example
2620 .PHONY: cleanall cleanobj cleandiff
2622 cleanall : cleanobj cleandiff
2623         rm program
2625 cleanobj :
2626         rm *.o
2628 cleandiff :
2629         rm *.diff
2630 @end example
2632 @node Force Targets, Empty Targets, Phony Targets, Rules
2633 @section Rules without Recipes or Prerequisites
2634 @cindex force targets
2635 @cindex targets, force
2636 @cindex @code{FORCE}
2637 @cindex rule, no recipe or prerequisites
2639 If a rule has no prerequisites or recipe, and the target of the rule
2640 is a nonexistent file, then @code{make} imagines this target to have
2641 been updated whenever its rule is run.  This implies that all targets
2642 depending on this one will always have their recipe run.
2644 An example will illustrate this:
2646 @example
2647 @group
2648 clean: FORCE
2649         rm $(objects)
2650 FORCE:
2651 @end group
2652 @end example
2654 Here the target @samp{FORCE} satisfies the special conditions, so the
2655 target @file{clean} that depends on it is forced to run its recipe.
2656 There is nothing special about the name @samp{FORCE}, but that is one
2657 name commonly used this way.
2659 As you can see, using @samp{FORCE} this way has the same results as using
2660 @samp{.PHONY: clean}.
2662 Using @samp{.PHONY} is more explicit and more efficient.  However,
2663 other versions of @code{make} do not support @samp{.PHONY}; thus
2664 @samp{FORCE} appears in many makefiles.  @xref{Phony Targets}.
2666 @node Empty Targets, Special Targets, Force Targets, Rules
2667 @section Empty Target Files to Record Events
2668 @cindex empty targets
2669 @cindex targets, empty
2670 @cindex recording events with empty targets
2672 The @dfn{empty target} is a variant of the phony target; it is used to hold
2673 recipes for an action that you request explicitly from time to time.
2674 Unlike a phony target, this target file can really exist; but the file's
2675 contents do not matter, and usually are empty.
2677 The purpose of the empty target file is to record, with its
2678 last-modification time, when the rule's recipe was last executed.  It
2679 does so because one of the commands in the recipe is a @code{touch}
2680 command to update the target file.
2682 The empty target file should have some prerequisites (otherwise it
2683 doesn't make sense).  When you ask to remake the empty target, the
2684 recipe is executed if any prerequisite is more recent than the target;
2685 in other words, if a prerequisite has changed since the last time you
2686 remade the target.  Here is an example:
2688 @example
2689 print: foo.c bar.c
2690         lpr -p $?
2691         touch print
2692 @end example
2693 @cindex @code{print} target
2694 @cindex @code{lpr} (shell command)
2695 @cindex @code{touch} (shell command)
2697 @noindent
2698 With this rule, @samp{make print} will execute the @code{lpr} command if
2699 either source file has changed since the last @samp{make print}.  The
2700 automatic variable @samp{$?} is used to print only those files that have
2701 changed (@pxref{Automatic Variables}).
2703 @node Special Targets, Multiple Targets, Empty Targets, Rules
2704 @section Special Built-in Target Names
2705 @cindex special targets
2706 @cindex built-in special targets
2707 @cindex targets, built-in special
2709 Certain names have special meanings if they appear as targets.
2711 @table @code
2712 @findex .PHONY
2713 @item .PHONY
2715 The prerequisites of the special target @code{.PHONY} are considered to
2716 be phony targets.  When it is time to consider such a target,
2717 @code{make} will run its recipe unconditionally, regardless of
2718 whether a file with that name exists or what its last-modification
2719 time is.  @xref{Phony Targets, ,Phony Targets}.
2721 @findex .SUFFIXES
2722 @item .SUFFIXES
2724 The prerequisites of the special target @code{.SUFFIXES} are the list
2725 of suffixes to be used in checking for suffix rules.
2726 @xref{Suffix Rules, , Old-Fashioned Suffix Rules}.
2728 @findex .DEFAULT
2729 @item .DEFAULT
2731 The recipe specified for @code{.DEFAULT} is used for any target for
2732 which no rules are found (either explicit rules or implicit rules).
2733 @xref{Last Resort}.  If a @code{.DEFAULT} recipe is specified, every
2734 file mentioned as a prerequisite, but not as a target in a rule, will have
2735 that recipe executed on its behalf.  @xref{Implicit Rule Search,
2736 ,Implicit Rule Search Algorithm}.
2738 @findex .PRECIOUS
2739 @item .PRECIOUS
2740 @cindex precious targets
2741 @cindex preserving with @code{.PRECIOUS}
2743 The targets which @code{.PRECIOUS} depends on are given the following
2744 special treatment: if @code{make} is killed or interrupted during the
2745 execution of their recipes, the target is not deleted.
2746 @xref{Interrupts, ,Interrupting or Killing @code{make}}.  Also, if the
2747 target is an intermediate file, it will not be deleted after it is no
2748 longer needed, as is normally done.  @xref{Chained Rules, ,Chains of
2749 Implicit Rules}.  In this latter respect it overlaps with the
2750 @code{.SECONDARY} special target.
2752 You can also list the target pattern of an implicit rule (such as
2753 @samp{%.o}) as a prerequisite file of the special target @code{.PRECIOUS}
2754 to preserve intermediate files created by rules whose target patterns
2755 match that file's name.
2757 @findex .INTERMEDIATE
2758 @item .INTERMEDIATE
2759 @cindex intermediate targets, explicit
2761 The targets which @code{.INTERMEDIATE} depends on are treated as
2762 intermediate files.  @xref{Chained Rules, ,Chains of Implicit Rules}.
2763 @code{.INTERMEDIATE} with no prerequisites has no effect.
2765 @findex .SECONDARY
2766 @item .SECONDARY
2767 @cindex secondary targets
2768 @cindex preserving with @code{.SECONDARY}
2770 The targets which @code{.SECONDARY} depends on are treated as
2771 intermediate files, except that they are never automatically deleted.
2772 @xref{Chained Rules, ,Chains of Implicit Rules}.
2774 @code{.SECONDARY} with no prerequisites causes all targets to be treated
2775 as secondary (i.e., no target is removed because it is considered
2776 intermediate).
2778 @findex .SECONDEXPANSION
2779 @item .SECONDEXPANSION
2781 If @code{.SECONDEXPANSION} is mentioned as a target anywhere in the
2782 makefile, then all prerequisite lists defined @emph{after} it appears
2783 will be expanded a second time after all makefiles have been read in.
2784 @xref{Secondary Expansion, ,Secondary Expansion}.
2786 @findex .DELETE_ON_ERROR
2787 @item .DELETE_ON_ERROR
2788 @cindex removing targets on failure
2790 If @code{.DELETE_ON_ERROR} is mentioned as a target anywhere in the
2791 makefile, then @code{make} will delete the target of a rule if it has
2792 changed and its recipe exits with a nonzero exit status, just as it
2793 does when it receives a signal.  @xref{Errors, ,Errors in Recipes}.
2795 @findex .IGNORE
2796 @item .IGNORE
2798 If you specify prerequisites for @code{.IGNORE}, then @code{make} will
2799 ignore errors in execution of the recipe for those particular files.
2800 The recipe for @code{.IGNORE} (if any) is ignored.
2802 If mentioned as a target with no prerequisites, @code{.IGNORE} says to
2803 ignore errors in execution of recipes for all files.  This usage of
2804 @samp{.IGNORE} is supported only for historical compatibility.  Since
2805 this affects every recipe in the makefile, it is not very useful; we
2806 recommend you use the more selective ways to ignore errors in specific
2807 recipes.  @xref{Errors, ,Errors in Recipes}.
2809 @findex .LOW_RESOLUTION_TIME
2810 @item .LOW_RESOLUTION_TIME
2812 If you specify prerequisites for @code{.LOW_RESOLUTION_TIME},
2813 @command{make} assumes that these files are created by commands that
2814 generate low resolution time stamps.  The recipe for the
2815 @code{.LOW_RESOLUTION_TIME} target are ignored.
2817 The high resolution file time stamps of many modern file systems
2818 lessen the chance of @command{make} incorrectly concluding that a file
2819 is up to date.  Unfortunately, some hosts do not provide a way to set a
2820 high resolution file time stamp, so commands like @samp{cp -p} that
2821 explicitly set a file's time stamp must discard its subsecond part.
2822 If a file is created by such a command, you should list it as a
2823 prerequisite of @code{.LOW_RESOLUTION_TIME} so that @command{make}
2824 does not mistakenly conclude that the file is out of date.  For
2825 example:
2827 @example
2828 @group
2829 .LOW_RESOLUTION_TIME: dst
2830 dst: src
2831         cp -p src dst
2832 @end group
2833 @end example
2835 Since @samp{cp -p} discards the subsecond part of @file{src}'s time
2836 stamp, @file{dst} is typically slightly older than @file{src} even when
2837 it is up to date.  The @code{.LOW_RESOLUTION_TIME} line causes
2838 @command{make} to consider @file{dst} to be up to date if its time stamp
2839 is at the start of the same second that @file{src}'s time stamp is in.
2841 Due to a limitation of the archive format, archive member time stamps
2842 are always low resolution.  You need not list archive members as
2843 prerequisites of @code{.LOW_RESOLUTION_TIME}, as @command{make} does this
2844 automatically.
2846 @findex .SILENT
2847 @item .SILENT
2849 If you specify prerequisites for @code{.SILENT}, then @code{make} will
2850 not print the recipe used to remake those particular files before
2851 executing them.  The recipe for @code{.SILENT} is ignored.
2853 If mentioned as a target with no prerequisites, @code{.SILENT} says not
2854 to print any recipes before executing them.  This usage of
2855 @samp{.SILENT} is supported only for historical compatibility.  We
2856 recommend you use the more selective ways to silence specific recipes.
2857 @xref{Echoing, ,Recipe Echoing}.  If you want to silence all recipes
2858 for a particular run of @code{make}, use the @samp{-s} or
2859 @w{@samp{--silent}} option (@pxref{Options Summary}).
2861 @findex .EXPORT_ALL_VARIABLES
2862 @item .EXPORT_ALL_VARIABLES
2864 Simply by being mentioned as a target, this tells @code{make} to
2865 export all variables to child processes by default.
2866 @xref{Variables/Recursion, ,Communicating Variables to a
2867 Sub-@code{make}}.
2869 @findex .NOTPARALLEL
2870 @item .NOTPARALLEL
2871 @cindex parallel execution, overriding
2873 If @code{.NOTPARALLEL} is mentioned as a target, then this invocation
2874 of @code{make} will be run serially, even if the @samp{-j} option is
2875 given.  Any recursively invoked @code{make} command will still run
2876 recipes in parallel (unless its makefile also contains this target).
2877 Any prerequisites on this target are ignored.
2879 @findex .ONESHELL
2880 @item .ONESHELL
2881 @cindex recipe execution, single invocation
2883 If @code{.ONESHELL} is mentioned as a target, then when a target is
2884 built all lines of the recipe will be given to a single invocation of
2885 the shell rather than each line being invoked separately
2886 (@pxref{Execution, ,Recipe Execution}).
2888 @findex .POSIX
2889 @item .POSIX
2890 @cindex POSIX-conforming mode, setting
2892 If @code{.POSIX} is mentioned as a target, then the makefile will be
2893 parsed and run in POSIX-conforming mode.  This does @emph{not} mean
2894 that only POSIX-conforming makefiles will be accepted: all advanced
2895 GNU @code{make} features are still available.  Rather, this target
2896 causes @code{make} to behave as required by POSIX in those areas
2897 where @code{make}'s default behavior differs.
2899 In particular, if this target is mentioned then recipes will be
2900 invoked as if the shell had been passed the @code{-e} flag: the first
2901 failing command in a recipe will cause the recipe to fail immediately.
2902 @end table
2904 Any defined implicit rule suffix also counts as a special target if it
2905 appears as a target, and so does the concatenation of two suffixes, such
2906 as @samp{.c.o}.  These targets are suffix rules, an obsolete way of
2907 defining implicit rules (but a way still widely used).  In principle, any
2908 target name could be special in this way if you break it in two and add
2909 both pieces to the suffix list.  In practice, suffixes normally begin with
2910 @samp{.}, so these special target names also begin with @samp{.}.
2911 @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
2913 @node Multiple Targets, Multiple Rules, Special Targets, Rules
2914 @section Multiple Targets in a Rule
2915 @cindex multiple targets
2916 @cindex several targets in a rule
2917 @cindex targets, multiple
2918 @cindex rule, with multiple targets
2920 A rule with multiple targets is equivalent to writing many rules, each with
2921 one target, and all identical aside from that.  The same recipe applies to
2922 all the targets, but its effect may vary because you can substitute the
2923 actual target name into the recipe using @samp{$@@}.  The rule contributes
2924 the same prerequisites to all the targets also.
2926 This is useful in two cases.
2928 @itemize @bullet
2929 @item
2930 You want just prerequisites, no recipe.  For example:
2932 @example
2933 kbd.o command.o files.o: command.h
2934 @end example
2936 @noindent
2937 gives an additional prerequisite to each of the three object files
2938 mentioned.
2940 @item
2941 Similar recipes work for all the targets.  The recipes do not need
2942 to be absolutely identical, since the automatic variable @samp{$@@}
2943 can be used to substitute the particular target to be remade into the
2944 commands (@pxref{Automatic Variables}).  For example:
2946 @example
2947 @group
2948 bigoutput littleoutput : text.g
2949         generate text.g -$(subst output,,$@@) > $@@
2950 @end group
2951 @end example
2952 @findex subst
2954 @noindent
2955 is equivalent to
2957 @example
2958 bigoutput : text.g
2959         generate text.g -big > bigoutput
2960 littleoutput : text.g
2961         generate text.g -little > littleoutput
2962 @end example
2964 @noindent
2965 Here we assume the hypothetical program @code{generate} makes two
2966 types of output, one if given @samp{-big} and one if given
2967 @samp{-little}.
2968 @xref{Text Functions, ,Functions for String Substitution and Analysis},
2969 for an explanation of the @code{subst} function.
2970 @end itemize
2972 Suppose you would like to vary the prerequisites according to the
2973 target, much as the variable @samp{$@@} allows you to vary the recipe.
2974 You cannot do this with multiple targets in an ordinary rule, but you
2975 can do it with a @dfn{static pattern rule}.  @xref{Static Pattern,
2976 ,Static Pattern Rules}.
2978 @node Multiple Rules, Static Pattern, Multiple Targets, Rules
2979 @section Multiple Rules for One Target
2980 @cindex multiple rules for one target
2981 @cindex several rules for one target
2982 @cindex rule, multiple for one target
2983 @cindex target, multiple rules for one
2985 One file can be the target of several rules.  All the prerequisites
2986 mentioned in all the rules are merged into one list of prerequisites for
2987 the target.  If the target is older than any prerequisite from any rule,
2988 the recipe is executed.
2990 There can only be one recipe to be executed for a file.  If more than
2991 one rule gives a recipe for the same file, @code{make} uses the last
2992 one given and prints an error message.  (As a special case, if the
2993 file's name begins with a dot, no error message is printed.  This odd
2994 behavior is only for compatibility with other implementations of
2995 @code{make}@dots{} you should avoid using it).  Occasionally it is
2996 useful to have the same target invoke multiple recipes which are
2997 defined in different parts of your makefile; you can use
2998 @dfn{double-colon rules} (@pxref{Double-Colon}) for this.
3000 An extra rule with just prerequisites can be used to give a few extra
3001 prerequisites to many files at once.  For example, makefiles often
3002 have a variable, such as @code{objects}, containing a list of all the
3003 compiler output files in the system being made.  An easy way to say
3004 that all of them must be recompiled if @file{config.h} changes is to
3005 write the following:
3007 @example
3008 objects = foo.o bar.o
3009 foo.o : defs.h
3010 bar.o : defs.h test.h
3011 $(objects) : config.h
3012 @end example
3014 This could be inserted or taken out without changing the rules that really
3015 specify how to make the object files, making it a convenient form to use if
3016 you wish to add the additional prerequisite intermittently.
3018 Another wrinkle is that the additional prerequisites could be
3019 specified with a variable that you set with a command line argument to
3020 @code{make} (@pxref{Overriding, ,Overriding Variables}).  For example,
3022 @example
3023 @group
3024 extradeps=
3025 $(objects) : $(extradeps)
3026 @end group
3027 @end example
3029 @noindent
3030 means that the command @samp{make extradeps=foo.h} will consider
3031 @file{foo.h} as a prerequisite of each object file, but plain @samp{make}
3032 will not.
3034 If none of the explicit rules for a target has a recipe, then @code{make}
3035 searches for an applicable implicit rule to find one
3036 @pxref{Implicit Rules, ,Using Implicit Rules}).
3038 @node Static Pattern, Double-Colon, Multiple Rules, Rules
3039 @section Static Pattern Rules
3040 @cindex static pattern rule
3041 @cindex rule, static pattern
3042 @cindex pattern rules, static (not implicit)
3043 @cindex varying prerequisites
3044 @cindex prerequisites, varying (static pattern)
3046 @dfn{Static pattern rules} are rules which specify multiple targets and
3047 construct the prerequisite names for each target based on the target name.
3048 They are more general than ordinary rules with multiple targets because the
3049 targets do not have to have identical prerequisites.  Their prerequisites must
3050 be @emph{analogous}, but not necessarily @emph{identical}.
3052 @menu
3053 * Static Usage::                The syntax of static pattern rules.
3054 * Static versus Implicit::      When are they better than implicit rules?
3055 @end menu
3057 @node Static Usage, Static versus Implicit, Static Pattern, Static Pattern
3058 @subsection Syntax of Static Pattern Rules
3059 @cindex static pattern rule, syntax of
3060 @cindex pattern rules, static, syntax of
3062 Here is the syntax of a static pattern rule:
3064 @example
3065 @var{targets} @dots{}: @var{target-pattern}: @var{prereq-patterns} @dots{}
3066         @var{recipe}
3067         @dots{}
3068 @end example
3070 @noindent
3071 The @var{targets} list specifies the targets that the rule applies to.
3072 The targets can contain wildcard characters, just like the targets of
3073 ordinary rules (@pxref{Wildcards, ,Using Wildcard Characters in File
3074 Names}).
3076 @cindex target pattern, static (not implicit)
3077 @cindex stem
3078 The @var{target-pattern} and @var{prereq-patterns} say how to compute the
3079 prerequisites of each target.  Each target is matched against the
3080 @var{target-pattern} to extract a part of the target name, called the
3081 @dfn{stem}.  This stem is substituted into each of the @var{prereq-patterns}
3082 to make the prerequisite names (one from each @var{prereq-pattern}).
3084 Each pattern normally contains the character @samp{%} just once.  When the
3085 @var{target-pattern} matches a target, the @samp{%} can match any part of
3086 the target name; this part is called the @dfn{stem}.  The rest of the
3087 pattern must match exactly.  For example, the target @file{foo.o} matches
3088 the pattern @samp{%.o}, with @samp{foo} as the stem.  The targets
3089 @file{foo.c} and @file{foo.out} do not match that pattern.@refill
3091 @cindex prerequisite pattern, static (not implicit)
3092 The prerequisite names for each target are made by substituting the stem
3093 for the @samp{%} in each prerequisite pattern.  For example, if one
3094 prerequisite pattern is @file{%.c}, then substitution of the stem
3095 @samp{foo} gives the prerequisite name @file{foo.c}.  It is legitimate
3096 to write a prerequisite pattern that does not contain @samp{%}; then this
3097 prerequisite is the same for all targets.
3099 @cindex @code{%}, quoting in static pattern
3100 @cindex @code{%}, quoting with @code{\} (backslash)
3101 @cindex @code{\} (backslash), to quote @code{%}
3102 @cindex backslash (@code{\}), to quote @code{%}
3103 @cindex quoting @code{%}, in static pattern
3104 @samp{%} characters in pattern rules can be quoted with preceding
3105 backslashes (@samp{\}).  Backslashes that would otherwise quote @samp{%}
3106 characters can be quoted with more backslashes.  Backslashes that quote
3107 @samp{%} characters or other backslashes are removed from the pattern
3108 before it is compared to file names or has a stem substituted into it.
3109 Backslashes that are not in danger of quoting @samp{%} characters go
3110 unmolested.  For example, the pattern @file{the\%weird\\%pattern\\} has
3111 @samp{the%weird\} preceding the operative @samp{%} character, and
3112 @samp{pattern\\} following it.  The final two backslashes are left alone
3113 because they cannot affect any @samp{%} character.@refill
3115 Here is an example, which compiles each of @file{foo.o} and @file{bar.o}
3116 from the corresponding @file{.c} file:
3118 @example
3119 @group
3120 objects = foo.o bar.o
3122 all: $(objects)
3124 $(objects): %.o: %.c
3125         $(CC) -c $(CFLAGS) $< -o $@@
3126 @end group
3127 @end example
3129 @noindent
3130 Here @samp{$<} is the automatic variable that holds the name of the
3131 prerequisite and @samp{$@@} is the automatic variable that holds the name
3132 of the target; see @ref{Automatic Variables}.
3134 Each target specified must match the target pattern; a warning is issued
3135 for each target that does not.  If you have a list of files, only some of
3136 which will match the pattern, you can use the @code{filter} function to
3137 remove nonmatching file names (@pxref{Text Functions, ,Functions for String Substitution and Analysis}):
3139 @example
3140 files = foo.elc bar.o lose.o
3142 $(filter %.o,$(files)): %.o: %.c
3143         $(CC) -c $(CFLAGS) $< -o $@@
3144 $(filter %.elc,$(files)): %.elc: %.el
3145         emacs -f batch-byte-compile $<
3146 @end example
3148 @noindent
3149 In this example the result of @samp{$(filter %.o,$(files))} is
3150 @file{bar.o lose.o}, and the first static pattern rule causes each of
3151 these object files to be updated by compiling the corresponding C source
3152 file.  The result of @w{@samp{$(filter %.elc,$(files))}} is
3153 @file{foo.elc}, so that file is made from @file{foo.el}.@refill
3155 Another example shows how to use @code{$*} in static pattern rules:
3156 @vindex $*@r{, and static pattern}
3158 @example
3159 @group
3160 bigoutput littleoutput : %output : text.g
3161         generate text.g -$* > $@@
3162 @end group
3163 @end example
3165 @noindent
3166 When the @code{generate} command is run, @code{$*} will expand to the
3167 stem, either @samp{big} or @samp{little}.
3169 @node Static versus Implicit,  , Static Usage, Static Pattern
3170 @subsection Static Pattern Rules versus Implicit Rules
3171 @cindex rule, static pattern versus implicit
3172 @cindex static pattern rule, versus implicit
3174 A static pattern rule has much in common with an implicit rule defined as a
3175 pattern rule (@pxref{Pattern Rules, ,Defining and Redefining Pattern Rules}).
3176 Both have a pattern for the target and patterns for constructing the
3177 names of prerequisites.  The difference is in how @code{make} decides
3178 @emph{when} the rule applies.
3180 An implicit rule @emph{can} apply to any target that matches its pattern,
3181 but it @emph{does} apply only when the target has no recipe otherwise
3182 specified, and only when the prerequisites can be found.  If more than one
3183 implicit rule appears applicable, only one applies; the choice depends on
3184 the order of rules.
3186 By contrast, a static pattern rule applies to the precise list of targets
3187 that you specify in the rule.  It cannot apply to any other target and it
3188 invariably does apply to each of the targets specified.  If two conflicting
3189 rules apply, and both have recipes, that's an error.
3191 The static pattern rule can be better than an implicit rule for these
3192 reasons:
3194 @itemize @bullet
3195 @item
3196 You may wish to override the usual implicit rule for a few
3197 files whose names cannot be categorized syntactically but
3198 can be given in an explicit list.
3200 @item
3201 If you cannot be sure of the precise contents of the directories
3202 you are using, you may not be sure which other irrelevant files
3203 might lead @code{make} to use the wrong implicit rule.  The choice
3204 might depend on the order in which the implicit rule search is done.
3205 With static pattern rules, there is no uncertainty: each rule applies
3206 to precisely the targets specified.
3207 @end itemize
3209 @node Double-Colon, Automatic Prerequisites, Static Pattern, Rules
3210 @section Double-Colon Rules
3211 @cindex double-colon rules
3212 @cindex rule, double-colon (@code{::})
3213 @cindex multiple rules for one target (@code{::})
3214 @cindex @code{::} rules (double-colon)
3216 @dfn{Double-colon} rules are explicit rules written with @samp{::}
3217 instead of @samp{:} after the target names.  They are handled
3218 differently from ordinary rules when the same target appears in more
3219 than one rule.  Pattern rules with double-colons have an entirely
3220 different meaning (@pxref{Match-Anything Rules}).
3222 When a target appears in multiple rules, all the rules must be the same
3223 type: all ordinary, or all double-colon.  If they are double-colon, each
3224 of them is independent of the others.  Each double-colon rule's recipe
3225 is executed if the target is older than any prerequisites of that rule.
3226 If there are no prerequisites for that rule, its recipe is always
3227 executed (even if the target already exists).  This can result in
3228 executing none, any, or all of the double-colon rules.
3230 Double-colon rules with the same target are in fact completely separate
3231 from one another.  Each double-colon rule is processed individually, just
3232 as rules with different targets are processed.
3234 The double-colon rules for a target are executed in the order they appear
3235 in the makefile.  However, the cases where double-colon rules really make
3236 sense are those where the order of executing the recipes would not matter.
3238 Double-colon rules are somewhat obscure and not often very useful; they
3239 provide a mechanism for cases in which the method used to update a target
3240 differs depending on which prerequisite files caused the update, and such
3241 cases are rare.
3243 Each double-colon rule should specify a recipe; if it does not, an
3244 implicit rule will be used if one applies.
3245 @xref{Implicit Rules, ,Using Implicit Rules}.
3247 @node Automatic Prerequisites,  , Double-Colon, Rules
3248 @section Generating Prerequisites Automatically
3249 @cindex prerequisites, automatic generation
3250 @cindex automatic generation of prerequisites
3251 @cindex generating prerequisites automatically
3253 In the makefile for a program, many of the rules you need to write often
3254 say only that some object file depends on some header
3255 file.  For example, if @file{main.c} uses @file{defs.h} via an
3256 @code{#include}, you would write:
3258 @example
3259 main.o: defs.h
3260 @end example
3262 @noindent
3263 You need this rule so that @code{make} knows that it must remake
3264 @file{main.o} whenever @file{defs.h} changes.  You can see that for a
3265 large program you would have to write dozens of such rules in your
3266 makefile.  And, you must always be very careful to update the makefile
3267 every time you add or remove an @code{#include}.
3268 @cindex @code{#include}
3270 @cindex @code{-M} (to compiler)
3271 To avoid this hassle, most modern C compilers can write these rules for
3272 you, by looking at the @code{#include} lines in the source files.
3273 Usually this is done with the @samp{-M} option to the compiler.
3274 For example, the command:
3276 @example
3277 cc -M main.c
3278 @end example
3280 @noindent
3281 generates the output:
3283 @example
3284 main.o : main.c defs.h
3285 @end example
3287 @noindent
3288 Thus you no longer have to write all those rules yourself.
3289 The compiler will do it for you.
3291 Note that such a rule constitutes mentioning @file{main.o} in a
3292 makefile, so it can never be considered an intermediate file by
3293 implicit rule search.  This means that @code{make} won't ever remove
3294 the file after using it; @pxref{Chained Rules, ,Chains of Implicit
3295 Rules}.
3297 @cindex @code{make depend}
3298 With old @code{make} programs, it was traditional practice to use this
3299 compiler feature to generate prerequisites on demand with a command like
3300 @samp{make depend}.  That command would create a file @file{depend}
3301 containing all the automatically-generated prerequisites; then the
3302 makefile could use @code{include} to read them in (@pxref{Include}).
3304 In GNU @code{make}, the feature of remaking makefiles makes this
3305 practice obsolete---you need never tell @code{make} explicitly to
3306 regenerate the prerequisites, because it always regenerates any makefile
3307 that is out of date.  @xref{Remaking Makefiles}.
3309 The practice we recommend for automatic prerequisite generation is to have
3310 one makefile corresponding to each source file.  For each source file
3311 @file{@var{name}.c} there is a makefile @file{@var{name}.d} which lists
3312 what files the object file @file{@var{name}.o} depends on.  That way
3313 only the source files that have changed need to be rescanned to produce
3314 the new prerequisites.
3316 Here is the pattern rule to generate a file of prerequisites (i.e., a makefile)
3317 called @file{@var{name}.d} from a C source file called @file{@var{name}.c}:
3319 @smallexample
3320 @group
3321 %.d: %.c
3322         @@set -e; rm -f $@@; \
3323          $(CC) -M $(CPPFLAGS) $< > $@@.$$$$; \
3324          sed 's,\($*\)\.o[ :]*,\1.o $@@ : ,g' < $@@.$$$$ > $@@; \
3325          rm -f $@@.$$$$
3326 @end group
3327 @end smallexample
3329 @noindent
3330 @xref{Pattern Rules}, for information on defining pattern rules.  The
3331 @samp{-e} flag to the shell causes it to exit immediately if the
3332 @code{$(CC)} command (or any other command) fails (exits with a
3333 nonzero status).
3334 @cindex @code{-e} (shell flag)
3336 @cindex @code{-MM} (to GNU compiler)
3337 With the GNU C compiler, you may wish to use the @samp{-MM} flag instead
3338 of @samp{-M}.  This omits prerequisites on system header files.
3339 @xref{Preprocessor Options, , Options Controlling the Preprocessor,
3340 gcc.info, Using GNU CC}, for details.
3342 @cindex @code{sed} (shell command)
3343 The purpose of the @code{sed} command is to translate (for example):
3345 @example
3346 main.o : main.c defs.h
3347 @end example
3349 @noindent
3350 into:
3352 @example
3353 main.o main.d : main.c defs.h
3354 @end example
3356 @noindent
3357 @cindex @code{.d}
3358 This makes each @samp{.d} file depend on all the source and header files
3359 that the corresponding @samp{.o} file depends on.  @code{make} then
3360 knows it must regenerate the prerequisites whenever any of the source or
3361 header files changes.
3363 Once you've defined the rule to remake the @samp{.d} files,
3364 you then use the @code{include} directive to read them all in.
3365 @xref{Include}.  For example:
3367 @example
3368 @group
3369 sources = foo.c bar.c
3371 include $(sources:.c=.d)
3372 @end group
3373 @end example
3375 @noindent
3376 (This example uses a substitution variable reference to translate the
3377 list of source files @samp{foo.c bar.c} into a list of prerequisite
3378 makefiles, @samp{foo.d bar.d}.  @xref{Substitution Refs}, for full
3379 information on substitution references.)  Since the @samp{.d} files are
3380 makefiles like any others, @code{make} will remake them as necessary
3381 with no further work from you.  @xref{Remaking Makefiles}.
3383 Note that the @samp{.d} files contain target definitions; you should
3384 be sure to place the @code{include} directive @emph{after} the first,
3385 default goal in your makefiles or run the risk of having a random
3386 object file become the default goal.
3387 @xref{How Make Works}.
3389 @node Recipes, Using Variables, Rules, Top
3390 @chapter Writing Recipes in Rules
3391 @cindex recipes
3392 @cindex recipes, how to write
3393 @cindex writing recipes
3395 The recipe of a rule consists of one or more shell command lines to
3396 be executed, one at a time, in the order they appear.  Typically, the
3397 result of executing these commands is that the target of the rule is
3398 brought up to date.
3400 Users use many different shell programs, but recipes in makefiles are
3401 always interpreted by @file{/bin/sh} unless the makefile specifies
3402 otherwise.  @xref{Execution, ,Recipe Execution}.
3404 @menu
3405 * Recipe Syntax::               Recipe syntax features and pitfalls.
3406 * Echoing::                     How to control when recipes are echoed.
3407 * Execution::                   How recipes are executed.
3408 * Parallel::                    How recipes can be executed in parallel.
3409 * Errors::                      What happens after a recipe execution error.
3410 * Interrupts::                  What happens when a recipe is interrupted.
3411 * Recursion::                   Invoking @code{make} from makefiles.
3412 * Canned Recipes::              Defining canned recipes.
3413 * Empty Recipes::               Defining useful, do-nothing recipes.
3414 @end menu
3416 @node Recipe Syntax, Echoing, Recipes, Recipes
3417 @section Recipe Syntax
3418 @cindex recipe syntax
3419 @cindex syntax of recipe
3421 Makefiles have the unusual property that there are really two distinct
3422 syntaxes in one file.  Most of the makefile uses @code{make} syntax
3423 (@pxref{Makefiles, ,Writing Makefiles}).  However, recipes are meant
3424 to be interpreted by the shell and so they are written using shell
3425 syntax.  The @code{make} program does not try to understand shell
3426 syntax: it performs only a very few specific translations on the
3427 content of the recipe before handing it to the shell.
3429 Each line in the recipe must start with a tab (or the first character
3430 in the value of the @code{.RECIPEPREFIX} variable; @pxref{Special
3431 Variables}), except that the first recipe line may be attached to the
3432 target-and-prerequisites line with a semicolon in between.  @emph{Any}
3433 line in the makefile that begins with a tab and appears in a ``rule
3434 context'' (that is, after a rule has been started until another rule
3435 or variable definition) will be considered part of a recipe for that
3436 rule.  Blank lines and lines of just comments may appear among the
3437 recipe lines; they are ignored.
3439 Some consequences of these rules include:
3441 @itemize @bullet
3442 @item
3443 A blank line that begins with a tab is not blank: it's an empty
3444 recipe (@pxref{Empty Recipes}).
3446 @cindex comments, in recipes
3447 @cindex recipes, comments in
3448 @cindex @code{#} (comments), in recipes
3449 @item
3450 A comment in a recipe is not a @code{make} comment; it will be
3451 passed to the shell as-is.  Whether the shell treats it as a comment
3452 or not depends on your shell.
3454 @item
3455 A variable definition in a ``rule context'' which is indented by a tab
3456 as the first character on the line, will be considered part of a
3457 recipe, not a @code{make} variable definition, and passed to the
3458 shell.
3460 @item
3461 A conditional expression (@code{ifdef}, @code{ifeq},
3462 etc. @pxref{Conditional Syntax, ,Syntax of Conditionals}) in a ``rule
3463 context'' which is indented by a tab as the first character on the
3464 line, will be considered part of a recipe and be passed to the shell.
3466 @end itemize
3468 @menu
3469 * Splitting Lines::             Breaking long recipe lines for readability.
3470 * Variables in Recipes::        Using @code{make} variables in recipes.
3471 @end menu
3473 @node Splitting Lines, Variables in Recipes, Recipe Syntax, Recipe Syntax
3474 @subsection Splitting Recipe Lines
3475 @cindex recipes, splitting
3476 @cindex splitting recipes
3477 @cindex recipes, backslash (@code{\}) in
3478 @cindex recipes, quoting newlines in
3479 @cindex backslash (@code{\}), in recipes
3480 @cindex @code{\} (backslash), in recipes
3481 @cindex quoting newline, in recipes
3482 @cindex newline, quoting, in recipes
3484 One of the few ways in which @code{make} does interpret recipes is
3485 checking for a backslash just before the newline.  As in normal
3486 makefile syntax, a single logical recipe line can be split into
3487 multiple physical lines in the makefile by placing a backslash before
3488 each newline.  A sequence of lines like this is considered a single
3489 recipe line, and one instance of the shell will be invoked to run it.
3491 However, in contrast to how they are treated in other places in a
3492 makefile, backslash-newline pairs are @emph{not} removed from the
3493 recipe.  Both the backslash and the newline characters are preserved
3494 and passed to the shell.  How the backslash-newline is interpreted
3495 depends on your shell.  If the first character of the next line after
3496 the backslash-newline is the recipe prefix character (a tab by
3497 default; @pxref{Special Variables}), then that character (and only
3498 that character) is removed.  Whitespace is never added to the recipe.
3500 For example, the recipe for the all target in this makefile:
3502 @example
3503 @group
3504 all :
3505         @@echo no\
3506 space
3507         @@echo no\
3508         space
3509         @@echo one \
3510         space
3511         @@echo one\
3512          space
3513 @end group
3514 @end example
3516 @noindent
3517 consists of four separate shell commands where the output is:
3519 @example
3520 @group
3521 nospace
3522 nospace
3523 one space
3524 one space
3525 @end group
3526 @end example
3528 As a more complex example, this makefile:
3530 @example
3531 @group
3532 all : ; @@echo 'hello \
3533         world' ; echo "hello \
3534     world"
3535 @end group
3536 @end example
3538 @noindent
3539 will invoke one shell with a command of:
3541 @example
3542 @group
3543 echo 'hello \
3544 world' ; echo "hello \
3545     world"
3546 @end group
3547 @end example
3549 @noindent
3550 which, according to shell quoting rules, will yield the following output:
3552 @example
3553 @group
3554 hello \
3555 world
3556 hello     world
3557 @end group
3558 @end example
3560 @noindent
3561 Notice how the backslash/newline pair was removed inside the string
3562 quoted with double quotes (@code{"@dots{}"}), but not from the string
3563 quoted with single quotes (@code{'@dots{}'}).  This is the way the
3564 default shell (@file{/bin/sh}) handles backslash/newline pairs.  If
3565 you specify a different shell in your makefiles it may treat them
3566 differently.
3568 Sometimes you want to split a long line inside of single quotes, but
3569 you don't want the backslash-newline to appear in the quoted content.
3570 This is often the case when passing scripts to languages such as Perl,
3571 where extraneous backslashes inside the script can change its meaning
3572 or even be a syntax error.  One simple way of handling this is to
3573 place the quoted string, or even the entire command, into a
3574 @code{make} variable then use the variable in the recipe.  In this
3575 situation the newline quoting rules for makefiles will be used, and
3576 the backslash-newline will be removed.  If we rewrite our example
3577 above using this method:
3579 @example
3580 @group
3581 HELLO = 'hello \
3582 world'
3584 all : ; @@echo $(HELLO)
3585 @end group
3586 @end example
3588 @noindent
3589 we will get output like this:
3591 @example
3592 @group
3593 hello world
3594 @end group
3595 @end example
3597 If you like, you can also use target-specific variables
3598 (@pxref{Target-specific, ,Target-specific Variable Values}) to obtain
3599 a tighter correspondence between the variable and the recipe that
3600 uses it.
3602 @node Variables in Recipes,  , Splitting Lines, Recipe Syntax
3603 @subsection Using Variables in Recipes
3604 @cindex variable references in recipes
3605 @cindex recipes, using variables in
3607 The other way in which @code{make} processes recipes is by expanding
3608 any variable references in them (@pxref{Reference,Basics of Variable
3609 References}).  This occurs after make has finished reading all the
3610 makefiles and the target is determined to be out of date; so, the
3611 recipes for targets which are not rebuilt are never expanded.
3613 Variable and function references in recipes have identical syntax and
3614 semantics to references elsewhere in the makefile.  They also have the
3615 same quoting rules: if you want a dollar sign to appear in your
3616 recipe, you must double it (@samp{$$}).  For shells like the default
3617 shell, that use dollar signs to introduce variables, it's important to
3618 keep clear in your mind whether the variable you want to reference is
3619 a @code{make} variable (use a single dollar sign) or a shell variable
3620 (use two dollar signs).  For example:
3622 @example
3623 @group
3624 LIST = one two three
3625 all:
3626         for i in $(LIST); do \
3627             echo $$i; \
3628         done
3629 @end group
3630 @end example
3632 @noindent
3633 results in the following command being passed to the shell:
3635 @example
3636 @group
3637 for i in one two three; do \
3638     echo $i; \
3639 done
3640 @end group
3641 @end example
3643 @noindent
3644 which generates the expected result:
3646 @example
3647 @group
3650 three
3651 @end group
3652 @end example
3654 @node Echoing, Execution, Recipe Syntax, Recipes
3655 @section Recipe Echoing
3656 @cindex echoing of recipes
3657 @cindex silent operation
3658 @cindex @code{@@} (in recipes)
3659 @cindex recipes, echoing
3660 @cindex printing of recipes
3662 Normally @code{make} prints each line of the recipe before it is
3663 executed.  We call this @dfn{echoing} because it gives the appearance
3664 that you are typing the lines yourself.
3666 When a line starts with @samp{@@}, the echoing of that line is suppressed.
3667 The @samp{@@} is discarded before the line is passed to the shell.
3668 Typically you would use this for a command whose only effect is to print
3669 something, such as an @code{echo} command to indicate progress through
3670 the makefile:
3672 @example
3673 @@echo About to make distribution files
3674 @end example
3676 @cindex @code{-n}
3677 @cindex @code{--just-print}
3678 @cindex @code{--dry-run}
3679 @cindex @code{--recon}
3680 When @code{make} is given the flag @samp{-n} or @samp{--just-print} it
3681 only echoes most recipes, without executing them.  @xref{Options
3682 Summary, ,Summary of Options}.  In this case even the recipe lines
3683 starting with @samp{@@} are printed.  This flag is useful for finding
3684 out which recipes @code{make} thinks are necessary without actually
3685 doing them.
3687 @cindex @code{-s}
3688 @cindex @code{--silent}
3689 @cindex @code{--quiet}
3690 @findex .SILENT
3691 The @samp{-s} or @samp{--silent}
3692 flag to @code{make} prevents all echoing, as if all recipes
3693 started with @samp{@@}.  A rule in the makefile for the special target
3694 @code{.SILENT} without prerequisites has the same effect
3695 (@pxref{Special Targets, ,Special Built-in Target Names}).
3696 @code{.SILENT} is essentially obsolete since @samp{@@} is more flexible.@refill
3698 @node Execution, Parallel, Echoing, Recipes
3699 @section Recipe Execution
3700 @cindex recipe, execution
3701 @cindex execution, of recipes
3702 @vindex @code{SHELL} @r{(recipe execution)}
3704 When it is time to execute recipes to update a target, they are
3705 executed by invoking a new subshell for each line of the recipe,
3706 unless the @code{.ONESHELL} special target is in effect
3707 (@pxref{One Shell, ,Using One Shell})  (In practice, @code{make} may
3708 take shortcuts that do not affect the results.)
3710 @cindex @code{cd} (shell command)
3711 @cindex shell variables, setting in recipes
3712 @cindex recipes setting shell variables
3713 @strong{Please note:} this implies that setting shell variables and
3714 invoking shell commands such as @code{cd} that set a context local to
3715 each process will not affect the following lines in the recipe.@footnote{On
3716 MS-DOS, the value of current working directory is @strong{global}, so
3717 changing it @emph{will} affect the following recipe lines on those
3718 systems.}  If you want to use @code{cd} to affect the next statement,
3719 put both statements in a single recipe line.  Then @code{make} will
3720 invoke one shell to run the entire line, and the shell will execute
3721 the statements in sequence.  For example:
3723 @example
3724 foo : bar/lose
3725         cd $(@@D) && gobble $(@@F) > ../$@@
3726 @end example
3728 @noindent
3729 Here we use the shell AND operator (@code{&&}) so that if the
3730 @code{cd} command fails, the script will fail without trying to invoke
3731 the @code{gobble} command in the wrong directory, which could cause
3732 problems (in this case it would certainly cause @file{../foo} to be
3733 truncated, at least).
3735 @menu
3736 * One Shell::                   One shell for all lines in a recipe
3737 * Choosing the Shell::          How @code{make} chooses the shell used
3738                                   to run recipes.
3739 @end menu
3741 @node One Shell, Choosing the Shell, Execution, Execution
3742 @subsection Using One Shell
3743 @cindex recipe lines, single shell
3744 @cindex @code{.ONESHELL}, use of
3745 @findex .ONESHELL
3747 Sometimes you would prefer that all the lines in the recipe be passed
3748 to a single invocation of the shell.  There are generally two
3749 situations where this is useful: first, it can improve performance in
3750 makefiles where recipes consist of many command lines, by avoiding
3751 extra processes.  Second, you might want newlines to be included in
3752 your recipe command (for example perhaps you are using a very
3753 different interpreter as your @code{SHELL}).  If the @code{.ONESHELL}
3754 special target appears anywhere in the makefile then @emph{all}
3755 recipe lines for each target will be provided to a single invocation
3756 of the shell.  Newlines between recipe lines will be preserved.  For
3757 example:
3759 @example
3760 .ONESHELL:
3761 foo : bar/lose
3762         cd $(@@D)
3763         gobble $(@@F) > ../$@@
3764 @end example
3766 @noindent
3767 would now work as expected even though the commands are on different
3768 recipe lines.
3770 If @code{.ONESHELL} is provided, then only the first line of the
3771 recipe will be checked for the special prefix characters (@samp{@@},
3772 @samp{-}, and @samp{+}).  Subsequent lines will include the special
3773 characters in the recipe line when the @code{SHELL} is invoked.  If
3774 you want your recipe to start with one of these special characters
3775 you'll need to arrange for them to not be the first characters on the
3776 first line, perhaps by adding a comment or similar.  For example, this
3777 would be a syntax error in Perl because the first @samp{@@} is removed
3778 by make:
3780 @example
3781 .ONESHELL:
3782 SHELL = /usr/bin/perl
3783 .SHELLFLAGS = -e
3784 show :
3785         @@f = qw(a b c);
3786         print "@@f\n";
3787 @end example
3789 @noindent
3790 However, either of these alternatives would work properly:
3792 @example
3793 .ONESHELL:
3794 SHELL = /usr/bin/perl
3795 .SHELLFLAGS = -e
3796 show :
3797         # Make sure "@@" is not the first character on the first line
3798         @@f = qw(a b c);
3799         print "@@f\n";
3800 @end example
3802 @noindent
3805 @example
3806 .ONESHELL:
3807 SHELL = /usr/bin/perl
3808 .SHELLFLAGS = -e
3809 show :
3810         my @@f = qw(a b c);
3811         print "@@f\n";
3812 @end example
3814 As a special feature, if @code{SHELL} is determined to be a
3815 POSIX-style shell, the special prefix characters in ``internal''
3816 recipe lines will @emph{removed} before the recipe is processed.  This
3817 feature is intended to allow existing makefiles to add the
3818 @code{.ONESHELL} special target and still run properly without
3819 extensive modifications.  Since the special prefix characters are not
3820 legal at the beginning of a line in a POSIX shell script this is not a
3821 loss in functionality.  For example, this works as expected:
3823 @example
3824 .ONESHELL:
3825 foo : bar/lose
3826         @@cd $(@@D)
3827         @@gobble $(@@F) > ../$@@
3828 @end example
3830 Even with this special feature, however, makefiles with
3831 @code{.ONESHELL} will behave differently in ways that could be
3832 noticeable.  For example, normally if any line in the recipe fails,
3833 that causes the rule to fail and no more recipe lines are processed.
3834 Under @code{.ONESHELL} a failure of any but the final recipe line will
3835 not be noticed by @code{make}.  You can modify @code{.SHELLFLAGS} to
3836 add the @code{-e} option to the shell which will cause any failure
3837 anywhere in the command line to cause the shell to fail, but this
3838 could itself cause your recipe to behave differently.  Ultimately you
3839 may need to harden your recipe lines to allow them to work with
3840 @code{.ONESHELL}.
3842 @node Choosing the Shell,  , One Shell, Execution
3843 @subsection Choosing the Shell
3844 @cindex shell, choosing the
3845 @cindex @code{SHELL}, value of
3846 @cindex @code{.SHELLFLAGS}, value of
3848 @vindex SHELL
3849 @vindex .SHELLFLAGS
3850 The program used as the shell is taken from the variable @code{SHELL}.
3851 If this variable is not set in your makefile, the program
3852 @file{/bin/sh} is used as the shell.  The argument(s) passed to the
3853 shell are taken from the variable @code{.SHELLFLAGS}.  The default
3854 value of @code{.SHELLFLAGS} is @code{-c} normally, or @code{-ec} in
3855 POSIX-conforming mode.
3857 @cindex environment, @code{SHELL} in
3858 Unlike most variables, the variable @code{SHELL} is never set from the
3859 environment.  This is because the @code{SHELL} environment variable is
3860 used to specify your personal choice of shell program for interactive
3861 use.  It would be very bad for personal choices like this to affect the
3862 functioning of makefiles.  @xref{Environment, ,Variables from the
3863 Environment}.
3865 Furthermore, when you do set @code{SHELL} in your makefile that value
3866 is @emph{not} exported in the environment to recipe lines that
3867 @code{make} invokes.  Instead, the value inherited from the user's
3868 environment, if any, is exported.  You can override this behavior by
3869 explicitly exporting @code{SHELL} (@pxref{Variables/Recursion,
3870 ,Communicating Variables to a Sub-@code{make}}), forcing it to be
3871 passed in the environment to recipe lines.
3873 @vindex @code{MAKESHELL} @r{(MS-DOS alternative to @code{SHELL})}
3874 However, on MS-DOS and MS-Windows the value of @code{SHELL} in the
3875 environment @strong{is} used, since on those systems most users do not
3876 set this variable, and therefore it is most likely set specifically to
3877 be used by @code{make}.  On MS-DOS, if the setting of @code{SHELL} is
3878 not suitable for @code{make}, you can set the variable
3879 @code{MAKESHELL} to the shell that @code{make} should use; if set it
3880 will be used as the shell instead of the value of @code{SHELL}.
3882 @subsubheading Choosing a Shell in DOS and Windows
3883 @cindex shell, in DOS and Windows
3884 @cindex DOS, choosing a shell in
3885 @cindex Windows, choosing a shell in
3887 Choosing a shell in MS-DOS and MS-Windows is much more complex than on
3888 other systems.
3890 @vindex COMSPEC
3891 On MS-DOS, if @code{SHELL} is not set, the value of the variable
3892 @code{COMSPEC} (which is always set) is used instead.
3894 @cindex @code{SHELL}, MS-DOS specifics
3895 The processing of lines that set the variable @code{SHELL} in Makefiles
3896 is different on MS-DOS.  The stock shell, @file{command.com}, is
3897 ridiculously limited in its functionality and many users of @code{make}
3898 tend to install a replacement shell.  Therefore, on MS-DOS, @code{make}
3899 examines the value of @code{SHELL}, and changes its behavior based on
3900 whether it points to a Unix-style or DOS-style shell.  This allows
3901 reasonable functionality even if @code{SHELL} points to
3902 @file{command.com}.
3904 If @code{SHELL} points to a Unix-style shell, @code{make} on MS-DOS
3905 additionally checks whether that shell can indeed be found; if not, it
3906 ignores the line that sets @code{SHELL}.  In MS-DOS, GNU @code{make}
3907 searches for the shell in the following places:
3909 @enumerate
3910 @item
3911 In the precise place pointed to by the value of @code{SHELL}.  For
3912 example, if the makefile specifies @samp{SHELL = /bin/sh}, @code{make}
3913 will look in the directory @file{/bin} on the current drive.
3915 @item
3916 In the current directory.
3918 @item
3919 In each of the directories in the @code{PATH} variable, in order.
3921 @end enumerate
3923 In every directory it examines, @code{make} will first look for the
3924 specific file (@file{sh} in the example above).  If this is not found,
3925 it will also look in that directory for that file with one of the known
3926 extensions which identify executable files.  For example @file{.exe},
3927 @file{.com}, @file{.bat}, @file{.btm}, @file{.sh}, and some others.
3929 If any of these attempts is successful, the value of @code{SHELL} will
3930 be set to the full pathname of the shell as found.  However, if none of
3931 these is found, the value of @code{SHELL} will not be changed, and thus
3932 the line that sets it will be effectively ignored.  This is so
3933 @code{make} will only support features specific to a Unix-style shell if
3934 such a shell is actually installed on the system where @code{make} runs.
3936 Note that this extended search for the shell is limited to the cases
3937 where @code{SHELL} is set from the Makefile; if it is set in the
3938 environment or command line, you are expected to set it to the full
3939 pathname of the shell, exactly as things are on Unix.
3941 The effect of the above DOS-specific processing is that a Makefile that
3942 contains @samp{SHELL = /bin/sh} (as many Unix makefiles do), will work
3943 on MS-DOS unaltered if you have e.g.@: @file{sh.exe} installed in some
3944 directory along your @code{PATH}.
3946 @vindex SHELL
3947 @vindex .SHELLFLAGS
3949 @node Parallel, Errors, Execution, Recipes
3950 @section Parallel Execution
3951 @cindex recipes, execution in parallel
3952 @cindex parallel execution
3953 @cindex execution, in parallel
3954 @cindex job slots
3955 @cindex @code{-j}
3956 @cindex @code{--jobs}
3958 GNU @code{make} knows how to execute several recipes at once.
3959 Normally, @code{make} will execute only one recipe at a time, waiting
3960 for it to finish before executing the next.  However, the @samp{-j} or
3961 @samp{--jobs} option tells @code{make} to execute many recipes
3962 simultaneously.  You can inhibit parallelism in a particular makefile
3963 with the @code{.NOTPARALLEL} pseudo-target (@pxref{Special
3964 Targets,Special Built-in Target Names}).@refill
3966 On MS-DOS, the @samp{-j} option has no effect, since that system doesn't
3967 support multi-processing.
3969 If the @samp{-j} option is followed by an integer, this is the number of
3970 recipes to execute at once; this is called the number of @dfn{job slots}.
3971 If there is nothing looking like an integer after the @samp{-j} option,
3972 there is no limit on the number of job slots.  The default number of job
3973 slots is one, which means serial execution (one thing at a time).
3975 One unpleasant consequence of running several recipes simultaneously is
3976 that output generated by the recipes appears whenever each recipe
3977 sends it, so messages from different recipes may be interspersed.
3979 Another problem is that two processes cannot both take input from the
3980 same device; so to make sure that only one recipe tries to take input
3981 from the terminal at once, @code{make} will invalidate the standard
3982 input streams of all but one running recipe.  This means that
3983 attempting to read from standard input will usually be a fatal error (a
3984 @samp{Broken pipe} signal) for most child processes if there are
3985 several.
3986 @cindex broken pipe
3987 @cindex standard input
3989 It is unpredictable which recipe will have a valid standard input stream
3990 (which will come from the terminal, or wherever you redirect the standard
3991 input of @code{make}).  The first recipe run will always get it first, and
3992 the first recipe started after that one finishes will get it next, and so
3995 We will change how this aspect of @code{make} works if we find a better
3996 alternative.  In the mean time, you should not rely on any recipe using
3997 standard input at all if you are using the parallel execution feature; but
3998 if you are not using this feature, then standard input works normally in
3999 all recipes.
4001 Finally, handling recursive @code{make} invocations raises issues.  For
4002 more information on this, see
4003 @ref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
4005 If a recipe fails (is killed by a signal or exits with a nonzero
4006 status), and errors are not ignored for that recipe
4007 (@pxref{Errors, ,Errors in Recipes}),
4008 the remaining recipe lines to remake the same target will not be run.
4009 If a recipe fails and the @samp{-k} or @samp{--keep-going}
4010 option was not given
4011 (@pxref{Options Summary, ,Summary of Options}),
4012 @code{make} aborts execution.  If make
4013 terminates for any reason (including a signal) with child processes
4014 running, it waits for them to finish before actually exiting.@refill
4016 @cindex load average
4017 @cindex limiting jobs based on load
4018 @cindex jobs, limiting based on load
4019 @cindex @code{-l} (load average)
4020 @cindex @code{--max-load}
4021 @cindex @code{--load-average}
4022 When the system is heavily loaded, you will probably want to run fewer jobs
4023 than when it is lightly loaded.  You can use the @samp{-l} option to tell
4024 @code{make} to limit the number of jobs to run at once, based on the load
4025 average.  The @samp{-l} or @samp{--max-load}
4026 option is followed by a floating-point number.  For
4027 example,
4029 @example
4030 -l 2.5
4031 @end example
4033 @noindent
4034 will not let @code{make} start more than one job if the load average is
4035 above 2.5.  The @samp{-l} option with no following number removes the
4036 load limit, if one was given with a previous @samp{-l} option.@refill
4038 More precisely, when @code{make} goes to start up a job, and it already has
4039 at least one job running, it checks the current load average; if it is not
4040 lower than the limit given with @samp{-l}, @code{make} waits until the load
4041 average goes below that limit, or until all the other jobs finish.
4043 By default, there is no load limit.
4045 @node Errors, Interrupts, Parallel, Recipes
4046 @section Errors in Recipes
4047 @cindex errors (in recipes)
4048 @cindex recipes, errors in
4049 @cindex exit status (errors)
4051 After each shell invocation returns, @code{make} looks at its exit
4052 status.  If the shell completed successfully (the exit status is
4053 zero), the next line in the recipe is executed in a new shell; after
4054 the last line is finished, the rule is finished.
4056 If there is an error (the exit status is nonzero), @code{make} gives up on
4057 the current rule, and perhaps on all rules.
4059 Sometimes the failure of a certain recipe line does not indicate a problem.
4060 For example, you may use the @code{mkdir} command to ensure that a
4061 directory exists.  If the directory already exists, @code{mkdir} will
4062 report an error, but you probably want @code{make} to continue regardless.
4064 @cindex @code{-} (in recipes)
4065 To ignore errors in a recipe line, write a @samp{-} at the beginning
4066 of the line's text (after the initial tab).  The @samp{-} is discarded
4067 before the line is passed to the shell for execution.
4069 For example,
4071 @example
4072 @group
4073 clean:
4074         -rm -f *.o
4075 @end group
4076 @end example
4077 @cindex @code{rm} (shell command)
4079 @noindent
4080 This causes @code{make} to continue even if @code{rm} is unable to
4081 remove a file.
4083 @cindex @code{-i}
4084 @cindex @code{--ignore-errors}
4085 @findex .IGNORE
4086 When you run @code{make} with the @samp{-i} or @samp{--ignore-errors}
4087 flag, errors are ignored in all recipes of all rules.  A rule in the
4088 makefile for the special target @code{.IGNORE} has the same effect, if
4089 there are no prerequisites.  These ways of ignoring errors are obsolete
4090 because @samp{-} is more flexible.
4092 When errors are to be ignored, because of either a @samp{-} or the
4093 @samp{-i} flag, @code{make} treats an error return just like success,
4094 except that it prints out a message that tells you the status code
4095 the shell exited with, and says that the error has been ignored.
4097 When an error happens that @code{make} has not been told to ignore,
4098 it implies that the current target cannot be correctly remade, and neither
4099 can any other that depends on it either directly or indirectly.  No further
4100 recipes will be executed for these targets, since their preconditions
4101 have not been achieved.
4104 @cindex @code{-k}
4105 @cindex @code{--keep-going}
4106 Normally @code{make} gives up immediately in this circumstance, returning a
4107 nonzero status.  However, if the @samp{-k} or @samp{--keep-going}
4108 flag is specified, @code{make}
4109 continues to consider the other prerequisites of the pending targets,
4110 remaking them if necessary, before it gives up and returns nonzero status.
4111 For example, after an error in compiling one object file, @samp{make -k}
4112 will continue compiling other object files even though it already knows
4113 that linking them will be impossible.  @xref{Options Summary, ,Summary of Options}.
4115 The usual behavior assumes that your purpose is to get the specified
4116 targets up to date; once @code{make} learns that this is impossible, it
4117 might as well report the failure immediately.  The @samp{-k} option says
4118 that the real purpose is to test as many of the changes made in the
4119 program as possible, perhaps to find several independent problems so
4120 that you can correct them all before the next attempt to compile.  This
4121 is why Emacs' @code{compile} command passes the @samp{-k} flag by
4122 default.
4123 @cindex Emacs (@code{M-x compile})
4125 @findex .DELETE_ON_ERROR
4126 @cindex deletion of target files
4127 @cindex removal of target files
4128 @cindex target, deleting on error
4129 Usually when a recipe line fails, if it has changed the target file at all,
4130 the file is corrupted and cannot be used---or at least it is not
4131 completely updated.  Yet the file's time stamp says that it is now up to
4132 date, so the next time @code{make} runs, it will not try to update that
4133 file.  The situation is just the same as when the shell is killed by a
4134 signal; @pxref{Interrupts}.  So generally the right thing to do is to
4135 delete the target file if the recipe fails after beginning to change
4136 the file.  @code{make} will do this if @code{.DELETE_ON_ERROR} appears
4137 as a target.  This is almost always what you want @code{make} to do, but
4138 it is not historical practice; so for compatibility, you must explicitly
4139 request it.
4141 @node Interrupts, Recursion, Errors, Recipes
4142 @section Interrupting or Killing @code{make}
4143 @cindex interrupt
4144 @cindex signal
4145 @cindex deletion of target files
4146 @cindex removal of target files
4147 @cindex target, deleting on interrupt
4148 @cindex killing (interruption)
4150 If @code{make} gets a fatal signal while a shell is executing, it may
4151 delete the target file that the recipe was supposed to update.  This is
4152 done if the target file's last-modification time has changed since
4153 @code{make} first checked it.
4155 The purpose of deleting the target is to make sure that it is remade from
4156 scratch when @code{make} is next run.  Why is this?  Suppose you type
4157 @kbd{Ctrl-c} while a compiler is running, and it has begun to write an
4158 object file @file{foo.o}.  The @kbd{Ctrl-c} kills the compiler, resulting
4159 in an incomplete file whose last-modification time is newer than the source
4160 file @file{foo.c}.  But @code{make} also receives the @kbd{Ctrl-c} signal
4161 and deletes this incomplete file.  If @code{make} did not do this, the next
4162 invocation of @code{make} would think that @file{foo.o} did not require
4163 updating---resulting in a strange error message from the linker when it
4164 tries to link an object file half of which is missing.
4166 @findex .PRECIOUS
4167 You can prevent the deletion of a target file in this way by making the
4168 special target @code{.PRECIOUS} depend on it.  Before remaking a target,
4169 @code{make} checks to see whether it appears on the prerequisites of
4170 @code{.PRECIOUS}, and thereby decides whether the target should be deleted
4171 if a signal happens.  Some reasons why you might do this are that the
4172 target is updated in some atomic fashion, or exists only to record a
4173 modification-time (its contents do not matter), or must exist at all
4174 times to prevent other sorts of trouble.
4176 @node Recursion, Canned Recipes, Interrupts, Recipes
4177 @section Recursive Use of @code{make}
4178 @cindex recursion
4179 @cindex subdirectories, recursion for
4181 Recursive use of @code{make} means using @code{make} as a command in a
4182 makefile.  This technique is useful when you want separate makefiles for
4183 various subsystems that compose a larger system.  For example, suppose you
4184 have a subdirectory @file{subdir} which has its own makefile, and you would
4185 like the containing directory's makefile to run @code{make} on the
4186 subdirectory.  You can do it by writing this:
4188 @example
4189 subsystem:
4190         cd subdir && $(MAKE)
4191 @end example
4193 @noindent
4194 or, equivalently, this (@pxref{Options Summary, ,Summary of Options}):
4196 @example
4197 subsystem:
4198         $(MAKE) -C subdir
4199 @end example
4200 @cindex @code{-C}
4201 @cindex @code{--directory}
4203 You can write recursive @code{make} commands just by copying this example,
4204 but there are many things to know about how they work and why, and about
4205 how the sub-@code{make} relates to the top-level @code{make}.  You may
4206 also find it useful to declare targets that invoke recursive
4207 @code{make} commands as @samp{.PHONY} (for more discussion on when
4208 this is useful, see @ref{Phony Targets}).
4210 @vindex @code{CURDIR}
4211 For your convenience, when GNU @code{make} starts (after it has
4212 processed any @code{-C} options) it sets the variable @code{CURDIR} to
4213 the pathname of the current working directory.  This value is never
4214 touched by @code{make} again: in particular note that if you include
4215 files from other directories the value of @code{CURDIR} does not
4216 change.  The value has the same precedence it would have if it were
4217 set in the makefile (by default, an environment variable @code{CURDIR}
4218 will not override this value).  Note that setting this variable has no
4219 impact on the operation of @code{make} (it does not cause @code{make}
4220 to change its working directory, for example).
4222 @menu
4223 * MAKE Variable::               The special effects of using @samp{$(MAKE)}.
4224 * Variables/Recursion::         How to communicate variables to a sub-@code{make}.
4225 * Options/Recursion::           How to communicate options to a sub-@code{make}.
4226 * -w Option::                   How the @samp{-w} or @samp{--print-directory} option
4227                                   helps debug use of recursive @code{make} commands.
4228 @end menu
4230 @node MAKE Variable, Variables/Recursion, Recursion, Recursion
4231 @subsection How the @code{MAKE} Variable Works
4232 @vindex MAKE
4233 @cindex recursion, and @code{MAKE} variable
4235 Recursive @code{make} commands should always use the variable @code{MAKE},
4236 not the explicit command name @samp{make}, as shown here:
4238 @example
4239 @group
4240 subsystem:
4241         cd subdir && $(MAKE)
4242 @end group
4243 @end example
4245 The value of this variable is the file name with which @code{make} was
4246 invoked.  If this file name was @file{/bin/make}, then the recipe executed
4247 is @samp{cd subdir && /bin/make}.  If you use a special version of
4248 @code{make} to run the top-level makefile, the same special version will be
4249 executed for recursive invocations.
4250 @cindex @code{cd} (shell command)
4252 @cindex +, and recipes
4253 As a special feature, using the variable @code{MAKE} in the recipe of
4254 a rule alters the effects of the @samp{-t} (@samp{--touch}), @samp{-n}
4255 (@samp{--just-print}), or @samp{-q} (@w{@samp{--question}}) option.
4256 Using the @code{MAKE} variable has the same effect as using a @samp{+}
4257 character at the beginning of the recipe line.  @xref{Instead of
4258 Execution, ,Instead of Executing the Recipes}.  This special feature
4259 is only enabled if the @code{MAKE} variable appears directly in the
4260 recipe: it does not apply if the @code{MAKE} variable is referenced
4261 through expansion of another variable.  In the latter case you must
4262 use the @samp{+} token to get these special effects.@refill
4264 Consider the command @samp{make -t} in the above example.  (The
4265 @samp{-t} option marks targets as up to date without actually running
4266 any recipes; see @ref{Instead of Execution}.)  Following the usual
4267 definition of @samp{-t}, a @samp{make -t} command in the example would
4268 create a file named @file{subsystem} and do nothing else.  What you
4269 really want it to do is run @samp{@w{cd subdir &&} @w{make -t}}; but
4270 that would require executing the recipe, and @samp{-t} says not to
4271 execute recipes.@refill
4272 @cindex @code{-t}, and recursion
4273 @cindex recursion, and @code{-t}
4274 @cindex @code{--touch}, and recursion
4276 The special feature makes this do what you want: whenever a recipe
4277 line of a rule contains the variable @code{MAKE}, the flags @samp{-t},
4278 @samp{-n} and @samp{-q} do not apply to that line.  Recipe lines
4279 containing @code{MAKE} are executed normally despite the presence of a
4280 flag that causes most recipes not to be run.  The usual
4281 @code{MAKEFLAGS} mechanism passes the flags to the sub-@code{make}
4282 (@pxref{Options/Recursion, ,Communicating Options to a
4283 Sub-@code{make}}), so your request to touch the files, or print the
4284 recipes, is propagated to the subsystem.@refill
4286 @node Variables/Recursion, Options/Recursion, MAKE Variable, Recursion
4287 @subsection Communicating Variables to a Sub-@code{make}
4288 @cindex sub-@code{make}
4289 @cindex environment, and recursion
4290 @cindex exporting variables
4291 @cindex variables, environment
4292 @cindex variables, exporting
4293 @cindex recursion, and environment
4294 @cindex recursion, and variables
4296 Variable values of the top-level @code{make} can be passed to the
4297 sub-@code{make} through the environment by explicit request.  These
4298 variables are defined in the sub-@code{make} as defaults, but they do
4299 not override variables defined in the makefile used by
4300 the sub-@code{make} unless you use the @samp{-e} switch (@pxref{Options
4301 Summary, ,Summary of Options}).@refill
4303 To pass down, or @dfn{export}, a variable, @code{make} adds the
4304 variable and its value to the environment for running each line of the
4305 recipe.  The sub-@code{make}, in turn, uses the environment to
4306 initialize its table of variable values.  @xref{Environment,
4307 ,Variables from the Environment}.
4309 Except by explicit request, @code{make} exports a variable only if it
4310 is either defined in the environment initially or set on the command
4311 line, and if its name consists only of letters, numbers, and underscores.
4312 Some shells cannot cope with environment variable names consisting of
4313 characters other than letters, numbers, and underscores.
4315 @cindex SHELL, exported value
4316 The value of the @code{make} variable @code{SHELL} is not exported.
4317 Instead, the value of the @code{SHELL} variable from the invoking
4318 environment is passed to the sub-@code{make}.  You can force
4319 @code{make} to export its value for @code{SHELL} by using the
4320 @code{export} directive, described below.  @xref{Choosing the Shell}.
4322 The special variable @code{MAKEFLAGS} is always exported (unless you
4323 unexport it).  @code{MAKEFILES} is exported if you set it to anything.
4325 @code{make} automatically passes down variable values that were defined
4326 on the command line, by putting them in the @code{MAKEFLAGS} variable.
4327 @iftex
4328 See the next section.
4329 @end iftex
4330 @ifnottex
4331 @xref{Options/Recursion}.
4332 @end ifnottex
4334 Variables are @emph{not} normally passed down if they were created by
4335 default by @code{make} (@pxref{Implicit Variables, ,Variables Used by
4336 Implicit Rules}).  The sub-@code{make} will define these for
4337 itself.@refill
4339 @findex export
4340 If you want to export specific variables to a sub-@code{make}, use the
4341 @code{export} directive, like this:
4343 @example
4344 export @var{variable} @dots{}
4345 @end example
4347 @noindent
4348 @findex unexport
4349 If you want to @emph{prevent} a variable from being exported, use the
4350 @code{unexport} directive, like this:
4352 @example
4353 unexport @var{variable} @dots{}
4354 @end example
4356 @noindent
4357 In both of these forms, the arguments to @code{export} and
4358 @code{unexport} are expanded, and so could be variables or functions
4359 which expand to a (list of) variable names to be (un)exported.
4361 As a convenience, you can define a variable and export it at the same
4362 time by doing:
4364 @example
4365 export @var{variable} = value
4366 @end example
4368 @noindent
4369 has the same result as:
4371 @example
4372 @var{variable} = value
4373 export @var{variable}
4374 @end example
4376 @noindent
4379 @example
4380 export @var{variable} := value
4381 @end example
4383 @noindent
4384 has the same result as:
4386 @example
4387 @var{variable} := value
4388 export @var{variable}
4389 @end example
4391 Likewise,
4393 @example
4394 export @var{variable} += value
4395 @end example
4397 @noindent
4398 is just like:
4400 @example
4401 @var{variable} += value
4402 export @var{variable}
4403 @end example
4405 @noindent
4406 @xref{Appending, ,Appending More Text to Variables}.
4408 You may notice that the @code{export} and @code{unexport} directives
4409 work in @code{make} in the same way they work in the shell, @code{sh}.
4411 If you want all variables to be exported by default, you can use
4412 @code{export} by itself:
4414 @example
4415 export
4416 @end example
4418 @noindent
4419 This tells @code{make} that variables which are not explicitly mentioned
4420 in an @code{export} or @code{unexport} directive should be exported.
4421 Any variable given in an @code{unexport} directive will still @emph{not}
4422 be exported.  If you use @code{export} by itself to export variables by
4423 default, variables whose names contain characters other than
4424 alphanumerics and underscores will not be exported unless specifically
4425 mentioned in an @code{export} directive.@refill
4427 @findex .EXPORT_ALL_VARIABLES
4428 The behavior elicited by an @code{export} directive by itself was the
4429 default in older versions of GNU @code{make}.  If your makefiles depend
4430 on this behavior and you want to be compatible with old versions of
4431 @code{make}, you can write a rule for the special target
4432 @code{.EXPORT_ALL_VARIABLES} instead of using the @code{export} directive.
4433 This will be ignored by old @code{make}s, while the @code{export}
4434 directive will cause a syntax error.@refill
4435 @cindex compatibility in exporting
4437 Likewise, you can use @code{unexport} by itself to tell @code{make}
4438 @emph{not} to export variables by default.  Since this is the default
4439 behavior, you would only need to do this if @code{export} had been used
4440 by itself earlier (in an included makefile, perhaps).  You
4441 @strong{cannot} use @code{export} and @code{unexport} by themselves to
4442 have variables exported for some recipes and not for others.  The last
4443 @code{export} or @code{unexport} directive that appears by itself
4444 determines the behavior for the entire run of @code{make}.@refill
4446 @vindex MAKELEVEL
4447 @cindex recursion, level of
4448 As a special feature, the variable @code{MAKELEVEL} is changed when it
4449 is passed down from level to level.  This variable's value is a string
4450 which is the depth of the level as a decimal number.  The value is
4451 @samp{0} for the top-level @code{make}; @samp{1} for a sub-@code{make},
4452 @samp{2} for a sub-sub-@code{make}, and so on.  The incrementation
4453 happens when @code{make} sets up the environment for a recipe.@refill
4455 The main use of @code{MAKELEVEL} is to test it in a conditional
4456 directive (@pxref{Conditionals, ,Conditional Parts of Makefiles}); this
4457 way you can write a makefile that behaves one way if run recursively and
4458 another way if run directly by you.@refill
4460 @vindex MAKEFILES
4461 You can use the variable @code{MAKEFILES} to cause all sub-@code{make}
4462 commands to use additional makefiles.  The value of @code{MAKEFILES} is
4463 a whitespace-separated list of file names.  This variable, if defined in
4464 the outer-level makefile, is passed down through the environment; then
4465 it serves as a list of extra makefiles for the sub-@code{make} to read
4466 before the usual or specified ones.  @xref{MAKEFILES Variable, ,The
4467 Variable @code{MAKEFILES}}.@refill
4469 @node Options/Recursion, -w Option, Variables/Recursion, Recursion
4470 @subsection Communicating Options to a Sub-@code{make}
4471 @cindex options, and recursion
4472 @cindex recursion, and options
4474 @vindex MAKEFLAGS
4475 Flags such as @samp{-s} and @samp{-k} are passed automatically to the
4476 sub-@code{make} through the variable @code{MAKEFLAGS}.  This variable is
4477 set up automatically by @code{make} to contain the flag letters that
4478 @code{make} received.  Thus, if you do @w{@samp{make -ks}} then
4479 @code{MAKEFLAGS} gets the value @samp{ks}.@refill
4481 As a consequence, every sub-@code{make} gets a value for @code{MAKEFLAGS}
4482 in its environment.  In response, it takes the flags from that value and
4483 processes them as if they had been given as arguments.
4484 @xref{Options Summary, ,Summary of Options}.
4486 @cindex command line variable definitions, and recursion
4487 @cindex variables, command line, and recursion
4488 @cindex recursion, and command line variable definitions
4489 Likewise variables defined on the command line are passed to the
4490 sub-@code{make} through @code{MAKEFLAGS}.  Words in the value of
4491 @code{MAKEFLAGS} that contain @samp{=}, @code{make} treats as variable
4492 definitions just as if they appeared on the command line.
4493 @xref{Overriding, ,Overriding Variables}.
4495 @cindex @code{-C}, and recursion
4496 @cindex @code{-f}, and recursion
4497 @cindex @code{-o}, and recursion
4498 @cindex @code{-W}, and recursion
4499 @cindex @code{--directory}, and recursion
4500 @cindex @code{--file}, and recursion
4501 @cindex @code{--old-file}, and recursion
4502 @cindex @code{--assume-old}, and recursion
4503 @cindex @code{--assume-new}, and recursion
4504 @cindex @code{--new-file}, and recursion
4505 @cindex recursion, and @code{-C}
4506 @cindex recursion, and @code{-f}
4507 @cindex recursion, and @code{-o}
4508 @cindex recursion, and @code{-W}
4509 The options @samp{-C}, @samp{-f}, @samp{-o}, and @samp{-W} are not put
4510 into @code{MAKEFLAGS}; these options are not passed down.@refill
4512 @cindex @code{-j}, and recursion
4513 @cindex @code{--jobs}, and recursion
4514 @cindex recursion, and @code{-j}
4515 @cindex job slots, and recursion
4516 The @samp{-j} option is a special case (@pxref{Parallel, ,Parallel Execution}).
4517 If you set it to some numeric value @samp{N} and your operating system
4518 supports it (most any UNIX system will; others typically won't), the
4519 parent @code{make} and all the sub-@code{make}s will communicate to
4520 ensure that there are only @samp{N} jobs running at the same time
4521 between them all.  Note that any job that is marked recursive
4522 (@pxref{Instead of Execution, ,Instead of Executing Recipes})
4523 doesn't count against the total jobs (otherwise we could get @samp{N}
4524 sub-@code{make}s running and have no slots left over for any real work!)
4526 If your operating system doesn't support the above communication, then
4527 @samp{-j 1} is always put into @code{MAKEFLAGS} instead of the value you
4528 specified.  This is because if the @w{@samp{-j}} option were passed down
4529 to sub-@code{make}s, you would get many more jobs running in parallel
4530 than you asked for.  If you give @samp{-j} with no numeric argument,
4531 meaning to run as many jobs as possible in parallel, this is passed
4532 down, since multiple infinities are no more than one.@refill
4534 If you do not want to pass the other flags down, you must change the
4535 value of @code{MAKEFLAGS}, like this:
4537 @example
4538 subsystem:
4539         cd subdir && $(MAKE) MAKEFLAGS=
4540 @end example
4542 @vindex MAKEOVERRIDES
4543 The command line variable definitions really appear in the variable
4544 @code{MAKEOVERRIDES}, and @code{MAKEFLAGS} contains a reference to this
4545 variable.  If you do want to pass flags down normally, but don't want to
4546 pass down the command line variable definitions, you can reset
4547 @code{MAKEOVERRIDES} to empty, like this:
4549 @example
4550 MAKEOVERRIDES =
4551 @end example
4553 @noindent
4554 @cindex Arg list too long
4555 @cindex E2BIG
4556 This is not usually useful to do.  However, some systems have a small
4557 fixed limit on the size of the environment, and putting so much
4558 information into the value of @code{MAKEFLAGS} can exceed it.  If you
4559 see the error message @samp{Arg list too long}, this may be the problem.
4560 @findex .POSIX
4561 @cindex POSIX.2
4562 (For strict compliance with POSIX.2, changing @code{MAKEOVERRIDES} does
4563 not affect @code{MAKEFLAGS} if the special target @samp{.POSIX} appears
4564 in the makefile.  You probably do not care about this.)
4566 @vindex MFLAGS
4567 A similar variable @code{MFLAGS} exists also, for historical
4568 compatibility.  It has the same value as @code{MAKEFLAGS} except that it
4569 does not contain the command line variable definitions, and it always
4570 begins with a hyphen unless it is empty (@code{MAKEFLAGS} begins with a
4571 hyphen only when it begins with an option that has no single-letter
4572 version, such as @samp{--warn-undefined-variables}).  @code{MFLAGS} was
4573 traditionally used explicitly in the recursive @code{make} command, like
4574 this:
4576 @example
4577 subsystem:
4578         cd subdir && $(MAKE) $(MFLAGS)
4579 @end example
4581 @noindent
4582 but now @code{MAKEFLAGS} makes this usage redundant.  If you want your
4583 makefiles to be compatible with old @code{make} programs, use this
4584 technique; it will work fine with more modern @code{make} versions too.
4586 @cindex setting options from environment
4587 @cindex options, setting from environment
4588 @cindex setting options in makefiles
4589 @cindex options, setting in makefiles
4590 The @code{MAKEFLAGS} variable can also be useful if you want to have
4591 certain options, such as @samp{-k} (@pxref{Options Summary, ,Summary of
4592 Options}), set each time you run @code{make}.  You simply put a value for
4593 @code{MAKEFLAGS} in your environment.  You can also set @code{MAKEFLAGS} in
4594 a makefile, to specify additional flags that should also be in effect for
4595 that makefile.  (Note that you cannot use @code{MFLAGS} this way.  That
4596 variable is set only for compatibility; @code{make} does not interpret a
4597 value you set for it in any way.)
4599 When @code{make} interprets the value of @code{MAKEFLAGS} (either from the
4600 environment or from a makefile), it first prepends a hyphen if the value
4601 does not already begin with one.  Then it chops the value into words
4602 separated by blanks, and parses these words as if they were options given
4603 on the command line (except that @samp{-C}, @samp{-f}, @samp{-h},
4604 @samp{-o}, @samp{-W}, and their long-named versions are ignored; and there
4605 is no error for an invalid option).
4607 If you do put @code{MAKEFLAGS} in your environment, you should be sure not
4608 to include any options that will drastically affect the actions of
4609 @code{make} and undermine the purpose of makefiles and of @code{make}
4610 itself.  For instance, the @samp{-t}, @samp{-n}, and @samp{-q} options, if
4611 put in one of these variables, could have disastrous consequences and would
4612 certainly have at least surprising and probably annoying effects.@refill
4614 @node -w Option,  , Options/Recursion, Recursion
4615 @subsection The @samp{--print-directory} Option
4616 @cindex directories, printing them
4617 @cindex printing directories
4618 @cindex recursion, and printing directories
4620 If you use several levels of recursive @code{make} invocations, the
4621 @samp{-w} or @w{@samp{--print-directory}} option can make the output a
4622 lot easier to understand by showing each directory as @code{make}
4623 starts processing it and as @code{make} finishes processing it.  For
4624 example, if @samp{make -w} is run in the directory @file{/u/gnu/make},
4625 @code{make} will print a line of the form:@refill
4627 @example
4628 make: Entering directory `/u/gnu/make'.
4629 @end example
4631 @noindent
4632 before doing anything else, and a line of the form:
4634 @example
4635 make: Leaving directory `/u/gnu/make'.
4636 @end example
4638 @noindent
4639 when processing is completed.
4641 @cindex @code{-C}, and @code{-w}
4642 @cindex @code{--directory}, and @code{--print-directory}
4643 @cindex recursion, and @code{-w}
4644 @cindex @code{-w}, and @code{-C}
4645 @cindex @code{-w}, and recursion
4646 @cindex @code{--print-directory}, and @code{--directory}
4647 @cindex @code{--print-directory}, and recursion
4648 @cindex @code{--no-print-directory}
4649 @cindex @code{--print-directory}, disabling
4650 @cindex @code{-w}, disabling
4651 Normally, you do not need to specify this option because @samp{make}
4652 does it for you: @samp{-w} is turned on automatically when you use the
4653 @samp{-C} option, and in sub-@code{make}s.  @code{make} will not
4654 automatically turn on @samp{-w} if you also use @samp{-s}, which says to
4655 be silent, or if you use @samp{--no-print-directory} to explicitly
4656 disable it.
4658 @node Canned Recipes, Empty Recipes, Recursion, Recipes
4659 @section Defining Canned Recipes
4660 @cindex canned recipes
4661 @cindex recipes, canned
4662 @cindex sequences of commands
4663 @cindex commands, sequences of
4665 When the same sequence of commands is useful in making various
4666 targets, you can define it as a canned sequence with the @code{define}
4667 directive, and refer to the canned sequence from the recipes for those
4668 targets.  The canned sequence is actually a variable, so the name must
4669 not conflict with other variable names.
4671 Here is an example of defining a canned recipe:
4673 @example
4674 define run-yacc =
4675 yacc $(firstword $^)
4676 mv y.tab.c $@@
4677 endef
4678 @end example
4679 @cindex @code{yacc}
4681 @noindent
4682 Here @code{run-yacc} is the name of the variable being defined;
4683 @code{endef} marks the end of the definition; the lines in between are the
4684 commands.  The @code{define} directive does not expand variable references
4685 and function calls in the canned sequence; the @samp{$} characters,
4686 parentheses, variable names, and so on, all become part of the value of the
4687 variable you are defining.
4688 @xref{Multi-Line, ,Defining Multi-Line Variables},
4689 for a complete explanation of @code{define}.
4691 The first command in this example runs Yacc on the first prerequisite of
4692 whichever rule uses the canned sequence.  The output file from Yacc is
4693 always named @file{y.tab.c}.  The second command moves the output to the
4694 rule's target file name.
4696 To use the canned sequence, substitute the variable into the recipe of a
4697 rule.  You can substitute it like any other variable
4698 (@pxref{Reference, ,Basics of Variable References}).
4699 Because variables defined by @code{define} are recursively expanded
4700 variables, all the variable references you wrote inside the @code{define}
4701 are expanded now.  For example:
4703 @example
4704 foo.c : foo.y
4705         $(run-yacc)
4706 @end example
4708 @noindent
4709 @samp{foo.y} will be substituted for the variable @samp{$^} when it occurs in
4710 @code{run-yacc}'s value, and @samp{foo.c} for @samp{$@@}.@refill
4712 This is a realistic example, but this particular one is not needed in
4713 practice because @code{make} has an implicit rule to figure out these
4714 commands based on the file names involved
4715 (@pxref{Implicit Rules, ,Using Implicit Rules}).
4717 @cindex @@, and @code{define}
4718 @cindex -, and @code{define}
4719 @cindex +, and @code{define}
4720 In recipe execution, each line of a canned sequence is treated just as
4721 if the line appeared on its own in the rule, preceded by a tab.  In
4722 particular, @code{make} invokes a separate subshell for each line.  You
4723 can use the special prefix characters that affect command lines
4724 (@samp{@@}, @samp{-}, and @samp{+}) on each line of a canned sequence.
4725 @xref{Recipes, ,Writing Recipes in Rules}.
4726 For example, using this canned sequence:
4728 @example
4729 define frobnicate =
4730 @@echo "frobnicating target $@@"
4731 frob-step-1 $< -o $@@-step-1
4732 frob-step-2 $@@-step-1 -o $@@
4733 endef
4734 @end example
4736 @noindent
4737 @code{make} will not echo the first line, the @code{echo} command.
4738 But it @emph{will} echo the following two recipe lines.
4740 On the other hand, prefix characters on the recipe line that refers to
4741 a canned sequence apply to every line in the sequence.  So the rule:
4743 @example
4744 frob.out: frob.in
4745         @@$(frobnicate)
4746 @end example
4748 @noindent
4749 does not echo @emph{any} recipe lines.
4750 (@xref{Echoing, ,Recipe Echoing}, for a full explanation of @samp{@@}.)
4752 @node Empty Recipes,  , Canned Recipes, Recipes
4753 @section Using Empty Recipes
4754 @cindex empty recipes
4755 @cindex recipes, empty
4757 It is sometimes useful to define recipes which do nothing.  This is done
4758 simply by giving a recipe that consists of nothing but whitespace.  For
4759 example:
4761 @example
4762 target: ;
4763 @end example
4765 @noindent
4766 defines an empty recipe for @file{target}.  You could also use a line
4767 beginning with a recipe prefix character to define an empty recipe,
4768 but this would be confusing because such a line looks empty.
4770 @findex .DEFAULT@r{, and empty recipes}
4771 You may be wondering why you would want to define a recipe that
4772 does nothing.  The only reason this is useful is to prevent a target
4773 from getting implicit recipes (from implicit rules or the
4774 @code{.DEFAULT} special target; @pxref{Implicit Rules} and
4775 @pxref{Last Resort, ,Defining Last-Resort Default Rules}).@refill
4777 @c !!! another reason is for canonical stamp files:
4778 @ignore
4779 @example
4780 foo: stamp-foo ;
4781 stamp-foo: foo.in
4782         create foo frm foo.in
4783         touch $@
4784 @end example
4785 @end ignore
4787 You may be inclined to define empty recipes for targets that are
4788 not actual files, but only exist so that their prerequisites can be
4789 remade.  However, this is not the best way to do that, because the
4790 prerequisites may not be remade properly if the target file actually does exist.
4791 @xref{Phony Targets, ,Phony Targets}, for a better way to do this.
4793 @node Using Variables, Conditionals, Recipes, Top
4794 @chapter How to Use Variables
4795 @cindex variable
4796 @cindex value
4797 @cindex recursive variable expansion
4798 @cindex simple variable expansion
4800 A @dfn{variable} is a name defined in a makefile to represent a string
4801 of text, called the variable's @dfn{value}.  These values are
4802 substituted by explicit request into targets, prerequisites, recipes,
4803 and other parts of the makefile.  (In some other versions of @code{make},
4804 variables are called @dfn{macros}.)
4805 @cindex macro
4807 Variables and functions in all parts of a makefile are expanded when
4808 read, except for in recipes, the right-hand sides of variable
4809 definitions using @samp{=}, and the bodies of variable definitions
4810 using the @code{define} directive.@refill
4812 Variables can represent lists of file names, options to pass to compilers,
4813 programs to run, directories to look in for source files, directories to
4814 write output in, or anything else you can imagine.
4816 A variable name may be any sequence of characters not containing @samp{:},
4817 @samp{#}, @samp{=}, or leading or trailing whitespace.  However,
4818 variable names containing characters other than letters, numbers, and
4819 underscores should be avoided, as they may be given special meanings in the
4820 future, and with some shells they cannot be passed through the environment to a
4821 sub-@code{make}
4822 (@pxref{Variables/Recursion, ,Communicating Variables to a Sub-@code{make}}).
4824 Variable names are case-sensitive.  The names @samp{foo}, @samp{FOO},
4825 and @samp{Foo} all refer to different variables.
4827 It is traditional to use upper case letters in variable names, but we
4828 recommend using lower case letters for variable names that serve internal
4829 purposes in the makefile, and reserving upper case for parameters that
4830 control implicit rules or for parameters that the user should override with
4831 command options (@pxref{Overriding, ,Overriding Variables}).
4833 A few variables have names that are a single punctuation character or
4834 just a few characters.  These are the @dfn{automatic variables}, and
4835 they have particular specialized uses.  @xref{Automatic Variables}.
4837 @menu
4838 * Reference::                   How to use the value of a variable.
4839 * Flavors::                     Variables come in two flavors.
4840 * Advanced::                    Advanced features for referencing a variable.
4841 * Values::                      All the ways variables get their values.
4842 * Setting::                     How to set a variable in the makefile.
4843 * Appending::                   How to append more text to the old value
4844                                   of a variable.
4845 * Override Directive::          How to set a variable in the makefile even if
4846                                   the user has set it with a command argument.
4847 * Multi-Line::                  An alternate way to set a variable
4848                                   to a multi-line string.
4849 * Undefine Directive::          How to undefine a variable so that it appears
4850                                   as if it was never set.
4851 * Environment::                 Variable values can come from the environment.
4852 * Target-specific::             Variable values can be defined on a per-target
4853                                   basis.
4854 * Pattern-specific::            Target-specific variable values can be applied
4855                                   to a group of targets that match a pattern.
4856 * Suppressing Inheritance::     Suppress inheritance of variables.
4857 * Special Variables::           Variables with special meaning or behavior.
4858 @end menu
4860 @node Reference, Flavors, Using Variables, Using Variables
4861 @section Basics of Variable References
4862 @cindex variables, how to reference
4863 @cindex reference to variables
4864 @cindex @code{$}, in variable reference
4865 @cindex dollar sign (@code{$}), in variable reference
4867 To substitute a variable's value, write a dollar sign followed by the name
4868 of the variable in parentheses or braces: either @samp{$(foo)} or
4869 @samp{$@{foo@}} is a valid reference to the variable @code{foo}.  This
4870 special significance of @samp{$} is why you must write @samp{$$} to have
4871 the effect of a single dollar sign in a file name or recipe.
4873 Variable references can be used in any context: targets, prerequisites,
4874 recipes, most directives, and new variable values.  Here is an
4875 example of a common case, where a variable holds the names of all the
4876 object files in a program:
4878 @example
4879 @group
4880 objects = program.o foo.o utils.o
4881 program : $(objects)
4882         cc -o program $(objects)
4884 $(objects) : defs.h
4885 @end group
4886 @end example
4888 Variable references work by strict textual substitution.  Thus, the rule
4890 @example
4891 @group
4892 foo = c
4893 prog.o : prog.$(foo)
4894         $(foo)$(foo) -$(foo) prog.$(foo)
4895 @end group
4896 @end example
4898 @noindent
4899 could be used to compile a C program @file{prog.c}.  Since spaces before
4900 the variable value are ignored in variable assignments, the value of
4901 @code{foo} is precisely @samp{c}.  (Don't actually write your makefiles
4902 this way!)
4904 A dollar sign followed by a character other than a dollar sign,
4905 open-parenthesis or open-brace treats that single character as the
4906 variable name.  Thus, you could reference the variable @code{x} with
4907 @samp{$x}.  However, this practice is strongly discouraged, except in
4908 the case of the automatic variables (@pxref{Automatic Variables}).
4910 @node Flavors, Advanced, Reference, Using Variables
4911 @section The Two Flavors of Variables
4912 @cindex flavors of variables
4913 @cindex recursive variable expansion
4914 @cindex variables, flavors
4915 @cindex recursively expanded variables
4916 @cindex variables, recursively expanded
4918 There are two ways that a variable in GNU @code{make} can have a value;
4919 we call them the two @dfn{flavors} of variables.  The two flavors are
4920 distinguished in how they are defined and in what they do when expanded.
4922 @cindex =
4923 The first flavor of variable is a @dfn{recursively expanded} variable.
4924 Variables of this sort are defined by lines using @samp{=}
4925 (@pxref{Setting, ,Setting Variables}) or by the @code{define} directive
4926 (@pxref{Multi-Line, ,Defining Multi-Line Variables}).  The value you specify
4927 is installed verbatim; if it contains references to other variables,
4928 these references are expanded whenever this variable is substituted (in
4929 the course of expanding some other string).  When this happens, it is
4930 called @dfn{recursive expansion}.@refill
4932 For example,
4934 @example
4935 foo = $(bar)
4936 bar = $(ugh)
4937 ugh = Huh?
4939 all:;echo $(foo)
4940 @end example
4942 @noindent
4943 will echo @samp{Huh?}: @samp{$(foo)} expands to @samp{$(bar)} which
4944 expands to @samp{$(ugh)} which finally expands to @samp{Huh?}.@refill
4946 This flavor of variable is the only sort supported by other versions of
4947 @code{make}.  It has its advantages and its disadvantages.  An advantage
4948 (most would say) is that:
4950 @example
4951 CFLAGS = $(include_dirs) -O
4952 include_dirs = -Ifoo -Ibar
4953 @end example
4955 @noindent
4956 will do what was intended: when @samp{CFLAGS} is expanded in a recipe,
4957 it will expand to @samp{-Ifoo -Ibar -O}.  A major disadvantage is that you
4958 cannot append something on the end of a variable, as in
4960 @example
4961 CFLAGS = $(CFLAGS) -O
4962 @end example
4964 @noindent
4965 because it will cause an infinite loop in the variable expansion.
4966 (Actually @code{make} detects the infinite loop and reports an error.)
4967 @cindex loops in variable expansion
4968 @cindex variables, loops in expansion
4970 Another disadvantage is that any functions
4971 (@pxref{Functions, ,Functions for Transforming Text})
4972 referenced in the definition will be executed every time the variable is
4973 expanded.  This makes @code{make} run slower; worse, it causes the
4974 @code{wildcard} and @code{shell} functions to give unpredictable results
4975 because you cannot easily control when they are called, or even how many
4976 times.
4978 To avoid all the problems and inconveniences of recursively expanded
4979 variables, there is another flavor: simply expanded variables.
4981 @cindex simply expanded variables
4982 @cindex variables, simply expanded
4983 @cindex :=
4984 @dfn{Simply expanded variables} are defined by lines using @samp{:=}
4985 (@pxref{Setting, ,Setting Variables}).
4986 The value of a simply expanded variable is scanned
4987 once and for all, expanding any references to other variables and
4988 functions, when the variable is defined.  The actual value of the simply
4989 expanded variable is the result of expanding the text that you write.
4990 It does not contain any references to other variables; it contains their
4991 values @emph{as of the time this variable was defined}.  Therefore,
4993 @example
4994 x := foo
4995 y := $(x) bar
4996 x := later
4997 @end example
4999 @noindent
5000 is equivalent to
5002 @example
5003 y := foo bar
5004 x := later
5005 @end example
5007 When a simply expanded variable is referenced, its value is substituted
5008 verbatim.
5010 Here is a somewhat more complicated example, illustrating the use of
5011 @samp{:=} in conjunction with the @code{shell} function.
5012 (@xref{Shell Function, , The @code{shell} Function}.)  This example
5013 also shows use of the variable @code{MAKELEVEL}, which is changed
5014 when it is passed down from level to level.
5015 (@xref{Variables/Recursion, , Communicating Variables to a
5016 Sub-@code{make}}, for information about @code{MAKELEVEL}.)
5018 @vindex MAKELEVEL
5019 @vindex MAKE
5020 @example
5021 @group
5022 ifeq (0,$@{MAKELEVEL@})
5023 whoami    := $(shell whoami)
5024 host-type := $(shell arch)
5025 MAKE := $@{MAKE@} host-type=$@{host-type@} whoami=$@{whoami@}
5026 endif
5027 @end group
5028 @end example
5030 @noindent
5031 An advantage of this use of @samp{:=} is that a typical
5032 `descend into a directory' recipe then looks like this:
5034 @example
5035 @group
5036 $@{subdirs@}:
5037         $@{MAKE@} -C $@@ all
5038 @end group
5039 @end example
5041 Simply expanded variables generally make complicated makefile programming
5042 more predictable because they work like variables in most programming
5043 languages.  They allow you to redefine a variable using its own value (or
5044 its value processed in some way by one of the expansion functions) and to
5045 use the expansion functions much more efficiently
5046 (@pxref{Functions, ,Functions for Transforming Text}).
5048 @cindex spaces, in variable values
5049 @cindex whitespace, in variable values
5050 @cindex variables, spaces in values
5051 You can also use them to introduce controlled leading whitespace into
5052 variable values.  Leading whitespace characters are discarded from your
5053 input before substitution of variable references and function calls;
5054 this means you can include leading spaces in a variable value by
5055 protecting them with variable references, like this:
5057 @example
5058 nullstring :=
5059 space := $(nullstring) # end of the line
5060 @end example
5062 @noindent
5063 Here the value of the variable @code{space} is precisely one space.  The
5064 comment @w{@samp{# end of the line}} is included here just for clarity.
5065 Since trailing space characters are @emph{not} stripped from variable
5066 values, just a space at the end of the line would have the same effect
5067 (but be rather hard to read).  If you put whitespace at the end of a
5068 variable value, it is a good idea to put a comment like that at the end
5069 of the line to make your intent clear.  Conversely, if you do @emph{not}
5070 want any whitespace characters at the end of your variable value, you
5071 must remember not to put a random comment on the end of the line after
5072 some whitespace, such as this:
5074 @example
5075 dir := /foo/bar    # directory to put the frobs in
5076 @end example
5078 @noindent
5079 Here the value of the variable @code{dir} is @w{@samp{/foo/bar    }}
5080 (with four trailing spaces), which was probably not the intention.
5081 (Imagine something like @w{@samp{$(dir)/file}} with this definition!)
5083 @cindex conditional variable assignment
5084 @cindex variables, conditional assignment
5085 @cindex ?=
5086 There is another assignment operator for variables, @samp{?=}.  This
5087 is called a conditional variable assignment operator, because it only
5088 has an effect if the variable is not yet defined.  This statement:
5090 @example
5091 FOO ?= bar
5092 @end example
5094 @noindent
5095 is exactly equivalent to this
5096 (@pxref{Origin Function, ,The @code{origin} Function}):
5098 @example
5099 ifeq ($(origin FOO), undefined)
5100   FOO = bar
5101 endif
5102 @end example
5104 Note that a variable set to an empty value is still defined, so
5105 @samp{?=} will not set that variable.
5107 @node Advanced, Values, Flavors, Using Variables
5108 @section Advanced Features for Reference to Variables
5109 @cindex reference to variables
5111 This section describes some advanced features you can use to reference
5112 variables in more flexible ways.
5114 @menu
5115 * Substitution Refs::           Referencing a variable with
5116                                   substitutions on the value.
5117 * Computed Names::              Computing the name of the variable to refer to.
5118 @end menu
5120 @node Substitution Refs, Computed Names, Advanced, Advanced
5121 @subsection Substitution References
5122 @cindex modified variable reference
5123 @cindex substitution variable reference
5124 @cindex variables, modified reference
5125 @cindex variables, substitution reference
5127 @cindex variables, substituting suffix in
5128 @cindex suffix, substituting in variables
5129 A @dfn{substitution reference} substitutes the value of a variable with
5130 alterations that you specify.  It has the form
5131 @samp{$(@var{var}:@var{a}=@var{b})} (or
5132 @samp{$@{@var{var}:@var{a}=@var{b}@}}) and its meaning is to take the value
5133 of the variable @var{var}, replace every @var{a} at the end of a word with
5134 @var{b} in that value, and substitute the resulting string.
5136 When we say ``at the end of a word'', we mean that @var{a} must appear
5137 either followed by whitespace or at the end of the value in order to be
5138 replaced; other occurrences of @var{a} in the value are unaltered.  For
5139 example:@refill
5141 @example
5142 foo := a.o b.o c.o
5143 bar := $(foo:.o=.c)
5144 @end example
5146 @noindent
5147 sets @samp{bar} to @samp{a.c b.c c.c}.  @xref{Setting, ,Setting Variables}.
5149 A substitution reference is actually an abbreviation for use of the
5150 @code{patsubst} expansion function (@pxref{Text Functions, ,Functions for String Substitution and Analysis}).  We provide
5151 substitution references as well as @code{patsubst} for compatibility with
5152 other implementations of @code{make}.
5154 @findex patsubst
5155 Another type of substitution reference lets you use the full power of
5156 the @code{patsubst} function.  It has the same form
5157 @samp{$(@var{var}:@var{a}=@var{b})} described above, except that now
5158 @var{a} must contain a single @samp{%} character.  This case is
5159 equivalent to @samp{$(patsubst @var{a},@var{b},$(@var{var}))}.
5160 @xref{Text Functions, ,Functions for String Substitution and Analysis},
5161 for a description of the @code{patsubst} function.@refill
5163 @example
5164 @group
5165 @exdent For example:
5167 foo := a.o b.o c.o
5168 bar := $(foo:%.o=%.c)
5169 @end group
5170 @end example
5172 @noindent
5173 sets @samp{bar} to @samp{a.c b.c c.c}.
5175 @node Computed Names,  , Substitution Refs, Advanced
5176 @subsection Computed Variable Names
5177 @cindex nested variable reference
5178 @cindex computed variable name
5179 @cindex variables, computed names
5180 @cindex variables, nested references
5181 @cindex variables, @samp{$} in name
5182 @cindex @code{$}, in variable name
5183 @cindex dollar sign (@code{$}), in variable name
5185 Computed variable names are a complicated concept needed only for
5186 sophisticated makefile programming.  For most purposes you need not
5187 consider them, except to know that making a variable with a dollar sign
5188 in its name might have strange results.  However, if you are the type
5189 that wants to understand everything, or you are actually interested in
5190 what they do, read on.
5192 Variables may be referenced inside the name of a variable.  This is
5193 called a @dfn{computed variable name} or a @dfn{nested variable
5194 reference}.  For example,
5196 @example
5197 x = y
5198 y = z
5199 a := $($(x))
5200 @end example
5202 @noindent
5203 defines @code{a} as @samp{z}: the @samp{$(x)} inside @samp{$($(x))} expands
5204 to @samp{y}, so @samp{$($(x))} expands to @samp{$(y)} which in turn expands
5205 to @samp{z}.  Here the name of the variable to reference is not stated
5206 explicitly; it is computed by expansion of @samp{$(x)}.  The reference
5207 @samp{$(x)} here is nested within the outer variable reference.
5209 The previous example shows two levels of nesting, but any number of levels
5210 is possible.  For example, here are three levels:
5212 @example
5213 x = y
5214 y = z
5215 z = u
5216 a := $($($(x)))
5217 @end example
5219 @noindent
5220 Here the innermost @samp{$(x)} expands to @samp{y}, so @samp{$($(x))}
5221 expands to @samp{$(y)} which in turn expands to @samp{z}; now we have
5222 @samp{$(z)}, which becomes @samp{u}.
5224 References to recursively-expanded variables within a variable name are
5225 reexpanded in the usual fashion.  For example:
5227 @example
5228 x = $(y)
5229 y = z
5230 z = Hello
5231 a := $($(x))
5232 @end example
5234 @noindent
5235 defines @code{a} as @samp{Hello}: @samp{$($(x))} becomes @samp{$($(y))}
5236 which becomes @samp{$(z)} which becomes @samp{Hello}.
5238 Nested variable references can also contain modified references and
5239 function invocations (@pxref{Functions, ,Functions for Transforming Text}),
5240 just like any other reference.
5241 For example, using the @code{subst} function
5242 (@pxref{Text Functions, ,Functions for String Substitution and Analysis}):
5244 @example
5245 @group
5246 x = variable1
5247 variable2 := Hello
5248 y = $(subst 1,2,$(x))
5249 z = y
5250 a := $($($(z)))
5251 @end group
5252 @end example
5254 @noindent
5255 eventually defines @code{a} as @samp{Hello}.  It is doubtful that anyone
5256 would ever want to write a nested reference as convoluted as this one, but
5257 it works: @samp{$($($(z)))} expands to @samp{$($(y))} which becomes
5258 @samp{$($(subst 1,2,$(x)))}.  This gets the value @samp{variable1} from
5259 @code{x} and changes it by substitution to @samp{variable2}, so that the
5260 entire string becomes @samp{$(variable2)}, a simple variable reference
5261 whose value is @samp{Hello}.@refill
5263 A computed variable name need not consist entirely of a single variable
5264 reference.  It can contain several variable references, as well as some
5265 invariant text.  For example,
5267 @example
5268 @group
5269 a_dirs := dira dirb
5270 1_dirs := dir1 dir2
5271 @end group
5273 @group
5274 a_files := filea fileb
5275 1_files := file1 file2
5276 @end group
5278 @group
5279 ifeq "$(use_a)" "yes"
5280 a1 := a
5281 else
5282 a1 := 1
5283 endif
5284 @end group
5286 @group
5287 ifeq "$(use_dirs)" "yes"
5288 df := dirs
5289 else
5290 df := files
5291 endif
5293 dirs := $($(a1)_$(df))
5294 @end group
5295 @end example
5297 @noindent
5298 will give @code{dirs} the same value as @code{a_dirs}, @code{1_dirs},
5299 @code{a_files} or @code{1_files} depending on the settings of @code{use_a}
5300 and @code{use_dirs}.@refill
5302 Computed variable names can also be used in substitution references:
5304 @example
5305 @group
5306 a_objects := a.o b.o c.o
5307 1_objects := 1.o 2.o 3.o
5309 sources := $($(a1)_objects:.o=.c)
5310 @end group
5311 @end example
5313 @noindent
5314 defines @code{sources} as either @samp{a.c b.c c.c} or @samp{1.c 2.c 3.c},
5315 depending on the value of @code{a1}.
5317 The only restriction on this sort of use of nested variable references
5318 is that they cannot specify part of the name of a function to be called.
5319 This is because the test for a recognized function name is done before
5320 the expansion of nested references.  For example,
5322 @example
5323 @group
5324 ifdef do_sort
5325 func := sort
5326 else
5327 func := strip
5328 endif
5329 @end group
5331 @group
5332 bar := a d b g q c
5333 @end group
5335 @group
5336 foo := $($(func) $(bar))
5337 @end group
5338 @end example
5340 @noindent
5341 attempts to give @samp{foo} the value of the variable @samp{sort a d b g
5342 q c} or @samp{strip a d b g q c}, rather than giving @samp{a d b g q c}
5343 as the argument to either the @code{sort} or the @code{strip} function.
5344 This restriction could be removed in the future if that change is shown
5345 to be a good idea.
5347 You can also use computed variable names in the left-hand side of a
5348 variable assignment, or in a @code{define} directive, as in:
5350 @example
5351 dir = foo
5352 $(dir)_sources := $(wildcard $(dir)/*.c)
5353 define $(dir)_print =
5354 lpr $($(dir)_sources)
5355 endef
5356 @end example
5358 @noindent
5359 This example defines the variables @samp{dir}, @samp{foo_sources}, and
5360 @samp{foo_print}.
5362 Note that @dfn{nested variable references} are quite different from
5363 @dfn{recursively expanded variables}
5364 (@pxref{Flavors, ,The Two Flavors of Variables}), though both are
5365 used together in complex ways when doing makefile programming.@refill
5367 @node Values, Setting, Advanced, Using Variables
5368 @section How Variables Get Their Values
5369 @cindex variables, how they get their values
5370 @cindex value, how a variable gets it
5372 Variables can get values in several different ways:
5374 @itemize @bullet
5375 @item
5376 You can specify an overriding value when you run @code{make}.
5377 @xref{Overriding, ,Overriding Variables}.
5379 @item
5380 You can specify a value in the makefile, either
5381 with an assignment (@pxref{Setting, ,Setting Variables}) or with a
5382 verbatim definition (@pxref{Multi-Line, ,Defining Multi-Line Variables}).@refill
5384 @item
5385 Variables in the environment become @code{make} variables.
5386 @xref{Environment, ,Variables from the Environment}.
5388 @item
5389 Several @dfn{automatic} variables are given new values for each rule.
5390 Each of these has a single conventional use.
5391 @xref{Automatic Variables}.
5393 @item
5394 Several variables have constant initial values.
5395 @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
5396 @end itemize
5398 @node Setting, Appending, Values, Using Variables
5399 @section Setting Variables
5400 @cindex setting variables
5401 @cindex variables, setting
5402 @cindex =
5403 @cindex :=
5404 @cindex ?=
5406 To set a variable from the makefile, write a line starting with the
5407 variable name followed by @samp{=} or @samp{:=}.  Whatever follows the
5408 @samp{=} or @samp{:=} on the line becomes the value.  For example,
5410 @example
5411 objects = main.o foo.o bar.o utils.o
5412 @end example
5414 @noindent
5415 defines a variable named @code{objects}.  Whitespace around the variable
5416 name and immediately after the @samp{=} is ignored.
5418 Variables defined with @samp{=} are @dfn{recursively expanded} variables.
5419 Variables defined with @samp{:=} are @dfn{simply expanded} variables; these
5420 definitions can contain variable references which will be expanded before
5421 the definition is made.  @xref{Flavors, ,The Two Flavors of Variables}.
5423 The variable name may contain function and variable references, which
5424 are expanded when the line is read to find the actual variable name to use.
5426 There is no limit on the length of the value of a variable except the
5427 amount of memory on the computer.  When a variable definition is long,
5428 it is a good idea to break it into several lines by inserting
5429 backslash-newline at convenient places in the definition.  This will
5430 make the makefile easier to read.  Every backslash-newline, along with
5431 any leading whitespace on the following line, will be replaced by a
5432 single space in the value of the variable.
5434 Most variable names are considered to have the empty string as a value if
5435 you have never set them.  Several variables have built-in initial values
5436 that are not empty, but you can set them in the usual ways
5437 (@pxref{Implicit Variables, ,Variables Used by Implicit Rules}).
5438 Several special variables are set
5439 automatically to a new value for each rule; these are called the
5440 @dfn{automatic} variables (@pxref{Automatic Variables}).
5442 If you'd like a variable to be set to a value only if it's not already
5443 set, then you can use the shorthand operator @samp{?=} instead of
5444 @samp{=}.  These two settings of the variable @samp{FOO} are identical
5445 (@pxref{Origin Function, ,The @code{origin} Function}):
5447 @example
5448 FOO ?= bar
5449 @end example
5451 @noindent
5454 @example
5455 ifeq ($(origin FOO), undefined)
5456 FOO = bar
5457 endif
5458 @end example
5460 @node Appending, Override Directive, Setting, Using Variables
5461 @section Appending More Text to Variables
5462 @cindex +=
5463 @cindex appending to variables
5464 @cindex variables, appending to
5466 Often it is useful to add more text to the value of a variable already defined.
5467 You do this with a line containing @samp{+=}, like this:
5469 @example
5470 objects += another.o
5471 @end example
5473 @noindent
5474 This takes the value of the variable @code{objects}, and adds the text
5475 @samp{another.o} to it (preceded by a single space).  Thus:
5477 @example
5478 objects = main.o foo.o bar.o utils.o
5479 objects += another.o
5480 @end example
5482 @noindent
5483 sets @code{objects} to @samp{main.o foo.o bar.o utils.o another.o}.
5485 Using @samp{+=} is similar to:
5487 @example
5488 objects = main.o foo.o bar.o utils.o
5489 objects := $(objects) another.o
5490 @end example
5492 @noindent
5493 but differs in ways that become important when you use more complex values.
5495 When the variable in question has not been defined before, @samp{+=}
5496 acts just like normal @samp{=}: it defines a recursively-expanded
5497 variable.  However, when there @emph{is} a previous definition, exactly
5498 what @samp{+=} does depends on what flavor of variable you defined
5499 originally.  @xref{Flavors, ,The Two Flavors of Variables}, for an
5500 explanation of the two flavors of variables.
5502 When you add to a variable's value with @samp{+=}, @code{make} acts
5503 essentially as if you had included the extra text in the initial
5504 definition of the variable.  If you defined it first with @samp{:=},
5505 making it a simply-expanded variable, @samp{+=} adds to that
5506 simply-expanded definition, and expands the new text before appending it
5507 to the old value just as @samp{:=} does
5508 (see @ref{Setting, ,Setting Variables}, for a full explanation of @samp{:=}).
5509 In fact,
5511 @example
5512 variable := value
5513 variable += more
5514 @end example
5516 @noindent
5517 is exactly equivalent to:
5519 @noindent
5520 @example
5521 variable := value
5522 variable := $(variable) more
5523 @end example
5525 On the other hand, when you use @samp{+=} with a variable that you defined
5526 first to be recursively-expanded using plain @samp{=}, @code{make} does
5527 something a bit different.  Recall that when you define a
5528 recursively-expanded variable, @code{make} does not expand the value you set
5529 for variable and function references immediately.  Instead it stores the text
5530 verbatim, and saves these variable and function references to be expanded
5531 later, when you refer to the new variable (@pxref{Flavors, ,The Two Flavors
5532 of Variables}).  When you use @samp{+=} on a recursively-expanded variable,
5533 it is this unexpanded text to which @code{make} appends the new text you
5534 specify.
5536 @example
5537 @group
5538 variable = value
5539 variable += more
5540 @end group
5541 @end example
5543 @noindent
5544 is roughly equivalent to:
5546 @example
5547 @group
5548 temp = value
5549 variable = $(temp) more
5550 @end group
5551 @end example
5553 @noindent
5554 except that of course it never defines a variable called @code{temp}.
5555 The importance of this comes when the variable's old value contains
5556 variable references.  Take this common example:
5558 @example
5559 CFLAGS = $(includes) -O
5560 @dots{}
5561 CFLAGS += -pg # enable profiling
5562 @end example
5564 @noindent
5565 The first line defines the @code{CFLAGS} variable with a reference to another
5566 variable, @code{includes}.  (@code{CFLAGS} is used by the rules for C
5567 compilation; @pxref{Catalogue of Rules, ,Catalogue of Implicit Rules}.)
5568 Using @samp{=} for the definition makes @code{CFLAGS} a recursively-expanded
5569 variable, meaning @w{@samp{$(includes) -O}} is @emph{not} expanded when
5570 @code{make} processes the definition of @code{CFLAGS}.  Thus, @code{includes}
5571 need not be defined yet for its value to take effect.  It only has to be
5572 defined before any reference to @code{CFLAGS}.  If we tried to append to the
5573 value of @code{CFLAGS} without using @samp{+=}, we might do it like this:
5575 @example
5576 CFLAGS := $(CFLAGS) -pg # enable profiling
5577 @end example
5579 @noindent
5580 This is pretty close, but not quite what we want.  Using @samp{:=}
5581 redefines @code{CFLAGS} as a simply-expanded variable; this means
5582 @code{make} expands the text @w{@samp{$(CFLAGS) -pg}} before setting the
5583 variable.  If @code{includes} is not yet defined, we get @w{@samp{ -O
5584 -pg}}, and a later definition of @code{includes} will have no effect.
5585 Conversely, by using @samp{+=} we set @code{CFLAGS} to the
5586 @emph{unexpanded} value @w{@samp{$(includes) -O -pg}}.  Thus we preserve
5587 the reference to @code{includes}, so if that variable gets defined at
5588 any later point, a reference like @samp{$(CFLAGS)} still uses its
5589 value.
5591 @node Override Directive, Multi-Line, Appending, Using Variables
5592 @section The @code{override} Directive
5593 @findex override
5594 @cindex overriding with @code{override}
5595 @cindex variables, overriding
5597 If a variable has been set with a command argument
5598 (@pxref{Overriding, ,Overriding Variables}),
5599 then ordinary assignments in the makefile are ignored.  If you want to set
5600 the variable in the makefile even though it was set with a command
5601 argument, you can use an @code{override} directive, which is a line that
5602 looks like this:@refill
5604 @example
5605 override @var{variable} = @var{value}
5606 @end example
5608 @noindent
5611 @example
5612 override @var{variable} := @var{value}
5613 @end example
5615 To append more text to a variable defined on the command line, use:
5617 @example
5618 override @var{variable} += @var{more text}
5619 @end example
5621 @noindent
5622 @xref{Appending, ,Appending More Text to Variables}.
5624 Variable assignments marked with the @code{override} flag have a
5625 higher priority than all other assignments, except another
5626 @code{override}.  Subsequent assignments or appends to this variable
5627 which are not marked @code{override} will be ignored.
5629 The @code{override} directive was not invented for escalation in the war
5630 between makefiles and command arguments.  It was invented so you can alter
5631 and add to values that the user specifies with command arguments.
5633 For example, suppose you always want the @samp{-g} switch when you run the
5634 C compiler, but you would like to allow the user to specify the other
5635 switches with a command argument just as usual.  You could use this
5636 @code{override} directive:
5638 @example
5639 override CFLAGS += -g
5640 @end example
5642 You can also use @code{override} directives with @code{define} directives.
5643 This is done as you might expect:
5645 @example
5646 override define foo =
5648 endef
5649 @end example
5651 @noindent
5652 @iftex
5653 See the next section for information about @code{define}.
5654 @end iftex
5655 @ifnottex
5656 @xref{Multi-Line, ,Defining Multi-Line Variables}.
5657 @end ifnottex
5659 @node Multi-Line, Undefine Directive, Override Directive, Using Variables
5660 @section Defining Multi-Line Variables
5661 @findex define
5662 @findex endef
5663 @cindex multi-line variable definition
5664 @cindex variables, multi-line
5665 @cindex verbatim variable definition
5666 @cindex defining variables verbatim
5667 @cindex variables, defining verbatim
5669 Another way to set the value of a variable is to use the @code{define}
5670 directive.  This directive has an unusual syntax which allows newline
5671 characters to be included in the value, which is convenient for
5672 defining both canned sequences of commands (@pxref{Canned Recipes,
5673 ,Defining Canned Recipes}), and also sections of makefile syntax to
5674 use with @code{eval} (@pxref{Eval Function}).@refill
5676 The @code{define} directive is followed on the same line by the name
5677 of the variable being defined and an (optional) assignment operator,
5678 and nothing more.  The value to give the variable appears on the
5679 following lines.  The end of the value is marked by a line containing
5680 just the word @code{endef}.  Aside from this difference in syntax,
5681 @code{define} works just like any other variable definition.  The
5682 variable name may contain function and variable references, which are
5683 expanded when the directive is read to find the actual variable name
5684 to use.
5686 You may omit the variable assignment operator if you prefer.  If
5687 omitted, @code{make} assumes it to be @samp{=} and creates a
5688 recursively-expanded variable (@pxref{Flavors, ,The Two Flavors of Variables}).
5689 When using a @samp{+=} operator, the value is appended to the previous
5690 value as with any other append operation: with a single space
5691 separating the old and new values.
5693 You may nest @code{define} directives: @code{make} will keep track of
5694 nested directives and report an error if they are not all properly
5695 closed with @code{endef}.  Note that lines beginning with the recipe
5696 prefix character are considered part of a recipe, so any @code{define}
5697 or @code{endef} strings appearing on such a line will not be
5698 considered @code{make} directives.
5700 @example
5701 define two-lines =
5702 echo foo
5703 echo $(bar)
5704 endef
5705 @end example
5707 The value in an ordinary assignment cannot contain a newline; but the
5708 newlines that separate the lines of the value in a @code{define} become
5709 part of the variable's value (except for the final newline which precedes
5710 the @code{endef} and is not considered part of the value).@refill
5712 @need 800
5713 When used in a recipe, the previous example is functionally equivalent
5714 to this:
5716 @example
5717 two-lines = echo foo; echo $(bar)
5718 @end example
5720 @noindent
5721 since two commands separated by semicolon behave much like two separate
5722 shell commands.  However, note that using two separate lines means
5723 @code{make} will invoke the shell twice, running an independent subshell
5724 for each line.  @xref{Execution, ,Recipe Execution}.
5726 If you want variable definitions made with @code{define} to take
5727 precedence over command-line variable definitions, you can use the
5728 @code{override} directive together with @code{define}:
5730 @example
5731 override define two-lines =
5733 $(bar)
5734 endef
5735 @end example
5737 @noindent
5738 @xref{Override Directive, ,The @code{override} Directive}.
5740 @node Undefine Directive, Environment, Multi-Line, Using Variables
5741 @section Undefining Variables
5742 @findex undefine
5743 @cindex undefining variable
5745 If you want to clear a variable, setting its value to empty is usually
5746 sufficient. Expanding such a variable will yield the same result (empty
5747 string) regardless of whether it was set or not. However, if you are
5748 using the @code{flavor} (@pxref{Flavor Function}) and
5749 @code{origin} (@pxref{Origin Function}) functions, there is a difference
5750 between a variable that was never set and a variable with an empty value.
5751 In such situations you may want to use the @code{undefine} directive to
5752 make a variable appear as if it was never set. For example:
5754 @example
5755 foo := foo
5756 bar = bar
5758 undefine foo
5759 undefine bar
5761 $(info $(origin foo))
5762 $(info $(flavor bar))
5763 @end example
5765 This example will print ``undefined'' for both variables.
5767 If you want to undefine a command-line variable definition, you can use
5768 the @code{override} directive together with @code{undefine}, similar to
5769 how this is done for variable definitions:
5771 @example
5772 override undefine CFLAGS
5773 @end example
5775 @node Environment, Target-specific, Undefine Directive, Using Variables
5776 @section Variables from the Environment
5778 @cindex variables, environment
5779 @cindex environment
5780 Variables in @code{make} can come from the environment in which
5781 @code{make} is run.  Every environment variable that @code{make} sees
5782 when it starts up is transformed into a @code{make} variable with the
5783 same name and value.  However, an explicit assignment in the makefile,
5784 or with a command argument, overrides the environment.  (If the
5785 @samp{-e} flag is specified, then values from the environment override
5786 assignments in the makefile.  @xref{Options Summary, ,Summary of
5787 Options}.  But this is not recommended practice.)
5789 Thus, by setting the variable @code{CFLAGS} in your environment, you can
5790 cause all C compilations in most makefiles to use the compiler switches you
5791 prefer.  This is safe for variables with standard or conventional meanings
5792 because you know that no makefile will use them for other things.  (Note
5793 this is not totally reliable; some makefiles set @code{CFLAGS} explicitly
5794 and therefore are not affected by the value in the environment.)
5796 When @code{make} runs a recipe, variables defined in the
5797 makefile are placed into the environment of each shell.  This allows
5798 you to pass values to sub-@code{make} invocations (@pxref{Recursion,
5799 ,Recursive Use of @code{make}}).  By default, only variables that came
5800 from the environment or the command line are passed to recursive
5801 invocations.  You can use the @code{export} directive to pass other
5802 variables.  @xref{Variables/Recursion, , Communicating Variables to a
5803 Sub-@code{make}}, for full details.
5805 Other use of variables from the environment is not recommended.  It is not
5806 wise for makefiles to depend for their functioning on environment variables
5807 set up outside their control, since this would cause different users to get
5808 different results from the same makefile.  This is against the whole
5809 purpose of most makefiles.
5811 @cindex SHELL, import from environment
5812 Such problems would be especially likely with the variable
5813 @code{SHELL}, which is normally present in the environment to specify
5814 the user's choice of interactive shell.  It would be very undesirable
5815 for this choice to affect @code{make}; so, @code{make} handles the
5816 @code{SHELL} environment variable in a special way; see @ref{Choosing
5817 the Shell}.@refill
5819 @node Target-specific, Pattern-specific, Environment, Using Variables
5820 @section Target-specific Variable Values
5821 @cindex target-specific variables
5822 @cindex variables, target-specific
5824 Variable values in @code{make} are usually global; that is, they are the
5825 same regardless of where they are evaluated (unless they're reset, of
5826 course).  One exception to that is automatic variables
5827 (@pxref{Automatic Variables}).
5829 The other exception is @dfn{target-specific variable values}.  This
5830 feature allows you to define different values for the same variable,
5831 based on the target that @code{make} is currently building.  As with
5832 automatic variables, these values are only available within the context
5833 of a target's recipe (and in other target-specific assignments).
5835 Set a target-specific variable value like this:
5837 @example
5838 @var{target} @dots{} : @var{variable-assignment}
5839 @end example
5841 Target-specific variable assignments can be prefixed with any or all of the
5842 special keywords @code{export}, @code{override}, or @code{private};
5843 these apply their normal behavior to this instance of the variable only.
5845 Multiple @var{target} values create a target-specific variable value for
5846 each member of the target list individually.
5848 The @var{variable-assignment} can be any valid form of assignment;
5849 recursive (@samp{=}), simple (@samp{:=}), appending (@samp{+=}), or
5850 conditional (@samp{?=}).  All variables that appear within the
5851 @var{variable-assignment} are evaluated within the context of the
5852 target: thus, any previously-defined target-specific variable values
5853 will be in effect.  Note that this variable is actually distinct from
5854 any ``global'' value: the two variables do not have to have the same
5855 flavor (recursive vs.@: simple).
5857 Target-specific variables have the same priority as any other makefile
5858 variable.  Variables provided on the command line (and in the
5859 environment if the @samp{-e} option is in force) will take precedence.
5860 Specifying the @code{override} directive will allow the target-specific
5861 variable value to be preferred.
5863 There is one more special feature of target-specific variables: when
5864 you define a target-specific variable that variable value is also in
5865 effect for all prerequisites of this target, and all their
5866 prerequisites, etc.@: (unless those prerequisites override that variable
5867 with their own target-specific variable value).  So, for example, a
5868 statement like this:
5870 @example
5871 prog : CFLAGS = -g
5872 prog : prog.o foo.o bar.o
5873 @end example
5875 @noindent
5876 will set @code{CFLAGS} to @samp{-g} in the recipe for @file{prog}, but
5877 it will also set @code{CFLAGS} to @samp{-g} in the recipes that create
5878 @file{prog.o}, @file{foo.o}, and @file{bar.o}, and any recipes which
5879 create their prerequisites.
5881 Be aware that a given prerequisite will only be built once per
5882 invocation of make, at most.  If the same file is a prerequisite of
5883 multiple targets, and each of those targets has a different value for
5884 the same target-specific variable, then the first target to be built
5885 will cause that prerequisite to be built and the prerequisite will
5886 inherit the target-specific value from the first target.  It will
5887 ignore the target-specific values from any other targets.
5889 @node Pattern-specific, Suppressing Inheritance, Target-specific, Using Variables
5890 @section Pattern-specific Variable Values
5891 @cindex pattern-specific variables
5892 @cindex variables, pattern-specific
5894 In addition to target-specific variable values
5895 (@pxref{Target-specific, ,Target-specific Variable Values}), GNU
5896 @code{make} supports pattern-specific variable values.  In this form,
5897 the variable is defined for any target that matches the pattern
5898 specified.
5900 Set a pattern-specific variable value like this:
5902 @example
5903 @var{pattern} @dots{} : @var{variable-assignment}
5904 @end example
5905 where @var{pattern} is a %-pattern.  As with target-specific variable
5906 values, multiple @var{pattern} values create a pattern-specific variable
5907 value for each pattern individually.  The @var{variable-assignment} can
5908 be any valid form of assignment.  Any command line variable setting will
5909 take precedence, unless @code{override} is specified.
5911 For example:
5913 @example
5914 %.o : CFLAGS = -O
5915 @end example
5917 @noindent
5918 will assign @code{CFLAGS} the value of @samp{-O} for all targets
5919 matching the pattern @code{%.o}.
5921 If a target matches more than one pattern, the matching pattern-specific
5922 variables with longer stems are interpreted first. This results in more
5923 specific variables taking precedence over the more generic ones, for
5924 example:
5926 @example
5927 %.o: %.c
5928         $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@@
5930 lib/%.o: CFLAGS := -fPIC -g
5931 %.o: CFLAGS := -g
5933 all: foo.o lib/bar.o
5934 @end example
5936 In this example the first definition of the @code{CFLAGS} variable
5937 will be used to update @file{lib/bar.o} even though the second one
5938 also applies to this target. Pattern-specific variables which result
5939 in the same stem length are considered in the order in which they
5940 were defined in the makefile.
5942 Pattern-specific variables are searched after any target-specific
5943 variables defined explicitly for that target, and before target-specific
5944 variables defined for the parent target.
5946 @node Suppressing Inheritance, Special Variables, Pattern-specific, Using Variables
5947 @section Suppressing Inheritance
5948 @findex private
5949 @cindex suppressing inheritance
5950 @cindex inheritance, suppressing
5952 As described in previous sections, @code{make} variables are inherited
5953 by prerequisites.  This capability allows you to modify the behavior
5954 of a prerequisite based on which targets caused it to be rebuilt.  For
5955 example, you might set a target-specific variable on a @code{debug}
5956 target, then running @samp{make debug} will cause that variable to be
5957 inherited by all prerequisites of @code{debug}, while just running
5958 @samp{make all} (for example) would not have that assignment.
5960 Sometimes, however, you may not want a variable to be inherited.  For
5961 these situations, @code{make} provides the @code{private} modifier.
5962 Although this modifier can be used with any variable assignment, it
5963 makes the most sense with target- and pattern-specific variables.  Any
5964 variable marked @code{private} will be visible to its local target but
5965 will not be inherited by prerequisites of that target.  A global
5966 variable marked @code{private} will be visible in the global scope but
5967 will not be inherited by any target, and hence will not be visible
5968 in any recipe.
5970 As an example, consider this makefile:
5971 @example
5972 EXTRA_CFLAGS =
5974 prog: private EXTRA_CFLAGS = -L/usr/local/lib
5975 prog: a.o b.o
5976 @end example
5978 Due to the @code{private} modifier, @code{a.o} and @code{b.o} will not
5979 inherit the @code{EXTRA_CFLAGS} variable assignment from the
5980 @code{progs} target.
5982 @node Special Variables,  , Suppressing Inheritance, Using Variables
5983 @comment  node-name,  next,  previous,  up
5984 @section Other Special Variables
5985 @cindex makefiles, and special variables
5986 @cindex special variables
5988 GNU @code{make} supports some variables that have special properties.
5990 @table @code
5992 @vindex MAKEFILE_LIST @r{(list of parsed makefiles)}
5993 @cindex makefiles, and @code{MAKEFILE_LIST} variable
5994 @cindex including (@code{MAKEFILE_LIST} variable)
5995 @item MAKEFILE_LIST
5996 Contains the name of each makefile that is parsed by @code{make}, in
5997 the order in which it was parsed.  The name is appended just
5998 before @code{make} begins to parse the makefile.  Thus, if the first
5999 thing a makefile does is examine the last word in this variable, it
6000 will be the name of the current makefile.  Once the current makefile
6001 has used @code{include}, however, the last word will be the
6002 just-included makefile.
6004 If a makefile named @code{Makefile} has this content:
6006 @example
6007 @group
6008 name1 := $(lastword $(MAKEFILE_LIST))
6010 include inc.mk
6012 name2 := $(lastword $(MAKEFILE_LIST))
6014 all:
6015         @@echo name1 = $(name1)
6016         @@echo name2 = $(name2)
6017 @end group
6018 @end example
6020 @noindent
6021 then you would expect to see this output:
6023 @example
6024 @group
6025 name1 = Makefile
6026 name2 = inc.mk
6027 @end group
6028 @end example
6030 @vindex .DEFAULT_GOAL @r{(define default goal)}
6031 @item .DEFAULT_GOAL
6032 Sets the default goal to be used if no targets were specified on the
6033 command line (@pxref{Goals, , Arguments to Specify the Goals}).  The
6034 @code{.DEFAULT_GOAL} variable allows you to discover the current
6035 default goal, restart the default goal selection algorithm by clearing
6036 its value, or to explicitly set the default goal.  The following
6037 example illustrates these cases:
6039 @example
6040 @group
6041 # Query the default goal.
6042 ifeq ($(.DEFAULT_GOAL),)
6043   $(warning no default goal is set)
6044 endif
6046 .PHONY: foo
6047 foo: ; @@echo $@@
6049 $(warning default goal is $(.DEFAULT_GOAL))
6051 # Reset the default goal.
6052 .DEFAULT_GOAL :=
6054 .PHONY: bar
6055 bar: ; @@echo $@@
6057 $(warning default goal is $(.DEFAULT_GOAL))
6059 # Set our own.
6060 .DEFAULT_GOAL := foo
6061 @end group
6062 @end example
6064 This makefile prints:
6066 @example
6067 @group
6068 no default goal is set
6069 default goal is foo
6070 default goal is bar
6072 @end group
6073 @end example
6075 Note that assigning more than one target name to @code{.DEFAULT_GOAL} is
6076 illegal and will result in an error.
6078 @vindex MAKE_RESTARTS @r{(number of times @code{make} has restarted)}
6079 @item MAKE_RESTARTS
6080 This variable is set only if this instance of @code{make} has
6081 restarted (@pxref{Remaking Makefiles, , How Makefiles Are Remade}): it
6082 will contain the number of times this instance has restarted.  Note
6083 this is not the same as recursion (counted by the @code{MAKELEVEL}
6084 variable).  You should not set, modify, or export this variable.
6086 @vindex .RECIPEPREFIX @r{(change the recipe prefix character)}
6087 @item .RECIPEPREFIX
6088 The first character of the value of this variable is used as the
6089 character make assumes is introducing a recipe line.  If the variable
6090 is empty (as it is by default) that character is the standard tab
6091 character.  For example, this is a valid makefile:
6093 @example
6094 @group
6095 .RECIPEPREFIX = >
6096 all:
6097 > @@echo Hello, world
6098 @end group
6099 @end example
6101 The value of @code{.RECIPEPREFIX} can be changed multiple times; once set
6102 it stays in effect for all rules parsed until it is modified.
6104 @vindex .VARIABLES @r{(list of variables)}
6105 @item .VARIABLES
6106 Expands to a list of the @emph{names} of all global variables defined
6107 so far.  This includes variables which have empty values, as well as
6108 built-in variables (@pxref{Implicit Variables, , Variables Used by
6109 Implicit Rules}), but does not include any variables which are only
6110 defined in a target-specific context.  Note that any value you assign
6111 to this variable will be ignored; it will always return its special
6112 value.
6114 @c @vindex .TARGETS @r{(list of targets)}
6115 @c @item .TARGETS
6116 @c The second special variable is @code{.TARGETS}.  When expanded, the
6117 @c value consists of a list of all targets defined in all makefiles read
6118 @c up until that point.  Note it's not enough for a file to be simply
6119 @c mentioned in the makefile to be listed in this variable, even if it
6120 @c would match an implicit rule and become an ``implicit target''.  The
6121 @c file must appear as a target, on the left-hand side of a ``:'', to be
6122 @c considered a target for the purposes of this variable.
6124 @vindex .FEATURES @r{(list of supported features)}
6125 @item .FEATURES
6126 Expands to a list of special features supported by this version of
6127 @code{make}.  Possible values include:
6129 @table @samp
6131 @item archives
6132 Supports @code{ar} (archive) files using special filename syntax.
6133 @xref{Archives, ,Using @code{make} to Update Archive Files}.
6135 @item check-symlink
6136 Supports the @code{-L} (@code{--check-symlink-times}) flag.
6137 @xref{Options Summary, ,Summary of Options}.
6139 @item else-if
6140 Supports ``else if'' non-nested conditionals.  @xref{Conditional
6141 Syntax, ,Syntax of Conditionals}.
6143 @item jobserver
6144 Supports ``job server'' enhanced parallel builds.  @xref{Parallel,
6145 ,Parallel Execution}.
6147 @item second-expansion
6148 Supports secondary expansion of prerequisite lists.
6150 @item order-only
6151 Supports order-only prerequisites.  @xref{Prerequisite Types, ,Types
6152 of Prerequisites}.
6154 @item target-specific
6155 Supports target-specific and pattern-specific variable assignments.
6156 @xref{Target-specific, ,Target-specific Variable Values}.
6158 @end table
6160 @vindex .INCLUDE_DIRS @r{(list of include directories)}
6161 @item .INCLUDE_DIRS
6162 Expands to a list of directories that @code{make} searches for
6163 included makefiles (@pxref{Include, , Including Other Makefiles}).
6165 @end table
6167 @node Conditionals, Functions, Using Variables, Top
6168 @chapter Conditional Parts of Makefiles
6170 @cindex conditionals
6171 A @dfn{conditional} directive causes part of a makefile to be obeyed
6172 or ignored depending on the values of variables.  Conditionals can
6173 compare the value of one variable to another, or the value of a
6174 variable to a constant string.  Conditionals control what @code{make}
6175 actually ``sees'' in the makefile, so they @emph{cannot} be used to
6176 control recipes at the time of execution.@refill
6178 @menu
6179 * Conditional Example::         Example of a conditional
6180 * Conditional Syntax::          The syntax of conditionals.
6181 * Testing Flags::               Conditionals that test flags.
6182 @end menu
6184 @node Conditional Example, Conditional Syntax, Conditionals, Conditionals
6185 @section Example of a Conditional
6187 The following example of a conditional tells @code{make} to use one
6188 set of libraries if the @code{CC} variable is @samp{gcc}, and a
6189 different set of libraries otherwise.  It works by controlling which
6190 of two recipe lines will be used for the rule.  The result is that
6191 @samp{CC=gcc} as an argument to @code{make} changes not only which
6192 compiler is used but also which libraries are linked.
6194 @example
6195 libs_for_gcc = -lgnu
6196 normal_libs =
6198 foo: $(objects)
6199 ifeq ($(CC),gcc)
6200         $(CC) -o foo $(objects) $(libs_for_gcc)
6201 else
6202         $(CC) -o foo $(objects) $(normal_libs)
6203 endif
6204 @end example
6206 This conditional uses three directives: one @code{ifeq}, one @code{else}
6207 and one @code{endif}.
6209 The @code{ifeq} directive begins the conditional, and specifies the
6210 condition.  It contains two arguments, separated by a comma and surrounded
6211 by parentheses.  Variable substitution is performed on both arguments and
6212 then they are compared.  The lines of the makefile following the
6213 @code{ifeq} are obeyed if the two arguments match; otherwise they are
6214 ignored.
6216 The @code{else} directive causes the following lines to be obeyed if the
6217 previous conditional failed.  In the example above, this means that the
6218 second alternative linking command is used whenever the first alternative
6219 is not used.  It is optional to have an @code{else} in a conditional.
6221 The @code{endif} directive ends the conditional.  Every conditional must
6222 end with an @code{endif}.  Unconditional makefile text follows.
6224 As this example illustrates, conditionals work at the textual level:
6225 the lines of the conditional are treated as part of the makefile, or
6226 ignored, according to the condition.  This is why the larger syntactic
6227 units of the makefile, such as rules, may cross the beginning or the
6228 end of the conditional.
6230 When the variable @code{CC} has the value @samp{gcc}, the above example has
6231 this effect:
6233 @example
6234 foo: $(objects)
6235         $(CC) -o foo $(objects) $(libs_for_gcc)
6236 @end example
6238 @noindent
6239 When the variable @code{CC} has any other value, the effect is this:
6241 @example
6242 foo: $(objects)
6243         $(CC) -o foo $(objects) $(normal_libs)
6244 @end example
6246 Equivalent results can be obtained in another way by conditionalizing a
6247 variable assignment and then using the variable unconditionally:
6249 @example
6250 libs_for_gcc = -lgnu
6251 normal_libs =
6253 ifeq ($(CC),gcc)
6254   libs=$(libs_for_gcc)
6255 else
6256   libs=$(normal_libs)
6257 endif
6259 foo: $(objects)
6260         $(CC) -o foo $(objects) $(libs)
6261 @end example
6263 @node Conditional Syntax, Testing Flags, Conditional Example, Conditionals
6264 @section Syntax of Conditionals
6265 @findex ifdef
6266 @findex ifeq
6267 @findex ifndef
6268 @findex ifneq
6269 @findex else
6270 @findex endif
6272 The syntax of a simple conditional with no @code{else} is as follows:
6274 @example
6275 @var{conditional-directive}
6276 @var{text-if-true}
6277 endif
6278 @end example
6280 @noindent
6281 The @var{text-if-true} may be any lines of text, to be considered as part
6282 of the makefile if the condition is true.  If the condition is false, no
6283 text is used instead.
6285 The syntax of a complex conditional is as follows:
6287 @example
6288 @var{conditional-directive}
6289 @var{text-if-true}
6290 else
6291 @var{text-if-false}
6292 endif
6293 @end example
6297 @example
6298 @var{conditional-directive}
6299 @var{text-if-one-is-true}
6300 else @var{conditional-directive}
6301 @var{text-if-true}
6302 else
6303 @var{text-if-false}
6304 endif
6305 @end example
6307 @noindent
6308 There can be as many ``@code{else} @var{conditional-directive}''
6309 clauses as necessary.  Once a given condition is true,
6310 @var{text-if-true} is used and no other clause is used; if no
6311 condition is true then @var{text-if-false} is used.  The
6312 @var{text-if-true} and @var{text-if-false} can be any number of lines
6313 of text.
6315 The syntax of the @var{conditional-directive} is the same whether the
6316 conditional is simple or complex; after an @code{else} or not.  There
6317 are four different directives that test different conditions.  Here is
6318 a table of them:
6320 @table @code
6321 @item ifeq (@var{arg1}, @var{arg2})
6322 @itemx ifeq '@var{arg1}' '@var{arg2}'
6323 @itemx ifeq "@var{arg1}" "@var{arg2}"
6324 @itemx ifeq "@var{arg1}" '@var{arg2}'
6325 @itemx ifeq '@var{arg1}' "@var{arg2}"
6326 Expand all variable references in @var{arg1} and @var{arg2} and
6327 compare them.  If they are identical, the @var{text-if-true} is
6328 effective; otherwise, the @var{text-if-false}, if any, is effective.
6330 Often you want to test if a variable has a non-empty value.  When the
6331 value results from complex expansions of variables and functions,
6332 expansions you would consider empty may actually contain whitespace
6333 characters and thus are not seen as empty.  However, you can use the
6334 @code{strip} function (@pxref{Text Functions}) to avoid interpreting
6335 whitespace as a non-empty value.  For example:
6337 @example
6338 @group
6339 ifeq ($(strip $(foo)),)
6340 @var{text-if-empty}
6341 endif
6342 @end group
6343 @end example
6345 @noindent
6346 will evaluate @var{text-if-empty} even if the expansion of
6347 @code{$(foo)} contains whitespace characters.
6349 @item ifneq (@var{arg1}, @var{arg2})
6350 @itemx ifneq '@var{arg1}' '@var{arg2}'
6351 @itemx ifneq "@var{arg1}" "@var{arg2}"
6352 @itemx ifneq "@var{arg1}" '@var{arg2}'
6353 @itemx ifneq '@var{arg1}' "@var{arg2}"
6354 Expand all variable references in @var{arg1} and @var{arg2} and
6355 compare them.  If they are different, the @var{text-if-true} is
6356 effective; otherwise, the @var{text-if-false}, if any, is effective.
6358 @item ifdef @var{variable-name}
6359 The @code{ifdef} form takes the @emph{name} of a variable as its
6360 argument, not a reference to a variable.  The value of that variable
6361 has a non-empty value, the @var{text-if-true} is effective; otherwise,
6362 the @var{text-if-false}, if any, is effective.  Variables that have
6363 never been defined have an empty value.  The text @var{variable-name}
6364 is expanded, so it could be a variable or function that expands
6365 to the name of a variable.  For example:
6367 @example
6368 bar = true
6369 foo = bar
6370 ifdef $(foo)
6371 frobozz = yes
6372 endif
6373 @end example
6375 The variable reference @code{$(foo)} is expanded, yielding @code{bar},
6376 which is considered to be the name of a variable.  The variable
6377 @code{bar} is not expanded, but its value is examined to determine if
6378 it is non-empty.
6380 Note that @code{ifdef} only tests whether a variable has a value.  It
6381 does not expand the variable to see if that value is nonempty.
6382 Consequently, tests using @code{ifdef} return true for all definitions
6383 except those like @code{foo =}.  To test for an empty value, use
6384 @w{@code{ifeq ($(foo),)}}.  For example,
6386 @example
6387 bar =
6388 foo = $(bar)
6389 ifdef foo
6390 frobozz = yes
6391 else
6392 frobozz = no
6393 endif
6394 @end example
6396 @noindent
6397 sets @samp{frobozz} to @samp{yes}, while:
6399 @example
6400 foo =
6401 ifdef foo
6402 frobozz = yes
6403 else
6404 frobozz = no
6405 endif
6406 @end example
6408 @noindent
6409 sets @samp{frobozz} to @samp{no}.
6411 @item ifndef @var{variable-name}
6412 If the variable @var{variable-name} has an empty value, the
6413 @var{text-if-true} is effective; otherwise, the @var{text-if-false},
6414 if any, is effective.  The rules for expansion and testing of
6415 @var{variable-name} are identical to the @code{ifdef} directive.
6416 @end table
6418 Extra spaces are allowed and ignored at the beginning of the
6419 conditional directive line, but a tab is not allowed.  (If the line
6420 begins with a tab, it will be considered part of a recipe for a rule.)
6421 Aside from this, extra spaces or tabs may be inserted with no effect
6422 anywhere except within the directive name or within an argument.  A
6423 comment starting with @samp{#} may appear at the end of the line.
6425 The other two directives that play a part in a conditional are @code{else}
6426 and @code{endif}.  Each of these directives is written as one word, with no
6427 arguments.  Extra spaces are allowed and ignored at the beginning of the
6428 line, and spaces or tabs at the end.  A comment starting with @samp{#} may
6429 appear at the end of the line.
6431 Conditionals affect which lines of the makefile @code{make} uses.  If
6432 the condition is true, @code{make} reads the lines of the
6433 @var{text-if-true} as part of the makefile; if the condition is false,
6434 @code{make} ignores those lines completely.  It follows that syntactic
6435 units of the makefile, such as rules, may safely be split across the
6436 beginning or the end of the conditional.@refill
6438 @code{make} evaluates conditionals when it reads a makefile.
6439 Consequently, you cannot use automatic variables in the tests of
6440 conditionals because they are not defined until recipes are run
6441 (@pxref{Automatic Variables}).
6443 To prevent intolerable confusion, it is not permitted to start a
6444 conditional in one makefile and end it in another.  However, you may
6445 write an @code{include} directive within a conditional, provided you do
6446 not attempt to terminate the conditional inside the included file.
6448 @node Testing Flags,  , Conditional Syntax, Conditionals
6449 @section Conditionals that Test Flags
6451 You can write a conditional that tests @code{make} command flags such as
6452 @samp{-t} by using the variable @code{MAKEFLAGS} together with the
6453 @code{findstring} function
6454 (@pxref{Text Functions, , Functions for String Substitution and Analysis}).
6455 This is useful when @code{touch} is not enough to make a file appear up
6456 to date.
6458 The @code{findstring} function determines whether one string appears as a
6459 substring of another.  If you want to test for the @samp{-t} flag,
6460 use @samp{t} as the first string and the value of @code{MAKEFLAGS} as
6461 the other.
6463 For example, here is how to arrange to use @samp{ranlib -t} to finish
6464 marking an archive file up to date:
6466 @example
6467 archive.a: @dots{}
6468 ifneq (,$(findstring t,$(MAKEFLAGS)))
6469         +touch archive.a
6470         +ranlib -t archive.a
6471 else
6472         ranlib archive.a
6473 endif
6474 @end example
6476 @noindent
6477 The @samp{+} prefix marks those recipe lines as ``recursive'' so that
6478 they will be executed despite use of the @samp{-t} flag.
6479 @xref{Recursion, ,Recursive Use of @code{make}}.
6481 @node Functions, Running, Conditionals, Top
6482 @chapter Functions for Transforming Text
6483 @cindex functions
6485 @dfn{Functions} allow you to do text processing in the makefile to
6486 compute the files to operate on or the commands to use in recipes.
6487 You use a function in a @dfn{function call}, where you give the name
6488 of the function and some text (the @dfn{arguments}) for the function
6489 to operate on.  The result of the function's processing is substituted
6490 into the makefile at the point of the call, just as a variable might
6491 be substituted.
6493 @menu
6494 * Syntax of Functions::         How to write a function call.
6495 * Text Functions::              General-purpose text manipulation functions.
6496 * File Name Functions::         Functions for manipulating file names.
6497 * Conditional Functions::       Functions that implement conditions.
6498 * Foreach Function::            Repeat some text with controlled variation.
6499 * Call Function::               Expand a user-defined function.
6500 * Value Function::              Return the un-expanded value of a variable.
6501 * Eval Function::               Evaluate the arguments as makefile syntax.
6502 * Origin Function::             Find where a variable got its value.
6503 * Flavor Function::             Find out the flavor of a variable.
6504 * Shell Function::              Substitute the output of a shell command.
6505 * Make Control Functions::      Functions that control how make runs.
6506 @end menu
6508 @node Syntax of Functions, Text Functions, Functions, Functions
6509 @section Function Call Syntax
6510 @cindex @code{$}, in function call
6511 @cindex dollar sign (@code{$}), in function call
6512 @cindex arguments of functions
6513 @cindex functions, syntax of
6515 A function call resembles a variable reference.  It can appear
6516 anywhere a variable reference can appear, and it is expanded using the
6517 same rules as variable references.  A function call looks like this:
6519 @example
6520 $(@var{function} @var{arguments})
6521 @end example
6523 @noindent
6524 or like this:
6526 @example
6527 $@{@var{function} @var{arguments}@}
6528 @end example
6530 Here @var{function} is a function name; one of a short list of names
6531 that are part of @code{make}.  You can also essentially create your own
6532 functions by using the @code{call} builtin function.
6534 The @var{arguments} are the arguments of the function.  They are
6535 separated from the function name by one or more spaces or tabs, and if
6536 there is more than one argument, then they are separated by commas.
6537 Such whitespace and commas are not part of an argument's value.  The
6538 delimiters which you use to surround the function call, whether
6539 parentheses or braces, can appear in an argument only in matching pairs;
6540 the other kind of delimiters may appear singly.  If the arguments
6541 themselves contain other function calls or variable references, it is
6542 wisest to use the same kind of delimiters for all the references; write
6543 @w{@samp{$(subst a,b,$(x))}}, not @w{@samp{$(subst a,b,$@{x@})}}.  This
6544 is because it is clearer, and because only one type of delimiter is
6545 matched to find the end of the reference.
6547 The text written for each argument is processed by substitution of
6548 variables and function calls to produce the argument value, which
6549 is the text on which the function acts.  The substitution is done in the
6550 order in which the arguments appear.
6552 Commas and unmatched parentheses or braces cannot appear in the text of an
6553 argument as written; leading spaces cannot appear in the text of the first
6554 argument as written.  These characters can be put into the argument value
6555 by variable substitution.  First define variables @code{comma} and
6556 @code{space} whose values are isolated comma and space characters, then
6557 substitute these variables where such characters are wanted, like this:
6559 @example
6560 @group
6561 comma:= ,
6562 empty:=
6563 space:= $(empty) $(empty)
6564 foo:= a b c
6565 bar:= $(subst $(space),$(comma),$(foo))
6566 # @r{bar is now `a,b,c'.}
6567 @end group
6568 @end example
6570 @noindent
6571 Here the @code{subst} function replaces each space with a comma, through
6572 the value of @code{foo}, and substitutes the result.
6574 @node Text Functions, File Name Functions, Syntax of Functions, Functions
6575 @section Functions for String Substitution and Analysis
6576 @cindex functions, for text
6578 Here are some functions that operate on strings:
6580 @table @code
6581 @item $(subst @var{from},@var{to},@var{text})
6582 @findex subst
6583 Performs a textual replacement on the text @var{text}: each occurrence
6584 of @var{from} is replaced by @var{to}.  The result is substituted for
6585 the function call.  For example,
6587 @example
6588 $(subst ee,EE,feet on the street)
6589 @end example
6591 substitutes the string @samp{fEEt on the strEEt}.
6593 @item $(patsubst @var{pattern},@var{replacement},@var{text})
6594 @findex patsubst
6595 Finds whitespace-separated words in @var{text} that match
6596 @var{pattern} and replaces them with @var{replacement}.  Here
6597 @var{pattern} may contain a @samp{%} which acts as a wildcard,
6598 matching any number of any characters within a word.  If
6599 @var{replacement} also contains a @samp{%}, the @samp{%} is replaced
6600 by the text that matched the @samp{%} in @var{pattern}.  Only the first
6601 @samp{%} in the @var{pattern} and @var{replacement} is treated this
6602 way; any subsequent @samp{%} is unchanged.@refill
6604 @cindex @code{%}, quoting in @code{patsubst}
6605 @cindex @code{%}, quoting with @code{\} (backslash)
6606 @cindex @code{\} (backslash), to quote @code{%}
6607 @cindex backslash (@code{\}), to quote @code{%}
6608 @cindex quoting @code{%}, in @code{patsubst}
6609 @samp{%} characters in @code{patsubst} function invocations can be
6610 quoted with preceding backslashes (@samp{\}).  Backslashes that would
6611 otherwise quote @samp{%} characters can be quoted with more backslashes.
6612 Backslashes that quote @samp{%} characters or other backslashes are
6613 removed from the pattern before it is compared file names or has a stem
6614 substituted into it.  Backslashes that are not in danger of quoting
6615 @samp{%} characters go unmolested.  For example, the pattern
6616 @file{the\%weird\\%pattern\\} has @samp{the%weird\} preceding the
6617 operative @samp{%} character, and @samp{pattern\\} following it.  The
6618 final two backslashes are left alone because they cannot affect any
6619 @samp{%} character.@refill
6621 Whitespace between words is folded into single space characters;
6622 leading and trailing whitespace is discarded.
6624 For example,
6626 @example
6627 $(patsubst %.c,%.o,x.c.c bar.c)
6628 @end example
6630 @noindent
6631 produces the value @samp{x.c.o bar.o}.
6633 Substitution references (@pxref{Substitution Refs, ,Substitution
6634 References}) are a simpler way to get the effect of the @code{patsubst}
6635 function:
6637 @example
6638 $(@var{var}:@var{pattern}=@var{replacement})
6639 @end example
6641 @noindent
6642 is equivalent to
6644 @example
6645 $(patsubst @var{pattern},@var{replacement},$(@var{var}))
6646 @end example
6648 The second shorthand simplifies one of the most common uses of
6649 @code{patsubst}: replacing the suffix at the end of file names.
6651 @example
6652 $(@var{var}:@var{suffix}=@var{replacement})
6653 @end example
6655 @noindent
6656 is equivalent to
6658 @example
6659 $(patsubst %@var{suffix},%@var{replacement},$(@var{var}))
6660 @end example
6662 @noindent
6663 For example, you might have a list of object files:
6665 @example
6666 objects = foo.o bar.o baz.o
6667 @end example
6669 @noindent
6670 To get the list of corresponding source files, you could simply write:
6672 @example
6673 $(objects:.o=.c)
6674 @end example
6676 @noindent
6677 instead of using the general form:
6679 @example
6680 $(patsubst %.o,%.c,$(objects))
6681 @end example
6683 @item $(strip @var{string})
6684 @cindex stripping whitespace
6685 @cindex whitespace, stripping
6686 @cindex spaces, stripping
6687 @findex strip
6688 Removes leading and trailing whitespace from @var{string} and replaces
6689 each internal sequence of one or more whitespace characters with a
6690 single space.  Thus, @samp{$(strip a b  c )} results in @w{@samp{a b c}}.
6692 The function @code{strip} can be very useful when used in conjunction
6693 with conditionals.  When comparing something with the empty string
6694 @samp{} using @code{ifeq} or @code{ifneq}, you usually want a string of
6695 just whitespace to match the empty string (@pxref{Conditionals}).
6697 Thus, the following may fail to have the desired results:
6699 @example
6700 .PHONY: all
6701 ifneq   "$(needs_made)" ""
6702 all: $(needs_made)
6703 else
6704 all:;@@echo 'Nothing to make!'
6705 endif
6706 @end example
6708 @noindent
6709 Replacing the variable reference @w{@samp{$(needs_made)}} with the
6710 function call @w{@samp{$(strip $(needs_made))}} in the @code{ifneq}
6711 directive would make it more robust.@refill
6713 @item $(findstring @var{find},@var{in})
6714 @findex findstring
6715 @cindex searching for strings
6716 @cindex finding strings
6717 @cindex strings, searching for
6718 Searches @var{in} for an occurrence of @var{find}.  If it occurs, the
6719 value is @var{find}; otherwise, the value is empty.  You can use this
6720 function in a conditional to test for the presence of a specific
6721 substring in a given string.  Thus, the two examples,
6723 @example
6724 $(findstring a,a b c)
6725 $(findstring a,b c)
6726 @end example
6728 @noindent
6729 produce the values @samp{a} and @samp{} (the empty string),
6730 respectively.  @xref{Testing Flags}, for a practical application of
6731 @code{findstring}.@refill
6733 @need 750
6734 @findex filter
6735 @cindex filtering words
6736 @cindex words, filtering
6737 @item $(filter @var{pattern}@dots{},@var{text})
6738 Returns all whitespace-separated words in @var{text} that @emph{do} match
6739 any of the @var{pattern} words, removing any words that @emph{do not}
6740 match.  The patterns are written using @samp{%}, just like the patterns
6741 used in the @code{patsubst} function above.@refill
6743 The @code{filter} function can be used to separate out different types
6744 of strings (such as file names) in a variable.  For example:
6746 @example
6747 sources := foo.c bar.c baz.s ugh.h
6748 foo: $(sources)
6749         cc $(filter %.c %.s,$(sources)) -o foo
6750 @end example
6752 @noindent
6753 says that @file{foo} depends of @file{foo.c}, @file{bar.c},
6754 @file{baz.s} and @file{ugh.h} but only @file{foo.c}, @file{bar.c} and
6755 @file{baz.s} should be specified in the command to the
6756 compiler.@refill
6758 @item $(filter-out @var{pattern}@dots{},@var{text})
6759 @findex filter-out
6760 @cindex filtering out words
6761 @cindex words, filtering out
6762 Returns all whitespace-separated words in @var{text} that @emph{do not}
6763 match any of the @var{pattern} words, removing the words that @emph{do}
6764 match one or more.  This is the exact opposite of the @code{filter}
6765 function.@refill
6767 For example, given:
6769 @example
6770 @group
6771 objects=main1.o foo.o main2.o bar.o
6772 mains=main1.o main2.o
6773 @end group
6774 @end example
6776 @noindent
6777 the following generates a list which contains all the object files not
6778 in @samp{mains}:
6780 @example
6781 $(filter-out $(mains),$(objects))
6782 @end example
6784 @need 1500
6785 @findex sort
6786 @cindex sorting words
6787 @item $(sort @var{list})
6788 Sorts the words of @var{list} in lexical order, removing duplicate
6789 words.  The output is a list of words separated by single spaces.
6790 Thus,
6792 @example
6793 $(sort foo bar lose)
6794 @end example
6796 @noindent
6797 returns the value @samp{bar foo lose}.
6799 @cindex removing duplicate words
6800 @cindex duplicate words, removing
6801 @cindex words, removing duplicates
6802 Incidentally, since @code{sort} removes duplicate words, you can use
6803 it for this purpose even if you don't care about the sort order.
6805 @item $(word @var{n},@var{text})
6806 @findex word
6807 @cindex word, selecting a
6808 @cindex selecting a word
6809 Returns the @var{n}th word of @var{text}.  The legitimate values of
6810 @var{n} start from 1.  If @var{n} is bigger than the number of words
6811 in @var{text}, the value is empty.  For example,
6813 @example
6814 $(word 2, foo bar baz)
6815 @end example
6817 @noindent
6818 returns @samp{bar}.
6820 @item $(wordlist @var{s},@var{e},@var{text})
6821 @findex wordlist
6822 @cindex words, selecting lists of
6823 @cindex selecting word lists
6824 Returns the list of words in @var{text} starting with word @var{s} and
6825 ending with word @var{e} (inclusive).  The legitimate values of @var{s}
6826 start from 1; @var{e} may start from 0.  If @var{s} is bigger than the
6827 number of words in @var{text}, the value is empty.  If @var{e} is
6828 bigger than the number of words in @var{text}, words up to the end of
6829 @var{text} are returned.  If @var{s} is greater than @var{e}, nothing
6830 is returned.  For example,
6832 @example
6833 $(wordlist 2, 3, foo bar baz)
6834 @end example
6836 @noindent
6837 returns @samp{bar baz}.
6839 @c Following item phrased to prevent overfull hbox.  --RJC 17 Jul 92
6840 @item $(words @var{text})
6841 @findex words
6842 @cindex words, finding number
6843 Returns the number of words in @var{text}.
6844 Thus, the last word of @var{text} is
6845 @w{@code{$(word $(words @var{text}),@var{text})}}.@refill
6847 @item $(firstword @var{names}@dots{})
6848 @findex firstword
6849 @cindex words, extracting first
6850 The argument @var{names} is regarded as a series of names, separated
6851 by whitespace.  The value is the first name in the series.  The rest
6852 of the names are ignored.
6854 For example,
6856 @example
6857 $(firstword foo bar)
6858 @end example
6860 @noindent
6861 produces the result @samp{foo}.  Although @code{$(firstword
6862 @var{text})} is the same as @code{$(word 1,@var{text})}, the
6863 @code{firstword} function is retained for its simplicity.@refill
6866 @item $(lastword @var{names}@dots{})
6867 @findex lastword
6868 @cindex words, extracting last
6869 The argument @var{names} is regarded as a series of names, separated
6870 by whitespace.  The value is the last name in the series.
6872 For example,
6874 @example
6875 $(lastword foo bar)
6876 @end example
6878 @noindent
6879 produces the result @samp{bar}.  Although @code{$(lastword
6880 @var{text})} is the same as @code{$(word $(words @var{text}),@var{text})},
6881 the @code{lastword} function was added for its simplicity and better
6882 performance.@refill
6883 @end table
6886 Here is a realistic example of the use of @code{subst} and
6887 @code{patsubst}.  Suppose that a makefile uses the @code{VPATH} variable
6888 to specify a list of directories that @code{make} should search for
6889 prerequisite files
6890 (@pxref{General Search, , @code{VPATH} Search Path for All Prerequisites}).
6891 This example shows how to
6892 tell the C compiler to search for header files in the same list of
6893 directories.@refill
6895 The value of @code{VPATH} is a list of directories separated by colons,
6896 such as @samp{src:../headers}.  First, the @code{subst} function is used to
6897 change the colons to spaces:
6899 @example
6900 $(subst :, ,$(VPATH))
6901 @end example
6903 @noindent
6904 This produces @samp{src ../headers}.  Then @code{patsubst} is used to turn
6905 each directory name into a @samp{-I} flag.  These can be added to the
6906 value of the variable @code{CFLAGS}, which is passed automatically to the C
6907 compiler, like this:
6909 @example
6910 override CFLAGS += $(patsubst %,-I%,$(subst :, ,$(VPATH)))
6911 @end example
6913 @noindent
6914 The effect is to append the text @samp{-Isrc -I../headers} to the
6915 previously given value of @code{CFLAGS}.  The @code{override} directive is
6916 used so that the new value is assigned even if the previous value of
6917 @code{CFLAGS} was specified with a command argument (@pxref{Override
6918 Directive, , The @code{override} Directive}).
6920 @node File Name Functions, Conditional Functions, Text Functions, Functions
6921 @section Functions for File Names
6922 @cindex functions, for file names
6923 @cindex file name functions
6925 Several of the built-in expansion functions relate specifically to
6926 taking apart file names or lists of file names.
6928 Each of the following functions performs a specific transformation on a
6929 file name.  The argument of the function is regarded as a series of file
6930 names, separated by whitespace.  (Leading and trailing whitespace is
6931 ignored.)  Each file name in the series is transformed in the same way and
6932 the results are concatenated with single spaces between them.
6934 @table @code
6935 @item $(dir @var{names}@dots{})
6936 @findex dir
6937 @cindex directory part
6938 @cindex file name, directory part
6939 Extracts the directory-part of each file name in @var{names}.  The
6940 directory-part of the file name is everything up through (and
6941 including) the last slash in it.  If the file name contains no slash,
6942 the directory part is the string @samp{./}.  For example,
6944 @example
6945 $(dir src/foo.c hacks)
6946 @end example
6948 @noindent
6949 produces the result @samp{src/ ./}.
6951 @item $(notdir @var{names}@dots{})
6952 @findex notdir
6953 @cindex file name, nondirectory part
6954 @cindex nondirectory part
6955 Extracts all but the directory-part of each file name in @var{names}.
6956 If the file name contains no slash, it is left unchanged.  Otherwise,
6957 everything through the last slash is removed from it.
6959 A file name that ends with a slash becomes an empty string.  This is
6960 unfortunate, because it means that the result does not always have the
6961 same number of whitespace-separated file names as the argument had;
6962 but we do not see any other valid alternative.
6964 For example,
6966 @example
6967 $(notdir src/foo.c hacks)
6968 @end example
6970 @noindent
6971 produces the result @samp{foo.c hacks}.
6973 @item $(suffix @var{names}@dots{})
6974 @findex suffix
6975 @cindex suffix, function to find
6976 @cindex file name suffix
6977 Extracts the suffix of each file name in @var{names}.  If the file name
6978 contains a period, the suffix is everything starting with the last
6979 period.  Otherwise, the suffix is the empty string.  This frequently
6980 means that the result will be empty when @var{names} is not, and if
6981 @var{names} contains multiple file names, the result may contain fewer
6982 file names.
6984 For example,
6986 @example
6987 $(suffix src/foo.c src-1.0/bar.c hacks)
6988 @end example
6990 @noindent
6991 produces the result @samp{.c .c}.
6993 @item $(basename @var{names}@dots{})
6994 @findex basename
6995 @cindex basename
6996 @cindex file name, basename of
6997 Extracts all but the suffix of each file name in @var{names}.  If the
6998 file name contains a period, the basename is everything starting up to
6999 (and not including) the last period.  Periods in the directory part are
7000 ignored.  If there is no period, the basename is the entire file name.
7001 For example,
7003 @example
7004 $(basename src/foo.c src-1.0/bar hacks)
7005 @end example
7007 @noindent
7008 produces the result @samp{src/foo src-1.0/bar hacks}.
7010 @c plural convention with dots (be consistent)
7011 @item $(addsuffix @var{suffix},@var{names}@dots{})
7012 @findex addsuffix
7013 @cindex suffix, adding
7014 @cindex file name suffix, adding
7015 The argument @var{names} is regarded as a series of names, separated
7016 by whitespace; @var{suffix} is used as a unit.  The value of
7017 @var{suffix} is appended to the end of each individual name and the
7018 resulting larger names are concatenated with single spaces between
7019 them.  For example,
7021 @example
7022 $(addsuffix .c,foo bar)
7023 @end example
7025 @noindent
7026 produces the result @samp{foo.c bar.c}.
7028 @item $(addprefix @var{prefix},@var{names}@dots{})
7029 @findex addprefix
7030 @cindex prefix, adding
7031 @cindex file name prefix, adding
7032 The argument @var{names} is regarded as a series of names, separated
7033 by whitespace; @var{prefix} is used as a unit.  The value of
7034 @var{prefix} is prepended to the front of each individual name and the
7035 resulting larger names are concatenated with single spaces between
7036 them.  For example,
7038 @example
7039 $(addprefix src/,foo bar)
7040 @end example
7042 @noindent
7043 produces the result @samp{src/foo src/bar}.
7045 @item $(join @var{list1},@var{list2})
7046 @findex join
7047 @cindex joining lists of words
7048 @cindex words, joining lists
7049 Concatenates the two arguments word by word: the two first words (one
7050 from each argument) concatenated form the first word of the result, the
7051 two second words form the second word of the result, and so on.  So the
7052 @var{n}th word of the result comes from the @var{n}th word of each
7053 argument.  If one argument has more words that the other, the extra
7054 words are copied unchanged into the result.
7056 For example, @samp{$(join a b,.c .o)} produces @samp{a.c b.o}.
7058 Whitespace between the words in the lists is not preserved; it is
7059 replaced with a single space.
7061 This function can merge the results of the @code{dir} and
7062 @code{notdir} functions, to produce the original list of files which
7063 was given to those two functions.@refill
7065 @item $(wildcard @var{pattern})
7066 @findex wildcard
7067 @cindex wildcard, function
7068 The argument @var{pattern} is a file name pattern, typically containing
7069 wildcard characters (as in shell file name patterns).  The result of
7070 @code{wildcard} is a space-separated list of the names of existing files
7071 that match the pattern.
7072 @xref{Wildcards, ,Using Wildcard Characters in File Names}.
7074 @item $(realpath @var{names}@dots{})
7075 @findex realpath
7076 @cindex realpath
7077 @cindex file name, realpath of
7078 For each file name in @var{names} return the canonical absolute name.
7079 A canonical name does not contain any @code{.} or @code{..} components,
7080 nor any repeated path separators (@code{/}) or symlinks.  In case of a
7081 failure the empty string is returned.  Consult the @code{realpath(3)}
7082 documentation for a list of possible failure causes.
7084 @item $(abspath @var{names}@dots{})
7085 @findex abspath
7086 @cindex abspath
7087 @cindex file name, abspath of
7088 For each file name in @var{names} return an absolute name that does
7089 not contain any @code{.} or @code{..} components, nor any repeated path
7090 separators (@code{/}).  Note that, in contrast to @code{realpath}
7091 function, @code{abspath} does not resolve symlinks and does not require
7092 the file names to refer to an existing file or directory.  Use the
7093 @code{wildcard} function to test for existence.
7094 @end table
7096 @node Conditional Functions, Foreach Function, File Name Functions, Functions
7097 @section Functions for Conditionals
7098 @findex if
7099 @cindex conditional expansion
7100 There are three functions that provide conditional expansion.  A key
7101 aspect of these functions is that not all of the arguments are
7102 expanded initially.  Only those arguments which need to be expanded,
7103 will be expanded.
7105 @table @code
7106 @item $(if @var{condition},@var{then-part}[,@var{else-part}])
7107 @findex if
7108 The @code{if} function provides support for conditional expansion in a
7109 functional context (as opposed to the GNU @code{make} makefile
7110 conditionals such as @code{ifeq} (@pxref{Conditional Syntax, ,Syntax of
7111 Conditionals}).
7113 The first argument, @var{condition}, first has all preceding and
7114 trailing whitespace stripped, then is expanded.  If it expands to any
7115 non-empty string, then the condition is considered to be true.  If it
7116 expands to an empty string, the condition is considered to be false.
7118 If the condition is true then the second argument, @var{then-part}, is
7119 evaluated and this is used as the result of the evaluation of the entire
7120 @code{if} function.
7122 If the condition is false then the third argument, @var{else-part}, is
7123 evaluated and this is the result of the @code{if} function.  If there is
7124 no third argument, the @code{if} function evaluates to nothing (the
7125 empty string).
7127 Note that only one of the @var{then-part} or the @var{else-part} will be
7128 evaluated, never both.  Thus, either can contain side-effects (such as
7129 @code{shell} function calls, etc.)
7131 @item $(or @var{condition1}[,@var{condition2}[,@var{condition3}@dots{}]])
7132 @findex or
7133 The @code{or} function provides a ``short-circuiting'' OR operation.
7134 Each argument is expanded, in order.  If an argument expands to a
7135 non-empty string the processing stops and the result of the expansion
7136 is that string.  If, after all arguments are expanded, all of them are
7137 false (empty), then the result of the expansion is the empty string.
7139 @item $(and @var{condition1}[,@var{condition2}[,@var{condition3}@dots{}]])
7140 @findex and
7141 The @code{and} function provides a ``short-circuiting'' AND operation.
7142 Each argument is expanded, in order.  If an argument expands to an
7143 empty string the processing stops and the result of the expansion is
7144 the empty string.  If all arguments expand to a non-empty string then
7145 the result of the expansion is the expansion of the last argument.
7147 @end table
7149 @node Foreach Function, Call Function, Conditional Functions, Functions
7150 @section The @code{foreach} Function
7151 @findex foreach
7152 @cindex words, iterating over
7154 The @code{foreach} function is very different from other functions.  It
7155 causes one piece of text to be used repeatedly, each time with a different
7156 substitution performed on it.  It resembles the @code{for} command in the
7157 shell @code{sh} and the @code{foreach} command in the C-shell @code{csh}.
7159 The syntax of the @code{foreach} function is:
7161 @example
7162 $(foreach @var{var},@var{list},@var{text})
7163 @end example
7165 @noindent
7166 The first two arguments, @var{var} and @var{list}, are expanded before
7167 anything else is done; note that the last argument, @var{text}, is
7168 @strong{not} expanded at the same time.  Then for each word of the expanded
7169 value of @var{list}, the variable named by the expanded value of @var{var}
7170 is set to that word, and @var{text} is expanded.  Presumably @var{text}
7171 contains references to that variable, so its expansion will be different
7172 each time.
7174 The result is that @var{text} is expanded as many times as there are
7175 whitespace-separated words in @var{list}.  The multiple expansions of
7176 @var{text} are concatenated, with spaces between them, to make the result
7177 of @code{foreach}.
7179 This simple example sets the variable @samp{files} to the list of all files
7180 in the directories in the list @samp{dirs}:
7182 @example
7183 dirs := a b c d
7184 files := $(foreach dir,$(dirs),$(wildcard $(dir)/*))
7185 @end example
7187 Here @var{text} is @samp{$(wildcard $(dir)/*)}.  The first repetition
7188 finds the value @samp{a} for @code{dir}, so it produces the same result
7189 as @samp{$(wildcard a/*)}; the second repetition produces the result
7190 of @samp{$(wildcard b/*)}; and the third, that of @samp{$(wildcard c/*)}.
7192 This example has the same result (except for setting @samp{dirs}) as
7193 the following example:
7195 @example
7196 files := $(wildcard a/* b/* c/* d/*)
7197 @end example
7199 When @var{text} is complicated, you can improve readability by giving it
7200 a name, with an additional variable:
7202 @example
7203 find_files = $(wildcard $(dir)/*)
7204 dirs := a b c d
7205 files := $(foreach dir,$(dirs),$(find_files))
7206 @end example
7208 @noindent
7209 Here we use the variable @code{find_files} this way.  We use plain @samp{=}
7210 to define a recursively-expanding variable, so that its value contains an
7211 actual function call to be reexpanded under the control of @code{foreach};
7212 a simply-expanded variable would not do, since @code{wildcard} would be
7213 called only once at the time of defining @code{find_files}.
7215 The @code{foreach} function has no permanent effect on the variable
7216 @var{var}; its value and flavor after the @code{foreach} function call are
7217 the same as they were beforehand.  The other values which are taken from
7218 @var{list} are in effect only temporarily, during the execution of
7219 @code{foreach}.  The variable @var{var} is a simply-expanded variable
7220 during the execution of @code{foreach}.  If @var{var} was undefined
7221 before the @code{foreach} function call, it is undefined after the call.
7222 @xref{Flavors, ,The Two Flavors of Variables}.@refill
7224 You must take care when using complex variable expressions that result in
7225 variable names because many strange things are valid variable names, but
7226 are probably not what you intended.  For example,
7228 @smallexample
7229 files := $(foreach Esta escrito en espanol!,b c ch,$(find_files))
7230 @end smallexample
7232 @noindent
7233 might be useful if the value of @code{find_files} references the variable
7234 whose name is @samp{Esta escrito en espanol!} (es un nombre bastante largo,
7235 no?), but it is more likely to be a mistake.
7237 @node Call Function, Value Function, Foreach Function, Functions
7238 @section The @code{call} Function
7239 @findex call
7240 @cindex functions, user defined
7241 @cindex user defined functions
7243 The @code{call} function is unique in that it can be used to create new
7244 parameterized functions.  You can write a complex expression as the
7245 value of a variable, then use @code{call} to expand it with different
7246 values.
7248 The syntax of the @code{call} function is:
7250 @example
7251 $(call @var{variable},@var{param},@var{param},@dots{})
7252 @end example
7254 When @code{make} expands this function, it assigns each @var{param} to
7255 temporary variables @code{$(1)}, @code{$(2)}, etc.  The variable
7256 @code{$(0)} will contain @var{variable}.  There is no maximum number of
7257 parameter arguments.  There is no minimum, either, but it doesn't make
7258 sense to use @code{call} with no parameters.
7260 Then @var{variable} is expanded as a @code{make} variable in the context
7261 of these temporary assignments.  Thus, any reference to @code{$(1)} in
7262 the value of @var{variable} will resolve to the first @var{param} in the
7263 invocation of @code{call}.
7265 Note that @var{variable} is the @emph{name} of a variable, not a
7266 @emph{reference} to that variable.  Therefore you would not normally use
7267 a @samp{$} or parentheses when writing it.  (You can, however, use a
7268 variable reference in the name if you want the name not to be a
7269 constant.)
7271 If @var{variable} is the name of a builtin function, the builtin function
7272 is always invoked (even if a @code{make} variable by that name also
7273 exists).
7275 The @code{call} function expands the @var{param} arguments before
7276 assigning them to temporary variables.  This means that @var{variable}
7277 values containing references to builtin functions that have special
7278 expansion rules, like @code{foreach} or @code{if}, may not work as you
7279 expect.
7281 Some examples may make this clearer.
7283 This macro simply reverses its arguments:
7285 @smallexample
7286 reverse = $(2) $(1)
7288 foo = $(call reverse,a,b)
7289 @end smallexample
7291 @noindent
7292 Here @var{foo} will contain @samp{b a}.
7294 This one is slightly more interesting: it defines a macro to search for
7295 the first instance of a program in @code{PATH}:
7297 @smallexample
7298 pathsearch = $(firstword $(wildcard $(addsuffix /$(1),$(subst :, ,$(PATH)))))
7300 LS := $(call pathsearch,ls)
7301 @end smallexample
7303 @noindent
7304 Now the variable LS contains @code{/bin/ls} or similar.
7306 The @code{call} function can be nested.  Each recursive invocation gets
7307 its own local values for @code{$(1)}, etc.@: that mask the values of
7308 higher-level @code{call}.  For example, here is an implementation of a
7309 @dfn{map} function:
7311 @smallexample
7312 map = $(foreach a,$(2),$(call $(1),$(a)))
7313 @end smallexample
7315 Now you can @var{map} a function that normally takes only one argument,
7316 such as @code{origin}, to multiple values in one step:
7318 @smallexample
7319 o = $(call map,origin,o map MAKE)
7320 @end smallexample
7322 and end up with @var{o} containing something like @samp{file file default}.
7324 A final caution: be careful when adding whitespace to the arguments to
7325 @code{call}.  As with other functions, any whitespace contained in the
7326 second and subsequent arguments is kept; this can cause strange
7327 effects.  It's generally safest to remove all extraneous whitespace when
7328 providing parameters to @code{call}.
7330 @node Value Function, Eval Function, Call Function, Functions
7331 @comment  node-name,  next,  previous,  up
7332 @section The @code{value} Function
7333 @findex value
7334 @cindex variables, unexpanded value
7336 The @code{value} function provides a way for you to use the value of a
7337 variable @emph{without} having it expanded.  Please note that this
7338 does not undo expansions which have already occurred; for example if
7339 you create a simply expanded variable its value is expanded during the
7340 definition; in that case the @code{value} function will return the
7341 same result as using the variable directly.
7343 The syntax of the @code{value} function is:
7345 @example
7346 $(value @var{variable})
7347 @end example
7349 Note that @var{variable} is the @emph{name} of a variable, not a
7350 @emph{reference} to that variable.  Therefore you would not normally
7351 use a @samp{$} or parentheses when writing it.  (You can, however, use
7352 a variable reference in the name if you want the name not to be a
7353 constant.)
7355 The result of this function is a string containing the value of
7356 @var{variable}, without any expansion occurring.  For example, in this
7357 makefile:
7359 @example
7360 @group
7361 FOO = $PATH
7363 all:
7364         @@echo $(FOO)
7365         @@echo $(value FOO)
7366 @end group
7367 @end example
7369 @noindent
7370 The first output line would be @code{ATH}, since the ``$P'' would be
7371 expanded as a @code{make} variable, while the second output line would
7372 be the current value of your @code{$PATH} environment variable, since
7373 the @code{value} function avoided the expansion.
7375 The @code{value} function is most often used in conjunction with the
7376 @code{eval} function (@pxref{Eval Function}).
7378 @node Eval Function, Origin Function, Value Function, Functions
7379 @comment  node-name,  next,  previous,  up
7380 @section The @code{eval} Function
7381 @findex eval
7382 @cindex evaluating makefile syntax
7383 @cindex makefile syntax, evaluating
7385 The @code{eval} function is very special: it allows you to define new
7386 makefile constructs that are not constant; which are the result of
7387 evaluating other variables and functions.  The argument to the
7388 @code{eval} function is expanded, then the results of that expansion
7389 are parsed as makefile syntax.  The expanded results can define new
7390 @code{make} variables, targets, implicit or explicit rules, etc.
7392 The result of the @code{eval} function is always the empty string;
7393 thus, it can be placed virtually anywhere in a makefile without
7394 causing syntax errors.
7396 It's important to realize that the @code{eval} argument is expanded
7397 @emph{twice}; first by the @code{eval} function, then the results of
7398 that expansion are expanded again when they are parsed as makefile
7399 syntax.  This means you may need to provide extra levels of escaping
7400 for ``$'' characters when using @code{eval}.  The @code{value}
7401 function (@pxref{Value Function}) can sometimes be useful in these
7402 situations, to circumvent unwanted expansions.
7404 Here is an example of how @code{eval} can be used; this example
7405 combines a number of concepts and other functions.  Although it might
7406 seem overly complex to use @code{eval} in this example, rather than
7407 just writing out the rules, consider two things: first, the template
7408 definition (in @code{PROGRAM_template}) could need to be much more
7409 complex than it is here; and second, you might put the complex,
7410 ``generic'' part of this example into another makefile, then include
7411 it in all the individual makefiles.  Now your individual makefiles are
7412 quite straightforward.
7414 @example
7415 @group
7416 PROGRAMS    = server client
7418 server_OBJS = server.o server_priv.o server_access.o
7419 server_LIBS = priv protocol
7421 client_OBJS = client.o client_api.o client_mem.o
7422 client_LIBS = protocol
7424 # Everything after this is generic
7426 .PHONY: all
7427 all: $(PROGRAMS)
7429 define PROGRAM_template =
7430  $(1): $$($(1)_OBJS) $$($(1)_LIBS:%=-l%)
7431  ALL_OBJS   += $$($(1)_OBJS)
7432 endef
7434 $(foreach prog,$(PROGRAMS),$(eval $(call PROGRAM_template,$(prog))))
7436 $(PROGRAMS):
7437         $(LINK.o) $^ $(LDLIBS) -o $@@
7439 clean:
7440         rm -f $(ALL_OBJS) $(PROGRAMS)
7441 @end group
7442 @end example
7444 @node Origin Function, Flavor Function, Eval Function, Functions
7445 @section The @code{origin} Function
7446 @findex origin
7447 @cindex variables, origin of
7448 @cindex origin of variable
7450 The @code{origin} function is unlike most other functions in that it does
7451 not operate on the values of variables; it tells you something @emph{about}
7452 a variable.  Specifically, it tells you where it came from.
7454 The syntax of the @code{origin} function is:
7456 @example
7457 $(origin @var{variable})
7458 @end example
7460 Note that @var{variable} is the @emph{name} of a variable to inquire about,
7461 not a @emph{reference} to that variable.  Therefore you would not normally
7462 use a @samp{$} or parentheses when writing it.  (You can, however, use a
7463 variable reference in the name if you want the name not to be a constant.)
7465 The result of this function is a string telling you how the variable
7466 @var{variable} was defined:
7468 @table @samp
7469 @item undefined
7471 if @var{variable} was never defined.
7473 @item default
7475 if @var{variable} has a default definition, as is usual with @code{CC}
7476 and so on.  @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
7477 Note that if you have redefined a default variable, the @code{origin}
7478 function will return the origin of the later definition.
7480 @item environment
7482 if @var{variable} was inherited from the environment provided to
7483 @code{make}.
7485 @item environment override
7487 if @var{variable} was inherited from the environment provided to
7488 @code{make}, and is overriding a setting for @var{variable} in the
7489 makefile as a result of the @w{@samp{-e}} option (@pxref{Options
7490 Summary, ,Summary of Options}).@refill
7492 @item file
7494 if @var{variable} was defined in a makefile.
7496 @item command line
7498 if @var{variable} was defined on the command line.
7500 @item override
7502 if @var{variable} was defined with an @code{override} directive in a
7503 makefile (@pxref{Override Directive, ,The @code{override} Directive}).
7505 @item automatic
7507 if @var{variable} is an automatic variable defined for the execution
7508 of the recipe for each rule (@pxref{Automatic Variables}).
7509 @end table
7511 This information is primarily useful (other than for your curiosity) to
7512 determine if you want to believe the value of a variable.  For example,
7513 suppose you have a makefile @file{foo} that includes another makefile
7514 @file{bar}.  You want a variable @code{bletch} to be defined in @file{bar}
7515 if you run the command @w{@samp{make -f bar}}, even if the environment contains
7516 a definition of @code{bletch}.  However, if @file{foo} defined
7517 @code{bletch} before including @file{bar}, you do not want to override that
7518 definition.  This could be done by using an @code{override} directive in
7519 @file{foo}, giving that definition precedence over the later definition in
7520 @file{bar}; unfortunately, the @code{override} directive would also
7521 override any command line definitions.  So, @file{bar} could
7522 include:@refill
7524 @example
7525 @group
7526 ifdef bletch
7527 ifeq "$(origin bletch)" "environment"
7528 bletch = barf, gag, etc.
7529 endif
7530 endif
7531 @end group
7532 @end example
7534 @noindent
7535 If @code{bletch} has been defined from the environment, this will redefine
7538 If you want to override a previous definition of @code{bletch} if it came
7539 from the environment, even under @samp{-e}, you could instead write:
7541 @example
7542 @group
7543 ifneq "$(findstring environment,$(origin bletch))" ""
7544 bletch = barf, gag, etc.
7545 endif
7546 @end group
7547 @end example
7549 Here the redefinition takes place if @samp{$(origin bletch)} returns either
7550 @samp{environment} or @samp{environment override}.
7551 @xref{Text Functions, , Functions for String Substitution and Analysis}.
7553 @node Flavor Function, Shell Function, Origin Function, Functions
7554 @section The @code{flavor} Function
7555 @findex flavor
7556 @cindex variables, flavor of
7557 @cindex flavor of variable
7559 The @code{flavor} function is unlike most other functions (and like
7560 @code{origin} function) in that it does not operate on the values of
7561 variables; it tells you something @emph{about} a variable.
7562 Specifically, it tells you the flavor of a variable (@pxref{Flavors,
7563 ,The Two Flavors of Variables}).
7565 The syntax of the @code{flavor} function is:
7567 @example
7568 $(flavor @var{variable})
7569 @end example
7571 Note that @var{variable} is the @emph{name} of a variable to inquire about,
7572 not a @emph{reference} to that variable.  Therefore you would not normally
7573 use a @samp{$} or parentheses when writing it.  (You can, however, use a
7574 variable reference in the name if you want the name not to be a constant.)
7576 The result of this function is a string that identifies the flavor of the
7577 variable @var{variable}:
7579 @table @samp
7580 @item undefined
7582 if @var{variable} was never defined.
7584 @item recursive
7586 if @var{variable} is a recursively expanded variable.
7588 @item simple
7590 if @var{variable} is a simply expanded variable.
7592 @end table
7595 @node Shell Function, Make Control Functions, Flavor Function, Functions
7596 @section The @code{shell} Function
7597 @findex shell
7598 @cindex command expansion
7599 @cindex backquotes
7600 @cindex shell command, function for
7602 The @code{shell} function is unlike any other function other than the
7603 @code{wildcard} function
7604 (@pxref{Wildcard Function, ,The Function @code{wildcard}}) in that it
7605 communicates with the world outside of @code{make}.
7607 The @code{shell} function performs the same function that backquotes
7608 (@samp{`}) perform in most shells: it does @dfn{command expansion}.
7609 This means that it takes as an argument a shell command and evaluates
7610 to the output of the command.  The only processing @code{make} does on
7611 the result is to convert each newline (or carriage-return / newline
7612 pair) to a single space.  If there is a trailing (carriage-return
7613 and) newline it will simply be removed.@refill
7615 The commands run by calls to the @code{shell} function are run when the
7616 function calls are expanded (@pxref{Reading Makefiles, , How
7617 @code{make} Reads a Makefile}).  Because this function involves
7618 spawning a new shell, you should carefully consider the performance
7619 implications of using the @code{shell} function within recursively
7620 expanded variables vs.@: simply expanded variables (@pxref{Flavors, ,The
7621 Two Flavors of Variables}).
7623 Here are some examples of the use of the @code{shell} function:
7625 @example
7626 contents := $(shell cat foo)
7627 @end example
7629 @noindent
7630 sets @code{contents} to the contents of the file @file{foo}, with a space
7631 (rather than a newline) separating each line.
7633 @example
7634 files := $(shell echo *.c)
7635 @end example
7637 @noindent
7638 sets @code{files} to the expansion of @samp{*.c}.  Unless @code{make} is
7639 using a very strange shell, this has the same result as
7640 @w{@samp{$(wildcard *.c)}} (as long as at least one @samp{.c} file
7641 exists).@refill
7643 @node Make Control Functions,  , Shell Function, Functions
7644 @section Functions That Control Make
7645 @cindex functions, for controlling make
7646 @cindex controlling make
7648 These functions control the way make runs.  Generally, they are used to
7649 provide information to the user of the makefile or to cause make to stop
7650 if some sort of environmental error is detected.
7652 @table @code
7653 @item $(error @var{text}@dots{})
7654 @findex error
7655 @cindex error, stopping on
7656 @cindex stopping make
7657 Generates a fatal error where the message is @var{text}.  Note that
7658 the error is generated whenever this function is evaluated.  So, if
7659 you put it inside a recipe or on the right side of a recursive
7660 variable assignment, it won't be evaluated until later.  The
7661 @var{text} will be expanded before the error is generated.
7663 For example,
7665 @example
7666 ifdef ERROR1
7667 $(error error is $(ERROR1))
7668 endif
7669 @end example
7671 @noindent
7672 will generate a fatal error during the read of the makefile if the
7673 @code{make} variable @code{ERROR1} is defined.  Or,
7675 @example
7676 ERR = $(error found an error!)
7678 .PHONY: err
7679 err: ; $(ERR)
7680 @end example
7682 @noindent
7683 will generate a fatal error while @code{make} is running, if the
7684 @code{err} target is invoked.
7686 @item $(warning @var{text}@dots{})
7687 @findex warning
7688 @cindex warnings, printing
7689 @cindex printing user warnings
7690 This function works similarly to the @code{error} function, above,
7691 except that @code{make} doesn't exit.  Instead, @var{text} is expanded
7692 and the resulting message is displayed, but processing of the makefile
7693 continues.
7695 The result of the expansion of this function is the empty string.
7697 @item $(info @var{text}@dots{})
7698 @findex info
7699 @cindex printing messages
7700 This function does nothing more than print its (expanded) argument(s)
7701 to standard output.  No makefile name or line number is added.  The
7702 result of the expansion of this function is the empty string.
7703 @end table
7705 @node Running, Implicit Rules, Functions, Top
7706 @chapter How to Run @code{make}
7708 A makefile that says how to recompile a program can be used in more
7709 than one way.  The simplest use is to recompile every file that is out
7710 of date.  Usually, makefiles are written so that if you run
7711 @code{make} with no arguments, it does just that.
7713 But you might want to update only some of the files; you might want to use
7714 a different compiler or different compiler options; you might want just to
7715 find out which files are out of date without changing them.
7717 By giving arguments when you run @code{make}, you can do any of these
7718 things and many others.
7720 @cindex exit status of make
7721 The exit status of @code{make} is always one of three values:
7722 @table @code
7723 @item 0
7724 The exit status is zero if @code{make} is successful.
7725 @item 2
7726 The exit status is two if @code{make} encounters any errors.
7727 It will print messages describing the particular errors.
7728 @item 1
7729 The exit status is one if you use the @samp{-q} flag and @code{make}
7730 determines that some target is not already up to date.
7731 @xref{Instead of Execution, ,Instead of Executing Recipes}.
7732 @end table
7734 @menu
7735 * Makefile Arguments::          How to specify which makefile to use.
7736 * Goals::                       How to use goal arguments to specify which
7737                                   parts of the makefile to use.
7738 * Instead of Execution::        How to use mode flags to specify what
7739                                   kind of thing to do with the recipes
7740                                   in the makefile other than simply
7741                                   execute them.
7742 * Avoiding Compilation::        How to avoid recompiling certain files.
7743 * Overriding::                  How to override a variable to specify
7744                                   an alternate compiler and other things.
7745 * Testing::                     How to proceed past some errors, to
7746                                   test compilation.
7747 * Options Summary::             Summary of Options
7748 @end menu
7750 @node Makefile Arguments, Goals, Running, Running
7751 @section Arguments to Specify the Makefile
7752 @cindex @code{--file}
7753 @cindex @code{--makefile}
7754 @cindex @code{-f}
7756 The way to specify the name of the makefile is with the @samp{-f} or
7757 @samp{--file} option (@samp{--makefile} also works).  For example,
7758 @samp{-f altmake} says to use the file @file{altmake} as the makefile.
7760 If you use the @samp{-f} flag several times and follow each @samp{-f}
7761 with an argument, all the specified files are used jointly as
7762 makefiles.
7764 If you do not use the @samp{-f} or @samp{--file} flag, the default is
7765 to try @file{GNUmakefile}, @file{makefile}, and @file{Makefile}, in
7766 that order, and use the first of these three which exists or can be made
7767 (@pxref{Makefiles, ,Writing Makefiles}).@refill
7769 @node Goals, Instead of Execution, Makefile Arguments, Running
7770 @section Arguments to Specify the Goals
7771 @cindex goal, how to specify
7773 The @dfn{goals} are the targets that @code{make} should strive ultimately
7774 to update.  Other targets are updated as well if they appear as
7775 prerequisites of goals, or prerequisites of prerequisites of goals, etc.
7777 By default, the goal is the first target in the makefile (not counting
7778 targets that start with a period).  Therefore, makefiles are usually
7779 written so that the first target is for compiling the entire program or
7780 programs they describe.  If the first rule in the makefile has several
7781 targets, only the first target in the rule becomes the default goal, not
7782 the whole list.  You can manage the selection of the default goal from
7783 within your makefile using the @code{.DEFAULT_GOAL} variable
7784 (@pxref{Special Variables, , Other Special Variables}).
7786 You can also specify a different goal or goals with command line
7787 arguments to @code{make}.  Use the name of the goal as an argument.
7788 If you specify several goals, @code{make} processes each of them in
7789 turn, in the order you name them.
7791 Any target in the makefile may be specified as a goal (unless it
7792 starts with @samp{-} or contains an @samp{=}, in which case it will be
7793 parsed as a switch or variable definition, respectively).  Even
7794 targets not in the makefile may be specified, if @code{make} can find
7795 implicit rules that say how to make them.
7797 @vindex MAKECMDGOALS
7798 @code{Make} will set the special variable @code{MAKECMDGOALS} to the
7799 list of goals you specified on the command line.  If no goals were given
7800 on the command line, this variable is empty.  Note that this variable
7801 should be used only in special circumstances.
7803 An example of appropriate use is to avoid including @file{.d} files
7804 during @code{clean} rules (@pxref{Automatic Prerequisites}), so
7805 @code{make} won't create them only to immediately remove them
7806 again:@refill
7808 @example
7809 @group
7810 sources = foo.c bar.c
7812 ifneq ($(MAKECMDGOALS),clean)
7813 include $(sources:.c=.d)
7814 endif
7815 @end group
7816 @end example
7818 One use of specifying a goal is if you want to compile only a part of
7819 the program, or only one of several programs.  Specify as a goal each
7820 file that you wish to remake.  For example, consider a directory containing
7821 several programs, with a makefile that starts like this:
7823 @example
7824 .PHONY: all
7825 all: size nm ld ar as
7826 @end example
7828 If you are working on the program @code{size}, you might want to say
7829 @w{@samp{make size}} so that only the files of that program are recompiled.
7831 Another use of specifying a goal is to make files that are not normally
7832 made.  For example, there may be a file of debugging output, or a
7833 version of the program that is compiled specially for testing, which has
7834 a rule in the makefile but is not a prerequisite of the default goal.
7836 Another use of specifying a goal is to run the recipe associated with
7837 a phony target (@pxref{Phony Targets}) or empty target (@pxref{Empty
7838 Targets, ,Empty Target Files to Record Events}).  Many makefiles contain
7839 a phony target named @file{clean} which deletes everything except source
7840 files.  Naturally, this is done only if you request it explicitly with
7841 @w{@samp{make clean}}.  Following is a list of typical phony and empty
7842 target names.  @xref{Standard Targets}, for a detailed list of all the
7843 standard target names which GNU software packages use.
7845 @table @file
7846 @item all
7847 @cindex @code{all} @r{(standard target)}
7848 Make all the top-level targets the makefile knows about.
7850 @item clean
7851 @cindex @code{clean} @r{(standard target)}
7852 Delete all files that are normally created by running @code{make}.
7854 @item mostlyclean
7855 @cindex @code{mostlyclean} @r{(standard target)}
7856 Like @samp{clean}, but may refrain from deleting a few files that people
7857 normally don't want to recompile.  For example, the @samp{mostlyclean}
7858 target for GCC does not delete @file{libgcc.a}, because recompiling it
7859 is rarely necessary and takes a lot of time.
7861 @item distclean
7862 @cindex @code{distclean} @r{(standard target)}
7863 @itemx realclean
7864 @cindex @code{realclean} @r{(standard target)}
7865 @itemx clobber
7866 @cindex @code{clobber} @r{(standard target)}
7867 Any of these targets might be defined to delete @emph{more} files than
7868 @samp{clean} does.  For example, this would delete configuration files
7869 or links that you would normally create as preparation for compilation,
7870 even if the makefile itself cannot create these files.
7872 @item install
7873 @cindex @code{install} @r{(standard target)}
7874 Copy the executable file into a directory that users typically search
7875 for commands; copy any auxiliary files that the executable uses into
7876 the directories where it will look for them.
7878 @item print
7879 @cindex @code{print} @r{(standard target)}
7880 Print listings of the source files that have changed.
7882 @item tar
7883 @cindex @code{tar} @r{(standard target)}
7884 Create a tar file of the source files.
7886 @item shar
7887 @cindex @code{shar} @r{(standard target)}
7888 Create a shell archive (shar file) of the source files.
7890 @item dist
7891 @cindex @code{dist} @r{(standard target)}
7892 Create a distribution file of the source files.  This might
7893 be a tar file, or a shar file, or a compressed version of one of the
7894 above, or even more than one of the above.
7896 @item TAGS
7897 @cindex @code{TAGS} @r{(standard target)}
7898 Update a tags table for this program.
7900 @item check
7901 @cindex @code{check} @r{(standard target)}
7902 @itemx test
7903 @cindex @code{test} @r{(standard target)}
7904 Perform self tests on the program this makefile builds.
7905 @end table
7907 @node Instead of Execution, Avoiding Compilation, Goals, Running
7908 @section Instead of Executing Recipes
7909 @cindex execution, instead of
7910 @cindex recipes, instead of executing
7912 The makefile tells @code{make} how to tell whether a target is up to date,
7913 and how to update each target.  But updating the targets is not always
7914 what you want.  Certain options specify other activities for @code{make}.
7916 @comment Extra blank lines make it print better.
7917 @table @samp
7918 @item -n
7919 @itemx --just-print
7920 @itemx --dry-run
7921 @itemx --recon
7922 @cindex @code{--just-print}
7923 @cindex @code{--dry-run}
7924 @cindex @code{--recon}
7925 @cindex @code{-n}
7927 ``No-op''.  Causes @code{make} to print the recipes that are needed to
7928 make the targets up to date, but not actually execute them.  Note that
7929 some recipes are still executed, even with this flag (@pxref{MAKE
7930 Variable, ,How the @code{MAKE} Variable Works}).  Also any recipes
7931 needed to update included makefiles are still executed
7932 (@pxref{Remaking Makefiles, ,How Makefiles Are Remade}).
7934 @item -t
7935 @itemx --touch
7936 @cindex @code{--touch}
7937 @cindex touching files
7938 @cindex target, touching
7939 @cindex @code{-t}
7941 ``Touch''.  Marks targets as up to date without actually changing
7942 them.  In other words, @code{make} pretends to update the targets but
7943 does not really change their contents; instead only their modified
7944 times are updated.
7946 @item -q
7947 @itemx --question
7948 @cindex @code{--question}
7949 @cindex @code{-q}
7950 @cindex question mode
7952 ``Question''.  Silently check whether the targets are up to date, but
7953 do not execute recipes; the exit code shows whether any updates are
7954 needed.
7956 @item -W @var{file}
7957 @itemx --what-if=@var{file}
7958 @itemx --assume-new=@var{file}
7959 @itemx --new-file=@var{file}
7960 @cindex @code{--what-if}
7961 @cindex @code{-W}
7962 @cindex @code{--assume-new}
7963 @cindex @code{--new-file}
7964 @cindex what if
7965 @cindex files, assuming new
7967 ``What if''.  Each @samp{-W} flag is followed by a file name.  The given
7968 files' modification times are recorded by @code{make} as being the present
7969 time, although the actual modification times remain the same.
7970 You can use the @samp{-W} flag in conjunction with the @samp{-n} flag
7971 to see what would happen if you were to modify specific files.@refill
7972 @end table
7974 With the @samp{-n} flag, @code{make} prints the recipe that it would
7975 normally execute but usually does not execute it.
7977 With the @samp{-t} flag, @code{make} ignores the recipes in the rules
7978 and uses (in effect) the command @code{touch} for each target that needs to
7979 be remade.  The @code{touch} command is also printed, unless @samp{-s} or
7980 @code{.SILENT} is used.  For speed, @code{make} does not actually invoke
7981 the program @code{touch}.  It does the work directly.
7983 With the @samp{-q} flag, @code{make} prints nothing and executes no
7984 recipes, but the exit status code it returns is zero if and only if the
7985 targets to be considered are already up to date.  If the exit status is
7986 one, then some updating needs to be done.  If @code{make} encounters an
7987 error, the exit status is two, so you can distinguish an error from a
7988 target that is not up to date.
7990 It is an error to use more than one of these three flags in the same
7991 invocation of @code{make}.
7993 @cindex +, and recipe execution
7994 The @samp{-n}, @samp{-t}, and @samp{-q} options do not affect recipe
7995 lines that begin with @samp{+} characters or contain the strings
7996 @samp{$(MAKE)} or @samp{$@{MAKE@}}.  Note that only the line containing
7997 the @samp{+} character or the strings @samp{$(MAKE)} or @samp{$@{MAKE@}}
7998 is run regardless of these options.  Other lines in the same rule are
7999 not run unless they too begin with @samp{+} or contain @samp{$(MAKE)} or
8000 @samp{$@{MAKE@}} (@xref{MAKE Variable, ,How the @code{MAKE} Variable Works}.)
8002 @cindex phony targets and recipe execution
8003 The @samp{-t} flag prevents phony targets (@pxref{Phony Targets}) from
8004 being updated, unless there are recipe lines beginning with @samp{+}
8005 or containing @samp{$(MAKE)} or @samp{$@{MAKE@}}.
8007 The @samp{-W} flag provides two features:
8009 @itemize @bullet
8010 @item
8011 If you also use the @samp{-n} or @samp{-q} flag, you can see what
8012 @code{make} would do if you were to modify some files.
8014 @item
8015 Without the @samp{-n} or @samp{-q} flag, when @code{make} is actually
8016 executing recipes, the @samp{-W} flag can direct @code{make} to act as
8017 if some files had been modified, without actually running the recipes
8018 for those files.@refill
8019 @end itemize
8021 Note that the options @samp{-p} and @samp{-v} allow you to obtain other
8022 information about @code{make} or about the makefiles in use
8023 (@pxref{Options Summary, ,Summary of Options}).@refill
8025 @node Avoiding Compilation, Overriding, Instead of Execution, Running
8026 @section Avoiding Recompilation of Some Files
8027 @cindex @code{-o}
8028 @cindex @code{--old-file}
8029 @cindex @code{--assume-old}
8030 @cindex files, assuming old
8031 @cindex files, avoiding recompilation of
8032 @cindex recompilation, avoiding
8034 Sometimes you may have changed a source file but you do not want to
8035 recompile all the files that depend on it.  For example, suppose you add
8036 a macro or a declaration to a header file that many other files depend
8037 on.  Being conservative, @code{make} assumes that any change in the
8038 header file requires recompilation of all dependent files, but you know
8039 that they do not need to be recompiled and you would rather not waste
8040 the time waiting for them to compile.
8042 If you anticipate the problem before changing the header file, you can
8043 use the @samp{-t} flag.  This flag tells @code{make} not to run the
8044 recipes in the rules, but rather to mark the target up to date by
8045 changing its last-modification date.  You would follow this procedure:
8047 @enumerate
8048 @item
8049 Use the command @samp{make} to recompile the source files that really
8050 need recompilation, ensuring that the object files are up-to-date
8051 before you begin.
8053 @item
8054 Make the changes in the header files.
8056 @item
8057 Use the command @samp{make -t} to mark all the object files as
8058 up to date.  The next time you run @code{make}, the changes in the
8059 header files will not cause any recompilation.
8060 @end enumerate
8062 If you have already changed the header file at a time when some files
8063 do need recompilation, it is too late to do this.  Instead, you can
8064 use the @w{@samp{-o @var{file}}} flag, which marks a specified file as
8065 ``old'' (@pxref{Options Summary, ,Summary of Options}).  This means
8066 that the file itself will not be remade, and nothing else will be
8067 remade on its account.  Follow this procedure:
8069 @enumerate
8070 @item
8071 Recompile the source files that need compilation for reasons independent
8072 of the particular header file, with @samp{make -o @var{headerfile}}.
8073 If several header files are involved, use a separate @samp{-o} option
8074 for each header file.
8076 @item
8077 Touch all the object files with @samp{make -t}.
8078 @end enumerate
8080 @node Overriding, Testing, Avoiding Compilation, Running
8081 @section Overriding Variables
8082 @cindex overriding variables with arguments
8083 @cindex variables, overriding with arguments
8084 @cindex command line variables
8085 @cindex variables, command line
8087 An argument that contains @samp{=} specifies the value of a variable:
8088 @samp{@var{v}=@var{x}} sets the value of the variable @var{v} to @var{x}.
8089 If you specify a value in this way, all ordinary assignments of the same
8090 variable in the makefile are ignored; we say they have been
8091 @dfn{overridden} by the command line argument.
8093 The most common way to use this facility is to pass extra flags to
8094 compilers.  For example, in a properly written makefile, the variable
8095 @code{CFLAGS} is included in each recipe that runs the C compiler, so a
8096 file @file{foo.c} would be compiled something like this:
8098 @example
8099 cc -c $(CFLAGS) foo.c
8100 @end example
8102 Thus, whatever value you set for @code{CFLAGS} affects each compilation
8103 that occurs.  The makefile probably specifies the usual value for
8104 @code{CFLAGS}, like this:
8106 @example
8107 CFLAGS=-g
8108 @end example
8110 Each time you run @code{make}, you can override this value if you
8111 wish.  For example, if you say @samp{make CFLAGS='-g -O'}, each C
8112 compilation will be done with @samp{cc -c -g -O}.  (This also
8113 illustrates how you can use quoting in the shell to enclose spaces and
8114 other special characters in the value of a variable when you override
8115 it.)
8117 The variable @code{CFLAGS} is only one of many standard variables that
8118 exist just so that you can change them this way.  @xref{Implicit
8119 Variables, , Variables Used by Implicit Rules}, for a complete list.
8121 You can also program the makefile to look at additional variables of your
8122 own, giving the user the ability to control other aspects of how the
8123 makefile works by changing the variables.
8125 When you override a variable with a command line argument, you can
8126 define either a recursively-expanded variable or a simply-expanded
8127 variable.  The examples shown above make a recursively-expanded
8128 variable; to make a simply-expanded variable, write @samp{:=} instead
8129 of @samp{=}.  But, unless you want to include a variable reference or
8130 function call in the @emph{value} that you specify, it makes no
8131 difference which kind of variable you create.
8133 There is one way that the makefile can change a variable that you have
8134 overridden.  This is to use the @code{override} directive, which is a line
8135 that looks like this: @samp{override @var{variable} = @var{value}}
8136 (@pxref{Override Directive, ,The @code{override} Directive}).
8138 @node Testing, Options Summary, Overriding, Running
8139 @section Testing the Compilation of a Program
8140 @cindex testing compilation
8141 @cindex compilation, testing
8143 Normally, when an error happens in executing a shell command, @code{make}
8144 gives up immediately, returning a nonzero status.  No further recipes are
8145 executed for any target.  The error implies that the goal cannot be
8146 correctly remade, and @code{make} reports this as soon as it knows.
8148 When you are compiling a program that you have just changed, this is not
8149 what you want.  Instead, you would rather that @code{make} try compiling
8150 every file that can be tried, to show you as many compilation errors
8151 as possible.
8153 @cindex @code{-k}
8154 @cindex @code{--keep-going}
8155 On these occasions, you should use the @samp{-k} or
8156 @samp{--keep-going} flag.  This tells @code{make} to continue to
8157 consider the other prerequisites of the pending targets, remaking them
8158 if necessary, before it gives up and returns nonzero status.  For
8159 example, after an error in compiling one object file, @samp{make -k}
8160 will continue compiling other object files even though it already
8161 knows that linking them will be impossible.  In addition to continuing
8162 after failed shell commands, @samp{make -k} will continue as much as
8163 possible after discovering that it does not know how to make a target
8164 or prerequisite file.  This will always cause an error message, but
8165 without @samp{-k}, it is a fatal error (@pxref{Options Summary,
8166 ,Summary of Options}).@refill
8168 The usual behavior of @code{make} assumes that your purpose is to get the
8169 goals up to date; once @code{make} learns that this is impossible, it might
8170 as well report the failure immediately.  The @samp{-k} flag says that the
8171 real purpose is to test as much as possible of the changes made in the
8172 program, perhaps to find several independent problems so that you can
8173 correct them all before the next attempt to compile.  This is why Emacs'
8174 @kbd{M-x compile} command passes the @samp{-k} flag by default.
8176 @node Options Summary,  , Testing, Running
8177 @section Summary of Options
8178 @cindex options
8179 @cindex flags
8180 @cindex switches
8182 Here is a table of all the options @code{make} understands:
8184 @table @samp
8185 @item -b
8186 @cindex @code{-b}
8187 @itemx -m
8188 @cindex @code{-m}
8189 These options are ignored for compatibility with other versions of @code{make}.
8191 @item -B
8192 @cindex @code{-B}
8193 @itemx --always-make
8194 @cindex @code{--always-make}
8195 Consider all targets out-of-date.  GNU @code{make} proceeds to
8196 consider targets and their prerequisites using the normal algorithms;
8197 however, all targets so considered are always remade regardless of the
8198 status of their prerequisites.  To avoid infinite recursion, if
8199 @code{MAKE_RESTARTS} (@pxref{Special Variables, , Other Special
8200 Variables}) is set to a number greater than 0 this option is disabled
8201 when considering whether to remake makefiles (@pxref{Remaking
8202 Makefiles, , How Makefiles Are Remade}).
8204 @item -C @var{dir}
8205 @cindex @code{-C}
8206 @itemx --directory=@var{dir}
8207 @cindex @code{--directory}
8208 Change to directory @var{dir} before reading the makefiles.  If multiple
8209 @samp{-C} options are specified, each is interpreted relative to the
8210 previous one: @samp{-C / -C etc} is equivalent to @samp{-C /etc}.
8211 This is typically used with recursive invocations of @code{make}
8212 (@pxref{Recursion, ,Recursive Use of @code{make}}).
8214 @item -d
8215 @cindex @code{-d}
8216 @c Extra blank line here makes the table look better.
8218 Print debugging information in addition to normal processing.  The
8219 debugging information says which files are being considered for
8220 remaking, which file-times are being compared and with what results,
8221 which files actually need to be remade, which implicit rules are
8222 considered and which are applied---everything interesting about how
8223 @code{make} decides what to do.  The @code{-d} option is equivalent to
8224 @samp{--debug=a} (see below).
8226 @item --debug[=@var{options}]
8227 @cindex @code{--debug}
8228 @c Extra blank line here makes the table look better.
8230 Print debugging information in addition to normal processing.  Various
8231 levels and types of output can be chosen.  With no arguments, print the
8232 ``basic'' level of debugging.  Possible arguments are below; only the
8233 first character is considered, and values must be comma- or
8234 space-separated.
8236 @table @code
8237 @item a (@i{all})
8238 All types of debugging output are enabled.  This is equivalent to using
8239 @samp{-d}.
8241 @item b (@i{basic})
8242 Basic debugging prints each target that was found to be out-of-date, and
8243 whether the build was successful or not.
8245 @item v (@i{verbose})
8246 A level above @samp{basic}; includes messages about which makefiles were
8247 parsed, prerequisites that did not need to be rebuilt, etc.  This option
8248 also enables @samp{basic} messages.
8250 @item i (@i{implicit})
8251 Prints messages describing the implicit rule searches for each target.
8252 This option also enables @samp{basic} messages.
8254 @item j (@i{jobs})
8255 Prints messages giving details on the invocation of specific subcommands.
8257 @item m (@i{makefile})
8258 By default, the above messages are not enabled while trying to remake
8259 the makefiles.  This option enables messages while rebuilding makefiles,
8260 too.  Note that the @samp{all} option does enable this option.  This
8261 option also enables @samp{basic} messages.
8262 @end table
8264 @item -e
8265 @cindex @code{-e}
8266 @itemx --environment-overrides
8267 @cindex @code{--environment-overrides}
8268 Give variables taken from the environment precedence
8269 over variables from makefiles.
8270 @xref{Environment, ,Variables from the Environment}.
8272 @item --eval=@var{string}
8273 @cindex @code{--eval}
8274 @c Extra blank line here makes the table look better.
8276 Evaluate @var{string} as makefile syntax.  This is a command-line
8277 version of the @code{eval} function (@pxref{Eval Function}).  The
8278 evaluation is performed after the default rules and variables have
8279 been defined, but before any makefiles are read.
8281 @item -f @var{file}
8282 @cindex @code{-f}
8283 @itemx --file=@var{file}
8284 @cindex @code{--file}
8285 @itemx --makefile=@var{file}
8286 @cindex @code{--makefile}
8287 Read the file named @var{file} as a makefile.
8288 @xref{Makefiles, ,Writing Makefiles}.
8290 @item -h
8291 @cindex @code{-h}
8292 @itemx --help
8293 @cindex @code{--help}
8294 @c Extra blank line here makes the table look better.
8296 Remind you of the options that @code{make} understands and then exit.
8298 @item -i
8299 @cindex @code{-i}
8300 @itemx --ignore-errors
8301 @cindex @code{--ignore-errors}
8302 Ignore all errors in recipes executed to remake files.
8303 @xref{Errors, ,Errors in Recipes}.
8305 @item -I @var{dir}
8306 @cindex @code{-I}
8307 @itemx --include-dir=@var{dir}
8308 @cindex @code{--include-dir}
8309 Specifies a directory @var{dir} to search for included makefiles.
8310 @xref{Include, ,Including Other Makefiles}.  If several @samp{-I}
8311 options are used to specify several directories, the directories are
8312 searched in the order specified.
8314 @item -j [@var{jobs}]
8315 @cindex @code{-j}
8316 @itemx --jobs[=@var{jobs}]
8317 @cindex @code{--jobs}
8318 Specifies the number of recipes (jobs) to run simultaneously.  With no
8319 argument, @code{make} runs as many recipes simultaneously as possible.
8320 If there is more than one @samp{-j} option, the last one is effective.
8321 @xref{Parallel, ,Parallel Execution}, for more information on how
8322 recipes are run.  Note that this option is ignored on MS-DOS.
8324 @item -k
8325 @cindex @code{-k}
8326 @itemx --keep-going
8327 @cindex @code{--keep-going}
8328 Continue as much as possible after an error.  While the target that
8329 failed, and those that depend on it, cannot be remade, the other
8330 prerequisites of these targets can be processed all the same.
8331 @xref{Testing, ,Testing the Compilation of a Program}.
8333 @item -l [@var{load}]
8334 @cindex @code{-l}
8335 @itemx --load-average[=@var{load}]
8336 @cindex @code{--load-average}
8337 @itemx --max-load[=@var{load}]
8338 @cindex @code{--max-load}
8339 Specifies that no new recipes should be started if there are other
8340 recipes running and the load average is at least @var{load} (a
8341 floating-point number).  With no argument, removes a previous load
8342 limit.  @xref{Parallel, ,Parallel Execution}.
8344 @item -L
8345 @cindex @code{-L}
8346 @itemx --check-symlink-times
8347 @cindex @code{--check-symlink-times}
8348 On systems that support symbolic links, this option causes @code{make}
8349 to consider the timestamps on any symbolic links in addition to the
8350 timestamp on the file referenced by those links.  When this option is
8351 provided, the most recent timestamp among the file and the symbolic
8352 links is taken as the modification time for this target file.
8354 @item -n
8355 @cindex @code{-n}
8356 @itemx --just-print
8357 @cindex @code{--just-print}
8358 @itemx --dry-run
8359 @cindex @code{--dry-run}
8360 @itemx --recon
8361 @cindex @code{--recon}
8362 @c Extra blank line here makes the table look better.
8364 Print the recipe that would be executed, but do not execute it (except
8365 in certain circumstances).
8366 @xref{Instead of Execution, ,Instead of Executing Recipes}.
8368 @item -o @var{file}
8369 @cindex @code{-o}
8370 @itemx --old-file=@var{file}
8371 @cindex @code{--old-file}
8372 @itemx --assume-old=@var{file}
8373 @cindex @code{--assume-old}
8374 Do not remake the file @var{file} even if it is older than its
8375 prerequisites, and do not remake anything on account of changes in
8376 @var{file}.  Essentially the file is treated as very old and its rules
8377 are ignored.  @xref{Avoiding Compilation, ,Avoiding Recompilation of
8378 Some Files}.@refill
8380 @item -p
8381 @cindex @code{-p}
8382 @itemx --print-data-base
8383 @cindex @code{--print-data-base}
8384 @cindex data base of @code{make} rules
8385 @cindex predefined rules and variables, printing
8386 Print the data base (rules and variable values) that results from
8387 reading the makefiles; then execute as usual or as otherwise
8388 specified.  This also prints the version information given by the
8389 @samp{-v} switch (see below).  To print the data base without trying
8390 to remake any files, use @w{@samp{make -qp}}.  To print the data base
8391 of predefined rules and variables, use @w{@samp{make -p -f /dev/null}}.
8392 The data base output contains filename and linenumber information for
8393 recipe and variable definitions, so it can be a useful debugging tool
8394 in complex environments.
8396 @item -q
8397 @cindex @code{-q}
8398 @itemx --question
8399 @cindex @code{--question}
8400 ``Question mode''.  Do not run any recipes, or print anything; just
8401 return an exit status that is zero if the specified targets are already
8402 up to date, one if any remaking is required, or two if an error is
8403 encountered.  @xref{Instead of Execution, ,Instead of Executing
8404 Recipes}.@refill
8406 @item -r
8407 @cindex @code{-r}
8408 @itemx --no-builtin-rules
8409 @cindex @code{--no-builtin-rules}
8410 Eliminate use of the built-in implicit rules (@pxref{Implicit Rules,
8411 ,Using Implicit Rules}).  You can still define your own by writing
8412 pattern rules (@pxref{Pattern Rules, ,Defining and Redefining Pattern
8413 Rules}).  The @samp{-r} option also clears out the default list of
8414 suffixes for suffix rules (@pxref{Suffix Rules, ,Old-Fashioned Suffix
8415 Rules}).  But you can still define your own suffixes with a rule for
8416 @code{.SUFFIXES}, and then define your own suffix rules.  Note that only
8417 @emph{rules} are affected by the @code{-r} option; default variables
8418 remain in effect (@pxref{Implicit Variables, ,Variables Used by Implicit
8419 Rules}); see the @samp{-R} option below.
8421 @item -R
8422 @cindex @code{-R}
8423 @itemx --no-builtin-variables
8424 @cindex @code{--no-builtin-variables}
8425 Eliminate use of the built-in rule-specific variables (@pxref{Implicit
8426 Variables, ,Variables Used by Implicit Rules}).  You can still define
8427 your own, of course.  The @samp{-R} option also automatically enables
8428 the @samp{-r} option (see above), since it doesn't make sense to have
8429 implicit rules without any definitions for the variables that they use.
8431 @item -s
8432 @cindex @code{-s}
8433 @itemx --silent
8434 @cindex @code{--silent}
8435 @itemx --quiet
8436 @cindex @code{--quiet}
8437 @c Extra blank line here makes the table look better.
8439 Silent operation; do not print the recipes as they are executed.
8440 @xref{Echoing, ,Recipe Echoing}.
8442 @item -S
8443 @cindex @code{-S}
8444 @itemx --no-keep-going
8445 @cindex @code{--no-keep-going}
8446 @itemx --stop
8447 @cindex @code{--stop}
8448 @c Extra blank line here makes the table look better.
8450 Cancel the effect of the @samp{-k} option.  This is never necessary
8451 except in a recursive @code{make} where @samp{-k} might be inherited
8452 from the top-level @code{make} via @code{MAKEFLAGS}
8453 (@pxref{Recursion, ,Recursive Use of @code{make}})
8454 or if you set @samp{-k} in @code{MAKEFLAGS} in your environment.@refill
8456 @item -t
8457 @cindex @code{-t}
8458 @itemx --touch
8459 @cindex @code{--touch}
8460 @c Extra blank line here makes the table look better.
8462 Touch files (mark them up to date without really changing them)
8463 instead of running their recipes.  This is used to pretend that the
8464 recipes were done, in order to fool future invocations of
8465 @code{make}.  @xref{Instead of Execution, ,Instead of Executing Recipes}.
8467 @item --trace
8468 @cindex @code{--trace}
8469 @c Extra blank line here makes the table look better.
8471 Print the entire recipe to be executed, even for recipes that are
8472 normally silent (due to @code{.SILENT} or @samp{@@}).  Also print the
8473 makefile name and linenumber where the recipe was defined.
8475 @item -v
8476 @cindex @code{-v}
8477 @itemx --version
8478 @cindex @code{--version}
8479 Print the version of the @code{make} program plus a copyright, a list
8480 of authors, and a notice that there is no warranty; then exit.
8482 @item -w
8483 @cindex @code{-w}
8484 @itemx --print-directory
8485 @cindex @code{--print-directory}
8486 Print a message containing the working directory both before and after
8487 executing the makefile.  This may be useful for tracking down errors
8488 from complicated nests of recursive @code{make} commands.
8489 @xref{Recursion, ,Recursive Use of @code{make}}.  (In practice, you
8490 rarely need to specify this option since @samp{make} does it for you;
8491 see @ref{-w Option, ,The @samp{--print-directory} Option}.)
8493 @itemx --no-print-directory
8494 @cindex @code{--no-print-directory}
8495 Disable printing of the working directory under @code{-w}.
8496 This option is useful when @code{-w} is turned on automatically,
8497 but you do not want to see the extra messages.
8498 @xref{-w Option, ,The @samp{--print-directory} Option}.
8500 @item -W @var{file}
8501 @cindex @code{-W}
8502 @itemx --what-if=@var{file}
8503 @cindex @code{--what-if}
8504 @itemx --new-file=@var{file}
8505 @cindex @code{--new-file}
8506 @itemx --assume-new=@var{file}
8507 @cindex @code{--assume-new}
8508 Pretend that the target @var{file} has just been modified.  When used
8509 with the @samp{-n} flag, this shows you what would happen if you were
8510 to modify that file.  Without @samp{-n}, it is almost the same as
8511 running a @code{touch} command on the given file before running
8512 @code{make}, except that the modification time is changed only in the
8513 imagination of @code{make}.
8514 @xref{Instead of Execution, ,Instead of Executing Recipes}.
8516 @item --warn-undefined-variables
8517 @cindex @code{--warn-undefined-variables}
8518 @cindex variables, warning for undefined
8519 @cindex undefined variables, warning message
8520 Issue a warning message whenever @code{make} sees a reference to an
8521 undefined variable.  This can be helpful when you are trying to debug
8522 makefiles which use variables in complex ways.
8523 @end table
8525 @node Implicit Rules, Archives, Running, Top
8526 @chapter Using Implicit Rules
8527 @cindex implicit rule
8528 @cindex rule, implicit
8530 Certain standard ways of remaking target files are used very often.  For
8531 example, one customary way to make an object file is from a C source file
8532 using the C compiler, @code{cc}.
8534 @dfn{Implicit rules} tell @code{make} how to use customary techniques so
8535 that you do not have to specify them in detail when you want to use
8536 them.  For example, there is an implicit rule for C compilation.  File
8537 names determine which implicit rules are run.  For example, C
8538 compilation typically takes a @file{.c} file and makes a @file{.o} file.
8539 So @code{make} applies the implicit rule for C compilation when it sees
8540 this combination of file name endings.@refill
8542 A chain of implicit rules can apply in sequence; for example, @code{make}
8543 will remake a @file{.o} file from a @file{.y} file by way of a @file{.c} file.
8544 @iftex
8545 @xref{Chained Rules, ,Chains of Implicit Rules}.
8546 @end iftex
8548 The built-in implicit rules use several variables in their recipes so
8549 that, by changing the values of the variables, you can change the way the
8550 implicit rule works.  For example, the variable @code{CFLAGS} controls the
8551 flags given to the C compiler by the implicit rule for C compilation.
8552 @iftex
8553 @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
8554 @end iftex
8556 You can define your own implicit rules by writing @dfn{pattern rules}.
8557 @iftex
8558 @xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.
8559 @end iftex
8561 @dfn{Suffix rules} are a more limited way to define implicit rules.
8562 Pattern rules are more general and clearer, but suffix rules are
8563 retained for compatibility.
8564 @iftex
8565 @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
8566 @end iftex
8568 @menu
8569 * Using Implicit::              How to use an existing implicit rule
8570                                   to get the recipes for updating a file.
8571 * Catalogue of Rules::          A list of built-in implicit rules.
8572 * Implicit Variables::          How to change what predefined rules do.
8573 * Chained Rules::               How to use a chain of implicit rules.
8574 * Pattern Rules::               How to define new implicit rules.
8575 * Last Resort::                 How to define recipes for rules which
8576                                   cannot find any.
8577 * Suffix Rules::                The old-fashioned style of implicit rule.
8578 * Implicit Rule Search::        The precise algorithm for applying
8579                                   implicit rules.
8580 @end menu
8582 @node Using Implicit, Catalogue of Rules, Implicit Rules, Implicit Rules
8583 @section Using Implicit Rules
8584 @cindex implicit rule, how to use
8585 @cindex rule, implicit, how to use
8587 To allow @code{make} to find a customary method for updating a target
8588 file, all you have to do is refrain from specifying recipes yourself.
8589 Either write a rule with no recipe, or don't write a rule at all.
8590 Then @code{make} will figure out which implicit rule to use based on
8591 which kind of source file exists or can be made.
8593 For example, suppose the makefile looks like this:
8595 @example
8596 foo : foo.o bar.o
8597         cc -o foo foo.o bar.o $(CFLAGS) $(LDFLAGS)
8598 @end example
8600 @noindent
8601 Because you mention @file{foo.o} but do not give a rule for it, @code{make}
8602 will automatically look for an implicit rule that tells how to update it.
8603 This happens whether or not the file @file{foo.o} currently exists.
8605 If an implicit rule is found, it can supply both a recipe and one or
8606 more prerequisites (the source files).  You would want to write a rule
8607 for @file{foo.o} with no recipe if you need to specify additional
8608 prerequisites, such as header files, that the implicit rule cannot
8609 supply.
8611 Each implicit rule has a target pattern and prerequisite patterns.  There may
8612 be many implicit rules with the same target pattern.  For example, numerous
8613 rules make @samp{.o} files: one, from a @samp{.c} file with the C compiler;
8614 another, from a @samp{.p} file with the Pascal compiler; and so on.  The rule
8615 that actually applies is the one whose prerequisites exist or can be made.
8616 So, if you have a file @file{foo.c}, @code{make} will run the C compiler;
8617 otherwise, if you have a file @file{foo.p}, @code{make} will run the Pascal
8618 compiler; and so on.
8620 Of course, when you write the makefile, you know which implicit rule you
8621 want @code{make} to use, and you know it will choose that one because you
8622 know which possible prerequisite files are supposed to exist.
8623 @xref{Catalogue of Rules, ,Catalogue of Implicit Rules},
8624 for a catalogue of all the predefined implicit rules.
8626 Above, we said an implicit rule applies if the required prerequisites ``exist
8627 or can be made''.  A file ``can be made'' if it is mentioned explicitly in
8628 the makefile as a target or a prerequisite, or if an implicit rule can be
8629 recursively found for how to make it.  When an implicit prerequisite is the
8630 result of another implicit rule, we say that @dfn{chaining} is occurring.
8631 @xref{Chained Rules, ,Chains of Implicit Rules}.
8633 In general, @code{make} searches for an implicit rule for each target, and
8634 for each double-colon rule, that has no recipe.  A file that is mentioned
8635 only as a prerequisite is considered a target whose rule specifies nothing,
8636 so implicit rule search happens for it.  @xref{Implicit Rule Search, ,Implicit Rule Search Algorithm}, for the
8637 details of how the search is done.
8639 Note that explicit prerequisites do not influence implicit rule search.
8640 For example, consider this explicit rule:
8642 @example
8643 foo.o: foo.p
8644 @end example
8646 @noindent
8647 The prerequisite on @file{foo.p} does not necessarily mean that
8648 @code{make} will remake @file{foo.o} according to the implicit rule to
8649 make an object file, a @file{.o} file, from a Pascal source file, a
8650 @file{.p} file.  For example, if @file{foo.c} also exists, the implicit
8651 rule to make an object file from a C source file is used instead,
8652 because it appears before the Pascal rule in the list of predefined
8653 implicit rules (@pxref{Catalogue of Rules, , Catalogue of Implicit
8654 Rules}).
8656 If you do not want an implicit rule to be used for a target that has no
8657 recipe, you can give that target an empty recipe by writing a semicolon
8658 (@pxref{Empty Recipes, ,Defining Empty Recipes}).
8660 @node Catalogue of Rules, Implicit Variables, Using Implicit, Implicit Rules
8661 @section Catalogue of Implicit Rules
8662 @cindex implicit rule, predefined
8663 @cindex rule, implicit, predefined
8665 Here is a catalogue of predefined implicit rules which are always
8666 available unless the makefile explicitly overrides or cancels them.
8667 @xref{Canceling Rules, ,Canceling Implicit Rules}, for information on
8668 canceling or overriding an implicit rule.  The @samp{-r} or
8669 @samp{--no-builtin-rules} option cancels all predefined rules.
8671 This manual only documents the default rules available on POSIX-based
8672 operating systems.  Other operating systems, such as VMS, Windows,
8673 OS/2, etc. may have different sets of default rules.  To see the full
8674 list of default rules and variables available in your version of GNU
8675 @code{make}, run @samp{make -p} in a directory with no makefile.
8677 Not all of these rules will always be defined, even when the @samp{-r}
8678 option is not given.  Many of the predefined implicit rules are
8679 implemented in @code{make} as suffix rules, so which ones will be
8680 defined depends on the @dfn{suffix list} (the list of prerequisites of
8681 the special target @code{.SUFFIXES}).  The default suffix list is:
8682 @code{.out}, @code{.a}, @code{.ln}, @code{.o}, @code{.c}, @code{.cc},
8683 @code{.C}, @code{.cpp}, @code{.p}, @code{.f}, @code{.F}, @code{.m},
8684 @code{.r}, @code{.y}, @code{.l}, @code{.ym}, @code{.lm}, @code{.s},
8685 @code{.S}, @code{.mod}, @code{.sym}, @code{.def}, @code{.h},
8686 @code{.info}, @code{.dvi}, @code{.tex}, @code{.texinfo}, @code{.texi},
8687 @code{.txinfo}, @code{.w}, @code{.ch} @code{.web}, @code{.sh},
8688 @code{.elc}, @code{.el}.  All of the implicit rules described below
8689 whose prerequisites have one of these suffixes are actually suffix
8690 rules.  If you modify the suffix list, the only predefined suffix
8691 rules in effect will be those named by one or two of the suffixes that
8692 are on the list you specify; rules whose suffixes fail to be on the
8693 list are disabled.  @xref{Suffix Rules, ,Old-Fashioned Suffix Rules},
8694 for full details on suffix rules.
8696 @table @asis
8697 @item Compiling C programs
8698 @cindex C, rule to compile
8699 @pindex cc
8700 @pindex gcc
8701 @pindex .o
8702 @pindex .c
8703 @file{@var{n}.o} is made automatically from @file{@var{n}.c} with
8704 a recipe of the form @samp{$(CC) $(CPPFLAGS) $(CFLAGS) -c}.@refill
8706 @item Compiling C++ programs
8707 @cindex C++, rule to compile
8708 @pindex g++
8709 @pindex .cc
8710 @pindex .cpp
8711 @pindex .C
8712 @file{@var{n}.o} is made automatically from @file{@var{n}.cc},
8713 @file{@var{n}.cpp}, or @file{@var{n}.C} with a recipe of the form
8714 @samp{$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c}.  We encourage you to use the
8715 suffix @samp{.cc} for C++ source files instead of @samp{.C}.@refill
8717 @item Compiling Pascal programs
8718 @cindex Pascal, rule to compile
8719 @pindex pc
8720 @pindex .p
8721 @file{@var{n}.o} is made automatically from @file{@var{n}.p}
8722 with the recipe @samp{$(PC) $(PFLAGS) -c}.@refill
8724 @item Compiling Fortran and Ratfor programs
8725 @cindex Fortran, rule to compile
8726 @cindex Ratfor, rule to compile
8727 @pindex f77
8728 @pindex .f
8729 @pindex .r
8730 @pindex .F
8731 @file{@var{n}.o} is made automatically from @file{@var{n}.r},
8732 @file{@var{n}.F} or @file{@var{n}.f} by running the
8733 Fortran compiler.  The precise recipe used is as follows:@refill
8735 @table @samp
8736 @item .f
8737 @samp{$(FC) $(FFLAGS) -c}.
8738 @item .F
8739 @samp{$(FC) $(FFLAGS) $(CPPFLAGS) -c}.
8740 @item .r
8741 @samp{$(FC) $(FFLAGS) $(RFLAGS) -c}.
8742 @end table
8744 @item Preprocessing Fortran and Ratfor programs
8745 @file{@var{n}.f} is made automatically from @file{@var{n}.r} or
8746 @file{@var{n}.F}.  This rule runs just the preprocessor to convert a
8747 Ratfor or preprocessable Fortran program into a strict Fortran
8748 program.  The precise recipe used is as follows:@refill
8750 @table @samp
8751 @item .F
8752 @samp{$(FC) $(CPPFLAGS) $(FFLAGS) -F}.
8753 @item .r
8754 @samp{$(FC) $(FFLAGS) $(RFLAGS) -F}.
8755 @end table
8757 @item Compiling Modula-2 programs
8758 @cindex Modula-2, rule to compile
8759 @pindex m2c
8760 @pindex .sym
8761 @pindex .def
8762 @pindex .mod
8763 @file{@var{n}.sym} is made from @file{@var{n}.def} with a recipe
8764 of the form @samp{$(M2C) $(M2FLAGS) $(DEFFLAGS)}.  @file{@var{n}.o}
8765 is made from @file{@var{n}.mod}; the form is:
8766 @w{@samp{$(M2C) $(M2FLAGS) $(MODFLAGS)}}.@refill
8768 @need 1200
8769 @item Assembling and preprocessing assembler programs
8770 @cindex assembly, rule to compile
8771 @pindex as
8772 @pindex .s
8773 @file{@var{n}.o} is made automatically from @file{@var{n}.s} by
8774 running the assembler, @code{as}.  The precise recipe is
8775 @samp{$(AS) $(ASFLAGS)}.@refill
8777 @pindex .S
8778 @file{@var{n}.s} is made automatically from @file{@var{n}.S} by
8779 running the C preprocessor, @code{cpp}.  The precise recipe is
8780 @w{@samp{$(CPP) $(CPPFLAGS)}}.
8782 @item Linking a single object file
8783 @cindex linking, predefined rule for
8784 @pindex ld
8785 @pindex .o
8786 @file{@var{n}} is made automatically from @file{@var{n}.o} by running
8787 the linker (usually called @code{ld}) via the C compiler.  The precise
8788 recipe used is @w{@samp{$(CC) $(LDFLAGS) @var{n}.o $(LOADLIBES) $(LDLIBS)}}.
8790 This rule does the right thing for a simple program with only one
8791 source file.  It will also do the right thing if there are multiple
8792 object files (presumably coming from various other source files), one
8793 of which has a name matching that of the executable file.  Thus,
8795 @example
8796 x: y.o z.o
8797 @end example
8799 @noindent
8800 when @file{x.c}, @file{y.c} and @file{z.c} all exist will execute:
8802 @example
8803 @group
8804 cc -c x.c -o x.o
8805 cc -c y.c -o y.o
8806 cc -c z.c -o z.o
8807 cc x.o y.o z.o -o x
8808 rm -f x.o
8809 rm -f y.o
8810 rm -f z.o
8811 @end group
8812 @end example
8814 @noindent
8815 In more complicated cases, such as when there is no object file whose
8816 name derives from the executable file name, you must write an explicit
8817 recipe for linking.
8819 Each kind of file automatically made into @samp{.o} object files will
8820 be automatically linked by using the compiler (@samp{$(CC)},
8821 @samp{$(FC)} or @samp{$(PC)}; the C compiler @samp{$(CC)} is used to
8822 assemble @samp{.s} files) without the @samp{-c} option.  This could be
8823 done by using the @samp{.o} object files as intermediates, but it is
8824 faster to do the compiling and linking in one step, so that's how it's
8825 done.@refill
8827 @item Yacc for C programs
8828 @pindex yacc
8829 @cindex Yacc, rule to run
8830 @pindex .y
8831 @file{@var{n}.c} is made automatically from @file{@var{n}.y} by
8832 running Yacc with the recipe @samp{$(YACC) $(YFLAGS)}.
8834 @item Lex for C programs
8835 @pindex lex
8836 @cindex Lex, rule to run
8837 @pindex .l
8838 @file{@var{n}.c} is made automatically from @file{@var{n}.l} by
8839 running Lex.  The actual recipe is @samp{$(LEX) $(LFLAGS)}.
8841 @item Lex for Ratfor programs
8842 @file{@var{n}.r} is made automatically from @file{@var{n}.l} by
8843 running Lex.  The actual recipe is @samp{$(LEX) $(LFLAGS)}.
8845 The convention of using the same suffix @samp{.l} for all Lex files
8846 regardless of whether they produce C code or Ratfor code makes it
8847 impossible for @code{make} to determine automatically which of the two
8848 languages you are using in any particular case.  If @code{make} is
8849 called upon to remake an object file from a @samp{.l} file, it must
8850 guess which compiler to use.  It will guess the C compiler, because
8851 that is more common.  If you are using Ratfor, make sure @code{make}
8852 knows this by mentioning @file{@var{n}.r} in the makefile.  Or, if you
8853 are using Ratfor exclusively, with no C files, remove @samp{.c} from
8854 the list of implicit rule suffixes with:@refill
8856 @example
8857 @group
8858 .SUFFIXES:
8859 .SUFFIXES: .o .r .f .l @dots{}
8860 @end group
8861 @end example
8863 @item Making Lint Libraries from C, Yacc, or Lex programs
8864 @pindex lint
8865 @cindex @code{lint}, rule to run
8866 @pindex .ln
8867 @file{@var{n}.ln} is made from @file{@var{n}.c} by running @code{lint}.
8868 The precise recipe is @w{@samp{$(LINT) $(LINTFLAGS) $(CPPFLAGS) -i}}.
8869 The same recipe is used on the C code produced from
8870 @file{@var{n}.y} or @file{@var{n}.l}.@refill
8872 @item @TeX{} and Web
8873 @cindex @TeX{}, rule to run
8874 @cindex Web, rule to run
8875 @pindex tex
8876 @pindex cweave
8877 @pindex weave
8878 @pindex tangle
8879 @pindex ctangle
8880 @pindex .dvi
8881 @pindex .tex
8882 @pindex .web
8883 @pindex .w
8884 @pindex .ch
8885 @file{@var{n}.dvi} is made from @file{@var{n}.tex} with the recipe
8886 @samp{$(TEX)}.  @file{@var{n}.tex} is made from @file{@var{n}.web} with
8887 @samp{$(WEAVE)}, or from @file{@var{n}.w} (and from @file{@var{n}.ch} if
8888 it exists or can be made) with @samp{$(CWEAVE)}.  @file{@var{n}.p} is
8889 made from @file{@var{n}.web} with @samp{$(TANGLE)} and @file{@var{n}.c}
8890 is made from @file{@var{n}.w} (and from @file{@var{n}.ch} if it exists
8891 or can be made) with @samp{$(CTANGLE)}.@refill
8893 @item Texinfo and Info
8894 @cindex Texinfo, rule to format
8895 @cindex Info, rule to format
8896 @pindex texi2dvi
8897 @pindex makeinfo
8898 @pindex .texinfo
8899 @pindex .info
8900 @pindex .texi
8901 @pindex .txinfo
8902 @file{@var{n}.dvi} is made from @file{@var{n}.texinfo},
8903 @file{@var{n}.texi}, or @file{@var{n}.txinfo}, with the recipe
8904 @w{@samp{$(TEXI2DVI) $(TEXI2DVI_FLAGS)}}.  @file{@var{n}.info} is made from
8905 @file{@var{n}.texinfo}, @file{@var{n}.texi}, or @file{@var{n}.txinfo}, with
8906 the recipe @w{@samp{$(MAKEINFO) $(MAKEINFO_FLAGS)}}.
8908 @item RCS
8909 @cindex RCS, rule to extract from
8910 @pindex co
8911 @pindex ,v @r{(RCS file extension)}
8912 Any file @file{@var{n}} is extracted if necessary from an RCS file
8913 named either @file{@var{n},v} or @file{RCS/@var{n},v}.  The precise
8914 recipe used is @w{@samp{$(CO) $(COFLAGS)}}.  @file{@var{n}} will not be
8915 extracted from RCS if it already exists, even if the RCS file is
8916 newer.  The rules for RCS are terminal
8917 (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}),
8918 so RCS files cannot be generated from another source; they must
8919 actually exist.@refill
8921 @item SCCS
8922 @cindex SCCS, rule to extract from
8923 @pindex get
8924 @pindex s. @r{(SCCS file prefix)}
8925 Any file @file{@var{n}} is extracted if necessary from an SCCS file
8926 named either @file{s.@var{n}} or @file{SCCS/s.@var{n}}.  The precise
8927 recipe used is @w{@samp{$(GET) $(GFLAGS)}}.  The rules for SCCS are
8928 terminal (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}),
8929 so SCCS files cannot be generated from another source; they must
8930 actually exist.@refill
8932 @pindex .sh
8933 For the benefit of SCCS, a file @file{@var{n}} is copied from
8934 @file{@var{n}.sh} and made executable (by everyone).  This is for
8935 shell scripts that are checked into SCCS.  Since RCS preserves the
8936 execution permission of a file, you do not need to use this feature
8937 with RCS.@refill
8939 We recommend that you avoid using of SCCS.  RCS is widely held to be
8940 superior, and is also free.  By choosing free software in place of
8941 comparable (or inferior) proprietary software, you support the free
8942 software movement.
8943 @end table
8945 Usually, you want to change only the variables listed in the table
8946 above, which are documented in the following section.
8948 However, the recipes in built-in implicit rules actually use
8949 variables such as @code{COMPILE.c}, @code{LINK.p}, and
8950 @code{PREPROCESS.S}, whose values contain the recipes listed above.
8952 @code{make} follows the convention that the rule to compile a
8953 @file{.@var{x}} source file uses the variable @code{COMPILE.@var{x}}.
8954 Similarly, the rule to produce an executable from a @file{.@var{x}}
8955 file uses @code{LINK.@var{x}}; and the rule to preprocess a
8956 @file{.@var{x}} file uses @code{PREPROCESS.@var{x}}.
8958 @vindex OUTPUT_OPTION
8959 Every rule that produces an object file uses the variable
8960 @code{OUTPUT_OPTION}.  @code{make} defines this variable either to
8961 contain @samp{-o $@@}, or to be empty, depending on a compile-time
8962 option.  You need the @samp{-o} option to ensure that the output goes
8963 into the right file when the source file is in a different directory,
8964 as when using @code{VPATH} (@pxref{Directory Search}).  However,
8965 compilers on some systems do not accept a @samp{-o} switch for object
8966 files.  If you use such a system, and use @code{VPATH}, some
8967 compilations will put their output in the wrong place.
8968 A possible workaround for this problem is to give @code{OUTPUT_OPTION}
8969 the value @w{@samp{; mv $*.o $@@}}.
8971 @node Implicit Variables, Chained Rules, Catalogue of Rules, Implicit Rules
8972 @section Variables Used by Implicit Rules
8973 @cindex flags for compilers
8975 The recipes in built-in implicit rules make liberal use of certain
8976 predefined variables.  You can alter the values of these variables in
8977 the makefile, with arguments to @code{make}, or in the environment to
8978 alter how the implicit rules work without redefining the rules
8979 themselves.  You can cancel all variables used by implicit rules with
8980 the @samp{-R} or @samp{--no-builtin-variables} option.
8982 For example, the recipe used to compile a C source file actually says
8983 @samp{$(CC) -c $(CFLAGS) $(CPPFLAGS)}.  The default values of the variables
8984 used are @samp{cc} and nothing, resulting in the command @samp{cc -c}.  By
8985 redefining @samp{CC} to @samp{ncc}, you could cause @samp{ncc} to be
8986 used for all C compilations performed by the implicit rule.  By redefining
8987 @samp{CFLAGS} to be @samp{-g}, you could pass the @samp{-g} option to
8988 each compilation.  @emph{All} implicit rules that do C compilation use
8989 @samp{$(CC)} to get the program name for the compiler and @emph{all}
8990 include @samp{$(CFLAGS)} among the arguments given to the compiler.@refill
8992 The variables used in implicit rules fall into two classes: those that are
8993 names of programs (like @code{CC}) and those that contain arguments for the
8994 programs (like @code{CFLAGS}).  (The ``name of a program'' may also contain
8995 some command arguments, but it must start with an actual executable program
8996 name.)  If a variable value contains more than one argument, separate them
8997 with spaces.
8999 The following tables describe of some of the more commonly-used predefined
9000 variables.  This list is not exhaustive, and the default values shown here may
9001 not be what @code{make} selects for your environment.  To see the
9002 complete list of predefined variables for your instance of GNU @code{make} you
9003 can run @samp{make -p} in a directory with no makefiles.
9005 Here is a table of some of the more common variables used as names of
9006 programs in built-in rules:
9008 @table @code
9009 @item AR
9010 @vindex AR
9011 Archive-maintaining program; default @samp{ar}.
9012 @pindex ar
9014 @item AS
9015 @vindex AS
9016 Program for compiling assembly files; default @samp{as}.
9017 @pindex as
9019 @item CC
9020 @vindex CC
9021 Program for compiling C programs; default @samp{cc}.
9022 @pindex cc
9024 @item CXX
9025 @vindex CXX
9026 Program for compiling C++ programs; default @samp{g++}.
9027 @pindex g++
9029 @item CPP
9030 @vindex CPP
9031 Program for running the C preprocessor, with results to standard output;
9032 default @samp{$(CC) -E}.
9034 @item FC
9035 @vindex FC
9036 Program for compiling or preprocessing Fortran and Ratfor programs;
9037 default @samp{f77}.
9038 @pindex f77
9040 @item M2C
9041 @vindex M2C
9042 Program to use to compile Modula-2 source code; default @samp{m2c}.
9043 @pindex m2c
9045 @item PC
9046 @vindex PC
9047 Program for compiling Pascal programs; default @samp{pc}.
9048 @pindex pc
9050 @item CO
9051 @vindex CO
9052 Program for extracting a file from RCS; default @samp{co}.
9053 @pindex co
9055 @item GET
9056 @vindex GET
9057 Program for extracting a file from SCCS; default @samp{get}.
9058 @pindex get
9060 @item LEX
9061 @vindex LEX
9062 Program to use to turn Lex grammars into source code; default @samp{lex}.
9063 @pindex lex
9065 @item YACC
9066 @vindex YACC
9067 Program to use to turn Yacc grammars into source code; default @samp{yacc}.
9068 @pindex yacc
9070 @item LINT
9071 @vindex LINT
9072 Program to use to run lint on source code; default @samp{lint}.
9073 @pindex lint
9075 @item MAKEINFO
9076 @vindex MAKEINFO
9077 Program to convert a Texinfo source file into an Info file; default
9078 @samp{makeinfo}.
9079 @pindex makeinfo
9081 @item TEX
9082 @vindex TEX
9083 Program to make @TeX{} @sc{dvi} files from @TeX{} source;
9084 default @samp{tex}.
9085 @pindex tex
9087 @item TEXI2DVI
9088 @vindex TEXI2DVI
9089 Program to make @TeX{} @sc{dvi} files from Texinfo source;
9090 default @samp{texi2dvi}.
9091 @pindex texi2dvi
9093 @item WEAVE
9094 @vindex WEAVE
9095 Program to translate Web into @TeX{}; default @samp{weave}.
9096 @pindex weave
9098 @item CWEAVE
9099 @vindex CWEAVE
9100 Program to translate C Web into @TeX{}; default @samp{cweave}.
9101 @pindex cweave
9103 @item TANGLE
9104 @vindex TANGLE
9105 Program to translate Web into Pascal; default @samp{tangle}.
9106 @pindex tangle
9108 @item CTANGLE
9109 @vindex CTANGLE
9110 Program to translate C Web into C; default @samp{ctangle}.
9111 @pindex ctangle
9113 @item RM
9114 @vindex RM
9115 Command to remove a file; default @samp{rm -f}.
9116 @pindex rm
9117 @end table
9119 Here is a table of variables whose values are additional arguments for the
9120 programs above.  The default values for all of these is the empty
9121 string, unless otherwise noted.
9123 @table @code
9124 @item ARFLAGS
9125 @vindex ARFLAGS
9126 Flags to give the archive-maintaining program; default @samp{rv}.
9128 @item ASFLAGS
9129 @vindex ASFLAGS
9130 Extra flags to give to the assembler (when explicitly
9131 invoked on a @samp{.s} or @samp{.S} file).
9133 @item CFLAGS
9134 @vindex CFLAGS
9135 Extra flags to give to the C compiler.
9137 @item CXXFLAGS
9138 @vindex CXXFLAGS
9139 Extra flags to give to the C++ compiler.
9141 @item COFLAGS
9142 @vindex COFLAGS
9143 Extra flags to give to the RCS @code{co} program.
9145 @item CPPFLAGS
9146 @vindex CPPFLAGS
9147 Extra flags to give to the C preprocessor and programs
9148 that use it (the C and Fortran compilers).
9150 @item FFLAGS
9151 @vindex FFLAGS
9152 Extra flags to give to the Fortran compiler.
9154 @item GFLAGS
9155 @vindex GFLAGS
9156 Extra flags to give to the SCCS @code{get} program.
9158 @item LDFLAGS
9159 @vindex LDFLAGS
9160 Extra flags to give to compilers when they are supposed to invoke the linker,
9161 @samp{ld}.
9163 @item LDLIBS
9164 @vindex LDLIBS
9165 @vindex LOADLIBES
9166 Library flags or names given to compilers when they are supposed to
9167 invoke the linker, @samp{ld}.  @code{LOADLIBES} is a deprecated (but
9168 still supported) alternative to @code{LDLIBS}.
9170 @item LFLAGS
9171 @vindex LFLAGS
9172 Extra flags to give to Lex.
9174 @item YFLAGS
9175 @vindex YFLAGS
9176 Extra flags to give to Yacc.
9178 @item PFLAGS
9179 @vindex PFLAGS
9180 Extra flags to give to the Pascal compiler.
9182 @item RFLAGS
9183 @vindex RFLAGS
9184 Extra flags to give to the Fortran compiler for Ratfor programs.
9186 @item LINTFLAGS
9187 @vindex LINTFLAGS
9188 Extra flags to give to lint.
9189 @end table
9191 @node Chained Rules, Pattern Rules, Implicit Variables, Implicit Rules
9192 @section Chains of Implicit Rules
9194 @cindex chains of rules
9195 @cindex rule, implicit, chains of
9196 Sometimes a file can be made by a sequence of implicit rules.  For example,
9197 a file @file{@var{n}.o} could be made from @file{@var{n}.y} by running
9198 first Yacc and then @code{cc}.  Such a sequence is called a @dfn{chain}.
9200 If the file @file{@var{n}.c} exists, or is mentioned in the makefile, no
9201 special searching is required: @code{make} finds that the object file can
9202 be made by C compilation from @file{@var{n}.c}; later on, when considering
9203 how to make @file{@var{n}.c}, the rule for running Yacc is
9204 used.  Ultimately both @file{@var{n}.c} and @file{@var{n}.o} are
9205 updated.@refill
9207 @cindex intermediate files
9208 @cindex files, intermediate
9209 However, even if @file{@var{n}.c} does not exist and is not mentioned,
9210 @code{make} knows how to envision it as the missing link between
9211 @file{@var{n}.o} and @file{@var{n}.y}!  In this case, @file{@var{n}.c} is
9212 called an @dfn{intermediate file}.  Once @code{make} has decided to use the
9213 intermediate file, it is entered in the data base as if it had been
9214 mentioned in the makefile, along with the implicit rule that says how to
9215 create it.@refill
9217 Intermediate files are remade using their rules just like all other
9218 files.  But intermediate files are treated differently in two ways.
9220 The first difference is what happens if the intermediate file does not
9221 exist.  If an ordinary file @var{b} does not exist, and @code{make}
9222 considers a target that depends on @var{b}, it invariably creates
9223 @var{b} and then updates the target from @var{b}.  But if @var{b} is an
9224 intermediate file, then @code{make} can leave well enough alone.  It
9225 won't bother updating @var{b}, or the ultimate target, unless some
9226 prerequisite of @var{b} is newer than that target or there is some other
9227 reason to update that target.
9229 The second difference is that if @code{make} @emph{does} create @var{b}
9230 in order to update something else, it deletes @var{b} later on after it
9231 is no longer needed.  Therefore, an intermediate file which did not
9232 exist before @code{make} also does not exist after @code{make}.
9233 @code{make} reports the deletion to you by printing a @samp{rm -f}
9234 command showing which file it is deleting.
9236 Ordinarily, a file cannot be intermediate if it is mentioned in the
9237 makefile as a target or prerequisite.  However, you can explicitly mark a
9238 file as intermediate by listing it as a prerequisite of the special target
9239 @code{.INTERMEDIATE}.  This takes effect even if the file is mentioned
9240 explicitly in some other way.
9242 @cindex intermediate files, preserving
9243 @cindex preserving intermediate files
9244 @cindex secondary files
9245 You can prevent automatic deletion of an intermediate file by marking it
9246 as a @dfn{secondary} file.  To do this, list it as a prerequisite of the
9247 special target @code{.SECONDARY}.  When a file is secondary, @code{make}
9248 will not create the file merely because it does not already exist, but
9249 @code{make} does not automatically delete the file.  Marking a file as
9250 secondary also marks it as intermediate.
9252 You can list the target pattern of an implicit rule (such as @samp{%.o})
9253 as a prerequisite of the special target @code{.PRECIOUS} to preserve
9254 intermediate files made by implicit rules whose target patterns match
9255 that file's name; see @ref{Interrupts}.@refill
9256 @cindex preserving with @code{.PRECIOUS}
9257 @cindex @code{.PRECIOUS} intermediate files
9259 A chain can involve more than two implicit rules.  For example, it is
9260 possible to make a file @file{foo} from @file{RCS/foo.y,v} by running RCS,
9261 Yacc and @code{cc}.  Then both @file{foo.y} and @file{foo.c} are
9262 intermediate files that are deleted at the end.@refill
9264 No single implicit rule can appear more than once in a chain.  This means
9265 that @code{make} will not even consider such a ridiculous thing as making
9266 @file{foo} from @file{foo.o.o} by running the linker twice.  This
9267 constraint has the added benefit of preventing any infinite loop in the
9268 search for an implicit rule chain.
9270 There are some special implicit rules to optimize certain cases that would
9271 otherwise be handled by rule chains.  For example, making @file{foo} from
9272 @file{foo.c} could be handled by compiling and linking with separate
9273 chained rules, using @file{foo.o} as an intermediate file.  But what
9274 actually happens is that a special rule for this case does the compilation
9275 and linking with a single @code{cc} command.  The optimized rule is used in
9276 preference to the step-by-step chain because it comes earlier in the
9277 ordering of rules.
9279 @node Pattern Rules, Last Resort, Chained Rules, Implicit Rules
9280 @section Defining and Redefining Pattern Rules
9282 You define an implicit rule by writing a @dfn{pattern rule}.  A pattern
9283 rule looks like an ordinary rule, except that its target contains the
9284 character @samp{%} (exactly one of them).  The target is considered a
9285 pattern for matching file names; the @samp{%} can match any nonempty
9286 substring, while other characters match only themselves.  The prerequisites
9287 likewise use @samp{%} to show how their names relate to the target name.
9289 Thus, a pattern rule @samp{%.o : %.c} says how to make any file
9290 @file{@var{stem}.o} from another file @file{@var{stem}.c}.@refill
9292 Note that expansion using @samp{%} in pattern rules occurs
9293 @strong{after} any variable or function expansions, which take place
9294 when the makefile is read.  @xref{Using Variables, , How to Use
9295 Variables}, and @ref{Functions, ,Functions for Transforming Text}.
9297 @menu
9298 * Pattern Intro::               An introduction to pattern rules.
9299 * Pattern Examples::            Examples of pattern rules.
9300 * Automatic Variables::         How to use automatic variables in the
9301                                   recipes of implicit rules.
9302 * Pattern Match::               How patterns match.
9303 * Match-Anything Rules::        Precautions you should take prior to
9304                                   defining rules that can match any
9305                                   target file whatever.
9306 * Canceling Rules::             How to override or cancel built-in rules.
9307 @end menu
9309 @node Pattern Intro, Pattern Examples, Pattern Rules, Pattern Rules
9310 @subsection Introduction to Pattern Rules
9311 @cindex pattern rule
9312 @cindex rule, pattern
9314 A pattern rule contains the character @samp{%} (exactly one of them)
9315 in the target; otherwise, it looks exactly like an ordinary rule.  The
9316 target is a pattern for matching file names; the @samp{%} matches any
9317 nonempty substring, while other characters match only themselves.
9318 @cindex target pattern, implicit
9319 @cindex @code{%}, in pattern rules
9321 For example, @samp{%.c} as a pattern matches any file name that ends in
9322 @samp{.c}.  @samp{s.%.c} as a pattern matches any file name that starts
9323 with @samp{s.}, ends in @samp{.c} and is at least five characters long.
9324 (There must be at least one character to match the @samp{%}.)  The substring
9325 that the @samp{%} matches is called the @dfn{stem}.@refill
9327 @samp{%} in a prerequisite of a pattern rule stands for the same stem
9328 that was matched by the @samp{%} in the target.  In order for the
9329 pattern rule to apply, its target pattern must match the file name
9330 under consideration and all of its prerequisites (after pattern
9331 substitution) must name files that exist or can be made.  These files
9332 become prerequisites of the target.
9333 @cindex prerequisite pattern, implicit
9335 Thus, a rule of the form
9337 @example
9338 %.o : %.c ; @var{recipe}@dots{}
9339 @end example
9341 @noindent
9342 specifies how to make a file @file{@var{n}.o}, with another file
9343 @file{@var{n}.c} as its prerequisite, provided that @file{@var{n}.c}
9344 exists or can be made.
9346 There may also be prerequisites that do not use @samp{%}; such a prerequisite
9347 attaches to every file made by this pattern rule.  These unvarying
9348 prerequisites are useful occasionally.
9350 A pattern rule need not have any prerequisites that contain @samp{%}, or
9351 in fact any prerequisites at all.  Such a rule is effectively a general
9352 wildcard.  It provides a way to make any file that matches the target
9353 pattern.  @xref{Last Resort}.
9355 More than one pattern rule may match a target.  In this case
9356 @code{make} will choose the ``best fit'' rule.  @xref{Pattern Match,
9357 ,How Patterns Match}.
9359 @c !!! The end of of this paragraph should be rewritten.  --bob
9360 Pattern rules may have more than one target.  Unlike normal rules,
9361 this does not act as many different rules with the same prerequisites
9362 and recipe.  If a pattern rule has multiple targets, @code{make} knows
9363 that the rule's recipe is responsible for making all of the targets.
9364 The recipe is executed only once to make all the targets.  When
9365 searching for a pattern rule to match a target, the target patterns of
9366 a rule other than the one that matches the target in need of a rule
9367 are incidental: @code{make} worries only about giving a recipe and
9368 prerequisites to the file presently in question.  However, when this
9369 file's recipe is run, the other targets are marked as having been
9370 updated themselves.
9371 @cindex multiple targets, in pattern rule
9372 @cindex target, multiple in pattern rule
9374 @node Pattern Examples, Automatic Variables, Pattern Intro, Pattern Rules
9375 @subsection Pattern Rule Examples
9377 Here are some examples of pattern rules actually predefined in
9378 @code{make}.  First, the rule that compiles @samp{.c} files into @samp{.o}
9379 files:@refill
9381 @example
9382 %.o : %.c
9383         $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@@
9384 @end example
9386 @noindent
9387 defines a rule that can make any file @file{@var{x}.o} from
9388 @file{@var{x}.c}.  The recipe uses the automatic variables @samp{$@@} and
9389 @samp{$<} to substitute the names of the target file and the source file
9390 in each case where the rule applies (@pxref{Automatic Variables}).@refill
9392 Here is a second built-in rule:
9394 @example
9395 % :: RCS/%,v
9396         $(CO) $(COFLAGS) $<
9397 @end example
9399 @noindent
9400 defines a rule that can make any file @file{@var{x}} whatsoever from a
9401 corresponding file @file{@var{x},v} in the subdirectory @file{RCS}.  Since
9402 the target is @samp{%}, this rule will apply to any file whatever, provided
9403 the appropriate prerequisite file exists.  The double colon makes the rule
9404 @dfn{terminal}, which means that its prerequisite may not be an intermediate
9405 file (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}).@refill
9407 @need 500
9408 This pattern rule has two targets:
9410 @example
9411 @group
9412 %.tab.c %.tab.h: %.y
9413         bison -d $<
9414 @end group
9415 @end example
9417 @noindent
9418 @c The following paragraph is rewritten to avoid overfull hboxes
9419 This tells @code{make} that the recipe @samp{bison -d @var{x}.y} will
9420 make both @file{@var{x}.tab.c} and @file{@var{x}.tab.h}.  If the file
9421 @file{foo} depends on the files @file{parse.tab.o} and @file{scan.o}
9422 and the file @file{scan.o} depends on the file @file{parse.tab.h},
9423 when @file{parse.y} is changed, the recipe @samp{bison -d parse.y}
9424 will be executed only once, and the prerequisites of both
9425 @file{parse.tab.o} and @file{scan.o} will be satisfied.  (Presumably
9426 the file @file{parse.tab.o} will be recompiled from @file{parse.tab.c}
9427 and the file @file{scan.o} from @file{scan.c}, while @file{foo} is
9428 linked from @file{parse.tab.o}, @file{scan.o}, and its other
9429 prerequisites, and it will execute happily ever after.)@refill
9431 @node Automatic Variables, Pattern Match, Pattern Examples, Pattern Rules
9432 @subsection Automatic Variables
9433 @cindex automatic variables
9434 @cindex variables, automatic
9435 @cindex variables, and implicit rule
9437 Suppose you are writing a pattern rule to compile a @samp{.c} file into a
9438 @samp{.o} file: how do you write the @samp{cc} command so that it operates
9439 on the right source file name?  You cannot write the name in the recipe,
9440 because the name is different each time the implicit rule is applied.
9442 What you do is use a special feature of @code{make}, the @dfn{automatic
9443 variables}.  These variables have values computed afresh for each rule that
9444 is executed, based on the target and prerequisites of the rule.  In this
9445 example, you would use @samp{$@@} for the object file name and @samp{$<}
9446 for the source file name.
9448 @cindex automatic variables in prerequisites
9449 @cindex prerequisites, and automatic variables
9450 It's very important that you recognize the limited scope in which
9451 automatic variable values are available: they only have values within
9452 the recipe.  In particular, you cannot use them anywhere
9453 within the target list of a rule; they have no value there and will
9454 expand to the empty string.  Also, they cannot be accessed directly
9455 within the prerequisite list of a rule.  A common mistake is
9456 attempting to use @code{$@@} within the prerequisites list; this will
9457 not work.  However, there is a special feature of GNU @code{make},
9458 secondary expansion (@pxref{Secondary Expansion}), which will allow
9459 automatic variable values to be used in prerequisite lists.
9461 Here is a table of automatic variables:
9463 @table @code
9464 @vindex $@@
9465 @vindex @@ @r{(automatic variable)}
9466 @item $@@
9467 The file name of the target of the rule.  If the target is an archive
9468 member, then @samp{$@@} is the name of the archive file.  In a pattern
9469 rule that has multiple targets (@pxref{Pattern Intro, ,Introduction to
9470 Pattern Rules}), @samp{$@@} is the name of whichever target caused the
9471 rule's recipe to be run.
9473 @vindex $%
9474 @vindex % @r{(automatic variable)}
9475 @item $%
9476 The target member name, when the target is an archive member.
9477 @xref{Archives}.  For example, if the target is @file{foo.a(bar.o)} then
9478 @samp{$%} is @file{bar.o} and @samp{$@@} is @file{foo.a}.  @samp{$%} is
9479 empty when the target is not an archive member.
9481 @vindex $<
9482 @vindex < @r{(automatic variable)}
9483 @item $<
9484 The name of the first prerequisite.  If the target got its recipe from
9485 an implicit rule, this will be the first prerequisite added by the
9486 implicit rule (@pxref{Implicit Rules}).
9488 @vindex $?
9489 @vindex ? @r{(automatic variable)}
9490 @item $?
9491 The names of all the prerequisites that are newer than the target, with
9492 spaces between them.  For prerequisites which are archive members, only
9493 the named member is used (@pxref{Archives}).
9494 @cindex prerequisites, list of changed
9495 @cindex list of changed prerequisites
9497 @vindex $^
9498 @vindex ^ @r{(automatic variable)}
9499 @item $^
9500 The names of all the prerequisites, with spaces between them.  For
9501 prerequisites which are archive members, only the named member is used
9502 (@pxref{Archives}).  A target has only one prerequisite on each other file
9503 it depends on, no matter how many times each file is listed as a
9504 prerequisite.  So if you list a prerequisite more than once for a target,
9505 the value of @code{$^} contains just one copy of the name.  This list
9506 does @strong{not} contain any of the order-only prerequisites; for those
9507 see the @samp{$|} variable, below.
9508 @cindex prerequisites, list of all
9509 @cindex list of all prerequisites
9511 @vindex $+
9512 @vindex + @r{(automatic variable)}
9513 @item $+
9514 This is like @samp{$^}, but prerequisites listed more than once are
9515 duplicated in the order they were listed in the makefile.  This is
9516 primarily useful for use in linking commands where it is meaningful to
9517 repeat library file names in a particular order.
9519 @vindex $|
9520 @vindex | @r{(automatic variable)}
9521 @item $|
9522 The names of all the order-only prerequisites, with spaces between
9523 them.
9525 @vindex $*
9526 @vindex * @r{(automatic variable)}
9527 @item $*
9528 The stem with which an implicit rule matches (@pxref{Pattern Match, ,How
9529 Patterns Match}).  If the target is @file{dir/a.foo.b} and the target
9530 pattern is @file{a.%.b} then the stem is @file{dir/foo}.  The stem is
9531 useful for constructing names of related files.@refill
9532 @cindex stem, variable for
9534 In a static pattern rule, the stem is part of the file name that matched
9535 the @samp{%} in the target pattern.
9537 In an explicit rule, there is no stem; so @samp{$*} cannot be determined
9538 in that way.  Instead, if the target name ends with a recognized suffix
9539 (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}), @samp{$*} is set to
9540 the target name minus the suffix.  For example, if the target name is
9541 @samp{foo.c}, then @samp{$*} is set to @samp{foo}, since @samp{.c} is a
9542 suffix.  GNU @code{make} does this bizarre thing only for compatibility
9543 with other implementations of @code{make}.  You should generally avoid
9544 using @samp{$*} except in implicit rules or static pattern rules.@refill
9546 If the target name in an explicit rule does not end with a recognized
9547 suffix, @samp{$*} is set to the empty string for that rule.
9548 @end table
9550 @samp{$?} is useful even in explicit rules when you wish to operate on only
9551 the prerequisites that have changed.  For example, suppose that an archive
9552 named @file{lib} is supposed to contain copies of several object files.
9553 This rule copies just the changed object files into the archive:
9555 @example
9556 @group
9557 lib: foo.o bar.o lose.o win.o
9558         ar r lib $?
9559 @end group
9560 @end example
9562 Of the variables listed above, four have values that are single file
9563 names, and three have values that are lists of file names.  These seven
9564 have variants that get just the file's directory name or just the file
9565 name within the directory.  The variant variables' names are formed by
9566 appending @samp{D} or @samp{F}, respectively.  These variants are
9567 semi-obsolete in GNU @code{make} since the functions @code{dir} and
9568 @code{notdir} can be used to get a similar effect (@pxref{File Name
9569 Functions, , Functions for File Names}).  Note, however, that the
9570 @samp{D} variants all omit the trailing slash which always appears in
9571 the output of the @code{dir} function.  Here is a table of the variants:
9573 @table @samp
9574 @vindex $(@@D)
9575 @vindex @@D @r{(automatic variable)}
9576 @item $(@@D)
9577 The directory part of the file name of the target, with the trailing
9578 slash removed.  If the value of @samp{$@@} is @file{dir/foo.o} then
9579 @samp{$(@@D)} is @file{dir}.  This value is @file{.} if @samp{$@@} does
9580 not contain a slash.
9582 @vindex $(@@F)
9583 @vindex @@F @r{(automatic variable)}
9584 @item $(@@F)
9585 The file-within-directory part of the file name of the target.  If the
9586 value of @samp{$@@} is @file{dir/foo.o} then @samp{$(@@F)} is
9587 @file{foo.o}.  @samp{$(@@F)} is equivalent to @samp{$(notdir $@@)}.
9589 @vindex $(*D)
9590 @vindex *D @r{(automatic variable)}
9591 @item $(*D)
9592 @vindex $(*F)
9593 @vindex *F @r{(automatic variable)}
9594 @itemx $(*F)
9595 The directory part and the file-within-directory
9596 part of the stem; @file{dir} and @file{foo} in this example.
9598 @vindex $(%D)
9599 @vindex %D @r{(automatic variable)}
9600 @item $(%D)
9601 @vindex $(%F)
9602 @vindex %F @r{(automatic variable)}
9603 @itemx $(%F)
9604 The directory part and the file-within-directory part of the target
9605 archive member name.  This makes sense only for archive member targets
9606 of the form @file{@var{archive}(@var{member})} and is useful only when
9607 @var{member} may contain a directory name.  (@xref{Archive Members,
9608 ,Archive Members as Targets}.)
9610 @vindex $(<D)
9611 @vindex <D @r{(automatic variable)}
9612 @item $(<D)
9613 @vindex $(<F)
9614 @vindex <F @r{(automatic variable)}
9615 @itemx $(<F)
9616 The directory part and the file-within-directory
9617 part of the first prerequisite.
9619 @vindex $(^D)
9620 @vindex ^D @r{(automatic variable)}
9621 @item $(^D)
9622 @vindex $(^F)
9623 @vindex ^F @r{(automatic variable)}
9624 @itemx $(^F)
9625 Lists of the directory parts and the file-within-directory
9626 parts of all prerequisites.
9628 @vindex $(+D)
9629 @vindex +D @r{(automatic variable)}
9630 @item $(+D)
9631 @vindex $(+F)
9632 @vindex +F @r{(automatic variable)}
9633 @itemx $(+F)
9634 Lists of the directory parts and the file-within-directory
9635 parts of all prerequisites, including multiple instances of duplicated
9636 prerequisites.
9638 @vindex $(?D)
9639 @vindex ?D @r{(automatic variable)}
9640 @item $(?D)
9641 @vindex $(?F)
9642 @vindex ?F @r{(automatic variable)}
9643 @itemx $(?F)
9644 Lists of the directory parts and the file-within-directory parts of
9645 all prerequisites that are newer than the target.
9646 @end table
9648 Note that we use a special stylistic convention when we talk about these
9649 automatic variables; we write ``the value of @samp{$<}'', rather than
9650 @w{``the variable @code{<}''} as we would write for ordinary variables
9651 such as @code{objects} and @code{CFLAGS}.  We think this convention
9652 looks more natural in this special case.  Please do not assume it has a
9653 deep significance; @samp{$<} refers to the variable named @code{<} just
9654 as @samp{$(CFLAGS)} refers to the variable named @code{CFLAGS}.
9655 You could just as well use @samp{$(<)} in place of @samp{$<}.
9657 @node Pattern Match, Match-Anything Rules, Automatic Variables, Pattern Rules
9658 @subsection How Patterns Match
9660 @cindex stem
9661 A target pattern is composed of a @samp{%} between a prefix and a suffix,
9662 either or both of which may be empty.  The pattern matches a file name only
9663 if the file name starts with the prefix and ends with the suffix, without
9664 overlap.  The text between the prefix and the suffix is called the
9665 @dfn{stem}.  Thus, when the pattern @samp{%.o} matches the file name
9666 @file{test.o}, the stem is @samp{test}.  The pattern rule prerequisites are
9667 turned into actual file names by substituting the stem for the character
9668 @samp{%}.  Thus, if in the same example one of the prerequisites is written
9669 as @samp{%.c}, it expands to @samp{test.c}.@refill
9671 When the target pattern does not contain a slash (and it usually does
9672 not), directory names in the file names are removed from the file name
9673 before it is compared with the target prefix and suffix.  After the
9674 comparison of the file name to the target pattern, the directory
9675 names, along with the slash that ends them, are added on to the
9676 prerequisite file names generated from the pattern rule's prerequisite
9677 patterns and the file name.  The directories are ignored only for the
9678 purpose of finding an implicit rule to use, not in the application of
9679 that rule.  Thus, @samp{e%t} matches the file name @file{src/eat},
9680 with @samp{src/a} as the stem.  When prerequisites are turned into file
9681 names, the directories from the stem are added at the front, while the
9682 rest of the stem is substituted for the @samp{%}.  The stem
9683 @samp{src/a} with a prerequisite pattern @samp{c%r} gives the file name
9684 @file{src/car}.@refill
9686 @cindex pattern rules, order of
9687 @cindex order of pattern rules
9688 A pattern rule can be used to build a given file only if there is a
9689 target pattern that matches the file name, @emph{and} all
9690 prerequisites in that rule either exist or can be built.  The rules
9691 you write take precedence over those that are built in. Note however,
9692 that a rule whose prerequisites actually exist or are mentioned always
9693 takes priority over a rule with prerequisites that must be made by
9694 chaining other implicit rules.
9696 @cindex stem, shortest
9697 It is possible that more than one pattern rule will meet these
9698 criteria.  In that case, @code{make} will choose the rule with the
9699 shortest stem (that is, the pattern that matches most specifically).
9700 If more than one pattern rule has the shortest stem, @code{make} will
9701 choose the first one found in the makefile.
9703 This algorithm results in more specific rules being preferred over
9704 more generic ones; for example:
9706 @example
9707 %.o: %.c
9708         $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@@
9710 %.o : %.f
9711         $(COMPILE.F) $(OUTPUT_OPTION) $<
9713 lib/%.o: lib/%.c
9714         $(CC) -fPIC -c $(CFLAGS) $(CPPFLAGS) $< -o $@@
9715 @end example
9717 Given these rules and asked to build @file{bar.o} where both
9718 @file{bar.c} and @file{bar.f} exist, @code{make} will choose the first
9719 rule and compile @file{bar.c} into @file{bar.o}.  In the same
9720 situation where @file{bar.c} does not exist, then @code{make} will
9721 choose the second rule and compile @file{bar.f} into @file{bar.o}.
9723 If @code{make} is asked to build @file{lib/bar.o} and both
9724 @file{lib/bar.c} and @file{lib/bar.f} exist, then the third rule will
9725 be chosen since the stem for this rule (@samp{bar}) is shorter than
9726 the stem for the first rule (@samp{lib/bar}).  If @file{lib/bar.c}
9727 does not exist then the third rule is not eligible and the second rule
9728 will be used, even though the stem is longer.
9730 @node Match-Anything Rules, Canceling Rules, Pattern Match, Pattern Rules
9731 @subsection Match-Anything Pattern Rules
9733 @cindex match-anything rule
9734 @cindex terminal rule
9735 When a pattern rule's target is just @samp{%}, it matches any file name
9736 whatever.  We call these rules @dfn{match-anything} rules.  They are very
9737 useful, but it can take a lot of time for @code{make} to think about them,
9738 because it must consider every such rule for each file name listed either
9739 as a target or as a prerequisite.
9741 Suppose the makefile mentions @file{foo.c}.  For this target, @code{make}
9742 would have to consider making it by linking an object file @file{foo.c.o},
9743 or by C compilation-and-linking in one step from @file{foo.c.c}, or by
9744 Pascal compilation-and-linking from @file{foo.c.p}, and many other
9745 possibilities.
9747 We know these possibilities are ridiculous since @file{foo.c} is a C source
9748 file, not an executable.  If @code{make} did consider these possibilities,
9749 it would ultimately reject them, because files such as @file{foo.c.o} and
9750 @file{foo.c.p} would not exist.  But these possibilities are so
9751 numerous that @code{make} would run very slowly if it had to consider
9752 them.@refill
9754 To gain speed, we have put various constraints on the way @code{make}
9755 considers match-anything rules.  There are two different constraints that
9756 can be applied, and each time you define a match-anything rule you must
9757 choose one or the other for that rule.
9759 One choice is to mark the match-anything rule as @dfn{terminal} by defining
9760 it with a double colon.  When a rule is terminal, it does not apply unless
9761 its prerequisites actually exist.  Prerequisites that could be made with
9762 other implicit rules are not good enough.  In other words, no further
9763 chaining is allowed beyond a terminal rule.
9765 For example, the built-in implicit rules for extracting sources from RCS
9766 and SCCS files are terminal; as a result, if the file @file{foo.c,v} does
9767 not exist, @code{make} will not even consider trying to make it as an
9768 intermediate file from @file{foo.c,v.o} or from @file{RCS/SCCS/s.foo.c,v}.
9769 RCS and SCCS files are generally ultimate source files, which should not be
9770 remade from any other files; therefore, @code{make} can save time by not
9771 looking for ways to remake them.@refill
9773 If you do not mark the match-anything rule as terminal, then it is
9774 nonterminal.  A nonterminal match-anything rule cannot apply to a file name
9775 that indicates a specific type of data.  A file name indicates a specific
9776 type of data if some non-match-anything implicit rule target matches it.
9778 For example, the file name @file{foo.c} matches the target for the pattern
9779 rule @samp{%.c : %.y} (the rule to run Yacc).  Regardless of whether this
9780 rule is actually applicable (which happens only if there is a file
9781 @file{foo.y}), the fact that its target matches is enough to prevent
9782 consideration of any nonterminal match-anything rules for the file
9783 @file{foo.c}.  Thus, @code{make} will not even consider trying to make
9784 @file{foo.c} as an executable file from @file{foo.c.o}, @file{foo.c.c},
9785 @file{foo.c.p}, etc.@refill
9787 The motivation for this constraint is that nonterminal match-anything
9788 rules are used for making files containing specific types of data (such as
9789 executable files) and a file name with a recognized suffix indicates some
9790 other specific type of data (such as a C source file).
9792 Special built-in dummy pattern rules are provided solely to recognize
9793 certain file names so that nonterminal match-anything rules will not be
9794 considered.  These dummy rules have no prerequisites and no recipes, and
9795 they are ignored for all other purposes.  For example, the built-in
9796 implicit rule
9798 @example
9799 %.p :
9800 @end example
9802 @noindent
9803 exists to make sure that Pascal source files such as @file{foo.p} match a
9804 specific target pattern and thereby prevent time from being wasted looking
9805 for @file{foo.p.o} or @file{foo.p.c}.
9807 Dummy pattern rules such as the one for @samp{%.p} are made for every
9808 suffix listed as valid for use in suffix rules (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}).
9810 @node Canceling Rules,  , Match-Anything Rules, Pattern Rules
9811 @subsection Canceling Implicit Rules
9813 You can override a built-in implicit rule (or one you have defined
9814 yourself) by defining a new pattern rule with the same target and
9815 prerequisites, but a different recipe.  When the new rule is defined, the
9816 built-in one is replaced.  The new rule's position in the sequence of
9817 implicit rules is determined by where you write the new rule.
9819 You can cancel a built-in implicit rule by defining a pattern rule with the
9820 same target and prerequisites, but no recipe.  For example, the following
9821 would cancel the rule that runs the assembler:
9823 @example
9824 %.o : %.s
9825 @end example
9827 @node Last Resort, Suffix Rules, Pattern Rules, Implicit Rules
9828 @section Defining Last-Resort Default Rules
9829 @cindex last-resort default rules
9830 @cindex default rules, last-resort
9832 You can define a last-resort implicit rule by writing a terminal
9833 match-anything pattern rule with no prerequisites (@pxref{Match-Anything
9834 Rules}).  This is just like any other pattern rule; the only thing
9835 special about it is that it will match any target.  So such a rule's
9836 recipe is used for all targets and prerequisites that have no recipe
9837 of their own and for which no other implicit rule applies.
9839 For example, when testing a makefile, you might not care if the source
9840 files contain real data, only that they exist.  Then you might do this:
9842 @example
9844         touch $@@
9845 @end example
9847 @noindent
9848 to cause all the source files needed (as prerequisites) to be created
9849 automatically.
9851 @findex .DEFAULT
9852 You can instead define a recipe to be used for targets for which there
9853 are no rules at all, even ones which don't specify recipes.  You do
9854 this by writing a rule for the target @code{.DEFAULT}.  Such a rule's
9855 recipe is used for all prerequisites which do not appear as targets in
9856 any explicit rule, and for which no implicit rule applies.  Naturally,
9857 there is no @code{.DEFAULT} rule unless you write one.
9859 If you use @code{.DEFAULT} with no recipe or prerequisites:
9861 @example
9862 .DEFAULT:
9863 @end example
9865 @noindent
9866 the recipe previously stored for @code{.DEFAULT} is cleared.  Then
9867 @code{make} acts as if you had never defined @code{.DEFAULT} at all.
9869 If you do not want a target to get the recipe from a match-anything
9870 pattern rule or @code{.DEFAULT}, but you also do not want any recipe
9871 to be run for the target, you can give it an empty recipe
9872 (@pxref{Empty Recipes, ,Defining Empty Recipes}).@refill
9874 You can use a last-resort rule to override part of another makefile.
9875 @xref{Overriding Makefiles, , Overriding Part of Another Makefile}.
9877 @node Suffix Rules, Implicit Rule Search, Last Resort, Implicit Rules
9878 @section Old-Fashioned Suffix Rules
9879 @cindex old-fashioned suffix rules
9880 @cindex suffix rule
9882 @dfn{Suffix rules} are the old-fashioned way of defining implicit rules for
9883 @code{make}.  Suffix rules are obsolete because pattern rules are more
9884 general and clearer.  They are supported in GNU @code{make} for
9885 compatibility with old makefiles.  They come in two kinds:
9886 @dfn{double-suffix} and @dfn{single-suffix}.@refill
9888 A double-suffix rule is defined by a pair of suffixes: the target suffix
9889 and the source suffix.  It matches any file whose name ends with the
9890 target suffix.  The corresponding implicit prerequisite is made by
9891 replacing the target suffix with the source suffix in the file name.  A
9892 two-suffix rule whose target and source suffixes are @samp{.o} and
9893 @samp{.c} is equivalent to the pattern rule @samp{%.o : %.c}.
9895 A single-suffix rule is defined by a single suffix, which is the source
9896 suffix.  It matches any file name, and the corresponding implicit
9897 prerequisite name is made by appending the source suffix.  A single-suffix
9898 rule whose source suffix is @samp{.c} is equivalent to the pattern rule
9899 @samp{% : %.c}.
9901 Suffix rule definitions are recognized by comparing each rule's target
9902 against a defined list of known suffixes.  When @code{make} sees a rule
9903 whose target is a known suffix, this rule is considered a single-suffix
9904 rule.  When @code{make} sees a rule whose target is two known suffixes
9905 concatenated, this rule is taken as a double-suffix rule.
9907 For example, @samp{.c} and @samp{.o} are both on the default list of
9908 known suffixes.  Therefore, if you define a rule whose target is
9909 @samp{.c.o}, @code{make} takes it to be a double-suffix rule with source
9910 suffix @samp{.c} and target suffix @samp{.o}.  Here is the old-fashioned
9911 way to define the rule for compiling a C source file:@refill
9913 @example
9914 .c.o:
9915         $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
9916 @end example
9918 Suffix rules cannot have any prerequisites of their own.  If they have any,
9919 they are treated as normal files with funny names, not as suffix rules.
9920 Thus, the rule:
9922 @example
9923 .c.o: foo.h
9924         $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
9925 @end example
9927 @noindent
9928 tells how to make the file @file{.c.o} from the prerequisite file
9929 @file{foo.h}, and is not at all like the pattern rule:
9931 @example
9932 %.o: %.c foo.h
9933         $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
9934 @end example
9936 @noindent
9937 which tells how to make @samp{.o} files from @samp{.c} files, and makes all
9938 @samp{.o} files using this pattern rule also depend on @file{foo.h}.
9940 Suffix rules with no recipe are also meaningless.  They do not remove
9941 previous rules as do pattern rules with no recipe (@pxref{Canceling
9942 Rules, , Canceling Implicit Rules}).  They simply enter the suffix or
9943 pair of suffixes concatenated as a target in the data base.@refill
9945 @findex .SUFFIXES
9946 The known suffixes are simply the names of the prerequisites of the special
9947 target @code{.SUFFIXES}.  You can add your own suffixes by writing a rule
9948 for @code{.SUFFIXES} that adds more prerequisites, as in:
9950 @example
9951 .SUFFIXES: .hack .win
9952 @end example
9954 @noindent
9955 which adds @samp{.hack} and @samp{.win} to the end of the list of suffixes.
9957 If you wish to eliminate the default known suffixes instead of just adding
9958 to them, write a rule for @code{.SUFFIXES} with no prerequisites.  By
9959 special dispensation, this eliminates all existing prerequisites of
9960 @code{.SUFFIXES}.  You can then write another rule to add the suffixes you
9961 want.  For example,
9963 @example
9964 @group
9965 .SUFFIXES:            # @r{Delete the default suffixes}
9966 .SUFFIXES: .c .o .h   # @r{Define our suffix list}
9967 @end group
9968 @end example
9970 The @samp{-r} or @samp{--no-builtin-rules} flag causes the default
9971 list of suffixes to be empty.
9973 @vindex SUFFIXES
9974 The variable @code{SUFFIXES} is defined to the default list of suffixes
9975 before @code{make} reads any makefiles.  You can change the list of suffixes
9976 with a rule for the special target @code{.SUFFIXES}, but that does not alter
9977 this variable.
9979 @node Implicit Rule Search,  , Suffix Rules, Implicit Rules
9980 @section Implicit Rule Search Algorithm
9981 @cindex implicit rule, search algorithm
9982 @cindex search algorithm, implicit rule
9984 Here is the procedure @code{make} uses for searching for an implicit rule
9985 for a target @var{t}.  This procedure is followed for each double-colon
9986 rule with no recipe, for each target of ordinary rules none of which have
9987 a recipe, and for each prerequisite that is not the target of any rule.  It
9988 is also followed recursively for prerequisites that come from implicit
9989 rules, in the search for a chain of rules.
9991 Suffix rules are not mentioned in this algorithm because suffix rules are
9992 converted to equivalent pattern rules once the makefiles have been read in.
9994 For an archive member target of the form
9995 @samp{@var{archive}(@var{member})}, the following algorithm is run
9996 twice, first using the entire target name @var{t}, and second using
9997 @samp{(@var{member})} as the target @var{t} if the first run found no
9998 rule.@refill
10000 @enumerate
10001 @item
10002 Split @var{t} into a directory part, called @var{d}, and the rest,
10003 called @var{n}.  For example, if @var{t} is @samp{src/foo.o}, then
10004 @var{d} is @samp{src/} and @var{n} is @samp{foo.o}.@refill
10006 @item
10007 Make a list of all the pattern rules one of whose targets matches
10008 @var{t} or @var{n}.  If the target pattern contains a slash, it is
10009 matched against @var{t}; otherwise, against @var{n}.
10011 @item
10012 If any rule in that list is @emph{not} a match-anything rule, then
10013 remove all nonterminal match-anything rules from the list.
10015 @item
10016 Remove from the list all rules with no recipe.
10018 @item
10019 For each pattern rule in the list:
10021 @enumerate a
10022 @item
10023 Find the stem @var{s}, which is the nonempty part of @var{t} or @var{n}
10024 matched by the @samp{%} in the target pattern.@refill
10026 @item
10027 Compute the prerequisite names by substituting @var{s} for @samp{%}; if
10028 the target pattern does not contain a slash, append @var{d} to
10029 the front of each prerequisite name.@refill
10031 @item
10032 Test whether all the prerequisites exist or ought to exist.  (If a
10033 file name is mentioned in the makefile as a target or as an explicit
10034 prerequisite, then we say it ought to exist.)
10036 If all prerequisites exist or ought to exist, or there are no prerequisites,
10037 then this rule applies.
10038 @end enumerate
10040 @item
10041 If no pattern rule has been found so far, try harder.
10042 For each pattern rule in the list:
10044 @enumerate a
10045 @item
10046 If the rule is terminal, ignore it and go on to the next rule.
10048 @item
10049 Compute the prerequisite names as before.
10051 @item
10052 Test whether all the prerequisites exist or ought to exist.
10054 @item
10055 For each prerequisite that does not exist, follow this algorithm
10056 recursively to see if the prerequisite can be made by an implicit
10057 rule.
10059 @item
10060 If all prerequisites exist, ought to exist, or can be
10061 made by implicit rules, then this rule applies.
10062 @end enumerate
10064 @item
10065 If no implicit rule applies, the rule for @code{.DEFAULT}, if any,
10066 applies.  In that case, give @var{t} the same recipe that
10067 @code{.DEFAULT} has.  Otherwise, there is no recipe for @var{t}.
10068 @end enumerate
10070 Once a rule that applies has been found, for each target pattern of
10071 the rule other than the one that matched @var{t} or @var{n}, the
10072 @samp{%} in the pattern is replaced with @var{s} and the resultant
10073 file name is stored until the recipe to remake the target file @var{t}
10074 is executed.  After the recipe is executed, each of these stored file
10075 names are entered into the data base and marked as having been updated
10076 and having the same update status as the file @var{t}.
10078 When the recipe of a pattern rule is executed for @var{t}, the
10079 automatic variables are set corresponding to the target and
10080 prerequisites.  @xref{Automatic Variables}.
10082 @node Archives, Features, Implicit Rules, Top
10083 @chapter Using @code{make} to Update Archive Files
10084 @cindex archive
10086 @dfn{Archive files} are files containing named subfiles called
10087 @dfn{members}; they are maintained with the program @code{ar} and their
10088 main use is as subroutine libraries for linking.
10090 @menu
10091 * Archive Members::             Archive members as targets.
10092 * Archive Update::              The implicit rule for archive member targets.
10093 * Archive Pitfalls::            Dangers to watch out for when using archives.
10094 * Archive Suffix Rules::        You can write a special kind of suffix rule
10095                                   for updating archives.
10096 @end menu
10098 @node Archive Members, Archive Update, Archives, Archives
10099 @section Archive Members as Targets
10100 @cindex archive member targets
10102 An individual member of an archive file can be used as a target or
10103 prerequisite in @code{make}.  You specify the member named @var{member} in
10104 archive file @var{archive} as follows:
10106 @example
10107 @var{archive}(@var{member})
10108 @end example
10110 @noindent
10111 This construct is available only in targets and prerequisites, not in
10112 recipes!  Most programs that you might use in recipes do not support
10113 this syntax and cannot act directly on archive members.  Only
10114 @code{ar} and other programs specifically designed to operate on
10115 archives can do so.  Therefore, valid recipes to update an archive
10116 member target probably must use @code{ar}.  For example, this rule
10117 says to create a member @file{hack.o} in archive @file{foolib} by
10118 copying the file @file{hack.o}:
10120 @example
10121 foolib(hack.o) : hack.o
10122         ar cr foolib hack.o
10123 @end example
10125 In fact, nearly all archive member targets are updated in just this way
10126 and there is an implicit rule to do it for you.  @strong{Please note:} The
10127 @samp{c} flag to @code{ar} is required if the archive file does not
10128 already exist.
10130 To specify several members in the same archive, you can write all the
10131 member names together between the parentheses.  For example:
10133 @example
10134 foolib(hack.o kludge.o)
10135 @end example
10137 @noindent
10138 is equivalent to:
10140 @example
10141 foolib(hack.o) foolib(kludge.o)
10142 @end example
10144 @cindex wildcard, in archive member
10145 You can also use shell-style wildcards in an archive member reference.
10146 @xref{Wildcards, ,Using Wildcard Characters in File Names}.  For
10147 example, @w{@samp{foolib(*.o)}} expands to all existing members of the
10148 @file{foolib} archive whose names end in @samp{.o}; perhaps
10149 @samp{@w{foolib(hack.o)} @w{foolib(kludge.o)}}.
10151 @node Archive Update, Archive Pitfalls, Archive Members, Archives
10152 @section Implicit Rule for Archive Member Targets
10154 Recall that a target that looks like @file{@var{a}(@var{m})} stands for the
10155 member named @var{m} in the archive file @var{a}.
10157 When @code{make} looks for an implicit rule for such a target, as a special
10158 feature it considers implicit rules that match @file{(@var{m})}, as well as
10159 those that match the actual target @file{@var{a}(@var{m})}.
10161 This causes one special rule whose target is @file{(%)} to match.  This
10162 rule updates the target @file{@var{a}(@var{m})} by copying the file @var{m}
10163 into the archive.  For example, it will update the archive member target
10164 @file{foo.a(bar.o)} by copying the @emph{file} @file{bar.o} into the
10165 archive @file{foo.a} as a @emph{member} named @file{bar.o}.
10167 When this rule is chained with others, the result is very powerful.
10168 Thus, @samp{make "foo.a(bar.o)"} (the quotes are needed to protect the
10169 @samp{(} and @samp{)} from being interpreted specially by the shell) in
10170 the presence of a file @file{bar.c} is enough to cause the following
10171 recipe to be run, even without a makefile:
10173 @example
10174 cc -c bar.c -o bar.o
10175 ar r foo.a bar.o
10176 rm -f bar.o
10177 @end example
10179 @noindent
10180 Here @code{make} has envisioned the file @file{bar.o} as an intermediate
10181 file.  @xref{Chained Rules, ,Chains of Implicit Rules}.
10183 Implicit rules such as this one are written using the automatic variable
10184 @samp{$%}.  @xref{Automatic Variables}.
10186 An archive member name in an archive cannot contain a directory name, but
10187 it may be useful in a makefile to pretend that it does.  If you write an
10188 archive member target @file{foo.a(dir/file.o)}, @code{make} will perform
10189 automatic updating with this recipe:
10191 @example
10192 ar r foo.a dir/file.o
10193 @end example
10195 @noindent
10196 which has the effect of copying the file @file{dir/file.o} into a member
10197 named @file{file.o}.  In connection with such usage, the automatic variables
10198 @code{%D} and @code{%F} may be useful.
10200 @menu
10201 * Archive Symbols::             How to update archive symbol directories.
10202 @end menu
10204 @node Archive Symbols,  , Archive Update, Archive Update
10205 @subsection Updating Archive Symbol Directories
10206 @cindex @code{__.SYMDEF}
10207 @cindex updating archive symbol directories
10208 @cindex archive symbol directory updating
10209 @cindex symbol directories, updating archive
10210 @cindex directories, updating archive symbol
10212 An archive file that is used as a library usually contains a special member
10213 named @file{__.SYMDEF} that contains a directory of the external symbol
10214 names defined by all the other members.  After you update any other
10215 members, you need to update @file{__.SYMDEF} so that it will summarize the
10216 other members properly.  This is done by running the @code{ranlib} program:
10218 @example
10219 ranlib @var{archivefile}
10220 @end example
10222 Normally you would put this command in the rule for the archive file,
10223 and make all the members of the archive file prerequisites of that rule.
10224 For example,
10226 @example
10227 libfoo.a: libfoo.a(x.o) libfoo.a(y.o) @dots{}
10228         ranlib libfoo.a
10229 @end example
10231 @noindent
10232 The effect of this is to update archive members @file{x.o}, @file{y.o},
10233 etc., and then update the symbol directory member @file{__.SYMDEF} by
10234 running @code{ranlib}.  The rules for updating the members are not shown
10235 here; most likely you can omit them and use the implicit rule which copies
10236 files into the archive, as described in the preceding section.
10238 This is not necessary when using the GNU @code{ar} program, which
10239 updates the @file{__.SYMDEF} member automatically.
10241 @node Archive Pitfalls, Archive Suffix Rules, Archive Update, Archives
10242 @section Dangers When Using Archives
10243 @cindex archive, and parallel execution
10244 @cindex parallel execution, and archive update
10245 @cindex archive, and @code{-j}
10246 @cindex @code{-j}, and archive update
10248 It is important to be careful when using parallel execution (the
10249 @code{-j} switch; @pxref{Parallel, ,Parallel Execution}) and archives.
10250 If multiple @code{ar} commands run at the same time on the same archive
10251 file, they will not know about each other and can corrupt the file.
10253 Possibly a future version of @code{make} will provide a mechanism to
10254 circumvent this problem by serializing all recipes that operate on the
10255 same archive file.  But for the time being, you must either write your
10256 makefiles to avoid this problem in some other way, or not use @code{-j}.
10258 @node Archive Suffix Rules,  , Archive Pitfalls, Archives
10259 @section Suffix Rules for Archive Files
10260 @cindex suffix rule, for archive
10261 @cindex archive, suffix rule for
10262 @cindex library archive, suffix rule for
10263 @cindex @code{.a} (archives)
10265 You can write a special kind of suffix rule for dealing with archive
10266 files.  @xref{Suffix Rules}, for a full explanation of suffix rules.
10267 Archive suffix rules are obsolete in GNU @code{make}, because pattern
10268 rules for archives are a more general mechanism (@pxref{Archive
10269 Update}).  But they are retained for compatibility with other
10270 @code{make}s.
10272 To write a suffix rule for archives, you simply write a suffix rule
10273 using the target suffix @samp{.a} (the usual suffix for archive files).
10274 For example, here is the old-fashioned suffix rule to update a library
10275 archive from C source files:
10277 @example
10278 @group
10279 .c.a:
10280         $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o
10281         $(AR) r $@@ $*.o
10282         $(RM) $*.o
10283 @end group
10284 @end example
10286 @noindent
10287 This works just as if you had written the pattern rule:
10289 @example
10290 @group
10291 (%.o): %.c
10292         $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o
10293         $(AR) r $@@ $*.o
10294         $(RM) $*.o
10295 @end group
10296 @end example
10298 In fact, this is just what @code{make} does when it sees a suffix rule
10299 with @samp{.a} as the target suffix.  Any double-suffix rule
10300 @w{@samp{.@var{x}.a}} is converted to a pattern rule with the target
10301 pattern @samp{(%.o)} and a prerequisite pattern of @samp{%.@var{x}}.
10303 Since you might want to use @samp{.a} as the suffix for some other kind
10304 of file, @code{make} also converts archive suffix rules to pattern rules
10305 in the normal way (@pxref{Suffix Rules}).  Thus a double-suffix rule
10306 @w{@samp{.@var{x}.a}} produces two pattern rules: @samp{@w{(%.o):}
10307 @w{%.@var{x}}} and @samp{@w{%.a}: @w{%.@var{x}}}.@refill
10309 @node Features, Missing, Archives, Top
10310 @chapter Features of GNU @code{make}
10311 @cindex features of GNU @code{make}
10312 @cindex portability
10313 @cindex compatibility
10315 Here is a summary of the features of GNU @code{make}, for comparison
10316 with and credit to other versions of @code{make}.  We consider the
10317 features of @code{make} in 4.2 BSD systems as a baseline.  If you are
10318 concerned with writing portable makefiles, you should not use the
10319 features of @code{make} listed here, nor the ones in @ref{Missing}.
10321 Many features come from the version of @code{make} in System V.
10323 @itemize @bullet
10324 @item
10325 The @code{VPATH} variable and its special meaning.
10326 @xref{Directory Search, , Searching Directories for Prerequisites}.
10327 This feature exists in System V @code{make}, but is undocumented.
10328 It is documented in 4.3 BSD @code{make} (which says it mimics System V's
10329 @code{VPATH} feature).@refill
10331 @item
10332 Included makefiles.  @xref{Include, ,Including Other Makefiles}.
10333 Allowing multiple files to be included with a single directive is a GNU
10334 extension.
10336 @item
10337 Variables are read from and communicated via the environment.
10338 @xref{Environment, ,Variables from the Environment}.
10340 @item
10341 Options passed through the variable @code{MAKEFLAGS} to recursive
10342 invocations of @code{make}.
10343 @xref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
10345 @item
10346 The automatic variable @code{$%} is set to the member name
10347 in an archive reference.  @xref{Automatic Variables}.
10349 @item
10350 The automatic variables @code{$@@}, @code{$*}, @code{$<}, @code{$%},
10351 and @code{$?} have corresponding forms like @code{$(@@F)} and
10352 @code{$(@@D)}.  We have generalized this to @code{$^} as an obvious
10353 extension.  @xref{Automatic Variables}.@refill
10355 @item
10356 Substitution variable references.
10357 @xref{Reference, ,Basics of Variable References}.
10359 @item
10360 The command line options @samp{-b} and @samp{-m}, accepted and
10361 ignored.  In System V @code{make}, these options actually do something.
10363 @item
10364 Execution of recursive commands to run @code{make} via the variable
10365 @code{MAKE} even if @samp{-n}, @samp{-q} or @samp{-t} is specified.
10366 @xref{Recursion, ,Recursive Use of @code{make}}.
10368 @item
10369 Support for suffix @samp{.a} in suffix rules.  @xref{Archive Suffix
10370 Rules}.  This feature is obsolete in GNU @code{make}, because the
10371 general feature of rule chaining (@pxref{Chained Rules, ,Chains of
10372 Implicit Rules}) allows one pattern rule for installing members in an
10373 archive (@pxref{Archive Update}) to be sufficient.
10375 @item
10376 The arrangement of lines and backslash-newline combinations in
10377 recipes is retained when the recipes are printed, so they appear as
10378 they do in the makefile, except for the stripping of initial
10379 whitespace.
10380 @end itemize
10382 The following features were inspired by various other versions of
10383 @code{make}.  In some cases it is unclear exactly which versions inspired
10384 which others.
10386 @itemize @bullet
10387 @item
10388 Pattern rules using @samp{%}.
10389 This has been implemented in several versions of @code{make}.
10390 We're not sure who invented it first, but it's been spread around a bit.
10391 @xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.@refill
10393 @item
10394 Rule chaining and implicit intermediate files.
10395 This was implemented by Stu Feldman in his version of @code{make}
10396 for AT&T Eighth Edition Research Unix, and later by Andrew Hume of
10397 AT&T Bell Labs in his @code{mk} program (where he terms it
10398 ``transitive closure'').  We do not really know if
10399 we got this from either of them or thought it up ourselves at the
10400 same time.  @xref{Chained Rules, ,Chains of Implicit Rules}.
10402 @item
10403 The automatic variable @code{$^} containing a list of all prerequisites
10404 of the current target.  We did not invent this, but we have no idea who
10405 did.  @xref{Automatic Variables}.  The automatic variable
10406 @code{$+} is a simple extension of @code{$^}.
10408 @item
10409 The ``what if'' flag (@samp{-W} in GNU @code{make}) was (as far as we know)
10410 invented by Andrew Hume in @code{mk}.
10411 @xref{Instead of Execution, ,Instead of Executing Recipes}.
10413 @item
10414 The concept of doing several things at once (parallelism) exists in
10415 many incarnations of @code{make} and similar programs, though not in the
10416 System V or BSD implementations.  @xref{Execution, ,Recipe Execution}.
10418 @item
10419 Modified variable references using pattern substitution come from
10420 SunOS 4.  @xref{Reference, ,Basics of Variable References}.
10421 This functionality was provided in GNU @code{make} by the
10422 @code{patsubst} function before the alternate syntax was implemented
10423 for compatibility with SunOS 4.  It is not altogether clear who
10424 inspired whom, since GNU @code{make} had @code{patsubst} before SunOS
10425 4 was released.@refill
10427 @item
10428 The special significance of @samp{+} characters preceding recipe lines
10429 (@pxref{Instead of Execution, ,Instead of Executing Recipes}) is
10430 mandated by @cite{IEEE Standard 1003.2-1992} (POSIX.2).
10432 @item
10433 The @samp{+=} syntax to append to the value of a variable comes from SunOS
10434 4 @code{make}.  @xref{Appending, , Appending More Text to Variables}.
10436 @item
10437 The syntax @w{@samp{@var{archive}(@var{mem1} @var{mem2}@dots{})}} to list
10438 multiple members in a single archive file comes from SunOS 4 @code{make}.
10439 @xref{Archive Members}.
10441 @item
10442 The @code{-include} directive to include makefiles with no error for a
10443 nonexistent file comes from SunOS 4 @code{make}.  (But note that SunOS 4
10444 @code{make} does not allow multiple makefiles to be specified in one
10445 @code{-include} directive.)  The same feature appears with the name
10446 @code{sinclude} in SGI @code{make} and perhaps others.
10447 @end itemize
10449 The remaining features are inventions new in GNU @code{make}:
10451 @itemize @bullet
10452 @item
10453 Use the @samp{-v} or @samp{--version} option to print version and
10454 copyright information.
10456 @item
10457 Use the @samp{-h} or @samp{--help} option to summarize the options to
10458 @code{make}.
10460 @item
10461 Simply-expanded variables.  @xref{Flavors, ,The Two Flavors of Variables}.
10463 @item
10464 Pass command line variable assignments automatically through the
10465 variable @code{MAKE} to recursive @code{make} invocations.
10466 @xref{Recursion, ,Recursive Use of @code{make}}.
10468 @item
10469 Use the @samp{-C} or @samp{--directory} command option to change
10470 directory.  @xref{Options Summary, ,Summary of Options}.
10472 @item
10473 Make verbatim variable definitions with @code{define}.
10474 @xref{Multi-Line, ,Defining Multi-Line Variables}.
10476 @item
10477 Declare phony targets with the special target @code{.PHONY}.
10479 Andrew Hume of AT&T Bell Labs implemented a similar feature with a
10480 different syntax in his @code{mk} program.  This seems to be a case of
10481 parallel discovery.  @xref{Phony Targets, ,Phony Targets}.
10483 @item
10484 Manipulate text by calling functions.
10485 @xref{Functions, ,Functions for Transforming Text}.
10487 @item
10488 Use the @samp{-o} or @samp{--old-file}
10489 option to pretend a file's modification-time is old.
10490 @xref{Avoiding Compilation, ,Avoiding Recompilation of Some Files}.
10492 @item
10493 Conditional execution.
10495 This feature has been implemented numerous times in various versions
10496 of @code{make}; it seems a natural extension derived from the features
10497 of the C preprocessor and similar macro languages and is not a
10498 revolutionary concept.  @xref{Conditionals, ,Conditional Parts of Makefiles}.
10500 @item
10501 Specify a search path for included makefiles.
10502 @xref{Include, ,Including Other Makefiles}.
10504 @item
10505 Specify extra makefiles to read with an environment variable.
10506 @xref{MAKEFILES Variable, ,The Variable @code{MAKEFILES}}.
10508 @item
10509 Strip leading sequences of @samp{./} from file names, so that
10510 @file{./@var{file}} and @file{@var{file}} are considered to be the
10511 same file.@refill
10513 @item
10514 Use a special search method for library prerequisites written in the
10515 form @samp{-l@var{name}}.
10516 @xref{Libraries/Search, ,Directory Search for Link Libraries}.
10518 @item
10519 Allow suffixes for suffix rules
10520 (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}) to contain any
10521 characters.  In other versions of @code{make}, they must begin with
10522 @samp{.} and not contain any @samp{/} characters.
10524 @item
10525 Keep track of the current level of @code{make} recursion using the
10526 variable @code{MAKELEVEL}.  @xref{Recursion, ,Recursive Use of @code{make}}.
10528 @item
10529 Provide any goals given on the command line in the variable
10530 @code{MAKECMDGOALS}.  @xref{Goals, ,Arguments to Specify the Goals}.
10532 @item
10533 Specify static pattern rules.  @xref{Static Pattern, ,Static Pattern Rules}.
10535 @item
10536 Provide selective @code{vpath} search.
10537 @xref{Directory Search, ,Searching Directories for Prerequisites}.
10539 @item
10540 Provide computed variable references.
10541 @xref{Reference, ,Basics of Variable References}.
10543 @item
10544 Update makefiles.  @xref{Remaking Makefiles, ,How Makefiles Are Remade}.
10545 System V @code{make} has a very, very limited form of this
10546 functionality in that it will check out SCCS files for makefiles.
10548 @item
10549 Various new built-in implicit rules.
10550 @xref{Catalogue of Rules, ,Catalogue of Implicit Rules}.
10552 @item
10553 The built-in variable @samp{MAKE_VERSION} gives the version number of
10554 @code{make}.
10555 @vindex MAKE_VERSION
10556 @end itemize
10558 @node Missing, Makefile Conventions, Features, Top
10559 @chapter Incompatibilities and Missing Features
10560 @cindex incompatibilities
10561 @cindex missing features
10562 @cindex features, missing
10564 The @code{make} programs in various other systems support a few features
10565 that are not implemented in GNU @code{make}.  The POSIX.2 standard
10566 (@cite{IEEE Standard 1003.2-1992}) which specifies @code{make} does not
10567 require any of these features.@refill
10569 @itemize @bullet
10570 @item
10571 A target of the form @samp{@var{file}((@var{entry}))} stands for a member
10572 of archive file @var{file}.  The member is chosen, not by name, but by
10573 being an object file which defines the linker symbol @var{entry}.@refill
10575 This feature was not put into GNU @code{make} because of the
10576 nonmodularity of putting knowledge into @code{make} of the internal
10577 format of archive file symbol tables.
10578 @xref{Archive Symbols, ,Updating Archive Symbol Directories}.
10580 @item
10581 Suffixes (used in suffix rules) that end with the character @samp{~}
10582 have a special meaning to System V @code{make};
10583 they refer to the SCCS file that corresponds
10584 to the file one would get without the @samp{~}.  For example, the
10585 suffix rule @samp{.c~.o} would make the file @file{@var{n}.o} from
10586 the SCCS file @file{s.@var{n}.c}.  For complete coverage, a whole
10587 series of such suffix rules is required.
10588 @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
10590 In GNU @code{make}, this entire series of cases is handled by two
10591 pattern rules for extraction from SCCS, in combination with the
10592 general feature of rule chaining.
10593 @xref{Chained Rules, ,Chains of Implicit Rules}.
10595 @item
10596 In System V and 4.3 BSD @code{make}, files found by @code{VPATH}
10597 search (@pxref{Directory Search, ,Searching Directories for
10598 Prerequisites}) have their names changed inside recipes.  We feel it
10599 is much cleaner to always use automatic variables and thus make this
10600 feature obsolete.@refill
10602 @item
10603 In some Unix @code{make}s, the automatic variable @code{$*} appearing in
10604 the prerequisites of a rule has the amazingly strange ``feature'' of
10605 expanding to the full name of the @emph{target of that rule}.  We cannot
10606 imagine what went on in the minds of Unix @code{make} developers to do
10607 this; it is utterly inconsistent with the normal definition of @code{$*}.
10608 @vindex * @r{(automatic variable), unsupported bizarre usage}
10610 @item
10611 In some Unix @code{make}s, implicit rule search (@pxref{Implicit
10612 Rules, ,Using Implicit Rules}) is apparently done for @emph{all}
10613 targets, not just those without recipes.  This means you can
10614 do:@refill
10616 @example
10617 @group
10618 foo.o:
10619         cc -c foo.c
10620 @end group
10621 @end example
10623 @noindent
10624 and Unix @code{make} will intuit that @file{foo.o} depends on
10625 @file{foo.c}.@refill
10627 We feel that such usage is broken.  The prerequisite properties of
10628 @code{make} are well-defined (for GNU @code{make}, at least),
10629 and doing such a thing simply does not fit the model.@refill
10631 @item
10632 GNU @code{make} does not include any built-in implicit rules for
10633 compiling or preprocessing EFL programs.  If we hear of anyone who is
10634 using EFL, we will gladly add them.
10636 @item
10637 It appears that in SVR4 @code{make}, a suffix rule can be specified
10638 with no recipe, and it is treated as if it had an empty recipe
10639 (@pxref{Empty Recipes}).  For example:
10641 @example
10642 .c.a:
10643 @end example
10645 @noindent
10646 will override the built-in @file{.c.a} suffix rule.
10648 We feel that it is cleaner for a rule without a recipe to always simply
10649 add to the prerequisite list for the target.  The above example can be
10650 easily rewritten to get the desired behavior in GNU @code{make}:
10652 @example
10653 .c.a: ;
10654 @end example
10656 @item
10657 Some versions of @code{make} invoke the shell with the @samp{-e} flag,
10658 except under @samp{-k} (@pxref{Testing, ,Testing the Compilation of a
10659 Program}).  The @samp{-e} flag tells the shell to exit as soon as any
10660 program it runs returns a nonzero status.  We feel it is cleaner to
10661 write each line of the recipe to stand on its own and not require this
10662 special treatment.
10663 @end itemize
10665 @comment The makefile standards are in a separate file that is also
10666 @comment included by standards.texi.
10667 @include make-stds.texi
10669 @node Quick Reference, Error Messages, Makefile Conventions, Top
10670 @appendix Quick Reference
10672 This appendix summarizes the directives, text manipulation functions,
10673 and special variables which GNU @code{make} understands.
10674 @xref{Special Targets}, @ref{Catalogue of Rules, ,Catalogue of Implicit Rules},
10675 and @ref{Options Summary, ,Summary of Options},
10676 for other summaries.
10678 Here is a summary of the directives GNU @code{make} recognizes:
10680 @table @code
10681 @item define @var{variable}
10682 @itemx define @var{variable} =
10683 @itemx define @var{variable} :=
10684 @itemx define @var{variable} +=
10685 @itemx define @var{variable} ?=
10686 @itemx endef
10687 Define multi-line variables.@*
10688 @xref{Multi-Line}.
10690 @item undefine @var{variable}
10691 Undefining variables.@*
10692 @xref{Undefine Directive}.
10694 @item ifdef @var{variable}
10695 @itemx ifndef @var{variable}
10696 @itemx ifeq (@var{a},@var{b})
10697 @itemx ifeq "@var{a}" "@var{b}"
10698 @itemx ifeq '@var{a}' '@var{b}'
10699 @itemx ifneq (@var{a},@var{b})
10700 @itemx ifneq "@var{a}" "@var{b}"
10701 @itemx ifneq '@var{a}' '@var{b}'
10702 @itemx else
10703 @itemx endif
10704 Conditionally evaluate part of the makefile.@*
10705 @xref{Conditionals}.
10707 @item include @var{file}
10708 @itemx -include @var{file}
10709 @itemx sinclude @var{file}
10710 Include another makefile.@*
10711 @xref{Include, ,Including Other Makefiles}.
10713 @item override @var{variable-assignment}
10714 Define a variable, overriding any previous definition, even one from
10715 the command line.@*
10716 @xref{Override Directive, ,The @code{override} Directive}.
10718 @item export
10719 Tell @code{make} to export all variables to child processes by default.@*
10720 @xref{Variables/Recursion, , Communicating Variables to a Sub-@code{make}}.
10722 @item export @var{variable}
10723 @itemx export @var{variable-assignment}
10724 @itemx unexport @var{variable}
10725 Tell @code{make} whether or not to export a particular variable to child
10726 processes.@*
10727 @xref{Variables/Recursion, , Communicating Variables to a Sub-@code{make}}.
10729 @item private @var{variable-assignment}
10730 Do not allow this variable assignment to be inherited by prerequisites.@*
10731 @xref{Suppressing Inheritance}.
10733 @item vpath @var{pattern} @var{path}
10734 Specify a search path for files matching a @samp{%} pattern.@*
10735 @xref{Selective Search, , The @code{vpath} Directive}.
10737 @item vpath @var{pattern}
10738 Remove all search paths previously specified for @var{pattern}.
10740 @item vpath
10741 Remove all search paths previously specified in any @code{vpath}
10742 directive.
10743 @end table
10745 Here is a summary of the built-in functions (@pxref{Functions}):
10747 @table @code
10748 @item $(subst @var{from},@var{to},@var{text})
10749 Replace @var{from} with @var{to} in @var{text}.@*
10750 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10752 @item $(patsubst @var{pattern},@var{replacement},@var{text})
10753 Replace words matching @var{pattern} with @var{replacement} in @var{text}.@*
10754 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10756 @item $(strip @var{string})
10757 Remove excess whitespace characters from @var{string}.@*
10758 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10760 @item $(findstring @var{find},@var{text})
10761 Locate @var{find} in @var{text}.@*
10762 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10764 @item $(filter @var{pattern}@dots{},@var{text})
10765 Select words in @var{text} that match one of the @var{pattern} words.@*
10766 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10768 @item $(filter-out @var{pattern}@dots{},@var{text})
10769 Select words in @var{text} that @emph{do not} match any of the @var{pattern} words.@*
10770 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10772 @item $(sort @var{list})
10773 Sort the words in @var{list} lexicographically, removing duplicates.@*
10774 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10776 @item $(word @var{n},@var{text})
10777 Extract the @var{n}th word (one-origin) of @var{text}.@*
10778 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10780 @item $(words @var{text})
10781 Count the number of words in @var{text}.@*
10782 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10784 @item $(wordlist @var{s},@var{e},@var{text})
10785 Returns the list of words in @var{text} from @var{s} to @var{e}.@*
10786 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10788 @item $(firstword @var{names}@dots{})
10789 Extract the first word of @var{names}.@*
10790 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10792 @item $(lastword @var{names}@dots{})
10793 Extract the last word of @var{names}.@*
10794 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10796 @item $(dir @var{names}@dots{})
10797 Extract the directory part of each file name.@*
10798 @xref{File Name Functions, ,Functions for File Names}.
10800 @item $(notdir @var{names}@dots{})
10801 Extract the non-directory part of each file name.@*
10802 @xref{File Name Functions, ,Functions for File Names}.
10804 @item $(suffix @var{names}@dots{})
10805 Extract the suffix (the last @samp{.} and following characters) of each file name.@*
10806 @xref{File Name Functions, ,Functions for File Names}.
10808 @item $(basename @var{names}@dots{})
10809 Extract the base name (name without suffix) of each file name.@*
10810 @xref{File Name Functions, ,Functions for File Names}.
10812 @item $(addsuffix @var{suffix},@var{names}@dots{})
10813 Append @var{suffix} to each word in @var{names}.@*
10814 @xref{File Name Functions, ,Functions for File Names}.
10816 @item $(addprefix @var{prefix},@var{names}@dots{})
10817 Prepend @var{prefix} to each word in @var{names}.@*
10818 @xref{File Name Functions, ,Functions for File Names}.
10820 @item $(join @var{list1},@var{list2})
10821 Join two parallel lists of words.@*
10822 @xref{File Name Functions, ,Functions for File Names}.
10824 @item $(wildcard @var{pattern}@dots{})
10825 Find file names matching a shell file name pattern (@emph{not} a
10826 @samp{%} pattern).@*
10827 @xref{Wildcard Function, ,The Function @code{wildcard}}.
10829 @item $(realpath @var{names}@dots{})
10830 For each file name in @var{names}, expand to an absolute name that
10831 does not contain any @code{.}, @code{..}, nor symlinks.@*
10832 @xref{File Name Functions, ,Functions for File Names}.
10834 @item $(abspath @var{names}@dots{})
10835 For each file name in @var{names}, expand to an absolute name that
10836 does not contain any @code{.} or @code{..} components, but preserves
10837 symlinks.@*
10838 @xref{File Name Functions, ,Functions for File Names}.
10840 @item $(error @var{text}@dots{})
10841 When this function is evaluated, @code{make} generates a fatal error
10842 with the message @var{text}.@*
10843 @xref{Make Control Functions, ,Functions That Control Make}.
10845 @item $(warning @var{text}@dots{})
10846 When this function is evaluated, @code{make} generates a warning with
10847 the message @var{text}.@*
10848 @xref{Make Control Functions, ,Functions That Control Make}.
10850 @item $(shell @var{command})
10851 Execute a shell command and return its output.@*
10852 @xref{Shell Function, , The @code{shell} Function}.
10854 @item $(origin @var{variable})
10855 Return a string describing how the @code{make} variable @var{variable} was
10856 defined.@*
10857 @xref{Origin Function, , The @code{origin} Function}.
10859 @item $(flavor @var{variable})
10860 Return a string describing the flavor of the @code{make} variable
10861 @var{variable}.@*
10862 @xref{Flavor Function, , The @code{flavor} Function}.
10864 @item $(foreach @var{var},@var{words},@var{text})
10865 Evaluate @var{text} with @var{var} bound to each word in @var{words},
10866 and concatenate the results.@*
10867 @xref{Foreach Function, ,The @code{foreach} Function}.
10869 @item $(if @var{condition},@var{then-part}[,@var{else-part}])
10870 Evaluate the condition @var{condition}; if it's non-empty substitute
10871 the expansion of the @var{then-part} otherwise substitute the
10872 expansion of the @var{else-part}.@*
10873 @xref{Conditional Functions, ,Functions for Conditionals}.
10875 @item $(or @var{condition1}[,@var{condition2}[,@var{condition3}@dots{}]])
10876 Evaluate each condition @var{conditionN} one at a time; substitute the
10877 first non-empty expansion.  If all expansions are empty, substitute
10878 the empty string.@*
10879 @xref{Conditional Functions, ,Functions for Conditionals}.
10881 @item $(and @var{condition1}[,@var{condition2}[,@var{condition3}@dots{}]])
10882 Evaluate each condition @var{conditionN} one at a time; if any
10883 expansion results in the empty string substitute the empty string.  If
10884 all expansions result in a non-empty string, substitute the expansion
10885 of the last @var{condition}.@*
10886 @xref{Conditional Functions, ,Functions for Conditionals}.
10888 @item $(call @var{var},@var{param},@dots{})
10889 Evaluate the variable @var{var} replacing any references to @code{$(1)},
10890 @code{$(2)} with the first, second, etc.@: @var{param} values.@*
10891 @xref{Call Function, ,The @code{call} Function}.
10893 @item $(eval @var{text})
10894 Evaluate @var{text} then read the results as makefile commands.
10895 Expands to the empty string.@*
10896 @xref{Eval Function, ,The @code{eval} Function}.
10898 @item $(value @var{var})
10899 Evaluates to the contents of the variable @var{var}, with no expansion
10900 performed on it.@*
10901 @xref{Value Function, ,The @code{value} Function}.
10902 @end table
10904 Here is a summary of the automatic variables.
10905 @xref{Automatic Variables},
10906 for full information.
10908 @table @code
10909 @item $@@
10910 The file name of the target.
10912 @item $%
10913 The target member name, when the target is an archive member.
10915 @item $<
10916 The name of the first prerequisite.
10918 @item $?
10919 The names of all the prerequisites that are
10920 newer than the target, with spaces between them.
10921 For prerequisites which are archive members, only
10922 the named member is used (@pxref{Archives}).
10924 @item $^
10925 @itemx $+
10926 The names of all the prerequisites, with spaces between them.  For
10927 prerequisites which are archive members, only the named member is used
10928 (@pxref{Archives}).  The value of @code{$^} omits duplicate
10929 prerequisites, while @code{$+} retains them and preserves their order.
10931 @item $*
10932 The stem with which an implicit rule matches
10933 (@pxref{Pattern Match, ,How Patterns Match}).
10935 @item $(@@D)
10936 @itemx $(@@F)
10937 The directory part and the file-within-directory part of @code{$@@}.
10939 @item $(*D)
10940 @itemx $(*F)
10941 The directory part and the file-within-directory part of @code{$*}.
10943 @item $(%D)
10944 @itemx $(%F)
10945 The directory part and the file-within-directory part of @code{$%}.
10947 @item $(<D)
10948 @itemx $(<F)
10949 The directory part and the file-within-directory part of @code{$<}.
10951 @item $(^D)
10952 @itemx $(^F)
10953 The directory part and the file-within-directory part of @code{$^}.
10955 @item $(+D)
10956 @itemx $(+F)
10957 The directory part and the file-within-directory part of @code{$+}.
10959 @item $(?D)
10960 @itemx $(?F)
10961 The directory part and the file-within-directory part of @code{$?}.
10962 @end table
10964 These variables are used specially by GNU @code{make}:
10966 @table @code
10967 @item MAKEFILES
10969 Makefiles to be read on every invocation of @code{make}.@*
10970 @xref{MAKEFILES Variable, ,The Variable @code{MAKEFILES}}.
10972 @item VPATH
10974 Directory search path for files not found in the current directory.@*
10975 @xref{General Search, , @code{VPATH} Search Path for All Prerequisites}.
10977 @item SHELL
10979 The name of the system default command interpreter, usually @file{/bin/sh}.
10980 You can set @code{SHELL} in the makefile to change the shell used to run
10981 recipes.  @xref{Execution, ,Recipe Execution}.  The @code{SHELL}
10982 variable is handled specially when importing from and exporting to the
10983 environment.  @xref{Choosing the Shell}.
10985 @item MAKESHELL
10987 On MS-DOS only, the name of the command interpreter that is to be used
10988 by @code{make}.  This value takes precedence over the value of
10989 @code{SHELL}.  @xref{Execution, ,MAKESHELL variable}.
10991 @item MAKE
10993 The name with which @code{make} was invoked.  Using this variable in
10994 recipes has special meaning.  @xref{MAKE Variable, ,How the
10995 @code{MAKE} Variable Works}.
10997 @item MAKELEVEL
10999 The number of levels of recursion (sub-@code{make}s).@*
11000 @xref{Variables/Recursion}.
11002 @item MAKEFLAGS
11004 The flags given to @code{make}.  You can set this in the environment or
11005 a makefile to set flags.@*
11006 @xref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
11008 It is @emph{never} appropriate to use @code{MAKEFLAGS} directly in a
11009 recipe line: its contents may not be quoted correctly for use in the
11010 shell.  Always allow recursive @code{make}'s to obtain these values
11011 through the environment from its parent.
11013 @item MAKECMDGOALS
11015 The targets given to @code{make} on the command line.  Setting this
11016 variable has no effect on the operation of @code{make}.@*
11017 @xref{Goals, ,Arguments to Specify the Goals}.
11019 @item CURDIR
11021 Set to the pathname of the current working directory (after all
11022 @code{-C} options are processed, if any).  Setting this variable has no
11023 effect on the operation of @code{make}.@*
11024 @xref{Recursion, ,Recursive Use of @code{make}}.
11026 @item SUFFIXES
11028 The default list of suffixes before @code{make} reads any makefiles.
11030 @item .LIBPATTERNS
11031 Defines the naming of the libraries @code{make} searches for, and their
11032 order.@*
11033 @xref{Libraries/Search, ,Directory Search for Link Libraries}.
11034 @end table
11036 @node Error Messages, Complex Makefile, Quick Reference, Top
11037 @comment  node-name,  next,  previous,  up
11038 @appendix Errors Generated by Make
11040 Here is a list of the more common errors you might see generated by
11041 @code{make}, and some information about what they mean and how to fix
11042 them.
11044 Sometimes @code{make} errors are not fatal, especially in the presence
11045 of a @code{-} prefix on a recipe line, or the @code{-k} command line
11046 option.  Errors that are fatal are prefixed with the string
11047 @code{***}.
11049 Error messages are all either prefixed with the name of the program
11050 (usually @samp{make}), or, if the error is found in a makefile, the name
11051 of the file and linenumber containing the problem.
11053 In the table below, these common prefixes are left off.
11055 @table @samp
11057 @item [@var{foo}] Error @var{NN}
11058 @itemx [@var{foo}] @var{signal description}
11059 These errors are not really @code{make} errors at all.  They mean that a
11060 program that @code{make} invoked as part of a recipe returned a
11061 non-0 error code (@samp{Error @var{NN}}), which @code{make} interprets
11062 as failure, or it exited in some other abnormal fashion (with a
11063 signal of some type).  @xref{Errors, ,Errors in Recipes}.
11065 If no @code{***} is attached to the message, then the subprocess failed
11066 but the rule in the makefile was prefixed with the @code{-} special
11067 character, so @code{make} ignored the error.
11069 @item missing separator.  Stop.
11070 @itemx missing separator (did you mean TAB instead of 8 spaces?).  Stop.
11071 This means that @code{make} could not understand much of anything
11072 about the makefile line it just read.  GNU @code{make} looks for
11073 various separators (@code{:}, @code{=}, recipe prefix characters,
11074 etc.) to indicate what kind of line it's parsing.  This message means
11075 it couldn't find a valid one.
11077 One of the most common reasons for this message is that you (or
11078 perhaps your oh-so-helpful editor, as is the case with many MS-Windows
11079 editors) have attempted to indent your recipe lines with spaces
11080 instead of a tab character.  In this case, @code{make} will use the
11081 second form of the error above.  Remember that every line in the
11082 recipe must begin with a tab character (unless you set
11083 @code{.RECIPEPREFIX}; @pxref{Special Variables}).  Eight spaces do not
11084 count.  @xref{Rule Syntax}.
11086 @item recipe commences before first target.  Stop.
11087 @itemx missing rule before recipe.  Stop.
11088 This means the first thing in the makefile seems to be part of a
11089 recipe: it begins with a recipe prefix character and doesn't appear to
11090 be a legal @code{make} directive (such as a variable assignment).
11091 Recipes must always be associated with a target.
11093 The second form is generated if the line has a semicolon as the first
11094 non-whitespace character; @code{make} interprets this to mean you left
11095 out the "target: prerequisite" section of a rule.  @xref{Rule Syntax}.
11097 @item No rule to make target `@var{xxx}'.
11098 @itemx No rule to make target `@var{xxx}', needed by `@var{yyy}'.
11099 This means that @code{make} decided it needed to build a target, but
11100 then couldn't find any instructions in the makefile on how to do that,
11101 either explicit or implicit (including in the default rules database).
11103 If you want that file to be built, you will need to add a rule to your
11104 makefile describing how that target can be built.  Other possible
11105 sources of this problem are typos in the makefile (if that filename is
11106 wrong) or a corrupted source tree (if that file is not supposed to be
11107 built, but rather only a prerequisite).
11109 @item No targets specified and no makefile found.  Stop.
11110 @itemx No targets.  Stop.
11111 The former means that you didn't provide any targets to be built on the
11112 command line, and @code{make} couldn't find any makefiles to read in.
11113 The latter means that some makefile was found, but it didn't contain any
11114 default goal and none was given on the command line.  GNU @code{make}
11115 has nothing to do in these situations.
11116 @xref{Makefile Arguments, ,Arguments to Specify the Makefile}.@refill
11118 @item Makefile `@var{xxx}' was not found.
11119 @itemx Included makefile `@var{xxx}' was not found.
11120 A makefile specified on the command line (first form) or included
11121 (second form) was not found.
11123 @item warning: overriding recipe for target `@var{xxx}'
11124 @itemx warning: ignoring old recipe for target `@var{xxx}'
11125 GNU @code{make} allows only one recipe to be specified per target
11126 (except for double-colon rules).  If you give a recipe for a target
11127 which already has been defined to have one, this warning is issued and
11128 the second recipe will overwrite the first.  @xref{Multiple Rules,
11129 ,Multiple Rules for One Target}.
11131 @item Circular @var{xxx} <- @var{yyy} dependency dropped.
11132 This means that @code{make} detected a loop in the dependency graph:
11133 after tracing the prerequisite @var{yyy} of target @var{xxx}, and its
11134 prerequisites, etc., one of them depended on @var{xxx} again.
11136 @item Recursive variable `@var{xxx}' references itself (eventually).  Stop.
11137 This means you've defined a normal (recursive) @code{make} variable
11138 @var{xxx} that, when it's expanded, will refer to itself (@var{xxx}).
11139 This is not allowed; either use simply-expanded variables (@code{:=}) or
11140 use the append operator (@code{+=}).  @xref{Using Variables, ,How to Use
11141 Variables}.
11143 @item Unterminated variable reference.  Stop.
11144 This means you forgot to provide the proper closing parenthesis
11145 or brace in your variable or function reference.
11147 @item insufficient arguments to function `@var{xxx}'.  Stop.
11148 This means you haven't provided the requisite number of arguments for
11149 this function.  See the documentation of the function for a description
11150 of its arguments.  @xref{Functions, ,Functions for Transforming Text}.
11152 @item missing target pattern.  Stop.
11153 @itemx multiple target patterns.  Stop.
11154 @itemx target pattern contains no `%'.  Stop.
11155 @itemx mixed implicit and static pattern rules.  Stop.
11156 These are generated for malformed static pattern rules.  The first means
11157 there's no pattern in the target section of the rule; the second means
11158 there are multiple patterns in the target section; the third means
11159 the target doesn't contain a pattern character (@code{%}); and the
11160 fourth means that all three parts of the static pattern rule contain
11161 pattern characters (@code{%})--only the first two parts should.
11162 @xref{Static Usage, ,Syntax of Static Pattern Rules}.
11164 @item warning: -jN forced in submake: disabling jobserver mode.
11165 This warning and the next are generated if @code{make} detects error
11166 conditions related to parallel processing on systems where
11167 sub-@code{make}s can communicate (@pxref{Options/Recursion,
11168 ,Communicating Options to a Sub-@code{make}}).  This warning is
11169 generated if a recursive invocation of a @code{make} process is forced
11170 to have @samp{-j@var{N}} in its argument list (where @var{N} is greater
11171 than one).  This could happen, for example, if you set the @code{MAKE}
11172 environment variable to @samp{make -j2}.  In this case, the
11173 sub-@code{make} doesn't communicate with other @code{make} processes and
11174 will simply pretend it has two jobs of its own.
11176 @item warning: jobserver unavailable: using -j1.  Add `+' to parent make rule.
11177 In order for @code{make} processes to communicate, the parent will pass
11178 information to the child.  Since this could result in problems if the
11179 child process isn't actually a @code{make}, the parent will only do this
11180 if it thinks the child is a @code{make}.  The parent uses the normal
11181 algorithms to determine this (@pxref{MAKE Variable, ,How the @code{MAKE}
11182 Variable Works}).  If the makefile is constructed such that the parent
11183 doesn't know the child is a @code{make} process, then the child will
11184 receive only part of the information necessary.  In this case, the child
11185 will generate this warning message and proceed with its build in a
11186 sequential manner.
11188 @end table
11190 @node Complex Makefile, GNU Free Documentation License, Error Messages, Top
11191 @appendix Complex Makefile Example
11193 Here is the makefile for the GNU @code{tar} program.  This is a
11194 moderately complex makefile.
11196 Because it is the first target, the default goal is @samp{all}.  An
11197 interesting feature of this makefile is that @file{testpad.h} is a
11198 source file automatically created by the @code{testpad} program,
11199 itself compiled from @file{testpad.c}.
11201 If you type @samp{make} or @samp{make all}, then @code{make} creates
11202 the @file{tar} executable, the @file{rmt} daemon that provides
11203 remote tape access, and the @file{tar.info} Info file.
11205 If you type @samp{make install}, then @code{make} not only creates
11206 @file{tar}, @file{rmt}, and @file{tar.info}, but also installs
11207 them.
11209 If you type @samp{make clean}, then @code{make} removes the @samp{.o}
11210 files, and the @file{tar}, @file{rmt}, @file{testpad},
11211 @file{testpad.h}, and @file{core} files.
11213 If you type @samp{make distclean}, then @code{make} not only removes
11214 the same files as does @samp{make clean} but also the
11215 @file{TAGS}, @file{Makefile}, and @file{config.status} files.
11216 (Although it is not evident, this makefile (and
11217 @file{config.status}) is generated by the user with the
11218 @code{configure} program, which is provided in the @code{tar}
11219 distribution, but is not shown here.)
11221 If you type @samp{make realclean}, then @code{make} removes the same
11222 files as does @samp{make distclean} and also removes the Info files
11223 generated from @file{tar.texinfo}.
11225 In addition, there are targets @code{shar} and @code{dist} that create
11226 distribution kits.
11228 @example
11229 @group
11230 # Generated automatically from Makefile.in by configure.
11231 # Un*x Makefile for GNU tar program.
11232 # Copyright (C) 1991 Free Software Foundation, Inc.
11233 @end group
11235 @group
11236 # This program is free software; you can redistribute
11237 # it and/or modify it under the terms of the GNU
11238 # General Public License @dots{}
11239 @dots{}
11240 @dots{}
11241 @end group
11243 SHELL = /bin/sh
11245 #### Start of system configuration section. ####
11247 srcdir = .
11249 @group
11250 # If you use gcc, you should either run the
11251 # fixincludes script that comes with it or else use
11252 # gcc with the -traditional option.  Otherwise ioctl
11253 # calls will be compiled incorrectly on some systems.
11254 CC = gcc -O
11255 YACC = bison -y
11256 INSTALL = /usr/local/bin/install -c
11257 INSTALLDATA = /usr/local/bin/install -c -m 644
11258 @end group
11260 # Things you might add to DEFS:
11261 # -DSTDC_HEADERS        If you have ANSI C headers and
11262 #                       libraries.
11263 # -DPOSIX               If you have POSIX.1 headers and
11264 #                       libraries.
11265 # -DBSD42               If you have sys/dir.h (unless
11266 #                       you use -DPOSIX), sys/file.h,
11267 #                       and st_blocks in `struct stat'.
11268 # -DUSG                 If you have System V/ANSI C
11269 #                       string and memory functions
11270 #                       and headers, sys/sysmacros.h,
11271 #                       fcntl.h, getcwd, no valloc,
11272 #                       and ndir.h (unless
11273 #                       you use -DDIRENT).
11274 # -DNO_MEMORY_H         If USG or STDC_HEADERS but do not
11275 #                       include memory.h.
11276 # -DDIRENT              If USG and you have dirent.h
11277 #                       instead of ndir.h.
11278 # -DSIGTYPE=int         If your signal handlers
11279 #                       return int, not void.
11280 # -DNO_MTIO             If you lack sys/mtio.h
11281 #                       (magtape ioctls).
11282 # -DNO_REMOTE           If you do not have a remote shell
11283 #                       or rexec.
11284 # -DUSE_REXEC           To use rexec for remote tape
11285 #                       operations instead of
11286 #                       forking rsh or remsh.
11287 # -DVPRINTF_MISSING     If you lack vprintf function
11288 #                       (but have _doprnt).
11289 # -DDOPRNT_MISSING      If you lack _doprnt function.
11290 #                       Also need to define
11291 #                       -DVPRINTF_MISSING.
11292 # -DFTIME_MISSING       If you lack ftime system call.
11293 # -DSTRSTR_MISSING      If you lack strstr function.
11294 # -DVALLOC_MISSING      If you lack valloc function.
11295 # -DMKDIR_MISSING       If you lack mkdir and
11296 #                       rmdir system calls.
11297 # -DRENAME_MISSING      If you lack rename system call.
11298 # -DFTRUNCATE_MISSING   If you lack ftruncate
11299 #                       system call.
11300 # -DV7                  On Version 7 Unix (not
11301 #                       tested in a long time).
11302 # -DEMUL_OPEN3          If you lack a 3-argument version
11303 #                       of open, and want to emulate it
11304 #                       with system calls you do have.
11305 # -DNO_OPEN3            If you lack the 3-argument open
11306 #                       and want to disable the tar -k
11307 #                       option instead of emulating open.
11308 # -DXENIX               If you have sys/inode.h
11309 #                       and need it 94 to be included.
11311 DEFS =  -DSIGTYPE=int -DDIRENT -DSTRSTR_MISSING \
11312         -DVPRINTF_MISSING -DBSD42
11313 # Set this to rtapelib.o unless you defined NO_REMOTE,
11314 # in which case make it empty.
11315 RTAPELIB = rtapelib.o
11316 LIBS =
11317 DEF_AR_FILE = /dev/rmt8
11318 DEFBLOCKING = 20
11320 @group
11321 CDEBUG = -g
11322 CFLAGS = $(CDEBUG) -I. -I$(srcdir) $(DEFS) \
11323         -DDEF_AR_FILE=\"$(DEF_AR_FILE)\" \
11324         -DDEFBLOCKING=$(DEFBLOCKING)
11325 LDFLAGS = -g
11326 @end group
11328 @group
11329 prefix = /usr/local
11330 # Prefix for each installed program,
11331 # normally empty or `g'.
11332 binprefix =
11334 # The directory to install tar in.
11335 bindir = $(prefix)/bin
11337 # The directory to install the info files in.
11338 infodir = $(prefix)/info
11339 @end group
11341 #### End of system configuration section. ####
11343 @group
11344 SRCS_C  = tar.c create.c extract.c buffer.c   \
11345           getoldopt.c update.c gnu.c mangle.c \
11346           version.c list.c names.c diffarch.c \
11347           port.c wildmat.c getopt.c getopt1.c \
11348           regex.c
11349 SRCS_Y  = getdate.y
11350 SRCS    = $(SRCS_C) $(SRCS_Y)
11351 OBJS    = $(SRCS_C:.c=.o) $(SRCS_Y:.y=.o) $(RTAPELIB)
11352 @end group
11353 @group
11354 AUX =   README COPYING ChangeLog Makefile.in  \
11355         makefile.pc configure configure.in \
11356         tar.texinfo tar.info* texinfo.tex \
11357         tar.h port.h open3.h getopt.h regex.h \
11358         rmt.h rmt.c rtapelib.c alloca.c \
11359         msd_dir.h msd_dir.c tcexparg.c \
11360         level-0 level-1 backup-specs testpad.c
11361 @end group
11363 .PHONY: all
11364 all:    tar rmt tar.info
11366 @group
11367 tar:    $(OBJS)
11368         $(CC) $(LDFLAGS) -o $@@ $(OBJS) $(LIBS)
11369 @end group
11371 @group
11372 rmt:    rmt.c
11373         $(CC) $(CFLAGS) $(LDFLAGS) -o $@@ rmt.c
11374 @end group
11376 @group
11377 tar.info: tar.texinfo
11378         makeinfo tar.texinfo
11379 @end group
11381 @group
11382 .PHONY: install
11383 install: all
11384         $(INSTALL) tar $(bindir)/$(binprefix)tar
11385         -test ! -f rmt || $(INSTALL) rmt /etc/rmt
11386         $(INSTALLDATA) $(srcdir)/tar.info* $(infodir)
11387 @end group
11389 @group
11390 $(OBJS): tar.h port.h testpad.h
11391 regex.o buffer.o tar.o: regex.h
11392 # getdate.y has 8 shift/reduce conflicts.
11393 @end group
11395 @group
11396 testpad.h: testpad
11397         ./testpad
11398 @end group
11400 @group
11401 testpad: testpad.o
11402         $(CC) -o $@@ testpad.o
11403 @end group
11405 @group
11406 TAGS:   $(SRCS)
11407         etags $(SRCS)
11408 @end group
11410 @group
11411 .PHONY: clean
11412 clean:
11413         rm -f *.o tar rmt testpad testpad.h core
11414 @end group
11416 @group
11417 .PHONY: distclean
11418 distclean: clean
11419         rm -f TAGS Makefile config.status
11420 @end group
11422 @group
11423 .PHONY: realclean
11424 realclean: distclean
11425         rm -f tar.info*
11426 @end group
11428 @group
11429 .PHONY: shar
11430 shar: $(SRCS) $(AUX)
11431         shar $(SRCS) $(AUX) | compress \
11432           > tar-`sed -e '/version_string/!d' \
11433                      -e 's/[^0-9.]*\([0-9.]*\).*/\1/' \
11434                      -e q
11435                      version.c`.shar.Z
11436 @end group
11438 @group
11439 .PHONY: dist
11440 dist: $(SRCS) $(AUX)
11441         echo tar-`sed \
11442              -e '/version_string/!d' \
11443              -e 's/[^0-9.]*\([0-9.]*\).*/\1/' \
11444              -e q
11445              version.c` > .fname
11446         -rm -rf `cat .fname`
11447         mkdir `cat .fname`
11448         ln $(SRCS) $(AUX) `cat .fname`
11449         tar chZf `cat .fname`.tar.Z `cat .fname`
11450         -rm -rf `cat .fname` .fname
11451 @end group
11453 @group
11454 tar.zoo: $(SRCS) $(AUX)
11455         -rm -rf tmp.dir
11456         -mkdir tmp.dir
11457         -rm tar.zoo
11458         for X in $(SRCS) $(AUX) ; do \
11459             echo $$X ; \
11460             sed 's/$$/^M/' $$X \
11461             > tmp.dir/$$X ; done
11462         cd tmp.dir ; zoo aM ../tar.zoo *
11463         -rm -rf tmp.dir
11464 @end group
11465 @end example
11467 @node GNU Free Documentation License, Concept Index, Complex Makefile, Top
11468 @appendixsec GNU Free Documentation License
11469 @cindex FDL, GNU Free Documentation License
11470 @include fdl.texi
11472 @node Concept Index, Name Index, GNU Free Documentation License, Top
11473 @unnumbered Index of Concepts
11475 @printindex cp
11477 @node Name Index,  , Concept Index, Top
11478 @unnumbered Index of Functions, Variables, & Directives
11480 @printindex fn
11482 @bye