Fix a bug where a variable could be used without being initialized in W32.
[make.git] / doc / make.texi
blobe0200301d8236d8b834c560b7cd603be76dfe9e6
1 \input texinfo                @c -*- Texinfo -*-
2 @c %**start of header
3 @setfilename make.info
4 @settitle GNU @code{make}
5 @setchapternewpage odd
6 @c %**end of header
8 @c FSF publishers: format makebook.texi instead of using this file directly.
10 @set RCSID $Id: make.texi,v 1.40 2006/02/01 07:54:22 psmith Exp $
11 @set EDITION 0.70
12 @set VERSION 3.81
13 @set UPDATED 29 Jan 2006
14 @set UPDATE-MONTH Jan 2006
15 @c ISBN provided by Lisa M. Opus Goldstein <opus@gnu.org>, 5 May 2004
16 @set ISBN 1-882114-83-5
18 @c finalout
20 @c ISPELL CHECK: done, 10 June 1993 --roland
21 @c ISPELL CHECK: done, 2000-06-25 --Martin Buchholz
23 @c Combine the variable and function indices:
24 @syncodeindex vr fn
25 @c Combine the program and concept indices:
26 @syncodeindex pg cp
28 @dircategory GNU Packages
29 @direntry
30 * Make: (make).            Remake files automatically.
31 @end direntry
33 @ifnottex
34 This file documents the GNU Make utility, which determines
35 automatically which pieces of a large program need to be recompiled,
36 and issues the commands to recompile them.
38 This is Edition @value{EDITION}, last updated @value{UPDATED},
39 of @cite{The GNU Make Manual}, for @code{make}, Version @value{VERSION}.
41 Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
42 1998, 1999, 2000, 2002, 2003, 2004, 2005
43 Free Software Foundation, Inc.
45 Permission is granted to copy, distribute and/or modify this document
46 under the terms of the GNU Free Documentation License, Version 1.1 or
47 any later version published by the Free Software Foundation; with no
48 Invariant Sections, with no Front-Cover Texts, and with no Back-Cover
49 Texts.  A copy of the license is included in the section entitled
50 ``GNU Free Documentation License''.
51 @end ifnottex
53 @iftex
54 @shorttitlepage GNU Make
55 @end iftex
56 @titlepage
57 @title GNU Make
58 @subtitle A Program for Directing Recompilation
59 @subtitle GNU @code{make} Version @value{VERSION}
60 @subtitle @value{UPDATE-MONTH}
61 @author Richard M. Stallman, Roland McGrath, Paul D. Smith
62 @page
63 @vskip 0pt plus 1filll
64 Copyright @copyright{} 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
65 1996, 1997, 1998, 1999, 2000, 2002, 2003, 2004 Free Software Foundation, Inc.
66 @sp 2
67 Published by the Free Software Foundation @*
68 59 Temple Place -- Suite 330, @*
69 Boston, MA 02111-1307 USA @*
70 ISBN @value{ISBN} @*
72 Permission is granted to copy, distribute and/or modify this document
73 under the terms of the GNU Free Documentation License, Version 1.1 or
74 any later version published by the Free Software Foundation; with the
75 Invariant Sections being ``GNU General Public License'', the Front-Cover
76 Texts being ``A GNU Manual'', and with the Back-Cover Texts being as in
77 (a) below.  A copy of the license is included in the section entitled
78 ``GNU Free Documentation License''.
80 (a) The FSF's Back-Cover Text is:
82 @quotation
83       You have freedom to copy and modify this GNU Manual, like GNU
84       software.  Copies published by the Free Software Foundation raise
85       funds for GNU development.
86 @end quotation
87 @sp 2
88 Cover art by Etienne Suvasa.
89 @end titlepage
90 @page
92 @ifnottex
93 @node Top, Overview, (dir), (dir)
94 @top Make
96 The GNU @code{make} utility automatically determines which pieces of a
97 large program need to be recompiled, and issues the commands to
98 recompile them.@refill
100 This edition of the @cite{GNU Make Manual},
101 last updated @value{UPDATED},
102 documents GNU @code{make} Version @value{VERSION}.@refill
104 This manual describes @code{make} and contains the following chapters:@refill
105 @end ifnottex
107 @menu
108 * Overview::                    Overview of @code{make}.
109 * Introduction::                An introduction to @code{make}.
110 * Makefiles::                   Makefiles tell @code{make} what to do.
111 * Rules::                       Rules describe when a file must be remade.
112 * Commands::                    Commands say how to remake a file.
113 * Using Variables::             You can use variables to avoid repetition.
114 * Conditionals::                Use or ignore parts of the makefile based
115                                   on the values of variables.
116 * Functions::                   Many powerful ways to manipulate text.
117 * Invoking make: Running.       How to invoke @code{make} on the command line.
118 * Implicit Rules::              Use implicit rules to treat many files alike,
119                                   based on their file names.
120 * Archives::                    How @code{make} can update library archives.
121 * Features::                    Features GNU @code{make} has over other @code{make}s.
122 * Missing::                     What GNU @code{make} lacks from other @code{make}s.
123 * Makefile Conventions::        Conventions for writing makefiles for
124                                   GNU programs.
125 * Quick Reference::             A quick reference for experienced users.
126 * Error Messages::              A list of common errors generated by @code{make}.
127 * Complex Makefile::            A real example of a straightforward,
128                                   but nontrivial, makefile.
130 * GNU Free Documentation License::  License for copying this manual
131 * Concept Index::               Index of Concepts
132 * Name Index::                  Index of Functions, Variables, & Directives
134 @detailmenu
135  --- The Detailed Node Listing ---
137 Overview of @code{make}
139 * Preparing::                   Preparing and Running Make
140 * Reading::                     On Reading this Text
141 * Bugs::                        Problems and Bugs
143 An Introduction to Makefiles
145 * Rule Introduction::           What a rule looks like.
146 * Simple Makefile::             A Simple Makefile
147 * How Make Works::              How @code{make} Processes This Makefile
148 * Variables Simplify::          Variables Make Makefiles Simpler
149 * make Deduces::                Letting @code{make} Deduce the Commands
150 * Combine By Prerequisite::     Another Style of Makefile
151 * Cleanup::                     Rules for Cleaning the Directory
153 Writing Makefiles
155 * Makefile Contents::           What makefiles contain.
156 * Makefile Names::              How to name your makefile.
157 * Include::                     How one makefile can use another makefile.
158 * MAKEFILES Variable::          The environment can specify extra makefiles.
159 * MAKEFILE_LIST Variable::      Discover which makefiles have been read.
160 * Special Variables::           Other special variables.
161 * Remaking Makefiles::          How makefiles get remade.
162 * Overriding Makefiles::        How to override part of one makefile
163                                   with another makefile.
164 * Reading Makefiles::           How makefiles are parsed.
165 * Secondary Expansion::         How and when secondary expansion is performed.
167 Writing Rules
169 * Rule Example::                An example explained.
170 * Rule Syntax::                 General syntax explained.
171 * Prerequisite Types::          There are two types of prerequisites.
172 * Wildcards::                   Using wildcard characters such as `*'.
173 * Directory Search::            Searching other directories for source files.
174 * Phony Targets::               Using a target that is not a real file's name.
175 * Force Targets::               You can use a target without commands
176                                   or prerequisites to mark other
177                                   targets as phony.
178 * Empty Targets::               When only the date matters and the
179                                   files are empty.
180 * Special Targets::             Targets with special built-in meanings.
181 * Multiple Targets::            When to make use of several targets in a rule.
182 * Multiple Rules::              How to use several rules with the same target.
183 * Static Pattern::              Static pattern rules apply to multiple targets
184                                   and can vary the prerequisites according to
185                                   the target name.
186 * Double-Colon::                How to use a special kind of rule to allow
187                                   several independent rules for one target.
188 * Automatic Prerequisites::     How to automatically generate rules giving
189                                   prerequisites from source files themselves.
191 Using Wildcard Characters in File Names
193 * Wildcard Examples::           Several examples
194 * Wildcard Pitfall::            Problems to avoid.
195 * Wildcard Function::           How to cause wildcard expansion where
196                                   it does not normally take place.
198 Searching Directories for Prerequisites
200 * General Search::              Specifying a search path that applies
201                                   to every prerequisite.
202 * Selective Search::            Specifying a search path
203                                   for a specified class of names.
204 * Search Algorithm::            When and how search paths are applied.
205 * Commands/Search::             How to write shell commands that work together
206                                   with search paths.
207 * Implicit/Search::             How search paths affect implicit rules.
208 * Libraries/Search::            Directory search for link libraries.
210 Static Pattern Rules
212 * Static Usage::                The syntax of static pattern rules.
213 * Static versus Implicit::      When are they better than implicit rules?
215 Writing the Commands in Rules
217 * Echoing::                     How to control when commands are echoed.
218 * Execution::                   How commands are executed.
219 * Parallel::                    How commands can be executed in parallel.
220 * Errors::                      What happens after a command execution error.
221 * Interrupts::                  What happens when a command is interrupted.
222 * Recursion::                   Invoking @code{make} from makefiles.
223 * Sequences::                   Defining canned sequences of commands.
224 * Empty Commands::              Defining useful, do-nothing commands.
226 Recursive Use of @code{make}
228 * MAKE Variable::               The special effects of using @samp{$(MAKE)}.
229 * Variables/Recursion::         How to communicate variables to a sub-@code{make}.
230 * Options/Recursion::           How to communicate options to a sub-@code{make}.
231 * -w Option::                   How the @samp{-w} or @samp{--print-directory} option
232                                   helps debug use of recursive @code{make} commands.
234 How to Use Variables
236 * Reference::                   How to use the value of a variable.
237 * Flavors::                     Variables come in two flavors.
238 * Advanced::                    Advanced features for referencing a variable.
239 * Values::                      All the ways variables get their values.
240 * Setting::                     How to set a variable in the makefile.
241 * Appending::                   How to append more text to the old value
242                                   of a variable.
243 * Override Directive::          How to set a variable in the makefile even if
244                                   the user has set it with a command argument.
245 * Defining::                    An alternate way to set a variable
246                                   to a verbatim string.
247 * Environment::                 Variable values can come from the environment.
248 * Target-specific::             Variable values can be defined on a per-target
249                                   basis.
250 * Pattern-specific::            Target-specific variable values can be applied
251                                   to a group of targets that match a pattern.
253 Advanced Features for Reference to Variables
255 * Substitution Refs::           Referencing a variable with
256                                   substitutions on the value.
257 * Computed Names::              Computing the name of the variable to refer to.
259 Conditional Parts of Makefiles
261 * Conditional Example::         Example of a conditional
262 * Conditional Syntax::          The syntax of conditionals.
263 * Testing Flags::               Conditionals that test flags.
265 Functions for Transforming Text
267 * Syntax of Functions::         How to write a function call.
268 * Text Functions::              General-purpose text manipulation functions.
269 * File Name Functions::         Functions for manipulating file names.
270 * Foreach Function::            Repeat some text with controlled variation.
271 * If Function::                 Conditionally expand a value.
272 * Call Function::               Expand a user-defined function.
273 * Value Function::              Return the un-expanded value of a variable.
274 * Eval Function::               Evaluate the arguments as makefile syntax.
275 * Origin Function::             Find where a variable got its value.
276 * Flavor Function::             Find out the flavor of a variable.
277 * Shell Function::              Substitute the output of a shell command.
278 * Make Control Functions::      Functions that control how make runs.
280 How to Run @code{make}
282 * Makefile Arguments::          How to specify which makefile to use.
283 * Goals::                       How to use goal arguments to specify which
284                                   parts of the makefile to use.
285 * Instead of Execution::        How to use mode flags to specify what
286                                   kind of thing to do with the commands
287                                   in the makefile other than simply
288                                   execute them.
289 * Avoiding Compilation::        How to avoid recompiling certain files.
290 * Overriding::                  How to override a variable to specify
291                                   an alternate compiler and other things.
292 * Testing::                     How to proceed past some errors, to
293                                   test compilation.
294 * Options Summary::             Summary of Options
296 Using Implicit Rules
298 * Using Implicit::              How to use an existing implicit rule
299                                   to get the commands for updating a file.
300 * Catalogue of Rules::          A list of built-in implicit rules.
301 * Implicit Variables::          How to change what predefined rules do.
302 * Chained Rules::               How to use a chain of implicit rules.
303 * Pattern Rules::               How to define new implicit rules.
304 * Last Resort::                 How to defining commands for rules
305                                   which cannot find any.
306 * Suffix Rules::                The old-fashioned style of implicit rule.
307 * Implicit Rule Search::        The precise algorithm for applying
308                                   implicit rules.
310 Defining and Redefining Pattern Rules
312 * Pattern Intro::               An introduction to pattern rules.
313 * Pattern Examples::            Examples of pattern rules.
314 * Automatic Variables::         How to use automatic variables in the
315                                   commands of implicit rules.
316 * Pattern Match::               How patterns match.
317 * Match-Anything Rules::        Precautions you should take prior to
318                                   defining rules that can match any
319                                   target file whatever.
320 * Canceling Rules::             How to override or cancel built-in rules.
322 Using @code{make} to Update Archive Files
324 * Archive Members::             Archive members as targets.
325 * Archive Update::              The implicit rule for archive member targets.
326 * Archive Pitfalls::            Dangers to watch out for when using archives.
327 * Archive Suffix Rules::        You can write a special kind of suffix rule
328                                   for updating archives.
330 Implicit Rule for Archive Member Targets
332 * Archive Symbols::             How to update archive symbol directories.
334 Makefile Conventions
336 * Makefile Basics::             General Conventions for Makefiles
337 * Utilities in Makefiles::      Utilities in Makefiles
338 * Command Variables::           Variables for Specifying Commands
339 * Directory Variables::         Variables for Installation Directories
340 * Standard Targets::            Standard Targets for Users
341 * Install Command Categories::  Three categories of commands in the `install'
343 @end detailmenu
344 @end menu
346 @node Overview, Introduction, Top, Top
347 @comment  node-name,  next,  previous,  up
348 @chapter Overview of @code{make}
350 The @code{make} utility automatically determines which pieces of a large
351 program need to be recompiled, and issues commands to recompile them.
352 This manual describes GNU @code{make}, which was implemented by Richard
353 Stallman and Roland McGrath.  Development since Version 3.76 has been
354 handled by Paul D. Smith.
356 GNU @code{make} conforms to section 6.2 of @cite{IEEE Standard
357 1003.2-1992} (POSIX.2).
358 @cindex POSIX
359 @cindex IEEE Standard 1003.2
360 @cindex standards conformance
362 Our examples show C programs, since they are most common, but you can use
363 @code{make} with any programming language whose compiler can be run with a
364 shell command.  Indeed, @code{make} is not limited to programs.  You can
365 use it to describe any task where some files must be updated automatically
366 from others whenever the others change.
368 @menu
369 * Preparing::                   Preparing and Running Make
370 * Reading::                     On Reading this Text
371 * Bugs::                        Problems and Bugs
372 @end menu
374 @node Preparing, Reading, Overview, Overview
375 @ifnottex
376 @heading Preparing and Running Make
377 @end ifnottex
379 To prepare to use @code{make}, you must write a file called
380 the @dfn{makefile} that describes the relationships among files
381 in your program and provides commands for updating each file.
382 In a program, typically, the executable file is updated from object
383 files, which are in turn made by compiling source files.@refill
385 Once a suitable makefile exists, each time you change some source files,
386 this simple shell command:
388 @example
389 make
390 @end example
392 @noindent
393 suffices to perform all necessary recompilations.  The @code{make} program
394 uses the makefile data base and the last-modification times of the files to
395 decide which of the files need to be updated.  For each of those files, it
396 issues the commands recorded in the data base.
398 You can provide command line arguments to @code{make} to control which
399 files should be recompiled, or how.  @xref{Running, ,How to Run
400 @code{make}}.
402 @node Reading, Bugs, Preparing, Overview
403 @section How to Read This Manual
405 If you are new to @code{make}, or are looking for a general
406 introduction, read the first few sections of each chapter, skipping the
407 later sections.  In each chapter, the first few sections contain
408 introductory or general information and the later sections contain
409 specialized or technical information.
410 @ifnottex
411 The exception is the second chapter, @ref{Introduction, ,An
412 Introduction to Makefiles}, all of which is introductory.
413 @end ifnottex
414 @iftex
415 The exception is @ref{Introduction, ,An Introduction to Makefiles},
416 all of which is introductory.
417 @end iftex
419 If you are familiar with other @code{make} programs, see @ref{Features,
420 ,Features of GNU @code{make}}, which lists the enhancements GNU
421 @code{make} has, and @ref{Missing, ,Incompatibilities and Missing
422 Features}, which explains the few things GNU @code{make} lacks that
423 others have.
425 For a quick summary, see @ref{Options Summary}, @ref{Quick Reference},
426 and @ref{Special Targets}.
428 @node Bugs,  , Reading, Overview
429 @section Problems and Bugs
430 @cindex reporting bugs
431 @cindex bugs, reporting
432 @cindex problems and bugs, reporting
434 If you have problems with GNU @code{make} or think you've found a bug,
435 please report it to the developers; we cannot promise to do anything but
436 we might well want to fix it.
438 Before reporting a bug, make sure you've actually found a real bug.
439 Carefully reread the documentation and see if it really says you can do
440 what you're trying to do.  If it's not clear whether you should be able
441 to do something or not, report that too; it's a bug in the
442 documentation!
444 Before reporting a bug or trying to fix it yourself, try to isolate it
445 to the smallest possible makefile that reproduces the problem.  Then
446 send us the makefile and the exact results @code{make} gave you,
447 including any error or warning messages.  Please don't paraphrase
448 these messages: it's best to cut and paste them into your report.
449 When generating this small makefile, be sure to not use any non-free
450 or unusual tools in your commands: you can almost always emulate what
451 such a tool would do with simple shell commands.  Finally, be sure to
452 explain what you expected to occur; this will help us decide whether
453 the problem was really in the documentation.
455 Once you have a precise problem you can report it in one of two ways.
456 Either send electronic mail to:
458 @example
459     bug-make@@gnu.org
460 @end example
462 @noindent
463 or use our Web-based project management tool, at:
465 @example
466     http://savannah.gnu.org/projects/make/
467 @end example
469 @noindent
470 In addition to the information above, please be careful to include the
471 version number of @code{make} you are using.  You can get this
472 information with the command @samp{make --version}.  Be sure also to
473 include the type of machine and operating system you are using.  One
474 way to obtain this information is by looking at the final lines of
475 output from the command @samp{make --help}.
477 @node Introduction, Makefiles, Overview, Top
478 @comment  node-name,  next,  previous,  up
479 @chapter An Introduction to Makefiles
481 You need a file called a @dfn{makefile} to tell @code{make} what to do.
482 Most often, the makefile tells @code{make} how to compile and link a
483 program.
484 @cindex makefile
486 In this chapter, we will discuss a simple makefile that describes how to
487 compile and link a text editor which consists of eight C source files
488 and three header files.  The makefile can also tell @code{make} how to
489 run miscellaneous commands when explicitly asked (for example, to remove
490 certain files as a clean-up operation).  To see a more complex example
491 of a makefile, see @ref{Complex Makefile}.
493 When @code{make} recompiles the editor, each changed C source file
494 must be recompiled.  If a header file has changed, each C source file
495 that includes the header file must be recompiled to be safe.  Each
496 compilation produces an object file corresponding to the source file.
497 Finally, if any source file has been recompiled, all the object files,
498 whether newly made or saved from previous compilations, must be linked
499 together to produce the new executable editor.
500 @cindex recompilation
501 @cindex editor
503 @menu
504 * Rule Introduction::           What a rule looks like.
505 * Simple Makefile::             A Simple Makefile
506 * How Make Works::              How @code{make} Processes This Makefile
507 * Variables Simplify::          Variables Make Makefiles Simpler
508 * make Deduces::                Letting @code{make} Deduce the Commands
509 * Combine By Prerequisite::     Another Style of Makefile
510 * Cleanup::                     Rules for Cleaning the Directory
511 @end menu
513 @node Rule Introduction, Simple Makefile, Introduction, Introduction
514 @comment  node-name,  next,  previous,  up
515 @section What a Rule Looks Like
516 @cindex rule, introduction to
517 @cindex makefile rule parts
518 @cindex parts of makefile rule
520 A simple makefile consists of ``rules'' with the following shape:
522 @cindex targets, introduction to
523 @cindex prerequisites, introduction to
524 @cindex commands, introduction to
525 @example
526 @group
527 @var{target} @dots{} : @var{prerequisites} @dots{}
528         @var{command}
529         @dots{}
530         @dots{}
531 @end group
532 @end example
534 A @dfn{target} is usually the name of a file that is generated by a
535 program; examples of targets are executable or object files.  A target
536 can also be the name of an action to carry out, such as @samp{clean}
537 (@pxref{Phony Targets}).
539 A @dfn{prerequisite} is a file that is used as input to create the
540 target.  A target often depends on several files.
542 @cindex tabs in rules
543 A @dfn{command} is an action that @code{make} carries out.
544 A rule may have more than one command, each on its own line.
545 @strong{Please note:} you need to put a tab character at the beginning of
546 every command line!  This is an obscurity that catches the unwary.
548 Usually a command is in a rule with prerequisites and serves to create a
549 target file if any of the prerequisites change.  However, the rule that
550 specifies commands for the target need not have prerequisites.  For
551 example, the rule containing the delete command associated with the
552 target @samp{clean} does not have prerequisites.
554 A @dfn{rule}, then, explains how and when to remake certain files
555 which are the targets of the particular rule.  @code{make} carries out
556 the commands on the prerequisites to create or update the target.  A
557 rule can also explain how and when to carry out an action.
558 @xref{Rules, , Writing Rules}.
560 A makefile may contain other text besides rules, but a simple makefile
561 need only contain rules.  Rules may look somewhat more complicated
562 than shown in this template, but all fit the pattern more or less.
564 @node Simple Makefile, How Make Works, Rule Introduction, Introduction
565 @section A Simple Makefile
566 @cindex simple makefile
567 @cindex makefile, simple
569 Here is a straightforward makefile that describes the way an
570 executable file called @code{edit} depends on eight object files
571 which, in turn, depend on eight C source and three header files.
573 In this example, all the C files include @file{defs.h}, but only those
574 defining editing commands include @file{command.h}, and only low
575 level files that change the editor buffer include @file{buffer.h}.
577 @example
578 @group
579 edit : main.o kbd.o command.o display.o \
580        insert.o search.o files.o utils.o
581         cc -o edit main.o kbd.o command.o display.o \
582                    insert.o search.o files.o utils.o
584 main.o : main.c defs.h
585         cc -c main.c
586 kbd.o : kbd.c defs.h command.h
587         cc -c kbd.c
588 command.o : command.c defs.h command.h
589         cc -c command.c
590 display.o : display.c defs.h buffer.h
591         cc -c display.c
592 insert.o : insert.c defs.h buffer.h
593         cc -c insert.c
594 search.o : search.c defs.h buffer.h
595         cc -c search.c
596 files.o : files.c defs.h buffer.h command.h
597         cc -c files.c
598 utils.o : utils.c defs.h
599         cc -c utils.c
600 clean :
601         rm edit main.o kbd.o command.o display.o \
602            insert.o search.o files.o utils.o
603 @end group
604 @end example
606 @noindent
607 We split each long line into two lines using backslash-newline; this is
608 like using one long line, but is easier to read.
609 @cindex continuation lines
610 @cindex @code{\} (backslash), for continuation lines
611 @cindex backslash (@code{\}), for continuation lines
612 @cindex quoting newline, in makefile
613 @cindex newline, quoting, in makefile
615 To use this makefile to create the executable file called @file{edit},
616 type:
618 @example
619 make
620 @end example
622 To use this makefile to delete the executable file and all the object
623 files from the directory, type:
625 @example
626 make clean
627 @end example
629 In the example makefile, the targets include the executable file
630 @samp{edit}, and the object files @samp{main.o} and @samp{kbd.o}.  The
631 prerequisites are files such as @samp{main.c} and @samp{defs.h}.
632 In fact, each @samp{.o} file is both a target and a prerequisite.
633 Commands include @w{@samp{cc -c main.c}} and @w{@samp{cc -c kbd.c}}.
635 When a target is a file, it needs to be recompiled or relinked if any
636 of its prerequisites change.  In addition, any prerequisites that are
637 themselves automatically generated should be updated first.  In this
638 example, @file{edit} depends on each of the eight object files; the
639 object file @file{main.o} depends on the source file @file{main.c} and
640 on the header file @file{defs.h}.
642 A shell command follows each line that contains a target and
643 prerequisites.  These shell commands say how to update the target file.
644 A tab character must come at the beginning of every command line to
645 distinguish command lines from other lines in the makefile.  (Bear in
646 mind that @code{make} does not know anything about how the commands
647 work.  It is up to you to supply commands that will update the target
648 file properly.  All @code{make} does is execute the commands in the rule
649 you have specified when the target file needs to be updated.)
650 @cindex shell command
652 The target @samp{clean} is not a file, but merely the name of an
653 action.  Since you
654 normally
655 do not want to carry out the actions in this rule, @samp{clean} is not a prerequisite of any other rule.
656 Consequently, @code{make} never does anything with it unless you tell
657 it specifically.  Note that this rule not only is not a prerequisite, it
658 also does not have any prerequisites, so the only purpose of the rule
659 is to run the specified commands.  Targets that do not refer to files
660 but are just actions are called @dfn{phony targets}.  @xref{Phony
661 Targets}, for information about this kind of target.  @xref{Errors, ,
662 Errors in Commands}, to see how to cause @code{make} to ignore errors
663 from @code{rm} or any other command.
664 @cindex @code{clean} target
665 @cindex @code{rm} (shell command)
667 @node How Make Works, Variables Simplify, Simple Makefile, Introduction
668 @comment  node-name,  next,  previous,  up
669 @section How @code{make} Processes a Makefile
670 @cindex processing a makefile
671 @cindex makefile, how @code{make} processes
673 By default, @code{make} starts with the first target (not targets whose
674 names start with @samp{.}).  This is called the @dfn{default goal}.
675 (@dfn{Goals} are the targets that @code{make} strives ultimately to
676 update.    You can override this behavior using the command line
677 (@pxref{Goals, , Arguments to Specify the Goals}) or with the
678 @code{.DEFAULT_GOAL} special variable (@pxref{Special Variables, ,
679 Other Special Variables}).
680 @cindex default goal
681 @cindex goal, default
682 @cindex goal
684 In the simple example of the previous section, the default goal is to
685 update the executable program @file{edit}; therefore, we put that rule
686 first.
688 Thus, when you give the command:
690 @example
691 make
692 @end example
694 @noindent
695 @code{make} reads the makefile in the current directory and begins by
696 processing the first rule.  In the example, this rule is for relinking
697 @file{edit}; but before @code{make} can fully process this rule, it
698 must process the rules for the files that @file{edit} depends on,
699 which in this case are the object files.  Each of these files is
700 processed according to its own rule.  These rules say to update each
701 @samp{.o} file by compiling its source file.  The recompilation must
702 be done if the source file, or any of the header files named as
703 prerequisites, is more recent than the object file, or if the object
704 file does not exist.
706 The other rules are processed because their targets appear as
707 prerequisites of the goal.  If some other rule is not depended on by the
708 goal (or anything it depends on, etc.), that rule is not processed,
709 unless you tell @code{make} to do so (with a command such as
710 @w{@code{make clean}}).
712 Before recompiling an object file, @code{make} considers updating its
713 prerequisites, the source file and header files.  This makefile does not
714 specify anything to be done for them---the @samp{.c} and @samp{.h} files
715 are not the targets of any rules---so @code{make} does nothing for these
716 files.  But @code{make} would update automatically generated C programs,
717 such as those made by Bison or Yacc, by their own rules at this time.
719 After recompiling whichever object files need it, @code{make} decides
720 whether to relink @file{edit}.  This must be done if the file
721 @file{edit} does not exist, or if any of the object files are newer than
722 it.  If an object file was just recompiled, it is now newer than
723 @file{edit}, so @file{edit} is relinked.
724 @cindex relinking
726 Thus, if we change the file @file{insert.c} and run @code{make},
727 @code{make} will compile that file to update @file{insert.o}, and then
728 link @file{edit}.  If we change the file @file{command.h} and run
729 @code{make}, @code{make} will recompile the object files @file{kbd.o},
730 @file{command.o} and @file{files.o} and then link the file @file{edit}.
732 @node Variables Simplify, make Deduces, How Make Works, Introduction
733 @section Variables Make Makefiles Simpler
734 @cindex variables
735 @cindex simplifying with variables
737 In our example, we had to list all the object files twice in the rule for
738 @file{edit} (repeated here):
740 @example
741 @group
742 edit : main.o kbd.o command.o display.o \
743               insert.o search.o files.o utils.o
744         cc -o edit main.o kbd.o command.o display.o \
745                    insert.o search.o files.o utils.o
746 @end group
747 @end example
749 @cindex @code{objects}
750 Such duplication is error-prone; if a new object file is added to the
751 system, we might add it to one list and forget the other.  We can eliminate
752 the risk and simplify the makefile by using a variable.  @dfn{Variables}
753 allow a text string to be defined once and substituted in multiple places
754 later (@pxref{Using Variables, ,How to Use Variables}).
756 @cindex @code{OBJECTS}
757 @cindex @code{objs}
758 @cindex @code{OBJS}
759 @cindex @code{obj}
760 @cindex @code{OBJ}
761 It is standard practice for every makefile to have a variable named
762 @code{objects}, @code{OBJECTS}, @code{objs}, @code{OBJS}, @code{obj},
763 or @code{OBJ} which is a list of all object file names.  We would
764 define such a variable @code{objects} with a line like this in the
765 makefile:@refill
767 @example
768 @group
769 objects = main.o kbd.o command.o display.o \
770           insert.o search.o files.o utils.o
771 @end group
772 @end example
774 @noindent
775 Then, each place we want to put a list of the object file names, we can
776 substitute the variable's value by writing @samp{$(objects)}
777 (@pxref{Using Variables, ,How to Use Variables}).
779 Here is how the complete simple makefile looks when you use a variable
780 for the object files:
782 @example
783 @group
784 objects = main.o kbd.o command.o display.o \
785           insert.o search.o files.o utils.o
787 edit : $(objects)
788         cc -o edit $(objects)
789 main.o : main.c defs.h
790         cc -c main.c
791 kbd.o : kbd.c defs.h command.h
792         cc -c kbd.c
793 command.o : command.c defs.h command.h
794         cc -c command.c
795 display.o : display.c defs.h buffer.h
796         cc -c display.c
797 insert.o : insert.c defs.h buffer.h
798         cc -c insert.c
799 search.o : search.c defs.h buffer.h
800         cc -c search.c
801 files.o : files.c defs.h buffer.h command.h
802         cc -c files.c
803 utils.o : utils.c defs.h
804         cc -c utils.c
805 clean :
806         rm edit $(objects)
807 @end group
808 @end example
810 @node make Deduces, Combine By Prerequisite, Variables Simplify, Introduction
811 @section Letting @code{make} Deduce the Commands
812 @cindex deducing commands (implicit rules)
813 @cindex implicit rule, introduction to
814 @cindex rule, implicit, introduction to
816 It is not necessary to spell out the commands for compiling the individual
817 C source files, because @code{make} can figure them out: it has an
818 @dfn{implicit rule} for updating a @samp{.o} file from a correspondingly
819 named @samp{.c} file using a @samp{cc -c} command.  For example, it will
820 use the command @samp{cc -c main.c -o main.o} to compile @file{main.c} into
821 @file{main.o}.  We can therefore omit the commands from the rules for the
822 object files.  @xref{Implicit Rules, ,Using Implicit Rules}.@refill
824 When a @samp{.c} file is used automatically in this way, it is also
825 automatically added to the list of prerequisites.  We can therefore omit
826 the @samp{.c} files from the prerequisites, provided we omit the commands.
828 Here is the entire example, with both of these changes, and a variable
829 @code{objects} as suggested above:
831 @example
832 @group
833 objects = main.o kbd.o command.o display.o \
834           insert.o search.o files.o utils.o
836 edit : $(objects)
837         cc -o edit $(objects)
839 main.o : defs.h
840 kbd.o : defs.h command.h
841 command.o : defs.h command.h
842 display.o : defs.h buffer.h
843 insert.o : defs.h buffer.h
844 search.o : defs.h buffer.h
845 files.o : defs.h buffer.h command.h
846 utils.o : defs.h
848 .PHONY : clean
849 clean :
850         rm edit $(objects)
851 @end group
852 @end example
854 @noindent
855 This is how we would write the makefile in actual practice.  (The
856 complications associated with @samp{clean} are described elsewhere.
857 See @ref{Phony Targets}, and @ref{Errors, ,Errors in Commands}.)
859 Because implicit rules are so convenient, they are important.  You
860 will see them used frequently.@refill
862 @node Combine By Prerequisite, Cleanup, make Deduces, Introduction
863 @section Another Style of Makefile
864 @cindex combining rules by prerequisite
866 When the objects of a makefile are created only by implicit rules, an
867 alternative style of makefile is possible.  In this style of makefile,
868 you group entries by their prerequisites instead of by their targets.
869 Here is what one looks like:
871 @example
872 @group
873 objects = main.o kbd.o command.o display.o \
874           insert.o search.o files.o utils.o
876 edit : $(objects)
877         cc -o edit $(objects)
879 $(objects) : defs.h
880 kbd.o command.o files.o : command.h
881 display.o insert.o search.o files.o : buffer.h
882 @end group
883 @end example
885 @noindent
886 Here @file{defs.h} is given as a prerequisite of all the object files;
887 @file{command.h} and @file{buffer.h} are prerequisites of the specific
888 object files listed for them.
890 Whether this is better is a matter of taste: it is more compact, but some
891 people dislike it because they find it clearer to put all the information
892 about each target in one place.
894 @node Cleanup,  , Combine By Prerequisite, Introduction
895 @section Rules for Cleaning the Directory
896 @cindex cleaning up
897 @cindex removing, to clean up
899 Compiling a program is not the only thing you might want to write rules
900 for.  Makefiles commonly tell how to do a few other things besides
901 compiling a program: for example, how to delete all the object files
902 and executables so that the directory is @samp{clean}.
904 @cindex @code{clean} target
905 Here is how we
906 could write a @code{make} rule for cleaning our example editor:
908 @example
909 @group
910 clean:
911         rm edit $(objects)
912 @end group
913 @end example
915 In practice, we might want to write the rule in a somewhat more
916 complicated manner to handle unanticipated situations.  We would do this:
918 @example
919 @group
920 .PHONY : clean
921 clean :
922         -rm edit $(objects)
923 @end group
924 @end example
926 @noindent
927 This prevents @code{make} from getting confused by an actual file
928 called @file{clean} and causes it to continue in spite of errors from
929 @code{rm}.  (See @ref{Phony Targets}, and @ref{Errors, ,Errors in
930 Commands}.)
932 @noindent
933 A rule such as this should not be placed at the beginning of the
934 makefile, because we do not want it to run by default!  Thus, in the
935 example makefile, we want the rule for @code{edit}, which recompiles
936 the editor, to remain the default goal.
938 Since @code{clean} is not a prerequisite of @code{edit}, this rule will not
939 run at all if we give the command @samp{make} with no arguments.  In
940 order to make the rule run, we have to type @samp{make clean}.
941 @xref{Running, ,How to Run @code{make}}.
943 @node Makefiles, Rules, Introduction, Top
944 @chapter Writing Makefiles
946 @cindex makefile, how to write
947 The information that tells @code{make} how to recompile a system comes from
948 reading a data base called the @dfn{makefile}.
950 @menu
951 * Makefile Contents::           What makefiles contain.
952 * Makefile Names::              How to name your makefile.
953 * Include::                     How one makefile can use another makefile.
954 * MAKEFILES Variable::          The environment can specify extra makefiles.
955 * MAKEFILE_LIST Variable::      Discover which makefiles have been read.
956 * Special Variables::           Other special variables.
957 * Remaking Makefiles::          How makefiles get remade.
958 * Overriding Makefiles::        How to override part of one makefile
959                                   with another makefile.
960 * Reading Makefiles::           How makefiles are parsed.
961 * Secondary Expansion::         How and when secondary expansion is performed.
962 @end menu
964 @node Makefile Contents, Makefile Names, Makefiles, Makefiles
965 @section What Makefiles Contain
967 Makefiles contain five kinds of things: @dfn{explicit rules},
968 @dfn{implicit rules}, @dfn{variable definitions}, @dfn{directives},
969 and @dfn{comments}.  Rules, variables, and directives are described at
970 length in later chapters.@refill
972 @itemize @bullet
973 @cindex rule, explicit, definition of
974 @cindex explicit rule, definition of
975 @item
976 An @dfn{explicit rule} says when and how to remake one or more files,
977 called the rule's @dfn{targets}.  It lists the other files that the
978 targets depend on, called the @dfn{prerequisites} of the target, and
979 may also give commands to use to create or update the targets.
980 @xref{Rules, ,Writing Rules}.
982 @cindex rule, implicit, definition of
983 @cindex implicit rule, definition of
984 @item
985 An @dfn{implicit rule} says when and how to remake a class of files
986 based on their names.  It describes how a target may depend on a file
987 with a name similar to the target and gives commands to create or
988 update such a target.  @xref{Implicit Rules, ,Using Implicit Rules}.
990 @cindex variable definition
991 @item
992 A @dfn{variable definition} is a line that specifies a text string
993 value for a variable that can be substituted into the text later.  The
994 simple makefile example shows a variable definition for @code{objects}
995 as a list of all object files (@pxref{Variables Simplify, , Variables
996 Make Makefiles Simpler}).
998 @cindex directive
999 @item
1000 A @dfn{directive} is a command for @code{make} to do something special while
1001 reading the makefile.  These include:
1003 @itemize @bullet
1004 @item
1005 Reading another makefile (@pxref{Include, ,Including Other Makefiles}).
1007 @item
1008 Deciding (based on the values of variables) whether to use or
1009 ignore a part of the makefile (@pxref{Conditionals, ,Conditional Parts of Makefiles}).
1011 @item
1012 Defining a variable from a verbatim string containing multiple lines
1013 (@pxref{Defining, ,Defining Variables Verbatim}).
1014 @end itemize
1016 @cindex comments, in makefile
1017 @cindex @code{#} (comments), in makefile
1018 @item
1019 @samp{#} in a line of a makefile starts a @dfn{comment}.  It and the
1020 rest of the line are ignored, except that a trailing backslash not
1021 escaped by another backslash will continue the comment across multiple
1022 lines.  A line containing just a comment (with perhaps spaces before
1023 it) is effectively blank, and is ignored.  If you want a literal
1024 @code{#}, escape it with a backslash (e.g., @code{\#}).  Comments may
1025 appear on any line in the makefile, although they are treated
1026 specially in certain situations.
1028 Within a command script (if the line begins with a TAB character) the
1029 entire line is passed to the shell, just as with any other line that
1030 begins with a TAB.  The shell decides how to interpret the text:
1031 whether or not this is a comment is up to the shell.
1033 Within a @code{define} directive, comments are not ignored during the
1034 definition of the variable, but rather kept intact in the value of the
1035 variable.  When the variable is expanded they will either be treated
1036 as @code{make} comments or as command script text, depending on the
1037 context in which the variable is evaluated.
1038 @end itemize
1040 @node Makefile Names, Include, Makefile Contents, Makefiles
1041 @section What Name to Give Your Makefile
1042 @cindex makefile name
1043 @cindex name of makefile
1044 @cindex default makefile name
1045 @cindex file name of makefile
1047 @c following paragraph rewritten to avoid overfull hbox
1048 By default, when @code{make} looks for the makefile, it tries the
1049 following names, in order: @file{GNUmakefile}, @file{makefile}
1050 and @file{Makefile}.@refill
1051 @findex Makefile
1052 @findex GNUmakefile
1053 @findex makefile
1055 @cindex @code{README}
1056 Normally you should call your makefile either @file{makefile} or
1057 @file{Makefile}.  (We recommend @file{Makefile} because it appears
1058 prominently near the beginning of a directory listing, right near other
1059 important files such as @file{README}.)  The first name checked,
1060 @file{GNUmakefile}, is not recommended for most makefiles.  You should
1061 use this name if you have a makefile that is specific to GNU
1062 @code{make}, and will not be understood by other versions of
1063 @code{make}.  Other @code{make} programs look for @file{makefile} and
1064 @file{Makefile}, but not @file{GNUmakefile}.
1066 If @code{make} finds none of these names, it does not use any makefile.
1067 Then you must specify a goal with a command argument, and @code{make}
1068 will attempt to figure out how to remake it using only its built-in
1069 implicit rules.  @xref{Implicit Rules, ,Using Implicit Rules}.
1071 @cindex @code{-f}
1072 @cindex @code{--file}
1073 @cindex @code{--makefile}
1074 If you want to use a nonstandard name for your makefile, you can specify
1075 the makefile name with the @samp{-f} or @samp{--file} option.  The
1076 arguments @w{@samp{-f @var{name}}} or @w{@samp{--file=@var{name}}} tell
1077 @code{make} to read the file @var{name} as the makefile.  If you use
1078 more than one @samp{-f} or @samp{--file} option, you can specify several
1079 makefiles.  All the makefiles are effectively concatenated in the order
1080 specified.  The default makefile names @file{GNUmakefile},
1081 @file{makefile} and @file{Makefile} are not checked automatically if you
1082 specify @samp{-f} or @samp{--file}.@refill
1083 @cindex specifying makefile name
1084 @cindex makefile name, how to specify
1085 @cindex name of makefile, how to specify
1086 @cindex file name of makefile, how to specify
1088 @node Include, MAKEFILES Variable, Makefile Names, Makefiles
1089 @section Including Other Makefiles
1090 @cindex including other makefiles
1091 @cindex makefile, including
1093 @findex include
1094 The @code{include} directive tells @code{make} to suspend reading the
1095 current makefile and read one or more other makefiles before continuing.
1096 The directive is a line in the makefile that looks like this:
1098 @example
1099 include @var{filenames}@dots{}
1100 @end example
1102 @noindent
1103 @var{filenames} can contain shell file name patterns.  If
1104 @var{filenames} is empty, nothing is included and no error is printed.
1105 @cindex shell file name pattern (in @code{include})
1106 @cindex shell wildcards (in @code{include})
1107 @cindex wildcard, in @code{include}
1109 Extra spaces are allowed and ignored at the beginning of the line, but
1110 a tab is not allowed.  (If the line begins with a tab, it will be
1111 considered a command line.)  Whitespace is required between
1112 @code{include} and the file names, and between file names; extra
1113 whitespace is ignored there and at the end of the directive.  A
1114 comment starting with @samp{#} is allowed at the end of the line.  If
1115 the file names contain any variable or function references, they are
1116 expanded.  @xref{Using Variables, ,How to Use Variables}.
1118 For example, if you have three @file{.mk} files, @file{a.mk},
1119 @file{b.mk}, and @file{c.mk}, and @code{$(bar)} expands to
1120 @code{bish bash}, then the following expression
1122 @example
1123 include foo *.mk $(bar)
1124 @end example
1126 is equivalent to
1128 @example
1129 include foo a.mk b.mk c.mk bish bash
1130 @end example
1132 When @code{make} processes an @code{include} directive, it suspends
1133 reading of the containing makefile and reads from each listed file in
1134 turn.  When that is finished, @code{make} resumes reading the
1135 makefile in which the directive appears.
1137 One occasion for using @code{include} directives is when several programs,
1138 handled by individual makefiles in various directories, need to use a
1139 common set of variable definitions
1140 (@pxref{Setting, ,Setting Variables}) or pattern rules
1141 (@pxref{Pattern Rules, ,Defining and Redefining Pattern Rules}).
1143 Another such occasion is when you want to generate prerequisites from
1144 source files automatically; the prerequisites can be put in a file that
1145 is included by the main makefile.  This practice is generally cleaner
1146 than that of somehow appending the prerequisites to the end of the main
1147 makefile as has been traditionally done with other versions of
1148 @code{make}.  @xref{Automatic Prerequisites}.
1149 @cindex prerequisites, automatic generation
1150 @cindex automatic generation of prerequisites
1151 @cindex generating prerequisites automatically
1153 @cindex @code{-I}
1154 @cindex @code{--include-dir}
1155 @cindex included makefiles, default directories
1156 @cindex default directories for included makefiles
1157 @findex /usr/gnu/include
1158 @findex /usr/local/include
1159 @findex /usr/include
1160 If the specified name does not start with a slash, and the file is not
1161 found in the current directory, several other directories are searched.
1162 First, any directories you have specified with the @samp{-I} or
1163 @samp{--include-dir} option are searched
1164 (@pxref{Options Summary, ,Summary of Options}).
1165 Then the following directories (if they exist)
1166 are searched, in this order:
1167 @file{@var{prefix}/include} (normally @file{/usr/local/include}
1168 @footnote{GNU Make compiled for MS-DOS and MS-Windows behaves as if
1169 @var{prefix} has been defined to be the root of the DJGPP tree
1170 hierarchy.})
1171 @file{/usr/gnu/include},
1172 @file{/usr/local/include}, @file{/usr/include}.
1174 If an included makefile cannot be found in any of these directories, a
1175 warning message is generated, but it is not an immediately fatal error;
1176 processing of the makefile containing the @code{include} continues.
1177 Once it has finished reading makefiles, @code{make} will try to remake
1178 any that are out of date or don't exist.
1179 @xref{Remaking Makefiles, ,How Makefiles Are Remade}.
1180 Only after it has tried to find a way to remake a makefile and failed,
1181 will @code{make} diagnose the missing makefile as a fatal error.
1183 If you want @code{make} to simply ignore a makefile which does not exist
1184 and cannot be remade, with no error message, use the @w{@code{-include}}
1185 directive instead of @code{include}, like this:
1187 @example
1188 -include @var{filenames}@dots{}
1189 @end example
1191 This acts like @code{include} in every way except that there is no
1192 error (not even a warning) if any of the @var{filenames} do not exist.
1193 For compatibility with some other @code{make} implementations,
1194 @code{sinclude} is another name for @w{@code{-include}}.
1196 @node MAKEFILES Variable, MAKEFILE_LIST Variable, Include, Makefiles
1197 @section The Variable @code{MAKEFILES}
1198 @cindex makefile, and @code{MAKEFILES} variable
1199 @cindex including (@code{MAKEFILES} variable)
1201 @vindex MAKEFILES
1202 If the environment variable @code{MAKEFILES} is defined, @code{make}
1203 considers its value as a list of names (separated by whitespace) of
1204 additional makefiles to be read before the others.  This works much like
1205 the @code{include} directive: various directories are searched for those
1206 files (@pxref{Include, ,Including Other Makefiles}).  In addition, the
1207 default goal is never taken from one of these makefiles and it is not an
1208 error if the files listed in @code{MAKEFILES} are not found.@refill
1210 @cindex recursion, and @code{MAKEFILES} variable
1211 The main use of @code{MAKEFILES} is in communication between recursive
1212 invocations of @code{make} (@pxref{Recursion, ,Recursive Use of
1213 @code{make}}).  It usually is not desirable to set the environment
1214 variable before a top-level invocation of @code{make}, because it is
1215 usually better not to mess with a makefile from outside.  However, if
1216 you are running @code{make} without a specific makefile, a makefile in
1217 @code{MAKEFILES} can do useful things to help the built-in implicit
1218 rules work better, such as defining search paths (@pxref{Directory Search}).
1220 Some users are tempted to set @code{MAKEFILES} in the environment
1221 automatically on login, and program makefiles to expect this to be done.
1222 This is a very bad idea, because such makefiles will fail to work if run by
1223 anyone else.  It is much better to write explicit @code{include} directives
1224 in the makefiles.  @xref{Include, , Including Other Makefiles}.
1226 @node MAKEFILE_LIST Variable, Special Variables, MAKEFILES Variable, Makefiles
1227 @comment  node-name,  next,  previous,  up
1228 @section The Variable @code{MAKEFILE_LIST}
1229 @cindex makefiles, and @code{MAKEFILE_LIST} variable
1230 @cindex including (@code{MAKEFILE_LIST} variable)
1231 @vindex MAKEFILE_LIST
1233 As @code{make} reads various makefiles, including any obtained from the
1234 @code{MAKEFILES} variable, the command line, the default files, or
1235 from @code{include} directives, their names will be automatically
1236 appended to the @code{MAKEFILE_LIST} variable.  They are added right
1237 before @code{make} begins to parse them.
1239 This means that if the first thing a makefile does is examine the last
1240 word in this variable, it will be the name of the current makefile.
1241 Once the current makefile has used @code{include}, however, the last
1242 word will be the just-included makefile.
1244 If a makefile named @code{Makefile} has this content:
1246 @example
1247 @group
1248 name1 := $(lastword $(MAKEFILE_LIST))
1250 include inc.mk
1252 name2 := $(lastword $(MAKEFILE_LIST))
1254 all:
1255         @@echo name1 = $(name1)
1256         @@echo name2 = $(name2)
1257 @end group
1258 @end example
1260 @noindent
1261 then you would expect to see this output:
1263 @example
1264 @group
1265 name1 = Makefile
1266 name2 = inc.mk
1267 @end group
1268 @end example
1270 @xref{Text Functions}, for more information on the @code{word} and
1271 @code{words} functions used above.  @xref{Flavors, The Two Flavors of
1272 Variables}, for more information on simply-expanded (@code{:=})
1273 variable definitions.
1275 @node Special Variables, Remaking Makefiles, MAKEFILE_LIST Variable, Makefiles
1276 @comment  node-name,  next,  previous,  up
1277 @section Other Special Variables
1278 @cindex makefiles, and special variables
1279 @cindex special variables
1281 GNU @code{make} also supports other special variables.  Unless
1282 otherwise documented here, these values lose their special properties
1283 if they are set by a makefile or on the command line.
1285 @table @code
1287 @vindex .DEFAULT_GOAL @r{(define default goal)}
1288 @item .DEFAULT_GOAL
1289 Sets the default goal to be used if no targets were specified on the
1290 command line (@pxref{Goals, , Arguments to Specify the Goals}).  The
1291 @code{.DEFAULT_GOAL} variable allows you to discover the current
1292 default goal, restart the default goal selection algorithm by clearing
1293 its value, or to explicitly set the default goal.  The following
1294 example illustrates these cases:
1296 @example
1297 @group
1298 # Query the default goal.
1299 ifeq ($(.DEFAULT_GOAL),)
1300   $(warning no default goal is set)
1301 endif
1303 .PHONY: foo
1304 foo: ; @@echo $@@
1306 $(warning default goal is $(.DEFAULT_GOAL))
1308 # Reset the default goal.
1309 .DEFAULT_GOAL :=
1311 .PHONY: bar
1312 bar: ; @@echo $@@
1314 $(warning default goal is $(.DEFAULT_GOAL))
1316 # Set our own.
1317 .DEFAULT_GOAL := foo
1318 @end group
1319 @end example
1321 This makefile prints:
1323 @example
1324 @group
1325 no default goal is set
1326 default goal is foo
1327 default goal is bar
1329 @end group
1330 @end example
1332 Note that assigning more than one target name to @code{.DEFAULT_GOAL} is
1333 illegal and will result in an error.
1335 @vindex MAKE_RESTARTS @r{(number of times @code{make} has restarted)}
1336 @item MAKE_RESTARTS
1337 This variable is set only if this instance of @code{make} has
1338 restarted (@pxref{Remaking Makefiles, , How Makefiles Are Remade}): it
1339 will contain the number of times this instance has restarted.  Note
1340 this is not the same as recursion (counted by the @code{MAKELEVEL}
1341 variable).  You should not set, modify, or export this variable.
1343 @vindex .VARIABLES @r{(list of variables)}
1344 @item .VARIABLES
1345 Expands to a list of the @emph{names} of all global variables defined
1346 so far.  This includes variables which have empty values, as well as
1347 built-in variables (@pxref{Implicit Variables, , Variables Used by
1348 Implicit Rules}), but does not include any variables which are only
1349 defined in a target-specific context.  Note that any value you assign
1350 to this variable will be ignored; it will always return its special
1351 value.
1353 @c @vindex .TARGETS @r{(list of targets)}
1354 @c @item .TARGETS
1355 @c The second special variable is @code{.TARGETS}.  When expanded, the
1356 @c value consists of a list of all targets defined in all makefiles read
1357 @c up until that point.  Note it's not enough for a file to be simply
1358 @c mentioned in the makefile to be listed in this variable, even if it
1359 @c would match an implicit rule and become an ``implicit target''.  The
1360 @c file must appear as a target, on the left-hand side of a ``:'', to be
1361 @c considered a target for the purposes of this variable.
1363 @vindex .FEATURES @r{(list of supported features)}
1364 @item .FEATURES
1365 Expands to a list of special features supported by this version of
1366 @code{make}.  Possible values include:
1368 @table @samp
1369 @item target-specific
1370 Supports target-specific and pattern-specific variable assignments.
1371 @xref{Target-specific, ,Target-specific Variable Values}.
1373 @item order-only
1374 Supports order-only prerequisites.  @xref{Prerequisite Types, ,Types
1375 of Prerequisites}.
1377 @item second-expansion
1378 Supports secondary expansion of prerequisite lists.
1380 @item jobserver
1381 Supports ``job server'' enhanced parallel builds.  @xref{Parallel,
1382 ,Parallel Execution}.
1384 @item else-if
1385 Supports ``else if'' non-nested conditionals.  @xref{Conditional
1386 Syntax, ,Syntax of Conditionals}.
1388 @item check-symlink
1389 Supports the @code{-L} (@code{--check-symlink-times}) flag.
1390 @xref{Options Summary, ,Summary of Options}.
1392 @end table
1394 @vindex .INCLUDE_DIRS @r{(list of include directories)}
1395 @item .INCLUDE_DIRS
1396 Expands to a list of directories that @code{make} searches for
1397 included makefiles (@pxref{Include, , Including Other Makefiles}).
1399 @end table
1401 @node Remaking Makefiles, Overriding Makefiles, Special Variables, Makefiles
1402 @section How Makefiles Are Remade
1404 @cindex updating makefiles
1405 @cindex remaking makefiles
1406 @cindex makefile, remaking of
1407 Sometimes makefiles can be remade from other files, such as RCS or SCCS
1408 files.  If a makefile can be remade from other files, you probably want
1409 @code{make} to get an up-to-date version of the makefile to read in.
1411 To this end, after reading in all makefiles, @code{make} will consider
1412 each as a goal target and attempt to update it.  If a makefile has a
1413 rule which says how to update it (found either in that very makefile or
1414 in another one) or if an implicit rule applies to it (@pxref{Implicit
1415 Rules, ,Using Implicit Rules}), it will be updated if necessary.  After
1416 all makefiles have been checked, if any have actually been changed,
1417 @code{make} starts with a clean slate and reads all the makefiles over
1418 again.  (It will also attempt to update each of them over again, but
1419 normally this will not change them again, since they are already up to
1420 date.)@refill
1422 If you know that one or more of your makefiles cannot be remade and you
1423 want to keep @code{make} from performing an implicit rule search on
1424 them, perhaps for efficiency reasons, you can use any normal method of
1425 preventing implicit rule lookup to do so.  For example, you can write an
1426 explicit rule with the makefile as the target, and an empty command
1427 string (@pxref{Empty Commands, ,Using Empty Commands}).
1429 If the makefiles specify a double-colon rule to remake a file with
1430 commands but no prerequisites, that file will always be remade
1431 (@pxref{Double-Colon}).  In the case of makefiles, a makefile that has a
1432 double-colon rule with commands but no prerequisites will be remade every
1433 time @code{make} is run, and then again after @code{make} starts over
1434 and reads the makefiles in again.  This would cause an infinite loop:
1435 @code{make} would constantly remake the makefile, and never do anything
1436 else.  So, to avoid this, @code{make} will @strong{not} attempt to
1437 remake makefiles which are specified as targets of a double-colon rule
1438 with commands but no prerequisites.@refill
1440 If you do not specify any makefiles to be read with @samp{-f} or
1441 @samp{--file} options, @code{make} will try the default makefile names;
1442 @pxref{Makefile Names, ,What Name to Give Your Makefile}.  Unlike
1443 makefiles explicitly requested with @samp{-f} or @samp{--file} options,
1444 @code{make} is not certain that these makefiles should exist.  However,
1445 if a default makefile does not exist but can be created by running
1446 @code{make} rules, you probably want the rules to be run so that the
1447 makefile can be used.
1449 Therefore, if none of the default makefiles exists, @code{make} will try
1450 to make each of them in the same order in which they are searched for
1451 (@pxref{Makefile Names, ,What Name to Give Your Makefile})
1452 until it succeeds in making one, or it runs out of names to try.  Note
1453 that it is not an error if @code{make} cannot find or make any makefile;
1454 a makefile is not always necessary.@refill
1456 When you use the @samp{-t} or @samp{--touch} option
1457 (@pxref{Instead of Execution, ,Instead of Executing the Commands}),
1458 you would not want to use an out-of-date makefile to decide which
1459 targets to touch.  So the @samp{-t} option has no effect on updating
1460 makefiles; they are really updated even if @samp{-t} is specified.
1461 Likewise, @samp{-q} (or @samp{--question}) and @samp{-n} (or
1462 @samp{--just-print}) do not prevent updating of makefiles, because an
1463 out-of-date makefile would result in the wrong output for other targets.
1464 Thus, @samp{make -f mfile -n foo} will update @file{mfile}, read it in,
1465 and then print the commands to update @file{foo} and its prerequisites
1466 without running them.  The commands printed for @file{foo} will be those
1467 specified in the updated contents of @file{mfile}.
1469 However, on occasion you might actually wish to prevent updating of even
1470 the makefiles.  You can do this by specifying the makefiles as goals in
1471 the command line as well as specifying them as makefiles.  When the
1472 makefile name is specified explicitly as a goal, the options @samp{-t}
1473 and so on do apply to them.
1475 Thus, @samp{make -f mfile -n mfile foo} would read the makefile
1476 @file{mfile}, print the commands needed to update it without actually
1477 running them, and then print the commands needed to update @file{foo}
1478 without running them.  The commands for @file{foo} will be those
1479 specified by the existing contents of @file{mfile}.
1481 @node Overriding Makefiles, Reading Makefiles, Remaking Makefiles, Makefiles
1482 @section Overriding Part of Another Makefile
1484 @cindex overriding makefiles
1485 @cindex makefile, overriding
1486 Sometimes it is useful to have a makefile that is mostly just like
1487 another makefile.  You can often use the @samp{include} directive to
1488 include one in the other, and add more targets or variable definitions.
1489 However, if the two makefiles give different commands for the same
1490 target, @code{make} will not let you just do this.  But there is another way.
1492 @cindex match-anything rule, used to override
1493 In the containing makefile (the one that wants to include the other),
1494 you can use a match-anything pattern rule to say that to remake any
1495 target that cannot be made from the information in the containing
1496 makefile, @code{make} should look in another makefile.
1497 @xref{Pattern Rules}, for more information on pattern rules.
1499 For example, if you have a makefile called @file{Makefile} that says how
1500 to make the target @samp{foo} (and other targets), you can write a
1501 makefile called @file{GNUmakefile} that contains:
1503 @example
1504 foo:
1505         frobnicate > foo
1507 %: force
1508         @@$(MAKE) -f Makefile $@@
1509 force: ;
1510 @end example
1512 If you say @samp{make foo}, @code{make} will find @file{GNUmakefile},
1513 read it, and see that to make @file{foo}, it needs to run the command
1514 @samp{frobnicate > foo}.  If you say @samp{make bar}, @code{make} will
1515 find no way to make @file{bar} in @file{GNUmakefile}, so it will use the
1516 commands from the pattern rule: @samp{make -f Makefile bar}.  If
1517 @file{Makefile} provides a rule for updating @file{bar}, @code{make}
1518 will apply the rule.  And likewise for any other target that
1519 @file{GNUmakefile} does not say how to make.
1521 The way this works is that the pattern rule has a pattern of just
1522 @samp{%}, so it matches any target whatever.  The rule specifies a
1523 prerequisite @file{force}, to guarantee that the commands will be run even
1524 if the target file already exists.  We give @file{force} target empty
1525 commands to prevent @code{make} from searching for an implicit rule to
1526 build it---otherwise it would apply the same match-anything rule to
1527 @file{force} itself and create a prerequisite loop!
1529 @node Reading Makefiles,  Secondary Expansion, Overriding Makefiles, Makefiles
1530 @section How @code{make} Reads a Makefile
1531 @cindex reading makefiles
1532 @cindex makefile, parsing
1534 GNU @code{make} does its work in two distinct phases.  During the first
1535 phase it reads all the makefiles, included makefiles, etc. and
1536 internalizes all the variables and their values, implicit and explicit
1537 rules, and constructs a dependency graph of all the targets and their
1538 prerequisites.  During the second phase, @code{make} uses these internal
1539 structures to determine what targets will need to be rebuilt and to
1540 invoke the rules necessary to do so.
1542 It's important to understand this two-phase approach because it has a
1543 direct impact on how variable and function expansion happens; this is
1544 often a source of some confusion when writing makefiles.  Here we will
1545 present a summary of the phases in which expansion happens for different
1546 constructs within the makefile.  We say that expansion is
1547 @dfn{immediate} if it happens during the first phase: in this case
1548 @code{make} will expand any variables or functions in that section of a
1549 construct as the makefile is parsed.  We say that expansion is
1550 @dfn{deferred} if expansion is not performed immediately.  Expansion of
1551 deferred construct is not performed until either the construct appears
1552 later in an immediate context, or until the second phase.
1554 You may not be familiar with some of these constructs yet.  You can
1555 reference this section as you become familiar with them, in later
1556 chapters.
1558 @subheading Variable Assignment
1559 @cindex +=, expansion
1560 @cindex =, expansion
1561 @cindex ?=, expansion
1562 @cindex +=, expansion
1563 @cindex define, expansion
1565 Variable definitions are parsed as follows:
1567 @example
1568 @var{immediate} = @var{deferred}
1569 @var{immediate} ?= @var{deferred}
1570 @var{immediate} := @var{immediate}
1571 @var{immediate} += @var{deferred} or @var{immediate}
1573 define @var{immediate}
1574   @var{deferred}
1575 endef
1576 @end example
1578 For the append operator, @samp{+=}, the right-hand side is considered
1579 immediate if the variable was previously set as a simple variable
1580 (@samp{:=}), and deferred otherwise.
1582 @subheading Conditional Statements
1583 @cindex ifdef, expansion
1584 @cindex ifeq, expansion
1585 @cindex ifndef, expansion
1586 @cindex ifneq, expansion
1588 All instances of conditional syntax are parsed immediately, in their
1589 entirety; this includes the @code{ifdef}, @code{ifeq}, @code{ifndef},
1590 and @code{ifneq} forms.  Of course this means that automatic variables
1591 cannot be used in conditional statements, as automatic variables are
1592 not set until the command script for that rule is invoked.  If you
1593 need to use automatic variables in a conditional you @emph{must} use
1594 shell conditional syntax, in your command script proper, for these
1595 tests, not @code{make} conditionals.
1597 @subheading Rule Definition
1598 @cindex target, expansion
1599 @cindex prerequisite, expansion
1600 @cindex implicit rule, expansion
1601 @cindex pattern rule, expansion
1602 @cindex explicit rule, expansion
1604 A rule is always expanded the same way, regardless of the form:
1606 @example
1607 @var{immediate} : @var{immediate} ; @var{deferred}
1608         @var{deferred}
1609 @end example
1611 That is, the target and prerequisite sections are expanded immediately,
1612 and the commands used to construct the target are always deferred.  This
1613 general rule is true for explicit rules, pattern rules, suffix rules,
1614 static pattern rules, and simple prerequisite definitions.
1616 @node Secondary Expansion, , Reading Makefiles, Makefiles
1617 @section Secondary Expansion
1618 @cindex secondary expansion
1619 @cindex expansion, secondary
1621 @findex .SECONDEXPANSION
1622 In the previous section we learned that GNU @code{make} works in two
1623 distinct phases: a read-in phase and a target-update phase
1624 (@pxref{Reading Makefiles, , How @code{make} Reads a Makefile}).  GNU
1625 make also has the ability to enable a @emph{second expansion} of the
1626 prerequisites (only) for some or all targets defined in the makefile.
1627 In order for this second expansion to occur, the special target
1628 @code{.SECONDEXPANSION} must be defined before the first prerequisite
1629 list that makes use of this feature.
1631 If that special target is defined then in between the two phases
1632 mentioned above, right at the end of the read-in phase, all the
1633 prerequisites of the targets defined after the special target are
1634 expanded a @emph{second time}.  In most circumstances this secondary
1635 expansion will have no effect, since all variable and function
1636 references will have been expanded during the initial parsing of the
1637 makefiles.  In order to take advantage of the secondary expansion
1638 phase of the parser, then, it's necessary to @emph{escape} the
1639 variable or function reference in the makefile.  In this case the
1640 first expansion merely un-escapes the reference but doesn't expand it,
1641 and expansion is left to the secondary expansion phase.  For example,
1642 consider this makefile:
1644 @example
1645 .SECONDEXPANSION:
1646 ONEVAR = onefile
1647 TWOVAR = twofile
1648 myfile: $(ONEVAR) $$(TWOVAR)
1649 @end example
1651 After the first expansion phase the prerequisites list of the
1652 @file{myfile} target will be @code{onefile} and @code{$(TWOVAR)}; the
1653 first (unescaped) variable reference to @var{ONEVAR} is expanded,
1654 while the second (escaped) variable reference is simply unescaped,
1655 without being recognized as a variable reference.  Now during the
1656 secondary expansion the first word is expanded again but since it
1657 contains no variable or function references it remains the static
1658 value @file{onefile}, while the second word is now a normal reference
1659 to the variable @var{TWOVAR}, which is expanded to the value
1660 @file{twofile}.  The final result is that there are two prerequisites,
1661 @file{onefile} and @file{twofile}.
1663 Obviously, this is not a very interesting case since the same result
1664 could more easily have been achieved simply by having both variables
1665 appear, unescaped, in the prerequisites list.  One difference becomes
1666 apparent if the variables are reset; consider this example:
1668 @example
1669 .SECONDEXPANSION:
1670 AVAR = top
1671 onefile: $(AVAR)
1672 twofile: $$(AVAR)
1673 AVAR = bottom
1674 @end example
1676 Here the prerequisite of @file{onefile} will be expanded immediately,
1677 and resolve to the value @file{top}, while the prerequisite of
1678 @file{twofile} will not be full expanded until the secondary expansion
1679 and yield a value of @file{bottom}.
1681 This is marginally more exciting, but the true power of this feature
1682 only becomes apparent when you discover that secondary expansions
1683 always take place within the scope of the automatic variables for that
1684 target.  This means that you can use variables such as @code{$@@},
1685 @code{$*}, etc. during the second expansion and they will have their
1686 expected values, just as in the command script.  All you have to do is
1687 defer the expansion by escaping the @code{$}.  Also, secondary
1688 expansion occurs for both explicit and implicit (pattern) rules.
1689 Knowing this, the possible uses for this feature increase
1690 dramatically.  For example:
1692 @example
1693 .SECONDEXPANSION:
1694 main_OBJS := main.o try.o test.o
1695 lib_OBJS := lib.o api.o
1697 main lib: $$($$@@_OBJS)
1698 @end example
1700 Here, after the initial expansion the prerequisites of both the
1701 @file{main} and @file{lib} targets will be @code{$($@@_OBJS)}.  During
1702 the secondary expansion, the @code{$@@} variable is set to the name of
1703 the target and so the expansion for the @file{main} target will yield
1704 @code{$(main_OBJS)}, or @code{main.o try.o test.o}, while the
1705 secondary expansion for the @file{lib} target will yield
1706 @code{$(lib_OBJS)}, or @code{lib.o api.o}.
1708 You can also mix functions here, as long as they are properly escaped:
1710 @example
1711 main_SRCS := main.c try.c test.c
1712 lib_SRCS := lib.c api.c
1714 .SECONDEXPANSION:
1715 main lib: $$(patsubst %.c,%.o,$$($$@@_SRCS))
1716 @end example
1718 This version allows users to specify source files rather than object
1719 files, but gives the same resulting prerequisites list as the previous
1720 example.
1722 Evaluation of automatic variables during the secondary expansion
1723 phase, especially of the target name variable @code{$$@@}, behaves
1724 similarly to evaluation within command scripts.  However, there are
1725 some subtle differences and ``corner cases'' which come into play for
1726 the different types of rule definitions that @code{make} understands.
1727 The subtleties of using the different automatic variables are
1728 described below.
1730 @subheading Secondary Expansion of Explicit Rules
1731 @cindex secondary expansion and explicit rules
1732 @cindex explicit rules, secondary expansion of
1734 During the secondary expansion of explicit rules, @code{$$@@} and
1735 @code{$$%} evaluate, respectively, to the file name of the target and,
1736 when the target is an archive member, the target member name.  The
1737 @code{$$<} variable evaluates to the first prerequisite in the first
1738 rule for this target.  @code{$$^} and @code{$$+} evaluate to the list
1739 of all prerequisites of rules @emph{that have already appeared} for
1740 the same target (@code{$$+} with repetitions and @code{$$^}
1741 without).  The following example will help illustrate these behaviors:
1743 @example
1744 .SECONDEXPANSION:
1746 foo: foo.1 bar.1 $$< $$^ $$+    # line #1
1748 foo: foo.2 bar.2 $$< $$^ $$+    # line #2
1750 foo: foo.3 bar.3 $$< $$^ $$+    # line #3
1751 @end example
1753 In the first prerequisite list, all three variables (@code{$$<},
1754 @code{$$^}, and @code{$$+}) expand to the empty string.  In the
1755 second, they will have values @code{foo.1}, @code{foo.1 bar.1}, and
1756 @code{foo.1 bar.1} respectively.  In the third they will have values
1757 @code{foo.1}, @code{foo.1 bar.1 foo.2 bar.2}, and @code{foo.1 bar.1
1758 foo.2 bar.2} respectively.
1760 Rules undergo secondary expansion in makefile order, except that
1761 the rule with the command script is always evaluated last.
1763 The variables @code{$$?} and @code{$$*} are not available and expand
1764 to the empty string.
1766 @subheading Secondary Expansion of Static Pattern Rules
1767 @cindex secondary expansion and static pattern rules
1768 @cindex static pattern rules, secondary expansion of
1770 Rules for secondary expansion of static pattern rules are identical to
1771 those for explicit rules, above, with one exception: for static
1772 pattern rules the @code{$$*} variable is set to the pattern stem.  As
1773 with explicit rules, @code{$$?} is not available and expands to the
1774 empty string.
1776 @subheading Secondary Expansion of Implicit Rules
1777 @cindex secondary expansion and implicit rules
1778 @cindex implicit rules, secondary expansion of
1780 As @code{make} searches for an implicit rule, it substitutes the stem
1781 and then performs secondary expansion for every rule with a matching
1782 target pattern.  The value of the automatic variables is derived in
1783 the same fashion as for static pattern rules.  As an example:
1785 @example
1786 .SECONDEXPANSION:
1788 foo: bar
1790 foo foz: fo%: bo%
1792 %oo: $$< $$^ $$+ $$*
1793 @end example
1795 When the implicit rule is tried for target @file{foo}, @code{$$<}
1796 expands to @file{bar}, @code{$$^} expands to @file{bar boo},
1797 @code{$$+} also expands to @file{bar boo}, and @code{$$*} expands to
1798 @file{f}.
1800 Note that the directory prefix (D), as described in @ref{Implicit Rule
1801 Search, ,Implicit Rule Search Algorithm}, is appended (after
1802 expansion) to all the patterns in the prerequisites list.  As an
1803 example:
1805 @example
1806 .SECONDEXPANSION:
1808 /tmp/foo.o:
1810 %.o: $$(addsuffix /%.c,foo bar) foo.h
1811 @end example
1813 The prerequisite list after the secondary expansion and directory
1814 prefix reconstruction will be @file{/tmp/foo/foo.c /tmp/var/bar/foo.c
1815 foo.h}.  If you are not interested in this reconstruction, you can use
1816 @code{$$*} instead of @code{%} in the prerequisites list.
1818 @node Rules, Commands, Makefiles, Top
1819 @chapter Writing Rules
1820 @cindex writing rules
1821 @cindex rule, how to write
1822 @cindex target
1823 @cindex prerequisite
1825 A @dfn{rule} appears in the makefile and says when and how to remake
1826 certain files, called the rule's @dfn{targets} (most often only one per rule).
1827 It lists the other files that are the @dfn{prerequisites} of the target, and
1828 @dfn{commands} to use to create or update the target.
1830 @cindex default goal
1831 @cindex goal, default
1832 The order of rules is not significant, except for determining the
1833 @dfn{default goal}: the target for @code{make} to consider, if you do
1834 not otherwise specify one.  The default goal is the target of the first
1835 rule in the first makefile.  If the first rule has multiple targets,
1836 only the first target is taken as the default.  There are two
1837 exceptions: a target starting with a period is not a default unless it
1838 contains one or more slashes, @samp{/}, as well; and, a target that
1839 defines a pattern rule has no effect on the default goal.
1840 (@xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.)
1842 Therefore, we usually write the makefile so that the first rule is the
1843 one for compiling the entire program or all the programs described by
1844 the makefile (often with a target called @samp{all}).
1845 @xref{Goals, ,Arguments to Specify the Goals}.
1847 @menu
1848 * Rule Example::                An example explained.
1849 * Rule Syntax::                 General syntax explained.
1850 * Prerequisite Types::          There are two types of prerequisites.
1851 * Wildcards::                   Using wildcard characters such as `*'.
1852 * Directory Search::            Searching other directories for source files.
1853 * Phony Targets::               Using a target that is not a real file's name.
1854 * Force Targets::               You can use a target without commands
1855                                   or prerequisites to mark other
1856                                   targets as phony.
1857 * Empty Targets::               When only the date matters and the
1858                                   files are empty.
1859 * Special Targets::             Targets with special built-in meanings.
1860 * Multiple Targets::            When to make use of several targets in a rule.
1861 * Multiple Rules::              How to use several rules with the same target.
1862 * Static Pattern::              Static pattern rules apply to multiple targets
1863                                   and can vary the prerequisites according to
1864                                   the target name.
1865 * Double-Colon::                How to use a special kind of rule to allow
1866                                   several independent rules for one target.
1867 * Automatic Prerequisites::     How to automatically generate rules giving
1868                                   prerequisites from source files themselves.
1869 @end menu
1871 @ifnottex
1872 @node Rule Example, Rule Syntax, Rules, Rules
1873 @section Rule Example
1875 Here is an example of a rule:
1877 @example
1878 foo.o : foo.c defs.h       # module for twiddling the frobs
1879         cc -c -g foo.c
1880 @end example
1882 Its target is @file{foo.o} and its prerequisites are @file{foo.c} and
1883 @file{defs.h}.  It has one command, which is @samp{cc -c -g foo.c}.
1884 The command line starts with a tab to identify it as a command.
1886 This rule says two things:
1888 @itemize @bullet
1889 @item
1890 How to decide whether @file{foo.o} is out of date: it is out of date
1891 if it does not exist, or if either @file{foo.c} or @file{defs.h} is
1892 more recent than it.
1894 @item
1895 How to update the file @file{foo.o}: by running @code{cc} as stated.
1896 The command does not explicitly mention @file{defs.h}, but we presume
1897 that @file{foo.c} includes it, and that that is why @file{defs.h} was
1898 added to the prerequisites.
1899 @end itemize
1900 @end ifnottex
1902 @node Rule Syntax, Prerequisite Types, Rule Example, Rules
1903 @section Rule Syntax
1905 @cindex rule syntax
1906 @cindex syntax of rules
1907 In general, a rule looks like this:
1909 @example
1910 @var{targets} : @var{prerequisites}
1911         @var{command}
1912         @dots{}
1913 @end example
1915 @noindent
1916 or like this:
1918 @example
1919 @var{targets} : @var{prerequisites} ; @var{command}
1920         @var{command}
1921         @dots{}
1922 @end example
1924 @cindex targets
1925 @cindex rule targets
1926 The @var{targets} are file names, separated by spaces.  Wildcard
1927 characters may be used (@pxref{Wildcards, ,Using Wildcard Characters
1928 in File Names}) and a name of the form @file{@var{a}(@var{m})}
1929 represents member @var{m} in archive file @var{a}
1930 (@pxref{Archive Members, ,Archive Members as Targets}).
1931 Usually there is only one
1932 target per rule, but occasionally there is a reason to have more
1933 (@pxref{Multiple Targets, , Multiple Targets in a Rule}).@refill
1935 @cindex commands
1936 @cindex tab character (in commands)
1937 The @var{command} lines start with a tab character.  The first command may
1938 appear on the line after the prerequisites, with a tab character, or may
1939 appear on the same line, with a semicolon.  Either way, the effect is the
1940 same.  @xref{Commands, ,Writing the Commands in Rules}.
1942 @cindex dollar sign (@code{$}), in rules
1943 @cindex @code{$}, in rules
1944 @cindex rule, and @code{$}
1945 Because dollar signs are used to start variable references, if you really
1946 want a dollar sign in a rule you must write two of them, @samp{$$}
1947 (@pxref{Using Variables, ,How to Use Variables}).  In prerequisite
1948 lists you must actually write @emph{four} dollar signs (@samp{$$$$}),
1949 due to secondary expansion (@pxref{Secondary Expansion}).
1950 You may split a long line by inserting a backslash
1951 followed by a newline, but this is not required, as @code{make} places no
1952 limit on the length of a line in a makefile.
1954 A rule tells @code{make} two things: when the targets are out of date,
1955 and how to update them when necessary.
1957 @cindex prerequisites
1958 @cindex rule prerequisites
1959 The criterion for being out of date is specified in terms of the
1960 @var{prerequisites}, which consist of file names separated by spaces.
1961 (Wildcards and archive members (@pxref{Archives}) are allowed here too.)
1962 A target is out of date if it does not exist or if it is older than any
1963 of the prerequisites (by comparison of last-modification times).  The
1964 idea is that the contents of the target file are computed based on
1965 information in the prerequisites, so if any of the prerequisites changes,
1966 the contents of the existing target file are no longer necessarily
1967 valid.
1969 How to update is specified by @var{commands}.  These are lines to be
1970 executed by the shell (normally @samp{sh}), but with some extra features
1971 (@pxref{Commands, ,Writing the Commands in Rules}).
1973 @node Prerequisite Types, Wildcards, Rule Syntax, Rules
1974 @comment  node-name,  next,  previous,  up
1975 @section Types of Prerequisites
1976 @cindex prerequisite types
1977 @cindex types of prerequisites
1979 @cindex prerequisites, normal
1980 @cindex normal prerequisites
1981 @cindex prerequisites, order-only
1982 @cindex order-only prerequisites
1983 There are actually two different types of prerequisites understood by
1984 GNU @code{make}: normal prerequisites such as described in the
1985 previous section, and @dfn{order-only} prerequisites.  A normal
1986 prerequisite makes two statements: first, it imposes an order of
1987 execution of build commands: any commands necessary to build any of a
1988 target's prerequisites will be fully executed before any commands
1989 necessary to build the target.  Second, it imposes a dependency
1990 relationship: if any prerequisite is newer than the target, then the
1991 target is considered out-of-date and must be rebuilt.
1993 Normally, this is exactly what you want: if a target's prerequisite is
1994 updated, then the target should also be updated.
1996 Occasionally, however, you have a situation where you want to impose a
1997 specific ordering on the rules to be invoked @emph{without} forcing
1998 the target to be updated if one of those rules is executed.  In that
1999 case, you want to define @dfn{order-only} prerequisites.  Order-only
2000 prerequisites can be specified by placing a pipe symbol (@code{|})
2001 in the prerequisites list: any prerequisites to the left of the pipe
2002 symbol are normal; any prerequisites to the right are order-only:
2004 @example
2005 @var{targets} : @var{normal-prerequisites} | @var{order-only-prerequisites}
2006 @end example
2008 The normal prerequisites section may of course be empty.  Also, you
2009 may still declare multiple lines of prerequisites for the same target:
2010 they are appended appropriately.  Note that if you declare the same
2011 file to be both a normal and an order-only prerequisite, the normal
2012 prerequisite takes precedence (since they are a strict superset of the
2013 behavior of an order-only prerequisite).
2015 @node Wildcards, Directory Search, Prerequisite Types, Rules
2016 @section Using Wildcard Characters in File Names
2017 @cindex wildcard
2018 @cindex file name with wildcards
2019 @cindex globbing (wildcards)
2021 @cindex @code{*} (wildcard character)
2022 @cindex @code{?} (wildcard character)
2023 @cindex @code{[@dots{}]} (wildcard characters)
2024 A single file name can specify many files using @dfn{wildcard characters}.
2025 The wildcard characters in @code{make} are @samp{*}, @samp{?} and
2026 @samp{[@dots{}]}, the same as in the Bourne shell.  For example, @file{*.c}
2027 specifies a list of all the files (in the working directory) whose names
2028 end in @samp{.c}.@refill
2030 @cindex @code{~} (tilde)
2031 @cindex tilde (@code{~})
2032 @cindex home directory
2033 The character @samp{~} at the beginning of a file name also has special
2034 significance.  If alone, or followed by a slash, it represents your home
2035 directory.  For example @file{~/bin} expands to @file{/home/you/bin}.
2036 If the @samp{~} is followed by a word, the string represents the home
2037 directory of the user named by that word.  For example @file{~john/bin}
2038 expands to @file{/home/john/bin}.  On systems which don't have a home
2039 directory for each user (such as MS-DOS or MS-Windows), this
2040 functionality can be simulated by setting the environment variable
2041 @var{HOME}.@refill
2043 Wildcard expansion happens automatically in targets, in prerequisites,
2044 and in commands (where the shell does the expansion).  In other
2045 contexts, wildcard expansion happens only if you request it explicitly
2046 with the @code{wildcard} function.
2048 The special significance of a wildcard character can be turned off by
2049 preceding it with a backslash.  Thus, @file{foo\*bar} would refer to a
2050 specific file whose name consists of @samp{foo}, an asterisk, and
2051 @samp{bar}.@refill
2053 @menu
2054 * Wildcard Examples::           Several examples
2055 * Wildcard Pitfall::            Problems to avoid.
2056 * Wildcard Function::           How to cause wildcard expansion where
2057                                   it does not normally take place.
2058 @end menu
2060 @node Wildcard Examples, Wildcard Pitfall, Wildcards, Wildcards
2061 @subsection Wildcard Examples
2063 Wildcards can be used in the commands of a rule, where they are expanded
2064 by the shell.  For example, here is a rule to delete all the object files:
2066 @example
2067 @group
2068 clean:
2069         rm -f *.o
2070 @end group
2071 @end example
2072 @cindex @code{rm} (shell command)
2074 Wildcards are also useful in the prerequisites of a rule.  With the
2075 following rule in the makefile, @samp{make print} will print all the
2076 @samp{.c} files that have changed since the last time you printed them:
2078 @example
2079 print: *.c
2080         lpr -p $?
2081         touch print
2082 @end example
2084 @cindex @code{print} target
2085 @cindex @code{lpr} (shell command)
2086 @cindex @code{touch} (shell command)
2087 @noindent
2088 This rule uses @file{print} as an empty target file; see @ref{Empty
2089 Targets, ,Empty Target Files to Record Events}.  (The automatic variable
2090 @samp{$?} is used to print only those files that have changed; see
2091 @ref{Automatic Variables}.)@refill
2093 Wildcard expansion does not happen when you define a variable.  Thus, if
2094 you write this:
2096 @example
2097 objects = *.o
2098 @end example
2100 @noindent
2101 then the value of the variable @code{objects} is the actual string
2102 @samp{*.o}.  However, if you use the value of @code{objects} in a target,
2103 prerequisite or command, wildcard expansion will take place at that time.
2104 To set @code{objects} to the expansion, instead use:
2106 @example
2107 objects := $(wildcard *.o)
2108 @end example
2110 @noindent
2111 @xref{Wildcard Function}.
2113 @node Wildcard Pitfall, Wildcard Function, Wildcard Examples, Wildcards
2114 @subsection Pitfalls of Using Wildcards
2115 @cindex wildcard pitfalls
2116 @cindex pitfalls of wildcards
2117 @cindex mistakes with wildcards
2118 @cindex errors with wildcards
2119 @cindex problems with wildcards
2121 Now here is an example of a naive way of using wildcard expansion, that
2122 does not do what you would intend.  Suppose you would like to say that the
2123 executable file @file{foo} is made from all the object files in the
2124 directory, and you write this:
2126 @example
2127 objects = *.o
2129 foo : $(objects)
2130         cc -o foo $(CFLAGS) $(objects)
2131 @end example
2133 @noindent
2134 The value of @code{objects} is the actual string @samp{*.o}.  Wildcard
2135 expansion happens in the rule for @file{foo}, so that each @emph{existing}
2136 @samp{.o} file becomes a prerequisite of @file{foo} and will be recompiled if
2137 necessary.
2139 But what if you delete all the @samp{.o} files?  When a wildcard matches
2140 no files, it is left as it is, so then @file{foo} will depend on the
2141 oddly-named file @file{*.o}.  Since no such file is likely to exist,
2142 @code{make} will give you an error saying it cannot figure out how to
2143 make @file{*.o}.  This is not what you want!
2145 Actually it is possible to obtain the desired result with wildcard
2146 expansion, but you need more sophisticated techniques, including the
2147 @code{wildcard} function and string substitution.
2148 @ifnottex
2149 @xref{Wildcard Function, ,The Function @code{wildcard}}.
2150 @end ifnottex
2151 @iftex
2152 These are described in the following section.
2153 @end iftex
2155 @cindex wildcards and MS-DOS/MS-Windows backslashes
2156 @cindex backslashes in pathnames and wildcard expansion
2158 Microsoft operating systems (MS-DOS and MS-Windows) use backslashes to
2159 separate directories in pathnames, like so:
2161 @example
2162   c:\foo\bar\baz.c
2163 @end example
2165 This is equivalent to the Unix-style @file{c:/foo/bar/baz.c} (the
2166 @file{c:} part is the so-called drive letter).  When @code{make} runs on
2167 these systems, it supports backslashes as well as the Unix-style forward
2168 slashes in pathnames.  However, this support does @emph{not} include the
2169 wildcard expansion, where backslash is a quote character.  Therefore,
2170 you @emph{must} use Unix-style slashes in these cases.
2173 @node Wildcard Function,  , Wildcard Pitfall, Wildcards
2174 @subsection The Function @code{wildcard}
2175 @findex wildcard
2177 Wildcard expansion happens automatically in rules.  But wildcard expansion
2178 does not normally take place when a variable is set, or inside the
2179 arguments of a function.  If you want to do wildcard expansion in such
2180 places, you need to use the @code{wildcard} function, like this:
2182 @example
2183 $(wildcard @var{pattern}@dots{})
2184 @end example
2186 @noindent
2187 This string, used anywhere in a makefile, is replaced by a
2188 space-separated list of names of existing files that match one of the
2189 given file name patterns.  If no existing file name matches a pattern,
2190 then that pattern is omitted from the output of the @code{wildcard}
2191 function.  Note that this is different from how unmatched wildcards
2192 behave in rules, where they are used verbatim rather than ignored
2193 (@pxref{Wildcard Pitfall}).
2195 One use of the @code{wildcard} function is to get a list of all the C source
2196 files in a directory, like this:
2198 @example
2199 $(wildcard *.c)
2200 @end example
2202 We can change the list of C source files into a list of object files by
2203 replacing the @samp{.c} suffix with @samp{.o} in the result, like this:
2205 @example
2206 $(patsubst %.c,%.o,$(wildcard *.c))
2207 @end example
2209 @noindent
2210 (Here we have used another function, @code{patsubst}.
2211 @xref{Text Functions, ,Functions for String Substitution and Analysis}.)@refill
2213 Thus, a makefile to compile all C source files in the directory and then
2214 link them together could be written as follows:
2216 @example
2217 objects := $(patsubst %.c,%.o,$(wildcard *.c))
2219 foo : $(objects)
2220         cc -o foo $(objects)
2221 @end example
2223 @noindent
2224 (This takes advantage of the implicit rule for compiling C programs, so
2225 there is no need to write explicit rules for compiling the files.
2226 @xref{Flavors, ,The Two Flavors of Variables}, for an explanation of
2227 @samp{:=}, which is a variant of @samp{=}.)
2229 @node Directory Search, Phony Targets, Wildcards, Rules
2230 @section Searching Directories for Prerequisites
2231 @vindex VPATH
2232 @findex vpath
2233 @cindex vpath
2234 @cindex search path for prerequisites (@code{VPATH})
2235 @cindex directory search (@code{VPATH})
2237 For large systems, it is often desirable to put sources in a separate
2238 directory from the binaries.  The @dfn{directory search} features of
2239 @code{make} facilitate this by searching several directories
2240 automatically to find a prerequisite.  When you redistribute the files
2241 among directories, you do not need to change the individual rules,
2242 just the search paths.
2244 @menu
2245 * General Search::              Specifying a search path that applies
2246                                   to every prerequisite.
2247 * Selective Search::            Specifying a search path
2248                                   for a specified class of names.
2249 * Search Algorithm::            When and how search paths are applied.
2250 * Commands/Search::             How to write shell commands that work together
2251                                   with search paths.
2252 * Implicit/Search::             How search paths affect implicit rules.
2253 * Libraries/Search::            Directory search for link libraries.
2254 @end menu
2256 @node General Search, Selective Search, Directory Search, Directory Search
2257 @subsection @code{VPATH}: Search Path for All Prerequisites
2258 @vindex VPATH
2260 The value of the @code{make} variable @code{VPATH} specifies a list of
2261 directories that @code{make} should search.  Most often, the
2262 directories are expected to contain prerequisite files that are not in the
2263 current directory; however, @code{make} uses @code{VPATH} as a search
2264 list for both prerequisites and targets of rules.
2266 Thus, if a file that is listed as a target or prerequisite does not exist
2267 in the current directory, @code{make} searches the directories listed in
2268 @code{VPATH} for a file with that name.  If a file is found in one of
2269 them, that file may become the prerequisite (see below).  Rules may then
2270 specify the names of files in the prerequisite list as if they all
2271 existed in the current directory.  @xref{Commands/Search, ,Writing Shell
2272 Commands with Directory Search}.
2274 In the @code{VPATH} variable, directory names are separated by colons or
2275 blanks.  The order in which directories are listed is the order followed
2276 by @code{make} in its search.  (On MS-DOS and MS-Windows, semi-colons
2277 are used as separators of directory names in @code{VPATH}, since the
2278 colon can be used in the pathname itself, after the drive letter.)
2280 For example,
2282 @example
2283 VPATH = src:../headers
2284 @end example
2286 @noindent
2287 specifies a path containing two directories, @file{src} and
2288 @file{../headers}, which @code{make} searches in that order.
2290 With this value of @code{VPATH}, the following rule,
2292 @example
2293 foo.o : foo.c
2294 @end example
2296 @noindent
2297 is interpreted as if it were written like this:
2299 @example
2300 foo.o : src/foo.c
2301 @end example
2303 @noindent
2304 assuming the file @file{foo.c} does not exist in the current directory but
2305 is found in the directory @file{src}.
2307 @node Selective Search, Search Algorithm, General Search, Directory Search
2308 @subsection The @code{vpath} Directive
2309 @findex vpath
2311 Similar to the @code{VPATH} variable, but more selective, is the
2312 @code{vpath} directive (note lower case), which allows you to specify a
2313 search path for a particular class of file names: those that match a
2314 particular pattern.  Thus you can supply certain search directories for
2315 one class of file names and other directories (or none) for other file
2316 names.
2318 There are three forms of the @code{vpath} directive:
2320 @table @code
2321 @item vpath @var{pattern} @var{directories}
2322 Specify the search path @var{directories} for file names that match
2323 @var{pattern}.
2325 The search path, @var{directories}, is a list of directories to be
2326 searched, separated by colons (semi-colons on MS-DOS and MS-Windows) or
2327 blanks, just like the search path used in the @code{VPATH} variable.
2329 @item vpath @var{pattern}
2330 Clear out the search path associated with @var{pattern}.
2332 @c Extra blank line makes sure this gets two lines.
2333 @item vpath
2335 Clear all search paths previously specified with @code{vpath} directives.
2336 @end table
2338 A @code{vpath} pattern is a string containing a @samp{%} character.  The
2339 string must match the file name of a prerequisite that is being searched
2340 for, the @samp{%} character matching any sequence of zero or more
2341 characters (as in pattern rules; @pxref{Pattern Rules, ,Defining and
2342 Redefining Pattern Rules}).  For example, @code{%.h} matches files that
2343 end in @code{.h}.  (If there is no @samp{%}, the pattern must match the
2344 prerequisite exactly, which is not useful very often.)
2346 @cindex @code{%}, quoting in @code{vpath}
2347 @cindex @code{%}, quoting with @code{\} (backslash)
2348 @cindex @code{\} (backslash), to quote @code{%}
2349 @cindex backslash (@code{\}), to quote @code{%}
2350 @cindex quoting @code{%}, in @code{vpath}
2351 @samp{%} characters in a @code{vpath} directive's pattern can be quoted
2352 with preceding backslashes (@samp{\}).  Backslashes that would otherwise
2353 quote @samp{%} characters can be quoted with more backslashes.
2354 Backslashes that quote @samp{%} characters or other backslashes are
2355 removed from the pattern before it is compared to file names.  Backslashes
2356 that are not in danger of quoting @samp{%} characters go unmolested.@refill
2358 When a prerequisite fails to exist in the current directory, if the
2359 @var{pattern} in a @code{vpath} directive matches the name of the
2360 prerequisite file, then the @var{directories} in that directive are searched
2361 just like (and before) the directories in the @code{VPATH} variable.
2363 For example,
2365 @example
2366 vpath %.h ../headers
2367 @end example
2369 @noindent
2370 tells @code{make} to look for any prerequisite whose name ends in @file{.h}
2371 in the directory @file{../headers} if the file is not found in the current
2372 directory.
2374 If several @code{vpath} patterns match the prerequisite file's name, then
2375 @code{make} processes each matching @code{vpath} directive one by one,
2376 searching all the directories mentioned in each directive.  @code{make}
2377 handles multiple @code{vpath} directives in the order in which they
2378 appear in the makefile; multiple directives with the same pattern are
2379 independent of each other.
2381 @need 750
2382 Thus,
2384 @example
2385 @group
2386 vpath %.c foo
2387 vpath %   blish
2388 vpath %.c bar
2389 @end group
2390 @end example
2392 @noindent
2393 will look for a file ending in @samp{.c} in @file{foo}, then
2394 @file{blish}, then @file{bar}, while
2396 @example
2397 @group
2398 vpath %.c foo:bar
2399 vpath %   blish
2400 @end group
2401 @end example
2403 @noindent
2404 will look for a file ending in @samp{.c} in @file{foo}, then
2405 @file{bar}, then @file{blish}.
2407 @node Search Algorithm, Commands/Search, Selective Search, Directory Search
2408 @subsection How Directory Searches are Performed
2409 @cindex algorithm for directory search
2410 @cindex directory search algorithm
2412 When a prerequisite is found through directory search, regardless of type
2413 (general or selective), the pathname located may not be the one that
2414 @code{make} actually provides you in the prerequisite list.  Sometimes
2415 the path discovered through directory search is thrown away.
2417 The algorithm @code{make} uses to decide whether to keep or abandon a
2418 path found via directory search is as follows:
2420 @enumerate
2421 @item
2422 If a target file does not exist at the path specified in the makefile,
2423 directory search is performed.
2425 @item
2426 If the directory search is successful, that path is kept and this file
2427 is tentatively stored as the target.
2429 @item
2430 All prerequisites of this target are examined using this same method.
2432 @item
2433 After processing the prerequisites, the target may or may not need to be
2434 rebuilt:
2436 @enumerate a
2437 @item
2438 If the target does @emph{not} need to be rebuilt, the path to the file
2439 found during directory search is used for any prerequisite lists which
2440 contain this target.  In short, if @code{make} doesn't need to rebuild
2441 the target then you use the path found via directory search.
2443 @item
2444 If the target @emph{does} need to be rebuilt (is out-of-date), the
2445 pathname found during directory search is @emph{thrown away}, and the
2446 target is rebuilt using the file name specified in the makefile.  In
2447 short, if @code{make} must rebuild, then the target is rebuilt locally,
2448 not in the directory found via directory search.
2449 @end enumerate
2450 @end enumerate
2452 This algorithm may seem complex, but in practice it is quite often
2453 exactly what you want.
2455 @cindex traditional directory search (GPATH)
2456 @cindex directory search, traditional (GPATH)
2457 Other versions of @code{make} use a simpler algorithm: if the file does
2458 not exist, and it is found via directory search, then that pathname is
2459 always used whether or not the target needs to be built.  Thus, if the
2460 target is rebuilt it is created at the pathname discovered during
2461 directory search.
2463 @vindex GPATH
2464 If, in fact, this is the behavior you want for some or all of your
2465 directories, you can use the @code{GPATH} variable to indicate this to
2466 @code{make}.
2468 @code{GPATH} has the same syntax and format as @code{VPATH} (that is, a
2469 space- or colon-delimited list of pathnames).  If an out-of-date target
2470 is found by directory search in a directory that also appears in
2471 @code{GPATH}, then that pathname is not thrown away.  The target is
2472 rebuilt using the expanded path.
2474 @node Commands/Search, Implicit/Search, Search Algorithm, Directory Search
2475 @subsection Writing Shell Commands with Directory Search
2476 @cindex shell command, and directory search
2477 @cindex directory search (@code{VPATH}), and shell commands
2479 When a prerequisite is found in another directory through directory search,
2480 this cannot change the commands of the rule; they will execute as written.
2481 Therefore, you must write the commands with care so that they will look for
2482 the prerequisite in the directory where @code{make} finds it.
2484 This is done with the @dfn{automatic variables} such as @samp{$^}
2485 (@pxref{Automatic Variables}).
2486 For instance, the value of @samp{$^} is a
2487 list of all the prerequisites of the rule, including the names of
2488 the directories in which they were found, and the value of
2489 @samp{$@@} is the target.  Thus:@refill
2491 @example
2492 foo.o : foo.c
2493         cc -c $(CFLAGS) $^ -o $@@
2494 @end example
2496 @noindent
2497 (The variable @code{CFLAGS} exists so you can specify flags for C
2498 compilation by implicit rules; we use it here for consistency so it will
2499 affect all C compilations uniformly;
2500 @pxref{Implicit Variables, ,Variables Used by Implicit Rules}.)
2502 Often the prerequisites include header files as well, which you do not
2503 want to mention in the commands.  The automatic variable @samp{$<} is
2504 just the first prerequisite:
2506 @example
2507 VPATH = src:../headers
2508 foo.o : foo.c defs.h hack.h
2509         cc -c $(CFLAGS) $< -o $@@
2510 @end example
2512 @node Implicit/Search, Libraries/Search, Commands/Search, Directory Search
2513 @subsection Directory Search and Implicit Rules
2514 @cindex @code{VPATH}, and implicit rules
2515 @cindex directory search (@code{VPATH}), and implicit rules
2516 @cindex search path for prerequisites (@code{VPATH}), and implicit rules
2517 @cindex implicit rule, and directory search
2518 @cindex implicit rule, and @code{VPATH}
2519 @cindex rule, implicit, and directory search
2520 @cindex rule, implicit, and @code{VPATH}
2522 The search through the directories specified in @code{VPATH} or with
2523 @code{vpath} also happens during consideration of implicit rules
2524 (@pxref{Implicit Rules, ,Using Implicit Rules}).
2526 For example, when a file @file{foo.o} has no explicit rule, @code{make}
2527 considers implicit rules, such as the built-in rule to compile
2528 @file{foo.c} if that file exists.  If such a file is lacking in the
2529 current directory, the appropriate directories are searched for it.  If
2530 @file{foo.c} exists (or is mentioned in the makefile) in any of the
2531 directories, the implicit rule for C compilation is applied.
2533 The commands of implicit rules normally use automatic variables as a
2534 matter of necessity; consequently they will use the file names found by
2535 directory search with no extra effort.
2537 @node Libraries/Search,  , Implicit/Search, Directory Search
2538 @subsection Directory Search for Link Libraries
2539 @cindex link libraries, and directory search
2540 @cindex libraries for linking, directory search
2541 @cindex directory search (@code{VPATH}), and link libraries
2542 @cindex @code{VPATH}, and link libraries
2543 @cindex search path for prerequisites (@code{VPATH}), and link libraries
2544 @cindex @code{-l} (library search)
2545 @cindex link libraries, patterns matching
2546 @cindex @code{.LIBPATTERNS}, and link libraries
2547 @vindex .LIBPATTERNS
2549 Directory search applies in a special way to libraries used with the
2550 linker.  This special feature comes into play when you write a prerequisite
2551 whose name is of the form @samp{-l@var{name}}.  (You can tell something
2552 strange is going on here because the prerequisite is normally the name of a
2553 file, and the @emph{file name} of a library generally looks like
2554 @file{lib@var{name}.a}, not like @samp{-l@var{name}}.)@refill
2556 When a prerequisite's name has the form @samp{-l@var{name}}, @code{make}
2557 handles it specially by searching for the file @file{lib@var{name}.so} in
2558 the current directory, in directories specified by matching @code{vpath}
2559 search paths and the @code{VPATH} search path, and then in the
2560 directories @file{/lib}, @file{/usr/lib}, and @file{@var{prefix}/lib}
2561 (normally @file{/usr/local/lib}, but MS-DOS/MS-Windows versions of
2562 @code{make} behave as if @var{prefix} is defined to be the root of the
2563 DJGPP installation tree).
2565 If that file is not found, then the file @file{lib@var{name}.a} is
2566 searched for, in the same directories as above.
2568 For example, if there is a @file{/usr/lib/libcurses.a} library on your
2569 system (and no @file{/usr/lib/libcurses.so} file), then
2571 @example
2572 @group
2573 foo : foo.c -lcurses
2574         cc $^ -o $@@
2575 @end group
2576 @end example
2578 @noindent
2579 would cause the command @samp{cc foo.c /usr/lib/libcurses.a -o foo} to
2580 be executed when @file{foo} is older than @file{foo.c} or than
2581 @file{/usr/lib/libcurses.a}.@refill
2583 Although the default set of files to be searched for is
2584 @file{lib@var{name}.so} and @file{lib@var{name}.a}, this is customizable
2585 via the @code{.LIBPATTERNS} variable.  Each word in the value of this
2586 variable is a pattern string.  When a prerequisite like
2587 @samp{-l@var{name}} is seen, @code{make} will replace the percent in
2588 each pattern in the list with @var{name} and perform the above directory
2589 searches using that library filename.  If no library is found, the next
2590 word in the list will be used.
2592 The default value for @code{.LIBPATTERNS} is @samp{lib%.so lib%.a},
2593 which provides the default behavior described above.
2595 You can turn off link library expansion completely by setting this
2596 variable to an empty value.
2598 @node Phony Targets, Force Targets, Directory Search, Rules
2599 @section Phony Targets
2600 @cindex phony targets
2601 @cindex targets, phony
2602 @cindex targets without a file
2604 A phony target is one that is not really the name of a file.  It is just a
2605 name for some commands to be executed when you make an explicit request.
2606 There are two reasons to use a phony target: to avoid a conflict with
2607 a file of the same name, and to improve performance.
2609 If you write a rule whose commands will not create the target file, the
2610 commands will be executed every time the target comes up for remaking.
2611 Here is an example:
2613 @example
2614 @group
2615 clean:
2616         rm *.o temp
2617 @end group
2618 @end example
2620 @noindent
2621 Because the @code{rm} command does not create a file named @file{clean},
2622 probably no such file will ever exist.  Therefore, the @code{rm} command
2623 will be executed every time you say @samp{make clean}.
2624 @cindex @code{rm} (shell command)
2626 @findex .PHONY
2627 The phony target will cease to work if anything ever does create a file
2628 named @file{clean} in this directory.  Since it has no prerequisites, the
2629 file @file{clean} would inevitably be considered up to date, and its
2630 commands would not be executed.  To avoid this problem, you can explicitly
2631 declare the target to be phony, using the special target @code{.PHONY}
2632 (@pxref{Special Targets, ,Special Built-in Target Names}) as follows:
2634 @example
2635 .PHONY : clean
2636 @end example
2638 @noindent
2639 Once this is done, @samp{make clean} will run the commands regardless of
2640 whether there is a file named @file{clean}.
2642 Since it knows that phony targets do not name actual files that could be
2643 remade from other files, @code{make} skips the implicit rule search for
2644 phony targets (@pxref{Implicit Rules}).  This is why declaring a target
2645 phony is good for performance, even if you are not worried about the
2646 actual file existing.
2648 Thus, you first write the line that states that @code{clean} is a
2649 phony target, then you write the rule, like this:
2651 @example
2652 @group
2653 .PHONY: clean
2654 clean:
2655         rm *.o temp
2656 @end group
2657 @end example
2659 Another example of the usefulness of phony targets is in conjunction
2660 with recursive invocations of @code{make} (for more information, see
2661 @ref{Recursion, ,Recursive Use of @code{make}}).  In this case the
2662 makefile will often contain a variable which lists a number of
2663 subdirectories to be built.  One way to handle this is with one rule
2664 whose command is a shell loop over the subdirectories, like this:
2666 @example
2667 @group
2668 SUBDIRS = foo bar baz
2670 subdirs:
2671         for dir in $(SUBDIRS); do \
2672           $(MAKE) -C $$dir; \
2673         done
2674 @end group
2675 @end example
2677 There are a few problems with this method, however.  First, any error
2678 detected in a submake is not noted by this rule, so it will continue to
2679 build the rest of the directories even when one fails.  This can be
2680 overcome by adding shell commands to note the error and exit, but then
2681 it will do so even if @code{make} is invoked with the @code{-k} option,
2682 which is unfortunate.  Second, and perhaps more importantly, you cannot
2683 take advantage of @code{make}'s ability to build targets in parallel
2684 (@pxref{Parallel, ,Parallel Execution}), since there is only one rule.
2686 By declaring the subdirectories as phony targets (you must do this as
2687 the subdirectory obviously always exists; otherwise it won't be built)
2688 you can remove these problems:
2690 @example
2691 @group
2692 SUBDIRS = foo bar baz
2694 .PHONY: subdirs $(SUBDIRS)
2696 subdirs: $(SUBDIRS)
2698 $(SUBDIRS):
2699         $(MAKE) -C $@@
2701 foo: baz
2702 @end group
2703 @end example
2705 Here we've also declared that the @file{foo} subdirectory cannot be
2706 built until after the @file{baz} subdirectory is complete; this kind of
2707 relationship declaration is particularly important when attempting
2708 parallel builds.
2710 A phony target should not be a prerequisite of a real target file; if it
2711 is, its commands are run every time @code{make} goes to update that
2712 file.  As long as a phony target is never a prerequisite of a real
2713 target, the phony target commands will be executed only when the phony
2714 target is a specified goal (@pxref{Goals, ,Arguments to Specify the
2715 Goals}).
2717 Phony targets can have prerequisites.  When one directory contains multiple
2718 programs, it is most convenient to describe all of the programs in one
2719 makefile @file{./Makefile}.  Since the target remade by default will be the
2720 first one in the makefile, it is common to make this a phony target named
2721 @samp{all} and give it, as prerequisites, all the individual programs.  For
2722 example:
2724 @example
2725 all : prog1 prog2 prog3
2726 .PHONY : all
2728 prog1 : prog1.o utils.o
2729         cc -o prog1 prog1.o utils.o
2731 prog2 : prog2.o
2732         cc -o prog2 prog2.o
2734 prog3 : prog3.o sort.o utils.o
2735         cc -o prog3 prog3.o sort.o utils.o
2736 @end example
2738 @noindent
2739 Now you can say just @samp{make} to remake all three programs, or
2740 specify as arguments the ones to remake (as in @samp{make prog1
2741 prog3}).  Phoniness is not inherited: the prerequisites of a phony
2742 target are not themselves phony, unless explicitly declared to be so.
2744 When one phony target is a prerequisite of another, it serves as a subroutine
2745 of the other.  For example, here @samp{make cleanall} will delete the
2746 object files, the difference files, and the file @file{program}:
2748 @example
2749 .PHONY: cleanall cleanobj cleandiff
2751 cleanall : cleanobj cleandiff
2752         rm program
2754 cleanobj :
2755         rm *.o
2757 cleandiff :
2758         rm *.diff
2759 @end example
2761 @node Force Targets, Empty Targets, Phony Targets, Rules
2762 @section Rules without Commands or Prerequisites
2763 @cindex force targets
2764 @cindex targets, force
2765 @cindex @code{FORCE}
2766 @cindex rule, no commands or prerequisites
2768 If a rule has no prerequisites or commands, and the target of the rule
2769 is a nonexistent file, then @code{make} imagines this target to have
2770 been updated whenever its rule is run.  This implies that all targets
2771 depending on this one will always have their commands run.
2773 An example will illustrate this:
2775 @example
2776 @group
2777 clean: FORCE
2778         rm $(objects)
2779 FORCE:
2780 @end group
2781 @end example
2783 Here the target @samp{FORCE} satisfies the special conditions, so the
2784 target @file{clean} that depends on it is forced to run its commands.
2785 There is nothing special about the name @samp{FORCE}, but that is one name
2786 commonly used this way.
2788 As you can see, using @samp{FORCE} this way has the same results as using
2789 @samp{.PHONY: clean}.
2791 Using @samp{.PHONY} is more explicit and more efficient.  However,
2792 other versions of @code{make} do not support @samp{.PHONY}; thus
2793 @samp{FORCE} appears in many makefiles.  @xref{Phony Targets}.
2795 @node Empty Targets, Special Targets, Force Targets, Rules
2796 @section Empty Target Files to Record Events
2797 @cindex empty targets
2798 @cindex targets, empty
2799 @cindex recording events with empty targets
2801 The @dfn{empty target} is a variant of the phony target; it is used to hold
2802 commands for an action that you request explicitly from time to time.
2803 Unlike a phony target, this target file can really exist; but the file's
2804 contents do not matter, and usually are empty.
2806 The purpose of the empty target file is to record, with its
2807 last-modification time, when the rule's commands were last executed.  It
2808 does so because one of the commands is a @code{touch} command to update the
2809 target file.
2811 The empty target file should have some prerequisites (otherwise it
2812 doesn't make sense).  When you ask to remake the empty target, the
2813 commands are executed if any prerequisite is more recent than the target;
2814 in other words, if a prerequisite has changed since the last time you
2815 remade the target.  Here is an example:
2817 @example
2818 print: foo.c bar.c
2819         lpr -p $?
2820         touch print
2821 @end example
2822 @cindex @code{print} target
2823 @cindex @code{lpr} (shell command)
2824 @cindex @code{touch} (shell command)
2826 @noindent
2827 With this rule, @samp{make print} will execute the @code{lpr} command if
2828 either source file has changed since the last @samp{make print}.  The
2829 automatic variable @samp{$?} is used to print only those files that have
2830 changed (@pxref{Automatic Variables}).
2832 @node Special Targets, Multiple Targets, Empty Targets, Rules
2833 @section Special Built-in Target Names
2834 @cindex special targets
2835 @cindex built-in special targets
2836 @cindex targets, built-in special
2838 Certain names have special meanings if they appear as targets.
2840 @table @code
2841 @findex .PHONY
2842 @item .PHONY
2844 The prerequisites of the special target @code{.PHONY} are considered to
2845 be phony targets.  When it is time to consider such a target,
2846 @code{make} will run its commands unconditionally, regardless of
2847 whether a file with that name exists or what its last-modification
2848 time is.  @xref{Phony Targets, ,Phony Targets}.
2850 @findex .SUFFIXES
2851 @item .SUFFIXES
2853 The prerequisites of the special target @code{.SUFFIXES} are the list
2854 of suffixes to be used in checking for suffix rules.
2855 @xref{Suffix Rules, , Old-Fashioned Suffix Rules}.
2857 @findex .DEFAULT
2858 @item .DEFAULT
2860 The commands specified for @code{.DEFAULT} are used for any target for
2861 which no rules are found (either explicit rules or implicit rules).
2862 @xref{Last Resort}.  If @code{.DEFAULT} commands are specified, every
2863 file mentioned as a prerequisite, but not as a target in a rule, will have
2864 these commands executed on its behalf.  @xref{Implicit Rule Search,
2865 ,Implicit Rule Search Algorithm}.
2867 @findex .PRECIOUS
2868 @item .PRECIOUS
2869 @cindex precious targets
2870 @cindex preserving with @code{.PRECIOUS}
2872 The targets which @code{.PRECIOUS} depends on are given the following
2873 special treatment: if @code{make} is killed or interrupted during the
2874 execution of their commands, the target is not deleted.
2875 @xref{Interrupts, ,Interrupting or Killing @code{make}}.  Also, if the
2876 target is an intermediate file, it will not be deleted after it is no
2877 longer needed, as is normally done.  @xref{Chained Rules, ,Chains of
2878 Implicit Rules}.  In this latter respect it overlaps with the
2879 @code{.SECONDARY} special target.
2881 You can also list the target pattern of an implicit rule (such as
2882 @samp{%.o}) as a prerequisite file of the special target @code{.PRECIOUS}
2883 to preserve intermediate files created by rules whose target patterns
2884 match that file's name.
2886 @findex .INTERMEDIATE
2887 @item .INTERMEDIATE
2888 @cindex intermediate targets, explicit
2890 The targets which @code{.INTERMEDIATE} depends on are treated as
2891 intermediate files.  @xref{Chained Rules, ,Chains of Implicit Rules}.
2892 @code{.INTERMEDIATE} with no prerequisites has no effect.
2894 @findex .SECONDARY
2895 @item .SECONDARY
2896 @cindex secondary targets
2897 @cindex preserving with @code{.SECONDARY}
2899 The targets which @code{.SECONDARY} depends on are treated as
2900 intermediate files, except that they are never automatically deleted.
2901 @xref{Chained Rules, ,Chains of Implicit Rules}.
2903 @code{.SECONDARY} with no prerequisites causes all targets to be treated
2904 as secondary (i.e., no target is removed because it is considered
2905 intermediate).
2907 @findex .SECONDEXPANSION
2908 @item .SECONDEXPANSION
2910 If @code{.SECONDEXPANSION} is mentioned as a target anywhere in the
2911 makefile, then all prerequisite lists defined @emph{after} it appears
2912 will be expanded a second time after all makefiles have been read in.
2913 @xref{Secondary Expansion, ,Secondary Expansion}.
2915 The prerequisites of the special target @code{.SUFFIXES} are the list
2916 of suffixes to be used in checking for suffix rules.
2917 @xref{Suffix Rules, , Old-Fashioned Suffix Rules}.
2919 @findex .DELETE_ON_ERROR
2920 @item .DELETE_ON_ERROR
2921 @cindex removing targets on failure
2923 If @code{.DELETE_ON_ERROR} is mentioned as a target anywhere in the
2924 makefile, then @code{make} will delete the target of a rule if it has
2925 changed and its commands exit with a nonzero exit status, just as it
2926 does when it receives a signal.  @xref{Errors, ,Errors in Commands}.
2928 @findex .IGNORE
2929 @item .IGNORE
2931 If you specify prerequisites for @code{.IGNORE}, then @code{make} will
2932 ignore errors in execution of the commands run for those particular
2933 files.  The commands for @code{.IGNORE} are not meaningful.
2935 If mentioned as a target with no prerequisites, @code{.IGNORE} says to
2936 ignore errors in execution of commands for all files.  This usage of
2937 @samp{.IGNORE} is supported only for historical compatibility.  Since
2938 this affects every command in the makefile, it is not very useful; we
2939 recommend you use the more selective ways to ignore errors in specific
2940 commands.  @xref{Errors, ,Errors in Commands}.
2942 @findex .LOW_RESOLUTION_TIME
2943 @item .LOW_RESOLUTION_TIME
2945 If you specify prerequisites for @code{.LOW_RESOLUTION_TIME},
2946 @command{make} assumes that these files are created by commands that
2947 generate low resolution time stamps.  The commands for
2948 @code{.LOW_RESOLUTION_TIME} are not meaningful.
2950 The high resolution file time stamps of many modern hosts lessen the
2951 chance of @command{make} incorrectly concluding that a file is up to
2952 date.  Unfortunately, these hosts provide no way to set a high
2953 resolution file time stamp, so commands like @samp{cp -p} that
2954 explicitly set a file's time stamp must discard its subsecond part.  If
2955 a file is created by such a command, you should list it as a
2956 prerequisite of @code{.LOW_RESOLUTION_TIME} so that @command{make} does
2957 not mistakenly conclude that the file is out of date.  For example:
2959 @example
2960 @group
2961 .LOW_RESOLUTION_TIME: dst
2962 dst: src
2963         cp -p src dst
2964 @end group
2965 @end example
2967 Since @samp{cp -p} discards the subsecond part of @file{src}'s time
2968 stamp, @file{dst} is typically slightly older than @file{src} even when
2969 it is up to date.  The @code{.LOW_RESOLUTION_TIME} line causes
2970 @command{make} to consider @file{dst} to be up to date if its time stamp
2971 is at the start of the same second that @file{src}'s time stamp is in.
2973 Due to a limitation of the archive format, archive member time stamps
2974 are always low resolution.  You need not list archive members as
2975 prerequisites of @code{.LOW_RESOLUTION_TIME}, as @command{make} does this
2976 automatically.
2978 @findex .SILENT
2979 @item .SILENT
2981 If you specify prerequisites for @code{.SILENT}, then @code{make} will
2982 not print the commands to remake those particular files before executing
2983 them.  The commands for @code{.SILENT} are not meaningful.
2985 If mentioned as a target with no prerequisites, @code{.SILENT} says not
2986 to print any commands before executing them.  This usage of
2987 @samp{.SILENT} is supported only for historical compatibility.  We
2988 recommend you use the more selective ways to silence specific commands.
2989 @xref{Echoing, ,Command Echoing}.  If you want to silence all commands
2990 for a particular run of @code{make}, use the @samp{-s} or
2991 @w{@samp{--silent}} option (@pxref{Options Summary}).
2993 @findex .EXPORT_ALL_VARIABLES
2994 @item .EXPORT_ALL_VARIABLES
2996 Simply by being mentioned as a target, this tells @code{make} to
2997 export all variables to child processes by default.
2998 @xref{Variables/Recursion, ,Communicating Variables to a
2999 Sub-@code{make}}.
3001 @findex .NOTPARALLEL
3002 @item .NOTPARALLEL
3003 @cindex parallel execution, overriding
3005 If @code{.NOTPARALLEL} is mentioned as a target, then this invocation of
3006 @code{make} will be run serially, even if the @samp{-j} option is
3007 given.  Any recursively invoked @code{make} command will still be run in
3008 parallel (unless its makefile contains this target).  Any prerequisites
3009 on this target are ignored.
3010 @end table
3012 Any defined implicit rule suffix also counts as a special target if it
3013 appears as a target, and so does the concatenation of two suffixes, such
3014 as @samp{.c.o}.  These targets are suffix rules, an obsolete way of
3015 defining implicit rules (but a way still widely used).  In principle, any
3016 target name could be special in this way if you break it in two and add
3017 both pieces to the suffix list.  In practice, suffixes normally begin with
3018 @samp{.}, so these special target names also begin with @samp{.}.
3019 @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
3021 @node Multiple Targets, Multiple Rules, Special Targets, Rules
3022 @section Multiple Targets in a Rule
3023 @cindex multiple targets
3024 @cindex several targets in a rule
3025 @cindex targets, multiple
3026 @cindex rule, with multiple targets
3028 A rule with multiple targets is equivalent to writing many rules, each with
3029 one target, and all identical aside from that.  The same commands apply to
3030 all the targets, but their effects may vary because you can substitute the
3031 actual target name into the command using @samp{$@@}.  The rule contributes
3032 the same prerequisites to all the targets also.
3034 This is useful in two cases.
3036 @itemize @bullet
3037 @item
3038 You want just prerequisites, no commands.  For example:
3040 @example
3041 kbd.o command.o files.o: command.h
3042 @end example
3044 @noindent
3045 gives an additional prerequisite to each of the three object files
3046 mentioned.
3048 @item
3049 Similar commands work for all the targets.  The commands do not need
3050 to be absolutely identical, since the automatic variable @samp{$@@}
3051 can be used to substitute the particular target to be remade into the
3052 commands (@pxref{Automatic Variables}).  For example:
3054 @example
3055 @group
3056 bigoutput littleoutput : text.g
3057         generate text.g -$(subst output,,$@@) > $@@
3058 @end group
3059 @end example
3060 @findex subst
3062 @noindent
3063 is equivalent to
3065 @example
3066 bigoutput : text.g
3067         generate text.g -big > bigoutput
3068 littleoutput : text.g
3069         generate text.g -little > littleoutput
3070 @end example
3072 @noindent
3073 Here we assume the hypothetical program @code{generate} makes two
3074 types of output, one if given @samp{-big} and one if given
3075 @samp{-little}.
3076 @xref{Text Functions, ,Functions for String Substitution and Analysis},
3077 for an explanation of the @code{subst} function.
3078 @end itemize
3080 Suppose you would like to vary the prerequisites according to the target,
3081 much as the variable @samp{$@@} allows you to vary the commands.
3082 You cannot do this with multiple targets in an ordinary rule, but you can
3083 do it with a @dfn{static pattern rule}.
3084 @xref{Static Pattern, ,Static Pattern Rules}.
3086 @node Multiple Rules, Static Pattern, Multiple Targets, Rules
3087 @section Multiple Rules for One Target
3088 @cindex multiple rules for one target
3089 @cindex several rules for one target
3090 @cindex rule, multiple for one target
3091 @cindex target, multiple rules for one
3093 One file can be the target of several rules.  All the prerequisites
3094 mentioned in all the rules are merged into one list of prerequisites for
3095 the target.  If the target is older than any prerequisite from any rule,
3096 the commands are executed.
3098 There can only be one set of commands to be executed for a file.  If
3099 more than one rule gives commands for the same file, @code{make} uses
3100 the last set given and prints an error message.  (As a special case,
3101 if the file's name begins with a dot, no error message is printed.
3102 This odd behavior is only for compatibility with other implementations
3103 of @code{make}... you should avoid using it).  Occasionally it is
3104 useful to have the same target invoke multiple commands which are
3105 defined in different parts of your makefile; you can use
3106 @dfn{double-colon rules} (@pxref{Double-Colon}) for this.
3108 An extra rule with just prerequisites can be used to give a few extra
3109 prerequisites to many files at once.  For example, makefiles often
3110 have a variable, such as @code{objects}, containing a list of all the
3111 compiler output files in the system being made.  An easy way to say
3112 that all of them must be recompiled if @file{config.h} changes is to
3113 write the following:
3115 @example
3116 objects = foo.o bar.o
3117 foo.o : defs.h
3118 bar.o : defs.h test.h
3119 $(objects) : config.h
3120 @end example
3122 This could be inserted or taken out without changing the rules that really
3123 specify how to make the object files, making it a convenient form to use if
3124 you wish to add the additional prerequisite intermittently.
3126 Another wrinkle is that the additional prerequisites could be specified with
3127 a variable that you set with a command argument to @code{make}
3128 (@pxref{Overriding, ,Overriding Variables}).  For example,
3130 @example
3131 @group
3132 extradeps=
3133 $(objects) : $(extradeps)
3134 @end group
3135 @end example
3137 @noindent
3138 means that the command @samp{make extradeps=foo.h} will consider
3139 @file{foo.h} as a prerequisite of each object file, but plain @samp{make}
3140 will not.
3142 If none of the explicit rules for a target has commands, then @code{make}
3143 searches for an applicable implicit rule to find some commands
3144 @pxref{Implicit Rules, ,Using Implicit Rules}).
3146 @node Static Pattern, Double-Colon, Multiple Rules, Rules
3147 @section Static Pattern Rules
3148 @cindex static pattern rule
3149 @cindex rule, static pattern
3150 @cindex pattern rules, static (not implicit)
3151 @cindex varying prerequisites
3152 @cindex prerequisites, varying (static pattern)
3154 @dfn{Static pattern rules} are rules which specify multiple targets and
3155 construct the prerequisite names for each target based on the target name.
3156 They are more general than ordinary rules with multiple targets because the
3157 targets do not have to have identical prerequisites.  Their prerequisites must
3158 be @emph{analogous}, but not necessarily @emph{identical}.
3160 @menu
3161 * Static Usage::                The syntax of static pattern rules.
3162 * Static versus Implicit::      When are they better than implicit rules?
3163 @end menu
3165 @node Static Usage, Static versus Implicit, Static Pattern, Static Pattern
3166 @subsection Syntax of Static Pattern Rules
3167 @cindex static pattern rule, syntax of
3168 @cindex pattern rules, static, syntax of
3170 Here is the syntax of a static pattern rule:
3172 @example
3173 @var{targets} @dots{}: @var{target-pattern}: @var{prereq-patterns} @dots{}
3174         @var{commands}
3175         @dots{}
3176 @end example
3178 @noindent
3179 The @var{targets} list specifies the targets that the rule applies to.
3180 The targets can contain wildcard characters, just like the targets of
3181 ordinary rules (@pxref{Wildcards, ,Using Wildcard Characters in File
3182 Names}).
3184 @cindex target pattern, static (not implicit)
3185 @cindex stem
3186 The @var{target-pattern} and @var{prereq-patterns} say how to compute the
3187 prerequisites of each target.  Each target is matched against the
3188 @var{target-pattern} to extract a part of the target name, called the
3189 @dfn{stem}.  This stem is substituted into each of the @var{prereq-patterns}
3190 to make the prerequisite names (one from each @var{prereq-pattern}).
3192 Each pattern normally contains the character @samp{%} just once.  When the
3193 @var{target-pattern} matches a target, the @samp{%} can match any part of
3194 the target name; this part is called the @dfn{stem}.  The rest of the
3195 pattern must match exactly.  For example, the target @file{foo.o} matches
3196 the pattern @samp{%.o}, with @samp{foo} as the stem.  The targets
3197 @file{foo.c} and @file{foo.out} do not match that pattern.@refill
3199 @cindex prerequisite pattern, static (not implicit)
3200 The prerequisite names for each target are made by substituting the stem
3201 for the @samp{%} in each prerequisite pattern.  For example, if one
3202 prerequisite pattern is @file{%.c}, then substitution of the stem
3203 @samp{foo} gives the prerequisite name @file{foo.c}.  It is legitimate
3204 to write a prerequisite pattern that does not contain @samp{%}; then this
3205 prerequisite is the same for all targets.
3207 @cindex @code{%}, quoting in static pattern
3208 @cindex @code{%}, quoting with @code{\} (backslash)
3209 @cindex @code{\} (backslash), to quote @code{%}
3210 @cindex backslash (@code{\}), to quote @code{%}
3211 @cindex quoting @code{%}, in static pattern
3212 @samp{%} characters in pattern rules can be quoted with preceding
3213 backslashes (@samp{\}).  Backslashes that would otherwise quote @samp{%}
3214 characters can be quoted with more backslashes.  Backslashes that quote
3215 @samp{%} characters or other backslashes are removed from the pattern
3216 before it is compared to file names or has a stem substituted into it.
3217 Backslashes that are not in danger of quoting @samp{%} characters go
3218 unmolested.  For example, the pattern @file{the\%weird\\%pattern\\} has
3219 @samp{the%weird\} preceding the operative @samp{%} character, and
3220 @samp{pattern\\} following it.  The final two backslashes are left alone
3221 because they cannot affect any @samp{%} character.@refill
3223 Here is an example, which compiles each of @file{foo.o} and @file{bar.o}
3224 from the corresponding @file{.c} file:
3226 @example
3227 @group
3228 objects = foo.o bar.o
3230 all: $(objects)
3232 $(objects): %.o: %.c
3233         $(CC) -c $(CFLAGS) $< -o $@@
3234 @end group
3235 @end example
3237 @noindent
3238 Here @samp{$<} is the automatic variable that holds the name of the
3239 prerequisite and @samp{$@@} is the automatic variable that holds the name
3240 of the target; see @ref{Automatic Variables}.
3242 Each target specified must match the target pattern; a warning is issued
3243 for each target that does not.  If you have a list of files, only some of
3244 which will match the pattern, you can use the @code{filter} function to
3245 remove nonmatching file names (@pxref{Text Functions, ,Functions for String Substitution and Analysis}):
3247 @example
3248 files = foo.elc bar.o lose.o
3250 $(filter %.o,$(files)): %.o: %.c
3251         $(CC) -c $(CFLAGS) $< -o $@@
3252 $(filter %.elc,$(files)): %.elc: %.el
3253         emacs -f batch-byte-compile $<
3254 @end example
3256 @noindent
3257 In this example the result of @samp{$(filter %.o,$(files))} is
3258 @file{bar.o lose.o}, and the first static pattern rule causes each of
3259 these object files to be updated by compiling the corresponding C source
3260 file.  The result of @w{@samp{$(filter %.elc,$(files))}} is
3261 @file{foo.elc}, so that file is made from @file{foo.el}.@refill
3263 Another example shows how to use @code{$*} in static pattern rules:
3264 @vindex $*@r{, and static pattern}
3266 @example
3267 @group
3268 bigoutput littleoutput : %output : text.g
3269         generate text.g -$* > $@@
3270 @end group
3271 @end example
3273 @noindent
3274 When the @code{generate} command is run, @code{$*} will expand to the
3275 stem, either @samp{big} or @samp{little}.
3277 @node Static versus Implicit,  , Static Usage, Static Pattern
3278 @subsection Static Pattern Rules versus Implicit Rules
3279 @cindex rule, static pattern versus implicit
3280 @cindex static pattern rule, versus implicit
3282 A static pattern rule has much in common with an implicit rule defined as a
3283 pattern rule (@pxref{Pattern Rules, ,Defining and Redefining Pattern Rules}).
3284 Both have a pattern for the target and patterns for constructing the
3285 names of prerequisites.  The difference is in how @code{make} decides
3286 @emph{when} the rule applies.
3288 An implicit rule @emph{can} apply to any target that matches its pattern,
3289 but it @emph{does} apply only when the target has no commands otherwise
3290 specified, and only when the prerequisites can be found.  If more than one
3291 implicit rule appears applicable, only one applies; the choice depends on
3292 the order of rules.
3294 By contrast, a static pattern rule applies to the precise list of targets
3295 that you specify in the rule.  It cannot apply to any other target and it
3296 invariably does apply to each of the targets specified.  If two conflicting
3297 rules apply, and both have commands, that's an error.
3299 The static pattern rule can be better than an implicit rule for these
3300 reasons:
3302 @itemize @bullet
3303 @item
3304 You may wish to override the usual implicit rule for a few
3305 files whose names cannot be categorized syntactically but
3306 can be given in an explicit list.
3308 @item
3309 If you cannot be sure of the precise contents of the directories
3310 you are using, you may not be sure which other irrelevant files
3311 might lead @code{make} to use the wrong implicit rule.  The choice
3312 might depend on the order in which the implicit rule search is done.
3313 With static pattern rules, there is no uncertainty: each rule applies
3314 to precisely the targets specified.
3315 @end itemize
3317 @node Double-Colon, Automatic Prerequisites, Static Pattern, Rules
3318 @section Double-Colon Rules
3319 @cindex double-colon rules
3320 @cindex rule, double-colon (@code{::})
3321 @cindex multiple rules for one target (@code{::})
3322 @cindex @code{::} rules (double-colon)
3324 @dfn{Double-colon} rules are rules written with @samp{::} instead of
3325 @samp{:} after the target names.  They are handled differently from
3326 ordinary rules when the same target appears in more than one rule.
3328 When a target appears in multiple rules, all the rules must be the same
3329 type: all ordinary, or all double-colon.  If they are double-colon, each
3330 of them is independent of the others.  Each double-colon rule's commands
3331 are executed if the target is older than any prerequisites of that rule.
3332 If there are no prerequisites for that rule, its commands are always
3333 executed (even if the target already exists).  This can result in
3334 executing none, any, or all of the double-colon rules.
3336 Double-colon rules with the same target are in fact completely separate
3337 from one another.  Each double-colon rule is processed individually, just
3338 as rules with different targets are processed.
3340 The double-colon rules for a target are executed in the order they appear
3341 in the makefile.  However, the cases where double-colon rules really make
3342 sense are those where the order of executing the commands would not matter.
3344 Double-colon rules are somewhat obscure and not often very useful; they
3345 provide a mechanism for cases in which the method used to update a target
3346 differs depending on which prerequisite files caused the update, and such
3347 cases are rare.
3349 Each double-colon rule should specify commands; if it does not, an
3350 implicit rule will be used if one applies.
3351 @xref{Implicit Rules, ,Using Implicit Rules}.
3353 @node Automatic Prerequisites,  , Double-Colon, Rules
3354 @section Generating Prerequisites Automatically
3355 @cindex prerequisites, automatic generation
3356 @cindex automatic generation of prerequisites
3357 @cindex generating prerequisites automatically
3359 In the makefile for a program, many of the rules you need to write often
3360 say only that some object file depends on some header
3361 file.  For example, if @file{main.c} uses @file{defs.h} via an
3362 @code{#include}, you would write:
3364 @example
3365 main.o: defs.h
3366 @end example
3368 @noindent
3369 You need this rule so that @code{make} knows that it must remake
3370 @file{main.o} whenever @file{defs.h} changes.  You can see that for a
3371 large program you would have to write dozens of such rules in your
3372 makefile.  And, you must always be very careful to update the makefile
3373 every time you add or remove an @code{#include}.
3374 @cindex @code{#include}
3376 @cindex @code{-M} (to compiler)
3377 To avoid this hassle, most modern C compilers can write these rules for
3378 you, by looking at the @code{#include} lines in the source files.
3379 Usually this is done with the @samp{-M} option to the compiler.
3380 For example, the command:
3382 @example
3383 cc -M main.c
3384 @end example
3386 @noindent
3387 generates the output:
3389 @example
3390 main.o : main.c defs.h
3391 @end example
3393 @noindent
3394 Thus you no longer have to write all those rules yourself.
3395 The compiler will do it for you.
3397 Note that such a prerequisite constitutes mentioning @file{main.o} in a
3398 makefile, so it can never be considered an intermediate file by implicit
3399 rule search.  This means that @code{make} won't ever remove the file
3400 after using it; @pxref{Chained Rules, ,Chains of Implicit Rules}.
3402 @cindex @code{make depend}
3403 With old @code{make} programs, it was traditional practice to use this
3404 compiler feature to generate prerequisites on demand with a command like
3405 @samp{make depend}.  That command would create a file @file{depend}
3406 containing all the automatically-generated prerequisites; then the
3407 makefile could use @code{include} to read them in (@pxref{Include}).
3409 In GNU @code{make}, the feature of remaking makefiles makes this
3410 practice obsolete---you need never tell @code{make} explicitly to
3411 regenerate the prerequisites, because it always regenerates any makefile
3412 that is out of date.  @xref{Remaking Makefiles}.
3414 The practice we recommend for automatic prerequisite generation is to have
3415 one makefile corresponding to each source file.  For each source file
3416 @file{@var{name}.c} there is a makefile @file{@var{name}.d} which lists
3417 what files the object file @file{@var{name}.o} depends on.  That way
3418 only the source files that have changed need to be rescanned to produce
3419 the new prerequisites.
3421 Here is the pattern rule to generate a file of prerequisites (i.e., a makefile)
3422 called @file{@var{name}.d} from a C source file called @file{@var{name}.c}:
3424 @smallexample
3425 @group
3426 %.d: %.c
3427         @@set -e; rm -f $@@; \
3428          $(CC) -M $(CPPFLAGS) $< > $@@.$$$$; \
3429          sed 's,\($*\)\.o[ :]*,\1.o $@@ : ,g' < $@@.$$$$ > $@@; \
3430          rm -f $@@.$$$$
3431 @end group
3432 @end smallexample
3434 @noindent
3435 @xref{Pattern Rules}, for information on defining pattern rules.  The
3436 @samp{-e} flag to the shell causes it to exit immediately if the
3437 @code{$(CC)} command (or any other command) fails (exits with a
3438 nonzero status).
3439 @cindex @code{-e} (shell flag)
3441 @cindex @code{-MM} (to GNU compiler)
3442 With the GNU C compiler, you may wish to use the @samp{-MM} flag instead
3443 of @samp{-M}.  This omits prerequisites on system header files.
3444 @xref{Preprocessor Options, , Options Controlling the Preprocessor,
3445 gcc.info, Using GNU CC}, for details.
3447 @cindex @code{sed} (shell command)
3448 The purpose of the @code{sed} command is to translate (for example):
3450 @example
3451 main.o : main.c defs.h
3452 @end example
3454 @noindent
3455 into:
3457 @example
3458 main.o main.d : main.c defs.h
3459 @end example
3461 @noindent
3462 @cindex @code{.d}
3463 This makes each @samp{.d} file depend on all the source and header files
3464 that the corresponding @samp{.o} file depends on.  @code{make} then
3465 knows it must regenerate the prerequisites whenever any of the source or
3466 header files changes.
3468 Once you've defined the rule to remake the @samp{.d} files,
3469 you then use the @code{include} directive to read them all in.
3470 @xref{Include}.  For example:
3472 @example
3473 @group
3474 sources = foo.c bar.c
3476 include $(sources:.c=.d)
3477 @end group
3478 @end example
3480 @noindent
3481 (This example uses a substitution variable reference to translate the
3482 list of source files @samp{foo.c bar.c} into a list of prerequisite
3483 makefiles, @samp{foo.d bar.d}.  @xref{Substitution Refs}, for full
3484 information on substitution references.)  Since the @samp{.d} files are
3485 makefiles like any others, @code{make} will remake them as necessary
3486 with no further work from you.  @xref{Remaking Makefiles}.
3488 Note that the @samp{.d} files contain target definitions; you should
3489 be sure to place the @code{include} directive @emph{after} the first,
3490 default goal in your makefiles or run the risk of having a random
3491 object file become the default goal.
3492 @xref{How Make Works}.
3494 @node Commands, Using Variables, Rules, Top
3495 @chapter Writing the Commands in Rules
3496 @cindex commands, how to write
3497 @cindex rule commands
3498 @cindex writing rule commands
3500 The commands of a rule consist of shell command lines to be executed one
3501 by one.  Each command line must start with a tab, except that the first
3502 command line may be attached to the target-and-prerequisites line with a
3503 semicolon in between.  Blank lines and lines of just comments may appear
3504 among the command lines; they are ignored.  (But beware, an apparently
3505 ``blank'' line that begins with a tab is @emph{not} blank!  It is an
3506 empty command; @pxref{Empty Commands}.)
3508 Users use many different shell programs, but commands in makefiles are
3509 always interpreted by @file{/bin/sh} unless the makefile specifies
3510 otherwise.  @xref{Execution, ,Command Execution}.
3512 @cindex comments, in commands
3513 @cindex commands, comments in
3514 @cindex @code{#} (comments), in commands
3515 The shell that is in use determines whether comments can be written on
3516 command lines, and what syntax they use.  When the shell is
3517 @file{/bin/sh}, a @samp{#} starts a comment that extends to the end of
3518 the line.  The @samp{#} does not have to be at the beginning of a line.
3519 Text on a line before a @samp{#} is not part of the comment.
3521 @menu
3522 * Echoing::                     How to control when commands are echoed.
3523 * Execution::                   How commands are executed.
3524 * Parallel::                    How commands can be executed in parallel.
3525 * Errors::                      What happens after a command execution error.
3526 * Interrupts::                  What happens when a command is interrupted.
3527 * Recursion::                   Invoking @code{make} from makefiles.
3528 * Sequences::                   Defining canned sequences of commands.
3529 * Empty Commands::              Defining useful, do-nothing commands.
3530 @end menu
3532 @node Echoing, Execution, Commands, Commands
3533 @section Command Echoing
3534 @cindex echoing of commands
3535 @cindex silent operation
3536 @cindex @code{@@} (in commands)
3537 @cindex commands, echoing
3538 @cindex printing of commands
3540 Normally @code{make} prints each command line before it is executed.
3541 We call this @dfn{echoing} because it gives the appearance that you
3542 are typing the commands yourself.
3544 When a line starts with @samp{@@}, the echoing of that line is suppressed.
3545 The @samp{@@} is discarded before the command is passed to the shell.
3546 Typically you would use this for a command whose only effect is to print
3547 something, such as an @code{echo} command to indicate progress through
3548 the makefile:
3550 @example
3551 @@echo About to make distribution files
3552 @end example
3554 @cindex @code{-n}
3555 @cindex @code{--just-print}
3556 @cindex @code{--dry-run}
3557 @cindex @code{--recon}
3558 When @code{make} is given the flag @samp{-n} or @samp{--just-print}
3559 it only echoes commands, it won't execute them.  @xref{Options Summary,
3560 ,Summary of Options}.  In this case and only this case, even the
3561 commands starting with @samp{@@} are printed.  This flag is useful for
3562 finding out which commands @code{make} thinks are necessary without
3563 actually doing them.
3565 @cindex @code{-s}
3566 @cindex @code{--silent}
3567 @cindex @code{--quiet}
3568 @findex .SILENT
3569 The @samp{-s} or @samp{--silent}
3570 flag to @code{make} prevents all echoing, as if all commands
3571 started with @samp{@@}.  A rule in the makefile for the special target
3572 @code{.SILENT} without prerequisites has the same effect
3573 (@pxref{Special Targets, ,Special Built-in Target Names}).
3574 @code{.SILENT} is essentially obsolete since @samp{@@} is more flexible.@refill
3576 @node Execution, Parallel, Echoing, Commands
3577 @section Command Execution
3578 @cindex commands, execution
3579 @cindex execution, of commands
3580 @cindex shell command, execution
3581 @vindex SHELL @r{(command execution)}
3583 When it is time to execute commands to update a target, they are executed
3584 by making a new subshell for each line.  (In practice, @code{make} may
3585 take shortcuts that do not affect the results.)
3587 @cindex @code{cd} (shell command)
3588 @strong{Please note:} this implies that shell commands such as
3589 @code{cd} that set variables local to each process will not affect the
3590 following command lines.@footnote{On MS-DOS, the value of current
3591 working directory is @strong{global}, so changing it @emph{will}
3592 affect the following command lines on those systems.}  If you want to
3593 use @code{cd} to affect the next command, put them on a single line.
3594 Then @code{make} will consider them a single command and pass them
3595 both to a single shell which will execute them in sequence.  For
3596 example:
3598 @example
3599 foo : bar/lose
3600         cd $(@@D) && gobble $(@@F) > ../$@@
3601 @end example
3603 @noindent
3604 Here we use the shell AND operator (@code{&&}) so that if the
3605 @code{cd} command fails, the script will fail without trying to invoke
3606 the @code{gobble} command in the wrong directory, which could very
3607 easily cause problems (in this case it would certainly cause
3608 @file{../foo} to be truncated, at least).
3610 @cindex commands, backslash (@code{\}) in
3611 @cindex commands, quoting newlines in
3612 @cindex backslash (@code{\}), in commands
3613 @cindex @code{\} (backslash), in commands
3614 @cindex quoting newline, in commands
3615 @cindex newline, quoting, in commands
3616 A shell command can be split into multiple lines of text by placing a
3617 backslash before each newline.  Such a sequence of lines is provided
3618 to the shell as a single command script.  The backslash and newline
3619 are preserved in the shell command.  If the first character on the
3620 line after a backslash-newline is a tab, the tab will @emph{not} be
3621 included in the shell command.  So, this makefile:
3623 @example
3624 @group
3625 all :
3626         @@echo no\
3627 space
3628         @@echo no\
3629         space
3630         @@echo one \
3631         space
3632         @@echo one\
3633          space
3634 @end group
3635 @end example
3637 @noindent
3638 consists of four separate shell commands where the output is:
3640 @example
3641 @group
3642 nospace
3643 nospace
3644 one space
3645 one space
3646 @end group
3647 @end example
3649 As a more complex example, this makefile:
3651 @example
3652 @group
3653 all : ; @@echo 'hello \
3654         world' ; echo "hello \
3655     world"
3656 @end group
3657 @end example
3659 @noindent
3660 will run one shell with a command script of:
3662 @example
3663 @group
3664 echo 'hello \
3665 world' ; echo "hello \
3666     world"
3667 @end group
3668 @end example
3670 @noindent
3671 which, according to shell quoting rules, will yield the following output:
3673 @example
3674 @group
3675 hello \
3676 world
3677 hello     world
3678 @end group
3679 @end example
3681 @noindent
3682 Notice how the backslash/newline pair was removed inside the string quoted
3683 with double quotes (@code{"..."}), but not from the string quoted with single
3684 quotes (@code{'...'}).  This is the way the default shell (@file{/bin/sh})
3685 handles backslash/newline pairs.  If you specify a different shell in your
3686 makefiles it may treat them differently.
3688 @vindex SHELL
3689 The program used as the shell is taken from the variable @code{SHELL}.
3690 By default, the program @file{/bin/sh} is used.
3692 @vindex COMSPEC
3693 On MS-DOS, if @code{SHELL} is not set, the value of the variable
3694 @code{COMSPEC} (which is always set) is used instead.
3696 @cindex @code{SHELL}, MS-DOS specifics
3697 The processing of lines that set the variable @code{SHELL} in Makefiles
3698 is different on MS-DOS.  The stock shell, @file{command.com}, is
3699 ridiculously limited in its functionality and many users of @code{make}
3700 tend to install a replacement shell.  Therefore, on MS-DOS, @code{make}
3701 examines the value of @code{SHELL}, and changes its behavior based on
3702 whether it points to a Unix-style or DOS-style shell.  This allows
3703 reasonable functionality even if @code{SHELL} points to
3704 @file{command.com}.
3706 If @code{SHELL} points to a Unix-style shell, @code{make} on MS-DOS
3707 additionally checks whether that shell can indeed be found; if not, it
3708 ignores the line that sets @code{SHELL}.  In MS-DOS, GNU @code{make}
3709 searches for the shell in the following places:
3711 @enumerate
3712 @item
3713 In the precise place pointed to by the value of @code{SHELL}.  For
3714 example, if the makefile specifies @samp{SHELL = /bin/sh}, @code{make}
3715 will look in the directory @file{/bin} on the current drive.
3717 @item
3718 In the current directory.
3720 @item
3721 In each of the directories in the @code{PATH} variable, in order.
3723 @end enumerate
3725 In every directory it examines, @code{make} will first look for the
3726 specific file (@file{sh} in the example above).  If this is not found,
3727 it will also look in that directory for that file with one of the known
3728 extensions which identify executable files.  For example @file{.exe},
3729 @file{.com}, @file{.bat}, @file{.btm}, @file{.sh}, and some others.
3731 If any of these attempts is successful, the value of @code{SHELL} will
3732 be set to the full pathname of the shell as found.  However, if none of
3733 these is found, the value of @code{SHELL} will not be changed, and thus
3734 the line that sets it will be effectively ignored.  This is so
3735 @code{make} will only support features specific to a Unix-style shell if
3736 such a shell is actually installed on the system where @code{make} runs.
3738 Note that this extended search for the shell is limited to the cases
3739 where @code{SHELL} is set from the Makefile; if it is set in the
3740 environment or command line, you are expected to set it to the full
3741 pathname of the shell, exactly as things are on Unix.
3743 The effect of the above DOS-specific processing is that a Makefile that
3744 says @samp{SHELL = /bin/sh} (as many Unix makefiles do), will work
3745 on MS-DOS unaltered if you have e.g.@: @file{sh.exe} installed in some
3746 directory along your @code{PATH}.
3748 @cindex environment, @code{SHELL} in
3749 @vindex MAKESHELL @r{(MS-DOS alternative to @code{SHELL})}
3750 Unlike most variables, the variable @code{SHELL} is never set from the
3751 environment.  This is because the @code{SHELL} environment variable is
3752 used to specify your personal choice of shell program for interactive
3753 use.  It would be very bad for personal choices like this to affect the
3754 functioning of makefiles.  @xref{Environment, ,Variables from the
3755 Environment}.  However, on MS-DOS and MS-Windows the value of
3756 @code{SHELL} in the environment @strong{is} used, since on those systems
3757 most users do not set this variable, and therefore it is most likely set
3758 specifically to be used by @code{make}.  On MS-DOS, if the setting of
3759 @code{SHELL} is not suitable for @code{make}, you can set the variable
3760 @code{MAKESHELL} to the shell that @code{make} should use; this will
3761 override the value of @code{SHELL}.
3763 @node Parallel, Errors, Execution, Commands
3764 @section Parallel Execution
3765 @cindex commands, execution in parallel
3766 @cindex parallel execution
3767 @cindex execution, in parallel
3768 @cindex job slots
3769 @cindex @code{-j}
3770 @cindex @code{--jobs}
3772 GNU @code{make} knows how to execute several commands at once.
3773 Normally, @code{make} will execute only one command at a time, waiting
3774 for it to finish before executing the next.  However, the @samp{-j} or
3775 @samp{--jobs} option tells @code{make} to execute many commands
3776 simultaneously.@refill
3778 On MS-DOS, the @samp{-j} option has no effect, since that system doesn't
3779 support multi-processing.
3781 If the @samp{-j} option is followed by an integer, this is the number of
3782 commands to execute at once; this is called the number of @dfn{job slots}.
3783 If there is nothing looking like an integer after the @samp{-j} option,
3784 there is no limit on the number of job slots.  The default number of job
3785 slots is one, which means serial execution (one thing at a time).
3787 One unpleasant consequence of running several commands simultaneously is
3788 that output generated by the commands appears whenever each command
3789 sends it, so messages from different commands may be interspersed.
3791 Another problem is that two processes cannot both take input from the
3792 same device; so to make sure that only one command tries to take input
3793 from the terminal at once, @code{make} will invalidate the standard
3794 input streams of all but one running command.  This means that
3795 attempting to read from standard input will usually be a fatal error (a
3796 @samp{Broken pipe} signal) for most child processes if there are
3797 several.
3798 @cindex broken pipe
3799 @cindex standard input
3801 It is unpredictable which command will have a valid standard input stream
3802 (which will come from the terminal, or wherever you redirect the standard
3803 input of @code{make}).  The first command run will always get it first, and
3804 the first command started after that one finishes will get it next, and so
3807 We will change how this aspect of @code{make} works if we find a better
3808 alternative.  In the mean time, you should not rely on any command using
3809 standard input at all if you are using the parallel execution feature; but
3810 if you are not using this feature, then standard input works normally in
3811 all commands.
3813 Finally, handling recursive @code{make} invocations raises issues.  For
3814 more information on this, see
3815 @ref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
3817 If a command fails (is killed by a signal or exits with a nonzero
3818 status), and errors are not ignored for that command
3819 (@pxref{Errors, ,Errors in Commands}),
3820 the remaining command lines to remake the same target will not be run.
3821 If a command fails and the @samp{-k} or @samp{--keep-going}
3822 option was not given
3823 (@pxref{Options Summary, ,Summary of Options}),
3824 @code{make} aborts execution.  If make
3825 terminates for any reason (including a signal) with child processes
3826 running, it waits for them to finish before actually exiting.@refill
3828 @cindex load average
3829 @cindex limiting jobs based on load
3830 @cindex jobs, limiting based on load
3831 @cindex @code{-l} (load average)
3832 @cindex @code{--max-load}
3833 @cindex @code{--load-average}
3834 When the system is heavily loaded, you will probably want to run fewer jobs
3835 than when it is lightly loaded.  You can use the @samp{-l} option to tell
3836 @code{make} to limit the number of jobs to run at once, based on the load
3837 average.  The @samp{-l} or @samp{--max-load}
3838 option is followed by a floating-point number.  For
3839 example,
3841 @example
3842 -l 2.5
3843 @end example
3845 @noindent
3846 will not let @code{make} start more than one job if the load average is
3847 above 2.5.  The @samp{-l} option with no following number removes the
3848 load limit, if one was given with a previous @samp{-l} option.@refill
3850 More precisely, when @code{make} goes to start up a job, and it already has
3851 at least one job running, it checks the current load average; if it is not
3852 lower than the limit given with @samp{-l}, @code{make} waits until the load
3853 average goes below that limit, or until all the other jobs finish.
3855 By default, there is no load limit.
3857 @node Errors, Interrupts, Parallel, Commands
3858 @section Errors in Commands
3859 @cindex errors (in commands)
3860 @cindex commands, errors in
3861 @cindex exit status (errors)
3863 After each shell command returns, @code{make} looks at its exit status.
3864 If the command completed successfully, the next command line is executed
3865 in a new shell; after the last command line is finished, the rule is
3866 finished.
3868 If there is an error (the exit status is nonzero), @code{make} gives up on
3869 the current rule, and perhaps on all rules.
3871 Sometimes the failure of a certain command does not indicate a problem.
3872 For example, you may use the @code{mkdir} command to ensure that a
3873 directory exists.  If the directory already exists, @code{mkdir} will
3874 report an error, but you probably want @code{make} to continue regardless.
3876 @cindex @code{-} (in commands)
3877 To ignore errors in a command line, write a @samp{-} at the beginning of
3878 the line's text (after the initial tab).  The @samp{-} is discarded before
3879 the command is passed to the shell for execution.
3881 For example,
3883 @example
3884 @group
3885 clean:
3886         -rm -f *.o
3887 @end group
3888 @end example
3889 @cindex @code{rm} (shell command)
3891 @noindent
3892 This causes @code{rm} to continue even if it is unable to remove a file.
3894 @cindex @code{-i}
3895 @cindex @code{--ignore-errors}
3896 @findex .IGNORE
3897 When you run @code{make} with the @samp{-i} or @samp{--ignore-errors}
3898 flag, errors are ignored in all commands of all rules.  A rule in the
3899 makefile for the special target @code{.IGNORE} has the same effect, if
3900 there are no prerequisites.  These ways of ignoring errors are obsolete
3901 because @samp{-} is more flexible.
3903 When errors are to be ignored, because of either a @samp{-} or the
3904 @samp{-i} flag, @code{make} treats an error return just like success,
3905 except that it prints out a message that tells you the status code
3906 the command exited with, and says that the error has been ignored.
3908 When an error happens that @code{make} has not been told to ignore,
3909 it implies that the current target cannot be correctly remade, and neither
3910 can any other that depends on it either directly or indirectly.  No further
3911 commands will be executed for these targets, since their preconditions
3912 have not been achieved.
3915 @cindex @code{-k}
3916 @cindex @code{--keep-going}
3917 Normally @code{make} gives up immediately in this circumstance, returning a
3918 nonzero status.  However, if the @samp{-k} or @samp{--keep-going}
3919 flag is specified, @code{make}
3920 continues to consider the other prerequisites of the pending targets,
3921 remaking them if necessary, before it gives up and returns nonzero status.
3922 For example, after an error in compiling one object file, @samp{make -k}
3923 will continue compiling other object files even though it already knows
3924 that linking them will be impossible.  @xref{Options Summary, ,Summary of Options}.
3926 The usual behavior assumes that your purpose is to get the specified
3927 targets up to date; once @code{make} learns that this is impossible, it
3928 might as well report the failure immediately.  The @samp{-k} option says
3929 that the real purpose is to test as many of the changes made in the
3930 program as possible, perhaps to find several independent problems so
3931 that you can correct them all before the next attempt to compile.  This
3932 is why Emacs' @code{compile} command passes the @samp{-k} flag by
3933 default.
3934 @cindex Emacs (@code{M-x compile})
3936 @findex .DELETE_ON_ERROR
3937 @cindex deletion of target files
3938 @cindex removal of target files
3939 @cindex target, deleting on error
3940 Usually when a command fails, if it has changed the target file at all,
3941 the file is corrupted and cannot be used---or at least it is not
3942 completely updated.  Yet the file's time stamp says that it is now up to
3943 date, so the next time @code{make} runs, it will not try to update that
3944 file.  The situation is just the same as when the command is killed by a
3945 signal; @pxref{Interrupts}.  So generally the right thing to do is to
3946 delete the target file if the command fails after beginning to change
3947 the file.  @code{make} will do this if @code{.DELETE_ON_ERROR} appears
3948 as a target.  This is almost always what you want @code{make} to do, but
3949 it is not historical practice; so for compatibility, you must explicitly
3950 request it.
3952 @node Interrupts, Recursion, Errors, Commands
3953 @section Interrupting or Killing @code{make}
3954 @cindex interrupt
3955 @cindex signal
3956 @cindex deletion of target files
3957 @cindex removal of target files
3958 @cindex target, deleting on interrupt
3959 @cindex killing (interruption)
3961 If @code{make} gets a fatal signal while a command is executing, it may
3962 delete the target file that the command was supposed to update.  This is
3963 done if the target file's last-modification time has changed since
3964 @code{make} first checked it.
3966 The purpose of deleting the target is to make sure that it is remade from
3967 scratch when @code{make} is next run.  Why is this?  Suppose you type
3968 @kbd{Ctrl-c} while a compiler is running, and it has begun to write an
3969 object file @file{foo.o}.  The @kbd{Ctrl-c} kills the compiler, resulting
3970 in an incomplete file whose last-modification time is newer than the source
3971 file @file{foo.c}.  But @code{make} also receives the @kbd{Ctrl-c} signal
3972 and deletes this incomplete file.  If @code{make} did not do this, the next
3973 invocation of @code{make} would think that @file{foo.o} did not require
3974 updating---resulting in a strange error message from the linker when it
3975 tries to link an object file half of which is missing.
3977 @findex .PRECIOUS
3978 You can prevent the deletion of a target file in this way by making the
3979 special target @code{.PRECIOUS} depend on it.  Before remaking a target,
3980 @code{make} checks to see whether it appears on the prerequisites of
3981 @code{.PRECIOUS}, and thereby decides whether the target should be deleted
3982 if a signal happens.  Some reasons why you might do this are that the
3983 target is updated in some atomic fashion, or exists only to record a
3984 modification-time (its contents do not matter), or must exist at all
3985 times to prevent other sorts of trouble.
3987 @node Recursion, Sequences, Interrupts, Commands
3988 @section Recursive Use of @code{make}
3989 @cindex recursion
3990 @cindex subdirectories, recursion for
3992 Recursive use of @code{make} means using @code{make} as a command in a
3993 makefile.  This technique is useful when you want separate makefiles for
3994 various subsystems that compose a larger system.  For example, suppose you
3995 have a subdirectory @file{subdir} which has its own makefile, and you would
3996 like the containing directory's makefile to run @code{make} on the
3997 subdirectory.  You can do it by writing this:
3999 @example
4000 subsystem:
4001         cd subdir && $(MAKE)
4002 @end example
4004 @noindent
4005 or, equivalently, this (@pxref{Options Summary, ,Summary of Options}):
4007 @example
4008 subsystem:
4009         $(MAKE) -C subdir
4010 @end example
4011 @cindex @code{-C}
4012 @cindex @code{--directory}
4014 You can write recursive @code{make} commands just by copying this example,
4015 but there are many things to know about how they work and why, and about
4016 how the sub-@code{make} relates to the top-level @code{make}.  You may
4017 also find it useful to declare targets that invoke recursive
4018 @code{make} commands as @samp{.PHONY} (for more discussion on when
4019 this is useful, see @ref{Phony Targets}).
4021 @vindex @code{CURDIR}
4022 For your convenience, when GNU @code{make} starts (after it has
4023 processed any @code{-C} options) it sets the variable @code{CURDIR} to
4024 the pathname of the current working directory.  This value is never
4025 touched by @code{make} again: in particular note that if you include
4026 files from other directories the value of @code{CURDIR} does not
4027 change.  The value has the same precedence it would have if it were
4028 set in the makefile (by default, an environment variable @code{CURDIR}
4029 will not override this value).  Note that setting this variable has no
4030 impact on the operation of @code{make} (it does not cause @code{make}
4031 to change its working directory, for example).
4033 @menu
4034 * MAKE Variable::               The special effects of using @samp{$(MAKE)}.
4035 * Variables/Recursion::         How to communicate variables to a sub-@code{make}.
4036 * Options/Recursion::           How to communicate options to a sub-@code{make}.
4037 * -w Option::                   How the @samp{-w} or @samp{--print-directory} option
4038                                   helps debug use of recursive @code{make} commands.
4039 @end menu
4041 @node MAKE Variable, Variables/Recursion, Recursion, Recursion
4042 @subsection How the @code{MAKE} Variable Works
4043 @vindex MAKE
4044 @cindex recursion, and @code{MAKE} variable
4046 Recursive @code{make} commands should always use the variable @code{MAKE},
4047 not the explicit command name @samp{make}, as shown here:
4049 @example
4050 @group
4051 subsystem:
4052         cd subdir && $(MAKE)
4053 @end group
4054 @end example
4056 The value of this variable is the file name with which @code{make} was
4057 invoked.  If this file name was @file{/bin/make}, then the command executed
4058 is @samp{cd subdir && /bin/make}.  If you use a special version of
4059 @code{make} to run the top-level makefile, the same special version will be
4060 executed for recursive invocations.
4061 @cindex @code{cd} (shell command)
4063 @cindex +, and commands
4064 As a special feature, using the variable @code{MAKE} in the commands of
4065 a rule alters the effects of the @samp{-t} (@samp{--touch}), @samp{-n}
4066 (@samp{--just-print}), or @samp{-q} (@w{@samp{--question}}) option.
4067 Using the @code{MAKE} variable has the same effect as using a @samp{+}
4068 character at the beginning of the command line.  @xref{Instead of
4069 Execution, ,Instead of Executing the Commands}.  This special feature
4070 is only enabled if the @code{MAKE} variable appears directly in the
4071 command script: it does not apply if the @code{MAKE} variable is
4072 referenced through expansion of another variable.  In the latter case
4073 you must use the @samp{+} token to get these special effects.@refill
4075 Consider the command @samp{make -t} in the above example.  (The
4076 @samp{-t} option marks targets as up to date without actually running
4077 any commands; see @ref{Instead of Execution}.)  Following the usual
4078 definition of @samp{-t}, a @samp{make -t} command in the example would
4079 create a file named @file{subsystem} and do nothing else.  What you
4080 really want it to do is run @samp{@w{cd subdir &&} @w{make -t}}; but that would
4081 require executing the command, and @samp{-t} says not to execute
4082 commands.@refill
4083 @cindex @code{-t}, and recursion
4084 @cindex recursion, and @code{-t}
4085 @cindex @code{--touch}, and recursion
4087 The special feature makes this do what you want: whenever a command
4088 line of a rule contains the variable @code{MAKE}, the flags @samp{-t},
4089 @samp{-n} and @samp{-q} do not apply to that line.  Command lines
4090 containing @code{MAKE} are executed normally despite the presence of a
4091 flag that causes most commands not to be run.  The usual
4092 @code{MAKEFLAGS} mechanism passes the flags to the sub-@code{make}
4093 (@pxref{Options/Recursion, ,Communicating Options to a
4094 Sub-@code{make}}), so your request to touch the files, or print the
4095 commands, is propagated to the subsystem.@refill
4097 @node Variables/Recursion, Options/Recursion, MAKE Variable, Recursion
4098 @subsection Communicating Variables to a Sub-@code{make}
4099 @cindex sub-@code{make}
4100 @cindex environment, and recursion
4101 @cindex exporting variables
4102 @cindex variables, environment
4103 @cindex variables, exporting
4104 @cindex recursion, and environment
4105 @cindex recursion, and variables
4107 Variable values of the top-level @code{make} can be passed to the
4108 sub-@code{make} through the environment by explicit request.  These
4109 variables are defined in the sub-@code{make} as defaults, but do not
4110 override what is specified in the makefile used by the sub-@code{make}
4111 makefile unless you use the @samp{-e} switch (@pxref{Options Summary,
4112 ,Summary of Options}).@refill
4114 To pass down, or @dfn{export}, a variable, @code{make} adds the variable
4115 and its value to the environment for running each command.  The
4116 sub-@code{make}, in turn, uses the environment to initialize its table
4117 of variable values.  @xref{Environment, ,Variables from the
4118 Environment}.
4120 Except by explicit request, @code{make} exports a variable only if it
4121 is either defined in the environment initially or set on the command
4122 line, and if its name consists only of letters, numbers, and underscores.
4123 Some shells cannot cope with environment variable names consisting of
4124 characters other than letters, numbers, and underscores.
4126 @cindex SHELL, exported value
4127 The value of the @code{make} variable @code{SHELL} is not exported.
4128 Instead, the value of the @code{SHELL} variable from the invoking
4129 environment is passed to the sub-@code{make}.  You can force
4130 @code{make} to export its value for @code{SHELL} by using the
4131 @code{export} directive, described below.
4133 The special variable @code{MAKEFLAGS} is always exported (unless you
4134 unexport it).  @code{MAKEFILES} is exported if you set it to anything.
4136 @code{make} automatically passes down variable values that were defined
4137 on the command line, by putting them in the @code{MAKEFLAGS} variable.
4138 @iftex
4139 See the next section.
4140 @end iftex
4141 @ifnottex
4142 @xref{Options/Recursion}.
4143 @end ifnottex
4145 Variables are @emph{not} normally passed down if they were created by
4146 default by @code{make} (@pxref{Implicit Variables, ,Variables Used by
4147 Implicit Rules}).  The sub-@code{make} will define these for
4148 itself.@refill
4150 @findex export
4151 If you want to export specific variables to a sub-@code{make}, use the
4152 @code{export} directive, like this:
4154 @example
4155 export @var{variable} @dots{}
4156 @end example
4158 @noindent
4159 @findex unexport
4160 If you want to @emph{prevent} a variable from being exported, use the
4161 @code{unexport} directive, like this:
4163 @example
4164 unexport @var{variable} @dots{}
4165 @end example
4167 @noindent
4168 In both of these forms, the arguments to @code{export} and
4169 @code{unexport} are expanded, and so could be variables or functions
4170 which expand to a (list of) variable names to be (un)exported.
4172 As a convenience, you can define a variable and export it at the same
4173 time by doing:
4175 @example
4176 export @var{variable} = value
4177 @end example
4179 @noindent
4180 has the same result as:
4182 @example
4183 @var{variable} = value
4184 export @var{variable}
4185 @end example
4187 @noindent
4190 @example
4191 export @var{variable} := value
4192 @end example
4194 @noindent
4195 has the same result as:
4197 @example
4198 @var{variable} := value
4199 export @var{variable}
4200 @end example
4202 Likewise,
4204 @example
4205 export @var{variable} += value
4206 @end example
4208 @noindent
4209 is just like:
4211 @example
4212 @var{variable} += value
4213 export @var{variable}
4214 @end example
4216 @noindent
4217 @xref{Appending, ,Appending More Text to Variables}.
4219 You may notice that the @code{export} and @code{unexport} directives
4220 work in @code{make} in the same way they work in the shell, @code{sh}.
4222 If you want all variables to be exported by default, you can use
4223 @code{export} by itself:
4225 @example
4226 export
4227 @end example
4229 @noindent
4230 This tells @code{make} that variables which are not explicitly mentioned
4231 in an @code{export} or @code{unexport} directive should be exported.
4232 Any variable given in an @code{unexport} directive will still @emph{not}
4233 be exported.  If you use @code{export} by itself to export variables by
4234 default, variables whose names contain characters other than
4235 alphanumerics and underscores will not be exported unless specifically
4236 mentioned in an @code{export} directive.@refill
4238 @findex .EXPORT_ALL_VARIABLES
4239 The behavior elicited by an @code{export} directive by itself was the
4240 default in older versions of GNU @code{make}.  If your makefiles depend
4241 on this behavior and you want to be compatible with old versions of
4242 @code{make}, you can write a rule for the special target
4243 @code{.EXPORT_ALL_VARIABLES} instead of using the @code{export} directive.
4244 This will be ignored by old @code{make}s, while the @code{export}
4245 directive will cause a syntax error.@refill
4246 @cindex compatibility in exporting
4248 Likewise, you can use @code{unexport} by itself to tell @code{make}
4249 @emph{not} to export variables by default.  Since this is the default
4250 behavior, you would only need to do this if @code{export} had been used
4251 by itself earlier (in an included makefile, perhaps).  You
4252 @strong{cannot} use @code{export} and @code{unexport} by themselves to
4253 have variables exported for some commands and not for others.  The last
4254 @code{export} or @code{unexport} directive that appears by itself
4255 determines the behavior for the entire run of @code{make}.@refill
4257 @vindex MAKELEVEL
4258 @cindex recursion, level of
4259 As a special feature, the variable @code{MAKELEVEL} is changed when it
4260 is passed down from level to level.  This variable's value is a string
4261 which is the depth of the level as a decimal number.  The value is
4262 @samp{0} for the top-level @code{make}; @samp{1} for a sub-@code{make},
4263 @samp{2} for a sub-sub-@code{make}, and so on.  The incrementation
4264 happens when @code{make} sets up the environment for a command.@refill
4266 The main use of @code{MAKELEVEL} is to test it in a conditional
4267 directive (@pxref{Conditionals, ,Conditional Parts of Makefiles}); this
4268 way you can write a makefile that behaves one way if run recursively and
4269 another way if run directly by you.@refill
4271 @vindex MAKEFILES
4272 You can use the variable @code{MAKEFILES} to cause all sub-@code{make}
4273 commands to use additional makefiles.  The value of @code{MAKEFILES} is
4274 a whitespace-separated list of file names.  This variable, if defined in
4275 the outer-level makefile, is passed down through the environment; then
4276 it serves as a list of extra makefiles for the sub-@code{make} to read
4277 before the usual or specified ones.  @xref{MAKEFILES Variable, ,The
4278 Variable @code{MAKEFILES}}.@refill
4280 @node Options/Recursion, -w Option, Variables/Recursion, Recursion
4281 @subsection Communicating Options to a Sub-@code{make}
4282 @cindex options, and recursion
4283 @cindex recursion, and options
4285 @vindex MAKEFLAGS
4286 Flags such as @samp{-s} and @samp{-k} are passed automatically to the
4287 sub-@code{make} through the variable @code{MAKEFLAGS}.  This variable is
4288 set up automatically by @code{make} to contain the flag letters that
4289 @code{make} received.  Thus, if you do @w{@samp{make -ks}} then
4290 @code{MAKEFLAGS} gets the value @samp{ks}.@refill
4292 As a consequence, every sub-@code{make} gets a value for @code{MAKEFLAGS}
4293 in its environment.  In response, it takes the flags from that value and
4294 processes them as if they had been given as arguments.
4295 @xref{Options Summary, ,Summary of Options}.
4297 @cindex command line variable definitions, and recursion
4298 @cindex variables, command line, and recursion
4299 @cindex recursion, and command line variable definitions
4300 Likewise variables defined on the command line are passed to the
4301 sub-@code{make} through @code{MAKEFLAGS}.  Words in the value of
4302 @code{MAKEFLAGS} that contain @samp{=}, @code{make} treats as variable
4303 definitions just as if they appeared on the command line.
4304 @xref{Overriding, ,Overriding Variables}.
4306 @cindex @code{-C}, and recursion
4307 @cindex @code{-f}, and recursion
4308 @cindex @code{-o}, and recursion
4309 @cindex @code{-W}, and recursion
4310 @cindex @code{--directory}, and recursion
4311 @cindex @code{--file}, and recursion
4312 @cindex @code{--old-file}, and recursion
4313 @cindex @code{--assume-old}, and recursion
4314 @cindex @code{--assume-new}, and recursion
4315 @cindex @code{--new-file}, and recursion
4316 @cindex recursion, and @code{-C}
4317 @cindex recursion, and @code{-f}
4318 @cindex recursion, and @code{-o}
4319 @cindex recursion, and @code{-W}
4320 The options @samp{-C}, @samp{-f}, @samp{-o}, and @samp{-W} are not put
4321 into @code{MAKEFLAGS}; these options are not passed down.@refill
4323 @cindex @code{-j}, and recursion
4324 @cindex @code{--jobs}, and recursion
4325 @cindex recursion, and @code{-j}
4326 @cindex job slots, and recursion
4327 The @samp{-j} option is a special case (@pxref{Parallel, ,Parallel Execution}).
4328 If you set it to some numeric value @samp{N} and your operating system
4329 supports it (most any UNIX system will; others typically won't), the
4330 parent @code{make} and all the sub-@code{make}s will communicate to
4331 ensure that there are only @samp{N} jobs running at the same time
4332 between them all.  Note that any job that is marked recursive
4333 (@pxref{Instead of Execution, ,Instead of Executing the Commands})
4334 doesn't count against the total jobs (otherwise we could get @samp{N}
4335 sub-@code{make}s running and have no slots left over for any real work!)
4337 If your operating system doesn't support the above communication, then
4338 @samp{-j 1} is always put into @code{MAKEFLAGS} instead of the value you
4339 specified.  This is because if the @w{@samp{-j}} option were passed down
4340 to sub-@code{make}s, you would get many more jobs running in parallel
4341 than you asked for.  If you give @samp{-j} with no numeric argument,
4342 meaning to run as many jobs as possible in parallel, this is passed
4343 down, since multiple infinities are no more than one.@refill
4345 If you do not want to pass the other flags down, you must change the
4346 value of @code{MAKEFLAGS}, like this:
4348 @example
4349 subsystem:
4350         cd subdir && $(MAKE) MAKEFLAGS=
4351 @end example
4353 @vindex MAKEOVERRIDES
4354 The command line variable definitions really appear in the variable
4355 @code{MAKEOVERRIDES}, and @code{MAKEFLAGS} contains a reference to this
4356 variable.  If you do want to pass flags down normally, but don't want to
4357 pass down the command line variable definitions, you can reset
4358 @code{MAKEOVERRIDES} to empty, like this:
4360 @example
4361 MAKEOVERRIDES =
4362 @end example
4364 @noindent
4365 @cindex Arg list too long
4366 @cindex E2BIG
4367 This is not usually useful to do.  However, some systems have a small
4368 fixed limit on the size of the environment, and putting so much
4369 information into the value of @code{MAKEFLAGS} can exceed it.  If you
4370 see the error message @samp{Arg list too long}, this may be the problem.
4371 @findex .POSIX
4372 @cindex POSIX.2
4373 (For strict compliance with POSIX.2, changing @code{MAKEOVERRIDES} does
4374 not affect @code{MAKEFLAGS} if the special target @samp{.POSIX} appears
4375 in the makefile.  You probably do not care about this.)
4377 @vindex MFLAGS
4378 A similar variable @code{MFLAGS} exists also, for historical
4379 compatibility.  It has the same value as @code{MAKEFLAGS} except that it
4380 does not contain the command line variable definitions, and it always
4381 begins with a hyphen unless it is empty (@code{MAKEFLAGS} begins with a
4382 hyphen only when it begins with an option that has no single-letter
4383 version, such as @samp{--warn-undefined-variables}).  @code{MFLAGS} was
4384 traditionally used explicitly in the recursive @code{make} command, like
4385 this:
4387 @example
4388 subsystem:
4389         cd subdir && $(MAKE) $(MFLAGS)
4390 @end example
4392 @noindent
4393 but now @code{MAKEFLAGS} makes this usage redundant.  If you want your
4394 makefiles to be compatible with old @code{make} programs, use this
4395 technique; it will work fine with more modern @code{make} versions too.
4397 @cindex setting options from environment
4398 @cindex options, setting from environment
4399 @cindex setting options in makefiles
4400 @cindex options, setting in makefiles
4401 The @code{MAKEFLAGS} variable can also be useful if you want to have
4402 certain options, such as @samp{-k} (@pxref{Options Summary, ,Summary of
4403 Options}), set each time you run @code{make}.  You simply put a value for
4404 @code{MAKEFLAGS} in your environment.  You can also set @code{MAKEFLAGS} in
4405 a makefile, to specify additional flags that should also be in effect for
4406 that makefile.  (Note that you cannot use @code{MFLAGS} this way.  That
4407 variable is set only for compatibility; @code{make} does not interpret a
4408 value you set for it in any way.)
4410 When @code{make} interprets the value of @code{MAKEFLAGS} (either from the
4411 environment or from a makefile), it first prepends a hyphen if the value
4412 does not already begin with one.  Then it chops the value into words
4413 separated by blanks, and parses these words as if they were options given
4414 on the command line (except that @samp{-C}, @samp{-f}, @samp{-h},
4415 @samp{-o}, @samp{-W}, and their long-named versions are ignored; and there
4416 is no error for an invalid option).
4418 If you do put @code{MAKEFLAGS} in your environment, you should be sure not
4419 to include any options that will drastically affect the actions of
4420 @code{make} and undermine the purpose of makefiles and of @code{make}
4421 itself.  For instance, the @samp{-t}, @samp{-n}, and @samp{-q} options, if
4422 put in one of these variables, could have disastrous consequences and would
4423 certainly have at least surprising and probably annoying effects.@refill
4425 @node -w Option,  , Options/Recursion, Recursion
4426 @subsection The @samp{--print-directory} Option
4427 @cindex directories, printing them
4428 @cindex printing directories
4429 @cindex recursion, and printing directories
4431 If you use several levels of recursive @code{make} invocations, the
4432 @samp{-w} or @w{@samp{--print-directory}} option can make the output a
4433 lot easier to understand by showing each directory as @code{make}
4434 starts processing it and as @code{make} finishes processing it.  For
4435 example, if @samp{make -w} is run in the directory @file{/u/gnu/make},
4436 @code{make} will print a line of the form:@refill
4438 @example
4439 make: Entering directory `/u/gnu/make'.
4440 @end example
4442 @noindent
4443 before doing anything else, and a line of the form:
4445 @example
4446 make: Leaving directory `/u/gnu/make'.
4447 @end example
4449 @noindent
4450 when processing is completed.
4452 @cindex @code{-C}, and @code{-w}
4453 @cindex @code{--directory}, and @code{--print-directory}
4454 @cindex recursion, and @code{-w}
4455 @cindex @code{-w}, and @code{-C}
4456 @cindex @code{-w}, and recursion
4457 @cindex @code{--print-directory}, and @code{--directory}
4458 @cindex @code{--print-directory}, and recursion
4459 @cindex @code{--no-print-directory}
4460 @cindex @code{--print-directory}, disabling
4461 @cindex @code{-w}, disabling
4462 Normally, you do not need to specify this option because @samp{make}
4463 does it for you: @samp{-w} is turned on automatically when you use the
4464 @samp{-C} option, and in sub-@code{make}s.  @code{make} will not
4465 automatically turn on @samp{-w} if you also use @samp{-s}, which says to
4466 be silent, or if you use @samp{--no-print-directory} to explicitly
4467 disable it.
4469 @node Sequences, Empty Commands, Recursion, Commands
4470 @section Defining Canned Command Sequences
4471 @cindex sequences of commands
4472 @cindex commands, sequences of
4474 When the same sequence of commands is useful in making various targets, you
4475 can define it as a canned sequence with the @code{define} directive, and
4476 refer to the canned sequence from the rules for those targets.  The canned
4477 sequence is actually a variable, so the name must not conflict with other
4478 variable names.
4480 Here is an example of defining a canned sequence of commands:
4482 @example
4483 define run-yacc
4484 yacc $(firstword $^)
4485 mv y.tab.c $@@
4486 endef
4487 @end example
4488 @cindex @code{yacc}
4490 @noindent
4491 Here @code{run-yacc} is the name of the variable being defined;
4492 @code{endef} marks the end of the definition; the lines in between are the
4493 commands.  The @code{define} directive does not expand variable references
4494 and function calls in the canned sequence; the @samp{$} characters,
4495 parentheses, variable names, and so on, all become part of the value of the
4496 variable you are defining.
4497 @xref{Defining, ,Defining Variables Verbatim},
4498 for a complete explanation of @code{define}.
4500 The first command in this example runs Yacc on the first prerequisite of
4501 whichever rule uses the canned sequence.  The output file from Yacc is
4502 always named @file{y.tab.c}.  The second command moves the output to the
4503 rule's target file name.
4505 To use the canned sequence, substitute the variable into the commands of a
4506 rule.  You can substitute it like any other variable
4507 (@pxref{Reference, ,Basics of Variable References}).
4508 Because variables defined by @code{define} are recursively expanded
4509 variables, all the variable references you wrote inside the @code{define}
4510 are expanded now.  For example:
4512 @example
4513 foo.c : foo.y
4514         $(run-yacc)
4515 @end example
4517 @noindent
4518 @samp{foo.y} will be substituted for the variable @samp{$^} when it occurs in
4519 @code{run-yacc}'s value, and @samp{foo.c} for @samp{$@@}.@refill
4521 This is a realistic example, but this particular one is not needed in
4522 practice because @code{make} has an implicit rule to figure out these
4523 commands based on the file names involved
4524 (@pxref{Implicit Rules, ,Using Implicit Rules}).
4526 @cindex @@, and @code{define}
4527 @cindex -, and @code{define}
4528 @cindex +, and @code{define}
4529 In command execution, each line of a canned sequence is treated just as
4530 if the line appeared on its own in the rule, preceded by a tab.  In
4531 particular, @code{make} invokes a separate subshell for each line.  You
4532 can use the special prefix characters that affect command lines
4533 (@samp{@@}, @samp{-}, and @samp{+}) on each line of a canned sequence.
4534 @xref{Commands, ,Writing the Commands in Rules}.
4535 For example, using this canned sequence:
4537 @example
4538 define frobnicate
4539 @@echo "frobnicating target $@@"
4540 frob-step-1 $< -o $@@-step-1
4541 frob-step-2 $@@-step-1 -o $@@
4542 endef
4543 @end example
4545 @noindent
4546 @code{make} will not echo the first line, the @code{echo} command.
4547 But it @emph{will} echo the following two command lines.
4549 On the other hand, prefix characters on the command line that refers to
4550 a canned sequence apply to every line in the sequence.  So the rule:
4552 @example
4553 frob.out: frob.in
4554         @@$(frobnicate)
4555 @end example
4557 @noindent
4558 does not echo @emph{any} commands.
4559 (@xref{Echoing, ,Command Echoing}, for a full explanation of @samp{@@}.)
4561 @node Empty Commands,  , Sequences, Commands
4562 @section Using Empty Commands
4563 @cindex empty commands
4564 @cindex commands, empty
4566 It is sometimes useful to define commands which do nothing.  This is done
4567 simply by giving a command that consists of nothing but whitespace.  For
4568 example:
4570 @example
4571 target: ;
4572 @end example
4574 @noindent
4575 defines an empty command string for @file{target}.  You could also use a
4576 line beginning with a tab character to define an empty command string,
4577 but this would be confusing because such a line looks empty.
4579 @findex .DEFAULT@r{, and empty commands}
4580 You may be wondering why you would want to define a command string that
4581 does nothing.  The only reason this is useful is to prevent a target
4582 from getting implicit commands (from implicit rules or the
4583 @code{.DEFAULT} special target; @pxref{Implicit Rules} and
4584 @pxref{Last Resort, ,Defining Last-Resort Default Rules}).@refill
4586 @c !!! another reason is for canonical stamp files:
4587 @ignore
4588 @example
4589 foo: stamp-foo ;
4590 stamp-foo: foo.in
4591         create foo frm foo.in
4592         touch $@
4593 @end example
4594 @end ignore
4596 You may be inclined to define empty command strings for targets that are
4597 not actual files, but only exist so that their prerequisites can be
4598 remade.  However, this is not the best way to do that, because the
4599 prerequisites may not be remade properly if the target file actually does exist.
4600 @xref{Phony Targets, ,Phony Targets}, for a better way to do this.
4602 @node Using Variables, Conditionals, Commands, Top
4603 @chapter How to Use Variables
4604 @cindex variable
4605 @cindex value
4606 @cindex recursive variable expansion
4607 @cindex simple variable expansion
4609 A @dfn{variable} is a name defined in a makefile to represent a string
4610 of text, called the variable's @dfn{value}.  These values are
4611 substituted by explicit request into targets, prerequisites, commands,
4612 and other parts of the makefile.  (In some other versions of @code{make},
4613 variables are called @dfn{macros}.)
4614 @cindex macro
4616 Variables and functions in all parts of a makefile are expanded when
4617 read, except for the shell commands in rules, the right-hand sides of
4618 variable definitions using @samp{=}, and the bodies of variable
4619 definitions using the @code{define} directive.@refill
4621 Variables can represent lists of file names, options to pass to compilers,
4622 programs to run, directories to look in for source files, directories to
4623 write output in, or anything else you can imagine.
4625 A variable name may be any sequence of characters not containing @samp{:},
4626 @samp{#}, @samp{=}, or leading or trailing whitespace.  However,
4627 variable names containing characters other than letters, numbers, and
4628 underscores should be avoided, as they may be given special meanings in the
4629 future, and with some shells they cannot be passed through the environment to a
4630 sub-@code{make}
4631 (@pxref{Variables/Recursion, ,Communicating Variables to a Sub-@code{make}}).
4633 Variable names are case-sensitive.  The names @samp{foo}, @samp{FOO},
4634 and @samp{Foo} all refer to different variables.
4636 It is traditional to use upper case letters in variable names, but we
4637 recommend using lower case letters for variable names that serve internal
4638 purposes in the makefile, and reserving upper case for parameters that
4639 control implicit rules or for parameters that the user should override with
4640 command options (@pxref{Overriding, ,Overriding Variables}).
4642 A few variables have names that are a single punctuation character or
4643 just a few characters.  These are the @dfn{automatic variables}, and
4644 they have particular specialized uses.  @xref{Automatic Variables}.
4646 @menu
4647 * Reference::                   How to use the value of a variable.
4648 * Flavors::                     Variables come in two flavors.
4649 * Advanced::                    Advanced features for referencing a variable.
4650 * Values::                      All the ways variables get their values.
4651 * Setting::                     How to set a variable in the makefile.
4652 * Appending::                   How to append more text to the old value
4653                                   of a variable.
4654 * Override Directive::          How to set a variable in the makefile even if
4655                                   the user has set it with a command argument.
4656 * Defining::                    An alternate way to set a variable
4657                                   to a verbatim string.
4658 * Environment::                 Variable values can come from the environment.
4659 * Target-specific::             Variable values can be defined on a per-target
4660                                   basis.
4661 * Pattern-specific::            Target-specific variable values can be applied
4662                                   to a group of targets that match a pattern.
4663 @end menu
4665 @node Reference, Flavors, Using Variables, Using Variables
4666 @section Basics of Variable References
4667 @cindex variables, how to reference
4668 @cindex reference to variables
4669 @cindex @code{$}, in variable reference
4670 @cindex dollar sign (@code{$}), in variable reference
4672 To substitute a variable's value, write a dollar sign followed by the name
4673 of the variable in parentheses or braces: either @samp{$(foo)} or
4674 @samp{$@{foo@}} is a valid reference to the variable @code{foo}.  This
4675 special significance of @samp{$} is why you must write @samp{$$} to have
4676 the effect of a single dollar sign in a file name or command.
4678 Variable references can be used in any context: targets, prerequisites,
4679 commands, most directives, and new variable values.  Here is an
4680 example of a common case, where a variable holds the names of all the
4681 object files in a program:
4683 @example
4684 @group
4685 objects = program.o foo.o utils.o
4686 program : $(objects)
4687         cc -o program $(objects)
4689 $(objects) : defs.h
4690 @end group
4691 @end example
4693 Variable references work by strict textual substitution.  Thus, the rule
4695 @example
4696 @group
4697 foo = c
4698 prog.o : prog.$(foo)
4699         $(foo)$(foo) -$(foo) prog.$(foo)
4700 @end group
4701 @end example
4703 @noindent
4704 could be used to compile a C program @file{prog.c}.  Since spaces before
4705 the variable value are ignored in variable assignments, the value of
4706 @code{foo} is precisely @samp{c}.  (Don't actually write your makefiles
4707 this way!)
4709 A dollar sign followed by a character other than a dollar sign,
4710 open-parenthesis or open-brace treats that single character as the
4711 variable name.  Thus, you could reference the variable @code{x} with
4712 @samp{$x}.  However, this practice is strongly discouraged, except in
4713 the case of the automatic variables (@pxref{Automatic Variables}).
4715 @node Flavors, Advanced, Reference, Using Variables
4716 @section The Two Flavors of Variables
4717 @cindex flavors of variables
4718 @cindex recursive variable expansion
4719 @cindex variables, flavors
4720 @cindex recursively expanded variables
4721 @cindex variables, recursively expanded
4723 There are two ways that a variable in GNU @code{make} can have a value;
4724 we call them the two @dfn{flavors} of variables.  The two flavors are
4725 distinguished in how they are defined and in what they do when expanded.
4727 @cindex =
4728 The first flavor of variable is a @dfn{recursively expanded} variable.
4729 Variables of this sort are defined by lines using @samp{=}
4730 (@pxref{Setting, ,Setting Variables}) or by the @code{define} directive
4731 (@pxref{Defining, ,Defining Variables Verbatim}).  The value you specify
4732 is installed verbatim; if it contains references to other variables,
4733 these references are expanded whenever this variable is substituted (in
4734 the course of expanding some other string).  When this happens, it is
4735 called @dfn{recursive expansion}.@refill
4737 For example,
4739 @example
4740 foo = $(bar)
4741 bar = $(ugh)
4742 ugh = Huh?
4744 all:;echo $(foo)
4745 @end example
4747 @noindent
4748 will echo @samp{Huh?}: @samp{$(foo)} expands to @samp{$(bar)} which
4749 expands to @samp{$(ugh)} which finally expands to @samp{Huh?}.@refill
4751 This flavor of variable is the only sort supported by other versions of
4752 @code{make}.  It has its advantages and its disadvantages.  An advantage
4753 (most would say) is that:
4755 @example
4756 CFLAGS = $(include_dirs) -O
4757 include_dirs = -Ifoo -Ibar
4758 @end example
4760 @noindent
4761 will do what was intended: when @samp{CFLAGS} is expanded in a command,
4762 it will expand to @samp{-Ifoo -Ibar -O}.  A major disadvantage is that you
4763 cannot append something on the end of a variable, as in
4765 @example
4766 CFLAGS = $(CFLAGS) -O
4767 @end example
4769 @noindent
4770 because it will cause an infinite loop in the variable expansion.
4771 (Actually @code{make} detects the infinite loop and reports an error.)
4772 @cindex loops in variable expansion
4773 @cindex variables, loops in expansion
4775 Another disadvantage is that any functions
4776 (@pxref{Functions, ,Functions for Transforming Text})
4777 referenced in the definition will be executed every time the variable is
4778 expanded.  This makes @code{make} run slower; worse, it causes the
4779 @code{wildcard} and @code{shell} functions to give unpredictable results
4780 because you cannot easily control when they are called, or even how many
4781 times.
4783 To avoid all the problems and inconveniences of recursively expanded
4784 variables, there is another flavor: simply expanded variables.
4786 @cindex simply expanded variables
4787 @cindex variables, simply expanded
4788 @cindex :=
4789 @dfn{Simply expanded variables} are defined by lines using @samp{:=}
4790 (@pxref{Setting, ,Setting Variables}).
4791 The value of a simply expanded variable is scanned
4792 once and for all, expanding any references to other variables and
4793 functions, when the variable is defined.  The actual value of the simply
4794 expanded variable is the result of expanding the text that you write.
4795 It does not contain any references to other variables; it contains their
4796 values @emph{as of the time this variable was defined}.  Therefore,
4798 @example
4799 x := foo
4800 y := $(x) bar
4801 x := later
4802 @end example
4804 @noindent
4805 is equivalent to
4807 @example
4808 y := foo bar
4809 x := later
4810 @end example
4812 When a simply expanded variable is referenced, its value is substituted
4813 verbatim.
4815 Here is a somewhat more complicated example, illustrating the use of
4816 @samp{:=} in conjunction with the @code{shell} function.
4817 (@xref{Shell Function, , The @code{shell} Function}.)  This example
4818 also shows use of the variable @code{MAKELEVEL}, which is changed
4819 when it is passed down from level to level.
4820 (@xref{Variables/Recursion, , Communicating Variables to a
4821 Sub-@code{make}}, for information about @code{MAKELEVEL}.)
4823 @vindex MAKELEVEL
4824 @vindex MAKE
4825 @example
4826 @group
4827 ifeq (0,$@{MAKELEVEL@})
4828 whoami    := $(shell whoami)
4829 host-type := $(shell arch)
4830 MAKE := $@{MAKE@} host-type=$@{host-type@} whoami=$@{whoami@}
4831 endif
4832 @end group
4833 @end example
4835 @noindent
4836 An advantage of this use of @samp{:=} is that a typical
4837 `descend into a directory' command then looks like this:
4839 @example
4840 @group
4841 $@{subdirs@}:
4842         $@{MAKE@} -C $@@ all
4843 @end group
4844 @end example
4846 Simply expanded variables generally make complicated makefile programming
4847 more predictable because they work like variables in most programming
4848 languages.  They allow you to redefine a variable using its own value (or
4849 its value processed in some way by one of the expansion functions) and to
4850 use the expansion functions much more efficiently
4851 (@pxref{Functions, ,Functions for Transforming Text}).
4853 @cindex spaces, in variable values
4854 @cindex whitespace, in variable values
4855 @cindex variables, spaces in values
4856 You can also use them to introduce controlled leading whitespace into
4857 variable values.  Leading whitespace characters are discarded from your
4858 input before substitution of variable references and function calls;
4859 this means you can include leading spaces in a variable value by
4860 protecting them with variable references, like this:
4862 @example
4863 nullstring :=
4864 space := $(nullstring) # end of the line
4865 @end example
4867 @noindent
4868 Here the value of the variable @code{space} is precisely one space.  The
4869 comment @w{@samp{# end of the line}} is included here just for clarity.
4870 Since trailing space characters are @emph{not} stripped from variable
4871 values, just a space at the end of the line would have the same effect
4872 (but be rather hard to read).  If you put whitespace at the end of a
4873 variable value, it is a good idea to put a comment like that at the end
4874 of the line to make your intent clear.  Conversely, if you do @emph{not}
4875 want any whitespace characters at the end of your variable value, you
4876 must remember not to put a random comment on the end of the line after
4877 some whitespace, such as this:
4879 @example
4880 dir := /foo/bar    # directory to put the frobs in
4881 @end example
4883 @noindent
4884 Here the value of the variable @code{dir} is @w{@samp{/foo/bar    }}
4885 (with four trailing spaces), which was probably not the intention.
4886 (Imagine something like @w{@samp{$(dir)/file}} with this definition!)
4888 @cindex conditional variable assignment
4889 @cindex variables, conditional assignment
4890 @cindex ?=
4891 There is another assignment operator for variables, @samp{?=}.  This
4892 is called a conditional variable assignment operator, because it only
4893 has an effect if the variable is not yet defined.  This statement:
4895 @example
4896 FOO ?= bar
4897 @end example
4899 @noindent
4900 is exactly equivalent to this
4901 (@pxref{Origin Function, ,The @code{origin} Function}):
4903 @example
4904 ifeq ($(origin FOO), undefined)
4905   FOO = bar
4906 endif
4907 @end example
4909 Note that a variable set to an empty value is still defined, so
4910 @samp{?=} will not set that variable.
4912 @node Advanced, Values, Flavors, Using Variables
4913 @section Advanced Features for Reference to Variables
4914 @cindex reference to variables
4916 This section describes some advanced features you can use to reference
4917 variables in more flexible ways.
4919 @menu
4920 * Substitution Refs::           Referencing a variable with
4921                                   substitutions on the value.
4922 * Computed Names::              Computing the name of the variable to refer to.
4923 @end menu
4925 @node Substitution Refs, Computed Names, Advanced, Advanced
4926 @subsection Substitution References
4927 @cindex modified variable reference
4928 @cindex substitution variable reference
4929 @cindex variables, modified reference
4930 @cindex variables, substitution reference
4932 @cindex variables, substituting suffix in
4933 @cindex suffix, substituting in variables
4934 A @dfn{substitution reference} substitutes the value of a variable with
4935 alterations that you specify.  It has the form
4936 @samp{$(@var{var}:@var{a}=@var{b})} (or
4937 @samp{$@{@var{var}:@var{a}=@var{b}@}}) and its meaning is to take the value
4938 of the variable @var{var}, replace every @var{a} at the end of a word with
4939 @var{b} in that value, and substitute the resulting string.
4941 When we say ``at the end of a word'', we mean that @var{a} must appear
4942 either followed by whitespace or at the end of the value in order to be
4943 replaced; other occurrences of @var{a} in the value are unaltered.  For
4944 example:@refill
4946 @example
4947 foo := a.o b.o c.o
4948 bar := $(foo:.o=.c)
4949 @end example
4951 @noindent
4952 sets @samp{bar} to @samp{a.c b.c c.c}.  @xref{Setting, ,Setting Variables}.
4954 A substitution reference is actually an abbreviation for use of the
4955 @code{patsubst} expansion function (@pxref{Text Functions, ,Functions for String Substitution and Analysis}).  We provide
4956 substitution references as well as @code{patsubst} for compatibility with
4957 other implementations of @code{make}.
4959 @findex patsubst
4960 Another type of substitution reference lets you use the full power of
4961 the @code{patsubst} function.  It has the same form
4962 @samp{$(@var{var}:@var{a}=@var{b})} described above, except that now
4963 @var{a} must contain a single @samp{%} character.  This case is
4964 equivalent to @samp{$(patsubst @var{a},@var{b},$(@var{var}))}.
4965 @xref{Text Functions, ,Functions for String Substitution and Analysis},
4966 for a description of the @code{patsubst} function.@refill
4968 @example
4969 @group
4970 @exdent For example:
4972 foo := a.o b.o c.o
4973 bar := $(foo:%.o=%.c)
4974 @end group
4975 @end example
4977 @noindent
4978 sets @samp{bar} to @samp{a.c b.c c.c}.
4980 @node Computed Names,  , Substitution Refs, Advanced
4981 @subsection Computed Variable Names
4982 @cindex nested variable reference
4983 @cindex computed variable name
4984 @cindex variables, computed names
4985 @cindex variables, nested references
4986 @cindex variables, @samp{$} in name
4987 @cindex @code{$}, in variable name
4988 @cindex dollar sign (@code{$}), in variable name
4990 Computed variable names are a complicated concept needed only for
4991 sophisticated makefile programming.  For most purposes you need not
4992 consider them, except to know that making a variable with a dollar sign
4993 in its name might have strange results.  However, if you are the type
4994 that wants to understand everything, or you are actually interested in
4995 what they do, read on.
4997 Variables may be referenced inside the name of a variable.  This is
4998 called a @dfn{computed variable name} or a @dfn{nested variable
4999 reference}.  For example,
5001 @example
5002 x = y
5003 y = z
5004 a := $($(x))
5005 @end example
5007 @noindent
5008 defines @code{a} as @samp{z}: the @samp{$(x)} inside @samp{$($(x))} expands
5009 to @samp{y}, so @samp{$($(x))} expands to @samp{$(y)} which in turn expands
5010 to @samp{z}.  Here the name of the variable to reference is not stated
5011 explicitly; it is computed by expansion of @samp{$(x)}.  The reference
5012 @samp{$(x)} here is nested within the outer variable reference.
5014 The previous example shows two levels of nesting, but any number of levels
5015 is possible.  For example, here are three levels:
5017 @example
5018 x = y
5019 y = z
5020 z = u
5021 a := $($($(x)))
5022 @end example
5024 @noindent
5025 Here the innermost @samp{$(x)} expands to @samp{y}, so @samp{$($(x))}
5026 expands to @samp{$(y)} which in turn expands to @samp{z}; now we have
5027 @samp{$(z)}, which becomes @samp{u}.
5029 References to recursively-expanded variables within a variable name are
5030 reexpanded in the usual fashion.  For example:
5032 @example
5033 x = $(y)
5034 y = z
5035 z = Hello
5036 a := $($(x))
5037 @end example
5039 @noindent
5040 defines @code{a} as @samp{Hello}: @samp{$($(x))} becomes @samp{$($(y))}
5041 which becomes @samp{$(z)} which becomes @samp{Hello}.
5043 Nested variable references can also contain modified references and
5044 function invocations (@pxref{Functions, ,Functions for Transforming Text}),
5045 just like any other reference.
5046 For example, using the @code{subst} function
5047 (@pxref{Text Functions, ,Functions for String Substitution and Analysis}):
5049 @example
5050 @group
5051 x = variable1
5052 variable2 := Hello
5053 y = $(subst 1,2,$(x))
5054 z = y
5055 a := $($($(z)))
5056 @end group
5057 @end example
5059 @noindent
5060 eventually defines @code{a} as @samp{Hello}.  It is doubtful that anyone
5061 would ever want to write a nested reference as convoluted as this one, but
5062 it works: @samp{$($($(z)))} expands to @samp{$($(y))} which becomes
5063 @samp{$($(subst 1,2,$(x)))}.  This gets the value @samp{variable1} from
5064 @code{x} and changes it by substitution to @samp{variable2}, so that the
5065 entire string becomes @samp{$(variable2)}, a simple variable reference
5066 whose value is @samp{Hello}.@refill
5068 A computed variable name need not consist entirely of a single variable
5069 reference.  It can contain several variable references, as well as some
5070 invariant text.  For example,
5072 @example
5073 @group
5074 a_dirs := dira dirb
5075 1_dirs := dir1 dir2
5076 @end group
5078 @group
5079 a_files := filea fileb
5080 1_files := file1 file2
5081 @end group
5083 @group
5084 ifeq "$(use_a)" "yes"
5085 a1 := a
5086 else
5087 a1 := 1
5088 endif
5089 @end group
5091 @group
5092 ifeq "$(use_dirs)" "yes"
5093 df := dirs
5094 else
5095 df := files
5096 endif
5098 dirs := $($(a1)_$(df))
5099 @end group
5100 @end example
5102 @noindent
5103 will give @code{dirs} the same value as @code{a_dirs}, @code{1_dirs},
5104 @code{a_files} or @code{1_files} depending on the settings of @code{use_a}
5105 and @code{use_dirs}.@refill
5107 Computed variable names can also be used in substitution references:
5109 @example
5110 @group
5111 a_objects := a.o b.o c.o
5112 1_objects := 1.o 2.o 3.o
5114 sources := $($(a1)_objects:.o=.c)
5115 @end group
5116 @end example
5118 @noindent
5119 defines @code{sources} as either @samp{a.c b.c c.c} or @samp{1.c 2.c 3.c},
5120 depending on the value of @code{a1}.
5122 The only restriction on this sort of use of nested variable references
5123 is that they cannot specify part of the name of a function to be called.
5124 This is because the test for a recognized function name is done before
5125 the expansion of nested references.  For example,
5127 @example
5128 @group
5129 ifdef do_sort
5130 func := sort
5131 else
5132 func := strip
5133 endif
5134 @end group
5136 @group
5137 bar := a d b g q c
5138 @end group
5140 @group
5141 foo := $($(func) $(bar))
5142 @end group
5143 @end example
5145 @noindent
5146 attempts to give @samp{foo} the value of the variable @samp{sort a d b g
5147 q c} or @samp{strip a d b g q c}, rather than giving @samp{a d b g q c}
5148 as the argument to either the @code{sort} or the @code{strip} function.
5149 This restriction could be removed in the future if that change is shown
5150 to be a good idea.
5152 You can also use computed variable names in the left-hand side of a
5153 variable assignment, or in a @code{define} directive, as in:
5155 @example
5156 dir = foo
5157 $(dir)_sources := $(wildcard $(dir)/*.c)
5158 define $(dir)_print
5159 lpr $($(dir)_sources)
5160 endef
5161 @end example
5163 @noindent
5164 This example defines the variables @samp{dir}, @samp{foo_sources}, and
5165 @samp{foo_print}.
5167 Note that @dfn{nested variable references} are quite different from
5168 @dfn{recursively expanded variables}
5169 (@pxref{Flavors, ,The Two Flavors of Variables}), though both are
5170 used together in complex ways when doing makefile programming.@refill
5172 @node Values, Setting, Advanced, Using Variables
5173 @section How Variables Get Their Values
5174 @cindex variables, how they get their values
5175 @cindex value, how a variable gets it
5177 Variables can get values in several different ways:
5179 @itemize @bullet
5180 @item
5181 You can specify an overriding value when you run @code{make}.
5182 @xref{Overriding, ,Overriding Variables}.
5184 @item
5185 You can specify a value in the makefile, either
5186 with an assignment (@pxref{Setting, ,Setting Variables}) or with a
5187 verbatim definition (@pxref{Defining, ,Defining Variables Verbatim}).@refill
5189 @item
5190 Variables in the environment become @code{make} variables.
5191 @xref{Environment, ,Variables from the Environment}.
5193 @item
5194 Several @dfn{automatic} variables are given new values for each rule.
5195 Each of these has a single conventional use.
5196 @xref{Automatic Variables}.
5198 @item
5199 Several variables have constant initial values.
5200 @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
5201 @end itemize
5203 @node Setting, Appending, Values, Using Variables
5204 @section Setting Variables
5205 @cindex setting variables
5206 @cindex variables, setting
5207 @cindex =
5208 @cindex :=
5209 @cindex ?=
5211 To set a variable from the makefile, write a line starting with the
5212 variable name followed by @samp{=} or @samp{:=}.  Whatever follows the
5213 @samp{=} or @samp{:=} on the line becomes the value.  For example,
5215 @example
5216 objects = main.o foo.o bar.o utils.o
5217 @end example
5219 @noindent
5220 defines a variable named @code{objects}.  Whitespace around the variable
5221 name and immediately after the @samp{=} is ignored.
5223 Variables defined with @samp{=} are @dfn{recursively expanded} variables.
5224 Variables defined with @samp{:=} are @dfn{simply expanded} variables; these
5225 definitions can contain variable references which will be expanded before
5226 the definition is made.  @xref{Flavors, ,The Two Flavors of Variables}.
5228 The variable name may contain function and variable references, which
5229 are expanded when the line is read to find the actual variable name to use.
5231 There is no limit on the length of the value of a variable except the
5232 amount of swapping space on the computer.  When a variable definition is
5233 long, it is a good idea to break it into several lines by inserting
5234 backslash-newline at convenient places in the definition.  This will not
5235 affect the functioning of @code{make}, but it will make the makefile easier
5236 to read.
5238 Most variable names are considered to have the empty string as a value if
5239 you have never set them.  Several variables have built-in initial values
5240 that are not empty, but you can set them in the usual ways
5241 (@pxref{Implicit Variables, ,Variables Used by Implicit Rules}).
5242 Several special variables are set
5243 automatically to a new value for each rule; these are called the
5244 @dfn{automatic} variables (@pxref{Automatic Variables}).
5246 If you'd like a variable to be set to a value only if it's not already
5247 set, then you can use the shorthand operator @samp{?=} instead of
5248 @samp{=}.  These two settings of the variable @samp{FOO} are identical
5249 (@pxref{Origin Function, ,The @code{origin} Function}):
5251 @example
5252 FOO ?= bar
5253 @end example
5255 @noindent
5258 @example
5259 ifeq ($(origin FOO), undefined)
5260 FOO = bar
5261 endif
5262 @end example
5264 @node Appending, Override Directive, Setting, Using Variables
5265 @section Appending More Text to Variables
5266 @cindex +=
5267 @cindex appending to variables
5268 @cindex variables, appending to
5270 Often it is useful to add more text to the value of a variable already defined.
5271 You do this with a line containing @samp{+=}, like this:
5273 @example
5274 objects += another.o
5275 @end example
5277 @noindent
5278 This takes the value of the variable @code{objects}, and adds the text
5279 @samp{another.o} to it (preceded by a single space).  Thus:
5281 @example
5282 objects = main.o foo.o bar.o utils.o
5283 objects += another.o
5284 @end example
5286 @noindent
5287 sets @code{objects} to @samp{main.o foo.o bar.o utils.o another.o}.
5289 Using @samp{+=} is similar to:
5291 @example
5292 objects = main.o foo.o bar.o utils.o
5293 objects := $(objects) another.o
5294 @end example
5296 @noindent
5297 but differs in ways that become important when you use more complex values.
5299 When the variable in question has not been defined before, @samp{+=}
5300 acts just like normal @samp{=}: it defines a recursively-expanded
5301 variable.  However, when there @emph{is} a previous definition, exactly
5302 what @samp{+=} does depends on what flavor of variable you defined
5303 originally.  @xref{Flavors, ,The Two Flavors of Variables}, for an
5304 explanation of the two flavors of variables.
5306 When you add to a variable's value with @samp{+=}, @code{make} acts
5307 essentially as if you had included the extra text in the initial
5308 definition of the variable.  If you defined it first with @samp{:=},
5309 making it a simply-expanded variable, @samp{+=} adds to that
5310 simply-expanded definition, and expands the new text before appending it
5311 to the old value just as @samp{:=} does
5312 (see @ref{Setting, ,Setting Variables}, for a full explanation of @samp{:=}).
5313 In fact,
5315 @example
5316 variable := value
5317 variable += more
5318 @end example
5320 @noindent
5321 is exactly equivalent to:
5323 @noindent
5324 @example
5325 variable := value
5326 variable := $(variable) more
5327 @end example
5329 On the other hand, when you use @samp{+=} with a variable that you defined
5330 first to be recursively-expanded using plain @samp{=}, @code{make} does
5331 something a bit different.  Recall that when you define a
5332 recursively-expanded variable, @code{make} does not expand the value you set
5333 for variable and function references immediately.  Instead it stores the text
5334 verbatim, and saves these variable and function references to be expanded
5335 later, when you refer to the new variable (@pxref{Flavors, ,The Two Flavors
5336 of Variables}).  When you use @samp{+=} on a recursively-expanded variable,
5337 it is this unexpanded text to which @code{make} appends the new text you
5338 specify.
5340 @example
5341 @group
5342 variable = value
5343 variable += more
5344 @end group
5345 @end example
5347 @noindent
5348 is roughly equivalent to:
5350 @example
5351 @group
5352 temp = value
5353 variable = $(temp) more
5354 @end group
5355 @end example
5357 @noindent
5358 except that of course it never defines a variable called @code{temp}.
5359 The importance of this comes when the variable's old value contains
5360 variable references.  Take this common example:
5362 @example
5363 CFLAGS = $(includes) -O
5364 @dots{}
5365 CFLAGS += -pg # enable profiling
5366 @end example
5368 @noindent
5369 The first line defines the @code{CFLAGS} variable with a reference to another
5370 variable, @code{includes}.  (@code{CFLAGS} is used by the rules for C
5371 compilation; @pxref{Catalogue of Rules, ,Catalogue of Implicit Rules}.)
5372 Using @samp{=} for the definition makes @code{CFLAGS} a recursively-expanded
5373 variable, meaning @w{@samp{$(includes) -O}} is @emph{not} expanded when
5374 @code{make} processes the definition of @code{CFLAGS}.  Thus, @code{includes}
5375 need not be defined yet for its value to take effect.  It only has to be
5376 defined before any reference to @code{CFLAGS}.  If we tried to append to the
5377 value of @code{CFLAGS} without using @samp{+=}, we might do it like this:
5379 @example
5380 CFLAGS := $(CFLAGS) -pg # enable profiling
5381 @end example
5383 @noindent
5384 This is pretty close, but not quite what we want.  Using @samp{:=}
5385 redefines @code{CFLAGS} as a simply-expanded variable; this means
5386 @code{make} expands the text @w{@samp{$(CFLAGS) -pg}} before setting the
5387 variable.  If @code{includes} is not yet defined, we get @w{@samp{ -O
5388 -pg}}, and a later definition of @code{includes} will have no effect.
5389 Conversely, by using @samp{+=} we set @code{CFLAGS} to the
5390 @emph{unexpanded} value @w{@samp{$(includes) -O -pg}}.  Thus we preserve
5391 the reference to @code{includes}, so if that variable gets defined at
5392 any later point, a reference like @samp{$(CFLAGS)} still uses its
5393 value.
5395 @node Override Directive, Defining, Appending, Using Variables
5396 @section The @code{override} Directive
5397 @findex override
5398 @cindex overriding with @code{override}
5399 @cindex variables, overriding
5401 If a variable has been set with a command argument
5402 (@pxref{Overriding, ,Overriding Variables}),
5403 then ordinary assignments in the makefile are ignored.  If you want to set
5404 the variable in the makefile even though it was set with a command
5405 argument, you can use an @code{override} directive, which is a line that
5406 looks like this:@refill
5408 @example
5409 override @var{variable} = @var{value}
5410 @end example
5412 @noindent
5415 @example
5416 override @var{variable} := @var{value}
5417 @end example
5419 To append more text to a variable defined on the command line, use:
5421 @example
5422 override @var{variable} += @var{more text}
5423 @end example
5425 @noindent
5426 @xref{Appending, ,Appending More Text to Variables}.
5428 The @code{override} directive was not invented for escalation in the war
5429 between makefiles and command arguments.  It was invented so you can alter
5430 and add to values that the user specifies with command arguments.
5432 For example, suppose you always want the @samp{-g} switch when you run the
5433 C compiler, but you would like to allow the user to specify the other
5434 switches with a command argument just as usual.  You could use this
5435 @code{override} directive:
5437 @example
5438 override CFLAGS += -g
5439 @end example
5441 You can also use @code{override} directives with @code{define} directives.
5442 This is done as you might expect:
5444 @example
5445 override define foo
5447 endef
5448 @end example
5450 @noindent
5451 @iftex
5452 See the next section for information about @code{define}.
5453 @end iftex
5454 @ifnottex
5455 @xref{Defining, ,Defining Variables Verbatim}.
5456 @end ifnottex
5458 @node Defining, Environment, Override Directive, Using Variables
5459 @section Defining Variables Verbatim
5460 @findex define
5461 @findex endef
5462 @cindex verbatim variable definition
5463 @cindex defining variables verbatim
5464 @cindex variables, defining verbatim
5466 Another way to set the value of a variable is to use the @code{define}
5467 directive.  This directive has an unusual syntax which allows newline
5468 characters to be included in the value, which is convenient for defining
5469 both canned sequences of commands
5470 (@pxref{Sequences, ,Defining Canned Command Sequences}), and also
5471 sections of makefile syntax to use with @code{eval} (@pxref{Eval Function}).
5473 The @code{define} directive is followed on the same line by the name of the
5474 variable and nothing more.  The value to give the variable appears on the
5475 following lines.  The end of the value is marked by a line containing just
5476 the word @code{endef}.  Aside from this difference in syntax, @code{define}
5477 works just like @samp{=}: it creates a recursively-expanded variable
5478 (@pxref{Flavors, ,The Two Flavors of Variables}).
5479 The variable name may contain function and variable references, which
5480 are expanded when the directive is read to find the actual variable name
5481 to use.
5483 You may nest @code{define} directives: @code{make} will keep track of
5484 nested directives and report an error if they are not all properly
5485 closed with @code{endef}.  Note that lines beginning with tab
5486 characters are considered part of a command script, so any
5487 @code{define} or @code{endef} strings appearing on such a line will
5488 not be considered @code{make} operators.
5490 @example
5491 define two-lines
5492 echo foo
5493 echo $(bar)
5494 endef
5495 @end example
5497 The value in an ordinary assignment cannot contain a newline; but the
5498 newlines that separate the lines of the value in a @code{define} become
5499 part of the variable's value (except for the final newline which precedes
5500 the @code{endef} and is not considered part of the value).@refill
5502 @need 800
5503 When used in a command script, the previous example is functionally
5504 equivalent to this:
5506 @example
5507 two-lines = echo foo; echo $(bar)
5508 @end example
5510 @noindent
5511 since two commands separated by semicolon behave much like two separate
5512 shell commands.  However, note that using two separate lines means
5513 @code{make} will invoke the shell twice, running an independent subshell
5514 for each line.  @xref{Execution, ,Command Execution}.
5516 If you want variable definitions made with @code{define} to take
5517 precedence over command-line variable definitions, you can use the
5518 @code{override} directive together with @code{define}:
5520 @example
5521 override define two-lines
5523 $(bar)
5524 endef
5525 @end example
5527 @noindent
5528 @xref{Override Directive, ,The @code{override} Directive}.
5530 @node Environment, Target-specific, Defining, Using Variables
5531 @section Variables from the Environment
5533 @cindex variables, environment
5534 @cindex environment
5535 Variables in @code{make} can come from the environment in which
5536 @code{make} is run.  Every environment variable that @code{make} sees
5537 when it starts up is transformed into a @code{make} variable with the
5538 same name and value.  However, an explicit assignment in the makefile,
5539 or with a command argument, overrides the environment.  (If the
5540 @samp{-e} flag is specified, then values from the environment override
5541 assignments in the makefile.  @xref{Options Summary, ,Summary of
5542 Options}.  But this is not recommended practice.)
5544 Thus, by setting the variable @code{CFLAGS} in your environment, you can
5545 cause all C compilations in most makefiles to use the compiler switches you
5546 prefer.  This is safe for variables with standard or conventional meanings
5547 because you know that no makefile will use them for other things.  (Note
5548 this is not totally reliable; some makefiles set @code{CFLAGS} explicitly
5549 and therefore are not affected by the value in the environment.)
5551 When @code{make} runs a command script, variables defined in the
5552 makefile are placed into the environment of that command.  This allows
5553 you to pass values to sub-@code{make} invocations (@pxref{Recursion,
5554 ,Recursive Use of @code{make}}).  By default, only variables that came
5555 from the environment or the command line are passed to recursive
5556 invocations.  You can use the @code{export} directive to pass other
5557 variables.  @xref{Variables/Recursion, , Communicating Variables to a
5558 Sub-@code{make}}, for full details.
5560 Other use of variables from the environment is not recommended.  It is not
5561 wise for makefiles to depend for their functioning on environment variables
5562 set up outside their control, since this would cause different users to get
5563 different results from the same makefile.  This is against the whole
5564 purpose of most makefiles.
5566 @cindex SHELL, import from environment
5567 Such problems would be especially likely with the variable @code{SHELL},
5568 which is normally present in the environment to specify the user's choice
5569 of interactive shell.  It would be very undesirable for this choice to
5570 affect @code{make}.  So @code{make} ignores the environment value of
5571 @code{SHELL} (except on MS-DOS and MS-Windows, where @code{SHELL} is
5572 usually not set.  @xref{Execution, ,Special handling of SHELL on
5573 MS-DOS}.)@refill
5575 @cindex SHELL, export to environment
5576 The @code{SHELL} variable is special in another way: just as the value
5577 of the @code{make} variable @code{SHELL} is not taken from the
5578 environment, so also it is not placed into the environment of commands
5579 that @code{make} invokes.  Instead, the value of @code{SHELL} from the
5580 invoking environment is provided to the command.  You can use
5581 @code{export SHELL} to force the value of the @code{make} variable
5582 @code{SHELL} to be placed in the environment of commands.
5584 @node Target-specific, Pattern-specific, Environment, Using Variables
5585 @section Target-specific Variable Values
5586 @cindex target-specific variables
5587 @cindex variables, target-specific
5589 Variable values in @code{make} are usually global; that is, they are the
5590 same regardless of where they are evaluated (unless they're reset, of
5591 course).  One exception to that is automatic variables
5592 (@pxref{Automatic Variables}).
5594 The other exception is @dfn{target-specific variable values}.  This
5595 feature allows you to define different values for the same variable,
5596 based on the target that @code{make} is currently building.  As with
5597 automatic variables, these values are only available within the context
5598 of a target's command script (and in other target-specific assignments).
5600 Set a target-specific variable value like this:
5602 @example
5603 @var{target} @dots{} : @var{variable-assignment}
5604 @end example
5606 @noindent
5607 or like this:
5609 @example
5610 @var{target} @dots{} : override @var{variable-assignment}
5611 @end example
5613 @noindent
5614 or like this:
5616 @example
5617 @var{target} @dots{} : export @var{variable-assignment}
5618 @end example
5620 Multiple @var{target} values create a target-specific variable value for
5621 each member of the target list individually.
5623 The @var{variable-assignment} can be any valid form of assignment;
5624 recursive (@samp{=}), static (@samp{:=}), appending (@samp{+=}), or
5625 conditional (@samp{?=}).  All variables that appear within the
5626 @var{variable-assignment} are evaluated within the context of the
5627 target: thus, any previously-defined target-specific variable values
5628 will be in effect.  Note that this variable is actually distinct from
5629 any ``global'' value: the two variables do not have to have the same
5630 flavor (recursive vs.@: static).
5632 Target-specific variables have the same priority as any other makefile
5633 variable.  Variables provided on the command-line (and in the
5634 environment if the @samp{-e} option is in force) will take precedence.
5635 Specifying the @code{override} directive will allow the target-specific
5636 variable value to be preferred.
5638 There is one more special feature of target-specific variables: when
5639 you define a target-specific variable that variable value is also in
5640 effect for all prerequisites of this target, and all their
5641 prerequisites, etc.@: (unless those prerequisites override that variable
5642 with their own target-specific variable value).  So, for example, a
5643 statement like this:
5645 @example
5646 prog : CFLAGS = -g
5647 prog : prog.o foo.o bar.o
5648 @end example
5650 @noindent
5651 will set @code{CFLAGS} to @samp{-g} in the command script for
5652 @file{prog}, but it will also set @code{CFLAGS} to @samp{-g} in the
5653 command scripts that create @file{prog.o}, @file{foo.o}, and
5654 @file{bar.o}, and any command scripts which create their
5655 prerequisites.
5657 Be aware that a given prerequisite will only be built once per
5658 invocation of make, at most.  If the same file is a prerequisite of
5659 multiple targets, and each of those targets has a different value for
5660 the same target-specific variable, then the first target to be built
5661 will cause that prerequisite to be built and the prerequisite will
5662 inherit the target-specific value from the first target.  It will
5663 ignore the target-specific values from any other targets.
5665 @node Pattern-specific,  , Target-specific, Using Variables
5666 @section Pattern-specific Variable Values
5667 @cindex pattern-specific variables
5668 @cindex variables, pattern-specific
5670 In addition to target-specific variable values
5671 (@pxref{Target-specific, ,Target-specific Variable Values}), GNU
5672 @code{make} supports pattern-specific variable values.  In this form,
5673 the variable is defined for any target that matches the pattern
5674 specified.  If a target matches more than one pattern, all the
5675 matching pattern-specific variables are interpreted in the order in
5676 which they were defined in the makefile, and collected together into
5677 one set.  Variables defined in this way are searched after any
5678 target-specific variables defined explicitly for that target, and
5679 before target-specific variables defined for the parent target.
5681 Set a pattern-specific variable value like this:
5683 @example
5684 @var{pattern} @dots{} : @var{variable-assignment}
5685 @end example
5687 @noindent
5688 or like this:
5690 @example
5691 @var{pattern} @dots{} : override @var{variable-assignment}
5692 @end example
5694 @noindent
5695 where @var{pattern} is a %-pattern.  As with target-specific variable
5696 values, multiple @var{pattern} values create a pattern-specific variable
5697 value for each pattern individually.  The @var{variable-assignment} can
5698 be any valid form of assignment.  Any command-line variable setting will
5699 take precedence, unless @code{override} is specified.
5701 For example:
5703 @example
5704 %.o : CFLAGS = -O
5705 @end example
5707 @noindent
5708 will assign @code{CFLAGS} the value of @samp{-O} for all targets
5709 matching the pattern @code{%.o}.
5711 @node Conditionals, Functions, Using Variables, Top
5712 @chapter Conditional Parts of Makefiles
5714 @cindex conditionals
5715 A @dfn{conditional} causes part of a makefile to be obeyed or ignored
5716 depending on the values of variables.  Conditionals can compare the
5717 value of one variable to another, or the value of a variable to
5718 a constant string.  Conditionals control what @code{make} actually
5719 ``sees'' in the makefile, so they @emph{cannot} be used to control shell
5720 commands at the time of execution.@refill
5722 @menu
5723 * Conditional Example::         Example of a conditional
5724 * Conditional Syntax::          The syntax of conditionals.
5725 * Testing Flags::               Conditionals that test flags.
5726 @end menu
5728 @node Conditional Example, Conditional Syntax, Conditionals, Conditionals
5729 @section Example of a Conditional
5731 The following example of a conditional tells @code{make} to use one set
5732 of libraries if the @code{CC} variable is @samp{gcc}, and a different
5733 set of libraries otherwise.  It works by controlling which of two
5734 command lines will be used as the command for a rule.  The result is
5735 that @samp{CC=gcc} as an argument to @code{make} changes not only which
5736 compiler is used but also which libraries are linked.
5738 @example
5739 libs_for_gcc = -lgnu
5740 normal_libs =
5742 foo: $(objects)
5743 ifeq ($(CC),gcc)
5744         $(CC) -o foo $(objects) $(libs_for_gcc)
5745 else
5746         $(CC) -o foo $(objects) $(normal_libs)
5747 endif
5748 @end example
5750 This conditional uses three directives: one @code{ifeq}, one @code{else}
5751 and one @code{endif}.
5753 The @code{ifeq} directive begins the conditional, and specifies the
5754 condition.  It contains two arguments, separated by a comma and surrounded
5755 by parentheses.  Variable substitution is performed on both arguments and
5756 then they are compared.  The lines of the makefile following the
5757 @code{ifeq} are obeyed if the two arguments match; otherwise they are
5758 ignored.
5760 The @code{else} directive causes the following lines to be obeyed if the
5761 previous conditional failed.  In the example above, this means that the
5762 second alternative linking command is used whenever the first alternative
5763 is not used.  It is optional to have an @code{else} in a conditional.
5765 The @code{endif} directive ends the conditional.  Every conditional must
5766 end with an @code{endif}.  Unconditional makefile text follows.
5768 As this example illustrates, conditionals work at the textual level:
5769 the lines of the conditional are treated as part of the makefile, or
5770 ignored, according to the condition.  This is why the larger syntactic
5771 units of the makefile, such as rules, may cross the beginning or the
5772 end of the conditional.
5774 When the variable @code{CC} has the value @samp{gcc}, the above example has
5775 this effect:
5777 @example
5778 foo: $(objects)
5779         $(CC) -o foo $(objects) $(libs_for_gcc)
5780 @end example
5782 @noindent
5783 When the variable @code{CC} has any other value, the effect is this:
5785 @example
5786 foo: $(objects)
5787         $(CC) -o foo $(objects) $(normal_libs)
5788 @end example
5790 Equivalent results can be obtained in another way by conditionalizing a
5791 variable assignment and then using the variable unconditionally:
5793 @example
5794 libs_for_gcc = -lgnu
5795 normal_libs =
5797 ifeq ($(CC),gcc)
5798   libs=$(libs_for_gcc)
5799 else
5800   libs=$(normal_libs)
5801 endif
5803 foo: $(objects)
5804         $(CC) -o foo $(objects) $(libs)
5805 @end example
5807 @node Conditional Syntax, Testing Flags, Conditional Example, Conditionals
5808 @section Syntax of Conditionals
5809 @findex ifdef
5810 @findex ifeq
5811 @findex ifndef
5812 @findex ifneq
5813 @findex else
5814 @findex endif
5816 The syntax of a simple conditional with no @code{else} is as follows:
5818 @example
5819 @var{conditional-directive}
5820 @var{text-if-true}
5821 endif
5822 @end example
5824 @noindent
5825 The @var{text-if-true} may be any lines of text, to be considered as part
5826 of the makefile if the condition is true.  If the condition is false, no
5827 text is used instead.
5829 The syntax of a complex conditional is as follows:
5831 @example
5832 @var{conditional-directive}
5833 @var{text-if-true}
5834 else
5835 @var{text-if-false}
5836 endif
5837 @end example
5841 @example
5842 @var{conditional-directive}
5843 @var{text-if-one-is-true}
5844 else @var{conditional-directive}
5845 @var{text-if-true}
5846 else
5847 @var{text-if-false}
5848 endif
5849 @end example
5851 @noindent
5852 There can be as many ``@code{else} @var{conditional-directive}''
5853 clauses as necessary.  Once a given condition is true,
5854 @var{text-if-true} is used and no other clause is used; if no
5855 condition is true then @var{text-if-false} is used.  The
5856 @var{text-if-true} and @var{text-if-false} can be any number of lines
5857 of text.
5859 The syntax of the @var{conditional-directive} is the same whether the
5860 conditional is simple or complex; after an @code{else} or not.  There
5861 are four different directives that test different conditions.  Here is
5862 a table of them:
5864 @table @code
5865 @item ifeq (@var{arg1}, @var{arg2})
5866 @itemx ifeq '@var{arg1}' '@var{arg2}'
5867 @itemx ifeq "@var{arg1}" "@var{arg2}"
5868 @itemx ifeq "@var{arg1}" '@var{arg2}'
5869 @itemx ifeq '@var{arg1}' "@var{arg2}"
5870 Expand all variable references in @var{arg1} and @var{arg2} and
5871 compare them.  If they are identical, the @var{text-if-true} is
5872 effective; otherwise, the @var{text-if-false}, if any, is effective.
5874 Often you want to test if a variable has a non-empty value.  When the
5875 value results from complex expansions of variables and functions,
5876 expansions you would consider empty may actually contain whitespace
5877 characters and thus are not seen as empty.  However, you can use the
5878 @code{strip} function (@pxref{Text Functions}) to avoid interpreting
5879 whitespace as a non-empty value.  For example:
5881 @example
5882 @group
5883 ifeq ($(strip $(foo)),)
5884 @var{text-if-empty}
5885 endif
5886 @end group
5887 @end example
5889 @noindent
5890 will evaluate @var{text-if-empty} even if the expansion of
5891 @code{$(foo)} contains whitespace characters.
5893 @item ifneq (@var{arg1}, @var{arg2})
5894 @itemx ifneq '@var{arg1}' '@var{arg2}'
5895 @itemx ifneq "@var{arg1}" "@var{arg2}"
5896 @itemx ifneq "@var{arg1}" '@var{arg2}'
5897 @itemx ifneq '@var{arg1}' "@var{arg2}"
5898 Expand all variable references in @var{arg1} and @var{arg2} and
5899 compare them.  If they are different, the @var{text-if-true} is
5900 effective; otherwise, the @var{text-if-false}, if any, is effective.
5902 @item ifdef @var{variable-name}
5903 The @code{ifdef} form takes the @emph{name} of a variable as its
5904 argument, not a reference to a variable.  The value of that variable
5905 has a non-empty value, the @var{text-if-true} is effective; otherwise,
5906 the @var{text-if-false}, if any, is effective.  Variables that have
5907 never been defined have an empty value.  The text @var{variable-name}
5908 is expanded, so it could be a variable or function that expands
5909 to the name of a variable.  For example:
5911 @example
5912 bar = true
5913 foo = bar
5914 ifdef $(foo)
5915 frobozz = yes
5916 endif
5917 @end example
5919 The variable reference @code{$(foo)} is expanded, yielding @code{bar},
5920 which is considered to be the name of a variable.  The variable
5921 @code{bar} is not expanded, but its value is examined to determine if
5922 it is non-empty.
5924 Note that @code{ifdef} only tests whether a variable has a value.  It
5925 does not expand the variable to see if that value is nonempty.
5926 Consequently, tests using @code{ifdef} return true for all definitions
5927 except those like @code{foo =}.  To test for an empty value, use
5928 @w{@code{ifeq ($(foo),)}}.  For example,
5930 @example
5931 bar =
5932 foo = $(bar)
5933 ifdef foo
5934 frobozz = yes
5935 else
5936 frobozz = no
5937 endif
5938 @end example
5940 @noindent
5941 sets @samp{frobozz} to @samp{yes}, while:
5943 @example
5944 foo =
5945 ifdef foo
5946 frobozz = yes
5947 else
5948 frobozz = no
5949 endif
5950 @end example
5952 @noindent
5953 sets @samp{frobozz} to @samp{no}.
5955 @item ifndef @var{variable-name}
5956 If the variable @var{variable-name} has an empty value, the
5957 @var{text-if-true} is effective; otherwise, the @var{text-if-false},
5958 if any, is effective.  The rules for expansion and testing of
5959 @var{variable-name} are identical to the @code{ifdef} directive.
5960 @end table
5962 Extra spaces are allowed and ignored at the beginning of the conditional
5963 directive line, but a tab is not allowed.  (If the line begins with a tab,
5964 it will be considered a command for a rule.)  Aside from this, extra spaces
5965 or tabs may be inserted with no effect anywhere except within the directive
5966 name or within an argument.  A comment starting with @samp{#} may appear at
5967 the end of the line.
5969 The other two directives that play a part in a conditional are @code{else}
5970 and @code{endif}.  Each of these directives is written as one word, with no
5971 arguments.  Extra spaces are allowed and ignored at the beginning of the
5972 line, and spaces or tabs at the end.  A comment starting with @samp{#} may
5973 appear at the end of the line.
5975 Conditionals affect which lines of the makefile @code{make} uses.  If
5976 the condition is true, @code{make} reads the lines of the
5977 @var{text-if-true} as part of the makefile; if the condition is false,
5978 @code{make} ignores those lines completely.  It follows that syntactic
5979 units of the makefile, such as rules, may safely be split across the
5980 beginning or the end of the conditional.@refill
5982 @code{make} evaluates conditionals when it reads a makefile.
5983 Consequently, you cannot use automatic variables in the tests of
5984 conditionals because they are not defined until commands are run
5985 (@pxref{Automatic Variables}).
5987 To prevent intolerable confusion, it is not permitted to start a
5988 conditional in one makefile and end it in another.  However, you may
5989 write an @code{include} directive within a conditional, provided you do
5990 not attempt to terminate the conditional inside the included file.
5992 @node Testing Flags,  , Conditional Syntax, Conditionals
5993 @section Conditionals that Test Flags
5995 You can write a conditional that tests @code{make} command flags such as
5996 @samp{-t} by using the variable @code{MAKEFLAGS} together with the
5997 @code{findstring} function
5998 (@pxref{Text Functions, , Functions for String Substitution and Analysis}).
5999 This is useful when @code{touch} is not enough to make a file appear up
6000 to date.
6002 The @code{findstring} function determines whether one string appears as a
6003 substring of another.  If you want to test for the @samp{-t} flag,
6004 use @samp{t} as the first string and the value of @code{MAKEFLAGS} as
6005 the other.
6007 For example, here is how to arrange to use @samp{ranlib -t} to finish
6008 marking an archive file up to date:
6010 @example
6011 archive.a: @dots{}
6012 ifneq (,$(findstring t,$(MAKEFLAGS)))
6013         +touch archive.a
6014         +ranlib -t archive.a
6015 else
6016         ranlib archive.a
6017 endif
6018 @end example
6020 @noindent
6021 The @samp{+} prefix marks those command lines as ``recursive'' so
6022 that they will be executed despite use of the @samp{-t} flag.
6023 @xref{Recursion, ,Recursive Use of @code{make}}.
6025 @node Functions, Running, Conditionals, Top
6026 @chapter Functions for Transforming Text
6027 @cindex functions
6029 @dfn{Functions} allow you to do text processing in the makefile to compute
6030 the files to operate on or the commands to use.  You use a function in a
6031 @dfn{function call}, where you give the name of the function and some text
6032 (the @dfn{arguments}) for the function to operate on.  The result of the
6033 function's processing is substituted into the makefile at the point of the
6034 call, just as a variable might be substituted.
6036 @menu
6037 * Syntax of Functions::         How to write a function call.
6038 * Text Functions::              General-purpose text manipulation functions.
6039 * File Name Functions::         Functions for manipulating file names.
6040 * Foreach Function::            Repeat some text with controlled variation.
6041 * If Function::                 Conditionally expand a value.
6042 * Call Function::               Expand a user-defined function.
6043 * Value Function::              Return the un-expanded value of a variable.
6044 * Eval Function::               Evaluate the arguments as makefile syntax.
6045 * Origin Function::             Find where a variable got its value.
6046 * Flavor Function::             Find out the flavor of a variable.
6047 * Shell Function::              Substitute the output of a shell command.
6048 * Make Control Functions::      Functions that control how make runs.
6049 @end menu
6051 @node Syntax of Functions, Text Functions, Functions, Functions
6052 @section Function Call Syntax
6053 @cindex @code{$}, in function call
6054 @cindex dollar sign (@code{$}), in function call
6055 @cindex arguments of functions
6056 @cindex functions, syntax of
6058 A function call resembles a variable reference.  It looks like this:
6060 @example
6061 $(@var{function} @var{arguments})
6062 @end example
6064 @noindent
6065 or like this:
6067 @example
6068 $@{@var{function} @var{arguments}@}
6069 @end example
6071 Here @var{function} is a function name; one of a short list of names
6072 that are part of @code{make}.  You can also essentially create your own
6073 functions by using the @code{call} builtin function.
6075 The @var{arguments} are the arguments of the function.  They are
6076 separated from the function name by one or more spaces or tabs, and if
6077 there is more than one argument, then they are separated by commas.
6078 Such whitespace and commas are not part of an argument's value.  The
6079 delimiters which you use to surround the function call, whether
6080 parentheses or braces, can appear in an argument only in matching pairs;
6081 the other kind of delimiters may appear singly.  If the arguments
6082 themselves contain other function calls or variable references, it is
6083 wisest to use the same kind of delimiters for all the references; write
6084 @w{@samp{$(subst a,b,$(x))}}, not @w{@samp{$(subst a,b,$@{x@})}}.  This
6085 is because it is clearer, and because only one type of delimiter is
6086 matched to find the end of the reference.
6088 The text written for each argument is processed by substitution of
6089 variables and function calls to produce the argument value, which
6090 is the text on which the function acts.  The substitution is done in the
6091 order in which the arguments appear.
6093 Commas and unmatched parentheses or braces cannot appear in the text of an
6094 argument as written; leading spaces cannot appear in the text of the first
6095 argument as written.  These characters can be put into the argument value
6096 by variable substitution.  First define variables @code{comma} and
6097 @code{space} whose values are isolated comma and space characters, then
6098 substitute these variables where such characters are wanted, like this:
6100 @example
6101 @group
6102 comma:= ,
6103 empty:=
6104 space:= $(empty) $(empty)
6105 foo:= a b c
6106 bar:= $(subst $(space),$(comma),$(foo))
6107 # @r{bar is now `a,b,c'.}
6108 @end group
6109 @end example
6111 @noindent
6112 Here the @code{subst} function replaces each space with a comma, through
6113 the value of @code{foo}, and substitutes the result.
6115 @node Text Functions, File Name Functions, Syntax of Functions, Functions
6116 @section Functions for String Substitution and Analysis
6117 @cindex functions, for text
6119 Here are some functions that operate on strings:
6121 @table @code
6122 @item $(subst @var{from},@var{to},@var{text})
6123 @findex subst
6124 Performs a textual replacement on the text @var{text}: each occurrence
6125 of @var{from} is replaced by @var{to}.  The result is substituted for
6126 the function call.  For example,
6128 @example
6129 $(subst ee,EE,feet on the street)
6130 @end example
6132 substitutes the string @samp{fEEt on the strEEt}.
6134 @item $(patsubst @var{pattern},@var{replacement},@var{text})
6135 @findex patsubst
6136 Finds whitespace-separated words in @var{text} that match
6137 @var{pattern} and replaces them with @var{replacement}.  Here
6138 @var{pattern} may contain a @samp{%} which acts as a wildcard,
6139 matching any number of any characters within a word.  If
6140 @var{replacement} also contains a @samp{%}, the @samp{%} is replaced
6141 by the text that matched the @samp{%} in @var{pattern}.  Only the first
6142 @samp{%} in the @var{pattern} and @var{replacement} is treated this
6143 way; any subsequent @samp{%} is unchanged.@refill
6145 @cindex @code{%}, quoting in @code{patsubst}
6146 @cindex @code{%}, quoting with @code{\} (backslash)
6147 @cindex @code{\} (backslash), to quote @code{%}
6148 @cindex backslash (@code{\}), to quote @code{%}
6149 @cindex quoting @code{%}, in @code{patsubst}
6150 @samp{%} characters in @code{patsubst} function invocations can be
6151 quoted with preceding backslashes (@samp{\}).  Backslashes that would
6152 otherwise quote @samp{%} characters can be quoted with more backslashes.
6153 Backslashes that quote @samp{%} characters or other backslashes are
6154 removed from the pattern before it is compared file names or has a stem
6155 substituted into it.  Backslashes that are not in danger of quoting
6156 @samp{%} characters go unmolested.  For example, the pattern
6157 @file{the\%weird\\%pattern\\} has @samp{the%weird\} preceding the
6158 operative @samp{%} character, and @samp{pattern\\} following it.  The
6159 final two backslashes are left alone because they cannot affect any
6160 @samp{%} character.@refill
6162 Whitespace between words is folded into single space characters;
6163 leading and trailing whitespace is discarded.
6165 For example,
6167 @example
6168 $(patsubst %.c,%.o,x.c.c bar.c)
6169 @end example
6171 @noindent
6172 produces the value @samp{x.c.o bar.o}.
6174 Substitution references (@pxref{Substitution Refs, ,Substitution
6175 References}) are a simpler way to get the effect of the @code{patsubst}
6176 function:
6178 @example
6179 $(@var{var}:@var{pattern}=@var{replacement})
6180 @end example
6182 @noindent
6183 is equivalent to
6185 @example
6186 $(patsubst @var{pattern},@var{replacement},$(@var{var}))
6187 @end example
6189 The second shorthand simplifies one of the most common uses of
6190 @code{patsubst}: replacing the suffix at the end of file names.
6192 @example
6193 $(@var{var}:@var{suffix}=@var{replacement})
6194 @end example
6196 @noindent
6197 is equivalent to
6199 @example
6200 $(patsubst %@var{suffix},%@var{replacement},$(@var{var}))
6201 @end example
6203 @noindent
6204 For example, you might have a list of object files:
6206 @example
6207 objects = foo.o bar.o baz.o
6208 @end example
6210 @noindent
6211 To get the list of corresponding source files, you could simply write:
6213 @example
6214 $(objects:.o=.c)
6215 @end example
6217 @noindent
6218 instead of using the general form:
6220 @example
6221 $(patsubst %.o,%.c,$(objects))
6222 @end example
6224 @item $(strip @var{string})
6225 @cindex stripping whitespace
6226 @cindex whitespace, stripping
6227 @cindex spaces, stripping
6228 @findex strip
6229 Removes leading and trailing whitespace from @var{string} and replaces
6230 each internal sequence of one or more whitespace characters with a
6231 single space.  Thus, @samp{$(strip a b  c )} results in @w{@samp{a b c}}.
6233 The function @code{strip} can be very useful when used in conjunction
6234 with conditionals.  When comparing something with the empty string
6235 @samp{} using @code{ifeq} or @code{ifneq}, you usually want a string of
6236 just whitespace to match the empty string (@pxref{Conditionals}).
6238 Thus, the following may fail to have the desired results:
6240 @example
6241 .PHONY: all
6242 ifneq   "$(needs_made)" ""
6243 all: $(needs_made)
6244 else
6245 all:;@@echo 'Nothing to make!'
6246 endif
6247 @end example
6249 @noindent
6250 Replacing the variable reference @w{@samp{$(needs_made)}} with the
6251 function call @w{@samp{$(strip $(needs_made))}} in the @code{ifneq}
6252 directive would make it more robust.@refill
6254 @item $(findstring @var{find},@var{in})
6255 @findex findstring
6256 @cindex searching for strings
6257 @cindex finding strings
6258 @cindex strings, searching for
6259 Searches @var{in} for an occurrence of @var{find}.  If it occurs, the
6260 value is @var{find}; otherwise, the value is empty.  You can use this
6261 function in a conditional to test for the presence of a specific
6262 substring in a given string.  Thus, the two examples,
6264 @example
6265 $(findstring a,a b c)
6266 $(findstring a,b c)
6267 @end example
6269 @noindent
6270 produce the values @samp{a} and @samp{} (the empty string),
6271 respectively.  @xref{Testing Flags}, for a practical application of
6272 @code{findstring}.@refill
6274 @need 750
6275 @findex filter
6276 @cindex filtering words
6277 @cindex words, filtering
6278 @item $(filter @var{pattern}@dots{},@var{text})
6279 Returns all whitespace-separated words in @var{text} that @emph{do} match
6280 any of the @var{pattern} words, removing any words that @emph{do not}
6281 match.  The patterns are written using @samp{%}, just like the patterns
6282 used in the @code{patsubst} function above.@refill
6284 The @code{filter} function can be used to separate out different types
6285 of strings (such as file names) in a variable.  For example:
6287 @example
6288 sources := foo.c bar.c baz.s ugh.h
6289 foo: $(sources)
6290         cc $(filter %.c %.s,$(sources)) -o foo
6291 @end example
6293 @noindent
6294 says that @file{foo} depends of @file{foo.c}, @file{bar.c},
6295 @file{baz.s} and @file{ugh.h} but only @file{foo.c}, @file{bar.c} and
6296 @file{baz.s} should be specified in the command to the
6297 compiler.@refill
6299 @item $(filter-out @var{pattern}@dots{},@var{text})
6300 @findex filter-out
6301 @cindex filtering out words
6302 @cindex words, filtering out
6303 Returns all whitespace-separated words in @var{text} that @emph{do not}
6304 match any of the @var{pattern} words, removing the words that @emph{do}
6305 match one or more.  This is the exact opposite of the @code{filter}
6306 function.@refill
6308 For example, given:
6310 @example
6311 @group
6312 objects=main1.o foo.o main2.o bar.o
6313 mains=main1.o main2.o
6314 @end group
6315 @end example
6317 @noindent
6318 the following generates a list which contains all the object files not
6319 in @samp{mains}:
6321 @example
6322 $(filter-out $(mains),$(objects))
6323 @end example
6325 @need 1500
6326 @findex sort
6327 @cindex sorting words
6328 @item $(sort @var{list})
6329 Sorts the words of @var{list} in lexical order, removing duplicate
6330 words.  The output is a list of words separated by single spaces.
6331 Thus,
6333 @example
6334 $(sort foo bar lose)
6335 @end example
6337 @noindent
6338 returns the value @samp{bar foo lose}.
6340 @cindex removing duplicate words
6341 @cindex duplicate words, removing
6342 @cindex words, removing duplicates
6343 Incidentally, since @code{sort} removes duplicate words, you can use
6344 it for this purpose even if you don't care about the sort order.
6346 @item $(word @var{n},@var{text})
6347 @findex word
6348 @cindex word, selecting a
6349 @cindex selecting a word
6350 Returns the @var{n}th word of @var{text}.  The legitimate values of
6351 @var{n} start from 1.  If @var{n} is bigger than the number of words
6352 in @var{text}, the value is empty.  For example,
6354 @example
6355 $(word 2, foo bar baz)
6356 @end example
6358 @noindent
6359 returns @samp{bar}.
6361 @item $(wordlist @var{s},@var{e},@var{text})
6362 @findex wordlist
6363 @cindex words, selecting lists of
6364 @cindex selecting word lists
6365 Returns the list of words in @var{text} starting with word @var{s} and
6366 ending with word @var{e} (inclusive).  The legitimate values of @var{s}
6367 start from 1; @var{e} may start from 0.  If @var{s} is bigger than the
6368 number of words in @var{text}, the value is empty.  If @var{e} is
6369 bigger than the number of words in @var{text}, words up to the end of
6370 @var{text} are returned.  If @var{s} is greater than @var{e}, nothing
6371 is returned.  For example,
6373 @example
6374 $(wordlist 2, 3, foo bar baz)
6375 @end example
6377 @noindent
6378 returns @samp{bar baz}.
6380 @c Following item phrased to prevent overfull hbox.  --RJC 17 Jul 92
6381 @item $(words @var{text})
6382 @findex words
6383 @cindex words, finding number
6384 Returns the number of words in @var{text}.
6385 Thus, the last word of @var{text} is
6386 @w{@code{$(word $(words @var{text}),@var{text})}}.@refill
6388 @item $(firstword @var{names}@dots{})
6389 @findex firstword
6390 @cindex words, extracting first
6391 The argument @var{names} is regarded as a series of names, separated
6392 by whitespace.  The value is the first name in the series.  The rest
6393 of the names are ignored.
6395 For example,
6397 @example
6398 $(firstword foo bar)
6399 @end example
6401 @noindent
6402 produces the result @samp{foo}.  Although @code{$(firstword
6403 @var{text})} is the same as @code{$(word 1,@var{text})}, the
6404 @code{firstword} function is retained for its simplicity.@refill
6407 @item $(lastword @var{names}@dots{})
6408 @findex lastword
6409 @cindex words, extracting last
6410 The argument @var{names} is regarded as a series of names, separated
6411 by whitespace.  The value is the last name in the series.
6413 For example,
6415 @example
6416 $(lastword foo bar)
6417 @end example
6419 @noindent
6420 produces the result @samp{bar}.  Although @code{$(lastword
6421 @var{text})} is the same as @code{$(word $(words @var{text}),@var{text})},
6422 the @code{lastword} function was added for its simplicity and better
6423 performance.@refill
6424 @end table
6427 Here is a realistic example of the use of @code{subst} and
6428 @code{patsubst}.  Suppose that a makefile uses the @code{VPATH} variable
6429 to specify a list of directories that @code{make} should search for
6430 prerequisite files
6431 (@pxref{General Search, , @code{VPATH} Search Path for All Prerequisites}).
6432 This example shows how to
6433 tell the C compiler to search for header files in the same list of
6434 directories.@refill
6436 The value of @code{VPATH} is a list of directories separated by colons,
6437 such as @samp{src:../headers}.  First, the @code{subst} function is used to
6438 change the colons to spaces:
6440 @example
6441 $(subst :, ,$(VPATH))
6442 @end example
6444 @noindent
6445 This produces @samp{src ../headers}.  Then @code{patsubst} is used to turn
6446 each directory name into a @samp{-I} flag.  These can be added to the
6447 value of the variable @code{CFLAGS}, which is passed automatically to the C
6448 compiler, like this:
6450 @example
6451 override CFLAGS += $(patsubst %,-I%,$(subst :, ,$(VPATH)))
6452 @end example
6454 @noindent
6455 The effect is to append the text @samp{-Isrc -I../headers} to the
6456 previously given value of @code{CFLAGS}.  The @code{override} directive is
6457 used so that the new value is assigned even if the previous value of
6458 @code{CFLAGS} was specified with a command argument (@pxref{Override
6459 Directive, , The @code{override} Directive}).
6461 @node File Name Functions, Foreach Function, Text Functions, Functions
6462 @section Functions for File Names
6463 @cindex functions, for file names
6464 @cindex file name functions
6466 Several of the built-in expansion functions relate specifically to
6467 taking apart file names or lists of file names.
6469 Each of the following functions performs a specific transformation on a
6470 file name.  The argument of the function is regarded as a series of file
6471 names, separated by whitespace.  (Leading and trailing whitespace is
6472 ignored.)  Each file name in the series is transformed in the same way and
6473 the results are concatenated with single spaces between them.
6475 @table @code
6476 @item $(dir @var{names}@dots{})
6477 @findex dir
6478 @cindex directory part
6479 @cindex file name, directory part
6480 Extracts the directory-part of each file name in @var{names}.  The
6481 directory-part of the file name is everything up through (and
6482 including) the last slash in it.  If the file name contains no slash,
6483 the directory part is the string @samp{./}.  For example,
6485 @example
6486 $(dir src/foo.c hacks)
6487 @end example
6489 @noindent
6490 produces the result @samp{src/ ./}.
6492 @item $(notdir @var{names}@dots{})
6493 @findex notdir
6494 @cindex file name, nondirectory part
6495 @cindex nondirectory part
6496 Extracts all but the directory-part of each file name in @var{names}.
6497 If the file name contains no slash, it is left unchanged.  Otherwise,
6498 everything through the last slash is removed from it.
6500 A file name that ends with a slash becomes an empty string.  This is
6501 unfortunate, because it means that the result does not always have the
6502 same number of whitespace-separated file names as the argument had;
6503 but we do not see any other valid alternative.
6505 For example,
6507 @example
6508 $(notdir src/foo.c hacks)
6509 @end example
6511 @noindent
6512 produces the result @samp{foo.c hacks}.
6514 @item $(suffix @var{names}@dots{})
6515 @findex suffix
6516 @cindex suffix, function to find
6517 @cindex file name suffix
6518 Extracts the suffix of each file name in @var{names}.  If the file name
6519 contains a period, the suffix is everything starting with the last
6520 period.  Otherwise, the suffix is the empty string.  This frequently
6521 means that the result will be empty when @var{names} is not, and if
6522 @var{names} contains multiple file names, the result may contain fewer
6523 file names.
6525 For example,
6527 @example
6528 $(suffix src/foo.c src-1.0/bar.c hacks)
6529 @end example
6531 @noindent
6532 produces the result @samp{.c .c}.
6534 @item $(basename @var{names}@dots{})
6535 @findex basename
6536 @cindex basename
6537 @cindex file name, basename of
6538 Extracts all but the suffix of each file name in @var{names}.  If the
6539 file name contains a period, the basename is everything starting up to
6540 (and not including) the last period.  Periods in the directory part are
6541 ignored.  If there is no period, the basename is the entire file name.
6542 For example,
6544 @example
6545 $(basename src/foo.c src-1.0/bar hacks)
6546 @end example
6548 @noindent
6549 produces the result @samp{src/foo src-1.0/bar hacks}.
6551 @c plural convention with dots (be consistent)
6552 @item $(addsuffix @var{suffix},@var{names}@dots{})
6553 @findex addsuffix
6554 @cindex suffix, adding
6555 @cindex file name suffix, adding
6556 The argument @var{names} is regarded as a series of names, separated
6557 by whitespace; @var{suffix} is used as a unit.  The value of
6558 @var{suffix} is appended to the end of each individual name and the
6559 resulting larger names are concatenated with single spaces between
6560 them.  For example,
6562 @example
6563 $(addsuffix .c,foo bar)
6564 @end example
6566 @noindent
6567 produces the result @samp{foo.c bar.c}.
6569 @item $(addprefix @var{prefix},@var{names}@dots{})
6570 @findex addprefix
6571 @cindex prefix, adding
6572 @cindex file name prefix, adding
6573 The argument @var{names} is regarded as a series of names, separated
6574 by whitespace; @var{prefix} is used as a unit.  The value of
6575 @var{prefix} is prepended to the front of each individual name and the
6576 resulting larger names are concatenated with single spaces between
6577 them.  For example,
6579 @example
6580 $(addprefix src/,foo bar)
6581 @end example
6583 @noindent
6584 produces the result @samp{src/foo src/bar}.
6586 @item $(join @var{list1},@var{list2})
6587 @findex join
6588 @cindex joining lists of words
6589 @cindex words, joining lists
6590 Concatenates the two arguments word by word: the two first words (one
6591 from each argument) concatenated form the first word of the result, the
6592 two second words form the second word of the result, and so on.  So the
6593 @var{n}th word of the result comes from the @var{n}th word of each
6594 argument.  If one argument has more words that the other, the extra
6595 words are copied unchanged into the result.
6597 For example, @samp{$(join a b,.c .o)} produces @samp{a.c b.o}.
6599 Whitespace between the words in the lists is not preserved; it is
6600 replaced with a single space.
6602 This function can merge the results of the @code{dir} and
6603 @code{notdir} functions, to produce the original list of files which
6604 was given to those two functions.@refill
6606 @item $(wildcard @var{pattern})
6607 @findex wildcard
6608 @cindex wildcard, function
6609 The argument @var{pattern} is a file name pattern, typically containing
6610 wildcard characters (as in shell file name patterns).  The result of
6611 @code{wildcard} is a space-separated list of the names of existing files
6612 that match the pattern.
6613 @xref{Wildcards, ,Using Wildcard Characters in File Names}.
6615 @item $(realpath @var{names}@dots{})
6616 @findex realpath
6617 @cindex realpath
6618 @cindex file name, realpath of
6619 For each file name in @var{names} return the canonical absolute name.
6620 A canonical name does not contain any @code{.} or @code{..} components,
6621 nor any repeated path separators (@code{/}) or symlinks.  In case of a
6622 failure the empty string is returned.  Consult the @code{realpath(3)}
6623 documentation for a list of possible failure causes.
6625 @item $(abspath @var{names}@dots{})
6626 @findex abspath
6627 @cindex abspath
6628 @cindex file name, abspath of
6629 For each file name in @var{names} return an absolute name that does
6630 not contain any @code{.} or @code{..} components, nor any repeated path
6631 separators (@code{/}).  Note that, in contrast to @code{realpath}
6632 function, @code{abspath} does not resolve symlinks and does not require
6633 the file names to refer to an existing file or directory.  Use the
6634 @code{wildcard} function to test for existence.
6635 @end table
6637 @node Foreach Function, If Function, File Name Functions, Functions
6638 @section The @code{foreach} Function
6639 @findex foreach
6640 @cindex words, iterating over
6642 The @code{foreach} function is very different from other functions.  It
6643 causes one piece of text to be used repeatedly, each time with a different
6644 substitution performed on it.  It resembles the @code{for} command in the
6645 shell @code{sh} and the @code{foreach} command in the C-shell @code{csh}.
6647 The syntax of the @code{foreach} function is:
6649 @example
6650 $(foreach @var{var},@var{list},@var{text})
6651 @end example
6653 @noindent
6654 The first two arguments, @var{var} and @var{list}, are expanded before
6655 anything else is done; note that the last argument, @var{text}, is
6656 @strong{not} expanded at the same time.  Then for each word of the expanded
6657 value of @var{list}, the variable named by the expanded value of @var{var}
6658 is set to that word, and @var{text} is expanded.  Presumably @var{text}
6659 contains references to that variable, so its expansion will be different
6660 each time.
6662 The result is that @var{text} is expanded as many times as there are
6663 whitespace-separated words in @var{list}.  The multiple expansions of
6664 @var{text} are concatenated, with spaces between them, to make the result
6665 of @code{foreach}.
6667 This simple example sets the variable @samp{files} to the list of all files
6668 in the directories in the list @samp{dirs}:
6670 @example
6671 dirs := a b c d
6672 files := $(foreach dir,$(dirs),$(wildcard $(dir)/*))
6673 @end example
6675 Here @var{text} is @samp{$(wildcard $(dir)/*)}.  The first repetition
6676 finds the value @samp{a} for @code{dir}, so it produces the same result
6677 as @samp{$(wildcard a/*)}; the second repetition produces the result
6678 of @samp{$(wildcard b/*)}; and the third, that of @samp{$(wildcard c/*)}.
6680 This example has the same result (except for setting @samp{dirs}) as
6681 the following example:
6683 @example
6684 files := $(wildcard a/* b/* c/* d/*)
6685 @end example
6687 When @var{text} is complicated, you can improve readability by giving it
6688 a name, with an additional variable:
6690 @example
6691 find_files = $(wildcard $(dir)/*)
6692 dirs := a b c d
6693 files := $(foreach dir,$(dirs),$(find_files))
6694 @end example
6696 @noindent
6697 Here we use the variable @code{find_files} this way.  We use plain @samp{=}
6698 to define a recursively-expanding variable, so that its value contains an
6699 actual function call to be reexpanded under the control of @code{foreach};
6700 a simply-expanded variable would not do, since @code{wildcard} would be
6701 called only once at the time of defining @code{find_files}.
6703 The @code{foreach} function has no permanent effect on the variable
6704 @var{var}; its value and flavor after the @code{foreach} function call are
6705 the same as they were beforehand.  The other values which are taken from
6706 @var{list} are in effect only temporarily, during the execution of
6707 @code{foreach}.  The variable @var{var} is a simply-expanded variable
6708 during the execution of @code{foreach}.  If @var{var} was undefined
6709 before the @code{foreach} function call, it is undefined after the call.
6710 @xref{Flavors, ,The Two Flavors of Variables}.@refill
6712 You must take care when using complex variable expressions that result in
6713 variable names because many strange things are valid variable names, but
6714 are probably not what you intended.  For example,
6716 @smallexample
6717 files := $(foreach Esta escrito en espanol!,b c ch,$(find_files))
6718 @end smallexample
6720 @noindent
6721 might be useful if the value of @code{find_files} references the variable
6722 whose name is @samp{Esta escrito en espanol!} (es un nombre bastante largo,
6723 no?), but it is more likely to be a mistake.
6725 @node If Function, Call Function, Foreach Function, Functions
6726 @section The @code{if} Function
6727 @findex if
6728 @cindex conditional expansion
6730 The @code{if} function provides support for conditional expansion in a
6731 functional context (as opposed to the GNU @code{make} makefile
6732 conditionals such as @code{ifeq} (@pxref{Conditional Syntax, ,Syntax of
6733 Conditionals}).
6735 An @code{if} function call can contain either two or three arguments:
6737 @example
6738 $(if @var{condition},@var{then-part}[,@var{else-part}])
6739 @end example
6741 The first argument, @var{condition}, first has all preceding and
6742 trailing whitespace stripped, then is expanded.  If it expands to any
6743 non-empty string, then the condition is considered to be true.  If it
6744 expands to an empty string, the condition is considered to be false.
6746 If the condition is true then the second argument, @var{then-part}, is
6747 evaluated and this is used as the result of the evaluation of the entire
6748 @code{if} function.
6750 If the condition is false then the third argument, @var{else-part}, is
6751 evaluated and this is the result of the @code{if} function.  If there is
6752 no third argument, the @code{if} function evaluates to nothing (the
6753 empty string).
6755 Note that only one of the @var{then-part} or the @var{else-part} will be
6756 evaluated, never both.  Thus, either can contain side-effects (such as
6757 @code{shell} function calls, etc.)
6759 @node Call Function, Value Function, If Function, Functions
6760 @section The @code{call} Function
6761 @findex call
6762 @cindex functions, user defined
6763 @cindex user defined functions
6765 The @code{call} function is unique in that it can be used to create new
6766 parameterized functions.  You can write a complex expression as the
6767 value of a variable, then use @code{call} to expand it with different
6768 values.
6770 The syntax of the @code{call} function is:
6772 @example
6773 $(call @var{variable},@var{param},@var{param},@dots{})
6774 @end example
6776 When @code{make} expands this function, it assigns each @var{param} to
6777 temporary variables @code{$(1)}, @code{$(2)}, etc.  The variable
6778 @code{$(0)} will contain @var{variable}.  There is no maximum number of
6779 parameter arguments.  There is no minimum, either, but it doesn't make
6780 sense to use @code{call} with no parameters.
6782 Then @var{variable} is expanded as a @code{make} variable in the context
6783 of these temporary assignments.  Thus, any reference to @code{$(1)} in
6784 the value of @var{variable} will resolve to the first @var{param} in the
6785 invocation of @code{call}.
6787 Note that @var{variable} is the @emph{name} of a variable, not a
6788 @emph{reference} to that variable.  Therefore you would not normally use
6789 a @samp{$} or parentheses when writing it.  (You can, however, use a
6790 variable reference in the name if you want the name not to be a
6791 constant.)
6793 If @var{variable} is the name of a builtin function, the builtin function
6794 is always invoked (even if a @code{make} variable by that name also
6795 exists).
6797 The @code{call} function expands the @var{param} arguments before
6798 assigning them to temporary variables.  This means that @var{variable}
6799 values containing references to builtin functions that have special
6800 expansion rules, like @code{foreach} or @code{if}, may not work as you
6801 expect.
6803 Some examples may make this clearer.
6805 This macro simply reverses its arguments:
6807 @smallexample
6808 reverse = $(2) $(1)
6810 foo = $(call reverse,a,b)
6811 @end smallexample
6813 @noindent
6814 Here @var{foo} will contain @samp{b a}.
6816 This one is slightly more interesting: it defines a macro to search for
6817 the first instance of a program in @code{PATH}:
6819 @smallexample
6820 pathsearch = $(firstword $(wildcard $(addsuffix /$(1),$(subst :, ,$(PATH)))))
6822 LS := $(call pathsearch,ls)
6823 @end smallexample
6825 @noindent
6826 Now the variable LS contains @code{/bin/ls} or similar.
6828 The @code{call} function can be nested.  Each recursive invocation gets
6829 its own local values for @code{$(1)}, etc.@: that mask the values of
6830 higher-level @code{call}.  For example, here is an implementation of a
6831 @dfn{map} function:
6833 @smallexample
6834 map = $(foreach a,$(2),$(call $(1),$(a)))
6835 @end smallexample
6837 Now you can @var{map} a function that normally takes only one argument,
6838 such as @code{origin}, to multiple values in one step:
6840 @smallexample
6841 o = $(call map,origin,o map MAKE)
6842 @end smallexample
6844 and end up with @var{o} containing something like @samp{file file default}.
6846 A final caution: be careful when adding whitespace to the arguments to
6847 @code{call}.  As with other functions, any whitespace contained in the
6848 second and subsequent arguments is kept; this can cause strange
6849 effects.  It's generally safest to remove all extraneous whitespace when
6850 providing parameters to @code{call}.
6852 @node Value Function, Eval Function, Call Function, Functions
6853 @comment  node-name,  next,  previous,  up
6854 @section The @code{value} Function
6855 @findex value
6856 @cindex variables, unexpanded value
6858 The @code{value} function provides a way for you to use the value of a
6859 variable @emph{without} having it expanded.  Please note that this
6860 does not undo expansions which have already occurred; for example if
6861 you create a simply expanded variable its value is expanded during the
6862 definition; in that case the @code{value} function will return the
6863 same result as using the variable directly.
6865 The syntax of the @code{value} function is:
6867 @example
6868 $(value @var{variable})
6869 @end example
6871 Note that @var{variable} is the @emph{name} of a variable; not a
6872 @emph{reference} to that variable.  Therefore you would not normally
6873 use a @samp{$} or parentheses when writing it.  (You can, however, use
6874 a variable reference in the name if you want the name not to be a
6875 constant.)
6877 The result of this function is a string containing the value of
6878 @var{variable}, without any expansion occurring.  For example, in this
6879 makefile:
6881 @example
6882 @group
6883 FOO = $PATH
6885 all:
6886         @@echo $(FOO)
6887         @@echo $(value FOO)
6888 @end group
6889 @end example
6891 @noindent
6892 The first output line would be @code{ATH}, since the ``$P'' would be
6893 expanded as a @code{make} variable, while the second output line would
6894 be the current value of your @code{$PATH} environment variable, since
6895 the @code{value} function avoided the expansion.
6897 The @code{value} function is most often used in conjunction with the
6898 @code{eval} function (@pxref{Eval Function}).
6900 @node Eval Function, Origin Function, Value Function, Functions
6901 @comment  node-name,  next,  previous,  up
6902 @section The @code{eval} Function
6903 @findex eval
6904 @cindex evaluating makefile syntax
6905 @cindex makefile syntax, evaluating
6907 The @code{eval} function is very special: it allows you to define new
6908 makefile constructs that are not constant; which are the result of
6909 evaluating other variables and functions.  The argument to the
6910 @code{eval} function is expanded, then the results of that expansion
6911 are parsed as makefile syntax.  The expanded results can define new
6912 @code{make} variables, targets, implicit or explicit rules, etc.
6914 The result of the @code{eval} function is always the empty string;
6915 thus, it can be placed virtually anywhere in a makefile without
6916 causing syntax errors.
6918 It's important to realize that the @code{eval} argument is expanded
6919 @emph{twice}; first by the @code{eval} function, then the results of
6920 that expansion are expanded again when they are parsed as makefile
6921 syntax.  This means you may need to provide extra levels of escaping
6922 for ``$'' characters when using @code{eval}.  The @code{value}
6923 function (@pxref{Value Function}) can sometimes be useful in these
6924 situations, to circumvent unwanted expansions.
6926 Here is an example of how @code{eval} can be used; this example
6927 combines a number of concepts and other functions.  Although it might
6928 seem overly complex to use @code{eval} in this example, rather than
6929 just writing out the rules, consider two things: first, the template
6930 definition (in @code{PROGRAM_template}) could need to be much more
6931 complex than it is here; and second, you might put the complex,
6932 ``generic'' part of this example into another makefile, then include
6933 it in all the individual makefiles.  Now your individual makefiles are
6934 quite straightforward.
6936 @example
6937 @group
6938 PROGRAMS    = server client
6940 server_OBJS = server.o server_priv.o server_access.o
6941 server_LIBS = priv protocol
6943 client_OBJS = client.o client_api.o client_mem.o
6944 client_LIBS = protocol
6946 # Everything after this is generic
6948 .PHONY: all
6949 all: $(PROGRAMS)
6951 define PROGRAM_template
6952  $(1): $$($(1)_OBJS) $$($(1)_LIBS:%=-l%)
6953  ALL_OBJS   += $$($(1)_OBJS)
6954 endef
6956 $(foreach prog,$(PROGRAMS),$(eval $(call PROGRAM_template,$(prog))))
6958 $(PROGRAMS):
6959         $(LINK.o) $^ $(LDLIBS) -o $@@
6961 clean:
6962         rm -f $(ALL_OBJS) $(PROGRAMS)
6963 @end group
6964 @end example
6966 @node Origin Function, Flavor Function, Eval Function, Functions
6967 @section The @code{origin} Function
6968 @findex origin
6969 @cindex variables, origin of
6970 @cindex origin of variable
6972 The @code{origin} function is unlike most other functions in that it does
6973 not operate on the values of variables; it tells you something @emph{about}
6974 a variable.  Specifically, it tells you where it came from.
6976 The syntax of the @code{origin} function is:
6978 @example
6979 $(origin @var{variable})
6980 @end example
6982 Note that @var{variable} is the @emph{name} of a variable to inquire about;
6983 not a @emph{reference} to that variable.  Therefore you would not normally
6984 use a @samp{$} or parentheses when writing it.  (You can, however, use a
6985 variable reference in the name if you want the name not to be a constant.)
6987 The result of this function is a string telling you how the variable
6988 @var{variable} was defined:
6990 @table @samp
6991 @item undefined
6993 if @var{variable} was never defined.
6995 @item default
6997 if @var{variable} has a default definition, as is usual with @code{CC}
6998 and so on.  @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
6999 Note that if you have redefined a default variable, the @code{origin}
7000 function will return the origin of the later definition.
7002 @item environment
7004 if @var{variable} was defined as an environment variable and the
7005 @samp{-e} option is @emph{not} turned on (@pxref{Options Summary, ,Summary of Options}).
7007 @item environment override
7009 if @var{variable} was defined as an environment variable and the
7010 @w{@samp{-e}} option @emph{is} turned on (@pxref{Options Summary,
7011 ,Summary of Options}).@refill
7013 @item file
7015 if @var{variable} was defined in a makefile.
7017 @item command line
7019 if @var{variable} was defined on the command line.
7021 @item override
7023 if @var{variable} was defined with an @code{override} directive in a
7024 makefile (@pxref{Override Directive, ,The @code{override} Directive}).
7026 @item automatic
7028 if @var{variable} is an automatic variable defined for the
7029 execution of the commands for each rule
7030 (@pxref{Automatic Variables}).
7031 @end table
7033 This information is primarily useful (other than for your curiosity) to
7034 determine if you want to believe the value of a variable.  For example,
7035 suppose you have a makefile @file{foo} that includes another makefile
7036 @file{bar}.  You want a variable @code{bletch} to be defined in @file{bar}
7037 if you run the command @w{@samp{make -f bar}}, even if the environment contains
7038 a definition of @code{bletch}.  However, if @file{foo} defined
7039 @code{bletch} before including @file{bar}, you do not want to override that
7040 definition.  This could be done by using an @code{override} directive in
7041 @file{foo}, giving that definition precedence over the later definition in
7042 @file{bar}; unfortunately, the @code{override} directive would also
7043 override any command line definitions.  So, @file{bar} could
7044 include:@refill
7046 @example
7047 @group
7048 ifdef bletch
7049 ifeq "$(origin bletch)" "environment"
7050 bletch = barf, gag, etc.
7051 endif
7052 endif
7053 @end group
7054 @end example
7056 @noindent
7057 If @code{bletch} has been defined from the environment, this will redefine
7060 If you want to override a previous definition of @code{bletch} if it came
7061 from the environment, even under @samp{-e}, you could instead write:
7063 @example
7064 @group
7065 ifneq "$(findstring environment,$(origin bletch))" ""
7066 bletch = barf, gag, etc.
7067 endif
7068 @end group
7069 @end example
7071 Here the redefinition takes place if @samp{$(origin bletch)} returns either
7072 @samp{environment} or @samp{environment override}.
7073 @xref{Text Functions, , Functions for String Substitution and Analysis}.
7075 @node Flavor Function, Shell Function, Origin Function, Functions
7076 @section The @code{flavor} Function
7077 @findex flavor
7078 @cindex variables, flavor of
7079 @cindex flavor of variable
7081 The @code{flavor} function is unlike most other functions (and like
7082 @code{origin} function) in that it does not operate on the values of
7083 variables; it tells you something @emph{about} a variable.
7084 Specifically, it tells you the flavor of a variable (@pxref{Flavors,
7085 ,The Two Flavors of Variables}).
7087 The syntax of the @code{flavor} function is:
7089 @example
7090 $(flavor @var{variable})
7091 @end example
7093 Note that @var{variable} is the @emph{name} of a variable to inquire about;
7094 not a @emph{reference} to that variable.  Therefore you would not normally
7095 use a @samp{$} or parentheses when writing it.  (You can, however, use a
7096 variable reference in the name if you want the name not to be a constant.)
7098 The result of this function is a string that identifies the flavor of the
7099 variable @var{variable}:
7101 @table @samp
7102 @item undefined
7104 if @var{variable} was never defined.
7106 @item recursive
7108 if @var{variable} is a recursively expanded variable.
7110 @item simple
7112 if @var{variable} is a simply expanded variable.
7114 @end table
7117 @node Shell Function, Make Control Functions, Flavor Function, Functions
7118 @section The @code{shell} Function
7119 @findex shell
7120 @cindex commands, expansion
7121 @cindex backquotes
7122 @cindex shell command, function for
7124 The @code{shell} function is unlike any other function other than the
7125 @code{wildcard} function
7126 (@pxref{Wildcard Function, ,The Function @code{wildcard}}) in that it
7127 communicates with the world outside of @code{make}.
7129 The @code{shell} function performs the same function that backquotes
7130 (@samp{`}) perform in most shells: it does @dfn{command expansion}.
7131 This means that it takes as an argument a shell command and evaluates
7132 to the output of the command.  The only processing @code{make} does on
7133 the result is to convert each newline (or carriage-return / newline
7134 pair) to a single space.  If there is a trailing (carriage-return
7135 and) newline it will simply be removed.@refill
7137 The commands run by calls to the @code{shell} function are run when the
7138 function calls are expanded (@pxref{Reading Makefiles, , How
7139 @code{make} Reads a Makefile}).  Because this function involves
7140 spawning a new shell, you should carefully consider the performance
7141 implications of using the @code{shell} function within recursively
7142 expanded variables vs.@: simply expanded variables (@pxref{Flavors, ,The
7143 Two Flavors of Variables}).
7145 Here are some examples of the use of the @code{shell} function:
7147 @example
7148 contents := $(shell cat foo)
7149 @end example
7151 @noindent
7152 sets @code{contents} to the contents of the file @file{foo}, with a space
7153 (rather than a newline) separating each line.
7155 @example
7156 files := $(shell echo *.c)
7157 @end example
7159 @noindent
7160 sets @code{files} to the expansion of @samp{*.c}.  Unless @code{make} is
7161 using a very strange shell, this has the same result as
7162 @w{@samp{$(wildcard *.c)}} (as long as at least one @samp{.c} file
7163 exists).@refill
7165 @node Make Control Functions,  , Shell Function, Functions
7166 @section Functions That Control Make
7167 @cindex functions, for controlling make
7168 @cindex controlling make
7170 These functions control the way make runs.  Generally, they are used to
7171 provide information to the user of the makefile or to cause make to stop
7172 if some sort of environmental error is detected.
7174 @table @code
7175 @item $(error @var{text}@dots{})
7176 @findex error
7177 @cindex error, stopping on
7178 @cindex stopping make
7179 Generates a fatal error where the message is @var{text}.  Note that the
7180 error is generated whenever this function is evaluated.  So, if you put
7181 it inside a command script or on the right side of a recursive variable
7182 assignment, it won't be evaluated until later.  The @var{text} will be
7183 expanded before the error is generated.
7185 For example,
7187 @example
7188 ifdef ERROR1
7189 $(error error is $(ERROR1))
7190 endif
7191 @end example
7193 @noindent
7194 will generate a fatal error during the read of the makefile if the
7195 @code{make} variable @code{ERROR1} is defined.  Or,
7197 @example
7198 ERR = $(error found an error!)
7200 .PHONY: err
7201 err: ; $(ERR)
7202 @end example
7204 @noindent
7205 will generate a fatal error while @code{make} is running, if the
7206 @code{err} target is invoked.
7208 @item $(warning @var{text}@dots{})
7209 @findex warning
7210 @cindex warnings, printing
7211 @cindex printing user warnings
7212 This function works similarly to the @code{error} function, above,
7213 except that @code{make} doesn't exit.  Instead, @var{text} is expanded
7214 and the resulting message is displayed, but processing of the makefile
7215 continues.
7217 The result of the expansion of this function is the empty string.
7219 @item $(info @var{text}@dots{})
7220 @findex info
7221 @cindex printing messages
7222 This function does nothing more than print its (expanded) argument(s)
7223 to standard output.  No makefile name or line number is added.  The
7224 result of the expansion of this function is the empty string.
7225 @end table
7227 @node Running, Implicit Rules, Functions, Top
7228 @chapter How to Run @code{make}
7230 A makefile that says how to recompile a program can be used in more
7231 than one way.  The simplest use is to recompile every file that is out
7232 of date.  Usually, makefiles are written so that if you run
7233 @code{make} with no arguments, it does just that.
7235 But you might want to update only some of the files; you might want to use
7236 a different compiler or different compiler options; you might want just to
7237 find out which files are out of date without changing them.
7239 By giving arguments when you run @code{make}, you can do any of these
7240 things and many others.
7242 @cindex exit status of make
7243 The exit status of @code{make} is always one of three values:
7244 @table @code
7245 @item 0
7246 The exit status is zero if @code{make} is successful.
7247 @item 2
7248 The exit status is two if @code{make} encounters any errors.
7249 It will print messages describing the particular errors.
7250 @item 1
7251 The exit status is one if you use the @samp{-q} flag and @code{make}
7252 determines that some target is not already up to date.
7253 @xref{Instead of Execution, ,Instead of Executing the Commands}.
7254 @end table
7256 @menu
7257 * Makefile Arguments::          How to specify which makefile to use.
7258 * Goals::                       How to use goal arguments to specify which
7259                                   parts of the makefile to use.
7260 * Instead of Execution::        How to use mode flags to specify what
7261                                   kind of thing to do with the commands
7262                                   in the makefile other than simply
7263                                   execute them.
7264 * Avoiding Compilation::        How to avoid recompiling certain files.
7265 * Overriding::                  How to override a variable to specify
7266                                   an alternate compiler and other things.
7267 * Testing::                     How to proceed past some errors, to
7268                                   test compilation.
7269 * Options Summary::             Summary of Options
7270 @end menu
7272 @node Makefile Arguments, Goals, Running, Running
7273 @section Arguments to Specify the Makefile
7274 @cindex @code{--file}
7275 @cindex @code{--makefile}
7276 @cindex @code{-f}
7278 The way to specify the name of the makefile is with the @samp{-f} or
7279 @samp{--file} option (@samp{--makefile} also works).  For example,
7280 @samp{-f altmake} says to use the file @file{altmake} as the makefile.
7282 If you use the @samp{-f} flag several times and follow each @samp{-f}
7283 with an argument, all the specified files are used jointly as
7284 makefiles.
7286 If you do not use the @samp{-f} or @samp{--file} flag, the default is
7287 to try @file{GNUmakefile}, @file{makefile}, and @file{Makefile}, in
7288 that order, and use the first of these three which exists or can be made
7289 (@pxref{Makefiles, ,Writing Makefiles}).@refill
7291 @node Goals, Instead of Execution, Makefile Arguments, Running
7292 @section Arguments to Specify the Goals
7293 @cindex goal, how to specify
7295 The @dfn{goals} are the targets that @code{make} should strive ultimately
7296 to update.  Other targets are updated as well if they appear as
7297 prerequisites of goals, or prerequisites of prerequisites of goals, etc.
7299 By default, the goal is the first target in the makefile (not counting
7300 targets that start with a period).  Therefore, makefiles are usually
7301 written so that the first target is for compiling the entire program or
7302 programs they describe.  If the first rule in the makefile has several
7303 targets, only the first target in the rule becomes the default goal, not
7304 the whole list.  You can manage the selection of the default goal from
7305 within your makefile using the @code{.DEFAULT_GOAL} variable
7306 (@pxref{Special Variables, , Other Special Variables}).
7308 You can also specify a different goal or goals with command-line
7309 arguments to @code{make}.  Use the name of the goal as an argument.
7310 If you specify several goals, @code{make} processes each of them in
7311 turn, in the order you name them.
7313 Any target in the makefile may be specified as a goal (unless it
7314 starts with @samp{-} or contains an @samp{=}, in which case it will be
7315 parsed as a switch or variable definition, respectively).  Even
7316 targets not in the makefile may be specified, if @code{make} can find
7317 implicit rules that say how to make them.
7319 @vindex MAKECMDGOALS
7320 @code{Make} will set the special variable @code{MAKECMDGOALS} to the
7321 list of goals you specified on the command line.  If no goals were given
7322 on the command line, this variable is empty.  Note that this variable
7323 should be used only in special circumstances.
7325 An example of appropriate use is to avoid including @file{.d} files
7326 during @code{clean} rules (@pxref{Automatic Prerequisites}), so
7327 @code{make} won't create them only to immediately remove them
7328 again:@refill
7330 @example
7331 @group
7332 sources = foo.c bar.c
7334 ifneq ($(MAKECMDGOALS),clean)
7335 include $(sources:.c=.d)
7336 endif
7337 @end group
7338 @end example
7340 One use of specifying a goal is if you want to compile only a part of
7341 the program, or only one of several programs.  Specify as a goal each
7342 file that you wish to remake.  For example, consider a directory containing
7343 several programs, with a makefile that starts like this:
7345 @example
7346 .PHONY: all
7347 all: size nm ld ar as
7348 @end example
7350 If you are working on the program @code{size}, you might want to say
7351 @w{@samp{make size}} so that only the files of that program are recompiled.
7353 Another use of specifying a goal is to make files that are not normally
7354 made.  For example, there may be a file of debugging output, or a
7355 version of the program that is compiled specially for testing, which has
7356 a rule in the makefile but is not a prerequisite of the default goal.
7358 Another use of specifying a goal is to run the commands associated with
7359 a phony target (@pxref{Phony Targets}) or empty target (@pxref{Empty
7360 Targets, ,Empty Target Files to Record Events}).  Many makefiles contain
7361 a phony target named @file{clean} which deletes everything except source
7362 files.  Naturally, this is done only if you request it explicitly with
7363 @w{@samp{make clean}}.  Following is a list of typical phony and empty
7364 target names.  @xref{Standard Targets}, for a detailed list of all the
7365 standard target names which GNU software packages use.
7367 @table @file
7368 @item all
7369 @cindex @code{all} @r{(standard target)}
7370 Make all the top-level targets the makefile knows about.
7372 @item clean
7373 @cindex @code{clean} @r{(standard target)}
7374 Delete all files that are normally created by running @code{make}.
7376 @item mostlyclean
7377 @cindex @code{mostlyclean} @r{(standard target)}
7378 Like @samp{clean}, but may refrain from deleting a few files that people
7379 normally don't want to recompile.  For example, the @samp{mostlyclean}
7380 target for GCC does not delete @file{libgcc.a}, because recompiling it
7381 is rarely necessary and takes a lot of time.
7383 @item distclean
7384 @cindex @code{distclean} @r{(standard target)}
7385 @itemx realclean
7386 @cindex @code{realclean} @r{(standard target)}
7387 @itemx clobber
7388 @cindex @code{clobber} @r{(standard target)}
7389 Any of these targets might be defined to delete @emph{more} files than
7390 @samp{clean} does.  For example, this would delete configuration files
7391 or links that you would normally create as preparation for compilation,
7392 even if the makefile itself cannot create these files.
7394 @item install
7395 @cindex @code{install} @r{(standard target)}
7396 Copy the executable file into a directory that users typically search
7397 for commands; copy any auxiliary files that the executable uses into
7398 the directories where it will look for them.
7400 @item print
7401 @cindex @code{print} @r{(standard target)}
7402 Print listings of the source files that have changed.
7404 @item tar
7405 @cindex @code{tar} @r{(standard target)}
7406 Create a tar file of the source files.
7408 @item shar
7409 @cindex @code{shar} @r{(standard target)}
7410 Create a shell archive (shar file) of the source files.
7412 @item dist
7413 @cindex @code{dist} @r{(standard target)}
7414 Create a distribution file of the source files.  This might
7415 be a tar file, or a shar file, or a compressed version of one of the
7416 above, or even more than one of the above.
7418 @item TAGS
7419 @cindex @code{TAGS} @r{(standard target)}
7420 Update a tags table for this program.
7422 @item check
7423 @cindex @code{check} @r{(standard target)}
7424 @itemx test
7425 @cindex @code{test} @r{(standard target)}
7426 Perform self tests on the program this makefile builds.
7427 @end table
7429 @node Instead of Execution, Avoiding Compilation, Goals, Running
7430 @section Instead of Executing the Commands
7431 @cindex execution, instead of
7432 @cindex commands, instead of executing
7434 The makefile tells @code{make} how to tell whether a target is up to date,
7435 and how to update each target.  But updating the targets is not always
7436 what you want.  Certain options specify other activities for @code{make}.
7438 @comment Extra blank lines make it print better.
7439 @table @samp
7440 @item -n
7441 @itemx --just-print
7442 @itemx --dry-run
7443 @itemx --recon
7444 @cindex @code{--just-print}
7445 @cindex @code{--dry-run}
7446 @cindex @code{--recon}
7447 @cindex @code{-n}
7449 ``No-op''.  The activity is to print what commands would be used to make
7450 the targets up to date, but not actually execute them.
7452 @item -t
7453 @itemx --touch
7454 @cindex @code{--touch}
7455 @cindex touching files
7456 @cindex target, touching
7457 @cindex @code{-t}
7459 ``Touch''.  The activity is to mark the targets as up to date without
7460 actually changing them.  In other words, @code{make} pretends to compile
7461 the targets but does not really change their contents.
7463 @item -q
7464 @itemx --question
7465 @cindex @code{--question}
7466 @cindex @code{-q}
7467 @cindex question mode
7469 ``Question''.  The activity is to find out silently whether the targets
7470 are up to date already; but execute no commands in either case.  In other
7471 words, neither compilation nor output will occur.
7473 @item -W @var{file}
7474 @itemx --what-if=@var{file}
7475 @itemx --assume-new=@var{file}
7476 @itemx --new-file=@var{file}
7477 @cindex @code{--what-if}
7478 @cindex @code{-W}
7479 @cindex @code{--assume-new}
7480 @cindex @code{--new-file}
7481 @cindex what if
7482 @cindex files, assuming new
7484 ``What if''.  Each @samp{-W} flag is followed by a file name.  The given
7485 files' modification times are recorded by @code{make} as being the present
7486 time, although the actual modification times remain the same.
7487 You can use the @samp{-W} flag in conjunction with the @samp{-n} flag
7488 to see what would happen if you were to modify specific files.@refill
7489 @end table
7491 With the @samp{-n} flag, @code{make} prints the commands that it would
7492 normally execute but does not execute them.
7494 With the @samp{-t} flag, @code{make} ignores the commands in the rules
7495 and uses (in effect) the command @code{touch} for each target that needs to
7496 be remade.  The @code{touch} command is also printed, unless @samp{-s} or
7497 @code{.SILENT} is used.  For speed, @code{make} does not actually invoke
7498 the program @code{touch}.  It does the work directly.
7500 With the @samp{-q} flag, @code{make} prints nothing and executes no
7501 commands, but the exit status code it returns is zero if and only if the
7502 targets to be considered are already up to date.  If the exit status is
7503 one, then some updating needs to be done.  If @code{make} encounters an
7504 error, the exit status is two, so you can distinguish an error from a
7505 target that is not up to date.
7507 It is an error to use more than one of these three flags in the same
7508 invocation of @code{make}.
7510 @cindex +, and command execution
7511 The @samp{-n}, @samp{-t}, and @samp{-q} options do not affect command
7512 lines that begin with @samp{+} characters or contain the strings
7513 @samp{$(MAKE)} or @samp{$@{MAKE@}}.  Note that only the line containing
7514 the @samp{+} character or the strings @samp{$(MAKE)} or @samp{$@{MAKE@}}
7515 is run regardless of these options.  Other lines in the same rule are
7516 not run unless they too begin with @samp{+} or contain @samp{$(MAKE)} or
7517 @samp{$@{MAKE@}} (@xref{MAKE Variable, ,How the @code{MAKE} Variable Works}.)
7519 The @samp{-W} flag provides two features:
7521 @itemize @bullet
7522 @item
7523 If you also use the @samp{-n} or @samp{-q} flag, you can see what
7524 @code{make} would do if you were to modify some files.
7526 @item
7527 Without the @samp{-n} or @samp{-q} flag, when @code{make} is actually
7528 executing commands, the @samp{-W} flag can direct @code{make} to act
7529 as if some files had been modified, without actually modifying the
7530 files.@refill
7531 @end itemize
7533 Note that the options @samp{-p} and @samp{-v} allow you to obtain other
7534 information about @code{make} or about the makefiles in use
7535 (@pxref{Options Summary, ,Summary of Options}).@refill
7537 @node Avoiding Compilation, Overriding, Instead of Execution, Running
7538 @section Avoiding Recompilation of Some Files
7539 @cindex @code{-o}
7540 @cindex @code{--old-file}
7541 @cindex @code{--assume-old}
7542 @cindex files, assuming old
7543 @cindex files, avoiding recompilation of
7544 @cindex recompilation, avoiding
7546 Sometimes you may have changed a source file but you do not want to
7547 recompile all the files that depend on it.  For example, suppose you add
7548 a macro or a declaration to a header file that many other files depend
7549 on.  Being conservative, @code{make} assumes that any change in the
7550 header file requires recompilation of all dependent files, but you know
7551 that they do not need to be recompiled and you would rather not waste
7552 the time waiting for them to compile.
7554 If you anticipate the problem before changing the header file, you can
7555 use the @samp{-t} flag.  This flag tells @code{make} not to run the
7556 commands in the rules, but rather to mark the target up to date by
7557 changing its last-modification date.  You would follow this procedure:
7559 @enumerate
7560 @item
7561 Use the command @samp{make} to recompile the source files that really
7562 need recompilation, ensuring that the object files are up-to-date
7563 before you begin.
7565 @item
7566 Make the changes in the header files.
7568 @item
7569 Use the command @samp{make -t} to mark all the object files as
7570 up to date.  The next time you run @code{make}, the changes in the
7571 header files will not cause any recompilation.
7572 @end enumerate
7574 If you have already changed the header file at a time when some files
7575 do need recompilation, it is too late to do this.  Instead, you can
7576 use the @w{@samp{-o @var{file}}} flag, which marks a specified file as
7577 ``old'' (@pxref{Options Summary, ,Summary of Options}).  This means
7578 that the file itself will not be remade, and nothing else will be
7579 remade on its account.  Follow this procedure:
7581 @enumerate
7582 @item
7583 Recompile the source files that need compilation for reasons independent
7584 of the particular header file, with @samp{make -o @var{headerfile}}.
7585 If several header files are involved, use a separate @samp{-o} option
7586 for each header file.
7588 @item
7589 Touch all the object files with @samp{make -t}.
7590 @end enumerate
7592 @node Overriding, Testing, Avoiding Compilation, Running
7593 @section Overriding Variables
7594 @cindex overriding variables with arguments
7595 @cindex variables, overriding with arguments
7596 @cindex command line variables
7597 @cindex variables, command line
7599 An argument that contains @samp{=} specifies the value of a variable:
7600 @samp{@var{v}=@var{x}} sets the value of the variable @var{v} to @var{x}.
7601 If you specify a value in this way, all ordinary assignments of the same
7602 variable in the makefile are ignored; we say they have been
7603 @dfn{overridden} by the command line argument.
7605 The most common way to use this facility is to pass extra flags to
7606 compilers.  For example, in a properly written makefile, the variable
7607 @code{CFLAGS} is included in each command that runs the C compiler, so a
7608 file @file{foo.c} would be compiled something like this:
7610 @example
7611 cc -c $(CFLAGS) foo.c
7612 @end example
7614 Thus, whatever value you set for @code{CFLAGS} affects each compilation
7615 that occurs.  The makefile probably specifies the usual value for
7616 @code{CFLAGS}, like this:
7618 @example
7619 CFLAGS=-g
7620 @end example
7622 Each time you run @code{make}, you can override this value if you
7623 wish.  For example, if you say @samp{make CFLAGS='-g -O'}, each C
7624 compilation will be done with @samp{cc -c -g -O}.  (This also
7625 illustrates how you can use quoting in the shell to enclose spaces and
7626 other special characters in the value of a variable when you override
7627 it.)
7629 The variable @code{CFLAGS} is only one of many standard variables that
7630 exist just so that you can change them this way.  @xref{Implicit
7631 Variables, , Variables Used by Implicit Rules}, for a complete list.
7633 You can also program the makefile to look at additional variables of your
7634 own, giving the user the ability to control other aspects of how the
7635 makefile works by changing the variables.
7637 When you override a variable with a command argument, you can define either
7638 a recursively-expanded variable or a simply-expanded variable.  The
7639 examples shown above make a recursively-expanded variable; to make a
7640 simply-expanded variable, write @samp{:=} instead of @samp{=}.  But, unless
7641 you want to include a variable reference or function call in the
7642 @emph{value} that you specify, it makes no difference which kind of
7643 variable you create.
7645 There is one way that the makefile can change a variable that you have
7646 overridden.  This is to use the @code{override} directive, which is a line
7647 that looks like this: @samp{override @var{variable} = @var{value}}
7648 (@pxref{Override Directive, ,The @code{override} Directive}).
7650 @node Testing, Options Summary, Overriding, Running
7651 @section Testing the Compilation of a Program
7652 @cindex testing compilation
7653 @cindex compilation, testing
7655 Normally, when an error happens in executing a shell command, @code{make}
7656 gives up immediately, returning a nonzero status.  No further commands are
7657 executed for any target.  The error implies that the goal cannot be
7658 correctly remade, and @code{make} reports this as soon as it knows.
7660 When you are compiling a program that you have just changed, this is not
7661 what you want.  Instead, you would rather that @code{make} try compiling
7662 every file that can be tried, to show you as many compilation errors
7663 as possible.
7665 @cindex @code{-k}
7666 @cindex @code{--keep-going}
7667 On these occasions, you should use the @samp{-k} or
7668 @samp{--keep-going} flag.  This tells @code{make} to continue to
7669 consider the other prerequisites of the pending targets, remaking them
7670 if necessary, before it gives up and returns nonzero status.  For
7671 example, after an error in compiling one object file, @samp{make -k}
7672 will continue compiling other object files even though it already
7673 knows that linking them will be impossible.  In addition to continuing
7674 after failed shell commands, @samp{make -k} will continue as much as
7675 possible after discovering that it does not know how to make a target
7676 or prerequisite file.  This will always cause an error message, but
7677 without @samp{-k}, it is a fatal error (@pxref{Options Summary,
7678 ,Summary of Options}).@refill
7680 The usual behavior of @code{make} assumes that your purpose is to get the
7681 goals up to date; once @code{make} learns that this is impossible, it might
7682 as well report the failure immediately.  The @samp{-k} flag says that the
7683 real purpose is to test as much as possible of the changes made in the
7684 program, perhaps to find several independent problems so that you can
7685 correct them all before the next attempt to compile.  This is why Emacs'
7686 @kbd{M-x compile} command passes the @samp{-k} flag by default.
7688 @node Options Summary,  , Testing, Running
7689 @section Summary of Options
7690 @cindex options
7691 @cindex flags
7692 @cindex switches
7694 Here is a table of all the options @code{make} understands:
7696 @table @samp
7697 @item -b
7698 @cindex @code{-b}
7699 @itemx -m
7700 @cindex @code{-m}
7701 These options are ignored for compatibility with other versions of @code{make}.
7703 @item -B
7704 @cindex @code{-B}
7705 @itemx --always-make
7706 @cindex @code{--always-make}
7707 Consider all targets out-of-date.  GNU @code{make} proceeds to
7708 consider targets and their prerequisites using the normal algorithms;
7709 however, all targets so considered are always remade regardless of the
7710 status of their prerequisites.  To avoid infinite recursion, if
7711 @code{MAKE_RESTARTS} (@pxref{Special Variables, , Other Special
7712 Variables}) is set to a number greater than 0 this option is disabled
7713 when considering whether to remake makefiles (@pxref{Remaking
7714 Makefiles, , How Makefiles Are Remade}).
7716 @item -C @var{dir}
7717 @cindex @code{-C}
7718 @itemx --directory=@var{dir}
7719 @cindex @code{--directory}
7720 Change to directory @var{dir} before reading the makefiles.  If multiple
7721 @samp{-C} options are specified, each is interpreted relative to the
7722 previous one: @samp{-C / -C etc} is equivalent to @samp{-C /etc}.
7723 This is typically used with recursive invocations of @code{make}
7724 (@pxref{Recursion, ,Recursive Use of @code{make}}).
7726 @item -d
7727 @cindex @code{-d}
7728 @c Extra blank line here makes the table look better.
7730 Print debugging information in addition to normal processing.  The
7731 debugging information says which files are being considered for
7732 remaking, which file-times are being compared and with what results,
7733 which files actually need to be remade, which implicit rules are
7734 considered and which are applied---everything interesting about how
7735 @code{make} decides what to do.  The @code{-d} option is equivalent to
7736 @samp{--debug=a} (see below).
7738 @item --debug[=@var{options}]
7739 @cindex @code{--debug}
7740 @c Extra blank line here makes the table look better.
7742 Print debugging information in addition to normal processing.  Various
7743 levels and types of output can be chosen.  With no arguments, print the
7744 ``basic'' level of debugging.  Possible arguments are below; only the
7745 first character is considered, and values must be comma- or
7746 space-separated.
7748 @table @code
7749 @item a (@i{all})
7750 All types of debugging output are enabled.  This is equivalent to using
7751 @samp{-d}.
7753 @item b (@i{basic})
7754 Basic debugging prints each target that was found to be out-of-date, and
7755 whether the build was successful or not.
7757 @item v (@i{verbose})
7758 A level above @samp{basic}; includes messages about which makefiles were
7759 parsed, prerequisites that did not need to be rebuilt, etc.  This option
7760 also enables @samp{basic} messages.
7762 @item i (@i{implicit})
7763 Prints messages describing the implicit rule searches for each target.
7764 This option also enables @samp{basic} messages.
7766 @item j (@i{jobs})
7767 Prints messages giving details on the invocation of specific subcommands.
7769 @item m (@i{makefile})
7770 By default, the above messages are not enabled while trying to remake
7771 the makefiles.  This option enables messages while rebuilding makefiles,
7772 too.  Note that the @samp{all} option does enable this option.  This
7773 option also enables @samp{basic} messages.
7774 @end table
7776 @item -e
7777 @cindex @code{-e}
7778 @itemx --environment-overrides
7779 @cindex @code{--environment-overrides}
7780 Give variables taken from the environment precedence
7781 over variables from makefiles.
7782 @xref{Environment, ,Variables from the Environment}.
7784 @item -f @var{file}
7785 @cindex @code{-f}
7786 @itemx --file=@var{file}
7787 @cindex @code{--file}
7788 @itemx --makefile=@var{file}
7789 @cindex @code{--makefile}
7790 Read the file named @var{file} as a makefile.
7791 @xref{Makefiles, ,Writing Makefiles}.
7793 @item -h
7794 @cindex @code{-h}
7795 @itemx --help
7796 @cindex @code{--help}
7797 @c Extra blank line here makes the table look better.
7799 Remind you of the options that @code{make} understands and then exit.
7801 @item -i
7802 @cindex @code{-i}
7803 @itemx --ignore-errors
7804 @cindex @code{--ignore-errors}
7805 Ignore all errors in commands executed to remake files.
7806 @xref{Errors, ,Errors in Commands}.
7808 @item -I @var{dir}
7809 @cindex @code{-I}
7810 @itemx --include-dir=@var{dir}
7811 @cindex @code{--include-dir}
7812 Specifies a directory @var{dir} to search for included makefiles.
7813 @xref{Include, ,Including Other Makefiles}.  If several @samp{-I}
7814 options are used to specify several directories, the directories are
7815 searched in the order specified.
7817 @item -j [@var{jobs}]
7818 @cindex @code{-j}
7819 @itemx --jobs[=@var{jobs}]
7820 @cindex @code{--jobs}
7821 Specifies the number of jobs (commands) to run simultaneously.  With no
7822 argument, @code{make} runs as many jobs simultaneously as possible.  If
7823 there is more than one @samp{-j} option, the last one is effective.
7824 @xref{Parallel, ,Parallel Execution},
7825 for more information on how commands are run.
7826 Note that this option is ignored on MS-DOS.
7828 @item -k
7829 @cindex @code{-k}
7830 @itemx --keep-going
7831 @cindex @code{--keep-going}
7832 Continue as much as possible after an error.  While the target that
7833 failed, and those that depend on it, cannot be remade, the other
7834 prerequisites of these targets can be processed all the same.
7835 @xref{Testing, ,Testing the Compilation of a Program}.
7837 @item -l [@var{load}]
7838 @cindex @code{-l}
7839 @itemx --load-average[=@var{load}]
7840 @cindex @code{--load-average}
7841 @itemx --max-load[=@var{load}]
7842 @cindex @code{--max-load}
7843 Specifies that no new jobs (commands) should be started if there are
7844 other jobs running and the load average is at least @var{load} (a
7845 floating-point number).  With no argument, removes a previous load
7846 limit.  @xref{Parallel, ,Parallel Execution}.
7848 @item -L
7849 @cindex @code{-L}
7850 @itemx --check-symlink-times
7851 @cindex @code{--check-symlink-times}
7852 On systems that support symbolic links, this option causes @code{make}
7853 to consider the timestamps on any symbolic links in addition to the
7854 timestamp on the file referenced by those links.  When this option is
7855 provided, the most recent timestamp among the file and the symbolic
7856 links is taken as the modification time for this target file.
7858 @item -n
7859 @cindex @code{-n}
7860 @itemx --just-print
7861 @cindex @code{--just-print}
7862 @itemx --dry-run
7863 @cindex @code{--dry-run}
7864 @itemx --recon
7865 @cindex @code{--recon}
7866 @c Extra blank line here makes the table look better.
7868 Print the commands that would be executed, but do not execute them.
7869 @xref{Instead of Execution, ,Instead of Executing the Commands}.
7871 @item -o @var{file}
7872 @cindex @code{-o}
7873 @itemx --old-file=@var{file}
7874 @cindex @code{--old-file}
7875 @itemx --assume-old=@var{file}
7876 @cindex @code{--assume-old}
7877 Do not remake the file @var{file} even if it is older than its
7878 prerequisites, and do not remake anything on account of changes in
7879 @var{file}.  Essentially the file is treated as very old and its rules
7880 are ignored.  @xref{Avoiding Compilation, ,Avoiding Recompilation of
7881 Some Files}.@refill
7883 @item -p
7884 @cindex @code{-p}
7885 @itemx --print-data-base
7886 @cindex @code{--print-data-base}
7887 @cindex data base of @code{make} rules
7888 @cindex predefined rules and variables, printing
7889 Print the data base (rules and variable values) that results from
7890 reading the makefiles; then execute as usual or as otherwise specified.
7891 This also prints the version information given by the @samp{-v} switch
7892 (see below).  To print the data base without trying to remake any files,
7893 use @w{@samp{make -qp}}.  To print the data base of predefined rules and
7894 variables, use @w{@samp{make -p -f /dev/null}}.  The data base output
7895 contains filename and linenumber information for command and variable
7896 definitions, so it can be a useful debugging tool in complex environments.
7898 @item -q
7899 @cindex @code{-q}
7900 @itemx --question
7901 @cindex @code{--question}
7902 ``Question mode''.  Do not run any commands, or print anything; just
7903 return an exit status that is zero if the specified targets are already
7904 up to date, one if any remaking is required, or two if an error is
7905 encountered.  @xref{Instead of Execution, ,Instead of Executing the
7906 Commands}.@refill
7908 @item -r
7909 @cindex @code{-r}
7910 @itemx --no-builtin-rules
7911 @cindex @code{--no-builtin-rules}
7912 Eliminate use of the built-in implicit rules (@pxref{Implicit Rules,
7913 ,Using Implicit Rules}).  You can still define your own by writing
7914 pattern rules (@pxref{Pattern Rules, ,Defining and Redefining Pattern
7915 Rules}).  The @samp{-r} option also clears out the default list of
7916 suffixes for suffix rules (@pxref{Suffix Rules, ,Old-Fashioned Suffix
7917 Rules}).  But you can still define your own suffixes with a rule for
7918 @code{.SUFFIXES}, and then define your own suffix rules.  Note that only
7919 @emph{rules} are affected by the @code{-r} option; default variables
7920 remain in effect (@pxref{Implicit Variables, ,Variables Used by Implicit
7921 Rules}); see the @samp{-R} option below.
7923 @item -R
7924 @cindex @code{-R}
7925 @itemx --no-builtin-variables
7926 @cindex @code{--no-builtin-variables}
7927 Eliminate use of the built-in rule-specific variables (@pxref{Implicit
7928 Variables, ,Variables Used by Implicit Rules}).  You can still define
7929 your own, of course.  The @samp{-R} option also automatically enables
7930 the @samp{-r} option (see above), since it doesn't make sense to have
7931 implicit rules without any definitions for the variables that they use.
7933 @item -s
7934 @cindex @code{-s}
7935 @itemx --silent
7936 @cindex @code{--silent}
7937 @itemx --quiet
7938 @cindex @code{--quiet}
7939 @c Extra blank line here makes the table look better.
7941 Silent operation; do not print the commands as they are executed.
7942 @xref{Echoing, ,Command Echoing}.
7944 @item -S
7945 @cindex @code{-S}
7946 @itemx --no-keep-going
7947 @cindex @code{--no-keep-going}
7948 @itemx --stop
7949 @cindex @code{--stop}
7950 @c Extra blank line here makes the table look better.
7952 Cancel the effect of the @samp{-k} option.  This is never necessary
7953 except in a recursive @code{make} where @samp{-k} might be inherited
7954 from the top-level @code{make} via @code{MAKEFLAGS}
7955 (@pxref{Recursion, ,Recursive Use of @code{make}})
7956 or if you set @samp{-k} in @code{MAKEFLAGS} in your environment.@refill
7958 @item -t
7959 @cindex @code{-t}
7960 @itemx --touch
7961 @cindex @code{--touch}
7962 @c Extra blank line here makes the table look better.
7964 Touch files (mark them up to date without really changing them)
7965 instead of running their commands.  This is used to pretend that the
7966 commands were done, in order to fool future invocations of
7967 @code{make}.  @xref{Instead of Execution, ,Instead of Executing the Commands}.
7969 @item -v
7970 @cindex @code{-v}
7971 @itemx --version
7972 @cindex @code{--version}
7973 Print the version of the @code{make} program plus a copyright, a list
7974 of authors, and a notice that there is no warranty; then exit.
7976 @item -w
7977 @cindex @code{-w}
7978 @itemx --print-directory
7979 @cindex @code{--print-directory}
7980 Print a message containing the working directory both before and after
7981 executing the makefile.  This may be useful for tracking down errors
7982 from complicated nests of recursive @code{make} commands.
7983 @xref{Recursion, ,Recursive Use of @code{make}}.  (In practice, you
7984 rarely need to specify this option since @samp{make} does it for you;
7985 see @ref{-w Option, ,The @samp{--print-directory} Option}.)
7987 @itemx --no-print-directory
7988 @cindex @code{--no-print-directory}
7989 Disable printing of the working directory under @code{-w}.
7990 This option is useful when @code{-w} is turned on automatically,
7991 but you do not want to see the extra messages.
7992 @xref{-w Option, ,The @samp{--print-directory} Option}.
7994 @item -W @var{file}
7995 @cindex @code{-W}
7996 @itemx --what-if=@var{file}
7997 @cindex @code{--what-if}
7998 @itemx --new-file=@var{file}
7999 @cindex @code{--new-file}
8000 @itemx --assume-new=@var{file}
8001 @cindex @code{--assume-new}
8002 Pretend that the target @var{file} has just been modified.  When used
8003 with the @samp{-n} flag, this shows you what would happen if you were
8004 to modify that file.  Without @samp{-n}, it is almost the same as
8005 running a @code{touch} command on the given file before running
8006 @code{make}, except that the modification time is changed only in the
8007 imagination of @code{make}.
8008 @xref{Instead of Execution, ,Instead of Executing the Commands}.
8010 @item --warn-undefined-variables
8011 @cindex @code{--warn-undefined-variables}
8012 @cindex variables, warning for undefined
8013 @cindex undefined variables, warning message
8014 Issue a warning message whenever @code{make} sees a reference to an
8015 undefined variable.  This can be helpful when you are trying to debug
8016 makefiles which use variables in complex ways.
8017 @end table
8019 @node Implicit Rules, Archives, Running, Top
8020 @chapter Using Implicit Rules
8021 @cindex implicit rule
8022 @cindex rule, implicit
8024 Certain standard ways of remaking target files are used very often.  For
8025 example, one customary way to make an object file is from a C source file
8026 using the C compiler, @code{cc}.
8028 @dfn{Implicit rules} tell @code{make} how to use customary techniques so
8029 that you do not have to specify them in detail when you want to use
8030 them.  For example, there is an implicit rule for C compilation.  File
8031 names determine which implicit rules are run.  For example, C
8032 compilation typically takes a @file{.c} file and makes a @file{.o} file.
8033 So @code{make} applies the implicit rule for C compilation when it sees
8034 this combination of file name endings.@refill
8036 A chain of implicit rules can apply in sequence; for example, @code{make}
8037 will remake a @file{.o} file from a @file{.y} file by way of a @file{.c} file.
8038 @iftex
8039 @xref{Chained Rules, ,Chains of Implicit Rules}.
8040 @end iftex
8042 The built-in implicit rules use several variables in their commands so
8043 that, by changing the values of the variables, you can change the way the
8044 implicit rule works.  For example, the variable @code{CFLAGS} controls the
8045 flags given to the C compiler by the implicit rule for C compilation.
8046 @iftex
8047 @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
8048 @end iftex
8050 You can define your own implicit rules by writing @dfn{pattern rules}.
8051 @iftex
8052 @xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.
8053 @end iftex
8055 @dfn{Suffix rules} are a more limited way to define implicit rules.
8056 Pattern rules are more general and clearer, but suffix rules are
8057 retained for compatibility.
8058 @iftex
8059 @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
8060 @end iftex
8062 @menu
8063 * Using Implicit::              How to use an existing implicit rule
8064                                   to get the commands for updating a file.
8065 * Catalogue of Rules::          A list of built-in implicit rules.
8066 * Implicit Variables::          How to change what predefined rules do.
8067 * Chained Rules::               How to use a chain of implicit rules.
8068 * Pattern Rules::               How to define new implicit rules.
8069 * Last Resort::                 How to define commands for rules which
8070                                 cannot find any.
8071 * Suffix Rules::                The old-fashioned style of implicit rule.
8072 * Implicit Rule Search::        The precise algorithm for applying
8073                                   implicit rules.
8074 @end menu
8076 @node Using Implicit, Catalogue of Rules, Implicit Rules, Implicit Rules
8077 @section Using Implicit Rules
8078 @cindex implicit rule, how to use
8079 @cindex rule, implicit, how to use
8081 To allow @code{make} to find a customary method for updating a target file,
8082 all you have to do is refrain from specifying commands yourself.  Either
8083 write a rule with no command lines, or don't write a rule at all.  Then
8084 @code{make} will figure out which implicit rule to use based on which
8085 kind of source file exists or can be made.
8087 For example, suppose the makefile looks like this:
8089 @example
8090 foo : foo.o bar.o
8091         cc -o foo foo.o bar.o $(CFLAGS) $(LDFLAGS)
8092 @end example
8094 @noindent
8095 Because you mention @file{foo.o} but do not give a rule for it, @code{make}
8096 will automatically look for an implicit rule that tells how to update it.
8097 This happens whether or not the file @file{foo.o} currently exists.
8099 If an implicit rule is found, it can supply both commands and one or
8100 more prerequisites (the source files).  You would want to write a rule
8101 for @file{foo.o} with no command lines if you need to specify additional
8102 prerequisites, such as header files, that the implicit rule cannot
8103 supply.
8105 Each implicit rule has a target pattern and prerequisite patterns.  There may
8106 be many implicit rules with the same target pattern.  For example, numerous
8107 rules make @samp{.o} files: one, from a @samp{.c} file with the C compiler;
8108 another, from a @samp{.p} file with the Pascal compiler; and so on.  The rule
8109 that actually applies is the one whose prerequisites exist or can be made.
8110 So, if you have a file @file{foo.c}, @code{make} will run the C compiler;
8111 otherwise, if you have a file @file{foo.p}, @code{make} will run the Pascal
8112 compiler; and so on.
8114 Of course, when you write the makefile, you know which implicit rule you
8115 want @code{make} to use, and you know it will choose that one because you
8116 know which possible prerequisite files are supposed to exist.
8117 @xref{Catalogue of Rules, ,Catalogue of Implicit Rules},
8118 for a catalogue of all the predefined implicit rules.
8120 Above, we said an implicit rule applies if the required prerequisites ``exist
8121 or can be made''.  A file ``can be made'' if it is mentioned explicitly in
8122 the makefile as a target or a prerequisite, or if an implicit rule can be
8123 recursively found for how to make it.  When an implicit prerequisite is the
8124 result of another implicit rule, we say that @dfn{chaining} is occurring.
8125 @xref{Chained Rules, ,Chains of Implicit Rules}.
8127 In general, @code{make} searches for an implicit rule for each target, and
8128 for each double-colon rule, that has no commands.  A file that is mentioned
8129 only as a prerequisite is considered a target whose rule specifies nothing,
8130 so implicit rule search happens for it.  @xref{Implicit Rule Search, ,Implicit Rule Search Algorithm}, for the
8131 details of how the search is done.
8133 Note that explicit prerequisites do not influence implicit rule search.
8134 For example, consider this explicit rule:
8136 @example
8137 foo.o: foo.p
8138 @end example
8140 @noindent
8141 The prerequisite on @file{foo.p} does not necessarily mean that
8142 @code{make} will remake @file{foo.o} according to the implicit rule to
8143 make an object file, a @file{.o} file, from a Pascal source file, a
8144 @file{.p} file.  For example, if @file{foo.c} also exists, the implicit
8145 rule to make an object file from a C source file is used instead,
8146 because it appears before the Pascal rule in the list of predefined
8147 implicit rules (@pxref{Catalogue of Rules, , Catalogue of Implicit
8148 Rules}).
8150 If you do not want an implicit rule to be used for a target that has no
8151 commands, you can give that target empty commands by writing a semicolon
8152 (@pxref{Empty Commands, ,Defining Empty Commands}).
8154 @node Catalogue of Rules, Implicit Variables, Using Implicit, Implicit Rules
8155 @section Catalogue of Implicit Rules
8156 @cindex implicit rule, predefined
8157 @cindex rule, implicit, predefined
8159 Here is a catalogue of predefined implicit rules which are always
8160 available unless the makefile explicitly overrides or cancels them.
8161 @xref{Canceling Rules, ,Canceling Implicit Rules}, for information on
8162 canceling or overriding an implicit rule.  The @samp{-r} or
8163 @samp{--no-builtin-rules} option cancels all predefined rules.
8165 This manual only documents the default rules available on POSIX-based
8166 operating systems.  Other operating systems, such as VMS, Windows,
8167 OS/2, etc. may have different sets of default rules.  To see the full
8168 list of default rules and variables available in your version of GNU
8169 @code{make}, run @samp{make -p} in a directory with no makefile.
8171 Not all of these rules will always be defined, even when the @samp{-r}
8172 option is not given.  Many of the predefined implicit rules are
8173 implemented in @code{make} as suffix rules, so which ones will be
8174 defined depends on the @dfn{suffix list} (the list of prerequisites of
8175 the special target @code{.SUFFIXES}).  The default suffix list is:
8176 @code{.out}, @code{.a}, @code{.ln}, @code{.o}, @code{.c}, @code{.cc},
8177 @code{.C}, @code{.cpp}, @code{.p}, @code{.f}, @code{.F}, @code{.r}, @code{.y},
8178 @code{.l}, @code{.s}, @code{.S}, @code{.mod}, @code{.sym}, @code{.def},
8179 @code{.h}, @code{.info}, @code{.dvi}, @code{.tex}, @code{.texinfo},
8180 @code{.texi}, @code{.txinfo}, @code{.w}, @code{.ch} @code{.web},
8181 @code{.sh}, @code{.elc}, @code{.el}.  All of the implicit rules
8182 described below whose prerequisites have one of these suffixes are
8183 actually suffix rules.  If you modify the suffix list, the only
8184 predefined suffix rules in effect will be those named by one or two of
8185 the suffixes that are on the list you specify; rules whose suffixes fail
8186 to be on the list are disabled.  @xref{Suffix Rules, ,Old-Fashioned
8187 Suffix Rules}, for full details on suffix rules.
8189 @table @asis
8190 @item Compiling C programs
8191 @cindex C, rule to compile
8192 @pindex cc
8193 @pindex gcc
8194 @pindex .o
8195 @pindex .c
8196 @file{@var{n}.o} is made automatically from @file{@var{n}.c} with
8197 a command of the form @samp{$(CC) -c $(CPPFLAGS) $(CFLAGS)}.@refill
8199 @item Compiling C++ programs
8200 @cindex C++, rule to compile
8201 @pindex g++
8202 @pindex .cc
8203 @pindex .cpp
8204 @pindex .C
8205 @file{@var{n}.o} is made automatically from @file{@var{n}.cc},
8206 @file{@var{n}.cpp}, or @file{@var{n}.C} with a command of the form
8207 @samp{$(CXX) -c $(CPPFLAGS) $(CXXFLAGS)}.  We encourage you to use the
8208 suffix @samp{.cc} for C++ source files instead of @samp{.C}.@refill
8210 @item Compiling Pascal programs
8211 @cindex Pascal, rule to compile
8212 @pindex pc
8213 @pindex .p
8214 @file{@var{n}.o} is made automatically from @file{@var{n}.p}
8215 with the command @samp{$(PC) -c $(PFLAGS)}.@refill
8217 @item Compiling Fortran and Ratfor programs
8218 @cindex Fortran, rule to compile
8219 @cindex Ratfor, rule to compile
8220 @pindex f77
8221 @pindex .f
8222 @pindex .r
8223 @pindex .F
8224 @file{@var{n}.o} is made automatically from @file{@var{n}.r},
8225 @file{@var{n}.F} or @file{@var{n}.f} by running the
8226 Fortran compiler.  The precise command used is as follows:@refill
8228 @table @samp
8229 @item .f
8230 @samp{$(FC) -c $(FFLAGS)}.
8231 @item .F
8232 @samp{$(FC) -c $(FFLAGS) $(CPPFLAGS)}.
8233 @item .r
8234 @samp{$(FC) -c $(FFLAGS) $(RFLAGS)}.
8235 @end table
8237 @item Preprocessing Fortran and Ratfor programs
8238 @file{@var{n}.f} is made automatically from @file{@var{n}.r} or
8239 @file{@var{n}.F}.  This rule runs just the preprocessor to convert a
8240 Ratfor or preprocessable Fortran program into a strict Fortran
8241 program.  The precise command used is as follows:@refill
8243 @table @samp
8244 @item .F
8245 @samp{$(FC) -F $(CPPFLAGS) $(FFLAGS)}.
8246 @item .r
8247 @samp{$(FC) -F $(FFLAGS) $(RFLAGS)}.
8248 @end table
8250 @item Compiling Modula-2 programs
8251 @cindex Modula-2, rule to compile
8252 @pindex m2c
8253 @pindex .sym
8254 @pindex .def
8255 @pindex .mod
8256 @file{@var{n}.sym} is made from @file{@var{n}.def} with a command
8257 of the form @samp{$(M2C) $(M2FLAGS) $(DEFFLAGS)}.  @file{@var{n}.o}
8258 is made from @file{@var{n}.mod}; the form is:
8259 @w{@samp{$(M2C) $(M2FLAGS) $(MODFLAGS)}}.@refill
8261 @need 1200
8262 @item Assembling and preprocessing assembler programs
8263 @cindex assembly, rule to compile
8264 @pindex as
8265 @pindex .s
8266 @file{@var{n}.o} is made automatically from @file{@var{n}.s} by
8267 running the assembler, @code{as}.  The precise command is
8268 @samp{$(AS) $(ASFLAGS)}.@refill
8270 @pindex .S
8271 @file{@var{n}.s} is made automatically from @file{@var{n}.S} by
8272 running the C preprocessor, @code{cpp}.  The precise command is
8273 @w{@samp{$(CPP) $(CPPFLAGS)}}.
8275 @item Linking a single object file
8276 @cindex linking, predefined rule for
8277 @pindex ld
8278 @pindex .o
8279 @file{@var{n}} is made automatically from @file{@var{n}.o} by running
8280 the linker (usually called @code{ld}) via the C compiler.  The precise
8281 command used is @w{@samp{$(CC) $(LDFLAGS) @var{n}.o $(LOADLIBES) $(LDLIBS)}}.
8283 This rule does the right thing for a simple program with only one
8284 source file.  It will also do the right thing if there are multiple
8285 object files (presumably coming from various other source files), one
8286 of which has a name matching that of the executable file.  Thus,
8288 @example
8289 x: y.o z.o
8290 @end example
8292 @noindent
8293 when @file{x.c}, @file{y.c} and @file{z.c} all exist will execute:
8295 @example
8296 @group
8297 cc -c x.c -o x.o
8298 cc -c y.c -o y.o
8299 cc -c z.c -o z.o
8300 cc x.o y.o z.o -o x
8301 rm -f x.o
8302 rm -f y.o
8303 rm -f z.o
8304 @end group
8305 @end example
8307 @noindent
8308 In more complicated cases, such as when there is no object file whose
8309 name derives from the executable file name, you must write an explicit
8310 command for linking.
8312 Each kind of file automatically made into @samp{.o} object files will
8313 be automatically linked by using the compiler (@samp{$(CC)},
8314 @samp{$(FC)} or @samp{$(PC)}; the C compiler @samp{$(CC)} is used to
8315 assemble @samp{.s} files) without the @samp{-c} option.  This could be
8316 done by using the @samp{.o} object files as intermediates, but it is
8317 faster to do the compiling and linking in one step, so that's how it's
8318 done.@refill
8320 @item Yacc for C programs
8321 @pindex yacc
8322 @cindex Yacc, rule to run
8323 @pindex .y
8324 @file{@var{n}.c} is made automatically from @file{@var{n}.y} by
8325 running Yacc with the command @samp{$(YACC) $(YFLAGS)}.
8327 @item Lex for C programs
8328 @pindex lex
8329 @cindex Lex, rule to run
8330 @pindex .l
8331 @file{@var{n}.c} is made automatically from @file{@var{n}.l} by
8332 running Lex.  The actual command is @samp{$(LEX) $(LFLAGS)}.
8334 @item Lex for Ratfor programs
8335 @file{@var{n}.r} is made automatically from @file{@var{n}.l} by
8336 running Lex.  The actual command is @samp{$(LEX) $(LFLAGS)}.
8338 The convention of using the same suffix @samp{.l} for all Lex files
8339 regardless of whether they produce C code or Ratfor code makes it
8340 impossible for @code{make} to determine automatically which of the two
8341 languages you are using in any particular case.  If @code{make} is
8342 called upon to remake an object file from a @samp{.l} file, it must
8343 guess which compiler to use.  It will guess the C compiler, because
8344 that is more common.  If you are using Ratfor, make sure @code{make}
8345 knows this by mentioning @file{@var{n}.r} in the makefile.  Or, if you
8346 are using Ratfor exclusively, with no C files, remove @samp{.c} from
8347 the list of implicit rule suffixes with:@refill
8349 @example
8350 @group
8351 .SUFFIXES:
8352 .SUFFIXES: .o .r .f .l @dots{}
8353 @end group
8354 @end example
8356 @item Making Lint Libraries from C, Yacc, or Lex programs
8357 @pindex lint
8358 @cindex @code{lint}, rule to run
8359 @pindex .ln
8360 @file{@var{n}.ln} is made from @file{@var{n}.c} by running @code{lint}.
8361 The precise command is @w{@samp{$(LINT) $(LINTFLAGS) $(CPPFLAGS) -i}}.
8362 The same command is used on the C code produced from
8363 @file{@var{n}.y} or @file{@var{n}.l}.@refill
8365 @item @TeX{} and Web
8366 @cindex @TeX{}, rule to run
8367 @cindex Web, rule to run
8368 @pindex tex
8369 @pindex cweave
8370 @pindex weave
8371 @pindex tangle
8372 @pindex ctangle
8373 @pindex .dvi
8374 @pindex .tex
8375 @pindex .web
8376 @pindex .w
8377 @pindex .ch
8378 @file{@var{n}.dvi} is made from @file{@var{n}.tex} with the command
8379 @samp{$(TEX)}.  @file{@var{n}.tex} is made from @file{@var{n}.web} with
8380 @samp{$(WEAVE)}, or from @file{@var{n}.w} (and from @file{@var{n}.ch} if
8381 it exists or can be made) with @samp{$(CWEAVE)}.  @file{@var{n}.p} is
8382 made from @file{@var{n}.web} with @samp{$(TANGLE)} and @file{@var{n}.c}
8383 is made from @file{@var{n}.w} (and from @file{@var{n}.ch} if it exists
8384 or can be made) with @samp{$(CTANGLE)}.@refill
8386 @item Texinfo and Info
8387 @cindex Texinfo, rule to format
8388 @cindex Info, rule to format
8389 @pindex texi2dvi
8390 @pindex makeinfo
8391 @pindex .texinfo
8392 @pindex .info
8393 @pindex .texi
8394 @pindex .txinfo
8395 @file{@var{n}.dvi} is made from @file{@var{n}.texinfo},
8396 @file{@var{n}.texi}, or @file{@var{n}.txinfo}, with the command
8397 @w{@samp{$(TEXI2DVI) $(TEXI2DVI_FLAGS)}}.  @file{@var{n}.info} is made from
8398 @file{@var{n}.texinfo}, @file{@var{n}.texi}, or @file{@var{n}.txinfo}, with
8399 the command @w{@samp{$(MAKEINFO) $(MAKEINFO_FLAGS)}}.
8401 @item RCS
8402 @cindex RCS, rule to extract from
8403 @pindex co
8404 @pindex ,v @r{(RCS file extension)}
8405 Any file @file{@var{n}} is extracted if necessary from an RCS file
8406 named either @file{@var{n},v} or @file{RCS/@var{n},v}.  The precise
8407 command used is @w{@samp{$(CO) $(COFLAGS)}}.  @file{@var{n}} will not be
8408 extracted from RCS if it already exists, even if the RCS file is
8409 newer.  The rules for RCS are terminal
8410 (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}),
8411 so RCS files cannot be generated from another source; they must
8412 actually exist.@refill
8414 @item SCCS
8415 @cindex SCCS, rule to extract from
8416 @pindex get
8417 @pindex s. @r{(SCCS file prefix)}
8418 Any file @file{@var{n}} is extracted if necessary from an SCCS file
8419 named either @file{s.@var{n}} or @file{SCCS/s.@var{n}}.  The precise
8420 command used is @w{@samp{$(GET) $(GFLAGS)}}.  The rules for SCCS are
8421 terminal (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}),
8422 so SCCS files cannot be generated from another source; they must
8423 actually exist.@refill
8425 @pindex .sh
8426 For the benefit of SCCS, a file @file{@var{n}} is copied from
8427 @file{@var{n}.sh} and made executable (by everyone).  This is for
8428 shell scripts that are checked into SCCS.  Since RCS preserves the
8429 execution permission of a file, you do not need to use this feature
8430 with RCS.@refill
8432 We recommend that you avoid using of SCCS.  RCS is widely held to be
8433 superior, and is also free.  By choosing free software in place of
8434 comparable (or inferior) proprietary software, you support the free
8435 software movement.
8436 @end table
8438 Usually, you want to change only the variables listed in the table
8439 above, which are documented in the following section.
8441 However, the commands in built-in implicit rules actually use
8442 variables such as @code{COMPILE.c}, @code{LINK.p}, and
8443 @code{PREPROCESS.S}, whose values contain the commands listed above.
8445 @code{make} follows the convention that the rule to compile a
8446 @file{.@var{x}} source file uses the variable @code{COMPILE.@var{x}}.
8447 Similarly, the rule to produce an executable from a @file{.@var{x}}
8448 file uses @code{LINK.@var{x}}; and the rule to preprocess a
8449 @file{.@var{x}} file uses @code{PREPROCESS.@var{x}}.
8451 @vindex OUTPUT_OPTION
8452 Every rule that produces an object file uses the variable
8453 @code{OUTPUT_OPTION}.  @code{make} defines this variable either to
8454 contain @samp{-o $@@}, or to be empty, depending on a compile-time
8455 option.  You need the @samp{-o} option to ensure that the output goes
8456 into the right file when the source file is in a different directory,
8457 as when using @code{VPATH} (@pxref{Directory Search}).  However,
8458 compilers on some systems do not accept a @samp{-o} switch for object
8459 files.  If you use such a system, and use @code{VPATH}, some
8460 compilations will put their output in the wrong place.
8461 A possible workaround for this problem is to give @code{OUTPUT_OPTION}
8462 the value @w{@samp{; mv $*.o $@@}}.
8464 @node Implicit Variables, Chained Rules, Catalogue of Rules, Implicit Rules
8465 @section Variables Used by Implicit Rules
8466 @cindex flags for compilers
8468 The commands in built-in implicit rules make liberal use of certain
8469 predefined variables.  You can alter the values of these variables in
8470 the makefile, with arguments to @code{make}, or in the environment to
8471 alter how the implicit rules work without redefining the rules
8472 themselves.  You can cancel all variables used by implicit rules with
8473 the @samp{-R} or @samp{--no-builtin-variables} option.
8475 For example, the command used to compile a C source file actually says
8476 @samp{$(CC) -c $(CFLAGS) $(CPPFLAGS)}.  The default values of the variables
8477 used are @samp{cc} and nothing, resulting in the command @samp{cc -c}.  By
8478 redefining @samp{CC} to @samp{ncc}, you could cause @samp{ncc} to be
8479 used for all C compilations performed by the implicit rule.  By redefining
8480 @samp{CFLAGS} to be @samp{-g}, you could pass the @samp{-g} option to
8481 each compilation.  @emph{All} implicit rules that do C compilation use
8482 @samp{$(CC)} to get the program name for the compiler and @emph{all}
8483 include @samp{$(CFLAGS)} among the arguments given to the compiler.@refill
8485 The variables used in implicit rules fall into two classes: those that are
8486 names of programs (like @code{CC}) and those that contain arguments for the
8487 programs (like @code{CFLAGS}).  (The ``name of a program'' may also contain
8488 some command arguments, but it must start with an actual executable program
8489 name.)  If a variable value contains more than one argument, separate them
8490 with spaces.
8492 The following tables describe of some of the more commonly-used predefined
8493 variables.  This list is not exhaustive, and the default values shown here may
8494 not be what is selected by @code{make} for your environment.  To see the
8495 complete list of predefined variables for your instance of GNU @code{make} you
8496 can run @samp{make -p} in a directory with no makefiles.
8498 Here is a table of some of the more common variables used as names of
8499 programs in built-in rules:
8500 makefiles.
8502 @table @code
8503 @item AR
8504 @vindex AR
8505 Archive-maintaining program; default @samp{ar}.
8506 @pindex ar
8508 @item AS
8509 @vindex AS
8510 Program for compiling assembly files; default @samp{as}.
8511 @pindex as
8513 @item CC
8514 @vindex CC
8515 Program for compiling C programs; default @samp{cc}.
8516 @pindex cc
8518 @item CO
8519 @vindex CO
8520 Program for checking out files from RCS; default @samp{co}.
8521 @pindex cc
8523 @item CXX
8524 @vindex CXX
8525 Program for compiling C++ programs; default @samp{g++}.
8526 @pindex g++
8528 @item CO
8529 @vindex CO
8530 Program for extracting a file from RCS; default @samp{co}.
8531 @pindex co
8533 @item CPP
8534 @vindex CPP
8535 Program for running the C preprocessor, with results to standard output;
8536 default @samp{$(CC) -E}.
8538 @item FC
8539 @vindex FC
8540 Program for compiling or preprocessing Fortran and Ratfor programs;
8541 default @samp{f77}.
8542 @pindex f77
8544 @item GET
8545 @vindex GET
8546 Program for extracting a file from SCCS; default @samp{get}.
8547 @pindex get
8549 @item LEX
8550 @vindex LEX
8551 Program to use to turn Lex grammars into source code; default @samp{lex}.
8552 @pindex lex
8554 @item YACC
8555 @vindex YACC
8556 Program to use to turn Yacc grammars into source code; default @samp{yacc}.
8557 @pindex yacc
8559 @item LINT
8560 @vindex LINT
8561 Program to use to run lint on source code; default @samp{lint}.
8562 @pindex lint
8564 @item M2C
8565 @vindex M2C
8566 Program to use to compile Modula-2 source code; default @samp{m2c}.
8567 @pindex m2c
8569 @item PC
8570 @vindex PC
8571 Program for compiling Pascal programs; default @samp{pc}.
8572 @pindex pc
8574 @item MAKEINFO
8575 @vindex MAKEINFO
8576 Program to convert a Texinfo source file into an Info file; default
8577 @samp{makeinfo}.
8578 @pindex makeinfo
8580 @item TEX
8581 @vindex TEX
8582 Program to make @TeX{} @sc{dvi} files from @TeX{} source;
8583 default @samp{tex}.
8584 @pindex tex
8586 @item TEXI2DVI
8587 @vindex TEXI2DVI
8588 Program to make @TeX{} @sc{dvi} files from Texinfo source;
8589 default @samp{texi2dvi}.
8590 @pindex texi2dvi
8592 @item WEAVE
8593 @vindex WEAVE
8594 Program to translate Web into @TeX{}; default @samp{weave}.
8595 @pindex weave
8597 @item CWEAVE
8598 @vindex CWEAVE
8599 Program to translate C Web into @TeX{}; default @samp{cweave}.
8600 @pindex cweave
8602 @item TANGLE
8603 @vindex TANGLE
8604 Program to translate Web into Pascal; default @samp{tangle}.
8605 @pindex tangle
8607 @item CTANGLE
8608 @vindex CTANGLE
8609 Program to translate C Web into C; default @samp{ctangle}.
8610 @pindex ctangle
8612 @item RM
8613 @vindex RM
8614 Command to remove a file; default @samp{rm -f}.
8615 @pindex rm
8616 @end table
8618 Here is a table of variables whose values are additional arguments for the
8619 programs above.  The default values for all of these is the empty
8620 string, unless otherwise noted.
8622 @table @code
8623 @item ARFLAGS
8624 @vindex ARFLAGS
8625 Flags to give the archive-maintaining program; default @samp{rv}.
8627 @item ASFLAGS
8628 @vindex ASFLAGS
8629 Extra flags to give to the assembler (when explicitly
8630 invoked on a @samp{.s} or @samp{.S} file).
8632 @item CFLAGS
8633 @vindex CFLAGS
8634 Extra flags to give to the C compiler.
8636 @item CXXFLAGS
8637 @vindex CXXFLAGS
8638 Extra flags to give to the C++ compiler.
8640 @item COFLAGS
8641 @vindex COFLAGS
8642 Extra flags to give to the RCS @code{co} program.
8644 @item CPPFLAGS
8645 @vindex CPPFLAGS
8646 Extra flags to give to the C preprocessor and programs
8647 that use it (the C and Fortran compilers).
8649 @item FFLAGS
8650 @vindex FFLAGS
8651 Extra flags to give to the Fortran compiler.
8653 @item GFLAGS
8654 @vindex GFLAGS
8655 Extra flags to give to the SCCS @code{get} program.
8657 @item LDFLAGS
8658 @vindex LDFLAGS
8659 Extra flags to give to compilers when they are supposed to invoke the linker,
8660 @samp{ld}.
8662 @item LFLAGS
8663 @vindex LFLAGS
8664 Extra flags to give to Lex.
8666 @item YFLAGS
8667 @vindex YFLAGS
8668 Extra flags to give to Yacc.
8670 @item PFLAGS
8671 @vindex PFLAGS
8672 Extra flags to give to the Pascal compiler.
8674 @item RFLAGS
8675 @vindex RFLAGS
8676 Extra flags to give to the Fortran compiler for Ratfor programs.
8678 @item LINTFLAGS
8679 @vindex LINTFLAGS
8680 Extra flags to give to lint.
8681 @end table
8683 @node Chained Rules, Pattern Rules, Implicit Variables, Implicit Rules
8684 @section Chains of Implicit Rules
8686 @cindex chains of rules
8687 @cindex rule, implicit, chains of
8688 Sometimes a file can be made by a sequence of implicit rules.  For example,
8689 a file @file{@var{n}.o} could be made from @file{@var{n}.y} by running
8690 first Yacc and then @code{cc}.  Such a sequence is called a @dfn{chain}.
8692 If the file @file{@var{n}.c} exists, or is mentioned in the makefile, no
8693 special searching is required: @code{make} finds that the object file can
8694 be made by C compilation from @file{@var{n}.c}; later on, when considering
8695 how to make @file{@var{n}.c}, the rule for running Yacc is
8696 used.  Ultimately both @file{@var{n}.c} and @file{@var{n}.o} are
8697 updated.@refill
8699 @cindex intermediate files
8700 @cindex files, intermediate
8701 However, even if @file{@var{n}.c} does not exist and is not mentioned,
8702 @code{make} knows how to envision it as the missing link between
8703 @file{@var{n}.o} and @file{@var{n}.y}!  In this case, @file{@var{n}.c} is
8704 called an @dfn{intermediate file}.  Once @code{make} has decided to use the
8705 intermediate file, it is entered in the data base as if it had been
8706 mentioned in the makefile, along with the implicit rule that says how to
8707 create it.@refill
8709 Intermediate files are remade using their rules just like all other
8710 files.  But intermediate files are treated differently in two ways.
8712 The first difference is what happens if the intermediate file does not
8713 exist.  If an ordinary file @var{b} does not exist, and @code{make}
8714 considers a target that depends on @var{b}, it invariably creates
8715 @var{b} and then updates the target from @var{b}.  But if @var{b} is an
8716 intermediate file, then @code{make} can leave well enough alone.  It
8717 won't bother updating @var{b}, or the ultimate target, unless some
8718 prerequisite of @var{b} is newer than that target or there is some other
8719 reason to update that target.
8721 The second difference is that if @code{make} @emph{does} create @var{b}
8722 in order to update something else, it deletes @var{b} later on after it
8723 is no longer needed.  Therefore, an intermediate file which did not
8724 exist before @code{make} also does not exist after @code{make}.
8725 @code{make} reports the deletion to you by printing a @samp{rm -f}
8726 command showing which file it is deleting.
8728 Ordinarily, a file cannot be intermediate if it is mentioned in the
8729 makefile as a target or prerequisite.  However, you can explicitly mark a
8730 file as intermediate by listing it as a prerequisite of the special target
8731 @code{.INTERMEDIATE}.  This takes effect even if the file is mentioned
8732 explicitly in some other way.
8734 @cindex intermediate files, preserving
8735 @cindex preserving intermediate files
8736 @cindex secondary files
8737 You can prevent automatic deletion of an intermediate file by marking it
8738 as a @dfn{secondary} file.  To do this, list it as a prerequisite of the
8739 special target @code{.SECONDARY}.  When a file is secondary, @code{make}
8740 will not create the file merely because it does not already exist, but
8741 @code{make} does not automatically delete the file.  Marking a file as
8742 secondary also marks it as intermediate.
8744 You can list the target pattern of an implicit rule (such as @samp{%.o})
8745 as a prerequisite of the special target @code{.PRECIOUS} to preserve
8746 intermediate files made by implicit rules whose target patterns match
8747 that file's name; see @ref{Interrupts}.@refill
8748 @cindex preserving with @code{.PRECIOUS}
8749 @cindex @code{.PRECIOUS} intermediate files
8751 A chain can involve more than two implicit rules.  For example, it is
8752 possible to make a file @file{foo} from @file{RCS/foo.y,v} by running RCS,
8753 Yacc and @code{cc}.  Then both @file{foo.y} and @file{foo.c} are
8754 intermediate files that are deleted at the end.@refill
8756 No single implicit rule can appear more than once in a chain.  This means
8757 that @code{make} will not even consider such a ridiculous thing as making
8758 @file{foo} from @file{foo.o.o} by running the linker twice.  This
8759 constraint has the added benefit of preventing any infinite loop in the
8760 search for an implicit rule chain.
8762 There are some special implicit rules to optimize certain cases that would
8763 otherwise be handled by rule chains.  For example, making @file{foo} from
8764 @file{foo.c} could be handled by compiling and linking with separate
8765 chained rules, using @file{foo.o} as an intermediate file.  But what
8766 actually happens is that a special rule for this case does the compilation
8767 and linking with a single @code{cc} command.  The optimized rule is used in
8768 preference to the step-by-step chain because it comes earlier in the
8769 ordering of rules.
8771 @node Pattern Rules, Last Resort, Chained Rules, Implicit Rules
8772 @section Defining and Redefining Pattern Rules
8774 You define an implicit rule by writing a @dfn{pattern rule}.  A pattern
8775 rule looks like an ordinary rule, except that its target contains the
8776 character @samp{%} (exactly one of them).  The target is considered a
8777 pattern for matching file names; the @samp{%} can match any nonempty
8778 substring, while other characters match only themselves.  The prerequisites
8779 likewise use @samp{%} to show how their names relate to the target name.
8781 Thus, a pattern rule @samp{%.o : %.c} says how to make any file
8782 @file{@var{stem}.o} from another file @file{@var{stem}.c}.@refill
8784 Note that expansion using @samp{%} in pattern rules occurs
8785 @strong{after} any variable or function expansions, which take place
8786 when the makefile is read.  @xref{Using Variables, , How to Use
8787 Variables}, and @ref{Functions, ,Functions for Transforming Text}.
8789 @menu
8790 * Pattern Intro::               An introduction to pattern rules.
8791 * Pattern Examples::            Examples of pattern rules.
8792 * Automatic Variables::         How to use automatic variables in the
8793                                   commands of implicit rules.
8794 * Pattern Match::               How patterns match.
8795 * Match-Anything Rules::        Precautions you should take prior to
8796                                   defining rules that can match any
8797                                   target file whatever.
8798 * Canceling Rules::             How to override or cancel built-in rules.
8799 @end menu
8801 @node Pattern Intro, Pattern Examples, Pattern Rules, Pattern Rules
8802 @subsection Introduction to Pattern Rules
8803 @cindex pattern rule
8804 @cindex rule, pattern
8806 A pattern rule contains the character @samp{%} (exactly one of them)
8807 in the target; otherwise, it looks exactly like an ordinary rule.  The
8808 target is a pattern for matching file names; the @samp{%} matches any
8809 nonempty substring, while other characters match only themselves.
8810 @cindex target pattern, implicit
8811 @cindex @code{%}, in pattern rules
8813 For example, @samp{%.c} as a pattern matches any file name that ends in
8814 @samp{.c}.  @samp{s.%.c} as a pattern matches any file name that starts
8815 with @samp{s.}, ends in @samp{.c} and is at least five characters long.
8816 (There must be at least one character to match the @samp{%}.)  The substring
8817 that the @samp{%} matches is called the @dfn{stem}.@refill
8819 @samp{%} in a prerequisite of a pattern rule stands for the same stem
8820 that was matched by the @samp{%} in the target.  In order for the
8821 pattern rule to apply, its target pattern must match the file name
8822 under consideration and all of its prerequisites (after pattern
8823 substitution) must name files that exist or can be made.  These files
8824 become prerequisites of the target.
8825 @cindex prerequisite pattern, implicit
8827 Thus, a rule of the form
8829 @example
8830 %.o : %.c ; @var{command}@dots{}
8831 @end example
8833 @noindent
8834 specifies how to make a file @file{@var{n}.o}, with another file
8835 @file{@var{n}.c} as its prerequisite, provided that @file{@var{n}.c}
8836 exists or can be made.
8838 There may also be prerequisites that do not use @samp{%}; such a prerequisite
8839 attaches to every file made by this pattern rule.  These unvarying
8840 prerequisites are useful occasionally.
8842 A pattern rule need not have any prerequisites that contain @samp{%}, or
8843 in fact any prerequisites at all.  Such a rule is effectively a general
8844 wildcard.  It provides a way to make any file that matches the target
8845 pattern.  @xref{Last Resort}.
8847 @c !!! The end of of this paragraph should be rewritten.  --bob
8848 Pattern rules may have more than one target.  Unlike normal rules, this
8849 does not act as many different rules with the same prerequisites and
8850 commands.  If a pattern rule has multiple targets, @code{make} knows that
8851 the rule's commands are responsible for making all of the targets.  The
8852 commands are executed only once to make all the targets.  When searching
8853 for a pattern rule to match a target, the target patterns of a rule other
8854 than the one that matches the target in need of a rule are incidental:
8855 @code{make} worries only about giving commands and prerequisites to the file
8856 presently in question.  However, when this file's commands are run, the
8857 other targets are marked as having been updated themselves.
8858 @cindex multiple targets, in pattern rule
8859 @cindex target, multiple in pattern rule
8861 The order in which pattern rules appear in the makefile is important
8862 since this is the order in which they are considered.
8863 Of equally applicable
8864 rules, only the first one found is used.  The rules you write take precedence
8865 over those that are built in.  Note however, that a rule whose
8866 prerequisites actually exist or are mentioned always takes priority over a
8867 rule with prerequisites that must be made by chaining other implicit rules.
8868 @cindex pattern rules, order of
8869 @cindex order of pattern rules
8871 @node Pattern Examples, Automatic Variables, Pattern Intro, Pattern Rules
8872 @subsection Pattern Rule Examples
8874 Here are some examples of pattern rules actually predefined in
8875 @code{make}.  First, the rule that compiles @samp{.c} files into @samp{.o}
8876 files:@refill
8878 @example
8879 %.o : %.c
8880         $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@@
8881 @end example
8883 @noindent
8884 defines a rule that can make any file @file{@var{x}.o} from
8885 @file{@var{x}.c}.  The command uses the automatic variables @samp{$@@} and
8886 @samp{$<} to substitute the names of the target file and the source file
8887 in each case where the rule applies (@pxref{Automatic Variables}).@refill
8889 Here is a second built-in rule:
8891 @example
8892 % :: RCS/%,v
8893         $(CO) $(COFLAGS) $<
8894 @end example
8896 @noindent
8897 defines a rule that can make any file @file{@var{x}} whatsoever from a
8898 corresponding file @file{@var{x},v} in the subdirectory @file{RCS}.  Since
8899 the target is @samp{%}, this rule will apply to any file whatever, provided
8900 the appropriate prerequisite file exists.  The double colon makes the rule
8901 @dfn{terminal}, which means that its prerequisite may not be an intermediate
8902 file (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}).@refill
8904 @need 500
8905 This pattern rule has two targets:
8907 @example
8908 @group
8909 %.tab.c %.tab.h: %.y
8910         bison -d $<
8911 @end group
8912 @end example
8914 @noindent
8915 @c The following paragraph is rewritten to avoid overfull hboxes
8916 This tells @code{make} that the command @samp{bison -d @var{x}.y} will
8917 make both @file{@var{x}.tab.c} and @file{@var{x}.tab.h}.  If the file
8918 @file{foo} depends on the files @file{parse.tab.o} and @file{scan.o}
8919 and the file @file{scan.o} depends on the file @file{parse.tab.h},
8920 when @file{parse.y} is changed, the command @samp{bison -d parse.y}
8921 will be executed only once, and the prerequisites of both
8922 @file{parse.tab.o} and @file{scan.o} will be satisfied.  (Presumably
8923 the file @file{parse.tab.o} will be recompiled from @file{parse.tab.c}
8924 and the file @file{scan.o} from @file{scan.c}, while @file{foo} is
8925 linked from @file{parse.tab.o}, @file{scan.o}, and its other
8926 prerequisites, and it will execute happily ever after.)@refill
8928 @node Automatic Variables, Pattern Match, Pattern Examples, Pattern Rules
8929 @subsection Automatic Variables
8930 @cindex automatic variables
8931 @cindex variables, automatic
8932 @cindex variables, and implicit rule
8934 Suppose you are writing a pattern rule to compile a @samp{.c} file into a
8935 @samp{.o} file: how do you write the @samp{cc} command so that it operates
8936 on the right source file name?  You cannot write the name in the command,
8937 because the name is different each time the implicit rule is applied.
8939 What you do is use a special feature of @code{make}, the @dfn{automatic
8940 variables}.  These variables have values computed afresh for each rule that
8941 is executed, based on the target and prerequisites of the rule.  In this
8942 example, you would use @samp{$@@} for the object file name and @samp{$<}
8943 for the source file name.
8945 @cindex automatic variables in prerequisites
8946 @cindex prerequisites, and automatic variables
8947 It's very important that you recognize the limited scope in which
8948 automatic variable values are available: they only have values within
8949 the command script.  In particular, you cannot use them anywhere
8950 within the target list of a rule; they have no value there and will
8951 expand to the empty string.  Also, they cannot be accessed directly
8952 within the prerequisite list of a rule.  A common mistake is
8953 attempting to use @code{$@@} within the prerequisites list; this will
8954 not work.  However, there is a special feature of GNU @code{make},
8955 secondary expansion (@pxref{Secondary Expansion}), which will allow
8956 automatic variable values to be used in prerequisite lists.
8958 Here is a table of automatic variables:
8960 @table @code
8961 @vindex $@@
8962 @vindex @@ @r{(automatic variable)}
8963 @item $@@
8964 The file name of the target of the rule.  If the target is an archive
8965 member, then @samp{$@@} is the name of the archive file.  In a pattern
8966 rule that has multiple targets (@pxref{Pattern Intro, ,Introduction to
8967 Pattern Rules}), @samp{$@@} is the name of whichever target caused the
8968 rule's commands to be run.
8970 @vindex $%
8971 @vindex % @r{(automatic variable)}
8972 @item $%
8973 The target member name, when the target is an archive member.
8974 @xref{Archives}.  For example, if the target is @file{foo.a(bar.o)} then
8975 @samp{$%} is @file{bar.o} and @samp{$@@} is @file{foo.a}.  @samp{$%} is
8976 empty when the target is not an archive member.
8978 @vindex $<
8979 @vindex < @r{(automatic variable)}
8980 @item $<
8981 The name of the first prerequisite.  If the target got its commands from
8982 an implicit rule, this will be the first prerequisite added by the
8983 implicit rule (@pxref{Implicit Rules}).
8985 @vindex $?
8986 @vindex ? @r{(automatic variable)}
8987 @item $?
8988 The names of all the prerequisites that are newer than the target, with
8989 spaces between them.  For prerequisites which are archive members, only
8990 the member named is used (@pxref{Archives}).
8991 @cindex prerequisites, list of changed
8992 @cindex list of changed prerequisites
8994 @vindex $^
8995 @vindex ^ @r{(automatic variable)}
8996 @item $^
8997 The names of all the prerequisites, with spaces between them.  For
8998 prerequisites which are archive members, only the member named is used
8999 (@pxref{Archives}).  A target has only one prerequisite on each other file
9000 it depends on, no matter how many times each file is listed as a
9001 prerequisite.  So if you list a prerequisite more than once for a target,
9002 the value of @code{$^} contains just one copy of the name.  This list
9003 does @strong{not} contain any of the order-only prerequisites; for those
9004 see the @samp{$|} variable, below.
9005 @cindex prerequisites, list of all
9006 @cindex list of all prerequisites
9008 @vindex $+
9009 @vindex + @r{(automatic variable)}
9010 @item $+
9011 This is like @samp{$^}, but prerequisites listed more than once are
9012 duplicated in the order they were listed in the makefile.  This is
9013 primarily useful for use in linking commands where it is meaningful to
9014 repeat library file names in a particular order.
9016 @vindex $|
9017 @vindex | @r{(automatic variable)}
9018 @item $|
9019 The names of all the order-only prerequisites, with spaces between
9020 them.
9022 @vindex $*
9023 @vindex * @r{(automatic variable)}
9024 @item $*
9025 The stem with which an implicit rule matches (@pxref{Pattern Match, ,How
9026 Patterns Match}).  If the target is @file{dir/a.foo.b} and the target
9027 pattern is @file{a.%.b} then the stem is @file{dir/foo}.  The stem is
9028 useful for constructing names of related files.@refill
9029 @cindex stem, variable for
9031 In a static pattern rule, the stem is part of the file name that matched
9032 the @samp{%} in the target pattern.
9034 In an explicit rule, there is no stem; so @samp{$*} cannot be determined
9035 in that way.  Instead, if the target name ends with a recognized suffix
9036 (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}), @samp{$*} is set to
9037 the target name minus the suffix.  For example, if the target name is
9038 @samp{foo.c}, then @samp{$*} is set to @samp{foo}, since @samp{.c} is a
9039 suffix.  GNU @code{make} does this bizarre thing only for compatibility
9040 with other implementations of @code{make}.  You should generally avoid
9041 using @samp{$*} except in implicit rules or static pattern rules.@refill
9043 If the target name in an explicit rule does not end with a recognized
9044 suffix, @samp{$*} is set to the empty string for that rule.
9045 @end table
9047 @samp{$?} is useful even in explicit rules when you wish to operate on only
9048 the prerequisites that have changed.  For example, suppose that an archive
9049 named @file{lib} is supposed to contain copies of several object files.
9050 This rule copies just the changed object files into the archive:
9052 @example
9053 @group
9054 lib: foo.o bar.o lose.o win.o
9055         ar r lib $?
9056 @end group
9057 @end example
9059 Of the variables listed above, four have values that are single file
9060 names, and three have values that are lists of file names.  These seven
9061 have variants that get just the file's directory name or just the file
9062 name within the directory.  The variant variables' names are formed by
9063 appending @samp{D} or @samp{F}, respectively.  These variants are
9064 semi-obsolete in GNU @code{make} since the functions @code{dir} and
9065 @code{notdir} can be used to get a similar effect (@pxref{File Name
9066 Functions, , Functions for File Names}).  Note, however, that the
9067 @samp{D} variants all omit the trailing slash which always appears in
9068 the output of the @code{dir} function.  Here is a table of the variants:
9070 @table @samp
9071 @vindex $(@@D)
9072 @vindex @@D @r{(automatic variable)}
9073 @item $(@@D)
9074 The directory part of the file name of the target, with the trailing
9075 slash removed.  If the value of @samp{$@@} is @file{dir/foo.o} then
9076 @samp{$(@@D)} is @file{dir}.  This value is @file{.} if @samp{$@@} does
9077 not contain a slash.
9079 @vindex $(@@F)
9080 @vindex @@F @r{(automatic variable)}
9081 @item $(@@F)
9082 The file-within-directory part of the file name of the target.  If the
9083 value of @samp{$@@} is @file{dir/foo.o} then @samp{$(@@F)} is
9084 @file{foo.o}.  @samp{$(@@F)} is equivalent to @samp{$(notdir $@@)}.
9086 @vindex $(*D)
9087 @vindex *D @r{(automatic variable)}
9088 @item $(*D)
9089 @vindex $(*F)
9090 @vindex *F @r{(automatic variable)}
9091 @itemx $(*F)
9092 The directory part and the file-within-directory
9093 part of the stem; @file{dir} and @file{foo} in this example.
9095 @vindex $(%D)
9096 @vindex %D @r{(automatic variable)}
9097 @item $(%D)
9098 @vindex $(%F)
9099 @vindex %F @r{(automatic variable)}
9100 @itemx $(%F)
9101 The directory part and the file-within-directory part of the target
9102 archive member name.  This makes sense only for archive member targets
9103 of the form @file{@var{archive}(@var{member})} and is useful only when
9104 @var{member} may contain a directory name.  (@xref{Archive Members,
9105 ,Archive Members as Targets}.)
9107 @vindex $(<D)
9108 @vindex <D @r{(automatic variable)}
9109 @item $(<D)
9110 @vindex $(<F)
9111 @vindex <F @r{(automatic variable)}
9112 @itemx $(<F)
9113 The directory part and the file-within-directory
9114 part of the first prerequisite.
9116 @vindex $(^D)
9117 @vindex ^D @r{(automatic variable)}
9118 @item $(^D)
9119 @vindex $(^F)
9120 @vindex ^F @r{(automatic variable)}
9121 @itemx $(^F)
9122 Lists of the directory parts and the file-within-directory
9123 parts of all prerequisites.
9125 @vindex $(+D)
9126 @vindex +D @r{(automatic variable)}
9127 @item $(+D)
9128 @vindex $(+F)
9129 @vindex +F @r{(automatic variable)}
9130 @itemx $(+F)
9131 Lists of the directory parts and the file-within-directory
9132 parts of all prerequisites, including multiple instances of duplicated
9133 prerequisites.
9135 @vindex $(?D)
9136 @vindex ?D @r{(automatic variable)}
9137 @item $(?D)
9138 @vindex $(?F)
9139 @vindex ?F @r{(automatic variable)}
9140 @itemx $(?F)
9141 Lists of the directory parts and the file-within-directory parts of
9142 all prerequisites that are newer than the target.
9143 @end table
9145 Note that we use a special stylistic convention when we talk about these
9146 automatic variables; we write ``the value of @samp{$<}'', rather than
9147 @w{``the variable @code{<}''} as we would write for ordinary variables
9148 such as @code{objects} and @code{CFLAGS}.  We think this convention
9149 looks more natural in this special case.  Please do not assume it has a
9150 deep significance; @samp{$<} refers to the variable named @code{<} just
9151 as @samp{$(CFLAGS)} refers to the variable named @code{CFLAGS}.
9152 You could just as well use @samp{$(<)} in place of @samp{$<}.
9154 @node Pattern Match, Match-Anything Rules, Automatic Variables, Pattern Rules
9155 @subsection How Patterns Match
9157 @cindex stem
9158 A target pattern is composed of a @samp{%} between a prefix and a suffix,
9159 either or both of which may be empty.  The pattern matches a file name only
9160 if the file name starts with the prefix and ends with the suffix, without
9161 overlap.  The text between the prefix and the suffix is called the
9162 @dfn{stem}.  Thus, when the pattern @samp{%.o} matches the file name
9163 @file{test.o}, the stem is @samp{test}.  The pattern rule prerequisites are
9164 turned into actual file names by substituting the stem for the character
9165 @samp{%}.  Thus, if in the same example one of the prerequisites is written
9166 as @samp{%.c}, it expands to @samp{test.c}.@refill
9168 When the target pattern does not contain a slash (and it usually does
9169 not), directory names in the file names are removed from the file name
9170 before it is compared with the target prefix and suffix.  After the
9171 comparison of the file name to the target pattern, the directory
9172 names, along with the slash that ends them, are added on to the
9173 prerequisite file names generated from the pattern rule's prerequisite
9174 patterns and the file name.  The directories are ignored only for the
9175 purpose of finding an implicit rule to use, not in the application of
9176 that rule.  Thus, @samp{e%t} matches the file name @file{src/eat},
9177 with @samp{src/a} as the stem.  When prerequisites are turned into file
9178 names, the directories from the stem are added at the front, while the
9179 rest of the stem is substituted for the @samp{%}.  The stem
9180 @samp{src/a} with a prerequisite pattern @samp{c%r} gives the file name
9181 @file{src/car}.@refill
9183 @node Match-Anything Rules, Canceling Rules, Pattern Match, Pattern Rules
9184 @subsection Match-Anything Pattern Rules
9186 @cindex match-anything rule
9187 @cindex terminal rule
9188 When a pattern rule's target is just @samp{%}, it matches any file name
9189 whatever.  We call these rules @dfn{match-anything} rules.  They are very
9190 useful, but it can take a lot of time for @code{make} to think about them,
9191 because it must consider every such rule for each file name listed either
9192 as a target or as a prerequisite.
9194 Suppose the makefile mentions @file{foo.c}.  For this target, @code{make}
9195 would have to consider making it by linking an object file @file{foo.c.o},
9196 or by C compilation-and-linking in one step from @file{foo.c.c}, or by
9197 Pascal compilation-and-linking from @file{foo.c.p}, and many other
9198 possibilities.
9200 We know these possibilities are ridiculous since @file{foo.c} is a C source
9201 file, not an executable.  If @code{make} did consider these possibilities,
9202 it would ultimately reject them, because files such as @file{foo.c.o} and
9203 @file{foo.c.p} would not exist.  But these possibilities are so
9204 numerous that @code{make} would run very slowly if it had to consider
9205 them.@refill
9207 To gain speed, we have put various constraints on the way @code{make}
9208 considers match-anything rules.  There are two different constraints that
9209 can be applied, and each time you define a match-anything rule you must
9210 choose one or the other for that rule.
9212 One choice is to mark the match-anything rule as @dfn{terminal} by defining
9213 it with a double colon.  When a rule is terminal, it does not apply unless
9214 its prerequisites actually exist.  Prerequisites that could be made with
9215 other implicit rules are not good enough.  In other words, no further
9216 chaining is allowed beyond a terminal rule.
9218 For example, the built-in implicit rules for extracting sources from RCS
9219 and SCCS files are terminal; as a result, if the file @file{foo.c,v} does
9220 not exist, @code{make} will not even consider trying to make it as an
9221 intermediate file from @file{foo.c,v.o} or from @file{RCS/SCCS/s.foo.c,v}.
9222 RCS and SCCS files are generally ultimate source files, which should not be
9223 remade from any other files; therefore, @code{make} can save time by not
9224 looking for ways to remake them.@refill
9226 If you do not mark the match-anything rule as terminal, then it is
9227 nonterminal.  A nonterminal match-anything rule cannot apply to a file name
9228 that indicates a specific type of data.  A file name indicates a specific
9229 type of data if some non-match-anything implicit rule target matches it.
9231 For example, the file name @file{foo.c} matches the target for the pattern
9232 rule @samp{%.c : %.y} (the rule to run Yacc).  Regardless of whether this
9233 rule is actually applicable (which happens only if there is a file
9234 @file{foo.y}), the fact that its target matches is enough to prevent
9235 consideration of any nonterminal match-anything rules for the file
9236 @file{foo.c}.  Thus, @code{make} will not even consider trying to make
9237 @file{foo.c} as an executable file from @file{foo.c.o}, @file{foo.c.c},
9238 @file{foo.c.p}, etc.@refill
9240 The motivation for this constraint is that nonterminal match-anything
9241 rules are used for making files containing specific types of data (such as
9242 executable files) and a file name with a recognized suffix indicates some
9243 other specific type of data (such as a C source file).
9245 Special built-in dummy pattern rules are provided solely to recognize
9246 certain file names so that nonterminal match-anything rules will not be
9247 considered.  These dummy rules have no prerequisites and no commands, and
9248 they are ignored for all other purposes.  For example, the built-in
9249 implicit rule
9251 @example
9252 %.p :
9253 @end example
9255 @noindent
9256 exists to make sure that Pascal source files such as @file{foo.p} match a
9257 specific target pattern and thereby prevent time from being wasted looking
9258 for @file{foo.p.o} or @file{foo.p.c}.
9260 Dummy pattern rules such as the one for @samp{%.p} are made for every
9261 suffix listed as valid for use in suffix rules (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}).
9263 @node Canceling Rules,  , Match-Anything Rules, Pattern Rules
9264 @subsection Canceling Implicit Rules
9266 You can override a built-in implicit rule (or one you have defined
9267 yourself) by defining a new pattern rule with the same target and
9268 prerequisites, but different commands.  When the new rule is defined, the
9269 built-in one is replaced.  The new rule's position in the sequence of
9270 implicit rules is determined by where you write the new rule.
9272 You can cancel a built-in implicit rule by defining a pattern rule with the
9273 same target and prerequisites, but no commands.  For example, the following
9274 would cancel the rule that runs the assembler:
9276 @example
9277 %.o : %.s
9278 @end example
9280 @node Last Resort, Suffix Rules, Pattern Rules, Implicit Rules
9281 @section Defining Last-Resort Default Rules
9282 @cindex last-resort default rules
9283 @cindex default rules, last-resort
9285 You can define a last-resort implicit rule by writing a terminal
9286 match-anything pattern rule with no prerequisites (@pxref{Match-Anything
9287 Rules}).  This is just like any other pattern rule; the only thing
9288 special about it is that it will match any target.  So such a rule's
9289 commands are used for all targets and prerequisites that have no commands
9290 of their own and for which no other implicit rule applies.
9292 For example, when testing a makefile, you might not care if the source
9293 files contain real data, only that they exist.  Then you might do this:
9295 @example
9297         touch $@@
9298 @end example
9300 @noindent
9301 to cause all the source files needed (as prerequisites) to be created
9302 automatically.
9304 @findex .DEFAULT
9305 You can instead define commands to be used for targets for which there
9306 are no rules at all, even ones which don't specify commands.  You do
9307 this by writing a rule for the target @code{.DEFAULT}.  Such a rule's
9308 commands are used for all prerequisites which do not appear as targets in
9309 any explicit rule, and for which no implicit rule applies.  Naturally,
9310 there is no @code{.DEFAULT} rule unless you write one.
9312 If you use @code{.DEFAULT} with no commands or prerequisites:
9314 @example
9315 .DEFAULT:
9316 @end example
9318 @noindent
9319 the commands previously stored for @code{.DEFAULT} are cleared.
9320 Then @code{make} acts as if you had never defined @code{.DEFAULT} at all.
9322 If you do not want a target to get the commands from a match-anything
9323 pattern rule or @code{.DEFAULT}, but you also do not want any commands
9324 to be run for the target, you can give it empty commands (@pxref{Empty
9325 Commands, ,Defining Empty Commands}).@refill
9327 You can use a last-resort rule to override part of another makefile.
9328 @xref{Overriding Makefiles, , Overriding Part of Another Makefile}.
9330 @node Suffix Rules, Implicit Rule Search, Last Resort, Implicit Rules
9331 @section Old-Fashioned Suffix Rules
9332 @cindex old-fashioned suffix rules
9333 @cindex suffix rule
9335 @dfn{Suffix rules} are the old-fashioned way of defining implicit rules for
9336 @code{make}.  Suffix rules are obsolete because pattern rules are more
9337 general and clearer.  They are supported in GNU @code{make} for
9338 compatibility with old makefiles.  They come in two kinds:
9339 @dfn{double-suffix} and @dfn{single-suffix}.@refill
9341 A double-suffix rule is defined by a pair of suffixes: the target suffix
9342 and the source suffix.  It matches any file whose name ends with the
9343 target suffix.  The corresponding implicit prerequisite is made by
9344 replacing the target suffix with the source suffix in the file name.  A
9345 two-suffix rule whose target and source suffixes are @samp{.o} and
9346 @samp{.c} is equivalent to the pattern rule @samp{%.o : %.c}.
9348 A single-suffix rule is defined by a single suffix, which is the source
9349 suffix.  It matches any file name, and the corresponding implicit
9350 prerequisite name is made by appending the source suffix.  A single-suffix
9351 rule whose source suffix is @samp{.c} is equivalent to the pattern rule
9352 @samp{% : %.c}.
9354 Suffix rule definitions are recognized by comparing each rule's target
9355 against a defined list of known suffixes.  When @code{make} sees a rule
9356 whose target is a known suffix, this rule is considered a single-suffix
9357 rule.  When @code{make} sees a rule whose target is two known suffixes
9358 concatenated, this rule is taken as a double-suffix rule.
9360 For example, @samp{.c} and @samp{.o} are both on the default list of
9361 known suffixes.  Therefore, if you define a rule whose target is
9362 @samp{.c.o}, @code{make} takes it to be a double-suffix rule with source
9363 suffix @samp{.c} and target suffix @samp{.o}.  Here is the old-fashioned
9364 way to define the rule for compiling a C source file:@refill
9366 @example
9367 .c.o:
9368         $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
9369 @end example
9371 Suffix rules cannot have any prerequisites of their own.  If they have any,
9372 they are treated as normal files with funny names, not as suffix rules.
9373 Thus, the rule:
9375 @example
9376 .c.o: foo.h
9377         $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
9378 @end example
9380 @noindent
9381 tells how to make the file @file{.c.o} from the prerequisite file
9382 @file{foo.h}, and is not at all like the pattern rule:
9384 @example
9385 %.o: %.c foo.h
9386         $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
9387 @end example
9389 @noindent
9390 which tells how to make @samp{.o} files from @samp{.c} files, and makes all
9391 @samp{.o} files using this pattern rule also depend on @file{foo.h}.
9393 Suffix rules with no commands are also meaningless.  They do not remove
9394 previous rules as do pattern rules with no commands (@pxref{Canceling
9395 Rules, , Canceling Implicit Rules}).  They simply enter the suffix or pair of suffixes concatenated as
9396 a target in the data base.@refill
9398 @findex .SUFFIXES
9399 The known suffixes are simply the names of the prerequisites of the special
9400 target @code{.SUFFIXES}.  You can add your own suffixes by writing a rule
9401 for @code{.SUFFIXES} that adds more prerequisites, as in:
9403 @example
9404 .SUFFIXES: .hack .win
9405 @end example
9407 @noindent
9408 which adds @samp{.hack} and @samp{.win} to the end of the list of suffixes.
9410 If you wish to eliminate the default known suffixes instead of just adding
9411 to them, write a rule for @code{.SUFFIXES} with no prerequisites.  By
9412 special dispensation, this eliminates all existing prerequisites of
9413 @code{.SUFFIXES}.  You can then write another rule to add the suffixes you
9414 want.  For example,
9416 @example
9417 @group
9418 .SUFFIXES:            # @r{Delete the default suffixes}
9419 .SUFFIXES: .c .o .h   # @r{Define our suffix list}
9420 @end group
9421 @end example
9423 The @samp{-r} or @samp{--no-builtin-rules} flag causes the default
9424 list of suffixes to be empty.
9426 @vindex SUFFIXES
9427 The variable @code{SUFFIXES} is defined to the default list of suffixes
9428 before @code{make} reads any makefiles.  You can change the list of suffixes
9429 with a rule for the special target @code{.SUFFIXES}, but that does not alter
9430 this variable.
9432 @node Implicit Rule Search,  , Suffix Rules, Implicit Rules
9433 @section Implicit Rule Search Algorithm
9434 @cindex implicit rule, search algorithm
9435 @cindex search algorithm, implicit rule
9437 Here is the procedure @code{make} uses for searching for an implicit rule
9438 for a target @var{t}.  This procedure is followed for each double-colon
9439 rule with no commands, for each target of ordinary rules none of which have
9440 commands, and for each prerequisite that is not the target of any rule.  It
9441 is also followed recursively for prerequisites that come from implicit
9442 rules, in the search for a chain of rules.
9444 Suffix rules are not mentioned in this algorithm because suffix rules are
9445 converted to equivalent pattern rules once the makefiles have been read in.
9447 For an archive member target of the form
9448 @samp{@var{archive}(@var{member})}, the following algorithm is run
9449 twice, first using the entire target name @var{t}, and second using
9450 @samp{(@var{member})} as the target @var{t} if the first run found no
9451 rule.@refill
9453 @enumerate
9454 @item
9455 Split @var{t} into a directory part, called @var{d}, and the rest,
9456 called @var{n}.  For example, if @var{t} is @samp{src/foo.o}, then
9457 @var{d} is @samp{src/} and @var{n} is @samp{foo.o}.@refill
9459 @item
9460 Make a list of all the pattern rules one of whose targets matches
9461 @var{t} or @var{n}.  If the target pattern contains a slash, it is
9462 matched against @var{t}; otherwise, against @var{n}.
9464 @item
9465 If any rule in that list is @emph{not} a match-anything rule, then
9466 remove all nonterminal match-anything rules from the list.
9468 @item
9469 Remove from the list all rules with no commands.
9471 @item
9472 For each pattern rule in the list:
9474 @enumerate a
9475 @item
9476 Find the stem @var{s}, which is the nonempty part of @var{t} or @var{n}
9477 matched by the @samp{%} in the target pattern.@refill
9479 @item
9480 Compute the prerequisite names by substituting @var{s} for @samp{%}; if
9481 the target pattern does not contain a slash, append @var{d} to
9482 the front of each prerequisite name.@refill
9484 @item
9485 Test whether all the prerequisites exist or ought to exist.  (If a
9486 file name is mentioned in the makefile as a target or as an explicit
9487 prerequisite, then we say it ought to exist.)
9489 If all prerequisites exist or ought to exist, or there are no prerequisites,
9490 then this rule applies.
9491 @end enumerate
9493 @item
9494 If no pattern rule has been found so far, try harder.
9495 For each pattern rule in the list:
9497 @enumerate a
9498 @item
9499 If the rule is terminal, ignore it and go on to the next rule.
9501 @item
9502 Compute the prerequisite names as before.
9504 @item
9505 Test whether all the prerequisites exist or ought to exist.
9507 @item
9508 For each prerequisite that does not exist, follow this algorithm
9509 recursively to see if the prerequisite can be made by an implicit
9510 rule.
9512 @item
9513 If all prerequisites exist, ought to exist, or can be
9514 made by implicit rules, then this rule applies.
9515 @end enumerate
9517 @item
9518 If no implicit rule applies, the rule for @code{.DEFAULT}, if any,
9519 applies.  In that case, give @var{t} the same commands that
9520 @code{.DEFAULT} has.  Otherwise, there are no commands for @var{t}.
9521 @end enumerate
9523 Once a rule that applies has been found, for each target pattern of the
9524 rule other than the one that matched @var{t} or @var{n}, the @samp{%} in
9525 the pattern is replaced with @var{s} and the resultant file name is stored
9526 until the commands to remake the target file @var{t} are executed.  After
9527 these commands are executed, each of these stored file names are entered
9528 into the data base and marked as having been updated and having the same
9529 update status as the file @var{t}.
9531 When the commands of a pattern rule are executed for @var{t}, the automatic
9532 variables are set corresponding to the target and prerequisites.
9533 @xref{Automatic Variables}.
9535 @node Archives, Features, Implicit Rules, Top
9536 @chapter Using @code{make} to Update Archive Files
9537 @cindex archive
9539 @dfn{Archive files} are files containing named subfiles called
9540 @dfn{members}; they are maintained with the program @code{ar} and their
9541 main use is as subroutine libraries for linking.
9543 @menu
9544 * Archive Members::             Archive members as targets.
9545 * Archive Update::              The implicit rule for archive member targets.
9546 * Archive Pitfalls::            Dangers to watch out for when using archives.
9547 * Archive Suffix Rules::        You can write a special kind of suffix rule
9548                                   for updating archives.
9549 @end menu
9551 @node Archive Members, Archive Update, Archives, Archives
9552 @section Archive Members as Targets
9553 @cindex archive member targets
9555 An individual member of an archive file can be used as a target or
9556 prerequisite in @code{make}.  You specify the member named @var{member} in
9557 archive file @var{archive} as follows:
9559 @example
9560 @var{archive}(@var{member})
9561 @end example
9563 @noindent
9564 This construct is available only in targets and prerequisites, not in
9565 commands!  Most programs that you might use in commands do not support this
9566 syntax and cannot act directly on archive members.  Only @code{ar} and
9567 other programs specifically designed to operate on archives can do so.
9568 Therefore, valid commands to update an archive member target probably must
9569 use @code{ar}.  For example, this rule says to create a member
9570 @file{hack.o} in archive @file{foolib} by copying the file @file{hack.o}:
9572 @example
9573 foolib(hack.o) : hack.o
9574         ar cr foolib hack.o
9575 @end example
9577 In fact, nearly all archive member targets are updated in just this way
9578 and there is an implicit rule to do it for you.  @strong{Please note:} The
9579 @samp{c} flag to @code{ar} is required if the archive file does not
9580 already exist.
9582 To specify several members in the same archive, you can write all the
9583 member names together between the parentheses.  For example:
9585 @example
9586 foolib(hack.o kludge.o)
9587 @end example
9589 @noindent
9590 is equivalent to:
9592 @example
9593 foolib(hack.o) foolib(kludge.o)
9594 @end example
9596 @cindex wildcard, in archive member
9597 You can also use shell-style wildcards in an archive member reference.
9598 @xref{Wildcards, ,Using Wildcard Characters in File Names}.  For
9599 example, @w{@samp{foolib(*.o)}} expands to all existing members of the
9600 @file{foolib} archive whose names end in @samp{.o}; perhaps
9601 @samp{@w{foolib(hack.o)} @w{foolib(kludge.o)}}.
9603 @node Archive Update, Archive Pitfalls, Archive Members, Archives
9604 @section Implicit Rule for Archive Member Targets
9606 Recall that a target that looks like @file{@var{a}(@var{m})} stands for the
9607 member named @var{m} in the archive file @var{a}.
9609 When @code{make} looks for an implicit rule for such a target, as a special
9610 feature it considers implicit rules that match @file{(@var{m})}, as well as
9611 those that match the actual target @file{@var{a}(@var{m})}.
9613 This causes one special rule whose target is @file{(%)} to match.  This
9614 rule updates the target @file{@var{a}(@var{m})} by copying the file @var{m}
9615 into the archive.  For example, it will update the archive member target
9616 @file{foo.a(bar.o)} by copying the @emph{file} @file{bar.o} into the
9617 archive @file{foo.a} as a @emph{member} named @file{bar.o}.
9619 When this rule is chained with others, the result is very powerful.
9620 Thus, @samp{make "foo.a(bar.o)"} (the quotes are needed to protect the
9621 @samp{(} and @samp{)} from being interpreted specially by the shell) in
9622 the presence of a file @file{bar.c} is enough to cause the following
9623 commands to be run, even without a makefile:
9625 @example
9626 cc -c bar.c -o bar.o
9627 ar r foo.a bar.o
9628 rm -f bar.o
9629 @end example
9631 @noindent
9632 Here @code{make} has envisioned the file @file{bar.o} as an intermediate
9633 file.  @xref{Chained Rules, ,Chains of Implicit Rules}.
9635 Implicit rules such as this one are written using the automatic variable
9636 @samp{$%}.  @xref{Automatic Variables}.
9638 An archive member name in an archive cannot contain a directory name, but
9639 it may be useful in a makefile to pretend that it does.  If you write an
9640 archive member target @file{foo.a(dir/file.o)}, @code{make} will perform
9641 automatic updating with this command:
9643 @example
9644 ar r foo.a dir/file.o
9645 @end example
9647 @noindent
9648 which has the effect of copying the file @file{dir/file.o} into a member
9649 named @file{file.o}.  In connection with such usage, the automatic variables
9650 @code{%D} and @code{%F} may be useful.
9652 @menu
9653 * Archive Symbols::             How to update archive symbol directories.
9654 @end menu
9656 @node Archive Symbols,  , Archive Update, Archive Update
9657 @subsection Updating Archive Symbol Directories
9658 @cindex @code{__.SYMDEF}
9659 @cindex updating archive symbol directories
9660 @cindex archive symbol directory updating
9661 @cindex symbol directories, updating archive
9662 @cindex directories, updating archive symbol
9664 An archive file that is used as a library usually contains a special member
9665 named @file{__.SYMDEF} that contains a directory of the external symbol
9666 names defined by all the other members.  After you update any other
9667 members, you need to update @file{__.SYMDEF} so that it will summarize the
9668 other members properly.  This is done by running the @code{ranlib} program:
9670 @example
9671 ranlib @var{archivefile}
9672 @end example
9674 Normally you would put this command in the rule for the archive file,
9675 and make all the members of the archive file prerequisites of that rule.
9676 For example,
9678 @example
9679 libfoo.a: libfoo.a(x.o) libfoo.a(y.o) @dots{}
9680         ranlib libfoo.a
9681 @end example
9683 @noindent
9684 The effect of this is to update archive members @file{x.o}, @file{y.o},
9685 etc., and then update the symbol directory member @file{__.SYMDEF} by
9686 running @code{ranlib}.  The rules for updating the members are not shown
9687 here; most likely you can omit them and use the implicit rule which copies
9688 files into the archive, as described in the preceding section.
9690 This is not necessary when using the GNU @code{ar} program, which
9691 updates the @file{__.SYMDEF} member automatically.
9693 @node Archive Pitfalls, Archive Suffix Rules, Archive Update, Archives
9694 @section Dangers When Using Archives
9695 @cindex archive, and parallel execution
9696 @cindex parallel execution, and archive update
9697 @cindex archive, and @code{-j}
9698 @cindex @code{-j}, and archive update
9700 It is important to be careful when using parallel execution (the
9701 @code{-j} switch; @pxref{Parallel, ,Parallel Execution}) and archives.
9702 If multiple @code{ar} commands run at the same time on the same archive
9703 file, they will not know about each other and can corrupt the file.
9705 Possibly a future version of @code{make} will provide a mechanism to
9706 circumvent this problem by serializing all commands that operate on the
9707 same archive file.  But for the time being, you must either write your
9708 makefiles to avoid this problem in some other way, or not use @code{-j}.
9710 @node Archive Suffix Rules,  , Archive Pitfalls, Archives
9711 @section Suffix Rules for Archive Files
9712 @cindex suffix rule, for archive
9713 @cindex archive, suffix rule for
9714 @cindex library archive, suffix rule for
9715 @cindex @code{.a} (archives)
9717 You can write a special kind of suffix rule for dealing with archive
9718 files.  @xref{Suffix Rules}, for a full explanation of suffix rules.
9719 Archive suffix rules are obsolete in GNU @code{make}, because pattern
9720 rules for archives are a more general mechanism (@pxref{Archive
9721 Update}).  But they are retained for compatibility with other
9722 @code{make}s.
9724 To write a suffix rule for archives, you simply write a suffix rule
9725 using the target suffix @samp{.a} (the usual suffix for archive files).
9726 For example, here is the old-fashioned suffix rule to update a library
9727 archive from C source files:
9729 @example
9730 @group
9731 .c.a:
9732         $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o
9733         $(AR) r $@@ $*.o
9734         $(RM) $*.o
9735 @end group
9736 @end example
9738 @noindent
9739 This works just as if you had written the pattern rule:
9741 @example
9742 @group
9743 (%.o): %.c
9744         $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o
9745         $(AR) r $@@ $*.o
9746         $(RM) $*.o
9747 @end group
9748 @end example
9750 In fact, this is just what @code{make} does when it sees a suffix rule
9751 with @samp{.a} as the target suffix.  Any double-suffix rule
9752 @w{@samp{.@var{x}.a}} is converted to a pattern rule with the target
9753 pattern @samp{(%.o)} and a prerequisite pattern of @samp{%.@var{x}}.
9755 Since you might want to use @samp{.a} as the suffix for some other kind
9756 of file, @code{make} also converts archive suffix rules to pattern rules
9757 in the normal way (@pxref{Suffix Rules}).  Thus a double-suffix rule
9758 @w{@samp{.@var{x}.a}} produces two pattern rules: @samp{@w{(%.o):}
9759 @w{%.@var{x}}} and @samp{@w{%.a}: @w{%.@var{x}}}.@refill
9761 @node Features, Missing, Archives, Top
9762 @chapter Features of GNU @code{make}
9763 @cindex features of GNU @code{make}
9764 @cindex portability
9765 @cindex compatibility
9767 Here is a summary of the features of GNU @code{make}, for comparison
9768 with and credit to other versions of @code{make}.  We consider the
9769 features of @code{make} in 4.2 BSD systems as a baseline.  If you are
9770 concerned with writing portable makefiles, you should not use the
9771 features of @code{make} listed here, nor the ones in @ref{Missing}.
9773 Many features come from the version of @code{make} in System V.
9775 @itemize @bullet
9776 @item
9777 The @code{VPATH} variable and its special meaning.
9778 @xref{Directory Search, , Searching Directories for Prerequisites}.
9779 This feature exists in System V @code{make}, but is undocumented.
9780 It is documented in 4.3 BSD @code{make} (which says it mimics System V's
9781 @code{VPATH} feature).@refill
9783 @item
9784 Included makefiles.  @xref{Include, ,Including Other Makefiles}.
9785 Allowing multiple files to be included with a single directive is a GNU
9786 extension.
9788 @item
9789 Variables are read from and communicated via the environment.
9790 @xref{Environment, ,Variables from the Environment}.
9792 @item
9793 Options passed through the variable @code{MAKEFLAGS} to recursive
9794 invocations of @code{make}.
9795 @xref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
9797 @item
9798 The automatic variable @code{$%} is set to the member name
9799 in an archive reference.  @xref{Automatic Variables}.
9801 @item
9802 The automatic variables @code{$@@}, @code{$*}, @code{$<}, @code{$%},
9803 and @code{$?} have corresponding forms like @code{$(@@F)} and
9804 @code{$(@@D)}.  We have generalized this to @code{$^} as an obvious
9805 extension.  @xref{Automatic Variables}.@refill
9807 @item
9808 Substitution variable references.
9809 @xref{Reference, ,Basics of Variable References}.
9811 @item
9812 The command-line options @samp{-b} and @samp{-m}, accepted and
9813 ignored.  In System V @code{make}, these options actually do something.
9815 @item
9816 Execution of recursive commands to run @code{make} via the variable
9817 @code{MAKE} even if @samp{-n}, @samp{-q} or @samp{-t} is specified.
9818 @xref{Recursion, ,Recursive Use of @code{make}}.
9820 @item
9821 Support for suffix @samp{.a} in suffix rules.  @xref{Archive Suffix
9822 Rules}.  This feature is obsolete in GNU @code{make}, because the
9823 general feature of rule chaining (@pxref{Chained Rules, ,Chains of
9824 Implicit Rules}) allows one pattern rule for installing members in an
9825 archive (@pxref{Archive Update}) to be sufficient.
9827 @item
9828 The arrangement of lines and backslash-newline combinations in
9829 commands is retained when the commands are printed, so they appear as
9830 they do in the makefile, except for the stripping of initial
9831 whitespace.
9832 @end itemize
9834 The following features were inspired by various other versions of
9835 @code{make}.  In some cases it is unclear exactly which versions inspired
9836 which others.
9838 @itemize @bullet
9839 @item
9840 Pattern rules using @samp{%}.
9841 This has been implemented in several versions of @code{make}.
9842 We're not sure who invented it first, but it's been spread around a bit.
9843 @xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.@refill
9845 @item
9846 Rule chaining and implicit intermediate files.
9847 This was implemented by Stu Feldman in his version of @code{make}
9848 for AT&T Eighth Edition Research Unix, and later by Andrew Hume of
9849 AT&T Bell Labs in his @code{mk} program (where he terms it
9850 ``transitive closure'').  We do not really know if
9851 we got this from either of them or thought it up ourselves at the
9852 same time.  @xref{Chained Rules, ,Chains of Implicit Rules}.
9854 @item
9855 The automatic variable @code{$^} containing a list of all prerequisites
9856 of the current target.  We did not invent this, but we have no idea who
9857 did.  @xref{Automatic Variables}.  The automatic variable
9858 @code{$+} is a simple extension of @code{$^}.
9860 @item
9861 The ``what if'' flag (@samp{-W} in GNU @code{make}) was (as far as we know)
9862 invented by Andrew Hume in @code{mk}.
9863 @xref{Instead of Execution, ,Instead of Executing the Commands}.
9865 @item
9866 The concept of doing several things at once (parallelism) exists in
9867 many incarnations of @code{make} and similar programs, though not in the
9868 System V or BSD implementations.  @xref{Execution, ,Command Execution}.
9870 @item
9871 Modified variable references using pattern substitution come from
9872 SunOS 4.  @xref{Reference, ,Basics of Variable References}.
9873 This functionality was provided in GNU @code{make} by the
9874 @code{patsubst} function before the alternate syntax was implemented
9875 for compatibility with SunOS 4.  It is not altogether clear who
9876 inspired whom, since GNU @code{make} had @code{patsubst} before SunOS
9877 4 was released.@refill
9879 @item
9880 The special significance of @samp{+} characters preceding command lines
9881 (@pxref{Instead of Execution, ,Instead of Executing the Commands}) is
9882 mandated by
9883 @cite{IEEE Standard 1003.2-1992} (POSIX.2).
9885 @item
9886 The @samp{+=} syntax to append to the value of a variable comes from SunOS
9887 4 @code{make}.  @xref{Appending, , Appending More Text to Variables}.
9889 @item
9890 The syntax @w{@samp{@var{archive}(@var{mem1} @var{mem2}@dots{})}} to list
9891 multiple members in a single archive file comes from SunOS 4 @code{make}.
9892 @xref{Archive Members}.
9894 @item
9895 The @code{-include} directive to include makefiles with no error for a
9896 nonexistent file comes from SunOS 4 @code{make}.  (But note that SunOS 4
9897 @code{make} does not allow multiple makefiles to be specified in one
9898 @code{-include} directive.)  The same feature appears with the name
9899 @code{sinclude} in SGI @code{make} and perhaps others.
9900 @end itemize
9902 The remaining features are inventions new in GNU @code{make}:
9904 @itemize @bullet
9905 @item
9906 Use the @samp{-v} or @samp{--version} option to print version and
9907 copyright information.
9909 @item
9910 Use the @samp{-h} or @samp{--help} option to summarize the options to
9911 @code{make}.
9913 @item
9914 Simply-expanded variables.  @xref{Flavors, ,The Two Flavors of Variables}.
9916 @item
9917 Pass command-line variable assignments automatically through the
9918 variable @code{MAKE} to recursive @code{make} invocations.
9919 @xref{Recursion, ,Recursive Use of @code{make}}.
9921 @item
9922 Use the @samp{-C} or @samp{--directory} command option to change
9923 directory.  @xref{Options Summary, ,Summary of Options}.
9925 @item
9926 Make verbatim variable definitions with @code{define}.
9927 @xref{Defining, ,Defining Variables Verbatim}.
9929 @item
9930 Declare phony targets with the special target @code{.PHONY}.
9932 Andrew Hume of AT&T Bell Labs implemented a similar feature with a
9933 different syntax in his @code{mk} program.  This seems to be a case of
9934 parallel discovery.  @xref{Phony Targets, ,Phony Targets}.
9936 @item
9937 Manipulate text by calling functions.
9938 @xref{Functions, ,Functions for Transforming Text}.
9940 @item
9941 Use the @samp{-o} or @samp{--old-file}
9942 option to pretend a file's modification-time is old.
9943 @xref{Avoiding Compilation, ,Avoiding Recompilation of Some Files}.
9945 @item
9946 Conditional execution.
9948 This feature has been implemented numerous times in various versions
9949 of @code{make}; it seems a natural extension derived from the features
9950 of the C preprocessor and similar macro languages and is not a
9951 revolutionary concept.  @xref{Conditionals, ,Conditional Parts of Makefiles}.
9953 @item
9954 Specify a search path for included makefiles.
9955 @xref{Include, ,Including Other Makefiles}.
9957 @item
9958 Specify extra makefiles to read with an environment variable.
9959 @xref{MAKEFILES Variable, ,The Variable @code{MAKEFILES}}.
9961 @item
9962 Strip leading sequences of @samp{./} from file names, so that
9963 @file{./@var{file}} and @file{@var{file}} are considered to be the
9964 same file.@refill
9966 @item
9967 Use a special search method for library prerequisites written in the
9968 form @samp{-l@var{name}}.
9969 @xref{Libraries/Search, ,Directory Search for Link Libraries}.
9971 @item
9972 Allow suffixes for suffix rules
9973 (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}) to contain any
9974 characters.  In other versions of @code{make}, they must begin with
9975 @samp{.} and not contain any @samp{/} characters.
9977 @item
9978 Keep track of the current level of @code{make} recursion using the
9979 variable @code{MAKELEVEL}.  @xref{Recursion, ,Recursive Use of @code{make}}.
9981 @item
9982 Provide any goals given on the command line in the variable
9983 @code{MAKECMDGOALS}.  @xref{Goals, ,Arguments to Specify the Goals}.
9985 @item
9986 Specify static pattern rules.  @xref{Static Pattern, ,Static Pattern Rules}.
9988 @item
9989 Provide selective @code{vpath} search.
9990 @xref{Directory Search, ,Searching Directories for Prerequisites}.
9992 @item
9993 Provide computed variable references.
9994 @xref{Reference, ,Basics of Variable References}.
9996 @item
9997 Update makefiles.  @xref{Remaking Makefiles, ,How Makefiles Are Remade}.
9998 System V @code{make} has a very, very limited form of this
9999 functionality in that it will check out SCCS files for makefiles.
10001 @item
10002 Various new built-in implicit rules.
10003 @xref{Catalogue of Rules, ,Catalogue of Implicit Rules}.
10005 @item
10006 The built-in variable @samp{MAKE_VERSION} gives the version number of
10007 @code{make}.
10008 @vindex MAKE_VERSION
10009 @end itemize
10011 @node Missing, Makefile Conventions, Features, Top
10012 @chapter Incompatibilities and Missing Features
10013 @cindex incompatibilities
10014 @cindex missing features
10015 @cindex features, missing
10017 The @code{make} programs in various other systems support a few features
10018 that are not implemented in GNU @code{make}.  The POSIX.2 standard
10019 (@cite{IEEE Standard 1003.2-1992}) which specifies @code{make} does not
10020 require any of these features.@refill
10022 @itemize @bullet
10023 @item
10024 A target of the form @samp{@var{file}((@var{entry}))} stands for a member
10025 of archive file @var{file}.  The member is chosen, not by name, but by
10026 being an object file which defines the linker symbol @var{entry}.@refill
10028 This feature was not put into GNU @code{make} because of the
10029 nonmodularity of putting knowledge into @code{make} of the internal
10030 format of archive file symbol tables.
10031 @xref{Archive Symbols, ,Updating Archive Symbol Directories}.
10033 @item
10034 Suffixes (used in suffix rules) that end with the character @samp{~}
10035 have a special meaning to System V @code{make};
10036 they refer to the SCCS file that corresponds
10037 to the file one would get without the @samp{~}.  For example, the
10038 suffix rule @samp{.c~.o} would make the file @file{@var{n}.o} from
10039 the SCCS file @file{s.@var{n}.c}.  For complete coverage, a whole
10040 series of such suffix rules is required.
10041 @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
10043 In GNU @code{make}, this entire series of cases is handled by two
10044 pattern rules for extraction from SCCS, in combination with the
10045 general feature of rule chaining.
10046 @xref{Chained Rules, ,Chains of Implicit Rules}.
10048 @item
10049 In System V and 4.3 BSD @code{make}, files found by @code{VPATH} search
10050 (@pxref{Directory Search, ,Searching Directories for Prerequisites}) have their names changed inside command
10051 strings.  We feel it is much cleaner to always use automatic variables
10052 and thus make this feature obsolete.@refill
10054 @item
10055 In some Unix @code{make}s, the automatic variable @code{$*} appearing in
10056 the prerequisites of a rule has the amazingly strange ``feature'' of
10057 expanding to the full name of the @emph{target of that rule}.  We cannot
10058 imagine what went on in the minds of Unix @code{make} developers to do
10059 this; it is utterly inconsistent with the normal definition of @code{$*}.
10060 @vindex * @r{(automatic variable), unsupported bizarre usage}
10062 @item
10063 In some Unix @code{make}s, implicit rule search
10064 (@pxref{Implicit Rules, ,Using Implicit Rules}) is apparently done for
10065 @emph{all} targets, not just those without commands.  This means you can
10066 do:@refill
10068 @example
10069 @group
10070 foo.o:
10071         cc -c foo.c
10072 @end group
10073 @end example
10075 @noindent
10076 and Unix @code{make} will intuit that @file{foo.o} depends on
10077 @file{foo.c}.@refill
10079 We feel that such usage is broken.  The prerequisite properties of
10080 @code{make} are well-defined (for GNU @code{make}, at least),
10081 and doing such a thing simply does not fit the model.@refill
10083 @item
10084 GNU @code{make} does not include any built-in implicit rules for
10085 compiling or preprocessing EFL programs.  If we hear of anyone who is
10086 using EFL, we will gladly add them.
10088 @item
10089 It appears that in SVR4 @code{make}, a suffix rule can be specified with
10090 no commands, and it is treated as if it had empty commands
10091 (@pxref{Empty Commands}).  For example:
10093 @example
10094 .c.a:
10095 @end example
10097 @noindent
10098 will override the built-in @file{.c.a} suffix rule.
10100 We feel that it is cleaner for a rule without commands to always simply
10101 add to the prerequisite list for the target.  The above example can be
10102 easily rewritten to get the desired behavior in GNU @code{make}:
10104 @example
10105 .c.a: ;
10106 @end example
10108 @item
10109 Some versions of @code{make} invoke the shell with the @samp{-e} flag,
10110 except under @samp{-k} (@pxref{Testing, ,Testing the Compilation of a
10111 Program}).  The @samp{-e} flag tells the shell to exit as soon as any
10112 program it runs returns a nonzero status.  We feel it is cleaner to
10113 write each shell command line to stand on its own and not require this
10114 special treatment.
10115 @end itemize
10117 @comment The makefile standards are in a separate file that is also
10118 @comment included by standards.texi.
10119 @include make-stds.texi
10121 @node Quick Reference, Error Messages, Makefile Conventions, Top
10122 @appendix Quick Reference
10124 This appendix summarizes the directives, text manipulation functions,
10125 and special variables which GNU @code{make} understands.
10126 @xref{Special Targets}, @ref{Catalogue of Rules, ,Catalogue of Implicit Rules},
10127 and @ref{Options Summary, ,Summary of Options},
10128 for other summaries.
10130 Here is a summary of the directives GNU @code{make} recognizes:
10132 @table @code
10133 @item define @var{variable}
10134 @itemx endef
10136 Define a multi-line, recursively-expanded variable.@*
10137 @xref{Sequences}.
10139 @item ifdef @var{variable}
10140 @itemx ifndef @var{variable}
10141 @itemx ifeq (@var{a},@var{b})
10142 @itemx ifeq "@var{a}" "@var{b}"
10143 @itemx ifeq '@var{a}' '@var{b}'
10144 @itemx ifneq (@var{a},@var{b})
10145 @itemx ifneq "@var{a}" "@var{b}"
10146 @itemx ifneq '@var{a}' '@var{b}'
10147 @itemx else
10148 @itemx endif
10150 Conditionally evaluate part of the makefile.@*
10151 @xref{Conditionals}.
10153 @item include @var{file}
10154 @itemx -include @var{file}
10155 @itemx sinclude @var{file}
10157 Include another makefile.@*
10158 @xref{Include, ,Including Other Makefiles}.
10160 @item override @var{variable} = @var{value}
10161 @itemx override @var{variable} := @var{value}
10162 @itemx override @var{variable} += @var{value}
10163 @itemx override @var{variable} ?= @var{value}
10164 @itemx override define @var{variable}
10165 @itemx endef
10167 Define a variable, overriding any previous definition, even one from
10168 the command line.@*
10169 @xref{Override Directive, ,The @code{override} Directive}.
10171 @item export
10173 Tell @code{make} to export all variables to child processes by default.@*
10174 @xref{Variables/Recursion, , Communicating Variables to a Sub-@code{make}}.
10176 @item export @var{variable}
10177 @itemx export @var{variable} = @var{value}
10178 @itemx export @var{variable} := @var{value}
10179 @itemx export @var{variable} += @var{value}
10180 @itemx export @var{variable} ?= @var{value}
10181 @itemx unexport @var{variable}
10182 Tell @code{make} whether or not to export a particular variable to child
10183 processes.@*
10184 @xref{Variables/Recursion, , Communicating Variables to a Sub-@code{make}}.
10186 @item vpath @var{pattern} @var{path}
10187 Specify a search path for files matching a @samp{%} pattern.@*
10188 @xref{Selective Search, , The @code{vpath} Directive}.
10190 @item vpath @var{pattern}
10191 Remove all search paths previously specified for @var{pattern}.
10193 @item vpath
10194 Remove all search paths previously specified in any @code{vpath}
10195 directive.
10196 @end table
10198 Here is a summary of the built-in functions (@pxref{Functions}):
10200 @table @code
10201 @item $(subst @var{from},@var{to},@var{text})
10202 Replace @var{from} with @var{to} in @var{text}.@*
10203 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10205 @item $(patsubst @var{pattern},@var{replacement},@var{text})
10206 Replace words matching @var{pattern} with @var{replacement} in @var{text}.@*
10207 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10209 @item $(strip @var{string})
10210 Remove excess whitespace characters from @var{string}.@*
10211 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10213 @item $(findstring @var{find},@var{text})
10214 Locate @var{find} in @var{text}.@*
10215 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10217 @item $(filter @var{pattern}@dots{},@var{text})
10218 Select words in @var{text} that match one of the @var{pattern} words.@*
10219 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10221 @item $(filter-out @var{pattern}@dots{},@var{text})
10222 Select words in @var{text} that @emph{do not} match any of the @var{pattern} words.@*
10223 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10225 @item $(sort @var{list})
10226 Sort the words in @var{list} lexicographically, removing duplicates.@*
10227 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10229 @item $(word @var{n},@var{text})
10230 Extract the @var{n}th word (one-origin) of @var{text}.@*
10231 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10233 @item $(words @var{text})
10234 Count the number of words in @var{text}.@*
10235 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10237 @item $(wordlist @var{s},@var{e},@var{text})
10238 Returns the list of words in @var{text} from @var{s} to @var{e}.@*
10239 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10241 @item $(firstword @var{names}@dots{})
10242 Extract the first word of @var{names}.@*
10243 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10245 @item $(lastword @var{names}@dots{})
10246 Extract the last word of @var{names}.@*
10247 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10249 @item $(dir @var{names}@dots{})
10250 Extract the directory part of each file name.@*
10251 @xref{File Name Functions, ,Functions for File Names}.
10253 @item $(notdir @var{names}@dots{})
10254 Extract the non-directory part of each file name.@*
10255 @xref{File Name Functions, ,Functions for File Names}.
10257 @item $(suffix @var{names}@dots{})
10258 Extract the suffix (the last @samp{.} and following characters) of each file name.@*
10259 @xref{File Name Functions, ,Functions for File Names}.
10261 @item $(basename @var{names}@dots{})
10262 Extract the base name (name without suffix) of each file name.@*
10263 @xref{File Name Functions, ,Functions for File Names}.
10265 @item $(addsuffix @var{suffix},@var{names}@dots{})
10266 Append @var{suffix} to each word in @var{names}.@*
10267 @xref{File Name Functions, ,Functions for File Names}.
10269 @item $(addprefix @var{prefix},@var{names}@dots{})
10270 Prepend @var{prefix} to each word in @var{names}.@*
10271 @xref{File Name Functions, ,Functions for File Names}.
10273 @item $(join @var{list1},@var{list2})
10274 Join two parallel lists of words.@*
10275 @xref{File Name Functions, ,Functions for File Names}.
10277 @item $(wildcard @var{pattern}@dots{})
10278 Find file names matching a shell file name pattern (@emph{not} a
10279 @samp{%} pattern).@*
10280 @xref{Wildcard Function, ,The Function @code{wildcard}}.
10282 @item $(realpath @var{names}@dots{})
10283 For each file name in @var{names}, expand to an absolute name that
10284 does not contain any @code{.}, @code{..}, nor symlinks.@*
10285 @xref{File Name Functions, ,Functions for File Names}.
10287 @item $(abspath @var{names}@dots{})
10288 For each file name in @var{names}, expand to an absolute name that
10289 does not contain any @code{.} or @code{..} components, but preserves
10290 symlinks.@*
10291 @xref{File Name Functions, ,Functions for File Names}.
10293 @item $(error @var{text}@dots{})
10295 When this function is evaluated, @code{make} generates a fatal error
10296 with the message @var{text}.@*
10297 @xref{Make Control Functions, ,Functions That Control Make}.
10299 @item $(warning @var{text}@dots{})
10301 When this function is evaluated, @code{make} generates a warning with
10302 the message @var{text}.@*
10303 @xref{Make Control Functions, ,Functions That Control Make}.
10305 @item $(shell @var{command})
10307 Execute a shell command and return its output.@*
10308 @xref{Shell Function, , The @code{shell} Function}.
10310 @item $(origin @var{variable})
10312 Return a string describing how the @code{make} variable @var{variable} was
10313 defined.@*
10314 @xref{Origin Function, , The @code{origin} Function}.
10316 @item $(flavor @var{variable})
10318 Return a string describing the flavor of the @code{make} variable
10319 @var{variable}.@*
10320 @xref{Flavor Function, , The @code{flavor} Function}.
10322 @item $(foreach @var{var},@var{words},@var{text})
10324 Evaluate @var{text} with @var{var} bound to each word in @var{words},
10325 and concatenate the results.@*
10326 @xref{Foreach Function, ,The @code{foreach} Function}.
10328 @item $(call @var{var},@var{param},@dots{})
10330 Evaluate the variable @var{var} replacing any references to @code{$(1)},
10331 @code{$(2)} with the first, second, etc.@: @var{param} values.@*
10332 @xref{Call Function, ,The @code{call} Function}.
10334 @item $(eval @var{text})
10336 Evaluate @var{text} then read the results as makefile commands.
10337 Expands to the empty string.@*
10338 @xref{Eval Function, ,The @code{eval} Function}.
10340 @item $(value @var{var})
10342 Evaluates to the contents of the variable @var{var}, with no expansion
10343 performed on it.@*
10344 @xref{Value Function, ,The @code{value} Function}.
10345 @end table
10347 Here is a summary of the automatic variables.
10348 @xref{Automatic Variables},
10349 for full information.
10351 @table @code
10352 @item $@@
10353 The file name of the target.
10355 @item $%
10356 The target member name, when the target is an archive member.
10358 @item $<
10359 The name of the first prerequisite.
10361 @item $?
10362 The names of all the prerequisites that are
10363 newer than the target, with spaces between them.
10364 For prerequisites which are archive members, only
10365 the member named is used (@pxref{Archives}).
10367 @item $^
10368 @itemx $+
10369 The names of all the prerequisites, with spaces between them.  For
10370 prerequisites which are archive members, only the member named is used
10371 (@pxref{Archives}).  The value of @code{$^} omits duplicate
10372 prerequisites, while @code{$+} retains them and preserves their order.
10374 @item $*
10375 The stem with which an implicit rule matches
10376 (@pxref{Pattern Match, ,How Patterns Match}).
10378 @item $(@@D)
10379 @itemx $(@@F)
10380 The directory part and the file-within-directory part of @code{$@@}.
10382 @item $(*D)
10383 @itemx $(*F)
10384 The directory part and the file-within-directory part of @code{$*}.
10386 @item $(%D)
10387 @itemx $(%F)
10388 The directory part and the file-within-directory part of @code{$%}.
10390 @item $(<D)
10391 @itemx $(<F)
10392 The directory part and the file-within-directory part of @code{$<}.
10394 @item $(^D)
10395 @itemx $(^F)
10396 The directory part and the file-within-directory part of @code{$^}.
10398 @item $(+D)
10399 @itemx $(+F)
10400 The directory part and the file-within-directory part of @code{$+}.
10402 @item $(?D)
10403 @itemx $(?F)
10404 The directory part and the file-within-directory part of @code{$?}.
10405 @end table
10407 These variables are used specially by GNU @code{make}:
10409 @table @code
10410 @item MAKEFILES
10412 Makefiles to be read on every invocation of @code{make}.@*
10413 @xref{MAKEFILES Variable, ,The Variable @code{MAKEFILES}}.
10415 @item VPATH
10417 Directory search path for files not found in the current directory.@*
10418 @xref{General Search, , @code{VPATH} Search Path for All Prerequisites}.
10420 @item SHELL
10422 The name of the system default command interpreter, usually @file{/bin/sh}.
10423 You can set @code{SHELL} in the makefile to change the shell used to run
10424 commands.  @xref{Execution, ,Command Execution}.  The @code{SHELL}
10425 variable is handled specially when importing from and exporting to the
10426 environment.  @xref{Environment, ,Using Variable from the Environment}.
10428 @item MAKESHELL
10430 On MS-DOS only, the name of the command interpreter that is to be used
10431 by @code{make}.  This value takes precedence over the value of
10432 @code{SHELL}.  @xref{Execution, ,MAKESHELL variable}.
10434 @item MAKE
10436 The name with which @code{make} was invoked.
10437 Using this variable in commands has special meaning.
10438 @xref{MAKE Variable, ,How the @code{MAKE} Variable Works}.
10440 @item MAKELEVEL
10442 The number of levels of recursion (sub-@code{make}s).@*
10443 @xref{Variables/Recursion}.
10445 @item MAKEFLAGS
10447 The flags given to @code{make}.  You can set this in the environment or
10448 a makefile to set flags.@*
10449 @xref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
10451 It is @emph{never} appropriate to use @code{MAKEFLAGS} directly on a
10452 command line: its contents may not be quoted correctly for use in the
10453 shell.  Always allow recursive @code{make}'s to obtain these values
10454 through the environment from its parent.
10456 @item MAKECMDGOALS
10458 The targets given to @code{make} on the command line.  Setting this
10459 variable has no effect on the operation of @code{make}.@*
10460 @xref{Goals, ,Arguments to Specify the Goals}.
10462 @item CURDIR
10464 Set to the pathname of the current working directory (after all
10465 @code{-C} options are processed, if any).  Setting this variable has no
10466 effect on the operation of @code{make}.@*
10467 @xref{Recursion, ,Recursive Use of @code{make}}.
10469 @item SUFFIXES
10471 The default list of suffixes before @code{make} reads any makefiles.
10473 @item .LIBPATTERNS
10474 Defines the naming of the libraries @code{make} searches for, and their
10475 order.@*
10476 @xref{Libraries/Search, ,Directory Search for Link Libraries}.
10477 @end table
10479 @node Error Messages, Complex Makefile, Quick Reference, Top
10480 @comment  node-name,  next,  previous,  up
10481 @appendix Errors Generated by Make
10483 Here is a list of the more common errors you might see generated by
10484 @code{make}, and some information about what they mean and how to fix
10485 them.
10487 Sometimes @code{make} errors are not fatal, especially in the presence
10488 of a @code{-} prefix on a command script line, or the @code{-k} command
10489 line option.  Errors that are fatal are prefixed with the string
10490 @code{***}.
10492 Error messages are all either prefixed with the name of the program
10493 (usually @samp{make}), or, if the error is found in a makefile, the name
10494 of the file and linenumber containing the problem.
10496 In the table below, these common prefixes are left off.
10498 @table @samp
10500 @item [@var{foo}] Error @var{NN}
10501 @itemx [@var{foo}] @var{signal description}
10502 These errors are not really @code{make} errors at all.  They mean that a
10503 program that @code{make} invoked as part of a command script returned a
10504 non-0 error code (@samp{Error @var{NN}}), which @code{make} interprets
10505 as failure, or it exited in some other abnormal fashion (with a
10506 signal of some type).  @xref{Errors, ,Errors in Commands}.
10508 If no @code{***} is attached to the message, then the subprocess failed
10509 but the rule in the makefile was prefixed with the @code{-} special
10510 character, so @code{make} ignored the error.
10512 @item missing separator.  Stop.
10513 @itemx missing separator (did you mean TAB instead of 8 spaces?).  Stop.
10514 This means that @code{make} could not understand much of anything about
10515 the command line it just read.  GNU @code{make} looks for various kinds
10516 of separators (@code{:}, @code{=}, TAB characters, etc.) to help it
10517 decide what kind of commandline it's seeing.  This means it couldn't
10518 find a valid one.
10520 One of the most common reasons for this message is that you (or perhaps
10521 your oh-so-helpful editor, as is the case with many MS-Windows editors)
10522 have attempted to indent your command scripts with spaces instead of a
10523 TAB character.  In this case, @code{make} will use the second form of
10524 the error above.  Remember that every line in the command script must
10525 begin with a TAB character.  Eight spaces do not count.  @xref{Rule
10526 Syntax}.
10528 @item commands commence before first target.  Stop.
10529 @itemx missing rule before commands.  Stop.
10530 This means the first thing in the makefile seems to be part of a command
10531 script: it begins with a TAB character and doesn't appear to be a legal
10532 @code{make} command (such as a variable assignment).  Command scripts
10533 must always be associated with a target.
10535 The second form is generated if the line has a semicolon as the first
10536 non-whitespace character; @code{make} interprets this to mean you left
10537 out the "target: prerequisite" section of a rule.  @xref{Rule Syntax}.
10539 @item No rule to make target `@var{xxx}'.
10540 @itemx No rule to make target `@var{xxx}', needed by `@var{yyy}'.
10541 This means that @code{make} decided it needed to build a target, but
10542 then couldn't find any instructions in the makefile on how to do that,
10543 either explicit or implicit (including in the default rules database).
10545 If you want that file to be built, you will need to add a rule to your
10546 makefile describing how that target can be built.  Other possible
10547 sources of this problem are typos in the makefile (if that filename is
10548 wrong) or a corrupted source tree (if that file is not supposed to be
10549 built, but rather only a prerequisite).
10551 @item No targets specified and no makefile found.  Stop.
10552 @itemx No targets.  Stop.
10553 The former means that you didn't provide any targets to be built on the
10554 command line, and @code{make} couldn't find any makefiles to read in.
10555 The latter means that some makefile was found, but it didn't contain any
10556 default goal and none was given on the command line.  GNU @code{make}
10557 has nothing to do in these situations.
10558 @xref{Makefile Arguments, ,Arguments to Specify the Makefile}.@refill
10560 @item Makefile `@var{xxx}' was not found.
10561 @itemx Included makefile `@var{xxx}' was not found.
10562 A makefile specified on the command line (first form) or included
10563 (second form) was not found.
10565 @item warning: overriding commands for target `@var{xxx}'
10566 @itemx warning: ignoring old commands for target `@var{xxx}'
10567 GNU @code{make} allows commands to be specified only once per target
10568 (except for double-colon rules).  If you give commands for a target
10569 which already has been defined to have commands, this warning is issued
10570 and the second set of commands will overwrite the first set.
10571 @xref{Multiple Rules, ,Multiple Rules for One Target}.
10573 @item Circular @var{xxx} <- @var{yyy} dependency dropped.
10574 This means that @code{make} detected a loop in the dependency graph:
10575 after tracing the prerequisite @var{yyy} of target @var{xxx}, and its
10576 prerequisites, etc., one of them depended on @var{xxx} again.
10578 @item Recursive variable `@var{xxx}' references itself (eventually).  Stop.
10579 This means you've defined a normal (recursive) @code{make} variable
10580 @var{xxx} that, when it's expanded, will refer to itself (@var{xxx}).
10581 This is not allowed; either use simply-expanded variables (@code{:=}) or
10582 use the append operator (@code{+=}).  @xref{Using Variables, ,How to Use
10583 Variables}.
10585 @item Unterminated variable reference.  Stop.
10586 This means you forgot to provide the proper closing parenthesis
10587 or brace in your variable or function reference.
10589 @item insufficient arguments to function `@var{xxx}'.  Stop.
10590 This means you haven't provided the requisite number of arguments for
10591 this function.  See the documentation of the function for a description
10592 of its arguments.  @xref{Functions, ,Functions for Transforming Text}.
10594 @item missing target pattern.  Stop.
10595 @itemx multiple target patterns.  Stop.
10596 @itemx target pattern contains no `%'.  Stop.
10597 @itemx mixed implicit and static pattern rules.  Stop.
10598 These are generated for malformed static pattern rules.  The first means
10599 there's no pattern in the target section of the rule; the second means
10600 there are multiple patterns in the target section; the third means
10601 the target doesn't contain a pattern character (@code{%}); and the
10602 fourth means that all three parts of the static pattern rule contain
10603 pattern characters (@code{%})--only the first two parts should.
10604 @xref{Static Usage, ,Syntax of Static Pattern Rules}.
10606 @item warning: -jN forced in submake: disabling jobserver mode.
10607 This warning and the next are generated if @code{make} detects error
10608 conditions related to parallel processing on systems where
10609 sub-@code{make}s can communicate (@pxref{Options/Recursion,
10610 ,Communicating Options to a Sub-@code{make}}).  This warning is
10611 generated if a recursive invocation of a @code{make} process is forced
10612 to have @samp{-j@var{N}} in its argument list (where @var{N} is greater
10613 than one).  This could happen, for example, if you set the @code{MAKE}
10614 environment variable to @samp{make -j2}.  In this case, the
10615 sub-@code{make} doesn't communicate with other @code{make} processes and
10616 will simply pretend it has two jobs of its own.
10618 @item warning: jobserver unavailable: using -j1.  Add `+' to parent make rule.
10619 In order for @code{make} processes to communicate, the parent will pass
10620 information to the child.  Since this could result in problems if the
10621 child process isn't actually a @code{make}, the parent will only do this
10622 if it thinks the child is a @code{make}.  The parent uses the normal
10623 algorithms to determine this (@pxref{MAKE Variable, ,How the @code{MAKE}
10624 Variable Works}).  If the makefile is constructed such that the parent
10625 doesn't know the child is a @code{make} process, then the child will
10626 receive only part of the information necessary.  In this case, the child
10627 will generate this warning message and proceed with its build in a
10628 sequential manner.
10630 @end table
10632 @node Complex Makefile, GNU Free Documentation License, Error Messages, Top
10633 @appendix Complex Makefile Example
10635 Here is the makefile for the GNU @code{tar} program.  This is a
10636 moderately complex makefile.
10638 Because it is the first target, the default goal is @samp{all}.  An
10639 interesting feature of this makefile is that @file{testpad.h} is a
10640 source file automatically created by the @code{testpad} program,
10641 itself compiled from @file{testpad.c}.
10643 If you type @samp{make} or @samp{make all}, then @code{make} creates
10644 the @file{tar} executable, the @file{rmt} daemon that provides
10645 remote tape access, and the @file{tar.info} Info file.
10647 If you type @samp{make install}, then @code{make} not only creates
10648 @file{tar}, @file{rmt}, and @file{tar.info}, but also installs
10649 them.
10651 If you type @samp{make clean}, then @code{make} removes the @samp{.o}
10652 files, and the @file{tar}, @file{rmt}, @file{testpad},
10653 @file{testpad.h}, and @file{core} files.
10655 If you type @samp{make distclean}, then @code{make} not only removes
10656 the same files as does @samp{make clean} but also the
10657 @file{TAGS}, @file{Makefile}, and @file{config.status} files.
10658 (Although it is not evident, this makefile (and
10659 @file{config.status}) is generated by the user with the
10660 @code{configure} program, which is provided in the @code{tar}
10661 distribution, but is not shown here.)
10663 If you type @samp{make realclean}, then @code{make} removes the same
10664 files as does @samp{make distclean} and also removes the Info files
10665 generated from @file{tar.texinfo}.
10667 In addition, there are targets @code{shar} and @code{dist} that create
10668 distribution kits.
10670 @example
10671 @group
10672 # Generated automatically from Makefile.in by configure.
10673 # Un*x Makefile for GNU tar program.
10674 # Copyright (C) 1991 Free Software Foundation, Inc.
10675 @end group
10677 @group
10678 # This program is free software; you can redistribute
10679 # it and/or modify it under the terms of the GNU
10680 # General Public License @dots{}
10681 @dots{}
10682 @dots{}
10683 @end group
10685 SHELL = /bin/sh
10687 #### Start of system configuration section. ####
10689 srcdir = .
10691 @group
10692 # If you use gcc, you should either run the
10693 # fixincludes script that comes with it or else use
10694 # gcc with the -traditional option.  Otherwise ioctl
10695 # calls will be compiled incorrectly on some systems.
10696 CC = gcc -O
10697 YACC = bison -y
10698 INSTALL = /usr/local/bin/install -c
10699 INSTALLDATA = /usr/local/bin/install -c -m 644
10700 @end group
10702 # Things you might add to DEFS:
10703 # -DSTDC_HEADERS        If you have ANSI C headers and
10704 #                       libraries.
10705 # -DPOSIX               If you have POSIX.1 headers and
10706 #                       libraries.
10707 # -DBSD42               If you have sys/dir.h (unless
10708 #                       you use -DPOSIX), sys/file.h,
10709 #                       and st_blocks in `struct stat'.
10710 # -DUSG                 If you have System V/ANSI C
10711 #                       string and memory functions
10712 #                       and headers, sys/sysmacros.h,
10713 #                       fcntl.h, getcwd, no valloc,
10714 #                       and ndir.h (unless
10715 #                       you use -DDIRENT).
10716 # -DNO_MEMORY_H         If USG or STDC_HEADERS but do not
10717 #                       include memory.h.
10718 # -DDIRENT              If USG and you have dirent.h
10719 #                       instead of ndir.h.
10720 # -DSIGTYPE=int         If your signal handlers
10721 #                       return int, not void.
10722 # -DNO_MTIO             If you lack sys/mtio.h
10723 #                       (magtape ioctls).
10724 # -DNO_REMOTE           If you do not have a remote shell
10725 #                       or rexec.
10726 # -DUSE_REXEC           To use rexec for remote tape
10727 #                       operations instead of
10728 #                       forking rsh or remsh.
10729 # -DVPRINTF_MISSING     If you lack vprintf function
10730 #                       (but have _doprnt).
10731 # -DDOPRNT_MISSING      If you lack _doprnt function.
10732 #                       Also need to define
10733 #                       -DVPRINTF_MISSING.
10734 # -DFTIME_MISSING       If you lack ftime system call.
10735 # -DSTRSTR_MISSING      If you lack strstr function.
10736 # -DVALLOC_MISSING      If you lack valloc function.
10737 # -DMKDIR_MISSING       If you lack mkdir and
10738 #                       rmdir system calls.
10739 # -DRENAME_MISSING      If you lack rename system call.
10740 # -DFTRUNCATE_MISSING   If you lack ftruncate
10741 #                       system call.
10742 # -DV7                  On Version 7 Unix (not
10743 #                       tested in a long time).
10744 # -DEMUL_OPEN3          If you lack a 3-argument version
10745 #                       of open, and want to emulate it
10746 #                       with system calls you do have.
10747 # -DNO_OPEN3            If you lack the 3-argument open
10748 #                       and want to disable the tar -k
10749 #                       option instead of emulating open.
10750 # -DXENIX               If you have sys/inode.h
10751 #                       and need it 94 to be included.
10753 DEFS =  -DSIGTYPE=int -DDIRENT -DSTRSTR_MISSING \
10754         -DVPRINTF_MISSING -DBSD42
10755 # Set this to rtapelib.o unless you defined NO_REMOTE,
10756 # in which case make it empty.
10757 RTAPELIB = rtapelib.o
10758 LIBS =
10759 DEF_AR_FILE = /dev/rmt8
10760 DEFBLOCKING = 20
10762 @group
10763 CDEBUG = -g
10764 CFLAGS = $(CDEBUG) -I. -I$(srcdir) $(DEFS) \
10765         -DDEF_AR_FILE=\"$(DEF_AR_FILE)\" \
10766         -DDEFBLOCKING=$(DEFBLOCKING)
10767 LDFLAGS = -g
10768 @end group
10770 @group
10771 prefix = /usr/local
10772 # Prefix for each installed program,
10773 # normally empty or `g'.
10774 binprefix =
10776 # The directory to install tar in.
10777 bindir = $(prefix)/bin
10779 # The directory to install the info files in.
10780 infodir = $(prefix)/info
10781 @end group
10783 #### End of system configuration section. ####
10785 SRC1 =  tar.c create.c extract.c buffer.c \
10786         getoldopt.c update.c gnu.c mangle.c
10787 SRC2 =  version.c list.c names.c diffarch.c \
10788         port.c wildmat.c getopt.c
10789 SRC3 =  getopt1.c regex.c getdate.y
10790 SRCS =  $(SRC1) $(SRC2) $(SRC3)
10791 OBJ1 =  tar.o create.o extract.o buffer.o \
10792         getoldopt.o update.o gnu.o mangle.o
10793 OBJ2 =  version.o list.o names.o diffarch.o \
10794         port.o wildmat.o getopt.o
10795 OBJ3 =  getopt1.o regex.o getdate.o $(RTAPELIB)
10796 OBJS =  $(OBJ1) $(OBJ2) $(OBJ3)
10797 @group
10798 AUX =   README COPYING ChangeLog Makefile.in  \
10799         makefile.pc configure configure.in \
10800         tar.texinfo tar.info* texinfo.tex \
10801         tar.h port.h open3.h getopt.h regex.h \
10802         rmt.h rmt.c rtapelib.c alloca.c \
10803         msd_dir.h msd_dir.c tcexparg.c \
10804         level-0 level-1 backup-specs testpad.c
10805 @end group
10807 .PHONY: all
10808 all:    tar rmt tar.info
10810 @group
10811 .PHONY: tar
10812 tar:    $(OBJS)
10813         $(CC) $(LDFLAGS) -o $@@ $(OBJS) $(LIBS)
10814 @end group
10816 @group
10817 rmt:    rmt.c
10818         $(CC) $(CFLAGS) $(LDFLAGS) -o $@@ rmt.c
10819 @end group
10821 @group
10822 tar.info: tar.texinfo
10823         makeinfo tar.texinfo
10824 @end group
10826 @group
10827 .PHONY: install
10828 install: all
10829         $(INSTALL) tar $(bindir)/$(binprefix)tar
10830         -test ! -f rmt || $(INSTALL) rmt /etc/rmt
10831         $(INSTALLDATA) $(srcdir)/tar.info* $(infodir)
10832 @end group
10834 @group
10835 $(OBJS): tar.h port.h testpad.h
10836 regex.o buffer.o tar.o: regex.h
10837 # getdate.y has 8 shift/reduce conflicts.
10838 @end group
10840 @group
10841 testpad.h: testpad
10842         ./testpad
10843 @end group
10845 @group
10846 testpad: testpad.o
10847         $(CC) -o $@@ testpad.o
10848 @end group
10850 @group
10851 TAGS:   $(SRCS)
10852         etags $(SRCS)
10853 @end group
10855 @group
10856 .PHONY: clean
10857 clean:
10858         rm -f *.o tar rmt testpad testpad.h core
10859 @end group
10861 @group
10862 .PHONY: distclean
10863 distclean: clean
10864         rm -f TAGS Makefile config.status
10865 @end group
10867 @group
10868 .PHONY: realclean
10869 realclean: distclean
10870         rm -f tar.info*
10871 @end group
10873 @group
10874 .PHONY: shar
10875 shar: $(SRCS) $(AUX)
10876         shar $(SRCS) $(AUX) | compress \
10877           > tar-`sed -e '/version_string/!d' \
10878                      -e 's/[^0-9.]*\([0-9.]*\).*/\1/' \
10879                      -e q
10880                      version.c`.shar.Z
10881 @end group
10883 @group
10884 .PHONY: dist
10885 dist: $(SRCS) $(AUX)
10886         echo tar-`sed \
10887              -e '/version_string/!d' \
10888              -e 's/[^0-9.]*\([0-9.]*\).*/\1/' \
10889              -e q
10890              version.c` > .fname
10891         -rm -rf `cat .fname`
10892         mkdir `cat .fname`
10893         ln $(SRCS) $(AUX) `cat .fname`
10894         tar chZf `cat .fname`.tar.Z `cat .fname`
10895         -rm -rf `cat .fname` .fname
10896 @end group
10898 @group
10899 tar.zoo: $(SRCS) $(AUX)
10900         -rm -rf tmp.dir
10901         -mkdir tmp.dir
10902         -rm tar.zoo
10903         for X in $(SRCS) $(AUX) ; do \
10904             echo $$X ; \
10905             sed 's/$$/^M/' $$X \
10906             > tmp.dir/$$X ; done
10907         cd tmp.dir ; zoo aM ../tar.zoo *
10908         -rm -rf tmp.dir
10909 @end group
10910 @end example
10912 @raisesections
10913 @include fdl.texi
10914 @lowersections
10916 @node Concept Index, Name Index, GNU Free Documentation License, Top
10917 @unnumbered Index of Concepts
10919 @printindex cp
10921 @node Name Index,  , Concept Index, Top
10922 @unnumbered Index of Functions, Variables, & Directives
10924 @printindex fn
10926 @summarycontents
10927 @contents
10928 @bye