RMS documented his .SECONDARY feature.
[make.git] / make.texinfo
blob0cd7c9bebff678c290fdfb7792a54883ba3334a8
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.texinfo,v 2.158 1996/03/20 00:58:43 roland Exp $
11 @set EDITION 0.50
12 @set VERSION 3.75 Beta
13 @set UPDATED 28 February 1996
14 @set UPDATE-MONTH February 1996
15 @comment The ISBN number might need to change on next publication.
16 @set ISBN 1-882114-78-7
18 @c finalout
20 @c ISPELL CHECK: done, 10 June 1993 --roland
22 @c Combine the variable and function indices:
23 @syncodeindex vr fn
24 @c Combine the program and concept indices:
25 @syncodeindex pg cp
27 @ifinfo
28 This file documents the GNU Make utility, which determines
29 automatically which pieces of a large program need to be recompiled,
30 and issues the commands to recompile them.
32 This is Edition @value{EDITION}, last updated @value{UPDATED},
33 of @cite{The GNU Make Manual}, for @code{make}, Version @value{VERSION}.
35 Copyright (C) 1988, '89, '90, '91, '92, '93, '94, '95, '96
36         Free Software Foundation, Inc.
38 Permission is granted to make and distribute verbatim copies of
39 this manual provided the copyright notice and this permission notice
40 are preserved on all copies.
42 @ignore
43 Permission is granted to process this file through TeX and print the
44 results, provided the printed document carries copying permission
45 notice identical to this one except for the removal of this paragraph
46 (this paragraph not being relevant to the printed manual).
48 @end ignore
49 Permission is granted to copy and distribute modified versions of this
50 manual under the conditions for verbatim copying, provided that the entire
51 resulting derived work is distributed under the terms of a permission
52 notice identical to this one.
54 Permission is granted to copy and distribute translations of this manual
55 into another language, under the above conditions for modified versions,
56 except that this permission notice may be stated in a translation approved
57 by the Free Software Foundation.
58 @end ifinfo
60 @iftex
61 @shorttitlepage GNU Make
62 @end iftex
63 @titlepage
64 @title GNU Make
65 @subtitle A Program for Directing Recompilation
66 @subtitle Edition @value{EDITION}, for @code{make} Version @value{VERSION}.
67 @subtitle @value{UPDATE-MONTH}
68 @author Richard M. Stallman and Roland McGrath
69 @page
70 @vskip 0pt plus 1filll
71 Copyright @copyright{} 1988, '89, '90, '91, '92, '93, '94, '95, '96 Free Software Foundation, Inc.
72 @sp 2
73 Published by the Free Software Foundation @*
74 59 Temple Place -- Suite 330, @*
75 Boston, MA 02111-1307 USA @*
76 Printed copies are available for $20 each. @*
77 ISBN @value{ISBN} @*
79 Permission is granted to make and distribute verbatim copies of
80 this manual provided the copyright notice and this permission notice
81 are preserved on all copies.
83 Permission is granted to copy and distribute modified versions of this
84 manual under the conditions for verbatim copying, provided that the entire
85 resulting derived work is distributed under the terms of a permission
86 notice identical to this one.
88 Permission is granted to copy and distribute translations of this manual
89 into another language, under the above conditions for modified versions,
90 except that this permission notice may be stated in a translation approved
91 by the Free Software Foundation.
92 @sp 2
93 Cover art by Etienne Suvasa.
94 @end titlepage
95 @page
97 @ifinfo
98 @node Top, Overview, (dir), (dir)
99 @top Make
101 The GNU @code{make} utility automatically determines which pieces of a
102 large program need to be recompiled, and issues the commands to
103 recompile them.@refill
105 This is Edition @value{EDITION} of the @cite{GNU Make Manual},
106 last updated @value{UPDATED}
107 for @code{make} Version @value{VERSION}.@refill
109 This manual describes @code{make} and contains the following chapters:@refill
110 @end ifinfo
112 @menu
113 * Overview::                    Overview of @code{make}.
114 * Introduction::                An introduction to @code{make}.
115 * Makefiles::                   Makefiles tell @code{make} what to do.
116 * Rules::                       Rules describe when a file must be remade.
117 * Commands::                    Commands say how to remake a file.
118 * Using Variables::             You can use variables to avoid repetition.
119 * Conditionals::                Use or ignore parts of the makefile based
120                                  on the values of variables.
121 * Functions::                   Many powerful ways to manipulate text.
122 * make Invocation: Running.     How to invoke @code{make} on the command line.
123 * Implicit Rules::              Use implicit rules to treat many files alike,
124                                  based on their file names.
125 * Archives::                    How @code{make} can update library archives.
126 * Features::                    Features GNU @code{make} has over other @code{make}s.
127 * Missing::                     What GNU @code{make} lacks from other @code{make}s.
128 * Makefile Conventions::        Conventions for makefiles in GNU programs.
129 * Quick Reference::             A quick reference for experienced users.
130 * Complex Makefile::            A real example of a straightforward,
131                                  but nontrivial, makefile.
132 * Concept Index::               Index of Concepts
133 * Name Index::                  Index of Functions, Variables, & Directives
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 Dependency::       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 * Remaking Makefiles::          How makefiles get remade.
160 * Overriding Makefiles::        How to override part of one makefile
161                                  with another makefile.
163 Writing Rules
165 * Rule Example::                An example explained.
166 * Rule Syntax::                 General syntax explained.
167 * Wildcards::                   Using wildcard characters such as `*'.
168 * Directory Search::            Searching other directories for source files.
169 * Phony Targets::               Using a target that is not a real file's name.
170 * Force Targets::               You can use a target without commands
171                                   or dependencies to mark other
172                                   targets as phony.
173 * Empty Targets::               When only the date matters and the
174                                   files are empty.
175 * Special Targets::             Targets with special built-in meanings.
176 * Multiple Targets::            When to make use of several targets in a rule.
177 * Multiple Rules::              How to use several rules with the same target.
178 * Static Pattern::              Static pattern rules apply to multiple targets
179                                   and can vary the dependencies according to
180                                   the target name.
181 * Double-Colon::                How to use a special kind of rule to allow
182                                   several independent rules for one target.
183 * Automatic Dependencies::      How to automatically generate rules giving
184                                  dependencies from the source files themselves.
186 Using Wildcard Characters in File Names
188 * Wildcard Examples::           Several examples
189 * Wildcard Pitfall::            Problems to avoid.
190 * Wildcard Function::           How to cause wildcard expansion where
191                                   it does not normally take place.
193 Searching Directories for Dependencies
195 * General Search::              Specifying a search path that applies
196                                   to every dependency.
197 * Selective Search::            Specifying a search path
198                                   for a specified class of names.
199 * Commands/Search::             How to write shell commands that work together
200                                   with search paths.
201 * Implicit/Search::             How search paths affect implicit rules.
202 * Libraries/Search::            Directory search for link libraries.
204 Static Pattern Rules
206 * Static Usage::                The syntax of static pattern rules.
207 * Static versus Implicit::      When are they better than implicit rules?
209 Writing the Commands in Rules
211 * Echoing::                     How to control when commands are echoed.
212 * Execution::                   How commands are executed.
213 * Parallel::                    How commands can be executed in parallel.
214 * Errors::                      What happens after a command execution error.
215 * Interrupts::                  What happens when a command is interrupted.
216 * Recursion::                   Invoking @code{make} from makefiles.
217 * Sequences::                   Defining canned sequences of commands.
218 * Empty Commands::              Defining useful, do-nothing commands.
220 Recursive Use of @code{make}
222 * MAKE Variable::               The special effects of using @samp{$(MAKE)}.
223 * Variables/Recursion::         How to communicate variables to a sub-@code{make}.
224 * Options/Recursion::           How to communicate options to a sub-@code{make}.
225 * -w Option::                   How the @samp{-w} or @samp{--print-directory} option
226                                  helps debug use of recursive @code{make} commands.
228 How to Use Variables
230 * Reference::                   How to use the value of a variable.
231 * Flavors::                     Variables come in two flavors.
232 * Advanced::                    Advanced features for referencing a variable.
233 * Values::                      All the ways variables get their values.
234 * Setting::                     How to set a variable in the makefile.
235 * Appending::                   How to append more text to the old value
236                                   of a variable.
237 * Override Directive::          How to set a variable in the makefile even if
238                                   the user has set it with a command argument.
239 * Defining::                    An alternate way to set a variable
240                                   to a verbatim string.
241 * Environment::                 Variable values can come from the environment.
243 Advanced Features for Reference to Variables
245 * Substitution Refs::           Referencing a variable with
246                                   substitutions on the value.
247 * Computed Names::              Computing the name of the variable to refer to.
249 Conditional Parts of Makefiles
251 * Conditional Example::         Example of a conditional
252 * Conditional Syntax::          The syntax of conditionals.
253 * Testing Flags::               Conditionals that test flags.
255 Functions for Transforming Text
257 * Syntax of Functions::         How to write a function call.
258 * Text Functions::              General-purpose text manipulation functions.
259 * Filename Functions::          Functions for manipulating file names.
260 * Foreach Function::            Repeat some text with controlled variation.
261 * Origin Function::             Find where a variable got its value.
262 * Shell Function::              Substitute the output of a shell command.
264 How to Run @code{make}
266 * Makefile Arguments::          How to specify which makefile to use.
267 * Goals::                       How to use goal arguments to specify which
268                                   parts of the makefile to use.
269 * Instead of Execution::        How to use mode flags to specify what
270                                   kind of thing to do with the commands
271                                   in the makefile other than simply
272                                   execute them.
273 * Avoiding Compilation::        How to avoid recompiling certain files.
274 * Overriding::                  How to override a variable to specify
275                                   an alternate compiler and other things.
276 * Testing::                     How to proceed past some errors, to
277                                   test compilation.
278 * Options Summary::             Summary of Options
280 Using Implicit Rules
282 * Using Implicit::              How to use an existing implicit rule
283                                   to get the commands for updating a file.
284 * Catalogue of Rules::          A list of built-in implicit rules.
285 * Implicit Variables::          How to change what predefined rules do.
286 * Chained Rules::               How to use a chain of implicit rules.
287 * Pattern Rules::               How to define new implicit rules.
288 * Last Resort::                 How to defining commands for rules
289                                   which cannot find any.
290 * Suffix Rules::                The old-fashioned style of implicit rule.
291 * Search Algorithm::            The precise algorithm for applying
292                                   implicit rules.
294 Defining and Redefining Pattern Rules
296 * Pattern Intro::               An introduction to pattern rules.
297 * Pattern Examples::            Examples of pattern rules.
298 * Automatic::                   How to use automatic variables in the
299                                   commands of implicit rules.
300 * Pattern Match::               How patterns match.
301 * Match-Anything Rules::        Precautions you should take prior to
302                                   defining rules that can match any
303                                   target file whatever.
304 * Canceling Rules::             How to override or cancel built-in rules.
306 Using @code{make} to Update Archive Files
308 * Archive Members::             Archive members as targets.
309 * Archive Update::              The implicit rule for archive member targets.
310 * Archive Suffix Rules::        You can write a special kind of suffix rule
311                                   for updating archives.
313 Implicit Rule for Archive Member Targets
315 * Archive Symbols::             How to update archive symbol directories.
316 @end menu
318 @node Overview, Introduction, Top, Top
319 @comment  node-name,  next,  previous,  up
320 @chapter Overview of @code{make}
322 The @code{make} utility automatically determines which pieces of a large
323 program need to be recompiled, and issues commands to recompile them.
324 This manual describes GNU @code{make}, which was implemented by Richard
325 Stallman and Roland McGrath.  GNU @code{make} conforms to section 6.2 of
326 @cite{IEEE Standard 1003.2-1992} (POSIX.2).
327 @cindex POSIX
328 @cindex IEEE Standard 1003.2
329 @cindex standards conformance
331 Our examples show C programs, since they are most common, but you can use
332 @code{make} with any programming language whose compiler can be run with a
333 shell command.  Indeed, @code{make} is not limited to programs.  You can
334 use it to describe any task where some files must be updated automatically
335 from others whenever the others change.
337 @menu
338 * Preparing::                   Preparing and Running Make
339 * Reading::                     On Reading this Text
340 * Bugs::                        Problems and Bugs
341 @end menu
343 @node Preparing, Reading,  , Overview
344 @ifinfo
345 @heading Preparing and Running Make
346 @end ifinfo
348 To prepare to use @code{make}, you must write a file called
349 the @dfn{makefile} that describes the relationships among files
350 in your program and provides commands for updating each file.
351 In a program, typically, the executable file is updated from object
352 files, which are in turn made by compiling source files.@refill
354 Once a suitable makefile exists, each time you change some source files,
355 this simple shell command:
357 @example
358 make
359 @end example
361 @noindent
362 suffices to perform all necessary recompilations.  The @code{make} program
363 uses the makefile data base and the last-modification times of the files to
364 decide which of the files need to be updated.  For each of those files, it
365 issues the commands recorded in the data base.
367 You can provide command line arguments to @code{make} to control which
368 files should be recompiled, or how.  @xref{Running, ,How to Run
369 @code{make}}.
371 @node Reading, Bugs, Preparing, Overview
372 @section How to Read This Manual
374 If you are new to @code{make}, or are looking for a general
375 introduction, read the first few sections of each chapter, skipping the
376 later sections.  In each chapter, the first few sections contain
377 introductory or general information and the later sections contain
378 specialized or technical information.
379 @ifinfo
380 The exception is the second chapter, @ref{Introduction, ,An
381 Introduction to Makefiles}, all of which is introductory.
382 @end ifinfo
383 @iftex
384 The exception is @ref{Introduction, ,An Introduction to Makefiles},
385 all of which is introductory.
386 @end iftex
388 If you are familiar with other @code{make} programs, see @ref{Features,
389 ,Features of GNU @code{make}}, which lists the enhancements GNU
390 @code{make} has, and @ref{Missing, ,Incompatibilities and Missing
391 Features}, which explains the few things GNU @code{make} lacks that
392 others have.
394 For a quick summary, see @ref{Options Summary}, @ref{Quick Reference},
395 and @ref{Special Targets}.
397 @node Bugs,  , Reading, Overview
398 @section Problems and Bugs
399 @cindex reporting bugs
400 @cindex bugs, reporting
401 @cindex problems and bugs, reporting
403 If you have problems with GNU @code{make} or think you've found a bug,
404 please report it to the developers; we cannot promise to do anything but
405 we might well want to fix it.
407 Before reporting a bug, make sure you've actually found a real bug.
408 Carefully reread the documentation and see if it really says you can do
409 what you're trying to do.  If it's not clear whether you should be able
410 to do something or not, report that too; it's a bug in the
411 documentation!
413 Before reporting a bug or trying to fix it yourself, try to isolate it
414 to the smallest possible makefile that reproduces the problem.  Then
415 send us the makefile and the exact results @code{make} gave you.  Also
416 say what you expected to occur; this will help us decide whether the
417 problem was really in the documentation.
419 Once you've got a precise problem, please send electronic mail either
420 through the Internet or via UUCP:
422 @example
423 @group
424 @r{Internet address:}
425     bug-gnu-utils@@prep.ai.mit.edu
427 @r{UUCP path:}
428     mit-eddie!prep.ai.mit.edu!bug-gnu-utils
429 @end group
430 @end example
432 @noindent
433 Please include the version number of @code{make} you are using.  You can
434 get this information with the command @samp{make --version}.
435 Be sure also to include the type of machine and operating system you are
436 using.  If possible, include the contents of the file @file{config.h}
437 that is generated by the configuration process.
439 @node Introduction, Makefiles, Overview, Top
440 @comment  node-name,  next,  previous,  up
441 @chapter An Introduction to Makefiles
443 You need a file called a @dfn{makefile} to tell @code{make} what to do.
444 Most often, the makefile tells @code{make} how to compile and link a
445 program.
446 @cindex makefile
448 In this chapter, we will discuss a simple makefile that describes how to
449 compile and link a text editor which consists of eight C source files
450 and three header files.  The makefile can also tell @code{make} how to
451 run miscellaneous commands when explicitly asked (for example, to remove
452 certain files as a clean-up operation).  To see a more complex example
453 of a makefile, see @ref{Complex Makefile}.
455 When @code{make} recompiles the editor, each changed C source file
456 must be recompiled.  If a header file has changed, each C source file
457 that includes the header file must be recompiled to be safe.  Each
458 compilation produces an object file corresponding to the source file.
459 Finally, if any source file has been recompiled, all the object files,
460 whether newly made or saved from previous compilations, must be linked
461 together to produce the new executable editor.
462 @cindex recompilation
463 @cindex editor
465 @menu
466 * Rule Introduction::           What a rule looks like.
467 * Simple Makefile::             A Simple Makefile
468 * How Make Works::              How @code{make} Processes This Makefile
469 * Variables Simplify::          Variables Make Makefiles Simpler
470 * make Deduces::                Letting @code{make} Deduce the Commands
471 * Combine By Dependency::       Another Style of Makefile
472 * Cleanup::                     Rules for Cleaning the Directory
473 @end menu
475 @node Rule Introduction, Simple Makefile,  , Introduction
476 @comment  node-name,  next,  previous,  up
477 @section What a Rule Looks Like
478 @cindex rule, introduction to
479 @cindex makefile rule parts
480 @cindex parts of makefile rule
482 A simple makefile consists of ``rules'' with the following shape:
484 @cindex targets, introduction to
485 @cindex dependencies, introduction to
486 @cindex commands, introduction to
487 @example
488 @group
489 @var{target} @dots{} : @var{dependencies} @dots{}
490         @var{command}
491         @dots{}
492         @dots{}
493 @end group
494 @end example
496 A @dfn{target} is usually the name of a file that is generated by a
497 program; examples of targets are executable or object files.  A target
498 can also be the name of an action to carry out, such as @samp{clean}
499 (@pxref{Phony Targets}).
501 A @dfn{dependency} is a file that is used as input to create the
502 target.  A target often depends on several files.
504 @cindex tabs in rules
505 A @dfn{command} is an action that @code{make} carries out.
506 A rule may have more than one command, each on its own line.
507 @strong{Please note:} you need to put a tab character at the beginning of
508 every command line!  This is an obscurity that catches the unwary.
510 Usually a command is in a rule with dependencies and serves to create a
511 target file if any of the dependencies change.  However, the rule that
512 specifies commands for the target need not have dependencies.  For
513 example, the rule containing the delete command associated with the
514 target @samp{clean} does not have dependencies.
516 A @dfn{rule}, then, explains how and when to remake certain files
517 which are the targets of the particular rule.  @code{make} carries out
518 the commands on the dependencies to create or update the target.  A
519 rule can also explain how and when to carry out an action.
520 @xref{Rules, , Writing Rules}.
522 A makefile may contain other text besides rules, but a simple makefile
523 need only contain rules.  Rules may look somewhat more complicated
524 than shown in this template, but all fit the pattern more or less.
526 @node Simple Makefile, How Make Works, Rule Introduction, Introduction
527 @section A Simple Makefile
528 @cindex simple makefile
529 @cindex makefile, simple
531 Here is a straightforward makefile that describes the way an
532 executable file called @code{edit} depends on eight object files
533 which, in turn, depend on eight C source and three header files.
535 In this example, all the C files include @file{defs.h}, but only those
536 defining editing commands include @file{command.h}, and only low
537 level files that change the editor buffer include @file{buffer.h}.
539 @example
540 @group
541 edit : main.o kbd.o command.o display.o \
542        insert.o search.o files.o utils.o
543         cc -o edit main.o kbd.o command.o display.o \
544                    insert.o search.o files.o utils.o
546 main.o : main.c defs.h
547         cc -c main.c
548 kbd.o : kbd.c defs.h command.h
549         cc -c kbd.c
550 command.o : command.c defs.h command.h
551         cc -c command.c
552 display.o : display.c defs.h buffer.h
553         cc -c display.c
554 insert.o : insert.c defs.h buffer.h
555         cc -c insert.c
556 search.o : search.c defs.h buffer.h
557         cc -c search.c
558 files.o : files.c defs.h buffer.h command.h
559         cc -c files.c
560 utils.o : utils.c defs.h
561         cc -c utils.c
562 clean :
563         rm edit main.o kbd.o command.o display.o \
564            insert.o search.o files.o utils.o
565 @end group
566 @end example
568 @noindent
569 We split each long line into two lines using backslash-newline; this is
570 like using one long line, but is easier to read.
571 @cindex continuation lines
572 @cindex @code{\} (backslash), for continuation lines
573 @cindex backslash (@code{\}), for continuation lines
574 @cindex quoting newline, in makefile
575 @cindex newline, quoting, in makefile
577 To use this makefile to create the executable file called @file{edit},
578 type:
580 @example
581 make
582 @end example
584 To use this makefile to delete the executable file and all the object
585 files from the directory, type:
587 @example
588 make clean
589 @end example
591 In the example makefile, the targets include the executable file
592 @samp{edit}, and the object files @samp{main.o} and @samp{kbd.o}.  The
593 dependencies are files such as @samp{main.c} and @samp{defs.h}.
594 In fact, each @samp{.o} file is both a target and a dependency.
595 Commands include @w{@samp{cc -c main.c}} and @w{@samp{cc -c kbd.c}}.
597 When a target is a file, it needs to be recompiled or relinked if any
598 of its dependencies change.  In addition, any dependencies that are
599 themselves automatically generated should be updated first.  In this
600 example, @file{edit} depends on each of the eight object files; the
601 object file @file{main.o} depends on the source file @file{main.c} and
602 on the header file @file{defs.h}.
604 A shell command follows each line that contains a target and
605 dependencies.  These shell commands say how to update the target file.
606 A tab character must come at the beginning of every command line to
607 distinguish commands lines from other lines in the makefile.  (Bear in
608 mind that @code{make} does not know anything about how the commands
609 work.  It is up to you to supply commands that will update the target
610 file properly.  All @code{make} does is execute the commands in the rule
611 you have specified when the target file needs to be updated.)
612 @cindex shell command
614 The target @samp{clean} is not a file, but merely the name of an
615 action.  Since you
616 normally
617 do not want to carry out the actions in this rule, @samp{clean} is not a dependency of any other rule.
618 Consequently, @code{make} never does anything with it unless you tell
619 it specifically.  Note that this rule not only is not a dependency, it
620 also does not have any dependencies, so the only purpose of the rule
621 is to run the specified commands.  Targets that do not refer to files
622 but are just actions are called @dfn{phony targets}.  @xref{Phony
623 Targets}, for information about this kind of target.  @xref{Errors, ,
624 Errors in Commands}, to see how to cause @code{make} to ignore errors
625 from @code{rm} or any other command.
626 @cindex @code{clean} target
627 @cindex @code{rm} (shell command)
629 @node How Make Works, Variables Simplify, Simple Makefile, Introduction
630 @comment  node-name,  next,  previous,  up
631 @section How @code{make} Processes a Makefile
632 @cindex processing a makefile
633 @cindex makefile, how @code{make} processes
635 By default, @code{make} starts with the first target (not targets whose
636 names start with @samp{.}).  This is called the @dfn{default goal}.
637 (@dfn{Goals} are the targets that @code{make} strives ultimately to
638 update.  @xref{Goals, , Arguments to Specify the Goals}.)
639 @cindex default goal
640 @cindex goal, default
641 @cindex goal
643 In the simple example of the previous section, the default goal is to
644 update the executable program @file{edit}; therefore, we put that rule
645 first.
647 Thus, when you give the command:
649 @example
650 make
651 @end example
653 @noindent
654 @code{make} reads the makefile in the current directory and begins by
655 processing the first rule.  In the example, this rule is for relinking
656 @file{edit}; but before @code{make} can fully process this rule, it
657 must process the rules for the files that @file{edit} depends on,
658 which in this case are the object files.  Each of these files is
659 processed according to its own rule.  These rules say to update each
660 @samp{.o} file by compiling its source file.  The recompilation must
661 be done if the source file, or any of the header files named as
662 dependencies, is more recent than the object file, or if the object
663 file does not exist.
665 The other rules are processed because their targets appear as
666 dependencies of the goal.  If some other rule is not depended on by the
667 goal (or anything it depends on, etc.), that rule is not processed,
668 unless you tell @code{make} to do so (with a command such as
669 @w{@code{make clean}}).
671 Before recompiling an object file, @code{make} considers updating its
672 dependencies, the source file and header files.  This makefile does not
673 specify anything to be done for them---the @samp{.c} and @samp{.h} files
674 are not the targets of any rules---so @code{make} does nothing for these
675 files.  But @code{make} would update automatically generated C programs,
676 such as those made by Bison or Yacc, by their own rules at this time.
678 After recompiling whichever object files need it, @code{make} decides
679 whether to relink @file{edit}.  This must be done if the file
680 @file{edit} does not exist, or if any of the object files are newer than
681 it.  If an object file was just recompiled, it is now newer than
682 @file{edit}, so @file{edit} is relinked.
683 @cindex relinking
685 Thus, if we change the file @file{insert.c} and run @code{make},
686 @code{make} will compile that file to update @file{insert.o}, and then
687 link @file{edit}.  If we change the file @file{command.h} and run
688 @code{make}, @code{make} will recompile the object files @file{kbd.o},
689 @file{command.o} and @file{files.o} and then link the file @file{edit}.
691 @node Variables Simplify, make Deduces, How Make Works, Introduction
692 @section Variables Make Makefiles Simpler
693 @cindex variables
694 @cindex simplifying with variables
696 In our example, we had to list all the object files twice in the rule for
697 @file{edit} (repeated here):
699 @example
700 @group
701 edit : main.o kbd.o command.o display.o \
702               insert.o search.o files.o utils.o
703         cc -o edit main.o kbd.o command.o display.o \
704                    insert.o search.o files.o utils.o
705 @end group
706 @end example
708 @cindex @code{objects}
709 Such duplication is error-prone; if a new object file is added to the
710 system, we might add it to one list and forget the other.  We can eliminate
711 the risk and simplify the makefile by using a variable.  @dfn{Variables}
712 allow a text string to be defined once and substituted in multiple places
713 later (@pxref{Using Variables, ,How to Use Variables}).
715 @cindex @code{OBJECTS}
716 @cindex @code{objs}
717 @cindex @code{OBJS}
718 @cindex @code{obj}
719 @cindex @code{OBJ}
720 It is standard practice for every makefile to have a variable named
721 @code{objects}, @code{OBJECTS}, @code{objs}, @code{OBJS}, @code{obj},
722 or @code{OBJ} which is a list of all object file names.  We would
723 define such a variable @code{objects} with a line like this in the
724 makefile:@refill
726 @example
727 @group
728 objects = main.o kbd.o command.o display.o \
729           insert.o search.o files.o utils.o
730 @end group
731 @end example
733 @noindent
734 Then, each place we want to put a list of the object file names, we can
735 substitute the variable's value by writing @samp{$(objects)}
736 (@pxref{Using Variables, ,How to Use Variables}).
738 Here is how the complete simple makefile looks when you use a variable
739 for the object files:
741 @example
742 @group
743 objects = main.o kbd.o command.o display.o \
744           insert.o search.o files.o utils.o
746 edit : $(objects)
747         cc -o edit $(objects)
748 main.o : main.c defs.h
749         cc -c main.c
750 kbd.o : kbd.c defs.h command.h
751         cc -c kbd.c
752 command.o : command.c defs.h command.h
753         cc -c command.c
754 display.o : display.c defs.h buffer.h
755         cc -c display.c
756 insert.o : insert.c defs.h buffer.h
757         cc -c insert.c
758 search.o : search.c defs.h buffer.h
759         cc -c search.c
760 files.o : files.c defs.h buffer.h command.h
761         cc -c files.c
762 utils.o : utils.c defs.h
763         cc -c utils.c
764 clean :
765         rm edit $(objects)
766 @end group
767 @end example
769 @node make Deduces, Combine By Dependency, Variables Simplify, Introduction
770 @section Letting @code{make} Deduce the Commands
771 @cindex deducing commands (implicit rules)
772 @cindex implicit rule, introduction to
773 @cindex rule, implicit, introduction to
775 It is not necessary to spell out the commands for compiling the individual
776 C source files, because @code{make} can figure them out: it has an
777 @dfn{implicit rule} for updating a @samp{.o} file from a correspondingly
778 named @samp{.c} file using a @samp{cc -c} command.  For example, it will
779 use the command @samp{cc -c main.c -o main.o} to compile @file{main.c} into
780 @file{main.o}.  We can therefore omit the commands from the rules for the
781 object files.  @xref{Implicit Rules, ,Using Implicit Rules}.@refill
783 When a @samp{.c} file is used automatically in this way, it is also
784 automatically added to the list of dependencies.  We can therefore omit
785 the @samp{.c} files from the dependencies, provided we omit the commands.
787 Here is the entire example, with both of these changes, and a variable
788 @code{objects} as suggested above:
790 @example
791 @group
792 objects = main.o kbd.o command.o display.o \
793           insert.o search.o files.o utils.o
795 edit : $(objects)
796         cc -o edit $(objects)
798 main.o : defs.h
799 kbd.o : defs.h command.h
800 command.o : defs.h command.h
801 display.o : defs.h buffer.h
802 insert.o : defs.h buffer.h
803 search.o : defs.h buffer.h
804 files.o : defs.h buffer.h command.h
805 utils.o : defs.h
807 .PHONY : clean
808 clean :
809         -rm edit $(objects)
810 @end group
811 @end example
813 @noindent
814 This is how we would write the makefile in actual practice.  (The
815 complications associated with @samp{clean} are described elsewhere.
816 See @ref{Phony Targets}, and @ref{Errors, ,Errors in Commands}.)
818 Because implicit rules are so convenient, they are important.  You
819 will see them used frequently.@refill
821 @node Combine By Dependency, Cleanup, make Deduces, Introduction
822 @section Another Style of Makefile
823 @cindex combining rules by dependency
825 When the objects of a makefile are created only by implicit rules, an
826 alternative style of makefile is possible.  In this style of makefile,
827 you group entries by their dependencies instead of by their targets.
828 Here is what one looks like:
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 $(objects) : defs.h
839 kbd.o command.o files.o : command.h
840 display.o insert.o search.o files.o : buffer.h
841 @end group
842 @end example
844 @noindent
845 Here @file{defs.h} is given as a dependency of all the object files;
846 @file{command.h} and @file{buffer.h} are dependencies of the specific
847 object files listed for them.
849 Whether this is better is a matter of taste: it is more compact, but some
850 people dislike it because they find it clearer to put all the information
851 about each target in one place.
853 @node Cleanup,  , Combine By Dependency, Introduction
854 @section Rules for Cleaning the Directory
855 @cindex cleaning up
856 @cindex removing, to clean up
858 Compiling a program is not the only thing you might want to write rules
859 for.  Makefiles commonly tell how to do a few other things besides
860 compiling a program: for example, how to delete all the object files
861 and executables so that the directory is @samp{clean}.
863 @cindex @code{clean} target
864 Here is how we
865 could write a @code{make} rule for cleaning our example editor:
867 @example
868 @group
869 clean:
870         rm edit $(objects)
871 @end group
872 @end example
874 In practice, we might want to write the rule in a somewhat more
875 complicated manner to handle unanticipated situations.  We would do this:
877 @example
878 @group
879 .PHONY : clean
880 clean :
881         -rm edit $(objects)
882 @end group
883 @end example
885 @noindent
886 This prevents @code{make} from getting confused by an actual file
887 called @file{clean} and causes it to continue in spite of errors from
888 @code{rm}.  (See @ref{Phony Targets}, and @ref{Errors, ,Errors in
889 Commands}.)
891 @noindent
892 A rule such as this should not be placed at the beginning of the
893 makefile, because we do not want it to run by default!  Thus, in the
894 example makefile, we want the rule for @code{edit}, which recompiles
895 the editor, to remain the default goal.
897 Since @code{clean} is not a dependency of @code{edit}, this rule will not
898 run at all if we give the command @samp{make} with no arguments.  In
899 order to make the rule run, we have to type @samp{make clean}.
900 @xref{Running, ,How to Run @code{make}}.
902 @node Makefiles, Rules, Introduction, Top
903 @chapter Writing Makefiles
905 @cindex makefile, how to write
906 The information that tells @code{make} how to recompile a system comes from
907 reading a data base called the @dfn{makefile}.
909 @menu
910 * Makefile Contents::           What makefiles contain.
911 * Makefile Names::              How to name your makefile.
912 * Include::                     How one makefile can use another makefile.
913 * MAKEFILES Variable::          The environment can specify extra makefiles.
914 * Remaking Makefiles::          How makefiles get remade.
915 * Overriding Makefiles::        How to override part of one makefile
916                                  with another makefile.
917 @end menu
919 @node Makefile Contents, Makefile Names,  , Makefiles
920 @section What Makefiles Contain
922 Makefiles contain five kinds of things: @dfn{explicit rules},
923 @dfn{implicit rules}, @dfn{variable definitions}, @dfn{directives},
924 and @dfn{comments}.  Rules, variables, and directives are described at
925 length in later chapters.@refill
927 @itemize @bullet
928 @cindex rule, explicit, definition of
929 @cindex explicit rule, definition of
930 @item
931 An @dfn{explicit rule} says when and how to remake one or more files,
932 called the rule's targets.  It lists the other files that the targets
933 @dfn{depend on}, and may also give commands to use to create or update
934 the targets.  @xref{Rules, ,Writing Rules}.
936 @cindex rule, implicit, definition of
937 @cindex implicit rule, definition of
938 @item
939 An @dfn{implicit rule} says when and how to remake a class of files
940 based on their names.  It describes how a target may depend on a file
941 with a name similar to the target and gives commands to create or
942 update such a target.  @xref{Implicit Rules, ,Using Implicit Rules}.
944 @cindex variable definition
945 @item
946 A @dfn{variable definition} is a line that specifies a text string
947 value for a variable that can be substituted into the text later.  The
948 simple makefile example shows a variable definition for @code{objects}
949 as a list of all object files (@pxref{Variables Simplify, , Variables
950 Make Makefiles Simpler}).
952 @cindex directive
953 @item
954 A @dfn{directive} is a command for @code{make} to do something special while
955 reading the makefile.  These include:
957 @itemize @bullet
958 @item
959 Reading another makefile (@pxref{Include, ,Including Other Makefiles}).
961 @item
962 Deciding (based on the values of variables) whether to use or
963 ignore a part of the makefile (@pxref{Conditionals, ,Conditional Parts of Makefiles}).
965 @item
966 Defining a variable from a verbatim string containing multiple lines
967 (@pxref{Defining, ,Defining Variables Verbatim}).
968 @end itemize
970 @cindex comments, in makefile
971 @cindex @code{#} (comments), in makefile
972 @item
973 @samp{#} in a line of a makefile starts a @dfn{comment}.  It and the rest of
974 the line are ignored, except that a trailing backslash not escaped by
975 another backslash will continue the comment across multiple lines.
976 Comments may appear on any of the lines in the makefile, except within a
977 @code{define} directive, and perhaps within commands (where the shell
978 decides what is a comment).  A line containing just a comment (with
979 perhaps spaces before it) is effectively blank, and is ignored.@refill
980 @end itemize
982 @node Makefile Names, Include, Makefile Contents, Makefiles
983 @section What Name to Give Your Makefile
984 @cindex makefile name
985 @cindex name of makefile
986 @cindex default makefile name
987 @cindex file name of makefile
989 @c following paragraph rewritten to avoid overfull hbox
990 By default, when @code{make} looks for the makefile, it tries the
991 following names, in order: @file{GNUmakefile}, @file{makefile}
992 and @file{Makefile}.@refill
993 @findex Makefile
994 @findex GNUmakefile
995 @findex makefile
997 @cindex @code{README}
998 Normally you should call your makefile either @file{makefile} or
999 @file{Makefile}.  (We recommend @file{Makefile} because it appears
1000 prominently near the beginning of a directory listing, right near other
1001 important files such as @file{README}.)  The first name checked,
1002 @file{GNUmakefile}, is not recommended for most makefiles.  You should
1003 use this name if you have a makefile that is specific to GNU
1004 @code{make}, and will not be understood by other versions of
1005 @code{make}.  Other @code{make} programs look for @file{makefile} and
1006 @file{Makefile}, but not @file{GNUmakefile}.
1008 If @code{make} finds none of these names, it does not use any makefile.
1009 Then you must specify a goal with a command argument, and @code{make}
1010 will attempt to figure out how to remake it using only its built-in
1011 implicit rules.  @xref{Implicit Rules, ,Using Implicit Rules}.
1013 @cindex @code{-f}
1014 @cindex @code{--file}
1015 @cindex @code{--makefile}
1016 If you want to use a nonstandard name for your makefile, you can specify
1017 the makefile name with the @samp{-f} or @samp{--file} option.  The
1018 arguments @w{@samp{-f @var{name}}} or @w{@samp{--file=@var{name}}} tell
1019 @code{make} to read the file @var{name} as the makefile.  If you use
1020 more than one @samp{-f} or @samp{--file} option, you can specify several
1021 makefiles.  All the makefiles are effectively concatenated in the order
1022 specified.  The default makefile names @file{GNUmakefile},
1023 @file{makefile} and @file{Makefile} are not checked automatically if you
1024 specify @samp{-f} or @samp{--file}.@refill
1025 @cindex specifying makefile name
1026 @cindex makefile name, how to specify
1027 @cindex name of makefile, how to specify
1028 @cindex file name of makefile, how to specify
1030 @node Include, MAKEFILES Variable, Makefile Names, Makefiles
1031 @section Including Other Makefiles
1032 @cindex including other makefiles
1033 @cindex makefile, including
1035 @findex include
1036 The @code{include} directive tells @code{make} to suspend reading the
1037 current makefile and read one or more other makefiles before continuing.
1038 The directive is a line in the makefile that looks like this:
1040 @example
1041 include @var{filenames}@dots{}
1042 @end example
1044 @noindent
1045 @var{filenames} can contain shell file name patterns.
1046 @cindex shell file name pattern (in @code{include})
1047 @cindex shell wildcards (in @code{include})
1048 @cindex wildcard, in @code{include}
1050 Extra spaces are allowed and ignored at the beginning of the line, but
1051 a tab is not allowed.  (If the line begins with a tab, it will be
1052 considered a command line.)  Whitespace is required between
1053 @code{include} and the file names, and between file names; extra
1054 whitespace is ignored there and at the end of the directive.  A
1055 comment starting with @samp{#} is allowed at the end of the line.  If
1056 the file names contain any variable or function references, they are
1057 expanded.  @xref{Using Variables, ,How to Use Variables}.
1059 For example, if you have three @file{.mk} files, @file{a.mk},
1060 @file{b.mk}, and @file{c.mk}, and @code{$(bar)} expands to
1061 @code{bish bash}, then the following expression
1063 @example
1064 include foo *.mk $(bar)
1065 @end example
1067 is equivalent to
1069 @example
1070 include foo a.mk b.mk c.mk bish bash
1071 @end example
1073 When @code{make} processes an @code{include} directive, it suspends
1074 reading of the containing makefile and reads from each listed file in
1075 turn.  When that is finished, @code{make} resumes reading the
1076 makefile in which the directive appears.
1078 One occasion for using @code{include} directives is when several programs,
1079 handled by individual makefiles in various directories, need to use a
1080 common set of variable definitions
1081 (@pxref{Setting, ,Setting Variables}) or pattern rules
1082 (@pxref{Pattern Rules, ,Defining and Redefining Pattern Rules}).
1084 Another such occasion is when you want to generate dependencies from
1085 source files automatically; the dependencies can be put in a file that
1086 is included by the main makefile.  This practice is generally cleaner
1087 than that of somehow appending the dependencies to the end of the main
1088 makefile as has been traditionally done with other versions of
1089 @code{make}.  @xref{Automatic Dependencies}.
1090 @cindex dependencies, automatic generation
1091 @cindex automatic generation of dependencies
1092 @cindex generating dependencies automatically
1094 @cindex @code{-I}
1095 @cindex @code{--include-dir}
1096 @findex /usr/gnu/include
1097 @findex /usr/local/include
1098 @findex /usr/include
1099 If the specified name does not start with a slash, and the file is not
1100 found in the current directory, several other directories are searched.
1101 First, any directories you have specified with the @samp{-I} or
1102 @samp{--include-dir} option are searched
1103 (@pxref{Options Summary, ,Summary of Options}).
1104 Then the following directories (if they exist)
1105 are searched, in this order:
1106 @file{@var{prefix}/include} (normally @file{/usr/local/include})
1107 @file{/usr/gnu/include},
1108 @file{/usr/local/include}, @file{/usr/include}.
1110 If an included makefile cannot be found in any of these directories, a
1111 warning message is generated, but it is not an immediately fatal error;
1112 processing of the makefile containing the @code{include} continues.
1113 Once it has finished reading makefiles, @code{make} will try to remake
1114 any that are out of date or don't exist.
1115 @xref{Remaking Makefiles, ,How Makefiles Are Remade}.
1116 Only after it has tried to find a way to remake a makefile and failed,
1117 will @code{make} diagnose the missing makefile as a fatal error.
1119 If you want @code{make} to simply ignore a makefile which does not exist
1120 and cannot be remade, with no error message, use the @w{@code{-include}}
1121 directive instead of @code{include}, like this:
1123 @example
1124 -include @var{filenames}@dots{}
1125 @end example
1127 This is acts like @code{include} in every way except that there is no
1128 error (not even a warning) if any of the @var{filenames} do not exist.
1130 @node MAKEFILES Variable, Remaking Makefiles, Include, Makefiles
1131 @section The Variable @code{MAKEFILES}
1132 @cindex makefile, and @code{MAKEFILES} variable
1133 @cindex including (@code{MAKEFILES} variable)
1135 @vindex MAKEFILES
1136 If the environment variable @code{MAKEFILES} is defined, @code{make}
1137 considers its value as a list of names (separated by whitespace) of
1138 additional makefiles to be read before the others.  This works much like
1139 the @code{include} directive: various directories are searched for those
1140 files (@pxref{Include, ,Including Other Makefiles}).  In addition, the
1141 default goal is never taken from one of these makefiles and it is not an
1142 error if the files listed in @code{MAKEFILES} are not found.@refill
1144 @cindex recursion, and @code{MAKEFILES} variable
1145 The main use of @code{MAKEFILES} is in communication between recursive
1146 invocations of @code{make} (@pxref{Recursion, ,Recursive Use of
1147 @code{make}}).  It usually is not desirable to set the environment
1148 variable before a top-level invocation of @code{make}, because it is
1149 usually better not to mess with a makefile from outside.  However, if
1150 you are running @code{make} without a specific makefile, a makefile in
1151 @code{MAKEFILES} can do useful things to help the built-in implicit
1152 rules work better, such as defining search paths (@pxref{Directory Search}).
1154 Some users are tempted to set @code{MAKEFILES} in the environment
1155 automatically on login, and program makefiles to expect this to be done.
1156 This is a very bad idea, because such makefiles will fail to work if run by
1157 anyone else.  It is much better to write explicit @code{include} directives
1158 in the makefiles.  @xref{Include, , Including Other Makefiles}.
1160 @node Remaking Makefiles, Overriding Makefiles, MAKEFILES Variable, Makefiles
1161 @section How Makefiles Are Remade
1163 @cindex updating makefiles
1164 @cindex remaking makefiles
1165 @cindex makefile, remaking of
1166 Sometimes makefiles can be remade from other files, such as RCS or SCCS
1167 files.  If a makefile can be remade from other files, you probably want
1168 @code{make} to get an up-to-date version of the makefile to read in.
1170 To this end, after reading in all makefiles, @code{make} will consider
1171 each as a goal target and attempt to update it.  If a makefile has a
1172 rule which says how to update it (found either in that very makefile or
1173 in another one) or if an implicit rule applies to it (@pxref{Implicit
1174 Rules, ,Using Implicit Rules}), it will be updated if necessary.  After
1175 all makefiles have been checked, if any have actually been changed,
1176 @code{make} starts with a clean slate and reads all the makefiles over
1177 again.  (It will also attempt to update each of them over again, but
1178 normally this will not change them again, since they are already up to
1179 date.)@refill
1181 If the makefiles specify a double-colon rule to remake a file with
1182 commands but no dependencies, that file will always be remade
1183 (@pxref{Double-Colon}).  In the case of makefiles, a makefile that has a
1184 double-colon rule with commands but no dependencies will be remade every
1185 time @code{make} is run, and then again after @code{make} starts over
1186 and reads the makefiles in again.  This would cause an infinite loop:
1187 @code{make} would constantly remake the makefile, and never do anything
1188 else.  So, to avoid this, @code{make} will @strong{not} attempt to
1189 remake makefiles which are specified as double-colon targets but have no
1190 dependencies.@refill
1192 If you do not specify any makefiles to be read with @samp{-f} or
1193 @samp{--file} options, @code{make} will try the default makefile names;
1194 @pxref{Makefile Names, ,What Name to Give Your Makefile}.  Unlike
1195 makefiles explicitly requested with @samp{-f} or @samp{--file} options,
1196 @code{make} is not certain that these makefiles should exist.  However,
1197 if a default makefile does not exist but can be created by running
1198 @code{make} rules, you probably want the rules to be run so that the
1199 makefile can be used.
1201 Therefore, if none of the default makefiles exists, @code{make} will try
1202 to make each of them in the same order in which they are searched for
1203 (@pxref{Makefile Names, ,What Name to Give Your Makefile})
1204 until it succeeds in making one, or it runs out of names to try.  Note
1205 that it is not an error if @code{make} cannot find or make any makefile;
1206 a makefile is not always necessary.@refill
1208 When you use the @samp{-t} or @samp{--touch} option
1209 (@pxref{Instead of Execution, ,Instead of Executing the Commands}),
1210 you would not want to use an out-of-date makefile to decide which
1211 targets to touch.  So the @samp{-t} option has no effect on updating
1212 makefiles; they are really updated even if @samp{-t} is specified.
1213 Likewise, @samp{-q} (or @samp{--question}) and @samp{-n} (or
1214 @samp{--just-print}) do not prevent updating of makefiles, because an
1215 out-of-date makefile would result in the wrong output for other targets.
1216 Thus, @samp{make -f mfile -n foo} will update @file{mfile}, read it in,
1217 and then print the commands to update @file{foo} and its dependencies
1218 without running them.  The commands printed for @file{foo} will be those
1219 specified in the updated contents of @file{mfile}.
1221 However, on occasion you might actually wish to prevent updating of even
1222 the makefiles.  You can do this by specifying the makefiles as goals in
1223 the command line as well as specifying them as makefiles.  When the
1224 makefile name is specified explicitly as a goal, the options @samp{-t}
1225 and so on do apply to them.
1227 Thus, @samp{make -f mfile -n mfile foo} would read the makefile
1228 @file{mfile}, print the commands needed to update it without actually
1229 running them, and then print the commands needed to update @file{foo}
1230 without running them.  The commands for @file{foo} will be those
1231 specified by the existing contents of @file{mfile}.
1233 @node Overriding Makefiles,  , Remaking Makefiles, Makefiles
1234 @section Overriding Part of Another Makefile
1236 @cindex overriding makefiles
1237 @cindex makefile, overriding
1238 Sometimes it is useful to have a makefile that is mostly just like
1239 another makefile.  You can often use the @samp{include} directive to
1240 include one in the other, and add more targets or variable definitions.
1241 However, if the two makefiles give different commands for the same
1242 target, @code{make} will not let you just do this.  But there is another way.
1244 @cindex match-anything rule, used to override
1245 In the containing makefile (the one that wants to include the other),
1246 you can use a match-anything pattern rule to say that to remake any
1247 target that cannot be made from the information in the containing
1248 makefile, @code{make} should look in another makefile.
1249 @xref{Pattern Rules}, for more information on pattern rules.
1251 For example, if you have a makefile called @file{Makefile} that says how
1252 to make the target @samp{foo} (and other targets), you can write a
1253 makefile called @file{GNUmakefile} that contains:
1255 @example
1256 foo:
1257         frobnicate > foo
1259 %: force
1260         @@$(MAKE) -f Makefile $@@
1261 force: ;
1262 @end example
1264 If you say @samp{make foo}, @code{make} will find @file{GNUmakefile},
1265 read it, and see that to make @file{foo}, it needs to run the command
1266 @samp{frobnicate > foo}.  If you say @samp{make bar}, @code{make} will
1267 find no way to make @file{bar} in @file{GNUmakefile}, so it will use the
1268 commands from the pattern rule: @samp{make -f Makefile bar}.  If
1269 @file{Makefile} provides a rule for updating @file{bar}, @code{make}
1270 will apply the rule.  And likewise for any other target that
1271 @file{GNUmakefile} does not say how to make.
1273 The way this works is that the pattern rule has a pattern of just
1274 @samp{%}, so it matches any target whatever.  The rule specifies a
1275 dependency @file{force}, to guarantee that the commands will be run even
1276 if the target file already exists.  We give @file{force} target empty
1277 commands to prevent @code{make} from searching for an implicit rule to
1278 build it---otherwise it would apply the same match-anything rule to
1279 @file{force} itself and create a dependency loop!
1281 @node Rules, Commands, Makefiles, Top
1282 @chapter Writing Rules
1283 @cindex writing rules
1284 @cindex rule, how to write
1285 @cindex target
1286 @cindex dependency
1288 A @dfn{rule} appears in the makefile and says when and how to remake
1289 certain files, called the rule's @dfn{targets} (most often only one per rule).
1290 It lists the other files that are the @dfn{dependencies} of the target, and
1291 @dfn{commands} to use to create or update the target.
1293 @cindex default goal
1294 @cindex goal, default
1295 The order of rules is not significant, except for determining the
1296 @dfn{default goal}: the target for @code{make} to consider, if you do
1297 not otherwise specify one.  The default goal is the target of the first
1298 rule in the first makefile.  If the first rule has multiple targets,
1299 only the first target is taken as the default.  There are two
1300 exceptions: a target starting with a period is not a default unless it
1301 contains one or more slashes, @samp{/}, as well; and, a target that
1302 defines a pattern rule has no effect on the default goal.
1303 (@xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.)
1305 Therefore, we usually write the makefile so that the first rule is the
1306 one for compiling the entire program or all the programs described by
1307 the makefile (often with a target called @samp{all}).
1308 @xref{Goals, ,Arguments to Specify the Goals}.
1310 @menu
1311 * Rule Example::                An example explained.
1312 * Rule Syntax::                 General syntax explained.
1313 * Wildcards::                   Using wildcard characters such as `*'.
1314 * Directory Search::            Searching other directories for source files.
1315 * Phony Targets::               Using a target that is not a real file's name.
1316 * Force Targets::               You can use a target without commands
1317                                   or dependencies to mark other
1318                                   targets as phony.
1319 * Empty Targets::               When only the date matters and the
1320                                   files are empty.
1321 * Special Targets::             Targets with special built-in meanings.
1322 * Multiple Targets::            When to make use of several targets in a rule.
1323 * Multiple Rules::              How to use several rules with the same target.
1324 * Static Pattern::              Static pattern rules apply to multiple targets
1325                                   and can vary the dependencies according to
1326                                   the target name.
1327 * Double-Colon::                How to use a special kind of rule to allow
1328                                   several independent rules for one target.
1329 * Automatic Dependencies::      How to automatically generate rules giving
1330                                  dependencies from the source files themselves.
1331 @end menu
1333 @ifinfo
1334 @node Rule Example, Rule Syntax,  , Rules
1335 @section Rule Example
1337 Here is an example of a rule:
1339 @example
1340 foo.o : foo.c defs.h       # module for twiddling the frobs
1341         cc -c -g foo.c
1342 @end example
1344 Its target is @file{foo.o} and its dependencies are @file{foo.c} and
1345 @file{defs.h}.  It has one command, which is @samp{cc -c -g foo.c}.
1346 The command line starts with a tab to identify it as a command.
1348 This rule says two things:
1350 @itemize @bullet
1351 @item
1352 How to decide whether @file{foo.o} is out of date: it is out of date
1353 if it does not exist, or if either @file{foo.c} or @file{defs.h} is
1354 more recent than it.
1356 @item
1357 How to update the file @file{foo.o}: by running @code{cc} as stated.
1358 The command does not explicitly mention @file{defs.h}, but we presume
1359 that @file{foo.c} includes it, and that that is why @file{defs.h} was
1360 added to the dependencies.
1361 @end itemize
1362 @end ifinfo
1364 @node Rule Syntax, Wildcards, Rule Example, Rules
1365 @section Rule Syntax
1367 @cindex rule syntax
1368 @cindex syntax of rules
1369 In general, a rule looks like this:
1371 @example
1372 @var{targets} : @var{dependencies}
1373         @var{command}
1374         @dots{}
1375 @end example
1377 @noindent
1378 or like this:
1380 @example
1381 @var{targets} : @var{dependencies} ; @var{command}
1382         @var{command}
1383         @dots{}
1384 @end example
1386 @cindex targets
1387 @cindex rule targets
1388 The @var{targets} are file names, separated by spaces.  Wildcard
1389 characters may be used (@pxref{Wildcards, ,Using Wildcard Characters
1390 in File Names}) and a name of the form @file{@var{a}(@var{m})}
1391 represents member @var{m} in archive file @var{a}
1392 (@pxref{Archive Members, ,Archive Members as Targets}).
1393 Usually there is only one
1394 target per rule, but occasionally there is a reason to have more
1395 (@pxref{Multiple Targets, , Multiple Targets in a Rule}).@refill
1397 @cindex commands
1398 @cindex tab character (in commands)
1399 The @var{command} lines start with a tab character.  The first command may
1400 appear on the line after the dependencies, with a tab character, or may
1401 appear on the same line, with a semicolon.  Either way, the effect is the
1402 same.  @xref{Commands, ,Writing the Commands in Rules}.
1404 @cindex dollar sign (@code{$}), in rules
1405 @cindex @code{$}, in rules
1406 @cindex rule, and @code{$}
1407 Because dollar signs are used to start variable references, if you really
1408 want a dollar sign in a rule you must write two of them, @samp{$$}
1409 (@pxref{Using Variables, ,How to Use Variables}).
1410 You may split a long line by inserting a backslash
1411 followed by a newline, but this is not required, as @code{make} places no
1412 limit on the length of a line in a makefile.
1414 A rule tells @code{make} two things: when the targets are out of date,
1415 and how to update them when necessary.
1417 @cindex dependencies
1418 @cindex rule dependencies
1419 The criterion for being out of date is specified in terms of the
1420 @var{dependencies}, which consist of file names separated by spaces.
1421 (Wildcards and archive members (@pxref{Archives}) are allowed here too.)
1422 A target is out of date if it does not exist or if it is older than any
1423 of the dependencies (by comparison of last-modification times).  The
1424 idea is that the contents of the target file are computed based on
1425 information in the dependencies, so if any of the dependencies changes,
1426 the contents of the existing target file are no longer necessarily
1427 valid.
1429 How to update is specified by @var{commands}.  These are lines to be
1430 executed by the shell (normally @samp{sh}), but with some extra features
1431 (@pxref{Commands, ,Writing the Commands in Rules}).
1433 @node Wildcards, Directory Search, Rule Syntax, Rules
1434 @section Using Wildcard Characters in File Names
1435 @cindex wildcard
1436 @cindex file name with wildcards
1437 @cindex globbing (wildcards)
1439 @cindex @code{*} (wildcard character)
1440 @cindex @code{?} (wildcard character)
1441 @cindex @code{[@dots{}]} (wildcard characters)
1442 A single file name can specify many files using @dfn{wildcard characters}.
1443 The wildcard characters in @code{make} are @samp{*}, @samp{?} and
1444 @samp{[@dots{}]}, the same as in the Bourne shell.  For example, @file{*.c}
1445 specifies a list of all the files (in the working directory) whose names
1446 end in @samp{.c}.@refill
1448 @cindex @code{~} (tilde)
1449 @cindex tilde (@code{~})
1450 @cindex home directory
1451 The character @samp{~} at the beginning of a file name also has special
1452 significance.  If alone, or followed by a slash, it represents your home
1453 directory.  For example @file{~/bin} expands to @file{/home/you/bin}.
1454 If the @samp{~} is followed by a word, the string represents the home
1455 directory of the user named by that word.  For example @file{~john/bin}
1456 expands to @file{/home/john/bin}.@refill
1458 Wildcard expansion happens automatically in targets, in dependencies,
1459 and in commands (where the shell does the expansion).  In other
1460 contexts, wildcard expansion happens only if you request it explicitly
1461 with the @code{wildcard} function.
1463 The special significance of a wildcard character can be turned off by
1464 preceding it with a backslash.  Thus, @file{foo\*bar} would refer to a
1465 specific file whose name consists of @samp{foo}, an asterisk, and
1466 @samp{bar}.@refill
1468 @menu
1469 * Wildcard Examples::           Several examples
1470 * Wildcard Pitfall::            Problems to avoid.
1471 * Wildcard Function::           How to cause wildcard expansion where
1472                                   it does not normally take place.
1473 @end menu
1475 @node Wildcard Examples, Wildcard Pitfall,  , Wildcards
1476 @subsection Wildcard Examples
1478 Wildcards can be used in the commands of a rule, where they are expanded
1479 by the shell.  For example, here is a rule to delete all the object files:
1481 @example
1482 @group
1483 clean:
1484         rm -f *.o
1485 @end group
1486 @end example
1487 @cindex @code{rm} (shell command)
1489 Wildcards are also useful in the dependencies of a rule.  With the
1490 following rule in the makefile, @samp{make print} will print all the
1491 @samp{.c} files that have changed since the last time you printed them:
1493 @example
1494 print: *.c
1495         lpr -p $?
1496         touch print
1497 @end example
1499 @cindex @code{print} target
1500 @cindex @code{lpr} (shell command)
1501 @cindex @code{touch} (shell command)
1502 @noindent
1503 This rule uses @file{print} as an empty target file; see @ref{Empty
1504 Targets, ,Empty Target Files to Record Events}.  (The automatic variable
1505 @samp{$?} is used to print only those files that have changed; see
1506 @ref{Automatic, ,Automatic Variables}.)@refill
1508 Wildcard expansion does not happen when you define a variable.  Thus, if
1509 you write this:
1511 @example
1512 objects = *.o
1513 @end example
1515 @noindent
1516 then the value of the variable @code{objects} is the actual string
1517 @samp{*.o}.  However, if you use the value of @code{objects} in a target,
1518 dependency or command, wildcard expansion will take place at that time.
1519 To set @code{objects} to the expansion, instead use:
1521 @example
1522 objects := $(wildcard *.o)
1523 @end example
1525 @noindent
1526 @xref{Wildcard Function}.
1528 @node Wildcard Pitfall, Wildcard Function, Wildcard Examples, Wildcards
1529 @subsection Pitfalls of Using Wildcards
1530 @cindex wildcard pitfalls
1531 @cindex pitfalls of wildcards
1532 @cindex mistakes with wildcards
1533 @cindex errors with wildcards
1534 @cindex problems with wildcards
1536 Now here is an example of a naive way of using wildcard expansion, that
1537 does not do what you would intend.  Suppose you would like to say that the
1538 executable file @file{foo} is made from all the object files in the
1539 directory, and you write this:
1541 @example
1542 objects = *.o
1544 foo : $(objects)
1545         cc -o foo $(CFLAGS) $(objects)
1546 @end example
1548 @noindent
1549 The value of @code{objects} is the actual string @samp{*.o}.  Wildcard
1550 expansion happens in the rule for @file{foo}, so that each @emph{existing}
1551 @samp{.o} file becomes a dependency of @file{foo} and will be recompiled if
1552 necessary.
1554 But what if you delete all the @samp{.o} files?  When a wildcard matches
1555 no files, it is left as it is, so then @file{foo} will depend on the
1556 oddly-named file @file{*.o}.  Since no such file is likely to exist,
1557 @code{make} will give you an error saying it cannot figure out how to
1558 make @file{*.o}.  This is not what you want!
1560 Actually it is possible to obtain the desired result with wildcard
1561 expansion, but you need more sophisticated techniques, including the
1562 @code{wildcard} function and string substitution.
1563 @ifinfo
1564 @xref{Wildcard Function, ,The Function @code{wildcard}}.
1565 @end ifinfo
1566 @iftex
1567 These are described in the following section.
1568 @end iftex
1570 @node Wildcard Function,  , Wildcard Pitfall, Wildcards
1571 @subsection The Function @code{wildcard}
1572 @findex wildcard
1574 Wildcard expansion happens automatically in rules.  But wildcard expansion
1575 does not normally take place when a variable is set, or inside the
1576 arguments of a function.  If you want to do wildcard expansion in such
1577 places, you need to use the @code{wildcard} function, like this:
1579 @example
1580 $(wildcard @var{pattern}@dots{})
1581 @end example
1583 @noindent
1584 This string, used anywhere in a makefile, is replaced by a
1585 space-separated list of names of existing files that match one of the
1586 given file name patterns.  If no existing file name matches a pattern,
1587 then that pattern is omitted from the output of the @code{wildcard}
1588 function.  Note that this is different from how unmatched wildcards
1589 behave in rules, where they are used verbatim rather than ignored
1590 (@pxref{Wildcard Pitfall}).
1592 One use of the @code{wildcard} function is to get a list of all the C source
1593 files in a directory, like this:
1595 @example
1596 $(wildcard *.c)
1597 @end example
1599 We can change the list of C source files into a list of object files by
1600 replacing the @samp{.o} suffix with @samp{.c} in the result, like this:
1602 @example
1603 $(patsubst %.c,%.o,$(wildcard *.c))
1604 @end example
1606 @noindent
1607 (Here we have used another function, @code{patsubst}.
1608 @xref{Text Functions, ,Functions for String Substitution and Analysis}.)@refill
1610 Thus, a makefile to compile all C source files in the directory and then
1611 link them together could be written as follows:
1613 @example
1614 objects := $(patsubst %.c,%.o,$(wildcard *.c))
1616 foo : $(objects)
1617         cc -o foo $(objects)
1618 @end example
1620 @noindent
1621 (This takes advantage of the implicit rule for compiling C programs, so
1622 there is no need to write explicit rules for compiling the files.
1623 @xref{Flavors, ,The Two Flavors of Variables}, for an explanation of
1624 @samp{:=}, which is a variant of @samp{=}.)
1626 @node Directory Search, Phony Targets, Wildcards, Rules
1627 @section Searching Directories for Dependencies
1628 @vindex VPATH
1629 @findex vpath
1630 @cindex vpath
1631 @cindex search path for dependencies (@code{VPATH})
1632 @cindex directory search (@code{VPATH})
1634 For large systems, it is often desirable to put sources in a separate
1635 directory from the binaries.  The @dfn{directory search} features of
1636 @code{make} facilitate this by searching several directories
1637 automatically to find a dependency.  When you redistribute the files
1638 among directories, you do not need to change the individual rules,
1639 just the search paths.
1641 @menu
1642 * General Search::              Specifying a search path that applies
1643                                   to every dependency.
1644 * Selective Search::            Specifying a search path
1645                                   for a specified class of names.
1646 * Commands/Search::             How to write shell commands that work together
1647                                   with search paths.
1648 * Implicit/Search::             How search paths affect implicit rules.
1649 * Libraries/Search::            Directory search for link libraries.
1650 @end menu
1652 @node General Search, Selective Search,  , Directory Search
1653 @subsection @code{VPATH}: Search Path for All Dependencies
1654 @vindex VPATH
1656 The value of the @code{make} variable @code{VPATH} specifies a list of
1657 directories that @code{make} should search.  Most often, the
1658 directories are expected to contain dependency files that are not in the
1659 current directory; however, @code{VPATH} specifies a search list that
1660 @code{make} applies for all files, including files which are targets of
1661 rules.
1663 Thus, if a file that is listed as a target or dependency does not exist
1664 in the current directory, @code{make} searches the directories listed in
1665 @code{VPATH} for a file with that name.  If a file is found in one of
1666 them, that file becomes the dependency.  Rules may then specify the
1667 names of source files in the dependencies as if they all existed in the
1668 current directory.  @xref{Commands/Search, ,Writing Shell Commands with
1669 Directory Search}.
1671 In the @code{VPATH} variable, directory names are separated by colons or
1672 blanks.  The order in which directories are listed is the order followed
1673 by @code{make} in its search.
1675 For example,
1677 @example
1678 VPATH = src:../headers
1679 @end example
1681 @noindent
1682 specifies a path containing two directories, @file{src} and
1683 @file{../headers}, which @code{make} searches in that order.
1685 With this value of @code{VPATH}, the following rule,
1687 @example
1688 foo.o : foo.c
1689 @end example
1691 @noindent
1692 is interpreted as if it were written like this:
1694 @example
1695 foo.o : src/foo.c
1696 @end example
1698 @noindent
1699 assuming the file @file{foo.c} does not exist in the current directory but
1700 is found in the directory @file{src}.
1702 @node Selective Search, Commands/Search, General Search, Directory Search
1703 @subsection The @code{vpath} Directive
1704 @findex vpath
1706 Similar to the @code{VPATH} variable but more selective is the @code{vpath}
1707 directive (note lower case), which allows you to specify a search path for a particular class
1708 of file names, those that match a particular pattern.  Thus you can supply
1709 certain search directories for one class of file names and other directories
1710 (or none) for other file names.
1712 There are three forms of the @code{vpath} directive:
1714 @table @code
1715 @item vpath @var{pattern} @var{directories}
1716 Specify the search path @var{directories} for file names that match
1717 @var{pattern}.
1719 The search path, @var{directories}, is a list of directories to be
1720 searched, separated by colons or blanks, just like the search path used
1721 in the @code{VPATH} variable.
1723 @item vpath @var{pattern}
1724 Clear out the search path associated with @var{pattern}.
1726 @c Extra blank line makes sure this gets two lines.
1727 @item vpath
1729 Clear all search paths previously specified with @code{vpath} directives.
1730 @end table
1732 A @code{vpath} pattern is a string containing a @samp{%} character.  The
1733 string must match the file name of a dependency that is being searched
1734 for, the @samp{%} character matching any sequence of zero or more
1735 characters (as in pattern rules; @pxref{Pattern Rules, ,Defining and
1736 Redefining Pattern Rules}).  For example, @code{%.h} matches files that
1737 end in @code{.h}.  (If there is no @samp{%}, the pattern must match the
1738 dependency exactly, which is not useful very often.)
1740 @cindex @code{%}, quoting in @code{vpath}
1741 @cindex @code{%}, quoting with @code{\} (backslash)
1742 @cindex @code{\} (backslash), to quote @code{%}
1743 @cindex backslash (@code{\}), to quote @code{%}
1744 @cindex quoting @code{%}, in @code{vpath}
1745 @samp{%} characters in a @code{vpath} directive's pattern can be quoted
1746 with preceding backslashes (@samp{\}).  Backslashes that would otherwise
1747 quote @samp{%} characters can be quoted with more backslashes.
1748 Backslashes that quote @samp{%} characters or other backslashes are
1749 removed from the pattern before it is compared to file names.  Backslashes
1750 that are not in danger of quoting @samp{%} characters go unmolested.@refill
1752 When a dependency fails to exist in the current directory, if the
1753 @var{pattern} in a @code{vpath} directive matches the name of the
1754 dependency file, then the @var{directories} in that directive are searched
1755 just like (and before) the directories in the @code{VPATH} variable.
1757 For example,
1759 @example
1760 vpath %.h ../headers
1761 @end example
1763 @noindent
1764 tells @code{make} to look for any dependency whose name ends in @file{.h}
1765 in the directory @file{../headers} if the file is not found in the current
1766 directory.
1768 If several @code{vpath} patterns match the dependency file's name, then
1769 @code{make} processes each matching @code{vpath} directive one by one,
1770 searching all the directories mentioned in each directive.  @code{make}
1771 handles multiple @code{vpath} directives in the order in which they
1772 appear in the makefile; multiple directives with the same pattern are
1773 independent of each other.
1775 @need 750
1776 Thus,
1778 @example
1779 @group
1780 vpath %.c foo
1781 vpath %   blish
1782 vpath %.c bar
1783 @end group
1784 @end example
1786 @noindent
1787 will look for a file ending in @samp{.c} in @file{foo}, then
1788 @file{blish}, then @file{bar}, while
1790 @example
1791 @group
1792 vpath %.c foo:bar
1793 vpath %   blish
1794 @end group
1795 @end example
1797 @noindent
1798 will look for a file ending in @samp{.c} in @file{foo}, then
1799 @file{bar}, then @file{blish}.
1801 @node Commands/Search, Implicit/Search, Selective Search, Directory Search
1802 @subsection Writing Shell Commands with Directory Search
1803 @cindex shell command, and directory search
1804 @cindex directory search (@code{VPATH}), and shell commands
1806 When a dependency is found in another directory through directory search,
1807 this cannot change the commands of the rule; they will execute as written.
1808 Therefore, you must write the commands with care so that they will look for
1809 the dependency in the directory where @code{make} finds it.
1811 This is done with the @dfn{automatic variables} such as @samp{$^}
1812 (@pxref{Automatic, ,Automatic Variables}).
1813 For instance, the value of @samp{$^} is a
1814 list of all the dependencies of the rule, including the names of
1815 the directories in which they were found, and the value of
1816 @samp{$@@} is the target.  Thus:@refill
1818 @example
1819 foo.o : foo.c
1820         cc -c $(CFLAGS) $^ -o $@@
1821 @end example
1823 @noindent
1824 (The variable @code{CFLAGS} exists so you can specify flags for C
1825 compilation by implicit rules; we use it here for consistency so it will
1826 affect all C compilations uniformly;
1827 @pxref{Implicit Variables, ,Variables Used by Implicit Rules}.)
1829 Often the dependencies include header files as well, which you do not
1830 want to mention in the commands.  The automatic variable @samp{$<} is
1831 just the first dependency:
1833 @example
1834 VPATH = src:../headers
1835 foo.o : foo.c defs.h hack.h
1836         cc -c $(CFLAGS) $< -o $@@
1837 @end example
1839 @node Implicit/Search, Libraries/Search, Commands/Search, Directory Search
1840 @subsection Directory Search and Implicit Rules
1841 @cindex @code{VPATH}, and implicit rules
1842 @cindex directory search (@code{VPATH}), and implicit rules
1843 @cindex search path for dependencies (@code{VPATH}), and implicit rules
1844 @cindex implicit rule, and directory search
1845 @cindex implicit rule, and @code{VPATH}
1846 @cindex rule, implicit, and directory search
1847 @cindex rule, implicit, and @code{VPATH}
1849 The search through the directories specified in @code{VPATH} or with
1850 @code{vpath} also happens during consideration of implicit rules
1851 (@pxref{Implicit Rules, ,Using Implicit Rules}).
1853 For example, when a file @file{foo.o} has no explicit rule, @code{make}
1854 considers implicit rules, such as the built-in rule to compile
1855 @file{foo.c} if that file exists.  If such a file is lacking in the
1856 current directory, the appropriate directories are searched for it.  If
1857 @file{foo.c} exists (or is mentioned in the makefile) in any of the
1858 directories, the implicit rule for C compilation is applied.
1860 The commands of implicit rules normally use automatic variables as a
1861 matter of necessity; consequently they will use the file names found by
1862 directory search with no extra effort.
1864 @node Libraries/Search,  , Implicit/Search, Directory Search
1865 @subsection Directory Search for Link Libraries
1866 @cindex link libraries, and directory search
1867 @cindex libraries for linking, directory search
1868 @cindex directory search (@code{VPATH}), and link libraries
1869 @cindex @code{VPATH}, and link libraries
1870 @cindex search path for dependencies (@code{VPATH}), and link libraries
1871 @cindex @code{-l} (library search)
1873 Directory search applies in a special way to libraries used with the
1874 linker.  This special feature comes into play when you write a dependency
1875 whose name is of the form @samp{-l@var{name}}.  (You can tell something
1876 strange is going on here because the dependency is normally the name of a
1877 file, and the @emph{file name} of the library looks like
1878 @file{lib@var{name}.a}, not like @samp{-l@var{name}}.)@refill
1880 When a dependency's name has the form @samp{-l@var{name}}, @code{make}
1881 handles it specially by searching for the file @file{lib@var{name}.a} in
1882 the current directory, in directories specified by matching @code{vpath}
1883 search paths and the @code{VPATH} search path, and then in the
1884 directories @file{/lib}, @file{/usr/lib}, and @file{@var{prefix}/lib}
1885 (normally @file{/usr/local/lib}).
1887 For example,
1889 @example
1890 @group
1891 foo : foo.c -lcurses
1892         cc $^ -o $@@
1893 @end group
1894 @end example
1896 @noindent
1897 would cause the command @samp{cc foo.c /usr/lib/libcurses.a -o foo} to
1898 be executed when @file{foo} is older than @file{foo.c} or than
1899 @file{/usr/lib/libcurses.a}.@refill
1901 @node Phony Targets, Force Targets, Directory Search, Rules
1902 @section Phony Targets
1903 @cindex phony targets
1904 @cindex targets, phony
1905 @cindex targets without a file
1907 A phony target is one that is not really the name of a file.  It is just a
1908 name for some commands to be executed when you make an explicit request.
1909 There are two reasons to use a phony target: to avoid a conflict with
1910 a file of the same name, and to improve performance.
1912 If you write a rule whose commands will not create the target file, the
1913 commands will be executed every time the target comes up for remaking.
1914 Here is an example:
1916 @example
1917 @group
1918 clean:
1919         rm *.o temp
1920 @end group
1921 @end example
1923 @noindent
1924 Because the @code{rm} command does not create a file named @file{clean},
1925 probably no such file will ever exist.  Therefore, the @code{rm} command
1926 will be executed every time you say @samp{make clean}.
1927 @cindex @code{rm} (shell command)
1929 @findex .PHONY
1930 The phony target will cease to work if anything ever does create a file
1931 named @file{clean} in this directory.  Since it has no dependencies, the
1932 file @file{clean} would inevitably be considered up to date, and its
1933 commands would not be executed.  To avoid this problem, you can explicitly
1934 declare the target to be phony, using the special target @code{.PHONY}
1935 (@pxref{Special Targets, ,Special Built-in Target Names}) as follows:
1937 @example
1938 .PHONY : clean
1939 @end example
1941 @noindent
1942 Once this is done, @samp{make clean} will run the commands regardless of
1943 whether there is a file named @file{clean}.
1945 Since it knows that phony targets do not name actual files that could be
1946 remade from other files, @code{make} skips the implicit rule search for
1947 phony targets (@pxref{Implicit Rules}).  This is why declaring a target
1948 phony is good for performance, even if you are not worried about the
1949 actual file existing.
1951 Thus, you first write the line that states that @code{clean} is a
1952 phony target, then you write the rule, like this:
1954 @example
1955 @group
1956 .PHONY: clean
1957 clean:
1958         rm *.o temp
1959 @end group
1960 @end example
1962 A phony target should not be a dependency of a real target file; if it
1963 is, its commands are run every time @code{make} goes to update that
1964 file.  As long as a phony target is never a dependency of a real
1965 target, the phony target commands will be executed only when the phony
1966 target is a specified goal (@pxref{Goals, ,Arguments to Specify the
1967 Goals}).
1969 Phony targets can have dependencies.  When one directory contains multiple
1970 programs, it is most convenient to describe all of the programs in one
1971 makefile @file{./Makefile}.  Since the target remade by default will be the
1972 first one in the makefile, it is common to make this a phony target named
1973 @samp{all} and give it, as dependencies, all the individual programs.  For
1974 example:
1976 @example
1977 all : prog1 prog2 prog3
1978 .PHONY : all
1980 prog1 : prog1.o utils.o
1981         cc -o prog1 prog1.o utils.o
1983 prog2 : prog2.o
1984         cc -o prog2 prog2.o
1986 prog3 : prog3.o sort.o utils.o
1987         cc -o prog3 prog3.o sort.o utils.o
1988 @end example
1990 @noindent
1991 Now you can say just @samp{make} to remake all three programs, or specify
1992 as arguments the ones to remake (as in @samp{make prog1 prog3}).
1994 When one phony target is a dependency of another, it serves as a subroutine
1995 of the other.  For example, here @samp{make cleanall} will delete the
1996 object files, the difference files, and the file @file{program}:
1998 @example
1999 .PHONY: cleanall cleanobj cleandiff
2001 cleanall : cleanobj cleandiff
2002         rm program
2004 cleanobj :
2005         rm *.o
2007 cleandiff :
2008         rm *.diff
2009 @end example
2011 @node Force Targets, Empty Targets, Phony Targets, Rules
2012 @section Rules without Commands or Dependencies
2013 @cindex force targets
2014 @cindex targets, force
2015 @cindex @code{FORCE}
2016 @cindex rule, no commands or dependencies
2018 If a rule has no dependencies or commands, and the target of the rule
2019 is a nonexistent file, then @code{make} imagines this target to have
2020 been updated whenever its rule is run.  This implies that all targets
2021 depending on this one will always have their commands run.
2023 An example will illustrate this:
2025 @example
2026 @group
2027 clean: FORCE
2028         rm $(objects)
2029 FORCE:
2030 @end group
2031 @end example
2033 Here the target @samp{FORCE} satisfies the special conditions, so the
2034 target @file{clean} that depends on it is forced to run its commands.
2035 There is nothing special about the name @samp{FORCE}, but that is one name
2036 commonly used this way.
2038 As you can see, using @samp{FORCE} this way has the same results as using
2039 @samp{.PHONY: clean}.
2041 Using @samp{.PHONY} is more explicit and more efficient.  However,
2042 other versions of @code{make} do not support @samp{.PHONY}; thus
2043 @samp{FORCE} appears in many makefiles.  @xref{Phony Targets}.
2045 @node Empty Targets, Special Targets, Force Targets, Rules
2046 @section Empty Target Files to Record Events
2047 @cindex empty targets
2048 @cindex targets, empty
2049 @cindex recording events with empty targets
2051 The @dfn{empty target} is a variant of the phony target; it is used to hold
2052 commands for an action that you request explicitly from time to time.
2053 Unlike a phony target, this target file can really exist; but the file's
2054 contents do not matter, and usually are empty.
2056 The purpose of the empty target file is to record, with its
2057 last-modification time, when the rule's commands were last executed.  It
2058 does so because one of the commands is a @code{touch} command to update the
2059 target file.
2061 The empty target file must have some dependencies.  When you ask to remake
2062 the empty target, the commands are executed if any dependency is more
2063 recent than the target; in other words, if a dependency has changed since
2064 the last time you remade the target.  Here is an example:
2066 @example
2067 print: foo.c bar.c
2068         lpr -p $?
2069         touch print
2070 @end example
2071 @cindex @code{print} target
2072 @cindex @code{lpr} (shell command)
2073 @cindex @code{touch} (shell command)
2075 @noindent
2076 With this rule, @samp{make print} will execute the @code{lpr} command if
2077 either source file has changed since the last @samp{make print}.  The
2078 automatic variable @samp{$?} is used to print only those files that have
2079 changed (@pxref{Automatic, ,Automatic Variables}).
2081 @node Special Targets, Multiple Targets, Empty Targets, Rules
2082 @section Special Built-in Target Names
2083 @cindex special targets
2084 @cindex built-in special targets
2085 @cindex targets, built-in special
2087 Certain names have special meanings if they appear as targets.
2089 @table @code
2090 @findex .PHONY
2091 @item .PHONY
2093 The dependencies of the special target @code{.PHONY} are considered to
2094 be phony targets.  When it is time to consider such a target,
2095 @code{make} will run its commands unconditionally, regardless of
2096 whether a file with that name exists or what its last-modification
2097 time is.  @xref{Phony Targets, ,Phony Targets}.
2099 @findex .SUFFIXES
2100 @item .SUFFIXES
2102 The dependencies of the special target @code{.SUFFIXES} are the list
2103 of suffixes to be used in checking for suffix rules.
2104 @xref{Suffix Rules, , Old-Fashioned Suffix Rules}.
2106 @findex .DEFAULT
2107 @item .DEFAULT
2109 The commands specified for @code{.DEFAULT} are used for any target for
2110 which no rules are found (either explicit rules or implicit rules).
2111 @xref{Last Resort}.  If @code{.DEFAULT} commands are specified, every
2112 file mentioned as a dependency, but not as a target in a rule, will have
2113 these commands executed on its behalf.  @xref{Search Algorithm,
2114 ,Implicit Rule Search Algorithm}.
2116 @findex .PRECIOUS
2117 @item .PRECIOUS
2118 @cindex precious targets
2119 @cindex preserving with @code{.PRECIOUS}
2121 The targets which @code{.PRECIOUS} depends on are given the following
2122 special treatment: if @code{make} is killed or interrupted during the
2123 execution of their commands, the target is not deleted.
2124 @xref{Interrupts, ,Interrupting or Killing @code{make}}.
2125 Also, if the target is an intermediate file, it will not be deleted
2126 after it is no longer needed, as is normally done.
2127 @xref{Chained Rules, ,Chains of Implicit Rules}.
2129 You can also list the target pattern of an implicit rule (such as
2130 @samp{%.o}) as a dependency file of the special target @code{.PRECIOUS}
2131 to preserve intermediate files created by rules whose target patterns
2132 match that file's name.
2134 @findex .INTERMEDIATE
2135 @item .INTERMEDIATE
2136 @cindex intermediate targets, explicit
2138 The targets which @code{.INTERMEDIATE} depends on are treated as
2139 intermediate files.  @xref{Chained Rules, ,Chains of Implicit Rules}.
2140 @code{.INTERMEDIATE} with no dependencies marks all file targets
2141 mentioned in the makefile as intermediate.
2143 @findex .SECONDARY
2144 @item .SECONDARY
2145 @cindex secondary targets
2146 @cindex preserving with @code{.SECONDARY}
2148 The targets which @code{.SECONDARY} depends on are treated as
2149 intermediate files, except that they are never automatically deleted.
2150 @xref{Chained Rules, ,Chains of Implicit Rules}.
2152 @code{.SECONDARY} with no dependencies marks all file targets mentioned
2153 in the makefile as secondary.
2155 @findex .IGNORE
2156 @item .IGNORE
2158 If you specify dependencies for @code{.IGNORE}, then @code{make} will
2159 ignore errors in execution of the commands run for those particular
2160 files.  The commands for @code{.IGNORE} are not meaningful.
2162 If mentioned as a target with no dependencies, @code{.IGNORE} says to
2163 ignore errors in execution of commands for all files.  This usage of
2164 @samp{.IGNORE} is supported only for historical compatibility.  Since
2165 this affects every command in the makefile, it is not very useful; we
2166 recommend you use the more selective ways to ignore errors in specific
2167 commands.  @xref{Errors, ,Errors in Commands}.
2169 @findex .SILENT
2170 @item .SILENT
2172 If you specify dependencies for @code{.SILENT}, then @code{make} will
2173 not the print commands to remake those particular files before executing
2174 them.  The commands for @code{.SILENT} are not meaningful.
2176 If mentioned as a target with no dependencies, @code{.SILENT} says not
2177 to print any commands before executing them.  This usage of
2178 @samp{.SILENT} is supported only for historical compatibility.  We
2179 recommend you use the more selective ways to silence specific commands.
2180 @xref{Echoing, ,Command Echoing}.  If you want to silence all commands
2181 for a particular run of @code{make}, use the @samp{-s} or
2182 @w{@samp{--silent}} option (@pxref{Options Summary}).
2184 @findex .EXPORT_ALL_VARIABLES
2185 @item .EXPORT_ALL_VARIABLES
2187 Simply by being mentioned as a target, this tells @code{make} to
2188 export all variables to child processes by default.
2189 @xref{Variables/Recursion, ,Communicating Variables to a
2190 Sub-@code{make}}.
2191 @end table
2193 Any defined implicit rule suffix also counts as a special target if it
2194 appears as a target, and so does the concatenation of two suffixes, such
2195 as @samp{.c.o}.  These targets are suffix rules, an obsolete way of
2196 defining implicit rules (but a way still widely used).  In principle, any
2197 target name could be special in this way if you break it in two and add
2198 both pieces to the suffix list.  In practice, suffixes normally begin with
2199 @samp{.}, so these special target names also begin with @samp{.}.
2200 @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
2202 @node Multiple Targets, Multiple Rules, Special Targets, Rules
2203 @section Multiple Targets in a Rule
2204 @cindex multiple targets
2205 @cindex several targets in a rule
2206 @cindex targets, multiple
2207 @cindex rule, with multiple targets
2209 A rule with multiple targets is equivalent to writing many rules, each with
2210 one target, and all identical aside from that.  The same commands apply to
2211 all the targets, but their effects may vary because you can substitute the
2212 actual target name into the command using @samp{$@@}.  The rule contributes
2213 the same dependencies to all the targets also.
2215 This is useful in two cases.
2217 @itemize @bullet
2218 @item
2219 You want just dependencies, no commands.  For example:
2221 @example
2222 kbd.o command.o files.o: command.h
2223 @end example
2225 @noindent
2226 gives an additional dependency to each of the three object files
2227 mentioned.
2229 @item
2230 Similar commands work for all the targets.  The commands do not need
2231 to be absolutely identical, since the automatic variable @samp{$@@}
2232 can be used to substitute the particular target to be remade into the
2233 commands (@pxref{Automatic, ,Automatic Variables}).  For example:
2235 @example
2236 @group
2237 bigoutput littleoutput : text.g
2238         generate text.g -$(subst output,,$@@) > $@@
2239 @end group
2240 @end example
2241 @findex subst
2243 @noindent
2244 is equivalent to
2246 @example
2247 bigoutput : text.g
2248         generate text.g -big > bigoutput
2249 littleoutput : text.g
2250         generate text.g -little > littleoutput
2251 @end example
2253 @noindent
2254 Here we assume the hypothetical program @code{generate} makes two
2255 types of output, one if given @samp{-big} and one if given
2256 @samp{-little}.
2257 @xref{Text Functions, ,Functions for String Substitution and Analysis},
2258 for an explanation of the @code{subst} function.
2259 @end itemize
2261 Suppose you would like to vary the dependencies according to the target,
2262 much as the variable @samp{$@@} allows you to vary the commands.
2263 You cannot do this with multiple targets in an ordinary rule, but you can
2264 do it with a @dfn{static pattern rule}.
2265 @xref{Static Pattern, ,Static Pattern Rules}.
2267 @node Multiple Rules, Static Pattern, Multiple Targets, Rules
2268 @section Multiple Rules for One Target
2269 @cindex multiple rules for one target
2270 @cindex several rules for one target
2271 @cindex rule, multiple for one target
2272 @cindex target, multiple rules for one
2274 One file can be the target of several rules.  All the dependencies
2275 mentioned in all the rules are merged into one list of dependencies for
2276 the target.  If the target is older than any dependency from any rule,
2277 the commands are executed.
2279 There can only be one set of commands to be executed for a file.
2280 If more than one rule gives commands for the same file,
2281 @code{make} uses the last set given and prints an error message.
2282 (As a special case, if the file's name begins with a dot, no
2283 error message is printed.  This odd behavior is only for
2284 compatibility with other implementations of @code{make}.)
2285 There is no reason to
2286 write your makefiles this way; that is why @code{make} gives you
2287 an error message.@refill
2289 An extra rule with just dependencies can be used to give a few extra
2290 dependencies to many files at once.  For example, one usually has a
2291 variable named @code{objects} containing a list of all the compiler output
2292 files in the system being made.  An easy way to say that all of them must
2293 be recompiled if @file{config.h} changes is to write the following:
2295 @example
2296 objects = foo.o bar.o
2297 foo.o : defs.h
2298 bar.o : defs.h test.h
2299 $(objects) : config.h
2300 @end example
2302 This could be inserted or taken out without changing the rules that really
2303 specify how to make the object files, making it a convenient form to use if
2304 you wish to add the additional dependency intermittently.
2306 Another wrinkle is that the additional dependencies could be specified with
2307 a variable that you set with a command argument to @code{make}
2308 (@pxref{Overriding, ,Overriding Variables}).  For example,
2310 @example
2311 @group
2312 extradeps=
2313 $(objects) : $(extradeps)
2314 @end group
2315 @end example
2317 @noindent
2318 means that the command @samp{make extradeps=foo.h} will consider
2319 @file{foo.h} as a dependency of each object file, but plain @samp{make}
2320 will not.
2322 If none of the explicit rules for a target has commands, then @code{make}
2323 searches for an applicable implicit rule to find some commands
2324 @pxref{Implicit Rules, ,Using Implicit Rules}).
2326 @node Static Pattern, Double-Colon, Multiple Rules, Rules
2327 @section Static Pattern Rules
2328 @cindex static pattern rule
2329 @cindex rule, static pattern
2330 @cindex pattern rules, static (not implicit)
2331 @cindex varying dependencies
2332 @cindex dependencies, varying (static pattern)
2334 @dfn{Static pattern rules} are rules which specify multiple targets and
2335 construct the dependency names for each target based on the target name.
2336 They are more general than ordinary rules with multiple targets because the
2337 targets do not have to have identical dependencies.  Their dependencies must
2338 be @emph{analogous}, but not necessarily @emph{identical}.
2340 @menu
2341 * Static Usage::                The syntax of static pattern rules.
2342 * Static versus Implicit::      When are they better than implicit rules?
2343 @end menu
2345 @node Static Usage, Static versus Implicit,  , Static Pattern
2346 @subsection Syntax of Static Pattern Rules
2347 @cindex static pattern rule, syntax of
2348 @cindex pattern rules, static, syntax of
2350 Here is the syntax of a static pattern rule:
2352 @example
2353 @var{targets} @dots{}: @var{target-pattern}: @var{dep-patterns} @dots{}
2354         @var{commands}
2355         @dots{}
2356 @end example
2358 @noindent
2359 The @var{targets} list specifies the targets that the rule applies to.
2360 The targets can contain wildcard characters, just like the targets of
2361 ordinary rules (@pxref{Wildcards, ,Using Wildcard Characters in File
2362 Names}).
2364 @cindex target pattern, static (not implicit)
2365 @cindex stem
2366 The @var{target-pattern} and @var{dep-patterns} say how to compute the
2367 dependencies of each target.  Each target is matched against the
2368 @var{target-pattern} to extract a part of the target name, called the
2369 @dfn{stem}.  This stem is substituted into each of the @var{dep-patterns}
2370 to make the dependency names (one from each @var{dep-pattern}).
2372 Each pattern normally contains the character @samp{%} just once.  When the
2373 @var{target-pattern} matches a target, the @samp{%} can match any part of
2374 the target name; this part is called the @dfn{stem}.  The rest of the
2375 pattern must match exactly.  For example, the target @file{foo.o} matches
2376 the pattern @samp{%.o}, with @samp{foo} as the stem.  The targets
2377 @file{foo.c} and @file{foo.out} do not match that pattern.@refill
2379 @cindex dependency pattern, static (not implicit)
2380 The dependency names for each target are made by substituting the stem
2381 for the @samp{%} in each dependency pattern.  For example, if one
2382 dependency pattern is @file{%.c}, then substitution of the stem
2383 @samp{foo} gives the dependency name @file{foo.c}.  It is legitimate
2384 to write a dependency pattern that does not contain @samp{%}; then this
2385 dependency is the same for all targets.
2387 @cindex @code{%}, quoting in static pattern
2388 @cindex @code{%}, quoting with @code{\} (backslash)
2389 @cindex @code{\} (backslash), to quote @code{%}
2390 @cindex backslash (@code{\}), to quote @code{%}
2391 @cindex quoting @code{%}, in static pattern
2392 @samp{%} characters in pattern rules can be quoted with preceding
2393 backslashes (@samp{\}).  Backslashes that would otherwise quote @samp{%}
2394 characters can be quoted with more backslashes.  Backslashes that quote
2395 @samp{%} characters or other backslashes are removed from the pattern
2396 before it is compared to file names or has a stem substituted into it.
2397 Backslashes that are not in danger of quoting @samp{%} characters go
2398 unmolested.  For example, the pattern @file{the\%weird\\%pattern\\} has
2399 @samp{the%weird\} preceding the operative @samp{%} character, and
2400 @samp{pattern\\} following it.  The final two backslashes are left alone
2401 because they cannot affect any @samp{%} character.@refill
2403 Here is an example, which compiles each of @file{foo.o} and @file{bar.o}
2404 from the corresponding @file{.c} file:
2406 @example
2407 @group
2408 objects = foo.o bar.o
2410 $(objects): %.o: %.c
2411         $(CC) -c $(CFLAGS) $< -o $@@
2412 @end group
2413 @end example
2415 @noindent
2416 Here @samp{$<} is the automatic variable that holds the name of the
2417 dependency and @samp{$@@} is the automatic variable that holds the name
2418 of the target; see @ref{Automatic, , Automatic Variables}.
2420 Each target specified must match the target pattern; a warning is issued
2421 for each target that does not.  If you have a list of files, only some of
2422 which will match the pattern, you can use the @code{filter} function to
2423 remove nonmatching file names (@pxref{Text Functions, ,Functions for String Substitution and Analysis}):
2425 @example
2426 files = foo.elc bar.o lose.o
2428 $(filter %.o,$(files)): %.o: %.c
2429         $(CC) -c $(CFLAGS) $< -o $@@
2430 $(filter %.elc,$(files)): %.elc: %.el
2431         emacs -f batch-byte-compile $<
2432 @end example
2434 @noindent
2435 In this example the result of @samp{$(filter %.o,$(files))} is
2436 @file{bar.o lose.o}, and the first static pattern rule causes each of
2437 these object files to be updated by compiling the corresponding C source
2438 file.  The result of @w{@samp{$(filter %.elc,$(files))}} is
2439 @file{foo.elc}, so that file is made from @file{foo.el}.@refill
2441 Another example shows how to use @code{$*} in static pattern rules:
2442 @vindex $*@r{, and static pattern}
2444 @example
2445 @group
2446 bigoutput littleoutput : %output : text.g
2447         generate text.g -$* > $@@
2448 @end group
2449 @end example
2451 @noindent
2452 When the @code{generate} command is run, @code{$*} will expand to the
2453 stem, either @samp{big} or @samp{little}.
2455 @node Static versus Implicit,  , Static Usage, Static Pattern
2456 @subsection Static Pattern Rules versus Implicit Rules
2457 @cindex rule, static pattern versus implicit
2458 @cindex static pattern rule, versus implicit
2460 A static pattern rule has much in common with an implicit rule defined as a
2461 pattern rule (@pxref{Pattern Rules, ,Defining and Redefining Pattern Rules}).
2462 Both have a pattern for the target and patterns for constructing the
2463 names of dependencies.  The difference is in how @code{make} decides
2464 @emph{when} the rule applies.
2466 An implicit rule @emph{can} apply to any target that matches its pattern,
2467 but it @emph{does} apply only when the target has no commands otherwise
2468 specified, and only when the dependencies can be found.  If more than one
2469 implicit rule appears applicable, only one applies; the choice depends on
2470 the order of rules.
2472 By contrast, a static pattern rule applies to the precise list of targets
2473 that you specify in the rule.  It cannot apply to any other target and it
2474 invariably does apply to each of the targets specified.  If two conflicting
2475 rules apply, and both have commands, that's an error.
2477 The static pattern rule can be better than an implicit rule for these
2478 reasons:
2480 @itemize @bullet
2481 @item
2482 You may wish to override the usual implicit rule for a few
2483 files whose names cannot be categorized syntactically but
2484 can be given in an explicit list.
2486 @item
2487 If you cannot be sure of the precise contents of the directories
2488 you are using, you may not be sure which other irrelevant files
2489 might lead @code{make} to use the wrong implicit rule.  The choice
2490 might depend on the order in which the implicit rule search is done.
2491 With static pattern rules, there is no uncertainty: each rule applies
2492 to precisely the targets specified.
2493 @end itemize
2495 @node Double-Colon, Automatic Dependencies, Static Pattern, Rules
2496 @section Double-Colon Rules
2497 @cindex double-colon rules
2498 @cindex rule, double-colon (@code{::})
2499 @cindex multiple rules for one target (@code{::})
2500 @cindex @code{::} rules (double-colon)
2502 @dfn{Double-colon} rules are rules written with @samp{::} instead of
2503 @samp{:} after the target names.  They are handled differently from
2504 ordinary rules when the same target appears in more than one rule.
2506 When a target appears in multiple rules, all the rules must be the same
2507 type: all ordinary, or all double-colon.  If they are double-colon, each of
2508 them is independent of the others.  Each double-colon rule's commands are
2509 executed if the target is older than any dependencies of that rule.  This
2510 can result in executing none, any, or all of the double-colon rules.
2512 Double-colon rules with the same target are in fact completely separate
2513 from one another.  Each double-colon rule is processed individually, just
2514 as rules with different targets are processed.
2516 The double-colon rules for a target are executed in the order they appear
2517 in the makefile.  However, the cases where double-colon rules really make
2518 sense are those where the order of executing the commands would not matter.
2520 Double-colon rules are somewhat obscure and not often very useful; they
2521 provide a mechanism for cases in which the method used to update a target
2522 differs depending on which dependency files caused the update, and such
2523 cases are rare.
2525 Each double-colon rule should specify commands; if it does not, an
2526 implicit rule will be used if one applies.
2527 @xref{Implicit Rules, ,Using Implicit Rules}.
2529 @node Automatic Dependencies,  , Double-Colon, Rules
2530 @section Generating Dependencies Automatically
2531 @cindex dependencies, automatic generation
2532 @cindex automatic generation of dependencies
2533 @cindex generating dependencies automatically
2535 In the makefile for a program, many of the rules you need to write often
2536 say only that some object file depends on some header
2537 file.  For example, if @file{main.c} uses @file{defs.h} via an
2538 @code{#include}, you would write:
2540 @example
2541 main.o: defs.h
2542 @end example
2544 @noindent
2545 You need this rule so that @code{make} knows that it must remake
2546 @file{main.o} whenever @file{defs.h} changes.  You can see that for a
2547 large program you would have to write dozens of such rules in your
2548 makefile.  And, you must always be very careful to update the makefile
2549 every time you add or remove an @code{#include}.
2550 @cindex @code{#include}
2552 @cindex @code{-M} (to compiler)
2553 To avoid this hassle, most modern C compilers can write these rules for
2554 you, by looking at the @code{#include} lines in the source files.
2555 Usually this is done with the @samp{-M} option to the compiler.
2556 For example, the command:
2558 @example
2559 cc -M main.c
2560 @end example
2562 @noindent
2563 generates the output:
2565 @example
2566 main.o : main.c defs.h
2567 @end example
2569 @noindent
2570 Thus you no longer have to write all those rules yourself.
2571 The compiler will do it for you.
2573 Note that such a dependency constitutes mentioning @file{main.o} in a
2574 makefile, so it can never be considered an intermediate file by implicit
2575 rule search.  This means that @code{make} won't ever remove the file
2576 after using it; @pxref{Chained Rules, ,Chains of Implicit Rules}.
2578 @cindex @code{make depend}
2579 With old @code{make} programs, it was traditional practice to use this
2580 compiler feature to generate dependencies on demand with a command like
2581 @samp{make depend}.  That command would create a file @file{depend}
2582 containing all the automatically-generated dependencies; then the
2583 makefile could use @code{include} to read them in (@pxref{Include}).
2585 In GNU @code{make}, the feature of remaking makefiles makes this
2586 practice obsolete---you need never tell @code{make} explicitly to
2587 regenerate the dependencies, because it always regenerates any makefile
2588 that is out of date.  @xref{Remaking Makefiles}.
2590 The practice we recommend for automatic dependency generation is to have
2591 one makefile corresponding to each source file.  For each source file
2592 @file{@var{name}.c} there is a makefile @file{@var{name}.d} which lists
2593 what files the object file @file{@var{name}.o} depends on.  That way
2594 only the source files that have changed need to be rescanned to produce
2595 the new dependencies.
2597 Here is the pattern rule to generate a file of dependencies (i.e., a makefile)
2598 called @file{@var{name}.d} from a C source file called @file{@var{name}.c}:
2600 @smallexample
2601 @group
2602 %.d: %.c
2603         $(SHELL) -ec '$(CC) -M $(CPPFLAGS) $< \
2604                       | sed '\''s/\($*\)\.o[ :]*/\1 $@@/g'\'' > $@@'
2605 @end group
2606 @end smallexample
2608 @noindent
2609 @xref{Pattern Rules}, for information on defining pattern rules.  The
2610 @samp{-e} flag to the shell makes it exit immediately if the
2611 @code{$(CC)} command fails (exits with a nonzero status).  Normally the
2612 shell exits with the status of the last command in the pipeline
2613 (@code{sed} in this case), so @code{make} would not notice a nonzero
2614 status from the compiler.
2615 @cindex @code{-e} (shell flag)
2617 @cindex @code{-MM} (to GNU compiler)
2618 With the GNU C compiler, you may wish to use the @samp{-MM} flag instead
2619 of @samp{-M}.  This omits dependencies on system header files.
2620 @xref{Preprocessor Options, , Options Controlling the Preprocessor,
2621 gcc.info, Using GNU CC}, for details.
2623 @cindex @code{sed} (shell command)
2624 The purpose of the @code{sed} command is to translate (for example):
2626 @example
2627 main.o : main.c defs.h
2628 @end example
2630 @noindent
2631 into:
2633 @example
2634 main.o main.d : main.c defs.h
2635 @end example
2637 @noindent
2638 @cindex @code{.d}
2639 This makes each @samp{.d} file depend on all the source and header files
2640 that the corresponding @samp{.o} file depends on.  @code{make} then
2641 knows it must regenerate the dependencies whenever any of the source or
2642 header files changes.
2644 Once you've defined the rule to remake the @samp{.d} files,
2645 you then use the @code{include} directive to read them all in.
2646 @xref{Include}.  For example:
2648 @example
2649 @group
2650 sources = foo.c bar.c
2652 include $(sources:.c=.d)
2653 @end group
2654 @end example
2656 @noindent
2657 (This example uses a substitution variable reference to translate the
2658 list of source files @samp{foo.c bar.c} into a list of dependency
2659 makefiles, @samp{foo.d bar.d}.  @xref{Substitution Refs}, for full
2660 information on substitution references.)  Since the @samp{.d} files are
2661 makefiles like any others, @code{make} will remake them as necessary
2662 with no further work from you.  @xref{Remaking Makefiles}.
2664 @node Commands, Using Variables, Rules, Top
2665 @chapter Writing the Commands in Rules
2666 @cindex commands, how to write
2667 @cindex rule commands
2668 @cindex writing rule commands
2670 The commands of a rule consist of shell command lines to be executed one
2671 by one.  Each command line must start with a tab, except that the first
2672 command line may be attached to the target-and-dependencies line with a
2673 semicolon in between.  Blank lines and lines of just comments may appear
2674 among the command lines; they are ignored.  (But beware, an apparently
2675 ``blank'' line that begins with a tab is @emph{not} blank!  It is an
2676 empty command; @pxref{Empty Commands}.)
2678 Users use many different shell programs, but commands in makefiles are
2679 always interpreted by @file{/bin/sh} unless the makefile specifies
2680 otherwise.  @xref{Execution, ,Command Execution}.
2682 @cindex comments, in commands
2683 @cindex commands, comments in
2684 @cindex @code{#} (comments), in commands
2685 The shell that is in use determines whether comments can be written on
2686 command lines, and what syntax they use.  When the shell is
2687 @file{/bin/sh}, a @samp{#} starts a comment that extends to the end of
2688 the line.  The @samp{#} does not have to be at the beginning of a line.
2689 Text on a line before a @samp{#} is not part of the comment.
2691 @menu
2692 * Echoing::                     How to control when commands are echoed.
2693 * Execution::                   How commands are executed.
2694 * Parallel::                    How commands can be executed in parallel.
2695 * Errors::                      What happens after a command execution error.
2696 * Interrupts::                  What happens when a command is interrupted.
2697 * Recursion::                   Invoking @code{make} from makefiles.
2698 * Sequences::                   Defining canned sequences of commands.
2699 * Empty Commands::              Defining useful, do-nothing commands.
2700 @end menu
2702 @node Echoing, Execution,  , Commands
2703 @section Command Echoing
2704 @cindex echoing of commands
2705 @cindex silent operation
2706 @cindex @code{@@} (in commands)
2707 @cindex commands, echoing
2708 @cindex printing of commands
2710 Normally @code{make} prints each command line before it is executed.
2711 We call this @dfn{echoing} because it gives the appearance that you
2712 are typing the commands yourself.
2714 When a line starts with @samp{@@}, the echoing of that line is suppressed.
2715 The @samp{@@} is discarded before the command is passed to the shell.
2716 Typically you would use this for a command whose only effect is to print
2717 something, such as an @code{echo} command to indicate progress through
2718 the makefile:
2720 @example
2721 @@echo About to make distribution files
2722 @end example
2724 @cindex @code{-n}
2725 @cindex @code{--just-print}
2726 @cindex @code{--dry-run}
2727 @cindex @code{--recon}
2728 When @code{make} is given the flag @samp{-n} or @samp{--just-print},
2729 echoing is all that happens, no execution.  @xref{Options Summary,
2730 ,Summary of Options}.  In this case and only this case, even the
2731 commands starting with @samp{@@} are printed.  This flag is useful for
2732 finding out which commands @code{make} thinks are necessary without
2733 actually doing them.
2735 @cindex @code{-s}
2736 @cindex @code{--silent}
2737 @cindex @code{--quiet}
2738 @findex .SILENT
2739 The @samp{-s} or @samp{--silent}
2740 flag to @code{make} prevents all echoing, as if all commands
2741 started with @samp{@@}.  A rule in the makefile for the special target
2742 @code{.SILENT} without dependencies has the same effect
2743 (@pxref{Special Targets, ,Special Built-in Target Names}).
2744 @code{.SILENT} is essentially obsolete since @samp{@@} is more flexible.@refill
2746 @node Execution, Parallel, Echoing, Commands
2747 @section Command Execution
2748 @cindex commands, execution
2749 @cindex execution, of commands
2750 @cindex shell command, execution
2751 @vindex SHELL @r{(command execution)}
2753 When it is time to execute commands to update a target, they are executed
2754 by making a new subshell for each line.  (In practice, @code{make} may
2755 take shortcuts that do not affect the results.)
2757 @cindex @code{cd} (shell command)
2758 @strong{Please note:} this implies that shell commands such as
2759 @code{cd} that set variables local to each process will not affect the
2760 following command lines.  If you want to use @code{cd} to affect the
2761 next command, put the two on a single line with a semicolon between
2762 them.  Then @code{make} will consider them a single command and pass
2763 them, together, to a shell which will execute them in sequence.  For
2764 example:
2766 @example
2767 foo : bar/lose
2768         cd bar; gobble lose > ../foo
2769 @end example
2771 @cindex commands, backslash (@code{\}) in
2772 @cindex commands, quoting newlines in
2773 @cindex backslash (@code{\}), in commands
2774 @cindex @code{\} (backslash), in commands
2775 @cindex quoting newline, in commands
2776 @cindex newline, quoting, in commands
2777 If you would like to split a single shell command into multiple lines of
2778 text, you must use a backslash at the end of all but the last subline.
2779 Such a sequence of lines is combined into a single line, by deleting the
2780 backslash-newline sequences, before passing it to the shell.  Thus, the
2781 following is equivalent to the preceding example:
2783 @example
2784 @group
2785 foo : bar/lose
2786         cd bar;  \
2787         gobble lose > ../foo
2788 @end group
2789 @end example
2791 @vindex SHELL
2792 The program used as the shell is taken from the variable @code{SHELL}.
2793 By default, the program @file{/bin/sh} is used.
2795 @cindex environment, @code{SHELL} in
2796 Unlike most variables, the variable @code{SHELL} is never set from the
2797 environment.  This is because the @code{SHELL} environment variable is
2798 used to specify your personal choice of shell program for interactive
2799 use.  It would be very bad for personal choices like this to affect
2800 the functioning of makefiles.  @xref{Environment, ,Variables from the
2801 Environment}.
2803 @node Parallel, Errors, Execution, Commands
2804 @section Parallel Execution
2805 @cindex commands, execution in parallel
2806 @cindex parallel execution
2807 @cindex execution, in parallel
2808 @cindex job slots
2809 @cindex @code{-j}
2810 @cindex @code{--jobs}
2812 GNU @code{make} knows how to execute several commands at once.
2813 Normally, @code{make} will execute only one command at a time, waiting
2814 for it to finish before executing the next.  However, the @samp{-j} or
2815 @samp{--jobs} option tells @code{make} to execute many commands
2816 simultaneously.@refill
2818 If the @samp{-j} option is followed by an integer, this is the number of
2819 commands to execute at once; this is called the number of @dfn{job slots}.
2820 If there is nothing looking like an integer after the @samp{-j} option,
2821 there is no limit on the number of job slots.  The default number of job
2822 slots is one, which means serial execution (one thing at a time).
2824 One unpleasant consequence of running several commands simultaneously is
2825 that output from all of the commands comes when the commands send it, so
2826 messages from different commands may be interspersed.
2828 Another problem is that two processes cannot both take input from the
2829 same device; so to make sure that only one command tries to take input
2830 from the terminal at once, @code{make} will invalidate the standard
2831 input streams of all but one running command.  This means that
2832 attempting to read from standard input will usually be a fatal error (a
2833 @samp{Broken pipe} signal) for most child processes if there are
2834 several.
2835 @cindex broken pipe
2836 @cindex standard input
2838 It is unpredictable which command will have a valid standard input stream
2839 (which will come from the terminal, or wherever you redirect the standard
2840 input of @code{make}).  The first command run will always get it first, and
2841 the first command started after that one finishes will get it next, and so
2844 We will change how this aspect of @code{make} works if we find a better
2845 alternative.  In the mean time, you should not rely on any command using
2846 standard input at all if you are using the parallel execution feature; but
2847 if you are not using this feature, then standard input works normally in
2848 all commands.
2850 If a command fails (is killed by a signal or exits with a nonzero
2851 status), and errors are not ignored for that command
2852 (@pxref{Errors, ,Errors in Commands}),
2853 the remaining command lines to remake the same target will not be run.
2854 If a command fails and the @samp{-k} or @samp{--keep-going}
2855 option was not given
2856 (@pxref{Options Summary, ,Summary of Options}),
2857 @code{make} aborts execution.  If make
2858 terminates for any reason (including a signal) with child processes
2859 running, it waits for them to finish before actually exiting.@refill
2861 @cindex load average
2862 @cindex limiting jobs based on load
2863 @cindex jobs, limiting based on load
2864 @cindex @code{-l} (load average)
2865 @cindex @code{--max-load}
2866 @cindex @code{--load-average}
2867 When the system is heavily loaded, you will probably want to run fewer jobs
2868 than when it is lightly loaded.  You can use the @samp{-l} option to tell
2869 @code{make} to limit the number of jobs to run at once, based on the load
2870 average.  The @samp{-l} or @samp{--max-load}
2871 option is followed by a floating-point number.  For
2872 example,
2874 @example
2875 -l 2.5
2876 @end example
2878 @noindent
2879 will not let @code{make} start more than one job if the load average is
2880 above 2.5.  The @samp{-l} option with no following number removes the
2881 load limit, if one was given with a previous @samp{-l} option.@refill
2883 More precisely, when @code{make} goes to start up a job, and it already has
2884 at least one job running, it checks the current load average; if it is not
2885 lower than the limit given with @samp{-l}, @code{make} waits until the load
2886 average goes below that limit, or until all the other jobs finish.
2888 By default, there is no load limit.
2890 @node Errors, Interrupts, Parallel, Commands
2891 @section Errors in Commands
2892 @cindex errors (in commands)
2893 @cindex commands, errors in
2894 @cindex exit status (errors)
2896 After each shell command returns, @code{make} looks at its exit status.
2897 If the command completed successfully, the next command line is executed
2898 in a new shell; after the last command line is finished, the rule is
2899 finished.
2901 If there is an error (the exit status is nonzero), @code{make} gives up on
2902 the current rule, and perhaps on all rules.
2904 Sometimes the failure of a certain command does not indicate a problem.
2905 For example, you may use the @code{mkdir} command to ensure that a
2906 directory exists.  If the directory already exists, @code{mkdir} will
2907 report an error, but you probably want @code{make} to continue regardless.
2909 @cindex @code{-} (in commands)
2910 To ignore errors in a command line, write a @samp{-} at the beginning of
2911 the line's text (after the initial tab).  The @samp{-} is discarded before
2912 the command is passed to the shell for execution.
2914 For example,
2916 @example
2917 @group
2918 clean:
2919         -rm -f *.o
2920 @end group
2921 @end example
2922 @cindex @code{rm} (shell command)
2924 @noindent
2925 This causes @code{rm} to continue even if it is unable to remove a file.
2927 @cindex @code{-i}
2928 @cindex @code{--ignore-errors}
2929 @findex .IGNORE
2930 When you run @code{make} with the @samp{-i} or @samp{--ignore-errors}
2931 flag, errors are ignored in all commands of all rules.  A rule in the
2932 makefile for the special target @code{.IGNORE} has the same effect, if
2933 there are no dependencies.  These ways of ignoring errors are obsolete
2934 because @samp{-} is more flexible.
2936 When errors are to be ignored, because of either a @samp{-} or the
2937 @samp{-i} flag, @code{make} treats an error return just like success,
2938 except that it prints out a message that tells you the status code
2939 the command exited with, and says that the error has been ignored.
2941 When an error happens that @code{make} has not been told to ignore,
2942 it implies that the current target cannot be correctly remade, and neither
2943 can any other that depends on it either directly or indirectly.  No further
2944 commands will be executed for these targets, since their preconditions
2945 have not been achieved.
2948 @cindex @code{-k}
2949 @cindex @code{--keep-going}
2950 Normally @code{make} gives up immediately in this circumstance, returning a
2951 nonzero status.  However, if the @samp{-k} or @samp{--keep-going}
2952 flag is specified, @code{make}
2953 continues to consider the other dependencies of the pending targets,
2954 remaking them if necessary, before it gives up and returns nonzero status.
2955 For example, after an error in compiling one object file, @samp{make -k}
2956 will continue compiling other object files even though it already knows
2957 that linking them will be impossible.  @xref{Options Summary, ,Summary of Options}.
2959 The usual behavior assumes that your purpose is to get the specified
2960 targets up to date; once @code{make} learns that this is impossible, it
2961 might as well report the failure immediately.  The @samp{-k} option says
2962 that the real purpose is to test as many of the changes made in the
2963 program as possible, perhaps to find several independent problems so
2964 that you can correct them all before the next attempt to compile.  This
2965 is why Emacs' @code{compile} command passes the @samp{-k} flag by
2966 default.
2967 @cindex Emacs (@code{M-x compile})
2969 @findex .DELETE_ON_ERROR
2970 @cindex deletion of target files
2971 @cindex removal of target files
2972 @cindex target, deleting on error
2973 Usually when a command fails, if it has changed the target file at all,
2974 the file is corrupted and cannot be used---or at least it is not
2975 completely updated.  Yet the file's timestamp says that it is now up to
2976 date, so the next time @code{make} runs, it will not try to update that
2977 file.  The situation is just the same as when the command is killed by a
2978 signal; @pxref{Interrupts}.  So generally the right thing to do is to
2979 delete the target file if the command fails after beginning to change
2980 the file.  @code{make} will do this if @code{.DELETE_ON_ERROR} appears
2981 as a target.  This is almost always what you want @code{make} to do, but
2982 it is not historical practice; so for compatibility, you must explicitly
2983 request it.
2985 @node Interrupts, Recursion, Errors, Commands
2986 @section Interrupting or Killing @code{make}
2987 @cindex interrupt
2988 @cindex signal
2989 @cindex deletion of target files
2990 @cindex removal of target files
2991 @cindex target, deleting on interrupt
2992 @cindex killing (interruption)
2994 If @code{make} gets a fatal signal while a command is executing, it may
2995 delete the target file that the command was supposed to update.  This is
2996 done if the target file's last-modification time has changed since
2997 @code{make} first checked it.
2999 The purpose of deleting the target is to make sure that it is remade from
3000 scratch when @code{make} is next run.  Why is this?  Suppose you type
3001 @kbd{Ctrl-c} while a compiler is running, and it has begun to write an
3002 object file @file{foo.o}.  The @kbd{Ctrl-c} kills the compiler, resulting
3003 in an incomplete file whose last-modification time is newer than the source
3004 file @file{foo.c}.  But @code{make} also receives the @kbd{Ctrl-c} signal
3005 and deletes this incomplete file.  If @code{make} did not do this, the next
3006 invocation of @code{make} would think that @file{foo.o} did not require
3007 updating---resulting in a strange error message from the linker when it
3008 tries to link an object file half of which is missing.
3010 @findex .PRECIOUS
3011 You can prevent the deletion of a target file in this way by making the
3012 special target @code{.PRECIOUS} depend on it.  Before remaking a target,
3013 @code{make} checks to see whether it appears on the dependencies of
3014 @code{.PRECIOUS}, and thereby decides whether the target should be deleted
3015 if a signal happens.  Some reasons why you might do this are that the
3016 target is updated in some atomic fashion, or exists only to record a
3017 modification-time (its contents do not matter), or must exist at all
3018 times to prevent other sorts of trouble.
3020 @node Recursion, Sequences, Interrupts, Commands
3021 @section Recursive Use of @code{make}
3022 @cindex recursion
3023 @cindex subdirectories, recursion for
3025 Recursive use of @code{make} means using @code{make} as a command in a
3026 makefile.  This technique is useful when you want separate makefiles for
3027 various subsystems that compose a larger system.  For example, suppose you
3028 have a subdirectory @file{subdir} which has its own makefile, and you would
3029 like the containing directory's makefile to run @code{make} on the
3030 subdirectory.  You can do it by writing this:
3032 @example
3033 subsystem:
3034         cd subdir; $(MAKE)
3035 @end example
3037 @noindent
3038 or, equivalently, this (@pxref{Options Summary, ,Summary of Options}):
3040 @example
3041 subsystem:
3042         $(MAKE) -C subdir
3043 @end example
3044 @cindex @code{-C}
3045 @cindex @code{--directory}
3047 You can write recursive @code{make} commands just by copying this example,
3048 but there are many things to know about how they work and why, and about
3049 how the sub-@code{make} relates to the top-level @code{make}.
3051 @menu
3052 * MAKE Variable::               The special effects of using @samp{$(MAKE)}.
3053 * Variables/Recursion::         How to communicate variables to a sub-@code{make}.
3054 * Options/Recursion::           How to communicate options to a sub-@code{make}.
3055 * -w Option::                   How the @samp{-w} or @samp{--print-directory} option
3056                                  helps debug use of recursive @code{make} commands.
3057 @end menu
3059 @node MAKE Variable, Variables/Recursion,  , Recursion
3060 @subsection How the @code{MAKE} Variable Works
3061 @vindex MAKE
3062 @cindex recursion, and @code{MAKE} variable
3064 Recursive @code{make} commands should always use the variable @code{MAKE},
3065 not the explicit command name @samp{make}, as shown here:
3067 @example
3068 @group
3069 subsystem:
3070         cd subdir; $(MAKE)
3071 @end group
3072 @end example
3074 The value of this variable is the file name with which @code{make} was
3075 invoked.  If this file name was @file{/bin/make}, then the command executed
3076 is @samp{cd subdir; /bin/make}.  If you use a special version of
3077 @code{make} to run the top-level makefile, the same special version will be
3078 executed for recursive invocations.
3079 @cindex @code{cd} (shell command)
3081 As a special feature, using the variable @code{MAKE} in the commands of
3082 a rule alters the effects of the @samp{-t} (@samp{--touch}), @samp{-n}
3083 (@samp{--just-print}), or @samp{-q} (@w{@samp{--question}}) option.
3084 Using the @code{MAKE} variable has the same effect as using a @samp{+}
3085 character at the beginning of the command line.  @xref{Instead of
3086 Execution, ,Instead of Executing the Commands}.@refill
3088 Consider the command @samp{make -t} in the above example.  (The
3089 @samp{-t} option marks targets as up to date without actually running
3090 any commands; see @ref{Instead of Execution}.)  Following the usual
3091 definition of @samp{-t}, a @samp{make -t} command in the example would
3092 create a file named @file{subsystem} and do nothing else.  What you
3093 really want it to do is run @samp{@w{cd subdir;} @w{make -t}}; but that would
3094 require executing the command, and @samp{-t} says not to execute
3095 commands.@refill
3096 @cindex @code{-t}, and recursion
3097 @cindex recursion, and @code{-t}
3098 @cindex @code{--touch}, and recursion
3100 The special feature makes this do what you want: whenever a command
3101 line of a rule contains the variable @code{MAKE}, the flags @samp{-t},
3102 @samp{-n} and @samp{-q} do not apply to that line.  Command lines
3103 containing @code{MAKE} are executed normally despite the presence of a
3104 flag that causes most commands not to be run.  The usual
3105 @code{MAKEFLAGS} mechanism passes the flags to the sub-@code{make}
3106 (@pxref{Options/Recursion, ,Communicating Options to a
3107 Sub-@code{make}}), so your request to touch the files, or print the
3108 commands, is propagated to the subsystem.@refill
3110 @node Variables/Recursion, Options/Recursion, MAKE Variable, Recursion
3111 @subsection Communicating Variables to a Sub-@code{make}
3112 @cindex sub-@code{make}
3113 @cindex environment, and recursion
3114 @cindex exporting variables
3115 @cindex variables, environment
3116 @cindex variables, exporting
3117 @cindex recursion, and environment
3118 @cindex recursion, and variables
3120 Variable values of the top-level @code{make} can be passed to the
3121 sub-@code{make} through the environment by explicit request.  These
3122 variables are defined in the sub-@code{make} as defaults, but do not
3123 override what is specified in the makefile used by the sub-@code{make}
3124 makefile unless you use the @samp{-e} switch (@pxref{Options Summary,
3125 ,Summary of Options}).@refill
3127 To pass down, or @dfn{export}, a variable, @code{make} adds the variable
3128 and its value to the environment for running each command.  The
3129 sub-@code{make}, in turn, uses the environment to initialize its table
3130 of variable values.  @xref{Environment, ,Variables from the
3131 Environment}.
3133 Except by explicit request, @code{make} exports a variable only if it
3134 is either defined in the environment initially or set on the command
3135 line, and if its name consists only of letters, numbers, and underscores.
3136 Some shells cannot cope with environment variable names consisting of
3137 characters other than letters, numbers, and underscores.
3139 The special variables @code{SHELL} and @code{MAKEFLAGS} are always
3140 exported (unless you unexport them).
3141 @code{MAKEFILES} is exported if you set it to anything.
3143 @code{make} automatically passes down variable values that were defined
3144 on the command line, by putting them in the @code{MAKEFLAGS} variable.
3145 @iftex
3146 See the next section.
3147 @end iftex
3148 @ifinfo
3149 @xref{Options/Recursion}.
3150 @end ifinfo
3152 Variables are @emph{not} normally passed down if they were created by
3153 default by @code{make} (@pxref{Implicit Variables, ,Variables Used by
3154 Implicit Rules}).  The sub-@code{make} will define these for
3155 itself.@refill
3157 @findex export
3158 If you want to export specific variables to a sub-@code{make}, use the
3159 @code{export} directive, like this:
3161 @example
3162 export @var{variable} @dots{}
3163 @end example
3165 @noindent
3166 @findex unexport
3167 If you want to @emph{prevent} a variable from being exported, use the
3168 @code{unexport} directive, like this:
3170 @example
3171 unexport @var{variable} @dots{}
3172 @end example
3174 @noindent
3175 As a convenience, you can define a variable and export it at the same
3176 time by doing:
3178 @example
3179 export @var{variable} = value
3180 @end example
3182 @noindent
3183 has the same result as:
3185 @example
3186 @var{variable} = value
3187 export @var{variable}
3188 @end example
3190 @noindent
3193 @example
3194 export @var{variable} := value
3195 @end example
3197 @noindent
3198 has the same result as:
3200 @example
3201 @var{variable} := value
3202 export @var{variable}
3203 @end example
3205 Likewise,
3207 @example
3208 export @var{variable} += value
3209 @end example
3211 @noindent
3212 is just like:
3214 @example
3215 @var{variable} += value
3216 export @var{variable}
3217 @end example
3219 @noindent
3220 @xref{Appending, ,Appending More Text to Variables}.
3222 You may notice that the @code{export} and @code{unexport} directives
3223 work in @code{make} in the same way they work in the shell, @code{sh}.
3225 If you want all variables to be exported by default, you can use
3226 @code{export} by itself:
3228 @example
3229 export
3230 @end example
3232 @noindent
3233 This tells @code{make} that variables which are not explicitly mentioned
3234 in an @code{export} or @code{unexport} directive should be exported.
3235 Any variable given in an @code{unexport} directive will still @emph{not}
3236 be exported.  If you use @code{export} by itself to export variables by
3237 default, variables whose names contain characters other than
3238 alphanumerics and underscores will not be exported unless specifically
3239 mentioned in an @code{export} directive.@refill
3241 @findex .EXPORT_ALL_VARIABLES
3242 The behavior elicited by an @code{export} directive by itself was the
3243 default in older versions of GNU @code{make}.  If your makefiles depend
3244 on this behavior and you want to be compatible with old versions of
3245 @code{make}, you can write a rule for the special target
3246 @code{.EXPORT_ALL_VARIABLES} instead of using the @code{export} directive.
3247 This will be ignored by old @code{make}s, while the @code{export}
3248 directive will cause a syntax error.@refill
3249 @cindex compatibility in exporting
3251 Likewise, you can use @code{unexport} by itself to tell @code{make}
3252 @emph{not} to export variables by default.  Since this is the default
3253 behavior, you would only need to do this if @code{export} had been used
3254 by itself earlier (in an included makefile, perhaps).  You
3255 @strong{cannot} use @code{export} and @code{unexport} by themselves to
3256 have variables exported for some commands and not for others.  The last
3257 @code{export} or @code{unexport} directive that appears by itself
3258 determines the behavior for the entire run of @code{make}.@refill
3260 @vindex MAKELEVEL
3261 @cindex recursion, level of
3262 As a special feature, the variable @code{MAKELEVEL} is changed when it
3263 is passed down from level to level.  This variable's value is a string
3264 which is the depth of the level as a decimal number.  The value is
3265 @samp{0} for the top-level @code{make}; @samp{1} for a sub-@code{make},
3266 @samp{2} for a sub-sub-@code{make}, and so on.  The incrementation
3267 happens when @code{make} sets up the environment for a command.@refill
3269 The main use of @code{MAKELEVEL} is to test it in a conditional
3270 directive (@pxref{Conditionals, ,Conditional Parts of Makefiles}); this
3271 way you can write a makefile that behaves one way if run recursively and
3272 another way if run directly by you.@refill
3274 @vindex MAKEFILES
3275 You can use the variable @code{MAKEFILES} to cause all sub-@code{make}
3276 commands to use additional makefiles.  The value of @code{MAKEFILES} is
3277 a whitespace-separated list of file names.  This variable, if defined in
3278 the outer-level makefile, is passed down through the environment; then
3279 it serves as a list of extra makefiles for the sub-@code{make} to read
3280 before the usual or specified ones.  @xref{MAKEFILES Variable, ,The
3281 Variable @code{MAKEFILES}}.@refill
3283 @node Options/Recursion, -w Option, Variables/Recursion, Recursion
3284 @subsection Communicating Options to a Sub-@code{make}
3285 @cindex options, and recursion
3286 @cindex recursion, and options
3288 @vindex MAKEFLAGS
3289 Flags such as @samp{-s} and @samp{-k} are passed automatically to the
3290 sub-@code{make} through the variable @code{MAKEFLAGS}.  This variable is
3291 set up automatically by @code{make} to contain the flag letters that
3292 @code{make} received.  Thus, if you do @w{@samp{make -ks}} then
3293 @code{MAKEFLAGS} gets the value @samp{ks}.@refill
3295 As a consequence, every sub-@code{make} gets a value for @code{MAKEFLAGS}
3296 in its environment.  In response, it takes the flags from that value and
3297 processes them as if they had been given as arguments.
3298 @xref{Options Summary, ,Summary of Options}.
3300 @cindex command line variable definitions, and recursion
3301 @cindex variables, command line, and recursion
3302 @cindex recursion, and command line variable definitions
3303 Likewise variables defined on the command line are passed to the
3304 sub-@code{make} through @code{MAKEFLAGS}.  Words in the value of
3305 @code{MAKEFLAGS} that contain @samp{=}, @code{make} treats as variable
3306 definitions just as if they appeared on the command line.
3307 @xref{Overriding, ,Overriding Variables}.
3309 @cindex @code{-C}, and recursion
3310 @cindex @code{-f}, and recursion
3311 @cindex @code{-o}, and recursion
3312 @cindex @code{-W}, and recursion
3313 @cindex @code{--directory}, and recursion
3314 @cindex @code{--file}, and recursion
3315 @cindex @code{--old-file}, and recursion
3316 @cindex @code{--assume-old}, and recursion
3317 @cindex @code{--assume-new}, and recursion
3318 @cindex @code{--new-file}, and recursion
3319 @cindex recursion, and @code{-C}
3320 @cindex recursion, and @code{-f}
3321 @cindex recursion, and @code{-o}
3322 @cindex recursion, and @code{-W}
3323 The options @samp{-C}, @samp{-f}, @samp{-o}, and @samp{-W} are not put
3324 into @code{MAKEFLAGS}; these options are not passed down.@refill
3326 @cindex @code{-j}, and recursion
3327 @cindex @code{--jobs}, and recursion
3328 @cindex recursion, and @code{-j}
3329 @cindex job slots, and recursion
3330 The @samp{-j} option is a special case (@pxref{Parallel, ,Parallel Execution}).
3331 If you set it to some numeric value, @samp{-j 1} is always put into
3332 @code{MAKEFLAGS} instead of the value you specified.  This is because if
3333 the @w{@samp{-j}} option were passed down to sub-@code{make}s, you would
3334 get many more jobs running in parallel than you asked for.  If you give
3335 @samp{-j} with no numeric argument, meaning to run as many jobs as
3336 possible in parallel, this is passed down, since multiple infinities are
3337 no more than one.@refill
3339 If you do not want to pass the other flags down, you must change the
3340 value of @code{MAKEFLAGS}, like this:
3342 @example
3343 subsystem:
3344         cd subdir; $(MAKE) MAKEFLAGS=
3345 @end example
3347 @vindex MAKEOVERRIDES
3348 The command line variable definitions really appear in the variable
3349 @code{MAKEOVERRIDES}, and @code{MAKEFLAGS} contains a reference to this
3350 variable.  If you do want to pass flags down normally, but don't want to
3351 pass down the command line variable definitions, you can reset
3352 @code{MAKEOVERRIDES} to empty, like this:
3354 @example
3355 MAKEOVERRIDES =
3356 @end example
3358 @noindent
3359 @cindex Arg list too long
3360 @cindex E2BIG
3361 This is not usually useful to do.  However, some systems have a small
3362 fixed limit on the size of the environment, and putting so much
3363 information in into the value of @code{MAKEFLAGS} can exceed it.
3364 If you see the error message @samp{Arg list too long}, this may be the problem.
3365 @findex .POSIX
3366 @cindex POSIX.2
3367 (For strict compliance with POSIX.2, changing @code{MAKEOVERRIDES} does
3368 not affect @code{MAKEFLAGS} if the special target @samp{.POSIX} appears
3369 in the makefile.  You probably do not care about this.)
3371 @vindex MFLAGS
3372 A similar variable @code{MFLAGS} exists also, for historical
3373 compatibility.  It has the same value as @code{MAKEFLAGS} except that it
3374 does not contain the command line variable definitions, and it always
3375 begins with a hyphen unless it is empty (@code{MAKEFLAGS} begins with a
3376 hyphen only when it begins with an option that has no single-letter
3377 version, such as @samp{--warn-undefined-variables}).  @code{MFLAGS} was
3378 traditionally used explicitly in the recursive @code{make} command, like
3379 this:
3381 @example
3382 subsystem:
3383         cd subdir; $(MAKE) $(MFLAGS)
3384 @end example
3386 @noindent
3387 but now @code{MAKEFLAGS} makes this usage redundant.  If you want your
3388 makefiles to be compatible with old @code{make} programs, use this
3389 technique; it will work fine with more modern @code{make} versions too.
3391 @cindex setting options from environment
3392 @cindex options, setting from environment
3393 @cindex setting options in makefiles
3394 @cindex options, setting in makefiles
3395 The @code{MAKEFLAGS} variable can also be useful if you want to have
3396 certain options, such as @samp{-k} (@pxref{Options Summary, ,Summary of
3397 Options}), set each time you run @code{make}.  You simply put a value for
3398 @code{MAKEFLAGS} in your environment.  You can also set @code{MAKEFLAGS} in
3399 a makefile, to specify additional flags that should also be in effect for
3400 that makefile.  (Note that you cannot use @code{MFLAGS} this way.  That
3401 variable is set only for compatibility; @code{make} does not interpret a
3402 value you set for it in any way.)
3404 When @code{make} interprets the value of @code{MAKEFLAGS} (either from the
3405 environment or from a makefile), it first prepends a hyphen if the value
3406 does not already begin with one.  Then it chops the value into words
3407 separated by blanks, and parses these words as if they were options given
3408 on the command line (except that @samp{-C}, @samp{-f}, @samp{-h},
3409 @samp{-o}, @samp{-W}, and their long-named versions are ignored; and there
3410 is no error for an invalid option).
3412 If you do put @code{MAKEFLAGS} in your environment, you should be sure not
3413 to include any options that will drastically affect the actions of
3414 @code{make} and undermine the purpose of makefiles and of @code{make}
3415 itself.  For instance, the @samp{-t}, @samp{-n}, and @samp{-q} options, if
3416 put in one of these variables, could have disastrous consequences and would
3417 certainly have at least surprising and probably annoying effects.@refill
3419 @node -w Option,  , Options/Recursion, Recursion
3420 @subsection The @samp{--print-directory} Option
3421 @cindex directories, printing them
3422 @cindex printing directories
3423 @cindex recursion, and printing directories
3425 If you use several levels of recursive @code{make} invocations, the
3426 @samp{-w} or @w{@samp{--print-directory}} option can make the output a
3427 lot easier to understand by showing each directory as @code{make}
3428 starts processing it and as @code{make} finishes processing it.  For
3429 example, if @samp{make -w} is run in the directory @file{/u/gnu/make},
3430 @code{make} will print a line of the form:@refill
3432 @example
3433 make: Entering directory `/u/gnu/make'.
3434 @end example
3436 @noindent
3437 before doing anything else, and a line of the form:
3439 @example
3440 make: Leaving directory `/u/gnu/make'.
3441 @end example
3443 @noindent
3444 when processing is completed.
3446 @cindex @code{-C}, and @code{-w}
3447 @cindex @code{--directory}, and @code{--print-directory}
3448 @cindex recursion, and @code{-w}
3449 @cindex @code{-w}, and @code{-C}
3450 @cindex @code{-w}, and recursion
3451 @cindex @code{--print-directory}, and @code{--directory}
3452 @cindex @code{--print-directory}, and recursion
3453 @cindex @code{--no-print-directory}
3454 @cindex @code{--print-directory}, disabling
3455 @cindex @code{-w}, disabling
3456 Normally, you do not need to specify this option because @samp{make}
3457 does it for you: @samp{-w} is turned on automatically when you use the
3458 @samp{-C} option, and in sub-@code{make}s.  @code{make} will not
3459 automatically turn on @samp{-w} if you also use @samp{-s}, which says to
3460 be silent, or if you use @samp{--no-print-directory} to explicitly
3461 disable it.
3463 @node Sequences, Empty Commands, Recursion, Commands
3464 @section Defining Canned Command Sequences
3465 @cindex sequences of commands
3466 @cindex commands, sequences of
3468 When the same sequence of commands is useful in making various targets, you
3469 can define it as a canned sequence with the @code{define} directive, and
3470 refer to the canned sequence from the rules for those targets.  The canned
3471 sequence is actually a variable, so the name must not conflict with other
3472 variable names.
3474 Here is an example of defining a canned sequence of commands:
3476 @example
3477 define run-yacc
3478 yacc $(firstword $^)
3479 mv y.tab.c $@@
3480 endef
3481 @end example
3482 @cindex @code{yacc}
3484 @noindent
3485 Here @code{run-yacc} is the name of the variable being defined;
3486 @code{endef} marks the end of the definition; the lines in between are the
3487 commands.  The @code{define} directive does not expand variable references
3488 and function calls in the canned sequence; the @samp{$} characters,
3489 parentheses, variable names, and so on, all become part of the value of the
3490 variable you are defining.
3491 @xref{Defining, ,Defining Variables Verbatim},
3492 for a complete explanation of @code{define}.
3494 The first command in this example runs Yacc on the first dependency of
3495 whichever rule uses the canned sequence.  The output file from Yacc is
3496 always named @file{y.tab.c}.  The second command moves the output to the
3497 rule's target file name.
3499 To use the canned sequence, substitute the variable into the commands of a
3500 rule.  You can substitute it like any other variable
3501 (@pxref{Reference, ,Basics of Variable References}).
3502 Because variables defined by @code{define} are recursively expanded
3503 variables, all the variable references you wrote inside the @code{define}
3504 are expanded now.  For example:
3506 @example
3507 foo.c : foo.y
3508         $(run-yacc)
3509 @end example
3511 @noindent
3512 @samp{foo.y} will be substituted for the variable @samp{$^} when it occurs in
3513 @code{run-yacc}'s value, and @samp{foo.c} for @samp{$@@}.@refill
3515 This is a realistic example, but this particular one is not needed in
3516 practice because @code{make} has an implicit rule to figure out these
3517 commands based on the file names involved
3518 (@pxref{Implicit Rules, ,Using Implicit Rules}).
3520 @cindex @@, and @code{define}
3521 @cindex -, and @code{define}
3522 @cindex +, and @code{define}
3523 In command execution, each line of a canned sequence is treated just as
3524 if the line appeared on its own in the rule, preceded by a tab.  In
3525 particular, @code{make} invokes a separate subshell for each line.  You
3526 can use the special prefix characters that affect command lines
3527 (@samp{@@}, @samp{-}, and @samp{+}) on each line of a canned sequence.
3528 @xref{Commands, ,Writing the Commands in Rules}.
3529 For example, using this canned sequence:
3531 @example
3532 define frobnicate
3533 @@echo "frobnicating target $@@"
3534 frob-step-1 $< -o $@@-step-1
3535 frob-step-2 $@@-step-1 -o $@@
3536 endef
3537 @end example
3539 @noindent
3540 @code{make} will not echo the first line, the @code{echo} command.
3541 But it @emph{will} echo the following two command lines.
3543 On the other hand, prefix characters on the command line that refers to
3544 a canned sequence apply to every line in the sequence.  So the rule:
3546 @example
3547 frob.out: frob.in
3548         @@$(frobnicate)
3549 @end example
3551 @noindent
3552 does not echo @emph{any} commands.
3553 (@xref{Echoing, ,Command Echoing}, for a full explanation of @samp{@@}.)
3555 @node Empty Commands,  , Sequences, Commands
3556 @section Using Empty Commands
3557 @cindex empty commands
3558 @cindex commands, empty
3560 It is sometimes useful to define commands which do nothing.  This is done
3561 simply by giving a command that consists of nothing but whitespace.  For
3562 example:
3564 @example
3565 target: ;
3566 @end example
3568 @noindent
3569 defines an empty command string for @file{target}.  You could also use a
3570 line beginning with a tab character to define an empty command string,
3571 but this would be confusing because such a line looks empty.
3573 @findex .DEFAULT@r{, and empty commands}
3574 You may be wondering why you would want to define a command string that
3575 does nothing.  The only reason this is useful is to prevent a target
3576 from getting implicit commands (from implicit rules or the
3577 @code{.DEFAULT} special target; @pxref{Implicit Rules} and
3578 @pxref{Last Resort, ,Defining Last-Resort Default Rules}).@refill
3580 @c !!! another reason is for canonical stamp files:
3581 @ignore
3582 foo: stamp-foo ;
3583 stamp-foo: foo.in
3584         create foo frm foo.in
3585         touch $@
3586 @end ignore
3588 You may be inclined to define empty command strings for targets that are
3589 not actual files, but only exist so that their dependencies can be
3590 remade.  However, this is not the best way to do that, because the
3591 dependencies may not be remade properly if the target file actually does exist.
3592 @xref{Phony Targets, ,Phony Targets}, for a better way to do this.
3594 @node Using Variables, Conditionals, Commands, Top
3595 @chapter How to Use Variables
3596 @cindex variable
3597 @cindex value
3598 @cindex recursive variable expansion
3599 @cindex simple variable expansion
3601 A @dfn{variable} is a name defined in a makefile to represent a string
3602 of text, called the variable's @dfn{value}.  These values are
3603 substituted by explicit request into targets, dependencies, commands,
3604 and other parts of the makefile.  (In some other versions of @code{make},
3605 variables are called @dfn{macros}.)
3606 @cindex macro
3608 Variables and functions in all parts of a makefile are expanded when
3609 read, except for the shell commands in rules, the right-hand sides of
3610 variable definitions using @samp{=}, and the bodies of variable
3611 definitions using the @code{define} directive.@refill
3613 Variables can represent lists of file names, options to pass to compilers,
3614 programs to run, directories to look in for source files, directories to
3615 write output in, or anything else you can imagine.
3617 A variable name may be any sequence of characters not containing @samp{:},
3618 @samp{#}, @samp{=}, or leading or trailing whitespace.  However,
3619 variable names containing characters other than letters, numbers, and
3620 underscores should be avoided, as they may be given special meanings in the
3621 future, and with some shells they cannot be passed through the environment to a
3622 sub-@code{make}
3623 (@pxref{Variables/Recursion, ,Communicating Variables to a Sub-@code{make}}).
3625 Variable names are case-sensitive.  The names @samp{foo}, @samp{FOO},
3626 and @samp{Foo} all refer to different variables.
3628 It is traditional to use upper case letters in variable names, but we
3629 recommend using lower case letters for variable names that serve internal
3630 purposes in the makefile, and reserving upper case for parameters that
3631 control implicit rules or for parameters that the user should override with
3632 command options (@pxref{Overriding, ,Overriding Variables}).
3634 A few variables have names that are a single punctuation character or
3635 just a few characters.  These are the @dfn{automatic variables}, and
3636 they have particular specialized uses.  @xref{Automatic, ,Automatic Variables}.
3638 @menu
3639 * Reference::                   How to use the value of a variable.
3640 * Flavors::                     Variables come in two flavors.
3641 * Advanced::                    Advanced features for referencing a variable.
3642 * Values::                      All the ways variables get their values.
3643 * Setting::                     How to set a variable in the makefile.
3644 * Appending::                   How to append more text to the old value
3645                                   of a variable.
3646 * Override Directive::          How to set a variable in the makefile even if
3647                                   the user has set it with a command argument.
3648 * Defining::                    An alternate way to set a variable
3649                                   to a verbatim string.
3650 * Environment::                 Variable values can come from the environment.
3651 * Automatic::                   Some special variables have predefined
3652                                   meanings for use with implicit rules.
3653 @end menu
3655 @node Reference, Flavors,  , Using Variables
3656 @section Basics of Variable References
3657 @cindex variables, how to reference
3658 @cindex reference to variables
3659 @cindex @code{$}, in variable reference
3660 @cindex dollar sign (@code{$}), in variable reference
3662 To substitute a variable's value, write a dollar sign followed by the name
3663 of the variable in parentheses or braces: either @samp{$(foo)} or
3664 @samp{$@{foo@}} is a valid reference to the variable @code{foo}.  This
3665 special significance of @samp{$} is why you must write @samp{$$} to have
3666 the effect of a single dollar sign in a file name or command.
3668 Variable references can be used in any context: targets, dependencies,
3669 commands, most directives, and new variable values.  Here is an
3670 example of a common case, where a variable holds the names of all the
3671 object files in a program:
3673 @example
3674 @group
3675 objects = program.o foo.o utils.o
3676 program : $(objects)
3677         cc -o program $(objects)
3679 $(objects) : defs.h
3680 @end group
3681 @end example
3683 Variable references work by strict textual substitution.  Thus, the rule
3685 @example
3686 @group
3687 foo = c
3688 prog.o : prog.$(foo)
3689         $(foo)$(foo) -$(foo) prog.$(foo)
3690 @end group
3691 @end example
3693 @noindent
3694 could be used to compile a C program @file{prog.c}.  Since spaces before
3695 the variable value are ignored in variable assignments, the value of
3696 @code{foo} is precisely @samp{c}.  (Don't actually write your makefiles
3697 this way!)
3699 A dollar sign followed by a character other than a dollar sign,
3700 open-parenthesis or open-brace treats that single character as the
3701 variable name.  Thus, you could reference the variable @code{x} with
3702 @samp{$x}.  However, this practice is strongly discouraged, except in
3703 the case of the automatic variables (@pxref{Automatic, ,Automatic Variables}).
3705 @node Flavors, Advanced, Reference, Using Variables
3706 @section The Two Flavors of Variables
3707 @cindex flavors of variables
3708 @cindex recursive variable expansion
3709 @cindex variables, flavors
3710 @cindex recursively expanded variables
3711 @cindex variables, recursively expanded
3713 There are two ways that a variable in GNU @code{make} can have a value;
3714 we call them the two @dfn{flavors} of variables.  The two flavors are
3715 distinguished in how they are defined and in what they do when expanded.
3717 @cindex =
3718 The first flavor of variable is a @dfn{recursively expanded} variable.
3719 Variables of this sort are defined by lines using @samp{=}
3720 (@pxref{Setting, ,Setting Variables}) or by the @code{define} directive
3721 (@pxref{Defining, ,Defining Variables Verbatim}).  The value you specify
3722 is installed verbatim; if it contains references to other variables,
3723 these references are expanded whenever this variable is substituted (in
3724 the course of expanding some other string).  When this happens, it is
3725 called @dfn{recursive expansion}.@refill
3727 For example,
3729 @example
3730 foo = $(bar)
3731 bar = $(ugh)
3732 ugh = Huh?
3734 all:;echo $(foo)
3735 @end example
3737 @noindent
3738 will echo @samp{Huh?}: @samp{$(foo)} expands to @samp{$(bar)} which
3739 expands to @samp{$(ugh)} which finally expands to @samp{Huh?}.@refill
3741 This flavor of variable is the only sort supported by other versions of
3742 @code{make}.  It has its advantages and its disadvantages.  An advantage
3743 (most would say) is that:
3745 @example
3746 CFLAGS = $(include_dirs) -O
3747 include_dirs = -Ifoo -Ibar
3748 @end example
3750 @noindent
3751 will do what was intended: when @samp{CFLAGS} is expanded in a command,
3752 it will expand to @samp{-Ifoo -Ibar -O}.  A major disadvantage is that you
3753 cannot append something on the end of a variable, as in
3755 @example
3756 CFLAGS = $(CFLAGS) -O
3757 @end example
3759 @noindent
3760 because it will cause an infinite loop in the variable expansion.
3761 (Actually @code{make} detects the infinite loop and reports an error.)
3762 @cindex loops in variable expansion
3763 @cindex variables, loops in expansion
3765 Another disadvantage is that any functions
3766 (@pxref{Functions, ,Functions for Transforming Text})
3767 referenced in the definition will be executed every time the variable is
3768 expanded.  This makes @code{make} run slower; worse, it causes the
3769 @code{wildcard} and @code{shell} functions to give unpredictable results
3770 because you cannot easily control when they are called, or even how many
3771 times.
3773 To avoid all the problems and inconveniences of recursively expanded
3774 variables, there is another flavor: simply expanded variables.
3776 @cindex simply expanded variables
3777 @cindex variables, simply expanded
3778 @cindex :=
3779 @dfn{Simply expanded variables} are defined by lines using @samp{:=}
3780 (@pxref{Setting, ,Setting Variables}).
3781 The value of a simply expanded variable is scanned
3782 once and for all, expanding any references to other variables and
3783 functions, when the variable is defined.  The actual value of the simply
3784 expanded variable is the result of expanding the text that you write.
3785 It does not contain any references to other variables; it contains their
3786 values @emph{as of the time this variable was defined}.  Therefore,
3788 @example
3789 x := foo
3790 y := $(x) bar
3791 x := later
3792 @end example
3794 @noindent
3795 is equivalent to
3797 @example
3798 y := foo bar
3799 x := later
3800 @end example
3802 When a simply expanded variable is referenced, its value is substituted
3803 verbatim.
3805 Here is a somewhat more complicated example, illustrating the use of
3806 @samp{:=} in conjunction with the @code{shell} function.
3807 (@xref{Shell Function, , The @code{shell} Function}.)  This example
3808 also shows use of the variable @code{MAKELEVEL}, which is changed
3809 when it is passed down from level to level.
3810 (@xref{Variables/Recursion, , Communicating Variables to a
3811 Sub-@code{make}}, for information about @code{MAKELEVEL}.)
3813 @vindex MAKELEVEL
3814 @vindex MAKE
3815 @example
3816 @group
3817 ifeq (0,$@{MAKELEVEL@})
3818 cur-dir   := $(shell pwd)
3819 whoami    := $(shell whoami)
3820 host-type := $(shell arch)
3821 MAKE := $@{MAKE@} host-type=$@{host-type@} whoami=$@{whoami@}
3822 endif
3823 @end group
3824 @end example
3826 @noindent
3827 An advantage of this use of @samp{:=} is that a typical
3828 `descend into a directory' command then looks like this:
3830 @example
3831 @group
3832 $@{subdirs@}:
3833       $@{MAKE@} cur-dir=$@{cur-dir@}/$@@ -C $@@ all
3834 @end group
3835 @end example
3837 Simply expanded variables generally make complicated makefile programming
3838 more predictable because they work like variables in most programming
3839 languages.  They allow you to redefine a variable using its own value (or
3840 its value processed in some way by one of the expansion functions) and to
3841 use the expansion functions much more efficiently
3842 (@pxref{Functions, ,Functions for Transforming Text}).
3844 @cindex spaces, in variable values
3845 @cindex whitespace, in variable values
3846 @cindex variables, spaces in values
3847 You can also use them to introduce controlled leading whitespace into
3848 variable values.  Leading whitespace characters are discarded from your
3849 input before substitution of variable references and function calls;
3850 this means you can include leading spaces in a variable value by
3851 protecting them with variable references, like this:
3853 @example
3854 nullstring :=
3855 space := $(nullstring) # end of the line
3856 @end example
3858 @noindent
3859 Here the value of the variable @code{space} is precisely one space.  The
3860 comment @w{@samp{# end of the line}} is included here just for clarity.
3861 Since trailing space characters are @emph{not} stripped from variable
3862 values, just a space at the end of the line would have the same effect
3863 (but be rather hard to read).  If you put whitespace at the end of a
3864 variable value, it is a good idea to put a comment like that at the end
3865 of the line to make your intent clear.  Conversely, if you do @emph{not}
3866 want any whitespace characters at the end of your variable value, you
3867 must remember not to put a random comment on the end of the line after
3868 some whitespace, such as this:
3870 @example
3871 dir := /foo/bar    # directory to put the frobs in
3872 @end example
3874 @noindent
3875 Here the value of the variable @code{dir} is @w{@samp{/foo/bar    }}
3876 (with four trailing spaces), which was probably not the intention.
3877 (Imagine something like @w{@samp{$(dir)/file}} with this definition!)
3879 @node Advanced, Values, Flavors, Using Variables
3880 @section Advanced Features for Reference to Variables
3881 @cindex reference to variables
3883 This section describes some advanced features you can use to reference
3884 variables in more flexible ways.
3886 @menu
3887 * Substitution Refs::           Referencing a variable with
3888                                   substitutions on the value.
3889 * Computed Names::              Computing the name of the variable to refer to.
3890 @end menu
3892 @node Substitution Refs, Computed Names,  , Advanced
3893 @subsection Substitution References
3894 @cindex modified variable reference
3895 @cindex substitution variable reference
3896 @cindex variables, modified reference
3897 @cindex variables, substitution reference
3899 @cindex variables, substituting suffix in
3900 @cindex suffix, substituting in variables
3901 A @dfn{substitution reference} substitutes the value of a variable with
3902 alterations that you specify.  It has the form
3903 @samp{$(@var{var}:@var{a}=@var{b})} (or
3904 @samp{$@{@var{var}:@var{a}=@var{b}@}}) and its meaning is to take the value
3905 of the variable @var{var}, replace every @var{a} at the end of a word with
3906 @var{b} in that value, and substitute the resulting string.
3908 When we say ``at the end of a word'', we mean that @var{a} must appear
3909 either followed by whitespace or at the end of the value in order to be
3910 replaced; other occurrences of @var{a} in the value are unaltered.  For
3911 example:@refill
3913 @example
3914 foo := a.o b.o c.o
3915 bar := $(foo:.o=.c)
3916 @end example
3918 @noindent
3919 sets @samp{bar} to @samp{a.c b.c c.c}.  @xref{Setting, ,Setting Variables}.
3921 A substitution reference is actually an abbreviation for use of the
3922 @code{patsubst} expansion function (@pxref{Text Functions, ,Functions for String Substitution and Analysis}).  We provide
3923 substitution references as well as @code{patsubst} for compatibility with
3924 other implementations of @code{make}.
3926 @findex patsubst
3927 Another type of substitution reference lets you use the full power of
3928 the @code{patsubst} function.  It has the same form
3929 @samp{$(@var{var}:@var{a}=@var{b})} described above, except that now
3930 @var{a} must contain a single @samp{%} character.  This case is
3931 equivalent to @samp{$(patsubst @var{a},@var{b},$(@var{var}))}.
3932 @xref{Text Functions, ,Functions for String Substitution and Analysis},
3933 for a description of the @code{patsubst} function.@refill
3935 @example
3936 @group
3937 @exdent For example:
3939 foo := a.o b.o c.o
3940 bar := $(foo:%.o=%.c)
3941 @end group
3942 @end example
3944 @noindent
3945 sets @samp{bar} to @samp{a.c b.c c.c}.
3947 @node Computed Names,  , Substitution Refs, Advanced
3948 @subsection Computed Variable Names
3949 @cindex nested variable reference
3950 @cindex computed variable name
3951 @cindex variables, computed names
3952 @cindex variables, nested references
3953 @cindex variables, @samp{$} in name
3954 @cindex @code{$}, in variable name
3955 @cindex dollar sign (@code{$}), in variable name
3957 Computed variable names are a complicated concept needed only for
3958 sophisticated makefile programming.  For most purposes you need not
3959 consider them, except to know that making a variable with a dollar sign
3960 in its name might have strange results.  However, if you are the type
3961 that wants to understand everything, or you are actually interested in
3962 what they do, read on.
3964 Variables may be referenced inside the name of a variable.  This is
3965 called a @dfn{computed variable name} or a @dfn{nested variable
3966 reference}.  For example,
3968 @example
3969 x = y
3970 y = z
3971 a := $($(x))
3972 @end example
3974 @noindent
3975 defines @code{a} as @samp{z}: the @samp{$(x)} inside @samp{$($(x))} expands
3976 to @samp{y}, so @samp{$($(x))} expands to @samp{$(y)} which in turn expands
3977 to @samp{z}.  Here the name of the variable to reference is not stated
3978 explicitly; it is computed by expansion of @samp{$(x)}.  The reference
3979 @samp{$(x)} here is nested within the outer variable reference.
3981 The previous example shows two levels of nesting, but any number of levels
3982 is possible.  For example, here are three levels:
3984 @example
3985 x = y
3986 y = z
3987 z = u
3988 a := $($($(x)))
3989 @end example
3991 @noindent
3992 Here the innermost @samp{$(x)} expands to @samp{y}, so @samp{$($(x))}
3993 expands to @samp{$(y)} which in turn expands to @samp{z}; now we have
3994 @samp{$(z)}, which becomes @samp{u}.
3996 References to recursively-expanded variables within a variable name are
3997 reexpanded in the usual fashion.  For example:
3999 @example
4000 x = $(y)
4001 y = z
4002 z = Hello
4003 a := $($(x))
4004 @end example
4006 @noindent
4007 defines @code{a} as @samp{Hello}: @samp{$($(x))} becomes @samp{$($(y))}
4008 which becomes @samp{$(z)} which becomes @samp{Hello}.
4010 Nested variable references can also contain modified references and
4011 function invocations (@pxref{Functions, ,Functions for Transforming Text}),
4012 just like any other reference.
4013 For example, using the @code{subst} function
4014 (@pxref{Text Functions, ,Functions for String Substitution and Analysis}):
4016 @example
4017 @group
4018 x = variable1
4019 variable2 := Hello
4020 y = $(subst 1,2,$(x))
4021 z = y
4022 a := $($($(z)))
4023 @end group
4024 @end example
4026 @noindent
4027 eventually defines @code{a} as @samp{Hello}.  It is doubtful that anyone
4028 would ever want to write a nested reference as convoluted as this one, but
4029 it works: @samp{$($($(z)))} expands to @samp{$($(y))} which becomes
4030 @samp{$($(subst 1,2,$(x)))}.  This gets the value @samp{variable1} from
4031 @code{x} and changes it by substitution to @samp{variable2}, so that the
4032 entire string becomes @samp{$(variable2)}, a simple variable reference
4033 whose value is @samp{Hello}.@refill
4035 A computed variable name need not consist entirely of a single variable
4036 reference.  It can contain several variable references, as well as some
4037 invariant text.  For example,
4039 @example
4040 @group
4041 a_dirs := dira dirb
4042 1_dirs := dir1 dir2
4043 @end group
4045 @group
4046 a_files := filea fileb
4047 1_files := file1 file2
4048 @end group
4050 @group
4051 ifeq "$(use_a)" "yes"
4052 a1 := a
4053 else
4054 a1 := 1
4055 endif
4056 @end group
4058 @group
4059 ifeq "$(use_dirs)" "yes"
4060 df := dirs
4061 else
4062 df := files
4063 endif
4065 dirs := $($(a1)_$(df))
4066 @end group
4067 @end example
4069 @noindent
4070 will give @code{dirs} the same value as @code{a_dirs}, @code{1_dirs},
4071 @code{a_files} or @code{1_files} depending on the settings of @code{use_a}
4072 and @code{use_dirs}.@refill
4074 Computed variable names can also be used in substitution references:
4076 @example
4077 @group
4078 a_objects := a.o b.o c.o
4079 1_objects := 1.o 2.o 3.o
4081 sources := $($(a1)_objects:.o=.c)
4082 @end group
4083 @end example
4085 @noindent
4086 defines @code{sources} as either @samp{a.c b.c c.c} or @samp{1.c 2.c 3.c},
4087 depending on the value of @code{a1}.
4089 The only restriction on this sort of use of nested variable references
4090 is that they cannot specify part of the name of a function to be called.
4091 This is because the test for a recognized function name is done before
4092 the expansion of nested references.  For example,
4094 @example
4095 @group
4096 ifdef do_sort
4097 func := sort
4098 else
4099 func := strip
4100 endif
4101 @end group
4103 @group
4104 bar := a d b g q c
4105 @end group
4107 @group
4108 foo := $($(func) $(bar))
4109 @end group
4110 @end example
4112 @noindent
4113 attempts to give @samp{foo} the value of the variable @samp{sort a d b g
4114 q c} or @samp{strip a d b g q c}, rather than giving @samp{a d b g q c}
4115 as the argument to either the @code{sort} or the @code{strip} function.
4116 This restriction could be removed in the future if that change is shown
4117 to be a good idea.
4119 You can also use computed variable names in the left-hand side of a
4120 variable assignment, or in a @code{define} directive, as in:
4122 @example
4123 dir = foo
4124 $(dir)_sources := $(wildcard $(dir)/*.c)
4125 define $(dir)_print
4126 lpr $($(dir)_sources)
4127 endef
4128 @end example
4130 @noindent
4131 This example defines the variables @samp{dir}, @samp{foo_sources}, and
4132 @samp{foo_print}.
4134 Note that @dfn{nested variable references} are quite different from
4135 @dfn{recursively expanded variables}
4136 (@pxref{Flavors, ,The Two Flavors of Variables}), though both are
4137 used together in complex ways when doing makefile programming.@refill
4139 @node Values, Setting, Advanced, Using Variables
4140 @section How Variables Get Their Values
4141 @cindex variables, how they get their values
4142 @cindex value, how a variable gets it
4144 Variables can get values in several different ways:
4146 @itemize @bullet
4147 @item
4148 You can specify an overriding value when you run @code{make}.
4149 @xref{Overriding, ,Overriding Variables}.
4151 @item
4152 You can specify a value in the makefile, either
4153 with an assignment (@pxref{Setting, ,Setting Variables}) or with a
4154 verbatim definition (@pxref{Defining, ,Defining Variables Verbatim}).@refill
4156 @item
4157 Variables in the environment become @code{make} variables.
4158 @xref{Environment, ,Variables from the Environment}.
4160 @item
4161 Several @dfn{automatic} variables are given new values for each rule.
4162 Each of these has a single conventional use.
4163 @xref{Automatic, ,Automatic Variables}.
4165 @item
4166 Several variables have constant initial values.
4167 @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
4168 @end itemize
4170 @node Setting, Appending, Values, Using Variables
4171 @section Setting Variables
4172 @cindex setting variables
4173 @cindex variables, setting
4174 @cindex =
4175 @cindex :=
4177 To set a variable from the makefile, write a line starting with the
4178 variable name followed by @samp{=} or @samp{:=}.  Whatever follows the
4179 @samp{=} or @samp{:=} on the line becomes the value.  For example,
4181 @example
4182 objects = main.o foo.o bar.o utils.o
4183 @end example
4185 @noindent
4186 defines a variable named @code{objects}.  Whitespace around the variable
4187 name and immediately after the @samp{=} is ignored.
4189 Variables defined with @samp{=} are @dfn{recursively expanded} variables.
4190 Variables defined with @samp{:=} are @dfn{simply expanded} variables; these
4191 definitions can contain variable references which will be expanded before
4192 the definition is made.  @xref{Flavors, ,The Two Flavors of Variables}.
4194 The variable name may contain function and variable references, which
4195 are expanded when the line is read to find the actual variable name to use.
4197 There is no limit on the length of the value of a variable except the
4198 amount of swapping space on the computer.  When a variable definition is
4199 long, it is a good idea to break it into several lines by inserting
4200 backslash-newline at convenient places in the definition.  This will not
4201 affect the functioning of @code{make}, but it will make the makefile easier
4202 to read.
4204 Most variable names are considered to have the empty string as a value if
4205 you have never set them.  Several variables have built-in initial values
4206 that are not empty, but you can set them in the usual ways
4207 (@pxref{Implicit Variables, ,Variables Used by Implicit Rules}).
4208 Several special variables are set
4209 automatically to a new value for each rule; these are called the
4210 @dfn{automatic} variables (@pxref{Automatic, ,Automatic Variables}).
4212 @node Appending, Override Directive, Setting, Using Variables
4213 @section Appending More Text to Variables
4214 @cindex +=
4215 @cindex appending to variables
4216 @cindex variables, appending to
4218 Often it is useful to add more text to the value of a variable already defined.
4219 You do this with a line containing @samp{+=}, like this:
4221 @example
4222 objects += another.o
4223 @end example
4225 @noindent
4226 This takes the value of the variable @code{objects}, and adds the text
4227 @samp{another.o} to it (preceded by a single space).  Thus:
4229 @example
4230 objects = main.o foo.o bar.o utils.o
4231 objects += another.o
4232 @end example
4234 @noindent
4235 sets @code{objects} to @samp{main.o foo.o bar.o utils.o another.o}.
4237 Using @samp{+=} is similar to:
4239 @example
4240 objects = main.o foo.o bar.o utils.o
4241 objects := $(objects) another.o
4242 @end example
4244 @noindent
4245 but differs in ways that become important when you use more complex values.
4247 When the variable in question has not been defined before, @samp{+=}
4248 acts just like normal @samp{=}: it defines a recursively-expanded
4249 variable.  However, when there @emph{is} a previous definition, exactly
4250 what @samp{+=} does depends on what flavor of variable you defined
4251 originally.  @xref{Flavors, ,The Two Flavors of Variables}, for an
4252 explanation of the two flavors of variables.
4254 When you add to a variable's value with @samp{+=}, @code{make} acts
4255 essentially as if you had included the extra text in the initial
4256 definition of the variable.  If you defined it first with @samp{:=},
4257 making it a simply-expanded variable, @samp{+=} adds to that
4258 simply-expanded definition, and expands the new text before appending it
4259 to the old value just as @samp{:=} does
4260 (@pxref{Setting, ,Setting Variables}, for a full explanation of @samp{:=}).
4261 In fact,
4263 @example
4264 variable := value
4265 variable += more
4266 @end example
4268 @noindent
4269 is exactly equivalent to:
4271 @noindent
4272 @example
4273 variable := value
4274 variable := $(variable) more
4275 @end example
4277 On the other hand, when you use @samp{+=} with a variable that you defined
4278 first to be recursively-expanded using plain @samp{=}, @code{make} does
4279 something a bit different.  Recall that when you define a
4280 recursively-expanded variable, @code{make} does not expand the value you set
4281 for variable and function references immediately.  Instead it stores the text
4282 verbatim, and saves these variable and function references to be expanded
4283 later, when you refer to the new variable (@pxref{Flavors, ,The Two Flavors
4284 of Variables}).  When you use @samp{+=} on a recursively-expanded variable,
4285 it is this unexpanded text to which @code{make} appends the new text you
4286 specify.
4288 @example
4289 @group
4290 variable = value
4291 variable += more
4292 @end group
4293 @end example
4295 @noindent
4296 is roughly equivalent to:
4298 @example
4299 @group
4300 temp = value
4301 variable = $(temp) more
4302 @end group
4303 @end example
4305 @noindent
4306 except that of course it never defines a variable called @code{temp}.
4307 The importance of this comes when the variable's old value contains
4308 variable references.  Take this common example:
4310 @example
4311 CFLAGS = $(includes) -O
4312 @dots{}
4313 CFLAGS += -pg # enable profiling
4314 @end example
4316 @noindent
4317 The first line defines the @code{CFLAGS} variable with a reference to another
4318 variable, @code{includes}.  (@code{CFLAGS} is used by the rules for C
4319 compilation; @pxref{Catalogue of Rules, ,Catalogue of Implicit Rules}.)
4320 Using @samp{=} for the definition makes @code{CFLAGS} a recursively-expanded
4321 variable, meaning @w{@samp{$(includes) -O}} is @emph{not} expanded when
4322 @code{make} processes the definition of @code{CFLAGS}.  Thus, @code{includes}
4323 need not be defined yet for its value to take effect.  It only has to be
4324 defined before any reference to @code{CFLAGS}.  If we tried to append to the
4325 value of @code{CFLAGS} without using @samp{+=}, we might do it like this:
4327 @example
4328 CFLAGS := $(CFLAGS) -pg # enable profiling
4329 @end example
4331 @noindent
4332 This is pretty close, but not quite what we want.  Using @samp{:=}
4333 redefines @code{CFLAGS} as a simply-expanded variable; this means
4334 @code{make} expands the text @w{@samp{$(CFLAGS) -pg}} before setting the
4335 variable.  If @code{includes} is not yet defined, we get @w{@samp{ -O
4336 -pg}}, and a later definition of @code{includes} will have no effect.
4337 Conversely, by using @samp{+=} we set @code{CFLAGS} to the
4338 @emph{unexpanded} value @w{@samp{$(includes) -O -pg}}.  Thus we preserve
4339 the reference to @code{includes}, so if that variable gets defined at
4340 any later point, a reference like @samp{$(CFLAGS)} still uses its
4341 value.
4343 @node Override Directive, Defining, Appending, Using Variables
4344 @section The @code{override} Directive
4345 @findex override
4346 @cindex overriding with @code{override}
4347 @cindex variables, overriding
4349 If a variable has been set with a command argument
4350 (@pxref{Overriding, ,Overriding Variables}),
4351 then ordinary assignments in the makefile are ignored.  If you want to set
4352 the variable in the makefile even though it was set with a command
4353 argument, you can use an @code{override} directive, which is a line that
4354 looks like this:@refill
4356 @example
4357 override @var{variable} = @var{value}
4358 @end example
4360 @noindent
4363 @example
4364 override @var{variable} := @var{value}
4365 @end example
4367 To append more text to a variable defined on the command line, use:
4369 @example
4370 override @var{variable} += @var{more text}
4371 @end example
4373 @noindent
4374 @xref{Appending, ,Appending More Text to Variables}.
4376 The @code{override} directive was not invented for escalation in the war
4377 between makefiles and command arguments.  It was invented so you can alter
4378 and add to values that the user specifies with command arguments.
4380 For example, suppose you always want the @samp{-g} switch when you run the
4381 C compiler, but you would like to allow the user to specify the other
4382 switches with a command argument just as usual.  You could use this
4383 @code{override} directive:
4385 @example
4386 override CFLAGS += -g
4387 @end example
4389 You can also use @code{override} directives with @code{define} directives.
4390 This is done as you might expect:
4392 @example
4393 override define foo
4395 endef
4396 @end example
4398 @noindent
4399 @iftex
4400 See the next section for information about @code{define}.
4401 @end iftex
4402 @ifinfo
4403 @xref{Defining, ,Defining Variables Verbatim}.
4404 @end ifinfo
4406 @node Defining, Environment, Override Directive, Using Variables
4407 @section Defining Variables Verbatim
4408 @findex define
4409 @findex endef
4410 @cindex verbatim variable definition
4411 @cindex defining variables verbatim
4412 @cindex variables, defining verbatim
4414 Another way to set the value of a variable is to use the @code{define}
4415 directive.  This directive has an unusual syntax which allows newline
4416 characters to be included in the value, which is convenient for defining
4417 canned sequences of commands
4418 (@pxref{Sequences, ,Defining Canned Command Sequences}).
4420 The @code{define} directive is followed on the same line by the name of the
4421 variable and nothing more.  The value to give the variable appears on the
4422 following lines.  The end of the value is marked by a line containing just
4423 the word @code{endef}.  Aside from this difference in syntax, @code{define}
4424 works just like @samp{=}: it creates a recursively-expanded variable
4425 (@pxref{Flavors, ,The Two Flavors of Variables}).
4426 The variable name may contain function and variable references, which
4427 are expanded when the directive is read to find the actual variable name
4428 to use.
4430 @example
4431 define two-lines
4432 echo foo
4433 echo $(bar)
4434 endef
4435 @end example
4437 The value in an ordinary assignment cannot contain a newline; but the
4438 newlines that separate the lines of the value in a @code{define} become
4439 part of the variable's value (except for the final newline which precedes
4440 the @code{endef} and is not considered part of the value).@refill
4442 @need 800
4443 The previous example is functionally equivalent to this:
4445 @example
4446 two-lines = echo foo; echo $(bar)
4447 @end example
4449 @noindent
4450 since two commands separated by semicolon behave much like two separate
4451 shell commands.  However, note that using two separate lines means
4452 @code{make} will invoke the shell twice, running an independent subshell
4453 for each line.  @xref{Execution, ,Command Execution}.
4455 If you want variable definitions made with @code{define} to take
4456 precedence over command-line variable definitions, you can use the
4457 @code{override} directive together with @code{define}:
4459 @example
4460 override define two-lines
4462 $(bar)
4463 endef
4464 @end example
4466 @noindent
4467 @xref{Override Directive, ,The @code{override} Directive}.
4469 @node Environment,  , Defining, Using Variables
4470 @section Variables from the Environment
4472 @cindex variables, environment
4473 @cindex environment
4474 Variables in @code{make} can come from the environment in which
4475 @code{make} is run.  Every environment variable that @code{make} sees when
4476 it starts up is transformed into a @code{make} variable with the same name
4477 and value.  But an explicit assignment in the makefile, or with a command
4478 argument, overrides the environment.  (If the @samp{-e} flag is specified,
4479 then values from the environment override assignments in the makefile.
4480 @xref{Options Summary, ,Summary of Options}.
4481 But this is not recommended practice.)
4483 Thus, by setting the variable @code{CFLAGS} in your environment, you can
4484 cause all C compilations in most makefiles to use the compiler switches you
4485 prefer.  This is safe for variables with standard or conventional meanings
4486 because you know that no makefile will use them for other things.  (But
4487 this is not totally reliable; some makefiles set @code{CFLAGS} explicitly
4488 and therefore are not affected by the value in the environment.)
4490 When @code{make} is invoked recursively, variables defined in the
4491 outer invocation can be passed to inner invocations through the
4492 environment (@pxref{Recursion, ,Recursive Use of @code{make}}).  By
4493 default, only variables that came from the environment or the command
4494 line are passed to recursive invocations.  You can use the
4495 @code{export} directive to pass other variables.
4496 @xref{Variables/Recursion, , Communicating Variables to a
4497 Sub-@code{make}}, for full details.
4499 Other use of variables from the environment is not recommended.  It is not
4500 wise for makefiles to depend for their functioning on environment variables
4501 set up outside their control, since this would cause different users to get
4502 different results from the same makefile.  This is against the whole
4503 purpose of most makefiles.
4505 Such problems would be especially likely with the variable @code{SHELL},
4506 which is normally present in the environment to specify the user's choice
4507 of interactive shell.  It would be very undesirable for this choice to
4508 affect @code{make}.  So @code{make} ignores the environment value of
4509 @code{SHELL}.@refill
4511 @node Conditionals, Functions, Using Variables, Top
4512 @chapter Conditional Parts of Makefiles
4514 @cindex conditionals
4515 A @dfn{conditional} causes part of a makefile to be obeyed or ignored
4516 depending on the values of variables.  Conditionals can compare the
4517 value of one variable to another, or the value of a variable to
4518 a constant string.  Conditionals control what @code{make} actually
4519 ``sees'' in the makefile, so they @emph{cannot} be used to control shell
4520 commands at the time of execution.@refill
4522 @menu
4523 * Conditional Example::         Example of a conditional
4524 * Conditional Syntax::          The syntax of conditionals.
4525 * Testing Flags::               Conditionals that test flags.
4526 @end menu
4528 @node Conditional Example, Conditional Syntax,  , Conditionals
4529 @section Example of a Conditional
4531 The following example of a conditional tells @code{make} to use one set
4532 of libraries if the @code{CC} variable is @samp{gcc}, and a different
4533 set of libraries otherwise.  It works by controlling which of two
4534 command lines will be used as the command for a rule.  The result is
4535 that @samp{CC=gcc} as an argument to @code{make} changes not only which
4536 compiler is used but also which libraries are linked.
4538 @example
4539 libs_for_gcc = -lgnu
4540 normal_libs =
4542 foo: $(objects)
4543 ifeq ($(CC),gcc)
4544         $(CC) -o foo $(objects) $(libs_for_gcc)
4545 else
4546         $(CC) -o foo $(objects) $(normal_libs)
4547 endif
4548 @end example
4550 This conditional uses three directives: one @code{ifeq}, one @code{else}
4551 and one @code{endif}.
4553 The @code{ifeq} directive begins the conditional, and specifies the
4554 condition.  It contains two arguments, separated by a comma and surrounded
4555 by parentheses.  Variable substitution is performed on both arguments and
4556 then they are compared.  The lines of the makefile following the
4557 @code{ifeq} are obeyed if the two arguments match; otherwise they are
4558 ignored.
4560 The @code{else} directive causes the following lines to be obeyed if the
4561 previous conditional failed.  In the example above, this means that the
4562 second alternative linking command is used whenever the first alternative
4563 is not used.  It is optional to have an @code{else} in a conditional.
4565 The @code{endif} directive ends the conditional.  Every conditional must
4566 end with an @code{endif}.  Unconditional makefile text follows.
4568 As this example illustrates, conditionals work at the textual level:
4569 the lines of the conditional are treated as part of the makefile, or
4570 ignored, according to the condition.  This is why the larger syntactic
4571 units of the makefile, such as rules, may cross the beginning or the
4572 end of the conditional.
4574 When the variable @code{CC} has the value @samp{gcc}, the above example has
4575 this effect:
4577 @example
4578 foo: $(objects)
4579         $(CC) -o foo $(objects) $(libs_for_gcc)
4580 @end example
4582 @noindent
4583 When the variable @code{CC} has any other value, the effect is this:
4585 @example
4586 foo: $(objects)
4587         $(CC) -o foo $(objects) $(normal_libs)
4588 @end example
4590 Equivalent results can be obtained in another way by conditionalizing a
4591 variable assignment and then using the variable unconditionally:
4593 @example
4594 libs_for_gcc = -lgnu
4595 normal_libs =
4597 ifeq ($(CC),gcc)
4598   libs=$(libs_for_gcc)
4599 else
4600   libs=$(normal_libs)
4601 endif
4603 foo: $(objects)
4604         $(CC) -o foo $(objects) $(libs)
4605 @end example
4607 @node Conditional Syntax, Testing Flags, Conditional Example, Conditionals
4608 @section Syntax of Conditionals
4609 @findex ifdef
4610 @findex ifeq
4611 @findex ifndef
4612 @findex ifneq
4613 @findex else
4614 @findex endif
4616 The syntax of a simple conditional with no @code{else} is as follows:
4618 @example
4619 @var{conditional-directive}
4620 @var{text-if-true}
4621 endif
4622 @end example
4624 @noindent
4625 The @var{text-if-true} may be any lines of text, to be considered as part
4626 of the makefile if the condition is true.  If the condition is false, no
4627 text is used instead.
4629 The syntax of a complex conditional is as follows:
4631 @example
4632 @var{conditional-directive}
4633 @var{text-if-true}
4634 else
4635 @var{text-if-false}
4636 endif
4637 @end example
4639 @noindent
4640 If the condition is true, @var{text-if-true} is used; otherwise,
4641 @var{text-if-false} is used instead.  The @var{text-if-false} can be any
4642 number of lines of text.
4644 The syntax of the @var{conditional-directive} is the same whether the
4645 conditional is simple or complex.  There are four different directives that
4646 test different conditions.  Here is a table of them:
4648 @table @code
4649 @item ifeq (@var{arg1}, @var{arg2})
4650 @itemx ifeq '@var{arg1}' '@var{arg2}'
4651 @itemx ifeq "@var{arg1}" "@var{arg2}"
4652 @itemx ifeq "@var{arg1}" '@var{arg2}'
4653 @itemx ifeq '@var{arg1}' "@var{arg2}"
4654 Expand all variable references in @var{arg1} and @var{arg2} and
4655 compare them.  If they are identical, the @var{text-if-true} is
4656 effective; otherwise, the @var{text-if-false}, if any, is effective.
4658 Often you want to test if a variable has a non-empty value.  When the
4659 value results from complex expansions of variables and functions,
4660 expansions you would consider empty may actually contain whitespace
4661 characters and thus are not seen as empty.  However, you can use the
4662 @code{strip} function (@pxref{Text Functions}) to avoid interpreting
4663 whitespace as a non-empty value.  For example:
4665 @example
4666 @group
4667 ifeq ($(strip $(foo)),)
4668 @var{text-if-empty}
4669 endif
4670 @end group
4671 @end example
4673 @noindent
4674 will evaluate @var{text-if-empty} even if the expansion of
4675 @code{$(foo)} contains whitespace characters.
4677 @item ifneq (@var{arg1}, @var{arg2})
4678 @itemx ifneq '@var{arg1}' '@var{arg2}'
4679 @itemx ifneq "@var{arg1}" "@var{arg2}"
4680 @itemx ifneq "@var{arg1}" '@var{arg2}'
4681 @itemx ifneq '@var{arg1}' "@var{arg2}"
4682 Expand all variable references in @var{arg1} and @var{arg2} and
4683 compare them.  If they are different, the @var{text-if-true} is
4684 effective; otherwise, the @var{text-if-false}, if any, is effective.
4686 @item ifdef @var{variable-name}
4687 If the variable @var{variable-name} has a non-empty value, the
4688 @var{text-if-true} is effective; otherwise, the @var{text-if-false},
4689 if any, is effective.  Variables that have never been defined have an
4690 empty value.
4692 Note that @code{ifdef} only tests whether a variable has a value.  It
4693 does not expand the variable to see if that value is nonempty.
4694 Consequently, tests using @code{ifdef} return true for all definitions
4695 except those like @code{foo =}.  To test for an empty value, use
4696 @w{@code{ifeq ($(foo),)}}.  For example,
4698 @example
4699 bar =
4700 foo = $(bar)
4701 ifdef foo
4702 frobozz = yes
4703 else
4704 frobozz = no
4705 endif
4706 @end example
4708 @noindent
4709 sets @samp{frobozz} to @samp{yes}, while:
4711 @example
4712 foo =
4713 ifdef foo
4714 frobozz = yes
4715 else
4716 frobozz = no
4717 endif
4718 @end example
4720 @noindent
4721 sets @samp{frobozz} to @samp{no}.
4723 @item ifndef @var{variable-name}
4724 If the variable @var{variable-name} has an empty value, the
4725 @var{text-if-true} is effective; otherwise, the @var{text-if-false},
4726 if any, is effective.
4727 @end table
4729 Extra spaces are allowed and ignored at the beginning of the conditional
4730 directive line, but a tab is not allowed.  (If the line begins with a tab,
4731 it will be considered a command for a rule.)  Aside from this, extra spaces
4732 or tabs may be inserted with no effect anywhere except within the directive
4733 name or within an argument.  A comment starting with @samp{#} may appear at
4734 the end of the line.
4736 The other two directives that play a part in a conditional are @code{else}
4737 and @code{endif}.  Each of these directives is written as one word, with no
4738 arguments.  Extra spaces are allowed and ignored at the beginning of the
4739 line, and spaces or tabs at the end.  A comment starting with @samp{#} may
4740 appear at the end of the line.
4742 Conditionals affect which lines of the makefile @code{make} uses.  If
4743 the condition is true, @code{make} reads the lines of the
4744 @var{text-if-true} as part of the makefile; if the condition is false,
4745 @code{make} ignores those lines completely.  It follows that syntactic
4746 units of the makefile, such as rules, may safely be split across the
4747 beginning or the end of the conditional.@refill
4749 @code{make} evaluates conditionals when it reads a makefile.
4750 Consequently, you cannot use automatic variables in the tests of
4751 conditionals because they are not defined until commands are run
4752 (@pxref{Automatic, , Automatic Variables}).
4754 To prevent intolerable confusion, it is not permitted to start a
4755 conditional in one makefile and end it in another.  However, you may
4756 write an @code{include} directive within a conditional, provided you do
4757 not attempt to terminate the conditional inside the included file.
4759 @node Testing Flags,  , Conditional Syntax, Conditionals
4760 @section Conditionals that Test Flags
4762 You can write a conditional that tests @code{make} command flags such as
4763 @samp{-t} by using the variable @code{MAKEFLAGS} together with the
4764 @code{findstring} function
4765 (@pxref{Text Functions, , Functions for String Substitution and Analysis}).
4766 This is useful when @code{touch} is not enough to make a file appear up
4767 to date.
4769 The @code{findstring} function determines whether one string appears as a
4770 substring of another.  If you want to test for the @samp{-t} flag,
4771 use @samp{t} as the first string and the value of @code{MAKEFLAGS} as
4772 the other.
4774 For example, here is how to arrange to use @samp{ranlib -t} to finish
4775 marking an archive file up to date:
4777 @example
4778 archive.a: @dots{}
4779 ifneq (,$(findstring t,$(MAKEFLAGS)))
4780         +touch archive.a
4781         +ranlib -t archive.a
4782 else
4783         ranlib archive.a
4784 endif
4785 @end example
4787 @noindent
4788 The @samp{+} prefix marks those command lines as ``recursive'' so
4789 that they will be executed despite use of the @samp{-t} flag.
4790 @xref{Recursion, ,Recursive Use of @code{make}}.
4792 @node Functions, Running, Conditionals, Top
4793 @chapter Functions for Transforming Text
4794 @cindex functions
4796 @dfn{Functions} allow you to do text processing in the makefile to compute
4797 the files to operate on or the commands to use.  You use a function in a
4798 @dfn{function call}, where you give the name of the function and some text
4799 (the @dfn{arguments}) for the function to operate on.  The result of the
4800 function's processing is substituted into the makefile at the point of the
4801 call, just as a variable might be substituted.
4803 @menu
4804 * Syntax of Functions::         How to write a function call.
4805 * Text Functions::              General-purpose text manipulation functions.
4806 * Filename Functions::          Functions for manipulating file names.
4807 * Foreach Function::            Repeat some text with controlled variation.
4808 * Origin Function::             Find where a variable got its value.
4809 * Shell Function::              Substitute the output of a shell command.
4810 @end menu
4812 @node Syntax of Functions, Text Functions,  , Functions
4813 @section Function Call Syntax
4814 @cindex @code{$}, in function call
4815 @cindex dollar sign (@code{$}), in function call
4816 @cindex arguments of functions
4817 @cindex functions, syntax of
4819 A function call resembles a variable reference.  It looks like this:
4821 @example
4822 $(@var{function} @var{arguments})
4823 @end example
4825 @noindent
4826 or like this:
4828 @example
4829 $@{@var{function} @var{arguments}@}
4830 @end example
4832 Here @var{function} is a function name; one of a short list of names that
4833 are part of @code{make}.  There is no provision for defining new functions.
4835 The @var{arguments} are the arguments of the function.  They are
4836 separated from the function name by one or more spaces or tabs, and if
4837 there is more than one argument, then they are separated by commas.
4838 Such whitespace and commas are not part of an argument's value.  The
4839 delimiters which you use to surround the function call, whether
4840 parentheses or braces, can appear in an argument only in matching pairs;
4841 the other kind of delimiters may appear singly.  If the arguments
4842 themselves contain other function calls or variable references, it is
4843 wisest to use the same kind of delimiters for all the references; write
4844 @w{@samp{$(subst a,b,$(x))}}, not @w{@samp{$(subst a,b,$@{x@})}}.  This
4845 is because it is clearer, and because only one type of delimiter is
4846 matched to find the end of the reference.
4848 The text written for each argument is processed by substitution of
4849 variables and function calls to produce the argument value, which
4850 is the text on which the function acts.  The substitution is done in the
4851 order in which the arguments appear.
4853 Commas and unmatched parentheses or braces cannot appear in the text of an
4854 argument as written; leading spaces cannot appear in the text of the first
4855 argument as written.  These characters can be put into the argument value
4856 by variable substitution.  First define variables @code{comma} and
4857 @code{space} whose values are isolated comma and space characters, then
4858 substitute these variables where such characters are wanted, like this:
4860 @example
4861 @group
4862 comma:= ,
4863 empty:=
4864 space:= $(empty) $(empty)
4865 foo:= a b c
4866 bar:= $(subst $(space),$(comma),$(foo))
4867 # @r{bar is now `a,b,c'.}
4868 @end group
4869 @end example
4871 @noindent
4872 Here the @code{subst} function replaces each space with a comma, through
4873 the value of @code{foo}, and substitutes the result.
4875 @node Text Functions, Filename Functions, Syntax of Functions, Functions
4876 @section Functions for String Substitution and Analysis
4877 @cindex functions, for text
4879 Here are some functions that operate on strings:
4881 @table @code
4882 @item $(subst @var{from},@var{to},@var{text})
4883 @findex subst
4884 Performs a textual replacement on the text @var{text}: each occurrence
4885 of @var{from} is replaced by @var{to}.  The result is substituted for
4886 the function call.  For example,
4888 @example
4889 $(subst ee,EE,feet on the street)
4890 @end example
4892 substitutes the string @samp{fEEt on the strEEt}.
4894 @item $(patsubst @var{pattern},@var{replacement},@var{text})
4895 @findex patsubst
4896 Finds whitespace-separated words in @var{text} that match
4897 @var{pattern} and replaces them with @var{replacement}.  Here
4898 @var{pattern} may contain a @samp{%} which acts as a wildcard,
4899 matching any number of any characters within a word.  If
4900 @var{replacement} also contains a @samp{%}, the @samp{%} is replaced
4901 by the text that matched the @samp{%} in @var{pattern}.@refill
4903 @cindex @code{%}, quoting in @code{patsubst}
4904 @cindex @code{%}, quoting with @code{\} (backslash)
4905 @cindex @code{\} (backslash), to quote @code{%}
4906 @cindex backslash (@code{\}), to quote @code{%}
4907 @cindex quoting @code{%}, in @code{patsubst}
4908 @samp{%} characters in @code{patsubst} function invocations can be
4909 quoted with preceding backslashes (@samp{\}).  Backslashes that would
4910 otherwise quote @samp{%} characters can be quoted with more backslashes.
4911 Backslashes that quote @samp{%} characters or other backslashes are
4912 removed from the pattern before it is compared file names or has a stem
4913 substituted into it.  Backslashes that are not in danger of quoting
4914 @samp{%} characters go unmolested.  For example, the pattern
4915 @file{the\%weird\\%pattern\\} has @samp{the%weird\} preceding the
4916 operative @samp{%} character, and @samp{pattern\\} following it.  The
4917 final two backslashes are left alone because they cannot affect any
4918 @samp{%} character.@refill
4920 Whitespace between words is folded into single space characters;
4921 leading and trailing whitespace is discarded.
4923 For example,
4925 @example
4926 $(patsubst %.c,%.o,x.c.c bar.c)
4927 @end example
4929 @noindent
4930 produces the value @samp{x.c.o bar.o}.
4932 Substitution references (@pxref{Substitution Refs, ,Substitution
4933 References}) are a simpler way to get the effect of the @code{patsubst}
4934 function:
4936 @example
4937 $(@var{var}:@var{pattern}=@var{replacement})
4938 @end example
4940 @noindent
4941 is equivalent to
4943 @example
4944 $(patsubst @var{pattern},@var{replacement},$(@var{var}))
4945 @end example
4947 The second shorthand simplifies one of the most common uses of
4948 @code{patsubst}: replacing the suffix at the end of file names.
4950 @example
4951 $(@var{var}:@var{suffix}=@var{replacement})
4952 @end example
4954 @noindent
4955 is equivalent to
4957 @example
4958 $(patsubst %@var{suffix},%@var{replacement},$(@var{var}))
4959 @end example
4961 @noindent
4962 For example, you might have a list of object files:
4964 @example
4965 objects = foo.o bar.o baz.o
4966 @end example
4968 @noindent
4969 To get the list of corresponding source files, you could simply write:
4971 @example
4972 $(objects:.o=.c)
4973 @end example
4975 @noindent
4976 instead of using the general form:
4978 @example
4979 $(patsubst %.o,%.c,$(objects))
4980 @end example
4982 @item $(strip @var{string})
4983 @cindex stripping whitespace
4984 @cindex whitespace, stripping
4985 @cindex spaces, stripping
4986 @findex strip
4987 Removes leading and trailing whitespace from @var{string} and replaces
4988 each internal sequence of one or more whitespace characters with a
4989 single space.  Thus, @samp{$(strip a b  c )} results in @w{@samp{a b c}}.
4991 The function @code{strip} can be very useful when used in conjunction
4992 with conditionals.  When comparing something with the empty string
4993 @samp{} using @code{ifeq} or @code{ifneq}, you usually want a string of
4994 just whitespace to match the empty string (@pxref{Conditionals}).
4996 Thus, the following may fail to have the desired results:
4998 @example
4999 .PHONY: all
5000 ifneq   "$(needs_made)" ""
5001 all: $(needs_made)
5002 else
5003 all:;@@echo 'Nothing to make!'
5004 endif
5005 @end example
5007 @noindent
5008 Replacing the variable reference @w{@samp{$(needs_made)}} with the
5009 function call @w{@samp{$(strip $(needs_made))}} in the @code{ifneq}
5010 directive would make it more robust.@refill
5012 @item $(findstring @var{find},@var{in})
5013 @findex findstring
5014 @cindex searching for strings
5015 @cindex finding strings
5016 @cindex strings, searching for
5017 Searches @var{in} for an occurrence of @var{find}.  If it occurs, the
5018 value is @var{find}; otherwise, the value is empty.  You can use this
5019 function in a conditional to test for the presence of a specific
5020 substring in a given string.  Thus, the two examples,
5022 @example
5023 $(findstring a,a b c)
5024 $(findstring a,b c)
5025 @end example
5027 @noindent
5028 produce the values @samp{a} and @samp{} (the empty string),
5029 respectively.  @xref{Testing Flags}, for a practical application of
5030 @code{findstring}.@refill
5032 @need 750
5033 @findex filter
5034 @cindex filtering words
5035 @cindex words, filtering
5036 @item $(filter @var{pattern}@dots{},@var{text})
5037 Removes all whitespace-separated words in @var{text} that do
5038 @emph{not} match any of the @var{pattern} words, returning only
5039 matching words.  The patterns are written using @samp{%}, just like
5040 the patterns used in the @code{patsubst} function above.@refill
5042 The @code{filter} function can be used to separate out different types
5043 of strings (such as file names) in a variable.  For example:
5045 @example
5046 sources := foo.c bar.c baz.s ugh.h
5047 foo: $(sources)
5048         cc $(filter %.c %.s,$(sources)) -o foo
5049 @end example
5051 @noindent
5052 says that @file{foo} depends of @file{foo.c}, @file{bar.c},
5053 @file{baz.s} and @file{ugh.h} but only @file{foo.c}, @file{bar.c} and
5054 @file{baz.s} should be specified in the command to the
5055 compiler.@refill
5057 @item $(filter-out @var{pattern}@dots{},@var{text})
5058 @findex filter-out
5059 @cindex filtering out words
5060 @cindex words, filtering out
5061 Removes all whitespace-separated words in @var{text} that @emph{do}
5062 match the @var{pattern} words, returning only the words that @emph{do
5063 not} match.  This is the exact opposite of the @code{filter}
5064 function.@refill
5066 For example, given:
5068 @example
5069 @group
5070 objects=main1.o foo.o main2.o bar.o
5071 mains=main1.o main2.o
5072 @end group
5073 @end example
5075 @noindent
5076 the following generates a list which contains all the object files not
5077 in @samp{mains}:
5079 @example
5080 $(filter-out $(mains),$(objects))
5081 @end example
5083 @need 1500
5084 @findex sort
5085 @cindex sorting words
5086 @item $(sort @var{list})
5087 Sorts the words of @var{list} in lexical order, removing duplicate
5088 words.  The output is a list of words separated by single spaces.
5089 Thus,
5091 @example
5092 $(sort foo bar lose)
5093 @end example
5095 @noindent
5096 returns the value @samp{bar foo lose}.
5098 @cindex removing duplicate words
5099 @cindex duplicate words, removing
5100 @cindex words, removing duplicates
5101 Incidentally, since @code{sort} removes duplicate words, you can use
5102 it for this purpose even if you don't care about the sort order.
5103 @end table
5105 Here is a realistic example of the use of @code{subst} and
5106 @code{patsubst}.  Suppose that a makefile uses the @code{VPATH} variable
5107 to specify a list of directories that @code{make} should search for
5108 dependency files
5109 (@pxref{General Search, , @code{VPATH} Search Path for All Dependencies}).
5110 This example shows how to
5111 tell the C compiler to search for header files in the same list of
5112 directories.@refill
5114 The value of @code{VPATH} is a list of directories separated by colons,
5115 such as @samp{src:../headers}.  First, the @code{subst} function is used to
5116 change the colons to spaces:
5118 @example
5119 $(subst :, ,$(VPATH))
5120 @end example
5122 @noindent
5123 This produces @samp{src ../headers}.  Then @code{patsubst} is used to turn
5124 each directory name into a @samp{-I} flag.  These can be added to the
5125 value of the variable @code{CFLAGS}, which is passed automatically to the C
5126 compiler, like this:
5128 @example
5129 override CFLAGS += $(patsubst %,-I%,$(subst :, ,$(VPATH)))
5130 @end example
5132 @noindent
5133 The effect is to append the text @samp{-Isrc -I../headers} to the
5134 previously given value of @code{CFLAGS}.  The @code{override} directive is
5135 used so that the new value is assigned even if the previous value of
5136 @code{CFLAGS} was specified with a command argument (@pxref{Override
5137 Directive, , The @code{override} Directive}).
5139 @node Filename Functions, Foreach Function, Text Functions, Functions
5140 @section Functions for File Names
5141 @cindex functions, for file names
5142 @cindex file name functions
5144 Several of the built-in expansion functions relate specifically to
5145 taking apart file names or lists of file names.
5147 Each of the following functions performs a specific transformation on a
5148 file name.  The argument of the function is regarded as a series of file
5149 names, separated by whitespace.  (Leading and trailing whitespace is
5150 ignored.)  Each file name in the series is transformed in the same way and
5151 the results are concatenated with single spaces between them.
5153 @table @code
5154 @item $(dir @var{names}@dots{})
5155 @findex dir
5156 @cindex directory part
5157 @cindex file name, directory part
5158 Extracts the directory-part of each file name in @var{names}.  The
5159 directory-part of the file name is everything up through (and
5160 including) the last slash in it.  If the file name contains no slash,
5161 the directory part is the string @samp{./}.  For example,
5163 @example
5164 $(dir src/foo.c hacks)
5165 @end example
5167 @noindent
5168 produces the result @samp{src/ ./}.
5170 @item $(notdir @var{names}@dots{})
5171 @findex notdir
5172 @cindex file name, nondirectory part
5173 @cindex nondirectory part
5174 Extracts all but the directory-part of each file name in @var{names}.
5175 If the file name contains no slash, it is left unchanged.  Otherwise,
5176 everything through the last slash is removed from it.
5178 A file name that ends with a slash becomes an empty string.  This is
5179 unfortunate, because it means that the result does not always have the
5180 same number of whitespace-separated file names as the argument had;
5181 but we do not see any other valid alternative.
5183 For example,
5185 @example
5186 $(notdir src/foo.c hacks)
5187 @end example
5189 @noindent
5190 produces the result @samp{foo.c hacks}.
5192 @item $(suffix @var{names}@dots{})
5193 @findex suffix
5194 @cindex suffix, function to find
5195 @cindex file name suffix
5196 Extracts the suffix of each file name in @var{names}.  If the file name
5197 contains a period, the suffix is everything starting with the last
5198 period.  Otherwise, the suffix is the empty string.  This frequently
5199 means that the result will be empty when @var{names} is not, and if
5200 @var{names} contains multiple file names, the result may contain fewer
5201 file names.
5203 For example,
5205 @example
5206 $(suffix src/foo.c hacks)
5207 @end example
5209 @noindent
5210 produces the result @samp{.c}.
5212 @item $(basename @var{names}@dots{})
5213 @findex basename
5214 @cindex basename
5215 @cindex file name, basename of
5216 Extracts all but the suffix of each file name in @var{names}.  If the
5217 file name contains a period, the basename is everything starting up to
5218 (and not including) the last period.  Otherwise, the basename is the
5219 entire file name.  For example,
5221 @example
5222 $(basename src/foo.c hacks)
5223 @end example
5225 @noindent
5226 produces the result @samp{src/foo hacks}.
5228 @c plural convention with dots (be consistent)
5229 @item $(addsuffix @var{suffix},@var{names}@dots{})
5230 @findex addsuffix
5231 @cindex suffix, adding
5232 @cindex file name suffix, adding
5233 The argument @var{names} is regarded as a series of names, separated
5234 by whitespace; @var{suffix} is used as a unit.  The value of
5235 @var{suffix} is appended to the end of each individual name and the
5236 resulting larger names are concatenated with single spaces between
5237 them.  For example,
5239 @example
5240 $(addsuffix .c,foo bar)
5241 @end example
5243 @noindent
5244 produces the result @samp{foo.c bar.c}.
5246 @item $(addprefix @var{prefix},@var{names}@dots{})
5247 @findex addprefix
5248 @cindex prefix, adding
5249 @cindex file name prefix, adding
5250 The argument @var{names} is regarded as a series of names, separated
5251 by whitespace; @var{prefix} is used as a unit.  The value of
5252 @var{prefix} is prepended to the front of each individual name and the
5253 resulting larger names are concatenated with single spaces between
5254 them.  For example,
5256 @example
5257 $(addprefix src/,foo bar)
5258 @end example
5260 @noindent
5261 produces the result @samp{src/foo src/bar}.
5263 @item $(join @var{list1},@var{list2})
5264 @findex join
5265 @cindex joining lists of words
5266 @cindex words, joining lists
5267 Concatenates the two arguments word by word: the two first words (one
5268 from each argument) concatenated form the first word of the result, the
5269 two second words form the second word of the result, and so on.  So the
5270 @var{n}th word of the result comes from the @var{n}th word of each
5271 argument.  If one argument has more words that the other, the extra
5272 words are copied unchanged into the result.
5274 For example, @samp{$(join a b,.c .o)} produces @samp{a.c b.o}.
5276 Whitespace between the words in the lists is not preserved; it is
5277 replaced with a single space.
5279 This function can merge the results of the @code{dir} and
5280 @code{notdir} functions, to produce the original list of files which
5281 was given to those two functions.@refill
5283 @item $(word @var{n},@var{text})
5284 @findex word
5285 @cindex words, selecting
5286 @cindex selecting words
5287 Returns the @var{n}th word of @var{text}.  The legitimate values of
5288 @var{n} start from 1.  If @var{n} is bigger than the number of words
5289 in @var{text}, the value is empty.  For example,
5291 @example
5292 $(word 2, foo bar baz)
5293 @end example
5295 @noindent
5296 returns @samp{bar}.
5298 @c Following item phrased to prevent overfull hbox.  --RJC 17 Jul 92
5299 @item $(words @var{text})
5300 @findex words
5301 @cindex words, finding number
5302 Returns the number of words in @var{text}.
5303 Thus, the last word of @var{text} is
5304 @w{@code{$(word $(words @var{text}),@var{text})}}.@refill
5306 @item $(firstword @var{names}@dots{})
5307 @findex firstword
5308 @cindex words, extracting first
5309 The argument @var{names} is regarded as a series of names, separated
5310 by whitespace.  The value is the first name in the series.  The rest
5311 of the names are ignored.
5313 For example,
5315 @example
5316 $(firstword foo bar)
5317 @end example
5319 @noindent
5320 produces the result @samp{foo}.  Although @code{$(firstword
5321 @var{text})} is the same as @code{$(word 1,@var{text})}, the
5322 @code{firstword} function is retained for its simplicity.@refill
5324 @item $(wildcard @var{pattern})
5325 @findex wildcard
5326 @cindex wildcard, function
5327 The argument @var{pattern} is a file name pattern, typically containing
5328 wildcard characters (as in shell file name patterns).  The result of
5329 @code{wildcard} is a space-separated list of the names of existing files
5330 that match the pattern.
5331 @xref{Wildcards, ,Using Wildcard Characters in File Names}.
5332 @end table
5334 @node Foreach Function, Origin Function, Filename Functions, Functions
5335 @section The @code{foreach} Function
5336 @findex foreach
5337 @cindex words, iterating over
5339 The @code{foreach} function is very different from other functions.  It
5340 causes one piece of text to be used repeatedly, each time with a different
5341 substitution performed on it.  It resembles the @code{for} command in the
5342 shell @code{sh} and the @code{foreach} command in the C-shell @code{csh}.
5344 The syntax of the @code{foreach} function is:
5346 @example
5347 $(foreach @var{var},@var{list},@var{text})
5348 @end example
5350 @noindent
5351 The first two arguments, @var{var} and @var{list}, are expanded before
5352 anything else is done; note that the last argument, @var{text}, is
5353 @strong{not} expanded at the same time.  Then for each word of the expanded
5354 value of @var{list}, the variable named by the expanded value of @var{var}
5355 is set to that word, and @var{text} is expanded.  Presumably @var{text}
5356 contains references to that variable, so its expansion will be different
5357 each time.
5359 The result is that @var{text} is expanded as many times as there are
5360 whitespace-separated words in @var{list}.  The multiple expansions of
5361 @var{text} are concatenated, with spaces between them, to make the result
5362 of @code{foreach}.
5364 This simple example sets the variable @samp{files} to the list of all files
5365 in the directories in the list @samp{dirs}:
5367 @example
5368 dirs := a b c d
5369 files := $(foreach dir,$(dirs),$(wildcard $(dir)/*))
5370 @end example
5372 Here @var{text} is @samp{$(wildcard $(dir)/*)}.  The first repetition
5373 finds the value @samp{a} for @code{dir}, so it produces the same result
5374 as @samp{$(wildcard a/*)}; the second repetition produces the result
5375 of @samp{$(wildcard b/*)}; and the third, that of @samp{$(wildcard c/*)}.
5377 This example has the same result (except for setting @samp{dirs}) as
5378 the following example:
5380 @example
5381 files := $(wildcard a/* b/* c/* d/*)
5382 @end example
5384 When @var{text} is complicated, you can improve readability by giving it
5385 a name, with an additional variable:
5387 @example
5388 find_files = $(wildcard $(dir)/*)
5389 dirs := a b c d
5390 files := $(foreach dir,$(dirs),$(find_files))
5391 @end example
5393 @noindent
5394 Here we use the variable @code{find_files} this way.  We use plain @samp{=}
5395 to define a recursively-expanding variable, so that its value contains an
5396 actual function call to be reexpanded under the control of @code{foreach};
5397 a simply-expanded variable would not do, since @code{wildcard} would be
5398 called only once at the time of defining @code{find_files}.
5400 The @code{foreach} function has no permanent effect on the variable
5401 @var{var}; its value and flavor after the @code{foreach} function call are
5402 the same as they were beforehand.  The other values which are taken from
5403 @var{list} are in effect only temporarily, during the execution of
5404 @code{foreach}.  The variable @var{var} is a simply-expanded variable
5405 during the execution of @code{foreach}.  If @var{var} was undefined
5406 before the @code{foreach} function call, it is undefined after the call.
5407 @xref{Flavors, ,The Two Flavors of Variables}.@refill
5409 You must take care when using complex variable expressions that result in
5410 variable names because many strange things are valid variable names, but
5411 are probably not what you intended.  For example,
5413 @smallexample
5414 files := $(foreach Esta escrito en espanol!,b c ch,$(find_files))
5415 @end smallexample
5417 @noindent
5418 might be useful if the value of @code{find_files} references the variable
5419 whose name is @samp{Esta escrito en espanol!} (es un nombre bastante largo,
5420 no?), but it is more likely to be a mistake.
5422 @node Origin Function, Shell Function, Foreach Function, Functions
5423 @section The @code{origin} Function
5424 @findex origin
5425 @cindex variables, origin of
5426 @cindex origin of variable
5428 The @code{origin} function is unlike most other functions in that it does
5429 not operate on the values of variables; it tells you something @emph{about}
5430 a variable.  Specifically, it tells you where it came from.
5432 The syntax of the @code{origin} function is:
5434 @example
5435 $(origin @var{variable})
5436 @end example
5438 Note that @var{variable} is the @emph{name} of a variable to inquire about;
5439 not a @emph{reference} to that variable.  Therefore you would not normally
5440 use a @samp{$} or parentheses when writing it.  (You can, however, use a
5441 variable reference in the name if you want the name not to be a constant.)
5443 The result of this function is a string telling you how the variable
5444 @var{variable} was defined:
5446 @table @samp
5447 @item undefined
5449 if @var{variable} was never defined.
5451 @item default
5453 if @var{variable} has a default definition, as is usual with @code{CC}
5454 and so on.  @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
5455 Note that if you have redefined a default variable, the @code{origin}
5456 function will return the origin of the later definition.
5458 @item environment
5460 if @var{variable} was defined as an environment variable and the
5461 @samp{-e} option is @emph{not} turned on (@pxref{Options Summary, ,Summary of Options}).
5463 @item environment override
5465 if @var{variable} was defined as an environment variable and the
5466 @w{@samp{-e}} option @emph{is} turned on (@pxref{Options Summary,
5467 ,Summary of Options}).@refill
5469 @item file
5471 if @var{variable} was defined in a makefile.
5473 @item command line
5475 if @var{variable} was defined on the command line.
5477 @item override
5479 if @var{variable} was defined with an @code{override} directive in a
5480 makefile (@pxref{Override Directive, ,The @code{override} Directive}).
5482 @item automatic
5484 if @var{variable} is an automatic variable defined for the
5485 execution of the commands for each rule
5486 (@pxref{Automatic, , Automatic Variables}).
5487 @end table
5489 This information is primarily useful (other than for your curiosity) to
5490 determine if you want to believe the value of a variable.  For example,
5491 suppose you have a makefile @file{foo} that includes another makefile
5492 @file{bar}.  You want a variable @code{bletch} to be defined in @file{bar}
5493 if you run the command @w{@samp{make -f bar}}, even if the environment contains
5494 a definition of @code{bletch}.  However, if @file{foo} defined
5495 @code{bletch} before including @file{bar}, you do not want to override that
5496 definition.  This could be done by using an @code{override} directive in
5497 @file{foo}, giving that definition precedence over the later definition in
5498 @file{bar}; unfortunately, the @code{override} directive would also
5499 override any command line definitions.  So, @file{bar} could
5500 include:@refill
5502 @example
5503 @group
5504 ifdef bletch
5505 ifeq "$(origin bletch)" "environment"
5506 bletch = barf, gag, etc.
5507 endif
5508 endif
5509 @end group
5510 @end example
5512 @noindent
5513 If @code{bletch} has been defined from the environment, this will redefine
5516 If you want to override a previous definition of @code{bletch} if it came
5517 from the environment, even under @samp{-e}, you could instead write:
5519 @example
5520 @group
5521 ifneq "$(findstring environment,$(origin bletch))" ""
5522 bletch = barf, gag, etc.
5523 endif
5524 @end group
5525 @end example
5527 Here the redefinition takes place if @samp{$(origin bletch)} returns either
5528 @samp{environment} or @samp{environment override}.
5529 @xref{Text Functions, , Functions for String Substitution and Analysis}.
5531 @node Shell Function,  , Origin Function, Functions
5532 @section The @code{shell} Function
5533 @findex shell
5534 @cindex commands, expansion
5535 @cindex backquotes
5536 @cindex shell command, function for
5538 The @code{shell} function is unlike any other function except the
5539 @code{wildcard} function
5540 (@pxref{Wildcard Function, ,The Function @code{wildcard}}) in that it
5541 communicates with the world outside of @code{make}.
5543 The @code{shell} function performs the same function that backquotes
5544 (@samp{`}) perform in most shells: it does @dfn{command expansion}.  This
5545 means that it takes an argument that is a shell command and returns the
5546 output of the command.  The only processing @code{make} does on the result,
5547 before substituting it into the surrounding text, is to convert newlines to
5548 spaces.@refill
5550 The commands run by calls to the @code{shell} function are run when the
5551 function calls are expanded.  In most cases, this is when the makefile is
5552 read in.  The exception is that function calls in the commands of the rules
5553 are expanded when the commands are run, and this applies to @code{shell}
5554 function calls like all others.
5556 Here are some examples of the use of the @code{shell} function:
5558 @example
5559 contents := $(shell cat foo)
5560 @end example
5562 @noindent
5563 sets @code{contents} to the contents of the file @file{foo}, with a space
5564 (rather than a newline) separating each line.
5566 @example
5567 files := $(shell echo *.c)
5568 @end example
5570 @noindent
5571 sets @code{files} to the expansion of @samp{*.c}.  Unless @code{make} is
5572 using a very strange shell, this has the same result as
5573 @w{@samp{$(wildcard *.c)}}.@refill
5575 @node Running, Implicit Rules, Functions, Top
5576 @chapter How to Run @code{make}
5578 A makefile that says how to recompile a program can be used in more
5579 than one way.  The simplest use is to recompile every file that is out
5580 of date.  Usually, makefiles are written so that if you run
5581 @code{make} with no arguments, it does just that.
5583 But you might want to update only some of the files; you might want to use
5584 a different compiler or different compiler options; you might want just to
5585 find out which files are out of date without changing them.
5587 By giving arguments when you run @code{make}, you can do any of these
5588 things and many others.
5590 The exit status of @code{make} is always one of three values:
5591 @table @code
5592 @item 0
5593 The exit status is zero if @code{make} is successful.
5594 @item 2
5595 The exit status is two if @code{make} encounters any errors.
5596 It will print messages describing the particular errors.
5597 @item 1
5598 The exit status is one if you use the @samp{-q} flag and @code{make}
5599 determines that some target is not already up to date.
5600 @xref{Instead of Execution, ,Instead of Executing the Commands}.
5601 @end table
5603 @menu
5604 * Makefile Arguments::          How to specify which makefile to use.
5605 * Goals::                       How to use goal arguments to specify which
5606                                   parts of the makefile to use.
5607 * Instead of Execution::        How to use mode flags to specify what
5608                                   kind of thing to do with the commands
5609                                   in the makefile other than simply
5610                                   execute them.
5611 * Avoiding Compilation::        How to avoid recompiling certain files.
5612 * Overriding::                  How to override a variable to specify
5613                                   an alternate compiler and other things.
5614 * Testing::                     How to proceed past some errors, to
5615                                   test compilation.
5616 * Options Summary::             Summary of Options
5617 @end menu
5619 @node Makefile Arguments, Goals,  , Running
5620 @section Arguments to Specify the Makefile
5621 @cindex @code{--file}
5622 @cindex @code{--makefile}
5623 @cindex @code{-f}
5625 The way to specify the name of the makefile is with the @samp{-f} or
5626 @samp{--file} option (@samp{--makefile} also works).  For example,
5627 @samp{-f altmake} says to use the file @file{altmake} as the makefile.
5629 If you use the @samp{-f} flag several times and follow each @samp{-f}
5630 with an argument, all the specified files are used jointly as
5631 makefiles.
5633 If you do not use the @samp{-f} or @samp{--file} flag, the default is
5634 to try @file{GNUmakefile}, @file{makefile}, and @file{Makefile}, in
5635 that order, and use the first of these three which exists or can be made
5636 (@pxref{Makefiles, ,Writing Makefiles}).@refill
5638 @node Goals, Instead of Execution, Makefile Arguments, Running
5639 @section Arguments to Specify the Goals
5640 @cindex goal, how to specify
5642 The @dfn{goals} are the targets that @code{make} should strive ultimately
5643 to update.  Other targets are updated as well if they appear as
5644 dependencies of goals, or dependencies of dependencies of goals, etc.
5646 By default, the goal is the first target in the makefile (not counting
5647 targets that start with a period).  Therefore, makefiles are usually
5648 written so that the first target is for compiling the entire program or
5649 programs they describe.  If the first rule in the makefile has several
5650 targets, only the first target in the rule becomes the default goal, not
5651 the whole list.
5653 You can specify a different goal or goals with arguments to @code{make}.
5654 Use the name of the goal as an argument.  If you specify several goals,
5655 @code{make} processes each of them in turn, in the order you name them.
5657 Any target in the makefile may be specified as a goal (unless it
5658 starts with @samp{-} or contains an @samp{=}, in which case it will be
5659 parsed as a switch or variable definition, respectively).  Even
5660 targets not in the makefile may be specified, if @code{make} can find
5661 implicit rules that say how to make them.
5663 One use of specifying a goal is if you want to compile only a part of
5664 the program, or only one of several programs.  Specify as a goal each
5665 file that you wish to remake.  For example, consider a directory containing
5666 several programs, with a makefile that starts like this:
5668 @example
5669 .PHONY: all
5670 all: size nm ld ar as
5671 @end example
5673 If you are working on the program @code{size}, you might want to say
5674 @w{@samp{make size}} so that only the files of that program are recompiled.
5676 Another use of specifying a goal is to make files that are not normally
5677 made.  For example, there may be a file of debugging output, or a
5678 version of the program that is compiled specially for testing, which has
5679 a rule in the makefile but is not a dependency of the default goal.
5681 Another use of specifying a goal is to run the commands associated with
5682 a phony target (@pxref{Phony Targets}) or empty target (@pxref{Empty
5683 Targets, ,Empty Target Files to Record Events}).  Many makefiles contain
5684 a phony target named @file{clean} which deletes everything except source
5685 files.  Naturally, this is done only if you request it explicitly with
5686 @w{@samp{make clean}}.  Following is a list of typical phony and empty
5687 target names.  @xref{Standard Targets}, for a detailed list of all the
5688 standard target names which GNU software packages use.
5690 @table @file
5691 @item all
5692 @cindex @code{all} @r{(standard target)}
5693 Make all the top-level targets the makefile knows about.
5695 @item clean
5696 @cindex @code{clean} @r{(standard target)}
5697 Delete all files that are normally created by running @code{make}.
5699 @item mostlyclean
5700 @cindex @code{mostlyclean} @r{(standard target)}
5701 Like @samp{clean}, but may refrain from deleting a few files that people
5702 normally don't want to recompile.  For example, the @samp{mostlyclean}
5703 target for GCC does not delete @file{libgcc.a}, because recompiling it
5704 is rarely necessary and takes a lot of time.
5706 @item distclean
5707 @cindex @code{distclean} @r{(standard target)}
5708 @itemx realclean
5709 @cindex @code{realclean} @r{(standard target)}
5710 @itemx clobber
5711 @cindex @code{clobber} @r{(standard target)}
5712 Any of these targets might be defined to delete @emph{more} files than
5713 @samp{clean} does.  For example, this would delete configuration files
5714 or links that you would normally create as preparation for compilation,
5715 even if the makefile itself cannot create these files.
5717 @item install
5718 @cindex @code{install} @r{(standard target)}
5719 Copy the executable file into a directory that users typically search
5720 for commands; copy any auxiliary files that the executable uses into
5721 the directories where it will look for them.
5723 @item print
5724 @cindex @code{print} @r{(standard target)}
5725 Print listings of the source files that have changed.
5727 @item tar
5728 @cindex @code{tar} @r{(standard target)}
5729 Create a tar file of the source files.
5731 @item shar
5732 @cindex @code{shar} @r{(standard target)}
5733 Create a shell archive (shar file) of the source files.
5735 @item dist
5736 @cindex @code{dist} @r{(standard target)}
5737 Create a distribution file of the source files.  This might
5738 be a tar file, or a shar file, or a compressed version of one of the
5739 above, or even more than one of the above.
5741 @item TAGS
5742 @cindex @code{TAGS} @r{(standard target)}
5743 Update a tags table for this program.
5745 @item check
5746 @cindex @code{check} @r{(standard target)}
5747 @itemx test
5748 @cindex @code{test} @r{(standard target)}
5749 Perform self tests on the program this makefile builds.
5750 @end table
5752 @node Instead of Execution, Avoiding Compilation, Goals, Running
5753 @section Instead of Executing the Commands
5754 @cindex execution, instead of
5755 @cindex commands, instead of executing
5757 The makefile tells @code{make} how to tell whether a target is up to date,
5758 and how to update each target.  But updating the targets is not always
5759 what you want.  Certain options specify other activities for @code{make}.
5761 @comment Extra blank lines make it print better.
5762 @table @samp
5763 @item -n
5764 @itemx --just-print
5765 @itemx --dry-run
5766 @itemx --recon
5767 @cindex @code{--just-print}
5768 @cindex @code{--dry-run}
5769 @cindex @code{--recon}
5770 @cindex @code{-n}
5772 ``No-op''.  The activity is to print what commands would be used to make
5773 the targets up to date, but not actually execute them.
5775 @item -t
5776 @itemx --touch
5777 @cindex @code{--touch}
5778 @cindex touching files
5779 @cindex target, touching
5780 @cindex @code{-t}
5782 ``Touch''.  The activity is to mark the targets as up to date without
5783 actually changing them.  In other words, @code{make} pretends to compile
5784 the targets but does not really change their contents.
5786 @item -q
5787 @itemx --question
5788 @cindex @code{--question}
5789 @cindex @code{-q}
5790 @cindex question mode
5792 ``Question''.  The activity is to find out silently whether the targets
5793 are up to date already; but execute no commands in either case.  In other
5794 words, neither compilation nor output will occur.
5796 @item -W @var{file}
5797 @itemx --what-if=@var{file}
5798 @itemx --assume-new=@var{file}
5799 @itemx --new-file=@var{file}
5800 @cindex @code{--what-if}
5801 @cindex @code{-W}
5802 @cindex @code{--assume-new}
5803 @cindex @code{--new-file}
5804 @cindex what if
5805 @cindex files, assuming new
5807 ``What if''.  Each @samp{-W} flag is followed by a file name.  The given
5808 files' modification times are recorded by @code{make} as being the present
5809 time, although the actual modification times remain the same.
5810 You can use the @samp{-W} flag in conjunction with the @samp{-n} flag
5811 to see what would happen if you were to modify specific files.@refill
5812 @end table
5814 With the @samp{-n} flag, @code{make} prints the commands that it would
5815 normally execute but does not execute them.
5817 With the @samp{-t} flag, @code{make} ignores the commands in the rules
5818 and uses (in effect) the command @code{touch} for each target that needs to
5819 be remade.  The @code{touch} command is also printed, unless @samp{-s} or
5820 @code{.SILENT} is used.  For speed, @code{make} does not actually invoke
5821 the program @code{touch}.  It does the work directly.
5823 With the @samp{-q} flag, @code{make} prints nothing and executes no
5824 commands, but the exit status code it returns is zero if and only if the
5825 targets to be considered are already up to date.  If the exit status is
5826 one, then some updating needs to be done.  If @code{make} encounters an
5827 error, the exit status is two, so you can distinguish an error from a
5828 target that is not up to date.
5830 It is an error to use more than one of these three flags in the same
5831 invocation of @code{make}.
5833 The @samp{-n}, @samp{-t}, and @samp{-q} options do not affect command
5834 lines that begin with @samp{+} characters or contain the strings
5835 @samp{$(MAKE)} or @samp{$@{MAKE@}}.  Note that only the line containing
5836 the @samp{+} character or the strings @samp{$(MAKE)} or @samp{$@{MAKE@}}
5837 is run regardless of these options.  Other lines in the same rule are
5838 not run unless they too begin with @samp{+} or contain @samp{$(MAKE)} or
5839 @samp{$@{MAKE@}} (@xref{MAKE Variable, ,How the @code{MAKE} Variable Works}.)
5841 The @samp{-W} flag provides two features:
5843 @itemize @bullet
5844 @item
5845 If you also use the @samp{-n} or @samp{-q} flag, you can see what
5846 @code{make} would do if you were to modify some files.
5848 @item
5849 Without the @samp{-n} or @samp{-q} flag, when @code{make} is actually
5850 executing commands, the @samp{-W} flag can direct @code{make} to act
5851 as if some files had been modified, without actually modifying the
5852 files.@refill
5853 @end itemize
5855 Note that the options @samp{-p} and @samp{-v} allow you to obtain other
5856 information about @code{make} or about the makefiles in use
5857 (@pxref{Options Summary, ,Summary of Options}).@refill
5859 @node Avoiding Compilation, Overriding, Instead of Execution, Running
5860 @section Avoiding Recompilation of Some Files
5861 @cindex @code{-o}
5862 @cindex @code{--old-file}
5863 @cindex @code{--assume-old}
5864 @cindex files, assuming old
5865 @cindex files, avoiding recompilation of
5866 @cindex recompilation, avoiding
5868 Sometimes you may have changed a source file but you do not want to
5869 recompile all the files that depend on it.  For example, suppose you add a
5870 macro or a declaration to a header file that many other files depend on.
5871 Being conservative, @code{make} assumes that any change in the header file
5872 requires recompilation of all dependent files, but you know that they do not
5873 need to be recompiled and you would rather not waste the time waiting for
5874 them to compile.
5876 If you anticipate the problem before changing the header file, you can
5877 use the @samp{-t} flag.  This flag tells @code{make} not to run the
5878 commands in the rules, but rather to mark the target up to date by
5879 changing its last-modification date.  You would follow this procedure:
5881 @enumerate
5882 @item
5883 Use the command @samp{make} to recompile the source files that really
5884 need recompilation.
5886 @item
5887 Make the changes in the header files.
5889 @item
5890 Use the command @samp{make -t} to mark all the object files as
5891 up to date.  The next time you run @code{make}, the changes in the
5892 header files will not cause any recompilation.
5893 @end enumerate
5895 If you have already changed the header file at a time when some files
5896 do need recompilation, it is too late to do this.  Instead, you can
5897 use the @w{@samp{-o @var{file}}} flag, which marks a specified file as
5898 ``old'' (@pxref{Options Summary, ,Summary of Options}).  This means
5899 that the file itself will not be remade, and nothing else will be
5900 remade on its account.  Follow this procedure:
5902 @enumerate
5903 @item
5904 Recompile the source files that need compilation for reasons independent
5905 of the particular header file, with @samp{make -o @var{headerfile}}.
5906 If several header files are involved, use a separate @samp{-o} option
5907 for each header file.
5909 @item
5910 Touch all the object files with @samp{make -t}.
5911 @end enumerate
5913 @node Overriding, Testing, Avoiding Compilation, Running
5914 @section Overriding Variables
5915 @cindex overriding variables with arguments
5916 @cindex variables, overriding with arguments
5917 @cindex command line variables
5918 @cindex variables, command line
5920 An argument that contains @samp{=} specifies the value of a variable:
5921 @samp{@var{v}=@var{x}} sets the value of the variable @var{v} to @var{x}.
5922 If you specify a value in this way, all ordinary assignments of the same
5923 variable in the makefile are ignored; we say they have been
5924 @dfn{overridden} by the command line argument.
5926 The most common way to use this facility is to pass extra flags to
5927 compilers.  For example, in a properly written makefile, the variable
5928 @code{CFLAGS} is included in each command that runs the C compiler, so a
5929 file @file{foo.c} would be compiled something like this:
5931 @example
5932 cc -c $(CFLAGS) foo.c
5933 @end example
5935 Thus, whatever value you set for @code{CFLAGS} affects each compilation
5936 that occurs.  The makefile probably specifies the usual value for
5937 @code{CFLAGS}, like this:
5939 @example
5940 CFLAGS=-g
5941 @end example
5943 Each time you run @code{make}, you can override this value if you
5944 wish.  For example, if you say @samp{make CFLAGS='-g -O'}, each C
5945 compilation will be done with @samp{cc -c -g -O}.  (This illustrates
5946 how you can use quoting in the shell to enclose spaces and other
5947 special characters in the value of a variable when you override it.)
5949 The variable @code{CFLAGS} is only one of many standard variables that
5950 exist just so that you can change them this way.  @xref{Implicit
5951 Variables, , Variables Used by Implicit Rules}, for a complete list.
5953 You can also program the makefile to look at additional variables of your
5954 own, giving the user the ability to control other aspects of how the
5955 makefile works by changing the variables.
5957 When you override a variable with a command argument, you can define either
5958 a recursively-expanded variable or a simply-expanded variable.  The
5959 examples shown above make a recursively-expanded variable; to make a
5960 simply-expanded variable, write @samp{:=} instead of @samp{=}.  But, unless
5961 you want to include a variable reference or function call in the
5962 @emph{value} that you specify, it makes no difference which kind of
5963 variable you create.
5965 There is one way that the makefile can change a variable that you have
5966 overridden.  This is to use the @code{override} directive, which is a line
5967 that looks like this: @samp{override @var{variable} = @var{value}}
5968 (@pxref{Override Directive, ,The @code{override} Directive}).
5970 @node Testing, Options Summary, Overriding, Running
5971 @section Testing the Compilation of a Program
5972 @cindex testing compilation
5973 @cindex compilation, testing
5975 Normally, when an error happens in executing a shell command, @code{make}
5976 gives up immediately, returning a nonzero status.  No further commands are
5977 executed for any target.  The error implies that the goal cannot be
5978 correctly remade, and @code{make} reports this as soon as it knows.
5980 When you are compiling a program that you have just changed, this is not
5981 what you want.  Instead, you would rather that @code{make} try compiling
5982 every file that can be tried, to show you as many compilation errors
5983 as possible.
5985 @cindex @code{-k}
5986 @cindex @code{--keep-going}
5987 On these occasions, you should use the @samp{-k} or
5988 @samp{--keep-going} flag.  This tells @code{make} to continue to
5989 consider the other dependencies of the pending targets, remaking them
5990 if necessary, before it gives up and returns nonzero status.  For
5991 example, after an error in compiling one object file, @samp{make -k}
5992 will continue compiling other object files even though it already
5993 knows that linking them will be impossible.  In addition to continuing
5994 after failed shell commands, @samp{make -k} will continue as much as
5995 possible after discovering that it does not know how to make a target
5996 or dependency file.  This will always cause an error message, but
5997 without @samp{-k}, it is a fatal error (@pxref{Options Summary,
5998 ,Summary of Options}).@refill
6000 The usual behavior of @code{make} assumes that your purpose is to get the
6001 goals up to date; once @code{make} learns that this is impossible, it might
6002 as well report the failure immediately.  The @samp{-k} flag says that the
6003 real purpose is to test as much as possible of the changes made in the
6004 program, perhaps to find several independent problems so that you can
6005 correct them all before the next attempt to compile.  This is why Emacs'
6006 @kbd{M-x compile} command passes the @samp{-k} flag by default.
6008 @node Options Summary,  , Testing, Running
6009 @section Summary of Options
6010 @cindex options
6011 @cindex flags
6012 @cindex switches
6014 Here is a table of all the options @code{make} understands:
6016 @table @samp
6017 @item -b
6018 @cindex @code{-b}
6019 @itemx -m
6020 @cindex @code{-m}
6021 These options are ignored for compatibility with other versions of @code{make}.
6023 @item -C @var{dir}
6024 @cindex @code{-C}
6025 @itemx --directory=@var{dir}
6026 @cindex @code{--directory}
6027 Change to directory @var{dir} before reading the makefiles.  If multiple
6028 @samp{-C} options are specified, each is interpreted relative to the
6029 previous one: @samp{-C / -C etc} is equivalent to @samp{-C /etc}.
6030 This is typically used with recursive invocations of @code{make}
6031 (@pxref{Recursion, ,Recursive Use of @code{make}}).
6033 @item -d
6034 @cindex @code{-d}
6035 @itemx --debug
6036 @cindex @code{--debug}
6037 @c Extra blank line here makes the table look better.
6039 Print debugging information in addition to normal processing.  The
6040 debugging information says which files are being considered for
6041 remaking, which file-times are being compared and with what results,
6042 which files actually need to be remade, which implicit rules are
6043 considered and which are applied---everything interesting about how
6044 @code{make} decides what to do.
6046 @item -e
6047 @cindex @code{-e}
6048 @itemx --environment-overrides
6049 @cindex @code{--environment-overrides}
6050 Give variables taken from the environment precedence
6051 over variables from makefiles.
6052 @xref{Environment, ,Variables from the Environment}.
6054 @item -f @var{file}
6055 @cindex @code{-f}
6056 @itemx --file=@var{file}
6057 @cindex @code{--file}
6058 @itemx --makefile=@var{file}
6059 @cindex @code{--makefile}
6060 Read the file named @var{file} as a makefile.
6061 @xref{Makefiles, ,Writing Makefiles}.
6063 @item -h
6064 @cindex @code{-h}
6065 @itemx --help
6066 @cindex @code{--help}
6067 @c Extra blank line here makes the table look better.
6069 Remind you of the options that @code{make} understands and then exit.
6071 @item -i
6072 @cindex @code{-i}
6073 @itemx --ignore-errors
6074 @cindex @code{--ignore-errors}
6075 Ignore all errors in commands executed to remake files.
6076 @xref{Errors, ,Errors in Commands}.
6078 @item -I @var{dir}
6079 @cindex @code{-I}
6080 @itemx --include-dir=@var{dir}
6081 @cindex @code{--include-dir}
6082 Specifies a directory @var{dir} to search for included makefiles.
6083 @xref{Include, ,Including Other Makefiles}.  If several @samp{-I}
6084 options are used to specify several directories, the directories are
6085 searched in the order specified.
6087 @item -j [@var{jobs}]
6088 @cindex @code{-j}
6089 @itemx --jobs=[@var{jobs}]
6090 @cindex @code{--jobs}
6091 Specifies the number of jobs (commands) to run simultaneously.  With no
6092 argument, @code{make} runs as many jobs simultaneously as possible.  If
6093 there is more than one @samp{-j} option, the last one is effective.
6094 @xref{Parallel, ,Parallel Execution},
6095 for more information on how commands are run.
6097 @item -k
6098 @cindex @code{-k}
6099 @itemx --keep-going
6100 @cindex @code{--keep-going}
6101 Continue as much as possible after an error.  While the target that
6102 failed, and those that depend on it, cannot be remade, the other
6103 dependencies of these targets can be processed all the same.
6104 @xref{Testing, ,Testing the Compilation of a Program}.
6106 @item -l [@var{load}]
6107 @cindex @code{-l}
6108 @itemx --load-average[=@var{load}]
6109 @cindex @code{--load-average}
6110 @itemx --max-load[=@var{load}]
6111 @cindex @code{--max-load}
6112 Specifies that no new jobs (commands) should be started if there are
6113 other jobs running and the load average is at least @var{load} (a
6114 floating-point number).  With no argument, removes a previous load
6115 limit.  @xref{Parallel, ,Parallel Execution}.
6117 @item -n
6118 @cindex @code{-n}
6119 @itemx --just-print
6120 @cindex @code{--just-print}
6121 @itemx --dry-run
6122 @cindex @code{--dry-run}
6123 @itemx --recon
6124 @cindex @code{--recon}
6125 @c Extra blank line here makes the table look better.
6127 Print the commands that would be executed, but do not execute them.
6128 @xref{Instead of Execution, ,Instead of Executing the Commands}.
6130 @item -o @var{file}
6131 @cindex @code{-o}
6132 @itemx --old-file=@var{file}
6133 @cindex @code{--old-file}
6134 @itemx --assume-old=@var{file}
6135 @cindex @code{--assume-old}
6136 Do not remake the file @var{file} even if it is older than its
6137 dependencies, and do not remake anything on account of changes in
6138 @var{file}.  Essentially the file is treated as very old and its rules
6139 are ignored.  @xref{Avoiding Compilation, ,Avoiding Recompilation of
6140 Some Files}.@refill
6142 @item -p
6143 @cindex @code{-p}
6144 @itemx --print-data-base
6145 @cindex @code{--print-data-base}
6146 Print the data base (rules and variable values) that results from
6147 reading the makefiles; then execute as usual or as otherwise
6148 specified.  This also prints the version information given by
6149 the @samp{-v} switch (see below).  To print the data base without
6150 trying to remake any files, use @w{@samp{make -p -f /dev/null}}.
6152 @item -q
6153 @cindex @code{-q}
6154 @itemx --question
6155 @cindex @code{--question}
6156 ``Question mode''.  Do not run any commands, or print anything; just
6157 return an exit status that is zero if the specified targets are already
6158 up to date, one if any remaking is required, or two if an error is
6159 encountered.  @xref{Instead of Execution, ,Instead of Executing the
6160 Commands}.@refill
6162 @item -r
6163 @cindex @code{-r}
6164 @itemx --no-builtin-rules
6165 @cindex @code{--no-builtin-rules}
6166 Eliminate use of the built-in implicit rules (@pxref{Implicit Rules,
6167 ,Using Implicit Rules}).  You can still define your own by writing
6168 pattern rules (@pxref{Pattern Rules, ,Defining and Redefining Pattern
6169 Rules}).  The @samp{-r} option also clears out the default list of
6170 suffixes for suffix rules (@pxref{Suffix Rules, ,Old-Fashioned Suffix
6171 Rules}).  But you can still define your own suffixes with a rule for
6172 @code{.SUFFIXES}, and then define your own suffix rules.
6174 @item -s
6175 @cindex @code{-s}
6176 @itemx --silent
6177 @cindex @code{--silent}
6178 @itemx --quiet
6179 @cindex @code{--quiet}
6180 @c Extra blank line here makes the table look better.
6182 Silent operation; do not print the commands as they are executed.
6183 @xref{Echoing, ,Command Echoing}.
6185 @item -S
6186 @cindex @code{-S}
6187 @itemx --no-keep-going
6188 @cindex @code{--no-keep-going}
6189 @itemx --stop
6190 @cindex @code{--stop}
6191 @c Extra blank line here makes the table look better.
6193 Cancel the effect of the @samp{-k} option.  This is never necessary
6194 except in a recursive @code{make} where @samp{-k} might be inherited
6195 from the top-level @code{make} via @code{MAKEFLAGS}
6196 (@pxref{Recursion, ,Recursive Use of @code{make}})
6197 or if you set @samp{-k} in @code{MAKEFLAGS} in your environment.@refill
6199 @item -t
6200 @cindex @code{-t}
6201 @itemx --touch
6202 @cindex @code{--touch}
6203 @c Extra blank line here makes the table look better.
6205 Touch files (mark them up to date without really changing them)
6206 instead of running their commands.  This is used to pretend that the
6207 commands were done, in order to fool future invocations of
6208 @code{make}.  @xref{Instead of Execution, ,Instead of Executing the Commands}.
6210 @item -v
6211 @cindex @code{-v}
6212 @itemx --version
6213 @cindex @code{--version}
6214 Print the version of the @code{make} program plus a copyright, a list
6215 of authors, and a notice that there is no warranty; then exit.
6217 @item -w
6218 @cindex @code{-w}
6219 @itemx --print-directory
6220 @cindex @code{--print-directory}
6221 Print a message containing the working directory both before and after
6222 executing the makefile.  This may be useful for tracking down errors
6223 from complicated nests of recursive @code{make} commands.
6224 @xref{Recursion, ,Recursive Use of @code{make}}.  (In practice, you
6225 rarely need to specify this option since @samp{make} does it for you;
6226 see @ref{-w Option, ,The @samp{--print-directory} Option}.)
6228 @itemx --no-print-directory
6229 @cindex @code{--no-print-directory}
6230 Disable printing of the working directory under @code{-w}.
6231 This option is useful when @code{-w} is turned on automatically,
6232 but you do not want to see the extra messages.
6233 @xref{-w Option, ,The @samp{--print-directory} Option}.
6235 @item -W @var{file}
6236 @cindex @code{-W}
6237 @itemx --what-if=@var{file}
6238 @cindex @code{--what-if}
6239 @itemx --new-file=@var{file}
6240 @cindex @code{--new-file}
6241 @itemx --assume-new=@var{file}
6242 @cindex @code{--assume-new}
6243 Pretend that the target @var{file} has just been modified.  When used
6244 with the @samp{-n} flag, this shows you what would happen if you were
6245 to modify that file.  Without @samp{-n}, it is almost the same as
6246 running a @code{touch} command on the given file before running
6247 @code{make}, except that the modification time is changed only in the
6248 imagination of @code{make}.
6249 @xref{Instead of Execution, ,Instead of Executing the Commands}.
6251 @item --warn-undefined-variables
6252 @cindex @code{--warn-undefined-variables}
6253 @cindex variables, warning for undefined
6254 @cindex undefined variables, warning message
6255 Issue a warning message whenever @code{make} sees a reference to an
6256 undefined variable.  This can be helpful when you are trying to debug
6257 makefiles which use variables in complex ways.
6258 @end table
6260 @node Implicit Rules, Archives, Running, Top
6261 @chapter Using Implicit Rules
6262 @cindex implicit rule
6263 @cindex rule, implicit
6265 Certain standard ways of remaking target files are used very often.  For
6266 example, one customary way to make an object file is from a C source file
6267 using the C compiler, @code{cc}.
6269 @dfn{Implicit rules} tell @code{make} how to use customary techniques so
6270 that you do not have to specify them in detail when you want to use
6271 them.  For example, there is an implicit rule for C compilation.  File
6272 names determine which implicit rules are run.  For example, C
6273 compilation typically takes a @file{.c} file and makes a @file{.o} file.
6274 So @code{make} applies the implicit rule for C compilation when it sees
6275 this combination of file name endings.@refill
6277 A chain of implicit rules can apply in sequence; for example, @code{make}
6278 will remake a @file{.o} file from a @file{.y} file by way of a @file{.c} file.
6279 @iftex
6280 @xref{Chained Rules, ,Chains of Implicit Rules}.
6281 @end iftex
6283 The built-in implicit rules use several variables in their commands so
6284 that, by changing the values of the variables, you can change the way the
6285 implicit rule works.  For example, the variable @code{CFLAGS} controls the
6286 flags given to the C compiler by the implicit rule for C compilation.
6287 @iftex
6288 @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
6289 @end iftex
6291 You can define your own implicit rules by writing @dfn{pattern rules}.
6292 @iftex
6293 @xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.
6294 @end iftex
6296 @dfn{Suffix rules} are a more limited way to define implicit rules.
6297 Pattern rules are more general and clearer, but suffix rules are
6298 retained for compatibility.
6299 @iftex
6300 @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
6301 @end iftex
6303 @menu
6304 * Using Implicit::              How to use an existing implicit rule
6305                                   to get the commands for updating a file.
6306 * Catalogue of Rules::          A list of built-in implicit rules.
6307 * Implicit Variables::          How to change what predefined rules do.
6308 * Chained Rules::               How to use a chain of implicit rules.
6309 * Pattern Rules::               How to define new implicit rules.
6310 * Last Resort::                 How to defining commands for rules
6311                                   which cannot find any.
6312 * Suffix Rules::                The old-fashioned style of implicit rule.
6313 * Search Algorithm::            The precise algorithm for applying
6314                                   implicit rules.
6315 @end menu
6317 @node Using Implicit, Catalogue of Rules,  , Implicit Rules
6318 @section Using Implicit Rules
6319 @cindex implicit rule, how to use
6320 @cindex rule, implicit, how to use
6322 To allow @code{make} to find a customary method for updating a target file,
6323 all you have to do is refrain from specifying commands yourself.  Either
6324 write a rule with no command lines, or don't write a rule at all.  Then
6325 @code{make} will figure out which implicit rule to use based on which
6326 kind of source file exists or can be made.
6328 For example, suppose the makefile looks like this:
6330 @example
6331 foo : foo.o bar.o
6332         cc -o foo foo.o bar.o $(CFLAGS) $(LDFLAGS)
6333 @end example
6335 @noindent
6336 Because you mention @file{foo.o} but do not give a rule for it, @code{make}
6337 will automatically look for an implicit rule that tells how to update it.
6338 This happens whether or not the file @file{foo.o} currently exists.
6340 If an implicit rule is found, it can supply both commands and one or
6341 more dependencies (the source files).  You would want to write a rule
6342 for @file{foo.o} with no command lines if you need to specify additional
6343 dependencies, such as header files, that the implicit rule cannot
6344 supply.
6346 Each implicit rule has a target pattern and dependency patterns.  There may
6347 be many implicit rules with the same target pattern.  For example, numerous
6348 rules make @samp{.o} files: one, from a @samp{.c} file with the C compiler;
6349 another, from a @samp{.p} file with the Pascal compiler; and so on.  The rule
6350 that actually applies is the one whose dependencies exist or can be made.
6351 So, if you have a file @file{foo.c}, @code{make} will run the C compiler;
6352 otherwise, if you have a file @file{foo.p}, @code{make} will run the Pascal
6353 compiler; and so on.
6355 Of course, when you write the makefile, you know which implicit rule you
6356 want @code{make} to use, and you know it will choose that one because you
6357 know which possible dependency files are supposed to exist.
6358 @xref{Catalogue of Rules, ,Catalogue of Implicit Rules},
6359 for a catalogue of all the predefined implicit rules.
6361 Above, we said an implicit rule applies if the required dependencies ``exist
6362 or can be made''.  A file ``can be made'' if it is mentioned explicitly in
6363 the makefile as a target or a dependency, or if an implicit rule can be
6364 recursively found for how to make it.  When an implicit dependency is the
6365 result of another implicit rule, we say that @dfn{chaining} is occurring.
6366 @xref{Chained Rules, ,Chains of Implicit Rules}.
6368 In general, @code{make} searches for an implicit rule for each target, and
6369 for each double-colon rule, that has no commands.  A file that is mentioned
6370 only as a dependency is considered a target whose rule specifies nothing,
6371 so implicit rule search happens for it.  @xref{Search Algorithm, ,Implicit Rule Search Algorithm}, for the
6372 details of how the search is done.
6374 Note that explicit dependencies do not influence implicit rule search.
6375 For example, consider this explicit rule:
6377 @example
6378 foo.o: foo.p
6379 @end example
6381 @noindent
6382 The dependency on @file{foo.p} does not necessarily mean that
6383 @code{make} will remake @file{foo.o} according to the implicit rule to
6384 make an object file, a @file{.o} file, from a Pascal source file, a
6385 @file{.p} file.  For example, if @file{foo.c} also exists, the implicit
6386 rule to make an object file from a C source file is used instead,
6387 because it appears before the Pascal rule in the list of predefined
6388 implicit rules (@pxref{Catalogue of Rules, , Catalogue of Implicit
6389 Rules}).
6391 If you do not want an implicit rule to be used for a target that has no
6392 commands, you can give that target empty commands by writing a semicolon
6393 (@pxref{Empty Commands, ,Defining Empty Commands}).
6395 @node Catalogue of Rules, Implicit Variables, Using Implicit, Implicit Rules
6396 @section Catalogue of Implicit Rules
6397 @cindex implicit rule, predefined
6398 @cindex rule, implicit, predefined
6400 Here is a catalogue of predefined implicit rules which are always
6401 available unless the makefile explicitly overrides or cancels them.
6402 @xref{Canceling Rules, ,Canceling Implicit Rules}, for information on
6403 canceling or overriding an implicit rule.  The @samp{-r} or
6404 @samp{--no-builtin-rules} option cancels all predefined rules.
6406 Not all of these rules will always be defined, even when the @samp{-r}
6407 option is not given.  Many of the predefined implicit rules are
6408 implemented in @code{make} as suffix rules, so which ones will be
6409 defined depends on the @dfn{suffix list} (the list of dependencies of
6410 the special target @code{.SUFFIXES}).  The default suffix list is:
6411 @code{.out}, @code{.a}, @code{.ln}, @code{.o}, @code{.c}, @code{.cc},
6412 @code{.C}, @code{.p}, @code{.f}, @code{.F}, @code{.r}, @code{.y},
6413 @code{.l}, @code{.s}, @code{.S}, @code{.mod}, @code{.sym}, @code{.def},
6414 @code{.h}, @code{.info}, @code{.dvi}, @code{.tex}, @code{.texinfo},
6415 @code{.texi}, @code{.txinfo}, @code{.w}, @code{.ch} @code{.web},
6416 @code{.sh}, @code{.elc}, @code{.el}.  All of the implicit rules
6417 described below whose dependencies have one of these suffixes are
6418 actually suffix rules.  If you modify the suffix list, the only
6419 predefined suffix rules in effect will be those named by one or two of
6420 the suffixes that are on the list you specify; rules whose suffixes fail
6421 to be on the list are disabled.  @xref{Suffix Rules, ,Old-Fashioned
6422 Suffix Rules}, for full details on suffix rules.
6424 @table @asis
6425 @item Compiling C programs
6426 @cindex C, rule to compile
6427 @pindex cc
6428 @pindex gcc
6429 @pindex .o
6430 @pindex .c
6431 @file{@var{n}.o} is made automatically from @file{@var{n}.c} with
6432 a command of the form @samp{$(CC) -c $(CPPFLAGS) $(CFLAGS)}.@refill
6434 @item Compiling C++ programs
6435 @cindex C++, rule to compile
6436 @pindex g++
6437 @pindex .C
6438 @pindex .cc
6439 @file{@var{n}.o} is made automatically from @file{@var{n}.cc} or
6440 @file{@var{n}.C} with a command of the form @samp{$(CXX) -c $(CPPFLAGS)
6441 $(CXXFLAGS)}.  We encourage you to use the suffix @samp{.cc} for C++
6442 source files instead of @samp{.C}.@refill
6444 @item Compiling Pascal programs
6445 @cindex Pascal, rule to compile
6446 @pindex pc
6447 @pindex .p
6448 @file{@var{n}.o} is made automatically from @file{@var{n}.p}
6449 with the command @samp{$(PC) -c $(PFLAGS)}.@refill
6451 @item Compiling Fortran and Ratfor programs
6452 @cindex Fortran, rule to compile
6453 @cindex Ratfor, rule to compile
6454 @pindex f77
6455 @pindex .f
6456 @pindex .r
6457 @pindex .F
6458 @file{@var{n}.o} is made automatically from @file{@var{n}.r},
6459 @file{@var{n}.F} or @file{@var{n}.f} by running the
6460 Fortran compiler.  The precise command used is as follows:@refill
6462 @table @samp
6463 @item .f
6464 @samp{$(FC) -c $(FFLAGS)}.
6465 @item .F
6466 @samp{$(FC) -c $(FFLAGS) $(CPPFLAGS)}.
6467 @item .r
6468 @samp{$(FC) -c $(FFLAGS) $(RFLAGS)}.
6469 @end table
6471 @item Preprocessing Fortran and Ratfor programs
6472 @file{@var{n}.f} is made automatically from @file{@var{n}.r} or
6473 @file{@var{n}.F}.  This rule runs just the preprocessor to convert a
6474 Ratfor or preprocessable Fortran program into a strict Fortran
6475 program.  The precise command used is as follows:@refill
6477 @table @samp
6478 @item .F
6479 @samp{$(FC) -F $(CPPFLAGS) $(FFLAGS)}.
6480 @item .r
6481 @samp{$(FC) -F $(FFLAGS) $(RFLAGS)}.
6482 @end table
6484 @item Compiling Modula-2 programs
6485 @cindex Modula-2, rule to compile
6486 @pindex m2c
6487 @pindex .sym
6488 @pindex .def
6489 @pindex .mod
6490 @file{@var{n}.sym} is made from @file{@var{n}.def} with a command
6491 of the form @samp{$(M2C) $(M2FLAGS) $(DEFFLAGS)}.  @file{@var{n}.o}
6492 is made from @file{@var{n}.mod}; the form is:
6493 @w{@samp{$(M2C) $(M2FLAGS) $(MODFLAGS)}}.@refill
6495 @need 1200
6496 @item Assembling and preprocessing assembler programs
6497 @cindex assembly, rule to compile
6498 @pindex as
6499 @pindex .s
6500 @file{@var{n}.o} is made automatically from @file{@var{n}.s} by
6501 running the assembler, @code{as}.  The precise command is
6502 @samp{$(AS) $(ASFLAGS)}.@refill
6504 @pindex .S
6505 @file{@var{n}.s} is made automatically from @file{@var{n}.S} by
6506 running the C preprocessor, @code{cpp}.  The precise command is
6507 @w{@samp{$(CPP) $(CPPFLAGS)}}.
6509 @item Linking a single object file
6510 @cindex linking, predefined rule for
6511 @pindex ld
6512 @pindex .o
6513 @file{@var{n}} is made automatically from @file{@var{n}.o} by running
6514 the linker (usually called @code{ld}) via the C compiler.  The precise
6515 command used is @w{@samp{$(CC) $(LDFLAGS) @var{n}.o $(LOADLIBES)}}.
6517 This rule does the right thing for a simple program with only one
6518 source file.  It will also do the right thing if there are multiple
6519 object files (presumably coming from various other source files), one
6520 of which has a name matching that of the executable file.  Thus,
6522 @example
6523 x: y.o z.o
6524 @end example
6526 @noindent
6527 when @file{x.c}, @file{y.c} and @file{z.c} all exist will execute:
6529 @example
6530 @group
6531 cc -c x.c -o x.o
6532 cc -c y.c -o y.o
6533 cc -c z.c -o z.o
6534 cc x.o y.o z.o -o x
6535 rm -f x.o
6536 rm -f y.o
6537 rm -f z.o
6538 @end group
6539 @end example
6541 @noindent
6542 In more complicated cases, such as when there is no object file whose
6543 name derives from the executable file name, you must write an explicit
6544 command for linking.
6546 Each kind of file automatically made into @samp{.o} object files will
6547 be automatically linked by using the compiler (@samp{$(CC)},
6548 @samp{$(FC)} or @samp{$(PC)}; the C compiler @samp{$(CC)} is used to
6549 assemble @samp{.s} files) without the @samp{-c} option.  This could be
6550 done by using the @samp{.o} object files as intermediates, but it is
6551 faster to do the compiling and linking in one step, so that's how it's
6552 done.@refill
6554 @item Yacc for C programs
6555 @pindex yacc
6556 @cindex Yacc, rule to run
6557 @pindex .y
6558 @file{@var{n}.c} is made automatically from @file{@var{n}.y} by
6559 running Yacc with the command @samp{$(YACC) $(YFLAGS)}.
6561 @item Lex for C programs
6562 @pindex lex
6563 @cindex Lex, rule to run
6564 @pindex .l
6565 @file{@var{n}.c} is made automatically from @file{@var{n}.l} by
6566 by running Lex.  The actual command is @samp{$(LEX) $(LFLAGS)}.
6568 @item Lex for Ratfor programs
6569 @file{@var{n}.r} is made automatically from @file{@var{n}.l} by
6570 by running Lex.  The actual command is @samp{$(LEX) $(LFLAGS)}.
6572 The convention of using the same suffix @samp{.l} for all Lex files
6573 regardless of whether they produce C code or Ratfor code makes it
6574 impossible for @code{make} to determine automatically which of the two
6575 languages you are using in any particular case.  If @code{make} is
6576 called upon to remake an object file from a @samp{.l} file, it must
6577 guess which compiler to use.  It will guess the C compiler, because
6578 that is more common.  If you are using Ratfor, make sure @code{make}
6579 knows this by mentioning @file{@var{n}.r} in the makefile.  Or, if you
6580 are using Ratfor exclusively, with no C files, remove @samp{.c} from
6581 the list of implicit rule suffixes with:@refill
6583 @example
6584 @group
6585 .SUFFIXES:
6586 .SUFFIXES: .o .r .f .l @dots{}
6587 @end group
6588 @end example
6590 @item Making Lint Libraries from C, Yacc, or Lex programs
6591 @pindex lint
6592 @cindex @code{lint}, rule to run
6593 @pindex .ln
6594 @file{@var{n}.ln} is made from @file{@var{n}.c} by running @code{lint}.
6595 The precise command is @w{@samp{$(LINT) $(LINTFLAGS) $(CPPFLAGS) -i}}.
6596 The same command is used on the C code produced from
6597 @file{@var{n}.y} or @file{@var{n}.l}.@refill
6599 @item @TeX{} and Web
6600 @cindex @TeX{}, rule to run
6601 @cindex Web, rule to run
6602 @pindex tex
6603 @pindex cweave
6604 @pindex weave
6605 @pindex tangle
6606 @pindex ctangle
6607 @pindex .dvi
6608 @pindex .tex
6609 @pindex .web
6610 @pindex .w
6611 @pindex .ch
6612 @file{@var{n}.dvi} is made from @file{@var{n}.tex} with the command
6613 @samp{$(TEX)}.  @file{@var{n}.tex} is made from @file{@var{n}.web} with
6614 @samp{$(WEAVE)}, or from @file{@var{n}.w} (and from @file{@var{n}.ch} if
6615 it exists or can be made) with @samp{$(CWEAVE)}.  @file{@var{n}.p} is
6616 made from @file{@var{n}.web} with @samp{$(TANGLE)} and @file{@var{n}.c}
6617 is made from @file{@var{n}.w} (and from @file{@var{n}.ch} if it exists
6618 or can be made) with @samp{$(CTANGLE)}.@refill
6620 @item Texinfo and Info
6621 @cindex Texinfo, rule to format
6622 @cindex Info, rule to format
6623 @pindex texi2dvi
6624 @pindex makeinfo
6625 @pindex .texinfo
6626 @pindex .info
6627 @pindex .texi
6628 @pindex .txinfo
6629 @file{@var{n}.dvi} is made from @file{@var{n}.texinfo},
6630 @file{@var{n}.texi}, or @file{@var{n}.txinfo}, with the command
6631 @w{@samp{$(TEXI2DVI) $(TEXI2DVI_FLAGS)}}.  @file{@var{n}.info} is made from
6632 @file{@var{n}.texinfo}, @file{@var{n}.texi}, or @file{@var{n}.txinfo}, with
6633 the command @w{@samp{$(MAKEINFO) $(MAKEINFO_FLAGS)}}.
6635 @item RCS
6636 @cindex RCS, rule to extract from
6637 @pindex co
6638 @pindex ,v @r{(RCS file extension)}
6639 Any file @file{@var{n}} is extracted if necessary from an RCS file
6640 named either @file{@var{n},v} or @file{RCS/@var{n},v}.  The precise
6641 command used is @w{@samp{$(CO) $(COFLAGS)}}.  @file{@var{n}} will not be
6642 extracted from RCS if it already exists, even if the RCS file is
6643 newer.  The rules for RCS are terminal
6644 (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}),
6645 so RCS files cannot be generated from another source; they must
6646 actually exist.@refill
6648 @item SCCS
6649 @cindex SCCS, rule to extract from
6650 @pindex get
6651 @pindex s. @r{(SCCS file prefix)}
6652 Any file @file{@var{n}} is extracted if necessary from an SCCS file
6653 named either @file{s.@var{n}} or @file{SCCS/s.@var{n}}.  The precise
6654 command used is @w{@samp{$(GET) $(GFLAGS)}}.  The rules for SCCS are
6655 terminal (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}),
6656 so SCCS files cannot be generated from another source; they must
6657 actually exist.@refill
6659 @pindex .sh
6660 For the benefit of SCCS, a file @file{@var{n}} is copied from
6661 @file{@var{n}.sh} and made executable (by everyone).  This is for
6662 shell scripts that are checked into SCCS.  Since RCS preserves the
6663 execution permission of a file, you do not need to use this feature
6664 with RCS.@refill
6666 We recommend that you avoid using of SCCS.  RCS is widely held to be
6667 superior, and is also free.  By choosing free software in place of
6668 comparable (or inferior) proprietary software, you support the free
6669 software movement.
6670 @end table
6672 Usually, you want to change only the variables listed in the table
6673 above, which are documented in the following section.
6675 However, the commands in built-in implicit rules actually use
6676 variables such as @code{COMPILE.c}, @code{LINK.p}, and
6677 @code{PREPROCESS.S}, whose values contain the commands listed above.
6679 @code{make} follows the convention that the rule to compile a
6680 @file{.@var{x}} source file uses the variable @code{COMPILE.@var{x}}.
6681 Similarly, the rule to produce an executable from a @file{.@var{x}}
6682 file uses @code{LINK.@var{x}}; and the rule to preprocess a
6683 @file{.@var{x}} file uses @code{PREPROCESS.@var{x}}.
6685 @vindex OUTPUT_OPTION
6686 Every rule that produces an object file uses the variable
6687 @code{OUTPUT_OPTION}.  @code{make} defines this variable either to
6688 contain @samp{-o $@@}, or to be empty, depending on a compile-time
6689 option.  You need the @samp{-o} option to ensure that the output goes
6690 into the right file when the source file is in a different directory,
6691 as when using @code{VPATH} (@pxref{Directory Search}).  However,
6692 compilers on some systems do not accept a @samp{-o} switch for object
6693 files.  If you use such a system, and use @code{VPATH}, some
6694 compilations will put their output in the wrong place.
6695 A possible workaround for this problem is to give @code{OUTPUT_OPTION}
6696 the value @w{@samp{; mv $*.o $@@}}.
6698 @node Implicit Variables, Chained Rules, Catalogue of Rules, Implicit Rules
6699 @section Variables Used by Implicit Rules
6700 @cindex flags for compilers
6702 The commands in built-in implicit rules make liberal use of certain
6703 predefined variables.  You can alter these variables in the makefile,
6704 with arguments to @code{make}, or in the environment to alter how the
6705 implicit rules work without redefining the rules themselves.
6707 For example, the command used to compile a C source file actually says
6708 @samp{$(CC) -c $(CFLAGS) $(CPPFLAGS)}.  The default values of the variables
6709 used are @samp{cc} and nothing, resulting in the command @samp{cc -c}.  By
6710 redefining @samp{CC} to @samp{ncc}, you could cause @samp{ncc} to be
6711 used for all C compilations performed by the implicit rule.  By redefining
6712 @samp{CFLAGS} to be @samp{-g}, you could pass the @samp{-g} option to
6713 each compilation.  @emph{All} implicit rules that do C compilation use
6714 @samp{$(CC)} to get the program name for the compiler and @emph{all}
6715 include @samp{$(CFLAGS)} among the arguments given to the compiler.@refill
6717 The variables used in implicit rules fall into two classes: those that are
6718 names of programs (like @code{CC}) and those that contain arguments for the
6719 programs (like @code{CFLAGS}).  (The ``name of a program'' may also contain
6720 some command arguments, but it must start with an actual executable program
6721 name.)  If a variable value contains more than one argument, separate them
6722 with spaces.
6724 Here is a table of variables used as names of programs in built-in rules:
6726 @table @code
6727 @item AR
6728 @vindex AR
6729 Archive-maintaining program; default @samp{ar}.
6730 @pindex ar
6732 @item AS
6733 @vindex AS
6734 Program for doing assembly; default @samp{as}.
6735 @pindex as
6737 @item CC
6738 @vindex CC
6739 Program for compiling C programs; default @samp{cc}.
6740 @pindex cc
6742 @item CXX
6743 @vindex CXX
6744 Program for compiling C++ programs; default @samp{g++}.
6745 @pindex g++
6747 @item CO
6748 @vindex CO
6749 Program for extracting a file from RCS; default @samp{co}.
6750 @pindex co
6752 @item CPP
6753 @vindex CPP
6754 Program for running the C preprocessor, with results to standard output;
6755 default @samp{$(CC) -E}.
6757 @item FC
6758 @vindex FC
6759 Program for compiling or preprocessing Fortran and Ratfor programs;
6760 default @samp{f77}.
6761 @pindex f77
6763 @item GET
6764 @vindex GET
6765 Program for extracting a file from SCCS; default @samp{get}.
6766 @pindex get
6768 @item LEX
6769 @vindex LEX
6770 Program to use to turn Lex grammars into C programs or Ratfor programs;
6771 default @samp{lex}.
6772 @pindex lex
6774 @item PC
6775 @vindex PC
6776 Program for compiling Pascal programs; default @samp{pc}.
6777 @pindex pc
6779 @item YACC
6780 @vindex YACC
6781 Program to use to turn Yacc grammars into C programs; default @samp{yacc}.
6782 @pindex yacc
6784 @item YACCR
6785 @vindex YACCR
6786 Program to use to turn Yacc grammars into Ratfor
6787 programs; default @samp{yacc -r}.
6789 @item MAKEINFO
6790 @vindex MAKEINFO
6791 Program to convert a Texinfo source file into an Info file; default
6792 @samp{makeinfo}.
6793 @pindex makeinfo
6795 @item TEX
6796 @vindex TEX
6797 Program to make @TeX{} @sc{dvi} files from @TeX{} source;
6798 default @samp{tex}.
6799 @pindex tex
6801 @item TEXI2DVI
6802 @vindex TEXI2DVI
6803 Program to make @TeX{} @sc{dvi} files from Texinfo source;
6804 default @samp{texi2dvi}.
6805 @pindex texi2dvi
6807 @item WEAVE
6808 @vindex WEAVE
6809 Program to translate Web into @TeX{}; default @samp{weave}.
6810 @pindex weave
6812 @item CWEAVE
6813 @vindex CWEAVE
6814 Program to translate C Web into @TeX{}; default @samp{cweave}.
6815 @pindex cweave
6817 @item TANGLE
6818 @vindex TANGLE
6819 Program to translate Web into Pascal; default @samp{tangle}.
6820 @pindex tangle
6822 @item CTANGLE
6823 @vindex CTANGLE
6824 Program to translate C Web into C; default @samp{ctangle}.
6825 @pindex ctangle
6827 @item RM
6828 @vindex RM
6829 Command to remove a file; default @samp{rm -f}.
6830 @pindex rm
6831 @end table
6833 Here is a table of variables whose values are additional arguments for the
6834 programs above.  The default values for all of these is the empty
6835 string, unless otherwise noted.
6837 @table @code
6838 @item ARFLAGS
6839 @vindex ARFLAGS
6840 Flags to give the archive-maintaining program; default @samp{rv}.
6842 @item ASFLAGS
6843 @vindex ASFLAGS
6844 Extra flags to give to the assembler (when explicitly
6845 invoked on a @samp{.s} or @samp{.S} file).
6847 @item CFLAGS
6848 @vindex CFLAGS
6849 Extra flags to give to the C compiler.
6851 @item CXXFLAGS
6852 @vindex CXXFLAGS
6853 Extra flags to give to the C++ compiler.
6855 @item COFLAGS
6856 @vindex COFLAGS
6857 Extra flags to give to the RCS @code{co} program.
6859 @item CPPFLAGS
6860 @vindex CPPFLAGS
6861 Extra flags to give to the C preprocessor and programs
6862 that use it (the C and Fortran compilers).
6864 @item FFLAGS
6865 @vindex FFLAGS
6866 Extra flags to give to the Fortran compiler.
6868 @item GFLAGS
6869 @vindex GFLAGS
6870 Extra flags to give to the SCCS @code{get} program.
6872 @item LDFLAGS
6873 @vindex LDFLAGS
6874 Extra flags to give to compilers when they are
6875 supposed to invoke the linker, @samp{ld}.
6877 @item LFLAGS
6878 @vindex LFLAGS
6879 Extra flags to give to Lex.
6881 @item PFLAGS
6882 @vindex PFLAGS
6883 Extra flags to give to the Pascal compiler.
6885 @item RFLAGS
6886 @vindex RFLAGS
6887 Extra flags to give to the Fortran compiler for Ratfor programs.
6889 @item YFLAGS
6890 @vindex YFLAGS
6891 Extra flags to give to Yacc.
6892 @end table
6894 @node Chained Rules, Pattern Rules, Implicit Variables, Implicit Rules
6895 @section Chains of Implicit Rules
6897 @cindex chains of rules
6898 @cindex rule, implicit, chains of
6899 Sometimes a file can be made by a sequence of implicit rules.  For example,
6900 a file @file{@var{n}.o} could be made from @file{@var{n}.y} by running
6901 first Yacc and then @code{cc}.  Such a sequence is called a @dfn{chain}.
6903 If the file @file{@var{n}.c} exists, or is mentioned in the makefile, no
6904 special searching is required: @code{make} finds that the object file can
6905 be made by C compilation from @file{@var{n}.c}; later on, when considering
6906 how to make @file{@var{n}.c}, the rule for running Yacc is
6907 used.  Ultimately both @file{@var{n}.c} and @file{@var{n}.o} are
6908 updated.@refill
6910 @cindex intermediate files
6911 @cindex files, intermediate
6912 However, even if @file{@var{n}.c} does not exist and is not mentioned,
6913 @code{make} knows how to envision it as the missing link between
6914 @file{@var{n}.o} and @file{@var{n}.y}!  In this case, @file{@var{n}.c} is
6915 called an @dfn{intermediate file}.  Once @code{make} has decided to use the
6916 intermediate file, it is entered in the data base as if it had been
6917 mentioned in the makefile, along with the implicit rule that says how to
6918 create it.@refill
6920 Intermediate files are remade using their rules just like all other
6921 files.  But intermediate files are treated differently in two ways.
6923 The first difference is what happens if the intermediate file does not
6924 exist.  If an ordinary file @var{b} does not exist, and @code{make}
6925 considers a target that depends on @var{b}, it invariably creates
6926 @var{b} and then updates the target from @var{b}.  But if @var{b} is an
6927 intermediate file, then @code{make} can leave well enough alone.  It
6928 won't bother updating @var{b}, or the ultimate target, unless some
6929 dependency of @var{b} is newer than that target or there is some other
6930 reason to update that target.
6932 The second difference is that if @code{make} @emph{does} create @var{b}
6933 in order to update something else, it deletes @var{b} later on after it
6934 is no longer needed.  Therefore, an intermediate file which did not
6935 exist before @code{make} also does not exist after @code{make}.
6936 @code{make} reports the deletion to you by printing a @samp{rm -f}
6937 command showing which file it is deleting.
6939 Ordinarily, a file cannot be intermediate if it is mentioned in the
6940 makefile as a target or dependency.  However, you can explicitly mark a
6941 file as intermediate by listing it as a dependency of the special target
6942 @code{.INTERMEDIATE}.  This takes effect even if the file is mentioned
6943 explicitly in some other way.
6945 @cindex intermediate files, preserving
6946 @cindex preserving intermediate files
6947 @cindex secondary files
6948 You can prevent automatic deletion of an intermediate file by marking it
6949 as a @dfn{secondary} file.  To do this, list it as a dependency of the
6950 special target @code{.SECONDARY}.  When a file is secondary, @code{make}
6951 will not create the file merely because it does not already exist, but
6952 @code{make} does not automatically delete the file.  Marking a file as
6953 secondary also marks it as intermediate.
6955 You can list the target pattern of an implicit rule (such as @samp{%.o})
6956 as a dependency of the special target @code{.PRECIOUS} to preserve
6957 intermediate files made by implicit rules whose target patterns match
6958 that file's name; see @ref{Interrupts}.@refill
6959 @cindex preserving with @code{.PRECIOUS}
6960 @cindex @code{.PRECIOUS} intermediate files
6962 A chain can involve more than two implicit rules.  For example, it is
6963 possible to make a file @file{foo} from @file{RCS/foo.y,v} by running RCS,
6964 Yacc and @code{cc}.  Then both @file{foo.y} and @file{foo.c} are
6965 intermediate files that are deleted at the end.@refill
6967 No single implicit rule can appear more than once in a chain.  This means
6968 that @code{make} will not even consider such a ridiculous thing as making
6969 @file{foo} from @file{foo.o.o} by running the linker twice.  This
6970 constraint has the added benefit of preventing any infinite loop in the
6971 search for an implicit rule chain.
6973 There are some special implicit rules to optimize certain cases that would
6974 otherwise be handled by rule chains.  For example, making @file{foo} from
6975 @file{foo.c} could be handled by compiling and linking with separate
6976 chained rules, using @file{foo.o} as an intermediate file.  But what
6977 actually happens is that a special rule for this case does the compilation
6978 and linking with a single @code{cc} command.  The optimized rule is used in
6979 preference to the step-by-step chain because it comes earlier in the
6980 ordering of rules.
6982 @node Pattern Rules, Last Resort, Chained Rules, Implicit Rules
6983 @section Defining and Redefining Pattern Rules
6985 You define an implicit rule by writing a @dfn{pattern rule}.  A pattern
6986 rule looks like an ordinary rule, except that its target contains the
6987 character @samp{%} (exactly one of them).  The target is considered a
6988 pattern for matching file names; the @samp{%} can match any nonempty
6989 substring, while other characters match only themselves.  The dependencies
6990 likewise use @samp{%} to show how their names relate to the target name.
6992 Thus, a pattern rule @samp{%.o : %.c} says how to make any file
6993 @file{@var{stem}.o} from another file @file{@var{stem}.c}.@refill
6995 Note that expansion using @samp{%} in pattern rules occurs
6996 @strong{after} any variable or function expansions, which take place
6997 when the makefile is read.  @xref{Using Variables, , How to Use
6998 Variables}, and @ref{Functions, ,Functions for Transforming Text}.
7000 @menu
7001 * Pattern Intro::               An introduction to pattern rules.
7002 * Pattern Examples::            Examples of pattern rules.
7003 * Automatic::                   How to use automatic variables in the
7004                                   commands of implicit rules.
7005 * Pattern Match::               How patterns match.
7006 * Match-Anything Rules::        Precautions you should take prior to
7007                                   defining rules that can match any
7008                                   target file whatever.
7009 * Canceling Rules::             How to override or cancel built-in rules.
7010 @end menu
7012 @node Pattern Intro, Pattern Examples,  , Pattern Rules
7013 @subsection Introduction to Pattern Rules
7014 @cindex pattern rule
7015 @cindex rule, pattern
7017 A pattern rule contains the character @samp{%} (exactly one of them)
7018 in the target; otherwise, it looks exactly like an ordinary rule.  The
7019 target is a pattern for matching file names; the @samp{%} matches any
7020 nonempty substring, while other characters match only themselves.
7021 @cindex target pattern, implicit
7022 @cindex @code{%}, in pattern rules
7024 For example, @samp{%.c} as a pattern matches any file name that ends in
7025 @samp{.c}.  @samp{s.%.c} as a pattern matches any file name that starts
7026 with @samp{s.}, ends in @samp{.c} and is at least five characters long.
7027 (There must be at least one character to match the @samp{%}.)  The substring
7028 that the @samp{%} matches is called the @dfn{stem}.@refill
7030 @samp{%} in a dependency of a pattern rule stands for the same stem
7031 that was matched by the @samp{%} in the target.  In order for
7032 the pattern rule to apply, its target pattern must match the file name
7033 under consideration, and its dependency patterns must name files that
7034 exist or can be made.  These files become dependencies of the target.
7035 @cindex dependency pattern, implicit
7037 Thus, a rule of the form
7039 @example
7040 %.o : %.c ; @var{command}@dots{}
7041 @end example
7043 @noindent
7044 specifies how to make a file @file{@var{n}.o}, with another file
7045 @file{@var{n}.c} as its dependency, provided that @file{@var{n}.c}
7046 exists or can be made.
7048 There may also be dependencies that do not use @samp{%}; such a dependency
7049 attaches to every file made by this pattern rule.  These unvarying
7050 dependencies are useful occasionally.
7052 A pattern rule need not have any dependencies that contain @samp{%}, or
7053 in fact any dependencies at all.  Such a rule is effectively a general
7054 wildcard.  It provides a way to make any file that matches the target
7055 pattern.  @xref{Last Resort}.
7057 @c !!! The end of of this paragraph should be rewritten.  --bob
7058 Pattern rules may have more than one target.  Unlike normal rules, this
7059 does not act as many different rules with the same dependencies and
7060 commands.  If a pattern rule has multiple targets, @code{make} knows that
7061 the rule's commands are responsible for making all of the targets.  The
7062 commands are executed only once to make all the targets.  When searching
7063 for a pattern rule to match a target, the target patterns of a rule other
7064 than the one that matches the target in need of a rule are incidental:
7065 @code{make} worries only about giving commands and dependencies to the file
7066 presently in question.  However, when this file's commands are run, the
7067 other targets are marked as having been updated themselves.
7068 @cindex multiple targets, in pattern rule
7069 @cindex target, multiple in pattern rule
7071 The order in which pattern rules appear in the makefile is important
7072 since this is the order in which they are considered.
7073 Of equally applicable
7074 rules, only the first one found is used.  The rules you write take precedence
7075 over those that are built in.  Note however, that a rule whose
7076 dependencies actually exist or are mentioned always takes priority over a
7077 rule with dependencies that must be made by chaining other implicit rules.
7078 @cindex pattern rules, order of
7079 @cindex order of pattern rules
7081 @node Pattern Examples, Automatic, Pattern Intro, Pattern Rules
7082 @subsection Pattern Rule Examples
7084 Here are some examples of pattern rules actually predefined in
7085 @code{make}.  First, the rule that compiles @samp{.c} files into @samp{.o}
7086 files:@refill
7088 @example
7089 %.o : %.c
7090         $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@@
7091 @end example
7093 @noindent
7094 defines a rule that can make any file @file{@var{x}.o} from
7095 @file{@var{x}.c}.  The command uses the automatic variables @samp{$@@} and
7096 @samp{$<} to substitute the names of the target file and the source file
7097 in each case where the rule applies (@pxref{Automatic, ,Automatic Variables}).@refill
7099 Here is a second built-in rule:
7101 @example
7102 % :: RCS/%,v
7103         $(CO) $(COFLAGS) $<
7104 @end example
7106 @noindent
7107 defines a rule that can make any file @file{@var{x}} whatsoever from a
7108 corresponding file @file{@var{x},v} in the subdirectory @file{RCS}.  Since
7109 the target is @samp{%}, this rule will apply to any file whatever, provided
7110 the appropriate dependency file exists.  The double colon makes the rule
7111 @dfn{terminal}, which means that its dependency may not be an intermediate
7112 file (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}).@refill
7114 @need 500
7115 This pattern rule has two targets:
7117 @example
7118 @group
7119 %.tab.c %.tab.h: %.y
7120         bison -d $<
7121 @end group
7122 @end example
7124 @noindent
7125 @c The following paragraph is rewritten to avoid overfull hboxes
7126 This tells @code{make} that the command @samp{bison -d @var{x}.y} will
7127 make both @file{@var{x}.tab.c} and @file{@var{x}.tab.h}.  If the file
7128 @file{foo} depends on the files @file{parse.tab.o} and @file{scan.o}
7129 and the file @file{scan.o} depends on the file @file{parse.tab.h},
7130 when @file{parse.y} is changed, the command @samp{bison -d parse.y}
7131 will be executed only once, and the dependencies of both
7132 @file{parse.tab.o} and @file{scan.o} will be satisfied.  (Presumably
7133 the file @file{parse.tab.o} will be recompiled from @file{parse.tab.c}
7134 and the file @file{scan.o} from @file{scan.c}, while @file{foo} is
7135 linked from @file{parse.tab.o}, @file{scan.o}, and its other
7136 dependencies, and it will execute happily ever after.)@refill
7138 @node Automatic, Pattern Match, Pattern Examples, Pattern Rules
7139 @subsection Automatic Variables
7140 @cindex automatic variables
7141 @cindex variables, automatic
7142 @cindex variables, and implicit rule
7144 Suppose you are writing a pattern rule to compile a @samp{.c} file into a
7145 @samp{.o} file: how do you write the @samp{cc} command so that it operates
7146 on the right source file name?  You cannot write the name in the command,
7147 because the name is different each time the implicit rule is applied.
7149 What you do is use a special feature of @code{make}, the @dfn{automatic
7150 variables}.  These variables have values computed afresh for each rule that
7151 is executed, based on the target and dependencies of the rule.  In this
7152 example, you would use @samp{$@@} for the object file name and @samp{$<}
7153 for the source file name.
7155 Here is a table of automatic variables:
7157 @table @code
7158 @vindex $@@
7159 @vindex @@ @r{(automatic variable)}
7160 @item $@@
7161 The file name of the target of the rule.  If the target is an archive
7162 member, then @samp{$@@} is the name of the archive file.  In a pattern
7163 rule that has multiple targets (@pxref{Pattern Intro, ,Introduction to
7164 Pattern Rules}), @samp{$@@} is the name of whichever target caused the
7165 rule's commands to be run.
7167 @vindex $%
7168 @vindex % @r{(automatic variable)}
7169 @item $%
7170 The target member name, when the target is an archive member.
7171 @xref{Archives}.  For example, if the target is @file{foo.a(bar.o)} then
7172 @samp{$%} is @file{bar.o} and @samp{$@@} is @file{foo.a}.  @samp{$%} is
7173 empty when the target is not an archive member.
7175 @vindex $<
7176 @vindex < @r{(automatic variable)}
7177 @item $<
7178 The name of the first dependency.  If the target got its commands from
7179 an implicit rule, this will be the first dependency added by the
7180 implicit rule (@pxref{Implicit Rules}).
7182 @vindex $?
7183 @vindex ? @r{(automatic variable)}
7184 @item $?
7185 The names of all the dependencies that are newer than the target, with
7186 spaces between them.  For dependencies which are archive members, only
7187 the member named is used (@pxref{Archives}).
7188 @cindex dependencies, list of changed
7189 @cindex list of changed dependencies
7191 @vindex $^
7192 @vindex ^ @r{(automatic variable)}
7193 @item $^
7194 The names of all the dependencies, with spaces between them.  For
7195 dependencies which are archive members, only the member named is used
7196 (@pxref{Archives}).  A target has only one dependency on each other file
7197 it depends on, no matter how many times each file is listed as a
7198 dependency.  So if you list a dependency more than once for a target,
7199 the value of @code{$^} contains just one copy of the name.
7200 @cindex dependencies, list of all
7201 @cindex list of all dependencies
7203 @vindex $+
7204 @vindex + @r{(automatic variable)}
7205 @item $+
7206 This is like @samp{$^}, but dependencies listed more than once are
7207 duplicated in the order they were listed in the makefile.  This is
7208 primarily useful for use in linking commands where it is meaningful to
7209 repeat library file names in a particular order.
7211 @vindex $*
7212 @vindex * @r{(automatic variable)}
7213 @item $*
7214 The stem with which an implicit rule matches (@pxref{Pattern Match, ,How
7215 Patterns Match}).  If the target is @file{dir/a.foo.b} and the target
7216 pattern is @file{a.%.b} then the stem is @file{dir/foo}.  The stem is
7217 useful for constructing names of related files.@refill
7218 @cindex stem, variable for
7220 In a static pattern rule, the stem is part of the file name that matched
7221 the @samp{%} in the target pattern.
7223 In an explicit rule, there is no stem; so @samp{$*} cannot be determined
7224 in that way.  Instead, if the target name ends with a recognized suffix
7225 (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}), @samp{$*} is set to
7226 the target name minus the suffix.  For example, if the target name is
7227 @samp{foo.c}, then @samp{$*} is set to @samp{foo}, since @samp{.c} is a
7228 suffix.  GNU @code{make} does this bizarre thing only for compatibility
7229 with other implementations of @code{make}.  You should generally avoid
7230 using @samp{$*} except in implicit rules or static pattern rules.@refill
7232 If the target name in an explicit rule does not end with a recognized
7233 suffix, @samp{$*} is set to the empty string for that rule.
7234 @end table
7236 @samp{$?} is useful even in explicit rules when you wish to operate on only
7237 the dependencies that have changed.  For example, suppose that an archive
7238 named @file{lib} is supposed to contain copies of several object files.
7239 This rule copies just the changed object files into the archive:
7241 @example
7242 @group
7243 lib: foo.o bar.o lose.o win.o
7244         ar r lib $?
7245 @end group
7246 @end example
7248 Of the variables listed above, four have values that are single file
7249 names, and two have values that are lists of file names.  These six have
7250 variants that get just the file's directory name or just the file name
7251 within the directory.  The variant variables' names are formed by
7252 appending @samp{D} or @samp{F}, respectively.  These variants are
7253 semi-obsolete in GNU @code{make} since the functions @code{dir} and
7254 @code{notdir} can be used to get a similar effect (@pxref{Filename
7255 Functions, , Functions for File Names}).  Note, however, that the
7256 @samp{F} variants all omit the trailing slash which always appears in
7257 the output of the @code{dir} function.  Here is a table of the variants:
7259 @table @samp
7260 @vindex $(@@D)
7261 @vindex @@D @r{(automatic variable)}
7262 @item $(@@D)
7263 The directory part of the file name of the target, with the trailing
7264 slash removed.  If the value of @samp{$@@} is @file{dir/foo.o} then
7265 @samp{$(@@D)} is @file{dir}.  This value is @file{.} if @samp{$@@} does
7266 not contain a slash.
7268 @vindex $(@@F)
7269 @vindex @@F @r{(automatic variable)}
7270 @item $(@@F)
7271 The file-within-directory part of the file name of the target.  If the
7272 value of @samp{$@@} is @file{dir/foo.o} then @samp{$(@@F)} is
7273 @file{foo.o}.  @samp{$(@@F)} is equivalent to @samp{$(notdir $@@)}.
7275 @vindex $(*D)
7276 @vindex *D @r{(automatic variable)}
7277 @item $(*D)
7278 @vindex $(*F)
7279 @vindex *F @r{(automatic variable)}
7280 @itemx $(*F)
7281 The directory part and the file-within-directory
7282 part of the stem; @file{dir} and @file{foo} in this example.
7284 @vindex $(%D)
7285 @vindex %D @r{(automatic variable)}
7286 @item $(%D)
7287 @vindex $(%F)
7288 @vindex %F @r{(automatic variable)}
7289 @itemx $(%F)
7290 The directory part and the file-within-directory part of the target
7291 archive member name.  This makes sense only for archive member targets
7292 of the form @file{@var{archive}(@var{member})} and is useful only when
7293 @var{member} may contain a directory name.  (@xref{Archive Members,
7294 ,Archive Members as Targets}.)
7296 @vindex $(<D)
7297 @vindex <D @r{(automatic variable)}
7298 @item $(<D)
7299 @vindex $(<F)
7300 @vindex <F @r{(automatic variable)}
7301 @itemx $(<F)
7302 The directory part and the file-within-directory
7303 part of the first dependency.
7305 @vindex $(^D)
7306 @vindex ^D @r{(automatic variable)}
7307 @item $(^D)
7308 @vindex $(^F)
7309 @vindex ^F @r{(automatic variable)}
7310 @itemx $(^F)
7311 Lists of the directory parts and the file-within-directory
7312 parts of all dependencies.
7314 @vindex $(?D)
7315 @vindex ?D @r{(automatic variable)}
7316 @item $(?D)
7317 @vindex $(?F)
7318 @vindex ?F @r{(automatic variable)}
7319 @itemx $(?F)
7320 Lists of the directory parts and the file-within-directory parts of
7321 all dependencies that are newer than the target.
7322 @end table
7324 Note that we use a special stylistic convention when we talk about these
7325 automatic variables; we write ``the value of @samp{$<}'', rather than
7326 @w{``the variable @code{<}''} as we would write for ordinary variables
7327 such as @code{objects} and @code{CFLAGS}.  We think this convention
7328 looks more natural in this special case.  Please do not assume it has a
7329 deep significance; @samp{$<} refers to the variable named @code{<} just
7330 as @samp{$(CFLAGS)} refers to the variable named @code{CFLAGS}.
7331 You could just as well use @samp{$(<)} in place of @samp{$<}.
7333 @node Pattern Match, Match-Anything Rules, Automatic, Pattern Rules
7334 @subsection How Patterns Match
7336 @cindex stem
7337 A target pattern is composed of a @samp{%} between a prefix and a suffix,
7338 either or both of which may be empty.  The pattern matches a file name only
7339 if the file name starts with the prefix and ends with the suffix, without
7340 overlap.  The text between the prefix and the suffix is called the
7341 @dfn{stem}.  Thus, when the pattern @samp{%.o} matches the file name
7342 @file{test.o}, the stem is @samp{test}.  The pattern rule dependencies are
7343 turned into actual file names by substituting the stem for the character
7344 @samp{%}.  Thus, if in the same example one of the dependencies is written
7345 as @samp{%.c}, it expands to @samp{test.c}.@refill
7347 When the target pattern does not contain a slash (and it usually does
7348 not), directory names in the file names are removed from the file name
7349 before it is compared with the target prefix and suffix.  After the
7350 comparison of the file name to the target pattern, the directory
7351 names, along with the slash that ends them, are added on to the
7352 dependency file names generated from the pattern rule's dependency
7353 patterns and the file name. The directories are ignored only for the
7354 purpose of finding an implicit rule to use, not in the application of
7355 that rule.  Thus, @samp{e%t} matches the file name @file{src/eat},
7356 with @samp{src/a} as the stem.  When dependencies are turned into file
7357 names, the directories from the stem are added at the front, while the
7358 rest of the stem is substituted for the @samp{%}.  The stem
7359 @samp{src/a} with a dependency pattern @samp{c%r} gives the file name
7360 @file{src/car}.@refill
7362 @node Match-Anything Rules, Canceling Rules, Pattern Match, Pattern Rules
7363 @subsection Match-Anything Pattern Rules
7365 @cindex match-anything rule
7366 @cindex terminal rule
7367 When a pattern rule's target is just @samp{%}, it matches any file name
7368 whatever.  We call these rules @dfn{match-anything} rules.  They are very
7369 useful, but it can take a lot of time for @code{make} to think about them,
7370 because it must consider every such rule for each file name listed either
7371 as a target or as a dependency.
7373 Suppose the makefile mentions @file{foo.c}.  For this target, @code{make}
7374 would have to consider making it by linking an object file @file{foo.c.o},
7375 or by C compilation-and-linking in one step from @file{foo.c.c}, or by
7376 Pascal compilation-and-linking from @file{foo.c.p}, and many other
7377 possibilities.
7379 We know these possibilities are ridiculous since @file{foo.c} is a C source
7380 file, not an executable.  If @code{make} did consider these possibilities,
7381 it would ultimately reject them, because files such as @file{foo.c.o} and
7382 @file{foo.c.p} would not exist.  But these possibilities are so
7383 numerous that @code{make} would run very slowly if it had to consider
7384 them.@refill
7386 To gain speed, we have put various constraints on the way @code{make}
7387 considers match-anything rules.  There are two different constraints that
7388 can be applied, and each time you define a match-anything rule you must
7389 choose one or the other for that rule.
7391 One choice is to mark the match-anything rule as @dfn{terminal} by defining
7392 it with a double colon.  When a rule is terminal, it does not apply unless
7393 its dependencies actually exist.  Dependencies that could be made with
7394 other implicit rules are not good enough.  In other words, no further
7395 chaining is allowed beyond a terminal rule.
7397 For example, the built-in implicit rules for extracting sources from RCS
7398 and SCCS files are terminal; as a result, if the file @file{foo.c,v} does
7399 not exist, @code{make} will not even consider trying to make it as an
7400 intermediate file from @file{foo.c,v.o} or from @file{RCS/SCCS/s.foo.c,v}.
7401 RCS and SCCS files are generally ultimate source files, which should not be
7402 remade from any other files; therefore, @code{make} can save time by not
7403 looking for ways to remake them.@refill
7405 If you do not mark the match-anything rule as terminal, then it is
7406 nonterminal.  A nonterminal match-anything rule cannot apply to a file name
7407 that indicates a specific type of data.  A file name indicates a specific
7408 type of data if some non-match-anything implicit rule target matches it.
7410 For example, the file name @file{foo.c} matches the target for the pattern
7411 rule @samp{%.c : %.y} (the rule to run Yacc).  Regardless of whether this
7412 rule is actually applicable (which happens only if there is a file
7413 @file{foo.y}), the fact that its target matches is enough to prevent
7414 consideration of any nonterminal match-anything rules for the file
7415 @file{foo.c}.  Thus, @code{make} will not even consider trying to make
7416 @file{foo.c} as an executable file from @file{foo.c.o}, @file{foo.c.c},
7417 @file{foo.c.p}, etc.@refill
7419 The motivation for this constraint is that nonterminal match-anything
7420 rules are used for making files containing specific types of data (such as
7421 executable files) and a file name with a recognized suffix indicates some
7422 other specific type of data (such as a C source file).
7424 Special built-in dummy pattern rules are provided solely to recognize
7425 certain file names so that nonterminal match-anything rules will not be
7426 considered.  These dummy rules have no dependencies and no commands, and
7427 they are ignored for all other purposes.  For example, the built-in
7428 implicit rule
7430 @example
7431 %.p :
7432 @end example
7434 @noindent
7435 exists to make sure that Pascal source files such as @file{foo.p} match a
7436 specific target pattern and thereby prevent time from being wasted looking
7437 for @file{foo.p.o} or @file{foo.p.c}.
7439 Dummy pattern rules such as the one for @samp{%.p} are made for every
7440 suffix listed as valid for use in suffix rules (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}).
7442 @node Canceling Rules,  , Match-Anything Rules, Pattern Rules
7443 @subsection Canceling Implicit Rules
7445 You can override a built-in implicit rule (or one you have defined
7446 yourself) by defining a new pattern rule with the same target and
7447 dependencies, but different commands.  When the new rule is defined, the
7448 built-in one is replaced.  The new rule's position in the sequence of
7449 implicit rules is determined by where you write the new rule.
7451 You can cancel a built-in implicit rule by defining a pattern rule with the
7452 same target and dependencies, but no commands.  For example, the following
7453 would cancel the rule that runs the assembler:
7455 @example
7456 %.o : %.s
7457 @end example
7459 @node Last Resort, Suffix Rules, Pattern Rules, Implicit Rules
7460 @section Defining Last-Resort Default Rules
7461 @cindex last-resort default rules
7462 @cindex default rules, last-resort
7464 You can define a last-resort implicit rule by writing a terminal
7465 match-anything pattern rule with no dependencies (@pxref{Match-Anything
7466 Rules}).  This is just like any other pattern rule; the only thing
7467 special about it is that it will match any target.  So such a rule's
7468 commands are used for all targets and dependencies that have no commands
7469 of their own and for which no other implicit rule applies.
7471 For example, when testing a makefile, you might not care if the source
7472 files contain real data, only that they exist.  Then you might do this:
7474 @example
7476         touch $@@
7477 @end example
7479 @noindent
7480 to cause all the source files needed (as dependencies) to be created
7481 automatically.
7483 @findex .DEFAULT
7484 You can instead define commands to be used for targets for which there
7485 are no rules at all, even ones which don't specify commands.  You do
7486 this by writing a rule for the target @code{.DEFAULT}.  Such a rule's
7487 commands are used for all dependencies which do not appear as targets in
7488 any explicit rule, and for which no implicit rule applies.  Naturally,
7489 there is no @code{.DEFAULT} rule unless you write one.
7491 If you use @code{.DEFAULT} with no commands or dependencies:
7493 @example
7494 .DEFAULT:
7495 @end example
7497 @noindent
7498 the commands previously stored for @code{.DEFAULT} are cleared.
7499 Then @code{make} acts as if you had never defined @code{.DEFAULT} at all.
7501 If you do not want a target to get the commands from a match-anything
7502 pattern rule or @code{.DEFAULT}, but you also do not want any commands
7503 to be run for the target, you can give it empty commands (@pxref{Empty
7504 Commands, ,Defining Empty Commands}).@refill
7506 You can use a last-resort rule to override part of another makefile.
7507 @xref{Overriding Makefiles, , Overriding Part of Another Makefile}.
7509 @node Suffix Rules, Search Algorithm, Last Resort, Implicit Rules
7510 @section Old-Fashioned Suffix Rules
7511 @cindex old-fashioned suffix rules
7512 @cindex suffix rule
7514 @dfn{Suffix rules} are the old-fashioned way of defining implicit rules for
7515 @code{make}.  Suffix rules are obsolete because pattern rules are more
7516 general and clearer.  They are supported in GNU @code{make} for
7517 compatibility with old makefiles.  They come in two kinds:
7518 @dfn{double-suffix} and @dfn{single-suffix}.@refill
7520 A double-suffix rule is defined by a pair of suffixes: the target suffix
7521 and the source suffix.  It matches any file whose name ends with the
7522 target suffix.  The corresponding implicit dependency is made by
7523 replacing the target suffix with the source suffix in the file name.  A
7524 two-suffix rule whose target and source suffixes are @samp{.o} and
7525 @samp{.c} is equivalent to the pattern rule @samp{%.o : %.c}.
7527 A single-suffix rule is defined by a single suffix, which is the source
7528 suffix.  It matches any file name, and the corresponding implicit
7529 dependency name is made by appending the source suffix.  A single-suffix
7530 rule whose source suffix is @samp{.c} is equivalent to the pattern rule
7531 @samp{% : %.c}.
7533 Suffix rule definitions are recognized by comparing each rule's target
7534 against a defined list of known suffixes.  When @code{make} sees a rule
7535 whose target is a known suffix, this rule is considered a single-suffix
7536 rule.  When @code{make} sees a rule whose target is two known suffixes
7537 concatenated, this rule is taken as a double-suffix rule.
7539 For example, @samp{.c} and @samp{.o} are both on the default list of
7540 known suffixes.  Therefore, if you define a rule whose target is
7541 @samp{.c.o}, @code{make} takes it to be a double-suffix rule with source
7542 suffix @samp{.c} and target suffix @samp{.o}.  Here is the old-fashioned
7543 way to define the rule for compiling a C source file:@refill
7545 @example
7546 .c.o:
7547         $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
7548 @end example
7550 Suffix rules cannot have any dependencies of their own.  If they have any,
7551 they are treated as normal files with funny names, not as suffix rules.
7552 Thus, the rule:
7554 @example
7555 .c.o: foo.h
7556         $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
7557 @end example
7559 @noindent
7560 tells how to make the file @file{.c.o} from the dependency file
7561 @file{foo.h}, and is not at all like the pattern rule:
7563 @example
7564 %.o: %.c foo.h
7565         $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
7566 @end example
7568 @noindent
7569 which tells how to make @samp{.o} files from @samp{.c} files, and makes all
7570 @samp{.o} files using this pattern rule also depend on @file{foo.h}.
7572 Suffix rules with no commands are also meaningless.  They do not remove
7573 previous rules as do pattern rules with no commands (@pxref{Canceling
7574 Rules, , Canceling Implicit Rules}).  They simply enter the suffix or pair of suffixes concatenated as
7575 a target in the data base.@refill
7577 @findex .SUFFIXES
7578 The known suffixes are simply the names of the dependencies of the special
7579 target @code{.SUFFIXES}.  You can add your own suffixes by writing a rule
7580 for @code{.SUFFIXES} that adds more dependencies, as in:
7582 @example
7583 .SUFFIXES: .hack .win
7584 @end example
7586 @noindent
7587 which adds @samp{.hack} and @samp{.win} to the end of the list of suffixes.
7589 If you wish to eliminate the default known suffixes instead of just adding
7590 to them, write a rule for @code{.SUFFIXES} with no dependencies.  By
7591 special dispensation, this eliminates all existing dependencies of
7592 @code{.SUFFIXES}.  You can then write another rule to add the suffixes you
7593 want.  For example,
7595 @example
7596 @group
7597 .SUFFIXES:            # @r{Delete the default suffixes}
7598 .SUFFIXES: .c .o .h   # @r{Define our suffix list}
7599 @end group
7600 @end example
7602 The @samp{-r} or @samp{--no-builtin-rules} flag causes the default
7603 list of suffixes to be empty.
7605 @vindex SUFFIXES
7606 The variable @code{SUFFIXES} is defined to the default list of suffixes
7607 before @code{make} reads any makefiles.  You can change the list of suffixes
7608 with a rule for the special target @code{.SUFFIXES}, but that does not alter
7609 this variable.
7611 @node Search Algorithm,  , Suffix Rules, Implicit Rules
7612 @section Implicit Rule Search Algorithm
7613 @cindex implicit rule, search algorithm
7614 @cindex search algorithm, implicit rule
7616 Here is the procedure @code{make} uses for searching for an implicit rule
7617 for a target @var{t}.  This procedure is followed for each double-colon
7618 rule with no commands, for each target of ordinary rules none of which have
7619 commands, and for each dependency that is not the target of any rule.  It
7620 is also followed recursively for dependencies that come from implicit
7621 rules, in the search for a chain of rules.
7623 Suffix rules are not mentioned in this algorithm because suffix rules are
7624 converted to equivalent pattern rules once the makefiles have been read in.
7626 For an archive member target of the form
7627 @samp{@var{archive}(@var{member})}, the following algorithm is run
7628 twice, first using the entire target name @var{t}, and second using
7629 @samp{(@var{member})} as the target @var{t} if the first run found no
7630 rule.@refill
7632 @enumerate
7633 @item
7634 Split @var{t} into a directory part, called @var{d}, and the rest,
7635 called @var{n}.  For example, if @var{t} is @samp{src/foo.o}, then
7636 @var{d} is @samp{src/} and @var{n} is @samp{foo.o}.@refill
7638 @item
7639 Make a list of all the pattern rules one of whose targets matches
7640 @var{t} or @var{n}.  If the target pattern contains a slash, it is
7641 matched against @var{t}; otherwise, against @var{n}.
7643 @item
7644 If any rule in that list is @emph{not} a match-anything rule, then
7645 remove all nonterminal match-anything rules from the list.
7647 @item
7648 Remove from the list all rules with no commands.
7650 @item
7651 For each pattern rule in the list:
7653 @enumerate a
7654 @item
7655 Find the stem @var{s}, which is the nonempty part of @var{t} or @var{n}
7656 matched by the @samp{%} in the target pattern.@refill
7658 @item
7659 Compute the dependency names by substituting @var{s} for @samp{%}; if
7660 the target pattern does not contain a slash, append @var{d} to
7661 the front of each dependency name.@refill
7663 @item
7664 Test whether all the dependencies exist or ought to exist.  (If a
7665 file name is mentioned in the makefile as a target or as an explicit
7666 dependency, then we say it ought to exist.)
7668 If all dependencies exist or ought to exist, or there are no dependencies,
7669 then this rule applies.
7670 @end enumerate
7672 @item
7673 If no pattern rule has been found so far, try harder.
7674 For each pattern rule in the list:
7676 @enumerate a
7677 @item
7678 If the rule is terminal, ignore it and go on to the next rule.
7680 @item
7681 Compute the dependency names as before.
7683 @item
7684 Test whether all the dependencies exist or ought to exist.
7686 @item
7687 For each dependency that does not exist, follow this algorithm
7688 recursively to see if the dependency can be made by an implicit
7689 rule.
7691 @item
7692 If all dependencies exist, ought to exist, or can be
7693 made by implicit rules, then this rule applies.
7694 @end enumerate
7696 @item
7697 If no implicit rule applies, the rule for @code{.DEFAULT}, if any,
7698 applies.  In that case, give @var{t} the same commands that
7699 @code{.DEFAULT} has.  Otherwise, there are no commands for @var{t}.
7700 @end enumerate
7702 Once a rule that applies has been found, for each target pattern of the
7703 rule other than the one that matched @var{t} or @var{n}, the @samp{%} in
7704 the pattern is replaced with @var{s} and the resultant file name is stored
7705 until the commands to remake the target file @var{t} are executed.  After
7706 these commands are executed, each of these stored file names are entered
7707 into the data base and marked as having been updated and having the same
7708 update status as the file @var{t}.
7710 When the commands of a pattern rule are executed for @var{t}, the automatic
7711 variables are set corresponding to the target and dependencies.
7712 @xref{Automatic, ,Automatic Variables}.
7714 @node Archives, Features, Implicit Rules, Top
7715 @chapter Using @code{make} to Update Archive Files
7716 @cindex archive
7718 @dfn{Archive files} are files containing named subfiles called
7719 @dfn{members}; they are maintained with the program @code{ar} and their
7720 main use is as subroutine libraries for linking.
7722 @menu
7723 * Archive Members::             Archive members as targets.
7724 * Archive Update::              The implicit rule for archive member targets.
7725 * Archive Pitfalls::            Dangers to watch out for when using archives.
7726 * Archive Suffix Rules::        You can write a special kind of suffix rule
7727                                   for updating archives.
7728 @end menu
7730 @node Archive Members, Archive Update,  , Archives
7731 @section Archive Members as Targets
7732 @cindex archive member targets
7734 An individual member of an archive file can be used as a target or
7735 dependency in @code{make}.  You specify the member named @var{member} in
7736 archive file @var{archive} as follows:
7738 @example
7739 @var{archive}(@var{member})
7740 @end example
7742 @noindent
7743 This construct is available only in targets and dependencies, not in
7744 commands!  Most programs that you might use in commands do not support this
7745 syntax and cannot act directly on archive members.  Only @code{ar} and
7746 other programs specifically designed to operate on archives can do so.
7747 Therefore, valid commands to update an archive member target probably must
7748 use @code{ar}.  For example, this rule says to create a member
7749 @file{hack.o} in archive @file{foolib} by copying the file @file{hack.o}:
7751 @example
7752 foolib(hack.o) : hack.o
7753         ar cr foolib hack.o
7754 @end example
7756 In fact, nearly all archive member targets are updated in just this way
7757 and there is an implicit rule to do it for you.  @strong{Note:} The
7758 @samp{c} flag to @code{ar} is required if the archive file does not
7759 already exist.
7761 To specify several members in the same archive, you can write all the
7762 member names together between the parentheses.  For example:
7764 @example
7765 foolib(hack.o kludge.o)
7766 @end example
7768 @noindent
7769 is equivalent to:
7771 @example
7772 foolib(hack.o) foolib(kludge.o)
7773 @end example
7775 @cindex wildcard, in archive member
7776 You can also use shell-style wildcards in an archive member reference.
7777 @xref{Wildcards, ,Using Wildcard Characters in File Names}.  For
7778 example, @w{@samp{foolib(*.o)}} expands to all existing members of the
7779 @file{foolib} archive whose names end in @samp{.o}; perhaps
7780 @samp{@w{foolib(hack.o)} @w{foolib(kludge.o)}}.
7782 @node Archive Update
7783 @section Implicit Rule for Archive Member Targets
7785 Recall that a target that looks like @file{@var{a}(@var{m})} stands for the
7786 member named @var{m} in the archive file @var{a}.
7788 When @code{make} looks for an implicit rule for such a target, as a special
7789 feature it considers implicit rules that match @file{(@var{m})}, as well as
7790 those that match the actual target @file{@var{a}(@var{m})}.
7792 This causes one special rule whose target is @file{(%)} to match.  This
7793 rule updates the target @file{@var{a}(@var{m})} by copying the file @var{m}
7794 into the archive.  For example, it will update the archive member target
7795 @file{foo.a(bar.o)} by copying the @emph{file} @file{bar.o} into the
7796 archive @file{foo.a} as a @emph{member} named @file{bar.o}.
7798 When this rule is chained with others, the result is very powerful.
7799 Thus, @samp{make "foo.a(bar.o)"} (the quotes are needed to protect the
7800 @samp{(} and @samp{)} from being interpreted specially by the shell) in
7801 the presence of a file @file{bar.c} is enough to cause the following
7802 commands to be run, even without a makefile:
7804 @example
7805 cc -c bar.c -o bar.o
7806 ar r foo.a bar.o
7807 rm -f bar.o
7808 @end example
7810 @noindent
7811 Here @code{make} has envisioned the file @file{bar.o} as an intermediate
7812 file.  @xref{Chained Rules, ,Chains of Implicit Rules}.
7814 Implicit rules such as this one are written using the automatic variable
7815 @samp{$%}.  @xref{Automatic, ,Automatic Variables}.
7817 An archive member name in an archive cannot contain a directory name, but
7818 it may be useful in a makefile to pretend that it does.  If you write an
7819 archive member target @file{foo.a(dir/file.o)}, @code{make} will perform
7820 automatic updating with this command:
7822 @example
7823 ar r foo.a dir/file.o
7824 @end example
7826 @noindent
7827 which has the effect of copying the file @file{dir/file.o} into a member
7828 named @file{file.o}.  In connection with such usage, the automatic variables
7829 @code{%D} and @code{%F} may be useful.
7831 @menu
7832 * Archive Symbols::             How to update archive symbol directories.
7833 @end menu
7835 @node Archive Symbols,  ,  , Archive Update
7836 @subsection Updating Archive Symbol Directories
7837 @cindex @code{__.SYMDEF}
7838 @cindex updating archive symbol directories
7839 @cindex archive symbol directory updating
7840 @cindex symbol directories, updating archive
7841 @cindex directories, updating archive symbol
7843 An archive file that is used as a library usually contains a special member
7844 named @file{__.SYMDEF} that contains a directory of the external symbol
7845 names defined by all the other members.  After you update any other
7846 members, you need to update @file{__.SYMDEF} so that it will summarize the
7847 other members properly.  This is done by running the @code{ranlib} program:
7849 @example
7850 ranlib @var{archivefile}
7851 @end example
7853 Normally you would put this command in the rule for the archive file,
7854 and make all the members of the archive file dependencies of that rule.
7855 For example,
7857 @example
7858 libfoo.a: libfoo.a(x.o) libfoo.a(y.o) @dots{}
7859         ranlib libfoo.a
7860 @end example
7862 @noindent
7863 The effect of this is to update archive members @file{x.o}, @file{y.o},
7864 etc., and then update the symbol directory member @file{__.SYMDEF} by
7865 running @code{ranlib}.  The rules for updating the members are not shown
7866 here; most likely you can omit them and use the implicit rule which copies
7867 files into the archive, as described in the preceding section.
7869 This is not necessary when using the GNU @code{ar} program, which
7870 updates the @file{__.SYMDEF} member automatically.
7872 @node Archive Pitfalls
7873 @section Dangers When Using Archives
7874 @cindex archive, and parallel execution
7875 @cindex parallel execution, and archive update
7876 @cindex archive, and @code{-j}
7877 @cindex @code{-j}, and archive update
7879 It is important to be careful when using parallel execution (the
7880 @code{-j} switch; @pxref{Parallel, ,Parallel Execution}) and archives.
7881 If multiple @code{ar} commands run at the same time on the same archive
7882 file, they will not know about each other and can corrupt the file.
7884 Possibly a future version of @code{make} will provide a mechanism to
7885 circumvent this problem by serializing all commands that operate on the
7886 same archive file.  But for the time being, you must either write your
7887 makefiles to avoid this problem in some other way, or not use @code{-j}.
7889 @node Archive Suffix Rules, , Archive Pitfalls, Archives
7890 @section Suffix Rules for Archive Files
7891 @cindex suffix rule, for archive
7892 @cindex archive, suffix rule for
7893 @cindex library archive, suffix rule for
7894 @cindex @code{.a} (archives)
7896 You can write a special kind of suffix rule for dealing with archive
7897 files.  @xref{Suffix Rules}, for a full explanation of suffix rules.
7898 Archive suffix rules are obsolete in GNU @code{make}, because pattern
7899 rules for archives are a more general mechanism (@pxref{Archive
7900 Update}).  But they are retained for compatibility with other
7901 @code{make}s.
7903 To write a suffix rule for archives, you simply write a suffix rule
7904 using the target suffix @samp{.a} (the usual suffix for archive files).
7905 For example, here is the old-fashioned suffix rule to update a library
7906 archive from C source files:
7908 @example
7909 @group
7910 .c.a:
7911         $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o
7912         $(AR) r $@@ $*.o
7913         $(RM) $*.o
7914 @end group
7915 @end example
7917 @noindent
7918 This works just as if you had written the pattern rule:
7920 @example
7921 @group
7922 (%.o): %.c
7923         $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o
7924         $(AR) r $@@ $*.o
7925         $(RM) $*.o
7926 @end group
7927 @end example
7929 In fact, this is just what @code{make} does when it sees a suffix rule
7930 with @samp{.a} as the target suffix.  Any double-suffix rule
7931 @w{@samp{.@var{x}.a}} is converted to a pattern rule with the target
7932 pattern @samp{(%.o)} and a dependency pattern of @samp{%.@var{x}}.
7934 Since you might want to use @samp{.a} as the suffix for some other kind
7935 of file, @code{make} also converts archive suffix rules to pattern rules
7936 in the normal way (@pxref{Suffix Rules}).  Thus a double-suffix rule
7937 @w{@samp{.@var{x}.a}} produces two pattern rules: @samp{@w{(%.o):}
7938 @w{%.@var{x}}} and @samp{@w{%.a}: @w{%.@var{x}}}.@refill
7940 @node Features, Missing, Archives, Top
7941 @chapter Features of GNU @code{make}
7942 @cindex features of GNU @code{make}
7943 @cindex portability
7944 @cindex compatibility
7946 Here is a summary of the features of GNU @code{make}, for comparison
7947 with and credit to other versions of @code{make}.  We consider the
7948 features of @code{make} in 4.2 BSD systems as a baseline.  If you are
7949 concerned with writing portable makefiles, you should use only the
7950 features of @code{make} @emph{not} listed here or in @ref{Missing}.
7952 Many features come from the version of @code{make} in System V.
7954 @itemize @bullet
7955 @item
7956 The @code{VPATH} variable and its special meaning.
7957 @xref{Directory Search, , Searching Directories for Dependencies}.
7958 This feature exists in System V @code{make}, but is undocumented.
7959 It is documented in 4.3 BSD @code{make} (which says it mimics System V's
7960 @code{VPATH} feature).@refill
7962 @item
7963 Included makefiles.  @xref{Include, ,Including Other Makefiles}.
7964 Allowing multiple files to be included with a single directive is a GNU
7965 extension.
7967 @item
7968 Variables are read from and communicated via the environment.
7969 @xref{Environment, ,Variables from the Environment}.
7971 @item
7972 Options passed through the variable @code{MAKEFLAGS} to recursive
7973 invocations of @code{make}.
7974 @xref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
7976 @item
7977 The automatic variable @code{$%} is set to the member name
7978 in an archive reference.  @xref{Automatic, ,Automatic Variables}.
7980 @item
7981 The automatic variables @code{$@@}, @code{$*}, @code{$<}, @code{$%},
7982 and @code{$?} have corresponding forms like @code{$(@@F)} and
7983 @code{$(@@D)}.  We have generalized this to @code{$^} as an obvious
7984 extension.  @xref{Automatic, ,Automatic Variables}.@refill
7986 @item
7987 Substitution variable references.
7988 @xref{Reference, ,Basics of Variable References}.
7990 @item
7991 The command-line options @samp{-b} and @samp{-m}, accepted and
7992 ignored.  In System V @code{make}, these options actually do something.
7994 @item
7995 Execution of recursive commands to run @code{make} via the variable
7996 @code{MAKE} even if @samp{-n}, @samp{-q} or @samp{-t} is specified.
7997 @xref{Recursion, ,Recursive Use of @code{make}}.
7999 @item
8000 Support for suffix @samp{.a} in suffix rules.  @xref{Archive Suffix
8001 Rules}.  This feature is obsolete in GNU @code{make}, because the
8002 general feature of rule chaining (@pxref{Chained Rules, ,Chains of
8003 Implicit Rules}) allows one pattern rule for installing members in an
8004 archive (@pxref{Archive Update}) to be sufficient.
8006 @item
8007 The arrangement of lines and backslash-newline combinations in
8008 commands is retained when the commands are printed, so they appear as
8009 they do in the makefile, except for the stripping of initial
8010 whitespace.
8011 @end itemize
8013 The following features were inspired by various other versions of
8014 @code{make}.  In some cases it is unclear exactly which versions inspired
8015 which others.
8017 @itemize @bullet
8018 @item
8019 Pattern rules using @samp{%}.
8020 This has been implemented in several versions of @code{make}.
8021 We're not sure who invented it first, but it's been spread around a bit.
8022 @xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.@refill
8024 @item
8025 Rule chaining and implicit intermediate files.
8026 This was implemented by Stu Feldman in his version of @code{make}
8027 for AT&T Eighth Edition Research Unix, and later by Andrew Hume of
8028 AT&T Bell Labs in his @code{mk} program (where he terms it
8029 ``transitive closure'').  We do not really know if
8030 we got this from either of them or thought it up ourselves at the
8031 same time.  @xref{Chained Rules, ,Chains of Implicit Rules}.
8033 @item
8034 The automatic variable @code{$^} containing a list of all dependencies
8035 of the current target.  We did not invent this, but we have no idea who
8036 did.  @xref{Automatic, ,Automatic Variables}.  The automatic variable
8037 @code{$+} is a simple extension of @code{$^}.
8039 @item
8040 The ``what if'' flag (@samp{-W} in GNU @code{make}) was (as far as we know)
8041 invented by Andrew Hume in @code{mk}.
8042 @xref{Instead of Execution, ,Instead of Executing the Commands}.
8044 @item
8045 The concept of doing several things at once (parallelism) exists in
8046 many incarnations of @code{make} and similar programs, though not in the
8047 System V or BSD implementations.  @xref{Execution, ,Command Execution}.
8049 @item
8050 Modified variable references using pattern substitution come from
8051 SunOS 4.  @xref{Reference, ,Basics of Variable References}.
8052 This functionality was provided in GNU @code{make} by the
8053 @code{patsubst} function before the alternate syntax was implemented
8054 for compatibility with SunOS 4.  It is not altogether clear who
8055 inspired whom, since GNU @code{make} had @code{patsubst} before SunOS
8056 4 was released.@refill
8058 @item
8059 The special significance of @samp{+} characters preceding command lines
8060 (@pxref{Instead of Execution, ,Instead of Executing the Commands}) is
8061 mandated by
8062 @cite{IEEE Standard 1003.2-1992} (POSIX.2).
8064 @item
8065 The @samp{+=} syntax to append to the value of a variable comes from SunOS
8066 4 @code{make}.  @xref{Appending, , Appending More Text to Variables}.
8068 @item
8069 The syntax @w{@samp{@var{archive}(@var{mem1} @var{mem2}@dots{})}} to list
8070 multiple members in a single archive file comes from SunOS 4 @code{make}.
8071 @xref{Archive Members}.
8073 @item
8074 The @code{-include} directive to include makefiles with no error for a
8075 nonexistent file comes from SunOS 4 @code{make}.  (But note that SunOS 4
8076 @code{make} does not allow multiple makefiles to be specified in one
8077 @code{-include} directive.)
8078 @end itemize
8080 The remaining features are inventions new in GNU @code{make}:
8082 @itemize @bullet
8083 @item
8084 Use the @samp{-v} or @samp{--version} option to print version and
8085 copyright information.
8087 @item
8088 Use the @samp{-h} or @samp{--help} option to summarize the options to
8089 @code{make}.
8091 @item
8092 Simply-expanded variables.  @xref{Flavors, ,The Two Flavors of Variables}.
8094 @item
8095 Pass command-line variable assignments automatically through the
8096 variable @code{MAKE} to recursive @code{make} invocations.
8097 @xref{Recursion, ,Recursive Use of @code{make}}.
8099 @item
8100 Use the @samp{-C} or @samp{--directory} command option to change
8101 directory.  @xref{Options Summary, ,Summary of Options}.
8103 @item
8104 Make verbatim variable definitions with @code{define}.
8105 @xref{Defining, ,Defining Variables Verbatim}.
8107 @item
8108 Declare phony targets with the special target @code{.PHONY}.
8110 Andrew Hume of AT&T Bell Labs implemented a similar feature with a
8111 different syntax in his @code{mk} program.  This seems to be a case of
8112 parallel discovery.  @xref{Phony Targets, ,Phony Targets}.
8114 @item
8115 Manipulate text by calling functions.
8116 @xref{Functions, ,Functions for Transforming Text}.
8118 @item
8119 Use the @samp{-o} or @samp{--old-file}
8120 option to pretend a file's modification-time is old.
8121 @xref{Avoiding Compilation, ,Avoiding Recompilation of Some Files}.
8123 @item
8124 Conditional execution.
8126 This feature has been implemented numerous times in various versions
8127 of @code{make}; it seems a natural extension derived from the features
8128 of the C preprocessor and similar macro languages and is not a
8129 revolutionary concept.  @xref{Conditionals, ,Conditional Parts of Makefiles}.
8131 @item
8132 Specify a search path for included makefiles.
8133 @xref{Include, ,Including Other Makefiles}.
8135 @item
8136 Specify extra makefiles to read with an environment variable.
8137 @xref{MAKEFILES Variable, ,The Variable @code{MAKEFILES}}.
8139 @item
8140 Strip leading sequences of @samp{./} from file names, so that
8141 @file{./@var{file}} and @file{@var{file}} are considered to be the
8142 same file.@refill
8144 @item
8145 Use a special search method for library dependencies written in the
8146 form @samp{-l@var{name}}.
8147 @xref{Libraries/Search, ,Directory Search for Link Libraries}.
8149 @item
8150 Allow suffixes for suffix rules
8151 (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}) to contain any
8152 characters.  In other versions of @code{make}, they must begin with
8153 @samp{.} and not contain any @samp{/} characters.
8155 @item
8156 Keep track of the current level of @code{make} recursion using the
8157 variable @code{MAKELEVEL}.  @xref{Recursion, ,Recursive Use of @code{make}}.
8159 @item
8160 Specify static pattern rules.  @xref{Static Pattern, ,Static Pattern Rules}.
8162 @item
8163 Provide selective @code{vpath} search.
8164 @xref{Directory Search, ,Searching Directories for Dependencies}.
8166 @item
8167 Provide computed variable references.
8168 @xref{Reference, ,Basics of Variable References}.
8170 @item
8171 Update makefiles.  @xref{Remaking Makefiles, ,How Makefiles Are Remade}.
8172 System V @code{make} has a very, very limited form of this
8173 functionality in that it will check out SCCS files for makefiles.
8175 @item
8176 Various new built-in implicit rules.
8177 @xref{Catalogue of Rules, ,Catalogue of Implicit Rules}.
8179 @item
8180 The built-in variable @samp{MAKE_VERSION} gives the version number of
8181 @code{make}.
8182 @end itemize
8184 @node Missing, Makefile Conventions, Features, Top
8185 @chapter Incompatibilities and Missing Features
8186 @cindex incompatibilities
8187 @cindex missing features
8188 @cindex features, missing
8190 The @code{make} programs in various other systems support a few features
8191 that are not implemented in GNU @code{make}.  The POSIX.2 standard
8192 (@cite{IEEE Standard 1003.2-1992}) which specifies @code{make} does not
8193 require any of these features.@refill
8195 @itemize @bullet
8196 @item
8197 A target of the form @samp{@var{file}((@var{entry}))} stands for a member
8198 of archive file @var{file}.  The member is chosen, not by name, but by
8199 being an object file which defines the linker symbol @var{entry}.@refill
8201 This feature was not put into GNU @code{make} because of the
8202 nonmodularity of putting knowledge into @code{make} of the internal
8203 format of archive file symbol tables.
8204 @xref{Archive Symbols, ,Updating Archive Symbol Directories}.
8206 @item
8207 Suffixes (used in suffix rules) that end with the character @samp{~}
8208 have a special meaning to System V @code{make};
8209 they refer to the SCCS file that corresponds
8210 to the file one would get without the @samp{~}.  For example, the
8211 suffix rule @samp{.c~.o} would make the file @file{@var{n}.o} from
8212 the SCCS file @file{s.@var{n}.c}.  For complete coverage, a whole
8213 series of such suffix rules is required.
8214 @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
8216 In GNU @code{make}, this entire series of cases is handled by two
8217 pattern rules for extraction from SCCS, in combination with the
8218 general feature of rule chaining.
8219 @xref{Chained Rules, ,Chains of Implicit Rules}.
8221 @item
8222 In System V @code{make}, the string @samp{$$@@} has the strange meaning
8223 that, in the dependencies of a rule with multiple targets, it stands
8224 for the particular target that is being processed.
8226 This is not defined in GNU @code{make} because @samp{$$} should always
8227 stand for an ordinary @samp{$}.
8229 It is possible to get this functionality through the use of static pattern
8230 rules (@pxref{Static Pattern, ,Static Pattern Rules}).
8231 The System V @code{make} rule:
8233 @example
8234 $(targets): $$@@.o lib.a
8235 @end example
8237 @noindent
8238 can be replaced with the GNU @code{make} static pattern rule:
8240 @example
8241 $(targets): %: %.o lib.a
8242 @end example
8244 @item
8245 In System V and 4.3 BSD @code{make}, files found by @code{VPATH} search
8246 (@pxref{Directory Search, ,Searching Directories for Dependencies}) have their names changed inside command
8247 strings.  We feel it is much cleaner to always use automatic variables
8248 and thus make this feature obsolete.@refill
8250 @item
8251 In some Unix @code{make}s, the automatic variable @code{$*} appearing in
8252 the dependencies of a rule has the amazingly strange ``feature'' of
8253 expanding to the full name of the @emph{target of that rule}.  We cannot
8254 imagine what went on in the minds of Unix @code{make} developers to do
8255 this; it is utterly inconsistent with the normal definition of @code{$*}.
8256 @vindex * @r{(automatic variable), unsupported bizarre usage}
8258 @item
8259 In some Unix @code{make}s, implicit rule search
8260 (@pxref{Implicit Rules, ,Using Implicit Rules}) is apparently done for
8261 @emph{all} targets, not just those without commands.  This means you can
8262 do:@refill
8264 @example
8265 @group
8266 foo.o:
8267         cc -c foo.c
8268 @end group
8269 @end example
8271 @noindent
8272 and Unix @code{make} will intuit that @file{foo.o} depends on
8273 @file{foo.c}.@refill
8275 We feel that such usage is broken.  The dependency properties of
8276 @code{make} are well-defined (for GNU @code{make}, at least),
8277 and doing such a thing simply does not fit the model.@refill
8279 @item
8280 GNU @code{make} does not include any built-in implicit rules for
8281 compiling or preprocessing EFL programs.  If we hear of anyone who is
8282 using EFL, we will gladly add them.
8284 @item
8285 It appears that in SVR4 @code{make}, a suffix rule can be specified with
8286 no commands, and it is treated as if it had empty commands
8287 (@pxref{Empty Commands}).  For example:
8289 @example
8290 .c.a:
8291 @end example
8293 @noindent
8294 will override the built-in @file{.c.a} suffix rule.
8296 We feel that it is cleaner for a rule without commands to always simply
8297 add to the dependency list for the target.  The above example can be
8298 easily rewritten to get the desired behavior in GNU @code{make}:
8300 @example
8301 .c.a: ;
8302 @end example
8304 @item
8305 Some versions of @code{make} invoke the shell with the @samp{-e} flag,
8306 except under @samp{-k} (@pxref{Testing, ,Testing the Compilation of a
8307 Program}).  The @samp{-e} flag tells the shell to exit as soon as any
8308 program it runs returns a nonzero status.  We feel it is cleaner to
8309 write each shell command line to stand on its own and not require this
8310 special treatment.
8311 @end itemize
8313 @comment The makefile standards are in a separate file that is also
8314 @comment included by standards.texi.
8315 @include make-stds.texi
8317 @node Quick Reference, Complex Makefile, Makefile Conventions, Top
8318 @appendix Quick Reference
8320 This appendix summarizes the directives, text manipulation functions,
8321 and special variables which GNU @code{make} understands.
8322 @xref{Special Targets}, @ref{Catalogue of Rules, ,Catalogue of Implicit Rules},
8323 and @ref{Options Summary, ,Summary of Options},
8324 for other summaries.
8326 Here is a summary of the directives GNU @code{make} recognizes:
8328 @table @code
8329 @item define @var{variable}
8330 @itemx endef
8332 Define a multi-line, recursively-expanded variable.@*
8333 @xref{Sequences}.
8335 @item ifdef @var{variable}
8336 @itemx ifndef @var{variable}
8337 @itemx ifeq (@var{a},@var{b})
8338 @itemx ifeq "@var{a}" "@var{b}"
8339 @itemx ifeq '@var{a}' '@var{b}'
8340 @itemx ifneq (@var{a},@var{b})
8341 @itemx ifneq "@var{a}" "@var{b}"
8342 @itemx ifneq '@var{a}' '@var{b}'
8343 @itemx else
8344 @itemx endif
8346 Conditionally evaluate part of the makefile.@*
8347 @xref{Conditionals}.
8349 @item include @var{file}
8351 Include another makefile.@*
8352 @xref{Include, ,Including Other Makefiles}.
8354 @item override @var{variable} = @var{value}
8355 @itemx override @var{variable} := @var{value}
8356 @itemx override @var{variable} += @var{value}
8357 @itemx override define @var{variable}
8358 @itemx endef
8360 Define a variable, overriding any previous definition, even one from
8361 the command line.@*
8362 @xref{Override Directive, ,The @code{override} Directive}.
8364 @item export
8366 Tell @code{make} to export all variables to child processes by default.@*
8367 @xref{Variables/Recursion, , Communicating Variables to a Sub-@code{make}}.
8369 @item export @var{variable}
8370 @itemx export @var{variable} = @var{value}
8371 @itemx export @var{variable} := @var{value}
8372 @itemx export @var{variable} += @var{value}
8373 @itemx unexport @var{variable}
8374 Tell @code{make} whether or not to export a particular variable to child
8375 processes.@*
8376 @xref{Variables/Recursion, , Communicating Variables to a Sub-@code{make}}.
8378 @item vpath @var{pattern} @var{path}
8379 Specify a search path for files matching a @samp{%} pattern.@*
8380 @xref{Selective Search, , The @code{vpath} Directive}.
8382 @item vpath @var{pattern}
8383 Remove all search paths previously specified for @var{pattern}.
8385 @item vpath
8386 Remove all search paths previously specified in any @code{vpath}
8387 directive.
8388 @end table
8390 Here is a summary of the text manipulation functions (@pxref{Functions}):
8392 @table @code
8393 @item $(subst @var{from},@var{to},@var{text})
8394 Replace @var{from} with @var{to} in @var{text}.@*
8395 @xref{Text Functions, , Functions for String Substitution and Analysis}.
8397 @item $(patsubst @var{pattern},@var{replacement},@var{text})
8398 Replace words matching @var{pattern} with @var{replacement} in @var{text}.@*
8399 @xref{Text Functions, , Functions for String Substitution and Analysis}.
8401 @item $(strip @var{string})
8402 Remove excess whitespace characters from @var{string}.@*
8403 @xref{Text Functions, , Functions for String Substitution and Analysis}.
8405 @item $(findstring @var{find},@var{text})
8406 Locate @var{find} in @var{text}.@*
8407 @xref{Text Functions, , Functions for String Substitution and Analysis}.
8409 @item $(filter @var{pattern}@dots{},@var{text})
8410 Select words in @var{text} that match one of the @var{pattern} words.@*
8411 @xref{Text Functions, , Functions for String Substitution and Analysis}.
8413 @item $(filter-out @var{pattern}@dots{},@var{text})
8414 Select words in @var{text} that @emph{do not} match any of the @var{pattern} words.@*
8415 @xref{Text Functions, , Functions for String Substitution and Analysis}.
8417 @item $(sort @var{list})
8418 Sort the words in @var{list} lexicographically, removing duplicates.@*
8419 @xref{Text Functions, , Functions for String Substitution and Analysis}.
8421 @item $(dir @var{names}@dots{})
8422 Extract the directory part of each file name.@*
8423 @xref{Filename Functions, ,Functions for File Names}.
8425 @item $(notdir @var{names}@dots{})
8426 Extract the non-directory part of each file name.@*
8427 @xref{Filename Functions, ,Functions for File Names}.
8429 @item $(suffix @var{names}@dots{})
8430 Extract the suffix (the last @samp{.} and following characters) of each file name.@*
8431 @xref{Filename Functions, ,Functions for File Names}.
8433 @item $(basename @var{names}@dots{})
8434 Extract the base name (name without suffix) of each file name.@*
8435 @xref{Filename Functions, ,Functions for File Names}.
8437 @item $(addsuffix @var{suffix},@var{names}@dots{})
8438 Append @var{suffix} to each word in @var{names}.@*
8439 @xref{Filename Functions, ,Functions for File Names}.
8441 @item $(addprefix @var{prefix},@var{names}@dots{})
8442 Prepend @var{prefix} to each word in @var{names}.@*
8443 @xref{Filename Functions, ,Functions for File Names}.
8445 @item $(join @var{list1},@var{list2})
8446 Join two parallel lists of words.@*
8447 @xref{Filename Functions, ,Functions for File Names}.
8449 @item $(word @var{n},@var{text})
8450 Extract the @var{n}th word (one-origin) of @var{text}.@*
8451 @xref{Filename Functions, ,Functions for File Names}.
8453 @item $(words @var{text})
8454 Count the number of words in @var{text}.@*
8455 @xref{Filename Functions, ,Functions for File Names}.
8457 @item $(firstword @var{names}@dots{})
8458 Extract the first word of @var{names}.@*
8459 @xref{Filename Functions, ,Functions for File Names}.
8461 @item $(wildcard @var{pattern}@dots{})
8462 Find file names matching a shell file name pattern (@emph{not} a
8463 @samp{%} pattern).@*
8464 @xref{Wildcard Function, ,The Function @code{wildcard}}.
8466 @item $(shell @var{command})
8468 Execute a shell command and return its output.@*
8469 @xref{Shell Function, , The @code{shell} Function}.
8471 @item $(origin @var{variable})
8473 Return a string describing how the @code{make} variable @var{variable} was
8474 defined.@*
8475 @xref{Origin Function, , The @code{origin} Function}.
8477 @item $(foreach @var{var},@var{words},@var{text})
8479 Evaluate @var{text} with @var{var} bound to each word in @var{words},
8480 and concatenate the results.@*
8481 @xref{Foreach Function, ,The @code{foreach} Function}.
8482 @end table
8484 Here is a summary of the automatic variables.
8485 @xref{Automatic, ,Automatic Variables},
8486 for full information.
8488 @table @code
8489 @item $@@
8490 The file name of the target.
8492 @item $%
8493 The target member name, when the target is an archive member.
8495 @item $<
8496 The name of the first dependency.
8498 @item $?
8499 The names of all the dependencies that are
8500 newer than the target, with spaces between them.
8501 For dependencies which are archive members, only
8502 the member named is used (@pxref{Archives}).
8504 @item $^
8505 @itemx $+
8506 The names of all the dependencies, with spaces between them.  For
8507 dependencies which are archive members, only the member named is used
8508 (@pxref{Archives}).  The value of @code{$^} omits duplicate
8509 dependencies, while @code{$+} retains them and preserves their order.
8511 @item $*
8512 The stem with which an implicit rule matches
8513 (@pxref{Pattern Match, ,How Patterns Match}).
8515 @item $(@@D)
8516 @itemx $(@@F)
8517 The directory part and the file-within-directory part of @code{$@@}.
8519 @item $(*D)
8520 @itemx $(*F)
8521 The directory part and the file-within-directory part of @code{$*}.
8523 @item $(%D)
8524 @itemx $(%F)
8525 The directory part and the file-within-directory part of @code{$%}.
8527 @item $(<D)
8528 @itemx $(<F)
8529 The directory part and the file-within-directory part of @code{$<}.
8531 @item $(^D)
8532 @itemx $(^F)
8533 The directory part and the file-within-directory part of @code{$^}.
8535 @item $(+D)
8536 @itemx $(+F)
8537 The directory part and the file-within-directory part of @code{$+}.
8539 @item $(?D)
8540 @itemx $(?F)
8541 The directory part and the file-within-directory part of @code{$?}.
8542 @end table
8544 These variables are used specially by GNU @code{make}:
8546 @table @code
8547 @item MAKEFILES
8549 Makefiles to be read on every invocation of @code{make}.@*
8550 @xref{MAKEFILES Variable, ,The Variable @code{MAKEFILES}}.
8552 @item VPATH
8554 Directory search path for files not found in the current directory.@*
8555 @xref{General Search, , @code{VPATH} Search Path for All Dependencies}.
8557 @item SHELL
8559 The name of the system default command interpreter, usually @file{/bin/sh}.
8560 You can set @code{SHELL} in the makefile to change the shell used to run
8561 commands.  @xref{Execution, ,Command Execution}.
8563 @item MAKE
8565 The name with which @code{make} was invoked.
8566 Using this variable in commands has special meaning.
8567 @xref{MAKE Variable, ,How the @code{MAKE} Variable Works}.
8569 @item MAKELEVEL
8571 The number of levels of recursion (sub-@code{make}s).@*
8572 @xref{Variables/Recursion}.
8574 @item MAKEFLAGS
8576 The flags given to @code{make}.  You can set this in the environment or
8577 a makefile to set flags.@*
8578 @xref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
8580 @item SUFFIXES
8582 The default list of suffixes before @code{make} reads any makefiles.
8583 @end table
8585 @node Complex Makefile, Concept Index, Quick Reference, Top
8586 @appendix Complex Makefile Example
8588 Here is the makefile for the GNU @code{tar} program.  This is a
8589 moderately complex makefile.
8591 Because it is the first target, the default goal is @samp{all}.  An
8592 interesting feature of this makefile is that @file{testpad.h} is a
8593 source file automatically created by the @code{testpad} program,
8594 itself compiled from @file{testpad.c}.
8596 If you type @samp{make} or @samp{make all}, then @code{make} creates
8597 the @file{tar} executable, the @file{rmt} daemon that provides
8598 remote tape access, and the @file{tar.info} Info file.
8600 If you type @samp{make install}, then @code{make} not only creates
8601 @file{tar}, @file{rmt}, and @file{tar.info}, but also installs
8602 them.
8604 If you type @samp{make clean}, then @code{make} removes the @samp{.o}
8605 files, and the @file{tar}, @file{rmt}, @file{testpad},
8606 @file{testpad.h}, and @file{core} files.
8608 If you type @samp{make distclean}, then @code{make} not only removes
8609 the same files as does @samp{make clean} but also the
8610 @file{TAGS}, @file{Makefile}, and @file{config.status} files.
8611 (Although it is not evident, this makefile (and
8612 @file{config.status}) is generated by the user with the
8613 @code{configure} program, which is provided in the @code{tar}
8614 distribution, but is not shown here.)
8616 If you type @samp{make realclean}, then @code{make} removes the same
8617 files as does @samp{make distclean} and also removes the Info files
8618 generated from @file{tar.texinfo}.
8620 In addition, there are targets @code{shar} and @code{dist} that create
8621 distribution kits.
8623 @example
8624 @group
8625 # Generated automatically from Makefile.in by configure.
8626 # Un*x Makefile for GNU tar program.
8627 # Copyright (C) 1991 Free Software Foundation, Inc.
8628 @end group
8630 @group
8631 # This program is free software; you can redistribute
8632 # it and/or modify it under the terms of the GNU
8633 # General Public License @dots{}
8634 @dots{}
8635 @dots{}
8636 @end group
8638 SHELL = /bin/sh
8640 #### Start of system configuration section. ####
8642 srcdir = .
8644 @group
8645 # If you use gcc, you should either run the
8646 # fixincludes script that comes with it or else use
8647 # gcc with the -traditional option.  Otherwise ioctl
8648 # calls will be compiled incorrectly on some systems.
8649 CC = gcc -O
8650 YACC = bison -y
8651 INSTALL = /usr/local/bin/install -c
8652 INSTALLDATA = /usr/local/bin/install -c -m 644
8653 @end group
8655 # Things you might add to DEFS:
8656 # -DSTDC_HEADERS        If you have ANSI C headers and
8657 #                       libraries.
8658 # -DPOSIX               If you have POSIX.1 headers and
8659 #                       libraries.
8660 # -DBSD42               If you have sys/dir.h (unless
8661 #                       you use -DPOSIX), sys/file.h,
8662 #                       and st_blocks in `struct stat'.
8663 # -DUSG                 If you have System V/ANSI C
8664 #                       string and memory functions
8665 #                       and headers, sys/sysmacros.h,
8666 #                       fcntl.h, getcwd, no valloc,
8667 #                       and ndir.h (unless
8668 #                       you use -DDIRENT).
8669 # -DNO_MEMORY_H         If USG or STDC_HEADERS but do not
8670 #                       include memory.h.
8671 # -DDIRENT              If USG and you have dirent.h
8672 #                       instead of ndir.h.
8673 # -DSIGTYPE=int         If your signal handlers
8674 #                       return int, not void.
8675 # -DNO_MTIO             If you lack sys/mtio.h
8676 #                       (magtape ioctls).
8677 # -DNO_REMOTE           If you do not have a remote shell
8678 #                       or rexec.
8679 # -DUSE_REXEC           To use rexec for remote tape
8680 #                       operations instead of
8681 #                       forking rsh or remsh.
8682 # -DVPRINTF_MISSING     If you lack vprintf function
8683 #                       (but have _doprnt).
8684 # -DDOPRNT_MISSING      If you lack _doprnt function.
8685 #                       Also need to define
8686 #                       -DVPRINTF_MISSING.
8687 # -DFTIME_MISSING       If you lack ftime system call.
8688 # -DSTRSTR_MISSING      If you lack strstr function.
8689 # -DVALLOC_MISSING      If you lack valloc function.
8690 # -DMKDIR_MISSING       If you lack mkdir and
8691 #                       rmdir system calls.
8692 # -DRENAME_MISSING      If you lack rename system call.
8693 # -DFTRUNCATE_MISSING   If you lack ftruncate
8694 #                       system call.
8695 # -DV7                  On Version 7 Unix (not
8696 #                       tested in a long time).
8697 # -DEMUL_OPEN3          If you lack a 3-argument version
8698 #                       of open, and want to emulate it
8699 #                       with system calls you do have.
8700 # -DNO_OPEN3            If you lack the 3-argument open
8701 #                       and want to disable the tar -k
8702 #                       option instead of emulating open.
8703 # -DXENIX               If you have sys/inode.h
8704 #                       and need it 94 to be included.
8706 DEFS =  -DSIGTYPE=int -DDIRENT -DSTRSTR_MISSING \
8707         -DVPRINTF_MISSING -DBSD42
8708 # Set this to rtapelib.o unless you defined NO_REMOTE,
8709 # in which case make it empty.
8710 RTAPELIB = rtapelib.o
8711 LIBS =
8712 DEF_AR_FILE = /dev/rmt8
8713 DEFBLOCKING = 20
8715 @group
8716 CDEBUG = -g
8717 CFLAGS = $(CDEBUG) -I. -I$(srcdir) $(DEFS) \
8718         -DDEF_AR_FILE=\"$(DEF_AR_FILE)\" \
8719         -DDEFBLOCKING=$(DEFBLOCKING)
8720 LDFLAGS = -g
8721 @end group
8723 @group
8724 prefix = /usr/local
8725 # Prefix for each installed program,
8726 # normally empty or `g'.
8727 binprefix =
8729 # The directory to install tar in.
8730 bindir = $(prefix)/bin
8732 # The directory to install the info files in.
8733 infodir = $(prefix)/info
8734 @end group
8736 #### End of system configuration section. ####
8738 SRC1 =  tar.c create.c extract.c buffer.c \
8739         getoldopt.c update.c gnu.c mangle.c
8740 SRC2 =  version.c list.c names.c diffarch.c \
8741         port.c wildmat.c getopt.c
8742 SRC3 =  getopt1.c regex.c getdate.y
8743 SRCS =  $(SRC1) $(SRC2) $(SRC3)
8744 OBJ1 =  tar.o create.o extract.o buffer.o \
8745         getoldopt.o update.o gnu.o mangle.o
8746 OBJ2 =  version.o list.o names.o diffarch.o \
8747         port.o wildmat.o getopt.o
8748 OBJ3 =  getopt1.o regex.o getdate.o $(RTAPELIB)
8749 OBJS =  $(OBJ1) $(OBJ2) $(OBJ3)
8750 @group
8751 AUX =   README COPYING ChangeLog Makefile.in  \
8752         makefile.pc configure configure.in \
8753         tar.texinfo tar.info* texinfo.tex \
8754         tar.h port.h open3.h getopt.h regex.h \
8755         rmt.h rmt.c rtapelib.c alloca.c \
8756         msd_dir.h msd_dir.c tcexparg.c \
8757         level-0 level-1 backup-specs testpad.c
8758 @end group
8760 all:    tar rmt tar.info
8762 @group
8763 tar:    $(OBJS)
8764         $(CC) $(LDFLAGS) -o $@@ $(OBJS) $(LIBS)
8765 @end group
8767 @group
8768 rmt:    rmt.c
8769         $(CC) $(CFLAGS) $(LDFLAGS) -o $@@ rmt.c
8770 @end group
8772 @group
8773 tar.info: tar.texinfo
8774         makeinfo tar.texinfo
8775 @end group
8777 @group
8778 install: all
8779         $(INSTALL) tar $(bindir)/$(binprefix)tar
8780         -test ! -f rmt || $(INSTALL) rmt /etc/rmt
8781         $(INSTALLDATA) $(srcdir)/tar.info* $(infodir)
8782 @end group
8784 @group
8785 $(OBJS): tar.h port.h testpad.h
8786 regex.o buffer.o tar.o: regex.h
8787 # getdate.y has 8 shift/reduce conflicts.
8788 @end group
8790 @group
8791 testpad.h: testpad
8792         ./testpad
8793 @end group
8795 @group
8796 testpad: testpad.o
8797         $(CC) -o $@@ testpad.o
8798 @end group
8800 @group
8801 TAGS:   $(SRCS)
8802         etags $(SRCS)
8803 @end group
8805 @group
8806 clean:
8807         rm -f *.o tar rmt testpad testpad.h core
8808 @end group
8810 @group
8811 distclean: clean
8812         rm -f TAGS Makefile config.status
8813 @end group
8815 @group
8816 realclean: distclean
8817         rm -f tar.info*
8818 @end group
8820 @group
8821 shar: $(SRCS) $(AUX)
8822         shar $(SRCS) $(AUX) | compress \
8823           > tar-`sed -e '/version_string/!d' \
8824                      -e 's/[^0-9.]*\([0-9.]*\).*/\1/' \
8825                      -e q
8826                      version.c`.shar.Z
8827 @end group
8829 @group
8830 dist: $(SRCS) $(AUX)
8831         echo tar-`sed \
8832              -e '/version_string/!d' \
8833              -e 's/[^0-9.]*\([0-9.]*\).*/\1/' \
8834              -e q
8835              version.c` > .fname
8836         -rm -rf `cat .fname`
8837         mkdir `cat .fname`
8838         ln $(SRCS) $(AUX) `cat .fname`
8839         -rm -rf `cat .fname` .fname
8840         tar chZf `cat .fname`.tar.Z `cat .fname`
8841 @end group
8843 @group
8844 tar.zoo: $(SRCS) $(AUX)
8845         -rm -rf tmp.dir
8846         -mkdir tmp.dir
8847         -rm tar.zoo
8848         for X in $(SRCS) $(AUX) ; do \
8849             echo $$X ; \
8850             sed 's/$$/^M/' $$X \
8851             > tmp.dir/$$X ; done
8852         cd tmp.dir ; zoo aM ../tar.zoo *
8853         -rm -rf tmp.dir
8854 @end group
8855 @end example
8857 @node Concept Index, Name Index, Complex Makefile, Top
8858 @unnumbered Index of Concepts
8860 @printindex cp
8862 @node Name Index,  , Concept Index, Top
8863 @unnumbered Index of Functions, Variables, & Directives
8865 @printindex fn
8867 @summarycontents
8868 @contents
8869 @bye