Imported Upstream version 4.0
[platform/upstream/make.git] / doc / make.texi
1 \input texinfo                @c -*- Texinfo -*-
2 @c %**start of header
3 @setfilename make.info
4
5 @include version.texi
6 @set EDITION 0.72
7
8 @settitle GNU @code{make}
9 @setchapternewpage odd
10 @c Combine the variable and function indices:
11 @syncodeindex vr fn
12 @c Combine the program and concept indices:
13 @syncodeindex pg cp
14 @c FSF publishers: format makebook.texi instead of using this file directly.
15 @c ISBN confirmed by Jasimin Huang <jasimin@fsf.org> on 25 Mar 2009
16 @set ISBN 1-882114-83-3
17 @c %**end of header
18
19 @copying
20 This file documents the GNU @code{make} utility, which determines
21 automatically which pieces of a large program need to be recompiled,
22 and issues the commands to recompile them.
23
24 This is Edition @value{EDITION}, last updated @value{UPDATED},
25 of @cite{The GNU Make Manual}, for GNU @code{make} version @value{VERSION}.
26
27 Copyright @copyright{} 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
28 1996, 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005, 2006, 2007,
29 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
30
31 @quotation
32 Permission is granted to copy, distribute and/or modify this document
33 under the terms of the GNU Free Documentation License, Version 1.3 or
34 any later version published by the Free Software Foundation; with no
35 Invariant Sections, with the Front-Cover Texts being ``A GNU Manual,''
36 and with the Back-Cover Texts as in (a) below.  A copy of the
37 license is included in the section entitled ``GNU Free Documentation
38 License.''
39
40 (a) The FSF's Back-Cover Text is: ``You have the freedom to copy and
41 modify this GNU manual.  Buying copies from the FSF supports it in
42 developing GNU and promoting software freedom.''
43 @end quotation
44 @end copying
45
46 @c finalout
47
48 @c ISPELL CHECK: done, 10 June 1993 --roland
49 @c ISPELL CHECK: done, 2000-06-25 --Martin Buchholz
50
51
52 @dircategory Software development
53 @direntry
54 * Make: (make).            Remake files automatically.
55 @end direntry
56
57 @iftex
58 @shorttitlepage GNU Make
59 @end iftex
60 @titlepage
61 @title GNU Make
62 @subtitle A Program for Directing Recompilation
63 @subtitle GNU @code{make} Version @value{VERSION}
64 @subtitle @value{UPDATED-MONTH}
65 @author Richard M. Stallman, Roland McGrath, Paul D. Smith
66 @page
67 @vskip 0pt plus 1filll
68 @insertcopying
69 @sp 2
70 Published by the Free Software Foundation @*
71 51 Franklin St. -- Fifth Floor @*
72 Boston, MA 02110-1301 USA @*
73 ISBN @value{ISBN} @*
74 @sp 2
75 Cover art by Etienne Suvasa.
76 @end titlepage
77
78 @summarycontents
79 @contents
80
81 @ifnottex
82 @node Top, Overview, (dir), (dir)
83 @top GNU @code{make}
84
85 @insertcopying
86 @end ifnottex
87
88 @menu
89 * Overview::                    Overview of @code{make}.
90 * Introduction::                An introduction to @code{make}.
91 * Makefiles::                   Makefiles tell @code{make} what to do.
92 * Rules::                       Rules describe when a file must be remade.
93 * Recipes::                     Recipes say how to remake a file.
94 * Using Variables::             You can use variables to avoid repetition.
95 * Conditionals::                Use or ignore parts of the makefile based
96                                   on the values of variables.
97 * Functions::                   Many powerful ways to manipulate text.
98 * Invoking make: Running.       How to invoke @code{make} on the command line.
99 * Implicit Rules::              Use implicit rules to treat many files alike,
100                                   based on their file names.
101 * Archives::                    How @code{make} can update library archives.
102 * Extending make::              Using extensions to @code{make}.
103 * Features::                    Features GNU @code{make} has over other @code{make}s.
104 * Missing::                     What GNU @code{make} lacks from other @code{make}s.
105 * Makefile Conventions::        Conventions for writing makefiles for
106                                   GNU programs.
107 * Quick Reference::             A quick reference for experienced users.
108 * Error Messages::              A list of common errors generated by @code{make}.
109 * Complex Makefile::            A real example of a straightforward,
110                                   but nontrivial, makefile.
111
112 * GNU Free Documentation License::  License for copying this manual.
113 * Concept Index::               Index of Concepts.
114 * Name Index::                  Index of Functions, Variables, & Directives.
115
116 @detailmenu
117  --- The Detailed Node Listing ---
118
119 Overview of @code{make}
120
121 * Preparing::                   Preparing and running @code{make}.
122 * Reading::                     On reading this text.
123 * Bugs::                        Problems and bugs.
124
125 An Introduction to Makefiles
126
127 * Rule Introduction::           What a rule looks like.
128 * Simple Makefile::             A simple makefile.
129 * How Make Works::              How @code{make} processes this makefile.
130 * Variables Simplify::          Variables make makefiles simpler.
131 * make Deduces::                Letting @code{make} deduce the recipes.
132 * Combine By Prerequisite::     Another style of makefile.
133 * Cleanup::                     Rules for cleaning the directory.
134
135 Writing Makefiles
136
137 * Makefile Contents::           What makefiles contain.
138 * Makefile Names::              How to name your makefile.
139 * Include::                     How one makefile can use another makefile.
140 * MAKEFILES Variable::          The environment can specify extra makefiles.
141 * Remaking Makefiles::          How makefiles get remade.
142 * Overriding Makefiles::        How to override part of one makefile
143                                   with another makefile.
144 * Reading Makefiles::           How makefiles are parsed.
145 * Secondary Expansion::         How and when secondary expansion is performed.
146
147 What Makefiles Contain
148
149 * Splitting Lines::             Splitting long lines in makefiles
150
151 Writing Rules
152
153 * Rule Example::                An example explained.
154 * Rule Syntax::                 General syntax explained.
155 * Prerequisite Types::          There are two types of prerequisites.
156 * Wildcards::                   Using wildcard characters such as `*'.
157 * Directory Search::            Searching other directories for source files.
158 * Phony Targets::               Using a target that is not a real file's name.
159 * Force Targets::               You can use a target without a recipe
160                                   or prerequisites to mark other targets
161                                   as phony.
162 * Empty Targets::               When only the date matters and the
163                                   files are empty.
164 * Special Targets::             Targets with special built-in meanings.
165 * Multiple Targets::            When to make use of several targets in a rule.
166 * Multiple Rules::              How to use several rules with the same target.
167 * Static Pattern::              Static pattern rules apply to multiple targets
168                                   and can vary the prerequisites according to
169                                   the target name.
170 * Double-Colon::                How to use a special kind of rule to allow
171                                   several independent rules for one target.
172 * Automatic Prerequisites::     How to automatically generate rules giving
173                                   prerequisites from source files themselves.
174
175 Using Wildcard Characters in File Names
176
177 * Wildcard Examples::           Several examples.
178 * Wildcard Pitfall::            Problems to avoid.
179 * Wildcard Function::           How to cause wildcard expansion where
180                                   it does not normally take place.
181
182 Searching Directories for Prerequisites
183
184 * General Search::              Specifying a search path that applies
185                                   to every prerequisite.
186 * Selective Search::            Specifying a search path
187                                   for a specified class of names.
188 * Search Algorithm::            When and how search paths are applied.
189 * Recipes/Search::              How to write recipes that work together
190                                   with search paths.
191 * Implicit/Search::             How search paths affect implicit rules.
192 * Libraries/Search::            Directory search for link libraries.
193
194 Static Pattern Rules
195
196 * Static Usage::                The syntax of static pattern rules.
197 * Static versus Implicit::      When are they better than implicit rules?
198
199 Writing Recipes in Rules
200
201 * Recipe Syntax::               Recipe syntax features and pitfalls.
202 * Echoing::                     How to control when recipes are echoed.
203 * Execution::                   How recipes are executed.
204 * Parallel::                    How recipes can be executed in parallel.
205 * Errors::                      What happens after a recipe execution error.
206 * Interrupts::                  What happens when a recipe is interrupted.
207 * Recursion::                   Invoking @code{make} from makefiles.
208 * Canned Recipes::              Defining canned recipes.
209 * Empty Recipes::               Defining useful, do-nothing recipes.
210
211 Recipe Syntax
212
213 * Splitting Recipe Lines::      Breaking long recipe lines for readability.
214 * Variables in Recipes::        Using @code{make} variables in recipes.
215
216 Recipe Execution
217
218 * One Shell::                   One shell for all lines in a recipe.
219 * Choosing the Shell::          How @code{make} chooses the shell used
220                                   to run recipes.
221
222 Parallel Execution
223
224 * Parallel Output::             Handling output during parallel execution
225 * Parallel Input::              Handling input during parallel execution
226
227 Recursive Use of @code{make}
228
229 * MAKE Variable::               The special effects of using @samp{$(MAKE)}.
230 * Variables/Recursion::         How to communicate variables to a sub-@code{make}.
231 * Options/Recursion::           How to communicate options to a sub-@code{make}.
232 * -w Option::                   How the @samp{-w} or @samp{--print-directory} option
233                                   helps debug use of recursive @code{make} commands.
234
235 How to Use Variables
236
237 * Reference::                   How to use the value of a variable.
238 * Flavors::                     Variables come in two flavors.
239 * Advanced::                    Advanced features for referencing a variable.
240 * Values::                      All the ways variables get their values.
241 * Setting::                     How to set a variable in the makefile.
242 * Appending::                   How to append more text to the old value
243                                   of a variable.
244 * Override Directive::          How to set a variable in the makefile even if
245                                   the user has set it with a command argument.
246 * Multi-Line::                  An alternate way to set a variable
247                                   to a multi-line string.
248 * Undefine Directive::          How to undefine a variable so that it appears
249                                   as if it was never set.
250 * Environment::                 Variable values can come from the environment.
251 * Target-specific::             Variable values can be defined on a per-target
252                                   basis.
253 * Pattern-specific::            Target-specific variable values can be applied
254                                   to a group of targets that match a pattern.
255 * Suppressing Inheritance::     Suppress inheritance of variables.
256 * Special Variables::           Variables with special meaning or behavior.
257
258 Advanced Features for Reference to Variables
259
260 * Substitution Refs::           Referencing a variable with
261                                   substitutions on the value.
262 * Computed Names::              Computing the name of the variable to refer to.
263
264 Conditional Parts of Makefiles
265
266 * Conditional Example::         Example of a conditional
267 * Conditional Syntax::          The syntax of conditionals.
268 * Testing Flags::               Conditionals that test flags.
269
270 Functions for Transforming Text
271
272 * Syntax of Functions::         How to write a function call.
273 * Text Functions::              General-purpose text manipulation functions.
274 * File Name Functions::         Functions for manipulating file names.
275 * Conditional Functions::       Functions that implement conditions.
276 * Foreach Function::            Repeat some text with controlled variation.
277 * File Function::               Write text to a file.
278 * Call Function::               Expand a user-defined function.
279 * Value Function::              Return the un-expanded value of a variable.
280 * Eval Function::               Evaluate the arguments as makefile syntax.
281 * Origin Function::             Find where a variable got its value.
282 * Flavor Function::             Find out the flavor of a variable.
283 * Make Control Functions::      Functions that control how make runs.
284 * Shell Function::              Substitute the output of a shell command.
285 * Guile Function::              Use GNU Guile embedded scripting language.
286
287 How to Run @code{make}
288
289 * Makefile Arguments::          How to specify which makefile to use.
290 * Goals::                       How to use goal arguments to specify which
291                                   parts of the makefile to use.
292 * Instead of Execution::        How to use mode flags to specify what
293                                   kind of thing to do with the recipes
294                                   in the makefile other than simply
295                                   execute them.
296 * Avoiding Compilation::        How to avoid recompiling certain files.
297 * Overriding::                  How to override a variable to specify
298                                   an alternate compiler and other things.
299 * Testing::                     How to proceed past some errors, to
300                                   test compilation.
301 * Options Summary::             Summary of Options
302
303 Using Implicit Rules
304
305 * Using Implicit::              How to use an existing implicit rule
306                                   to get the recipes for updating a file.
307 * Catalogue of Rules::          A list of built-in implicit rules.
308 * Implicit Variables::          How to change what predefined rules do.
309 * Chained Rules::               How to use a chain of implicit rules.
310 * Pattern Rules::               How to define new implicit rules.
311 * Last Resort::                 How to define a recipe for rules which
312                                   cannot find any.
313 * Suffix Rules::                The old-fashioned style of implicit rule.
314 * Implicit Rule Search::        The precise algorithm for applying
315                                   implicit rules.
316
317 Defining and Redefining Pattern Rules
318
319 * Pattern Intro::               An introduction to pattern rules.
320 * Pattern Examples::            Examples of pattern rules.
321 * Automatic Variables::         How to use automatic variables in the
322                                   recipe of implicit rules.
323 * Pattern Match::               How patterns match.
324 * Match-Anything Rules::        Precautions you should take prior to
325                                   defining rules that can match any
326                                   target file whatever.
327 * Canceling Rules::             How to override or cancel built-in rules.
328
329 Using @code{make} to Update Archive Files
330
331 * Archive Members::             Archive members as targets.
332 * Archive Update::              The implicit rule for archive member targets.
333 * Archive Pitfalls::            Dangers to watch out for when using archives.
334 * Archive Suffix Rules::        You can write a special kind of suffix rule
335                                   for updating archives.
336
337 Implicit Rule for Archive Member Targets
338
339 * Archive Symbols::             How to update archive symbol directories.
340
341 Extending GNU @code{make}
342
343 * Guile Integration::           Using Guile as an embedded scripting language.
344 * Loading Objects::             Loading dynamic objects as extensions.
345
346 GNU Guile Integration
347
348 * Guile Types::                 Converting Guile types to @code{make} strings.
349 * Guile Interface::             Invoking @code{make} functions from Guile.
350 * Guile Example::               Example using Guile in @code{make}.
351
352 Loading Dynamic Objects
353
354 * load Directive::              Loading dynamic objects as extensions.
355 * Remaking Loaded Objects::     How loaded objects get remade.
356 * Loaded Object API::           Programmatic interface for loaded objects.
357 * Loaded Object Example::       Example of a loaded object
358
359 @end detailmenu
360 @end menu
361
362 @node Overview, Introduction, Top, Top
363 @comment  node-name,  next,  previous,  up
364 @chapter Overview of @code{make}
365
366 The @code{make} utility automatically determines which pieces of a large
367 program need to be recompiled, and issues commands to recompile them.
368 This manual describes GNU @code{make}, which was implemented by Richard
369 Stallman and Roland McGrath.  Development since Version 3.76 has been
370 handled by Paul D. Smith.
371
372 GNU @code{make} conforms to section 6.2 of @cite{IEEE Standard
373 1003.2-1992} (POSIX.2).
374 @cindex POSIX
375 @cindex IEEE Standard 1003.2
376 @cindex standards conformance
377
378 Our examples show C programs, since they are most common, but you can use
379 @code{make} with any programming language whose compiler can be run with a
380 shell command.  Indeed, @code{make} is not limited to programs.  You can
381 use it to describe any task where some files must be updated automatically
382 from others whenever the others change.
383
384 @menu
385 * Preparing::                   Preparing and running @code{make}.
386 * Reading::                     On reading this text.
387 * Bugs::                        Problems and bugs.
388 @end menu
389
390 @node Preparing, Reading, Overview, Overview
391 @ifnottex
392 @heading Preparing and Running Make
393 @end ifnottex
394
395 To prepare to use @code{make}, you must write a file called
396 the @dfn{makefile} that describes the relationships among files
397 in your program and provides commands for updating each file.
398 In a program, typically, the executable file is updated from object
399 files, which are in turn made by compiling source files.@refill
400
401 Once a suitable makefile exists, each time you change some source files,
402 this simple shell command:
403
404 @example
405 make
406 @end example
407
408 @noindent
409 suffices to perform all necessary recompilations.  The @code{make} program
410 uses the makefile data base and the last-modification times of the files to
411 decide which of the files need to be updated.  For each of those files, it
412 issues the recipes recorded in the data base.
413
414 You can provide command line arguments to @code{make} to control which
415 files should be recompiled, or how.  @xref{Running, ,How to Run
416 @code{make}}.
417
418 @node Reading, Bugs, Preparing, Overview
419 @section How to Read This Manual
420
421 If you are new to @code{make}, or are looking for a general
422 introduction, read the first few sections of each chapter, skipping the
423 later sections.  In each chapter, the first few sections contain
424 introductory or general information and the later sections contain
425 specialized or technical information.
426 @ifnottex
427 The exception is the second chapter, @ref{Introduction, ,An
428 Introduction to Makefiles}, all of which is introductory.
429 @end ifnottex
430 @iftex
431 The exception is @ref{Introduction, ,An Introduction to Makefiles},
432 all of which is introductory.
433 @end iftex
434
435 If you are familiar with other @code{make} programs, see @ref{Features,
436 ,Features of GNU @code{make}}, which lists the enhancements GNU
437 @code{make} has, and @ref{Missing, ,Incompatibilities and Missing
438 Features}, which explains the few things GNU @code{make} lacks that
439 others have.
440
441 For a quick summary, see @ref{Options Summary}, @ref{Quick Reference},
442 and @ref{Special Targets}.
443
444 @node Bugs,  , Reading, Overview
445 @section Problems and Bugs
446 @cindex reporting bugs
447 @cindex bugs, reporting
448 @cindex problems and bugs, reporting
449
450 If you have problems with GNU @code{make} or think you've found a bug,
451 please report it to the developers; we cannot promise to do anything but
452 we might well want to fix it.
453
454 Before reporting a bug, make sure you've actually found a real bug.
455 Carefully reread the documentation and see if it really says you can do
456 what you're trying to do.  If it's not clear whether you should be able
457 to do something or not, report that too; it's a bug in the
458 documentation!
459
460 Before reporting a bug or trying to fix it yourself, try to isolate it
461 to the smallest possible makefile that reproduces the problem.  Then
462 send us the makefile and the exact results @code{make} gave you,
463 including any error or warning messages.  Please don't paraphrase
464 these messages: it's best to cut and paste them into your report.
465 When generating this small makefile, be sure to not use any non-free
466 or unusual tools in your recipes: you can almost always emulate what
467 such a tool would do with simple shell commands.  Finally, be sure to
468 explain what you expected to occur; this will help us decide whether
469 the problem was really in the documentation.
470
471 Once you have a precise problem you can report it in one of two ways.
472 Either send electronic mail to:
473
474 @example
475     bug-make@@gnu.org
476 @end example
477
478 @noindent
479 or use our Web-based project management tool, at:
480
481 @example
482     http://savannah.gnu.org/projects/make/
483 @end example
484
485 @noindent
486 In addition to the information above, please be careful to include the
487 version number of @code{make} you are using.  You can get this
488 information with the command @samp{make --version}.  Be sure also to
489 include the type of machine and operating system you are using.  One
490 way to obtain this information is by looking at the final lines of
491 output from the command @samp{make --help}.
492
493 @node Introduction, Makefiles, Overview, Top
494 @comment  node-name,  next,  previous,  up
495 @chapter An Introduction to Makefiles
496
497 You need a file called a @dfn{makefile} to tell @code{make} what to do.
498 Most often, the makefile tells @code{make} how to compile and link a
499 program.
500 @cindex makefile
501
502 In this chapter, we will discuss a simple makefile that describes how to
503 compile and link a text editor which consists of eight C source files
504 and three header files.  The makefile can also tell @code{make} how to
505 run miscellaneous commands when explicitly asked (for example, to remove
506 certain files as a clean-up operation).  To see a more complex example
507 of a makefile, see @ref{Complex Makefile}.
508
509 When @code{make} recompiles the editor, each changed C source file
510 must be recompiled.  If a header file has changed, each C source file
511 that includes the header file must be recompiled to be safe.  Each
512 compilation produces an object file corresponding to the source file.
513 Finally, if any source file has been recompiled, all the object files,
514 whether newly made or saved from previous compilations, must be linked
515 together to produce the new executable editor.
516 @cindex recompilation
517 @cindex editor
518
519 @menu
520 * Rule Introduction::           What a rule looks like.
521 * Simple Makefile::             A simple makefile.
522 * How Make Works::              How @code{make} processes this makefile.
523 * Variables Simplify::          Variables make makefiles simpler.
524 * make Deduces::                Letting @code{make} deduce the recipes.
525 * Combine By Prerequisite::     Another style of makefile.
526 * Cleanup::                     Rules for cleaning the directory.
527 @end menu
528
529 @node Rule Introduction, Simple Makefile, Introduction, Introduction
530 @comment  node-name,  next,  previous,  up
531 @section What a Rule Looks Like
532 @cindex rule, introduction to
533 @cindex makefile rule parts
534 @cindex parts of makefile rule
535
536 A simple makefile consists of ``rules'' with the following shape:
537
538 @cindex targets, introduction to
539 @cindex prerequisites, introduction to
540 @cindex recipes, introduction to
541 @example
542 @group
543 @var{target} @dots{} : @var{prerequisites} @dots{}
544         @var{recipe}
545         @dots{}
546         @dots{}
547 @end group
548 @end example
549
550 A @dfn{target} is usually the name of a file that is generated by a
551 program; examples of targets are executable or object files.  A target
552 can also be the name of an action to carry out, such as @samp{clean}
553 (@pxref{Phony Targets}).
554
555 A @dfn{prerequisite} is a file that is used as input to create the
556 target.  A target often depends on several files.
557
558 @cindex tabs in rules
559 A @dfn{recipe} is an action that @code{make} carries out.  A recipe
560 may have more than one command, either on the same line or each on its
561 own line.  @strong{Please note:} you need to put a tab character at
562 the beginning of every recipe line!  This is an obscurity that catches
563 the unwary.  If you prefer to prefix your recipes with a character
564 other than tab, you can set the @code{.RECIPEPREFIX} variable to an
565 alternate character (@pxref{Special Variables}).
566
567 Usually a recipe is in a rule with prerequisites and serves to create a
568 target file if any of the prerequisites change.  However, the rule that
569 specifies a recipe for the target need not have prerequisites.  For
570 example, the rule containing the delete command associated with the
571 target @samp{clean} does not have prerequisites.
572
573 A @dfn{rule}, then, explains how and when to remake certain files
574 which are the targets of the particular rule.  @code{make} carries out
575 the recipe on the prerequisites to create or update the target.  A
576 rule can also explain how and when to carry out an action.
577 @xref{Rules, , Writing Rules}.
578
579 A makefile may contain other text besides rules, but a simple makefile
580 need only contain rules.  Rules may look somewhat more complicated
581 than shown in this template, but all fit the pattern more or less.
582
583 @node Simple Makefile, How Make Works, Rule Introduction, Introduction
584 @section A Simple Makefile
585 @cindex simple makefile
586 @cindex makefile, simple
587
588 Here is a straightforward makefile that describes the way an
589 executable file called @code{edit} depends on eight object files
590 which, in turn, depend on eight C source and three header files.
591
592 In this example, all the C files include @file{defs.h}, but only those
593 defining editing commands include @file{command.h}, and only low
594 level files that change the editor buffer include @file{buffer.h}.
595
596 @example
597 @group
598 edit : main.o kbd.o command.o display.o \
599        insert.o search.o files.o utils.o
600         cc -o edit main.o kbd.o command.o display.o \
601                    insert.o search.o files.o utils.o
602
603 main.o : main.c defs.h
604         cc -c main.c
605 kbd.o : kbd.c defs.h command.h
606         cc -c kbd.c
607 command.o : command.c defs.h command.h
608         cc -c command.c
609 display.o : display.c defs.h buffer.h
610         cc -c display.c
611 insert.o : insert.c defs.h buffer.h
612         cc -c insert.c
613 search.o : search.c defs.h buffer.h
614         cc -c search.c
615 files.o : files.c defs.h buffer.h command.h
616         cc -c files.c
617 utils.o : utils.c defs.h
618         cc -c utils.c
619 clean :
620         rm edit main.o kbd.o command.o display.o \
621            insert.o search.o files.o utils.o
622 @end group
623 @end example
624
625 @noindent
626 We split each long line into two lines using backslash/newline; this is
627 like using one long line, but is easier to read.  @xref{Splitting Lines,
628 , Splitting Long Lines}.
629 @cindex continuation lines
630 @cindex @code{\} (backslash), for continuation lines
631 @cindex backslash (@code{\}), for continuation lines
632 @cindex quoting newline, in makefile
633 @cindex newline, quoting, in makefile
634
635 To use this makefile to create the executable file called @file{edit},
636 type:
637
638 @example
639 make
640 @end example
641
642 To use this makefile to delete the executable file and all the object
643 files from the directory, type:
644
645 @example
646 make clean
647 @end example
648
649 In the example makefile, the targets include the executable file
650 @samp{edit}, and the object files @samp{main.o} and @samp{kbd.o}.  The
651 prerequisites are files such as @samp{main.c} and @samp{defs.h}.
652 In fact, each @samp{.o} file is both a target and a prerequisite.
653 Recipes include @w{@samp{cc -c main.c}} and @w{@samp{cc -c kbd.c}}.
654
655 When a target is a file, it needs to be recompiled or relinked if any
656 of its prerequisites change.  In addition, any prerequisites that are
657 themselves automatically generated should be updated first.  In this
658 example, @file{edit} depends on each of the eight object files; the
659 object file @file{main.o} depends on the source file @file{main.c} and
660 on the header file @file{defs.h}.
661
662 A recipe may follow each line that contains a target and
663 prerequisites.  These recipes say how to update the target file.  A
664 tab character (or whatever character is specified by the
665 @code{.RECIPEPREFIX} variable; @pxref{Special Variables}) must come at
666 the beginning of every line in the recipe to distinguish recipes from
667 other lines in the makefile.  (Bear in mind that @code{make} does not
668 know anything about how the recipes work.  It is up to you to supply
669 recipes that will update the target file properly.  All @code{make}
670 does is execute the recipe you have specified when the target file
671 needs to be updated.)@refill
672 @cindex recipe
673
674 The target @samp{clean} is not a file, but merely the name of an
675 action.  Since you normally do not want to carry out the actions in
676 this rule, @samp{clean} is not a prerequisite of any other rule.
677 Consequently, @code{make} never does anything with it unless you tell
678 it specifically.  Note that this rule not only is not a prerequisite,
679 it also does not have any prerequisites, so the only purpose of the
680 rule is to run the specified recipe.  Targets that do not refer to
681 files but are just actions are called @dfn{phony targets}.
682 @xref{Phony Targets}, for information about this kind of target.
683 @xref{Errors, , Errors in Recipes}, to see how to cause @code{make}
684 to ignore errors from @code{rm} or any other command.
685 @cindex @code{clean} target
686 @cindex @code{rm} (shell command)
687
688 @node How Make Works, Variables Simplify, Simple Makefile, Introduction
689 @comment  node-name,  next,  previous,  up
690 @section How @code{make} Processes a Makefile
691 @cindex processing a makefile
692 @cindex makefile, how @code{make} processes
693
694 By default, @code{make} starts with the first target (not targets whose
695 names start with @samp{.}).  This is called the @dfn{default goal}.
696 (@dfn{Goals} are the targets that @code{make} strives ultimately to
697 update.    You can override this behavior using the command line
698 (@pxref{Goals, , Arguments to Specify the Goals}) or with the
699 @code{.DEFAULT_GOAL} special variable (@pxref{Special Variables, ,
700 Other Special Variables}).
701 @cindex default goal
702 @cindex goal, default
703 @cindex goal
704
705 In the simple example of the previous section, the default goal is to
706 update the executable program @file{edit}; therefore, we put that rule
707 first.
708
709 Thus, when you give the command:
710
711 @example
712 make
713 @end example
714
715 @noindent
716 @code{make} reads the makefile in the current directory and begins by
717 processing the first rule.  In the example, this rule is for relinking
718 @file{edit}; but before @code{make} can fully process this rule, it
719 must process the rules for the files that @file{edit} depends on,
720 which in this case are the object files.  Each of these files is
721 processed according to its own rule.  These rules say to update each
722 @samp{.o} file by compiling its source file.  The recompilation must
723 be done if the source file, or any of the header files named as
724 prerequisites, is more recent than the object file, or if the object
725 file does not exist.
726
727 The other rules are processed because their targets appear as
728 prerequisites of the goal.  If some other rule is not depended on by the
729 goal (or anything it depends on, etc.), that rule is not processed,
730 unless you tell @code{make} to do so (with a command such as
731 @w{@code{make clean}}).
732
733 Before recompiling an object file, @code{make} considers updating its
734 prerequisites, the source file and header files.  This makefile does not
735 specify anything to be done for them---the @samp{.c} and @samp{.h} files
736 are not the targets of any rules---so @code{make} does nothing for these
737 files.  But @code{make} would update automatically generated C programs,
738 such as those made by Bison or Yacc, by their own rules at this time.
739
740 After recompiling whichever object files need it, @code{make} decides
741 whether to relink @file{edit}.  This must be done if the file
742 @file{edit} does not exist, or if any of the object files are newer than
743 it.  If an object file was just recompiled, it is now newer than
744 @file{edit}, so @file{edit} is relinked.
745 @cindex relinking
746
747 Thus, if we change the file @file{insert.c} and run @code{make},
748 @code{make} will compile that file to update @file{insert.o}, and then
749 link @file{edit}.  If we change the file @file{command.h} and run
750 @code{make}, @code{make} will recompile the object files @file{kbd.o},
751 @file{command.o} and @file{files.o} and then link the file @file{edit}.
752
753 @node Variables Simplify, make Deduces, How Make Works, Introduction
754 @section Variables Make Makefiles Simpler
755 @cindex variables
756 @cindex simplifying with variables
757
758 In our example, we had to list all the object files twice in the rule for
759 @file{edit} (repeated here):
760
761 @example
762 @group
763 edit : main.o kbd.o command.o display.o \
764               insert.o search.o files.o utils.o
765         cc -o edit main.o kbd.o command.o display.o \
766                    insert.o search.o files.o utils.o
767 @end group
768 @end example
769
770 @cindex @code{objects}
771 Such duplication is error-prone; if a new object file is added to the
772 system, we might add it to one list and forget the other.  We can eliminate
773 the risk and simplify the makefile by using a variable.  @dfn{Variables}
774 allow a text string to be defined once and substituted in multiple places
775 later (@pxref{Using Variables, ,How to Use Variables}).
776
777 @cindex @code{OBJECTS}
778 @cindex @code{objs}
779 @cindex @code{OBJS}
780 @cindex @code{obj}
781 @cindex @code{OBJ}
782 It is standard practice for every makefile to have a variable named
783 @code{objects}, @code{OBJECTS}, @code{objs}, @code{OBJS}, @code{obj},
784 or @code{OBJ} which is a list of all object file names.  We would
785 define such a variable @code{objects} with a line like this in the
786 makefile:@refill
787
788 @example
789 @group
790 objects = main.o kbd.o command.o display.o \
791           insert.o search.o files.o utils.o
792 @end group
793 @end example
794
795 @noindent
796 Then, each place we want to put a list of the object file names, we can
797 substitute the variable's value by writing @samp{$(objects)}
798 (@pxref{Using Variables, ,How to Use Variables}).
799
800 Here is how the complete simple makefile looks when you use a variable
801 for the object files:
802
803 @example
804 @group
805 objects = main.o kbd.o command.o display.o \
806           insert.o search.o files.o utils.o
807
808 edit : $(objects)
809         cc -o edit $(objects)
810 main.o : main.c defs.h
811         cc -c main.c
812 kbd.o : kbd.c defs.h command.h
813         cc -c kbd.c
814 command.o : command.c defs.h command.h
815         cc -c command.c
816 display.o : display.c defs.h buffer.h
817         cc -c display.c
818 insert.o : insert.c defs.h buffer.h
819         cc -c insert.c
820 search.o : search.c defs.h buffer.h
821         cc -c search.c
822 files.o : files.c defs.h buffer.h command.h
823         cc -c files.c
824 utils.o : utils.c defs.h
825         cc -c utils.c
826 clean :
827         rm edit $(objects)
828 @end group
829 @end example
830
831 @node make Deduces, Combine By Prerequisite, Variables Simplify, Introduction
832 @section Letting @code{make} Deduce the Recipes
833 @cindex deducing recipes (implicit rules)
834 @cindex implicit rule, introduction to
835 @cindex rule, implicit, introduction to
836
837 It is not necessary to spell out the recipes for compiling the individual
838 C source files, because @code{make} can figure them out: it has an
839 @dfn{implicit rule} for updating a @samp{.o} file from a correspondingly
840 named @samp{.c} file using a @samp{cc -c} command.  For example, it will
841 use the recipe @samp{cc -c main.c -o main.o} to compile @file{main.c} into
842 @file{main.o}.  We can therefore omit the recipes from the rules for the
843 object files.  @xref{Implicit Rules, ,Using Implicit Rules}.@refill
844
845 When a @samp{.c} file is used automatically in this way, it is also
846 automatically added to the list of prerequisites.  We can therefore omit
847 the @samp{.c} files from the prerequisites, provided we omit the recipe.
848
849 Here is the entire example, with both of these changes, and a variable
850 @code{objects} as suggested above:
851
852 @example
853 @group
854 objects = main.o kbd.o command.o display.o \
855           insert.o search.o files.o utils.o
856
857 edit : $(objects)
858         cc -o edit $(objects)
859
860 main.o : defs.h
861 kbd.o : defs.h command.h
862 command.o : defs.h command.h
863 display.o : defs.h buffer.h
864 insert.o : defs.h buffer.h
865 search.o : defs.h buffer.h
866 files.o : defs.h buffer.h command.h
867 utils.o : defs.h
868
869 .PHONY : clean
870 clean :
871         rm edit $(objects)
872 @end group
873 @end example
874
875 @noindent
876 This is how we would write the makefile in actual practice.  (The
877 complications associated with @samp{clean} are described elsewhere.
878 See @ref{Phony Targets}, and @ref{Errors, ,Errors in Recipes}.)
879
880 Because implicit rules are so convenient, they are important.  You
881 will see them used frequently.@refill
882
883 @node Combine By Prerequisite, Cleanup, make Deduces, Introduction
884 @section Another Style of Makefile
885 @cindex combining rules by prerequisite
886
887 When the objects of a makefile are created only by implicit rules, an
888 alternative style of makefile is possible.  In this style of makefile,
889 you group entries by their prerequisites instead of by their targets.
890 Here is what one looks like:
891
892 @example
893 @group
894 objects = main.o kbd.o command.o display.o \
895           insert.o search.o files.o utils.o
896
897 edit : $(objects)
898         cc -o edit $(objects)
899
900 $(objects) : defs.h
901 kbd.o command.o files.o : command.h
902 display.o insert.o search.o files.o : buffer.h
903 @end group
904 @end example
905
906 @noindent
907 Here @file{defs.h} is given as a prerequisite of all the object files;
908 @file{command.h} and @file{buffer.h} are prerequisites of the specific
909 object files listed for them.
910
911 Whether this is better is a matter of taste: it is more compact, but some
912 people dislike it because they find it clearer to put all the information
913 about each target in one place.
914
915 @node Cleanup,  , Combine By Prerequisite, Introduction
916 @section Rules for Cleaning the Directory
917 @cindex cleaning up
918 @cindex removing, to clean up
919
920 Compiling a program is not the only thing you might want to write rules
921 for.  Makefiles commonly tell how to do a few other things besides
922 compiling a program: for example, how to delete all the object files
923 and executables so that the directory is @samp{clean}.
924
925 @cindex @code{clean} target
926 Here is how we
927 could write a @code{make} rule for cleaning our example editor:
928
929 @example
930 @group
931 clean:
932         rm edit $(objects)
933 @end group
934 @end example
935
936 In practice, we might want to write the rule in a somewhat more
937 complicated manner to handle unanticipated situations.  We would do this:
938
939 @example
940 @group
941 .PHONY : clean
942 clean :
943         -rm edit $(objects)
944 @end group
945 @end example
946
947 @noindent
948 This prevents @code{make} from getting confused by an actual file
949 called @file{clean} and causes it to continue in spite of errors from
950 @code{rm}.  (See @ref{Phony Targets}, and @ref{Errors, ,Errors in
951 Recipes}.)
952
953 @noindent
954 A rule such as this should not be placed at the beginning of the
955 makefile, because we do not want it to run by default!  Thus, in the
956 example makefile, we want the rule for @code{edit}, which recompiles
957 the editor, to remain the default goal.
958
959 Since @code{clean} is not a prerequisite of @code{edit}, this rule will not
960 run at all if we give the command @samp{make} with no arguments.  In
961 order to make the rule run, we have to type @samp{make clean}.
962 @xref{Running, ,How to Run @code{make}}.
963
964 @node Makefiles, Rules, Introduction, Top
965 @chapter Writing Makefiles
966
967 @cindex makefile, how to write
968 The information that tells @code{make} how to recompile a system comes from
969 reading a data base called the @dfn{makefile}.
970
971 @menu
972 * Makefile Contents::           What makefiles contain.
973 * Makefile Names::              How to name your makefile.
974 * Include::                     How one makefile can use another makefile.
975 * MAKEFILES Variable::          The environment can specify extra makefiles.
976 * Remaking Makefiles::          How makefiles get remade.
977 * Overriding Makefiles::        How to override part of one makefile
978                                   with another makefile.
979 * Reading Makefiles::           How makefiles are parsed.
980 * Secondary Expansion::         How and when secondary expansion is performed.
981 @end menu
982
983 @node Makefile Contents, Makefile Names, Makefiles, Makefiles
984 @section What Makefiles Contain
985
986 Makefiles contain five kinds of things: @dfn{explicit rules},
987 @dfn{implicit rules}, @dfn{variable definitions}, @dfn{directives},
988 and @dfn{comments}.  Rules, variables, and directives are described at
989 length in later chapters.@refill
990
991 @itemize @bullet
992 @cindex rule, explicit, definition of
993 @cindex explicit rule, definition of
994 @item
995 An @dfn{explicit rule} says when and how to remake one or more files,
996 called the rule's @dfn{targets}.  It lists the other files that the
997 targets depend on, called the @dfn{prerequisites} of the target, and
998 may also give a recipe to use to create or update the targets.
999 @xref{Rules, ,Writing Rules}.
1000
1001 @cindex rule, implicit, definition of
1002 @cindex implicit rule, definition of
1003 @item
1004 An @dfn{implicit rule} says when and how to remake a class of files
1005 based on their names.  It describes how a target may depend on a file
1006 with a name similar to the target and gives a recipe to create or
1007 update such a target.  @xref{Implicit Rules, ,Using Implicit Rules}.
1008
1009 @cindex variable definition
1010 @item
1011 A @dfn{variable definition} is a line that specifies a text string
1012 value for a variable that can be substituted into the text later.  The
1013 simple makefile example shows a variable definition for @code{objects}
1014 as a list of all object files (@pxref{Variables Simplify, , Variables
1015 Make Makefiles Simpler}).
1016
1017 @cindex directive
1018 @item
1019 A @dfn{directive} is an instruction for @code{make} to do something
1020 special while reading the makefile.  These include:
1021
1022 @itemize @bullet
1023 @item
1024 Reading another makefile (@pxref{Include, ,Including Other Makefiles}).
1025
1026 @item
1027 Deciding (based on the values of variables) whether to use or
1028 ignore a part of the makefile (@pxref{Conditionals, ,Conditional Parts of Makefiles}).
1029
1030 @item
1031 Defining a variable from a verbatim string containing multiple lines
1032 (@pxref{Multi-Line, ,Defining Multi-Line Variables}).
1033 @end itemize
1034
1035 @cindex comments, in makefile
1036 @cindex @code{#} (comments), in makefile
1037 @item
1038 @samp{#} in a line of a makefile starts a @dfn{comment}.  It and the
1039 rest of the line are ignored, except that a trailing backslash not
1040 escaped by another backslash will continue the comment across multiple
1041 lines.  A line containing just a comment (with perhaps spaces before
1042 it) is effectively blank, and is ignored.  If you want a literal
1043 @code{#}, escape it with a backslash (e.g., @code{\#}).  Comments may
1044 appear on any line in the makefile, although they are treated
1045 specially in certain situations.
1046
1047 You cannot use comments within variable references or function calls:
1048 any instance of @code{#} will be treated literally (rather than as the
1049 start of a comment) inside a variable reference or function call.
1050
1051 Comments within a recipe are passed to the shell, just as with any
1052 other recipe text.  The shell decides how to interpret it: whether or
1053 not this is a comment is up to the shell.
1054
1055 Within a @code{define} directive, comments are not ignored during the
1056 definition of the variable, but rather kept intact in the value of the
1057 variable.  When the variable is expanded they will either be treated
1058 as @code{make} comments or as recipe text, depending on the context in
1059 which the variable is evaluated.
1060 @end itemize
1061
1062 @menu
1063 * Splitting Lines::             Splitting long lines in makefiles
1064 @end menu
1065
1066 @node Splitting Lines,  , Makefile Contents, Makefile Contents
1067 @subsection Splitting Long Lines
1068 @cindex splitting long lines
1069 @cindex long lines, splitting
1070 @cindex backslash (@code{\}), to quote newlines
1071
1072 Makefiles use a ``line-based'' syntax in which the newline character
1073 is special and marks the end of a statement.  GNU @code{make} has no
1074 limit on the length of a statement line, up to the amount of memory in
1075 your computer.
1076
1077 However, it is difficult to read lines which are too long to display
1078 without wrapping or scrolling.  So, you can format your makefiles for
1079 readability by adding newlines into the middle of a statement: you do
1080 this by escaping the internal newlines with a backslash (@code{\})
1081 character.  Where we need to make a distinction we will refer to
1082 ``physical lines'' as a single line ending with a newline (regardless
1083 of whether it is escaped) and a ``logical line'' being a complete
1084 statement including all escaped newlines up to the first non-escaped
1085 newline.
1086
1087 The way in which backslash/newline combinations are handled depends on
1088 whether the statement is a recipe line or a non-recipe line.  Handling
1089 of backslash/newline in a recipe line is discussed later
1090 (@pxref{Splitting Recipe Lines}).
1091
1092 Outside of recipe lines, backslash/newlines are converted into a
1093 single space character.  Once that is done, all whitespace around the
1094 backslash/newline is condensed into a single space: this includes all
1095 whitespace preceding the backslash, all whitespace at the beginning of
1096 the line after the backslash/newline, and any consecutive
1097 backslash/newline combinations.
1098
1099 If the @code{.POSIX} special target is defined then backslash/newline
1100 handling is modified slightly to conform to POSIX.2: first, whitespace
1101 preceding a backslash is not removed and second, consecutive
1102 backslash/newlines are not condensed.
1103
1104 @node Makefile Names, Include, Makefile Contents, Makefiles
1105 @section What Name to Give Your Makefile
1106 @cindex makefile name
1107 @cindex name of makefile
1108 @cindex default makefile name
1109 @cindex file name of makefile
1110
1111 @c following paragraph rewritten to avoid overfull hbox
1112 By default, when @code{make} looks for the makefile, it tries the
1113 following names, in order: @file{GNUmakefile}, @file{makefile}
1114 and @file{Makefile}.@refill
1115 @findex Makefile
1116 @findex GNUmakefile
1117 @findex makefile
1118
1119 @cindex @code{README}
1120 Normally you should call your makefile either @file{makefile} or
1121 @file{Makefile}.  (We recommend @file{Makefile} because it appears
1122 prominently near the beginning of a directory listing, right near other
1123 important files such as @file{README}.)  The first name checked,
1124 @file{GNUmakefile}, is not recommended for most makefiles.  You should
1125 use this name if you have a makefile that is specific to GNU
1126 @code{make}, and will not be understood by other versions of
1127 @code{make}.  Other @code{make} programs look for @file{makefile} and
1128 @file{Makefile}, but not @file{GNUmakefile}.
1129
1130 If @code{make} finds none of these names, it does not use any makefile.
1131 Then you must specify a goal with a command argument, and @code{make}
1132 will attempt to figure out how to remake it using only its built-in
1133 implicit rules.  @xref{Implicit Rules, ,Using Implicit Rules}.
1134
1135 @cindex @code{-f}
1136 @cindex @code{--file}
1137 @cindex @code{--makefile}
1138 If you want to use a nonstandard name for your makefile, you can specify
1139 the makefile name with the @samp{-f} or @samp{--file} option.  The
1140 arguments @w{@samp{-f @var{name}}} or @w{@samp{--file=@var{name}}} tell
1141 @code{make} to read the file @var{name} as the makefile.  If you use
1142 more than one @samp{-f} or @samp{--file} option, you can specify several
1143 makefiles.  All the makefiles are effectively concatenated in the order
1144 specified.  The default makefile names @file{GNUmakefile},
1145 @file{makefile} and @file{Makefile} are not checked automatically if you
1146 specify @samp{-f} or @samp{--file}.@refill
1147 @cindex specifying makefile name
1148 @cindex makefile name, how to specify
1149 @cindex name of makefile, how to specify
1150 @cindex file name of makefile, how to specify
1151
1152 @node Include, MAKEFILES Variable, Makefile Names, Makefiles
1153 @section Including Other Makefiles
1154 @cindex including other makefiles
1155 @cindex makefile, including
1156
1157 @findex include
1158 The @code{include} directive tells @code{make} to suspend reading the
1159 current makefile and read one or more other makefiles before continuing.
1160 The directive is a line in the makefile that looks like this:
1161
1162 @example
1163 include @var{filenames}@dots{}
1164 @end example
1165
1166 @noindent
1167 @var{filenames} can contain shell file name patterns.  If
1168 @var{filenames} is empty, nothing is included and no error is printed.
1169 @cindex shell file name pattern (in @code{include})
1170 @cindex shell wildcards (in @code{include})
1171 @cindex wildcard, in @code{include}
1172
1173 Extra spaces are allowed and ignored at the beginning of the line, but
1174 the first character must not be a tab (or the value of
1175 @code{.RECIPEPREFIX})---if the line begins with a tab, it will be
1176 considered a recipe line.  Whitespace is required between
1177 @code{include} and the file names, and between file names; extra
1178 whitespace is ignored there and at the end of the directive.  A
1179 comment starting with @samp{#} is allowed at the end of the line.  If
1180 the file names contain any variable or function references, they are
1181 expanded.  @xref{Using Variables, ,How to Use Variables}.
1182
1183 For example, if you have three @file{.mk} files, @file{a.mk},
1184 @file{b.mk}, and @file{c.mk}, and @code{$(bar)} expands to
1185 @code{bish bash}, then the following expression
1186
1187 @example
1188 include foo *.mk $(bar)
1189 @end example
1190
1191 is equivalent to
1192
1193 @example
1194 include foo a.mk b.mk c.mk bish bash
1195 @end example
1196
1197 When @code{make} processes an @code{include} directive, it suspends
1198 reading of the containing makefile and reads from each listed file in
1199 turn.  When that is finished, @code{make} resumes reading the
1200 makefile in which the directive appears.
1201
1202 One occasion for using @code{include} directives is when several programs,
1203 handled by individual makefiles in various directories, need to use a
1204 common set of variable definitions
1205 (@pxref{Setting, ,Setting Variables}) or pattern rules
1206 (@pxref{Pattern Rules, ,Defining and Redefining Pattern Rules}).
1207
1208 Another such occasion is when you want to generate prerequisites from
1209 source files automatically; the prerequisites can be put in a file that
1210 is included by the main makefile.  This practice is generally cleaner
1211 than that of somehow appending the prerequisites to the end of the main
1212 makefile as has been traditionally done with other versions of
1213 @code{make}.  @xref{Automatic Prerequisites}.
1214 @cindex prerequisites, automatic generation
1215 @cindex automatic generation of prerequisites
1216 @cindex generating prerequisites automatically
1217
1218 @cindex @code{-I}
1219 @cindex @code{--include-dir}
1220 @cindex included makefiles, default directories
1221 @cindex default directories for included makefiles
1222 @findex /usr/gnu/include
1223 @findex /usr/local/include
1224 @findex /usr/include
1225 If the specified name does not start with a slash, and the file is not
1226 found in the current directory, several other directories are searched.
1227 First, any directories you have specified with the @samp{-I} or
1228 @samp{--include-dir} option are searched
1229 (@pxref{Options Summary, ,Summary of Options}).
1230 Then the following directories (if they exist)
1231 are searched, in this order:
1232 @file{@var{prefix}/include} (normally @file{/usr/local/include}
1233 @footnote{GNU Make compiled for MS-DOS and MS-Windows behaves as if
1234 @var{prefix} has been defined to be the root of the DJGPP tree
1235 hierarchy.})
1236 @file{/usr/gnu/include},
1237 @file{/usr/local/include}, @file{/usr/include}.
1238
1239 If an included makefile cannot be found in any of these directories, a
1240 warning message is generated, but it is not an immediately fatal error;
1241 processing of the makefile containing the @code{include} continues.
1242 Once it has finished reading makefiles, @code{make} will try to remake
1243 any that are out of date or don't exist.
1244 @xref{Remaking Makefiles, ,How Makefiles Are Remade}.
1245 Only after it has tried to find a way to remake a makefile and failed,
1246 will @code{make} diagnose the missing makefile as a fatal error.
1247
1248 If you want @code{make} to simply ignore a makefile which does not exist
1249 or cannot be remade, with no error message, use the @w{@code{-include}}
1250 directive instead of @code{include}, like this:
1251
1252 @example
1253 -include @var{filenames}@dots{}
1254 @end example
1255
1256 This acts like @code{include} in every way except that there is no
1257 error (not even a warning) if any of the @var{filenames} (or any
1258 prerequisites of any of the @var{filenames}) do not exist or cannot be
1259 remade.
1260
1261 For compatibility with some other @code{make} implementations,
1262 @code{sinclude} is another name for @w{@code{-include}}.
1263
1264 @node MAKEFILES Variable, Remaking Makefiles, Include, Makefiles
1265 @section The Variable @code{MAKEFILES}
1266 @cindex makefile, and @code{MAKEFILES} variable
1267 @cindex including (@code{MAKEFILES} variable)
1268
1269 @vindex MAKEFILES
1270 If the environment variable @code{MAKEFILES} is defined, @code{make}
1271 considers its value as a list of names (separated by whitespace) of
1272 additional makefiles to be read before the others.  This works much
1273 like the @code{include} directive: various directories are searched
1274 for those files (@pxref{Include, ,Including Other Makefiles}).  In
1275 addition, the default goal is never taken from one of these makefiles
1276 (or any makefile included by them) and it is not an error if the files
1277 listed in @code{MAKEFILES} are not found.@refill
1278
1279 @cindex recursion, and @code{MAKEFILES} variable
1280 The main use of @code{MAKEFILES} is in communication between recursive
1281 invocations of @code{make} (@pxref{Recursion, ,Recursive Use of
1282 @code{make}}).  It usually is not desirable to set the environment
1283 variable before a top-level invocation of @code{make}, because it is
1284 usually better not to mess with a makefile from outside.  However, if
1285 you are running @code{make} without a specific makefile, a makefile in
1286 @code{MAKEFILES} can do useful things to help the built-in implicit
1287 rules work better, such as defining search paths (@pxref{Directory Search}).
1288
1289 Some users are tempted to set @code{MAKEFILES} in the environment
1290 automatically on login, and program makefiles to expect this to be done.
1291 This is a very bad idea, because such makefiles will fail to work if run by
1292 anyone else.  It is much better to write explicit @code{include} directives
1293 in the makefiles.  @xref{Include, , Including Other Makefiles}.
1294
1295 @node Remaking Makefiles, Overriding Makefiles, MAKEFILES Variable, Makefiles
1296 @section How Makefiles Are Remade
1297 @cindex updating makefiles
1298 @cindex remaking makefiles
1299 @cindex makefile, remaking of
1300 Sometimes makefiles can be remade from other files, such as RCS or SCCS
1301 files.  If a makefile can be remade from other files, you probably want
1302 @code{make} to get an up-to-date version of the makefile to read in.
1303
1304 To this end, after reading in all makefiles, @code{make} will consider
1305 each as a goal target and attempt to update it.  If a makefile has a
1306 rule which says how to update it (found either in that very makefile or
1307 in another one) or if an implicit rule applies to it (@pxref{Implicit
1308 Rules, ,Using Implicit Rules}), it will be updated if necessary.  After
1309 all makefiles have been checked, if any have actually been changed,
1310 @code{make} starts with a clean slate and reads all the makefiles over
1311 again.  (It will also attempt to update each of them over again, but
1312 normally this will not change them again, since they are already up to
1313 date.)@refill
1314
1315 If you know that one or more of your makefiles cannot be remade and
1316 you want to keep @code{make} from performing an implicit rule search
1317 on them, perhaps for efficiency reasons, you can use any normal method
1318 of preventing implicit rule look-up to do so.  For example, you can
1319 write an explicit rule with the makefile as the target, and an empty
1320 recipe (@pxref{Empty Recipes, ,Using Empty Recipes}).
1321
1322 If the makefiles specify a double-colon rule to remake a file with
1323 a recipe but no prerequisites, that file will always be remade
1324 (@pxref{Double-Colon}).  In the case of makefiles, a makefile that has a
1325 double-colon rule with a recipe but no prerequisites will be remade every
1326 time @code{make} is run, and then again after @code{make} starts over
1327 and reads the makefiles in again.  This would cause an infinite loop:
1328 @code{make} would constantly remake the makefile, and never do anything
1329 else.  So, to avoid this, @code{make} will @strong{not} attempt to
1330 remake makefiles which are specified as targets of a double-colon rule
1331 with a recipe but no prerequisites.@refill
1332
1333 If you do not specify any makefiles to be read with @samp{-f} or
1334 @samp{--file} options, @code{make} will try the default makefile names;
1335 @pxref{Makefile Names, ,What Name to Give Your Makefile}.  Unlike
1336 makefiles explicitly requested with @samp{-f} or @samp{--file} options,
1337 @code{make} is not certain that these makefiles should exist.  However,
1338 if a default makefile does not exist but can be created by running
1339 @code{make} rules, you probably want the rules to be run so that the
1340 makefile can be used.
1341
1342 Therefore, if none of the default makefiles exists, @code{make} will try
1343 to make each of them in the same order in which they are searched for
1344 (@pxref{Makefile Names, ,What Name to Give Your Makefile})
1345 until it succeeds in making one, or it runs out of names to try.  Note
1346 that it is not an error if @code{make} cannot find or make any makefile;
1347 a makefile is not always necessary.@refill
1348
1349 When you use the @samp{-t} or @samp{--touch} option
1350 (@pxref{Instead of Execution, ,Instead of Executing Recipes}),
1351 you would not want to use an out-of-date makefile to decide which
1352 targets to touch.  So the @samp{-t} option has no effect on updating
1353 makefiles; they are really updated even if @samp{-t} is specified.
1354 Likewise, @samp{-q} (or @samp{--question}) and @samp{-n} (or
1355 @samp{--just-print}) do not prevent updating of makefiles, because an
1356 out-of-date makefile would result in the wrong output for other targets.
1357 Thus, @samp{make -f mfile -n foo} will update @file{mfile}, read it in,
1358 and then print the recipe to update @file{foo} and its prerequisites
1359 without running it.  The recipe printed for @file{foo} will be the one
1360 specified in the updated contents of @file{mfile}.
1361
1362 However, on occasion you might actually wish to prevent updating of even
1363 the makefiles.  You can do this by specifying the makefiles as goals in
1364 the command line as well as specifying them as makefiles.  When the
1365 makefile name is specified explicitly as a goal, the options @samp{-t}
1366 and so on do apply to them.
1367
1368 Thus, @samp{make -f mfile -n mfile foo} would read the makefile
1369 @file{mfile}, print the recipe needed to update it without actually
1370 running it, and then print the recipe needed to update @file{foo}
1371 without running that.  The recipe for @file{foo} will be the one
1372 specified by the existing contents of @file{mfile}.
1373
1374 @node Overriding Makefiles, Reading Makefiles, Remaking Makefiles, Makefiles
1375 @section Overriding Part of Another Makefile
1376
1377 @cindex overriding makefiles
1378 @cindex makefile, overriding
1379 Sometimes it is useful to have a makefile that is mostly just like
1380 another makefile.  You can often use the @samp{include} directive to
1381 include one in the other, and add more targets or variable definitions.
1382 However, it is invalid for two makefiles to give different recipes for
1383 the same target.  But there is another way.
1384
1385 @cindex match-anything rule, used to override
1386 In the containing makefile (the one that wants to include the other),
1387 you can use a match-anything pattern rule to say that to remake any
1388 target that cannot be made from the information in the containing
1389 makefile, @code{make} should look in another makefile.
1390 @xref{Pattern Rules}, for more information on pattern rules.
1391
1392 For example, if you have a makefile called @file{Makefile} that says how
1393 to make the target @samp{foo} (and other targets), you can write a
1394 makefile called @file{GNUmakefile} that contains:
1395
1396 @example
1397 foo:
1398         frobnicate > foo
1399
1400 %: force
1401         @@$(MAKE) -f Makefile $@@
1402 force: ;
1403 @end example
1404
1405 If you say @samp{make foo}, @code{make} will find @file{GNUmakefile},
1406 read it, and see that to make @file{foo}, it needs to run the recipe
1407 @samp{frobnicate > foo}.  If you say @samp{make bar}, @code{make} will
1408 find no way to make @file{bar} in @file{GNUmakefile}, so it will use the
1409 recipe from the pattern rule: @samp{make -f Makefile bar}.  If
1410 @file{Makefile} provides a rule for updating @file{bar}, @code{make}
1411 will apply the rule.  And likewise for any other target that
1412 @file{GNUmakefile} does not say how to make.
1413
1414 The way this works is that the pattern rule has a pattern of just
1415 @samp{%}, so it matches any target whatever.  The rule specifies a
1416 prerequisite @file{force}, to guarantee that the recipe will be run even
1417 if the target file already exists.  We give the @file{force} target an
1418 empty recipe to prevent @code{make} from searching for an implicit rule to
1419 build it---otherwise it would apply the same match-anything rule to
1420 @file{force} itself and create a prerequisite loop!
1421
1422 @node Reading Makefiles,  Secondary Expansion, Overriding Makefiles, Makefiles
1423 @section How @code{make} Reads a Makefile
1424 @cindex reading makefiles
1425 @cindex makefile, parsing
1426
1427 GNU @code{make} does its work in two distinct phases.  During the first
1428 phase it reads all the makefiles, included makefiles, etc. and
1429 internalizes all the variables and their values, implicit and explicit
1430 rules, and constructs a dependency graph of all the targets and their
1431 prerequisites.  During the second phase, @code{make} uses these internal
1432 structures to determine what targets will need to be rebuilt and to
1433 invoke the rules necessary to do so.
1434
1435 It's important to understand this two-phase approach because it has a
1436 direct impact on how variable and function expansion happens; this is
1437 often a source of some confusion when writing makefiles.  Here we will
1438 present a summary of the phases in which expansion happens for different
1439 constructs within the makefile.  We say that expansion is
1440 @dfn{immediate} if it happens during the first phase: in this case
1441 @code{make} will expand any variables or functions in that section of a
1442 construct as the makefile is parsed.  We say that expansion is
1443 @dfn{deferred} if expansion is not performed immediately.  Expansion of
1444 a deferred construct is not performed until either the construct appears
1445 later in an immediate context, or until the second phase.
1446
1447 You may not be familiar with some of these constructs yet.  You can
1448 reference this section as you become familiar with them, in later
1449 chapters.
1450
1451 @subheading Variable Assignment
1452 @cindex +=, expansion
1453 @cindex =, expansion
1454 @cindex ?=, expansion
1455 @cindex +=, expansion
1456 @cindex !=, expansion
1457 @cindex define, expansion
1458
1459 Variable definitions are parsed as follows:
1460
1461 @example
1462 @var{immediate} = @var{deferred}
1463 @var{immediate} ?= @var{deferred}
1464 @var{immediate} := @var{immediate}
1465 @var{immediate} ::= @var{immediate}
1466 @var{immediate} += @var{deferred} or @var{immediate}
1467 @var{immediate} != @var{immediate}
1468
1469 define @var{immediate}
1470   @var{deferred}
1471 endef
1472
1473 define @var{immediate} =
1474   @var{deferred}
1475 endef
1476
1477 define @var{immediate} ?=
1478   @var{deferred}
1479 endef
1480
1481 define @var{immediate} :=
1482   @var{immediate}
1483 endef
1484
1485 define @var{immediate} ::=
1486   @var{immediate}
1487 endef
1488
1489 define @var{immediate} +=
1490   @var{deferred} or @var{immediate}
1491 endef
1492
1493 define @var{immediate} !=
1494   @var{immediate}
1495 endef
1496 @end example
1497
1498 For the append operator, @samp{+=}, the right-hand side is considered
1499 immediate if the variable was previously set as a simple variable
1500 (@samp{:=} or @samp{::=}), and deferred otherwise.
1501
1502 For the shell assignment operator, @samp{!=}, the right-hand side is
1503 evaluated immediately and handed to the shell.  The result is stored in the
1504 variable named on the left, and that variable becomes a simple variable
1505 (and will thus be re-evaluated on each reference).
1506
1507 @subheading Conditional Directives
1508 @cindex ifdef, expansion
1509 @cindex ifeq, expansion
1510 @cindex ifndef, expansion
1511 @cindex ifneq, expansion
1512
1513 Conditional directives are parsed immediately.  This means, for
1514 example, that automatic variables cannot be used in conditional
1515 directives, as automatic variables are not set until the recipe for
1516 that rule is invoked.  If you need to use automatic variables in a
1517 conditional directive you @emph{must} move the condition into the
1518 recipe and use shell conditional syntax instead.
1519
1520 @subheading Rule Definition
1521 @cindex target, expansion
1522 @cindex prerequisite, expansion
1523 @cindex implicit rule, expansion
1524 @cindex pattern rule, expansion
1525 @cindex explicit rule, expansion
1526
1527 A rule is always expanded the same way, regardless of the form:
1528
1529 @example
1530 @var{immediate} : @var{immediate} ; @var{deferred}
1531         @var{deferred}
1532 @end example
1533
1534 That is, the target and prerequisite sections are expanded immediately,
1535 and the recipe used to construct the target is always deferred.  This
1536 general rule is true for explicit rules, pattern rules, suffix rules,
1537 static pattern rules, and simple prerequisite definitions.
1538
1539 @node Secondary Expansion, , Reading Makefiles, Makefiles
1540 @section Secondary Expansion
1541 @cindex secondary expansion
1542 @cindex expansion, secondary
1543
1544 @findex .SECONDEXPANSION
1545 In the previous section we learned that GNU @code{make} works in two
1546 distinct phases: a read-in phase and a target-update phase
1547 (@pxref{Reading Makefiles, , How @code{make} Reads a Makefile}).  GNU
1548 make also has the ability to enable a @emph{second expansion} of the
1549 prerequisites (only) for some or all targets defined in the makefile.
1550 In order for this second expansion to occur, the special target
1551 @code{.SECONDEXPANSION} must be defined before the first prerequisite
1552 list that makes use of this feature.
1553
1554 If that special target is defined then in between the two phases
1555 mentioned above, right at the end of the read-in phase, all the
1556 prerequisites of the targets defined after the special target are
1557 expanded a @emph{second time}.  In most circumstances this secondary
1558 expansion will have no effect, since all variable and function
1559 references will have been expanded during the initial parsing of the
1560 makefiles.  In order to take advantage of the secondary expansion
1561 phase of the parser, then, it's necessary to @emph{escape} the
1562 variable or function reference in the makefile.  In this case the
1563 first expansion merely un-escapes the reference but doesn't expand it,
1564 and expansion is left to the secondary expansion phase.  For example,
1565 consider this makefile:
1566
1567 @example
1568 .SECONDEXPANSION:
1569 ONEVAR = onefile
1570 TWOVAR = twofile
1571 myfile: $(ONEVAR) $$(TWOVAR)
1572 @end example
1573
1574 After the first expansion phase the prerequisites list of the
1575 @file{myfile} target will be @code{onefile} and @code{$(TWOVAR)}; the
1576 first (unescaped) variable reference to @var{ONEVAR} is expanded,
1577 while the second (escaped) variable reference is simply unescaped,
1578 without being recognized as a variable reference.  Now during the
1579 secondary expansion the first word is expanded again but since it
1580 contains no variable or function references it remains the value
1581 @file{onefile}, while the second word is now a normal reference to the
1582 variable @var{TWOVAR}, which is expanded to the value @file{twofile}.
1583 The final result is that there are two prerequisites, @file{onefile}
1584 and @file{twofile}.
1585
1586 Obviously, this is not a very interesting case since the same result
1587 could more easily have been achieved simply by having both variables
1588 appear, unescaped, in the prerequisites list.  One difference becomes
1589 apparent if the variables are reset; consider this example:
1590
1591 @example
1592 .SECONDEXPANSION:
1593 AVAR = top
1594 onefile: $(AVAR)
1595 twofile: $$(AVAR)
1596 AVAR = bottom
1597 @end example
1598
1599 Here the prerequisite of @file{onefile} will be expanded immediately,
1600 and resolve to the value @file{top}, while the prerequisite of
1601 @file{twofile} will not be full expanded until the secondary expansion
1602 and yield a value of @file{bottom}.
1603
1604 This is marginally more exciting, but the true power of this feature
1605 only becomes apparent when you discover that secondary expansions
1606 always take place within the scope of the automatic variables for that
1607 target.  This means that you can use variables such as @code{$@@},
1608 @code{$*}, etc. during the second expansion and they will have their
1609 expected values, just as in the recipe.  All you have to do is defer
1610 the expansion by escaping the @code{$}.  Also, secondary expansion
1611 occurs for both explicit and implicit (pattern) rules.  Knowing this,
1612 the possible uses for this feature increase dramatically.  For
1613 example:
1614
1615 @example
1616 .SECONDEXPANSION:
1617 main_OBJS := main.o try.o test.o
1618 lib_OBJS := lib.o api.o
1619
1620 main lib: $$($$@@_OBJS)
1621 @end example
1622
1623 Here, after the initial expansion the prerequisites of both the
1624 @file{main} and @file{lib} targets will be @code{$($@@_OBJS)}.  During
1625 the secondary expansion, the @code{$@@} variable is set to the name of
1626 the target and so the expansion for the @file{main} target will yield
1627 @code{$(main_OBJS)}, or @code{main.o try.o test.o}, while the
1628 secondary expansion for the @file{lib} target will yield
1629 @code{$(lib_OBJS)}, or @code{lib.o api.o}.
1630
1631 You can also mix in functions here, as long as they are properly escaped:
1632
1633 @example
1634 main_SRCS := main.c try.c test.c
1635 lib_SRCS := lib.c api.c
1636
1637 .SECONDEXPANSION:
1638 main lib: $$(patsubst %.c,%.o,$$($$@@_SRCS))
1639 @end example
1640
1641 This version allows users to specify source files rather than object
1642 files, but gives the same resulting prerequisites list as the previous
1643 example.
1644
1645 Evaluation of automatic variables during the secondary expansion
1646 phase, especially of the target name variable @code{$$@@}, behaves
1647 similarly to evaluation within recipes.  However, there are some
1648 subtle differences and ``corner cases'' which come into play for the
1649 different types of rule definitions that @code{make} understands.  The
1650 subtleties of using the different automatic variables are described
1651 below.
1652
1653 @subheading Secondary Expansion of Explicit Rules
1654 @cindex secondary expansion and explicit rules
1655 @cindex explicit rules, secondary expansion of
1656
1657 During the secondary expansion of explicit rules, @code{$$@@} and
1658 @code{$$%} evaluate, respectively, to the file name of the target and,
1659 when the target is an archive member, the target member name.  The
1660 @code{$$<} variable evaluates to the first prerequisite in the first
1661 rule for this target.  @code{$$^} and @code{$$+} evaluate to the list
1662 of all prerequisites of rules @emph{that have already appeared} for
1663 the same target (@code{$$+} with repetitions and @code{$$^}
1664 without).  The following example will help illustrate these behaviors:
1665
1666 @example
1667 .SECONDEXPANSION:
1668
1669 foo: foo.1 bar.1 $$< $$^ $$+    # line #1
1670
1671 foo: foo.2 bar.2 $$< $$^ $$+    # line #2
1672
1673 foo: foo.3 bar.3 $$< $$^ $$+    # line #3
1674 @end example
1675
1676 In the first prerequisite list, all three variables (@code{$$<},
1677 @code{$$^}, and @code{$$+}) expand to the empty string.  In the
1678 second, they will have values @code{foo.1}, @code{foo.1 bar.1}, and
1679 @code{foo.1 bar.1} respectively.  In the third they will have values
1680 @code{foo.1}, @code{foo.1 bar.1 foo.2 bar.2}, and @code{foo.1 bar.1
1681 foo.2 bar.2 foo.1 foo.1 bar.1 foo.1 bar.1} respectively.
1682
1683 Rules undergo secondary expansion in makefile order, except that
1684 the rule with the recipe is always evaluated last.
1685
1686 The variables @code{$$?} and @code{$$*} are not available and expand
1687 to the empty string.
1688
1689 @subheading Secondary Expansion of Static Pattern Rules
1690 @cindex secondary expansion and static pattern rules
1691 @cindex static pattern rules, secondary expansion of
1692
1693 Rules for secondary expansion of static pattern rules are identical to
1694 those for explicit rules, above, with one exception: for static
1695 pattern rules the @code{$$*} variable is set to the pattern stem.  As
1696 with explicit rules, @code{$$?} is not available and expands to the
1697 empty string.
1698
1699 @subheading Secondary Expansion of Implicit Rules
1700 @cindex secondary expansion and implicit rules
1701 @cindex implicit rules, secondary expansion of
1702
1703 As @code{make} searches for an implicit rule, it substitutes the stem
1704 and then performs secondary expansion for every rule with a matching
1705 target pattern.  The value of the automatic variables is derived in
1706 the same fashion as for static pattern rules.  As an example:
1707
1708 @example
1709 .SECONDEXPANSION:
1710
1711 foo: bar
1712
1713 foo foz: fo%: bo%
1714
1715 %oo: $$< $$^ $$+ $$*
1716 @end example
1717
1718 When the implicit rule is tried for target @file{foo}, @code{$$<}
1719 expands to @file{bar}, @code{$$^} expands to @file{bar boo},
1720 @code{$$+} also expands to @file{bar boo}, and @code{$$*} expands to
1721 @file{f}.
1722
1723 Note that the directory prefix (D), as described in @ref{Implicit Rule
1724 Search, ,Implicit Rule Search Algorithm}, is appended (after
1725 expansion) to all the patterns in the prerequisites list.  As an
1726 example:@refill
1727
1728 @example
1729 .SECONDEXPANSION:
1730
1731 /tmp/foo.o:
1732
1733 %.o: $$(addsuffix /%.c,foo bar) foo.h
1734         @@echo $^
1735 @end example
1736
1737 The prerequisite list printed, after the secondary expansion and
1738 directory prefix reconstruction, will be @file{/tmp/foo/foo.c
1739 /tmp/bar/foo.c foo.h}.  If you are not interested in this
1740 reconstruction, you can use @code{$$*} instead of @code{%} in the
1741 prerequisites list.
1742
1743 @node Rules, Recipes, Makefiles, Top
1744 @chapter Writing Rules
1745 @cindex writing rules
1746 @cindex rule, how to write
1747 @cindex target
1748 @cindex prerequisite
1749
1750 A @dfn{rule} appears in the makefile and says when and how to remake
1751 certain files, called the rule's @dfn{targets} (most often only one per rule).
1752 It lists the other files that are the @dfn{prerequisites} of the target, and
1753 the @dfn{recipe} to use to create or update the target.
1754
1755 @cindex default goal
1756 @cindex goal, default
1757 The order of rules is not significant, except for determining the
1758 @dfn{default goal}: the target for @code{make} to consider, if you do
1759 not otherwise specify one.  The default goal is the target of the first
1760 rule in the first makefile.  If the first rule has multiple targets,
1761 only the first target is taken as the default.  There are two
1762 exceptions: a target starting with a period is not a default unless it
1763 contains one or more slashes, @samp{/}, as well; and, a target that
1764 defines a pattern rule has no effect on the default goal.
1765 (@xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.)
1766
1767 Therefore, we usually write the makefile so that the first rule is the
1768 one for compiling the entire program or all the programs described by
1769 the makefile (often with a target called @samp{all}).
1770 @xref{Goals, ,Arguments to Specify the Goals}.
1771
1772 @menu
1773 * Rule Example::                An example explained.
1774 * Rule Syntax::                 General syntax explained.
1775 * Prerequisite Types::          There are two types of prerequisites.
1776 * Wildcards::                   Using wildcard characters such as `*'.
1777 * Directory Search::            Searching other directories for source files.
1778 * Phony Targets::               Using a target that is not a real file's name.
1779 * Force Targets::               You can use a target without a recipe
1780                                   or prerequisites to mark other targets
1781                                   as phony.
1782 * Empty Targets::               When only the date matters and the
1783                                   files are empty.
1784 * Special Targets::             Targets with special built-in meanings.
1785 * Multiple Targets::            When to make use of several targets in a rule.
1786 * Multiple Rules::              How to use several rules with the same target.
1787 * Static Pattern::              Static pattern rules apply to multiple targets
1788                                   and can vary the prerequisites according to
1789                                   the target name.
1790 * Double-Colon::                How to use a special kind of rule to allow
1791                                   several independent rules for one target.
1792 * Automatic Prerequisites::     How to automatically generate rules giving
1793                                   prerequisites from source files themselves.
1794 @end menu
1795
1796 @ifnottex
1797 @node Rule Example, Rule Syntax, Rules, Rules
1798 @section Rule Example
1799
1800 Here is an example of a rule:
1801
1802 @example
1803 foo.o : foo.c defs.h       # module for twiddling the frobs
1804         cc -c -g foo.c
1805 @end example
1806
1807 Its target is @file{foo.o} and its prerequisites are @file{foo.c} and
1808 @file{defs.h}.  It has one command in the recipe: @samp{cc -c -g foo.c}.
1809 The recipe starts with a tab to identify it as a recipe.
1810
1811 This rule says two things:
1812
1813 @itemize @bullet
1814 @item
1815 How to decide whether @file{foo.o} is out of date: it is out of date
1816 if it does not exist, or if either @file{foo.c} or @file{defs.h} is
1817 more recent than it.
1818
1819 @item
1820 How to update the file @file{foo.o}: by running @code{cc} as stated.
1821 The recipe does not explicitly mention @file{defs.h}, but we presume
1822 that @file{foo.c} includes it, and that that is why @file{defs.h} was
1823 added to the prerequisites.
1824 @end itemize
1825 @end ifnottex
1826
1827 @node Rule Syntax, Prerequisite Types, Rule Example, Rules
1828 @section Rule Syntax
1829
1830 @cindex rule syntax
1831 @cindex syntax of rules
1832 In general, a rule looks like this:
1833
1834 @example
1835 @var{targets} : @var{prerequisites}
1836         @var{recipe}
1837         @dots{}
1838 @end example
1839
1840 @noindent
1841 or like this:
1842
1843 @example
1844 @var{targets} : @var{prerequisites} ; @var{recipe}
1845         @var{recipe}
1846         @dots{}
1847 @end example
1848
1849 @cindex targets
1850 @cindex rule targets
1851 The @var{targets} are file names, separated by spaces.  Wildcard
1852 characters may be used (@pxref{Wildcards, ,Using Wildcard Characters
1853 in File Names}) and a name of the form @file{@var{a}(@var{m})}
1854 represents member @var{m} in archive file @var{a}
1855 (@pxref{Archive Members, ,Archive Members as Targets}).
1856 Usually there is only one
1857 target per rule, but occasionally there is a reason to have more
1858 (@pxref{Multiple Targets, , Multiple Targets in a Rule}).@refill
1859
1860 @cindex recipes
1861 @cindex tab character (in commands)
1862 The @var{recipe} lines start with a tab character (or the first
1863 character in the value of the @code{.RECIPEPREFIX} variable;
1864 @pxref{Special Variables}).  The first recipe line may appear on the line
1865 after the prerequisites, with a tab character, or may appear on the
1866 same line, with a semicolon.  Either way, the effect is the same.
1867 There are other differences in the syntax of recipes.
1868 @xref{Recipes, ,Writing Recipes in Rules}.
1869
1870 @cindex dollar sign (@code{$}), in rules
1871 @cindex @code{$}, in rules
1872 @cindex rules, and @code{$}
1873 Because dollar signs are used to start @code{make} variable
1874 references, if you really want a dollar sign in a target or
1875 prerequisite you must write two of them, @samp{$$} (@pxref{Using
1876 Variables, ,How to Use Variables}).  If you have enabled secondary
1877 expansion (@pxref{Secondary Expansion}) and you want a literal dollar
1878 sign in the prerequisites list, you must actually write @emph{four}
1879 dollar signs (@samp{$$$$}).
1880
1881 You may split a long line by inserting a backslash followed by a
1882 newline, but this is not required, as @code{make} places no limit on
1883 the length of a line in a makefile.
1884
1885 A rule tells @code{make} two things: when the targets are out of date,
1886 and how to update them when necessary.
1887
1888 @cindex prerequisites
1889 @cindex rule prerequisites
1890 The criterion for being out of date is specified in terms of the
1891 @var{prerequisites}, which consist of file names separated by spaces.
1892 (Wildcards and archive members (@pxref{Archives}) are allowed here too.)
1893 A target is out of date if it does not exist or if it is older than any
1894 of the prerequisites (by comparison of last-modification times).  The
1895 idea is that the contents of the target file are computed based on
1896 information in the prerequisites, so if any of the prerequisites changes,
1897 the contents of the existing target file are no longer necessarily
1898 valid.
1899
1900 How to update is specified by a @var{recipe}.  This is one or more
1901 lines to be executed by the shell (normally @samp{sh}), but with some
1902 extra features (@pxref{Recipes, ,Writing Recipes in Rules}).
1903
1904 @node Prerequisite Types, Wildcards, Rule Syntax, Rules
1905 @comment  node-name,  next,  previous,  up
1906 @section Types of Prerequisites
1907 @cindex prerequisite types
1908 @cindex types of prerequisites
1909
1910 @cindex prerequisites, normal
1911 @cindex normal prerequisites
1912 @cindex prerequisites, order-only
1913 @cindex order-only prerequisites
1914 There are actually two different types of prerequisites understood by
1915 GNU @code{make}: normal prerequisites such as described in the
1916 previous section, and @dfn{order-only} prerequisites.  A normal
1917 prerequisite makes two statements: first, it imposes an order in which
1918 recipes will be invoked: the recipes for all prerequisites of a target
1919 will be completed before the recipe for the target is run.  Second, it
1920 imposes a dependency relationship: if any prerequisite is newer than
1921 the target, then the target is considered out-of-date and must be
1922 rebuilt.
1923
1924 Normally, this is exactly what you want: if a target's prerequisite is
1925 updated, then the target should also be updated.
1926
1927 Occasionally, however, you have a situation where you want to impose a
1928 specific ordering on the rules to be invoked @emph{without} forcing
1929 the target to be updated if one of those rules is executed.  In that
1930 case, you want to define @dfn{order-only} prerequisites.  Order-only
1931 prerequisites can be specified by placing a pipe symbol (@code{|})
1932 in the prerequisites list: any prerequisites to the left of the pipe
1933 symbol are normal; any prerequisites to the right are order-only:
1934
1935 @example
1936 @var{targets} : @var{normal-prerequisites} | @var{order-only-prerequisites}
1937 @end example
1938
1939 The normal prerequisites section may of course be empty.  Also, you
1940 may still declare multiple lines of prerequisites for the same target:
1941 they are appended appropriately (normal prerequisites are appended to
1942 the list of normal prerequisites; order-only prerequisites are
1943 appended to the list of order-only prerequisites).  Note that if you
1944 declare the same file to be both a normal and an order-only
1945 prerequisite, the normal prerequisite takes precedence (since they
1946 have a strict superset of the behavior of an order-only prerequisite).
1947
1948 Consider an example where your targets are to be placed in a separate
1949 directory, and that directory might not exist before @code{make} is
1950 run.  In this situation, you want the directory to be created before
1951 any targets are placed into it but, because the timestamps on
1952 directories change whenever a file is added, removed, or renamed, we
1953 certainly don't want to rebuild all the targets whenever the
1954 directory's timestamp changes.  One way to manage this is with
1955 order-only prerequisites: make the directory an order-only
1956 prerequisite on all the targets:
1957
1958 @example
1959 OBJDIR := objdir
1960 OBJS := $(addprefix $(OBJDIR)/,foo.o bar.o baz.o)
1961
1962 $(OBJDIR)/%.o : %.c
1963         $(COMPILE.c) $(OUTPUT_OPTION) $<
1964
1965 all: $(OBJS)
1966
1967 $(OBJS): | $(OBJDIR)
1968
1969 $(OBJDIR):
1970         mkdir $(OBJDIR)
1971 @end example
1972
1973 Now the rule to create the @file{objdir} directory will be run, if
1974 needed, before any @samp{.o} is built, but no @samp{.o} will be built
1975 because the @file{objdir} directory timestamp changed.
1976
1977 @node Wildcards, Directory Search, Prerequisite Types, Rules
1978 @section Using Wildcard Characters in File Names
1979 @cindex wildcard
1980 @cindex file name with wildcards
1981 @cindex globbing (wildcards)
1982
1983 @cindex @code{*} (wildcard character)
1984 @cindex @code{?} (wildcard character)
1985 @cindex @code{[@dots{}]} (wildcard characters)
1986 A single file name can specify many files using @dfn{wildcard characters}.
1987 The wildcard characters in @code{make} are @samp{*}, @samp{?} and
1988 @samp{[@dots{}]}, the same as in the Bourne shell.  For example, @file{*.c}
1989 specifies a list of all the files (in the working directory) whose names
1990 end in @samp{.c}.@refill
1991
1992 @cindex @code{~} (tilde)
1993 @cindex tilde (@code{~})
1994 @cindex home directory
1995 The character @samp{~} at the beginning of a file name also has special
1996 significance.  If alone, or followed by a slash, it represents your home
1997 directory.  For example @file{~/bin} expands to @file{/home/you/bin}.
1998 If the @samp{~} is followed by a word, the string represents the home
1999 directory of the user named by that word.  For example @file{~john/bin}
2000 expands to @file{/home/john/bin}.  On systems which don't have a home
2001 directory for each user (such as MS-DOS or MS-Windows), this
2002 functionality can be simulated by setting the environment variable
2003 @var{HOME}.@refill
2004
2005 Wildcard expansion is performed by @code{make} automatically in
2006 targets and in prerequisites.  In recipes, the shell is responsible
2007 for wildcard expansion.  In other contexts, wildcard expansion happens
2008 only if you request it explicitly with the @code{wildcard} function.
2009
2010 The special significance of a wildcard character can be turned off by
2011 preceding it with a backslash.  Thus, @file{foo\*bar} would refer to a
2012 specific file whose name consists of @samp{foo}, an asterisk, and
2013 @samp{bar}.@refill
2014
2015 @menu
2016 * Wildcard Examples::           Several examples.
2017 * Wildcard Pitfall::            Problems to avoid.
2018 * Wildcard Function::           How to cause wildcard expansion where
2019                                   it does not normally take place.
2020 @end menu
2021
2022 @node Wildcard Examples, Wildcard Pitfall, Wildcards, Wildcards
2023 @subsection Wildcard Examples
2024
2025 Wildcards can be used in the recipe of a rule, where they are expanded
2026 by the shell.  For example, here is a rule to delete all the object files:
2027
2028 @example
2029 @group
2030 clean:
2031         rm -f *.o
2032 @end group
2033 @end example
2034 @cindex @code{rm} (shell command)
2035
2036 Wildcards are also useful in the prerequisites of a rule.  With the
2037 following rule in the makefile, @samp{make print} will print all the
2038 @samp{.c} files that have changed since the last time you printed them:
2039
2040 @example
2041 print: *.c
2042         lpr -p $?
2043         touch print
2044 @end example
2045
2046 @cindex @code{print} target
2047 @cindex @code{lpr} (shell command)
2048 @cindex @code{touch} (shell command)
2049 @noindent
2050 This rule uses @file{print} as an empty target file; see @ref{Empty
2051 Targets, ,Empty Target Files to Record Events}.  (The automatic variable
2052 @samp{$?} is used to print only those files that have changed; see
2053 @ref{Automatic Variables}.)@refill
2054
2055 Wildcard expansion does not happen when you define a variable.  Thus, if
2056 you write this:
2057
2058 @example
2059 objects = *.o
2060 @end example
2061
2062 @noindent
2063 then the value of the variable @code{objects} is the actual string
2064 @samp{*.o}.  However, if you use the value of @code{objects} in a
2065 target or prerequisite, wildcard expansion will take place there.  If
2066 you use the value of @code{objects} in a recipe, the shell may perform
2067 wildcard expansion when the recipe runs.  To set @code{objects} to the
2068 expansion, instead use:
2069
2070 @example
2071 objects := $(wildcard *.o)
2072 @end example
2073
2074 @noindent
2075 @xref{Wildcard Function}.
2076
2077 @node Wildcard Pitfall, Wildcard Function, Wildcard Examples, Wildcards
2078 @subsection Pitfalls of Using Wildcards
2079 @cindex wildcard pitfalls
2080 @cindex pitfalls of wildcards
2081 @cindex mistakes with wildcards
2082 @cindex errors with wildcards
2083 @cindex problems with wildcards
2084
2085 Now here is an example of a naive way of using wildcard expansion, that
2086 does not do what you would intend.  Suppose you would like to say that the
2087 executable file @file{foo} is made from all the object files in the
2088 directory, and you write this:
2089
2090 @example
2091 objects = *.o
2092
2093 foo : $(objects)
2094         cc -o foo $(CFLAGS) $(objects)
2095 @end example
2096
2097 @noindent
2098 The value of @code{objects} is the actual string @samp{*.o}.  Wildcard
2099 expansion happens in the rule for @file{foo}, so that each @emph{existing}
2100 @samp{.o} file becomes a prerequisite of @file{foo} and will be recompiled if
2101 necessary.
2102
2103 But what if you delete all the @samp{.o} files?  When a wildcard matches
2104 no files, it is left as it is, so then @file{foo} will depend on the
2105 oddly-named file @file{*.o}.  Since no such file is likely to exist,
2106 @code{make} will give you an error saying it cannot figure out how to
2107 make @file{*.o}.  This is not what you want!
2108
2109 Actually it is possible to obtain the desired result with wildcard
2110 expansion, but you need more sophisticated techniques, including the
2111 @code{wildcard} function and string substitution.
2112 @ifnottex
2113 @xref{Wildcard Function, ,The Function @code{wildcard}}.
2114 @end ifnottex
2115 @iftex
2116 These are described in the following section.
2117 @end iftex
2118
2119 @cindex wildcards and MS-DOS/MS-Windows backslashes
2120 @cindex backslashes in pathnames and wildcard expansion
2121
2122 Microsoft operating systems (MS-DOS and MS-Windows) use backslashes to
2123 separate directories in pathnames, like so:
2124
2125 @example
2126   c:\foo\bar\baz.c
2127 @end example
2128
2129 This is equivalent to the Unix-style @file{c:/foo/bar/baz.c} (the
2130 @file{c:} part is the so-called drive letter).  When @code{make} runs on
2131 these systems, it supports backslashes as well as the Unix-style forward
2132 slashes in pathnames.  However, this support does @emph{not} include the
2133 wildcard expansion, where backslash is a quote character.  Therefore,
2134 you @emph{must} use Unix-style slashes in these cases.
2135
2136
2137 @node Wildcard Function,  , Wildcard Pitfall, Wildcards
2138 @subsection The Function @code{wildcard}
2139 @findex wildcard
2140
2141 Wildcard expansion happens automatically in rules.  But wildcard expansion
2142 does not normally take place when a variable is set, or inside the
2143 arguments of a function.  If you want to do wildcard expansion in such
2144 places, you need to use the @code{wildcard} function, like this:
2145
2146 @example
2147 $(wildcard @var{pattern}@dots{})
2148 @end example
2149
2150 @noindent
2151 This string, used anywhere in a makefile, is replaced by a
2152 space-separated list of names of existing files that match one of the
2153 given file name patterns.  If no existing file name matches a pattern,
2154 then that pattern is omitted from the output of the @code{wildcard}
2155 function.  Note that this is different from how unmatched wildcards
2156 behave in rules, where they are used verbatim rather than ignored
2157 (@pxref{Wildcard Pitfall}).
2158
2159 One use of the @code{wildcard} function is to get a list of all the C source
2160 files in a directory, like this:
2161
2162 @example
2163 $(wildcard *.c)
2164 @end example
2165
2166 We can change the list of C source files into a list of object files by
2167 replacing the @samp{.c} suffix with @samp{.o} in the result, like this:
2168
2169 @example
2170 $(patsubst %.c,%.o,$(wildcard *.c))
2171 @end example
2172
2173 @noindent
2174 (Here we have used another function, @code{patsubst}.
2175 @xref{Text Functions, ,Functions for String Substitution and Analysis}.)@refill
2176
2177 Thus, a makefile to compile all C source files in the directory and then
2178 link them together could be written as follows:
2179
2180 @example
2181 objects := $(patsubst %.c,%.o,$(wildcard *.c))
2182
2183 foo : $(objects)
2184         cc -o foo $(objects)
2185 @end example
2186
2187 @noindent
2188 (This takes advantage of the implicit rule for compiling C programs, so
2189 there is no need to write explicit rules for compiling the files.
2190 @xref{Flavors, ,The Two Flavors of Variables}, for an explanation of
2191 @samp{:=}, which is a variant of @samp{=}.)
2192
2193 @node Directory Search, Phony Targets, Wildcards, Rules
2194 @section Searching Directories for Prerequisites
2195 @vindex VPATH
2196 @findex vpath
2197 @cindex vpath
2198 @cindex search path for prerequisites (@code{VPATH})
2199 @cindex directory search (@code{VPATH})
2200
2201 For large systems, it is often desirable to put sources in a separate
2202 directory from the binaries.  The @dfn{directory search} features of
2203 @code{make} facilitate this by searching several directories
2204 automatically to find a prerequisite.  When you redistribute the files
2205 among directories, you do not need to change the individual rules,
2206 just the search paths.
2207
2208 @menu
2209 * General Search::              Specifying a search path that applies
2210                                   to every prerequisite.
2211 * Selective Search::            Specifying a search path
2212                                   for a specified class of names.
2213 * Search Algorithm::            When and how search paths are applied.
2214 * Recipes/Search::              How to write recipes that work together
2215                                   with search paths.
2216 * Implicit/Search::             How search paths affect implicit rules.
2217 * Libraries/Search::            Directory search for link libraries.
2218 @end menu
2219
2220 @node General Search, Selective Search, Directory Search, Directory Search
2221 @subsection @code{VPATH}: Search Path for All Prerequisites
2222 @vindex VPATH
2223
2224 The value of the @code{make} variable @code{VPATH} specifies a list of
2225 directories that @code{make} should search.  Most often, the
2226 directories are expected to contain prerequisite files that are not in the
2227 current directory; however, @code{make} uses @code{VPATH} as a search
2228 list for both prerequisites and targets of rules.
2229
2230 Thus, if a file that is listed as a target or prerequisite does not exist
2231 in the current directory, @code{make} searches the directories listed in
2232 @code{VPATH} for a file with that name.  If a file is found in one of
2233 them, that file may become the prerequisite (see below).  Rules may then
2234 specify the names of files in the prerequisite list as if they all
2235 existed in the current directory.  @xref{Recipes/Search, ,Writing Recipes with Directory Search}.
2236
2237 In the @code{VPATH} variable, directory names are separated by colons or
2238 blanks.  The order in which directories are listed is the order followed
2239 by @code{make} in its search.  (On MS-DOS and MS-Windows, semi-colons
2240 are used as separators of directory names in @code{VPATH}, since the
2241 colon can be used in the pathname itself, after the drive letter.)
2242
2243 For example,
2244
2245 @example
2246 VPATH = src:../headers
2247 @end example
2248
2249 @noindent
2250 specifies a path containing two directories, @file{src} and
2251 @file{../headers}, which @code{make} searches in that order.
2252
2253 With this value of @code{VPATH}, the following rule,
2254
2255 @example
2256 foo.o : foo.c
2257 @end example
2258
2259 @noindent
2260 is interpreted as if it were written like this:
2261
2262 @example
2263 foo.o : src/foo.c
2264 @end example
2265
2266 @noindent
2267 assuming the file @file{foo.c} does not exist in the current directory but
2268 is found in the directory @file{src}.
2269
2270 @node Selective Search, Search Algorithm, General Search, Directory Search
2271 @subsection The @code{vpath} Directive
2272 @findex vpath
2273
2274 Similar to the @code{VPATH} variable, but more selective, is the
2275 @code{vpath} directive (note lower case), which allows you to specify a
2276 search path for a particular class of file names: those that match a
2277 particular pattern.  Thus you can supply certain search directories for
2278 one class of file names and other directories (or none) for other file
2279 names.
2280
2281 There are three forms of the @code{vpath} directive:
2282
2283 @table @code
2284 @item vpath @var{pattern} @var{directories}
2285 Specify the search path @var{directories} for file names that match
2286 @var{pattern}.
2287
2288 The search path, @var{directories}, is a list of directories to be
2289 searched, separated by colons (semi-colons on MS-DOS and MS-Windows) or
2290 blanks, just like the search path used in the @code{VPATH} variable.
2291
2292 @item vpath @var{pattern}
2293 Clear out the search path associated with @var{pattern}.
2294
2295 @c Extra blank line makes sure this gets two lines.
2296 @item vpath
2297
2298 Clear all search paths previously specified with @code{vpath} directives.
2299 @end table
2300
2301 A @code{vpath} pattern is a string containing a @samp{%} character.  The
2302 string must match the file name of a prerequisite that is being searched
2303 for, the @samp{%} character matching any sequence of zero or more
2304 characters (as in pattern rules; @pxref{Pattern Rules, ,Defining and
2305 Redefining Pattern Rules}).  For example, @code{%.h} matches files that
2306 end in @code{.h}.  (If there is no @samp{%}, the pattern must match the
2307 prerequisite exactly, which is not useful very often.)
2308
2309 @cindex @code{%}, quoting in @code{vpath}
2310 @cindex @code{%}, quoting with @code{\} (backslash)
2311 @cindex @code{\} (backslash), to quote @code{%}
2312 @cindex backslash (@code{\}), to quote @code{%}
2313 @cindex quoting @code{%}, in @code{vpath}
2314 @samp{%} characters in a @code{vpath} directive's pattern can be quoted
2315 with preceding backslashes (@samp{\}).  Backslashes that would otherwise
2316 quote @samp{%} characters can be quoted with more backslashes.
2317 Backslashes that quote @samp{%} characters or other backslashes are
2318 removed from the pattern before it is compared to file names.  Backslashes
2319 that are not in danger of quoting @samp{%} characters go unmolested.@refill
2320
2321 When a prerequisite fails to exist in the current directory, if the
2322 @var{pattern} in a @code{vpath} directive matches the name of the
2323 prerequisite file, then the @var{directories} in that directive are searched
2324 just like (and before) the directories in the @code{VPATH} variable.
2325
2326 For example,
2327
2328 @example
2329 vpath %.h ../headers
2330 @end example
2331
2332 @noindent
2333 tells @code{make} to look for any prerequisite whose name ends in @file{.h}
2334 in the directory @file{../headers} if the file is not found in the current
2335 directory.
2336
2337 If several @code{vpath} patterns match the prerequisite file's name, then
2338 @code{make} processes each matching @code{vpath} directive one by one,
2339 searching all the directories mentioned in each directive.  @code{make}
2340 handles multiple @code{vpath} directives in the order in which they
2341 appear in the makefile; multiple directives with the same pattern are
2342 independent of each other.
2343
2344 @need 750
2345 Thus,
2346
2347 @example
2348 @group
2349 vpath %.c foo
2350 vpath %   blish
2351 vpath %.c bar
2352 @end group
2353 @end example
2354
2355 @noindent
2356 will look for a file ending in @samp{.c} in @file{foo}, then
2357 @file{blish}, then @file{bar}, while
2358
2359 @example
2360 @group
2361 vpath %.c foo:bar
2362 vpath %   blish
2363 @end group
2364 @end example
2365
2366 @noindent
2367 will look for a file ending in @samp{.c} in @file{foo}, then
2368 @file{bar}, then @file{blish}.
2369
2370 @node Search Algorithm, Recipes/Search, Selective Search, Directory Search
2371 @subsection How Directory Searches are Performed
2372 @cindex algorithm for directory search
2373 @cindex directory search algorithm
2374
2375 When a prerequisite is found through directory search, regardless of type
2376 (general or selective), the pathname located may not be the one that
2377 @code{make} actually provides you in the prerequisite list.  Sometimes
2378 the path discovered through directory search is thrown away.
2379
2380 The algorithm @code{make} uses to decide whether to keep or abandon a
2381 path found via directory search is as follows:
2382
2383 @enumerate
2384 @item
2385 If a target file does not exist at the path specified in the makefile,
2386 directory search is performed.
2387
2388 @item
2389 If the directory search is successful, that path is kept and this file
2390 is tentatively stored as the target.
2391
2392 @item
2393 All prerequisites of this target are examined using this same method.
2394
2395 @item
2396 After processing the prerequisites, the target may or may not need to be
2397 rebuilt:
2398
2399 @enumerate a
2400 @item
2401 If the target does @emph{not} need to be rebuilt, the path to the file
2402 found during directory search is used for any prerequisite lists which
2403 contain this target.  In short, if @code{make} doesn't need to rebuild
2404 the target then you use the path found via directory search.
2405
2406 @item
2407 If the target @emph{does} need to be rebuilt (is out-of-date), the
2408 pathname found during directory search is @emph{thrown away}, and the
2409 target is rebuilt using the file name specified in the makefile.  In
2410 short, if @code{make} must rebuild, then the target is rebuilt locally,
2411 not in the directory found via directory search.
2412 @end enumerate
2413 @end enumerate
2414
2415 This algorithm may seem complex, but in practice it is quite often
2416 exactly what you want.
2417
2418 @cindex traditional directory search (GPATH)
2419 @cindex directory search, traditional (GPATH)
2420 Other versions of @code{make} use a simpler algorithm: if the file does
2421 not exist, and it is found via directory search, then that pathname is
2422 always used whether or not the target needs to be built.  Thus, if the
2423 target is rebuilt it is created at the pathname discovered during
2424 directory search.
2425
2426 @vindex GPATH
2427 If, in fact, this is the behavior you want for some or all of your
2428 directories, you can use the @code{GPATH} variable to indicate this to
2429 @code{make}.
2430
2431 @code{GPATH} has the same syntax and format as @code{VPATH} (that is, a
2432 space- or colon-delimited list of pathnames).  If an out-of-date target
2433 is found by directory search in a directory that also appears in
2434 @code{GPATH}, then that pathname is not thrown away.  The target is
2435 rebuilt using the expanded path.
2436
2437 @node Recipes/Search, Implicit/Search, Search Algorithm, Directory Search
2438 @subsection Writing Recipes with Directory Search
2439 @cindex recipes, and directory search
2440 @cindex directory search (@code{VPATH}), and recipes
2441
2442 When a prerequisite is found in another directory through directory search,
2443 this cannot change the recipe of the rule; they will execute as written.
2444 Therefore, you must write the recipe with care so that it will look for
2445 the prerequisite in the directory where @code{make} finds it.
2446
2447 This is done with the @dfn{automatic variables} such as @samp{$^}
2448 (@pxref{Automatic Variables}).
2449 For instance, the value of @samp{$^} is a
2450 list of all the prerequisites of the rule, including the names of
2451 the directories in which they were found, and the value of
2452 @samp{$@@} is the target.  Thus:@refill
2453
2454 @example
2455 foo.o : foo.c
2456         cc -c $(CFLAGS) $^ -o $@@
2457 @end example
2458
2459 @noindent
2460 (The variable @code{CFLAGS} exists so you can specify flags for C
2461 compilation by implicit rules; we use it here for consistency so it will
2462 affect all C compilations uniformly;
2463 @pxref{Implicit Variables, ,Variables Used by Implicit Rules}.)
2464
2465 Often the prerequisites include header files as well, which you do not
2466 want to mention in the recipe.  The automatic variable @samp{$<} is
2467 just the first prerequisite:
2468
2469 @example
2470 VPATH = src:../headers
2471 foo.o : foo.c defs.h hack.h
2472         cc -c $(CFLAGS) $< -o $@@
2473 @end example
2474
2475 @node Implicit/Search, Libraries/Search, Recipes/Search, Directory Search
2476 @subsection Directory Search and Implicit Rules
2477 @cindex @code{VPATH}, and implicit rules
2478 @cindex directory search (@code{VPATH}), and implicit rules
2479 @cindex search path for prerequisites (@code{VPATH}), and implicit rules
2480 @cindex implicit rule, and directory search
2481 @cindex implicit rule, and @code{VPATH}
2482 @cindex rule, implicit, and directory search
2483 @cindex rule, implicit, and @code{VPATH}
2484
2485 The search through the directories specified in @code{VPATH} or with
2486 @code{vpath} also happens during consideration of implicit rules
2487 (@pxref{Implicit Rules, ,Using Implicit Rules}).
2488
2489 For example, when a file @file{foo.o} has no explicit rule, @code{make}
2490 considers implicit rules, such as the built-in rule to compile
2491 @file{foo.c} if that file exists.  If such a file is lacking in the
2492 current directory, the appropriate directories are searched for it.  If
2493 @file{foo.c} exists (or is mentioned in the makefile) in any of the
2494 directories, the implicit rule for C compilation is applied.
2495
2496 The recipes of implicit rules normally use automatic variables as a
2497 matter of necessity; consequently they will use the file names found by
2498 directory search with no extra effort.
2499
2500 @node Libraries/Search,  , Implicit/Search, Directory Search
2501 @subsection Directory Search for Link Libraries
2502 @cindex link libraries, and directory search
2503 @cindex libraries for linking, directory search
2504 @cindex directory search (@code{VPATH}), and link libraries
2505 @cindex @code{VPATH}, and link libraries
2506 @cindex search path for prerequisites (@code{VPATH}), and link libraries
2507 @cindex @code{-l} (library search)
2508 @cindex link libraries, patterns matching
2509 @cindex @code{.LIBPATTERNS}, and link libraries
2510 @vindex .LIBPATTERNS
2511
2512 Directory search applies in a special way to libraries used with the
2513 linker.  This special feature comes into play when you write a prerequisite
2514 whose name is of the form @samp{-l@var{name}}.  (You can tell something
2515 strange is going on here because the prerequisite is normally the name of a
2516 file, and the @emph{file name} of a library generally looks like
2517 @file{lib@var{name}.a}, not like @samp{-l@var{name}}.)@refill
2518
2519 When a prerequisite's name has the form @samp{-l@var{name}}, @code{make}
2520 handles it specially by searching for the file @file{lib@var{name}.so},
2521 and, if it is not found, for the file @file{lib@var{name}.a} in the current
2522 directory, in directories specified by matching @code{vpath}
2523 search paths and the @code{VPATH} search path, and then in the
2524 directories @file{/lib}, @file{/usr/lib}, and @file{@var{prefix}/lib}
2525 (normally @file{/usr/local/lib}, but MS-DOS/MS-Windows versions of
2526 @code{make} behave as if @var{prefix} is defined to be the root of the
2527 DJGPP installation tree).
2528
2529 For example, if there is a @file{/usr/lib/libcurses.a} library on your
2530 system (and no @file{/usr/lib/libcurses.so} file), then
2531
2532 @example
2533 @group
2534 foo : foo.c -lcurses
2535         cc $^ -o $@@
2536 @end group
2537 @end example
2538
2539 @noindent
2540 would cause the command @samp{cc foo.c /usr/lib/libcurses.a -o foo} to
2541 be executed when @file{foo} is older than @file{foo.c} or than
2542 @file{/usr/lib/libcurses.a}.@refill
2543
2544 Although the default set of files to be searched for is
2545 @file{lib@var{name}.so} and @file{lib@var{name}.a}, this is customizable
2546 via the @code{.LIBPATTERNS} variable.  Each word in the value of this
2547 variable is a pattern string.  When a prerequisite like
2548 @samp{-l@var{name}} is seen, @code{make} will replace the percent in
2549 each pattern in the list with @var{name} and perform the above directory
2550 searches using each library file name.
2551
2552 The default value for @code{.LIBPATTERNS} is @samp{lib%.so lib%.a},
2553 which provides the default behavior described above.
2554
2555 You can turn off link library expansion completely by setting this
2556 variable to an empty value.
2557
2558 @node Phony Targets, Force Targets, Directory Search, Rules
2559 @section Phony Targets
2560 @cindex phony targets
2561 @cindex targets, phony
2562 @cindex targets without a file
2563
2564 A phony target is one that is not really the name of a file; rather it
2565 is just a name for a recipe to be executed when you make an explicit
2566 request.  There are two reasons to use a phony target: to avoid a
2567 conflict with a file of the same name, and to improve performance.
2568
2569 If you write a rule whose recipe will not create the target file, the
2570 recipe will be executed every time the target comes up for remaking.
2571 Here is an example:
2572
2573 @example
2574 @group
2575 clean:
2576         rm *.o temp
2577 @end group
2578 @end example
2579
2580 @noindent
2581 Because the @code{rm} command does not create a file named @file{clean},
2582 probably no such file will ever exist.  Therefore, the @code{rm} command
2583 will be executed every time you say @samp{make clean}.
2584 @cindex @code{rm} (shell command)
2585
2586 @findex .PHONY
2587 The phony target will cease to work if anything ever does create a file
2588 named @file{clean} in this directory.  Since it has no prerequisites, the
2589 file @file{clean} would inevitably be considered up to date, and its
2590 recipe would not be executed.  To avoid this problem, you can explicitly
2591 declare the target to be phony, using the special target @code{.PHONY}
2592 (@pxref{Special Targets, ,Special Built-in Target Names}) as follows:
2593
2594 @example
2595 .PHONY : clean
2596 @end example
2597
2598 @noindent
2599 Once this is done, @samp{make clean} will run the recipe regardless of
2600 whether there is a file named @file{clean}.
2601
2602 Since it knows that phony targets do not name actual files that could be
2603 remade from other files, @code{make} skips the implicit rule search for
2604 phony targets (@pxref{Implicit Rules}).  This is why declaring a target
2605 phony is good for performance, even if you are not worried about the
2606 actual file existing.
2607
2608 Thus, you first write the line that states that @code{clean} is a
2609 phony target, then you write the rule, like this:
2610
2611 @example
2612 @group
2613 .PHONY: clean
2614 clean:
2615         rm *.o temp
2616 @end group
2617 @end example
2618
2619 Another example of the usefulness of phony targets is in conjunction
2620 with recursive invocations of @code{make} (for more information, see
2621 @ref{Recursion, ,Recursive Use of @code{make}}).  In this case the
2622 makefile will often contain a variable which lists a number of
2623 sub-directories to be built.  One way to handle this is with one rule
2624 whose recipe is a shell loop over the sub-directories, like this:
2625
2626 @example
2627 @group
2628 SUBDIRS = foo bar baz
2629
2630 subdirs:
2631         for dir in $(SUBDIRS); do \
2632           $(MAKE) -C $$dir; \
2633         done
2634 @end group
2635 @end example
2636
2637 There are problems with this method, however.  First, any error
2638 detected in a sub-make is ignored by this rule, so it will continue
2639 to build the rest of the directories even when one fails.  This can be
2640 overcome by adding shell commands to note the error and exit, but then
2641 it will do so even if @code{make} is invoked with the @code{-k}
2642 option, which is unfortunate.  Second, and perhaps more importantly,
2643 you cannot take advantage of @code{make}'s ability to build targets in
2644 parallel (@pxref{Parallel, ,Parallel Execution}), since there is only
2645 one rule.
2646
2647 By declaring the sub-directories as phony targets (you must do this as
2648 the sub-directory obviously always exists; otherwise it won't be built)
2649 you can remove these problems:
2650
2651 @example
2652 @group
2653 SUBDIRS = foo bar baz
2654
2655 .PHONY: subdirs $(SUBDIRS)
2656
2657 subdirs: $(SUBDIRS)
2658
2659 $(SUBDIRS):
2660         $(MAKE) -C $@@
2661
2662 foo: baz
2663 @end group
2664 @end example
2665
2666 Here we've also declared that the @file{foo} sub-directory cannot be
2667 built until after the @file{baz} sub-directory is complete; this kind of
2668 relationship declaration is particularly important when attempting
2669 parallel builds.
2670
2671 A phony target should not be a prerequisite of a real target file; if it
2672 is, its recipe will be run every time @code{make} goes to update that
2673 file.  As long as a phony target is never a prerequisite of a real
2674 target, the phony target recipe will be executed only when the phony
2675 target is a specified goal (@pxref{Goals, ,Arguments to Specify the
2676 Goals}).
2677
2678 Phony targets can have prerequisites.  When one directory contains multiple
2679 programs, it is most convenient to describe all of the programs in one
2680 makefile @file{./Makefile}.  Since the target remade by default will be the
2681 first one in the makefile, it is common to make this a phony target named
2682 @samp{all} and give it, as prerequisites, all the individual programs.  For
2683 example:
2684
2685 @example
2686 all : prog1 prog2 prog3
2687 .PHONY : all
2688
2689 prog1 : prog1.o utils.o
2690         cc -o prog1 prog1.o utils.o
2691
2692 prog2 : prog2.o
2693         cc -o prog2 prog2.o
2694
2695 prog3 : prog3.o sort.o utils.o
2696         cc -o prog3 prog3.o sort.o utils.o
2697 @end example
2698
2699 @noindent
2700 Now you can say just @samp{make} to remake all three programs, or
2701 specify as arguments the ones to remake (as in @samp{make prog1
2702 prog3}).  Phoniness is not inherited: the prerequisites of a phony
2703 target are not themselves phony, unless explicitly declared to be so.
2704
2705 When one phony target is a prerequisite of another, it serves as a subroutine
2706 of the other.  For example, here @samp{make cleanall} will delete the
2707 object files, the difference files, and the file @file{program}:
2708
2709 @example
2710 .PHONY: cleanall cleanobj cleandiff
2711
2712 cleanall : cleanobj cleandiff
2713         rm program
2714
2715 cleanobj :
2716         rm *.o
2717
2718 cleandiff :
2719         rm *.diff
2720 @end example
2721
2722 @node Force Targets, Empty Targets, Phony Targets, Rules
2723 @section Rules without Recipes or Prerequisites
2724 @cindex force targets
2725 @cindex targets, force
2726 @cindex @code{FORCE}
2727 @cindex rule, no recipe or prerequisites
2728
2729 If a rule has no prerequisites or recipe, and the target of the rule
2730 is a nonexistent file, then @code{make} imagines this target to have
2731 been updated whenever its rule is run.  This implies that all targets
2732 depending on this one will always have their recipe run.
2733
2734 An example will illustrate this:
2735
2736 @example
2737 @group
2738 clean: FORCE
2739         rm $(objects)
2740 FORCE:
2741 @end group
2742 @end example
2743
2744 Here the target @samp{FORCE} satisfies the special conditions, so the
2745 target @file{clean} that depends on it is forced to run its recipe.
2746 There is nothing special about the name @samp{FORCE}, but that is one
2747 name commonly used this way.
2748
2749 As you can see, using @samp{FORCE} this way has the same results as using
2750 @samp{.PHONY: clean}.
2751
2752 Using @samp{.PHONY} is more explicit and more efficient.  However,
2753 other versions of @code{make} do not support @samp{.PHONY}; thus
2754 @samp{FORCE} appears in many makefiles.  @xref{Phony Targets}.
2755
2756 @node Empty Targets, Special Targets, Force Targets, Rules
2757 @section Empty Target Files to Record Events
2758 @cindex empty targets
2759 @cindex targets, empty
2760 @cindex recording events with empty targets
2761
2762 The @dfn{empty target} is a variant of the phony target; it is used to hold
2763 recipes for an action that you request explicitly from time to time.
2764 Unlike a phony target, this target file can really exist; but the file's
2765 contents do not matter, and usually are empty.
2766
2767 The purpose of the empty target file is to record, with its
2768 last-modification time, when the rule's recipe was last executed.  It
2769 does so because one of the commands in the recipe is a @code{touch}
2770 command to update the target file.
2771
2772 The empty target file should have some prerequisites (otherwise it
2773 doesn't make sense).  When you ask to remake the empty target, the
2774 recipe is executed if any prerequisite is more recent than the target;
2775 in other words, if a prerequisite has changed since the last time you
2776 remade the target.  Here is an example:
2777
2778 @example
2779 print: foo.c bar.c
2780         lpr -p $?
2781         touch print
2782 @end example
2783 @cindex @code{print} target
2784 @cindex @code{lpr} (shell command)
2785 @cindex @code{touch} (shell command)
2786
2787 @noindent
2788 With this rule, @samp{make print} will execute the @code{lpr} command if
2789 either source file has changed since the last @samp{make print}.  The
2790 automatic variable @samp{$?} is used to print only those files that have
2791 changed (@pxref{Automatic Variables}).
2792
2793 @node Special Targets, Multiple Targets, Empty Targets, Rules
2794 @section Special Built-in Target Names
2795 @cindex special targets
2796 @cindex built-in special targets
2797 @cindex targets, built-in special
2798
2799 Certain names have special meanings if they appear as targets.
2800
2801 @table @code
2802 @findex .PHONY
2803 @item .PHONY
2804
2805 The prerequisites of the special target @code{.PHONY} are considered to
2806 be phony targets.  When it is time to consider such a target,
2807 @code{make} will run its recipe unconditionally, regardless of
2808 whether a file with that name exists or what its last-modification
2809 time is.  @xref{Phony Targets, ,Phony Targets}.
2810
2811 @findex .SUFFIXES
2812 @item .SUFFIXES
2813
2814 The prerequisites of the special target @code{.SUFFIXES} are the list
2815 of suffixes to be used in checking for suffix rules.
2816 @xref{Suffix Rules, , Old-Fashioned Suffix Rules}.
2817
2818 @findex .DEFAULT
2819 @item .DEFAULT
2820
2821 The recipe specified for @code{.DEFAULT} is used for any target for
2822 which no rules are found (either explicit rules or implicit rules).
2823 @xref{Last Resort}.  If a @code{.DEFAULT} recipe is specified, every
2824 file mentioned as a prerequisite, but not as a target in a rule, will have
2825 that recipe executed on its behalf.  @xref{Implicit Rule Search,
2826 ,Implicit Rule Search Algorithm}.
2827
2828 @findex .PRECIOUS
2829 @item .PRECIOUS
2830 @cindex precious targets
2831 @cindex preserving with @code{.PRECIOUS}
2832
2833 The targets which @code{.PRECIOUS} depends on are given the following
2834 special treatment: if @code{make} is killed or interrupted during the
2835 execution of their recipes, the target is not deleted.
2836 @xref{Interrupts, ,Interrupting or Killing @code{make}}.  Also, if the
2837 target is an intermediate file, it will not be deleted after it is no
2838 longer needed, as is normally done.  @xref{Chained Rules, ,Chains of
2839 Implicit Rules}.  In this latter respect it overlaps with the
2840 @code{.SECONDARY} special target.
2841
2842 You can also list the target pattern of an implicit rule (such as
2843 @samp{%.o}) as a prerequisite file of the special target @code{.PRECIOUS}
2844 to preserve intermediate files created by rules whose target patterns
2845 match that file's name.
2846
2847 @findex .INTERMEDIATE
2848 @item .INTERMEDIATE
2849 @cindex intermediate targets, explicit
2850
2851 The targets which @code{.INTERMEDIATE} depends on are treated as
2852 intermediate files.  @xref{Chained Rules, ,Chains of Implicit Rules}.
2853 @code{.INTERMEDIATE} with no prerequisites has no effect.
2854
2855 @findex .SECONDARY
2856 @item .SECONDARY
2857 @cindex secondary targets
2858 @cindex preserving with @code{.SECONDARY}
2859
2860 The targets which @code{.SECONDARY} depends on are treated as
2861 intermediate files, except that they are never automatically deleted.
2862 @xref{Chained Rules, ,Chains of Implicit Rules}.
2863
2864 @code{.SECONDARY} with no prerequisites causes all targets to be treated
2865 as secondary (i.e., no target is removed because it is considered
2866 intermediate).
2867
2868 @findex .SECONDEXPANSION
2869 @item .SECONDEXPANSION
2870
2871 If @code{.SECONDEXPANSION} is mentioned as a target anywhere in the
2872 makefile, then all prerequisite lists defined @emph{after} it appears
2873 will be expanded a second time after all makefiles have been read in.
2874 @xref{Secondary Expansion, ,Secondary Expansion}.
2875
2876 @findex .DELETE_ON_ERROR
2877 @item .DELETE_ON_ERROR
2878 @cindex removing targets on failure
2879
2880 If @code{.DELETE_ON_ERROR} is mentioned as a target anywhere in the
2881 makefile, then @code{make} will delete the target of a rule if it has
2882 changed and its recipe exits with a nonzero exit status, just as it
2883 does when it receives a signal.  @xref{Errors, ,Errors in Recipes}.
2884
2885 @findex .IGNORE
2886 @item .IGNORE
2887
2888 If you specify prerequisites for @code{.IGNORE}, then @code{make} will
2889 ignore errors in execution of the recipe for those particular files.
2890 The recipe for @code{.IGNORE} (if any) is ignored.
2891
2892 If mentioned as a target with no prerequisites, @code{.IGNORE} says to
2893 ignore errors in execution of recipes for all files.  This usage of
2894 @samp{.IGNORE} is supported only for historical compatibility.  Since
2895 this affects every recipe in the makefile, it is not very useful; we
2896 recommend you use the more selective ways to ignore errors in specific
2897 recipes.  @xref{Errors, ,Errors in Recipes}.
2898
2899 @findex .LOW_RESOLUTION_TIME
2900 @item .LOW_RESOLUTION_TIME
2901
2902 If you specify prerequisites for @code{.LOW_RESOLUTION_TIME},
2903 @command{make} assumes that these files are created by commands that
2904 generate low resolution time stamps.  The recipe for the
2905 @code{.LOW_RESOLUTION_TIME} target are ignored.
2906
2907 The high resolution file time stamps of many modern file systems
2908 lessen the chance of @command{make} incorrectly concluding that a file
2909 is up to date.  Unfortunately, some hosts do not provide a way to set a
2910 high resolution file time stamp, so commands like @samp{cp -p} that
2911 explicitly set a file's time stamp must discard its sub-second part.
2912 If a file is created by such a command, you should list it as a
2913 prerequisite of @code{.LOW_RESOLUTION_TIME} so that @command{make}
2914 does not mistakenly conclude that the file is out of date.  For
2915 example:
2916
2917 @example
2918 @group
2919 .LOW_RESOLUTION_TIME: dst
2920 dst: src
2921         cp -p src dst
2922 @end group
2923 @end example
2924
2925 Since @samp{cp -p} discards the sub-second part of @file{src}'s time
2926 stamp, @file{dst} is typically slightly older than @file{src} even when
2927 it is up to date.  The @code{.LOW_RESOLUTION_TIME} line causes
2928 @command{make} to consider @file{dst} to be up to date if its time stamp
2929 is at the start of the same second that @file{src}'s time stamp is in.
2930
2931 Due to a limitation of the archive format, archive member time stamps
2932 are always low resolution.  You need not list archive members as
2933 prerequisites of @code{.LOW_RESOLUTION_TIME}, as @command{make} does this
2934 automatically.
2935
2936 @findex .SILENT
2937 @item .SILENT
2938
2939 If you specify prerequisites for @code{.SILENT}, then @code{make} will
2940 not print the recipe used to remake those particular files before
2941 executing them.  The recipe for @code{.SILENT} is ignored.
2942
2943 If mentioned as a target with no prerequisites, @code{.SILENT} says not
2944 to print any recipes before executing them.  This usage of
2945 @samp{.SILENT} is supported only for historical compatibility.  We
2946 recommend you use the more selective ways to silence specific recipes.
2947 @xref{Echoing, ,Recipe Echoing}.  If you want to silence all recipes
2948 for a particular run of @code{make}, use the @samp{-s} or
2949 @w{@samp{--silent}} option (@pxref{Options Summary}).
2950
2951 @findex .EXPORT_ALL_VARIABLES
2952 @item .EXPORT_ALL_VARIABLES
2953
2954 Simply by being mentioned as a target, this tells @code{make} to
2955 export all variables to child processes by default.
2956 @xref{Variables/Recursion, ,Communicating Variables to a
2957 Sub-@code{make}}.
2958
2959 @findex .NOTPARALLEL
2960 @item .NOTPARALLEL
2961 @cindex parallel execution, overriding
2962
2963 If @code{.NOTPARALLEL} is mentioned as a target, then this invocation
2964 of @code{make} will be run serially, even if the @samp{-j} option is
2965 given.  Any recursively invoked @code{make} command will still run
2966 recipes in parallel (unless its makefile also contains this target).
2967 Any prerequisites on this target are ignored.
2968
2969 @findex .ONESHELL
2970 @item .ONESHELL
2971 @cindex recipe execution, single invocation
2972
2973 If @code{.ONESHELL} is mentioned as a target, then when a target is
2974 built all lines of the recipe will be given to a single invocation of
2975 the shell rather than each line being invoked separately
2976 (@pxref{Execution, ,Recipe Execution}).
2977
2978 @findex .POSIX
2979 @item .POSIX
2980 @cindex POSIX-conforming mode, setting
2981
2982 If @code{.POSIX} is mentioned as a target, then the makefile will be
2983 parsed and run in POSIX-conforming mode.  This does @emph{not} mean
2984 that only POSIX-conforming makefiles will be accepted: all advanced
2985 GNU @code{make} features are still available.  Rather, this target
2986 causes @code{make} to behave as required by POSIX in those areas
2987 where @code{make}'s default behavior differs.
2988
2989 In particular, if this target is mentioned then recipes will be
2990 invoked as if the shell had been passed the @code{-e} flag: the first
2991 failing command in a recipe will cause the recipe to fail immediately.
2992 @end table
2993
2994 Any defined implicit rule suffix also counts as a special target if it
2995 appears as a target, and so does the concatenation of two suffixes, such
2996 as @samp{.c.o}.  These targets are suffix rules, an obsolete way of
2997 defining implicit rules (but a way still widely used).  In principle, any
2998 target name could be special in this way if you break it in two and add
2999 both pieces to the suffix list.  In practice, suffixes normally begin with
3000 @samp{.}, so these special target names also begin with @samp{.}.
3001 @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
3002
3003 @node Multiple Targets, Multiple Rules, Special Targets, Rules
3004 @section Multiple Targets in a Rule
3005 @cindex multiple targets
3006 @cindex several targets in a rule
3007 @cindex targets, multiple
3008 @cindex rule, with multiple targets
3009
3010 A rule with multiple targets is equivalent to writing many rules, each with
3011 one target, and all identical aside from that.  The same recipe applies to
3012 all the targets, but its effect may vary because you can substitute the
3013 actual target name into the recipe using @samp{$@@}.  The rule contributes
3014 the same prerequisites to all the targets also.
3015
3016 This is useful in two cases.
3017
3018 @itemize @bullet
3019 @item
3020 You want just prerequisites, no recipe.  For example:
3021
3022 @example
3023 kbd.o command.o files.o: command.h
3024 @end example
3025
3026 @noindent
3027 gives an additional prerequisite to each of the three object files
3028 mentioned.
3029
3030 @item
3031 Similar recipes work for all the targets.  The recipes do not need
3032 to be absolutely identical, since the automatic variable @samp{$@@}
3033 can be used to substitute the particular target to be remade into the
3034 commands (@pxref{Automatic Variables}).  For example:
3035
3036 @example
3037 @group
3038 bigoutput littleoutput : text.g
3039         generate text.g -$(subst output,,$@@) > $@@
3040 @end group
3041 @end example
3042 @findex subst
3043
3044 @noindent
3045 is equivalent to
3046
3047 @example
3048 bigoutput : text.g
3049         generate text.g -big > bigoutput
3050 littleoutput : text.g
3051         generate text.g -little > littleoutput
3052 @end example
3053
3054 @noindent
3055 Here we assume the hypothetical program @code{generate} makes two
3056 types of output, one if given @samp{-big} and one if given
3057 @samp{-little}.
3058 @xref{Text Functions, ,Functions for String Substitution and Analysis},
3059 for an explanation of the @code{subst} function.
3060 @end itemize
3061
3062 Suppose you would like to vary the prerequisites according to the
3063 target, much as the variable @samp{$@@} allows you to vary the recipe.
3064 You cannot do this with multiple targets in an ordinary rule, but you
3065 can do it with a @dfn{static pattern rule}.  @xref{Static Pattern,
3066 ,Static Pattern Rules}.
3067
3068 @node Multiple Rules, Static Pattern, Multiple Targets, Rules
3069 @section Multiple Rules for One Target
3070 @cindex multiple rules for one target
3071 @cindex several rules for one target
3072 @cindex rule, multiple for one target
3073 @cindex target, multiple rules for one
3074
3075 One file can be the target of several rules.  All the prerequisites
3076 mentioned in all the rules are merged into one list of prerequisites for
3077 the target.  If the target is older than any prerequisite from any rule,
3078 the recipe is executed.
3079
3080 There can only be one recipe to be executed for a file.  If more than
3081 one rule gives a recipe for the same file, @code{make} uses the last
3082 one given and prints an error message.  (As a special case, if the
3083 file's name begins with a dot, no error message is printed.  This odd
3084 behavior is only for compatibility with other implementations of
3085 @code{make}@dots{} you should avoid using it).  Occasionally it is
3086 useful to have the same target invoke multiple recipes which are
3087 defined in different parts of your makefile; you can use
3088 @dfn{double-colon rules} (@pxref{Double-Colon}) for this.
3089
3090 An extra rule with just prerequisites can be used to give a few extra
3091 prerequisites to many files at once.  For example, makefiles often
3092 have a variable, such as @code{objects}, containing a list of all the
3093 compiler output files in the system being made.  An easy way to say
3094 that all of them must be recompiled if @file{config.h} changes is to
3095 write the following:
3096
3097 @example
3098 objects = foo.o bar.o
3099 foo.o : defs.h
3100 bar.o : defs.h test.h
3101 $(objects) : config.h
3102 @end example
3103
3104 This could be inserted or taken out without changing the rules that really
3105 specify how to make the object files, making it a convenient form to use if
3106 you wish to add the additional prerequisite intermittently.
3107
3108 Another wrinkle is that the additional prerequisites could be
3109 specified with a variable that you set with a command line argument to
3110 @code{make} (@pxref{Overriding, ,Overriding Variables}).  For example,
3111
3112 @example
3113 @group
3114 extradeps=
3115 $(objects) : $(extradeps)
3116 @end group
3117 @end example
3118
3119 @noindent
3120 means that the command @samp{make extradeps=foo.h} will consider
3121 @file{foo.h} as a prerequisite of each object file, but plain @samp{make}
3122 will not.
3123
3124 If none of the explicit rules for a target has a recipe, then @code{make}
3125 searches for an applicable implicit rule to find one
3126 @pxref{Implicit Rules, ,Using Implicit Rules}).
3127
3128 @node Static Pattern, Double-Colon, Multiple Rules, Rules
3129 @section Static Pattern Rules
3130 @cindex static pattern rule
3131 @cindex rule, static pattern
3132 @cindex pattern rules, static (not implicit)
3133 @cindex varying prerequisites
3134 @cindex prerequisites, varying (static pattern)
3135
3136 @dfn{Static pattern rules} are rules which specify multiple targets and
3137 construct the prerequisite names for each target based on the target name.
3138 They are more general than ordinary rules with multiple targets because the
3139 targets do not have to have identical prerequisites.  Their prerequisites must
3140 be @emph{analogous}, but not necessarily @emph{identical}.
3141
3142 @menu
3143 * Static Usage::                The syntax of static pattern rules.
3144 * Static versus Implicit::      When are they better than implicit rules?
3145 @end menu
3146
3147 @node Static Usage, Static versus Implicit, Static Pattern, Static Pattern
3148 @subsection Syntax of Static Pattern Rules
3149 @cindex static pattern rule, syntax of
3150 @cindex pattern rules, static, syntax of
3151
3152 Here is the syntax of a static pattern rule:
3153
3154 @example
3155 @var{targets} @dots{}: @var{target-pattern}: @var{prereq-patterns} @dots{}
3156         @var{recipe}
3157         @dots{}
3158 @end example
3159
3160 @noindent
3161 The @var{targets} list specifies the targets that the rule applies to.
3162 The targets can contain wildcard characters, just like the targets of
3163 ordinary rules (@pxref{Wildcards, ,Using Wildcard Characters in File
3164 Names}).
3165
3166 @cindex target pattern, static (not implicit)
3167 @cindex stem
3168 The @var{target-pattern} and @var{prereq-patterns} say how to compute the
3169 prerequisites of each target.  Each target is matched against the
3170 @var{target-pattern} to extract a part of the target name, called the
3171 @dfn{stem}.  This stem is substituted into each of the @var{prereq-patterns}
3172 to make the prerequisite names (one from each @var{prereq-pattern}).
3173
3174 Each pattern normally contains the character @samp{%} just once.  When the
3175 @var{target-pattern} matches a target, the @samp{%} can match any part of
3176 the target name; this part is called the @dfn{stem}.  The rest of the
3177 pattern must match exactly.  For example, the target @file{foo.o} matches
3178 the pattern @samp{%.o}, with @samp{foo} as the stem.  The targets
3179 @file{foo.c} and @file{foo.out} do not match that pattern.@refill
3180
3181 @cindex prerequisite pattern, static (not implicit)
3182 The prerequisite names for each target are made by substituting the stem
3183 for the @samp{%} in each prerequisite pattern.  For example, if one
3184 prerequisite pattern is @file{%.c}, then substitution of the stem
3185 @samp{foo} gives the prerequisite name @file{foo.c}.  It is legitimate
3186 to write a prerequisite pattern that does not contain @samp{%}; then this
3187 prerequisite is the same for all targets.
3188
3189 @cindex @code{%}, quoting in static pattern
3190 @cindex @code{%}, quoting with @code{\} (backslash)
3191 @cindex @code{\} (backslash), to quote @code{%}
3192 @cindex backslash (@code{\}), to quote @code{%}
3193 @cindex quoting @code{%}, in static pattern
3194 @samp{%} characters in pattern rules can be quoted with preceding
3195 backslashes (@samp{\}).  Backslashes that would otherwise quote @samp{%}
3196 characters can be quoted with more backslashes.  Backslashes that quote
3197 @samp{%} characters or other backslashes are removed from the pattern
3198 before it is compared to file names or has a stem substituted into it.
3199 Backslashes that are not in danger of quoting @samp{%} characters go
3200 unmolested.  For example, the pattern @file{the\%weird\\%pattern\\} has
3201 @samp{the%weird\} preceding the operative @samp{%} character, and
3202 @samp{pattern\\} following it.  The final two backslashes are left alone
3203 because they cannot affect any @samp{%} character.@refill
3204
3205 Here is an example, which compiles each of @file{foo.o} and @file{bar.o}
3206 from the corresponding @file{.c} file:
3207
3208 @example
3209 @group
3210 objects = foo.o bar.o
3211
3212 all: $(objects)
3213
3214 $(objects): %.o: %.c
3215         $(CC) -c $(CFLAGS) $< -o $@@
3216 @end group
3217 @end example
3218
3219 @noindent
3220 Here @samp{$<} is the automatic variable that holds the name of the
3221 prerequisite and @samp{$@@} is the automatic variable that holds the name
3222 of the target; see @ref{Automatic Variables}.
3223
3224 Each target specified must match the target pattern; a warning is issued
3225 for each target that does not.  If you have a list of files, only some of
3226 which will match the pattern, you can use the @code{filter} function to
3227 remove non-matching file names (@pxref{Text Functions, ,Functions for String Substitution and Analysis}):
3228
3229 @example
3230 files = foo.elc bar.o lose.o
3231
3232 $(filter %.o,$(files)): %.o: %.c
3233         $(CC) -c $(CFLAGS) $< -o $@@
3234 $(filter %.elc,$(files)): %.elc: %.el
3235         emacs -f batch-byte-compile $<
3236 @end example
3237
3238 @noindent
3239 In this example the result of @samp{$(filter %.o,$(files))} is
3240 @file{bar.o lose.o}, and the first static pattern rule causes each of
3241 these object files to be updated by compiling the corresponding C source
3242 file.  The result of @w{@samp{$(filter %.elc,$(files))}} is
3243 @file{foo.elc}, so that file is made from @file{foo.el}.@refill
3244
3245 Another example shows how to use @code{$*} in static pattern rules:
3246 @vindex $*@r{, and static pattern}
3247
3248 @example
3249 @group
3250 bigoutput littleoutput : %output : text.g
3251         generate text.g -$* > $@@
3252 @end group
3253 @end example
3254
3255 @noindent
3256 When the @code{generate} command is run, @code{$*} will expand to the
3257 stem, either @samp{big} or @samp{little}.
3258
3259 @node Static versus Implicit,  , Static Usage, Static Pattern
3260 @subsection Static Pattern Rules versus Implicit Rules
3261 @cindex rule, static pattern versus implicit
3262 @cindex static pattern rule, versus implicit
3263
3264 A static pattern rule has much in common with an implicit rule defined as a
3265 pattern rule (@pxref{Pattern Rules, ,Defining and Redefining Pattern Rules}).
3266 Both have a pattern for the target and patterns for constructing the
3267 names of prerequisites.  The difference is in how @code{make} decides
3268 @emph{when} the rule applies.
3269
3270 An implicit rule @emph{can} apply to any target that matches its pattern,
3271 but it @emph{does} apply only when the target has no recipe otherwise
3272 specified, and only when the prerequisites can be found.  If more than one
3273 implicit rule appears applicable, only one applies; the choice depends on
3274 the order of rules.
3275
3276 By contrast, a static pattern rule applies to the precise list of targets
3277 that you specify in the rule.  It cannot apply to any other target and it
3278 invariably does apply to each of the targets specified.  If two conflicting
3279 rules apply, and both have recipes, that's an error.
3280
3281 The static pattern rule can be better than an implicit rule for these
3282 reasons:
3283
3284 @itemize @bullet
3285 @item
3286 You may wish to override the usual implicit rule for a few
3287 files whose names cannot be categorized syntactically but
3288 can be given in an explicit list.
3289
3290 @item
3291 If you cannot be sure of the precise contents of the directories
3292 you are using, you may not be sure which other irrelevant files
3293 might lead @code{make} to use the wrong implicit rule.  The choice
3294 might depend on the order in which the implicit rule search is done.
3295 With static pattern rules, there is no uncertainty: each rule applies
3296 to precisely the targets specified.
3297 @end itemize
3298
3299 @node Double-Colon, Automatic Prerequisites, Static Pattern, Rules
3300 @section Double-Colon Rules
3301 @cindex double-colon rules
3302 @cindex rule, double-colon (@code{::})
3303 @cindex multiple rules for one target (@code{::})
3304 @cindex @code{::} rules (double-colon)
3305
3306 @dfn{Double-colon} rules are explicit rules written with @samp{::}
3307 instead of @samp{:} after the target names.  They are handled
3308 differently from ordinary rules when the same target appears in more
3309 than one rule.  Pattern rules with double-colons have an entirely
3310 different meaning (@pxref{Match-Anything Rules}).
3311
3312 When a target appears in multiple rules, all the rules must be the same
3313 type: all ordinary, or all double-colon.  If they are double-colon, each
3314 of them is independent of the others.  Each double-colon rule's recipe
3315 is executed if the target is older than any prerequisites of that rule.
3316 If there are no prerequisites for that rule, its recipe is always
3317 executed (even if the target already exists).  This can result in
3318 executing none, any, or all of the double-colon rules.
3319
3320 Double-colon rules with the same target are in fact completely separate
3321 from one another.  Each double-colon rule is processed individually, just
3322 as rules with different targets are processed.
3323
3324 The double-colon rules for a target are executed in the order they appear
3325 in the makefile.  However, the cases where double-colon rules really make
3326 sense are those where the order of executing the recipes would not matter.
3327
3328 Double-colon rules are somewhat obscure and not often very useful; they
3329 provide a mechanism for cases in which the method used to update a target
3330 differs depending on which prerequisite files caused the update, and such
3331 cases are rare.
3332
3333 Each double-colon rule should specify a recipe; if it does not, an
3334 implicit rule will be used if one applies.
3335 @xref{Implicit Rules, ,Using Implicit Rules}.
3336
3337 @node Automatic Prerequisites,  , Double-Colon, Rules
3338 @section Generating Prerequisites Automatically
3339 @cindex prerequisites, automatic generation
3340 @cindex automatic generation of prerequisites
3341 @cindex generating prerequisites automatically
3342
3343 In the makefile for a program, many of the rules you need to write often
3344 say only that some object file depends on some header
3345 file.  For example, if @file{main.c} uses @file{defs.h} via an
3346 @code{#include}, you would write:
3347
3348 @example
3349 main.o: defs.h
3350 @end example
3351
3352 @noindent
3353 You need this rule so that @code{make} knows that it must remake
3354 @file{main.o} whenever @file{defs.h} changes.  You can see that for a
3355 large program you would have to write dozens of such rules in your
3356 makefile.  And, you must always be very careful to update the makefile
3357 every time you add or remove an @code{#include}.
3358 @cindex @code{#include}
3359
3360 @cindex @code{-M} (to compiler)
3361 To avoid this hassle, most modern C compilers can write these rules for
3362 you, by looking at the @code{#include} lines in the source files.
3363 Usually this is done with the @samp{-M} option to the compiler.
3364 For example, the command:
3365
3366 @example
3367 cc -M main.c
3368 @end example
3369
3370 @noindent
3371 generates the output:
3372
3373 @example
3374 main.o : main.c defs.h
3375 @end example
3376
3377 @noindent
3378 Thus you no longer have to write all those rules yourself.
3379 The compiler will do it for you.
3380
3381 Note that such a rule constitutes mentioning @file{main.o} in a
3382 makefile, so it can never be considered an intermediate file by
3383 implicit rule search.  This means that @code{make} won't ever remove
3384 the file after using it; @pxref{Chained Rules, ,Chains of Implicit
3385 Rules}.
3386
3387 @cindex @code{make depend}
3388 With old @code{make} programs, it was traditional practice to use this
3389 compiler feature to generate prerequisites on demand with a command like
3390 @samp{make depend}.  That command would create a file @file{depend}
3391 containing all the automatically-generated prerequisites; then the
3392 makefile could use @code{include} to read them in (@pxref{Include}).
3393
3394 In GNU @code{make}, the feature of remaking makefiles makes this
3395 practice obsolete---you need never tell @code{make} explicitly to
3396 regenerate the prerequisites, because it always regenerates any makefile
3397 that is out of date.  @xref{Remaking Makefiles}.
3398
3399 The practice we recommend for automatic prerequisite generation is to have
3400 one makefile corresponding to each source file.  For each source file
3401 @file{@var{name}.c} there is a makefile @file{@var{name}.d} which lists
3402 what files the object file @file{@var{name}.o} depends on.  That way
3403 only the source files that have changed need to be rescanned to produce
3404 the new prerequisites.
3405
3406 Here is the pattern rule to generate a file of prerequisites (i.e., a makefile)
3407 called @file{@var{name}.d} from a C source file called @file{@var{name}.c}:
3408
3409 @smallexample
3410 @group
3411 %.d: %.c
3412         @@set -e; rm -f $@@; \
3413          $(CC) -M $(CPPFLAGS) $< > $@@.$$$$; \
3414          sed 's,\($*\)\.o[ :]*,\1.o $@@ : ,g' < $@@.$$$$ > $@@; \
3415          rm -f $@@.$$$$
3416 @end group
3417 @end smallexample
3418
3419 @noindent
3420 @xref{Pattern Rules}, for information on defining pattern rules.  The
3421 @samp{-e} flag to the shell causes it to exit immediately if the
3422 @code{$(CC)} command (or any other command) fails (exits with a
3423 nonzero status).
3424 @cindex @code{-e} (shell flag)
3425
3426 @cindex @code{-MM} (to GNU compiler)
3427 With the GNU C compiler, you may wish to use the @samp{-MM} flag instead
3428 of @samp{-M}.  This omits prerequisites on system header files.
3429 @xref{Preprocessor Options, , Options Controlling the Preprocessor,
3430 gcc, Using GNU CC}, for details.
3431
3432 @cindex @code{sed} (shell command)
3433 The purpose of the @code{sed} command is to translate (for example):
3434
3435 @example
3436 main.o : main.c defs.h
3437 @end example
3438
3439 @noindent
3440 into:
3441
3442 @example
3443 main.o main.d : main.c defs.h
3444 @end example
3445
3446 @noindent
3447 @cindex @code{.d}
3448 This makes each @samp{.d} file depend on all the source and header files
3449 that the corresponding @samp{.o} file depends on.  @code{make} then
3450 knows it must regenerate the prerequisites whenever any of the source or
3451 header files changes.
3452
3453 Once you've defined the rule to remake the @samp{.d} files,
3454 you then use the @code{include} directive to read them all in.
3455 @xref{Include}.  For example:
3456
3457 @example
3458 @group
3459 sources = foo.c bar.c
3460
3461 include $(sources:.c=.d)
3462 @end group
3463 @end example
3464
3465 @noindent
3466 (This example uses a substitution variable reference to translate the
3467 list of source files @samp{foo.c bar.c} into a list of prerequisite
3468 makefiles, @samp{foo.d bar.d}.  @xref{Substitution Refs}, for full
3469 information on substitution references.)  Since the @samp{.d} files are
3470 makefiles like any others, @code{make} will remake them as necessary
3471 with no further work from you.  @xref{Remaking Makefiles}.
3472
3473 Note that the @samp{.d} files contain target definitions; you should
3474 be sure to place the @code{include} directive @emph{after} the first,
3475 default goal in your makefiles or run the risk of having a random
3476 object file become the default goal.
3477 @xref{How Make Works}.
3478
3479 @node Recipes, Using Variables, Rules, Top
3480 @chapter Writing Recipes in Rules
3481 @cindex recipes
3482 @cindex recipes, how to write
3483 @cindex writing recipes
3484
3485 The recipe of a rule consists of one or more shell command lines to
3486 be executed, one at a time, in the order they appear.  Typically, the
3487 result of executing these commands is that the target of the rule is
3488 brought up to date.
3489
3490 Users use many different shell programs, but recipes in makefiles are
3491 always interpreted by @file{/bin/sh} unless the makefile specifies
3492 otherwise.  @xref{Execution, ,Recipe Execution}.
3493
3494 @menu
3495 * Recipe Syntax::               Recipe syntax features and pitfalls.
3496 * Echoing::                     How to control when recipes are echoed.
3497 * Execution::                   How recipes are executed.
3498 * Parallel::                    How recipes can be executed in parallel.
3499 * Errors::                      What happens after a recipe execution error.
3500 * Interrupts::                  What happens when a recipe is interrupted.
3501 * Recursion::                   Invoking @code{make} from makefiles.
3502 * Canned Recipes::              Defining canned recipes.
3503 * Empty Recipes::               Defining useful, do-nothing recipes.
3504 @end menu
3505
3506 @node Recipe Syntax, Echoing, Recipes, Recipes
3507 @section Recipe Syntax
3508 @cindex recipe syntax
3509 @cindex syntax of recipe
3510
3511 Makefiles have the unusual property that there are really two distinct
3512 syntaxes in one file.  Most of the makefile uses @code{make} syntax
3513 (@pxref{Makefiles, ,Writing Makefiles}).  However, recipes are meant
3514 to be interpreted by the shell and so they are written using shell
3515 syntax.  The @code{make} program does not try to understand shell
3516 syntax: it performs only a very few specific translations on the
3517 content of the recipe before handing it to the shell.
3518
3519 Each line in the recipe must start with a tab (or the first character
3520 in the value of the @code{.RECIPEPREFIX} variable; @pxref{Special
3521 Variables}), except that the first recipe line may be attached to the
3522 target-and-prerequisites line with a semicolon in between.  @emph{Any}
3523 line in the makefile that begins with a tab and appears in a ``rule
3524 context'' (that is, after a rule has been started until another rule
3525 or variable definition) will be considered part of a recipe for that
3526 rule.  Blank lines and lines of just comments may appear among the
3527 recipe lines; they are ignored.
3528
3529 Some consequences of these rules include:
3530
3531 @itemize @bullet
3532 @item
3533 A blank line that begins with a tab is not blank: it's an empty
3534 recipe (@pxref{Empty Recipes}).
3535
3536 @cindex comments, in recipes
3537 @cindex recipes, comments in
3538 @cindex @code{#} (comments), in recipes
3539 @item
3540 A comment in a recipe is not a @code{make} comment; it will be
3541 passed to the shell as-is.  Whether the shell treats it as a comment
3542 or not depends on your shell.
3543
3544 @item
3545 A variable definition in a ``rule context'' which is indented by a tab
3546 as the first character on the line, will be considered part of a
3547 recipe, not a @code{make} variable definition, and passed to the
3548 shell.
3549
3550 @item
3551 A conditional expression (@code{ifdef}, @code{ifeq},
3552 etc. @pxref{Conditional Syntax, ,Syntax of Conditionals}) in a ``rule
3553 context'' which is indented by a tab as the first character on the
3554 line, will be considered part of a recipe and be passed to the shell.
3555
3556 @end itemize
3557
3558 @menu
3559 * Splitting Recipe Lines::      Breaking long recipe lines for readability.
3560 * Variables in Recipes::        Using @code{make} variables in recipes.
3561 @end menu
3562
3563 @node Splitting Recipe Lines, Variables in Recipes, Recipe Syntax, Recipe Syntax
3564 @subsection Splitting Recipe Lines
3565 @cindex recipes, splitting
3566 @cindex splitting recipes
3567 @cindex recipes, backslash (@code{\}) in
3568 @cindex recipes, quoting newlines in
3569 @cindex backslash (@code{\}), in recipes
3570 @cindex @code{\} (backslash), in recipes
3571 @cindex quoting newline, in recipes
3572 @cindex newline, quoting, in recipes
3573
3574 One of the few ways in which @code{make} does interpret recipes is
3575 checking for a backslash just before the newline.  As in normal
3576 makefile syntax, a single logical recipe line can be split into
3577 multiple physical lines in the makefile by placing a backslash before
3578 each newline.  A sequence of lines like this is considered a single
3579 recipe line, and one instance of the shell will be invoked to run it.
3580
3581 However, in contrast to how they are treated in other places in a
3582 makefile (@pxref{Splitting Lines, , Splitting Long Lines}),
3583 backslash/newline pairs are @emph{not} removed from the recipe.  Both
3584 the backslash and the newline characters are preserved and passed to
3585 the shell.  How the backslash/newline is interpreted depends on your
3586 shell.  If the first character of the next line after the
3587 backslash/newline is the recipe prefix character (a tab by default;
3588 @pxref{Special Variables}), then that character (and only that
3589 character) is removed.  Whitespace is never added to the recipe.
3590
3591 For example, the recipe for the all target in this makefile:
3592
3593 @example
3594 @group
3595 all :
3596         @@echo no\
3597 space
3598         @@echo no\
3599         space
3600         @@echo one \
3601         space
3602         @@echo one\
3603          space
3604 @end group
3605 @end example
3606
3607 @noindent
3608 consists of four separate shell commands where the output is:
3609
3610 @example
3611 @group
3612 nospace
3613 nospace
3614 one space
3615 one space
3616 @end group
3617 @end example
3618
3619 As a more complex example, this makefile:
3620
3621 @example
3622 @group
3623 all : ; @@echo 'hello \
3624         world' ; echo "hello \
3625     world"
3626 @end group
3627 @end example
3628
3629 @noindent
3630 will invoke one shell with a command of:
3631
3632 @example
3633 @group
3634 echo 'hello \
3635 world' ; echo "hello \
3636     world"
3637 @end group
3638 @end example
3639
3640 @noindent
3641 which, according to shell quoting rules, will yield the following output:
3642
3643 @example
3644 @group
3645 hello \
3646 world
3647 hello     world
3648 @end group
3649 @end example
3650
3651 @noindent
3652 Notice how the backslash/newline pair was removed inside the string
3653 quoted with double quotes (@code{"@dots{}"}), but not from the string
3654 quoted with single quotes (@code{'@dots{}'}).  This is the way the
3655 default shell (@file{/bin/sh}) handles backslash/newline pairs.  If
3656 you specify a different shell in your makefiles it may treat them
3657 differently.
3658
3659 Sometimes you want to split a long line inside of single quotes, but
3660 you don't want the backslash/newline to appear in the quoted content.
3661 This is often the case when passing scripts to languages such as Perl,
3662 where extraneous backslashes inside the script can change its meaning
3663 or even be a syntax error.  One simple way of handling this is to
3664 place the quoted string, or even the entire command, into a
3665 @code{make} variable then use the variable in the recipe.  In this
3666 situation the newline quoting rules for makefiles will be used, and
3667 the backslash/newline will be removed.  If we rewrite our example
3668 above using this method:
3669
3670 @example
3671 @group
3672 HELLO = 'hello \
3673 world'
3674
3675 all : ; @@echo $(HELLO)
3676 @end group
3677 @end example
3678
3679 @noindent
3680 we will get output like this:
3681
3682 @example
3683 @group
3684 hello world
3685 @end group
3686 @end example
3687
3688 If you like, you can also use target-specific variables
3689 (@pxref{Target-specific, ,Target-specific Variable Values}) to obtain
3690 a tighter correspondence between the variable and the recipe that
3691 uses it.
3692
3693 @node Variables in Recipes,  , Splitting Recipe Lines, Recipe Syntax
3694 @subsection Using Variables in Recipes
3695 @cindex variable references in recipes
3696 @cindex recipes, using variables in
3697
3698 The other way in which @code{make} processes recipes is by expanding
3699 any variable references in them (@pxref{Reference,Basics of Variable
3700 References}).  This occurs after make has finished reading all the
3701 makefiles and the target is determined to be out of date; so, the
3702 recipes for targets which are not rebuilt are never expanded.
3703
3704 Variable and function references in recipes have identical syntax and
3705 semantics to references elsewhere in the makefile.  They also have the
3706 same quoting rules: if you want a dollar sign to appear in your
3707 recipe, you must double it (@samp{$$}).  For shells like the default
3708 shell, that use dollar signs to introduce variables, it's important to
3709 keep clear in your mind whether the variable you want to reference is
3710 a @code{make} variable (use a single dollar sign) or a shell variable
3711 (use two dollar signs).  For example:
3712
3713 @example
3714 @group
3715 LIST = one two three
3716 all:
3717         for i in $(LIST); do \
3718             echo $$i; \
3719         done
3720 @end group
3721 @end example
3722
3723 @noindent
3724 results in the following command being passed to the shell:
3725
3726 @example
3727 @group
3728 for i in one two three; do \
3729     echo $i; \
3730 done
3731 @end group
3732 @end example
3733
3734 @noindent
3735 which generates the expected result:
3736
3737 @example
3738 @group
3739 one
3740 two
3741 three
3742 @end group
3743 @end example
3744
3745 @node Echoing, Execution, Recipe Syntax, Recipes
3746 @section Recipe Echoing
3747 @cindex echoing of recipes
3748 @cindex silent operation
3749 @cindex @code{@@} (in recipes)
3750 @cindex recipes, echoing
3751 @cindex printing of recipes
3752
3753 Normally @code{make} prints each line of the recipe before it is
3754 executed.  We call this @dfn{echoing} because it gives the appearance
3755 that you are typing the lines yourself.
3756
3757 When a line starts with @samp{@@}, the echoing of that line is suppressed.
3758 The @samp{@@} is discarded before the line is passed to the shell.
3759 Typically you would use this for a command whose only effect is to print
3760 something, such as an @code{echo} command to indicate progress through
3761 the makefile:
3762
3763 @example
3764 @@echo About to make distribution files
3765 @end example
3766
3767 @cindex @code{-n}
3768 @cindex @code{--just-print}
3769 @cindex @code{--dry-run}
3770 @cindex @code{--recon}
3771 When @code{make} is given the flag @samp{-n} or @samp{--just-print} it
3772 only echoes most recipes, without executing them.  @xref{Options
3773 Summary, ,Summary of Options}.  In this case even the recipe lines
3774 starting with @samp{@@} are printed.  This flag is useful for finding
3775 out which recipes @code{make} thinks are necessary without actually
3776 doing them.
3777
3778 @cindex @code{-s}
3779 @cindex @code{--silent}
3780 @cindex @code{--quiet}
3781 @findex .SILENT
3782 The @samp{-s} or @samp{--silent}
3783 flag to @code{make} prevents all echoing, as if all recipes
3784 started with @samp{@@}.  A rule in the makefile for the special target
3785 @code{.SILENT} without prerequisites has the same effect
3786 (@pxref{Special Targets, ,Special Built-in Target Names}).
3787 @code{.SILENT} is essentially obsolete since @samp{@@} is more flexible.@refill
3788
3789 @node Execution, Parallel, Echoing, Recipes
3790 @section Recipe Execution
3791 @cindex recipe, execution
3792 @cindex execution, of recipes
3793 @vindex @code{SHELL} @r{(recipe execution)}
3794
3795 When it is time to execute recipes to update a target, they are
3796 executed by invoking a new sub-shell for each line of the recipe,
3797 unless the @code{.ONESHELL} special target is in effect
3798 (@pxref{One Shell, ,Using One Shell})  (In practice, @code{make} may
3799 take shortcuts that do not affect the results.)
3800
3801 @cindex @code{cd} (shell command)
3802 @cindex shell variables, setting in recipes
3803 @cindex recipes setting shell variables
3804 @strong{Please note:} this implies that setting shell variables and
3805 invoking shell commands such as @code{cd} that set a context local to
3806 each process will not affect the following lines in the recipe.@footnote{On
3807 MS-DOS, the value of current working directory is @strong{global}, so
3808 changing it @emph{will} affect the following recipe lines on those
3809 systems.}  If you want to use @code{cd} to affect the next statement,
3810 put both statements in a single recipe line.  Then @code{make} will
3811 invoke one shell to run the entire line, and the shell will execute
3812 the statements in sequence.  For example:
3813
3814 @example
3815 foo : bar/lose
3816         cd $(@@D) && gobble $(@@F) > ../$@@
3817 @end example
3818
3819 @noindent
3820 Here we use the shell AND operator (@code{&&}) so that if the
3821 @code{cd} command fails, the script will fail without trying to invoke
3822 the @code{gobble} command in the wrong directory, which could cause
3823 problems (in this case it would certainly cause @file{../foo} to be
3824 truncated, at least).
3825
3826 @menu
3827 * One Shell::                   One shell for all lines in a recipe.
3828 * Choosing the Shell::          How @code{make} chooses the shell used
3829                                   to run recipes.
3830 @end menu
3831
3832 @node One Shell, Choosing the Shell, Execution, Execution
3833 @subsection Using One Shell
3834 @cindex recipe lines, single shell
3835 @cindex @code{.ONESHELL}, use of
3836 @findex .ONESHELL
3837
3838 Sometimes you would prefer that all the lines in the recipe be passed
3839 to a single invocation of the shell.  There are generally two
3840 situations where this is useful: first, it can improve performance in
3841 makefiles where recipes consist of many command lines, by avoiding
3842 extra processes.  Second, you might want newlines to be included in
3843 your recipe command (for example perhaps you are using a very
3844 different interpreter as your @code{SHELL}).  If the @code{.ONESHELL}
3845 special target appears anywhere in the makefile then @emph{all}
3846 recipe lines for each target will be provided to a single invocation
3847 of the shell.  Newlines between recipe lines will be preserved.  For
3848 example:
3849
3850 @example
3851 .ONESHELL:
3852 foo : bar/lose
3853         cd $(@@D)
3854         gobble $(@@F) > ../$@@
3855 @end example
3856
3857 @noindent
3858 would now work as expected even though the commands are on different
3859 recipe lines.
3860
3861 If @code{.ONESHELL} is provided, then only the first line of the
3862 recipe will be checked for the special prefix characters (@samp{@@},
3863 @samp{-}, and @samp{+}).  Subsequent lines will include the special
3864 characters in the recipe line when the @code{SHELL} is invoked.  If
3865 you want your recipe to start with one of these special characters
3866 you'll need to arrange for them to not be the first characters on the
3867 first line, perhaps by adding a comment or similar.  For example, this
3868 would be a syntax error in Perl because the first @samp{@@} is removed
3869 by make:
3870
3871 @example
3872 .ONESHELL:
3873 SHELL = /usr/bin/perl
3874 .SHELLFLAGS = -e
3875 show :
3876         @@f = qw(a b c);
3877         print "@@f\n";
3878 @end example
3879
3880 @noindent
3881 However, either of these alternatives would work properly:
3882
3883 @example
3884 .ONESHELL:
3885 SHELL = /usr/bin/perl
3886 .SHELLFLAGS = -e
3887 show :
3888         # Make sure "@@" is not the first character on the first line
3889         @@f = qw(a b c);
3890         print "@@f\n";
3891 @end example
3892
3893 @noindent
3894 or
3895
3896 @example
3897 .ONESHELL:
3898 SHELL = /usr/bin/perl
3899 .SHELLFLAGS = -e
3900 show :
3901         my @@f = qw(a b c);
3902         print "@@f\n";
3903 @end example
3904
3905 As a special feature, if @code{SHELL} is determined to be a
3906 POSIX-style shell, the special prefix characters in ``internal''
3907 recipe lines will @emph{removed} before the recipe is processed.  This
3908 feature is intended to allow existing makefiles to add the
3909 @code{.ONESHELL} special target and still run properly without
3910 extensive modifications.  Since the special prefix characters are not
3911 legal at the beginning of a line in a POSIX shell script this is not a
3912 loss in functionality.  For example, this works as expected:
3913
3914 @example
3915 .ONESHELL:
3916 foo : bar/lose
3917         @@cd $(@@D)
3918         @@gobble $(@@F) > ../$@@
3919 @end example
3920
3921 Even with this special feature, however, makefiles with
3922 @code{.ONESHELL} will behave differently in ways that could be
3923 noticeable.  For example, normally if any line in the recipe fails,
3924 that causes the rule to fail and no more recipe lines are processed.
3925 Under @code{.ONESHELL} a failure of any but the final recipe line will
3926 not be noticed by @code{make}.  You can modify @code{.SHELLFLAGS} to
3927 add the @code{-e} option to the shell which will cause any failure
3928 anywhere in the command line to cause the shell to fail, but this
3929 could itself cause your recipe to behave differently.  Ultimately you
3930 may need to harden your recipe lines to allow them to work with
3931 @code{.ONESHELL}.
3932
3933 @node Choosing the Shell,  , One Shell, Execution
3934 @subsection Choosing the Shell
3935 @cindex shell, choosing the
3936 @cindex @code{SHELL}, value of
3937 @cindex @code{.SHELLFLAGS}, value of
3938
3939 @vindex SHELL
3940 @vindex .SHELLFLAGS
3941 The program used as the shell is taken from the variable @code{SHELL}.
3942 If this variable is not set in your makefile, the program
3943 @file{/bin/sh} is used as the shell.  The argument(s) passed to the
3944 shell are taken from the variable @code{.SHELLFLAGS}.  The default
3945 value of @code{.SHELLFLAGS} is @code{-c} normally, or @code{-ec} in
3946 POSIX-conforming mode.
3947
3948 @cindex environment, @code{SHELL} in
3949 Unlike most variables, the variable @code{SHELL} is never set from the
3950 environment.  This is because the @code{SHELL} environment variable is
3951 used to specify your personal choice of shell program for interactive
3952 use.  It would be very bad for personal choices like this to affect the
3953 functioning of makefiles.  @xref{Environment, ,Variables from the
3954 Environment}.
3955
3956 Furthermore, when you do set @code{SHELL} in your makefile that value
3957 is @emph{not} exported in the environment to recipe lines that
3958 @code{make} invokes.  Instead, the value inherited from the user's
3959 environment, if any, is exported.  You can override this behavior by
3960 explicitly exporting @code{SHELL} (@pxref{Variables/Recursion,
3961 ,Communicating Variables to a Sub-@code{make}}), forcing it to be
3962 passed in the environment to recipe lines.
3963
3964 @vindex @code{MAKESHELL} @r{(MS-DOS alternative to @code{SHELL})}
3965 However, on MS-DOS and MS-Windows the value of @code{SHELL} in the
3966 environment @strong{is} used, since on those systems most users do not
3967 set this variable, and therefore it is most likely set specifically to
3968 be used by @code{make}.  On MS-DOS, if the setting of @code{SHELL} is
3969 not suitable for @code{make}, you can set the variable
3970 @code{MAKESHELL} to the shell that @code{make} should use; if set it
3971 will be used as the shell instead of the value of @code{SHELL}.
3972
3973 @subsubheading Choosing a Shell in DOS and Windows
3974 @cindex shell, in DOS and Windows
3975 @cindex DOS, choosing a shell in
3976 @cindex Windows, choosing a shell in
3977
3978 Choosing a shell in MS-DOS and MS-Windows is much more complex than on
3979 other systems.
3980
3981 @vindex COMSPEC
3982 On MS-DOS, if @code{SHELL} is not set, the value of the variable
3983 @code{COMSPEC} (which is always set) is used instead.
3984
3985 @cindex @code{SHELL}, MS-DOS specifics
3986 The processing of lines that set the variable @code{SHELL} in Makefiles
3987 is different on MS-DOS.  The stock shell, @file{command.com}, is
3988 ridiculously limited in its functionality and many users of @code{make}
3989 tend to install a replacement shell.  Therefore, on MS-DOS, @code{make}
3990 examines the value of @code{SHELL}, and changes its behavior based on
3991 whether it points to a Unix-style or DOS-style shell.  This allows
3992 reasonable functionality even if @code{SHELL} points to
3993 @file{command.com}.
3994
3995 If @code{SHELL} points to a Unix-style shell, @code{make} on MS-DOS
3996 additionally checks whether that shell can indeed be found; if not, it
3997 ignores the line that sets @code{SHELL}.  In MS-DOS, GNU @code{make}
3998 searches for the shell in the following places:
3999
4000 @enumerate
4001 @item
4002 In the precise place pointed to by the value of @code{SHELL}.  For
4003 example, if the makefile specifies @samp{SHELL = /bin/sh}, @code{make}
4004 will look in the directory @file{/bin} on the current drive.
4005
4006 @item
4007 In the current directory.
4008
4009 @item
4010 In each of the directories in the @code{PATH} variable, in order.
4011
4012 @end enumerate
4013
4014 In every directory it examines, @code{make} will first look for the
4015 specific file (@file{sh} in the example above).  If this is not found,
4016 it will also look in that directory for that file with one of the known
4017 extensions which identify executable files.  For example @file{.exe},
4018 @file{.com}, @file{.bat}, @file{.btm}, @file{.sh}, and some others.
4019
4020 If any of these attempts is successful, the value of @code{SHELL} will
4021 be set to the full pathname of the shell as found.  However, if none of
4022 these is found, the value of @code{SHELL} will not be changed, and thus
4023 the line that sets it will be effectively ignored.  This is so
4024 @code{make} will only support features specific to a Unix-style shell if
4025 such a shell is actually installed on the system where @code{make} runs.
4026
4027 Note that this extended search for the shell is limited to the cases
4028 where @code{SHELL} is set from the Makefile; if it is set in the
4029 environment or command line, you are expected to set it to the full
4030 pathname of the shell, exactly as things are on Unix.
4031
4032 The effect of the above DOS-specific processing is that a Makefile that
4033 contains @samp{SHELL = /bin/sh} (as many Unix makefiles do), will work
4034 on MS-DOS unaltered if you have e.g.@: @file{sh.exe} installed in some
4035 directory along your @code{PATH}.
4036
4037 @vindex SHELL
4038 @vindex .SHELLFLAGS
4039
4040 @node Parallel, Errors, Execution, Recipes
4041 @section Parallel Execution
4042 @cindex recipes, execution in parallel
4043 @cindex parallel execution
4044 @cindex execution, in parallel
4045 @cindex job slots
4046 @cindex @code{-j}
4047 @cindex @code{--jobs}
4048
4049 GNU @code{make} knows how to execute several recipes at once.
4050 Normally, @code{make} will execute only one recipe at a time, waiting
4051 for it to finish before executing the next.  However, the @samp{-j} or
4052 @samp{--jobs} option tells @code{make} to execute many recipes
4053 simultaneously.  You can inhibit parallelism in a particular makefile
4054 with the @code{.NOTPARALLEL} pseudo-target (@pxref{Special
4055 Targets,Special Built-in Target Names}).@refill
4056
4057 On MS-DOS, the @samp{-j} option has no effect, since that system doesn't
4058 support multi-processing.
4059
4060 If the @samp{-j} option is followed by an integer, this is the number of
4061 recipes to execute at once; this is called the number of @dfn{job slots}.
4062 If there is nothing looking like an integer after the @samp{-j} option,
4063 there is no limit on the number of job slots.  The default number of job
4064 slots is one, which means serial execution (one thing at a time).
4065
4066 Handling recursive @code{make} invocations raises issues for parallel
4067 execution.  For more information on this, see @ref{Options/Recursion,
4068 ,Communicating Options to a Sub-@code{make}}.
4069
4070 If a recipe fails (is killed by a signal or exits with a nonzero
4071 status), and errors are not ignored for that recipe (@pxref{Errors,
4072 ,Errors in Recipes}), the remaining recipe lines to remake the same
4073 target will not be run.  If a recipe fails and the @samp{-k} or
4074 @samp{--keep-going} option was not given (@pxref{Options Summary,
4075 ,Summary of Options}), @code{make} aborts execution.  If make
4076 terminates for any reason (including a signal) with child processes
4077 running, it waits for them to finish before actually exiting.@refill
4078
4079 @cindex load average
4080 @cindex limiting jobs based on load
4081 @cindex jobs, limiting based on load
4082 @cindex @code{-l} (load average)
4083 @cindex @code{--max-load}
4084 @cindex @code{--load-average}
4085 When the system is heavily loaded, you will probably want to run fewer jobs
4086 than when it is lightly loaded.  You can use the @samp{-l} option to tell
4087 @code{make} to limit the number of jobs to run at once, based on the load
4088 average.  The @samp{-l} or @samp{--max-load}
4089 option is followed by a floating-point number.  For
4090 example,
4091
4092 @example
4093 -l 2.5
4094 @end example
4095
4096 @noindent
4097 will not let @code{make} start more than one job if the load average is
4098 above 2.5.  The @samp{-l} option with no following number removes the
4099 load limit, if one was given with a previous @samp{-l} option.@refill
4100
4101 More precisely, when @code{make} goes to start up a job, and it already has
4102 at least one job running, it checks the current load average; if it is not
4103 lower than the limit given with @samp{-l}, @code{make} waits until the load
4104 average goes below that limit, or until all the other jobs finish.
4105
4106 By default, there is no load limit.
4107
4108 @menu
4109 * Parallel Output::             Handling output during parallel execution
4110 * Parallel Input::              Handling input during parallel execution
4111 @end menu
4112
4113 @node Parallel Output, Parallel Input, Parallel, Parallel
4114 @subsection Output During Parallel Execution
4115 @cindex output during parallel execution
4116 @cindex parallel execution, output during
4117
4118 When running several recipes in parallel the output from each
4119 recipe appears as soon as it is generated, with the result that
4120 messages from different recipes may be interspersed, sometimes even
4121 appearing on the same line.  This can make reading the output very
4122 difficult.
4123
4124 @cindex @code{--output-sync}
4125 @cindex @code{-O}
4126 To avoid this you can use the @samp{--output-sync} (@samp{-O}) option.
4127 This option instructs @code{make} to save the output from the commands
4128 it invokes and print it all once the commands are completed.
4129 Additionally, if there are multiple recursive @code{make} invocations
4130 running in parallel, they will communicate so that only one of them is
4131 generating output at a time.
4132
4133 If working directory printing is enabled (@pxref{-w Option, ,The
4134 @samp{--print-directory} Option}), the enter/leave messages are
4135 printed around each output grouping.  If you prefer not to see these
4136 messages add the @samp{--no-print-directory} option to @code{MAKEFLAGS}.
4137
4138 There are four levels of granularity when synchronizing output,
4139 specified by giving an argument to the option (e.g.,  @samp{-Oline} or
4140 @samp{--output-sync=recurse}).
4141
4142 @table @code
4143 @item none
4144 This is the default: all output is sent directly as it is generated and
4145 no synchronization is performed.
4146
4147 @item line
4148 Output from each individual line of the recipe is grouped and printed
4149 as soon as that line is complete.  If a recipe consists of multiple
4150 lines, they may be interspersed with lines from other recipes.
4151
4152 @item target
4153 Output from the entire recipe for each target is grouped and printed
4154 once the target is complete.  This is the default if the
4155 @code{--output-sync} or @code{-O} option is given with no argument.
4156
4157 @item recurse
4158 Output from each recursive invocation of @code{make} is grouped and
4159 printed once the recursive invocation is complete.
4160
4161 @end table
4162
4163 Regardless of the mode chosen, the total build time will be the same.
4164 The only difference is in how the output appears.
4165
4166 The @samp{target} and @samp{recurse} modes both collect the output of
4167 the entire recipe of a target and display it uninterrupted when the
4168 recipe completes.  The difference between them is in how recipes that
4169 contain recursive invocations of @code{make} are treated
4170 (@pxref{Recursion, ,Recursive Use of @code{make}}).  For all recipes
4171 which have no recursive lines, the @samp{target} and @samp{recurse}
4172 modes behave identically.
4173
4174 If the @samp{recurse} mode is chosen, recipes that contain recursive
4175 @code{make} invocations are treated the same as other targets: the
4176 output from the recipe, including the output from the recursive
4177 @code{make}, is saved and printed after the entire recipe is complete.
4178 This ensures output from all the targets built by a given recursive
4179 @code{make} instance are grouped together, which may make the output
4180 easier to understand.  However it also leads to long periods of time
4181 during the build where no output is seen, followed by large bursts of
4182 output.  If you are not watching the build as it proceeds, but instead
4183 viewing a log of the build after the fact, this may be the best option
4184 for you.
4185
4186 If you are watching the output, the long gaps of quiet during the
4187 build can be frustrating.  The @samp{target} output synchronization
4188 mode detects when @code{make} is going to be invoked recursively,
4189 using the standard methods, and it will not synchronize the output of
4190 those lines.  The recursive @code{make} will perform the
4191 synchronization for its targets and the output from each will be
4192 displayed immediately when it completes.  Be aware that output from
4193 recursive lines of the recipe are not synchronized (for example if
4194 the recursive line prints a message before running @code{make}, that
4195 message will not be synchronized).
4196
4197 The @samp{line} mode can be useful for front-ends that are watching
4198 the output of @code{make} to track when recipes are started and
4199 completed.
4200
4201 Some programs invoked by @code{make} may behave differently if they
4202 determine they're writing output to a terminal versus a file (often
4203 described as ``interactive'' vs. ``non-interactive'' modes).  For
4204 example, many programs that can display colorized output will not do
4205 so if they determine they are not writing to a terminal.  If your
4206 makefile invokes a program like this then using the output
4207 synchronization options will cause the program to believe it's running
4208 in ``non-interactive'' mode even though the output will ultimately go
4209 to the terminal.
4210
4211 @node Parallel Input,  , Parallel Output, Parallel
4212 @subsection Input During Parallel Execution
4213 @cindex input during parallel execution
4214 @cindex parallel execution, input during
4215 @cindex standard input
4216
4217 Two processes cannot both take input from the same device at the same
4218 time.  To make sure that only one recipe tries to take input from the
4219 terminal at once, @code{make} will invalidate the standard input
4220 streams of all but one running recipe.  If another recipe attempts to
4221 read from standard input it will usually incur a fatal error (a
4222 @samp{Broken pipe} signal).
4223 @cindex broken pipe
4224
4225 It is unpredictable which recipe will have a valid standard input stream
4226 (which will come from the terminal, or wherever you redirect the standard
4227 input of @code{make}).  The first recipe run will always get it first, and
4228 the first recipe started after that one finishes will get it next, and so
4229 on.
4230
4231 We will change how this aspect of @code{make} works if we find a better
4232 alternative.  In the mean time, you should not rely on any recipe using
4233 standard input at all if you are using the parallel execution feature; but
4234 if you are not using this feature, then standard input works normally in
4235 all recipes.
4236
4237 @node Errors, Interrupts, Parallel, Recipes
4238 @section Errors in Recipes
4239 @cindex errors (in recipes)
4240 @cindex recipes, errors in
4241 @cindex exit status (errors)
4242
4243 After each shell invocation returns, @code{make} looks at its exit
4244 status.  If the shell completed successfully (the exit status is
4245 zero), the next line in the recipe is executed in a new shell; after
4246 the last line is finished, the rule is finished.
4247
4248 If there is an error (the exit status is nonzero), @code{make} gives up on
4249 the current rule, and perhaps on all rules.
4250
4251 Sometimes the failure of a certain recipe line does not indicate a problem.
4252 For example, you may use the @code{mkdir} command to ensure that a
4253 directory exists.  If the directory already exists, @code{mkdir} will
4254 report an error, but you probably want @code{make} to continue regardless.
4255
4256 @cindex @code{-} (in recipes)
4257 To ignore errors in a recipe line, write a @samp{-} at the beginning
4258 of the line's text (after the initial tab).  The @samp{-} is discarded
4259 before the line is passed to the shell for execution.
4260
4261 For example,
4262
4263 @example
4264 @group
4265 clean:
4266         -rm -f *.o
4267 @end group
4268 @end example
4269 @cindex @code{rm} (shell command)
4270
4271 @noindent
4272 This causes @code{make} to continue even if @code{rm} is unable to
4273 remove a file.
4274
4275 @cindex @code{-i}
4276 @cindex @code{--ignore-errors}
4277 @findex .IGNORE
4278 When you run @code{make} with the @samp{-i} or @samp{--ignore-errors}
4279 flag, errors are ignored in all recipes of all rules.  A rule in the
4280 makefile for the special target @code{.IGNORE} has the same effect, if
4281 there are no prerequisites.  These ways of ignoring errors are obsolete
4282 because @samp{-} is more flexible.
4283
4284 When errors are to be ignored, because of either a @samp{-} or the
4285 @samp{-i} flag, @code{make} treats an error return just like success,
4286 except that it prints out a message that tells you the status code
4287 the shell exited with, and says that the error has been ignored.
4288
4289 When an error happens that @code{make} has not been told to ignore,
4290 it implies that the current target cannot be correctly remade, and neither
4291 can any other that depends on it either directly or indirectly.  No further
4292 recipes will be executed for these targets, since their preconditions
4293 have not been achieved.
4294
4295
4296 @cindex @code{-k}
4297 @cindex @code{--keep-going}
4298 Normally @code{make} gives up immediately in this circumstance, returning a
4299 nonzero status.  However, if the @samp{-k} or @samp{--keep-going}
4300 flag is specified, @code{make}
4301 continues to consider the other prerequisites of the pending targets,
4302 remaking them if necessary, before it gives up and returns nonzero status.
4303 For example, after an error in compiling one object file, @samp{make -k}
4304 will continue compiling other object files even though it already knows
4305 that linking them will be impossible.  @xref{Options Summary, ,Summary of Options}.
4306
4307 The usual behavior assumes that your purpose is to get the specified
4308 targets up to date; once @code{make} learns that this is impossible, it
4309 might as well report the failure immediately.  The @samp{-k} option says
4310 that the real purpose is to test as many of the changes made in the
4311 program as possible, perhaps to find several independent problems so
4312 that you can correct them all before the next attempt to compile.  This
4313 is why Emacs' @code{compile} command passes the @samp{-k} flag by
4314 default.
4315 @cindex Emacs (@code{M-x compile})
4316
4317 @findex .DELETE_ON_ERROR
4318 @cindex deletion of target files
4319 @cindex removal of target files
4320 @cindex target, deleting on error
4321 Usually when a recipe line fails, if it has changed the target file at all,
4322 the file is corrupted and cannot be used---or at least it is not
4323 completely updated.  Yet the file's time stamp says that it is now up to
4324 date, so the next time @code{make} runs, it will not try to update that
4325 file.  The situation is just the same as when the shell is killed by a
4326 signal; @pxref{Interrupts}.  So generally the right thing to do is to
4327 delete the target file if the recipe fails after beginning to change
4328 the file.  @code{make} will do this if @code{.DELETE_ON_ERROR} appears
4329 as a target.  This is almost always what you want @code{make} to do, but
4330 it is not historical practice; so for compatibility, you must explicitly
4331 request it.
4332
4333 @node Interrupts, Recursion, Errors, Recipes
4334 @section Interrupting or Killing @code{make}
4335 @cindex interrupt
4336 @cindex signal
4337 @cindex deletion of target files
4338 @cindex removal of target files
4339 @cindex target, deleting on interrupt
4340 @cindex killing (interruption)
4341
4342 If @code{make} gets a fatal signal while a shell is executing, it may
4343 delete the target file that the recipe was supposed to update.  This is
4344 done if the target file's last-modification time has changed since
4345 @code{make} first checked it.
4346
4347 The purpose of deleting the target is to make sure that it is remade from
4348 scratch when @code{make} is next run.  Why is this?  Suppose you type
4349 @kbd{Ctrl-c} while a compiler is running, and it has begun to write an
4350 object file @file{foo.o}.  The @kbd{Ctrl-c} kills the compiler, resulting
4351 in an incomplete file whose last-modification time is newer than the source
4352 file @file{foo.c}.  But @code{make} also receives the @kbd{Ctrl-c} signal
4353 and deletes this incomplete file.  If @code{make} did not do this, the next
4354 invocation of @code{make} would think that @file{foo.o} did not require
4355 updating---resulting in a strange error message from the linker when it
4356 tries to link an object file half of which is missing.
4357
4358 @findex .PRECIOUS
4359 You can prevent the deletion of a target file in this way by making the
4360 special target @code{.PRECIOUS} depend on it.  Before remaking a target,
4361 @code{make} checks to see whether it appears on the prerequisites of
4362 @code{.PRECIOUS}, and thereby decides whether the target should be deleted
4363 if a signal happens.  Some reasons why you might do this are that the
4364 target is updated in some atomic fashion, or exists only to record a
4365 modification-time (its contents do not matter), or must exist at all
4366 times to prevent other sorts of trouble.
4367
4368 @node Recursion, Canned Recipes, Interrupts, Recipes
4369 @section Recursive Use of @code{make}
4370 @cindex recursion
4371 @cindex subdirectories, recursion for
4372
4373 Recursive use of @code{make} means using @code{make} as a command in a
4374 makefile.  This technique is useful when you want separate makefiles for
4375 various subsystems that compose a larger system.  For example, suppose you
4376 have a sub-directory @file{subdir} which has its own makefile, and you would
4377 like the containing directory's makefile to run @code{make} on the
4378 sub-directory.  You can do it by writing this:
4379
4380 @example
4381 subsystem:
4382         cd subdir && $(MAKE)
4383 @end example
4384
4385 @noindent
4386 or, equivalently, this (@pxref{Options Summary, ,Summary of Options}):
4387
4388 @example
4389 subsystem:
4390         $(MAKE) -C subdir
4391 @end example
4392 @cindex @code{-C}
4393 @cindex @code{--directory}
4394
4395 You can write recursive @code{make} commands just by copying this example,
4396 but there are many things to know about how they work and why, and about
4397 how the sub-@code{make} relates to the top-level @code{make}.  You may
4398 also find it useful to declare targets that invoke recursive
4399 @code{make} commands as @samp{.PHONY} (for more discussion on when
4400 this is useful, see @ref{Phony Targets}).
4401
4402 @vindex @code{CURDIR}
4403 For your convenience, when GNU @code{make} starts (after it has
4404 processed any @code{-C} options) it sets the variable @code{CURDIR} to
4405 the pathname of the current working directory.  This value is never
4406 touched by @code{make} again: in particular note that if you include
4407 files from other directories the value of @code{CURDIR} does not
4408 change.  The value has the same precedence it would have if it were
4409 set in the makefile (by default, an environment variable @code{CURDIR}
4410 will not override this value).  Note that setting this variable has no
4411 impact on the operation of @code{make} (it does not cause @code{make}
4412 to change its working directory, for example).
4413
4414 @menu
4415 * MAKE Variable::               The special effects of using @samp{$(MAKE)}.
4416 * Variables/Recursion::         How to communicate variables to a sub-@code{make}.
4417 * Options/Recursion::           How to communicate options to a sub-@code{make}.
4418 * -w Option::                   How the @samp{-w} or @samp{--print-directory} option
4419                                   helps debug use of recursive @code{make} commands.
4420 @end menu
4421
4422 @node MAKE Variable, Variables/Recursion, Recursion, Recursion
4423 @subsection How the @code{MAKE} Variable Works
4424 @vindex MAKE
4425 @cindex recursion, and @code{MAKE} variable
4426
4427 Recursive @code{make} commands should always use the variable @code{MAKE},
4428 not the explicit command name @samp{make}, as shown here:
4429
4430 @example
4431 @group
4432 subsystem:
4433         cd subdir && $(MAKE)
4434 @end group
4435 @end example
4436
4437 The value of this variable is the file name with which @code{make} was
4438 invoked.  If this file name was @file{/bin/make}, then the recipe executed
4439 is @samp{cd subdir && /bin/make}.  If you use a special version of
4440 @code{make} to run the top-level makefile, the same special version will be
4441 executed for recursive invocations.
4442 @cindex @code{cd} (shell command)
4443
4444 @cindex +, and recipes
4445 As a special feature, using the variable @code{MAKE} in the recipe of
4446 a rule alters the effects of the @samp{-t} (@samp{--touch}), @samp{-n}
4447 (@samp{--just-print}), or @samp{-q} (@w{@samp{--question}}) option.
4448 Using the @code{MAKE} variable has the same effect as using a @samp{+}
4449 character at the beginning of the recipe line.  @xref{Instead of
4450 Execution, ,Instead of Executing the Recipes}.  This special feature
4451 is only enabled if the @code{MAKE} variable appears directly in the
4452 recipe: it does not apply if the @code{MAKE} variable is referenced
4453 through expansion of another variable.  In the latter case you must
4454 use the @samp{+} token to get these special effects.@refill
4455
4456 Consider the command @samp{make -t} in the above example.  (The
4457 @samp{-t} option marks targets as up to date without actually running
4458 any recipes; see @ref{Instead of Execution}.)  Following the usual
4459 definition of @samp{-t}, a @samp{make -t} command in the example would
4460 create a file named @file{subsystem} and do nothing else.  What you
4461 really want it to do is run @samp{@w{cd subdir &&} @w{make -t}}; but
4462 that would require executing the recipe, and @samp{-t} says not to
4463 execute recipes.@refill
4464 @cindex @code{-t}, and recursion
4465 @cindex recursion, and @code{-t}
4466 @cindex @code{--touch}, and recursion
4467
4468 The special feature makes this do what you want: whenever a recipe
4469 line of a rule contains the variable @code{MAKE}, the flags @samp{-t},
4470 @samp{-n} and @samp{-q} do not apply to that line.  Recipe lines
4471 containing @code{MAKE} are executed normally despite the presence of a
4472 flag that causes most recipes not to be run.  The usual
4473 @code{MAKEFLAGS} mechanism passes the flags to the sub-@code{make}
4474 (@pxref{Options/Recursion, ,Communicating Options to a
4475 Sub-@code{make}}), so your request to touch the files, or print the
4476 recipes, is propagated to the subsystem.@refill
4477
4478 @node Variables/Recursion, Options/Recursion, MAKE Variable, Recursion
4479 @subsection Communicating Variables to a Sub-@code{make}
4480 @cindex sub-@code{make}
4481 @cindex environment, and recursion
4482 @cindex exporting variables
4483 @cindex variables, environment
4484 @cindex variables, exporting
4485 @cindex recursion, and environment
4486 @cindex recursion, and variables
4487
4488 Variable values of the top-level @code{make} can be passed to the
4489 sub-@code{make} through the environment by explicit request.  These
4490 variables are defined in the sub-@code{make} as defaults, but they do
4491 not override variables defined in the makefile used by
4492 the sub-@code{make} unless you use the @samp{-e} switch (@pxref{Options
4493 Summary, ,Summary of Options}).@refill
4494
4495 To pass down, or @dfn{export}, a variable, @code{make} adds the
4496 variable and its value to the environment for running each line of the
4497 recipe.  The sub-@code{make}, in turn, uses the environment to
4498 initialize its table of variable values.  @xref{Environment,
4499 ,Variables from the Environment}.
4500
4501 Except by explicit request, @code{make} exports a variable only if it
4502 is either defined in the environment initially or set on the command
4503 line, and if its name consists only of letters, numbers, and underscores.
4504 Some shells cannot cope with environment variable names consisting of
4505 characters other than letters, numbers, and underscores.
4506
4507 @cindex SHELL, exported value
4508 The value of the @code{make} variable @code{SHELL} is not exported.
4509 Instead, the value of the @code{SHELL} variable from the invoking
4510 environment is passed to the sub-@code{make}.  You can force
4511 @code{make} to export its value for @code{SHELL} by using the
4512 @code{export} directive, described below.  @xref{Choosing the Shell}.
4513
4514 The special variable @code{MAKEFLAGS} is always exported (unless you
4515 unexport it).  @code{MAKEFILES} is exported if you set it to anything.
4516
4517 @code{make} automatically passes down variable values that were defined
4518 on the command line, by putting them in the @code{MAKEFLAGS} variable.
4519 @iftex
4520 See the next section.
4521 @end iftex
4522 @ifnottex
4523 @xref{Options/Recursion}.
4524 @end ifnottex
4525
4526 Variables are @emph{not} normally passed down if they were created by
4527 default by @code{make} (@pxref{Implicit Variables, ,Variables Used by
4528 Implicit Rules}).  The sub-@code{make} will define these for
4529 itself.@refill
4530
4531 @findex export
4532 If you want to export specific variables to a sub-@code{make}, use the
4533 @code{export} directive, like this:
4534
4535 @example
4536 export @var{variable} @dots{}
4537 @end example
4538
4539 @noindent
4540 @findex unexport
4541 If you want to @emph{prevent} a variable from being exported, use the
4542 @code{unexport} directive, like this:
4543
4544 @example
4545 unexport @var{variable} @dots{}
4546 @end example
4547
4548 @noindent
4549 In both of these forms, the arguments to @code{export} and
4550 @code{unexport} are expanded, and so could be variables or functions
4551 which expand to a (list of) variable names to be (un)exported.
4552
4553 As a convenience, you can define a variable and export it at the same
4554 time by doing:
4555
4556 @example
4557 export @var{variable} = value
4558 @end example
4559
4560 @noindent
4561 has the same result as:
4562
4563 @example
4564 @var{variable} = value
4565 export @var{variable}
4566 @end example
4567
4568 @noindent
4569 and
4570
4571 @example
4572 export @var{variable} := value
4573 @end example
4574
4575 @noindent
4576 has the same result as:
4577
4578 @example
4579 @var{variable} := value
4580 export @var{variable}
4581 @end example
4582
4583 Likewise,
4584
4585 @example
4586 export @var{variable} += value
4587 @end example
4588
4589 @noindent
4590 is just like:
4591
4592 @example
4593 @var{variable} += value
4594 export @var{variable}
4595 @end example
4596
4597 @noindent
4598 @xref{Appending, ,Appending More Text to Variables}.
4599
4600 You may notice that the @code{export} and @code{unexport} directives
4601 work in @code{make} in the same way they work in the shell, @code{sh}.
4602
4603 If you want all variables to be exported by default, you can use
4604 @code{export} by itself:
4605
4606 @example
4607 export
4608 @end example
4609
4610 @noindent
4611 This tells @code{make} that variables which are not explicitly mentioned
4612 in an @code{export} or @code{unexport} directive should be exported.
4613 Any variable given in an @code{unexport} directive will still @emph{not}
4614 be exported.  If you use @code{export} by itself to export variables by
4615 default, variables whose names contain characters other than
4616 alphanumerics and underscores will not be exported unless specifically
4617 mentioned in an @code{export} directive.@refill
4618
4619 @findex .EXPORT_ALL_VARIABLES
4620 The behavior elicited by an @code{export} directive by itself was the
4621 default in older versions of GNU @code{make}.  If your makefiles depend
4622 on this behavior and you want to be compatible with old versions of
4623 @code{make}, you can write a rule for the special target
4624 @code{.EXPORT_ALL_VARIABLES} instead of using the @code{export} directive.
4625 This will be ignored by old @code{make}s, while the @code{export}
4626 directive will cause a syntax error.@refill
4627 @cindex compatibility in exporting
4628
4629 Likewise, you can use @code{unexport} by itself to tell @code{make}
4630 @emph{not} to export variables by default.  Since this is the default
4631 behavior, you would only need to do this if @code{export} had been used
4632 by itself earlier (in an included makefile, perhaps).  You
4633 @strong{cannot} use @code{export} and @code{unexport} by themselves to
4634 have variables exported for some recipes and not for others.  The last
4635 @code{export} or @code{unexport} directive that appears by itself
4636 determines the behavior for the entire run of @code{make}.@refill
4637
4638 @vindex MAKELEVEL
4639 @cindex recursion, level of
4640 As a special feature, the variable @code{MAKELEVEL} is changed when it
4641 is passed down from level to level.  This variable's value is a string
4642 which is the depth of the level as a decimal number.  The value is
4643 @samp{0} for the top-level @code{make}; @samp{1} for a sub-@code{make},
4644 @samp{2} for a sub-sub-@code{make}, and so on.  The incrementation
4645 happens when @code{make} sets up the environment for a recipe.@refill
4646
4647 The main use of @code{MAKELEVEL} is to test it in a conditional
4648 directive (@pxref{Conditionals, ,Conditional Parts of Makefiles}); this
4649 way you can write a makefile that behaves one way if run recursively and
4650 another way if run directly by you.@refill
4651
4652 @vindex MAKEFILES
4653 You can use the variable @code{MAKEFILES} to cause all sub-@code{make}
4654 commands to use additional makefiles.  The value of @code{MAKEFILES} is
4655 a whitespace-separated list of file names.  This variable, if defined in
4656 the outer-level makefile, is passed down through the environment; then
4657 it serves as a list of extra makefiles for the sub-@code{make} to read
4658 before the usual or specified ones.  @xref{MAKEFILES Variable, ,The
4659 Variable @code{MAKEFILES}}.@refill
4660
4661 @node Options/Recursion, -w Option, Variables/Recursion, Recursion
4662 @subsection Communicating Options to a Sub-@code{make}
4663 @cindex options, and recursion
4664 @cindex recursion, and options
4665
4666 @vindex MAKEFLAGS
4667 Flags such as @samp{-s} and @samp{-k} are passed automatically to the
4668 sub-@code{make} through the variable @code{MAKEFLAGS}.  This variable is
4669 set up automatically by @code{make} to contain the flag letters that
4670 @code{make} received.  Thus, if you do @w{@samp{make -ks}} then
4671 @code{MAKEFLAGS} gets the value @samp{ks}.@refill
4672
4673 As a consequence, every sub-@code{make} gets a value for @code{MAKEFLAGS}
4674 in its environment.  In response, it takes the flags from that value and
4675 processes them as if they had been given as arguments.
4676 @xref{Options Summary, ,Summary of Options}.
4677
4678 @cindex command line variable definitions, and recursion
4679 @cindex variables, command line, and recursion
4680 @cindex recursion, and command line variable definitions
4681 Likewise variables defined on the command line are passed to the
4682 sub-@code{make} through @code{MAKEFLAGS}.  Words in the value of
4683 @code{MAKEFLAGS} that contain @samp{=}, @code{make} treats as variable
4684 definitions just as if they appeared on the command line.
4685 @xref{Overriding, ,Overriding Variables}.
4686
4687 @cindex @code{-C}, and recursion
4688 @cindex @code{-f}, and recursion
4689 @cindex @code{-o}, and recursion
4690 @cindex @code{-W}, and recursion
4691 @cindex @code{--directory}, and recursion
4692 @cindex @code{--file}, and recursion
4693 @cindex @code{--old-file}, and recursion
4694 @cindex @code{--assume-old}, and recursion
4695 @cindex @code{--assume-new}, and recursion
4696 @cindex @code{--new-file}, and recursion
4697 @cindex recursion, and @code{-C}
4698 @cindex recursion, and @code{-f}
4699 @cindex recursion, and @code{-o}
4700 @cindex recursion, and @code{-W}
4701 The options @samp{-C}, @samp{-f}, @samp{-o}, and @samp{-W} are not put
4702 into @code{MAKEFLAGS}; these options are not passed down.@refill
4703
4704 @cindex @code{-j}, and recursion
4705 @cindex @code{--jobs}, and recursion
4706 @cindex recursion, and @code{-j}
4707 @cindex job slots, and recursion
4708 The @samp{-j} option is a special case (@pxref{Parallel, ,Parallel Execution}).
4709 If you set it to some numeric value @samp{N} and your operating system
4710 supports it (most any UNIX system will; others typically won't), the
4711 parent @code{make} and all the sub-@code{make}s will communicate to
4712 ensure that there are only @samp{N} jobs running at the same time
4713 between them all.  Note that any job that is marked recursive
4714 (@pxref{Instead of Execution, ,Instead of Executing Recipes})
4715 doesn't count against the total jobs (otherwise we could get @samp{N}
4716 sub-@code{make}s running and have no slots left over for any real work!)
4717
4718 If your operating system doesn't support the above communication, then
4719 @samp{-j 1} is always put into @code{MAKEFLAGS} instead of the value you
4720 specified.  This is because if the @w{@samp{-j}} option were passed down
4721 to sub-@code{make}s, you would get many more jobs running in parallel
4722 than you asked for.  If you give @samp{-j} with no numeric argument,
4723 meaning to run as many jobs as possible in parallel, this is passed
4724 down, since multiple infinities are no more than one.@refill
4725
4726 If you do not want to pass the other flags down, you must change the
4727 value of @code{MAKEFLAGS}, like this:
4728
4729 @example
4730 subsystem:
4731         cd subdir && $(MAKE) MAKEFLAGS=
4732 @end example
4733
4734 @vindex MAKEOVERRIDES
4735 The command line variable definitions really appear in the variable
4736 @code{MAKEOVERRIDES}, and @code{MAKEFLAGS} contains a reference to this
4737 variable.  If you do want to pass flags down normally, but don't want to
4738 pass down the command line variable definitions, you can reset
4739 @code{MAKEOVERRIDES} to empty, like this:
4740
4741 @example
4742 MAKEOVERRIDES =
4743 @end example
4744
4745 @noindent
4746 @cindex Arg list too long
4747 @cindex E2BIG
4748 This is not usually useful to do.  However, some systems have a small
4749 fixed limit on the size of the environment, and putting so much
4750 information into the value of @code{MAKEFLAGS} can exceed it.  If you
4751 see the error message @samp{Arg list too long}, this may be the problem.
4752 @findex .POSIX
4753 @cindex POSIX
4754 (For strict compliance with POSIX.2, changing @code{MAKEOVERRIDES} does
4755 not affect @code{MAKEFLAGS} if the special target @samp{.POSIX} appears
4756 in the makefile.  You probably do not care about this.)
4757
4758 @vindex MFLAGS
4759 A similar variable @code{MFLAGS} exists also, for historical
4760 compatibility.  It has the same value as @code{MAKEFLAGS} except that it
4761 does not contain the command line variable definitions, and it always
4762 begins with a hyphen unless it is empty (@code{MAKEFLAGS} begins with a
4763 hyphen only when it begins with an option that has no single-letter
4764 version, such as @samp{--warn-undefined-variables}).  @code{MFLAGS} was
4765 traditionally used explicitly in the recursive @code{make} command, like
4766 this:
4767
4768 @example
4769 subsystem:
4770         cd subdir && $(MAKE) $(MFLAGS)
4771 @end example
4772
4773 @noindent
4774 but now @code{MAKEFLAGS} makes this usage redundant.  If you want your
4775 makefiles to be compatible with old @code{make} programs, use this
4776 technique; it will work fine with more modern @code{make} versions too.
4777
4778 @cindex setting options from environment
4779 @cindex options, setting from environment
4780 @cindex setting options in makefiles
4781 @cindex options, setting in makefiles
4782 The @code{MAKEFLAGS} variable can also be useful if you want to have
4783 certain options, such as @samp{-k} (@pxref{Options Summary, ,Summary of
4784 Options}), set each time you run @code{make}.  You simply put a value for
4785 @code{MAKEFLAGS} in your environment.  You can also set @code{MAKEFLAGS} in
4786 a makefile, to specify additional flags that should also be in effect for
4787 that makefile.  (Note that you cannot use @code{MFLAGS} this way.  That
4788 variable is set only for compatibility; @code{make} does not interpret a
4789 value you set for it in any way.)
4790
4791 When @code{make} interprets the value of @code{MAKEFLAGS} (either from the
4792 environment or from a makefile), it first prepends a hyphen if the value
4793 does not already begin with one.  Then it chops the value into words
4794 separated by blanks, and parses these words as if they were options given
4795 on the command line (except that @samp{-C}, @samp{-f}, @samp{-h},
4796 @samp{-o}, @samp{-W}, and their long-named versions are ignored; and there
4797 is no error for an invalid option).
4798
4799 If you do put @code{MAKEFLAGS} in your environment, you should be sure not
4800 to include any options that will drastically affect the actions of
4801 @code{make} and undermine the purpose of makefiles and of @code{make}
4802 itself.  For instance, the @samp{-t}, @samp{-n}, and @samp{-q} options, if
4803 put in one of these variables, could have disastrous consequences and would
4804 certainly have at least surprising and probably annoying effects.@refill
4805
4806 If you'd like to run other implementations of @code{make} in addition
4807 to GNU @code{make}, and hence do not want to add GNU
4808 @code{make}-specific flags to the @code{MAKEFLAGS} variable, you can
4809 add them to the @code{GNUMAKEFLAGS} variable instead.  This variable
4810 is parsed just before @code{MAKEFLAGS}, in the same way as
4811 @code{MAKEFLAGS}.  When @code{make} constructs @code{MAKEFLAGS} to
4812 pass to a recursive @code{make} it will include all flags, even those
4813 taken from @code{GNUMAKEFLAGS}.  As a result, after parsing
4814 @code{GNUMAKEFLAGS} GNU @code{make} sets this variable to the empty
4815 string to avoid duplicating flags during recursion.
4816
4817 It's best to use @code{GNUMAKEFLAGS} only with flags which won't
4818 materially change the behavior of your makefiles.  If your makefiles
4819 require GNU make anyway then simply use @code{MAKEFLAGS}.  Flags such
4820 as @samp{--no-print-directory} or @samp{--output-sync} may be
4821 appropriate for @code{GNUMAKEFLAGS}.
4822
4823 @node -w Option,  , Options/Recursion, Recursion
4824 @subsection The @samp{--print-directory} Option
4825 @cindex directories, printing them
4826 @cindex printing directories
4827 @cindex recursion, and printing directories
4828
4829 If you use several levels of recursive @code{make} invocations, the
4830 @samp{-w} or @w{@samp{--print-directory}} option can make the output a
4831 lot easier to understand by showing each directory as @code{make}
4832 starts processing it and as @code{make} finishes processing it.  For
4833 example, if @samp{make -w} is run in the directory @file{/u/gnu/make},
4834 @code{make} will print a line of the form:@refill
4835
4836 @example
4837 make: Entering directory `/u/gnu/make'.
4838 @end example
4839
4840 @noindent
4841 before doing anything else, and a line of the form:
4842
4843 @example
4844 make: Leaving directory `/u/gnu/make'.
4845 @end example
4846
4847 @noindent
4848 when processing is completed.
4849
4850 @cindex @code{-C}, and @code{-w}
4851 @cindex @code{--directory}, and @code{--print-directory}
4852 @cindex recursion, and @code{-w}
4853 @cindex @code{-w}, and @code{-C}
4854 @cindex @code{-w}, and recursion
4855 @cindex @code{--print-directory}, and @code{--directory}
4856 @cindex @code{--print-directory}, and recursion
4857 @cindex @code{--no-print-directory}
4858 @cindex @code{--print-directory}, disabling
4859 @cindex @code{-w}, disabling
4860 Normally, you do not need to specify this option because @samp{make}
4861 does it for you: @samp{-w} is turned on automatically when you use the
4862 @samp{-C} option, and in sub-@code{make}s.  @code{make} will not
4863 automatically turn on @samp{-w} if you also use @samp{-s}, which says to
4864 be silent, or if you use @samp{--no-print-directory} to explicitly
4865 disable it.
4866
4867 @node Canned Recipes, Empty Recipes, Recursion, Recipes
4868 @section Defining Canned Recipes
4869 @cindex canned recipes
4870 @cindex recipes, canned
4871 @cindex sequences of commands
4872 @cindex commands, sequences of
4873
4874 When the same sequence of commands is useful in making various
4875 targets, you can define it as a canned sequence with the @code{define}
4876 directive, and refer to the canned sequence from the recipes for those
4877 targets.  The canned sequence is actually a variable, so the name must
4878 not conflict with other variable names.
4879
4880 Here is an example of defining a canned recipe:
4881
4882 @example
4883 define run-yacc =
4884 yacc $(firstword $^)
4885 mv y.tab.c $@@
4886 endef
4887 @end example
4888 @cindex @code{yacc}
4889
4890 @noindent
4891 Here @code{run-yacc} is the name of the variable being defined;
4892 @code{endef} marks the end of the definition; the lines in between are the
4893 commands.  The @code{define} directive does not expand variable references
4894 and function calls in the canned sequence; the @samp{$} characters,
4895 parentheses, variable names, and so on, all become part of the value of the
4896 variable you are defining.
4897 @xref{Multi-Line, ,Defining Multi-Line Variables},
4898 for a complete explanation of @code{define}.
4899
4900 The first command in this example runs Yacc on the first prerequisite of
4901 whichever rule uses the canned sequence.  The output file from Yacc is
4902 always named @file{y.tab.c}.  The second command moves the output to the
4903 rule's target file name.
4904
4905 To use the canned sequence, substitute the variable into the recipe of a
4906 rule.  You can substitute it like any other variable
4907 (@pxref{Reference, ,Basics of Variable References}).
4908 Because variables defined by @code{define} are recursively expanded
4909 variables, all the variable references you wrote inside the @code{define}
4910 are expanded now.  For example:
4911
4912 @example
4913 foo.c : foo.y
4914         $(run-yacc)
4915 @end example
4916
4917 @noindent
4918 @samp{foo.y} will be substituted for the variable @samp{$^} when it occurs in
4919 @code{run-yacc}'s value, and @samp{foo.c} for @samp{$@@}.@refill
4920
4921 This is a realistic example, but this particular one is not needed in
4922 practice because @code{make} has an implicit rule to figure out these
4923 commands based on the file names involved
4924 (@pxref{Implicit Rules, ,Using Implicit Rules}).
4925
4926 @cindex @@, and @code{define}
4927 @cindex -, and @code{define}
4928 @cindex +, and @code{define}
4929 In recipe execution, each line of a canned sequence is treated just as
4930 if the line appeared on its own in the rule, preceded by a tab.  In
4931 particular, @code{make} invokes a separate sub-shell for each line.  You
4932 can use the special prefix characters that affect command lines
4933 (@samp{@@}, @samp{-}, and @samp{+}) on each line of a canned sequence.
4934 @xref{Recipes, ,Writing Recipes in Rules}.
4935 For example, using this canned sequence:
4936
4937 @example
4938 define frobnicate =
4939 @@echo "frobnicating target $@@"
4940 frob-step-1 $< -o $@@-step-1
4941 frob-step-2 $@@-step-1 -o $@@
4942 endef
4943 @end example
4944
4945 @noindent
4946 @code{make} will not echo the first line, the @code{echo} command.
4947 But it @emph{will} echo the following two recipe lines.
4948
4949 On the other hand, prefix characters on the recipe line that refers to
4950 a canned sequence apply to every line in the sequence.  So the rule:
4951
4952 @example
4953 frob.out: frob.in
4954         @@$(frobnicate)
4955 @end example
4956
4957 @noindent
4958 does not echo @emph{any} recipe lines.
4959 (@xref{Echoing, ,Recipe Echoing}, for a full explanation of @samp{@@}.)
4960
4961 @node Empty Recipes,  , Canned Recipes, Recipes
4962 @section Using Empty Recipes
4963 @cindex empty recipes
4964 @cindex recipes, empty
4965
4966 It is sometimes useful to define recipes which do nothing.  This is done
4967 simply by giving a recipe that consists of nothing but whitespace.  For
4968 example:
4969
4970 @example
4971 target: ;
4972 @end example
4973
4974 @noindent
4975 defines an empty recipe for @file{target}.  You could also use a line
4976 beginning with a recipe prefix character to define an empty recipe,
4977 but this would be confusing because such a line looks empty.
4978
4979 @findex .DEFAULT@r{, and empty recipes}
4980 You may be wondering why you would want to define a recipe that
4981 does nothing.  The only reason this is useful is to prevent a target
4982 from getting implicit recipes (from implicit rules or the
4983 @code{.DEFAULT} special target; @pxref{Implicit Rules} and
4984 @pxref{Last Resort, ,Defining Last-Resort Default Rules}).@refill
4985
4986 @c !!! another reason is for canonical stamp files:
4987 @ignore
4988 @example
4989 foo: stamp-foo ;
4990 stamp-foo: foo.in
4991         create foo frm foo.in
4992         touch $@
4993 @end example
4994 @end ignore
4995
4996 You may be inclined to define empty recipes for targets that are
4997 not actual files, but only exist so that their prerequisites can be
4998 remade.  However, this is not the best way to do that, because the
4999 prerequisites may not be remade properly if the target file actually does exist.
5000 @xref{Phony Targets, ,Phony Targets}, for a better way to do this.
5001
5002 @node Using Variables, Conditionals, Recipes, Top
5003 @chapter How to Use Variables
5004 @cindex variable
5005 @cindex value
5006 @cindex recursive variable expansion
5007 @cindex simple variable expansion
5008
5009 A @dfn{variable} is a name defined in a makefile to represent a string
5010 of text, called the variable's @dfn{value}.  These values are
5011 substituted by explicit request into targets, prerequisites, recipes,
5012 and other parts of the makefile.  (In some other versions of @code{make},
5013 variables are called @dfn{macros}.)
5014 @cindex macro
5015
5016 Variables and functions in all parts of a makefile are expanded when
5017 read, except for in recipes, the right-hand sides of variable
5018 definitions using @samp{=}, and the bodies of variable definitions
5019 using the @code{define} directive.@refill
5020
5021 Variables can represent lists of file names, options to pass to compilers,
5022 programs to run, directories to look in for source files, directories to
5023 write output in, or anything else you can imagine.
5024
5025 A variable name may be any sequence of characters not containing
5026 @samp{:}, @samp{#}, @samp{=}, or whitespace.  However, variable names
5027 containing characters other than letters, numbers, and underscores
5028 should be considered carefully, as in some shells they cannot be
5029 passed through the environment to a sub-@code{make}
5030 (@pxref{Variables/Recursion, ,Communicating Variables to a
5031 Sub-@code{make}}).  Variable names beginning with @samp{.} and an
5032 uppercase letter may be given special meaning in future versions of
5033 @code{make}.
5034
5035 Variable names are case-sensitive.  The names @samp{foo}, @samp{FOO},
5036 and @samp{Foo} all refer to different variables.
5037
5038 It is traditional to use upper case letters in variable names, but we
5039 recommend using lower case letters for variable names that serve internal
5040 purposes in the makefile, and reserving upper case for parameters that
5041 control implicit rules or for parameters that the user should override with
5042 command options (@pxref{Overriding, ,Overriding Variables}).
5043
5044 A few variables have names that are a single punctuation character or
5045 just a few characters.  These are the @dfn{automatic variables}, and
5046 they have particular specialized uses.  @xref{Automatic Variables}.
5047
5048 @menu
5049 * Reference::                   How to use the value of a variable.
5050 * Flavors::                     Variables come in two flavors.
5051 * Advanced::                    Advanced features for referencing a variable.
5052 * Values::                      All the ways variables get their values.
5053 * Setting::                     How to set a variable in the makefile.
5054 * Appending::                   How to append more text to the old value
5055                                   of a variable.
5056 * Override Directive::          How to set a variable in the makefile even if
5057                                   the user has set it with a command argument.
5058 * Multi-Line::                  An alternate way to set a variable
5059                                   to a multi-line string.
5060 * Undefine Directive::          How to undefine a variable so that it appears
5061                                   as if it was never set.
5062 * Environment::                 Variable values can come from the environment.
5063 * Target-specific::             Variable values can be defined on a per-target
5064                                   basis.
5065 * Pattern-specific::            Target-specific variable values can be applied
5066                                   to a group of targets that match a pattern.
5067 * Suppressing Inheritance::     Suppress inheritance of variables.
5068 * Special Variables::           Variables with special meaning or behavior.
5069 @end menu
5070
5071 @node Reference, Flavors, Using Variables, Using Variables
5072 @section Basics of Variable References
5073 @cindex variables, how to reference
5074 @cindex reference to variables
5075 @cindex @code{$}, in variable reference
5076 @cindex dollar sign (@code{$}), in variable reference
5077
5078 To substitute a variable's value, write a dollar sign followed by the name
5079 of the variable in parentheses or braces: either @samp{$(foo)} or
5080 @samp{$@{foo@}} is a valid reference to the variable @code{foo}.  This
5081 special significance of @samp{$} is why you must write @samp{$$} to have
5082 the effect of a single dollar sign in a file name or recipe.
5083
5084 Variable references can be used in any context: targets, prerequisites,
5085 recipes, most directives, and new variable values.  Here is an
5086 example of a common case, where a variable holds the names of all the
5087 object files in a program:
5088
5089 @example
5090 @group
5091 objects = program.o foo.o utils.o
5092 program : $(objects)
5093         cc -o program $(objects)
5094
5095 $(objects) : defs.h
5096 @end group
5097 @end example
5098
5099 Variable references work by strict textual substitution.  Thus, the rule
5100
5101 @example
5102 @group
5103 foo = c
5104 prog.o : prog.$(foo)
5105         $(foo)$(foo) -$(foo) prog.$(foo)
5106 @end group
5107 @end example
5108
5109 @noindent
5110 could be used to compile a C program @file{prog.c}.  Since spaces before
5111 the variable value are ignored in variable assignments, the value of
5112 @code{foo} is precisely @samp{c}.  (Don't actually write your makefiles
5113 this way!)
5114
5115 A dollar sign followed by a character other than a dollar sign,
5116 open-parenthesis or open-brace treats that single character as the
5117 variable name.  Thus, you could reference the variable @code{x} with
5118 @samp{$x}.  However, this practice is strongly discouraged, except in
5119 the case of the automatic variables (@pxref{Automatic Variables}).
5120
5121 @node Flavors, Advanced, Reference, Using Variables
5122 @section The Two Flavors of Variables
5123 @cindex flavors of variables
5124 @cindex recursive variable expansion
5125 @cindex variables, flavors
5126 @cindex recursively expanded variables
5127 @cindex variables, recursively expanded
5128
5129 There are two ways that a variable in GNU @code{make} can have a value;
5130 we call them the two @dfn{flavors} of variables.  The two flavors are
5131 distinguished in how they are defined and in what they do when expanded.
5132
5133 @cindex =
5134 The first flavor of variable is a @dfn{recursively expanded} variable.
5135 Variables of this sort are defined by lines using @samp{=}
5136 (@pxref{Setting, ,Setting Variables}) or by the @code{define} directive
5137 (@pxref{Multi-Line, ,Defining Multi-Line Variables}).  The value you specify
5138 is installed verbatim; if it contains references to other variables,
5139 these references are expanded whenever this variable is substituted (in
5140 the course of expanding some other string).  When this happens, it is
5141 called @dfn{recursive expansion}.@refill
5142
5143 For example,
5144
5145 @example
5146 foo = $(bar)
5147 bar = $(ugh)
5148 ugh = Huh?
5149
5150 all:;echo $(foo)
5151 @end example
5152
5153 @noindent
5154 will echo @samp{Huh?}: @samp{$(foo)} expands to @samp{$(bar)} which
5155 expands to @samp{$(ugh)} which finally expands to @samp{Huh?}.@refill
5156
5157 This flavor of variable is the only sort supported by most other
5158 versions of @code{make}.  It has its advantages and its disadvantages.
5159 An advantage (most would say) is that:
5160
5161 @example
5162 CFLAGS = $(include_dirs) -O
5163 include_dirs = -Ifoo -Ibar
5164 @end example
5165
5166 @noindent
5167 will do what was intended: when @samp{CFLAGS} is expanded in a recipe,
5168 it will expand to @samp{-Ifoo -Ibar -O}.  A major disadvantage is that you
5169 cannot append something on the end of a variable, as in
5170
5171 @example
5172 CFLAGS = $(CFLAGS) -O
5173 @end example
5174
5175 @noindent
5176 because it will cause an infinite loop in the variable expansion.
5177 (Actually @code{make} detects the infinite loop and reports an error.)
5178 @cindex loops in variable expansion
5179 @cindex variables, loops in expansion
5180
5181 Another disadvantage is that any functions
5182 (@pxref{Functions, ,Functions for Transforming Text})
5183 referenced in the definition will be executed every time the variable is
5184 expanded.  This makes @code{make} run slower; worse, it causes the
5185 @code{wildcard} and @code{shell} functions to give unpredictable results
5186 because you cannot easily control when they are called, or even how many
5187 times.
5188
5189 To avoid all the problems and inconveniences of recursively expanded
5190 variables, there is another flavor: simply expanded variables.
5191
5192 @cindex simply expanded variables
5193 @cindex variables, simply expanded
5194 @cindex :=
5195 @cindex ::=
5196 @dfn{Simply expanded variables} are defined by lines using @samp{:=}
5197 or @samp{::=} (@pxref{Setting, ,Setting Variables}).  Both forms are
5198 equivalent in GNU @code{make}; however only the @samp{::=} form is
5199 described by the POSIX standard (support for @samp{::=} was added to
5200 the POSIX standard in 2012, so older versions of @code{make} won't
5201 accept this form either).
5202
5203 The value of a simply expanded variable is scanned
5204 once and for all, expanding any references to other variables and
5205 functions, when the variable is defined.  The actual value of the simply
5206 expanded variable is the result of expanding the text that you write.
5207 It does not contain any references to other variables; it contains their
5208 values @emph{as of the time this variable was defined}.  Therefore,
5209
5210 @example
5211 x := foo
5212 y := $(x) bar
5213 x := later
5214 @end example
5215
5216 @noindent
5217 is equivalent to
5218
5219 @example
5220 y := foo bar
5221 x := later
5222 @end example
5223
5224 When a simply expanded variable is referenced, its value is substituted
5225 verbatim.
5226
5227 Here is a somewhat more complicated example, illustrating the use of
5228 @samp{:=} in conjunction with the @code{shell} function.
5229 (@xref{Shell Function, , The @code{shell} Function}.)  This example
5230 also shows use of the variable @code{MAKELEVEL}, which is changed
5231 when it is passed down from level to level.
5232 (@xref{Variables/Recursion, , Communicating Variables to a
5233 Sub-@code{make}}, for information about @code{MAKELEVEL}.)
5234
5235 @vindex MAKELEVEL
5236 @vindex MAKE
5237 @example
5238 @group
5239 ifeq (0,$@{MAKELEVEL@})
5240 whoami    := $(shell whoami)
5241 host-type := $(shell arch)
5242 MAKE := $@{MAKE@} host-type=$@{host-type@} whoami=$@{whoami@}
5243 endif
5244 @end group
5245 @end example
5246
5247 @noindent
5248 An advantage of this use of @samp{:=} is that a typical
5249 `descend into a directory' recipe then looks like this:
5250
5251 @example
5252 @group
5253 $@{subdirs@}:
5254         $@{MAKE@} -C $@@ all
5255 @end group
5256 @end example
5257
5258 Simply expanded variables generally make complicated makefile programming
5259 more predictable because they work like variables in most programming
5260 languages.  They allow you to redefine a variable using its own value (or
5261 its value processed in some way by one of the expansion functions) and to
5262 use the expansion functions much more efficiently
5263 (@pxref{Functions, ,Functions for Transforming Text}).
5264
5265 @cindex spaces, in variable values
5266 @cindex whitespace, in variable values
5267 @cindex variables, spaces in values
5268 You can also use them to introduce controlled leading whitespace into
5269 variable values.  Leading whitespace characters are discarded from your
5270 input before substitution of variable references and function calls;
5271 this means you can include leading spaces in a variable value by
5272 protecting them with variable references, like this:
5273
5274 @example
5275 nullstring :=
5276 space := $(nullstring) # end of the line
5277 @end example
5278
5279 @noindent
5280 Here the value of the variable @code{space} is precisely one space.  The
5281 comment @w{@samp{# end of the line}} is included here just for clarity.
5282 Since trailing space characters are @emph{not} stripped from variable
5283 values, just a space at the end of the line would have the same effect
5284 (but be rather hard to read).  If you put whitespace at the end of a
5285 variable value, it is a good idea to put a comment like that at the end
5286 of the line to make your intent clear.  Conversely, if you do @emph{not}
5287 want any whitespace characters at the end of your variable value, you
5288 must remember not to put a random comment on the end of the line after
5289 some whitespace, such as this:
5290
5291 @example
5292 dir := /foo/bar    # directory to put the frobs in
5293 @end example
5294
5295 @noindent
5296 Here the value of the variable @code{dir} is @w{@samp{/foo/bar    }}
5297 (with four trailing spaces), which was probably not the intention.
5298 (Imagine something like @w{@samp{$(dir)/file}} with this definition!)
5299
5300 @cindex conditional variable assignment
5301 @cindex variables, conditional assignment
5302 @cindex ?=
5303 There is another assignment operator for variables, @samp{?=}.  This
5304 is called a conditional variable assignment operator, because it only
5305 has an effect if the variable is not yet defined.  This statement:
5306
5307 @example
5308 FOO ?= bar
5309 @end example
5310
5311 @noindent
5312 is exactly equivalent to this
5313 (@pxref{Origin Function, ,The @code{origin} Function}):
5314
5315 @example
5316 ifeq ($(origin FOO), undefined)
5317   FOO = bar
5318 endif
5319 @end example
5320
5321 Note that a variable set to an empty value is still defined, so
5322 @samp{?=} will not set that variable.
5323
5324 @node Advanced, Values, Flavors, Using Variables
5325 @section Advanced Features for Reference to Variables
5326 @cindex reference to variables
5327
5328 This section describes some advanced features you can use to reference
5329 variables in more flexible ways.
5330
5331 @menu
5332 * Substitution Refs::           Referencing a variable with
5333                                   substitutions on the value.
5334 * Computed Names::              Computing the name of the variable to refer to.
5335 @end menu
5336
5337 @node Substitution Refs, Computed Names, Advanced, Advanced
5338 @subsection Substitution References
5339 @cindex modified variable reference
5340 @cindex substitution variable reference
5341 @cindex variables, modified reference
5342 @cindex variables, substitution reference
5343
5344 @cindex variables, substituting suffix in
5345 @cindex suffix, substituting in variables
5346 A @dfn{substitution reference} substitutes the value of a variable with
5347 alterations that you specify.  It has the form
5348 @samp{$(@var{var}:@var{a}=@var{b})} (or
5349 @samp{$@{@var{var}:@var{a}=@var{b}@}}) and its meaning is to take the value
5350 of the variable @var{var}, replace every @var{a} at the end of a word with
5351 @var{b} in that value, and substitute the resulting string.
5352
5353 When we say ``at the end of a word'', we mean that @var{a} must appear
5354 either followed by whitespace or at the end of the value in order to be
5355 replaced; other occurrences of @var{a} in the value are unaltered.  For
5356 example:@refill
5357
5358 @example
5359 foo := a.o b.o c.o
5360 bar := $(foo:.o=.c)
5361 @end example
5362
5363 @noindent
5364 sets @samp{bar} to @samp{a.c b.c c.c}.  @xref{Setting, ,Setting Variables}.
5365
5366 A substitution reference is actually an abbreviation for use of the
5367 @code{patsubst} expansion function (@pxref{Text Functions, ,Functions for String Substitution and Analysis}).  We provide
5368 substitution references as well as @code{patsubst} for compatibility with
5369 other implementations of @code{make}.
5370
5371 @findex patsubst
5372 Another type of substitution reference lets you use the full power of
5373 the @code{patsubst} function.  It has the same form
5374 @samp{$(@var{var}:@var{a}=@var{b})} described above, except that now
5375 @var{a} must contain a single @samp{%} character.  This case is
5376 equivalent to @samp{$(patsubst @var{a},@var{b},$(@var{var}))}.
5377 @xref{Text Functions, ,Functions for String Substitution and Analysis},
5378 for a description of the @code{patsubst} function.@refill
5379
5380 @example
5381 @group
5382 @exdent For example:
5383
5384 foo := a.o b.o c.o
5385 bar := $(foo:%.o=%.c)
5386 @end group
5387 @end example
5388
5389 @noindent
5390 sets @samp{bar} to @samp{a.c b.c c.c}.
5391
5392 @node Computed Names,  , Substitution Refs, Advanced
5393 @subsection Computed Variable Names
5394 @cindex nested variable reference
5395 @cindex computed variable name
5396 @cindex variables, computed names
5397 @cindex variables, nested references
5398 @cindex variables, @samp{$} in name
5399 @cindex @code{$}, in variable name
5400 @cindex dollar sign (@code{$}), in variable name
5401
5402 Computed variable names are a complicated concept needed only for
5403 sophisticated makefile programming.  For most purposes you need not
5404 consider them, except to know that making a variable with a dollar sign
5405 in its name might have strange results.  However, if you are the type
5406 that wants to understand everything, or you are actually interested in
5407 what they do, read on.
5408
5409 Variables may be referenced inside the name of a variable.  This is
5410 called a @dfn{computed variable name} or a @dfn{nested variable
5411 reference}.  For example,
5412
5413 @example
5414 x = y
5415 y = z
5416 a := $($(x))
5417 @end example
5418
5419 @noindent
5420 defines @code{a} as @samp{z}: the @samp{$(x)} inside @samp{$($(x))} expands
5421 to @samp{y}, so @samp{$($(x))} expands to @samp{$(y)} which in turn expands
5422 to @samp{z}.  Here the name of the variable to reference is not stated
5423 explicitly; it is computed by expansion of @samp{$(x)}.  The reference
5424 @samp{$(x)} here is nested within the outer variable reference.
5425
5426 The previous example shows two levels of nesting, but any number of levels
5427 is possible.  For example, here are three levels:
5428
5429 @example
5430 x = y
5431 y = z
5432 z = u
5433 a := $($($(x)))
5434 @end example
5435
5436 @noindent
5437 Here the innermost @samp{$(x)} expands to @samp{y}, so @samp{$($(x))}
5438 expands to @samp{$(y)} which in turn expands to @samp{z}; now we have
5439 @samp{$(z)}, which becomes @samp{u}.
5440
5441 References to recursively-expanded variables within a variable name are
5442 re-expanded in the usual fashion.  For example:
5443
5444 @example
5445 x = $(y)
5446 y = z
5447 z = Hello
5448 a := $($(x))
5449 @end example
5450
5451 @noindent
5452 defines @code{a} as @samp{Hello}: @samp{$($(x))} becomes @samp{$($(y))}
5453 which becomes @samp{$(z)} which becomes @samp{Hello}.
5454
5455 Nested variable references can also contain modified references and
5456 function invocations (@pxref{Functions, ,Functions for Transforming Text}),
5457 just like any other reference.
5458 For example, using the @code{subst} function
5459 (@pxref{Text Functions, ,Functions for String Substitution and Analysis}):
5460
5461 @example
5462 @group
5463 x = variable1
5464 variable2 := Hello
5465 y = $(subst 1,2,$(x))
5466 z = y
5467 a := $($($(z)))
5468 @end group
5469 @end example
5470
5471 @noindent
5472 eventually defines @code{a} as @samp{Hello}.  It is doubtful that anyone
5473 would ever want to write a nested reference as convoluted as this one, but
5474 it works: @samp{$($($(z)))} expands to @samp{$($(y))} which becomes
5475 @samp{$($(subst 1,2,$(x)))}.  This gets the value @samp{variable1} from
5476 @code{x} and changes it by substitution to @samp{variable2}, so that the
5477 entire string becomes @samp{$(variable2)}, a simple variable reference
5478 whose value is @samp{Hello}.@refill
5479
5480 A computed variable name need not consist entirely of a single variable
5481 reference.  It can contain several variable references, as well as some
5482 invariant text.  For example,
5483
5484 @example
5485 @group
5486 a_dirs := dira dirb
5487 1_dirs := dir1 dir2
5488 @end group
5489
5490 @group
5491 a_files := filea fileb
5492 1_files := file1 file2
5493 @end group
5494
5495 @group
5496 ifeq "$(use_a)" "yes"
5497 a1 := a
5498 else
5499 a1 := 1
5500 endif
5501 @end group
5502
5503 @group
5504 ifeq "$(use_dirs)" "yes"
5505 df := dirs
5506 else
5507 df := files
5508 endif
5509
5510 dirs := $($(a1)_$(df))
5511 @end group
5512 @end example
5513
5514 @noindent
5515 will give @code{dirs} the same value as @code{a_dirs}, @code{1_dirs},
5516 @code{a_files} or @code{1_files} depending on the settings of @code{use_a}
5517 and @code{use_dirs}.@refill
5518
5519 Computed variable names can also be used in substitution references:
5520
5521 @example
5522 @group
5523 a_objects := a.o b.o c.o
5524 1_objects := 1.o 2.o 3.o
5525
5526 sources := $($(a1)_objects:.o=.c)
5527 @end group
5528 @end example
5529
5530 @noindent
5531 defines @code{sources} as either @samp{a.c b.c c.c} or @samp{1.c 2.c 3.c},
5532 depending on the value of @code{a1}.
5533
5534 The only restriction on this sort of use of nested variable references
5535 is that they cannot specify part of the name of a function to be called.
5536 This is because the test for a recognized function name is done before
5537 the expansion of nested references.  For example,
5538
5539 @example
5540 @group
5541 ifdef do_sort
5542 func := sort
5543 else
5544 func := strip
5545 endif
5546 @end group
5547
5548 @group
5549 bar := a d b g q c
5550 @end group
5551
5552 @group
5553 foo := $($(func) $(bar))
5554 @end group
5555 @end example
5556
5557 @noindent
5558 attempts to give @samp{foo} the value of the variable @samp{sort a d b g
5559 q c} or @samp{strip a d b g q c}, rather than giving @samp{a d b g q c}
5560 as the argument to either the @code{sort} or the @code{strip} function.
5561 This restriction could be removed in the future if that change is shown
5562 to be a good idea.
5563
5564 You can also use computed variable names in the left-hand side of a
5565 variable assignment, or in a @code{define} directive, as in:
5566
5567 @example
5568 dir = foo
5569 $(dir)_sources := $(wildcard $(dir)/*.c)
5570 define $(dir)_print =
5571 lpr $($(dir)_sources)
5572 endef
5573 @end example
5574
5575 @noindent
5576 This example defines the variables @samp{dir}, @samp{foo_sources}, and
5577 @samp{foo_print}.
5578
5579 Note that @dfn{nested variable references} are quite different from
5580 @dfn{recursively expanded variables}
5581 (@pxref{Flavors, ,The Two Flavors of Variables}), though both are
5582 used together in complex ways when doing makefile programming.@refill
5583
5584 @node Values, Setting, Advanced, Using Variables
5585 @section How Variables Get Their Values
5586 @cindex variables, how they get their values
5587 @cindex value, how a variable gets it
5588
5589 Variables can get values in several different ways:
5590
5591 @itemize @bullet
5592 @item
5593 You can specify an overriding value when you run @code{make}.
5594 @xref{Overriding, ,Overriding Variables}.
5595
5596 @item
5597 You can specify a value in the makefile, either
5598 with an assignment (@pxref{Setting, ,Setting Variables}) or with a
5599 verbatim definition (@pxref{Multi-Line, ,Defining Multi-Line Variables}).@refill
5600
5601 @item
5602 Variables in the environment become @code{make} variables.
5603 @xref{Environment, ,Variables from the Environment}.
5604
5605 @item
5606 Several @dfn{automatic} variables are given new values for each rule.
5607 Each of these has a single conventional use.
5608 @xref{Automatic Variables}.
5609
5610 @item
5611 Several variables have constant initial values.
5612 @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
5613 @end itemize
5614
5615 @node Setting, Appending, Values, Using Variables
5616 @section Setting Variables
5617 @cindex setting variables
5618 @cindex variables, setting
5619 @cindex =
5620 @cindex :=
5621 @cindex ::=
5622 @cindex ?=
5623 @cindex !=
5624
5625 To set a variable from the makefile, write a line starting with the
5626 variable name followed by @samp{=} @samp{:=}, or @samp{::=}.  Whatever
5627 follows the @samp{=}, @samp{:=}, or @samp{::=} on the line becomes the
5628 value.  For example,
5629
5630 @example
5631 objects = main.o foo.o bar.o utils.o
5632 @end example
5633
5634 @noindent
5635 defines a variable named @code{objects}.  Whitespace around the variable
5636 name and immediately after the @samp{=} is ignored.
5637
5638 Variables defined with @samp{=} are @dfn{recursively expanded}
5639 variables.  Variables defined with @samp{:=} or @samp{::=} are
5640 @dfn{simply expanded} variables; these definitions can contain
5641 variable references which will be expanded before the definition is
5642 made.  @xref{Flavors, ,The Two Flavors of Variables}.
5643
5644 The variable name may contain function and variable references, which
5645 are expanded when the line is read to find the actual variable name to use.
5646
5647 There is no limit on the length of the value of a variable except the
5648 amount of memory on the computer.  You can split the value of a
5649 variable into multiple physical lines for readability
5650 (@pxref{Splitting Lines, ,Splitting Long Lines}).
5651
5652 Most variable names are considered to have the empty string as a value if
5653 you have never set them.  Several variables have built-in initial values
5654 that are not empty, but you can set them in the usual ways
5655 (@pxref{Implicit Variables, ,Variables Used by Implicit Rules}).
5656 Several special variables are set
5657 automatically to a new value for each rule; these are called the
5658 @dfn{automatic} variables (@pxref{Automatic Variables}).
5659
5660 If you'd like a variable to be set to a value only if it's not already
5661 set, then you can use the shorthand operator @samp{?=} instead of
5662 @samp{=}.  These two settings of the variable @samp{FOO} are identical
5663 (@pxref{Origin Function, ,The @code{origin} Function}):
5664
5665 @example
5666 FOO ?= bar
5667 @end example
5668
5669 @noindent
5670 and
5671
5672 @example
5673 ifeq ($(origin FOO), undefined)
5674 FOO = bar
5675 endif
5676 @end example
5677
5678 The shell assignment operator @samp{!=} can be used to execute a
5679 program and set a variable to its output.  This operator first
5680 evaluates the right-hand side, then passes that result to the shell
5681 for execution.  If the result of the execution ends in a newline, that
5682 one newline is removed; all other newlines are replaced by spaces.
5683 The resulting string is then placed into the named
5684 recursively-expanded variable.  For example:
5685
5686 @example
5687 hash != printf '\043'
5688 file_list != find . -name '*.c'
5689 @end example
5690
5691 If the result of the execution could produce a @code{$}, and you don't
5692 intend what follows that to be interpreted as a make variable or
5693 function reference, then you must replace every @code{$} with
5694 @code{$$} as part of the execution.  Alternatively, you can set a
5695 simply expanded variable to the result of running a program using the
5696 @code{shell} function call.  @xref{Shell Function, , The @code{shell}
5697 Function}.  For example:
5698
5699 @example
5700 hash := $(shell printf '\043')
5701 var := $(shell find . -name "*.c")
5702 @end example
5703
5704
5705 @node Appending, Override Directive, Setting, Using Variables
5706 @section Appending More Text to Variables
5707 @cindex +=
5708 @cindex appending to variables
5709 @cindex variables, appending to
5710
5711 Often it is useful to add more text to the value of a variable already defined.
5712 You do this with a line containing @samp{+=}, like this:
5713
5714 @example
5715 objects += another.o
5716 @end example
5717
5718 @noindent
5719 This takes the value of the variable @code{objects}, and adds the text
5720 @samp{another.o} to it (preceded by a single space).  Thus:
5721
5722 @example
5723 objects = main.o foo.o bar.o utils.o
5724 objects += another.o
5725 @end example
5726
5727 @noindent
5728 sets @code{objects} to @samp{main.o foo.o bar.o utils.o another.o}.
5729
5730 Using @samp{+=} is similar to:
5731
5732 @example
5733 objects = main.o foo.o bar.o utils.o
5734 objects := $(objects) another.o
5735 @end example
5736
5737 @noindent
5738 but differs in ways that become important when you use more complex values.
5739
5740 When the variable in question has not been defined before, @samp{+=}
5741 acts just like normal @samp{=}: it defines a recursively-expanded
5742 variable.  However, when there @emph{is} a previous definition, exactly
5743 what @samp{+=} does depends on what flavor of variable you defined
5744 originally.  @xref{Flavors, ,The Two Flavors of Variables}, for an
5745 explanation of the two flavors of variables.
5746
5747 When you add to a variable's value with @samp{+=}, @code{make} acts
5748 essentially as if you had included the extra text in the initial
5749 definition of the variable.  If you defined it first with @samp{:=} or
5750 @samp{::=}, making it a simply-expanded variable, @samp{+=} adds to
5751 that simply-expanded definition, and expands the new text before
5752 appending it to the old value just as @samp{:=} does (see
5753 @ref{Setting, ,Setting Variables}, for a full explanation of
5754 @samp{:=} or @samp{::=}).  In fact,
5755
5756 @example
5757 variable := value
5758 variable += more
5759 @end example
5760
5761 @noindent
5762 is exactly equivalent to:
5763
5764 @noindent
5765 @example
5766 variable := value
5767 variable := $(variable) more
5768 @end example
5769
5770 On the other hand, when you use @samp{+=} with a variable that you defined
5771 first to be recursively-expanded using plain @samp{=}, @code{make} does
5772 something a bit different.  Recall that when you define a
5773 recursively-expanded variable, @code{make} does not expand the value you set
5774 for variable and function references immediately.  Instead it stores the text
5775 verbatim, and saves these variable and function references to be expanded
5776 later, when you refer to the new variable (@pxref{Flavors, ,The Two Flavors
5777 of Variables}).  When you use @samp{+=} on a recursively-expanded variable,
5778 it is this unexpanded text to which @code{make} appends the new text you
5779 specify.
5780
5781 @example
5782 @group
5783 variable = value
5784 variable += more
5785 @end group
5786 @end example
5787
5788 @noindent
5789 is roughly equivalent to:
5790
5791 @example
5792 @group
5793 temp = value
5794 variable = $(temp) more
5795 @end group
5796 @end example
5797
5798 @noindent
5799 except that of course it never defines a variable called @code{temp}.
5800 The importance of this comes when the variable's old value contains
5801 variable references.  Take this common example:
5802
5803 @example
5804 CFLAGS = $(includes) -O
5805 @dots{}
5806 CFLAGS += -pg # enable profiling
5807 @end example
5808
5809 @noindent
5810 The first line defines the @code{CFLAGS} variable with a reference to another
5811 variable, @code{includes}.  (@code{CFLAGS} is used by the rules for C
5812 compilation; @pxref{Catalogue of Rules, ,Catalogue of Implicit Rules}.)
5813 Using @samp{=} for the definition makes @code{CFLAGS} a recursively-expanded
5814 variable, meaning @w{@samp{$(includes) -O}} is @emph{not} expanded when
5815 @code{make} processes the definition of @code{CFLAGS}.  Thus, @code{includes}
5816 need not be defined yet for its value to take effect.  It only has to be
5817 defined before any reference to @code{CFLAGS}.  If we tried to append to the
5818 value of @code{CFLAGS} without using @samp{+=}, we might do it like this:
5819
5820 @example
5821 CFLAGS := $(CFLAGS) -pg # enable profiling
5822 @end example
5823
5824 @noindent
5825 This is pretty close, but not quite what we want.  Using @samp{:=}
5826 redefines @code{CFLAGS} as a simply-expanded variable; this means
5827 @code{make} expands the text @w{@samp{$(CFLAGS) -pg}} before setting the
5828 variable.  If @code{includes} is not yet defined, we get @w{@samp{ -O
5829 -pg}}, and a later definition of @code{includes} will have no effect.
5830 Conversely, by using @samp{+=} we set @code{CFLAGS} to the
5831 @emph{unexpanded} value @w{@samp{$(includes) -O -pg}}.  Thus we preserve
5832 the reference to @code{includes}, so if that variable gets defined at
5833 any later point, a reference like @samp{$(CFLAGS)} still uses its
5834 value.
5835
5836 @node Override Directive, Multi-Line, Appending, Using Variables
5837 @section The @code{override} Directive
5838 @findex override
5839 @cindex overriding with @code{override}
5840 @cindex variables, overriding
5841
5842 If a variable has been set with a command argument
5843 (@pxref{Overriding, ,Overriding Variables}),
5844 then ordinary assignments in the makefile are ignored.  If you want to set
5845 the variable in the makefile even though it was set with a command
5846 argument, you can use an @code{override} directive, which is a line that
5847 looks like this:@refill
5848
5849 @example
5850 override @var{variable} = @var{value}
5851 @end example
5852
5853 @noindent
5854 or
5855
5856 @example
5857 override @var{variable} := @var{value}
5858 @end example
5859
5860 To append more text to a variable defined on the command line, use:
5861
5862 @example
5863 override @var{variable} += @var{more text}
5864 @end example
5865
5866 @noindent
5867 @xref{Appending, ,Appending More Text to Variables}.
5868
5869 Variable assignments marked with the @code{override} flag have a
5870 higher priority than all other assignments, except another
5871 @code{override}.  Subsequent assignments or appends to this variable
5872 which are not marked @code{override} will be ignored.
5873
5874 The @code{override} directive was not invented for escalation in the war
5875 between makefiles and command arguments.  It was invented so you can alter
5876 and add to values that the user specifies with command arguments.
5877
5878 For example, suppose you always want the @samp{-g} switch when you run the
5879 C compiler, but you would like to allow the user to specify the other
5880 switches with a command argument just as usual.  You could use this
5881 @code{override} directive:
5882
5883 @example
5884 override CFLAGS += -g
5885 @end example
5886
5887 You can also use @code{override} directives with @code{define} directives.
5888 This is done as you might expect:
5889
5890 @example
5891 override define foo =
5892 bar
5893 endef
5894 @end example
5895
5896 @noindent
5897 @iftex
5898 See the next section for information about @code{define}.
5899 @end iftex
5900 @ifnottex
5901 @xref{Multi-Line, ,Defining Multi-Line Variables}.
5902 @end ifnottex
5903
5904 @node Multi-Line, Undefine Directive, Override Directive, Using Variables
5905 @section Defining Multi-Line Variables
5906 @findex define
5907 @findex endef
5908 @cindex multi-line variable definition
5909 @cindex variables, multi-line
5910 @cindex verbatim variable definition
5911 @cindex defining variables verbatim
5912 @cindex variables, defining verbatim
5913
5914 Another way to set the value of a variable is to use the @code{define}
5915 directive.  This directive has an unusual syntax which allows newline
5916 characters to be included in the value, which is convenient for
5917 defining both canned sequences of commands (@pxref{Canned Recipes,
5918 ,Defining Canned Recipes}), and also sections of makefile syntax to
5919 use with @code{eval} (@pxref{Eval Function}).@refill
5920
5921 The @code{define} directive is followed on the same line by the name
5922 of the variable being defined and an (optional) assignment operator,
5923 and nothing more.  The value to give the variable appears on the
5924 following lines.  The end of the value is marked by a line containing
5925 just the word @code{endef}.  Aside from this difference in syntax,
5926 @code{define} works just like any other variable definition.  The
5927 variable name may contain function and variable references, which are
5928 expanded when the directive is read to find the actual variable name
5929 to use.
5930
5931 You may omit the variable assignment operator if you prefer.  If
5932 omitted, @code{make} assumes it to be @samp{=} and creates a
5933 recursively-expanded variable (@pxref{Flavors, ,The Two Flavors of Variables}).
5934 When using a @samp{+=} operator, the value is appended to the previous
5935 value as with any other append operation: with a single space
5936 separating the old and new values.
5937
5938 You may nest @code{define} directives: @code{make} will keep track of
5939 nested directives and report an error if they are not all properly
5940 closed with @code{endef}.  Note that lines beginning with the recipe
5941 prefix character are considered part of a recipe, so any @code{define}
5942 or @code{endef} strings appearing on such a line will not be
5943 considered @code{make} directives.
5944
5945 @example
5946 define two-lines =
5947 echo foo
5948 echo $(bar)
5949 endef
5950 @end example
5951
5952 The value in an ordinary assignment cannot contain a newline; but the
5953 newlines that separate the lines of the value in a @code{define} become
5954 part of the variable's value (except for the final newline which precedes
5955 the @code{endef} and is not considered part of the value).@refill
5956
5957 @need 800
5958 When used in a recipe, the previous example is functionally equivalent
5959 to this:
5960
5961 @example
5962 two-lines = echo foo; echo $(bar)
5963 @end example
5964
5965 @noindent
5966 since two commands separated by semicolon behave much like two separate
5967 shell commands.  However, note that using two separate lines means
5968 @code{make} will invoke the shell twice, running an independent sub-shell
5969 for each line.  @xref{Execution, ,Recipe Execution}.
5970
5971 If you want variable definitions made with @code{define} to take
5972 precedence over command-line variable definitions, you can use the
5973 @code{override} directive together with @code{define}:
5974
5975 @example
5976 override define two-lines =
5977 foo
5978 $(bar)
5979 endef
5980 @end example
5981
5982 @noindent
5983 @xref{Override Directive, ,The @code{override} Directive}.
5984
5985 @node Undefine Directive, Environment, Multi-Line, Using Variables
5986 @section Undefining Variables
5987 @findex undefine
5988 @cindex undefining variable
5989
5990 If you want to clear a variable, setting its value to empty is usually
5991 sufficient. Expanding such a variable will yield the same result (empty
5992 string) regardless of whether it was set or not. However, if you are
5993 using the @code{flavor} (@pxref{Flavor Function}) and
5994 @code{origin} (@pxref{Origin Function}) functions, there is a difference
5995 between a variable that was never set and a variable with an empty value.
5996 In such situations you may want to use the @code{undefine} directive to
5997 make a variable appear as if it was never set. For example:
5998
5999 @example
6000 foo := foo
6001 bar = bar
6002
6003 undefine foo
6004 undefine bar
6005
6006 $(info $(origin foo))
6007 $(info $(flavor bar))
6008 @end example
6009
6010 This example will print ``undefined'' for both variables.
6011
6012 If you want to undefine a command-line variable definition, you can use
6013 the @code{override} directive together with @code{undefine}, similar to
6014 how this is done for variable definitions:
6015
6016 @example
6017 override undefine CFLAGS
6018 @end example
6019
6020 @node Environment, Target-specific, Undefine Directive, Using Variables
6021 @section Variables from the Environment
6022
6023 @cindex variables, environment
6024 @cindex environment
6025 Variables in @code{make} can come from the environment in which
6026 @code{make} is run.  Every environment variable that @code{make} sees
6027 when it starts up is transformed into a @code{make} variable with the
6028 same name and value.  However, an explicit assignment in the makefile,
6029 or with a command argument, overrides the environment.  (If the
6030 @samp{-e} flag is specified, then values from the environment override
6031 assignments in the makefile.  @xref{Options Summary, ,Summary of
6032 Options}.  But this is not recommended practice.)
6033
6034 Thus, by setting the variable @code{CFLAGS} in your environment, you can
6035 cause all C compilations in most makefiles to use the compiler switches you
6036 prefer.  This is safe for variables with standard or conventional meanings
6037 because you know that no makefile will use them for other things.  (Note
6038 this is not totally reliable; some makefiles set @code{CFLAGS} explicitly
6039 and therefore are not affected by the value in the environment.)
6040
6041 When @code{make} runs a recipe, variables defined in the
6042 makefile are placed into the environment of each shell.  This allows
6043 you to pass values to sub-@code{make} invocations (@pxref{Recursion,
6044 ,Recursive Use of @code{make}}).  By default, only variables that came
6045 from the environment or the command line are passed to recursive
6046 invocations.  You can use the @code{export} directive to pass other
6047 variables.  @xref{Variables/Recursion, , Communicating Variables to a
6048 Sub-@code{make}}, for full details.
6049
6050 Other use of variables from the environment is not recommended.  It is not
6051 wise for makefiles to depend for their functioning on environment variables
6052 set up outside their control, since this would cause different users to get
6053 different results from the same makefile.  This is against the whole
6054 purpose of most makefiles.
6055
6056 @cindex SHELL, import from environment
6057 Such problems would be especially likely with the variable
6058 @code{SHELL}, which is normally present in the environment to specify
6059 the user's choice of interactive shell.  It would be very undesirable
6060 for this choice to affect @code{make}; so, @code{make} handles the
6061 @code{SHELL} environment variable in a special way; see @ref{Choosing
6062 the Shell}.@refill
6063
6064 @node Target-specific, Pattern-specific, Environment, Using Variables
6065 @section Target-specific Variable Values
6066 @cindex target-specific variables
6067 @cindex variables, target-specific
6068
6069 Variable values in @code{make} are usually global; that is, they are the
6070 same regardless of where they are evaluated (unless they're reset, of
6071 course).  One exception to that is automatic variables
6072 (@pxref{Automatic Variables}).
6073
6074 The other exception is @dfn{target-specific variable values}.  This
6075 feature allows you to define different values for the same variable,
6076 based on the target that @code{make} is currently building.  As with
6077 automatic variables, these values are only available within the context
6078 of a target's recipe (and in other target-specific assignments).
6079
6080 Set a target-specific variable value like this:
6081
6082 @example
6083 @var{target} @dots{} : @var{variable-assignment}
6084 @end example
6085
6086 Target-specific variable assignments can be prefixed with any or all of the
6087 special keywords @code{export}, @code{override}, or @code{private};
6088 these apply their normal behavior to this instance of the variable only.
6089
6090 Multiple @var{target} values create a target-specific variable value for
6091 each member of the target list individually.
6092
6093 The @var{variable-assignment} can be any valid form of assignment;
6094 recursive (@samp{=}), simple (@samp{:=} or @samp{::=}), appending
6095 (@samp{+=}), or conditional (@samp{?=}).  All variables that appear
6096 within the @var{variable-assignment} are evaluated within the context
6097 of the target: thus, any previously-defined target-specific variable
6098 values will be in effect.  Note that this variable is actually
6099 distinct from any ``global'' value: the two variables do not have to
6100 have the same flavor (recursive vs.@: simple).
6101
6102 Target-specific variables have the same priority as any other makefile
6103 variable.  Variables provided on the command line (and in the
6104 environment if the @samp{-e} option is in force) will take precedence.
6105 Specifying the @code{override} directive will allow the target-specific
6106 variable value to be preferred.
6107
6108 There is one more special feature of target-specific variables: when
6109 you define a target-specific variable that variable value is also in
6110 effect for all prerequisites of this target, and all their
6111 prerequisites, etc.@: (unless those prerequisites override that variable
6112 with their own target-specific variable value).  So, for example, a
6113 statement like this:
6114
6115 @example
6116 prog : CFLAGS = -g
6117 prog : prog.o foo.o bar.o
6118 @end example
6119
6120 @noindent
6121 will set @code{CFLAGS} to @samp{-g} in the recipe for @file{prog}, but
6122 it will also set @code{CFLAGS} to @samp{-g} in the recipes that create
6123 @file{prog.o}, @file{foo.o}, and @file{bar.o}, and any recipes which
6124 create their prerequisites.
6125
6126 Be aware that a given prerequisite will only be built once per
6127 invocation of make, at most.  If the same file is a prerequisite of
6128 multiple targets, and each of those targets has a different value for
6129 the same target-specific variable, then the first target to be built
6130 will cause that prerequisite to be built and the prerequisite will
6131 inherit the target-specific value from the first target.  It will
6132 ignore the target-specific values from any other targets.
6133
6134 @node Pattern-specific, Suppressing Inheritance, Target-specific, Using Variables
6135 @section Pattern-specific Variable Values
6136 @cindex pattern-specific variables
6137 @cindex variables, pattern-specific
6138
6139 In addition to target-specific variable values
6140 (@pxref{Target-specific, ,Target-specific Variable Values}), GNU
6141 @code{make} supports pattern-specific variable values.  In this form,
6142 the variable is defined for any target that matches the pattern
6143 specified.
6144
6145 Set a pattern-specific variable value like this:
6146
6147 @example
6148 @var{pattern} @dots{} : @var{variable-assignment}
6149 @end example
6150 where @var{pattern} is a %-pattern.  As with target-specific variable
6151 values, multiple @var{pattern} values create a pattern-specific variable
6152 value for each pattern individually.  The @var{variable-assignment} can
6153 be any valid form of assignment.  Any command line variable setting will
6154 take precedence, unless @code{override} is specified.
6155
6156 For example:
6157
6158 @example
6159 %.o : CFLAGS = -O
6160 @end example
6161
6162 @noindent
6163 will assign @code{CFLAGS} the value of @samp{-O} for all targets
6164 matching the pattern @code{%.o}.
6165
6166 If a target matches more than one pattern, the matching pattern-specific
6167 variables with longer stems are interpreted first. This results in more
6168 specific variables taking precedence over the more generic ones, for
6169 example:
6170
6171 @example
6172 %.o: %.c
6173         $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@@
6174
6175 lib/%.o: CFLAGS := -fPIC -g
6176 %.o: CFLAGS := -g
6177
6178 all: foo.o lib/bar.o
6179 @end example
6180
6181 In this example the first definition of the @code{CFLAGS} variable
6182 will be used to update @file{lib/bar.o} even though the second one
6183 also applies to this target. Pattern-specific variables which result
6184 in the same stem length are considered in the order in which they
6185 were defined in the makefile.
6186
6187 Pattern-specific variables are searched after any target-specific
6188 variables defined explicitly for that target, and before target-specific
6189 variables defined for the parent target.
6190
6191 @node Suppressing Inheritance, Special Variables, Pattern-specific, Using Variables
6192 @section Suppressing Inheritance
6193 @findex private
6194 @cindex suppressing inheritance
6195 @cindex inheritance, suppressing
6196
6197 As described in previous sections, @code{make} variables are inherited
6198 by prerequisites.  This capability allows you to modify the behavior
6199 of a prerequisite based on which targets caused it to be rebuilt.  For
6200 example, you might set a target-specific variable on a @code{debug}
6201 target, then running @samp{make debug} will cause that variable to be
6202 inherited by all prerequisites of @code{debug}, while just running
6203 @samp{make all} (for example) would not have that assignment.
6204
6205 Sometimes, however, you may not want a variable to be inherited.  For
6206 these situations, @code{make} provides the @code{private} modifier.
6207 Although this modifier can be used with any variable assignment, it
6208 makes the most sense with target- and pattern-specific variables.  Any
6209 variable marked @code{private} will be visible to its local target but
6210 will not be inherited by prerequisites of that target.  A global
6211 variable marked @code{private} will be visible in the global scope but
6212 will not be inherited by any target, and hence will not be visible
6213 in any recipe.
6214
6215 As an example, consider this makefile:
6216 @example
6217 EXTRA_CFLAGS =
6218
6219 prog: private EXTRA_CFLAGS = -L/usr/local/lib
6220 prog: a.o b.o
6221 @end example
6222
6223 Due to the @code{private} modifier, @code{a.o} and @code{b.o} will not
6224 inherit the @code{EXTRA_CFLAGS} variable assignment from the
6225 @code{prog} target.
6226
6227 @node Special Variables,  , Suppressing Inheritance, Using Variables
6228 @comment  node-name,  next,  previous,  up
6229 @section Other Special Variables
6230 @cindex makefiles, and special variables
6231 @cindex special variables
6232
6233 GNU @code{make} supports some variables that have special properties.
6234
6235 @table @code
6236
6237 @vindex MAKEFILE_LIST @r{(list of parsed makefiles)}
6238 @cindex makefiles, and @code{MAKEFILE_LIST} variable
6239 @cindex including (@code{MAKEFILE_LIST} variable)
6240 @item MAKEFILE_LIST
6241 Contains the name of each makefile that is parsed by @code{make}, in
6242 the order in which it was parsed.  The name is appended just
6243 before @code{make} begins to parse the makefile.  Thus, if the first
6244 thing a makefile does is examine the last word in this variable, it
6245 will be the name of the current makefile.  Once the current makefile
6246 has used @code{include}, however, the last word will be the
6247 just-included makefile.
6248
6249 If a makefile named @code{Makefile} has this content:
6250
6251 @example
6252 @group
6253 name1 := $(lastword $(MAKEFILE_LIST))
6254
6255 include inc.mk
6256
6257 name2 := $(lastword $(MAKEFILE_LIST))
6258
6259 all:
6260         @@echo name1 = $(name1)
6261         @@echo name2 = $(name2)
6262 @end group
6263 @end example
6264
6265 @noindent
6266 then you would expect to see this output:
6267
6268 @example
6269 @group
6270 name1 = Makefile
6271 name2 = inc.mk
6272 @end group
6273 @end example
6274
6275 @vindex .DEFAULT_GOAL @r{(define default goal)}
6276 @item .DEFAULT_GOAL
6277 Sets the default goal to be used if no targets were specified on the
6278 command line (@pxref{Goals, , Arguments to Specify the Goals}).  The
6279 @code{.DEFAULT_GOAL} variable allows you to discover the current
6280 default goal, restart the default goal selection algorithm by clearing
6281 its value, or to explicitly set the default goal.  The following
6282 example illustrates these cases:
6283
6284 @example
6285 @group
6286 # Query the default goal.
6287 ifeq ($(.DEFAULT_GOAL),)
6288   $(warning no default goal is set)
6289 endif
6290
6291 .PHONY: foo
6292 foo: ; @@echo $@@
6293
6294 $(warning default goal is $(.DEFAULT_GOAL))
6295
6296 # Reset the default goal.
6297 .DEFAULT_GOAL :=
6298
6299 .PHONY: bar
6300 bar: ; @@echo $@@
6301
6302 $(warning default goal is $(.DEFAULT_GOAL))
6303
6304 # Set our own.
6305 .DEFAULT_GOAL := foo
6306 @end group
6307 @end example
6308
6309 This makefile prints:
6310
6311 @example
6312 @group
6313 no default goal is set
6314 default goal is foo
6315 default goal is bar
6316 foo
6317 @end group
6318 @end example
6319
6320 Note that assigning more than one target name to @code{.DEFAULT_GOAL} is
6321 invalid and will result in an error.
6322
6323 @vindex MAKE_RESTARTS @r{(number of times @code{make} has restarted)}
6324 @item MAKE_RESTARTS
6325 This variable is set only if this instance of @code{make} has
6326 restarted (@pxref{Remaking Makefiles, , How Makefiles Are Remade}): it
6327 will contain the number of times this instance has restarted.  Note
6328 this is not the same as recursion (counted by the @code{MAKELEVEL}
6329 variable).  You should not set, modify, or export this variable.
6330
6331 @vindex .RECIPEPREFIX @r{(change the recipe prefix character)}
6332 @item .RECIPEPREFIX
6333 The first character of the value of this variable is used as the
6334 character make assumes is introducing a recipe line.  If the variable
6335 is empty (as it is by default) that character is the standard tab
6336 character.  For example, this is a valid makefile:
6337
6338 @example
6339 @group
6340 .RECIPEPREFIX = >
6341 all:
6342 > @@echo Hello, world
6343 @end group
6344 @end example
6345
6346 The value of @code{.RECIPEPREFIX} can be changed multiple times; once set
6347 it stays in effect for all rules parsed until it is modified.
6348
6349 @vindex .VARIABLES @r{(list of variables)}
6350 @item .VARIABLES
6351 Expands to a list of the @emph{names} of all global variables defined
6352 so far.  This includes variables which have empty values, as well as
6353 built-in variables (@pxref{Implicit Variables, , Variables Used by
6354 Implicit Rules}), but does not include any variables which are only
6355 defined in a target-specific context.  Note that any value you assign
6356 to this variable will be ignored; it will always return its special
6357 value.
6358
6359 @c @vindex .TARGETS @r{(list of targets)}
6360 @c @item .TARGETS
6361 @c The second special variable is @code{.TARGETS}.  When expanded, the
6362 @c value consists of a list of all targets defined in all makefiles read
6363 @c up until that point.  Note it's not enough for a file to be simply
6364 @c mentioned in the makefile to be listed in this variable, even if it
6365 @c would match an implicit rule and become an ``implicit target''.  The
6366 @c file must appear as a target, on the left-hand side of a ``:'', to be
6367 @c considered a target for the purposes of this variable.
6368
6369 @vindex .FEATURES @r{(list of supported features)}
6370 @item .FEATURES
6371 Expands to a list of special features supported by this version of
6372 @code{make}.  Possible values include, but are not limited to:
6373
6374 @table @samp
6375
6376 @item archives
6377 Supports @code{ar} (archive) files using special file name syntax.
6378 @xref{Archives, ,Using @code{make} to Update Archive Files}.
6379
6380 @item check-symlink
6381 Supports the @code{-L} (@code{--check-symlink-times}) flag.
6382 @xref{Options Summary, ,Summary of Options}.
6383
6384 @item else-if
6385 Supports ``else if'' non-nested conditionals.  @xref{Conditional
6386 Syntax, ,Syntax of Conditionals}.
6387
6388 @item jobserver
6389 Supports ``job server'' enhanced parallel builds.  @xref{Parallel,
6390 ,Parallel Execution}.
6391
6392 @item oneshell
6393 Supports the @code{.ONESHELL} special target.  @xref{One Shell, ,Using
6394 One Shell}.
6395
6396 @item order-only
6397 Supports order-only prerequisites.  @xref{Prerequisite Types, ,Types
6398 of Prerequisites}.
6399
6400 @item second-expansion
6401 Supports secondary expansion of prerequisite lists.
6402
6403 @item shortest-stem
6404 Uses the ``shortest stem'' method of choosing which pattern, of
6405 multiple applicable options, will be used.  @xref{Pattern Match, ,How
6406 Patterns Match}.
6407
6408 @item target-specific
6409 Supports target-specific and pattern-specific variable assignments.
6410 @xref{Target-specific, ,Target-specific Variable Values}.
6411
6412 @item undefine
6413 Supports the @code{undefine} directive.  @xref{Undefine Directive}.
6414
6415 @item guile
6416 Has GNU Guile available as an embedded extension language.
6417 @xref{Guile Integration, ,GNU Guile Integration}.
6418
6419 @item load
6420 Supports dynamically loadable objects for creating custom extensions.
6421 @xref{Loading Objects, ,Loading Dynamic Objects}.
6422 @end table
6423
6424 @vindex .INCLUDE_DIRS @r{(list of include directories)}
6425 @item .INCLUDE_DIRS
6426 Expands to a list of directories that @code{make} searches for
6427 included makefiles (@pxref{Include, , Including Other Makefiles}).
6428
6429 @end table
6430
6431 @node Conditionals, Functions, Using Variables, Top
6432 @chapter Conditional Parts of Makefiles
6433
6434 @cindex conditionals
6435 A @dfn{conditional} directive causes part of a makefile to be obeyed
6436 or ignored depending on the values of variables.  Conditionals can
6437 compare the value of one variable to another, or the value of a
6438 variable to a constant string.  Conditionals control what @code{make}
6439 actually ``sees'' in the makefile, so they @emph{cannot} be used to
6440 control recipes at the time of execution.@refill
6441
6442 @menu
6443 * Conditional Example::         Example of a conditional
6444 * Conditional Syntax::          The syntax of conditionals.
6445 * Testing Flags::               Conditionals that test flags.
6446 @end menu
6447
6448 @node Conditional Example, Conditional Syntax, Conditionals, Conditionals
6449 @section Example of a Conditional
6450
6451 The following example of a conditional tells @code{make} to use one
6452 set of libraries if the @code{CC} variable is @samp{gcc}, and a
6453 different set of libraries otherwise.  It works by controlling which
6454 of two recipe lines will be used for the rule.  The result is that
6455 @samp{CC=gcc} as an argument to @code{make} changes not only which
6456 compiler is used but also which libraries are linked.
6457
6458 @example
6459 libs_for_gcc = -lgnu
6460 normal_libs =
6461
6462 foo: $(objects)
6463 ifeq ($(CC),gcc)
6464         $(CC) -o foo $(objects) $(libs_for_gcc)
6465 else
6466         $(CC) -o foo $(objects) $(normal_libs)
6467 endif
6468 @end example
6469
6470 This conditional uses three directives: one @code{ifeq}, one @code{else}
6471 and one @code{endif}.
6472
6473 The @code{ifeq} directive begins the conditional, and specifies the
6474 condition.  It contains two arguments, separated by a comma and surrounded
6475 by parentheses.  Variable substitution is performed on both arguments and
6476 then they are compared.  The lines of the makefile following the
6477 @code{ifeq} are obeyed if the two arguments match; otherwise they are
6478 ignored.
6479
6480 The @code{else} directive causes the following lines to be obeyed if the
6481 previous conditional failed.  In the example above, this means that the
6482 second alternative linking command is used whenever the first alternative
6483 is not used.  It is optional to have an @code{else} in a conditional.
6484
6485 The @code{endif} directive ends the conditional.  Every conditional must
6486 end with an @code{endif}.  Unconditional makefile text follows.
6487
6488 As this example illustrates, conditionals work at the textual level:
6489 the lines of the conditional are treated as part of the makefile, or
6490 ignored, according to the condition.  This is why the larger syntactic
6491 units of the makefile, such as rules, may cross the beginning or the
6492 end of the conditional.
6493
6494 When the variable @code{CC} has the value @samp{gcc}, the above example has
6495 this effect:
6496
6497 @example
6498 foo: $(objects)
6499         $(CC) -o foo $(objects) $(libs_for_gcc)
6500 @end example
6501
6502 @noindent
6503 When the variable @code{CC} has any other value, the effect is this:
6504
6505 @example
6506 foo: $(objects)
6507         $(CC) -o foo $(objects) $(normal_libs)
6508 @end example
6509
6510 Equivalent results can be obtained in another way by conditionalizing a
6511 variable assignment and then using the variable unconditionally:
6512
6513 @example
6514 libs_for_gcc = -lgnu
6515 normal_libs =
6516
6517 ifeq ($(CC),gcc)
6518   libs=$(libs_for_gcc)
6519 else
6520   libs=$(normal_libs)
6521 endif
6522
6523 foo: $(objects)
6524         $(CC) -o foo $(objects) $(libs)
6525 @end example
6526
6527 @node Conditional Syntax, Testing Flags, Conditional Example, Conditionals
6528 @section Syntax of Conditionals
6529 @findex ifdef
6530 @findex ifeq
6531 @findex ifndef
6532 @findex ifneq
6533 @findex else
6534 @findex endif
6535
6536 The syntax of a simple conditional with no @code{else} is as follows:
6537
6538 @example
6539 @var{conditional-directive}
6540 @var{text-if-true}
6541 endif
6542 @end example
6543
6544 @noindent
6545 The @var{text-if-true} may be any lines of text, to be considered as part
6546 of the makefile if the condition is true.  If the condition is false, no
6547 text is used instead.
6548
6549 The syntax of a complex conditional is as follows:
6550
6551 @example
6552 @var{conditional-directive}
6553 @var{text-if-true}
6554 else
6555 @var{text-if-false}
6556 endif
6557 @end example
6558
6559 or:
6560
6561 @example
6562 @var{conditional-directive-one}
6563 @var{text-if-one-is-true}
6564 else @var{conditional-directive-two}
6565 @var{text-if-two-is-true}
6566 else
6567 @var{text-if-one-and-two-are-false}
6568 endif
6569 @end example
6570
6571 @noindent
6572 There can be as many ``@code{else} @var{conditional-directive}''
6573 clauses as necessary.  Once a given condition is true,
6574 @var{text-if-true} is used and no other clause is used; if no
6575 condition is true then @var{text-if-false} is used.  The
6576 @var{text-if-true} and @var{text-if-false} can be any number of lines
6577 of text.
6578
6579 The syntax of the @var{conditional-directive} is the same whether the
6580 conditional is simple or complex; after an @code{else} or not.  There
6581 are four different directives that test different conditions.  Here is
6582 a table of them:
6583
6584 @table @code
6585 @item ifeq (@var{arg1}, @var{arg2})
6586 @itemx ifeq '@var{arg1}' '@var{arg2}'
6587 @itemx ifeq "@var{arg1}" "@var{arg2}"
6588 @itemx ifeq "@var{arg1}" '@var{arg2}'
6589 @itemx ifeq '@var{arg1}' "@var{arg2}"
6590 Expand all variable references in @var{arg1} and @var{arg2} and
6591 compare them.  If they are identical, the @var{text-if-true} is
6592 effective; otherwise, the @var{text-if-false}, if any, is effective.
6593
6594 Often you want to test if a variable has a non-empty value.  When the
6595 value results from complex expansions of variables and functions,
6596 expansions you would consider empty may actually contain whitespace
6597 characters and thus are not seen as empty.  However, you can use the
6598 @code{strip} function (@pxref{Text Functions}) to avoid interpreting
6599 whitespace as a non-empty value.  For example:
6600
6601 @example
6602 @group
6603 ifeq ($(strip $(foo)),)
6604 @var{text-if-empty}
6605 endif
6606 @end group
6607 @end example
6608
6609 @noindent
6610 will evaluate @var{text-if-empty} even if the expansion of
6611 @code{$(foo)} contains whitespace characters.
6612
6613 @item ifneq (@var{arg1}, @var{arg2})
6614 @itemx ifneq '@var{arg1}' '@var{arg2}'
6615 @itemx ifneq "@var{arg1}" "@var{arg2}"
6616 @itemx ifneq "@var{arg1}" '@var{arg2}'
6617 @itemx ifneq '@var{arg1}' "@var{arg2}"
6618 Expand all variable references in @var{arg1} and @var{arg2} and
6619 compare them.  If they are different, the @var{text-if-true} is
6620 effective; otherwise, the @var{text-if-false}, if any, is effective.
6621
6622 @item ifdef @var{variable-name}
6623 The @code{ifdef} form takes the @emph{name} of a variable as its
6624 argument, not a reference to a variable.  The value of that variable
6625 has a non-empty value, the @var{text-if-true} is effective; otherwise,
6626 the @var{text-if-false}, if any, is effective.  Variables that have
6627 never been defined have an empty value.  The text @var{variable-name}
6628 is expanded, so it could be a variable or function that expands
6629 to the name of a variable.  For example:
6630
6631 @example
6632 bar = true
6633 foo = bar
6634 ifdef $(foo)
6635 frobozz = yes
6636 endif
6637 @end example
6638
6639 The variable reference @code{$(foo)} is expanded, yielding @code{bar},
6640 which is considered to be the name of a variable.  The variable
6641 @code{bar} is not expanded, but its value is examined to determine if
6642 it is non-empty.
6643
6644 Note that @code{ifdef} only tests whether a variable has a value.  It
6645 does not expand the variable to see if that value is nonempty.
6646 Consequently, tests using @code{ifdef} return true for all definitions
6647 except those like @code{foo =}.  To test for an empty value, use
6648 @w{@code{ifeq ($(foo),)}}.  For example,
6649
6650 @example
6651 bar =
6652 foo = $(bar)
6653 ifdef foo
6654 frobozz = yes
6655 else
6656 frobozz = no
6657 endif
6658 @end example
6659
6660 @noindent
6661 sets @samp{frobozz} to @samp{yes}, while:
6662
6663 @example
6664 foo =
6665 ifdef foo
6666 frobozz = yes
6667 else
6668 frobozz = no
6669 endif
6670 @end example
6671
6672 @noindent
6673 sets @samp{frobozz} to @samp{no}.
6674
6675 @item ifndef @var{variable-name}
6676 If the variable @var{variable-name} has an empty value, the
6677 @var{text-if-true} is effective; otherwise, the @var{text-if-false},
6678 if any, is effective.  The rules for expansion and testing of
6679 @var{variable-name} are identical to the @code{ifdef} directive.
6680 @end table
6681
6682 Extra spaces are allowed and ignored at the beginning of the
6683 conditional directive line, but a tab is not allowed.  (If the line
6684 begins with a tab, it will be considered part of a recipe for a rule.)
6685 Aside from this, extra spaces or tabs may be inserted with no effect
6686 anywhere except within the directive name or within an argument.  A
6687 comment starting with @samp{#} may appear at the end of the line.
6688
6689 The other two directives that play a part in a conditional are @code{else}
6690 and @code{endif}.  Each of these directives is written as one word, with no
6691 arguments.  Extra spaces are allowed and ignored at the beginning of the
6692 line, and spaces or tabs at the end.  A comment starting with @samp{#} may
6693 appear at the end of the line.
6694
6695 Conditionals affect which lines of the makefile @code{make} uses.  If
6696 the condition is true, @code{make} reads the lines of the
6697 @var{text-if-true} as part of the makefile; if the condition is false,
6698 @code{make} ignores those lines completely.  It follows that syntactic
6699 units of the makefile, such as rules, may safely be split across the
6700 beginning or the end of the conditional.@refill
6701
6702 @code{make} evaluates conditionals when it reads a makefile.
6703 Consequently, you cannot use automatic variables in the tests of
6704 conditionals because they are not defined until recipes are run
6705 (@pxref{Automatic Variables}).
6706
6707 To prevent intolerable confusion, it is not permitted to start a
6708 conditional in one makefile and end it in another.  However, you may
6709 write an @code{include} directive within a conditional, provided you do
6710 not attempt to terminate the conditional inside the included file.
6711
6712 @node Testing Flags,  , Conditional Syntax, Conditionals
6713 @section Conditionals that Test Flags
6714
6715 You can write a conditional that tests @code{make} command flags such as
6716 @samp{-t} by using the variable @code{MAKEFLAGS} together with the
6717 @code{findstring} function
6718 (@pxref{Text Functions, , Functions for String Substitution and Analysis}).
6719 This is useful when @code{touch} is not enough to make a file appear up
6720 to date.
6721
6722 The @code{findstring} function determines whether one string appears as a
6723 substring of another.  If you want to test for the @samp{-t} flag,
6724 use @samp{t} as the first string and the value of @code{MAKEFLAGS} as
6725 the other.
6726
6727 For example, here is how to arrange to use @samp{ranlib -t} to finish
6728 marking an archive file up to date:
6729
6730 @example
6731 archive.a: @dots{}
6732 ifneq (,$(findstring t,$(MAKEFLAGS)))
6733         +touch archive.a
6734         +ranlib -t archive.a
6735 else
6736         ranlib archive.a
6737 endif
6738 @end example
6739
6740 @noindent
6741 The @samp{+} prefix marks those recipe lines as ``recursive'' so that
6742 they will be executed despite use of the @samp{-t} flag.
6743 @xref{Recursion, ,Recursive Use of @code{make}}.
6744
6745 @node Functions, Running, Conditionals, Top
6746 @chapter Functions for Transforming Text
6747 @cindex functions
6748
6749 @dfn{Functions} allow you to do text processing in the makefile to
6750 compute the files to operate on or the commands to use in recipes.
6751 You use a function in a @dfn{function call}, where you give the name
6752 of the function and some text (the @dfn{arguments}) for the function
6753 to operate on.  The result of the function's processing is substituted
6754 into the makefile at the point of the call, just as a variable might
6755 be substituted.
6756
6757 @menu
6758 * Syntax of Functions::         How to write a function call.
6759 * Text Functions::              General-purpose text manipulation functions.
6760 * File Name Functions::         Functions for manipulating file names.
6761 * Conditional Functions::       Functions that implement conditions.
6762 * Foreach Function::            Repeat some text with controlled variation.
6763 * File Function::               Write text to a file.
6764 * Call Function::               Expand a user-defined function.
6765 * Value Function::              Return the un-expanded value of a variable.
6766 * Eval Function::               Evaluate the arguments as makefile syntax.
6767 * Origin Function::             Find where a variable got its value.
6768 * Flavor Function::             Find out the flavor of a variable.
6769 * Make Control Functions::      Functions that control how make runs.
6770 * Shell Function::              Substitute the output of a shell command.
6771 * Guile Function::              Use GNU Guile embedded scripting language.
6772 @end menu
6773
6774 @node Syntax of Functions, Text Functions, Functions, Functions
6775 @section Function Call Syntax
6776 @cindex @code{$}, in function call
6777 @cindex dollar sign (@code{$}), in function call
6778 @cindex arguments of functions
6779 @cindex functions, syntax of
6780
6781 A function call resembles a variable reference.  It can appear
6782 anywhere a variable reference can appear, and it is expanded using the
6783 same rules as variable references.  A function call looks like this:
6784
6785 @example
6786 $(@var{function} @var{arguments})
6787 @end example
6788
6789 @noindent
6790 or like this:
6791
6792 @example
6793 $@{@var{function} @var{arguments}@}
6794 @end example
6795
6796 Here @var{function} is a function name; one of a short list of names
6797 that are part of @code{make}.  You can also essentially create your own
6798 functions by using the @code{call} built-in function.
6799
6800 The @var{arguments} are the arguments of the function.  They are
6801 separated from the function name by one or more spaces or tabs, and if
6802 there is more than one argument, then they are separated by commas.
6803 Such whitespace and commas are not part of an argument's value.  The
6804 delimiters which you use to surround the function call, whether
6805 parentheses or braces, can appear in an argument only in matching pairs;
6806 the other kind of delimiters may appear singly.  If the arguments
6807 themselves contain other function calls or variable references, it is
6808 wisest to use the same kind of delimiters for all the references; write
6809 @w{@samp{$(subst a,b,$(x))}}, not @w{@samp{$(subst a,b,$@{x@})}}.  This
6810 is because it is clearer, and because only one type of delimiter is
6811 matched to find the end of the reference.
6812
6813 The text written for each argument is processed by substitution of
6814 variables and function calls to produce the argument value, which
6815 is the text on which the function acts.  The substitution is done in the
6816 order in which the arguments appear.
6817
6818 Commas and unmatched parentheses or braces cannot appear in the text of an
6819 argument as written; leading spaces cannot appear in the text of the first
6820 argument as written.  These characters can be put into the argument value
6821 by variable substitution.  First define variables @code{comma} and
6822 @code{space} whose values are isolated comma and space characters, then
6823 substitute these variables where such characters are wanted, like this:
6824
6825 @example
6826 @group
6827 comma:= ,
6828 empty:=
6829 space:= $(empty) $(empty)
6830 foo:= a b c
6831 bar:= $(subst $(space),$(comma),$(foo))
6832 # @r{bar is now `a,b,c'.}
6833 @end group
6834 @end example
6835
6836 @noindent
6837 Here the @code{subst} function replaces each space with a comma, through
6838 the value of @code{foo}, and substitutes the result.
6839
6840 @node Text Functions, File Name Functions, Syntax of Functions, Functions
6841 @section Functions for String Substitution and Analysis
6842 @cindex functions, for text
6843
6844 Here are some functions that operate on strings:
6845
6846 @table @code
6847 @item $(subst @var{from},@var{to},@var{text})
6848 @findex subst
6849 Performs a textual replacement on the text @var{text}: each occurrence
6850 of @var{from} is replaced by @var{to}.  The result is substituted for
6851 the function call.  For example,
6852
6853 @example
6854 $(subst ee,EE,feet on the street)
6855 @end example
6856
6857 substitutes the string @samp{fEEt on the strEEt}.
6858
6859 @item $(patsubst @var{pattern},@var{replacement},@var{text})
6860 @findex patsubst
6861 Finds whitespace-separated words in @var{text} that match
6862 @var{pattern} and replaces them with @var{replacement}.  Here
6863 @var{pattern} may contain a @samp{%} which acts as a wildcard,
6864 matching any number of any characters within a word.  If
6865 @var{replacement} also contains a @samp{%}, the @samp{%} is replaced
6866 by the text that matched the @samp{%} in @var{pattern}.  Only the first
6867 @samp{%} in the @var{pattern} and @var{replacement} is treated this
6868 way; any subsequent @samp{%} is unchanged.@refill
6869
6870 @cindex @code{%}, quoting in @code{patsubst}
6871 @cindex @code{%}, quoting with @code{\} (backslash)
6872 @cindex @code{\} (backslash), to quote @code{%}
6873 @cindex backslash (@code{\}), to quote @code{%}
6874 @cindex quoting @code{%}, in @code{patsubst}
6875 @samp{%} characters in @code{patsubst} function invocations can be
6876 quoted with preceding backslashes (@samp{\}).  Backslashes that would
6877 otherwise quote @samp{%} characters can be quoted with more backslashes.
6878 Backslashes that quote @samp{%} characters or other backslashes are
6879 removed from the pattern before it is compared file names or has a stem
6880 substituted into it.  Backslashes that are not in danger of quoting
6881 @samp{%} characters go unmolested.  For example, the pattern
6882 @file{the\%weird\\%pattern\\} has @samp{the%weird\} preceding the
6883 operative @samp{%} character, and @samp{pattern\\} following it.  The
6884 final two backslashes are left alone because they cannot affect any
6885 @samp{%} character.@refill
6886
6887 Whitespace between words is folded into single space characters;
6888 leading and trailing whitespace is discarded.
6889
6890 For example,
6891
6892 @example
6893 $(patsubst %.c,%.o,x.c.c bar.c)
6894 @end example
6895
6896 @noindent
6897 produces the value @samp{x.c.o bar.o}.
6898
6899 Substitution references (@pxref{Substitution Refs, ,Substitution
6900 References}) are a simpler way to get the effect of the @code{patsubst}
6901 function:
6902
6903 @example
6904 $(@var{var}:@var{pattern}=@var{replacement})
6905 @end example
6906
6907 @noindent
6908 is equivalent to
6909
6910 @example
6911 $(patsubst @var{pattern},@var{replacement},$(@var{var}))
6912 @end example
6913
6914 The second shorthand simplifies one of the most common uses of
6915 @code{patsubst}: replacing the suffix at the end of file names.
6916
6917 @example
6918 $(@var{var}:@var{suffix}=@var{replacement})
6919 @end example
6920
6921 @noindent
6922 is equivalent to
6923
6924 @example
6925 $(patsubst %@var{suffix},%@var{replacement},$(@var{var}))
6926 @end example
6927
6928 @noindent
6929 For example, you might have a list of object files:
6930
6931 @example
6932 objects = foo.o bar.o baz.o
6933 @end example
6934
6935 @noindent
6936 To get the list of corresponding source files, you could simply write:
6937
6938 @example
6939 $(objects:.o=.c)
6940 @end example
6941
6942 @noindent
6943 instead of using the general form:
6944
6945 @example
6946 $(patsubst %.o,%.c,$(objects))
6947 @end example
6948
6949 @item $(strip @var{string})
6950 @cindex stripping whitespace
6951 @cindex whitespace, stripping
6952 @cindex spaces, stripping
6953 @findex strip
6954 Removes leading and trailing whitespace from @var{string} and replaces
6955 each internal sequence of one or more whitespace characters with a
6956 single space.  Thus, @samp{$(strip a b  c )} results in @w{@samp{a b c}}.
6957
6958 The function @code{strip} can be very useful when used in conjunction
6959 with conditionals.  When comparing something with the empty string
6960 @samp{} using @code{ifeq} or @code{ifneq}, you usually want a string of
6961 just whitespace to match the empty string (@pxref{Conditionals}).
6962
6963 Thus, the following may fail to have the desired results:
6964
6965 @example
6966 .PHONY: all
6967 ifneq   "$(needs_made)" ""
6968 all: $(needs_made)
6969 else
6970 all:;@@echo 'Nothing to make!'
6971 endif
6972 @end example
6973
6974 @noindent
6975 Replacing the variable reference @w{@samp{$(needs_made)}} with the
6976 function call @w{@samp{$(strip $(needs_made))}} in the @code{ifneq}
6977 directive would make it more robust.@refill
6978
6979 @item $(findstring @var{find},@var{in})
6980 @findex findstring
6981 @cindex searching for strings
6982 @cindex finding strings
6983 @cindex strings, searching for
6984 Searches @var{in} for an occurrence of @var{find}.  If it occurs, the
6985 value is @var{find}; otherwise, the value is empty.  You can use this
6986 function in a conditional to test for the presence of a specific
6987 substring in a given string.  Thus, the two examples,
6988
6989 @example
6990 $(findstring a,a b c)
6991 $(findstring a,b c)
6992 @end example
6993
6994 @noindent
6995 produce the values @samp{a} and @samp{} (the empty string),
6996 respectively.  @xref{Testing Flags}, for a practical application of
6997 @code{findstring}.@refill
6998
6999 @need 750
7000 @findex filter
7001 @cindex filtering words
7002 @cindex words, filtering
7003 @item $(filter @var{pattern}@dots{},@var{text})
7004 Returns all whitespace-separated words in @var{text} that @emph{do} match
7005 any of the @var{pattern} words, removing any words that @emph{do not}
7006 match.  The patterns are written using @samp{%}, just like the patterns
7007 used in the @code{patsubst} function above.@refill
7008
7009 The @code{filter} function can be used to separate out different types
7010 of strings (such as file names) in a variable.  For example:
7011
7012 @example
7013 sources := foo.c bar.c baz.s ugh.h
7014 foo: $(sources)
7015         cc $(filter %.c %.s,$(sources)) -o foo
7016 @end example
7017
7018 @noindent
7019 says that @file{foo} depends of @file{foo.c}, @file{bar.c},
7020 @file{baz.s} and @file{ugh.h} but only @file{foo.c}, @file{bar.c} and
7021 @file{baz.s} should be specified in the command to the
7022 compiler.@refill
7023
7024 @item $(filter-out @var{pattern}@dots{},@var{text})
7025 @findex filter-out
7026 @cindex filtering out words
7027 @cindex words, filtering out
7028 Returns all whitespace-separated words in @var{text} that @emph{do not}
7029 match any of the @var{pattern} words, removing the words that @emph{do}
7030 match one or more.  This is the exact opposite of the @code{filter}
7031 function.@refill
7032
7033 For example, given:
7034
7035 @example
7036 @group
7037 objects=main1.o foo.o main2.o bar.o
7038 mains=main1.o main2.o
7039 @end group
7040 @end example
7041
7042 @noindent
7043 the following generates a list which contains all the object files not
7044 in @samp{mains}:
7045
7046 @example
7047 $(filter-out $(mains),$(objects))
7048 @end example
7049
7050 @need 1500
7051 @findex sort
7052 @cindex sorting words
7053 @item $(sort @var{list})
7054 Sorts the words of @var{list} in lexical order, removing duplicate
7055 words.  The output is a list of words separated by single spaces.
7056 Thus,
7057
7058 @example
7059 $(sort foo bar lose)
7060 @end example
7061
7062 @noindent
7063 returns the value @samp{bar foo lose}.
7064
7065 @cindex removing duplicate words
7066 @cindex duplicate words, removing
7067 @cindex words, removing duplicates
7068 Incidentally, since @code{sort} removes duplicate words, you can use
7069 it for this purpose even if you don't care about the sort order.
7070
7071 @item $(word @var{n},@var{text})
7072 @findex word
7073 @cindex word, selecting a
7074 @cindex selecting a word
7075 Returns the @var{n}th word of @var{text}.  The legitimate values of
7076 @var{n} start from 1.  If @var{n} is bigger than the number of words
7077 in @var{text}, the value is empty.  For example,
7078
7079 @example
7080 $(word 2, foo bar baz)
7081 @end example
7082
7083 @noindent
7084 returns @samp{bar}.
7085
7086 @item $(wordlist @var{s},@var{e},@var{text})
7087 @findex wordlist
7088 @cindex words, selecting lists of
7089 @cindex selecting word lists
7090 Returns the list of words in @var{text} starting with word @var{s} and
7091 ending with word @var{e} (inclusive).  The legitimate values of @var{s}
7092 start from 1; @var{e} may start from 0.  If @var{s} is bigger than the
7093 number of words in @var{text}, the value is empty.  If @var{e} is
7094 bigger than the number of words in @var{text}, words up to the end of
7095 @var{text} are returned.  If @var{s} is greater than @var{e}, nothing
7096 is returned.  For example,
7097
7098 @example
7099 $(wordlist 2, 3, foo bar baz)
7100 @end example
7101
7102 @noindent
7103 returns @samp{bar baz}.
7104
7105 @c Following item phrased to prevent overfull hbox.  --RJC 17 Jul 92
7106 @item $(words @var{text})
7107 @findex words
7108 @cindex words, finding number
7109 Returns the number of words in @var{text}.
7110 Thus, the last word of @var{text} is
7111 @w{@code{$(word $(words @var{text}),@var{text})}}.@refill
7112
7113 @item $(firstword @var{names}@dots{})
7114 @findex firstword
7115 @cindex words, extracting first
7116 The argument @var{names} is regarded as a series of names, separated
7117 by whitespace.  The value is the first name in the series.  The rest
7118 of the names are ignored.
7119
7120 For example,
7121
7122 @example
7123 $(firstword foo bar)
7124 @end example
7125
7126 @noindent
7127 produces the result @samp{foo}.  Although @code{$(firstword
7128 @var{text})} is the same as @code{$(word 1,@var{text})}, the
7129 @code{firstword} function is retained for its simplicity.@refill
7130
7131
7132 @item $(lastword @var{names}@dots{})
7133 @findex lastword
7134 @cindex words, extracting last
7135 The argument @var{names} is regarded as a series of names, separated
7136 by whitespace.  The value is the last name in the series.
7137
7138 For example,
7139
7140 @example
7141 $(lastword foo bar)
7142 @end example
7143
7144 @noindent
7145 produces the result @samp{bar}.  Although @code{$(lastword
7146 @var{text})} is the same as @code{$(word $(words @var{text}),@var{text})},
7147 the @code{lastword} function was added for its simplicity and better
7148 performance.@refill
7149 @end table
7150
7151
7152 Here is a realistic example of the use of @code{subst} and
7153 @code{patsubst}.  Suppose that a makefile uses the @code{VPATH} variable
7154 to specify a list of directories that @code{make} should search for
7155 prerequisite files
7156 (@pxref{General Search, , @code{VPATH} Search Path for All Prerequisites}).
7157 This example shows how to
7158 tell the C compiler to search for header files in the same list of
7159 directories.@refill
7160
7161 The value of @code{VPATH} is a list of directories separated by colons,
7162 such as @samp{src:../headers}.  First, the @code{subst} function is used to
7163 change the colons to spaces:
7164
7165 @example
7166 $(subst :, ,$(VPATH))
7167 @end example
7168
7169 @noindent
7170 This produces @samp{src ../headers}.  Then @code{patsubst} is used to turn
7171 each directory name into a @samp{-I} flag.  These can be added to the
7172 value of the variable @code{CFLAGS}, which is passed automatically to the C
7173 compiler, like this:
7174
7175 @example
7176 override CFLAGS += $(patsubst %,-I%,$(subst :, ,$(VPATH)))
7177 @end example
7178
7179 @noindent
7180 The effect is to append the text @samp{-Isrc -I../headers} to the
7181 previously given value of @code{CFLAGS}.  The @code{override} directive is
7182 used so that the new value is assigned even if the previous value of
7183 @code{CFLAGS} was specified with a command argument (@pxref{Override
7184 Directive, , The @code{override} Directive}).
7185
7186 @node File Name Functions, Conditional Functions, Text Functions, Functions
7187 @section Functions for File Names
7188 @cindex functions, for file names
7189 @cindex file name functions
7190
7191 Several of the built-in expansion functions relate specifically to
7192 taking apart file names or lists of file names.
7193
7194 Each of the following functions performs a specific transformation on a
7195 file name.  The argument of the function is regarded as a series of file
7196 names, separated by whitespace.  (Leading and trailing whitespace is
7197 ignored.)  Each file name in the series is transformed in the same way and
7198 the results are concatenated with single spaces between them.
7199
7200 @table @code
7201 @item $(dir @var{names}@dots{})
7202 @findex dir
7203 @cindex directory part
7204 @cindex file name, directory part
7205 Extracts the directory-part of each file name in @var{names}.  The
7206 directory-part of the file name is everything up through (and
7207 including) the last slash in it.  If the file name contains no slash,
7208 the directory part is the string @samp{./}.  For example,
7209
7210 @example
7211 $(dir src/foo.c hacks)
7212 @end example
7213
7214 @noindent
7215 produces the result @samp{src/ ./}.
7216
7217 @item $(notdir @var{names}@dots{})
7218 @findex notdir
7219 @cindex file name, nondirectory part
7220 @cindex nondirectory part
7221 Extracts all but the directory-part of each file name in @var{names}.
7222 If the file name contains no slash, it is left unchanged.  Otherwise,
7223 everything through the last slash is removed from it.
7224
7225 A file name that ends with a slash becomes an empty string.  This is
7226 unfortunate, because it means that the result does not always have the
7227 same number of whitespace-separated file names as the argument had;
7228 but we do not see any other valid alternative.
7229
7230 For example,
7231
7232 @example
7233 $(notdir src/foo.c hacks)
7234 @end example
7235
7236 @noindent
7237 produces the result @samp{foo.c hacks}.
7238
7239 @item $(suffix @var{names}@dots{})
7240 @findex suffix
7241 @cindex suffix, function to find
7242 @cindex file name suffix
7243 Extracts the suffix of each file name in @var{names}.  If the file name
7244 contains a period, the suffix is everything starting with the last
7245 period.  Otherwise, the suffix is the empty string.  This frequently
7246 means that the result will be empty when @var{names} is not, and if
7247 @var{names} contains multiple file names, the result may contain fewer
7248 file names.
7249
7250 For example,
7251
7252 @example
7253 $(suffix src/foo.c src-1.0/bar.c hacks)
7254 @end example
7255
7256 @noindent
7257 produces the result @samp{.c .c}.
7258
7259 @item $(basename @var{names}@dots{})
7260 @findex basename
7261 @cindex basename
7262 @cindex file name, basename of
7263 Extracts all but the suffix of each file name in @var{names}.  If the
7264 file name contains a period, the basename is everything starting up to
7265 (and not including) the last period.  Periods in the directory part are
7266 ignored.  If there is no period, the basename is the entire file name.
7267 For example,
7268
7269 @example
7270 $(basename src/foo.c src-1.0/bar hacks)
7271 @end example
7272
7273 @noindent
7274 produces the result @samp{src/foo src-1.0/bar hacks}.
7275
7276 @c plural convention with dots (be consistent)
7277 @item $(addsuffix @var{suffix},@var{names}@dots{})
7278 @findex addsuffix
7279 @cindex suffix, adding
7280 @cindex file name suffix, adding
7281 The argument @var{names} is regarded as a series of names, separated
7282 by whitespace; @var{suffix} is used as a unit.  The value of
7283 @var{suffix} is appended to the end of each individual name and the
7284 resulting larger names are concatenated with single spaces between
7285 them.  For example,
7286
7287 @example
7288 $(addsuffix .c,foo bar)
7289 @end example
7290
7291 @noindent
7292 produces the result @samp{foo.c bar.c}.
7293
7294 @item $(addprefix @var{prefix},@var{names}@dots{})
7295 @findex addprefix
7296 @cindex prefix, adding
7297 @cindex file name prefix, adding
7298 The argument @var{names} is regarded as a series of names, separated
7299 by whitespace; @var{prefix} is used as a unit.  The value of
7300 @var{prefix} is prepended to the front of each individual name and the
7301 resulting larger names are concatenated with single spaces between
7302 them.  For example,
7303
7304 @example
7305 $(addprefix src/,foo bar)
7306 @end example
7307
7308 @noindent
7309 produces the result @samp{src/foo src/bar}.
7310
7311 @item $(join @var{list1},@var{list2})
7312 @findex join
7313 @cindex joining lists of words
7314 @cindex words, joining lists
7315 Concatenates the two arguments word by word: the two first words (one
7316 from each argument) concatenated form the first word of the result, the
7317 two second words form the second word of the result, and so on.  So the
7318 @var{n}th word of the result comes from the @var{n}th word of each
7319 argument.  If one argument has more words that the other, the extra
7320 words are copied unchanged into the result.
7321
7322 For example, @samp{$(join a b,.c .o)} produces @samp{a.c b.o}.
7323
7324 Whitespace between the words in the lists is not preserved; it is
7325 replaced with a single space.
7326
7327 This function can merge the results of the @code{dir} and
7328 @code{notdir} functions, to produce the original list of files which
7329 was given to those two functions.@refill
7330
7331 @item $(wildcard @var{pattern})
7332 @findex wildcard
7333 @cindex wildcard, function
7334 The argument @var{pattern} is a file name pattern, typically containing
7335 wildcard characters (as in shell file name patterns).  The result of
7336 @code{wildcard} is a space-separated list of the names of existing files
7337 that match the pattern.
7338 @xref{Wildcards, ,Using Wildcard Characters in File Names}.
7339
7340 @item $(realpath @var{names}@dots{})
7341 @findex realpath
7342 @cindex realpath
7343 @cindex file name, realpath of
7344 For each file name in @var{names} return the canonical absolute name.
7345 A canonical name does not contain any @code{.} or @code{..} components,
7346 nor any repeated path separators (@code{/}) or symlinks.  In case of a
7347 failure the empty string is returned.  Consult the @code{realpath(3)}
7348 documentation for a list of possible failure causes.
7349
7350 @item $(abspath @var{names}@dots{})
7351 @findex abspath
7352 @cindex abspath
7353 @cindex file name, abspath of
7354 For each file name in @var{names} return an absolute name that does
7355 not contain any @code{.} or @code{..} components, nor any repeated path
7356 separators (@code{/}).  Note that, in contrast to @code{realpath}
7357 function, @code{abspath} does not resolve symlinks and does not require
7358 the file names to refer to an existing file or directory.  Use the
7359 @code{wildcard} function to test for existence.
7360 @end table
7361
7362 @node Conditional Functions, Foreach Function, File Name Functions, Functions
7363 @section Functions for Conditionals
7364 @findex if
7365 @cindex conditional expansion
7366 There are three functions that provide conditional expansion.  A key
7367 aspect of these functions is that not all of the arguments are
7368 expanded initially.  Only those arguments which need to be expanded,
7369 will be expanded.
7370
7371 @table @code
7372 @item $(if @var{condition},@var{then-part}[,@var{else-part}])
7373 @findex if
7374 The @code{if} function provides support for conditional expansion in a
7375 functional context (as opposed to the GNU @code{make} makefile
7376 conditionals such as @code{ifeq} (@pxref{Conditional Syntax, ,Syntax of
7377 Conditionals}).
7378
7379 The first argument, @var{condition}, first has all preceding and
7380 trailing whitespace stripped, then is expanded.  If it expands to any
7381 non-empty string, then the condition is considered to be true.  If it
7382 expands to an empty string, the condition is considered to be false.
7383
7384 If the condition is true then the second argument, @var{then-part}, is
7385 evaluated and this is used as the result of the evaluation of the entire
7386 @code{if} function.
7387
7388 If the condition is false then the third argument, @var{else-part}, is
7389 evaluated and this is the result of the @code{if} function.  If there is
7390 no third argument, the @code{if} function evaluates to nothing (the
7391 empty string).
7392
7393 Note that only one of the @var{then-part} or the @var{else-part} will be
7394 evaluated, never both.  Thus, either can contain side-effects (such as
7395 @code{shell} function calls, etc.)
7396
7397 @item $(or @var{condition1}[,@var{condition2}[,@var{condition3}@dots{}]])
7398 @findex or
7399 The @code{or} function provides a ``short-circuiting'' OR operation.
7400 Each argument is expanded, in order.  If an argument expands to a
7401 non-empty string the processing stops and the result of the expansion
7402 is that string.  If, after all arguments are expanded, all of them are
7403 false (empty), then the result of the expansion is the empty string.
7404
7405 @item $(and @var{condition1}[,@var{condition2}[,@var{condition3}@dots{}]])
7406 @findex and
7407 The @code{and} function provides a ``short-circuiting'' AND operation.
7408 Each argument is expanded, in order.  If an argument expands to an
7409 empty string the processing stops and the result of the expansion is
7410 the empty string.  If all arguments expand to a non-empty string then
7411 the result of the expansion is the expansion of the last argument.
7412
7413 @end table
7414
7415 @node Foreach Function, File Function, Conditional Functions, Functions
7416 @section The @code{foreach} Function
7417 @findex foreach
7418 @cindex words, iterating over
7419
7420 The @code{foreach} function is very different from other functions.  It
7421 causes one piece of text to be used repeatedly, each time with a different
7422 substitution performed on it.  It resembles the @code{for} command in the
7423 shell @code{sh} and the @code{foreach} command in the C-shell @code{csh}.
7424
7425 The syntax of the @code{foreach} function is:
7426
7427 @example
7428 $(foreach @var{var},@var{list},@var{text})
7429 @end example
7430
7431 @noindent
7432 The first two arguments, @var{var} and @var{list}, are expanded before
7433 anything else is done; note that the last argument, @var{text}, is
7434 @strong{not} expanded at the same time.  Then for each word of the expanded
7435 value of @var{list}, the variable named by the expanded value of @var{var}
7436 is set to that word, and @var{text} is expanded.  Presumably @var{text}
7437 contains references to that variable, so its expansion will be different
7438 each time.
7439
7440 The result is that @var{text} is expanded as many times as there are
7441 whitespace-separated words in @var{list}.  The multiple expansions of
7442 @var{text} are concatenated, with spaces between them, to make the result
7443 of @code{foreach}.
7444
7445 This simple example sets the variable @samp{files} to the list of all files
7446 in the directories in the list @samp{dirs}:
7447
7448 @example
7449 dirs := a b c d
7450 files := $(foreach dir,$(dirs),$(wildcard $(dir)/*))
7451 @end example
7452
7453 Here @var{text} is @samp{$(wildcard $(dir)/*)}.  The first repetition
7454 finds the value @samp{a} for @code{dir}, so it produces the same result
7455 as @samp{$(wildcard a/*)}; the second repetition produces the result
7456 of @samp{$(wildcard b/*)}; and the third, that of @samp{$(wildcard c/*)}.
7457
7458 This example has the same result (except for setting @samp{dirs}) as
7459 the following example:
7460
7461 @example
7462 files := $(wildcard a/* b/* c/* d/*)
7463 @end example
7464
7465 When @var{text} is complicated, you can improve readability by giving it
7466 a name, with an additional variable:
7467
7468 @example
7469 find_files = $(wildcard $(dir)/*)
7470 dirs := a b c d
7471 files := $(foreach dir,$(dirs),$(find_files))
7472 @end example
7473
7474 @noindent
7475 Here we use the variable @code{find_files} this way.  We use plain @samp{=}
7476 to define a recursively-expanding variable, so that its value contains an
7477 actual function call to be re-expanded under the control of @code{foreach};
7478 a simply-expanded variable would not do, since @code{wildcard} would be
7479 called only once at the time of defining @code{find_files}.
7480
7481 The @code{foreach} function has no permanent effect on the variable
7482 @var{var}; its value and flavor after the @code{foreach} function call are
7483 the same as they were beforehand.  The other values which are taken from
7484 @var{list} are in effect only temporarily, during the execution of
7485 @code{foreach}.  The variable @var{var} is a simply-expanded variable
7486 during the execution of @code{foreach}.  If @var{var} was undefined
7487 before the @code{foreach} function call, it is undefined after the call.
7488 @xref{Flavors, ,The Two Flavors of Variables}.@refill
7489
7490 You must take care when using complex variable expressions that result in
7491 variable names because many strange things are valid variable names, but
7492 are probably not what you intended.  For example,
7493
7494 @smallexample
7495 files := $(foreach Esta-escrito-en-espanol!,b c ch,$(find_files))
7496 @end smallexample
7497
7498 @noindent
7499 might be useful if the value of @code{find_files} references the variable
7500 whose name is @samp{Esta-escrito-en-espanol!} (es un nombre bastante largo,
7501 no?), but it is more likely to be a mistake.
7502
7503 @node File Function, Call Function, Foreach Function, Functions
7504 @section The @code{file} Function
7505 @findex file
7506 @cindex writing to a file
7507 @cindex file, writing to
7508
7509 The @code{file} function allows the makefile to write to a file.  Two
7510 modes of writing are supported: overwrite, where the text is written
7511 to the beginning of the file and any existing content is lost, and
7512 append, where the text is written to the end of the file, preserving
7513 the existing content.  In all cases the file is created if it does not
7514 exist.
7515
7516 The syntax of the @code{file} function is:
7517
7518 @example
7519 $(file @var{op} @var{filename},@var{text})
7520 @end example
7521
7522 The operator @var{op} can be either @code{>} which indicates overwrite
7523 mode, or @code{>>} which indicates append mode.  The @var{filename}
7524 indicates the file to be written to.  There may optionally be
7525 whitespace between the operator and the file name.
7526
7527 When the @code{file} function is expanded all its arguments are
7528 expanded first, then the file indicated by @var{filename} will be
7529 opened in the mode described by @var{op}.  Finally @var{text} will be
7530 written to the file.  If @var{text} does not already end in a newline,
7531 a final newline will be written.  The result of evaluating the
7532 @code{file} function is always the empty string.
7533
7534 It is a fatal error if the file cannot be opened for writing, or if
7535 the write operation fails.
7536
7537 For example, the @code{file} function can be useful if your build
7538 system has a limited command line size and your recipe runs a command
7539 that can accept arguments from a file as well.  Many commands use the
7540 convention that an argument prefixed with an @code{@@} specifies a
7541 file containing more arguments.  Then you might write your recipe in
7542 this way:
7543
7544 @example
7545 @group
7546 program: $(OBJECTS)
7547         $(file >$@@.in,$^)
7548         $(CMD) $(CMDFLAGS) @@$@@.in
7549         @@rm $@@.in
7550 @end group
7551 @end example
7552
7553 If the command required each argument to be on a separate line of the
7554 input file, you might write your recipe like this:
7555
7556 @example
7557 @group
7558 program: $(OBJECTS)
7559         $(file >$@@.in,) $(foreach O,$^,$(file >>$@@.in,$O))
7560         $(CMD) $(CMDFLAGS) @@$@@.in
7561         @@rm $@@.in
7562 @end group
7563 @end example
7564
7565 @node Call Function, Value Function, File Function, Functions
7566 @section The @code{call} Function
7567 @findex call
7568 @cindex functions, user defined
7569 @cindex user defined functions
7570
7571 The @code{call} function is unique in that it can be used to create new
7572 parameterized functions.  You can write a complex expression as the
7573 value of a variable, then use @code{call} to expand it with different
7574 values.
7575
7576 The syntax of the @code{call} function is:
7577
7578 @example
7579 $(call @var{variable},@var{param},@var{param},@dots{})
7580 @end example
7581
7582 When @code{make} expands this function, it assigns each @var{param} to
7583 temporary variables @code{$(1)}, @code{$(2)}, etc.  The variable
7584 @code{$(0)} will contain @var{variable}.  There is no maximum number of
7585 parameter arguments.  There is no minimum, either, but it doesn't make
7586 sense to use @code{call} with no parameters.
7587
7588 Then @var{variable} is expanded as a @code{make} variable in the context
7589 of these temporary assignments.  Thus, any reference to @code{$(1)} in
7590 the value of @var{variable} will resolve to the first @var{param} in the
7591 invocation of @code{call}.
7592
7593 Note that @var{variable} is the @emph{name} of a variable, not a
7594 @emph{reference} to that variable.  Therefore you would not normally use
7595 a @samp{$} or parentheses when writing it.  (You can, however, use a
7596 variable reference in the name if you want the name not to be a
7597 constant.)
7598
7599 If @var{variable} is the name of a built-in function, the built-in function
7600 is always invoked (even if a @code{make} variable by that name also
7601 exists).
7602
7603 The @code{call} function expands the @var{param} arguments before
7604 assigning them to temporary variables.  This means that @var{variable}
7605 values containing references to built-in functions that have special
7606 expansion rules, like @code{foreach} or @code{if}, may not work as you
7607 expect.
7608
7609 Some examples may make this clearer.
7610
7611 This macro simply reverses its arguments:
7612
7613 @smallexample
7614 reverse = $(2) $(1)
7615
7616 foo = $(call reverse,a,b)
7617 @end smallexample
7618
7619 @noindent
7620 Here @var{foo} will contain @samp{b a}.
7621
7622 This one is slightly more interesting: it defines a macro to search for
7623 the first instance of a program in @code{PATH}:
7624
7625 @smallexample
7626 pathsearch = $(firstword $(wildcard $(addsuffix /$(1),$(subst :, ,$(PATH)))))
7627
7628 LS := $(call pathsearch,ls)
7629 @end smallexample
7630
7631 @noindent
7632 Now the variable LS contains @code{/bin/ls} or similar.
7633
7634 The @code{call} function can be nested.  Each recursive invocation gets
7635 its own local values for @code{$(1)}, etc.@: that mask the values of
7636 higher-level @code{call}.  For example, here is an implementation of a
7637 @dfn{map} function:
7638
7639 @smallexample
7640 map = $(foreach a,$(2),$(call $(1),$(a)))
7641 @end smallexample
7642
7643 Now you can @var{map} a function that normally takes only one argument,
7644 such as @code{origin}, to multiple values in one step:
7645
7646 @smallexample
7647 o = $(call map,origin,o map MAKE)
7648 @end smallexample
7649
7650 and end up with @var{o} containing something like @samp{file file default}.
7651
7652 A final caution: be careful when adding whitespace to the arguments to
7653 @code{call}.  As with other functions, any whitespace contained in the
7654 second and subsequent arguments is kept; this can cause strange
7655 effects.  It's generally safest to remove all extraneous whitespace when
7656 providing parameters to @code{call}.
7657
7658 @node Value Function, Eval Function, Call Function, Functions
7659 @comment  node-name,  next,  previous,  up
7660 @section The @code{value} Function
7661 @findex value
7662 @cindex variables, unexpanded value
7663
7664 The @code{value} function provides a way for you to use the value of a
7665 variable @emph{without} having it expanded.  Please note that this
7666 does not undo expansions which have already occurred; for example if
7667 you create a simply expanded variable its value is expanded during the
7668 definition; in that case the @code{value} function will return the
7669 same result as using the variable directly.
7670
7671 The syntax of the @code{value} function is:
7672
7673 @example
7674 $(value @var{variable})
7675 @end example
7676
7677 Note that @var{variable} is the @emph{name} of a variable, not a
7678 @emph{reference} to that variable.  Therefore you would not normally
7679 use a @samp{$} or parentheses when writing it.  (You can, however, use
7680 a variable reference in the name if you want the name not to be a
7681 constant.)
7682
7683 The result of this function is a string containing the value of
7684 @var{variable}, without any expansion occurring.  For example, in this
7685 makefile:
7686
7687 @example
7688 @group
7689 FOO = $PATH
7690
7691 all:
7692         @@echo $(FOO)
7693         @@echo $(value FOO)
7694 @end group
7695 @end example
7696
7697 @noindent
7698 The first output line would be @code{ATH}, since the ``$P'' would be
7699 expanded as a @code{make} variable, while the second output line would
7700 be the current value of your @code{$PATH} environment variable, since
7701 the @code{value} function avoided the expansion.
7702
7703 The @code{value} function is most often used in conjunction with the
7704 @code{eval} function (@pxref{Eval Function}).
7705
7706 @node Eval Function, Origin Function, Value Function, Functions
7707 @comment  node-name,  next,  previous,  up
7708 @section The @code{eval} Function
7709 @findex eval
7710 @cindex evaluating makefile syntax
7711 @cindex makefile syntax, evaluating
7712
7713 The @code{eval} function is very special: it allows you to define new
7714 makefile constructs that are not constant; which are the result of
7715 evaluating other variables and functions.  The argument to the
7716 @code{eval} function is expanded, then the results of that expansion
7717 are parsed as makefile syntax.  The expanded results can define new
7718 @code{make} variables, targets, implicit or explicit rules, etc.
7719
7720 The result of the @code{eval} function is always the empty string;
7721 thus, it can be placed virtually anywhere in a makefile without
7722 causing syntax errors.
7723
7724 It's important to realize that the @code{eval} argument is expanded
7725 @emph{twice}; first by the @code{eval} function, then the results of
7726 that expansion are expanded again when they are parsed as makefile
7727 syntax.  This means you may need to provide extra levels of escaping
7728 for ``$'' characters when using @code{eval}.  The @code{value}
7729 function (@pxref{Value Function}) can sometimes be useful in these
7730 situations, to circumvent unwanted expansions.
7731
7732 Here is an example of how @code{eval} can be used; this example
7733 combines a number of concepts and other functions.  Although it might
7734 seem overly complex to use @code{eval} in this example, rather than
7735 just writing out the rules, consider two things: first, the template
7736 definition (in @code{PROGRAM_template}) could need to be much more
7737 complex than it is here; and second, you might put the complex,
7738 ``generic'' part of this example into another makefile, then include
7739 it in all the individual makefiles.  Now your individual makefiles are
7740 quite straightforward.
7741
7742 @example
7743 @group
7744 PROGRAMS    = server client
7745
7746 server_OBJS = server.o server_priv.o server_access.o
7747 server_LIBS = priv protocol
7748
7749 client_OBJS = client.o client_api.o client_mem.o
7750 client_LIBS = protocol
7751
7752 # Everything after this is generic
7753
7754 .PHONY: all
7755 all: $(PROGRAMS)
7756
7757 define PROGRAM_template =
7758  $(1): $$($(1)_OBJS) $$($(1)_LIBS:%=-l%)
7759  ALL_OBJS   += $$($(1)_OBJS)
7760 endef
7761
7762 $(foreach prog,$(PROGRAMS),$(eval $(call PROGRAM_template,$(prog))))
7763
7764 $(PROGRAMS):
7765         $(LINK.o) $^ $(LDLIBS) -o $@@
7766
7767 clean:
7768         rm -f $(ALL_OBJS) $(PROGRAMS)
7769 @end group
7770 @end example
7771
7772 @node Origin Function, Flavor Function, Eval Function, Functions
7773 @section The @code{origin} Function
7774 @findex origin
7775 @cindex variables, origin of
7776 @cindex origin of variable
7777
7778 The @code{origin} function is unlike most other functions in that it does
7779 not operate on the values of variables; it tells you something @emph{about}
7780 a variable.  Specifically, it tells you where it came from.
7781
7782 The syntax of the @code{origin} function is:
7783
7784 @example
7785 $(origin @var{variable})
7786 @end example
7787
7788 Note that @var{variable} is the @emph{name} of a variable to inquire about,
7789 not a @emph{reference} to that variable.  Therefore you would not normally
7790 use a @samp{$} or parentheses when writing it.  (You can, however, use a
7791 variable reference in the name if you want the name not to be a constant.)
7792
7793 The result of this function is a string telling you how the variable
7794 @var{variable} was defined:
7795
7796 @table @samp
7797 @item undefined
7798
7799 if @var{variable} was never defined.
7800
7801 @item default
7802
7803 if @var{variable} has a default definition, as is usual with @code{CC}
7804 and so on.  @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
7805 Note that if you have redefined a default variable, the @code{origin}
7806 function will return the origin of the later definition.
7807
7808 @item environment
7809
7810 if @var{variable} was inherited from the environment provided to
7811 @code{make}.
7812
7813 @item environment override
7814
7815 if @var{variable} was inherited from the environment provided to
7816 @code{make}, and is overriding a setting for @var{variable} in the
7817 makefile as a result of the @w{@samp{-e}} option (@pxref{Options
7818 Summary, ,Summary of Options}).@refill
7819
7820 @item file
7821
7822 if @var{variable} was defined in a makefile.
7823
7824 @item command line
7825
7826 if @var{variable} was defined on the command line.
7827
7828 @item override
7829
7830 if @var{variable} was defined with an @code{override} directive in a
7831 makefile (@pxref{Override Directive, ,The @code{override} Directive}).
7832
7833 @item automatic
7834
7835 if @var{variable} is an automatic variable defined for the execution
7836 of the recipe for each rule (@pxref{Automatic Variables}).
7837 @end table
7838
7839 This information is primarily useful (other than for your curiosity) to
7840 determine if you want to believe the value of a variable.  For example,
7841 suppose you have a makefile @file{foo} that includes another makefile
7842 @file{bar}.  You want a variable @code{bletch} to be defined in @file{bar}
7843 if you run the command @w{@samp{make -f bar}}, even if the environment contains
7844 a definition of @code{bletch}.  However, if @file{foo} defined
7845 @code{bletch} before including @file{bar}, you do not want to override that
7846 definition.  This could be done by using an @code{override} directive in
7847 @file{foo}, giving that definition precedence over the later definition in
7848 @file{bar}; unfortunately, the @code{override} directive would also
7849 override any command line definitions.  So, @file{bar} could
7850 include:@refill
7851
7852 @example
7853 @group
7854 ifdef bletch
7855 ifeq "$(origin bletch)" "environment"
7856 bletch = barf, gag, etc.
7857 endif
7858 endif
7859 @end group
7860 @end example
7861
7862 @noindent
7863 If @code{bletch} has been defined from the environment, this will redefine
7864 it.
7865
7866 If you want to override a previous definition of @code{bletch} if it came
7867 from the environment, even under @samp{-e}, you could instead write:
7868
7869 @example
7870 @group
7871 ifneq "$(findstring environment,$(origin bletch))" ""
7872 bletch = barf, gag, etc.
7873 endif
7874 @end group
7875 @end example
7876
7877 Here the redefinition takes place if @samp{$(origin bletch)} returns either
7878 @samp{environment} or @samp{environment override}.
7879 @xref{Text Functions, , Functions for String Substitution and Analysis}.
7880
7881 @node Flavor Function, Make Control Functions, Origin Function, Functions
7882 @section The @code{flavor} Function
7883 @findex flavor
7884 @cindex variables, flavor of
7885 @cindex flavor of variable
7886
7887 The @code{flavor} function, like the @code{origin} function, does not
7888 operate on the values of variables but rather it tells you something
7889 @emph{about} a variable.  Specifically, it tells you the flavor of a
7890 variable (@pxref{Flavors, ,The Two Flavors of Variables}).
7891
7892 The syntax of the @code{flavor} function is:
7893
7894 @example
7895 $(flavor @var{variable})
7896 @end example
7897
7898 Note that @var{variable} is the @emph{name} of a variable to inquire about,
7899 not a @emph{reference} to that variable.  Therefore you would not normally
7900 use a @samp{$} or parentheses when writing it.  (You can, however, use a
7901 variable reference in the name if you want the name not to be a constant.)
7902
7903 The result of this function is a string that identifies the flavor of the
7904 variable @var{variable}:
7905
7906 @table @samp
7907 @item undefined
7908
7909 if @var{variable} was never defined.
7910
7911 @item recursive
7912
7913 if @var{variable} is a recursively expanded variable.
7914
7915 @item simple
7916
7917 if @var{variable} is a simply expanded variable.
7918
7919 @end table
7920
7921 @node Make Control Functions, Shell Function, Flavor Function, Functions
7922 @section Functions That Control Make
7923 @cindex functions, for controlling make
7924 @cindex controlling make
7925
7926 These functions control the way make runs.  Generally, they are used to
7927 provide information to the user of the makefile or to cause make to stop
7928 if some sort of environmental error is detected.
7929
7930 @table @code
7931 @item $(error @var{text}@dots{})
7932 @findex error
7933 @cindex error, stopping on
7934 @cindex stopping make
7935 Generates a fatal error where the message is @var{text}.  Note that
7936 the error is generated whenever this function is evaluated.  So, if
7937 you put it inside a recipe or on the right side of a recursive
7938 variable assignment, it won't be evaluated until later.  The
7939 @var{text} will be expanded before the error is generated.
7940
7941 For example,
7942
7943 @example
7944 ifdef ERROR1
7945 $(error error is $(ERROR1))
7946 endif
7947 @end example
7948
7949 @noindent
7950 will generate a fatal error during the read of the makefile if the
7951 @code{make} variable @code{ERROR1} is defined.  Or,
7952
7953 @example
7954 ERR = $(error found an error!)
7955
7956 .PHONY: err
7957 err: ; $(ERR)
7958 @end example
7959
7960 @noindent
7961 will generate a fatal error while @code{make} is running, if the
7962 @code{err} target is invoked.
7963
7964 @item $(warning @var{text}@dots{})
7965 @findex warning
7966 @cindex warnings, printing
7967 @cindex printing user warnings
7968 This function works similarly to the @code{error} function, above,
7969 except that @code{make} doesn't exit.  Instead, @var{text} is expanded
7970 and the resulting message is displayed, but processing of the makefile
7971 continues.
7972
7973 The result of the expansion of this function is the empty string.
7974
7975 @item $(info @var{text}@dots{})
7976 @findex info
7977 @cindex printing messages
7978 This function does nothing more than print its (expanded) argument(s)
7979 to standard output.  No makefile name or line number is added.  The
7980 result of the expansion of this function is the empty string.
7981 @end table
7982
7983 @node Shell Function, Guile Function, Make Control Functions, Functions
7984 @section The @code{shell} Function
7985 @findex shell
7986 @cindex command expansion
7987 @cindex backquotes
7988 @cindex shell command, function for
7989
7990 The @code{shell} function is unlike any other function other than the
7991 @code{wildcard} function
7992 (@pxref{Wildcard Function, ,The Function @code{wildcard}}) in that it
7993 communicates with the world outside of @code{make}.
7994
7995 The @code{shell} function performs the same function that backquotes
7996 (@samp{`}) perform in most shells: it does @dfn{command expansion}.
7997 This means that it takes as an argument a shell command and evaluates
7998 to the output of the command.  The only processing @code{make} does on
7999 the result is to convert each newline (or carriage-return / newline
8000 pair) to a single space.  If there is a trailing (carriage-return
8001 and) newline it will simply be removed.@refill
8002
8003 The commands run by calls to the @code{shell} function are run when the
8004 function calls are expanded (@pxref{Reading Makefiles, , How
8005 @code{make} Reads a Makefile}).  Because this function involves
8006 spawning a new shell, you should carefully consider the performance
8007 implications of using the @code{shell} function within recursively
8008 expanded variables vs.@: simply expanded variables (@pxref{Flavors, ,The
8009 Two Flavors of Variables}).
8010
8011 Here are some examples of the use of the @code{shell} function:
8012
8013 @example
8014 contents := $(shell cat foo)
8015 @end example
8016
8017 @noindent
8018 sets @code{contents} to the contents of the file @file{foo}, with a space
8019 (rather than a newline) separating each line.
8020
8021 @example
8022 files := $(shell echo *.c)
8023 @end example
8024
8025 @noindent
8026 sets @code{files} to the expansion of @samp{*.c}.  Unless @code{make} is
8027 using a very strange shell, this has the same result as
8028 @w{@samp{$(wildcard *.c)}} (as long as at least one @samp{.c} file
8029 exists).@refill
8030
8031 @node Guile Function,  , Shell Function, Functions
8032 @section The @code{guile} Function
8033 @findex guile
8034 @cindex Guile
8035
8036 If GNU @code{make} is built with support for GNU Guile as an embedded
8037 extension language then the @code{guile} function will be available.
8038 The @code{guile} function takes one argument which is first expanded
8039 by @code{make} in the normal fashion, then passed to the GNU Guile
8040 evaluator.  The result of the evaluator is converted into a string and
8041 used as the expansion of the @code{guile} function in the makefile.
8042 See @ref{Guile Integration, ,GNU Guile Integration} for details on
8043 writing extensions to @code{make} in Guile.
8044
8045 You can determine whether GNU Guile support is available by checking
8046 the @code{.FEATURES} variable for the word @var{guile}.
8047
8048 @node Running, Implicit Rules, Functions, Top
8049 @chapter How to Run @code{make}
8050
8051 A makefile that says how to recompile a program can be used in more
8052 than one way.  The simplest use is to recompile every file that is out
8053 of date.  Usually, makefiles are written so that if you run
8054 @code{make} with no arguments, it does just that.
8055
8056 But you might want to update only some of the files; you might want to use
8057 a different compiler or different compiler options; you might want just to
8058 find out which files are out of date without changing them.
8059
8060 By giving arguments when you run @code{make}, you can do any of these
8061 things and many others.
8062
8063 @cindex exit status of make
8064 The exit status of @code{make} is always one of three values:
8065 @table @code
8066 @item 0
8067 The exit status is zero if @code{make} is successful.
8068 @item 2
8069 The exit status is two if @code{make} encounters any errors.
8070 It will print messages describing the particular errors.
8071 @item 1
8072 The exit status is one if you use the @samp{-q} flag and @code{make}
8073 determines that some target is not already up to date.
8074 @xref{Instead of Execution, ,Instead of Executing Recipes}.
8075 @end table
8076
8077 @menu
8078 * Makefile Arguments::          How to specify which makefile to use.
8079 * Goals::                       How to use goal arguments to specify which
8080                                   parts of the makefile to use.
8081 * Instead of Execution::        How to use mode flags to specify what
8082                                   kind of thing to do with the recipes
8083                                   in the makefile other than simply
8084                                   execute them.
8085 * Avoiding Compilation::        How to avoid recompiling certain files.
8086 * Overriding::                  How to override a variable to specify
8087                                   an alternate compiler and other things.
8088 * Testing::                     How to proceed past some errors, to
8089                                   test compilation.
8090 * Options Summary::             Summary of Options
8091 @end menu
8092
8093 @node Makefile Arguments, Goals, Running, Running
8094 @section Arguments to Specify the Makefile
8095 @cindex @code{--file}
8096 @cindex @code{--makefile}
8097 @cindex @code{-f}
8098
8099 The way to specify the name of the makefile is with the @samp{-f} or
8100 @samp{--file} option (@samp{--makefile} also works).  For example,
8101 @samp{-f altmake} says to use the file @file{altmake} as the makefile.
8102
8103 If you use the @samp{-f} flag several times and follow each @samp{-f}
8104 with an argument, all the specified files are used jointly as
8105 makefiles.
8106
8107 If you do not use the @samp{-f} or @samp{--file} flag, the default is
8108 to try @file{GNUmakefile}, @file{makefile}, and @file{Makefile}, in
8109 that order, and use the first of these three which exists or can be made
8110 (@pxref{Makefiles, ,Writing Makefiles}).@refill
8111
8112 @node Goals, Instead of Execution, Makefile Arguments, Running
8113 @section Arguments to Specify the Goals
8114 @cindex goal, how to specify
8115
8116 The @dfn{goals} are the targets that @code{make} should strive ultimately
8117 to update.  Other targets are updated as well if they appear as
8118 prerequisites of goals, or prerequisites of prerequisites of goals, etc.
8119
8120 By default, the goal is the first target in the makefile (not counting
8121 targets that start with a period).  Therefore, makefiles are usually
8122 written so that the first target is for compiling the entire program or
8123 programs they describe.  If the first rule in the makefile has several
8124 targets, only the first target in the rule becomes the default goal, not
8125 the whole list.  You can manage the selection of the default goal from
8126 within your makefile using the @code{.DEFAULT_GOAL} variable
8127 (@pxref{Special Variables, , Other Special Variables}).
8128
8129 You can also specify a different goal or goals with command line
8130 arguments to @code{make}.  Use the name of the goal as an argument.
8131 If you specify several goals, @code{make} processes each of them in
8132 turn, in the order you name them.
8133
8134 Any target in the makefile may be specified as a goal (unless it
8135 starts with @samp{-} or contains an @samp{=}, in which case it will be
8136 parsed as a switch or variable definition, respectively).  Even
8137 targets not in the makefile may be specified, if @code{make} can find
8138 implicit rules that say how to make them.
8139
8140 @vindex MAKECMDGOALS
8141 @code{Make} will set the special variable @code{MAKECMDGOALS} to the
8142 list of goals you specified on the command line.  If no goals were given
8143 on the command line, this variable is empty.  Note that this variable
8144 should be used only in special circumstances.
8145
8146 An example of appropriate use is to avoid including @file{.d} files
8147 during @code{clean} rules (@pxref{Automatic Prerequisites}), so
8148 @code{make} won't create them only to immediately remove them
8149 again:@refill
8150
8151 @example
8152 @group
8153 sources = foo.c bar.c
8154
8155 ifneq ($(MAKECMDGOALS),clean)
8156 include $(sources:.c=.d)
8157 endif
8158 @end group
8159 @end example
8160
8161 One use of specifying a goal is if you want to compile only a part of
8162 the program, or only one of several programs.  Specify as a goal each
8163 file that you wish to remake.  For example, consider a directory containing
8164 several programs, with a makefile that starts like this:
8165
8166 @example
8167 .PHONY: all
8168 all: size nm ld ar as
8169 @end example
8170
8171 If you are working on the program @code{size}, you might want to say
8172 @w{@samp{make size}} so that only the files of that program are recompiled.
8173
8174 Another use of specifying a goal is to make files that are not normally
8175 made.  For example, there may be a file of debugging output, or a
8176 version of the program that is compiled specially for testing, which has
8177 a rule in the makefile but is not a prerequisite of the default goal.
8178
8179 Another use of specifying a goal is to run the recipe associated with
8180 a phony target (@pxref{Phony Targets}) or empty target (@pxref{Empty
8181 Targets, ,Empty Target Files to Record Events}).  Many makefiles contain
8182 a phony target named @file{clean} which deletes everything except source
8183 files.  Naturally, this is done only if you request it explicitly with
8184 @w{@samp{make clean}}.  Following is a list of typical phony and empty
8185 target names.  @xref{Standard Targets}, for a detailed list of all the
8186 standard target names which GNU software packages use.
8187
8188 @table @file
8189 @item all
8190 @cindex @code{all} @r{(standard target)}
8191 Make all the top-level targets the makefile knows about.
8192
8193 @item clean
8194 @cindex @code{clean} @r{(standard target)}
8195 Delete all files that are normally created by running @code{make}.
8196
8197 @item mostlyclean
8198 @cindex @code{mostlyclean} @r{(standard target)}
8199 Like @samp{clean}, but may refrain from deleting a few files that people
8200 normally don't want to recompile.  For example, the @samp{mostlyclean}
8201 target for GCC does not delete @file{libgcc.a}, because recompiling it
8202 is rarely necessary and takes a lot of time.
8203
8204 @item distclean
8205 @cindex @code{distclean} @r{(standard target)}
8206 @itemx realclean
8207 @cindex @code{realclean} @r{(standard target)}
8208 @itemx clobber
8209 @cindex @code{clobber} @r{(standard target)}
8210 Any of these targets might be defined to delete @emph{more} files than
8211 @samp{clean} does.  For example, this would delete configuration files
8212 or links that you would normally create as preparation for compilation,
8213 even if the makefile itself cannot create these files.
8214
8215 @item install
8216 @cindex @code{install} @r{(standard target)}
8217 Copy the executable file into a directory that users typically search
8218 for commands; copy any auxiliary files that the executable uses into
8219 the directories where it will look for them.
8220
8221 @item print
8222 @cindex @code{print} @r{(standard target)}
8223 Print listings of the source files that have changed.
8224
8225 @item tar
8226 @cindex @code{tar} @r{(standard target)}
8227 Create a tar file of the source files.
8228
8229 @item shar
8230 @cindex @code{shar} @r{(standard target)}
8231 Create a shell archive (shar file) of the source files.
8232
8233 @item dist
8234 @cindex @code{dist} @r{(standard target)}
8235 Create a distribution file of the source files.  This might
8236 be a tar file, or a shar file, or a compressed version of one of the
8237 above, or even more than one of the above.
8238
8239 @item TAGS
8240 @cindex @code{TAGS} @r{(standard target)}
8241 Update a tags table for this program.
8242
8243 @item check
8244 @cindex @code{check} @r{(standard target)}
8245 @itemx test
8246 @cindex @code{test} @r{(standard target)}
8247 Perform self tests on the program this makefile builds.
8248 @end table
8249
8250 @node Instead of Execution, Avoiding Compilation, Goals, Running
8251 @section Instead of Executing Recipes
8252 @cindex execution, instead of
8253 @cindex recipes, instead of executing
8254
8255 The makefile tells @code{make} how to tell whether a target is up to date,
8256 and how to update each target.  But updating the targets is not always
8257 what you want.  Certain options specify other activities for @code{make}.
8258
8259 @comment Extra blank lines make it print better.
8260 @table @samp
8261 @item -n
8262 @itemx --just-print
8263 @itemx --dry-run
8264 @itemx --recon
8265 @cindex @code{--just-print}
8266 @cindex @code{--dry-run}
8267 @cindex @code{--recon}
8268 @cindex @code{-n}
8269
8270 ``No-op''.  Causes @code{make} to print the recipes that are needed to
8271 make the targets up to date, but not actually execute them.  Note that
8272 some recipes are still executed, even with this flag (@pxref{MAKE
8273 Variable, ,How the @code{MAKE} Variable Works}).  Also any recipes
8274 needed to update included makefiles are still executed
8275 (@pxref{Remaking Makefiles, ,How Makefiles Are Remade}).
8276
8277 @item -t
8278 @itemx --touch
8279 @cindex @code{--touch}
8280 @cindex touching files
8281 @cindex target, touching
8282 @cindex @code{-t}
8283
8284 ``Touch''.  Marks targets as up to date without actually changing
8285 them.  In other words, @code{make} pretends to update the targets but
8286 does not really change their contents; instead only their modified
8287 times are updated.
8288
8289 @item -q
8290 @itemx --question
8291 @cindex @code{--question}
8292 @cindex @code{-q}
8293 @cindex question mode
8294
8295 ``Question''.  Silently check whether the targets are up to date, but
8296 do not execute recipes; the exit code shows whether any updates are
8297 needed.
8298
8299 @item -W @var{file}
8300 @itemx --what-if=@var{file}
8301 @itemx --assume-new=@var{file}
8302 @itemx --new-file=@var{file}
8303 @cindex @code{--what-if}
8304 @cindex @code{-W}
8305 @cindex @code{--assume-new}
8306 @cindex @code{--new-file}
8307 @cindex what if
8308 @cindex files, assuming new
8309
8310 ``What if''.  Each @samp{-W} flag is followed by a file name.  The given
8311 files' modification times are recorded by @code{make} as being the present
8312 time, although the actual modification times remain the same.
8313 You can use the @samp{-W} flag in conjunction with the @samp{-n} flag
8314 to see what would happen if you were to modify specific files.@refill
8315 @end table
8316
8317 With the @samp{-n} flag, @code{make} prints the recipe that it would
8318 normally execute but usually does not execute it.
8319
8320 With the @samp{-t} flag, @code{make} ignores the recipes in the rules
8321 and uses (in effect) the command @code{touch} for each target that needs to
8322 be remade.  The @code{touch} command is also printed, unless @samp{-s} or
8323 @code{.SILENT} is used.  For speed, @code{make} does not actually invoke
8324 the program @code{touch}.  It does the work directly.
8325
8326 With the @samp{-q} flag, @code{make} prints nothing and executes no
8327 recipes, but the exit status code it returns is zero if and only if the
8328 targets to be considered are already up to date.  If the exit status is
8329 one, then some updating needs to be done.  If @code{make} encounters an
8330 error, the exit status is two, so you can distinguish an error from a
8331 target that is not up to date.
8332
8333 It is an error to use more than one of these three flags in the same
8334 invocation of @code{make}.
8335
8336 @cindex +, and recipe execution
8337 The @samp{-n}, @samp{-t}, and @samp{-q} options do not affect recipe
8338 lines that begin with @samp{+} characters or contain the strings
8339 @samp{$(MAKE)} or @samp{$@{MAKE@}}.  Note that only the line containing
8340 the @samp{+} character or the strings @samp{$(MAKE)} or @samp{$@{MAKE@}}
8341 is run regardless of these options.  Other lines in the same rule are
8342 not run unless they too begin with @samp{+} or contain @samp{$(MAKE)} or
8343 @samp{$@{MAKE@}} (@xref{MAKE Variable, ,How the @code{MAKE} Variable Works}.)
8344
8345 @cindex phony targets and recipe execution
8346 The @samp{-t} flag prevents phony targets (@pxref{Phony Targets}) from
8347 being updated, unless there are recipe lines beginning with @samp{+}
8348 or containing @samp{$(MAKE)} or @samp{$@{MAKE@}}.
8349
8350 The @samp{-W} flag provides two features:
8351
8352 @itemize @bullet
8353 @item
8354 If you also use the @samp{-n} or @samp{-q} flag, you can see what
8355 @code{make} would do if you were to modify some files.
8356
8357 @item
8358 Without the @samp{-n} or @samp{-q} flag, when @code{make} is actually
8359 executing recipes, the @samp{-W} flag can direct @code{make} to act as
8360 if some files had been modified, without actually running the recipes
8361 for those files.@refill
8362 @end itemize
8363
8364 Note that the options @samp{-p} and @samp{-v} allow you to obtain other
8365 information about @code{make} or about the makefiles in use
8366 (@pxref{Options Summary, ,Summary of Options}).@refill
8367
8368 @node Avoiding Compilation, Overriding, Instead of Execution, Running
8369 @section Avoiding Recompilation of Some Files
8370 @cindex @code{-o}
8371 @cindex @code{--old-file}
8372 @cindex @code{--assume-old}
8373 @cindex files, assuming old
8374 @cindex files, avoiding recompilation of
8375 @cindex recompilation, avoiding
8376
8377 Sometimes you may have changed a source file but you do not want to
8378 recompile all the files that depend on it.  For example, suppose you add
8379 a macro or a declaration to a header file that many other files depend
8380 on.  Being conservative, @code{make} assumes that any change in the
8381 header file requires recompilation of all dependent files, but you know
8382 that they do not need to be recompiled and you would rather not waste
8383 the time waiting for them to compile.
8384
8385 If you anticipate the problem before changing the header file, you can
8386 use the @samp{-t} flag.  This flag tells @code{make} not to run the
8387 recipes in the rules, but rather to mark the target up to date by
8388 changing its last-modification date.  You would follow this procedure:
8389
8390 @enumerate
8391 @item
8392 Use the command @samp{make} to recompile the source files that really
8393 need recompilation, ensuring that the object files are up-to-date
8394 before you begin.
8395
8396 @item
8397 Make the changes in the header files.
8398
8399 @item
8400 Use the command @samp{make -t} to mark all the object files as
8401 up to date.  The next time you run @code{make}, the changes in the
8402 header files will not cause any recompilation.
8403 @end enumerate
8404
8405 If you have already changed the header file at a time when some files
8406 do need recompilation, it is too late to do this.  Instead, you can
8407 use the @w{@samp{-o @var{file}}} flag, which marks a specified file as
8408 ``old'' (@pxref{Options Summary, ,Summary of Options}).  This means
8409 that the file itself will not be remade, and nothing else will be
8410 remade on its account.  Follow this procedure:
8411
8412 @enumerate
8413 @item
8414 Recompile the source files that need compilation for reasons independent
8415 of the particular header file, with @samp{make -o @var{headerfile}}.
8416 If several header files are involved, use a separate @samp{-o} option
8417 for each header file.
8418
8419 @item
8420 Touch all the object files with @samp{make -t}.
8421 @end enumerate
8422
8423 @node Overriding, Testing, Avoiding Compilation, Running
8424 @section Overriding Variables
8425 @cindex overriding variables with arguments
8426 @cindex variables, overriding with arguments
8427 @cindex command line variables
8428 @cindex variables, command line
8429
8430 An argument that contains @samp{=} specifies the value of a variable:
8431 @samp{@var{v}=@var{x}} sets the value of the variable @var{v} to @var{x}.
8432 If you specify a value in this way, all ordinary assignments of the same
8433 variable in the makefile are ignored; we say they have been
8434 @dfn{overridden} by the command line argument.
8435
8436 The most common way to use this facility is to pass extra flags to
8437 compilers.  For example, in a properly written makefile, the variable
8438 @code{CFLAGS} is included in each recipe that runs the C compiler, so a
8439 file @file{foo.c} would be compiled something like this:
8440
8441 @example
8442 cc -c $(CFLAGS) foo.c
8443 @end example
8444
8445 Thus, whatever value you set for @code{CFLAGS} affects each compilation
8446 that occurs.  The makefile probably specifies the usual value for
8447 @code{CFLAGS}, like this:
8448
8449 @example
8450 CFLAGS=-g
8451 @end example
8452
8453 Each time you run @code{make}, you can override this value if you
8454 wish.  For example, if you say @samp{make CFLAGS='-g -O'}, each C
8455 compilation will be done with @samp{cc -c -g -O}.  (This also
8456 illustrates how you can use quoting in the shell to enclose spaces and
8457 other special characters in the value of a variable when you override
8458 it.)
8459
8460 The variable @code{CFLAGS} is only one of many standard variables that
8461 exist just so that you can change them this way.  @xref{Implicit
8462 Variables, , Variables Used by Implicit Rules}, for a complete list.
8463
8464 You can also program the makefile to look at additional variables of your
8465 own, giving the user the ability to control other aspects of how the
8466 makefile works by changing the variables.
8467
8468 When you override a variable with a command line argument, you can
8469 define either a recursively-expanded variable or a simply-expanded
8470 variable.  The examples shown above make a recursively-expanded
8471 variable; to make a simply-expanded variable, write @samp{:=} or
8472 @samp{::=} instead of @samp{=}.  But, unless you want to include a
8473 variable reference or function call in the @emph{value} that you
8474 specify, it makes no difference which kind of variable you create.
8475
8476 There is one way that the makefile can change a variable that you have
8477 overridden.  This is to use the @code{override} directive, which is a line
8478 that looks like this: @samp{override @var{variable} = @var{value}}
8479 (@pxref{Override Directive, ,The @code{override} Directive}).
8480
8481 @node Testing, Options Summary, Overriding, Running
8482 @section Testing the Compilation of a Program
8483 @cindex testing compilation
8484 @cindex compilation, testing
8485
8486 Normally, when an error happens in executing a shell command, @code{make}
8487 gives up immediately, returning a nonzero status.  No further recipes are
8488 executed for any target.  The error implies that the goal cannot be
8489 correctly remade, and @code{make} reports this as soon as it knows.
8490
8491 When you are compiling a program that you have just changed, this is not
8492 what you want.  Instead, you would rather that @code{make} try compiling
8493 every file that can be tried, to show you as many compilation errors
8494 as possible.
8495
8496 @cindex @code{-k}
8497 @cindex @code{--keep-going}
8498 On these occasions, you should use the @samp{-k} or
8499 @samp{--keep-going} flag.  This tells @code{make} to continue to
8500 consider the other prerequisites of the pending targets, remaking them
8501 if necessary, before it gives up and returns nonzero status.  For
8502 example, after an error in compiling one object file, @samp{make -k}
8503 will continue compiling other object files even though it already
8504 knows that linking them will be impossible.  In addition to continuing
8505 after failed shell commands, @samp{make -k} will continue as much as
8506 possible after discovering that it does not know how to make a target
8507 or prerequisite file.  This will always cause an error message, but
8508 without @samp{-k}, it is a fatal error (@pxref{Options Summary,
8509 ,Summary of Options}).@refill
8510
8511 The usual behavior of @code{make} assumes that your purpose is to get the
8512 goals up to date; once @code{make} learns that this is impossible, it might
8513 as well report the failure immediately.  The @samp{-k} flag says that the
8514 real purpose is to test as much as possible of the changes made in the
8515 program, perhaps to find several independent problems so that you can
8516 correct them all before the next attempt to compile.  This is why Emacs'
8517 @kbd{M-x compile} command passes the @samp{-k} flag by default.
8518
8519 @node Options Summary,  , Testing, Running
8520 @section Summary of Options
8521 @cindex options
8522 @cindex flags
8523 @cindex switches
8524
8525 Here is a table of all the options @code{make} understands:
8526
8527 @table @samp
8528 @item -b
8529 @cindex @code{-b}
8530 @itemx -m
8531 @cindex @code{-m}
8532 These options are ignored for compatibility with other versions of @code{make}.
8533
8534 @item -B
8535 @cindex @code{-B}
8536 @itemx --always-make
8537 @cindex @code{--always-make}
8538 Consider all targets out-of-date.  GNU @code{make} proceeds to
8539 consider targets and their prerequisites using the normal algorithms;
8540 however, all targets so considered are always remade regardless of the
8541 status of their prerequisites.  To avoid infinite recursion, if
8542 @code{MAKE_RESTARTS} (@pxref{Special Variables, , Other Special
8543 Variables}) is set to a number greater than 0 this option is disabled
8544 when considering whether to remake makefiles (@pxref{Remaking
8545 Makefiles, , How Makefiles Are Remade}).
8546
8547 @item -C @var{dir}
8548 @cindex @code{-C}
8549 @itemx --directory=@var{dir}
8550 @cindex @code{--directory}
8551 Change to directory @var{dir} before reading the makefiles.  If multiple
8552 @samp{-C} options are specified, each is interpreted relative to the
8553 previous one: @samp{-C / -C etc} is equivalent to @samp{-C /etc}.
8554 This is typically used with recursive invocations of @code{make}
8555 (@pxref{Recursion, ,Recursive Use of @code{make}}).
8556
8557 @item -d
8558 @cindex @code{-d}
8559 @c Extra blank line here makes the table look better.
8560
8561 Print debugging information in addition to normal processing.  The
8562 debugging information says which files are being considered for
8563 remaking, which file-times are being compared and with what results,
8564 which files actually need to be remade, which implicit rules are
8565 considered and which are applied---everything interesting about how
8566 @code{make} decides what to do.  The @code{-d} option is equivalent to
8567 @samp{--debug=a} (see below).
8568
8569 @item --debug[=@var{options}]
8570 @cindex @code{--debug}
8571 @c Extra blank line here makes the table look better.
8572
8573 Print debugging information in addition to normal processing.  Various
8574 levels and types of output can be chosen.  With no arguments, print the
8575 ``basic'' level of debugging.  Possible arguments are below; only the
8576 first character is considered, and values must be comma- or
8577 space-separated.
8578
8579 @table @code
8580 @item a (@i{all})
8581 All types of debugging output are enabled.  This is equivalent to using
8582 @samp{-d}.
8583
8584 @item b (@i{basic})
8585 Basic debugging prints each target that was found to be out-of-date, and
8586 whether the build was successful or not.
8587
8588 @item v (@i{verbose})
8589 A level above @samp{basic}; includes messages about which makefiles were
8590 parsed, prerequisites that did not need to be rebuilt, etc.  This option
8591 also enables @samp{basic} messages.
8592
8593 @item i (@i{implicit})
8594 Prints messages describing the implicit rule searches for each target.
8595 This option also enables @samp{basic} messages.
8596
8597 @item j (@i{jobs})
8598 Prints messages giving details on the invocation of specific sub-commands.
8599
8600 @item m (@i{makefile})
8601 By default, the above messages are not enabled while trying to remake
8602 the makefiles.  This option enables messages while rebuilding makefiles,
8603 too.  Note that the @samp{all} option does enable this option.  This
8604 option also enables @samp{basic} messages.
8605
8606 @item n (@i{none})
8607 Disable all debugging currently enabled.  If additional debugging
8608 flags are encountered after this they will still take effect.
8609 @end table
8610
8611 @item -e
8612 @cindex @code{-e}
8613 @itemx --environment-overrides
8614 @cindex @code{--environment-overrides}
8615 Give variables taken from the environment precedence
8616 over variables from makefiles.
8617 @xref{Environment, ,Variables from the Environment}.
8618
8619 @item --eval=@var{string}
8620 @cindex @code{--eval}
8621 @c Extra blank line here makes the table look better.
8622
8623 Evaluate @var{string} as makefile syntax.  This is a command-line
8624 version of the @code{eval} function (@pxref{Eval Function}).  The
8625 evaluation is performed after the default rules and variables have
8626 been defined, but before any makefiles are read.
8627
8628 @item -f @var{file}
8629 @cindex @code{-f}
8630 @itemx --file=@var{file}
8631 @cindex @code{--file}
8632 @itemx --makefile=@var{file}
8633 @cindex @code{--makefile}
8634 Read the file named @var{file} as a makefile.
8635 @xref{Makefiles, ,Writing Makefiles}.
8636
8637 @item -h
8638 @cindex @code{-h}
8639 @itemx --help
8640 @cindex @code{--help}
8641 @c Extra blank line here makes the table look better.
8642
8643 Remind you of the options that @code{make} understands and then exit.
8644
8645 @item -i
8646 @cindex @code{-i}
8647 @itemx --ignore-errors
8648 @cindex @code{--ignore-errors}
8649 Ignore all errors in recipes executed to remake files.
8650 @xref{Errors, ,Errors in Recipes}.
8651
8652 @item -I @var{dir}
8653 @cindex @code{-I}
8654 @itemx --include-dir=@var{dir}
8655 @cindex @code{--include-dir}
8656 Specifies a directory @var{dir} to search for included makefiles.
8657 @xref{Include, ,Including Other Makefiles}.  If several @samp{-I}
8658 options are used to specify several directories, the directories are
8659 searched in the order specified.
8660
8661 @item -j [@var{jobs}]
8662 @cindex @code{-j}
8663 @itemx --jobs[=@var{jobs}]
8664 @cindex @code{--jobs}
8665 Specifies the number of recipes (jobs) to run simultaneously.  With no
8666 argument, @code{make} runs as many recipes simultaneously as possible.
8667 If there is more than one @samp{-j} option, the last one is effective.
8668 @xref{Parallel, ,Parallel Execution}, for more information on how
8669 recipes are run.  Note that this option is ignored on MS-DOS.
8670
8671 @item -k
8672 @cindex @code{-k}
8673 @itemx --keep-going
8674 @cindex @code{--keep-going}
8675 Continue as much as possible after an error.  While the target that
8676 failed, and those that depend on it, cannot be remade, the other
8677 prerequisites of these targets can be processed all the same.
8678 @xref{Testing, ,Testing the Compilation of a Program}.
8679
8680 @item -l [@var{load}]
8681 @cindex @code{-l}
8682 @itemx --load-average[=@var{load}]
8683 @cindex @code{--load-average}
8684 @itemx --max-load[=@var{load}]
8685 @cindex @code{--max-load}
8686 Specifies that no new recipes should be started if there are other
8687 recipes running and the load average is at least @var{load} (a
8688 floating-point number).  With no argument, removes a previous load
8689 limit.  @xref{Parallel, ,Parallel Execution}.
8690
8691 @item -L
8692 @cindex @code{-L}
8693 @itemx --check-symlink-times
8694 @cindex @code{--check-symlink-times}
8695 On systems that support symbolic links, this option causes @code{make}
8696 to consider the timestamps on any symbolic links in addition to the
8697 timestamp on the file referenced by those links.  When this option is
8698 provided, the most recent timestamp among the file and the symbolic
8699 links is taken as the modification time for this target file.
8700
8701 @item -n
8702 @cindex @code{-n}
8703 @itemx --just-print
8704 @cindex @code{--just-print}
8705 @itemx --dry-run
8706 @cindex @code{--dry-run}
8707 @itemx --recon
8708 @cindex @code{--recon}
8709 @c Extra blank line here makes the table look better.
8710
8711 Print the recipe that would be executed, but do not execute it (except
8712 in certain circumstances).
8713 @xref{Instead of Execution, ,Instead of Executing Recipes}.
8714
8715 @item -o @var{file}
8716 @cindex @code{-o}
8717 @itemx --old-file=@var{file}
8718 @cindex @code{--old-file}
8719 @itemx --assume-old=@var{file}
8720 @cindex @code{--assume-old}
8721 Do not remake the file @var{file} even if it is older than its
8722 prerequisites, and do not remake anything on account of changes in
8723 @var{file}.  Essentially the file is treated as very old and its rules
8724 are ignored.  @xref{Avoiding Compilation, ,Avoiding Recompilation of
8725 Some Files}.@refill
8726
8727 @item -O[@var{type}]
8728 @cindex @code{-O}
8729 @itemx --output-sync[=@var{type}]
8730 @cindex @code{--output-sync}
8731 @cindex output during parallel execution
8732 @cindex parallel execution, output during
8733 Ensure that the complete output from each recipe is printed in one
8734 uninterrupted sequence.  This option is only useful when using the
8735 @code{--jobs} option to run multiple recipes simultaneously
8736 (@pxref{Parallel, ,Parallel Execution})  Without this option output
8737 will be displayed as it is generated by the recipes.@refill
8738
8739 With no type or the type @samp{target}, output from the entire recipe
8740 of each target is grouped together.  With the type @samp{line}, output
8741 from each line in the recipe is grouped together.  With the type
8742 @samp{recurse}, the output from an entire recursive make is grouped
8743 together.  With the type @samp{none}, no output synchronization is
8744 performed.  @xref{Parallel Output, ,Output During Parallel Execution}.
8745
8746 @item -p
8747 @cindex @code{-p}
8748 @itemx --print-data-base
8749 @cindex @code{--print-data-base}
8750 @cindex data base of @code{make} rules
8751 @cindex predefined rules and variables, printing
8752 Print the data base (rules and variable values) that results from
8753 reading the makefiles; then execute as usual or as otherwise
8754 specified.  This also prints the version information given by the
8755 @samp{-v} switch (see below).  To print the data base without trying
8756 to remake any files, use @w{@samp{make -qp}}.  To print the data base
8757 of predefined rules and variables, use @w{@samp{make -p -f /dev/null}}.
8758 The data base output contains file name and line number information for
8759 recipe and variable definitions, so it can be a useful debugging tool
8760 in complex environments.
8761
8762 @item -q
8763 @cindex @code{-q}
8764 @itemx --question
8765 @cindex @code{--question}
8766 ``Question mode''.  Do not run any recipes, or print anything; just
8767 return an exit status that is zero if the specified targets are already
8768 up to date, one if any remaking is required, or two if an error is
8769 encountered.  @xref{Instead of Execution, ,Instead of Executing
8770 Recipes}.@refill
8771
8772 @item -r
8773 @cindex @code{-r}
8774 @itemx --no-builtin-rules
8775 @cindex @code{--no-builtin-rules}
8776 Eliminate use of the built-in implicit rules (@pxref{Implicit Rules,
8777 ,Using Implicit Rules}).  You can still define your own by writing
8778 pattern rules (@pxref{Pattern Rules, ,Defining and Redefining Pattern
8779 Rules}).  The @samp{-r} option also clears out the default list of
8780 suffixes for suffix rules (@pxref{Suffix Rules, ,Old-Fashioned Suffix
8781 Rules}).  But you can still define your own suffixes with a rule for
8782 @code{.SUFFIXES}, and then define your own suffix rules.  Note that only
8783 @emph{rules} are affected by the @code{-r} option; default variables
8784 remain in effect (@pxref{Implicit Variables, ,Variables Used by Implicit
8785 Rules}); see the @samp{-R} option below.
8786
8787 @item -R
8788 @cindex @code{-R}
8789 @itemx --no-builtin-variables
8790 @cindex @code{--no-builtin-variables}
8791 Eliminate use of the built-in rule-specific variables (@pxref{Implicit
8792 Variables, ,Variables Used by Implicit Rules}).  You can still define
8793 your own, of course.  The @samp{-R} option also automatically enables
8794 the @samp{-r} option (see above), since it doesn't make sense to have
8795 implicit rules without any definitions for the variables that they use.
8796
8797 @item -s
8798 @cindex @code{-s}
8799 @itemx --silent
8800 @cindex @code{--silent}
8801 @itemx --quiet
8802 @cindex @code{--quiet}
8803 @c Extra blank line here makes the table look better.
8804
8805 Silent operation; do not print the recipes as they are executed.
8806 @xref{Echoing, ,Recipe Echoing}.
8807
8808 @item -S
8809 @cindex @code{-S}
8810 @itemx --no-keep-going
8811 @cindex @code{--no-keep-going}
8812 @itemx --stop
8813 @cindex @code{--stop}
8814 @c Extra blank line here makes the table look better.
8815
8816 Cancel the effect of the @samp{-k} option.  This is never necessary
8817 except in a recursive @code{make} where @samp{-k} might be inherited
8818 from the top-level @code{make} via @code{MAKEFLAGS}
8819 (@pxref{Recursion, ,Recursive Use of @code{make}})
8820 or if you set @samp{-k} in @code{MAKEFLAGS} in your environment.@refill
8821
8822 @item -t
8823 @cindex @code{-t}
8824 @itemx --touch
8825 @cindex @code{--touch}
8826 @c Extra blank line here makes the table look better.
8827
8828 Touch files (mark them up to date without really changing them)
8829 instead of running their recipes.  This is used to pretend that the
8830 recipes were done, in order to fool future invocations of
8831 @code{make}.  @xref{Instead of Execution, ,Instead of Executing Recipes}.
8832
8833 @item --trace
8834 @cindex @code{--trace}
8835 Show tracing information for @code{make} execution.  Prints the entire
8836 recipe to be executed, even for recipes that are normally silent (due
8837 to @code{.SILENT} or @samp{@@}).  Also prints the makefile name and
8838 line number where the recipe was defined, and information on why the
8839 target is being rebuilt.
8840
8841 @item -v
8842 @cindex @code{-v}
8843 @itemx --version
8844 @cindex @code{--version}
8845 Print the version of the @code{make} program plus a copyright, a list
8846 of authors, and a notice that there is no warranty; then exit.
8847
8848 @item -w
8849 @cindex @code{-w}
8850 @itemx --print-directory
8851 @cindex @code{--print-directory}
8852 Print a message containing the working directory both before and after
8853 executing the makefile.  This may be useful for tracking down errors
8854 from complicated nests of recursive @code{make} commands.
8855 @xref{Recursion, ,Recursive Use of @code{make}}.  (In practice, you
8856 rarely need to specify this option since @samp{make} does it for you;
8857 see @ref{-w Option, ,The @samp{--print-directory} Option}.)
8858
8859 @item --no-print-directory
8860 @cindex @code{--no-print-directory}
8861 Disable printing of the working directory under @code{-w}.
8862 This option is useful when @code{-w} is turned on automatically,
8863 but you do not want to see the extra messages.
8864 @xref{-w Option, ,The @samp{--print-directory} Option}.
8865
8866 @item -W @var{file}
8867 @cindex @code{-W}
8868 @itemx --what-if=@var{file}
8869 @cindex @code{--what-if}
8870 @itemx --new-file=@var{file}
8871 @cindex @code{--new-file}
8872 @itemx --assume-new=@var{file}
8873 @cindex @code{--assume-new}
8874 Pretend that the target @var{file} has just been modified.  When used
8875 with the @samp{-n} flag, this shows you what would happen if you were
8876 to modify that file.  Without @samp{-n}, it is almost the same as
8877 running a @code{touch} command on the given file before running
8878 @code{make}, except that the modification time is changed only in the
8879 imagination of @code{make}.
8880 @xref{Instead of Execution, ,Instead of Executing Recipes}.
8881
8882 @item --warn-undefined-variables
8883 @cindex @code{--warn-undefined-variables}
8884 @cindex variables, warning for undefined
8885 @cindex undefined variables, warning message
8886 Issue a warning message whenever @code{make} sees a reference to an
8887 undefined variable.  This can be helpful when you are trying to debug
8888 makefiles which use variables in complex ways.
8889 @end table
8890
8891 @node Implicit Rules, Archives, Running, Top
8892 @chapter Using Implicit Rules
8893 @cindex implicit rule
8894 @cindex rule, implicit
8895
8896 Certain standard ways of remaking target files are used very often.  For
8897 example, one customary way to make an object file is from a C source file
8898 using the C compiler, @code{cc}.
8899
8900 @dfn{Implicit rules} tell @code{make} how to use customary techniques so
8901 that you do not have to specify them in detail when you want to use
8902 them.  For example, there is an implicit rule for C compilation.  File
8903 names determine which implicit rules are run.  For example, C
8904 compilation typically takes a @file{.c} file and makes a @file{.o} file.
8905 So @code{make} applies the implicit rule for C compilation when it sees
8906 this combination of file name endings.@refill
8907
8908 A chain of implicit rules can apply in sequence; for example, @code{make}
8909 will remake a @file{.o} file from a @file{.y} file by way of a @file{.c} file.
8910 @iftex
8911 @xref{Chained Rules, ,Chains of Implicit Rules}.
8912 @end iftex
8913
8914 The built-in implicit rules use several variables in their recipes so
8915 that, by changing the values of the variables, you can change the way the
8916 implicit rule works.  For example, the variable @code{CFLAGS} controls the
8917 flags given to the C compiler by the implicit rule for C compilation.
8918 @iftex
8919 @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
8920 @end iftex
8921
8922 You can define your own implicit rules by writing @dfn{pattern rules}.
8923 @iftex
8924 @xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.
8925 @end iftex
8926
8927 @dfn{Suffix rules} are a more limited way to define implicit rules.
8928 Pattern rules are more general and clearer, but suffix rules are
8929 retained for compatibility.
8930 @iftex
8931 @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
8932 @end iftex
8933
8934 @menu
8935 * Using Implicit::              How to use an existing implicit rule
8936                                   to get the recipes for updating a file.
8937 * Catalogue of Rules::          A list of built-in implicit rules.
8938 * Implicit Variables::          How to change what predefined rules do.
8939 * Chained Rules::               How to use a chain of implicit rules.
8940 * Pattern Rules::               How to define new implicit rules.
8941 * Last Resort::                 How to define a recipe for rules which
8942                                   cannot find any.
8943 * Suffix Rules::                The old-fashioned style of implicit rule.
8944 * Implicit Rule Search::        The precise algorithm for applying
8945                                   implicit rules.
8946 @end menu
8947
8948 @node Using Implicit, Catalogue of Rules, Implicit Rules, Implicit Rules
8949 @section Using Implicit Rules
8950 @cindex implicit rule, how to use
8951 @cindex rule, implicit, how to use
8952
8953 To allow @code{make} to find a customary method for updating a target
8954 file, all you have to do is refrain from specifying recipes yourself.
8955 Either write a rule with no recipe, or don't write a rule at all.
8956 Then @code{make} will figure out which implicit rule to use based on
8957 which kind of source file exists or can be made.
8958
8959 For example, suppose the makefile looks like this:
8960
8961 @example
8962 foo : foo.o bar.o
8963         cc -o foo foo.o bar.o $(CFLAGS) $(LDFLAGS)
8964 @end example
8965
8966 @noindent
8967 Because you mention @file{foo.o} but do not give a rule for it, @code{make}
8968 will automatically look for an implicit rule that tells how to update it.
8969 This happens whether or not the file @file{foo.o} currently exists.
8970
8971 If an implicit rule is found, it can supply both a recipe and one or
8972 more prerequisites (the source files).  You would want to write a rule
8973 for @file{foo.o} with no recipe if you need to specify additional
8974 prerequisites, such as header files, that the implicit rule cannot
8975 supply.
8976
8977 Each implicit rule has a target pattern and prerequisite patterns.  There may
8978 be many implicit rules with the same target pattern.  For example, numerous
8979 rules make @samp{.o} files: one, from a @samp{.c} file with the C compiler;
8980 another, from a @samp{.p} file with the Pascal compiler; and so on.  The rule
8981 that actually applies is the one whose prerequisites exist or can be made.
8982 So, if you have a file @file{foo.c}, @code{make} will run the C compiler;
8983 otherwise, if you have a file @file{foo.p}, @code{make} will run the Pascal
8984 compiler; and so on.
8985
8986 Of course, when you write the makefile, you know which implicit rule you
8987 want @code{make} to use, and you know it will choose that one because you
8988 know which possible prerequisite files are supposed to exist.
8989 @xref{Catalogue of Rules, ,Catalogue of Implicit Rules},
8990 for a catalogue of all the predefined implicit rules.
8991
8992 Above, we said an implicit rule applies if the required prerequisites ``exist
8993 or can be made''.  A file ``can be made'' if it is mentioned explicitly in
8994 the makefile as a target or a prerequisite, or if an implicit rule can be
8995 recursively found for how to make it.  When an implicit prerequisite is the
8996 result of another implicit rule, we say that @dfn{chaining} is occurring.
8997 @xref{Chained Rules, ,Chains of Implicit Rules}.
8998
8999 In general, @code{make} searches for an implicit rule for each target, and
9000 for each double-colon rule, that has no recipe.  A file that is mentioned
9001 only as a prerequisite is considered a target whose rule specifies nothing,
9002 so implicit rule search happens for it.  @xref{Implicit Rule Search, ,Implicit Rule Search Algorithm}, for the
9003 details of how the search is done.
9004
9005 Note that explicit prerequisites do not influence implicit rule search.
9006 For example, consider this explicit rule:
9007
9008 @example
9009 foo.o: foo.p
9010 @end example
9011
9012 @noindent
9013 The prerequisite on @file{foo.p} does not necessarily mean that
9014 @code{make} will remake @file{foo.o} according to the implicit rule to
9015 make an object file, a @file{.o} file, from a Pascal source file, a
9016 @file{.p} file.  For example, if @file{foo.c} also exists, the implicit
9017 rule to make an object file from a C source file is used instead,
9018 because it appears before the Pascal rule in the list of predefined
9019 implicit rules (@pxref{Catalogue of Rules, , Catalogue of Implicit
9020 Rules}).
9021
9022 If you do not want an implicit rule to be used for a target that has no
9023 recipe, you can give that target an empty recipe by writing a semicolon
9024 (@pxref{Empty Recipes, ,Defining Empty Recipes}).
9025
9026 @node Catalogue of Rules, Implicit Variables, Using Implicit, Implicit Rules
9027 @section Catalogue of Implicit Rules
9028 @cindex implicit rule, predefined
9029 @cindex rule, implicit, predefined
9030
9031 Here is a catalogue of predefined implicit rules which are always
9032 available unless the makefile explicitly overrides or cancels them.
9033 @xref{Canceling Rules, ,Canceling Implicit Rules}, for information on
9034 canceling or overriding an implicit rule.  The @samp{-r} or
9035 @samp{--no-builtin-rules} option cancels all predefined rules.
9036
9037 This manual only documents the default rules available on POSIX-based
9038 operating systems.  Other operating systems, such as VMS, Windows,
9039 OS/2, etc. may have different sets of default rules.  To see the full
9040 list of default rules and variables available in your version of GNU
9041 @code{make}, run @samp{make -p} in a directory with no makefile.
9042
9043 Not all of these rules will always be defined, even when the @samp{-r}
9044 option is not given.  Many of the predefined implicit rules are
9045 implemented in @code{make} as suffix rules, so which ones will be
9046 defined depends on the @dfn{suffix list} (the list of prerequisites of
9047 the special target @code{.SUFFIXES}).  The default suffix list is:
9048 @code{.out}, @code{.a}, @code{.ln}, @code{.o}, @code{.c}, @code{.cc},
9049 @code{.C}, @code{.cpp}, @code{.p}, @code{.f}, @code{.F}, @code{.m},
9050 @code{.r}, @code{.y}, @code{.l}, @code{.ym}, @code{.lm}, @code{.s},
9051 @code{.S}, @code{.mod}, @code{.sym}, @code{.def}, @code{.h},
9052 @code{.info}, @code{.dvi}, @code{.tex}, @code{.texinfo}, @code{.texi},
9053 @code{.txinfo}, @code{.w}, @code{.ch} @code{.web}, @code{.sh},
9054 @code{.elc}, @code{.el}.  All of the implicit rules described below
9055 whose prerequisites have one of these suffixes are actually suffix
9056 rules.  If you modify the suffix list, the only predefined suffix
9057 rules in effect will be those named by one or two of the suffixes that
9058 are on the list you specify; rules whose suffixes fail to be on the
9059 list are disabled.  @xref{Suffix Rules, ,Old-Fashioned Suffix Rules},
9060 for full details on suffix rules.
9061
9062 @table @asis
9063 @item Compiling C programs
9064 @cindex C, rule to compile
9065 @pindex cc
9066 @pindex gcc
9067 @pindex .o
9068 @pindex .c
9069 @file{@var{n}.o} is made automatically from @file{@var{n}.c} with
9070 a recipe of the form @samp{$(CC) $(CPPFLAGS) $(CFLAGS) -c}.@refill
9071
9072 @item Compiling C++ programs
9073 @cindex C++, rule to compile
9074 @pindex g++
9075 @pindex .cc
9076 @pindex .cpp
9077 @pindex .C
9078 @file{@var{n}.o} is made automatically from @file{@var{n}.cc},
9079 @file{@var{n}.cpp}, or @file{@var{n}.C} with a recipe of the form
9080 @samp{$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c}.  We encourage you to use the
9081 suffix @samp{.cc} for C++ source files instead of @samp{.C}.@refill
9082
9083 @item Compiling Pascal programs
9084 @cindex Pascal, rule to compile
9085 @pindex pc
9086 @pindex .p
9087 @file{@var{n}.o} is made automatically from @file{@var{n}.p}
9088 with the recipe @samp{$(PC) $(PFLAGS) -c}.@refill
9089
9090 @item Compiling Fortran and Ratfor programs
9091 @cindex Fortran, rule to compile
9092 @cindex Ratfor, rule to compile
9093 @pindex f77
9094 @pindex .f
9095 @pindex .r
9096 @pindex .F
9097 @file{@var{n}.o} is made automatically from @file{@var{n}.r},
9098 @file{@var{n}.F} or @file{@var{n}.f} by running the
9099 Fortran compiler.  The precise recipe used is as follows:@refill
9100
9101 @table @samp
9102 @item .f
9103 @samp{$(FC) $(FFLAGS) -c}.
9104 @item .F
9105 @samp{$(FC) $(FFLAGS) $(CPPFLAGS) -c}.
9106 @item .r
9107 @samp{$(FC) $(FFLAGS) $(RFLAGS) -c}.
9108 @end table
9109
9110 @item Preprocessing Fortran and Ratfor programs
9111 @file{@var{n}.f} is made automatically from @file{@var{n}.r} or
9112 @file{@var{n}.F}.  This rule runs just the preprocessor to convert a
9113 Ratfor or preprocessable Fortran program into a strict Fortran
9114 program.  The precise recipe used is as follows:@refill
9115
9116 @table @samp
9117 @item .F
9118 @samp{$(FC) $(CPPFLAGS) $(FFLAGS) -F}.
9119 @item .r
9120 @samp{$(FC) $(FFLAGS) $(RFLAGS) -F}.
9121 @end table
9122
9123 @item Compiling Modula-2 programs
9124 @cindex Modula-2, rule to compile
9125 @pindex m2c
9126 @pindex .sym
9127 @pindex .def
9128 @pindex .mod
9129 @file{@var{n}.sym} is made from @file{@var{n}.def} with a recipe
9130 of the form @samp{$(M2C) $(M2FLAGS) $(DEFFLAGS)}.  @file{@var{n}.o}
9131 is made from @file{@var{n}.mod}; the form is:
9132 @w{@samp{$(M2C) $(M2FLAGS) $(MODFLAGS)}}.@refill
9133
9134 @need 1200
9135 @item Assembling and preprocessing assembler programs
9136 @cindex assembly, rule to compile
9137 @pindex as
9138 @pindex .s
9139 @file{@var{n}.o} is made automatically from @file{@var{n}.s} by
9140 running the assembler, @code{as}.  The precise recipe is
9141 @samp{$(AS) $(ASFLAGS)}.@refill
9142
9143 @pindex .S
9144 @file{@var{n}.s} is made automatically from @file{@var{n}.S} by
9145 running the C preprocessor, @code{cpp}.  The precise recipe is
9146 @w{@samp{$(CPP) $(CPPFLAGS)}}.
9147
9148 @item Linking a single object file
9149 @cindex linking, predefined rule for
9150 @pindex ld
9151 @pindex .o
9152 @file{@var{n}} is made automatically from @file{@var{n}.o} by running
9153 the linker (usually called @code{ld}) via the C compiler.  The precise
9154 recipe used is @w{@samp{$(CC) $(LDFLAGS) @var{n}.o $(LOADLIBES) $(LDLIBS)}}.
9155
9156 This rule does the right thing for a simple program with only one
9157 source file.  It will also do the right thing if there are multiple
9158 object files (presumably coming from various other source files), one
9159 of which has a name matching that of the executable file.  Thus,
9160
9161 @example
9162 x: y.o z.o
9163 @end example
9164
9165 @noindent
9166 when @file{x.c}, @file{y.c} and @file{z.c} all exist will execute:
9167
9168 @example
9169 @group
9170 cc -c x.c -o x.o
9171 cc -c y.c -o y.o
9172 cc -c z.c -o z.o
9173 cc x.o y.o z.o -o x
9174 rm -f x.o
9175 rm -f y.o
9176 rm -f z.o
9177 @end group
9178 @end example
9179
9180 @noindent
9181 In more complicated cases, such as when there is no object file whose
9182 name derives from the executable file name, you must write an explicit
9183 recipe for linking.
9184
9185 Each kind of file automatically made into @samp{.o} object files will
9186 be automatically linked by using the compiler (@samp{$(CC)},
9187 @samp{$(FC)} or @samp{$(PC)}; the C compiler @samp{$(CC)} is used to
9188 assemble @samp{.s} files) without the @samp{-c} option.  This could be
9189 done by using the @samp{.o} object files as intermediates, but it is
9190 faster to do the compiling and linking in one step, so that's how it's
9191 done.@refill
9192
9193 @item Yacc for C programs
9194 @pindex yacc
9195 @cindex Yacc, rule to run
9196 @pindex .y
9197 @file{@var{n}.c} is made automatically from @file{@var{n}.y} by
9198 running Yacc with the recipe @samp{$(YACC) $(YFLAGS)}.
9199
9200 @item Lex for C programs
9201 @pindex lex
9202 @cindex Lex, rule to run
9203 @pindex .l
9204 @file{@var{n}.c} is made automatically from @file{@var{n}.l} by
9205 running Lex.  The actual recipe is @samp{$(LEX) $(LFLAGS)}.
9206
9207 @item Lex for Ratfor programs
9208 @file{@var{n}.r} is made automatically from @file{@var{n}.l} by
9209 running Lex.  The actual recipe is @samp{$(LEX) $(LFLAGS)}.
9210
9211 The convention of using the same suffix @samp{.l} for all Lex files
9212 regardless of whether they produce C code or Ratfor code makes it
9213 impossible for @code{make} to determine automatically which of the two
9214 languages you are using in any particular case.  If @code{make} is
9215 called upon to remake an object file from a @samp{.l} file, it must
9216 guess which compiler to use.  It will guess the C compiler, because
9217 that is more common.  If you are using Ratfor, make sure @code{make}
9218 knows this by mentioning @file{@var{n}.r} in the makefile.  Or, if you
9219 are using Ratfor exclusively, with no C files, remove @samp{.c} from
9220 the list of implicit rule suffixes with:@refill
9221
9222 @example
9223 @group
9224 .SUFFIXES:
9225 .SUFFIXES: .o .r .f .l @dots{}
9226 @end group
9227 @end example
9228
9229 @item Making Lint Libraries from C, Yacc, or Lex programs
9230 @pindex lint
9231 @cindex @code{lint}, rule to run
9232 @pindex .ln
9233 @file{@var{n}.ln} is made from @file{@var{n}.c} by running @code{lint}.
9234 The precise recipe is @w{@samp{$(LINT) $(LINTFLAGS) $(CPPFLAGS) -i}}.
9235 The same recipe is used on the C code produced from
9236 @file{@var{n}.y} or @file{@var{n}.l}.@refill
9237
9238 @item @TeX{} and Web
9239 @cindex @TeX{}, rule to run
9240 @cindex Web, rule to run
9241 @pindex tex
9242 @pindex cweave
9243 @pindex weave
9244 @pindex tangle
9245 @pindex ctangle
9246 @pindex .dvi
9247 @pindex .tex
9248 @pindex .web
9249 @pindex .w
9250 @pindex .ch
9251 @file{@var{n}.dvi} is made from @file{@var{n}.tex} with the recipe
9252 @samp{$(TEX)}.  @file{@var{n}.tex} is made from @file{@var{n}.web} with
9253 @samp{$(WEAVE)}, or from @file{@var{n}.w} (and from @file{@var{n}.ch} if
9254 it exists or can be made) with @samp{$(CWEAVE)}.  @file{@var{n}.p} is
9255 made from @file{@var{n}.web} with @samp{$(TANGLE)} and @file{@var{n}.c}
9256 is made from @file{@var{n}.w} (and from @file{@var{n}.ch} if it exists
9257 or can be made) with @samp{$(CTANGLE)}.@refill
9258
9259 @item Texinfo and Info
9260 @cindex Texinfo, rule to format
9261 @cindex Info, rule to format
9262 @pindex texi2dvi
9263 @pindex makeinfo
9264 @pindex .texinfo
9265 @pindex .info
9266 @pindex .texi
9267 @pindex .txinfo
9268 @file{@var{n}.dvi} is made from @file{@var{n}.texinfo},
9269 @file{@var{n}.texi}, or @file{@var{n}.txinfo}, with the recipe
9270 @w{@samp{$(TEXI2DVI) $(TEXI2DVI_FLAGS)}}.  @file{@var{n}.info} is made from
9271 @file{@var{n}.texinfo}, @file{@var{n}.texi}, or @file{@var{n}.txinfo}, with
9272 the recipe @w{@samp{$(MAKEINFO) $(MAKEINFO_FLAGS)}}.
9273
9274 @item RCS
9275 @cindex RCS, rule to extract from
9276 @pindex co
9277 @pindex ,v @r{(RCS file extension)}
9278 Any file @file{@var{n}} is extracted if necessary from an RCS file
9279 named either @file{@var{n},v} or @file{RCS/@var{n},v}.  The precise
9280 recipe used is @w{@samp{$(CO) $(COFLAGS)}}.  @file{@var{n}} will not be
9281 extracted from RCS if it already exists, even if the RCS file is
9282 newer.  The rules for RCS are terminal
9283 (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}),
9284 so RCS files cannot be generated from another source; they must
9285 actually exist.@refill
9286
9287 @item SCCS
9288 @cindex SCCS, rule to extract from
9289 @pindex get
9290 @pindex s. @r{(SCCS file prefix)}
9291 Any file @file{@var{n}} is extracted if necessary from an SCCS file
9292 named either @file{s.@var{n}} or @file{SCCS/s.@var{n}}.  The precise
9293 recipe used is @w{@samp{$(GET) $(GFLAGS)}}.  The rules for SCCS are
9294 terminal (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}),
9295 so SCCS files cannot be generated from another source; they must
9296 actually exist.@refill
9297
9298 @pindex .sh
9299 For the benefit of SCCS, a file @file{@var{n}} is copied from
9300 @file{@var{n}.sh} and made executable (by everyone).  This is for
9301 shell scripts that are checked into SCCS.  Since RCS preserves the
9302 execution permission of a file, you do not need to use this feature
9303 with RCS.@refill
9304
9305 We recommend that you avoid using of SCCS.  RCS is widely held to be
9306 superior, and is also free.  By choosing free software in place of
9307 comparable (or inferior) proprietary software, you support the free
9308 software movement.
9309 @end table
9310
9311 Usually, you want to change only the variables listed in the table
9312 above, which are documented in the following section.
9313
9314 However, the recipes in built-in implicit rules actually use
9315 variables such as @code{COMPILE.c}, @code{LINK.p}, and
9316 @code{PREPROCESS.S}, whose values contain the recipes listed above.
9317
9318 @code{make} follows the convention that the rule to compile a
9319 @file{.@var{x}} source file uses the variable @code{COMPILE.@var{x}}.
9320 Similarly, the rule to produce an executable from a @file{.@var{x}}
9321 file uses @code{LINK.@var{x}}; and the rule to preprocess a
9322 @file{.@var{x}} file uses @code{PREPROCESS.@var{x}}.
9323
9324 @vindex OUTPUT_OPTION
9325 Every rule that produces an object file uses the variable
9326 @code{OUTPUT_OPTION}.  @code{make} defines this variable either to
9327 contain @samp{-o $@@}, or to be empty, depending on a compile-time
9328 option.  You need the @samp{-o} option to ensure that the output goes
9329 into the right file when the source file is in a different directory,
9330 as when using @code{VPATH} (@pxref{Directory Search}).  However,
9331 compilers on some systems do not accept a @samp{-o} switch for object
9332 files.  If you use such a system, and use @code{VPATH}, some
9333 compilations will put their output in the wrong place.
9334 A possible workaround for this problem is to give @code{OUTPUT_OPTION}
9335 the value @w{@samp{; mv $*.o $@@}}.
9336
9337 @node Implicit Variables, Chained Rules, Catalogue of Rules, Implicit Rules
9338 @section Variables Used by Implicit Rules
9339 @cindex flags for compilers
9340
9341 The recipes in built-in implicit rules make liberal use of certain
9342 predefined variables.  You can alter the values of these variables in
9343 the makefile, with arguments to @code{make}, or in the environment to
9344 alter how the implicit rules work without redefining the rules
9345 themselves.  You can cancel all variables used by implicit rules with
9346 the @samp{-R} or @samp{--no-builtin-variables} option.
9347
9348 For example, the recipe used to compile a C source file actually says
9349 @samp{$(CC) -c $(CFLAGS) $(CPPFLAGS)}.  The default values of the variables
9350 used are @samp{cc} and nothing, resulting in the command @samp{cc -c}.  By
9351 redefining @samp{CC} to @samp{ncc}, you could cause @samp{ncc} to be
9352 used for all C compilations performed by the implicit rule.  By redefining
9353 @samp{CFLAGS} to be @samp{-g}, you could pass the @samp{-g} option to
9354 each compilation.  @emph{All} implicit rules that do C compilation use
9355 @samp{$(CC)} to get the program name for the compiler and @emph{all}
9356 include @samp{$(CFLAGS)} among the arguments given to the compiler.@refill
9357
9358 The variables used in implicit rules fall into two classes: those that are
9359 names of programs (like @code{CC}) and those that contain arguments for the
9360 programs (like @code{CFLAGS}).  (The ``name of a program'' may also contain
9361 some command arguments, but it must start with an actual executable program
9362 name.)  If a variable value contains more than one argument, separate them
9363 with spaces.
9364
9365 The following tables describe of some of the more commonly-used predefined
9366 variables.  This list is not exhaustive, and the default values shown here may
9367 not be what @code{make} selects for your environment.  To see the
9368 complete list of predefined variables for your instance of GNU @code{make} you
9369 can run @samp{make -p} in a directory with no makefiles.
9370
9371 Here is a table of some of the more common variables used as names of
9372 programs in built-in rules:
9373
9374 @table @code
9375 @item AR
9376 @vindex AR
9377 Archive-maintaining program; default @samp{ar}.
9378 @pindex ar
9379
9380 @item AS
9381 @vindex AS
9382 Program for compiling assembly files; default @samp{as}.
9383 @pindex as
9384
9385 @item CC
9386 @vindex CC
9387 Program for compiling C programs; default @samp{cc}.
9388 @pindex cc
9389
9390 @item CXX
9391 @vindex CXX
9392 Program for compiling C++ programs; default @samp{g++}.
9393 @pindex g++
9394
9395 @item CPP
9396 @vindex CPP
9397 Program for running the C preprocessor, with results to standard output;
9398 default @samp{$(CC) -E}.
9399
9400 @item FC
9401 @vindex FC
9402 Program for compiling or preprocessing Fortran and Ratfor programs;
9403 default @samp{f77}.
9404 @pindex f77
9405
9406 @item M2C
9407 @vindex M2C
9408 Program to use to compile Modula-2 source code; default @samp{m2c}.
9409 @pindex m2c
9410
9411 @item PC
9412 @vindex PC
9413 Program for compiling Pascal programs; default @samp{pc}.
9414 @pindex pc
9415
9416 @item CO
9417 @vindex CO
9418 Program for extracting a file from RCS; default @samp{co}.
9419 @pindex co
9420
9421 @item GET
9422 @vindex GET
9423 Program for extracting a file from SCCS; default @samp{get}.
9424 @pindex get
9425
9426 @item LEX
9427 @vindex LEX
9428 Program to use to turn Lex grammars into source code; default @samp{lex}.
9429 @pindex lex
9430
9431 @item YACC
9432 @vindex YACC
9433 Program to use to turn Yacc grammars into source code; default @samp{yacc}.
9434 @pindex yacc
9435
9436 @item LINT
9437 @vindex LINT
9438 Program to use to run lint on source code; default @samp{lint}.
9439 @pindex lint
9440
9441 @item MAKEINFO
9442 @vindex MAKEINFO
9443 Program to convert a Texinfo source file into an Info file; default
9444 @samp{makeinfo}.
9445 @pindex makeinfo
9446
9447 @item TEX
9448 @vindex TEX
9449 Program to make @TeX{} @sc{dvi} files from @TeX{} source;
9450 default @samp{tex}.
9451 @pindex tex
9452
9453 @item TEXI2DVI
9454 @vindex TEXI2DVI
9455 Program to make @TeX{} @sc{dvi} files from Texinfo source;
9456 default @samp{texi2dvi}.
9457 @pindex texi2dvi
9458
9459 @item WEAVE
9460 @vindex WEAVE
9461 Program to translate Web into @TeX{}; default @samp{weave}.
9462 @pindex weave
9463
9464 @item CWEAVE
9465 @vindex CWEAVE
9466 Program to translate C Web into @TeX{}; default @samp{cweave}.
9467 @pindex cweave
9468
9469 @item TANGLE
9470 @vindex TANGLE
9471 Program to translate Web into Pascal; default @samp{tangle}.
9472 @pindex tangle
9473
9474 @item CTANGLE
9475 @vindex CTANGLE
9476 Program to translate C Web into C; default @samp{ctangle}.
9477 @pindex ctangle
9478
9479 @item RM
9480 @vindex RM
9481 Command to remove a file; default @samp{rm -f}.
9482 @pindex rm
9483 @end table
9484
9485 Here is a table of variables whose values are additional arguments for the
9486 programs above.  The default values for all of these is the empty
9487 string, unless otherwise noted.
9488
9489 @table @code
9490 @item ARFLAGS
9491 @vindex ARFLAGS
9492 Flags to give the archive-maintaining program; default @samp{rv}.
9493
9494 @item ASFLAGS
9495 @vindex ASFLAGS
9496 Extra flags to give to the assembler (when explicitly
9497 invoked on a @samp{.s} or @samp{.S} file).
9498
9499 @item CFLAGS
9500 @vindex CFLAGS
9501 Extra flags to give to the C compiler.
9502
9503 @item CXXFLAGS
9504 @vindex CXXFLAGS
9505 Extra flags to give to the C++ compiler.
9506
9507 @item COFLAGS
9508 @vindex COFLAGS
9509 Extra flags to give to the RCS @code{co} program.
9510
9511 @item CPPFLAGS
9512 @vindex CPPFLAGS
9513 Extra flags to give to the C preprocessor and programs
9514 that use it (the C and Fortran compilers).
9515
9516 @item FFLAGS
9517 @vindex FFLAGS
9518 Extra flags to give to the Fortran compiler.
9519
9520 @item GFLAGS
9521 @vindex GFLAGS
9522 Extra flags to give to the SCCS @code{get} program.
9523
9524 @item LDFLAGS
9525 @vindex LDFLAGS
9526 Extra flags to give to compilers when they are supposed to invoke the linker,
9527 @samp{ld}, such as @code{-L}.  Libraries (@code{-lfoo}) should be
9528 added to the @code{LDLIBS} variable instead.
9529
9530 @item LDLIBS
9531 @vindex LDLIBS
9532 @vindex LOADLIBES
9533 Library flags or names given to compilers when they are supposed to
9534 invoke the linker, @samp{ld}.  @code{LOADLIBES} is a deprecated (but
9535 still supported) alternative to @code{LDLIBS}.  Non-library linker
9536 flags, such as @code{-L}, should go in the @code{LDFLAGS} variable.
9537
9538 @item LFLAGS
9539 @vindex LFLAGS
9540 Extra flags to give to Lex.
9541
9542 @item YFLAGS
9543 @vindex YFLAGS
9544 Extra flags to give to Yacc.
9545
9546 @item PFLAGS
9547 @vindex PFLAGS
9548 Extra flags to give to the Pascal compiler.
9549
9550 @item RFLAGS
9551 @vindex RFLAGS
9552 Extra flags to give to the Fortran compiler for Ratfor programs.
9553
9554 @item LINTFLAGS
9555 @vindex LINTFLAGS
9556 Extra flags to give to lint.
9557 @end table
9558
9559 @node Chained Rules, Pattern Rules, Implicit Variables, Implicit Rules
9560 @section Chains of Implicit Rules
9561
9562 @cindex chains of rules
9563 @cindex rule, implicit, chains of
9564 Sometimes a file can be made by a sequence of implicit rules.  For example,
9565 a file @file{@var{n}.o} could be made from @file{@var{n}.y} by running
9566 first Yacc and then @code{cc}.  Such a sequence is called a @dfn{chain}.
9567
9568 If the file @file{@var{n}.c} exists, or is mentioned in the makefile, no
9569 special searching is required: @code{make} finds that the object file can
9570 be made by C compilation from @file{@var{n}.c}; later on, when considering
9571 how to make @file{@var{n}.c}, the rule for running Yacc is
9572 used.  Ultimately both @file{@var{n}.c} and @file{@var{n}.o} are
9573 updated.@refill
9574
9575 @cindex intermediate files
9576 @cindex files, intermediate
9577 However, even if @file{@var{n}.c} does not exist and is not mentioned,
9578 @code{make} knows how to envision it as the missing link between
9579 @file{@var{n}.o} and @file{@var{n}.y}!  In this case, @file{@var{n}.c} is
9580 called an @dfn{intermediate file}.  Once @code{make} has decided to use the
9581 intermediate file, it is entered in the data base as if it had been
9582 mentioned in the makefile, along with the implicit rule that says how to
9583 create it.@refill
9584
9585 Intermediate files are remade using their rules just like all other
9586 files.  But intermediate files are treated differently in two ways.
9587
9588 The first difference is what happens if the intermediate file does not
9589 exist.  If an ordinary file @var{b} does not exist, and @code{make}
9590 considers a target that depends on @var{b}, it invariably creates
9591 @var{b} and then updates the target from @var{b}.  But if @var{b} is an
9592 intermediate file, then @code{make} can leave well enough alone.  It
9593 won't bother updating @var{b}, or the ultimate target, unless some
9594 prerequisite of @var{b} is newer than that target or there is some other
9595 reason to update that target.
9596
9597 The second difference is that if @code{make} @emph{does} create @var{b}
9598 in order to update something else, it deletes @var{b} later on after it
9599 is no longer needed.  Therefore, an intermediate file which did not
9600 exist before @code{make} also does not exist after @code{make}.
9601 @code{make} reports the deletion to you by printing a @samp{rm -f}
9602 command showing which file it is deleting.
9603
9604 Ordinarily, a file cannot be intermediate if it is mentioned in the
9605 makefile as a target or prerequisite.  However, you can explicitly mark a
9606 file as intermediate by listing it as a prerequisite of the special target
9607 @code{.INTERMEDIATE}.  This takes effect even if the file is mentioned
9608 explicitly in some other way.
9609
9610 @cindex intermediate files, preserving
9611 @cindex preserving intermediate files
9612 @cindex secondary files
9613 You can prevent automatic deletion of an intermediate file by marking it
9614 as a @dfn{secondary} file.  To do this, list it as a prerequisite of the
9615 special target @code{.SECONDARY}.  When a file is secondary, @code{make}
9616 will not create the file merely because it does not already exist, but
9617 @code{make} does not automatically delete the file.  Marking a file as
9618 secondary also marks it as intermediate.
9619
9620 You can list the target pattern of an implicit rule (such as @samp{%.o})
9621 as a prerequisite of the special target @code{.PRECIOUS} to preserve
9622 intermediate files made by implicit rules whose target patterns match
9623 that file's name; see @ref{Interrupts}.@refill
9624 @cindex preserving with @code{.PRECIOUS}
9625 @cindex @code{.PRECIOUS} intermediate files
9626
9627 A chain can involve more than two implicit rules.  For example, it is
9628 possible to make a file @file{foo} from @file{RCS/foo.y,v} by running RCS,
9629 Yacc and @code{cc}.  Then both @file{foo.y} and @file{foo.c} are
9630 intermediate files that are deleted at the end.@refill
9631
9632 No single implicit rule can appear more than once in a chain.  This means
9633 that @code{make} will not even consider such a ridiculous thing as making
9634 @file{foo} from @file{foo.o.o} by running the linker twice.  This
9635 constraint has the added benefit of preventing any infinite loop in the
9636 search for an implicit rule chain.
9637
9638 There are some special implicit rules to optimize certain cases that would
9639 otherwise be handled by rule chains.  For example, making @file{foo} from
9640 @file{foo.c} could be handled by compiling and linking with separate
9641 chained rules, using @file{foo.o} as an intermediate file.  But what
9642 actually happens is that a special rule for this case does the compilation
9643 and linking with a single @code{cc} command.  The optimized rule is used in
9644 preference to the step-by-step chain because it comes earlier in the
9645 ordering of rules.
9646
9647 @node Pattern Rules, Last Resort, Chained Rules, Implicit Rules
9648 @section Defining and Redefining Pattern Rules
9649
9650 You define an implicit rule by writing a @dfn{pattern rule}.  A pattern
9651 rule looks like an ordinary rule, except that its target contains the
9652 character @samp{%} (exactly one of them).  The target is considered a
9653 pattern for matching file names; the @samp{%} can match any nonempty
9654 substring, while other characters match only themselves.  The prerequisites
9655 likewise use @samp{%} to show how their names relate to the target name.
9656
9657 Thus, a pattern rule @samp{%.o : %.c} says how to make any file
9658 @file{@var{stem}.o} from another file @file{@var{stem}.c}.@refill
9659
9660 Note that expansion using @samp{%} in pattern rules occurs
9661 @strong{after} any variable or function expansions, which take place
9662 when the makefile is read.  @xref{Using Variables, , How to Use
9663 Variables}, and @ref{Functions, ,Functions for Transforming Text}.
9664
9665 @menu
9666 * Pattern Intro::               An introduction to pattern rules.
9667 * Pattern Examples::            Examples of pattern rules.
9668 * Automatic Variables::         How to use automatic variables in the
9669                                   recipe of implicit rules.
9670 * Pattern Match::               How patterns match.
9671 * Match-Anything Rules::        Precautions you should take prior to
9672                                   defining rules that can match any
9673                                   target file whatever.
9674 * Canceling Rules::             How to override or cancel built-in rules.
9675 @end menu
9676
9677 @node Pattern Intro, Pattern Examples, Pattern Rules, Pattern Rules
9678 @subsection Introduction to Pattern Rules
9679 @cindex pattern rule
9680 @cindex rule, pattern
9681
9682 A pattern rule contains the character @samp{%} (exactly one of them)
9683 in the target; otherwise, it looks exactly like an ordinary rule.  The
9684 target is a pattern for matching file names; the @samp{%} matches any
9685 nonempty substring, while other characters match only themselves.
9686 @cindex target pattern, implicit
9687 @cindex @code{%}, in pattern rules
9688
9689 For example, @samp{%.c} as a pattern matches any file name that ends in
9690 @samp{.c}.  @samp{s.%.c} as a pattern matches any file name that starts
9691 with @samp{s.}, ends in @samp{.c} and is at least five characters long.
9692 (There must be at least one character to match the @samp{%}.)  The substring
9693 that the @samp{%} matches is called the @dfn{stem}.@refill
9694
9695 @samp{%} in a prerequisite of a pattern rule stands for the same stem
9696 that was matched by the @samp{%} in the target.  In order for the
9697 pattern rule to apply, its target pattern must match the file name
9698 under consideration and all of its prerequisites (after pattern
9699 substitution) must name files that exist or can be made.  These files
9700 become prerequisites of the target.
9701 @cindex prerequisite pattern, implicit
9702
9703 Thus, a rule of the form
9704
9705 @example
9706 %.o : %.c ; @var{recipe}@dots{}
9707 @end example
9708
9709 @noindent
9710 specifies how to make a file @file{@var{n}.o}, with another file
9711 @file{@var{n}.c} as its prerequisite, provided that @file{@var{n}.c}
9712 exists or can be made.
9713
9714 There may also be prerequisites that do not use @samp{%}; such a prerequisite
9715 attaches to every file made by this pattern rule.  These unvarying
9716 prerequisites are useful occasionally.
9717
9718 A pattern rule need not have any prerequisites that contain @samp{%}, or
9719 in fact any prerequisites at all.  Such a rule is effectively a general
9720 wildcard.  It provides a way to make any file that matches the target
9721 pattern.  @xref{Last Resort}.
9722
9723 More than one pattern rule may match a target.  In this case
9724 @code{make} will choose the ``best fit'' rule.  @xref{Pattern Match,
9725 ,How Patterns Match}.
9726
9727 @c !!! The end of of this paragraph should be rewritten.  --bob
9728 Pattern rules may have more than one target.  Unlike normal rules,
9729 this does not act as many different rules with the same prerequisites
9730 and recipe.  If a pattern rule has multiple targets, @code{make} knows
9731 that the rule's recipe is responsible for making all of the targets.
9732 The recipe is executed only once to make all the targets.  When
9733 searching for a pattern rule to match a target, the target patterns of
9734 a rule other than the one that matches the target in need of a rule
9735 are incidental: @code{make} worries only about giving a recipe and
9736 prerequisites to the file presently in question.  However, when this
9737 file's recipe is run, the other targets are marked as having been
9738 updated themselves.
9739 @cindex multiple targets, in pattern rule
9740 @cindex target, multiple in pattern rule
9741
9742 @node Pattern Examples, Automatic Variables, Pattern Intro, Pattern Rules
9743 @subsection Pattern Rule Examples
9744
9745 Here are some examples of pattern rules actually predefined in
9746 @code{make}.  First, the rule that compiles @samp{.c} files into @samp{.o}
9747 files:@refill
9748
9749 @example
9750 %.o : %.c
9751         $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@@
9752 @end example
9753
9754 @noindent
9755 defines a rule that can make any file @file{@var{x}.o} from
9756 @file{@var{x}.c}.  The recipe uses the automatic variables @samp{$@@} and
9757 @samp{$<} to substitute the names of the target file and the source file
9758 in each case where the rule applies (@pxref{Automatic Variables}).@refill
9759
9760 Here is a second built-in rule:
9761
9762 @example
9763 % :: RCS/%,v
9764         $(CO) $(COFLAGS) $<
9765 @end example
9766
9767 @noindent
9768 defines a rule that can make any file @file{@var{x}} whatsoever from a
9769 corresponding file @file{@var{x},v} in the sub-directory @file{RCS}.  Since
9770 the target is @samp{%}, this rule will apply to any file whatever, provided
9771 the appropriate prerequisite file exists.  The double colon makes the rule
9772 @dfn{terminal}, which means that its prerequisite may not be an intermediate
9773 file (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}).@refill
9774
9775 @need 500
9776 This pattern rule has two targets:
9777
9778 @example
9779 @group
9780 %.tab.c %.tab.h: %.y
9781         bison -d $<
9782 @end group
9783 @end example
9784
9785 @noindent
9786 @c The following paragraph is rewritten to avoid overfull hboxes
9787 This tells @code{make} that the recipe @samp{bison -d @var{x}.y} will
9788 make both @file{@var{x}.tab.c} and @file{@var{x}.tab.h}.  If the file
9789 @file{foo} depends on the files @file{parse.tab.o} and @file{scan.o}
9790 and the file @file{scan.o} depends on the file @file{parse.tab.h},
9791 when @file{parse.y} is changed, the recipe @samp{bison -d parse.y}
9792 will be executed only once, and the prerequisites of both
9793 @file{parse.tab.o} and @file{scan.o} will be satisfied.  (Presumably
9794 the file @file{parse.tab.o} will be recompiled from @file{parse.tab.c}
9795 and the file @file{scan.o} from @file{scan.c}, while @file{foo} is
9796 linked from @file{parse.tab.o}, @file{scan.o}, and its other
9797 prerequisites, and it will execute happily ever after.)@refill
9798
9799 @node Automatic Variables, Pattern Match, Pattern Examples, Pattern Rules
9800 @subsection Automatic Variables
9801 @cindex automatic variables
9802 @cindex variables, automatic
9803 @cindex variables, and implicit rule
9804
9805 Suppose you are writing a pattern rule to compile a @samp{.c} file into a
9806 @samp{.o} file: how do you write the @samp{cc} command so that it operates
9807 on the right source file name?  You cannot write the name in the recipe,
9808 because the name is different each time the implicit rule is applied.
9809
9810 What you do is use a special feature of @code{make}, the @dfn{automatic
9811 variables}.  These variables have values computed afresh for each rule that
9812 is executed, based on the target and prerequisites of the rule.  In this
9813 example, you would use @samp{$@@} for the object file name and @samp{$<}
9814 for the source file name.
9815
9816 @cindex automatic variables in prerequisites
9817 @cindex prerequisites, and automatic variables
9818 It's very important that you recognize the limited scope in which
9819 automatic variable values are available: they only have values within
9820 the recipe.  In particular, you cannot use them anywhere
9821 within the target list of a rule; they have no value there and will
9822 expand to the empty string.  Also, they cannot be accessed directly
9823 within the prerequisite list of a rule.  A common mistake is
9824 attempting to use @code{$@@} within the prerequisites list; this will
9825 not work.  However, there is a special feature of GNU @code{make},
9826 secondary expansion (@pxref{Secondary Expansion}), which will allow
9827 automatic variable values to be used in prerequisite lists.
9828
9829 Here is a table of automatic variables:
9830
9831 @table @code
9832 @vindex $@@
9833 @vindex @@ @r{(automatic variable)}
9834 @item $@@
9835 The file name of the target of the rule.  If the target is an archive
9836 member, then @samp{$@@} is the name of the archive file.  In a pattern
9837 rule that has multiple targets (@pxref{Pattern Intro, ,Introduction to
9838 Pattern Rules}), @samp{$@@} is the name of whichever target caused the
9839 rule's recipe to be run.
9840
9841 @vindex $%
9842 @vindex % @r{(automatic variable)}
9843 @item $%
9844 The target member name, when the target is an archive member.
9845 @xref{Archives}.  For example, if the target is @file{foo.a(bar.o)} then
9846 @samp{$%} is @file{bar.o} and @samp{$@@} is @file{foo.a}.  @samp{$%} is
9847 empty when the target is not an archive member.
9848
9849 @vindex $<
9850 @vindex < @r{(automatic variable)}
9851 @item $<
9852 The name of the first prerequisite.  If the target got its recipe from
9853 an implicit rule, this will be the first prerequisite added by the
9854 implicit rule (@pxref{Implicit Rules}).
9855
9856 @vindex $?
9857 @vindex ? @r{(automatic variable)}
9858 @item $?
9859 The names of all the prerequisites that are newer than the target, with
9860 spaces between them.  For prerequisites which are archive members, only
9861 the named member is used (@pxref{Archives}).
9862 @cindex prerequisites, list of changed
9863 @cindex list of changed prerequisites
9864
9865 @vindex $^
9866 @vindex ^ @r{(automatic variable)}
9867 @item $^
9868 The names of all the prerequisites, with spaces between them.  For
9869 prerequisites which are archive members, only the named member is used
9870 (@pxref{Archives}).  A target has only one prerequisite on each other file
9871 it depends on, no matter how many times each file is listed as a
9872 prerequisite.  So if you list a prerequisite more than once for a target,
9873 the value of @code{$^} contains just one copy of the name.  This list
9874 does @strong{not} contain any of the order-only prerequisites; for those
9875 see the @samp{$|} variable, below.
9876 @cindex prerequisites, list of all
9877 @cindex list of all prerequisites
9878
9879 @vindex $+
9880 @vindex + @r{(automatic variable)}
9881 @item $+
9882 This is like @samp{$^}, but prerequisites listed more than once are
9883 duplicated in the order they were listed in the makefile.  This is
9884 primarily useful for use in linking commands where it is meaningful to
9885 repeat library file names in a particular order.
9886
9887 @vindex $|
9888 @vindex | @r{(automatic variable)}
9889 @item $|
9890 The names of all the order-only prerequisites, with spaces between
9891 them.
9892
9893 @vindex $*
9894 @vindex * @r{(automatic variable)}
9895 @item $*
9896 The stem with which an implicit rule matches (@pxref{Pattern Match, ,How
9897 Patterns Match}).  If the target is @file{dir/a.foo.b} and the target
9898 pattern is @file{a.%.b} then the stem is @file{dir/foo}.  The stem is
9899 useful for constructing names of related files.@refill
9900 @cindex stem, variable for
9901
9902 In a static pattern rule, the stem is part of the file name that matched
9903 the @samp{%} in the target pattern.
9904
9905 In an explicit rule, there is no stem; so @samp{$*} cannot be determined
9906 in that way.  Instead, if the target name ends with a recognized suffix
9907 (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}), @samp{$*} is set to
9908 the target name minus the suffix.  For example, if the target name is
9909 @samp{foo.c}, then @samp{$*} is set to @samp{foo}, since @samp{.c} is a
9910 suffix.  GNU @code{make} does this bizarre thing only for compatibility
9911 with other implementations of @code{make}.  You should generally avoid
9912 using @samp{$*} except in implicit rules or static pattern rules.@refill
9913
9914 If the target name in an explicit rule does not end with a recognized
9915 suffix, @samp{$*} is set to the empty string for that rule.
9916 @end table
9917
9918 @samp{$?} is useful even in explicit rules when you wish to operate on only
9919 the prerequisites that have changed.  For example, suppose that an archive
9920 named @file{lib} is supposed to contain copies of several object files.
9921 This rule copies just the changed object files into the archive:
9922
9923 @example
9924 @group
9925 lib: foo.o bar.o lose.o win.o
9926         ar r lib $?
9927 @end group
9928 @end example
9929
9930 Of the variables listed above, four have values that are single file
9931 names, and three have values that are lists of file names.  These seven
9932 have variants that get just the file's directory name or just the file
9933 name within the directory.  The variant variables' names are formed by
9934 appending @samp{D} or @samp{F}, respectively.  These variants are
9935 semi-obsolete in GNU @code{make} since the functions @code{dir} and
9936 @code{notdir} can be used to get a similar effect (@pxref{File Name
9937 Functions, , Functions for File Names}).  Note, however, that the
9938 @samp{D} variants all omit the trailing slash which always appears in
9939 the output of the @code{dir} function.  Here is a table of the variants:
9940
9941 @table @samp
9942 @vindex $(@@D)
9943 @vindex @@D @r{(automatic variable)}
9944 @item $(@@D)
9945 The directory part of the file name of the target, with the trailing
9946 slash removed.  If the value of @samp{$@@} is @file{dir/foo.o} then
9947 @samp{$(@@D)} is @file{dir}.  This value is @file{.} if @samp{$@@} does
9948 not contain a slash.
9949
9950 @vindex $(@@F)
9951 @vindex @@F @r{(automatic variable)}
9952 @item $(@@F)
9953 The file-within-directory part of the file name of the target.  If the
9954 value of @samp{$@@} is @file{dir/foo.o} then @samp{$(@@F)} is
9955 @file{foo.o}.  @samp{$(@@F)} is equivalent to @samp{$(notdir $@@)}.
9956
9957 @vindex $(*D)
9958 @vindex *D @r{(automatic variable)}
9959 @item $(*D)
9960 @vindex $(*F)
9961 @vindex *F @r{(automatic variable)}
9962 @itemx $(*F)
9963 The directory part and the file-within-directory
9964 part of the stem; @file{dir} and @file{foo} in this example.
9965
9966 @vindex $(%D)
9967 @vindex %D @r{(automatic variable)}
9968 @item $(%D)
9969 @vindex $(%F)
9970 @vindex %F @r{(automatic variable)}
9971 @itemx $(%F)
9972 The directory part and the file-within-directory part of the target
9973 archive member name.  This makes sense only for archive member targets
9974 of the form @file{@var{archive}(@var{member})} and is useful only when
9975 @var{member} may contain a directory name.  (@xref{Archive Members,
9976 ,Archive Members as Targets}.)
9977
9978 @vindex $(<D)
9979 @vindex <D @r{(automatic variable)}
9980 @item $(<D)
9981 @vindex $(<F)
9982 @vindex <F @r{(automatic variable)}
9983 @itemx $(<F)
9984 The directory part and the file-within-directory
9985 part of the first prerequisite.
9986
9987 @vindex $(^D)
9988 @vindex ^D @r{(automatic variable)}
9989 @item $(^D)
9990 @vindex $(^F)
9991 @vindex ^F @r{(automatic variable)}
9992 @itemx $(^F)
9993 Lists of the directory parts and the file-within-directory
9994 parts of all prerequisites.
9995
9996 @vindex $(+D)
9997 @vindex +D @r{(automatic variable)}
9998 @item $(+D)
9999 @vindex $(+F)
10000 @vindex +F @r{(automatic variable)}
10001 @itemx $(+F)
10002 Lists of the directory parts and the file-within-directory
10003 parts of all prerequisites, including multiple instances of duplicated
10004 prerequisites.
10005
10006 @vindex $(?D)
10007 @vindex ?D @r{(automatic variable)}
10008 @item $(?D)
10009 @vindex $(?F)
10010 @vindex ?F @r{(automatic variable)}
10011 @itemx $(?F)
10012 Lists of the directory parts and the file-within-directory parts of
10013 all prerequisites that are newer than the target.
10014 @end table
10015
10016 Note that we use a special stylistic convention when we talk about these
10017 automatic variables; we write ``the value of @samp{$<}'', rather than
10018 @w{``the variable @code{<}''} as we would write for ordinary variables
10019 such as @code{objects} and @code{CFLAGS}.  We think this convention
10020 looks more natural in this special case.  Please do not assume it has a
10021 deep significance; @samp{$<} refers to the variable named @code{<} just
10022 as @samp{$(CFLAGS)} refers to the variable named @code{CFLAGS}.
10023 You could just as well use @samp{$(<)} in place of @samp{$<}.
10024
10025 @node Pattern Match, Match-Anything Rules, Automatic Variables, Pattern Rules
10026 @subsection How Patterns Match
10027
10028 @cindex stem
10029 A target pattern is composed of a @samp{%} between a prefix and a suffix,
10030 either or both of which may be empty.  The pattern matches a file name only
10031 if the file name starts with the prefix and ends with the suffix, without
10032 overlap.  The text between the prefix and the suffix is called the
10033 @dfn{stem}.  Thus, when the pattern @samp{%.o} matches the file name
10034 @file{test.o}, the stem is @samp{test}.  The pattern rule prerequisites are
10035 turned into actual file names by substituting the stem for the character
10036 @samp{%}.  Thus, if in the same example one of the prerequisites is written
10037 as @samp{%.c}, it expands to @samp{test.c}.@refill
10038
10039 When the target pattern does not contain a slash (and it usually does
10040 not), directory names in the file names are removed from the file name
10041 before it is compared with the target prefix and suffix.  After the
10042 comparison of the file name to the target pattern, the directory
10043 names, along with the slash that ends them, are added on to the
10044 prerequisite file names generated from the pattern rule's prerequisite
10045 patterns and the file name.  The directories are ignored only for the
10046 purpose of finding an implicit rule to use, not in the application of
10047 that rule.  Thus, @samp{e%t} matches the file name @file{src/eat},
10048 with @samp{src/a} as the stem.  When prerequisites are turned into file
10049 names, the directories from the stem are added at the front, while the
10050 rest of the stem is substituted for the @samp{%}.  The stem
10051 @samp{src/a} with a prerequisite pattern @samp{c%r} gives the file name
10052 @file{src/car}.@refill
10053
10054 @cindex pattern rules, order of
10055 @cindex order of pattern rules
10056 A pattern rule can be used to build a given file only if there is a
10057 target pattern that matches the file name, @emph{and} all
10058 prerequisites in that rule either exist or can be built.  The rules
10059 you write take precedence over those that are built in. Note however,
10060 that a rule whose prerequisites actually exist or are mentioned always
10061 takes priority over a rule with prerequisites that must be made by
10062 chaining other implicit rules.
10063
10064 @cindex stem, shortest
10065 It is possible that more than one pattern rule will meet these
10066 criteria.  In that case, @code{make} will choose the rule with the
10067 shortest stem (that is, the pattern that matches most specifically).
10068 If more than one pattern rule has the shortest stem, @code{make} will
10069 choose the first one found in the makefile.
10070
10071 This algorithm results in more specific rules being preferred over
10072 more generic ones; for example:
10073
10074 @example
10075 %.o: %.c
10076         $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@@
10077
10078 %.o : %.f
10079         $(COMPILE.F) $(OUTPUT_OPTION) $<
10080
10081 lib/%.o: lib/%.c
10082         $(CC) -fPIC -c $(CFLAGS) $(CPPFLAGS) $< -o $@@
10083 @end example
10084
10085 Given these rules and asked to build @file{bar.o} where both
10086 @file{bar.c} and @file{bar.f} exist, @code{make} will choose the first
10087 rule and compile @file{bar.c} into @file{bar.o}.  In the same
10088 situation where @file{bar.c} does not exist, then @code{make} will
10089 choose the second rule and compile @file{bar.f} into @file{bar.o}.
10090
10091 If @code{make} is asked to build @file{lib/bar.o} and both
10092 @file{lib/bar.c} and @file{lib/bar.f} exist, then the third rule will
10093 be chosen since the stem for this rule (@samp{bar}) is shorter than
10094 the stem for the first rule (@samp{lib/bar}).  If @file{lib/bar.c}
10095 does not exist then the third rule is not eligible and the second rule
10096 will be used, even though the stem is longer.
10097
10098 @node Match-Anything Rules, Canceling Rules, Pattern Match, Pattern Rules
10099 @subsection Match-Anything Pattern Rules
10100
10101 @cindex match-anything rule
10102 @cindex terminal rule
10103 When a pattern rule's target is just @samp{%}, it matches any file name
10104 whatever.  We call these rules @dfn{match-anything} rules.  They are very
10105 useful, but it can take a lot of time for @code{make} to think about them,
10106 because it must consider every such rule for each file name listed either
10107 as a target or as a prerequisite.
10108
10109 Suppose the makefile mentions @file{foo.c}.  For this target, @code{make}
10110 would have to consider making it by linking an object file @file{foo.c.o},
10111 or by C compilation-and-linking in one step from @file{foo.c.c}, or by
10112 Pascal compilation-and-linking from @file{foo.c.p}, and many other
10113 possibilities.
10114
10115 We know these possibilities are ridiculous since @file{foo.c} is a C source
10116 file, not an executable.  If @code{make} did consider these possibilities,
10117 it would ultimately reject them, because files such as @file{foo.c.o} and
10118 @file{foo.c.p} would not exist.  But these possibilities are so
10119 numerous that @code{make} would run very slowly if it had to consider
10120 them.@refill
10121
10122 To gain speed, we have put various constraints on the way @code{make}
10123 considers match-anything rules.  There are two different constraints that
10124 can be applied, and each time you define a match-anything rule you must
10125 choose one or the other for that rule.
10126
10127 One choice is to mark the match-anything rule as @dfn{terminal} by defining
10128 it with a double colon.  When a rule is terminal, it does not apply unless
10129 its prerequisites actually exist.  Prerequisites that could be made with
10130 other implicit rules are not good enough.  In other words, no further
10131 chaining is allowed beyond a terminal rule.
10132
10133 For example, the built-in implicit rules for extracting sources from RCS
10134 and SCCS files are terminal; as a result, if the file @file{foo.c,v} does
10135 not exist, @code{make} will not even consider trying to make it as an
10136 intermediate file from @file{foo.c,v.o} or from @file{RCS/SCCS/s.foo.c,v}.
10137 RCS and SCCS files are generally ultimate source files, which should not be
10138 remade from any other files; therefore, @code{make} can save time by not
10139 looking for ways to remake them.@refill
10140
10141 If you do not mark the match-anything rule as terminal, then it is
10142 non-terminal.  A non-terminal match-anything rule cannot apply to a file name
10143 that indicates a specific type of data.  A file name indicates a specific
10144 type of data if some non-match-anything implicit rule target matches it.
10145
10146 For example, the file name @file{foo.c} matches the target for the pattern
10147 rule @samp{%.c : %.y} (the rule to run Yacc).  Regardless of whether this
10148 rule is actually applicable (which happens only if there is a file
10149 @file{foo.y}), the fact that its target matches is enough to prevent
10150 consideration of any non-terminal match-anything rules for the file
10151 @file{foo.c}.  Thus, @code{make} will not even consider trying to make
10152 @file{foo.c} as an executable file from @file{foo.c.o}, @file{foo.c.c},
10153 @file{foo.c.p}, etc.@refill
10154
10155 The motivation for this constraint is that non-terminal match-anything
10156 rules are used for making files containing specific types of data (such as
10157 executable files) and a file name with a recognized suffix indicates some
10158 other specific type of data (such as a C source file).
10159
10160 Special built-in dummy pattern rules are provided solely to recognize
10161 certain file names so that non-terminal match-anything rules will not be
10162 considered.  These dummy rules have no prerequisites and no recipes, and
10163 they are ignored for all other purposes.  For example, the built-in
10164 implicit rule
10165
10166 @example
10167 %.p :
10168 @end example
10169
10170 @noindent
10171 exists to make sure that Pascal source files such as @file{foo.p} match a
10172 specific target pattern and thereby prevent time from being wasted looking
10173 for @file{foo.p.o} or @file{foo.p.c}.
10174
10175 Dummy pattern rules such as the one for @samp{%.p} are made for every
10176 suffix listed as valid for use in suffix rules (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}).
10177
10178 @node Canceling Rules,  , Match-Anything Rules, Pattern Rules
10179 @subsection Canceling Implicit Rules
10180
10181 You can override a built-in implicit rule (or one you have defined
10182 yourself) by defining a new pattern rule with the same target and
10183 prerequisites, but a different recipe.  When the new rule is defined, the
10184 built-in one is replaced.  The new rule's position in the sequence of
10185 implicit rules is determined by where you write the new rule.
10186
10187 You can cancel a built-in implicit rule by defining a pattern rule with the
10188 same target and prerequisites, but no recipe.  For example, the following
10189 would cancel the rule that runs the assembler:
10190
10191 @example
10192 %.o : %.s
10193 @end example
10194
10195 @node Last Resort, Suffix Rules, Pattern Rules, Implicit Rules
10196 @section Defining Last-Resort Default Rules
10197 @cindex last-resort default rules
10198 @cindex default rules, last-resort
10199
10200 You can define a last-resort implicit rule by writing a terminal
10201 match-anything pattern rule with no prerequisites (@pxref{Match-Anything
10202 Rules}).  This is just like any other pattern rule; the only thing
10203 special about it is that it will match any target.  So such a rule's
10204 recipe is used for all targets and prerequisites that have no recipe
10205 of their own and for which no other implicit rule applies.
10206
10207 For example, when testing a makefile, you might not care if the source
10208 files contain real data, only that they exist.  Then you might do this:
10209
10210 @example
10211 %::
10212         touch $@@
10213 @end example
10214
10215 @noindent
10216 to cause all the source files needed (as prerequisites) to be created
10217 automatically.
10218
10219 @findex .DEFAULT
10220 You can instead define a recipe to be used for targets for which there
10221 are no rules at all, even ones which don't specify recipes.  You do
10222 this by writing a rule for the target @code{.DEFAULT}.  Such a rule's
10223 recipe is used for all prerequisites which do not appear as targets in
10224 any explicit rule, and for which no implicit rule applies.  Naturally,
10225 there is no @code{.DEFAULT} rule unless you write one.
10226
10227 If you use @code{.DEFAULT} with no recipe or prerequisites:
10228
10229 @example
10230 .DEFAULT:
10231 @end example
10232
10233 @noindent
10234 the recipe previously stored for @code{.DEFAULT} is cleared.  Then
10235 @code{make} acts as if you had never defined @code{.DEFAULT} at all.
10236
10237 If you do not want a target to get the recipe from a match-anything
10238 pattern rule or @code{.DEFAULT}, but you also do not want any recipe
10239 to be run for the target, you can give it an empty recipe
10240 (@pxref{Empty Recipes, ,Defining Empty Recipes}).@refill
10241
10242 You can use a last-resort rule to override part of another makefile.
10243 @xref{Overriding Makefiles, , Overriding Part of Another Makefile}.
10244
10245 @node Suffix Rules, Implicit Rule Search, Last Resort, Implicit Rules
10246 @section Old-Fashioned Suffix Rules
10247 @cindex old-fashioned suffix rules
10248 @cindex suffix rule
10249
10250 @dfn{Suffix rules} are the old-fashioned way of defining implicit rules for
10251 @code{make}.  Suffix rules are obsolete because pattern rules are more
10252 general and clearer.  They are supported in GNU @code{make} for
10253 compatibility with old makefiles.  They come in two kinds:
10254 @dfn{double-suffix} and @dfn{single-suffix}.@refill
10255
10256 A double-suffix rule is defined by a pair of suffixes: the target suffix
10257 and the source suffix.  It matches any file whose name ends with the
10258 target suffix.  The corresponding implicit prerequisite is made by
10259 replacing the target suffix with the source suffix in the file name.  A
10260 two-suffix rule whose target and source suffixes are @samp{.o} and
10261 @samp{.c} is equivalent to the pattern rule @samp{%.o : %.c}.
10262
10263 A single-suffix rule is defined by a single suffix, which is the source
10264 suffix.  It matches any file name, and the corresponding implicit
10265 prerequisite name is made by appending the source suffix.  A single-suffix
10266 rule whose source suffix is @samp{.c} is equivalent to the pattern rule
10267 @samp{% : %.c}.
10268
10269 Suffix rule definitions are recognized by comparing each rule's target
10270 against a defined list of known suffixes.  When @code{make} sees a rule
10271 whose target is a known suffix, this rule is considered a single-suffix
10272 rule.  When @code{make} sees a rule whose target is two known suffixes
10273 concatenated, this rule is taken as a double-suffix rule.
10274
10275 For example, @samp{.c} and @samp{.o} are both on the default list of
10276 known suffixes.  Therefore, if you define a rule whose target is
10277 @samp{.c.o}, @code{make} takes it to be a double-suffix rule with source
10278 suffix @samp{.c} and target suffix @samp{.o}.  Here is the old-fashioned
10279 way to define the rule for compiling a C source file:@refill
10280
10281 @example
10282 .c.o:
10283         $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
10284 @end example
10285
10286 Suffix rules cannot have any prerequisites of their own.  If they have any,
10287 they are treated as normal files with funny names, not as suffix rules.
10288 Thus, the rule:
10289
10290 @example
10291 .c.o: foo.h
10292         $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
10293 @end example
10294
10295 @noindent
10296 tells how to make the file @file{.c.o} from the prerequisite file
10297 @file{foo.h}, and is not at all like the pattern rule:
10298
10299 @example
10300 %.o: %.c foo.h
10301         $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
10302 @end example
10303
10304 @noindent
10305 which tells how to make @samp{.o} files from @samp{.c} files, and makes all
10306 @samp{.o} files using this pattern rule also depend on @file{foo.h}.
10307
10308 Suffix rules with no recipe are also meaningless.  They do not remove
10309 previous rules as do pattern rules with no recipe (@pxref{Canceling
10310 Rules, , Canceling Implicit Rules}).  They simply enter the suffix or
10311 pair of suffixes concatenated as a target in the data base.@refill
10312
10313 @findex .SUFFIXES
10314 The known suffixes are simply the names of the prerequisites of the special
10315 target @code{.SUFFIXES}.  You can add your own suffixes by writing a rule
10316 for @code{.SUFFIXES} that adds more prerequisites, as in:
10317
10318 @example
10319 .SUFFIXES: .hack .win
10320 @end example
10321
10322 @noindent
10323 which adds @samp{.hack} and @samp{.win} to the end of the list of suffixes.
10324
10325 If you wish to eliminate the default known suffixes instead of just adding
10326 to them, write a rule for @code{.SUFFIXES} with no prerequisites.  By
10327 special dispensation, this eliminates all existing prerequisites of
10328 @code{.SUFFIXES}.  You can then write another rule to add the suffixes you
10329 want.  For example,
10330
10331 @example
10332 @group
10333 .SUFFIXES:            # @r{Delete the default suffixes}
10334 .SUFFIXES: .c .o .h   # @r{Define our suffix list}
10335 @end group
10336 @end example
10337
10338 The @samp{-r} or @samp{--no-builtin-rules} flag causes the default
10339 list of suffixes to be empty.
10340
10341 @vindex SUFFIXES
10342 The variable @code{SUFFIXES} is defined to the default list of suffixes
10343 before @code{make} reads any makefiles.  You can change the list of suffixes
10344 with a rule for the special target @code{.SUFFIXES}, but that does not alter
10345 this variable.
10346
10347 @node Implicit Rule Search,  , Suffix Rules, Implicit Rules
10348 @section Implicit Rule Search Algorithm
10349 @cindex implicit rule, search algorithm
10350 @cindex search algorithm, implicit rule
10351
10352 Here is the procedure @code{make} uses for searching for an implicit rule
10353 for a target @var{t}.  This procedure is followed for each double-colon
10354 rule with no recipe, for each target of ordinary rules none of which have
10355 a recipe, and for each prerequisite that is not the target of any rule.  It
10356 is also followed recursively for prerequisites that come from implicit
10357 rules, in the search for a chain of rules.
10358
10359 Suffix rules are not mentioned in this algorithm because suffix rules are
10360 converted to equivalent pattern rules once the makefiles have been read in.
10361
10362 For an archive member target of the form
10363 @samp{@var{archive}(@var{member})}, the following algorithm is run
10364 twice, first using the entire target name @var{t}, and second using
10365 @samp{(@var{member})} as the target @var{t} if the first run found no
10366 rule.@refill
10367
10368 @enumerate
10369 @item
10370 Split @var{t} into a directory part, called @var{d}, and the rest,
10371 called @var{n}.  For example, if @var{t} is @samp{src/foo.o}, then
10372 @var{d} is @samp{src/} and @var{n} is @samp{foo.o}.@refill
10373
10374 @item
10375 Make a list of all the pattern rules one of whose targets matches
10376 @var{t} or @var{n}.  If the target pattern contains a slash, it is
10377 matched against @var{t}; otherwise, against @var{n}.
10378
10379 @item
10380 If any rule in that list is @emph{not} a match-anything rule, then
10381 remove all non-terminal match-anything rules from the list.
10382
10383 @item
10384 Remove from the list all rules with no recipe.
10385
10386 @item
10387 For each pattern rule in the list:
10388
10389 @enumerate a
10390 @item
10391 Find the stem @var{s}, which is the nonempty part of @var{t} or @var{n}
10392 matched by the @samp{%} in the target pattern.@refill
10393
10394 @item
10395 Compute the prerequisite names by substituting @var{s} for @samp{%}; if
10396 the target pattern does not contain a slash, append @var{d} to
10397 the front of each prerequisite name.@refill
10398
10399 @item
10400 Test whether all the prerequisites exist or ought to exist.  (If a
10401 file name is mentioned in the makefile as a target or as an explicit
10402 prerequisite, then we say it ought to exist.)
10403
10404 If all prerequisites exist or ought to exist, or there are no prerequisites,
10405 then this rule applies.
10406 @end enumerate
10407
10408 @item
10409 If no pattern rule has been found so far, try harder.
10410 For each pattern rule in the list:
10411
10412 @enumerate a
10413 @item
10414 If the rule is terminal, ignore it and go on to the next rule.
10415
10416 @item
10417 Compute the prerequisite names as before.
10418
10419 @item
10420 Test whether all the prerequisites exist or ought to exist.
10421
10422 @item
10423 For each prerequisite that does not exist, follow this algorithm
10424 recursively to see if the prerequisite can be made by an implicit
10425 rule.
10426
10427 @item
10428 If all prerequisites exist, ought to exist, or can be
10429 made by implicit rules, then this rule applies.
10430 @end enumerate
10431
10432 @item
10433 If no implicit rule applies, the rule for @code{.DEFAULT}, if any,
10434 applies.  In that case, give @var{t} the same recipe that
10435 @code{.DEFAULT} has.  Otherwise, there is no recipe for @var{t}.
10436 @end enumerate
10437
10438 Once a rule that applies has been found, for each target pattern of
10439 the rule other than the one that matched @var{t} or @var{n}, the
10440 @samp{%} in the pattern is replaced with @var{s} and the resultant
10441 file name is stored until the recipe to remake the target file @var{t}
10442 is executed.  After the recipe is executed, each of these stored file
10443 names are entered into the data base and marked as having been updated
10444 and having the same update status as the file @var{t}.
10445
10446 When the recipe of a pattern rule is executed for @var{t}, the
10447 automatic variables are set corresponding to the target and
10448 prerequisites.  @xref{Automatic Variables}.
10449
10450 @node Archives, Extending make, Implicit Rules, Top
10451 @chapter Using @code{make} to Update Archive Files
10452 @cindex archive
10453
10454 @dfn{Archive files} are files containing named sub-files called
10455 @dfn{members}; they are maintained with the program @code{ar} and their
10456 main use is as subroutine libraries for linking.
10457
10458 @menu
10459 * Archive Members::             Archive members as targets.
10460 * Archive Update::              The implicit rule for archive member targets.
10461 * Archive Pitfalls::            Dangers to watch out for when using archives.
10462 * Archive Suffix Rules::        You can write a special kind of suffix rule
10463                                   for updating archives.
10464 @end menu
10465
10466 @node Archive Members, Archive Update, Archives, Archives
10467 @section Archive Members as Targets
10468 @cindex archive member targets
10469
10470 An individual member of an archive file can be used as a target or
10471 prerequisite in @code{make}.  You specify the member named @var{member} in
10472 archive file @var{archive} as follows:
10473
10474 @example
10475 @var{archive}(@var{member})
10476 @end example
10477
10478 @noindent
10479 This construct is available only in targets and prerequisites, not in
10480 recipes!  Most programs that you might use in recipes do not support
10481 this syntax and cannot act directly on archive members.  Only
10482 @code{ar} and other programs specifically designed to operate on
10483 archives can do so.  Therefore, valid recipes to update an archive
10484 member target probably must use @code{ar}.  For example, this rule
10485 says to create a member @file{hack.o} in archive @file{foolib} by
10486 copying the file @file{hack.o}:
10487
10488 @example
10489 foolib(hack.o) : hack.o
10490         ar cr foolib hack.o
10491 @end example
10492
10493 In fact, nearly all archive member targets are updated in just this way
10494 and there is an implicit rule to do it for you.  @strong{Please note:} The
10495 @samp{c} flag to @code{ar} is required if the archive file does not
10496 already exist.
10497
10498 To specify several members in the same archive, you can write all the
10499 member names together between the parentheses.  For example:
10500
10501 @example
10502 foolib(hack.o kludge.o)
10503 @end example
10504
10505 @noindent
10506 is equivalent to:
10507
10508 @example
10509 foolib(hack.o) foolib(kludge.o)
10510 @end example
10511
10512 @cindex wildcard, in archive member
10513 You can also use shell-style wildcards in an archive member reference.
10514 @xref{Wildcards, ,Using Wildcard Characters in File Names}.  For
10515 example, @w{@samp{foolib(*.o)}} expands to all existing members of the
10516 @file{foolib} archive whose names end in @samp{.o}; perhaps
10517 @samp{@w{foolib(hack.o)} @w{foolib(kludge.o)}}.
10518
10519 @node Archive Update, Archive Pitfalls, Archive Members, Archives
10520 @section Implicit Rule for Archive Member Targets
10521
10522 Recall that a target that looks like @file{@var{a}(@var{m})} stands for the
10523 member named @var{m} in the archive file @var{a}.
10524
10525 When @code{make} looks for an implicit rule for such a target, as a special
10526 feature it considers implicit rules that match @file{(@var{m})}, as well as
10527 those that match the actual target @file{@var{a}(@var{m})}.
10528
10529 This causes one special rule whose target is @file{(%)} to match.  This
10530 rule updates the target @file{@var{a}(@var{m})} by copying the file @var{m}
10531 into the archive.  For example, it will update the archive member target
10532 @file{foo.a(bar.o)} by copying the @emph{file} @file{bar.o} into the
10533 archive @file{foo.a} as a @emph{member} named @file{bar.o}.
10534
10535 When this rule is chained with others, the result is very powerful.
10536 Thus, @samp{make "foo.a(bar.o)"} (the quotes are needed to protect the
10537 @samp{(} and @samp{)} from being interpreted specially by the shell) in
10538 the presence of a file @file{bar.c} is enough to cause the following
10539 recipe to be run, even without a makefile:
10540
10541 @example
10542 cc -c bar.c -o bar.o
10543 ar r foo.a bar.o
10544 rm -f bar.o
10545 @end example
10546
10547 @noindent
10548 Here @code{make} has envisioned the file @file{bar.o} as an intermediate
10549 file.  @xref{Chained Rules, ,Chains of Implicit Rules}.
10550
10551 Implicit rules such as this one are written using the automatic variable
10552 @samp{$%}.  @xref{Automatic Variables}.
10553
10554 An archive member name in an archive cannot contain a directory name, but
10555 it may be useful in a makefile to pretend that it does.  If you write an
10556 archive member target @file{foo.a(dir/file.o)}, @code{make} will perform
10557 automatic updating with this recipe:
10558
10559 @example
10560 ar r foo.a dir/file.o
10561 @end example
10562
10563 @noindent
10564 which has the effect of copying the file @file{dir/file.o} into a member
10565 named @file{file.o}.  In connection with such usage, the automatic variables
10566 @code{%D} and @code{%F} may be useful.
10567
10568 @menu
10569 * Archive Symbols::             How to update archive symbol directories.
10570 @end menu
10571
10572 @node Archive Symbols,  , Archive Update, Archive Update
10573 @subsection Updating Archive Symbol Directories
10574 @cindex @code{__.SYMDEF}
10575 @cindex updating archive symbol directories
10576 @cindex archive symbol directory updating
10577 @cindex symbol directories, updating archive
10578 @cindex directories, updating archive symbol
10579
10580 An archive file that is used as a library usually contains a special member
10581 named @file{__.SYMDEF} that contains a directory of the external symbol
10582 names defined by all the other members.  After you update any other
10583 members, you need to update @file{__.SYMDEF} so that it will summarize the
10584 other members properly.  This is done by running the @code{ranlib} program:
10585
10586 @example
10587 ranlib @var{archivefile}
10588 @end example
10589
10590 Normally you would put this command in the rule for the archive file,
10591 and make all the members of the archive file prerequisites of that rule.
10592 For example,
10593
10594 @example
10595 libfoo.a: libfoo.a(x.o) libfoo.a(y.o) @dots{}
10596         ranlib libfoo.a
10597 @end example
10598
10599 @noindent
10600 The effect of this is to update archive members @file{x.o}, @file{y.o},
10601 etc., and then update the symbol directory member @file{__.SYMDEF} by
10602 running @code{ranlib}.  The rules for updating the members are not shown
10603 here; most likely you can omit them and use the implicit rule which copies
10604 files into the archive, as described in the preceding section.
10605
10606 This is not necessary when using the GNU @code{ar} program, which
10607 updates the @file{__.SYMDEF} member automatically.
10608
10609 @node Archive Pitfalls, Archive Suffix Rules, Archive Update, Archives
10610 @section Dangers When Using Archives
10611 @cindex archive, and parallel execution
10612 @cindex parallel execution, and archive update
10613 @cindex archive, and @code{-j}
10614 @cindex @code{-j}, and archive update
10615
10616 It is important to be careful when using parallel execution (the
10617 @code{-j} switch; @pxref{Parallel, ,Parallel Execution}) and archives.
10618 If multiple @code{ar} commands run at the same time on the same archive
10619 file, they will not know about each other and can corrupt the file.
10620
10621 Possibly a future version of @code{make} will provide a mechanism to
10622 circumvent this problem by serializing all recipes that operate on the
10623 same archive file.  But for the time being, you must either write your
10624 makefiles to avoid this problem in some other way, or not use @code{-j}.
10625
10626 @node Archive Suffix Rules,  , Archive Pitfalls, Archives
10627 @section Suffix Rules for Archive Files
10628 @cindex suffix rule, for archive
10629 @cindex archive, suffix rule for
10630 @cindex library archive, suffix rule for
10631 @cindex @code{.a} (archives)
10632
10633 You can write a special kind of suffix rule for dealing with archive
10634 files.  @xref{Suffix Rules}, for a full explanation of suffix rules.
10635 Archive suffix rules are obsolete in GNU @code{make}, because pattern
10636 rules for archives are a more general mechanism (@pxref{Archive
10637 Update}).  But they are retained for compatibility with other
10638 @code{make}s.
10639
10640 To write a suffix rule for archives, you simply write a suffix rule
10641 using the target suffix @samp{.a} (the usual suffix for archive files).
10642 For example, here is the old-fashioned suffix rule to update a library
10643 archive from C source files:
10644
10645 @example
10646 @group
10647 .c.a:
10648         $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o
10649         $(AR) r $@@ $*.o
10650         $(RM) $*.o
10651 @end group
10652 @end example
10653
10654 @noindent
10655 This works just as if you had written the pattern rule:
10656
10657 @example
10658 @group
10659 (%.o): %.c
10660         $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o
10661         $(AR) r $@@ $*.o
10662         $(RM) $*.o
10663 @end group
10664 @end example
10665
10666 In fact, this is just what @code{make} does when it sees a suffix rule
10667 with @samp{.a} as the target suffix.  Any double-suffix rule
10668 @w{@samp{.@var{x}.a}} is converted to a pattern rule with the target
10669 pattern @samp{(%.o)} and a prerequisite pattern of @samp{%.@var{x}}.
10670
10671 Since you might want to use @samp{.a} as the suffix for some other kind
10672 of file, @code{make} also converts archive suffix rules to pattern rules
10673 in the normal way (@pxref{Suffix Rules}).  Thus a double-suffix rule
10674 @w{@samp{.@var{x}.a}} produces two pattern rules: @samp{@w{(%.o):}
10675 @w{%.@var{x}}} and @samp{@w{%.a}: @w{%.@var{x}}}.@refill
10676
10677 @node Extending make, Features, Archives, Top
10678 @chapter Extending GNU @code{make}
10679 @cindex make extensions
10680
10681 GNU @code{make} provides many advanced capabilities, including many
10682 useful functions.  However, it does not contain a complete programming
10683 language and so it has limitations.  Sometimes these limitations can be
10684 overcome through use of the @code{shell} function to invoke a separate
10685 program, although this can be inefficient.
10686
10687 In cases where the built-in capabilities of GNU @code{make} are
10688 insufficient to your requirements there are two options for extending
10689 @code{make}.  On systems where it's provided, you can utilize GNU
10690 Guile as an embedded scripting language (@pxref{Guile Integration,,GNU
10691 Guile Integration}).  On systems which support dynamically loadable
10692 objects, you can write your own extension in any language (which can
10693 be compiled into such an object) and load it to provide extended
10694 capabilities (@pxref{load Directive, ,The @code{load} Directive}).
10695
10696 @menu
10697 * Guile Integration::           Using Guile as an embedded scripting language.
10698 * Loading Objects::             Loading dynamic objects as extensions.
10699 @end menu
10700
10701 @node Guile Integration, Loading Objects, Extending make, Extending make
10702 @section GNU Guile Integration
10703 @cindex Guile
10704 @cindex extensions, Guile
10705
10706 GNU @code{make} may be built with support for GNU Guile as an embedded
10707 extension language.  Guile implements the Scheme language.  A review
10708 of GNU Guile and the Scheme language and its features is beyond the
10709 scope of this manual: see the documentation for GNU Guile and Scheme.
10710
10711 You can determine if @code{make} contains support for Guile by
10712 examining the @code{.FEATURES} variable; it will contain the word
10713 @var{guile} if Guile support is available.
10714
10715 The Guile integration provides one new @code{make} function: @code{guile}.
10716 The @code{guile} function takes one argument which is first expanded
10717 by @code{make} in the normal fashion, then passed to the GNU Guile
10718 evaluator.  The result of the evaluator is converted into a string and
10719 used as the expansion of the @code{guile} function in the makefile.
10720
10721 In addition, GNU @code{make} exposes Guile procedures for use in Guile
10722 scripts.
10723
10724 @menu
10725 * Guile Types::                 Converting Guile types to @code{make} strings.
10726 * Guile Interface::             Invoking @code{make} functions from Guile.
10727 * Guile Example::               Example using Guile in @code{make}.
10728 @end menu
10729
10730 @node Guile Types, Guile Interface, Guile Integration, Guile Integration
10731 @subsection Conversion of Guile Types
10732 @cindex convert guile types
10733 @cindex guile, conversion of types
10734 @cindex types, conversion of
10735
10736 There is only one ``data type'' in @code{make}: a string.  GNU Guile,
10737 on the other hand, provides a rich variety of different data types.
10738 An important aspect of the interface between @code{make} and GNU Guile
10739 is the conversion of Guile data types into @code{make} strings.
10740
10741 This conversion is relevant in two places: when a makefile invokes the
10742 @code{guile} function to evaluate a Guile expression, the result of
10743 that evaluation must be converted into a make string so it can be
10744 further evaluated by @code{make}.  And secondly, when a Guile script
10745 invokes one of the procedures exported by @code{make} the argument
10746 provided to the procedure must be converted into a string.
10747
10748 The conversion of Guile types into @code{make} strings is as below:
10749
10750 @table @code
10751 @item #f
10752 False is converted into the empty string: in @code{make} conditionals
10753 the empty string is considered false.
10754
10755 @item #t
10756 True is converted to the string @samp{#t}: in @code{make} conditionals
10757 any non-empty string is considered true.
10758
10759 @item symbol
10760 @item number
10761 A symbol or number is converted into the string representation of that
10762 symbol or number.
10763
10764 @item character
10765 A printable character is converted to the same character.
10766
10767 @item string
10768 A string containing only printable characters is converted to the same
10769 string.
10770
10771 @item list
10772 A list is converted recursively according to the above rules.  This
10773 implies that any structured list will be flattened (that is, a result
10774 of @samp{'(a b (c d) e)} will be converted to the @code{make} string
10775 @samp{a b c d e}).
10776
10777 @item other
10778 Any other Guile type results in an error.  In future versions of
10779 @code{make}, other Guile types may be converted.
10780
10781 @end table
10782
10783 The translation of @samp{#f} (to the empty string) and @samp{#t} (to
10784 the non-empty string @samp{#t}) is designed to allow you to use Guile
10785 boolean results directly as @code{make} boolean conditions.  For
10786 example:
10787
10788 @example
10789 $(if $(guile (access? "myfile" R_OK)),$(info myfile exists))
10790 @end example
10791
10792 As a consequence of these conversion rules you must consider the
10793 result of your Guile script, as that result will be converted into a
10794 string and parsed by @code{make}.  If there is no natural result for
10795 the script (that is, the script exists solely for its side-effects),
10796 you should add @samp{#f} as the final expression in order to avoid
10797 syntax errors in your makefile.
10798
10799 @node Guile Interface, Guile Example, Guile Types, Guile Integration
10800 @subsection Interfaces from Guile to @code{make}
10801 @cindex make interface to guile
10802 @cindex make procedures in guile
10803
10804 In addition to the @code{guile} function available in makefiles,
10805 @code{make} exposes some procedures for use in your Guile scripts.  At
10806 startup @code{make} creates a new Guile module, @code{gnu make}, and
10807 exports these procedures as public interfaces from that module:
10808
10809 @table @code
10810 @item gmk-expand
10811 @findex gmk-expand
10812 This procedure takes a single argument which is converted into a
10813 string.  The string is expanded by @code{make} using normal
10814 @code{make} expansion rules.  The result of the expansion is converted
10815 into a Guile string and provided as the result of the procedure.
10816
10817 @item gmk-eval
10818 @findex gmk-eval
10819 This procedure takes a single argument which is converted into a
10820 string.  The string is evaluated by @code{make} as if it were a
10821 makefile.  This is the same capability available via the @code{eval}
10822 function (@pxref{Eval Function}).  The result of the @code{gmk-eval}
10823 procedure is always the empty string.
10824
10825 Note that @code{gmk-eval} is not quite the same as using
10826 @code{gmk-expand} with the @code{eval} function: in the latter case
10827 the evaluated string will be expanded @emph{twice}; first by
10828 @code{gmk-expand}, then again by the @code{eval} function.
10829
10830 @end table
10831
10832 @node Guile Example,  , Guile Interface, Guile Integration
10833 @subsection Example Using Guile in @code{make}
10834 @cindex Guile example
10835 @cindex example using Guile
10836
10837 Here is a very simple example using GNU Guile to manage writing to a
10838 file.  These Guile procedures simply open a file, allow writing to the
10839 file (one string per line), and close the file.  Note that because we
10840 cannot store complex values such as Guile ports in @code{make}
10841 variables, we'll keep the port as a global variable in the Guile
10842 interpreter.
10843
10844 You can create Guile functions easily using @code{define}/@code{endef}
10845 to create a Guile script, then use the @code{guile} function to
10846 internalize it:
10847
10848 @example
10849 @group
10850 define GUILEIO
10851 ;; A simple Guile IO library for GNU make
10852
10853 (define MKPORT #f)
10854
10855 (define (mkopen name mode)
10856   (set! MKPORT (open-file name mode))
10857   #f)
10858
10859 (define (mkwrite s)
10860   (display s MKPORT)
10861   (newline MKPORT)
10862   #f)
10863
10864 (define (mkclose)
10865   (close-port MKPORT)
10866   #f)
10867
10868 #f
10869 endef
10870
10871 # Internalize the Guile IO functions
10872 $(guile $(GUILEIO))
10873 @end group
10874 @end example
10875
10876 If you have a significant amount of Guile support code, you might
10877 consider keeping it in a different file (e.g., @file{guileio.scm}) and
10878 then loading it in your makefile using the @code{guile} function:
10879
10880 @example
10881 $(guile (load "guileio.scm"))
10882 @end example
10883
10884 An advantage to this method is that when editing @file{guileio.scm},
10885 your editor will understand that this file contains Scheme syntax
10886 rather than makefile syntax.
10887
10888 Now you can use these Guile functions to create files.  Suppose you
10889 need to operate on a very large list, which cannot fit on the command
10890 line, but the utility you're using accepts the list as input as well:
10891
10892 @example
10893 @group
10894 prog: $(PREREQS)
10895         @@$(guile (mkopen "tmp.out" "w")) \
10896          $(foreach X,$^,$(guile (mkwrite "$(X)"))) \
10897          $(guile (mkclose))
10898         $(LINK) < tmp.out
10899 @end group
10900 @end example
10901
10902 A more comprehensive suite of file manipulation procedures is possible
10903 of course.  You could, for example, maintain multiple output files at
10904 the same time by choosing a symbol for each one and using it as the
10905 key to a hash table, where the value is a port, then returning the
10906 symbol to be stored in a @code{make} variable.
10907
10908 @node Loading Objects,  , Guile Integration, Extending make
10909 @section Loading Dynamic Objects
10910 @cindex loaded objects
10911 @cindex objects, loaded
10912 @cindex extensions, loading
10913
10914 @cartouche
10915 @quotation Warning
10916 The @code{load} directive and extension capability is considered a
10917 ``technology preview'' in this release of GNU make.  We encourage you
10918 to experiment with this feature and we appreciate any feedback on it.
10919 However we cannot guarantee to maintain backward-compatibility in the
10920 next release.  Consider using GNU Guile instead for extending GNU make
10921 (@pxref{Guile Function, ,The @code{guile} Function}).
10922 @end quotation
10923 @end cartouche
10924
10925 Many operating systems provide a facility for dynamically loading
10926 compiled objects.  If your system provides this facility, GNU
10927 @code{make} can make use of it to load dynamic objects at runtime,
10928 providing new capabilities which may then be invoked by your makefile.
10929
10930 The @code{load} directive is used to load a dynamic object.  Once the
10931 object is loaded, a ``setup'' function will be invoked to allow the
10932 object to initialize itself and register new facilities with GNU
10933 @code{make}.  A dynamic object might include new @code{make} functions,
10934 for example, and the ``setup'' function would register them with GNU
10935 @code{make}'s function handling system.
10936
10937 @menu
10938 * load Directive::              Loading dynamic objects as extensions.
10939 * Remaking Loaded Objects::     How loaded objects get remade.
10940 * Loaded Object API::           Programmatic interface for loaded objects.
10941 * Loaded Object Example::       Example of a loaded object
10942 @end menu
10943
10944 @node load Directive, Remaking Loaded Objects, Loading Objects, Loading Objects
10945 @subsection The @code{load} Directive
10946 @cindex load directive
10947 @cindex extensions, load directive
10948
10949 Objects are loaded into GNU @code{make} by placing the @code{load}
10950 directive into your makefile.  The syntax of the @code{load} directive
10951 is as follows:
10952
10953 @findex load
10954 @example
10955 load @var{object-file} @dots{}
10956 @end example
10957
10958 or:
10959
10960 @example
10961 load @var{object-file}(@var{symbol-name}) @dots{}
10962 @end example
10963
10964 The file @var{object-file} is dynamically loaded by GNU @code{make}.
10965 If @var{object-file} does not include a directory path then it is
10966 first looked for in the current directory.  If it is not found there,
10967 or a directory path is included, then system-specific paths will be
10968 searched.  If the load fails for any reason, @code{make} will print a
10969 message and exit.
10970
10971 If the load succeeds @code{make} will invoke an initializing function.
10972
10973 If @var{symbol-name} is provided, it will be used as the name of the
10974 initializing function.
10975
10976 If no @var{symbol-name} is provided, the initializing function name is
10977 created by taking the base file name of @var{object-file}, up to the
10978 first character which is not a valid symbol name character
10979 (alphanumerics and underscores are valid symbol name characters).  To
10980 this prefix will be appended the suffix @code{_gmk_setup}.
10981
10982 More than one object file may be loaded with a single @code{load}
10983 directive, and both forms of @code{load} arguments may be used in the
10984 same directive.
10985
10986 The initializing function will be provided the file name and line
10987 number of the invocation of the @code{load} operation.  It should
10988 return a value of type @code{int}, which must be @code{0} on failure
10989 and non-@code{0} on success.  If the return value is @code{-1}, then
10990 GNU make will @emph{not} attempt to rebuild the object file
10991 (@pxref{Remaking Loaded Objects, ,How Loaded Objects Are Remade}).
10992
10993 For example:
10994
10995 @example
10996 load ../mk_funcs.so
10997 @end example
10998
10999 will load the dynamic object @file{../mk_funcs.so}.  After the object
11000 is loaded, @code{make} will invoke the function (assumed to be defined
11001 by the shared object) @code{mk_funcs_gmk_setup}.
11002
11003 On the other hand:
11004
11005 @example
11006 load ../mk_funcs.so(init_mk_func)
11007 @end example
11008
11009 will load the dynamic object @file{../mk_funcs.so}.  After the object
11010 is loaded, @code{make} will invoke the function @code{init_mk_func}.
11011
11012 Regardless of how many times an object file appears in a @code{load}
11013 directive, it will only be loaded (and its setup function will only
11014 be invoked) once.
11015
11016 @vindex .LOADED
11017 After an object has been successfully loaded, its file name is
11018 appended to the @code{.LOADED} variable.
11019
11020 @findex -load
11021 If you would prefer that failure to load a dynamic object not be
11022 reported as an error, you can use the @code{-load} directive instead
11023 of @code{load}.  GNU @code{make} will not fail and no message will be
11024 generated if an object fails to load.  The failed object is not added
11025 to the @code{.LOADED} variable, which can then be consulted to
11026 determine if the load was successful.
11027
11028 @node Remaking Loaded Objects, Loaded Object API, load Directive, Loading Objects
11029 @subsection How Loaded Objects Are Remade
11030 @cindex updating loaded objects
11031 @cindex remaking loaded objects
11032 @cindex loaded objects, remaking of
11033
11034 Loaded objects undergo the same re-make procedure as makefiles
11035 (@pxref{Remaking Makefiles, ,How Makefiles Are Remade}).  If any
11036 loaded object is recreated, then @code{make} will start from scratch
11037 and re-read all the makefiles, and reload the object files again.  It
11038 is not necessary for the loaded object to do anything special to
11039 support this.@refill
11040
11041 It's up to the makefile author to provide the rules needed for
11042 rebuilding the loaded object.
11043
11044 @node Loaded Object API, Loaded Object Example, Remaking Loaded Objects, Loading Objects
11045 @subsection Loaded Object Interface
11046 @cindex loaded object API
11047 @cindex interface for loaded objects
11048
11049 @cartouche
11050 @quotation Warning
11051 For this feature to be useful your extensions will need to invoke
11052 various functions internal to GNU @code{make}.  The programming
11053 interfaces provided in this release should not be considered stable:
11054 functions may be added, removed, or change calling signatures or
11055 implementations in future versions of GNU @code{make}.
11056 @end quotation
11057 @end cartouche
11058
11059 To be useful, loaded objects must be able to interact with GNU
11060 @code{make}.  This interaction includes both interfaces the loaded
11061 object provides to makefiles and also interfaces @code{make} provides
11062 to the loaded object to manipulate @code{make}'s operation.
11063
11064 The interface between loaded objects and @code{make} is defined by the
11065 @file{gnumake.h} C header file.  All loaded objects written in C
11066 should include this header file.  Any loaded object not written in C
11067 will need to implement the interface defined in this header file.
11068
11069 Typically, a loaded object will register one or more new GNU
11070 @code{make} functions using the @code{gmk_add_function} routine from
11071 within its setup function.  The implementations of these @code{make}
11072 functions may make use of the @code{gmk_expand} and @code{gmk_eval}
11073 routines to perform their tasks, then optionally return a string as
11074 the result of the function expansion.
11075
11076 @subsubheading Loaded Object Licensing
11077 @cindex loaded object licensing
11078 @cindex plugin_is_GPL_compatible
11079
11080 Every dynamic extension should define the global symbol
11081 @code{plugin_is_GPL_compatible} to assert that it has been licensed
11082 under a GPL-compatible license.  If this symbol does not exist,
11083 @code{make} emits a fatal error and exits when it tries to load your
11084 extension.
11085
11086 The declared type of the symbol should be @code{int}. It does not need
11087 to be in any allocated section, though.  The code merely asserts that
11088 the symbol exists in the global scope. Something like this is enough:
11089
11090 @example
11091 int plugin_is_GPL_compatible;
11092 @end example
11093
11094 @subsubheading Data Structures
11095
11096 @table @code
11097 @item gmk_floc
11098 This structure represents a filename/location pair.  It is provided
11099 when defining items, so GNU @code{make} can inform the user later
11100 where the definition occurred if necessary.
11101 @end table
11102
11103 @subsubheading Registering Functions
11104 @findex gmk_add_function
11105
11106 There is currently one way for makefiles to invoke operations provided
11107 by the loaded object: through the @code{make} function call
11108 interface.  A loaded object can register one or more new functions
11109 which may then be invoked from within the makefile in the same way as
11110 any other function.
11111
11112 Use @code{gmk_add_function} to create a new @code{make} function.  Its
11113 arguments are as follows:
11114
11115 @table @code
11116 @item name
11117 The function name.  This is what the makefile should use to invoke the
11118 function.  The name must be between 1 and 255 characters long and it
11119 may only contain alphanumeric, period (@samp{.}), dash (@samp{-}), and
11120 underscore (@samp{_}) characters.  It may not begin with a period.
11121
11122 @item func_ptr
11123 A pointer to a function that @code{make} will invoke when it expands
11124 the function in a makefile.  This function must be defined by the
11125 loaded object.
11126
11127 @item min_args
11128 The minimum number of arguments the function will accept.  Must be
11129 between 0 and 255.  GNU @code{make} will check this and fail before
11130 invoking @code{func_ptr} if the function was invoked with too few
11131 arguments.
11132
11133 @item max_args
11134 The maximum number of arguments the function will accept.  Must be
11135 between 0 and 255.  GNU @code{make} will check this and fail before
11136 invoking @code{func_ptr} if the function was invoked with too few
11137 arguments.  If the value is 0, then any number of arguments is
11138 accepted.  If the value is greater than 0, then it must be greater
11139 than or equal to @code{min_args}.
11140
11141 @item flags
11142 Flags that specify how this function will operate; the desired flags
11143 should be OR'd together.  If the @code{GMK_FUNC_NOEXPAND} flag is
11144 given then the function arguments will not be expanded before the
11145 function is called; otherwise they will be expanded first.
11146 @end table
11147
11148 @subsubheading Registered Function Interface
11149 @findex gmk_func_ptr
11150
11151 A function registered with @code{make} must match the
11152 @code{gmk_func_ptr} type.  It will be invoked with three parameters:
11153 @code{name} (the name of the function), @code{argc} (the number of
11154 arguments to the function), and @code{argv} (an array of pointers to
11155 arguments to the function).  The last pointer (that is,
11156 @code{argv[argc]}) will be null (@code{0}).
11157
11158 The return value of the function is the result of expanding the
11159 function.  If the function expands to nothing the return value may be
11160 null.  Otherwise, it must be a pointer to a string created with
11161 @code{gmk_alloc}.  Once the function returns, @code{make} owns this
11162 string and will free it when appropriate; it cannot be accessed by the
11163 loaded object.
11164
11165 @subsubheading GNU @code{make} Facilities
11166
11167 There are some facilities exported by GNU @code{make} for use by
11168 loaded objects.  Typically these would be run from within the
11169 setup function and/or the functions registered via
11170 @code{gmk_add_function}, to retrieve or modify the data @code{make}
11171 works with.
11172
11173 @table @code
11174 @item gmk_expand
11175 @findex gmk_expand
11176 This function takes a string and expands it using @code{make}
11177 expansion rules.  The result of the expansion is returned in a
11178 nil-terminated string buffer.  The caller is responsible for calling
11179 @code{gmk_free} with a pointer to the returned buffer when done.
11180
11181 @item gmk_eval
11182 @findex gmk_eval
11183 This function takes a buffer and evaluates it as a segment of makefile
11184 syntax.  This function can be used to define new variables, new rules,
11185 etc.  It is equivalent to using the @code{eval} @code{make} function.
11186 @end table
11187
11188 Note that there is a difference between @code{gmk_eval} and calling
11189 @code{gmk_expand} with a string using the @code{eval} function: in
11190 the latter case the string will be expanded @emph{twice}; once by
11191 @code{gmk_expand} and then again by the @code{eval} function.  Using
11192 @code{gmk_eval} the buffer is only expanded once, at most (as it's
11193 read by the @code{make} parser).
11194
11195 @subsubheading Memory Management
11196
11197 Some systems allow for different memory management schemes.  Thus you
11198 should never pass memory that you've allocated directly to any
11199 @code{make} function, nor should you attempt to directly free any
11200 memory returned to you by any @code{make} function.  Instead, use the
11201 @code{gmk_alloc} and @code{gmk_free} functions.
11202
11203 In particular, the string returned to @code{make} by a function
11204 registered using @code{gmk_add_function} @emph{must} be allocated
11205 using @code{gmk_alloc}, and the string returned from the @code{make}
11206 @code{gmk_expand} function @emph{must} be freed (when no longer
11207 needed) using @code{gmk_free}.
11208
11209 @table @code
11210 @item gmk_alloc
11211 @findex gmk_alloc
11212 Return a pointer to a newly-allocated buffer.  This function will
11213 always return a valid pointer; if not enough memory is available
11214 @code{make} will exit.
11215
11216 @item gmk_free
11217 @findex gmk_free
11218 Free a buffer returned to you by @code{make}.  Once the
11219 @code{gmk_free} function returns the string will no longer be valid.
11220 @end table
11221
11222 @node Loaded Object Example,  , Loaded Object API, Loading Objects
11223 @subsection Example Loaded Object
11224 @cindex loaded object example
11225 @cindex example of loaded objects
11226
11227 Let's suppose we wanted to write a new GNU @code{make} function that
11228 would create a temporary file and return its name.  We would like our
11229 function to take a prefix as an argument.  First we can write the
11230 function in a file @file{mk_temp.c}:
11231
11232 @example
11233 @group
11234 #include <stdlib.h>
11235 #include <stdlib.h>
11236 #include <stdio.h>
11237 #include <string.h>
11238 #include <unistd.h>
11239 #include <errno.h>
11240
11241 #include <gnumake.h>
11242
11243 int plugin_is_GPL_compatible;
11244
11245 char *
11246 gen_tmpfile(const char *nm, int argc, char **argv)
11247 @{
11248   int fd;
11249
11250   /* Compute the size of the filename and allocate space for it.  */
11251   int len = strlen (argv[0]) + 6 + 1;
11252   char *buf = gmk_alloc (len);
11253
11254   strcpy (buf, argv[0]);
11255   strcat (buf, "XXXXXX");
11256
11257   fd = mkstemp(buf);
11258   if (fd >= 0)
11259     @{
11260       /* Don't leak the file descriptor.  */
11261       close (fd);
11262       return buf;
11263     @}
11264
11265   /* Failure.  */
11266   fprintf (stderr, "mkstemp(%s) failed: %s\n", buf, strerror (errno));
11267   gmk_free (buf);
11268   return NULL;
11269 @}
11270
11271 int
11272 mk_temp_gmk_setup ()
11273 @{
11274   /* Register the function with make name "mk-temp".  */
11275   gmk_add_function ("mk-temp", gen_tmpfile, 1, 1, 1);
11276   return 1;
11277 @}
11278 @end group
11279 @end example
11280
11281 Next, we will write a makefile that can build this shared object, load
11282 it, and use it:
11283
11284 @example
11285 @group
11286 all:
11287         @@echo Temporary file: $(mk-temp tmpfile.)
11288
11289 load mk_temp.so
11290
11291 mk_temp.so: mk_temp.c
11292         $(CC) -shared -fPIC -o $@ $<
11293 @end group
11294 @end example
11295
11296 On MS-Windows, due to peculiarities of how shared objects are
11297 produced, the compiler needs to scan the @dfn{import library} produced
11298 when building @code{make}, typically called
11299 @file{libgnumake-@var{version}.dll.a}, where @var{version} is the
11300 version of the load object API.  So the recipe to produce a shared
11301 object will look on Windows like this (assuming the API version is 1):
11302
11303 @example
11304 @group
11305 mk_temp.dll: mk_temp.c
11306         $(CC) -shared -o $@ $< -lgnumake-1
11307 @end group
11308 @end example
11309
11310 Now when you run @code{make} you'll see something like:
11311
11312 @example
11313 $ make
11314 cc -shared -fPIC -o mk_temp.so mk_temp.c
11315 Temporary filename: tmpfile.A7JEwd
11316 @end example
11317
11318 @node Features, Missing, Extending make, Top
11319 @chapter Features of GNU @code{make}
11320 @cindex features of GNU @code{make}
11321 @cindex portability
11322 @cindex compatibility
11323
11324 Here is a summary of the features of GNU @code{make}, for comparison
11325 with and credit to other versions of @code{make}.  We consider the
11326 features of @code{make} in 4.2 BSD systems as a baseline.  If you are
11327 concerned with writing portable makefiles, you should not use the
11328 features of @code{make} listed here, nor the ones in @ref{Missing}.
11329
11330 Many features come from the version of @code{make} in System V.
11331
11332 @itemize @bullet
11333 @item
11334 The @code{VPATH} variable and its special meaning.
11335 @xref{Directory Search, , Searching Directories for Prerequisites}.
11336 This feature exists in System V @code{make}, but is undocumented.
11337 It is documented in 4.3 BSD @code{make} (which says it mimics System V's
11338 @code{VPATH} feature).@refill
11339
11340 @item
11341 Included makefiles.  @xref{Include, ,Including Other Makefiles}.
11342 Allowing multiple files to be included with a single directive is a GNU
11343 extension.
11344
11345 @item
11346 Variables are read from and communicated via the environment.
11347 @xref{Environment, ,Variables from the Environment}.
11348
11349 @item
11350 Options passed through the variable @code{MAKEFLAGS} to recursive
11351 invocations of @code{make}.
11352 @xref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
11353
11354 @item
11355 The automatic variable @code{$%} is set to the member name
11356 in an archive reference.  @xref{Automatic Variables}.
11357
11358 @item
11359 The automatic variables @code{$@@}, @code{$*}, @code{$<}, @code{$%},
11360 and @code{$?} have corresponding forms like @code{$(@@F)} and
11361 @code{$(@@D)}.  We have generalized this to @code{$^} as an obvious
11362 extension.  @xref{Automatic Variables}.@refill
11363
11364 @item
11365 Substitution variable references.
11366 @xref{Reference, ,Basics of Variable References}.
11367
11368 @item
11369 The command line options @samp{-b} and @samp{-m}, accepted and
11370 ignored.  In System V @code{make}, these options actually do something.
11371
11372 @item
11373 Execution of recursive commands to run @code{make} via the variable
11374 @code{MAKE} even if @samp{-n}, @samp{-q} or @samp{-t} is specified.
11375 @xref{Recursion, ,Recursive Use of @code{make}}.
11376
11377 @item
11378 Support for suffix @samp{.a} in suffix rules.  @xref{Archive Suffix
11379 Rules}.  This feature is obsolete in GNU @code{make}, because the
11380 general feature of rule chaining (@pxref{Chained Rules, ,Chains of
11381 Implicit Rules}) allows one pattern rule for installing members in an
11382 archive (@pxref{Archive Update}) to be sufficient.
11383
11384 @item
11385 The arrangement of lines and backslash/newline combinations in
11386 recipes is retained when the recipes are printed, so they appear as
11387 they do in the makefile, except for the stripping of initial
11388 whitespace.
11389 @end itemize
11390
11391 The following features were inspired by various other versions of
11392 @code{make}.  In some cases it is unclear exactly which versions inspired
11393 which others.
11394
11395 @itemize @bullet
11396 @item
11397 Pattern rules using @samp{%}.
11398 This has been implemented in several versions of @code{make}.
11399 We're not sure who invented it first, but it's been spread around a bit.
11400 @xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.@refill
11401
11402 @item
11403 Rule chaining and implicit intermediate files.
11404 This was implemented by Stu Feldman in his version of @code{make}
11405 for AT&T Eighth Edition Research Unix, and later by Andrew Hume of
11406 AT&T Bell Labs in his @code{mk} program (where he terms it
11407 ``transitive closure'').  We do not really know if
11408 we got this from either of them or thought it up ourselves at the
11409 same time.  @xref{Chained Rules, ,Chains of Implicit Rules}.
11410
11411 @item
11412 The automatic variable @code{$^} containing a list of all prerequisites
11413 of the current target.  We did not invent this, but we have no idea who
11414 did.  @xref{Automatic Variables}.  The automatic variable
11415 @code{$+} is a simple extension of @code{$^}.
11416
11417 @item
11418 The ``what if'' flag (@samp{-W} in GNU @code{make}) was (as far as we know)
11419 invented by Andrew Hume in @code{mk}.
11420 @xref{Instead of Execution, ,Instead of Executing Recipes}.
11421
11422 @item
11423 The concept of doing several things at once (parallelism) exists in
11424 many incarnations of @code{make} and similar programs, though not in the
11425 System V or BSD implementations.  @xref{Execution, ,Recipe Execution}.
11426
11427 @item
11428 A number of different build tools that support parallelism also
11429 support collecting output and displaying as a single block.
11430 @xref{Parallel Output, ,Output During Parallel Execution}.
11431
11432 @item
11433 Modified variable references using pattern substitution come from
11434 SunOS 4.  @xref{Reference, ,Basics of Variable References}.
11435 This functionality was provided in GNU @code{make} by the
11436 @code{patsubst} function before the alternate syntax was implemented
11437 for compatibility with SunOS 4.  It is not altogether clear who
11438 inspired whom, since GNU @code{make} had @code{patsubst} before SunOS
11439 4 was released.@refill
11440
11441 @item
11442 The special significance of @samp{+} characters preceding recipe lines
11443 (@pxref{Instead of Execution, ,Instead of Executing Recipes}) is
11444 mandated by @cite{IEEE Standard 1003.2-1992} (POSIX.2).
11445
11446 @item
11447 The @samp{+=} syntax to append to the value of a variable comes from SunOS
11448 4 @code{make}.  @xref{Appending, , Appending More Text to Variables}.
11449
11450 @item
11451 The syntax @w{@samp{@var{archive}(@var{mem1} @var{mem2}@dots{})}} to list
11452 multiple members in a single archive file comes from SunOS 4 @code{make}.
11453 @xref{Archive Members}.
11454
11455 @item
11456 The @code{-include} directive to include makefiles with no error for a
11457 nonexistent file comes from SunOS 4 @code{make}.  (But note that SunOS 4
11458 @code{make} does not allow multiple makefiles to be specified in one
11459 @code{-include} directive.)  The same feature appears with the name
11460 @code{sinclude} in SGI @code{make} and perhaps others.
11461
11462 @item
11463 The @code{!=} shell assignment operator exists in many BSD of
11464 @code{make} and is purposefully implemented here to behave identically
11465 to those implementations.
11466
11467 @item
11468 Various build management tools are implemented using scripting
11469 languages such as Perl or Python and thus provide a natural embedded
11470 scripting language, similar to GNU @code{make}'s integration of GNU
11471 Guile.
11472 @end itemize
11473
11474 The remaining features are inventions new in GNU @code{make}:
11475
11476 @itemize @bullet
11477 @item
11478 Use the @samp{-v} or @samp{--version} option to print version and
11479 copyright information.
11480
11481 @item
11482 Use the @samp{-h} or @samp{--help} option to summarize the options to
11483 @code{make}.
11484
11485 @item
11486 Simply-expanded variables.  @xref{Flavors, ,The Two Flavors of Variables}.
11487
11488 @item
11489 Pass command line variable assignments automatically through the
11490 variable @code{MAKE} to recursive @code{make} invocations.
11491 @xref{Recursion, ,Recursive Use of @code{make}}.
11492
11493 @item
11494 Use the @samp{-C} or @samp{--directory} command option to change
11495 directory.  @xref{Options Summary, ,Summary of Options}.
11496
11497 @item
11498 Make verbatim variable definitions with @code{define}.
11499 @xref{Multi-Line, ,Defining Multi-Line Variables}.
11500
11501 @item
11502 Declare phony targets with the special target @code{.PHONY}.
11503
11504 Andrew Hume of AT&T Bell Labs implemented a similar feature with a
11505 different syntax in his @code{mk} program.  This seems to be a case of
11506 parallel discovery.  @xref{Phony Targets, ,Phony Targets}.
11507
11508 @item
11509 Manipulate text by calling functions.
11510 @xref{Functions, ,Functions for Transforming Text}.
11511
11512 @item
11513 Use the @samp{-o} or @samp{--old-file}
11514 option to pretend a file's modification-time is old.
11515 @xref{Avoiding Compilation, ,Avoiding Recompilation of Some Files}.
11516
11517 @item
11518 Conditional execution.
11519
11520 This feature has been implemented numerous times in various versions
11521 of @code{make}; it seems a natural extension derived from the features
11522 of the C preprocessor and similar macro languages and is not a
11523 revolutionary concept.  @xref{Conditionals, ,Conditional Parts of Makefiles}.
11524
11525 @item
11526 Specify a search path for included makefiles.
11527 @xref{Include, ,Including Other Makefiles}.
11528
11529 @item
11530 Specify extra makefiles to read with an environment variable.
11531 @xref{MAKEFILES Variable, ,The Variable @code{MAKEFILES}}.
11532
11533 @item
11534 Strip leading sequences of @samp{./} from file names, so that
11535 @file{./@var{file}} and @file{@var{file}} are considered to be the
11536 same file.@refill
11537
11538 @item
11539 Use a special search method for library prerequisites written in the
11540 form @samp{-l@var{name}}.
11541 @xref{Libraries/Search, ,Directory Search for Link Libraries}.
11542
11543 @item
11544 Allow suffixes for suffix rules
11545 (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}) to contain any
11546 characters.  In other versions of @code{make}, they must begin with
11547 @samp{.} and not contain any @samp{/} characters.
11548
11549 @item
11550 Keep track of the current level of @code{make} recursion using the
11551 variable @code{MAKELEVEL}.  @xref{Recursion, ,Recursive Use of @code{make}}.
11552
11553 @item
11554 Provide any goals given on the command line in the variable
11555 @code{MAKECMDGOALS}.  @xref{Goals, ,Arguments to Specify the Goals}.
11556
11557 @item
11558 Specify static pattern rules.  @xref{Static Pattern, ,Static Pattern Rules}.
11559
11560 @item
11561 Provide selective @code{vpath} search.
11562 @xref{Directory Search, ,Searching Directories for Prerequisites}.
11563
11564 @item
11565 Provide computed variable references.
11566 @xref{Reference, ,Basics of Variable References}.
11567
11568 @item
11569 Update makefiles.  @xref{Remaking Makefiles, ,How Makefiles Are Remade}.
11570 System V @code{make} has a very, very limited form of this
11571 functionality in that it will check out SCCS files for makefiles.
11572
11573 @item
11574 Various new built-in implicit rules.
11575 @xref{Catalogue of Rules, ,Catalogue of Implicit Rules}.
11576
11577 @item
11578 Load dynamic objects which can modify the behavior of @code{make}.
11579 @xref{Loading Objects, ,Loading Dynamic Objects}.
11580 @end itemize
11581
11582 @node Missing, Makefile Conventions, Features, Top
11583 @chapter Incompatibilities and Missing Features
11584 @cindex incompatibilities
11585 @cindex missing features
11586 @cindex features, missing
11587
11588 The @code{make} programs in various other systems support a few features
11589 that are not implemented in GNU @code{make}.  The POSIX.2 standard
11590 (@cite{IEEE Standard 1003.2-1992}) which specifies @code{make} does not
11591 require any of these features.@refill
11592
11593 @itemize @bullet
11594 @item
11595 A target of the form @samp{@var{file}((@var{entry}))} stands for a member
11596 of archive file @var{file}.  The member is chosen, not by name, but by
11597 being an object file which defines the linker symbol @var{entry}.@refill
11598
11599 This feature was not put into GNU @code{make} because of the
11600 non-modularity of putting knowledge into @code{make} of the internal
11601 format of archive file symbol tables.
11602 @xref{Archive Symbols, ,Updating Archive Symbol Directories}.
11603
11604 @item
11605 Suffixes (used in suffix rules) that end with the character @samp{~}
11606 have a special meaning to System V @code{make};
11607 they refer to the SCCS file that corresponds
11608 to the file one would get without the @samp{~}.  For example, the
11609 suffix rule @samp{.c~.o} would make the file @file{@var{n}.o} from
11610 the SCCS file @file{s.@var{n}.c}.  For complete coverage, a whole
11611 series of such suffix rules is required.
11612 @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
11613
11614 In GNU @code{make}, this entire series of cases is handled by two
11615 pattern rules for extraction from SCCS, in combination with the
11616 general feature of rule chaining.
11617 @xref{Chained Rules, ,Chains of Implicit Rules}.
11618
11619 @item
11620 In System V and 4.3 BSD @code{make}, files found by @code{VPATH}
11621 search (@pxref{Directory Search, ,Searching Directories for
11622 Prerequisites}) have their names changed inside recipes.  We feel it
11623 is much cleaner to always use automatic variables and thus make this
11624 feature obsolete.@refill
11625
11626 @item
11627 In some Unix @code{make}s, the automatic variable @code{$*} appearing in
11628 the prerequisites of a rule has the amazingly strange ``feature'' of
11629 expanding to the full name of the @emph{target of that rule}.  We cannot
11630 imagine what went on in the minds of Unix @code{make} developers to do
11631 this; it is utterly inconsistent with the normal definition of @code{$*}.
11632 @vindex * @r{(automatic variable), unsupported bizarre usage}
11633
11634 @item
11635 In some Unix @code{make}s, implicit rule search (@pxref{Implicit
11636 Rules, ,Using Implicit Rules}) is apparently done for @emph{all}
11637 targets, not just those without recipes.  This means you can
11638 do:@refill
11639
11640 @example
11641 @group
11642 foo.o:
11643         cc -c foo.c
11644 @end group
11645 @end example
11646
11647 @noindent
11648 and Unix @code{make} will intuit that @file{foo.o} depends on
11649 @file{foo.c}.@refill
11650
11651 We feel that such usage is broken.  The prerequisite properties of
11652 @code{make} are well-defined (for GNU @code{make}, at least),
11653 and doing such a thing simply does not fit the model.@refill
11654
11655 @item
11656 GNU @code{make} does not include any built-in implicit rules for
11657 compiling or preprocessing EFL programs.  If we hear of anyone who is
11658 using EFL, we will gladly add them.
11659
11660 @item
11661 It appears that in SVR4 @code{make}, a suffix rule can be specified
11662 with no recipe, and it is treated as if it had an empty recipe
11663 (@pxref{Empty Recipes}).  For example:
11664
11665 @example
11666 .c.a:
11667 @end example
11668
11669 @noindent
11670 will override the built-in @file{.c.a} suffix rule.
11671
11672 We feel that it is cleaner for a rule without a recipe to always simply
11673 add to the prerequisite list for the target.  The above example can be
11674 easily rewritten to get the desired behavior in GNU @code{make}:
11675
11676 @example
11677 .c.a: ;
11678 @end example
11679
11680 @item
11681 Some versions of @code{make} invoke the shell with the @samp{-e} flag,
11682 except under @samp{-k} (@pxref{Testing, ,Testing the Compilation of a
11683 Program}).  The @samp{-e} flag tells the shell to exit as soon as any
11684 program it runs returns a nonzero status.  We feel it is cleaner to
11685 write each line of the recipe to stand on its own and not require this
11686 special treatment.
11687 @end itemize
11688
11689 @comment The makefile standards are in a separate file that is also
11690 @comment included by standards.texi.
11691 @include make-stds.texi
11692
11693 @node Quick Reference, Error Messages, Makefile Conventions, Top
11694 @appendix Quick Reference
11695
11696 This appendix summarizes the directives, text manipulation functions,
11697 and special variables which GNU @code{make} understands.
11698 @xref{Special Targets}, @ref{Catalogue of Rules, ,Catalogue of Implicit Rules},
11699 and @ref{Options Summary, ,Summary of Options},
11700 for other summaries.
11701
11702 Here is a summary of the directives GNU @code{make} recognizes:
11703
11704 @table @code
11705 @item define @var{variable}
11706 @itemx define @var{variable} =
11707 @itemx define @var{variable} :=
11708 @itemx define @var{variable} ::=
11709 @itemx define @var{variable} +=
11710 @itemx define @var{variable} ?=
11711 @itemx endef
11712 Define multi-line variables.@*
11713 @xref{Multi-Line}.
11714
11715 @item undefine @var{variable}
11716 Undefining variables.@*
11717 @xref{Undefine Directive}.
11718
11719 @item ifdef @var{variable}
11720 @itemx ifndef @var{variable}
11721 @itemx ifeq (@var{a},@var{b})
11722 @itemx ifeq "@var{a}" "@var{b}"
11723 @itemx ifeq '@var{a}' '@var{b}'
11724 @itemx ifneq (@var{a},@var{b})
11725 @itemx ifneq "@var{a}" "@var{b}"
11726 @itemx ifneq '@var{a}' '@var{b}'
11727 @itemx else
11728 @itemx endif
11729 Conditionally evaluate part of the makefile.@*
11730 @xref{Conditionals}.
11731
11732 @item include @var{file}
11733 @itemx -include @var{file}
11734 @itemx sinclude @var{file}
11735 Include another makefile.@*
11736 @xref{Include, ,Including Other Makefiles}.
11737
11738 @item override @var{variable-assignment}
11739 Define a variable, overriding any previous definition, even one from
11740 the command line.@*
11741 @xref{Override Directive, ,The @code{override} Directive}.
11742
11743 @item export
11744 Tell @code{make} to export all variables to child processes by default.@*
11745 @xref{Variables/Recursion, , Communicating Variables to a Sub-@code{make}}.
11746
11747 @item export @var{variable}
11748 @itemx export @var{variable-assignment}
11749 @itemx unexport @var{variable}
11750 Tell @code{make} whether or not to export a particular variable to child
11751 processes.@*
11752 @xref{Variables/Recursion, , Communicating Variables to a Sub-@code{make}}.
11753
11754 @item private @var{variable-assignment}
11755 Do not allow this variable assignment to be inherited by prerequisites.@*
11756 @xref{Suppressing Inheritance}.
11757
11758 @item vpath @var{pattern} @var{path}
11759 Specify a search path for files matching a @samp{%} pattern.@*
11760 @xref{Selective Search, , The @code{vpath} Directive}.
11761
11762 @item vpath @var{pattern}
11763 Remove all search paths previously specified for @var{pattern}.
11764
11765 @item vpath
11766 Remove all search paths previously specified in any @code{vpath}
11767 directive.
11768 @end table
11769
11770 Here is a summary of the built-in functions (@pxref{Functions}):
11771
11772 @table @code
11773 @item $(subst @var{from},@var{to},@var{text})
11774 Replace @var{from} with @var{to} in @var{text}.@*
11775 @xref{Text Functions, , Functions for String Substitution and Analysis}.
11776
11777 @item $(patsubst @var{pattern},@var{replacement},@var{text})
11778 Replace words matching @var{pattern} with @var{replacement} in @var{text}.@*
11779 @xref{Text Functions, , Functions for String Substitution and Analysis}.
11780
11781 @item $(strip @var{string})
11782 Remove excess whitespace characters from @var{string}.@*
11783 @xref{Text Functions, , Functions for String Substitution and Analysis}.
11784
11785 @item $(findstring @var{find},@var{text})
11786 Locate @var{find} in @var{text}.@*
11787 @xref{Text Functions, , Functions for String Substitution and Analysis}.
11788
11789 @item $(filter @var{pattern}@dots{},@var{text})
11790 Select words in @var{text} that match one of the @var{pattern} words.@*
11791 @xref{Text Functions, , Functions for String Substitution and Analysis}.
11792
11793 @item $(filter-out @var{pattern}@dots{},@var{text})
11794 Select words in @var{text} that @emph{do not} match any of the @var{pattern} words.@*
11795 @xref{Text Functions, , Functions for String Substitution and Analysis}.
11796
11797 @item $(sort @var{list})
11798 Sort the words in @var{list} lexicographically, removing duplicates.@*
11799 @xref{Text Functions, , Functions for String Substitution and Analysis}.
11800
11801 @item $(word @var{n},@var{text})
11802 Extract the @var{n}th word (one-origin) of @var{text}.@*
11803 @xref{Text Functions, , Functions for String Substitution and Analysis}.
11804
11805 @item $(words @var{text})
11806 Count the number of words in @var{text}.@*
11807 @xref{Text Functions, , Functions for String Substitution and Analysis}.
11808
11809 @item $(wordlist @var{s},@var{e},@var{text})
11810 Returns the list of words in @var{text} from @var{s} to @var{e}.@*
11811 @xref{Text Functions, , Functions for String Substitution and Analysis}.
11812
11813 @item $(firstword @var{names}@dots{})
11814 Extract the first word of @var{names}.@*
11815 @xref{Text Functions, , Functions for String Substitution and Analysis}.
11816
11817 @item $(lastword @var{names}@dots{})
11818 Extract the last word of @var{names}.@*
11819 @xref{Text Functions, , Functions for String Substitution and Analysis}.
11820
11821 @item $(dir @var{names}@dots{})
11822 Extract the directory part of each file name.@*
11823 @xref{File Name Functions, ,Functions for File Names}.
11824
11825 @item $(notdir @var{names}@dots{})
11826 Extract the non-directory part of each file name.@*
11827 @xref{File Name Functions, ,Functions for File Names}.
11828
11829 @item $(suffix @var{names}@dots{})
11830 Extract the suffix (the last @samp{.} and following characters) of each file name.@*
11831 @xref{File Name Functions, ,Functions for File Names}.
11832
11833 @item $(basename @var{names}@dots{})
11834 Extract the base name (name without suffix) of each file name.@*
11835 @xref{File Name Functions, ,Functions for File Names}.
11836
11837 @item $(addsuffix @var{suffix},@var{names}@dots{})
11838 Append @var{suffix} to each word in @var{names}.@*
11839 @xref{File Name Functions, ,Functions for File Names}.
11840
11841 @item $(addprefix @var{prefix},@var{names}@dots{})
11842 Prepend @var{prefix} to each word in @var{names}.@*
11843 @xref{File Name Functions, ,Functions for File Names}.
11844
11845 @item $(join @var{list1},@var{list2})
11846 Join two parallel lists of words.@*
11847 @xref{File Name Functions, ,Functions for File Names}.
11848
11849 @item $(wildcard @var{pattern}@dots{})
11850 Find file names matching a shell file name pattern (@emph{not} a
11851 @samp{%} pattern).@*
11852 @xref{Wildcard Function, ,The Function @code{wildcard}}.
11853
11854 @item $(realpath @var{names}@dots{})
11855 For each file name in @var{names}, expand to an absolute name that
11856 does not contain any @code{.}, @code{..}, nor symlinks.@*
11857 @xref{File Name Functions, ,Functions for File Names}.
11858
11859 @item $(abspath @var{names}@dots{})
11860 For each file name in @var{names}, expand to an absolute name that
11861 does not contain any @code{.} or @code{..} components, but preserves
11862 symlinks.@*
11863 @xref{File Name Functions, ,Functions for File Names}.
11864
11865 @item $(error @var{text}@dots{})
11866 When this function is evaluated, @code{make} generates a fatal error
11867 with the message @var{text}.@*
11868 @xref{Make Control Functions, ,Functions That Control Make}.
11869
11870 @item $(warning @var{text}@dots{})
11871 When this function is evaluated, @code{make} generates a warning with
11872 the message @var{text}.@*
11873 @xref{Make Control Functions, ,Functions That Control Make}.
11874
11875 @item $(shell @var{command})
11876 Execute a shell command and return its output.@*
11877 @xref{Shell Function, , The @code{shell} Function}.
11878
11879 @item $(origin @var{variable})
11880 Return a string describing how the @code{make} variable @var{variable} was
11881 defined.@*
11882 @xref{Origin Function, , The @code{origin} Function}.
11883
11884 @item $(flavor @var{variable})
11885 Return a string describing the flavor of the @code{make} variable
11886 @var{variable}.@*
11887 @xref{Flavor Function, , The @code{flavor} Function}.
11888
11889 @item $(foreach @var{var},@var{words},@var{text})
11890 Evaluate @var{text} with @var{var} bound to each word in @var{words},
11891 and concatenate the results.@*
11892 @xref{Foreach Function, ,The @code{foreach} Function}.
11893
11894 @item $(if @var{condition},@var{then-part}[,@var{else-part}])
11895 Evaluate the condition @var{condition}; if it's non-empty substitute
11896 the expansion of the @var{then-part} otherwise substitute the
11897 expansion of the @var{else-part}.@*
11898 @xref{Conditional Functions, ,Functions for Conditionals}.
11899
11900 @item $(or @var{condition1}[,@var{condition2}[,@var{condition3}@dots{}]])
11901 Evaluate each condition @var{conditionN} one at a time; substitute the
11902 first non-empty expansion.  If all expansions are empty, substitute
11903 the empty string.@*
11904 @xref{Conditional Functions, ,Functions for Conditionals}.
11905
11906 @item $(and @var{condition1}[,@var{condition2}[,@var{condition3}@dots{}]])
11907 Evaluate each condition @var{conditionN} one at a time; if any
11908 expansion results in the empty string substitute the empty string.  If
11909 all expansions result in a non-empty string, substitute the expansion
11910 of the last @var{condition}.@*
11911 @xref{Conditional Functions, ,Functions for Conditionals}.
11912
11913 @item $(call @var{var},@var{param},@dots{})
11914 Evaluate the variable @var{var} replacing any references to @code{$(1)},
11915 @code{$(2)} with the first, second, etc.@: @var{param} values.@*
11916 @xref{Call Function, ,The @code{call} Function}.
11917
11918 @item $(eval @var{text})
11919 Evaluate @var{text} then read the results as makefile commands.
11920 Expands to the empty string.@*
11921 @xref{Eval Function, ,The @code{eval} Function}.
11922
11923 @item $(file @var{op} @var{filename},@var{text})
11924 Expand the arguments, then open the file @var{filename} using mode
11925 @var{op} and write @var{text} to that file.@*
11926 @xref{File Function, ,The @code{file} Function}.
11927
11928 @item $(value @var{var})
11929 Evaluates to the contents of the variable @var{var}, with no expansion
11930 performed on it.@*
11931 @xref{Value Function, ,The @code{value} Function}.
11932 @end table
11933
11934 Here is a summary of the automatic variables.
11935 @xref{Automatic Variables},
11936 for full information.
11937
11938 @table @code
11939 @item $@@
11940 The file name of the target.
11941
11942 @item $%
11943 The target member name, when the target is an archive member.
11944
11945 @item $<
11946 The name of the first prerequisite.
11947
11948 @item $?
11949 The names of all the prerequisites that are
11950 newer than the target, with spaces between them.
11951 For prerequisites which are archive members, only
11952 the named member is used (@pxref{Archives}).
11953
11954 @item $^
11955 @itemx $+
11956 The names of all the prerequisites, with spaces between them.  For
11957 prerequisites which are archive members, only the named member is used
11958 (@pxref{Archives}).  The value of @code{$^} omits duplicate
11959 prerequisites, while @code{$+} retains them and preserves their order.
11960
11961 @item $*
11962 The stem with which an implicit rule matches
11963 (@pxref{Pattern Match, ,How Patterns Match}).
11964
11965 @item $(@@D)
11966 @itemx $(@@F)
11967 The directory part and the file-within-directory part of @code{$@@}.
11968
11969 @item $(*D)
11970 @itemx $(*F)
11971 The directory part and the file-within-directory part of @code{$*}.
11972
11973 @item $(%D)
11974 @itemx $(%F)
11975 The directory part and the file-within-directory part of @code{$%}.
11976
11977 @item $(<D)
11978 @itemx $(<F)
11979 The directory part and the file-within-directory part of @code{$<}.
11980
11981 @item $(^D)
11982 @itemx $(^F)
11983 The directory part and the file-within-directory part of @code{$^}.
11984
11985 @item $(+D)
11986 @itemx $(+F)
11987 The directory part and the file-within-directory part of @code{$+}.
11988
11989 @item $(?D)
11990 @itemx $(?F)
11991 The directory part and the file-within-directory part of @code{$?}.
11992 @end table
11993
11994 These variables are used specially by GNU @code{make}:
11995
11996 @table @code
11997 @item MAKEFILES
11998
11999 Makefiles to be read on every invocation of @code{make}.@*
12000 @xref{MAKEFILES Variable, ,The Variable @code{MAKEFILES}}.
12001
12002 @item VPATH
12003
12004 Directory search path for files not found in the current directory.@*
12005 @xref{General Search, , @code{VPATH} Search Path for All Prerequisites}.
12006
12007 @item SHELL
12008
12009 The name of the system default command interpreter, usually @file{/bin/sh}.
12010 You can set @code{SHELL} in the makefile to change the shell used to run
12011 recipes.  @xref{Execution, ,Recipe Execution}.  The @code{SHELL}
12012 variable is handled specially when importing from and exporting to the
12013 environment.  @xref{Choosing the Shell}.
12014
12015 @item MAKESHELL
12016
12017 On MS-DOS only, the name of the command interpreter that is to be used
12018 by @code{make}.  This value takes precedence over the value of
12019 @code{SHELL}.  @xref{Execution, ,MAKESHELL variable}.
12020
12021 @item MAKE
12022
12023 The name with which @code{make} was invoked.  Using this variable in
12024 recipes has special meaning.  @xref{MAKE Variable, ,How the
12025 @code{MAKE} Variable Works}.
12026
12027 @item MAKE_VERSION
12028
12029 The built-in variable @samp{MAKE_VERSION} expands to the version
12030 number of the GNU @code{make} program.
12031 @vindex MAKE_VERSION
12032
12033 @item MAKE_HOST
12034
12035 The built-in variable @samp{MAKE_HOST} expands to a string
12036 representing the host that GNU @code{make} was built to run on.
12037 @vindex MAKE_HOST
12038
12039 @item MAKELEVEL
12040
12041 The number of levels of recursion (sub-@code{make}s).@*
12042 @xref{Variables/Recursion}.
12043
12044 @item MAKEFLAGS
12045
12046 The flags given to @code{make}.  You can set this in the environment or
12047 a makefile to set flags.@*
12048 @xref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
12049
12050 It is @emph{never} appropriate to use @code{MAKEFLAGS} directly in a
12051 recipe line: its contents may not be quoted correctly for use in the
12052 shell.  Always allow recursive @code{make}'s to obtain these values
12053 through the environment from its parent.
12054
12055 @item GNUMAKEFLAGS
12056
12057 Other flags parsed by @code{make}.  You can set this in the environment or
12058 a makefile to set @code{make} command-line flags.  GNU @code{make}
12059 never sets this variable itself.  This variable is only needed if
12060 you'd like to set GNU @code{make}-specific flags in a POSIX-compliant
12061 makefile.  This variable will be seen by GNU @code{make} and ignored
12062 by other @code{make} implementations.  It's not needed if you only use
12063 GNU @code{make}; just use @code{MAKEFLAGS} directly.
12064 @xref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
12065
12066 @item MAKECMDGOALS
12067
12068 The targets given to @code{make} on the command line.  Setting this
12069 variable has no effect on the operation of @code{make}.@*
12070 @xref{Goals, ,Arguments to Specify the Goals}.
12071
12072 @item CURDIR
12073
12074 Set to the pathname of the current working directory (after all
12075 @code{-C} options are processed, if any).  Setting this variable has no
12076 effect on the operation of @code{make}.@*
12077 @xref{Recursion, ,Recursive Use of @code{make}}.
12078
12079 @item SUFFIXES
12080
12081 The default list of suffixes before @code{make} reads any makefiles.
12082
12083 @item .LIBPATTERNS
12084 Defines the naming of the libraries @code{make} searches for, and their
12085 order.@*
12086 @xref{Libraries/Search, ,Directory Search for Link Libraries}.
12087 @end table
12088
12089 @node Error Messages, Complex Makefile, Quick Reference, Top
12090 @comment  node-name,  next,  previous,  up
12091 @appendix Errors Generated by Make
12092
12093 Here is a list of the more common errors you might see generated by
12094 @code{make}, and some information about what they mean and how to fix
12095 them.
12096
12097 Sometimes @code{make} errors are not fatal, especially in the presence
12098 of a @code{-} prefix on a recipe line, or the @code{-k} command line
12099 option.  Errors that are fatal are prefixed with the string
12100 @code{***}.
12101
12102 Error messages are all either prefixed with the name of the program
12103 (usually @samp{make}), or, if the error is found in a makefile, the name
12104 of the file and line number containing the problem.
12105
12106 In the table below, these common prefixes are left off.
12107
12108 @table @samp
12109
12110 @item [@var{foo}] Error @var{NN}
12111 @itemx [@var{foo}] @var{signal description}
12112 These errors are not really @code{make} errors at all.  They mean that a
12113 program that @code{make} invoked as part of a recipe returned a
12114 non-0 error code (@samp{Error @var{NN}}), which @code{make} interprets
12115 as failure, or it exited in some other abnormal fashion (with a
12116 signal of some type).  @xref{Errors, ,Errors in Recipes}.
12117
12118 If no @code{***} is attached to the message, then the sub-process failed
12119 but the rule in the makefile was prefixed with the @code{-} special
12120 character, so @code{make} ignored the error.
12121
12122 @item missing separator.  Stop.
12123 @itemx missing separator (did you mean TAB instead of 8 spaces?).  Stop.
12124 This means that @code{make} could not understand much of anything
12125 about the makefile line it just read.  GNU @code{make} looks for
12126 various separators (@code{:}, @code{=}, recipe prefix characters,
12127 etc.) to indicate what kind of line it's parsing.  This message means
12128 it couldn't find a valid one.
12129
12130 One of the most common reasons for this message is that you (or
12131 perhaps your oh-so-helpful editor, as is the case with many MS-Windows
12132 editors) have attempted to indent your recipe lines with spaces
12133 instead of a tab character.  In this case, @code{make} will use the
12134 second form of the error above.  Remember that every line in the
12135 recipe must begin with a tab character (unless you set
12136 @code{.RECIPEPREFIX}; @pxref{Special Variables}).  Eight spaces do not
12137 count.  @xref{Rule Syntax}.
12138
12139 @item recipe commences before first target.  Stop.
12140 @itemx missing rule before recipe.  Stop.
12141 This means the first thing in the makefile seems to be part of a
12142 recipe: it begins with a recipe prefix character and doesn't appear to
12143 be a legal @code{make} directive (such as a variable assignment).
12144 Recipes must always be associated with a target.
12145
12146 The second form is generated if the line has a semicolon as the first
12147 non-whitespace character; @code{make} interprets this to mean you left
12148 out the "target: prerequisite" section of a rule.  @xref{Rule Syntax}.
12149
12150 @item No rule to make target `@var{xxx}'.
12151 @itemx No rule to make target `@var{xxx}', needed by `@var{yyy}'.
12152 This means that @code{make} decided it needed to build a target, but
12153 then couldn't find any instructions in the makefile on how to do that,
12154 either explicit or implicit (including in the default rules database).
12155
12156 If you want that file to be built, you will need to add a rule to your
12157 makefile describing how that target can be built.  Other possible
12158 sources of this problem are typos in the makefile (if that file name is
12159 wrong) or a corrupted source tree (if that file is not supposed to be
12160 built, but rather only a prerequisite).
12161
12162 @item No targets specified and no makefile found.  Stop.
12163 @itemx No targets.  Stop.
12164 The former means that you didn't provide any targets to be built on the
12165 command line, and @code{make} couldn't find any makefiles to read in.
12166 The latter means that some makefile was found, but it didn't contain any
12167 default goal and none was given on the command line.  GNU @code{make}
12168 has nothing to do in these situations.
12169 @xref{Makefile Arguments, ,Arguments to Specify the Makefile}.@refill
12170
12171 @item Makefile `@var{xxx}' was not found.
12172 @itemx Included makefile `@var{xxx}' was not found.
12173 A makefile specified on the command line (first form) or included
12174 (second form) was not found.
12175
12176 @item warning: overriding recipe for target `@var{xxx}'
12177 @itemx warning: ignoring old recipe for target `@var{xxx}'
12178 GNU @code{make} allows only one recipe to be specified per target
12179 (except for double-colon rules).  If you give a recipe for a target
12180 which already has been defined to have one, this warning is issued and
12181 the second recipe will overwrite the first.  @xref{Multiple Rules,
12182 ,Multiple Rules for One Target}.
12183
12184 @item Circular @var{xxx} <- @var{yyy} dependency dropped.
12185 This means that @code{make} detected a loop in the dependency graph:
12186 after tracing the prerequisite @var{yyy} of target @var{xxx}, and its
12187 prerequisites, etc., one of them depended on @var{xxx} again.
12188
12189 @item Recursive variable `@var{xxx}' references itself (eventually).  Stop.
12190 This means you've defined a normal (recursive) @code{make} variable
12191 @var{xxx} that, when it's expanded, will refer to itself (@var{xxx}).
12192 This is not allowed; either use simply-expanded variables (@samp{:=}
12193 or @samp{::=}) or use the append operator (@samp{+=}).  @xref{Using
12194 Variables, ,How to Use Variables}.
12195
12196 @item Unterminated variable reference.  Stop.
12197 This means you forgot to provide the proper closing parenthesis
12198 or brace in your variable or function reference.
12199
12200 @item insufficient arguments to function `@var{xxx}'.  Stop.
12201 This means you haven't provided the requisite number of arguments for
12202 this function.  See the documentation of the function for a description
12203 of its arguments.  @xref{Functions, ,Functions for Transforming Text}.
12204
12205 @item missing target pattern.  Stop.
12206 @itemx multiple target patterns.  Stop.
12207 @itemx target pattern contains no `%'.  Stop.
12208 @itemx mixed implicit and static pattern rules.  Stop.
12209 These are generated for malformed static pattern rules.  The first
12210 means there's no pattern in the target section of the rule; the second
12211 means there are multiple patterns in the target section; the third
12212 means the target doesn't contain a pattern character (@code{%}); and
12213 the fourth means that all three parts of the static pattern rule
12214 contain pattern characters (@code{%})--only the first two parts
12215 should.  If you see these errors and you aren't trying to create a
12216 static pattern rule, check the value of any variables in your target
12217 and prerequisite lists to be sure they do not contain colons.
12218 @xref{Static Usage, ,Syntax of Static Pattern Rules}.
12219
12220 @item warning: -jN forced in submake: disabling jobserver mode.
12221 This warning and the next are generated if @code{make} detects error
12222 conditions related to parallel processing on systems where
12223 sub-@code{make}s can communicate (@pxref{Options/Recursion,
12224 ,Communicating Options to a Sub-@code{make}}).  This warning is
12225 generated if a recursive invocation of a @code{make} process is forced
12226 to have @samp{-j@var{N}} in its argument list (where @var{N} is greater
12227 than one).  This could happen, for example, if you set the @code{MAKE}
12228 environment variable to @samp{make -j2}.  In this case, the
12229 sub-@code{make} doesn't communicate with other @code{make} processes and
12230 will simply pretend it has two jobs of its own.
12231
12232 @item warning: jobserver unavailable: using -j1.  Add `+' to parent make rule.
12233 In order for @code{make} processes to communicate, the parent will pass
12234 information to the child.  Since this could result in problems if the
12235 child process isn't actually a @code{make}, the parent will only do this
12236 if it thinks the child is a @code{make}.  The parent uses the normal
12237 algorithms to determine this (@pxref{MAKE Variable, ,How the @code{MAKE}
12238 Variable Works}).  If the makefile is constructed such that the parent
12239 doesn't know the child is a @code{make} process, then the child will
12240 receive only part of the information necessary.  In this case, the child
12241 will generate this warning message and proceed with its build in a
12242 sequential manner.
12243
12244 @end table
12245
12246 @node Complex Makefile, GNU Free Documentation License, Error Messages, Top
12247 @appendix Complex Makefile Example
12248
12249 Here is the makefile for the GNU @code{tar} program.  This is a
12250 moderately complex makefile.  The first line uses a @code{#!} setting
12251 to allow the makefile to be executed directly.
12252
12253 Because it is the first target, the default goal is @samp{all}.  An
12254 interesting feature of this makefile is that @file{testpad.h} is a
12255 source file automatically created by the @code{testpad} program,
12256 itself compiled from @file{testpad.c}.
12257
12258 If you type @samp{make} or @samp{make all}, then @code{make} creates
12259 the @file{tar} executable, the @file{rmt} daemon that provides
12260 remote tape access, and the @file{tar.info} Info file.
12261
12262 If you type @samp{make install}, then @code{make} not only creates
12263 @file{tar}, @file{rmt}, and @file{tar.info}, but also installs
12264 them.
12265
12266 If you type @samp{make clean}, then @code{make} removes the @samp{.o}
12267 files, and the @file{tar}, @file{rmt}, @file{testpad},
12268 @file{testpad.h}, and @file{core} files.
12269
12270 If you type @samp{make distclean}, then @code{make} not only removes
12271 the same files as does @samp{make clean} but also the
12272 @file{TAGS}, @file{Makefile}, and @file{config.status} files.
12273 (Although it is not evident, this makefile (and
12274 @file{config.status}) is generated by the user with the
12275 @code{configure} program, which is provided in the @code{tar}
12276 distribution, but is not shown here.)
12277
12278 If you type @samp{make realclean}, then @code{make} removes the same
12279 files as does @samp{make distclean} and also removes the Info files
12280 generated from @file{tar.texinfo}.
12281
12282 In addition, there are targets @code{shar} and @code{dist} that create
12283 distribution kits.
12284
12285 @example
12286 @group
12287 #!/usr/bin/make -f
12288 # Generated automatically from Makefile.in by configure.
12289 # Un*x Makefile for GNU tar program.
12290 # Copyright (C) 1991 Free Software Foundation, Inc.
12291 @end group
12292
12293 @group
12294 # This program is free software; you can redistribute
12295 # it and/or modify it under the terms of the GNU
12296 # General Public License @dots{}
12297 @dots{}
12298 @dots{}
12299 @end group
12300
12301 SHELL = /bin/sh
12302
12303 #### Start of system configuration section. ####
12304
12305 srcdir = .
12306
12307 @group
12308 # If you use gcc, you should either run the
12309 # fixincludes script that comes with it or else use
12310 # gcc with the -traditional option.  Otherwise ioctl
12311 # calls will be compiled incorrectly on some systems.
12312 CC = gcc -O
12313 YACC = bison -y
12314 INSTALL = /usr/local/bin/install -c
12315 INSTALLDATA = /usr/local/bin/install -c -m 644
12316 @end group
12317
12318 # Things you might add to DEFS:
12319 # -DSTDC_HEADERS        If you have ANSI C headers and
12320 #                       libraries.
12321 # -DPOSIX               If you have POSIX.1 headers and
12322 #                       libraries.
12323 # -DBSD42               If you have sys/dir.h (unless
12324 #                       you use -DPOSIX), sys/file.h,
12325 #                       and st_blocks in `struct stat'.
12326 # -DUSG                 If you have System V/ANSI C
12327 #                       string and memory functions
12328 #                       and headers, sys/sysmacros.h,
12329 #                       fcntl.h, getcwd, no valloc,
12330 #                       and ndir.h (unless
12331 #                       you use -DDIRENT).
12332 # -DNO_MEMORY_H         If USG or STDC_HEADERS but do not
12333 #                       include memory.h.
12334 # -DDIRENT              If USG and you have dirent.h
12335 #                       instead of ndir.h.
12336 # -DSIGTYPE=int         If your signal handlers
12337 #                       return int, not void.
12338 # -DNO_MTIO             If you lack sys/mtio.h
12339 #                       (magtape ioctls).
12340 # -DNO_REMOTE           If you do not have a remote shell
12341 #                       or rexec.
12342 # -DUSE_REXEC           To use rexec for remote tape
12343 #                       operations instead of
12344 #                       forking rsh or remsh.
12345 # -DVPRINTF_MISSING     If you lack vprintf function
12346 #                       (but have _doprnt).
12347 # -DDOPRNT_MISSING      If you lack _doprnt function.
12348 #                       Also need to define
12349 #                       -DVPRINTF_MISSING.
12350 # -DFTIME_MISSING       If you lack ftime system call.
12351 # -DSTRSTR_MISSING      If you lack strstr function.
12352 # -DVALLOC_MISSING      If you lack valloc function.
12353 # -DMKDIR_MISSING       If you lack mkdir and
12354 #                       rmdir system calls.
12355 # -DRENAME_MISSING      If you lack rename system call.
12356 # -DFTRUNCATE_MISSING   If you lack ftruncate
12357 #                       system call.
12358 # -DV7                  On Version 7 Unix (not
12359 #                       tested in a long time).
12360 # -DEMUL_OPEN3          If you lack a 3-argument version
12361 #                       of open, and want to emulate it
12362 #                       with system calls you do have.
12363 # -DNO_OPEN3            If you lack the 3-argument open
12364 #                       and want to disable the tar -k
12365 #                       option instead of emulating open.
12366 # -DXENIX               If you have sys/inode.h
12367 #                       and need it 94 to be included.
12368
12369 DEFS =  -DSIGTYPE=int -DDIRENT -DSTRSTR_MISSING \
12370         -DVPRINTF_MISSING -DBSD42
12371 # Set this to rtapelib.o unless you defined NO_REMOTE,
12372 # in which case make it empty.
12373 RTAPELIB = rtapelib.o
12374 LIBS =
12375 DEF_AR_FILE = /dev/rmt8
12376 DEFBLOCKING = 20
12377
12378 @group
12379 CDEBUG = -g
12380 CFLAGS = $(CDEBUG) -I. -I$(srcdir) $(DEFS) \
12381         -DDEF_AR_FILE=\"$(DEF_AR_FILE)\" \
12382         -DDEFBLOCKING=$(DEFBLOCKING)
12383 LDFLAGS = -g
12384 @end group
12385
12386 @group
12387 prefix = /usr/local
12388 # Prefix for each installed program,
12389 # normally empty or `g'.
12390 binprefix =
12391
12392 # The directory to install tar in.
12393 bindir = $(prefix)/bin
12394
12395 # The directory to install the info files in.
12396 infodir = $(prefix)/info
12397 @end group
12398
12399 #### End of system configuration section. ####
12400
12401 @group
12402 SRCS_C  = tar.c create.c extract.c buffer.c   \
12403           getoldopt.c update.c gnu.c mangle.c \
12404           version.c list.c names.c diffarch.c \
12405           port.c wildmat.c getopt.c getopt1.c \
12406           regex.c
12407 SRCS_Y  = getdate.y
12408 SRCS    = $(SRCS_C) $(SRCS_Y)
12409 OBJS    = $(SRCS_C:.c=.o) $(SRCS_Y:.y=.o) $(RTAPELIB)
12410 @end group
12411 @group
12412 AUX =   README COPYING ChangeLog Makefile.in  \
12413         makefile.pc configure configure.in \
12414         tar.texinfo tar.info* texinfo.tex \
12415         tar.h port.h open3.h getopt.h regex.h \
12416         rmt.h rmt.c rtapelib.c alloca.c \
12417         msd_dir.h msd_dir.c tcexparg.c \
12418         level-0 level-1 backup-specs testpad.c
12419 @end group
12420
12421 .PHONY: all
12422 all:    tar rmt tar.info
12423
12424 @group
12425 tar:    $(OBJS)
12426         $(CC) $(LDFLAGS) -o $@@ $(OBJS) $(LIBS)
12427 @end group
12428
12429 @group
12430 rmt:    rmt.c
12431         $(CC) $(CFLAGS) $(LDFLAGS) -o $@@ rmt.c
12432 @end group
12433
12434 @group
12435 tar.info: tar.texinfo
12436         makeinfo tar.texinfo
12437 @end group
12438
12439 @group
12440 .PHONY: install
12441 install: all
12442         $(INSTALL) tar $(bindir)/$(binprefix)tar
12443         -test ! -f rmt || $(INSTALL) rmt /etc/rmt
12444         $(INSTALLDATA) $(srcdir)/tar.info* $(infodir)
12445 @end group
12446
12447 @group
12448 $(OBJS): tar.h port.h testpad.h
12449 regex.o buffer.o tar.o: regex.h
12450 # getdate.y has 8 shift/reduce conflicts.
12451 @end group
12452
12453 @group
12454 testpad.h: testpad
12455         ./testpad
12456 @end group
12457
12458 @group
12459 testpad: testpad.o
12460         $(CC) -o $@@ testpad.o
12461 @end group
12462
12463 @group
12464 TAGS:   $(SRCS)
12465         etags $(SRCS)
12466 @end group
12467
12468 @group
12469 .PHONY: clean
12470 clean:
12471         rm -f *.o tar rmt testpad testpad.h core
12472 @end group
12473
12474 @group
12475 .PHONY: distclean
12476 distclean: clean
12477         rm -f TAGS Makefile config.status
12478 @end group
12479
12480 @group
12481 .PHONY: realclean
12482 realclean: distclean
12483         rm -f tar.info*
12484 @end group
12485
12486 @group
12487 .PHONY: shar
12488 shar: $(SRCS) $(AUX)
12489         shar $(SRCS) $(AUX) | compress \
12490           > tar-`sed -e '/version_string/!d' \
12491                      -e 's/[^0-9.]*\([0-9.]*\).*/\1/' \
12492                      -e q
12493                      version.c`.shar.Z
12494 @end group
12495
12496 @group
12497 .PHONY: dist
12498 dist: $(SRCS) $(AUX)
12499         echo tar-`sed \
12500              -e '/version_string/!d' \
12501              -e 's/[^0-9.]*\([0-9.]*\).*/\1/' \
12502              -e q
12503              version.c` > .fname
12504         -rm -rf `cat .fname`
12505         mkdir `cat .fname`
12506         ln $(SRCS) $(AUX) `cat .fname`
12507         tar chZf `cat .fname`.tar.Z `cat .fname`
12508         -rm -rf `cat .fname` .fname
12509 @end group
12510
12511 @group
12512 tar.zoo: $(SRCS) $(AUX)
12513         -rm -rf tmp.dir
12514         -mkdir tmp.dir
12515         -rm tar.zoo
12516         for X in $(SRCS) $(AUX) ; do \
12517             echo $$X ; \
12518             sed 's/$$/^M/' $$X \
12519             > tmp.dir/$$X ; done
12520         cd tmp.dir ; zoo aM ../tar.zoo *
12521         -rm -rf tmp.dir
12522 @end group
12523 @end example
12524
12525 @node GNU Free Documentation License, Concept Index, Complex Makefile, Top
12526 @appendixsec GNU Free Documentation License
12527 @cindex FDL, GNU Free Documentation License
12528 @include fdl.texi
12529
12530 @node Concept Index, Name Index, GNU Free Documentation License, Top
12531 @unnumbered Index of Concepts
12532
12533 @printindex cp
12534
12535 @node Name Index,  , Concept Index, Top
12536 @unnumbered Index of Functions, Variables, & Directives
12537
12538 @printindex fn
12539
12540 @bye