* Various changes.
[make/kirr.git] / make.texinfo
blob44973bd95a1284043182a7839f53c9a94a718afd
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.179 1999/08/22 17:51:01 psmith Exp $
11 @set EDITION 0.53
12 @set VERSION 3.78
13 @set UPDATED 14 April 1999
14 @set UPDATE-MONTH April 1999
15 @comment The ISBN number might need to change on next publication.
16 @set ISBN 1-882114-80-9 @c CHANGE THIS BEFORE PRINTING AGAIN! --psmith 16jul98
18 @c finalout
20 @c ISPELL CHECK: done, 10 June 1993 --roland
22 @c Combine the variable and function indices:
23 @syncodeindex vr fn
24 @c Combine the program and concept indices:
25 @syncodeindex pg cp
27 @dircategory GNU Packages
28 @direntry
29 * Make: (make).            Remake files automatically.
30 @end direntry
32 @ifinfo
33 This file documents the GNU Make utility, which determines
34 automatically which pieces of a large program need to be recompiled,
35 and issues the commands to recompile them.
37 This is Edition @value{EDITION}, last updated @value{UPDATED},
38 of @cite{The GNU Make Manual}, for @code{make}, Version @value{VERSION}.
40 Copyright (C) 1988, '89, '90, '91, '92, '93, '94, '95, '96, '97, '98, '99
41         Free Software Foundation, Inc.
43 Permission is granted to make and distribute verbatim copies of
44 this manual provided the copyright notice and this permission notice
45 are preserved on all copies.
47 @ignore
48 Permission is granted to process this file through TeX and print the
49 results, provided the printed document carries copying permission
50 notice identical to this one except for the removal of this paragraph
51 (this paragraph not being relevant to the printed manual).
53 @end ignore
54 Permission is granted to copy and distribute modified versions of this
55 manual under the conditions for verbatim copying, provided that the entire
56 resulting derived work is distributed under the terms of a permission
57 notice identical to this one.
59 Permission is granted to copy and distribute translations of this manual
60 into another language, under the above conditions for modified versions,
61 except that this permission notice may be stated in a translation approved
62 by the Free Software Foundation.
63 @end ifinfo
65 @iftex
66 @shorttitlepage GNU Make
67 @end iftex
68 @titlepage
69 @title GNU Make
70 @subtitle A Program for Directing Recompilation
71 @subtitle GNU @code{make} Version @value{VERSION}
72 @subtitle @value{UPDATE-MONTH}
73 @author Richard M. Stallman and Roland McGrath
74 @page
75 @vskip 0pt plus 1filll
76 Copyright @copyright{} 1988, '89, '90, '91, '92, '93, '94, '95, '96, '97, '98
77 Free Software Foundation, Inc.
78 @sp 2
79 Published by the Free Software Foundation @*
80 59 Temple Place -- Suite 330, @*
81 Boston, MA 02111-1307 USA @*
82 ISBN @value{ISBN} @*
84 Maintenance and updates since Version 3.76 by Paul D. Smith.
86 Permission is granted to make and distribute verbatim copies of
87 this manual provided the copyright notice and this permission notice
88 are preserved on all copies.
90 Permission is granted to copy and distribute modified versions of this
91 manual under the conditions for verbatim copying, provided that the entire
92 resulting derived work is distributed under the terms of a permission
93 notice identical to this one.
95 Permission is granted to copy and distribute translations of this manual
96 into another language, under the above conditions for modified versions,
97 except that this permission notice may be stated in a translation approved
98 by the Free Software Foundation.
99 @sp 2
100 Cover art by Etienne Suvasa.
101 @end titlepage
102 @page
104 @ifinfo
105 @node Top, Overview, , (dir)
106 @top Make
108 The GNU @code{make} utility automatically determines which pieces of a
109 large program need to be recompiled, and issues the commands to
110 recompile them.@refill
112 This edition of the @cite{GNU Make Manual},
113 last updated @value{UPDATED},
114 documents GNU @code{make} Version @value{VERSION}.@refill
116 This manual describes @code{make} and contains the following chapters:@refill
117 @end ifinfo
119 @menu
120 * Overview::                    Overview of @code{make}.
121 * Introduction::                An introduction to @code{make}.
122 * Makefiles::                   Makefiles tell @code{make} what to do.
123 * Rules::                       Rules describe when a file must be remade.
124 * Commands::                    Commands say how to remake a file.
125 * Using Variables::             You can use variables to avoid repetition.
126 * Conditionals::                Use or ignore parts of the makefile based
127                                  on the values of variables.
128 * Functions::                   Many powerful ways to manipulate text.
129 * Invoking make: Running.       How to invoke @code{make} on the command line.
130 * Implicit Rules::              Use implicit rules to treat many files alike,
131                                  based on their file names.
132 * Archives::                    How @code{make} can update library archives.
133 * Features::                    Features GNU @code{make} has over other @code{make}s.
134 * Missing::                     What GNU @code{make} lacks from other @code{make}s.
135 * Makefile Conventions::        Conventions for makefiles in GNU programs.
136 * Quick Reference::             A quick reference for experienced users.
137 * Make Errors::                 A list of common errors generated by @code{make}.
138 * Complex Makefile::            A real example of a straightforward,
139                                  but nontrivial, makefile.
140 * Concept Index::               Index of Concepts
141 * Name Index::                  Index of Functions, Variables, & Directives
143  --- The Detailed Node Listing ---
145 Overview of @code{make}
147 * Preparing::                   Preparing and Running Make
148 * Reading::                     On Reading this Text
149 * Bugs::                        Problems and Bugs
151 An Introduction to Makefiles
153 * Rule Introduction::           What a rule looks like.
154 * Simple Makefile::             A Simple Makefile
155 * How Make Works::              How @code{make} Processes This Makefile
156 * Variables Simplify::          Variables Make Makefiles Simpler
157 * make Deduces::                Letting @code{make} Deduce the Commands
158 * Combine By Dependency::       Another Style of Makefile
159 * Cleanup::                     Rules for Cleaning the Directory
161 Writing Makefiles
163 * Makefile Contents::           What makefiles contain.
164 * Makefile Names::              How to name your makefile.
165 * Include::                     How one makefile can use another makefile.
166 * MAKEFILES Variable::          The environment can specify extra makefiles.
167 * Remaking Makefiles::          How makefiles get remade.
168 * Overriding Makefiles::        How to override part of one makefile
169                                  with another makefile.
171 Writing Rules
173 * Rule Example::                An example explained.
174 * Rule Syntax::                 General syntax explained.
175 * Wildcards::                   Using wildcard characters such as `*'.
176 * Directory Search::            Searching other directories for source files.
177 * Phony Targets::               Using a target that is not a real file's name.
178 * Force Targets::               You can use a target without commands
179                                   or dependencies to mark other
180                                   targets as phony.
181 * Empty Targets::               When only the date matters and the
182                                   files are empty.
183 * Special Targets::             Targets with special built-in meanings.
184 * Multiple Targets::            When to make use of several targets in a rule.
185 * Multiple Rules::              How to use several rules with the same target.
186 * Static Pattern::              Static pattern rules apply to multiple targets
187                                   and can vary the dependencies according to
188                                   the target name.
189 * Double-Colon::                How to use a special kind of rule to allow
190                                   several independent rules for one target.
191 * Automatic Dependencies::      How to automatically generate rules giving
192                                  dependencies from the source files themselves.
194 Using Wildcard Characters in File Names
196 * Wildcard Examples::           Several examples
197 * Wildcard Pitfall::            Problems to avoid.
198 * Wildcard Function::           How to cause wildcard expansion where
199                                   it does not normally take place.
201 Searching Directories for Dependencies
203 * General Search::              Specifying a search path that applies
204                                   to every dependency.
205 * Selective Search::            Specifying a search path
206                                   for a specified class of names.
207 * Search Algorithm::            When and how search paths are applied.
208 * Commands/Search::             How to write shell commands that work together
209                                   with search paths.
210 * Implicit/Search::             How search paths affect implicit rules.
211 * Libraries/Search::            Directory search for link libraries.
213 Static Pattern Rules
215 * Static Usage::                The syntax of static pattern rules.
216 * Static versus Implicit::      When are they better than implicit rules?
218 Writing the Commands in Rules
220 * Echoing::                     How to control when commands are echoed.
221 * Execution::                   How commands are executed.
222 * Parallel::                    How commands can be executed in parallel.
223 * Errors::                      What happens after a command execution error.
224 * Interrupts::                  What happens when a command is interrupted.
225 * Recursion::                   Invoking @code{make} from makefiles.
226 * Sequences::                   Defining canned sequences of commands.
227 * Empty Commands::              Defining useful, do-nothing commands.
229 Recursive Use of @code{make}
231 * MAKE Variable::               The special effects of using @samp{$(MAKE)}.
232 * Variables/Recursion::         How to communicate variables to a sub-@code{make}.
233 * Options/Recursion::           How to communicate options to a sub-@code{make}.
234 * -w Option::                   How the @samp{-w} or @samp{--print-directory} option
235                                  helps debug use of recursive @code{make} commands.
237 How to Use Variables
239 * Reference::                   How to use the value of a variable.
240 * Flavors::                     Variables come in two flavors.
241 * Advanced::                    Advanced features for referencing a variable.
242 * Values::                      All the ways variables get their values.
243 * Setting::                     How to set a variable in the makefile.
244 * Appending::                   How to append more text to the old value
245                                   of a variable.
246 * Override Directive::          How to set a variable in the makefile even if
247                                   the user has set it with a command argument.
248 * Defining::                    An alternate way to set a variable
249                                   to a verbatim string.
250 * Environment::                 Variable values can come from the environment.
251 * Automatic::                   Some special variables have predefined
252                                   meanings for use with implicit rules.
254 Advanced Features for Reference to Variables
256 * Substitution Refs::           Referencing a variable with
257                                   substitutions on the value.
258 * Computed Names::              Computing the name of the variable to refer to.
260 Conditional Parts of Makefiles
262 * Conditional Example::         Example of a conditional
263 * Conditional Syntax::          The syntax of conditionals.
264 * Testing Flags::               Conditionals that test flags.
266 Functions for Transforming Text
268 * Syntax of Functions::         How to write a function call.
269 * Text Functions::              General-purpose text manipulation functions.
270 * File Name Functions::         Functions for manipulating file names.
271 * Foreach Function::            Repeat some text with controlled variation.
272 * Call Function::               Expand a user-defined function.
273 * Origin Function::             Find where a variable got its value.
274 * Shell Function::              Substitute the output of a shell command.
276 How to Run @code{make}
278 * Makefile Arguments::          How to specify which makefile to use.
279 * Goals::                       How to use goal arguments to specify which
280                                   parts of the makefile to use.
281 * Instead of Execution::        How to use mode flags to specify what
282                                   kind of thing to do with the commands
283                                   in the makefile other than simply
284                                   execute them.
285 * Avoiding Compilation::        How to avoid recompiling certain files.
286 * Overriding::                  How to override a variable to specify
287                                   an alternate compiler and other things.
288 * Testing::                     How to proceed past some errors, to
289                                   test compilation.
290 * Options Summary::             Summary of Options
292 Using Implicit Rules
294 * Using Implicit::              How to use an existing implicit rule
295                                   to get the commands for updating a file.
296 * Catalogue of Rules::          A list of built-in implicit rules.
297 * Implicit Variables::          How to change what predefined rules do.
298 * Chained Rules::               How to use a chain of implicit rules.
299 * Pattern Rules::               How to define new implicit rules.
300 * Last Resort::                 How to defining commands for rules
301                                   which cannot find any.
302 * Suffix Rules::                The old-fashioned style of implicit rule.
303 * Implicit Rule Search::        The precise algorithm for applying
304                                   implicit rules.
306 Defining and Redefining Pattern Rules
308 * Pattern Intro::               An introduction to pattern rules.
309 * Pattern Examples::            Examples of pattern rules.
310 * Automatic::                   How to use automatic variables in the
311                                   commands of implicit rules.
312 * Pattern Match::               How patterns match.
313 * Match-Anything Rules::        Precautions you should take prior to
314                                   defining rules that can match any
315                                   target file whatever.
316 * Canceling Rules::             How to override or cancel built-in rules.
318 Using @code{make} to Update Archive Files
320 * Archive Members::             Archive members as targets.
321 * Archive Update::              The implicit rule for archive member targets.
322 * Archive Pitfalls::            Dangers to watch out for when using archives.
323 * Archive Suffix Rules::        You can write a special kind of suffix rule
324                                   for updating archives.
326 Implicit Rule for Archive Member Targets
328 * Archive Symbols::             How to update archive symbol directories.
329 @end menu
331 @node Overview, Introduction, Top, Top
332 @comment  node-name,  next,  previous,  up
333 @chapter Overview of @code{make}
335 The @code{make} utility automatically determines which pieces of a large
336 program need to be recompiled, and issues commands to recompile them.
337 This manual describes GNU @code{make}, which was implemented by Richard
338 Stallman and Roland McGrath.  Development since Version 3.76 has been
339 handled by Paul D. Smith.
341 GNU @code{make} conforms to section 6.2 of @cite{IEEE Standard
342 1003.2-1992} (POSIX.2).
343 @cindex POSIX
344 @cindex IEEE Standard 1003.2
345 @cindex standards conformance
347 Our examples show C programs, since they are most common, but you can use
348 @code{make} with any programming language whose compiler can be run with a
349 shell command.  Indeed, @code{make} is not limited to programs.  You can
350 use it to describe any task where some files must be updated automatically
351 from others whenever the others change.
353 @menu
354 * Preparing::                   Preparing and Running Make
355 * Reading::                     On Reading this Text
356 * Bugs::                        Problems and Bugs
357 @end menu
359 @node Preparing, Reading,  , Overview
360 @ifinfo
361 @heading Preparing and Running Make
362 @end ifinfo
364 To prepare to use @code{make}, you must write a file called
365 the @dfn{makefile} that describes the relationships among files
366 in your program and provides commands for updating each file.
367 In a program, typically, the executable file is updated from object
368 files, which are in turn made by compiling source files.@refill
370 Once a suitable makefile exists, each time you change some source files,
371 this simple shell command:
373 @example
374 make
375 @end example
377 @noindent
378 suffices to perform all necessary recompilations.  The @code{make} program
379 uses the makefile data base and the last-modification times of the files to
380 decide which of the files need to be updated.  For each of those files, it
381 issues the commands recorded in the data base.
383 You can provide command line arguments to @code{make} to control which
384 files should be recompiled, or how.  @xref{Running, ,How to Run
385 @code{make}}.
387 @node Reading, Bugs, Preparing, Overview
388 @section How to Read This Manual
390 If you are new to @code{make}, or are looking for a general
391 introduction, read the first few sections of each chapter, skipping the
392 later sections.  In each chapter, the first few sections contain
393 introductory or general information and the later sections contain
394 specialized or technical information.
395 @ifinfo
396 The exception is the second chapter, @ref{Introduction, ,An
397 Introduction to Makefiles}, all of which is introductory.
398 @end ifinfo
399 @iftex
400 The exception is @ref{Introduction, ,An Introduction to Makefiles},
401 all of which is introductory.
402 @end iftex
404 If you are familiar with other @code{make} programs, see @ref{Features,
405 ,Features of GNU @code{make}}, which lists the enhancements GNU
406 @code{make} has, and @ref{Missing, ,Incompatibilities and Missing
407 Features}, which explains the few things GNU @code{make} lacks that
408 others have.
410 For a quick summary, see @ref{Options Summary}, @ref{Quick Reference},
411 and @ref{Special Targets}.
413 @node Bugs,  , Reading, Overview
414 @section Problems and Bugs
415 @cindex reporting bugs
416 @cindex bugs, reporting
417 @cindex problems and bugs, reporting
419 If you have problems with GNU @code{make} or think you've found a bug,
420 please report it to the developers; we cannot promise to do anything but
421 we might well want to fix it.
423 Before reporting a bug, make sure you've actually found a real bug.
424 Carefully reread the documentation and see if it really says you can do
425 what you're trying to do.  If it's not clear whether you should be able
426 to do something or not, report that too; it's a bug in the
427 documentation!
429 Before reporting a bug or trying to fix it yourself, try to isolate it
430 to the smallest possible makefile that reproduces the problem.  Then
431 send us the makefile and the exact results @code{make} gave you.  Also
432 say what you expected to occur; this will help us decide whether the
433 problem was really in the documentation.
435 Once you've got a precise problem, please send electronic mail to:
437 @example
438     bug-make@@gnu.org
439 @end example
441 @noindent
442 Please include the version number of @code{make} you are using.  You can
443 get this information with the command @samp{make --version}.
444 Be sure also to include the type of machine and operating system you are
445 using.  If possible, include the contents of the file @file{config.h}
446 that is generated by the configuration process.
448 @node Introduction, Makefiles, Overview, Top
449 @comment  node-name,  next,  previous,  up
450 @chapter An Introduction to Makefiles
452 You need a file called a @dfn{makefile} to tell @code{make} what to do.
453 Most often, the makefile tells @code{make} how to compile and link a
454 program.
455 @cindex makefile
457 In this chapter, we will discuss a simple makefile that describes how to
458 compile and link a text editor which consists of eight C source files
459 and three header files.  The makefile can also tell @code{make} how to
460 run miscellaneous commands when explicitly asked (for example, to remove
461 certain files as a clean-up operation).  To see a more complex example
462 of a makefile, see @ref{Complex Makefile}.
464 When @code{make} recompiles the editor, each changed C source file
465 must be recompiled.  If a header file has changed, each C source file
466 that includes the header file must be recompiled to be safe.  Each
467 compilation produces an object file corresponding to the source file.
468 Finally, if any source file has been recompiled, all the object files,
469 whether newly made or saved from previous compilations, must be linked
470 together to produce the new executable editor.
471 @cindex recompilation
472 @cindex editor
474 @menu
475 * Rule Introduction::           What a rule looks like.
476 * Simple Makefile::             A Simple Makefile
477 * How Make Works::              How @code{make} Processes This Makefile
478 * Variables Simplify::          Variables Make Makefiles Simpler
479 * make Deduces::                Letting @code{make} Deduce the Commands
480 * Combine By Dependency::       Another Style of Makefile
481 * Cleanup::                     Rules for Cleaning the Directory
482 @end menu
484 @node Rule Introduction, Simple Makefile,  , Introduction
485 @comment  node-name,  next,  previous,  up
486 @section What a Rule Looks Like
487 @cindex rule, introduction to
488 @cindex makefile rule parts
489 @cindex parts of makefile rule
491 A simple makefile consists of ``rules'' with the following shape:
493 @cindex targets, introduction to
494 @cindex dependencies, introduction to
495 @cindex commands, introduction to
496 @example
497 @group
498 @var{target} @dots{} : @var{dependencies} @dots{}
499         @var{command}
500         @dots{}
501         @dots{}
502 @end group
503 @end example
505 A @dfn{target} is usually the name of a file that is generated by a
506 program; examples of targets are executable or object files.  A target
507 can also be the name of an action to carry out, such as @samp{clean}
508 (@pxref{Phony Targets}).
510 A @dfn{dependency} is a file that is used as input to create the
511 target.  A target often depends on several files.
513 @cindex tabs in rules
514 A @dfn{command} is an action that @code{make} carries out.
515 A rule may have more than one command, each on its own line.
516 @strong{Please note:} you need to put a tab character at the beginning of
517 every command line!  This is an obscurity that catches the unwary.
519 Usually a command is in a rule with dependencies and serves to create a
520 target file if any of the dependencies change.  However, the rule that
521 specifies commands for the target need not have dependencies.  For
522 example, the rule containing the delete command associated with the
523 target @samp{clean} does not have dependencies.
525 A @dfn{rule}, then, explains how and when to remake certain files
526 which are the targets of the particular rule.  @code{make} carries out
527 the commands on the dependencies to create or update the target.  A
528 rule can also explain how and when to carry out an action.
529 @xref{Rules, , Writing Rules}.
531 A makefile may contain other text besides rules, but a simple makefile
532 need only contain rules.  Rules may look somewhat more complicated
533 than shown in this template, but all fit the pattern more or less.
535 @node Simple Makefile, How Make Works, Rule Introduction, Introduction
536 @section A Simple Makefile
537 @cindex simple makefile
538 @cindex makefile, simple
540 Here is a straightforward makefile that describes the way an
541 executable file called @code{edit} depends on eight object files
542 which, in turn, depend on eight C source and three header files.
544 In this example, all the C files include @file{defs.h}, but only those
545 defining editing commands include @file{command.h}, and only low
546 level files that change the editor buffer include @file{buffer.h}.
548 @example
549 @group
550 edit : main.o kbd.o command.o display.o \
551        insert.o search.o files.o utils.o
552         cc -o edit main.o kbd.o command.o display.o \
553                    insert.o search.o files.o utils.o
555 main.o : main.c defs.h
556         cc -c main.c
557 kbd.o : kbd.c defs.h command.h
558         cc -c kbd.c
559 command.o : command.c defs.h command.h
560         cc -c command.c
561 display.o : display.c defs.h buffer.h
562         cc -c display.c
563 insert.o : insert.c defs.h buffer.h
564         cc -c insert.c
565 search.o : search.c defs.h buffer.h
566         cc -c search.c
567 files.o : files.c defs.h buffer.h command.h
568         cc -c files.c
569 utils.o : utils.c defs.h
570         cc -c utils.c
571 clean :
572         rm edit main.o kbd.o command.o display.o \
573            insert.o search.o files.o utils.o
574 @end group
575 @end example
577 @noindent
578 We split each long line into two lines using backslash-newline; this is
579 like using one long line, but is easier to read.
580 @cindex continuation lines
581 @cindex @code{\} (backslash), for continuation lines
582 @cindex backslash (@code{\}), for continuation lines
583 @cindex quoting newline, in makefile
584 @cindex newline, quoting, in makefile
586 To use this makefile to create the executable file called @file{edit},
587 type:
589 @example
590 make
591 @end example
593 To use this makefile to delete the executable file and all the object
594 files from the directory, type:
596 @example
597 make clean
598 @end example
600 In the example makefile, the targets include the executable file
601 @samp{edit}, and the object files @samp{main.o} and @samp{kbd.o}.  The
602 dependencies are files such as @samp{main.c} and @samp{defs.h}.
603 In fact, each @samp{.o} file is both a target and a dependency.
604 Commands include @w{@samp{cc -c main.c}} and @w{@samp{cc -c kbd.c}}.
606 When a target is a file, it needs to be recompiled or relinked if any
607 of its dependencies change.  In addition, any dependencies that are
608 themselves automatically generated should be updated first.  In this
609 example, @file{edit} depends on each of the eight object files; the
610 object file @file{main.o} depends on the source file @file{main.c} and
611 on the header file @file{defs.h}.
613 A shell command follows each line that contains a target and
614 dependencies.  These shell commands say how to update the target file.
615 A tab character must come at the beginning of every command line to
616 distinguish commands lines from other lines in the makefile.  (Bear in
617 mind that @code{make} does not know anything about how the commands
618 work.  It is up to you to supply commands that will update the target
619 file properly.  All @code{make} does is execute the commands in the rule
620 you have specified when the target file needs to be updated.)
621 @cindex shell command
623 The target @samp{clean} is not a file, but merely the name of an
624 action.  Since you
625 normally
626 do not want to carry out the actions in this rule, @samp{clean} is not a dependency of any other rule.
627 Consequently, @code{make} never does anything with it unless you tell
628 it specifically.  Note that this rule not only is not a dependency, it
629 also does not have any dependencies, so the only purpose of the rule
630 is to run the specified commands.  Targets that do not refer to files
631 but are just actions are called @dfn{phony targets}.  @xref{Phony
632 Targets}, for information about this kind of target.  @xref{Errors, ,
633 Errors in Commands}, to see how to cause @code{make} to ignore errors
634 from @code{rm} or any other command.
635 @cindex @code{clean} target
636 @cindex @code{rm} (shell command)
638 @node How Make Works, Variables Simplify, Simple Makefile, Introduction
639 @comment  node-name,  next,  previous,  up
640 @section How @code{make} Processes a Makefile
641 @cindex processing a makefile
642 @cindex makefile, how @code{make} processes
644 By default, @code{make} starts with the first target (not targets whose
645 names start with @samp{.}).  This is called the @dfn{default goal}.
646 (@dfn{Goals} are the targets that @code{make} strives ultimately to
647 update.  @xref{Goals, , Arguments to Specify the Goals}.)
648 @cindex default goal
649 @cindex goal, default
650 @cindex goal
652 In the simple example of the previous section, the default goal is to
653 update the executable program @file{edit}; therefore, we put that rule
654 first.
656 Thus, when you give the command:
658 @example
659 make
660 @end example
662 @noindent
663 @code{make} reads the makefile in the current directory and begins by
664 processing the first rule.  In the example, this rule is for relinking
665 @file{edit}; but before @code{make} can fully process this rule, it
666 must process the rules for the files that @file{edit} depends on,
667 which in this case are the object files.  Each of these files is
668 processed according to its own rule.  These rules say to update each
669 @samp{.o} file by compiling its source file.  The recompilation must
670 be done if the source file, or any of the header files named as
671 dependencies, is more recent than the object file, or if the object
672 file does not exist.
674 The other rules are processed because their targets appear as
675 dependencies of the goal.  If some other rule is not depended on by the
676 goal (or anything it depends on, etc.), that rule is not processed,
677 unless you tell @code{make} to do so (with a command such as
678 @w{@code{make clean}}).
680 Before recompiling an object file, @code{make} considers updating its
681 dependencies, the source file and header files.  This makefile does not
682 specify anything to be done for them---the @samp{.c} and @samp{.h} files
683 are not the targets of any rules---so @code{make} does nothing for these
684 files.  But @code{make} would update automatically generated C programs,
685 such as those made by Bison or Yacc, by their own rules at this time.
687 After recompiling whichever object files need it, @code{make} decides
688 whether to relink @file{edit}.  This must be done if the file
689 @file{edit} does not exist, or if any of the object files are newer than
690 it.  If an object file was just recompiled, it is now newer than
691 @file{edit}, so @file{edit} is relinked.
692 @cindex relinking
694 Thus, if we change the file @file{insert.c} and run @code{make},
695 @code{make} will compile that file to update @file{insert.o}, and then
696 link @file{edit}.  If we change the file @file{command.h} and run
697 @code{make}, @code{make} will recompile the object files @file{kbd.o},
698 @file{command.o} and @file{files.o} and then link the file @file{edit}.
700 @node Variables Simplify, make Deduces, How Make Works, Introduction
701 @section Variables Make Makefiles Simpler
702 @cindex variables
703 @cindex simplifying with variables
705 In our example, we had to list all the object files twice in the rule for
706 @file{edit} (repeated here):
708 @example
709 @group
710 edit : main.o kbd.o command.o display.o \
711               insert.o search.o files.o utils.o
712         cc -o edit main.o kbd.o command.o display.o \
713                    insert.o search.o files.o utils.o
714 @end group
715 @end example
717 @cindex @code{objects}
718 Such duplication is error-prone; if a new object file is added to the
719 system, we might add it to one list and forget the other.  We can eliminate
720 the risk and simplify the makefile by using a variable.  @dfn{Variables}
721 allow a text string to be defined once and substituted in multiple places
722 later (@pxref{Using Variables, ,How to Use Variables}).
724 @cindex @code{OBJECTS}
725 @cindex @code{objs}
726 @cindex @code{OBJS}
727 @cindex @code{obj}
728 @cindex @code{OBJ}
729 It is standard practice for every makefile to have a variable named
730 @code{objects}, @code{OBJECTS}, @code{objs}, @code{OBJS}, @code{obj},
731 or @code{OBJ} which is a list of all object file names.  We would
732 define such a variable @code{objects} with a line like this in the
733 makefile:@refill
735 @example
736 @group
737 objects = main.o kbd.o command.o display.o \
738           insert.o search.o files.o utils.o
739 @end group
740 @end example
742 @noindent
743 Then, each place we want to put a list of the object file names, we can
744 substitute the variable's value by writing @samp{$(objects)}
745 (@pxref{Using Variables, ,How to Use Variables}).
747 Here is how the complete simple makefile looks when you use a variable
748 for the object files:
750 @example
751 @group
752 objects = main.o kbd.o command.o display.o \
753           insert.o search.o files.o utils.o
755 edit : $(objects)
756         cc -o edit $(objects)
757 main.o : main.c defs.h
758         cc -c main.c
759 kbd.o : kbd.c defs.h command.h
760         cc -c kbd.c
761 command.o : command.c defs.h command.h
762         cc -c command.c
763 display.o : display.c defs.h buffer.h
764         cc -c display.c
765 insert.o : insert.c defs.h buffer.h
766         cc -c insert.c
767 search.o : search.c defs.h buffer.h
768         cc -c search.c
769 files.o : files.c defs.h buffer.h command.h
770         cc -c files.c
771 utils.o : utils.c defs.h
772         cc -c utils.c
773 clean :
774         rm edit $(objects)
775 @end group
776 @end example
778 @node make Deduces, Combine By Dependency, Variables Simplify, Introduction
779 @section Letting @code{make} Deduce the Commands
780 @cindex deducing commands (implicit rules)
781 @cindex implicit rule, introduction to
782 @cindex rule, implicit, introduction to
784 It is not necessary to spell out the commands for compiling the individual
785 C source files, because @code{make} can figure them out: it has an
786 @dfn{implicit rule} for updating a @samp{.o} file from a correspondingly
787 named @samp{.c} file using a @samp{cc -c} command.  For example, it will
788 use the command @samp{cc -c main.c -o main.o} to compile @file{main.c} into
789 @file{main.o}.  We can therefore omit the commands from the rules for the
790 object files.  @xref{Implicit Rules, ,Using Implicit Rules}.@refill
792 When a @samp{.c} file is used automatically in this way, it is also
793 automatically added to the list of dependencies.  We can therefore omit
794 the @samp{.c} files from the dependencies, provided we omit the commands.
796 Here is the entire example, with both of these changes, and a variable
797 @code{objects} as suggested above:
799 @example
800 @group
801 objects = main.o kbd.o command.o display.o \
802           insert.o search.o files.o utils.o
804 edit : $(objects)
805         cc -o edit $(objects)
807 main.o : defs.h
808 kbd.o : defs.h command.h
809 command.o : defs.h command.h
810 display.o : defs.h buffer.h
811 insert.o : defs.h buffer.h
812 search.o : defs.h buffer.h
813 files.o : defs.h buffer.h command.h
814 utils.o : defs.h
816 .PHONY : clean
817 clean :
818         -rm edit $(objects)
819 @end group
820 @end example
822 @noindent
823 This is how we would write the makefile in actual practice.  (The
824 complications associated with @samp{clean} are described elsewhere.
825 See @ref{Phony Targets}, and @ref{Errors, ,Errors in Commands}.)
827 Because implicit rules are so convenient, they are important.  You
828 will see them used frequently.@refill
830 @node Combine By Dependency, Cleanup, make Deduces, Introduction
831 @section Another Style of Makefile
832 @cindex combining rules by dependency
834 When the objects of a makefile are created only by implicit rules, an
835 alternative style of makefile is possible.  In this style of makefile,
836 you group entries by their dependencies instead of by their targets.
837 Here is what one looks like:
839 @example
840 @group
841 objects = main.o kbd.o command.o display.o \
842           insert.o search.o files.o utils.o
844 edit : $(objects)
845         cc -o edit $(objects)
847 $(objects) : defs.h
848 kbd.o command.o files.o : command.h
849 display.o insert.o search.o files.o : buffer.h
850 @end group
851 @end example
853 @noindent
854 Here @file{defs.h} is given as a dependency of all the object files;
855 @file{command.h} and @file{buffer.h} are dependencies of the specific
856 object files listed for them.
858 Whether this is better is a matter of taste: it is more compact, but some
859 people dislike it because they find it clearer to put all the information
860 about each target in one place.
862 @node Cleanup,  , Combine By Dependency, Introduction
863 @section Rules for Cleaning the Directory
864 @cindex cleaning up
865 @cindex removing, to clean up
867 Compiling a program is not the only thing you might want to write rules
868 for.  Makefiles commonly tell how to do a few other things besides
869 compiling a program: for example, how to delete all the object files
870 and executables so that the directory is @samp{clean}.
872 @cindex @code{clean} target
873 Here is how we
874 could write a @code{make} rule for cleaning our example editor:
876 @example
877 @group
878 clean:
879         rm edit $(objects)
880 @end group
881 @end example
883 In practice, we might want to write the rule in a somewhat more
884 complicated manner to handle unanticipated situations.  We would do this:
886 @example
887 @group
888 .PHONY : clean
889 clean :
890         -rm edit $(objects)
891 @end group
892 @end example
894 @noindent
895 This prevents @code{make} from getting confused by an actual file
896 called @file{clean} and causes it to continue in spite of errors from
897 @code{rm}.  (See @ref{Phony Targets}, and @ref{Errors, ,Errors in
898 Commands}.)
900 @noindent
901 A rule such as this should not be placed at the beginning of the
902 makefile, because we do not want it to run by default!  Thus, in the
903 example makefile, we want the rule for @code{edit}, which recompiles
904 the editor, to remain the default goal.
906 Since @code{clean} is not a dependency of @code{edit}, this rule will not
907 run at all if we give the command @samp{make} with no arguments.  In
908 order to make the rule run, we have to type @samp{make clean}.
909 @xref{Running, ,How to Run @code{make}}.
911 @node Makefiles, Rules, Introduction, Top
912 @chapter Writing Makefiles
914 @cindex makefile, how to write
915 The information that tells @code{make} how to recompile a system comes from
916 reading a data base called the @dfn{makefile}.
918 @menu
919 * Makefile Contents::           What makefiles contain.
920 * Makefile Names::              How to name your makefile.
921 * Include::                     How one makefile can use another makefile.
922 * MAKEFILES Variable::          The environment can specify extra makefiles.
923 * Remaking Makefiles::          How makefiles get remade.
924 * Overriding Makefiles::        How to override part of one makefile
925                                  with another makefile.
926 @end menu
928 @node Makefile Contents, Makefile Names,  , Makefiles
929 @section What Makefiles Contain
931 Makefiles contain five kinds of things: @dfn{explicit rules},
932 @dfn{implicit rules}, @dfn{variable definitions}, @dfn{directives},
933 and @dfn{comments}.  Rules, variables, and directives are described at
934 length in later chapters.@refill
936 @itemize @bullet
937 @cindex rule, explicit, definition of
938 @cindex explicit rule, definition of
939 @item
940 An @dfn{explicit rule} says when and how to remake one or more files,
941 called the rule's targets.  It lists the other files that the targets
942 @dfn{depend on}, and may also give commands to use to create or update
943 the targets.  @xref{Rules, ,Writing Rules}.
945 @cindex rule, implicit, definition of
946 @cindex implicit rule, definition of
947 @item
948 An @dfn{implicit rule} says when and how to remake a class of files
949 based on their names.  It describes how a target may depend on a file
950 with a name similar to the target and gives commands to create or
951 update such a target.  @xref{Implicit Rules, ,Using Implicit Rules}.
953 @cindex variable definition
954 @item
955 A @dfn{variable definition} is a line that specifies a text string
956 value for a variable that can be substituted into the text later.  The
957 simple makefile example shows a variable definition for @code{objects}
958 as a list of all object files (@pxref{Variables Simplify, , Variables
959 Make Makefiles Simpler}).
961 @cindex directive
962 @item
963 A @dfn{directive} is a command for @code{make} to do something special while
964 reading the makefile.  These include:
966 @itemize @bullet
967 @item
968 Reading another makefile (@pxref{Include, ,Including Other Makefiles}).
970 @item
971 Deciding (based on the values of variables) whether to use or
972 ignore a part of the makefile (@pxref{Conditionals, ,Conditional Parts of Makefiles}).
974 @item
975 Defining a variable from a verbatim string containing multiple lines
976 (@pxref{Defining, ,Defining Variables Verbatim}).
977 @end itemize
979 @cindex comments, in makefile
980 @cindex @code{#} (comments), in makefile
981 @item
982 @samp{#} in a line of a makefile starts a @dfn{comment}.  It and the rest of
983 the line are ignored, except that a trailing backslash not escaped by
984 another backslash will continue the comment across multiple lines.
985 Comments may appear on any of the lines in the makefile, except within a
986 @code{define} directive, and perhaps within commands (where the shell
987 decides what is a comment).  A line containing just a comment (with
988 perhaps spaces before it) is effectively blank, and is ignored.@refill
989 @end itemize
991 @node Makefile Names, Include, Makefile Contents, Makefiles
992 @section What Name to Give Your Makefile
993 @cindex makefile name
994 @cindex name of makefile
995 @cindex default makefile name
996 @cindex file name of makefile
998 @c following paragraph rewritten to avoid overfull hbox
999 By default, when @code{make} looks for the makefile, it tries the
1000 following names, in order: @file{GNUmakefile}, @file{makefile}
1001 and @file{Makefile}.@refill
1002 @findex Makefile
1003 @findex GNUmakefile
1004 @findex makefile
1006 @cindex @code{README}
1007 Normally you should call your makefile either @file{makefile} or
1008 @file{Makefile}.  (We recommend @file{Makefile} because it appears
1009 prominently near the beginning of a directory listing, right near other
1010 important files such as @file{README}.)  The first name checked,
1011 @file{GNUmakefile}, is not recommended for most makefiles.  You should
1012 use this name if you have a makefile that is specific to GNU
1013 @code{make}, and will not be understood by other versions of
1014 @code{make}.  Other @code{make} programs look for @file{makefile} and
1015 @file{Makefile}, but not @file{GNUmakefile}.
1017 If @code{make} finds none of these names, it does not use any makefile.
1018 Then you must specify a goal with a command argument, and @code{make}
1019 will attempt to figure out how to remake it using only its built-in
1020 implicit rules.  @xref{Implicit Rules, ,Using Implicit Rules}.
1022 @cindex @code{-f}
1023 @cindex @code{--file}
1024 @cindex @code{--makefile}
1025 If you want to use a nonstandard name for your makefile, you can specify
1026 the makefile name with the @samp{-f} or @samp{--file} option.  The
1027 arguments @w{@samp{-f @var{name}}} or @w{@samp{--file=@var{name}}} tell
1028 @code{make} to read the file @var{name} as the makefile.  If you use
1029 more than one @samp{-f} or @samp{--file} option, you can specify several
1030 makefiles.  All the makefiles are effectively concatenated in the order
1031 specified.  The default makefile names @file{GNUmakefile},
1032 @file{makefile} and @file{Makefile} are not checked automatically if you
1033 specify @samp{-f} or @samp{--file}.@refill
1034 @cindex specifying makefile name
1035 @cindex makefile name, how to specify
1036 @cindex name of makefile, how to specify
1037 @cindex file name of makefile, how to specify
1039 @node Include, MAKEFILES Variable, Makefile Names, Makefiles
1040 @section Including Other Makefiles
1041 @cindex including other makefiles
1042 @cindex makefile, including
1044 @findex include
1045 The @code{include} directive tells @code{make} to suspend reading the
1046 current makefile and read one or more other makefiles before continuing.
1047 The directive is a line in the makefile that looks like this:
1049 @example
1050 include @var{filenames}@dots{}
1051 @end example
1053 @noindent
1054 @var{filenames} can contain shell file name patterns.
1055 @cindex shell file name pattern (in @code{include})
1056 @cindex shell wildcards (in @code{include})
1057 @cindex wildcard, in @code{include}
1059 Extra spaces are allowed and ignored at the beginning of the line, but
1060 a tab is not allowed.  (If the line begins with a tab, it will be
1061 considered a command line.)  Whitespace is required between
1062 @code{include} and the file names, and between file names; extra
1063 whitespace is ignored there and at the end of the directive.  A
1064 comment starting with @samp{#} is allowed at the end of the line.  If
1065 the file names contain any variable or function references, they are
1066 expanded.  @xref{Using Variables, ,How to Use Variables}.
1068 For example, if you have three @file{.mk} files, @file{a.mk},
1069 @file{b.mk}, and @file{c.mk}, and @code{$(bar)} expands to
1070 @code{bish bash}, then the following expression
1072 @example
1073 include foo *.mk $(bar)
1074 @end example
1076 is equivalent to
1078 @example
1079 include foo a.mk b.mk c.mk bish bash
1080 @end example
1082 When @code{make} processes an @code{include} directive, it suspends
1083 reading of the containing makefile and reads from each listed file in
1084 turn.  When that is finished, @code{make} resumes reading the
1085 makefile in which the directive appears.
1087 One occasion for using @code{include} directives is when several programs,
1088 handled by individual makefiles in various directories, need to use a
1089 common set of variable definitions
1090 (@pxref{Setting, ,Setting Variables}) or pattern rules
1091 (@pxref{Pattern Rules, ,Defining and Redefining Pattern Rules}).
1093 Another such occasion is when you want to generate dependencies from
1094 source files automatically; the dependencies can be put in a file that
1095 is included by the main makefile.  This practice is generally cleaner
1096 than that of somehow appending the dependencies to the end of the main
1097 makefile as has been traditionally done with other versions of
1098 @code{make}.  @xref{Automatic Dependencies}.
1099 @cindex dependencies, automatic generation
1100 @cindex automatic generation of dependencies
1101 @cindex generating dependencies automatically
1103 @cindex @code{-I}
1104 @cindex @code{--include-dir}
1105 @cindex included makefiles, default directries
1106 @cindex default directries for included makefiles
1107 @findex /usr/gnu/include
1108 @findex /usr/local/include
1109 @findex /usr/include
1110 If the specified name does not start with a slash, and the file is not
1111 found in the current directory, several other directories are searched.
1112 First, any directories you have specified with the @samp{-I} or
1113 @samp{--include-dir} option are searched
1114 (@pxref{Options Summary, ,Summary of Options}).
1115 Then the following directories (if they exist)
1116 are searched, in this order:
1117 @file{@var{prefix}/include} (normally @file{/usr/local/include}
1118 @footnote{GNU Make compiled for MS-DOS and MS-Windows behaves as if
1119 @var{prefix} has been defined to be the root of the DJGPP tree
1120 hierarchy.})
1121 @file{/usr/gnu/include},
1122 @file{/usr/local/include}, @file{/usr/include}.
1124 If an included makefile cannot be found in any of these directories, a
1125 warning message is generated, but it is not an immediately fatal error;
1126 processing of the makefile containing the @code{include} continues.
1127 Once it has finished reading makefiles, @code{make} will try to remake
1128 any that are out of date or don't exist.
1129 @xref{Remaking Makefiles, ,How Makefiles Are Remade}.
1130 Only after it has tried to find a way to remake a makefile and failed,
1131 will @code{make} diagnose the missing makefile as a fatal error.
1133 If you want @code{make} to simply ignore a makefile which does not exist
1134 and cannot be remade, with no error message, use the @w{@code{-include}}
1135 directive instead of @code{include}, like this:
1137 @example
1138 -include @var{filenames}@dots{}
1139 @end example
1141 This is acts like @code{include} in every way except that there is no
1142 error (not even a warning) if any of the @var{filenames} do not exist.
1143 For compatibility with some other @code{make} implementations,
1144 @code{sinclude} is another name for @w{@code{-include}}.
1146 @node MAKEFILES Variable, Remaking Makefiles, Include, Makefiles
1147 @section The Variable @code{MAKEFILES}
1148 @cindex makefile, and @code{MAKEFILES} variable
1149 @cindex including (@code{MAKEFILES} variable)
1151 @vindex MAKEFILES
1152 If the environment variable @code{MAKEFILES} is defined, @code{make}
1153 considers its value as a list of names (separated by whitespace) of
1154 additional makefiles to be read before the others.  This works much like
1155 the @code{include} directive: various directories are searched for those
1156 files (@pxref{Include, ,Including Other Makefiles}).  In addition, the
1157 default goal is never taken from one of these makefiles and it is not an
1158 error if the files listed in @code{MAKEFILES} are not found.@refill
1160 @cindex recursion, and @code{MAKEFILES} variable
1161 The main use of @code{MAKEFILES} is in communication between recursive
1162 invocations of @code{make} (@pxref{Recursion, ,Recursive Use of
1163 @code{make}}).  It usually is not desirable to set the environment
1164 variable before a top-level invocation of @code{make}, because it is
1165 usually better not to mess with a makefile from outside.  However, if
1166 you are running @code{make} without a specific makefile, a makefile in
1167 @code{MAKEFILES} can do useful things to help the built-in implicit
1168 rules work better, such as defining search paths (@pxref{Directory Search}).
1170 Some users are tempted to set @code{MAKEFILES} in the environment
1171 automatically on login, and program makefiles to expect this to be done.
1172 This is a very bad idea, because such makefiles will fail to work if run by
1173 anyone else.  It is much better to write explicit @code{include} directives
1174 in the makefiles.  @xref{Include, , Including Other Makefiles}.
1176 @node Remaking Makefiles, Overriding Makefiles, MAKEFILES Variable, Makefiles
1177 @section How Makefiles Are Remade
1179 @cindex updating makefiles
1180 @cindex remaking makefiles
1181 @cindex makefile, remaking of
1182 Sometimes makefiles can be remade from other files, such as RCS or SCCS
1183 files.  If a makefile can be remade from other files, you probably want
1184 @code{make} to get an up-to-date version of the makefile to read in.
1186 To this end, after reading in all makefiles, @code{make} will consider
1187 each as a goal target and attempt to update it.  If a makefile has a
1188 rule which says how to update it (found either in that very makefile or
1189 in another one) or if an implicit rule applies to it (@pxref{Implicit
1190 Rules, ,Using Implicit Rules}), it will be updated if necessary.  After
1191 all makefiles have been checked, if any have actually been changed,
1192 @code{make} starts with a clean slate and reads all the makefiles over
1193 again.  (It will also attempt to update each of them over again, but
1194 normally this will not change them again, since they are already up to
1195 date.)@refill
1197 If you know that one or more of your makefiles cannot be remade and you
1198 want to keep @code{make} from performing an implicit rule search on
1199 them, perhaps for efficiency reasons, you can use any normal method of
1200 preventing implicit rule lookup to do so.  For example, you can write an
1201 explicit rule with the makefile as the target, and an empty command
1202 string (@pxref{Empty Commands, ,Using Empty Commands}).
1204 If the makefiles specify a double-colon rule to remake a file with
1205 commands but no dependencies, that file will always be remade
1206 (@pxref{Double-Colon}).  In the case of makefiles, a makefile that has a
1207 double-colon rule with commands but no dependencies will be remade every
1208 time @code{make} is run, and then again after @code{make} starts over
1209 and reads the makefiles in again.  This would cause an infinite loop:
1210 @code{make} would constantly remake the makefile, and never do anything
1211 else.  So, to avoid this, @code{make} will @strong{not} attempt to
1212 remake makefiles which are specified as targets of a double-colon rule
1213 with commands but no dependencies.@refill
1215 If you do not specify any makefiles to be read with @samp{-f} or
1216 @samp{--file} options, @code{make} will try the default makefile names;
1217 @pxref{Makefile Names, ,What Name to Give Your Makefile}.  Unlike
1218 makefiles explicitly requested with @samp{-f} or @samp{--file} options,
1219 @code{make} is not certain that these makefiles should exist.  However,
1220 if a default makefile does not exist but can be created by running
1221 @code{make} rules, you probably want the rules to be run so that the
1222 makefile can be used.
1224 Therefore, if none of the default makefiles exists, @code{make} will try
1225 to make each of them in the same order in which they are searched for
1226 (@pxref{Makefile Names, ,What Name to Give Your Makefile})
1227 until it succeeds in making one, or it runs out of names to try.  Note
1228 that it is not an error if @code{make} cannot find or make any makefile;
1229 a makefile is not always necessary.@refill
1231 When you use the @samp{-t} or @samp{--touch} option
1232 (@pxref{Instead of Execution, ,Instead of Executing the Commands}),
1233 you would not want to use an out-of-date makefile to decide which
1234 targets to touch.  So the @samp{-t} option has no effect on updating
1235 makefiles; they are really updated even if @samp{-t} is specified.
1236 Likewise, @samp{-q} (or @samp{--question}) and @samp{-n} (or
1237 @samp{--just-print}) do not prevent updating of makefiles, because an
1238 out-of-date makefile would result in the wrong output for other targets.
1239 Thus, @samp{make -f mfile -n foo} will update @file{mfile}, read it in,
1240 and then print the commands to update @file{foo} and its dependencies
1241 without running them.  The commands printed for @file{foo} will be those
1242 specified in the updated contents of @file{mfile}.
1244 However, on occasion you might actually wish to prevent updating of even
1245 the makefiles.  You can do this by specifying the makefiles as goals in
1246 the command line as well as specifying them as makefiles.  When the
1247 makefile name is specified explicitly as a goal, the options @samp{-t}
1248 and so on do apply to them.
1250 Thus, @samp{make -f mfile -n mfile foo} would read the makefile
1251 @file{mfile}, print the commands needed to update it without actually
1252 running them, and then print the commands needed to update @file{foo}
1253 without running them.  The commands for @file{foo} will be those
1254 specified by the existing contents of @file{mfile}.
1256 @node Overriding Makefiles,  , Remaking Makefiles, Makefiles
1257 @section Overriding Part of Another Makefile
1259 @cindex overriding makefiles
1260 @cindex makefile, overriding
1261 Sometimes it is useful to have a makefile that is mostly just like
1262 another makefile.  You can often use the @samp{include} directive to
1263 include one in the other, and add more targets or variable definitions.
1264 However, if the two makefiles give different commands for the same
1265 target, @code{make} will not let you just do this.  But there is another way.
1267 @cindex match-anything rule, used to override
1268 In the containing makefile (the one that wants to include the other),
1269 you can use a match-anything pattern rule to say that to remake any
1270 target that cannot be made from the information in the containing
1271 makefile, @code{make} should look in another makefile.
1272 @xref{Pattern Rules}, for more information on pattern rules.
1274 For example, if you have a makefile called @file{Makefile} that says how
1275 to make the target @samp{foo} (and other targets), you can write a
1276 makefile called @file{GNUmakefile} that contains:
1278 @example
1279 foo:
1280         frobnicate > foo
1282 %: force
1283         @@$(MAKE) -f Makefile $@@
1284 force: ;
1285 @end example
1287 If you say @samp{make foo}, @code{make} will find @file{GNUmakefile},
1288 read it, and see that to make @file{foo}, it needs to run the command
1289 @samp{frobnicate > foo}.  If you say @samp{make bar}, @code{make} will
1290 find no way to make @file{bar} in @file{GNUmakefile}, so it will use the
1291 commands from the pattern rule: @samp{make -f Makefile bar}.  If
1292 @file{Makefile} provides a rule for updating @file{bar}, @code{make}
1293 will apply the rule.  And likewise for any other target that
1294 @file{GNUmakefile} does not say how to make.
1296 The way this works is that the pattern rule has a pattern of just
1297 @samp{%}, so it matches any target whatever.  The rule specifies a
1298 dependency @file{force}, to guarantee that the commands will be run even
1299 if the target file already exists.  We give @file{force} target empty
1300 commands to prevent @code{make} from searching for an implicit rule to
1301 build it---otherwise it would apply the same match-anything rule to
1302 @file{force} itself and create a dependency loop!
1304 @node Rules, Commands, Makefiles, Top
1305 @chapter Writing Rules
1306 @cindex writing rules
1307 @cindex rule, how to write
1308 @cindex target
1309 @cindex dependency
1311 A @dfn{rule} appears in the makefile and says when and how to remake
1312 certain files, called the rule's @dfn{targets} (most often only one per rule).
1313 It lists the other files that are the @dfn{dependencies} of the target, and
1314 @dfn{commands} to use to create or update the target.
1316 @cindex default goal
1317 @cindex goal, default
1318 The order of rules is not significant, except for determining the
1319 @dfn{default goal}: the target for @code{make} to consider, if you do
1320 not otherwise specify one.  The default goal is the target of the first
1321 rule in the first makefile.  If the first rule has multiple targets,
1322 only the first target is taken as the default.  There are two
1323 exceptions: a target starting with a period is not a default unless it
1324 contains one or more slashes, @samp{/}, as well; and, a target that
1325 defines a pattern rule has no effect on the default goal.
1326 (@xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.)
1328 Therefore, we usually write the makefile so that the first rule is the
1329 one for compiling the entire program or all the programs described by
1330 the makefile (often with a target called @samp{all}).
1331 @xref{Goals, ,Arguments to Specify the Goals}.
1333 @menu
1334 * Rule Example::                An example explained.
1335 * Rule Syntax::                 General syntax explained.
1336 * Wildcards::                   Using wildcard characters such as `*'.
1337 * Directory Search::            Searching other directories for source files.
1338 * Phony Targets::               Using a target that is not a real file's name.
1339 * Force Targets::               You can use a target without commands
1340                                   or dependencies to mark other
1341                                   targets as phony.
1342 * Empty Targets::               When only the date matters and the
1343                                   files are empty.
1344 * Special Targets::             Targets with special built-in meanings.
1345 * Multiple Targets::            When to make use of several targets in a rule.
1346 * Multiple Rules::              How to use several rules with the same target.
1347 * Static Pattern::              Static pattern rules apply to multiple targets
1348                                   and can vary the dependencies according to
1349                                   the target name.
1350 * Double-Colon::                How to use a special kind of rule to allow
1351                                   several independent rules for one target.
1352 * Automatic Dependencies::      How to automatically generate rules giving
1353                                  dependencies from the source files themselves.
1354 @end menu
1356 @ifinfo
1357 @node Rule Example, Rule Syntax,  , Rules
1358 @section Rule Example
1360 Here is an example of a rule:
1362 @example
1363 foo.o : foo.c defs.h       # module for twiddling the frobs
1364         cc -c -g foo.c
1365 @end example
1367 Its target is @file{foo.o} and its dependencies are @file{foo.c} and
1368 @file{defs.h}.  It has one command, which is @samp{cc -c -g foo.c}.
1369 The command line starts with a tab to identify it as a command.
1371 This rule says two things:
1373 @itemize @bullet
1374 @item
1375 How to decide whether @file{foo.o} is out of date: it is out of date
1376 if it does not exist, or if either @file{foo.c} or @file{defs.h} is
1377 more recent than it.
1379 @item
1380 How to update the file @file{foo.o}: by running @code{cc} as stated.
1381 The command does not explicitly mention @file{defs.h}, but we presume
1382 that @file{foo.c} includes it, and that that is why @file{defs.h} was
1383 added to the dependencies.
1384 @end itemize
1385 @end ifinfo
1387 @node Rule Syntax, Wildcards, Rule Example, Rules
1388 @section Rule Syntax
1390 @cindex rule syntax
1391 @cindex syntax of rules
1392 In general, a rule looks like this:
1394 @example
1395 @var{targets} : @var{dependencies}
1396         @var{command}
1397         @dots{}
1398 @end example
1400 @noindent
1401 or like this:
1403 @example
1404 @var{targets} : @var{dependencies} ; @var{command}
1405         @var{command}
1406         @dots{}
1407 @end example
1409 @cindex targets
1410 @cindex rule targets
1411 The @var{targets} are file names, separated by spaces.  Wildcard
1412 characters may be used (@pxref{Wildcards, ,Using Wildcard Characters
1413 in File Names}) and a name of the form @file{@var{a}(@var{m})}
1414 represents member @var{m} in archive file @var{a}
1415 (@pxref{Archive Members, ,Archive Members as Targets}).
1416 Usually there is only one
1417 target per rule, but occasionally there is a reason to have more
1418 (@pxref{Multiple Targets, , Multiple Targets in a Rule}).@refill
1420 @cindex commands
1421 @cindex tab character (in commands)
1422 The @var{command} lines start with a tab character.  The first command may
1423 appear on the line after the dependencies, with a tab character, or may
1424 appear on the same line, with a semicolon.  Either way, the effect is the
1425 same.  @xref{Commands, ,Writing the Commands in Rules}.
1427 @cindex dollar sign (@code{$}), in rules
1428 @cindex @code{$}, in rules
1429 @cindex rule, and @code{$}
1430 Because dollar signs are used to start variable references, if you really
1431 want a dollar sign in a rule you must write two of them, @samp{$$}
1432 (@pxref{Using Variables, ,How to Use Variables}).
1433 You may split a long line by inserting a backslash
1434 followed by a newline, but this is not required, as @code{make} places no
1435 limit on the length of a line in a makefile.
1437 A rule tells @code{make} two things: when the targets are out of date,
1438 and how to update them when necessary.
1440 @cindex dependencies
1441 @cindex rule dependencies
1442 The criterion for being out of date is specified in terms of the
1443 @var{dependencies}, which consist of file names separated by spaces.
1444 (Wildcards and archive members (@pxref{Archives}) are allowed here too.)
1445 A target is out of date if it does not exist or if it is older than any
1446 of the dependencies (by comparison of last-modification times).  The
1447 idea is that the contents of the target file are computed based on
1448 information in the dependencies, so if any of the dependencies changes,
1449 the contents of the existing target file are no longer necessarily
1450 valid.
1452 How to update is specified by @var{commands}.  These are lines to be
1453 executed by the shell (normally @samp{sh}), but with some extra features
1454 (@pxref{Commands, ,Writing the Commands in Rules}).
1456 @node Wildcards, Directory Search, Rule Syntax, Rules
1457 @section Using Wildcard Characters in File Names
1458 @cindex wildcard
1459 @cindex file name with wildcards
1460 @cindex globbing (wildcards)
1462 @cindex @code{*} (wildcard character)
1463 @cindex @code{?} (wildcard character)
1464 @cindex @code{[@dots{}]} (wildcard characters)
1465 A single file name can specify many files using @dfn{wildcard characters}.
1466 The wildcard characters in @code{make} are @samp{*}, @samp{?} and
1467 @samp{[@dots{}]}, the same as in the Bourne shell.  For example, @file{*.c}
1468 specifies a list of all the files (in the working directory) whose names
1469 end in @samp{.c}.@refill
1471 @cindex @code{~} (tilde)
1472 @cindex tilde (@code{~})
1473 @cindex home directory
1474 The character @samp{~} at the beginning of a file name also has special
1475 significance.  If alone, or followed by a slash, it represents your home
1476 directory.  For example @file{~/bin} expands to @file{/home/you/bin}.
1477 If the @samp{~} is followed by a word, the string represents the home
1478 directory of the user named by that word.  For example @file{~john/bin}
1479 expands to @file{/home/john/bin}.  On systems which don't have a home
1480 directory for each user (such as MS-DOS or MS-Windows), this
1481 functionality can be simulated by setting the environment variable
1482 @var{HOME}.@refill
1484 Wildcard expansion happens automatically in targets, in dependencies,
1485 and in commands (where the shell does the expansion).  In other
1486 contexts, wildcard expansion happens only if you request it explicitly
1487 with the @code{wildcard} function.
1489 The special significance of a wildcard character can be turned off by
1490 preceding it with a backslash.  Thus, @file{foo\*bar} would refer to a
1491 specific file whose name consists of @samp{foo}, an asterisk, and
1492 @samp{bar}.@refill
1494 @menu
1495 * Wildcard Examples::           Several examples
1496 * Wildcard Pitfall::            Problems to avoid.
1497 * Wildcard Function::           How to cause wildcard expansion where
1498                                   it does not normally take place.
1499 @end menu
1501 @node Wildcard Examples, Wildcard Pitfall,  , Wildcards
1502 @subsection Wildcard Examples
1504 Wildcards can be used in the commands of a rule, where they are expanded
1505 by the shell.  For example, here is a rule to delete all the object files:
1507 @example
1508 @group
1509 clean:
1510         rm -f *.o
1511 @end group
1512 @end example
1513 @cindex @code{rm} (shell command)
1515 Wildcards are also useful in the dependencies of a rule.  With the
1516 following rule in the makefile, @samp{make print} will print all the
1517 @samp{.c} files that have changed since the last time you printed them:
1519 @example
1520 print: *.c
1521         lpr -p $?
1522         touch print
1523 @end example
1525 @cindex @code{print} target
1526 @cindex @code{lpr} (shell command)
1527 @cindex @code{touch} (shell command)
1528 @noindent
1529 This rule uses @file{print} as an empty target file; see @ref{Empty
1530 Targets, ,Empty Target Files to Record Events}.  (The automatic variable
1531 @samp{$?} is used to print only those files that have changed; see
1532 @ref{Automatic, ,Automatic Variables}.)@refill
1534 Wildcard expansion does not happen when you define a variable.  Thus, if
1535 you write this:
1537 @example
1538 objects = *.o
1539 @end example
1541 @noindent
1542 then the value of the variable @code{objects} is the actual string
1543 @samp{*.o}.  However, if you use the value of @code{objects} in a target,
1544 dependency or command, wildcard expansion will take place at that time.
1545 To set @code{objects} to the expansion, instead use:
1547 @example
1548 objects := $(wildcard *.o)
1549 @end example
1551 @noindent
1552 @xref{Wildcard Function}.
1554 @node Wildcard Pitfall, Wildcard Function, Wildcard Examples, Wildcards
1555 @subsection Pitfalls of Using Wildcards
1556 @cindex wildcard pitfalls
1557 @cindex pitfalls of wildcards
1558 @cindex mistakes with wildcards
1559 @cindex errors with wildcards
1560 @cindex problems with wildcards
1562 Now here is an example of a naive way of using wildcard expansion, that
1563 does not do what you would intend.  Suppose you would like to say that the
1564 executable file @file{foo} is made from all the object files in the
1565 directory, and you write this:
1567 @example
1568 objects = *.o
1570 foo : $(objects)
1571         cc -o foo $(CFLAGS) $(objects)
1572 @end example
1574 @noindent
1575 The value of @code{objects} is the actual string @samp{*.o}.  Wildcard
1576 expansion happens in the rule for @file{foo}, so that each @emph{existing}
1577 @samp{.o} file becomes a dependency of @file{foo} and will be recompiled if
1578 necessary.
1580 But what if you delete all the @samp{.o} files?  When a wildcard matches
1581 no files, it is left as it is, so then @file{foo} will depend on the
1582 oddly-named file @file{*.o}.  Since no such file is likely to exist,
1583 @code{make} will give you an error saying it cannot figure out how to
1584 make @file{*.o}.  This is not what you want!
1586 Actually it is possible to obtain the desired result with wildcard
1587 expansion, but you need more sophisticated techniques, including the
1588 @code{wildcard} function and string substitution.
1589 @ifinfo
1590 @xref{Wildcard Function, ,The Function @code{wildcard}}.
1591 @end ifinfo
1592 @iftex
1593 These are described in the following section.
1594 @end iftex
1596 @cindex wildcards and MS-DOS/MS-Windows backslashes
1597 @cindex backslashes in pathnames and wildcard expansion
1599 Microsoft operating systems (MS-DOS and MS-Windows) use backslashes to
1600 separate directories in pathnames, like so:
1602 @example
1603   c:\foo\bar\baz.c
1604 @end example
1606 This is equivalent to the Unix-style @file{c:/foo/bar/baz.c} (the
1607 @file{c:} part is the so-called drive letter).  When @code{make} runs on
1608 these systems, it supports backslashes as well as the Unix-style forward
1609 slashes in pathnames.  However, this support does @emph{not} include the
1610 wildcard expansion, where backslash is a quote character.  Therefore,
1611 you @emph{must} use Unix-style slashes in these cases.
1614 @node Wildcard Function,  , Wildcard Pitfall, Wildcards
1615 @subsection The Function @code{wildcard}
1616 @findex wildcard
1618 Wildcard expansion happens automatically in rules.  But wildcard expansion
1619 does not normally take place when a variable is set, or inside the
1620 arguments of a function.  If you want to do wildcard expansion in such
1621 places, you need to use the @code{wildcard} function, like this:
1623 @example
1624 $(wildcard @var{pattern}@dots{})
1625 @end example
1627 @noindent
1628 This string, used anywhere in a makefile, is replaced by a
1629 space-separated list of names of existing files that match one of the
1630 given file name patterns.  If no existing file name matches a pattern,
1631 then that pattern is omitted from the output of the @code{wildcard}
1632 function.  Note that this is different from how unmatched wildcards
1633 behave in rules, where they are used verbatim rather than ignored
1634 (@pxref{Wildcard Pitfall}).
1636 One use of the @code{wildcard} function is to get a list of all the C source
1637 files in a directory, like this:
1639 @example
1640 $(wildcard *.c)
1641 @end example
1643 We can change the list of C source files into a list of object files by
1644 replacing the @samp{.c} suffix with @samp{.o} in the result, like this:
1646 @example
1647 $(patsubst %.c,%.o,$(wildcard *.c))
1648 @end example
1650 @noindent
1651 (Here we have used another function, @code{patsubst}.
1652 @xref{Text Functions, ,Functions for String Substitution and Analysis}.)@refill
1654 Thus, a makefile to compile all C source files in the directory and then
1655 link them together could be written as follows:
1657 @example
1658 objects := $(patsubst %.c,%.o,$(wildcard *.c))
1660 foo : $(objects)
1661         cc -o foo $(objects)
1662 @end example
1664 @noindent
1665 (This takes advantage of the implicit rule for compiling C programs, so
1666 there is no need to write explicit rules for compiling the files.
1667 @xref{Flavors, ,The Two Flavors of Variables}, for an explanation of
1668 @samp{:=}, which is a variant of @samp{=}.)
1670 @node Directory Search, Phony Targets, Wildcards, Rules
1671 @section Searching Directories for Dependencies
1672 @vindex VPATH
1673 @findex vpath
1674 @cindex vpath
1675 @cindex search path for dependencies (@code{VPATH})
1676 @cindex directory search (@code{VPATH})
1678 For large systems, it is often desirable to put sources in a separate
1679 directory from the binaries.  The @dfn{directory search} features of
1680 @code{make} facilitate this by searching several directories
1681 automatically to find a dependency.  When you redistribute the files
1682 among directories, you do not need to change the individual rules,
1683 just the search paths.
1685 @menu
1686 * General Search::              Specifying a search path that applies
1687                                   to every dependency.
1688 * Selective Search::            Specifying a search path
1689                                   for a specified class of names.
1690 * Search Algorithm::            When and how search paths are applied.
1691 * Commands/Search::             How to write shell commands that work together
1692                                   with search paths.
1693 * Implicit/Search::             How search paths affect implicit rules.
1694 * Libraries/Search::            Directory search for link libraries.
1695 @end menu
1697 @node General Search, Selective Search,  , Directory Search
1698 @subsection @code{VPATH}: Search Path for All Dependencies
1699 @vindex VPATH
1701 The value of the @code{make} variable @code{VPATH} specifies a list of
1702 directories that @code{make} should search.  Most often, the
1703 directories are expected to contain dependency files that are not in the
1704 current directory; however, @code{VPATH} specifies a search list that
1705 @code{make} applies for all files, including files which are targets of
1706 rules.
1708 Thus, if a file that is listed as a target or dependency does not exist
1709 in the current directory, @code{make} searches the directories listed in
1710 @code{VPATH} for a file with that name.  If a file is found in one of
1711 them, that file may become the dependency (see below).  Rules may then
1712 specify the names of files in the dependency list as if they all
1713 existed in the current directory.  @xref{Commands/Search, ,Writing Shell
1714 Commands with Directory Search}.
1716 In the @code{VPATH} variable, directory names are separated by colons or
1717 blanks.  The order in which directories are listed is the order followed
1718 by @code{make} in its search.  (On MS-DOS and MS-Windows, semi-colons
1719 are used as separators of directory names in @code{VPATH}, since the
1720 colon can be used in the pathname itself, after the drive letter.)
1722 For example,
1724 @example
1725 VPATH = src:../headers
1726 @end example
1728 @noindent
1729 specifies a path containing two directories, @file{src} and
1730 @file{../headers}, which @code{make} searches in that order.
1732 With this value of @code{VPATH}, the following rule,
1734 @example
1735 foo.o : foo.c
1736 @end example
1738 @noindent
1739 is interpreted as if it were written like this:
1741 @example
1742 foo.o : src/foo.c
1743 @end example
1745 @noindent
1746 assuming the file @file{foo.c} does not exist in the current directory but
1747 is found in the directory @file{src}.
1749 @node Selective Search, Search Algorithm, General Search, Directory Search
1750 @subsection The @code{vpath} Directive
1751 @findex vpath
1753 Similar to the @code{VPATH} variable, but more selective, is the
1754 @code{vpath} directive (note lower case), which allows you to specify a
1755 search path for a particular class of file names: those that match a
1756 particular pattern.  Thus you can supply certain search directories for
1757 one class of file names and other directories (or none) for other file
1758 names.
1760 There are three forms of the @code{vpath} directive:
1762 @table @code
1763 @item vpath @var{pattern} @var{directories}
1764 Specify the search path @var{directories} for file names that match
1765 @var{pattern}.
1767 The search path, @var{directories}, is a list of directories to be
1768 searched, separated by colons (semi-colons on MS-DOS and MS-Windows) or
1769 blanks, just like the search path used in the @code{VPATH} variable.
1771 @item vpath @var{pattern}
1772 Clear out the search path associated with @var{pattern}.
1774 @c Extra blank line makes sure this gets two lines.
1775 @item vpath
1777 Clear all search paths previously specified with @code{vpath} directives.
1778 @end table
1780 A @code{vpath} pattern is a string containing a @samp{%} character.  The
1781 string must match the file name of a dependency that is being searched
1782 for, the @samp{%} character matching any sequence of zero or more
1783 characters (as in pattern rules; @pxref{Pattern Rules, ,Defining and
1784 Redefining Pattern Rules}).  For example, @code{%.h} matches files that
1785 end in @code{.h}.  (If there is no @samp{%}, the pattern must match the
1786 dependency exactly, which is not useful very often.)
1788 @cindex @code{%}, quoting in @code{vpath}
1789 @cindex @code{%}, quoting with @code{\} (backslash)
1790 @cindex @code{\} (backslash), to quote @code{%}
1791 @cindex backslash (@code{\}), to quote @code{%}
1792 @cindex quoting @code{%}, in @code{vpath}
1793 @samp{%} characters in a @code{vpath} directive's pattern can be quoted
1794 with preceding backslashes (@samp{\}).  Backslashes that would otherwise
1795 quote @samp{%} characters can be quoted with more backslashes.
1796 Backslashes that quote @samp{%} characters or other backslashes are
1797 removed from the pattern before it is compared to file names.  Backslashes
1798 that are not in danger of quoting @samp{%} characters go unmolested.@refill
1800 When a dependency fails to exist in the current directory, if the
1801 @var{pattern} in a @code{vpath} directive matches the name of the
1802 dependency file, then the @var{directories} in that directive are searched
1803 just like (and before) the directories in the @code{VPATH} variable.
1805 For example,
1807 @example
1808 vpath %.h ../headers
1809 @end example
1811 @noindent
1812 tells @code{make} to look for any dependency whose name ends in @file{.h}
1813 in the directory @file{../headers} if the file is not found in the current
1814 directory.
1816 If several @code{vpath} patterns match the dependency file's name, then
1817 @code{make} processes each matching @code{vpath} directive one by one,
1818 searching all the directories mentioned in each directive.  @code{make}
1819 handles multiple @code{vpath} directives in the order in which they
1820 appear in the makefile; multiple directives with the same pattern are
1821 independent of each other.
1823 @need 750
1824 Thus,
1826 @example
1827 @group
1828 vpath %.c foo
1829 vpath %   blish
1830 vpath %.c bar
1831 @end group
1832 @end example
1834 @noindent
1835 will look for a file ending in @samp{.c} in @file{foo}, then
1836 @file{blish}, then @file{bar}, while
1838 @example
1839 @group
1840 vpath %.c foo:bar
1841 vpath %   blish
1842 @end group
1843 @end example
1845 @noindent
1846 will look for a file ending in @samp{.c} in @file{foo}, then
1847 @file{bar}, then @file{blish}.
1849 @node Search Algorithm, Commands/Search, Selective Search, Directory Search
1850 @subsection How Directory Searches are Performed
1851 @cindex algorithm for directory search
1852 @cindex directory search algorithm
1854 When a dependency is found through directory search, regardless of type
1855 (general or selective), the pathname located may not be the one that
1856 @code{make} actually provides you in the dependency list.  Sometimes
1857 the path discovered through directory search is thrown away.
1859 The algorithm @code{make} uses to decide whether to keep or abandon a
1860 path found via directory search is as follows:
1862 @enumerate
1863 @item
1864 If a target file does not exist at the path specified in the makefile,
1865 directory search is performed.
1867 @item
1868 If the directory search is successful, that path is kept and this file
1869 is tentatively stored as the target.
1871 @item
1872 All dependencies of this target are examined using this same method.
1874 @item
1875 After processing the dependencies, the target may or may not need to be
1876 rebuilt:
1878 @enumerate a
1879 @item
1880 If the target does @emph{not} need to be rebuilt, the path to the file
1881 found during directory search is used for any dependency lists which
1882 contain this target.  In short, if @code{make} doesn't need to rebuild
1883 the target then you use the path found via directory search.
1885 @item
1886 If the target @emph{does} need to be rebuilt (is out-of-date), the
1887 pathname found during directory search is @emph{thrown away}, and the
1888 target is rebuilt using the file name specified in the makefile.  In
1889 short, if @code{make} must rebuild, then the target is rebuilt locally,
1890 not in the directory found via directory search.
1891 @end enumerate
1892 @end enumerate
1894 This algorithm may seem complex, but in practice it is quite often
1895 exactly what you want.
1897 @cindex traditional directory search
1898 @cindex directory search, traditional
1899 Other versions of @code{make} use a simpler algorithm: if the file does
1900 not exist, and it is found via directory search, then that pathname is
1901 always used whether or not the target needs to be built.  Thus, if the
1902 target is rebuilt it is created at the pathname discovered during
1903 directory search.
1905 @vindex GPATH
1906 If, in fact, this is the behavior you want for some or all of your
1907 directories, you can use the @code{GPATH} variable to indicate this to
1908 @code{make}.
1910 @code{GPATH} has the same syntax and format as @code{VPATH} (that is, a
1911 space- or colon-delimited list of pathnames).  If an out-of-date target
1912 is found by directory search in a directory that also appears in
1913 @code{GPATH}, then that pathname is not thrown away.  The target is
1914 rebuilt using the expanded path.
1916 @node Commands/Search, Implicit/Search, Search Algorithm, Directory Search
1917 @subsection Writing Shell Commands with Directory Search
1918 @cindex shell command, and directory search
1919 @cindex directory search (@code{VPATH}), and shell commands
1921 When a dependency is found in another directory through directory search,
1922 this cannot change the commands of the rule; they will execute as written.
1923 Therefore, you must write the commands with care so that they will look for
1924 the dependency in the directory where @code{make} finds it.
1926 This is done with the @dfn{automatic variables} such as @samp{$^}
1927 (@pxref{Automatic, ,Automatic Variables}).
1928 For instance, the value of @samp{$^} is a
1929 list of all the dependencies of the rule, including the names of
1930 the directories in which they were found, and the value of
1931 @samp{$@@} is the target.  Thus:@refill
1933 @example
1934 foo.o : foo.c
1935         cc -c $(CFLAGS) $^ -o $@@
1936 @end example
1938 @noindent
1939 (The variable @code{CFLAGS} exists so you can specify flags for C
1940 compilation by implicit rules; we use it here for consistency so it will
1941 affect all C compilations uniformly;
1942 @pxref{Implicit Variables, ,Variables Used by Implicit Rules}.)
1944 Often the dependencies include header files as well, which you do not
1945 want to mention in the commands.  The automatic variable @samp{$<} is
1946 just the first dependency:
1948 @example
1949 VPATH = src:../headers
1950 foo.o : foo.c defs.h hack.h
1951         cc -c $(CFLAGS) $< -o $@@
1952 @end example
1954 @node Implicit/Search, Libraries/Search, Commands/Search, Directory Search
1955 @subsection Directory Search and Implicit Rules
1956 @cindex @code{VPATH}, and implicit rules
1957 @cindex directory search (@code{VPATH}), and implicit rules
1958 @cindex search path for dependencies (@code{VPATH}), and implicit rules
1959 @cindex implicit rule, and directory search
1960 @cindex implicit rule, and @code{VPATH}
1961 @cindex rule, implicit, and directory search
1962 @cindex rule, implicit, and @code{VPATH}
1964 The search through the directories specified in @code{VPATH} or with
1965 @code{vpath} also happens during consideration of implicit rules
1966 (@pxref{Implicit Rules, ,Using Implicit Rules}).
1968 For example, when a file @file{foo.o} has no explicit rule, @code{make}
1969 considers implicit rules, such as the built-in rule to compile
1970 @file{foo.c} if that file exists.  If such a file is lacking in the
1971 current directory, the appropriate directories are searched for it.  If
1972 @file{foo.c} exists (or is mentioned in the makefile) in any of the
1973 directories, the implicit rule for C compilation is applied.
1975 The commands of implicit rules normally use automatic variables as a
1976 matter of necessity; consequently they will use the file names found by
1977 directory search with no extra effort.
1979 @node Libraries/Search,  , Implicit/Search, Directory Search
1980 @subsection Directory Search for Link Libraries
1981 @cindex link libraries, and directory search
1982 @cindex libraries for linking, directory search
1983 @cindex directory search (@code{VPATH}), and link libraries
1984 @cindex @code{VPATH}, and link libraries
1985 @cindex search path for dependencies (@code{VPATH}), and link libraries
1986 @cindex @code{-l} (library search)
1987 @cindex link libraries, patterns matching
1988 @cindex @code{.LIBPATTERNS}, and link libraries
1989 @vindex .LIBPATTERNS
1991 Directory search applies in a special way to libraries used with the
1992 linker.  This special feature comes into play when you write a dependency
1993 whose name is of the form @samp{-l@var{name}}.  (You can tell something
1994 strange is going on here because the dependency is normally the name of a
1995 file, and the @emph{file name} of a library generally looks like
1996 @file{lib@var{name}.a}, not like @samp{-l@var{name}}.)@refill
1998 When a dependency's name has the form @samp{-l@var{name}}, @code{make}
1999 handles it specially by searching for the file @file{lib@var{name}.so} in
2000 the current directory, in directories specified by matching @code{vpath}
2001 search paths and the @code{VPATH} search path, and then in the
2002 directories @file{/lib}, @file{/usr/lib}, and @file{@var{prefix}/lib}
2003 (normally @file{/usr/local/lib}, but MS-DOS/MS-Windows versions of
2004 @code{make} behave as if @var{prefix} is defined to be the root of the
2005 DJGPP installation tree).
2007 If that file is not found, then the file @file{lib@var{name}.a} is
2008 searched for, in the same directories as above.
2010 For example, if there is a @file{/usr/lib/libcurses.a} library on your
2011 system (and no @file{/usr/lib/libcurses.so} file), then
2013 @example
2014 @group
2015 foo : foo.c -lcurses
2016         cc $^ -o $@@
2017 @end group
2018 @end example
2020 @noindent
2021 would cause the command @samp{cc foo.c /usr/lib/libcurses.a -o foo} to
2022 be executed when @file{foo} is older than @file{foo.c} or than
2023 @file{/usr/lib/libcurses.a}.@refill
2025 Although the default set of files to be searched for is
2026 @file{lib@var{name}.so} and @file{lib@var{name}.a}, this is customizable
2027 via the @code{.LIBPATTERNS} variable.  Each word in the value of this
2028 variable is a pattern string.  When a dependency like
2029 @samp{-l@var{name}} is seen, @code{make} will replace the percent in
2030 each pattern in the list with @var{name} and perform the above directory
2031 searches using that library filename.  If no library is found, the next
2032 word in the list will be used.
2034 The default value for @code{.LIBPATTERNS} is ``@samp{lib%.so lib%.a}'',
2035 which provides the default behavior described above.
2037 You can turn off link library expansion completely by setting this
2038 variable to an empty value.
2040 @node Phony Targets, Force Targets, Directory Search, Rules
2041 @section Phony Targets
2042 @cindex phony targets
2043 @cindex targets, phony
2044 @cindex targets without a file
2046 A phony target is one that is not really the name of a file.  It is just a
2047 name for some commands to be executed when you make an explicit request.
2048 There are two reasons to use a phony target: to avoid a conflict with
2049 a file of the same name, and to improve performance.
2051 If you write a rule whose commands will not create the target file, the
2052 commands will be executed every time the target comes up for remaking.
2053 Here is an example:
2055 @example
2056 @group
2057 clean:
2058         rm *.o temp
2059 @end group
2060 @end example
2062 @noindent
2063 Because the @code{rm} command does not create a file named @file{clean},
2064 probably no such file will ever exist.  Therefore, the @code{rm} command
2065 will be executed every time you say @samp{make clean}.
2066 @cindex @code{rm} (shell command)
2068 @findex .PHONY
2069 The phony target will cease to work if anything ever does create a file
2070 named @file{clean} in this directory.  Since it has no dependencies, the
2071 file @file{clean} would inevitably be considered up to date, and its
2072 commands would not be executed.  To avoid this problem, you can explicitly
2073 declare the target to be phony, using the special target @code{.PHONY}
2074 (@pxref{Special Targets, ,Special Built-in Target Names}) as follows:
2076 @example
2077 .PHONY : clean
2078 @end example
2080 @noindent
2081 Once this is done, @samp{make clean} will run the commands regardless of
2082 whether there is a file named @file{clean}.
2084 Since it knows that phony targets do not name actual files that could be
2085 remade from other files, @code{make} skips the implicit rule search for
2086 phony targets (@pxref{Implicit Rules}).  This is why declaring a target
2087 phony is good for performance, even if you are not worried about the
2088 actual file existing.
2090 Thus, you first write the line that states that @code{clean} is a
2091 phony target, then you write the rule, like this:
2093 @example
2094 @group
2095 .PHONY: clean
2096 clean:
2097         rm *.o temp
2098 @end group
2099 @end example
2101 A phony target should not be a dependency of a real target file; if it
2102 is, its commands are run every time @code{make} goes to update that
2103 file.  As long as a phony target is never a dependency of a real
2104 target, the phony target commands will be executed only when the phony
2105 target is a specified goal (@pxref{Goals, ,Arguments to Specify the
2106 Goals}).
2108 Phony targets can have dependencies.  When one directory contains multiple
2109 programs, it is most convenient to describe all of the programs in one
2110 makefile @file{./Makefile}.  Since the target remade by default will be the
2111 first one in the makefile, it is common to make this a phony target named
2112 @samp{all} and give it, as dependencies, all the individual programs.  For
2113 example:
2115 @example
2116 all : prog1 prog2 prog3
2117 .PHONY : all
2119 prog1 : prog1.o utils.o
2120         cc -o prog1 prog1.o utils.o
2122 prog2 : prog2.o
2123         cc -o prog2 prog2.o
2125 prog3 : prog3.o sort.o utils.o
2126         cc -o prog3 prog3.o sort.o utils.o
2127 @end example
2129 @noindent
2130 Now you can say just @samp{make} to remake all three programs, or specify
2131 as arguments the ones to remake (as in @samp{make prog1 prog3}).
2133 When one phony target is a dependency of another, it serves as a subroutine
2134 of the other.  For example, here @samp{make cleanall} will delete the
2135 object files, the difference files, and the file @file{program}:
2137 @example
2138 .PHONY: cleanall cleanobj cleandiff
2140 cleanall : cleanobj cleandiff
2141         rm program
2143 cleanobj :
2144         rm *.o
2146 cleandiff :
2147         rm *.diff
2148 @end example
2150 @node Force Targets, Empty Targets, Phony Targets, Rules
2151 @section Rules without Commands or Dependencies
2152 @cindex force targets
2153 @cindex targets, force
2154 @cindex @code{FORCE}
2155 @cindex rule, no commands or dependencies
2157 If a rule has no dependencies or commands, and the target of the rule
2158 is a nonexistent file, then @code{make} imagines this target to have
2159 been updated whenever its rule is run.  This implies that all targets
2160 depending on this one will always have their commands run.
2162 An example will illustrate this:
2164 @example
2165 @group
2166 clean: FORCE
2167         rm $(objects)
2168 FORCE:
2169 @end group
2170 @end example
2172 Here the target @samp{FORCE} satisfies the special conditions, so the
2173 target @file{clean} that depends on it is forced to run its commands.
2174 There is nothing special about the name @samp{FORCE}, but that is one name
2175 commonly used this way.
2177 As you can see, using @samp{FORCE} this way has the same results as using
2178 @samp{.PHONY: clean}.
2180 Using @samp{.PHONY} is more explicit and more efficient.  However,
2181 other versions of @code{make} do not support @samp{.PHONY}; thus
2182 @samp{FORCE} appears in many makefiles.  @xref{Phony Targets}.
2184 @node Empty Targets, Special Targets, Force Targets, Rules
2185 @section Empty Target Files to Record Events
2186 @cindex empty targets
2187 @cindex targets, empty
2188 @cindex recording events with empty targets
2190 The @dfn{empty target} is a variant of the phony target; it is used to hold
2191 commands for an action that you request explicitly from time to time.
2192 Unlike a phony target, this target file can really exist; but the file's
2193 contents do not matter, and usually are empty.
2195 The purpose of the empty target file is to record, with its
2196 last-modification time, when the rule's commands were last executed.  It
2197 does so because one of the commands is a @code{touch} command to update the
2198 target file.
2200 The empty target file should have some dependencies (otherwise it
2201 doesn't make sense).  When you ask to remake the empty target, the
2202 commands are executed if any dependency is more recent than the target;
2203 in other words, if a dependency has changed since the last time you
2204 remade the target.  Here is an example:
2206 @example
2207 print: foo.c bar.c
2208         lpr -p $?
2209         touch print
2210 @end example
2211 @cindex @code{print} target
2212 @cindex @code{lpr} (shell command)
2213 @cindex @code{touch} (shell command)
2215 @noindent
2216 With this rule, @samp{make print} will execute the @code{lpr} command if
2217 either source file has changed since the last @samp{make print}.  The
2218 automatic variable @samp{$?} is used to print only those files that have
2219 changed (@pxref{Automatic, ,Automatic Variables}).
2221 @node Special Targets, Multiple Targets, Empty Targets, Rules
2222 @section Special Built-in Target Names
2223 @cindex special targets
2224 @cindex built-in special targets
2225 @cindex targets, built-in special
2227 Certain names have special meanings if they appear as targets.
2229 @table @code
2230 @findex .PHONY
2231 @item .PHONY
2233 The dependencies of the special target @code{.PHONY} are considered to
2234 be phony targets.  When it is time to consider such a target,
2235 @code{make} will run its commands unconditionally, regardless of
2236 whether a file with that name exists or what its last-modification
2237 time is.  @xref{Phony Targets, ,Phony Targets}.
2239 @findex .SUFFIXES
2240 @item .SUFFIXES
2242 The dependencies of the special target @code{.SUFFIXES} are the list
2243 of suffixes to be used in checking for suffix rules.
2244 @xref{Suffix Rules, , Old-Fashioned Suffix Rules}.
2246 @findex .DEFAULT
2247 @item .DEFAULT
2249 The commands specified for @code{.DEFAULT} are used for any target for
2250 which no rules are found (either explicit rules or implicit rules).
2251 @xref{Last Resort}.  If @code{.DEFAULT} commands are specified, every
2252 file mentioned as a dependency, but not as a target in a rule, will have
2253 these commands executed on its behalf.  @xref{Implicit Rule Search,
2254 ,Implicit Rule Search Algorithm}.
2256 @findex .PRECIOUS
2257 @item .PRECIOUS
2258 @cindex precious targets
2259 @cindex preserving with @code{.PRECIOUS}
2261 The targets which @code{.PRECIOUS} depends on are given the following
2262 special treatment: if @code{make} is killed or interrupted during the
2263 execution of their commands, the target is not deleted.
2264 @xref{Interrupts, ,Interrupting or Killing @code{make}}.
2265 Also, if the target is an intermediate file, it will not be deleted
2266 after it is no longer needed, as is normally done.
2267 @xref{Chained Rules, ,Chains of Implicit Rules}.
2269 You can also list the target pattern of an implicit rule (such as
2270 @samp{%.o}) as a dependency file of the special target @code{.PRECIOUS}
2271 to preserve intermediate files created by rules whose target patterns
2272 match that file's name.
2274 @findex .INTERMEDIATE
2275 @item .INTERMEDIATE
2276 @cindex intermediate targets, explicit
2278 The targets which @code{.INTERMEDIATE} depends on are treated as
2279 intermediate files.  @xref{Chained Rules, ,Chains of Implicit Rules}.
2280 @code{.INTERMEDIATE} with no dependencies has no effect.
2282 @findex .SECONDARY
2283 @item .SECONDARY
2284 @cindex secondary targets
2285 @cindex preserving with @code{.SECONDARY}
2287 The targets which @code{.SECONDARY} depends on are treated as
2288 intermediate files, except that they are never automatically deleted.
2289 @xref{Chained Rules, ,Chains of Implicit Rules}.
2291 @code{.SECONDARY} with no dependencies marks all file targets mentioned
2292 in the makefile as secondary.
2294 @findex .DELETE_ON_ERROR
2295 @item .DELETE_ON_ERROR
2296 @cindex removing targets on failure
2298 If @code{.DELETE_ON_ERROR} is mentioned as a target anywhere in the
2299 makefile, then @code{make} will delete the target of a rule if it has
2300 changed and its commands exit with a nonzero exit status, just as it
2301 does when it receives a signal.  @xref{Errors, ,Errors in Commands}.
2303 @findex .IGNORE
2304 @item .IGNORE
2306 If you specify dependencies for @code{.IGNORE}, then @code{make} will
2307 ignore errors in execution of the commands run for those particular
2308 files.  The commands for @code{.IGNORE} are not meaningful.
2310 If mentioned as a target with no dependencies, @code{.IGNORE} says to
2311 ignore errors in execution of commands for all files.  This usage of
2312 @samp{.IGNORE} is supported only for historical compatibility.  Since
2313 this affects every command in the makefile, it is not very useful; we
2314 recommend you use the more selective ways to ignore errors in specific
2315 commands.  @xref{Errors, ,Errors in Commands}.
2317 @findex .SILENT
2318 @item .SILENT
2320 If you specify dependencies for @code{.SILENT}, then @code{make} will
2321 not print the commands to remake those particular files before executing
2322 them.  The commands for @code{.SILENT} are not meaningful.
2324 If mentioned as a target with no dependencies, @code{.SILENT} says not
2325 to print any commands before executing them.  This usage of
2326 @samp{.SILENT} is supported only for historical compatibility.  We
2327 recommend you use the more selective ways to silence specific commands.
2328 @xref{Echoing, ,Command Echoing}.  If you want to silence all commands
2329 for a particular run of @code{make}, use the @samp{-s} or
2330 @w{@samp{--silent}} option (@pxref{Options Summary}).
2332 @findex .EXPORT_ALL_VARIABLES
2333 @item .EXPORT_ALL_VARIABLES
2335 Simply by being mentioned as a target, this tells @code{make} to
2336 export all variables to child processes by default.
2337 @xref{Variables/Recursion, ,Communicating Variables to a
2338 Sub-@code{make}}.
2339 @end table
2341 Any defined implicit rule suffix also counts as a special target if it
2342 appears as a target, and so does the concatenation of two suffixes, such
2343 as @samp{.c.o}.  These targets are suffix rules, an obsolete way of
2344 defining implicit rules (but a way still widely used).  In principle, any
2345 target name could be special in this way if you break it in two and add
2346 both pieces to the suffix list.  In practice, suffixes normally begin with
2347 @samp{.}, so these special target names also begin with @samp{.}.
2348 @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
2350 @node Multiple Targets, Multiple Rules, Special Targets, Rules
2351 @section Multiple Targets in a Rule
2352 @cindex multiple targets
2353 @cindex several targets in a rule
2354 @cindex targets, multiple
2355 @cindex rule, with multiple targets
2357 A rule with multiple targets is equivalent to writing many rules, each with
2358 one target, and all identical aside from that.  The same commands apply to
2359 all the targets, but their effects may vary because you can substitute the
2360 actual target name into the command using @samp{$@@}.  The rule contributes
2361 the same dependencies to all the targets also.
2363 This is useful in two cases.
2365 @itemize @bullet
2366 @item
2367 You want just dependencies, no commands.  For example:
2369 @example
2370 kbd.o command.o files.o: command.h
2371 @end example
2373 @noindent
2374 gives an additional dependency to each of the three object files
2375 mentioned.
2377 @item
2378 Similar commands work for all the targets.  The commands do not need
2379 to be absolutely identical, since the automatic variable @samp{$@@}
2380 can be used to substitute the particular target to be remade into the
2381 commands (@pxref{Automatic, ,Automatic Variables}).  For example:
2383 @example
2384 @group
2385 bigoutput littleoutput : text.g
2386         generate text.g -$(subst output,,$@@) > $@@
2387 @end group
2388 @end example
2389 @findex subst
2391 @noindent
2392 is equivalent to
2394 @example
2395 bigoutput : text.g
2396         generate text.g -big > bigoutput
2397 littleoutput : text.g
2398         generate text.g -little > littleoutput
2399 @end example
2401 @noindent
2402 Here we assume the hypothetical program @code{generate} makes two
2403 types of output, one if given @samp{-big} and one if given
2404 @samp{-little}.
2405 @xref{Text Functions, ,Functions for String Substitution and Analysis},
2406 for an explanation of the @code{subst} function.
2407 @end itemize
2409 Suppose you would like to vary the dependencies according to the target,
2410 much as the variable @samp{$@@} allows you to vary the commands.
2411 You cannot do this with multiple targets in an ordinary rule, but you can
2412 do it with a @dfn{static pattern rule}.
2413 @xref{Static Pattern, ,Static Pattern Rules}.
2415 @node Multiple Rules, Static Pattern, Multiple Targets, Rules
2416 @section Multiple Rules for One Target
2417 @cindex multiple rules for one target
2418 @cindex several rules for one target
2419 @cindex rule, multiple for one target
2420 @cindex target, multiple rules for one
2422 One file can be the target of several rules.  All the dependencies
2423 mentioned in all the rules are merged into one list of dependencies for
2424 the target.  If the target is older than any dependency from any rule,
2425 the commands are executed.
2427 There can only be one set of commands to be executed for a file.
2428 If more than one rule gives commands for the same file,
2429 @code{make} uses the last set given and prints an error message.
2430 (As a special case, if the file's name begins with a dot, no
2431 error message is printed.  This odd behavior is only for
2432 compatibility with other implementations of @code{make}.)
2433 There is no reason to
2434 write your makefiles this way; that is why @code{make} gives you
2435 an error message.@refill
2437 An extra rule with just dependencies can be used to give a few extra
2438 dependencies to many files at once.  For example, one usually has a
2439 variable named @code{objects} containing a list of all the compiler output
2440 files in the system being made.  An easy way to say that all of them must
2441 be recompiled if @file{config.h} changes is to write the following:
2443 @example
2444 objects = foo.o bar.o
2445 foo.o : defs.h
2446 bar.o : defs.h test.h
2447 $(objects) : config.h
2448 @end example
2450 This could be inserted or taken out without changing the rules that really
2451 specify how to make the object files, making it a convenient form to use if
2452 you wish to add the additional dependency intermittently.
2454 Another wrinkle is that the additional dependencies could be specified with
2455 a variable that you set with a command argument to @code{make}
2456 (@pxref{Overriding, ,Overriding Variables}).  For example,
2458 @example
2459 @group
2460 extradeps=
2461 $(objects) : $(extradeps)
2462 @end group
2463 @end example
2465 @noindent
2466 means that the command @samp{make extradeps=foo.h} will consider
2467 @file{foo.h} as a dependency of each object file, but plain @samp{make}
2468 will not.
2470 If none of the explicit rules for a target has commands, then @code{make}
2471 searches for an applicable implicit rule to find some commands
2472 @pxref{Implicit Rules, ,Using Implicit Rules}).
2474 @node Static Pattern, Double-Colon, Multiple Rules, Rules
2475 @section Static Pattern Rules
2476 @cindex static pattern rule
2477 @cindex rule, static pattern
2478 @cindex pattern rules, static (not implicit)
2479 @cindex varying dependencies
2480 @cindex dependencies, varying (static pattern)
2482 @dfn{Static pattern rules} are rules which specify multiple targets and
2483 construct the dependency names for each target based on the target name.
2484 They are more general than ordinary rules with multiple targets because the
2485 targets do not have to have identical dependencies.  Their dependencies must
2486 be @emph{analogous}, but not necessarily @emph{identical}.
2488 @menu
2489 * Static Usage::                The syntax of static pattern rules.
2490 * Static versus Implicit::      When are they better than implicit rules?
2491 @end menu
2493 @node Static Usage, Static versus Implicit,  , Static Pattern
2494 @subsection Syntax of Static Pattern Rules
2495 @cindex static pattern rule, syntax of
2496 @cindex pattern rules, static, syntax of
2498 Here is the syntax of a static pattern rule:
2500 @example
2501 @var{targets} @dots{}: @var{target-pattern}: @var{dep-patterns} @dots{}
2502         @var{commands}
2503         @dots{}
2504 @end example
2506 @noindent
2507 The @var{targets} list specifies the targets that the rule applies to.
2508 The targets can contain wildcard characters, just like the targets of
2509 ordinary rules (@pxref{Wildcards, ,Using Wildcard Characters in File
2510 Names}).
2512 @cindex target pattern, static (not implicit)
2513 @cindex stem
2514 The @var{target-pattern} and @var{dep-patterns} say how to compute the
2515 dependencies of each target.  Each target is matched against the
2516 @var{target-pattern} to extract a part of the target name, called the
2517 @dfn{stem}.  This stem is substituted into each of the @var{dep-patterns}
2518 to make the dependency names (one from each @var{dep-pattern}).
2520 Each pattern normally contains the character @samp{%} just once.  When the
2521 @var{target-pattern} matches a target, the @samp{%} can match any part of
2522 the target name; this part is called the @dfn{stem}.  The rest of the
2523 pattern must match exactly.  For example, the target @file{foo.o} matches
2524 the pattern @samp{%.o}, with @samp{foo} as the stem.  The targets
2525 @file{foo.c} and @file{foo.out} do not match that pattern.@refill
2527 @cindex dependency pattern, static (not implicit)
2528 The dependency names for each target are made by substituting the stem
2529 for the @samp{%} in each dependency pattern.  For example, if one
2530 dependency pattern is @file{%.c}, then substitution of the stem
2531 @samp{foo} gives the dependency name @file{foo.c}.  It is legitimate
2532 to write a dependency pattern that does not contain @samp{%}; then this
2533 dependency is the same for all targets.
2535 @cindex @code{%}, quoting in static pattern
2536 @cindex @code{%}, quoting with @code{\} (backslash)
2537 @cindex @code{\} (backslash), to quote @code{%}
2538 @cindex backslash (@code{\}), to quote @code{%}
2539 @cindex quoting @code{%}, in static pattern
2540 @samp{%} characters in pattern rules can be quoted with preceding
2541 backslashes (@samp{\}).  Backslashes that would otherwise quote @samp{%}
2542 characters can be quoted with more backslashes.  Backslashes that quote
2543 @samp{%} characters or other backslashes are removed from the pattern
2544 before it is compared to file names or has a stem substituted into it.
2545 Backslashes that are not in danger of quoting @samp{%} characters go
2546 unmolested.  For example, the pattern @file{the\%weird\\%pattern\\} has
2547 @samp{the%weird\} preceding the operative @samp{%} character, and
2548 @samp{pattern\\} following it.  The final two backslashes are left alone
2549 because they cannot affect any @samp{%} character.@refill
2551 Here is an example, which compiles each of @file{foo.o} and @file{bar.o}
2552 from the corresponding @file{.c} file:
2554 @example
2555 @group
2556 objects = foo.o bar.o
2558 all: $(objects)
2560 $(objects): %.o: %.c
2561         $(CC) -c $(CFLAGS) $< -o $@@
2562 @end group
2563 @end example
2565 @noindent
2566 Here @samp{$<} is the automatic variable that holds the name of the
2567 dependency and @samp{$@@} is the automatic variable that holds the name
2568 of the target; see @ref{Automatic, , Automatic Variables}.
2570 Each target specified must match the target pattern; a warning is issued
2571 for each target that does not.  If you have a list of files, only some of
2572 which will match the pattern, you can use the @code{filter} function to
2573 remove nonmatching file names (@pxref{Text Functions, ,Functions for String Substitution and Analysis}):
2575 @example
2576 files = foo.elc bar.o lose.o
2578 $(filter %.o,$(files)): %.o: %.c
2579         $(CC) -c $(CFLAGS) $< -o $@@
2580 $(filter %.elc,$(files)): %.elc: %.el
2581         emacs -f batch-byte-compile $<
2582 @end example
2584 @noindent
2585 In this example the result of @samp{$(filter %.o,$(files))} is
2586 @file{bar.o lose.o}, and the first static pattern rule causes each of
2587 these object files to be updated by compiling the corresponding C source
2588 file.  The result of @w{@samp{$(filter %.elc,$(files))}} is
2589 @file{foo.elc}, so that file is made from @file{foo.el}.@refill
2591 Another example shows how to use @code{$*} in static pattern rules:
2592 @vindex $*@r{, and static pattern}
2594 @example
2595 @group
2596 bigoutput littleoutput : %output : text.g
2597         generate text.g -$* > $@@
2598 @end group
2599 @end example
2601 @noindent
2602 When the @code{generate} command is run, @code{$*} will expand to the
2603 stem, either @samp{big} or @samp{little}.
2605 @node Static versus Implicit,  , Static Usage, Static Pattern
2606 @subsection Static Pattern Rules versus Implicit Rules
2607 @cindex rule, static pattern versus implicit
2608 @cindex static pattern rule, versus implicit
2610 A static pattern rule has much in common with an implicit rule defined as a
2611 pattern rule (@pxref{Pattern Rules, ,Defining and Redefining Pattern Rules}).
2612 Both have a pattern for the target and patterns for constructing the
2613 names of dependencies.  The difference is in how @code{make} decides
2614 @emph{when} the rule applies.
2616 An implicit rule @emph{can} apply to any target that matches its pattern,
2617 but it @emph{does} apply only when the target has no commands otherwise
2618 specified, and only when the dependencies can be found.  If more than one
2619 implicit rule appears applicable, only one applies; the choice depends on
2620 the order of rules.
2622 By contrast, a static pattern rule applies to the precise list of targets
2623 that you specify in the rule.  It cannot apply to any other target and it
2624 invariably does apply to each of the targets specified.  If two conflicting
2625 rules apply, and both have commands, that's an error.
2627 The static pattern rule can be better than an implicit rule for these
2628 reasons:
2630 @itemize @bullet
2631 @item
2632 You may wish to override the usual implicit rule for a few
2633 files whose names cannot be categorized syntactically but
2634 can be given in an explicit list.
2636 @item
2637 If you cannot be sure of the precise contents of the directories
2638 you are using, you may not be sure which other irrelevant files
2639 might lead @code{make} to use the wrong implicit rule.  The choice
2640 might depend on the order in which the implicit rule search is done.
2641 With static pattern rules, there is no uncertainty: each rule applies
2642 to precisely the targets specified.
2643 @end itemize
2645 @node Double-Colon, Automatic Dependencies, Static Pattern, Rules
2646 @section Double-Colon Rules
2647 @cindex double-colon rules
2648 @cindex rule, double-colon (@code{::})
2649 @cindex multiple rules for one target (@code{::})
2650 @cindex @code{::} rules (double-colon)
2652 @dfn{Double-colon} rules are rules written with @samp{::} instead of
2653 @samp{:} after the target names.  They are handled differently from
2654 ordinary rules when the same target appears in more than one rule.
2656 When a target appears in multiple rules, all the rules must be the same
2657 type: all ordinary, or all double-colon.  If they are double-colon, each of
2658 them is independent of the others.  Each double-colon rule's commands are
2659 executed if the target is older than any dependencies of that rule.  This
2660 can result in executing none, any, or all of the double-colon rules.
2662 Double-colon rules with the same target are in fact completely separate
2663 from one another.  Each double-colon rule is processed individually, just
2664 as rules with different targets are processed.
2666 The double-colon rules for a target are executed in the order they appear
2667 in the makefile.  However, the cases where double-colon rules really make
2668 sense are those where the order of executing the commands would not matter.
2670 Double-colon rules are somewhat obscure and not often very useful; they
2671 provide a mechanism for cases in which the method used to update a target
2672 differs depending on which dependency files caused the update, and such
2673 cases are rare.
2675 Each double-colon rule should specify commands; if it does not, an
2676 implicit rule will be used if one applies.
2677 @xref{Implicit Rules, ,Using Implicit Rules}.
2679 @node Automatic Dependencies,  , Double-Colon, Rules
2680 @section Generating Dependencies Automatically
2681 @cindex dependencies, automatic generation
2682 @cindex automatic generation of dependencies
2683 @cindex generating dependencies automatically
2685 In the makefile for a program, many of the rules you need to write often
2686 say only that some object file depends on some header
2687 file.  For example, if @file{main.c} uses @file{defs.h} via an
2688 @code{#include}, you would write:
2690 @example
2691 main.o: defs.h
2692 @end example
2694 @noindent
2695 You need this rule so that @code{make} knows that it must remake
2696 @file{main.o} whenever @file{defs.h} changes.  You can see that for a
2697 large program you would have to write dozens of such rules in your
2698 makefile.  And, you must always be very careful to update the makefile
2699 every time you add or remove an @code{#include}.
2700 @cindex @code{#include}
2702 @cindex @code{-M} (to compiler)
2703 To avoid this hassle, most modern C compilers can write these rules for
2704 you, by looking at the @code{#include} lines in the source files.
2705 Usually this is done with the @samp{-M} option to the compiler.
2706 For example, the command:
2708 @example
2709 cc -M main.c
2710 @end example
2712 @noindent
2713 generates the output:
2715 @example
2716 main.o : main.c defs.h
2717 @end example
2719 @noindent
2720 Thus you no longer have to write all those rules yourself.
2721 The compiler will do it for you.
2723 Note that such a dependency constitutes mentioning @file{main.o} in a
2724 makefile, so it can never be considered an intermediate file by implicit
2725 rule search.  This means that @code{make} won't ever remove the file
2726 after using it; @pxref{Chained Rules, ,Chains of Implicit Rules}.
2728 @cindex @code{make depend}
2729 With old @code{make} programs, it was traditional practice to use this
2730 compiler feature to generate dependencies on demand with a command like
2731 @samp{make depend}.  That command would create a file @file{depend}
2732 containing all the automatically-generated dependencies; then the
2733 makefile could use @code{include} to read them in (@pxref{Include}).
2735 In GNU @code{make}, the feature of remaking makefiles makes this
2736 practice obsolete---you need never tell @code{make} explicitly to
2737 regenerate the dependencies, because it always regenerates any makefile
2738 that is out of date.  @xref{Remaking Makefiles}.
2740 The practice we recommend for automatic dependency generation is to have
2741 one makefile corresponding to each source file.  For each source file
2742 @file{@var{name}.c} there is a makefile @file{@var{name}.d} which lists
2743 what files the object file @file{@var{name}.o} depends on.  That way
2744 only the source files that have changed need to be rescanned to produce
2745 the new dependencies.
2747 Here is the pattern rule to generate a file of dependencies (i.e., a makefile)
2748 called @file{@var{name}.d} from a C source file called @file{@var{name}.c}:
2750 @smallexample
2751 @group
2752 %.d: %.c
2753         set -e; $(CC) -M $(CPPFLAGS) $< \
2754                   | sed 's/\($*\)\.o[ :]*/\1.o $@@ : /g' > $@@; \
2755                 [ -s $@@ ] || rm -f $@@
2756 @end group
2757 @end smallexample
2759 @noindent
2760 @xref{Pattern Rules}, for information on defining pattern rules.  The
2761 @samp{-e} flag to the shell makes it exit immediately if the
2762 @code{$(CC)} command fails (exits with a nonzero status).  Normally the
2763 shell exits with the status of the last command in the pipeline
2764 (@code{sed} in this case), so @code{make} would not notice a nonzero
2765 status from the compiler.
2766 @cindex @code{-e} (shell flag)
2768 @cindex @code{-MM} (to GNU compiler)
2769 With the GNU C compiler, you may wish to use the @samp{-MM} flag instead
2770 of @samp{-M}.  This omits dependencies on system header files.
2771 @xref{Preprocessor Options, , Options Controlling the Preprocessor,
2772 gcc.info, Using GNU CC}, for details.
2774 @cindex @code{sed} (shell command)
2775 The purpose of the @code{sed} command is to translate (for example):
2777 @example
2778 main.o : main.c defs.h
2779 @end example
2781 @noindent
2782 into:
2784 @example
2785 main.o main.d : main.c defs.h
2786 @end example
2788 @noindent
2789 @cindex @code{.d}
2790 This makes each @samp{.d} file depend on all the source and header files
2791 that the corresponding @samp{.o} file depends on.  @code{make} then
2792 knows it must regenerate the dependencies whenever any of the source or
2793 header files changes.
2795 Once you've defined the rule to remake the @samp{.d} files,
2796 you then use the @code{include} directive to read them all in.
2797 @xref{Include}.  For example:
2799 @example
2800 @group
2801 sources = foo.c bar.c
2803 include $(sources:.c=.d)
2804 @end group
2805 @end example
2807 @noindent
2808 (This example uses a substitution variable reference to translate the
2809 list of source files @samp{foo.c bar.c} into a list of dependency
2810 makefiles, @samp{foo.d bar.d}.  @xref{Substitution Refs}, for full
2811 information on substitution references.)  Since the @samp{.d} files are
2812 makefiles like any others, @code{make} will remake them as necessary
2813 with no further work from you.  @xref{Remaking Makefiles}.
2815 @node Commands, Using Variables, Rules, Top
2816 @chapter Writing the Commands in Rules
2817 @cindex commands, how to write
2818 @cindex rule commands
2819 @cindex writing rule commands
2821 The commands of a rule consist of shell command lines to be executed one
2822 by one.  Each command line must start with a tab, except that the first
2823 command line may be attached to the target-and-dependencies line with a
2824 semicolon in between.  Blank lines and lines of just comments may appear
2825 among the command lines; they are ignored.  (But beware, an apparently
2826 ``blank'' line that begins with a tab is @emph{not} blank!  It is an
2827 empty command; @pxref{Empty Commands}.)
2829 Users use many different shell programs, but commands in makefiles are
2830 always interpreted by @file{/bin/sh} unless the makefile specifies
2831 otherwise.  @xref{Execution, ,Command Execution}.
2833 @cindex comments, in commands
2834 @cindex commands, comments in
2835 @cindex @code{#} (comments), in commands
2836 The shell that is in use determines whether comments can be written on
2837 command lines, and what syntax they use.  When the shell is
2838 @file{/bin/sh}, a @samp{#} starts a comment that extends to the end of
2839 the line.  The @samp{#} does not have to be at the beginning of a line.
2840 Text on a line before a @samp{#} is not part of the comment.
2842 @menu
2843 * Echoing::                     How to control when commands are echoed.
2844 * Execution::                   How commands are executed.
2845 * Parallel::                    How commands can be executed in parallel.
2846 * Errors::                      What happens after a command execution error.
2847 * Interrupts::                  What happens when a command is interrupted.
2848 * Recursion::                   Invoking @code{make} from makefiles.
2849 * Sequences::                   Defining canned sequences of commands.
2850 * Empty Commands::              Defining useful, do-nothing commands.
2851 @end menu
2853 @node Echoing, Execution,  , Commands
2854 @section Command Echoing
2855 @cindex echoing of commands
2856 @cindex silent operation
2857 @cindex @code{@@} (in commands)
2858 @cindex commands, echoing
2859 @cindex printing of commands
2861 Normally @code{make} prints each command line before it is executed.
2862 We call this @dfn{echoing} because it gives the appearance that you
2863 are typing the commands yourself.
2865 When a line starts with @samp{@@}, the echoing of that line is suppressed.
2866 The @samp{@@} is discarded before the command is passed to the shell.
2867 Typically you would use this for a command whose only effect is to print
2868 something, such as an @code{echo} command to indicate progress through
2869 the makefile:
2871 @example
2872 @@echo About to make distribution files
2873 @end example
2875 @cindex @code{-n}
2876 @cindex @code{--just-print}
2877 @cindex @code{--dry-run}
2878 @cindex @code{--recon}
2879 When @code{make} is given the flag @samp{-n} or @samp{--just-print}
2880 it only echoes commands, it won't execute them.  @xref{Options Summary,
2881 ,Summary of Options}.  In this case and only this case, even the
2882 commands starting with @samp{@@} are printed.  This flag is useful for
2883 finding out which commands @code{make} thinks are necessary without
2884 actually doing them.
2886 @cindex @code{-s}
2887 @cindex @code{--silent}
2888 @cindex @code{--quiet}
2889 @findex .SILENT
2890 The @samp{-s} or @samp{--silent}
2891 flag to @code{make} prevents all echoing, as if all commands
2892 started with @samp{@@}.  A rule in the makefile for the special target
2893 @code{.SILENT} without dependencies has the same effect
2894 (@pxref{Special Targets, ,Special Built-in Target Names}).
2895 @code{.SILENT} is essentially obsolete since @samp{@@} is more flexible.@refill
2897 @node Execution, Parallel, Echoing, Commands
2898 @section Command Execution
2899 @cindex commands, execution
2900 @cindex execution, of commands
2901 @cindex shell command, execution
2902 @vindex SHELL @r{(command execution)}
2904 When it is time to execute commands to update a target, they are executed
2905 by making a new subshell for each line.  (In practice, @code{make} may
2906 take shortcuts that do not affect the results.)
2908 @cindex @code{cd} (shell command)
2909 @strong{Please note:} this implies that shell commands such as @code{cd}
2910 that set variables local to each process will not affect the following
2911 command lines. @footnote{On MS-DOS, the value of current working
2912 directory is @strong{global}, so changing it @emph{will} affect the
2913 following command lines on those systems.}  If you want to use @code{cd}
2914 to affect the next command, put the two on a single line with a
2915 semicolon between them.  Then @code{make} will consider them a single
2916 command and pass them, together, to a shell which will execute them in
2917 sequence.  For example:
2919 @example
2920 foo : bar/lose
2921         cd bar; gobble lose > ../foo
2922 @end example
2924 @cindex commands, backslash (@code{\}) in
2925 @cindex commands, quoting newlines in
2926 @cindex backslash (@code{\}), in commands
2927 @cindex @code{\} (backslash), in commands
2928 @cindex quoting newline, in commands
2929 @cindex newline, quoting, in commands
2930 If you would like to split a single shell command into multiple lines of
2931 text, you must use a backslash at the end of all but the last subline.
2932 Such a sequence of lines is combined into a single line, by deleting the
2933 backslash-newline sequences, before passing it to the shell.  Thus, the
2934 following is equivalent to the preceding example:
2936 @example
2937 @group
2938 foo : bar/lose
2939         cd bar;  \
2940         gobble lose > ../foo
2941 @end group
2942 @end example
2944 @vindex SHELL
2945 The program used as the shell is taken from the variable @code{SHELL}.
2946 By default, the program @file{/bin/sh} is used.
2948 @vindex COMSPEC
2949 On MS-DOS, if @code{SHELL} is not set, the value of the variable
2950 @code{COMSPEC} (which is always set) is used instead.
2952 @cindex @code{SHELL}, MS-DOS specifics
2953 The processing of lines that set the variable @code{SHELL} in Makefiles
2954 is different on MS-DOS.  The stock shell, @file{command.com}, is
2955 ridiculously limited in its functionality and many users of @code{make}
2956 tend to install a replacement shell.  Therefore, on MS-DOS, @code{make}
2957 examines the value of @code{SHELL}, and changes its behavior based on
2958 whether it points to a Unix-style or DOS-style shell.  This allows
2959 reasonable functionality even if @code{SHELL} points to
2960 @file{command.com}.
2962 If @code{SHELL} points to a Unix-style shell, @code{make} on MS-DOS
2963 additionally checks whether that shell can indeed be found; if not, it
2964 ignores the line that sets @code{SHELL}.  In MS-DOS, GNU @code{make}
2965 searches for the shell in the following places:
2967 @enumerate
2968 @item
2969 In the precise place pointed to by the value of @code{SHELL}.  For
2970 example, if the makefile specifies @samp{SHELL = /bin/sh}, @code{make}
2971 will look in the directory @file{/bin} on the current drive.
2973 @item
2974 In the current directory.
2976 @item
2977 In each of the directories in the @code{PATH} variable, in order.
2979 @end enumerate
2981 In every directory it examines, @code{make} will first look for the
2982 specific file (@file{sh} in the example above).  If this is not found,
2983 it will also look in that directory for that file with one of the known
2984 extensions which identify executable files.  For example @file{.exe},
2985 @file{.com}, @file{.bat}, @file{.btm}, @file{.sh}, and some others.
2987 If any of these attempts is successful, the value of @code{SHELL} will
2988 be set to the full pathname of the shell as found.  However, if none of
2989 these is found, the value of @code{SHELL} will not be changed, and thus
2990 the line that sets it will be effectively ignored.  This is so
2991 @code{make} will only support features specific to a Unix-style shell if
2992 such a shell is actually installed on the system where @code{make} runs.
2994 Note that this extended search for the shell is limited to the cases
2995 where @code{SHELL} is set from the Makefile; if it is set in the
2996 environment or command line, you are expected to set it to the full
2997 pathname of the shell, exactly as things are on Unix.
2999 The effect of the above DOS-specific processing is that a Makefile that
3000 says @samp{SHELL = /bin/sh} (as many Unix makefiles do), will work
3001 on MS-DOS unaltered if you have e.g. @file{sh.exe} installed in some
3002 directory along your @code{PATH}.
3004 @cindex environment, @code{SHELL} in
3005 Unlike most variables, the variable @code{SHELL} is never set from the
3006 environment.  This is because the @code{SHELL} environment variable is
3007 used to specify your personal choice of shell program for interactive
3008 use.  It would be very bad for personal choices like this to affect the
3009 functioning of makefiles.  @xref{Environment, ,Variables from the
3010 Environment}.  However, on MS-DOS and MS-Windows the value of
3011 @code{SHELL} in the environment @strong{is} used, since on those systems
3012 most users do not set this variable, and therefore it is most likely set
3013 specifically to be used by @code{make}.  On MS-DOS, if the setting of
3014 @code{SHELL} is not suitable for @code{make}, you can set the variable
3015 @code{MAKESHELL} to the shell that @code{make} should use; this will
3016 override the value of @code{SHELL}.
3018 @node Parallel, Errors, Execution, Commands
3019 @section Parallel Execution
3020 @cindex commands, execution in parallel
3021 @cindex parallel execution
3022 @cindex execution, in parallel
3023 @cindex job slots
3024 @cindex @code{-j}
3025 @cindex @code{--jobs}
3027 GNU @code{make} knows how to execute several commands at once.
3028 Normally, @code{make} will execute only one command at a time, waiting
3029 for it to finish before executing the next.  However, the @samp{-j} or
3030 @samp{--jobs} option tells @code{make} to execute many commands
3031 simultaneously.@refill
3033 On MS-DOS, the @samp{-j} option has no effect, since that system doesn't
3034 support multi-processing.
3036 If the @samp{-j} option is followed by an integer, this is the number of
3037 commands to execute at once; this is called the number of @dfn{job slots}.
3038 If there is nothing looking like an integer after the @samp{-j} option,
3039 there is no limit on the number of job slots.  The default number of job
3040 slots is one, which means serial execution (one thing at a time).
3042 One unpleasant consequence of running several commands simultaneously is
3043 that output generated by the commands appears whenever each command
3044 sends it, so messages from different commands may be interspersed.
3046 Another problem is that two processes cannot both take input from the
3047 same device; so to make sure that only one command tries to take input
3048 from the terminal at once, @code{make} will invalidate the standard
3049 input streams of all but one running command.  This means that
3050 attempting to read from standard input will usually be a fatal error (a
3051 @samp{Broken pipe} signal) for most child processes if there are
3052 several.
3053 @cindex broken pipe
3054 @cindex standard input
3056 It is unpredictable which command will have a valid standard input stream
3057 (which will come from the terminal, or wherever you redirect the standard
3058 input of @code{make}).  The first command run will always get it first, and
3059 the first command started after that one finishes will get it next, and so
3062 We will change how this aspect of @code{make} works if we find a better
3063 alternative.  In the mean time, you should not rely on any command using
3064 standard input at all if you are using the parallel execution feature; but
3065 if you are not using this feature, then standard input works normally in
3066 all commands.
3068 Finally, handling recursive @code{make} invocations raises issues.  For
3069 more information on this, see
3070 @ref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
3072 If a command fails (is killed by a signal or exits with a nonzero
3073 status), and errors are not ignored for that command
3074 (@pxref{Errors, ,Errors in Commands}),
3075 the remaining command lines to remake the same target will not be run.
3076 If a command fails and the @samp{-k} or @samp{--keep-going}
3077 option was not given
3078 (@pxref{Options Summary, ,Summary of Options}),
3079 @code{make} aborts execution.  If make
3080 terminates for any reason (including a signal) with child processes
3081 running, it waits for them to finish before actually exiting.@refill
3083 @cindex load average
3084 @cindex limiting jobs based on load
3085 @cindex jobs, limiting based on load
3086 @cindex @code{-l} (load average)
3087 @cindex @code{--max-load}
3088 @cindex @code{--load-average}
3089 When the system is heavily loaded, you will probably want to run fewer jobs
3090 than when it is lightly loaded.  You can use the @samp{-l} option to tell
3091 @code{make} to limit the number of jobs to run at once, based on the load
3092 average.  The @samp{-l} or @samp{--max-load}
3093 option is followed by a floating-point number.  For
3094 example,
3096 @example
3097 -l 2.5
3098 @end example
3100 @noindent
3101 will not let @code{make} start more than one job if the load average is
3102 above 2.5.  The @samp{-l} option with no following number removes the
3103 load limit, if one was given with a previous @samp{-l} option.@refill
3105 More precisely, when @code{make} goes to start up a job, and it already has
3106 at least one job running, it checks the current load average; if it is not
3107 lower than the limit given with @samp{-l}, @code{make} waits until the load
3108 average goes below that limit, or until all the other jobs finish.
3110 By default, there is no load limit.
3112 @node Errors, Interrupts, Parallel, Commands
3113 @section Errors in Commands
3114 @cindex errors (in commands)
3115 @cindex commands, errors in
3116 @cindex exit status (errors)
3118 After each shell command returns, @code{make} looks at its exit status.
3119 If the command completed successfully, the next command line is executed
3120 in a new shell; after the last command line is finished, the rule is
3121 finished.
3123 If there is an error (the exit status is nonzero), @code{make} gives up on
3124 the current rule, and perhaps on all rules.
3126 Sometimes the failure of a certain command does not indicate a problem.
3127 For example, you may use the @code{mkdir} command to ensure that a
3128 directory exists.  If the directory already exists, @code{mkdir} will
3129 report an error, but you probably want @code{make} to continue regardless.
3131 @cindex @code{-} (in commands)
3132 To ignore errors in a command line, write a @samp{-} at the beginning of
3133 the line's text (after the initial tab).  The @samp{-} is discarded before
3134 the command is passed to the shell for execution.
3136 For example,
3138 @example
3139 @group
3140 clean:
3141         -rm -f *.o
3142 @end group
3143 @end example
3144 @cindex @code{rm} (shell command)
3146 @noindent
3147 This causes @code{rm} to continue even if it is unable to remove a file.
3149 @cindex @code{-i}
3150 @cindex @code{--ignore-errors}
3151 @findex .IGNORE
3152 When you run @code{make} with the @samp{-i} or @samp{--ignore-errors}
3153 flag, errors are ignored in all commands of all rules.  A rule in the
3154 makefile for the special target @code{.IGNORE} has the same effect, if
3155 there are no dependencies.  These ways of ignoring errors are obsolete
3156 because @samp{-} is more flexible.
3158 When errors are to be ignored, because of either a @samp{-} or the
3159 @samp{-i} flag, @code{make} treats an error return just like success,
3160 except that it prints out a message that tells you the status code
3161 the command exited with, and says that the error has been ignored.
3163 When an error happens that @code{make} has not been told to ignore,
3164 it implies that the current target cannot be correctly remade, and neither
3165 can any other that depends on it either directly or indirectly.  No further
3166 commands will be executed for these targets, since their preconditions
3167 have not been achieved.
3170 @cindex @code{-k}
3171 @cindex @code{--keep-going}
3172 Normally @code{make} gives up immediately in this circumstance, returning a
3173 nonzero status.  However, if the @samp{-k} or @samp{--keep-going}
3174 flag is specified, @code{make}
3175 continues to consider the other dependencies of the pending targets,
3176 remaking them if necessary, before it gives up and returns nonzero status.
3177 For example, after an error in compiling one object file, @samp{make -k}
3178 will continue compiling other object files even though it already knows
3179 that linking them will be impossible.  @xref{Options Summary, ,Summary of Options}.
3181 The usual behavior assumes that your purpose is to get the specified
3182 targets up to date; once @code{make} learns that this is impossible, it
3183 might as well report the failure immediately.  The @samp{-k} option says
3184 that the real purpose is to test as many of the changes made in the
3185 program as possible, perhaps to find several independent problems so
3186 that you can correct them all before the next attempt to compile.  This
3187 is why Emacs' @code{compile} command passes the @samp{-k} flag by
3188 default.
3189 @cindex Emacs (@code{M-x compile})
3191 @findex .DELETE_ON_ERROR
3192 @cindex deletion of target files
3193 @cindex removal of target files
3194 @cindex target, deleting on error
3195 Usually when a command fails, if it has changed the target file at all,
3196 the file is corrupted and cannot be used---or at least it is not
3197 completely updated.  Yet the file's timestamp says that it is now up to
3198 date, so the next time @code{make} runs, it will not try to update that
3199 file.  The situation is just the same as when the command is killed by a
3200 signal; @pxref{Interrupts}.  So generally the right thing to do is to
3201 delete the target file if the command fails after beginning to change
3202 the file.  @code{make} will do this if @code{.DELETE_ON_ERROR} appears
3203 as a target.  This is almost always what you want @code{make} to do, but
3204 it is not historical practice; so for compatibility, you must explicitly
3205 request it.
3207 @node Interrupts, Recursion, Errors, Commands
3208 @section Interrupting or Killing @code{make}
3209 @cindex interrupt
3210 @cindex signal
3211 @cindex deletion of target files
3212 @cindex removal of target files
3213 @cindex target, deleting on interrupt
3214 @cindex killing (interruption)
3216 If @code{make} gets a fatal signal while a command is executing, it may
3217 delete the target file that the command was supposed to update.  This is
3218 done if the target file's last-modification time has changed since
3219 @code{make} first checked it.
3221 The purpose of deleting the target is to make sure that it is remade from
3222 scratch when @code{make} is next run.  Why is this?  Suppose you type
3223 @kbd{Ctrl-c} while a compiler is running, and it has begun to write an
3224 object file @file{foo.o}.  The @kbd{Ctrl-c} kills the compiler, resulting
3225 in an incomplete file whose last-modification time is newer than the source
3226 file @file{foo.c}.  But @code{make} also receives the @kbd{Ctrl-c} signal
3227 and deletes this incomplete file.  If @code{make} did not do this, the next
3228 invocation of @code{make} would think that @file{foo.o} did not require
3229 updating---resulting in a strange error message from the linker when it
3230 tries to link an object file half of which is missing.
3232 @findex .PRECIOUS
3233 You can prevent the deletion of a target file in this way by making the
3234 special target @code{.PRECIOUS} depend on it.  Before remaking a target,
3235 @code{make} checks to see whether it appears on the dependencies of
3236 @code{.PRECIOUS}, and thereby decides whether the target should be deleted
3237 if a signal happens.  Some reasons why you might do this are that the
3238 target is updated in some atomic fashion, or exists only to record a
3239 modification-time (its contents do not matter), or must exist at all
3240 times to prevent other sorts of trouble.
3242 @node Recursion, Sequences, Interrupts, Commands
3243 @section Recursive Use of @code{make}
3244 @cindex recursion
3245 @cindex subdirectories, recursion for
3247 Recursive use of @code{make} means using @code{make} as a command in a
3248 makefile.  This technique is useful when you want separate makefiles for
3249 various subsystems that compose a larger system.  For example, suppose you
3250 have a subdirectory @file{subdir} which has its own makefile, and you would
3251 like the containing directory's makefile to run @code{make} on the
3252 subdirectory.  You can do it by writing this:
3254 @example
3255 subsystem:
3256         cd subdir && $(MAKE)
3257 @end example
3259 @noindent
3260 or, equivalently, this (@pxref{Options Summary, ,Summary of Options}):
3262 @example
3263 subsystem:
3264         $(MAKE) -C subdir
3265 @end example
3266 @cindex @code{-C}
3267 @cindex @code{--directory}
3269 You can write recursive @code{make} commands just by copying this example,
3270 but there are many things to know about how they work and why, and about
3271 how the sub-@code{make} relates to the top-level @code{make}.
3273 For your convenience, GNU @code{make} sets the variable @code{CURDIR} to
3274 the pathname of the current working directory for you.  If @code{-C} is
3275 in effect, it will contain the path of the new directory, not the
3276 original.  The value has the same precedence it would have if it were
3277 set in the makefile (by default, an environment variable @code{CURDIR}
3278 will not override this value).  Note that setting this variable has no
3279 effect on the operation of @code{make}
3281 @menu
3282 * MAKE Variable::               The special effects of using @samp{$(MAKE)}.
3283 * Variables/Recursion::         How to communicate variables to a sub-@code{make}.
3284 * Options/Recursion::           How to communicate options to a sub-@code{make}.
3285 * -w Option::                   How the @samp{-w} or @samp{--print-directory} option
3286                                  helps debug use of recursive @code{make} commands.
3287 @end menu
3289 @node MAKE Variable, Variables/Recursion,  , Recursion
3290 @subsection How the @code{MAKE} Variable Works
3291 @vindex MAKE
3292 @cindex recursion, and @code{MAKE} variable
3294 Recursive @code{make} commands should always use the variable @code{MAKE},
3295 not the explicit command name @samp{make}, as shown here:
3297 @example
3298 @group
3299 subsystem:
3300         cd subdir && $(MAKE)
3301 @end group
3302 @end example
3304 The value of this variable is the file name with which @code{make} was
3305 invoked.  If this file name was @file{/bin/make}, then the command executed
3306 is @samp{cd subdir && /bin/make}.  If you use a special version of
3307 @code{make} to run the top-level makefile, the same special version will be
3308 executed for recursive invocations.
3309 @cindex @code{cd} (shell command)
3311 As a special feature, using the variable @code{MAKE} in the commands of
3312 a rule alters the effects of the @samp{-t} (@samp{--touch}), @samp{-n}
3313 (@samp{--just-print}), or @samp{-q} (@w{@samp{--question}}) option.
3314 Using the @code{MAKE} variable has the same effect as using a @samp{+}
3315 character at the beginning of the command line.  @xref{Instead of
3316 Execution, ,Instead of Executing the Commands}.@refill
3318 Consider the command @samp{make -t} in the above example.  (The
3319 @samp{-t} option marks targets as up to date without actually running
3320 any commands; see @ref{Instead of Execution}.)  Following the usual
3321 definition of @samp{-t}, a @samp{make -t} command in the example would
3322 create a file named @file{subsystem} and do nothing else.  What you
3323 really want it to do is run @samp{@w{cd subdir &&} @w{make -t}}; but that would
3324 require executing the command, and @samp{-t} says not to execute
3325 commands.@refill
3326 @cindex @code{-t}, and recursion
3327 @cindex recursion, and @code{-t}
3328 @cindex @code{--touch}, and recursion
3330 The special feature makes this do what you want: whenever a command
3331 line of a rule contains the variable @code{MAKE}, the flags @samp{-t},
3332 @samp{-n} and @samp{-q} do not apply to that line.  Command lines
3333 containing @code{MAKE} are executed normally despite the presence of a
3334 flag that causes most commands not to be run.  The usual
3335 @code{MAKEFLAGS} mechanism passes the flags to the sub-@code{make}
3336 (@pxref{Options/Recursion, ,Communicating Options to a
3337 Sub-@code{make}}), so your request to touch the files, or print the
3338 commands, is propagated to the subsystem.@refill
3340 @node Variables/Recursion, Options/Recursion, MAKE Variable, Recursion
3341 @subsection Communicating Variables to a Sub-@code{make}
3342 @cindex sub-@code{make}
3343 @cindex environment, and recursion
3344 @cindex exporting variables
3345 @cindex variables, environment
3346 @cindex variables, exporting
3347 @cindex recursion, and environment
3348 @cindex recursion, and variables
3350 Variable values of the top-level @code{make} can be passed to the
3351 sub-@code{make} through the environment by explicit request.  These
3352 variables are defined in the sub-@code{make} as defaults, but do not
3353 override what is specified in the makefile used by the sub-@code{make}
3354 makefile unless you use the @samp{-e} switch (@pxref{Options Summary,
3355 ,Summary of Options}).@refill
3357 To pass down, or @dfn{export}, a variable, @code{make} adds the variable
3358 and its value to the environment for running each command.  The
3359 sub-@code{make}, in turn, uses the environment to initialize its table
3360 of variable values.  @xref{Environment, ,Variables from the
3361 Environment}.
3363 Except by explicit request, @code{make} exports a variable only if it
3364 is either defined in the environment initially or set on the command
3365 line, and if its name consists only of letters, numbers, and underscores.
3366 Some shells cannot cope with environment variable names consisting of
3367 characters other than letters, numbers, and underscores.
3369 The special variables @code{SHELL} and @code{MAKEFLAGS} are always
3370 exported (unless you unexport them).
3371 @code{MAKEFILES} is exported if you set it to anything.
3373 @code{make} automatically passes down variable values that were defined
3374 on the command line, by putting them in the @code{MAKEFLAGS} variable.
3375 @iftex
3376 See the next section.
3377 @end iftex
3378 @ifinfo
3379 @xref{Options/Recursion}.
3380 @end ifinfo
3382 Variables are @emph{not} normally passed down if they were created by
3383 default by @code{make} (@pxref{Implicit Variables, ,Variables Used by
3384 Implicit Rules}).  The sub-@code{make} will define these for
3385 itself.@refill
3387 @findex export
3388 If you want to export specific variables to a sub-@code{make}, use the
3389 @code{export} directive, like this:
3391 @example
3392 export @var{variable} @dots{}
3393 @end example
3395 @noindent
3396 @findex unexport
3397 If you want to @emph{prevent} a variable from being exported, use the
3398 @code{unexport} directive, like this:
3400 @example
3401 unexport @var{variable} @dots{}
3402 @end example
3404 @noindent
3405 As a convenience, you can define a variable and export it at the same
3406 time by doing:
3408 @example
3409 export @var{variable} = value
3410 @end example
3412 @noindent
3413 has the same result as:
3415 @example
3416 @var{variable} = value
3417 export @var{variable}
3418 @end example
3420 @noindent
3423 @example
3424 export @var{variable} := value
3425 @end example
3427 @noindent
3428 has the same result as:
3430 @example
3431 @var{variable} := value
3432 export @var{variable}
3433 @end example
3435 Likewise,
3437 @example
3438 export @var{variable} += value
3439 @end example
3441 @noindent
3442 is just like:
3444 @example
3445 @var{variable} += value
3446 export @var{variable}
3447 @end example
3449 @noindent
3450 @xref{Appending, ,Appending More Text to Variables}.
3452 You may notice that the @code{export} and @code{unexport} directives
3453 work in @code{make} in the same way they work in the shell, @code{sh}.
3455 If you want all variables to be exported by default, you can use
3456 @code{export} by itself:
3458 @example
3459 export
3460 @end example
3462 @noindent
3463 This tells @code{make} that variables which are not explicitly mentioned
3464 in an @code{export} or @code{unexport} directive should be exported.
3465 Any variable given in an @code{unexport} directive will still @emph{not}
3466 be exported.  If you use @code{export} by itself to export variables by
3467 default, variables whose names contain characters other than
3468 alphanumerics and underscores will not be exported unless specifically
3469 mentioned in an @code{export} directive.@refill
3471 @findex .EXPORT_ALL_VARIABLES
3472 The behavior elicited by an @code{export} directive by itself was the
3473 default in older versions of GNU @code{make}.  If your makefiles depend
3474 on this behavior and you want to be compatible with old versions of
3475 @code{make}, you can write a rule for the special target
3476 @code{.EXPORT_ALL_VARIABLES} instead of using the @code{export} directive.
3477 This will be ignored by old @code{make}s, while the @code{export}
3478 directive will cause a syntax error.@refill
3479 @cindex compatibility in exporting
3481 Likewise, you can use @code{unexport} by itself to tell @code{make}
3482 @emph{not} to export variables by default.  Since this is the default
3483 behavior, you would only need to do this if @code{export} had been used
3484 by itself earlier (in an included makefile, perhaps).  You
3485 @strong{cannot} use @code{export} and @code{unexport} by themselves to
3486 have variables exported for some commands and not for others.  The last
3487 @code{export} or @code{unexport} directive that appears by itself
3488 determines the behavior for the entire run of @code{make}.@refill
3490 @vindex MAKELEVEL
3491 @cindex recursion, level of
3492 As a special feature, the variable @code{MAKELEVEL} is changed when it
3493 is passed down from level to level.  This variable's value is a string
3494 which is the depth of the level as a decimal number.  The value is
3495 @samp{0} for the top-level @code{make}; @samp{1} for a sub-@code{make},
3496 @samp{2} for a sub-sub-@code{make}, and so on.  The incrementation
3497 happens when @code{make} sets up the environment for a command.@refill
3499 The main use of @code{MAKELEVEL} is to test it in a conditional
3500 directive (@pxref{Conditionals, ,Conditional Parts of Makefiles}); this
3501 way you can write a makefile that behaves one way if run recursively and
3502 another way if run directly by you.@refill
3504 @vindex MAKEFILES
3505 You can use the variable @code{MAKEFILES} to cause all sub-@code{make}
3506 commands to use additional makefiles.  The value of @code{MAKEFILES} is
3507 a whitespace-separated list of file names.  This variable, if defined in
3508 the outer-level makefile, is passed down through the environment; then
3509 it serves as a list of extra makefiles for the sub-@code{make} to read
3510 before the usual or specified ones.  @xref{MAKEFILES Variable, ,The
3511 Variable @code{MAKEFILES}}.@refill
3513 @node Options/Recursion, -w Option, Variables/Recursion, Recursion
3514 @subsection Communicating Options to a Sub-@code{make}
3515 @cindex options, and recursion
3516 @cindex recursion, and options
3518 @vindex MAKEFLAGS
3519 Flags such as @samp{-s} and @samp{-k} are passed automatically to the
3520 sub-@code{make} through the variable @code{MAKEFLAGS}.  This variable is
3521 set up automatically by @code{make} to contain the flag letters that
3522 @code{make} received.  Thus, if you do @w{@samp{make -ks}} then
3523 @code{MAKEFLAGS} gets the value @samp{ks}.@refill
3525 As a consequence, every sub-@code{make} gets a value for @code{MAKEFLAGS}
3526 in its environment.  In response, it takes the flags from that value and
3527 processes them as if they had been given as arguments.
3528 @xref{Options Summary, ,Summary of Options}.
3530 @cindex command line variable definitions, and recursion
3531 @cindex variables, command line, and recursion
3532 @cindex recursion, and command line variable definitions
3533 Likewise variables defined on the command line are passed to the
3534 sub-@code{make} through @code{MAKEFLAGS}.  Words in the value of
3535 @code{MAKEFLAGS} that contain @samp{=}, @code{make} treats as variable
3536 definitions just as if they appeared on the command line.
3537 @xref{Overriding, ,Overriding Variables}.
3539 @cindex @code{-C}, and recursion
3540 @cindex @code{-f}, and recursion
3541 @cindex @code{-o}, and recursion
3542 @cindex @code{-W}, and recursion
3543 @cindex @code{--directory}, and recursion
3544 @cindex @code{--file}, and recursion
3545 @cindex @code{--old-file}, and recursion
3546 @cindex @code{--assume-old}, and recursion
3547 @cindex @code{--assume-new}, and recursion
3548 @cindex @code{--new-file}, and recursion
3549 @cindex recursion, and @code{-C}
3550 @cindex recursion, and @code{-f}
3551 @cindex recursion, and @code{-o}
3552 @cindex recursion, and @code{-W}
3553 The options @samp{-C}, @samp{-f}, @samp{-o}, and @samp{-W} are not put
3554 into @code{MAKEFLAGS}; these options are not passed down.@refill
3556 @cindex @code{-j}, and recursion
3557 @cindex @code{--jobs}, and recursion
3558 @cindex recursion, and @code{-j}
3559 @cindex job slots, and recursion
3560 The @samp{-j} option is a special case (@pxref{Parallel, ,Parallel Execution}).
3561 If you set it to some numeric value @samp{N} and your operating system
3562 supports it (most any UNIX system will; others typically won't), the
3563 parent @code{make} and all the sub-@code{make}s will communicate to
3564 ensure that there are only @samp{N} jobs running at the same time
3565 between them all.  Note that any job that is marked recursive
3566 (@pxref{Instead of Execution, ,Instead of Executing the Commands})
3567 doesn't count against the total jobs (otherwise we could get @samp{N}
3568 sub-@code{make}s running and have no slots left over for any real work!)
3570 If your operating system doesn't support the above communication, then
3571 @samp{-j 1} is always put into @code{MAKEFLAGS} instead of the value you
3572 specified.  This is because if the @w{@samp{-j}} option were passed down
3573 to sub-@code{make}s, you would get many more jobs running in parallel
3574 than you asked for.  If you give @samp{-j} with no numeric argument,
3575 meaning to run as many jobs as possible in parallel, this is passed
3576 down, since multiple infinities are no more than one.@refill
3578 If you do not want to pass the other flags down, you must change the
3579 value of @code{MAKEFLAGS}, like this:
3581 @example
3582 subsystem:
3583         cd subdir && $(MAKE) MAKEFLAGS=
3584 @end example
3586 @vindex MAKEOVERRIDES
3587 The command line variable definitions really appear in the variable
3588 @code{MAKEOVERRIDES}, and @code{MAKEFLAGS} contains a reference to this
3589 variable.  If you do want to pass flags down normally, but don't want to
3590 pass down the command line variable definitions, you can reset
3591 @code{MAKEOVERRIDES} to empty, like this:
3593 @example
3594 MAKEOVERRIDES =
3595 @end example
3597 @noindent
3598 @cindex Arg list too long
3599 @cindex E2BIG
3600 This is not usually useful to do.  However, some systems have a small
3601 fixed limit on the size of the environment, and putting so much
3602 information into the value of @code{MAKEFLAGS} can exceed it.  If you
3603 see the error message @samp{Arg list too long}, this may be the problem.
3604 @findex .POSIX
3605 @cindex POSIX.2
3606 (For strict compliance with POSIX.2, changing @code{MAKEOVERRIDES} does
3607 not affect @code{MAKEFLAGS} if the special target @samp{.POSIX} appears
3608 in the makefile.  You probably do not care about this.)
3610 @vindex MFLAGS
3611 A similar variable @code{MFLAGS} exists also, for historical
3612 compatibility.  It has the same value as @code{MAKEFLAGS} except that it
3613 does not contain the command line variable definitions, and it always
3614 begins with a hyphen unless it is empty (@code{MAKEFLAGS} begins with a
3615 hyphen only when it begins with an option that has no single-letter
3616 version, such as @samp{--warn-undefined-variables}).  @code{MFLAGS} was
3617 traditionally used explicitly in the recursive @code{make} command, like
3618 this:
3620 @example
3621 subsystem:
3622         cd subdir && $(MAKE) $(MFLAGS)
3623 @end example
3625 @noindent
3626 but now @code{MAKEFLAGS} makes this usage redundant.  If you want your
3627 makefiles to be compatible with old @code{make} programs, use this
3628 technique; it will work fine with more modern @code{make} versions too.
3630 @cindex setting options from environment
3631 @cindex options, setting from environment
3632 @cindex setting options in makefiles
3633 @cindex options, setting in makefiles
3634 The @code{MAKEFLAGS} variable can also be useful if you want to have
3635 certain options, such as @samp{-k} (@pxref{Options Summary, ,Summary of
3636 Options}), set each time you run @code{make}.  You simply put a value for
3637 @code{MAKEFLAGS} in your environment.  You can also set @code{MAKEFLAGS} in
3638 a makefile, to specify additional flags that should also be in effect for
3639 that makefile.  (Note that you cannot use @code{MFLAGS} this way.  That
3640 variable is set only for compatibility; @code{make} does not interpret a
3641 value you set for it in any way.)
3643 When @code{make} interprets the value of @code{MAKEFLAGS} (either from the
3644 environment or from a makefile), it first prepends a hyphen if the value
3645 does not already begin with one.  Then it chops the value into words
3646 separated by blanks, and parses these words as if they were options given
3647 on the command line (except that @samp{-C}, @samp{-f}, @samp{-h},
3648 @samp{-o}, @samp{-W}, and their long-named versions are ignored; and there
3649 is no error for an invalid option).
3651 If you do put @code{MAKEFLAGS} in your environment, you should be sure not
3652 to include any options that will drastically affect the actions of
3653 @code{make} and undermine the purpose of makefiles and of @code{make}
3654 itself.  For instance, the @samp{-t}, @samp{-n}, and @samp{-q} options, if
3655 put in one of these variables, could have disastrous consequences and would
3656 certainly have at least surprising and probably annoying effects.@refill
3658 @node -w Option,  , Options/Recursion, Recursion
3659 @subsection The @samp{--print-directory} Option
3660 @cindex directories, printing them
3661 @cindex printing directories
3662 @cindex recursion, and printing directories
3664 If you use several levels of recursive @code{make} invocations, the
3665 @samp{-w} or @w{@samp{--print-directory}} option can make the output a
3666 lot easier to understand by showing each directory as @code{make}
3667 starts processing it and as @code{make} finishes processing it.  For
3668 example, if @samp{make -w} is run in the directory @file{/u/gnu/make},
3669 @code{make} will print a line of the form:@refill
3671 @example
3672 make: Entering directory `/u/gnu/make'.
3673 @end example
3675 @noindent
3676 before doing anything else, and a line of the form:
3678 @example
3679 make: Leaving directory `/u/gnu/make'.
3680 @end example
3682 @noindent
3683 when processing is completed.
3685 @cindex @code{-C}, and @code{-w}
3686 @cindex @code{--directory}, and @code{--print-directory}
3687 @cindex recursion, and @code{-w}
3688 @cindex @code{-w}, and @code{-C}
3689 @cindex @code{-w}, and recursion
3690 @cindex @code{--print-directory}, and @code{--directory}
3691 @cindex @code{--print-directory}, and recursion
3692 @cindex @code{--no-print-directory}
3693 @cindex @code{--print-directory}, disabling
3694 @cindex @code{-w}, disabling
3695 Normally, you do not need to specify this option because @samp{make}
3696 does it for you: @samp{-w} is turned on automatically when you use the
3697 @samp{-C} option, and in sub-@code{make}s.  @code{make} will not
3698 automatically turn on @samp{-w} if you also use @samp{-s}, which says to
3699 be silent, or if you use @samp{--no-print-directory} to explicitly
3700 disable it.
3702 @node Sequences, Empty Commands, Recursion, Commands
3703 @section Defining Canned Command Sequences
3704 @cindex sequences of commands
3705 @cindex commands, sequences of
3707 When the same sequence of commands is useful in making various targets, you
3708 can define it as a canned sequence with the @code{define} directive, and
3709 refer to the canned sequence from the rules for those targets.  The canned
3710 sequence is actually a variable, so the name must not conflict with other
3711 variable names.
3713 Here is an example of defining a canned sequence of commands:
3715 @example
3716 define run-yacc
3717 yacc $(firstword $^)
3718 mv y.tab.c $@@
3719 endef
3720 @end example
3721 @cindex @code{yacc}
3723 @noindent
3724 Here @code{run-yacc} is the name of the variable being defined;
3725 @code{endef} marks the end of the definition; the lines in between are the
3726 commands.  The @code{define} directive does not expand variable references
3727 and function calls in the canned sequence; the @samp{$} characters,
3728 parentheses, variable names, and so on, all become part of the value of the
3729 variable you are defining.
3730 @xref{Defining, ,Defining Variables Verbatim},
3731 for a complete explanation of @code{define}.
3733 The first command in this example runs Yacc on the first dependency of
3734 whichever rule uses the canned sequence.  The output file from Yacc is
3735 always named @file{y.tab.c}.  The second command moves the output to the
3736 rule's target file name.
3738 To use the canned sequence, substitute the variable into the commands of a
3739 rule.  You can substitute it like any other variable
3740 (@pxref{Reference, ,Basics of Variable References}).
3741 Because variables defined by @code{define} are recursively expanded
3742 variables, all the variable references you wrote inside the @code{define}
3743 are expanded now.  For example:
3745 @example
3746 foo.c : foo.y
3747         $(run-yacc)
3748 @end example
3750 @noindent
3751 @samp{foo.y} will be substituted for the variable @samp{$^} when it occurs in
3752 @code{run-yacc}'s value, and @samp{foo.c} for @samp{$@@}.@refill
3754 This is a realistic example, but this particular one is not needed in
3755 practice because @code{make} has an implicit rule to figure out these
3756 commands based on the file names involved
3757 (@pxref{Implicit Rules, ,Using Implicit Rules}).
3759 @cindex @@, and @code{define}
3760 @cindex -, and @code{define}
3761 @cindex +, and @code{define}
3762 In command execution, each line of a canned sequence is treated just as
3763 if the line appeared on its own in the rule, preceded by a tab.  In
3764 particular, @code{make} invokes a separate subshell for each line.  You
3765 can use the special prefix characters that affect command lines
3766 (@samp{@@}, @samp{-}, and @samp{+}) on each line of a canned sequence.
3767 @xref{Commands, ,Writing the Commands in Rules}.
3768 For example, using this canned sequence:
3770 @example
3771 define frobnicate
3772 @@echo "frobnicating target $@@"
3773 frob-step-1 $< -o $@@-step-1
3774 frob-step-2 $@@-step-1 -o $@@
3775 endef
3776 @end example
3778 @noindent
3779 @code{make} will not echo the first line, the @code{echo} command.
3780 But it @emph{will} echo the following two command lines.
3782 On the other hand, prefix characters on the command line that refers to
3783 a canned sequence apply to every line in the sequence.  So the rule:
3785 @example
3786 frob.out: frob.in
3787         @@$(frobnicate)
3788 @end example
3790 @noindent
3791 does not echo @emph{any} commands.
3792 (@xref{Echoing, ,Command Echoing}, for a full explanation of @samp{@@}.)
3794 @node Empty Commands,  , Sequences, Commands
3795 @section Using Empty Commands
3796 @cindex empty commands
3797 @cindex commands, empty
3799 It is sometimes useful to define commands which do nothing.  This is done
3800 simply by giving a command that consists of nothing but whitespace.  For
3801 example:
3803 @example
3804 target: ;
3805 @end example
3807 @noindent
3808 defines an empty command string for @file{target}.  You could also use a
3809 line beginning with a tab character to define an empty command string,
3810 but this would be confusing because such a line looks empty.
3812 @findex .DEFAULT@r{, and empty commands}
3813 You may be wondering why you would want to define a command string that
3814 does nothing.  The only reason this is useful is to prevent a target
3815 from getting implicit commands (from implicit rules or the
3816 @code{.DEFAULT} special target; @pxref{Implicit Rules} and
3817 @pxref{Last Resort, ,Defining Last-Resort Default Rules}).@refill
3819 @c !!! another reason is for canonical stamp files:
3820 @ignore
3821 foo: stamp-foo ;
3822 stamp-foo: foo.in
3823         create foo frm foo.in
3824         touch $@
3825 @end ignore
3827 You may be inclined to define empty command strings for targets that are
3828 not actual files, but only exist so that their dependencies can be
3829 remade.  However, this is not the best way to do that, because the
3830 dependencies may not be remade properly if the target file actually does exist.
3831 @xref{Phony Targets, ,Phony Targets}, for a better way to do this.
3833 @node Using Variables, Conditionals, Commands, Top
3834 @chapter How to Use Variables
3835 @cindex variable
3836 @cindex value
3837 @cindex recursive variable expansion
3838 @cindex simple variable expansion
3840 A @dfn{variable} is a name defined in a makefile to represent a string
3841 of text, called the variable's @dfn{value}.  These values are
3842 substituted by explicit request into targets, dependencies, commands,
3843 and other parts of the makefile.  (In some other versions of @code{make},
3844 variables are called @dfn{macros}.)
3845 @cindex macro
3847 Variables and functions in all parts of a makefile are expanded when
3848 read, except for the shell commands in rules, the right-hand sides of
3849 variable definitions using @samp{=}, and the bodies of variable
3850 definitions using the @code{define} directive.@refill
3852 Variables can represent lists of file names, options to pass to compilers,
3853 programs to run, directories to look in for source files, directories to
3854 write output in, or anything else you can imagine.
3856 A variable name may be any sequence of characters not containing @samp{:},
3857 @samp{#}, @samp{=}, or leading or trailing whitespace.  However,
3858 variable names containing characters other than letters, numbers, and
3859 underscores should be avoided, as they may be given special meanings in the
3860 future, and with some shells they cannot be passed through the environment to a
3861 sub-@code{make}
3862 (@pxref{Variables/Recursion, ,Communicating Variables to a Sub-@code{make}}).
3864 Variable names are case-sensitive.  The names @samp{foo}, @samp{FOO},
3865 and @samp{Foo} all refer to different variables.
3867 It is traditional to use upper case letters in variable names, but we
3868 recommend using lower case letters for variable names that serve internal
3869 purposes in the makefile, and reserving upper case for parameters that
3870 control implicit rules or for parameters that the user should override with
3871 command options (@pxref{Overriding, ,Overriding Variables}).
3873 A few variables have names that are a single punctuation character or
3874 just a few characters.  These are the @dfn{automatic variables}, and
3875 they have particular specialized uses.  @xref{Automatic, ,Automatic Variables}.
3877 @menu
3878 * Reference::                   How to use the value of a variable.
3879 * Flavors::                     Variables come in two flavors.
3880 * Advanced::                    Advanced features for referencing a variable.
3881 * Values::                      All the ways variables get their values.
3882 * Setting::                     How to set a variable in the makefile.
3883 * Appending::                   How to append more text to the old value
3884                                   of a variable.
3885 * Override Directive::          How to set a variable in the makefile even if
3886                                   the user has set it with a command argument.
3887 * Defining::                    An alternate way to set a variable
3888                                   to a verbatim string.
3889 * Environment::                 Variable values can come from the environment.
3890 * Target-specific::             Variable values can be defined on a per-target
3891                                   basis.
3892 * Pattern-specific::            Target-specific variable values can be applied
3893                                   to a group of targets that match a pattern.
3894 * Automatic::                   Some special variables have predefined
3895                                   meanings for use with implicit rules.
3896 @end menu
3898 @node Reference, Flavors,  , Using Variables
3899 @section Basics of Variable References
3900 @cindex variables, how to reference
3901 @cindex reference to variables
3902 @cindex @code{$}, in variable reference
3903 @cindex dollar sign (@code{$}), in variable reference
3905 To substitute a variable's value, write a dollar sign followed by the name
3906 of the variable in parentheses or braces: either @samp{$(foo)} or
3907 @samp{$@{foo@}} is a valid reference to the variable @code{foo}.  This
3908 special significance of @samp{$} is why you must write @samp{$$} to have
3909 the effect of a single dollar sign in a file name or command.
3911 Variable references can be used in any context: targets, dependencies,
3912 commands, most directives, and new variable values.  Here is an
3913 example of a common case, where a variable holds the names of all the
3914 object files in a program:
3916 @example
3917 @group
3918 objects = program.o foo.o utils.o
3919 program : $(objects)
3920         cc -o program $(objects)
3922 $(objects) : defs.h
3923 @end group
3924 @end example
3926 Variable references work by strict textual substitution.  Thus, the rule
3928 @example
3929 @group
3930 foo = c
3931 prog.o : prog.$(foo)
3932         $(foo)$(foo) -$(foo) prog.$(foo)
3933 @end group
3934 @end example
3936 @noindent
3937 could be used to compile a C program @file{prog.c}.  Since spaces before
3938 the variable value are ignored in variable assignments, the value of
3939 @code{foo} is precisely @samp{c}.  (Don't actually write your makefiles
3940 this way!)
3942 A dollar sign followed by a character other than a dollar sign,
3943 open-parenthesis or open-brace treats that single character as the
3944 variable name.  Thus, you could reference the variable @code{x} with
3945 @samp{$x}.  However, this practice is strongly discouraged, except in
3946 the case of the automatic variables (@pxref{Automatic, ,Automatic Variables}).
3948 @node Flavors, Advanced, Reference, Using Variables
3949 @section The Two Flavors of Variables
3950 @cindex flavors of variables
3951 @cindex recursive variable expansion
3952 @cindex variables, flavors
3953 @cindex recursively expanded variables
3954 @cindex variables, recursively expanded
3956 There are two ways that a variable in GNU @code{make} can have a value;
3957 we call them the two @dfn{flavors} of variables.  The two flavors are
3958 distinguished in how they are defined and in what they do when expanded.
3960 @cindex =
3961 The first flavor of variable is a @dfn{recursively expanded} variable.
3962 Variables of this sort are defined by lines using @samp{=}
3963 (@pxref{Setting, ,Setting Variables}) or by the @code{define} directive
3964 (@pxref{Defining, ,Defining Variables Verbatim}).  The value you specify
3965 is installed verbatim; if it contains references to other variables,
3966 these references are expanded whenever this variable is substituted (in
3967 the course of expanding some other string).  When this happens, it is
3968 called @dfn{recursive expansion}.@refill
3970 For example,
3972 @example
3973 foo = $(bar)
3974 bar = $(ugh)
3975 ugh = Huh?
3977 all:;echo $(foo)
3978 @end example
3980 @noindent
3981 will echo @samp{Huh?}: @samp{$(foo)} expands to @samp{$(bar)} which
3982 expands to @samp{$(ugh)} which finally expands to @samp{Huh?}.@refill
3984 This flavor of variable is the only sort supported by other versions of
3985 @code{make}.  It has its advantages and its disadvantages.  An advantage
3986 (most would say) is that:
3988 @example
3989 CFLAGS = $(include_dirs) -O
3990 include_dirs = -Ifoo -Ibar
3991 @end example
3993 @noindent
3994 will do what was intended: when @samp{CFLAGS} is expanded in a command,
3995 it will expand to @samp{-Ifoo -Ibar -O}.  A major disadvantage is that you
3996 cannot append something on the end of a variable, as in
3998 @example
3999 CFLAGS = $(CFLAGS) -O
4000 @end example
4002 @noindent
4003 because it will cause an infinite loop in the variable expansion.
4004 (Actually @code{make} detects the infinite loop and reports an error.)
4005 @cindex loops in variable expansion
4006 @cindex variables, loops in expansion
4008 Another disadvantage is that any functions
4009 (@pxref{Functions, ,Functions for Transforming Text})
4010 referenced in the definition will be executed every time the variable is
4011 expanded.  This makes @code{make} run slower; worse, it causes the
4012 @code{wildcard} and @code{shell} functions to give unpredictable results
4013 because you cannot easily control when they are called, or even how many
4014 times.
4016 To avoid all the problems and inconveniences of recursively expanded
4017 variables, there is another flavor: simply expanded variables.
4019 @cindex simply expanded variables
4020 @cindex variables, simply expanded
4021 @cindex :=
4022 @dfn{Simply expanded variables} are defined by lines using @samp{:=}
4023 (@pxref{Setting, ,Setting Variables}).
4024 The value of a simply expanded variable is scanned
4025 once and for all, expanding any references to other variables and
4026 functions, when the variable is defined.  The actual value of the simply
4027 expanded variable is the result of expanding the text that you write.
4028 It does not contain any references to other variables; it contains their
4029 values @emph{as of the time this variable was defined}.  Therefore,
4031 @example
4032 x := foo
4033 y := $(x) bar
4034 x := later
4035 @end example
4037 @noindent
4038 is equivalent to
4040 @example
4041 y := foo bar
4042 x := later
4043 @end example
4045 When a simply expanded variable is referenced, its value is substituted
4046 verbatim.
4048 Here is a somewhat more complicated example, illustrating the use of
4049 @samp{:=} in conjunction with the @code{shell} function.
4050 (@xref{Shell Function, , The @code{shell} Function}.)  This example
4051 also shows use of the variable @code{MAKELEVEL}, which is changed
4052 when it is passed down from level to level.
4053 (@xref{Variables/Recursion, , Communicating Variables to a
4054 Sub-@code{make}}, for information about @code{MAKELEVEL}.)
4056 @vindex MAKELEVEL
4057 @vindex MAKE
4058 @example
4059 @group
4060 ifeq (0,$@{MAKELEVEL@})
4061 cur-dir   := $(shell pwd)
4062 whoami    := $(shell whoami)
4063 host-type := $(shell arch)
4064 MAKE := $@{MAKE@} host-type=$@{host-type@} whoami=$@{whoami@}
4065 endif
4066 @end group
4067 @end example
4069 @noindent
4070 An advantage of this use of @samp{:=} is that a typical
4071 `descend into a directory' command then looks like this:
4073 @example
4074 @group
4075 $@{subdirs@}:
4076       $@{MAKE@} cur-dir=$@{cur-dir@}/$@@ -C $@@ all
4077 @end group
4078 @end example
4080 Simply expanded variables generally make complicated makefile programming
4081 more predictable because they work like variables in most programming
4082 languages.  They allow you to redefine a variable using its own value (or
4083 its value processed in some way by one of the expansion functions) and to
4084 use the expansion functions much more efficiently
4085 (@pxref{Functions, ,Functions for Transforming Text}).
4087 @cindex spaces, in variable values
4088 @cindex whitespace, in variable values
4089 @cindex variables, spaces in values
4090 You can also use them to introduce controlled leading whitespace into
4091 variable values.  Leading whitespace characters are discarded from your
4092 input before substitution of variable references and function calls;
4093 this means you can include leading spaces in a variable value by
4094 protecting them with variable references, like this:
4096 @example
4097 nullstring :=
4098 space := $(nullstring) # end of the line
4099 @end example
4101 @noindent
4102 Here the value of the variable @code{space} is precisely one space.  The
4103 comment @w{@samp{# end of the line}} is included here just for clarity.
4104 Since trailing space characters are @emph{not} stripped from variable
4105 values, just a space at the end of the line would have the same effect
4106 (but be rather hard to read).  If you put whitespace at the end of a
4107 variable value, it is a good idea to put a comment like that at the end
4108 of the line to make your intent clear.  Conversely, if you do @emph{not}
4109 want any whitespace characters at the end of your variable value, you
4110 must remember not to put a random comment on the end of the line after
4111 some whitespace, such as this:
4113 @example
4114 dir := /foo/bar    # directory to put the frobs in
4115 @end example
4117 @noindent
4118 Here the value of the variable @code{dir} is @w{@samp{/foo/bar    }}
4119 (with four trailing spaces), which was probably not the intention.
4120 (Imagine something like @w{@samp{$(dir)/file}} with this definition!)
4122 @cindex conditional variable assignment
4123 @cindex variables, conditional assignment
4124 @cindex ?=
4125 There is another assignment operator for variables, @samp{?=}.  This
4126 is called a conditional variable assignment operator, because it only
4127 has an effect if the variable is not yet defined.  This statement:
4129 @example
4130 FOO ?= bar
4131 @end example
4133 @noindent
4134 is exactly equivalent to this
4135 (@pxref{Origin Function, ,The @code{origin} Function}):
4137 @example
4138 ifeq ($(origin FOO), undefined)
4139   FOO = bar
4140 endif
4141 @end example
4143 Note that a variable set to an empty value is still defined, so
4144 @samp{?=} will not set that variable.
4146 @node Advanced, Values, Flavors, Using Variables
4147 @section Advanced Features for Reference to Variables
4148 @cindex reference to variables
4150 This section describes some advanced features you can use to reference
4151 variables in more flexible ways.
4153 @menu
4154 * Substitution Refs::           Referencing a variable with
4155                                   substitutions on the value.
4156 * Computed Names::              Computing the name of the variable to refer to.
4157 @end menu
4159 @node Substitution Refs, Computed Names,  , Advanced
4160 @subsection Substitution References
4161 @cindex modified variable reference
4162 @cindex substitution variable reference
4163 @cindex variables, modified reference
4164 @cindex variables, substitution reference
4166 @cindex variables, substituting suffix in
4167 @cindex suffix, substituting in variables
4168 A @dfn{substitution reference} substitutes the value of a variable with
4169 alterations that you specify.  It has the form
4170 @samp{$(@var{var}:@var{a}=@var{b})} (or
4171 @samp{$@{@var{var}:@var{a}=@var{b}@}}) and its meaning is to take the value
4172 of the variable @var{var}, replace every @var{a} at the end of a word with
4173 @var{b} in that value, and substitute the resulting string.
4175 When we say ``at the end of a word'', we mean that @var{a} must appear
4176 either followed by whitespace or at the end of the value in order to be
4177 replaced; other occurrences of @var{a} in the value are unaltered.  For
4178 example:@refill
4180 @example
4181 foo := a.o b.o c.o
4182 bar := $(foo:.o=.c)
4183 @end example
4185 @noindent
4186 sets @samp{bar} to @samp{a.c b.c c.c}.  @xref{Setting, ,Setting Variables}.
4188 A substitution reference is actually an abbreviation for use of the
4189 @code{patsubst} expansion function (@pxref{Text Functions, ,Functions for String Substitution and Analysis}).  We provide
4190 substitution references as well as @code{patsubst} for compatibility with
4191 other implementations of @code{make}.
4193 @findex patsubst
4194 Another type of substitution reference lets you use the full power of
4195 the @code{patsubst} function.  It has the same form
4196 @samp{$(@var{var}:@var{a}=@var{b})} described above, except that now
4197 @var{a} must contain a single @samp{%} character.  This case is
4198 equivalent to @samp{$(patsubst @var{a},@var{b},$(@var{var}))}.
4199 @xref{Text Functions, ,Functions for String Substitution and Analysis},
4200 for a description of the @code{patsubst} function.@refill
4202 @example
4203 @group
4204 @exdent For example:
4206 foo := a.o b.o c.o
4207 bar := $(foo:%.o=%.c)
4208 @end group
4209 @end example
4211 @noindent
4212 sets @samp{bar} to @samp{a.c b.c c.c}.
4214 @node Computed Names,  , Substitution Refs, Advanced
4215 @subsection Computed Variable Names
4216 @cindex nested variable reference
4217 @cindex computed variable name
4218 @cindex variables, computed names
4219 @cindex variables, nested references
4220 @cindex variables, @samp{$} in name
4221 @cindex @code{$}, in variable name
4222 @cindex dollar sign (@code{$}), in variable name
4224 Computed variable names are a complicated concept needed only for
4225 sophisticated makefile programming.  For most purposes you need not
4226 consider them, except to know that making a variable with a dollar sign
4227 in its name might have strange results.  However, if you are the type
4228 that wants to understand everything, or you are actually interested in
4229 what they do, read on.
4231 Variables may be referenced inside the name of a variable.  This is
4232 called a @dfn{computed variable name} or a @dfn{nested variable
4233 reference}.  For example,
4235 @example
4236 x = y
4237 y = z
4238 a := $($(x))
4239 @end example
4241 @noindent
4242 defines @code{a} as @samp{z}: the @samp{$(x)} inside @samp{$($(x))} expands
4243 to @samp{y}, so @samp{$($(x))} expands to @samp{$(y)} which in turn expands
4244 to @samp{z}.  Here the name of the variable to reference is not stated
4245 explicitly; it is computed by expansion of @samp{$(x)}.  The reference
4246 @samp{$(x)} here is nested within the outer variable reference.
4248 The previous example shows two levels of nesting, but any number of levels
4249 is possible.  For example, here are three levels:
4251 @example
4252 x = y
4253 y = z
4254 z = u
4255 a := $($($(x)))
4256 @end example
4258 @noindent
4259 Here the innermost @samp{$(x)} expands to @samp{y}, so @samp{$($(x))}
4260 expands to @samp{$(y)} which in turn expands to @samp{z}; now we have
4261 @samp{$(z)}, which becomes @samp{u}.
4263 References to recursively-expanded variables within a variable name are
4264 reexpanded in the usual fashion.  For example:
4266 @example
4267 x = $(y)
4268 y = z
4269 z = Hello
4270 a := $($(x))
4271 @end example
4273 @noindent
4274 defines @code{a} as @samp{Hello}: @samp{$($(x))} becomes @samp{$($(y))}
4275 which becomes @samp{$(z)} which becomes @samp{Hello}.
4277 Nested variable references can also contain modified references and
4278 function invocations (@pxref{Functions, ,Functions for Transforming Text}),
4279 just like any other reference.
4280 For example, using the @code{subst} function
4281 (@pxref{Text Functions, ,Functions for String Substitution and Analysis}):
4283 @example
4284 @group
4285 x = variable1
4286 variable2 := Hello
4287 y = $(subst 1,2,$(x))
4288 z = y
4289 a := $($($(z)))
4290 @end group
4291 @end example
4293 @noindent
4294 eventually defines @code{a} as @samp{Hello}.  It is doubtful that anyone
4295 would ever want to write a nested reference as convoluted as this one, but
4296 it works: @samp{$($($(z)))} expands to @samp{$($(y))} which becomes
4297 @samp{$($(subst 1,2,$(x)))}.  This gets the value @samp{variable1} from
4298 @code{x} and changes it by substitution to @samp{variable2}, so that the
4299 entire string becomes @samp{$(variable2)}, a simple variable reference
4300 whose value is @samp{Hello}.@refill
4302 A computed variable name need not consist entirely of a single variable
4303 reference.  It can contain several variable references, as well as some
4304 invariant text.  For example,
4306 @example
4307 @group
4308 a_dirs := dira dirb
4309 1_dirs := dir1 dir2
4310 @end group
4312 @group
4313 a_files := filea fileb
4314 1_files := file1 file2
4315 @end group
4317 @group
4318 ifeq "$(use_a)" "yes"
4319 a1 := a
4320 else
4321 a1 := 1
4322 endif
4323 @end group
4325 @group
4326 ifeq "$(use_dirs)" "yes"
4327 df := dirs
4328 else
4329 df := files
4330 endif
4332 dirs := $($(a1)_$(df))
4333 @end group
4334 @end example
4336 @noindent
4337 will give @code{dirs} the same value as @code{a_dirs}, @code{1_dirs},
4338 @code{a_files} or @code{1_files} depending on the settings of @code{use_a}
4339 and @code{use_dirs}.@refill
4341 Computed variable names can also be used in substitution references:
4343 @example
4344 @group
4345 a_objects := a.o b.o c.o
4346 1_objects := 1.o 2.o 3.o
4348 sources := $($(a1)_objects:.o=.c)
4349 @end group
4350 @end example
4352 @noindent
4353 defines @code{sources} as either @samp{a.c b.c c.c} or @samp{1.c 2.c 3.c},
4354 depending on the value of @code{a1}.
4356 The only restriction on this sort of use of nested variable references
4357 is that they cannot specify part of the name of a function to be called.
4358 This is because the test for a recognized function name is done before
4359 the expansion of nested references.  For example,
4361 @example
4362 @group
4363 ifdef do_sort
4364 func := sort
4365 else
4366 func := strip
4367 endif
4368 @end group
4370 @group
4371 bar := a d b g q c
4372 @end group
4374 @group
4375 foo := $($(func) $(bar))
4376 @end group
4377 @end example
4379 @noindent
4380 attempts to give @samp{foo} the value of the variable @samp{sort a d b g
4381 q c} or @samp{strip a d b g q c}, rather than giving @samp{a d b g q c}
4382 as the argument to either the @code{sort} or the @code{strip} function.
4383 This restriction could be removed in the future if that change is shown
4384 to be a good idea.
4386 You can also use computed variable names in the left-hand side of a
4387 variable assignment, or in a @code{define} directive, as in:
4389 @example
4390 dir = foo
4391 $(dir)_sources := $(wildcard $(dir)/*.c)
4392 define $(dir)_print
4393 lpr $($(dir)_sources)
4394 endef
4395 @end example
4397 @noindent
4398 This example defines the variables @samp{dir}, @samp{foo_sources}, and
4399 @samp{foo_print}.
4401 Note that @dfn{nested variable references} are quite different from
4402 @dfn{recursively expanded variables}
4403 (@pxref{Flavors, ,The Two Flavors of Variables}), though both are
4404 used together in complex ways when doing makefile programming.@refill
4406 @node Values, Setting, Advanced, Using Variables
4407 @section How Variables Get Their Values
4408 @cindex variables, how they get their values
4409 @cindex value, how a variable gets it
4411 Variables can get values in several different ways:
4413 @itemize @bullet
4414 @item
4415 You can specify an overriding value when you run @code{make}.
4416 @xref{Overriding, ,Overriding Variables}.
4418 @item
4419 You can specify a value in the makefile, either
4420 with an assignment (@pxref{Setting, ,Setting Variables}) or with a
4421 verbatim definition (@pxref{Defining, ,Defining Variables Verbatim}).@refill
4423 @item
4424 Variables in the environment become @code{make} variables.
4425 @xref{Environment, ,Variables from the Environment}.
4427 @item
4428 Several @dfn{automatic} variables are given new values for each rule.
4429 Each of these has a single conventional use.
4430 @xref{Automatic, ,Automatic Variables}.
4432 @item
4433 Several variables have constant initial values.
4434 @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
4435 @end itemize
4437 @node Setting, Appending, Values, Using Variables
4438 @section Setting Variables
4439 @cindex setting variables
4440 @cindex variables, setting
4441 @cindex =
4442 @cindex :=
4443 @cindex ?=
4445 To set a variable from the makefile, write a line starting with the
4446 variable name followed by @samp{=} or @samp{:=}.  Whatever follows the
4447 @samp{=} or @samp{:=} on the line becomes the value.  For example,
4449 @example
4450 objects = main.o foo.o bar.o utils.o
4451 @end example
4453 @noindent
4454 defines a variable named @code{objects}.  Whitespace around the variable
4455 name and immediately after the @samp{=} is ignored.
4457 Variables defined with @samp{=} are @dfn{recursively expanded} variables.
4458 Variables defined with @samp{:=} are @dfn{simply expanded} variables; these
4459 definitions can contain variable references which will be expanded before
4460 the definition is made.  @xref{Flavors, ,The Two Flavors of Variables}.
4462 The variable name may contain function and variable references, which
4463 are expanded when the line is read to find the actual variable name to use.
4465 There is no limit on the length of the value of a variable except the
4466 amount of swapping space on the computer.  When a variable definition is
4467 long, it is a good idea to break it into several lines by inserting
4468 backslash-newline at convenient places in the definition.  This will not
4469 affect the functioning of @code{make}, but it will make the makefile easier
4470 to read.
4472 Most variable names are considered to have the empty string as a value if
4473 you have never set them.  Several variables have built-in initial values
4474 that are not empty, but you can set them in the usual ways
4475 (@pxref{Implicit Variables, ,Variables Used by Implicit Rules}).
4476 Several special variables are set
4477 automatically to a new value for each rule; these are called the
4478 @dfn{automatic} variables (@pxref{Automatic, ,Automatic Variables}).
4480 If you'd like a variable to be set to a value only if it's not already
4481 set, then you can use the shorthand operator @samp{?=} instead of
4482 @samp{=}.  These two settings of the variable @samp{FOO} are identical
4483 (@pxref{Origin Function, ,The @code{origin} Function}):
4485 @example
4486 FOO ?= bar
4487 @end example
4489 @noindent
4492 @example
4493 ifeq ($(origin FOO), undefined)
4494 FOO = bar
4495 endif
4496 @end example
4498 @node Appending, Override Directive, Setting, Using Variables
4499 @section Appending More Text to Variables
4500 @cindex +=
4501 @cindex appending to variables
4502 @cindex variables, appending to
4504 Often it is useful to add more text to the value of a variable already defined.
4505 You do this with a line containing @samp{+=}, like this:
4507 @example
4508 objects += another.o
4509 @end example
4511 @noindent
4512 This takes the value of the variable @code{objects}, and adds the text
4513 @samp{another.o} to it (preceded by a single space).  Thus:
4515 @example
4516 objects = main.o foo.o bar.o utils.o
4517 objects += another.o
4518 @end example
4520 @noindent
4521 sets @code{objects} to @samp{main.o foo.o bar.o utils.o another.o}.
4523 Using @samp{+=} is similar to:
4525 @example
4526 objects = main.o foo.o bar.o utils.o
4527 objects := $(objects) another.o
4528 @end example
4530 @noindent
4531 but differs in ways that become important when you use more complex values.
4533 When the variable in question has not been defined before, @samp{+=}
4534 acts just like normal @samp{=}: it defines a recursively-expanded
4535 variable.  However, when there @emph{is} a previous definition, exactly
4536 what @samp{+=} does depends on what flavor of variable you defined
4537 originally.  @xref{Flavors, ,The Two Flavors of Variables}, for an
4538 explanation of the two flavors of variables.
4540 When you add to a variable's value with @samp{+=}, @code{make} acts
4541 essentially as if you had included the extra text in the initial
4542 definition of the variable.  If you defined it first with @samp{:=},
4543 making it a simply-expanded variable, @samp{+=} adds to that
4544 simply-expanded definition, and expands the new text before appending it
4545 to the old value just as @samp{:=} does
4546 (@pxref{Setting, ,Setting Variables}, for a full explanation of @samp{:=}).
4547 In fact,
4549 @example
4550 variable := value
4551 variable += more
4552 @end example
4554 @noindent
4555 is exactly equivalent to:
4557 @noindent
4558 @example
4559 variable := value
4560 variable := $(variable) more
4561 @end example
4563 On the other hand, when you use @samp{+=} with a variable that you defined
4564 first to be recursively-expanded using plain @samp{=}, @code{make} does
4565 something a bit different.  Recall that when you define a
4566 recursively-expanded variable, @code{make} does not expand the value you set
4567 for variable and function references immediately.  Instead it stores the text
4568 verbatim, and saves these variable and function references to be expanded
4569 later, when you refer to the new variable (@pxref{Flavors, ,The Two Flavors
4570 of Variables}).  When you use @samp{+=} on a recursively-expanded variable,
4571 it is this unexpanded text to which @code{make} appends the new text you
4572 specify.
4574 @example
4575 @group
4576 variable = value
4577 variable += more
4578 @end group
4579 @end example
4581 @noindent
4582 is roughly equivalent to:
4584 @example
4585 @group
4586 temp = value
4587 variable = $(temp) more
4588 @end group
4589 @end example
4591 @noindent
4592 except that of course it never defines a variable called @code{temp}.
4593 The importance of this comes when the variable's old value contains
4594 variable references.  Take this common example:
4596 @example
4597 CFLAGS = $(includes) -O
4598 @dots{}
4599 CFLAGS += -pg # enable profiling
4600 @end example
4602 @noindent
4603 The first line defines the @code{CFLAGS} variable with a reference to another
4604 variable, @code{includes}.  (@code{CFLAGS} is used by the rules for C
4605 compilation; @pxref{Catalogue of Rules, ,Catalogue of Implicit Rules}.)
4606 Using @samp{=} for the definition makes @code{CFLAGS} a recursively-expanded
4607 variable, meaning @w{@samp{$(includes) -O}} is @emph{not} expanded when
4608 @code{make} processes the definition of @code{CFLAGS}.  Thus, @code{includes}
4609 need not be defined yet for its value to take effect.  It only has to be
4610 defined before any reference to @code{CFLAGS}.  If we tried to append to the
4611 value of @code{CFLAGS} without using @samp{+=}, we might do it like this:
4613 @example
4614 CFLAGS := $(CFLAGS) -pg # enable profiling
4615 @end example
4617 @noindent
4618 This is pretty close, but not quite what we want.  Using @samp{:=}
4619 redefines @code{CFLAGS} as a simply-expanded variable; this means
4620 @code{make} expands the text @w{@samp{$(CFLAGS) -pg}} before setting the
4621 variable.  If @code{includes} is not yet defined, we get @w{@samp{ -O
4622 -pg}}, and a later definition of @code{includes} will have no effect.
4623 Conversely, by using @samp{+=} we set @code{CFLAGS} to the
4624 @emph{unexpanded} value @w{@samp{$(includes) -O -pg}}.  Thus we preserve
4625 the reference to @code{includes}, so if that variable gets defined at
4626 any later point, a reference like @samp{$(CFLAGS)} still uses its
4627 value.
4629 @node Override Directive, Defining, Appending, Using Variables
4630 @section The @code{override} Directive
4631 @findex override
4632 @cindex overriding with @code{override}
4633 @cindex variables, overriding
4635 If a variable has been set with a command argument
4636 (@pxref{Overriding, ,Overriding Variables}),
4637 then ordinary assignments in the makefile are ignored.  If you want to set
4638 the variable in the makefile even though it was set with a command
4639 argument, you can use an @code{override} directive, which is a line that
4640 looks like this:@refill
4642 @example
4643 override @var{variable} = @var{value}
4644 @end example
4646 @noindent
4649 @example
4650 override @var{variable} := @var{value}
4651 @end example
4653 To append more text to a variable defined on the command line, use:
4655 @example
4656 override @var{variable} += @var{more text}
4657 @end example
4659 @noindent
4660 @xref{Appending, ,Appending More Text to Variables}.
4662 The @code{override} directive was not invented for escalation in the war
4663 between makefiles and command arguments.  It was invented so you can alter
4664 and add to values that the user specifies with command arguments.
4666 For example, suppose you always want the @samp{-g} switch when you run the
4667 C compiler, but you would like to allow the user to specify the other
4668 switches with a command argument just as usual.  You could use this
4669 @code{override} directive:
4671 @example
4672 override CFLAGS += -g
4673 @end example
4675 You can also use @code{override} directives with @code{define} directives.
4676 This is done as you might expect:
4678 @example
4679 override define foo
4681 endef
4682 @end example
4684 @noindent
4685 @iftex
4686 See the next section for information about @code{define}.
4687 @end iftex
4688 @ifinfo
4689 @xref{Defining, ,Defining Variables Verbatim}.
4690 @end ifinfo
4692 @node Defining, Environment, Override Directive, Using Variables
4693 @section Defining Variables Verbatim
4694 @findex define
4695 @findex endef
4696 @cindex verbatim variable definition
4697 @cindex defining variables verbatim
4698 @cindex variables, defining verbatim
4700 Another way to set the value of a variable is to use the @code{define}
4701 directive.  This directive has an unusual syntax which allows newline
4702 characters to be included in the value, which is convenient for defining
4703 canned sequences of commands
4704 (@pxref{Sequences, ,Defining Canned Command Sequences}).
4706 The @code{define} directive is followed on the same line by the name of the
4707 variable and nothing more.  The value to give the variable appears on the
4708 following lines.  The end of the value is marked by a line containing just
4709 the word @code{endef}.  Aside from this difference in syntax, @code{define}
4710 works just like @samp{=}: it creates a recursively-expanded variable
4711 (@pxref{Flavors, ,The Two Flavors of Variables}).
4712 The variable name may contain function and variable references, which
4713 are expanded when the directive is read to find the actual variable name
4714 to use.
4716 @example
4717 define two-lines
4718 echo foo
4719 echo $(bar)
4720 endef
4721 @end example
4723 The value in an ordinary assignment cannot contain a newline; but the
4724 newlines that separate the lines of the value in a @code{define} become
4725 part of the variable's value (except for the final newline which precedes
4726 the @code{endef} and is not considered part of the value).@refill
4728 @need 800
4729 The previous example is functionally equivalent to this:
4731 @example
4732 two-lines = echo foo; echo $(bar)
4733 @end example
4735 @noindent
4736 since two commands separated by semicolon behave much like two separate
4737 shell commands.  However, note that using two separate lines means
4738 @code{make} will invoke the shell twice, running an independent subshell
4739 for each line.  @xref{Execution, ,Command Execution}.
4741 If you want variable definitions made with @code{define} to take
4742 precedence over command-line variable definitions, you can use the
4743 @code{override} directive together with @code{define}:
4745 @example
4746 override define two-lines
4748 $(bar)
4749 endef
4750 @end example
4752 @noindent
4753 @xref{Override Directive, ,The @code{override} Directive}.
4755 @node Environment, Target-specific, Defining, Using Variables
4756 @section Variables from the Environment
4758 @cindex variables, environment
4759 @cindex environment
4760 Variables in @code{make} can come from the environment in which
4761 @code{make} is run.  Every environment variable that @code{make} sees when
4762 it starts up is transformed into a @code{make} variable with the same name
4763 and value.  But an explicit assignment in the makefile, or with a command
4764 argument, overrides the environment.  (If the @samp{-e} flag is specified,
4765 then values from the environment override assignments in the makefile.
4766 @xref{Options Summary, ,Summary of Options}.
4767 But this is not recommended practice.)
4769 Thus, by setting the variable @code{CFLAGS} in your environment, you can
4770 cause all C compilations in most makefiles to use the compiler switches you
4771 prefer.  This is safe for variables with standard or conventional meanings
4772 because you know that no makefile will use them for other things.  (But
4773 this is not totally reliable; some makefiles set @code{CFLAGS} explicitly
4774 and therefore are not affected by the value in the environment.)
4776 When @code{make} is invoked recursively, variables defined in the
4777 outer invocation can be passed to inner invocations through the
4778 environment (@pxref{Recursion, ,Recursive Use of @code{make}}).  By
4779 default, only variables that came from the environment or the command
4780 line are passed to recursive invocations.  You can use the
4781 @code{export} directive to pass other variables.
4782 @xref{Variables/Recursion, , Communicating Variables to a
4783 Sub-@code{make}}, for full details.
4785 Other use of variables from the environment is not recommended.  It is not
4786 wise for makefiles to depend for their functioning on environment variables
4787 set up outside their control, since this would cause different users to get
4788 different results from the same makefile.  This is against the whole
4789 purpose of most makefiles.
4791 Such problems would be especially likely with the variable @code{SHELL},
4792 which is normally present in the environment to specify the user's choice
4793 of interactive shell.  It would be very undesirable for this choice to
4794 affect @code{make}.  So @code{make} ignores the environment value of
4795 @code{SHELL} (except on MS-DOS and MS-Windows, where @code{SHELL} is
4796 usually not set.  @xref{Execution, ,Special handling of SHELL on
4797 MS-DOS}.)@refill
4799 @node Target-specific, Pattern-specific, Environment, Using Variables
4800 @section Target-specific Variable Values
4801 @cindex target-specific variables
4802 @cindex variables, target-specific
4804 Variable values in @code{make} are usually global; that is, they are the
4805 same regardless of where they are evaluated (unless they're reset, of
4806 course).  One exception to that is automatic variables
4807 (@pxref{Automatic, ,Automatic Variables}).
4809 The other exception is @dfn{target-specific variable values}.  This
4810 feature allows you to define different values for the same variable,
4811 based on the target that @code{make} is currently building.  As with
4812 automatic variables, these values are only available within the context
4813 of a target's command script (and in other target-specific assignments).
4815 Set a target-specific variable value like this:
4817 @example
4818 @var{target} @dots{} : @var{variable-assignment}
4819 @end example
4821 @noindent
4822 or like this:
4824 @example
4825 @var{target} @dots{} : override @var{variable-assignment}
4826 @end example
4828 Multiple @var{target} values create a target-specific variable value for
4829 each member of the target list individually.
4831 The @var{variable-assignment} can be any valid form of assignment;
4832 recursive (@samp{=}), static (@samp{:=}), appending (@samp{+=}), or
4833 conditional (@samp{?=}).  All variables that appear within the
4834 @var{variable-assignment} are evaluated within the context of the
4835 target: thus, any previously-defined target-specific variable values
4836 will be in effect.  Note that this variable is actually distinct from
4837 any ``global'' value: the two variables do not have to have the same
4838 flavor (recursive vs. static).
4840 Target-specific variables have the same priority as any other makefile
4841 variable.  Variables provided on the command-line (and in the
4842 environment if the @samp{-e} option is in force) will take precedence.
4843 Specifying the @code{override} directive will allow the target-specific
4844 variable value to be preferred.
4846 There is one more special feature of target-specific variables: when you
4847 define a target-specific variable, that variable value is also in effect
4848 for all dependencies of this target (unless those dependencies override
4849 it with their own target-specific variable value).  So, for example, a
4850 statement like this:
4852 @example
4853 prog : CFLAGS = -g
4854 prog : prog.o foo.o bar.o
4855 @end example
4857 @noindent
4858 will set @code{CFLAGS} to @samp{-g} in the command script for
4859 @file{prog}, but it will also set @code{CFLAGS} to @samp{-g} in the
4860 command scripts that create @file{prog.o}, @file{foo.o}, and
4861 @file{bar.o}, and any command scripts which create their dependencies.
4863 @node Pattern-specific,  , Target-specific, Using Variables
4864 @section Pattern-specific Variable Values
4865 @cindex pattern-specific variables
4866 @cindex variables, pattern-specific
4868 In addition to target-specific variable values (@pxref{Target-specific,
4869 ,Target-specific Variable Values}), GNU @code{make} supports
4870 pattern-specific variable values.  In this form, a variable is defined
4871 for any target that matches the pattern specified.  Variables defined in
4872 this way are searched after any target-specific variables defined
4873 explicitly for that target, and before target-specific variables defined
4874 for the parent target.
4876 Set a pattern-specific variable value like this:
4878 @example
4879 @var{pattern} @dots{} : @var{variable-assignment}
4880 @end example
4882 @noindent
4883 or like this:
4885 @example
4886 @var{pattern} @dots{} : override @var{variable-assignment}
4887 @end example
4889 @noindent
4890 where @var{pattern} is a %-pattern.  As with target-specific variable
4891 values, multiple @var{pattern} values create a pattern-specific variable
4892 value for each pattern individually.  The @var{variable-assignment} can
4893 be any valid form of assignment.  Any command-line variable setting will
4894 take precedence, unless @code{override} is specified.
4896 For example:
4898 @example
4899 %.o : CFLAGS = -O
4900 @end example
4902 @noindent
4903 will assign @code{CFLAGS} the value of @samp{-O} for all targets
4904 matching the pattern @code{%.o}.
4906 @node Conditionals, Functions, Using Variables, Top
4907 @chapter Conditional Parts of Makefiles
4909 @cindex conditionals
4910 A @dfn{conditional} causes part of a makefile to be obeyed or ignored
4911 depending on the values of variables.  Conditionals can compare the
4912 value of one variable to another, or the value of a variable to
4913 a constant string.  Conditionals control what @code{make} actually
4914 ``sees'' in the makefile, so they @emph{cannot} be used to control shell
4915 commands at the time of execution.@refill
4917 @menu
4918 * Conditional Example::         Example of a conditional
4919 * Conditional Syntax::          The syntax of conditionals.
4920 * Testing Flags::               Conditionals that test flags.
4921 @end menu
4923 @node Conditional Example, Conditional Syntax,  , Conditionals
4924 @section Example of a Conditional
4926 The following example of a conditional tells @code{make} to use one set
4927 of libraries if the @code{CC} variable is @samp{gcc}, and a different
4928 set of libraries otherwise.  It works by controlling which of two
4929 command lines will be used as the command for a rule.  The result is
4930 that @samp{CC=gcc} as an argument to @code{make} changes not only which
4931 compiler is used but also which libraries are linked.
4933 @example
4934 libs_for_gcc = -lgnu
4935 normal_libs =
4937 foo: $(objects)
4938 ifeq ($(CC),gcc)
4939         $(CC) -o foo $(objects) $(libs_for_gcc)
4940 else
4941         $(CC) -o foo $(objects) $(normal_libs)
4942 endif
4943 @end example
4945 This conditional uses three directives: one @code{ifeq}, one @code{else}
4946 and one @code{endif}.
4948 The @code{ifeq} directive begins the conditional, and specifies the
4949 condition.  It contains two arguments, separated by a comma and surrounded
4950 by parentheses.  Variable substitution is performed on both arguments and
4951 then they are compared.  The lines of the makefile following the
4952 @code{ifeq} are obeyed if the two arguments match; otherwise they are
4953 ignored.
4955 The @code{else} directive causes the following lines to be obeyed if the
4956 previous conditional failed.  In the example above, this means that the
4957 second alternative linking command is used whenever the first alternative
4958 is not used.  It is optional to have an @code{else} in a conditional.
4960 The @code{endif} directive ends the conditional.  Every conditional must
4961 end with an @code{endif}.  Unconditional makefile text follows.
4963 As this example illustrates, conditionals work at the textual level:
4964 the lines of the conditional are treated as part of the makefile, or
4965 ignored, according to the condition.  This is why the larger syntactic
4966 units of the makefile, such as rules, may cross the beginning or the
4967 end of the conditional.
4969 When the variable @code{CC} has the value @samp{gcc}, the above example has
4970 this effect:
4972 @example
4973 foo: $(objects)
4974         $(CC) -o foo $(objects) $(libs_for_gcc)
4975 @end example
4977 @noindent
4978 When the variable @code{CC} has any other value, the effect is this:
4980 @example
4981 foo: $(objects)
4982         $(CC) -o foo $(objects) $(normal_libs)
4983 @end example
4985 Equivalent results can be obtained in another way by conditionalizing a
4986 variable assignment and then using the variable unconditionally:
4988 @example
4989 libs_for_gcc = -lgnu
4990 normal_libs =
4992 ifeq ($(CC),gcc)
4993   libs=$(libs_for_gcc)
4994 else
4995   libs=$(normal_libs)
4996 endif
4998 foo: $(objects)
4999         $(CC) -o foo $(objects) $(libs)
5000 @end example
5002 @node Conditional Syntax, Testing Flags, Conditional Example, Conditionals
5003 @section Syntax of Conditionals
5004 @findex ifdef
5005 @findex ifeq
5006 @findex ifndef
5007 @findex ifneq
5008 @findex else
5009 @findex endif
5011 The syntax of a simple conditional with no @code{else} is as follows:
5013 @example
5014 @var{conditional-directive}
5015 @var{text-if-true}
5016 endif
5017 @end example
5019 @noindent
5020 The @var{text-if-true} may be any lines of text, to be considered as part
5021 of the makefile if the condition is true.  If the condition is false, no
5022 text is used instead.
5024 The syntax of a complex conditional is as follows:
5026 @example
5027 @var{conditional-directive}
5028 @var{text-if-true}
5029 else
5030 @var{text-if-false}
5031 endif
5032 @end example
5034 @noindent
5035 If the condition is true, @var{text-if-true} is used; otherwise,
5036 @var{text-if-false} is used instead.  The @var{text-if-false} can be any
5037 number of lines of text.
5039 The syntax of the @var{conditional-directive} is the same whether the
5040 conditional is simple or complex.  There are four different directives that
5041 test different conditions.  Here is a table of them:
5043 @table @code
5044 @item ifeq (@var{arg1}, @var{arg2})
5045 @itemx ifeq '@var{arg1}' '@var{arg2}'
5046 @itemx ifeq "@var{arg1}" "@var{arg2}"
5047 @itemx ifeq "@var{arg1}" '@var{arg2}'
5048 @itemx ifeq '@var{arg1}' "@var{arg2}"
5049 Expand all variable references in @var{arg1} and @var{arg2} and
5050 compare them.  If they are identical, the @var{text-if-true} is
5051 effective; otherwise, the @var{text-if-false}, if any, is effective.
5053 Often you want to test if a variable has a non-empty value.  When the
5054 value results from complex expansions of variables and functions,
5055 expansions you would consider empty may actually contain whitespace
5056 characters and thus are not seen as empty.  However, you can use the
5057 @code{strip} function (@pxref{Text Functions}) to avoid interpreting
5058 whitespace as a non-empty value.  For example:
5060 @example
5061 @group
5062 ifeq ($(strip $(foo)),)
5063 @var{text-if-empty}
5064 endif
5065 @end group
5066 @end example
5068 @noindent
5069 will evaluate @var{text-if-empty} even if the expansion of
5070 @code{$(foo)} contains whitespace characters.
5072 @item ifneq (@var{arg1}, @var{arg2})
5073 @itemx ifneq '@var{arg1}' '@var{arg2}'
5074 @itemx ifneq "@var{arg1}" "@var{arg2}"
5075 @itemx ifneq "@var{arg1}" '@var{arg2}'
5076 @itemx ifneq '@var{arg1}' "@var{arg2}"
5077 Expand all variable references in @var{arg1} and @var{arg2} and
5078 compare them.  If they are different, the @var{text-if-true} is
5079 effective; otherwise, the @var{text-if-false}, if any, is effective.
5081 @item ifdef @var{variable-name}
5082 If the variable @var{variable-name} has a non-empty value, the
5083 @var{text-if-true} is effective; otherwise, the @var{text-if-false},
5084 if any, is effective.  Variables that have never been defined have an
5085 empty value.
5087 Note that @code{ifdef} only tests whether a variable has a value.  It
5088 does not expand the variable to see if that value is nonempty.
5089 Consequently, tests using @code{ifdef} return true for all definitions
5090 except those like @code{foo =}.  To test for an empty value, use
5091 @w{@code{ifeq ($(foo),)}}.  For example,
5093 @example
5094 bar =
5095 foo = $(bar)
5096 ifdef foo
5097 frobozz = yes
5098 else
5099 frobozz = no
5100 endif
5101 @end example
5103 @noindent
5104 sets @samp{frobozz} to @samp{yes}, while:
5106 @example
5107 foo =
5108 ifdef foo
5109 frobozz = yes
5110 else
5111 frobozz = no
5112 endif
5113 @end example
5115 @noindent
5116 sets @samp{frobozz} to @samp{no}.
5118 @item ifndef @var{variable-name}
5119 If the variable @var{variable-name} has an empty value, the
5120 @var{text-if-true} is effective; otherwise, the @var{text-if-false},
5121 if any, is effective.
5122 @end table
5124 Extra spaces are allowed and ignored at the beginning of the conditional
5125 directive line, but a tab is not allowed.  (If the line begins with a tab,
5126 it will be considered a command for a rule.)  Aside from this, extra spaces
5127 or tabs may be inserted with no effect anywhere except within the directive
5128 name or within an argument.  A comment starting with @samp{#} may appear at
5129 the end of the line.
5131 The other two directives that play a part in a conditional are @code{else}
5132 and @code{endif}.  Each of these directives is written as one word, with no
5133 arguments.  Extra spaces are allowed and ignored at the beginning of the
5134 line, and spaces or tabs at the end.  A comment starting with @samp{#} may
5135 appear at the end of the line.
5137 Conditionals affect which lines of the makefile @code{make} uses.  If
5138 the condition is true, @code{make} reads the lines of the
5139 @var{text-if-true} as part of the makefile; if the condition is false,
5140 @code{make} ignores those lines completely.  It follows that syntactic
5141 units of the makefile, such as rules, may safely be split across the
5142 beginning or the end of the conditional.@refill
5144 @code{make} evaluates conditionals when it reads a makefile.
5145 Consequently, you cannot use automatic variables in the tests of
5146 conditionals because they are not defined until commands are run
5147 (@pxref{Automatic, , Automatic Variables}).
5149 To prevent intolerable confusion, it is not permitted to start a
5150 conditional in one makefile and end it in another.  However, you may
5151 write an @code{include} directive within a conditional, provided you do
5152 not attempt to terminate the conditional inside the included file.
5154 @node Testing Flags,  , Conditional Syntax, Conditionals
5155 @section Conditionals that Test Flags
5157 You can write a conditional that tests @code{make} command flags such as
5158 @samp{-t} by using the variable @code{MAKEFLAGS} together with the
5159 @code{findstring} function
5160 (@pxref{Text Functions, , Functions for String Substitution and Analysis}).
5161 This is useful when @code{touch} is not enough to make a file appear up
5162 to date.
5164 The @code{findstring} function determines whether one string appears as a
5165 substring of another.  If you want to test for the @samp{-t} flag,
5166 use @samp{t} as the first string and the value of @code{MAKEFLAGS} as
5167 the other.
5169 For example, here is how to arrange to use @samp{ranlib -t} to finish
5170 marking an archive file up to date:
5172 @example
5173 archive.a: @dots{}
5174 ifneq (,$(findstring t,$(MAKEFLAGS)))
5175         +touch archive.a
5176         +ranlib -t archive.a
5177 else
5178         ranlib archive.a
5179 endif
5180 @end example
5182 @noindent
5183 The @samp{+} prefix marks those command lines as ``recursive'' so
5184 that they will be executed despite use of the @samp{-t} flag.
5185 @xref{Recursion, ,Recursive Use of @code{make}}.
5187 @node Functions, Running, Conditionals, Top
5188 @chapter Functions for Transforming Text
5189 @cindex functions
5191 @dfn{Functions} allow you to do text processing in the makefile to compute
5192 the files to operate on or the commands to use.  You use a function in a
5193 @dfn{function call}, where you give the name of the function and some text
5194 (the @dfn{arguments}) for the function to operate on.  The result of the
5195 function's processing is substituted into the makefile at the point of the
5196 call, just as a variable might be substituted.
5198 @menu
5199 * Syntax of Functions::         How to write a function call.
5200 * Text Functions::              General-purpose text manipulation functions.
5201 * File Name Functions::         Functions for manipulating file names.
5202 * Foreach Function::            Repeat some text with controlled variation.
5203 * If Function::                 Conditionally expand a value.
5204 * Call Function::               Expand a user-defined function.
5205 * Origin Function::             Find where a variable got its value.
5206 * Shell Function::              Substitute the output of a shell command.
5207 * Make Control Functions::      Functions that control how make runs.
5208 @end menu
5210 @node Syntax of Functions, Text Functions,  , Functions
5211 @section Function Call Syntax
5212 @cindex @code{$}, in function call
5213 @cindex dollar sign (@code{$}), in function call
5214 @cindex arguments of functions
5215 @cindex functions, syntax of
5217 A function call resembles a variable reference.  It looks like this:
5219 @example
5220 $(@var{function} @var{arguments})
5221 @end example
5223 @noindent
5224 or like this:
5226 @example
5227 $@{@var{function} @var{arguments}@}
5228 @end example
5230 Here @var{function} is a function name; one of a short list of names
5231 that are part of @code{make}.  You can also essentially create your own
5232 functions by using the @code{call} builtin function.
5234 The @var{arguments} are the arguments of the function.  They are
5235 separated from the function name by one or more spaces or tabs, and if
5236 there is more than one argument, then they are separated by commas.
5237 Such whitespace and commas are not part of an argument's value.  The
5238 delimiters which you use to surround the function call, whether
5239 parentheses or braces, can appear in an argument only in matching pairs;
5240 the other kind of delimiters may appear singly.  If the arguments
5241 themselves contain other function calls or variable references, it is
5242 wisest to use the same kind of delimiters for all the references; write
5243 @w{@samp{$(subst a,b,$(x))}}, not @w{@samp{$(subst a,b,$@{x@})}}.  This
5244 is because it is clearer, and because only one type of delimiter is
5245 matched to find the end of the reference.
5247 The text written for each argument is processed by substitution of
5248 variables and function calls to produce the argument value, which
5249 is the text on which the function acts.  The substitution is done in the
5250 order in which the arguments appear.
5252 Commas and unmatched parentheses or braces cannot appear in the text of an
5253 argument as written; leading spaces cannot appear in the text of the first
5254 argument as written.  These characters can be put into the argument value
5255 by variable substitution.  First define variables @code{comma} and
5256 @code{space} whose values are isolated comma and space characters, then
5257 substitute these variables where such characters are wanted, like this:
5259 @example
5260 @group
5261 comma:= ,
5262 empty:=
5263 space:= $(empty) $(empty)
5264 foo:= a b c
5265 bar:= $(subst $(space),$(comma),$(foo))
5266 # @r{bar is now `a,b,c'.}
5267 @end group
5268 @end example
5270 @noindent
5271 Here the @code{subst} function replaces each space with a comma, through
5272 the value of @code{foo}, and substitutes the result.
5274 @node Text Functions, File Name Functions, Syntax of Functions, Functions
5275 @section Functions for String Substitution and Analysis
5276 @cindex functions, for text
5278 Here are some functions that operate on strings:
5280 @table @code
5281 @item $(subst @var{from},@var{to},@var{text})
5282 @findex subst
5283 Performs a textual replacement on the text @var{text}: each occurrence
5284 of @var{from} is replaced by @var{to}.  The result is substituted for
5285 the function call.  For example,
5287 @example
5288 $(subst ee,EE,feet on the street)
5289 @end example
5291 substitutes the string @samp{fEEt on the strEEt}.
5293 @item $(patsubst @var{pattern},@var{replacement},@var{text})
5294 @findex patsubst
5295 Finds whitespace-separated words in @var{text} that match
5296 @var{pattern} and replaces them with @var{replacement}.  Here
5297 @var{pattern} may contain a @samp{%} which acts as a wildcard,
5298 matching any number of any characters within a word.  If
5299 @var{replacement} also contains a @samp{%}, the @samp{%} is replaced
5300 by the text that matched the @samp{%} in @var{pattern}.@refill
5302 @cindex @code{%}, quoting in @code{patsubst}
5303 @cindex @code{%}, quoting with @code{\} (backslash)
5304 @cindex @code{\} (backslash), to quote @code{%}
5305 @cindex backslash (@code{\}), to quote @code{%}
5306 @cindex quoting @code{%}, in @code{patsubst}
5307 @samp{%} characters in @code{patsubst} function invocations can be
5308 quoted with preceding backslashes (@samp{\}).  Backslashes that would
5309 otherwise quote @samp{%} characters can be quoted with more backslashes.
5310 Backslashes that quote @samp{%} characters or other backslashes are
5311 removed from the pattern before it is compared file names or has a stem
5312 substituted into it.  Backslashes that are not in danger of quoting
5313 @samp{%} characters go unmolested.  For example, the pattern
5314 @file{the\%weird\\%pattern\\} has @samp{the%weird\} preceding the
5315 operative @samp{%} character, and @samp{pattern\\} following it.  The
5316 final two backslashes are left alone because they cannot affect any
5317 @samp{%} character.@refill
5319 Whitespace between words is folded into single space characters;
5320 leading and trailing whitespace is discarded.
5322 For example,
5324 @example
5325 $(patsubst %.c,%.o,x.c.c bar.c)
5326 @end example
5328 @noindent
5329 produces the value @samp{x.c.o bar.o}.
5331 Substitution references (@pxref{Substitution Refs, ,Substitution
5332 References}) are a simpler way to get the effect of the @code{patsubst}
5333 function:
5335 @example
5336 $(@var{var}:@var{pattern}=@var{replacement})
5337 @end example
5339 @noindent
5340 is equivalent to
5342 @example
5343 $(patsubst @var{pattern},@var{replacement},$(@var{var}))
5344 @end example
5346 The second shorthand simplifies one of the most common uses of
5347 @code{patsubst}: replacing the suffix at the end of file names.
5349 @example
5350 $(@var{var}:@var{suffix}=@var{replacement})
5351 @end example
5353 @noindent
5354 is equivalent to
5356 @example
5357 $(patsubst %@var{suffix},%@var{replacement},$(@var{var}))
5358 @end example
5360 @noindent
5361 For example, you might have a list of object files:
5363 @example
5364 objects = foo.o bar.o baz.o
5365 @end example
5367 @noindent
5368 To get the list of corresponding source files, you could simply write:
5370 @example
5371 $(objects:.o=.c)
5372 @end example
5374 @noindent
5375 instead of using the general form:
5377 @example
5378 $(patsubst %.o,%.c,$(objects))
5379 @end example
5381 @item $(strip @var{string})
5382 @cindex stripping whitespace
5383 @cindex whitespace, stripping
5384 @cindex spaces, stripping
5385 @findex strip
5386 Removes leading and trailing whitespace from @var{string} and replaces
5387 each internal sequence of one or more whitespace characters with a
5388 single space.  Thus, @samp{$(strip a b  c )} results in @w{@samp{a b c}}.
5390 The function @code{strip} can be very useful when used in conjunction
5391 with conditionals.  When comparing something with the empty string
5392 @samp{} using @code{ifeq} or @code{ifneq}, you usually want a string of
5393 just whitespace to match the empty string (@pxref{Conditionals}).
5395 Thus, the following may fail to have the desired results:
5397 @example
5398 .PHONY: all
5399 ifneq   "$(needs_made)" ""
5400 all: $(needs_made)
5401 else
5402 all:;@@echo 'Nothing to make!'
5403 endif
5404 @end example
5406 @noindent
5407 Replacing the variable reference @w{@samp{$(needs_made)}} with the
5408 function call @w{@samp{$(strip $(needs_made))}} in the @code{ifneq}
5409 directive would make it more robust.@refill
5411 @item $(findstring @var{find},@var{in})
5412 @findex findstring
5413 @cindex searching for strings
5414 @cindex finding strings
5415 @cindex strings, searching for
5416 Searches @var{in} for an occurrence of @var{find}.  If it occurs, the
5417 value is @var{find}; otherwise, the value is empty.  You can use this
5418 function in a conditional to test for the presence of a specific
5419 substring in a given string.  Thus, the two examples,
5421 @example
5422 $(findstring a,a b c)
5423 $(findstring a,b c)
5424 @end example
5426 @noindent
5427 produce the values @samp{a} and @samp{} (the empty string),
5428 respectively.  @xref{Testing Flags}, for a practical application of
5429 @code{findstring}.@refill
5431 @need 750
5432 @findex filter
5433 @cindex filtering words
5434 @cindex words, filtering
5435 @item $(filter @var{pattern}@dots{},@var{text})
5436 Removes all whitespace-separated words in @var{text} that do
5437 @emph{not} match any of the @var{pattern} words, returning only
5438 matching words.  The patterns are written using @samp{%}, just like
5439 the patterns used in the @code{patsubst} function above.@refill
5441 The @code{filter} function can be used to separate out different types
5442 of strings (such as file names) in a variable.  For example:
5444 @example
5445 sources := foo.c bar.c baz.s ugh.h
5446 foo: $(sources)
5447         cc $(filter %.c %.s,$(sources)) -o foo
5448 @end example
5450 @noindent
5451 says that @file{foo} depends of @file{foo.c}, @file{bar.c},
5452 @file{baz.s} and @file{ugh.h} but only @file{foo.c}, @file{bar.c} and
5453 @file{baz.s} should be specified in the command to the
5454 compiler.@refill
5456 @item $(filter-out @var{pattern}@dots{},@var{text})
5457 @findex filter-out
5458 @cindex filtering out words
5459 @cindex words, filtering out
5460 Removes all whitespace-separated words in @var{text} that @emph{do}
5461 match the @var{pattern} words, returning only the words that @emph{do
5462 not} match.  This is the exact opposite of the @code{filter}
5463 function.@refill
5465 For example, given:
5467 @example
5468 @group
5469 objects=main1.o foo.o main2.o bar.o
5470 mains=main1.o main2.o
5471 @end group
5472 @end example
5474 @noindent
5475 the following generates a list which contains all the object files not
5476 in @samp{mains}:
5478 @example
5479 $(filter-out $(mains),$(objects))
5480 @end example
5482 @need 1500
5483 @findex sort
5484 @cindex sorting words
5485 @item $(sort @var{list})
5486 Sorts the words of @var{list} in lexical order, removing duplicate
5487 words.  The output is a list of words separated by single spaces.
5488 Thus,
5490 @example
5491 $(sort foo bar lose)
5492 @end example
5494 @noindent
5495 returns the value @samp{bar foo lose}.
5497 @cindex removing duplicate words
5498 @cindex duplicate words, removing
5499 @cindex words, removing duplicates
5500 Incidentally, since @code{sort} removes duplicate words, you can use
5501 it for this purpose even if you don't care about the sort order.
5502 @end table
5504 Here is a realistic example of the use of @code{subst} and
5505 @code{patsubst}.  Suppose that a makefile uses the @code{VPATH} variable
5506 to specify a list of directories that @code{make} should search for
5507 dependency files
5508 (@pxref{General Search, , @code{VPATH} Search Path for All Dependencies}).
5509 This example shows how to
5510 tell the C compiler to search for header files in the same list of
5511 directories.@refill
5513 The value of @code{VPATH} is a list of directories separated by colons,
5514 such as @samp{src:../headers}.  First, the @code{subst} function is used to
5515 change the colons to spaces:
5517 @example
5518 $(subst :, ,$(VPATH))
5519 @end example
5521 @noindent
5522 This produces @samp{src ../headers}.  Then @code{patsubst} is used to turn
5523 each directory name into a @samp{-I} flag.  These can be added to the
5524 value of the variable @code{CFLAGS}, which is passed automatically to the C
5525 compiler, like this:
5527 @example
5528 override CFLAGS += $(patsubst %,-I%,$(subst :, ,$(VPATH)))
5529 @end example
5531 @noindent
5532 The effect is to append the text @samp{-Isrc -I../headers} to the
5533 previously given value of @code{CFLAGS}.  The @code{override} directive is
5534 used so that the new value is assigned even if the previous value of
5535 @code{CFLAGS} was specified with a command argument (@pxref{Override
5536 Directive, , The @code{override} Directive}).
5538 @node File Name Functions, Foreach Function, Text Functions, Functions
5539 @section Functions for File Names
5540 @cindex functions, for file names
5541 @cindex file name functions
5543 Several of the built-in expansion functions relate specifically to
5544 taking apart file names or lists of file names.
5546 Each of the following functions performs a specific transformation on a
5547 file name.  The argument of the function is regarded as a series of file
5548 names, separated by whitespace.  (Leading and trailing whitespace is
5549 ignored.)  Each file name in the series is transformed in the same way and
5550 the results are concatenated with single spaces between them.
5552 @table @code
5553 @item $(dir @var{names}@dots{})
5554 @findex dir
5555 @cindex directory part
5556 @cindex file name, directory part
5557 Extracts the directory-part of each file name in @var{names}.  The
5558 directory-part of the file name is everything up through (and
5559 including) the last slash in it.  If the file name contains no slash,
5560 the directory part is the string @samp{./}.  For example,
5562 @example
5563 $(dir src/foo.c hacks)
5564 @end example
5566 @noindent
5567 produces the result @samp{src/ ./}.
5569 @item $(notdir @var{names}@dots{})
5570 @findex notdir
5571 @cindex file name, nondirectory part
5572 @cindex nondirectory part
5573 Extracts all but the directory-part of each file name in @var{names}.
5574 If the file name contains no slash, it is left unchanged.  Otherwise,
5575 everything through the last slash is removed from it.
5577 A file name that ends with a slash becomes an empty string.  This is
5578 unfortunate, because it means that the result does not always have the
5579 same number of whitespace-separated file names as the argument had;
5580 but we do not see any other valid alternative.
5582 For example,
5584 @example
5585 $(notdir src/foo.c hacks)
5586 @end example
5588 @noindent
5589 produces the result @samp{foo.c hacks}.
5591 @item $(suffix @var{names}@dots{})
5592 @findex suffix
5593 @cindex suffix, function to find
5594 @cindex file name suffix
5595 Extracts the suffix of each file name in @var{names}.  If the file name
5596 contains a period, the suffix is everything starting with the last
5597 period.  Otherwise, the suffix is the empty string.  This frequently
5598 means that the result will be empty when @var{names} is not, and if
5599 @var{names} contains multiple file names, the result may contain fewer
5600 file names.
5602 For example,
5604 @example
5605 $(suffix src/foo.c src-1.0/bar.c hacks)
5606 @end example
5608 @noindent
5609 produces the result @samp{.c .c}.
5611 @item $(basename @var{names}@dots{})
5612 @findex basename
5613 @cindex basename
5614 @cindex file name, basename of
5615 Extracts all but the suffix of each file name in @var{names}.  If the
5616 file name contains a period, the basename is everything starting up to
5617 (and not including) the last period.  Periods in the directory part are
5618 ignored.  If there is no period, the basename is the entire file name.
5619 For example,
5621 @example
5622 $(basename src/foo.c src-1.0/bar hacks)
5623 @end example
5625 @noindent
5626 produces the result @samp{src/foo src-1.0/bar hacks}.
5628 @c plural convention with dots (be consistent)
5629 @item $(addsuffix @var{suffix},@var{names}@dots{})
5630 @findex addsuffix
5631 @cindex suffix, adding
5632 @cindex file name suffix, adding
5633 The argument @var{names} is regarded as a series of names, separated
5634 by whitespace; @var{suffix} is used as a unit.  The value of
5635 @var{suffix} is appended to the end of each individual name and the
5636 resulting larger names are concatenated with single spaces between
5637 them.  For example,
5639 @example
5640 $(addsuffix .c,foo bar)
5641 @end example
5643 @noindent
5644 produces the result @samp{foo.c bar.c}.
5646 @item $(addprefix @var{prefix},@var{names}@dots{})
5647 @findex addprefix
5648 @cindex prefix, adding
5649 @cindex file name prefix, adding
5650 The argument @var{names} is regarded as a series of names, separated
5651 by whitespace; @var{prefix} is used as a unit.  The value of
5652 @var{prefix} is prepended to the front of each individual name and the
5653 resulting larger names are concatenated with single spaces between
5654 them.  For example,
5656 @example
5657 $(addprefix src/,foo bar)
5658 @end example
5660 @noindent
5661 produces the result @samp{src/foo src/bar}.
5663 @item $(join @var{list1},@var{list2})
5664 @findex join
5665 @cindex joining lists of words
5666 @cindex words, joining lists
5667 Concatenates the two arguments word by word: the two first words (one
5668 from each argument) concatenated form the first word of the result, the
5669 two second words form the second word of the result, and so on.  So the
5670 @var{n}th word of the result comes from the @var{n}th word of each
5671 argument.  If one argument has more words that the other, the extra
5672 words are copied unchanged into the result.
5674 For example, @samp{$(join a b,.c .o)} produces @samp{a.c b.o}.
5676 Whitespace between the words in the lists is not preserved; it is
5677 replaced with a single space.
5679 This function can merge the results of the @code{dir} and
5680 @code{notdir} functions, to produce the original list of files which
5681 was given to those two functions.@refill
5683 @item $(word @var{n},@var{text})
5684 @findex word
5685 @cindex word, selecting a
5686 @cindex selecting a word
5687 Returns the @var{n}th word of @var{text}.  The legitimate values of
5688 @var{n} start from 1.  If @var{n} is bigger than the number of words
5689 in @var{text}, the value is empty.  For example,
5691 @example
5692 $(word 2, foo bar baz)
5693 @end example
5695 @noindent
5696 returns @samp{bar}.
5698 @item $(wordlist @var{s},@var{e},@var{text})
5699 @findex wordlist
5700 @cindex words, selecting lists of
5701 @cindex selecting word lists
5702 Returns the list of words in @var{text} starting with word @var{s} and
5703 ending with word @var{e} (inclusive).  The legitimate values of @var{s}
5704 and @var{e} start from 1.  If @var{s} is bigger than the number of words
5705 in @var{text}, the value is empty.  If @var{e} is bigger than the number
5706 of words in @var{text}, words up to the end of @var{text} are returned.
5707 If @var{s} is greater than @var{e}, @code{make} swaps them for you.  For
5708 example,
5710 @example
5711 $(wordlist 2, 3, foo bar baz)
5712 @end example
5714 @noindent
5715 returns @samp{bar baz}.
5717 @c Following item phrased to prevent overfull hbox.  --RJC 17 Jul 92
5718 @item $(words @var{text})
5719 @findex words
5720 @cindex words, finding number
5721 Returns the number of words in @var{text}.
5722 Thus, the last word of @var{text} is
5723 @w{@code{$(word $(words @var{text}),@var{text})}}.@refill
5725 @item $(firstword @var{names}@dots{})
5726 @findex firstword
5727 @cindex words, extracting first
5728 The argument @var{names} is regarded as a series of names, separated
5729 by whitespace.  The value is the first name in the series.  The rest
5730 of the names are ignored.
5732 For example,
5734 @example
5735 $(firstword foo bar)
5736 @end example
5738 @noindent
5739 produces the result @samp{foo}.  Although @code{$(firstword
5740 @var{text})} is the same as @code{$(word 1,@var{text})}, the
5741 @code{firstword} function is retained for its simplicity.@refill
5743 @item $(wildcard @var{pattern})
5744 @findex wildcard
5745 @cindex wildcard, function
5746 The argument @var{pattern} is a file name pattern, typically containing
5747 wildcard characters (as in shell file name patterns).  The result of
5748 @code{wildcard} is a space-separated list of the names of existing files
5749 that match the pattern.
5750 @xref{Wildcards, ,Using Wildcard Characters in File Names}.
5751 @end table
5753 @node Foreach Function, If Function, File Name Functions, Functions
5754 @section The @code{foreach} Function
5755 @findex foreach
5756 @cindex words, iterating over
5758 The @code{foreach} function is very different from other functions.  It
5759 causes one piece of text to be used repeatedly, each time with a different
5760 substitution performed on it.  It resembles the @code{for} command in the
5761 shell @code{sh} and the @code{foreach} command in the C-shell @code{csh}.
5763 The syntax of the @code{foreach} function is:
5765 @example
5766 $(foreach @var{var},@var{list},@var{text})
5767 @end example
5769 @noindent
5770 The first two arguments, @var{var} and @var{list}, are expanded before
5771 anything else is done; note that the last argument, @var{text}, is
5772 @strong{not} expanded at the same time.  Then for each word of the expanded
5773 value of @var{list}, the variable named by the expanded value of @var{var}
5774 is set to that word, and @var{text} is expanded.  Presumably @var{text}
5775 contains references to that variable, so its expansion will be different
5776 each time.
5778 The result is that @var{text} is expanded as many times as there are
5779 whitespace-separated words in @var{list}.  The multiple expansions of
5780 @var{text} are concatenated, with spaces between them, to make the result
5781 of @code{foreach}.
5783 This simple example sets the variable @samp{files} to the list of all files
5784 in the directories in the list @samp{dirs}:
5786 @example
5787 dirs := a b c d
5788 files := $(foreach dir,$(dirs),$(wildcard $(dir)/*))
5789 @end example
5791 Here @var{text} is @samp{$(wildcard $(dir)/*)}.  The first repetition
5792 finds the value @samp{a} for @code{dir}, so it produces the same result
5793 as @samp{$(wildcard a/*)}; the second repetition produces the result
5794 of @samp{$(wildcard b/*)}; and the third, that of @samp{$(wildcard c/*)}.
5796 This example has the same result (except for setting @samp{dirs}) as
5797 the following example:
5799 @example
5800 files := $(wildcard a/* b/* c/* d/*)
5801 @end example
5803 When @var{text} is complicated, you can improve readability by giving it
5804 a name, with an additional variable:
5806 @example
5807 find_files = $(wildcard $(dir)/*)
5808 dirs := a b c d
5809 files := $(foreach dir,$(dirs),$(find_files))
5810 @end example
5812 @noindent
5813 Here we use the variable @code{find_files} this way.  We use plain @samp{=}
5814 to define a recursively-expanding variable, so that its value contains an
5815 actual function call to be reexpanded under the control of @code{foreach};
5816 a simply-expanded variable would not do, since @code{wildcard} would be
5817 called only once at the time of defining @code{find_files}.
5819 The @code{foreach} function has no permanent effect on the variable
5820 @var{var}; its value and flavor after the @code{foreach} function call are
5821 the same as they were beforehand.  The other values which are taken from
5822 @var{list} are in effect only temporarily, during the execution of
5823 @code{foreach}.  The variable @var{var} is a simply-expanded variable
5824 during the execution of @code{foreach}.  If @var{var} was undefined
5825 before the @code{foreach} function call, it is undefined after the call.
5826 @xref{Flavors, ,The Two Flavors of Variables}.@refill
5828 You must take care when using complex variable expressions that result in
5829 variable names because many strange things are valid variable names, but
5830 are probably not what you intended.  For example,
5832 @smallexample
5833 files := $(foreach Esta escrito en espanol!,b c ch,$(find_files))
5834 @end smallexample
5836 @noindent
5837 might be useful if the value of @code{find_files} references the variable
5838 whose name is @samp{Esta escrito en espanol!} (es un nombre bastante largo,
5839 no?), but it is more likely to be a mistake.
5841 @node If Function, Call Function, Foreach Function, Functions
5842 @section The @code{if} Function
5843 @findex if
5844 @cindex conditional expansion
5846 The @code{if} function provides support for conditional expansion in a
5847 functional context (as opposed to the GNU @code{make} makefile
5848 conditionals such as @code{ifeq} (@pxref{Conditional Syntax, ,Syntax of
5849 Conditionals}).
5851 An @code{if} function call can contain either two or three arguments:
5853 @example
5854 $(if @var{condition},@var{then-part}[,@var{else-part}])
5855 @end example
5857 The first argument, @var{condition}, first has all preceding and
5858 trailing whitespace stripped, then is expanded.  If it expands to any
5859 non-empty string, then the condition is considered to be true.  If it
5860 expands to an empty string, the condition is considered to be false.
5862 If the condition is true then the second argument, @var{then-part}, is
5863 evaluated and this is used as the result of the evaluation of the entire
5864 @code{if} function.
5866 If the condition is false then the third argument, @var{else-part}, is
5867 evaluated and this is the result of the @code{if} function.  If there is
5868 no third argument, the @code{if} function evaluates to nothing (the
5869 empty string).
5871 Note that only one of the @var{then-part} or the @var{else-part} will be
5872 evaluated, never both.  Thus, either can contain side-effects (such as
5873 @code{shell} function calls, etc.)
5875 @node Call Function, Origin Function, If Function, Functions
5876 @section The @code{call} Function
5877 @findex call
5878 @cindex functions, user defined
5879 @cindex user defined functions
5881 The @code{call} function is unique in that it can be used to create new
5882 parameterized functions.  You can write a complex expression as the
5883 value of a variable, then use @code{call} to expand it with different
5884 values.
5886 The syntax of the @code{call} function is:
5888 @example
5889 $(call @var{variable},@var{param},@var{param},@dots{})
5890 @end example
5892 When @code{make} expands this function, it assigns each @var{param} to
5893 temporary variables @var{$(1)}, @var{$(2)}, etc.  The variable
5894 @var{$(0)} will contain @var{variable}.  There is no maximum number of
5895 parameter arguments.  There is no minimum, either, but it doesn't make
5896 sense to use @code{call} with no parameters.
5898 Then @var{variable} is expanded as a @code{make} variable in the context
5899 of these temporary assignments.  Thus, any reference to @var{$(1)} in
5900 the value of @var{variable} will resolve to the first @var{param} in the
5901 invocation of @code{call}.
5903 Note that @var{variable} is the @emph{name} of a variable, not a
5904 @emph{reference} to that variable.  Therefore you would not normally use
5905 a @samp{$} or parentheses when writing it.  (You can, however, use a
5906 variable reference in the name if you want the name not to be a
5907 constant.)
5909 If @var{variable} is the name of a builtin function, the builtin function
5910 is always invoked (even if a @code{make} variable by that name also
5911 exists).
5913 Some examples may make this clearer.
5915 This macro simply reverses its arguments:
5917 @smallexample
5918 reverse = $2 $1
5920 foo = a b
5921 bar = $(call reverse,$(foo))
5922 @end smallexample
5924 @noindent
5925 Here @var{bar} will contain @samp{b a}.
5927 This one is slightly more interesting: it defines a macro to search for
5928 the first instance of a program in @code{PATH}:
5930 @smallexample
5931 pathsearch = $(firstword $(wildcard $(addsufix /$1,$(subst :, ,$(PATH)))))
5933 LS := $(call pathsearch,ls)
5934 @end smallexample
5936 @noindent
5937 Now the variable LS contains @code{/bin/ls} or similar.
5939 The @code{call} function can be nested.  Each recursive invocation gets
5940 its own local values for @var{$(1)}, etc. that mask the values of
5941 higher-level @code{call}.  For example, here is an implementation of a
5942 @dfn{map} function:
5944 @smallexample
5945 map = $(foreach a,$2,$(call $1,$a))
5946 @end smallexample
5948 Now you can @var{map} a function that normally takes only one argument,
5949 such as @code{origin}, to multiple values in one step:
5951 @smallexample
5952 o = $(call map,origin,o map MAKE)
5953 @end smallexample
5955 and end up with @var{o} containing something like @samp{file file default}.
5957 A final caution: be careful when adding whitespace to the arguments to
5958 @code{call}.  As with other functions, any whitespace contained in the
5959 second and subsequent arguments is kept; this can cause strange
5960 effects.  It's generally safest to remove all extraneous whitespace when
5961 providing parameters to @code{call}.
5964 @node Origin Function, Shell Function, Call Function, Functions
5965 @section The @code{origin} Function
5966 @findex origin
5967 @cindex variables, origin of
5968 @cindex origin of variable
5970 The @code{origin} function is unlike most other functions in that it does
5971 not operate on the values of variables; it tells you something @emph{about}
5972 a variable.  Specifically, it tells you where it came from.
5974 The syntax of the @code{origin} function is:
5976 @example
5977 $(origin @var{variable})
5978 @end example
5980 Note that @var{variable} is the @emph{name} of a variable to inquire about;
5981 not a @emph{reference} to that variable.  Therefore you would not normally
5982 use a @samp{$} or parentheses when writing it.  (You can, however, use a
5983 variable reference in the name if you want the name not to be a constant.)
5985 The result of this function is a string telling you how the variable
5986 @var{variable} was defined:
5988 @table @samp
5989 @item undefined
5991 if @var{variable} was never defined.
5993 @item default
5995 if @var{variable} has a default definition, as is usual with @code{CC}
5996 and so on.  @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
5997 Note that if you have redefined a default variable, the @code{origin}
5998 function will return the origin of the later definition.
6000 @item environment
6002 if @var{variable} was defined as an environment variable and the
6003 @samp{-e} option is @emph{not} turned on (@pxref{Options Summary, ,Summary of Options}).
6005 @item environment override
6007 if @var{variable} was defined as an environment variable and the
6008 @w{@samp{-e}} option @emph{is} turned on (@pxref{Options Summary,
6009 ,Summary of Options}).@refill
6011 @item file
6013 if @var{variable} was defined in a makefile.
6015 @item command line
6017 if @var{variable} was defined on the command line.
6019 @item override
6021 if @var{variable} was defined with an @code{override} directive in a
6022 makefile (@pxref{Override Directive, ,The @code{override} Directive}).
6024 @item automatic
6026 if @var{variable} is an automatic variable defined for the
6027 execution of the commands for each rule
6028 (@pxref{Automatic, , Automatic Variables}).
6029 @end table
6031 This information is primarily useful (other than for your curiosity) to
6032 determine if you want to believe the value of a variable.  For example,
6033 suppose you have a makefile @file{foo} that includes another makefile
6034 @file{bar}.  You want a variable @code{bletch} to be defined in @file{bar}
6035 if you run the command @w{@samp{make -f bar}}, even if the environment contains
6036 a definition of @code{bletch}.  However, if @file{foo} defined
6037 @code{bletch} before including @file{bar}, you do not want to override that
6038 definition.  This could be done by using an @code{override} directive in
6039 @file{foo}, giving that definition precedence over the later definition in
6040 @file{bar}; unfortunately, the @code{override} directive would also
6041 override any command line definitions.  So, @file{bar} could
6042 include:@refill
6044 @example
6045 @group
6046 ifdef bletch
6047 ifeq "$(origin bletch)" "environment"
6048 bletch = barf, gag, etc.
6049 endif
6050 endif
6051 @end group
6052 @end example
6054 @noindent
6055 If @code{bletch} has been defined from the environment, this will redefine
6058 If you want to override a previous definition of @code{bletch} if it came
6059 from the environment, even under @samp{-e}, you could instead write:
6061 @example
6062 @group
6063 ifneq "$(findstring environment,$(origin bletch))" ""
6064 bletch = barf, gag, etc.
6065 endif
6066 @end group
6067 @end example
6069 Here the redefinition takes place if @samp{$(origin bletch)} returns either
6070 @samp{environment} or @samp{environment override}.
6071 @xref{Text Functions, , Functions for String Substitution and Analysis}.
6073 @node Shell Function, Make Control Functions, Origin Function, Functions
6074 @section The @code{shell} Function
6075 @findex shell
6076 @cindex commands, expansion
6077 @cindex backquotes
6078 @cindex shell command, function for
6080 The @code{shell} function is unlike any other function except the
6081 @code{wildcard} function
6082 (@pxref{Wildcard Function, ,The Function @code{wildcard}}) in that it
6083 communicates with the world outside of @code{make}.
6085 The @code{shell} function performs the same function that backquotes
6086 (@samp{`}) perform in most shells: it does @dfn{command expansion}.  This
6087 means that it takes an argument that is a shell command and returns the
6088 output of the command.  The only processing @code{make} does on the result,
6089 before substituting it into the surrounding text, is to convert each
6090 newline or carriage-return / newline pair to a single space.  It also
6091 removes the trailing (carriage-return and) newline, if it's the last
6092 thing in the result.@refill
6094 The commands run by calls to the @code{shell} function are run when the
6095 function calls are expanded.  In most cases, this is when the makefile is
6096 read in.  The exception is that function calls in the commands of the rules
6097 are expanded when the commands are run, and this applies to @code{shell}
6098 function calls like all others.
6100 Here are some examples of the use of the @code{shell} function:
6102 @example
6103 contents := $(shell cat foo)
6104 @end example
6106 @noindent
6107 sets @code{contents} to the contents of the file @file{foo}, with a space
6108 (rather than a newline) separating each line.
6110 @example
6111 files := $(shell echo *.c)
6112 @end example
6114 @noindent
6115 sets @code{files} to the expansion of @samp{*.c}.  Unless @code{make} is
6116 using a very strange shell, this has the same result as
6117 @w{@samp{$(wildcard *.c)}}.@refill
6119 @node Make Control Functions,  , Shell Function, Functions
6120 @section Functions That Control Make
6121 @cindex functions, for controlling make
6122 @cindex controlling make
6124 These functions control the way make runs.  Generally, they are used to
6125 provide information to the user of the makefile or to cause make to stop
6126 if some sort of environmental error is detected.
6128 @table @code
6129 @item $(error @var{text}@dots{})
6130 @findex error
6131 @cindex error, stopping on
6132 @cindex stopping make
6133 Generates a fatal error where the message is @var{text}.  Note that the
6134 error is generated whenever this function is evaluated.  So, if you put
6135 it inside a command script or on the right side of a recursive variable
6136 assignment, it won't be evaluated until later.  The @var{text} will be
6137 expanded before the error is generated.
6139 For example,
6141 @example
6142 ifdef ERROR1
6143 $(error error is $(ERROR1))
6144 endif
6145 @end example
6147 @noindent
6148 will generate a fatal error during the read of the makefile if the
6149 @code{make} variable @code{ERROR1} is defined.  Or,
6151 @example
6152 ERR = $(error found an error!)
6154 .PHONY: err
6155 err: ; $(ERR)
6156 @end example
6158 @noindent
6159 will generate a fatal error while @code{make} is running, if the
6160 @code{err} target is invoked.
6162 @item $(warning @var{text}@dots{})
6163 @findex warning
6164 @cindex warnings, printing
6165 @cindex printing user warnings
6166 This function works similarly to the @code{error} function, above,
6167 except that @code{make} doesn't exit.  Instead, @var{text} is expanded
6168 and the resulting message is displayed, but processing of the makefile
6169 continues.
6171 The result of the expansion of this function is the empty string.
6172 @end table
6174 @node Running, Implicit Rules, Functions, Top
6175 @chapter How to Run @code{make}
6177 A makefile that says how to recompile a program can be used in more
6178 than one way.  The simplest use is to recompile every file that is out
6179 of date.  Usually, makefiles are written so that if you run
6180 @code{make} with no arguments, it does just that.
6182 But you might want to update only some of the files; you might want to use
6183 a different compiler or different compiler options; you might want just to
6184 find out which files are out of date without changing them.
6186 By giving arguments when you run @code{make}, you can do any of these
6187 things and many others.
6189 The exit status of @code{make} is always one of three values:
6190 @table @code
6191 @item 0
6192 The exit status is zero if @code{make} is successful.
6193 @item 2
6194 The exit status is two if @code{make} encounters any errors.
6195 It will print messages describing the particular errors.
6196 @item 1
6197 The exit status is one if you use the @samp{-q} flag and @code{make}
6198 determines that some target is not already up to date.
6199 @xref{Instead of Execution, ,Instead of Executing the Commands}.
6200 @end table
6202 @menu
6203 * Makefile Arguments::          How to specify which makefile to use.
6204 * Goals::                       How to use goal arguments to specify which
6205                                   parts of the makefile to use.
6206 * Instead of Execution::        How to use mode flags to specify what
6207                                   kind of thing to do with the commands
6208                                   in the makefile other than simply
6209                                   execute them.
6210 * Avoiding Compilation::        How to avoid recompiling certain files.
6211 * Overriding::                  How to override a variable to specify
6212                                   an alternate compiler and other things.
6213 * Testing::                     How to proceed past some errors, to
6214                                   test compilation.
6215 * Options Summary::             Summary of Options
6216 @end menu
6218 @node Makefile Arguments, Goals,  , Running
6219 @section Arguments to Specify the Makefile
6220 @cindex @code{--file}
6221 @cindex @code{--makefile}
6222 @cindex @code{-f}
6224 The way to specify the name of the makefile is with the @samp{-f} or
6225 @samp{--file} option (@samp{--makefile} also works).  For example,
6226 @samp{-f altmake} says to use the file @file{altmake} as the makefile.
6228 If you use the @samp{-f} flag several times and follow each @samp{-f}
6229 with an argument, all the specified files are used jointly as
6230 makefiles.
6232 If you do not use the @samp{-f} or @samp{--file} flag, the default is
6233 to try @file{GNUmakefile}, @file{makefile}, and @file{Makefile}, in
6234 that order, and use the first of these three which exists or can be made
6235 (@pxref{Makefiles, ,Writing Makefiles}).@refill
6237 @node Goals, Instead of Execution, Makefile Arguments, Running
6238 @section Arguments to Specify the Goals
6239 @cindex goal, how to specify
6241 The @dfn{goals} are the targets that @code{make} should strive ultimately
6242 to update.  Other targets are updated as well if they appear as
6243 dependencies of goals, or dependencies of dependencies of goals, etc.
6245 By default, the goal is the first target in the makefile (not counting
6246 targets that start with a period).  Therefore, makefiles are usually
6247 written so that the first target is for compiling the entire program or
6248 programs they describe.  If the first rule in the makefile has several
6249 targets, only the first target in the rule becomes the default goal, not
6250 the whole list.
6252 You can specify a different goal or goals with arguments to @code{make}.
6253 Use the name of the goal as an argument.  If you specify several goals,
6254 @code{make} processes each of them in turn, in the order you name them.
6256 Any target in the makefile may be specified as a goal (unless it
6257 starts with @samp{-} or contains an @samp{=}, in which case it will be
6258 parsed as a switch or variable definition, respectively).  Even
6259 targets not in the makefile may be specified, if @code{make} can find
6260 implicit rules that say how to make them.
6262 @cindex @code{MAKECMDGOALS}
6263 @vindex MAKECMDGOALS
6264 @code{Make} will set the special variable @code{MAKECMDGOALS} to the
6265 list of goals you specified on the command line.  If no goals were given
6266 on the command line, this variable is empty.  Note that this variable
6267 should be used only in special circumstances.
6269 An example of appropriate use is to avoid including @file{.d} files
6270 during @code{clean} rules (@pxref{Automatic Dependencies}), so
6271 @code{make} won't create them only to immediately remove them
6272 again:@refill
6274 @example
6275 @group
6276 sources = foo.c bar.c
6278 ifneq ($(MAKECMDGOALS),clean)
6279 include $(sources:.c=.d)
6280 endif
6281 @end group
6282 @end example
6284 One use of specifying a goal is if you want to compile only a part of
6285 the program, or only one of several programs.  Specify as a goal each
6286 file that you wish to remake.  For example, consider a directory containing
6287 several programs, with a makefile that starts like this:
6289 @example
6290 .PHONY: all
6291 all: size nm ld ar as
6292 @end example
6294 If you are working on the program @code{size}, you might want to say
6295 @w{@samp{make size}} so that only the files of that program are recompiled.
6297 Another use of specifying a goal is to make files that are not normally
6298 made.  For example, there may be a file of debugging output, or a
6299 version of the program that is compiled specially for testing, which has
6300 a rule in the makefile but is not a dependency of the default goal.
6302 Another use of specifying a goal is to run the commands associated with
6303 a phony target (@pxref{Phony Targets}) or empty target (@pxref{Empty
6304 Targets, ,Empty Target Files to Record Events}).  Many makefiles contain
6305 a phony target named @file{clean} which deletes everything except source
6306 files.  Naturally, this is done only if you request it explicitly with
6307 @w{@samp{make clean}}.  Following is a list of typical phony and empty
6308 target names.  @xref{Standard Targets}, for a detailed list of all the
6309 standard target names which GNU software packages use.
6311 @table @file
6312 @item all
6313 @cindex @code{all} @r{(standard target)}
6314 Make all the top-level targets the makefile knows about.
6316 @item clean
6317 @cindex @code{clean} @r{(standard target)}
6318 Delete all files that are normally created by running @code{make}.
6320 @item mostlyclean
6321 @cindex @code{mostlyclean} @r{(standard target)}
6322 Like @samp{clean}, but may refrain from deleting a few files that people
6323 normally don't want to recompile.  For example, the @samp{mostlyclean}
6324 target for GCC does not delete @file{libgcc.a}, because recompiling it
6325 is rarely necessary and takes a lot of time.
6327 @item distclean
6328 @cindex @code{distclean} @r{(standard target)}
6329 @itemx realclean
6330 @cindex @code{realclean} @r{(standard target)}
6331 @itemx clobber
6332 @cindex @code{clobber} @r{(standard target)}
6333 Any of these targets might be defined to delete @emph{more} files than
6334 @samp{clean} does.  For example, this would delete configuration files
6335 or links that you would normally create as preparation for compilation,
6336 even if the makefile itself cannot create these files.
6338 @item install
6339 @cindex @code{install} @r{(standard target)}
6340 Copy the executable file into a directory that users typically search
6341 for commands; copy any auxiliary files that the executable uses into
6342 the directories where it will look for them.
6344 @item print
6345 @cindex @code{print} @r{(standard target)}
6346 Print listings of the source files that have changed.
6348 @item tar
6349 @cindex @code{tar} @r{(standard target)}
6350 Create a tar file of the source files.
6352 @item shar
6353 @cindex @code{shar} @r{(standard target)}
6354 Create a shell archive (shar file) of the source files.
6356 @item dist
6357 @cindex @code{dist} @r{(standard target)}
6358 Create a distribution file of the source files.  This might
6359 be a tar file, or a shar file, or a compressed version of one of the
6360 above, or even more than one of the above.
6362 @item TAGS
6363 @cindex @code{TAGS} @r{(standard target)}
6364 Update a tags table for this program.
6366 @item check
6367 @cindex @code{check} @r{(standard target)}
6368 @itemx test
6369 @cindex @code{test} @r{(standard target)}
6370 Perform self tests on the program this makefile builds.
6371 @end table
6373 @node Instead of Execution, Avoiding Compilation, Goals, Running
6374 @section Instead of Executing the Commands
6375 @cindex execution, instead of
6376 @cindex commands, instead of executing
6378 The makefile tells @code{make} how to tell whether a target is up to date,
6379 and how to update each target.  But updating the targets is not always
6380 what you want.  Certain options specify other activities for @code{make}.
6382 @comment Extra blank lines make it print better.
6383 @table @samp
6384 @item -n
6385 @itemx --just-print
6386 @itemx --dry-run
6387 @itemx --recon
6388 @cindex @code{--just-print}
6389 @cindex @code{--dry-run}
6390 @cindex @code{--recon}
6391 @cindex @code{-n}
6393 ``No-op''.  The activity is to print what commands would be used to make
6394 the targets up to date, but not actually execute them.
6396 @item -t
6397 @itemx --touch
6398 @cindex @code{--touch}
6399 @cindex touching files
6400 @cindex target, touching
6401 @cindex @code{-t}
6403 ``Touch''.  The activity is to mark the targets as up to date without
6404 actually changing them.  In other words, @code{make} pretends to compile
6405 the targets but does not really change their contents.
6407 @item -q
6408 @itemx --question
6409 @cindex @code{--question}
6410 @cindex @code{-q}
6411 @cindex question mode
6413 ``Question''.  The activity is to find out silently whether the targets
6414 are up to date already; but execute no commands in either case.  In other
6415 words, neither compilation nor output will occur.
6417 @item -W @var{file}
6418 @itemx --what-if=@var{file}
6419 @itemx --assume-new=@var{file}
6420 @itemx --new-file=@var{file}
6421 @cindex @code{--what-if}
6422 @cindex @code{-W}
6423 @cindex @code{--assume-new}
6424 @cindex @code{--new-file}
6425 @cindex what if
6426 @cindex files, assuming new
6428 ``What if''.  Each @samp{-W} flag is followed by a file name.  The given
6429 files' modification times are recorded by @code{make} as being the present
6430 time, although the actual modification times remain the same.
6431 You can use the @samp{-W} flag in conjunction with the @samp{-n} flag
6432 to see what would happen if you were to modify specific files.@refill
6433 @end table
6435 With the @samp{-n} flag, @code{make} prints the commands that it would
6436 normally execute but does not execute them.
6438 With the @samp{-t} flag, @code{make} ignores the commands in the rules
6439 and uses (in effect) the command @code{touch} for each target that needs to
6440 be remade.  The @code{touch} command is also printed, unless @samp{-s} or
6441 @code{.SILENT} is used.  For speed, @code{make} does not actually invoke
6442 the program @code{touch}.  It does the work directly.
6444 With the @samp{-q} flag, @code{make} prints nothing and executes no
6445 commands, but the exit status code it returns is zero if and only if the
6446 targets to be considered are already up to date.  If the exit status is
6447 one, then some updating needs to be done.  If @code{make} encounters an
6448 error, the exit status is two, so you can distinguish an error from a
6449 target that is not up to date.
6451 It is an error to use more than one of these three flags in the same
6452 invocation of @code{make}.
6454 The @samp{-n}, @samp{-t}, and @samp{-q} options do not affect command
6455 lines that begin with @samp{+} characters or contain the strings
6456 @samp{$(MAKE)} or @samp{$@{MAKE@}}.  Note that only the line containing
6457 the @samp{+} character or the strings @samp{$(MAKE)} or @samp{$@{MAKE@}}
6458 is run regardless of these options.  Other lines in the same rule are
6459 not run unless they too begin with @samp{+} or contain @samp{$(MAKE)} or
6460 @samp{$@{MAKE@}} (@xref{MAKE Variable, ,How the @code{MAKE} Variable Works}.)
6462 The @samp{-W} flag provides two features:
6464 @itemize @bullet
6465 @item
6466 If you also use the @samp{-n} or @samp{-q} flag, you can see what
6467 @code{make} would do if you were to modify some files.
6469 @item
6470 Without the @samp{-n} or @samp{-q} flag, when @code{make} is actually
6471 executing commands, the @samp{-W} flag can direct @code{make} to act
6472 as if some files had been modified, without actually modifying the
6473 files.@refill
6474 @end itemize
6476 Note that the options @samp{-p} and @samp{-v} allow you to obtain other
6477 information about @code{make} or about the makefiles in use
6478 (@pxref{Options Summary, ,Summary of Options}).@refill
6480 @node Avoiding Compilation, Overriding, Instead of Execution, Running
6481 @section Avoiding Recompilation of Some Files
6482 @cindex @code{-o}
6483 @cindex @code{--old-file}
6484 @cindex @code{--assume-old}
6485 @cindex files, assuming old
6486 @cindex files, avoiding recompilation of
6487 @cindex recompilation, avoiding
6489 Sometimes you may have changed a source file but you do not want to
6490 recompile all the files that depend on it.  For example, suppose you add a
6491 macro or a declaration to a header file that many other files depend on.
6492 Being conservative, @code{make} assumes that any change in the header file
6493 requires recompilation of all dependent files, but you know that they do not
6494 need to be recompiled and you would rather not waste the time waiting for
6495 them to compile.
6497 If you anticipate the problem before changing the header file, you can
6498 use the @samp{-t} flag.  This flag tells @code{make} not to run the
6499 commands in the rules, but rather to mark the target up to date by
6500 changing its last-modification date.  You would follow this procedure:
6502 @enumerate
6503 @item
6504 Use the command @samp{make} to recompile the source files that really
6505 need recompilation.
6507 @item
6508 Make the changes in the header files.
6510 @item
6511 Use the command @samp{make -t} to mark all the object files as
6512 up to date.  The next time you run @code{make}, the changes in the
6513 header files will not cause any recompilation.
6514 @end enumerate
6516 If you have already changed the header file at a time when some files
6517 do need recompilation, it is too late to do this.  Instead, you can
6518 use the @w{@samp{-o @var{file}}} flag, which marks a specified file as
6519 ``old'' (@pxref{Options Summary, ,Summary of Options}).  This means
6520 that the file itself will not be remade, and nothing else will be
6521 remade on its account.  Follow this procedure:
6523 @enumerate
6524 @item
6525 Recompile the source files that need compilation for reasons independent
6526 of the particular header file, with @samp{make -o @var{headerfile}}.
6527 If several header files are involved, use a separate @samp{-o} option
6528 for each header file.
6530 @item
6531 Touch all the object files with @samp{make -t}.
6532 @end enumerate
6534 @node Overriding, Testing, Avoiding Compilation, Running
6535 @section Overriding Variables
6536 @cindex overriding variables with arguments
6537 @cindex variables, overriding with arguments
6538 @cindex command line variables
6539 @cindex variables, command line
6541 An argument that contains @samp{=} specifies the value of a variable:
6542 @samp{@var{v}=@var{x}} sets the value of the variable @var{v} to @var{x}.
6543 If you specify a value in this way, all ordinary assignments of the same
6544 variable in the makefile are ignored; we say they have been
6545 @dfn{overridden} by the command line argument.
6547 The most common way to use this facility is to pass extra flags to
6548 compilers.  For example, in a properly written makefile, the variable
6549 @code{CFLAGS} is included in each command that runs the C compiler, so a
6550 file @file{foo.c} would be compiled something like this:
6552 @example
6553 cc -c $(CFLAGS) foo.c
6554 @end example
6556 Thus, whatever value you set for @code{CFLAGS} affects each compilation
6557 that occurs.  The makefile probably specifies the usual value for
6558 @code{CFLAGS}, like this:
6560 @example
6561 CFLAGS=-g
6562 @end example
6564 Each time you run @code{make}, you can override this value if you
6565 wish.  For example, if you say @samp{make CFLAGS='-g -O'}, each C
6566 compilation will be done with @samp{cc -c -g -O}.  (This illustrates
6567 how you can use quoting in the shell to enclose spaces and other
6568 special characters in the value of a variable when you override it.)
6570 The variable @code{CFLAGS} is only one of many standard variables that
6571 exist just so that you can change them this way.  @xref{Implicit
6572 Variables, , Variables Used by Implicit Rules}, for a complete list.
6574 You can also program the makefile to look at additional variables of your
6575 own, giving the user the ability to control other aspects of how the
6576 makefile works by changing the variables.
6578 When you override a variable with a command argument, you can define either
6579 a recursively-expanded variable or a simply-expanded variable.  The
6580 examples shown above make a recursively-expanded variable; to make a
6581 simply-expanded variable, write @samp{:=} instead of @samp{=}.  But, unless
6582 you want to include a variable reference or function call in the
6583 @emph{value} that you specify, it makes no difference which kind of
6584 variable you create.
6586 There is one way that the makefile can change a variable that you have
6587 overridden.  This is to use the @code{override} directive, which is a line
6588 that looks like this: @samp{override @var{variable} = @var{value}}
6589 (@pxref{Override Directive, ,The @code{override} Directive}).
6591 @node Testing, Options Summary, Overriding, Running
6592 @section Testing the Compilation of a Program
6593 @cindex testing compilation
6594 @cindex compilation, testing
6596 Normally, when an error happens in executing a shell command, @code{make}
6597 gives up immediately, returning a nonzero status.  No further commands are
6598 executed for any target.  The error implies that the goal cannot be
6599 correctly remade, and @code{make} reports this as soon as it knows.
6601 When you are compiling a program that you have just changed, this is not
6602 what you want.  Instead, you would rather that @code{make} try compiling
6603 every file that can be tried, to show you as many compilation errors
6604 as possible.
6606 @cindex @code{-k}
6607 @cindex @code{--keep-going}
6608 On these occasions, you should use the @samp{-k} or
6609 @samp{--keep-going} flag.  This tells @code{make} to continue to
6610 consider the other dependencies of the pending targets, remaking them
6611 if necessary, before it gives up and returns nonzero status.  For
6612 example, after an error in compiling one object file, @samp{make -k}
6613 will continue compiling other object files even though it already
6614 knows that linking them will be impossible.  In addition to continuing
6615 after failed shell commands, @samp{make -k} will continue as much as
6616 possible after discovering that it does not know how to make a target
6617 or dependency file.  This will always cause an error message, but
6618 without @samp{-k}, it is a fatal error (@pxref{Options Summary,
6619 ,Summary of Options}).@refill
6621 The usual behavior of @code{make} assumes that your purpose is to get the
6622 goals up to date; once @code{make} learns that this is impossible, it might
6623 as well report the failure immediately.  The @samp{-k} flag says that the
6624 real purpose is to test as much as possible of the changes made in the
6625 program, perhaps to find several independent problems so that you can
6626 correct them all before the next attempt to compile.  This is why Emacs'
6627 @kbd{M-x compile} command passes the @samp{-k} flag by default.
6629 @node Options Summary,  , Testing, Running
6630 @section Summary of Options
6631 @cindex options
6632 @cindex flags
6633 @cindex switches
6635 Here is a table of all the options @code{make} understands:
6637 @table @samp
6638 @item -b
6639 @cindex @code{-b}
6640 @itemx -m
6641 @cindex @code{-m}
6642 These options are ignored for compatibility with other versions of @code{make}.
6644 @item -C @var{dir}
6645 @cindex @code{-C}
6646 @itemx --directory=@var{dir}
6647 @cindex @code{--directory}
6648 Change to directory @var{dir} before reading the makefiles.  If multiple
6649 @samp{-C} options are specified, each is interpreted relative to the
6650 previous one: @samp{-C / -C etc} is equivalent to @samp{-C /etc}.
6651 This is typically used with recursive invocations of @code{make}
6652 (@pxref{Recursion, ,Recursive Use of @code{make}}).
6654 @item -d
6655 @cindex @code{-d}
6656 @itemx --debug
6657 @cindex @code{--debug}
6658 @c Extra blank line here makes the table look better.
6660 Print debugging information in addition to normal processing.  The
6661 debugging information says which files are being considered for
6662 remaking, which file-times are being compared and with what results,
6663 which files actually need to be remade, which implicit rules are
6664 considered and which are applied---everything interesting about how
6665 @code{make} decides what to do.
6667 @item -e
6668 @cindex @code{-e}
6669 @itemx --environment-overrides
6670 @cindex @code{--environment-overrides}
6671 Give variables taken from the environment precedence
6672 over variables from makefiles.
6673 @xref{Environment, ,Variables from the Environment}.
6675 @item -f @var{file}
6676 @cindex @code{-f}
6677 @itemx --file=@var{file}
6678 @cindex @code{--file}
6679 @itemx --makefile=@var{file}
6680 @cindex @code{--makefile}
6681 Read the file named @var{file} as a makefile.
6682 @xref{Makefiles, ,Writing Makefiles}.
6684 @item -h
6685 @cindex @code{-h}
6686 @itemx --help
6687 @cindex @code{--help}
6688 @c Extra blank line here makes the table look better.
6690 Remind you of the options that @code{make} understands and then exit.
6692 @item -i
6693 @cindex @code{-i}
6694 @itemx --ignore-errors
6695 @cindex @code{--ignore-errors}
6696 Ignore all errors in commands executed to remake files.
6697 @xref{Errors, ,Errors in Commands}.
6699 @item -I @var{dir}
6700 @cindex @code{-I}
6701 @itemx --include-dir=@var{dir}
6702 @cindex @code{--include-dir}
6703 Specifies a directory @var{dir} to search for included makefiles.
6704 @xref{Include, ,Including Other Makefiles}.  If several @samp{-I}
6705 options are used to specify several directories, the directories are
6706 searched in the order specified.
6708 @item -j [@var{jobs}]
6709 @cindex @code{-j}
6710 @itemx --jobs=[@var{jobs}]
6711 @cindex @code{--jobs}
6712 Specifies the number of jobs (commands) to run simultaneously.  With no
6713 argument, @code{make} runs as many jobs simultaneously as possible.  If
6714 there is more than one @samp{-j} option, the last one is effective.
6715 @xref{Parallel, ,Parallel Execution},
6716 for more information on how commands are run.
6717 Note that this option is ignored on MS-DOS.
6719 @item -k
6720 @cindex @code{-k}
6721 @itemx --keep-going
6722 @cindex @code{--keep-going}
6723 Continue as much as possible after an error.  While the target that
6724 failed, and those that depend on it, cannot be remade, the other
6725 dependencies of these targets can be processed all the same.
6726 @xref{Testing, ,Testing the Compilation of a Program}.
6728 @item -l [@var{load}]
6729 @cindex @code{-l}
6730 @itemx --load-average[=@var{load}]
6731 @cindex @code{--load-average}
6732 @itemx --max-load[=@var{load}]
6733 @cindex @code{--max-load}
6734 Specifies that no new jobs (commands) should be started if there are
6735 other jobs running and the load average is at least @var{load} (a
6736 floating-point number).  With no argument, removes a previous load
6737 limit.  @xref{Parallel, ,Parallel Execution}.
6739 @item -n
6740 @cindex @code{-n}
6741 @itemx --just-print
6742 @cindex @code{--just-print}
6743 @itemx --dry-run
6744 @cindex @code{--dry-run}
6745 @itemx --recon
6746 @cindex @code{--recon}
6747 @c Extra blank line here makes the table look better.
6749 Print the commands that would be executed, but do not execute them.
6750 @xref{Instead of Execution, ,Instead of Executing the Commands}.
6752 @item -o @var{file}
6753 @cindex @code{-o}
6754 @itemx --old-file=@var{file}
6755 @cindex @code{--old-file}
6756 @itemx --assume-old=@var{file}
6757 @cindex @code{--assume-old}
6758 Do not remake the file @var{file} even if it is older than its
6759 dependencies, and do not remake anything on account of changes in
6760 @var{file}.  Essentially the file is treated as very old and its rules
6761 are ignored.  @xref{Avoiding Compilation, ,Avoiding Recompilation of
6762 Some Files}.@refill
6764 @item -p
6765 @cindex @code{-p}
6766 @itemx --print-data-base
6767 @cindex @code{--print-data-base}
6768 Print the data base (rules and variable values) that results from
6769 reading the makefiles; then execute as usual or as otherwise specified.
6770 This also prints the version information given by the @samp{-v} switch
6771 (see below).  To print the data base without trying to remake any files,
6772 use @w{@samp{make -qp}}.  To print the data base of predefined rules and
6773 variables, use @w{@samp{make -p -f /dev/null}}.
6775 @item -q
6776 @cindex @code{-q}
6777 @itemx --question
6778 @cindex @code{--question}
6779 ``Question mode''.  Do not run any commands, or print anything; just
6780 return an exit status that is zero if the specified targets are already
6781 up to date, one if any remaking is required, or two if an error is
6782 encountered.  @xref{Instead of Execution, ,Instead of Executing the
6783 Commands}.@refill
6785 @item -r
6786 @cindex @code{-r}
6787 @itemx --no-builtin-rules
6788 @cindex @code{--no-builtin-rules}
6789 Eliminate use of the built-in implicit rules (@pxref{Implicit Rules,
6790 ,Using Implicit Rules}).  You can still define your own by writing
6791 pattern rules (@pxref{Pattern Rules, ,Defining and Redefining Pattern
6792 Rules}).  The @samp{-r} option also clears out the default list of
6793 suffixes for suffix rules (@pxref{Suffix Rules, ,Old-Fashioned Suffix
6794 Rules}).  But you can still define your own suffixes with a rule for
6795 @code{.SUFFIXES}, and then define your own suffix rules.  Note that only
6796 @emph{rules} are affected by the @code{-r} option; default variables
6797 remain in effect (@pxref{Implicit Variables, ,Variables Used by Implicit
6798 Rules}); see the @samp{-R} option below.
6800 @item -R
6801 @cindex @code{-R}
6802 @itemx --no-builtin-variables
6803 @cindex @code{--no-builtin-variables}
6804 Eliminate use of the built-in rule-specific variables (@pxref{Implicit
6805 Variables, ,Variables Used by Implicit Rules}).  You can still define
6806 your own, of course.  The @samp{-R} option also automatically enables
6807 the @samp{-r} option (see above), since it doesn't make sense to have
6808 implicit rules without any definitions for the variables that they use.
6810 @item -s
6811 @cindex @code{-s}
6812 @itemx --silent
6813 @cindex @code{--silent}
6814 @itemx --quiet
6815 @cindex @code{--quiet}
6816 @c Extra blank line here makes the table look better.
6818 Silent operation; do not print the commands as they are executed.
6819 @xref{Echoing, ,Command Echoing}.
6821 @item -S
6822 @cindex @code{-S}
6823 @itemx --no-keep-going
6824 @cindex @code{--no-keep-going}
6825 @itemx --stop
6826 @cindex @code{--stop}
6827 @c Extra blank line here makes the table look better.
6829 Cancel the effect of the @samp{-k} option.  This is never necessary
6830 except in a recursive @code{make} where @samp{-k} might be inherited
6831 from the top-level @code{make} via @code{MAKEFLAGS}
6832 (@pxref{Recursion, ,Recursive Use of @code{make}})
6833 or if you set @samp{-k} in @code{MAKEFLAGS} in your environment.@refill
6835 @item -t
6836 @cindex @code{-t}
6837 @itemx --touch
6838 @cindex @code{--touch}
6839 @c Extra blank line here makes the table look better.
6841 Touch files (mark them up to date without really changing them)
6842 instead of running their commands.  This is used to pretend that the
6843 commands were done, in order to fool future invocations of
6844 @code{make}.  @xref{Instead of Execution, ,Instead of Executing the Commands}.
6846 @item -v
6847 @cindex @code{-v}
6848 @itemx --version
6849 @cindex @code{--version}
6850 Print the version of the @code{make} program plus a copyright, a list
6851 of authors, and a notice that there is no warranty; then exit.
6853 @item -w
6854 @cindex @code{-w}
6855 @itemx --print-directory
6856 @cindex @code{--print-directory}
6857 Print a message containing the working directory both before and after
6858 executing the makefile.  This may be useful for tracking down errors
6859 from complicated nests of recursive @code{make} commands.
6860 @xref{Recursion, ,Recursive Use of @code{make}}.  (In practice, you
6861 rarely need to specify this option since @samp{make} does it for you;
6862 see @ref{-w Option, ,The @samp{--print-directory} Option}.)
6864 @itemx --no-print-directory
6865 @cindex @code{--no-print-directory}
6866 Disable printing of the working directory under @code{-w}.
6867 This option is useful when @code{-w} is turned on automatically,
6868 but you do not want to see the extra messages.
6869 @xref{-w Option, ,The @samp{--print-directory} Option}.
6871 @item -W @var{file}
6872 @cindex @code{-W}
6873 @itemx --what-if=@var{file}
6874 @cindex @code{--what-if}
6875 @itemx --new-file=@var{file}
6876 @cindex @code{--new-file}
6877 @itemx --assume-new=@var{file}
6878 @cindex @code{--assume-new}
6879 Pretend that the target @var{file} has just been modified.  When used
6880 with the @samp{-n} flag, this shows you what would happen if you were
6881 to modify that file.  Without @samp{-n}, it is almost the same as
6882 running a @code{touch} command on the given file before running
6883 @code{make}, except that the modification time is changed only in the
6884 imagination of @code{make}.
6885 @xref{Instead of Execution, ,Instead of Executing the Commands}.
6887 @item --warn-undefined-variables
6888 @cindex @code{--warn-undefined-variables}
6889 @cindex variables, warning for undefined
6890 @cindex undefined variables, warning message
6891 Issue a warning message whenever @code{make} sees a reference to an
6892 undefined variable.  This can be helpful when you are trying to debug
6893 makefiles which use variables in complex ways.
6894 @end table
6896 @node Implicit Rules, Archives, Running, Top
6897 @chapter Using Implicit Rules
6898 @cindex implicit rule
6899 @cindex rule, implicit
6901 Certain standard ways of remaking target files are used very often.  For
6902 example, one customary way to make an object file is from a C source file
6903 using the C compiler, @code{cc}.
6905 @dfn{Implicit rules} tell @code{make} how to use customary techniques so
6906 that you do not have to specify them in detail when you want to use
6907 them.  For example, there is an implicit rule for C compilation.  File
6908 names determine which implicit rules are run.  For example, C
6909 compilation typically takes a @file{.c} file and makes a @file{.o} file.
6910 So @code{make} applies the implicit rule for C compilation when it sees
6911 this combination of file name endings.@refill
6913 A chain of implicit rules can apply in sequence; for example, @code{make}
6914 will remake a @file{.o} file from a @file{.y} file by way of a @file{.c} file.
6915 @iftex
6916 @xref{Chained Rules, ,Chains of Implicit Rules}.
6917 @end iftex
6919 The built-in implicit rules use several variables in their commands so
6920 that, by changing the values of the variables, you can change the way the
6921 implicit rule works.  For example, the variable @code{CFLAGS} controls the
6922 flags given to the C compiler by the implicit rule for C compilation.
6923 @iftex
6924 @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
6925 @end iftex
6927 You can define your own implicit rules by writing @dfn{pattern rules}.
6928 @iftex
6929 @xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.
6930 @end iftex
6932 @dfn{Suffix rules} are a more limited way to define implicit rules.
6933 Pattern rules are more general and clearer, but suffix rules are
6934 retained for compatibility.
6935 @iftex
6936 @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
6937 @end iftex
6939 @menu
6940 * Using Implicit::              How to use an existing implicit rule
6941                                   to get the commands for updating a file.
6942 * Catalogue of Rules::          A list of built-in implicit rules.
6943 * Implicit Variables::          How to change what predefined rules do.
6944 * Chained Rules::               How to use a chain of implicit rules.
6945 * Pattern Rules::               How to define new implicit rules.
6946 * Last Resort::                 How to defining commands for rules
6947                                   which cannot find any.
6948 * Suffix Rules::                The old-fashioned style of implicit rule.
6949 * Implicit Rule Search::        The precise algorithm for applying
6950                                   implicit rules.
6951 @end menu
6953 @node Using Implicit, Catalogue of Rules,  , Implicit Rules
6954 @section Using Implicit Rules
6955 @cindex implicit rule, how to use
6956 @cindex rule, implicit, how to use
6958 To allow @code{make} to find a customary method for updating a target file,
6959 all you have to do is refrain from specifying commands yourself.  Either
6960 write a rule with no command lines, or don't write a rule at all.  Then
6961 @code{make} will figure out which implicit rule to use based on which
6962 kind of source file exists or can be made.
6964 For example, suppose the makefile looks like this:
6966 @example
6967 foo : foo.o bar.o
6968         cc -o foo foo.o bar.o $(CFLAGS) $(LDFLAGS)
6969 @end example
6971 @noindent
6972 Because you mention @file{foo.o} but do not give a rule for it, @code{make}
6973 will automatically look for an implicit rule that tells how to update it.
6974 This happens whether or not the file @file{foo.o} currently exists.
6976 If an implicit rule is found, it can supply both commands and one or
6977 more dependencies (the source files).  You would want to write a rule
6978 for @file{foo.o} with no command lines if you need to specify additional
6979 dependencies, such as header files, that the implicit rule cannot
6980 supply.
6982 Each implicit rule has a target pattern and dependency patterns.  There may
6983 be many implicit rules with the same target pattern.  For example, numerous
6984 rules make @samp{.o} files: one, from a @samp{.c} file with the C compiler;
6985 another, from a @samp{.p} file with the Pascal compiler; and so on.  The rule
6986 that actually applies is the one whose dependencies exist or can be made.
6987 So, if you have a file @file{foo.c}, @code{make} will run the C compiler;
6988 otherwise, if you have a file @file{foo.p}, @code{make} will run the Pascal
6989 compiler; and so on.
6991 Of course, when you write the makefile, you know which implicit rule you
6992 want @code{make} to use, and you know it will choose that one because you
6993 know which possible dependency files are supposed to exist.
6994 @xref{Catalogue of Rules, ,Catalogue of Implicit Rules},
6995 for a catalogue of all the predefined implicit rules.
6997 Above, we said an implicit rule applies if the required dependencies ``exist
6998 or can be made''.  A file ``can be made'' if it is mentioned explicitly in
6999 the makefile as a target or a dependency, or if an implicit rule can be
7000 recursively found for how to make it.  When an implicit dependency is the
7001 result of another implicit rule, we say that @dfn{chaining} is occurring.
7002 @xref{Chained Rules, ,Chains of Implicit Rules}.
7004 In general, @code{make} searches for an implicit rule for each target, and
7005 for each double-colon rule, that has no commands.  A file that is mentioned
7006 only as a dependency is considered a target whose rule specifies nothing,
7007 so implicit rule search happens for it.  @xref{Implicit Rule Search, ,Implicit Rule Search Algorithm}, for the
7008 details of how the search is done.
7010 Note that explicit dependencies do not influence implicit rule search.
7011 For example, consider this explicit rule:
7013 @example
7014 foo.o: foo.p
7015 @end example
7017 @noindent
7018 The dependency on @file{foo.p} does not necessarily mean that
7019 @code{make} will remake @file{foo.o} according to the implicit rule to
7020 make an object file, a @file{.o} file, from a Pascal source file, a
7021 @file{.p} file.  For example, if @file{foo.c} also exists, the implicit
7022 rule to make an object file from a C source file is used instead,
7023 because it appears before the Pascal rule in the list of predefined
7024 implicit rules (@pxref{Catalogue of Rules, , Catalogue of Implicit
7025 Rules}).
7027 If you do not want an implicit rule to be used for a target that has no
7028 commands, you can give that target empty commands by writing a semicolon
7029 (@pxref{Empty Commands, ,Defining Empty Commands}).
7031 @node Catalogue of Rules, Implicit Variables, Using Implicit, Implicit Rules
7032 @section Catalogue of Implicit Rules
7033 @cindex implicit rule, predefined
7034 @cindex rule, implicit, predefined
7036 Here is a catalogue of predefined implicit rules which are always
7037 available unless the makefile explicitly overrides or cancels them.
7038 @xref{Canceling Rules, ,Canceling Implicit Rules}, for information on
7039 canceling or overriding an implicit rule.  The @samp{-r} or
7040 @samp{--no-builtin-rules} option cancels all predefined rules.
7042 Not all of these rules will always be defined, even when the @samp{-r}
7043 option is not given.  Many of the predefined implicit rules are
7044 implemented in @code{make} as suffix rules, so which ones will be
7045 defined depends on the @dfn{suffix list} (the list of dependencies of
7046 the special target @code{.SUFFIXES}).  The default suffix list is:
7047 @code{.out}, @code{.a}, @code{.ln}, @code{.o}, @code{.c}, @code{.cc},
7048 @code{.C}, @code{.p}, @code{.f}, @code{.F}, @code{.r}, @code{.y},
7049 @code{.l}, @code{.s}, @code{.S}, @code{.mod}, @code{.sym}, @code{.def},
7050 @code{.h}, @code{.info}, @code{.dvi}, @code{.tex}, @code{.texinfo},
7051 @code{.texi}, @code{.txinfo}, @code{.w}, @code{.ch} @code{.web},
7052 @code{.sh}, @code{.elc}, @code{.el}.  All of the implicit rules
7053 described below whose dependencies have one of these suffixes are
7054 actually suffix rules.  If you modify the suffix list, the only
7055 predefined suffix rules in effect will be those named by one or two of
7056 the suffixes that are on the list you specify; rules whose suffixes fail
7057 to be on the list are disabled.  @xref{Suffix Rules, ,Old-Fashioned
7058 Suffix Rules}, for full details on suffix rules.
7060 @table @asis
7061 @item Compiling C programs
7062 @cindex C, rule to compile
7063 @pindex cc
7064 @pindex gcc
7065 @pindex .o
7066 @pindex .c
7067 @file{@var{n}.o} is made automatically from @file{@var{n}.c} with
7068 a command of the form @samp{$(CC) -c $(CPPFLAGS) $(CFLAGS)}.@refill
7070 @item Compiling C++ programs
7071 @cindex C++, rule to compile
7072 @pindex g++
7073 @pindex .C
7074 @pindex .cc
7075 @file{@var{n}.o} is made automatically from @file{@var{n}.cc} or
7076 @file{@var{n}.C} with a command of the form @samp{$(CXX) -c $(CPPFLAGS)
7077 $(CXXFLAGS)}.  We encourage you to use the suffix @samp{.cc} for C++
7078 source files instead of @samp{.C}.@refill
7080 @item Compiling Pascal programs
7081 @cindex Pascal, rule to compile
7082 @pindex pc
7083 @pindex .p
7084 @file{@var{n}.o} is made automatically from @file{@var{n}.p}
7085 with the command @samp{$(PC) -c $(PFLAGS)}.@refill
7087 @item Compiling Fortran and Ratfor programs
7088 @cindex Fortran, rule to compile
7089 @cindex Ratfor, rule to compile
7090 @pindex f77
7091 @pindex .f
7092 @pindex .r
7093 @pindex .F
7094 @file{@var{n}.o} is made automatically from @file{@var{n}.r},
7095 @file{@var{n}.F} or @file{@var{n}.f} by running the
7096 Fortran compiler.  The precise command used is as follows:@refill
7098 @table @samp
7099 @item .f
7100 @samp{$(FC) -c $(FFLAGS)}.
7101 @item .F
7102 @samp{$(FC) -c $(FFLAGS) $(CPPFLAGS)}.
7103 @item .r
7104 @samp{$(FC) -c $(FFLAGS) $(RFLAGS)}.
7105 @end table
7107 @item Preprocessing Fortran and Ratfor programs
7108 @file{@var{n}.f} is made automatically from @file{@var{n}.r} or
7109 @file{@var{n}.F}.  This rule runs just the preprocessor to convert a
7110 Ratfor or preprocessable Fortran program into a strict Fortran
7111 program.  The precise command used is as follows:@refill
7113 @table @samp
7114 @item .F
7115 @samp{$(FC) -F $(CPPFLAGS) $(FFLAGS)}.
7116 @item .r
7117 @samp{$(FC) -F $(FFLAGS) $(RFLAGS)}.
7118 @end table
7120 @item Compiling Modula-2 programs
7121 @cindex Modula-2, rule to compile
7122 @pindex m2c
7123 @pindex .sym
7124 @pindex .def
7125 @pindex .mod
7126 @file{@var{n}.sym} is made from @file{@var{n}.def} with a command
7127 of the form @samp{$(M2C) $(M2FLAGS) $(DEFFLAGS)}.  @file{@var{n}.o}
7128 is made from @file{@var{n}.mod}; the form is:
7129 @w{@samp{$(M2C) $(M2FLAGS) $(MODFLAGS)}}.@refill
7131 @need 1200
7132 @item Assembling and preprocessing assembler programs
7133 @cindex assembly, rule to compile
7134 @pindex as
7135 @pindex .s
7136 @file{@var{n}.o} is made automatically from @file{@var{n}.s} by
7137 running the assembler, @code{as}.  The precise command is
7138 @samp{$(AS) $(ASFLAGS)}.@refill
7140 @pindex .S
7141 @file{@var{n}.s} is made automatically from @file{@var{n}.S} by
7142 running the C preprocessor, @code{cpp}.  The precise command is
7143 @w{@samp{$(CPP) $(CPPFLAGS)}}.
7145 @item Linking a single object file
7146 @cindex linking, predefined rule for
7147 @pindex ld
7148 @pindex .o
7149 @file{@var{n}} is made automatically from @file{@var{n}.o} by running
7150 the linker (usually called @code{ld}) via the C compiler.  The precise
7151 command used is @w{@samp{$(CC) $(LDFLAGS) @var{n}.o $(LOADLIBES) $(LDLIBS)}}.
7153 This rule does the right thing for a simple program with only one
7154 source file.  It will also do the right thing if there are multiple
7155 object files (presumably coming from various other source files), one
7156 of which has a name matching that of the executable file.  Thus,
7158 @example
7159 x: y.o z.o
7160 @end example
7162 @noindent
7163 when @file{x.c}, @file{y.c} and @file{z.c} all exist will execute:
7165 @example
7166 @group
7167 cc -c x.c -o x.o
7168 cc -c y.c -o y.o
7169 cc -c z.c -o z.o
7170 cc x.o y.o z.o -o x
7171 rm -f x.o
7172 rm -f y.o
7173 rm -f z.o
7174 @end group
7175 @end example
7177 @noindent
7178 In more complicated cases, such as when there is no object file whose
7179 name derives from the executable file name, you must write an explicit
7180 command for linking.
7182 Each kind of file automatically made into @samp{.o} object files will
7183 be automatically linked by using the compiler (@samp{$(CC)},
7184 @samp{$(FC)} or @samp{$(PC)}; the C compiler @samp{$(CC)} is used to
7185 assemble @samp{.s} files) without the @samp{-c} option.  This could be
7186 done by using the @samp{.o} object files as intermediates, but it is
7187 faster to do the compiling and linking in one step, so that's how it's
7188 done.@refill
7190 @item Yacc for C programs
7191 @pindex yacc
7192 @cindex Yacc, rule to run
7193 @pindex .y
7194 @file{@var{n}.c} is made automatically from @file{@var{n}.y} by
7195 running Yacc with the command @samp{$(YACC) $(YFLAGS)}.
7197 @item Lex for C programs
7198 @pindex lex
7199 @cindex Lex, rule to run
7200 @pindex .l
7201 @file{@var{n}.c} is made automatically from @file{@var{n}.l} by
7202 by running Lex.  The actual command is @samp{$(LEX) $(LFLAGS)}.
7204 @item Lex for Ratfor programs
7205 @file{@var{n}.r} is made automatically from @file{@var{n}.l} by
7206 by running Lex.  The actual command is @samp{$(LEX) $(LFLAGS)}.
7208 The convention of using the same suffix @samp{.l} for all Lex files
7209 regardless of whether they produce C code or Ratfor code makes it
7210 impossible for @code{make} to determine automatically which of the two
7211 languages you are using in any particular case.  If @code{make} is
7212 called upon to remake an object file from a @samp{.l} file, it must
7213 guess which compiler to use.  It will guess the C compiler, because
7214 that is more common.  If you are using Ratfor, make sure @code{make}
7215 knows this by mentioning @file{@var{n}.r} in the makefile.  Or, if you
7216 are using Ratfor exclusively, with no C files, remove @samp{.c} from
7217 the list of implicit rule suffixes with:@refill
7219 @example
7220 @group
7221 .SUFFIXES:
7222 .SUFFIXES: .o .r .f .l @dots{}
7223 @end group
7224 @end example
7226 @item Making Lint Libraries from C, Yacc, or Lex programs
7227 @pindex lint
7228 @cindex @code{lint}, rule to run
7229 @pindex .ln
7230 @file{@var{n}.ln} is made from @file{@var{n}.c} by running @code{lint}.
7231 The precise command is @w{@samp{$(LINT) $(LINTFLAGS) $(CPPFLAGS) -i}}.
7232 The same command is used on the C code produced from
7233 @file{@var{n}.y} or @file{@var{n}.l}.@refill
7235 @item @TeX{} and Web
7236 @cindex @TeX{}, rule to run
7237 @cindex Web, rule to run
7238 @pindex tex
7239 @pindex cweave
7240 @pindex weave
7241 @pindex tangle
7242 @pindex ctangle
7243 @pindex .dvi
7244 @pindex .tex
7245 @pindex .web
7246 @pindex .w
7247 @pindex .ch
7248 @file{@var{n}.dvi} is made from @file{@var{n}.tex} with the command
7249 @samp{$(TEX)}.  @file{@var{n}.tex} is made from @file{@var{n}.web} with
7250 @samp{$(WEAVE)}, or from @file{@var{n}.w} (and from @file{@var{n}.ch} if
7251 it exists or can be made) with @samp{$(CWEAVE)}.  @file{@var{n}.p} is
7252 made from @file{@var{n}.web} with @samp{$(TANGLE)} and @file{@var{n}.c}
7253 is made from @file{@var{n}.w} (and from @file{@var{n}.ch} if it exists
7254 or can be made) with @samp{$(CTANGLE)}.@refill
7256 @item Texinfo and Info
7257 @cindex Texinfo, rule to format
7258 @cindex Info, rule to format
7259 @pindex texi2dvi
7260 @pindex makeinfo
7261 @pindex .texinfo
7262 @pindex .info
7263 @pindex .texi
7264 @pindex .txinfo
7265 @file{@var{n}.dvi} is made from @file{@var{n}.texinfo},
7266 @file{@var{n}.texi}, or @file{@var{n}.txinfo}, with the command
7267 @w{@samp{$(TEXI2DVI) $(TEXI2DVI_FLAGS)}}.  @file{@var{n}.info} is made from
7268 @file{@var{n}.texinfo}, @file{@var{n}.texi}, or @file{@var{n}.txinfo}, with
7269 the command @w{@samp{$(MAKEINFO) $(MAKEINFO_FLAGS)}}.
7271 @item RCS
7272 @cindex RCS, rule to extract from
7273 @pindex co
7274 @pindex ,v @r{(RCS file extension)}
7275 Any file @file{@var{n}} is extracted if necessary from an RCS file
7276 named either @file{@var{n},v} or @file{RCS/@var{n},v}.  The precise
7277 command used is @w{@samp{$(CO) $(COFLAGS)}}.  @file{@var{n}} will not be
7278 extracted from RCS if it already exists, even if the RCS file is
7279 newer.  The rules for RCS are terminal
7280 (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}),
7281 so RCS files cannot be generated from another source; they must
7282 actually exist.@refill
7284 @item SCCS
7285 @cindex SCCS, rule to extract from
7286 @pindex get
7287 @pindex s. @r{(SCCS file prefix)}
7288 Any file @file{@var{n}} is extracted if necessary from an SCCS file
7289 named either @file{s.@var{n}} or @file{SCCS/s.@var{n}}.  The precise
7290 command used is @w{@samp{$(GET) $(GFLAGS)}}.  The rules for SCCS are
7291 terminal (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}),
7292 so SCCS files cannot be generated from another source; they must
7293 actually exist.@refill
7295 @pindex .sh
7296 For the benefit of SCCS, a file @file{@var{n}} is copied from
7297 @file{@var{n}.sh} and made executable (by everyone).  This is for
7298 shell scripts that are checked into SCCS.  Since RCS preserves the
7299 execution permission of a file, you do not need to use this feature
7300 with RCS.@refill
7302 We recommend that you avoid using of SCCS.  RCS is widely held to be
7303 superior, and is also free.  By choosing free software in place of
7304 comparable (or inferior) proprietary software, you support the free
7305 software movement.
7306 @end table
7308 Usually, you want to change only the variables listed in the table
7309 above, which are documented in the following section.
7311 However, the commands in built-in implicit rules actually use
7312 variables such as @code{COMPILE.c}, @code{LINK.p}, and
7313 @code{PREPROCESS.S}, whose values contain the commands listed above.
7315 @code{make} follows the convention that the rule to compile a
7316 @file{.@var{x}} source file uses the variable @code{COMPILE.@var{x}}.
7317 Similarly, the rule to produce an executable from a @file{.@var{x}}
7318 file uses @code{LINK.@var{x}}; and the rule to preprocess a
7319 @file{.@var{x}} file uses @code{PREPROCESS.@var{x}}.
7321 @vindex OUTPUT_OPTION
7322 Every rule that produces an object file uses the variable
7323 @code{OUTPUT_OPTION}.  @code{make} defines this variable either to
7324 contain @samp{-o $@@}, or to be empty, depending on a compile-time
7325 option.  You need the @samp{-o} option to ensure that the output goes
7326 into the right file when the source file is in a different directory,
7327 as when using @code{VPATH} (@pxref{Directory Search}).  However,
7328 compilers on some systems do not accept a @samp{-o} switch for object
7329 files.  If you use such a system, and use @code{VPATH}, some
7330 compilations will put their output in the wrong place.
7331 A possible workaround for this problem is to give @code{OUTPUT_OPTION}
7332 the value @w{@samp{; mv $*.o $@@}}.
7334 @node Implicit Variables, Chained Rules, Catalogue of Rules, Implicit Rules
7335 @section Variables Used by Implicit Rules
7336 @cindex flags for compilers
7338 The commands in built-in implicit rules make liberal use of certain
7339 predefined variables.  You can alter these variables in the makefile,
7340 with arguments to @code{make}, or in the environment to alter how the
7341 implicit rules work without redefining the rules themselves.  You can
7342 cancel all variables used by implicit rules with the @samp{-R} or
7343 @samp{--no-builtin-variables} option.
7345 For example, the command used to compile a C source file actually says
7346 @samp{$(CC) -c $(CFLAGS) $(CPPFLAGS)}.  The default values of the variables
7347 used are @samp{cc} and nothing, resulting in the command @samp{cc -c}.  By
7348 redefining @samp{CC} to @samp{ncc}, you could cause @samp{ncc} to be
7349 used for all C compilations performed by the implicit rule.  By redefining
7350 @samp{CFLAGS} to be @samp{-g}, you could pass the @samp{-g} option to
7351 each compilation.  @emph{All} implicit rules that do C compilation use
7352 @samp{$(CC)} to get the program name for the compiler and @emph{all}
7353 include @samp{$(CFLAGS)} among the arguments given to the compiler.@refill
7355 The variables used in implicit rules fall into two classes: those that are
7356 names of programs (like @code{CC}) and those that contain arguments for the
7357 programs (like @code{CFLAGS}).  (The ``name of a program'' may also contain
7358 some command arguments, but it must start with an actual executable program
7359 name.)  If a variable value contains more than one argument, separate them
7360 with spaces.
7362 Here is a table of variables used as names of programs in built-in rules:
7364 @table @code
7365 @item AR
7366 @vindex AR
7367 Archive-maintaining program; default @samp{ar}.
7368 @pindex ar
7370 @item AS
7371 @vindex AS
7372 Program for doing assembly; default @samp{as}.
7373 @pindex as
7375 @item CC
7376 @vindex CC
7377 Program for compiling C programs; default @samp{cc}.
7378 @pindex cc
7380 @item CXX
7381 @vindex CXX
7382 Program for compiling C++ programs; default @samp{g++}.
7383 @pindex g++
7385 @item CO
7386 @vindex CO
7387 Program for extracting a file from RCS; default @samp{co}.
7388 @pindex co
7390 @item CPP
7391 @vindex CPP
7392 Program for running the C preprocessor, with results to standard output;
7393 default @samp{$(CC) -E}.
7395 @item FC
7396 @vindex FC
7397 Program for compiling or preprocessing Fortran and Ratfor programs;
7398 default @samp{f77}.
7399 @pindex f77
7401 @item GET
7402 @vindex GET
7403 Program for extracting a file from SCCS; default @samp{get}.
7404 @pindex get
7406 @item LEX
7407 @vindex LEX
7408 Program to use to turn Lex grammars into C programs or Ratfor programs;
7409 default @samp{lex}.
7410 @pindex lex
7412 @item PC
7413 @vindex PC
7414 Program for compiling Pascal programs; default @samp{pc}.
7415 @pindex pc
7417 @item YACC
7418 @vindex YACC
7419 Program to use to turn Yacc grammars into C programs; default @samp{yacc}.
7420 @pindex yacc
7422 @item YACCR
7423 @vindex YACCR
7424 Program to use to turn Yacc grammars into Ratfor
7425 programs; default @samp{yacc -r}.
7427 @item MAKEINFO
7428 @vindex MAKEINFO
7429 Program to convert a Texinfo source file into an Info file; default
7430 @samp{makeinfo}.
7431 @pindex makeinfo
7433 @item TEX
7434 @vindex TEX
7435 Program to make @TeX{} @sc{dvi} files from @TeX{} source;
7436 default @samp{tex}.
7437 @pindex tex
7439 @item TEXI2DVI
7440 @vindex TEXI2DVI
7441 Program to make @TeX{} @sc{dvi} files from Texinfo source;
7442 default @samp{texi2dvi}.
7443 @pindex texi2dvi
7445 @item WEAVE
7446 @vindex WEAVE
7447 Program to translate Web into @TeX{}; default @samp{weave}.
7448 @pindex weave
7450 @item CWEAVE
7451 @vindex CWEAVE
7452 Program to translate C Web into @TeX{}; default @samp{cweave}.
7453 @pindex cweave
7455 @item TANGLE
7456 @vindex TANGLE
7457 Program to translate Web into Pascal; default @samp{tangle}.
7458 @pindex tangle
7460 @item CTANGLE
7461 @vindex CTANGLE
7462 Program to translate C Web into C; default @samp{ctangle}.
7463 @pindex ctangle
7465 @item RM
7466 @vindex RM
7467 Command to remove a file; default @samp{rm -f}.
7468 @pindex rm
7469 @end table
7471 Here is a table of variables whose values are additional arguments for the
7472 programs above.  The default values for all of these is the empty
7473 string, unless otherwise noted.
7475 @table @code
7476 @item ARFLAGS
7477 @vindex ARFLAGS
7478 Flags to give the archive-maintaining program; default @samp{rv}.
7480 @item ASFLAGS
7481 @vindex ASFLAGS
7482 Extra flags to give to the assembler (when explicitly
7483 invoked on a @samp{.s} or @samp{.S} file).
7485 @item CFLAGS
7486 @vindex CFLAGS
7487 Extra flags to give to the C compiler.
7489 @item CXXFLAGS
7490 @vindex CXXFLAGS
7491 Extra flags to give to the C++ compiler.
7493 @item COFLAGS
7494 @vindex COFLAGS
7495 Extra flags to give to the RCS @code{co} program.
7497 @item CPPFLAGS
7498 @vindex CPPFLAGS
7499 Extra flags to give to the C preprocessor and programs
7500 that use it (the C and Fortran compilers).
7502 @item FFLAGS
7503 @vindex FFLAGS
7504 Extra flags to give to the Fortran compiler.
7506 @item GFLAGS
7507 @vindex GFLAGS
7508 Extra flags to give to the SCCS @code{get} program.
7510 @item LDFLAGS
7511 @vindex LDFLAGS
7512 Extra flags to give to compilers when they are
7513 supposed to invoke the linker, @samp{ld}.
7515 @item LFLAGS
7516 @vindex LFLAGS
7517 Extra flags to give to Lex.
7519 @item PFLAGS
7520 @vindex PFLAGS
7521 Extra flags to give to the Pascal compiler.
7523 @item RFLAGS
7524 @vindex RFLAGS
7525 Extra flags to give to the Fortran compiler for Ratfor programs.
7527 @item YFLAGS
7528 @vindex YFLAGS
7529 Extra flags to give to Yacc.
7530 @end table
7532 @node Chained Rules, Pattern Rules, Implicit Variables, Implicit Rules
7533 @section Chains of Implicit Rules
7535 @cindex chains of rules
7536 @cindex rule, implicit, chains of
7537 Sometimes a file can be made by a sequence of implicit rules.  For example,
7538 a file @file{@var{n}.o} could be made from @file{@var{n}.y} by running
7539 first Yacc and then @code{cc}.  Such a sequence is called a @dfn{chain}.
7541 If the file @file{@var{n}.c} exists, or is mentioned in the makefile, no
7542 special searching is required: @code{make} finds that the object file can
7543 be made by C compilation from @file{@var{n}.c}; later on, when considering
7544 how to make @file{@var{n}.c}, the rule for running Yacc is
7545 used.  Ultimately both @file{@var{n}.c} and @file{@var{n}.o} are
7546 updated.@refill
7548 @cindex intermediate files
7549 @cindex files, intermediate
7550 However, even if @file{@var{n}.c} does not exist and is not mentioned,
7551 @code{make} knows how to envision it as the missing link between
7552 @file{@var{n}.o} and @file{@var{n}.y}!  In this case, @file{@var{n}.c} is
7553 called an @dfn{intermediate file}.  Once @code{make} has decided to use the
7554 intermediate file, it is entered in the data base as if it had been
7555 mentioned in the makefile, along with the implicit rule that says how to
7556 create it.@refill
7558 Intermediate files are remade using their rules just like all other
7559 files.  But intermediate files are treated differently in two ways.
7561 The first difference is what happens if the intermediate file does not
7562 exist.  If an ordinary file @var{b} does not exist, and @code{make}
7563 considers a target that depends on @var{b}, it invariably creates
7564 @var{b} and then updates the target from @var{b}.  But if @var{b} is an
7565 intermediate file, then @code{make} can leave well enough alone.  It
7566 won't bother updating @var{b}, or the ultimate target, unless some
7567 dependency of @var{b} is newer than that target or there is some other
7568 reason to update that target.
7570 The second difference is that if @code{make} @emph{does} create @var{b}
7571 in order to update something else, it deletes @var{b} later on after it
7572 is no longer needed.  Therefore, an intermediate file which did not
7573 exist before @code{make} also does not exist after @code{make}.
7574 @code{make} reports the deletion to you by printing a @samp{rm -f}
7575 command showing which file it is deleting.
7577 Ordinarily, a file cannot be intermediate if it is mentioned in the
7578 makefile as a target or dependency.  However, you can explicitly mark a
7579 file as intermediate by listing it as a dependency of the special target
7580 @code{.INTERMEDIATE}.  This takes effect even if the file is mentioned
7581 explicitly in some other way.
7583 @cindex intermediate files, preserving
7584 @cindex preserving intermediate files
7585 @cindex secondary files
7586 You can prevent automatic deletion of an intermediate file by marking it
7587 as a @dfn{secondary} file.  To do this, list it as a dependency of the
7588 special target @code{.SECONDARY}.  When a file is secondary, @code{make}
7589 will not create the file merely because it does not already exist, but
7590 @code{make} does not automatically delete the file.  Marking a file as
7591 secondary also marks it as intermediate.
7593 You can list the target pattern of an implicit rule (such as @samp{%.o})
7594 as a dependency of the special target @code{.PRECIOUS} to preserve
7595 intermediate files made by implicit rules whose target patterns match
7596 that file's name; see @ref{Interrupts}.@refill
7597 @cindex preserving with @code{.PRECIOUS}
7598 @cindex @code{.PRECIOUS} intermediate files
7600 A chain can involve more than two implicit rules.  For example, it is
7601 possible to make a file @file{foo} from @file{RCS/foo.y,v} by running RCS,
7602 Yacc and @code{cc}.  Then both @file{foo.y} and @file{foo.c} are
7603 intermediate files that are deleted at the end.@refill
7605 No single implicit rule can appear more than once in a chain.  This means
7606 that @code{make} will not even consider such a ridiculous thing as making
7607 @file{foo} from @file{foo.o.o} by running the linker twice.  This
7608 constraint has the added benefit of preventing any infinite loop in the
7609 search for an implicit rule chain.
7611 There are some special implicit rules to optimize certain cases that would
7612 otherwise be handled by rule chains.  For example, making @file{foo} from
7613 @file{foo.c} could be handled by compiling and linking with separate
7614 chained rules, using @file{foo.o} as an intermediate file.  But what
7615 actually happens is that a special rule for this case does the compilation
7616 and linking with a single @code{cc} command.  The optimized rule is used in
7617 preference to the step-by-step chain because it comes earlier in the
7618 ordering of rules.
7620 @node Pattern Rules, Last Resort, Chained Rules, Implicit Rules
7621 @section Defining and Redefining Pattern Rules
7623 You define an implicit rule by writing a @dfn{pattern rule}.  A pattern
7624 rule looks like an ordinary rule, except that its target contains the
7625 character @samp{%} (exactly one of them).  The target is considered a
7626 pattern for matching file names; the @samp{%} can match any nonempty
7627 substring, while other characters match only themselves.  The dependencies
7628 likewise use @samp{%} to show how their names relate to the target name.
7630 Thus, a pattern rule @samp{%.o : %.c} says how to make any file
7631 @file{@var{stem}.o} from another file @file{@var{stem}.c}.@refill
7633 Note that expansion using @samp{%} in pattern rules occurs
7634 @strong{after} any variable or function expansions, which take place
7635 when the makefile is read.  @xref{Using Variables, , How to Use
7636 Variables}, and @ref{Functions, ,Functions for Transforming Text}.
7638 @menu
7639 * Pattern Intro::               An introduction to pattern rules.
7640 * Pattern Examples::            Examples of pattern rules.
7641 * Automatic::                   How to use automatic variables in the
7642                                   commands of implicit rules.
7643 * Pattern Match::               How patterns match.
7644 * Match-Anything Rules::        Precautions you should take prior to
7645                                   defining rules that can match any
7646                                   target file whatever.
7647 * Canceling Rules::             How to override or cancel built-in rules.
7648 @end menu
7650 @node Pattern Intro, Pattern Examples,  , Pattern Rules
7651 @subsection Introduction to Pattern Rules
7652 @cindex pattern rule
7653 @cindex rule, pattern
7655 A pattern rule contains the character @samp{%} (exactly one of them)
7656 in the target; otherwise, it looks exactly like an ordinary rule.  The
7657 target is a pattern for matching file names; the @samp{%} matches any
7658 nonempty substring, while other characters match only themselves.
7659 @cindex target pattern, implicit
7660 @cindex @code{%}, in pattern rules
7662 For example, @samp{%.c} as a pattern matches any file name that ends in
7663 @samp{.c}.  @samp{s.%.c} as a pattern matches any file name that starts
7664 with @samp{s.}, ends in @samp{.c} and is at least five characters long.
7665 (There must be at least one character to match the @samp{%}.)  The substring
7666 that the @samp{%} matches is called the @dfn{stem}.@refill
7668 @samp{%} in a dependency of a pattern rule stands for the same stem
7669 that was matched by the @samp{%} in the target.  In order for
7670 the pattern rule to apply, its target pattern must match the file name
7671 under consideration, and its dependency patterns must name files that
7672 exist or can be made.  These files become dependencies of the target.
7673 @cindex dependency pattern, implicit
7675 Thus, a rule of the form
7677 @example
7678 %.o : %.c ; @var{command}@dots{}
7679 @end example
7681 @noindent
7682 specifies how to make a file @file{@var{n}.o}, with another file
7683 @file{@var{n}.c} as its dependency, provided that @file{@var{n}.c}
7684 exists or can be made.
7686 There may also be dependencies that do not use @samp{%}; such a dependency
7687 attaches to every file made by this pattern rule.  These unvarying
7688 dependencies are useful occasionally.
7690 A pattern rule need not have any dependencies that contain @samp{%}, or
7691 in fact any dependencies at all.  Such a rule is effectively a general
7692 wildcard.  It provides a way to make any file that matches the target
7693 pattern.  @xref{Last Resort}.
7695 @c !!! The end of of this paragraph should be rewritten.  --bob
7696 Pattern rules may have more than one target.  Unlike normal rules, this
7697 does not act as many different rules with the same dependencies and
7698 commands.  If a pattern rule has multiple targets, @code{make} knows that
7699 the rule's commands are responsible for making all of the targets.  The
7700 commands are executed only once to make all the targets.  When searching
7701 for a pattern rule to match a target, the target patterns of a rule other
7702 than the one that matches the target in need of a rule are incidental:
7703 @code{make} worries only about giving commands and dependencies to the file
7704 presently in question.  However, when this file's commands are run, the
7705 other targets are marked as having been updated themselves.
7706 @cindex multiple targets, in pattern rule
7707 @cindex target, multiple in pattern rule
7709 The order in which pattern rules appear in the makefile is important
7710 since this is the order in which they are considered.
7711 Of equally applicable
7712 rules, only the first one found is used.  The rules you write take precedence
7713 over those that are built in.  Note however, that a rule whose
7714 dependencies actually exist or are mentioned always takes priority over a
7715 rule with dependencies that must be made by chaining other implicit rules.
7716 @cindex pattern rules, order of
7717 @cindex order of pattern rules
7719 @node Pattern Examples, Automatic, Pattern Intro, Pattern Rules
7720 @subsection Pattern Rule Examples
7722 Here are some examples of pattern rules actually predefined in
7723 @code{make}.  First, the rule that compiles @samp{.c} files into @samp{.o}
7724 files:@refill
7726 @example
7727 %.o : %.c
7728         $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@@
7729 @end example
7731 @noindent
7732 defines a rule that can make any file @file{@var{x}.o} from
7733 @file{@var{x}.c}.  The command uses the automatic variables @samp{$@@} and
7734 @samp{$<} to substitute the names of the target file and the source file
7735 in each case where the rule applies (@pxref{Automatic, ,Automatic Variables}).@refill
7737 Here is a second built-in rule:
7739 @example
7740 % :: RCS/%,v
7741         $(CO) $(COFLAGS) $<
7742 @end example
7744 @noindent
7745 defines a rule that can make any file @file{@var{x}} whatsoever from a
7746 corresponding file @file{@var{x},v} in the subdirectory @file{RCS}.  Since
7747 the target is @samp{%}, this rule will apply to any file whatever, provided
7748 the appropriate dependency file exists.  The double colon makes the rule
7749 @dfn{terminal}, which means that its dependency may not be an intermediate
7750 file (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}).@refill
7752 @need 500
7753 This pattern rule has two targets:
7755 @example
7756 @group
7757 %.tab.c %.tab.h: %.y
7758         bison -d $<
7759 @end group
7760 @end example
7762 @noindent
7763 @c The following paragraph is rewritten to avoid overfull hboxes
7764 This tells @code{make} that the command @samp{bison -d @var{x}.y} will
7765 make both @file{@var{x}.tab.c} and @file{@var{x}.tab.h}.  If the file
7766 @file{foo} depends on the files @file{parse.tab.o} and @file{scan.o}
7767 and the file @file{scan.o} depends on the file @file{parse.tab.h},
7768 when @file{parse.y} is changed, the command @samp{bison -d parse.y}
7769 will be executed only once, and the dependencies of both
7770 @file{parse.tab.o} and @file{scan.o} will be satisfied.  (Presumably
7771 the file @file{parse.tab.o} will be recompiled from @file{parse.tab.c}
7772 and the file @file{scan.o} from @file{scan.c}, while @file{foo} is
7773 linked from @file{parse.tab.o}, @file{scan.o}, and its other
7774 dependencies, and it will execute happily ever after.)@refill
7776 @node Automatic, Pattern Match, Pattern Examples, Pattern Rules
7777 @subsection Automatic Variables
7778 @cindex automatic variables
7779 @cindex variables, automatic
7780 @cindex variables, and implicit rule
7782 Suppose you are writing a pattern rule to compile a @samp{.c} file into a
7783 @samp{.o} file: how do you write the @samp{cc} command so that it operates
7784 on the right source file name?  You cannot write the name in the command,
7785 because the name is different each time the implicit rule is applied.
7787 What you do is use a special feature of @code{make}, the @dfn{automatic
7788 variables}.  These variables have values computed afresh for each rule that
7789 is executed, based on the target and dependencies of the rule.  In this
7790 example, you would use @samp{$@@} for the object file name and @samp{$<}
7791 for the source file name.
7793 Here is a table of automatic variables:
7795 @table @code
7796 @vindex $@@
7797 @vindex @@ @r{(automatic variable)}
7798 @item $@@
7799 The file name of the target of the rule.  If the target is an archive
7800 member, then @samp{$@@} is the name of the archive file.  In a pattern
7801 rule that has multiple targets (@pxref{Pattern Intro, ,Introduction to
7802 Pattern Rules}), @samp{$@@} is the name of whichever target caused the
7803 rule's commands to be run.
7805 @vindex $%
7806 @vindex % @r{(automatic variable)}
7807 @item $%
7808 The target member name, when the target is an archive member.
7809 @xref{Archives}.  For example, if the target is @file{foo.a(bar.o)} then
7810 @samp{$%} is @file{bar.o} and @samp{$@@} is @file{foo.a}.  @samp{$%} is
7811 empty when the target is not an archive member.
7813 @vindex $<
7814 @vindex < @r{(automatic variable)}
7815 @item $<
7816 The name of the first dependency.  If the target got its commands from
7817 an implicit rule, this will be the first dependency added by the
7818 implicit rule (@pxref{Implicit Rules}).
7820 @vindex $?
7821 @vindex ? @r{(automatic variable)}
7822 @item $?
7823 The names of all the dependencies that are newer than the target, with
7824 spaces between them.  For dependencies which are archive members, only
7825 the member named is used (@pxref{Archives}).
7826 @cindex dependencies, list of changed
7827 @cindex list of changed dependencies
7829 @vindex $^
7830 @vindex ^ @r{(automatic variable)}
7831 @item $^
7832 The names of all the dependencies, with spaces between them.  For
7833 dependencies which are archive members, only the member named is used
7834 (@pxref{Archives}).  A target has only one dependency on each other file
7835 it depends on, no matter how many times each file is listed as a
7836 dependency.  So if you list a dependency more than once for a target,
7837 the value of @code{$^} contains just one copy of the name.
7838 @cindex dependencies, list of all
7839 @cindex list of all dependencies
7841 @vindex $+
7842 @vindex + @r{(automatic variable)}
7843 @item $+
7844 This is like @samp{$^}, but dependencies listed more than once are
7845 duplicated in the order they were listed in the makefile.  This is
7846 primarily useful for use in linking commands where it is meaningful to
7847 repeat library file names in a particular order.
7849 @vindex $*
7850 @vindex * @r{(automatic variable)}
7851 @item $*
7852 The stem with which an implicit rule matches (@pxref{Pattern Match, ,How
7853 Patterns Match}).  If the target is @file{dir/a.foo.b} and the target
7854 pattern is @file{a.%.b} then the stem is @file{dir/foo}.  The stem is
7855 useful for constructing names of related files.@refill
7856 @cindex stem, variable for
7858 In a static pattern rule, the stem is part of the file name that matched
7859 the @samp{%} in the target pattern.
7861 In an explicit rule, there is no stem; so @samp{$*} cannot be determined
7862 in that way.  Instead, if the target name ends with a recognized suffix
7863 (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}), @samp{$*} is set to
7864 the target name minus the suffix.  For example, if the target name is
7865 @samp{foo.c}, then @samp{$*} is set to @samp{foo}, since @samp{.c} is a
7866 suffix.  GNU @code{make} does this bizarre thing only for compatibility
7867 with other implementations of @code{make}.  You should generally avoid
7868 using @samp{$*} except in implicit rules or static pattern rules.@refill
7870 If the target name in an explicit rule does not end with a recognized
7871 suffix, @samp{$*} is set to the empty string for that rule.
7872 @end table
7874 @samp{$?} is useful even in explicit rules when you wish to operate on only
7875 the dependencies that have changed.  For example, suppose that an archive
7876 named @file{lib} is supposed to contain copies of several object files.
7877 This rule copies just the changed object files into the archive:
7879 @example
7880 @group
7881 lib: foo.o bar.o lose.o win.o
7882         ar r lib $?
7883 @end group
7884 @end example
7886 Of the variables listed above, four have values that are single file
7887 names, and three have values that are lists of file names.  These seven
7888 have variants that get just the file's directory name or just the file
7889 name within the directory.  The variant variables' names are formed by
7890 appending @samp{D} or @samp{F}, respectively.  These variants are
7891 semi-obsolete in GNU @code{make} since the functions @code{dir} and
7892 @code{notdir} can be used to get a similar effect (@pxref{File Name
7893 Functions, , Functions for File Names}).  Note, however, that the
7894 @samp{F} variants all omit the trailing slash which always appears in
7895 the output of the @code{dir} function.  Here is a table of the variants:
7897 @table @samp
7898 @vindex $(@@D)
7899 @vindex @@D @r{(automatic variable)}
7900 @item $(@@D)
7901 The directory part of the file name of the target, with the trailing
7902 slash removed.  If the value of @samp{$@@} is @file{dir/foo.o} then
7903 @samp{$(@@D)} is @file{dir}.  This value is @file{.} if @samp{$@@} does
7904 not contain a slash.
7906 @vindex $(@@F)
7907 @vindex @@F @r{(automatic variable)}
7908 @item $(@@F)
7909 The file-within-directory part of the file name of the target.  If the
7910 value of @samp{$@@} is @file{dir/foo.o} then @samp{$(@@F)} is
7911 @file{foo.o}.  @samp{$(@@F)} is equivalent to @samp{$(notdir $@@)}.
7913 @vindex $(*D)
7914 @vindex *D @r{(automatic variable)}
7915 @item $(*D)
7916 @vindex $(*F)
7917 @vindex *F @r{(automatic variable)}
7918 @itemx $(*F)
7919 The directory part and the file-within-directory
7920 part of the stem; @file{dir} and @file{foo} in this example.
7922 @vindex $(%D)
7923 @vindex %D @r{(automatic variable)}
7924 @item $(%D)
7925 @vindex $(%F)
7926 @vindex %F @r{(automatic variable)}
7927 @itemx $(%F)
7928 The directory part and the file-within-directory part of the target
7929 archive member name.  This makes sense only for archive member targets
7930 of the form @file{@var{archive}(@var{member})} and is useful only when
7931 @var{member} may contain a directory name.  (@xref{Archive Members,
7932 ,Archive Members as Targets}.)
7934 @vindex $(<D)
7935 @vindex <D @r{(automatic variable)}
7936 @item $(<D)
7937 @vindex $(<F)
7938 @vindex <F @r{(automatic variable)}
7939 @itemx $(<F)
7940 The directory part and the file-within-directory
7941 part of the first dependency.
7943 @vindex $(^D)
7944 @vindex ^D @r{(automatic variable)}
7945 @item $(^D)
7946 @vindex $(^F)
7947 @vindex ^F @r{(automatic variable)}
7948 @itemx $(^F)
7949 Lists of the directory parts and the file-within-directory
7950 parts of all dependencies.
7952 @vindex $(?D)
7953 @vindex ?D @r{(automatic variable)}
7954 @item $(?D)
7955 @vindex $(?F)
7956 @vindex ?F @r{(automatic variable)}
7957 @itemx $(?F)
7958 Lists of the directory parts and the file-within-directory parts of
7959 all dependencies that are newer than the target.
7960 @end table
7962 Note that we use a special stylistic convention when we talk about these
7963 automatic variables; we write ``the value of @samp{$<}'', rather than
7964 @w{``the variable @code{<}''} as we would write for ordinary variables
7965 such as @code{objects} and @code{CFLAGS}.  We think this convention
7966 looks more natural in this special case.  Please do not assume it has a
7967 deep significance; @samp{$<} refers to the variable named @code{<} just
7968 as @samp{$(CFLAGS)} refers to the variable named @code{CFLAGS}.
7969 You could just as well use @samp{$(<)} in place of @samp{$<}.
7971 @node Pattern Match, Match-Anything Rules, Automatic, Pattern Rules
7972 @subsection How Patterns Match
7974 @cindex stem
7975 A target pattern is composed of a @samp{%} between a prefix and a suffix,
7976 either or both of which may be empty.  The pattern matches a file name only
7977 if the file name starts with the prefix and ends with the suffix, without
7978 overlap.  The text between the prefix and the suffix is called the
7979 @dfn{stem}.  Thus, when the pattern @samp{%.o} matches the file name
7980 @file{test.o}, the stem is @samp{test}.  The pattern rule dependencies are
7981 turned into actual file names by substituting the stem for the character
7982 @samp{%}.  Thus, if in the same example one of the dependencies is written
7983 as @samp{%.c}, it expands to @samp{test.c}.@refill
7985 When the target pattern does not contain a slash (and it usually does
7986 not), directory names in the file names are removed from the file name
7987 before it is compared with the target prefix and suffix.  After the
7988 comparison of the file name to the target pattern, the directory
7989 names, along with the slash that ends them, are added on to the
7990 dependency file names generated from the pattern rule's dependency
7991 patterns and the file name. The directories are ignored only for the
7992 purpose of finding an implicit rule to use, not in the application of
7993 that rule.  Thus, @samp{e%t} matches the file name @file{src/eat},
7994 with @samp{src/a} as the stem.  When dependencies are turned into file
7995 names, the directories from the stem are added at the front, while the
7996 rest of the stem is substituted for the @samp{%}.  The stem
7997 @samp{src/a} with a dependency pattern @samp{c%r} gives the file name
7998 @file{src/car}.@refill
8000 @node Match-Anything Rules, Canceling Rules, Pattern Match, Pattern Rules
8001 @subsection Match-Anything Pattern Rules
8003 @cindex match-anything rule
8004 @cindex terminal rule
8005 When a pattern rule's target is just @samp{%}, it matches any file name
8006 whatever.  We call these rules @dfn{match-anything} rules.  They are very
8007 useful, but it can take a lot of time for @code{make} to think about them,
8008 because it must consider every such rule for each file name listed either
8009 as a target or as a dependency.
8011 Suppose the makefile mentions @file{foo.c}.  For this target, @code{make}
8012 would have to consider making it by linking an object file @file{foo.c.o},
8013 or by C compilation-and-linking in one step from @file{foo.c.c}, or by
8014 Pascal compilation-and-linking from @file{foo.c.p}, and many other
8015 possibilities.
8017 We know these possibilities are ridiculous since @file{foo.c} is a C source
8018 file, not an executable.  If @code{make} did consider these possibilities,
8019 it would ultimately reject them, because files such as @file{foo.c.o} and
8020 @file{foo.c.p} would not exist.  But these possibilities are so
8021 numerous that @code{make} would run very slowly if it had to consider
8022 them.@refill
8024 To gain speed, we have put various constraints on the way @code{make}
8025 considers match-anything rules.  There are two different constraints that
8026 can be applied, and each time you define a match-anything rule you must
8027 choose one or the other for that rule.
8029 One choice is to mark the match-anything rule as @dfn{terminal} by defining
8030 it with a double colon.  When a rule is terminal, it does not apply unless
8031 its dependencies actually exist.  Dependencies that could be made with
8032 other implicit rules are not good enough.  In other words, no further
8033 chaining is allowed beyond a terminal rule.
8035 For example, the built-in implicit rules for extracting sources from RCS
8036 and SCCS files are terminal; as a result, if the file @file{foo.c,v} does
8037 not exist, @code{make} will not even consider trying to make it as an
8038 intermediate file from @file{foo.c,v.o} or from @file{RCS/SCCS/s.foo.c,v}.
8039 RCS and SCCS files are generally ultimate source files, which should not be
8040 remade from any other files; therefore, @code{make} can save time by not
8041 looking for ways to remake them.@refill
8043 If you do not mark the match-anything rule as terminal, then it is
8044 nonterminal.  A nonterminal match-anything rule cannot apply to a file name
8045 that indicates a specific type of data.  A file name indicates a specific
8046 type of data if some non-match-anything implicit rule target matches it.
8048 For example, the file name @file{foo.c} matches the target for the pattern
8049 rule @samp{%.c : %.y} (the rule to run Yacc).  Regardless of whether this
8050 rule is actually applicable (which happens only if there is a file
8051 @file{foo.y}), the fact that its target matches is enough to prevent
8052 consideration of any nonterminal match-anything rules for the file
8053 @file{foo.c}.  Thus, @code{make} will not even consider trying to make
8054 @file{foo.c} as an executable file from @file{foo.c.o}, @file{foo.c.c},
8055 @file{foo.c.p}, etc.@refill
8057 The motivation for this constraint is that nonterminal match-anything
8058 rules are used for making files containing specific types of data (such as
8059 executable files) and a file name with a recognized suffix indicates some
8060 other specific type of data (such as a C source file).
8062 Special built-in dummy pattern rules are provided solely to recognize
8063 certain file names so that nonterminal match-anything rules will not be
8064 considered.  These dummy rules have no dependencies and no commands, and
8065 they are ignored for all other purposes.  For example, the built-in
8066 implicit rule
8068 @example
8069 %.p :
8070 @end example
8072 @noindent
8073 exists to make sure that Pascal source files such as @file{foo.p} match a
8074 specific target pattern and thereby prevent time from being wasted looking
8075 for @file{foo.p.o} or @file{foo.p.c}.
8077 Dummy pattern rules such as the one for @samp{%.p} are made for every
8078 suffix listed as valid for use in suffix rules (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}).
8080 @node Canceling Rules,  , Match-Anything Rules, Pattern Rules
8081 @subsection Canceling Implicit Rules
8083 You can override a built-in implicit rule (or one you have defined
8084 yourself) by defining a new pattern rule with the same target and
8085 dependencies, but different commands.  When the new rule is defined, the
8086 built-in one is replaced.  The new rule's position in the sequence of
8087 implicit rules is determined by where you write the new rule.
8089 You can cancel a built-in implicit rule by defining a pattern rule with the
8090 same target and dependencies, but no commands.  For example, the following
8091 would cancel the rule that runs the assembler:
8093 @example
8094 %.o : %.s
8095 @end example
8097 @node Last Resort, Suffix Rules, Pattern Rules, Implicit Rules
8098 @section Defining Last-Resort Default Rules
8099 @cindex last-resort default rules
8100 @cindex default rules, last-resort
8102 You can define a last-resort implicit rule by writing a terminal
8103 match-anything pattern rule with no dependencies (@pxref{Match-Anything
8104 Rules}).  This is just like any other pattern rule; the only thing
8105 special about it is that it will match any target.  So such a rule's
8106 commands are used for all targets and dependencies that have no commands
8107 of their own and for which no other implicit rule applies.
8109 For example, when testing a makefile, you might not care if the source
8110 files contain real data, only that they exist.  Then you might do this:
8112 @example
8114         touch $@@
8115 @end example
8117 @noindent
8118 to cause all the source files needed (as dependencies) to be created
8119 automatically.
8121 @findex .DEFAULT
8122 You can instead define commands to be used for targets for which there
8123 are no rules at all, even ones which don't specify commands.  You do
8124 this by writing a rule for the target @code{.DEFAULT}.  Such a rule's
8125 commands are used for all dependencies which do not appear as targets in
8126 any explicit rule, and for which no implicit rule applies.  Naturally,
8127 there is no @code{.DEFAULT} rule unless you write one.
8129 If you use @code{.DEFAULT} with no commands or dependencies:
8131 @example
8132 .DEFAULT:
8133 @end example
8135 @noindent
8136 the commands previously stored for @code{.DEFAULT} are cleared.
8137 Then @code{make} acts as if you had never defined @code{.DEFAULT} at all.
8139 If you do not want a target to get the commands from a match-anything
8140 pattern rule or @code{.DEFAULT}, but you also do not want any commands
8141 to be run for the target, you can give it empty commands (@pxref{Empty
8142 Commands, ,Defining Empty Commands}).@refill
8144 You can use a last-resort rule to override part of another makefile.
8145 @xref{Overriding Makefiles, , Overriding Part of Another Makefile}.
8147 @node Suffix Rules, Implicit Rule Search, Last Resort, Implicit Rules
8148 @section Old-Fashioned Suffix Rules
8149 @cindex old-fashioned suffix rules
8150 @cindex suffix rule
8152 @dfn{Suffix rules} are the old-fashioned way of defining implicit rules for
8153 @code{make}.  Suffix rules are obsolete because pattern rules are more
8154 general and clearer.  They are supported in GNU @code{make} for
8155 compatibility with old makefiles.  They come in two kinds:
8156 @dfn{double-suffix} and @dfn{single-suffix}.@refill
8158 A double-suffix rule is defined by a pair of suffixes: the target suffix
8159 and the source suffix.  It matches any file whose name ends with the
8160 target suffix.  The corresponding implicit dependency is made by
8161 replacing the target suffix with the source suffix in the file name.  A
8162 two-suffix rule whose target and source suffixes are @samp{.o} and
8163 @samp{.c} is equivalent to the pattern rule @samp{%.o : %.c}.
8165 A single-suffix rule is defined by a single suffix, which is the source
8166 suffix.  It matches any file name, and the corresponding implicit
8167 dependency name is made by appending the source suffix.  A single-suffix
8168 rule whose source suffix is @samp{.c} is equivalent to the pattern rule
8169 @samp{% : %.c}.
8171 Suffix rule definitions are recognized by comparing each rule's target
8172 against a defined list of known suffixes.  When @code{make} sees a rule
8173 whose target is a known suffix, this rule is considered a single-suffix
8174 rule.  When @code{make} sees a rule whose target is two known suffixes
8175 concatenated, this rule is taken as a double-suffix rule.
8177 For example, @samp{.c} and @samp{.o} are both on the default list of
8178 known suffixes.  Therefore, if you define a rule whose target is
8179 @samp{.c.o}, @code{make} takes it to be a double-suffix rule with source
8180 suffix @samp{.c} and target suffix @samp{.o}.  Here is the old-fashioned
8181 way to define the rule for compiling a C source file:@refill
8183 @example
8184 .c.o:
8185         $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
8186 @end example
8188 Suffix rules cannot have any dependencies of their own.  If they have any,
8189 they are treated as normal files with funny names, not as suffix rules.
8190 Thus, the rule:
8192 @example
8193 .c.o: foo.h
8194         $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
8195 @end example
8197 @noindent
8198 tells how to make the file @file{.c.o} from the dependency file
8199 @file{foo.h}, and is not at all like the pattern rule:
8201 @example
8202 %.o: %.c foo.h
8203         $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
8204 @end example
8206 @noindent
8207 which tells how to make @samp{.o} files from @samp{.c} files, and makes all
8208 @samp{.o} files using this pattern rule also depend on @file{foo.h}.
8210 Suffix rules with no commands are also meaningless.  They do not remove
8211 previous rules as do pattern rules with no commands (@pxref{Canceling
8212 Rules, , Canceling Implicit Rules}).  They simply enter the suffix or pair of suffixes concatenated as
8213 a target in the data base.@refill
8215 @findex .SUFFIXES
8216 The known suffixes are simply the names of the dependencies of the special
8217 target @code{.SUFFIXES}.  You can add your own suffixes by writing a rule
8218 for @code{.SUFFIXES} that adds more dependencies, as in:
8220 @example
8221 .SUFFIXES: .hack .win
8222 @end example
8224 @noindent
8225 which adds @samp{.hack} and @samp{.win} to the end of the list of suffixes.
8227 If you wish to eliminate the default known suffixes instead of just adding
8228 to them, write a rule for @code{.SUFFIXES} with no dependencies.  By
8229 special dispensation, this eliminates all existing dependencies of
8230 @code{.SUFFIXES}.  You can then write another rule to add the suffixes you
8231 want.  For example,
8233 @example
8234 @group
8235 .SUFFIXES:            # @r{Delete the default suffixes}
8236 .SUFFIXES: .c .o .h   # @r{Define our suffix list}
8237 @end group
8238 @end example
8240 The @samp{-r} or @samp{--no-builtin-rules} flag causes the default
8241 list of suffixes to be empty.
8243 @vindex SUFFIXES
8244 The variable @code{SUFFIXES} is defined to the default list of suffixes
8245 before @code{make} reads any makefiles.  You can change the list of suffixes
8246 with a rule for the special target @code{.SUFFIXES}, but that does not alter
8247 this variable.
8249 @node Implicit Rule Search,  , Suffix Rules, Implicit Rules
8250 @section Implicit Rule Search Algorithm
8251 @cindex implicit rule, search algorithm
8252 @cindex search algorithm, implicit rule
8254 Here is the procedure @code{make} uses for searching for an implicit rule
8255 for a target @var{t}.  This procedure is followed for each double-colon
8256 rule with no commands, for each target of ordinary rules none of which have
8257 commands, and for each dependency that is not the target of any rule.  It
8258 is also followed recursively for dependencies that come from implicit
8259 rules, in the search for a chain of rules.
8261 Suffix rules are not mentioned in this algorithm because suffix rules are
8262 converted to equivalent pattern rules once the makefiles have been read in.
8264 For an archive member target of the form
8265 @samp{@var{archive}(@var{member})}, the following algorithm is run
8266 twice, first using the entire target name @var{t}, and second using
8267 @samp{(@var{member})} as the target @var{t} if the first run found no
8268 rule.@refill
8270 @enumerate
8271 @item
8272 Split @var{t} into a directory part, called @var{d}, and the rest,
8273 called @var{n}.  For example, if @var{t} is @samp{src/foo.o}, then
8274 @var{d} is @samp{src/} and @var{n} is @samp{foo.o}.@refill
8276 @item
8277 Make a list of all the pattern rules one of whose targets matches
8278 @var{t} or @var{n}.  If the target pattern contains a slash, it is
8279 matched against @var{t}; otherwise, against @var{n}.
8281 @item
8282 If any rule in that list is @emph{not} a match-anything rule, then
8283 remove all nonterminal match-anything rules from the list.
8285 @item
8286 Remove from the list all rules with no commands.
8288 @item
8289 For each pattern rule in the list:
8291 @enumerate a
8292 @item
8293 Find the stem @var{s}, which is the nonempty part of @var{t} or @var{n}
8294 matched by the @samp{%} in the target pattern.@refill
8296 @item
8297 Compute the dependency names by substituting @var{s} for @samp{%}; if
8298 the target pattern does not contain a slash, append @var{d} to
8299 the front of each dependency name.@refill
8301 @item
8302 Test whether all the dependencies exist or ought to exist.  (If a
8303 file name is mentioned in the makefile as a target or as an explicit
8304 dependency, then we say it ought to exist.)
8306 If all dependencies exist or ought to exist, or there are no dependencies,
8307 then this rule applies.
8308 @end enumerate
8310 @item
8311 If no pattern rule has been found so far, try harder.
8312 For each pattern rule in the list:
8314 @enumerate a
8315 @item
8316 If the rule is terminal, ignore it and go on to the next rule.
8318 @item
8319 Compute the dependency names as before.
8321 @item
8322 Test whether all the dependencies exist or ought to exist.
8324 @item
8325 For each dependency that does not exist, follow this algorithm
8326 recursively to see if the dependency can be made by an implicit
8327 rule.
8329 @item
8330 If all dependencies exist, ought to exist, or can be
8331 made by implicit rules, then this rule applies.
8332 @end enumerate
8334 @item
8335 If no implicit rule applies, the rule for @code{.DEFAULT}, if any,
8336 applies.  In that case, give @var{t} the same commands that
8337 @code{.DEFAULT} has.  Otherwise, there are no commands for @var{t}.
8338 @end enumerate
8340 Once a rule that applies has been found, for each target pattern of the
8341 rule other than the one that matched @var{t} or @var{n}, the @samp{%} in
8342 the pattern is replaced with @var{s} and the resultant file name is stored
8343 until the commands to remake the target file @var{t} are executed.  After
8344 these commands are executed, each of these stored file names are entered
8345 into the data base and marked as having been updated and having the same
8346 update status as the file @var{t}.
8348 When the commands of a pattern rule are executed for @var{t}, the automatic
8349 variables are set corresponding to the target and dependencies.
8350 @xref{Automatic, ,Automatic Variables}.
8352 @node Archives, Features, Implicit Rules, Top
8353 @chapter Using @code{make} to Update Archive Files
8354 @cindex archive
8356 @dfn{Archive files} are files containing named subfiles called
8357 @dfn{members}; they are maintained with the program @code{ar} and their
8358 main use is as subroutine libraries for linking.
8360 @menu
8361 * Archive Members::             Archive members as targets.
8362 * Archive Update::              The implicit rule for archive member targets.
8363 * Archive Pitfalls::            Dangers to watch out for when using archives.
8364 * Archive Suffix Rules::        You can write a special kind of suffix rule
8365                                   for updating archives.
8366 @end menu
8368 @node Archive Members, Archive Update,  , Archives
8369 @section Archive Members as Targets
8370 @cindex archive member targets
8372 An individual member of an archive file can be used as a target or
8373 dependency in @code{make}.  You specify the member named @var{member} in
8374 archive file @var{archive} as follows:
8376 @example
8377 @var{archive}(@var{member})
8378 @end example
8380 @noindent
8381 This construct is available only in targets and dependencies, not in
8382 commands!  Most programs that you might use in commands do not support this
8383 syntax and cannot act directly on archive members.  Only @code{ar} and
8384 other programs specifically designed to operate on archives can do so.
8385 Therefore, valid commands to update an archive member target probably must
8386 use @code{ar}.  For example, this rule says to create a member
8387 @file{hack.o} in archive @file{foolib} by copying the file @file{hack.o}:
8389 @example
8390 foolib(hack.o) : hack.o
8391         ar cr foolib hack.o
8392 @end example
8394 In fact, nearly all archive member targets are updated in just this way
8395 and there is an implicit rule to do it for you.  @strong{Note:} The
8396 @samp{c} flag to @code{ar} is required if the archive file does not
8397 already exist.
8399 To specify several members in the same archive, you can write all the
8400 member names together between the parentheses.  For example:
8402 @example
8403 foolib(hack.o kludge.o)
8404 @end example
8406 @noindent
8407 is equivalent to:
8409 @example
8410 foolib(hack.o) foolib(kludge.o)
8411 @end example
8413 @cindex wildcard, in archive member
8414 You can also use shell-style wildcards in an archive member reference.
8415 @xref{Wildcards, ,Using Wildcard Characters in File Names}.  For
8416 example, @w{@samp{foolib(*.o)}} expands to all existing members of the
8417 @file{foolib} archive whose names end in @samp{.o}; perhaps
8418 @samp{@w{foolib(hack.o)} @w{foolib(kludge.o)}}.
8420 @node Archive Update
8421 @section Implicit Rule for Archive Member Targets
8423 Recall that a target that looks like @file{@var{a}(@var{m})} stands for the
8424 member named @var{m} in the archive file @var{a}.
8426 When @code{make} looks for an implicit rule for such a target, as a special
8427 feature it considers implicit rules that match @file{(@var{m})}, as well as
8428 those that match the actual target @file{@var{a}(@var{m})}.
8430 This causes one special rule whose target is @file{(%)} to match.  This
8431 rule updates the target @file{@var{a}(@var{m})} by copying the file @var{m}
8432 into the archive.  For example, it will update the archive member target
8433 @file{foo.a(bar.o)} by copying the @emph{file} @file{bar.o} into the
8434 archive @file{foo.a} as a @emph{member} named @file{bar.o}.
8436 When this rule is chained with others, the result is very powerful.
8437 Thus, @samp{make "foo.a(bar.o)"} (the quotes are needed to protect the
8438 @samp{(} and @samp{)} from being interpreted specially by the shell) in
8439 the presence of a file @file{bar.c} is enough to cause the following
8440 commands to be run, even without a makefile:
8442 @example
8443 cc -c bar.c -o bar.o
8444 ar r foo.a bar.o
8445 rm -f bar.o
8446 @end example
8448 @noindent
8449 Here @code{make} has envisioned the file @file{bar.o} as an intermediate
8450 file.  @xref{Chained Rules, ,Chains of Implicit Rules}.
8452 Implicit rules such as this one are written using the automatic variable
8453 @samp{$%}.  @xref{Automatic, ,Automatic Variables}.
8455 An archive member name in an archive cannot contain a directory name, but
8456 it may be useful in a makefile to pretend that it does.  If you write an
8457 archive member target @file{foo.a(dir/file.o)}, @code{make} will perform
8458 automatic updating with this command:
8460 @example
8461 ar r foo.a dir/file.o
8462 @end example
8464 @noindent
8465 which has the effect of copying the file @file{dir/file.o} into a member
8466 named @file{file.o}.  In connection with such usage, the automatic variables
8467 @code{%D} and @code{%F} may be useful.
8469 @menu
8470 * Archive Symbols::             How to update archive symbol directories.
8471 @end menu
8473 @node Archive Symbols,  ,  , Archive Update
8474 @subsection Updating Archive Symbol Directories
8475 @cindex @code{__.SYMDEF}
8476 @cindex updating archive symbol directories
8477 @cindex archive symbol directory updating
8478 @cindex symbol directories, updating archive
8479 @cindex directories, updating archive symbol
8481 An archive file that is used as a library usually contains a special member
8482 named @file{__.SYMDEF} that contains a directory of the external symbol
8483 names defined by all the other members.  After you update any other
8484 members, you need to update @file{__.SYMDEF} so that it will summarize the
8485 other members properly.  This is done by running the @code{ranlib} program:
8487 @example
8488 ranlib @var{archivefile}
8489 @end example
8491 Normally you would put this command in the rule for the archive file,
8492 and make all the members of the archive file dependencies of that rule.
8493 For example,
8495 @example
8496 libfoo.a: libfoo.a(x.o) libfoo.a(y.o) @dots{}
8497         ranlib libfoo.a
8498 @end example
8500 @noindent
8501 The effect of this is to update archive members @file{x.o}, @file{y.o},
8502 etc., and then update the symbol directory member @file{__.SYMDEF} by
8503 running @code{ranlib}.  The rules for updating the members are not shown
8504 here; most likely you can omit them and use the implicit rule which copies
8505 files into the archive, as described in the preceding section.
8507 This is not necessary when using the GNU @code{ar} program, which
8508 updates the @file{__.SYMDEF} member automatically.
8510 @node Archive Pitfalls
8511 @section Dangers When Using Archives
8512 @cindex archive, and parallel execution
8513 @cindex parallel execution, and archive update
8514 @cindex archive, and @code{-j}
8515 @cindex @code{-j}, and archive update
8517 It is important to be careful when using parallel execution (the
8518 @code{-j} switch; @pxref{Parallel, ,Parallel Execution}) and archives.
8519 If multiple @code{ar} commands run at the same time on the same archive
8520 file, they will not know about each other and can corrupt the file.
8522 Possibly a future version of @code{make} will provide a mechanism to
8523 circumvent this problem by serializing all commands that operate on the
8524 same archive file.  But for the time being, you must either write your
8525 makefiles to avoid this problem in some other way, or not use @code{-j}.
8527 @node Archive Suffix Rules, , Archive Pitfalls, Archives
8528 @section Suffix Rules for Archive Files
8529 @cindex suffix rule, for archive
8530 @cindex archive, suffix rule for
8531 @cindex library archive, suffix rule for
8532 @cindex @code{.a} (archives)
8534 You can write a special kind of suffix rule for dealing with archive
8535 files.  @xref{Suffix Rules}, for a full explanation of suffix rules.
8536 Archive suffix rules are obsolete in GNU @code{make}, because pattern
8537 rules for archives are a more general mechanism (@pxref{Archive
8538 Update}).  But they are retained for compatibility with other
8539 @code{make}s.
8541 To write a suffix rule for archives, you simply write a suffix rule
8542 using the target suffix @samp{.a} (the usual suffix for archive files).
8543 For example, here is the old-fashioned suffix rule to update a library
8544 archive from C source files:
8546 @example
8547 @group
8548 .c.a:
8549         $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o
8550         $(AR) r $@@ $*.o
8551         $(RM) $*.o
8552 @end group
8553 @end example
8555 @noindent
8556 This works just as if you had written the pattern rule:
8558 @example
8559 @group
8560 (%.o): %.c
8561         $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o
8562         $(AR) r $@@ $*.o
8563         $(RM) $*.o
8564 @end group
8565 @end example
8567 In fact, this is just what @code{make} does when it sees a suffix rule
8568 with @samp{.a} as the target suffix.  Any double-suffix rule
8569 @w{@samp{.@var{x}.a}} is converted to a pattern rule with the target
8570 pattern @samp{(%.o)} and a dependency pattern of @samp{%.@var{x}}.
8572 Since you might want to use @samp{.a} as the suffix for some other kind
8573 of file, @code{make} also converts archive suffix rules to pattern rules
8574 in the normal way (@pxref{Suffix Rules}).  Thus a double-suffix rule
8575 @w{@samp{.@var{x}.a}} produces two pattern rules: @samp{@w{(%.o):}
8576 @w{%.@var{x}}} and @samp{@w{%.a}: @w{%.@var{x}}}.@refill
8578 @node Features, Missing, Archives, Top
8579 @chapter Features of GNU @code{make}
8580 @cindex features of GNU @code{make}
8581 @cindex portability
8582 @cindex compatibility
8584 Here is a summary of the features of GNU @code{make}, for comparison
8585 with and credit to other versions of @code{make}.  We consider the
8586 features of @code{make} in 4.2 BSD systems as a baseline.  If you are
8587 concerned with writing portable makefiles, you should not use the
8588 features of @code{make} listed here, nor the ones in @ref{Missing}.
8590 Many features come from the version of @code{make} in System V.
8592 @itemize @bullet
8593 @item
8594 The @code{VPATH} variable and its special meaning.
8595 @xref{Directory Search, , Searching Directories for Dependencies}.
8596 This feature exists in System V @code{make}, but is undocumented.
8597 It is documented in 4.3 BSD @code{make} (which says it mimics System V's
8598 @code{VPATH} feature).@refill
8600 @item
8601 Included makefiles.  @xref{Include, ,Including Other Makefiles}.
8602 Allowing multiple files to be included with a single directive is a GNU
8603 extension.
8605 @item
8606 Variables are read from and communicated via the environment.
8607 @xref{Environment, ,Variables from the Environment}.
8609 @item
8610 Options passed through the variable @code{MAKEFLAGS} to recursive
8611 invocations of @code{make}.
8612 @xref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
8614 @item
8615 The automatic variable @code{$%} is set to the member name
8616 in an archive reference.  @xref{Automatic, ,Automatic Variables}.
8618 @item
8619 The automatic variables @code{$@@}, @code{$*}, @code{$<}, @code{$%},
8620 and @code{$?} have corresponding forms like @code{$(@@F)} and
8621 @code{$(@@D)}.  We have generalized this to @code{$^} as an obvious
8622 extension.  @xref{Automatic, ,Automatic Variables}.@refill
8624 @item
8625 Substitution variable references.
8626 @xref{Reference, ,Basics of Variable References}.
8628 @item
8629 The command-line options @samp{-b} and @samp{-m}, accepted and
8630 ignored.  In System V @code{make}, these options actually do something.
8632 @item
8633 Execution of recursive commands to run @code{make} via the variable
8634 @code{MAKE} even if @samp{-n}, @samp{-q} or @samp{-t} is specified.
8635 @xref{Recursion, ,Recursive Use of @code{make}}.
8637 @item
8638 Support for suffix @samp{.a} in suffix rules.  @xref{Archive Suffix
8639 Rules}.  This feature is obsolete in GNU @code{make}, because the
8640 general feature of rule chaining (@pxref{Chained Rules, ,Chains of
8641 Implicit Rules}) allows one pattern rule for installing members in an
8642 archive (@pxref{Archive Update}) to be sufficient.
8644 @item
8645 The arrangement of lines and backslash-newline combinations in
8646 commands is retained when the commands are printed, so they appear as
8647 they do in the makefile, except for the stripping of initial
8648 whitespace.
8649 @end itemize
8651 The following features were inspired by various other versions of
8652 @code{make}.  In some cases it is unclear exactly which versions inspired
8653 which others.
8655 @itemize @bullet
8656 @item
8657 Pattern rules using @samp{%}.
8658 This has been implemented in several versions of @code{make}.
8659 We're not sure who invented it first, but it's been spread around a bit.
8660 @xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.@refill
8662 @item
8663 Rule chaining and implicit intermediate files.
8664 This was implemented by Stu Feldman in his version of @code{make}
8665 for AT&T Eighth Edition Research Unix, and later by Andrew Hume of
8666 AT&T Bell Labs in his @code{mk} program (where he terms it
8667 ``transitive closure'').  We do not really know if
8668 we got this from either of them or thought it up ourselves at the
8669 same time.  @xref{Chained Rules, ,Chains of Implicit Rules}.
8671 @item
8672 The automatic variable @code{$^} containing a list of all dependencies
8673 of the current target.  We did not invent this, but we have no idea who
8674 did.  @xref{Automatic, ,Automatic Variables}.  The automatic variable
8675 @code{$+} is a simple extension of @code{$^}.
8677 @item
8678 The ``what if'' flag (@samp{-W} in GNU @code{make}) was (as far as we know)
8679 invented by Andrew Hume in @code{mk}.
8680 @xref{Instead of Execution, ,Instead of Executing the Commands}.
8682 @item
8683 The concept of doing several things at once (parallelism) exists in
8684 many incarnations of @code{make} and similar programs, though not in the
8685 System V or BSD implementations.  @xref{Execution, ,Command Execution}.
8687 @item
8688 Modified variable references using pattern substitution come from
8689 SunOS 4.  @xref{Reference, ,Basics of Variable References}.
8690 This functionality was provided in GNU @code{make} by the
8691 @code{patsubst} function before the alternate syntax was implemented
8692 for compatibility with SunOS 4.  It is not altogether clear who
8693 inspired whom, since GNU @code{make} had @code{patsubst} before SunOS
8694 4 was released.@refill
8696 @item
8697 The special significance of @samp{+} characters preceding command lines
8698 (@pxref{Instead of Execution, ,Instead of Executing the Commands}) is
8699 mandated by
8700 @cite{IEEE Standard 1003.2-1992} (POSIX.2).
8702 @item
8703 The @samp{+=} syntax to append to the value of a variable comes from SunOS
8704 4 @code{make}.  @xref{Appending, , Appending More Text to Variables}.
8706 @item
8707 The syntax @w{@samp{@var{archive}(@var{mem1} @var{mem2}@dots{})}} to list
8708 multiple members in a single archive file comes from SunOS 4 @code{make}.
8709 @xref{Archive Members}.
8711 @item
8712 The @code{-include} directive to include makefiles with no error for a
8713 nonexistent file comes from SunOS 4 @code{make}.  (But note that SunOS 4
8714 @code{make} does not allow multiple makefiles to be specified in one
8715 @code{-include} directive.)  The same feature appears with the name
8716 @code{sinclude} in SGI @code{make} and perhaps others.
8717 @end itemize
8719 The remaining features are inventions new in GNU @code{make}:
8721 @itemize @bullet
8722 @item
8723 Use the @samp{-v} or @samp{--version} option to print version and
8724 copyright information.
8726 @item
8727 Use the @samp{-h} or @samp{--help} option to summarize the options to
8728 @code{make}.
8730 @item
8731 Simply-expanded variables.  @xref{Flavors, ,The Two Flavors of Variables}.
8733 @item
8734 Pass command-line variable assignments automatically through the
8735 variable @code{MAKE} to recursive @code{make} invocations.
8736 @xref{Recursion, ,Recursive Use of @code{make}}.
8738 @item
8739 Use the @samp{-C} or @samp{--directory} command option to change
8740 directory.  @xref{Options Summary, ,Summary of Options}.
8742 @item
8743 Make verbatim variable definitions with @code{define}.
8744 @xref{Defining, ,Defining Variables Verbatim}.
8746 @item
8747 Declare phony targets with the special target @code{.PHONY}.
8749 Andrew Hume of AT&T Bell Labs implemented a similar feature with a
8750 different syntax in his @code{mk} program.  This seems to be a case of
8751 parallel discovery.  @xref{Phony Targets, ,Phony Targets}.
8753 @item
8754 Manipulate text by calling functions.
8755 @xref{Functions, ,Functions for Transforming Text}.
8757 @item
8758 Use the @samp{-o} or @samp{--old-file}
8759 option to pretend a file's modification-time is old.
8760 @xref{Avoiding Compilation, ,Avoiding Recompilation of Some Files}.
8762 @item
8763 Conditional execution.
8765 This feature has been implemented numerous times in various versions
8766 of @code{make}; it seems a natural extension derived from the features
8767 of the C preprocessor and similar macro languages and is not a
8768 revolutionary concept.  @xref{Conditionals, ,Conditional Parts of Makefiles}.
8770 @item
8771 Specify a search path for included makefiles.
8772 @xref{Include, ,Including Other Makefiles}.
8774 @item
8775 Specify extra makefiles to read with an environment variable.
8776 @xref{MAKEFILES Variable, ,The Variable @code{MAKEFILES}}.
8778 @item
8779 Strip leading sequences of @samp{./} from file names, so that
8780 @file{./@var{file}} and @file{@var{file}} are considered to be the
8781 same file.@refill
8783 @item
8784 Use a special search method for library dependencies written in the
8785 form @samp{-l@var{name}}.
8786 @xref{Libraries/Search, ,Directory Search for Link Libraries}.
8788 @item
8789 Allow suffixes for suffix rules
8790 (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}) to contain any
8791 characters.  In other versions of @code{make}, they must begin with
8792 @samp{.} and not contain any @samp{/} characters.
8794 @item
8795 Keep track of the current level of @code{make} recursion using the
8796 variable @code{MAKELEVEL}.  @xref{Recursion, ,Recursive Use of @code{make}}.
8798 @item
8799 Provide any goals given on the command line in the variable
8800 @code{MAKECMDGOALS}.  @xref{Goals, ,Arguments to Specify the Goals}.
8802 @item
8803 Specify static pattern rules.  @xref{Static Pattern, ,Static Pattern Rules}.
8805 @item
8806 Provide selective @code{vpath} search.
8807 @xref{Directory Search, ,Searching Directories for Dependencies}.
8809 @item
8810 Provide computed variable references.
8811 @xref{Reference, ,Basics of Variable References}.
8813 @item
8814 Update makefiles.  @xref{Remaking Makefiles, ,How Makefiles Are Remade}.
8815 System V @code{make} has a very, very limited form of this
8816 functionality in that it will check out SCCS files for makefiles.
8818 @item
8819 Various new built-in implicit rules.
8820 @xref{Catalogue of Rules, ,Catalogue of Implicit Rules}.
8822 @item
8823 The built-in variable @samp{MAKE_VERSION} gives the version number of
8824 @code{make}.
8825 @end itemize
8827 @node Missing, Makefile Conventions, Features, Top
8828 @chapter Incompatibilities and Missing Features
8829 @cindex incompatibilities
8830 @cindex missing features
8831 @cindex features, missing
8833 The @code{make} programs in various other systems support a few features
8834 that are not implemented in GNU @code{make}.  The POSIX.2 standard
8835 (@cite{IEEE Standard 1003.2-1992}) which specifies @code{make} does not
8836 require any of these features.@refill
8838 @itemize @bullet
8839 @item
8840 A target of the form @samp{@var{file}((@var{entry}))} stands for a member
8841 of archive file @var{file}.  The member is chosen, not by name, but by
8842 being an object file which defines the linker symbol @var{entry}.@refill
8844 This feature was not put into GNU @code{make} because of the
8845 nonmodularity of putting knowledge into @code{make} of the internal
8846 format of archive file symbol tables.
8847 @xref{Archive Symbols, ,Updating Archive Symbol Directories}.
8849 @item
8850 Suffixes (used in suffix rules) that end with the character @samp{~}
8851 have a special meaning to System V @code{make};
8852 they refer to the SCCS file that corresponds
8853 to the file one would get without the @samp{~}.  For example, the
8854 suffix rule @samp{.c~.o} would make the file @file{@var{n}.o} from
8855 the SCCS file @file{s.@var{n}.c}.  For complete coverage, a whole
8856 series of such suffix rules is required.
8857 @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
8859 In GNU @code{make}, this entire series of cases is handled by two
8860 pattern rules for extraction from SCCS, in combination with the
8861 general feature of rule chaining.
8862 @xref{Chained Rules, ,Chains of Implicit Rules}.
8864 @item
8865 In System V @code{make}, the string @samp{$$@@} has the strange meaning
8866 that, in the dependencies of a rule with multiple targets, it stands
8867 for the particular target that is being processed.
8869 This is not defined in GNU @code{make} because @samp{$$} should always
8870 stand for an ordinary @samp{$}.
8872 It is possible to get portions of this functionality through the use of
8873 static pattern rules (@pxref{Static Pattern, ,Static Pattern Rules}).
8874 The System V @code{make} rule:
8876 @example
8877 $(targets): $$@@.o lib.a
8878 @end example
8880 @noindent
8881 can be replaced with the GNU @code{make} static pattern rule:
8883 @example
8884 $(targets): %: %.o lib.a
8885 @end example
8887 @item
8888 In System V and 4.3 BSD @code{make}, files found by @code{VPATH} search
8889 (@pxref{Directory Search, ,Searching Directories for Dependencies}) have their names changed inside command
8890 strings.  We feel it is much cleaner to always use automatic variables
8891 and thus make this feature obsolete.@refill
8893 @item
8894 In some Unix @code{make}s, the automatic variable @code{$*} appearing in
8895 the dependencies of a rule has the amazingly strange ``feature'' of
8896 expanding to the full name of the @emph{target of that rule}.  We cannot
8897 imagine what went on in the minds of Unix @code{make} developers to do
8898 this; it is utterly inconsistent with the normal definition of @code{$*}.
8899 @vindex * @r{(automatic variable), unsupported bizarre usage}
8901 @item
8902 In some Unix @code{make}s, implicit rule search
8903 (@pxref{Implicit Rules, ,Using Implicit Rules}) is apparently done for
8904 @emph{all} targets, not just those without commands.  This means you can
8905 do:@refill
8907 @example
8908 @group
8909 foo.o:
8910         cc -c foo.c
8911 @end group
8912 @end example
8914 @noindent
8915 and Unix @code{make} will intuit that @file{foo.o} depends on
8916 @file{foo.c}.@refill
8918 We feel that such usage is broken.  The dependency properties of
8919 @code{make} are well-defined (for GNU @code{make}, at least),
8920 and doing such a thing simply does not fit the model.@refill
8922 @item
8923 GNU @code{make} does not include any built-in implicit rules for
8924 compiling or preprocessing EFL programs.  If we hear of anyone who is
8925 using EFL, we will gladly add them.
8927 @item
8928 It appears that in SVR4 @code{make}, a suffix rule can be specified with
8929 no commands, and it is treated as if it had empty commands
8930 (@pxref{Empty Commands}).  For example:
8932 @example
8933 .c.a:
8934 @end example
8936 @noindent
8937 will override the built-in @file{.c.a} suffix rule.
8939 We feel that it is cleaner for a rule without commands to always simply
8940 add to the dependency list for the target.  The above example can be
8941 easily rewritten to get the desired behavior in GNU @code{make}:
8943 @example
8944 .c.a: ;
8945 @end example
8947 @item
8948 Some versions of @code{make} invoke the shell with the @samp{-e} flag,
8949 except under @samp{-k} (@pxref{Testing, ,Testing the Compilation of a
8950 Program}).  The @samp{-e} flag tells the shell to exit as soon as any
8951 program it runs returns a nonzero status.  We feel it is cleaner to
8952 write each shell command line to stand on its own and not require this
8953 special treatment.
8954 @end itemize
8956 @comment The makefile standards are in a separate file that is also
8957 @comment included by standards.texi.
8958 @include make-stds.texi
8960 @node Quick Reference, Make Errors, Makefile Conventions, Top
8961 @appendix Quick Reference
8963 This appendix summarizes the directives, text manipulation functions,
8964 and special variables which GNU @code{make} understands.
8965 @xref{Special Targets}, @ref{Catalogue of Rules, ,Catalogue of Implicit Rules},
8966 and @ref{Options Summary, ,Summary of Options},
8967 for other summaries.
8969 Here is a summary of the directives GNU @code{make} recognizes:
8971 @table @code
8972 @item define @var{variable}
8973 @itemx endef
8975 Define a multi-line, recursively-expanded variable.@*
8976 @xref{Sequences}.
8978 @item ifdef @var{variable}
8979 @itemx ifndef @var{variable}
8980 @itemx ifeq (@var{a},@var{b})
8981 @itemx ifeq "@var{a}" "@var{b}"
8982 @itemx ifeq '@var{a}' '@var{b}'
8983 @itemx ifneq (@var{a},@var{b})
8984 @itemx ifneq "@var{a}" "@var{b}"
8985 @itemx ifneq '@var{a}' '@var{b}'
8986 @itemx else
8987 @itemx endif
8989 Conditionally evaluate part of the makefile.@*
8990 @xref{Conditionals}.
8992 @item include @var{file}
8993 @itemx -include @var{file}
8994 @itemx sinclude @var{file}
8996 Include another makefile.@*
8997 @xref{Include, ,Including Other Makefiles}.
8999 @item override @var{variable} = @var{value}
9000 @itemx override @var{variable} := @var{value}
9001 @itemx override @var{variable} += @var{value}
9002 @itemx override @var{variable} ?= @var{value}
9003 @itemx override define @var{variable}
9004 @itemx endef
9006 Define a variable, overriding any previous definition, even one from
9007 the command line.@*
9008 @xref{Override Directive, ,The @code{override} Directive}.
9010 @item export
9012 Tell @code{make} to export all variables to child processes by default.@*
9013 @xref{Variables/Recursion, , Communicating Variables to a Sub-@code{make}}.
9015 @item export @var{variable}
9016 @itemx export @var{variable} = @var{value}
9017 @itemx export @var{variable} := @var{value}
9018 @itemx export @var{variable} += @var{value}
9019 @itemx export @var{variable} ?= @var{value}
9020 @itemx unexport @var{variable}
9021 Tell @code{make} whether or not to export a particular variable to child
9022 processes.@*
9023 @xref{Variables/Recursion, , Communicating Variables to a Sub-@code{make}}.
9025 @item vpath @var{pattern} @var{path}
9026 Specify a search path for files matching a @samp{%} pattern.@*
9027 @xref{Selective Search, , The @code{vpath} Directive}.
9029 @item vpath @var{pattern}
9030 Remove all search paths previously specified for @var{pattern}.
9032 @item vpath
9033 Remove all search paths previously specified in any @code{vpath}
9034 directive.
9035 @end table
9037 Here is a summary of the text manipulation functions (@pxref{Functions}):
9039 @table @code
9040 @item $(subst @var{from},@var{to},@var{text})
9041 Replace @var{from} with @var{to} in @var{text}.@*
9042 @xref{Text Functions, , Functions for String Substitution and Analysis}.
9044 @item $(patsubst @var{pattern},@var{replacement},@var{text})
9045 Replace words matching @var{pattern} with @var{replacement} in @var{text}.@*
9046 @xref{Text Functions, , Functions for String Substitution and Analysis}.
9048 @item $(strip @var{string})
9049 Remove excess whitespace characters from @var{string}.@*
9050 @xref{Text Functions, , Functions for String Substitution and Analysis}.
9052 @item $(findstring @var{find},@var{text})
9053 Locate @var{find} in @var{text}.@*
9054 @xref{Text Functions, , Functions for String Substitution and Analysis}.
9056 @item $(filter @var{pattern}@dots{},@var{text})
9057 Select words in @var{text} that match one of the @var{pattern} words.@*
9058 @xref{Text Functions, , Functions for String Substitution and Analysis}.
9060 @item $(filter-out @var{pattern}@dots{},@var{text})
9061 Select words in @var{text} that @emph{do not} match any of the @var{pattern} words.@*
9062 @xref{Text Functions, , Functions for String Substitution and Analysis}.
9064 @item $(sort @var{list})
9065 Sort the words in @var{list} lexicographically, removing duplicates.@*
9066 @xref{Text Functions, , Functions for String Substitution and Analysis}.
9068 @item $(dir @var{names}@dots{})
9069 Extract the directory part of each file name.@*
9070 @xref{File Name Functions, ,Functions for File Names}.
9072 @item $(notdir @var{names}@dots{})
9073 Extract the non-directory part of each file name.@*
9074 @xref{File Name Functions, ,Functions for File Names}.
9076 @item $(suffix @var{names}@dots{})
9077 Extract the suffix (the last @samp{.} and following characters) of each file name.@*
9078 @xref{File Name Functions, ,Functions for File Names}.
9080 @item $(basename @var{names}@dots{})
9081 Extract the base name (name without suffix) of each file name.@*
9082 @xref{File Name Functions, ,Functions for File Names}.
9084 @item $(addsuffix @var{suffix},@var{names}@dots{})
9085 Append @var{suffix} to each word in @var{names}.@*
9086 @xref{File Name Functions, ,Functions for File Names}.
9088 @item $(addprefix @var{prefix},@var{names}@dots{})
9089 Prepend @var{prefix} to each word in @var{names}.@*
9090 @xref{File Name Functions, ,Functions for File Names}.
9092 @item $(join @var{list1},@var{list2})
9093 Join two parallel lists of words.@*
9094 @xref{File Name Functions, ,Functions for File Names}.
9096 @item $(word @var{n},@var{text})
9097 Extract the @var{n}th word (one-origin) of @var{text}.@*
9098 @xref{File Name Functions, ,Functions for File Names}.
9100 @item $(words @var{text})
9101 Count the number of words in @var{text}.@*
9102 @xref{File Name Functions, ,Functions for File Names}.
9104 @item $(wordlist @var{s},@var{e},@var{text})
9105 Returns the list of words in @var{text} from @var{s} to @var{e}.@*
9106 @xref{File Name Functions, ,Functions for File Names}.
9108 @item $(firstword @var{names}@dots{})
9109 Extract the first word of @var{names}.@*
9110 @xref{File Name Functions, ,Functions for File Names}.
9112 @item $(wildcard @var{pattern}@dots{})
9113 Find file names matching a shell file name pattern (@emph{not} a
9114 @samp{%} pattern).@*
9115 @xref{Wildcard Function, ,The Function @code{wildcard}}.
9117 @item $(error @var{text}@dots{})
9119 When this function is evaluated, @code{make} generates a fatal error
9120 with the message @var{text}.@*
9121 @xref{Make Control Functions, ,Functions That Control Make}.
9123 @item $(warning @var{text}@dots{})
9125 When this function is evaluated, @code{make} generates a warning with
9126 the message @var{text}.@*
9127 @xref{Make Control Functions, ,Functions That Control Make}.
9129 @item $(shell @var{command})
9131 Execute a shell command and return its output.@*
9132 @xref{Shell Function, , The @code{shell} Function}.
9134 @item $(origin @var{variable})
9136 Return a string describing how the @code{make} variable @var{variable} was
9137 defined.@*
9138 @xref{Origin Function, , The @code{origin} Function}.
9140 @item $(foreach @var{var},@var{words},@var{text})
9142 Evaluate @var{text} with @var{var} bound to each word in @var{words},
9143 and concatenate the results.@*
9144 @xref{Foreach Function, ,The @code{foreach} Function}.
9146 @item $(call @var{var},@var{param},@dots{})
9148 Evaluate the variable @var{var} replacing any references to @var{$(1)},
9149 @var{$(2)} with the first, second, etc. @var{param} values.@*
9150 @xref{Call Function, ,The @code{call} Function}.
9151 @end table
9153 Here is a summary of the automatic variables.
9154 @xref{Automatic, ,Automatic Variables},
9155 for full information.
9157 @table @code
9158 @item $@@
9159 The file name of the target.
9161 @item $%
9162 The target member name, when the target is an archive member.
9164 @item $<
9165 The name of the first dependency.
9167 @item $?
9168 The names of all the dependencies that are
9169 newer than the target, with spaces between them.
9170 For dependencies which are archive members, only
9171 the member named is used (@pxref{Archives}).
9173 @item $^
9174 @itemx $+
9175 The names of all the dependencies, with spaces between them.  For
9176 dependencies which are archive members, only the member named is used
9177 (@pxref{Archives}).  The value of @code{$^} omits duplicate
9178 dependencies, while @code{$+} retains them and preserves their order.
9180 @item $*
9181 The stem with which an implicit rule matches
9182 (@pxref{Pattern Match, ,How Patterns Match}).
9184 @item $(@@D)
9185 @itemx $(@@F)
9186 The directory part and the file-within-directory part of @code{$@@}.
9188 @item $(*D)
9189 @itemx $(*F)
9190 The directory part and the file-within-directory part of @code{$*}.
9192 @item $(%D)
9193 @itemx $(%F)
9194 The directory part and the file-within-directory part of @code{$%}.
9196 @item $(<D)
9197 @itemx $(<F)
9198 The directory part and the file-within-directory part of @code{$<}.
9200 @item $(^D)
9201 @itemx $(^F)
9202 The directory part and the file-within-directory part of @code{$^}.
9204 @item $(+D)
9205 @itemx $(+F)
9206 The directory part and the file-within-directory part of @code{$+}.
9208 @item $(?D)
9209 @itemx $(?F)
9210 The directory part and the file-within-directory part of @code{$?}.
9211 @end table
9213 These variables are used specially by GNU @code{make}:
9215 @table @code
9216 @item MAKEFILES
9218 Makefiles to be read on every invocation of @code{make}.@*
9219 @xref{MAKEFILES Variable, ,The Variable @code{MAKEFILES}}.
9221 @item VPATH
9223 Directory search path for files not found in the current directory.@*
9224 @xref{General Search, , @code{VPATH} Search Path for All Dependencies}.
9226 @item SHELL
9228 The name of the system default command interpreter, usually @file{/bin/sh}.
9229 You can set @code{SHELL} in the makefile to change the shell used to run
9230 commands.  @xref{Execution, ,Command Execution}.
9232 @item MAKESHELL
9234 On MS-DOS only, the name of the command interpreter that is to be used
9235 by @code{make}. This value takes precedence over the value of
9236 @code{SHELL}.  @xref{Execution, ,MAKESHELL variable}.
9238 @item MAKE
9240 The name with which @code{make} was invoked.
9241 Using this variable in commands has special meaning.
9242 @xref{MAKE Variable, ,How the @code{MAKE} Variable Works}.
9244 @item MAKELEVEL
9246 The number of levels of recursion (sub-@code{make}s).@*
9247 @xref{Variables/Recursion}.
9249 @item MAKEFLAGS
9251 The flags given to @code{make}.  You can set this in the environment or
9252 a makefile to set flags.@*
9253 @xref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
9255 @item MAKECMDGOALS
9257 The targets given to @code{make} on the command line.  Setting this
9258 variable has no effect on the operation of @code{make}.@*
9259 @xref{Goals, ,Arguments to Specify the Goals}.
9261 @item CURDIR
9263 Set to the pathname of the current working directory (after all
9264 @code{-C} options are processed, if any).  Setting this variable has no
9265 effect on the operation of @code{make}.@*
9266 @xref{Recursion, ,Recursive Use of @code{make}}.
9268 @item SUFFIXES
9270 The default list of suffixes before @code{make} reads any makefiles.
9272 @item .LIBPATTERNS
9273 Defines the naming of the libraries @code{make} searches for, and their
9274 order.@*
9275 @xref{Libraries/Search, ,Directory Search for Link Libraries}.
9276 @end table
9278 @node Make Errors, Complex Makefile, Quick Reference, Top
9279 @comment  node-name,  next,  previous,  up
9280 @appendix Errors Generated by Make
9282 Here is a list of the most common errors you might see generated by
9283 @code{make}, and some information about what they mean and how to fix
9284 them.
9286 Sometimes @code{make} errors are not fatal, especially in the presence
9287 of a @code{-} prefix on a command script line, or the @code{-k} command
9288 line option.  Errors that are fatal are prefixed with the string
9289 @code{***}.
9291 Error messages are all either prefixed with the name of the program
9292 (usually @samp{make}), or, if the error is found in a makefile, the name
9293 of the file and linenumber containing the problem.
9295 In the table below, these common prefixes are left off.
9297 @table @samp
9299 @item [@var{foo}] Error @var{NN}
9300 @itemx [@var{foo}] @var{signal description}
9301 These errors are not really @code{make} errors at all.  They mean that a
9302 program that @code{make} invoked as part of a command script returned a
9303 non-0 error code (@samp{Error @var{NN}}), which @code{make} interprets
9304 as failure, or it exited in some other abnormal fashion (with a
9305 signal of some type).  @xref{Errors, ,Errors in Commands}.
9307 If no @code{***} is attached to the message, then the subprocess failed
9308 but the rule in the makefile was prefixed with the @code{-} special
9309 character, so @code{make} ignored the error.
9311 @item missing separator.  Stop.
9312 This means that @code{make} could not understand much of anything about
9313 the command line it just read.  GNU @code{make} looks for various kinds
9314 of separators (@code{:}, @code{=}, TAB characters, etc.) to help it
9315 decide what kind of commandline it's seeing.  This means it couldn't
9316 find a valid one.
9318 One of the most common reasons for this message is that you (or perhaps
9319 your oh-so-helpful editor, as is the case with many MS-Windows editors)
9320 have attempted to indent your command scripts with spaces instead of a
9321 TAB character.  Remember that every line in the command script must
9322 begin with a TAB character.  Eight spaces do not count.  @xref{Rule Syntax}.
9324 @item commands commence before first target.  Stop.
9325 @itemx missing rule before commands.  Stop.
9326 This means the first thing in the makefile seems to be part of a command
9327 script: it begins with a TAB character and doesn't appear to be a legal
9328 @code{make} command (such as a variable assignment).  Command scripts
9329 must always be associated with a target.
9331 The second form is generated if the line has a semicolon as the first
9332 non-whitespace character; @code{make} interprets this to mean you left
9333 out the "target: dependency" section of a rule.  @xref{Rule Syntax}.
9335 @item No rule to make target `@var{xxx}'.
9336 @itemx No rule to make target `@var{xxx}', needed by `@var{yyy}'.
9337 This means that @code{make} decided it needed to build a target, but
9338 then couldn't find any instructions in the makefile on how to do that,
9339 either explicit or implicit (including in the default rules database).
9341 If you want that file to be built, you will need to add a rule to your
9342 makefile describing how that target can be built.  Other possible
9343 sources of this problem are typos in the makefile (if that filename is
9344 wrong) or a corrupted source tree (if that file is not supposed to be
9345 built, but rather only a dependency).
9347 @item No targets specified and no makefile found.  Stop.
9348 @itemx No targets.  Stop.
9349 The former means that you didn't provide any targets to be built on the
9350 command line, and @code{make} couldn't find any makefiles to read in.
9351 The latter means that some makefile was found, but it didn't contain any
9352 default target and none was given on the command line.  GNU @code{make}
9353 has nothing to do in these situations.
9354 @xref{Makefile Arguments, ,Arguments to Specify the Makefile}.@refill
9356 @item Makefile `@var{xxx}' was not found.
9357 @itemx Included makefile `@var{xxx}' was not found.
9358 A makefile specified on the command line (first form) or included
9359 (second form) was not found.
9361 @item warning: overriding commands for target `@var{xxx}'
9362 @itemx warning: ignoring old commands for target `@var{xxx}'
9363 GNU @code{make} allows commands to be specified only once per target
9364 (except for double-colon rules).  If you give commands for a target
9365 which already has been defined to have commands, this warning is issued
9366 and the second set of commands will overwrite the first set.
9367 @xref{Multiple Rules, ,Multiple Rules for One Target}.
9369 @item Circular @var{xxx} <- @var{yyy} dependency dropped.
9370 This means that @code{make} detected a loop in the dependency graph:
9371 after tracing the dependency @var{yyy} of target @var{xxx}, and its
9372 dependencies, etc., one of them depended on @var{xxx} again.
9374 @item Recursive variable `@var{xxx}' references itself (eventually).  Stop.
9375 This means you've defined a normal (recursive) @code{make} variable
9376 @var{xxx} that, when its expanded, will refer to itself (@var{xxx}).
9377 This is not allowed; either use simply-expanded variables (@code{:=}) or
9378 use the append operator (@code{+=}).  @xref{Using Variables, ,How to Use
9379 Variables}.
9381 @item Unterminated variable reference.  Stop.
9382 This means you forgot to provide the proper closing parenthesis
9383 or brace in your variable or function reference.
9385 @item insufficient arguments to function `@var{xxx}'.  Stop.
9386 This means you haven't provided the requisite number of arguments for
9387 this function.  See the documentation of the function for a description
9388 of its arguments.  @xref{Functions, ,Functions for Transforming Text}.
9390 @item missing target pattern.  Stop.
9391 @itemx multiple target patterns.  Stop.
9392 @itemx target pattern contains no `%'.  Stop.
9393 These are generated for malformed static pattern rules.  The first means
9394 there's no pattern in the target section of the rule, the second means
9395 there are multiple patterns in the target section, and the third means
9396 the target doesn't contain a pattern character (@code{%}).  @xref{Static
9397 Usage, ,Syntax of Static Pattern Rules}.
9399 @end table
9401 @node Complex Makefile, Concept Index, Make Errors, Top
9402 @appendix Complex Makefile Example
9404 Here is the makefile for the GNU @code{tar} program.  This is a
9405 moderately complex makefile.
9407 Because it is the first target, the default goal is @samp{all}.  An
9408 interesting feature of this makefile is that @file{testpad.h} is a
9409 source file automatically created by the @code{testpad} program,
9410 itself compiled from @file{testpad.c}.
9412 If you type @samp{make} or @samp{make all}, then @code{make} creates
9413 the @file{tar} executable, the @file{rmt} daemon that provides
9414 remote tape access, and the @file{tar.info} Info file.
9416 If you type @samp{make install}, then @code{make} not only creates
9417 @file{tar}, @file{rmt}, and @file{tar.info}, but also installs
9418 them.
9420 If you type @samp{make clean}, then @code{make} removes the @samp{.o}
9421 files, and the @file{tar}, @file{rmt}, @file{testpad},
9422 @file{testpad.h}, and @file{core} files.
9424 If you type @samp{make distclean}, then @code{make} not only removes
9425 the same files as does @samp{make clean} but also the
9426 @file{TAGS}, @file{Makefile}, and @file{config.status} files.
9427 (Although it is not evident, this makefile (and
9428 @file{config.status}) is generated by the user with the
9429 @code{configure} program, which is provided in the @code{tar}
9430 distribution, but is not shown here.)
9432 If you type @samp{make realclean}, then @code{make} removes the same
9433 files as does @samp{make distclean} and also removes the Info files
9434 generated from @file{tar.texinfo}.
9436 In addition, there are targets @code{shar} and @code{dist} that create
9437 distribution kits.
9439 @example
9440 @group
9441 # Generated automatically from Makefile.in by configure.
9442 # Un*x Makefile for GNU tar program.
9443 # Copyright (C) 1991 Free Software Foundation, Inc.
9444 @end group
9446 @group
9447 # This program is free software; you can redistribute
9448 # it and/or modify it under the terms of the GNU
9449 # General Public License @dots{}
9450 @dots{}
9451 @dots{}
9452 @end group
9454 SHELL = /bin/sh
9456 #### Start of system configuration section. ####
9458 srcdir = .
9460 @group
9461 # If you use gcc, you should either run the
9462 # fixincludes script that comes with it or else use
9463 # gcc with the -traditional option.  Otherwise ioctl
9464 # calls will be compiled incorrectly on some systems.
9465 CC = gcc -O
9466 YACC = bison -y
9467 INSTALL = /usr/local/bin/install -c
9468 INSTALLDATA = /usr/local/bin/install -c -m 644
9469 @end group
9471 # Things you might add to DEFS:
9472 # -DSTDC_HEADERS        If you have ANSI C headers and
9473 #                       libraries.
9474 # -DPOSIX               If you have POSIX.1 headers and
9475 #                       libraries.
9476 # -DBSD42               If you have sys/dir.h (unless
9477 #                       you use -DPOSIX), sys/file.h,
9478 #                       and st_blocks in `struct stat'.
9479 # -DUSG                 If you have System V/ANSI C
9480 #                       string and memory functions
9481 #                       and headers, sys/sysmacros.h,
9482 #                       fcntl.h, getcwd, no valloc,
9483 #                       and ndir.h (unless
9484 #                       you use -DDIRENT).
9485 # -DNO_MEMORY_H         If USG or STDC_HEADERS but do not
9486 #                       include memory.h.
9487 # -DDIRENT              If USG and you have dirent.h
9488 #                       instead of ndir.h.
9489 # -DSIGTYPE=int         If your signal handlers
9490 #                       return int, not void.
9491 # -DNO_MTIO             If you lack sys/mtio.h
9492 #                       (magtape ioctls).
9493 # -DNO_REMOTE           If you do not have a remote shell
9494 #                       or rexec.
9495 # -DUSE_REXEC           To use rexec for remote tape
9496 #                       operations instead of
9497 #                       forking rsh or remsh.
9498 # -DVPRINTF_MISSING     If you lack vprintf function
9499 #                       (but have _doprnt).
9500 # -DDOPRNT_MISSING      If you lack _doprnt function.
9501 #                       Also need to define
9502 #                       -DVPRINTF_MISSING.
9503 # -DFTIME_MISSING       If you lack ftime system call.
9504 # -DSTRSTR_MISSING      If you lack strstr function.
9505 # -DVALLOC_MISSING      If you lack valloc function.
9506 # -DMKDIR_MISSING       If you lack mkdir and
9507 #                       rmdir system calls.
9508 # -DRENAME_MISSING      If you lack rename system call.
9509 # -DFTRUNCATE_MISSING   If you lack ftruncate
9510 #                       system call.
9511 # -DV7                  On Version 7 Unix (not
9512 #                       tested in a long time).
9513 # -DEMUL_OPEN3          If you lack a 3-argument version
9514 #                       of open, and want to emulate it
9515 #                       with system calls you do have.
9516 # -DNO_OPEN3            If you lack the 3-argument open
9517 #                       and want to disable the tar -k
9518 #                       option instead of emulating open.
9519 # -DXENIX               If you have sys/inode.h
9520 #                       and need it 94 to be included.
9522 DEFS =  -DSIGTYPE=int -DDIRENT -DSTRSTR_MISSING \
9523         -DVPRINTF_MISSING -DBSD42
9524 # Set this to rtapelib.o unless you defined NO_REMOTE,
9525 # in which case make it empty.
9526 RTAPELIB = rtapelib.o
9527 LIBS =
9528 DEF_AR_FILE = /dev/rmt8
9529 DEFBLOCKING = 20
9531 @group
9532 CDEBUG = -g
9533 CFLAGS = $(CDEBUG) -I. -I$(srcdir) $(DEFS) \
9534         -DDEF_AR_FILE=\"$(DEF_AR_FILE)\" \
9535         -DDEFBLOCKING=$(DEFBLOCKING)
9536 LDFLAGS = -g
9537 @end group
9539 @group
9540 prefix = /usr/local
9541 # Prefix for each installed program,
9542 # normally empty or `g'.
9543 binprefix =
9545 # The directory to install tar in.
9546 bindir = $(prefix)/bin
9548 # The directory to install the info files in.
9549 infodir = $(prefix)/info
9550 @end group
9552 #### End of system configuration section. ####
9554 SRC1 =  tar.c create.c extract.c buffer.c \
9555         getoldopt.c update.c gnu.c mangle.c
9556 SRC2 =  version.c list.c names.c diffarch.c \
9557         port.c wildmat.c getopt.c
9558 SRC3 =  getopt1.c regex.c getdate.y
9559 SRCS =  $(SRC1) $(SRC2) $(SRC3)
9560 OBJ1 =  tar.o create.o extract.o buffer.o \
9561         getoldopt.o update.o gnu.o mangle.o
9562 OBJ2 =  version.o list.o names.o diffarch.o \
9563         port.o wildmat.o getopt.o
9564 OBJ3 =  getopt1.o regex.o getdate.o $(RTAPELIB)
9565 OBJS =  $(OBJ1) $(OBJ2) $(OBJ3)
9566 @group
9567 AUX =   README COPYING ChangeLog Makefile.in  \
9568         makefile.pc configure configure.in \
9569         tar.texinfo tar.info* texinfo.tex \
9570         tar.h port.h open3.h getopt.h regex.h \
9571         rmt.h rmt.c rtapelib.c alloca.c \
9572         msd_dir.h msd_dir.c tcexparg.c \
9573         level-0 level-1 backup-specs testpad.c
9574 @end group
9576 all:    tar rmt tar.info
9578 @group
9579 tar:    $(OBJS)
9580         $(CC) $(LDFLAGS) -o $@@ $(OBJS) $(LIBS)
9581 @end group
9583 @group
9584 rmt:    rmt.c
9585         $(CC) $(CFLAGS) $(LDFLAGS) -o $@@ rmt.c
9586 @end group
9588 @group
9589 tar.info: tar.texinfo
9590         makeinfo tar.texinfo
9591 @end group
9593 @group
9594 install: all
9595         $(INSTALL) tar $(bindir)/$(binprefix)tar
9596         -test ! -f rmt || $(INSTALL) rmt /etc/rmt
9597         $(INSTALLDATA) $(srcdir)/tar.info* $(infodir)
9598 @end group
9600 @group
9601 $(OBJS): tar.h port.h testpad.h
9602 regex.o buffer.o tar.o: regex.h
9603 # getdate.y has 8 shift/reduce conflicts.
9604 @end group
9606 @group
9607 testpad.h: testpad
9608         ./testpad
9609 @end group
9611 @group
9612 testpad: testpad.o
9613         $(CC) -o $@@ testpad.o
9614 @end group
9616 @group
9617 TAGS:   $(SRCS)
9618         etags $(SRCS)
9619 @end group
9621 @group
9622 clean:
9623         rm -f *.o tar rmt testpad testpad.h core
9624 @end group
9626 @group
9627 distclean: clean
9628         rm -f TAGS Makefile config.status
9629 @end group
9631 @group
9632 realclean: distclean
9633         rm -f tar.info*
9634 @end group
9636 @group
9637 shar: $(SRCS) $(AUX)
9638         shar $(SRCS) $(AUX) | compress \
9639           > tar-`sed -e '/version_string/!d' \
9640                      -e 's/[^0-9.]*\([0-9.]*\).*/\1/' \
9641                      -e q
9642                      version.c`.shar.Z
9643 @end group
9645 @group
9646 dist: $(SRCS) $(AUX)
9647         echo tar-`sed \
9648              -e '/version_string/!d' \
9649              -e 's/[^0-9.]*\([0-9.]*\).*/\1/' \
9650              -e q
9651              version.c` > .fname
9652         -rm -rf `cat .fname`
9653         mkdir `cat .fname`
9654         ln $(SRCS) $(AUX) `cat .fname`
9655         tar chZf `cat .fname`.tar.Z `cat .fname`
9656         -rm -rf `cat .fname` .fname
9657 @end group
9659 @group
9660 tar.zoo: $(SRCS) $(AUX)
9661         -rm -rf tmp.dir
9662         -mkdir tmp.dir
9663         -rm tar.zoo
9664         for X in $(SRCS) $(AUX) ; do \
9665             echo $$X ; \
9666             sed 's/$$/^M/' $$X \
9667             > tmp.dir/$$X ; done
9668         cd tmp.dir ; zoo aM ../tar.zoo *
9669         -rm -rf tmp.dir
9670 @end group
9671 @end example
9673 @node Concept Index, Name Index, Complex Makefile, Top
9674 @unnumbered Index of Concepts
9676 @printindex cp
9678 @node Name Index,  , Concept Index, Top
9679 @unnumbered Index of Functions, Variables, & Directives
9681 @printindex fn
9683 @summarycontents
9684 @contents
9685 @bye