Add a new variable: MAKE_RESTARTS, to count how many times make has re-exec'd.
[make.git] / doc / make.texi
blob78872320cdaac03c41f0fa3a6e316d743940f36d
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.32 2005/06/25 18:57:28 psmith Exp $
11 @set EDITION 0.70
12 @set VERSION 3.81
13 @set UPDATED 07 May 2005
14 @set UPDATE-MONTH May 2005
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 * Shell Function::              Substitute the output of a shell command.
277 * Make Control Functions::      Functions that control how make runs.
279 How to Run @code{make}
281 * Makefile Arguments::          How to specify which makefile to use.
282 * Goals::                       How to use goal arguments to specify which
283                                   parts of the makefile to use.
284 * Instead of Execution::        How to use mode flags to specify what
285                                   kind of thing to do with the commands
286                                   in the makefile other than simply
287                                   execute them.
288 * Avoiding Compilation::        How to avoid recompiling certain files.
289 * Overriding::                  How to override a variable to specify
290                                   an alternate compiler and other things.
291 * Testing::                     How to proceed past some errors, to
292                                   test compilation.
293 * Options Summary::             Summary of Options
295 Using Implicit Rules
297 * Using Implicit::              How to use an existing implicit rule
298                                   to get the commands for updating a file.
299 * Catalogue of Rules::          A list of built-in implicit rules.
300 * Implicit Variables::          How to change what predefined rules do.
301 * Chained Rules::               How to use a chain of implicit rules.
302 * Pattern Rules::               How to define new implicit rules.
303 * Last Resort::                 How to defining commands for rules
304                                   which cannot find any.
305 * Suffix Rules::                The old-fashioned style of implicit rule.
306 * Implicit Rule Search::        The precise algorithm for applying
307                                   implicit rules.
309 Defining and Redefining Pattern Rules
311 * Pattern Intro::               An introduction to pattern rules.
312 * Pattern Examples::            Examples of pattern rules.
313 * Automatic Variables::         How to use automatic variables in the
314                                   commands of implicit rules.
315 * Pattern Match::               How patterns match.
316 * Match-Anything Rules::        Precautions you should take prior to
317                                   defining rules that can match any
318                                   target file whatever.
319 * Canceling Rules::             How to override or cancel built-in rules.
321 Using @code{make} to Update Archive Files
323 * Archive Members::             Archive members as targets.
324 * Archive Update::              The implicit rule for archive member targets.
325 * Archive Pitfalls::            Dangers to watch out for when using archives.
326 * Archive Suffix Rules::        You can write a special kind of suffix rule
327                                   for updating archives.
329 Implicit Rule for Archive Member Targets
331 * Archive Symbols::             How to update archive symbol directories.
333 Makefile Conventions
335 * Makefile Basics::             General Conventions for Makefiles
336 * Utilities in Makefiles::      Utilities in Makefiles
337 * Command Variables::           Variables for Specifying Commands
338 * Directory Variables::         Variables for Installation Directories
339 * Standard Targets::            Standard Targets for Users
340 * Install Command Categories::  Three categories of commands in the `install'
342 @end detailmenu
343 @end menu
345 @node Overview, Introduction, Top, Top
346 @comment  node-name,  next,  previous,  up
347 @chapter Overview of @code{make}
349 The @code{make} utility automatically determines which pieces of a large
350 program need to be recompiled, and issues commands to recompile them.
351 This manual describes GNU @code{make}, which was implemented by Richard
352 Stallman and Roland McGrath.  Development since Version 3.76 has been
353 handled by Paul D. Smith.
355 GNU @code{make} conforms to section 6.2 of @cite{IEEE Standard
356 1003.2-1992} (POSIX.2).
357 @cindex POSIX
358 @cindex IEEE Standard 1003.2
359 @cindex standards conformance
361 Our examples show C programs, since they are most common, but you can use
362 @code{make} with any programming language whose compiler can be run with a
363 shell command.  Indeed, @code{make} is not limited to programs.  You can
364 use it to describe any task where some files must be updated automatically
365 from others whenever the others change.
367 @menu
368 * Preparing::                   Preparing and Running Make
369 * Reading::                     On Reading this Text
370 * Bugs::                        Problems and Bugs
371 @end menu
373 @node Preparing, Reading, Overview, Overview
374 @ifnottex
375 @heading Preparing and Running Make
376 @end ifnottex
378 To prepare to use @code{make}, you must write a file called
379 the @dfn{makefile} that describes the relationships among files
380 in your program and provides commands for updating each file.
381 In a program, typically, the executable file is updated from object
382 files, which are in turn made by compiling source files.@refill
384 Once a suitable makefile exists, each time you change some source files,
385 this simple shell command:
387 @example
388 make
389 @end example
391 @noindent
392 suffices to perform all necessary recompilations.  The @code{make} program
393 uses the makefile data base and the last-modification times of the files to
394 decide which of the files need to be updated.  For each of those files, it
395 issues the commands recorded in the data base.
397 You can provide command line arguments to @code{make} to control which
398 files should be recompiled, or how.  @xref{Running, ,How to Run
399 @code{make}}.
401 @node Reading, Bugs, Preparing, Overview
402 @section How to Read This Manual
404 If you are new to @code{make}, or are looking for a general
405 introduction, read the first few sections of each chapter, skipping the
406 later sections.  In each chapter, the first few sections contain
407 introductory or general information and the later sections contain
408 specialized or technical information.
409 @ifnottex
410 The exception is the second chapter, @ref{Introduction, ,An
411 Introduction to Makefiles}, all of which is introductory.
412 @end ifnottex
413 @iftex
414 The exception is @ref{Introduction, ,An Introduction to Makefiles},
415 all of which is introductory.
416 @end iftex
418 If you are familiar with other @code{make} programs, see @ref{Features,
419 ,Features of GNU @code{make}}, which lists the enhancements GNU
420 @code{make} has, and @ref{Missing, ,Incompatibilities and Missing
421 Features}, which explains the few things GNU @code{make} lacks that
422 others have.
424 For a quick summary, see @ref{Options Summary}, @ref{Quick Reference},
425 and @ref{Special Targets}.
427 @node Bugs,  , Reading, Overview
428 @section Problems and Bugs
429 @cindex reporting bugs
430 @cindex bugs, reporting
431 @cindex problems and bugs, reporting
433 If you have problems with GNU @code{make} or think you've found a bug,
434 please report it to the developers; we cannot promise to do anything but
435 we might well want to fix it.
437 Before reporting a bug, make sure you've actually found a real bug.
438 Carefully reread the documentation and see if it really says you can do
439 what you're trying to do.  If it's not clear whether you should be able
440 to do something or not, report that too; it's a bug in the
441 documentation!
443 Before reporting a bug or trying to fix it yourself, try to isolate it
444 to the smallest possible makefile that reproduces the problem.  Then
445 send us the makefile and the exact results @code{make} gave you,
446 including any error or warning messages.  Please don't paraphrase
447 these messages: it's best to cut and paste them into your report.
448 When generating this small makefile, be sure to not use any non-free
449 or unusual tools in your commands: you can almost always emulate what
450 such a tool would do with simple shell commands.  Finally, be sure to
451 explain what you expected to occur; this will help us decide whether
452 the problem was really in the documentation.
454 Once you have a precise problem you can report it in one of two ways.
455 Either send electronic mail to:
457 @example
458     bug-make@@gnu.org
459 @end example
461 @noindent
462 or use our Web-based project management tool, at:
464 @example
465     http://savannah.gnu.org/projects/make/
466 @end example
468 @noindent
469 In addition to the information above, please be careful to include the
470 version number of @code{make} you are using.  You can get this
471 information with the command @samp{make --version}.  Be sure also to
472 include the type of machine and operating system you are using.  One
473 way to obtain this information is by looking at the final lines of
474 output from the command @samp{make --help}.
476 @node Introduction, Makefiles, Overview, Top
477 @comment  node-name,  next,  previous,  up
478 @chapter An Introduction to Makefiles
480 You need a file called a @dfn{makefile} to tell @code{make} what to do.
481 Most often, the makefile tells @code{make} how to compile and link a
482 program.
483 @cindex makefile
485 In this chapter, we will discuss a simple makefile that describes how to
486 compile and link a text editor which consists of eight C source files
487 and three header files.  The makefile can also tell @code{make} how to
488 run miscellaneous commands when explicitly asked (for example, to remove
489 certain files as a clean-up operation).  To see a more complex example
490 of a makefile, see @ref{Complex Makefile}.
492 When @code{make} recompiles the editor, each changed C source file
493 must be recompiled.  If a header file has changed, each C source file
494 that includes the header file must be recompiled to be safe.  Each
495 compilation produces an object file corresponding to the source file.
496 Finally, if any source file has been recompiled, all the object files,
497 whether newly made or saved from previous compilations, must be linked
498 together to produce the new executable editor.
499 @cindex recompilation
500 @cindex editor
502 @menu
503 * Rule Introduction::           What a rule looks like.
504 * Simple Makefile::             A Simple Makefile
505 * How Make Works::              How @code{make} Processes This Makefile
506 * Variables Simplify::          Variables Make Makefiles Simpler
507 * make Deduces::                Letting @code{make} Deduce the Commands
508 * Combine By Prerequisite::     Another Style of Makefile
509 * Cleanup::                     Rules for Cleaning the Directory
510 @end menu
512 @node Rule Introduction, Simple Makefile, Introduction, Introduction
513 @comment  node-name,  next,  previous,  up
514 @section What a Rule Looks Like
515 @cindex rule, introduction to
516 @cindex makefile rule parts
517 @cindex parts of makefile rule
519 A simple makefile consists of ``rules'' with the following shape:
521 @cindex targets, introduction to
522 @cindex prerequisites, introduction to
523 @cindex commands, introduction to
524 @example
525 @group
526 @var{target} @dots{} : @var{prerequisites} @dots{}
527         @var{command}
528         @dots{}
529         @dots{}
530 @end group
531 @end example
533 A @dfn{target} is usually the name of a file that is generated by a
534 program; examples of targets are executable or object files.  A target
535 can also be the name of an action to carry out, such as @samp{clean}
536 (@pxref{Phony Targets}).
538 A @dfn{prerequisite} is a file that is used as input to create the
539 target.  A target often depends on several files.
541 @cindex tabs in rules
542 A @dfn{command} is an action that @code{make} carries out.
543 A rule may have more than one command, each on its own line.
544 @strong{Please note:} you need to put a tab character at the beginning of
545 every command line!  This is an obscurity that catches the unwary.
547 Usually a command is in a rule with prerequisites and serves to create a
548 target file if any of the prerequisites change.  However, the rule that
549 specifies commands for the target need not have prerequisites.  For
550 example, the rule containing the delete command associated with the
551 target @samp{clean} does not have prerequisites.
553 A @dfn{rule}, then, explains how and when to remake certain files
554 which are the targets of the particular rule.  @code{make} carries out
555 the commands on the prerequisites to create or update the target.  A
556 rule can also explain how and when to carry out an action.
557 @xref{Rules, , Writing Rules}.
559 A makefile may contain other text besides rules, but a simple makefile
560 need only contain rules.  Rules may look somewhat more complicated
561 than shown in this template, but all fit the pattern more or less.
563 @node Simple Makefile, How Make Works, Rule Introduction, Introduction
564 @section A Simple Makefile
565 @cindex simple makefile
566 @cindex makefile, simple
568 Here is a straightforward makefile that describes the way an
569 executable file called @code{edit} depends on eight object files
570 which, in turn, depend on eight C source and three header files.
572 In this example, all the C files include @file{defs.h}, but only those
573 defining editing commands include @file{command.h}, and only low
574 level files that change the editor buffer include @file{buffer.h}.
576 @example
577 @group
578 edit : main.o kbd.o command.o display.o \
579        insert.o search.o files.o utils.o
580         cc -o edit main.o kbd.o command.o display.o \
581                    insert.o search.o files.o utils.o
583 main.o : main.c defs.h
584         cc -c main.c
585 kbd.o : kbd.c defs.h command.h
586         cc -c kbd.c
587 command.o : command.c defs.h command.h
588         cc -c command.c
589 display.o : display.c defs.h buffer.h
590         cc -c display.c
591 insert.o : insert.c defs.h buffer.h
592         cc -c insert.c
593 search.o : search.c defs.h buffer.h
594         cc -c search.c
595 files.o : files.c defs.h buffer.h command.h
596         cc -c files.c
597 utils.o : utils.c defs.h
598         cc -c utils.c
599 clean :
600         rm edit main.o kbd.o command.o display.o \
601            insert.o search.o files.o utils.o
602 @end group
603 @end example
605 @noindent
606 We split each long line into two lines using backslash-newline; this is
607 like using one long line, but is easier to read.
608 @cindex continuation lines
609 @cindex @code{\} (backslash), for continuation lines
610 @cindex backslash (@code{\}), for continuation lines
611 @cindex quoting newline, in makefile
612 @cindex newline, quoting, in makefile
614 To use this makefile to create the executable file called @file{edit},
615 type:
617 @example
618 make
619 @end example
621 To use this makefile to delete the executable file and all the object
622 files from the directory, type:
624 @example
625 make clean
626 @end example
628 In the example makefile, the targets include the executable file
629 @samp{edit}, and the object files @samp{main.o} and @samp{kbd.o}.  The
630 prerequisites are files such as @samp{main.c} and @samp{defs.h}.
631 In fact, each @samp{.o} file is both a target and a prerequisite.
632 Commands include @w{@samp{cc -c main.c}} and @w{@samp{cc -c kbd.c}}.
634 When a target is a file, it needs to be recompiled or relinked if any
635 of its prerequisites change.  In addition, any prerequisites that are
636 themselves automatically generated should be updated first.  In this
637 example, @file{edit} depends on each of the eight object files; the
638 object file @file{main.o} depends on the source file @file{main.c} and
639 on the header file @file{defs.h}.
641 A shell command follows each line that contains a target and
642 prerequisites.  These shell commands say how to update the target file.
643 A tab character must come at the beginning of every command line to
644 distinguish command lines from other lines in the makefile.  (Bear in
645 mind that @code{make} does not know anything about how the commands
646 work.  It is up to you to supply commands that will update the target
647 file properly.  All @code{make} does is execute the commands in the rule
648 you have specified when the target file needs to be updated.)
649 @cindex shell command
651 The target @samp{clean} is not a file, but merely the name of an
652 action.  Since you
653 normally
654 do not want to carry out the actions in this rule, @samp{clean} is not a prerequisite of any other rule.
655 Consequently, @code{make} never does anything with it unless you tell
656 it specifically.  Note that this rule not only is not a prerequisite, it
657 also does not have any prerequisites, so the only purpose of the rule
658 is to run the specified commands.  Targets that do not refer to files
659 but are just actions are called @dfn{phony targets}.  @xref{Phony
660 Targets}, for information about this kind of target.  @xref{Errors, ,
661 Errors in Commands}, to see how to cause @code{make} to ignore errors
662 from @code{rm} or any other command.
663 @cindex @code{clean} target
664 @cindex @code{rm} (shell command)
666 @node How Make Works, Variables Simplify, Simple Makefile, Introduction
667 @comment  node-name,  next,  previous,  up
668 @section How @code{make} Processes a Makefile
669 @cindex processing a makefile
670 @cindex makefile, how @code{make} processes
672 By default, @code{make} starts with the first target (not targets whose
673 names start with @samp{.}).  This is called the @dfn{default goal}.
674 (@dfn{Goals} are the targets that @code{make} strives ultimately to
675 update.    You can override this behavior using the command line
676 (@pxref{Goals, , Arguments to Specify the Goals}) or with the
677 @code{.DEFAULT_GOAL} special variable (@pxref{Special Variables, ,
678 Other Special Variables}).
679 @cindex default goal
680 @cindex goal, default
681 @cindex goal
683 In the simple example of the previous section, the default goal is to
684 update the executable program @file{edit}; therefore, we put that rule
685 first.
687 Thus, when you give the command:
689 @example
690 make
691 @end example
693 @noindent
694 @code{make} reads the makefile in the current directory and begins by
695 processing the first rule.  In the example, this rule is for relinking
696 @file{edit}; but before @code{make} can fully process this rule, it
697 must process the rules for the files that @file{edit} depends on,
698 which in this case are the object files.  Each of these files is
699 processed according to its own rule.  These rules say to update each
700 @samp{.o} file by compiling its source file.  The recompilation must
701 be done if the source file, or any of the header files named as
702 prerequisites, is more recent than the object file, or if the object
703 file does not exist.
705 The other rules are processed because their targets appear as
706 prerequisites of the goal.  If some other rule is not depended on by the
707 goal (or anything it depends on, etc.), that rule is not processed,
708 unless you tell @code{make} to do so (with a command such as
709 @w{@code{make clean}}).
711 Before recompiling an object file, @code{make} considers updating its
712 prerequisites, the source file and header files.  This makefile does not
713 specify anything to be done for them---the @samp{.c} and @samp{.h} files
714 are not the targets of any rules---so @code{make} does nothing for these
715 files.  But @code{make} would update automatically generated C programs,
716 such as those made by Bison or Yacc, by their own rules at this time.
718 After recompiling whichever object files need it, @code{make} decides
719 whether to relink @file{edit}.  This must be done if the file
720 @file{edit} does not exist, or if any of the object files are newer than
721 it.  If an object file was just recompiled, it is now newer than
722 @file{edit}, so @file{edit} is relinked.
723 @cindex relinking
725 Thus, if we change the file @file{insert.c} and run @code{make},
726 @code{make} will compile that file to update @file{insert.o}, and then
727 link @file{edit}.  If we change the file @file{command.h} and run
728 @code{make}, @code{make} will recompile the object files @file{kbd.o},
729 @file{command.o} and @file{files.o} and then link the file @file{edit}.
731 @node Variables Simplify, make Deduces, How Make Works, Introduction
732 @section Variables Make Makefiles Simpler
733 @cindex variables
734 @cindex simplifying with variables
736 In our example, we had to list all the object files twice in the rule for
737 @file{edit} (repeated here):
739 @example
740 @group
741 edit : main.o kbd.o command.o display.o \
742               insert.o search.o files.o utils.o
743         cc -o edit main.o kbd.o command.o display.o \
744                    insert.o search.o files.o utils.o
745 @end group
746 @end example
748 @cindex @code{objects}
749 Such duplication is error-prone; if a new object file is added to the
750 system, we might add it to one list and forget the other.  We can eliminate
751 the risk and simplify the makefile by using a variable.  @dfn{Variables}
752 allow a text string to be defined once and substituted in multiple places
753 later (@pxref{Using Variables, ,How to Use Variables}).
755 @cindex @code{OBJECTS}
756 @cindex @code{objs}
757 @cindex @code{OBJS}
758 @cindex @code{obj}
759 @cindex @code{OBJ}
760 It is standard practice for every makefile to have a variable named
761 @code{objects}, @code{OBJECTS}, @code{objs}, @code{OBJS}, @code{obj},
762 or @code{OBJ} which is a list of all object file names.  We would
763 define such a variable @code{objects} with a line like this in the
764 makefile:@refill
766 @example
767 @group
768 objects = main.o kbd.o command.o display.o \
769           insert.o search.o files.o utils.o
770 @end group
771 @end example
773 @noindent
774 Then, each place we want to put a list of the object file names, we can
775 substitute the variable's value by writing @samp{$(objects)}
776 (@pxref{Using Variables, ,How to Use Variables}).
778 Here is how the complete simple makefile looks when you use a variable
779 for the object files:
781 @example
782 @group
783 objects = main.o kbd.o command.o display.o \
784           insert.o search.o files.o utils.o
786 edit : $(objects)
787         cc -o edit $(objects)
788 main.o : main.c defs.h
789         cc -c main.c
790 kbd.o : kbd.c defs.h command.h
791         cc -c kbd.c
792 command.o : command.c defs.h command.h
793         cc -c command.c
794 display.o : display.c defs.h buffer.h
795         cc -c display.c
796 insert.o : insert.c defs.h buffer.h
797         cc -c insert.c
798 search.o : search.c defs.h buffer.h
799         cc -c search.c
800 files.o : files.c defs.h buffer.h command.h
801         cc -c files.c
802 utils.o : utils.c defs.h
803         cc -c utils.c
804 clean :
805         rm edit $(objects)
806 @end group
807 @end example
809 @node make Deduces, Combine By Prerequisite, Variables Simplify, Introduction
810 @section Letting @code{make} Deduce the Commands
811 @cindex deducing commands (implicit rules)
812 @cindex implicit rule, introduction to
813 @cindex rule, implicit, introduction to
815 It is not necessary to spell out the commands for compiling the individual
816 C source files, because @code{make} can figure them out: it has an
817 @dfn{implicit rule} for updating a @samp{.o} file from a correspondingly
818 named @samp{.c} file using a @samp{cc -c} command.  For example, it will
819 use the command @samp{cc -c main.c -o main.o} to compile @file{main.c} into
820 @file{main.o}.  We can therefore omit the commands from the rules for the
821 object files.  @xref{Implicit Rules, ,Using Implicit Rules}.@refill
823 When a @samp{.c} file is used automatically in this way, it is also
824 automatically added to the list of prerequisites.  We can therefore omit
825 the @samp{.c} files from the prerequisites, provided we omit the commands.
827 Here is the entire example, with both of these changes, and a variable
828 @code{objects} as suggested above:
830 @example
831 @group
832 objects = main.o kbd.o command.o display.o \
833           insert.o search.o files.o utils.o
835 edit : $(objects)
836         cc -o edit $(objects)
838 main.o : defs.h
839 kbd.o : defs.h command.h
840 command.o : defs.h command.h
841 display.o : defs.h buffer.h
842 insert.o : defs.h buffer.h
843 search.o : defs.h buffer.h
844 files.o : defs.h buffer.h command.h
845 utils.o : defs.h
847 .PHONY : clean
848 clean :
849         rm edit $(objects)
850 @end group
851 @end example
853 @noindent
854 This is how we would write the makefile in actual practice.  (The
855 complications associated with @samp{clean} are described elsewhere.
856 See @ref{Phony Targets}, and @ref{Errors, ,Errors in Commands}.)
858 Because implicit rules are so convenient, they are important.  You
859 will see them used frequently.@refill
861 @node Combine By Prerequisite, Cleanup, make Deduces, Introduction
862 @section Another Style of Makefile
863 @cindex combining rules by prerequisite
865 When the objects of a makefile are created only by implicit rules, an
866 alternative style of makefile is possible.  In this style of makefile,
867 you group entries by their prerequisites instead of by their targets.
868 Here is what one looks like:
870 @example
871 @group
872 objects = main.o kbd.o command.o display.o \
873           insert.o search.o files.o utils.o
875 edit : $(objects)
876         cc -o edit $(objects)
878 $(objects) : defs.h
879 kbd.o command.o files.o : command.h
880 display.o insert.o search.o files.o : buffer.h
881 @end group
882 @end example
884 @noindent
885 Here @file{defs.h} is given as a prerequisite of all the object files;
886 @file{command.h} and @file{buffer.h} are prerequisites of the specific
887 object files listed for them.
889 Whether this is better is a matter of taste: it is more compact, but some
890 people dislike it because they find it clearer to put all the information
891 about each target in one place.
893 @node Cleanup,  , Combine By Prerequisite, Introduction
894 @section Rules for Cleaning the Directory
895 @cindex cleaning up
896 @cindex removing, to clean up
898 Compiling a program is not the only thing you might want to write rules
899 for.  Makefiles commonly tell how to do a few other things besides
900 compiling a program: for example, how to delete all the object files
901 and executables so that the directory is @samp{clean}.
903 @cindex @code{clean} target
904 Here is how we
905 could write a @code{make} rule for cleaning our example editor:
907 @example
908 @group
909 clean:
910         rm edit $(objects)
911 @end group
912 @end example
914 In practice, we might want to write the rule in a somewhat more
915 complicated manner to handle unanticipated situations.  We would do this:
917 @example
918 @group
919 .PHONY : clean
920 clean :
921         -rm edit $(objects)
922 @end group
923 @end example
925 @noindent
926 This prevents @code{make} from getting confused by an actual file
927 called @file{clean} and causes it to continue in spite of errors from
928 @code{rm}.  (See @ref{Phony Targets}, and @ref{Errors, ,Errors in
929 Commands}.)
931 @noindent
932 A rule such as this should not be placed at the beginning of the
933 makefile, because we do not want it to run by default!  Thus, in the
934 example makefile, we want the rule for @code{edit}, which recompiles
935 the editor, to remain the default goal.
937 Since @code{clean} is not a prerequisite of @code{edit}, this rule will not
938 run at all if we give the command @samp{make} with no arguments.  In
939 order to make the rule run, we have to type @samp{make clean}.
940 @xref{Running, ,How to Run @code{make}}.
942 @node Makefiles, Rules, Introduction, Top
943 @chapter Writing Makefiles
945 @cindex makefile, how to write
946 The information that tells @code{make} how to recompile a system comes from
947 reading a data base called the @dfn{makefile}.
949 @menu
950 * Makefile Contents::           What makefiles contain.
951 * Makefile Names::              How to name your makefile.
952 * Include::                     How one makefile can use another makefile.
953 * MAKEFILES Variable::          The environment can specify extra makefiles.
954 * MAKEFILE_LIST Variable::      Discover which makefiles have been read.
955 * Special Variables::           Other special variables.
956 * Remaking Makefiles::          How makefiles get remade.
957 * Overriding Makefiles::        How to override part of one makefile
958                                   with another makefile.
959 * Reading Makefiles::           How makefiles are parsed.
960 * Secondary Expansion::         How and when secondary expansion is performed.
961 @end menu
963 @node Makefile Contents, Makefile Names, Makefiles, Makefiles
964 @section What Makefiles Contain
966 Makefiles contain five kinds of things: @dfn{explicit rules},
967 @dfn{implicit rules}, @dfn{variable definitions}, @dfn{directives},
968 and @dfn{comments}.  Rules, variables, and directives are described at
969 length in later chapters.@refill
971 @itemize @bullet
972 @cindex rule, explicit, definition of
973 @cindex explicit rule, definition of
974 @item
975 An @dfn{explicit rule} says when and how to remake one or more files,
976 called the rule's @dfn{targets}.  It lists the other files that the
977 targets depend on, called the @dfn{prerequisites} of the target, and
978 may also give commands to use to create or update the targets.
979 @xref{Rules, ,Writing Rules}.
981 @cindex rule, implicit, definition of
982 @cindex implicit rule, definition of
983 @item
984 An @dfn{implicit rule} says when and how to remake a class of files
985 based on their names.  It describes how a target may depend on a file
986 with a name similar to the target and gives commands to create or
987 update such a target.  @xref{Implicit Rules, ,Using Implicit Rules}.
989 @cindex variable definition
990 @item
991 A @dfn{variable definition} is a line that specifies a text string
992 value for a variable that can be substituted into the text later.  The
993 simple makefile example shows a variable definition for @code{objects}
994 as a list of all object files (@pxref{Variables Simplify, , Variables
995 Make Makefiles Simpler}).
997 @cindex directive
998 @item
999 A @dfn{directive} is a command for @code{make} to do something special while
1000 reading the makefile.  These include:
1002 @itemize @bullet
1003 @item
1004 Reading another makefile (@pxref{Include, ,Including Other Makefiles}).
1006 @item
1007 Deciding (based on the values of variables) whether to use or
1008 ignore a part of the makefile (@pxref{Conditionals, ,Conditional Parts of Makefiles}).
1010 @item
1011 Defining a variable from a verbatim string containing multiple lines
1012 (@pxref{Defining, ,Defining Variables Verbatim}).
1013 @end itemize
1015 @cindex comments, in makefile
1016 @cindex @code{#} (comments), in makefile
1017 @item
1018 @samp{#} in a line of a makefile starts a @dfn{comment}.  It and the
1019 rest of the line are ignored, except that a trailing backslash not
1020 escaped by another backslash will continue the comment across multiple
1021 lines.  A line containing just a comment (with perhaps spaces before
1022 it) is effectively blank, and is ignored.  If you want a literal
1023 @code{#}, escape it with a backslash (e.g., @code{\#}).  Comments may
1024 appear on any line in the makefile, although they are treated
1025 specially in certain situations.
1027 Within a command script (if the line begins with a TAB character) the
1028 entire line is passed to the shell, just as with any other line that
1029 begins with a TAB.  The shell decides how to interpret the text:
1030 whether or not this is a comment is up to the shell.
1032 Within a @code{define} directive, comments are not ignored during the
1033 definition of the variable, but rather kept intact in the value of the
1034 variable.  When the variable is expanded they will either be treated
1035 as @code{make} comments or as command script text, depending on the
1036 context in which the variable is evaluated.
1037 @end itemize
1039 @node Makefile Names, Include, Makefile Contents, Makefiles
1040 @section What Name to Give Your Makefile
1041 @cindex makefile name
1042 @cindex name of makefile
1043 @cindex default makefile name
1044 @cindex file name of makefile
1046 @c following paragraph rewritten to avoid overfull hbox
1047 By default, when @code{make} looks for the makefile, it tries the
1048 following names, in order: @file{GNUmakefile}, @file{makefile}
1049 and @file{Makefile}.@refill
1050 @findex Makefile
1051 @findex GNUmakefile
1052 @findex makefile
1054 @cindex @code{README}
1055 Normally you should call your makefile either @file{makefile} or
1056 @file{Makefile}.  (We recommend @file{Makefile} because it appears
1057 prominently near the beginning of a directory listing, right near other
1058 important files such as @file{README}.)  The first name checked,
1059 @file{GNUmakefile}, is not recommended for most makefiles.  You should
1060 use this name if you have a makefile that is specific to GNU
1061 @code{make}, and will not be understood by other versions of
1062 @code{make}.  Other @code{make} programs look for @file{makefile} and
1063 @file{Makefile}, but not @file{GNUmakefile}.
1065 If @code{make} finds none of these names, it does not use any makefile.
1066 Then you must specify a goal with a command argument, and @code{make}
1067 will attempt to figure out how to remake it using only its built-in
1068 implicit rules.  @xref{Implicit Rules, ,Using Implicit Rules}.
1070 @cindex @code{-f}
1071 @cindex @code{--file}
1072 @cindex @code{--makefile}
1073 If you want to use a nonstandard name for your makefile, you can specify
1074 the makefile name with the @samp{-f} or @samp{--file} option.  The
1075 arguments @w{@samp{-f @var{name}}} or @w{@samp{--file=@var{name}}} tell
1076 @code{make} to read the file @var{name} as the makefile.  If you use
1077 more than one @samp{-f} or @samp{--file} option, you can specify several
1078 makefiles.  All the makefiles are effectively concatenated in the order
1079 specified.  The default makefile names @file{GNUmakefile},
1080 @file{makefile} and @file{Makefile} are not checked automatically if you
1081 specify @samp{-f} or @samp{--file}.@refill
1082 @cindex specifying makefile name
1083 @cindex makefile name, how to specify
1084 @cindex name of makefile, how to specify
1085 @cindex file name of makefile, how to specify
1087 @node Include, MAKEFILES Variable, Makefile Names, Makefiles
1088 @section Including Other Makefiles
1089 @cindex including other makefiles
1090 @cindex makefile, including
1092 @findex include
1093 The @code{include} directive tells @code{make} to suspend reading the
1094 current makefile and read one or more other makefiles before continuing.
1095 The directive is a line in the makefile that looks like this:
1097 @example
1098 include @var{filenames}@dots{}
1099 @end example
1101 @noindent
1102 @var{filenames} can contain shell file name patterns.
1103 @cindex shell file name pattern (in @code{include})
1104 @cindex shell wildcards (in @code{include})
1105 @cindex wildcard, in @code{include}
1107 Extra spaces are allowed and ignored at the beginning of the line, but
1108 a tab is not allowed.  (If the line begins with a tab, it will be
1109 considered a command line.)  Whitespace is required between
1110 @code{include} and the file names, and between file names; extra
1111 whitespace is ignored there and at the end of the directive.  A
1112 comment starting with @samp{#} is allowed at the end of the line.  If
1113 the file names contain any variable or function references, they are
1114 expanded.  @xref{Using Variables, ,How to Use Variables}.
1116 For example, if you have three @file{.mk} files, @file{a.mk},
1117 @file{b.mk}, and @file{c.mk}, and @code{$(bar)} expands to
1118 @code{bish bash}, then the following expression
1120 @example
1121 include foo *.mk $(bar)
1122 @end example
1124 is equivalent to
1126 @example
1127 include foo a.mk b.mk c.mk bish bash
1128 @end example
1130 When @code{make} processes an @code{include} directive, it suspends
1131 reading of the containing makefile and reads from each listed file in
1132 turn.  When that is finished, @code{make} resumes reading the
1133 makefile in which the directive appears.
1135 One occasion for using @code{include} directives is when several programs,
1136 handled by individual makefiles in various directories, need to use a
1137 common set of variable definitions
1138 (@pxref{Setting, ,Setting Variables}) or pattern rules
1139 (@pxref{Pattern Rules, ,Defining and Redefining Pattern Rules}).
1141 Another such occasion is when you want to generate prerequisites from
1142 source files automatically; the prerequisites can be put in a file that
1143 is included by the main makefile.  This practice is generally cleaner
1144 than that of somehow appending the prerequisites to the end of the main
1145 makefile as has been traditionally done with other versions of
1146 @code{make}.  @xref{Automatic Prerequisites}.
1147 @cindex prerequisites, automatic generation
1148 @cindex automatic generation of prerequisites
1149 @cindex generating prerequisites automatically
1151 @cindex @code{-I}
1152 @cindex @code{--include-dir}
1153 @cindex included makefiles, default directories
1154 @cindex default directories for included makefiles
1155 @findex /usr/gnu/include
1156 @findex /usr/local/include
1157 @findex /usr/include
1158 If the specified name does not start with a slash, and the file is not
1159 found in the current directory, several other directories are searched.
1160 First, any directories you have specified with the @samp{-I} or
1161 @samp{--include-dir} option are searched
1162 (@pxref{Options Summary, ,Summary of Options}).
1163 Then the following directories (if they exist)
1164 are searched, in this order:
1165 @file{@var{prefix}/include} (normally @file{/usr/local/include}
1166 @footnote{GNU Make compiled for MS-DOS and MS-Windows behaves as if
1167 @var{prefix} has been defined to be the root of the DJGPP tree
1168 hierarchy.})
1169 @file{/usr/gnu/include},
1170 @file{/usr/local/include}, @file{/usr/include}.
1172 If an included makefile cannot be found in any of these directories, a
1173 warning message is generated, but it is not an immediately fatal error;
1174 processing of the makefile containing the @code{include} continues.
1175 Once it has finished reading makefiles, @code{make} will try to remake
1176 any that are out of date or don't exist.
1177 @xref{Remaking Makefiles, ,How Makefiles Are Remade}.
1178 Only after it has tried to find a way to remake a makefile and failed,
1179 will @code{make} diagnose the missing makefile as a fatal error.
1181 If you want @code{make} to simply ignore a makefile which does not exist
1182 and cannot be remade, with no error message, use the @w{@code{-include}}
1183 directive instead of @code{include}, like this:
1185 @example
1186 -include @var{filenames}@dots{}
1187 @end example
1189 This acts like @code{include} in every way except that there is no
1190 error (not even a warning) if any of the @var{filenames} do not exist.
1191 For compatibility with some other @code{make} implementations,
1192 @code{sinclude} is another name for @w{@code{-include}}.
1194 @node MAKEFILES Variable, MAKEFILE_LIST Variable, Include, Makefiles
1195 @section The Variable @code{MAKEFILES}
1196 @cindex makefile, and @code{MAKEFILES} variable
1197 @cindex including (@code{MAKEFILES} variable)
1199 @vindex MAKEFILES
1200 If the environment variable @code{MAKEFILES} is defined, @code{make}
1201 considers its value as a list of names (separated by whitespace) of
1202 additional makefiles to be read before the others.  This works much like
1203 the @code{include} directive: various directories are searched for those
1204 files (@pxref{Include, ,Including Other Makefiles}).  In addition, the
1205 default goal is never taken from one of these makefiles and it is not an
1206 error if the files listed in @code{MAKEFILES} are not found.@refill
1208 @cindex recursion, and @code{MAKEFILES} variable
1209 The main use of @code{MAKEFILES} is in communication between recursive
1210 invocations of @code{make} (@pxref{Recursion, ,Recursive Use of
1211 @code{make}}).  It usually is not desirable to set the environment
1212 variable before a top-level invocation of @code{make}, because it is
1213 usually better not to mess with a makefile from outside.  However, if
1214 you are running @code{make} without a specific makefile, a makefile in
1215 @code{MAKEFILES} can do useful things to help the built-in implicit
1216 rules work better, such as defining search paths (@pxref{Directory Search}).
1218 Some users are tempted to set @code{MAKEFILES} in the environment
1219 automatically on login, and program makefiles to expect this to be done.
1220 This is a very bad idea, because such makefiles will fail to work if run by
1221 anyone else.  It is much better to write explicit @code{include} directives
1222 in the makefiles.  @xref{Include, , Including Other Makefiles}.
1224 @node MAKEFILE_LIST Variable, Special Variables, MAKEFILES Variable, Makefiles
1225 @comment  node-name,  next,  previous,  up
1226 @section The Variable @code{MAKEFILE_LIST}
1227 @cindex makefiles, and @code{MAKEFILE_LIST} variable
1228 @cindex including (@code{MAKEFILE_LIST} variable)
1229 @vindex MAKEFILE_LIST
1231 As @code{make} reads various makefiles, including any obtained from the
1232 @code{MAKEFILES} variable, the command line, the default files, or
1233 from @code{include} directives, their names will be automatically
1234 appended to the @code{MAKEFILE_LIST} variable.  They are added right
1235 before @code{make} begins to parse them.
1237 This means that if the first thing a makefile does is examine the last
1238 word in this variable, it will be the name of the current makefile.
1239 Once the current makefile has used @code{include}, however, the last
1240 word will be the just-included makefile.
1242 If a makefile named @code{Makefile} has this content:
1244 @example
1245 @group
1246 name1 := $(lastword $(MAKEFILE_LIST))
1248 include inc.mk
1250 name2 := $(lastword $(MAKEFILE_LIST))
1252 all:
1253         @@echo name1 = $(name1)
1254         @@echo name2 = $(name2)
1255 @end group
1256 @end example
1258 @noindent
1259 then you would expect to see this output:
1261 @example
1262 @group
1263 name1 = Makefile
1264 name2 = inc.mk
1265 @end group
1266 @end example
1268 @xref{Text Functions}, for more information on the @code{word} and
1269 @code{words} functions used above.  @xref{Flavors, The Two Flavors of
1270 Variables}, for more information on simply-expanded (@code{:=})
1271 variable definitions.
1273 @node Special Variables, Remaking Makefiles, MAKEFILE_LIST Variable, Makefiles
1274 @comment  node-name,  next,  previous,  up
1275 @section Other Special Variables
1276 @cindex makefiles, and special variables
1277 @cindex special variables
1279 GNU @code{make} also supports other special variables.  Unless
1280 otherwise documented here, these values lose their special properties
1281 if they are set by a makefile or on the command line.
1283 @table @code
1285 @vindex .DEFAULT_GOAL @r{(define default goal)}
1286 @item .DEFAULT_GOAL
1287 Sets the default goal to be used if no targets were specified on the
1288 command line (@pxref{Goals, , Arguments to Specify the Goals}).  The
1289 @code{.DEFAULT_GOAL} variable allows you to discover the current
1290 default goal, restart the default goal selection algorithm by clearing
1291 its value, or to explicitly set the default goal. The following
1292 example illustrates these cases:
1294 @example
1295 @group
1296 # Query the default goal.
1297 ifeq ($(.DEFAULT_GOAL),)
1298   $(warning no default goal is set)
1299 endif
1301 .PHONY: foo
1302 foo: ; @@echo $@@
1304 $(warning default goal is $(.DEFAULT_GOAL))
1306 # Reset the default goal.
1307 .DEFAULT_GOAL :=
1309 .PHONY: bar
1310 bar: ; @@echo $@@
1312 $(warning default goal is $(.DEFAULT_GOAL))
1314 # Set our own.
1315 .DEFAULT_GOAL := foo
1316 @end group
1317 @end example
1319 This makefile prints:
1321 @example
1322 @group
1323 no default goal is set
1324 default goal is foo
1325 default goal is bar
1327 @end group
1328 @end example
1330 Note that assigning more than one target name to @code{.DEFAULT_GOAL} is
1331 illegal and will result in an error.
1333 @vindex MAKE_RESTARTS @r{(number of times @code{make} has restarted)}
1334 @item MAKE_RESTARTS
1335 This variable is set only if this instance of @code{make} has
1336 restarted (@pxref{Remaking Makefiles, , How Makefiles Are Remade}): it
1337 will contain the number of times this instance has restarted.  Note
1338 this is not the same as recursion (counted by the @code{MAKELEVEL}
1339 variable).  You should not set, modify, or export this variable.
1341 @vindex .VARIABLES @r{(list of variables)}
1342 @item .VARIABLES
1343 Expands to a list of the @emph{names} of all global variables defined
1344 so far.  This includes variables which have empty values, as well as
1345 built-in variables (@pxref{Implicit Variables, , Variables Used by
1346 Implicit Rules}), but does not include any variables which are only
1347 defined in a target-specific context.  Note that any value you assign
1348 to this variable will be ignored; it will always return its special
1349 value.
1351 @c @vindex .TARGETS @r{(list of targets)}
1352 @c @item .TARGETS
1353 @c The second special variable is @code{.TARGETS}.  When expanded, the
1354 @c value consists of a list of all targets defined in all makefiles read
1355 @c up until that point.  Note it's not enough for a file to be simply
1356 @c mentioned in the makefile to be listed in this variable, even if it
1357 @c would match an implicit rule and become an ``implicit target''.  The
1358 @c file must appear as a target, on the left-hand side of a ``:'', to be
1359 @c considered a target for the purposes of this variable.
1361 @vindex .FEATURES @r{(list of supported features)}
1362 @item .FEATURES
1363 Expands to a list of special features supported by this version of
1364 @code{make}.  Possible values include:
1366 @table @samp
1367 @item target-specific
1368 Supports target-specific and pattern-specific variable assignments.
1369 @xref{Target-specific, ,Target-specific Variable Values}.
1371 @item order-only
1372 Supports order-only prerequisites.  @xref{Prerequisite Types, ,Types
1373 of Prerequisites}.
1375 @item second-expansion
1376 Supports secondary expansion of prerequisite lists.
1378 @item jobserver
1379 Supports ``job server'' enhanced parallel builds.  @xref{Parallel,
1380 ,Parallel Execution}.
1382 @item check-symlink
1383 Supports the @code{-L} (@code{--check-symlink-times}) flag.
1384 @xref{Options Summary, ,Summary of Options}.
1386 @end table
1388 @end table
1390 @node Remaking Makefiles, Overriding Makefiles, Special Variables, Makefiles
1391 @section How Makefiles Are Remade
1393 @cindex updating makefiles
1394 @cindex remaking makefiles
1395 @cindex makefile, remaking of
1396 Sometimes makefiles can be remade from other files, such as RCS or SCCS
1397 files.  If a makefile can be remade from other files, you probably want
1398 @code{make} to get an up-to-date version of the makefile to read in.
1400 To this end, after reading in all makefiles, @code{make} will consider
1401 each as a goal target and attempt to update it.  If a makefile has a
1402 rule which says how to update it (found either in that very makefile or
1403 in another one) or if an implicit rule applies to it (@pxref{Implicit
1404 Rules, ,Using Implicit Rules}), it will be updated if necessary.  After
1405 all makefiles have been checked, if any have actually been changed,
1406 @code{make} starts with a clean slate and reads all the makefiles over
1407 again.  (It will also attempt to update each of them over again, but
1408 normally this will not change them again, since they are already up to
1409 date.)@refill
1411 If you know that one or more of your makefiles cannot be remade and you
1412 want to keep @code{make} from performing an implicit rule search on
1413 them, perhaps for efficiency reasons, you can use any normal method of
1414 preventing implicit rule lookup to do so.  For example, you can write an
1415 explicit rule with the makefile as the target, and an empty command
1416 string (@pxref{Empty Commands, ,Using Empty Commands}).
1418 If the makefiles specify a double-colon rule to remake a file with
1419 commands but no prerequisites, that file will always be remade
1420 (@pxref{Double-Colon}).  In the case of makefiles, a makefile that has a
1421 double-colon rule with commands but no prerequisites will be remade every
1422 time @code{make} is run, and then again after @code{make} starts over
1423 and reads the makefiles in again.  This would cause an infinite loop:
1424 @code{make} would constantly remake the makefile, and never do anything
1425 else.  So, to avoid this, @code{make} will @strong{not} attempt to
1426 remake makefiles which are specified as targets of a double-colon rule
1427 with commands but no prerequisites.@refill
1429 If you do not specify any makefiles to be read with @samp{-f} or
1430 @samp{--file} options, @code{make} will try the default makefile names;
1431 @pxref{Makefile Names, ,What Name to Give Your Makefile}.  Unlike
1432 makefiles explicitly requested with @samp{-f} or @samp{--file} options,
1433 @code{make} is not certain that these makefiles should exist.  However,
1434 if a default makefile does not exist but can be created by running
1435 @code{make} rules, you probably want the rules to be run so that the
1436 makefile can be used.
1438 Therefore, if none of the default makefiles exists, @code{make} will try
1439 to make each of them in the same order in which they are searched for
1440 (@pxref{Makefile Names, ,What Name to Give Your Makefile})
1441 until it succeeds in making one, or it runs out of names to try.  Note
1442 that it is not an error if @code{make} cannot find or make any makefile;
1443 a makefile is not always necessary.@refill
1445 When you use the @samp{-t} or @samp{--touch} option
1446 (@pxref{Instead of Execution, ,Instead of Executing the Commands}),
1447 you would not want to use an out-of-date makefile to decide which
1448 targets to touch.  So the @samp{-t} option has no effect on updating
1449 makefiles; they are really updated even if @samp{-t} is specified.
1450 Likewise, @samp{-q} (or @samp{--question}) and @samp{-n} (or
1451 @samp{--just-print}) do not prevent updating of makefiles, because an
1452 out-of-date makefile would result in the wrong output for other targets.
1453 Thus, @samp{make -f mfile -n foo} will update @file{mfile}, read it in,
1454 and then print the commands to update @file{foo} and its prerequisites
1455 without running them.  The commands printed for @file{foo} will be those
1456 specified in the updated contents of @file{mfile}.
1458 However, on occasion you might actually wish to prevent updating of even
1459 the makefiles.  You can do this by specifying the makefiles as goals in
1460 the command line as well as specifying them as makefiles.  When the
1461 makefile name is specified explicitly as a goal, the options @samp{-t}
1462 and so on do apply to them.
1464 Thus, @samp{make -f mfile -n mfile foo} would read the makefile
1465 @file{mfile}, print the commands needed to update it without actually
1466 running them, and then print the commands needed to update @file{foo}
1467 without running them.  The commands for @file{foo} will be those
1468 specified by the existing contents of @file{mfile}.
1470 @node Overriding Makefiles, Reading Makefiles, Remaking Makefiles, Makefiles
1471 @section Overriding Part of Another Makefile
1473 @cindex overriding makefiles
1474 @cindex makefile, overriding
1475 Sometimes it is useful to have a makefile that is mostly just like
1476 another makefile.  You can often use the @samp{include} directive to
1477 include one in the other, and add more targets or variable definitions.
1478 However, if the two makefiles give different commands for the same
1479 target, @code{make} will not let you just do this.  But there is another way.
1481 @cindex match-anything rule, used to override
1482 In the containing makefile (the one that wants to include the other),
1483 you can use a match-anything pattern rule to say that to remake any
1484 target that cannot be made from the information in the containing
1485 makefile, @code{make} should look in another makefile.
1486 @xref{Pattern Rules}, for more information on pattern rules.
1488 For example, if you have a makefile called @file{Makefile} that says how
1489 to make the target @samp{foo} (and other targets), you can write a
1490 makefile called @file{GNUmakefile} that contains:
1492 @example
1493 foo:
1494         frobnicate > foo
1496 %: force
1497         @@$(MAKE) -f Makefile $@@
1498 force: ;
1499 @end example
1501 If you say @samp{make foo}, @code{make} will find @file{GNUmakefile},
1502 read it, and see that to make @file{foo}, it needs to run the command
1503 @samp{frobnicate > foo}.  If you say @samp{make bar}, @code{make} will
1504 find no way to make @file{bar} in @file{GNUmakefile}, so it will use the
1505 commands from the pattern rule: @samp{make -f Makefile bar}.  If
1506 @file{Makefile} provides a rule for updating @file{bar}, @code{make}
1507 will apply the rule.  And likewise for any other target that
1508 @file{GNUmakefile} does not say how to make.
1510 The way this works is that the pattern rule has a pattern of just
1511 @samp{%}, so it matches any target whatever.  The rule specifies a
1512 prerequisite @file{force}, to guarantee that the commands will be run even
1513 if the target file already exists.  We give @file{force} target empty
1514 commands to prevent @code{make} from searching for an implicit rule to
1515 build it---otherwise it would apply the same match-anything rule to
1516 @file{force} itself and create a prerequisite loop!
1518 @node Reading Makefiles,  Secondary Expansion, Overriding Makefiles, Makefiles
1519 @section How @code{make} Reads a Makefile
1520 @cindex reading makefiles
1521 @cindex makefile, parsing
1523 GNU @code{make} does its work in two distinct phases.  During the first
1524 phase it reads all the makefiles, included makefiles, etc. and
1525 internalizes all the variables and their values, implicit and explicit
1526 rules, and constructs a dependency graph of all the targets and their
1527 prerequisites.  During the second phase, @code{make} uses these internal
1528 structures to determine what targets will need to be rebuilt and to
1529 invoke the rules necessary to do so.
1531 It's important to understand this two-phase approach because it has a
1532 direct impact on how variable and function expansion happens; this is
1533 often a source of some confusion when writing makefiles.  Here we will
1534 present a summary of the phases in which expansion happens for different
1535 constructs within the makefile.  We say that expansion is
1536 @dfn{immediate} if it happens during the first phase: in this case
1537 @code{make} will expand any variables or functions in that section of a
1538 construct as the makefile is parsed.  We say that expansion is
1539 @dfn{deferred} if expansion is not performed immediately.  Expansion of
1540 deferred construct is not performed until either the construct appears
1541 later in an immediate context, or until the second phase.
1543 You may not be familiar with some of these constructs yet.  You can
1544 reference this section as you become familiar with them, in later
1545 chapters.
1547 @subheading Variable Assignment
1548 @cindex +=, expansion
1549 @cindex =, expansion
1550 @cindex ?=, expansion
1551 @cindex +=, expansion
1552 @cindex define, expansion
1554 Variable definitions are parsed as follows:
1556 @example
1557 @var{immediate} = @var{deferred}
1558 @var{immediate} ?= @var{deferred}
1559 @var{immediate} := @var{immediate}
1560 @var{immediate} += @var{deferred} or @var{immediate}
1562 define @var{immediate}
1563   @var{deferred}
1564 endef
1565 @end example
1567 For the append operator, @samp{+=}, the right-hand side is considered
1568 immediate if the variable was previously set as a simple variable
1569 (@samp{:=}), and deferred otherwise.
1571 @subheading Conditional Statements
1572 @cindex ifdef, expansion
1573 @cindex ifeq, expansion
1574 @cindex ifndef, expansion
1575 @cindex ifneq, expansion
1577 All instances of conditional syntax are parsed immediately, in their
1578 entirety; this includes the @code{ifdef}, @code{ifeq}, @code{ifndef},
1579 and @code{ifneq} forms.  Of course this means that automatic variables
1580 cannot be used in conditional statements, as automatic variables are
1581 not set until the command script for that rule is invoked.  If you
1582 need to use automatic variables in a conditional you @emph{must} use
1583 shell conditional syntax, in your command script proper, for these
1584 tests, not @code{make} conditionals.
1586 @subheading Rule Definition
1587 @cindex target, expansion
1588 @cindex prerequisite, expansion
1589 @cindex implicit rule, expansion
1590 @cindex pattern rule, expansion
1591 @cindex explicit rule, expansion
1593 A rule is always expanded the same way, regardless of the form:
1595 @example
1596 @var{immediate} : @var{immediate} ; @var{deferred}
1597         @var{deferred}
1598 @end example
1600 That is, the target and prerequisite sections are expanded immediately,
1601 and the commands used to construct the target are always deferred.  This
1602 general rule is true for explicit rules, pattern rules, suffix rules,
1603 static pattern rules, and simple prerequisite definitions.
1605 @node Secondary Expansion, , Reading Makefiles, Makefiles
1606 @section Secondary Expansion
1607 @cindex secondary expansion
1608 @cindex expansion, secondary
1610 In the previous section we learned that GNU @code{make} works in two
1611 distinct phases: a read-in phase and a target-update phase
1612 (@pxref{Reading Makefiles, , How @code{make} Reads a Makefile}).
1613 There is an extra wrinkle that comes in between those two phases,
1614 right at the end of the read-in phase: at that time, all the
1615 prerequisites of all of the targets are expanded a @emph{second time}.
1616 In most circumstances this secondary expansion will have no effect,
1617 since all variable and function references will have been expanded
1618 during the initial parsing of the makefiles.  In order to take
1619 advantage of the secondary expansion phase of the parser, then, it's
1620 necessary to @emph{escape} the variable or function reference in the
1621 makefile.  In this case the first expansion merely un-escapes the
1622 reference but doesn't expand it, and expansion is left to the
1623 secondary expansion phase.  For example, consider this makefile:
1625 @example
1626 ONEVAR = onefile
1627 TWOVAR = twofile
1628 myfile: $(ONEVAR) $$(TWOVAR)
1629 @end example
1631 After the first expansion phase the prerequisites list of the
1632 @file{myfile} target will be @code{onefile} and @code{$(TWOVAR)}; the
1633 first (unescaped) variable reference to @var{ONEVAR} is expanded,
1634 while the second (escaped) variable reference is simply unescaped,
1635 without being recognized as a variable reference.  Now during the
1636 secondary expansion the first word is expanded again but since it
1637 contains no variable or function references it remains the static
1638 value @file{onefile}, while the second word is now a normal reference
1639 to the variable @var{TWOVAR}, which is expanded to the value
1640 @file{twofile}.  The final result is that there are two prerequisites,
1641 @file{onefile} and @file{twofile}.
1643 Obviously, this is not a very interesting case since the same result
1644 could more easily have been achieved simply by having both variables
1645 appear, unescaped, in the prerequisites list.  One difference becomes
1646 apparent if the variables are reset; consider this example:
1648 @example
1649 AVAR = top
1650 onefile: $(AVAR)
1651 twofile: $$(AVAR)
1652 AVAR = bottom
1653 @end example
1655 Here the prerequisite of @file{onefile} will be expanded immediately,
1656 and resolve to the value @file{top}, while the prerequisite of
1657 @file{twofile} will not be full expanded until the secondary expansion
1658 and yield a value of @file{bottom}.
1660 This is marginally more exciting, but the true power of this feature
1661 only becomes apparent when you discover that secondary expansions
1662 always take place within the scope of the automatic variables for that
1663 target.  This means that you can use variables such as @code{$@@},
1664 @code{$*}, etc. during the second expansion and they will have their
1665 expected values, just as in the command script.  All you have to do is
1666 defer the expansion by escaping the @code{$}.  Also, secondary
1667 expansion occurs for both explicit and implicit (pattern) rules.
1668 Knowing this, the possible uses for this feature are almost endless.
1669 For example:
1671 @example
1672 main_OBJS := main.o try.o test.o
1673 lib_OBJS := lib.o api.o
1675 main lib: $$($$@@_OBJS)
1676 @end example
1678 Here, after the initial expansion the prerequisites of both the
1679 @file{main} and @file{lib} targets will be @code{$($@@_OBJS)}.  During
1680 the secondary expansion, the @code{$@@} variable is set to the name of
1681 the target and so the expansion for the @file{main} target will yield
1682 @code{$(main_OBJS)}, or @code{main.o try.o test.o}, while the
1683 secondary expansion for the @file{lib} target will yield
1684 @code{$(lib_OBJS)}, or @code{lib.o api.o}.
1686 You can also mix functions here, as long as they are properly escaped:
1688 @example
1689 main_SRCS := main.c try.c test.c
1690 lib_SRCS := lib.c api.c
1692 main lib: $$(patsubst %.c,%.o,$$($$@@_SRCS))
1693 @end example
1695 This version allows users to specify source files rather than object
1696 files, but gives the same resulting prerequisites list as the previous
1697 example.
1699 Evaluation of automatic variables during the secondary expansion
1700 phase, especially of the target name variable @code{$$@@}, behaves
1701 similarly to evaluation within command scripts.  However, there are
1702 some subtle differences and ``corner cases'' which come into play for
1703 the different types of rule definitions that @code{make} understands.
1704 The subtleties of using the different automatic variables are
1705 described below.
1707 @subheading Secondary Expansion of Explicit Rules
1708 @cindex secondary expansion and explicit rules
1709 @cindex explicit rules, secondary expansion of
1711 During the secondary expansion of explicit rules, @code{$$@@} and
1712 @code{$$%} evaluate, respectively, to the file name of the target and,
1713 when the target is an archive member, the target member name.  The
1714 @code{$$<} variable evaluates to the first prerequisite in the first
1715 rule for this target.  @code{$$^} and @code{$$+} evaluate to the list
1716 of all prerequisites of rules @emph{that have already appeared} for
1717 the same target (@code{$$+} with repetitions and @code{$$^}
1718 without). The following example will help illustrate these behaviors:
1720 @example
1721 foo: foo.1 bar.1 $$< $$^ $$+    # line #1
1723 foo: foo.2 bar.2 $$< $$^ $$+    # line #2
1725 foo: foo.3 bar.3 $$< $$^ $$+    # line #3
1726 @end example
1728 For the first line, all three variables (@code{$$<}, @code{$$^}, and
1729 @code{$$+}) expand to the empty string. For the second line, they will
1730 have values @code{foo.1}, @code{foo.1 bar.1}, and @code{foo.1 bar.1}
1731 respectively. For the third they will have values @code{foo.1},
1732 @code{foo.1 bar.1 foo.2 bar.2}, and @code{foo.1 bar.1 foo.2 bar.2}
1733 respectively.
1735 Rules undergo secondary expansion in makefile order, except that
1736 the rule with the command script is always evaluated last.
1738 The variables @code{$$?} and @code{$$*} are not available and expand
1739 to the empty string.
1741 @subheading Secondary Expansion of Static Pattern Rules
1742 @cindex secondary expansion and static pattern rules
1743 @cindex static pattern rules, secondary expansion of
1745 Rules for secondary expansion of static pattern rules are identical to
1746 those for explicit rules, above, with one exception: for static
1747 pattern rules the @code{$$*} variable is set to the pattern stem.  As
1748 with explicit rules, @code{$$?} is not available and expands to the
1749 empty string.
1751 @subheading Secondary Expansion of Implicit Rules
1752 @cindex secondary expansion and implicit rules
1753 @cindex implicit rules, secondary expansion of
1755 As @code{make} searches for an implicit rule, it substitutes the stem
1756 and then performs secondary expansion for every rule with a matching
1757 target pattern.  The value of the automatic variables is derived in
1758 the same fashion as for static pattern rules.  As an example:
1760 @example
1761 foo: bar
1763 foo foz: fo%: bo%
1765 %oo: $$< $$^ $$+ $$*
1766 @end example
1768 When the implicit rule is tried for target @file{foo}, @code{$$<}
1769 expands to @file{bar}, @code{$$^} expands to @file{bar boo},
1770 @code{$$+} also expands to @file{bar boo}, and @code{$$*} expands to
1771 @file{f}.
1773 Note that the directory prefix (D), as described in @ref{Implicit Rule
1774 Search, ,Implicit Rule Search Algorithm}, is appended (after
1775 expansion) to all the patterns in the prerequisites list.  As an
1776 example:
1778 @example
1779 /tmp/foo.o:
1781 %.o: $$(addsuffix /%.c,foo bar) foo.h
1782 @end example
1784 The prerequisite list after the secondary expansion and directory
1785 prefix reconstruction will be @file{/tmp/foo/foo.c /tmp/var/bar/foo.c
1786 foo.h}.  If you are not interested in this reconstruction, you can use
1787 @code{$$*} instead of @code{%} in the prerequisites list.
1789 @node Rules, Commands, Makefiles, Top
1790 @chapter Writing Rules
1791 @cindex writing rules
1792 @cindex rule, how to write
1793 @cindex target
1794 @cindex prerequisite
1796 A @dfn{rule} appears in the makefile and says when and how to remake
1797 certain files, called the rule's @dfn{targets} (most often only one per rule).
1798 It lists the other files that are the @dfn{prerequisites} of the target, and
1799 @dfn{commands} to use to create or update the target.
1801 @cindex default goal
1802 @cindex goal, default
1803 The order of rules is not significant, except for determining the
1804 @dfn{default goal}: the target for @code{make} to consider, if you do
1805 not otherwise specify one.  The default goal is the target of the first
1806 rule in the first makefile.  If the first rule has multiple targets,
1807 only the first target is taken as the default.  There are two
1808 exceptions: a target starting with a period is not a default unless it
1809 contains one or more slashes, @samp{/}, as well; and, a target that
1810 defines a pattern rule has no effect on the default goal.
1811 (@xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.)
1813 Therefore, we usually write the makefile so that the first rule is the
1814 one for compiling the entire program or all the programs described by
1815 the makefile (often with a target called @samp{all}).
1816 @xref{Goals, ,Arguments to Specify the Goals}.
1818 @menu
1819 * Rule Example::                An example explained.
1820 * Rule Syntax::                 General syntax explained.
1821 * Prerequisite Types::          There are two types of prerequisites.
1822 * Wildcards::                   Using wildcard characters such as `*'.
1823 * Directory Search::            Searching other directories for source files.
1824 * Phony Targets::               Using a target that is not a real file's name.
1825 * Force Targets::               You can use a target without commands
1826                                   or prerequisites to mark other
1827                                   targets as phony.
1828 * Empty Targets::               When only the date matters and the
1829                                   files are empty.
1830 * Special Targets::             Targets with special built-in meanings.
1831 * Multiple Targets::            When to make use of several targets in a rule.
1832 * Multiple Rules::              How to use several rules with the same target.
1833 * Static Pattern::              Static pattern rules apply to multiple targets
1834                                   and can vary the prerequisites according to
1835                                   the target name.
1836 * Double-Colon::                How to use a special kind of rule to allow
1837                                   several independent rules for one target.
1838 * Automatic Prerequisites::     How to automatically generate rules giving
1839                                   prerequisites from source files themselves.
1840 @end menu
1842 @ifnottex
1843 @node Rule Example, Rule Syntax, Rules, Rules
1844 @section Rule Example
1846 Here is an example of a rule:
1848 @example
1849 foo.o : foo.c defs.h       # module for twiddling the frobs
1850         cc -c -g foo.c
1851 @end example
1853 Its target is @file{foo.o} and its prerequisites are @file{foo.c} and
1854 @file{defs.h}.  It has one command, which is @samp{cc -c -g foo.c}.
1855 The command line starts with a tab to identify it as a command.
1857 This rule says two things:
1859 @itemize @bullet
1860 @item
1861 How to decide whether @file{foo.o} is out of date: it is out of date
1862 if it does not exist, or if either @file{foo.c} or @file{defs.h} is
1863 more recent than it.
1865 @item
1866 How to update the file @file{foo.o}: by running @code{cc} as stated.
1867 The command does not explicitly mention @file{defs.h}, but we presume
1868 that @file{foo.c} includes it, and that that is why @file{defs.h} was
1869 added to the prerequisites.
1870 @end itemize
1871 @end ifnottex
1873 @node Rule Syntax, Prerequisite Types, Rule Example, Rules
1874 @section Rule Syntax
1876 @cindex rule syntax
1877 @cindex syntax of rules
1878 In general, a rule looks like this:
1880 @example
1881 @var{targets} : @var{prerequisites}
1882         @var{command}
1883         @dots{}
1884 @end example
1886 @noindent
1887 or like this:
1889 @example
1890 @var{targets} : @var{prerequisites} ; @var{command}
1891         @var{command}
1892         @dots{}
1893 @end example
1895 @cindex targets
1896 @cindex rule targets
1897 The @var{targets} are file names, separated by spaces.  Wildcard
1898 characters may be used (@pxref{Wildcards, ,Using Wildcard Characters
1899 in File Names}) and a name of the form @file{@var{a}(@var{m})}
1900 represents member @var{m} in archive file @var{a}
1901 (@pxref{Archive Members, ,Archive Members as Targets}).
1902 Usually there is only one
1903 target per rule, but occasionally there is a reason to have more
1904 (@pxref{Multiple Targets, , Multiple Targets in a Rule}).@refill
1906 @cindex commands
1907 @cindex tab character (in commands)
1908 The @var{command} lines start with a tab character.  The first command may
1909 appear on the line after the prerequisites, with a tab character, or may
1910 appear on the same line, with a semicolon.  Either way, the effect is the
1911 same.  @xref{Commands, ,Writing the Commands in Rules}.
1913 @cindex dollar sign (@code{$}), in rules
1914 @cindex @code{$}, in rules
1915 @cindex rule, and @code{$}
1916 Because dollar signs are used to start variable references, if you really
1917 want a dollar sign in a rule you must write two of them, @samp{$$}
1918 (@pxref{Using Variables, ,How to Use Variables}).  In prerequisite
1919 lists you must actually write @emph{four} dollar signs (@samp{$$$$}),
1920 due to secondary expansion (@pxref{Secondary Expansion}).
1921 You may split a long line by inserting a backslash
1922 followed by a newline, but this is not required, as @code{make} places no
1923 limit on the length of a line in a makefile.
1925 A rule tells @code{make} two things: when the targets are out of date,
1926 and how to update them when necessary.
1928 @cindex prerequisites
1929 @cindex rule prerequisites
1930 The criterion for being out of date is specified in terms of the
1931 @var{prerequisites}, which consist of file names separated by spaces.
1932 (Wildcards and archive members (@pxref{Archives}) are allowed here too.)
1933 A target is out of date if it does not exist or if it is older than any
1934 of the prerequisites (by comparison of last-modification times).  The
1935 idea is that the contents of the target file are computed based on
1936 information in the prerequisites, so if any of the prerequisites changes,
1937 the contents of the existing target file are no longer necessarily
1938 valid.
1940 How to update is specified by @var{commands}.  These are lines to be
1941 executed by the shell (normally @samp{sh}), but with some extra features
1942 (@pxref{Commands, ,Writing the Commands in Rules}).
1944 @node Prerequisite Types, Wildcards, Rule Syntax, Rules
1945 @comment  node-name,  next,  previous,  up
1946 @section Types of Prerequisites
1947 @cindex prerequisite types
1948 @cindex types of prerequisites
1950 @cindex prerequisites, normal
1951 @cindex normal prerequisites
1952 @cindex prerequisites, order-only
1953 @cindex order-only prerequisites
1954 There are actually two different types of prerequisites understood by
1955 GNU @code{make}: normal prerequisites such as described in the
1956 previous section, and @dfn{order-only} prerequisites.  A normal
1957 prerequisite makes two statements: first, it imposes an order of
1958 execution of build commands: any commands necessary to build any of a
1959 target's prerequisites will be fully executed before any commands
1960 necessary to build the target.  Second, it imposes a dependency
1961 relationship: if any prerequisite is newer than the target, then the
1962 target is considered out-of-date and must be rebuilt.
1964 Normally, this is exactly what you want: if a target's prerequisite is
1965 updated, then the target should also be updated.
1967 Occasionally, however, you have a situation where you want to impose a
1968 specific ordering on the rules to be invoked @emph{without} forcing
1969 the target to be updated if one of those rules is executed.  In that
1970 case, you want to define @dfn{order-only} prerequisites.  Order-only
1971 prerequisites can be specified by placing a pipe symbol (@code{|})
1972 in the prerequisites list: any prerequisites to the left of the pipe
1973 symbol are normal; any prerequisites to the right are order-only:
1975 @example
1976 @var{targets} : @var{normal-prerequisites} | @var{order-only-prerequisites}
1977 @end example
1979 The normal prerequisites section may of course be empty.  Also, you
1980 may still declare multiple lines of prerequisites for the same target:
1981 they are appended appropriately.  Note that if you declare the same
1982 file to be both a normal and an order-only prerequisite, the normal
1983 prerequisite takes precedence (since they are a strict superset of the
1984 behavior of an order-only prerequisite).
1986 @node Wildcards, Directory Search, Prerequisite Types, Rules
1987 @section Using Wildcard Characters in File Names
1988 @cindex wildcard
1989 @cindex file name with wildcards
1990 @cindex globbing (wildcards)
1992 @cindex @code{*} (wildcard character)
1993 @cindex @code{?} (wildcard character)
1994 @cindex @code{[@dots{}]} (wildcard characters)
1995 A single file name can specify many files using @dfn{wildcard characters}.
1996 The wildcard characters in @code{make} are @samp{*}, @samp{?} and
1997 @samp{[@dots{}]}, the same as in the Bourne shell.  For example, @file{*.c}
1998 specifies a list of all the files (in the working directory) whose names
1999 end in @samp{.c}.@refill
2001 @cindex @code{~} (tilde)
2002 @cindex tilde (@code{~})
2003 @cindex home directory
2004 The character @samp{~} at the beginning of a file name also has special
2005 significance.  If alone, or followed by a slash, it represents your home
2006 directory.  For example @file{~/bin} expands to @file{/home/you/bin}.
2007 If the @samp{~} is followed by a word, the string represents the home
2008 directory of the user named by that word.  For example @file{~john/bin}
2009 expands to @file{/home/john/bin}.  On systems which don't have a home
2010 directory for each user (such as MS-DOS or MS-Windows), this
2011 functionality can be simulated by setting the environment variable
2012 @var{HOME}.@refill
2014 Wildcard expansion happens automatically in targets, in prerequisites,
2015 and in commands (where the shell does the expansion).  In other
2016 contexts, wildcard expansion happens only if you request it explicitly
2017 with the @code{wildcard} function.
2019 The special significance of a wildcard character can be turned off by
2020 preceding it with a backslash.  Thus, @file{foo\*bar} would refer to a
2021 specific file whose name consists of @samp{foo}, an asterisk, and
2022 @samp{bar}.@refill
2024 @menu
2025 * Wildcard Examples::           Several examples
2026 * Wildcard Pitfall::            Problems to avoid.
2027 * Wildcard Function::           How to cause wildcard expansion where
2028                                   it does not normally take place.
2029 @end menu
2031 @node Wildcard Examples, Wildcard Pitfall, Wildcards, Wildcards
2032 @subsection Wildcard Examples
2034 Wildcards can be used in the commands of a rule, where they are expanded
2035 by the shell.  For example, here is a rule to delete all the object files:
2037 @example
2038 @group
2039 clean:
2040         rm -f *.o
2041 @end group
2042 @end example
2043 @cindex @code{rm} (shell command)
2045 Wildcards are also useful in the prerequisites of a rule.  With the
2046 following rule in the makefile, @samp{make print} will print all the
2047 @samp{.c} files that have changed since the last time you printed them:
2049 @example
2050 print: *.c
2051         lpr -p $?
2052         touch print
2053 @end example
2055 @cindex @code{print} target
2056 @cindex @code{lpr} (shell command)
2057 @cindex @code{touch} (shell command)
2058 @noindent
2059 This rule uses @file{print} as an empty target file; see @ref{Empty
2060 Targets, ,Empty Target Files to Record Events}.  (The automatic variable
2061 @samp{$?} is used to print only those files that have changed; see
2062 @ref{Automatic Variables}.)@refill
2064 Wildcard expansion does not happen when you define a variable.  Thus, if
2065 you write this:
2067 @example
2068 objects = *.o
2069 @end example
2071 @noindent
2072 then the value of the variable @code{objects} is the actual string
2073 @samp{*.o}.  However, if you use the value of @code{objects} in a target,
2074 prerequisite or command, wildcard expansion will take place at that time.
2075 To set @code{objects} to the expansion, instead use:
2077 @example
2078 objects := $(wildcard *.o)
2079 @end example
2081 @noindent
2082 @xref{Wildcard Function}.
2084 @node Wildcard Pitfall, Wildcard Function, Wildcard Examples, Wildcards
2085 @subsection Pitfalls of Using Wildcards
2086 @cindex wildcard pitfalls
2087 @cindex pitfalls of wildcards
2088 @cindex mistakes with wildcards
2089 @cindex errors with wildcards
2090 @cindex problems with wildcards
2092 Now here is an example of a naive way of using wildcard expansion, that
2093 does not do what you would intend.  Suppose you would like to say that the
2094 executable file @file{foo} is made from all the object files in the
2095 directory, and you write this:
2097 @example
2098 objects = *.o
2100 foo : $(objects)
2101         cc -o foo $(CFLAGS) $(objects)
2102 @end example
2104 @noindent
2105 The value of @code{objects} is the actual string @samp{*.o}.  Wildcard
2106 expansion happens in the rule for @file{foo}, so that each @emph{existing}
2107 @samp{.o} file becomes a prerequisite of @file{foo} and will be recompiled if
2108 necessary.
2110 But what if you delete all the @samp{.o} files?  When a wildcard matches
2111 no files, it is left as it is, so then @file{foo} will depend on the
2112 oddly-named file @file{*.o}.  Since no such file is likely to exist,
2113 @code{make} will give you an error saying it cannot figure out how to
2114 make @file{*.o}.  This is not what you want!
2116 Actually it is possible to obtain the desired result with wildcard
2117 expansion, but you need more sophisticated techniques, including the
2118 @code{wildcard} function and string substitution.
2119 @ifnottex
2120 @xref{Wildcard Function, ,The Function @code{wildcard}}.
2121 @end ifnottex
2122 @iftex
2123 These are described in the following section.
2124 @end iftex
2126 @cindex wildcards and MS-DOS/MS-Windows backslashes
2127 @cindex backslashes in pathnames and wildcard expansion
2129 Microsoft operating systems (MS-DOS and MS-Windows) use backslashes to
2130 separate directories in pathnames, like so:
2132 @example
2133   c:\foo\bar\baz.c
2134 @end example
2136 This is equivalent to the Unix-style @file{c:/foo/bar/baz.c} (the
2137 @file{c:} part is the so-called drive letter).  When @code{make} runs on
2138 these systems, it supports backslashes as well as the Unix-style forward
2139 slashes in pathnames.  However, this support does @emph{not} include the
2140 wildcard expansion, where backslash is a quote character.  Therefore,
2141 you @emph{must} use Unix-style slashes in these cases.
2144 @node Wildcard Function,  , Wildcard Pitfall, Wildcards
2145 @subsection The Function @code{wildcard}
2146 @findex wildcard
2148 Wildcard expansion happens automatically in rules.  But wildcard expansion
2149 does not normally take place when a variable is set, or inside the
2150 arguments of a function.  If you want to do wildcard expansion in such
2151 places, you need to use the @code{wildcard} function, like this:
2153 @example
2154 $(wildcard @var{pattern}@dots{})
2155 @end example
2157 @noindent
2158 This string, used anywhere in a makefile, is replaced by a
2159 space-separated list of names of existing files that match one of the
2160 given file name patterns.  If no existing file name matches a pattern,
2161 then that pattern is omitted from the output of the @code{wildcard}
2162 function.  Note that this is different from how unmatched wildcards
2163 behave in rules, where they are used verbatim rather than ignored
2164 (@pxref{Wildcard Pitfall}).
2166 One use of the @code{wildcard} function is to get a list of all the C source
2167 files in a directory, like this:
2169 @example
2170 $(wildcard *.c)
2171 @end example
2173 We can change the list of C source files into a list of object files by
2174 replacing the @samp{.c} suffix with @samp{.o} in the result, like this:
2176 @example
2177 $(patsubst %.c,%.o,$(wildcard *.c))
2178 @end example
2180 @noindent
2181 (Here we have used another function, @code{patsubst}.
2182 @xref{Text Functions, ,Functions for String Substitution and Analysis}.)@refill
2184 Thus, a makefile to compile all C source files in the directory and then
2185 link them together could be written as follows:
2187 @example
2188 objects := $(patsubst %.c,%.o,$(wildcard *.c))
2190 foo : $(objects)
2191         cc -o foo $(objects)
2192 @end example
2194 @noindent
2195 (This takes advantage of the implicit rule for compiling C programs, so
2196 there is no need to write explicit rules for compiling the files.
2197 @xref{Flavors, ,The Two Flavors of Variables}, for an explanation of
2198 @samp{:=}, which is a variant of @samp{=}.)
2200 @node Directory Search, Phony Targets, Wildcards, Rules
2201 @section Searching Directories for Prerequisites
2202 @vindex VPATH
2203 @findex vpath
2204 @cindex vpath
2205 @cindex search path for prerequisites (@code{VPATH})
2206 @cindex directory search (@code{VPATH})
2208 For large systems, it is often desirable to put sources in a separate
2209 directory from the binaries.  The @dfn{directory search} features of
2210 @code{make} facilitate this by searching several directories
2211 automatically to find a prerequisite.  When you redistribute the files
2212 among directories, you do not need to change the individual rules,
2213 just the search paths.
2215 @menu
2216 * General Search::              Specifying a search path that applies
2217                                   to every prerequisite.
2218 * Selective Search::            Specifying a search path
2219                                   for a specified class of names.
2220 * Search Algorithm::            When and how search paths are applied.
2221 * Commands/Search::             How to write shell commands that work together
2222                                   with search paths.
2223 * Implicit/Search::             How search paths affect implicit rules.
2224 * Libraries/Search::            Directory search for link libraries.
2225 @end menu
2227 @node General Search, Selective Search, Directory Search, Directory Search
2228 @subsection @code{VPATH}: Search Path for All Prerequisites
2229 @vindex VPATH
2231 The value of the @code{make} variable @code{VPATH} specifies a list of
2232 directories that @code{make} should search.  Most often, the
2233 directories are expected to contain prerequisite files that are not in the
2234 current directory; however, @code{make} uses @code{VPATH} as a search
2235 list for both prerequisites and targets of rules.
2237 Thus, if a file that is listed as a target or prerequisite does not exist
2238 in the current directory, @code{make} searches the directories listed in
2239 @code{VPATH} for a file with that name.  If a file is found in one of
2240 them, that file may become the prerequisite (see below).  Rules may then
2241 specify the names of files in the prerequisite list as if they all
2242 existed in the current directory.  @xref{Commands/Search, ,Writing Shell
2243 Commands with Directory Search}.
2245 In the @code{VPATH} variable, directory names are separated by colons or
2246 blanks.  The order in which directories are listed is the order followed
2247 by @code{make} in its search.  (On MS-DOS and MS-Windows, semi-colons
2248 are used as separators of directory names in @code{VPATH}, since the
2249 colon can be used in the pathname itself, after the drive letter.)
2251 For example,
2253 @example
2254 VPATH = src:../headers
2255 @end example
2257 @noindent
2258 specifies a path containing two directories, @file{src} and
2259 @file{../headers}, which @code{make} searches in that order.
2261 With this value of @code{VPATH}, the following rule,
2263 @example
2264 foo.o : foo.c
2265 @end example
2267 @noindent
2268 is interpreted as if it were written like this:
2270 @example
2271 foo.o : src/foo.c
2272 @end example
2274 @noindent
2275 assuming the file @file{foo.c} does not exist in the current directory but
2276 is found in the directory @file{src}.
2278 @node Selective Search, Search Algorithm, General Search, Directory Search
2279 @subsection The @code{vpath} Directive
2280 @findex vpath
2282 Similar to the @code{VPATH} variable, but more selective, is the
2283 @code{vpath} directive (note lower case), which allows you to specify a
2284 search path for a particular class of file names: those that match a
2285 particular pattern.  Thus you can supply certain search directories for
2286 one class of file names and other directories (or none) for other file
2287 names.
2289 There are three forms of the @code{vpath} directive:
2291 @table @code
2292 @item vpath @var{pattern} @var{directories}
2293 Specify the search path @var{directories} for file names that match
2294 @var{pattern}.
2296 The search path, @var{directories}, is a list of directories to be
2297 searched, separated by colons (semi-colons on MS-DOS and MS-Windows) or
2298 blanks, just like the search path used in the @code{VPATH} variable.
2300 @item vpath @var{pattern}
2301 Clear out the search path associated with @var{pattern}.
2303 @c Extra blank line makes sure this gets two lines.
2304 @item vpath
2306 Clear all search paths previously specified with @code{vpath} directives.
2307 @end table
2309 A @code{vpath} pattern is a string containing a @samp{%} character.  The
2310 string must match the file name of a prerequisite that is being searched
2311 for, the @samp{%} character matching any sequence of zero or more
2312 characters (as in pattern rules; @pxref{Pattern Rules, ,Defining and
2313 Redefining Pattern Rules}).  For example, @code{%.h} matches files that
2314 end in @code{.h}.  (If there is no @samp{%}, the pattern must match the
2315 prerequisite exactly, which is not useful very often.)
2317 @cindex @code{%}, quoting in @code{vpath}
2318 @cindex @code{%}, quoting with @code{\} (backslash)
2319 @cindex @code{\} (backslash), to quote @code{%}
2320 @cindex backslash (@code{\}), to quote @code{%}
2321 @cindex quoting @code{%}, in @code{vpath}
2322 @samp{%} characters in a @code{vpath} directive's pattern can be quoted
2323 with preceding backslashes (@samp{\}).  Backslashes that would otherwise
2324 quote @samp{%} characters can be quoted with more backslashes.
2325 Backslashes that quote @samp{%} characters or other backslashes are
2326 removed from the pattern before it is compared to file names.  Backslashes
2327 that are not in danger of quoting @samp{%} characters go unmolested.@refill
2329 When a prerequisite fails to exist in the current directory, if the
2330 @var{pattern} in a @code{vpath} directive matches the name of the
2331 prerequisite file, then the @var{directories} in that directive are searched
2332 just like (and before) the directories in the @code{VPATH} variable.
2334 For example,
2336 @example
2337 vpath %.h ../headers
2338 @end example
2340 @noindent
2341 tells @code{make} to look for any prerequisite whose name ends in @file{.h}
2342 in the directory @file{../headers} if the file is not found in the current
2343 directory.
2345 If several @code{vpath} patterns match the prerequisite file's name, then
2346 @code{make} processes each matching @code{vpath} directive one by one,
2347 searching all the directories mentioned in each directive.  @code{make}
2348 handles multiple @code{vpath} directives in the order in which they
2349 appear in the makefile; multiple directives with the same pattern are
2350 independent of each other.
2352 @need 750
2353 Thus,
2355 @example
2356 @group
2357 vpath %.c foo
2358 vpath %   blish
2359 vpath %.c bar
2360 @end group
2361 @end example
2363 @noindent
2364 will look for a file ending in @samp{.c} in @file{foo}, then
2365 @file{blish}, then @file{bar}, while
2367 @example
2368 @group
2369 vpath %.c foo:bar
2370 vpath %   blish
2371 @end group
2372 @end example
2374 @noindent
2375 will look for a file ending in @samp{.c} in @file{foo}, then
2376 @file{bar}, then @file{blish}.
2378 @node Search Algorithm, Commands/Search, Selective Search, Directory Search
2379 @subsection How Directory Searches are Performed
2380 @cindex algorithm for directory search
2381 @cindex directory search algorithm
2383 When a prerequisite is found through directory search, regardless of type
2384 (general or selective), the pathname located may not be the one that
2385 @code{make} actually provides you in the prerequisite list.  Sometimes
2386 the path discovered through directory search is thrown away.
2388 The algorithm @code{make} uses to decide whether to keep or abandon a
2389 path found via directory search is as follows:
2391 @enumerate
2392 @item
2393 If a target file does not exist at the path specified in the makefile,
2394 directory search is performed.
2396 @item
2397 If the directory search is successful, that path is kept and this file
2398 is tentatively stored as the target.
2400 @item
2401 All prerequisites of this target are examined using this same method.
2403 @item
2404 After processing the prerequisites, the target may or may not need to be
2405 rebuilt:
2407 @enumerate a
2408 @item
2409 If the target does @emph{not} need to be rebuilt, the path to the file
2410 found during directory search is used for any prerequisite lists which
2411 contain this target.  In short, if @code{make} doesn't need to rebuild
2412 the target then you use the path found via directory search.
2414 @item
2415 If the target @emph{does} need to be rebuilt (is out-of-date), the
2416 pathname found during directory search is @emph{thrown away}, and the
2417 target is rebuilt using the file name specified in the makefile.  In
2418 short, if @code{make} must rebuild, then the target is rebuilt locally,
2419 not in the directory found via directory search.
2420 @end enumerate
2421 @end enumerate
2423 This algorithm may seem complex, but in practice it is quite often
2424 exactly what you want.
2426 @cindex traditional directory search (GPATH)
2427 @cindex directory search, traditional (GPATH)
2428 Other versions of @code{make} use a simpler algorithm: if the file does
2429 not exist, and it is found via directory search, then that pathname is
2430 always used whether or not the target needs to be built.  Thus, if the
2431 target is rebuilt it is created at the pathname discovered during
2432 directory search.
2434 @vindex GPATH
2435 If, in fact, this is the behavior you want for some or all of your
2436 directories, you can use the @code{GPATH} variable to indicate this to
2437 @code{make}.
2439 @code{GPATH} has the same syntax and format as @code{VPATH} (that is, a
2440 space- or colon-delimited list of pathnames).  If an out-of-date target
2441 is found by directory search in a directory that also appears in
2442 @code{GPATH}, then that pathname is not thrown away.  The target is
2443 rebuilt using the expanded path.
2445 @node Commands/Search, Implicit/Search, Search Algorithm, Directory Search
2446 @subsection Writing Shell Commands with Directory Search
2447 @cindex shell command, and directory search
2448 @cindex directory search (@code{VPATH}), and shell commands
2450 When a prerequisite is found in another directory through directory search,
2451 this cannot change the commands of the rule; they will execute as written.
2452 Therefore, you must write the commands with care so that they will look for
2453 the prerequisite in the directory where @code{make} finds it.
2455 This is done with the @dfn{automatic variables} such as @samp{$^}
2456 (@pxref{Automatic Variables}).
2457 For instance, the value of @samp{$^} is a
2458 list of all the prerequisites of the rule, including the names of
2459 the directories in which they were found, and the value of
2460 @samp{$@@} is the target.  Thus:@refill
2462 @example
2463 foo.o : foo.c
2464         cc -c $(CFLAGS) $^ -o $@@
2465 @end example
2467 @noindent
2468 (The variable @code{CFLAGS} exists so you can specify flags for C
2469 compilation by implicit rules; we use it here for consistency so it will
2470 affect all C compilations uniformly;
2471 @pxref{Implicit Variables, ,Variables Used by Implicit Rules}.)
2473 Often the prerequisites include header files as well, which you do not
2474 want to mention in the commands.  The automatic variable @samp{$<} is
2475 just the first prerequisite:
2477 @example
2478 VPATH = src:../headers
2479 foo.o : foo.c defs.h hack.h
2480         cc -c $(CFLAGS) $< -o $@@
2481 @end example
2483 @node Implicit/Search, Libraries/Search, Commands/Search, Directory Search
2484 @subsection Directory Search and Implicit Rules
2485 @cindex @code{VPATH}, and implicit rules
2486 @cindex directory search (@code{VPATH}), and implicit rules
2487 @cindex search path for prerequisites (@code{VPATH}), and implicit rules
2488 @cindex implicit rule, and directory search
2489 @cindex implicit rule, and @code{VPATH}
2490 @cindex rule, implicit, and directory search
2491 @cindex rule, implicit, and @code{VPATH}
2493 The search through the directories specified in @code{VPATH} or with
2494 @code{vpath} also happens during consideration of implicit rules
2495 (@pxref{Implicit Rules, ,Using Implicit Rules}).
2497 For example, when a file @file{foo.o} has no explicit rule, @code{make}
2498 considers implicit rules, such as the built-in rule to compile
2499 @file{foo.c} if that file exists.  If such a file is lacking in the
2500 current directory, the appropriate directories are searched for it.  If
2501 @file{foo.c} exists (or is mentioned in the makefile) in any of the
2502 directories, the implicit rule for C compilation is applied.
2504 The commands of implicit rules normally use automatic variables as a
2505 matter of necessity; consequently they will use the file names found by
2506 directory search with no extra effort.
2508 @node Libraries/Search,  , Implicit/Search, Directory Search
2509 @subsection Directory Search for Link Libraries
2510 @cindex link libraries, and directory search
2511 @cindex libraries for linking, directory search
2512 @cindex directory search (@code{VPATH}), and link libraries
2513 @cindex @code{VPATH}, and link libraries
2514 @cindex search path for prerequisites (@code{VPATH}), and link libraries
2515 @cindex @code{-l} (library search)
2516 @cindex link libraries, patterns matching
2517 @cindex @code{.LIBPATTERNS}, and link libraries
2518 @vindex .LIBPATTERNS
2520 Directory search applies in a special way to libraries used with the
2521 linker.  This special feature comes into play when you write a prerequisite
2522 whose name is of the form @samp{-l@var{name}}.  (You can tell something
2523 strange is going on here because the prerequisite is normally the name of a
2524 file, and the @emph{file name} of a library generally looks like
2525 @file{lib@var{name}.a}, not like @samp{-l@var{name}}.)@refill
2527 When a prerequisite's name has the form @samp{-l@var{name}}, @code{make}
2528 handles it specially by searching for the file @file{lib@var{name}.so} in
2529 the current directory, in directories specified by matching @code{vpath}
2530 search paths and the @code{VPATH} search path, and then in the
2531 directories @file{/lib}, @file{/usr/lib}, and @file{@var{prefix}/lib}
2532 (normally @file{/usr/local/lib}, but MS-DOS/MS-Windows versions of
2533 @code{make} behave as if @var{prefix} is defined to be the root of the
2534 DJGPP installation tree).
2536 If that file is not found, then the file @file{lib@var{name}.a} is
2537 searched for, in the same directories as above.
2539 For example, if there is a @file{/usr/lib/libcurses.a} library on your
2540 system (and no @file{/usr/lib/libcurses.so} file), then
2542 @example
2543 @group
2544 foo : foo.c -lcurses
2545         cc $^ -o $@@
2546 @end group
2547 @end example
2549 @noindent
2550 would cause the command @samp{cc foo.c /usr/lib/libcurses.a -o foo} to
2551 be executed when @file{foo} is older than @file{foo.c} or than
2552 @file{/usr/lib/libcurses.a}.@refill
2554 Although the default set of files to be searched for is
2555 @file{lib@var{name}.so} and @file{lib@var{name}.a}, this is customizable
2556 via the @code{.LIBPATTERNS} variable.  Each word in the value of this
2557 variable is a pattern string.  When a prerequisite like
2558 @samp{-l@var{name}} is seen, @code{make} will replace the percent in
2559 each pattern in the list with @var{name} and perform the above directory
2560 searches using that library filename.  If no library is found, the next
2561 word in the list will be used.
2563 The default value for @code{.LIBPATTERNS} is ``@samp{lib%.so lib%.a}'',
2564 which provides the default behavior described above.
2566 You can turn off link library expansion completely by setting this
2567 variable to an empty value.
2569 @node Phony Targets, Force Targets, Directory Search, Rules
2570 @section Phony Targets
2571 @cindex phony targets
2572 @cindex targets, phony
2573 @cindex targets without a file
2575 A phony target is one that is not really the name of a file.  It is just a
2576 name for some commands to be executed when you make an explicit request.
2577 There are two reasons to use a phony target: to avoid a conflict with
2578 a file of the same name, and to improve performance.
2580 If you write a rule whose commands will not create the target file, the
2581 commands will be executed every time the target comes up for remaking.
2582 Here is an example:
2584 @example
2585 @group
2586 clean:
2587         rm *.o temp
2588 @end group
2589 @end example
2591 @noindent
2592 Because the @code{rm} command does not create a file named @file{clean},
2593 probably no such file will ever exist.  Therefore, the @code{rm} command
2594 will be executed every time you say @samp{make clean}.
2595 @cindex @code{rm} (shell command)
2597 @findex .PHONY
2598 The phony target will cease to work if anything ever does create a file
2599 named @file{clean} in this directory.  Since it has no prerequisites, the
2600 file @file{clean} would inevitably be considered up to date, and its
2601 commands would not be executed.  To avoid this problem, you can explicitly
2602 declare the target to be phony, using the special target @code{.PHONY}
2603 (@pxref{Special Targets, ,Special Built-in Target Names}) as follows:
2605 @example
2606 .PHONY : clean
2607 @end example
2609 @noindent
2610 Once this is done, @samp{make clean} will run the commands regardless of
2611 whether there is a file named @file{clean}.
2613 Since it knows that phony targets do not name actual files that could be
2614 remade from other files, @code{make} skips the implicit rule search for
2615 phony targets (@pxref{Implicit Rules}).  This is why declaring a target
2616 phony is good for performance, even if you are not worried about the
2617 actual file existing.
2619 Thus, you first write the line that states that @code{clean} is a
2620 phony target, then you write the rule, like this:
2622 @example
2623 @group
2624 .PHONY: clean
2625 clean:
2626         rm *.o temp
2627 @end group
2628 @end example
2630 Another example of the usefulness of phony targets is in conjunction
2631 with recursive invocations of @code{make} (for more information, see
2632 @ref{Recursion, ,Recursive Use of @code{make}}).  In this case the
2633 makefile will often contain a variable which lists a number of
2634 subdirectories to be built.  One way to handle this is with one rule
2635 whose command is a shell loop over the subdirectories, like this:
2637 @example
2638 @group
2639 SUBDIRS = foo bar baz
2641 subdirs:
2642         for dir in $(SUBDIRS); do \
2643           $(MAKE) -C $$dir; \
2644         done
2645 @end group
2646 @end example
2648 There are a few problems with this method, however.  First, any error
2649 detected in a submake is not noted by this rule, so it will continue to
2650 build the rest of the directories even when one fails.  This can be
2651 overcome by adding shell commands to note the error and exit, but then
2652 it will do so even if @code{make} is invoked with the @code{-k} option,
2653 which is unfortunate.  Second, and perhaps more importantly, you cannot
2654 take advantage of the parallel build capabilities of make using this
2655 method, since there is only one rule.
2657 By declaring the subdirectories as phony targets (you must do this as
2658 the subdirectory obviously always exists; otherwise it won't be built)
2659 you can remove these problems:
2661 @example
2662 @group
2663 SUBDIRS = foo bar baz
2665 .PHONY: subdirs $(SUBDIRS)
2667 subdirs: $(SUBDIRS)
2669 $(SUBDIRS):
2670         $(MAKE) -C $@@
2672 foo: baz
2673 @end group
2674 @end example
2676 Here we've also declared that the @file{foo} subdirectory cannot be
2677 built until after the @file{baz} subdirectory is complete; this kind of
2678 relationship declaration is particularly important when attempting
2679 parallel builds.
2681 A phony target should not be a prerequisite of a real target file; if it
2682 is, its commands are run every time @code{make} goes to update that
2683 file.  As long as a phony target is never a prerequisite of a real
2684 target, the phony target commands will be executed only when the phony
2685 target is a specified goal (@pxref{Goals, ,Arguments to Specify the
2686 Goals}).
2688 Phony targets can have prerequisites.  When one directory contains multiple
2689 programs, it is most convenient to describe all of the programs in one
2690 makefile @file{./Makefile}.  Since the target remade by default will be the
2691 first one in the makefile, it is common to make this a phony target named
2692 @samp{all} and give it, as prerequisites, all the individual programs.  For
2693 example:
2695 @example
2696 all : prog1 prog2 prog3
2697 .PHONY : all
2699 prog1 : prog1.o utils.o
2700         cc -o prog1 prog1.o utils.o
2702 prog2 : prog2.o
2703         cc -o prog2 prog2.o
2705 prog3 : prog3.o sort.o utils.o
2706         cc -o prog3 prog3.o sort.o utils.o
2707 @end example
2709 @noindent
2710 Now you can say just @samp{make} to remake all three programs, or
2711 specify as arguments the ones to remake (as in @samp{make prog1
2712 prog3}).  Phoniness is not inherited: the prerequisites of a phony
2713 target are not themselves phony, unless explicitly declared to be so.
2715 When one phony target is a prerequisite of another, it serves as a subroutine
2716 of the other.  For example, here @samp{make cleanall} will delete the
2717 object files, the difference files, and the file @file{program}:
2719 @example
2720 .PHONY: cleanall cleanobj cleandiff
2722 cleanall : cleanobj cleandiff
2723         rm program
2725 cleanobj :
2726         rm *.o
2728 cleandiff :
2729         rm *.diff
2730 @end example
2732 @node Force Targets, Empty Targets, Phony Targets, Rules
2733 @section Rules without Commands or Prerequisites
2734 @cindex force targets
2735 @cindex targets, force
2736 @cindex @code{FORCE}
2737 @cindex rule, no commands or prerequisites
2739 If a rule has no prerequisites or commands, and the target of the rule
2740 is a nonexistent file, then @code{make} imagines this target to have
2741 been updated whenever its rule is run.  This implies that all targets
2742 depending on this one will always have their commands run.
2744 An example will illustrate this:
2746 @example
2747 @group
2748 clean: FORCE
2749         rm $(objects)
2750 FORCE:
2751 @end group
2752 @end example
2754 Here the target @samp{FORCE} satisfies the special conditions, so the
2755 target @file{clean} that depends on it is forced to run its commands.
2756 There is nothing special about the name @samp{FORCE}, but that is one name
2757 commonly used this way.
2759 As you can see, using @samp{FORCE} this way has the same results as using
2760 @samp{.PHONY: clean}.
2762 Using @samp{.PHONY} is more explicit and more efficient.  However,
2763 other versions of @code{make} do not support @samp{.PHONY}; thus
2764 @samp{FORCE} appears in many makefiles.  @xref{Phony Targets}.
2766 @node Empty Targets, Special Targets, Force Targets, Rules
2767 @section Empty Target Files to Record Events
2768 @cindex empty targets
2769 @cindex targets, empty
2770 @cindex recording events with empty targets
2772 The @dfn{empty target} is a variant of the phony target; it is used to hold
2773 commands for an action that you request explicitly from time to time.
2774 Unlike a phony target, this target file can really exist; but the file's
2775 contents do not matter, and usually are empty.
2777 The purpose of the empty target file is to record, with its
2778 last-modification time, when the rule's commands were last executed.  It
2779 does so because one of the commands is a @code{touch} command to update the
2780 target file.
2782 The empty target file should have some prerequisites (otherwise it
2783 doesn't make sense).  When you ask to remake the empty target, the
2784 commands are executed if any prerequisite is more recent than the target;
2785 in other words, if a prerequisite has changed since the last time you
2786 remade the target.  Here is an example:
2788 @example
2789 print: foo.c bar.c
2790         lpr -p $?
2791         touch print
2792 @end example
2793 @cindex @code{print} target
2794 @cindex @code{lpr} (shell command)
2795 @cindex @code{touch} (shell command)
2797 @noindent
2798 With this rule, @samp{make print} will execute the @code{lpr} command if
2799 either source file has changed since the last @samp{make print}.  The
2800 automatic variable @samp{$?} is used to print only those files that have
2801 changed (@pxref{Automatic Variables}).
2803 @node Special Targets, Multiple Targets, Empty Targets, Rules
2804 @section Special Built-in Target Names
2805 @cindex special targets
2806 @cindex built-in special targets
2807 @cindex targets, built-in special
2809 Certain names have special meanings if they appear as targets.
2811 @table @code
2812 @findex .PHONY
2813 @item .PHONY
2815 The prerequisites of the special target @code{.PHONY} are considered to
2816 be phony targets.  When it is time to consider such a target,
2817 @code{make} will run its commands unconditionally, regardless of
2818 whether a file with that name exists or what its last-modification
2819 time is.  @xref{Phony Targets, ,Phony Targets}.
2821 @findex .SUFFIXES
2822 @item .SUFFIXES
2824 The prerequisites of the special target @code{.SUFFIXES} are the list
2825 of suffixes to be used in checking for suffix rules.
2826 @xref{Suffix Rules, , Old-Fashioned Suffix Rules}.
2828 @findex .DEFAULT
2829 @item .DEFAULT
2831 The commands specified for @code{.DEFAULT} are used for any target for
2832 which no rules are found (either explicit rules or implicit rules).
2833 @xref{Last Resort}.  If @code{.DEFAULT} commands are specified, every
2834 file mentioned as a prerequisite, but not as a target in a rule, will have
2835 these commands executed on its behalf.  @xref{Implicit Rule Search,
2836 ,Implicit Rule Search Algorithm}.
2838 @findex .PRECIOUS
2839 @item .PRECIOUS
2840 @cindex precious targets
2841 @cindex preserving with @code{.PRECIOUS}
2843 The targets which @code{.PRECIOUS} depends on are given the following
2844 special treatment: if @code{make} is killed or interrupted during the
2845 execution of their commands, the target is not deleted.
2846 @xref{Interrupts, ,Interrupting or Killing @code{make}}.  Also, if the
2847 target is an intermediate file, it will not be deleted after it is no
2848 longer needed, as is normally done.  @xref{Chained Rules, ,Chains of
2849 Implicit Rules}.  In this latter respect it overlaps with the
2850 @code{.SECONDARY} special target.
2852 You can also list the target pattern of an implicit rule (such as
2853 @samp{%.o}) as a prerequisite file of the special target @code{.PRECIOUS}
2854 to preserve intermediate files created by rules whose target patterns
2855 match that file's name.
2857 @findex .INTERMEDIATE
2858 @item .INTERMEDIATE
2859 @cindex intermediate targets, explicit
2861 The targets which @code{.INTERMEDIATE} depends on are treated as
2862 intermediate files.  @xref{Chained Rules, ,Chains of Implicit Rules}.
2863 @code{.INTERMEDIATE} with no prerequisites has no effect.
2865 @findex .SECONDARY
2866 @item .SECONDARY
2867 @cindex secondary targets
2868 @cindex preserving with @code{.SECONDARY}
2870 The targets which @code{.SECONDARY} depends on are treated as
2871 intermediate files, except that they are never automatically deleted.
2872 @xref{Chained Rules, ,Chains of Implicit Rules}.
2874 @code{.SECONDARY} with no prerequisites causes all targets to be treated
2875 as secondary (i.e., no target is removed because it is considered
2876 intermediate).
2878 @findex .DELETE_ON_ERROR
2879 @item .DELETE_ON_ERROR
2880 @cindex removing targets on failure
2882 If @code{.DELETE_ON_ERROR} is mentioned as a target anywhere in the
2883 makefile, then @code{make} will delete the target of a rule if it has
2884 changed and its commands exit with a nonzero exit status, just as it
2885 does when it receives a signal.  @xref{Errors, ,Errors in Commands}.
2887 @findex .IGNORE
2888 @item .IGNORE
2890 If you specify prerequisites for @code{.IGNORE}, then @code{make} will
2891 ignore errors in execution of the commands run for those particular
2892 files.  The commands for @code{.IGNORE} are not meaningful.
2894 If mentioned as a target with no prerequisites, @code{.IGNORE} says to
2895 ignore errors in execution of commands for all files.  This usage of
2896 @samp{.IGNORE} is supported only for historical compatibility.  Since
2897 this affects every command in the makefile, it is not very useful; we
2898 recommend you use the more selective ways to ignore errors in specific
2899 commands.  @xref{Errors, ,Errors in Commands}.
2901 @findex .LOW_RESOLUTION_TIME
2902 @item .LOW_RESOLUTION_TIME
2904 If you specify prerequisites for @code{.LOW_RESOLUTION_TIME},
2905 @command{make} assumes that these files are created by commands that
2906 generate low resolution time stamps.  The commands for
2907 @code{.LOW_RESOLUTION_TIME} are not meaningful.
2909 The high resolution file time stamps of many modern hosts lessen the
2910 chance of @command{make} incorrectly concluding that a file is up to
2911 date.  Unfortunately, these hosts provide no way to set a high
2912 resolution file time stamp, so commands like @samp{cp -p} that
2913 explicitly set a file's time stamp must discard its subsecond part.  If
2914 a file is created by such a command, you should list it as a
2915 prerequisite of @code{.LOW_RESOLUTION_TIME} so that @command{make} does
2916 not mistakenly conclude that the file is out of date.  For example:
2918 @example
2919 @group
2920 .LOW_RESOLUTION_TIME: dst
2921 dst: src
2922         cp -p src dst
2923 @end group
2924 @end example
2926 Since @samp{cp -p} discards the subsecond part of @file{src}'s time
2927 stamp, @file{dst} is typically slightly older than @file{src} even when
2928 it is up to date.  The @code{.LOW_RESOLUTION_TIME} line causes
2929 @command{make} to consider @file{dst} to be up to date if its time stamp
2930 is at the start of the same second that @file{src}'s time stamp is in.
2932 Due to a limitation of the archive format, archive member time stamps
2933 are always low resolution.  You need not list archive members as
2934 prerequisites of @code{.LOW_RESOLUTION_TIME}, as @command{make} does this
2935 automatically.
2937 @findex .SILENT
2938 @item .SILENT
2940 If you specify prerequisites for @code{.SILENT}, then @code{make} will
2941 not print the commands to remake those particular files before executing
2942 them.  The commands for @code{.SILENT} are not meaningful.
2944 If mentioned as a target with no prerequisites, @code{.SILENT} says not
2945 to print any commands before executing them.  This usage of
2946 @samp{.SILENT} is supported only for historical compatibility.  We
2947 recommend you use the more selective ways to silence specific commands.
2948 @xref{Echoing, ,Command Echoing}.  If you want to silence all commands
2949 for a particular run of @code{make}, use the @samp{-s} or
2950 @w{@samp{--silent}} option (@pxref{Options Summary}).
2952 @findex .EXPORT_ALL_VARIABLES
2953 @item .EXPORT_ALL_VARIABLES
2955 Simply by being mentioned as a target, this tells @code{make} to
2956 export all variables to child processes by default.
2957 @xref{Variables/Recursion, ,Communicating Variables to a
2958 Sub-@code{make}}.
2960 @findex .NOTPARALLEL
2961 @item .NOTPARALLEL
2962 @cindex parallel execution, overriding
2964 If @code{.NOTPARALLEL} is mentioned as a target, then this invocation of
2965 @code{make} will be run serially, even if the @samp{-j} option is
2966 given.  Any recursively invoked @code{make} command will still be run in
2967 parallel (unless its makefile contains this target).  Any prerequisites
2968 on this target are ignored.
2969 @end table
2971 Any defined implicit rule suffix also counts as a special target if it
2972 appears as a target, and so does the concatenation of two suffixes, such
2973 as @samp{.c.o}.  These targets are suffix rules, an obsolete way of
2974 defining implicit rules (but a way still widely used).  In principle, any
2975 target name could be special in this way if you break it in two and add
2976 both pieces to the suffix list.  In practice, suffixes normally begin with
2977 @samp{.}, so these special target names also begin with @samp{.}.
2978 @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
2980 @node Multiple Targets, Multiple Rules, Special Targets, Rules
2981 @section Multiple Targets in a Rule
2982 @cindex multiple targets
2983 @cindex several targets in a rule
2984 @cindex targets, multiple
2985 @cindex rule, with multiple targets
2987 A rule with multiple targets is equivalent to writing many rules, each with
2988 one target, and all identical aside from that.  The same commands apply to
2989 all the targets, but their effects may vary because you can substitute the
2990 actual target name into the command using @samp{$@@}.  The rule contributes
2991 the same prerequisites to all the targets also.
2993 This is useful in two cases.
2995 @itemize @bullet
2996 @item
2997 You want just prerequisites, no commands.  For example:
2999 @example
3000 kbd.o command.o files.o: command.h
3001 @end example
3003 @noindent
3004 gives an additional prerequisite to each of the three object files
3005 mentioned.
3007 @item
3008 Similar commands work for all the targets.  The commands do not need
3009 to be absolutely identical, since the automatic variable @samp{$@@}
3010 can be used to substitute the particular target to be remade into the
3011 commands (@pxref{Automatic Variables}).  For example:
3013 @example
3014 @group
3015 bigoutput littleoutput : text.g
3016         generate text.g -$(subst output,,$@@) > $@@
3017 @end group
3018 @end example
3019 @findex subst
3021 @noindent
3022 is equivalent to
3024 @example
3025 bigoutput : text.g
3026         generate text.g -big > bigoutput
3027 littleoutput : text.g
3028         generate text.g -little > littleoutput
3029 @end example
3031 @noindent
3032 Here we assume the hypothetical program @code{generate} makes two
3033 types of output, one if given @samp{-big} and one if given
3034 @samp{-little}.
3035 @xref{Text Functions, ,Functions for String Substitution and Analysis},
3036 for an explanation of the @code{subst} function.
3037 @end itemize
3039 Suppose you would like to vary the prerequisites according to the target,
3040 much as the variable @samp{$@@} allows you to vary the commands.
3041 You cannot do this with multiple targets in an ordinary rule, but you can
3042 do it with a @dfn{static pattern rule}.
3043 @xref{Static Pattern, ,Static Pattern Rules}.
3045 @node Multiple Rules, Static Pattern, Multiple Targets, Rules
3046 @section Multiple Rules for One Target
3047 @cindex multiple rules for one target
3048 @cindex several rules for one target
3049 @cindex rule, multiple for one target
3050 @cindex target, multiple rules for one
3052 One file can be the target of several rules.  All the prerequisites
3053 mentioned in all the rules are merged into one list of prerequisites for
3054 the target.  If the target is older than any prerequisite from any rule,
3055 the commands are executed.
3057 There can only be one set of commands to be executed for a file.  If
3058 more than one rule gives commands for the same file, @code{make} uses
3059 the last set given and prints an error message.  (As a special case,
3060 if the file's name begins with a dot, no error message is printed.
3061 This odd behavior is only for compatibility with other implementations
3062 of @code{make}... you should avoid using it).  Occasionally it is
3063 useful to have the same target invoke multiple commands which are
3064 defined in different parts of your makefile; you can use
3065 @dfn{double-colon rules} (@pxref{Double-Colon}) for this.
3067 An extra rule with just prerequisites can be used to give a few extra
3068 prerequisites to many files at once.  For example, makefiles often
3069 have a variable, such as @code{objects}, containing a list of all the
3070 compiler output files in the system being made.  An easy way to say
3071 that all of them must be recompiled if @file{config.h} changes is to
3072 write the following:
3074 @example
3075 objects = foo.o bar.o
3076 foo.o : defs.h
3077 bar.o : defs.h test.h
3078 $(objects) : config.h
3079 @end example
3081 This could be inserted or taken out without changing the rules that really
3082 specify how to make the object files, making it a convenient form to use if
3083 you wish to add the additional prerequisite intermittently.
3085 Another wrinkle is that the additional prerequisites could be specified with
3086 a variable that you set with a command argument to @code{make}
3087 (@pxref{Overriding, ,Overriding Variables}).  For example,
3089 @example
3090 @group
3091 extradeps=
3092 $(objects) : $(extradeps)
3093 @end group
3094 @end example
3096 @noindent
3097 means that the command @samp{make extradeps=foo.h} will consider
3098 @file{foo.h} as a prerequisite of each object file, but plain @samp{make}
3099 will not.
3101 If none of the explicit rules for a target has commands, then @code{make}
3102 searches for an applicable implicit rule to find some commands
3103 @pxref{Implicit Rules, ,Using Implicit Rules}).
3105 @node Static Pattern, Double-Colon, Multiple Rules, Rules
3106 @section Static Pattern Rules
3107 @cindex static pattern rule
3108 @cindex rule, static pattern
3109 @cindex pattern rules, static (not implicit)
3110 @cindex varying prerequisites
3111 @cindex prerequisites, varying (static pattern)
3113 @dfn{Static pattern rules} are rules which specify multiple targets and
3114 construct the prerequisite names for each target based on the target name.
3115 They are more general than ordinary rules with multiple targets because the
3116 targets do not have to have identical prerequisites.  Their prerequisites must
3117 be @emph{analogous}, but not necessarily @emph{identical}.
3119 @menu
3120 * Static Usage::                The syntax of static pattern rules.
3121 * Static versus Implicit::      When are they better than implicit rules?
3122 @end menu
3124 @node Static Usage, Static versus Implicit, Static Pattern, Static Pattern
3125 @subsection Syntax of Static Pattern Rules
3126 @cindex static pattern rule, syntax of
3127 @cindex pattern rules, static, syntax of
3129 Here is the syntax of a static pattern rule:
3131 @example
3132 @var{targets} @dots{}: @var{target-pattern}: @var{prereq-patterns} @dots{}
3133         @var{commands}
3134         @dots{}
3135 @end example
3137 @noindent
3138 The @var{targets} list specifies the targets that the rule applies to.
3139 The targets can contain wildcard characters, just like the targets of
3140 ordinary rules (@pxref{Wildcards, ,Using Wildcard Characters in File
3141 Names}).
3143 @cindex target pattern, static (not implicit)
3144 @cindex stem
3145 The @var{target-pattern} and @var{prereq-patterns} say how to compute the
3146 prerequisites of each target.  Each target is matched against the
3147 @var{target-pattern} to extract a part of the target name, called the
3148 @dfn{stem}.  This stem is substituted into each of the @var{prereq-patterns}
3149 to make the prerequisite names (one from each @var{prereq-pattern}).
3151 Each pattern normally contains the character @samp{%} just once.  When the
3152 @var{target-pattern} matches a target, the @samp{%} can match any part of
3153 the target name; this part is called the @dfn{stem}.  The rest of the
3154 pattern must match exactly.  For example, the target @file{foo.o} matches
3155 the pattern @samp{%.o}, with @samp{foo} as the stem.  The targets
3156 @file{foo.c} and @file{foo.out} do not match that pattern.@refill
3158 @cindex prerequisite pattern, static (not implicit)
3159 The prerequisite names for each target are made by substituting the stem
3160 for the @samp{%} in each prerequisite pattern.  For example, if one
3161 prerequisite pattern is @file{%.c}, then substitution of the stem
3162 @samp{foo} gives the prerequisite name @file{foo.c}.  It is legitimate
3163 to write a prerequisite pattern that does not contain @samp{%}; then this
3164 prerequisite is the same for all targets.
3166 @cindex @code{%}, quoting in static pattern
3167 @cindex @code{%}, quoting with @code{\} (backslash)
3168 @cindex @code{\} (backslash), to quote @code{%}
3169 @cindex backslash (@code{\}), to quote @code{%}
3170 @cindex quoting @code{%}, in static pattern
3171 @samp{%} characters in pattern rules can be quoted with preceding
3172 backslashes (@samp{\}).  Backslashes that would otherwise quote @samp{%}
3173 characters can be quoted with more backslashes.  Backslashes that quote
3174 @samp{%} characters or other backslashes are removed from the pattern
3175 before it is compared to file names or has a stem substituted into it.
3176 Backslashes that are not in danger of quoting @samp{%} characters go
3177 unmolested.  For example, the pattern @file{the\%weird\\%pattern\\} has
3178 @samp{the%weird\} preceding the operative @samp{%} character, and
3179 @samp{pattern\\} following it.  The final two backslashes are left alone
3180 because they cannot affect any @samp{%} character.@refill
3182 Here is an example, which compiles each of @file{foo.o} and @file{bar.o}
3183 from the corresponding @file{.c} file:
3185 @example
3186 @group
3187 objects = foo.o bar.o
3189 all: $(objects)
3191 $(objects): %.o: %.c
3192         $(CC) -c $(CFLAGS) $< -o $@@
3193 @end group
3194 @end example
3196 @noindent
3197 Here @samp{$<} is the automatic variable that holds the name of the
3198 prerequisite and @samp{$@@} is the automatic variable that holds the name
3199 of the target; see @ref{Automatic Variables}.
3201 Each target specified must match the target pattern; a warning is issued
3202 for each target that does not.  If you have a list of files, only some of
3203 which will match the pattern, you can use the @code{filter} function to
3204 remove nonmatching file names (@pxref{Text Functions, ,Functions for String Substitution and Analysis}):
3206 @example
3207 files = foo.elc bar.o lose.o
3209 $(filter %.o,$(files)): %.o: %.c
3210         $(CC) -c $(CFLAGS) $< -o $@@
3211 $(filter %.elc,$(files)): %.elc: %.el
3212         emacs -f batch-byte-compile $<
3213 @end example
3215 @noindent
3216 In this example the result of @samp{$(filter %.o,$(files))} is
3217 @file{bar.o lose.o}, and the first static pattern rule causes each of
3218 these object files to be updated by compiling the corresponding C source
3219 file.  The result of @w{@samp{$(filter %.elc,$(files))}} is
3220 @file{foo.elc}, so that file is made from @file{foo.el}.@refill
3222 Another example shows how to use @code{$*} in static pattern rules:
3223 @vindex $*@r{, and static pattern}
3225 @example
3226 @group
3227 bigoutput littleoutput : %output : text.g
3228         generate text.g -$* > $@@
3229 @end group
3230 @end example
3232 @noindent
3233 When the @code{generate} command is run, @code{$*} will expand to the
3234 stem, either @samp{big} or @samp{little}.
3236 @node Static versus Implicit,  , Static Usage, Static Pattern
3237 @subsection Static Pattern Rules versus Implicit Rules
3238 @cindex rule, static pattern versus implicit
3239 @cindex static pattern rule, versus implicit
3241 A static pattern rule has much in common with an implicit rule defined as a
3242 pattern rule (@pxref{Pattern Rules, ,Defining and Redefining Pattern Rules}).
3243 Both have a pattern for the target and patterns for constructing the
3244 names of prerequisites.  The difference is in how @code{make} decides
3245 @emph{when} the rule applies.
3247 An implicit rule @emph{can} apply to any target that matches its pattern,
3248 but it @emph{does} apply only when the target has no commands otherwise
3249 specified, and only when the prerequisites can be found.  If more than one
3250 implicit rule appears applicable, only one applies; the choice depends on
3251 the order of rules.
3253 By contrast, a static pattern rule applies to the precise list of targets
3254 that you specify in the rule.  It cannot apply to any other target and it
3255 invariably does apply to each of the targets specified.  If two conflicting
3256 rules apply, and both have commands, that's an error.
3258 The static pattern rule can be better than an implicit rule for these
3259 reasons:
3261 @itemize @bullet
3262 @item
3263 You may wish to override the usual implicit rule for a few
3264 files whose names cannot be categorized syntactically but
3265 can be given in an explicit list.
3267 @item
3268 If you cannot be sure of the precise contents of the directories
3269 you are using, you may not be sure which other irrelevant files
3270 might lead @code{make} to use the wrong implicit rule.  The choice
3271 might depend on the order in which the implicit rule search is done.
3272 With static pattern rules, there is no uncertainty: each rule applies
3273 to precisely the targets specified.
3274 @end itemize
3276 @node Double-Colon, Automatic Prerequisites, Static Pattern, Rules
3277 @section Double-Colon Rules
3278 @cindex double-colon rules
3279 @cindex rule, double-colon (@code{::})
3280 @cindex multiple rules for one target (@code{::})
3281 @cindex @code{::} rules (double-colon)
3283 @dfn{Double-colon} rules are rules written with @samp{::} instead of
3284 @samp{:} after the target names.  They are handled differently from
3285 ordinary rules when the same target appears in more than one rule.
3287 When a target appears in multiple rules, all the rules must be the same
3288 type: all ordinary, or all double-colon.  If they are double-colon, each
3289 of them is independent of the others.  Each double-colon rule's commands
3290 are executed if the target is older than any prerequisites of that rule.
3291 If there are no prerequisites for that rule, its commands are always
3292 executed (even if the target already exists).  This can result in
3293 executing none, any, or all of the double-colon rules.
3295 Double-colon rules with the same target are in fact completely separate
3296 from one another.  Each double-colon rule is processed individually, just
3297 as rules with different targets are processed.
3299 The double-colon rules for a target are executed in the order they appear
3300 in the makefile.  However, the cases where double-colon rules really make
3301 sense are those where the order of executing the commands would not matter.
3303 Double-colon rules are somewhat obscure and not often very useful; they
3304 provide a mechanism for cases in which the method used to update a target
3305 differs depending on which prerequisite files caused the update, and such
3306 cases are rare.
3308 Each double-colon rule should specify commands; if it does not, an
3309 implicit rule will be used if one applies.
3310 @xref{Implicit Rules, ,Using Implicit Rules}.
3312 @node Automatic Prerequisites,  , Double-Colon, Rules
3313 @section Generating Prerequisites Automatically
3314 @cindex prerequisites, automatic generation
3315 @cindex automatic generation of prerequisites
3316 @cindex generating prerequisites automatically
3318 In the makefile for a program, many of the rules you need to write often
3319 say only that some object file depends on some header
3320 file.  For example, if @file{main.c} uses @file{defs.h} via an
3321 @code{#include}, you would write:
3323 @example
3324 main.o: defs.h
3325 @end example
3327 @noindent
3328 You need this rule so that @code{make} knows that it must remake
3329 @file{main.o} whenever @file{defs.h} changes.  You can see that for a
3330 large program you would have to write dozens of such rules in your
3331 makefile.  And, you must always be very careful to update the makefile
3332 every time you add or remove an @code{#include}.
3333 @cindex @code{#include}
3335 @cindex @code{-M} (to compiler)
3336 To avoid this hassle, most modern C compilers can write these rules for
3337 you, by looking at the @code{#include} lines in the source files.
3338 Usually this is done with the @samp{-M} option to the compiler.
3339 For example, the command:
3341 @example
3342 cc -M main.c
3343 @end example
3345 @noindent
3346 generates the output:
3348 @example
3349 main.o : main.c defs.h
3350 @end example
3352 @noindent
3353 Thus you no longer have to write all those rules yourself.
3354 The compiler will do it for you.
3356 Note that such a prerequisite constitutes mentioning @file{main.o} in a
3357 makefile, so it can never be considered an intermediate file by implicit
3358 rule search.  This means that @code{make} won't ever remove the file
3359 after using it; @pxref{Chained Rules, ,Chains of Implicit Rules}.
3361 @cindex @code{make depend}
3362 With old @code{make} programs, it was traditional practice to use this
3363 compiler feature to generate prerequisites on demand with a command like
3364 @samp{make depend}.  That command would create a file @file{depend}
3365 containing all the automatically-generated prerequisites; then the
3366 makefile could use @code{include} to read them in (@pxref{Include}).
3368 In GNU @code{make}, the feature of remaking makefiles makes this
3369 practice obsolete---you need never tell @code{make} explicitly to
3370 regenerate the prerequisites, because it always regenerates any makefile
3371 that is out of date.  @xref{Remaking Makefiles}.
3373 The practice we recommend for automatic prerequisite generation is to have
3374 one makefile corresponding to each source file.  For each source file
3375 @file{@var{name}.c} there is a makefile @file{@var{name}.d} which lists
3376 what files the object file @file{@var{name}.o} depends on.  That way
3377 only the source files that have changed need to be rescanned to produce
3378 the new prerequisites.
3380 Here is the pattern rule to generate a file of prerequisites (i.e., a makefile)
3381 called @file{@var{name}.d} from a C source file called @file{@var{name}.c}:
3383 @smallexample
3384 @group
3385 %.d: %.c
3386         @set -e; rm -f $@@; \
3387          $(CC) -M $(CPPFLAGS) $< > $@@.$$$$; \
3388          sed 's,\($*\)\.o[ :]*,\1.o $@@ : ,g' < $@@.$$$$ > $@@; \
3389          rm -f $@@.$$$$
3390 @end group
3391 @end smallexample
3393 @noindent
3394 @xref{Pattern Rules}, for information on defining pattern rules.  The
3395 @samp{-e} flag to the shell causes it to exit immediately if the
3396 @code{$(CC)} command (or any other command) fails (exits with a
3397 nonzero status).
3398 @cindex @code{-e} (shell flag)
3400 @cindex @code{-MM} (to GNU compiler)
3401 With the GNU C compiler, you may wish to use the @samp{-MM} flag instead
3402 of @samp{-M}.  This omits prerequisites on system header files.
3403 @xref{Preprocessor Options, , Options Controlling the Preprocessor,
3404 gcc.info, Using GNU CC}, for details.
3406 @cindex @code{sed} (shell command)
3407 The purpose of the @code{sed} command is to translate (for example):
3409 @example
3410 main.o : main.c defs.h
3411 @end example
3413 @noindent
3414 into:
3416 @example
3417 main.o main.d : main.c defs.h
3418 @end example
3420 @noindent
3421 @cindex @code{.d}
3422 This makes each @samp{.d} file depend on all the source and header files
3423 that the corresponding @samp{.o} file depends on.  @code{make} then
3424 knows it must regenerate the prerequisites whenever any of the source or
3425 header files changes.
3427 Once you've defined the rule to remake the @samp{.d} files,
3428 you then use the @code{include} directive to read them all in.
3429 @xref{Include}.  For example:
3431 @example
3432 @group
3433 sources = foo.c bar.c
3435 include $(sources:.c=.d)
3436 @end group
3437 @end example
3439 @noindent
3440 (This example uses a substitution variable reference to translate the
3441 list of source files @samp{foo.c bar.c} into a list of prerequisite
3442 makefiles, @samp{foo.d bar.d}.  @xref{Substitution Refs}, for full
3443 information on substitution references.)  Since the @samp{.d} files are
3444 makefiles like any others, @code{make} will remake them as necessary
3445 with no further work from you.  @xref{Remaking Makefiles}.
3447 Note that the @samp{.d} files contain target definitions; you should
3448 be sure to place the @code{include} directive @emph{after} the first,
3449 default goal in your makefiles or run the risk of having a random
3450 object file become the default goal.
3451 @xref{How Make Works}.
3453 @node Commands, Using Variables, Rules, Top
3454 @chapter Writing the Commands in Rules
3455 @cindex commands, how to write
3456 @cindex rule commands
3457 @cindex writing rule commands
3459 The commands of a rule consist of shell command lines to be executed one
3460 by one.  Each command line must start with a tab, except that the first
3461 command line may be attached to the target-and-prerequisites line with a
3462 semicolon in between.  Blank lines and lines of just comments may appear
3463 among the command lines; they are ignored.  (But beware, an apparently
3464 ``blank'' line that begins with a tab is @emph{not} blank!  It is an
3465 empty command; @pxref{Empty Commands}.)
3467 Users use many different shell programs, but commands in makefiles are
3468 always interpreted by @file{/bin/sh} unless the makefile specifies
3469 otherwise.  @xref{Execution, ,Command Execution}.
3471 @cindex comments, in commands
3472 @cindex commands, comments in
3473 @cindex @code{#} (comments), in commands
3474 The shell that is in use determines whether comments can be written on
3475 command lines, and what syntax they use.  When the shell is
3476 @file{/bin/sh}, a @samp{#} starts a comment that extends to the end of
3477 the line.  The @samp{#} does not have to be at the beginning of a line.
3478 Text on a line before a @samp{#} is not part of the comment.
3480 @menu
3481 * Echoing::                     How to control when commands are echoed.
3482 * Execution::                   How commands are executed.
3483 * Parallel::                    How commands can be executed in parallel.
3484 * Errors::                      What happens after a command execution error.
3485 * Interrupts::                  What happens when a command is interrupted.
3486 * Recursion::                   Invoking @code{make} from makefiles.
3487 * Sequences::                   Defining canned sequences of commands.
3488 * Empty Commands::              Defining useful, do-nothing commands.
3489 @end menu
3491 @node Echoing, Execution, Commands, Commands
3492 @section Command Echoing
3493 @cindex echoing of commands
3494 @cindex silent operation
3495 @cindex @code{@@} (in commands)
3496 @cindex commands, echoing
3497 @cindex printing of commands
3499 Normally @code{make} prints each command line before it is executed.
3500 We call this @dfn{echoing} because it gives the appearance that you
3501 are typing the commands yourself.
3503 When a line starts with @samp{@@}, the echoing of that line is suppressed.
3504 The @samp{@@} is discarded before the command is passed to the shell.
3505 Typically you would use this for a command whose only effect is to print
3506 something, such as an @code{echo} command to indicate progress through
3507 the makefile:
3509 @example
3510 @@echo About to make distribution files
3511 @end example
3513 @cindex @code{-n}
3514 @cindex @code{--just-print}
3515 @cindex @code{--dry-run}
3516 @cindex @code{--recon}
3517 When @code{make} is given the flag @samp{-n} or @samp{--just-print}
3518 it only echoes commands, it won't execute them.  @xref{Options Summary,
3519 ,Summary of Options}.  In this case and only this case, even the
3520 commands starting with @samp{@@} are printed.  This flag is useful for
3521 finding out which commands @code{make} thinks are necessary without
3522 actually doing them.
3524 @cindex @code{-s}
3525 @cindex @code{--silent}
3526 @cindex @code{--quiet}
3527 @findex .SILENT
3528 The @samp{-s} or @samp{--silent}
3529 flag to @code{make} prevents all echoing, as if all commands
3530 started with @samp{@@}.  A rule in the makefile for the special target
3531 @code{.SILENT} without prerequisites has the same effect
3532 (@pxref{Special Targets, ,Special Built-in Target Names}).
3533 @code{.SILENT} is essentially obsolete since @samp{@@} is more flexible.@refill
3535 @node Execution, Parallel, Echoing, Commands
3536 @section Command Execution
3537 @cindex commands, execution
3538 @cindex execution, of commands
3539 @cindex shell command, execution
3540 @vindex SHELL @r{(command execution)}
3542 When it is time to execute commands to update a target, they are executed
3543 by making a new subshell for each line.  (In practice, @code{make} may
3544 take shortcuts that do not affect the results.)
3546 @cindex @code{cd} (shell command)
3547 @strong{Please note:} this implies that shell commands such as @code{cd}
3548 that set variables local to each process will not affect the following
3549 command lines. @footnote{On MS-DOS, the value of current working
3550 directory is @strong{global}, so changing it @emph{will} affect the
3551 following command lines on those systems.}  If you want to use @code{cd}
3552 to affect the next command, put the two on a single line with a
3553 semicolon between them.  Then @code{make} will consider them a single
3554 command and pass them, together, to a shell which will execute them in
3555 sequence.  For example:
3557 @example
3558 foo : bar/lose
3559         cd bar; gobble lose > ../foo
3560 @end example
3562 @cindex commands, backslash (@code{\}) in
3563 @cindex commands, quoting newlines in
3564 @cindex backslash (@code{\}), in commands
3565 @cindex @code{\} (backslash), in commands
3566 @cindex quoting newline, in commands
3567 @cindex newline, quoting, in commands
3568 If you would like to split a single shell command into multiple lines of
3569 text, you must use a backslash at the end of all but the last subline.
3570 Such a sequence of lines is combined into a single line, by deleting the
3571 backslash-newline sequences, before passing it to the shell.  Thus, the
3572 following is equivalent to the preceding example:
3574 @example
3575 @group
3576 foo : bar/lose
3577         cd bar;  \
3578         gobble lose > ../foo
3579 @end group
3580 @end example
3582 @vindex SHELL
3583 The program used as the shell is taken from the variable @code{SHELL}.
3584 By default, the program @file{/bin/sh} is used.
3586 @vindex COMSPEC
3587 On MS-DOS, if @code{SHELL} is not set, the value of the variable
3588 @code{COMSPEC} (which is always set) is used instead.
3590 @cindex @code{SHELL}, MS-DOS specifics
3591 The processing of lines that set the variable @code{SHELL} in Makefiles
3592 is different on MS-DOS.  The stock shell, @file{command.com}, is
3593 ridiculously limited in its functionality and many users of @code{make}
3594 tend to install a replacement shell.  Therefore, on MS-DOS, @code{make}
3595 examines the value of @code{SHELL}, and changes its behavior based on
3596 whether it points to a Unix-style or DOS-style shell.  This allows
3597 reasonable functionality even if @code{SHELL} points to
3598 @file{command.com}.
3600 If @code{SHELL} points to a Unix-style shell, @code{make} on MS-DOS
3601 additionally checks whether that shell can indeed be found; if not, it
3602 ignores the line that sets @code{SHELL}.  In MS-DOS, GNU @code{make}
3603 searches for the shell in the following places:
3605 @enumerate
3606 @item
3607 In the precise place pointed to by the value of @code{SHELL}.  For
3608 example, if the makefile specifies @samp{SHELL = /bin/sh}, @code{make}
3609 will look in the directory @file{/bin} on the current drive.
3611 @item
3612 In the current directory.
3614 @item
3615 In each of the directories in the @code{PATH} variable, in order.
3617 @end enumerate
3619 In every directory it examines, @code{make} will first look for the
3620 specific file (@file{sh} in the example above).  If this is not found,
3621 it will also look in that directory for that file with one of the known
3622 extensions which identify executable files.  For example @file{.exe},
3623 @file{.com}, @file{.bat}, @file{.btm}, @file{.sh}, and some others.
3625 If any of these attempts is successful, the value of @code{SHELL} will
3626 be set to the full pathname of the shell as found.  However, if none of
3627 these is found, the value of @code{SHELL} will not be changed, and thus
3628 the line that sets it will be effectively ignored.  This is so
3629 @code{make} will only support features specific to a Unix-style shell if
3630 such a shell is actually installed on the system where @code{make} runs.
3632 Note that this extended search for the shell is limited to the cases
3633 where @code{SHELL} is set from the Makefile; if it is set in the
3634 environment or command line, you are expected to set it to the full
3635 pathname of the shell, exactly as things are on Unix.
3637 The effect of the above DOS-specific processing is that a Makefile that
3638 says @samp{SHELL = /bin/sh} (as many Unix makefiles do), will work
3639 on MS-DOS unaltered if you have e.g. @file{sh.exe} installed in some
3640 directory along your @code{PATH}.
3642 @cindex environment, @code{SHELL} in
3643 @vindex MAKESHELL @r{(MS-DOS alternative to @code{SHELL})}
3644 Unlike most variables, the variable @code{SHELL} is never set from the
3645 environment.  This is because the @code{SHELL} environment variable is
3646 used to specify your personal choice of shell program for interactive
3647 use.  It would be very bad for personal choices like this to affect the
3648 functioning of makefiles.  @xref{Environment, ,Variables from the
3649 Environment}.  However, on MS-DOS and MS-Windows the value of
3650 @code{SHELL} in the environment @strong{is} used, since on those systems
3651 most users do not set this variable, and therefore it is most likely set
3652 specifically to be used by @code{make}.  On MS-DOS, if the setting of
3653 @code{SHELL} is not suitable for @code{make}, you can set the variable
3654 @code{MAKESHELL} to the shell that @code{make} should use; this will
3655 override the value of @code{SHELL}.
3657 @node Parallel, Errors, Execution, Commands
3658 @section Parallel Execution
3659 @cindex commands, execution in parallel
3660 @cindex parallel execution
3661 @cindex execution, in parallel
3662 @cindex job slots
3663 @cindex @code{-j}
3664 @cindex @code{--jobs}
3666 GNU @code{make} knows how to execute several commands at once.
3667 Normally, @code{make} will execute only one command at a time, waiting
3668 for it to finish before executing the next.  However, the @samp{-j} or
3669 @samp{--jobs} option tells @code{make} to execute many commands
3670 simultaneously.@refill
3672 On MS-DOS, the @samp{-j} option has no effect, since that system doesn't
3673 support multi-processing.
3675 If the @samp{-j} option is followed by an integer, this is the number of
3676 commands to execute at once; this is called the number of @dfn{job slots}.
3677 If there is nothing looking like an integer after the @samp{-j} option,
3678 there is no limit on the number of job slots.  The default number of job
3679 slots is one, which means serial execution (one thing at a time).
3681 One unpleasant consequence of running several commands simultaneously is
3682 that output generated by the commands appears whenever each command
3683 sends it, so messages from different commands may be interspersed.
3685 Another problem is that two processes cannot both take input from the
3686 same device; so to make sure that only one command tries to take input
3687 from the terminal at once, @code{make} will invalidate the standard
3688 input streams of all but one running command.  This means that
3689 attempting to read from standard input will usually be a fatal error (a
3690 @samp{Broken pipe} signal) for most child processes if there are
3691 several.
3692 @cindex broken pipe
3693 @cindex standard input
3695 It is unpredictable which command will have a valid standard input stream
3696 (which will come from the terminal, or wherever you redirect the standard
3697 input of @code{make}).  The first command run will always get it first, and
3698 the first command started after that one finishes will get it next, and so
3701 We will change how this aspect of @code{make} works if we find a better
3702 alternative.  In the mean time, you should not rely on any command using
3703 standard input at all if you are using the parallel execution feature; but
3704 if you are not using this feature, then standard input works normally in
3705 all commands.
3707 Finally, handling recursive @code{make} invocations raises issues.  For
3708 more information on this, see
3709 @ref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
3711 If a command fails (is killed by a signal or exits with a nonzero
3712 status), and errors are not ignored for that command
3713 (@pxref{Errors, ,Errors in Commands}),
3714 the remaining command lines to remake the same target will not be run.
3715 If a command fails and the @samp{-k} or @samp{--keep-going}
3716 option was not given
3717 (@pxref{Options Summary, ,Summary of Options}),
3718 @code{make} aborts execution.  If make
3719 terminates for any reason (including a signal) with child processes
3720 running, it waits for them to finish before actually exiting.@refill
3722 @cindex load average
3723 @cindex limiting jobs based on load
3724 @cindex jobs, limiting based on load
3725 @cindex @code{-l} (load average)
3726 @cindex @code{--max-load}
3727 @cindex @code{--load-average}
3728 When the system is heavily loaded, you will probably want to run fewer jobs
3729 than when it is lightly loaded.  You can use the @samp{-l} option to tell
3730 @code{make} to limit the number of jobs to run at once, based on the load
3731 average.  The @samp{-l} or @samp{--max-load}
3732 option is followed by a floating-point number.  For
3733 example,
3735 @example
3736 -l 2.5
3737 @end example
3739 @noindent
3740 will not let @code{make} start more than one job if the load average is
3741 above 2.5.  The @samp{-l} option with no following number removes the
3742 load limit, if one was given with a previous @samp{-l} option.@refill
3744 More precisely, when @code{make} goes to start up a job, and it already has
3745 at least one job running, it checks the current load average; if it is not
3746 lower than the limit given with @samp{-l}, @code{make} waits until the load
3747 average goes below that limit, or until all the other jobs finish.
3749 By default, there is no load limit.
3751 @node Errors, Interrupts, Parallel, Commands
3752 @section Errors in Commands
3753 @cindex errors (in commands)
3754 @cindex commands, errors in
3755 @cindex exit status (errors)
3757 After each shell command returns, @code{make} looks at its exit status.
3758 If the command completed successfully, the next command line is executed
3759 in a new shell; after the last command line is finished, the rule is
3760 finished.
3762 If there is an error (the exit status is nonzero), @code{make} gives up on
3763 the current rule, and perhaps on all rules.
3765 Sometimes the failure of a certain command does not indicate a problem.
3766 For example, you may use the @code{mkdir} command to ensure that a
3767 directory exists.  If the directory already exists, @code{mkdir} will
3768 report an error, but you probably want @code{make} to continue regardless.
3770 @cindex @code{-} (in commands)
3771 To ignore errors in a command line, write a @samp{-} at the beginning of
3772 the line's text (after the initial tab).  The @samp{-} is discarded before
3773 the command is passed to the shell for execution.
3775 For example,
3777 @example
3778 @group
3779 clean:
3780         -rm -f *.o
3781 @end group
3782 @end example
3783 @cindex @code{rm} (shell command)
3785 @noindent
3786 This causes @code{rm} to continue even if it is unable to remove a file.
3788 @cindex @code{-i}
3789 @cindex @code{--ignore-errors}
3790 @findex .IGNORE
3791 When you run @code{make} with the @samp{-i} or @samp{--ignore-errors}
3792 flag, errors are ignored in all commands of all rules.  A rule in the
3793 makefile for the special target @code{.IGNORE} has the same effect, if
3794 there are no prerequisites.  These ways of ignoring errors are obsolete
3795 because @samp{-} is more flexible.
3797 When errors are to be ignored, because of either a @samp{-} or the
3798 @samp{-i} flag, @code{make} treats an error return just like success,
3799 except that it prints out a message that tells you the status code
3800 the command exited with, and says that the error has been ignored.
3802 When an error happens that @code{make} has not been told to ignore,
3803 it implies that the current target cannot be correctly remade, and neither
3804 can any other that depends on it either directly or indirectly.  No further
3805 commands will be executed for these targets, since their preconditions
3806 have not been achieved.
3809 @cindex @code{-k}
3810 @cindex @code{--keep-going}
3811 Normally @code{make} gives up immediately in this circumstance, returning a
3812 nonzero status.  However, if the @samp{-k} or @samp{--keep-going}
3813 flag is specified, @code{make}
3814 continues to consider the other prerequisites of the pending targets,
3815 remaking them if necessary, before it gives up and returns nonzero status.
3816 For example, after an error in compiling one object file, @samp{make -k}
3817 will continue compiling other object files even though it already knows
3818 that linking them will be impossible.  @xref{Options Summary, ,Summary of Options}.
3820 The usual behavior assumes that your purpose is to get the specified
3821 targets up to date; once @code{make} learns that this is impossible, it
3822 might as well report the failure immediately.  The @samp{-k} option says
3823 that the real purpose is to test as many of the changes made in the
3824 program as possible, perhaps to find several independent problems so
3825 that you can correct them all before the next attempt to compile.  This
3826 is why Emacs' @code{compile} command passes the @samp{-k} flag by
3827 default.
3828 @cindex Emacs (@code{M-x compile})
3830 @findex .DELETE_ON_ERROR
3831 @cindex deletion of target files
3832 @cindex removal of target files
3833 @cindex target, deleting on error
3834 Usually when a command fails, if it has changed the target file at all,
3835 the file is corrupted and cannot be used---or at least it is not
3836 completely updated.  Yet the file's time stamp says that it is now up to
3837 date, so the next time @code{make} runs, it will not try to update that
3838 file.  The situation is just the same as when the command is killed by a
3839 signal; @pxref{Interrupts}.  So generally the right thing to do is to
3840 delete the target file if the command fails after beginning to change
3841 the file.  @code{make} will do this if @code{.DELETE_ON_ERROR} appears
3842 as a target.  This is almost always what you want @code{make} to do, but
3843 it is not historical practice; so for compatibility, you must explicitly
3844 request it.
3846 @node Interrupts, Recursion, Errors, Commands
3847 @section Interrupting or Killing @code{make}
3848 @cindex interrupt
3849 @cindex signal
3850 @cindex deletion of target files
3851 @cindex removal of target files
3852 @cindex target, deleting on interrupt
3853 @cindex killing (interruption)
3855 If @code{make} gets a fatal signal while a command is executing, it may
3856 delete the target file that the command was supposed to update.  This is
3857 done if the target file's last-modification time has changed since
3858 @code{make} first checked it.
3860 The purpose of deleting the target is to make sure that it is remade from
3861 scratch when @code{make} is next run.  Why is this?  Suppose you type
3862 @kbd{Ctrl-c} while a compiler is running, and it has begun to write an
3863 object file @file{foo.o}.  The @kbd{Ctrl-c} kills the compiler, resulting
3864 in an incomplete file whose last-modification time is newer than the source
3865 file @file{foo.c}.  But @code{make} also receives the @kbd{Ctrl-c} signal
3866 and deletes this incomplete file.  If @code{make} did not do this, the next
3867 invocation of @code{make} would think that @file{foo.o} did not require
3868 updating---resulting in a strange error message from the linker when it
3869 tries to link an object file half of which is missing.
3871 @findex .PRECIOUS
3872 You can prevent the deletion of a target file in this way by making the
3873 special target @code{.PRECIOUS} depend on it.  Before remaking a target,
3874 @code{make} checks to see whether it appears on the prerequisites of
3875 @code{.PRECIOUS}, and thereby decides whether the target should be deleted
3876 if a signal happens.  Some reasons why you might do this are that the
3877 target is updated in some atomic fashion, or exists only to record a
3878 modification-time (its contents do not matter), or must exist at all
3879 times to prevent other sorts of trouble.
3881 @node Recursion, Sequences, Interrupts, Commands
3882 @section Recursive Use of @code{make}
3883 @cindex recursion
3884 @cindex subdirectories, recursion for
3886 Recursive use of @code{make} means using @code{make} as a command in a
3887 makefile.  This technique is useful when you want separate makefiles for
3888 various subsystems that compose a larger system.  For example, suppose you
3889 have a subdirectory @file{subdir} which has its own makefile, and you would
3890 like the containing directory's makefile to run @code{make} on the
3891 subdirectory.  You can do it by writing this:
3893 @example
3894 subsystem:
3895         cd subdir && $(MAKE)
3896 @end example
3898 @noindent
3899 or, equivalently, this (@pxref{Options Summary, ,Summary of Options}):
3901 @example
3902 subsystem:
3903         $(MAKE) -C subdir
3904 @end example
3905 @cindex @code{-C}
3906 @cindex @code{--directory}
3908 You can write recursive @code{make} commands just by copying this example,
3909 but there are many things to know about how they work and why, and about
3910 how the sub-@code{make} relates to the top-level @code{make}.  You may
3911 also find it useful to declare targets that invoke recursive
3912 @code{make} commands as @samp{.PHONY} (for more discussion on when
3913 this is useful, see @ref{Phony Targets}).
3915 @vindex @code{CURDIR}
3916 For your convenience, when GNU @code{make} starts (after it has
3917 processed any @code{-C} options) it sets the variable @code{CURDIR} to
3918 the pathname of the current working directory.  This value is never
3919 touched by @code{make} again: in particular note that if you include
3920 files from other directories the value of @code{CURDIR} does not
3921 change.  The value has the same precedence it would have if it were
3922 set in the makefile (by default, an environment variable @code{CURDIR}
3923 will not override this value).  Note that setting this variable has no
3924 impact on the operation of @code{make} (it does not cause @code{make}
3925 to change its working directory, for example).
3927 @menu
3928 * MAKE Variable::               The special effects of using @samp{$(MAKE)}.
3929 * Variables/Recursion::         How to communicate variables to a sub-@code{make}.
3930 * Options/Recursion::           How to communicate options to a sub-@code{make}.
3931 * -w Option::                   How the @samp{-w} or @samp{--print-directory} option
3932                                   helps debug use of recursive @code{make} commands.
3933 @end menu
3935 @node MAKE Variable, Variables/Recursion, Recursion, Recursion
3936 @subsection How the @code{MAKE} Variable Works
3937 @vindex MAKE
3938 @cindex recursion, and @code{MAKE} variable
3940 Recursive @code{make} commands should always use the variable @code{MAKE},
3941 not the explicit command name @samp{make}, as shown here:
3943 @example
3944 @group
3945 subsystem:
3946         cd subdir && $(MAKE)
3947 @end group
3948 @end example
3950 The value of this variable is the file name with which @code{make} was
3951 invoked.  If this file name was @file{/bin/make}, then the command executed
3952 is @samp{cd subdir && /bin/make}.  If you use a special version of
3953 @code{make} to run the top-level makefile, the same special version will be
3954 executed for recursive invocations.
3955 @cindex @code{cd} (shell command)
3957 @cindex +, and commands
3958 As a special feature, using the variable @code{MAKE} in the commands of
3959 a rule alters the effects of the @samp{-t} (@samp{--touch}), @samp{-n}
3960 (@samp{--just-print}), or @samp{-q} (@w{@samp{--question}}) option.
3961 Using the @code{MAKE} variable has the same effect as using a @samp{+}
3962 character at the beginning of the command line.  @xref{Instead of
3963 Execution, ,Instead of Executing the Commands}.  This special feature
3964 is only enabled if the @code{MAKE} variable appears directly in the
3965 command script: it does not apply if the @code{MAKE} variable is
3966 referenced through expansion of another variable.  In the latter case
3967 you must use the @samp{+} token to get these special effects.@refill
3969 Consider the command @samp{make -t} in the above example.  (The
3970 @samp{-t} option marks targets as up to date without actually running
3971 any commands; see @ref{Instead of Execution}.)  Following the usual
3972 definition of @samp{-t}, a @samp{make -t} command in the example would
3973 create a file named @file{subsystem} and do nothing else.  What you
3974 really want it to do is run @samp{@w{cd subdir &&} @w{make -t}}; but that would
3975 require executing the command, and @samp{-t} says not to execute
3976 commands.@refill
3977 @cindex @code{-t}, and recursion
3978 @cindex recursion, and @code{-t}
3979 @cindex @code{--touch}, and recursion
3981 The special feature makes this do what you want: whenever a command
3982 line of a rule contains the variable @code{MAKE}, the flags @samp{-t},
3983 @samp{-n} and @samp{-q} do not apply to that line.  Command lines
3984 containing @code{MAKE} are executed normally despite the presence of a
3985 flag that causes most commands not to be run.  The usual
3986 @code{MAKEFLAGS} mechanism passes the flags to the sub-@code{make}
3987 (@pxref{Options/Recursion, ,Communicating Options to a
3988 Sub-@code{make}}), so your request to touch the files, or print the
3989 commands, is propagated to the subsystem.@refill
3991 @node Variables/Recursion, Options/Recursion, MAKE Variable, Recursion
3992 @subsection Communicating Variables to a Sub-@code{make}
3993 @cindex sub-@code{make}
3994 @cindex environment, and recursion
3995 @cindex exporting variables
3996 @cindex variables, environment
3997 @cindex variables, exporting
3998 @cindex recursion, and environment
3999 @cindex recursion, and variables
4001 Variable values of the top-level @code{make} can be passed to the
4002 sub-@code{make} through the environment by explicit request.  These
4003 variables are defined in the sub-@code{make} as defaults, but do not
4004 override what is specified in the makefile used by the sub-@code{make}
4005 makefile unless you use the @samp{-e} switch (@pxref{Options Summary,
4006 ,Summary of Options}).@refill
4008 To pass down, or @dfn{export}, a variable, @code{make} adds the variable
4009 and its value to the environment for running each command.  The
4010 sub-@code{make}, in turn, uses the environment to initialize its table
4011 of variable values.  @xref{Environment, ,Variables from the
4012 Environment}.
4014 Except by explicit request, @code{make} exports a variable only if it
4015 is either defined in the environment initially or set on the command
4016 line, and if its name consists only of letters, numbers, and underscores.
4017 Some shells cannot cope with environment variable names consisting of
4018 characters other than letters, numbers, and underscores.
4020 @cindex SHELL, exported value
4021 The value of the @code{make} variable @code{SHELL} is not exported.
4022 Instead, the value of the @code{SHELL} variable from the invoking
4023 environment is passed to the sub-@code{make}.  You can force
4024 @code{make} to export its value for @code{SHELL} by using the
4025 @code{export} directive, described below.
4027 The special variable @code{MAKEFLAGS} is always exported (unless you
4028 unexport it).  @code{MAKEFILES} is exported if you set it to anything.
4030 @code{make} automatically passes down variable values that were defined
4031 on the command line, by putting them in the @code{MAKEFLAGS} variable.
4032 @iftex
4033 See the next section.
4034 @end iftex
4035 @ifnottex
4036 @xref{Options/Recursion}.
4037 @end ifnottex
4039 Variables are @emph{not} normally passed down if they were created by
4040 default by @code{make} (@pxref{Implicit Variables, ,Variables Used by
4041 Implicit Rules}).  The sub-@code{make} will define these for
4042 itself.@refill
4044 @findex export
4045 If you want to export specific variables to a sub-@code{make}, use the
4046 @code{export} directive, like this:
4048 @example
4049 export @var{variable} @dots{}
4050 @end example
4052 @noindent
4053 @findex unexport
4054 If you want to @emph{prevent} a variable from being exported, use the
4055 @code{unexport} directive, like this:
4057 @example
4058 unexport @var{variable} @dots{}
4059 @end example
4061 @noindent
4062 In both of these forms, the arguments to @code{export} and
4063 @code{unexport} are expanded, and so could be variables or functions
4064 which expand to a (list of) variable names to be (un)exported.
4066 As a convenience, you can define a variable and export it at the same
4067 time by doing:
4069 @example
4070 export @var{variable} = value
4071 @end example
4073 @noindent
4074 has the same result as:
4076 @example
4077 @var{variable} = value
4078 export @var{variable}
4079 @end example
4081 @noindent
4084 @example
4085 export @var{variable} := value
4086 @end example
4088 @noindent
4089 has the same result as:
4091 @example
4092 @var{variable} := value
4093 export @var{variable}
4094 @end example
4096 Likewise,
4098 @example
4099 export @var{variable} += value
4100 @end example
4102 @noindent
4103 is just like:
4105 @example
4106 @var{variable} += value
4107 export @var{variable}
4108 @end example
4110 @noindent
4111 @xref{Appending, ,Appending More Text to Variables}.
4113 You may notice that the @code{export} and @code{unexport} directives
4114 work in @code{make} in the same way they work in the shell, @code{sh}.
4116 If you want all variables to be exported by default, you can use
4117 @code{export} by itself:
4119 @example
4120 export
4121 @end example
4123 @noindent
4124 This tells @code{make} that variables which are not explicitly mentioned
4125 in an @code{export} or @code{unexport} directive should be exported.
4126 Any variable given in an @code{unexport} directive will still @emph{not}
4127 be exported.  If you use @code{export} by itself to export variables by
4128 default, variables whose names contain characters other than
4129 alphanumerics and underscores will not be exported unless specifically
4130 mentioned in an @code{export} directive.@refill
4132 @findex .EXPORT_ALL_VARIABLES
4133 The behavior elicited by an @code{export} directive by itself was the
4134 default in older versions of GNU @code{make}.  If your makefiles depend
4135 on this behavior and you want to be compatible with old versions of
4136 @code{make}, you can write a rule for the special target
4137 @code{.EXPORT_ALL_VARIABLES} instead of using the @code{export} directive.
4138 This will be ignored by old @code{make}s, while the @code{export}
4139 directive will cause a syntax error.@refill
4140 @cindex compatibility in exporting
4142 Likewise, you can use @code{unexport} by itself to tell @code{make}
4143 @emph{not} to export variables by default.  Since this is the default
4144 behavior, you would only need to do this if @code{export} had been used
4145 by itself earlier (in an included makefile, perhaps).  You
4146 @strong{cannot} use @code{export} and @code{unexport} by themselves to
4147 have variables exported for some commands and not for others.  The last
4148 @code{export} or @code{unexport} directive that appears by itself
4149 determines the behavior for the entire run of @code{make}.@refill
4151 @vindex MAKELEVEL
4152 @cindex recursion, level of
4153 As a special feature, the variable @code{MAKELEVEL} is changed when it
4154 is passed down from level to level.  This variable's value is a string
4155 which is the depth of the level as a decimal number.  The value is
4156 @samp{0} for the top-level @code{make}; @samp{1} for a sub-@code{make},
4157 @samp{2} for a sub-sub-@code{make}, and so on.  The incrementation
4158 happens when @code{make} sets up the environment for a command.@refill
4160 The main use of @code{MAKELEVEL} is to test it in a conditional
4161 directive (@pxref{Conditionals, ,Conditional Parts of Makefiles}); this
4162 way you can write a makefile that behaves one way if run recursively and
4163 another way if run directly by you.@refill
4165 @vindex MAKEFILES
4166 You can use the variable @code{MAKEFILES} to cause all sub-@code{make}
4167 commands to use additional makefiles.  The value of @code{MAKEFILES} is
4168 a whitespace-separated list of file names.  This variable, if defined in
4169 the outer-level makefile, is passed down through the environment; then
4170 it serves as a list of extra makefiles for the sub-@code{make} to read
4171 before the usual or specified ones.  @xref{MAKEFILES Variable, ,The
4172 Variable @code{MAKEFILES}}.@refill
4174 @node Options/Recursion, -w Option, Variables/Recursion, Recursion
4175 @subsection Communicating Options to a Sub-@code{make}
4176 @cindex options, and recursion
4177 @cindex recursion, and options
4179 @vindex MAKEFLAGS
4180 Flags such as @samp{-s} and @samp{-k} are passed automatically to the
4181 sub-@code{make} through the variable @code{MAKEFLAGS}.  This variable is
4182 set up automatically by @code{make} to contain the flag letters that
4183 @code{make} received.  Thus, if you do @w{@samp{make -ks}} then
4184 @code{MAKEFLAGS} gets the value @samp{ks}.@refill
4186 As a consequence, every sub-@code{make} gets a value for @code{MAKEFLAGS}
4187 in its environment.  In response, it takes the flags from that value and
4188 processes them as if they had been given as arguments.
4189 @xref{Options Summary, ,Summary of Options}.
4191 @cindex command line variable definitions, and recursion
4192 @cindex variables, command line, and recursion
4193 @cindex recursion, and command line variable definitions
4194 Likewise variables defined on the command line are passed to the
4195 sub-@code{make} through @code{MAKEFLAGS}.  Words in the value of
4196 @code{MAKEFLAGS} that contain @samp{=}, @code{make} treats as variable
4197 definitions just as if they appeared on the command line.
4198 @xref{Overriding, ,Overriding Variables}.
4200 @cindex @code{-C}, and recursion
4201 @cindex @code{-f}, and recursion
4202 @cindex @code{-o}, and recursion
4203 @cindex @code{-W}, and recursion
4204 @cindex @code{--directory}, and recursion
4205 @cindex @code{--file}, and recursion
4206 @cindex @code{--old-file}, and recursion
4207 @cindex @code{--assume-old}, and recursion
4208 @cindex @code{--assume-new}, and recursion
4209 @cindex @code{--new-file}, and recursion
4210 @cindex recursion, and @code{-C}
4211 @cindex recursion, and @code{-f}
4212 @cindex recursion, and @code{-o}
4213 @cindex recursion, and @code{-W}
4214 The options @samp{-C}, @samp{-f}, @samp{-o}, and @samp{-W} are not put
4215 into @code{MAKEFLAGS}; these options are not passed down.@refill
4217 @cindex @code{-j}, and recursion
4218 @cindex @code{--jobs}, and recursion
4219 @cindex recursion, and @code{-j}
4220 @cindex job slots, and recursion
4221 The @samp{-j} option is a special case (@pxref{Parallel, ,Parallel Execution}).
4222 If you set it to some numeric value @samp{N} and your operating system
4223 supports it (most any UNIX system will; others typically won't), the
4224 parent @code{make} and all the sub-@code{make}s will communicate to
4225 ensure that there are only @samp{N} jobs running at the same time
4226 between them all.  Note that any job that is marked recursive
4227 (@pxref{Instead of Execution, ,Instead of Executing the Commands})
4228 doesn't count against the total jobs (otherwise we could get @samp{N}
4229 sub-@code{make}s running and have no slots left over for any real work!)
4231 If your operating system doesn't support the above communication, then
4232 @samp{-j 1} is always put into @code{MAKEFLAGS} instead of the value you
4233 specified.  This is because if the @w{@samp{-j}} option were passed down
4234 to sub-@code{make}s, you would get many more jobs running in parallel
4235 than you asked for.  If you give @samp{-j} with no numeric argument,
4236 meaning to run as many jobs as possible in parallel, this is passed
4237 down, since multiple infinities are no more than one.@refill
4239 If you do not want to pass the other flags down, you must change the
4240 value of @code{MAKEFLAGS}, like this:
4242 @example
4243 subsystem:
4244         cd subdir && $(MAKE) MAKEFLAGS=
4245 @end example
4247 @vindex MAKEOVERRIDES
4248 The command line variable definitions really appear in the variable
4249 @code{MAKEOVERRIDES}, and @code{MAKEFLAGS} contains a reference to this
4250 variable.  If you do want to pass flags down normally, but don't want to
4251 pass down the command line variable definitions, you can reset
4252 @code{MAKEOVERRIDES} to empty, like this:
4254 @example
4255 MAKEOVERRIDES =
4256 @end example
4258 @noindent
4259 @cindex Arg list too long
4260 @cindex E2BIG
4261 This is not usually useful to do.  However, some systems have a small
4262 fixed limit on the size of the environment, and putting so much
4263 information into the value of @code{MAKEFLAGS} can exceed it.  If you
4264 see the error message @samp{Arg list too long}, this may be the problem.
4265 @findex .POSIX
4266 @cindex POSIX.2
4267 (For strict compliance with POSIX.2, changing @code{MAKEOVERRIDES} does
4268 not affect @code{MAKEFLAGS} if the special target @samp{.POSIX} appears
4269 in the makefile.  You probably do not care about this.)
4271 @vindex MFLAGS
4272 A similar variable @code{MFLAGS} exists also, for historical
4273 compatibility.  It has the same value as @code{MAKEFLAGS} except that it
4274 does not contain the command line variable definitions, and it always
4275 begins with a hyphen unless it is empty (@code{MAKEFLAGS} begins with a
4276 hyphen only when it begins with an option that has no single-letter
4277 version, such as @samp{--warn-undefined-variables}).  @code{MFLAGS} was
4278 traditionally used explicitly in the recursive @code{make} command, like
4279 this:
4281 @example
4282 subsystem:
4283         cd subdir && $(MAKE) $(MFLAGS)
4284 @end example
4286 @noindent
4287 but now @code{MAKEFLAGS} makes this usage redundant.  If you want your
4288 makefiles to be compatible with old @code{make} programs, use this
4289 technique; it will work fine with more modern @code{make} versions too.
4291 @cindex setting options from environment
4292 @cindex options, setting from environment
4293 @cindex setting options in makefiles
4294 @cindex options, setting in makefiles
4295 The @code{MAKEFLAGS} variable can also be useful if you want to have
4296 certain options, such as @samp{-k} (@pxref{Options Summary, ,Summary of
4297 Options}), set each time you run @code{make}.  You simply put a value for
4298 @code{MAKEFLAGS} in your environment.  You can also set @code{MAKEFLAGS} in
4299 a makefile, to specify additional flags that should also be in effect for
4300 that makefile.  (Note that you cannot use @code{MFLAGS} this way.  That
4301 variable is set only for compatibility; @code{make} does not interpret a
4302 value you set for it in any way.)
4304 When @code{make} interprets the value of @code{MAKEFLAGS} (either from the
4305 environment or from a makefile), it first prepends a hyphen if the value
4306 does not already begin with one.  Then it chops the value into words
4307 separated by blanks, and parses these words as if they were options given
4308 on the command line (except that @samp{-C}, @samp{-f}, @samp{-h},
4309 @samp{-o}, @samp{-W}, and their long-named versions are ignored; and there
4310 is no error for an invalid option).
4312 If you do put @code{MAKEFLAGS} in your environment, you should be sure not
4313 to include any options that will drastically affect the actions of
4314 @code{make} and undermine the purpose of makefiles and of @code{make}
4315 itself.  For instance, the @samp{-t}, @samp{-n}, and @samp{-q} options, if
4316 put in one of these variables, could have disastrous consequences and would
4317 certainly have at least surprising and probably annoying effects.@refill
4319 @node -w Option,  , Options/Recursion, Recursion
4320 @subsection The @samp{--print-directory} Option
4321 @cindex directories, printing them
4322 @cindex printing directories
4323 @cindex recursion, and printing directories
4325 If you use several levels of recursive @code{make} invocations, the
4326 @samp{-w} or @w{@samp{--print-directory}} option can make the output a
4327 lot easier to understand by showing each directory as @code{make}
4328 starts processing it and as @code{make} finishes processing it.  For
4329 example, if @samp{make -w} is run in the directory @file{/u/gnu/make},
4330 @code{make} will print a line of the form:@refill
4332 @example
4333 make: Entering directory `/u/gnu/make'.
4334 @end example
4336 @noindent
4337 before doing anything else, and a line of the form:
4339 @example
4340 make: Leaving directory `/u/gnu/make'.
4341 @end example
4343 @noindent
4344 when processing is completed.
4346 @cindex @code{-C}, and @code{-w}
4347 @cindex @code{--directory}, and @code{--print-directory}
4348 @cindex recursion, and @code{-w}
4349 @cindex @code{-w}, and @code{-C}
4350 @cindex @code{-w}, and recursion
4351 @cindex @code{--print-directory}, and @code{--directory}
4352 @cindex @code{--print-directory}, and recursion
4353 @cindex @code{--no-print-directory}
4354 @cindex @code{--print-directory}, disabling
4355 @cindex @code{-w}, disabling
4356 Normally, you do not need to specify this option because @samp{make}
4357 does it for you: @samp{-w} is turned on automatically when you use the
4358 @samp{-C} option, and in sub-@code{make}s.  @code{make} will not
4359 automatically turn on @samp{-w} if you also use @samp{-s}, which says to
4360 be silent, or if you use @samp{--no-print-directory} to explicitly
4361 disable it.
4363 @node Sequences, Empty Commands, Recursion, Commands
4364 @section Defining Canned Command Sequences
4365 @cindex sequences of commands
4366 @cindex commands, sequences of
4368 When the same sequence of commands is useful in making various targets, you
4369 can define it as a canned sequence with the @code{define} directive, and
4370 refer to the canned sequence from the rules for those targets.  The canned
4371 sequence is actually a variable, so the name must not conflict with other
4372 variable names.
4374 Here is an example of defining a canned sequence of commands:
4376 @example
4377 define run-yacc
4378 yacc $(firstword $^)
4379 mv y.tab.c $@@
4380 endef
4381 @end example
4382 @cindex @code{yacc}
4384 @noindent
4385 Here @code{run-yacc} is the name of the variable being defined;
4386 @code{endef} marks the end of the definition; the lines in between are the
4387 commands.  The @code{define} directive does not expand variable references
4388 and function calls in the canned sequence; the @samp{$} characters,
4389 parentheses, variable names, and so on, all become part of the value of the
4390 variable you are defining.
4391 @xref{Defining, ,Defining Variables Verbatim},
4392 for a complete explanation of @code{define}.
4394 The first command in this example runs Yacc on the first prerequisite of
4395 whichever rule uses the canned sequence.  The output file from Yacc is
4396 always named @file{y.tab.c}.  The second command moves the output to the
4397 rule's target file name.
4399 To use the canned sequence, substitute the variable into the commands of a
4400 rule.  You can substitute it like any other variable
4401 (@pxref{Reference, ,Basics of Variable References}).
4402 Because variables defined by @code{define} are recursively expanded
4403 variables, all the variable references you wrote inside the @code{define}
4404 are expanded now.  For example:
4406 @example
4407 foo.c : foo.y
4408         $(run-yacc)
4409 @end example
4411 @noindent
4412 @samp{foo.y} will be substituted for the variable @samp{$^} when it occurs in
4413 @code{run-yacc}'s value, and @samp{foo.c} for @samp{$@@}.@refill
4415 This is a realistic example, but this particular one is not needed in
4416 practice because @code{make} has an implicit rule to figure out these
4417 commands based on the file names involved
4418 (@pxref{Implicit Rules, ,Using Implicit Rules}).
4420 @cindex @@, and @code{define}
4421 @cindex -, and @code{define}
4422 @cindex +, and @code{define}
4423 In command execution, each line of a canned sequence is treated just as
4424 if the line appeared on its own in the rule, preceded by a tab.  In
4425 particular, @code{make} invokes a separate subshell for each line.  You
4426 can use the special prefix characters that affect command lines
4427 (@samp{@@}, @samp{-}, and @samp{+}) on each line of a canned sequence.
4428 @xref{Commands, ,Writing the Commands in Rules}.
4429 For example, using this canned sequence:
4431 @example
4432 define frobnicate
4433 @@echo "frobnicating target $@@"
4434 frob-step-1 $< -o $@@-step-1
4435 frob-step-2 $@@-step-1 -o $@@
4436 endef
4437 @end example
4439 @noindent
4440 @code{make} will not echo the first line, the @code{echo} command.
4441 But it @emph{will} echo the following two command lines.
4443 On the other hand, prefix characters on the command line that refers to
4444 a canned sequence apply to every line in the sequence.  So the rule:
4446 @example
4447 frob.out: frob.in
4448         @@$(frobnicate)
4449 @end example
4451 @noindent
4452 does not echo @emph{any} commands.
4453 (@xref{Echoing, ,Command Echoing}, for a full explanation of @samp{@@}.)
4455 @node Empty Commands,  , Sequences, Commands
4456 @section Using Empty Commands
4457 @cindex empty commands
4458 @cindex commands, empty
4460 It is sometimes useful to define commands which do nothing.  This is done
4461 simply by giving a command that consists of nothing but whitespace.  For
4462 example:
4464 @example
4465 target: ;
4466 @end example
4468 @noindent
4469 defines an empty command string for @file{target}.  You could also use a
4470 line beginning with a tab character to define an empty command string,
4471 but this would be confusing because such a line looks empty.
4473 @findex .DEFAULT@r{, and empty commands}
4474 You may be wondering why you would want to define a command string that
4475 does nothing.  The only reason this is useful is to prevent a target
4476 from getting implicit commands (from implicit rules or the
4477 @code{.DEFAULT} special target; @pxref{Implicit Rules} and
4478 @pxref{Last Resort, ,Defining Last-Resort Default Rules}).@refill
4480 @c !!! another reason is for canonical stamp files:
4481 @ignore
4482 @example
4483 foo: stamp-foo ;
4484 stamp-foo: foo.in
4485         create foo frm foo.in
4486         touch $@
4487 @end example
4488 @end ignore
4490 You may be inclined to define empty command strings for targets that are
4491 not actual files, but only exist so that their prerequisites can be
4492 remade.  However, this is not the best way to do that, because the
4493 prerequisites may not be remade properly if the target file actually does exist.
4494 @xref{Phony Targets, ,Phony Targets}, for a better way to do this.
4496 @node Using Variables, Conditionals, Commands, Top
4497 @chapter How to Use Variables
4498 @cindex variable
4499 @cindex value
4500 @cindex recursive variable expansion
4501 @cindex simple variable expansion
4503 A @dfn{variable} is a name defined in a makefile to represent a string
4504 of text, called the variable's @dfn{value}.  These values are
4505 substituted by explicit request into targets, prerequisites, commands,
4506 and other parts of the makefile.  (In some other versions of @code{make},
4507 variables are called @dfn{macros}.)
4508 @cindex macro
4510 Variables and functions in all parts of a makefile are expanded when
4511 read, except for the shell commands in rules, the right-hand sides of
4512 variable definitions using @samp{=}, and the bodies of variable
4513 definitions using the @code{define} directive.@refill
4515 Variables can represent lists of file names, options to pass to compilers,
4516 programs to run, directories to look in for source files, directories to
4517 write output in, or anything else you can imagine.
4519 A variable name may be any sequence of characters not containing @samp{:},
4520 @samp{#}, @samp{=}, or leading or trailing whitespace.  However,
4521 variable names containing characters other than letters, numbers, and
4522 underscores should be avoided, as they may be given special meanings in the
4523 future, and with some shells they cannot be passed through the environment to a
4524 sub-@code{make}
4525 (@pxref{Variables/Recursion, ,Communicating Variables to a Sub-@code{make}}).
4527 Variable names are case-sensitive.  The names @samp{foo}, @samp{FOO},
4528 and @samp{Foo} all refer to different variables.
4530 It is traditional to use upper case letters in variable names, but we
4531 recommend using lower case letters for variable names that serve internal
4532 purposes in the makefile, and reserving upper case for parameters that
4533 control implicit rules or for parameters that the user should override with
4534 command options (@pxref{Overriding, ,Overriding Variables}).
4536 A few variables have names that are a single punctuation character or
4537 just a few characters.  These are the @dfn{automatic variables}, and
4538 they have particular specialized uses.  @xref{Automatic Variables}.
4540 @menu
4541 * Reference::                   How to use the value of a variable.
4542 * Flavors::                     Variables come in two flavors.
4543 * Advanced::                    Advanced features for referencing a variable.
4544 * Values::                      All the ways variables get their values.
4545 * Setting::                     How to set a variable in the makefile.
4546 * Appending::                   How to append more text to the old value
4547                                   of a variable.
4548 * Override Directive::          How to set a variable in the makefile even if
4549                                   the user has set it with a command argument.
4550 * Defining::                    An alternate way to set a variable
4551                                   to a verbatim string.
4552 * Environment::                 Variable values can come from the environment.
4553 * Target-specific::             Variable values can be defined on a per-target
4554                                   basis.
4555 * Pattern-specific::            Target-specific variable values can be applied
4556                                   to a group of targets that match a pattern.
4557 @end menu
4559 @node Reference, Flavors, Using Variables, Using Variables
4560 @section Basics of Variable References
4561 @cindex variables, how to reference
4562 @cindex reference to variables
4563 @cindex @code{$}, in variable reference
4564 @cindex dollar sign (@code{$}), in variable reference
4566 To substitute a variable's value, write a dollar sign followed by the name
4567 of the variable in parentheses or braces: either @samp{$(foo)} or
4568 @samp{$@{foo@}} is a valid reference to the variable @code{foo}.  This
4569 special significance of @samp{$} is why you must write @samp{$$} to have
4570 the effect of a single dollar sign in a file name or command.
4572 Variable references can be used in any context: targets, prerequisites,
4573 commands, most directives, and new variable values.  Here is an
4574 example of a common case, where a variable holds the names of all the
4575 object files in a program:
4577 @example
4578 @group
4579 objects = program.o foo.o utils.o
4580 program : $(objects)
4581         cc -o program $(objects)
4583 $(objects) : defs.h
4584 @end group
4585 @end example
4587 Variable references work by strict textual substitution.  Thus, the rule
4589 @example
4590 @group
4591 foo = c
4592 prog.o : prog.$(foo)
4593         $(foo)$(foo) -$(foo) prog.$(foo)
4594 @end group
4595 @end example
4597 @noindent
4598 could be used to compile a C program @file{prog.c}.  Since spaces before
4599 the variable value are ignored in variable assignments, the value of
4600 @code{foo} is precisely @samp{c}.  (Don't actually write your makefiles
4601 this way!)
4603 A dollar sign followed by a character other than a dollar sign,
4604 open-parenthesis or open-brace treats that single character as the
4605 variable name.  Thus, you could reference the variable @code{x} with
4606 @samp{$x}.  However, this practice is strongly discouraged, except in
4607 the case of the automatic variables (@pxref{Automatic Variables}).
4609 @node Flavors, Advanced, Reference, Using Variables
4610 @section The Two Flavors of Variables
4611 @cindex flavors of variables
4612 @cindex recursive variable expansion
4613 @cindex variables, flavors
4614 @cindex recursively expanded variables
4615 @cindex variables, recursively expanded
4617 There are two ways that a variable in GNU @code{make} can have a value;
4618 we call them the two @dfn{flavors} of variables.  The two flavors are
4619 distinguished in how they are defined and in what they do when expanded.
4621 @cindex =
4622 The first flavor of variable is a @dfn{recursively expanded} variable.
4623 Variables of this sort are defined by lines using @samp{=}
4624 (@pxref{Setting, ,Setting Variables}) or by the @code{define} directive
4625 (@pxref{Defining, ,Defining Variables Verbatim}).  The value you specify
4626 is installed verbatim; if it contains references to other variables,
4627 these references are expanded whenever this variable is substituted (in
4628 the course of expanding some other string).  When this happens, it is
4629 called @dfn{recursive expansion}.@refill
4631 For example,
4633 @example
4634 foo = $(bar)
4635 bar = $(ugh)
4636 ugh = Huh?
4638 all:;echo $(foo)
4639 @end example
4641 @noindent
4642 will echo @samp{Huh?}: @samp{$(foo)} expands to @samp{$(bar)} which
4643 expands to @samp{$(ugh)} which finally expands to @samp{Huh?}.@refill
4645 This flavor of variable is the only sort supported by other versions of
4646 @code{make}.  It has its advantages and its disadvantages.  An advantage
4647 (most would say) is that:
4649 @example
4650 CFLAGS = $(include_dirs) -O
4651 include_dirs = -Ifoo -Ibar
4652 @end example
4654 @noindent
4655 will do what was intended: when @samp{CFLAGS} is expanded in a command,
4656 it will expand to @samp{-Ifoo -Ibar -O}.  A major disadvantage is that you
4657 cannot append something on the end of a variable, as in
4659 @example
4660 CFLAGS = $(CFLAGS) -O
4661 @end example
4663 @noindent
4664 because it will cause an infinite loop in the variable expansion.
4665 (Actually @code{make} detects the infinite loop and reports an error.)
4666 @cindex loops in variable expansion
4667 @cindex variables, loops in expansion
4669 Another disadvantage is that any functions
4670 (@pxref{Functions, ,Functions for Transforming Text})
4671 referenced in the definition will be executed every time the variable is
4672 expanded.  This makes @code{make} run slower; worse, it causes the
4673 @code{wildcard} and @code{shell} functions to give unpredictable results
4674 because you cannot easily control when they are called, or even how many
4675 times.
4677 To avoid all the problems and inconveniences of recursively expanded
4678 variables, there is another flavor: simply expanded variables.
4680 @cindex simply expanded variables
4681 @cindex variables, simply expanded
4682 @cindex :=
4683 @dfn{Simply expanded variables} are defined by lines using @samp{:=}
4684 (@pxref{Setting, ,Setting Variables}).
4685 The value of a simply expanded variable is scanned
4686 once and for all, expanding any references to other variables and
4687 functions, when the variable is defined.  The actual value of the simply
4688 expanded variable is the result of expanding the text that you write.
4689 It does not contain any references to other variables; it contains their
4690 values @emph{as of the time this variable was defined}.  Therefore,
4692 @example
4693 x := foo
4694 y := $(x) bar
4695 x := later
4696 @end example
4698 @noindent
4699 is equivalent to
4701 @example
4702 y := foo bar
4703 x := later
4704 @end example
4706 When a simply expanded variable is referenced, its value is substituted
4707 verbatim.
4709 Here is a somewhat more complicated example, illustrating the use of
4710 @samp{:=} in conjunction with the @code{shell} function.
4711 (@xref{Shell Function, , The @code{shell} Function}.)  This example
4712 also shows use of the variable @code{MAKELEVEL}, which is changed
4713 when it is passed down from level to level.
4714 (@xref{Variables/Recursion, , Communicating Variables to a
4715 Sub-@code{make}}, for information about @code{MAKELEVEL}.)
4717 @vindex MAKELEVEL
4718 @vindex MAKE
4719 @example
4720 @group
4721 ifeq (0,$@{MAKELEVEL@})
4722 cur-dir   := $(shell pwd)
4723 whoami    := $(shell whoami)
4724 host-type := $(shell arch)
4725 MAKE := $@{MAKE@} host-type=$@{host-type@} whoami=$@{whoami@}
4726 endif
4727 @end group
4728 @end example
4730 @noindent
4731 An advantage of this use of @samp{:=} is that a typical
4732 `descend into a directory' command then looks like this:
4734 @example
4735 @group
4736 $@{subdirs@}:
4737       $@{MAKE@} cur-dir=$@{cur-dir@}/$@@ -C $@@ all
4738 @end group
4739 @end example
4741 Simply expanded variables generally make complicated makefile programming
4742 more predictable because they work like variables in most programming
4743 languages.  They allow you to redefine a variable using its own value (or
4744 its value processed in some way by one of the expansion functions) and to
4745 use the expansion functions much more efficiently
4746 (@pxref{Functions, ,Functions for Transforming Text}).
4748 @cindex spaces, in variable values
4749 @cindex whitespace, in variable values
4750 @cindex variables, spaces in values
4751 You can also use them to introduce controlled leading whitespace into
4752 variable values.  Leading whitespace characters are discarded from your
4753 input before substitution of variable references and function calls;
4754 this means you can include leading spaces in a variable value by
4755 protecting them with variable references, like this:
4757 @example
4758 nullstring :=
4759 space := $(nullstring) # end of the line
4760 @end example
4762 @noindent
4763 Here the value of the variable @code{space} is precisely one space.  The
4764 comment @w{@samp{# end of the line}} is included here just for clarity.
4765 Since trailing space characters are @emph{not} stripped from variable
4766 values, just a space at the end of the line would have the same effect
4767 (but be rather hard to read).  If you put whitespace at the end of a
4768 variable value, it is a good idea to put a comment like that at the end
4769 of the line to make your intent clear.  Conversely, if you do @emph{not}
4770 want any whitespace characters at the end of your variable value, you
4771 must remember not to put a random comment on the end of the line after
4772 some whitespace, such as this:
4774 @example
4775 dir := /foo/bar    # directory to put the frobs in
4776 @end example
4778 @noindent
4779 Here the value of the variable @code{dir} is @w{@samp{/foo/bar    }}
4780 (with four trailing spaces), which was probably not the intention.
4781 (Imagine something like @w{@samp{$(dir)/file}} with this definition!)
4783 @cindex conditional variable assignment
4784 @cindex variables, conditional assignment
4785 @cindex ?=
4786 There is another assignment operator for variables, @samp{?=}.  This
4787 is called a conditional variable assignment operator, because it only
4788 has an effect if the variable is not yet defined.  This statement:
4790 @example
4791 FOO ?= bar
4792 @end example
4794 @noindent
4795 is exactly equivalent to this
4796 (@pxref{Origin Function, ,The @code{origin} Function}):
4798 @example
4799 ifeq ($(origin FOO), undefined)
4800   FOO = bar
4801 endif
4802 @end example
4804 Note that a variable set to an empty value is still defined, so
4805 @samp{?=} will not set that variable.
4807 @node Advanced, Values, Flavors, Using Variables
4808 @section Advanced Features for Reference to Variables
4809 @cindex reference to variables
4811 This section describes some advanced features you can use to reference
4812 variables in more flexible ways.
4814 @menu
4815 * Substitution Refs::           Referencing a variable with
4816                                   substitutions on the value.
4817 * Computed Names::              Computing the name of the variable to refer to.
4818 @end menu
4820 @node Substitution Refs, Computed Names, Advanced, Advanced
4821 @subsection Substitution References
4822 @cindex modified variable reference
4823 @cindex substitution variable reference
4824 @cindex variables, modified reference
4825 @cindex variables, substitution reference
4827 @cindex variables, substituting suffix in
4828 @cindex suffix, substituting in variables
4829 A @dfn{substitution reference} substitutes the value of a variable with
4830 alterations that you specify.  It has the form
4831 @samp{$(@var{var}:@var{a}=@var{b})} (or
4832 @samp{$@{@var{var}:@var{a}=@var{b}@}}) and its meaning is to take the value
4833 of the variable @var{var}, replace every @var{a} at the end of a word with
4834 @var{b} in that value, and substitute the resulting string.
4836 When we say ``at the end of a word'', we mean that @var{a} must appear
4837 either followed by whitespace or at the end of the value in order to be
4838 replaced; other occurrences of @var{a} in the value are unaltered.  For
4839 example:@refill
4841 @example
4842 foo := a.o b.o c.o
4843 bar := $(foo:.o=.c)
4844 @end example
4846 @noindent
4847 sets @samp{bar} to @samp{a.c b.c c.c}.  @xref{Setting, ,Setting Variables}.
4849 A substitution reference is actually an abbreviation for use of the
4850 @code{patsubst} expansion function (@pxref{Text Functions, ,Functions for String Substitution and Analysis}).  We provide
4851 substitution references as well as @code{patsubst} for compatibility with
4852 other implementations of @code{make}.
4854 @findex patsubst
4855 Another type of substitution reference lets you use the full power of
4856 the @code{patsubst} function.  It has the same form
4857 @samp{$(@var{var}:@var{a}=@var{b})} described above, except that now
4858 @var{a} must contain a single @samp{%} character.  This case is
4859 equivalent to @samp{$(patsubst @var{a},@var{b},$(@var{var}))}.
4860 @xref{Text Functions, ,Functions for String Substitution and Analysis},
4861 for a description of the @code{patsubst} function.@refill
4863 @example
4864 @group
4865 @exdent For example:
4867 foo := a.o b.o c.o
4868 bar := $(foo:%.o=%.c)
4869 @end group
4870 @end example
4872 @noindent
4873 sets @samp{bar} to @samp{a.c b.c c.c}.
4875 @node Computed Names,  , Substitution Refs, Advanced
4876 @subsection Computed Variable Names
4877 @cindex nested variable reference
4878 @cindex computed variable name
4879 @cindex variables, computed names
4880 @cindex variables, nested references
4881 @cindex variables, @samp{$} in name
4882 @cindex @code{$}, in variable name
4883 @cindex dollar sign (@code{$}), in variable name
4885 Computed variable names are a complicated concept needed only for
4886 sophisticated makefile programming.  For most purposes you need not
4887 consider them, except to know that making a variable with a dollar sign
4888 in its name might have strange results.  However, if you are the type
4889 that wants to understand everything, or you are actually interested in
4890 what they do, read on.
4892 Variables may be referenced inside the name of a variable.  This is
4893 called a @dfn{computed variable name} or a @dfn{nested variable
4894 reference}.  For example,
4896 @example
4897 x = y
4898 y = z
4899 a := $($(x))
4900 @end example
4902 @noindent
4903 defines @code{a} as @samp{z}: the @samp{$(x)} inside @samp{$($(x))} expands
4904 to @samp{y}, so @samp{$($(x))} expands to @samp{$(y)} which in turn expands
4905 to @samp{z}.  Here the name of the variable to reference is not stated
4906 explicitly; it is computed by expansion of @samp{$(x)}.  The reference
4907 @samp{$(x)} here is nested within the outer variable reference.
4909 The previous example shows two levels of nesting, but any number of levels
4910 is possible.  For example, here are three levels:
4912 @example
4913 x = y
4914 y = z
4915 z = u
4916 a := $($($(x)))
4917 @end example
4919 @noindent
4920 Here the innermost @samp{$(x)} expands to @samp{y}, so @samp{$($(x))}
4921 expands to @samp{$(y)} which in turn expands to @samp{z}; now we have
4922 @samp{$(z)}, which becomes @samp{u}.
4924 References to recursively-expanded variables within a variable name are
4925 reexpanded in the usual fashion.  For example:
4927 @example
4928 x = $(y)
4929 y = z
4930 z = Hello
4931 a := $($(x))
4932 @end example
4934 @noindent
4935 defines @code{a} as @samp{Hello}: @samp{$($(x))} becomes @samp{$($(y))}
4936 which becomes @samp{$(z)} which becomes @samp{Hello}.
4938 Nested variable references can also contain modified references and
4939 function invocations (@pxref{Functions, ,Functions for Transforming Text}),
4940 just like any other reference.
4941 For example, using the @code{subst} function
4942 (@pxref{Text Functions, ,Functions for String Substitution and Analysis}):
4944 @example
4945 @group
4946 x = variable1
4947 variable2 := Hello
4948 y = $(subst 1,2,$(x))
4949 z = y
4950 a := $($($(z)))
4951 @end group
4952 @end example
4954 @noindent
4955 eventually defines @code{a} as @samp{Hello}.  It is doubtful that anyone
4956 would ever want to write a nested reference as convoluted as this one, but
4957 it works: @samp{$($($(z)))} expands to @samp{$($(y))} which becomes
4958 @samp{$($(subst 1,2,$(x)))}.  This gets the value @samp{variable1} from
4959 @code{x} and changes it by substitution to @samp{variable2}, so that the
4960 entire string becomes @samp{$(variable2)}, a simple variable reference
4961 whose value is @samp{Hello}.@refill
4963 A computed variable name need not consist entirely of a single variable
4964 reference.  It can contain several variable references, as well as some
4965 invariant text.  For example,
4967 @example
4968 @group
4969 a_dirs := dira dirb
4970 1_dirs := dir1 dir2
4971 @end group
4973 @group
4974 a_files := filea fileb
4975 1_files := file1 file2
4976 @end group
4978 @group
4979 ifeq "$(use_a)" "yes"
4980 a1 := a
4981 else
4982 a1 := 1
4983 endif
4984 @end group
4986 @group
4987 ifeq "$(use_dirs)" "yes"
4988 df := dirs
4989 else
4990 df := files
4991 endif
4993 dirs := $($(a1)_$(df))
4994 @end group
4995 @end example
4997 @noindent
4998 will give @code{dirs} the same value as @code{a_dirs}, @code{1_dirs},
4999 @code{a_files} or @code{1_files} depending on the settings of @code{use_a}
5000 and @code{use_dirs}.@refill
5002 Computed variable names can also be used in substitution references:
5004 @example
5005 @group
5006 a_objects := a.o b.o c.o
5007 1_objects := 1.o 2.o 3.o
5009 sources := $($(a1)_objects:.o=.c)
5010 @end group
5011 @end example
5013 @noindent
5014 defines @code{sources} as either @samp{a.c b.c c.c} or @samp{1.c 2.c 3.c},
5015 depending on the value of @code{a1}.
5017 The only restriction on this sort of use of nested variable references
5018 is that they cannot specify part of the name of a function to be called.
5019 This is because the test for a recognized function name is done before
5020 the expansion of nested references.  For example,
5022 @example
5023 @group
5024 ifdef do_sort
5025 func := sort
5026 else
5027 func := strip
5028 endif
5029 @end group
5031 @group
5032 bar := a d b g q c
5033 @end group
5035 @group
5036 foo := $($(func) $(bar))
5037 @end group
5038 @end example
5040 @noindent
5041 attempts to give @samp{foo} the value of the variable @samp{sort a d b g
5042 q c} or @samp{strip a d b g q c}, rather than giving @samp{a d b g q c}
5043 as the argument to either the @code{sort} or the @code{strip} function.
5044 This restriction could be removed in the future if that change is shown
5045 to be a good idea.
5047 You can also use computed variable names in the left-hand side of a
5048 variable assignment, or in a @code{define} directive, as in:
5050 @example
5051 dir = foo
5052 $(dir)_sources := $(wildcard $(dir)/*.c)
5053 define $(dir)_print
5054 lpr $($(dir)_sources)
5055 endef
5056 @end example
5058 @noindent
5059 This example defines the variables @samp{dir}, @samp{foo_sources}, and
5060 @samp{foo_print}.
5062 Note that @dfn{nested variable references} are quite different from
5063 @dfn{recursively expanded variables}
5064 (@pxref{Flavors, ,The Two Flavors of Variables}), though both are
5065 used together in complex ways when doing makefile programming.@refill
5067 @node Values, Setting, Advanced, Using Variables
5068 @section How Variables Get Their Values
5069 @cindex variables, how they get their values
5070 @cindex value, how a variable gets it
5072 Variables can get values in several different ways:
5074 @itemize @bullet
5075 @item
5076 You can specify an overriding value when you run @code{make}.
5077 @xref{Overriding, ,Overriding Variables}.
5079 @item
5080 You can specify a value in the makefile, either
5081 with an assignment (@pxref{Setting, ,Setting Variables}) or with a
5082 verbatim definition (@pxref{Defining, ,Defining Variables Verbatim}).@refill
5084 @item
5085 Variables in the environment become @code{make} variables.
5086 @xref{Environment, ,Variables from the Environment}.
5088 @item
5089 Several @dfn{automatic} variables are given new values for each rule.
5090 Each of these has a single conventional use.
5091 @xref{Automatic Variables}.
5093 @item
5094 Several variables have constant initial values.
5095 @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
5096 @end itemize
5098 @node Setting, Appending, Values, Using Variables
5099 @section Setting Variables
5100 @cindex setting variables
5101 @cindex variables, setting
5102 @cindex =
5103 @cindex :=
5104 @cindex ?=
5106 To set a variable from the makefile, write a line starting with the
5107 variable name followed by @samp{=} or @samp{:=}.  Whatever follows the
5108 @samp{=} or @samp{:=} on the line becomes the value.  For example,
5110 @example
5111 objects = main.o foo.o bar.o utils.o
5112 @end example
5114 @noindent
5115 defines a variable named @code{objects}.  Whitespace around the variable
5116 name and immediately after the @samp{=} is ignored.
5118 Variables defined with @samp{=} are @dfn{recursively expanded} variables.
5119 Variables defined with @samp{:=} are @dfn{simply expanded} variables; these
5120 definitions can contain variable references which will be expanded before
5121 the definition is made.  @xref{Flavors, ,The Two Flavors of Variables}.
5123 The variable name may contain function and variable references, which
5124 are expanded when the line is read to find the actual variable name to use.
5126 There is no limit on the length of the value of a variable except the
5127 amount of swapping space on the computer.  When a variable definition is
5128 long, it is a good idea to break it into several lines by inserting
5129 backslash-newline at convenient places in the definition.  This will not
5130 affect the functioning of @code{make}, but it will make the makefile easier
5131 to read.
5133 Most variable names are considered to have the empty string as a value if
5134 you have never set them.  Several variables have built-in initial values
5135 that are not empty, but you can set them in the usual ways
5136 (@pxref{Implicit Variables, ,Variables Used by Implicit Rules}).
5137 Several special variables are set
5138 automatically to a new value for each rule; these are called the
5139 @dfn{automatic} variables (@pxref{Automatic Variables}).
5141 If you'd like a variable to be set to a value only if it's not already
5142 set, then you can use the shorthand operator @samp{?=} instead of
5143 @samp{=}.  These two settings of the variable @samp{FOO} are identical
5144 (@pxref{Origin Function, ,The @code{origin} Function}):
5146 @example
5147 FOO ?= bar
5148 @end example
5150 @noindent
5153 @example
5154 ifeq ($(origin FOO), undefined)
5155 FOO = bar
5156 endif
5157 @end example
5159 @node Appending, Override Directive, Setting, Using Variables
5160 @section Appending More Text to Variables
5161 @cindex +=
5162 @cindex appending to variables
5163 @cindex variables, appending to
5165 Often it is useful to add more text to the value of a variable already defined.
5166 You do this with a line containing @samp{+=}, like this:
5168 @example
5169 objects += another.o
5170 @end example
5172 @noindent
5173 This takes the value of the variable @code{objects}, and adds the text
5174 @samp{another.o} to it (preceded by a single space).  Thus:
5176 @example
5177 objects = main.o foo.o bar.o utils.o
5178 objects += another.o
5179 @end example
5181 @noindent
5182 sets @code{objects} to @samp{main.o foo.o bar.o utils.o another.o}.
5184 Using @samp{+=} is similar to:
5186 @example
5187 objects = main.o foo.o bar.o utils.o
5188 objects := $(objects) another.o
5189 @end example
5191 @noindent
5192 but differs in ways that become important when you use more complex values.
5194 When the variable in question has not been defined before, @samp{+=}
5195 acts just like normal @samp{=}: it defines a recursively-expanded
5196 variable.  However, when there @emph{is} a previous definition, exactly
5197 what @samp{+=} does depends on what flavor of variable you defined
5198 originally.  @xref{Flavors, ,The Two Flavors of Variables}, for an
5199 explanation of the two flavors of variables.
5201 When you add to a variable's value with @samp{+=}, @code{make} acts
5202 essentially as if you had included the extra text in the initial
5203 definition of the variable.  If you defined it first with @samp{:=},
5204 making it a simply-expanded variable, @samp{+=} adds to that
5205 simply-expanded definition, and expands the new text before appending it
5206 to the old value just as @samp{:=} does
5207 (@pxref{Setting, ,Setting Variables}, for a full explanation of @samp{:=}).
5208 In fact,
5210 @example
5211 variable := value
5212 variable += more
5213 @end example
5215 @noindent
5216 is exactly equivalent to:
5218 @noindent
5219 @example
5220 variable := value
5221 variable := $(variable) more
5222 @end example
5224 On the other hand, when you use @samp{+=} with a variable that you defined
5225 first to be recursively-expanded using plain @samp{=}, @code{make} does
5226 something a bit different.  Recall that when you define a
5227 recursively-expanded variable, @code{make} does not expand the value you set
5228 for variable and function references immediately.  Instead it stores the text
5229 verbatim, and saves these variable and function references to be expanded
5230 later, when you refer to the new variable (@pxref{Flavors, ,The Two Flavors
5231 of Variables}).  When you use @samp{+=} on a recursively-expanded variable,
5232 it is this unexpanded text to which @code{make} appends the new text you
5233 specify.
5235 @example
5236 @group
5237 variable = value
5238 variable += more
5239 @end group
5240 @end example
5242 @noindent
5243 is roughly equivalent to:
5245 @example
5246 @group
5247 temp = value
5248 variable = $(temp) more
5249 @end group
5250 @end example
5252 @noindent
5253 except that of course it never defines a variable called @code{temp}.
5254 The importance of this comes when the variable's old value contains
5255 variable references.  Take this common example:
5257 @example
5258 CFLAGS = $(includes) -O
5259 @dots{}
5260 CFLAGS += -pg # enable profiling
5261 @end example
5263 @noindent
5264 The first line defines the @code{CFLAGS} variable with a reference to another
5265 variable, @code{includes}.  (@code{CFLAGS} is used by the rules for C
5266 compilation; @pxref{Catalogue of Rules, ,Catalogue of Implicit Rules}.)
5267 Using @samp{=} for the definition makes @code{CFLAGS} a recursively-expanded
5268 variable, meaning @w{@samp{$(includes) -O}} is @emph{not} expanded when
5269 @code{make} processes the definition of @code{CFLAGS}.  Thus, @code{includes}
5270 need not be defined yet for its value to take effect.  It only has to be
5271 defined before any reference to @code{CFLAGS}.  If we tried to append to the
5272 value of @code{CFLAGS} without using @samp{+=}, we might do it like this:
5274 @example
5275 CFLAGS := $(CFLAGS) -pg # enable profiling
5276 @end example
5278 @noindent
5279 This is pretty close, but not quite what we want.  Using @samp{:=}
5280 redefines @code{CFLAGS} as a simply-expanded variable; this means
5281 @code{make} expands the text @w{@samp{$(CFLAGS) -pg}} before setting the
5282 variable.  If @code{includes} is not yet defined, we get @w{@samp{ -O
5283 -pg}}, and a later definition of @code{includes} will have no effect.
5284 Conversely, by using @samp{+=} we set @code{CFLAGS} to the
5285 @emph{unexpanded} value @w{@samp{$(includes) -O -pg}}.  Thus we preserve
5286 the reference to @code{includes}, so if that variable gets defined at
5287 any later point, a reference like @samp{$(CFLAGS)} still uses its
5288 value.
5290 @node Override Directive, Defining, Appending, Using Variables
5291 @section The @code{override} Directive
5292 @findex override
5293 @cindex overriding with @code{override}
5294 @cindex variables, overriding
5296 If a variable has been set with a command argument
5297 (@pxref{Overriding, ,Overriding Variables}),
5298 then ordinary assignments in the makefile are ignored.  If you want to set
5299 the variable in the makefile even though it was set with a command
5300 argument, you can use an @code{override} directive, which is a line that
5301 looks like this:@refill
5303 @example
5304 override @var{variable} = @var{value}
5305 @end example
5307 @noindent
5310 @example
5311 override @var{variable} := @var{value}
5312 @end example
5314 To append more text to a variable defined on the command line, use:
5316 @example
5317 override @var{variable} += @var{more text}
5318 @end example
5320 @noindent
5321 @xref{Appending, ,Appending More Text to Variables}.
5323 The @code{override} directive was not invented for escalation in the war
5324 between makefiles and command arguments.  It was invented so you can alter
5325 and add to values that the user specifies with command arguments.
5327 For example, suppose you always want the @samp{-g} switch when you run the
5328 C compiler, but you would like to allow the user to specify the other
5329 switches with a command argument just as usual.  You could use this
5330 @code{override} directive:
5332 @example
5333 override CFLAGS += -g
5334 @end example
5336 You can also use @code{override} directives with @code{define} directives.
5337 This is done as you might expect:
5339 @example
5340 override define foo
5342 endef
5343 @end example
5345 @noindent
5346 @iftex
5347 See the next section for information about @code{define}.
5348 @end iftex
5349 @ifnottex
5350 @xref{Defining, ,Defining Variables Verbatim}.
5351 @end ifnottex
5353 @node Defining, Environment, Override Directive, Using Variables
5354 @section Defining Variables Verbatim
5355 @findex define
5356 @findex endef
5357 @cindex verbatim variable definition
5358 @cindex defining variables verbatim
5359 @cindex variables, defining verbatim
5361 Another way to set the value of a variable is to use the @code{define}
5362 directive.  This directive has an unusual syntax which allows newline
5363 characters to be included in the value, which is convenient for defining
5364 both canned sequences of commands
5365 (@pxref{Sequences, ,Defining Canned Command Sequences}), and also
5366 sections of makefile syntax to use with @code{eval} (@pxref{Eval Function}).
5368 The @code{define} directive is followed on the same line by the name of the
5369 variable and nothing more.  The value to give the variable appears on the
5370 following lines.  The end of the value is marked by a line containing just
5371 the word @code{endef}.  Aside from this difference in syntax, @code{define}
5372 works just like @samp{=}: it creates a recursively-expanded variable
5373 (@pxref{Flavors, ,The Two Flavors of Variables}).
5374 The variable name may contain function and variable references, which
5375 are expanded when the directive is read to find the actual variable name
5376 to use.
5378 You may nest @code{define} directives: @code{make} will keep track of
5379 nested directives and report an error if they are not all properly
5380 closed with @code{endef}.  Note that lines beginning with tab
5381 characters are considered part of a command script, so any
5382 @code{define} or @code{endef} strings appearing on such a line will
5383 not be considered @code{make} operators.
5385 @example
5386 define two-lines
5387 echo foo
5388 echo $(bar)
5389 endef
5390 @end example
5392 The value in an ordinary assignment cannot contain a newline; but the
5393 newlines that separate the lines of the value in a @code{define} become
5394 part of the variable's value (except for the final newline which precedes
5395 the @code{endef} and is not considered part of the value).@refill
5397 @need 800
5398 When used in a command script, the previous example is functionally
5399 equivalent to this:
5401 @example
5402 two-lines = echo foo; echo $(bar)
5403 @end example
5405 @noindent
5406 since two commands separated by semicolon behave much like two separate
5407 shell commands.  However, note that using two separate lines means
5408 @code{make} will invoke the shell twice, running an independent subshell
5409 for each line.  @xref{Execution, ,Command Execution}.
5411 If you want variable definitions made with @code{define} to take
5412 precedence over command-line variable definitions, you can use the
5413 @code{override} directive together with @code{define}:
5415 @example
5416 override define two-lines
5418 $(bar)
5419 endef
5420 @end example
5422 @noindent
5423 @xref{Override Directive, ,The @code{override} Directive}.
5425 @node Environment, Target-specific, Defining, Using Variables
5426 @section Variables from the Environment
5428 @cindex variables, environment
5429 @cindex environment
5430 Variables in @code{make} can come from the environment in which
5431 @code{make} is run.  Every environment variable that @code{make} sees
5432 when it starts up is transformed into a @code{make} variable with the
5433 same name and value.  However, an explicit assignment in the makefile,
5434 or with a command argument, overrides the environment.  (If the
5435 @samp{-e} flag is specified, then values from the environment override
5436 assignments in the makefile.  @xref{Options Summary, ,Summary of
5437 Options}.  But this is not recommended practice.)
5439 Thus, by setting the variable @code{CFLAGS} in your environment, you can
5440 cause all C compilations in most makefiles to use the compiler switches you
5441 prefer.  This is safe for variables with standard or conventional meanings
5442 because you know that no makefile will use them for other things.  (Note
5443 this is not totally reliable; some makefiles set @code{CFLAGS} explicitly
5444 and therefore are not affected by the value in the environment.)
5446 When @code{make} runs a command script, variables defined in the
5447 makefile are placed into the environment of that command.  This allows
5448 you to pass values to sub-@code{make} invocations. (@pxref{Recursion,
5449 ,Recursive Use of @code{make}}).  By default, only variables that came
5450 from the environment or the command line are passed to recursive
5451 invocations.  You can use the @code{export} directive to pass other
5452 variables.  @xref{Variables/Recursion, , Communicating Variables to a
5453 Sub-@code{make}}, for full details.
5455 Other use of variables from the environment is not recommended.  It is not
5456 wise for makefiles to depend for their functioning on environment variables
5457 set up outside their control, since this would cause different users to get
5458 different results from the same makefile.  This is against the whole
5459 purpose of most makefiles.
5461 @cindex SHELL, import from environment
5462 Such problems would be especially likely with the variable @code{SHELL},
5463 which is normally present in the environment to specify the user's choice
5464 of interactive shell.  It would be very undesirable for this choice to
5465 affect @code{make}.  So @code{make} ignores the environment value of
5466 @code{SHELL} (except on MS-DOS and MS-Windows, where @code{SHELL} is
5467 usually not set.  @xref{Execution, ,Special handling of SHELL on
5468 MS-DOS}.)@refill
5470 @cindex SHELL, export to environment
5471 The @code{SHELL} variable is special in another way: just as the value
5472 of the @code{make} variable @code{SHELL} is not taken from the
5473 environment, so also it is not placed into the environment of commands
5474 that @code{make} invokes.  Instead, the value of @code{SHELL} from the
5475 invoking environment is provided to the command.  You can use
5476 @code{export SHELL} to force the value of the @code{make} variable
5477 @code{SHELL} to be placed in the environment of commands.
5479 @node Target-specific, Pattern-specific, Environment, Using Variables
5480 @section Target-specific Variable Values
5481 @cindex target-specific variables
5482 @cindex variables, target-specific
5484 Variable values in @code{make} are usually global; that is, they are the
5485 same regardless of where they are evaluated (unless they're reset, of
5486 course).  One exception to that is automatic variables
5487 (@pxref{Automatic Variables}).
5489 The other exception is @dfn{target-specific variable values}.  This
5490 feature allows you to define different values for the same variable,
5491 based on the target that @code{make} is currently building.  As with
5492 automatic variables, these values are only available within the context
5493 of a target's command script (and in other target-specific assignments).
5495 Set a target-specific variable value like this:
5497 @example
5498 @var{target} @dots{} : @var{variable-assignment}
5499 @end example
5501 @noindent
5502 or like this:
5504 @example
5505 @var{target} @dots{} : override @var{variable-assignment}
5506 @end example
5508 @noindent
5509 or like this:
5511 @example
5512 @var{target} @dots{} : export @var{variable-assignment}
5513 @end example
5515 Multiple @var{target} values create a target-specific variable value for
5516 each member of the target list individually.
5518 The @var{variable-assignment} can be any valid form of assignment;
5519 recursive (@samp{=}), static (@samp{:=}), appending (@samp{+=}), or
5520 conditional (@samp{?=}).  All variables that appear within the
5521 @var{variable-assignment} are evaluated within the context of the
5522 target: thus, any previously-defined target-specific variable values
5523 will be in effect.  Note that this variable is actually distinct from
5524 any ``global'' value: the two variables do not have to have the same
5525 flavor (recursive vs. static).
5527 Target-specific variables have the same priority as any other makefile
5528 variable.  Variables provided on the command-line (and in the
5529 environment if the @samp{-e} option is in force) will take precedence.
5530 Specifying the @code{override} directive will allow the target-specific
5531 variable value to be preferred.
5533 There is one more special feature of target-specific variables: when
5534 you define a target-specific variable that variable value is also in
5535 effect for all prerequisites of this target, and all their
5536 prerequisites, etc. (unless those prerequisites override that variable
5537 with their own target-specific variable value).  So, for example, a
5538 statement like this:
5540 @example
5541 prog : CFLAGS = -g
5542 prog : prog.o foo.o bar.o
5543 @end example
5545 @noindent
5546 will set @code{CFLAGS} to @samp{-g} in the command script for
5547 @file{prog}, but it will also set @code{CFLAGS} to @samp{-g} in the
5548 command scripts that create @file{prog.o}, @file{foo.o}, and
5549 @file{bar.o}, and any command scripts which create their
5550 prerequisites.
5552 Be aware that a given prerequisite will only be built once per
5553 invocation of make, at most.  If the same file is a prerequisite of
5554 multiple targets, and each of those targets has a different value for
5555 the same target-specific variable, then the first target to be built
5556 will cause that prerequisite to be built and the prerequisite will
5557 inherit the target-specific value from the first target.  It will
5558 ignore the target-specific values from any other targets.
5560 @node Pattern-specific,  , Target-specific, Using Variables
5561 @section Pattern-specific Variable Values
5562 @cindex pattern-specific variables
5563 @cindex variables, pattern-specific
5565 In addition to target-specific variable values
5566 (@pxref{Target-specific, ,Target-specific Variable Values}), GNU
5567 @code{make} supports pattern-specific variable values.  In this form,
5568 the variable is defined for any target that matches the pattern
5569 specified.  If a target matches more than one pattern, all the
5570 matching pattern-specific variables are interpreted in the order in
5571 which they were defined in the makefile, and collected together into
5572 one set.  Variables defined in this way are searched after any
5573 target-specific variables defined explicitly for that target, and
5574 before target-specific variables defined for the parent target.
5576 Set a pattern-specific variable value like this:
5578 @example
5579 @var{pattern} @dots{} : @var{variable-assignment}
5580 @end example
5582 @noindent
5583 or like this:
5585 @example
5586 @var{pattern} @dots{} : override @var{variable-assignment}
5587 @end example
5589 @noindent
5590 where @var{pattern} is a %-pattern.  As with target-specific variable
5591 values, multiple @var{pattern} values create a pattern-specific variable
5592 value for each pattern individually.  The @var{variable-assignment} can
5593 be any valid form of assignment.  Any command-line variable setting will
5594 take precedence, unless @code{override} is specified.
5596 For example:
5598 @example
5599 %.o : CFLAGS = -O
5600 @end example
5602 @noindent
5603 will assign @code{CFLAGS} the value of @samp{-O} for all targets
5604 matching the pattern @code{%.o}.
5606 @node Conditionals, Functions, Using Variables, Top
5607 @chapter Conditional Parts of Makefiles
5609 @cindex conditionals
5610 A @dfn{conditional} causes part of a makefile to be obeyed or ignored
5611 depending on the values of variables.  Conditionals can compare the
5612 value of one variable to another, or the value of a variable to
5613 a constant string.  Conditionals control what @code{make} actually
5614 ``sees'' in the makefile, so they @emph{cannot} be used to control shell
5615 commands at the time of execution.@refill
5617 @menu
5618 * Conditional Example::         Example of a conditional
5619 * Conditional Syntax::          The syntax of conditionals.
5620 * Testing Flags::               Conditionals that test flags.
5621 @end menu
5623 @node Conditional Example, Conditional Syntax, Conditionals, Conditionals
5624 @section Example of a Conditional
5626 The following example of a conditional tells @code{make} to use one set
5627 of libraries if the @code{CC} variable is @samp{gcc}, and a different
5628 set of libraries otherwise.  It works by controlling which of two
5629 command lines will be used as the command for a rule.  The result is
5630 that @samp{CC=gcc} as an argument to @code{make} changes not only which
5631 compiler is used but also which libraries are linked.
5633 @example
5634 libs_for_gcc = -lgnu
5635 normal_libs =
5637 foo: $(objects)
5638 ifeq ($(CC),gcc)
5639         $(CC) -o foo $(objects) $(libs_for_gcc)
5640 else
5641         $(CC) -o foo $(objects) $(normal_libs)
5642 endif
5643 @end example
5645 This conditional uses three directives: one @code{ifeq}, one @code{else}
5646 and one @code{endif}.
5648 The @code{ifeq} directive begins the conditional, and specifies the
5649 condition.  It contains two arguments, separated by a comma and surrounded
5650 by parentheses.  Variable substitution is performed on both arguments and
5651 then they are compared.  The lines of the makefile following the
5652 @code{ifeq} are obeyed if the two arguments match; otherwise they are
5653 ignored.
5655 The @code{else} directive causes the following lines to be obeyed if the
5656 previous conditional failed.  In the example above, this means that the
5657 second alternative linking command is used whenever the first alternative
5658 is not used.  It is optional to have an @code{else} in a conditional.
5660 The @code{endif} directive ends the conditional.  Every conditional must
5661 end with an @code{endif}.  Unconditional makefile text follows.
5663 As this example illustrates, conditionals work at the textual level:
5664 the lines of the conditional are treated as part of the makefile, or
5665 ignored, according to the condition.  This is why the larger syntactic
5666 units of the makefile, such as rules, may cross the beginning or the
5667 end of the conditional.
5669 When the variable @code{CC} has the value @samp{gcc}, the above example has
5670 this effect:
5672 @example
5673 foo: $(objects)
5674         $(CC) -o foo $(objects) $(libs_for_gcc)
5675 @end example
5677 @noindent
5678 When the variable @code{CC} has any other value, the effect is this:
5680 @example
5681 foo: $(objects)
5682         $(CC) -o foo $(objects) $(normal_libs)
5683 @end example
5685 Equivalent results can be obtained in another way by conditionalizing a
5686 variable assignment and then using the variable unconditionally:
5688 @example
5689 libs_for_gcc = -lgnu
5690 normal_libs =
5692 ifeq ($(CC),gcc)
5693   libs=$(libs_for_gcc)
5694 else
5695   libs=$(normal_libs)
5696 endif
5698 foo: $(objects)
5699         $(CC) -o foo $(objects) $(libs)
5700 @end example
5702 @node Conditional Syntax, Testing Flags, Conditional Example, Conditionals
5703 @section Syntax of Conditionals
5704 @findex ifdef
5705 @findex ifeq
5706 @findex ifndef
5707 @findex ifneq
5708 @findex else
5709 @findex endif
5711 The syntax of a simple conditional with no @code{else} is as follows:
5713 @example
5714 @var{conditional-directive}
5715 @var{text-if-true}
5716 endif
5717 @end example
5719 @noindent
5720 The @var{text-if-true} may be any lines of text, to be considered as part
5721 of the makefile if the condition is true.  If the condition is false, no
5722 text is used instead.
5724 The syntax of a complex conditional is as follows:
5726 @example
5727 @var{conditional-directive}
5728 @var{text-if-true}
5729 else
5730 @var{text-if-false}
5731 endif
5732 @end example
5736 @example
5737 @var{conditional-directive}
5738 @var{text-if-one-is-true}
5739 else @var{conditional-directive}
5740 @var{text-if-true}
5741 else
5742 @var{text-if-false}
5743 endif
5744 @end example
5746 @noindent
5747 There can be as many ``@code{else} @var{conditional-directive}''
5748 clauses as necessary.  Once a given condition is true,
5749 @var{text-if-true} is used and no other clause is used; if no
5750 condition is true then @var{text-if-false} is used.  The
5751 @var{text-if-true} and @var{text-if-false} can be any number of lines
5752 of text.
5754 The syntax of the @var{conditional-directive} is the same whether the
5755 conditional is simple or complex; after an @code{else} or not.  There
5756 are four different directives that test different conditions.  Here is
5757 a table of them:
5759 @table @code
5760 @item ifeq (@var{arg1}, @var{arg2})
5761 @itemx ifeq '@var{arg1}' '@var{arg2}'
5762 @itemx ifeq "@var{arg1}" "@var{arg2}"
5763 @itemx ifeq "@var{arg1}" '@var{arg2}'
5764 @itemx ifeq '@var{arg1}' "@var{arg2}"
5765 Expand all variable references in @var{arg1} and @var{arg2} and
5766 compare them.  If they are identical, the @var{text-if-true} is
5767 effective; otherwise, the @var{text-if-false}, if any, is effective.
5769 Often you want to test if a variable has a non-empty value.  When the
5770 value results from complex expansions of variables and functions,
5771 expansions you would consider empty may actually contain whitespace
5772 characters and thus are not seen as empty.  However, you can use the
5773 @code{strip} function (@pxref{Text Functions}) to avoid interpreting
5774 whitespace as a non-empty value.  For example:
5776 @example
5777 @group
5778 ifeq ($(strip $(foo)),)
5779 @var{text-if-empty}
5780 endif
5781 @end group
5782 @end example
5784 @noindent
5785 will evaluate @var{text-if-empty} even if the expansion of
5786 @code{$(foo)} contains whitespace characters.
5788 @item ifneq (@var{arg1}, @var{arg2})
5789 @itemx ifneq '@var{arg1}' '@var{arg2}'
5790 @itemx ifneq "@var{arg1}" "@var{arg2}"
5791 @itemx ifneq "@var{arg1}" '@var{arg2}'
5792 @itemx ifneq '@var{arg1}' "@var{arg2}"
5793 Expand all variable references in @var{arg1} and @var{arg2} and
5794 compare them.  If they are different, the @var{text-if-true} is
5795 effective; otherwise, the @var{text-if-false}, if any, is effective.
5797 @item ifdef @var{variable-name}
5798 The @code{ifdef} form takes the @emph{name} of a variable as its
5799 argument, not a reference to a variable.  The value of that variable
5800 has a non-empty value, the @var{text-if-true} is effective; otherwise,
5801 the @var{text-if-false}, if any, is effective.  Variables that have
5802 never been defined have an empty value.  The text @var{variable-name}
5803 is expanded, so it could be a variable or function that expands
5804 to the name of a variable.  For example:
5806 @example
5807 bar = true
5808 foo = bar
5809 ifdef $(foo)
5810 frobozz = yes
5811 endif
5812 @end example
5814 The variable reference @code{$(foo)} is expanded, yielding @code{bar},
5815 which is considered to be the name of a variable.  The variable
5816 @code{bar} is not expanded, but its value is examined to determine if
5817 it is non-empty.
5819 Note that @code{ifdef} only tests whether a variable has a value.  It
5820 does not expand the variable to see if that value is nonempty.
5821 Consequently, tests using @code{ifdef} return true for all definitions
5822 except those like @code{foo =}.  To test for an empty value, use
5823 @w{@code{ifeq ($(foo),)}}.  For example,
5825 @example
5826 bar =
5827 foo = $(bar)
5828 ifdef foo
5829 frobozz = yes
5830 else
5831 frobozz = no
5832 endif
5833 @end example
5835 @noindent
5836 sets @samp{frobozz} to @samp{yes}, while:
5838 @example
5839 foo =
5840 ifdef foo
5841 frobozz = yes
5842 else
5843 frobozz = no
5844 endif
5845 @end example
5847 @noindent
5848 sets @samp{frobozz} to @samp{no}.
5850 @item ifndef @var{variable-name}
5851 If the variable @var{variable-name} has an empty value, the
5852 @var{text-if-true} is effective; otherwise, the @var{text-if-false},
5853 if any, is effective.  The rules for expansion and testing of
5854 @var{variable-name} are identical to the @code{ifdef} directive.
5855 @end table
5857 Extra spaces are allowed and ignored at the beginning of the conditional
5858 directive line, but a tab is not allowed.  (If the line begins with a tab,
5859 it will be considered a command for a rule.)  Aside from this, extra spaces
5860 or tabs may be inserted with no effect anywhere except within the directive
5861 name or within an argument.  A comment starting with @samp{#} may appear at
5862 the end of the line.
5864 The other two directives that play a part in a conditional are @code{else}
5865 and @code{endif}.  Each of these directives is written as one word, with no
5866 arguments.  Extra spaces are allowed and ignored at the beginning of the
5867 line, and spaces or tabs at the end.  A comment starting with @samp{#} may
5868 appear at the end of the line.
5870 Conditionals affect which lines of the makefile @code{make} uses.  If
5871 the condition is true, @code{make} reads the lines of the
5872 @var{text-if-true} as part of the makefile; if the condition is false,
5873 @code{make} ignores those lines completely.  It follows that syntactic
5874 units of the makefile, such as rules, may safely be split across the
5875 beginning or the end of the conditional.@refill
5877 @code{make} evaluates conditionals when it reads a makefile.
5878 Consequently, you cannot use automatic variables in the tests of
5879 conditionals because they are not defined until commands are run
5880 (@pxref{Automatic Variables}).
5882 To prevent intolerable confusion, it is not permitted to start a
5883 conditional in one makefile and end it in another.  However, you may
5884 write an @code{include} directive within a conditional, provided you do
5885 not attempt to terminate the conditional inside the included file.
5887 @node Testing Flags,  , Conditional Syntax, Conditionals
5888 @section Conditionals that Test Flags
5890 You can write a conditional that tests @code{make} command flags such as
5891 @samp{-t} by using the variable @code{MAKEFLAGS} together with the
5892 @code{findstring} function
5893 (@pxref{Text Functions, , Functions for String Substitution and Analysis}).
5894 This is useful when @code{touch} is not enough to make a file appear up
5895 to date.
5897 The @code{findstring} function determines whether one string appears as a
5898 substring of another.  If you want to test for the @samp{-t} flag,
5899 use @samp{t} as the first string and the value of @code{MAKEFLAGS} as
5900 the other.
5902 For example, here is how to arrange to use @samp{ranlib -t} to finish
5903 marking an archive file up to date:
5905 @example
5906 archive.a: @dots{}
5907 ifneq (,$(findstring t,$(MAKEFLAGS)))
5908         +touch archive.a
5909         +ranlib -t archive.a
5910 else
5911         ranlib archive.a
5912 endif
5913 @end example
5915 @noindent
5916 The @samp{+} prefix marks those command lines as ``recursive'' so
5917 that they will be executed despite use of the @samp{-t} flag.
5918 @xref{Recursion, ,Recursive Use of @code{make}}.
5920 @node Functions, Running, Conditionals, Top
5921 @chapter Functions for Transforming Text
5922 @cindex functions
5924 @dfn{Functions} allow you to do text processing in the makefile to compute
5925 the files to operate on or the commands to use.  You use a function in a
5926 @dfn{function call}, where you give the name of the function and some text
5927 (the @dfn{arguments}) for the function to operate on.  The result of the
5928 function's processing is substituted into the makefile at the point of the
5929 call, just as a variable might be substituted.
5931 @menu
5932 * Syntax of Functions::         How to write a function call.
5933 * Text Functions::              General-purpose text manipulation functions.
5934 * File Name Functions::         Functions for manipulating file names.
5935 * Foreach Function::            Repeat some text with controlled variation.
5936 * If Function::                 Conditionally expand a value.
5937 * Call Function::               Expand a user-defined function.
5938 * Value Function::              Return the un-expanded value of a variable.
5939 * Eval Function::               Evaluate the arguments as makefile syntax.
5940 * Origin Function::             Find where a variable got its value.
5941 * Shell Function::              Substitute the output of a shell command.
5942 * Make Control Functions::      Functions that control how make runs.
5943 @end menu
5945 @node Syntax of Functions, Text Functions, Functions, Functions
5946 @section Function Call Syntax
5947 @cindex @code{$}, in function call
5948 @cindex dollar sign (@code{$}), in function call
5949 @cindex arguments of functions
5950 @cindex functions, syntax of
5952 A function call resembles a variable reference.  It looks like this:
5954 @example
5955 $(@var{function} @var{arguments})
5956 @end example
5958 @noindent
5959 or like this:
5961 @example
5962 $@{@var{function} @var{arguments}@}
5963 @end example
5965 Here @var{function} is a function name; one of a short list of names
5966 that are part of @code{make}.  You can also essentially create your own
5967 functions by using the @code{call} builtin function.
5969 The @var{arguments} are the arguments of the function.  They are
5970 separated from the function name by one or more spaces or tabs, and if
5971 there is more than one argument, then they are separated by commas.
5972 Such whitespace and commas are not part of an argument's value.  The
5973 delimiters which you use to surround the function call, whether
5974 parentheses or braces, can appear in an argument only in matching pairs;
5975 the other kind of delimiters may appear singly.  If the arguments
5976 themselves contain other function calls or variable references, it is
5977 wisest to use the same kind of delimiters for all the references; write
5978 @w{@samp{$(subst a,b,$(x))}}, not @w{@samp{$(subst a,b,$@{x@})}}.  This
5979 is because it is clearer, and because only one type of delimiter is
5980 matched to find the end of the reference.
5982 The text written for each argument is processed by substitution of
5983 variables and function calls to produce the argument value, which
5984 is the text on which the function acts.  The substitution is done in the
5985 order in which the arguments appear.
5987 Commas and unmatched parentheses or braces cannot appear in the text of an
5988 argument as written; leading spaces cannot appear in the text of the first
5989 argument as written.  These characters can be put into the argument value
5990 by variable substitution.  First define variables @code{comma} and
5991 @code{space} whose values are isolated comma and space characters, then
5992 substitute these variables where such characters are wanted, like this:
5994 @example
5995 @group
5996 comma:= ,
5997 empty:=
5998 space:= $(empty) $(empty)
5999 foo:= a b c
6000 bar:= $(subst $(space),$(comma),$(foo))
6001 # @r{bar is now `a,b,c'.}
6002 @end group
6003 @end example
6005 @noindent
6006 Here the @code{subst} function replaces each space with a comma, through
6007 the value of @code{foo}, and substitutes the result.
6009 @node Text Functions, File Name Functions, Syntax of Functions, Functions
6010 @section Functions for String Substitution and Analysis
6011 @cindex functions, for text
6013 Here are some functions that operate on strings:
6015 @table @code
6016 @item $(subst @var{from},@var{to},@var{text})
6017 @findex subst
6018 Performs a textual replacement on the text @var{text}: each occurrence
6019 of @var{from} is replaced by @var{to}.  The result is substituted for
6020 the function call.  For example,
6022 @example
6023 $(subst ee,EE,feet on the street)
6024 @end example
6026 substitutes the string @samp{fEEt on the strEEt}.
6028 @item $(patsubst @var{pattern},@var{replacement},@var{text})
6029 @findex patsubst
6030 Finds whitespace-separated words in @var{text} that match
6031 @var{pattern} and replaces them with @var{replacement}.  Here
6032 @var{pattern} may contain a @samp{%} which acts as a wildcard,
6033 matching any number of any characters within a word.  If
6034 @var{replacement} also contains a @samp{%}, the @samp{%} is replaced
6035 by the text that matched the @samp{%} in @var{pattern}.  Only the first
6036 @samp{%} in the @var{pattern} and @var{replacement} is treated this
6037 way; any subsequent @samp{%} is unchanged.@refill
6039 @cindex @code{%}, quoting in @code{patsubst}
6040 @cindex @code{%}, quoting with @code{\} (backslash)
6041 @cindex @code{\} (backslash), to quote @code{%}
6042 @cindex backslash (@code{\}), to quote @code{%}
6043 @cindex quoting @code{%}, in @code{patsubst}
6044 @samp{%} characters in @code{patsubst} function invocations can be
6045 quoted with preceding backslashes (@samp{\}).  Backslashes that would
6046 otherwise quote @samp{%} characters can be quoted with more backslashes.
6047 Backslashes that quote @samp{%} characters or other backslashes are
6048 removed from the pattern before it is compared file names or has a stem
6049 substituted into it.  Backslashes that are not in danger of quoting
6050 @samp{%} characters go unmolested.  For example, the pattern
6051 @file{the\%weird\\%pattern\\} has @samp{the%weird\} preceding the
6052 operative @samp{%} character, and @samp{pattern\\} following it.  The
6053 final two backslashes are left alone because they cannot affect any
6054 @samp{%} character.@refill
6056 Whitespace between words is folded into single space characters;
6057 leading and trailing whitespace is discarded.
6059 For example,
6061 @example
6062 $(patsubst %.c,%.o,x.c.c bar.c)
6063 @end example
6065 @noindent
6066 produces the value @samp{x.c.o bar.o}.
6068 Substitution references (@pxref{Substitution Refs, ,Substitution
6069 References}) are a simpler way to get the effect of the @code{patsubst}
6070 function:
6072 @example
6073 $(@var{var}:@var{pattern}=@var{replacement})
6074 @end example
6076 @noindent
6077 is equivalent to
6079 @example
6080 $(patsubst @var{pattern},@var{replacement},$(@var{var}))
6081 @end example
6083 The second shorthand simplifies one of the most common uses of
6084 @code{patsubst}: replacing the suffix at the end of file names.
6086 @example
6087 $(@var{var}:@var{suffix}=@var{replacement})
6088 @end example
6090 @noindent
6091 is equivalent to
6093 @example
6094 $(patsubst %@var{suffix},%@var{replacement},$(@var{var}))
6095 @end example
6097 @noindent
6098 For example, you might have a list of object files:
6100 @example
6101 objects = foo.o bar.o baz.o
6102 @end example
6104 @noindent
6105 To get the list of corresponding source files, you could simply write:
6107 @example
6108 $(objects:.o=.c)
6109 @end example
6111 @noindent
6112 instead of using the general form:
6114 @example
6115 $(patsubst %.o,%.c,$(objects))
6116 @end example
6118 @item $(strip @var{string})
6119 @cindex stripping whitespace
6120 @cindex whitespace, stripping
6121 @cindex spaces, stripping
6122 @findex strip
6123 Removes leading and trailing whitespace from @var{string} and replaces
6124 each internal sequence of one or more whitespace characters with a
6125 single space.  Thus, @samp{$(strip a b  c )} results in @w{@samp{a b c}}.
6127 The function @code{strip} can be very useful when used in conjunction
6128 with conditionals.  When comparing something with the empty string
6129 @samp{} using @code{ifeq} or @code{ifneq}, you usually want a string of
6130 just whitespace to match the empty string (@pxref{Conditionals}).
6132 Thus, the following may fail to have the desired results:
6134 @example
6135 .PHONY: all
6136 ifneq   "$(needs_made)" ""
6137 all: $(needs_made)
6138 else
6139 all:;@@echo 'Nothing to make!'
6140 endif
6141 @end example
6143 @noindent
6144 Replacing the variable reference @w{@samp{$(needs_made)}} with the
6145 function call @w{@samp{$(strip $(needs_made))}} in the @code{ifneq}
6146 directive would make it more robust.@refill
6148 @item $(findstring @var{find},@var{in})
6149 @findex findstring
6150 @cindex searching for strings
6151 @cindex finding strings
6152 @cindex strings, searching for
6153 Searches @var{in} for an occurrence of @var{find}.  If it occurs, the
6154 value is @var{find}; otherwise, the value is empty.  You can use this
6155 function in a conditional to test for the presence of a specific
6156 substring in a given string.  Thus, the two examples,
6158 @example
6159 $(findstring a,a b c)
6160 $(findstring a,b c)
6161 @end example
6163 @noindent
6164 produce the values @samp{a} and @samp{} (the empty string),
6165 respectively.  @xref{Testing Flags}, for a practical application of
6166 @code{findstring}.@refill
6168 @need 750
6169 @findex filter
6170 @cindex filtering words
6171 @cindex words, filtering
6172 @item $(filter @var{pattern}@dots{},@var{text})
6173 Returns all whitespace-separated words in @var{text} that @emph{do} match
6174 any of the @var{pattern} words, removing any words that @emph{do not}
6175 match.  The patterns are written using @samp{%}, just like the patterns
6176 used in the @code{patsubst} function above.@refill
6178 The @code{filter} function can be used to separate out different types
6179 of strings (such as file names) in a variable.  For example:
6181 @example
6182 sources := foo.c bar.c baz.s ugh.h
6183 foo: $(sources)
6184         cc $(filter %.c %.s,$(sources)) -o foo
6185 @end example
6187 @noindent
6188 says that @file{foo} depends of @file{foo.c}, @file{bar.c},
6189 @file{baz.s} and @file{ugh.h} but only @file{foo.c}, @file{bar.c} and
6190 @file{baz.s} should be specified in the command to the
6191 compiler.@refill
6193 @item $(filter-out @var{pattern}@dots{},@var{text})
6194 @findex filter-out
6195 @cindex filtering out words
6196 @cindex words, filtering out
6197 Returns all whitespace-separated words in @var{text} that @emph{do not}
6198 match any of the @var{pattern} words, removing the words that @emph{do}
6199 match one or more.  This is the exact opposite of the @code{filter}
6200 function.@refill
6202 For example, given:
6204 @example
6205 @group
6206 objects=main1.o foo.o main2.o bar.o
6207 mains=main1.o main2.o
6208 @end group
6209 @end example
6211 @noindent
6212 the following generates a list which contains all the object files not
6213 in @samp{mains}:
6215 @example
6216 $(filter-out $(mains),$(objects))
6217 @end example
6219 @need 1500
6220 @findex sort
6221 @cindex sorting words
6222 @item $(sort @var{list})
6223 Sorts the words of @var{list} in lexical order, removing duplicate
6224 words.  The output is a list of words separated by single spaces.
6225 Thus,
6227 @example
6228 $(sort foo bar lose)
6229 @end example
6231 @noindent
6232 returns the value @samp{bar foo lose}.
6234 @cindex removing duplicate words
6235 @cindex duplicate words, removing
6236 @cindex words, removing duplicates
6237 Incidentally, since @code{sort} removes duplicate words, you can use
6238 it for this purpose even if you don't care about the sort order.
6240 @item $(word @var{n},@var{text})
6241 @findex word
6242 @cindex word, selecting a
6243 @cindex selecting a word
6244 Returns the @var{n}th word of @var{text}.  The legitimate values of
6245 @var{n} start from 1.  If @var{n} is bigger than the number of words
6246 in @var{text}, the value is empty.  For example,
6248 @example
6249 $(word 2, foo bar baz)
6250 @end example
6252 @noindent
6253 returns @samp{bar}.
6255 @item $(wordlist @var{s},@var{e},@var{text})
6256 @findex wordlist
6257 @cindex words, selecting lists of
6258 @cindex selecting word lists
6259 Returns the list of words in @var{text} starting with word @var{s} and
6260 ending with word @var{e} (inclusive).  The legitimate values of @var{s}
6261 start from 1; @var{e} may start from 0.  If @var{s} is bigger than the
6262 number of words in @var{text}, the value is empty.  If @var{e} is
6263 bigger than the number of words in @var{text}, words up to the end of
6264 @var{text} are returned.  If @var{s} is greater than @var{e}, nothing
6265 is returned.  For example,
6267 @example
6268 $(wordlist 2, 3, foo bar baz)
6269 @end example
6271 @noindent
6272 returns @samp{bar baz}.
6274 @c Following item phrased to prevent overfull hbox.  --RJC 17 Jul 92
6275 @item $(words @var{text})
6276 @findex words
6277 @cindex words, finding number
6278 Returns the number of words in @var{text}.
6279 Thus, the last word of @var{text} is
6280 @w{@code{$(word $(words @var{text}),@var{text})}}.@refill
6282 @item $(firstword @var{names}@dots{})
6283 @findex firstword
6284 @cindex words, extracting first
6285 The argument @var{names} is regarded as a series of names, separated
6286 by whitespace.  The value is the first name in the series.  The rest
6287 of the names are ignored.
6289 For example,
6291 @example
6292 $(firstword foo bar)
6293 @end example
6295 @noindent
6296 produces the result @samp{foo}.  Although @code{$(firstword
6297 @var{text})} is the same as @code{$(word 1,@var{text})}, the
6298 @code{firstword} function is retained for its simplicity.@refill
6301 @item $(lastword @var{names}@dots{})
6302 @findex lastword
6303 @cindex words, extracting last
6304 The argument @var{names} is regarded as a series of names, separated
6305 by whitespace.  The value is the last name in the series.
6307 For example,
6309 @example
6310 $(lastword foo bar)
6311 @end example
6313 @noindent
6314 produces the result @samp{bar}.  Although @code{$(lastword
6315 @var{text})} is the same as @code{$(word $(words @var{text}),@var{text})},
6316 the @code{lastword} function was added for its simplicity and better
6317 performance.@refill
6318 @end table
6321 Here is a realistic example of the use of @code{subst} and
6322 @code{patsubst}.  Suppose that a makefile uses the @code{VPATH} variable
6323 to specify a list of directories that @code{make} should search for
6324 prerequisite files
6325 (@pxref{General Search, , @code{VPATH} Search Path for All Prerequisites}).
6326 This example shows how to
6327 tell the C compiler to search for header files in the same list of
6328 directories.@refill
6330 The value of @code{VPATH} is a list of directories separated by colons,
6331 such as @samp{src:../headers}.  First, the @code{subst} function is used to
6332 change the colons to spaces:
6334 @example
6335 $(subst :, ,$(VPATH))
6336 @end example
6338 @noindent
6339 This produces @samp{src ../headers}.  Then @code{patsubst} is used to turn
6340 each directory name into a @samp{-I} flag.  These can be added to the
6341 value of the variable @code{CFLAGS}, which is passed automatically to the C
6342 compiler, like this:
6344 @example
6345 override CFLAGS += $(patsubst %,-I%,$(subst :, ,$(VPATH)))
6346 @end example
6348 @noindent
6349 The effect is to append the text @samp{-Isrc -I../headers} to the
6350 previously given value of @code{CFLAGS}.  The @code{override} directive is
6351 used so that the new value is assigned even if the previous value of
6352 @code{CFLAGS} was specified with a command argument (@pxref{Override
6353 Directive, , The @code{override} Directive}).
6355 @node File Name Functions, Foreach Function, Text Functions, Functions
6356 @section Functions for File Names
6357 @cindex functions, for file names
6358 @cindex file name functions
6360 Several of the built-in expansion functions relate specifically to
6361 taking apart file names or lists of file names.
6363 Each of the following functions performs a specific transformation on a
6364 file name.  The argument of the function is regarded as a series of file
6365 names, separated by whitespace.  (Leading and trailing whitespace is
6366 ignored.)  Each file name in the series is transformed in the same way and
6367 the results are concatenated with single spaces between them.
6369 @table @code
6370 @item $(dir @var{names}@dots{})
6371 @findex dir
6372 @cindex directory part
6373 @cindex file name, directory part
6374 Extracts the directory-part of each file name in @var{names}.  The
6375 directory-part of the file name is everything up through (and
6376 including) the last slash in it.  If the file name contains no slash,
6377 the directory part is the string @samp{./}.  For example,
6379 @example
6380 $(dir src/foo.c hacks)
6381 @end example
6383 @noindent
6384 produces the result @samp{src/ ./}.
6386 @item $(notdir @var{names}@dots{})
6387 @findex notdir
6388 @cindex file name, nondirectory part
6389 @cindex nondirectory part
6390 Extracts all but the directory-part of each file name in @var{names}.
6391 If the file name contains no slash, it is left unchanged.  Otherwise,
6392 everything through the last slash is removed from it.
6394 A file name that ends with a slash becomes an empty string.  This is
6395 unfortunate, because it means that the result does not always have the
6396 same number of whitespace-separated file names as the argument had;
6397 but we do not see any other valid alternative.
6399 For example,
6401 @example
6402 $(notdir src/foo.c hacks)
6403 @end example
6405 @noindent
6406 produces the result @samp{foo.c hacks}.
6408 @item $(suffix @var{names}@dots{})
6409 @findex suffix
6410 @cindex suffix, function to find
6411 @cindex file name suffix
6412 Extracts the suffix of each file name in @var{names}.  If the file name
6413 contains a period, the suffix is everything starting with the last
6414 period.  Otherwise, the suffix is the empty string.  This frequently
6415 means that the result will be empty when @var{names} is not, and if
6416 @var{names} contains multiple file names, the result may contain fewer
6417 file names.
6419 For example,
6421 @example
6422 $(suffix src/foo.c src-1.0/bar.c hacks)
6423 @end example
6425 @noindent
6426 produces the result @samp{.c .c}.
6428 @item $(basename @var{names}@dots{})
6429 @findex basename
6430 @cindex basename
6431 @cindex file name, basename of
6432 Extracts all but the suffix of each file name in @var{names}.  If the
6433 file name contains a period, the basename is everything starting up to
6434 (and not including) the last period.  Periods in the directory part are
6435 ignored.  If there is no period, the basename is the entire file name.
6436 For example,
6438 @example
6439 $(basename src/foo.c src-1.0/bar hacks)
6440 @end example
6442 @noindent
6443 produces the result @samp{src/foo src-1.0/bar hacks}.
6445 @c plural convention with dots (be consistent)
6446 @item $(addsuffix @var{suffix},@var{names}@dots{})
6447 @findex addsuffix
6448 @cindex suffix, adding
6449 @cindex file name suffix, adding
6450 The argument @var{names} is regarded as a series of names, separated
6451 by whitespace; @var{suffix} is used as a unit.  The value of
6452 @var{suffix} is appended to the end of each individual name and the
6453 resulting larger names are concatenated with single spaces between
6454 them.  For example,
6456 @example
6457 $(addsuffix .c,foo bar)
6458 @end example
6460 @noindent
6461 produces the result @samp{foo.c bar.c}.
6463 @item $(addprefix @var{prefix},@var{names}@dots{})
6464 @findex addprefix
6465 @cindex prefix, adding
6466 @cindex file name prefix, adding
6467 The argument @var{names} is regarded as a series of names, separated
6468 by whitespace; @var{prefix} is used as a unit.  The value of
6469 @var{prefix} is prepended to the front of each individual name and the
6470 resulting larger names are concatenated with single spaces between
6471 them.  For example,
6473 @example
6474 $(addprefix src/,foo bar)
6475 @end example
6477 @noindent
6478 produces the result @samp{src/foo src/bar}.
6480 @item $(join @var{list1},@var{list2})
6481 @findex join
6482 @cindex joining lists of words
6483 @cindex words, joining lists
6484 Concatenates the two arguments word by word: the two first words (one
6485 from each argument) concatenated form the first word of the result, the
6486 two second words form the second word of the result, and so on.  So the
6487 @var{n}th word of the result comes from the @var{n}th word of each
6488 argument.  If one argument has more words that the other, the extra
6489 words are copied unchanged into the result.
6491 For example, @samp{$(join a b,.c .o)} produces @samp{a.c b.o}.
6493 Whitespace between the words in the lists is not preserved; it is
6494 replaced with a single space.
6496 This function can merge the results of the @code{dir} and
6497 @code{notdir} functions, to produce the original list of files which
6498 was given to those two functions.@refill
6500 @item $(wildcard @var{pattern})
6501 @findex wildcard
6502 @cindex wildcard, function
6503 The argument @var{pattern} is a file name pattern, typically containing
6504 wildcard characters (as in shell file name patterns).  The result of
6505 @code{wildcard} is a space-separated list of the names of existing files
6506 that match the pattern.
6507 @xref{Wildcards, ,Using Wildcard Characters in File Names}.
6509 @item $(realpath @var{names}@dots{})
6510 @findex realpath
6511 @cindex realpath
6512 @cindex file name, realpath of
6513 For each file name in @var{names} return the canonical absolute name.
6514 A canonical name does not contain any @code{.} or @code{..} components,
6515 nor any repeated path separators (@code{/}) or symlinks. In case of a
6516 failure the empty string is returned. Consult the @code{realpath(3)}
6517 documentation for a list of possible failure causes.
6519 @item $(abspath @var{names}@dots{})
6520 @findex abspath
6521 @cindex abspath
6522 @cindex file name, abspath of
6523 For each file name in @var{names} return an absolute name that does
6524 not contain any @code{.} or @code{..} components, nor any repeated path
6525 separators (@code{/}). Note that in contrast to @code{realpath}
6526 function, @code{abspath} does not resolve symlinks and does not require
6527 the file names to refer to an existing file or directory. Use the
6528 @code{wildcard} function to test for existence.
6529 @end table
6531 @node Foreach Function, If Function, File Name Functions, Functions
6532 @section The @code{foreach} Function
6533 @findex foreach
6534 @cindex words, iterating over
6536 The @code{foreach} function is very different from other functions.  It
6537 causes one piece of text to be used repeatedly, each time with a different
6538 substitution performed on it.  It resembles the @code{for} command in the
6539 shell @code{sh} and the @code{foreach} command in the C-shell @code{csh}.
6541 The syntax of the @code{foreach} function is:
6543 @example
6544 $(foreach @var{var},@var{list},@var{text})
6545 @end example
6547 @noindent
6548 The first two arguments, @var{var} and @var{list}, are expanded before
6549 anything else is done; note that the last argument, @var{text}, is
6550 @strong{not} expanded at the same time.  Then for each word of the expanded
6551 value of @var{list}, the variable named by the expanded value of @var{var}
6552 is set to that word, and @var{text} is expanded.  Presumably @var{text}
6553 contains references to that variable, so its expansion will be different
6554 each time.
6556 The result is that @var{text} is expanded as many times as there are
6557 whitespace-separated words in @var{list}.  The multiple expansions of
6558 @var{text} are concatenated, with spaces between them, to make the result
6559 of @code{foreach}.
6561 This simple example sets the variable @samp{files} to the list of all files
6562 in the directories in the list @samp{dirs}:
6564 @example
6565 dirs := a b c d
6566 files := $(foreach dir,$(dirs),$(wildcard $(dir)/*))
6567 @end example
6569 Here @var{text} is @samp{$(wildcard $(dir)/*)}.  The first repetition
6570 finds the value @samp{a} for @code{dir}, so it produces the same result
6571 as @samp{$(wildcard a/*)}; the second repetition produces the result
6572 of @samp{$(wildcard b/*)}; and the third, that of @samp{$(wildcard c/*)}.
6574 This example has the same result (except for setting @samp{dirs}) as
6575 the following example:
6577 @example
6578 files := $(wildcard a/* b/* c/* d/*)
6579 @end example
6581 When @var{text} is complicated, you can improve readability by giving it
6582 a name, with an additional variable:
6584 @example
6585 find_files = $(wildcard $(dir)/*)
6586 dirs := a b c d
6587 files := $(foreach dir,$(dirs),$(find_files))
6588 @end example
6590 @noindent
6591 Here we use the variable @code{find_files} this way.  We use plain @samp{=}
6592 to define a recursively-expanding variable, so that its value contains an
6593 actual function call to be reexpanded under the control of @code{foreach};
6594 a simply-expanded variable would not do, since @code{wildcard} would be
6595 called only once at the time of defining @code{find_files}.
6597 The @code{foreach} function has no permanent effect on the variable
6598 @var{var}; its value and flavor after the @code{foreach} function call are
6599 the same as they were beforehand.  The other values which are taken from
6600 @var{list} are in effect only temporarily, during the execution of
6601 @code{foreach}.  The variable @var{var} is a simply-expanded variable
6602 during the execution of @code{foreach}.  If @var{var} was undefined
6603 before the @code{foreach} function call, it is undefined after the call.
6604 @xref{Flavors, ,The Two Flavors of Variables}.@refill
6606 You must take care when using complex variable expressions that result in
6607 variable names because many strange things are valid variable names, but
6608 are probably not what you intended.  For example,
6610 @smallexample
6611 files := $(foreach Esta escrito en espanol!,b c ch,$(find_files))
6612 @end smallexample
6614 @noindent
6615 might be useful if the value of @code{find_files} references the variable
6616 whose name is @samp{Esta escrito en espanol!} (es un nombre bastante largo,
6617 no?), but it is more likely to be a mistake.
6619 @node If Function, Call Function, Foreach Function, Functions
6620 @section The @code{if} Function
6621 @findex if
6622 @cindex conditional expansion
6624 The @code{if} function provides support for conditional expansion in a
6625 functional context (as opposed to the GNU @code{make} makefile
6626 conditionals such as @code{ifeq} (@pxref{Conditional Syntax, ,Syntax of
6627 Conditionals}).
6629 An @code{if} function call can contain either two or three arguments:
6631 @example
6632 $(if @var{condition},@var{then-part}[,@var{else-part}])
6633 @end example
6635 The first argument, @var{condition}, first has all preceding and
6636 trailing whitespace stripped, then is expanded.  If it expands to any
6637 non-empty string, then the condition is considered to be true.  If it
6638 expands to an empty string, the condition is considered to be false.
6640 If the condition is true then the second argument, @var{then-part}, is
6641 evaluated and this is used as the result of the evaluation of the entire
6642 @code{if} function.
6644 If the condition is false then the third argument, @var{else-part}, is
6645 evaluated and this is the result of the @code{if} function.  If there is
6646 no third argument, the @code{if} function evaluates to nothing (the
6647 empty string).
6649 Note that only one of the @var{then-part} or the @var{else-part} will be
6650 evaluated, never both.  Thus, either can contain side-effects (such as
6651 @code{shell} function calls, etc.)
6653 @node Call Function, Value Function, If Function, Functions
6654 @section The @code{call} Function
6655 @findex call
6656 @cindex functions, user defined
6657 @cindex user defined functions
6659 The @code{call} function is unique in that it can be used to create new
6660 parameterized functions.  You can write a complex expression as the
6661 value of a variable, then use @code{call} to expand it with different
6662 values.
6664 The syntax of the @code{call} function is:
6666 @example
6667 $(call @var{variable},@var{param},@var{param},@dots{})
6668 @end example
6670 When @code{make} expands this function, it assigns each @var{param} to
6671 temporary variables @code{$(1)}, @code{$(2)}, etc.  The variable
6672 @code{$(0)} will contain @var{variable}.  There is no maximum number of
6673 parameter arguments.  There is no minimum, either, but it doesn't make
6674 sense to use @code{call} with no parameters.
6676 Then @var{variable} is expanded as a @code{make} variable in the context
6677 of these temporary assignments.  Thus, any reference to @code{$(1)} in
6678 the value of @var{variable} will resolve to the first @var{param} in the
6679 invocation of @code{call}.
6681 Note that @var{variable} is the @emph{name} of a variable, not a
6682 @emph{reference} to that variable.  Therefore you would not normally use
6683 a @samp{$} or parentheses when writing it.  (You can, however, use a
6684 variable reference in the name if you want the name not to be a
6685 constant.)
6687 If @var{variable} is the name of a builtin function, the builtin function
6688 is always invoked (even if a @code{make} variable by that name also
6689 exists).
6691 The @code{call} function expands the @var{param} arguments before
6692 assigning them to temporary variables.  This means that @var{variable}
6693 values containing references to builtin functions that have special
6694 expansion rules, like @code{foreach} or @code{if}, may not work as you
6695 expect.
6697 Some examples may make this clearer.
6699 This macro simply reverses its arguments:
6701 @smallexample
6702 reverse = $(2) $(1)
6704 foo = $(call reverse,a,b)
6705 @end smallexample
6707 @noindent
6708 Here @var{foo} will contain @samp{b a}.
6710 This one is slightly more interesting: it defines a macro to search for
6711 the first instance of a program in @code{PATH}:
6713 @smallexample
6714 pathsearch = $(firstword $(wildcard $(addsuffix /$(1),$(subst :, ,$(PATH)))))
6716 LS := $(call pathsearch,ls)
6717 @end smallexample
6719 @noindent
6720 Now the variable LS contains @code{/bin/ls} or similar.
6722 The @code{call} function can be nested.  Each recursive invocation gets
6723 its own local values for @code{$(1)}, etc. that mask the values of
6724 higher-level @code{call}.  For example, here is an implementation of a
6725 @dfn{map} function:
6727 @smallexample
6728 map = $(foreach a,$(2),$(call $(1),$(a)))
6729 @end smallexample
6731 Now you can @var{map} a function that normally takes only one argument,
6732 such as @code{origin}, to multiple values in one step:
6734 @smallexample
6735 o = $(call map,origin,o map MAKE)
6736 @end smallexample
6738 and end up with @var{o} containing something like @samp{file file default}.
6740 A final caution: be careful when adding whitespace to the arguments to
6741 @code{call}.  As with other functions, any whitespace contained in the
6742 second and subsequent arguments is kept; this can cause strange
6743 effects.  It's generally safest to remove all extraneous whitespace when
6744 providing parameters to @code{call}.
6746 @node Value Function, Eval Function, Call Function, Functions
6747 @comment  node-name,  next,  previous,  up
6748 @section The @code{value} Function
6749 @findex value
6750 @cindex variables, unexpanded value
6752 The @code{value} function provides a way for you to use the value of a
6753 variable @emph{without} having it expanded.  Please note that this
6754 does not undo expansions which have already occurred; for example if
6755 you create a simply expanded variable its value is expanded during the
6756 definition; in that case the @code{value} function will return the
6757 same result as using the variable directly.
6759 The syntax of the @code{value} function is:
6761 @example
6762 $(value @var{variable})
6763 @end example
6765 Note that @var{variable} is the @emph{name} of a variable; not a
6766 @emph{reference} to that variable.  Therefore you would not normally
6767 use a @samp{$} or parentheses when writing it.  (You can, however, use
6768 a variable reference in the name if you want the name not to be a
6769 constant.)
6771 The result of this function is a string containing the value of
6772 @var{variable}, without any expansion occurring.  For example, in this
6773 makefile:
6775 @example
6776 @group
6777 FOO = $PATH
6779 all:
6780         @@echo $(FOO)
6781         @@echo $(value FOO)
6782 @end group
6783 @end example
6785 @noindent
6786 The first output line would be @code{ATH}, since the ``$P'' would be
6787 expanded as a @code{make} variable, while the second output line would
6788 be the current value of your @code{$PATH} environment variable, since
6789 the @code{value} function avoided the expansion.
6791 The @code{value} function is most often used in conjunction with the
6792 @code{eval} function (@pxref{Eval Function}).
6794 @node Eval Function, Origin Function, Value Function, Functions
6795 @comment  node-name,  next,  previous,  up
6796 @section The @code{eval} Function
6797 @findex eval
6798 @cindex evaluating makefile syntax
6799 @cindex makefile syntax, evaluating
6801 The @code{eval} function is very special: it allows you to define new
6802 makefile constructs that are not constant; which are the result of
6803 evaluating other variables and functions.  The argument to the
6804 @code{eval} function is expanded, then the results of that expansion
6805 are parsed as makefile syntax.  The expanded results can define new
6806 @code{make} variables, targets, implicit or explicit rules, etc.
6808 The result of the @code{eval} function is always the empty string;
6809 thus, it can be placed virtually anywhere in a makefile without
6810 causing syntax errors.
6812 It's important to realize that the @code{eval} argument is expanded
6813 @emph{twice}; first by the @code{eval} function, then the results of
6814 that expansion are expanded again when they are parsed as makefile
6815 syntax.  This means you may need to provide extra levels of escaping
6816 for ``$'' characters when using @code{eval}.  The @code{value}
6817 function (@pxref{Value Function}) can sometimes be useful in these
6818 situations, to circumvent unwanted expansions.
6820 Here is an example of how @code{eval} can be used; this example
6821 combines a number of concepts and other functions.  Although it might
6822 seem overly complex to use @code{eval} in this example, rather than
6823 just writing out the rules, consider two things: first, the template
6824 definition (in @code{PROGRAM_template}) could need to be much more
6825 complex than it is here; and second, you might put the complex,
6826 ``generic'' part of this example into another makefile, then include
6827 it in all the individual makefiles.  Now your individual makefiles are
6828 quite straightforward.
6830 @example
6831 @group
6832 PROGRAMS    = server client
6834 server_OBJS = server.o server_priv.o server_access.o
6835 server_LIBS = priv protocol
6837 client_OBJS = client.o client_api.o client_mem.o
6838 client_LIBS = protocol
6840 # Everything after this is generic
6842 .PHONY: all
6843 all: $(PROGRAMS)
6845 define PROGRAM_template
6846  $(1): $$($(1)_OBJS) $$($(1)_LIBS:%=-l%)
6847  ALL_OBJS   += $$($(1)_OBJS)
6848 endef
6850 $(foreach prog,$(PROGRAMS),$(eval $(call PROGRAM_template,$(prog))))
6852 $(PROGRAMS):
6853         $(LINK.o) $^ $(LDLIBS) -o $@@
6855 clean:
6856         rm -f $(ALL_OBJS) $(PROGRAMS)
6857 @end group
6858 @end example
6860 @node Origin Function, Shell Function, Eval Function, Functions
6861 @section The @code{origin} Function
6862 @findex origin
6863 @cindex variables, origin of
6864 @cindex origin of variable
6866 The @code{origin} function is unlike most other functions in that it does
6867 not operate on the values of variables; it tells you something @emph{about}
6868 a variable.  Specifically, it tells you where it came from.
6870 The syntax of the @code{origin} function is:
6872 @example
6873 $(origin @var{variable})
6874 @end example
6876 Note that @var{variable} is the @emph{name} of a variable to inquire about;
6877 not a @emph{reference} to that variable.  Therefore you would not normally
6878 use a @samp{$} or parentheses when writing it.  (You can, however, use a
6879 variable reference in the name if you want the name not to be a constant.)
6881 The result of this function is a string telling you how the variable
6882 @var{variable} was defined:
6884 @table @samp
6885 @item undefined
6887 if @var{variable} was never defined.
6889 @item default
6891 if @var{variable} has a default definition, as is usual with @code{CC}
6892 and so on.  @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
6893 Note that if you have redefined a default variable, the @code{origin}
6894 function will return the origin of the later definition.
6896 @item environment
6898 if @var{variable} was defined as an environment variable and the
6899 @samp{-e} option is @emph{not} turned on (@pxref{Options Summary, ,Summary of Options}).
6901 @item environment override
6903 if @var{variable} was defined as an environment variable and the
6904 @w{@samp{-e}} option @emph{is} turned on (@pxref{Options Summary,
6905 ,Summary of Options}).@refill
6907 @item file
6909 if @var{variable} was defined in a makefile.
6911 @item command line
6913 if @var{variable} was defined on the command line.
6915 @item override
6917 if @var{variable} was defined with an @code{override} directive in a
6918 makefile (@pxref{Override Directive, ,The @code{override} Directive}).
6920 @item automatic
6922 if @var{variable} is an automatic variable defined for the
6923 execution of the commands for each rule
6924 (@pxref{Automatic Variables}).
6925 @end table
6927 This information is primarily useful (other than for your curiosity) to
6928 determine if you want to believe the value of a variable.  For example,
6929 suppose you have a makefile @file{foo} that includes another makefile
6930 @file{bar}.  You want a variable @code{bletch} to be defined in @file{bar}
6931 if you run the command @w{@samp{make -f bar}}, even if the environment contains
6932 a definition of @code{bletch}.  However, if @file{foo} defined
6933 @code{bletch} before including @file{bar}, you do not want to override that
6934 definition.  This could be done by using an @code{override} directive in
6935 @file{foo}, giving that definition precedence over the later definition in
6936 @file{bar}; unfortunately, the @code{override} directive would also
6937 override any command line definitions.  So, @file{bar} could
6938 include:@refill
6940 @example
6941 @group
6942 ifdef bletch
6943 ifeq "$(origin bletch)" "environment"
6944 bletch = barf, gag, etc.
6945 endif
6946 endif
6947 @end group
6948 @end example
6950 @noindent
6951 If @code{bletch} has been defined from the environment, this will redefine
6954 If you want to override a previous definition of @code{bletch} if it came
6955 from the environment, even under @samp{-e}, you could instead write:
6957 @example
6958 @group
6959 ifneq "$(findstring environment,$(origin bletch))" ""
6960 bletch = barf, gag, etc.
6961 endif
6962 @end group
6963 @end example
6965 Here the redefinition takes place if @samp{$(origin bletch)} returns either
6966 @samp{environment} or @samp{environment override}.
6967 @xref{Text Functions, , Functions for String Substitution and Analysis}.
6969 @node Shell Function, Make Control Functions, Origin Function, Functions
6970 @section The @code{shell} Function
6971 @findex shell
6972 @cindex commands, expansion
6973 @cindex backquotes
6974 @cindex shell command, function for
6976 The @code{shell} function is unlike any other function other than the
6977 @code{wildcard} function
6978 (@pxref{Wildcard Function, ,The Function @code{wildcard}}) in that it
6979 communicates with the world outside of @code{make}.
6981 The @code{shell} function performs the same function that backquotes
6982 (@samp{`}) perform in most shells: it does @dfn{command expansion}.
6983 This means that it takes as an argument a shell command and evaluates
6984 to the output of the command.  The only processing @code{make} does on
6985 the result is to convert each newline (or carriage-return / newline
6986 pair) to a single space.  If there is a trailing (carriage-return
6987 and) newline it will simply be removed.@refill
6989 The commands run by calls to the @code{shell} function are run when the
6990 function calls are expanded (@pxref{Reading Makefiles, , How
6991 @code{make} Reads a Makefile}).  Because this function involves
6992 spawning a new shell, you should carefully consider the performance
6993 implications of using the @code{shell} function within recursively
6994 expanded variables vs. simply expanded variables (@pxref{Flavors, ,The
6995 Two Flavors of Variables}).
6997 Here are some examples of the use of the @code{shell} function:
6999 @example
7000 contents := $(shell cat foo)
7001 @end example
7003 @noindent
7004 sets @code{contents} to the contents of the file @file{foo}, with a space
7005 (rather than a newline) separating each line.
7007 @example
7008 files := $(shell echo *.c)
7009 @end example
7011 @noindent
7012 sets @code{files} to the expansion of @samp{*.c}.  Unless @code{make} is
7013 using a very strange shell, this has the same result as
7014 @w{@samp{$(wildcard *.c)}} (as long as at least one @samp{.c} file
7015 exists).@refill
7017 @node Make Control Functions,  , Shell Function, Functions
7018 @section Functions That Control Make
7019 @cindex functions, for controlling make
7020 @cindex controlling make
7022 These functions control the way make runs.  Generally, they are used to
7023 provide information to the user of the makefile or to cause make to stop
7024 if some sort of environmental error is detected.
7026 @table @code
7027 @item $(error @var{text}@dots{})
7028 @findex error
7029 @cindex error, stopping on
7030 @cindex stopping make
7031 Generates a fatal error where the message is @var{text}.  Note that the
7032 error is generated whenever this function is evaluated.  So, if you put
7033 it inside a command script or on the right side of a recursive variable
7034 assignment, it won't be evaluated until later.  The @var{text} will be
7035 expanded before the error is generated.
7037 For example,
7039 @example
7040 ifdef ERROR1
7041 $(error error is $(ERROR1))
7042 endif
7043 @end example
7045 @noindent
7046 will generate a fatal error during the read of the makefile if the
7047 @code{make} variable @code{ERROR1} is defined.  Or,
7049 @example
7050 ERR = $(error found an error!)
7052 .PHONY: err
7053 err: ; $(ERR)
7054 @end example
7056 @noindent
7057 will generate a fatal error while @code{make} is running, if the
7058 @code{err} target is invoked.
7060 @item $(warning @var{text}@dots{})
7061 @findex warning
7062 @cindex warnings, printing
7063 @cindex printing user warnings
7064 This function works similarly to the @code{error} function, above,
7065 except that @code{make} doesn't exit.  Instead, @var{text} is expanded
7066 and the resulting message is displayed, but processing of the makefile
7067 continues.
7069 The result of the expansion of this function is the empty string.
7071 @item $(info @var{text}@dots{})
7072 @findex info
7073 @cindex printing messages
7074 This function does nothing more than print its (expanded) argument(s)
7075 to standard output.  No makefile name or line number is added.  The
7076 result of the expansion of this function is the empty string.
7077 @end table
7079 @node Running, Implicit Rules, Functions, Top
7080 @chapter How to Run @code{make}
7082 A makefile that says how to recompile a program can be used in more
7083 than one way.  The simplest use is to recompile every file that is out
7084 of date.  Usually, makefiles are written so that if you run
7085 @code{make} with no arguments, it does just that.
7087 But you might want to update only some of the files; you might want to use
7088 a different compiler or different compiler options; you might want just to
7089 find out which files are out of date without changing them.
7091 By giving arguments when you run @code{make}, you can do any of these
7092 things and many others.
7094 The exit status of @code{make} is always one of three values:
7095 @table @code
7096 @item 0
7097 The exit status is zero if @code{make} is successful.
7098 @item 2
7099 The exit status is two if @code{make} encounters any errors.
7100 It will print messages describing the particular errors.
7101 @item 1
7102 The exit status is one if you use the @samp{-q} flag and @code{make}
7103 determines that some target is not already up to date.
7104 @xref{Instead of Execution, ,Instead of Executing the Commands}.
7105 @end table
7107 @menu
7108 * Makefile Arguments::          How to specify which makefile to use.
7109 * Goals::                       How to use goal arguments to specify which
7110                                   parts of the makefile to use.
7111 * Instead of Execution::        How to use mode flags to specify what
7112                                   kind of thing to do with the commands
7113                                   in the makefile other than simply
7114                                   execute them.
7115 * Avoiding Compilation::        How to avoid recompiling certain files.
7116 * Overriding::                  How to override a variable to specify
7117                                   an alternate compiler and other things.
7118 * Testing::                     How to proceed past some errors, to
7119                                   test compilation.
7120 * Options Summary::             Summary of Options
7121 @end menu
7123 @node Makefile Arguments, Goals, Running, Running
7124 @section Arguments to Specify the Makefile
7125 @cindex @code{--file}
7126 @cindex @code{--makefile}
7127 @cindex @code{-f}
7129 The way to specify the name of the makefile is with the @samp{-f} or
7130 @samp{--file} option (@samp{--makefile} also works).  For example,
7131 @samp{-f altmake} says to use the file @file{altmake} as the makefile.
7133 If you use the @samp{-f} flag several times and follow each @samp{-f}
7134 with an argument, all the specified files are used jointly as
7135 makefiles.
7137 If you do not use the @samp{-f} or @samp{--file} flag, the default is
7138 to try @file{GNUmakefile}, @file{makefile}, and @file{Makefile}, in
7139 that order, and use the first of these three which exists or can be made
7140 (@pxref{Makefiles, ,Writing Makefiles}).@refill
7142 @node Goals, Instead of Execution, Makefile Arguments, Running
7143 @section Arguments to Specify the Goals
7144 @cindex goal, how to specify
7146 The @dfn{goals} are the targets that @code{make} should strive ultimately
7147 to update.  Other targets are updated as well if they appear as
7148 prerequisites of goals, or prerequisites of prerequisites of goals, etc.
7150 By default, the goal is the first target in the makefile (not counting
7151 targets that start with a period).  Therefore, makefiles are usually
7152 written so that the first target is for compiling the entire program or
7153 programs they describe.  If the first rule in the makefile has several
7154 targets, only the first target in the rule becomes the default goal, not
7155 the whole list.  You can manage the selection of the default goal from
7156 within your makefile using the @code{.DEFAULT_GOAL} variable
7157 (@pxref{Special Variables, , Other Special Variables}).
7159 You can also specify a different goal or goals with command-line
7160 arguments to @code{make}.  Use the name of the goal as an argument.
7161 If you specify several goals, @code{make} processes each of them in
7162 turn, in the order you name them.
7164 Any target in the makefile may be specified as a goal (unless it
7165 starts with @samp{-} or contains an @samp{=}, in which case it will be
7166 parsed as a switch or variable definition, respectively).  Even
7167 targets not in the makefile may be specified, if @code{make} can find
7168 implicit rules that say how to make them.
7170 @vindex MAKECMDGOALS
7171 @code{Make} will set the special variable @code{MAKECMDGOALS} to the
7172 list of goals you specified on the command line.  If no goals were given
7173 on the command line, this variable is empty.  Note that this variable
7174 should be used only in special circumstances.
7176 An example of appropriate use is to avoid including @file{.d} files
7177 during @code{clean} rules (@pxref{Automatic Prerequisites}), so
7178 @code{make} won't create them only to immediately remove them
7179 again:@refill
7181 @example
7182 @group
7183 sources = foo.c bar.c
7185 ifneq ($(MAKECMDGOALS),clean)
7186 include $(sources:.c=.d)
7187 endif
7188 @end group
7189 @end example
7191 One use of specifying a goal is if you want to compile only a part of
7192 the program, or only one of several programs.  Specify as a goal each
7193 file that you wish to remake.  For example, consider a directory containing
7194 several programs, with a makefile that starts like this:
7196 @example
7197 .PHONY: all
7198 all: size nm ld ar as
7199 @end example
7201 If you are working on the program @code{size}, you might want to say
7202 @w{@samp{make size}} so that only the files of that program are recompiled.
7204 Another use of specifying a goal is to make files that are not normally
7205 made.  For example, there may be a file of debugging output, or a
7206 version of the program that is compiled specially for testing, which has
7207 a rule in the makefile but is not a prerequisite of the default goal.
7209 Another use of specifying a goal is to run the commands associated with
7210 a phony target (@pxref{Phony Targets}) or empty target (@pxref{Empty
7211 Targets, ,Empty Target Files to Record Events}).  Many makefiles contain
7212 a phony target named @file{clean} which deletes everything except source
7213 files.  Naturally, this is done only if you request it explicitly with
7214 @w{@samp{make clean}}.  Following is a list of typical phony and empty
7215 target names.  @xref{Standard Targets}, for a detailed list of all the
7216 standard target names which GNU software packages use.
7218 @table @file
7219 @item all
7220 @cindex @code{all} @r{(standard target)}
7221 Make all the top-level targets the makefile knows about.
7223 @item clean
7224 @cindex @code{clean} @r{(standard target)}
7225 Delete all files that are normally created by running @code{make}.
7227 @item mostlyclean
7228 @cindex @code{mostlyclean} @r{(standard target)}
7229 Like @samp{clean}, but may refrain from deleting a few files that people
7230 normally don't want to recompile.  For example, the @samp{mostlyclean}
7231 target for GCC does not delete @file{libgcc.a}, because recompiling it
7232 is rarely necessary and takes a lot of time.
7234 @item distclean
7235 @cindex @code{distclean} @r{(standard target)}
7236 @itemx realclean
7237 @cindex @code{realclean} @r{(standard target)}
7238 @itemx clobber
7239 @cindex @code{clobber} @r{(standard target)}
7240 Any of these targets might be defined to delete @emph{more} files than
7241 @samp{clean} does.  For example, this would delete configuration files
7242 or links that you would normally create as preparation for compilation,
7243 even if the makefile itself cannot create these files.
7245 @item install
7246 @cindex @code{install} @r{(standard target)}
7247 Copy the executable file into a directory that users typically search
7248 for commands; copy any auxiliary files that the executable uses into
7249 the directories where it will look for them.
7251 @item print
7252 @cindex @code{print} @r{(standard target)}
7253 Print listings of the source files that have changed.
7255 @item tar
7256 @cindex @code{tar} @r{(standard target)}
7257 Create a tar file of the source files.
7259 @item shar
7260 @cindex @code{shar} @r{(standard target)}
7261 Create a shell archive (shar file) of the source files.
7263 @item dist
7264 @cindex @code{dist} @r{(standard target)}
7265 Create a distribution file of the source files.  This might
7266 be a tar file, or a shar file, or a compressed version of one of the
7267 above, or even more than one of the above.
7269 @item TAGS
7270 @cindex @code{TAGS} @r{(standard target)}
7271 Update a tags table for this program.
7273 @item check
7274 @cindex @code{check} @r{(standard target)}
7275 @itemx test
7276 @cindex @code{test} @r{(standard target)}
7277 Perform self tests on the program this makefile builds.
7278 @end table
7280 @node Instead of Execution, Avoiding Compilation, Goals, Running
7281 @section Instead of Executing the Commands
7282 @cindex execution, instead of
7283 @cindex commands, instead of executing
7285 The makefile tells @code{make} how to tell whether a target is up to date,
7286 and how to update each target.  But updating the targets is not always
7287 what you want.  Certain options specify other activities for @code{make}.
7289 @comment Extra blank lines make it print better.
7290 @table @samp
7291 @item -n
7292 @itemx --just-print
7293 @itemx --dry-run
7294 @itemx --recon
7295 @cindex @code{--just-print}
7296 @cindex @code{--dry-run}
7297 @cindex @code{--recon}
7298 @cindex @code{-n}
7300 ``No-op''.  The activity is to print what commands would be used to make
7301 the targets up to date, but not actually execute them.
7303 @item -t
7304 @itemx --touch
7305 @cindex @code{--touch}
7306 @cindex touching files
7307 @cindex target, touching
7308 @cindex @code{-t}
7310 ``Touch''.  The activity is to mark the targets as up to date without
7311 actually changing them.  In other words, @code{make} pretends to compile
7312 the targets but does not really change their contents.
7314 @item -q
7315 @itemx --question
7316 @cindex @code{--question}
7317 @cindex @code{-q}
7318 @cindex question mode
7320 ``Question''.  The activity is to find out silently whether the targets
7321 are up to date already; but execute no commands in either case.  In other
7322 words, neither compilation nor output will occur.
7324 @item -W @var{file}
7325 @itemx --what-if=@var{file}
7326 @itemx --assume-new=@var{file}
7327 @itemx --new-file=@var{file}
7328 @cindex @code{--what-if}
7329 @cindex @code{-W}
7330 @cindex @code{--assume-new}
7331 @cindex @code{--new-file}
7332 @cindex what if
7333 @cindex files, assuming new
7335 ``What if''.  Each @samp{-W} flag is followed by a file name.  The given
7336 files' modification times are recorded by @code{make} as being the present
7337 time, although the actual modification times remain the same.
7338 You can use the @samp{-W} flag in conjunction with the @samp{-n} flag
7339 to see what would happen if you were to modify specific files.@refill
7340 @end table
7342 With the @samp{-n} flag, @code{make} prints the commands that it would
7343 normally execute but does not execute them.
7345 With the @samp{-t} flag, @code{make} ignores the commands in the rules
7346 and uses (in effect) the command @code{touch} for each target that needs to
7347 be remade.  The @code{touch} command is also printed, unless @samp{-s} or
7348 @code{.SILENT} is used.  For speed, @code{make} does not actually invoke
7349 the program @code{touch}.  It does the work directly.
7351 With the @samp{-q} flag, @code{make} prints nothing and executes no
7352 commands, but the exit status code it returns is zero if and only if the
7353 targets to be considered are already up to date.  If the exit status is
7354 one, then some updating needs to be done.  If @code{make} encounters an
7355 error, the exit status is two, so you can distinguish an error from a
7356 target that is not up to date.
7358 It is an error to use more than one of these three flags in the same
7359 invocation of @code{make}.
7361 @cindex +, and command execution
7362 The @samp{-n}, @samp{-t}, and @samp{-q} options do not affect command
7363 lines that begin with @samp{+} characters or contain the strings
7364 @samp{$(MAKE)} or @samp{$@{MAKE@}}.  Note that only the line containing
7365 the @samp{+} character or the strings @samp{$(MAKE)} or @samp{$@{MAKE@}}
7366 is run regardless of these options.  Other lines in the same rule are
7367 not run unless they too begin with @samp{+} or contain @samp{$(MAKE)} or
7368 @samp{$@{MAKE@}} (@xref{MAKE Variable, ,How the @code{MAKE} Variable Works}.)
7370 The @samp{-W} flag provides two features:
7372 @itemize @bullet
7373 @item
7374 If you also use the @samp{-n} or @samp{-q} flag, you can see what
7375 @code{make} would do if you were to modify some files.
7377 @item
7378 Without the @samp{-n} or @samp{-q} flag, when @code{make} is actually
7379 executing commands, the @samp{-W} flag can direct @code{make} to act
7380 as if some files had been modified, without actually modifying the
7381 files.@refill
7382 @end itemize
7384 Note that the options @samp{-p} and @samp{-v} allow you to obtain other
7385 information about @code{make} or about the makefiles in use
7386 (@pxref{Options Summary, ,Summary of Options}).@refill
7388 @node Avoiding Compilation, Overriding, Instead of Execution, Running
7389 @section Avoiding Recompilation of Some Files
7390 @cindex @code{-o}
7391 @cindex @code{--old-file}
7392 @cindex @code{--assume-old}
7393 @cindex files, assuming old
7394 @cindex files, avoiding recompilation of
7395 @cindex recompilation, avoiding
7397 Sometimes you may have changed a source file but you do not want to
7398 recompile all the files that depend on it.  For example, suppose you add
7399 a macro or a declaration to a header file that many other files depend
7400 on.  Being conservative, @code{make} assumes that any change in the
7401 header file requires recompilation of all dependent files, but you know
7402 that they do not need to be recompiled and you would rather not waste
7403 the time waiting for them to compile.
7405 If you anticipate the problem before changing the header file, you can
7406 use the @samp{-t} flag.  This flag tells @code{make} not to run the
7407 commands in the rules, but rather to mark the target up to date by
7408 changing its last-modification date.  You would follow this procedure:
7410 @enumerate
7411 @item
7412 Use the command @samp{make} to recompile the source files that really
7413 need recompilation, ensuring that the object files are up-to-date
7414 before you begin.
7416 @item
7417 Make the changes in the header files.
7419 @item
7420 Use the command @samp{make -t} to mark all the object files as
7421 up to date.  The next time you run @code{make}, the changes in the
7422 header files will not cause any recompilation.
7423 @end enumerate
7425 If you have already changed the header file at a time when some files
7426 do need recompilation, it is too late to do this.  Instead, you can
7427 use the @w{@samp{-o @var{file}}} flag, which marks a specified file as
7428 ``old'' (@pxref{Options Summary, ,Summary of Options}).  This means
7429 that the file itself will not be remade, and nothing else will be
7430 remade on its account.  Follow this procedure:
7432 @enumerate
7433 @item
7434 Recompile the source files that need compilation for reasons independent
7435 of the particular header file, with @samp{make -o @var{headerfile}}.
7436 If several header files are involved, use a separate @samp{-o} option
7437 for each header file.
7439 @item
7440 Touch all the object files with @samp{make -t}.
7441 @end enumerate
7443 @node Overriding, Testing, Avoiding Compilation, Running
7444 @section Overriding Variables
7445 @cindex overriding variables with arguments
7446 @cindex variables, overriding with arguments
7447 @cindex command line variables
7448 @cindex variables, command line
7450 An argument that contains @samp{=} specifies the value of a variable:
7451 @samp{@var{v}=@var{x}} sets the value of the variable @var{v} to @var{x}.
7452 If you specify a value in this way, all ordinary assignments of the same
7453 variable in the makefile are ignored; we say they have been
7454 @dfn{overridden} by the command line argument.
7456 The most common way to use this facility is to pass extra flags to
7457 compilers.  For example, in a properly written makefile, the variable
7458 @code{CFLAGS} is included in each command that runs the C compiler, so a
7459 file @file{foo.c} would be compiled something like this:
7461 @example
7462 cc -c $(CFLAGS) foo.c
7463 @end example
7465 Thus, whatever value you set for @code{CFLAGS} affects each compilation
7466 that occurs.  The makefile probably specifies the usual value for
7467 @code{CFLAGS}, like this:
7469 @example
7470 CFLAGS=-g
7471 @end example
7473 Each time you run @code{make}, you can override this value if you
7474 wish.  For example, if you say @samp{make CFLAGS='-g -O'}, each C
7475 compilation will be done with @samp{cc -c -g -O}.  (This also
7476 illustrates how you can use quoting in the shell to enclose spaces and
7477 other special characters in the value of a variable when you override
7478 it.)
7480 The variable @code{CFLAGS} is only one of many standard variables that
7481 exist just so that you can change them this way.  @xref{Implicit
7482 Variables, , Variables Used by Implicit Rules}, for a complete list.
7484 You can also program the makefile to look at additional variables of your
7485 own, giving the user the ability to control other aspects of how the
7486 makefile works by changing the variables.
7488 When you override a variable with a command argument, you can define either
7489 a recursively-expanded variable or a simply-expanded variable.  The
7490 examples shown above make a recursively-expanded variable; to make a
7491 simply-expanded variable, write @samp{:=} instead of @samp{=}.  But, unless
7492 you want to include a variable reference or function call in the
7493 @emph{value} that you specify, it makes no difference which kind of
7494 variable you create.
7496 There is one way that the makefile can change a variable that you have
7497 overridden.  This is to use the @code{override} directive, which is a line
7498 that looks like this: @samp{override @var{variable} = @var{value}}
7499 (@pxref{Override Directive, ,The @code{override} Directive}).
7501 @node Testing, Options Summary, Overriding, Running
7502 @section Testing the Compilation of a Program
7503 @cindex testing compilation
7504 @cindex compilation, testing
7506 Normally, when an error happens in executing a shell command, @code{make}
7507 gives up immediately, returning a nonzero status.  No further commands are
7508 executed for any target.  The error implies that the goal cannot be
7509 correctly remade, and @code{make} reports this as soon as it knows.
7511 When you are compiling a program that you have just changed, this is not
7512 what you want.  Instead, you would rather that @code{make} try compiling
7513 every file that can be tried, to show you as many compilation errors
7514 as possible.
7516 @cindex @code{-k}
7517 @cindex @code{--keep-going}
7518 On these occasions, you should use the @samp{-k} or
7519 @samp{--keep-going} flag.  This tells @code{make} to continue to
7520 consider the other prerequisites of the pending targets, remaking them
7521 if necessary, before it gives up and returns nonzero status.  For
7522 example, after an error in compiling one object file, @samp{make -k}
7523 will continue compiling other object files even though it already
7524 knows that linking them will be impossible.  In addition to continuing
7525 after failed shell commands, @samp{make -k} will continue as much as
7526 possible after discovering that it does not know how to make a target
7527 or prerequisite file.  This will always cause an error message, but
7528 without @samp{-k}, it is a fatal error (@pxref{Options Summary,
7529 ,Summary of Options}).@refill
7531 The usual behavior of @code{make} assumes that your purpose is to get the
7532 goals up to date; once @code{make} learns that this is impossible, it might
7533 as well report the failure immediately.  The @samp{-k} flag says that the
7534 real purpose is to test as much as possible of the changes made in the
7535 program, perhaps to find several independent problems so that you can
7536 correct them all before the next attempt to compile.  This is why Emacs'
7537 @kbd{M-x compile} command passes the @samp{-k} flag by default.
7539 @node Options Summary,  , Testing, Running
7540 @section Summary of Options
7541 @cindex options
7542 @cindex flags
7543 @cindex switches
7545 Here is a table of all the options @code{make} understands:
7547 @table @samp
7548 @item -b
7549 @cindex @code{-b}
7550 @itemx -m
7551 @cindex @code{-m}
7552 These options are ignored for compatibility with other versions of @code{make}.
7554 @item -B
7555 @cindex @code{-B}
7556 @itemx --always-make
7557 @cindex @code{--always-make}
7558 Consider all targets out-of-date.  GNU @code{make} proceeds to
7559 consider targets and their prerequisites using the normal algorithms;
7560 however, all targets so considered are always remade regardless of the
7561 status of their prerequisites.  To avoid infinite recursion, if
7562 @code{MAKE_RESTARTS} (@pxref{Special Variables, , Other Special
7563 Variables}) is set to a number greater than 0 this option is disabled
7564 when considering whether to remake makefiles (@pxref{Remaking
7565 Makefiles, , How Makefiles Are Remade}).
7567 @item -C @var{dir}
7568 @cindex @code{-C}
7569 @itemx --directory=@var{dir}
7570 @cindex @code{--directory}
7571 Change to directory @var{dir} before reading the makefiles.  If multiple
7572 @samp{-C} options are specified, each is interpreted relative to the
7573 previous one: @samp{-C / -C etc} is equivalent to @samp{-C /etc}.
7574 This is typically used with recursive invocations of @code{make}
7575 (@pxref{Recursion, ,Recursive Use of @code{make}}).
7577 @item -d
7578 @cindex @code{-d}
7579 @c Extra blank line here makes the table look better.
7581 Print debugging information in addition to normal processing.  The
7582 debugging information says which files are being considered for
7583 remaking, which file-times are being compared and with what results,
7584 which files actually need to be remade, which implicit rules are
7585 considered and which are applied---everything interesting about how
7586 @code{make} decides what to do.  The @code{-d} option is equivalent to
7587 @samp{--debug=a} (see below).
7589 @item --debug[=@var{options}]
7590 @cindex @code{--debug}
7591 @c Extra blank line here makes the table look better.
7593 Print debugging information in addition to normal processing.  Various
7594 levels and types of output can be chosen.  With no arguments, print the
7595 ``basic'' level of debugging.  Possible arguments are below; only the
7596 first character is considered, and values must be comma- or
7597 space-separated.
7599 @table @code
7600 @item a (@i{all})
7601 All types of debugging output are enabled.  This is equivalent to using
7602 @samp{-d}.
7604 @item b (@i{basic})
7605 Basic debugging prints each target that was found to be out-of-date, and
7606 whether the build was successful or not.
7608 @item v (@i{verbose})
7609 A level above @samp{basic}; includes messages about which makefiles were
7610 parsed, prerequisites that did not need to be rebuilt, etc.  This option
7611 also enables @samp{basic} messages.
7613 @item i (@i{implicit})
7614 Prints messages describing the implicit rule searches for each target.
7615 This option also enables @samp{basic} messages.
7617 @item j (@i{jobs})
7618 Prints messages giving details on the invocation of specific subcommands.
7620 @item m (@i{makefile})
7621 By default, the above messages are not enabled while trying to remake
7622 the makefiles.  This option enables messages while rebuilding makefiles,
7623 too.  Note that the @samp{all} option does enable this option.  This
7624 option also enables @samp{basic} messages.
7625 @end table
7627 @item -e
7628 @cindex @code{-e}
7629 @itemx --environment-overrides
7630 @cindex @code{--environment-overrides}
7631 Give variables taken from the environment precedence
7632 over variables from makefiles.
7633 @xref{Environment, ,Variables from the Environment}.
7635 @item -f @var{file}
7636 @cindex @code{-f}
7637 @itemx --file=@var{file}
7638 @cindex @code{--file}
7639 @itemx --makefile=@var{file}
7640 @cindex @code{--makefile}
7641 Read the file named @var{file} as a makefile.
7642 @xref{Makefiles, ,Writing Makefiles}.
7644 @item -h
7645 @cindex @code{-h}
7646 @itemx --help
7647 @cindex @code{--help}
7648 @c Extra blank line here makes the table look better.
7650 Remind you of the options that @code{make} understands and then exit.
7652 @item -i
7653 @cindex @code{-i}
7654 @itemx --ignore-errors
7655 @cindex @code{--ignore-errors}
7656 Ignore all errors in commands executed to remake files.
7657 @xref{Errors, ,Errors in Commands}.
7659 @item -I @var{dir}
7660 @cindex @code{-I}
7661 @itemx --include-dir=@var{dir}
7662 @cindex @code{--include-dir}
7663 Specifies a directory @var{dir} to search for included makefiles.
7664 @xref{Include, ,Including Other Makefiles}.  If several @samp{-I}
7665 options are used to specify several directories, the directories are
7666 searched in the order specified.
7668 @item -j [@var{jobs}]
7669 @cindex @code{-j}
7670 @itemx --jobs[=@var{jobs}]
7671 @cindex @code{--jobs}
7672 Specifies the number of jobs (commands) to run simultaneously.  With no
7673 argument, @code{make} runs as many jobs simultaneously as possible.  If
7674 there is more than one @samp{-j} option, the last one is effective.
7675 @xref{Parallel, ,Parallel Execution},
7676 for more information on how commands are run.
7677 Note that this option is ignored on MS-DOS.
7679 @item -k
7680 @cindex @code{-k}
7681 @itemx --keep-going
7682 @cindex @code{--keep-going}
7683 Continue as much as possible after an error.  While the target that
7684 failed, and those that depend on it, cannot be remade, the other
7685 prerequisites of these targets can be processed all the same.
7686 @xref{Testing, ,Testing the Compilation of a Program}.
7688 @item -l [@var{load}]
7689 @cindex @code{-l}
7690 @itemx --load-average[=@var{load}]
7691 @cindex @code{--load-average}
7692 @itemx --max-load[=@var{load}]
7693 @cindex @code{--max-load}
7694 Specifies that no new jobs (commands) should be started if there are
7695 other jobs running and the load average is at least @var{load} (a
7696 floating-point number).  With no argument, removes a previous load
7697 limit.  @xref{Parallel, ,Parallel Execution}.
7699 @item -L
7700 @cindex @code{-L}
7701 @itemx --check-symlink-times
7702 @cindex @code{--check-symlink-times}
7703 On systems that support symbolic links, this option causes @code{make}
7704 to consider the timestamps on any symbolic links in addition to the
7705 timestamp on the file referenced by those links.  When this option is
7706 provided, the most recent timestamp among the file and the symbolic
7707 links is taken as the modification time for this target file.
7709 @item -n
7710 @cindex @code{-n}
7711 @itemx --just-print
7712 @cindex @code{--just-print}
7713 @itemx --dry-run
7714 @cindex @code{--dry-run}
7715 @itemx --recon
7716 @cindex @code{--recon}
7717 @c Extra blank line here makes the table look better.
7719 Print the commands that would be executed, but do not execute them.
7720 @xref{Instead of Execution, ,Instead of Executing the Commands}.
7722 @item -o @var{file}
7723 @cindex @code{-o}
7724 @itemx --old-file=@var{file}
7725 @cindex @code{--old-file}
7726 @itemx --assume-old=@var{file}
7727 @cindex @code{--assume-old}
7728 Do not remake the file @var{file} even if it is older than its
7729 prerequisites, and do not remake anything on account of changes in
7730 @var{file}.  Essentially the file is treated as very old and its rules
7731 are ignored.  @xref{Avoiding Compilation, ,Avoiding Recompilation of
7732 Some Files}.@refill
7734 @item -p
7735 @cindex @code{-p}
7736 @itemx --print-data-base
7737 @cindex @code{--print-data-base}
7738 @cindex data base of @code{make} rules
7739 @cindex predefined rules and variables, printing
7740 Print the data base (rules and variable values) that results from
7741 reading the makefiles; then execute as usual or as otherwise specified.
7742 This also prints the version information given by the @samp{-v} switch
7743 (see below).  To print the data base without trying to remake any files,
7744 use @w{@samp{make -qp}}.  To print the data base of predefined rules and
7745 variables, use @w{@samp{make -p -f /dev/null}}.  The data base output
7746 contains filename and linenumber information for command and variable
7747 definitions, so it can be a useful debugging tool in complex environments.
7749 @item -q
7750 @cindex @code{-q}
7751 @itemx --question
7752 @cindex @code{--question}
7753 ``Question mode''.  Do not run any commands, or print anything; just
7754 return an exit status that is zero if the specified targets are already
7755 up to date, one if any remaking is required, or two if an error is
7756 encountered.  @xref{Instead of Execution, ,Instead of Executing the
7757 Commands}.@refill
7759 @item -r
7760 @cindex @code{-r}
7761 @itemx --no-builtin-rules
7762 @cindex @code{--no-builtin-rules}
7763 Eliminate use of the built-in implicit rules (@pxref{Implicit Rules,
7764 ,Using Implicit Rules}).  You can still define your own by writing
7765 pattern rules (@pxref{Pattern Rules, ,Defining and Redefining Pattern
7766 Rules}).  The @samp{-r} option also clears out the default list of
7767 suffixes for suffix rules (@pxref{Suffix Rules, ,Old-Fashioned Suffix
7768 Rules}).  But you can still define your own suffixes with a rule for
7769 @code{.SUFFIXES}, and then define your own suffix rules.  Note that only
7770 @emph{rules} are affected by the @code{-r} option; default variables
7771 remain in effect (@pxref{Implicit Variables, ,Variables Used by Implicit
7772 Rules}); see the @samp{-R} option below.
7774 @item -R
7775 @cindex @code{-R}
7776 @itemx --no-builtin-variables
7777 @cindex @code{--no-builtin-variables}
7778 Eliminate use of the built-in rule-specific variables (@pxref{Implicit
7779 Variables, ,Variables Used by Implicit Rules}).  You can still define
7780 your own, of course.  The @samp{-R} option also automatically enables
7781 the @samp{-r} option (see above), since it doesn't make sense to have
7782 implicit rules without any definitions for the variables that they use.
7784 @item -s
7785 @cindex @code{-s}
7786 @itemx --silent
7787 @cindex @code{--silent}
7788 @itemx --quiet
7789 @cindex @code{--quiet}
7790 @c Extra blank line here makes the table look better.
7792 Silent operation; do not print the commands as they are executed.
7793 @xref{Echoing, ,Command Echoing}.
7795 @item -S
7796 @cindex @code{-S}
7797 @itemx --no-keep-going
7798 @cindex @code{--no-keep-going}
7799 @itemx --stop
7800 @cindex @code{--stop}
7801 @c Extra blank line here makes the table look better.
7803 Cancel the effect of the @samp{-k} option.  This is never necessary
7804 except in a recursive @code{make} where @samp{-k} might be inherited
7805 from the top-level @code{make} via @code{MAKEFLAGS}
7806 (@pxref{Recursion, ,Recursive Use of @code{make}})
7807 or if you set @samp{-k} in @code{MAKEFLAGS} in your environment.@refill
7809 @item -t
7810 @cindex @code{-t}
7811 @itemx --touch
7812 @cindex @code{--touch}
7813 @c Extra blank line here makes the table look better.
7815 Touch files (mark them up to date without really changing them)
7816 instead of running their commands.  This is used to pretend that the
7817 commands were done, in order to fool future invocations of
7818 @code{make}.  @xref{Instead of Execution, ,Instead of Executing the Commands}.
7820 @item -v
7821 @cindex @code{-v}
7822 @itemx --version
7823 @cindex @code{--version}
7824 Print the version of the @code{make} program plus a copyright, a list
7825 of authors, and a notice that there is no warranty; then exit.
7827 @item -w
7828 @cindex @code{-w}
7829 @itemx --print-directory
7830 @cindex @code{--print-directory}
7831 Print a message containing the working directory both before and after
7832 executing the makefile.  This may be useful for tracking down errors
7833 from complicated nests of recursive @code{make} commands.
7834 @xref{Recursion, ,Recursive Use of @code{make}}.  (In practice, you
7835 rarely need to specify this option since @samp{make} does it for you;
7836 see @ref{-w Option, ,The @samp{--print-directory} Option}.)
7838 @itemx --no-print-directory
7839 @cindex @code{--no-print-directory}
7840 Disable printing of the working directory under @code{-w}.
7841 This option is useful when @code{-w} is turned on automatically,
7842 but you do not want to see the extra messages.
7843 @xref{-w Option, ,The @samp{--print-directory} Option}.
7845 @item -W @var{file}
7846 @cindex @code{-W}
7847 @itemx --what-if=@var{file}
7848 @cindex @code{--what-if}
7849 @itemx --new-file=@var{file}
7850 @cindex @code{--new-file}
7851 @itemx --assume-new=@var{file}
7852 @cindex @code{--assume-new}
7853 Pretend that the target @var{file} has just been modified.  When used
7854 with the @samp{-n} flag, this shows you what would happen if you were
7855 to modify that file.  Without @samp{-n}, it is almost the same as
7856 running a @code{touch} command on the given file before running
7857 @code{make}, except that the modification time is changed only in the
7858 imagination of @code{make}.
7859 @xref{Instead of Execution, ,Instead of Executing the Commands}.
7861 @item --warn-undefined-variables
7862 @cindex @code{--warn-undefined-variables}
7863 @cindex variables, warning for undefined
7864 @cindex undefined variables, warning message
7865 Issue a warning message whenever @code{make} sees a reference to an
7866 undefined variable.  This can be helpful when you are trying to debug
7867 makefiles which use variables in complex ways.
7868 @end table
7870 @node Implicit Rules, Archives, Running, Top
7871 @chapter Using Implicit Rules
7872 @cindex implicit rule
7873 @cindex rule, implicit
7875 Certain standard ways of remaking target files are used very often.  For
7876 example, one customary way to make an object file is from a C source file
7877 using the C compiler, @code{cc}.
7879 @dfn{Implicit rules} tell @code{make} how to use customary techniques so
7880 that you do not have to specify them in detail when you want to use
7881 them.  For example, there is an implicit rule for C compilation.  File
7882 names determine which implicit rules are run.  For example, C
7883 compilation typically takes a @file{.c} file and makes a @file{.o} file.
7884 So @code{make} applies the implicit rule for C compilation when it sees
7885 this combination of file name endings.@refill
7887 A chain of implicit rules can apply in sequence; for example, @code{make}
7888 will remake a @file{.o} file from a @file{.y} file by way of a @file{.c} file.
7889 @iftex
7890 @xref{Chained Rules, ,Chains of Implicit Rules}.
7891 @end iftex
7893 The built-in implicit rules use several variables in their commands so
7894 that, by changing the values of the variables, you can change the way the
7895 implicit rule works.  For example, the variable @code{CFLAGS} controls the
7896 flags given to the C compiler by the implicit rule for C compilation.
7897 @iftex
7898 @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
7899 @end iftex
7901 You can define your own implicit rules by writing @dfn{pattern rules}.
7902 @iftex
7903 @xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.
7904 @end iftex
7906 @dfn{Suffix rules} are a more limited way to define implicit rules.
7907 Pattern rules are more general and clearer, but suffix rules are
7908 retained for compatibility.
7909 @iftex
7910 @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
7911 @end iftex
7913 @menu
7914 * Using Implicit::              How to use an existing implicit rule
7915                                   to get the commands for updating a file.
7916 * Catalogue of Rules::          A list of built-in implicit rules.
7917 * Implicit Variables::          How to change what predefined rules do.
7918 * Chained Rules::               How to use a chain of implicit rules.
7919 * Pattern Rules::               How to define new implicit rules.
7920 * Last Resort::                 How to define commands for rules which
7921                                 cannot find any.
7922 * Suffix Rules::                The old-fashioned style of implicit rule.
7923 * Implicit Rule Search::        The precise algorithm for applying
7924                                   implicit rules.
7925 @end menu
7927 @node Using Implicit, Catalogue of Rules, Implicit Rules, Implicit Rules
7928 @section Using Implicit Rules
7929 @cindex implicit rule, how to use
7930 @cindex rule, implicit, how to use
7932 To allow @code{make} to find a customary method for updating a target file,
7933 all you have to do is refrain from specifying commands yourself.  Either
7934 write a rule with no command lines, or don't write a rule at all.  Then
7935 @code{make} will figure out which implicit rule to use based on which
7936 kind of source file exists or can be made.
7938 For example, suppose the makefile looks like this:
7940 @example
7941 foo : foo.o bar.o
7942         cc -o foo foo.o bar.o $(CFLAGS) $(LDFLAGS)
7943 @end example
7945 @noindent
7946 Because you mention @file{foo.o} but do not give a rule for it, @code{make}
7947 will automatically look for an implicit rule that tells how to update it.
7948 This happens whether or not the file @file{foo.o} currently exists.
7950 If an implicit rule is found, it can supply both commands and one or
7951 more prerequisites (the source files).  You would want to write a rule
7952 for @file{foo.o} with no command lines if you need to specify additional
7953 prerequisites, such as header files, that the implicit rule cannot
7954 supply.
7956 Each implicit rule has a target pattern and prerequisite patterns.  There may
7957 be many implicit rules with the same target pattern.  For example, numerous
7958 rules make @samp{.o} files: one, from a @samp{.c} file with the C compiler;
7959 another, from a @samp{.p} file with the Pascal compiler; and so on.  The rule
7960 that actually applies is the one whose prerequisites exist or can be made.
7961 So, if you have a file @file{foo.c}, @code{make} will run the C compiler;
7962 otherwise, if you have a file @file{foo.p}, @code{make} will run the Pascal
7963 compiler; and so on.
7965 Of course, when you write the makefile, you know which implicit rule you
7966 want @code{make} to use, and you know it will choose that one because you
7967 know which possible prerequisite files are supposed to exist.
7968 @xref{Catalogue of Rules, ,Catalogue of Implicit Rules},
7969 for a catalogue of all the predefined implicit rules.
7971 Above, we said an implicit rule applies if the required prerequisites ``exist
7972 or can be made''.  A file ``can be made'' if it is mentioned explicitly in
7973 the makefile as a target or a prerequisite, or if an implicit rule can be
7974 recursively found for how to make it.  When an implicit prerequisite is the
7975 result of another implicit rule, we say that @dfn{chaining} is occurring.
7976 @xref{Chained Rules, ,Chains of Implicit Rules}.
7978 In general, @code{make} searches for an implicit rule for each target, and
7979 for each double-colon rule, that has no commands.  A file that is mentioned
7980 only as a prerequisite is considered a target whose rule specifies nothing,
7981 so implicit rule search happens for it.  @xref{Implicit Rule Search, ,Implicit Rule Search Algorithm}, for the
7982 details of how the search is done.
7984 Note that explicit prerequisites do not influence implicit rule search.
7985 For example, consider this explicit rule:
7987 @example
7988 foo.o: foo.p
7989 @end example
7991 @noindent
7992 The prerequisite on @file{foo.p} does not necessarily mean that
7993 @code{make} will remake @file{foo.o} according to the implicit rule to
7994 make an object file, a @file{.o} file, from a Pascal source file, a
7995 @file{.p} file.  For example, if @file{foo.c} also exists, the implicit
7996 rule to make an object file from a C source file is used instead,
7997 because it appears before the Pascal rule in the list of predefined
7998 implicit rules (@pxref{Catalogue of Rules, , Catalogue of Implicit
7999 Rules}).
8001 If you do not want an implicit rule to be used for a target that has no
8002 commands, you can give that target empty commands by writing a semicolon
8003 (@pxref{Empty Commands, ,Defining Empty Commands}).
8005 @node Catalogue of Rules, Implicit Variables, Using Implicit, Implicit Rules
8006 @section Catalogue of Implicit Rules
8007 @cindex implicit rule, predefined
8008 @cindex rule, implicit, predefined
8010 Here is a catalogue of predefined implicit rules which are always
8011 available unless the makefile explicitly overrides or cancels them.
8012 @xref{Canceling Rules, ,Canceling Implicit Rules}, for information on
8013 canceling or overriding an implicit rule.  The @samp{-r} or
8014 @samp{--no-builtin-rules} option cancels all predefined rules.
8016 Not all of these rules will always be defined, even when the @samp{-r}
8017 option is not given.  Many of the predefined implicit rules are
8018 implemented in @code{make} as suffix rules, so which ones will be
8019 defined depends on the @dfn{suffix list} (the list of prerequisites of
8020 the special target @code{.SUFFIXES}).  The default suffix list is:
8021 @code{.out}, @code{.a}, @code{.ln}, @code{.o}, @code{.c}, @code{.cc},
8022 @code{.C}, @code{.p}, @code{.f}, @code{.F}, @code{.r}, @code{.y},
8023 @code{.l}, @code{.s}, @code{.S}, @code{.mod}, @code{.sym}, @code{.def},
8024 @code{.h}, @code{.info}, @code{.dvi}, @code{.tex}, @code{.texinfo},
8025 @code{.texi}, @code{.txinfo}, @code{.w}, @code{.ch} @code{.web},
8026 @code{.sh}, @code{.elc}, @code{.el}.  All of the implicit rules
8027 described below whose prerequisites have one of these suffixes are
8028 actually suffix rules.  If you modify the suffix list, the only
8029 predefined suffix rules in effect will be those named by one or two of
8030 the suffixes that are on the list you specify; rules whose suffixes fail
8031 to be on the list are disabled.  @xref{Suffix Rules, ,Old-Fashioned
8032 Suffix Rules}, for full details on suffix rules.
8034 @table @asis
8035 @item Compiling C programs
8036 @cindex C, rule to compile
8037 @pindex cc
8038 @pindex gcc
8039 @pindex .o
8040 @pindex .c
8041 @file{@var{n}.o} is made automatically from @file{@var{n}.c} with
8042 a command of the form @samp{$(CC) -c $(CPPFLAGS) $(CFLAGS)}.@refill
8044 @item Compiling C++ programs
8045 @cindex C++, rule to compile
8046 @pindex g++
8047 @pindex .C
8048 @pindex .cc
8049 @file{@var{n}.o} is made automatically from @file{@var{n}.cc} or
8050 @file{@var{n}.C} with a command of the form @samp{$(CXX) -c $(CPPFLAGS)
8051 $(CXXFLAGS)}.  We encourage you to use the suffix @samp{.cc} for C++
8052 source files instead of @samp{.C}.@refill
8054 @item Compiling Pascal programs
8055 @cindex Pascal, rule to compile
8056 @pindex pc
8057 @pindex .p
8058 @file{@var{n}.o} is made automatically from @file{@var{n}.p}
8059 with the command @samp{$(PC) -c $(PFLAGS)}.@refill
8061 @item Compiling Fortran and Ratfor programs
8062 @cindex Fortran, rule to compile
8063 @cindex Ratfor, rule to compile
8064 @pindex f77
8065 @pindex .f
8066 @pindex .r
8067 @pindex .F
8068 @file{@var{n}.o} is made automatically from @file{@var{n}.r},
8069 @file{@var{n}.F} or @file{@var{n}.f} by running the
8070 Fortran compiler.  The precise command used is as follows:@refill
8072 @table @samp
8073 @item .f
8074 @samp{$(FC) -c $(FFLAGS)}.
8075 @item .F
8076 @samp{$(FC) -c $(FFLAGS) $(CPPFLAGS)}.
8077 @item .r
8078 @samp{$(FC) -c $(FFLAGS) $(RFLAGS)}.
8079 @end table
8081 @item Preprocessing Fortran and Ratfor programs
8082 @file{@var{n}.f} is made automatically from @file{@var{n}.r} or
8083 @file{@var{n}.F}.  This rule runs just the preprocessor to convert a
8084 Ratfor or preprocessable Fortran program into a strict Fortran
8085 program.  The precise command used is as follows:@refill
8087 @table @samp
8088 @item .F
8089 @samp{$(FC) -F $(CPPFLAGS) $(FFLAGS)}.
8090 @item .r
8091 @samp{$(FC) -F $(FFLAGS) $(RFLAGS)}.
8092 @end table
8094 @item Compiling Modula-2 programs
8095 @cindex Modula-2, rule to compile
8096 @pindex m2c
8097 @pindex .sym
8098 @pindex .def
8099 @pindex .mod
8100 @file{@var{n}.sym} is made from @file{@var{n}.def} with a command
8101 of the form @samp{$(M2C) $(M2FLAGS) $(DEFFLAGS)}.  @file{@var{n}.o}
8102 is made from @file{@var{n}.mod}; the form is:
8103 @w{@samp{$(M2C) $(M2FLAGS) $(MODFLAGS)}}.@refill
8105 @need 1200
8106 @item Assembling and preprocessing assembler programs
8107 @cindex assembly, rule to compile
8108 @pindex as
8109 @pindex .s
8110 @file{@var{n}.o} is made automatically from @file{@var{n}.s} by
8111 running the assembler, @code{as}.  The precise command is
8112 @samp{$(AS) $(ASFLAGS)}.@refill
8114 @pindex .S
8115 @file{@var{n}.s} is made automatically from @file{@var{n}.S} by
8116 running the C preprocessor, @code{cpp}.  The precise command is
8117 @w{@samp{$(CPP) $(CPPFLAGS)}}.
8119 @item Linking a single object file
8120 @cindex linking, predefined rule for
8121 @pindex ld
8122 @pindex .o
8123 @file{@var{n}} is made automatically from @file{@var{n}.o} by running
8124 the linker (usually called @code{ld}) via the C compiler.  The precise
8125 command used is @w{@samp{$(CC) $(LDFLAGS) @var{n}.o $(LOADLIBES) $(LDLIBS)}}.
8127 This rule does the right thing for a simple program with only one
8128 source file.  It will also do the right thing if there are multiple
8129 object files (presumably coming from various other source files), one
8130 of which has a name matching that of the executable file.  Thus,
8132 @example
8133 x: y.o z.o
8134 @end example
8136 @noindent
8137 when @file{x.c}, @file{y.c} and @file{z.c} all exist will execute:
8139 @example
8140 @group
8141 cc -c x.c -o x.o
8142 cc -c y.c -o y.o
8143 cc -c z.c -o z.o
8144 cc x.o y.o z.o -o x
8145 rm -f x.o
8146 rm -f y.o
8147 rm -f z.o
8148 @end group
8149 @end example
8151 @noindent
8152 In more complicated cases, such as when there is no object file whose
8153 name derives from the executable file name, you must write an explicit
8154 command for linking.
8156 Each kind of file automatically made into @samp{.o} object files will
8157 be automatically linked by using the compiler (@samp{$(CC)},
8158 @samp{$(FC)} or @samp{$(PC)}; the C compiler @samp{$(CC)} is used to
8159 assemble @samp{.s} files) without the @samp{-c} option.  This could be
8160 done by using the @samp{.o} object files as intermediates, but it is
8161 faster to do the compiling and linking in one step, so that's how it's
8162 done.@refill
8164 @item Yacc for C programs
8165 @pindex yacc
8166 @cindex Yacc, rule to run
8167 @pindex .y
8168 @file{@var{n}.c} is made automatically from @file{@var{n}.y} by
8169 running Yacc with the command @samp{$(YACC) $(YFLAGS)}.
8171 @item Lex for C programs
8172 @pindex lex
8173 @cindex Lex, rule to run
8174 @pindex .l
8175 @file{@var{n}.c} is made automatically from @file{@var{n}.l} by
8176 running Lex.  The actual command is @samp{$(LEX) $(LFLAGS)}.
8178 @item Lex for Ratfor programs
8179 @file{@var{n}.r} is made automatically from @file{@var{n}.l} by
8180 running Lex.  The actual command is @samp{$(LEX) $(LFLAGS)}.
8182 The convention of using the same suffix @samp{.l} for all Lex files
8183 regardless of whether they produce C code or Ratfor code makes it
8184 impossible for @code{make} to determine automatically which of the two
8185 languages you are using in any particular case.  If @code{make} is
8186 called upon to remake an object file from a @samp{.l} file, it must
8187 guess which compiler to use.  It will guess the C compiler, because
8188 that is more common.  If you are using Ratfor, make sure @code{make}
8189 knows this by mentioning @file{@var{n}.r} in the makefile.  Or, if you
8190 are using Ratfor exclusively, with no C files, remove @samp{.c} from
8191 the list of implicit rule suffixes with:@refill
8193 @example
8194 @group
8195 .SUFFIXES:
8196 .SUFFIXES: .o .r .f .l @dots{}
8197 @end group
8198 @end example
8200 @item Making Lint Libraries from C, Yacc, or Lex programs
8201 @pindex lint
8202 @cindex @code{lint}, rule to run
8203 @pindex .ln
8204 @file{@var{n}.ln} is made from @file{@var{n}.c} by running @code{lint}.
8205 The precise command is @w{@samp{$(LINT) $(LINTFLAGS) $(CPPFLAGS) -i}}.
8206 The same command is used on the C code produced from
8207 @file{@var{n}.y} or @file{@var{n}.l}.@refill
8209 @item @TeX{} and Web
8210 @cindex @TeX{}, rule to run
8211 @cindex Web, rule to run
8212 @pindex tex
8213 @pindex cweave
8214 @pindex weave
8215 @pindex tangle
8216 @pindex ctangle
8217 @pindex .dvi
8218 @pindex .tex
8219 @pindex .web
8220 @pindex .w
8221 @pindex .ch
8222 @file{@var{n}.dvi} is made from @file{@var{n}.tex} with the command
8223 @samp{$(TEX)}.  @file{@var{n}.tex} is made from @file{@var{n}.web} with
8224 @samp{$(WEAVE)}, or from @file{@var{n}.w} (and from @file{@var{n}.ch} if
8225 it exists or can be made) with @samp{$(CWEAVE)}.  @file{@var{n}.p} is
8226 made from @file{@var{n}.web} with @samp{$(TANGLE)} and @file{@var{n}.c}
8227 is made from @file{@var{n}.w} (and from @file{@var{n}.ch} if it exists
8228 or can be made) with @samp{$(CTANGLE)}.@refill
8230 @item Texinfo and Info
8231 @cindex Texinfo, rule to format
8232 @cindex Info, rule to format
8233 @pindex texi2dvi
8234 @pindex makeinfo
8235 @pindex .texinfo
8236 @pindex .info
8237 @pindex .texi
8238 @pindex .txinfo
8239 @file{@var{n}.dvi} is made from @file{@var{n}.texinfo},
8240 @file{@var{n}.texi}, or @file{@var{n}.txinfo}, with the command
8241 @w{@samp{$(TEXI2DVI) $(TEXI2DVI_FLAGS)}}.  @file{@var{n}.info} is made from
8242 @file{@var{n}.texinfo}, @file{@var{n}.texi}, or @file{@var{n}.txinfo}, with
8243 the command @w{@samp{$(MAKEINFO) $(MAKEINFO_FLAGS)}}.
8245 @item RCS
8246 @cindex RCS, rule to extract from
8247 @pindex co
8248 @pindex ,v @r{(RCS file extension)}
8249 Any file @file{@var{n}} is extracted if necessary from an RCS file
8250 named either @file{@var{n},v} or @file{RCS/@var{n},v}.  The precise
8251 command used is @w{@samp{$(CO) $(COFLAGS)}}.  @file{@var{n}} will not be
8252 extracted from RCS if it already exists, even if the RCS file is
8253 newer.  The rules for RCS are terminal
8254 (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}),
8255 so RCS files cannot be generated from another source; they must
8256 actually exist.@refill
8258 @item SCCS
8259 @cindex SCCS, rule to extract from
8260 @pindex get
8261 @pindex s. @r{(SCCS file prefix)}
8262 Any file @file{@var{n}} is extracted if necessary from an SCCS file
8263 named either @file{s.@var{n}} or @file{SCCS/s.@var{n}}.  The precise
8264 command used is @w{@samp{$(GET) $(GFLAGS)}}.  The rules for SCCS are
8265 terminal (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}),
8266 so SCCS files cannot be generated from another source; they must
8267 actually exist.@refill
8269 @pindex .sh
8270 For the benefit of SCCS, a file @file{@var{n}} is copied from
8271 @file{@var{n}.sh} and made executable (by everyone).  This is for
8272 shell scripts that are checked into SCCS.  Since RCS preserves the
8273 execution permission of a file, you do not need to use this feature
8274 with RCS.@refill
8276 We recommend that you avoid using of SCCS.  RCS is widely held to be
8277 superior, and is also free.  By choosing free software in place of
8278 comparable (or inferior) proprietary software, you support the free
8279 software movement.
8280 @end table
8282 Usually, you want to change only the variables listed in the table
8283 above, which are documented in the following section.
8285 However, the commands in built-in implicit rules actually use
8286 variables such as @code{COMPILE.c}, @code{LINK.p}, and
8287 @code{PREPROCESS.S}, whose values contain the commands listed above.
8289 @code{make} follows the convention that the rule to compile a
8290 @file{.@var{x}} source file uses the variable @code{COMPILE.@var{x}}.
8291 Similarly, the rule to produce an executable from a @file{.@var{x}}
8292 file uses @code{LINK.@var{x}}; and the rule to preprocess a
8293 @file{.@var{x}} file uses @code{PREPROCESS.@var{x}}.
8295 @vindex OUTPUT_OPTION
8296 Every rule that produces an object file uses the variable
8297 @code{OUTPUT_OPTION}.  @code{make} defines this variable either to
8298 contain @samp{-o $@@}, or to be empty, depending on a compile-time
8299 option.  You need the @samp{-o} option to ensure that the output goes
8300 into the right file when the source file is in a different directory,
8301 as when using @code{VPATH} (@pxref{Directory Search}).  However,
8302 compilers on some systems do not accept a @samp{-o} switch for object
8303 files.  If you use such a system, and use @code{VPATH}, some
8304 compilations will put their output in the wrong place.
8305 A possible workaround for this problem is to give @code{OUTPUT_OPTION}
8306 the value @w{@samp{; mv $*.o $@@}}.
8308 @node Implicit Variables, Chained Rules, Catalogue of Rules, Implicit Rules
8309 @section Variables Used by Implicit Rules
8310 @cindex flags for compilers
8312 The commands in built-in implicit rules make liberal use of certain
8313 predefined variables.  You can alter these variables in the makefile,
8314 with arguments to @code{make}, or in the environment to alter how the
8315 implicit rules work without redefining the rules themselves.  You can
8316 cancel all variables used by implicit rules with the @samp{-R} or
8317 @samp{--no-builtin-variables} option.
8319 For example, the command used to compile a C source file actually says
8320 @samp{$(CC) -c $(CFLAGS) $(CPPFLAGS)}.  The default values of the variables
8321 used are @samp{cc} and nothing, resulting in the command @samp{cc -c}.  By
8322 redefining @samp{CC} to @samp{ncc}, you could cause @samp{ncc} to be
8323 used for all C compilations performed by the implicit rule.  By redefining
8324 @samp{CFLAGS} to be @samp{-g}, you could pass the @samp{-g} option to
8325 each compilation.  @emph{All} implicit rules that do C compilation use
8326 @samp{$(CC)} to get the program name for the compiler and @emph{all}
8327 include @samp{$(CFLAGS)} among the arguments given to the compiler.@refill
8329 The variables used in implicit rules fall into two classes: those that are
8330 names of programs (like @code{CC}) and those that contain arguments for the
8331 programs (like @code{CFLAGS}).  (The ``name of a program'' may also contain
8332 some command arguments, but it must start with an actual executable program
8333 name.)  If a variable value contains more than one argument, separate them
8334 with spaces.
8336 Here is a table of variables used as names of programs in built-in rules:
8338 @table @code
8339 @item AR
8340 @vindex AR
8341 Archive-maintaining program; default @samp{ar}.
8342 @pindex ar
8344 @item AS
8345 @vindex AS
8346 Program for doing assembly; default @samp{as}.
8347 @pindex as
8349 @item CC
8350 @vindex CC
8351 Program for compiling C programs; default @samp{cc}.
8352 @pindex cc
8354 @item CXX
8355 @vindex CXX
8356 Program for compiling C++ programs; default @samp{g++}.
8357 @pindex g++
8359 @item CO
8360 @vindex CO
8361 Program for extracting a file from RCS; default @samp{co}.
8362 @pindex co
8364 @item CPP
8365 @vindex CPP
8366 Program for running the C preprocessor, with results to standard output;
8367 default @samp{$(CC) -E}.
8369 @item FC
8370 @vindex FC
8371 Program for compiling or preprocessing Fortran and Ratfor programs;
8372 default @samp{f77}.
8373 @pindex f77
8375 @item GET
8376 @vindex GET
8377 Program for extracting a file from SCCS; default @samp{get}.
8378 @pindex get
8380 @item LEX
8381 @vindex LEX
8382 Program to use to turn Lex grammars into C programs or Ratfor programs;
8383 default @samp{lex}.
8384 @pindex lex
8386 @item PC
8387 @vindex PC
8388 Program for compiling Pascal programs; default @samp{pc}.
8389 @pindex pc
8391 @item YACC
8392 @vindex YACC
8393 Program to use to turn Yacc grammars into C programs; default @samp{yacc}.
8394 @pindex yacc
8396 @item YACCR
8397 @vindex YACCR
8398 Program to use to turn Yacc grammars into Ratfor
8399 programs; default @samp{yacc -r}.
8401 @item MAKEINFO
8402 @vindex MAKEINFO
8403 Program to convert a Texinfo source file into an Info file; default
8404 @samp{makeinfo}.
8405 @pindex makeinfo
8407 @item TEX
8408 @vindex TEX
8409 Program to make @TeX{} @sc{dvi} files from @TeX{} source;
8410 default @samp{tex}.
8411 @pindex tex
8413 @item TEXI2DVI
8414 @vindex TEXI2DVI
8415 Program to make @TeX{} @sc{dvi} files from Texinfo source;
8416 default @samp{texi2dvi}.
8417 @pindex texi2dvi
8419 @item WEAVE
8420 @vindex WEAVE
8421 Program to translate Web into @TeX{}; default @samp{weave}.
8422 @pindex weave
8424 @item CWEAVE
8425 @vindex CWEAVE
8426 Program to translate C Web into @TeX{}; default @samp{cweave}.
8427 @pindex cweave
8429 @item TANGLE
8430 @vindex TANGLE
8431 Program to translate Web into Pascal; default @samp{tangle}.
8432 @pindex tangle
8434 @item CTANGLE
8435 @vindex CTANGLE
8436 Program to translate C Web into C; default @samp{ctangle}.
8437 @pindex ctangle
8439 @item RM
8440 @vindex RM
8441 Command to remove a file; default @samp{rm -f}.
8442 @pindex rm
8443 @end table
8445 Here is a table of variables whose values are additional arguments for the
8446 programs above.  The default values for all of these is the empty
8447 string, unless otherwise noted.
8449 @table @code
8450 @item ARFLAGS
8451 @vindex ARFLAGS
8452 Flags to give the archive-maintaining program; default @samp{rv}.
8454 @item ASFLAGS
8455 @vindex ASFLAGS
8456 Extra flags to give to the assembler (when explicitly
8457 invoked on a @samp{.s} or @samp{.S} file).
8459 @item CFLAGS
8460 @vindex CFLAGS
8461 Extra flags to give to the C compiler.
8463 @item CXXFLAGS
8464 @vindex CXXFLAGS
8465 Extra flags to give to the C++ compiler.
8467 @item COFLAGS
8468 @vindex COFLAGS
8469 Extra flags to give to the RCS @code{co} program.
8471 @item CPPFLAGS
8472 @vindex CPPFLAGS
8473 Extra flags to give to the C preprocessor and programs
8474 that use it (the C and Fortran compilers).
8476 @item FFLAGS
8477 @vindex FFLAGS
8478 Extra flags to give to the Fortran compiler.
8480 @item GFLAGS
8481 @vindex GFLAGS
8482 Extra flags to give to the SCCS @code{get} program.
8484 @item LDFLAGS
8485 @vindex LDFLAGS
8486 Extra flags to give to compilers when they are
8487 supposed to invoke the linker, @samp{ld}.
8489 @item LFLAGS
8490 @vindex LFLAGS
8491 Extra flags to give to Lex.
8493 @item PFLAGS
8494 @vindex PFLAGS
8495 Extra flags to give to the Pascal compiler.
8497 @item RFLAGS
8498 @vindex RFLAGS
8499 Extra flags to give to the Fortran compiler for Ratfor programs.
8501 @item YFLAGS
8502 @vindex YFLAGS
8503 Extra flags to give to Yacc.
8504 @end table
8506 @node Chained Rules, Pattern Rules, Implicit Variables, Implicit Rules
8507 @section Chains of Implicit Rules
8509 @cindex chains of rules
8510 @cindex rule, implicit, chains of
8511 Sometimes a file can be made by a sequence of implicit rules.  For example,
8512 a file @file{@var{n}.o} could be made from @file{@var{n}.y} by running
8513 first Yacc and then @code{cc}.  Such a sequence is called a @dfn{chain}.
8515 If the file @file{@var{n}.c} exists, or is mentioned in the makefile, no
8516 special searching is required: @code{make} finds that the object file can
8517 be made by C compilation from @file{@var{n}.c}; later on, when considering
8518 how to make @file{@var{n}.c}, the rule for running Yacc is
8519 used.  Ultimately both @file{@var{n}.c} and @file{@var{n}.o} are
8520 updated.@refill
8522 @cindex intermediate files
8523 @cindex files, intermediate
8524 However, even if @file{@var{n}.c} does not exist and is not mentioned,
8525 @code{make} knows how to envision it as the missing link between
8526 @file{@var{n}.o} and @file{@var{n}.y}!  In this case, @file{@var{n}.c} is
8527 called an @dfn{intermediate file}.  Once @code{make} has decided to use the
8528 intermediate file, it is entered in the data base as if it had been
8529 mentioned in the makefile, along with the implicit rule that says how to
8530 create it.@refill
8532 Intermediate files are remade using their rules just like all other
8533 files.  But intermediate files are treated differently in two ways.
8535 The first difference is what happens if the intermediate file does not
8536 exist.  If an ordinary file @var{b} does not exist, and @code{make}
8537 considers a target that depends on @var{b}, it invariably creates
8538 @var{b} and then updates the target from @var{b}.  But if @var{b} is an
8539 intermediate file, then @code{make} can leave well enough alone.  It
8540 won't bother updating @var{b}, or the ultimate target, unless some
8541 prerequisite of @var{b} is newer than that target or there is some other
8542 reason to update that target.
8544 The second difference is that if @code{make} @emph{does} create @var{b}
8545 in order to update something else, it deletes @var{b} later on after it
8546 is no longer needed.  Therefore, an intermediate file which did not
8547 exist before @code{make} also does not exist after @code{make}.
8548 @code{make} reports the deletion to you by printing a @samp{rm -f}
8549 command showing which file it is deleting.
8551 Ordinarily, a file cannot be intermediate if it is mentioned in the
8552 makefile as a target or prerequisite.  However, you can explicitly mark a
8553 file as intermediate by listing it as a prerequisite of the special target
8554 @code{.INTERMEDIATE}.  This takes effect even if the file is mentioned
8555 explicitly in some other way.
8557 @cindex intermediate files, preserving
8558 @cindex preserving intermediate files
8559 @cindex secondary files
8560 You can prevent automatic deletion of an intermediate file by marking it
8561 as a @dfn{secondary} file.  To do this, list it as a prerequisite of the
8562 special target @code{.SECONDARY}.  When a file is secondary, @code{make}
8563 will not create the file merely because it does not already exist, but
8564 @code{make} does not automatically delete the file.  Marking a file as
8565 secondary also marks it as intermediate.
8567 You can list the target pattern of an implicit rule (such as @samp{%.o})
8568 as a prerequisite of the special target @code{.PRECIOUS} to preserve
8569 intermediate files made by implicit rules whose target patterns match
8570 that file's name; see @ref{Interrupts}.@refill
8571 @cindex preserving with @code{.PRECIOUS}
8572 @cindex @code{.PRECIOUS} intermediate files
8574 A chain can involve more than two implicit rules.  For example, it is
8575 possible to make a file @file{foo} from @file{RCS/foo.y,v} by running RCS,
8576 Yacc and @code{cc}.  Then both @file{foo.y} and @file{foo.c} are
8577 intermediate files that are deleted at the end.@refill
8579 No single implicit rule can appear more than once in a chain.  This means
8580 that @code{make} will not even consider such a ridiculous thing as making
8581 @file{foo} from @file{foo.o.o} by running the linker twice.  This
8582 constraint has the added benefit of preventing any infinite loop in the
8583 search for an implicit rule chain.
8585 There are some special implicit rules to optimize certain cases that would
8586 otherwise be handled by rule chains.  For example, making @file{foo} from
8587 @file{foo.c} could be handled by compiling and linking with separate
8588 chained rules, using @file{foo.o} as an intermediate file.  But what
8589 actually happens is that a special rule for this case does the compilation
8590 and linking with a single @code{cc} command.  The optimized rule is used in
8591 preference to the step-by-step chain because it comes earlier in the
8592 ordering of rules.
8594 @node Pattern Rules, Last Resort, Chained Rules, Implicit Rules
8595 @section Defining and Redefining Pattern Rules
8597 You define an implicit rule by writing a @dfn{pattern rule}.  A pattern
8598 rule looks like an ordinary rule, except that its target contains the
8599 character @samp{%} (exactly one of them).  The target is considered a
8600 pattern for matching file names; the @samp{%} can match any nonempty
8601 substring, while other characters match only themselves.  The prerequisites
8602 likewise use @samp{%} to show how their names relate to the target name.
8604 Thus, a pattern rule @samp{%.o : %.c} says how to make any file
8605 @file{@var{stem}.o} from another file @file{@var{stem}.c}.@refill
8607 Note that expansion using @samp{%} in pattern rules occurs
8608 @strong{after} any variable or function expansions, which take place
8609 when the makefile is read.  @xref{Using Variables, , How to Use
8610 Variables}, and @ref{Functions, ,Functions for Transforming Text}.
8612 @menu
8613 * Pattern Intro::               An introduction to pattern rules.
8614 * Pattern Examples::            Examples of pattern rules.
8615 * Automatic Variables::         How to use automatic variables in the
8616                                   commands of implicit rules.
8617 * Pattern Match::               How patterns match.
8618 * Match-Anything Rules::        Precautions you should take prior to
8619                                   defining rules that can match any
8620                                   target file whatever.
8621 * Canceling Rules::             How to override or cancel built-in rules.
8622 @end menu
8624 @node Pattern Intro, Pattern Examples, Pattern Rules, Pattern Rules
8625 @subsection Introduction to Pattern Rules
8626 @cindex pattern rule
8627 @cindex rule, pattern
8629 A pattern rule contains the character @samp{%} (exactly one of them)
8630 in the target; otherwise, it looks exactly like an ordinary rule.  The
8631 target is a pattern for matching file names; the @samp{%} matches any
8632 nonempty substring, while other characters match only themselves.
8633 @cindex target pattern, implicit
8634 @cindex @code{%}, in pattern rules
8636 For example, @samp{%.c} as a pattern matches any file name that ends in
8637 @samp{.c}.  @samp{s.%.c} as a pattern matches any file name that starts
8638 with @samp{s.}, ends in @samp{.c} and is at least five characters long.
8639 (There must be at least one character to match the @samp{%}.)  The substring
8640 that the @samp{%} matches is called the @dfn{stem}.@refill
8642 @samp{%} in a prerequisite of a pattern rule stands for the same stem
8643 that was matched by the @samp{%} in the target.  In order for
8644 the pattern rule to apply, its target pattern must match the file name
8645 under consideration, and its prerequisite patterns must name files that
8646 exist or can be made.  These files become prerequisites of the target.
8647 @cindex prerequisite pattern, implicit
8649 Thus, a rule of the form
8651 @example
8652 %.o : %.c ; @var{command}@dots{}
8653 @end example
8655 @noindent
8656 specifies how to make a file @file{@var{n}.o}, with another file
8657 @file{@var{n}.c} as its prerequisite, provided that @file{@var{n}.c}
8658 exists or can be made.
8660 There may also be prerequisites that do not use @samp{%}; such a prerequisite
8661 attaches to every file made by this pattern rule.  These unvarying
8662 prerequisites are useful occasionally.
8664 A pattern rule need not have any prerequisites that contain @samp{%}, or
8665 in fact any prerequisites at all.  Such a rule is effectively a general
8666 wildcard.  It provides a way to make any file that matches the target
8667 pattern.  @xref{Last Resort}.
8669 @c !!! The end of of this paragraph should be rewritten.  --bob
8670 Pattern rules may have more than one target.  Unlike normal rules, this
8671 does not act as many different rules with the same prerequisites and
8672 commands.  If a pattern rule has multiple targets, @code{make} knows that
8673 the rule's commands are responsible for making all of the targets.  The
8674 commands are executed only once to make all the targets.  When searching
8675 for a pattern rule to match a target, the target patterns of a rule other
8676 than the one that matches the target in need of a rule are incidental:
8677 @code{make} worries only about giving commands and prerequisites to the file
8678 presently in question.  However, when this file's commands are run, the
8679 other targets are marked as having been updated themselves.
8680 @cindex multiple targets, in pattern rule
8681 @cindex target, multiple in pattern rule
8683 The order in which pattern rules appear in the makefile is important
8684 since this is the order in which they are considered.
8685 Of equally applicable
8686 rules, only the first one found is used.  The rules you write take precedence
8687 over those that are built in.  Note however, that a rule whose
8688 prerequisites actually exist or are mentioned always takes priority over a
8689 rule with prerequisites that must be made by chaining other implicit rules.
8690 @cindex pattern rules, order of
8691 @cindex order of pattern rules
8693 @node Pattern Examples, Automatic Variables, Pattern Intro, Pattern Rules
8694 @subsection Pattern Rule Examples
8696 Here are some examples of pattern rules actually predefined in
8697 @code{make}.  First, the rule that compiles @samp{.c} files into @samp{.o}
8698 files:@refill
8700 @example
8701 %.o : %.c
8702         $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@@
8703 @end example
8705 @noindent
8706 defines a rule that can make any file @file{@var{x}.o} from
8707 @file{@var{x}.c}.  The command uses the automatic variables @samp{$@@} and
8708 @samp{$<} to substitute the names of the target file and the source file
8709 in each case where the rule applies (@pxref{Automatic Variables}).@refill
8711 Here is a second built-in rule:
8713 @example
8714 % :: RCS/%,v
8715         $(CO) $(COFLAGS) $<
8716 @end example
8718 @noindent
8719 defines a rule that can make any file @file{@var{x}} whatsoever from a
8720 corresponding file @file{@var{x},v} in the subdirectory @file{RCS}.  Since
8721 the target is @samp{%}, this rule will apply to any file whatever, provided
8722 the appropriate prerequisite file exists.  The double colon makes the rule
8723 @dfn{terminal}, which means that its prerequisite may not be an intermediate
8724 file (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}).@refill
8726 @need 500
8727 This pattern rule has two targets:
8729 @example
8730 @group
8731 %.tab.c %.tab.h: %.y
8732         bison -d $<
8733 @end group
8734 @end example
8736 @noindent
8737 @c The following paragraph is rewritten to avoid overfull hboxes
8738 This tells @code{make} that the command @samp{bison -d @var{x}.y} will
8739 make both @file{@var{x}.tab.c} and @file{@var{x}.tab.h}.  If the file
8740 @file{foo} depends on the files @file{parse.tab.o} and @file{scan.o}
8741 and the file @file{scan.o} depends on the file @file{parse.tab.h},
8742 when @file{parse.y} is changed, the command @samp{bison -d parse.y}
8743 will be executed only once, and the prerequisites of both
8744 @file{parse.tab.o} and @file{scan.o} will be satisfied.  (Presumably
8745 the file @file{parse.tab.o} will be recompiled from @file{parse.tab.c}
8746 and the file @file{scan.o} from @file{scan.c}, while @file{foo} is
8747 linked from @file{parse.tab.o}, @file{scan.o}, and its other
8748 prerequisites, and it will execute happily ever after.)@refill
8750 @node Automatic Variables, Pattern Match, Pattern Examples, Pattern Rules
8751 @subsection Automatic Variables
8752 @cindex automatic variables
8753 @cindex variables, automatic
8754 @cindex variables, and implicit rule
8756 Suppose you are writing a pattern rule to compile a @samp{.c} file into a
8757 @samp{.o} file: how do you write the @samp{cc} command so that it operates
8758 on the right source file name?  You cannot write the name in the command,
8759 because the name is different each time the implicit rule is applied.
8761 What you do is use a special feature of @code{make}, the @dfn{automatic
8762 variables}.  These variables have values computed afresh for each rule that
8763 is executed, based on the target and prerequisites of the rule.  In this
8764 example, you would use @samp{$@@} for the object file name and @samp{$<}
8765 for the source file name.
8767 @cindex automatic variables in prerequisites
8768 @cindex prerequisites, and automatic variables
8769 It's very important that you recognize the limited scope in which
8770 automatic variable values are available: they only have values within
8771 the command script.  In particular, you cannot use them anywhere
8772 within the target list of a rule; they have no value there and will
8773 expand to the empty string.  Also, they cannot be accessed directly
8774 within the prerequisite list of a rule.  A common mistake is
8775 attempting to use @code{$@@} within the prerequisites list; this will
8776 not work.  However, there is a special feature of GNU @code{make},
8777 secondary expansion (@pxref{Secondary Expansion}), which will allow
8778 automatic variable values to be used in prerequisite lists.
8780 Here is a table of automatic variables:
8782 @table @code
8783 @vindex $@@
8784 @vindex @@ @r{(automatic variable)}
8785 @item $@@
8786 The file name of the target of the rule.  If the target is an archive
8787 member, then @samp{$@@} is the name of the archive file.  In a pattern
8788 rule that has multiple targets (@pxref{Pattern Intro, ,Introduction to
8789 Pattern Rules}), @samp{$@@} is the name of whichever target caused the
8790 rule's commands to be run.
8792 @vindex $%
8793 @vindex % @r{(automatic variable)}
8794 @item $%
8795 The target member name, when the target is an archive member.
8796 @xref{Archives}.  For example, if the target is @file{foo.a(bar.o)} then
8797 @samp{$%} is @file{bar.o} and @samp{$@@} is @file{foo.a}.  @samp{$%} is
8798 empty when the target is not an archive member.
8800 @vindex $<
8801 @vindex < @r{(automatic variable)}
8802 @item $<
8803 The name of the first prerequisite.  If the target got its commands from
8804 an implicit rule, this will be the first prerequisite added by the
8805 implicit rule (@pxref{Implicit Rules}).
8807 @vindex $?
8808 @vindex ? @r{(automatic variable)}
8809 @item $?
8810 The names of all the prerequisites that are newer than the target, with
8811 spaces between them.  For prerequisites which are archive members, only
8812 the member named is used (@pxref{Archives}).
8813 @cindex prerequisites, list of changed
8814 @cindex list of changed prerequisites
8816 @vindex $^
8817 @vindex ^ @r{(automatic variable)}
8818 @item $^
8819 The names of all the prerequisites, with spaces between them.  For
8820 prerequisites which are archive members, only the member named is used
8821 (@pxref{Archives}).  A target has only one prerequisite on each other file
8822 it depends on, no matter how many times each file is listed as a
8823 prerequisite.  So if you list a prerequisite more than once for a target,
8824 the value of @code{$^} contains just one copy of the name.  This list
8825 does @strong{not} contain any of the order-only prerequisites; for those
8826 see the @samp{$|} variable, below.
8827 @cindex prerequisites, list of all
8828 @cindex list of all prerequisites
8830 @vindex $+
8831 @vindex + @r{(automatic variable)}
8832 @item $+
8833 This is like @samp{$^}, but prerequisites listed more than once are
8834 duplicated in the order they were listed in the makefile.  This is
8835 primarily useful for use in linking commands where it is meaningful to
8836 repeat library file names in a particular order.
8838 @vindex $|
8839 @vindex | @r{(automatic variable)}
8840 @item $|
8841 The names of all the order-only prerequisites, with spaces between
8842 them.
8844 @vindex $*
8845 @vindex * @r{(automatic variable)}
8846 @item $*
8847 The stem with which an implicit rule matches (@pxref{Pattern Match, ,How
8848 Patterns Match}).  If the target is @file{dir/a.foo.b} and the target
8849 pattern is @file{a.%.b} then the stem is @file{dir/foo}.  The stem is
8850 useful for constructing names of related files.@refill
8851 @cindex stem, variable for
8853 In a static pattern rule, the stem is part of the file name that matched
8854 the @samp{%} in the target pattern.
8856 In an explicit rule, there is no stem; so @samp{$*} cannot be determined
8857 in that way.  Instead, if the target name ends with a recognized suffix
8858 (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}), @samp{$*} is set to
8859 the target name minus the suffix.  For example, if the target name is
8860 @samp{foo.c}, then @samp{$*} is set to @samp{foo}, since @samp{.c} is a
8861 suffix.  GNU @code{make} does this bizarre thing only for compatibility
8862 with other implementations of @code{make}.  You should generally avoid
8863 using @samp{$*} except in implicit rules or static pattern rules.@refill
8865 If the target name in an explicit rule does not end with a recognized
8866 suffix, @samp{$*} is set to the empty string for that rule.
8867 @end table
8869 @samp{$?} is useful even in explicit rules when you wish to operate on only
8870 the prerequisites that have changed.  For example, suppose that an archive
8871 named @file{lib} is supposed to contain copies of several object files.
8872 This rule copies just the changed object files into the archive:
8874 @example
8875 @group
8876 lib: foo.o bar.o lose.o win.o
8877         ar r lib $?
8878 @end group
8879 @end example
8881 Of the variables listed above, four have values that are single file
8882 names, and three have values that are lists of file names.  These seven
8883 have variants that get just the file's directory name or just the file
8884 name within the directory.  The variant variables' names are formed by
8885 appending @samp{D} or @samp{F}, respectively.  These variants are
8886 semi-obsolete in GNU @code{make} since the functions @code{dir} and
8887 @code{notdir} can be used to get a similar effect (@pxref{File Name
8888 Functions, , Functions for File Names}).  Note, however, that the
8889 @samp{D} variants all omit the trailing slash which always appears in
8890 the output of the @code{dir} function.  Here is a table of the variants:
8892 @table @samp
8893 @vindex $(@@D)
8894 @vindex @@D @r{(automatic variable)}
8895 @item $(@@D)
8896 The directory part of the file name of the target, with the trailing
8897 slash removed.  If the value of @samp{$@@} is @file{dir/foo.o} then
8898 @samp{$(@@D)} is @file{dir}.  This value is @file{.} if @samp{$@@} does
8899 not contain a slash.
8901 @vindex $(@@F)
8902 @vindex @@F @r{(automatic variable)}
8903 @item $(@@F)
8904 The file-within-directory part of the file name of the target.  If the
8905 value of @samp{$@@} is @file{dir/foo.o} then @samp{$(@@F)} is
8906 @file{foo.o}.  @samp{$(@@F)} is equivalent to @samp{$(notdir $@@)}.
8908 @vindex $(*D)
8909 @vindex *D @r{(automatic variable)}
8910 @item $(*D)
8911 @vindex $(*F)
8912 @vindex *F @r{(automatic variable)}
8913 @itemx $(*F)
8914 The directory part and the file-within-directory
8915 part of the stem; @file{dir} and @file{foo} in this example.
8917 @vindex $(%D)
8918 @vindex %D @r{(automatic variable)}
8919 @item $(%D)
8920 @vindex $(%F)
8921 @vindex %F @r{(automatic variable)}
8922 @itemx $(%F)
8923 The directory part and the file-within-directory part of the target
8924 archive member name.  This makes sense only for archive member targets
8925 of the form @file{@var{archive}(@var{member})} and is useful only when
8926 @var{member} may contain a directory name.  (@xref{Archive Members,
8927 ,Archive Members as Targets}.)
8929 @vindex $(<D)
8930 @vindex <D @r{(automatic variable)}
8931 @item $(<D)
8932 @vindex $(<F)
8933 @vindex <F @r{(automatic variable)}
8934 @itemx $(<F)
8935 The directory part and the file-within-directory
8936 part of the first prerequisite.
8938 @vindex $(^D)
8939 @vindex ^D @r{(automatic variable)}
8940 @item $(^D)
8941 @vindex $(^F)
8942 @vindex ^F @r{(automatic variable)}
8943 @itemx $(^F)
8944 Lists of the directory parts and the file-within-directory
8945 parts of all prerequisites.
8947 @vindex $(+D)
8948 @vindex +D @r{(automatic variable)}
8949 @item $(+D)
8950 @vindex $(+F)
8951 @vindex +F @r{(automatic variable)}
8952 @itemx $(+F)
8953 Lists of the directory parts and the file-within-directory
8954 parts of all prerequisites, including multiple instances of duplicated
8955 prerequisites.
8957 @vindex $(?D)
8958 @vindex ?D @r{(automatic variable)}
8959 @item $(?D)
8960 @vindex $(?F)
8961 @vindex ?F @r{(automatic variable)}
8962 @itemx $(?F)
8963 Lists of the directory parts and the file-within-directory parts of
8964 all prerequisites that are newer than the target.
8965 @end table
8967 Note that we use a special stylistic convention when we talk about these
8968 automatic variables; we write ``the value of @samp{$<}'', rather than
8969 @w{``the variable @code{<}''} as we would write for ordinary variables
8970 such as @code{objects} and @code{CFLAGS}.  We think this convention
8971 looks more natural in this special case.  Please do not assume it has a
8972 deep significance; @samp{$<} refers to the variable named @code{<} just
8973 as @samp{$(CFLAGS)} refers to the variable named @code{CFLAGS}.
8974 You could just as well use @samp{$(<)} in place of @samp{$<}.
8976 @node Pattern Match, Match-Anything Rules, Automatic Variables, Pattern Rules
8977 @subsection How Patterns Match
8979 @cindex stem
8980 A target pattern is composed of a @samp{%} between a prefix and a suffix,
8981 either or both of which may be empty.  The pattern matches a file name only
8982 if the file name starts with the prefix and ends with the suffix, without
8983 overlap.  The text between the prefix and the suffix is called the
8984 @dfn{stem}.  Thus, when the pattern @samp{%.o} matches the file name
8985 @file{test.o}, the stem is @samp{test}.  The pattern rule prerequisites are
8986 turned into actual file names by substituting the stem for the character
8987 @samp{%}.  Thus, if in the same example one of the prerequisites is written
8988 as @samp{%.c}, it expands to @samp{test.c}.@refill
8990 When the target pattern does not contain a slash (and it usually does
8991 not), directory names in the file names are removed from the file name
8992 before it is compared with the target prefix and suffix.  After the
8993 comparison of the file name to the target pattern, the directory
8994 names, along with the slash that ends them, are added on to the
8995 prerequisite file names generated from the pattern rule's prerequisite
8996 patterns and the file name. The directories are ignored only for the
8997 purpose of finding an implicit rule to use, not in the application of
8998 that rule.  Thus, @samp{e%t} matches the file name @file{src/eat},
8999 with @samp{src/a} as the stem.  When prerequisites are turned into file
9000 names, the directories from the stem are added at the front, while the
9001 rest of the stem is substituted for the @samp{%}.  The stem
9002 @samp{src/a} with a prerequisite pattern @samp{c%r} gives the file name
9003 @file{src/car}.@refill
9005 @node Match-Anything Rules, Canceling Rules, Pattern Match, Pattern Rules
9006 @subsection Match-Anything Pattern Rules
9008 @cindex match-anything rule
9009 @cindex terminal rule
9010 When a pattern rule's target is just @samp{%}, it matches any file name
9011 whatever.  We call these rules @dfn{match-anything} rules.  They are very
9012 useful, but it can take a lot of time for @code{make} to think about them,
9013 because it must consider every such rule for each file name listed either
9014 as a target or as a prerequisite.
9016 Suppose the makefile mentions @file{foo.c}.  For this target, @code{make}
9017 would have to consider making it by linking an object file @file{foo.c.o},
9018 or by C compilation-and-linking in one step from @file{foo.c.c}, or by
9019 Pascal compilation-and-linking from @file{foo.c.p}, and many other
9020 possibilities.
9022 We know these possibilities are ridiculous since @file{foo.c} is a C source
9023 file, not an executable.  If @code{make} did consider these possibilities,
9024 it would ultimately reject them, because files such as @file{foo.c.o} and
9025 @file{foo.c.p} would not exist.  But these possibilities are so
9026 numerous that @code{make} would run very slowly if it had to consider
9027 them.@refill
9029 To gain speed, we have put various constraints on the way @code{make}
9030 considers match-anything rules.  There are two different constraints that
9031 can be applied, and each time you define a match-anything rule you must
9032 choose one or the other for that rule.
9034 One choice is to mark the match-anything rule as @dfn{terminal} by defining
9035 it with a double colon.  When a rule is terminal, it does not apply unless
9036 its prerequisites actually exist.  Prerequisites that could be made with
9037 other implicit rules are not good enough.  In other words, no further
9038 chaining is allowed beyond a terminal rule.
9040 For example, the built-in implicit rules for extracting sources from RCS
9041 and SCCS files are terminal; as a result, if the file @file{foo.c,v} does
9042 not exist, @code{make} will not even consider trying to make it as an
9043 intermediate file from @file{foo.c,v.o} or from @file{RCS/SCCS/s.foo.c,v}.
9044 RCS and SCCS files are generally ultimate source files, which should not be
9045 remade from any other files; therefore, @code{make} can save time by not
9046 looking for ways to remake them.@refill
9048 If you do not mark the match-anything rule as terminal, then it is
9049 nonterminal.  A nonterminal match-anything rule cannot apply to a file name
9050 that indicates a specific type of data.  A file name indicates a specific
9051 type of data if some non-match-anything implicit rule target matches it.
9053 For example, the file name @file{foo.c} matches the target for the pattern
9054 rule @samp{%.c : %.y} (the rule to run Yacc).  Regardless of whether this
9055 rule is actually applicable (which happens only if there is a file
9056 @file{foo.y}), the fact that its target matches is enough to prevent
9057 consideration of any nonterminal match-anything rules for the file
9058 @file{foo.c}.  Thus, @code{make} will not even consider trying to make
9059 @file{foo.c} as an executable file from @file{foo.c.o}, @file{foo.c.c},
9060 @file{foo.c.p}, etc.@refill
9062 The motivation for this constraint is that nonterminal match-anything
9063 rules are used for making files containing specific types of data (such as
9064 executable files) and a file name with a recognized suffix indicates some
9065 other specific type of data (such as a C source file).
9067 Special built-in dummy pattern rules are provided solely to recognize
9068 certain file names so that nonterminal match-anything rules will not be
9069 considered.  These dummy rules have no prerequisites and no commands, and
9070 they are ignored for all other purposes.  For example, the built-in
9071 implicit rule
9073 @example
9074 %.p :
9075 @end example
9077 @noindent
9078 exists to make sure that Pascal source files such as @file{foo.p} match a
9079 specific target pattern and thereby prevent time from being wasted looking
9080 for @file{foo.p.o} or @file{foo.p.c}.
9082 Dummy pattern rules such as the one for @samp{%.p} are made for every
9083 suffix listed as valid for use in suffix rules (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}).
9085 @node Canceling Rules,  , Match-Anything Rules, Pattern Rules
9086 @subsection Canceling Implicit Rules
9088 You can override a built-in implicit rule (or one you have defined
9089 yourself) by defining a new pattern rule with the same target and
9090 prerequisites, but different commands.  When the new rule is defined, the
9091 built-in one is replaced.  The new rule's position in the sequence of
9092 implicit rules is determined by where you write the new rule.
9094 You can cancel a built-in implicit rule by defining a pattern rule with the
9095 same target and prerequisites, but no commands.  For example, the following
9096 would cancel the rule that runs the assembler:
9098 @example
9099 %.o : %.s
9100 @end example
9102 @node Last Resort, Suffix Rules, Pattern Rules, Implicit Rules
9103 @section Defining Last-Resort Default Rules
9104 @cindex last-resort default rules
9105 @cindex default rules, last-resort
9107 You can define a last-resort implicit rule by writing a terminal
9108 match-anything pattern rule with no prerequisites (@pxref{Match-Anything
9109 Rules}).  This is just like any other pattern rule; the only thing
9110 special about it is that it will match any target.  So such a rule's
9111 commands are used for all targets and prerequisites that have no commands
9112 of their own and for which no other implicit rule applies.
9114 For example, when testing a makefile, you might not care if the source
9115 files contain real data, only that they exist.  Then you might do this:
9117 @example
9119         touch $@@
9120 @end example
9122 @noindent
9123 to cause all the source files needed (as prerequisites) to be created
9124 automatically.
9126 @findex .DEFAULT
9127 You can instead define commands to be used for targets for which there
9128 are no rules at all, even ones which don't specify commands.  You do
9129 this by writing a rule for the target @code{.DEFAULT}.  Such a rule's
9130 commands are used for all prerequisites which do not appear as targets in
9131 any explicit rule, and for which no implicit rule applies.  Naturally,
9132 there is no @code{.DEFAULT} rule unless you write one.
9134 If you use @code{.DEFAULT} with no commands or prerequisites:
9136 @example
9137 .DEFAULT:
9138 @end example
9140 @noindent
9141 the commands previously stored for @code{.DEFAULT} are cleared.
9142 Then @code{make} acts as if you had never defined @code{.DEFAULT} at all.
9144 If you do not want a target to get the commands from a match-anything
9145 pattern rule or @code{.DEFAULT}, but you also do not want any commands
9146 to be run for the target, you can give it empty commands (@pxref{Empty
9147 Commands, ,Defining Empty Commands}).@refill
9149 You can use a last-resort rule to override part of another makefile.
9150 @xref{Overriding Makefiles, , Overriding Part of Another Makefile}.
9152 @node Suffix Rules, Implicit Rule Search, Last Resort, Implicit Rules
9153 @section Old-Fashioned Suffix Rules
9154 @cindex old-fashioned suffix rules
9155 @cindex suffix rule
9157 @dfn{Suffix rules} are the old-fashioned way of defining implicit rules for
9158 @code{make}.  Suffix rules are obsolete because pattern rules are more
9159 general and clearer.  They are supported in GNU @code{make} for
9160 compatibility with old makefiles.  They come in two kinds:
9161 @dfn{double-suffix} and @dfn{single-suffix}.@refill
9163 A double-suffix rule is defined by a pair of suffixes: the target suffix
9164 and the source suffix.  It matches any file whose name ends with the
9165 target suffix.  The corresponding implicit prerequisite is made by
9166 replacing the target suffix with the source suffix in the file name.  A
9167 two-suffix rule whose target and source suffixes are @samp{.o} and
9168 @samp{.c} is equivalent to the pattern rule @samp{%.o : %.c}.
9170 A single-suffix rule is defined by a single suffix, which is the source
9171 suffix.  It matches any file name, and the corresponding implicit
9172 prerequisite name is made by appending the source suffix.  A single-suffix
9173 rule whose source suffix is @samp{.c} is equivalent to the pattern rule
9174 @samp{% : %.c}.
9176 Suffix rule definitions are recognized by comparing each rule's target
9177 against a defined list of known suffixes.  When @code{make} sees a rule
9178 whose target is a known suffix, this rule is considered a single-suffix
9179 rule.  When @code{make} sees a rule whose target is two known suffixes
9180 concatenated, this rule is taken as a double-suffix rule.
9182 For example, @samp{.c} and @samp{.o} are both on the default list of
9183 known suffixes.  Therefore, if you define a rule whose target is
9184 @samp{.c.o}, @code{make} takes it to be a double-suffix rule with source
9185 suffix @samp{.c} and target suffix @samp{.o}.  Here is the old-fashioned
9186 way to define the rule for compiling a C source file:@refill
9188 @example
9189 .c.o:
9190         $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
9191 @end example
9193 Suffix rules cannot have any prerequisites of their own.  If they have any,
9194 they are treated as normal files with funny names, not as suffix rules.
9195 Thus, the rule:
9197 @example
9198 .c.o: foo.h
9199         $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
9200 @end example
9202 @noindent
9203 tells how to make the file @file{.c.o} from the prerequisite file
9204 @file{foo.h}, and is not at all like the pattern rule:
9206 @example
9207 %.o: %.c foo.h
9208         $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
9209 @end example
9211 @noindent
9212 which tells how to make @samp{.o} files from @samp{.c} files, and makes all
9213 @samp{.o} files using this pattern rule also depend on @file{foo.h}.
9215 Suffix rules with no commands are also meaningless.  They do not remove
9216 previous rules as do pattern rules with no commands (@pxref{Canceling
9217 Rules, , Canceling Implicit Rules}).  They simply enter the suffix or pair of suffixes concatenated as
9218 a target in the data base.@refill
9220 @findex .SUFFIXES
9221 The known suffixes are simply the names of the prerequisites of the special
9222 target @code{.SUFFIXES}.  You can add your own suffixes by writing a rule
9223 for @code{.SUFFIXES} that adds more prerequisites, as in:
9225 @example
9226 .SUFFIXES: .hack .win
9227 @end example
9229 @noindent
9230 which adds @samp{.hack} and @samp{.win} to the end of the list of suffixes.
9232 If you wish to eliminate the default known suffixes instead of just adding
9233 to them, write a rule for @code{.SUFFIXES} with no prerequisites.  By
9234 special dispensation, this eliminates all existing prerequisites of
9235 @code{.SUFFIXES}.  You can then write another rule to add the suffixes you
9236 want.  For example,
9238 @example
9239 @group
9240 .SUFFIXES:            # @r{Delete the default suffixes}
9241 .SUFFIXES: .c .o .h   # @r{Define our suffix list}
9242 @end group
9243 @end example
9245 The @samp{-r} or @samp{--no-builtin-rules} flag causes the default
9246 list of suffixes to be empty.
9248 @vindex SUFFIXES
9249 The variable @code{SUFFIXES} is defined to the default list of suffixes
9250 before @code{make} reads any makefiles.  You can change the list of suffixes
9251 with a rule for the special target @code{.SUFFIXES}, but that does not alter
9252 this variable.
9254 @node Implicit Rule Search,  , Suffix Rules, Implicit Rules
9255 @section Implicit Rule Search Algorithm
9256 @cindex implicit rule, search algorithm
9257 @cindex search algorithm, implicit rule
9259 Here is the procedure @code{make} uses for searching for an implicit rule
9260 for a target @var{t}.  This procedure is followed for each double-colon
9261 rule with no commands, for each target of ordinary rules none of which have
9262 commands, and for each prerequisite that is not the target of any rule.  It
9263 is also followed recursively for prerequisites that come from implicit
9264 rules, in the search for a chain of rules.
9266 Suffix rules are not mentioned in this algorithm because suffix rules are
9267 converted to equivalent pattern rules once the makefiles have been read in.
9269 For an archive member target of the form
9270 @samp{@var{archive}(@var{member})}, the following algorithm is run
9271 twice, first using the entire target name @var{t}, and second using
9272 @samp{(@var{member})} as the target @var{t} if the first run found no
9273 rule.@refill
9275 @enumerate
9276 @item
9277 Split @var{t} into a directory part, called @var{d}, and the rest,
9278 called @var{n}.  For example, if @var{t} is @samp{src/foo.o}, then
9279 @var{d} is @samp{src/} and @var{n} is @samp{foo.o}.@refill
9281 @item
9282 Make a list of all the pattern rules one of whose targets matches
9283 @var{t} or @var{n}.  If the target pattern contains a slash, it is
9284 matched against @var{t}; otherwise, against @var{n}.
9286 @item
9287 If any rule in that list is @emph{not} a match-anything rule, then
9288 remove all nonterminal match-anything rules from the list.
9290 @item
9291 Remove from the list all rules with no commands.
9293 @item
9294 For each pattern rule in the list:
9296 @enumerate a
9297 @item
9298 Find the stem @var{s}, which is the nonempty part of @var{t} or @var{n}
9299 matched by the @samp{%} in the target pattern.@refill
9301 @item
9302 Compute the prerequisite names by substituting @var{s} for @samp{%}; if
9303 the target pattern does not contain a slash, append @var{d} to
9304 the front of each prerequisite name.@refill
9306 @item
9307 Test whether all the prerequisites exist or ought to exist.  (If a
9308 file name is mentioned in the makefile as a target or as an explicit
9309 prerequisite, then we say it ought to exist.)
9311 If all prerequisites exist or ought to exist, or there are no prerequisites,
9312 then this rule applies.
9313 @end enumerate
9315 @item
9316 If no pattern rule has been found so far, try harder.
9317 For each pattern rule in the list:
9319 @enumerate a
9320 @item
9321 If the rule is terminal, ignore it and go on to the next rule.
9323 @item
9324 Compute the prerequisite names as before.
9326 @item
9327 Test whether all the prerequisites exist or ought to exist.
9329 @item
9330 For each prerequisite that does not exist, follow this algorithm
9331 recursively to see if the prerequisite can be made by an implicit
9332 rule.
9334 @item
9335 If all prerequisites exist, ought to exist, or can be
9336 made by implicit rules, then this rule applies.
9337 @end enumerate
9339 @item
9340 If no implicit rule applies, the rule for @code{.DEFAULT}, if any,
9341 applies.  In that case, give @var{t} the same commands that
9342 @code{.DEFAULT} has.  Otherwise, there are no commands for @var{t}.
9343 @end enumerate
9345 Once a rule that applies has been found, for each target pattern of the
9346 rule other than the one that matched @var{t} or @var{n}, the @samp{%} in
9347 the pattern is replaced with @var{s} and the resultant file name is stored
9348 until the commands to remake the target file @var{t} are executed.  After
9349 these commands are executed, each of these stored file names are entered
9350 into the data base and marked as having been updated and having the same
9351 update status as the file @var{t}.
9353 When the commands of a pattern rule are executed for @var{t}, the automatic
9354 variables are set corresponding to the target and prerequisites.
9355 @xref{Automatic Variables}.
9357 @node Archives, Features, Implicit Rules, Top
9358 @chapter Using @code{make} to Update Archive Files
9359 @cindex archive
9361 @dfn{Archive files} are files containing named subfiles called
9362 @dfn{members}; they are maintained with the program @code{ar} and their
9363 main use is as subroutine libraries for linking.
9365 @menu
9366 * Archive Members::             Archive members as targets.
9367 * Archive Update::              The implicit rule for archive member targets.
9368 * Archive Pitfalls::            Dangers to watch out for when using archives.
9369 * Archive Suffix Rules::        You can write a special kind of suffix rule
9370                                   for updating archives.
9371 @end menu
9373 @node Archive Members, Archive Update, Archives, Archives
9374 @section Archive Members as Targets
9375 @cindex archive member targets
9377 An individual member of an archive file can be used as a target or
9378 prerequisite in @code{make}.  You specify the member named @var{member} in
9379 archive file @var{archive} as follows:
9381 @example
9382 @var{archive}(@var{member})
9383 @end example
9385 @noindent
9386 This construct is available only in targets and prerequisites, not in
9387 commands!  Most programs that you might use in commands do not support this
9388 syntax and cannot act directly on archive members.  Only @code{ar} and
9389 other programs specifically designed to operate on archives can do so.
9390 Therefore, valid commands to update an archive member target probably must
9391 use @code{ar}.  For example, this rule says to create a member
9392 @file{hack.o} in archive @file{foolib} by copying the file @file{hack.o}:
9394 @example
9395 foolib(hack.o) : hack.o
9396         ar cr foolib hack.o
9397 @end example
9399 In fact, nearly all archive member targets are updated in just this way
9400 and there is an implicit rule to do it for you.  @strong{Please note:} The
9401 @samp{c} flag to @code{ar} is required if the archive file does not
9402 already exist.
9404 To specify several members in the same archive, you can write all the
9405 member names together between the parentheses.  For example:
9407 @example
9408 foolib(hack.o kludge.o)
9409 @end example
9411 @noindent
9412 is equivalent to:
9414 @example
9415 foolib(hack.o) foolib(kludge.o)
9416 @end example
9418 @cindex wildcard, in archive member
9419 You can also use shell-style wildcards in an archive member reference.
9420 @xref{Wildcards, ,Using Wildcard Characters in File Names}.  For
9421 example, @w{@samp{foolib(*.o)}} expands to all existing members of the
9422 @file{foolib} archive whose names end in @samp{.o}; perhaps
9423 @samp{@w{foolib(hack.o)} @w{foolib(kludge.o)}}.
9425 @node Archive Update, Archive Pitfalls, Archive Members, Archives
9426 @section Implicit Rule for Archive Member Targets
9428 Recall that a target that looks like @file{@var{a}(@var{m})} stands for the
9429 member named @var{m} in the archive file @var{a}.
9431 When @code{make} looks for an implicit rule for such a target, as a special
9432 feature it considers implicit rules that match @file{(@var{m})}, as well as
9433 those that match the actual target @file{@var{a}(@var{m})}.
9435 This causes one special rule whose target is @file{(%)} to match.  This
9436 rule updates the target @file{@var{a}(@var{m})} by copying the file @var{m}
9437 into the archive.  For example, it will update the archive member target
9438 @file{foo.a(bar.o)} by copying the @emph{file} @file{bar.o} into the
9439 archive @file{foo.a} as a @emph{member} named @file{bar.o}.
9441 When this rule is chained with others, the result is very powerful.
9442 Thus, @samp{make "foo.a(bar.o)"} (the quotes are needed to protect the
9443 @samp{(} and @samp{)} from being interpreted specially by the shell) in
9444 the presence of a file @file{bar.c} is enough to cause the following
9445 commands to be run, even without a makefile:
9447 @example
9448 cc -c bar.c -o bar.o
9449 ar r foo.a bar.o
9450 rm -f bar.o
9451 @end example
9453 @noindent
9454 Here @code{make} has envisioned the file @file{bar.o} as an intermediate
9455 file.  @xref{Chained Rules, ,Chains of Implicit Rules}.
9457 Implicit rules such as this one are written using the automatic variable
9458 @samp{$%}.  @xref{Automatic Variables}.
9460 An archive member name in an archive cannot contain a directory name, but
9461 it may be useful in a makefile to pretend that it does.  If you write an
9462 archive member target @file{foo.a(dir/file.o)}, @code{make} will perform
9463 automatic updating with this command:
9465 @example
9466 ar r foo.a dir/file.o
9467 @end example
9469 @noindent
9470 which has the effect of copying the file @file{dir/file.o} into a member
9471 named @file{file.o}.  In connection with such usage, the automatic variables
9472 @code{%D} and @code{%F} may be useful.
9474 @menu
9475 * Archive Symbols::             How to update archive symbol directories.
9476 @end menu
9478 @node Archive Symbols,  , Archive Update, Archive Update
9479 @subsection Updating Archive Symbol Directories
9480 @cindex @code{__.SYMDEF}
9481 @cindex updating archive symbol directories
9482 @cindex archive symbol directory updating
9483 @cindex symbol directories, updating archive
9484 @cindex directories, updating archive symbol
9486 An archive file that is used as a library usually contains a special member
9487 named @file{__.SYMDEF} that contains a directory of the external symbol
9488 names defined by all the other members.  After you update any other
9489 members, you need to update @file{__.SYMDEF} so that it will summarize the
9490 other members properly.  This is done by running the @code{ranlib} program:
9492 @example
9493 ranlib @var{archivefile}
9494 @end example
9496 Normally you would put this command in the rule for the archive file,
9497 and make all the members of the archive file prerequisites of that rule.
9498 For example,
9500 @example
9501 libfoo.a: libfoo.a(x.o) libfoo.a(y.o) @dots{}
9502         ranlib libfoo.a
9503 @end example
9505 @noindent
9506 The effect of this is to update archive members @file{x.o}, @file{y.o},
9507 etc., and then update the symbol directory member @file{__.SYMDEF} by
9508 running @code{ranlib}.  The rules for updating the members are not shown
9509 here; most likely you can omit them and use the implicit rule which copies
9510 files into the archive, as described in the preceding section.
9512 This is not necessary when using the GNU @code{ar} program, which
9513 updates the @file{__.SYMDEF} member automatically.
9515 @node Archive Pitfalls, Archive Suffix Rules, Archive Update, Archives
9516 @section Dangers When Using Archives
9517 @cindex archive, and parallel execution
9518 @cindex parallel execution, and archive update
9519 @cindex archive, and @code{-j}
9520 @cindex @code{-j}, and archive update
9522 It is important to be careful when using parallel execution (the
9523 @code{-j} switch; @pxref{Parallel, ,Parallel Execution}) and archives.
9524 If multiple @code{ar} commands run at the same time on the same archive
9525 file, they will not know about each other and can corrupt the file.
9527 Possibly a future version of @code{make} will provide a mechanism to
9528 circumvent this problem by serializing all commands that operate on the
9529 same archive file.  But for the time being, you must either write your
9530 makefiles to avoid this problem in some other way, or not use @code{-j}.
9532 @node Archive Suffix Rules,  , Archive Pitfalls, Archives
9533 @section Suffix Rules for Archive Files
9534 @cindex suffix rule, for archive
9535 @cindex archive, suffix rule for
9536 @cindex library archive, suffix rule for
9537 @cindex @code{.a} (archives)
9539 You can write a special kind of suffix rule for dealing with archive
9540 files.  @xref{Suffix Rules}, for a full explanation of suffix rules.
9541 Archive suffix rules are obsolete in GNU @code{make}, because pattern
9542 rules for archives are a more general mechanism (@pxref{Archive
9543 Update}).  But they are retained for compatibility with other
9544 @code{make}s.
9546 To write a suffix rule for archives, you simply write a suffix rule
9547 using the target suffix @samp{.a} (the usual suffix for archive files).
9548 For example, here is the old-fashioned suffix rule to update a library
9549 archive from C source files:
9551 @example
9552 @group
9553 .c.a:
9554         $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o
9555         $(AR) r $@@ $*.o
9556         $(RM) $*.o
9557 @end group
9558 @end example
9560 @noindent
9561 This works just as if you had written the pattern rule:
9563 @example
9564 @group
9565 (%.o): %.c
9566         $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o
9567         $(AR) r $@@ $*.o
9568         $(RM) $*.o
9569 @end group
9570 @end example
9572 In fact, this is just what @code{make} does when it sees a suffix rule
9573 with @samp{.a} as the target suffix.  Any double-suffix rule
9574 @w{@samp{.@var{x}.a}} is converted to a pattern rule with the target
9575 pattern @samp{(%.o)} and a prerequisite pattern of @samp{%.@var{x}}.
9577 Since you might want to use @samp{.a} as the suffix for some other kind
9578 of file, @code{make} also converts archive suffix rules to pattern rules
9579 in the normal way (@pxref{Suffix Rules}).  Thus a double-suffix rule
9580 @w{@samp{.@var{x}.a}} produces two pattern rules: @samp{@w{(%.o):}
9581 @w{%.@var{x}}} and @samp{@w{%.a}: @w{%.@var{x}}}.@refill
9583 @node Features, Missing, Archives, Top
9584 @chapter Features of GNU @code{make}
9585 @cindex features of GNU @code{make}
9586 @cindex portability
9587 @cindex compatibility
9589 Here is a summary of the features of GNU @code{make}, for comparison
9590 with and credit to other versions of @code{make}.  We consider the
9591 features of @code{make} in 4.2 BSD systems as a baseline.  If you are
9592 concerned with writing portable makefiles, you should not use the
9593 features of @code{make} listed here, nor the ones in @ref{Missing}.
9595 Many features come from the version of @code{make} in System V.
9597 @itemize @bullet
9598 @item
9599 The @code{VPATH} variable and its special meaning.
9600 @xref{Directory Search, , Searching Directories for Prerequisites}.
9601 This feature exists in System V @code{make}, but is undocumented.
9602 It is documented in 4.3 BSD @code{make} (which says it mimics System V's
9603 @code{VPATH} feature).@refill
9605 @item
9606 Included makefiles.  @xref{Include, ,Including Other Makefiles}.
9607 Allowing multiple files to be included with a single directive is a GNU
9608 extension.
9610 @item
9611 Variables are read from and communicated via the environment.
9612 @xref{Environment, ,Variables from the Environment}.
9614 @item
9615 Options passed through the variable @code{MAKEFLAGS} to recursive
9616 invocations of @code{make}.
9617 @xref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
9619 @item
9620 The automatic variable @code{$%} is set to the member name
9621 in an archive reference.  @xref{Automatic Variables}.
9623 @item
9624 The automatic variables @code{$@@}, @code{$*}, @code{$<}, @code{$%},
9625 and @code{$?} have corresponding forms like @code{$(@@F)} and
9626 @code{$(@@D)}.  We have generalized this to @code{$^} as an obvious
9627 extension.  @xref{Automatic Variables}.@refill
9629 @item
9630 Substitution variable references.
9631 @xref{Reference, ,Basics of Variable References}.
9633 @item
9634 The command-line options @samp{-b} and @samp{-m}, accepted and
9635 ignored.  In System V @code{make}, these options actually do something.
9637 @item
9638 Execution of recursive commands to run @code{make} via the variable
9639 @code{MAKE} even if @samp{-n}, @samp{-q} or @samp{-t} is specified.
9640 @xref{Recursion, ,Recursive Use of @code{make}}.
9642 @item
9643 Support for suffix @samp{.a} in suffix rules.  @xref{Archive Suffix
9644 Rules}.  This feature is obsolete in GNU @code{make}, because the
9645 general feature of rule chaining (@pxref{Chained Rules, ,Chains of
9646 Implicit Rules}) allows one pattern rule for installing members in an
9647 archive (@pxref{Archive Update}) to be sufficient.
9649 @item
9650 The arrangement of lines and backslash-newline combinations in
9651 commands is retained when the commands are printed, so they appear as
9652 they do in the makefile, except for the stripping of initial
9653 whitespace.
9654 @end itemize
9656 The following features were inspired by various other versions of
9657 @code{make}.  In some cases it is unclear exactly which versions inspired
9658 which others.
9660 @itemize @bullet
9661 @item
9662 Pattern rules using @samp{%}.
9663 This has been implemented in several versions of @code{make}.
9664 We're not sure who invented it first, but it's been spread around a bit.
9665 @xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.@refill
9667 @item
9668 Rule chaining and implicit intermediate files.
9669 This was implemented by Stu Feldman in his version of @code{make}
9670 for AT&T Eighth Edition Research Unix, and later by Andrew Hume of
9671 AT&T Bell Labs in his @code{mk} program (where he terms it
9672 ``transitive closure'').  We do not really know if
9673 we got this from either of them or thought it up ourselves at the
9674 same time.  @xref{Chained Rules, ,Chains of Implicit Rules}.
9676 @item
9677 The automatic variable @code{$^} containing a list of all prerequisites
9678 of the current target.  We did not invent this, but we have no idea who
9679 did.  @xref{Automatic Variables}.  The automatic variable
9680 @code{$+} is a simple extension of @code{$^}.
9682 @item
9683 The ``what if'' flag (@samp{-W} in GNU @code{make}) was (as far as we know)
9684 invented by Andrew Hume in @code{mk}.
9685 @xref{Instead of Execution, ,Instead of Executing the Commands}.
9687 @item
9688 The concept of doing several things at once (parallelism) exists in
9689 many incarnations of @code{make} and similar programs, though not in the
9690 System V or BSD implementations.  @xref{Execution, ,Command Execution}.
9692 @item
9693 Modified variable references using pattern substitution come from
9694 SunOS 4.  @xref{Reference, ,Basics of Variable References}.
9695 This functionality was provided in GNU @code{make} by the
9696 @code{patsubst} function before the alternate syntax was implemented
9697 for compatibility with SunOS 4.  It is not altogether clear who
9698 inspired whom, since GNU @code{make} had @code{patsubst} before SunOS
9699 4 was released.@refill
9701 @item
9702 The special significance of @samp{+} characters preceding command lines
9703 (@pxref{Instead of Execution, ,Instead of Executing the Commands}) is
9704 mandated by
9705 @cite{IEEE Standard 1003.2-1992} (POSIX.2).
9707 @item
9708 The @samp{+=} syntax to append to the value of a variable comes from SunOS
9709 4 @code{make}.  @xref{Appending, , Appending More Text to Variables}.
9711 @item
9712 The syntax @w{@samp{@var{archive}(@var{mem1} @var{mem2}@dots{})}} to list
9713 multiple members in a single archive file comes from SunOS 4 @code{make}.
9714 @xref{Archive Members}.
9716 @item
9717 The @code{-include} directive to include makefiles with no error for a
9718 nonexistent file comes from SunOS 4 @code{make}.  (But note that SunOS 4
9719 @code{make} does not allow multiple makefiles to be specified in one
9720 @code{-include} directive.)  The same feature appears with the name
9721 @code{sinclude} in SGI @code{make} and perhaps others.
9722 @end itemize
9724 The remaining features are inventions new in GNU @code{make}:
9726 @itemize @bullet
9727 @item
9728 Use the @samp{-v} or @samp{--version} option to print version and
9729 copyright information.
9731 @item
9732 Use the @samp{-h} or @samp{--help} option to summarize the options to
9733 @code{make}.
9735 @item
9736 Simply-expanded variables.  @xref{Flavors, ,The Two Flavors of Variables}.
9738 @item
9739 Pass command-line variable assignments automatically through the
9740 variable @code{MAKE} to recursive @code{make} invocations.
9741 @xref{Recursion, ,Recursive Use of @code{make}}.
9743 @item
9744 Use the @samp{-C} or @samp{--directory} command option to change
9745 directory.  @xref{Options Summary, ,Summary of Options}.
9747 @item
9748 Make verbatim variable definitions with @code{define}.
9749 @xref{Defining, ,Defining Variables Verbatim}.
9751 @item
9752 Declare phony targets with the special target @code{.PHONY}.
9754 Andrew Hume of AT&T Bell Labs implemented a similar feature with a
9755 different syntax in his @code{mk} program.  This seems to be a case of
9756 parallel discovery.  @xref{Phony Targets, ,Phony Targets}.
9758 @item
9759 Manipulate text by calling functions.
9760 @xref{Functions, ,Functions for Transforming Text}.
9762 @item
9763 Use the @samp{-o} or @samp{--old-file}
9764 option to pretend a file's modification-time is old.
9765 @xref{Avoiding Compilation, ,Avoiding Recompilation of Some Files}.
9767 @item
9768 Conditional execution.
9770 This feature has been implemented numerous times in various versions
9771 of @code{make}; it seems a natural extension derived from the features
9772 of the C preprocessor and similar macro languages and is not a
9773 revolutionary concept.  @xref{Conditionals, ,Conditional Parts of Makefiles}.
9775 @item
9776 Specify a search path for included makefiles.
9777 @xref{Include, ,Including Other Makefiles}.
9779 @item
9780 Specify extra makefiles to read with an environment variable.
9781 @xref{MAKEFILES Variable, ,The Variable @code{MAKEFILES}}.
9783 @item
9784 Strip leading sequences of @samp{./} from file names, so that
9785 @file{./@var{file}} and @file{@var{file}} are considered to be the
9786 same file.@refill
9788 @item
9789 Use a special search method for library prerequisites written in the
9790 form @samp{-l@var{name}}.
9791 @xref{Libraries/Search, ,Directory Search for Link Libraries}.
9793 @item
9794 Allow suffixes for suffix rules
9795 (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}) to contain any
9796 characters.  In other versions of @code{make}, they must begin with
9797 @samp{.} and not contain any @samp{/} characters.
9799 @item
9800 Keep track of the current level of @code{make} recursion using the
9801 variable @code{MAKELEVEL}.  @xref{Recursion, ,Recursive Use of @code{make}}.
9803 @item
9804 Provide any goals given on the command line in the variable
9805 @code{MAKECMDGOALS}.  @xref{Goals, ,Arguments to Specify the Goals}.
9807 @item
9808 Specify static pattern rules.  @xref{Static Pattern, ,Static Pattern Rules}.
9810 @item
9811 Provide selective @code{vpath} search.
9812 @xref{Directory Search, ,Searching Directories for Prerequisites}.
9814 @item
9815 Provide computed variable references.
9816 @xref{Reference, ,Basics of Variable References}.
9818 @item
9819 Update makefiles.  @xref{Remaking Makefiles, ,How Makefiles Are Remade}.
9820 System V @code{make} has a very, very limited form of this
9821 functionality in that it will check out SCCS files for makefiles.
9823 @item
9824 Various new built-in implicit rules.
9825 @xref{Catalogue of Rules, ,Catalogue of Implicit Rules}.
9827 @item
9828 The built-in variable @samp{MAKE_VERSION} gives the version number of
9829 @code{make}.
9830 @vindex MAKE_VERSION
9831 @end itemize
9833 @node Missing, Makefile Conventions, Features, Top
9834 @chapter Incompatibilities and Missing Features
9835 @cindex incompatibilities
9836 @cindex missing features
9837 @cindex features, missing
9839 The @code{make} programs in various other systems support a few features
9840 that are not implemented in GNU @code{make}.  The POSIX.2 standard
9841 (@cite{IEEE Standard 1003.2-1992}) which specifies @code{make} does not
9842 require any of these features.@refill
9844 @itemize @bullet
9845 @item
9846 A target of the form @samp{@var{file}((@var{entry}))} stands for a member
9847 of archive file @var{file}.  The member is chosen, not by name, but by
9848 being an object file which defines the linker symbol @var{entry}.@refill
9850 This feature was not put into GNU @code{make} because of the
9851 nonmodularity of putting knowledge into @code{make} of the internal
9852 format of archive file symbol tables.
9853 @xref{Archive Symbols, ,Updating Archive Symbol Directories}.
9855 @item
9856 Suffixes (used in suffix rules) that end with the character @samp{~}
9857 have a special meaning to System V @code{make};
9858 they refer to the SCCS file that corresponds
9859 to the file one would get without the @samp{~}.  For example, the
9860 suffix rule @samp{.c~.o} would make the file @file{@var{n}.o} from
9861 the SCCS file @file{s.@var{n}.c}.  For complete coverage, a whole
9862 series of such suffix rules is required.
9863 @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
9865 In GNU @code{make}, this entire series of cases is handled by two
9866 pattern rules for extraction from SCCS, in combination with the
9867 general feature of rule chaining.
9868 @xref{Chained Rules, ,Chains of Implicit Rules}.
9870 @item
9871 In System V and 4.3 BSD @code{make}, files found by @code{VPATH} search
9872 (@pxref{Directory Search, ,Searching Directories for Prerequisites}) have their names changed inside command
9873 strings.  We feel it is much cleaner to always use automatic variables
9874 and thus make this feature obsolete.@refill
9876 @item
9877 In some Unix @code{make}s, the automatic variable @code{$*} appearing in
9878 the prerequisites of a rule has the amazingly strange ``feature'' of
9879 expanding to the full name of the @emph{target of that rule}.  We cannot
9880 imagine what went on in the minds of Unix @code{make} developers to do
9881 this; it is utterly inconsistent with the normal definition of @code{$*}.
9882 @vindex * @r{(automatic variable), unsupported bizarre usage}
9884 @item
9885 In some Unix @code{make}s, implicit rule search
9886 (@pxref{Implicit Rules, ,Using Implicit Rules}) is apparently done for
9887 @emph{all} targets, not just those without commands.  This means you can
9888 do:@refill
9890 @example
9891 @group
9892 foo.o:
9893         cc -c foo.c
9894 @end group
9895 @end example
9897 @noindent
9898 and Unix @code{make} will intuit that @file{foo.o} depends on
9899 @file{foo.c}.@refill
9901 We feel that such usage is broken.  The prerequisite properties of
9902 @code{make} are well-defined (for GNU @code{make}, at least),
9903 and doing such a thing simply does not fit the model.@refill
9905 @item
9906 GNU @code{make} does not include any built-in implicit rules for
9907 compiling or preprocessing EFL programs.  If we hear of anyone who is
9908 using EFL, we will gladly add them.
9910 @item
9911 It appears that in SVR4 @code{make}, a suffix rule can be specified with
9912 no commands, and it is treated as if it had empty commands
9913 (@pxref{Empty Commands}).  For example:
9915 @example
9916 .c.a:
9917 @end example
9919 @noindent
9920 will override the built-in @file{.c.a} suffix rule.
9922 We feel that it is cleaner for a rule without commands to always simply
9923 add to the prerequisite list for the target.  The above example can be
9924 easily rewritten to get the desired behavior in GNU @code{make}:
9926 @example
9927 .c.a: ;
9928 @end example
9930 @item
9931 Some versions of @code{make} invoke the shell with the @samp{-e} flag,
9932 except under @samp{-k} (@pxref{Testing, ,Testing the Compilation of a
9933 Program}).  The @samp{-e} flag tells the shell to exit as soon as any
9934 program it runs returns a nonzero status.  We feel it is cleaner to
9935 write each shell command line to stand on its own and not require this
9936 special treatment.
9937 @end itemize
9939 @comment The makefile standards are in a separate file that is also
9940 @comment included by standards.texi.
9941 @include make-stds.texi
9943 @node Quick Reference, Error Messages, Makefile Conventions, Top
9944 @appendix Quick Reference
9946 This appendix summarizes the directives, text manipulation functions,
9947 and special variables which GNU @code{make} understands.
9948 @xref{Special Targets}, @ref{Catalogue of Rules, ,Catalogue of Implicit Rules},
9949 and @ref{Options Summary, ,Summary of Options},
9950 for other summaries.
9952 Here is a summary of the directives GNU @code{make} recognizes:
9954 @table @code
9955 @item define @var{variable}
9956 @itemx endef
9958 Define a multi-line, recursively-expanded variable.@*
9959 @xref{Sequences}.
9961 @item ifdef @var{variable}
9962 @itemx ifndef @var{variable}
9963 @itemx ifeq (@var{a},@var{b})
9964 @itemx ifeq "@var{a}" "@var{b}"
9965 @itemx ifeq '@var{a}' '@var{b}'
9966 @itemx ifneq (@var{a},@var{b})
9967 @itemx ifneq "@var{a}" "@var{b}"
9968 @itemx ifneq '@var{a}' '@var{b}'
9969 @itemx else
9970 @itemx endif
9972 Conditionally evaluate part of the makefile.@*
9973 @xref{Conditionals}.
9975 @item include @var{file}
9976 @itemx -include @var{file}
9977 @itemx sinclude @var{file}
9979 Include another makefile.@*
9980 @xref{Include, ,Including Other Makefiles}.
9982 @item override @var{variable} = @var{value}
9983 @itemx override @var{variable} := @var{value}
9984 @itemx override @var{variable} += @var{value}
9985 @itemx override @var{variable} ?= @var{value}
9986 @itemx override define @var{variable}
9987 @itemx endef
9989 Define a variable, overriding any previous definition, even one from
9990 the command line.@*
9991 @xref{Override Directive, ,The @code{override} Directive}.
9993 @item export
9995 Tell @code{make} to export all variables to child processes by default.@*
9996 @xref{Variables/Recursion, , Communicating Variables to a Sub-@code{make}}.
9998 @item export @var{variable}
9999 @itemx export @var{variable} = @var{value}
10000 @itemx export @var{variable} := @var{value}
10001 @itemx export @var{variable} += @var{value}
10002 @itemx export @var{variable} ?= @var{value}
10003 @itemx unexport @var{variable}
10004 Tell @code{make} whether or not to export a particular variable to child
10005 processes.@*
10006 @xref{Variables/Recursion, , Communicating Variables to a Sub-@code{make}}.
10008 @item vpath @var{pattern} @var{path}
10009 Specify a search path for files matching a @samp{%} pattern.@*
10010 @xref{Selective Search, , The @code{vpath} Directive}.
10012 @item vpath @var{pattern}
10013 Remove all search paths previously specified for @var{pattern}.
10015 @item vpath
10016 Remove all search paths previously specified in any @code{vpath}
10017 directive.
10018 @end table
10020 Here is a summary of the built-in functions (@pxref{Functions}):
10022 @table @code
10023 @item $(subst @var{from},@var{to},@var{text})
10024 Replace @var{from} with @var{to} in @var{text}.@*
10025 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10027 @item $(patsubst @var{pattern},@var{replacement},@var{text})
10028 Replace words matching @var{pattern} with @var{replacement} in @var{text}.@*
10029 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10031 @item $(strip @var{string})
10032 Remove excess whitespace characters from @var{string}.@*
10033 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10035 @item $(findstring @var{find},@var{text})
10036 Locate @var{find} in @var{text}.@*
10037 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10039 @item $(filter @var{pattern}@dots{},@var{text})
10040 Select words in @var{text} that match one of the @var{pattern} words.@*
10041 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10043 @item $(filter-out @var{pattern}@dots{},@var{text})
10044 Select words in @var{text} that @emph{do not} match any of the @var{pattern} words.@*
10045 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10047 @item $(sort @var{list})
10048 Sort the words in @var{list} lexicographically, removing duplicates.@*
10049 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10051 @item $(word @var{n},@var{text})
10052 Extract the @var{n}th word (one-origin) of @var{text}.@*
10053 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10055 @item $(words @var{text})
10056 Count the number of words in @var{text}.@*
10057 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10059 @item $(wordlist @var{s},@var{e},@var{text})
10060 Returns the list of words in @var{text} from @var{s} to @var{e}.@*
10061 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10063 @item $(firstword @var{names}@dots{})
10064 Extract the first word of @var{names}.@*
10065 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10067 @item $(lastword @var{names}@dots{})
10068 Extract the last word of @var{names}.@*
10069 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10071 @item $(dir @var{names}@dots{})
10072 Extract the directory part of each file name.@*
10073 @xref{File Name Functions, ,Functions for File Names}.
10075 @item $(notdir @var{names}@dots{})
10076 Extract the non-directory part of each file name.@*
10077 @xref{File Name Functions, ,Functions for File Names}.
10079 @item $(suffix @var{names}@dots{})
10080 Extract the suffix (the last @samp{.} and following characters) of each file name.@*
10081 @xref{File Name Functions, ,Functions for File Names}.
10083 @item $(basename @var{names}@dots{})
10084 Extract the base name (name without suffix) of each file name.@*
10085 @xref{File Name Functions, ,Functions for File Names}.
10087 @item $(addsuffix @var{suffix},@var{names}@dots{})
10088 Append @var{suffix} to each word in @var{names}.@*
10089 @xref{File Name Functions, ,Functions for File Names}.
10091 @item $(addprefix @var{prefix},@var{names}@dots{})
10092 Prepend @var{prefix} to each word in @var{names}.@*
10093 @xref{File Name Functions, ,Functions for File Names}.
10095 @item $(join @var{list1},@var{list2})
10096 Join two parallel lists of words.@*
10097 @xref{File Name Functions, ,Functions for File Names}.
10099 @item $(wildcard @var{pattern}@dots{})
10100 Find file names matching a shell file name pattern (@emph{not} a
10101 @samp{%} pattern).@*
10102 @xref{Wildcard Function, ,The Function @code{wildcard}}.
10104 @item $(realpath @var{names}@dots{})
10105 For each file name in @var{names}, expand to an absolute name that
10106 does not contain any @code{.}, @code{..}, nor symlinks.@*
10107 @xref{File Name Functions, ,Functions for File Names}.
10109 @item $(abspath @var{names}@dots{})
10110 For each file name in @var{names}, expand to an absolute name that
10111 does not contain any @code{.} or @code{..} components, but preserves
10112 symlinks.@*
10113 @xref{File Name Functions, ,Functions for File Names}.
10115 @item $(error @var{text}@dots{})
10117 When this function is evaluated, @code{make} generates a fatal error
10118 with the message @var{text}.@*
10119 @xref{Make Control Functions, ,Functions That Control Make}.
10121 @item $(warning @var{text}@dots{})
10123 When this function is evaluated, @code{make} generates a warning with
10124 the message @var{text}.@*
10125 @xref{Make Control Functions, ,Functions That Control Make}.
10127 @item $(shell @var{command})
10129 Execute a shell command and return its output.@*
10130 @xref{Shell Function, , The @code{shell} Function}.
10132 @item $(origin @var{variable})
10134 Return a string describing how the @code{make} variable @var{variable} was
10135 defined.@*
10136 @xref{Origin Function, , The @code{origin} Function}.
10138 @item $(foreach @var{var},@var{words},@var{text})
10140 Evaluate @var{text} with @var{var} bound to each word in @var{words},
10141 and concatenate the results.@*
10142 @xref{Foreach Function, ,The @code{foreach} Function}.
10144 @item $(call @var{var},@var{param},@dots{})
10146 Evaluate the variable @var{var} replacing any references to @code{$(1)},
10147 @code{$(2)} with the first, second, etc. @var{param} values.@*
10148 @xref{Call Function, ,The @code{call} Function}.
10150 @item $(eval @var{text})
10152 Evaluate @var{text} then read the results as makefile commands.
10153 Expands to the empty string.@*
10154 @xref{Eval Function, ,The @code{eval} Function}.
10156 @item $(value @var{var})
10158 Evaluates to the contents of the variable @var{var}, with no expansion
10159 performed on it.@*
10160 @xref{Value Function, ,The @code{value} Function}.
10161 @end table
10163 Here is a summary of the automatic variables.
10164 @xref{Automatic Variables},
10165 for full information.
10167 @table @code
10168 @item $@@
10169 The file name of the target.
10171 @item $%
10172 The target member name, when the target is an archive member.
10174 @item $<
10175 The name of the first prerequisite.
10177 @item $?
10178 The names of all the prerequisites that are
10179 newer than the target, with spaces between them.
10180 For prerequisites which are archive members, only
10181 the member named is used (@pxref{Archives}).
10183 @item $^
10184 @itemx $+
10185 The names of all the prerequisites, with spaces between them.  For
10186 prerequisites which are archive members, only the member named is used
10187 (@pxref{Archives}).  The value of @code{$^} omits duplicate
10188 prerequisites, while @code{$+} retains them and preserves their order.
10190 @item $*
10191 The stem with which an implicit rule matches
10192 (@pxref{Pattern Match, ,How Patterns Match}).
10194 @item $(@@D)
10195 @itemx $(@@F)
10196 The directory part and the file-within-directory part of @code{$@@}.
10198 @item $(*D)
10199 @itemx $(*F)
10200 The directory part and the file-within-directory part of @code{$*}.
10202 @item $(%D)
10203 @itemx $(%F)
10204 The directory part and the file-within-directory part of @code{$%}.
10206 @item $(<D)
10207 @itemx $(<F)
10208 The directory part and the file-within-directory part of @code{$<}.
10210 @item $(^D)
10211 @itemx $(^F)
10212 The directory part and the file-within-directory part of @code{$^}.
10214 @item $(+D)
10215 @itemx $(+F)
10216 The directory part and the file-within-directory part of @code{$+}.
10218 @item $(?D)
10219 @itemx $(?F)
10220 The directory part and the file-within-directory part of @code{$?}.
10221 @end table
10223 These variables are used specially by GNU @code{make}:
10225 @table @code
10226 @item MAKEFILES
10228 Makefiles to be read on every invocation of @code{make}.@*
10229 @xref{MAKEFILES Variable, ,The Variable @code{MAKEFILES}}.
10231 @item VPATH
10233 Directory search path for files not found in the current directory.@*
10234 @xref{General Search, , @code{VPATH} Search Path for All Prerequisites}.
10236 @item SHELL
10238 The name of the system default command interpreter, usually @file{/bin/sh}.
10239 You can set @code{SHELL} in the makefile to change the shell used to run
10240 commands.  @xref{Execution, ,Command Execution}.  The @code{SHELL}
10241 variable is handled specially when importing from and exporting to the
10242 environment.  @xref{Environment, ,Using Variable from the Environment}.
10244 @item MAKESHELL
10246 On MS-DOS only, the name of the command interpreter that is to be used
10247 by @code{make}. This value takes precedence over the value of
10248 @code{SHELL}.  @xref{Execution, ,MAKESHELL variable}.
10250 @item MAKE
10252 The name with which @code{make} was invoked.
10253 Using this variable in commands has special meaning.
10254 @xref{MAKE Variable, ,How the @code{MAKE} Variable Works}.
10256 @item MAKELEVEL
10258 The number of levels of recursion (sub-@code{make}s).@*
10259 @xref{Variables/Recursion}.
10261 @item MAKEFLAGS
10263 The flags given to @code{make}.  You can set this in the environment or
10264 a makefile to set flags.@*
10265 @xref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
10267 It is @emph{never} appropriate to use @code{MAKEFLAGS} directly on a
10268 command line: its contents may not be quoted correctly for use in the
10269 shell.  Always allow recursive @code{make}'s to obtain these values
10270 through the environment from its parent.
10272 @item MAKECMDGOALS
10274 The targets given to @code{make} on the command line.  Setting this
10275 variable has no effect on the operation of @code{make}.@*
10276 @xref{Goals, ,Arguments to Specify the Goals}.
10278 @item CURDIR
10280 Set to the pathname of the current working directory (after all
10281 @code{-C} options are processed, if any).  Setting this variable has no
10282 effect on the operation of @code{make}.@*
10283 @xref{Recursion, ,Recursive Use of @code{make}}.
10285 @item SUFFIXES
10287 The default list of suffixes before @code{make} reads any makefiles.
10289 @item .LIBPATTERNS
10290 Defines the naming of the libraries @code{make} searches for, and their
10291 order.@*
10292 @xref{Libraries/Search, ,Directory Search for Link Libraries}.
10293 @end table
10295 @node Error Messages, Complex Makefile, Quick Reference, Top
10296 @comment  node-name,  next,  previous,  up
10297 @appendix Errors Generated by Make
10299 Here is a list of the more common errors you might see generated by
10300 @code{make}, and some information about what they mean and how to fix
10301 them.
10303 Sometimes @code{make} errors are not fatal, especially in the presence
10304 of a @code{-} prefix on a command script line, or the @code{-k} command
10305 line option.  Errors that are fatal are prefixed with the string
10306 @code{***}.
10308 Error messages are all either prefixed with the name of the program
10309 (usually @samp{make}), or, if the error is found in a makefile, the name
10310 of the file and linenumber containing the problem.
10312 In the table below, these common prefixes are left off.
10314 @table @samp
10316 @item [@var{foo}] Error @var{NN}
10317 @itemx [@var{foo}] @var{signal description}
10318 These errors are not really @code{make} errors at all.  They mean that a
10319 program that @code{make} invoked as part of a command script returned a
10320 non-0 error code (@samp{Error @var{NN}}), which @code{make} interprets
10321 as failure, or it exited in some other abnormal fashion (with a
10322 signal of some type).  @xref{Errors, ,Errors in Commands}.
10324 If no @code{***} is attached to the message, then the subprocess failed
10325 but the rule in the makefile was prefixed with the @code{-} special
10326 character, so @code{make} ignored the error.
10328 @item missing separator.  Stop.
10329 @itemx missing separator (did you mean TAB instead of 8 spaces?).  Stop.
10330 This means that @code{make} could not understand much of anything about
10331 the command line it just read.  GNU @code{make} looks for various kinds
10332 of separators (@code{:}, @code{=}, TAB characters, etc.) to help it
10333 decide what kind of commandline it's seeing.  This means it couldn't
10334 find a valid one.
10336 One of the most common reasons for this message is that you (or perhaps
10337 your oh-so-helpful editor, as is the case with many MS-Windows editors)
10338 have attempted to indent your command scripts with spaces instead of a
10339 TAB character.  In this case, @code{make} will use the second form of
10340 the error above.  Remember that every line in the command script must
10341 begin with a TAB character.  Eight spaces do not count.  @xref{Rule
10342 Syntax}.
10344 @item commands commence before first target.  Stop.
10345 @itemx missing rule before commands.  Stop.
10346 This means the first thing in the makefile seems to be part of a command
10347 script: it begins with a TAB character and doesn't appear to be a legal
10348 @code{make} command (such as a variable assignment).  Command scripts
10349 must always be associated with a target.
10351 The second form is generated if the line has a semicolon as the first
10352 non-whitespace character; @code{make} interprets this to mean you left
10353 out the "target: prerequisite" section of a rule.  @xref{Rule Syntax}.
10355 @item No rule to make target `@var{xxx}'.
10356 @itemx No rule to make target `@var{xxx}', needed by `@var{yyy}'.
10357 This means that @code{make} decided it needed to build a target, but
10358 then couldn't find any instructions in the makefile on how to do that,
10359 either explicit or implicit (including in the default rules database).
10361 If you want that file to be built, you will need to add a rule to your
10362 makefile describing how that target can be built.  Other possible
10363 sources of this problem are typos in the makefile (if that filename is
10364 wrong) or a corrupted source tree (if that file is not supposed to be
10365 built, but rather only a prerequisite).
10367 @item No targets specified and no makefile found.  Stop.
10368 @itemx No targets.  Stop.
10369 The former means that you didn't provide any targets to be built on the
10370 command line, and @code{make} couldn't find any makefiles to read in.
10371 The latter means that some makefile was found, but it didn't contain any
10372 default goal and none was given on the command line.  GNU @code{make}
10373 has nothing to do in these situations.
10374 @xref{Makefile Arguments, ,Arguments to Specify the Makefile}.@refill
10376 @item Makefile `@var{xxx}' was not found.
10377 @itemx Included makefile `@var{xxx}' was not found.
10378 A makefile specified on the command line (first form) or included
10379 (second form) was not found.
10381 @item warning: overriding commands for target `@var{xxx}'
10382 @itemx warning: ignoring old commands for target `@var{xxx}'
10383 GNU @code{make} allows commands to be specified only once per target
10384 (except for double-colon rules).  If you give commands for a target
10385 which already has been defined to have commands, this warning is issued
10386 and the second set of commands will overwrite the first set.
10387 @xref{Multiple Rules, ,Multiple Rules for One Target}.
10389 @item Circular @var{xxx} <- @var{yyy} dependency dropped.
10390 This means that @code{make} detected a loop in the dependency graph:
10391 after tracing the prerequisite @var{yyy} of target @var{xxx}, and its
10392 prerequisites, etc., one of them depended on @var{xxx} again.
10394 @item Recursive variable `@var{xxx}' references itself (eventually).  Stop.
10395 This means you've defined a normal (recursive) @code{make} variable
10396 @var{xxx} that, when it's expanded, will refer to itself (@var{xxx}).
10397 This is not allowed; either use simply-expanded variables (@code{:=}) or
10398 use the append operator (@code{+=}).  @xref{Using Variables, ,How to Use
10399 Variables}.
10401 @item Unterminated variable reference.  Stop.
10402 This means you forgot to provide the proper closing parenthesis
10403 or brace in your variable or function reference.
10405 @item insufficient arguments to function `@var{xxx}'.  Stop.
10406 This means you haven't provided the requisite number of arguments for
10407 this function.  See the documentation of the function for a description
10408 of its arguments.  @xref{Functions, ,Functions for Transforming Text}.
10410 @item missing target pattern.  Stop.
10411 @itemx multiple target patterns.  Stop.
10412 @itemx target pattern contains no `%'.  Stop.
10413 @itemx mixed implicit and static pattern rules.  Stop.
10414 These are generated for malformed static pattern rules.  The first means
10415 there's no pattern in the target section of the rule; the second means
10416 there are multiple patterns in the target section; the third means
10417 the target doesn't contain a pattern character (@code{%}); and the
10418 fourth means that all three parts of the static pattern rule contain
10419 pattern characters (@code{%})--only the first two parts should.
10420 @xref{Static Usage, ,Syntax of Static Pattern Rules}.
10422 @item warning: -jN forced in submake: disabling jobserver mode.
10423 This warning and the next are generated if @code{make} detects error
10424 conditions related to parallel processing on systems where
10425 sub-@code{make}s can communicate (@pxref{Options/Recursion,
10426 ,Communicating Options to a Sub-@code{make}}).  This warning is
10427 generated if a recursive invocation of a @code{make} process is forced
10428 to have @samp{-j@var{N}} in its argument list (where @var{N} is greater
10429 than one).  This could happen, for example, if you set the @code{MAKE}
10430 environment variable to @samp{make -j2}.  In this case, the
10431 sub-@code{make} doesn't communicate with other @code{make} processes and
10432 will simply pretend it has two jobs of its own.
10434 @item warning: jobserver unavailable: using -j1.  Add `+' to parent make rule.
10435 In order for @code{make} processes to communicate, the parent will pass
10436 information to the child.  Since this could result in problems if the
10437 child process isn't actually a @code{make}, the parent will only do this
10438 if it thinks the child is a @code{make}.  The parent uses the normal
10439 algorithms to determine this (@pxref{MAKE Variable, ,How the @code{MAKE}
10440 Variable Works}).  If the makefile is constructed such that the parent
10441 doesn't know the child is a @code{make} process, then the child will
10442 receive only part of the information necessary.  In this case, the child
10443 will generate this warning message and proceed with its build in a
10444 sequential manner.
10446 @end table
10448 @node Complex Makefile, GNU Free Documentation License, Error Messages, Top
10449 @appendix Complex Makefile Example
10451 Here is the makefile for the GNU @code{tar} program.  This is a
10452 moderately complex makefile.
10454 Because it is the first target, the default goal is @samp{all}.  An
10455 interesting feature of this makefile is that @file{testpad.h} is a
10456 source file automatically created by the @code{testpad} program,
10457 itself compiled from @file{testpad.c}.
10459 If you type @samp{make} or @samp{make all}, then @code{make} creates
10460 the @file{tar} executable, the @file{rmt} daemon that provides
10461 remote tape access, and the @file{tar.info} Info file.
10463 If you type @samp{make install}, then @code{make} not only creates
10464 @file{tar}, @file{rmt}, and @file{tar.info}, but also installs
10465 them.
10467 If you type @samp{make clean}, then @code{make} removes the @samp{.o}
10468 files, and the @file{tar}, @file{rmt}, @file{testpad},
10469 @file{testpad.h}, and @file{core} files.
10471 If you type @samp{make distclean}, then @code{make} not only removes
10472 the same files as does @samp{make clean} but also the
10473 @file{TAGS}, @file{Makefile}, and @file{config.status} files.
10474 (Although it is not evident, this makefile (and
10475 @file{config.status}) is generated by the user with the
10476 @code{configure} program, which is provided in the @code{tar}
10477 distribution, but is not shown here.)
10479 If you type @samp{make realclean}, then @code{make} removes the same
10480 files as does @samp{make distclean} and also removes the Info files
10481 generated from @file{tar.texinfo}.
10483 In addition, there are targets @code{shar} and @code{dist} that create
10484 distribution kits.
10486 @example
10487 @group
10488 # Generated automatically from Makefile.in by configure.
10489 # Un*x Makefile for GNU tar program.
10490 # Copyright (C) 1991 Free Software Foundation, Inc.
10491 @end group
10493 @group
10494 # This program is free software; you can redistribute
10495 # it and/or modify it under the terms of the GNU
10496 # General Public License @dots{}
10497 @dots{}
10498 @dots{}
10499 @end group
10501 SHELL = /bin/sh
10503 #### Start of system configuration section. ####
10505 srcdir = .
10507 @group
10508 # If you use gcc, you should either run the
10509 # fixincludes script that comes with it or else use
10510 # gcc with the -traditional option.  Otherwise ioctl
10511 # calls will be compiled incorrectly on some systems.
10512 CC = gcc -O
10513 YACC = bison -y
10514 INSTALL = /usr/local/bin/install -c
10515 INSTALLDATA = /usr/local/bin/install -c -m 644
10516 @end group
10518 # Things you might add to DEFS:
10519 # -DSTDC_HEADERS        If you have ANSI C headers and
10520 #                       libraries.
10521 # -DPOSIX               If you have POSIX.1 headers and
10522 #                       libraries.
10523 # -DBSD42               If you have sys/dir.h (unless
10524 #                       you use -DPOSIX), sys/file.h,
10525 #                       and st_blocks in `struct stat'.
10526 # -DUSG                 If you have System V/ANSI C
10527 #                       string and memory functions
10528 #                       and headers, sys/sysmacros.h,
10529 #                       fcntl.h, getcwd, no valloc,
10530 #                       and ndir.h (unless
10531 #                       you use -DDIRENT).
10532 # -DNO_MEMORY_H         If USG or STDC_HEADERS but do not
10533 #                       include memory.h.
10534 # -DDIRENT              If USG and you have dirent.h
10535 #                       instead of ndir.h.
10536 # -DSIGTYPE=int         If your signal handlers
10537 #                       return int, not void.
10538 # -DNO_MTIO             If you lack sys/mtio.h
10539 #                       (magtape ioctls).
10540 # -DNO_REMOTE           If you do not have a remote shell
10541 #                       or rexec.
10542 # -DUSE_REXEC           To use rexec for remote tape
10543 #                       operations instead of
10544 #                       forking rsh or remsh.
10545 # -DVPRINTF_MISSING     If you lack vprintf function
10546 #                       (but have _doprnt).
10547 # -DDOPRNT_MISSING      If you lack _doprnt function.
10548 #                       Also need to define
10549 #                       -DVPRINTF_MISSING.
10550 # -DFTIME_MISSING       If you lack ftime system call.
10551 # -DSTRSTR_MISSING      If you lack strstr function.
10552 # -DVALLOC_MISSING      If you lack valloc function.
10553 # -DMKDIR_MISSING       If you lack mkdir and
10554 #                       rmdir system calls.
10555 # -DRENAME_MISSING      If you lack rename system call.
10556 # -DFTRUNCATE_MISSING   If you lack ftruncate
10557 #                       system call.
10558 # -DV7                  On Version 7 Unix (not
10559 #                       tested in a long time).
10560 # -DEMUL_OPEN3          If you lack a 3-argument version
10561 #                       of open, and want to emulate it
10562 #                       with system calls you do have.
10563 # -DNO_OPEN3            If you lack the 3-argument open
10564 #                       and want to disable the tar -k
10565 #                       option instead of emulating open.
10566 # -DXENIX               If you have sys/inode.h
10567 #                       and need it 94 to be included.
10569 DEFS =  -DSIGTYPE=int -DDIRENT -DSTRSTR_MISSING \
10570         -DVPRINTF_MISSING -DBSD42
10571 # Set this to rtapelib.o unless you defined NO_REMOTE,
10572 # in which case make it empty.
10573 RTAPELIB = rtapelib.o
10574 LIBS =
10575 DEF_AR_FILE = /dev/rmt8
10576 DEFBLOCKING = 20
10578 @group
10579 CDEBUG = -g
10580 CFLAGS = $(CDEBUG) -I. -I$(srcdir) $(DEFS) \
10581         -DDEF_AR_FILE=\"$(DEF_AR_FILE)\" \
10582         -DDEFBLOCKING=$(DEFBLOCKING)
10583 LDFLAGS = -g
10584 @end group
10586 @group
10587 prefix = /usr/local
10588 # Prefix for each installed program,
10589 # normally empty or `g'.
10590 binprefix =
10592 # The directory to install tar in.
10593 bindir = $(prefix)/bin
10595 # The directory to install the info files in.
10596 infodir = $(prefix)/info
10597 @end group
10599 #### End of system configuration section. ####
10601 SRC1 =  tar.c create.c extract.c buffer.c \
10602         getoldopt.c update.c gnu.c mangle.c
10603 SRC2 =  version.c list.c names.c diffarch.c \
10604         port.c wildmat.c getopt.c
10605 SRC3 =  getopt1.c regex.c getdate.y
10606 SRCS =  $(SRC1) $(SRC2) $(SRC3)
10607 OBJ1 =  tar.o create.o extract.o buffer.o \
10608         getoldopt.o update.o gnu.o mangle.o
10609 OBJ2 =  version.o list.o names.o diffarch.o \
10610         port.o wildmat.o getopt.o
10611 OBJ3 =  getopt1.o regex.o getdate.o $(RTAPELIB)
10612 OBJS =  $(OBJ1) $(OBJ2) $(OBJ3)
10613 @group
10614 AUX =   README COPYING ChangeLog Makefile.in  \
10615         makefile.pc configure configure.in \
10616         tar.texinfo tar.info* texinfo.tex \
10617         tar.h port.h open3.h getopt.h regex.h \
10618         rmt.h rmt.c rtapelib.c alloca.c \
10619         msd_dir.h msd_dir.c tcexparg.c \
10620         level-0 level-1 backup-specs testpad.c
10621 @end group
10623 .PHONY: all
10624 all:    tar rmt tar.info
10626 @group
10627 .PHONY: tar
10628 tar:    $(OBJS)
10629         $(CC) $(LDFLAGS) -o $@@ $(OBJS) $(LIBS)
10630 @end group
10632 @group
10633 rmt:    rmt.c
10634         $(CC) $(CFLAGS) $(LDFLAGS) -o $@@ rmt.c
10635 @end group
10637 @group
10638 tar.info: tar.texinfo
10639         makeinfo tar.texinfo
10640 @end group
10642 @group
10643 .PHONY: install
10644 install: all
10645         $(INSTALL) tar $(bindir)/$(binprefix)tar
10646         -test ! -f rmt || $(INSTALL) rmt /etc/rmt
10647         $(INSTALLDATA) $(srcdir)/tar.info* $(infodir)
10648 @end group
10650 @group
10651 $(OBJS): tar.h port.h testpad.h
10652 regex.o buffer.o tar.o: regex.h
10653 # getdate.y has 8 shift/reduce conflicts.
10654 @end group
10656 @group
10657 testpad.h: testpad
10658         ./testpad
10659 @end group
10661 @group
10662 testpad: testpad.o
10663         $(CC) -o $@@ testpad.o
10664 @end group
10666 @group
10667 TAGS:   $(SRCS)
10668         etags $(SRCS)
10669 @end group
10671 @group
10672 .PHONY: clean
10673 clean:
10674         rm -f *.o tar rmt testpad testpad.h core
10675 @end group
10677 @group
10678 .PHONY: distclean
10679 distclean: clean
10680         rm -f TAGS Makefile config.status
10681 @end group
10683 @group
10684 .PHONY: realclean
10685 realclean: distclean
10686         rm -f tar.info*
10687 @end group
10689 @group
10690 .PHONY: shar
10691 shar: $(SRCS) $(AUX)
10692         shar $(SRCS) $(AUX) | compress \
10693           > tar-`sed -e '/version_string/!d' \
10694                      -e 's/[^0-9.]*\([0-9.]*\).*/\1/' \
10695                      -e q
10696                      version.c`.shar.Z
10697 @end group
10699 @group
10700 .PHONY: dist
10701 dist: $(SRCS) $(AUX)
10702         echo tar-`sed \
10703              -e '/version_string/!d' \
10704              -e 's/[^0-9.]*\([0-9.]*\).*/\1/' \
10705              -e q
10706              version.c` > .fname
10707         -rm -rf `cat .fname`
10708         mkdir `cat .fname`
10709         ln $(SRCS) $(AUX) `cat .fname`
10710         tar chZf `cat .fname`.tar.Z `cat .fname`
10711         -rm -rf `cat .fname` .fname
10712 @end group
10714 @group
10715 tar.zoo: $(SRCS) $(AUX)
10716         -rm -rf tmp.dir
10717         -mkdir tmp.dir
10718         -rm tar.zoo
10719         for X in $(SRCS) $(AUX) ; do \
10720             echo $$X ; \
10721             sed 's/$$/^M/' $$X \
10722             > tmp.dir/$$X ; done
10723         cd tmp.dir ; zoo aM ../tar.zoo *
10724         -rm -rf tmp.dir
10725 @end group
10726 @end example
10728 @raisesections
10729 @include fdl.texi
10730 @lowersections
10732 @node Concept Index, Name Index, GNU Free Documentation License, Top
10733 @unnumbered Index of Concepts
10735 @printindex cp
10737 @node Name Index,  , Concept Index, Top
10738 @unnumbered Index of Functions, Variables, & Directives
10740 @printindex fn
10742 @summarycontents
10743 @contents
10744 @bye