Abbreviate copyright years.
[make.git] / make.texinfo
blob8e29a3ab9f841a02ce9fd0dc6727b927d3f0b217
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.149 1994/11/04 20:23:23 roland Alpha $
11 @set EDITION 0.47
12 @set VERSION 3.72 Beta
13 @set UPDATED 1 November 1994
14 @set UPDATE-MONTH November 1994
15 @set ISBN 1-882114-50-7
17 @c finalout
19 @c ISPELL CHECK: done, 10 June 1993 --roland
21 @c Combine the variable and function indices:
22 @syncodeindex vr fn
23 @c Combine the program and concept indices:
24 @syncodeindex pg cp
26 @ifinfo
27 This file documents the GNU Make utility, which determines
28 automatically which pieces of a large program need to be recompiled,
29 and issues the commands to recompile them.
31 This is Edition @value{EDITION}, last updated @value{UPDATED},
32 of @cite{The GNU Make Manual}, for @code{make}, Version @value{VERSION}.
34 Copyright (C) 1988, '89, '90, '91, '92, '93, '94 Free Software Foundation, Inc.
36 Permission is granted to make and distribute verbatim copies of
37 this manual provided the copyright notice and this permission notice
38 are preserved on all copies.
40 @ignore
41 Permission is granted to process this file through TeX and print the
42 results, provided the printed document carries copying permission
43 notice identical to this one except for the removal of this paragraph
44 (this paragraph not being relevant to the printed manual).
46 @end ignore
47 Permission is granted to copy and distribute modified versions of this
48 manual under the conditions for verbatim copying, provided that the entire
49 resulting derived work is distributed under the terms of a permission
50 notice identical to this one.
52 Permission is granted to copy and distribute translations of this manual
53 into another language, under the above conditions for modified versions,
54 except that this permission notice may be stated in a translation approved
55 by the Free Software Foundation.
56 @end ifinfo
58 @iftex
59 @shorttitlepage GNU Make
60 @end iftex
61 @titlepage
62 @title GNU Make
63 @subtitle A Program for Directing Recompilation
64 @subtitle Edition @value{EDITION}, for @code{make} Version @value{VERSION}.
65 @subtitle @value{UPDATE-MONTH}
66 @author Richard M. Stallman and Roland McGrath
67 @page
68 @vskip 0pt plus 1filll
69 Copyright @copyright{} 1988, '89, '90, '91, '92, '93, '94 Free Software Foundation, Inc.
70 @sp 2
71 Published by the Free Software Foundation @*
72 675 Massachusetts Avenue, @*
73 Cambridge, MA 02139 USA @*
74 Printed copies are available for $20 each. @*
75 ISBN @value{ISBN} @*
77 Permission is granted to make and distribute verbatim copies of
78 this manual provided the copyright notice and this permission notice
79 are preserved on all copies.
81 Permission is granted to copy and distribute modified versions of this
82 manual under the conditions for verbatim copying, provided that the entire
83 resulting derived work is distributed under the terms of a permission
84 notice identical to this one.
86 Permission is granted to copy and distribute translations of this manual
87 into another language, under the above conditions for modified versions,
88 except that this permission notice may be stated in a translation approved
89 by the Free Software Foundation.
90 @sp 2
91 Cover art by Etienne Suvasa.
92 @end titlepage
93 @page
95 @ifinfo
96 @node Top, Overview, (dir), (dir)
97 @top Make
99 The GNU @code{make} utility automatically determines which pieces of a
100 large program need to be recompiled, and issues the commands to
101 recompile them.@refill
103 This is Edition @value{EDITION} of the @cite{GNU Make Manual},
104 last updated @value{UPDATED}
105 for @code{make} Version @value{VERSION}.@refill
107 This manual describes @code{make} and contains the following chapters:@refill
108 @end ifinfo
110 @menu
111 * Overview::                    Overview of @code{make}.
112 * Introduction::                An introduction to @code{make}.
113 * Makefiles::                   Makefiles tell @code{make} what to do.
114 * Rules::                       Rules describe when a file must be remade.
115 * Commands::                    Commands say how to remake a file.
116 * Using Variables::             You can use variables to avoid repetition.
117 * Conditionals::                Use or ignore parts of the makefile based
118                                  on the values of variables.
119 * Functions::                   Many powerful ways to manipulate text.
120 * make Invocation: Running.     How to invoke @code{make} on the command line.
121 * Implicit Rules::              Use implicit rules to treat many files alike,
122                                  based on their file names.
123 * Archives::                    How @code{make} can update library archives.
124 * Features::                    Features GNU @code{make} has over other @code{make}s.
125 * Missing::                     What GNU @code{make} lacks from other @code{make}s.
126 * Makefile Conventions::        Conventions for makefiles in GNU programs.
127 * Quick Reference::             A quick reference for experienced users.
128 * Complex Makefile::            A real example of a straightforward,
129                                  but nontrivial, makefile.
130 * Concept Index::               Index of Concepts
131 * Name Index::                  Index of Functions, Variables, & Directives
133  --- The Detailed Node Listing ---
135 Overview of @code{make}
137 * Preparing::                   Preparing and Running Make
138 * Reading::                     On Reading this Text
139 * Bugs::                        Problems and Bugs
141 An Introduction to Makefiles
143 * Rule Introduction::           What a rule looks like.
144 * Simple Makefile::             A Simple Makefile
145 * How Make Works::              How @code{make} Processes This Makefile
146 * Variables Simplify::          Variables Make Makefiles Simpler
147 * make Deduces::                Letting @code{make} Deduce the Commands
148 * Combine By Dependency::       Another Style of Makefile
149 * Cleanup::                     Rules for Cleaning the Directory
151 Writing Makefiles
153 * Makefile Contents::           What makefiles contain.
154 * Makefile Names::              How to name your makefile.
155 * Include::                     How one makefile can use another makefile.
156 * MAKEFILES Variable::          The environment can specify extra makefiles.
157 * Remaking Makefiles::          How makefiles get remade.
158 * Overriding Makefiles::        How to override part of one makefile
159                                  with another makefile.
161 Writing Rules
163 * Rule Example::                An example explained.
164 * Rule Syntax::                 General syntax explained.
165 * Wildcards::                   Using wildcard characters such as `*'.
166 * Directory Search::            Searching other directories for source files.
167 * Phony Targets::               Using a target that is not a real file's name.
168 * Force Targets::               You can use a target without commands
169                                   or dependencies to mark other 
170                                   targets as phony.
171 * Empty Targets::               When only the date matters and the
172                                   files are empty.
173 * Special Targets::             Targets with special built-in meanings.
174 * Multiple Targets::            When to make use of several targets in a rule.
175 * Multiple Rules::              How to use several rules with the same target.
176 * Static Pattern::              Static pattern rules apply to multiple targets
177                                   and can vary the dependencies according to 
178                                   the target name.
179 * Double-Colon::                How to use a special kind of rule to allow
180                                   several independent rules for one target.
181 * Automatic Dependencies::      How to automatically generate rules giving
182                                  dependencies from the source files themselves.
184 Using Wildcard Characters in File Names
186 * Wildcard Examples::           Several examples
187 * Wildcard Pitfall::            Problems to avoid.
188 * Wildcard Function::           How to cause wildcard expansion where
189                                   it does not normally take place.
191 Searching Directories for Dependencies
193 * General Search::              Specifying a search path that applies 
194                                   to every dependency.
195 * Selective Search::            Specifying a search path 
196                                   for a specified class of names.
197 * Commands/Search::             How to write shell commands that work together
198                                   with search paths.
199 * Implicit/Search::             How search paths affect implicit rules.
200 * Libraries/Search::            Directory search for link libraries.
202 Static Pattern Rules
204 * Static Usage::                The syntax of static pattern rules.
205 * Static versus Implicit::      When are they better than implicit rules?
207 Writing the Commands in Rules
209 * Echoing::                     How to control when commands are echoed.
210 * Execution::                   How commands are executed.
211 * Parallel::                    How commands can be executed in parallel.
212 * Errors::                      What happens after a command execution error. 
213 * Interrupts::                  What happens when a command is interrupted.
214 * Recursion::                   Invoking @code{make} from makefiles.
215 * Sequences::                   Defining canned sequences of commands.
216 * Empty Commands::              Defining useful, do-nothing commands.
218 Recursive Use of @code{make}
220 * MAKE Variable::               The special effects of using @samp{$(MAKE)}.
221 * Variables/Recursion::         How to communicate variables to a sub-@code{make}.
222 * Options/Recursion::           How to communicate options to a sub-@code{make}.
223 * -w Option::                   How the @samp{-w} or @samp{--print-directory} option
224                                  helps debug use of recursive @code{make} commands.
226 How to Use Variables
228 * Reference::                   How to use the value of a variable.
229 * Flavors::                     Variables come in two flavors.
230 * Advanced::                    Advanced features for referencing a variable.
231 * Values::                      All the ways variables get their values.
232 * Setting::                     How to set a variable in the makefile.
233 * Appending::                   How to append more text to the old value
234                                   of a variable.
235 * Override Directive::          How to set a variable in the makefile even if
236                                   the user has set it with a command argument.
237 * Defining::                    An alternate way to set a variable 
238                                   to a verbatim string.
239 * Environment::                 Variable values can come from the environment.
241 Advanced Features for Reference to Variables
243 * Substitution Refs::           Referencing a variable with 
244                                   substitutions on the value.
245 * Computed Names::              Computing the name of the variable to refer to.
247 Conditional Parts of Makefiles
249 * Conditional Example::         Example of a conditional
250 * Conditional Syntax::          The syntax of conditionals.
251 * Testing Flags::               Conditionals that test flags.
253 Functions for Transforming Text
255 * Syntax of Functions::         How to write a function call.
256 * Text Functions::              General-purpose text manipulation functions.
257 * Filename Functions::          Functions for manipulating file names.
258 * Foreach Function::            Repeat some text with controlled variation.
259 * Origin Function::             Find where a variable got its value.
260 * Shell Function::              Substitute the output of a shell command.
262 How to Run @code{make}
264 * Makefile Arguments::          How to specify which makefile to use.
265 * Goals::                       How to use goal arguments to specify which 
266                                   parts of the makefile to use.
267 * Instead of Execution::        How to use mode flags to specify what
268                                   kind of thing to do with the commands 
269                                   in the makefile other than simply 
270                                   execute them.
271 * Avoiding Compilation::        How to avoid recompiling certain files.
272 * Overriding::                  How to override a variable to specify 
273                                   an alternate compiler and other things.
274 * Testing::                     How to proceed past some errors, to 
275                                   test compilation.
276 * Options Summary::             Summary of Options
278 Using Implicit Rules
280 * Using Implicit::              How to use an existing implicit rule
281                                   to get the commands for updating a file.
282 * Catalogue of Rules::          A list of built-in implicit rules.
283 * Implicit Variables::          How to change what predefined rules do.
284 * Chained Rules::               How to use a chain of implicit rules.
285 * Pattern Rules::               How to define new implicit rules.
286 * Last Resort::                 How to defining commands for rules 
287                                   which cannot find any.
288 * Suffix Rules::                The old-fashioned style of implicit rule.
289 * Search Algorithm::            The precise algorithm for applying 
290                                   implicit rules.
292 Defining and Redefining Pattern Rules
294 * Pattern Intro::               An introduction to pattern rules.
295 * Pattern Examples::            Examples of pattern rules.
296 * Automatic::                   How to use automatic variables in the
297                                   commands of implicit rules.
298 * Pattern Match::               How patterns match.
299 * Match-Anything Rules::        Precautions you should take prior to
300                                   defining rules that can match any
301                                   target file whatever.
302 * Canceling Rules::             How to override or cancel built-in rules.
304 Using @code{make} to Update Archive Files
306 * Archive Members::             Archive members as targets.
307 * Archive Update::              The implicit rule for archive member targets.
308 * Archive Suffix Rules::        You can write a special kind of suffix rule
309                                   for updating archives.
311 Implicit Rule for Archive Member Targets
313 * Archive Symbols::             How to update archive symbol directories.
314 @end menu
316 @node Overview, Introduction, Top, Top
317 @comment  node-name,  next,  previous,  up
318 @chapter Overview of @code{make}
320 The @code{make} utility automatically determines which pieces of a large
321 program need to be recompiled, and issues commands to recompile them.
322 This manual describes GNU @code{make}, which was implemented by Richard
323 Stallman and Roland McGrath.  GNU @code{make} conforms to section 6.2 of
324 @cite{IEEE Standard 1003.2-1992} (POSIX.2).
325 @cindex POSIX
326 @cindex IEEE Standard 1003.2
327 @cindex standards conformance
329 Our examples show C programs, since they are most common, but you can use
330 @code{make} with any programming language whose compiler can be run with a
331 shell command.  Indeed, @code{make} is not limited to programs.  You can
332 use it to describe any task where some files must be updated automatically
333 from others whenever the others change.
335 @menu
336 * Preparing::                   Preparing and Running Make
337 * Reading::                     On Reading this Text
338 * Bugs::                        Problems and Bugs
339 @end menu
341 @node Preparing, Reading,  , Overview
342 @ifinfo
343 @heading Preparing and Running Make
344 @end ifinfo
346 To prepare to use @code{make}, you must write a file called
347 the @dfn{makefile} that describes the relationships among files
348 in your program and provides commands for updating each file.
349 In a program, typically, the executable file is updated from object
350 files, which are in turn made by compiling source files.@refill
352 Once a suitable makefile exists, each time you change some source files,
353 this simple shell command:
355 @example
356 make
357 @end example
359 @noindent
360 suffices to perform all necessary recompilations.  The @code{make} program
361 uses the makefile data base and the last-modification times of the files to
362 decide which of the files need to be updated.  For each of those files, it
363 issues the commands recorded in the data base.
365 You can provide command line arguments to @code{make} to control which
366 files should be recompiled, or how.  @xref{Running, ,How to Run
367 @code{make}}.
369 @node Reading, Bugs, Preparing, Overview
370 @section How to Read This Manual
372 If you are new to @code{make}, or are looking for a general
373 introduction, read the first few sections of each chapter, skipping the
374 later sections.  In each chapter, the first few sections contain
375 introductory or general information and the later sections contain
376 specialized or technical information.
377 @ifinfo        
378 The exception is the second chapter, @ref{Introduction, ,An
379 Introduction to Makefiles}, all of which is introductory.
380 @end ifinfo
381 @iftex 
382 The exception is @ref{Introduction, ,An Introduction to Makefiles},
383 all of which is introductory.
384 @end iftex
386 If you are familiar with other @code{make} programs, see @ref{Features,
387 ,Features of GNU @code{make}}, which lists the enhancements GNU
388 @code{make} has, and @ref{Missing, ,Incompatibilities and Missing
389 Features}, which explains the few things GNU @code{make} lacks that
390 others have.
392 For a quick summary, see @ref{Options Summary}, @ref{Quick Reference},
393 and @ref{Special Targets}.
395 @node Bugs,  , Reading, Overview
396 @section Problems and Bugs
397 @cindex reporting bugs
398 @cindex bugs, reporting
399 @cindex problems and bugs, reporting
401 If you have problems with GNU @code{make} or think you've found a bug,
402 please report it to the developers; we cannot promise to do anything but
403 we might well want to fix it.
405 Before reporting a bug, make sure you've actually found a real bug.
406 Carefully reread the documentation and see if it really says you can do
407 what you're trying to do.  If it's not clear whether you should be able
408 to do something or not, report that too; it's a bug in the
409 documentation!
411 Before reporting a bug or trying to fix it yourself, try to isolate it
412 to the smallest possible makefile that reproduces the problem.  Then
413 send us the makefile and the exact results @code{make} gave you.  Also
414 say what you expected to occur; this will help us decide whether the
415 problem was really in the documentation.
417 Once you've got a precise problem, please send electronic mail either
418 through the Internet or via UUCP:
420 @example
421 @group
422 @r{Internet address:}
423     bug-gnu-utils@@prep.ai.mit.edu
425 @r{UUCP path:}
426     mit-eddie!prep.ai.mit.edu!bug-gnu-utils
427 @end group
428 @end example
430 @noindent
431 Please include the version number of @code{make} you are using.  You can
432 get this information with the command @samp{make --version}.
433 Be sure also to include the type of machine and operating system you are
434 using.  If possible, include the contents of the file @file{config.h}
435 that is generated by the configuration process.
437 Non-bug suggestions are always welcome as well.  If you have questions
438 about things that are unclear in the documentation or are just obscure
439 features, send a message to the bug reporting address.  We cannot
440 guarantee you'll get help with your problem, but many seasoned
441 @code{make} users read the mailing list and they will probably try to
442 help you out.  The maintainers sometimes answer such questions as well,
443 when time permits.
445 @node Introduction, Makefiles, Overview, Top
446 @comment  node-name,  next,  previous,  up
447 @chapter An Introduction to Makefiles
449 You need a file called a @dfn{makefile} to tell @code{make} what to do.
450 Most often, the makefile tells @code{make} how to compile and link a
451 program.
452 @cindex makefile
454 In this chapter, we will discuss a simple makefile that describes how to
455 compile and link a text editor which consists of eight C source files
456 and three header files.  The makefile can also tell @code{make} how to
457 run miscellaneous commands when explicitly asked (for example, to remove
458 certain files as a clean-up operation).  To see a more complex example
459 of a makefile, see @ref{Complex Makefile}.
461 When @code{make} recompiles the editor, each changed C source file
462 must be recompiled.  If a header file has changed, each C source file
463 that includes the header file must be recompiled to be safe.  Each
464 compilation produces an object file corresponding to the source file.
465 Finally, if any source file has been recompiled, all the object files,
466 whether newly made or saved from previous compilations, must be linked
467 together to produce the new executable editor.
468 @cindex recompilation
469 @cindex editor
471 @menu
472 * Rule Introduction::           What a rule looks like.
473 * Simple Makefile::             A Simple Makefile
474 * How Make Works::              How @code{make} Processes This Makefile
475 * Variables Simplify::          Variables Make Makefiles Simpler
476 * make Deduces::                Letting @code{make} Deduce the Commands
477 * Combine By Dependency::       Another Style of Makefile
478 * Cleanup::                     Rules for Cleaning the Directory
479 @end menu
481 @node Rule Introduction, Simple Makefile,  , Introduction
482 @comment  node-name,  next,  previous,  up
483 @section What a Rule Looks Like
484 @cindex rule, introduction to
485 @cindex makefile rule parts
486 @cindex parts of makefile rule
488 A simple makefile consists of ``rules'' with the following shape:
490 @cindex targets, introduction to
491 @cindex dependencies, introduction to
492 @cindex commands, introduction to
493 @example
494 @group
495 @var{target} @dots{} : @var{dependencies} @dots{}
496         @var{command}
497         @dots{}
498         @dots{}
499 @end group
500 @end example
502 A @dfn{target} is usually the name of a file that is generated by a
503 program; examples of targets are executable or object files.  A target
504 can also be the name of an action to carry out, such as @samp{clean}
505 (@pxref{Phony Targets}).
507 A @dfn{dependency} is a file that is used as input to create the
508 target.  A target often depends on several files.  
510 @cindex tabs in rules
511 A @dfn{command} is an action that @code{make} carries out.
512 A rule may have more than one command, each on its own line.
513 @strong{Please note:} you need to put a tab character at the beginning of
514 every command line!  This is an obscurity that catches the unwary.
516 Usually a command is in a rule with dependencies and serves to create a
517 target file if any of the dependencies change.  However, the rule that
518 specifies commands for the target need not have dependencies.  For
519 example, the rule containing the delete command associated with the
520 target @samp{clean} does not have dependencies.
522 A @dfn{rule}, then, explains how and when to remake certain files
523 which are the targets of the particular rule.  @code{make} carries out
524 the commands on the dependencies to create or update the target.  A
525 rule can also explain how and when to carry out an action.
526 @xref{Rules, , Writing Rules}.
528 A makefile may contain other text besides rules, but a simple makefile
529 need only contain rules.  Rules may look somewhat more complicated
530 than shown in this template, but all fit the pattern more or less.
532 @node Simple Makefile, How Make Works, Rule Introduction, Introduction
533 @section A Simple Makefile
534 @cindex simple makefile
535 @cindex makefile, simple
537 Here is a straightforward makefile that describes the way an
538 executable file called @code{edit} depends on eight object files
539 which, in turn, depend on eight C source and three header files.
541 In this example, all the C files include @file{defs.h}, but only those
542 defining editing commands include @file{command.h}, and only low
543 level files that change the editor buffer include @file{buffer.h}.
545 @example
546 @group
547 edit : main.o kbd.o command.o display.o \
548        insert.o search.o files.o utils.o
549         cc -o edit main.o kbd.o command.o display.o \
550                    insert.o search.o files.o utils.o
552 main.o : main.c defs.h
553         cc -c main.c
554 kbd.o : kbd.c defs.h command.h
555         cc -c kbd.c
556 command.o : command.c defs.h command.h
557         cc -c command.c
558 display.o : display.c defs.h buffer.h
559         cc -c display.c
560 insert.o : insert.c defs.h buffer.h
561         cc -c insert.c
562 search.o : search.c defs.h buffer.h
563         cc -c search.c
564 files.o : files.c defs.h buffer.h command.h
565         cc -c files.c
566 utils.o : utils.c defs.h
567         cc -c utils.c
568 clean :
569         rm edit main.o kbd.o command.o display.o \
570            insert.o search.o files.o utils.o
571 @end group
572 @end example
574 @noindent
575 We split each long line into two lines using backslash-newline; this is
576 like using one long line, but is easier to read.
577 @cindex continuation lines
578 @cindex @code{\} (backslash), for continuation lines
579 @cindex backslash (@code{\}), for continuation lines
580 @cindex quoting newline, in makefile
581 @cindex newline, quoting, in makefile
583 To use this makefile to create the executable file called @file{edit},
584 type:
586 @example
587 make
588 @end example
590 To use this makefile to delete the executable file and all the object
591 files from the directory, type:
593 @example
594 make clean
595 @end example
597 In the example makefile, the targets include the executable file
598 @samp{edit}, and the object files @samp{main.o} and @samp{kbd.o}.  The
599 dependencies are files such as @samp{main.c} and @samp{defs.h}.
600 In fact, each @samp{.o} file is both a target and a dependency.
601 Commands include @w{@samp{cc -c main.c}} and @w{@samp{cc -c kbd.c}}.
603 When a target is a file, it needs to be recompiled or relinked if any
604 of its dependencies change.  In addition, any dependencies that are
605 themselves automatically generated should be updated first.  In this
606 example, @file{edit} depends on each of the eight object files; the
607 object file @file{main.o} depends on the source file @file{main.c} and
608 on the header file @file{defs.h}.  
610 A shell command follows each line that contains a target and
611 dependencies.  These shell commands say how to update the target file.
612 A tab character must come at the beginning of every command line to
613 distinguish commands lines from other lines in the makefile.  (Bear in
614 mind that @code{make} does not know anything about how the commands
615 work.  It is up to you to supply commands that will update the target
616 file properly.  All @code{make} does is execute the commands in the rule
617 you have specified when the target file needs to be updated.)
618 @cindex shell command
620 The target @samp{clean} is not a file, but merely the name of an
621 action.  Since you 
622 normally
623 do not want to carry out the actions in this rule, @samp{clean} is not a dependency of any other rule.
624 Consequently, @code{make} never does anything with it unless you tell
625 it specifically.  Note that this rule not only is not a dependency, it
626 also does not have any dependencies, so the only purpose of the rule
627 is to run the specified commands.  Targets that do not refer to files
628 but are just actions are called @dfn{phony targets}.  @xref{Phony
629 Targets}, for information about this kind of target.  @xref{Errors, ,
630 Errors in Commands}, to see how to cause @code{make} to ignore errors
631 from @code{rm} or any other command.
632 @cindex @code{clean} target
633 @cindex @code{rm} (shell command)
635 @node How Make Works, Variables Simplify, Simple Makefile, Introduction
636 @comment  node-name,  next,  previous,  up
637 @section How @code{make} Processes a Makefile
638 @cindex processing a makefile
639 @cindex makefile, how @code{make} processes
641 By default, @code{make} starts with the first rule (not counting rules
642 whose target names start with @samp{.}).  This is called the
643 @dfn{default goal}.  (@dfn{Goals} are the targets that @code{make}
644 strives ultimately to update.  @xref{Goals, , Arguments to Specify the
645 Goals}.)
646 @cindex default goal
647 @cindex goal, default
648 @cindex goal
650 In the simple example of the previous section, the default goal is to
651 update the executable program @file{edit}; therefore, we put that rule
652 first.
654 Thus, when you give the command:
656 @example
657 make
658 @end example
660 @noindent
661 @code{make} reads the makefile in the current directory and begins by
662 processing the first rule.  In the example, this rule is for relinking
663 @file{edit}; but before @code{make} can fully process this rule, it
664 must process the rules for the files that @file{edit} depends on,
665 which in this case are the object files.  Each of these files is
666 processed according to its own rule.  These rules say to update each
667 @samp{.o} file by compiling its source file.  The recompilation must
668 be done if the source file, or any of the header files named as
669 dependencies, is more recent than the object file, or if the object
670 file does not exist.
672 The other rules are processed because their targets appear as
673 dependencies of the goal.  If some other rule is not depended on by the
674 goal (or anything it depends on, etc.), that rule is not processed,
675 unless you tell @code{make} to do so (with a command such as
676 @w{@code{make clean}}).
678 Before recompiling an object file, @code{make} considers updating its
679 dependencies, the source file and header files.  This makefile does not
680 specify anything to be done for them---the @samp{.c} and @samp{.h} files
681 are not the targets of any rules---so @code{make} does nothing for these
682 files.  But @code{make} would update automatically generated C programs,
683 such as those made by Bison or Yacc, by their own rules at this time.
685 After recompiling whichever object files need it, @code{make} decides
686 whether to relink @file{edit}.  This must be done if the file
687 @file{edit} does not exist, or if any of the object files are newer than
688 it.  If an object file was just recompiled, it is now newer than
689 @file{edit}, so @file{edit} is relinked.
690 @cindex relinking
692 Thus, if we change the file @file{insert.c} and run @code{make},
693 @code{make} will compile that file to update @file{insert.o}, and then
694 link @file{edit}.  If we change the file @file{command.h} and run
695 @code{make}, @code{make} will recompile the object files @file{kbd.o},
696 @file{command.o} and @file{files.o} and then link the file @file{edit}.
698 @node Variables Simplify, make Deduces, How Make Works, Introduction
699 @section Variables Make Makefiles Simpler
700 @cindex variables
701 @cindex simplifying with variables
703 In our example, we had to list all the object files twice in the rule for
704 @file{edit} (repeated here):
706 @example
707 @group
708 edit : main.o kbd.o command.o display.o \
709               insert.o search.o files.o utils.o
710         cc -o edit main.o kbd.o command.o display.o \
711                    insert.o search.o files.o utils.o
712 @end group
713 @end example
715 @cindex @code{objects}
716 Such duplication is error-prone; if a new object file is added to the
717 system, we might add it to one list and forget the other.  We can eliminate
718 the risk and simplify the makefile by using a variable.  @dfn{Variables}
719 allow a text string to be defined once and substituted in multiple places
720 later (@pxref{Using Variables, ,How to Use Variables}).
722 @cindex @code{OBJECTS}
723 @cindex @code{objs}
724 @cindex @code{OBJS}
725 @cindex @code{obj}
726 @cindex @code{OBJ}
727 It is standard practice for every makefile to have a variable named
728 @code{objects}, @code{OBJECTS}, @code{objs}, @code{OBJS}, @code{obj},
729 or @code{OBJ} which is a list of all object file names.  We would
730 define such a variable @code{objects} with a line like this in the
731 makefile:@refill
733 @example
734 @group
735 objects = main.o kbd.o command.o display.o \
736           insert.o search.o files.o utils.o
737 @end group
738 @end example
740 @noindent
741 Then, each place we want to put a list of the object file names, we can
742 substitute the variable's value by writing @samp{$(objects)}
743 (@pxref{Using Variables, ,How to Use Variables}).  
745 Here is how the complete simple makefile looks when you use a variable
746 for the object files:
748 @example
749 @group
750 objects = main.o kbd.o command.o display.o \
751           insert.o search.o files.o utils.o
753 edit : $(objects)
754         cc -o edit $(objects)
755 main.o : main.c defs.h
756         cc -c main.c
757 kbd.o : kbd.c defs.h command.h
758         cc -c kbd.c
759 command.o : command.c defs.h command.h
760         cc -c command.c
761 display.o : display.c defs.h buffer.h
762         cc -c display.c
763 insert.o : insert.c defs.h buffer.h
764         cc -c insert.c
765 search.o : search.c defs.h buffer.h
766         cc -c search.c
767 files.o : files.c defs.h buffer.h command.h
768         cc -c files.c
769 utils.o : utils.c defs.h
770         cc -c utils.c
771 clean :
772         rm edit $(objects)
773 @end group
774 @end example
776 @node make Deduces, Combine By Dependency, Variables Simplify, Introduction
777 @section Letting @code{make} Deduce the Commands
778 @cindex deducing commands (implicit rules)
779 @cindex implicit rule, introduction to
780 @cindex rule, implicit, introduction to
782 It is not necessary to spell out the commands for compiling the individual
783 C source files, because @code{make} can figure them out: it has an
784 @dfn{implicit rule} for updating a @samp{.o} file from a correspondingly
785 named @samp{.c} file using a @samp{cc -c} command.  For example, it will
786 use the command @samp{cc -c main.c -o main.o} to compile @file{main.c} into
787 @file{main.o}.  We can therefore omit the commands from the rules for the
788 object files.  @xref{Implicit Rules, ,Using Implicit Rules}.@refill
790 When a @samp{.c} file is used automatically in this way, it is also
791 automatically added to the list of dependencies.  We can therefore omit
792 the @samp{.c} files from the dependencies, provided we omit the commands.
794 Here is the entire example, with both of these changes, and a variable
795 @code{objects} as suggested above:
797 @example
798 @group
799 objects = main.o kbd.o command.o display.o \
800           insert.o search.o files.o utils.o
802 edit : $(objects)
803         cc -o edit $(objects)
805 main.o : defs.h
806 kbd.o : defs.h command.h
807 command.o : defs.h command.h
808 display.o : defs.h buffer.h
809 insert.o : defs.h buffer.h
810 search.o : defs.h buffer.h
811 files.o : defs.h buffer.h command.h
812 utils.o : defs.h
814 .PHONY : clean
815 clean :
816         -rm edit $(objects)
817 @end group
818 @end example
820 @noindent
821 This is how we would write the makefile in actual practice.  (The
822 complications associated with @samp{clean} are described elsewhere.
823 See @ref{Phony Targets}, and @ref{Errors, ,Errors in Commands}.)
825 Because implicit rules are so convenient, they are important.  You
826 will see them used frequently.@refill
828 @node Combine By Dependency, Cleanup, make Deduces, Introduction
829 @section Another Style of Makefile
830 @cindex combining rules by dependency
832 When the objects of a makefile are created only by implicit rules, an
833 alternative style of makefile is possible.  In this style of makefile,
834 you group entries by their dependencies instead of by their targets.
835 Here is what one looks like:
837 @example
838 @group
839 objects = main.o kbd.o command.o display.o \
840           insert.o search.o files.o utils.o
842 edit : $(objects)
843         cc -o edit $(objects)
845 $(objects) : defs.h
846 kbd.o command.o files.o : command.h
847 display.o insert.o search.o files.o : buffer.h
848 @end group
849 @end example
851 @noindent
852 Here @file{defs.h} is given as a dependency of all the object files;
853 @file{command.h} and @file{buffer.h} are dependencies of the specific
854 object files listed for them.
856 Whether this is better is a matter of taste: it is more compact, but some
857 people dislike it because they find it clearer to put all the information
858 about each target in one place.
860 @node Cleanup,  , Combine By Dependency, Introduction
861 @section Rules for Cleaning the Directory
862 @cindex cleaning up
863 @cindex removing, to clean up
865 Compiling a program is not the only thing you might want to write rules
866 for.  Makefiles commonly tell how to do a few other things besides
867 compiling a program: for example, how to delete all the object files
868 and executables so that the directory is @samp{clean}.
870 @cindex @code{clean} target
871 Here is how we
872 could write a @code{make} rule for cleaning our example editor:
874 @example
875 @group
876 clean:
877         rm edit $(objects)
878 @end group
879 @end example
881 In practice, we might want to write the rule in a somewhat more
882 complicated manner to handle unanticipated situations.  We would do this:
884 @example
885 @group
886 .PHONY : clean
887 clean :
888         -rm edit $(objects)
889 @end group
890 @end example
892 @noindent
893 This prevents @code{make} from getting confused by an actual file
894 called @file{clean} and causes it to continue in spite of errors from
895 @code{rm}.  (See @ref{Phony Targets}, and @ref{Errors, ,Errors in
896 Commands}.)
898 @noindent
899 A rule such as this should not be placed at the beginning of the
900 makefile, because we do not want it to run by default!  Thus, in the
901 example makefile, we want the rule for @code{edit}, which recompiles
902 the editor, to remain the default goal.
904 Since @code{clean} is not a dependency of @code{edit}, this rule will not
905 run at all if we give the command @samp{make} with no arguments.  In
906 order to make the rule run, we have to type @samp{make clean}.
907 @xref{Running, ,How to Run @code{make}}.
909 @node Makefiles, Rules, Introduction, Top
910 @chapter Writing Makefiles
912 @cindex makefile, how to write
913 The information that tells @code{make} how to recompile a system comes from
914 reading a data base called the @dfn{makefile}.
916 @menu
917 * Makefile Contents::           What makefiles contain.
918 * Makefile Names::              How to name your makefile.
919 * Include::                     How one makefile can use another makefile.
920 * MAKEFILES Variable::          The environment can specify extra makefiles.
921 * Remaking Makefiles::          How makefiles get remade.
922 * Overriding Makefiles::        How to override part of one makefile
923                                  with another makefile.
924 @end menu
926 @node Makefile Contents, Makefile Names,  , Makefiles
927 @section What Makefiles Contain
929 Makefiles contain five kinds of things: @dfn{explicit rules},
930 @dfn{implicit rules}, @dfn{variable definitions}, @dfn{directives},
931 and @dfn{comments}.  Rules, variables, and directives are described at
932 length in later chapters.@refill
934 @itemize @bullet
935 @cindex rule, explicit, definition of
936 @cindex explicit rule, definition of
937 @item
938 An @dfn{explicit rule} says when and how to remake one or more files,
939 called the rule's targets.  It lists the other files that the targets
940 @dfn{depend on}, and may also give commands to use to create or update
941 the targets.  @xref{Rules, ,Writing Rules}.
943 @cindex rule, implicit, definition of
944 @cindex implicit rule, definition of
945 @item
946 An @dfn{implicit rule} says when and how to remake a class of files
947 based on their names.  It describes how a target may depend on a file
948 with a name similar to the target and gives commands to create or
949 update such a target.  @xref{Implicit Rules, ,Using Implicit Rules}.
951 @cindex variable definition
952 @item
953 A @dfn{variable definition} is a line that specifies a text string
954 value for a variable that can be substituted into the text later.  The
955 simple makefile example shows a variable definition for @code{objects}
956 as a list of all object files (@pxref{Variables Simplify, , Variables
957 Make Makefiles Simpler}).
959 @cindex directive
960 @item
961 A @dfn{directive} is a command for @code{make} to do something special while
962 reading the makefile.  These include:
964 @itemize @bullet
965 @item
966 Reading another makefile (@pxref{Include, ,Including Other Makefiles}).
968 @item
969 Deciding (based on the values of variables) whether to use or
970 ignore a part of the makefile (@pxref{Conditionals, ,Conditional Parts of Makefiles}).
972 @item
973 Defining a variable from a verbatim string containing multiple lines
974 (@pxref{Defining, ,Defining Variables Verbatim}).
975 @end itemize
977 @cindex comments, in makefile
978 @cindex @code{#} (comments), in makefile
979 @item
980 @samp{#} in a line of a makefile starts a @dfn{comment}.  It and the rest of
981 the line are ignored, except that a trailing backslash not escaped by
982 another backslash will continue the comment across multiple lines.
983 Comments may appear on any of the lines in the makefile, except within a
984 @code{define} directive, and perhaps within commands (where the shell
985 decides what is a comment).  A line containing just a comment (with
986 perhaps spaces before it) is effectively blank, and is ignored.@refill
987 @end itemize
989 @node Makefile Names, Include, Makefile Contents, Makefiles
990 @section What Name to Give Your Makefile
991 @cindex makefile name
992 @cindex name of makefile
993 @cindex default makefile name
994 @cindex file name of makefile
996 @c following paragraph rewritten to avoid overfull hbox
997 By default, when @code{make} looks for the makefile, it tries the
998 following names, in order: @file{GNUmakefile}, @file{makefile}
999 and @file{Makefile}.@refill
1000 @findex Makefile
1001 @findex GNUmakefile
1002 @findex makefile
1004 @cindex @code{README}
1005 Normally you should call your makefile either @file{makefile} or
1006 @file{Makefile}.  (We recommend @file{Makefile} because it appears
1007 prominently near the beginning of a directory listing, right near other
1008 important files such as @file{README}.)  The first name checked,
1009 @file{GNUmakefile}, is not recommended for most makefiles.  You should
1010 use this name if you have a makefile that is specific to GNU
1011 @code{make}, and will not be understood by other versions of
1012 @code{make}.  Other @code{make} programs look for @file{makefile} and
1013 @file{Makefile}, but not @file{GNUmakefile}.
1015 If @code{make} finds none of these names, it does not use any makefile.
1016 Then you must specify a goal with a command argument, and @code{make}
1017 will attempt to figure out how to remake it using only its built-in
1018 implicit rules.  @xref{Implicit Rules, ,Using Implicit Rules}.
1020 @cindex @code{-f}
1021 @cindex @code{--file}
1022 @cindex @code{--makefile}
1023 If you want to use a nonstandard name for your makefile, you can specify
1024 the makefile name with the @samp{-f} or @samp{--file} option.  The
1025 arguments @w{@samp{-f @var{name}}} or @w{@samp{--file=@var{name}}} tell
1026 @code{make} to read the file @var{name} as the makefile.  If you use
1027 more than one @samp{-f} or @samp{--file} option, you can specify several
1028 makefiles.  All the makefiles are effectively concatenated in the order
1029 specified.  The default makefile names @file{GNUmakefile},
1030 @file{makefile} and @file{Makefile} are not checked automatically if you
1031 specify @samp{-f} or @samp{--file}.@refill
1032 @cindex specifying makefile name
1033 @cindex makefile name, how to specify
1034 @cindex name of makefile, how to specify
1035 @cindex file name of makefile, how to specify
1037 @node Include, MAKEFILES Variable, Makefile Names, Makefiles
1038 @section Including Other Makefiles
1039 @cindex including other makefiles
1040 @cindex makefile, including
1042 @findex include
1043 The @code{include} directive tells @code{make} to suspend reading the
1044 current makefile and read one or more other makefiles before continuing.
1045 The directive is a line in the makefile that looks like this:
1047 @example
1048 include @var{filenames}@dots{}
1049 @end example
1051 @noindent
1052 @var{filenames} can contain shell file name patterns.
1053 @cindex shell file name pattern (in @code{include})
1054 @cindex shell wildcards (in @code{include})
1055 @cindex wildcard, in @code{include}
1057 Extra spaces are allowed and ignored at the beginning of the line, but
1058 a tab is not allowed.  (If the line begins with a tab, it will be
1059 considered a command line.)  Whitespace is required between
1060 @code{include} and the file names, and between file names; extra
1061 whitespace is ignored there and at the end of the directive.  A
1062 comment starting with @samp{#} is allowed at the end of the line.  If
1063 the file names contain any variable or function references, they are
1064 expanded.  @xref{Using Variables, ,How to Use Variables}.
1066 For example, if you have three @file{.mk} files, @file{a.mk}, 
1067 @file{b.mk}, and @file{c.mk}, and @code{$(bar)} expands to
1068 @code{bish bash}, then the following expression
1070 @example
1071 include foo *.mk $(bar)
1072 @end example
1074 is equivalent to
1076 @example
1077 include foo a.mk b.mk c.mk bish bash
1078 @end example
1080 When @code{make} processes an @code{include} directive, it suspends
1081 reading of the containing makefile and reads from each listed file in
1082 turn.  When that is finished, @code{make} resumes reading the
1083 makefile in which the directive appears.
1085 One occasion for using @code{include} directives is when several programs,
1086 handled by individual makefiles in various directories, need to use a
1087 common set of variable definitions 
1088 (@pxref{Setting, ,Setting Variables}) or pattern rules
1089 (@pxref{Pattern Rules, ,Defining and Redefining Pattern Rules}).
1091 Another such occasion is when you want to generate dependencies from
1092 source files automatically; the dependencies can be put in a file that
1093 is included by the main makefile.  This practice is generally cleaner
1094 than that of somehow appending the dependencies to the end of the main
1095 makefile as has been traditionally done with other versions of
1096 @code{make}.  @xref{Automatic Dependencies}.
1097 @cindex dependencies, automatic generation
1098 @cindex automatic generation of dependencies
1099 @cindex generating dependencies automatically
1101 @cindex @code{-I}
1102 @cindex @code{--include-dir}
1103 @findex /usr/gnu/include
1104 @findex /usr/local/include
1105 @findex /usr/include
1106 If the specified name does not start with a slash, and the file is not
1107 found in the current directory, several other directories are searched.
1108 First, any directories you have specified with the @samp{-I} or
1109 @samp{--include-dir} option are searched
1110 (@pxref{Options Summary, ,Summary of Options}).
1111 Then the following directories (if they exist)
1112 are searched, in this order: 
1113 @file{@var{prefix}/include} (normally @file{/usr/local/include})
1114 @file{/usr/gnu/include},
1115 @file{/usr/local/include}, @file{/usr/include}.  
1117 If an included makefile cannot be found in any of these directories, a
1118 warning message is generated, but it is not an immediately fatal error;
1119 processing of the makefile containing the @code{include} continues.
1120 Once it has finished reading makefiles, @code{make} will try to remake
1121 any that are out of date or don't exist.
1122 @xref{Remaking Makefiles, ,How Makefiles Are Remade}.
1123 Only after it has tried to find a way to remake a makefile and failed,
1124 will @code{make} diagnose the missing makefile as a fatal error.
1126 If you want @code{make} to simply ignore a makefile which does not exist
1127 and cannot be remade, with no error message, use the @w{@code{-include}}
1128 directive instead of @code{include}, like this:
1130 @example
1131 -include @var{filenames}@dots{}
1132 @end example
1134 This is acts like @code{include} in every way except that there is no
1135 error (not even a warning) if any of the @var{filenames} do not exist.
1137 @node MAKEFILES Variable, Remaking Makefiles, Include, Makefiles
1138 @section The Variable @code{MAKEFILES}
1139 @cindex makefile, and @code{MAKEFILES} variable
1140 @cindex including (@code{MAKEFILES} variable)
1142 @vindex MAKEFILES
1143 If the environment variable @code{MAKEFILES} is defined, @code{make}
1144 considers its value as a list of names (separated by whitespace) of
1145 additional makefiles to be read before the others.  This works much like
1146 the @code{include} directive: various directories are searched for those
1147 files (@pxref{Include, ,Including Other Makefiles}).  In addition, the
1148 default goal is never taken from one of these makefiles and it is not an
1149 error if the files listed in @code{MAKEFILES} are not found.@refill
1151 @cindex recursion, and @code{MAKEFILES} variable
1152 The main use of @code{MAKEFILES} is in communication between recursive
1153 invocations of @code{make} (@pxref{Recursion, ,Recursive Use of
1154 @code{make}}).  It usually is not desirable to set the environment
1155 variable before a top-level invocation of @code{make}, because it is
1156 usually better not to mess with a makefile from outside.  However, if
1157 you are running @code{make} without a specific makefile, a makefile in
1158 @code{MAKEFILES} can do useful things to help the built-in implicit
1159 rules work better, such as defining search paths (@pxref{Directory Search}).
1161 Some users are tempted to set @code{MAKEFILES} in the environment
1162 automatically on login, and program makefiles to expect this to be done.
1163 This is a very bad idea, because such makefiles will fail to work if run by
1164 anyone else.  It is much better to write explicit @code{include} directives
1165 in the makefiles.  @xref{Include, , Including Other Makefiles}.
1167 @node Remaking Makefiles, Overriding Makefiles, MAKEFILES Variable, Makefiles
1168 @section How Makefiles Are Remade
1170 @cindex updating makefiles
1171 @cindex remaking makefiles
1172 @cindex makefile, remaking of
1173 Sometimes makefiles can be remade from other files, such as RCS or SCCS
1174 files.  If a makefile can be remade from other files, you probably want
1175 @code{make} to get an up-to-date version of the makefile to read in.
1177 To this end, after reading in all makefiles, @code{make} will consider
1178 each as a goal target and attempt to update it.  If a makefile has a
1179 rule which says how to update it (found either in that very makefile or
1180 in another one) or if an implicit rule applies to it (@pxref{Implicit
1181 Rules, ,Using Implicit Rules}), it will be updated if necessary.  After
1182 all makefiles have been checked, if any have actually been changed,
1183 @code{make} starts with a clean slate and reads all the makefiles over
1184 again.  (It will also attempt to update each of them over again, but
1185 normally this will not change them again, since they are already up to
1186 date.)@refill
1188 If the makefiles specify a double-colon rule to remake a file with
1189 commands but no dependencies, that file will always be remade
1190 (@pxref{Double-Colon}).  In the case of makefiles, a makefile that has a
1191 double-colon rule with commands but no dependencies will be remade every
1192 time @code{make} is run, and then again after @code{make} starts over
1193 and reads the makefiles in again.  This would cause an infinite loop:
1194 @code{make} would constantly remake the makefile, and never do anything
1195 else.  So, to avoid this, @code{make} will @strong{not} attempt to
1196 remake makefiles which are specified as double-colon targets but have no
1197 dependencies.@refill
1199 If you do not specify any makefiles to be read with @samp{-f} or
1200 @samp{--file} options, @code{make} will try the default makefile names;
1201 @pxref{Makefile Names, ,What Name to Give Your Makefile}.  Unlike
1202 makefiles explicitly requested with @samp{-f} or @samp{--file} options,
1203 @code{make} is not certain that these makefiles should exist.  However,
1204 if a default makefile does not exist but can be created by running
1205 @code{make} rules, you probably want the rules to be run so that the
1206 makefile can be used.
1208 Therefore, if none of the default makefiles exists, @code{make} will try
1209 to make each of them in the same order in which they are searched for
1210 (@pxref{Makefile Names, ,What Name to Give Your Makefile})
1211 until it succeeds in making one, or it runs out of names to try.  Note
1212 that it is not an error if @code{make} cannot find or make any makefile;
1213 a makefile is not always necessary.@refill
1215 When you use the @samp{-t} or @samp{--touch} option
1216 (@pxref{Instead of Execution, ,Instead of Executing the Commands}),
1217 you would not want to use an out-of-date makefile to decide which
1218 targets to touch.  So the @samp{-t} option has no effect on updating
1219 makefiles; they are really updated even if @samp{-t} is specified.
1220 Likewise, @samp{-q} (or @samp{--question}) and @samp{-n} (or
1221 @samp{--just-print}) do not prevent updating of makefiles, because an
1222 out-of-date makefile would result in the wrong output for other targets.
1223 Thus, @samp{make -f mfile -n foo} will update @file{mfile}, read it in,
1224 and then print the commands to update @file{foo} and its dependencies
1225 without running them.  The commands printed for @file{foo} will be those
1226 specified in the updated contents of @file{mfile}.
1228 However, on occasion you might actually wish to prevent updating of even
1229 the makefiles.  You can do this by specifying the makefiles as goals in
1230 the command line as well as specifying them as makefiles.  When the
1231 makefile name is specified explicitly as a goal, the options @samp{-t}
1232 and so on do apply to them.
1234 Thus, @samp{make -f mfile -n mfile foo} would read the makefile
1235 @file{mfile}, print the commands needed to update it without actually
1236 running them, and then print the commands needed to update @file{foo}
1237 without running them.  The commands for @file{foo} will be those
1238 specified by the existing contents of @file{mfile}.
1240 @node Overriding Makefiles,  , Remaking Makefiles, Makefiles
1241 @section Overriding Part of Another Makefile
1243 @cindex overriding makefiles
1244 @cindex makefile, overriding
1245 Sometimes it is useful to have a makefile that is mostly just like
1246 another makefile.  You can often use the @samp{include} directive to
1247 include one in the other, and add more targets or variable definitions.
1248 However, if the two makefiles give different commands for the same
1249 target, @code{make} will not let you just do this.  But there is another way.
1251 @cindex match-anything rule, used to override
1252 In the containing makefile (the one that wants to include the other),
1253 you can use a match-anything pattern rule to say that to remake any
1254 target that cannot be made from the information in the containing
1255 makefile, @code{make} should look in another makefile.
1256 @xref{Pattern Rules}, for more information on pattern rules.
1258 For example, if you have a makefile called @file{Makefile} that says how
1259 to make the target @samp{foo} (and other targets), you can write a
1260 makefile called @file{GNUmakefile} that contains:
1262 @example
1263 foo:
1264         frobnicate > foo
1266 %: force
1267         @@$(MAKE) -f Makefile $@@
1268 force: ;
1269 @end example
1271 If you say @samp{make foo}, @code{make} will find @file{GNUmakefile},
1272 read it, and see that to make @file{foo}, it needs to run the command
1273 @samp{frobnicate > foo}.  If you say @samp{make bar}, @code{make} will
1274 find no way to make @file{bar} in @file{GNUmakefile}, so it will use the
1275 commands from the pattern rule: @samp{make -f Makefile bar}.  If
1276 @file{Makefile} provides a rule for updating @file{bar}, @code{make}
1277 will apply the rule.  And likewise for any other target that
1278 @file{GNUmakefile} does not say how to make.
1280 The way this works is that the pattern rule has a pattern of just
1281 @samp{%}, so it matches any target whatever.  The rule specifies a
1282 dependency @file{force}, to guarantee that the commands will be run even
1283 if the target file already exists.  We give @file{force} target empty
1284 commands to prevent @code{make} from searching for an implicit rule to
1285 build it---otherwise it would apply the same match-anything rule to
1286 @file{force} itself and create a dependency loop!
1288 @node Rules, Commands, Makefiles, Top
1289 @chapter Writing Rules
1290 @cindex writing rules
1291 @cindex rule, how to write
1292 @cindex target
1293 @cindex dependency
1295 A @dfn{rule} appears in the makefile and says when and how to remake
1296 certain files, called the rule's @dfn{targets} (most often only one per rule).
1297 It lists the other files that are the @dfn{dependencies} of the target, and
1298 @dfn{commands} to use to create or update the target.
1300 @cindex default goal
1301 @cindex goal, default
1302 The order of rules is not significant, except for determining the
1303 @dfn{default goal}: the target for @code{make} to consider, if you do
1304 not otherwise specify one.  The default goal is the target of the first
1305 rule in the first makefile.  If the first rule has multiple targets,
1306 only the first target is taken as the default.  There are two
1307 exceptions: a target starting with a period is not a default unless it
1308 contains one or more slashes, @samp{/}, as well; and, a target that
1309 defines a pattern rule has no effect on the default goal.
1310 (@xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.)
1312 Therefore, we usually write the makefile so that the first rule is the
1313 one for compiling the entire program or all the programs described by
1314 the makefile (often with a target called @samp{all}).
1315 @xref{Goals, ,Arguments to Specify the Goals}.
1317 @menu
1318 * Rule Example::                An example explained.
1319 * Rule Syntax::                 General syntax explained.
1320 * Wildcards::                   Using wildcard characters such as `*'.
1321 * Directory Search::            Searching other directories for source files.
1322 * Phony Targets::               Using a target that is not a real file's name.
1323 * Force Targets::               You can use a target without commands
1324                                   or dependencies to mark other 
1325                                   targets as phony.
1326 * Empty Targets::               When only the date matters and the
1327                                   files are empty.
1328 * Special Targets::             Targets with special built-in meanings.
1329 * Multiple Targets::            When to make use of several targets in a rule.
1330 * Multiple Rules::              How to use several rules with the same target.
1331 * Static Pattern::              Static pattern rules apply to multiple targets
1332                                   and can vary the dependencies according to 
1333                                   the target name.
1334 * Double-Colon::                How to use a special kind of rule to allow
1335                                   several independent rules for one target.
1336 * Automatic Dependencies::      How to automatically generate rules giving
1337                                  dependencies from the source files themselves.
1338 @end menu
1340 @ifinfo
1341 @node Rule Example, Rule Syntax,  , Rules
1342 @section Rule Example
1344 Here is an example of a rule:
1346 @example
1347 foo.o : foo.c defs.h       # module for twiddling the frobs
1348         cc -c -g foo.c
1349 @end example
1351 Its target is @file{foo.o} and its dependencies are @file{foo.c} and
1352 @file{defs.h}.  It has one command, which is @samp{cc -c -g foo.c}.
1353 The command line starts with a tab to identify it as a command.
1355 This rule says two things:
1357 @itemize @bullet
1358 @item
1359 How to decide whether @file{foo.o} is out of date: it is out of date
1360 if it does not exist, or if either @file{foo.c} or @file{defs.h} is
1361 more recent than it.
1363 @item
1364 How to update the file @file{foo.o}: by running @code{cc} as stated.
1365 The command does not explicitly mention @file{defs.h}, but we presume
1366 that @file{foo.c} includes it, and that that is why @file{defs.h} was
1367 added to the dependencies.
1368 @end itemize
1369 @end ifinfo
1371 @node Rule Syntax, Wildcards, Rule Example, Rules
1372 @section Rule Syntax
1374 @cindex rule syntax
1375 @cindex syntax of rules
1376 In general, a rule looks like this:
1378 @example
1379 @var{targets} : @var{dependencies}
1380         @var{command}
1381         @dots{}
1382 @end example
1384 @noindent
1385 or like this:
1387 @example
1388 @var{targets} : @var{dependencies} ; @var{command}
1389         @var{command}
1390         @dots{}
1391 @end example
1393 @cindex targets
1394 @cindex rule targets
1395 The @var{targets} are file names, separated by spaces.  Wildcard
1396 characters may be used (@pxref{Wildcards, ,Using Wildcard Characters
1397 in File Names}) and a name of the form @file{@var{a}(@var{m})}
1398 represents member @var{m} in archive file @var{a} 
1399 (@pxref{Archive Members, ,Archive Members as Targets}).  
1400 Usually there is only one
1401 target per rule, but occasionally there is a reason to have more
1402 (@pxref{Multiple Targets, , Multiple Targets in a Rule}).@refill
1404 @cindex commands
1405 @cindex tab character (in commands)
1406 The @var{command} lines start with a tab character.  The first command may
1407 appear on the line after the dependencies, with a tab character, or may
1408 appear on the same line, with a semicolon.  Either way, the effect is the
1409 same.  @xref{Commands, ,Writing the Commands in Rules}.
1411 @cindex dollar sign (@code{$}), in rules
1412 @cindex @code{$}, in rules
1413 @cindex rule, and @code{$}
1414 Because dollar signs are used to start variable references, if you really
1415 want a dollar sign in a rule you must write two of them, @samp{$$}
1416 (@pxref{Using Variables, ,How to Use Variables}).  
1417 You may split a long line by inserting a backslash
1418 followed by a newline, but this is not required, as @code{make} places no
1419 limit on the length of a line in a makefile.
1421 A rule tells @code{make} two things: when the targets are out of date,
1422 and how to update them when necessary.
1424 @cindex dependencies
1425 @cindex rule dependencies
1426 The criterion for being out of date is specified in terms of the
1427 @var{dependencies}, which consist of file names separated by spaces.
1428 (Wildcards and archive members (@pxref{Archives}) are allowed here too.)
1429 A target is out of date if it does not exist or if it is older than any
1430 of the dependencies (by comparison of last-modification times).  The
1431 idea is that the contents of the target file are computed based on
1432 information in the dependencies, so if any of the dependencies changes,
1433 the contents of the existing target file are no longer necessarily
1434 valid.
1436 How to update is specified by @var{commands}.  These are lines to be
1437 executed by the shell (normally @samp{sh}), but with some extra features
1438 (@pxref{Commands, ,Writing the Commands in Rules}).
1440 @node Wildcards, Directory Search, Rule Syntax, Rules
1441 @section Using Wildcard Characters in File Names
1442 @cindex wildcard
1443 @cindex file name with wildcards
1444 @cindex globbing (wildcards)
1446 @cindex @code{*} (wildcard character)
1447 @cindex @code{?} (wildcard character)
1448 @cindex @code{[@dots{}]} (wildcard characters)
1449 A single file name can specify many files using @dfn{wildcard characters}.
1450 The wildcard characters in @code{make} are @samp{*}, @samp{?} and
1451 @samp{[@dots{}]}, the same as in the Bourne shell.  For example, @file{*.c}
1452 specifies a list of all the files (in the working directory) whose names
1453 end in @samp{.c}.@refill
1455 @cindex @code{~} (tilde)
1456 @cindex tilde (@code{~})
1457 @cindex home directory
1458 The character @samp{~} at the beginning of a file name also has special
1459 significance.  If alone, or followed by a slash, it represents your home
1460 directory.  For example @file{~/bin} expands to @file{/home/you/bin}.
1461 If the @samp{~} is followed by a word, the string represents the home
1462 directory of the user named by that word.  For example @file{~john/bin}
1463 expands to @file{/home/john/bin}.@refill
1465 Wildcard expansion happens automatically in targets, in dependencies,
1466 and in commands (where the shell does the expansion).  In other
1467 contexts, wildcard expansion happens only if you request it explicitly
1468 with the @code{wildcard} function.
1470 The special significance of a wildcard character can be turned off by
1471 preceding it with a backslash.  Thus, @file{foo\*bar} would refer to a
1472 specific file whose name consists of @samp{foo}, an asterisk, and
1473 @samp{bar}.@refill
1475 @menu
1476 * Wildcard Examples::           Several examples
1477 * Wildcard Pitfall::            Problems to avoid.
1478 * Wildcard Function::           How to cause wildcard expansion where
1479                                   it does not normally take place.
1480 @end menu
1482 @node Wildcard Examples, Wildcard Pitfall,  , Wildcards
1483 @subsection Wildcard Examples
1485 Wildcards can be used in the commands of a rule, where they are expanded
1486 by the shell.  For example, here is a rule to delete all the object files:
1488 @example
1489 @group
1490 clean:
1491         rm -f *.o
1492 @end group
1493 @end example
1494 @cindex @code{rm} (shell command)
1496 Wildcards are also useful in the dependencies of a rule.  With the
1497 following rule in the makefile, @samp{make print} will print all the
1498 @samp{.c} files that have changed since the last time you printed them:
1500 @example
1501 print: *.c
1502         lpr -p $?
1503         touch print
1504 @end example
1506 @cindex @code{print} target
1507 @cindex @code{lpr} (shell command)
1508 @cindex @code{touch} (shell command)
1509 @noindent
1510 This rule uses @file{print} as an empty target file; see @ref{Empty
1511 Targets, ,Empty Target Files to Record Events}.  (The automatic variable
1512 @samp{$?} is used to print only those files that have changed; see
1513 @ref{Automatic, ,Automatic Variables}.)@refill
1515 Wildcard expansion does not happen when you define a variable.  Thus, if
1516 you write this:
1518 @example
1519 objects = *.o
1520 @end example
1522 @noindent
1523 then the value of the variable @code{objects} is the actual string
1524 @samp{*.o}.  However, if you use the value of @code{objects} in a target,
1525 dependency or command, wildcard expansion will take place at that time.
1526 To set @code{objects} to the expansion, instead use:
1528 @example
1529 objects := $(wildcard *.o)
1530 @end example
1532 @noindent
1533 @xref{Wildcard Function}.
1535 @node Wildcard Pitfall, Wildcard Function, Wildcard Examples, Wildcards
1536 @subsection Pitfalls of Using Wildcards
1537 @cindex wildcard pitfalls
1538 @cindex pitfalls of wildcards
1539 @cindex mistakes with wildcards
1540 @cindex errors with wildcards
1541 @cindex problems with wildcards
1543 Now here is an example of a naive way of using wildcard expansion, that
1544 does not do what you would intend.  Suppose you would like to say that the
1545 executable file @file{foo} is made from all the object files in the
1546 directory, and you write this:
1548 @example
1549 objects = *.o
1551 foo : $(objects)
1552         cc -o foo $(CFLAGS) $(objects)
1553 @end example
1555 @noindent
1556 The value of @code{objects} is the actual string @samp{*.o}.  Wildcard
1557 expansion happens in the rule for @file{foo}, so that each @emph{existing}
1558 @samp{.o} file becomes a dependency of @file{foo} and will be recompiled if
1559 necessary.
1561 But what if you delete all the @samp{.o} files?  When a wildcard matches
1562 no files, it is left as it is, so then @file{foo} will depend on the
1563 oddly-named file @file{*.o}.  Since no such file is likely to exist,
1564 @code{make} will give you an error saying it cannot figure out how to
1565 make @file{*.o}.  This is not what you want!
1567 Actually it is possible to obtain the desired result with wildcard
1568 expansion, but you need more sophisticated techniques, including the
1569 @code{wildcard} function and string substitution.
1570 @ifinfo
1571 @xref{Wildcard Function, ,The Function @code{wildcard}}.
1572 @end ifinfo
1573 @iftex
1574 These are described in the following section.
1575 @end iftex
1577 @node Wildcard Function,  , Wildcard Pitfall, Wildcards
1578 @subsection The Function @code{wildcard}
1579 @findex wildcard
1581 Wildcard expansion happens automatically in rules.  But wildcard expansion
1582 does not normally take place when a variable is set, or inside the
1583 arguments of a function.  If you want to do wildcard expansion in such
1584 places, you need to use the @code{wildcard} function, like this:
1586 @example
1587 $(wildcard @var{pattern}@dots{})
1588 @end example
1590 @noindent
1591 This string, used anywhere in a makefile, is replaced by a
1592 space-separated list of names of existing files that match one of the
1593 given file name patterns.  If no existing file name matches a pattern,
1594 then that pattern is omitted from the output of the @code{wildcard}
1595 function.  Note that this is different from how unmatched wildcards
1596 behave in rules, where they are used verbatim rather than ignored
1597 (@pxref{Wildcard Pitfall}).
1599 One use of the @code{wildcard} function is to get a list of all the C source
1600 files in a directory, like this:
1602 @example
1603 $(wildcard *.c)
1604 @end example
1606 We can change the list of C source files into a list of object files by
1607 replacing the @samp{.o} suffix with @samp{.c} in the result, like this:
1609 @example
1610 $(patsubst %.c,%.o,$(wildcard *.c))
1611 @end example
1613 @noindent
1614 (Here we have used another function, @code{patsubst}.
1615 @xref{Text Functions, ,Functions for String Substitution and Analysis}.)@refill
1617 Thus, a makefile to compile all C source files in the directory and then
1618 link them together could be written as follows:
1620 @example
1621 objects := $(patsubst %.c,%.o,$(wildcard *.c))
1623 foo : $(objects)
1624         cc -o foo $(objects)
1625 @end example
1627 @noindent
1628 (This takes advantage of the implicit rule for compiling C programs, so
1629 there is no need to write explicit rules for compiling the files.
1630 @xref{Flavors, ,The Two Flavors of Variables}, for an explanation of
1631 @samp{:=}, which is a variant of @samp{=}.)
1633 @node Directory Search, Phony Targets, Wildcards, Rules
1634 @section Searching Directories for Dependencies
1635 @vindex VPATH
1636 @findex vpath
1637 @cindex vpath 
1638 @cindex search path for dependencies (@code{VPATH})
1639 @cindex directory search (@code{VPATH})
1641 For large systems, it is often desirable to put sources in a separate
1642 directory from the binaries.  The @dfn{directory search} features of
1643 @code{make} facilitate this by searching several directories
1644 automatically to find a dependency.  When you redistribute the files
1645 among directories, you do not need to change the individual rules,
1646 just the search paths.
1648 @menu
1649 * General Search::              Specifying a search path that applies 
1650                                   to every dependency.
1651 * Selective Search::            Specifying a search path 
1652                                   for a specified class of names.
1653 * Commands/Search::             How to write shell commands that work together
1654                                   with search paths.
1655 * Implicit/Search::             How search paths affect implicit rules.
1656 * Libraries/Search::            Directory search for link libraries.
1657 @end menu
1659 @node General Search, Selective Search,  , Directory Search
1660 @subsection @code{VPATH}: Search Path for All Dependencies
1661 @vindex VPATH
1663 The value of the @code{make} variable @code{VPATH} specifies a list of
1664 directories that @code{make} should search.  Most often, the
1665 directories are expected to contain dependency files that are not in the
1666 current directory; however, @code{VPATH} specifies a search list that
1667 @code{make} applies for all files, including files which are targets of
1668 rules.
1670 Thus, if a file that is listed as a target or dependency does not exist
1671 in the current directory, @code{make} searches the directories listed in
1672 @code{VPATH} for a file with that name.  If a file is found in one of
1673 them, that file becomes the dependency.  Rules may then specify the
1674 names of source files in the dependencies as if they all existed in the
1675 current directory.  @xref{Commands/Search, ,Writing Shell Commands with
1676 Directory Search}.
1678 In the @code{VPATH} variable, directory names are separated by colons or
1679 blanks.  The order in which directories are listed is the order followed
1680 by @code{make} in its search.
1682 For example,
1684 @example
1685 VPATH = src:../headers
1686 @end example
1688 @noindent
1689 specifies a path containing two directories, @file{src} and
1690 @file{../headers}, which @code{make} searches in that order.
1692 With this value of @code{VPATH}, the following rule,
1694 @example
1695 foo.o : foo.c
1696 @end example
1698 @noindent
1699 is interpreted as if it were written like this:
1701 @example
1702 foo.o : src/foo.c
1703 @end example
1705 @noindent
1706 assuming the file @file{foo.c} does not exist in the current directory but
1707 is found in the directory @file{src}.
1709 @node Selective Search, Commands/Search, General Search, Directory Search
1710 @subsection The @code{vpath} Directive
1711 @findex vpath
1713 Similar to the @code{VPATH} variable but more selective is the @code{vpath}
1714 directive (note lower case), which allows you to specify a search path for a particular class
1715 of file names, those that match a particular pattern.  Thus you can supply
1716 certain search directories for one class of file names and other directories
1717 (or none) for other file names.
1719 There are three forms of the @code{vpath} directive:
1721 @table @code
1722 @item vpath @var{pattern} @var{directories}
1723 Specify the search path @var{directories} for file names that match
1724 @var{pattern}.  
1726 The search path, @var{directories}, is a list of directories to be
1727 searched, separated by colons or blanks, just like the search path used
1728 in the @code{VPATH} variable.
1730 @item vpath @var{pattern}
1731 Clear out the search path associated with @var{pattern}.
1733 @c Extra blank line makes sure this gets two lines.
1734 @item vpath
1736 Clear all search paths previously specified with @code{vpath} directives.
1737 @end table
1739 A @code{vpath} pattern is a string containing a @samp{%} character.  The
1740 string must match the file name of a dependency that is being searched
1741 for, the @samp{%} character matching any sequence of zero or more
1742 characters (as in pattern rules; @pxref{Pattern Rules, ,Defining and
1743 Redefining Pattern Rules}).  For example, @code{%.h} matches files that
1744 end in @code{.h}.  (If there is no @samp{%}, the pattern must match the
1745 dependency exactly, which is not useful very often.)
1747 @cindex @code{%}, quoting in @code{vpath}
1748 @cindex @code{%}, quoting with @code{\} (backslash)
1749 @cindex @code{\} (backslash), to quote @code{%}
1750 @cindex backslash (@code{\}), to quote @code{%}
1751 @cindex quoting @code{%}, in @code{vpath}
1752 @samp{%} characters in a @code{vpath} directive's pattern can be quoted
1753 with preceding backslashes (@samp{\}).  Backslashes that would otherwise
1754 quote @samp{%} characters can be quoted with more backslashes.
1755 Backslashes that quote @samp{%} characters or other backslashes are
1756 removed from the pattern before it is compared to file names.  Backslashes
1757 that are not in danger of quoting @samp{%} characters go unmolested.@refill
1759 When a dependency fails to exist in the current directory, if the
1760 @var{pattern} in a @code{vpath} directive matches the name of the
1761 dependency file, then the @var{directories} in that directive are searched
1762 just like (and before) the directories in the @code{VPATH} variable.  
1764 For example,
1766 @example
1767 vpath %.h ../headers
1768 @end example
1770 @noindent
1771 tells @code{make} to look for any dependency whose name ends in @file{.h}
1772 in the directory @file{../headers} if the file is not found in the current
1773 directory.
1775 If several @code{vpath} patterns match the dependency file's name, then
1776 @code{make} processes each matching @code{vpath} directive one by one,
1777 searching all the directories mentioned in each directive.  @code{make}
1778 handles multiple @code{vpath} directives in the order in which they
1779 appear in the makefile; multiple directives with the same pattern are
1780 independent of each other.
1782 @need 750
1783 Thus, 
1785 @example
1786 @group
1787 vpath %.c foo
1788 vpath %   blish
1789 vpath %.c bar
1790 @end group
1791 @end example
1793 @noindent
1794 will look for a file ending in @samp{.c} in @file{foo}, then
1795 @file{blish}, then @file{bar}, while
1797 @example
1798 @group
1799 vpath %.c foo:bar
1800 vpath %   blish
1801 @end group
1802 @end example
1804 @noindent
1805 will look for a file ending in @samp{.c} in @file{foo}, then
1806 @file{bar}, then @file{blish}.
1808 @node Commands/Search, Implicit/Search, Selective Search, Directory Search
1809 @subsection Writing Shell Commands with Directory Search
1810 @cindex shell command, and directory search
1811 @cindex directory search (@code{VPATH}), and shell commands
1813 When a dependency is found in another directory through directory search,
1814 this cannot change the commands of the rule; they will execute as written.
1815 Therefore, you must write the commands with care so that they will look for
1816 the dependency in the directory where @code{make} finds it.
1818 This is done with the @dfn{automatic variables} such as @samp{$^}
1819 (@pxref{Automatic, ,Automatic Variables}).  
1820 For instance, the value of @samp{$^} is a
1821 list of all the dependencies of the rule, including the names of
1822 the directories in which they were found, and the value of
1823 @samp{$@@} is the target.  Thus:@refill
1825 @example
1826 foo.o : foo.c
1827         cc -c $(CFLAGS) $^ -o $@@
1828 @end example
1830 @noindent
1831 (The variable @code{CFLAGS} exists so you can specify flags for C
1832 compilation by implicit rules; we use it here for consistency so it will
1833 affect all C compilations uniformly;
1834 @pxref{Implicit Variables, ,Variables Used by Implicit Rules}.)
1836 Often the dependencies include header files as well, which you do not
1837 want to mention in the commands.  The automatic variable @samp{$<} is
1838 just the first dependency:
1840 @example
1841 VPATH = src:../headers
1842 foo.o : foo.c defs.h hack.h
1843         cc -c $(CFLAGS) $< -o $@@
1844 @end example
1846 @node Implicit/Search, Libraries/Search, Commands/Search, Directory Search
1847 @subsection Directory Search and Implicit Rules
1848 @cindex @code{VPATH}, and implicit rules
1849 @cindex directory search (@code{VPATH}), and implicit rules
1850 @cindex search path for dependencies (@code{VPATH}), and implicit rules
1851 @cindex implicit rule, and directory search
1852 @cindex implicit rule, and @code{VPATH}
1853 @cindex rule, implicit, and directory search
1854 @cindex rule, implicit, and @code{VPATH}
1856 The search through the directories specified in @code{VPATH} or with
1857 @code{vpath} also happens during consideration of implicit rules
1858 (@pxref{Implicit Rules, ,Using Implicit Rules}).
1860 For example, when a file @file{foo.o} has no explicit rule, @code{make}
1861 considers implicit rules, such as the built-in rule to compile
1862 @file{foo.c} if that file exists.  If such a file is lacking in the
1863 current directory, the appropriate directories are searched for it.  If
1864 @file{foo.c} exists (or is mentioned in the makefile) in any of the
1865 directories, the implicit rule for C compilation is applied.
1867 The commands of implicit rules normally use automatic variables as a
1868 matter of necessity; consequently they will use the file names found by
1869 directory search with no extra effort.
1871 @node Libraries/Search,  , Implicit/Search, Directory Search
1872 @subsection Directory Search for Link Libraries
1873 @cindex link libraries, and directory search
1874 @cindex libraries for linking, directory search
1875 @cindex directory search (@code{VPATH}), and link libraries 
1876 @cindex @code{VPATH}, and link libraries 
1877 @cindex search path for dependencies (@code{VPATH}), and link libraries 
1878 @cindex @code{-l} (library search)
1880 Directory search applies in a special way to libraries used with the
1881 linker.  This special feature comes into play when you write a dependency
1882 whose name is of the form @samp{-l@var{name}}.  (You can tell something
1883 strange is going on here because the dependency is normally the name of a
1884 file, and the @emph{file name} of the library looks like
1885 @file{lib@var{name}.a}, not like @samp{-l@var{name}}.)@refill
1887 When a dependency's name has the form @samp{-l@var{name}}, @code{make}
1888 handles it specially by searching for the file @file{lib@var{name}.a} in
1889 the current directory, in directories specified by matching @code{vpath}
1890 search paths and the @code{VPATH} search path, and then in the
1891 directories @file{/lib}, @file{/usr/lib}, and @file{@var{prefix}/lib}
1892 (normally @file{/usr/local/lib}).
1894 For example,
1896 @example
1897 @group
1898 foo : foo.c -lcurses
1899         cc $^ -o $@@
1900 @end group
1901 @end example
1903 @noindent
1904 would cause the command @samp{cc foo.c /usr/lib/libcurses.a -o foo} to
1905 be executed when @file{foo} is older than @file{foo.c} or than
1906 @file{/usr/lib/libcurses.a}.@refill
1908 @node Phony Targets, Force Targets, Directory Search, Rules
1909 @section Phony Targets
1910 @cindex phony targets
1911 @cindex targets, phony
1912 @cindex targets without a file
1914 A phony target is one that is not really the name of a file.  It is just a
1915 name for some commands to be executed when you make an explicit request.
1916 There are two reasons to use a phony target: to avoid a conflict with
1917 a file of the same name, and to improve performance.
1919 If you write a rule whose commands will not create the target file, the
1920 commands will be executed every time the target comes up for remaking.
1921 Here is an example:
1923 @example
1924 @group
1925 clean:
1926         rm *.o temp
1927 @end group
1928 @end example
1930 @noindent
1931 Because the @code{rm} command does not create a file named @file{clean},
1932 probably no such file will ever exist.  Therefore, the @code{rm} command
1933 will be executed every time you say @samp{make clean}.
1934 @cindex @code{rm} (shell command)
1936 @findex .PHONY
1937 The phony target will cease to work if anything ever does create a file
1938 named @file{clean} in this directory.  Since it has no dependencies, the
1939 file @file{clean} would inevitably be considered up to date, and its
1940 commands would not be executed.  To avoid this problem, you can explicitly
1941 declare the target to be phony, using the special target @code{.PHONY}
1942 (@pxref{Special Targets, ,Special Built-in Target Names}) as follows:
1944 @example
1945 .PHONY : clean
1946 @end example
1948 @noindent
1949 Once this is done, @samp{make clean} will run the commands regardless of
1950 whether there is a file named @file{clean}.
1952 Since it knows that phony targets do not name actual files that could be
1953 remade from other files, @code{make} skips the implicit rule search for
1954 phony targets (@pxref{Implicit Rules}).  This is why declaring a target
1955 phony is good for performance, even if you are not worried about the
1956 actual file existing.
1958 Thus, you first write the line that states that @code{clean} is a
1959 phony target, then you write the rule, like this:
1961 @example
1962 @group
1963 .PHONY: clean
1964 clean:
1965         rm *.o temp
1966 @end group
1967 @end example
1969 A phony target should not be a dependency of a real target file; if it
1970 is, its commands are run every time @code{make} goes to update that
1971 file.  As long as a phony target is never a dependency of a real
1972 target, the phony target commands will be executed only when the phony
1973 target is a specified goal (@pxref{Goals, ,Arguments to Specify the
1974 Goals}).
1976 Phony targets can have dependencies.  When one directory contains multiple
1977 programs, it is most convenient to describe all of the programs in one
1978 makefile @file{./Makefile}.  Since the target remade by default will be the
1979 first one in the makefile, it is common to make this a phony target named
1980 @samp{all} and give it, as dependencies, all the individual programs.  For
1981 example:
1983 @example
1984 all : prog1 prog2 prog3
1985 .PHONY : all
1987 prog1 : prog1.o utils.o
1988         cc -o prog1 prog1.o utils.o
1990 prog2 : prog2.o
1991         cc -o prog2 prog2.o
1993 prog3 : prog3.o sort.o utils.o
1994         cc -o prog3 prog3.o sort.o utils.o
1995 @end example
1997 @noindent
1998 Now you can say just @samp{make} to remake all three programs, or specify
1999 as arguments the ones to remake (as in @samp{make prog1 prog3}).
2001 When one phony target is a dependency of another, it serves as a subroutine
2002 of the other.  For example, here @samp{make cleanall} will delete the
2003 object files, the difference files, and the file @file{program}:
2005 @example
2006 .PHONY: cleanall cleanobj cleandiff
2008 cleanall : cleanobj cleandiff
2009         rm program
2011 cleanobj :
2012         rm *.o
2014 cleandiff :
2015         rm *.diff
2016 @end example
2018 @node Force Targets, Empty Targets, Phony Targets, Rules
2019 @section Rules without Commands or Dependencies
2020 @cindex force targets
2021 @cindex targets, force
2022 @cindex @code{FORCE}
2023 @cindex rule, no commands or dependencies
2025 If a rule has no dependencies or commands, and the target of the rule
2026 is a nonexistent file, then @code{make} imagines this target to have
2027 been updated whenever its rule is run.  This implies that all targets
2028 depending on this one will always have their commands run.
2030 An example will illustrate this:
2032 @example
2033 @group
2034 clean: FORCE
2035         rm $(objects)
2036 FORCE:
2037 @end group
2038 @end example
2040 Here the target @samp{FORCE} satisfies the special conditions, so the
2041 target @file{clean} that depends on it is forced to run its commands.
2042 There is nothing special about the name @samp{FORCE}, but that is one name
2043 commonly used this way.
2045 As you can see, using @samp{FORCE} this way has the same results as using
2046 @samp{.PHONY: clean}.  
2048 Using @samp{.PHONY} is more explicit and more efficient.  However,
2049 other versions of @code{make} do not support @samp{.PHONY}; thus
2050 @samp{FORCE} appears in many makefiles.  @xref{Phony Targets}.
2052 @node Empty Targets, Special Targets, Force Targets, Rules
2053 @section Empty Target Files to Record Events
2054 @cindex empty targets
2055 @cindex targets, empty
2056 @cindex recording events with empty targets
2058 The @dfn{empty target} is a variant of the phony target; it is used to hold
2059 commands for an action that you request explicitly from time to time.
2060 Unlike a phony target, this target file can really exist; but the file's
2061 contents do not matter, and usually are empty.
2063 The purpose of the empty target file is to record, with its
2064 last-modification time, when the rule's commands were last executed.  It
2065 does so because one of the commands is a @code{touch} command to update the
2066 target file.
2068 The empty target file must have some dependencies.  When you ask to remake
2069 the empty target, the commands are executed if any dependency is more
2070 recent than the target; in other words, if a dependency has changed since
2071 the last time you remade the target.  Here is an example:
2073 @example
2074 print: foo.c bar.c
2075         lpr -p $?
2076         touch print
2077 @end example
2078 @cindex @code{print} target
2079 @cindex @code{lpr} (shell command)
2080 @cindex @code{touch} (shell command)
2082 @noindent
2083 With this rule, @samp{make print} will execute the @code{lpr} command if
2084 either source file has changed since the last @samp{make print}.  The
2085 automatic variable @samp{$?} is used to print only those files that have
2086 changed (@pxref{Automatic, ,Automatic Variables}).
2088 @node Special Targets, Multiple Targets, Empty Targets, Rules
2089 @section Special Built-in Target Names
2090 @cindex special targets
2091 @cindex built-in special targets
2092 @cindex targets, built-in special
2094 Certain names have special meanings if they appear as targets.
2096 @table @code
2097 @findex .PHONY
2098 @item .PHONY
2100 The dependencies of the special target @code{.PHONY} are considered to
2101 be phony targets.  When it is time to consider such a target,
2102 @code{make} will run its commands unconditionally, regardless of
2103 whether a file with that name exists or what its last-modification
2104 time is.  @xref{Phony Targets, ,Phony Targets}.
2106 @findex .SUFFIXES
2107 @item .SUFFIXES
2109 The dependencies of the special target @code{.SUFFIXES} are the list
2110 of suffixes to be used in checking for suffix rules.  
2111 @xref{Suffix Rules, , Old-Fashioned Suffix Rules}.
2113 @findex .DEFAULT
2114 @item .DEFAULT
2116 The commands specified for @code{.DEFAULT} are used for any target for
2117 which no rules are found (either explicit rules or implicit rules).
2118 @xref{Last Resort}.  If @code{.DEFAULT} commands are specified, every
2119 file mentioned as a dependency, but not as a target in a rule, will have
2120 these commands executed on its behalf.  @xref{Search Algorithm,
2121 ,Implicit Rule Search Algorithm}.
2123 @findex .PRECIOUS
2124 @item .PRECIOUS
2125 @cindex precious targets
2126 @cindex preserving with @code{.PRECIOUS}
2128 The targets which @code{.PRECIOUS} depends on are given the following
2129 special treatment: if @code{make} is killed or interrupted during the
2130 execution of their commands, the target is not deleted.
2131 @xref{Interrupts, ,Interrupting or Killing @code{make}}.
2132 Also, if the target is an intermediate file, it will not be deleted
2133 after it is no longer needed, as is normally done.
2134 @xref{Chained Rules, ,Chains of Implicit Rules}.
2136 You can also list the target pattern of an implicit rule (such as
2137 @samp{%.o}) as a dependency file of the special target @code{.PRECIOUS}
2138 to preserve intermediate files created by rules whose target patterns
2139 match that file's name.
2141 @findex .IGNORE
2142 @item .IGNORE
2144 If you specify dependencies for @code{.IGNORE}, then @code{make} will
2145 ignore errors in execution of the commands run for those particular
2146 files.  The commands for @code{.IGNORE} are not meaningful.
2148 If mentioned as a target with no dependencies, @code{.IGNORE} says to
2149 ignore errors in execution of commands for all files.  This usage of
2150 @samp{.IGNORE} is supported only for historical compatibility.  Since
2151 this affects every command in the makefile, it is not very useful; we
2152 recommend you use the more selective ways to ignore errors in specific
2153 commands.  @xref{Errors, ,Errors in Commands}.
2155 @findex .SILENT
2156 @item .SILENT
2158 If you specify dependencies for @code{.SILENT}, then @code{make} will
2159 not the print commands to remake those particular files before executing
2160 them.  The commands for @code{.SILENT} are not meaningful.
2162 If mentioned as a target with no dependencies, @code{.SILENT} says not
2163 to print any commands before executing them.  This usage of
2164 @samp{.SILENT} is supported only for historical compatibility.  We
2165 recommend you use the more selective ways to silence specific commands.
2166 @xref{Echoing, ,Command Echoing}.  If you want to silence all commands
2167 for a particular run of @code{make}, use the @samp{-s} or
2168 @w{@samp{--silent}} option (@pxref{Options Summary}).
2170 @findex .EXPORT_ALL_VARIABLES
2171 @item .EXPORT_ALL_VARIABLES
2173 Simply by being mentioned as a target, this tells @code{make} to
2174 export all variables to child processes by default.
2175 @xref{Variables/Recursion, ,Communicating Variables to a
2176 Sub-@code{make}}.
2177 @end table
2179 Any defined implicit rule suffix also counts as a special target if it
2180 appears as a target, and so does the concatenation of two suffixes, such
2181 as @samp{.c.o}.  These targets are suffix rules, an obsolete way of
2182 defining implicit rules (but a way still widely used).  In principle, any
2183 target name could be special in this way if you break it in two and add
2184 both pieces to the suffix list.  In practice, suffixes normally begin with
2185 @samp{.}, so these special target names also begin with @samp{.}.
2186 @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
2188 @node Multiple Targets, Multiple Rules, Special Targets, Rules
2189 @section Multiple Targets in a Rule
2190 @cindex multiple targets
2191 @cindex several targets in a rule
2192 @cindex targets, multiple
2193 @cindex rule, with multiple targets
2195 A rule with multiple targets is equivalent to writing many rules, each with
2196 one target, and all identical aside from that.  The same commands apply to
2197 all the targets, but their effects may vary because you can substitute the
2198 actual target name into the command using @samp{$@@}.  The rule contributes
2199 the same dependencies to all the targets also.
2201 This is useful in two cases.
2203 @itemize @bullet
2204 @item
2205 You want just dependencies, no commands.  For example:
2207 @example
2208 kbd.o command.o files.o: command.h
2209 @end example
2211 @noindent
2212 gives an additional dependency to each of the three object files
2213 mentioned.
2215 @item
2216 Similar commands work for all the targets.  The commands do not need
2217 to be absolutely identical, since the automatic variable @samp{$@@}
2218 can be used to substitute the particular target to be remade into the
2219 commands (@pxref{Automatic, ,Automatic Variables}).  For example:
2221 @example
2222 @group
2223 bigoutput littleoutput : text.g
2224         generate text.g -$(subst output,,$@@) > $@@
2225 @end group
2226 @end example
2227 @findex subst
2229 @noindent
2230 is equivalent to
2232 @example
2233 bigoutput : text.g
2234         generate text.g -big > bigoutput
2235 littleoutput : text.g
2236         generate text.g -little > littleoutput
2237 @end example
2239 @noindent
2240 Here we assume the hypothetical program @code{generate} makes two
2241 types of output, one if given @samp{-big} and one if given
2242 @samp{-little}.
2243 @xref{Text Functions, ,Functions for String Substitution and Analysis},
2244 for an explanation of the @code{subst} function.
2245 @end itemize
2247 Suppose you would like to vary the dependencies according to the target,
2248 much as the variable @samp{$@@} allows you to vary the commands.
2249 You cannot do this with multiple targets in an ordinary rule, but you can
2250 do it with a @dfn{static pattern rule}.
2251 @xref{Static Pattern, ,Static Pattern Rules}.
2253 @node Multiple Rules, Static Pattern, Multiple Targets, Rules
2254 @section Multiple Rules for One Target
2255 @cindex multiple rules for one target
2256 @cindex several rules for one target
2257 @cindex rule, multiple for one target
2258 @cindex target, multiple rules for one
2260 One file can be the target of several rules.  All the dependencies
2261 mentioned in all the rules are merged into one list of dependencies for
2262 the target.  If the target is older than any dependency from any rule,
2263 the commands are executed.
2265 There can only be one set of commands to be executed for a file.
2266 If more than one rule gives commands for the same file, 
2267 @code{make} uses the last set given and prints an error message.
2268 (As a special case, if the file's name begins with a dot, no
2269 error message is printed.  This odd behavior is only for
2270 compatibility with other implementations of @code{make}.)  
2271 There is no reason to
2272 write your makefiles this way; that is why @code{make} gives you
2273 an error message.@refill
2275 An extra rule with just dependencies can be used to give a few extra
2276 dependencies to many files at once.  For example, one usually has a
2277 variable named @code{objects} containing a list of all the compiler output
2278 files in the system being made.  An easy way to say that all of them must
2279 be recompiled if @file{config.h} changes is to write the following:
2281 @example
2282 objects = foo.o bar.o
2283 foo.o : defs.h
2284 bar.o : defs.h test.h
2285 $(objects) : config.h
2286 @end example
2288 This could be inserted or taken out without changing the rules that really
2289 specify how to make the object files, making it a convenient form to use if
2290 you wish to add the additional dependency intermittently.
2292 Another wrinkle is that the additional dependencies could be specified with
2293 a variable that you set with a command argument to @code{make}
2294 (@pxref{Overriding, ,Overriding Variables}).  For example,
2296 @example
2297 @group
2298 extradeps=
2299 $(objects) : $(extradeps)
2300 @end group
2301 @end example
2303 @noindent
2304 means that the command @samp{make extradeps=foo.h} will consider
2305 @file{foo.h} as a dependency of each object file, but plain @samp{make}
2306 will not.
2308 If none of the explicit rules for a target has commands, then @code{make}
2309 searches for an applicable implicit rule to find some commands
2310 @pxref{Implicit Rules, ,Using Implicit Rules}).
2312 @node Static Pattern, Double-Colon, Multiple Rules, Rules
2313 @section Static Pattern Rules
2314 @cindex static pattern rule
2315 @cindex rule, static pattern
2316 @cindex pattern rules, static (not implicit)
2317 @cindex varying dependencies
2318 @cindex dependencies, varying (static pattern)
2320 @dfn{Static pattern rules} are rules which specify multiple targets and
2321 construct the dependency names for each target based on the target name.
2322 They are more general than ordinary rules with multiple targets because the
2323 targets do not have to have identical dependencies.  Their dependencies must
2324 be @emph{analogous}, but not necessarily @emph{identical}.
2326 @menu
2327 * Static Usage::                The syntax of static pattern rules.
2328 * Static versus Implicit::      When are they better than implicit rules?
2329 @end menu
2331 @node Static Usage, Static versus Implicit,  , Static Pattern
2332 @subsection Syntax of Static Pattern Rules
2333 @cindex static pattern rule, syntax of
2334 @cindex pattern rules, static, syntax of
2336 Here is the syntax of a static pattern rule:
2338 @example
2339 @var{targets} @dots{}: @var{target-pattern}: @var{dep-patterns} @dots{}
2340         @var{commands}
2341         @dots{}
2342 @end example
2344 @noindent
2345 The @var{targets} list specifies the targets that the rule applies to.
2346 The targets can contain wildcard characters, just like the targets of
2347 ordinary rules (@pxref{Wildcards, ,Using Wildcard Characters in File
2348 Names}).
2350 @cindex target pattern, static (not implicit)
2351 @cindex stem
2352 The @var{target-pattern} and @var{dep-patterns} say how to compute the
2353 dependencies of each target.  Each target is matched against the
2354 @var{target-pattern} to extract a part of the target name, called the
2355 @dfn{stem}.  This stem is substituted into each of the @var{dep-patterns}
2356 to make the dependency names (one from each @var{dep-pattern}).
2358 Each pattern normally contains the character @samp{%} just once.  When the
2359 @var{target-pattern} matches a target, the @samp{%} can match any part of
2360 the target name; this part is called the @dfn{stem}.  The rest of the
2361 pattern must match exactly.  For example, the target @file{foo.o} matches
2362 the pattern @samp{%.o}, with @samp{foo} as the stem.  The targets
2363 @file{foo.c} and @file{foo.out} do not match that pattern.@refill
2365 @cindex dependency pattern, static (not implicit)
2366 The dependency names for each target are made by substituting the stem
2367 for the @samp{%} in each dependency pattern.  For example, if one
2368 dependency pattern is @file{%.c}, then substitution of the stem
2369 @samp{foo} gives the dependency name @file{foo.c}.  It is legitimate
2370 to write a dependency pattern that does not contain @samp{%}; then this
2371 dependency is the same for all targets.
2373 @cindex @code{%}, quoting in static pattern
2374 @cindex @code{%}, quoting with @code{\} (backslash)
2375 @cindex @code{\} (backslash), to quote @code{%}
2376 @cindex backslash (@code{\}), to quote @code{%}
2377 @cindex quoting @code{%}, in static pattern
2378 @samp{%} characters in pattern rules can be quoted with preceding
2379 backslashes (@samp{\}).  Backslashes that would otherwise quote @samp{%}
2380 characters can be quoted with more backslashes.  Backslashes that quote
2381 @samp{%} characters or other backslashes are removed from the pattern
2382 before it is compared to file names or has a stem substituted into it.
2383 Backslashes that are not in danger of quoting @samp{%} characters go
2384 unmolested.  For example, the pattern @file{the\%weird\\%pattern\\} has
2385 @samp{the%weird\} preceding the operative @samp{%} character, and
2386 @samp{pattern\\} following it.  The final two backslashes are left alone
2387 because they cannot affect any @samp{%} character.@refill
2389 Here is an example, which compiles each of @file{foo.o} and @file{bar.o}
2390 from the corresponding @file{.c} file:
2392 @example
2393 @group
2394 objects = foo.o bar.o
2396 $(objects): %.o: %.c
2397         $(CC) -c $(CFLAGS) $< -o $@@
2398 @end group
2399 @end example
2401 @noindent
2402 Here @samp{$<} is the automatic variable that holds the name of the
2403 dependency and @samp{$@@} is the automatic variable that holds the name
2404 of the target; see @ref{Automatic, , Automatic Variables}.
2406 Each target specified must match the target pattern; a warning is issued
2407 for each target that does not.  If you have a list of files, only some of
2408 which will match the pattern, you can use the @code{filter} function to
2409 remove nonmatching file names (@pxref{Text Functions, ,Functions for String Substitution and Analysis}):
2411 @example
2412 files = foo.elc bar.o lose.o
2414 $(filter %.o,$(files)): %.o: %.c
2415         $(CC) -c $(CFLAGS) $< -o $@@
2416 $(filter %.elc,$(files)): %.elc: %.el
2417         emacs -f batch-byte-compile $<
2418 @end example
2420 @noindent
2421 In this example the result of @samp{$(filter %.o,$(files))} is
2422 @file{bar.o lose.o}, and the first static pattern rule causes each of
2423 these object files to be updated by compiling the corresponding C source
2424 file.  The result of @w{@samp{$(filter %.elc,$(files))}} is
2425 @file{foo.elc}, so that file is made from @file{foo.el}.@refill
2427 Another example shows how to use @code{$*} in static pattern rules:
2428 @vindex $*@r{, and static pattern}
2430 @example
2431 @group
2432 bigoutput littleoutput : %output : text.g
2433         generate text.g -$* > $@@
2434 @end group
2435 @end example
2437 @noindent
2438 When the @code{generate} command is run, @code{$*} will expand to the
2439 stem, either @samp{big} or @samp{little}.
2441 @node Static versus Implicit,  , Static Usage, Static Pattern
2442 @subsection Static Pattern Rules versus Implicit Rules
2443 @cindex rule, static pattern versus implicit
2444 @cindex static pattern rule, versus implicit
2446 A static pattern rule has much in common with an implicit rule defined as a
2447 pattern rule (@pxref{Pattern Rules, ,Defining and Redefining Pattern Rules}).
2448 Both have a pattern for the target and patterns for constructing the
2449 names of dependencies.  The difference is in how @code{make} decides
2450 @emph{when} the rule applies.
2452 An implicit rule @emph{can} apply to any target that matches its pattern,
2453 but it @emph{does} apply only when the target has no commands otherwise
2454 specified, and only when the dependencies can be found.  If more than one
2455 implicit rule appears applicable, only one applies; the choice depends on
2456 the order of rules.
2458 By contrast, a static pattern rule applies to the precise list of targets
2459 that you specify in the rule.  It cannot apply to any other target and it
2460 invariably does apply to each of the targets specified.  If two conflicting
2461 rules apply, and both have commands, that's an error.
2463 The static pattern rule can be better than an implicit rule for these
2464 reasons:
2466 @itemize @bullet
2467 @item
2468 You may wish to override the usual implicit rule for a few
2469 files whose names cannot be categorized syntactically but
2470 can be given in an explicit list.
2472 @item
2473 If you cannot be sure of the precise contents of the directories
2474 you are using, you may not be sure which other irrelevant files
2475 might lead @code{make} to use the wrong implicit rule.  The choice
2476 might depend on the order in which the implicit rule search is done.
2477 With static pattern rules, there is no uncertainty: each rule applies
2478 to precisely the targets specified.
2479 @end itemize
2481 @node Double-Colon, Automatic Dependencies, Static Pattern, Rules
2482 @section Double-Colon Rules
2483 @cindex double-colon rules
2484 @cindex rule, double-colon (@code{::})
2485 @cindex multiple rules for one target (@code{::})
2486 @cindex @code{::} rules (double-colon)
2488 @dfn{Double-colon} rules are rules written with @samp{::} instead of
2489 @samp{:} after the target names.  They are handled differently from
2490 ordinary rules when the same target appears in more than one rule.
2492 When a target appears in multiple rules, all the rules must be the same
2493 type: all ordinary, or all double-colon.  If they are double-colon, each of
2494 them is independent of the others.  Each double-colon rule's commands are
2495 executed if the target is older than any dependencies of that rule.  This
2496 can result in executing none, any, or all of the double-colon rules.
2498 Double-colon rules with the same target are in fact completely separate
2499 from one another.  Each double-colon rule is processed individually, just
2500 as rules with different targets are processed.
2502 The double-colon rules for a target are executed in the order they appear
2503 in the makefile.  However, the cases where double-colon rules really make
2504 sense are those where the order of executing the commands would not matter.
2506 Double-colon rules are somewhat obscure and not often very useful; they
2507 provide a mechanism for cases in which the method used to update a target
2508 differs depending on which dependency files caused the update, and such
2509 cases are rare.
2511 Each double-colon rule should specify commands; if it does not, an
2512 implicit rule will be used if one applies.  
2513 @xref{Implicit Rules, ,Using Implicit Rules}.
2515 @node Automatic Dependencies,  , Double-Colon, Rules
2516 @section Generating Dependencies Automatically
2517 @cindex dependencies, automatic generation
2518 @cindex automatic generation of dependencies
2519 @cindex generating dependencies automatically
2521 In the makefile for a program, many of the rules you need to write often
2522 say only that some object file depends on some header
2523 file.  For example, if @file{main.c} uses @file{defs.h} via an
2524 @code{#include}, you would write:
2526 @example
2527 main.o: defs.h
2528 @end example
2530 @noindent
2531 You need this rule so that @code{make} knows that it must remake
2532 @file{main.o} whenever @file{defs.h} changes.  You can see that for a
2533 large program you would have to write dozens of such rules in your
2534 makefile.  And, you must always be very careful to update the makefile
2535 every time you add or remove an @code{#include}.
2536 @cindex @code{#include}
2538 @cindex @code{-M} (to compiler)
2539 To avoid this hassle, most modern C compilers can write these rules for
2540 you, by looking at the @code{#include} lines in the source files.
2541 Usually this is done with the @samp{-M} option to the compiler.
2542 For example, the command:
2544 @example
2545 cc -M main.c
2546 @end example
2548 @noindent
2549 generates the output:
2551 @example
2552 main.o : main.c defs.h
2553 @end example
2555 @noindent
2556 Thus you no longer have to write all those rules yourself.
2557 The compiler will do it for you.
2559 Note that such a dependency constitutes mentioning @file{main.o} in a
2560 makefile, so it can never be considered an intermediate file by implicit
2561 rule search.  This means that @code{make} won't ever remove the file
2562 after using it; @pxref{Chained Rules, ,Chains of Implicit Rules}.
2564 @cindex @code{make depend}
2565 With old @code{make} programs, it was traditional practice to use this
2566 compiler feature to generate dependencies on demand with a command like
2567 @samp{make depend}.  That command would create a file @file{depend}
2568 containing all the automatically-generated dependencies; then the
2569 makefile could use @code{include} to read them in (@pxref{Include}).
2571 In GNU @code{make}, the feature of remaking makefiles makes this
2572 practice obsolete---you need never tell @code{make} explicitly to
2573 regenerate the dependencies, because it always regenerates any makefile
2574 that is out of date.  @xref{Remaking Makefiles}.
2576 The practice we recommend for automatic dependency generation is to have
2577 one makefile corresponding to each source file.  For each source file
2578 @file{@var{name}.c} there is a makefile @file{@var{name}.d} which lists
2579 what files the object file @file{@var{name}.o} depends on.  That way
2580 only the source files that have changed need to be rescanned to produce
2581 the new dependencies.
2583 Here is the pattern rule to generate a file of dependencies (i.e., a makefile)
2584 called @file{@var{name}.d} from a C source file called @file{@var{name}.c}:
2586 @example
2587 @group
2588 %.d: %.c
2589         $(SHELL) -ec '$(CC) -M $(CPPFLAGS) $< \
2590                       | sed '\''s/$*\\.o[ :]*/& $@@/g'\'' > $@@'
2591 @end group
2592 @end example
2594 @noindent
2595 @xref{Pattern Rules}, for information on defining pattern rules.  The
2596 @samp{-e} flag to the shell makes it exit immediately if the
2597 @code{$(CC)} command fails (exits with a nonzero status).  Normally the
2598 shell exits with the status of the last command in the pipeline
2599 (@code{sed} in this case), so @code{make} would not notice a nonzero
2600 status from the compiler.
2601 @cindex @code{-e} (shell flag)
2603 @cindex @code{-MM} (to GNU compiler)
2604 With the GNU C compiler, you may wish to use the @samp{-MM} flag instead
2605 of @samp{-M}.  This omits dependencies on system header files.
2606 @xref{Preprocessor Options, , Options Controlling the Preprocessor, 
2607 gcc.info, Using GNU CC}, for details.
2609 @cindex @code{sed} (shell command)
2610 The purpose of the @code{sed} command is to translate (for example):
2612 @example
2613 main.o : main.c defs.h
2614 @end example
2616 @noindent
2617 into:
2619 @example
2620 main.o main.d : main.c defs.h
2621 @end example
2623 @noindent
2624 @cindex @code{.d}
2625 This makes each @samp{.d} file depend on all the source and header files
2626 that the corresponding @samp{.o} file depends on.  @code{make} then
2627 knows it must regenerate the dependencies whenever any of the source or
2628 header files changes.
2630 Once you've defined the rule to remake the @samp{.d} files,
2631 you then use the @code{include} directive to read them all in.
2632 @xref{Include}.  For example:
2634 @example
2635 @group
2636 sources = foo.c bar.c
2638 include $(sources:.c=.d)
2639 @end group
2640 @end example
2642 @noindent
2643 (This example uses a substitution variable reference to translate the
2644 list of source files @samp{foo.c bar.c} into a list of dependency
2645 makefiles, @samp{foo.d bar.d}.  @xref{Substitution Refs}, for full
2646 information on substitution references.)  Since the @samp{.d} files are
2647 makefiles like any others, @code{make} will remake them as necessary
2648 with no further work from you.  @xref{Remaking Makefiles}.
2650 @node Commands, Using Variables, Rules, Top
2651 @chapter Writing the Commands in Rules
2652 @cindex commands, how to write
2653 @cindex rule commands
2654 @cindex writing rule commands
2656 The commands of a rule consist of shell command lines to be executed one
2657 by one.  Each command line must start with a tab, except that the first
2658 command line may be attached to the target-and-dependencies line with a
2659 semicolon in between.  Blank lines and lines of just comments may appear
2660 among the command lines; they are ignored.  (But beware, an apparently
2661 ``blank'' line that begins with a tab is @emph{not} blank!  It is an
2662 empty command; @pxref{Empty Commands}.)
2664 Users use many different shell programs, but commands in makefiles are
2665 always interpreted by @file{/bin/sh} unless the makefile specifies
2666 otherwise.  @xref{Execution, ,Command Execution}.
2668 @cindex comments, in commands
2669 @cindex commands, comments in
2670 @cindex @code{#} (comments), in commands
2671 The shell that is in use determines whether comments can be written on
2672 command lines, and what syntax they use.  When the shell is
2673 @file{/bin/sh}, a @samp{#} starts a comment that extends to the end of
2674 the line.  The @samp{#} does not have to be at the beginning of a line.
2675 Text on a line before a @samp{#} is not part of the comment.
2677 @menu
2678 * Echoing::                     How to control when commands are echoed.
2679 * Execution::                   How commands are executed.
2680 * Parallel::                    How commands can be executed in parallel.
2681 * Errors::                      What happens after a command execution error. 
2682 * Interrupts::                  What happens when a command is interrupted.
2683 * Recursion::                   Invoking @code{make} from makefiles.
2684 * Sequences::                   Defining canned sequences of commands.
2685 * Empty Commands::              Defining useful, do-nothing commands.
2686 @end menu
2688 @node Echoing, Execution,  , Commands
2689 @section Command Echoing
2690 @cindex echoing of commands
2691 @cindex silent operation
2692 @cindex @code{@@} (in commands)
2693 @cindex commands, echoing
2694 @cindex printing of commands
2696 Normally @code{make} prints each command line before it is executed.
2697 We call this @dfn{echoing} because it gives the appearance that you
2698 are typing the commands yourself.
2700 When a line starts with @samp{@@}, the echoing of that line is suppressed.
2701 The @samp{@@} is discarded before the command is passed to the shell.
2702 Typically you would use this for a command whose only effect is to print
2703 something, such as an @code{echo} command to indicate progress through
2704 the makefile:
2706 @example
2707 @@echo About to make distribution files
2708 @end example
2710 @cindex @code{-n}
2711 @cindex @code{--just-print}
2712 @cindex @code{--dry-run}
2713 @cindex @code{--recon}
2714 When @code{make} is given the flag @samp{-n} or @samp{--just-print},
2715 echoing is all that happens, no execution.  @xref{Options Summary,
2716 ,Summary of Options}.  In this case and only this case, even the
2717 commands starting with @samp{@@} are printed.  This flag is useful for
2718 finding out which commands @code{make} thinks are necessary without
2719 actually doing them.
2721 @cindex @code{-s}
2722 @cindex @code{--silent}
2723 @cindex @code{--quiet}
2724 @findex .SILENT
2725 The @samp{-s} or @samp{--silent}
2726 flag to @code{make} prevents all echoing, as if all commands
2727 started with @samp{@@}.  A rule in the makefile for the special target
2728 @code{.SILENT} without dependencies has the same effect 
2729 (@pxref{Special Targets, ,Special Built-in Target Names}).
2730 @code{.SILENT} is essentially obsolete since @samp{@@} is more flexible.@refill
2732 @node Execution, Parallel, Echoing, Commands
2733 @section Command Execution
2734 @cindex commands, execution
2735 @cindex execution, of commands
2736 @cindex shell command, execution
2737 @vindex SHELL @r{(command execution)}
2739 When it is time to execute commands to update a target, they are executed
2740 by making a new subshell for each line.  (In practice, @code{make} may
2741 take shortcuts that do not affect the results.)
2743 @cindex @code{cd} (shell command)
2744 @strong{Please note:} this implies that shell commands such as
2745 @code{cd} that set variables local to each process will not affect the
2746 following command lines.  If you want to use @code{cd} to affect the
2747 next command, put the two on a single line with a semicolon between
2748 them.  Then @code{make} will consider them a single command and pass
2749 them, together, to a shell which will execute them in sequence.  For
2750 example:
2752 @example
2753 foo : bar/lose
2754         cd bar; gobble lose > ../foo
2755 @end example
2757 @cindex commands, backslash (@code{\}) in
2758 @cindex commands, quoting newlines in
2759 @cindex backslash (@code{\}), in commands
2760 @cindex @code{\} (backslash), in commands
2761 @cindex quoting newline, in commands
2762 @cindex newline, quoting, in commands
2763 If you would like to split a single shell command into multiple lines of
2764 text, you must use a backslash at the end of all but the last subline.
2765 Such a sequence of lines is combined into a single line, by deleting the
2766 backslash-newline sequences, before passing it to the shell.  Thus, the
2767 following is equivalent to the preceding example:
2769 @example
2770 @group
2771 foo : bar/lose
2772         cd bar;  \
2773         gobble lose > ../foo
2774 @end group
2775 @end example
2777 @vindex SHELL
2778 The program used as the shell is taken from the variable @code{SHELL}.
2779 By default, the program @file{/bin/sh} is used.
2781 @cindex environment, @code{SHELL} in
2782 Unlike most variables, the variable @code{SHELL} is never set from the
2783 environment.  This is because the @code{SHELL} environment variable is
2784 used to specify your personal choice of shell program for interactive
2785 use.  It would be very bad for personal choices like this to affect
2786 the functioning of makefiles.  @xref{Environment, ,Variables from the
2787 Environment}.
2789 @node Parallel, Errors, Execution, Commands
2790 @section Parallel Execution
2791 @cindex commands, execution in parallel
2792 @cindex parallel execution
2793 @cindex execution, in parallel
2794 @cindex job slots
2795 @cindex @code{-j}
2796 @cindex @code{--jobs}
2798 GNU @code{make} knows how to execute several commands at once.
2799 Normally, @code{make} will execute only one command at a time, waiting
2800 for it to finish before executing the next.  However, the @samp{-j} or
2801 @samp{--jobs} option tells @code{make} to execute many commands
2802 simultaneously.@refill
2804 If the @samp{-j} option is followed by an integer, this is the number of
2805 commands to execute at once; this is called the number of @dfn{job slots}.
2806 If there is nothing looking like an integer after the @samp{-j} option,
2807 there is no limit on the number of job slots.  The default number of job
2808 slots is one, which means serial execution (one thing at a time).
2810 One unpleasant consequence of running several commands simultaneously is
2811 that output from all of the commands comes when the commands send it, so
2812 messages from different commands may be interspersed.
2814 Another problem is that two processes cannot both take input from the
2815 same device; so to make sure that only one command tries to take input
2816 from the terminal at once, @code{make} will invalidate the standard
2817 input streams of all but one running command.  This means that
2818 attempting to read from standard input will usually be a fatal error (a
2819 @samp{Broken pipe} signal) for most child processes if there are
2820 several.
2821 @cindex broken pipe
2822 @cindex standard input
2824 It is unpredictable which command will have a valid standard input stream
2825 (which will come from the terminal, or wherever you redirect the standard
2826 input of @code{make}).  The first command run will always get it first, and
2827 the first command started after that one finishes will get it next, and so
2830 We will change how this aspect of @code{make} works if we find a better
2831 alternative.  In the mean time, you should not rely on any command using
2832 standard input at all if you are using the parallel execution feature; but
2833 if you are not using this feature, then standard input works normally in
2834 all commands.
2836 If a command fails (is killed by a signal or exits with a nonzero
2837 status), and errors are not ignored for that command
2838 (@pxref{Errors, ,Errors in Commands}),
2839 the remaining command lines to remake the same target will not be run.
2840 If a command fails and the @samp{-k} or @samp{--keep-going}
2841 option was not given
2842 (@pxref{Options Summary, ,Summary of Options}), 
2843 @code{make} aborts execution.  If make
2844 terminates for any reason (including a signal) with child processes
2845 running, it waits for them to finish before actually exiting.@refill
2847 @cindex load average
2848 @cindex limiting jobs based on load
2849 @cindex jobs, limiting based on load
2850 @cindex @code{-l} (load average)
2851 @cindex @code{--max-load}
2852 @cindex @code{--load-average}
2853 When the system is heavily loaded, you will probably want to run fewer jobs
2854 than when it is lightly loaded.  You can use the @samp{-l} option to tell
2855 @code{make} to limit the number of jobs to run at once, based on the load
2856 average.  The @samp{-l} or @samp{--max-load}
2857 option is followed by a floating-point number.  For
2858 example,
2860 @example
2861 -l 2.5
2862 @end example
2864 @noindent
2865 will not let @code{make} start more than one job if the load average is
2866 above 2.5.  The @samp{-l} option with no following number removes the
2867 load limit, if one was given with a previous @samp{-l} option.@refill
2869 More precisely, when @code{make} goes to start up a job, and it already has
2870 at least one job running, it checks the current load average; if it is not
2871 lower than the limit given with @samp{-l}, @code{make} waits until the load
2872 average goes below that limit, or until all the other jobs finish.
2874 By default, there is no load limit.
2876 @node Errors, Interrupts, Parallel, Commands
2877 @section Errors in Commands
2878 @cindex errors (in commands)
2879 @cindex commands, errors in
2880 @cindex exit status (errors)
2882 After each shell command returns, @code{make} looks at its exit status.
2883 If the command completed successfully, the next command line is executed
2884 in a new shell; after the last command line is finished, the rule is
2885 finished. 
2887 If there is an error (the exit status is nonzero), @code{make} gives up on
2888 the current rule, and perhaps on all rules.
2890 Sometimes the failure of a certain command does not indicate a problem.
2891 For example, you may use the @code{mkdir} command to ensure that a
2892 directory exists.  If the directory already exists, @code{mkdir} will
2893 report an error, but you probably want @code{make} to continue regardless.
2895 @cindex @code{-} (in commands)
2896 To ignore errors in a command line, write a @samp{-} at the beginning of
2897 the line's text (after the initial tab).  The @samp{-} is discarded before
2898 the command is passed to the shell for execution.  
2900 For example,
2902 @example
2903 @group
2904 clean:
2905         -rm -f *.o
2906 @end group
2907 @end example
2908 @cindex @code{rm} (shell command)
2910 @noindent
2911 This causes @code{rm} to continue even if it is unable to remove a file.
2913 @cindex @code{-i}
2914 @cindex @code{--ignore-errors}
2915 @findex .IGNORE
2916 When you run @code{make} with the @samp{-i} or @samp{--ignore-errors}
2917 flag, errors are ignored in all commands of all rules.  A rule in the
2918 makefile for the special target @code{.IGNORE} has the same effect, if
2919 there are no dependencies.  These ways of ignoring errors are obsolete
2920 because @samp{-} is more flexible.
2922 When errors are to be ignored, because of either a @samp{-} or the
2923 @samp{-i} flag, @code{make} treats an error return just like success,
2924 except that it prints out a message that tells you the status code
2925 the command exited with, and says that the error has been ignored.
2927 When an error happens that @code{make} has not been told to ignore,
2928 it implies that the current target cannot be correctly remade, and neither
2929 can any other that depends on it either directly or indirectly.  No further
2930 commands will be executed for these targets, since their preconditions
2931 have not been achieved.
2934 @cindex @code{-k}
2935 @cindex @code{--keep-going}
2936 Normally @code{make} gives up immediately in this circumstance, returning a
2937 nonzero status.  However, if the @samp{-k} or @samp{--keep-going}
2938 flag is specified, @code{make}
2939 continues to consider the other dependencies of the pending targets,
2940 remaking them if necessary, before it gives up and returns nonzero status.
2941 For example, after an error in compiling one object file, @samp{make -k}
2942 will continue compiling other object files even though it already knows
2943 that linking them will be impossible.  @xref{Options Summary, ,Summary of Options}.
2945 The usual behavior assumes that your purpose is to get the specified
2946 targets up to date; once @code{make} learns that this is impossible, it
2947 might as well report the failure immediately.  The @samp{-k} option says
2948 that the real purpose is to test as many of the changes made in the
2949 program as possible, perhaps to find several independent problems so
2950 that you can correct them all before the next attempt to compile.  This
2951 is why Emacs' @code{compile} command passes the @samp{-k} flag by
2952 default.
2953 @cindex Emacs (@code{M-x compile})
2955 @findex .DELETE_ON_ERROR
2956 @cindex deletion of target files
2957 @cindex removal of target files
2958 @cindex target, deleting on error
2959 Usually when a command fails, if it has changed the target file at all,
2960 the file is corrupted and cannot be used---or at least it is not
2961 completely updated.  Yet the file's timestamp says that it is now up to
2962 date, so the next time @code{make} runs, it will not try to update that
2963 file.  The situation is just the same as when the command is killed by a
2964 signal; @pxref{Interrupts}.  So generally the right thing to do is to
2965 delete the target file if the command fails after beginning to change
2966 the file.  @code{make} will do this if @code{.DELETE_ON_ERROR} appears
2967 as a target.  This is almost always what you want @code{make} to do, but
2968 it is not historical practice; so for compatibility, you must explicitly
2969 request it.
2971 @node Interrupts, Recursion, Errors, Commands
2972 @section Interrupting or Killing @code{make}
2973 @cindex interrupt
2974 @cindex signal
2975 @cindex deletion of target files
2976 @cindex removal of target files
2977 @cindex target, deleting on interrupt
2978 @cindex killing (interruption)
2980 If @code{make} gets a fatal signal while a command is executing, it may
2981 delete the target file that the command was supposed to update.  This is
2982 done if the target file's last-modification time has changed since
2983 @code{make} first checked it.
2985 The purpose of deleting the target is to make sure that it is remade from
2986 scratch when @code{make} is next run.  Why is this?  Suppose you type
2987 @kbd{Ctrl-c} while a compiler is running, and it has begun to write an
2988 object file @file{foo.o}.  The @kbd{Ctrl-c} kills the compiler, resulting
2989 in an incomplete file whose last-modification time is newer than the source
2990 file @file{foo.c}.  But @code{make} also receives the @kbd{Ctrl-c} signal
2991 and deletes this incomplete file.  If @code{make} did not do this, the next
2992 invocation of @code{make} would think that @file{foo.o} did not require
2993 updating---resulting in a strange error message from the linker when it
2994 tries to link an object file half of which is missing.
2996 @findex .PRECIOUS
2997 You can prevent the deletion of a target file in this way by making the
2998 special target @code{.PRECIOUS} depend on it.  Before remaking a target,
2999 @code{make} checks to see whether it appears on the dependencies of
3000 @code{.PRECIOUS}, and thereby decides whether the target should be deleted
3001 if a signal happens.  Some reasons why you might do this are that the
3002 target is updated in some atomic fashion, or exists only to record a
3003 modification-time (its contents do not matter), or must exist at all
3004 times to prevent other sorts of trouble.
3006 @node Recursion, Sequences, Interrupts, Commands
3007 @section Recursive Use of @code{make}
3008 @cindex recursion
3009 @cindex subdirectories, recursion for
3011 Recursive use of @code{make} means using @code{make} as a command in a
3012 makefile.  This technique is useful when you want separate makefiles for
3013 various subsystems that compose a larger system.  For example, suppose you
3014 have a subdirectory @file{subdir} which has its own makefile, and you would
3015 like the containing directory's makefile to run @code{make} on the
3016 subdirectory.  You can do it by writing this:
3018 @example
3019 subsystem:
3020         cd subdir; $(MAKE)
3021 @end example
3023 @noindent
3024 or, equivalently, this (@pxref{Options Summary, ,Summary of Options}):
3026 @example
3027 subsystem:
3028         $(MAKE) -C subdir
3029 @end example
3030 @cindex @code{-C}
3031 @cindex @code{--directory}
3033 You can write recursive @code{make} commands just by copying this example,
3034 but there are many things to know about how they work and why, and about
3035 how the sub-@code{make} relates to the top-level @code{make}.
3037 @menu
3038 * MAKE Variable::               The special effects of using @samp{$(MAKE)}.
3039 * Variables/Recursion::         How to communicate variables to a sub-@code{make}.
3040 * Options/Recursion::           How to communicate options to a sub-@code{make}.
3041 * -w Option::                   How the @samp{-w} or @samp{--print-directory} option
3042                                  helps debug use of recursive @code{make} commands.
3043 @end menu
3045 @node MAKE Variable, Variables/Recursion,  , Recursion
3046 @subsection How the @code{MAKE} Variable Works
3047 @vindex MAKE
3048 @cindex recursion, and @code{MAKE} variable
3050 Recursive @code{make} commands should always use the variable @code{MAKE},
3051 not the explicit command name @samp{make}, as shown here:
3053 @example
3054 @group
3055 subsystem:
3056         cd subdir; $(MAKE)
3057 @end group
3058 @end example
3060 The value of this variable is the file name with which @code{make} was
3061 invoked.  If this file name was @file{/bin/make}, then the command executed
3062 is @samp{cd subdir; /bin/make}.  If you use a special version of
3063 @code{make} to run the top-level makefile, the same special version will be
3064 executed for recursive invocations.
3065 @cindex @code{cd} (shell command)
3067 As a special feature, using the variable @code{MAKE} in the commands of
3068 a rule alters the effects of the @samp{-t} (@samp{--touch}), @samp{-n}
3069 (@samp{--just-print}), or @samp{-q} (@w{@samp{--question}}) option.
3070 Using the @code{MAKE} variable has the same effect as using a @samp{+}
3071 character at the beginning of the command line.  @xref{Instead of
3072 Execution, ,Instead of Executing the Commands}.@refill
3074 Consider the command @samp{make -t} in the above example.  (The
3075 @samp{-t} option marks targets as up to date without actually running
3076 any commands; see @ref{Instead of Execution}.)  Following the usual
3077 definition of @samp{-t}, a @samp{make -t} command in the example would
3078 create a file named @file{subsystem} and do nothing else.  What you
3079 really want it to do is run @samp{@w{cd subdir;} @w{make -t}}; but that would
3080 require executing the command, and @samp{-t} says not to execute
3081 commands.@refill
3082 @cindex @code{-t}, and recursion
3083 @cindex recursion, and @code{-t}
3084 @cindex @code{--touch}, and recursion
3086 The special feature makes this do what you want: whenever a command
3087 line of a rule contains the variable @code{MAKE}, the flags @samp{-t},
3088 @samp{-n} and @samp{-q} do not apply to that line.  Command lines
3089 containing @code{MAKE} are executed normally despite the presence of a
3090 flag that causes most commands not to be run.  The usual
3091 @code{MAKEFLAGS} mechanism passes the flags to the sub-@code{make}
3092 (@pxref{Options/Recursion, ,Communicating Options to a
3093 Sub-@code{make}}), so your request to touch the files, or print the
3094 commands, is propagated to the subsystem.@refill
3096 @node Variables/Recursion, Options/Recursion, MAKE Variable, Recursion
3097 @subsection Communicating Variables to a Sub-@code{make}
3098 @cindex sub-@code{make}
3099 @cindex environment, and recursion
3100 @cindex exporting variables
3101 @cindex variables, environment
3102 @cindex variables, exporting
3103 @cindex recursion, and environment
3104 @cindex recursion, and variables
3106 Variable values of the top-level @code{make} can be passed to the
3107 sub-@code{make} through the environment by explicit request.  These
3108 variables are defined in the sub-@code{make} as defaults, but do not
3109 override what is specified in the makefile used by the sub-@code{make}
3110 makefile unless you use the @samp{-e} switch (@pxref{Options Summary,
3111 ,Summary of Options}).@refill
3113 To pass down, or @dfn{export}, a variable, @code{make} adds the variable
3114 and its value to the environment for running each command.  The
3115 sub-@code{make}, in turn, uses the environment to initialize its table
3116 of variable values.  @xref{Environment, ,Variables from the
3117 Environment}.
3119 Except by explicit request, @code{make} exports a variable only if it
3120 is either defined in the environment initially or set on the command
3121 line, and if its name consists only of letters, numbers, and underscores.
3122 Some shells cannot cope with environment variable names consisting of
3123 characters other than letters, numbers, and underscores.
3125 The special variables @code{SHELL} and @code{MAKEFLAGS} are always
3126 exported (unless you unexport them).
3127 @code{MAKEFILES} is exported if you set it to anything.
3129 @code{make} automatically passes down variable values that were defined
3130 on the command line, by putting them in the @code{MAKEFLAGS} variable.
3131 @iftex
3132 See the next section.
3133 @end iftex
3134 @ifinfo
3135 @xref{Options/Recursion}.
3136 @end ifinfo
3138 Variables are @emph{not} normally passed down if they were created by
3139 default by @code{make} (@pxref{Implicit Variables, ,Variables Used by
3140 Implicit Rules}).  The sub-@code{make} will define these for
3141 itself.@refill
3143 @findex export
3144 If you want to export specific variables to a sub-@code{make}, use the
3145 @code{export} directive, like this:
3147 @example
3148 export @var{variable} @dots{}
3149 @end example
3151 @noindent
3152 @findex unexport
3153 If you want to @emph{prevent} a variable from being exported, use the
3154 @code{unexport} directive, like this:
3156 @example
3157 unexport @var{variable} @dots{}
3158 @end example
3160 @noindent
3161 As a convenience, you can define a variable and export it at the same
3162 time by doing:
3164 @example
3165 export @var{variable} = value
3166 @end example
3168 @noindent
3169 has the same result as:
3171 @example
3172 @var{variable} = value
3173 export @var{variable}
3174 @end example
3176 @noindent
3179 @example
3180 export @var{variable} := value
3181 @end example
3183 @noindent
3184 has the same result as:
3186 @example
3187 @var{variable} := value
3188 export @var{variable}
3189 @end example
3191 Likewise,
3193 @example
3194 export @var{variable} += value
3195 @end example
3197 @noindent
3198 is just like:
3200 @example
3201 @var{variable} += value
3202 export @var{variable}
3203 @end example
3205 @noindent
3206 @xref{Appending, ,Appending More Text to Variables}.
3208 You may notice that the @code{export} and @code{unexport} directives
3209 work in @code{make} in the same way they work in the shell, @code{sh}.
3211 If you want all variables to be exported by default, you can use
3212 @code{export} by itself:
3214 @example
3215 export
3216 @end example
3218 @noindent
3219 This tells @code{make} that variables which are not explicitly mentioned
3220 in an @code{export} or @code{unexport} directive should be exported.
3221 Any variable given in an @code{unexport} directive will still @emph{not}
3222 be exported.  If you use @code{export} by itself to export variables by
3223 default, variables whose names contain characters other than
3224 alphanumerics and underscores will not be exported unless specifically
3225 mentioned in an @code{export} directive.@refill
3227 @findex .EXPORT_ALL_VARIABLES
3228 The behavior elicited by an @code{export} directive by itself was the
3229 default in older versions of GNU @code{make}.  If your makefiles depend
3230 on this behavior and you want to be compatible with old versions of
3231 @code{make}, you can write a rule for the special target
3232 @code{.EXPORT_ALL_VARIABLES} instead of using the @code{export} directive.
3233 This will be ignored by old @code{make}s, while the @code{export}
3234 directive will cause a syntax error.@refill
3235 @cindex compatibility in exporting
3237 Likewise, you can use @code{unexport} by itself to tell @code{make}
3238 @emph{not} to export variables by default.  Since this is the default
3239 behavior, you would only need to do this if @code{export} had been used
3240 by itself earlier (in an included makefile, perhaps).  You
3241 @strong{cannot} use @code{export} and @code{unexport} by themselves to
3242 have variables exported for some commands and not for others.  The last
3243 @code{export} or @code{unexport} directive that appears by itself
3244 determines the behavior for the entire run of @code{make}.@refill
3246 @vindex MAKELEVEL
3247 @cindex recursion, level of
3248 As a special feature, the variable @code{MAKELEVEL} is changed when it
3249 is passed down from level to level.  This variable's value is a string
3250 which is the depth of the level as a decimal number.  The value is
3251 @samp{0} for the top-level @code{make}; @samp{1} for a sub-@code{make},
3252 @samp{2} for a sub-sub-@code{make}, and so on.  The incrementation
3253 happens when @code{make} sets up the environment for a command.@refill
3255 The main use of @code{MAKELEVEL} is to test it in a conditional
3256 directive (@pxref{Conditionals, ,Conditional Parts of Makefiles}); this
3257 way you can write a makefile that behaves one way if run recursively and
3258 another way if run directly by you.@refill
3260 @vindex MAKEFILES
3261 You can use the variable @code{MAKEFILES} to cause all sub-@code{make}
3262 commands to use additional makefiles.  The value of @code{MAKEFILES} is
3263 a whitespace-separated list of file names.  This variable, if defined in
3264 the outer-level makefile, is passed down through the environment; then
3265 it serves as a list of extra makefiles for the sub-@code{make} to read
3266 before the usual or specified ones.  @xref{MAKEFILES Variable, ,The
3267 Variable @code{MAKEFILES}}.@refill
3269 @node Options/Recursion, -w Option, Variables/Recursion, Recursion
3270 @subsection Communicating Options to a Sub-@code{make}
3271 @cindex options, and recursion
3272 @cindex recursion, and options
3274 @vindex MAKEFLAGS
3275 Flags such as @samp{-s} and @samp{-k} are passed automatically to the
3276 sub-@code{make} through the variable @code{MAKEFLAGS}.  This variable is
3277 set up automatically by @code{make} to contain the flag letters that
3278 @code{make} received.  Thus, if you do @w{@samp{make -ks}} then
3279 @code{MAKEFLAGS} gets the value @samp{ks}.@refill
3281 As a consequence, every sub-@code{make} gets a value for @code{MAKEFLAGS}
3282 in its environment.  In response, it takes the flags from that value and
3283 processes them as if they had been given as arguments.
3284 @xref{Options Summary, ,Summary of Options}.
3286 @cindex command line variable definitions, and recursion
3287 @cindex variables, command line, and recursion
3288 @cindex recursion, and command line variable definitions
3289 Likewise variables defined on the command line are passed to the
3290 sub-@code{make} through @code{MAKEFLAGS}.  Words in the value of
3291 @code{MAKEFLAGS} that contain @samp{=}, @code{make} treats as variable
3292 definitions just as if they appeared on the command line.
3293 @xref{Overriding, ,Overriding Variables}.
3295 @cindex @code{-C}, and recursion
3296 @cindex @code{-f}, and recursion
3297 @cindex @code{-I}, and recursion
3298 @cindex @code{-o}, and recursion
3299 @cindex @code{-W}, and recursion
3300 @cindex @code{--directory}, and recursion
3301 @cindex @code{--file}, and recursion
3302 @cindex @code{--include-dir}, and recursion
3303 @cindex @code{--old-file}, and recursion
3304 @cindex @code{--assume-old}, and recursion
3305 @cindex @code{--assume-new}, and recursion
3306 @cindex @code{--new-file}, and recursion
3307 @cindex recursion, and @code{-C}
3308 @cindex recursion, and @code{-f}
3309 @cindex recursion, and @code{-I}
3310 @cindex recursion, and @code{-o}
3311 @cindex recursion, and @code{-W}
3312 The options @samp{-C}, @samp{-f}, @samp{-I}, @samp{-o}, and @samp{-W}
3313 are not put into @code{MAKEFLAGS}; these options are not passed down.@refill
3315 @cindex @code{-j}, and recursion
3316 @cindex @code{--jobs}, and recursion
3317 @cindex recursion, and @code{-j}
3318 @cindex job slots, and recursion
3319 The @samp{-j} option is a special case (@pxref{Parallel, ,Parallel Execution}).
3320 If you set it to some numeric value, @samp{-j 1} is always put into
3321 @code{MAKEFLAGS} instead of the value you specified.  This is because if
3322 the @w{@samp{-j}} option were passed down to sub-@code{make}s, you would
3323 get many more jobs running in parallel than you asked for.  If you give
3324 @samp{-j} with no numeric argument, meaning to run as many jobs as
3325 possible in parallel, this is passed down, since multiple infinities are
3326 no more than one.@refill
3328 If you do not want to pass the other flags down, you must change the
3329 value of @code{MAKEFLAGS}, like this:
3331 @example
3332 MAKEFLAGS=
3333 subsystem:
3334         cd subdir; $(MAKE)
3335 @end example
3337 or like this:
3339 @example
3340 subsystem:
3341         cd subdir; $(MAKE) MAKEFLAGS=
3342 @end example
3344 @vindex MAKEOVERRIDES
3345 The command line variable definitions really appear in the variable
3346 @code{MAKEOVERRIDES}, and @code{MAKEFLAGS} contains a reference to this
3347 variable.  If you do want to pass flags down normally, but don't want to
3348 pass down the command line variable definitions, you can reset
3349 @code{MAKEOVERRIDES} to empty, like this:
3351 @example
3352 MAKEOVERRIDES =
3353 @end example
3355 @noindent
3356 @cindex Arg list too long
3357 @cindex E2BIG
3358 This is not usually useful to do.  However, some systems have a small
3359 fixed limit on the size of the environment, and putting so much
3360 information in into the value of @code{MAKEFLAGS} can exceed it.
3361 If you see the error message @samp{Arg list too long}, this may be the problem.
3362 @findex .POSIX
3363 @cindex POSIX.2
3364 (For strict compliance with POSIX.2, changing @code{MAKEOVERRIDES} does
3365 not affect @code{MAKEFLAGS} if the special target @samp{.POSIX} appears
3366 in the makefile.  You probably do not care about this.)
3368 @vindex MFLAGS
3369 A similar variable @code{MFLAGS} exists also, for historical
3370 compatibility.  It has the same value as @code{MAKEFLAGS} except that it
3371 does not contain the command line variable definitions, and it always
3372 begins with a hyphen unless it is empty (@code{MAKEFLAGS} begins with a
3373 hyphen only when it begins with an option that has no single-letter
3374 version, such as @samp{--warn-undefined-variables}).  @code{MFLAGS} was
3375 traditionally used explicitly in the recursive @code{make} command, like
3376 this:
3378 @example
3379 subsystem:
3380         cd subdir; $(MAKE) $(MFLAGS)
3381 @end example
3383 @noindent
3384 but now @code{MAKEFLAGS} makes this usage redundant.  If you want your
3385 makefiles to be compatible with old @code{make} programs, use this
3386 technique; it will work fine with more modern @code{make} versions too.
3388 @cindex setting options from environment
3389 @cindex options, setting from environment
3390 @cindex setting options in makefiles
3391 @cindex options, setting in makefiles
3392 The @code{MAKEFLAGS} variable can also be useful if you want to have
3393 certain options, such as @samp{-k} (@pxref{Options Summary, ,Summary of
3394 Options}), set each time you run @code{make}.  You simply put a value for
3395 @code{MAKEFLAGS} in your environment.  You can also set @code{MAKEFLAGS} in
3396 a makefile, to specify additional flags that should also be in effect for
3397 that makefile.  (Note that you cannot use @code{MFLAGS} this way.  That
3398 variable is set only for compatibility; @code{make} does not interpret a
3399 value you set for it in any way.)
3401 When @code{make} interprets the value of @code{MAKEFLAGS} (either from the
3402 environment or from a makefile), it first prepends a hyphen if the value
3403 does not already begin with one.  Then it chops the value into words
3404 separated by blanks, and parses these words as if they were options given
3405 on the command line (except that @samp{-C}, @samp{-f}, @samp{-h},
3406 @samp{-o}, @samp{-W}, and their long-named versions are ignored; and there
3407 is no error for an invalid option).
3409 If you do put @code{MAKEFLAGS} in your environment, you should be sure not
3410 to include any options that will drastically affect the actions of
3411 @code{make} and undermine the purpose of makefiles and of @code{make}
3412 itself.  For instance, the @samp{-t}, @samp{-n}, and @samp{-q} options, if
3413 put in one of these variables, could have disastrous consequences and would
3414 certainly have at least surprising and probably annoying effects.@refill
3416 @node -w Option,  , Options/Recursion, Recursion
3417 @subsection The @samp{--print-directory} Option
3418 @cindex directories, printing them
3419 @cindex printing directories
3420 @cindex recursion, and printing directories
3422 If you use several levels of recursive @code{make} invocations, the
3423 @samp{-w} or @w{@samp{--print-directory}} option can make the output a
3424 lot easier to understand by showing each directory as @code{make}
3425 starts processing it and as @code{make} finishes processing it.  For
3426 example, if @samp{make -w} is run in the directory @file{/u/gnu/make},
3427 @code{make} will print a line of the form:@refill
3429 @example
3430 make: Entering directory `/u/gnu/make'.
3431 @end example
3433 @noindent
3434 before doing anything else, and a line of the form:
3436 @example
3437 make: Leaving directory `/u/gnu/make'.
3438 @end example
3440 @noindent
3441 when processing is completed.
3443 @cindex @code{-C}, and @code{-w}
3444 @cindex @code{--directory}, and @code{--print-directory}
3445 @cindex recursion, and @code{-w}
3446 @cindex @code{-w}, and @code{-C}
3447 @cindex @code{-w}, and recursion
3448 @cindex @code{--print-directory}, and @code{--directory}
3449 @cindex @code{--print-directory}, and recursion
3450 @cindex @code{--no-print-directory}
3451 @cindex @code{--print-directory}, disabling
3452 @cindex @code{-w}, disabling
3453 Normally, you do not need to specify this option because @samp{make}
3454 does it for you: @samp{-w} is turned on automatically when you use the
3455 @samp{-C} option, and in sub-@code{make}s.  @code{make} will not
3456 automatically turn on @samp{-w} if you also use @samp{-s}, which says to
3457 be silent, or if you use @samp{--no-print-directory} to explicitly
3458 disable it.
3460 @node Sequences, Empty Commands, Recursion, Commands
3461 @section Defining Canned Command Sequences
3462 @cindex sequences of commands
3463 @cindex commands, sequences of
3465 When the same sequence of commands is useful in making various targets, you
3466 can define it as a canned sequence with the @code{define} directive, and
3467 refer to the canned sequence from the rules for those targets.  The canned
3468 sequence is actually a variable, so the name must not conflict with other
3469 variable names.
3471 Here is an example of defining a canned sequence of commands:
3473 @example
3474 define run-yacc
3475 yacc $(firstword $^)
3476 mv y.tab.c $@@
3477 endef
3478 @end example
3479 @cindex @code{yacc}
3481 @noindent
3482 Here @code{run-yacc} is the name of the variable being defined;
3483 @code{endef} marks the end of the definition; the lines in between are the
3484 commands.  The @code{define} directive does not expand variable references
3485 and function calls in the canned sequence; the @samp{$} characters,
3486 parentheses, variable names, and so on, all become part of the value of the
3487 variable you are defining.
3488 @xref{Defining, ,Defining Variables Verbatim},
3489 for a complete explanation of @code{define}.
3491 The first command in this example runs Yacc on the first dependency of
3492 whichever rule uses the canned sequence.  The output file from Yacc is
3493 always named @file{y.tab.c}.  The second command moves the output to the
3494 rule's target file name.
3496 To use the canned sequence, substitute the variable into the commands of a
3497 rule.  You can substitute it like any other variable 
3498 (@pxref{Reference, ,Basics of Variable References}).
3499 Because variables defined by @code{define} are recursively expanded
3500 variables, all the variable references you wrote inside the @code{define}
3501 are expanded now.  For example:
3503 @example
3504 foo.c : foo.y
3505         $(run-yacc)
3506 @end example
3508 @noindent
3509 @samp{foo.y} will be substituted for the variable @samp{$^} when it occurs in
3510 @code{run-yacc}'s value, and @samp{foo.c} for @samp{$@@}.@refill
3512 This is a realistic example, but this particular one is not needed in
3513 practice because @code{make} has an implicit rule to figure out these
3514 commands based on the file names involved 
3515 (@pxref{Implicit Rules, ,Using Implicit Rules}).
3517 @cindex @@, and @code{define}
3518 @cindex -, and @code{define}
3519 @cindex +, and @code{define}
3520 In command execution, each line of a canned sequence is treated just as
3521 if the line appeared on its own in the rule, preceded by a tab.  In
3522 particular, @code{make} invokes a separate subshell for each line.  You
3523 can use the special prefix characters that affect command lines
3524 (@samp{@@}, @samp{-}, and @samp{+}) on each line of a canned sequence.
3525 @xref{Commands, ,Writing the Commands in Rules}.
3526 For example, using this canned sequence:
3528 @example
3529 define frobnicate
3530 @@echo "frobnicating target $@@"
3531 frob-step-1 $< -o $@@-step-1
3532 frob-step-2 $@@-step-1 -o $@@
3533 endef
3534 @end example
3536 @noindent
3537 @code{make} will not echo the first line, the @code{echo} command.
3538 But it @emph{will} echo the following two command lines.
3540 On the other hand, prefix characters on the command line that refers to
3541 a canned sequence apply to every line in the sequence.  So the rule:
3543 @example
3544 frob.out: frob.in
3545         @@$(frobnicate)
3546 @end example
3548 @noindent
3549 does not echo @emph{any} commands.
3550 (@xref{Echoing, ,Command Echoing}, for a full explanation of @samp{@@}.)
3552 @node Empty Commands,  , Sequences, Commands
3553 @section Using Empty Commands
3554 @cindex empty commands
3555 @cindex commands, empty
3557 It is sometimes useful to define commands which do nothing.  This is done
3558 simply by giving a command that consists of nothing but whitespace.  For
3559 example:
3561 @example
3562 target: ;
3563 @end example
3565 @noindent
3566 defines an empty command string for @file{target}.  You could also use a
3567 line beginning with a tab character to define an empty command string,
3568 but this would be confusing because such a line looks empty.
3570 @findex .DEFAULT@r{, and empty commands}
3571 You may be wondering why you would want to define a command string that
3572 does nothing.  The only reason this is useful is to prevent a target
3573 from getting implicit commands (from implicit rules or the
3574 @code{.DEFAULT} special target; @pxref{Implicit Rules} and 
3575 @pxref{Last Resort, ,Defining Last-Resort Default Rules}).@refill
3577 You may be inclined to define empty command strings for targets that are
3578 not actual files, but only exist so that their dependencies can be
3579 remade.  However, this is not the best way to do that, because the
3580 dependencies may not be remade properly if the target file actually does exist.
3581 @xref{Phony Targets, ,Phony Targets}, for a better way to do this.
3583 @node Using Variables, Conditionals, Commands, Top
3584 @chapter How to Use Variables
3585 @cindex variable
3586 @cindex value
3587 @cindex recursive variable expansion
3588 @cindex simple variable expansion
3590 A @dfn{variable} is a name defined in a makefile to represent a string
3591 of text, called the variable's @dfn{value}.  These values are
3592 substituted by explicit request into targets, dependencies, commands,
3593 and other parts of the makefile.  (In some other versions of @code{make},
3594 variables are called @dfn{macros}.)
3595 @cindex macro
3597 Variables and functions in all parts of a makefile are expanded when
3598 read, except for the shell commands in rules, the right-hand sides of
3599 variable definitions using @samp{=}, and the bodies of variable
3600 definitions using the @code{define} directive.@refill
3602 Variables can represent lists of file names, options to pass to compilers,
3603 programs to run, directories to look in for source files, directories to
3604 write output in, or anything else you can imagine.
3606 A variable name may be any sequence of characters not containing @samp{:},
3607 @samp{#}, @samp{=}, or leading or trailing whitespace.  However,
3608 variable names containing characters other than letters, numbers, and
3609 underscores should be avoided, as they may be given special meanings in the
3610 future, and with some shells they cannot be passed through the environment to a
3611 sub-@code{make} 
3612 (@pxref{Variables/Recursion, ,Communicating Variables to a Sub-@code{make}}).
3614 Variable names are case-sensitive.  The names @samp{foo}, @samp{FOO},
3615 and @samp{Foo} all refer to different variables.
3617 It is traditional to use upper case letters in variable names, but we
3618 recommend using lower case letters for variable names that serve internal
3619 purposes in the makefile, and reserving upper case for parameters that
3620 control implicit rules or for parameters that the user should override with
3621 command options (@pxref{Overriding, ,Overriding Variables}).
3623 A few variables have names that are a single punctuation character or
3624 just a few characters.  These are the @dfn{automatic variables}, and
3625 they have particular specialized uses.  @xref{Automatic, ,Automatic Variables}.
3627 @menu
3628 * Reference::                   How to use the value of a variable.
3629 * Flavors::                     Variables come in two flavors.
3630 * Advanced::                    Advanced features for referencing a variable.
3631 * Values::                      All the ways variables get their values.
3632 * Setting::                     How to set a variable in the makefile.
3633 * Appending::                   How to append more text to the old value
3634                                   of a variable.
3635 * Override Directive::          How to set a variable in the makefile even if
3636                                   the user has set it with a command argument.
3637 * Defining::                    An alternate way to set a variable 
3638                                   to a verbatim string.
3639 * Environment::                 Variable values can come from the environment.
3640 * Automatic::                   Some special variables have predefined
3641                                   meanings for use with implicit rules.
3642 @end menu
3644 @node Reference, Flavors,  , Using Variables
3645 @section Basics of Variable References
3646 @cindex variables, how to reference
3647 @cindex reference to variables
3648 @cindex @code{$}, in variable reference
3649 @cindex dollar sign (@code{$}), in variable reference
3651 To substitute a variable's value, write a dollar sign followed by the name
3652 of the variable in parentheses or braces: either @samp{$(foo)} or
3653 @samp{$@{foo@}} is a valid reference to the variable @code{foo}.  This
3654 special significance of @samp{$} is why you must write @samp{$$} to have
3655 the effect of a single dollar sign in a file name or command.
3657 Variable references can be used in any context: targets, dependencies,
3658 commands, most directives, and new variable values.  Here is an
3659 example of a common case, where a variable holds the names of all the
3660 object files in a program:
3662 @example
3663 @group
3664 objects = program.o foo.o utils.o
3665 program : $(objects)
3666         cc -o program $(objects)
3668 $(objects) : defs.h
3669 @end group
3670 @end example
3672 Variable references work by strict textual substitution.  Thus, the rule
3674 @example
3675 @group
3676 foo = c
3677 prog.o : prog.$(foo)
3678         $(foo)$(foo) -$(foo) prog.$(foo)
3679 @end group
3680 @end example
3682 @noindent
3683 could be used to compile a C program @file{prog.c}.  Since spaces before
3684 the variable value are ignored in variable assignments, the value of
3685 @code{foo} is precisely @samp{c}.  (Don't actually write your makefiles
3686 this way!)
3688 A dollar sign followed by a character other than a dollar sign,
3689 open-parenthesis or open-brace treats that single character as the
3690 variable name.  Thus, you could reference the variable @code{x} with
3691 @samp{$x}.  However, this practice is strongly discouraged, except in
3692 the case of the automatic variables (@pxref{Automatic, ,Automatic Variables}).
3694 @node Flavors, Advanced, Reference, Using Variables
3695 @section The Two Flavors of Variables
3696 @cindex flavors of variables
3697 @cindex recursive variable expansion
3698 @cindex variables, flavors
3699 @cindex recursively expanded variables
3700 @cindex variables, recursively expanded
3702 There are two ways that a variable in GNU @code{make} can have a value;
3703 we call them the two @dfn{flavors} of variables.  The two flavors are
3704 distinguished in how they are defined and in what they do when expanded.
3706 @cindex =
3707 The first flavor of variable is a @dfn{recursively expanded} variable.
3708 Variables of this sort are defined by lines using @samp{=}
3709 (@pxref{Setting, ,Setting Variables}) or by the @code{define} directive
3710 (@pxref{Defining, ,Defining Variables Verbatim}).  The value you specify
3711 is installed verbatim; if it contains references to other variables,
3712 these references are expanded whenever this variable is substituted (in
3713 the course of expanding some other string).  When this happens, it is
3714 called @dfn{recursive expansion}.@refill
3716 For example,
3718 @example
3719 foo = $(bar)
3720 bar = $(ugh)
3721 ugh = Huh?
3723 all:;echo $(foo)
3724 @end example
3726 @noindent
3727 will echo @samp{Huh?}: @samp{$(foo)} expands to @samp{$(bar)} which
3728 expands to @samp{$(ugh)} which finally expands to @samp{Huh?}.@refill
3730 This flavor of variable is the only sort supported by other versions of
3731 @code{make}.  It has its advantages and its disadvantages.  An advantage
3732 (most would say) is that:
3734 @example
3735 CFLAGS = $(include_dirs) -O
3736 include_dirs = -Ifoo -Ibar
3737 @end example
3739 @noindent
3740 will do what was intended: when @samp{CFLAGS} is expanded in a command,
3741 it will expand to @samp{-Ifoo -Ibar -O}.  A major disadvantage is that you
3742 cannot append something on the end of a variable, as in
3744 @example
3745 CFLAGS = $(CFLAGS) -O
3746 @end example
3748 @noindent
3749 because it will cause an infinite loop in the variable expansion.
3750 (Actually @code{make} detects the infinite loop and reports an error.)
3751 @cindex loops in variable expansion
3752 @cindex variables, loops in expansion
3754 Another disadvantage is that any functions 
3755 (@pxref{Functions, ,Functions for Transforming Text})
3756 referenced in the definition will be executed every time the variable is
3757 expanded.  This makes @code{make} run slower; worse, it causes the
3758 @code{wildcard} and @code{shell} functions to give unpredictable results
3759 because you cannot easily control when they are called, or even how many
3760 times.
3762 To avoid all the problems and inconveniences of recursively expanded
3763 variables, there is another flavor: simply expanded variables.
3765 @cindex simply expanded variables
3766 @cindex variables, simply expanded
3767 @cindex :=
3768 @dfn{Simply expanded variables} are defined by lines using @samp{:=}
3769 (@pxref{Setting, ,Setting Variables}).  
3770 The value of a simply expanded variable is scanned
3771 once and for all, expanding any references to other variables and
3772 functions, when the variable is defined.  The actual value of the simply
3773 expanded variable is the result of expanding the text that you write.
3774 It does not contain any references to other variables; it contains their
3775 values @emph{as of the time this variable was defined}.  Therefore,
3777 @example
3778 x := foo
3779 y := $(x) bar
3780 x := later
3781 @end example
3783 @noindent
3784 is equivalent to
3786 @example
3787 y := foo bar
3788 x := later
3789 @end example
3791 When a simply expanded variable is referenced, its value is substituted
3792 verbatim.
3794 Here is a somewhat more complicated example, illustrating the use of
3795 @samp{:=} in conjunction with the @code{shell} function.
3796 (@xref{Shell Function, , The @code{shell} Function}.)  This example
3797 also shows use of the variable @code{MAKELEVEL}, which is changed
3798 when it is passed down from level to level.
3799 (@xref{Variables/Recursion, , Communicating Variables to a
3800 Sub-@code{make}}, for information about @code{MAKELEVEL}.)
3802 @vindex MAKELEVEL
3803 @vindex MAKE
3804 @example
3805 @group
3806 ifeq (0,$@{MAKELEVEL@})
3807 cur-dir   := $(shell pwd)
3808 whoami    := $(shell whoami)
3809 host-type := $(shell arch)
3810 MAKE := $@{MAKE@} host-type=$@{host-type@} whoami=$@{whoami@}
3811 endif
3812 @end group
3813 @end example
3815 @noindent 
3816 An advantage of this use of @samp{:=} is that a typical
3817 `descend into a directory' command then looks like this:
3819 @example
3820 @group
3821 $@{subdirs@}:
3822       $@{MAKE@} cur-dir=$@{cur-dir@}/$@@ -C $@@ all
3823 @end group
3824 @end example
3826 Simply expanded variables generally make complicated makefile programming
3827 more predictable because they work like variables in most programming
3828 languages.  They allow you to redefine a variable using its own value (or
3829 its value processed in some way by one of the expansion functions) and to
3830 use the expansion functions much more efficiently 
3831 (@pxref{Functions, ,Functions for Transforming Text}).
3833 @cindex spaces, in variable values
3834 @cindex whitespace, in variable values
3835 @cindex variables, spaces in values
3836 You can also use them to introduce controlled leading whitespace into
3837 variable values.  Leading whitespace characters are discarded from your
3838 input before substitution of variable references and function calls;
3839 this means you can include leading spaces in a variable value by
3840 protecting them with variable references, like this:
3842 @example
3843 nullstring :=
3844 space := $(nullstring) # end of the line
3845 @end example
3847 @noindent
3848 Here the value of the variable @code{space} is precisely one space.  The
3849 comment @w{@samp{# end of the line}} is included here just for clarity.
3850 Since trailing space characters are @emph{not} stripped from variable
3851 values, just a space at the end of the line would have the same effect
3852 (but be rather hard to read).  If you put whitespace at the end of a
3853 variable value, it is a good idea to put a comment like that at the end
3854 of the line to make your intent clear.  Conversely, if you do @emph{not}
3855 want any whitespace characters at the end of your variable value, you
3856 must remember not to put a random comment on the end of the line after
3857 some whitespace, such as this:
3859 @example
3860 dir := /foo/bar    # directory to put the frobs in
3861 @end example
3863 @noindent
3864 Here the value of the variable @code{dir} is @w{@samp{/foo/bar    }}
3865 (with four trailing spaces), which was probably not the intention.
3866 (Imagine something like @w{@samp{$(dir)/file}} with this definition!)
3868 @node Advanced, Values, Flavors, Using Variables
3869 @section Advanced Features for Reference to Variables
3870 @cindex reference to variables
3872 This section describes some advanced features you can use to reference
3873 variables in more flexible ways.
3875 @menu
3876 * Substitution Refs::           Referencing a variable with 
3877                                   substitutions on the value.
3878 * Computed Names::              Computing the name of the variable to refer to.
3879 @end menu
3881 @node Substitution Refs, Computed Names,  , Advanced
3882 @subsection Substitution References
3883 @cindex modified variable reference
3884 @cindex substitution variable reference
3885 @cindex variables, modified reference
3886 @cindex variables, substitution reference
3888 @cindex variables, substituting suffix in
3889 @cindex suffix, substituting in variables
3890 A @dfn{substitution reference} substitutes the value of a variable with
3891 alterations that you specify.  It has the form
3892 @samp{$(@var{var}:@var{a}=@var{b})} (or
3893 @samp{$@{@var{var}:@var{a}=@var{b}@}}) and its meaning is to take the value
3894 of the variable @var{var}, replace every @var{a} at the end of a word with
3895 @var{b} in that value, and substitute the resulting string.
3897 When we say ``at the end of a word'', we mean that @var{a} must appear
3898 either followed by whitespace or at the end of the value in order to be
3899 replaced; other occurrences of @var{a} in the value are unaltered.  For
3900 example:@refill
3902 @example
3903 foo := a.o b.o c.o
3904 bar := $(foo:.o=.c)
3905 @end example
3907 @noindent
3908 sets @samp{bar} to @samp{a.c b.c c.c}.  @xref{Setting, ,Setting Variables}.
3910 A substitution reference is actually an abbreviation for use of the
3911 @code{patsubst} expansion function (@pxref{Text Functions, ,Functions for String Substitution and Analysis}).  We provide
3912 substitution references as well as @code{patsubst} for compatibility with
3913 other implementations of @code{make}.
3915 @findex patsubst
3916 Another type of substitution reference lets you use the full power of
3917 the @code{patsubst} function.  It has the same form
3918 @samp{$(@var{var}:@var{a}=@var{b})} described above, except that now
3919 @var{a} must contain a single @samp{%} character.  This case is
3920 equivalent to @samp{$(patsubst @var{a},@var{b},$(@var{var}))}.
3921 @xref{Text Functions, ,Functions for String Substitution and Analysis},
3922 for a description of the @code{patsubst} function.@refill
3924 @example
3925 @group
3926 @exdent For example:
3928 foo := a.o b.o c.o
3929 bar := $(foo:%.o=%.c)
3930 @end group
3931 @end example
3933 @noindent
3934 sets @samp{bar} to @samp{a.c b.c c.c}.
3936 @node Computed Names,  , Substitution Refs, Advanced
3937 @subsection Computed Variable Names
3938 @cindex nested variable reference
3939 @cindex computed variable name
3940 @cindex variables, computed names
3941 @cindex variables, nested references
3942 @cindex variables, @samp{$} in name
3943 @cindex @code{$}, in variable name
3944 @cindex dollar sign (@code{$}), in variable name
3946 Computed variable names are a complicated concept needed only for
3947 sophisticated makefile programming.  For most purposes you need not
3948 consider them, except to know that making a variable with a dollar sign
3949 in its name might have strange results.  However, if you are the type
3950 that wants to understand everything, or you are actually interested in
3951 what they do, read on.
3953 Variables may be referenced inside the name of a variable.  This is
3954 called a @dfn{computed variable name} or a @dfn{nested variable
3955 reference}.  For example,
3957 @example
3958 x = y
3959 y = z
3960 a := $($(x))
3961 @end example
3963 @noindent
3964 defines @code{a} as @samp{z}: the @samp{$(x)} inside @samp{$($(x))} expands
3965 to @samp{y}, so @samp{$($(x))} expands to @samp{$(y)} which in turn expands
3966 to @samp{z}.  Here the name of the variable to reference is not stated
3967 explicitly; it is computed by expansion of @samp{$(x)}.  The reference
3968 @samp{$(x)} here is nested within the outer variable reference.
3970 The previous example shows two levels of nesting, but any number of levels
3971 is possible.  For example, here are three levels:
3973 @example
3974 x = y
3975 y = z
3976 z = u
3977 a := $($($(x)))
3978 @end example
3980 @noindent
3981 Here the innermost @samp{$(x)} expands to @samp{y}, so @samp{$($(x))}
3982 expands to @samp{$(y)} which in turn expands to @samp{z}; now we have
3983 @samp{$(z)}, which becomes @samp{u}.
3985 References to recursively-expanded variables within a variable name are
3986 reexpanded in the usual fashion.  For example:
3988 @example
3989 x = $(y)
3990 y = z
3991 z = Hello
3992 a := $($(x))
3993 @end example
3995 @noindent
3996 defines @code{a} as @samp{Hello}: @samp{$($(x))} becomes @samp{$($(y))}
3997 which becomes @samp{$(z)} which becomes @samp{Hello}.
3999 Nested variable references can also contain modified references and
4000 function invocations (@pxref{Functions, ,Functions for Transforming Text}),
4001 just like any other reference.
4002 For example, using the @code{subst} function 
4003 (@pxref{Text Functions, ,Functions for String Substitution and Analysis}):
4005 @example
4006 @group
4007 x = variable1
4008 variable2 := Hello
4009 y = $(subst 1,2,$(x))
4010 z = y
4011 a := $($($(z)))
4012 @end group
4013 @end example
4015 @noindent
4016 eventually defines @code{a} as @samp{Hello}.  It is doubtful that anyone
4017 would ever want to write a nested reference as convoluted as this one, but
4018 it works: @samp{$($($(z)))} expands to @samp{$($(y))} which becomes
4019 @samp{$($(subst 1,2,$(x)))}.  This gets the value @samp{variable1} from
4020 @code{x} and changes it by substitution to @samp{variable2}, so that the
4021 entire string becomes @samp{$(variable2)}, a simple variable reference
4022 whose value is @samp{Hello}.@refill
4024 A computed variable name need not consist entirely of a single variable
4025 reference.  It can contain several variable references, as well as some
4026 invariant text.  For example,
4028 @example
4029 @group
4030 a_dirs := dira dirb
4031 1_dirs := dir1 dir2
4032 @end group
4034 @group
4035 a_files := filea fileb
4036 1_files := file1 file2
4037 @end group
4039 @group
4040 ifeq "$(use_a)" "yes"
4041 a1 := a
4042 else
4043 a1 := 1
4044 endif
4045 @end group
4047 @group
4048 ifeq "$(use_dirs)" "yes"
4049 df := dirs
4050 else
4051 df := files
4052 endif
4054 dirs := $($(a1)_$(df))
4055 @end group
4056 @end example
4058 @noindent
4059 will give @code{dirs} the same value as @code{a_dirs}, @code{1_dirs},
4060 @code{a_files} or @code{1_files} depending on the settings of @code{use_a}
4061 and @code{use_dirs}.@refill
4063 Computed variable names can also be used in substitution references:
4065 @example
4066 @group
4067 a_objects := a.o b.o c.o
4068 1_objects := 1.o 2.o 3.o
4070 sources := $($(a1)_objects:.o=.c)
4071 @end group
4072 @end example
4074 @noindent
4075 defines @code{sources} as either @samp{a.c b.c c.c} or @samp{1.c 2.c 3.c},
4076 depending on the value of @code{a1}.
4078 The only restriction on this sort of use of nested variable references
4079 is that they cannot specify part of the name of a function to be called.
4080 This is because the test for a recognized function name is done before
4081 the expansion of nested references.  For example,
4083 @example
4084 @group
4085 ifdef do_sort
4086 func := sort
4087 else
4088 func := strip
4089 endif
4090 @end group
4092 @group
4093 bar := a d b g q c
4094 @end group
4096 @group
4097 foo := $($(func) $(bar))
4098 @end group
4099 @end example
4101 @noindent
4102 attempts to give @samp{foo} the value of the variable @samp{sort a d b g
4103 q c} or @samp{strip a d b g q c}, rather than giving @samp{a d b g q c}
4104 as the argument to either the @code{sort} or the @code{strip} function.
4105 This restriction could be removed in the future if that change is shown
4106 to be a good idea.
4108 You can also use computed variable names in the left-hand side of a
4109 variable assignment, or in a @code{define} directive, as in:
4111 @example
4112 dir = foo
4113 $(dir)_sources := $(wildcard $(dir)/*.c)
4114 define $(dir)_print
4115 lpr $($(dir)_sources)
4116 endef
4117 @end example
4119 @noindent
4120 This example defines the variables @samp{dir}, @samp{foo_sources}, and
4121 @samp{foo_print}.
4123 Note that @dfn{nested variable references} are quite different from
4124 @dfn{recursively expanded variables}
4125 (@pxref{Flavors, ,The Two Flavors of Variables}), though both are
4126 used together in complex ways when doing makefile programming.@refill
4128 @node Values, Setting, Advanced, Using Variables
4129 @section How Variables Get Their Values
4130 @cindex variables, how they get their values
4131 @cindex value, how a variable gets it
4133 Variables can get values in several different ways:
4135 @itemize @bullet
4136 @item
4137 You can specify an overriding value when you run @code{make}.
4138 @xref{Overriding, ,Overriding Variables}.
4140 @item
4141 You can specify a value in the makefile, either
4142 with an assignment (@pxref{Setting, ,Setting Variables}) or with a
4143 verbatim definition (@pxref{Defining, ,Defining Variables Verbatim}).@refill
4145 @item
4146 Variables in the environment become @code{make} variables.
4147 @xref{Environment, ,Variables from the Environment}.
4149 @item
4150 Several @dfn{automatic} variables are given new values for each rule.
4151 Each of these has a single conventional use.
4152 @xref{Automatic, ,Automatic Variables}.
4154 @item
4155 Several variables have constant initial values.
4156 @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
4157 @end itemize
4159 @node Setting, Appending, Values, Using Variables
4160 @section Setting Variables
4161 @cindex setting variables
4162 @cindex variables, setting
4163 @cindex =
4164 @cindex :=
4166 To set a variable from the makefile, write a line starting with the
4167 variable name followed by @samp{=} or @samp{:=}.  Whatever follows the
4168 @samp{=} or @samp{:=} on the line becomes the value.  For example,
4170 @example
4171 objects = main.o foo.o bar.o utils.o
4172 @end example
4174 @noindent
4175 defines a variable named @code{objects}.  Whitespace around the variable
4176 name and immediately after the @samp{=} is ignored.
4178 Variables defined with @samp{=} are @dfn{recursively expanded} variables.
4179 Variables defined with @samp{:=} are @dfn{simply expanded} variables; these
4180 definitions can contain variable references which will be expanded before
4181 the definition is made.  @xref{Flavors, ,The Two Flavors of Variables}.
4183 The variable name may contain function and variable references, which
4184 are expanded when the line is read to find the actual variable name to use.
4186 There is no limit on the length of the value of a variable except the
4187 amount of swapping space on the computer.  When a variable definition is
4188 long, it is a good idea to break it into several lines by inserting
4189 backslash-newline at convenient places in the definition.  This will not
4190 affect the functioning of @code{make}, but it will make the makefile easier
4191 to read.
4193 Most variable names are considered to have the empty string as a value if
4194 you have never set them.  Several variables have built-in initial values
4195 that are not empty, but you can set them in the usual ways
4196 (@pxref{Implicit Variables, ,Variables Used by Implicit Rules}).  
4197 Several special variables are set
4198 automatically to a new value for each rule; these are called the
4199 @dfn{automatic} variables (@pxref{Automatic, ,Automatic Variables}).
4201 @node Appending, Override Directive, Setting, Using Variables
4202 @section Appending More Text to Variables
4203 @cindex +=
4204 @cindex appending to variables
4205 @cindex variables, appending to
4207 Often it is useful to add more text to the value of a variable already defined.
4208 You do this with a line containing @samp{+=}, like this:
4210 @example
4211 objects += another.o
4212 @end example
4214 @noindent
4215 This takes the value of the variable @code{objects}, and adds the text
4216 @samp{another.o} to it (preceded by a single space).  Thus:
4218 @example
4219 objects = main.o foo.o bar.o utils.o
4220 objects += another.o
4221 @end example
4223 @noindent
4224 sets @code{objects} to @samp{main.o foo.o bar.o utils.o another.o}.
4226 Using @samp{+=} is similar to:
4228 @example
4229 objects = main.o foo.o bar.o utils.o
4230 objects := $(objects) another.o
4231 @end example
4233 @noindent
4234 but differs in ways that become important when you use more complex values.
4236 When the variable in question has not been defined before, @samp{+=}
4237 acts just like normal @samp{=}: it defines a recursively-expanded
4238 variable.  However, when there @emph{is} a previous definition, exactly
4239 what @samp{+=} does depends on what flavor of variable you defined
4240 originally.  @xref{Flavors, ,The Two Flavors of Variables}, for an
4241 explanation of the two flavors of variables.
4243 When you add to a variable's value with @samp{+=}, @code{make} acts
4244 essentially as if you had included the extra text in the initial
4245 definition of the variable.  If you defined it first with @samp{:=},
4246 making it a simply-expanded variable, @samp{+=} adds to that
4247 simply-expanded definition, and expands the new text before appending it
4248 to the old value just as @samp{:=} does
4249 (@pxref{Setting, ,Setting Variables}, for a full explanation of @samp{:=}).
4250 In fact,
4252 @example
4253 variable := value
4254 variable += more
4255 @end example
4257 @noindent
4258 is exactly equivalent to:
4260 @noindent
4261 @example
4262 variable := value
4263 variable := $(variable) more
4264 @end example
4266 On the other hand, when you use @samp{+=} with a variable that you defined
4267 first to be recursively-expanded using plain @samp{=}, @code{make} does
4268 something a bit different.  Recall that when you define a
4269 recursively-expanded variable, @code{make} does not expand the value you set
4270 for variable and function references immediately.  Instead it stores the text
4271 verbatim, and saves these variable and function references to be expanded
4272 later, when you refer to the new variable (@pxref{Flavors, ,The Two Flavors
4273 of Variables}).  When you use @samp{+=} on a recursively-expanded variable,
4274 it is this unexpanded text to which @code{make} appends the new text you
4275 specify.
4277 @example
4278 @group
4279 variable = value
4280 variable += more
4281 @end group
4282 @end example
4284 @noindent
4285 is roughly equivalent to:
4287 @example
4288 @group
4289 temp = value
4290 variable = $(temp) more
4291 @end group
4292 @end example
4294 @noindent
4295 except that of course it never defines a variable called @code{temp}.
4296 The importance of this comes when the variable's old value contains
4297 variable references.  Take this common example:
4299 @example
4300 CFLAGS = $(includes) -O
4301 @dots{}
4302 CFLAGS += -pg # enable profiling
4303 @end example
4305 @noindent
4306 The first line defines the @code{CFLAGS} variable with a reference to another
4307 variable, @code{includes}.  (@code{CFLAGS} is used by the rules for C
4308 compilation; @pxref{Catalogue of Rules, ,Catalogue of Implicit Rules}.)
4309 Using @samp{=} for the definition makes @code{CFLAGS} a recursively-expanded
4310 variable, meaning @w{@samp{$(includes) -O}} is @emph{not} expanded when
4311 @code{make} processes the definition of @code{CFLAGS}.  Thus, @code{includes}
4312 need not be defined yet for its value to take effect.  It only has to be
4313 defined before any reference to @code{CFLAGS}.  If we tried to append to the
4314 value of @code{CFLAGS} without using @samp{+=}, we might do it like this:
4316 @example
4317 CFLAGS := $(CFLAGS) -pg # enable profiling
4318 @end example
4320 @noindent
4321 This is pretty close, but not quite what we want.  Using @samp{:=}
4322 redefines @code{CFLAGS} as a simply-expanded variable; this means
4323 @code{make} expands the text @w{@samp{$(CFLAGS) -pg}} before setting the
4324 variable.  If @code{includes} is not yet defined, we get @w{@samp{ -O
4325 -pg}}, and a later definition of @code{includes} will have no effect.
4326 Conversely, by using @samp{+=} we set @code{CFLAGS} to the
4327 @emph{unexpanded} value @w{@samp{$(includes) -O -pg}}.  Thus we preserve
4328 the reference to @code{includes}, so if that variable gets defined at
4329 any later point, a reference like @samp{$(CFLAGS)} still uses its
4330 value.
4332 @node Override Directive, Defining, Appending, Using Variables
4333 @section The @code{override} Directive
4334 @findex override
4335 @cindex overriding with @code{override}
4336 @cindex variables, overriding
4338 If a variable has been set with a command argument 
4339 (@pxref{Overriding, ,Overriding Variables}),
4340 then ordinary assignments in the makefile are ignored.  If you want to set
4341 the variable in the makefile even though it was set with a command
4342 argument, you can use an @code{override} directive, which is a line that
4343 looks like this:@refill
4345 @example
4346 override @var{variable} = @var{value}
4347 @end example
4349 @noindent
4352 @example
4353 override @var{variable} := @var{value}
4354 @end example
4356 To append more text to a variable defined on the command line, use:
4358 @example
4359 override @var{variable} += @var{more text}
4360 @end example
4362 @noindent
4363 @xref{Appending, ,Appending More Text to Variables}.
4365 The @code{override} directive was not invented for escalation in the war
4366 between makefiles and command arguments.  It was invented so you can alter
4367 and add to values that the user specifies with command arguments.
4369 For example, suppose you always want the @samp{-g} switch when you run the
4370 C compiler, but you would like to allow the user to specify the other
4371 switches with a command argument just as usual.  You could use this
4372 @code{override} directive:
4374 @example
4375 override CFLAGS += -g
4376 @end example
4378 You can also use @code{override} directives with @code{define} directives.
4379 This is done as you might expect:
4381 @example
4382 override define foo
4384 endef
4385 @end example
4387 @noindent
4388 @iftex
4389 See the next section for information about @code{define}.
4390 @end iftex
4391 @ifinfo
4392 @xref{Defining, ,Defining Variables Verbatim}.
4393 @end ifinfo
4395 @node Defining, Environment, Override Directive, Using Variables
4396 @section Defining Variables Verbatim
4397 @findex define
4398 @findex endef
4399 @cindex verbatim variable definition
4400 @cindex defining variables verbatim
4401 @cindex variables, defining verbatim
4403 Another way to set the value of a variable is to use the @code{define}
4404 directive.  This directive has an unusual syntax which allows newline
4405 characters to be included in the value, which is convenient for defining
4406 canned sequences of commands 
4407 (@pxref{Sequences, ,Defining Canned Command Sequences}).
4409 The @code{define} directive is followed on the same line by the name of the
4410 variable and nothing more.  The value to give the variable appears on the
4411 following lines.  The end of the value is marked by a line containing just
4412 the word @code{endef}.  Aside from this difference in syntax, @code{define}
4413 works just like @samp{=}: it creates a recursively-expanded variable
4414 (@pxref{Flavors, ,The Two Flavors of Variables}).
4415 The variable name may contain function and variable references, which
4416 are expanded when the directive is read to find the actual variable name
4417 to use.
4419 @example
4420 define two-lines
4421 echo foo
4422 echo $(bar)
4423 endef
4424 @end example
4426 The value in an ordinary assignment cannot contain a newline; but the
4427 newlines that separate the lines of the value in a @code{define} become
4428 part of the variable's value (except for the final newline which precedes
4429 the @code{endef} and is not considered part of the value).@refill
4431 @need 800
4432 The previous example is functionally equivalent to this:
4434 @example
4435 two-lines = echo foo; echo $(bar)
4436 @end example
4438 @noindent
4439 since two commands separated by semicolon behave much like two separate
4440 shell commands.  However, note that using two separate lines means
4441 @code{make} will invoke the shell twice, running an independent subshell
4442 for each line.  @xref{Execution, ,Command Execution}.
4444 If you want variable definitions made with @code{define} to take
4445 precedence over command-line variable definitions, you can use the
4446 @code{override} directive together with @code{define}:
4448 @example
4449 override define two-lines
4451 $(bar)
4452 endef
4453 @end example
4455 @noindent
4456 @xref{Override Directive, ,The @code{override} Directive}.
4458 @node Environment,  , Defining, Using Variables
4459 @section Variables from the Environment
4461 @cindex variables, environment
4462 @cindex environment
4463 Variables in @code{make} can come from the environment in which
4464 @code{make} is run.  Every environment variable that @code{make} sees when
4465 it starts up is transformed into a @code{make} variable with the same name
4466 and value.  But an explicit assignment in the makefile, or with a command
4467 argument, overrides the environment.  (If the @samp{-e} flag is specified,
4468 then values from the environment override assignments in the makefile.
4469 @xref{Options Summary, ,Summary of Options}.
4470 But this is not recommended practice.)
4472 Thus, by setting the variable @code{CFLAGS} in your environment, you can
4473 cause all C compilations in most makefiles to use the compiler switches you
4474 prefer.  This is safe for variables with standard or conventional meanings
4475 because you know that no makefile will use them for other things.  (But
4476 this is not totally reliable; some makefiles set @code{CFLAGS} explicitly
4477 and therefore are not affected by the value in the environment.)
4479 When @code{make} is invoked recursively, variables defined in the
4480 outer invocation can be passed to inner invocations through the
4481 environment (@pxref{Recursion, ,Recursive Use of @code{make}}).  By
4482 default, only variables that came from the environment or the command
4483 line are passed to recursive invocations.  You can use the
4484 @code{export} directive to pass other variables.
4485 @xref{Variables/Recursion, , Communicating Variables to a
4486 Sub-@code{make}}, for full details.
4488 Other use of variables from the environment is not recommended.  It is not
4489 wise for makefiles to depend for their functioning on environment variables
4490 set up outside their control, since this would cause different users to get
4491 different results from the same makefile.  This is against the whole
4492 purpose of most makefiles.
4494 Such problems would be especially likely with the variable @code{SHELL},
4495 which is normally present in the environment to specify the user's choice
4496 of interactive shell.  It would be very undesirable for this choice to
4497 affect @code{make}.  So @code{make} ignores the environment value of
4498 @code{SHELL}.@refill
4500 @node Conditionals, Functions, Using Variables, Top
4501 @chapter Conditional Parts of Makefiles
4503 @cindex conditionals
4504 A @dfn{conditional} causes part of a makefile to be obeyed or ignored
4505 depending on the values of variables.  Conditionals can compare the
4506 value of one variable to another, or the value of a variable to
4507 a constant string.  Conditionals control what @code{make} actually
4508 ``sees'' in the makefile, so they @emph{cannot} be used to control shell
4509 commands at the time of execution.@refill
4511 @menu
4512 * Conditional Example::         Example of a conditional
4513 * Conditional Syntax::          The syntax of conditionals.
4514 * Testing Flags::               Conditionals that test flags.
4515 @end menu
4517 @node Conditional Example, Conditional Syntax,  , Conditionals
4518 @section Example of a Conditional
4520 The following example of a conditional tells @code{make} to use one set
4521 of libraries if the @code{CC} variable is @samp{gcc}, and a different
4522 set of libraries otherwise.  It works by controlling which of two
4523 command lines will be used as the command for a rule.  The result is
4524 that @samp{CC=gcc} as an argument to @code{make} changes not only which
4525 compiler is used but also which libraries are linked.
4527 @example
4528 libs_for_gcc = -lgnu
4529 normal_libs =
4531 foo: $(objects)
4532 ifeq ($(CC),gcc)
4533         $(CC) -o foo $(objects) $(libs_for_gcc)
4534 else
4535         $(CC) -o foo $(objects) $(normal_libs)
4536 endif
4537 @end example
4539 This conditional uses three directives: one @code{ifeq}, one @code{else}
4540 and one @code{endif}.
4542 The @code{ifeq} directive begins the conditional, and specifies the
4543 condition.  It contains two arguments, separated by a comma and surrounded
4544 by parentheses.  Variable substitution is performed on both arguments and
4545 then they are compared.  The lines of the makefile following the
4546 @code{ifeq} are obeyed if the two arguments match; otherwise they are
4547 ignored.
4549 The @code{else} directive causes the following lines to be obeyed if the
4550 previous conditional failed.  In the example above, this means that the
4551 second alternative linking command is used whenever the first alternative
4552 is not used.  It is optional to have an @code{else} in a conditional.
4554 The @code{endif} directive ends the conditional.  Every conditional must
4555 end with an @code{endif}.  Unconditional makefile text follows.
4557 As this example illustrates, conditionals work at the textual level:
4558 the lines of the conditional are treated as part of the makefile, or
4559 ignored, according to the condition.  This is why the larger syntactic
4560 units of the makefile, such as rules, may cross the beginning or the
4561 end of the conditional.
4563 When the variable @code{CC} has the value @samp{gcc}, the above example has
4564 this effect:
4566 @example
4567 foo: $(objects)
4568         $(CC) -o foo $(objects) $(libs_for_gcc)
4569 @end example
4571 @noindent
4572 When the variable @code{CC} has any other value, the effect is this:
4574 @example
4575 foo: $(objects)
4576         $(CC) -o foo $(objects) $(normal_libs)
4577 @end example
4579 Equivalent results can be obtained in another way by conditionalizing a
4580 variable assignment and then using the variable unconditionally:
4582 @example
4583 libs_for_gcc = -lgnu
4584 normal_libs =
4586 ifeq ($(CC),gcc)
4587   libs=$(libs_for_gcc)
4588 else
4589   libs=$(normal_libs)
4590 endif
4592 foo: $(objects)
4593         $(CC) -o foo $(objects) $(libs)
4594 @end example
4596 @node Conditional Syntax, Testing Flags, Conditional Example, Conditionals
4597 @section Syntax of Conditionals
4598 @findex ifdef
4599 @findex ifeq
4600 @findex ifndef
4601 @findex ifneq
4602 @findex else
4603 @findex endif
4605 The syntax of a simple conditional with no @code{else} is as follows:
4607 @example
4608 @var{conditional-directive}
4609 @var{text-if-true}
4610 endif
4611 @end example
4613 @noindent
4614 The @var{text-if-true} may be any lines of text, to be considered as part
4615 of the makefile if the condition is true.  If the condition is false, no
4616 text is used instead.
4618 The syntax of a complex conditional is as follows:
4620 @example
4621 @var{conditional-directive}
4622 @var{text-if-true}
4623 else
4624 @var{text-if-false}
4625 endif
4626 @end example
4628 @noindent
4629 If the condition is true, @var{text-if-true} is used; otherwise,
4630 @var{text-if-false} is used instead.  The @var{text-if-false} can be any
4631 number of lines of text.
4633 The syntax of the @var{conditional-directive} is the same whether the
4634 conditional is simple or complex.  There are four different directives that
4635 test different conditions.  Here is a table of them:
4637 @table @code
4638 @item ifeq (@var{arg1}, @var{arg2})
4639 @itemx ifeq '@var{arg1}' '@var{arg2}'
4640 @itemx ifeq "@var{arg1}" "@var{arg2}"
4641 @itemx ifeq "@var{arg1}" '@var{arg2}'
4642 @itemx ifeq '@var{arg1}' "@var{arg2}"
4643 Expand all variable references in @var{arg1} and @var{arg2} and
4644 compare them.  If they are identical, the @var{text-if-true} is
4645 effective; otherwise, the @var{text-if-false}, if any, is effective.
4647 Often you want to test if a variable has a non-empty value.  When the
4648 value results from complex expansions of variables and functions,
4649 expansions you would consider empty may actually contain whitespace
4650 characters and thus are not seen as empty.  However, you can use the
4651 @code{strip} function (@pxref{Text Functions}) to avoid interpreting
4652 whitespace as a non-empty value.  For example:
4654 @example
4655 @group
4656 ifeq ($(strip $(foo)),)
4657 @var{text-if-empty}
4658 endif
4659 @end group
4660 @end example
4662 @noindent
4663 will evaluate @var{text-if-empty} even if the expansion of
4664 @code{$(foo)} contains whitespace characters.
4666 @item ifneq (@var{arg1}, @var{arg2})
4667 @itemx ifneq '@var{arg1}' '@var{arg2}'
4668 @itemx ifneq "@var{arg1}" "@var{arg2}"
4669 @itemx ifneq "@var{arg1}" '@var{arg2}'
4670 @itemx ifneq '@var{arg1}' "@var{arg2}"
4671 Expand all variable references in @var{arg1} and @var{arg2} and
4672 compare them.  If they are different, the @var{text-if-true} is
4673 effective; otherwise, the @var{text-if-false}, if any, is effective.
4675 @item ifdef @var{variable-name}
4676 If the variable @var{variable-name} has a non-empty value, the
4677 @var{text-if-true} is effective; otherwise, the @var{text-if-false},
4678 if any, is effective.  Variables that have never been defined have an
4679 empty value.
4681 Note that @code{ifdef} only tests whether a variable has a value.  It
4682 does not expand the variable to see if that value is nonempty.
4683 Consequently, tests using @code{ifdef} return true for all definitions
4684 except those like @code{foo =}.  To test for an empty value, use
4685 @w{@code{ifeq ($(foo),)}}.  For example,
4687 @example
4688 bar =
4689 foo = $(bar)
4690 ifdef foo
4691 frobozz = yes
4692 else
4693 frobozz = no
4694 endif
4695 @end example
4697 @noindent
4698 sets @samp{frobozz} to @samp{yes}, while:
4700 @example
4701 foo =
4702 ifdef foo
4703 frobozz = yes
4704 else
4705 frobozz = no
4706 endif
4707 @end example
4709 @noindent
4710 sets @samp{frobozz} to @samp{no}.
4712 @item ifndef @var{variable-name}
4713 If the variable @var{variable-name} has an empty value, the
4714 @var{text-if-true} is effective; otherwise, the @var{text-if-false},
4715 if any, is effective.
4716 @end table
4718 Extra spaces are allowed and ignored at the beginning of the conditional
4719 directive line, but a tab is not allowed.  (If the line begins with a tab,
4720 it will be considered a command for a rule.)  Aside from this, extra spaces
4721 or tabs may be inserted with no effect anywhere except within the directive
4722 name or within an argument.  A comment starting with @samp{#} may appear at
4723 the end of the line.
4725 The other two directives that play a part in a conditional are @code{else}
4726 and @code{endif}.  Each of these directives is written as one word, with no
4727 arguments.  Extra spaces are allowed and ignored at the beginning of the
4728 line, and spaces or tabs at the end.  A comment starting with @samp{#} may
4729 appear at the end of the line.
4731 Conditionals affect which lines of the makefile @code{make} uses.  If
4732 the condition is true, @code{make} reads the lines of the
4733 @var{text-if-true} as part of the makefile; if the condition is false,
4734 @code{make} ignores those lines completely.  It follows that syntactic
4735 units of the makefile, such as rules, may safely be split across the
4736 beginning or the end of the conditional.@refill
4738 @code{make} evaluates conditionals when it reads a makefile.
4739 Consequently, you cannot use automatic variables in the tests of
4740 conditionals because they are not defined until commands are run
4741 (@pxref{Automatic, , Automatic Variables}).
4743 To prevent intolerable confusion, it is not permitted to start a
4744 conditional in one makefile and end it in another.  However, you may
4745 write an @code{include} directive within a conditional, provided you do
4746 not attempt to terminate the conditional inside the included file.
4748 @node Testing Flags,  , Conditional Syntax, Conditionals
4749 @section Conditionals that Test Flags
4751 You can write a conditional that tests @code{make} command flags such as
4752 @samp{-t} by using the variable @code{MAKEFLAGS} together with the
4753 @code{findstring} function 
4754 (@pxref{Text Functions, , Functions for String Substitution and Analysis}).
4755 This is useful when @code{touch} is not enough to make a file appear up
4756 to date.
4758 The @code{findstring} function determines whether one string appears as a
4759 substring of another.  If you want to test for the @samp{-t} flag,
4760 use @samp{t} as the first string and the value of @code{MAKEFLAGS} as
4761 the other.
4763 For example, here is how to arrange to use @samp{ranlib -t} to finish
4764 marking an archive file up to date:
4766 @example
4767 archive.a: @dots{}
4768 ifneq (,$(findstring t,$(MAKEFLAGS)))
4769         +touch archive.a
4770         +ranlib -t archive.a
4771 else
4772         ranlib archive.a
4773 endif
4774 @end example
4776 @noindent
4777 The @samp{+} prefix marks those command lines as ``recursive'' so
4778 that they will be executed despite use of the @samp{-t} flag.
4779 @xref{Recursion, ,Recursive Use of @code{make}}.
4781 @node Functions, Running, Conditionals, Top
4782 @chapter Functions for Transforming Text
4783 @cindex functions
4785 @dfn{Functions} allow you to do text processing in the makefile to compute
4786 the files to operate on or the commands to use.  You use a function in a
4787 @dfn{function call}, where you give the name of the function and some text
4788 (the @dfn{arguments}) for the function to operate on.  The result of the
4789 function's processing is substituted into the makefile at the point of the
4790 call, just as a variable might be substituted.
4792 @menu
4793 * Syntax of Functions::         How to write a function call.
4794 * Text Functions::              General-purpose text manipulation functions.
4795 * Filename Functions::          Functions for manipulating file names.
4796 * Foreach Function::            Repeat some text with controlled variation.
4797 * Origin Function::             Find where a variable got its value.
4798 * Shell Function::              Substitute the output of a shell command.
4799 @end menu
4801 @node Syntax of Functions, Text Functions,  , Functions
4802 @section Function Call Syntax
4803 @cindex @code{$}, in function call
4804 @cindex dollar sign (@code{$}), in function call
4805 @cindex arguments of functions
4806 @cindex functions, syntax of
4808 A function call resembles a variable reference.  It looks like this:
4810 @example
4811 $(@var{function} @var{arguments})
4812 @end example
4814 @noindent
4815 or like this:
4817 @example
4818 $@{@var{function} @var{arguments}@}
4819 @end example
4821 Here @var{function} is a function name; one of a short list of names that
4822 are part of @code{make}.  There is no provision for defining new functions.
4824 The @var{arguments} are the arguments of the function.  They are
4825 separated from the function name by one or more spaces or tabs, and if
4826 there is more than one argument, then they are separated by commas.
4827 Such whitespace and commas are not part of an argument's value.  The
4828 delimiters which you use to surround the function call, whether
4829 parentheses or braces, can appear in an argument only in matching pairs;
4830 the other kind of delimiters may appear singly.  If the arguments
4831 themselves contain other function calls or variable references, it is
4832 wisest to use the same kind of delimiters for all the references; write
4833 @w{@samp{$(subst a,b,$(x))}}, not @w{@samp{$(subst a,b,$@{x@})}}.  This
4834 is because it is clearer, and because only one type of delimiter is
4835 matched to find the end of the reference.
4837 The text written for each argument is processed by substitution of
4838 variables and function calls to produce the argument value, which
4839 is the text on which the function acts.  The substitution is done in the
4840 order in which the arguments appear.
4842 Commas and unmatched parentheses or braces cannot appear in the text of an
4843 argument as written; leading spaces cannot appear in the text of the first
4844 argument as written.  These characters can be put into the argument value
4845 by variable substitution.  First define variables @code{comma} and
4846 @code{space} whose values are isolated comma and space characters, then
4847 substitute these variables where such characters are wanted, like this:
4849 @example
4850 @group
4851 comma:= ,
4852 empty:=
4853 space:= $(empty) $(empty)
4854 foo:= a b c
4855 bar:= $(subst $(space),$(comma),$(foo))
4856 # @r{bar is now `a,b,c'.}
4857 @end group
4858 @end example
4860 @noindent
4861 Here the @code{subst} function replaces each space with a comma, through
4862 the value of @code{foo}, and substitutes the result.
4864 @node Text Functions, Filename Functions, Syntax of Functions, Functions
4865 @section Functions for String Substitution and Analysis
4866 @cindex functions, for text
4868 Here are some functions that operate on strings:
4870 @table @code
4871 @item $(subst @var{from},@var{to},@var{text})
4872 @findex subst
4873 Performs a textual replacement on the text @var{text}: each occurrence
4874 of @var{from} is replaced by @var{to}.  The result is substituted for
4875 the function call.  For example,
4877 @example
4878 $(subst ee,EE,feet on the street)
4879 @end example
4881 substitutes the string @samp{fEEt on the strEEt}.
4883 @item $(patsubst @var{pattern},@var{replacement},@var{text})
4884 @findex patsubst
4885 Finds whitespace-separated words in @var{text} that match
4886 @var{pattern} and replaces them with @var{replacement}.  Here
4887 @var{pattern} may contain a @samp{%} which acts as a wildcard,
4888 matching any number of any characters within a word.  If
4889 @var{replacement} also contains a @samp{%}, the @samp{%} is replaced
4890 by the text that matched the @samp{%} in @var{pattern}.@refill
4892 @cindex @code{%}, quoting in @code{patsubst}
4893 @cindex @code{%}, quoting with @code{\} (backslash)
4894 @cindex @code{\} (backslash), to quote @code{%}
4895 @cindex backslash (@code{\}), to quote @code{%}
4896 @cindex quoting @code{%}, in @code{patsubst}
4897 @samp{%} characters in @code{patsubst} function invocations can be
4898 quoted with preceding backslashes (@samp{\}).  Backslashes that would
4899 otherwise quote @samp{%} characters can be quoted with more backslashes.
4900 Backslashes that quote @samp{%} characters or other backslashes are
4901 removed from the pattern before it is compared file names or has a stem
4902 substituted into it.  Backslashes that are not in danger of quoting
4903 @samp{%} characters go unmolested.  For example, the pattern
4904 @file{the\%weird\\%pattern\\} has @samp{the%weird\} preceding the
4905 operative @samp{%} character, and @samp{pattern\\} following it.  The
4906 final two backslashes are left alone because they cannot affect any
4907 @samp{%} character.@refill
4909 Whitespace between words is folded into single space characters;
4910 leading and trailing whitespace is discarded.
4912 For example,
4914 @example
4915 $(patsubst %.c,%.o,x.c.c bar.c)
4916 @end example
4918 @noindent
4919 produces the value @samp{x.c.o bar.o}.
4921 Substitution references (@pxref{Substitution Refs, ,Substitution
4922 References}) are a simpler way to get the effect of the @code{patsubst}
4923 function:
4925 @example
4926 $(@var{var}:@var{pattern}=@var{replacement})  
4927 @end example
4929 @noindent
4930 is equivalent to 
4932 @example
4933 $(patsubst @var{pattern},@var{replacement},$(@var{var}))
4934 @end example
4936 The second shorthand simplifies one of the most common uses of
4937 @code{patsubst}: replacing the suffix at the end of file names.
4939 @example
4940 $(@var{var}:@var{suffix}=@var{replacement}) 
4941 @end example
4943 @noindent
4944 is equivalent to 
4946 @example
4947 $(patsubst %@var{suffix},%@var{replacement},$(@var{var}))
4948 @end example
4950 @noindent
4951 For example, you might have a list of object files:
4953 @example
4954 objects = foo.o bar.o baz.o
4955 @end example
4957 @noindent
4958 To get the list of corresponding source files, you could simply write:
4960 @example
4961 $(objects:.o=.c)
4962 @end example
4964 @noindent
4965 instead of using the general form:
4967 @example
4968 $(patsubst %.o,%.c,$(objects))
4969 @end example
4971 @item $(strip @var{string})
4972 @cindex stripping whitespace
4973 @cindex whitespace, stripping
4974 @cindex spaces, stripping
4975 @findex strip
4976 Removes leading and trailing whitespace from @var{string} and replaces
4977 each internal sequence of one or more whitespace characters with a
4978 single space.  Thus, @samp{$(strip a b  c )} results in @w{@samp{a b c}}.
4980 The function @code{strip} can be very useful when used in conjunction
4981 with conditionals.  When comparing something with the empty string
4982 @samp{} using @code{ifeq} or @code{ifneq}, you usually want a string of
4983 just whitespace to match the empty string (@pxref{Conditionals}).
4985 Thus, the following may fail to have the desired results:
4987 @example
4988 .PHONY: all
4989 ifneq   "$(needs_made)" ""
4990 all: $(needs_made)
4991 else
4992 all:;@@echo 'Nothing to make!'
4993 endif
4994 @end example
4996 @noindent
4997 Replacing the variable reference @w{@samp{$(needs_made)}} with the
4998 function call @w{@samp{$(strip $(needs_made))}} in the @code{ifneq}
4999 directive would make it more robust.@refill
5001 @item $(findstring @var{find},@var{in})
5002 @findex findstring
5003 @cindex searching for strings
5004 @cindex finding strings
5005 @cindex strings, searching for
5006 Searches @var{in} for an occurrence of @var{find}.  If it occurs, the
5007 value is @var{find}; otherwise, the value is empty.  You can use this
5008 function in a conditional to test for the presence of a specific
5009 substring in a given string.  Thus, the two examples,
5011 @example
5012 $(findstring a,a b c)
5013 $(findstring a,b c)
5014 @end example
5016 @noindent
5017 produce the values @samp{a} and @samp{} (the empty string),
5018 respectively.  @xref{Testing Flags}, for a practical application of
5019 @code{findstring}.@refill
5021 @need 750
5022 @findex filter
5023 @cindex filtering words
5024 @cindex words, filtering
5025 @item $(filter @var{pattern}@dots{},@var{text})
5026 Removes all whitespace-separated words in @var{text} that do
5027 @emph{not} match any of the @var{pattern} words, returning only
5028 matching words.  The patterns are written using @samp{%}, just like
5029 the patterns used in the @code{patsubst} function above.@refill
5031 The @code{filter} function can be used to separate out different types
5032 of strings (such as file names) in a variable.  For example:
5034 @example
5035 sources := foo.c bar.c baz.s ugh.h
5036 foo: $(sources)
5037         cc $(filter %.c %.s,$(sources)) -o foo
5038 @end example
5040 @noindent
5041 says that @file{foo} depends of @file{foo.c}, @file{bar.c},
5042 @file{baz.s} and @file{ugh.h} but only @file{foo.c}, @file{bar.c} and
5043 @file{baz.s} should be specified in the command to the
5044 compiler.@refill
5046 @item $(filter-out @var{pattern}@dots{},@var{text})
5047 @findex filter-out
5048 @cindex filtering out words
5049 @cindex words, filtering out
5050 Removes all whitespace-separated words in @var{text} that @emph{do}
5051 match the @var{pattern} words, returning only the words that @emph{do
5052 not} match.  This is the exact opposite of the @code{filter}
5053 function.@refill
5055 For example, given:
5057 @example
5058 @group
5059 objects=main1.o foo.o main2.o bar.o
5060 mains=main1.o main2.o
5061 @end group
5062 @end example
5064 @noindent
5065 the following generates a list which contains all the object files not
5066 in @samp{mains}:
5068 @example
5069 $(filter-out $(mains),$(objects))
5070 @end example
5072 @need 1500
5073 @findex sort
5074 @cindex sorting words
5075 @item $(sort @var{list})
5076 Sorts the words of @var{list} in lexical order, removing duplicate
5077 words.  The output is a list of words separated by single spaces.
5078 Thus,
5080 @example
5081 $(sort foo bar lose)
5082 @end example
5084 @noindent
5085 returns the value @samp{bar foo lose}.
5087 @cindex removing duplicate words
5088 @cindex duplicate words, removing
5089 @cindex words, removing duplicates
5090 Incidentally, since @code{sort} removes duplicate words, you can use
5091 it for this purpose even if you don't care about the sort order.
5092 @end table
5094 Here is a realistic example of the use of @code{subst} and
5095 @code{patsubst}.  Suppose that a makefile uses the @code{VPATH} variable
5096 to specify a list of directories that @code{make} should search for
5097 dependency files 
5098 (@pxref{General Search, , @code{VPATH} Search Path for All Dependencies}).  
5099 This example shows how to
5100 tell the C compiler to search for header files in the same list of
5101 directories.@refill
5103 The value of @code{VPATH} is a list of directories separated by colons,
5104 such as @samp{src:../headers}.  First, the @code{subst} function is used to
5105 change the colons to spaces:
5107 @example
5108 $(subst :, ,$(VPATH))
5109 @end example
5111 @noindent
5112 This produces @samp{src ../headers}.  Then @code{patsubst} is used to turn
5113 each directory name into a @samp{-I} flag.  These can be added to the
5114 value of the variable @code{CFLAGS}, which is passed automatically to the C
5115 compiler, like this:
5117 @example
5118 override CFLAGS += $(patsubst %,-I%,$(subst :, ,$(VPATH)))
5119 @end example
5121 @noindent
5122 The effect is to append the text @samp{-Isrc -I../headers} to the
5123 previously given value of @code{CFLAGS}.  The @code{override} directive is
5124 used so that the new value is assigned even if the previous value of
5125 @code{CFLAGS} was specified with a command argument (@pxref{Override
5126 Directive, , The @code{override} Directive}).
5128 @node Filename Functions, Foreach Function, Text Functions, Functions
5129 @section Functions for File Names
5130 @cindex functions, for file names
5131 @cindex file name functions
5133 Several of the built-in expansion functions relate specifically to
5134 taking apart file names or lists of file names.
5136 Each of the following functions performs a specific transformation on a
5137 file name.  The argument of the function is regarded as a series of file
5138 names, separated by whitespace.  (Leading and trailing whitespace is
5139 ignored.)  Each file name in the series is transformed in the same way and
5140 the results are concatenated with single spaces between them.
5142 @table @code
5143 @item $(dir @var{names}@dots{})
5144 @findex dir
5145 @cindex directory part
5146 @cindex file name, directory part
5147 Extracts the directory-part of each file name in @var{names}.  The
5148 directory-part of the file name is everything up through (and
5149 including) the last slash in it.  If the file name contains no slash,
5150 the directory part is the string @samp{./}.  For example,
5152 @example
5153 $(dir src/foo.c hacks)
5154 @end example
5156 @noindent
5157 produces the result @samp{src/ ./}.
5159 @item $(notdir @var{names}@dots{})
5160 @findex notdir
5161 @cindex file name, nondirectory part
5162 @cindex nondirectory part
5163 Extracts all but the directory-part of each file name in @var{names}.
5164 If the file name contains no slash, it is left unchanged.  Otherwise,
5165 everything through the last slash is removed from it.
5167 A file name that ends with a slash becomes an empty string.  This is
5168 unfortunate, because it means that the result does not always have the
5169 same number of whitespace-separated file names as the argument had;
5170 but we do not see any other valid alternative.
5172 For example,
5174 @example
5175 $(notdir src/foo.c hacks)
5176 @end example
5178 @noindent
5179 produces the result @samp{foo.c hacks}.
5181 @item $(suffix @var{names}@dots{})
5182 @findex suffix
5183 @cindex suffix, function to find
5184 @cindex file name suffix
5185 Extracts the suffix of each file name in @var{names}.  If the file name
5186 contains a period, the suffix is everything starting with the last
5187 period.  Otherwise, the suffix is the empty string.  This frequently
5188 means that the result will be empty when @var{names} is not, and if
5189 @var{names} contains multiple file names, the result may contain fewer
5190 file names.
5192 For example,
5194 @example
5195 $(suffix src/foo.c hacks)
5196 @end example
5198 @noindent
5199 produces the result @samp{.c}.
5201 @item $(basename @var{names}@dots{})
5202 @findex basename
5203 @cindex basename
5204 @cindex file name, basename of
5205 Extracts all but the suffix of each file name in @var{names}.  If the
5206 file name contains a period, the basename is everything starting up to
5207 (and not including) the last period.  Otherwise, the basename is the
5208 entire file name.  For example,
5210 @example
5211 $(basename src/foo.c hacks)
5212 @end example
5214 @noindent
5215 produces the result @samp{src/foo hacks}.
5217 @c plural convention with dots (be consistent)
5218 @item $(addsuffix @var{suffix},@var{names}@dots{})
5219 @findex addsuffix
5220 @cindex suffix, adding
5221 @cindex file name suffix, adding
5222 The argument @var{names} is regarded as a series of names, separated
5223 by whitespace; @var{suffix} is used as a unit.  The value of
5224 @var{suffix} is appended to the end of each individual name and the
5225 resulting larger names are concatenated with single spaces between
5226 them.  For example,
5228 @example
5229 $(addsuffix .c,foo bar)
5230 @end example
5232 @noindent
5233 produces the result @samp{foo.c bar.c}.
5235 @item $(addprefix @var{prefix},@var{names}@dots{})
5236 @findex addprefix
5237 @cindex prefix, adding
5238 @cindex file name prefix, adding
5239 The argument @var{names} is regarded as a series of names, separated
5240 by whitespace; @var{prefix} is used as a unit.  The value of
5241 @var{prefix} is prepended to the front of each individual name and the
5242 resulting larger names are concatenated with single spaces between
5243 them.  For example,
5245 @example
5246 $(addprefix src/,foo bar)
5247 @end example
5249 @noindent
5250 produces the result @samp{src/foo src/bar}.
5252 @item $(join @var{list1},@var{list2})
5253 @findex join
5254 @cindex joining lists of words
5255 @cindex words, joining lists
5256 Concatenates the two arguments word by word: the two first words (one
5257 from each argument) concatenated form the first word of the result, the
5258 two second words form the second word of the result, and so on.  So the
5259 @var{n}th word of the result comes from the @var{n}th word of each
5260 argument.  If one argument has more words that the other, the extra
5261 words are copied unchanged into the result.
5263 For example, @samp{$(join a b,.c .o)} produces @samp{a.c b.o}.
5265 Whitespace between the words in the lists is not preserved; it is
5266 replaced with a single space.
5268 This function can merge the results of the @code{dir} and
5269 @code{notdir} functions, to produce the original list of files which
5270 was given to those two functions.@refill
5272 @item $(word @var{n},@var{text})
5273 @findex word
5274 @cindex words, selecting
5275 @cindex selecting words
5276 Returns the @var{n}th word of @var{text}.  The legitimate values of
5277 @var{n} start from 1.  If @var{n} is bigger than the number of words
5278 in @var{text}, the value is empty.  For example,
5280 @example
5281 $(word 2, foo bar baz)
5282 @end example
5284 @noindent
5285 returns @samp{bar}.
5287 @c Following item phrased to prevent overfull hbox.  --RJC 17 Jul 92
5288 @item $(words @var{text})
5289 @findex words
5290 @cindex words, finding number
5291 Returns the number of words in @var{text}.  
5292 Thus, the last word of @var{text} is
5293 @w{@code{$(word $(words @var{text}),@var{text})}}.@refill
5295 @item $(firstword @var{names}@dots{})
5296 @findex firstword
5297 @cindex words, extracting first
5298 The argument @var{names} is regarded as a series of names, separated
5299 by whitespace.  The value is the first name in the series.  The rest
5300 of the names are ignored.
5302 For example,
5304 @example
5305 $(firstword foo bar)
5306 @end example
5308 @noindent
5309 produces the result @samp{foo}.  Although @code{$(firstword
5310 @var{text})} is the same as @code{$(word 1,@var{text})}, the
5311 @code{firstword} function is retained for its simplicity.@refill
5313 @item $(wildcard @var{pattern})
5314 @findex wildcard
5315 @cindex wildcard, function
5316 The argument @var{pattern} is a file name pattern, typically containing
5317 wildcard characters (as in shell file name patterns).  The result of
5318 @code{wildcard} is a space-separated list of the names of existing files
5319 that match the pattern.
5320 @xref{Wildcards, ,Using Wildcard Characters in File Names}.
5321 @end table
5323 @node Foreach Function, Origin Function, Filename Functions, Functions
5324 @section The @code{foreach} Function
5325 @findex foreach
5326 @cindex words, iterating over
5328 The @code{foreach} function is very different from other functions.  It
5329 causes one piece of text to be used repeatedly, each time with a different
5330 substitution performed on it.  It resembles the @code{for} command in the
5331 shell @code{sh} and the @code{foreach} command in the C-shell @code{csh}.
5333 The syntax of the @code{foreach} function is:
5335 @example
5336 $(foreach @var{var},@var{list},@var{text})
5337 @end example
5339 @noindent
5340 The first two arguments, @var{var} and @var{list}, are expanded before
5341 anything else is done; note that the last argument, @var{text}, is
5342 @strong{not} expanded at the same time.  Then for each word of the expanded
5343 value of @var{list}, the variable named by the expanded value of @var{var}
5344 is set to that word, and @var{text} is expanded.  Presumably @var{text}
5345 contains references to that variable, so its expansion will be different
5346 each time.
5348 The result is that @var{text} is expanded as many times as there are
5349 whitespace-separated words in @var{list}.  The multiple expansions of
5350 @var{text} are concatenated, with spaces between them, to make the result
5351 of @code{foreach}.
5353 This simple example sets the variable @samp{files} to the list of all files
5354 in the directories in the list @samp{dirs}:
5356 @example
5357 dirs := a b c d
5358 files := $(foreach dir,$(dirs),$(wildcard $(dir)/*))
5359 @end example
5361 Here @var{text} is @samp{$(wildcard $(dir)/*)}.  The first repetition
5362 finds the value @samp{a} for @code{dir}, so it produces the same result
5363 as @samp{$(wildcard a/*)}; the second repetition produces the result
5364 of @samp{$(wildcard b/*)}; and the third, that of @samp{$(wildcard c/*)}.
5366 This example has the same result (except for setting @samp{dirs}) as
5367 the following example:
5369 @example
5370 files := $(wildcard a/* b/* c/* d/*)
5371 @end example
5373 When @var{text} is complicated, you can improve readability by giving it
5374 a name, with an additional variable:
5376 @example
5377 find_files = $(wildcard $(dir)/*)
5378 dirs := a b c d
5379 files := $(foreach dir,$(dirs),$(find_files))
5380 @end example
5382 @noindent
5383 Here we use the variable @code{find_files} this way.  We use plain @samp{=}
5384 to define a recursively-expanding variable, so that its value contains an
5385 actual function call to be reexpanded under the control of @code{foreach};
5386 a simply-expanded variable would not do, since @code{wildcard} would be
5387 called only once at the time of defining @code{find_files}.
5389 The @code{foreach} function has no permanent effect on the variable
5390 @var{var}; its value and flavor after the @code{foreach} function call are
5391 the same as they were beforehand.  The other values which are taken from
5392 @var{list} are in effect only temporarily, during the execution of
5393 @code{foreach}.  The variable @var{var} is a simply-expanded variable
5394 during the execution of @code{foreach}.  If @var{var} was undefined
5395 before the @code{foreach} function call, it is undefined after the call.
5396 @xref{Flavors, ,The Two Flavors of Variables}.@refill
5398 You must take care when using complex variable expressions that result in
5399 variable names because many strange things are valid variable names, but
5400 are probably not what you intended.  For example,
5402 @smallexample
5403 files := $(foreach Es escrito en espanol!,b c ch,$(find_files))
5404 @end smallexample
5406 @noindent
5407 might be useful if the value of @code{find_files} references the variable
5408 whose name is @samp{Es escrito en espanol!} (es un nombre bastante largo,
5409 no?), but it is more likely to be a mistake.
5411 @node Origin Function, Shell Function, Foreach Function, Functions
5412 @section The @code{origin} Function
5413 @findex origin
5414 @cindex variables, origin of
5415 @cindex origin of variable
5417 The @code{origin} function is unlike most other functions in that it does
5418 not operate on the values of variables; it tells you something @emph{about}
5419 a variable.  Specifically, it tells you where it came from.
5421 The syntax of the @code{origin} function is:
5423 @example
5424 $(origin @var{variable})
5425 @end example
5427 Note that @var{variable} is the @emph{name} of a variable to inquire about;
5428 not a @emph{reference} to that variable.  Therefore you would not normally
5429 use a @samp{$} or parentheses when writing it.  (You can, however, use a
5430 variable reference in the name if you want the name not to be a constant.)
5432 The result of this function is a string telling you how the variable
5433 @var{variable} was defined:
5435 @table @samp
5436 @item undefined
5438 if @var{variable} was never defined.
5440 @item default
5442 if @var{variable} has a default definition, as is usual with @code{CC}
5443 and so on.  @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
5444 Note that if you have redefined a default variable, the @code{origin}
5445 function will return the origin of the later definition.
5447 @item environment
5449 if @var{variable} was defined as an environment variable and the
5450 @samp{-e} option is @emph{not} turned on (@pxref{Options Summary, ,Summary of Options}).
5452 @item environment override
5454 if @var{variable} was defined as an environment variable and the
5455 @w{@samp{-e}} option @emph{is} turned on (@pxref{Options Summary,
5456 ,Summary of Options}).@refill
5458 @item file
5460 if @var{variable} was defined in a makefile.
5462 @item command line
5464 if @var{variable} was defined on the command line.
5466 @item override
5468 if @var{variable} was defined with an @code{override} directive in a
5469 makefile (@pxref{Override Directive, ,The @code{override} Directive}).
5471 @item automatic
5473 if @var{variable} is an automatic variable defined for the
5474 execution of the commands for each rule
5475 (@pxref{Automatic, , Automatic Variables}).
5476 @end table
5478 This information is primarily useful (other than for your curiosity) to
5479 determine if you want to believe the value of a variable.  For example,
5480 suppose you have a makefile @file{foo} that includes another makefile
5481 @file{bar}.  You want a variable @code{bletch} to be defined in @file{bar}
5482 if you run the command @w{@samp{make -f bar}}, even if the environment contains
5483 a definition of @code{bletch}.  However, if @file{foo} defined
5484 @code{bletch} before including @file{bar}, you do not want to override that
5485 definition.  This could be done by using an @code{override} directive in
5486 @file{foo}, giving that definition precedence over the later definition in
5487 @file{bar}; unfortunately, the @code{override} directive would also
5488 override any command line definitions.  So, @file{bar} could
5489 include:@refill
5491 @example
5492 @group
5493 ifdef bletch
5494 ifeq "$(origin bletch)" "environment"
5495 bletch = barf, gag, etc.
5496 endif
5497 endif
5498 @end group
5499 @end example
5501 @noindent
5502 If @code{bletch} has been defined from the environment, this will redefine
5505 If you want to override a previous definition of @code{bletch} if it came
5506 from the environment, even under @samp{-e}, you could instead write:
5508 @example
5509 @group
5510 ifneq "$(findstring environment,$(origin bletch))" ""
5511 bletch = barf, gag, etc.
5512 endif
5513 @end group
5514 @end example
5516 Here the redefinition takes place if @samp{$(origin bletch)} returns either
5517 @samp{environment} or @samp{environment override}.
5518 @xref{Text Functions, , Functions for String Substitution and Analysis}.
5520 @node Shell Function,  , Origin Function, Functions
5521 @section The @code{shell} Function
5522 @findex shell
5523 @cindex commands, expansion
5524 @cindex backquotes
5525 @cindex shell command, function for
5527 The @code{shell} function is unlike any other function except the
5528 @code{wildcard} function 
5529 (@pxref{Wildcard Function, ,The Function @code{wildcard}}) in that it
5530 communicates with the world outside of @code{make}.
5532 The @code{shell} function performs the same function that backquotes
5533 (@samp{`}) perform in most shells: it does @dfn{command expansion}.  This
5534 means that it takes an argument that is a shell command and returns the
5535 output of the command.  The only processing @code{make} does on the result,
5536 before substituting it into the surrounding text, is to convert newlines to
5537 spaces.@refill
5539 The commands run by calls to the @code{shell} function are run when the
5540 function calls are expanded.  In most cases, this is when the makefile is
5541 read in.  The exception is that function calls in the commands of the rules
5542 are expanded when the commands are run, and this applies to @code{shell}
5543 function calls like all others.
5545 Here are some examples of the use of the @code{shell} function:
5547 @example
5548 contents := $(shell cat foo)
5549 @end example
5551 @noindent
5552 sets @code{contents} to the contents of the file @file{foo}, with a space
5553 (rather than a newline) separating each line.
5555 @example
5556 files := $(shell echo *.c)
5557 @end example
5559 @noindent
5560 sets @code{files} to the expansion of @samp{*.c}.  Unless @code{make} is
5561 using a very strange shell, this has the same result as
5562 @w{@samp{$(wildcard *.c)}}.@refill
5564 @node Running, Implicit Rules, Functions, Top
5565 @chapter How to Run @code{make}
5567 A makefile that says how to recompile a program can be used in more
5568 than one way.  The simplest use is to recompile every file that is out
5569 of date.  Usually, makefiles are written so that if you run
5570 @code{make} with no arguments, it does just that.
5572 But you might want to update only some of the files; you might want to use
5573 a different compiler or different compiler options; you might want just to
5574 find out which files are out of date without changing them.
5576 By giving arguments when you run @code{make}, you can do any of these
5577 things and many others.
5579 The exit status of @code{make} is always one of three values:
5580 @table @code
5581 @item 0
5582 The exit status is zero if @code{make} is successful.
5583 @item 2
5584 The exit status is two if @code{make} encounters any errors.
5585 It will print messages describing the particular errors.
5586 @item 1
5587 The exit status is one if you use the @samp{-q} flag and @code{make}
5588 determines that some target is not already up to date.
5589 @xref{Instead of Execution, ,Instead of Executing the Commands}.
5590 @end table
5592 @menu
5593 * Makefile Arguments::          How to specify which makefile to use.
5594 * Goals::                       How to use goal arguments to specify which 
5595                                   parts of the makefile to use.
5596 * Instead of Execution::        How to use mode flags to specify what
5597                                   kind of thing to do with the commands 
5598                                   in the makefile other than simply 
5599                                   execute them.
5600 * Avoiding Compilation::        How to avoid recompiling certain files.
5601 * Overriding::                  How to override a variable to specify 
5602                                   an alternate compiler and other things.
5603 * Testing::                     How to proceed past some errors, to 
5604                                   test compilation.
5605 * Options Summary::             Summary of Options
5606 @end menu
5608 @node Makefile Arguments, Goals,  , Running
5609 @section Arguments to Specify the Makefile
5610 @cindex @code{--file}
5611 @cindex @code{--makefile}
5612 @cindex @code{-f}
5614 The way to specify the name of the makefile is with the @samp{-f} or
5615 @samp{--file} option (@samp{--makefile} also works).  For example,
5616 @samp{-f altmake} says to use the file @file{altmake} as the makefile.
5618 If you use the @samp{-f} flag several times and follow each @samp{-f}
5619 with an argument, all the specified files are used jointly as
5620 makefiles.
5622 If you do not use the @samp{-f} or @samp{--file} flag, the default is
5623 to try @file{GNUmakefile}, @file{makefile}, and @file{Makefile}, in
5624 that order, and use the first of these three which exists or can be made
5625 (@pxref{Makefiles, ,Writing Makefiles}).@refill
5627 @node Goals, Instead of Execution, Makefile Arguments, Running
5628 @section Arguments to Specify the Goals
5629 @cindex goal, how to specify
5631 The @dfn{goals} are the targets that @code{make} should strive ultimately
5632 to update.  Other targets are updated as well if they appear as
5633 dependencies of goals, or dependencies of dependencies of goals, etc.
5635 By default, the goal is the first target in the makefile (not counting
5636 targets that start with a period).  Therefore, makefiles are usually
5637 written so that the first target is for compiling the entire program or
5638 programs they describe.  If the first rule in the makefile has several
5639 targets, only the first target in the rule becomes the default goal, not
5640 the whole list.
5642 You can specify a different goal or goals with arguments to @code{make}.
5643 Use the name of the goal as an argument.  If you specify several goals,
5644 @code{make} processes each of them in turn, in the order you name them.
5646 Any target in the makefile may be specified as a goal (unless it
5647 starts with @samp{-} or contains an @samp{=}, in which case it will be
5648 parsed as a switch or variable definition, respectively).  Even
5649 targets not in the makefile may be specified, if @code{make} can find
5650 implicit rules that say how to make them.
5652 One use of specifying a goal is if you want to compile only a part of
5653 the program, or only one of several programs.  Specify as a goal each
5654 file that you wish to remake.  For example, consider a directory containing
5655 several programs, with a makefile that starts like this:
5657 @example
5658 .PHONY: all
5659 all: size nm ld ar as
5660 @end example
5662 If you are working on the program @code{size}, you might want to say
5663 @w{@samp{make size}} so that only the files of that program are recompiled.
5665 Another use of specifying a goal is to make files that are not normally
5666 made.  For example, there may be a file of debugging output, or a
5667 version of the program that is compiled specially for testing, which has
5668 a rule in the makefile but is not a dependency of the default goal.
5670 Another use of specifying a goal is to run the commands associated with
5671 a phony target (@pxref{Phony Targets}) or empty target (@pxref{Empty
5672 Targets, ,Empty Target Files to Record Events}).  Many makefiles contain
5673 a phony target named @file{clean} which deletes everything except source
5674 files.  Naturally, this is done only if you request it explicitly with
5675 @w{@samp{make clean}}.  Following is a list of typical phony and empty
5676 target names.  @xref{Standard Targets}, for a detailed list of all the
5677 standard target names which GNU software packages use.
5679 @table @file
5680 @item all
5681 @cindex @code{all} @r{(standard target)}
5682 Make all the top-level targets the makefile knows about.
5684 @item clean
5685 @cindex @code{clean} @r{(standard target)}
5686 Delete all files that are normally created by running @code{make}.
5688 @item mostlyclean
5689 @cindex @code{mostlyclean} @r{(standard target)}
5690 Like @samp{clean}, but may refrain from deleting a few files that people
5691 normally don't want to recompile.  For example, the @samp{mostlyclean}
5692 target for GCC does not delete @file{libgcc.a}, because recompiling it
5693 is rarely necessary and takes a lot of time.
5695 @item distclean
5696 @cindex @code{distclean} @r{(standard target)}
5697 @itemx realclean
5698 @cindex @code{realclean} @r{(standard target)}
5699 @itemx clobber
5700 @cindex @code{clobber} @r{(standard target)}
5701 Any of these targets might be defined to delete @emph{more} files than
5702 @samp{clean} does.  For example, this would delete configuration files
5703 or links that you would normally create as preparation for compilation,
5704 even if the makefile itself cannot create these files.
5706 @item install
5707 @cindex @code{install} @r{(standard target)}
5708 Copy the executable file into a directory that users typically search
5709 for commands; copy any auxiliary files that the executable uses into
5710 the directories where it will look for them.
5712 @item print
5713 @cindex @code{print} @r{(standard target)}
5714 Print listings of the source files that have changed.
5716 @item tar
5717 @cindex @code{tar} @r{(standard target)}
5718 Create a tar file of the source files.
5720 @item shar
5721 @cindex @code{shar} @r{(standard target)}
5722 Create a shell archive (shar file) of the source files.
5724 @item dist
5725 @cindex @code{dist} @r{(standard target)}
5726 Create a distribution file of the source files.  This might
5727 be a tar file, or a shar file, or a compressed version of one of the
5728 above, or even more than one of the above.
5730 @item TAGS
5731 @cindex @code{TAGS} @r{(standard target)}
5732 Update a tags table for this program.
5734 @item check
5735 @cindex @code{check} @r{(standard target)}
5736 @itemx test
5737 @cindex @code{test} @r{(standard target)}
5738 Perform self tests on the program this makefile builds.
5739 @end table
5741 @node Instead of Execution, Avoiding Compilation, Goals, Running
5742 @section Instead of Executing the Commands
5743 @cindex execution, instead of
5744 @cindex commands, instead of executing
5746 The makefile tells @code{make} how to tell whether a target is up to date,
5747 and how to update each target.  But updating the targets is not always
5748 what you want.  Certain options specify other activities for @code{make}.
5750 @comment Extra blank lines make it print better.
5751 @table @samp
5752 @item -n
5753 @itemx --just-print
5754 @itemx --dry-run
5755 @itemx --recon
5756 @cindex @code{--just-print}
5757 @cindex @code{--dry-run}
5758 @cindex @code{--recon}
5759 @cindex @code{-n}
5761 ``No-op''.  The activity is to print what commands would be used to make
5762 the targets up to date, but not actually execute them.
5764 @item -t
5765 @itemx --touch
5766 @cindex @code{--touch}
5767 @cindex touching files
5768 @cindex target, touching
5769 @cindex @code{-t}
5771 ``Touch''.  The activity is to mark the targets as up to date without
5772 actually changing them.  In other words, @code{make} pretends to compile
5773 the targets but does not really change their contents.
5775 @item -q
5776 @itemx --question
5777 @cindex @code{--question}
5778 @cindex @code{-q}
5779 @cindex question mode
5781 ``Question''.  The activity is to find out silently whether the targets
5782 are up to date already; but execute no commands in either case.  In other
5783 words, neither compilation nor output will occur.
5785 @item -W @var{file}
5786 @itemx --what-if=@var{file}
5787 @itemx --assume-new=@var{file}
5788 @itemx --new-file=@var{file}
5789 @cindex @code{--what-if}
5790 @cindex @code{-W}
5791 @cindex @code{--assume-new}
5792 @cindex @code{--new-file}
5793 @cindex what if
5794 @cindex files, assuming new
5796 ``What if''.  Each @samp{-W} flag is followed by a file name.  The given
5797 files' modification times are recorded by @code{make} as being the present
5798 time, although the actual modification times remain the same.  
5799 You can use the @samp{-W} flag in conjunction with the @samp{-n} flag
5800 to see what would happen if you were to modify specific files.@refill
5801 @end table
5803 With the @samp{-n} flag, @code{make} prints the commands that it would
5804 normally execute but does not execute them.
5806 With the @samp{-t} flag, @code{make} ignores the commands in the rules
5807 and uses (in effect) the command @code{touch} for each target that needs to
5808 be remade.  The @code{touch} command is also printed, unless @samp{-s} or
5809 @code{.SILENT} is used.  For speed, @code{make} does not actually invoke
5810 the program @code{touch}.  It does the work directly.
5812 With the @samp{-q} flag, @code{make} prints nothing and executes no
5813 commands, but the exit status code it returns is zero if and only if the
5814 targets to be considered are already up to date.  If the exit status is
5815 one, then some updating needs to be done.  If @code{make} encounters an
5816 error, the exit status is two, so you can distinguish an error from a
5817 target that is not up to date.
5819 It is an error to use more than one of these three flags in the same
5820 invocation of @code{make}.
5822 The @samp{-n}, @samp{-t}, and @samp{-q} options do not affect command
5823 lines that begin with @samp{+} characters or contain the strings
5824 @samp{$(MAKE)} or @samp{$@{MAKE@}}.  Note that only the line containing
5825 the @samp{+} character or the strings @samp{$(MAKE)} or @samp{$@{MAKE@}}
5826 is run regardless of these options.  Other lines in the same rule are
5827 not run unless they too begin with @samp{+} or contain @samp{$(MAKE)} or
5828 @samp{$@{MAKE@}} (@xref{MAKE Variable, ,How the @code{MAKE} Variable Works}.)
5830 The @samp{-W} flag provides two features:
5832 @itemize @bullet
5833 @item
5834 If you also use the @samp{-n} or @samp{-q} flag, you can see what
5835 @code{make} would do if you were to modify some files.
5837 @item
5838 Without the @samp{-n} or @samp{-q} flag, when @code{make} is actually
5839 executing commands, the @samp{-W} flag can direct @code{make} to act
5840 as if some files had been modified, without actually modifying the
5841 files.@refill
5842 @end itemize
5844 Note that the options @samp{-p} and @samp{-v} allow you to obtain other
5845 information about @code{make} or about the makefiles in use
5846 (@pxref{Options Summary, ,Summary of Options}).@refill
5848 @node Avoiding Compilation, Overriding, Instead of Execution, Running
5849 @section Avoiding Recompilation of Some Files
5850 @cindex @code{-o}
5851 @cindex @code{--old-file}
5852 @cindex @code{--assume-old}
5853 @cindex files, assuming old
5854 @cindex files, avoiding recompilation of
5855 @cindex recompilation, avoiding
5857 Sometimes you may have changed a source file but you do not want to
5858 recompile all the files that depend on it.  For example, suppose you add a
5859 macro or a declaration to a header file that many other files depend on.
5860 Being conservative, @code{make} assumes that any change in the header file
5861 requires recompilation of all dependent files, but you know that they do not
5862 need to be recompiled and you would rather not waste the time waiting for
5863 them to compile.
5865 If you anticipate the problem before changing the header file, you can
5866 use the @samp{-t} flag.  This flag tells @code{make} not to run the
5867 commands in the rules, but rather to mark the target up to date by
5868 changing its last-modification date.  You would follow this procedure:
5870 @enumerate
5871 @item
5872 Use the command @samp{make} to recompile the source files that really
5873 need recompilation.
5875 @item
5876 Make the changes in the header files.
5878 @item
5879 Use the command @samp{make -t} to mark all the object files as
5880 up to date.  The next time you run @code{make}, the changes in the
5881 header files will not cause any recompilation.
5882 @end enumerate
5884 If you have already changed the header file at a time when some files
5885 do need recompilation, it is too late to do this.  Instead, you can
5886 use the @w{@samp{-o @var{file}}} flag, which marks a specified file as
5887 ``old'' (@pxref{Options Summary, ,Summary of Options}).  This means
5888 that the file itself will not be remade, and nothing else will be
5889 remade on its account.  Follow this procedure:
5891 @enumerate
5892 @item
5893 Recompile the source files that need compilation for reasons independent
5894 of the particular header file, with @samp{make -o @var{headerfile}}.
5895 If several header files are involved, use a separate @samp{-o} option
5896 for each header file.
5898 @item
5899 Touch all the object files with @samp{make -t}.
5900 @end enumerate
5902 @node Overriding, Testing, Avoiding Compilation, Running
5903 @section Overriding Variables
5904 @cindex overriding variables with arguments
5905 @cindex variables, overriding with arguments
5906 @cindex command line variables
5907 @cindex variables, command line
5909 An argument that contains @samp{=} specifies the value of a variable:
5910 @samp{@var{v}=@var{x}} sets the value of the variable @var{v} to @var{x}.
5911 If you specify a value in this way, all ordinary assignments of the same
5912 variable in the makefile are ignored; we say they have been
5913 @dfn{overridden} by the command line argument.
5915 The most common way to use this facility is to pass extra flags to
5916 compilers.  For example, in a properly written makefile, the variable
5917 @code{CFLAGS} is included in each command that runs the C compiler, so a
5918 file @file{foo.c} would be compiled something like this:
5920 @example
5921 cc -c $(CFLAGS) foo.c
5922 @end example
5924 Thus, whatever value you set for @code{CFLAGS} affects each compilation
5925 that occurs.  The makefile probably specifies the usual value for
5926 @code{CFLAGS}, like this:
5928 @example
5929 CFLAGS=-g
5930 @end example
5932 Each time you run @code{make}, you can override this value if you
5933 wish.  For example, if you say @samp{make CFLAGS='-g -O'}, each C
5934 compilation will be done with @samp{cc -c -g -O}.  (This illustrates
5935 how you can use quoting in the shell to enclose spaces and other
5936 special characters in the value of a variable when you override it.)
5938 The variable @code{CFLAGS} is only one of many standard variables that
5939 exist just so that you can change them this way.  @xref{Implicit
5940 Variables, , Variables Used by Implicit Rules}, for a complete list.
5942 You can also program the makefile to look at additional variables of your
5943 own, giving the user the ability to control other aspects of how the
5944 makefile works by changing the variables.
5946 When you override a variable with a command argument, you can define either
5947 a recursively-expanded variable or a simply-expanded variable.  The
5948 examples shown above make a recursively-expanded variable; to make a
5949 simply-expanded variable, write @samp{:=} instead of @samp{=}.  But, unless
5950 you want to include a variable reference or function call in the
5951 @emph{value} that you specify, it makes no difference which kind of
5952 variable you create.
5954 There is one way that the makefile can change a variable that you have
5955 overridden.  This is to use the @code{override} directive, which is a line
5956 that looks like this: @samp{override @var{variable} = @var{value}}
5957 (@pxref{Override Directive, ,The @code{override} Directive}).
5959 @node Testing, Options Summary, Overriding, Running
5960 @section Testing the Compilation of a Program
5961 @cindex testing compilation
5962 @cindex compilation, testing
5964 Normally, when an error happens in executing a shell command, @code{make}
5965 gives up immediately, returning a nonzero status.  No further commands are
5966 executed for any target.  The error implies that the goal cannot be
5967 correctly remade, and @code{make} reports this as soon as it knows.
5969 When you are compiling a program that you have just changed, this is not
5970 what you want.  Instead, you would rather that @code{make} try compiling
5971 every file that can be tried, to show you as many compilation errors
5972 as possible.
5974 @cindex @code{-k}
5975 @cindex @code{--keep-going}
5976 On these occasions, you should use the @samp{-k} or
5977 @samp{--keep-going} flag.  This tells @code{make} to continue to
5978 consider the other dependencies of the pending targets, remaking them
5979 if necessary, before it gives up and returns nonzero status.  For
5980 example, after an error in compiling one object file, @samp{make -k}
5981 will continue compiling other object files even though it already
5982 knows that linking them will be impossible.  In addition to continuing
5983 after failed shell commands, @samp{make -k} will continue as much as
5984 possible after discovering that it does not know how to make a target
5985 or dependency file.  This will always cause an error message, but
5986 without @samp{-k}, it is a fatal error (@pxref{Options Summary,
5987 ,Summary of Options}).@refill
5989 The usual behavior of @code{make} assumes that your purpose is to get the
5990 goals up to date; once @code{make} learns that this is impossible, it might
5991 as well report the failure immediately.  The @samp{-k} flag says that the
5992 real purpose is to test as much as possible of the changes made in the
5993 program, perhaps to find several independent problems so that you can
5994 correct them all before the next attempt to compile.  This is why Emacs'
5995 @kbd{M-x compile} command passes the @samp{-k} flag by default.
5997 @node Options Summary,  , Testing, Running
5998 @section Summary of Options
5999 @cindex options
6000 @cindex flags
6001 @cindex switches
6003 Here is a table of all the options @code{make} understands:
6005 @table @samp
6006 @item -b
6007 @cindex @code{-b}
6008 @itemx -m
6009 @cindex @code{-m}
6010 These options are ignored for compatibility with other versions of @code{make}.
6012 @item -C @var{dir}
6013 @cindex @code{-C}
6014 @itemx --directory=@var{dir}
6015 @cindex @code{--directory}
6016 Change to directory @var{dir} before reading the makefiles.  If multiple
6017 @samp{-C} options are specified, each is interpreted relative to the
6018 previous one: @samp{-C / -C etc} is equivalent to @samp{-C /etc}.
6019 This is typically used with recursive invocations of @code{make}
6020 (@pxref{Recursion, ,Recursive Use of @code{make}}).
6022 @item -d
6023 @cindex @code{-d}
6024 @itemx --debug
6025 @cindex @code{--debug}
6026 @c Extra blank line here makes the table look better.
6028 Print debugging information in addition to normal processing.  The
6029 debugging information says which files are being considered for
6030 remaking, which file-times are being compared and with what results,
6031 which files actually need to be remade, which implicit rules are
6032 considered and which are applied---everything interesting about how
6033 @code{make} decides what to do.
6035 @item -e
6036 @cindex @code{-e}
6037 @itemx --environment-overrides
6038 @cindex @code{--environment-overrides}
6039 Give variables taken from the environment precedence
6040 over variables from makefiles.
6041 @xref{Environment, ,Variables from the Environment}.
6043 @item -f @var{file}
6044 @cindex @code{-f}
6045 @itemx --file=@var{file}
6046 @cindex @code{--file}
6047 @itemx --makefile=@var{file}
6048 @cindex @code{--makefile}
6049 Read the file named @var{file} as a makefile.
6050 @xref{Makefiles, ,Writing Makefiles}.
6052 @item -h
6053 @cindex @code{-h}
6054 @itemx --help
6055 @cindex @code{--help}
6056 @c Extra blank line here makes the table look better.
6058 Remind you of the options that @code{make} understands and then exit.
6060 @item -i
6061 @cindex @code{-i}
6062 @itemx --ignore-errors
6063 @cindex @code{--ignore-errors}
6064 Ignore all errors in commands executed to remake files.
6065 @xref{Errors, ,Errors in Commands}.
6067 @item -I @var{dir}
6068 @cindex @code{-I}
6069 @itemx --include-dir=@var{dir}
6070 @cindex @code{--include-dir}
6071 Specifies a directory @var{dir} to search for included makefiles.
6072 @xref{Include, ,Including Other Makefiles}.  If several @samp{-I}
6073 options are used to specify several directories, the directories are
6074 searched in the order specified.
6076 @item -j [@var{jobs}]
6077 @cindex @code{-j}
6078 @itemx --jobs=[@var{jobs}]
6079 @cindex @code{--jobs}
6080 Specifies the number of jobs (commands) to run simultaneously.  With no
6081 argument, @code{make} runs as many jobs simultaneously as possible.  If
6082 there is more than one @samp{-j} option, the last one is effective.
6083 @xref{Parallel, ,Parallel Execution},
6084 for more information on how commands are run.
6086 @item -k
6087 @cindex @code{-k}
6088 @itemx --keep-going
6089 @cindex @code{--keep-going}
6090 Continue as much as possible after an error.  While the target that
6091 failed, and those that depend on it, cannot be remade, the other
6092 dependencies of these targets can be processed all the same.
6093 @xref{Testing, ,Testing the Compilation of a Program}.
6095 @item -l [@var{load}]
6096 @cindex @code{-l}
6097 @itemx --load-average[=@var{load}]
6098 @cindex @code{--load-average}
6099 @itemx --max-load[=@var{load}]
6100 @cindex @code{--max-load}
6101 Specifies that no new jobs (commands) should be started if there are
6102 other jobs running and the load average is at least @var{load} (a
6103 floating-point number).  With no argument, removes a previous load
6104 limit.  @xref{Parallel, ,Parallel Execution}.
6106 @item -n
6107 @cindex @code{-n}
6108 @itemx --just-print
6109 @cindex @code{--just-print}
6110 @itemx --dry-run
6111 @cindex @code{--dry-run}
6112 @itemx --recon
6113 @cindex @code{--recon}
6114 @c Extra blank line here makes the table look better.
6116 Print the commands that would be executed, but do not execute them.
6117 @xref{Instead of Execution, ,Instead of Executing the Commands}.
6119 @item -o @var{file}
6120 @cindex @code{-o}
6121 @itemx --old-file=@var{file}
6122 @cindex @code{--old-file}
6123 @itemx --assume-old=@var{file}
6124 @cindex @code{--assume-old}
6125 Do not remake the file @var{file} even if it is older than its
6126 dependencies, and do not remake anything on account of changes in
6127 @var{file}.  Essentially the file is treated as very old and its rules
6128 are ignored.  @xref{Avoiding Compilation, ,Avoiding Recompilation of
6129 Some Files}.@refill
6131 @item -p
6132 @cindex @code{-p}
6133 @itemx --print-data-base
6134 @cindex @code{--print-data-base}
6135 Print the data base (rules and variable values) that results from
6136 reading the makefiles; then execute as usual or as otherwise
6137 specified.  This also prints the version information given by
6138 the @samp{-v} switch (see below).  To print the data base without
6139 trying to remake any files, use @w{@samp{make -p -f /dev/null}}.
6141 @item -q
6142 @cindex @code{-q}
6143 @itemx --question
6144 @cindex @code{--question}
6145 ``Question mode''.  Do not run any commands, or print anything; just
6146 return an exit status that is zero if the specified targets are already
6147 up to date, one if any remaking is required, or two if an error is
6148 encountered.  @xref{Instead of Execution, ,Instead of Executing the
6149 Commands}.@refill
6151 @item -r
6152 @cindex @code{-r}
6153 @itemx --no-builtin-rules
6154 @cindex @code{--no-builtin-rules}
6155 Eliminate use of the built-in implicit rules (@pxref{Implicit Rules,
6156 ,Using Implicit Rules}).  You can still define your own by writing
6157 pattern rules (@pxref{Pattern Rules, ,Defining and Redefining Pattern
6158 Rules}).  The @samp{-r} option also clears out the default list of
6159 suffixes for suffix rules (@pxref{Suffix Rules, ,Old-Fashioned Suffix
6160 Rules}).  But you can still define your own suffixes with a rule for
6161 @code{.SUFFIXES}, and then define your own suffix rules.
6163 @item -s
6164 @cindex @code{-s}
6165 @itemx --silent
6166 @cindex @code{--silent}
6167 @itemx --quiet
6168 @cindex @code{--quiet}
6169 @c Extra blank line here makes the table look better.
6171 Silent operation; do not print the commands as they are executed.
6172 @xref{Echoing, ,Command Echoing}.
6174 @item -S
6175 @cindex @code{-S}
6176 @itemx --no-keep-going
6177 @cindex @code{--no-keep-going}
6178 @itemx --stop
6179 @cindex @code{--stop}
6180 @c Extra blank line here makes the table look better.
6182 Cancel the effect of the @samp{-k} option.  This is never necessary
6183 except in a recursive @code{make} where @samp{-k} might be inherited
6184 from the top-level @code{make} via @code{MAKEFLAGS}
6185 (@pxref{Recursion, ,Recursive Use of @code{make}})
6186 or if you set @samp{-k} in @code{MAKEFLAGS} in your environment.@refill
6188 @item -t
6189 @cindex @code{-t}
6190 @itemx --touch
6191 @cindex @code{--touch}
6192 @c Extra blank line here makes the table look better.
6194 Touch files (mark them up to date without really changing them)
6195 instead of running their commands.  This is used to pretend that the
6196 commands were done, in order to fool future invocations of
6197 @code{make}.  @xref{Instead of Execution, ,Instead of Executing the Commands}.
6199 @item -v
6200 @cindex @code{-v}
6201 @itemx --version
6202 @cindex @code{--version}
6203 Print the version of the @code{make} program plus a copyright, a list
6204 of authors, and a notice that there is no warranty; then exit.
6206 @item -w
6207 @cindex @code{-w}
6208 @itemx --print-directory
6209 @cindex @code{--print-directory}
6210 Print a message containing the working directory both before and after
6211 executing the makefile.  This may be useful for tracking down errors
6212 from complicated nests of recursive @code{make} commands.
6213 @xref{Recursion, ,Recursive Use of @code{make}}.  (In practice, you
6214 rarely need to specify this option since @samp{make} does it for you;
6215 see @ref{-w Option, ,The @samp{--print-directory} Option}.)
6217 @itemx --no-print-directory
6218 @cindex @code{--no-print-directory}
6219 Disable printing of the working directory under @code{-w}.
6220 This option is useful when @code{-w} is turned on automatically,
6221 but you do not want to see the extra messages.
6222 @xref{-w Option, ,The @samp{--print-directory} Option}.
6224 @item -W @var{file}
6225 @cindex @code{-W}
6226 @itemx --what-if=@var{file}
6227 @cindex @code{--what-if}
6228 @itemx --new-file=@var{file}
6229 @cindex @code{--new-file}
6230 @itemx --assume-new=@var{file}
6231 @cindex @code{--assume-new}
6232 Pretend that the target @var{file} has just been modified.  When used
6233 with the @samp{-n} flag, this shows you what would happen if you were
6234 to modify that file.  Without @samp{-n}, it is almost the same as
6235 running a @code{touch} command on the given file before running
6236 @code{make}, except that the modification time is changed only in the
6237 imagination of @code{make}.
6238 @xref{Instead of Execution, ,Instead of Executing the Commands}.
6240 @item --warn-undefined-variables
6241 @cindex @code{--warn-undefined-variables}
6242 @cindex variables, warning for undefined
6243 @cindex undefined variables, warning message
6244 Issue a warning message whenever @code{make} sees a reference to an
6245 undefined variable.  This can be helpful when you are trying to debug
6246 makefiles which use variables in complex ways.
6247 @end table
6249 @node Implicit Rules, Archives, Running, Top
6250 @chapter Using Implicit Rules
6251 @cindex implicit rule
6252 @cindex rule, implicit
6254 Certain standard ways of remaking target files are used very often.  For
6255 example, one customary way to make an object file is from a C source file
6256 using the C compiler, @code{cc}.
6258 @dfn{Implicit rules} tell @code{make} how to use customary techniques so
6259 that you do not have to specify them in detail when you want to use
6260 them.  For example, there is an implicit rule for C compilation.  File
6261 names determine which implicit rules are run.  For example, C
6262 compilation typically takes a @file{.c} file and makes a @file{.o} file.
6263 So @code{make} applies the implicit rule for C compilation when it sees
6264 this combination of file name endings.@refill
6266 A chain of implicit rules can apply in sequence; for example, @code{make}
6267 will remake a @file{.o} file from a @file{.y} file by way of a @file{.c} file.
6268 @iftex
6269 @xref{Chained Rules, ,Chains of Implicit Rules}.
6270 @end iftex
6272 The built-in implicit rules use several variables in their commands so
6273 that, by changing the values of the variables, you can change the way the
6274 implicit rule works.  For example, the variable @code{CFLAGS} controls the
6275 flags given to the C compiler by the implicit rule for C compilation.
6276 @iftex
6277 @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
6278 @end iftex
6280 You can define your own implicit rules by writing @dfn{pattern rules}.
6281 @iftex
6282 @xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.
6283 @end iftex
6285 @dfn{Suffix rules} are a more limited way to define implicit rules.
6286 Pattern rules are more general and clearer, but suffix rules are
6287 retained for compatibility.
6288 @iftex
6289 @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
6290 @end iftex
6292 @menu
6293 * Using Implicit::              How to use an existing implicit rule
6294                                   to get the commands for updating a file.
6295 * Catalogue of Rules::          A list of built-in implicit rules.
6296 * Implicit Variables::          How to change what predefined rules do.
6297 * Chained Rules::               How to use a chain of implicit rules.
6298 * Pattern Rules::               How to define new implicit rules.
6299 * Last Resort::                 How to defining commands for rules 
6300                                   which cannot find any.
6301 * Suffix Rules::                The old-fashioned style of implicit rule.
6302 * Search Algorithm::            The precise algorithm for applying 
6303                                   implicit rules.
6304 @end menu
6306 @node Using Implicit, Catalogue of Rules,  , Implicit Rules
6307 @section Using Implicit Rules
6308 @cindex implicit rule, how to use
6309 @cindex rule, implicit, how to use
6311 To allow @code{make} to find a customary method for updating a target file,
6312 all you have to do is refrain from specifying commands yourself.  Either
6313 write a rule with no command lines, or don't write a rule at all.  Then
6314 @code{make} will figure out which implicit rule to use based on which
6315 kind of source file exists or can be made.
6317 For example, suppose the makefile looks like this:
6319 @example
6320 foo : foo.o bar.o
6321         cc -o foo foo.o bar.o $(CFLAGS) $(LDFLAGS)
6322 @end example
6324 @noindent
6325 Because you mention @file{foo.o} but do not give a rule for it, @code{make}
6326 will automatically look for an implicit rule that tells how to update it.
6327 This happens whether or not the file @file{foo.o} currently exists.
6329 If an implicit rule is found, it can supply both commands and one or
6330 more dependencies (the source files).  You would want to write a rule
6331 for @file{foo.o} with no command lines if you need to specify additional
6332 dependencies, such as header files, that the implicit rule cannot
6333 supply.
6335 Each implicit rule has a target pattern and dependency patterns.  There may
6336 be many implicit rules with the same target pattern.  For example, numerous
6337 rules make @samp{.o} files: one, from a @samp{.c} file with the C compiler;
6338 another, from a @samp{.p} file with the Pascal compiler; and so on.  The rule
6339 that actually applies is the one whose dependencies exist or can be made.
6340 So, if you have a file @file{foo.c}, @code{make} will run the C compiler;
6341 otherwise, if you have a file @file{foo.p}, @code{make} will run the Pascal
6342 compiler; and so on.
6344 Of course, when you write the makefile, you know which implicit rule you
6345 want @code{make} to use, and you know it will choose that one because you
6346 know which possible dependency files are supposed to exist.
6347 @xref{Catalogue of Rules, ,Catalogue of Implicit Rules},
6348 for a catalogue of all the predefined implicit rules.
6350 Above, we said an implicit rule applies if the required dependencies ``exist
6351 or can be made''.  A file ``can be made'' if it is mentioned explicitly in
6352 the makefile as a target or a dependency, or if an implicit rule can be
6353 recursively found for how to make it.  When an implicit dependency is the
6354 result of another implicit rule, we say that @dfn{chaining} is occurring.
6355 @xref{Chained Rules, ,Chains of Implicit Rules}.
6357 In general, @code{make} searches for an implicit rule for each target, and
6358 for each double-colon rule, that has no commands.  A file that is mentioned
6359 only as a dependency is considered a target whose rule specifies nothing,
6360 so implicit rule search happens for it.  @xref{Search Algorithm, ,Implicit Rule Search Algorithm}, for the
6361 details of how the search is done.
6363 Note that explicit dependencies do not influence implicit rule search.
6364 For example, consider this explicit rule:
6366 @example
6367 foo.o: foo.p
6368 @end example
6370 @noindent
6371 The dependency on @file{foo.p} does not necessarily mean that
6372 @code{make} will remake @file{foo.o} according to the implicit rule to
6373 make an object file, a @file{.o} file, from a Pascal source file, a
6374 @file{.p} file.  For example, if @file{foo.c} also exists, the implicit
6375 rule to make an object file from a C source file is used instead,
6376 because it appears before the Pascal rule in the list of predefined
6377 implicit rules (@pxref{Catalogue of Rules, , Catalogue of Implicit
6378 Rules}).
6380 If you do not want an implicit rule to be used for a target that has no
6381 commands, you can give that target empty commands by writing a semicolon
6382 (@pxref{Empty Commands, ,Defining Empty Commands}).
6384 @node Catalogue of Rules, Implicit Variables, Using Implicit, Implicit Rules
6385 @section Catalogue of Implicit Rules
6386 @cindex implicit rule, predefined
6387 @cindex rule, implicit, predefined
6389 Here is a catalogue of predefined implicit rules which are always
6390 available unless the makefile explicitly overrides or cancels them.
6391 @xref{Canceling Rules, ,Canceling Implicit Rules}, for information on
6392 canceling or overriding an implicit rule.  The @samp{-r} or
6393 @samp{--no-builtin-rules} option cancels all predefined rules.
6395 Not all of these rules will always be defined, even when the @samp{-r}
6396 option is not given.  Many of the predefined implicit rules are
6397 implemented in @code{make} as suffix rules, so which ones will be
6398 defined depends on the @dfn{suffix list} (the list of dependencies of
6399 the special target @code{.SUFFIXES}).  The default suffix list is:
6400 @code{.out}, @code{.a}, @code{.ln}, @code{.o}, @code{.c}, @code{.cc},
6401 @code{.C}, @code{.p}, @code{.f}, @code{.F}, @code{.r}, @code{.y},
6402 @code{.l}, @code{.s}, @code{.S}, @code{.mod}, @code{.sym}, @code{.def},
6403 @code{.h}, @code{.info}, @code{.dvi}, @code{.tex}, @code{.texinfo},
6404 @code{.texi}, @code{.txinfo}, @code{.w}, @code{.ch} @code{.web},
6405 @code{.sh}, @code{.elc}, @code{.el}.  All of the implicit rules
6406 described below whose dependencies have one of these suffixes are
6407 actually suffix rules.  If you modify the suffix list, the only
6408 predefined suffix rules in effect will be those named by one or two of
6409 the suffixes that are on the list you specify; rules whose suffixes fail
6410 to be on the list are disabled.  @xref{Suffix Rules, ,Old-Fashioned
6411 Suffix Rules}, for full details on suffix rules.
6413 @table @asis
6414 @item Compiling C programs
6415 @cindex C, rule to compile
6416 @pindex cc
6417 @pindex gcc
6418 @pindex .o
6419 @pindex .c
6420 @file{@var{n}.o} is made automatically from @file{@var{n}.c} with
6421 a command of the form @samp{$(CC) -c $(CPPFLAGS) $(CFLAGS)}.@refill
6423 @item Compiling C++ programs
6424 @cindex C++, rule to compile
6425 @pindex g++
6426 @pindex .C
6427 @pindex .cc
6428 @file{@var{n}.o} is made automatically from @file{@var{n}.cc} or
6429 @file{@var{n}.C} with a command of the form @samp{$(CXX) -c $(CPPFLAGS)
6430 $(CXXFLAGS)}.  We encourage you to use the suffix @samp{.cc} for C++
6431 source files instead of @samp{.C}.@refill
6433 @item Compiling Pascal programs
6434 @cindex Pascal, rule to compile
6435 @pindex pc
6436 @pindex .p
6437 @file{@var{n}.o} is made automatically from @file{@var{n}.p}
6438 with the command @samp{$(PC) -c $(PFLAGS)}.@refill
6440 @item Compiling Fortran and Ratfor programs
6441 @cindex Fortran, rule to compile
6442 @cindex Ratfor, rule to compile
6443 @pindex f77
6444 @pindex .f
6445 @pindex .r
6446 @pindex .F
6447 @file{@var{n}.o} is made automatically from @file{@var{n}.r},
6448 @file{@var{n}.F} or @file{@var{n}.f} by running the
6449 Fortran compiler.  The precise command used is as follows:@refill
6451 @table @samp
6452 @item .f
6453 @samp{$(FC) -c $(FFLAGS)}.
6454 @item .F
6455 @samp{$(FC) -c $(FFLAGS) $(CPPFLAGS)}.
6456 @item .r
6457 @samp{$(FC) -c $(FFLAGS) $(RFLAGS)}.
6458 @end table
6460 @item Preprocessing Fortran and Ratfor programs
6461 @file{@var{n}.f} is made automatically from @file{@var{n}.r} or
6462 @file{@var{n}.F}.  This rule runs just the preprocessor to convert a
6463 Ratfor or preprocessable Fortran program into a strict Fortran
6464 program.  The precise command used is as follows:@refill
6466 @table @samp
6467 @item .F
6468 @samp{$(FC) -F $(CPPFLAGS) $(FFLAGS)}.
6469 @item .r
6470 @samp{$(FC) -F $(FFLAGS) $(RFLAGS)}.
6471 @end table
6473 @item Compiling Modula-2 programs
6474 @cindex Modula-2, rule to compile
6475 @pindex m2c
6476 @pindex .sym
6477 @pindex .def
6478 @pindex .mod
6479 @file{@var{n}.sym} is made from @file{@var{n}.def} with a command
6480 of the form @samp{$(M2C) $(M2FLAGS) $(DEFFLAGS)}.  @file{@var{n}.o}
6481 is made from @file{@var{n}.mod}; the form is:
6482 @w{@samp{$(M2C) $(M2FLAGS) $(MODFLAGS)}}.@refill
6484 @need 1200
6485 @item Assembling and preprocessing assembler programs
6486 @cindex assembly, rule to compile
6487 @pindex as
6488 @pindex .s
6489 @file{@var{n}.o} is made automatically from @file{@var{n}.s} by
6490 running the assembler, @code{as}.  The precise command is
6491 @samp{$(AS) $(ASFLAGS)}.@refill
6493 @pindex .S
6494 @file{@var{n}.s} is made automatically from @file{@var{n}.S} by
6495 running the C preprocessor, @code{cpp}.  The precise command is
6496 @w{@samp{$(CPP) $(CPPFLAGS)}}.
6498 @item Linking a single object file
6499 @cindex linking, predefined rule for
6500 @pindex ld
6501 @pindex .o
6502 @file{@var{n}} is made automatically from @file{@var{n}.o} by running
6503 the linker (usually called @code{ld}) via the C compiler.  The precise
6504 command used is @w{@samp{$(CC) $(LDFLAGS) @var{n}.o $(LOADLIBES)}}.
6506 This rule does the right thing for a simple program with only one
6507 source file.  It will also do the right thing if there are multiple
6508 object files (presumably coming from various other source files), one
6509 of which has a name matching that of the executable file.  Thus,
6511 @example
6512 x: y.o z.o
6513 @end example
6515 @noindent
6516 when @file{x.c}, @file{y.c} and @file{z.c} all exist will execute:
6518 @example
6519 @group
6520 cc -c x.c -o x.o
6521 cc -c y.c -o y.o
6522 cc -c z.c -o z.o
6523 cc x.o y.o z.o -o x
6524 rm -f x.o
6525 rm -f y.o
6526 rm -f z.o
6527 @end group
6528 @end example
6530 @noindent
6531 In more complicated cases, such as when there is no object file whose
6532 name derives from the executable file name, you must write an explicit
6533 command for linking.
6535 Each kind of file automatically made into @samp{.o} object files will
6536 be automatically linked by using the compiler (@samp{$(CC)},
6537 @samp{$(FC)} or @samp{$(PC)}; the C compiler @samp{$(CC)} is used to
6538 assemble @samp{.s} files) without the @samp{-c} option.  This could be
6539 done by using the @samp{.o} object files as intermediates, but it is
6540 faster to do the compiling and linking in one step, so that's how it's
6541 done.@refill
6543 @item Yacc for C programs
6544 @pindex yacc
6545 @cindex Yacc, rule to run
6546 @pindex .y
6547 @file{@var{n}.c} is made automatically from @file{@var{n}.y} by
6548 running Yacc with the command @samp{$(YACC) $(YFLAGS)}.
6550 @item Lex for C programs
6551 @pindex lex
6552 @cindex Lex, rule to run
6553 @pindex .l
6554 @file{@var{n}.c} is made automatically from @file{@var{n}.l} by
6555 by running Lex.  The actual command is @samp{$(LEX) $(LFLAGS)}.
6557 @item Lex for Ratfor programs
6558 @file{@var{n}.r} is made automatically from @file{@var{n}.l} by
6559 by running Lex.  The actual command is @samp{$(LEX) $(LFLAGS)}.
6561 The convention of using the same suffix @samp{.l} for all Lex files
6562 regardless of whether they produce C code or Ratfor code makes it
6563 impossible for @code{make} to determine automatically which of the two
6564 languages you are using in any particular case.  If @code{make} is
6565 called upon to remake an object file from a @samp{.l} file, it must
6566 guess which compiler to use.  It will guess the C compiler, because
6567 that is more common.  If you are using Ratfor, make sure @code{make}
6568 knows this by mentioning @file{@var{n}.r} in the makefile.  Or, if you
6569 are using Ratfor exclusively, with no C files, remove @samp{.c} from
6570 the list of implicit rule suffixes with:@refill
6572 @example
6573 @group
6574 .SUFFIXES:
6575 .SUFFIXES: .o .r .f .l @dots{}
6576 @end group
6577 @end example
6579 @item Making Lint Libraries from C, Yacc, or Lex programs
6580 @pindex lint
6581 @cindex @code{lint}, rule to run
6582 @pindex .ln
6583 @file{@var{n}.ln} is made from @file{@var{n}.c} by running @code{lint}.
6584 The precise command is @w{@samp{$(LINT) $(LINTFLAGS) $(CPPFLAGS) -i}}.  
6585 The same command is used on the C code produced from 
6586 @file{@var{n}.y} or @file{@var{n}.l}.@refill
6588 @item @TeX{} and Web
6589 @cindex @TeX{}, rule to run
6590 @cindex Web, rule to run
6591 @pindex tex
6592 @pindex cweave
6593 @pindex weave
6594 @pindex tangle
6595 @pindex ctangle
6596 @pindex .dvi
6597 @pindex .tex
6598 @pindex .web
6599 @pindex .w
6600 @pindex .ch
6601 @file{@var{n}.dvi} is made from @file{@var{n}.tex} with the command
6602 @samp{$(TEX)}.  @file{@var{n}.tex} is made from @file{@var{n}.web} with
6603 @samp{$(WEAVE)}, or from @file{@var{n}.w} (and from @file{@var{n}.ch} if
6604 it exists or can be made) with @samp{$(CWEAVE)}.  @file{@var{n}.p} is
6605 made from @file{@var{n}.web} with @samp{$(TANGLE)} and @file{@var{n}.c}
6606 is made from @file{@var{n}.w} (and from @file{@var{n}.ch} if it exists
6607 or can be made) with @samp{$(CTANGLE)}.@refill
6609 @item Texinfo and Info
6610 @cindex Texinfo, rule to format
6611 @cindex Info, rule to format
6612 @pindex texi2dvi
6613 @pindex makeinfo
6614 @pindex .texinfo
6615 @pindex .info
6616 @pindex .texi
6617 @pindex .txinfo
6618 @file{@var{n}.dvi} is made from @file{@var{n}.texinfo},
6619 @file{@var{n}.texi}, or @file{@var{n}.txinfo}, with the command
6620 @w{@samp{$(TEXI2DVI) $(TEXI2DVI_FLAGS)}}.  @file{@var{n}.info} is made from
6621 @file{@var{n}.texinfo}, @file{@var{n}.texi}, or @file{@var{n}.txinfo}, with
6622 the command @w{@samp{$(MAKEINFO) $(MAKEINFO_FLAGS)}}.
6624 @item RCS
6625 @cindex RCS, rule to extract from
6626 @pindex co
6627 @pindex ,v @r{(RCS file extension)}
6628 Any file @file{@var{n}} is extracted if necessary from an RCS file
6629 named either @file{@var{n},v} or @file{RCS/@var{n},v}.  The precise
6630 command used is @w{@samp{$(CO) $(COFLAGS)}}.  @file{@var{n}} will not be
6631 extracted from RCS if it already exists, even if the RCS file is
6632 newer.  The rules for RCS are terminal 
6633 (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}), 
6634 so RCS files cannot be generated from another source; they must
6635 actually exist.@refill
6637 @item SCCS
6638 @cindex SCCS, rule to extract from
6639 @pindex get
6640 @pindex s. @r{(SCCS file prefix)}
6641 Any file @file{@var{n}} is extracted if necessary from an SCCS file
6642 named either @file{s.@var{n}} or @file{SCCS/s.@var{n}}.  The precise
6643 command used is @w{@samp{$(GET) $(GFLAGS)}}.  The rules for SCCS are
6644 terminal (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}),
6645 so SCCS files cannot be generated from another source; they must
6646 actually exist.@refill
6648 @pindex .sh
6649 For the benefit of SCCS, a file @file{@var{n}} is copied from
6650 @file{@var{n}.sh} and made executable (by everyone).  This is for
6651 shell scripts that are checked into SCCS.  Since RCS preserves the
6652 execution permission of a file, you do not need to use this feature
6653 with RCS.@refill
6655 We recommend that you avoid using of SCCS.  RCS is widely held to be
6656 superior, and is also free.  By choosing free software in place of
6657 comparable (or inferior) proprietary software, you support the free
6658 software movement.
6659 @end table
6661 Usually, you want to change only the variables listed in the table
6662 above, which are documented in the following section.
6664 However, the commands in built-in implicit rules actually use
6665 variables such as @code{COMPILE.c}, @code{LINK.p}, and
6666 @code{PREPROCESS.S}, whose values contain the commands listed above.
6668 @code{make} follows the convention that the rule to compile a
6669 @file{.@var{x}} source file uses the variable @code{COMPILE.@var{x}}.
6670 Similarly, the rule to produce an executable from a @file{.@var{x}}
6671 file uses @code{LINK.@var{x}}; and the rule to preprocess a
6672 @file{.@var{x}} file uses @code{PREPROCESS.@var{x}}.
6674 @vindex OUTPUT_OPTION
6675 Every rule that produces an object file uses the variable
6676 @code{OUTPUT_OPTION}.  @code{make} defines this variable either to
6677 contain @samp{-o $@@}, or to be empty, depending on a compile-time
6678 option.  You need the @samp{-o} option to ensure that the output goes
6679 into the right file when the source file is in a different directory,
6680 as when using @code{VPATH} (@pxref{Directory Search}).  However,
6681 compilers on some systems do not accept a @samp{-o} switch for object
6682 files.  If you use such a system, and use @code{VPATH}, some
6683 compilations will put their output in the wrong place.
6684 A possible workaround for this problem is to give @code{OUTPUT_OPTION}
6685 the value @w{@samp{; mv $*.o $@@}}.
6687 @node Implicit Variables, Chained Rules, Catalogue of Rules, Implicit Rules
6688 @section Variables Used by Implicit Rules
6689 @cindex flags for compilers
6691 The commands in built-in implicit rules make liberal use of certain
6692 predefined variables.  You can alter these variables in the makefile,
6693 with arguments to @code{make}, or in the environment to alter how the
6694 implicit rules work without redefining the rules themselves.
6696 For example, the command used to compile a C source file actually says
6697 @samp{$(CC) -c $(CFLAGS) $(CPPFLAGS)}.  The default values of the variables
6698 used are @samp{cc} and nothing, resulting in the command @samp{cc -c}.  By
6699 redefining @samp{CC} to @samp{ncc}, you could cause @samp{ncc} to be
6700 used for all C compilations performed by the implicit rule.  By redefining
6701 @samp{CFLAGS} to be @samp{-g}, you could pass the @samp{-g} option to
6702 each compilation.  @emph{All} implicit rules that do C compilation use
6703 @samp{$(CC)} to get the program name for the compiler and @emph{all}
6704 include @samp{$(CFLAGS)} among the arguments given to the compiler.@refill
6706 The variables used in implicit rules fall into two classes: those that are
6707 names of programs (like @code{CC}) and those that contain arguments for the
6708 programs (like @code{CFLAGS}).  (The ``name of a program'' may also contain
6709 some command arguments, but it must start with an actual executable program
6710 name.)  If a variable value contains more than one argument, separate them
6711 with spaces.
6713 Here is a table of variables used as names of programs in built-in rules:
6715 @table @code
6716 @item AR
6717 @vindex AR
6718 Archive-maintaining program; default @samp{ar}.
6719 @pindex ar
6721 @item AS
6722 @vindex AS
6723 Program for doing assembly; default @samp{as}.
6724 @pindex as
6726 @item CC
6727 @vindex CC
6728 Program for compiling C programs; default @samp{cc}.
6729 @pindex cc
6731 @item CXX
6732 @vindex CXX
6733 Program for compiling C++ programs; default @samp{g++}.
6734 @pindex g++
6736 @item CO
6737 @vindex CO
6738 Program for extracting a file from RCS; default @samp{co}.
6739 @pindex co
6741 @item CPP
6742 @vindex CPP
6743 Program for running the C preprocessor, with results to standard output;
6744 default @samp{$(CC) -E}.
6746 @item FC
6747 @vindex FC
6748 Program for compiling or preprocessing Fortran and Ratfor programs;
6749 default @samp{f77}.
6750 @pindex f77
6752 @item GET
6753 @vindex GET
6754 Program for extracting a file from SCCS; default @samp{get}.
6755 @pindex get
6757 @item LEX
6758 @vindex LEX
6759 Program to use to turn Lex grammars into C programs or Ratfor programs;
6760 default @samp{lex}.
6761 @pindex lex
6763 @item PC
6764 @vindex PC
6765 Program for compiling Pascal programs; default @samp{pc}.
6766 @pindex pc
6768 @item YACC
6769 @vindex YACC
6770 Program to use to turn Yacc grammars into C programs; default @samp{yacc}.
6771 @pindex yacc
6773 @item YACCR
6774 @vindex YACCR
6775 Program to use to turn Yacc grammars into Ratfor
6776 programs; default @samp{yacc -r}.
6778 @item MAKEINFO
6779 @vindex MAKEINFO
6780 Program to convert a Texinfo source file into an Info file; default
6781 @samp{makeinfo}.
6782 @pindex makeinfo
6784 @item TEX
6785 @vindex TEX
6786 Program to make @TeX{} @sc{dvi} files from @TeX{} source;
6787 default @samp{tex}.
6788 @pindex tex
6790 @item TEXI2DVI
6791 @vindex TEXI2DVI
6792 Program to make @TeX{} @sc{dvi} files from Texinfo source;
6793 default @samp{texi2dvi}.
6794 @pindex texi2dvi
6796 @item WEAVE
6797 @vindex WEAVE
6798 Program to translate Web into @TeX{}; default @samp{weave}.
6799 @pindex weave
6801 @item CWEAVE
6802 @vindex CWEAVE
6803 Program to translate C Web into @TeX{}; default @samp{cweave}.
6804 @pindex cweave
6806 @item TANGLE
6807 @vindex TANGLE
6808 Program to translate Web into Pascal; default @samp{tangle}.
6809 @pindex tangle
6811 @item CTANGLE
6812 @vindex CTANGLE
6813 Program to translate C Web into C; default @samp{ctangle}.
6814 @pindex ctangle
6816 @item RM
6817 @vindex RM
6818 Command to remove a file; default @samp{rm -f}.
6819 @pindex rm
6820 @end table
6822 Here is a table of variables whose values are additional arguments for the
6823 programs above.  The default values for all of these is the empty
6824 string, unless otherwise noted.
6826 @table @code
6827 @item ARFLAGS
6828 @vindex ARFLAGS
6829 Flags to give the archive-maintaining program; default @samp{rv}.
6831 @item ASFLAGS
6832 @vindex ASFLAGS
6833 Extra flags to give to the assembler (when explicitly
6834 invoked on a @samp{.s} or @samp{.S} file).
6836 @item CFLAGS
6837 @vindex CFLAGS
6838 Extra flags to give to the C compiler.
6840 @item CXXFLAGS
6841 @vindex CXXFLAGS
6842 Extra flags to give to the C++ compiler.
6844 @item COFLAGS
6845 @vindex COFLAGS
6846 Extra flags to give to the RCS @code{co} program.
6848 @item CPPFLAGS
6849 @vindex CPPFLAGS
6850 Extra flags to give to the C preprocessor and programs
6851 that use it (the C and Fortran compilers).
6853 @item FFLAGS
6854 @vindex FFLAGS
6855 Extra flags to give to the Fortran compiler.
6857 @item GFLAGS
6858 @vindex GFLAGS
6859 Extra flags to give to the SCCS @code{get} program.
6861 @item LDFLAGS
6862 @vindex LDFLAGS
6863 Extra flags to give to compilers when they are
6864 supposed to invoke the linker, @samp{ld}.
6866 @item LFLAGS
6867 @vindex LFLAGS
6868 Extra flags to give to Lex.
6870 @item PFLAGS
6871 @vindex PFLAGS
6872 Extra flags to give to the Pascal compiler.
6874 @item RFLAGS
6875 @vindex RFLAGS
6876 Extra flags to give to the Fortran compiler for Ratfor programs.
6878 @item YFLAGS
6879 @vindex YFLAGS
6880 Extra flags to give to Yacc.
6881 @end table
6883 @node Chained Rules, Pattern Rules, Implicit Variables, Implicit Rules
6884 @section Chains of Implicit Rules
6886 @cindex chains of rules
6887 @cindex rule, implicit, chains of
6888 Sometimes a file can be made by a sequence of implicit rules.  For example,
6889 a file @file{@var{n}.o} could be made from @file{@var{n}.y} by running
6890 first Yacc and then @code{cc}.  Such a sequence is called a @dfn{chain}.
6892 If the file @file{@var{n}.c} exists, or is mentioned in the makefile, no
6893 special searching is required: @code{make} finds that the object file can
6894 be made by C compilation from @file{@var{n}.c}; later on, when considering
6895 how to make @file{@var{n}.c}, the rule for running Yacc is
6896 used.  Ultimately both @file{@var{n}.c} and @file{@var{n}.o} are
6897 updated.@refill
6899 @cindex intermediate files
6900 @cindex files, intermediate
6901 However, even if @file{@var{n}.c} does not exist and is not mentioned,
6902 @code{make} knows how to envision it as the missing link between
6903 @file{@var{n}.o} and @file{@var{n}.y}!  In this case, @file{@var{n}.c} is
6904 called an @dfn{intermediate file}.  Once @code{make} has decided to use the
6905 intermediate file, it is entered in the data base as if it had been
6906 mentioned in the makefile, along with the implicit rule that says how to
6907 create it.@refill
6909 Intermediate files are remade using their rules just like all other
6910 files.  The difference is that the intermediate file is deleted when
6911 @code{make} is finished.  Therefore, the intermediate file which did not
6912 exist before @code{make} also does not exist after @code{make}.  The
6913 deletion is reported to you by printing a @samp{rm -f} command that
6914 shows what @code{make} is doing.  (You can list the target pattern of an
6915 implicit rule (such as @samp{%.o}) as a dependency of the special
6916 target @code{.PRECIOUS} to preserve intermediate files made by implicit
6917 rules whose target patterns match that file's name; 
6918 see @ref{Interrupts}.)@refill
6919 @cindex intermediate files, preserving
6920 @cindex preserving intermediate files
6921 @cindex preserving with @code{.PRECIOUS}
6922 @cindex @code{.PRECIOUS} intermediate files
6924 A chain can involve more than two implicit rules.  For example, it is
6925 possible to make a file @file{foo} from @file{RCS/foo.y,v} by running RCS,
6926 Yacc and @code{cc}.  Then both @file{foo.y} and @file{foo.c} are
6927 intermediate files that are deleted at the end.@refill
6929 No single implicit rule can appear more than once in a chain.  This means
6930 that @code{make} will not even consider such a ridiculous thing as making
6931 @file{foo} from @file{foo.o.o} by running the linker twice.  This
6932 constraint has the added benefit of preventing any infinite loop in the
6933 search for an implicit rule chain.
6935 There are some special implicit rules to optimize certain cases that would
6936 otherwise be handled by rule chains.  For example, making @file{foo} from
6937 @file{foo.c} could be handled by compiling and linking with separate
6938 chained rules, using @file{foo.o} as an intermediate file.  But what
6939 actually happens is that a special rule for this case does the compilation
6940 and linking with a single @code{cc} command.  The optimized rule is used in
6941 preference to the step-by-step chain because it comes earlier in the
6942 ordering of rules.
6944 @node Pattern Rules, Last Resort, Chained Rules, Implicit Rules
6945 @section Defining and Redefining Pattern Rules
6947 You define an implicit rule by writing a @dfn{pattern rule}.  A pattern
6948 rule looks like an ordinary rule, except that its target contains the
6949 character @samp{%} (exactly one of them).  The target is considered a
6950 pattern for matching file names; the @samp{%} can match any nonempty
6951 substring, while other characters match only themselves.  The dependencies
6952 likewise use @samp{%} to show how their names relate to the target name.
6954 Thus, a pattern rule @samp{%.o : %.c} says how to make any file
6955 @file{@var{stem}.o} from another file @file{@var{stem}.c}.@refill
6957 Note that expansion using @samp{%} in pattern rules occurs
6958 @strong{after} any variable or function expansions, which take place
6959 when the makefile is read.  @xref{Using Variables, , How to Use
6960 Variables}, and @ref{Functions, ,Functions for Transforming Text}.
6962 @menu
6963 * Pattern Intro::               An introduction to pattern rules.
6964 * Pattern Examples::            Examples of pattern rules.
6965 * Automatic::                   How to use automatic variables in the
6966                                   commands of implicit rules.
6967 * Pattern Match::               How patterns match.
6968 * Match-Anything Rules::        Precautions you should take prior to
6969                                   defining rules that can match any
6970                                   target file whatever.
6971 * Canceling Rules::             How to override or cancel built-in rules.
6972 @end menu
6974 @node Pattern Intro, Pattern Examples,  , Pattern Rules
6975 @subsection Introduction to Pattern Rules
6976 @cindex pattern rule
6977 @cindex rule, pattern
6979 A pattern rule contains the character @samp{%} (exactly one of them)
6980 in the target; otherwise, it looks exactly like an ordinary rule.  The
6981 target is a pattern for matching file names; the @samp{%} matches any
6982 nonempty substring, while other characters match only themselves.
6983 @cindex target pattern, implicit
6984 @cindex @code{%}, in pattern rules
6986 For example, @samp{%.c} as a pattern matches any file name that ends in
6987 @samp{.c}.  @samp{s.%.c} as a pattern matches any file name that starts
6988 with @samp{s.}, ends in @samp{.c} and is at least five characters long.
6989 (There must be at least one character to match the @samp{%}.)  The substring
6990 that the @samp{%} matches is called the @dfn{stem}.@refill
6992 @samp{%} in a dependency of a pattern rule stands for the same stem
6993 that was matched by the @samp{%} in the target.  In order for
6994 the pattern rule to apply, its target pattern must match the file name
6995 under consideration, and its dependency patterns must name files that
6996 exist or can be made.  These files become dependencies of the target.
6997 @cindex dependency pattern, implicit
6999 Thus, a rule of the form
7001 @example
7002 %.o : %.c ; @var{command}@dots{}
7003 @end example
7005 @noindent
7006 specifies how to make a file @file{@var{n}.o}, with another file
7007 @file{@var{n}.c} as its dependency, provided that @file{@var{n}.c}
7008 exists or can be made.
7010 There may also be dependencies that do not use @samp{%}; such a dependency
7011 attaches to every file made by this pattern rule.  These unvarying
7012 dependencies are useful occasionally.
7014 A pattern rule need not have any dependencies that contain @samp{%}, or
7015 in fact any dependencies at all.  Such a rule is effectively a general
7016 wildcard.  It provides a way to make any file that matches the target
7017 pattern.  @xref{Last Resort}.
7019 @c !!! The end of of this paragraph should be rewritten.  --bob
7020 Pattern rules may have more than one target.  Unlike normal rules, this
7021 does not act as many different rules with the same dependencies and
7022 commands.  If a pattern rule has multiple targets, @code{make} knows that
7023 the rule's commands are responsible for making all of the targets.  The
7024 commands are executed only once to make all the targets.  When searching
7025 for a pattern rule to match a target, the target patterns of a rule other
7026 than the one that matches the target in need of a rule are incidental:
7027 @code{make} worries only about giving commands and dependencies to the file
7028 presently in question.  However, when this file's commands are run, the
7029 other targets are marked as having been updated themselves.
7030 @cindex multiple targets, in pattern rule
7031 @cindex target, multiple in pattern rule
7033 The order in which pattern rules appear in the makefile is important
7034 since this is the order in which they are considered.
7035 Of equally applicable
7036 rules, only the first one found is used.  The rules you write take precedence
7037 over those that are built in.  Note however, that a rule whose
7038 dependencies actually exist or are mentioned always takes priority over a
7039 rule with dependencies that must be made by chaining other implicit rules.
7040 @cindex pattern rules, order of
7041 @cindex order of pattern rules
7043 @node Pattern Examples, Automatic, Pattern Intro, Pattern Rules
7044 @subsection Pattern Rule Examples
7046 Here are some examples of pattern rules actually predefined in
7047 @code{make}.  First, the rule that compiles @samp{.c} files into @samp{.o}
7048 files:@refill
7050 @example
7051 %.o : %.c
7052         $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@@
7053 @end example
7055 @noindent
7056 defines a rule that can make any file @file{@var{x}.o} from
7057 @file{@var{x}.c}.  The command uses the automatic variables @samp{$@@} and
7058 @samp{$<} to substitute the names of the target file and the source file
7059 in each case where the rule applies (@pxref{Automatic, ,Automatic Variables}).@refill
7061 Here is a second built-in rule:
7063 @example
7064 % :: RCS/%,v
7065         $(CO) $(COFLAGS) $<
7066 @end example
7068 @noindent
7069 defines a rule that can make any file @file{@var{x}} whatsoever from a
7070 corresponding file @file{@var{x},v} in the subdirectory @file{RCS}.  Since
7071 the target is @samp{%}, this rule will apply to any file whatever, provided
7072 the appropriate dependency file exists.  The double colon makes the rule
7073 @dfn{terminal}, which means that its dependency may not be an intermediate
7074 file (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}).@refill
7076 @need 500
7077 This pattern rule has two targets:
7079 @example
7080 @group
7081 %.tab.c %.tab.h: %.y
7082         bison -d $<
7083 @end group
7084 @end example
7086 @noindent
7087 @c The following paragraph is rewritten to avoid overfull hboxes
7088 This tells @code{make} that the command @samp{bison -d @var{x}.y} will
7089 make both @file{@var{x}.tab.c} and @file{@var{x}.tab.h}.  If the file
7090 @file{foo} depends on the files @file{parse.tab.o} and @file{scan.o}
7091 and the file @file{scan.o} depends on the file @file{parse.tab.h},
7092 when @file{parse.y} is changed, the command @samp{bison -d parse.y}
7093 will be executed only once, and the dependencies of both
7094 @file{parse.tab.o} and @file{scan.o} will be satisfied.  (Presumably
7095 the file @file{parse.tab.o} will be recompiled from @file{parse.tab.c}
7096 and the file @file{scan.o} from @file{scan.c}, while @file{foo} is
7097 linked from @file{parse.tab.o}, @file{scan.o}, and its other
7098 dependencies, and it will execute happily ever after.)@refill
7100 @node Automatic, Pattern Match, Pattern Examples, Pattern Rules
7101 @subsection Automatic Variables
7102 @cindex automatic variables
7103 @cindex variables, automatic
7104 @cindex variables, and implicit rule
7106 Suppose you are writing a pattern rule to compile a @samp{.c} file into a
7107 @samp{.o} file: how do you write the @samp{cc} command so that it operates
7108 on the right source file name?  You cannot write the name in the command,
7109 because the name is different each time the implicit rule is applied.
7111 What you do is use a special feature of @code{make}, the @dfn{automatic
7112 variables}.  These variables have values computed afresh for each rule that
7113 is executed, based on the target and dependencies of the rule.  In this
7114 example, you would use @samp{$@@} for the object file name and @samp{$<}
7115 for the source file name.
7117 Here is a table of automatic variables:
7119 @table @code
7120 @vindex $@@
7121 @vindex @@ @r{(automatic variable)}
7122 @item $@@
7123 The file name of the target of the rule.  If the target is an archive
7124 member, then @samp{$@@} is the name of the archive file.  In a pattern
7125 rule that has multiple targets (@pxref{Pattern Intro, ,Introduction to
7126 Pattern Rules}), @samp{$@@} is the name of whichever target caused the
7127 rule's commands to be run.
7129 @vindex $%
7130 @vindex % @r{(automatic variable)}
7131 @item $%
7132 The target member name, when the target is an archive member.
7133 @xref{Archives}.  For example, if the target is @file{foo.a(bar.o)} then
7134 @samp{$%} is @file{bar.o} and @samp{$@@} is @file{foo.a}.  @samp{$%} is
7135 empty when the target is not an archive member.
7137 @vindex $<
7138 @vindex < @r{(automatic variable)}
7139 @item $<
7140 The name of the first dependency.  If the target got its commands from
7141 an implicit rule, this will be the first dependency added by the
7142 implicit rule (@pxref{Implicit Rules}).
7144 @vindex $?
7145 @vindex ? @r{(automatic variable)}
7146 @item $?
7147 The names of all the dependencies that are newer than the target, with
7148 spaces between them.  For dependencies which are archive members, only
7149 the member named is used (@pxref{Archives}).
7150 @cindex dependencies, list of changed
7151 @cindex list of changed dependencies
7153 @vindex $^
7154 @vindex ^ @r{(automatic variable)}
7155 @item $^
7156 The names of all the dependencies, with spaces between them.  For
7157 dependencies which are archive members, only the member named is used
7158 (@pxref{Archives}).  A target has only one dependency on each other file
7159 it depends on, no matter how many times each file is listed as a
7160 dependency.  So if you list a dependency more than once for a target,
7161 the value of @code{$^} contains just one copy of the name.
7162 @cindex dependencies, list of all
7163 @cindex list of all dependencies
7165 @vindex $+
7166 @vindex + @r{(automatic variable)}
7167 @item $+
7168 This is like @samp{$^}, but dependencies listed more than once are
7169 duplicated in the order they were listed in the makefile.  This is
7170 primarily useful for use in linking commands where it is meaningful to
7171 repeat library file names in a particular order.
7173 @vindex $*
7174 @vindex * @r{(automatic variable)}
7175 @item $*
7176 The stem with which an implicit rule matches (@pxref{Pattern Match, ,How
7177 Patterns Match}).  If the target is @file{dir/a.foo.b} and the target
7178 pattern is @file{a.%.b} then the stem is @file{dir/foo}.  The stem is
7179 useful for constructing names of related files.@refill
7180 @cindex stem, variable for
7182 In a static pattern rule, the stem is part of the file name that matched
7183 the @samp{%} in the target pattern.
7185 In an explicit rule, there is no stem; so @samp{$*} cannot be determined
7186 in that way.  Instead, if the target name ends with a recognized suffix
7187 (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}), @samp{$*} is set to
7188 the target name minus the suffix.  For example, if the target name is
7189 @samp{foo.c}, then @samp{$*} is set to @samp{foo}, since @samp{.c} is a
7190 suffix.  GNU @code{make} does this bizarre thing only for compatibility
7191 with other implementations of @code{make}.  You should generally avoid
7192 using @samp{$*} except in implicit rules or static pattern rules.@refill
7194 If the target name in an explicit rule does not end with a recognized
7195 suffix, @samp{$*} is set to the empty string for that rule.
7196 @end table
7198 @samp{$?} is useful even in explicit rules when you wish to operate on only
7199 the dependencies that have changed.  For example, suppose that an archive
7200 named @file{lib} is supposed to contain copies of several object files.
7201 This rule copies just the changed object files into the archive:
7203 @example
7204 @group
7205 lib: foo.o bar.o lose.o win.o
7206         ar r lib $?
7207 @end group
7208 @end example
7210 Of the variables listed above, four have values that are single file
7211 names, and two have values that are lists of file names.  These six have
7212 variants that get just the file's directory name or just the file name
7213 within the directory.  The variant variables' names are formed by
7214 appending @samp{D} or @samp{F}, respectively.  These variants are
7215 semi-obsolete in GNU @code{make} since the functions @code{dir} and
7216 @code{notdir} can be used to get a similar effect (@pxref{Filename
7217 Functions, , Functions for File Names}).  Note, however, that the
7218 @samp{F} variants all omit the trailing slash which always appears in
7219 the output of the @code{dir} function.  Here is a table of the variants:
7221 @table @samp
7222 @vindex $(@@D)
7223 @vindex @@D @r{(automatic variable)}
7224 @item $(@@D)
7225 The directory part of the file name of the target, with the trailing
7226 slash removed.  If the value of @samp{$@@} is @file{dir/foo.o} then
7227 @samp{$(@@D)} is @file{dir}.  This value is @file{.} if @samp{$@@} does
7228 not contain a slash.
7230 @vindex $(@@F)
7231 @vindex @@F @r{(automatic variable)}
7232 @item $(@@F)
7233 The file-within-directory part of the file name of the target.  If the
7234 value of @samp{$@@} is @file{dir/foo.o} then @samp{$(@@F)} is
7235 @file{foo.o}.  @samp{$(@@F)} is equivalent to @samp{$(notdir $@@)}.
7237 @vindex $(*D)
7238 @vindex *D @r{(automatic variable)}
7239 @item $(*D)
7240 @vindex $(*F)
7241 @vindex *F @r{(automatic variable)}
7242 @itemx $(*F)
7243 The directory part and the file-within-directory
7244 part of the stem; @file{dir} and @file{foo} in this example.
7246 @vindex $(%D)
7247 @vindex %D @r{(automatic variable)}
7248 @item $(%D)
7249 @vindex $(%F)
7250 @vindex %F @r{(automatic variable)}
7251 @itemx $(%F)
7252 The directory part and the file-within-directory part of the target
7253 archive member name.  This makes sense only for archive member targets
7254 of the form @file{@var{archive}(@var{member})} and is useful only when
7255 @var{member} may contain a directory name.  (@xref{Archive Members,
7256 ,Archive Members as Targets}.)
7258 @vindex $(<D)
7259 @vindex <D @r{(automatic variable)}
7260 @item $(<D)
7261 @vindex $(<F)
7262 @vindex <F @r{(automatic variable)}
7263 @itemx $(<F)
7264 The directory part and the file-within-directory
7265 part of the first dependency.
7267 @vindex $(^D)
7268 @vindex ^D @r{(automatic variable)}
7269 @item $(^D)
7270 @vindex $(^F)
7271 @vindex ^F @r{(automatic variable)}
7272 @itemx $(^F)
7273 Lists of the directory parts and the file-within-directory
7274 parts of all dependencies.
7276 @vindex $(?D)
7277 @vindex ?D @r{(automatic variable)}
7278 @item $(?D)
7279 @vindex $(?F)
7280 @vindex ?F @r{(automatic variable)}
7281 @itemx $(?F)
7282 Lists of the directory parts and the file-within-directory parts of
7283 all dependencies that are newer than the target.
7284 @end table
7286 Note that we use a special stylistic convention when we talk about these
7287 automatic variables; we write ``the value of @samp{$<}'', rather than
7288 @w{``the variable @code{<}''} as we would write for ordinary variables
7289 such as @code{objects} and @code{CFLAGS}.  We think this convention
7290 looks more natural in this special case.  Please do not assume it has a
7291 deep significance; @samp{$<} refers to the variable named @code{<} just
7292 as @samp{$(CFLAGS)} refers to the variable named @code{CFLAGS}.
7293 You could just as well use @samp{$(<)} in place of @samp{$<}.
7295 @node Pattern Match, Match-Anything Rules, Automatic, Pattern Rules
7296 @subsection How Patterns Match
7298 @cindex stem
7299 A target pattern is composed of a @samp{%} between a prefix and a suffix,
7300 either or both of which may be empty.  The pattern matches a file name only
7301 if the file name starts with the prefix and ends with the suffix, without
7302 overlap.  The text between the prefix and the suffix is called the
7303 @dfn{stem}.  Thus, when the pattern @samp{%.o} matches the file name
7304 @file{test.o}, the stem is @samp{test}.  The pattern rule dependencies are
7305 turned into actual file names by substituting the stem for the character
7306 @samp{%}.  Thus, if in the same example one of the dependencies is written
7307 as @samp{%.c}, it expands to @samp{test.c}.@refill
7309 When the target pattern does not contain a slash (and it usually does
7310 not), directory names in the file names are removed from the file name
7311 before it is compared with the target prefix and suffix.  After the
7312 comparison of the file name to the target pattern, the directory
7313 names, along with the slash that ends them, are added on to the
7314 dependency file names generated from the pattern rule's dependency
7315 patterns and the file name. The directories are ignored only for the
7316 purpose of finding an implicit rule to use, not in the application of
7317 that rule.  Thus, @samp{e%t} matches the file name @file{src/eat},
7318 with @samp{src/a} as the stem.  When dependencies are turned into file
7319 names, the directories from the stem are added at the front, while the
7320 rest of the stem is substituted for the @samp{%}.  The stem
7321 @samp{src/a} with a dependency pattern @samp{c%r} gives the file name
7322 @file{src/car}.@refill
7324 @node Match-Anything Rules, Canceling Rules, Pattern Match, Pattern Rules
7325 @subsection Match-Anything Pattern Rules
7327 @cindex match-anything rule
7328 @cindex terminal rule
7329 When a pattern rule's target is just @samp{%}, it matches any file name
7330 whatever.  We call these rules @dfn{match-anything} rules.  They are very
7331 useful, but it can take a lot of time for @code{make} to think about them,
7332 because it must consider every such rule for each file name listed either
7333 as a target or as a dependency.
7335 Suppose the makefile mentions @file{foo.c}.  For this target, @code{make}
7336 would have to consider making it by linking an object file @file{foo.c.o},
7337 or by C compilation-and-linking in one step from @file{foo.c.c}, or by
7338 Pascal compilation-and-linking from @file{foo.c.p}, and many other
7339 possibilities.
7341 We know these possibilities are ridiculous since @file{foo.c} is a C source
7342 file, not an executable.  If @code{make} did consider these possibilities,
7343 it would ultimately reject them, because files such as @file{foo.c.o} and
7344 @file{foo.c.p} would not exist.  But these possibilities are so
7345 numerous that @code{make} would run very slowly if it had to consider
7346 them.@refill
7348 To gain speed, we have put various constraints on the way @code{make}
7349 considers match-anything rules.  There are two different constraints that
7350 can be applied, and each time you define a match-anything rule you must
7351 choose one or the other for that rule.
7353 One choice is to mark the match-anything rule as @dfn{terminal} by defining
7354 it with a double colon.  When a rule is terminal, it does not apply unless
7355 its dependencies actually exist.  Dependencies that could be made with
7356 other implicit rules are not good enough.  In other words, no further
7357 chaining is allowed beyond a terminal rule.
7359 For example, the built-in implicit rules for extracting sources from RCS
7360 and SCCS files are terminal; as a result, if the file @file{foo.c,v} does
7361 not exist, @code{make} will not even consider trying to make it as an
7362 intermediate file from @file{foo.c,v.o} or from @file{RCS/SCCS/s.foo.c,v}.
7363 RCS and SCCS files are generally ultimate source files, which should not be
7364 remade from any other files; therefore, @code{make} can save time by not
7365 looking for ways to remake them.@refill
7367 If you do not mark the match-anything rule as terminal, then it is
7368 nonterminal.  A nonterminal match-anything rule cannot apply to a file name
7369 that indicates a specific type of data.  A file name indicates a specific
7370 type of data if some non-match-anything implicit rule target matches it.
7372 For example, the file name @file{foo.c} matches the target for the pattern
7373 rule @samp{%.c : %.y} (the rule to run Yacc).  Regardless of whether this
7374 rule is actually applicable (which happens only if there is a file
7375 @file{foo.y}), the fact that its target matches is enough to prevent
7376 consideration of any nonterminal match-anything rules for the file
7377 @file{foo.c}.  Thus, @code{make} will not even consider trying to make
7378 @file{foo.c} as an executable file from @file{foo.c.o}, @file{foo.c.c},
7379 @file{foo.c.p}, etc.@refill
7381 The motivation for this constraint is that nonterminal match-anything
7382 rules are used for making files containing specific types of data (such as
7383 executable files) and a file name with a recognized suffix indicates some
7384 other specific type of data (such as a C source file).
7386 Special built-in dummy pattern rules are provided solely to recognize
7387 certain file names so that nonterminal match-anything rules will not be
7388 considered.  These dummy rules have no dependencies and no commands, and
7389 they are ignored for all other purposes.  For example, the built-in
7390 implicit rule
7392 @example
7393 %.p :
7394 @end example
7396 @noindent
7397 exists to make sure that Pascal source files such as @file{foo.p} match a
7398 specific target pattern and thereby prevent time from being wasted looking
7399 for @file{foo.p.o} or @file{foo.p.c}.
7401 Dummy pattern rules such as the one for @samp{%.p} are made for every
7402 suffix listed as valid for use in suffix rules (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}).
7404 @node Canceling Rules,  , Match-Anything Rules, Pattern Rules
7405 @subsection Canceling Implicit Rules
7407 You can override a built-in implicit rule (or one you have defined
7408 yourself) by defining a new pattern rule with the same target and
7409 dependencies, but different commands.  When the new rule is defined, the
7410 built-in one is replaced.  The new rule's position in the sequence of
7411 implicit rules is determined by where you write the new rule.
7413 You can cancel a built-in implicit rule by defining a pattern rule with the
7414 same target and dependencies, but no commands.  For example, the following
7415 would cancel the rule that runs the assembler:
7417 @example
7418 %.o : %.s
7419 @end example
7421 @node Last Resort, Suffix Rules, Pattern Rules, Implicit Rules
7422 @section Defining Last-Resort Default Rules
7423 @cindex last-resort default rules
7424 @cindex default rules, last-resort
7426 You can define a last-resort implicit rule by writing a terminal
7427 match-anything pattern rule with no dependencies (@pxref{Match-Anything
7428 Rules}).  This is just like any other pattern rule; the only thing
7429 special about it is that it will match any target.  So such a rule's
7430 commands are used for all targets and dependencies that have no commands
7431 of their own and for which no other implicit rule applies.
7433 For example, when testing a makefile, you might not care if the source
7434 files contain real data, only that they exist.  Then you might do this:
7436 @example
7438         touch $@@
7439 @end example
7441 @noindent
7442 to cause all the source files needed (as dependencies) to be created
7443 automatically.
7445 @findex .DEFAULT
7446 You can instead define commands to be used for targets for which there
7447 are no rules at all, even ones which don't specify commands.  You do
7448 this by writing a rule for the target @code{.DEFAULT}.  Such a rule's
7449 commands are used for all dependencies which do not appear as targets in
7450 any explicit rule, and for which no implicit rule applies.  Naturally,
7451 there is no @code{.DEFAULT} rule unless you write one.
7453 If you use @code{.DEFAULT} with no commands or dependencies:
7455 @example
7456 .DEFAULT:
7457 @end example
7459 @noindent
7460 the commands previously stored for @code{.DEFAULT} are cleared.
7461 Then @code{make} acts as if you had never defined @code{.DEFAULT} at all.
7463 If you do not want a target to get the commands from a match-anything
7464 pattern rule or @code{.DEFAULT}, but you also do not want any commands
7465 to be run for the target, you can give it empty commands (@pxref{Empty
7466 Commands, ,Defining Empty Commands}).@refill
7468 You can use a last-resort rule to override part of another makefile.
7469 @xref{Overriding Makefiles, , Overriding Part of Another Makefile}.
7471 @node Suffix Rules, Search Algorithm, Last Resort, Implicit Rules
7472 @section Old-Fashioned Suffix Rules
7473 @cindex old-fashioned suffix rules
7474 @cindex suffix rule
7476 @dfn{Suffix rules} are the old-fashioned way of defining implicit rules for
7477 @code{make}.  Suffix rules are obsolete because pattern rules are more
7478 general and clearer.  They are supported in GNU @code{make} for
7479 compatibility with old makefiles.  They come in two kinds:
7480 @dfn{double-suffix} and @dfn{single-suffix}.@refill
7482 A double-suffix rule is defined by a pair of suffixes: the target suffix
7483 and the source suffix.  It matches any file whose name ends with the
7484 target suffix.  The corresponding implicit dependency is made by
7485 replacing the target suffix with the source suffix in the file name.  A
7486 two-suffix rule whose target and source suffixes are @samp{.o} and
7487 @samp{.c} is equivalent to the pattern rule @samp{%.o : %.c}.
7489 A single-suffix rule is defined by a single suffix, which is the source
7490 suffix.  It matches any file name, and the corresponding implicit
7491 dependency name is made by appending the source suffix.  A single-suffix
7492 rule whose source suffix is @samp{.c} is equivalent to the pattern rule
7493 @samp{% : %.c}.
7495 Suffix rule definitions are recognized by comparing each rule's target
7496 against a defined list of known suffixes.  When @code{make} sees a rule
7497 whose target is a known suffix, this rule is considered a single-suffix
7498 rule.  When @code{make} sees a rule whose target is two known suffixes
7499 concatenated, this rule is taken as a double-suffix rule.
7501 For example, @samp{.c} and @samp{.o} are both on the default list of
7502 known suffixes.  Therefore, if you define a rule whose target is
7503 @samp{.c.o}, @code{make} takes it to be a double-suffix rule with source
7504 suffix @samp{.c} and target suffix @samp{.o}.  Here is the old-fashioned
7505 way to define the rule for compiling a C source file:@refill
7507 @example
7508 .c.o:
7509         $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
7510 @end example
7512 Suffix rules cannot have any dependencies of their own.  If they have any,
7513 they are treated as normal files with funny names, not as suffix rules.
7514 Thus, the rule:
7516 @example
7517 .c.o: foo.h
7518         $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
7519 @end example
7521 @noindent
7522 tells how to make the file @file{.c.o} from the dependency file
7523 @file{foo.h}, and is not at all like the pattern rule:
7525 @example
7526 %.o: %.c foo.h
7527         $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
7528 @end example
7530 @noindent
7531 which tells how to make @samp{.o} files from @samp{.c} files, and makes all
7532 @samp{.o} files using this pattern rule also depend on @file{foo.h}.
7534 Suffix rules with no commands are also meaningless.  They do not remove
7535 previous rules as do pattern rules with no commands (@pxref{Canceling
7536 Rules, , Canceling Implicit Rules}).  They simply enter the suffix or pair of suffixes concatenated as
7537 a target in the data base.@refill
7539 @findex .SUFFIXES
7540 The known suffixes are simply the names of the dependencies of the special
7541 target @code{.SUFFIXES}.  You can add your own suffixes by writing a rule
7542 for @code{.SUFFIXES} that adds more dependencies, as in:
7544 @example
7545 .SUFFIXES: .hack .win
7546 @end example
7548 @noindent
7549 which adds @samp{.hack} and @samp{.win} to the end of the list of suffixes.
7551 If you wish to eliminate the default known suffixes instead of just adding
7552 to them, write a rule for @code{.SUFFIXES} with no dependencies.  By
7553 special dispensation, this eliminates all existing dependencies of
7554 @code{.SUFFIXES}.  You can then write another rule to add the suffixes you
7555 want.  For example,
7557 @example
7558 @group
7559 .SUFFIXES:            # @r{Delete the default suffixes}
7560 .SUFFIXES: .c .o .h   # @r{Define our suffix list}
7561 @end group
7562 @end example
7564 The @samp{-r} or @samp{--no-builtin-rules} flag causes the default
7565 list of suffixes to be empty.
7567 @vindex SUFFIXES
7568 The variable @code{SUFFIXES} is defined to the default list of suffixes
7569 before @code{make} reads any makefiles.  You can change the list of suffixes
7570 with a rule for the special target @code{.SUFFIXES}, but that does not alter
7571 this variable.
7573 @node Search Algorithm,  , Suffix Rules, Implicit Rules
7574 @section Implicit Rule Search Algorithm
7575 @cindex implicit rule, search algorithm
7576 @cindex search algorithm, implicit rule
7578 Here is the procedure @code{make} uses for searching for an implicit rule
7579 for a target @var{t}.  This procedure is followed for each double-colon
7580 rule with no commands, for each target of ordinary rules none of which have
7581 commands, and for each dependency that is not the target of any rule.  It
7582 is also followed recursively for dependencies that come from implicit
7583 rules, in the search for a chain of rules.
7585 Suffix rules are not mentioned in this algorithm because suffix rules are
7586 converted to equivalent pattern rules once the makefiles have been read in.
7588 For an archive member target of the form
7589 @samp{@var{archive}(@var{member})}, the following algorithm is run
7590 twice, first using the entire target name @var{t}, and second using
7591 @samp{(@var{member})} as the target @var{t} if the first run found no
7592 rule.@refill
7594 @enumerate
7595 @item
7596 Split @var{t} into a directory part, called @var{d}, and the rest,
7597 called @var{n}.  For example, if @var{t} is @samp{src/foo.o}, then
7598 @var{d} is @samp{src/} and @var{n} is @samp{foo.o}.@refill
7600 @item
7601 Make a list of all the pattern rules one of whose targets matches
7602 @var{t} or @var{n}.  If the target pattern contains a slash, it is
7603 matched against @var{t}; otherwise, against @var{n}.
7605 @item
7606 If any rule in that list is @emph{not} a match-anything rule, then
7607 remove all nonterminal match-anything rules from the list.
7609 @item
7610 Remove from the list all rules with no commands.
7612 @item
7613 For each pattern rule in the list:
7615 @enumerate a
7616 @item
7617 Find the stem @var{s}, which is the nonempty part of @var{t} or @var{n}
7618 matched by the @samp{%} in the target pattern.@refill
7620 @item
7621 Compute the dependency names by substituting @var{s} for @samp{%}; if
7622 the target pattern does not contain a slash, append @var{d} to
7623 the front of each dependency name.@refill
7625 @item
7626 Test whether all the dependencies exist or ought to exist.  (If a
7627 file name is mentioned in the makefile as a target or as an explicit
7628 dependency, then we say it ought to exist.)
7630 If all dependencies exist or ought to exist, or there are no dependencies,
7631 then this rule applies.
7632 @end enumerate
7634 @item
7635 If no pattern rule has been found so far, try harder.
7636 For each pattern rule in the list:
7638 @enumerate a
7639 @item
7640 If the rule is terminal, ignore it and go on to the next rule.
7642 @item
7643 Compute the dependency names as before.
7645 @item
7646 Test whether all the dependencies exist or ought to exist.
7648 @item
7649 For each dependency that does not exist, follow this algorithm
7650 recursively to see if the dependency can be made by an implicit
7651 rule.
7653 @item
7654 If all dependencies exist, ought to exist, or can be
7655 made by implicit rules, then this rule applies.
7656 @end enumerate
7658 @item
7659 If no implicit rule applies, the rule for @code{.DEFAULT}, if any,
7660 applies.  In that case, give @var{t} the same commands that
7661 @code{.DEFAULT} has.  Otherwise, there are no commands for @var{t}.
7662 @end enumerate
7664 Once a rule that applies has been found, for each target pattern of the
7665 rule other than the one that matched @var{t} or @var{n}, the @samp{%} in
7666 the pattern is replaced with @var{s} and the resultant file name is stored
7667 until the commands to remake the target file @var{t} are executed.  After
7668 these commands are executed, each of these stored file names are entered
7669 into the data base and marked as having been updated and having the same
7670 update status as the file @var{t}.
7672 When the commands of a pattern rule are executed for @var{t}, the automatic
7673 variables are set corresponding to the target and dependencies.
7674 @xref{Automatic, ,Automatic Variables}.
7676 @node Archives, Features, Implicit Rules, Top
7677 @chapter Using @code{make} to Update Archive Files
7678 @cindex archive
7680 @dfn{Archive files} are files containing named subfiles called
7681 @dfn{members}; they are maintained with the program @code{ar} and their
7682 main use is as subroutine libraries for linking.
7684 @menu
7685 * Archive Members::             Archive members as targets.
7686 * Archive Update::              The implicit rule for archive member targets.
7687 * Archive Pitfalls::            Dangers to watch out for when using archives.
7688 * Archive Suffix Rules::        You can write a special kind of suffix rule
7689                                   for updating archives.
7690 @end menu
7692 @node Archive Members, Archive Update,  , Archives
7693 @section Archive Members as Targets
7694 @cindex archive member targets
7696 An individual member of an archive file can be used as a target or
7697 dependency in @code{make}.  You specify the member named @var{member} in
7698 archive file @var{archive} as follows:
7700 @example
7701 @var{archive}(@var{member})
7702 @end example
7704 @noindent
7705 This construct is available only in targets and dependencies, not in
7706 commands!  Most programs that you might use in commands do not support this
7707 syntax and cannot act directly on archive members.  Only @code{ar} and
7708 other programs specifically designed to operate on archives can do so.
7709 Therefore, valid commands to update an archive member target probably must
7710 use @code{ar}.  For example, this rule says to create a member
7711 @file{hack.o} in archive @file{foolib} by copying the file @file{hack.o}:
7713 @example
7714 foolib(hack.o) : hack.o
7715         ar cr foolib hack.o
7716 @end example
7718 In fact, nearly all archive member targets are updated in just this way
7719 and there is an implicit rule to do it for you.  @strong{Note:} The
7720 @samp{c} flag to @code{ar} is required if the archive file does not
7721 already exist.
7723 To specify several members in the same archive, you can write all the
7724 member names together between the parentheses.  For example:
7726 @example
7727 foolib(hack.o kludge.o)
7728 @end example
7730 @noindent
7731 is equivalent to:
7733 @example
7734 foolib(hack.o) foolib(kludge.o)
7735 @end example
7737 @cindex wildcard, in archive member
7738 You can also use shell-style wildcards in an archive member reference.
7739 @xref{Wildcards, ,Using Wildcard Characters in File Names}.  For
7740 example, @w{@samp{foolib(*.o)}} expands to all existing members of the
7741 @file{foolib} archive whose names end in @samp{.o}; perhaps
7742 @samp{@w{foolib(hack.o)} @w{foolib(kludge.o)}}.
7744 @node Archive Update
7745 @section Implicit Rule for Archive Member Targets
7747 Recall that a target that looks like @file{@var{a}(@var{m})} stands for the
7748 member named @var{m} in the archive file @var{a}.
7750 When @code{make} looks for an implicit rule for such a target, as a special
7751 feature it considers implicit rules that match @file{(@var{m})}, as well as
7752 those that match the actual target @file{@var{a}(@var{m})}.
7754 This causes one special rule whose target is @file{(%)} to match.  This
7755 rule updates the target @file{@var{a}(@var{m})} by copying the file @var{m}
7756 into the archive.  For example, it will update the archive member target
7757 @file{foo.a(bar.o)} by copying the @emph{file} @file{bar.o} into the
7758 archive @file{foo.a} as a @emph{member} named @file{bar.o}.
7760 When this rule is chained with others, the result is very powerful.
7761 Thus, @samp{make "foo.a(bar.o)"} (the quotes are needed to protect the
7762 @samp{(} and @samp{)} from being interpreted specially by the shell) in
7763 the presence of a file @file{bar.c} is enough to cause the following
7764 commands to be run, even without a makefile:
7766 @example
7767 cc -c bar.c -o bar.o
7768 ar r foo.a bar.o
7769 rm -f bar.o
7770 @end example
7772 @noindent
7773 Here @code{make} has envisioned the file @file{bar.o} as an intermediate
7774 file.  @xref{Chained Rules, ,Chains of Implicit Rules}.
7776 Implicit rules such as this one are written using the automatic variable
7777 @samp{$%}.  @xref{Automatic, ,Automatic Variables}.
7779 An archive member name in an archive cannot contain a directory name, but
7780 it may be useful in a makefile to pretend that it does.  If you write an
7781 archive member target @file{foo.a(dir/file.o)}, @code{make} will perform
7782 automatic updating with this command:
7784 @example
7785 ar r foo.a dir/file.o
7786 @end example
7788 @noindent
7789 which has the effect of copying the file @file{dir/file.o} into a member
7790 named @file{file.o}.  In connection with such usage, the automatic variables
7791 @code{%D} and @code{%F} may be useful.
7793 @menu
7794 * Archive Symbols::             How to update archive symbol directories.
7795 @end menu
7797 @node Archive Symbols,  ,  , Archive Update
7798 @subsection Updating Archive Symbol Directories
7799 @cindex @code{__.SYMDEF}
7800 @cindex updating archive symbol directories
7801 @cindex archive symbol directory updating
7802 @cindex symbol directories, updating archive 
7803 @cindex directories, updating archive symbol
7805 An archive file that is used as a library usually contains a special member
7806 named @file{__.SYMDEF} that contains a directory of the external symbol
7807 names defined by all the other members.  After you update any other
7808 members, you need to update @file{__.SYMDEF} so that it will summarize the
7809 other members properly.  This is done by running the @code{ranlib} program:
7811 @example
7812 ranlib @var{archivefile}
7813 @end example
7815 Normally you would put this command in the rule for the archive file,
7816 and make all the members of the archive file dependencies of that rule.
7817 For example,
7819 @example
7820 libfoo.a: libfoo.a(x.o) libfoo.a(y.o) @dots{}
7821         ranlib libfoo.a
7822 @end example
7824 @noindent
7825 The effect of this is to update archive members @file{x.o}, @file{y.o},
7826 etc., and then update the symbol directory member @file{__.SYMDEF} by
7827 running @code{ranlib}.  The rules for updating the members are not shown
7828 here; most likely you can omit them and use the implicit rule which copies
7829 files into the archive, as described in the preceding section.
7831 This is not necessary when using the GNU @code{ar} program, which
7832 updates the @file{__.SYMDEF} member automatically.
7834 @node Archive Pitfalls
7835 @section Dangers When Using Archives
7836 @cindex archive, and parallel execution
7837 @cindex parallel execution, and archive update
7838 @cindex archive, and @code{-j}
7839 @cindex @code{-j}, and archive update
7841 It is important to be careful when using parallel execution (the
7842 @code{-j} switch; @pxref{Parallel, ,Parallel Execution}) and archives.
7843 If multiple @code{ar} commands run at the same time on the same archive
7844 file, they will not know about each other and can corrupt the file.
7846 Possibly a future version of @code{make} will provide a mechanism to
7847 circumvent this problem by serializing all commands that operate on the
7848 same archive file.  But for the time being, you must either write your
7849 makefiles to avoid this problem in some other way, or not use @code{-j}.
7851 @node Archive Suffix Rules, , Archive Pitfalls, Archives
7852 @section Suffix Rules for Archive Files
7853 @cindex suffix rule, for archive
7854 @cindex archive, suffix rule for
7855 @cindex library archive, suffix rule for
7856 @cindex @code{.a} (archives)
7858 You can write a special kind of suffix rule for dealing with archive
7859 files.  @xref{Suffix Rules}, for a full explanation of suffix rules.
7860 Archive suffix rules are obsolete in GNU @code{make}, because pattern
7861 rules for archives are a more general mechanism (@pxref{Archive
7862 Update}).  But they are retained for compatibility with other
7863 @code{make}s.
7865 To write a suffix rule for archives, you simply write a suffix rule
7866 using the target suffix @samp{.a} (the usual suffix for archive files).
7867 For example, here is the old-fashioned suffix rule to update a library
7868 archive from C source files:
7870 @example
7871 @group
7872 .c.a:
7873         $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o
7874         $(AR) r $@@ $*.o
7875         $(RM) $*.o
7876 @end group
7877 @end example
7879 @noindent
7880 This works just as if you had written the pattern rule:
7882 @example
7883 @group
7884 (%.o): %.c
7885         $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o
7886         $(AR) r $@@ $*.o
7887         $(RM) $*.o
7888 @end group
7889 @end example
7891 In fact, this is just what @code{make} does when it sees a suffix rule
7892 with @samp{.a} as the target suffix.  Any double-suffix rule
7893 @w{@samp{.@var{x}.a}} is converted to a pattern rule with the target
7894 pattern @samp{(%.o)} and a dependency pattern of @samp{%.@var{x}}.
7896 Since you might want to use @samp{.a} as the suffix for some other kind
7897 of file, @code{make} also converts archive suffix rules to pattern rules
7898 in the normal way (@pxref{Suffix Rules}).  Thus a double-suffix rule
7899 @w{@samp{.@var{x}.a}} produces two pattern rules: @samp{@w{(%.o):}
7900 @w{%.@var{x}}} and @samp{@w{%.a}: @w{%.@var{x}}}.@refill
7902 @node Features, Missing, Archives, Top
7903 @chapter Features of GNU @code{make}
7904 @cindex features of GNU @code{make}
7905 @cindex portability
7906 @cindex compatibility
7908 Here is a summary of the features of GNU @code{make}, for comparison
7909 with and credit to other versions of @code{make}.  We consider the
7910 features of @code{make} in 4.2 BSD systems as a baseline.  If you are
7911 concerned with writing portable makefiles, you should use only the
7912 features of @code{make} @emph{not} listed here or in @ref{Missing}.
7914 Many features come from the version of @code{make} in System V.
7916 @itemize @bullet
7917 @item
7918 The @code{VPATH} variable and its special meaning. 
7919 @xref{Directory Search, , Searching Directories for Dependencies}.  
7920 This feature exists in System V @code{make}, but is undocumented.
7921 It is documented in 4.3 BSD @code{make} (which says it mimics System V's
7922 @code{VPATH} feature).@refill
7924 @item
7925 Included makefiles.  @xref{Include, ,Including Other Makefiles}.
7926 Allowing multiple files to be included with a single directive is a GNU
7927 extension.
7929 @item
7930 Variables are read from and communicated via the environment.
7931 @xref{Environment, ,Variables from the Environment}.
7933 @item
7934 Options passed through the variable @code{MAKEFLAGS} to recursive
7935 invocations of @code{make}.
7936 @xref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
7938 @item
7939 The automatic variable @code{$%} is set to the member name
7940 in an archive reference.  @xref{Automatic, ,Automatic Variables}.
7942 @item
7943 The automatic variables @code{$@@}, @code{$*}, @code{$<}, @code{$%},
7944 and @code{$?} have corresponding forms like @code{$(@@F)} and
7945 @code{$(@@D)}.  We have generalized this to @code{$^} as an obvious
7946 extension.  @xref{Automatic, ,Automatic Variables}.@refill
7948 @item
7949 Substitution variable references.
7950 @xref{Reference, ,Basics of Variable References}.
7952 @item
7953 The command-line options @samp{-b} and @samp{-m}, accepted and
7954 ignored.  In System V @code{make}, these options actually do something.
7956 @item
7957 Execution of recursive commands to run @code{make} via the variable
7958 @code{MAKE} even if @samp{-n}, @samp{-q} or @samp{-t} is specified.
7959 @xref{Recursion, ,Recursive Use of @code{make}}.
7961 @item
7962 Support for suffix @samp{.a} in suffix rules.  @xref{Archive Suffix
7963 Rules}.  This feature is obsolete in GNU @code{make}, because the
7964 general feature of rule chaining (@pxref{Chained Rules, ,Chains of
7965 Implicit Rules}) allows one pattern rule for installing members in an
7966 archive (@pxref{Archive Update}) to be sufficient.
7968 @item
7969 The arrangement of lines and backslash-newline combinations in
7970 commands is retained when the commands are printed, so they appear as
7971 they do in the makefile, except for the stripping of initial
7972 whitespace.
7973 @end itemize
7975 The following features were inspired by various other versions of
7976 @code{make}.  In some cases it is unclear exactly which versions inspired
7977 which others.
7979 @itemize @bullet
7980 @item
7981 Pattern rules using @samp{%}.
7982 This has been implemented in several versions of @code{make}.
7983 We're not sure who invented it first, but it's been spread around a bit.
7984 @xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.@refill
7986 @item
7987 Rule chaining and implicit intermediate files.
7988 This was implemented by Stu Feldman in his version of @code{make}
7989 for AT&T Eighth Edition Research Unix, and later by Andrew Hume of
7990 AT&T Bell Labs in his @code{mk} program (where he terms it
7991 ``transitive closure'').  We do not really know if
7992 we got this from either of them or thought it up ourselves at the
7993 same time.  @xref{Chained Rules, ,Chains of Implicit Rules}.
7995 @item
7996 The automatic variable @code{$^} containing a list of all dependencies
7997 of the current target.  We did not invent this, but we have no idea who
7998 did.  @xref{Automatic, ,Automatic Variables}.  The automatic variable
7999 @code{$+} is a simple extension of @code{$^}.
8001 @item
8002 The ``what if'' flag (@samp{-W} in GNU @code{make}) was (as far as we know)
8003 invented by Andrew Hume in @code{mk}.  
8004 @xref{Instead of Execution, ,Instead of Executing the Commands}.
8006 @item
8007 The concept of doing several things at once (parallelism) exists in
8008 many incarnations of @code{make} and similar programs, though not in the
8009 System V or BSD implementations.  @xref{Execution, ,Command Execution}.
8011 @item
8012 Modified variable references using pattern substitution come from
8013 SunOS 4.  @xref{Reference, ,Basics of Variable References}.
8014 This functionality was provided in GNU @code{make} by the
8015 @code{patsubst} function before the alternate syntax was implemented
8016 for compatibility with SunOS 4.  It is not altogether clear who
8017 inspired whom, since GNU @code{make} had @code{patsubst} before SunOS
8018 4 was released.@refill
8020 @item
8021 The special significance of @samp{+} characters preceding command lines
8022 (@pxref{Instead of Execution, ,Instead of Executing the Commands}) is
8023 mandated by
8024 @cite{IEEE Standard 1003.2-1992} (POSIX.2).
8026 @item
8027 The @samp{+=} syntax to append to the value of a variable comes from SunOS
8028 4 @code{make}.  @xref{Appending, , Appending More Text to Variables}.
8030 @item
8031 The syntax @w{@samp{@var{archive}(@var{mem1} @var{mem2}@dots{})}} to list
8032 multiple members in a single archive file comes from SunOS 4 @code{make}.
8033 @xref{Archive Members}.
8035 @item
8036 The @code{-include} directive to include makefiles with no error for a
8037 nonexistent file comes from SunOS 4 @code{make}.  (But note that SunOS 4
8038 @code{make} does not allow multiple makefiles to be specified in one
8039 @code{-include} directive.)
8040 @end itemize
8042 The remaining features are inventions new in GNU @code{make}:
8044 @itemize @bullet
8045 @item
8046 Use the @samp{-v} or @samp{--version} option to print version and
8047 copyright information.
8049 @item 
8050 Use the @samp{-h} or @samp{--help} option to summarize the options to
8051 @code{make}.
8053 @item
8054 Simply-expanded variables.  @xref{Flavors, ,The Two Flavors of Variables}.
8056 @item
8057 Pass command-line variable assignments automatically through the
8058 variable @code{MAKE} to recursive @code{make} invocations.
8059 @xref{Recursion, ,Recursive Use of @code{make}}.
8061 @item
8062 Use the @samp{-C} or @samp{--directory} command option to change
8063 directory.  @xref{Options Summary, ,Summary of Options}.
8065 @item
8066 Make verbatim variable definitions with @code{define}.  
8067 @xref{Defining, ,Defining Variables Verbatim}.
8069 @item
8070 Declare phony targets with the special target @code{.PHONY}.
8072 Andrew Hume of AT&T Bell Labs implemented a similar feature with a
8073 different syntax in his @code{mk} program.  This seems to be a case of
8074 parallel discovery.  @xref{Phony Targets, ,Phony Targets}.
8076 @item
8077 Manipulate text by calling functions.  
8078 @xref{Functions, ,Functions for Transforming Text}.
8080 @item
8081 Use the @samp{-o} or @samp{--old-file}
8082 option to pretend a file's modification-time is old.
8083 @xref{Avoiding Compilation, ,Avoiding Recompilation of Some Files}.
8085 @item
8086 Conditional execution.
8088 This feature has been implemented numerous times in various versions
8089 of @code{make}; it seems a natural extension derived from the features
8090 of the C preprocessor and similar macro languages and is not a
8091 revolutionary concept.  @xref{Conditionals, ,Conditional Parts of Makefiles}.
8093 @item
8094 Specify a search path for included makefiles.
8095 @xref{Include, ,Including Other Makefiles}.
8097 @item
8098 Specify extra makefiles to read with an environment variable.
8099 @xref{MAKEFILES Variable, ,The Variable @code{MAKEFILES}}.
8101 @item
8102 Strip leading sequences of @samp{./} from file names, so that
8103 @file{./@var{file}} and @file{@var{file}} are considered to be the
8104 same file.@refill
8106 @item
8107 Use a special search method for library dependencies written in the
8108 form @samp{-l@var{name}}.  
8109 @xref{Libraries/Search, ,Directory Search for Link Libraries}.
8111 @item
8112 Allow suffixes for suffix rules 
8113 (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}) to contain any
8114 characters.  In other versions of @code{make}, they must begin with
8115 @samp{.} and not contain any @samp{/} characters.
8117 @item
8118 Keep track of the current level of @code{make} recursion using the
8119 variable @code{MAKELEVEL}.  @xref{Recursion, ,Recursive Use of @code{make}}.
8121 @item
8122 Specify static pattern rules.  @xref{Static Pattern, ,Static Pattern Rules}.
8124 @item
8125 Provide selective @code{vpath} search.  
8126 @xref{Directory Search, ,Searching Directories for Dependencies}.
8128 @item
8129 Provide computed variable references.  
8130 @xref{Reference, ,Basics of Variable References}.
8132 @item
8133 Update makefiles.  @xref{Remaking Makefiles, ,How Makefiles Are Remade}.
8134 System V @code{make} has a very, very limited form of this
8135 functionality in that it will check out SCCS files for makefiles.
8137 @item
8138 Various new built-in implicit rules.  
8139 @xref{Catalogue of Rules, ,Catalogue of Implicit Rules}.
8141 @item
8142 The built-in variable @samp{MAKE_VERSION} gives the version number of
8143 @code{make}.
8144 @end itemize
8146 @node Missing, Makefile Conventions, Features, Top
8147 @chapter Incompatibilities and Missing Features
8148 @cindex incompatibilities 
8149 @cindex missing features
8150 @cindex features, missing
8152 The @code{make} programs in various other systems support a few features
8153 that are not implemented in GNU @code{make}.  The POSIX.2 standard
8154 (@cite{IEEE Standard 1003.2-1992}) which specifies @code{make} does not
8155 require any of these features.@refill
8157 @itemize @bullet
8158 @item
8159 A target of the form @samp{@var{file}((@var{entry}))} stands for a member
8160 of archive file @var{file}.  The member is chosen, not by name, but by
8161 being an object file which defines the linker symbol @var{entry}.@refill
8163 This feature was not put into GNU @code{make} because of the
8164 nonmodularity of putting knowledge into @code{make} of the internal
8165 format of archive file symbol tables.  
8166 @xref{Archive Symbols, ,Updating Archive Symbol Directories}.
8168 @item
8169 Suffixes (used in suffix rules) that end with the character @samp{~}
8170 have a special meaning to System V @code{make}; 
8171 they refer to the SCCS file that corresponds
8172 to the file one would get without the @samp{~}.  For example, the
8173 suffix rule @samp{.c~.o} would make the file @file{@var{n}.o} from
8174 the SCCS file @file{s.@var{n}.c}.  For complete coverage, a whole
8175 series of such suffix rules is required.  
8176 @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
8178 In GNU @code{make}, this entire series of cases is handled by two
8179 pattern rules for extraction from SCCS, in combination with the
8180 general feature of rule chaining.  
8181 @xref{Chained Rules, ,Chains of Implicit Rules}.
8183 @item
8184 In System V @code{make}, the string @samp{$$@@} has the strange meaning
8185 that, in the dependencies of a rule with multiple targets, it stands
8186 for the particular target that is being processed.
8188 This is not defined in GNU @code{make} because @samp{$$} should always
8189 stand for an ordinary @samp{$}.
8191 It is possible to get this functionality through the use of static pattern
8192 rules (@pxref{Static Pattern, ,Static Pattern Rules}).  
8193 The System V @code{make} rule:
8195 @example
8196 $(targets): $$@@.o lib.a
8197 @end example
8199 @noindent
8200 can be replaced with the GNU @code{make} static pattern rule:
8202 @example
8203 $(targets): %: %.o lib.a
8204 @end example
8206 @item
8207 In System V and 4.3 BSD @code{make}, files found by @code{VPATH} search
8208 (@pxref{Directory Search, ,Searching Directories for Dependencies}) have their names changed inside command
8209 strings.  We feel it is much cleaner to always use automatic variables
8210 and thus make this feature obsolete.@refill
8212 @item
8213 In some Unix @code{make}s, the automatic variable @code{$*} appearing in
8214 the dependencies of a rule has the amazingly strange ``feature'' of
8215 expanding to the full name of the @emph{target of that rule}.  We cannot
8216 imagine what went on in the minds of Unix @code{make} developers to do
8217 this; it is utterly inconsistent with the normal definition of @code{$*}.
8218 @vindex * @r{(automatic variable), unsupported bizarre usage}
8220 @item
8221 In some Unix @code{make}s, implicit rule search
8222 (@pxref{Implicit Rules, ,Using Implicit Rules}) is apparently done for
8223 @emph{all} targets, not just those without commands.  This means you can
8224 do:@refill
8226 @example
8227 @group
8228 foo.o:
8229         cc -c foo.c
8230 @end group
8231 @end example
8233 @noindent
8234 and Unix @code{make} will intuit that @file{foo.o} depends on
8235 @file{foo.c}.@refill
8237 We feel that such usage is broken.  The dependency properties of
8238 @code{make} are well-defined (for GNU @code{make}, at least),
8239 and doing such a thing simply does not fit the model.@refill
8241 @item
8242 GNU @code{make} does not include any built-in implicit rules for
8243 compiling or preprocessing EFL programs.  If we hear of anyone who is
8244 using EFL, we will gladly add them.
8246 @item
8247 It appears that in SVR4 @code{make}, a suffix rule can be specified with
8248 no commands, and it is treated as if it had empty commands
8249 (@pxref{Empty Commands}).  For example:
8251 @example
8252 .c.a:
8253 @end example
8255 @noindent
8256 will override the built-in @file{.c.a} suffix rule.
8258 We feel that it is cleaner for a rule without commands to always simply
8259 add to the dependency list for the target.  The above example can be
8260 easily rewritten to get the desired behavior in GNU @code{make}:
8262 @example
8263 .c.a: ;
8264 @end example
8266 @item
8267 Some versions of @code{make} invoke the shell with the @samp{-e} flag,
8268 except under @samp{-k} (@pxref{Testing, ,Testing the Compilation of a
8269 Program}).  The @samp{-e} flag tells the shell to exit as soon as any
8270 program it runs returns a nonzero status.  We feel it is cleaner to
8271 write each shell command line to stand on its own and not require this
8272 special treatment.
8273 @end itemize
8275 @comment The makefile standards are in a separate file that is also
8276 @comment included by standards.texi.
8277 @include make-stds.texi
8279 @node Quick Reference, Complex Makefile, Makefile Conventions, Top
8280 @appendix Quick Reference
8282 This appendix summarizes the directives, text manipulation functions,
8283 and special variables which GNU @code{make} understands.
8284 @xref{Special Targets}, @ref{Catalogue of Rules, ,Catalogue of Implicit Rules},
8285 and @ref{Options Summary, ,Summary of Options},
8286 for other summaries.
8288 Here is a summary of the directives GNU @code{make} recognizes:
8290 @table @code
8291 @item define @var{variable}
8292 @itemx endef
8294 Define a multi-line, recursively-expanded variable.@*
8295 @xref{Sequences}.
8297 @item ifdef @var{variable}
8298 @itemx ifndef @var{variable}
8299 @itemx ifeq (@var{a},@var{b})
8300 @itemx ifeq "@var{a}" "@var{b}"
8301 @itemx ifeq '@var{a}' '@var{b}'
8302 @itemx ifneq (@var{a},@var{b})
8303 @itemx ifneq "@var{a}" "@var{b}"
8304 @itemx ifneq '@var{a}' '@var{b}'
8305 @itemx else
8306 @itemx endif
8308 Conditionally evaluate part of the makefile.@*
8309 @xref{Conditionals}.
8311 @item include @var{file}
8313 Include another makefile.@*
8314 @xref{Include, ,Including Other Makefiles}.
8316 @item override @var{variable} = @var{value}
8317 @itemx override @var{variable} := @var{value}
8318 @itemx override @var{variable} += @var{value}
8319 @itemx override define @var{variable}
8320 @itemx endef
8322 Define a variable, overriding any previous definition, even one from
8323 the command line.@*
8324 @xref{Override Directive, ,The @code{override} Directive}.
8326 @item export
8328 Tell @code{make} to export all variables to child processes by default.@*
8329 @xref{Variables/Recursion, , Communicating Variables to a Sub-@code{make}}.
8331 @item export @var{variable}
8332 @itemx export @var{variable} = @var{value}
8333 @itemx export @var{variable} := @var{value}
8334 @itemx export @var{variable} += @var{value}
8335 @itemx unexport @var{variable}
8336 Tell @code{make} whether or not to export a particular variable to child
8337 processes.@*
8338 @xref{Variables/Recursion, , Communicating Variables to a Sub-@code{make}}.
8340 @item vpath @var{pattern} @var{path}
8341 Specify a search path for files matching a @samp{%} pattern.@*
8342 @xref{Selective Search, , The @code{vpath} Directive}.
8344 @item vpath @var{pattern}
8345 Remove all search paths previously specified for @var{pattern}.
8347 @item vpath
8348 Remove all search paths previously specified in any @code{vpath}
8349 directive.
8350 @end table
8352 Here is a summary of the text manipulation functions (@pxref{Functions}):
8354 @table @code
8355 @item $(subst @var{from},@var{to},@var{text})
8356 Replace @var{from} with @var{to} in @var{text}.@*
8357 @xref{Text Functions, , Functions for String Substitution and Analysis}.
8359 @item $(patsubst @var{pattern},@var{replacement},@var{text})
8360 Replace words matching @var{pattern} with @var{replacement} in @var{text}.@*
8361 @xref{Text Functions, , Functions for String Substitution and Analysis}.
8363 @item $(strip @var{string})
8364 Remove excess whitespace characters from @var{string}.@*
8365 @xref{Text Functions, , Functions for String Substitution and Analysis}.
8367 @item $(findstring @var{find},@var{text})
8368 Locate @var{find} in @var{text}.@*
8369 @xref{Text Functions, , Functions for String Substitution and Analysis}.
8371 @item $(filter @var{pattern}@dots{},@var{text})
8372 Select words in @var{text} that match one of the @var{pattern} words.@*
8373 @xref{Text Functions, , Functions for String Substitution and Analysis}.
8375 @item $(filter-out @var{pattern}@dots{},@var{text})
8376 Select words in @var{text} that @emph{do not} match any of the @var{pattern} words.@*
8377 @xref{Text Functions, , Functions for String Substitution and Analysis}.
8379 @item $(sort @var{list})
8380 Sort the words in @var{list} lexicographically, removing duplicates.@*
8381 @xref{Text Functions, , Functions for String Substitution and Analysis}.
8383 @item $(dir @var{names}@dots{})
8384 Extract the directory part of each file name.@*
8385 @xref{Filename Functions, ,Functions for File Names}.
8387 @item $(notdir @var{names}@dots{})
8388 Extract the non-directory part of each file name.@*
8389 @xref{Filename Functions, ,Functions for File Names}.
8391 @item $(suffix @var{names}@dots{})
8392 Extract the suffix (the last @samp{.} and following characters) of each file name.@*
8393 @xref{Filename Functions, ,Functions for File Names}.
8395 @item $(basename @var{names}@dots{})
8396 Extract the base name (name without suffix) of each file name.@*
8397 @xref{Filename Functions, ,Functions for File Names}.
8399 @item $(addsuffix @var{suffix},@var{names}@dots{})
8400 Append @var{suffix} to each word in @var{names}.@*
8401 @xref{Filename Functions, ,Functions for File Names}.
8403 @item $(addprefix @var{prefix},@var{names}@dots{})
8404 Prepend @var{prefix} to each word in @var{names}.@*
8405 @xref{Filename Functions, ,Functions for File Names}.
8407 @item $(join @var{list1},@var{list2})
8408 Join two parallel lists of words.@*
8409 @xref{Filename Functions, ,Functions for File Names}.
8411 @item $(word @var{n},@var{text})
8412 Extract the @var{n}th word (one-origin) of @var{text}.@*
8413 @xref{Filename Functions, ,Functions for File Names}.
8415 @item $(words @var{text})
8416 Count the number of words in @var{text}.@*
8417 @xref{Filename Functions, ,Functions for File Names}.
8419 @item $(firstword @var{names}@dots{})
8420 Extract the first word of @var{names}.@*
8421 @xref{Filename Functions, ,Functions for File Names}.
8423 @item $(wildcard @var{pattern}@dots{})
8424 Find file names matching a shell file name pattern (@emph{not} a
8425 @samp{%} pattern).@*
8426 @xref{Wildcard Function, ,The Function @code{wildcard}}.
8428 @item $(shell @var{command})
8430 Execute a shell command and return its output.@*
8431 @xref{Shell Function, , The @code{shell} Function}.
8433 @item $(origin @var{variable})
8435 Return a string describing how the @code{make} variable @var{variable} was
8436 defined.@*
8437 @xref{Origin Function, , The @code{origin} Function}.
8439 @item $(foreach @var{var},@var{words},@var{text})
8441 Evaluate @var{text} with @var{var} bound to each word in @var{words},
8442 and concatenate the results.@*
8443 @xref{Foreach Function, ,The @code{foreach} Function}.
8444 @end table
8446 Here is a summary of the automatic variables.
8447 @xref{Automatic, ,Automatic Variables},
8448 for full information.
8450 @table @code
8451 @item $@@
8452 The file name of the target.
8454 @item $%
8455 The target member name, when the target is an archive member.
8457 @item $<
8458 The name of the first dependency.
8460 @item $?
8461 The names of all the dependencies that are
8462 newer than the target, with spaces between them.
8463 For dependencies which are archive members, only
8464 the member named is used (@pxref{Archives}).
8466 @item $^
8467 @itemx $+
8468 The names of all the dependencies, with spaces between them.  For
8469 dependencies which are archive members, only the member named is used
8470 (@pxref{Archives}).  The value of @code{$^} omits duplicate
8471 dependencies, while @code{$+} retains them and preserves their order.
8473 @item $*
8474 The stem with which an implicit rule matches 
8475 (@pxref{Pattern Match, ,How Patterns Match}).
8477 @item $(@@D)
8478 @itemx $(@@F)
8479 The directory part and the file-within-directory part of @code{$@@}.
8481 @item $(*D)
8482 @itemx $(*F)
8483 The directory part and the file-within-directory part of @code{$*}.
8485 @item $(%D)
8486 @itemx $(%F)
8487 The directory part and the file-within-directory part of @code{$%}.
8489 @item $(<D)
8490 @itemx $(<F)
8491 The directory part and the file-within-directory part of @code{$<}.
8493 @item $(^D)
8494 @itemx $(^F)
8495 The directory part and the file-within-directory part of @code{$^}.
8497 @item $(+D)
8498 @itemx $(+F)
8499 The directory part and the file-within-directory part of @code{$+}.
8501 @item $(?D)
8502 @itemx $(?F)
8503 The directory part and the file-within-directory part of @code{$?}.
8504 @end table
8506 These variables are used specially by GNU @code{make}:
8508 @table @code
8509 @item MAKEFILES
8511 Makefiles to be read on every invocation of @code{make}.@*
8512 @xref{MAKEFILES Variable, ,The Variable @code{MAKEFILES}}.
8514 @item VPATH
8516 Directory search path for files not found in the current directory.@*
8517 @xref{General Search, , @code{VPATH} Search Path for All Dependencies}.  
8519 @item SHELL
8521 The name of the system default command interpreter, usually @file{/bin/sh}.
8522 You can set @code{SHELL} in the makefile to change the shell used to run
8523 commands.  @xref{Execution, ,Command Execution}.
8525 @item MAKE
8527 The name with which @code{make} was invoked.
8528 Using this variable in commands has special meaning.
8529 @xref{MAKE Variable, ,How the @code{MAKE} Variable Works}.
8531 @item MAKELEVEL
8533 The number of levels of recursion (sub-@code{make}s).@*
8534 @xref{Variables/Recursion}.
8536 @item MAKEFLAGS
8538 The flags given to @code{make}.  You can set this in the environment or
8539 a makefile to set flags.@*
8540 @xref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
8542 @item SUFFIXES
8544 The default list of suffixes before @code{make} reads any makefiles.
8545 @end table
8547 @node Complex Makefile, Concept Index, Quick Reference, Top
8548 @appendix Complex Makefile Example
8550 Here is the makefile for the GNU @code{tar} program.  This is a
8551 moderately complex makefile.
8553 Because it is the first target, the default goal is @samp{all}.  An
8554 interesting feature of this makefile is that @file{testpad.h} is a
8555 source file automatically created by the @code{testpad} program,
8556 itself compiled from @file{testpad.c}.
8558 If you type @samp{make} or @samp{make all}, then @code{make} creates
8559 the @file{tar} executable, the @file{rmt} daemon that provides
8560 remote tape access, and the @file{tar.info} Info file.
8562 If you type @samp{make install}, then @code{make} not only creates
8563 @file{tar}, @file{rmt}, and @file{tar.info}, but also installs
8564 them.
8566 If you type @samp{make clean}, then @code{make} removes the @samp{.o}
8567 files, and the @file{tar}, @file{rmt}, @file{testpad},
8568 @file{testpad.h}, and @file{core} files.
8570 If you type @samp{make distclean}, then @code{make} not only removes
8571 the same files as does @samp{make clean} but also the
8572 @file{TAGS}, @file{Makefile}, and @file{config.status} files.
8573 (Although it is not evident, this makefile (and
8574 @file{config.status}) is generated by the user with the
8575 @code{configure} program, which is provided in the @code{tar}
8576 distribution, but is not shown here.)
8578 If you type @samp{make realclean}, then @code{make} removes the same
8579 files as does @samp{make distclean} and also removes the Info files
8580 generated from @file{tar.texinfo}.
8582 In addition, there are targets @code{shar} and @code{dist} that create
8583 distribution kits.
8585 @example
8586 @group
8587 # Generated automatically from Makefile.in by configure.
8588 # Un*x Makefile for GNU tar program.
8589 # Copyright (C) 1991 Free Software Foundation, Inc.
8590 @end group
8592 @group
8593 # This program is free software; you can redistribute
8594 # it and/or modify it under the terms of the GNU
8595 # General Public License @dots{}
8596 @dots{}
8597 @dots{}
8598 @end group
8600 SHELL = /bin/sh
8602 #### Start of system configuration section. ####
8604 srcdir = .
8606 @group
8607 # If you use gcc, you should either run the
8608 # fixincludes script that comes with it or else use
8609 # gcc with the -traditional option.  Otherwise ioctl
8610 # calls will be compiled incorrectly on some systems.
8611 CC = gcc -O
8612 YACC = bison -y
8613 INSTALL = /usr/local/bin/install -c
8614 INSTALLDATA = /usr/local/bin/install -c -m 644
8615 @end group
8617 # Things you might add to DEFS:
8618 # -DSTDC_HEADERS        If you have ANSI C headers and
8619 #                       libraries.
8620 # -DPOSIX               If you have POSIX.1 headers and 
8621 #                       libraries.
8622 # -DBSD42               If you have sys/dir.h (unless 
8623 #                       you use -DPOSIX), sys/file.h, 
8624 #                       and st_blocks in `struct stat'.
8625 # -DUSG                 If you have System V/ANSI C 
8626 #                       string and memory functions 
8627 #                       and headers, sys/sysmacros.h, 
8628 #                       fcntl.h, getcwd, no valloc, 
8629 #                       and ndir.h (unless 
8630 #                       you use -DDIRENT).
8631 # -DNO_MEMORY_H         If USG or STDC_HEADERS but do not 
8632 #                       include memory.h.
8633 # -DDIRENT              If USG and you have dirent.h 
8634 #                       instead of ndir.h.
8635 # -DSIGTYPE=int         If your signal handlers 
8636 #                       return int, not void.
8637 # -DNO_MTIO             If you lack sys/mtio.h 
8638 #                       (magtape ioctls).
8639 # -DNO_REMOTE           If you do not have a remote shell 
8640 #                       or rexec.
8641 # -DUSE_REXEC           To use rexec for remote tape
8642 #                       operations instead of 
8643 #                       forking rsh or remsh.
8644 # -DVPRINTF_MISSING     If you lack vprintf function 
8645 #                       (but have _doprnt).
8646 # -DDOPRNT_MISSING      If you lack _doprnt function.  
8647 #                       Also need to define 
8648 #                       -DVPRINTF_MISSING.
8649 # -DFTIME_MISSING       If you lack ftime system call.
8650 # -DSTRSTR_MISSING      If you lack strstr function.
8651 # -DVALLOC_MISSING      If you lack valloc function.
8652 # -DMKDIR_MISSING       If you lack mkdir and 
8653 #                       rmdir system calls.
8654 # -DRENAME_MISSING      If you lack rename system call.
8655 # -DFTRUNCATE_MISSING   If you lack ftruncate 
8656 #                       system call.
8657 # -DV7                  On Version 7 Unix (not 
8658 #                       tested in a long time).
8659 # -DEMUL_OPEN3          If you lack a 3-argument version 
8660 #                       of open, and want to emulate it 
8661 #                       with system calls you do have.
8662 # -DNO_OPEN3            If you lack the 3-argument open
8663 #                       and want to disable the tar -k 
8664 #                       option instead of emulating open.
8665 # -DXENIX               If you have sys/inode.h 
8666 #                       and need it 94 to be included.
8668 DEFS =  -DSIGTYPE=int -DDIRENT -DSTRSTR_MISSING \
8669         -DVPRINTF_MISSING -DBSD42
8670 # Set this to rtapelib.o unless you defined NO_REMOTE, 
8671 # in which case make it empty.
8672 RTAPELIB = rtapelib.o
8673 LIBS = 
8674 DEF_AR_FILE = /dev/rmt8
8675 DEFBLOCKING = 20
8677 @group
8678 CDEBUG = -g
8679 CFLAGS = $(CDEBUG) -I. -I$(srcdir) $(DEFS) \
8680         -DDEF_AR_FILE=\"$(DEF_AR_FILE)\" \
8681         -DDEFBLOCKING=$(DEFBLOCKING)
8682 LDFLAGS = -g
8683 @end group
8685 @group
8686 prefix = /usr/local
8687 # Prefix for each installed program, 
8688 # normally empty or `g'.
8689 binprefix = 
8691 # The directory to install tar in.
8692 bindir = $(prefix)/bin
8694 # The directory to install the info files in.
8695 infodir = $(prefix)/info
8696 @end group
8698 #### End of system configuration section. ####
8700 SRC1 =  tar.c create.c extract.c buffer.c \
8701         getoldopt.c update.c gnu.c mangle.c
8702 SRC2 =  version.c list.c names.c diffarch.c \
8703         port.c wildmat.c getopt.c
8704 SRC3 =  getopt1.c regex.c getdate.y
8705 SRCS =  $(SRC1) $(SRC2) $(SRC3)
8706 OBJ1 =  tar.o create.o extract.o buffer.o \
8707         getoldopt.o update.o gnu.o mangle.o
8708 OBJ2 =  version.o list.o names.o diffarch.o \
8709         port.o wildmat.o getopt.o
8710 OBJ3 =  getopt1.o regex.o getdate.o $(RTAPELIB)
8711 OBJS =  $(OBJ1) $(OBJ2) $(OBJ3)
8712 @group
8713 AUX =   README COPYING ChangeLog Makefile.in  \
8714         makefile.pc configure configure.in \
8715         tar.texinfo tar.info* texinfo.tex \
8716         tar.h port.h open3.h getopt.h regex.h \
8717         rmt.h rmt.c rtapelib.c alloca.c \
8718         msd_dir.h msd_dir.c tcexparg.c \
8719         level-0 level-1 backup-specs testpad.c
8720 @end group
8722 all:    tar rmt tar.info
8724 @group
8725 tar:    $(OBJS)
8726         $(CC) $(LDFLAGS) -o $@@ $(OBJS) $(LIBS)
8727 @end group
8729 @group
8730 rmt:    rmt.c
8731         $(CC) $(CFLAGS) $(LDFLAGS) -o $@@ rmt.c
8732 @end group
8734 @group
8735 tar.info: tar.texinfo
8736         makeinfo tar.texinfo
8737 @end group
8739 @group
8740 install: all
8741         $(INSTALL) tar $(bindir)/$(binprefix)tar
8742         -test ! -f rmt || $(INSTALL) rmt /etc/rmt
8743         $(INSTALLDATA) $(srcdir)/tar.info* $(infodir)
8744 @end group
8746 @group
8747 $(OBJS): tar.h port.h testpad.h
8748 regex.o buffer.o tar.o: regex.h
8749 # getdate.y has 8 shift/reduce conflicts.
8750 @end group
8752 @group
8753 testpad.h: testpad
8754         ./testpad
8755 @end group
8757 @group
8758 testpad: testpad.o
8759         $(CC) -o $@@ testpad.o
8760 @end group
8762 @group
8763 TAGS:   $(SRCS)
8764         etags $(SRCS)
8765 @end group
8767 @group
8768 clean:
8769         rm -f *.o tar rmt testpad testpad.h core
8770 @end group
8772 @group
8773 distclean: clean
8774         rm -f TAGS Makefile config.status
8775 @end group
8777 @group
8778 realclean: distclean
8779         rm -f tar.info*
8780 @end group
8782 @group
8783 shar: $(SRCS) $(AUX)
8784         shar $(SRCS) $(AUX) | compress \
8785           > tar-`sed -e '/version_string/!d' \
8786                      -e 's/[^0-9.]*\([0-9.]*\).*/\1/' \
8787                      -e q 
8788                      version.c`.shar.Z
8789 @end group
8791 @group
8792 dist: $(SRCS) $(AUX)
8793         echo tar-`sed \
8794              -e '/version_string/!d' \
8795              -e 's/[^0-9.]*\([0-9.]*\).*/\1/' \
8796              -e q 
8797              version.c` > .fname
8798         -rm -rf `cat .fname`
8799         mkdir `cat .fname`
8800         ln $(SRCS) $(AUX) `cat .fname`
8801         -rm -rf `cat .fname` .fname
8802         tar chZf `cat .fname`.tar.Z `cat .fname`
8803 @end group
8805 @group
8806 tar.zoo: $(SRCS) $(AUX)
8807         -rm -rf tmp.dir
8808         -mkdir tmp.dir
8809         -rm tar.zoo
8810         for X in $(SRCS) $(AUX) ; do \
8811             echo $$X ; \
8812             sed 's/$$/^M/' $$X \
8813             > tmp.dir/$$X ; done
8814         cd tmp.dir ; zoo aM ../tar.zoo *
8815         -rm -rf tmp.dir
8816 @end group
8817 @end example
8819 @node Concept Index, Name Index, Complex Makefile, Top
8820 @unnumbered Index of Concepts
8822 @printindex cp
8824 @node Name Index,  , Concept Index, Top
8825 @unnumbered Index of Functions, Variables, & Directives
8827 @printindex fn
8829 @summarycontents
8830 @contents
8831 @bye