Add missing load.c file to POTFILES.in
[make.git] / doc / make.texi
blobd4921f04666c94fcd4e0d8321996ef8d8027d93c
1 \input texinfo                @c -*- Texinfo -*-
2 @c %**start of header
3 @setfilename make.info
5 @include version.texi
6 @set EDITION 0.72
7 @set RCSID $Id: make.texi,v 1.80 2012/10/29 07:05:29 psmith Exp $
9 @settitle GNU @code{make}
10 @setchapternewpage odd
11 @c Combine the variable and function indices:
12 @syncodeindex vr fn
13 @c Combine the program and concept indices:
14 @syncodeindex pg cp
15 @c FSF publishers: format makebook.texi instead of using this file directly.
16 @c ISBN confirmed by Jasimin Huang <jasimin@fsf.org> on 25 Mar 2009
17 @set ISBN 1-882114-83-3
18 @c %**end of header
20 @copying
21 This file documents the GNU @code{make} utility, which determines
22 automatically which pieces of a large program need to be recompiled,
23 and issues the commands to recompile them.
25 This is Edition @value{EDITION}, last updated @value{UPDATED},
26 of @cite{The GNU Make Manual}, for GNU @code{make} version @value{VERSION}.
28 Copyright @copyright{} 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
29 1996, 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005, 2006, 2007,
30 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
32 @quotation
33 Permission is granted to copy, distribute and/or modify this document
34 under the terms of the GNU Free Documentation License, Version 1.3 or
35 any later version published by the Free Software Foundation; with no
36 Invariant Sections, with the Front-Cover Texts being ``A GNU Manual,''
37 and with the Back-Cover Texts as in (a) below.  A copy of the
38 license is included in the section entitled ``GNU Free Documentation
39 License.''
41 (a) The FSF's Back-Cover Text is: ``You have the freedom to copy and
42 modify this GNU manual.  Buying copies from the FSF supports it in
43 developing GNU and promoting software freedom.''
44 @end quotation
45 @end copying
47 @c finalout
49 @c ISPELL CHECK: done, 10 June 1993 --roland
50 @c ISPELL CHECK: done, 2000-06-25 --Martin Buchholz
53 @dircategory Software development
54 @direntry
55 * Make: (make).            Remake files automatically.
56 @end direntry
58 @iftex
59 @shorttitlepage GNU Make
60 @end iftex
61 @titlepage
62 @title GNU Make
63 @subtitle A Program for Directing Recompilation
64 @subtitle GNU @code{make} Version @value{VERSION}
65 @subtitle @value{UPDATED-MONTH}
66 @author Richard M. Stallman, Roland McGrath, Paul D. Smith
67 @page
68 @vskip 0pt plus 1filll
69 @insertcopying
70 @sp 2
71 Published by the Free Software Foundation @*
72 51 Franklin St. -- Fifth Floor @*
73 Boston, MA 02110-1301 USA @*
74 ISBN @value{ISBN} @*
75 @sp 2
76 Cover art by Etienne Suvasa.
77 @end titlepage
79 @summarycontents
80 @contents
82 @ifnottex
83 @node Top, Overview, (dir), (dir)
84 @top GNU @code{make}
86 @insertcopying
87 @end ifnottex
89 @menu
90 * Overview::                    Overview of @code{make}.
91 * Introduction::                An introduction to @code{make}.
92 * Makefiles::                   Makefiles tell @code{make} what to do.
93 * Rules::                       Rules describe when a file must be remade.
94 * Recipes::                     Recipes say how to remake a file.
95 * Using Variables::             You can use variables to avoid repetition.
96 * Conditionals::                Use or ignore parts of the makefile based
97                                   on the values of variables.
98 * Functions::                   Many powerful ways to manipulate text.
99 * Invoking make: Running.       How to invoke @code{make} on the command line.
100 * Implicit Rules::              Use implicit rules to treat many files alike,
101                                   based on their file names.
102 * Archives::                    How @code{make} can update library archives.
103 * Extending make::              Using extensions to @code{make}.
104 * Features::                    Features GNU @code{make} has over other @code{make}s.
105 * Missing::                     What GNU @code{make} lacks from other @code{make}s.
106 * Makefile Conventions::        Conventions for writing makefiles for
107                                   GNU programs.
108 * Quick Reference::             A quick reference for experienced users.
109 * Error Messages::              A list of common errors generated by @code{make}.
110 * Complex Makefile::            A real example of a straightforward,
111                                   but nontrivial, makefile.
113 * GNU Free Documentation License::  License for copying this manual.
114 * Concept Index::               Index of Concepts.
115 * Name Index::                  Index of Functions, Variables, & Directives.
117 @detailmenu
118  --- The Detailed Node Listing ---
120 Overview of @code{make}
122 * Preparing::                   Preparing and running @code{make}.
123 * Reading::                     On reading this text.
124 * Bugs::                        Problems and bugs.
126 An Introduction to Makefiles
128 * Rule Introduction::           What a rule looks like.
129 * Simple Makefile::             A simple makefile.
130 * How Make Works::              How @code{make} processes this makefile.
131 * Variables Simplify::          Variables make makefiles simpler.
132 * make Deduces::                Letting @code{make} deduce the recipes.
133 * Combine By Prerequisite::     Another style of makefile.
134 * Cleanup::                     Rules for cleaning the directory.
136 Writing Makefiles
138 * Makefile Contents::           What makefiles contain.
139 * Makefile Names::              How to name your makefile.
140 * Include::                     How one makefile can use another makefile.
141 * MAKEFILES Variable::          The environment can specify extra makefiles.
142 * Remaking Makefiles::          How makefiles get remade.
143 * Overriding Makefiles::        How to override part of one makefile
144                                   with another makefile.
145 * Reading Makefiles::           How makefiles are parsed.
146 * Secondary Expansion::         How and when secondary expansion is performed.
148 What Makefiles Contain
150 * Splitting Lines::             Splitting long lines in makefiles
152 Writing Rules
154 * Rule Example::                An example explained.
155 * Rule Syntax::                 General syntax explained.
156 * Prerequisite Types::          There are two types of prerequisites.
157 * Wildcards::                   Using wildcard characters such as `*'.
158 * Directory Search::            Searching other directories for source files.
159 * Phony Targets::               Using a target that is not a real file's name.
160 * Force Targets::               You can use a target without a recipe
161                                   or prerequisites to mark other targets
162                                   as phony.
163 * Empty Targets::               When only the date matters and the
164                                   files are empty.
165 * Special Targets::             Targets with special built-in meanings.
166 * Multiple Targets::            When to make use of several targets in a rule.
167 * Multiple Rules::              How to use several rules with the same target.
168 * Static Pattern::              Static pattern rules apply to multiple targets
169                                   and can vary the prerequisites according to
170                                   the target name.
171 * Double-Colon::                How to use a special kind of rule to allow
172                                   several independent rules for one target.
173 * Automatic Prerequisites::     How to automatically generate rules giving
174                                   prerequisites from source files themselves.
176 Using Wildcard Characters in File Names
178 * Wildcard Examples::           Several examples.
179 * Wildcard Pitfall::            Problems to avoid.
180 * Wildcard Function::           How to cause wildcard expansion where
181                                   it does not normally take place.
183 Searching Directories for Prerequisites
185 * General Search::              Specifying a search path that applies
186                                   to every prerequisite.
187 * Selective Search::            Specifying a search path
188                                   for a specified class of names.
189 * Search Algorithm::            When and how search paths are applied.
190 * Recipes/Search::              How to write recipes that work together
191                                   with search paths.
192 * Implicit/Search::             How search paths affect implicit rules.
193 * Libraries/Search::            Directory search for link libraries.
195 Static Pattern Rules
197 * Static Usage::                The syntax of static pattern rules.
198 * Static versus Implicit::      When are they better than implicit rules?
200 Writing Recipes in Rules
202 * Recipe Syntax::               Recipe syntax features and pitfalls.
203 * Echoing::                     How to control when recipes are echoed.
204 * Execution::                   How recipes are executed.
205 * Parallel::                    How recipes can be executed in parallel.
206 * Errors::                      What happens after a recipe execution error.
207 * Interrupts::                  What happens when a recipe is interrupted.
208 * Recursion::                   Invoking @code{make} from makefiles.
209 * Canned Recipes::              Defining canned recipes.
210 * Empty Recipes::               Defining useful, do-nothing recipes.
212 Recipe Syntax
214 * Splitting Recipe Lines::      Breaking long recipe lines for readability.
215 * Variables in Recipes::        Using @code{make} variables in recipes.
217 Recipe Execution
219 * One Shell::                   One shell for all lines in a recipe.
220 * Choosing the Shell::          How @code{make} chooses the shell used
221                                   to run recipes.
223 Recursive Use of @code{make}
225 * MAKE Variable::               The special effects of using @samp{$(MAKE)}.
226 * Variables/Recursion::         How to communicate variables to a sub-@code{make}.
227 * Options/Recursion::           How to communicate options to a sub-@code{make}.
228 * -w Option::                   How the @samp{-w} or @samp{--print-directory} option
229                                   helps debug use of recursive @code{make} commands.
231 How to Use Variables
233 * Reference::                   How to use the value of a variable.
234 * Flavors::                     Variables come in two flavors.
235 * Advanced::                    Advanced features for referencing a variable.
236 * Values::                      All the ways variables get their values.
237 * Setting::                     How to set a variable in the makefile.
238 * Appending::                   How to append more text to the old value
239                                   of a variable.
240 * Override Directive::          How to set a variable in the makefile even if
241                                   the user has set it with a command argument.
242 * Multi-Line::                  An alternate way to set a variable
243                                   to a multi-line string.
244 * Undefine Directive::          How to undefine a variable so that it appears
245                                   as if it was never set.
246 * Environment::                 Variable values can come from the environment.
247 * Target-specific::             Variable values can be defined on a per-target
248                                   basis.
249 * Pattern-specific::            Target-specific variable values can be applied
250                                   to a group of targets that match a pattern.
251 * Suppressing Inheritance::     Suppress inheritance of variables.
252 * Special Variables::           Variables with special meaning or behavior.
254 Advanced Features for Reference to Variables
256 * Substitution Refs::           Referencing a variable with
257                                   substitutions on the value.
258 * Computed Names::              Computing the name of the variable to refer to.
260 Conditional Parts of Makefiles
262 * Conditional Example::         Example of a conditional
263 * Conditional Syntax::          The syntax of conditionals.
264 * Testing Flags::               Conditionals that test flags.
266 Functions for Transforming Text
268 * Syntax of Functions::         How to write a function call.
269 * Text Functions::              General-purpose text manipulation functions.
270 * File Name Functions::         Functions for manipulating file names.
271 * Conditional Functions::       Functions that implement conditions.
272 * Foreach Function::            Repeat some text with controlled variation.
273 * File Function::               Write text to a file.
274 * Call Function::               Expand a user-defined function.
275 * Value Function::              Return the un-expanded value of a variable.
276 * Eval Function::               Evaluate the arguments as makefile syntax.
277 * Origin Function::             Find where a variable got its value.
278 * Flavor Function::             Find out the flavor of a variable.
279 * Make Control Functions::      Functions that control how make runs.
280 * Shell Function::              Substitute the output of a shell command.
281 * Guile Function::              Use GNU Guile embedded scripting language.
283 How to Run @code{make}
285 * Makefile Arguments::          How to specify which makefile to use.
286 * Goals::                       How to use goal arguments to specify which
287                                   parts of the makefile to use.
288 * Instead of Execution::        How to use mode flags to specify what
289                                   kind of thing to do with the recipes
290                                   in the makefile other than simply
291                                   execute them.
292 * Avoiding Compilation::        How to avoid recompiling certain files.
293 * Overriding::                  How to override a variable to specify
294                                   an alternate compiler and other things.
295 * Testing::                     How to proceed past some errors, to
296                                   test compilation.
297 * Options Summary::             Summary of Options
299 Using Implicit Rules
301 * Using Implicit::              How to use an existing implicit rule
302                                   to get the recipes for updating a file.
303 * Catalogue of Rules::          A list of built-in implicit rules.
304 * Implicit Variables::          How to change what predefined rules do.
305 * Chained Rules::               How to use a chain of implicit rules.
306 * Pattern Rules::               How to define new implicit rules.
307 * Last Resort::                 How to define a recipe for rules which
308                                   cannot find any.
309 * Suffix Rules::                The old-fashioned style of implicit rule.
310 * Implicit Rule Search::        The precise algorithm for applying
311                                   implicit rules.
313 Defining and Redefining Pattern Rules
315 * Pattern Intro::               An introduction to pattern rules.
316 * Pattern Examples::            Examples of pattern rules.
317 * Automatic Variables::         How to use automatic variables in the
318                                   recipe of implicit rules.
319 * Pattern Match::               How patterns match.
320 * Match-Anything Rules::        Precautions you should take prior to
321                                   defining rules that can match any
322                                   target file whatever.
323 * Canceling Rules::             How to override or cancel built-in rules.
325 Using @code{make} to Update Archive Files
327 * Archive Members::             Archive members as targets.
328 * Archive Update::              The implicit rule for archive member targets.
329 * Archive Pitfalls::            Dangers to watch out for when using archives.
330 * Archive Suffix Rules::        You can write a special kind of suffix rule
331                                   for updating archives.
333 Implicit Rule for Archive Member Targets
335 * Archive Symbols::             How to update archive symbol directories.
337 Extending GNU @code{make}
339 * Guile Integration::           Using Guile as an embedded scripting language.
340 * Loading Objects::             Loading dynamic objects as extensions.
342 GNU Guile Integration
344 * Guile Types::                 Converting Guile types to @code{make} strings.
345 * Guile Interface::             Invoking @code{make} functions from Guile.
346 * Guile Example::               Example using Guile in @code{make}.
348 Loading Dynamic Objects
350 * load Directive::              Loading dynamic objects as extensions.
352 @end detailmenu
353 @end menu
355 @node Overview, Introduction, Top, Top
356 @comment  node-name,  next,  previous,  up
357 @chapter Overview of @code{make}
359 The @code{make} utility automatically determines which pieces of a large
360 program need to be recompiled, and issues commands to recompile them.
361 This manual describes GNU @code{make}, which was implemented by Richard
362 Stallman and Roland McGrath.  Development since Version 3.76 has been
363 handled by Paul D. Smith.
365 GNU @code{make} conforms to section 6.2 of @cite{IEEE Standard
366 1003.2-1992} (POSIX.2).
367 @cindex POSIX
368 @cindex IEEE Standard 1003.2
369 @cindex standards conformance
371 Our examples show C programs, since they are most common, but you can use
372 @code{make} with any programming language whose compiler can be run with a
373 shell command.  Indeed, @code{make} is not limited to programs.  You can
374 use it to describe any task where some files must be updated automatically
375 from others whenever the others change.
377 @menu
378 * Preparing::                   Preparing and running @code{make}.
379 * Reading::                     On reading this text.
380 * Bugs::                        Problems and bugs.
381 @end menu
383 @node Preparing, Reading, Overview, Overview
384 @ifnottex
385 @heading Preparing and Running Make
386 @end ifnottex
388 To prepare to use @code{make}, you must write a file called
389 the @dfn{makefile} that describes the relationships among files
390 in your program and provides commands for updating each file.
391 In a program, typically, the executable file is updated from object
392 files, which are in turn made by compiling source files.@refill
394 Once a suitable makefile exists, each time you change some source files,
395 this simple shell command:
397 @example
398 make
399 @end example
401 @noindent
402 suffices to perform all necessary recompilations.  The @code{make} program
403 uses the makefile data base and the last-modification times of the files to
404 decide which of the files need to be updated.  For each of those files, it
405 issues the recipes recorded in the data base.
407 You can provide command line arguments to @code{make} to control which
408 files should be recompiled, or how.  @xref{Running, ,How to Run
409 @code{make}}.
411 @node Reading, Bugs, Preparing, Overview
412 @section How to Read This Manual
414 If you are new to @code{make}, or are looking for a general
415 introduction, read the first few sections of each chapter, skipping the
416 later sections.  In each chapter, the first few sections contain
417 introductory or general information and the later sections contain
418 specialized or technical information.
419 @ifnottex
420 The exception is the second chapter, @ref{Introduction, ,An
421 Introduction to Makefiles}, all of which is introductory.
422 @end ifnottex
423 @iftex
424 The exception is @ref{Introduction, ,An Introduction to Makefiles},
425 all of which is introductory.
426 @end iftex
428 If you are familiar with other @code{make} programs, see @ref{Features,
429 ,Features of GNU @code{make}}, which lists the enhancements GNU
430 @code{make} has, and @ref{Missing, ,Incompatibilities and Missing
431 Features}, which explains the few things GNU @code{make} lacks that
432 others have.
434 For a quick summary, see @ref{Options Summary}, @ref{Quick Reference},
435 and @ref{Special Targets}.
437 @node Bugs,  , Reading, Overview
438 @section Problems and Bugs
439 @cindex reporting bugs
440 @cindex bugs, reporting
441 @cindex problems and bugs, reporting
443 If you have problems with GNU @code{make} or think you've found a bug,
444 please report it to the developers; we cannot promise to do anything but
445 we might well want to fix it.
447 Before reporting a bug, make sure you've actually found a real bug.
448 Carefully reread the documentation and see if it really says you can do
449 what you're trying to do.  If it's not clear whether you should be able
450 to do something or not, report that too; it's a bug in the
451 documentation!
453 Before reporting a bug or trying to fix it yourself, try to isolate it
454 to the smallest possible makefile that reproduces the problem.  Then
455 send us the makefile and the exact results @code{make} gave you,
456 including any error or warning messages.  Please don't paraphrase
457 these messages: it's best to cut and paste them into your report.
458 When generating this small makefile, be sure to not use any non-free
459 or unusual tools in your recipes: you can almost always emulate what
460 such a tool would do with simple shell commands.  Finally, be sure to
461 explain what you expected to occur; this will help us decide whether
462 the problem was really in the documentation.
464 Once you have a precise problem you can report it in one of two ways.
465 Either send electronic mail to:
467 @example
468     bug-make@@gnu.org
469 @end example
471 @noindent
472 or use our Web-based project management tool, at:
474 @example
475     http://savannah.gnu.org/projects/make/
476 @end example
478 @noindent
479 In addition to the information above, please be careful to include the
480 version number of @code{make} you are using.  You can get this
481 information with the command @samp{make --version}.  Be sure also to
482 include the type of machine and operating system you are using.  One
483 way to obtain this information is by looking at the final lines of
484 output from the command @samp{make --help}.
486 @node Introduction, Makefiles, Overview, Top
487 @comment  node-name,  next,  previous,  up
488 @chapter An Introduction to Makefiles
490 You need a file called a @dfn{makefile} to tell @code{make} what to do.
491 Most often, the makefile tells @code{make} how to compile and link a
492 program.
493 @cindex makefile
495 In this chapter, we will discuss a simple makefile that describes how to
496 compile and link a text editor which consists of eight C source files
497 and three header files.  The makefile can also tell @code{make} how to
498 run miscellaneous commands when explicitly asked (for example, to remove
499 certain files as a clean-up operation).  To see a more complex example
500 of a makefile, see @ref{Complex Makefile}.
502 When @code{make} recompiles the editor, each changed C source file
503 must be recompiled.  If a header file has changed, each C source file
504 that includes the header file must be recompiled to be safe.  Each
505 compilation produces an object file corresponding to the source file.
506 Finally, if any source file has been recompiled, all the object files,
507 whether newly made or saved from previous compilations, must be linked
508 together to produce the new executable editor.
509 @cindex recompilation
510 @cindex editor
512 @menu
513 * Rule Introduction::           What a rule looks like.
514 * Simple Makefile::             A simple makefile.
515 * How Make Works::              How @code{make} processes this makefile.
516 * Variables Simplify::          Variables make makefiles simpler.
517 * make Deduces::                Letting @code{make} deduce the recipes.
518 * Combine By Prerequisite::     Another style of makefile.
519 * Cleanup::                     Rules for cleaning the directory.
520 @end menu
522 @node Rule Introduction, Simple Makefile, Introduction, Introduction
523 @comment  node-name,  next,  previous,  up
524 @section What a Rule Looks Like
525 @cindex rule, introduction to
526 @cindex makefile rule parts
527 @cindex parts of makefile rule
529 A simple makefile consists of ``rules'' with the following shape:
531 @cindex targets, introduction to
532 @cindex prerequisites, introduction to
533 @cindex recipes, introduction to
534 @example
535 @group
536 @var{target} @dots{} : @var{prerequisites} @dots{}
537         @var{recipe}
538         @dots{}
539         @dots{}
540 @end group
541 @end example
543 A @dfn{target} is usually the name of a file that is generated by a
544 program; examples of targets are executable or object files.  A target
545 can also be the name of an action to carry out, such as @samp{clean}
546 (@pxref{Phony Targets}).
548 A @dfn{prerequisite} is a file that is used as input to create the
549 target.  A target often depends on several files.
551 @cindex tabs in rules
552 A @dfn{recipe} is an action that @code{make} carries out.  A recipe
553 may have more than one command, either on the same line or each on its
554 own line.  @strong{Please note:} you need to put a tab character at
555 the beginning of every recipe line!  This is an obscurity that catches
556 the unwary.  If you prefer to prefix your recipes with a character
557 other than tab, you can set the @code{.RECIPEPREFIX} variable to an
558 alternate character (@pxref{Special Variables}).
560 Usually a recipe is in a rule with prerequisites and serves to create a
561 target file if any of the prerequisites change.  However, the rule that
562 specifies a recipe for the target need not have prerequisites.  For
563 example, the rule containing the delete command associated with the
564 target @samp{clean} does not have prerequisites.
566 A @dfn{rule}, then, explains how and when to remake certain files
567 which are the targets of the particular rule.  @code{make} carries out
568 the recipe on the prerequisites to create or update the target.  A
569 rule can also explain how and when to carry out an action.
570 @xref{Rules, , Writing Rules}.
572 A makefile may contain other text besides rules, but a simple makefile
573 need only contain rules.  Rules may look somewhat more complicated
574 than shown in this template, but all fit the pattern more or less.
576 @node Simple Makefile, How Make Works, Rule Introduction, Introduction
577 @section A Simple Makefile
578 @cindex simple makefile
579 @cindex makefile, simple
581 Here is a straightforward makefile that describes the way an
582 executable file called @code{edit} depends on eight object files
583 which, in turn, depend on eight C source and three header files.
585 In this example, all the C files include @file{defs.h}, but only those
586 defining editing commands include @file{command.h}, and only low
587 level files that change the editor buffer include @file{buffer.h}.
589 @example
590 @group
591 edit : main.o kbd.o command.o display.o \
592        insert.o search.o files.o utils.o
593         cc -o edit main.o kbd.o command.o display.o \
594                    insert.o search.o files.o utils.o
596 main.o : main.c defs.h
597         cc -c main.c
598 kbd.o : kbd.c defs.h command.h
599         cc -c kbd.c
600 command.o : command.c defs.h command.h
601         cc -c command.c
602 display.o : display.c defs.h buffer.h
603         cc -c display.c
604 insert.o : insert.c defs.h buffer.h
605         cc -c insert.c
606 search.o : search.c defs.h buffer.h
607         cc -c search.c
608 files.o : files.c defs.h buffer.h command.h
609         cc -c files.c
610 utils.o : utils.c defs.h
611         cc -c utils.c
612 clean :
613         rm edit main.o kbd.o command.o display.o \
614            insert.o search.o files.o utils.o
615 @end group
616 @end example
618 @noindent
619 We split each long line into two lines using backslash/newline; this is
620 like using one long line, but is easier to read.  @xref{Splitting Lines,
621 , Splitting Long Lines}.
622 @cindex continuation lines
623 @cindex @code{\} (backslash), for continuation lines
624 @cindex backslash (@code{\}), for continuation lines
625 @cindex quoting newline, in makefile
626 @cindex newline, quoting, in makefile
628 To use this makefile to create the executable file called @file{edit},
629 type:
631 @example
632 make
633 @end example
635 To use this makefile to delete the executable file and all the object
636 files from the directory, type:
638 @example
639 make clean
640 @end example
642 In the example makefile, the targets include the executable file
643 @samp{edit}, and the object files @samp{main.o} and @samp{kbd.o}.  The
644 prerequisites are files such as @samp{main.c} and @samp{defs.h}.
645 In fact, each @samp{.o} file is both a target and a prerequisite.
646 Recipes include @w{@samp{cc -c main.c}} and @w{@samp{cc -c kbd.c}}.
648 When a target is a file, it needs to be recompiled or relinked if any
649 of its prerequisites change.  In addition, any prerequisites that are
650 themselves automatically generated should be updated first.  In this
651 example, @file{edit} depends on each of the eight object files; the
652 object file @file{main.o} depends on the source file @file{main.c} and
653 on the header file @file{defs.h}.
655 A recipe may follow each line that contains a target and
656 prerequisites.  These recipes say how to update the target file.  A
657 tab character (or whatever character is specified by the
658 @code{.RECIPEPREFIX} variable; @pxref{Special Variables}) must come at
659 the beginning of every line in the recipe to distinguish recipes from
660 other lines in the makefile.  (Bear in mind that @code{make} does not
661 know anything about how the recipes work.  It is up to you to supply
662 recipes that will update the target file properly.  All @code{make}
663 does is execute the recipe you have specified when the target file
664 needs to be updated.)@refill
665 @cindex recipe
667 The target @samp{clean} is not a file, but merely the name of an
668 action.  Since you normally do not want to carry out the actions in
669 this rule, @samp{clean} is not a prerequisite of any other rule.
670 Consequently, @code{make} never does anything with it unless you tell
671 it specifically.  Note that this rule not only is not a prerequisite,
672 it also does not have any prerequisites, so the only purpose of the
673 rule is to run the specified recipe.  Targets that do not refer to
674 files but are just actions are called @dfn{phony targets}.
675 @xref{Phony Targets}, for information about this kind of target.
676 @xref{Errors, , Errors in Recipes}, to see how to cause @code{make}
677 to ignore errors from @code{rm} or any other command.
678 @cindex @code{clean} target
679 @cindex @code{rm} (shell command)
681 @node How Make Works, Variables Simplify, Simple Makefile, Introduction
682 @comment  node-name,  next,  previous,  up
683 @section How @code{make} Processes a Makefile
684 @cindex processing a makefile
685 @cindex makefile, how @code{make} processes
687 By default, @code{make} starts with the first target (not targets whose
688 names start with @samp{.}).  This is called the @dfn{default goal}.
689 (@dfn{Goals} are the targets that @code{make} strives ultimately to
690 update.    You can override this behavior using the command line
691 (@pxref{Goals, , Arguments to Specify the Goals}) or with the
692 @code{.DEFAULT_GOAL} special variable (@pxref{Special Variables, ,
693 Other Special Variables}).
694 @cindex default goal
695 @cindex goal, default
696 @cindex goal
698 In the simple example of the previous section, the default goal is to
699 update the executable program @file{edit}; therefore, we put that rule
700 first.
702 Thus, when you give the command:
704 @example
705 make
706 @end example
708 @noindent
709 @code{make} reads the makefile in the current directory and begins by
710 processing the first rule.  In the example, this rule is for relinking
711 @file{edit}; but before @code{make} can fully process this rule, it
712 must process the rules for the files that @file{edit} depends on,
713 which in this case are the object files.  Each of these files is
714 processed according to its own rule.  These rules say to update each
715 @samp{.o} file by compiling its source file.  The recompilation must
716 be done if the source file, or any of the header files named as
717 prerequisites, is more recent than the object file, or if the object
718 file does not exist.
720 The other rules are processed because their targets appear as
721 prerequisites of the goal.  If some other rule is not depended on by the
722 goal (or anything it depends on, etc.), that rule is not processed,
723 unless you tell @code{make} to do so (with a command such as
724 @w{@code{make clean}}).
726 Before recompiling an object file, @code{make} considers updating its
727 prerequisites, the source file and header files.  This makefile does not
728 specify anything to be done for them---the @samp{.c} and @samp{.h} files
729 are not the targets of any rules---so @code{make} does nothing for these
730 files.  But @code{make} would update automatically generated C programs,
731 such as those made by Bison or Yacc, by their own rules at this time.
733 After recompiling whichever object files need it, @code{make} decides
734 whether to relink @file{edit}.  This must be done if the file
735 @file{edit} does not exist, or if any of the object files are newer than
736 it.  If an object file was just recompiled, it is now newer than
737 @file{edit}, so @file{edit} is relinked.
738 @cindex relinking
740 Thus, if we change the file @file{insert.c} and run @code{make},
741 @code{make} will compile that file to update @file{insert.o}, and then
742 link @file{edit}.  If we change the file @file{command.h} and run
743 @code{make}, @code{make} will recompile the object files @file{kbd.o},
744 @file{command.o} and @file{files.o} and then link the file @file{edit}.
746 @node Variables Simplify, make Deduces, How Make Works, Introduction
747 @section Variables Make Makefiles Simpler
748 @cindex variables
749 @cindex simplifying with variables
751 In our example, we had to list all the object files twice in the rule for
752 @file{edit} (repeated here):
754 @example
755 @group
756 edit : main.o kbd.o command.o display.o \
757               insert.o search.o files.o utils.o
758         cc -o edit main.o kbd.o command.o display.o \
759                    insert.o search.o files.o utils.o
760 @end group
761 @end example
763 @cindex @code{objects}
764 Such duplication is error-prone; if a new object file is added to the
765 system, we might add it to one list and forget the other.  We can eliminate
766 the risk and simplify the makefile by using a variable.  @dfn{Variables}
767 allow a text string to be defined once and substituted in multiple places
768 later (@pxref{Using Variables, ,How to Use Variables}).
770 @cindex @code{OBJECTS}
771 @cindex @code{objs}
772 @cindex @code{OBJS}
773 @cindex @code{obj}
774 @cindex @code{OBJ}
775 It is standard practice for every makefile to have a variable named
776 @code{objects}, @code{OBJECTS}, @code{objs}, @code{OBJS}, @code{obj},
777 or @code{OBJ} which is a list of all object file names.  We would
778 define such a variable @code{objects} with a line like this in the
779 makefile:@refill
781 @example
782 @group
783 objects = main.o kbd.o command.o display.o \
784           insert.o search.o files.o utils.o
785 @end group
786 @end example
788 @noindent
789 Then, each place we want to put a list of the object file names, we can
790 substitute the variable's value by writing @samp{$(objects)}
791 (@pxref{Using Variables, ,How to Use Variables}).
793 Here is how the complete simple makefile looks when you use a variable
794 for the object files:
796 @example
797 @group
798 objects = main.o kbd.o command.o display.o \
799           insert.o search.o files.o utils.o
801 edit : $(objects)
802         cc -o edit $(objects)
803 main.o : main.c defs.h
804         cc -c main.c
805 kbd.o : kbd.c defs.h command.h
806         cc -c kbd.c
807 command.o : command.c defs.h command.h
808         cc -c command.c
809 display.o : display.c defs.h buffer.h
810         cc -c display.c
811 insert.o : insert.c defs.h buffer.h
812         cc -c insert.c
813 search.o : search.c defs.h buffer.h
814         cc -c search.c
815 files.o : files.c defs.h buffer.h command.h
816         cc -c files.c
817 utils.o : utils.c defs.h
818         cc -c utils.c
819 clean :
820         rm edit $(objects)
821 @end group
822 @end example
824 @node make Deduces, Combine By Prerequisite, Variables Simplify, Introduction
825 @section Letting @code{make} Deduce the Recipes
826 @cindex deducing recipes (implicit rules)
827 @cindex implicit rule, introduction to
828 @cindex rule, implicit, introduction to
830 It is not necessary to spell out the recipes for compiling the individual
831 C source files, because @code{make} can figure them out: it has an
832 @dfn{implicit rule} for updating a @samp{.o} file from a correspondingly
833 named @samp{.c} file using a @samp{cc -c} command.  For example, it will
834 use the recipe @samp{cc -c main.c -o main.o} to compile @file{main.c} into
835 @file{main.o}.  We can therefore omit the recipes from the rules for the
836 object files.  @xref{Implicit Rules, ,Using Implicit Rules}.@refill
838 When a @samp{.c} file is used automatically in this way, it is also
839 automatically added to the list of prerequisites.  We can therefore omit
840 the @samp{.c} files from the prerequisites, provided we omit the recipe.
842 Here is the entire example, with both of these changes, and a variable
843 @code{objects} as suggested above:
845 @example
846 @group
847 objects = main.o kbd.o command.o display.o \
848           insert.o search.o files.o utils.o
850 edit : $(objects)
851         cc -o edit $(objects)
853 main.o : defs.h
854 kbd.o : defs.h command.h
855 command.o : defs.h command.h
856 display.o : defs.h buffer.h
857 insert.o : defs.h buffer.h
858 search.o : defs.h buffer.h
859 files.o : defs.h buffer.h command.h
860 utils.o : defs.h
862 .PHONY : clean
863 clean :
864         rm edit $(objects)
865 @end group
866 @end example
868 @noindent
869 This is how we would write the makefile in actual practice.  (The
870 complications associated with @samp{clean} are described elsewhere.
871 See @ref{Phony Targets}, and @ref{Errors, ,Errors in Recipes}.)
873 Because implicit rules are so convenient, they are important.  You
874 will see them used frequently.@refill
876 @node Combine By Prerequisite, Cleanup, make Deduces, Introduction
877 @section Another Style of Makefile
878 @cindex combining rules by prerequisite
880 When the objects of a makefile are created only by implicit rules, an
881 alternative style of makefile is possible.  In this style of makefile,
882 you group entries by their prerequisites instead of by their targets.
883 Here is what one looks like:
885 @example
886 @group
887 objects = main.o kbd.o command.o display.o \
888           insert.o search.o files.o utils.o
890 edit : $(objects)
891         cc -o edit $(objects)
893 $(objects) : defs.h
894 kbd.o command.o files.o : command.h
895 display.o insert.o search.o files.o : buffer.h
896 @end group
897 @end example
899 @noindent
900 Here @file{defs.h} is given as a prerequisite of all the object files;
901 @file{command.h} and @file{buffer.h} are prerequisites of the specific
902 object files listed for them.
904 Whether this is better is a matter of taste: it is more compact, but some
905 people dislike it because they find it clearer to put all the information
906 about each target in one place.
908 @node Cleanup,  , Combine By Prerequisite, Introduction
909 @section Rules for Cleaning the Directory
910 @cindex cleaning up
911 @cindex removing, to clean up
913 Compiling a program is not the only thing you might want to write rules
914 for.  Makefiles commonly tell how to do a few other things besides
915 compiling a program: for example, how to delete all the object files
916 and executables so that the directory is @samp{clean}.
918 @cindex @code{clean} target
919 Here is how we
920 could write a @code{make} rule for cleaning our example editor:
922 @example
923 @group
924 clean:
925         rm edit $(objects)
926 @end group
927 @end example
929 In practice, we might want to write the rule in a somewhat more
930 complicated manner to handle unanticipated situations.  We would do this:
932 @example
933 @group
934 .PHONY : clean
935 clean :
936         -rm edit $(objects)
937 @end group
938 @end example
940 @noindent
941 This prevents @code{make} from getting confused by an actual file
942 called @file{clean} and causes it to continue in spite of errors from
943 @code{rm}.  (See @ref{Phony Targets}, and @ref{Errors, ,Errors in
944 Recipes}.)
946 @noindent
947 A rule such as this should not be placed at the beginning of the
948 makefile, because we do not want it to run by default!  Thus, in the
949 example makefile, we want the rule for @code{edit}, which recompiles
950 the editor, to remain the default goal.
952 Since @code{clean} is not a prerequisite of @code{edit}, this rule will not
953 run at all if we give the command @samp{make} with no arguments.  In
954 order to make the rule run, we have to type @samp{make clean}.
955 @xref{Running, ,How to Run @code{make}}.
957 @node Makefiles, Rules, Introduction, Top
958 @chapter Writing Makefiles
960 @cindex makefile, how to write
961 The information that tells @code{make} how to recompile a system comes from
962 reading a data base called the @dfn{makefile}.
964 @menu
965 * Makefile Contents::           What makefiles contain.
966 * Makefile Names::              How to name your makefile.
967 * Include::                     How one makefile can use another makefile.
968 * MAKEFILES Variable::          The environment can specify extra makefiles.
969 * Remaking Makefiles::          How makefiles get remade.
970 * Overriding Makefiles::        How to override part of one makefile
971                                   with another makefile.
972 * Reading Makefiles::           How makefiles are parsed.
973 * Secondary Expansion::         How and when secondary expansion is performed.
974 @end menu
976 @node Makefile Contents, Makefile Names, Makefiles, Makefiles
977 @section What Makefiles Contain
979 Makefiles contain five kinds of things: @dfn{explicit rules},
980 @dfn{implicit rules}, @dfn{variable definitions}, @dfn{directives},
981 and @dfn{comments}.  Rules, variables, and directives are described at
982 length in later chapters.@refill
984 @itemize @bullet
985 @cindex rule, explicit, definition of
986 @cindex explicit rule, definition of
987 @item
988 An @dfn{explicit rule} says when and how to remake one or more files,
989 called the rule's @dfn{targets}.  It lists the other files that the
990 targets depend on, called the @dfn{prerequisites} of the target, and
991 may also give a recipe to use to create or update the targets.
992 @xref{Rules, ,Writing Rules}.
994 @cindex rule, implicit, definition of
995 @cindex implicit rule, definition of
996 @item
997 An @dfn{implicit rule} says when and how to remake a class of files
998 based on their names.  It describes how a target may depend on a file
999 with a name similar to the target and gives a recipe to create or
1000 update such a target.  @xref{Implicit Rules, ,Using Implicit Rules}.
1002 @cindex variable definition
1003 @item
1004 A @dfn{variable definition} is a line that specifies a text string
1005 value for a variable that can be substituted into the text later.  The
1006 simple makefile example shows a variable definition for @code{objects}
1007 as a list of all object files (@pxref{Variables Simplify, , Variables
1008 Make Makefiles Simpler}).
1010 @cindex directive
1011 @item
1012 A @dfn{directive} is an instruction for @code{make} to do something
1013 special while reading the makefile.  These include:
1015 @itemize @bullet
1016 @item
1017 Reading another makefile (@pxref{Include, ,Including Other Makefiles}).
1019 @item
1020 Deciding (based on the values of variables) whether to use or
1021 ignore a part of the makefile (@pxref{Conditionals, ,Conditional Parts of Makefiles}).
1023 @item
1024 Defining a variable from a verbatim string containing multiple lines
1025 (@pxref{Multi-Line, ,Defining Multi-Line Variables}).
1026 @end itemize
1028 @cindex comments, in makefile
1029 @cindex @code{#} (comments), in makefile
1030 @item
1031 @samp{#} in a line of a makefile starts a @dfn{comment}.  It and the
1032 rest of the line are ignored, except that a trailing backslash not
1033 escaped by another backslash will continue the comment across multiple
1034 lines.  A line containing just a comment (with perhaps spaces before
1035 it) is effectively blank, and is ignored.  If you want a literal
1036 @code{#}, escape it with a backslash (e.g., @code{\#}).  Comments may
1037 appear on any line in the makefile, although they are treated
1038 specially in certain situations.
1040 You cannot use comments within variable references or function calls:
1041 any instance of @code{#} will be treated literally (rather than as the
1042 start of a comment) inside a variable reference or function call.
1044 Comments within a recipe are passed to the shell, just as with any
1045 other recipe text.  The shell decides how to interpret it: whether or
1046 not this is a comment is up to the shell.
1048 Within a @code{define} directive, comments are not ignored during the
1049 definition of the variable, but rather kept intact in the value of the
1050 variable.  When the variable is expanded they will either be treated
1051 as @code{make} comments or as recipe text, depending on the context in
1052 which the variable is evaluated.
1053 @end itemize
1055 @menu
1056 * Splitting Lines::             Splitting long lines in makefiles
1057 @end menu
1059 @node Splitting Lines,  , Makefile Contents, Makefile Contents
1060 @subsection Splitting Long Lines
1061 @cindex splitting long lines
1062 @cindex long lines, splitting
1063 @cindex backslash (@code{\}), to quote newlines
1065 Makefiles use a ``line-based'' syntax in which the newline character
1066 is special and marks the end of a statement.  GNU @code{make} has no
1067 limit on the length of a statement line, up to the amount of memory in
1068 your computer.
1070 However, it is difficult to read lines which are too long to display
1071 without wrapping or scrolling.  So, you can format your makefiles for
1072 readability by adding newlines into the middle of a statement: you do
1073 this by escaping the internal newlines with a backslash (@code{\})
1074 character.  Where we need to make a distinction we will refer to
1075 ``physical lines'' as a single line ending with a newline (regardless
1076 of whether it is escaped) and a ``logical line'' being a complete
1077 statement including all escaped newlines up to the first non-escaped
1078 newline.
1080 The way in which backslash/newline combinations are handled depends on
1081 whether the statement is a recipe line or a non-recipe line.  Handling
1082 of backslash/newline in a recipe line is discussed later
1083 (@pxref{Splitting Recipe Lines}).
1085 Outside of recipe lines, backslash/newlines are converted into a
1086 single space character.  Once that is done, all whitespace around the
1087 backslash/newline is condensed into a single space: this includes all
1088 whitespace preceding the backslash, all whitespace at the beginning of
1089 the line after the backslash/newline, and any consecutive
1090 backslash/newline combinations.
1092 If the @code{.POSIX} special target is defined then backslash/newline
1093 handling is modified slightly to conform to POSIX.2: first, whitespace
1094 preceding a backslash is not removed and second, consecutive
1095 backslash/newlines are not condensed.
1097 @node Makefile Names, Include, Makefile Contents, Makefiles
1098 @section What Name to Give Your Makefile
1099 @cindex makefile name
1100 @cindex name of makefile
1101 @cindex default makefile name
1102 @cindex file name of makefile
1104 @c following paragraph rewritten to avoid overfull hbox
1105 By default, when @code{make} looks for the makefile, it tries the
1106 following names, in order: @file{GNUmakefile}, @file{makefile}
1107 and @file{Makefile}.@refill
1108 @findex Makefile
1109 @findex GNUmakefile
1110 @findex makefile
1112 @cindex @code{README}
1113 Normally you should call your makefile either @file{makefile} or
1114 @file{Makefile}.  (We recommend @file{Makefile} because it appears
1115 prominently near the beginning of a directory listing, right near other
1116 important files such as @file{README}.)  The first name checked,
1117 @file{GNUmakefile}, is not recommended for most makefiles.  You should
1118 use this name if you have a makefile that is specific to GNU
1119 @code{make}, and will not be understood by other versions of
1120 @code{make}.  Other @code{make} programs look for @file{makefile} and
1121 @file{Makefile}, but not @file{GNUmakefile}.
1123 If @code{make} finds none of these names, it does not use any makefile.
1124 Then you must specify a goal with a command argument, and @code{make}
1125 will attempt to figure out how to remake it using only its built-in
1126 implicit rules.  @xref{Implicit Rules, ,Using Implicit Rules}.
1128 @cindex @code{-f}
1129 @cindex @code{--file}
1130 @cindex @code{--makefile}
1131 If you want to use a nonstandard name for your makefile, you can specify
1132 the makefile name with the @samp{-f} or @samp{--file} option.  The
1133 arguments @w{@samp{-f @var{name}}} or @w{@samp{--file=@var{name}}} tell
1134 @code{make} to read the file @var{name} as the makefile.  If you use
1135 more than one @samp{-f} or @samp{--file} option, you can specify several
1136 makefiles.  All the makefiles are effectively concatenated in the order
1137 specified.  The default makefile names @file{GNUmakefile},
1138 @file{makefile} and @file{Makefile} are not checked automatically if you
1139 specify @samp{-f} or @samp{--file}.@refill
1140 @cindex specifying makefile name
1141 @cindex makefile name, how to specify
1142 @cindex name of makefile, how to specify
1143 @cindex file name of makefile, how to specify
1145 @node Include, MAKEFILES Variable, Makefile Names, Makefiles
1146 @section Including Other Makefiles
1147 @cindex including other makefiles
1148 @cindex makefile, including
1150 @findex include
1151 The @code{include} directive tells @code{make} to suspend reading the
1152 current makefile and read one or more other makefiles before continuing.
1153 The directive is a line in the makefile that looks like this:
1155 @example
1156 include @var{filenames}@dots{}
1157 @end example
1159 @noindent
1160 @var{filenames} can contain shell file name patterns.  If
1161 @var{filenames} is empty, nothing is included and no error is printed.
1162 @cindex shell file name pattern (in @code{include})
1163 @cindex shell wildcards (in @code{include})
1164 @cindex wildcard, in @code{include}
1166 Extra spaces are allowed and ignored at the beginning of the line, but
1167 the first character must not be a tab (or the value of
1168 @code{.RECIPEPREFIX})---if the line begins with a tab, it will be
1169 considered a recipe line.  Whitespace is required between
1170 @code{include} and the file names, and between file names; extra
1171 whitespace is ignored there and at the end of the directive.  A
1172 comment starting with @samp{#} is allowed at the end of the line.  If
1173 the file names contain any variable or function references, they are
1174 expanded.  @xref{Using Variables, ,How to Use Variables}.
1176 For example, if you have three @file{.mk} files, @file{a.mk},
1177 @file{b.mk}, and @file{c.mk}, and @code{$(bar)} expands to
1178 @code{bish bash}, then the following expression
1180 @example
1181 include foo *.mk $(bar)
1182 @end example
1184 is equivalent to
1186 @example
1187 include foo a.mk b.mk c.mk bish bash
1188 @end example
1190 When @code{make} processes an @code{include} directive, it suspends
1191 reading of the containing makefile and reads from each listed file in
1192 turn.  When that is finished, @code{make} resumes reading the
1193 makefile in which the directive appears.
1195 One occasion for using @code{include} directives is when several programs,
1196 handled by individual makefiles in various directories, need to use a
1197 common set of variable definitions
1198 (@pxref{Setting, ,Setting Variables}) or pattern rules
1199 (@pxref{Pattern Rules, ,Defining and Redefining Pattern Rules}).
1201 Another such occasion is when you want to generate prerequisites from
1202 source files automatically; the prerequisites can be put in a file that
1203 is included by the main makefile.  This practice is generally cleaner
1204 than that of somehow appending the prerequisites to the end of the main
1205 makefile as has been traditionally done with other versions of
1206 @code{make}.  @xref{Automatic Prerequisites}.
1207 @cindex prerequisites, automatic generation
1208 @cindex automatic generation of prerequisites
1209 @cindex generating prerequisites automatically
1211 @cindex @code{-I}
1212 @cindex @code{--include-dir}
1213 @cindex included makefiles, default directories
1214 @cindex default directories for included makefiles
1215 @findex /usr/gnu/include
1216 @findex /usr/local/include
1217 @findex /usr/include
1218 If the specified name does not start with a slash, and the file is not
1219 found in the current directory, several other directories are searched.
1220 First, any directories you have specified with the @samp{-I} or
1221 @samp{--include-dir} option are searched
1222 (@pxref{Options Summary, ,Summary of Options}).
1223 Then the following directories (if they exist)
1224 are searched, in this order:
1225 @file{@var{prefix}/include} (normally @file{/usr/local/include}
1226 @footnote{GNU Make compiled for MS-DOS and MS-Windows behaves as if
1227 @var{prefix} has been defined to be the root of the DJGPP tree
1228 hierarchy.})
1229 @file{/usr/gnu/include},
1230 @file{/usr/local/include}, @file{/usr/include}.
1232 If an included makefile cannot be found in any of these directories, a
1233 warning message is generated, but it is not an immediately fatal error;
1234 processing of the makefile containing the @code{include} continues.
1235 Once it has finished reading makefiles, @code{make} will try to remake
1236 any that are out of date or don't exist.
1237 @xref{Remaking Makefiles, ,How Makefiles Are Remade}.
1238 Only after it has tried to find a way to remake a makefile and failed,
1239 will @code{make} diagnose the missing makefile as a fatal error.
1241 If you want @code{make} to simply ignore a makefile which does not exist
1242 or cannot be remade, with no error message, use the @w{@code{-include}}
1243 directive instead of @code{include}, like this:
1245 @example
1246 -include @var{filenames}@dots{}
1247 @end example
1249 This acts like @code{include} in every way except that there is no
1250 error (not even a warning) if any of the @var{filenames} (or any
1251 prerequisites of any of the @var{filenames}) do not exist or cannot be
1252 remade.
1254 For compatibility with some other @code{make} implementations,
1255 @code{sinclude} is another name for @w{@code{-include}}.
1257 @node MAKEFILES Variable, Remaking Makefiles, Include, Makefiles
1258 @section The Variable @code{MAKEFILES}
1259 @cindex makefile, and @code{MAKEFILES} variable
1260 @cindex including (@code{MAKEFILES} variable)
1262 @vindex MAKEFILES
1263 If the environment variable @code{MAKEFILES} is defined, @code{make}
1264 considers its value as a list of names (separated by whitespace) of
1265 additional makefiles to be read before the others.  This works much
1266 like the @code{include} directive: various directories are searched
1267 for those files (@pxref{Include, ,Including Other Makefiles}).  In
1268 addition, the default goal is never taken from one of these makefiles
1269 (or any makefile included by them) and it is not an error if the files
1270 listed in @code{MAKEFILES} are not found.@refill
1272 @cindex recursion, and @code{MAKEFILES} variable
1273 The main use of @code{MAKEFILES} is in communication between recursive
1274 invocations of @code{make} (@pxref{Recursion, ,Recursive Use of
1275 @code{make}}).  It usually is not desirable to set the environment
1276 variable before a top-level invocation of @code{make}, because it is
1277 usually better not to mess with a makefile from outside.  However, if
1278 you are running @code{make} without a specific makefile, a makefile in
1279 @code{MAKEFILES} can do useful things to help the built-in implicit
1280 rules work better, such as defining search paths (@pxref{Directory Search}).
1282 Some users are tempted to set @code{MAKEFILES} in the environment
1283 automatically on login, and program makefiles to expect this to be done.
1284 This is a very bad idea, because such makefiles will fail to work if run by
1285 anyone else.  It is much better to write explicit @code{include} directives
1286 in the makefiles.  @xref{Include, , Including Other Makefiles}.
1288 @node Remaking Makefiles, Overriding Makefiles, MAKEFILES Variable, Makefiles
1289 @section How Makefiles Are Remade
1291 @cindex updating makefiles
1292 @cindex remaking makefiles
1293 @cindex makefile, remaking of
1294 Sometimes makefiles can be remade from other files, such as RCS or SCCS
1295 files.  If a makefile can be remade from other files, you probably want
1296 @code{make} to get an up-to-date version of the makefile to read in.
1298 To this end, after reading in all makefiles, @code{make} will consider
1299 each as a goal target and attempt to update it.  If a makefile has a
1300 rule which says how to update it (found either in that very makefile or
1301 in another one) or if an implicit rule applies to it (@pxref{Implicit
1302 Rules, ,Using Implicit Rules}), it will be updated if necessary.  After
1303 all makefiles have been checked, if any have actually been changed,
1304 @code{make} starts with a clean slate and reads all the makefiles over
1305 again.  (It will also attempt to update each of them over again, but
1306 normally this will not change them again, since they are already up to
1307 date.)@refill
1309 If you know that one or more of your makefiles cannot be remade and
1310 you want to keep @code{make} from performing an implicit rule search
1311 on them, perhaps for efficiency reasons, you can use any normal method
1312 of preventing implicit rule look-up to do so.  For example, you can
1313 write an explicit rule with the makefile as the target, and an empty
1314 recipe (@pxref{Empty Recipes, ,Using Empty Recipes}).
1316 If the makefiles specify a double-colon rule to remake a file with
1317 a recipe but no prerequisites, that file will always be remade
1318 (@pxref{Double-Colon}).  In the case of makefiles, a makefile that has a
1319 double-colon rule with a recipe but no prerequisites will be remade every
1320 time @code{make} is run, and then again after @code{make} starts over
1321 and reads the makefiles in again.  This would cause an infinite loop:
1322 @code{make} would constantly remake the makefile, and never do anything
1323 else.  So, to avoid this, @code{make} will @strong{not} attempt to
1324 remake makefiles which are specified as targets of a double-colon rule
1325 with a recipe but no prerequisites.@refill
1327 If you do not specify any makefiles to be read with @samp{-f} or
1328 @samp{--file} options, @code{make} will try the default makefile names;
1329 @pxref{Makefile Names, ,What Name to Give Your Makefile}.  Unlike
1330 makefiles explicitly requested with @samp{-f} or @samp{--file} options,
1331 @code{make} is not certain that these makefiles should exist.  However,
1332 if a default makefile does not exist but can be created by running
1333 @code{make} rules, you probably want the rules to be run so that the
1334 makefile can be used.
1336 Therefore, if none of the default makefiles exists, @code{make} will try
1337 to make each of them in the same order in which they are searched for
1338 (@pxref{Makefile Names, ,What Name to Give Your Makefile})
1339 until it succeeds in making one, or it runs out of names to try.  Note
1340 that it is not an error if @code{make} cannot find or make any makefile;
1341 a makefile is not always necessary.@refill
1343 When you use the @samp{-t} or @samp{--touch} option
1344 (@pxref{Instead of Execution, ,Instead of Executing Recipes}),
1345 you would not want to use an out-of-date makefile to decide which
1346 targets to touch.  So the @samp{-t} option has no effect on updating
1347 makefiles; they are really updated even if @samp{-t} is specified.
1348 Likewise, @samp{-q} (or @samp{--question}) and @samp{-n} (or
1349 @samp{--just-print}) do not prevent updating of makefiles, because an
1350 out-of-date makefile would result in the wrong output for other targets.
1351 Thus, @samp{make -f mfile -n foo} will update @file{mfile}, read it in,
1352 and then print the recipe to update @file{foo} and its prerequisites
1353 without running it.  The recipe printed for @file{foo} will be the one
1354 specified in the updated contents of @file{mfile}.
1356 However, on occasion you might actually wish to prevent updating of even
1357 the makefiles.  You can do this by specifying the makefiles as goals in
1358 the command line as well as specifying them as makefiles.  When the
1359 makefile name is specified explicitly as a goal, the options @samp{-t}
1360 and so on do apply to them.
1362 Thus, @samp{make -f mfile -n mfile foo} would read the makefile
1363 @file{mfile}, print the recipe needed to update it without actually
1364 running it, and then print the recipe needed to update @file{foo}
1365 without running that.  The recipe for @file{foo} will be the one
1366 specified by the existing contents of @file{mfile}.
1368 @node Overriding Makefiles, Reading Makefiles, Remaking Makefiles, Makefiles
1369 @section Overriding Part of Another Makefile
1371 @cindex overriding makefiles
1372 @cindex makefile, overriding
1373 Sometimes it is useful to have a makefile that is mostly just like
1374 another makefile.  You can often use the @samp{include} directive to
1375 include one in the other, and add more targets or variable definitions.
1376 However, it is invalid for two makefiles to give different recipes for
1377 the same target.  But there is another way.
1379 @cindex match-anything rule, used to override
1380 In the containing makefile (the one that wants to include the other),
1381 you can use a match-anything pattern rule to say that to remake any
1382 target that cannot be made from the information in the containing
1383 makefile, @code{make} should look in another makefile.
1384 @xref{Pattern Rules}, for more information on pattern rules.
1386 For example, if you have a makefile called @file{Makefile} that says how
1387 to make the target @samp{foo} (and other targets), you can write a
1388 makefile called @file{GNUmakefile} that contains:
1390 @example
1391 foo:
1392         frobnicate > foo
1394 %: force
1395         @@$(MAKE) -f Makefile $@@
1396 force: ;
1397 @end example
1399 If you say @samp{make foo}, @code{make} will find @file{GNUmakefile},
1400 read it, and see that to make @file{foo}, it needs to run the recipe
1401 @samp{frobnicate > foo}.  If you say @samp{make bar}, @code{make} will
1402 find no way to make @file{bar} in @file{GNUmakefile}, so it will use the
1403 recipe from the pattern rule: @samp{make -f Makefile bar}.  If
1404 @file{Makefile} provides a rule for updating @file{bar}, @code{make}
1405 will apply the rule.  And likewise for any other target that
1406 @file{GNUmakefile} does not say how to make.
1408 The way this works is that the pattern rule has a pattern of just
1409 @samp{%}, so it matches any target whatever.  The rule specifies a
1410 prerequisite @file{force}, to guarantee that the recipe will be run even
1411 if the target file already exists.  We give the @file{force} target an
1412 empty recipe to prevent @code{make} from searching for an implicit rule to
1413 build it---otherwise it would apply the same match-anything rule to
1414 @file{force} itself and create a prerequisite loop!
1416 @node Reading Makefiles,  Secondary Expansion, Overriding Makefiles, Makefiles
1417 @section How @code{make} Reads a Makefile
1418 @cindex reading makefiles
1419 @cindex makefile, parsing
1421 GNU @code{make} does its work in two distinct phases.  During the first
1422 phase it reads all the makefiles, included makefiles, etc. and
1423 internalizes all the variables and their values, implicit and explicit
1424 rules, and constructs a dependency graph of all the targets and their
1425 prerequisites.  During the second phase, @code{make} uses these internal
1426 structures to determine what targets will need to be rebuilt and to
1427 invoke the rules necessary to do so.
1429 It's important to understand this two-phase approach because it has a
1430 direct impact on how variable and function expansion happens; this is
1431 often a source of some confusion when writing makefiles.  Here we will
1432 present a summary of the phases in which expansion happens for different
1433 constructs within the makefile.  We say that expansion is
1434 @dfn{immediate} if it happens during the first phase: in this case
1435 @code{make} will expand any variables or functions in that section of a
1436 construct as the makefile is parsed.  We say that expansion is
1437 @dfn{deferred} if expansion is not performed immediately.  Expansion of
1438 a deferred construct is not performed until either the construct appears
1439 later in an immediate context, or until the second phase.
1441 You may not be familiar with some of these constructs yet.  You can
1442 reference this section as you become familiar with them, in later
1443 chapters.
1445 @subheading Variable Assignment
1446 @cindex +=, expansion
1447 @cindex =, expansion
1448 @cindex ?=, expansion
1449 @cindex +=, expansion
1450 @cindex !=, expansion
1451 @cindex define, expansion
1453 Variable definitions are parsed as follows:
1455 @example
1456 @var{immediate} = @var{deferred}
1457 @var{immediate} ?= @var{deferred}
1458 @var{immediate} := @var{immediate}
1459 @var{immediate} ::= @var{immediate}
1460 @var{immediate} += @var{deferred} or @var{immediate}
1461 @var{immediate} != @var{immediate}
1463 define @var{immediate}
1464   @var{deferred}
1465 endef
1467 define @var{immediate} =
1468   @var{deferred}
1469 endef
1471 define @var{immediate} ?=
1472   @var{deferred}
1473 endef
1475 define @var{immediate} :=
1476   @var{immediate}
1477 endef
1479 define @var{immediate} ::=
1480   @var{immediate}
1481 endef
1483 define @var{immediate} +=
1484   @var{deferred} or @var{immediate}
1485 endef
1487 define @var{immediate} !=
1488   @var{immediate}
1489 endef
1490 @end example
1492 For the append operator, @samp{+=}, the right-hand side is considered
1493 immediate if the variable was previously set as a simple variable
1494 (@samp{:=} or @samp{::=}), and deferred otherwise.
1496 For the shell assignment operator, @samp{!=}, the right-hand side is
1497 evaluated immediately and handed to the shell.  The result is stored in the
1498 variable named on the left, and that variable becomes a simple variable
1499 (and will thus be re-evaluated on each reference).
1501 @subheading Conditional Directives
1502 @cindex ifdef, expansion
1503 @cindex ifeq, expansion
1504 @cindex ifndef, expansion
1505 @cindex ifneq, expansion
1507 Conditional directives are parsed immediately.  This means, for
1508 example, that automatic variables cannot be used in conditional
1509 directives, as automatic variables are not set until the recipe for
1510 that rule is invoked.  If you need to use automatic variables in a
1511 conditional directive you @emph{must} move the condition into the
1512 recipe and use shell conditional syntax instead.
1514 @subheading Rule Definition
1515 @cindex target, expansion
1516 @cindex prerequisite, expansion
1517 @cindex implicit rule, expansion
1518 @cindex pattern rule, expansion
1519 @cindex explicit rule, expansion
1521 A rule is always expanded the same way, regardless of the form:
1523 @example
1524 @var{immediate} : @var{immediate} ; @var{deferred}
1525         @var{deferred}
1526 @end example
1528 That is, the target and prerequisite sections are expanded immediately,
1529 and the recipe used to construct the target is always deferred.  This
1530 general rule is true for explicit rules, pattern rules, suffix rules,
1531 static pattern rules, and simple prerequisite definitions.
1533 @node Secondary Expansion, , Reading Makefiles, Makefiles
1534 @section Secondary Expansion
1535 @cindex secondary expansion
1536 @cindex expansion, secondary
1538 @findex .SECONDEXPANSION
1539 In the previous section we learned that GNU @code{make} works in two
1540 distinct phases: a read-in phase and a target-update phase
1541 (@pxref{Reading Makefiles, , How @code{make} Reads a Makefile}).  GNU
1542 make also has the ability to enable a @emph{second expansion} of the
1543 prerequisites (only) for some or all targets defined in the makefile.
1544 In order for this second expansion to occur, the special target
1545 @code{.SECONDEXPANSION} must be defined before the first prerequisite
1546 list that makes use of this feature.
1548 If that special target is defined then in between the two phases
1549 mentioned above, right at the end of the read-in phase, all the
1550 prerequisites of the targets defined after the special target are
1551 expanded a @emph{second time}.  In most circumstances this secondary
1552 expansion will have no effect, since all variable and function
1553 references will have been expanded during the initial parsing of the
1554 makefiles.  In order to take advantage of the secondary expansion
1555 phase of the parser, then, it's necessary to @emph{escape} the
1556 variable or function reference in the makefile.  In this case the
1557 first expansion merely un-escapes the reference but doesn't expand it,
1558 and expansion is left to the secondary expansion phase.  For example,
1559 consider this makefile:
1561 @example
1562 .SECONDEXPANSION:
1563 ONEVAR = onefile
1564 TWOVAR = twofile
1565 myfile: $(ONEVAR) $$(TWOVAR)
1566 @end example
1568 After the first expansion phase the prerequisites list of the
1569 @file{myfile} target will be @code{onefile} and @code{$(TWOVAR)}; the
1570 first (unescaped) variable reference to @var{ONEVAR} is expanded,
1571 while the second (escaped) variable reference is simply unescaped,
1572 without being recognized as a variable reference.  Now during the
1573 secondary expansion the first word is expanded again but since it
1574 contains no variable or function references it remains the value
1575 @file{onefile}, while the second word is now a normal reference to the
1576 variable @var{TWOVAR}, which is expanded to the value @file{twofile}.
1577 The final result is that there are two prerequisites, @file{onefile}
1578 and @file{twofile}.
1580 Obviously, this is not a very interesting case since the same result
1581 could more easily have been achieved simply by having both variables
1582 appear, unescaped, in the prerequisites list.  One difference becomes
1583 apparent if the variables are reset; consider this example:
1585 @example
1586 .SECONDEXPANSION:
1587 AVAR = top
1588 onefile: $(AVAR)
1589 twofile: $$(AVAR)
1590 AVAR = bottom
1591 @end example
1593 Here the prerequisite of @file{onefile} will be expanded immediately,
1594 and resolve to the value @file{top}, while the prerequisite of
1595 @file{twofile} will not be full expanded until the secondary expansion
1596 and yield a value of @file{bottom}.
1598 This is marginally more exciting, but the true power of this feature
1599 only becomes apparent when you discover that secondary expansions
1600 always take place within the scope of the automatic variables for that
1601 target.  This means that you can use variables such as @code{$@@},
1602 @code{$*}, etc. during the second expansion and they will have their
1603 expected values, just as in the recipe.  All you have to do is defer
1604 the expansion by escaping the @code{$}.  Also, secondary expansion
1605 occurs for both explicit and implicit (pattern) rules.  Knowing this,
1606 the possible uses for this feature increase dramatically.  For
1607 example:
1609 @example
1610 .SECONDEXPANSION:
1611 main_OBJS := main.o try.o test.o
1612 lib_OBJS := lib.o api.o
1614 main lib: $$($$@@_OBJS)
1615 @end example
1617 Here, after the initial expansion the prerequisites of both the
1618 @file{main} and @file{lib} targets will be @code{$($@@_OBJS)}.  During
1619 the secondary expansion, the @code{$@@} variable is set to the name of
1620 the target and so the expansion for the @file{main} target will yield
1621 @code{$(main_OBJS)}, or @code{main.o try.o test.o}, while the
1622 secondary expansion for the @file{lib} target will yield
1623 @code{$(lib_OBJS)}, or @code{lib.o api.o}.
1625 You can also mix in functions here, as long as they are properly escaped:
1627 @example
1628 main_SRCS := main.c try.c test.c
1629 lib_SRCS := lib.c api.c
1631 .SECONDEXPANSION:
1632 main lib: $$(patsubst %.c,%.o,$$($$@@_SRCS))
1633 @end example
1635 This version allows users to specify source files rather than object
1636 files, but gives the same resulting prerequisites list as the previous
1637 example.
1639 Evaluation of automatic variables during the secondary expansion
1640 phase, especially of the target name variable @code{$$@@}, behaves
1641 similarly to evaluation within recipes.  However, there are some
1642 subtle differences and ``corner cases'' which come into play for the
1643 different types of rule definitions that @code{make} understands.  The
1644 subtleties of using the different automatic variables are described
1645 below.
1647 @subheading Secondary Expansion of Explicit Rules
1648 @cindex secondary expansion and explicit rules
1649 @cindex explicit rules, secondary expansion of
1651 During the secondary expansion of explicit rules, @code{$$@@} and
1652 @code{$$%} evaluate, respectively, to the file name of the target and,
1653 when the target is an archive member, the target member name.  The
1654 @code{$$<} variable evaluates to the first prerequisite in the first
1655 rule for this target.  @code{$$^} and @code{$$+} evaluate to the list
1656 of all prerequisites of rules @emph{that have already appeared} for
1657 the same target (@code{$$+} with repetitions and @code{$$^}
1658 without).  The following example will help illustrate these behaviors:
1660 @example
1661 .SECONDEXPANSION:
1663 foo: foo.1 bar.1 $$< $$^ $$+    # line #1
1665 foo: foo.2 bar.2 $$< $$^ $$+    # line #2
1667 foo: foo.3 bar.3 $$< $$^ $$+    # line #3
1668 @end example
1670 In the first prerequisite list, all three variables (@code{$$<},
1671 @code{$$^}, and @code{$$+}) expand to the empty string.  In the
1672 second, they will have values @code{foo.1}, @code{foo.1 bar.1}, and
1673 @code{foo.1 bar.1} respectively.  In the third they will have values
1674 @code{foo.1}, @code{foo.1 bar.1 foo.2 bar.2}, and @code{foo.1 bar.1
1675 foo.2 bar.2 foo.1 foo.1 bar.1 foo.1 bar.1} respectively.
1677 Rules undergo secondary expansion in makefile order, except that
1678 the rule with the recipe is always evaluated last.
1680 The variables @code{$$?} and @code{$$*} are not available and expand
1681 to the empty string.
1683 @subheading Secondary Expansion of Static Pattern Rules
1684 @cindex secondary expansion and static pattern rules
1685 @cindex static pattern rules, secondary expansion of
1687 Rules for secondary expansion of static pattern rules are identical to
1688 those for explicit rules, above, with one exception: for static
1689 pattern rules the @code{$$*} variable is set to the pattern stem.  As
1690 with explicit rules, @code{$$?} is not available and expands to the
1691 empty string.
1693 @subheading Secondary Expansion of Implicit Rules
1694 @cindex secondary expansion and implicit rules
1695 @cindex implicit rules, secondary expansion of
1697 As @code{make} searches for an implicit rule, it substitutes the stem
1698 and then performs secondary expansion for every rule with a matching
1699 target pattern.  The value of the automatic variables is derived in
1700 the same fashion as for static pattern rules.  As an example:
1702 @example
1703 .SECONDEXPANSION:
1705 foo: bar
1707 foo foz: fo%: bo%
1709 %oo: $$< $$^ $$+ $$*
1710 @end example
1712 When the implicit rule is tried for target @file{foo}, @code{$$<}
1713 expands to @file{bar}, @code{$$^} expands to @file{bar boo},
1714 @code{$$+} also expands to @file{bar boo}, and @code{$$*} expands to
1715 @file{f}.
1717 Note that the directory prefix (D), as described in @ref{Implicit Rule
1718 Search, ,Implicit Rule Search Algorithm}, is appended (after
1719 expansion) to all the patterns in the prerequisites list.  As an
1720 example:@refill
1722 @example
1723 .SECONDEXPANSION:
1725 /tmp/foo.o:
1727 %.o: $$(addsuffix /%.c,foo bar) foo.h
1728         @@echo $^
1729 @end example
1731 The prerequisite list printed, after the secondary expansion and
1732 directory prefix reconstruction, will be @file{/tmp/foo/foo.c
1733 /tmp/bar/foo.c foo.h}.  If you are not interested in this
1734 reconstruction, you can use @code{$$*} instead of @code{%} in the
1735 prerequisites list.
1737 @node Rules, Recipes, Makefiles, Top
1738 @chapter Writing Rules
1739 @cindex writing rules
1740 @cindex rule, how to write
1741 @cindex target
1742 @cindex prerequisite
1744 A @dfn{rule} appears in the makefile and says when and how to remake
1745 certain files, called the rule's @dfn{targets} (most often only one per rule).
1746 It lists the other files that are the @dfn{prerequisites} of the target, and
1747 the @dfn{recipe} to use to create or update the target.
1749 @cindex default goal
1750 @cindex goal, default
1751 The order of rules is not significant, except for determining the
1752 @dfn{default goal}: the target for @code{make} to consider, if you do
1753 not otherwise specify one.  The default goal is the target of the first
1754 rule in the first makefile.  If the first rule has multiple targets,
1755 only the first target is taken as the default.  There are two
1756 exceptions: a target starting with a period is not a default unless it
1757 contains one or more slashes, @samp{/}, as well; and, a target that
1758 defines a pattern rule has no effect on the default goal.
1759 (@xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.)
1761 Therefore, we usually write the makefile so that the first rule is the
1762 one for compiling the entire program or all the programs described by
1763 the makefile (often with a target called @samp{all}).
1764 @xref{Goals, ,Arguments to Specify the Goals}.
1766 @menu
1767 * Rule Example::                An example explained.
1768 * Rule Syntax::                 General syntax explained.
1769 * Prerequisite Types::          There are two types of prerequisites.
1770 * Wildcards::                   Using wildcard characters such as `*'.
1771 * Directory Search::            Searching other directories for source files.
1772 * Phony Targets::               Using a target that is not a real file's name.
1773 * Force Targets::               You can use a target without a recipe
1774                                   or prerequisites to mark other targets
1775                                   as phony.
1776 * Empty Targets::               When only the date matters and the
1777                                   files are empty.
1778 * Special Targets::             Targets with special built-in meanings.
1779 * Multiple Targets::            When to make use of several targets in a rule.
1780 * Multiple Rules::              How to use several rules with the same target.
1781 * Static Pattern::              Static pattern rules apply to multiple targets
1782                                   and can vary the prerequisites according to
1783                                   the target name.
1784 * Double-Colon::                How to use a special kind of rule to allow
1785                                   several independent rules for one target.
1786 * Automatic Prerequisites::     How to automatically generate rules giving
1787                                   prerequisites from source files themselves.
1788 @end menu
1790 @ifnottex
1791 @node Rule Example, Rule Syntax, Rules, Rules
1792 @section Rule Example
1794 Here is an example of a rule:
1796 @example
1797 foo.o : foo.c defs.h       # module for twiddling the frobs
1798         cc -c -g foo.c
1799 @end example
1801 Its target is @file{foo.o} and its prerequisites are @file{foo.c} and
1802 @file{defs.h}.  It has one command in the recipe: @samp{cc -c -g foo.c}.
1803 The recipe starts with a tab to identify it as a recipe.
1805 This rule says two things:
1807 @itemize @bullet
1808 @item
1809 How to decide whether @file{foo.o} is out of date: it is out of date
1810 if it does not exist, or if either @file{foo.c} or @file{defs.h} is
1811 more recent than it.
1813 @item
1814 How to update the file @file{foo.o}: by running @code{cc} as stated.
1815 The recipe does not explicitly mention @file{defs.h}, but we presume
1816 that @file{foo.c} includes it, and that that is why @file{defs.h} was
1817 added to the prerequisites.
1818 @end itemize
1819 @end ifnottex
1821 @node Rule Syntax, Prerequisite Types, Rule Example, Rules
1822 @section Rule Syntax
1824 @cindex rule syntax
1825 @cindex syntax of rules
1826 In general, a rule looks like this:
1828 @example
1829 @var{targets} : @var{prerequisites}
1830         @var{recipe}
1831         @dots{}
1832 @end example
1834 @noindent
1835 or like this:
1837 @example
1838 @var{targets} : @var{prerequisites} ; @var{recipe}
1839         @var{recipe}
1840         @dots{}
1841 @end example
1843 @cindex targets
1844 @cindex rule targets
1845 The @var{targets} are file names, separated by spaces.  Wildcard
1846 characters may be used (@pxref{Wildcards, ,Using Wildcard Characters
1847 in File Names}) and a name of the form @file{@var{a}(@var{m})}
1848 represents member @var{m} in archive file @var{a}
1849 (@pxref{Archive Members, ,Archive Members as Targets}).
1850 Usually there is only one
1851 target per rule, but occasionally there is a reason to have more
1852 (@pxref{Multiple Targets, , Multiple Targets in a Rule}).@refill
1854 @cindex recipes
1855 @cindex tab character (in commands)
1856 The @var{recipe} lines start with a tab character (or the first
1857 character in the value of the @code{.RECIPEPREFIX} variable;
1858 @pxref{Special Variables}).  The first recipe line may appear on the line
1859 after the prerequisites, with a tab character, or may appear on the
1860 same line, with a semicolon.  Either way, the effect is the same.
1861 There are other differences in the syntax of recipes.
1862 @xref{Recipes, ,Writing Recipes in Rules}.
1864 @cindex dollar sign (@code{$}), in rules
1865 @cindex @code{$}, in rules
1866 @cindex rules, and @code{$}
1867 Because dollar signs are used to start @code{make} variable
1868 references, if you really want a dollar sign in a target or
1869 prerequisite you must write two of them, @samp{$$} (@pxref{Using
1870 Variables, ,How to Use Variables}).  If you have enabled secondary
1871 expansion (@pxref{Secondary Expansion}) and you want a literal dollar
1872 sign in the prerequisites list, you must actually write @emph{four}
1873 dollar signs (@samp{$$$$}).
1875 You may split a long line by inserting a backslash followed by a
1876 newline, but this is not required, as @code{make} places no limit on
1877 the length of a line in a makefile.
1879 A rule tells @code{make} two things: when the targets are out of date,
1880 and how to update them when necessary.
1882 @cindex prerequisites
1883 @cindex rule prerequisites
1884 The criterion for being out of date is specified in terms of the
1885 @var{prerequisites}, which consist of file names separated by spaces.
1886 (Wildcards and archive members (@pxref{Archives}) are allowed here too.)
1887 A target is out of date if it does not exist or if it is older than any
1888 of the prerequisites (by comparison of last-modification times).  The
1889 idea is that the contents of the target file are computed based on
1890 information in the prerequisites, so if any of the prerequisites changes,
1891 the contents of the existing target file are no longer necessarily
1892 valid.
1894 How to update is specified by a @var{recipe}.  This is one or more
1895 lines to be executed by the shell (normally @samp{sh}), but with some
1896 extra features (@pxref{Recipes, ,Writing Recipes in Rules}).
1898 @node Prerequisite Types, Wildcards, Rule Syntax, Rules
1899 @comment  node-name,  next,  previous,  up
1900 @section Types of Prerequisites
1901 @cindex prerequisite types
1902 @cindex types of prerequisites
1904 @cindex prerequisites, normal
1905 @cindex normal prerequisites
1906 @cindex prerequisites, order-only
1907 @cindex order-only prerequisites
1908 There are actually two different types of prerequisites understood by
1909 GNU @code{make}: normal prerequisites such as described in the
1910 previous section, and @dfn{order-only} prerequisites.  A normal
1911 prerequisite makes two statements: first, it imposes an order in which
1912 recipes will be invoked: the recipes for all prerequisites of a target
1913 will be completed before the recipe for the target is run.  Second, it
1914 imposes a dependency relationship: if any prerequisite is newer than
1915 the target, then the target is considered out-of-date and must be
1916 rebuilt.
1918 Normally, this is exactly what you want: if a target's prerequisite is
1919 updated, then the target should also be updated.
1921 Occasionally, however, you have a situation where you want to impose a
1922 specific ordering on the rules to be invoked @emph{without} forcing
1923 the target to be updated if one of those rules is executed.  In that
1924 case, you want to define @dfn{order-only} prerequisites.  Order-only
1925 prerequisites can be specified by placing a pipe symbol (@code{|})
1926 in the prerequisites list: any prerequisites to the left of the pipe
1927 symbol are normal; any prerequisites to the right are order-only:
1929 @example
1930 @var{targets} : @var{normal-prerequisites} | @var{order-only-prerequisites}
1931 @end example
1933 The normal prerequisites section may of course be empty.  Also, you
1934 may still declare multiple lines of prerequisites for the same target:
1935 they are appended appropriately (normal prerequisites are appended to
1936 the list of normal prerequisites; order-only prerequisites are
1937 appended to the list of order-only prerequisites).  Note that if you
1938 declare the same file to be both a normal and an order-only
1939 prerequisite, the normal prerequisite takes precedence (since they
1940 have a strict superset of the behavior of an order-only prerequisite).
1942 Consider an example where your targets are to be placed in a separate
1943 directory, and that directory might not exist before @code{make} is
1944 run.  In this situation, you want the directory to be created before
1945 any targets are placed into it but, because the timestamps on
1946 directories change whenever a file is added, removed, or renamed, we
1947 certainly don't want to rebuild all the targets whenever the
1948 directory's timestamp changes.  One way to manage this is with
1949 order-only prerequisites: make the directory an order-only
1950 prerequisite on all the targets:
1952 @example
1953 OBJDIR := objdir
1954 OBJS := $(addprefix $(OBJDIR)/,foo.o bar.o baz.o)
1956 $(OBJDIR)/%.o : %.c
1957         $(COMPILE.c) $(OUTPUT_OPTION) $<
1959 all: $(OBJS)
1961 $(OBJS): | $(OBJDIR)
1963 $(OBJDIR):
1964         mkdir $(OBJDIR)
1965 @end example
1967 Now the rule to create the @file{objdir} directory will be run, if
1968 needed, before any @samp{.o} is built, but no @samp{.o} will be built
1969 because the @file{objdir} directory timestamp changed.
1971 @node Wildcards, Directory Search, Prerequisite Types, Rules
1972 @section Using Wildcard Characters in File Names
1973 @cindex wildcard
1974 @cindex file name with wildcards
1975 @cindex globbing (wildcards)
1977 @cindex @code{*} (wildcard character)
1978 @cindex @code{?} (wildcard character)
1979 @cindex @code{[@dots{}]} (wildcard characters)
1980 A single file name can specify many files using @dfn{wildcard characters}.
1981 The wildcard characters in @code{make} are @samp{*}, @samp{?} and
1982 @samp{[@dots{}]}, the same as in the Bourne shell.  For example, @file{*.c}
1983 specifies a list of all the files (in the working directory) whose names
1984 end in @samp{.c}.@refill
1986 @cindex @code{~} (tilde)
1987 @cindex tilde (@code{~})
1988 @cindex home directory
1989 The character @samp{~} at the beginning of a file name also has special
1990 significance.  If alone, or followed by a slash, it represents your home
1991 directory.  For example @file{~/bin} expands to @file{/home/you/bin}.
1992 If the @samp{~} is followed by a word, the string represents the home
1993 directory of the user named by that word.  For example @file{~john/bin}
1994 expands to @file{/home/john/bin}.  On systems which don't have a home
1995 directory for each user (such as MS-DOS or MS-Windows), this
1996 functionality can be simulated by setting the environment variable
1997 @var{HOME}.@refill
1999 Wildcard expansion is performed by @code{make} automatically in
2000 targets and in prerequisites.  In recipes, the shell is responsible
2001 for wildcard expansion.  In other contexts, wildcard expansion happens
2002 only if you request it explicitly with the @code{wildcard} function.
2004 The special significance of a wildcard character can be turned off by
2005 preceding it with a backslash.  Thus, @file{foo\*bar} would refer to a
2006 specific file whose name consists of @samp{foo}, an asterisk, and
2007 @samp{bar}.@refill
2009 @menu
2010 * Wildcard Examples::           Several examples.
2011 * Wildcard Pitfall::            Problems to avoid.
2012 * Wildcard Function::           How to cause wildcard expansion where
2013                                   it does not normally take place.
2014 @end menu
2016 @node Wildcard Examples, Wildcard Pitfall, Wildcards, Wildcards
2017 @subsection Wildcard Examples
2019 Wildcards can be used in the recipe of a rule, where they are expanded
2020 by the shell.  For example, here is a rule to delete all the object files:
2022 @example
2023 @group
2024 clean:
2025         rm -f *.o
2026 @end group
2027 @end example
2028 @cindex @code{rm} (shell command)
2030 Wildcards are also useful in the prerequisites of a rule.  With the
2031 following rule in the makefile, @samp{make print} will print all the
2032 @samp{.c} files that have changed since the last time you printed them:
2034 @example
2035 print: *.c
2036         lpr -p $?
2037         touch print
2038 @end example
2040 @cindex @code{print} target
2041 @cindex @code{lpr} (shell command)
2042 @cindex @code{touch} (shell command)
2043 @noindent
2044 This rule uses @file{print} as an empty target file; see @ref{Empty
2045 Targets, ,Empty Target Files to Record Events}.  (The automatic variable
2046 @samp{$?} is used to print only those files that have changed; see
2047 @ref{Automatic Variables}.)@refill
2049 Wildcard expansion does not happen when you define a variable.  Thus, if
2050 you write this:
2052 @example
2053 objects = *.o
2054 @end example
2056 @noindent
2057 then the value of the variable @code{objects} is the actual string
2058 @samp{*.o}.  However, if you use the value of @code{objects} in a
2059 target or prerequisite, wildcard expansion will take place there.  If
2060 you use the value of @code{objects} in a recipe, the shell may perform
2061 wildcard expansion when the recipe runs.  To set @code{objects} to the
2062 expansion, instead use:
2064 @example
2065 objects := $(wildcard *.o)
2066 @end example
2068 @noindent
2069 @xref{Wildcard Function}.
2071 @node Wildcard Pitfall, Wildcard Function, Wildcard Examples, Wildcards
2072 @subsection Pitfalls of Using Wildcards
2073 @cindex wildcard pitfalls
2074 @cindex pitfalls of wildcards
2075 @cindex mistakes with wildcards
2076 @cindex errors with wildcards
2077 @cindex problems with wildcards
2079 Now here is an example of a naive way of using wildcard expansion, that
2080 does not do what you would intend.  Suppose you would like to say that the
2081 executable file @file{foo} is made from all the object files in the
2082 directory, and you write this:
2084 @example
2085 objects = *.o
2087 foo : $(objects)
2088         cc -o foo $(CFLAGS) $(objects)
2089 @end example
2091 @noindent
2092 The value of @code{objects} is the actual string @samp{*.o}.  Wildcard
2093 expansion happens in the rule for @file{foo}, so that each @emph{existing}
2094 @samp{.o} file becomes a prerequisite of @file{foo} and will be recompiled if
2095 necessary.
2097 But what if you delete all the @samp{.o} files?  When a wildcard matches
2098 no files, it is left as it is, so then @file{foo} will depend on the
2099 oddly-named file @file{*.o}.  Since no such file is likely to exist,
2100 @code{make} will give you an error saying it cannot figure out how to
2101 make @file{*.o}.  This is not what you want!
2103 Actually it is possible to obtain the desired result with wildcard
2104 expansion, but you need more sophisticated techniques, including the
2105 @code{wildcard} function and string substitution.
2106 @ifnottex
2107 @xref{Wildcard Function, ,The Function @code{wildcard}}.
2108 @end ifnottex
2109 @iftex
2110 These are described in the following section.
2111 @end iftex
2113 @cindex wildcards and MS-DOS/MS-Windows backslashes
2114 @cindex backslashes in pathnames and wildcard expansion
2116 Microsoft operating systems (MS-DOS and MS-Windows) use backslashes to
2117 separate directories in pathnames, like so:
2119 @example
2120   c:\foo\bar\baz.c
2121 @end example
2123 This is equivalent to the Unix-style @file{c:/foo/bar/baz.c} (the
2124 @file{c:} part is the so-called drive letter).  When @code{make} runs on
2125 these systems, it supports backslashes as well as the Unix-style forward
2126 slashes in pathnames.  However, this support does @emph{not} include the
2127 wildcard expansion, where backslash is a quote character.  Therefore,
2128 you @emph{must} use Unix-style slashes in these cases.
2131 @node Wildcard Function,  , Wildcard Pitfall, Wildcards
2132 @subsection The Function @code{wildcard}
2133 @findex wildcard
2135 Wildcard expansion happens automatically in rules.  But wildcard expansion
2136 does not normally take place when a variable is set, or inside the
2137 arguments of a function.  If you want to do wildcard expansion in such
2138 places, you need to use the @code{wildcard} function, like this:
2140 @example
2141 $(wildcard @var{pattern}@dots{})
2142 @end example
2144 @noindent
2145 This string, used anywhere in a makefile, is replaced by a
2146 space-separated list of names of existing files that match one of the
2147 given file name patterns.  If no existing file name matches a pattern,
2148 then that pattern is omitted from the output of the @code{wildcard}
2149 function.  Note that this is different from how unmatched wildcards
2150 behave in rules, where they are used verbatim rather than ignored
2151 (@pxref{Wildcard Pitfall}).
2153 One use of the @code{wildcard} function is to get a list of all the C source
2154 files in a directory, like this:
2156 @example
2157 $(wildcard *.c)
2158 @end example
2160 We can change the list of C source files into a list of object files by
2161 replacing the @samp{.c} suffix with @samp{.o} in the result, like this:
2163 @example
2164 $(patsubst %.c,%.o,$(wildcard *.c))
2165 @end example
2167 @noindent
2168 (Here we have used another function, @code{patsubst}.
2169 @xref{Text Functions, ,Functions for String Substitution and Analysis}.)@refill
2171 Thus, a makefile to compile all C source files in the directory and then
2172 link them together could be written as follows:
2174 @example
2175 objects := $(patsubst %.c,%.o,$(wildcard *.c))
2177 foo : $(objects)
2178         cc -o foo $(objects)
2179 @end example
2181 @noindent
2182 (This takes advantage of the implicit rule for compiling C programs, so
2183 there is no need to write explicit rules for compiling the files.
2184 @xref{Flavors, ,The Two Flavors of Variables}, for an explanation of
2185 @samp{:=}, which is a variant of @samp{=}.)
2187 @node Directory Search, Phony Targets, Wildcards, Rules
2188 @section Searching Directories for Prerequisites
2189 @vindex VPATH
2190 @findex vpath
2191 @cindex vpath
2192 @cindex search path for prerequisites (@code{VPATH})
2193 @cindex directory search (@code{VPATH})
2195 For large systems, it is often desirable to put sources in a separate
2196 directory from the binaries.  The @dfn{directory search} features of
2197 @code{make} facilitate this by searching several directories
2198 automatically to find a prerequisite.  When you redistribute the files
2199 among directories, you do not need to change the individual rules,
2200 just the search paths.
2202 @menu
2203 * General Search::              Specifying a search path that applies
2204                                   to every prerequisite.
2205 * Selective Search::            Specifying a search path
2206                                   for a specified class of names.
2207 * Search Algorithm::            When and how search paths are applied.
2208 * Recipes/Search::              How to write recipes that work together
2209                                   with search paths.
2210 * Implicit/Search::             How search paths affect implicit rules.
2211 * Libraries/Search::            Directory search for link libraries.
2212 @end menu
2214 @node General Search, Selective Search, Directory Search, Directory Search
2215 @subsection @code{VPATH}: Search Path for All Prerequisites
2216 @vindex VPATH
2218 The value of the @code{make} variable @code{VPATH} specifies a list of
2219 directories that @code{make} should search.  Most often, the
2220 directories are expected to contain prerequisite files that are not in the
2221 current directory; however, @code{make} uses @code{VPATH} as a search
2222 list for both prerequisites and targets of rules.
2224 Thus, if a file that is listed as a target or prerequisite does not exist
2225 in the current directory, @code{make} searches the directories listed in
2226 @code{VPATH} for a file with that name.  If a file is found in one of
2227 them, that file may become the prerequisite (see below).  Rules may then
2228 specify the names of files in the prerequisite list as if they all
2229 existed in the current directory.  @xref{Recipes/Search, ,Writing Recipes with Directory Search}.
2231 In the @code{VPATH} variable, directory names are separated by colons or
2232 blanks.  The order in which directories are listed is the order followed
2233 by @code{make} in its search.  (On MS-DOS and MS-Windows, semi-colons
2234 are used as separators of directory names in @code{VPATH}, since the
2235 colon can be used in the pathname itself, after the drive letter.)
2237 For example,
2239 @example
2240 VPATH = src:../headers
2241 @end example
2243 @noindent
2244 specifies a path containing two directories, @file{src} and
2245 @file{../headers}, which @code{make} searches in that order.
2247 With this value of @code{VPATH}, the following rule,
2249 @example
2250 foo.o : foo.c
2251 @end example
2253 @noindent
2254 is interpreted as if it were written like this:
2256 @example
2257 foo.o : src/foo.c
2258 @end example
2260 @noindent
2261 assuming the file @file{foo.c} does not exist in the current directory but
2262 is found in the directory @file{src}.
2264 @node Selective Search, Search Algorithm, General Search, Directory Search
2265 @subsection The @code{vpath} Directive
2266 @findex vpath
2268 Similar to the @code{VPATH} variable, but more selective, is the
2269 @code{vpath} directive (note lower case), which allows you to specify a
2270 search path for a particular class of file names: those that match a
2271 particular pattern.  Thus you can supply certain search directories for
2272 one class of file names and other directories (or none) for other file
2273 names.
2275 There are three forms of the @code{vpath} directive:
2277 @table @code
2278 @item vpath @var{pattern} @var{directories}
2279 Specify the search path @var{directories} for file names that match
2280 @var{pattern}.
2282 The search path, @var{directories}, is a list of directories to be
2283 searched, separated by colons (semi-colons on MS-DOS and MS-Windows) or
2284 blanks, just like the search path used in the @code{VPATH} variable.
2286 @item vpath @var{pattern}
2287 Clear out the search path associated with @var{pattern}.
2289 @c Extra blank line makes sure this gets two lines.
2290 @item vpath
2292 Clear all search paths previously specified with @code{vpath} directives.
2293 @end table
2295 A @code{vpath} pattern is a string containing a @samp{%} character.  The
2296 string must match the file name of a prerequisite that is being searched
2297 for, the @samp{%} character matching any sequence of zero or more
2298 characters (as in pattern rules; @pxref{Pattern Rules, ,Defining and
2299 Redefining Pattern Rules}).  For example, @code{%.h} matches files that
2300 end in @code{.h}.  (If there is no @samp{%}, the pattern must match the
2301 prerequisite exactly, which is not useful very often.)
2303 @cindex @code{%}, quoting in @code{vpath}
2304 @cindex @code{%}, quoting with @code{\} (backslash)
2305 @cindex @code{\} (backslash), to quote @code{%}
2306 @cindex backslash (@code{\}), to quote @code{%}
2307 @cindex quoting @code{%}, in @code{vpath}
2308 @samp{%} characters in a @code{vpath} directive's pattern can be quoted
2309 with preceding backslashes (@samp{\}).  Backslashes that would otherwise
2310 quote @samp{%} characters can be quoted with more backslashes.
2311 Backslashes that quote @samp{%} characters or other backslashes are
2312 removed from the pattern before it is compared to file names.  Backslashes
2313 that are not in danger of quoting @samp{%} characters go unmolested.@refill
2315 When a prerequisite fails to exist in the current directory, if the
2316 @var{pattern} in a @code{vpath} directive matches the name of the
2317 prerequisite file, then the @var{directories} in that directive are searched
2318 just like (and before) the directories in the @code{VPATH} variable.
2320 For example,
2322 @example
2323 vpath %.h ../headers
2324 @end example
2326 @noindent
2327 tells @code{make} to look for any prerequisite whose name ends in @file{.h}
2328 in the directory @file{../headers} if the file is not found in the current
2329 directory.
2331 If several @code{vpath} patterns match the prerequisite file's name, then
2332 @code{make} processes each matching @code{vpath} directive one by one,
2333 searching all the directories mentioned in each directive.  @code{make}
2334 handles multiple @code{vpath} directives in the order in which they
2335 appear in the makefile; multiple directives with the same pattern are
2336 independent of each other.
2338 @need 750
2339 Thus,
2341 @example
2342 @group
2343 vpath %.c foo
2344 vpath %   blish
2345 vpath %.c bar
2346 @end group
2347 @end example
2349 @noindent
2350 will look for a file ending in @samp{.c} in @file{foo}, then
2351 @file{blish}, then @file{bar}, while
2353 @example
2354 @group
2355 vpath %.c foo:bar
2356 vpath %   blish
2357 @end group
2358 @end example
2360 @noindent
2361 will look for a file ending in @samp{.c} in @file{foo}, then
2362 @file{bar}, then @file{blish}.
2364 @node Search Algorithm, Recipes/Search, Selective Search, Directory Search
2365 @subsection How Directory Searches are Performed
2366 @cindex algorithm for directory search
2367 @cindex directory search algorithm
2369 When a prerequisite is found through directory search, regardless of type
2370 (general or selective), the pathname located may not be the one that
2371 @code{make} actually provides you in the prerequisite list.  Sometimes
2372 the path discovered through directory search is thrown away.
2374 The algorithm @code{make} uses to decide whether to keep or abandon a
2375 path found via directory search is as follows:
2377 @enumerate
2378 @item
2379 If a target file does not exist at the path specified in the makefile,
2380 directory search is performed.
2382 @item
2383 If the directory search is successful, that path is kept and this file
2384 is tentatively stored as the target.
2386 @item
2387 All prerequisites of this target are examined using this same method.
2389 @item
2390 After processing the prerequisites, the target may or may not need to be
2391 rebuilt:
2393 @enumerate a
2394 @item
2395 If the target does @emph{not} need to be rebuilt, the path to the file
2396 found during directory search is used for any prerequisite lists which
2397 contain this target.  In short, if @code{make} doesn't need to rebuild
2398 the target then you use the path found via directory search.
2400 @item
2401 If the target @emph{does} need to be rebuilt (is out-of-date), the
2402 pathname found during directory search is @emph{thrown away}, and the
2403 target is rebuilt using the file name specified in the makefile.  In
2404 short, if @code{make} must rebuild, then the target is rebuilt locally,
2405 not in the directory found via directory search.
2406 @end enumerate
2407 @end enumerate
2409 This algorithm may seem complex, but in practice it is quite often
2410 exactly what you want.
2412 @cindex traditional directory search (GPATH)
2413 @cindex directory search, traditional (GPATH)
2414 Other versions of @code{make} use a simpler algorithm: if the file does
2415 not exist, and it is found via directory search, then that pathname is
2416 always used whether or not the target needs to be built.  Thus, if the
2417 target is rebuilt it is created at the pathname discovered during
2418 directory search.
2420 @vindex GPATH
2421 If, in fact, this is the behavior you want for some or all of your
2422 directories, you can use the @code{GPATH} variable to indicate this to
2423 @code{make}.
2425 @code{GPATH} has the same syntax and format as @code{VPATH} (that is, a
2426 space- or colon-delimited list of pathnames).  If an out-of-date target
2427 is found by directory search in a directory that also appears in
2428 @code{GPATH}, then that pathname is not thrown away.  The target is
2429 rebuilt using the expanded path.
2431 @node Recipes/Search, Implicit/Search, Search Algorithm, Directory Search
2432 @subsection Writing Recipes with Directory Search
2433 @cindex recipes, and directory search
2434 @cindex directory search (@code{VPATH}), and recipes
2436 When a prerequisite is found in another directory through directory search,
2437 this cannot change the recipe of the rule; they will execute as written.
2438 Therefore, you must write the recipe with care so that it will look for
2439 the prerequisite in the directory where @code{make} finds it.
2441 This is done with the @dfn{automatic variables} such as @samp{$^}
2442 (@pxref{Automatic Variables}).
2443 For instance, the value of @samp{$^} is a
2444 list of all the prerequisites of the rule, including the names of
2445 the directories in which they were found, and the value of
2446 @samp{$@@} is the target.  Thus:@refill
2448 @example
2449 foo.o : foo.c
2450         cc -c $(CFLAGS) $^ -o $@@
2451 @end example
2453 @noindent
2454 (The variable @code{CFLAGS} exists so you can specify flags for C
2455 compilation by implicit rules; we use it here for consistency so it will
2456 affect all C compilations uniformly;
2457 @pxref{Implicit Variables, ,Variables Used by Implicit Rules}.)
2459 Often the prerequisites include header files as well, which you do not
2460 want to mention in the recipe.  The automatic variable @samp{$<} is
2461 just the first prerequisite:
2463 @example
2464 VPATH = src:../headers
2465 foo.o : foo.c defs.h hack.h
2466         cc -c $(CFLAGS) $< -o $@@
2467 @end example
2469 @node Implicit/Search, Libraries/Search, Recipes/Search, Directory Search
2470 @subsection Directory Search and Implicit Rules
2471 @cindex @code{VPATH}, and implicit rules
2472 @cindex directory search (@code{VPATH}), and implicit rules
2473 @cindex search path for prerequisites (@code{VPATH}), and implicit rules
2474 @cindex implicit rule, and directory search
2475 @cindex implicit rule, and @code{VPATH}
2476 @cindex rule, implicit, and directory search
2477 @cindex rule, implicit, and @code{VPATH}
2479 The search through the directories specified in @code{VPATH} or with
2480 @code{vpath} also happens during consideration of implicit rules
2481 (@pxref{Implicit Rules, ,Using Implicit Rules}).
2483 For example, when a file @file{foo.o} has no explicit rule, @code{make}
2484 considers implicit rules, such as the built-in rule to compile
2485 @file{foo.c} if that file exists.  If such a file is lacking in the
2486 current directory, the appropriate directories are searched for it.  If
2487 @file{foo.c} exists (or is mentioned in the makefile) in any of the
2488 directories, the implicit rule for C compilation is applied.
2490 The recipes of implicit rules normally use automatic variables as a
2491 matter of necessity; consequently they will use the file names found by
2492 directory search with no extra effort.
2494 @node Libraries/Search,  , Implicit/Search, Directory Search
2495 @subsection Directory Search for Link Libraries
2496 @cindex link libraries, and directory search
2497 @cindex libraries for linking, directory search
2498 @cindex directory search (@code{VPATH}), and link libraries
2499 @cindex @code{VPATH}, and link libraries
2500 @cindex search path for prerequisites (@code{VPATH}), and link libraries
2501 @cindex @code{-l} (library search)
2502 @cindex link libraries, patterns matching
2503 @cindex @code{.LIBPATTERNS}, and link libraries
2504 @vindex .LIBPATTERNS
2506 Directory search applies in a special way to libraries used with the
2507 linker.  This special feature comes into play when you write a prerequisite
2508 whose name is of the form @samp{-l@var{name}}.  (You can tell something
2509 strange is going on here because the prerequisite is normally the name of a
2510 file, and the @emph{file name} of a library generally looks like
2511 @file{lib@var{name}.a}, not like @samp{-l@var{name}}.)@refill
2513 When a prerequisite's name has the form @samp{-l@var{name}}, @code{make}
2514 handles it specially by searching for the file @file{lib@var{name}.so},
2515 and, if it is not found, for the file @file{lib@var{name}.a} in the current
2516 directory, in directories specified by matching @code{vpath}
2517 search paths and the @code{VPATH} search path, and then in the
2518 directories @file{/lib}, @file{/usr/lib}, and @file{@var{prefix}/lib}
2519 (normally @file{/usr/local/lib}, but MS-DOS/MS-Windows versions of
2520 @code{make} behave as if @var{prefix} is defined to be the root of the
2521 DJGPP installation tree).
2523 For example, if there is a @file{/usr/lib/libcurses.a} library on your
2524 system (and no @file{/usr/lib/libcurses.so} file), then
2526 @example
2527 @group
2528 foo : foo.c -lcurses
2529         cc $^ -o $@@
2530 @end group
2531 @end example
2533 @noindent
2534 would cause the command @samp{cc foo.c /usr/lib/libcurses.a -o foo} to
2535 be executed when @file{foo} is older than @file{foo.c} or than
2536 @file{/usr/lib/libcurses.a}.@refill
2538 Although the default set of files to be searched for is
2539 @file{lib@var{name}.so} and @file{lib@var{name}.a}, this is customizable
2540 via the @code{.LIBPATTERNS} variable.  Each word in the value of this
2541 variable is a pattern string.  When a prerequisite like
2542 @samp{-l@var{name}} is seen, @code{make} will replace the percent in
2543 each pattern in the list with @var{name} and perform the above directory
2544 searches using each library file name.
2546 The default value for @code{.LIBPATTERNS} is @samp{lib%.so lib%.a},
2547 which provides the default behavior described above.
2549 You can turn off link library expansion completely by setting this
2550 variable to an empty value.
2552 @node Phony Targets, Force Targets, Directory Search, Rules
2553 @section Phony Targets
2554 @cindex phony targets
2555 @cindex targets, phony
2556 @cindex targets without a file
2558 A phony target is one that is not really the name of a file; rather it
2559 is just a name for a recipe to be executed when you make an explicit
2560 request.  There are two reasons to use a phony target: to avoid a
2561 conflict with a file of the same name, and to improve performance.
2563 If you write a rule whose recipe will not create the target file, the
2564 recipe will be executed every time the target comes up for remaking.
2565 Here is an example:
2567 @example
2568 @group
2569 clean:
2570         rm *.o temp
2571 @end group
2572 @end example
2574 @noindent
2575 Because the @code{rm} command does not create a file named @file{clean},
2576 probably no such file will ever exist.  Therefore, the @code{rm} command
2577 will be executed every time you say @samp{make clean}.
2578 @cindex @code{rm} (shell command)
2580 @findex .PHONY
2581 The phony target will cease to work if anything ever does create a file
2582 named @file{clean} in this directory.  Since it has no prerequisites, the
2583 file @file{clean} would inevitably be considered up to date, and its
2584 recipe would not be executed.  To avoid this problem, you can explicitly
2585 declare the target to be phony, using the special target @code{.PHONY}
2586 (@pxref{Special Targets, ,Special Built-in Target Names}) as follows:
2588 @example
2589 .PHONY : clean
2590 @end example
2592 @noindent
2593 Once this is done, @samp{make clean} will run the recipe regardless of
2594 whether there is a file named @file{clean}.
2596 Since it knows that phony targets do not name actual files that could be
2597 remade from other files, @code{make} skips the implicit rule search for
2598 phony targets (@pxref{Implicit Rules}).  This is why declaring a target
2599 phony is good for performance, even if you are not worried about the
2600 actual file existing.
2602 Thus, you first write the line that states that @code{clean} is a
2603 phony target, then you write the rule, like this:
2605 @example
2606 @group
2607 .PHONY: clean
2608 clean:
2609         rm *.o temp
2610 @end group
2611 @end example
2613 Another example of the usefulness of phony targets is in conjunction
2614 with recursive invocations of @code{make} (for more information, see
2615 @ref{Recursion, ,Recursive Use of @code{make}}).  In this case the
2616 makefile will often contain a variable which lists a number of
2617 sub-directories to be built.  One way to handle this is with one rule
2618 whose recipe is a shell loop over the sub-directories, like this:
2620 @example
2621 @group
2622 SUBDIRS = foo bar baz
2624 subdirs:
2625         for dir in $(SUBDIRS); do \
2626           $(MAKE) -C $$dir; \
2627         done
2628 @end group
2629 @end example
2631 There are problems with this method, however.  First, any error
2632 detected in a sub-make is ignored by this rule, so it will continue
2633 to build the rest of the directories even when one fails.  This can be
2634 overcome by adding shell commands to note the error and exit, but then
2635 it will do so even if @code{make} is invoked with the @code{-k}
2636 option, which is unfortunate.  Second, and perhaps more importantly,
2637 you cannot take advantage of @code{make}'s ability to build targets in
2638 parallel (@pxref{Parallel, ,Parallel Execution}), since there is only
2639 one rule.
2641 By declaring the sub-directories as phony targets (you must do this as
2642 the sub-directory obviously always exists; otherwise it won't be built)
2643 you can remove these problems:
2645 @example
2646 @group
2647 SUBDIRS = foo bar baz
2649 .PHONY: subdirs $(SUBDIRS)
2651 subdirs: $(SUBDIRS)
2653 $(SUBDIRS):
2654         $(MAKE) -C $@@
2656 foo: baz
2657 @end group
2658 @end example
2660 Here we've also declared that the @file{foo} sub-directory cannot be
2661 built until after the @file{baz} sub-directory is complete; this kind of
2662 relationship declaration is particularly important when attempting
2663 parallel builds.
2665 A phony target should not be a prerequisite of a real target file; if it
2666 is, its recipe will be run every time @code{make} goes to update that
2667 file.  As long as a phony target is never a prerequisite of a real
2668 target, the phony target recipe will be executed only when the phony
2669 target is a specified goal (@pxref{Goals, ,Arguments to Specify the
2670 Goals}).
2672 Phony targets can have prerequisites.  When one directory contains multiple
2673 programs, it is most convenient to describe all of the programs in one
2674 makefile @file{./Makefile}.  Since the target remade by default will be the
2675 first one in the makefile, it is common to make this a phony target named
2676 @samp{all} and give it, as prerequisites, all the individual programs.  For
2677 example:
2679 @example
2680 all : prog1 prog2 prog3
2681 .PHONY : all
2683 prog1 : prog1.o utils.o
2684         cc -o prog1 prog1.o utils.o
2686 prog2 : prog2.o
2687         cc -o prog2 prog2.o
2689 prog3 : prog3.o sort.o utils.o
2690         cc -o prog3 prog3.o sort.o utils.o
2691 @end example
2693 @noindent
2694 Now you can say just @samp{make} to remake all three programs, or
2695 specify as arguments the ones to remake (as in @samp{make prog1
2696 prog3}).  Phoniness is not inherited: the prerequisites of a phony
2697 target are not themselves phony, unless explicitly declared to be so.
2699 When one phony target is a prerequisite of another, it serves as a subroutine
2700 of the other.  For example, here @samp{make cleanall} will delete the
2701 object files, the difference files, and the file @file{program}:
2703 @example
2704 .PHONY: cleanall cleanobj cleandiff
2706 cleanall : cleanobj cleandiff
2707         rm program
2709 cleanobj :
2710         rm *.o
2712 cleandiff :
2713         rm *.diff
2714 @end example
2716 @node Force Targets, Empty Targets, Phony Targets, Rules
2717 @section Rules without Recipes or Prerequisites
2718 @cindex force targets
2719 @cindex targets, force
2720 @cindex @code{FORCE}
2721 @cindex rule, no recipe or prerequisites
2723 If a rule has no prerequisites or recipe, and the target of the rule
2724 is a nonexistent file, then @code{make} imagines this target to have
2725 been updated whenever its rule is run.  This implies that all targets
2726 depending on this one will always have their recipe run.
2728 An example will illustrate this:
2730 @example
2731 @group
2732 clean: FORCE
2733         rm $(objects)
2734 FORCE:
2735 @end group
2736 @end example
2738 Here the target @samp{FORCE} satisfies the special conditions, so the
2739 target @file{clean} that depends on it is forced to run its recipe.
2740 There is nothing special about the name @samp{FORCE}, but that is one
2741 name commonly used this way.
2743 As you can see, using @samp{FORCE} this way has the same results as using
2744 @samp{.PHONY: clean}.
2746 Using @samp{.PHONY} is more explicit and more efficient.  However,
2747 other versions of @code{make} do not support @samp{.PHONY}; thus
2748 @samp{FORCE} appears in many makefiles.  @xref{Phony Targets}.
2750 @node Empty Targets, Special Targets, Force Targets, Rules
2751 @section Empty Target Files to Record Events
2752 @cindex empty targets
2753 @cindex targets, empty
2754 @cindex recording events with empty targets
2756 The @dfn{empty target} is a variant of the phony target; it is used to hold
2757 recipes for an action that you request explicitly from time to time.
2758 Unlike a phony target, this target file can really exist; but the file's
2759 contents do not matter, and usually are empty.
2761 The purpose of the empty target file is to record, with its
2762 last-modification time, when the rule's recipe was last executed.  It
2763 does so because one of the commands in the recipe is a @code{touch}
2764 command to update the target file.
2766 The empty target file should have some prerequisites (otherwise it
2767 doesn't make sense).  When you ask to remake the empty target, the
2768 recipe is executed if any prerequisite is more recent than the target;
2769 in other words, if a prerequisite has changed since the last time you
2770 remade the target.  Here is an example:
2772 @example
2773 print: foo.c bar.c
2774         lpr -p $?
2775         touch print
2776 @end example
2777 @cindex @code{print} target
2778 @cindex @code{lpr} (shell command)
2779 @cindex @code{touch} (shell command)
2781 @noindent
2782 With this rule, @samp{make print} will execute the @code{lpr} command if
2783 either source file has changed since the last @samp{make print}.  The
2784 automatic variable @samp{$?} is used to print only those files that have
2785 changed (@pxref{Automatic Variables}).
2787 @node Special Targets, Multiple Targets, Empty Targets, Rules
2788 @section Special Built-in Target Names
2789 @cindex special targets
2790 @cindex built-in special targets
2791 @cindex targets, built-in special
2793 Certain names have special meanings if they appear as targets.
2795 @table @code
2796 @findex .PHONY
2797 @item .PHONY
2799 The prerequisites of the special target @code{.PHONY} are considered to
2800 be phony targets.  When it is time to consider such a target,
2801 @code{make} will run its recipe unconditionally, regardless of
2802 whether a file with that name exists or what its last-modification
2803 time is.  @xref{Phony Targets, ,Phony Targets}.
2805 @findex .SUFFIXES
2806 @item .SUFFIXES
2808 The prerequisites of the special target @code{.SUFFIXES} are the list
2809 of suffixes to be used in checking for suffix rules.
2810 @xref{Suffix Rules, , Old-Fashioned Suffix Rules}.
2812 @findex .DEFAULT
2813 @item .DEFAULT
2815 The recipe specified for @code{.DEFAULT} is used for any target for
2816 which no rules are found (either explicit rules or implicit rules).
2817 @xref{Last Resort}.  If a @code{.DEFAULT} recipe is specified, every
2818 file mentioned as a prerequisite, but not as a target in a rule, will have
2819 that recipe executed on its behalf.  @xref{Implicit Rule Search,
2820 ,Implicit Rule Search Algorithm}.
2822 @findex .PRECIOUS
2823 @item .PRECIOUS
2824 @cindex precious targets
2825 @cindex preserving with @code{.PRECIOUS}
2827 The targets which @code{.PRECIOUS} depends on are given the following
2828 special treatment: if @code{make} is killed or interrupted during the
2829 execution of their recipes, the target is not deleted.
2830 @xref{Interrupts, ,Interrupting or Killing @code{make}}.  Also, if the
2831 target is an intermediate file, it will not be deleted after it is no
2832 longer needed, as is normally done.  @xref{Chained Rules, ,Chains of
2833 Implicit Rules}.  In this latter respect it overlaps with the
2834 @code{.SECONDARY} special target.
2836 You can also list the target pattern of an implicit rule (such as
2837 @samp{%.o}) as a prerequisite file of the special target @code{.PRECIOUS}
2838 to preserve intermediate files created by rules whose target patterns
2839 match that file's name.
2841 @findex .INTERMEDIATE
2842 @item .INTERMEDIATE
2843 @cindex intermediate targets, explicit
2845 The targets which @code{.INTERMEDIATE} depends on are treated as
2846 intermediate files.  @xref{Chained Rules, ,Chains of Implicit Rules}.
2847 @code{.INTERMEDIATE} with no prerequisites has no effect.
2849 @findex .SECONDARY
2850 @item .SECONDARY
2851 @cindex secondary targets
2852 @cindex preserving with @code{.SECONDARY}
2854 The targets which @code{.SECONDARY} depends on are treated as
2855 intermediate files, except that they are never automatically deleted.
2856 @xref{Chained Rules, ,Chains of Implicit Rules}.
2858 @code{.SECONDARY} with no prerequisites causes all targets to be treated
2859 as secondary (i.e., no target is removed because it is considered
2860 intermediate).
2862 @findex .SECONDEXPANSION
2863 @item .SECONDEXPANSION
2865 If @code{.SECONDEXPANSION} is mentioned as a target anywhere in the
2866 makefile, then all prerequisite lists defined @emph{after} it appears
2867 will be expanded a second time after all makefiles have been read in.
2868 @xref{Secondary Expansion, ,Secondary Expansion}.
2870 @findex .DELETE_ON_ERROR
2871 @item .DELETE_ON_ERROR
2872 @cindex removing targets on failure
2874 If @code{.DELETE_ON_ERROR} is mentioned as a target anywhere in the
2875 makefile, then @code{make} will delete the target of a rule if it has
2876 changed and its recipe exits with a nonzero exit status, just as it
2877 does when it receives a signal.  @xref{Errors, ,Errors in Recipes}.
2879 @findex .IGNORE
2880 @item .IGNORE
2882 If you specify prerequisites for @code{.IGNORE}, then @code{make} will
2883 ignore errors in execution of the recipe for those particular files.
2884 The recipe for @code{.IGNORE} (if any) is ignored.
2886 If mentioned as a target with no prerequisites, @code{.IGNORE} says to
2887 ignore errors in execution of recipes for all files.  This usage of
2888 @samp{.IGNORE} is supported only for historical compatibility.  Since
2889 this affects every recipe in the makefile, it is not very useful; we
2890 recommend you use the more selective ways to ignore errors in specific
2891 recipes.  @xref{Errors, ,Errors in Recipes}.
2893 @findex .LOW_RESOLUTION_TIME
2894 @item .LOW_RESOLUTION_TIME
2896 If you specify prerequisites for @code{.LOW_RESOLUTION_TIME},
2897 @command{make} assumes that these files are created by commands that
2898 generate low resolution time stamps.  The recipe for the
2899 @code{.LOW_RESOLUTION_TIME} target are ignored.
2901 The high resolution file time stamps of many modern file systems
2902 lessen the chance of @command{make} incorrectly concluding that a file
2903 is up to date.  Unfortunately, some hosts do not provide a way to set a
2904 high resolution file time stamp, so commands like @samp{cp -p} that
2905 explicitly set a file's time stamp must discard its sub-second part.
2906 If a file is created by such a command, you should list it as a
2907 prerequisite of @code{.LOW_RESOLUTION_TIME} so that @command{make}
2908 does not mistakenly conclude that the file is out of date.  For
2909 example:
2911 @example
2912 @group
2913 .LOW_RESOLUTION_TIME: dst
2914 dst: src
2915         cp -p src dst
2916 @end group
2917 @end example
2919 Since @samp{cp -p} discards the sub-second part of @file{src}'s time
2920 stamp, @file{dst} is typically slightly older than @file{src} even when
2921 it is up to date.  The @code{.LOW_RESOLUTION_TIME} line causes
2922 @command{make} to consider @file{dst} to be up to date if its time stamp
2923 is at the start of the same second that @file{src}'s time stamp is in.
2925 Due to a limitation of the archive format, archive member time stamps
2926 are always low resolution.  You need not list archive members as
2927 prerequisites of @code{.LOW_RESOLUTION_TIME}, as @command{make} does this
2928 automatically.
2930 @findex .SILENT
2931 @item .SILENT
2933 If you specify prerequisites for @code{.SILENT}, then @code{make} will
2934 not print the recipe used to remake those particular files before
2935 executing them.  The recipe for @code{.SILENT} is ignored.
2937 If mentioned as a target with no prerequisites, @code{.SILENT} says not
2938 to print any recipes before executing them.  This usage of
2939 @samp{.SILENT} is supported only for historical compatibility.  We
2940 recommend you use the more selective ways to silence specific recipes.
2941 @xref{Echoing, ,Recipe Echoing}.  If you want to silence all recipes
2942 for a particular run of @code{make}, use the @samp{-s} or
2943 @w{@samp{--silent}} option (@pxref{Options Summary}).
2945 @findex .EXPORT_ALL_VARIABLES
2946 @item .EXPORT_ALL_VARIABLES
2948 Simply by being mentioned as a target, this tells @code{make} to
2949 export all variables to child processes by default.
2950 @xref{Variables/Recursion, ,Communicating Variables to a
2951 Sub-@code{make}}.
2953 @findex .NOTPARALLEL
2954 @item .NOTPARALLEL
2955 @cindex parallel execution, overriding
2957 If @code{.NOTPARALLEL} is mentioned as a target, then this invocation
2958 of @code{make} will be run serially, even if the @samp{-j} option is
2959 given.  Any recursively invoked @code{make} command will still run
2960 recipes in parallel (unless its makefile also contains this target).
2961 Any prerequisites on this target are ignored.
2963 @findex .ONESHELL
2964 @item .ONESHELL
2965 @cindex recipe execution, single invocation
2967 If @code{.ONESHELL} is mentioned as a target, then when a target is
2968 built all lines of the recipe will be given to a single invocation of
2969 the shell rather than each line being invoked separately
2970 (@pxref{Execution, ,Recipe Execution}).
2972 @findex .POSIX
2973 @item .POSIX
2974 @cindex POSIX-conforming mode, setting
2976 If @code{.POSIX} is mentioned as a target, then the makefile will be
2977 parsed and run in POSIX-conforming mode.  This does @emph{not} mean
2978 that only POSIX-conforming makefiles will be accepted: all advanced
2979 GNU @code{make} features are still available.  Rather, this target
2980 causes @code{make} to behave as required by POSIX in those areas
2981 where @code{make}'s default behavior differs.
2983 In particular, if this target is mentioned then recipes will be
2984 invoked as if the shell had been passed the @code{-e} flag: the first
2985 failing command in a recipe will cause the recipe to fail immediately.
2986 @end table
2988 Any defined implicit rule suffix also counts as a special target if it
2989 appears as a target, and so does the concatenation of two suffixes, such
2990 as @samp{.c.o}.  These targets are suffix rules, an obsolete way of
2991 defining implicit rules (but a way still widely used).  In principle, any
2992 target name could be special in this way if you break it in two and add
2993 both pieces to the suffix list.  In practice, suffixes normally begin with
2994 @samp{.}, so these special target names also begin with @samp{.}.
2995 @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
2997 @node Multiple Targets, Multiple Rules, Special Targets, Rules
2998 @section Multiple Targets in a Rule
2999 @cindex multiple targets
3000 @cindex several targets in a rule
3001 @cindex targets, multiple
3002 @cindex rule, with multiple targets
3004 A rule with multiple targets is equivalent to writing many rules, each with
3005 one target, and all identical aside from that.  The same recipe applies to
3006 all the targets, but its effect may vary because you can substitute the
3007 actual target name into the recipe using @samp{$@@}.  The rule contributes
3008 the same prerequisites to all the targets also.
3010 This is useful in two cases.
3012 @itemize @bullet
3013 @item
3014 You want just prerequisites, no recipe.  For example:
3016 @example
3017 kbd.o command.o files.o: command.h
3018 @end example
3020 @noindent
3021 gives an additional prerequisite to each of the three object files
3022 mentioned.
3024 @item
3025 Similar recipes work for all the targets.  The recipes do not need
3026 to be absolutely identical, since the automatic variable @samp{$@@}
3027 can be used to substitute the particular target to be remade into the
3028 commands (@pxref{Automatic Variables}).  For example:
3030 @example
3031 @group
3032 bigoutput littleoutput : text.g
3033         generate text.g -$(subst output,,$@@) > $@@
3034 @end group
3035 @end example
3036 @findex subst
3038 @noindent
3039 is equivalent to
3041 @example
3042 bigoutput : text.g
3043         generate text.g -big > bigoutput
3044 littleoutput : text.g
3045         generate text.g -little > littleoutput
3046 @end example
3048 @noindent
3049 Here we assume the hypothetical program @code{generate} makes two
3050 types of output, one if given @samp{-big} and one if given
3051 @samp{-little}.
3052 @xref{Text Functions, ,Functions for String Substitution and Analysis},
3053 for an explanation of the @code{subst} function.
3054 @end itemize
3056 Suppose you would like to vary the prerequisites according to the
3057 target, much as the variable @samp{$@@} allows you to vary the recipe.
3058 You cannot do this with multiple targets in an ordinary rule, but you
3059 can do it with a @dfn{static pattern rule}.  @xref{Static Pattern,
3060 ,Static Pattern Rules}.
3062 @node Multiple Rules, Static Pattern, Multiple Targets, Rules
3063 @section Multiple Rules for One Target
3064 @cindex multiple rules for one target
3065 @cindex several rules for one target
3066 @cindex rule, multiple for one target
3067 @cindex target, multiple rules for one
3069 One file can be the target of several rules.  All the prerequisites
3070 mentioned in all the rules are merged into one list of prerequisites for
3071 the target.  If the target is older than any prerequisite from any rule,
3072 the recipe is executed.
3074 There can only be one recipe to be executed for a file.  If more than
3075 one rule gives a recipe for the same file, @code{make} uses the last
3076 one given and prints an error message.  (As a special case, if the
3077 file's name begins with a dot, no error message is printed.  This odd
3078 behavior is only for compatibility with other implementations of
3079 @code{make}@dots{} you should avoid using it).  Occasionally it is
3080 useful to have the same target invoke multiple recipes which are
3081 defined in different parts of your makefile; you can use
3082 @dfn{double-colon rules} (@pxref{Double-Colon}) for this.
3084 An extra rule with just prerequisites can be used to give a few extra
3085 prerequisites to many files at once.  For example, makefiles often
3086 have a variable, such as @code{objects}, containing a list of all the
3087 compiler output files in the system being made.  An easy way to say
3088 that all of them must be recompiled if @file{config.h} changes is to
3089 write the following:
3091 @example
3092 objects = foo.o bar.o
3093 foo.o : defs.h
3094 bar.o : defs.h test.h
3095 $(objects) : config.h
3096 @end example
3098 This could be inserted or taken out without changing the rules that really
3099 specify how to make the object files, making it a convenient form to use if
3100 you wish to add the additional prerequisite intermittently.
3102 Another wrinkle is that the additional prerequisites could be
3103 specified with a variable that you set with a command line argument to
3104 @code{make} (@pxref{Overriding, ,Overriding Variables}).  For example,
3106 @example
3107 @group
3108 extradeps=
3109 $(objects) : $(extradeps)
3110 @end group
3111 @end example
3113 @noindent
3114 means that the command @samp{make extradeps=foo.h} will consider
3115 @file{foo.h} as a prerequisite of each object file, but plain @samp{make}
3116 will not.
3118 If none of the explicit rules for a target has a recipe, then @code{make}
3119 searches for an applicable implicit rule to find one
3120 @pxref{Implicit Rules, ,Using Implicit Rules}).
3122 @node Static Pattern, Double-Colon, Multiple Rules, Rules
3123 @section Static Pattern Rules
3124 @cindex static pattern rule
3125 @cindex rule, static pattern
3126 @cindex pattern rules, static (not implicit)
3127 @cindex varying prerequisites
3128 @cindex prerequisites, varying (static pattern)
3130 @dfn{Static pattern rules} are rules which specify multiple targets and
3131 construct the prerequisite names for each target based on the target name.
3132 They are more general than ordinary rules with multiple targets because the
3133 targets do not have to have identical prerequisites.  Their prerequisites must
3134 be @emph{analogous}, but not necessarily @emph{identical}.
3136 @menu
3137 * Static Usage::                The syntax of static pattern rules.
3138 * Static versus Implicit::      When are they better than implicit rules?
3139 @end menu
3141 @node Static Usage, Static versus Implicit, Static Pattern, Static Pattern
3142 @subsection Syntax of Static Pattern Rules
3143 @cindex static pattern rule, syntax of
3144 @cindex pattern rules, static, syntax of
3146 Here is the syntax of a static pattern rule:
3148 @example
3149 @var{targets} @dots{}: @var{target-pattern}: @var{prereq-patterns} @dots{}
3150         @var{recipe}
3151         @dots{}
3152 @end example
3154 @noindent
3155 The @var{targets} list specifies the targets that the rule applies to.
3156 The targets can contain wildcard characters, just like the targets of
3157 ordinary rules (@pxref{Wildcards, ,Using Wildcard Characters in File
3158 Names}).
3160 @cindex target pattern, static (not implicit)
3161 @cindex stem
3162 The @var{target-pattern} and @var{prereq-patterns} say how to compute the
3163 prerequisites of each target.  Each target is matched against the
3164 @var{target-pattern} to extract a part of the target name, called the
3165 @dfn{stem}.  This stem is substituted into each of the @var{prereq-patterns}
3166 to make the prerequisite names (one from each @var{prereq-pattern}).
3168 Each pattern normally contains the character @samp{%} just once.  When the
3169 @var{target-pattern} matches a target, the @samp{%} can match any part of
3170 the target name; this part is called the @dfn{stem}.  The rest of the
3171 pattern must match exactly.  For example, the target @file{foo.o} matches
3172 the pattern @samp{%.o}, with @samp{foo} as the stem.  The targets
3173 @file{foo.c} and @file{foo.out} do not match that pattern.@refill
3175 @cindex prerequisite pattern, static (not implicit)
3176 The prerequisite names for each target are made by substituting the stem
3177 for the @samp{%} in each prerequisite pattern.  For example, if one
3178 prerequisite pattern is @file{%.c}, then substitution of the stem
3179 @samp{foo} gives the prerequisite name @file{foo.c}.  It is legitimate
3180 to write a prerequisite pattern that does not contain @samp{%}; then this
3181 prerequisite is the same for all targets.
3183 @cindex @code{%}, quoting in static pattern
3184 @cindex @code{%}, quoting with @code{\} (backslash)
3185 @cindex @code{\} (backslash), to quote @code{%}
3186 @cindex backslash (@code{\}), to quote @code{%}
3187 @cindex quoting @code{%}, in static pattern
3188 @samp{%} characters in pattern rules can be quoted with preceding
3189 backslashes (@samp{\}).  Backslashes that would otherwise quote @samp{%}
3190 characters can be quoted with more backslashes.  Backslashes that quote
3191 @samp{%} characters or other backslashes are removed from the pattern
3192 before it is compared to file names or has a stem substituted into it.
3193 Backslashes that are not in danger of quoting @samp{%} characters go
3194 unmolested.  For example, the pattern @file{the\%weird\\%pattern\\} has
3195 @samp{the%weird\} preceding the operative @samp{%} character, and
3196 @samp{pattern\\} following it.  The final two backslashes are left alone
3197 because they cannot affect any @samp{%} character.@refill
3199 Here is an example, which compiles each of @file{foo.o} and @file{bar.o}
3200 from the corresponding @file{.c} file:
3202 @example
3203 @group
3204 objects = foo.o bar.o
3206 all: $(objects)
3208 $(objects): %.o: %.c
3209         $(CC) -c $(CFLAGS) $< -o $@@
3210 @end group
3211 @end example
3213 @noindent
3214 Here @samp{$<} is the automatic variable that holds the name of the
3215 prerequisite and @samp{$@@} is the automatic variable that holds the name
3216 of the target; see @ref{Automatic Variables}.
3218 Each target specified must match the target pattern; a warning is issued
3219 for each target that does not.  If you have a list of files, only some of
3220 which will match the pattern, you can use the @code{filter} function to
3221 remove non-matching file names (@pxref{Text Functions, ,Functions for String Substitution and Analysis}):
3223 @example
3224 files = foo.elc bar.o lose.o
3226 $(filter %.o,$(files)): %.o: %.c
3227         $(CC) -c $(CFLAGS) $< -o $@@
3228 $(filter %.elc,$(files)): %.elc: %.el
3229         emacs -f batch-byte-compile $<
3230 @end example
3232 @noindent
3233 In this example the result of @samp{$(filter %.o,$(files))} is
3234 @file{bar.o lose.o}, and the first static pattern rule causes each of
3235 these object files to be updated by compiling the corresponding C source
3236 file.  The result of @w{@samp{$(filter %.elc,$(files))}} is
3237 @file{foo.elc}, so that file is made from @file{foo.el}.@refill
3239 Another example shows how to use @code{$*} in static pattern rules:
3240 @vindex $*@r{, and static pattern}
3242 @example
3243 @group
3244 bigoutput littleoutput : %output : text.g
3245         generate text.g -$* > $@@
3246 @end group
3247 @end example
3249 @noindent
3250 When the @code{generate} command is run, @code{$*} will expand to the
3251 stem, either @samp{big} or @samp{little}.
3253 @node Static versus Implicit,  , Static Usage, Static Pattern
3254 @subsection Static Pattern Rules versus Implicit Rules
3255 @cindex rule, static pattern versus implicit
3256 @cindex static pattern rule, versus implicit
3258 A static pattern rule has much in common with an implicit rule defined as a
3259 pattern rule (@pxref{Pattern Rules, ,Defining and Redefining Pattern Rules}).
3260 Both have a pattern for the target and patterns for constructing the
3261 names of prerequisites.  The difference is in how @code{make} decides
3262 @emph{when} the rule applies.
3264 An implicit rule @emph{can} apply to any target that matches its pattern,
3265 but it @emph{does} apply only when the target has no recipe otherwise
3266 specified, and only when the prerequisites can be found.  If more than one
3267 implicit rule appears applicable, only one applies; the choice depends on
3268 the order of rules.
3270 By contrast, a static pattern rule applies to the precise list of targets
3271 that you specify in the rule.  It cannot apply to any other target and it
3272 invariably does apply to each of the targets specified.  If two conflicting
3273 rules apply, and both have recipes, that's an error.
3275 The static pattern rule can be better than an implicit rule for these
3276 reasons:
3278 @itemize @bullet
3279 @item
3280 You may wish to override the usual implicit rule for a few
3281 files whose names cannot be categorized syntactically but
3282 can be given in an explicit list.
3284 @item
3285 If you cannot be sure of the precise contents of the directories
3286 you are using, you may not be sure which other irrelevant files
3287 might lead @code{make} to use the wrong implicit rule.  The choice
3288 might depend on the order in which the implicit rule search is done.
3289 With static pattern rules, there is no uncertainty: each rule applies
3290 to precisely the targets specified.
3291 @end itemize
3293 @node Double-Colon, Automatic Prerequisites, Static Pattern, Rules
3294 @section Double-Colon Rules
3295 @cindex double-colon rules
3296 @cindex rule, double-colon (@code{::})
3297 @cindex multiple rules for one target (@code{::})
3298 @cindex @code{::} rules (double-colon)
3300 @dfn{Double-colon} rules are explicit rules written with @samp{::}
3301 instead of @samp{:} after the target names.  They are handled
3302 differently from ordinary rules when the same target appears in more
3303 than one rule.  Pattern rules with double-colons have an entirely
3304 different meaning (@pxref{Match-Anything Rules}).
3306 When a target appears in multiple rules, all the rules must be the same
3307 type: all ordinary, or all double-colon.  If they are double-colon, each
3308 of them is independent of the others.  Each double-colon rule's recipe
3309 is executed if the target is older than any prerequisites of that rule.
3310 If there are no prerequisites for that rule, its recipe is always
3311 executed (even if the target already exists).  This can result in
3312 executing none, any, or all of the double-colon rules.
3314 Double-colon rules with the same target are in fact completely separate
3315 from one another.  Each double-colon rule is processed individually, just
3316 as rules with different targets are processed.
3318 The double-colon rules for a target are executed in the order they appear
3319 in the makefile.  However, the cases where double-colon rules really make
3320 sense are those where the order of executing the recipes would not matter.
3322 Double-colon rules are somewhat obscure and not often very useful; they
3323 provide a mechanism for cases in which the method used to update a target
3324 differs depending on which prerequisite files caused the update, and such
3325 cases are rare.
3327 Each double-colon rule should specify a recipe; if it does not, an
3328 implicit rule will be used if one applies.
3329 @xref{Implicit Rules, ,Using Implicit Rules}.
3331 @node Automatic Prerequisites,  , Double-Colon, Rules
3332 @section Generating Prerequisites Automatically
3333 @cindex prerequisites, automatic generation
3334 @cindex automatic generation of prerequisites
3335 @cindex generating prerequisites automatically
3337 In the makefile for a program, many of the rules you need to write often
3338 say only that some object file depends on some header
3339 file.  For example, if @file{main.c} uses @file{defs.h} via an
3340 @code{#include}, you would write:
3342 @example
3343 main.o: defs.h
3344 @end example
3346 @noindent
3347 You need this rule so that @code{make} knows that it must remake
3348 @file{main.o} whenever @file{defs.h} changes.  You can see that for a
3349 large program you would have to write dozens of such rules in your
3350 makefile.  And, you must always be very careful to update the makefile
3351 every time you add or remove an @code{#include}.
3352 @cindex @code{#include}
3354 @cindex @code{-M} (to compiler)
3355 To avoid this hassle, most modern C compilers can write these rules for
3356 you, by looking at the @code{#include} lines in the source files.
3357 Usually this is done with the @samp{-M} option to the compiler.
3358 For example, the command:
3360 @example
3361 cc -M main.c
3362 @end example
3364 @noindent
3365 generates the output:
3367 @example
3368 main.o : main.c defs.h
3369 @end example
3371 @noindent
3372 Thus you no longer have to write all those rules yourself.
3373 The compiler will do it for you.
3375 Note that such a rule constitutes mentioning @file{main.o} in a
3376 makefile, so it can never be considered an intermediate file by
3377 implicit rule search.  This means that @code{make} won't ever remove
3378 the file after using it; @pxref{Chained Rules, ,Chains of Implicit
3379 Rules}.
3381 @cindex @code{make depend}
3382 With old @code{make} programs, it was traditional practice to use this
3383 compiler feature to generate prerequisites on demand with a command like
3384 @samp{make depend}.  That command would create a file @file{depend}
3385 containing all the automatically-generated prerequisites; then the
3386 makefile could use @code{include} to read them in (@pxref{Include}).
3388 In GNU @code{make}, the feature of remaking makefiles makes this
3389 practice obsolete---you need never tell @code{make} explicitly to
3390 regenerate the prerequisites, because it always regenerates any makefile
3391 that is out of date.  @xref{Remaking Makefiles}.
3393 The practice we recommend for automatic prerequisite generation is to have
3394 one makefile corresponding to each source file.  For each source file
3395 @file{@var{name}.c} there is a makefile @file{@var{name}.d} which lists
3396 what files the object file @file{@var{name}.o} depends on.  That way
3397 only the source files that have changed need to be rescanned to produce
3398 the new prerequisites.
3400 Here is the pattern rule to generate a file of prerequisites (i.e., a makefile)
3401 called @file{@var{name}.d} from a C source file called @file{@var{name}.c}:
3403 @smallexample
3404 @group
3405 %.d: %.c
3406         @@set -e; rm -f $@@; \
3407          $(CC) -M $(CPPFLAGS) $< > $@@.$$$$; \
3408          sed 's,\($*\)\.o[ :]*,\1.o $@@ : ,g' < $@@.$$$$ > $@@; \
3409          rm -f $@@.$$$$
3410 @end group
3411 @end smallexample
3413 @noindent
3414 @xref{Pattern Rules}, for information on defining pattern rules.  The
3415 @samp{-e} flag to the shell causes it to exit immediately if the
3416 @code{$(CC)} command (or any other command) fails (exits with a
3417 nonzero status).
3418 @cindex @code{-e} (shell flag)
3420 @cindex @code{-MM} (to GNU compiler)
3421 With the GNU C compiler, you may wish to use the @samp{-MM} flag instead
3422 of @samp{-M}.  This omits prerequisites on system header files.
3423 @xref{Preprocessor Options, , Options Controlling the Preprocessor,
3424 gcc, Using GNU CC}, for details.
3426 @cindex @code{sed} (shell command)
3427 The purpose of the @code{sed} command is to translate (for example):
3429 @example
3430 main.o : main.c defs.h
3431 @end example
3433 @noindent
3434 into:
3436 @example
3437 main.o main.d : main.c defs.h
3438 @end example
3440 @noindent
3441 @cindex @code{.d}
3442 This makes each @samp{.d} file depend on all the source and header files
3443 that the corresponding @samp{.o} file depends on.  @code{make} then
3444 knows it must regenerate the prerequisites whenever any of the source or
3445 header files changes.
3447 Once you've defined the rule to remake the @samp{.d} files,
3448 you then use the @code{include} directive to read them all in.
3449 @xref{Include}.  For example:
3451 @example
3452 @group
3453 sources = foo.c bar.c
3455 include $(sources:.c=.d)
3456 @end group
3457 @end example
3459 @noindent
3460 (This example uses a substitution variable reference to translate the
3461 list of source files @samp{foo.c bar.c} into a list of prerequisite
3462 makefiles, @samp{foo.d bar.d}.  @xref{Substitution Refs}, for full
3463 information on substitution references.)  Since the @samp{.d} files are
3464 makefiles like any others, @code{make} will remake them as necessary
3465 with no further work from you.  @xref{Remaking Makefiles}.
3467 Note that the @samp{.d} files contain target definitions; you should
3468 be sure to place the @code{include} directive @emph{after} the first,
3469 default goal in your makefiles or run the risk of having a random
3470 object file become the default goal.
3471 @xref{How Make Works}.
3473 @node Recipes, Using Variables, Rules, Top
3474 @chapter Writing Recipes in Rules
3475 @cindex recipes
3476 @cindex recipes, how to write
3477 @cindex writing recipes
3479 The recipe of a rule consists of one or more shell command lines to
3480 be executed, one at a time, in the order they appear.  Typically, the
3481 result of executing these commands is that the target of the rule is
3482 brought up to date.
3484 Users use many different shell programs, but recipes in makefiles are
3485 always interpreted by @file{/bin/sh} unless the makefile specifies
3486 otherwise.  @xref{Execution, ,Recipe Execution}.
3488 @menu
3489 * Recipe Syntax::               Recipe syntax features and pitfalls.
3490 * Echoing::                     How to control when recipes are echoed.
3491 * Execution::                   How recipes are executed.
3492 * Parallel::                    How recipes can be executed in parallel.
3493 * Errors::                      What happens after a recipe execution error.
3494 * Interrupts::                  What happens when a recipe is interrupted.
3495 * Recursion::                   Invoking @code{make} from makefiles.
3496 * Canned Recipes::              Defining canned recipes.
3497 * Empty Recipes::               Defining useful, do-nothing recipes.
3498 @end menu
3500 @node Recipe Syntax, Echoing, Recipes, Recipes
3501 @section Recipe Syntax
3502 @cindex recipe syntax
3503 @cindex syntax of recipe
3505 Makefiles have the unusual property that there are really two distinct
3506 syntaxes in one file.  Most of the makefile uses @code{make} syntax
3507 (@pxref{Makefiles, ,Writing Makefiles}).  However, recipes are meant
3508 to be interpreted by the shell and so they are written using shell
3509 syntax.  The @code{make} program does not try to understand shell
3510 syntax: it performs only a very few specific translations on the
3511 content of the recipe before handing it to the shell.
3513 Each line in the recipe must start with a tab (or the first character
3514 in the value of the @code{.RECIPEPREFIX} variable; @pxref{Special
3515 Variables}), except that the first recipe line may be attached to the
3516 target-and-prerequisites line with a semicolon in between.  @emph{Any}
3517 line in the makefile that begins with a tab and appears in a ``rule
3518 context'' (that is, after a rule has been started until another rule
3519 or variable definition) will be considered part of a recipe for that
3520 rule.  Blank lines and lines of just comments may appear among the
3521 recipe lines; they are ignored.
3523 Some consequences of these rules include:
3525 @itemize @bullet
3526 @item
3527 A blank line that begins with a tab is not blank: it's an empty
3528 recipe (@pxref{Empty Recipes}).
3530 @cindex comments, in recipes
3531 @cindex recipes, comments in
3532 @cindex @code{#} (comments), in recipes
3533 @item
3534 A comment in a recipe is not a @code{make} comment; it will be
3535 passed to the shell as-is.  Whether the shell treats it as a comment
3536 or not depends on your shell.
3538 @item
3539 A variable definition in a ``rule context'' which is indented by a tab
3540 as the first character on the line, will be considered part of a
3541 recipe, not a @code{make} variable definition, and passed to the
3542 shell.
3544 @item
3545 A conditional expression (@code{ifdef}, @code{ifeq},
3546 etc. @pxref{Conditional Syntax, ,Syntax of Conditionals}) in a ``rule
3547 context'' which is indented by a tab as the first character on the
3548 line, will be considered part of a recipe and be passed to the shell.
3550 @end itemize
3552 @menu
3553 * Splitting Recipe Lines::      Breaking long recipe lines for readability.
3554 * Variables in Recipes::        Using @code{make} variables in recipes.
3555 @end menu
3557 @node Splitting Recipe Lines, Variables in Recipes, Recipe Syntax, Recipe Syntax
3558 @subsection Splitting Recipe Lines
3559 @cindex recipes, splitting
3560 @cindex splitting recipes
3561 @cindex recipes, backslash (@code{\}) in
3562 @cindex recipes, quoting newlines in
3563 @cindex backslash (@code{\}), in recipes
3564 @cindex @code{\} (backslash), in recipes
3565 @cindex quoting newline, in recipes
3566 @cindex newline, quoting, in recipes
3568 One of the few ways in which @code{make} does interpret recipes is
3569 checking for a backslash just before the newline.  As in normal
3570 makefile syntax, a single logical recipe line can be split into
3571 multiple physical lines in the makefile by placing a backslash before
3572 each newline.  A sequence of lines like this is considered a single
3573 recipe line, and one instance of the shell will be invoked to run it.
3575 However, in contrast to how they are treated in other places in a
3576 makefile (@pxref{Splitting Lines, , Splitting Long Lines}),
3577 backslash/newline pairs are @emph{not} removed from the recipe.  Both
3578 the backslash and the newline characters are preserved and passed to
3579 the shell.  How the backslash/newline is interpreted depends on your
3580 shell.  If the first character of the next line after the
3581 backslash/newline is the recipe prefix character (a tab by default;
3582 @pxref{Special Variables}), then that character (and only that
3583 character) is removed.  Whitespace is never added to the recipe.
3585 For example, the recipe for the all target in this makefile:
3587 @example
3588 @group
3589 all :
3590         @@echo no\
3591 space
3592         @@echo no\
3593         space
3594         @@echo one \
3595         space
3596         @@echo one\
3597          space
3598 @end group
3599 @end example
3601 @noindent
3602 consists of four separate shell commands where the output is:
3604 @example
3605 @group
3606 nospace
3607 nospace
3608 one space
3609 one space
3610 @end group
3611 @end example
3613 As a more complex example, this makefile:
3615 @example
3616 @group
3617 all : ; @@echo 'hello \
3618         world' ; echo "hello \
3619     world"
3620 @end group
3621 @end example
3623 @noindent
3624 will invoke one shell with a command of:
3626 @example
3627 @group
3628 echo 'hello \
3629 world' ; echo "hello \
3630     world"
3631 @end group
3632 @end example
3634 @noindent
3635 which, according to shell quoting rules, will yield the following output:
3637 @example
3638 @group
3639 hello \
3640 world
3641 hello     world
3642 @end group
3643 @end example
3645 @noindent
3646 Notice how the backslash/newline pair was removed inside the string
3647 quoted with double quotes (@code{"@dots{}"}), but not from the string
3648 quoted with single quotes (@code{'@dots{}'}).  This is the way the
3649 default shell (@file{/bin/sh}) handles backslash/newline pairs.  If
3650 you specify a different shell in your makefiles it may treat them
3651 differently.
3653 Sometimes you want to split a long line inside of single quotes, but
3654 you don't want the backslash/newline to appear in the quoted content.
3655 This is often the case when passing scripts to languages such as Perl,
3656 where extraneous backslashes inside the script can change its meaning
3657 or even be a syntax error.  One simple way of handling this is to
3658 place the quoted string, or even the entire command, into a
3659 @code{make} variable then use the variable in the recipe.  In this
3660 situation the newline quoting rules for makefiles will be used, and
3661 the backslash/newline will be removed.  If we rewrite our example
3662 above using this method:
3664 @example
3665 @group
3666 HELLO = 'hello \
3667 world'
3669 all : ; @@echo $(HELLO)
3670 @end group
3671 @end example
3673 @noindent
3674 we will get output like this:
3676 @example
3677 @group
3678 hello world
3679 @end group
3680 @end example
3682 If you like, you can also use target-specific variables
3683 (@pxref{Target-specific, ,Target-specific Variable Values}) to obtain
3684 a tighter correspondence between the variable and the recipe that
3685 uses it.
3687 @node Variables in Recipes,  , Splitting Recipe Lines, Recipe Syntax
3688 @subsection Using Variables in Recipes
3689 @cindex variable references in recipes
3690 @cindex recipes, using variables in
3692 The other way in which @code{make} processes recipes is by expanding
3693 any variable references in them (@pxref{Reference,Basics of Variable
3694 References}).  This occurs after make has finished reading all the
3695 makefiles and the target is determined to be out of date; so, the
3696 recipes for targets which are not rebuilt are never expanded.
3698 Variable and function references in recipes have identical syntax and
3699 semantics to references elsewhere in the makefile.  They also have the
3700 same quoting rules: if you want a dollar sign to appear in your
3701 recipe, you must double it (@samp{$$}).  For shells like the default
3702 shell, that use dollar signs to introduce variables, it's important to
3703 keep clear in your mind whether the variable you want to reference is
3704 a @code{make} variable (use a single dollar sign) or a shell variable
3705 (use two dollar signs).  For example:
3707 @example
3708 @group
3709 LIST = one two three
3710 all:
3711         for i in $(LIST); do \
3712             echo $$i; \
3713         done
3714 @end group
3715 @end example
3717 @noindent
3718 results in the following command being passed to the shell:
3720 @example
3721 @group
3722 for i in one two three; do \
3723     echo $i; \
3724 done
3725 @end group
3726 @end example
3728 @noindent
3729 which generates the expected result:
3731 @example
3732 @group
3735 three
3736 @end group
3737 @end example
3739 @node Echoing, Execution, Recipe Syntax, Recipes
3740 @section Recipe Echoing
3741 @cindex echoing of recipes
3742 @cindex silent operation
3743 @cindex @code{@@} (in recipes)
3744 @cindex recipes, echoing
3745 @cindex printing of recipes
3747 Normally @code{make} prints each line of the recipe before it is
3748 executed.  We call this @dfn{echoing} because it gives the appearance
3749 that you are typing the lines yourself.
3751 When a line starts with @samp{@@}, the echoing of that line is suppressed.
3752 The @samp{@@} is discarded before the line is passed to the shell.
3753 Typically you would use this for a command whose only effect is to print
3754 something, such as an @code{echo} command to indicate progress through
3755 the makefile:
3757 @example
3758 @@echo About to make distribution files
3759 @end example
3761 @cindex @code{-n}
3762 @cindex @code{--just-print}
3763 @cindex @code{--dry-run}
3764 @cindex @code{--recon}
3765 When @code{make} is given the flag @samp{-n} or @samp{--just-print} it
3766 only echoes most recipes, without executing them.  @xref{Options
3767 Summary, ,Summary of Options}.  In this case even the recipe lines
3768 starting with @samp{@@} are printed.  This flag is useful for finding
3769 out which recipes @code{make} thinks are necessary without actually
3770 doing them.
3772 @cindex @code{-s}
3773 @cindex @code{--silent}
3774 @cindex @code{--quiet}
3775 @findex .SILENT
3776 The @samp{-s} or @samp{--silent}
3777 flag to @code{make} prevents all echoing, as if all recipes
3778 started with @samp{@@}.  A rule in the makefile for the special target
3779 @code{.SILENT} without prerequisites has the same effect
3780 (@pxref{Special Targets, ,Special Built-in Target Names}).
3781 @code{.SILENT} is essentially obsolete since @samp{@@} is more flexible.@refill
3783 @node Execution, Parallel, Echoing, Recipes
3784 @section Recipe Execution
3785 @cindex recipe, execution
3786 @cindex execution, of recipes
3787 @vindex @code{SHELL} @r{(recipe execution)}
3789 When it is time to execute recipes to update a target, they are
3790 executed by invoking a new sub-shell for each line of the recipe,
3791 unless the @code{.ONESHELL} special target is in effect
3792 (@pxref{One Shell, ,Using One Shell})  (In practice, @code{make} may
3793 take shortcuts that do not affect the results.)
3795 @cindex @code{cd} (shell command)
3796 @cindex shell variables, setting in recipes
3797 @cindex recipes setting shell variables
3798 @strong{Please note:} this implies that setting shell variables and
3799 invoking shell commands such as @code{cd} that set a context local to
3800 each process will not affect the following lines in the recipe.@footnote{On
3801 MS-DOS, the value of current working directory is @strong{global}, so
3802 changing it @emph{will} affect the following recipe lines on those
3803 systems.}  If you want to use @code{cd} to affect the next statement,
3804 put both statements in a single recipe line.  Then @code{make} will
3805 invoke one shell to run the entire line, and the shell will execute
3806 the statements in sequence.  For example:
3808 @example
3809 foo : bar/lose
3810         cd $(@@D) && gobble $(@@F) > ../$@@
3811 @end example
3813 @noindent
3814 Here we use the shell AND operator (@code{&&}) so that if the
3815 @code{cd} command fails, the script will fail without trying to invoke
3816 the @code{gobble} command in the wrong directory, which could cause
3817 problems (in this case it would certainly cause @file{../foo} to be
3818 truncated, at least).
3820 @menu
3821 * One Shell::                   One shell for all lines in a recipe.
3822 * Choosing the Shell::          How @code{make} chooses the shell used
3823                                   to run recipes.
3824 @end menu
3826 @node One Shell, Choosing the Shell, Execution, Execution
3827 @subsection Using One Shell
3828 @cindex recipe lines, single shell
3829 @cindex @code{.ONESHELL}, use of
3830 @findex .ONESHELL
3832 Sometimes you would prefer that all the lines in the recipe be passed
3833 to a single invocation of the shell.  There are generally two
3834 situations where this is useful: first, it can improve performance in
3835 makefiles where recipes consist of many command lines, by avoiding
3836 extra processes.  Second, you might want newlines to be included in
3837 your recipe command (for example perhaps you are using a very
3838 different interpreter as your @code{SHELL}).  If the @code{.ONESHELL}
3839 special target appears anywhere in the makefile then @emph{all}
3840 recipe lines for each target will be provided to a single invocation
3841 of the shell.  Newlines between recipe lines will be preserved.  For
3842 example:
3844 @example
3845 .ONESHELL:
3846 foo : bar/lose
3847         cd $(@@D)
3848         gobble $(@@F) > ../$@@
3849 @end example
3851 @noindent
3852 would now work as expected even though the commands are on different
3853 recipe lines.
3855 If @code{.ONESHELL} is provided, then only the first line of the
3856 recipe will be checked for the special prefix characters (@samp{@@},
3857 @samp{-}, and @samp{+}).  Subsequent lines will include the special
3858 characters in the recipe line when the @code{SHELL} is invoked.  If
3859 you want your recipe to start with one of these special characters
3860 you'll need to arrange for them to not be the first characters on the
3861 first line, perhaps by adding a comment or similar.  For example, this
3862 would be a syntax error in Perl because the first @samp{@@} is removed
3863 by make:
3865 @example
3866 .ONESHELL:
3867 SHELL = /usr/bin/perl
3868 .SHELLFLAGS = -e
3869 show :
3870         @@f = qw(a b c);
3871         print "@@f\n";
3872 @end example
3874 @noindent
3875 However, either of these alternatives would work properly:
3877 @example
3878 .ONESHELL:
3879 SHELL = /usr/bin/perl
3880 .SHELLFLAGS = -e
3881 show :
3882         # Make sure "@@" is not the first character on the first line
3883         @@f = qw(a b c);
3884         print "@@f\n";
3885 @end example
3887 @noindent
3890 @example
3891 .ONESHELL:
3892 SHELL = /usr/bin/perl
3893 .SHELLFLAGS = -e
3894 show :
3895         my @@f = qw(a b c);
3896         print "@@f\n";
3897 @end example
3899 As a special feature, if @code{SHELL} is determined to be a
3900 POSIX-style shell, the special prefix characters in ``internal''
3901 recipe lines will @emph{removed} before the recipe is processed.  This
3902 feature is intended to allow existing makefiles to add the
3903 @code{.ONESHELL} special target and still run properly without
3904 extensive modifications.  Since the special prefix characters are not
3905 legal at the beginning of a line in a POSIX shell script this is not a
3906 loss in functionality.  For example, this works as expected:
3908 @example
3909 .ONESHELL:
3910 foo : bar/lose
3911         @@cd $(@@D)
3912         @@gobble $(@@F) > ../$@@
3913 @end example
3915 Even with this special feature, however, makefiles with
3916 @code{.ONESHELL} will behave differently in ways that could be
3917 noticeable.  For example, normally if any line in the recipe fails,
3918 that causes the rule to fail and no more recipe lines are processed.
3919 Under @code{.ONESHELL} a failure of any but the final recipe line will
3920 not be noticed by @code{make}.  You can modify @code{.SHELLFLAGS} to
3921 add the @code{-e} option to the shell which will cause any failure
3922 anywhere in the command line to cause the shell to fail, but this
3923 could itself cause your recipe to behave differently.  Ultimately you
3924 may need to harden your recipe lines to allow them to work with
3925 @code{.ONESHELL}.
3927 @node Choosing the Shell,  , One Shell, Execution
3928 @subsection Choosing the Shell
3929 @cindex shell, choosing the
3930 @cindex @code{SHELL}, value of
3931 @cindex @code{.SHELLFLAGS}, value of
3933 @vindex SHELL
3934 @vindex .SHELLFLAGS
3935 The program used as the shell is taken from the variable @code{SHELL}.
3936 If this variable is not set in your makefile, the program
3937 @file{/bin/sh} is used as the shell.  The argument(s) passed to the
3938 shell are taken from the variable @code{.SHELLFLAGS}.  The default
3939 value of @code{.SHELLFLAGS} is @code{-c} normally, or @code{-ec} in
3940 POSIX-conforming mode.
3942 @cindex environment, @code{SHELL} in
3943 Unlike most variables, the variable @code{SHELL} is never set from the
3944 environment.  This is because the @code{SHELL} environment variable is
3945 used to specify your personal choice of shell program for interactive
3946 use.  It would be very bad for personal choices like this to affect the
3947 functioning of makefiles.  @xref{Environment, ,Variables from the
3948 Environment}.
3950 Furthermore, when you do set @code{SHELL} in your makefile that value
3951 is @emph{not} exported in the environment to recipe lines that
3952 @code{make} invokes.  Instead, the value inherited from the user's
3953 environment, if any, is exported.  You can override this behavior by
3954 explicitly exporting @code{SHELL} (@pxref{Variables/Recursion,
3955 ,Communicating Variables to a Sub-@code{make}}), forcing it to be
3956 passed in the environment to recipe lines.
3958 @vindex @code{MAKESHELL} @r{(MS-DOS alternative to @code{SHELL})}
3959 However, on MS-DOS and MS-Windows the value of @code{SHELL} in the
3960 environment @strong{is} used, since on those systems most users do not
3961 set this variable, and therefore it is most likely set specifically to
3962 be used by @code{make}.  On MS-DOS, if the setting of @code{SHELL} is
3963 not suitable for @code{make}, you can set the variable
3964 @code{MAKESHELL} to the shell that @code{make} should use; if set it
3965 will be used as the shell instead of the value of @code{SHELL}.
3967 @subsubheading Choosing a Shell in DOS and Windows
3968 @cindex shell, in DOS and Windows
3969 @cindex DOS, choosing a shell in
3970 @cindex Windows, choosing a shell in
3972 Choosing a shell in MS-DOS and MS-Windows is much more complex than on
3973 other systems.
3975 @vindex COMSPEC
3976 On MS-DOS, if @code{SHELL} is not set, the value of the variable
3977 @code{COMSPEC} (which is always set) is used instead.
3979 @cindex @code{SHELL}, MS-DOS specifics
3980 The processing of lines that set the variable @code{SHELL} in Makefiles
3981 is different on MS-DOS.  The stock shell, @file{command.com}, is
3982 ridiculously limited in its functionality and many users of @code{make}
3983 tend to install a replacement shell.  Therefore, on MS-DOS, @code{make}
3984 examines the value of @code{SHELL}, and changes its behavior based on
3985 whether it points to a Unix-style or DOS-style shell.  This allows
3986 reasonable functionality even if @code{SHELL} points to
3987 @file{command.com}.
3989 If @code{SHELL} points to a Unix-style shell, @code{make} on MS-DOS
3990 additionally checks whether that shell can indeed be found; if not, it
3991 ignores the line that sets @code{SHELL}.  In MS-DOS, GNU @code{make}
3992 searches for the shell in the following places:
3994 @enumerate
3995 @item
3996 In the precise place pointed to by the value of @code{SHELL}.  For
3997 example, if the makefile specifies @samp{SHELL = /bin/sh}, @code{make}
3998 will look in the directory @file{/bin} on the current drive.
4000 @item
4001 In the current directory.
4003 @item
4004 In each of the directories in the @code{PATH} variable, in order.
4006 @end enumerate
4008 In every directory it examines, @code{make} will first look for the
4009 specific file (@file{sh} in the example above).  If this is not found,
4010 it will also look in that directory for that file with one of the known
4011 extensions which identify executable files.  For example @file{.exe},
4012 @file{.com}, @file{.bat}, @file{.btm}, @file{.sh}, and some others.
4014 If any of these attempts is successful, the value of @code{SHELL} will
4015 be set to the full pathname of the shell as found.  However, if none of
4016 these is found, the value of @code{SHELL} will not be changed, and thus
4017 the line that sets it will be effectively ignored.  This is so
4018 @code{make} will only support features specific to a Unix-style shell if
4019 such a shell is actually installed on the system where @code{make} runs.
4021 Note that this extended search for the shell is limited to the cases
4022 where @code{SHELL} is set from the Makefile; if it is set in the
4023 environment or command line, you are expected to set it to the full
4024 pathname of the shell, exactly as things are on Unix.
4026 The effect of the above DOS-specific processing is that a Makefile that
4027 contains @samp{SHELL = /bin/sh} (as many Unix makefiles do), will work
4028 on MS-DOS unaltered if you have e.g.@: @file{sh.exe} installed in some
4029 directory along your @code{PATH}.
4031 @vindex SHELL
4032 @vindex .SHELLFLAGS
4034 @node Parallel, Errors, Execution, Recipes
4035 @section Parallel Execution
4036 @cindex recipes, execution in parallel
4037 @cindex parallel execution
4038 @cindex execution, in parallel
4039 @cindex job slots
4040 @cindex @code{-j}
4041 @cindex @code{--jobs}
4043 GNU @code{make} knows how to execute several recipes at once.
4044 Normally, @code{make} will execute only one recipe at a time, waiting
4045 for it to finish before executing the next.  However, the @samp{-j} or
4046 @samp{--jobs} option tells @code{make} to execute many recipes
4047 simultaneously.  You can inhibit parallelism in a particular makefile
4048 with the @code{.NOTPARALLEL} pseudo-target (@pxref{Special
4049 Targets,Special Built-in Target Names}).@refill
4051 On MS-DOS, the @samp{-j} option has no effect, since that system doesn't
4052 support multi-processing.
4054 If the @samp{-j} option is followed by an integer, this is the number of
4055 recipes to execute at once; this is called the number of @dfn{job slots}.
4056 If there is nothing looking like an integer after the @samp{-j} option,
4057 there is no limit on the number of job slots.  The default number of job
4058 slots is one, which means serial execution (one thing at a time).
4060 One unpleasant consequence of running several recipes simultaneously is
4061 that output generated by the recipes appears whenever each recipe
4062 sends it, so messages from different recipes may be interspersed.
4064 Another problem is that two processes cannot both take input from the
4065 same device; so to make sure that only one recipe tries to take input
4066 from the terminal at once, @code{make} will invalidate the standard
4067 input streams of all but one running recipe.  This means that
4068 attempting to read from standard input will usually be a fatal error (a
4069 @samp{Broken pipe} signal) for most child processes if there are
4070 several.
4071 @cindex broken pipe
4072 @cindex standard input
4074 It is unpredictable which recipe will have a valid standard input stream
4075 (which will come from the terminal, or wherever you redirect the standard
4076 input of @code{make}).  The first recipe run will always get it first, and
4077 the first recipe started after that one finishes will get it next, and so
4080 We will change how this aspect of @code{make} works if we find a better
4081 alternative.  In the mean time, you should not rely on any recipe using
4082 standard input at all if you are using the parallel execution feature; but
4083 if you are not using this feature, then standard input works normally in
4084 all recipes.
4086 Finally, handling recursive @code{make} invocations raises issues.  For
4087 more information on this, see
4088 @ref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
4090 If a recipe fails (is killed by a signal or exits with a nonzero
4091 status), and errors are not ignored for that recipe
4092 (@pxref{Errors, ,Errors in Recipes}),
4093 the remaining recipe lines to remake the same target will not be run.
4094 If a recipe fails and the @samp{-k} or @samp{--keep-going}
4095 option was not given
4096 (@pxref{Options Summary, ,Summary of Options}),
4097 @code{make} aborts execution.  If make
4098 terminates for any reason (including a signal) with child processes
4099 running, it waits for them to finish before actually exiting.@refill
4101 @cindex load average
4102 @cindex limiting jobs based on load
4103 @cindex jobs, limiting based on load
4104 @cindex @code{-l} (load average)
4105 @cindex @code{--max-load}
4106 @cindex @code{--load-average}
4107 When the system is heavily loaded, you will probably want to run fewer jobs
4108 than when it is lightly loaded.  You can use the @samp{-l} option to tell
4109 @code{make} to limit the number of jobs to run at once, based on the load
4110 average.  The @samp{-l} or @samp{--max-load}
4111 option is followed by a floating-point number.  For
4112 example,
4114 @example
4115 -l 2.5
4116 @end example
4118 @noindent
4119 will not let @code{make} start more than one job if the load average is
4120 above 2.5.  The @samp{-l} option with no following number removes the
4121 load limit, if one was given with a previous @samp{-l} option.@refill
4123 More precisely, when @code{make} goes to start up a job, and it already has
4124 at least one job running, it checks the current load average; if it is not
4125 lower than the limit given with @samp{-l}, @code{make} waits until the load
4126 average goes below that limit, or until all the other jobs finish.
4128 By default, there is no load limit.
4130 @node Errors, Interrupts, Parallel, Recipes
4131 @section Errors in Recipes
4132 @cindex errors (in recipes)
4133 @cindex recipes, errors in
4134 @cindex exit status (errors)
4136 After each shell invocation returns, @code{make} looks at its exit
4137 status.  If the shell completed successfully (the exit status is
4138 zero), the next line in the recipe is executed in a new shell; after
4139 the last line is finished, the rule is finished.
4141 If there is an error (the exit status is nonzero), @code{make} gives up on
4142 the current rule, and perhaps on all rules.
4144 Sometimes the failure of a certain recipe line does not indicate a problem.
4145 For example, you may use the @code{mkdir} command to ensure that a
4146 directory exists.  If the directory already exists, @code{mkdir} will
4147 report an error, but you probably want @code{make} to continue regardless.
4149 @cindex @code{-} (in recipes)
4150 To ignore errors in a recipe line, write a @samp{-} at the beginning
4151 of the line's text (after the initial tab).  The @samp{-} is discarded
4152 before the line is passed to the shell for execution.
4154 For example,
4156 @example
4157 @group
4158 clean:
4159         -rm -f *.o
4160 @end group
4161 @end example
4162 @cindex @code{rm} (shell command)
4164 @noindent
4165 This causes @code{make} to continue even if @code{rm} is unable to
4166 remove a file.
4168 @cindex @code{-i}
4169 @cindex @code{--ignore-errors}
4170 @findex .IGNORE
4171 When you run @code{make} with the @samp{-i} or @samp{--ignore-errors}
4172 flag, errors are ignored in all recipes of all rules.  A rule in the
4173 makefile for the special target @code{.IGNORE} has the same effect, if
4174 there are no prerequisites.  These ways of ignoring errors are obsolete
4175 because @samp{-} is more flexible.
4177 When errors are to be ignored, because of either a @samp{-} or the
4178 @samp{-i} flag, @code{make} treats an error return just like success,
4179 except that it prints out a message that tells you the status code
4180 the shell exited with, and says that the error has been ignored.
4182 When an error happens that @code{make} has not been told to ignore,
4183 it implies that the current target cannot be correctly remade, and neither
4184 can any other that depends on it either directly or indirectly.  No further
4185 recipes will be executed for these targets, since their preconditions
4186 have not been achieved.
4189 @cindex @code{-k}
4190 @cindex @code{--keep-going}
4191 Normally @code{make} gives up immediately in this circumstance, returning a
4192 nonzero status.  However, if the @samp{-k} or @samp{--keep-going}
4193 flag is specified, @code{make}
4194 continues to consider the other prerequisites of the pending targets,
4195 remaking them if necessary, before it gives up and returns nonzero status.
4196 For example, after an error in compiling one object file, @samp{make -k}
4197 will continue compiling other object files even though it already knows
4198 that linking them will be impossible.  @xref{Options Summary, ,Summary of Options}.
4200 The usual behavior assumes that your purpose is to get the specified
4201 targets up to date; once @code{make} learns that this is impossible, it
4202 might as well report the failure immediately.  The @samp{-k} option says
4203 that the real purpose is to test as many of the changes made in the
4204 program as possible, perhaps to find several independent problems so
4205 that you can correct them all before the next attempt to compile.  This
4206 is why Emacs' @code{compile} command passes the @samp{-k} flag by
4207 default.
4208 @cindex Emacs (@code{M-x compile})
4210 @findex .DELETE_ON_ERROR
4211 @cindex deletion of target files
4212 @cindex removal of target files
4213 @cindex target, deleting on error
4214 Usually when a recipe line fails, if it has changed the target file at all,
4215 the file is corrupted and cannot be used---or at least it is not
4216 completely updated.  Yet the file's time stamp says that it is now up to
4217 date, so the next time @code{make} runs, it will not try to update that
4218 file.  The situation is just the same as when the shell is killed by a
4219 signal; @pxref{Interrupts}.  So generally the right thing to do is to
4220 delete the target file if the recipe fails after beginning to change
4221 the file.  @code{make} will do this if @code{.DELETE_ON_ERROR} appears
4222 as a target.  This is almost always what you want @code{make} to do, but
4223 it is not historical practice; so for compatibility, you must explicitly
4224 request it.
4226 @node Interrupts, Recursion, Errors, Recipes
4227 @section Interrupting or Killing @code{make}
4228 @cindex interrupt
4229 @cindex signal
4230 @cindex deletion of target files
4231 @cindex removal of target files
4232 @cindex target, deleting on interrupt
4233 @cindex killing (interruption)
4235 If @code{make} gets a fatal signal while a shell is executing, it may
4236 delete the target file that the recipe was supposed to update.  This is
4237 done if the target file's last-modification time has changed since
4238 @code{make} first checked it.
4240 The purpose of deleting the target is to make sure that it is remade from
4241 scratch when @code{make} is next run.  Why is this?  Suppose you type
4242 @kbd{Ctrl-c} while a compiler is running, and it has begun to write an
4243 object file @file{foo.o}.  The @kbd{Ctrl-c} kills the compiler, resulting
4244 in an incomplete file whose last-modification time is newer than the source
4245 file @file{foo.c}.  But @code{make} also receives the @kbd{Ctrl-c} signal
4246 and deletes this incomplete file.  If @code{make} did not do this, the next
4247 invocation of @code{make} would think that @file{foo.o} did not require
4248 updating---resulting in a strange error message from the linker when it
4249 tries to link an object file half of which is missing.
4251 @findex .PRECIOUS
4252 You can prevent the deletion of a target file in this way by making the
4253 special target @code{.PRECIOUS} depend on it.  Before remaking a target,
4254 @code{make} checks to see whether it appears on the prerequisites of
4255 @code{.PRECIOUS}, and thereby decides whether the target should be deleted
4256 if a signal happens.  Some reasons why you might do this are that the
4257 target is updated in some atomic fashion, or exists only to record a
4258 modification-time (its contents do not matter), or must exist at all
4259 times to prevent other sorts of trouble.
4261 @node Recursion, Canned Recipes, Interrupts, Recipes
4262 @section Recursive Use of @code{make}
4263 @cindex recursion
4264 @cindex subdirectories, recursion for
4266 Recursive use of @code{make} means using @code{make} as a command in a
4267 makefile.  This technique is useful when you want separate makefiles for
4268 various subsystems that compose a larger system.  For example, suppose you
4269 have a sub-directory @file{subdir} which has its own makefile, and you would
4270 like the containing directory's makefile to run @code{make} on the
4271 sub-directory.  You can do it by writing this:
4273 @example
4274 subsystem:
4275         cd subdir && $(MAKE)
4276 @end example
4278 @noindent
4279 or, equivalently, this (@pxref{Options Summary, ,Summary of Options}):
4281 @example
4282 subsystem:
4283         $(MAKE) -C subdir
4284 @end example
4285 @cindex @code{-C}
4286 @cindex @code{--directory}
4288 You can write recursive @code{make} commands just by copying this example,
4289 but there are many things to know about how they work and why, and about
4290 how the sub-@code{make} relates to the top-level @code{make}.  You may
4291 also find it useful to declare targets that invoke recursive
4292 @code{make} commands as @samp{.PHONY} (for more discussion on when
4293 this is useful, see @ref{Phony Targets}).
4295 @vindex @code{CURDIR}
4296 For your convenience, when GNU @code{make} starts (after it has
4297 processed any @code{-C} options) it sets the variable @code{CURDIR} to
4298 the pathname of the current working directory.  This value is never
4299 touched by @code{make} again: in particular note that if you include
4300 files from other directories the value of @code{CURDIR} does not
4301 change.  The value has the same precedence it would have if it were
4302 set in the makefile (by default, an environment variable @code{CURDIR}
4303 will not override this value).  Note that setting this variable has no
4304 impact on the operation of @code{make} (it does not cause @code{make}
4305 to change its working directory, for example).
4307 @menu
4308 * MAKE Variable::               The special effects of using @samp{$(MAKE)}.
4309 * Variables/Recursion::         How to communicate variables to a sub-@code{make}.
4310 * Options/Recursion::           How to communicate options to a sub-@code{make}.
4311 * -w Option::                   How the @samp{-w} or @samp{--print-directory} option
4312                                   helps debug use of recursive @code{make} commands.
4313 @end menu
4315 @node MAKE Variable, Variables/Recursion, Recursion, Recursion
4316 @subsection How the @code{MAKE} Variable Works
4317 @vindex MAKE
4318 @cindex recursion, and @code{MAKE} variable
4320 Recursive @code{make} commands should always use the variable @code{MAKE},
4321 not the explicit command name @samp{make}, as shown here:
4323 @example
4324 @group
4325 subsystem:
4326         cd subdir && $(MAKE)
4327 @end group
4328 @end example
4330 The value of this variable is the file name with which @code{make} was
4331 invoked.  If this file name was @file{/bin/make}, then the recipe executed
4332 is @samp{cd subdir && /bin/make}.  If you use a special version of
4333 @code{make} to run the top-level makefile, the same special version will be
4334 executed for recursive invocations.
4335 @cindex @code{cd} (shell command)
4337 @cindex +, and recipes
4338 As a special feature, using the variable @code{MAKE} in the recipe of
4339 a rule alters the effects of the @samp{-t} (@samp{--touch}), @samp{-n}
4340 (@samp{--just-print}), or @samp{-q} (@w{@samp{--question}}) option.
4341 Using the @code{MAKE} variable has the same effect as using a @samp{+}
4342 character at the beginning of the recipe line.  @xref{Instead of
4343 Execution, ,Instead of Executing the Recipes}.  This special feature
4344 is only enabled if the @code{MAKE} variable appears directly in the
4345 recipe: it does not apply if the @code{MAKE} variable is referenced
4346 through expansion of another variable.  In the latter case you must
4347 use the @samp{+} token to get these special effects.@refill
4349 Consider the command @samp{make -t} in the above example.  (The
4350 @samp{-t} option marks targets as up to date without actually running
4351 any recipes; see @ref{Instead of Execution}.)  Following the usual
4352 definition of @samp{-t}, a @samp{make -t} command in the example would
4353 create a file named @file{subsystem} and do nothing else.  What you
4354 really want it to do is run @samp{@w{cd subdir &&} @w{make -t}}; but
4355 that would require executing the recipe, and @samp{-t} says not to
4356 execute recipes.@refill
4357 @cindex @code{-t}, and recursion
4358 @cindex recursion, and @code{-t}
4359 @cindex @code{--touch}, and recursion
4361 The special feature makes this do what you want: whenever a recipe
4362 line of a rule contains the variable @code{MAKE}, the flags @samp{-t},
4363 @samp{-n} and @samp{-q} do not apply to that line.  Recipe lines
4364 containing @code{MAKE} are executed normally despite the presence of a
4365 flag that causes most recipes not to be run.  The usual
4366 @code{MAKEFLAGS} mechanism passes the flags to the sub-@code{make}
4367 (@pxref{Options/Recursion, ,Communicating Options to a
4368 Sub-@code{make}}), so your request to touch the files, or print the
4369 recipes, is propagated to the subsystem.@refill
4371 @node Variables/Recursion, Options/Recursion, MAKE Variable, Recursion
4372 @subsection Communicating Variables to a Sub-@code{make}
4373 @cindex sub-@code{make}
4374 @cindex environment, and recursion
4375 @cindex exporting variables
4376 @cindex variables, environment
4377 @cindex variables, exporting
4378 @cindex recursion, and environment
4379 @cindex recursion, and variables
4381 Variable values of the top-level @code{make} can be passed to the
4382 sub-@code{make} through the environment by explicit request.  These
4383 variables are defined in the sub-@code{make} as defaults, but they do
4384 not override variables defined in the makefile used by
4385 the sub-@code{make} unless you use the @samp{-e} switch (@pxref{Options
4386 Summary, ,Summary of Options}).@refill
4388 To pass down, or @dfn{export}, a variable, @code{make} adds the
4389 variable and its value to the environment for running each line of the
4390 recipe.  The sub-@code{make}, in turn, uses the environment to
4391 initialize its table of variable values.  @xref{Environment,
4392 ,Variables from the Environment}.
4394 Except by explicit request, @code{make} exports a variable only if it
4395 is either defined in the environment initially or set on the command
4396 line, and if its name consists only of letters, numbers, and underscores.
4397 Some shells cannot cope with environment variable names consisting of
4398 characters other than letters, numbers, and underscores.
4400 @cindex SHELL, exported value
4401 The value of the @code{make} variable @code{SHELL} is not exported.
4402 Instead, the value of the @code{SHELL} variable from the invoking
4403 environment is passed to the sub-@code{make}.  You can force
4404 @code{make} to export its value for @code{SHELL} by using the
4405 @code{export} directive, described below.  @xref{Choosing the Shell}.
4407 The special variable @code{MAKEFLAGS} is always exported (unless you
4408 unexport it).  @code{MAKEFILES} is exported if you set it to anything.
4410 @code{make} automatically passes down variable values that were defined
4411 on the command line, by putting them in the @code{MAKEFLAGS} variable.
4412 @iftex
4413 See the next section.
4414 @end iftex
4415 @ifnottex
4416 @xref{Options/Recursion}.
4417 @end ifnottex
4419 Variables are @emph{not} normally passed down if they were created by
4420 default by @code{make} (@pxref{Implicit Variables, ,Variables Used by
4421 Implicit Rules}).  The sub-@code{make} will define these for
4422 itself.@refill
4424 @findex export
4425 If you want to export specific variables to a sub-@code{make}, use the
4426 @code{export} directive, like this:
4428 @example
4429 export @var{variable} @dots{}
4430 @end example
4432 @noindent
4433 @findex unexport
4434 If you want to @emph{prevent} a variable from being exported, use the
4435 @code{unexport} directive, like this:
4437 @example
4438 unexport @var{variable} @dots{}
4439 @end example
4441 @noindent
4442 In both of these forms, the arguments to @code{export} and
4443 @code{unexport} are expanded, and so could be variables or functions
4444 which expand to a (list of) variable names to be (un)exported.
4446 As a convenience, you can define a variable and export it at the same
4447 time by doing:
4449 @example
4450 export @var{variable} = value
4451 @end example
4453 @noindent
4454 has the same result as:
4456 @example
4457 @var{variable} = value
4458 export @var{variable}
4459 @end example
4461 @noindent
4464 @example
4465 export @var{variable} := value
4466 @end example
4468 @noindent
4469 has the same result as:
4471 @example
4472 @var{variable} := value
4473 export @var{variable}
4474 @end example
4476 Likewise,
4478 @example
4479 export @var{variable} += value
4480 @end example
4482 @noindent
4483 is just like:
4485 @example
4486 @var{variable} += value
4487 export @var{variable}
4488 @end example
4490 @noindent
4491 @xref{Appending, ,Appending More Text to Variables}.
4493 You may notice that the @code{export} and @code{unexport} directives
4494 work in @code{make} in the same way they work in the shell, @code{sh}.
4496 If you want all variables to be exported by default, you can use
4497 @code{export} by itself:
4499 @example
4500 export
4501 @end example
4503 @noindent
4504 This tells @code{make} that variables which are not explicitly mentioned
4505 in an @code{export} or @code{unexport} directive should be exported.
4506 Any variable given in an @code{unexport} directive will still @emph{not}
4507 be exported.  If you use @code{export} by itself to export variables by
4508 default, variables whose names contain characters other than
4509 alphanumerics and underscores will not be exported unless specifically
4510 mentioned in an @code{export} directive.@refill
4512 @findex .EXPORT_ALL_VARIABLES
4513 The behavior elicited by an @code{export} directive by itself was the
4514 default in older versions of GNU @code{make}.  If your makefiles depend
4515 on this behavior and you want to be compatible with old versions of
4516 @code{make}, you can write a rule for the special target
4517 @code{.EXPORT_ALL_VARIABLES} instead of using the @code{export} directive.
4518 This will be ignored by old @code{make}s, while the @code{export}
4519 directive will cause a syntax error.@refill
4520 @cindex compatibility in exporting
4522 Likewise, you can use @code{unexport} by itself to tell @code{make}
4523 @emph{not} to export variables by default.  Since this is the default
4524 behavior, you would only need to do this if @code{export} had been used
4525 by itself earlier (in an included makefile, perhaps).  You
4526 @strong{cannot} use @code{export} and @code{unexport} by themselves to
4527 have variables exported for some recipes and not for others.  The last
4528 @code{export} or @code{unexport} directive that appears by itself
4529 determines the behavior for the entire run of @code{make}.@refill
4531 @vindex MAKELEVEL
4532 @cindex recursion, level of
4533 As a special feature, the variable @code{MAKELEVEL} is changed when it
4534 is passed down from level to level.  This variable's value is a string
4535 which is the depth of the level as a decimal number.  The value is
4536 @samp{0} for the top-level @code{make}; @samp{1} for a sub-@code{make},
4537 @samp{2} for a sub-sub-@code{make}, and so on.  The incrementation
4538 happens when @code{make} sets up the environment for a recipe.@refill
4540 The main use of @code{MAKELEVEL} is to test it in a conditional
4541 directive (@pxref{Conditionals, ,Conditional Parts of Makefiles}); this
4542 way you can write a makefile that behaves one way if run recursively and
4543 another way if run directly by you.@refill
4545 @vindex MAKEFILES
4546 You can use the variable @code{MAKEFILES} to cause all sub-@code{make}
4547 commands to use additional makefiles.  The value of @code{MAKEFILES} is
4548 a whitespace-separated list of file names.  This variable, if defined in
4549 the outer-level makefile, is passed down through the environment; then
4550 it serves as a list of extra makefiles for the sub-@code{make} to read
4551 before the usual or specified ones.  @xref{MAKEFILES Variable, ,The
4552 Variable @code{MAKEFILES}}.@refill
4554 @node Options/Recursion, -w Option, Variables/Recursion, Recursion
4555 @subsection Communicating Options to a Sub-@code{make}
4556 @cindex options, and recursion
4557 @cindex recursion, and options
4559 @vindex MAKEFLAGS
4560 Flags such as @samp{-s} and @samp{-k} are passed automatically to the
4561 sub-@code{make} through the variable @code{MAKEFLAGS}.  This variable is
4562 set up automatically by @code{make} to contain the flag letters that
4563 @code{make} received.  Thus, if you do @w{@samp{make -ks}} then
4564 @code{MAKEFLAGS} gets the value @samp{ks}.@refill
4566 As a consequence, every sub-@code{make} gets a value for @code{MAKEFLAGS}
4567 in its environment.  In response, it takes the flags from that value and
4568 processes them as if they had been given as arguments.
4569 @xref{Options Summary, ,Summary of Options}.
4571 @cindex command line variable definitions, and recursion
4572 @cindex variables, command line, and recursion
4573 @cindex recursion, and command line variable definitions
4574 Likewise variables defined on the command line are passed to the
4575 sub-@code{make} through @code{MAKEFLAGS}.  Words in the value of
4576 @code{MAKEFLAGS} that contain @samp{=}, @code{make} treats as variable
4577 definitions just as if they appeared on the command line.
4578 @xref{Overriding, ,Overriding Variables}.
4580 @cindex @code{-C}, and recursion
4581 @cindex @code{-f}, and recursion
4582 @cindex @code{-o}, and recursion
4583 @cindex @code{-W}, and recursion
4584 @cindex @code{--directory}, and recursion
4585 @cindex @code{--file}, and recursion
4586 @cindex @code{--old-file}, and recursion
4587 @cindex @code{--assume-old}, and recursion
4588 @cindex @code{--assume-new}, and recursion
4589 @cindex @code{--new-file}, and recursion
4590 @cindex recursion, and @code{-C}
4591 @cindex recursion, and @code{-f}
4592 @cindex recursion, and @code{-o}
4593 @cindex recursion, and @code{-W}
4594 The options @samp{-C}, @samp{-f}, @samp{-o}, and @samp{-W} are not put
4595 into @code{MAKEFLAGS}; these options are not passed down.@refill
4597 @cindex @code{-j}, and recursion
4598 @cindex @code{--jobs}, and recursion
4599 @cindex recursion, and @code{-j}
4600 @cindex job slots, and recursion
4601 The @samp{-j} option is a special case (@pxref{Parallel, ,Parallel Execution}).
4602 If you set it to some numeric value @samp{N} and your operating system
4603 supports it (most any UNIX system will; others typically won't), the
4604 parent @code{make} and all the sub-@code{make}s will communicate to
4605 ensure that there are only @samp{N} jobs running at the same time
4606 between them all.  Note that any job that is marked recursive
4607 (@pxref{Instead of Execution, ,Instead of Executing Recipes})
4608 doesn't count against the total jobs (otherwise we could get @samp{N}
4609 sub-@code{make}s running and have no slots left over for any real work!)
4611 If your operating system doesn't support the above communication, then
4612 @samp{-j 1} is always put into @code{MAKEFLAGS} instead of the value you
4613 specified.  This is because if the @w{@samp{-j}} option were passed down
4614 to sub-@code{make}s, you would get many more jobs running in parallel
4615 than you asked for.  If you give @samp{-j} with no numeric argument,
4616 meaning to run as many jobs as possible in parallel, this is passed
4617 down, since multiple infinities are no more than one.@refill
4619 If you do not want to pass the other flags down, you must change the
4620 value of @code{MAKEFLAGS}, like this:
4622 @example
4623 subsystem:
4624         cd subdir && $(MAKE) MAKEFLAGS=
4625 @end example
4627 @vindex MAKEOVERRIDES
4628 The command line variable definitions really appear in the variable
4629 @code{MAKEOVERRIDES}, and @code{MAKEFLAGS} contains a reference to this
4630 variable.  If you do want to pass flags down normally, but don't want to
4631 pass down the command line variable definitions, you can reset
4632 @code{MAKEOVERRIDES} to empty, like this:
4634 @example
4635 MAKEOVERRIDES =
4636 @end example
4638 @noindent
4639 @cindex Arg list too long
4640 @cindex E2BIG
4641 This is not usually useful to do.  However, some systems have a small
4642 fixed limit on the size of the environment, and putting so much
4643 information into the value of @code{MAKEFLAGS} can exceed it.  If you
4644 see the error message @samp{Arg list too long}, this may be the problem.
4645 @findex .POSIX
4646 @cindex POSIX
4647 (For strict compliance with POSIX.2, changing @code{MAKEOVERRIDES} does
4648 not affect @code{MAKEFLAGS} if the special target @samp{.POSIX} appears
4649 in the makefile.  You probably do not care about this.)
4651 @vindex MFLAGS
4652 A similar variable @code{MFLAGS} exists also, for historical
4653 compatibility.  It has the same value as @code{MAKEFLAGS} except that it
4654 does not contain the command line variable definitions, and it always
4655 begins with a hyphen unless it is empty (@code{MAKEFLAGS} begins with a
4656 hyphen only when it begins with an option that has no single-letter
4657 version, such as @samp{--warn-undefined-variables}).  @code{MFLAGS} was
4658 traditionally used explicitly in the recursive @code{make} command, like
4659 this:
4661 @example
4662 subsystem:
4663         cd subdir && $(MAKE) $(MFLAGS)
4664 @end example
4666 @noindent
4667 but now @code{MAKEFLAGS} makes this usage redundant.  If you want your
4668 makefiles to be compatible with old @code{make} programs, use this
4669 technique; it will work fine with more modern @code{make} versions too.
4671 @cindex setting options from environment
4672 @cindex options, setting from environment
4673 @cindex setting options in makefiles
4674 @cindex options, setting in makefiles
4675 The @code{MAKEFLAGS} variable can also be useful if you want to have
4676 certain options, such as @samp{-k} (@pxref{Options Summary, ,Summary of
4677 Options}), set each time you run @code{make}.  You simply put a value for
4678 @code{MAKEFLAGS} in your environment.  You can also set @code{MAKEFLAGS} in
4679 a makefile, to specify additional flags that should also be in effect for
4680 that makefile.  (Note that you cannot use @code{MFLAGS} this way.  That
4681 variable is set only for compatibility; @code{make} does not interpret a
4682 value you set for it in any way.)
4684 When @code{make} interprets the value of @code{MAKEFLAGS} (either from the
4685 environment or from a makefile), it first prepends a hyphen if the value
4686 does not already begin with one.  Then it chops the value into words
4687 separated by blanks, and parses these words as if they were options given
4688 on the command line (except that @samp{-C}, @samp{-f}, @samp{-h},
4689 @samp{-o}, @samp{-W}, and their long-named versions are ignored; and there
4690 is no error for an invalid option).
4692 If you do put @code{MAKEFLAGS} in your environment, you should be sure not
4693 to include any options that will drastically affect the actions of
4694 @code{make} and undermine the purpose of makefiles and of @code{make}
4695 itself.  For instance, the @samp{-t}, @samp{-n}, and @samp{-q} options, if
4696 put in one of these variables, could have disastrous consequences and would
4697 certainly have at least surprising and probably annoying effects.@refill
4699 @node -w Option,  , Options/Recursion, Recursion
4700 @subsection The @samp{--print-directory} Option
4701 @cindex directories, printing them
4702 @cindex printing directories
4703 @cindex recursion, and printing directories
4705 If you use several levels of recursive @code{make} invocations, the
4706 @samp{-w} or @w{@samp{--print-directory}} option can make the output a
4707 lot easier to understand by showing each directory as @code{make}
4708 starts processing it and as @code{make} finishes processing it.  For
4709 example, if @samp{make -w} is run in the directory @file{/u/gnu/make},
4710 @code{make} will print a line of the form:@refill
4712 @example
4713 make: Entering directory `/u/gnu/make'.
4714 @end example
4716 @noindent
4717 before doing anything else, and a line of the form:
4719 @example
4720 make: Leaving directory `/u/gnu/make'.
4721 @end example
4723 @noindent
4724 when processing is completed.
4726 @cindex @code{-C}, and @code{-w}
4727 @cindex @code{--directory}, and @code{--print-directory}
4728 @cindex recursion, and @code{-w}
4729 @cindex @code{-w}, and @code{-C}
4730 @cindex @code{-w}, and recursion
4731 @cindex @code{--print-directory}, and @code{--directory}
4732 @cindex @code{--print-directory}, and recursion
4733 @cindex @code{--no-print-directory}
4734 @cindex @code{--print-directory}, disabling
4735 @cindex @code{-w}, disabling
4736 Normally, you do not need to specify this option because @samp{make}
4737 does it for you: @samp{-w} is turned on automatically when you use the
4738 @samp{-C} option, and in sub-@code{make}s.  @code{make} will not
4739 automatically turn on @samp{-w} if you also use @samp{-s}, which says to
4740 be silent, or if you use @samp{--no-print-directory} to explicitly
4741 disable it.
4743 @node Canned Recipes, Empty Recipes, Recursion, Recipes
4744 @section Defining Canned Recipes
4745 @cindex canned recipes
4746 @cindex recipes, canned
4747 @cindex sequences of commands
4748 @cindex commands, sequences of
4750 When the same sequence of commands is useful in making various
4751 targets, you can define it as a canned sequence with the @code{define}
4752 directive, and refer to the canned sequence from the recipes for those
4753 targets.  The canned sequence is actually a variable, so the name must
4754 not conflict with other variable names.
4756 Here is an example of defining a canned recipe:
4758 @example
4759 define run-yacc =
4760 yacc $(firstword $^)
4761 mv y.tab.c $@@
4762 endef
4763 @end example
4764 @cindex @code{yacc}
4766 @noindent
4767 Here @code{run-yacc} is the name of the variable being defined;
4768 @code{endef} marks the end of the definition; the lines in between are the
4769 commands.  The @code{define} directive does not expand variable references
4770 and function calls in the canned sequence; the @samp{$} characters,
4771 parentheses, variable names, and so on, all become part of the value of the
4772 variable you are defining.
4773 @xref{Multi-Line, ,Defining Multi-Line Variables},
4774 for a complete explanation of @code{define}.
4776 The first command in this example runs Yacc on the first prerequisite of
4777 whichever rule uses the canned sequence.  The output file from Yacc is
4778 always named @file{y.tab.c}.  The second command moves the output to the
4779 rule's target file name.
4781 To use the canned sequence, substitute the variable into the recipe of a
4782 rule.  You can substitute it like any other variable
4783 (@pxref{Reference, ,Basics of Variable References}).
4784 Because variables defined by @code{define} are recursively expanded
4785 variables, all the variable references you wrote inside the @code{define}
4786 are expanded now.  For example:
4788 @example
4789 foo.c : foo.y
4790         $(run-yacc)
4791 @end example
4793 @noindent
4794 @samp{foo.y} will be substituted for the variable @samp{$^} when it occurs in
4795 @code{run-yacc}'s value, and @samp{foo.c} for @samp{$@@}.@refill
4797 This is a realistic example, but this particular one is not needed in
4798 practice because @code{make} has an implicit rule to figure out these
4799 commands based on the file names involved
4800 (@pxref{Implicit Rules, ,Using Implicit Rules}).
4802 @cindex @@, and @code{define}
4803 @cindex -, and @code{define}
4804 @cindex +, and @code{define}
4805 In recipe execution, each line of a canned sequence is treated just as
4806 if the line appeared on its own in the rule, preceded by a tab.  In
4807 particular, @code{make} invokes a separate sub-shell for each line.  You
4808 can use the special prefix characters that affect command lines
4809 (@samp{@@}, @samp{-}, and @samp{+}) on each line of a canned sequence.
4810 @xref{Recipes, ,Writing Recipes in Rules}.
4811 For example, using this canned sequence:
4813 @example
4814 define frobnicate =
4815 @@echo "frobnicating target $@@"
4816 frob-step-1 $< -o $@@-step-1
4817 frob-step-2 $@@-step-1 -o $@@
4818 endef
4819 @end example
4821 @noindent
4822 @code{make} will not echo the first line, the @code{echo} command.
4823 But it @emph{will} echo the following two recipe lines.
4825 On the other hand, prefix characters on the recipe line that refers to
4826 a canned sequence apply to every line in the sequence.  So the rule:
4828 @example
4829 frob.out: frob.in
4830         @@$(frobnicate)
4831 @end example
4833 @noindent
4834 does not echo @emph{any} recipe lines.
4835 (@xref{Echoing, ,Recipe Echoing}, for a full explanation of @samp{@@}.)
4837 @node Empty Recipes,  , Canned Recipes, Recipes
4838 @section Using Empty Recipes
4839 @cindex empty recipes
4840 @cindex recipes, empty
4842 It is sometimes useful to define recipes which do nothing.  This is done
4843 simply by giving a recipe that consists of nothing but whitespace.  For
4844 example:
4846 @example
4847 target: ;
4848 @end example
4850 @noindent
4851 defines an empty recipe for @file{target}.  You could also use a line
4852 beginning with a recipe prefix character to define an empty recipe,
4853 but this would be confusing because such a line looks empty.
4855 @findex .DEFAULT@r{, and empty recipes}
4856 You may be wondering why you would want to define a recipe that
4857 does nothing.  The only reason this is useful is to prevent a target
4858 from getting implicit recipes (from implicit rules or the
4859 @code{.DEFAULT} special target; @pxref{Implicit Rules} and
4860 @pxref{Last Resort, ,Defining Last-Resort Default Rules}).@refill
4862 @c !!! another reason is for canonical stamp files:
4863 @ignore
4864 @example
4865 foo: stamp-foo ;
4866 stamp-foo: foo.in
4867         create foo frm foo.in
4868         touch $@
4869 @end example
4870 @end ignore
4872 You may be inclined to define empty recipes for targets that are
4873 not actual files, but only exist so that their prerequisites can be
4874 remade.  However, this is not the best way to do that, because the
4875 prerequisites may not be remade properly if the target file actually does exist.
4876 @xref{Phony Targets, ,Phony Targets}, for a better way to do this.
4878 @node Using Variables, Conditionals, Recipes, Top
4879 @chapter How to Use Variables
4880 @cindex variable
4881 @cindex value
4882 @cindex recursive variable expansion
4883 @cindex simple variable expansion
4885 A @dfn{variable} is a name defined in a makefile to represent a string
4886 of text, called the variable's @dfn{value}.  These values are
4887 substituted by explicit request into targets, prerequisites, recipes,
4888 and other parts of the makefile.  (In some other versions of @code{make},
4889 variables are called @dfn{macros}.)
4890 @cindex macro
4892 Variables and functions in all parts of a makefile are expanded when
4893 read, except for in recipes, the right-hand sides of variable
4894 definitions using @samp{=}, and the bodies of variable definitions
4895 using the @code{define} directive.@refill
4897 Variables can represent lists of file names, options to pass to compilers,
4898 programs to run, directories to look in for source files, directories to
4899 write output in, or anything else you can imagine.
4901 A variable name may be any sequence of characters not containing
4902 @samp{:}, @samp{#}, @samp{=}, or whitespace.  However, variable names
4903 containing characters other than letters, numbers, and underscores
4904 should be considered carefully, as in some shells they cannot be
4905 passed through the environment to a sub-@code{make}
4906 (@pxref{Variables/Recursion, ,Communicating Variables to a
4907 Sub-@code{make}}).  Variable names beginning with @samp{.} and an
4908 uppercase letter may be given special meaning in future versions of
4909 @code{make}.
4911 Variable names are case-sensitive.  The names @samp{foo}, @samp{FOO},
4912 and @samp{Foo} all refer to different variables.
4914 It is traditional to use upper case letters in variable names, but we
4915 recommend using lower case letters for variable names that serve internal
4916 purposes in the makefile, and reserving upper case for parameters that
4917 control implicit rules or for parameters that the user should override with
4918 command options (@pxref{Overriding, ,Overriding Variables}).
4920 A few variables have names that are a single punctuation character or
4921 just a few characters.  These are the @dfn{automatic variables}, and
4922 they have particular specialized uses.  @xref{Automatic Variables}.
4924 @menu
4925 * Reference::                   How to use the value of a variable.
4926 * Flavors::                     Variables come in two flavors.
4927 * Advanced::                    Advanced features for referencing a variable.
4928 * Values::                      All the ways variables get their values.
4929 * Setting::                     How to set a variable in the makefile.
4930 * Appending::                   How to append more text to the old value
4931                                   of a variable.
4932 * Override Directive::          How to set a variable in the makefile even if
4933                                   the user has set it with a command argument.
4934 * Multi-Line::                  An alternate way to set a variable
4935                                   to a multi-line string.
4936 * Undefine Directive::          How to undefine a variable so that it appears
4937                                   as if it was never set.
4938 * Environment::                 Variable values can come from the environment.
4939 * Target-specific::             Variable values can be defined on a per-target
4940                                   basis.
4941 * Pattern-specific::            Target-specific variable values can be applied
4942                                   to a group of targets that match a pattern.
4943 * Suppressing Inheritance::     Suppress inheritance of variables.
4944 * Special Variables::           Variables with special meaning or behavior.
4945 @end menu
4947 @node Reference, Flavors, Using Variables, Using Variables
4948 @section Basics of Variable References
4949 @cindex variables, how to reference
4950 @cindex reference to variables
4951 @cindex @code{$}, in variable reference
4952 @cindex dollar sign (@code{$}), in variable reference
4954 To substitute a variable's value, write a dollar sign followed by the name
4955 of the variable in parentheses or braces: either @samp{$(foo)} or
4956 @samp{$@{foo@}} is a valid reference to the variable @code{foo}.  This
4957 special significance of @samp{$} is why you must write @samp{$$} to have
4958 the effect of a single dollar sign in a file name or recipe.
4960 Variable references can be used in any context: targets, prerequisites,
4961 recipes, most directives, and new variable values.  Here is an
4962 example of a common case, where a variable holds the names of all the
4963 object files in a program:
4965 @example
4966 @group
4967 objects = program.o foo.o utils.o
4968 program : $(objects)
4969         cc -o program $(objects)
4971 $(objects) : defs.h
4972 @end group
4973 @end example
4975 Variable references work by strict textual substitution.  Thus, the rule
4977 @example
4978 @group
4979 foo = c
4980 prog.o : prog.$(foo)
4981         $(foo)$(foo) -$(foo) prog.$(foo)
4982 @end group
4983 @end example
4985 @noindent
4986 could be used to compile a C program @file{prog.c}.  Since spaces before
4987 the variable value are ignored in variable assignments, the value of
4988 @code{foo} is precisely @samp{c}.  (Don't actually write your makefiles
4989 this way!)
4991 A dollar sign followed by a character other than a dollar sign,
4992 open-parenthesis or open-brace treats that single character as the
4993 variable name.  Thus, you could reference the variable @code{x} with
4994 @samp{$x}.  However, this practice is strongly discouraged, except in
4995 the case of the automatic variables (@pxref{Automatic Variables}).
4997 @node Flavors, Advanced, Reference, Using Variables
4998 @section The Two Flavors of Variables
4999 @cindex flavors of variables
5000 @cindex recursive variable expansion
5001 @cindex variables, flavors
5002 @cindex recursively expanded variables
5003 @cindex variables, recursively expanded
5005 There are two ways that a variable in GNU @code{make} can have a value;
5006 we call them the two @dfn{flavors} of variables.  The two flavors are
5007 distinguished in how they are defined and in what they do when expanded.
5009 @cindex =
5010 The first flavor of variable is a @dfn{recursively expanded} variable.
5011 Variables of this sort are defined by lines using @samp{=}
5012 (@pxref{Setting, ,Setting Variables}) or by the @code{define} directive
5013 (@pxref{Multi-Line, ,Defining Multi-Line Variables}).  The value you specify
5014 is installed verbatim; if it contains references to other variables,
5015 these references are expanded whenever this variable is substituted (in
5016 the course of expanding some other string).  When this happens, it is
5017 called @dfn{recursive expansion}.@refill
5019 For example,
5021 @example
5022 foo = $(bar)
5023 bar = $(ugh)
5024 ugh = Huh?
5026 all:;echo $(foo)
5027 @end example
5029 @noindent
5030 will echo @samp{Huh?}: @samp{$(foo)} expands to @samp{$(bar)} which
5031 expands to @samp{$(ugh)} which finally expands to @samp{Huh?}.@refill
5033 This flavor of variable is the only sort supported by most other
5034 versions of @code{make}.  It has its advantages and its disadvantages.
5035 An advantage (most would say) is that:
5037 @example
5038 CFLAGS = $(include_dirs) -O
5039 include_dirs = -Ifoo -Ibar
5040 @end example
5042 @noindent
5043 will do what was intended: when @samp{CFLAGS} is expanded in a recipe,
5044 it will expand to @samp{-Ifoo -Ibar -O}.  A major disadvantage is that you
5045 cannot append something on the end of a variable, as in
5047 @example
5048 CFLAGS = $(CFLAGS) -O
5049 @end example
5051 @noindent
5052 because it will cause an infinite loop in the variable expansion.
5053 (Actually @code{make} detects the infinite loop and reports an error.)
5054 @cindex loops in variable expansion
5055 @cindex variables, loops in expansion
5057 Another disadvantage is that any functions
5058 (@pxref{Functions, ,Functions for Transforming Text})
5059 referenced in the definition will be executed every time the variable is
5060 expanded.  This makes @code{make} run slower; worse, it causes the
5061 @code{wildcard} and @code{shell} functions to give unpredictable results
5062 because you cannot easily control when they are called, or even how many
5063 times.
5065 To avoid all the problems and inconveniences of recursively expanded
5066 variables, there is another flavor: simply expanded variables.
5068 @cindex simply expanded variables
5069 @cindex variables, simply expanded
5070 @cindex :=
5071 @cindex ::=
5072 @dfn{Simply expanded variables} are defined by lines using @samp{:=}
5073 or @samp{::=} (@pxref{Setting, ,Setting Variables}).  Both forms are
5074 equivalent in GNU @code{make}; however only the @samp{::=} form is
5075 described by the POSIX standard (support for @samp{::=} was added to
5076 the POSIX standard in 2012, so older versions of @code{make} won't
5077 accept this form either).
5079 The value of a simply expanded variable is scanned
5080 once and for all, expanding any references to other variables and
5081 functions, when the variable is defined.  The actual value of the simply
5082 expanded variable is the result of expanding the text that you write.
5083 It does not contain any references to other variables; it contains their
5084 values @emph{as of the time this variable was defined}.  Therefore,
5086 @example
5087 x := foo
5088 y := $(x) bar
5089 x := later
5090 @end example
5092 @noindent
5093 is equivalent to
5095 @example
5096 y := foo bar
5097 x := later
5098 @end example
5100 When a simply expanded variable is referenced, its value is substituted
5101 verbatim.
5103 Here is a somewhat more complicated example, illustrating the use of
5104 @samp{:=} in conjunction with the @code{shell} function.
5105 (@xref{Shell Function, , The @code{shell} Function}.)  This example
5106 also shows use of the variable @code{MAKELEVEL}, which is changed
5107 when it is passed down from level to level.
5108 (@xref{Variables/Recursion, , Communicating Variables to a
5109 Sub-@code{make}}, for information about @code{MAKELEVEL}.)
5111 @vindex MAKELEVEL
5112 @vindex MAKE
5113 @example
5114 @group
5115 ifeq (0,$@{MAKELEVEL@})
5116 whoami    := $(shell whoami)
5117 host-type := $(shell arch)
5118 MAKE := $@{MAKE@} host-type=$@{host-type@} whoami=$@{whoami@}
5119 endif
5120 @end group
5121 @end example
5123 @noindent
5124 An advantage of this use of @samp{:=} is that a typical
5125 `descend into a directory' recipe then looks like this:
5127 @example
5128 @group
5129 $@{subdirs@}:
5130         $@{MAKE@} -C $@@ all
5131 @end group
5132 @end example
5134 Simply expanded variables generally make complicated makefile programming
5135 more predictable because they work like variables in most programming
5136 languages.  They allow you to redefine a variable using its own value (or
5137 its value processed in some way by one of the expansion functions) and to
5138 use the expansion functions much more efficiently
5139 (@pxref{Functions, ,Functions for Transforming Text}).
5141 @cindex spaces, in variable values
5142 @cindex whitespace, in variable values
5143 @cindex variables, spaces in values
5144 You can also use them to introduce controlled leading whitespace into
5145 variable values.  Leading whitespace characters are discarded from your
5146 input before substitution of variable references and function calls;
5147 this means you can include leading spaces in a variable value by
5148 protecting them with variable references, like this:
5150 @example
5151 nullstring :=
5152 space := $(nullstring) # end of the line
5153 @end example
5155 @noindent
5156 Here the value of the variable @code{space} is precisely one space.  The
5157 comment @w{@samp{# end of the line}} is included here just for clarity.
5158 Since trailing space characters are @emph{not} stripped from variable
5159 values, just a space at the end of the line would have the same effect
5160 (but be rather hard to read).  If you put whitespace at the end of a
5161 variable value, it is a good idea to put a comment like that at the end
5162 of the line to make your intent clear.  Conversely, if you do @emph{not}
5163 want any whitespace characters at the end of your variable value, you
5164 must remember not to put a random comment on the end of the line after
5165 some whitespace, such as this:
5167 @example
5168 dir := /foo/bar    # directory to put the frobs in
5169 @end example
5171 @noindent
5172 Here the value of the variable @code{dir} is @w{@samp{/foo/bar    }}
5173 (with four trailing spaces), which was probably not the intention.
5174 (Imagine something like @w{@samp{$(dir)/file}} with this definition!)
5176 @cindex conditional variable assignment
5177 @cindex variables, conditional assignment
5178 @cindex ?=
5179 There is another assignment operator for variables, @samp{?=}.  This
5180 is called a conditional variable assignment operator, because it only
5181 has an effect if the variable is not yet defined.  This statement:
5183 @example
5184 FOO ?= bar
5185 @end example
5187 @noindent
5188 is exactly equivalent to this
5189 (@pxref{Origin Function, ,The @code{origin} Function}):
5191 @example
5192 ifeq ($(origin FOO), undefined)
5193   FOO = bar
5194 endif
5195 @end example
5197 Note that a variable set to an empty value is still defined, so
5198 @samp{?=} will not set that variable.
5200 @node Advanced, Values, Flavors, Using Variables
5201 @section Advanced Features for Reference to Variables
5202 @cindex reference to variables
5204 This section describes some advanced features you can use to reference
5205 variables in more flexible ways.
5207 @menu
5208 * Substitution Refs::           Referencing a variable with
5209                                   substitutions on the value.
5210 * Computed Names::              Computing the name of the variable to refer to.
5211 @end menu
5213 @node Substitution Refs, Computed Names, Advanced, Advanced
5214 @subsection Substitution References
5215 @cindex modified variable reference
5216 @cindex substitution variable reference
5217 @cindex variables, modified reference
5218 @cindex variables, substitution reference
5220 @cindex variables, substituting suffix in
5221 @cindex suffix, substituting in variables
5222 A @dfn{substitution reference} substitutes the value of a variable with
5223 alterations that you specify.  It has the form
5224 @samp{$(@var{var}:@var{a}=@var{b})} (or
5225 @samp{$@{@var{var}:@var{a}=@var{b}@}}) and its meaning is to take the value
5226 of the variable @var{var}, replace every @var{a} at the end of a word with
5227 @var{b} in that value, and substitute the resulting string.
5229 When we say ``at the end of a word'', we mean that @var{a} must appear
5230 either followed by whitespace or at the end of the value in order to be
5231 replaced; other occurrences of @var{a} in the value are unaltered.  For
5232 example:@refill
5234 @example
5235 foo := a.o b.o c.o
5236 bar := $(foo:.o=.c)
5237 @end example
5239 @noindent
5240 sets @samp{bar} to @samp{a.c b.c c.c}.  @xref{Setting, ,Setting Variables}.
5242 A substitution reference is actually an abbreviation for use of the
5243 @code{patsubst} expansion function (@pxref{Text Functions, ,Functions for String Substitution and Analysis}).  We provide
5244 substitution references as well as @code{patsubst} for compatibility with
5245 other implementations of @code{make}.
5247 @findex patsubst
5248 Another type of substitution reference lets you use the full power of
5249 the @code{patsubst} function.  It has the same form
5250 @samp{$(@var{var}:@var{a}=@var{b})} described above, except that now
5251 @var{a} must contain a single @samp{%} character.  This case is
5252 equivalent to @samp{$(patsubst @var{a},@var{b},$(@var{var}))}.
5253 @xref{Text Functions, ,Functions for String Substitution and Analysis},
5254 for a description of the @code{patsubst} function.@refill
5256 @example
5257 @group
5258 @exdent For example:
5260 foo := a.o b.o c.o
5261 bar := $(foo:%.o=%.c)
5262 @end group
5263 @end example
5265 @noindent
5266 sets @samp{bar} to @samp{a.c b.c c.c}.
5268 @node Computed Names,  , Substitution Refs, Advanced
5269 @subsection Computed Variable Names
5270 @cindex nested variable reference
5271 @cindex computed variable name
5272 @cindex variables, computed names
5273 @cindex variables, nested references
5274 @cindex variables, @samp{$} in name
5275 @cindex @code{$}, in variable name
5276 @cindex dollar sign (@code{$}), in variable name
5278 Computed variable names are a complicated concept needed only for
5279 sophisticated makefile programming.  For most purposes you need not
5280 consider them, except to know that making a variable with a dollar sign
5281 in its name might have strange results.  However, if you are the type
5282 that wants to understand everything, or you are actually interested in
5283 what they do, read on.
5285 Variables may be referenced inside the name of a variable.  This is
5286 called a @dfn{computed variable name} or a @dfn{nested variable
5287 reference}.  For example,
5289 @example
5290 x = y
5291 y = z
5292 a := $($(x))
5293 @end example
5295 @noindent
5296 defines @code{a} as @samp{z}: the @samp{$(x)} inside @samp{$($(x))} expands
5297 to @samp{y}, so @samp{$($(x))} expands to @samp{$(y)} which in turn expands
5298 to @samp{z}.  Here the name of the variable to reference is not stated
5299 explicitly; it is computed by expansion of @samp{$(x)}.  The reference
5300 @samp{$(x)} here is nested within the outer variable reference.
5302 The previous example shows two levels of nesting, but any number of levels
5303 is possible.  For example, here are three levels:
5305 @example
5306 x = y
5307 y = z
5308 z = u
5309 a := $($($(x)))
5310 @end example
5312 @noindent
5313 Here the innermost @samp{$(x)} expands to @samp{y}, so @samp{$($(x))}
5314 expands to @samp{$(y)} which in turn expands to @samp{z}; now we have
5315 @samp{$(z)}, which becomes @samp{u}.
5317 References to recursively-expanded variables within a variable name are
5318 re-expanded in the usual fashion.  For example:
5320 @example
5321 x = $(y)
5322 y = z
5323 z = Hello
5324 a := $($(x))
5325 @end example
5327 @noindent
5328 defines @code{a} as @samp{Hello}: @samp{$($(x))} becomes @samp{$($(y))}
5329 which becomes @samp{$(z)} which becomes @samp{Hello}.
5331 Nested variable references can also contain modified references and
5332 function invocations (@pxref{Functions, ,Functions for Transforming Text}),
5333 just like any other reference.
5334 For example, using the @code{subst} function
5335 (@pxref{Text Functions, ,Functions for String Substitution and Analysis}):
5337 @example
5338 @group
5339 x = variable1
5340 variable2 := Hello
5341 y = $(subst 1,2,$(x))
5342 z = y
5343 a := $($($(z)))
5344 @end group
5345 @end example
5347 @noindent
5348 eventually defines @code{a} as @samp{Hello}.  It is doubtful that anyone
5349 would ever want to write a nested reference as convoluted as this one, but
5350 it works: @samp{$($($(z)))} expands to @samp{$($(y))} which becomes
5351 @samp{$($(subst 1,2,$(x)))}.  This gets the value @samp{variable1} from
5352 @code{x} and changes it by substitution to @samp{variable2}, so that the
5353 entire string becomes @samp{$(variable2)}, a simple variable reference
5354 whose value is @samp{Hello}.@refill
5356 A computed variable name need not consist entirely of a single variable
5357 reference.  It can contain several variable references, as well as some
5358 invariant text.  For example,
5360 @example
5361 @group
5362 a_dirs := dira dirb
5363 1_dirs := dir1 dir2
5364 @end group
5366 @group
5367 a_files := filea fileb
5368 1_files := file1 file2
5369 @end group
5371 @group
5372 ifeq "$(use_a)" "yes"
5373 a1 := a
5374 else
5375 a1 := 1
5376 endif
5377 @end group
5379 @group
5380 ifeq "$(use_dirs)" "yes"
5381 df := dirs
5382 else
5383 df := files
5384 endif
5386 dirs := $($(a1)_$(df))
5387 @end group
5388 @end example
5390 @noindent
5391 will give @code{dirs} the same value as @code{a_dirs}, @code{1_dirs},
5392 @code{a_files} or @code{1_files} depending on the settings of @code{use_a}
5393 and @code{use_dirs}.@refill
5395 Computed variable names can also be used in substitution references:
5397 @example
5398 @group
5399 a_objects := a.o b.o c.o
5400 1_objects := 1.o 2.o 3.o
5402 sources := $($(a1)_objects:.o=.c)
5403 @end group
5404 @end example
5406 @noindent
5407 defines @code{sources} as either @samp{a.c b.c c.c} or @samp{1.c 2.c 3.c},
5408 depending on the value of @code{a1}.
5410 The only restriction on this sort of use of nested variable references
5411 is that they cannot specify part of the name of a function to be called.
5412 This is because the test for a recognized function name is done before
5413 the expansion of nested references.  For example,
5415 @example
5416 @group
5417 ifdef do_sort
5418 func := sort
5419 else
5420 func := strip
5421 endif
5422 @end group
5424 @group
5425 bar := a d b g q c
5426 @end group
5428 @group
5429 foo := $($(func) $(bar))
5430 @end group
5431 @end example
5433 @noindent
5434 attempts to give @samp{foo} the value of the variable @samp{sort a d b g
5435 q c} or @samp{strip a d b g q c}, rather than giving @samp{a d b g q c}
5436 as the argument to either the @code{sort} or the @code{strip} function.
5437 This restriction could be removed in the future if that change is shown
5438 to be a good idea.
5440 You can also use computed variable names in the left-hand side of a
5441 variable assignment, or in a @code{define} directive, as in:
5443 @example
5444 dir = foo
5445 $(dir)_sources := $(wildcard $(dir)/*.c)
5446 define $(dir)_print =
5447 lpr $($(dir)_sources)
5448 endef
5449 @end example
5451 @noindent
5452 This example defines the variables @samp{dir}, @samp{foo_sources}, and
5453 @samp{foo_print}.
5455 Note that @dfn{nested variable references} are quite different from
5456 @dfn{recursively expanded variables}
5457 (@pxref{Flavors, ,The Two Flavors of Variables}), though both are
5458 used together in complex ways when doing makefile programming.@refill
5460 @node Values, Setting, Advanced, Using Variables
5461 @section How Variables Get Their Values
5462 @cindex variables, how they get their values
5463 @cindex value, how a variable gets it
5465 Variables can get values in several different ways:
5467 @itemize @bullet
5468 @item
5469 You can specify an overriding value when you run @code{make}.
5470 @xref{Overriding, ,Overriding Variables}.
5472 @item
5473 You can specify a value in the makefile, either
5474 with an assignment (@pxref{Setting, ,Setting Variables}) or with a
5475 verbatim definition (@pxref{Multi-Line, ,Defining Multi-Line Variables}).@refill
5477 @item
5478 Variables in the environment become @code{make} variables.
5479 @xref{Environment, ,Variables from the Environment}.
5481 @item
5482 Several @dfn{automatic} variables are given new values for each rule.
5483 Each of these has a single conventional use.
5484 @xref{Automatic Variables}.
5486 @item
5487 Several variables have constant initial values.
5488 @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
5489 @end itemize
5491 @node Setting, Appending, Values, Using Variables
5492 @section Setting Variables
5493 @cindex setting variables
5494 @cindex variables, setting
5495 @cindex =
5496 @cindex :=
5497 @cindex ::=
5498 @cindex ?=
5499 @cindex !=
5501 To set a variable from the makefile, write a line starting with the
5502 variable name followed by @samp{=} @samp{:=}, or @samp{::=}.  Whatever
5503 follows the @samp{=}, @samp{:=}, or @samp{::=} on the line becomes the
5504 value.  For example,
5506 @example
5507 objects = main.o foo.o bar.o utils.o
5508 @end example
5510 @noindent
5511 defines a variable named @code{objects}.  Whitespace around the variable
5512 name and immediately after the @samp{=} is ignored.
5514 Variables defined with @samp{=} are @dfn{recursively expanded}
5515 variables.  Variables defined with @samp{:=} or @samp{::=} are
5516 @dfn{simply expanded} variables; these definitions can contain
5517 variable references which will be expanded before the definition is
5518 made.  @xref{Flavors, ,The Two Flavors of Variables}.
5520 The variable name may contain function and variable references, which
5521 are expanded when the line is read to find the actual variable name to use.
5523 There is no limit on the length of the value of a variable except the
5524 amount of memory on the computer.  You can split the value of a
5525 variable into multiple physical lines for readability
5526 (@pxref{Splitting Lines, ,Splitting Long Lines}).
5528 Most variable names are considered to have the empty string as a value if
5529 you have never set them.  Several variables have built-in initial values
5530 that are not empty, but you can set them in the usual ways
5531 (@pxref{Implicit Variables, ,Variables Used by Implicit Rules}).
5532 Several special variables are set
5533 automatically to a new value for each rule; these are called the
5534 @dfn{automatic} variables (@pxref{Automatic Variables}).
5536 If you'd like a variable to be set to a value only if it's not already
5537 set, then you can use the shorthand operator @samp{?=} instead of
5538 @samp{=}.  These two settings of the variable @samp{FOO} are identical
5539 (@pxref{Origin Function, ,The @code{origin} Function}):
5541 @example
5542 FOO ?= bar
5543 @end example
5545 @noindent
5548 @example
5549 ifeq ($(origin FOO), undefined)
5550 FOO = bar
5551 endif
5552 @end example
5554 The shell assignment operator @samp{!=} can be used to execute a
5555 program and set a variable to its output.  This operator first
5556 evaluates the right-hand side, then passes that result to the shell
5557 for execution.  If the result of the execution ends in a newline, that
5558 one newline is removed; all other newlines are replaced by spaces.
5559 The resulting string is then placed into the named
5560 recursively-expanded variable.  For example:
5562 @example
5563 hash != printf '\043'
5564 file_list != find . -name '*.c'
5565 @end example
5567 If the result of the execution could produce a @code{$}, and you don't
5568 intend what follows that to be interpreted as a make variable or
5569 function reference, then you must replace every @code{$} with
5570 @code{$$} as part of the execution.  Alternatively, you can set a
5571 simply expanded variable to the result of running a program using the
5572 @code{shell} function call.  @xref{Shell Function, , The @code{shell}
5573 Function}.  For example:
5575 @example
5576 hash := $(shell printf '\043')
5577 var := $(shell find . -name "*.c")
5578 @end example
5581 @node Appending, Override Directive, Setting, Using Variables
5582 @section Appending More Text to Variables
5583 @cindex +=
5584 @cindex appending to variables
5585 @cindex variables, appending to
5587 Often it is useful to add more text to the value of a variable already defined.
5588 You do this with a line containing @samp{+=}, like this:
5590 @example
5591 objects += another.o
5592 @end example
5594 @noindent
5595 This takes the value of the variable @code{objects}, and adds the text
5596 @samp{another.o} to it (preceded by a single space).  Thus:
5598 @example
5599 objects = main.o foo.o bar.o utils.o
5600 objects += another.o
5601 @end example
5603 @noindent
5604 sets @code{objects} to @samp{main.o foo.o bar.o utils.o another.o}.
5606 Using @samp{+=} is similar to:
5608 @example
5609 objects = main.o foo.o bar.o utils.o
5610 objects := $(objects) another.o
5611 @end example
5613 @noindent
5614 but differs in ways that become important when you use more complex values.
5616 When the variable in question has not been defined before, @samp{+=}
5617 acts just like normal @samp{=}: it defines a recursively-expanded
5618 variable.  However, when there @emph{is} a previous definition, exactly
5619 what @samp{+=} does depends on what flavor of variable you defined
5620 originally.  @xref{Flavors, ,The Two Flavors of Variables}, for an
5621 explanation of the two flavors of variables.
5623 When you add to a variable's value with @samp{+=}, @code{make} acts
5624 essentially as if you had included the extra text in the initial
5625 definition of the variable.  If you defined it first with @samp{:=} or
5626 @samp{::=}, making it a simply-expanded variable, @samp{+=} adds to
5627 that simply-expanded definition, and expands the new text before
5628 appending it to the old value just as @samp{:=} does (see
5629 @ref{Setting, ,Setting Variables}, for a full explanation of
5630 @samp{:=} or @samp{::=}).  In fact,
5632 @example
5633 variable := value
5634 variable += more
5635 @end example
5637 @noindent
5638 is exactly equivalent to:
5640 @noindent
5641 @example
5642 variable := value
5643 variable := $(variable) more
5644 @end example
5646 On the other hand, when you use @samp{+=} with a variable that you defined
5647 first to be recursively-expanded using plain @samp{=}, @code{make} does
5648 something a bit different.  Recall that when you define a
5649 recursively-expanded variable, @code{make} does not expand the value you set
5650 for variable and function references immediately.  Instead it stores the text
5651 verbatim, and saves these variable and function references to be expanded
5652 later, when you refer to the new variable (@pxref{Flavors, ,The Two Flavors
5653 of Variables}).  When you use @samp{+=} on a recursively-expanded variable,
5654 it is this unexpanded text to which @code{make} appends the new text you
5655 specify.
5657 @example
5658 @group
5659 variable = value
5660 variable += more
5661 @end group
5662 @end example
5664 @noindent
5665 is roughly equivalent to:
5667 @example
5668 @group
5669 temp = value
5670 variable = $(temp) more
5671 @end group
5672 @end example
5674 @noindent
5675 except that of course it never defines a variable called @code{temp}.
5676 The importance of this comes when the variable's old value contains
5677 variable references.  Take this common example:
5679 @example
5680 CFLAGS = $(includes) -O
5681 @dots{}
5682 CFLAGS += -pg # enable profiling
5683 @end example
5685 @noindent
5686 The first line defines the @code{CFLAGS} variable with a reference to another
5687 variable, @code{includes}.  (@code{CFLAGS} is used by the rules for C
5688 compilation; @pxref{Catalogue of Rules, ,Catalogue of Implicit Rules}.)
5689 Using @samp{=} for the definition makes @code{CFLAGS} a recursively-expanded
5690 variable, meaning @w{@samp{$(includes) -O}} is @emph{not} expanded when
5691 @code{make} processes the definition of @code{CFLAGS}.  Thus, @code{includes}
5692 need not be defined yet for its value to take effect.  It only has to be
5693 defined before any reference to @code{CFLAGS}.  If we tried to append to the
5694 value of @code{CFLAGS} without using @samp{+=}, we might do it like this:
5696 @example
5697 CFLAGS := $(CFLAGS) -pg # enable profiling
5698 @end example
5700 @noindent
5701 This is pretty close, but not quite what we want.  Using @samp{:=}
5702 redefines @code{CFLAGS} as a simply-expanded variable; this means
5703 @code{make} expands the text @w{@samp{$(CFLAGS) -pg}} before setting the
5704 variable.  If @code{includes} is not yet defined, we get @w{@samp{ -O
5705 -pg}}, and a later definition of @code{includes} will have no effect.
5706 Conversely, by using @samp{+=} we set @code{CFLAGS} to the
5707 @emph{unexpanded} value @w{@samp{$(includes) -O -pg}}.  Thus we preserve
5708 the reference to @code{includes}, so if that variable gets defined at
5709 any later point, a reference like @samp{$(CFLAGS)} still uses its
5710 value.
5712 @node Override Directive, Multi-Line, Appending, Using Variables
5713 @section The @code{override} Directive
5714 @findex override
5715 @cindex overriding with @code{override}
5716 @cindex variables, overriding
5718 If a variable has been set with a command argument
5719 (@pxref{Overriding, ,Overriding Variables}),
5720 then ordinary assignments in the makefile are ignored.  If you want to set
5721 the variable in the makefile even though it was set with a command
5722 argument, you can use an @code{override} directive, which is a line that
5723 looks like this:@refill
5725 @example
5726 override @var{variable} = @var{value}
5727 @end example
5729 @noindent
5732 @example
5733 override @var{variable} := @var{value}
5734 @end example
5736 To append more text to a variable defined on the command line, use:
5738 @example
5739 override @var{variable} += @var{more text}
5740 @end example
5742 @noindent
5743 @xref{Appending, ,Appending More Text to Variables}.
5745 Variable assignments marked with the @code{override} flag have a
5746 higher priority than all other assignments, except another
5747 @code{override}.  Subsequent assignments or appends to this variable
5748 which are not marked @code{override} will be ignored.
5750 The @code{override} directive was not invented for escalation in the war
5751 between makefiles and command arguments.  It was invented so you can alter
5752 and add to values that the user specifies with command arguments.
5754 For example, suppose you always want the @samp{-g} switch when you run the
5755 C compiler, but you would like to allow the user to specify the other
5756 switches with a command argument just as usual.  You could use this
5757 @code{override} directive:
5759 @example
5760 override CFLAGS += -g
5761 @end example
5763 You can also use @code{override} directives with @code{define} directives.
5764 This is done as you might expect:
5766 @example
5767 override define foo =
5769 endef
5770 @end example
5772 @noindent
5773 @iftex
5774 See the next section for information about @code{define}.
5775 @end iftex
5776 @ifnottex
5777 @xref{Multi-Line, ,Defining Multi-Line Variables}.
5778 @end ifnottex
5780 @node Multi-Line, Undefine Directive, Override Directive, Using Variables
5781 @section Defining Multi-Line Variables
5782 @findex define
5783 @findex endef
5784 @cindex multi-line variable definition
5785 @cindex variables, multi-line
5786 @cindex verbatim variable definition
5787 @cindex defining variables verbatim
5788 @cindex variables, defining verbatim
5790 Another way to set the value of a variable is to use the @code{define}
5791 directive.  This directive has an unusual syntax which allows newline
5792 characters to be included in the value, which is convenient for
5793 defining both canned sequences of commands (@pxref{Canned Recipes,
5794 ,Defining Canned Recipes}), and also sections of makefile syntax to
5795 use with @code{eval} (@pxref{Eval Function}).@refill
5797 The @code{define} directive is followed on the same line by the name
5798 of the variable being defined and an (optional) assignment operator,
5799 and nothing more.  The value to give the variable appears on the
5800 following lines.  The end of the value is marked by a line containing
5801 just the word @code{endef}.  Aside from this difference in syntax,
5802 @code{define} works just like any other variable definition.  The
5803 variable name may contain function and variable references, which are
5804 expanded when the directive is read to find the actual variable name
5805 to use.
5807 You may omit the variable assignment operator if you prefer.  If
5808 omitted, @code{make} assumes it to be @samp{=} and creates a
5809 recursively-expanded variable (@pxref{Flavors, ,The Two Flavors of Variables}).
5810 When using a @samp{+=} operator, the value is appended to the previous
5811 value as with any other append operation: with a single space
5812 separating the old and new values.
5814 You may nest @code{define} directives: @code{make} will keep track of
5815 nested directives and report an error if they are not all properly
5816 closed with @code{endef}.  Note that lines beginning with the recipe
5817 prefix character are considered part of a recipe, so any @code{define}
5818 or @code{endef} strings appearing on such a line will not be
5819 considered @code{make} directives.
5821 @example
5822 define two-lines =
5823 echo foo
5824 echo $(bar)
5825 endef
5826 @end example
5828 The value in an ordinary assignment cannot contain a newline; but the
5829 newlines that separate the lines of the value in a @code{define} become
5830 part of the variable's value (except for the final newline which precedes
5831 the @code{endef} and is not considered part of the value).@refill
5833 @need 800
5834 When used in a recipe, the previous example is functionally equivalent
5835 to this:
5837 @example
5838 two-lines = echo foo; echo $(bar)
5839 @end example
5841 @noindent
5842 since two commands separated by semicolon behave much like two separate
5843 shell commands.  However, note that using two separate lines means
5844 @code{make} will invoke the shell twice, running an independent sub-shell
5845 for each line.  @xref{Execution, ,Recipe Execution}.
5847 If you want variable definitions made with @code{define} to take
5848 precedence over command-line variable definitions, you can use the
5849 @code{override} directive together with @code{define}:
5851 @example
5852 override define two-lines =
5854 $(bar)
5855 endef
5856 @end example
5858 @noindent
5859 @xref{Override Directive, ,The @code{override} Directive}.
5861 @node Undefine Directive, Environment, Multi-Line, Using Variables
5862 @section Undefining Variables
5863 @findex undefine
5864 @cindex undefining variable
5866 If you want to clear a variable, setting its value to empty is usually
5867 sufficient. Expanding such a variable will yield the same result (empty
5868 string) regardless of whether it was set or not. However, if you are
5869 using the @code{flavor} (@pxref{Flavor Function}) and
5870 @code{origin} (@pxref{Origin Function}) functions, there is a difference
5871 between a variable that was never set and a variable with an empty value.
5872 In such situations you may want to use the @code{undefine} directive to
5873 make a variable appear as if it was never set. For example:
5875 @example
5876 foo := foo
5877 bar = bar
5879 undefine foo
5880 undefine bar
5882 $(info $(origin foo))
5883 $(info $(flavor bar))
5884 @end example
5886 This example will print ``undefined'' for both variables.
5888 If you want to undefine a command-line variable definition, you can use
5889 the @code{override} directive together with @code{undefine}, similar to
5890 how this is done for variable definitions:
5892 @example
5893 override undefine CFLAGS
5894 @end example
5896 @node Environment, Target-specific, Undefine Directive, Using Variables
5897 @section Variables from the Environment
5899 @cindex variables, environment
5900 @cindex environment
5901 Variables in @code{make} can come from the environment in which
5902 @code{make} is run.  Every environment variable that @code{make} sees
5903 when it starts up is transformed into a @code{make} variable with the
5904 same name and value.  However, an explicit assignment in the makefile,
5905 or with a command argument, overrides the environment.  (If the
5906 @samp{-e} flag is specified, then values from the environment override
5907 assignments in the makefile.  @xref{Options Summary, ,Summary of
5908 Options}.  But this is not recommended practice.)
5910 Thus, by setting the variable @code{CFLAGS} in your environment, you can
5911 cause all C compilations in most makefiles to use the compiler switches you
5912 prefer.  This is safe for variables with standard or conventional meanings
5913 because you know that no makefile will use them for other things.  (Note
5914 this is not totally reliable; some makefiles set @code{CFLAGS} explicitly
5915 and therefore are not affected by the value in the environment.)
5917 When @code{make} runs a recipe, variables defined in the
5918 makefile are placed into the environment of each shell.  This allows
5919 you to pass values to sub-@code{make} invocations (@pxref{Recursion,
5920 ,Recursive Use of @code{make}}).  By default, only variables that came
5921 from the environment or the command line are passed to recursive
5922 invocations.  You can use the @code{export} directive to pass other
5923 variables.  @xref{Variables/Recursion, , Communicating Variables to a
5924 Sub-@code{make}}, for full details.
5926 Other use of variables from the environment is not recommended.  It is not
5927 wise for makefiles to depend for their functioning on environment variables
5928 set up outside their control, since this would cause different users to get
5929 different results from the same makefile.  This is against the whole
5930 purpose of most makefiles.
5932 @cindex SHELL, import from environment
5933 Such problems would be especially likely with the variable
5934 @code{SHELL}, which is normally present in the environment to specify
5935 the user's choice of interactive shell.  It would be very undesirable
5936 for this choice to affect @code{make}; so, @code{make} handles the
5937 @code{SHELL} environment variable in a special way; see @ref{Choosing
5938 the Shell}.@refill
5940 @node Target-specific, Pattern-specific, Environment, Using Variables
5941 @section Target-specific Variable Values
5942 @cindex target-specific variables
5943 @cindex variables, target-specific
5945 Variable values in @code{make} are usually global; that is, they are the
5946 same regardless of where they are evaluated (unless they're reset, of
5947 course).  One exception to that is automatic variables
5948 (@pxref{Automatic Variables}).
5950 The other exception is @dfn{target-specific variable values}.  This
5951 feature allows you to define different values for the same variable,
5952 based on the target that @code{make} is currently building.  As with
5953 automatic variables, these values are only available within the context
5954 of a target's recipe (and in other target-specific assignments).
5956 Set a target-specific variable value like this:
5958 @example
5959 @var{target} @dots{} : @var{variable-assignment}
5960 @end example
5962 Target-specific variable assignments can be prefixed with any or all of the
5963 special keywords @code{export}, @code{override}, or @code{private};
5964 these apply their normal behavior to this instance of the variable only.
5966 Multiple @var{target} values create a target-specific variable value for
5967 each member of the target list individually.
5969 The @var{variable-assignment} can be any valid form of assignment;
5970 recursive (@samp{=}), simple (@samp{:=} or @samp{::=}), appending
5971 (@samp{+=}), or conditional (@samp{?=}).  All variables that appear
5972 within the @var{variable-assignment} are evaluated within the context
5973 of the target: thus, any previously-defined target-specific variable
5974 values will be in effect.  Note that this variable is actually
5975 distinct from any ``global'' value: the two variables do not have to
5976 have the same flavor (recursive vs.@: simple).
5978 Target-specific variables have the same priority as any other makefile
5979 variable.  Variables provided on the command line (and in the
5980 environment if the @samp{-e} option is in force) will take precedence.
5981 Specifying the @code{override} directive will allow the target-specific
5982 variable value to be preferred.
5984 There is one more special feature of target-specific variables: when
5985 you define a target-specific variable that variable value is also in
5986 effect for all prerequisites of this target, and all their
5987 prerequisites, etc.@: (unless those prerequisites override that variable
5988 with their own target-specific variable value).  So, for example, a
5989 statement like this:
5991 @example
5992 prog : CFLAGS = -g
5993 prog : prog.o foo.o bar.o
5994 @end example
5996 @noindent
5997 will set @code{CFLAGS} to @samp{-g} in the recipe for @file{prog}, but
5998 it will also set @code{CFLAGS} to @samp{-g} in the recipes that create
5999 @file{prog.o}, @file{foo.o}, and @file{bar.o}, and any recipes which
6000 create their prerequisites.
6002 Be aware that a given prerequisite will only be built once per
6003 invocation of make, at most.  If the same file is a prerequisite of
6004 multiple targets, and each of those targets has a different value for
6005 the same target-specific variable, then the first target to be built
6006 will cause that prerequisite to be built and the prerequisite will
6007 inherit the target-specific value from the first target.  It will
6008 ignore the target-specific values from any other targets.
6010 @node Pattern-specific, Suppressing Inheritance, Target-specific, Using Variables
6011 @section Pattern-specific Variable Values
6012 @cindex pattern-specific variables
6013 @cindex variables, pattern-specific
6015 In addition to target-specific variable values
6016 (@pxref{Target-specific, ,Target-specific Variable Values}), GNU
6017 @code{make} supports pattern-specific variable values.  In this form,
6018 the variable is defined for any target that matches the pattern
6019 specified.
6021 Set a pattern-specific variable value like this:
6023 @example
6024 @var{pattern} @dots{} : @var{variable-assignment}
6025 @end example
6026 where @var{pattern} is a %-pattern.  As with target-specific variable
6027 values, multiple @var{pattern} values create a pattern-specific variable
6028 value for each pattern individually.  The @var{variable-assignment} can
6029 be any valid form of assignment.  Any command line variable setting will
6030 take precedence, unless @code{override} is specified.
6032 For example:
6034 @example
6035 %.o : CFLAGS = -O
6036 @end example
6038 @noindent
6039 will assign @code{CFLAGS} the value of @samp{-O} for all targets
6040 matching the pattern @code{%.o}.
6042 If a target matches more than one pattern, the matching pattern-specific
6043 variables with longer stems are interpreted first. This results in more
6044 specific variables taking precedence over the more generic ones, for
6045 example:
6047 @example
6048 %.o: %.c
6049         $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@@
6051 lib/%.o: CFLAGS := -fPIC -g
6052 %.o: CFLAGS := -g
6054 all: foo.o lib/bar.o
6055 @end example
6057 In this example the first definition of the @code{CFLAGS} variable
6058 will be used to update @file{lib/bar.o} even though the second one
6059 also applies to this target. Pattern-specific variables which result
6060 in the same stem length are considered in the order in which they
6061 were defined in the makefile.
6063 Pattern-specific variables are searched after any target-specific
6064 variables defined explicitly for that target, and before target-specific
6065 variables defined for the parent target.
6067 @node Suppressing Inheritance, Special Variables, Pattern-specific, Using Variables
6068 @section Suppressing Inheritance
6069 @findex private
6070 @cindex suppressing inheritance
6071 @cindex inheritance, suppressing
6073 As described in previous sections, @code{make} variables are inherited
6074 by prerequisites.  This capability allows you to modify the behavior
6075 of a prerequisite based on which targets caused it to be rebuilt.  For
6076 example, you might set a target-specific variable on a @code{debug}
6077 target, then running @samp{make debug} will cause that variable to be
6078 inherited by all prerequisites of @code{debug}, while just running
6079 @samp{make all} (for example) would not have that assignment.
6081 Sometimes, however, you may not want a variable to be inherited.  For
6082 these situations, @code{make} provides the @code{private} modifier.
6083 Although this modifier can be used with any variable assignment, it
6084 makes the most sense with target- and pattern-specific variables.  Any
6085 variable marked @code{private} will be visible to its local target but
6086 will not be inherited by prerequisites of that target.  A global
6087 variable marked @code{private} will be visible in the global scope but
6088 will not be inherited by any target, and hence will not be visible
6089 in any recipe.
6091 As an example, consider this makefile:
6092 @example
6093 EXTRA_CFLAGS =
6095 prog: private EXTRA_CFLAGS = -L/usr/local/lib
6096 prog: a.o b.o
6097 @end example
6099 Due to the @code{private} modifier, @code{a.o} and @code{b.o} will not
6100 inherit the @code{EXTRA_CFLAGS} variable assignment from the
6101 @code{prog} target.
6103 @node Special Variables,  , Suppressing Inheritance, Using Variables
6104 @comment  node-name,  next,  previous,  up
6105 @section Other Special Variables
6106 @cindex makefiles, and special variables
6107 @cindex special variables
6109 GNU @code{make} supports some variables that have special properties.
6111 @table @code
6113 @vindex MAKEFILE_LIST @r{(list of parsed makefiles)}
6114 @cindex makefiles, and @code{MAKEFILE_LIST} variable
6115 @cindex including (@code{MAKEFILE_LIST} variable)
6116 @item MAKEFILE_LIST
6117 Contains the name of each makefile that is parsed by @code{make}, in
6118 the order in which it was parsed.  The name is appended just
6119 before @code{make} begins to parse the makefile.  Thus, if the first
6120 thing a makefile does is examine the last word in this variable, it
6121 will be the name of the current makefile.  Once the current makefile
6122 has used @code{include}, however, the last word will be the
6123 just-included makefile.
6125 If a makefile named @code{Makefile} has this content:
6127 @example
6128 @group
6129 name1 := $(lastword $(MAKEFILE_LIST))
6131 include inc.mk
6133 name2 := $(lastword $(MAKEFILE_LIST))
6135 all:
6136         @@echo name1 = $(name1)
6137         @@echo name2 = $(name2)
6138 @end group
6139 @end example
6141 @noindent
6142 then you would expect to see this output:
6144 @example
6145 @group
6146 name1 = Makefile
6147 name2 = inc.mk
6148 @end group
6149 @end example
6151 @vindex .DEFAULT_GOAL @r{(define default goal)}
6152 @item .DEFAULT_GOAL
6153 Sets the default goal to be used if no targets were specified on the
6154 command line (@pxref{Goals, , Arguments to Specify the Goals}).  The
6155 @code{.DEFAULT_GOAL} variable allows you to discover the current
6156 default goal, restart the default goal selection algorithm by clearing
6157 its value, or to explicitly set the default goal.  The following
6158 example illustrates these cases:
6160 @example
6161 @group
6162 # Query the default goal.
6163 ifeq ($(.DEFAULT_GOAL),)
6164   $(warning no default goal is set)
6165 endif
6167 .PHONY: foo
6168 foo: ; @@echo $@@
6170 $(warning default goal is $(.DEFAULT_GOAL))
6172 # Reset the default goal.
6173 .DEFAULT_GOAL :=
6175 .PHONY: bar
6176 bar: ; @@echo $@@
6178 $(warning default goal is $(.DEFAULT_GOAL))
6180 # Set our own.
6181 .DEFAULT_GOAL := foo
6182 @end group
6183 @end example
6185 This makefile prints:
6187 @example
6188 @group
6189 no default goal is set
6190 default goal is foo
6191 default goal is bar
6193 @end group
6194 @end example
6196 Note that assigning more than one target name to @code{.DEFAULT_GOAL} is
6197 invalid and will result in an error.
6199 @vindex MAKE_RESTARTS @r{(number of times @code{make} has restarted)}
6200 @item MAKE_RESTARTS
6201 This variable is set only if this instance of @code{make} has
6202 restarted (@pxref{Remaking Makefiles, , How Makefiles Are Remade}): it
6203 will contain the number of times this instance has restarted.  Note
6204 this is not the same as recursion (counted by the @code{MAKELEVEL}
6205 variable).  You should not set, modify, or export this variable.
6207 @vindex .RECIPEPREFIX @r{(change the recipe prefix character)}
6208 @item .RECIPEPREFIX
6209 The first character of the value of this variable is used as the
6210 character make assumes is introducing a recipe line.  If the variable
6211 is empty (as it is by default) that character is the standard tab
6212 character.  For example, this is a valid makefile:
6214 @example
6215 @group
6216 .RECIPEPREFIX = >
6217 all:
6218 > @@echo Hello, world
6219 @end group
6220 @end example
6222 The value of @code{.RECIPEPREFIX} can be changed multiple times; once set
6223 it stays in effect for all rules parsed until it is modified.
6225 @vindex .VARIABLES @r{(list of variables)}
6226 @item .VARIABLES
6227 Expands to a list of the @emph{names} of all global variables defined
6228 so far.  This includes variables which have empty values, as well as
6229 built-in variables (@pxref{Implicit Variables, , Variables Used by
6230 Implicit Rules}), but does not include any variables which are only
6231 defined in a target-specific context.  Note that any value you assign
6232 to this variable will be ignored; it will always return its special
6233 value.
6235 @c @vindex .TARGETS @r{(list of targets)}
6236 @c @item .TARGETS
6237 @c The second special variable is @code{.TARGETS}.  When expanded, the
6238 @c value consists of a list of all targets defined in all makefiles read
6239 @c up until that point.  Note it's not enough for a file to be simply
6240 @c mentioned in the makefile to be listed in this variable, even if it
6241 @c would match an implicit rule and become an ``implicit target''.  The
6242 @c file must appear as a target, on the left-hand side of a ``:'', to be
6243 @c considered a target for the purposes of this variable.
6245 @vindex .FEATURES @r{(list of supported features)}
6246 @item .FEATURES
6247 Expands to a list of special features supported by this version of
6248 @code{make}.  Possible values include, but are not limited to:
6250 @table @samp
6252 @item archives
6253 Supports @code{ar} (archive) files using special file name syntax.
6254 @xref{Archives, ,Using @code{make} to Update Archive Files}.
6256 @item check-symlink
6257 Supports the @code{-L} (@code{--check-symlink-times}) flag.
6258 @xref{Options Summary, ,Summary of Options}.
6260 @item else-if
6261 Supports ``else if'' non-nested conditionals.  @xref{Conditional
6262 Syntax, ,Syntax of Conditionals}.
6264 @item jobserver
6265 Supports ``job server'' enhanced parallel builds.  @xref{Parallel,
6266 ,Parallel Execution}.
6268 @item oneshell
6269 Supports the @code{.ONESHELL} special target.  @xref{One Shell, ,Using
6270 One Shell}.
6272 @item order-only
6273 Supports order-only prerequisites.  @xref{Prerequisite Types, ,Types
6274 of Prerequisites}.
6276 @item second-expansion
6277 Supports secondary expansion of prerequisite lists.
6279 @item shortest-stem
6280 Uses the ``shortest stem'' method of choosing which pattern, of
6281 multiple applicable options, will be used.  @xref{Pattern Match, ,How
6282 Patterns Match}.
6284 @item target-specific
6285 Supports target-specific and pattern-specific variable assignments.
6286 @xref{Target-specific, ,Target-specific Variable Values}.
6288 @item undefine
6289 Supports the @code{undefine} directive.  @xref{Undefine Directive}.
6291 @item guile
6292 Has GNU Guile available as an embedded extension language.
6293 @xref{Guile Integration, ,GNU Guile Integration}.
6295 @item load
6296 Supports dynamically loadable objects for creating custom extensions.
6297 @xref{Loading Objects, ,Loading Dynamic Objects}.
6299 @end table
6301 @vindex .INCLUDE_DIRS @r{(list of include directories)}
6302 @item .INCLUDE_DIRS
6303 Expands to a list of directories that @code{make} searches for
6304 included makefiles (@pxref{Include, , Including Other Makefiles}).
6306 @end table
6308 @node Conditionals, Functions, Using Variables, Top
6309 @chapter Conditional Parts of Makefiles
6311 @cindex conditionals
6312 A @dfn{conditional} directive causes part of a makefile to be obeyed
6313 or ignored depending on the values of variables.  Conditionals can
6314 compare the value of one variable to another, or the value of a
6315 variable to a constant string.  Conditionals control what @code{make}
6316 actually ``sees'' in the makefile, so they @emph{cannot} be used to
6317 control recipes at the time of execution.@refill
6319 @menu
6320 * Conditional Example::         Example of a conditional
6321 * Conditional Syntax::          The syntax of conditionals.
6322 * Testing Flags::               Conditionals that test flags.
6323 @end menu
6325 @node Conditional Example, Conditional Syntax, Conditionals, Conditionals
6326 @section Example of a Conditional
6328 The following example of a conditional tells @code{make} to use one
6329 set of libraries if the @code{CC} variable is @samp{gcc}, and a
6330 different set of libraries otherwise.  It works by controlling which
6331 of two recipe lines will be used for the rule.  The result is that
6332 @samp{CC=gcc} as an argument to @code{make} changes not only which
6333 compiler is used but also which libraries are linked.
6335 @example
6336 libs_for_gcc = -lgnu
6337 normal_libs =
6339 foo: $(objects)
6340 ifeq ($(CC),gcc)
6341         $(CC) -o foo $(objects) $(libs_for_gcc)
6342 else
6343         $(CC) -o foo $(objects) $(normal_libs)
6344 endif
6345 @end example
6347 This conditional uses three directives: one @code{ifeq}, one @code{else}
6348 and one @code{endif}.
6350 The @code{ifeq} directive begins the conditional, and specifies the
6351 condition.  It contains two arguments, separated by a comma and surrounded
6352 by parentheses.  Variable substitution is performed on both arguments and
6353 then they are compared.  The lines of the makefile following the
6354 @code{ifeq} are obeyed if the two arguments match; otherwise they are
6355 ignored.
6357 The @code{else} directive causes the following lines to be obeyed if the
6358 previous conditional failed.  In the example above, this means that the
6359 second alternative linking command is used whenever the first alternative
6360 is not used.  It is optional to have an @code{else} in a conditional.
6362 The @code{endif} directive ends the conditional.  Every conditional must
6363 end with an @code{endif}.  Unconditional makefile text follows.
6365 As this example illustrates, conditionals work at the textual level:
6366 the lines of the conditional are treated as part of the makefile, or
6367 ignored, according to the condition.  This is why the larger syntactic
6368 units of the makefile, such as rules, may cross the beginning or the
6369 end of the conditional.
6371 When the variable @code{CC} has the value @samp{gcc}, the above example has
6372 this effect:
6374 @example
6375 foo: $(objects)
6376         $(CC) -o foo $(objects) $(libs_for_gcc)
6377 @end example
6379 @noindent
6380 When the variable @code{CC} has any other value, the effect is this:
6382 @example
6383 foo: $(objects)
6384         $(CC) -o foo $(objects) $(normal_libs)
6385 @end example
6387 Equivalent results can be obtained in another way by conditionalizing a
6388 variable assignment and then using the variable unconditionally:
6390 @example
6391 libs_for_gcc = -lgnu
6392 normal_libs =
6394 ifeq ($(CC),gcc)
6395   libs=$(libs_for_gcc)
6396 else
6397   libs=$(normal_libs)
6398 endif
6400 foo: $(objects)
6401         $(CC) -o foo $(objects) $(libs)
6402 @end example
6404 @node Conditional Syntax, Testing Flags, Conditional Example, Conditionals
6405 @section Syntax of Conditionals
6406 @findex ifdef
6407 @findex ifeq
6408 @findex ifndef
6409 @findex ifneq
6410 @findex else
6411 @findex endif
6413 The syntax of a simple conditional with no @code{else} is as follows:
6415 @example
6416 @var{conditional-directive}
6417 @var{text-if-true}
6418 endif
6419 @end example
6421 @noindent
6422 The @var{text-if-true} may be any lines of text, to be considered as part
6423 of the makefile if the condition is true.  If the condition is false, no
6424 text is used instead.
6426 The syntax of a complex conditional is as follows:
6428 @example
6429 @var{conditional-directive}
6430 @var{text-if-true}
6431 else
6432 @var{text-if-false}
6433 endif
6434 @end example
6438 @example
6439 @var{conditional-directive-one}
6440 @var{text-if-one-is-true}
6441 else @var{conditional-directive-two}
6442 @var{text-if-two-is-true}
6443 else
6444 @var{text-if-one-and-two-are-false}
6445 endif
6446 @end example
6448 @noindent
6449 There can be as many ``@code{else} @var{conditional-directive}''
6450 clauses as necessary.  Once a given condition is true,
6451 @var{text-if-true} is used and no other clause is used; if no
6452 condition is true then @var{text-if-false} is used.  The
6453 @var{text-if-true} and @var{text-if-false} can be any number of lines
6454 of text.
6456 The syntax of the @var{conditional-directive} is the same whether the
6457 conditional is simple or complex; after an @code{else} or not.  There
6458 are four different directives that test different conditions.  Here is
6459 a table of them:
6461 @table @code
6462 @item ifeq (@var{arg1}, @var{arg2})
6463 @itemx ifeq '@var{arg1}' '@var{arg2}'
6464 @itemx ifeq "@var{arg1}" "@var{arg2}"
6465 @itemx ifeq "@var{arg1}" '@var{arg2}'
6466 @itemx ifeq '@var{arg1}' "@var{arg2}"
6467 Expand all variable references in @var{arg1} and @var{arg2} and
6468 compare them.  If they are identical, the @var{text-if-true} is
6469 effective; otherwise, the @var{text-if-false}, if any, is effective.
6471 Often you want to test if a variable has a non-empty value.  When the
6472 value results from complex expansions of variables and functions,
6473 expansions you would consider empty may actually contain whitespace
6474 characters and thus are not seen as empty.  However, you can use the
6475 @code{strip} function (@pxref{Text Functions}) to avoid interpreting
6476 whitespace as a non-empty value.  For example:
6478 @example
6479 @group
6480 ifeq ($(strip $(foo)),)
6481 @var{text-if-empty}
6482 endif
6483 @end group
6484 @end example
6486 @noindent
6487 will evaluate @var{text-if-empty} even if the expansion of
6488 @code{$(foo)} contains whitespace characters.
6490 @item ifneq (@var{arg1}, @var{arg2})
6491 @itemx ifneq '@var{arg1}' '@var{arg2}'
6492 @itemx ifneq "@var{arg1}" "@var{arg2}"
6493 @itemx ifneq "@var{arg1}" '@var{arg2}'
6494 @itemx ifneq '@var{arg1}' "@var{arg2}"
6495 Expand all variable references in @var{arg1} and @var{arg2} and
6496 compare them.  If they are different, the @var{text-if-true} is
6497 effective; otherwise, the @var{text-if-false}, if any, is effective.
6499 @item ifdef @var{variable-name}
6500 The @code{ifdef} form takes the @emph{name} of a variable as its
6501 argument, not a reference to a variable.  The value of that variable
6502 has a non-empty value, the @var{text-if-true} is effective; otherwise,
6503 the @var{text-if-false}, if any, is effective.  Variables that have
6504 never been defined have an empty value.  The text @var{variable-name}
6505 is expanded, so it could be a variable or function that expands
6506 to the name of a variable.  For example:
6508 @example
6509 bar = true
6510 foo = bar
6511 ifdef $(foo)
6512 frobozz = yes
6513 endif
6514 @end example
6516 The variable reference @code{$(foo)} is expanded, yielding @code{bar},
6517 which is considered to be the name of a variable.  The variable
6518 @code{bar} is not expanded, but its value is examined to determine if
6519 it is non-empty.
6521 Note that @code{ifdef} only tests whether a variable has a value.  It
6522 does not expand the variable to see if that value is nonempty.
6523 Consequently, tests using @code{ifdef} return true for all definitions
6524 except those like @code{foo =}.  To test for an empty value, use
6525 @w{@code{ifeq ($(foo),)}}.  For example,
6527 @example
6528 bar =
6529 foo = $(bar)
6530 ifdef foo
6531 frobozz = yes
6532 else
6533 frobozz = no
6534 endif
6535 @end example
6537 @noindent
6538 sets @samp{frobozz} to @samp{yes}, while:
6540 @example
6541 foo =
6542 ifdef foo
6543 frobozz = yes
6544 else
6545 frobozz = no
6546 endif
6547 @end example
6549 @noindent
6550 sets @samp{frobozz} to @samp{no}.
6552 @item ifndef @var{variable-name}
6553 If the variable @var{variable-name} has an empty value, the
6554 @var{text-if-true} is effective; otherwise, the @var{text-if-false},
6555 if any, is effective.  The rules for expansion and testing of
6556 @var{variable-name} are identical to the @code{ifdef} directive.
6557 @end table
6559 Extra spaces are allowed and ignored at the beginning of the
6560 conditional directive line, but a tab is not allowed.  (If the line
6561 begins with a tab, it will be considered part of a recipe for a rule.)
6562 Aside from this, extra spaces or tabs may be inserted with no effect
6563 anywhere except within the directive name or within an argument.  A
6564 comment starting with @samp{#} may appear at the end of the line.
6566 The other two directives that play a part in a conditional are @code{else}
6567 and @code{endif}.  Each of these directives is written as one word, with no
6568 arguments.  Extra spaces are allowed and ignored at the beginning of the
6569 line, and spaces or tabs at the end.  A comment starting with @samp{#} may
6570 appear at the end of the line.
6572 Conditionals affect which lines of the makefile @code{make} uses.  If
6573 the condition is true, @code{make} reads the lines of the
6574 @var{text-if-true} as part of the makefile; if the condition is false,
6575 @code{make} ignores those lines completely.  It follows that syntactic
6576 units of the makefile, such as rules, may safely be split across the
6577 beginning or the end of the conditional.@refill
6579 @code{make} evaluates conditionals when it reads a makefile.
6580 Consequently, you cannot use automatic variables in the tests of
6581 conditionals because they are not defined until recipes are run
6582 (@pxref{Automatic Variables}).
6584 To prevent intolerable confusion, it is not permitted to start a
6585 conditional in one makefile and end it in another.  However, you may
6586 write an @code{include} directive within a conditional, provided you do
6587 not attempt to terminate the conditional inside the included file.
6589 @node Testing Flags,  , Conditional Syntax, Conditionals
6590 @section Conditionals that Test Flags
6592 You can write a conditional that tests @code{make} command flags such as
6593 @samp{-t} by using the variable @code{MAKEFLAGS} together with the
6594 @code{findstring} function
6595 (@pxref{Text Functions, , Functions for String Substitution and Analysis}).
6596 This is useful when @code{touch} is not enough to make a file appear up
6597 to date.
6599 The @code{findstring} function determines whether one string appears as a
6600 substring of another.  If you want to test for the @samp{-t} flag,
6601 use @samp{t} as the first string and the value of @code{MAKEFLAGS} as
6602 the other.
6604 For example, here is how to arrange to use @samp{ranlib -t} to finish
6605 marking an archive file up to date:
6607 @example
6608 archive.a: @dots{}
6609 ifneq (,$(findstring t,$(MAKEFLAGS)))
6610         +touch archive.a
6611         +ranlib -t archive.a
6612 else
6613         ranlib archive.a
6614 endif
6615 @end example
6617 @noindent
6618 The @samp{+} prefix marks those recipe lines as ``recursive'' so that
6619 they will be executed despite use of the @samp{-t} flag.
6620 @xref{Recursion, ,Recursive Use of @code{make}}.
6622 @node Functions, Running, Conditionals, Top
6623 @chapter Functions for Transforming Text
6624 @cindex functions
6626 @dfn{Functions} allow you to do text processing in the makefile to
6627 compute the files to operate on or the commands to use in recipes.
6628 You use a function in a @dfn{function call}, where you give the name
6629 of the function and some text (the @dfn{arguments}) for the function
6630 to operate on.  The result of the function's processing is substituted
6631 into the makefile at the point of the call, just as a variable might
6632 be substituted.
6634 @menu
6635 * Syntax of Functions::         How to write a function call.
6636 * Text Functions::              General-purpose text manipulation functions.
6637 * File Name Functions::         Functions for manipulating file names.
6638 * Conditional Functions::       Functions that implement conditions.
6639 * Foreach Function::            Repeat some text with controlled variation.
6640 * File Function::               Write text to a file.
6641 * Call Function::               Expand a user-defined function.
6642 * Value Function::              Return the un-expanded value of a variable.
6643 * Eval Function::               Evaluate the arguments as makefile syntax.
6644 * Origin Function::             Find where a variable got its value.
6645 * Flavor Function::             Find out the flavor of a variable.
6646 * Make Control Functions::      Functions that control how make runs.
6647 * Shell Function::              Substitute the output of a shell command.
6648 * Guile Function::              Use GNU Guile embedded scripting language.
6649 @end menu
6651 @node Syntax of Functions, Text Functions, Functions, Functions
6652 @section Function Call Syntax
6653 @cindex @code{$}, in function call
6654 @cindex dollar sign (@code{$}), in function call
6655 @cindex arguments of functions
6656 @cindex functions, syntax of
6658 A function call resembles a variable reference.  It can appear
6659 anywhere a variable reference can appear, and it is expanded using the
6660 same rules as variable references.  A function call looks like this:
6662 @example
6663 $(@var{function} @var{arguments})
6664 @end example
6666 @noindent
6667 or like this:
6669 @example
6670 $@{@var{function} @var{arguments}@}
6671 @end example
6673 Here @var{function} is a function name; one of a short list of names
6674 that are part of @code{make}.  You can also essentially create your own
6675 functions by using the @code{call} built-in function.
6677 The @var{arguments} are the arguments of the function.  They are
6678 separated from the function name by one or more spaces or tabs, and if
6679 there is more than one argument, then they are separated by commas.
6680 Such whitespace and commas are not part of an argument's value.  The
6681 delimiters which you use to surround the function call, whether
6682 parentheses or braces, can appear in an argument only in matching pairs;
6683 the other kind of delimiters may appear singly.  If the arguments
6684 themselves contain other function calls or variable references, it is
6685 wisest to use the same kind of delimiters for all the references; write
6686 @w{@samp{$(subst a,b,$(x))}}, not @w{@samp{$(subst a,b,$@{x@})}}.  This
6687 is because it is clearer, and because only one type of delimiter is
6688 matched to find the end of the reference.
6690 The text written for each argument is processed by substitution of
6691 variables and function calls to produce the argument value, which
6692 is the text on which the function acts.  The substitution is done in the
6693 order in which the arguments appear.
6695 Commas and unmatched parentheses or braces cannot appear in the text of an
6696 argument as written; leading spaces cannot appear in the text of the first
6697 argument as written.  These characters can be put into the argument value
6698 by variable substitution.  First define variables @code{comma} and
6699 @code{space} whose values are isolated comma and space characters, then
6700 substitute these variables where such characters are wanted, like this:
6702 @example
6703 @group
6704 comma:= ,
6705 empty:=
6706 space:= $(empty) $(empty)
6707 foo:= a b c
6708 bar:= $(subst $(space),$(comma),$(foo))
6709 # @r{bar is now `a,b,c'.}
6710 @end group
6711 @end example
6713 @noindent
6714 Here the @code{subst} function replaces each space with a comma, through
6715 the value of @code{foo}, and substitutes the result.
6717 @node Text Functions, File Name Functions, Syntax of Functions, Functions
6718 @section Functions for String Substitution and Analysis
6719 @cindex functions, for text
6721 Here are some functions that operate on strings:
6723 @table @code
6724 @item $(subst @var{from},@var{to},@var{text})
6725 @findex subst
6726 Performs a textual replacement on the text @var{text}: each occurrence
6727 of @var{from} is replaced by @var{to}.  The result is substituted for
6728 the function call.  For example,
6730 @example
6731 $(subst ee,EE,feet on the street)
6732 @end example
6734 substitutes the string @samp{fEEt on the strEEt}.
6736 @item $(patsubst @var{pattern},@var{replacement},@var{text})
6737 @findex patsubst
6738 Finds whitespace-separated words in @var{text} that match
6739 @var{pattern} and replaces them with @var{replacement}.  Here
6740 @var{pattern} may contain a @samp{%} which acts as a wildcard,
6741 matching any number of any characters within a word.  If
6742 @var{replacement} also contains a @samp{%}, the @samp{%} is replaced
6743 by the text that matched the @samp{%} in @var{pattern}.  Only the first
6744 @samp{%} in the @var{pattern} and @var{replacement} is treated this
6745 way; any subsequent @samp{%} is unchanged.@refill
6747 @cindex @code{%}, quoting in @code{patsubst}
6748 @cindex @code{%}, quoting with @code{\} (backslash)
6749 @cindex @code{\} (backslash), to quote @code{%}
6750 @cindex backslash (@code{\}), to quote @code{%}
6751 @cindex quoting @code{%}, in @code{patsubst}
6752 @samp{%} characters in @code{patsubst} function invocations can be
6753 quoted with preceding backslashes (@samp{\}).  Backslashes that would
6754 otherwise quote @samp{%} characters can be quoted with more backslashes.
6755 Backslashes that quote @samp{%} characters or other backslashes are
6756 removed from the pattern before it is compared file names or has a stem
6757 substituted into it.  Backslashes that are not in danger of quoting
6758 @samp{%} characters go unmolested.  For example, the pattern
6759 @file{the\%weird\\%pattern\\} has @samp{the%weird\} preceding the
6760 operative @samp{%} character, and @samp{pattern\\} following it.  The
6761 final two backslashes are left alone because they cannot affect any
6762 @samp{%} character.@refill
6764 Whitespace between words is folded into single space characters;
6765 leading and trailing whitespace is discarded.
6767 For example,
6769 @example
6770 $(patsubst %.c,%.o,x.c.c bar.c)
6771 @end example
6773 @noindent
6774 produces the value @samp{x.c.o bar.o}.
6776 Substitution references (@pxref{Substitution Refs, ,Substitution
6777 References}) are a simpler way to get the effect of the @code{patsubst}
6778 function:
6780 @example
6781 $(@var{var}:@var{pattern}=@var{replacement})
6782 @end example
6784 @noindent
6785 is equivalent to
6787 @example
6788 $(patsubst @var{pattern},@var{replacement},$(@var{var}))
6789 @end example
6791 The second shorthand simplifies one of the most common uses of
6792 @code{patsubst}: replacing the suffix at the end of file names.
6794 @example
6795 $(@var{var}:@var{suffix}=@var{replacement})
6796 @end example
6798 @noindent
6799 is equivalent to
6801 @example
6802 $(patsubst %@var{suffix},%@var{replacement},$(@var{var}))
6803 @end example
6805 @noindent
6806 For example, you might have a list of object files:
6808 @example
6809 objects = foo.o bar.o baz.o
6810 @end example
6812 @noindent
6813 To get the list of corresponding source files, you could simply write:
6815 @example
6816 $(objects:.o=.c)
6817 @end example
6819 @noindent
6820 instead of using the general form:
6822 @example
6823 $(patsubst %.o,%.c,$(objects))
6824 @end example
6826 @item $(strip @var{string})
6827 @cindex stripping whitespace
6828 @cindex whitespace, stripping
6829 @cindex spaces, stripping
6830 @findex strip
6831 Removes leading and trailing whitespace from @var{string} and replaces
6832 each internal sequence of one or more whitespace characters with a
6833 single space.  Thus, @samp{$(strip a b  c )} results in @w{@samp{a b c}}.
6835 The function @code{strip} can be very useful when used in conjunction
6836 with conditionals.  When comparing something with the empty string
6837 @samp{} using @code{ifeq} or @code{ifneq}, you usually want a string of
6838 just whitespace to match the empty string (@pxref{Conditionals}).
6840 Thus, the following may fail to have the desired results:
6842 @example
6843 .PHONY: all
6844 ifneq   "$(needs_made)" ""
6845 all: $(needs_made)
6846 else
6847 all:;@@echo 'Nothing to make!'
6848 endif
6849 @end example
6851 @noindent
6852 Replacing the variable reference @w{@samp{$(needs_made)}} with the
6853 function call @w{@samp{$(strip $(needs_made))}} in the @code{ifneq}
6854 directive would make it more robust.@refill
6856 @item $(findstring @var{find},@var{in})
6857 @findex findstring
6858 @cindex searching for strings
6859 @cindex finding strings
6860 @cindex strings, searching for
6861 Searches @var{in} for an occurrence of @var{find}.  If it occurs, the
6862 value is @var{find}; otherwise, the value is empty.  You can use this
6863 function in a conditional to test for the presence of a specific
6864 substring in a given string.  Thus, the two examples,
6866 @example
6867 $(findstring a,a b c)
6868 $(findstring a,b c)
6869 @end example
6871 @noindent
6872 produce the values @samp{a} and @samp{} (the empty string),
6873 respectively.  @xref{Testing Flags}, for a practical application of
6874 @code{findstring}.@refill
6876 @need 750
6877 @findex filter
6878 @cindex filtering words
6879 @cindex words, filtering
6880 @item $(filter @var{pattern}@dots{},@var{text})
6881 Returns all whitespace-separated words in @var{text} that @emph{do} match
6882 any of the @var{pattern} words, removing any words that @emph{do not}
6883 match.  The patterns are written using @samp{%}, just like the patterns
6884 used in the @code{patsubst} function above.@refill
6886 The @code{filter} function can be used to separate out different types
6887 of strings (such as file names) in a variable.  For example:
6889 @example
6890 sources := foo.c bar.c baz.s ugh.h
6891 foo: $(sources)
6892         cc $(filter %.c %.s,$(sources)) -o foo
6893 @end example
6895 @noindent
6896 says that @file{foo} depends of @file{foo.c}, @file{bar.c},
6897 @file{baz.s} and @file{ugh.h} but only @file{foo.c}, @file{bar.c} and
6898 @file{baz.s} should be specified in the command to the
6899 compiler.@refill
6901 @item $(filter-out @var{pattern}@dots{},@var{text})
6902 @findex filter-out
6903 @cindex filtering out words
6904 @cindex words, filtering out
6905 Returns all whitespace-separated words in @var{text} that @emph{do not}
6906 match any of the @var{pattern} words, removing the words that @emph{do}
6907 match one or more.  This is the exact opposite of the @code{filter}
6908 function.@refill
6910 For example, given:
6912 @example
6913 @group
6914 objects=main1.o foo.o main2.o bar.o
6915 mains=main1.o main2.o
6916 @end group
6917 @end example
6919 @noindent
6920 the following generates a list which contains all the object files not
6921 in @samp{mains}:
6923 @example
6924 $(filter-out $(mains),$(objects))
6925 @end example
6927 @need 1500
6928 @findex sort
6929 @cindex sorting words
6930 @item $(sort @var{list})
6931 Sorts the words of @var{list} in lexical order, removing duplicate
6932 words.  The output is a list of words separated by single spaces.
6933 Thus,
6935 @example
6936 $(sort foo bar lose)
6937 @end example
6939 @noindent
6940 returns the value @samp{bar foo lose}.
6942 @cindex removing duplicate words
6943 @cindex duplicate words, removing
6944 @cindex words, removing duplicates
6945 Incidentally, since @code{sort} removes duplicate words, you can use
6946 it for this purpose even if you don't care about the sort order.
6948 @item $(word @var{n},@var{text})
6949 @findex word
6950 @cindex word, selecting a
6951 @cindex selecting a word
6952 Returns the @var{n}th word of @var{text}.  The legitimate values of
6953 @var{n} start from 1.  If @var{n} is bigger than the number of words
6954 in @var{text}, the value is empty.  For example,
6956 @example
6957 $(word 2, foo bar baz)
6958 @end example
6960 @noindent
6961 returns @samp{bar}.
6963 @item $(wordlist @var{s},@var{e},@var{text})
6964 @findex wordlist
6965 @cindex words, selecting lists of
6966 @cindex selecting word lists
6967 Returns the list of words in @var{text} starting with word @var{s} and
6968 ending with word @var{e} (inclusive).  The legitimate values of @var{s}
6969 start from 1; @var{e} may start from 0.  If @var{s} is bigger than the
6970 number of words in @var{text}, the value is empty.  If @var{e} is
6971 bigger than the number of words in @var{text}, words up to the end of
6972 @var{text} are returned.  If @var{s} is greater than @var{e}, nothing
6973 is returned.  For example,
6975 @example
6976 $(wordlist 2, 3, foo bar baz)
6977 @end example
6979 @noindent
6980 returns @samp{bar baz}.
6982 @c Following item phrased to prevent overfull hbox.  --RJC 17 Jul 92
6983 @item $(words @var{text})
6984 @findex words
6985 @cindex words, finding number
6986 Returns the number of words in @var{text}.
6987 Thus, the last word of @var{text} is
6988 @w{@code{$(word $(words @var{text}),@var{text})}}.@refill
6990 @item $(firstword @var{names}@dots{})
6991 @findex firstword
6992 @cindex words, extracting first
6993 The argument @var{names} is regarded as a series of names, separated
6994 by whitespace.  The value is the first name in the series.  The rest
6995 of the names are ignored.
6997 For example,
6999 @example
7000 $(firstword foo bar)
7001 @end example
7003 @noindent
7004 produces the result @samp{foo}.  Although @code{$(firstword
7005 @var{text})} is the same as @code{$(word 1,@var{text})}, the
7006 @code{firstword} function is retained for its simplicity.@refill
7009 @item $(lastword @var{names}@dots{})
7010 @findex lastword
7011 @cindex words, extracting last
7012 The argument @var{names} is regarded as a series of names, separated
7013 by whitespace.  The value is the last name in the series.
7015 For example,
7017 @example
7018 $(lastword foo bar)
7019 @end example
7021 @noindent
7022 produces the result @samp{bar}.  Although @code{$(lastword
7023 @var{text})} is the same as @code{$(word $(words @var{text}),@var{text})},
7024 the @code{lastword} function was added for its simplicity and better
7025 performance.@refill
7026 @end table
7029 Here is a realistic example of the use of @code{subst} and
7030 @code{patsubst}.  Suppose that a makefile uses the @code{VPATH} variable
7031 to specify a list of directories that @code{make} should search for
7032 prerequisite files
7033 (@pxref{General Search, , @code{VPATH} Search Path for All Prerequisites}).
7034 This example shows how to
7035 tell the C compiler to search for header files in the same list of
7036 directories.@refill
7038 The value of @code{VPATH} is a list of directories separated by colons,
7039 such as @samp{src:../headers}.  First, the @code{subst} function is used to
7040 change the colons to spaces:
7042 @example
7043 $(subst :, ,$(VPATH))
7044 @end example
7046 @noindent
7047 This produces @samp{src ../headers}.  Then @code{patsubst} is used to turn
7048 each directory name into a @samp{-I} flag.  These can be added to the
7049 value of the variable @code{CFLAGS}, which is passed automatically to the C
7050 compiler, like this:
7052 @example
7053 override CFLAGS += $(patsubst %,-I%,$(subst :, ,$(VPATH)))
7054 @end example
7056 @noindent
7057 The effect is to append the text @samp{-Isrc -I../headers} to the
7058 previously given value of @code{CFLAGS}.  The @code{override} directive is
7059 used so that the new value is assigned even if the previous value of
7060 @code{CFLAGS} was specified with a command argument (@pxref{Override
7061 Directive, , The @code{override} Directive}).
7063 @node File Name Functions, Conditional Functions, Text Functions, Functions
7064 @section Functions for File Names
7065 @cindex functions, for file names
7066 @cindex file name functions
7068 Several of the built-in expansion functions relate specifically to
7069 taking apart file names or lists of file names.
7071 Each of the following functions performs a specific transformation on a
7072 file name.  The argument of the function is regarded as a series of file
7073 names, separated by whitespace.  (Leading and trailing whitespace is
7074 ignored.)  Each file name in the series is transformed in the same way and
7075 the results are concatenated with single spaces between them.
7077 @table @code
7078 @item $(dir @var{names}@dots{})
7079 @findex dir
7080 @cindex directory part
7081 @cindex file name, directory part
7082 Extracts the directory-part of each file name in @var{names}.  The
7083 directory-part of the file name is everything up through (and
7084 including) the last slash in it.  If the file name contains no slash,
7085 the directory part is the string @samp{./}.  For example,
7087 @example
7088 $(dir src/foo.c hacks)
7089 @end example
7091 @noindent
7092 produces the result @samp{src/ ./}.
7094 @item $(notdir @var{names}@dots{})
7095 @findex notdir
7096 @cindex file name, nondirectory part
7097 @cindex nondirectory part
7098 Extracts all but the directory-part of each file name in @var{names}.
7099 If the file name contains no slash, it is left unchanged.  Otherwise,
7100 everything through the last slash is removed from it.
7102 A file name that ends with a slash becomes an empty string.  This is
7103 unfortunate, because it means that the result does not always have the
7104 same number of whitespace-separated file names as the argument had;
7105 but we do not see any other valid alternative.
7107 For example,
7109 @example
7110 $(notdir src/foo.c hacks)
7111 @end example
7113 @noindent
7114 produces the result @samp{foo.c hacks}.
7116 @item $(suffix @var{names}@dots{})
7117 @findex suffix
7118 @cindex suffix, function to find
7119 @cindex file name suffix
7120 Extracts the suffix of each file name in @var{names}.  If the file name
7121 contains a period, the suffix is everything starting with the last
7122 period.  Otherwise, the suffix is the empty string.  This frequently
7123 means that the result will be empty when @var{names} is not, and if
7124 @var{names} contains multiple file names, the result may contain fewer
7125 file names.
7127 For example,
7129 @example
7130 $(suffix src/foo.c src-1.0/bar.c hacks)
7131 @end example
7133 @noindent
7134 produces the result @samp{.c .c}.
7136 @item $(basename @var{names}@dots{})
7137 @findex basename
7138 @cindex basename
7139 @cindex file name, basename of
7140 Extracts all but the suffix of each file name in @var{names}.  If the
7141 file name contains a period, the basename is everything starting up to
7142 (and not including) the last period.  Periods in the directory part are
7143 ignored.  If there is no period, the basename is the entire file name.
7144 For example,
7146 @example
7147 $(basename src/foo.c src-1.0/bar hacks)
7148 @end example
7150 @noindent
7151 produces the result @samp{src/foo src-1.0/bar hacks}.
7153 @c plural convention with dots (be consistent)
7154 @item $(addsuffix @var{suffix},@var{names}@dots{})
7155 @findex addsuffix
7156 @cindex suffix, adding
7157 @cindex file name suffix, adding
7158 The argument @var{names} is regarded as a series of names, separated
7159 by whitespace; @var{suffix} is used as a unit.  The value of
7160 @var{suffix} is appended to the end of each individual name and the
7161 resulting larger names are concatenated with single spaces between
7162 them.  For example,
7164 @example
7165 $(addsuffix .c,foo bar)
7166 @end example
7168 @noindent
7169 produces the result @samp{foo.c bar.c}.
7171 @item $(addprefix @var{prefix},@var{names}@dots{})
7172 @findex addprefix
7173 @cindex prefix, adding
7174 @cindex file name prefix, adding
7175 The argument @var{names} is regarded as a series of names, separated
7176 by whitespace; @var{prefix} is used as a unit.  The value of
7177 @var{prefix} is prepended to the front of each individual name and the
7178 resulting larger names are concatenated with single spaces between
7179 them.  For example,
7181 @example
7182 $(addprefix src/,foo bar)
7183 @end example
7185 @noindent
7186 produces the result @samp{src/foo src/bar}.
7188 @item $(join @var{list1},@var{list2})
7189 @findex join
7190 @cindex joining lists of words
7191 @cindex words, joining lists
7192 Concatenates the two arguments word by word: the two first words (one
7193 from each argument) concatenated form the first word of the result, the
7194 two second words form the second word of the result, and so on.  So the
7195 @var{n}th word of the result comes from the @var{n}th word of each
7196 argument.  If one argument has more words that the other, the extra
7197 words are copied unchanged into the result.
7199 For example, @samp{$(join a b,.c .o)} produces @samp{a.c b.o}.
7201 Whitespace between the words in the lists is not preserved; it is
7202 replaced with a single space.
7204 This function can merge the results of the @code{dir} and
7205 @code{notdir} functions, to produce the original list of files which
7206 was given to those two functions.@refill
7208 @item $(wildcard @var{pattern})
7209 @findex wildcard
7210 @cindex wildcard, function
7211 The argument @var{pattern} is a file name pattern, typically containing
7212 wildcard characters (as in shell file name patterns).  The result of
7213 @code{wildcard} is a space-separated list of the names of existing files
7214 that match the pattern.
7215 @xref{Wildcards, ,Using Wildcard Characters in File Names}.
7217 @item $(realpath @var{names}@dots{})
7218 @findex realpath
7219 @cindex realpath
7220 @cindex file name, realpath of
7221 For each file name in @var{names} return the canonical absolute name.
7222 A canonical name does not contain any @code{.} or @code{..} components,
7223 nor any repeated path separators (@code{/}) or symlinks.  In case of a
7224 failure the empty string is returned.  Consult the @code{realpath(3)}
7225 documentation for a list of possible failure causes.
7227 @item $(abspath @var{names}@dots{})
7228 @findex abspath
7229 @cindex abspath
7230 @cindex file name, abspath of
7231 For each file name in @var{names} return an absolute name that does
7232 not contain any @code{.} or @code{..} components, nor any repeated path
7233 separators (@code{/}).  Note that, in contrast to @code{realpath}
7234 function, @code{abspath} does not resolve symlinks and does not require
7235 the file names to refer to an existing file or directory.  Use the
7236 @code{wildcard} function to test for existence.
7237 @end table
7239 @node Conditional Functions, Foreach Function, File Name Functions, Functions
7240 @section Functions for Conditionals
7241 @findex if
7242 @cindex conditional expansion
7243 There are three functions that provide conditional expansion.  A key
7244 aspect of these functions is that not all of the arguments are
7245 expanded initially.  Only those arguments which need to be expanded,
7246 will be expanded.
7248 @table @code
7249 @item $(if @var{condition},@var{then-part}[,@var{else-part}])
7250 @findex if
7251 The @code{if} function provides support for conditional expansion in a
7252 functional context (as opposed to the GNU @code{make} makefile
7253 conditionals such as @code{ifeq} (@pxref{Conditional Syntax, ,Syntax of
7254 Conditionals}).
7256 The first argument, @var{condition}, first has all preceding and
7257 trailing whitespace stripped, then is expanded.  If it expands to any
7258 non-empty string, then the condition is considered to be true.  If it
7259 expands to an empty string, the condition is considered to be false.
7261 If the condition is true then the second argument, @var{then-part}, is
7262 evaluated and this is used as the result of the evaluation of the entire
7263 @code{if} function.
7265 If the condition is false then the third argument, @var{else-part}, is
7266 evaluated and this is the result of the @code{if} function.  If there is
7267 no third argument, the @code{if} function evaluates to nothing (the
7268 empty string).
7270 Note that only one of the @var{then-part} or the @var{else-part} will be
7271 evaluated, never both.  Thus, either can contain side-effects (such as
7272 @code{shell} function calls, etc.)
7274 @item $(or @var{condition1}[,@var{condition2}[,@var{condition3}@dots{}]])
7275 @findex or
7276 The @code{or} function provides a ``short-circuiting'' OR operation.
7277 Each argument is expanded, in order.  If an argument expands to a
7278 non-empty string the processing stops and the result of the expansion
7279 is that string.  If, after all arguments are expanded, all of them are
7280 false (empty), then the result of the expansion is the empty string.
7282 @item $(and @var{condition1}[,@var{condition2}[,@var{condition3}@dots{}]])
7283 @findex and
7284 The @code{and} function provides a ``short-circuiting'' AND operation.
7285 Each argument is expanded, in order.  If an argument expands to an
7286 empty string the processing stops and the result of the expansion is
7287 the empty string.  If all arguments expand to a non-empty string then
7288 the result of the expansion is the expansion of the last argument.
7290 @end table
7292 @node Foreach Function, File Function, Conditional Functions, Functions
7293 @section The @code{foreach} Function
7294 @findex foreach
7295 @cindex words, iterating over
7297 The @code{foreach} function is very different from other functions.  It
7298 causes one piece of text to be used repeatedly, each time with a different
7299 substitution performed on it.  It resembles the @code{for} command in the
7300 shell @code{sh} and the @code{foreach} command in the C-shell @code{csh}.
7302 The syntax of the @code{foreach} function is:
7304 @example
7305 $(foreach @var{var},@var{list},@var{text})
7306 @end example
7308 @noindent
7309 The first two arguments, @var{var} and @var{list}, are expanded before
7310 anything else is done; note that the last argument, @var{text}, is
7311 @strong{not} expanded at the same time.  Then for each word of the expanded
7312 value of @var{list}, the variable named by the expanded value of @var{var}
7313 is set to that word, and @var{text} is expanded.  Presumably @var{text}
7314 contains references to that variable, so its expansion will be different
7315 each time.
7317 The result is that @var{text} is expanded as many times as there are
7318 whitespace-separated words in @var{list}.  The multiple expansions of
7319 @var{text} are concatenated, with spaces between them, to make the result
7320 of @code{foreach}.
7322 This simple example sets the variable @samp{files} to the list of all files
7323 in the directories in the list @samp{dirs}:
7325 @example
7326 dirs := a b c d
7327 files := $(foreach dir,$(dirs),$(wildcard $(dir)/*))
7328 @end example
7330 Here @var{text} is @samp{$(wildcard $(dir)/*)}.  The first repetition
7331 finds the value @samp{a} for @code{dir}, so it produces the same result
7332 as @samp{$(wildcard a/*)}; the second repetition produces the result
7333 of @samp{$(wildcard b/*)}; and the third, that of @samp{$(wildcard c/*)}.
7335 This example has the same result (except for setting @samp{dirs}) as
7336 the following example:
7338 @example
7339 files := $(wildcard a/* b/* c/* d/*)
7340 @end example
7342 When @var{text} is complicated, you can improve readability by giving it
7343 a name, with an additional variable:
7345 @example
7346 find_files = $(wildcard $(dir)/*)
7347 dirs := a b c d
7348 files := $(foreach dir,$(dirs),$(find_files))
7349 @end example
7351 @noindent
7352 Here we use the variable @code{find_files} this way.  We use plain @samp{=}
7353 to define a recursively-expanding variable, so that its value contains an
7354 actual function call to be re-expanded under the control of @code{foreach};
7355 a simply-expanded variable would not do, since @code{wildcard} would be
7356 called only once at the time of defining @code{find_files}.
7358 The @code{foreach} function has no permanent effect on the variable
7359 @var{var}; its value and flavor after the @code{foreach} function call are
7360 the same as they were beforehand.  The other values which are taken from
7361 @var{list} are in effect only temporarily, during the execution of
7362 @code{foreach}.  The variable @var{var} is a simply-expanded variable
7363 during the execution of @code{foreach}.  If @var{var} was undefined
7364 before the @code{foreach} function call, it is undefined after the call.
7365 @xref{Flavors, ,The Two Flavors of Variables}.@refill
7367 You must take care when using complex variable expressions that result in
7368 variable names because many strange things are valid variable names, but
7369 are probably not what you intended.  For example,
7371 @smallexample
7372 files := $(foreach Esta-escrito-en-espanol!,b c ch,$(find_files))
7373 @end smallexample
7375 @noindent
7376 might be useful if the value of @code{find_files} references the variable
7377 whose name is @samp{Esta-escrito-en-espanol!} (es un nombre bastante largo,
7378 no?), but it is more likely to be a mistake.
7380 @node File Function, Call Function, Foreach Function, Functions
7381 @section The @code{file} Function
7382 @findex file
7383 @cindex writing to a file
7384 @cindex file, writing to
7386 The @code{file} function allows the makefile to write to a file.  Two
7387 modes of writing are supported: overwrite, where the text is written
7388 to the beginning of the file and any existing content is lost, and
7389 append, where the text is written to the end of the file, preserving
7390 the existing content.  In all cases the file is created if it does not
7391 exist.
7393 The syntax of the @code{file} function is:
7395 @example
7396 $(file @var{op} @var{filename},@var{text})
7397 @end example
7399 The operator @var{op} can be either @code{>} which indicates overwrite
7400 mode, or @code{>>} which indicates append mode.  The @var{filename}
7401 indicates the file to be written to.  There may optionally be
7402 whitespace between the operator and the file name.
7404 When the @code{file} function is expanded all its arguments are
7405 expanded first, then the file indicated by @var{filename} will be
7406 opened in the mode described by @var{op}.  Finally @var{text} will be
7407 written to the file.  If @var{text} does not already end in a newline,
7408 a final newline will be written.  The result of evaluating the
7409 @code{file} function is always the empty string.
7411 It is a fatal error if the file cannot be opened for writing, or if
7412 the write operation fails.
7414 For example, the @code{file} function can be useful if your build
7415 system has a limited command line size and your recipe runs a command
7416 that can accept arguments from a file as well.  Many commands use the
7417 convention that an argument prefixed with an @code{@@} specifies a
7418 file containing more arguments.  Then you might write your recipe in
7419 this way:
7421 @example
7422 @group
7423 program: $(OBJECTS)
7424         $(file >$@@.in,$^)
7425         $(CMD) $(CMDFLAGS) @@$@@.in
7426         @@rm $@@.in
7427 @end group
7428 @end example
7430 If the command required each argument to be on a separate line of the
7431 input file, you might write your recipe like this:
7433 @example
7434 @group
7435 program: $(OBJECTS)
7436         $(file >$@@.in,) $(foreach O,$^,$(file >>$@@.in,$O))
7437         $(CMD) $(CMDFLAGS) @@$@@.in
7438         @@rm $@@.in
7439 @end group
7440 @end example
7442 @node Call Function, Value Function, File Function, Functions
7443 @section The @code{call} Function
7444 @findex call
7445 @cindex functions, user defined
7446 @cindex user defined functions
7448 The @code{call} function is unique in that it can be used to create new
7449 parameterized functions.  You can write a complex expression as the
7450 value of a variable, then use @code{call} to expand it with different
7451 values.
7453 The syntax of the @code{call} function is:
7455 @example
7456 $(call @var{variable},@var{param},@var{param},@dots{})
7457 @end example
7459 When @code{make} expands this function, it assigns each @var{param} to
7460 temporary variables @code{$(1)}, @code{$(2)}, etc.  The variable
7461 @code{$(0)} will contain @var{variable}.  There is no maximum number of
7462 parameter arguments.  There is no minimum, either, but it doesn't make
7463 sense to use @code{call} with no parameters.
7465 Then @var{variable} is expanded as a @code{make} variable in the context
7466 of these temporary assignments.  Thus, any reference to @code{$(1)} in
7467 the value of @var{variable} will resolve to the first @var{param} in the
7468 invocation of @code{call}.
7470 Note that @var{variable} is the @emph{name} of a variable, not a
7471 @emph{reference} to that variable.  Therefore you would not normally use
7472 a @samp{$} or parentheses when writing it.  (You can, however, use a
7473 variable reference in the name if you want the name not to be a
7474 constant.)
7476 If @var{variable} is the name of a built-in function, the built-in function
7477 is always invoked (even if a @code{make} variable by that name also
7478 exists).
7480 The @code{call} function expands the @var{param} arguments before
7481 assigning them to temporary variables.  This means that @var{variable}
7482 values containing references to built-in functions that have special
7483 expansion rules, like @code{foreach} or @code{if}, may not work as you
7484 expect.
7486 Some examples may make this clearer.
7488 This macro simply reverses its arguments:
7490 @smallexample
7491 reverse = $(2) $(1)
7493 foo = $(call reverse,a,b)
7494 @end smallexample
7496 @noindent
7497 Here @var{foo} will contain @samp{b a}.
7499 This one is slightly more interesting: it defines a macro to search for
7500 the first instance of a program in @code{PATH}:
7502 @smallexample
7503 pathsearch = $(firstword $(wildcard $(addsuffix /$(1),$(subst :, ,$(PATH)))))
7505 LS := $(call pathsearch,ls)
7506 @end smallexample
7508 @noindent
7509 Now the variable LS contains @code{/bin/ls} or similar.
7511 The @code{call} function can be nested.  Each recursive invocation gets
7512 its own local values for @code{$(1)}, etc.@: that mask the values of
7513 higher-level @code{call}.  For example, here is an implementation of a
7514 @dfn{map} function:
7516 @smallexample
7517 map = $(foreach a,$(2),$(call $(1),$(a)))
7518 @end smallexample
7520 Now you can @var{map} a function that normally takes only one argument,
7521 such as @code{origin}, to multiple values in one step:
7523 @smallexample
7524 o = $(call map,origin,o map MAKE)
7525 @end smallexample
7527 and end up with @var{o} containing something like @samp{file file default}.
7529 A final caution: be careful when adding whitespace to the arguments to
7530 @code{call}.  As with other functions, any whitespace contained in the
7531 second and subsequent arguments is kept; this can cause strange
7532 effects.  It's generally safest to remove all extraneous whitespace when
7533 providing parameters to @code{call}.
7535 @node Value Function, Eval Function, Call Function, Functions
7536 @comment  node-name,  next,  previous,  up
7537 @section The @code{value} Function
7538 @findex value
7539 @cindex variables, unexpanded value
7541 The @code{value} function provides a way for you to use the value of a
7542 variable @emph{without} having it expanded.  Please note that this
7543 does not undo expansions which have already occurred; for example if
7544 you create a simply expanded variable its value is expanded during the
7545 definition; in that case the @code{value} function will return the
7546 same result as using the variable directly.
7548 The syntax of the @code{value} function is:
7550 @example
7551 $(value @var{variable})
7552 @end example
7554 Note that @var{variable} is the @emph{name} of a variable, not a
7555 @emph{reference} to that variable.  Therefore you would not normally
7556 use a @samp{$} or parentheses when writing it.  (You can, however, use
7557 a variable reference in the name if you want the name not to be a
7558 constant.)
7560 The result of this function is a string containing the value of
7561 @var{variable}, without any expansion occurring.  For example, in this
7562 makefile:
7564 @example
7565 @group
7566 FOO = $PATH
7568 all:
7569         @@echo $(FOO)
7570         @@echo $(value FOO)
7571 @end group
7572 @end example
7574 @noindent
7575 The first output line would be @code{ATH}, since the ``$P'' would be
7576 expanded as a @code{make} variable, while the second output line would
7577 be the current value of your @code{$PATH} environment variable, since
7578 the @code{value} function avoided the expansion.
7580 The @code{value} function is most often used in conjunction with the
7581 @code{eval} function (@pxref{Eval Function}).
7583 @node Eval Function, Origin Function, Value Function, Functions
7584 @comment  node-name,  next,  previous,  up
7585 @section The @code{eval} Function
7586 @findex eval
7587 @cindex evaluating makefile syntax
7588 @cindex makefile syntax, evaluating
7590 The @code{eval} function is very special: it allows you to define new
7591 makefile constructs that are not constant; which are the result of
7592 evaluating other variables and functions.  The argument to the
7593 @code{eval} function is expanded, then the results of that expansion
7594 are parsed as makefile syntax.  The expanded results can define new
7595 @code{make} variables, targets, implicit or explicit rules, etc.
7597 The result of the @code{eval} function is always the empty string;
7598 thus, it can be placed virtually anywhere in a makefile without
7599 causing syntax errors.
7601 It's important to realize that the @code{eval} argument is expanded
7602 @emph{twice}; first by the @code{eval} function, then the results of
7603 that expansion are expanded again when they are parsed as makefile
7604 syntax.  This means you may need to provide extra levels of escaping
7605 for ``$'' characters when using @code{eval}.  The @code{value}
7606 function (@pxref{Value Function}) can sometimes be useful in these
7607 situations, to circumvent unwanted expansions.
7609 Here is an example of how @code{eval} can be used; this example
7610 combines a number of concepts and other functions.  Although it might
7611 seem overly complex to use @code{eval} in this example, rather than
7612 just writing out the rules, consider two things: first, the template
7613 definition (in @code{PROGRAM_template}) could need to be much more
7614 complex than it is here; and second, you might put the complex,
7615 ``generic'' part of this example into another makefile, then include
7616 it in all the individual makefiles.  Now your individual makefiles are
7617 quite straightforward.
7619 @example
7620 @group
7621 PROGRAMS    = server client
7623 server_OBJS = server.o server_priv.o server_access.o
7624 server_LIBS = priv protocol
7626 client_OBJS = client.o client_api.o client_mem.o
7627 client_LIBS = protocol
7629 # Everything after this is generic
7631 .PHONY: all
7632 all: $(PROGRAMS)
7634 define PROGRAM_template =
7635  $(1): $$($(1)_OBJS) $$($(1)_LIBS:%=-l%)
7636  ALL_OBJS   += $$($(1)_OBJS)
7637 endef
7639 $(foreach prog,$(PROGRAMS),$(eval $(call PROGRAM_template,$(prog))))
7641 $(PROGRAMS):
7642         $(LINK.o) $^ $(LDLIBS) -o $@@
7644 clean:
7645         rm -f $(ALL_OBJS) $(PROGRAMS)
7646 @end group
7647 @end example
7649 @node Origin Function, Flavor Function, Eval Function, Functions
7650 @section The @code{origin} Function
7651 @findex origin
7652 @cindex variables, origin of
7653 @cindex origin of variable
7655 The @code{origin} function is unlike most other functions in that it does
7656 not operate on the values of variables; it tells you something @emph{about}
7657 a variable.  Specifically, it tells you where it came from.
7659 The syntax of the @code{origin} function is:
7661 @example
7662 $(origin @var{variable})
7663 @end example
7665 Note that @var{variable} is the @emph{name} of a variable to inquire about,
7666 not a @emph{reference} to that variable.  Therefore you would not normally
7667 use a @samp{$} or parentheses when writing it.  (You can, however, use a
7668 variable reference in the name if you want the name not to be a constant.)
7670 The result of this function is a string telling you how the variable
7671 @var{variable} was defined:
7673 @table @samp
7674 @item undefined
7676 if @var{variable} was never defined.
7678 @item default
7680 if @var{variable} has a default definition, as is usual with @code{CC}
7681 and so on.  @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
7682 Note that if you have redefined a default variable, the @code{origin}
7683 function will return the origin of the later definition.
7685 @item environment
7687 if @var{variable} was inherited from the environment provided to
7688 @code{make}.
7690 @item environment override
7692 if @var{variable} was inherited from the environment provided to
7693 @code{make}, and is overriding a setting for @var{variable} in the
7694 makefile as a result of the @w{@samp{-e}} option (@pxref{Options
7695 Summary, ,Summary of Options}).@refill
7697 @item file
7699 if @var{variable} was defined in a makefile.
7701 @item command line
7703 if @var{variable} was defined on the command line.
7705 @item override
7707 if @var{variable} was defined with an @code{override} directive in a
7708 makefile (@pxref{Override Directive, ,The @code{override} Directive}).
7710 @item automatic
7712 if @var{variable} is an automatic variable defined for the execution
7713 of the recipe for each rule (@pxref{Automatic Variables}).
7714 @end table
7716 This information is primarily useful (other than for your curiosity) to
7717 determine if you want to believe the value of a variable.  For example,
7718 suppose you have a makefile @file{foo} that includes another makefile
7719 @file{bar}.  You want a variable @code{bletch} to be defined in @file{bar}
7720 if you run the command @w{@samp{make -f bar}}, even if the environment contains
7721 a definition of @code{bletch}.  However, if @file{foo} defined
7722 @code{bletch} before including @file{bar}, you do not want to override that
7723 definition.  This could be done by using an @code{override} directive in
7724 @file{foo}, giving that definition precedence over the later definition in
7725 @file{bar}; unfortunately, the @code{override} directive would also
7726 override any command line definitions.  So, @file{bar} could
7727 include:@refill
7729 @example
7730 @group
7731 ifdef bletch
7732 ifeq "$(origin bletch)" "environment"
7733 bletch = barf, gag, etc.
7734 endif
7735 endif
7736 @end group
7737 @end example
7739 @noindent
7740 If @code{bletch} has been defined from the environment, this will redefine
7743 If you want to override a previous definition of @code{bletch} if it came
7744 from the environment, even under @samp{-e}, you could instead write:
7746 @example
7747 @group
7748 ifneq "$(findstring environment,$(origin bletch))" ""
7749 bletch = barf, gag, etc.
7750 endif
7751 @end group
7752 @end example
7754 Here the redefinition takes place if @samp{$(origin bletch)} returns either
7755 @samp{environment} or @samp{environment override}.
7756 @xref{Text Functions, , Functions for String Substitution and Analysis}.
7758 @node Flavor Function, Make Control Functions, Origin Function, Functions
7759 @section The @code{flavor} Function
7760 @findex flavor
7761 @cindex variables, flavor of
7762 @cindex flavor of variable
7764 The @code{flavor} function, like the @code{origin} function, does not
7765 operate on the values of variables but rather it tells you something
7766 @emph{about} a variable.  Specifically, it tells you the flavor of a
7767 variable (@pxref{Flavors, ,The Two Flavors of Variables}).
7769 The syntax of the @code{flavor} function is:
7771 @example
7772 $(flavor @var{variable})
7773 @end example
7775 Note that @var{variable} is the @emph{name} of a variable to inquire about,
7776 not a @emph{reference} to that variable.  Therefore you would not normally
7777 use a @samp{$} or parentheses when writing it.  (You can, however, use a
7778 variable reference in the name if you want the name not to be a constant.)
7780 The result of this function is a string that identifies the flavor of the
7781 variable @var{variable}:
7783 @table @samp
7784 @item undefined
7786 if @var{variable} was never defined.
7788 @item recursive
7790 if @var{variable} is a recursively expanded variable.
7792 @item simple
7794 if @var{variable} is a simply expanded variable.
7796 @end table
7798 @node Make Control Functions, Shell Function, Flavor Function, Functions
7799 @section Functions That Control Make
7800 @cindex functions, for controlling make
7801 @cindex controlling make
7803 These functions control the way make runs.  Generally, they are used to
7804 provide information to the user of the makefile or to cause make to stop
7805 if some sort of environmental error is detected.
7807 @table @code
7808 @item $(error @var{text}@dots{})
7809 @findex error
7810 @cindex error, stopping on
7811 @cindex stopping make
7812 Generates a fatal error where the message is @var{text}.  Note that
7813 the error is generated whenever this function is evaluated.  So, if
7814 you put it inside a recipe or on the right side of a recursive
7815 variable assignment, it won't be evaluated until later.  The
7816 @var{text} will be expanded before the error is generated.
7818 For example,
7820 @example
7821 ifdef ERROR1
7822 $(error error is $(ERROR1))
7823 endif
7824 @end example
7826 @noindent
7827 will generate a fatal error during the read of the makefile if the
7828 @code{make} variable @code{ERROR1} is defined.  Or,
7830 @example
7831 ERR = $(error found an error!)
7833 .PHONY: err
7834 err: ; $(ERR)
7835 @end example
7837 @noindent
7838 will generate a fatal error while @code{make} is running, if the
7839 @code{err} target is invoked.
7841 @item $(warning @var{text}@dots{})
7842 @findex warning
7843 @cindex warnings, printing
7844 @cindex printing user warnings
7845 This function works similarly to the @code{error} function, above,
7846 except that @code{make} doesn't exit.  Instead, @var{text} is expanded
7847 and the resulting message is displayed, but processing of the makefile
7848 continues.
7850 The result of the expansion of this function is the empty string.
7852 @item $(info @var{text}@dots{})
7853 @findex info
7854 @cindex printing messages
7855 This function does nothing more than print its (expanded) argument(s)
7856 to standard output.  No makefile name or line number is added.  The
7857 result of the expansion of this function is the empty string.
7858 @end table
7860 @node Shell Function, Guile Function, Make Control Functions, Functions
7861 @section The @code{shell} Function
7862 @findex shell
7863 @cindex command expansion
7864 @cindex backquotes
7865 @cindex shell command, function for
7867 The @code{shell} function is unlike any other function other than the
7868 @code{wildcard} function
7869 (@pxref{Wildcard Function, ,The Function @code{wildcard}}) in that it
7870 communicates with the world outside of @code{make}.
7872 The @code{shell} function performs the same function that backquotes
7873 (@samp{`}) perform in most shells: it does @dfn{command expansion}.
7874 This means that it takes as an argument a shell command and evaluates
7875 to the output of the command.  The only processing @code{make} does on
7876 the result is to convert each newline (or carriage-return / newline
7877 pair) to a single space.  If there is a trailing (carriage-return
7878 and) newline it will simply be removed.@refill
7880 The commands run by calls to the @code{shell} function are run when the
7881 function calls are expanded (@pxref{Reading Makefiles, , How
7882 @code{make} Reads a Makefile}).  Because this function involves
7883 spawning a new shell, you should carefully consider the performance
7884 implications of using the @code{shell} function within recursively
7885 expanded variables vs.@: simply expanded variables (@pxref{Flavors, ,The
7886 Two Flavors of Variables}).
7888 Here are some examples of the use of the @code{shell} function:
7890 @example
7891 contents := $(shell cat foo)
7892 @end example
7894 @noindent
7895 sets @code{contents} to the contents of the file @file{foo}, with a space
7896 (rather than a newline) separating each line.
7898 @example
7899 files := $(shell echo *.c)
7900 @end example
7902 @noindent
7903 sets @code{files} to the expansion of @samp{*.c}.  Unless @code{make} is
7904 using a very strange shell, this has the same result as
7905 @w{@samp{$(wildcard *.c)}} (as long as at least one @samp{.c} file
7906 exists).@refill
7908 @node Guile Function,  , Shell Function, Functions
7909 @section The @code{guile} Function
7910 @findex guile
7911 @cindex Guile
7913 If GNU @code{make} is built with support for GNU Guile as an embedded
7914 extension language then the @code{guile} function will be available.
7915 The @code{guile} function takes one argument which is first expanded
7916 by @code{make} in the normal fashion, then passed to the GNU Guile
7917 evaluator.  The result of the evaluator is converted into a string and
7918 used as the expansion of the @code{guile} function in the makefile.
7919 See @ref{Guile Integration, ,GNU Guile Integration} for details on
7920 writing extensions to @code{make} in Guile.
7922 You can determine whether GNU Guile support is available by checking
7923 the @code{.FEATURES} variable for the word @var{guile}.
7925 @node Running, Implicit Rules, Functions, Top
7926 @chapter How to Run @code{make}
7928 A makefile that says how to recompile a program can be used in more
7929 than one way.  The simplest use is to recompile every file that is out
7930 of date.  Usually, makefiles are written so that if you run
7931 @code{make} with no arguments, it does just that.
7933 But you might want to update only some of the files; you might want to use
7934 a different compiler or different compiler options; you might want just to
7935 find out which files are out of date without changing them.
7937 By giving arguments when you run @code{make}, you can do any of these
7938 things and many others.
7940 @cindex exit status of make
7941 The exit status of @code{make} is always one of three values:
7942 @table @code
7943 @item 0
7944 The exit status is zero if @code{make} is successful.
7945 @item 2
7946 The exit status is two if @code{make} encounters any errors.
7947 It will print messages describing the particular errors.
7948 @item 1
7949 The exit status is one if you use the @samp{-q} flag and @code{make}
7950 determines that some target is not already up to date.
7951 @xref{Instead of Execution, ,Instead of Executing Recipes}.
7952 @end table
7954 @menu
7955 * Makefile Arguments::          How to specify which makefile to use.
7956 * Goals::                       How to use goal arguments to specify which
7957                                   parts of the makefile to use.
7958 * Instead of Execution::        How to use mode flags to specify what
7959                                   kind of thing to do with the recipes
7960                                   in the makefile other than simply
7961                                   execute them.
7962 * Avoiding Compilation::        How to avoid recompiling certain files.
7963 * Overriding::                  How to override a variable to specify
7964                                   an alternate compiler and other things.
7965 * Testing::                     How to proceed past some errors, to
7966                                   test compilation.
7967 * Options Summary::             Summary of Options
7968 @end menu
7970 @node Makefile Arguments, Goals, Running, Running
7971 @section Arguments to Specify the Makefile
7972 @cindex @code{--file}
7973 @cindex @code{--makefile}
7974 @cindex @code{-f}
7976 The way to specify the name of the makefile is with the @samp{-f} or
7977 @samp{--file} option (@samp{--makefile} also works).  For example,
7978 @samp{-f altmake} says to use the file @file{altmake} as the makefile.
7980 If you use the @samp{-f} flag several times and follow each @samp{-f}
7981 with an argument, all the specified files are used jointly as
7982 makefiles.
7984 If you do not use the @samp{-f} or @samp{--file} flag, the default is
7985 to try @file{GNUmakefile}, @file{makefile}, and @file{Makefile}, in
7986 that order, and use the first of these three which exists or can be made
7987 (@pxref{Makefiles, ,Writing Makefiles}).@refill
7989 @node Goals, Instead of Execution, Makefile Arguments, Running
7990 @section Arguments to Specify the Goals
7991 @cindex goal, how to specify
7993 The @dfn{goals} are the targets that @code{make} should strive ultimately
7994 to update.  Other targets are updated as well if they appear as
7995 prerequisites of goals, or prerequisites of prerequisites of goals, etc.
7997 By default, the goal is the first target in the makefile (not counting
7998 targets that start with a period).  Therefore, makefiles are usually
7999 written so that the first target is for compiling the entire program or
8000 programs they describe.  If the first rule in the makefile has several
8001 targets, only the first target in the rule becomes the default goal, not
8002 the whole list.  You can manage the selection of the default goal from
8003 within your makefile using the @code{.DEFAULT_GOAL} variable
8004 (@pxref{Special Variables, , Other Special Variables}).
8006 You can also specify a different goal or goals with command line
8007 arguments to @code{make}.  Use the name of the goal as an argument.
8008 If you specify several goals, @code{make} processes each of them in
8009 turn, in the order you name them.
8011 Any target in the makefile may be specified as a goal (unless it
8012 starts with @samp{-} or contains an @samp{=}, in which case it will be
8013 parsed as a switch or variable definition, respectively).  Even
8014 targets not in the makefile may be specified, if @code{make} can find
8015 implicit rules that say how to make them.
8017 @vindex MAKECMDGOALS
8018 @code{Make} will set the special variable @code{MAKECMDGOALS} to the
8019 list of goals you specified on the command line.  If no goals were given
8020 on the command line, this variable is empty.  Note that this variable
8021 should be used only in special circumstances.
8023 An example of appropriate use is to avoid including @file{.d} files
8024 during @code{clean} rules (@pxref{Automatic Prerequisites}), so
8025 @code{make} won't create them only to immediately remove them
8026 again:@refill
8028 @example
8029 @group
8030 sources = foo.c bar.c
8032 ifneq ($(MAKECMDGOALS),clean)
8033 include $(sources:.c=.d)
8034 endif
8035 @end group
8036 @end example
8038 One use of specifying a goal is if you want to compile only a part of
8039 the program, or only one of several programs.  Specify as a goal each
8040 file that you wish to remake.  For example, consider a directory containing
8041 several programs, with a makefile that starts like this:
8043 @example
8044 .PHONY: all
8045 all: size nm ld ar as
8046 @end example
8048 If you are working on the program @code{size}, you might want to say
8049 @w{@samp{make size}} so that only the files of that program are recompiled.
8051 Another use of specifying a goal is to make files that are not normally
8052 made.  For example, there may be a file of debugging output, or a
8053 version of the program that is compiled specially for testing, which has
8054 a rule in the makefile but is not a prerequisite of the default goal.
8056 Another use of specifying a goal is to run the recipe associated with
8057 a phony target (@pxref{Phony Targets}) or empty target (@pxref{Empty
8058 Targets, ,Empty Target Files to Record Events}).  Many makefiles contain
8059 a phony target named @file{clean} which deletes everything except source
8060 files.  Naturally, this is done only if you request it explicitly with
8061 @w{@samp{make clean}}.  Following is a list of typical phony and empty
8062 target names.  @xref{Standard Targets}, for a detailed list of all the
8063 standard target names which GNU software packages use.
8065 @table @file
8066 @item all
8067 @cindex @code{all} @r{(standard target)}
8068 Make all the top-level targets the makefile knows about.
8070 @item clean
8071 @cindex @code{clean} @r{(standard target)}
8072 Delete all files that are normally created by running @code{make}.
8074 @item mostlyclean
8075 @cindex @code{mostlyclean} @r{(standard target)}
8076 Like @samp{clean}, but may refrain from deleting a few files that people
8077 normally don't want to recompile.  For example, the @samp{mostlyclean}
8078 target for GCC does not delete @file{libgcc.a}, because recompiling it
8079 is rarely necessary and takes a lot of time.
8081 @item distclean
8082 @cindex @code{distclean} @r{(standard target)}
8083 @itemx realclean
8084 @cindex @code{realclean} @r{(standard target)}
8085 @itemx clobber
8086 @cindex @code{clobber} @r{(standard target)}
8087 Any of these targets might be defined to delete @emph{more} files than
8088 @samp{clean} does.  For example, this would delete configuration files
8089 or links that you would normally create as preparation for compilation,
8090 even if the makefile itself cannot create these files.
8092 @item install
8093 @cindex @code{install} @r{(standard target)}
8094 Copy the executable file into a directory that users typically search
8095 for commands; copy any auxiliary files that the executable uses into
8096 the directories where it will look for them.
8098 @item print
8099 @cindex @code{print} @r{(standard target)}
8100 Print listings of the source files that have changed.
8102 @item tar
8103 @cindex @code{tar} @r{(standard target)}
8104 Create a tar file of the source files.
8106 @item shar
8107 @cindex @code{shar} @r{(standard target)}
8108 Create a shell archive (shar file) of the source files.
8110 @item dist
8111 @cindex @code{dist} @r{(standard target)}
8112 Create a distribution file of the source files.  This might
8113 be a tar file, or a shar file, or a compressed version of one of the
8114 above, or even more than one of the above.
8116 @item TAGS
8117 @cindex @code{TAGS} @r{(standard target)}
8118 Update a tags table for this program.
8120 @item check
8121 @cindex @code{check} @r{(standard target)}
8122 @itemx test
8123 @cindex @code{test} @r{(standard target)}
8124 Perform self tests on the program this makefile builds.
8125 @end table
8127 @node Instead of Execution, Avoiding Compilation, Goals, Running
8128 @section Instead of Executing Recipes
8129 @cindex execution, instead of
8130 @cindex recipes, instead of executing
8132 The makefile tells @code{make} how to tell whether a target is up to date,
8133 and how to update each target.  But updating the targets is not always
8134 what you want.  Certain options specify other activities for @code{make}.
8136 @comment Extra blank lines make it print better.
8137 @table @samp
8138 @item -n
8139 @itemx --just-print
8140 @itemx --dry-run
8141 @itemx --recon
8142 @cindex @code{--just-print}
8143 @cindex @code{--dry-run}
8144 @cindex @code{--recon}
8145 @cindex @code{-n}
8147 ``No-op''.  Causes @code{make} to print the recipes that are needed to
8148 make the targets up to date, but not actually execute them.  Note that
8149 some recipes are still executed, even with this flag (@pxref{MAKE
8150 Variable, ,How the @code{MAKE} Variable Works}).  Also any recipes
8151 needed to update included makefiles are still executed
8152 (@pxref{Remaking Makefiles, ,How Makefiles Are Remade}).
8154 @item -t
8155 @itemx --touch
8156 @cindex @code{--touch}
8157 @cindex touching files
8158 @cindex target, touching
8159 @cindex @code{-t}
8161 ``Touch''.  Marks targets as up to date without actually changing
8162 them.  In other words, @code{make} pretends to update the targets but
8163 does not really change their contents; instead only their modified
8164 times are updated.
8166 @item -q
8167 @itemx --question
8168 @cindex @code{--question}
8169 @cindex @code{-q}
8170 @cindex question mode
8172 ``Question''.  Silently check whether the targets are up to date, but
8173 do not execute recipes; the exit code shows whether any updates are
8174 needed.
8176 @item -W @var{file}
8177 @itemx --what-if=@var{file}
8178 @itemx --assume-new=@var{file}
8179 @itemx --new-file=@var{file}
8180 @cindex @code{--what-if}
8181 @cindex @code{-W}
8182 @cindex @code{--assume-new}
8183 @cindex @code{--new-file}
8184 @cindex what if
8185 @cindex files, assuming new
8187 ``What if''.  Each @samp{-W} flag is followed by a file name.  The given
8188 files' modification times are recorded by @code{make} as being the present
8189 time, although the actual modification times remain the same.
8190 You can use the @samp{-W} flag in conjunction with the @samp{-n} flag
8191 to see what would happen if you were to modify specific files.@refill
8192 @end table
8194 With the @samp{-n} flag, @code{make} prints the recipe that it would
8195 normally execute but usually does not execute it.
8197 With the @samp{-t} flag, @code{make} ignores the recipes in the rules
8198 and uses (in effect) the command @code{touch} for each target that needs to
8199 be remade.  The @code{touch} command is also printed, unless @samp{-s} or
8200 @code{.SILENT} is used.  For speed, @code{make} does not actually invoke
8201 the program @code{touch}.  It does the work directly.
8203 With the @samp{-q} flag, @code{make} prints nothing and executes no
8204 recipes, but the exit status code it returns is zero if and only if the
8205 targets to be considered are already up to date.  If the exit status is
8206 one, then some updating needs to be done.  If @code{make} encounters an
8207 error, the exit status is two, so you can distinguish an error from a
8208 target that is not up to date.
8210 It is an error to use more than one of these three flags in the same
8211 invocation of @code{make}.
8213 @cindex +, and recipe execution
8214 The @samp{-n}, @samp{-t}, and @samp{-q} options do not affect recipe
8215 lines that begin with @samp{+} characters or contain the strings
8216 @samp{$(MAKE)} or @samp{$@{MAKE@}}.  Note that only the line containing
8217 the @samp{+} character or the strings @samp{$(MAKE)} or @samp{$@{MAKE@}}
8218 is run regardless of these options.  Other lines in the same rule are
8219 not run unless they too begin with @samp{+} or contain @samp{$(MAKE)} or
8220 @samp{$@{MAKE@}} (@xref{MAKE Variable, ,How the @code{MAKE} Variable Works}.)
8222 @cindex phony targets and recipe execution
8223 The @samp{-t} flag prevents phony targets (@pxref{Phony Targets}) from
8224 being updated, unless there are recipe lines beginning with @samp{+}
8225 or containing @samp{$(MAKE)} or @samp{$@{MAKE@}}.
8227 The @samp{-W} flag provides two features:
8229 @itemize @bullet
8230 @item
8231 If you also use the @samp{-n} or @samp{-q} flag, you can see what
8232 @code{make} would do if you were to modify some files.
8234 @item
8235 Without the @samp{-n} or @samp{-q} flag, when @code{make} is actually
8236 executing recipes, the @samp{-W} flag can direct @code{make} to act as
8237 if some files had been modified, without actually running the recipes
8238 for those files.@refill
8239 @end itemize
8241 Note that the options @samp{-p} and @samp{-v} allow you to obtain other
8242 information about @code{make} or about the makefiles in use
8243 (@pxref{Options Summary, ,Summary of Options}).@refill
8245 @node Avoiding Compilation, Overriding, Instead of Execution, Running
8246 @section Avoiding Recompilation of Some Files
8247 @cindex @code{-o}
8248 @cindex @code{--old-file}
8249 @cindex @code{--assume-old}
8250 @cindex files, assuming old
8251 @cindex files, avoiding recompilation of
8252 @cindex recompilation, avoiding
8254 Sometimes you may have changed a source file but you do not want to
8255 recompile all the files that depend on it.  For example, suppose you add
8256 a macro or a declaration to a header file that many other files depend
8257 on.  Being conservative, @code{make} assumes that any change in the
8258 header file requires recompilation of all dependent files, but you know
8259 that they do not need to be recompiled and you would rather not waste
8260 the time waiting for them to compile.
8262 If you anticipate the problem before changing the header file, you can
8263 use the @samp{-t} flag.  This flag tells @code{make} not to run the
8264 recipes in the rules, but rather to mark the target up to date by
8265 changing its last-modification date.  You would follow this procedure:
8267 @enumerate
8268 @item
8269 Use the command @samp{make} to recompile the source files that really
8270 need recompilation, ensuring that the object files are up-to-date
8271 before you begin.
8273 @item
8274 Make the changes in the header files.
8276 @item
8277 Use the command @samp{make -t} to mark all the object files as
8278 up to date.  The next time you run @code{make}, the changes in the
8279 header files will not cause any recompilation.
8280 @end enumerate
8282 If you have already changed the header file at a time when some files
8283 do need recompilation, it is too late to do this.  Instead, you can
8284 use the @w{@samp{-o @var{file}}} flag, which marks a specified file as
8285 ``old'' (@pxref{Options Summary, ,Summary of Options}).  This means
8286 that the file itself will not be remade, and nothing else will be
8287 remade on its account.  Follow this procedure:
8289 @enumerate
8290 @item
8291 Recompile the source files that need compilation for reasons independent
8292 of the particular header file, with @samp{make -o @var{headerfile}}.
8293 If several header files are involved, use a separate @samp{-o} option
8294 for each header file.
8296 @item
8297 Touch all the object files with @samp{make -t}.
8298 @end enumerate
8300 @node Overriding, Testing, Avoiding Compilation, Running
8301 @section Overriding Variables
8302 @cindex overriding variables with arguments
8303 @cindex variables, overriding with arguments
8304 @cindex command line variables
8305 @cindex variables, command line
8307 An argument that contains @samp{=} specifies the value of a variable:
8308 @samp{@var{v}=@var{x}} sets the value of the variable @var{v} to @var{x}.
8309 If you specify a value in this way, all ordinary assignments of the same
8310 variable in the makefile are ignored; we say they have been
8311 @dfn{overridden} by the command line argument.
8313 The most common way to use this facility is to pass extra flags to
8314 compilers.  For example, in a properly written makefile, the variable
8315 @code{CFLAGS} is included in each recipe that runs the C compiler, so a
8316 file @file{foo.c} would be compiled something like this:
8318 @example
8319 cc -c $(CFLAGS) foo.c
8320 @end example
8322 Thus, whatever value you set for @code{CFLAGS} affects each compilation
8323 that occurs.  The makefile probably specifies the usual value for
8324 @code{CFLAGS}, like this:
8326 @example
8327 CFLAGS=-g
8328 @end example
8330 Each time you run @code{make}, you can override this value if you
8331 wish.  For example, if you say @samp{make CFLAGS='-g -O'}, each C
8332 compilation will be done with @samp{cc -c -g -O}.  (This also
8333 illustrates how you can use quoting in the shell to enclose spaces and
8334 other special characters in the value of a variable when you override
8335 it.)
8337 The variable @code{CFLAGS} is only one of many standard variables that
8338 exist just so that you can change them this way.  @xref{Implicit
8339 Variables, , Variables Used by Implicit Rules}, for a complete list.
8341 You can also program the makefile to look at additional variables of your
8342 own, giving the user the ability to control other aspects of how the
8343 makefile works by changing the variables.
8345 When you override a variable with a command line argument, you can
8346 define either a recursively-expanded variable or a simply-expanded
8347 variable.  The examples shown above make a recursively-expanded
8348 variable; to make a simply-expanded variable, write @samp{:=} or
8349 @samp{::=} instead of @samp{=}.  But, unless you want to include a
8350 variable reference or function call in the @emph{value} that you
8351 specify, it makes no difference which kind of variable you create.
8353 There is one way that the makefile can change a variable that you have
8354 overridden.  This is to use the @code{override} directive, which is a line
8355 that looks like this: @samp{override @var{variable} = @var{value}}
8356 (@pxref{Override Directive, ,The @code{override} Directive}).
8358 @node Testing, Options Summary, Overriding, Running
8359 @section Testing the Compilation of a Program
8360 @cindex testing compilation
8361 @cindex compilation, testing
8363 Normally, when an error happens in executing a shell command, @code{make}
8364 gives up immediately, returning a nonzero status.  No further recipes are
8365 executed for any target.  The error implies that the goal cannot be
8366 correctly remade, and @code{make} reports this as soon as it knows.
8368 When you are compiling a program that you have just changed, this is not
8369 what you want.  Instead, you would rather that @code{make} try compiling
8370 every file that can be tried, to show you as many compilation errors
8371 as possible.
8373 @cindex @code{-k}
8374 @cindex @code{--keep-going}
8375 On these occasions, you should use the @samp{-k} or
8376 @samp{--keep-going} flag.  This tells @code{make} to continue to
8377 consider the other prerequisites of the pending targets, remaking them
8378 if necessary, before it gives up and returns nonzero status.  For
8379 example, after an error in compiling one object file, @samp{make -k}
8380 will continue compiling other object files even though it already
8381 knows that linking them will be impossible.  In addition to continuing
8382 after failed shell commands, @samp{make -k} will continue as much as
8383 possible after discovering that it does not know how to make a target
8384 or prerequisite file.  This will always cause an error message, but
8385 without @samp{-k}, it is a fatal error (@pxref{Options Summary,
8386 ,Summary of Options}).@refill
8388 The usual behavior of @code{make} assumes that your purpose is to get the
8389 goals up to date; once @code{make} learns that this is impossible, it might
8390 as well report the failure immediately.  The @samp{-k} flag says that the
8391 real purpose is to test as much as possible of the changes made in the
8392 program, perhaps to find several independent problems so that you can
8393 correct them all before the next attempt to compile.  This is why Emacs'
8394 @kbd{M-x compile} command passes the @samp{-k} flag by default.
8396 @node Options Summary,  , Testing, Running
8397 @section Summary of Options
8398 @cindex options
8399 @cindex flags
8400 @cindex switches
8402 Here is a table of all the options @code{make} understands:
8404 @table @samp
8405 @item -b
8406 @cindex @code{-b}
8407 @itemx -m
8408 @cindex @code{-m}
8409 These options are ignored for compatibility with other versions of @code{make}.
8411 @item -B
8412 @cindex @code{-B}
8413 @itemx --always-make
8414 @cindex @code{--always-make}
8415 Consider all targets out-of-date.  GNU @code{make} proceeds to
8416 consider targets and their prerequisites using the normal algorithms;
8417 however, all targets so considered are always remade regardless of the
8418 status of their prerequisites.  To avoid infinite recursion, if
8419 @code{MAKE_RESTARTS} (@pxref{Special Variables, , Other Special
8420 Variables}) is set to a number greater than 0 this option is disabled
8421 when considering whether to remake makefiles (@pxref{Remaking
8422 Makefiles, , How Makefiles Are Remade}).
8424 @item -C @var{dir}
8425 @cindex @code{-C}
8426 @itemx --directory=@var{dir}
8427 @cindex @code{--directory}
8428 Change to directory @var{dir} before reading the makefiles.  If multiple
8429 @samp{-C} options are specified, each is interpreted relative to the
8430 previous one: @samp{-C / -C etc} is equivalent to @samp{-C /etc}.
8431 This is typically used with recursive invocations of @code{make}
8432 (@pxref{Recursion, ,Recursive Use of @code{make}}).
8434 @item -d
8435 @cindex @code{-d}
8436 @c Extra blank line here makes the table look better.
8438 Print debugging information in addition to normal processing.  The
8439 debugging information says which files are being considered for
8440 remaking, which file-times are being compared and with what results,
8441 which files actually need to be remade, which implicit rules are
8442 considered and which are applied---everything interesting about how
8443 @code{make} decides what to do.  The @code{-d} option is equivalent to
8444 @samp{--debug=a} (see below).
8446 @item --debug[=@var{options}]
8447 @cindex @code{--debug}
8448 @c Extra blank line here makes the table look better.
8450 Print debugging information in addition to normal processing.  Various
8451 levels and types of output can be chosen.  With no arguments, print the
8452 ``basic'' level of debugging.  Possible arguments are below; only the
8453 first character is considered, and values must be comma- or
8454 space-separated.
8456 @table @code
8457 @item a (@i{all})
8458 All types of debugging output are enabled.  This is equivalent to using
8459 @samp{-d}.
8461 @item b (@i{basic})
8462 Basic debugging prints each target that was found to be out-of-date, and
8463 whether the build was successful or not.
8465 @item v (@i{verbose})
8466 A level above @samp{basic}; includes messages about which makefiles were
8467 parsed, prerequisites that did not need to be rebuilt, etc.  This option
8468 also enables @samp{basic} messages.
8470 @item i (@i{implicit})
8471 Prints messages describing the implicit rule searches for each target.
8472 This option also enables @samp{basic} messages.
8474 @item j (@i{jobs})
8475 Prints messages giving details on the invocation of specific sub-commands.
8477 @item m (@i{makefile})
8478 By default, the above messages are not enabled while trying to remake
8479 the makefiles.  This option enables messages while rebuilding makefiles,
8480 too.  Note that the @samp{all} option does enable this option.  This
8481 option also enables @samp{basic} messages.
8482 @end table
8484 @item -e
8485 @cindex @code{-e}
8486 @itemx --environment-overrides
8487 @cindex @code{--environment-overrides}
8488 Give variables taken from the environment precedence
8489 over variables from makefiles.
8490 @xref{Environment, ,Variables from the Environment}.
8492 @item --eval=@var{string}
8493 @cindex @code{--eval}
8494 @c Extra blank line here makes the table look better.
8496 Evaluate @var{string} as makefile syntax.  This is a command-line
8497 version of the @code{eval} function (@pxref{Eval Function}).  The
8498 evaluation is performed after the default rules and variables have
8499 been defined, but before any makefiles are read.
8501 @item -f @var{file}
8502 @cindex @code{-f}
8503 @itemx --file=@var{file}
8504 @cindex @code{--file}
8505 @itemx --makefile=@var{file}
8506 @cindex @code{--makefile}
8507 Read the file named @var{file} as a makefile.
8508 @xref{Makefiles, ,Writing Makefiles}.
8510 @item -h
8511 @cindex @code{-h}
8512 @itemx --help
8513 @cindex @code{--help}
8514 @c Extra blank line here makes the table look better.
8516 Remind you of the options that @code{make} understands and then exit.
8518 @item -i
8519 @cindex @code{-i}
8520 @itemx --ignore-errors
8521 @cindex @code{--ignore-errors}
8522 Ignore all errors in recipes executed to remake files.
8523 @xref{Errors, ,Errors in Recipes}.
8525 @item -I @var{dir}
8526 @cindex @code{-I}
8527 @itemx --include-dir=@var{dir}
8528 @cindex @code{--include-dir}
8529 Specifies a directory @var{dir} to search for included makefiles.
8530 @xref{Include, ,Including Other Makefiles}.  If several @samp{-I}
8531 options are used to specify several directories, the directories are
8532 searched in the order specified.
8534 @item -j [@var{jobs}]
8535 @cindex @code{-j}
8536 @itemx --jobs[=@var{jobs}]
8537 @cindex @code{--jobs}
8538 Specifies the number of recipes (jobs) to run simultaneously.  With no
8539 argument, @code{make} runs as many recipes simultaneously as possible.
8540 If there is more than one @samp{-j} option, the last one is effective.
8541 @xref{Parallel, ,Parallel Execution}, for more information on how
8542 recipes are run.  Note that this option is ignored on MS-DOS.
8544 @item -k
8545 @cindex @code{-k}
8546 @itemx --keep-going
8547 @cindex @code{--keep-going}
8548 Continue as much as possible after an error.  While the target that
8549 failed, and those that depend on it, cannot be remade, the other
8550 prerequisites of these targets can be processed all the same.
8551 @xref{Testing, ,Testing the Compilation of a Program}.
8553 @item -l [@var{load}]
8554 @cindex @code{-l}
8555 @itemx --load-average[=@var{load}]
8556 @cindex @code{--load-average}
8557 @itemx --max-load[=@var{load}]
8558 @cindex @code{--max-load}
8559 Specifies that no new recipes should be started if there are other
8560 recipes running and the load average is at least @var{load} (a
8561 floating-point number).  With no argument, removes a previous load
8562 limit.  @xref{Parallel, ,Parallel Execution}.
8564 @item -L
8565 @cindex @code{-L}
8566 @itemx --check-symlink-times
8567 @cindex @code{--check-symlink-times}
8568 On systems that support symbolic links, this option causes @code{make}
8569 to consider the timestamps on any symbolic links in addition to the
8570 timestamp on the file referenced by those links.  When this option is
8571 provided, the most recent timestamp among the file and the symbolic
8572 links is taken as the modification time for this target file.
8574 @item -n
8575 @cindex @code{-n}
8576 @itemx --just-print
8577 @cindex @code{--just-print}
8578 @itemx --dry-run
8579 @cindex @code{--dry-run}
8580 @itemx --recon
8581 @cindex @code{--recon}
8582 @c Extra blank line here makes the table look better.
8584 Print the recipe that would be executed, but do not execute it (except
8585 in certain circumstances).
8586 @xref{Instead of Execution, ,Instead of Executing Recipes}.
8588 @item -o @var{file}
8589 @cindex @code{-o}
8590 @itemx --old-file=@var{file}
8591 @cindex @code{--old-file}
8592 @itemx --assume-old=@var{file}
8593 @cindex @code{--assume-old}
8594 Do not remake the file @var{file} even if it is older than its
8595 prerequisites, and do not remake anything on account of changes in
8596 @var{file}.  Essentially the file is treated as very old and its rules
8597 are ignored.  @xref{Avoiding Compilation, ,Avoiding Recompilation of
8598 Some Files}.@refill
8600 @item -p
8601 @cindex @code{-p}
8602 @itemx --print-data-base
8603 @cindex @code{--print-data-base}
8604 @cindex data base of @code{make} rules
8605 @cindex predefined rules and variables, printing
8606 Print the data base (rules and variable values) that results from
8607 reading the makefiles; then execute as usual or as otherwise
8608 specified.  This also prints the version information given by the
8609 @samp{-v} switch (see below).  To print the data base without trying
8610 to remake any files, use @w{@samp{make -qp}}.  To print the data base
8611 of predefined rules and variables, use @w{@samp{make -p -f /dev/null}}.
8612 The data base output contains file name and line number information for
8613 recipe and variable definitions, so it can be a useful debugging tool
8614 in complex environments.
8616 @item -q
8617 @cindex @code{-q}
8618 @itemx --question
8619 @cindex @code{--question}
8620 ``Question mode''.  Do not run any recipes, or print anything; just
8621 return an exit status that is zero if the specified targets are already
8622 up to date, one if any remaking is required, or two if an error is
8623 encountered.  @xref{Instead of Execution, ,Instead of Executing
8624 Recipes}.@refill
8626 @item -r
8627 @cindex @code{-r}
8628 @itemx --no-builtin-rules
8629 @cindex @code{--no-builtin-rules}
8630 Eliminate use of the built-in implicit rules (@pxref{Implicit Rules,
8631 ,Using Implicit Rules}).  You can still define your own by writing
8632 pattern rules (@pxref{Pattern Rules, ,Defining and Redefining Pattern
8633 Rules}).  The @samp{-r} option also clears out the default list of
8634 suffixes for suffix rules (@pxref{Suffix Rules, ,Old-Fashioned Suffix
8635 Rules}).  But you can still define your own suffixes with a rule for
8636 @code{.SUFFIXES}, and then define your own suffix rules.  Note that only
8637 @emph{rules} are affected by the @code{-r} option; default variables
8638 remain in effect (@pxref{Implicit Variables, ,Variables Used by Implicit
8639 Rules}); see the @samp{-R} option below.
8641 @item -R
8642 @cindex @code{-R}
8643 @itemx --no-builtin-variables
8644 @cindex @code{--no-builtin-variables}
8645 Eliminate use of the built-in rule-specific variables (@pxref{Implicit
8646 Variables, ,Variables Used by Implicit Rules}).  You can still define
8647 your own, of course.  The @samp{-R} option also automatically enables
8648 the @samp{-r} option (see above), since it doesn't make sense to have
8649 implicit rules without any definitions for the variables that they use.
8651 @item -s
8652 @cindex @code{-s}
8653 @itemx --silent
8654 @cindex @code{--silent}
8655 @itemx --quiet
8656 @cindex @code{--quiet}
8657 @c Extra blank line here makes the table look better.
8659 Silent operation; do not print the recipes as they are executed.
8660 @xref{Echoing, ,Recipe Echoing}.
8662 @item -S
8663 @cindex @code{-S}
8664 @itemx --no-keep-going
8665 @cindex @code{--no-keep-going}
8666 @itemx --stop
8667 @cindex @code{--stop}
8668 @c Extra blank line here makes the table look better.
8670 Cancel the effect of the @samp{-k} option.  This is never necessary
8671 except in a recursive @code{make} where @samp{-k} might be inherited
8672 from the top-level @code{make} via @code{MAKEFLAGS}
8673 (@pxref{Recursion, ,Recursive Use of @code{make}})
8674 or if you set @samp{-k} in @code{MAKEFLAGS} in your environment.@refill
8676 @item -t
8677 @cindex @code{-t}
8678 @itemx --touch
8679 @cindex @code{--touch}
8680 @c Extra blank line here makes the table look better.
8682 Touch files (mark them up to date without really changing them)
8683 instead of running their recipes.  This is used to pretend that the
8684 recipes were done, in order to fool future invocations of
8685 @code{make}.  @xref{Instead of Execution, ,Instead of Executing Recipes}.
8687 @item --trace
8688 @cindex @code{--trace}
8689 @c Extra blank line here makes the table look better.
8691 Print the entire recipe to be executed, even for recipes that are
8692 normally silent (due to @code{.SILENT} or @samp{@@}).  Also print the
8693 makefile name and line number where the recipe was defined.
8695 @item -v
8696 @cindex @code{-v}
8697 @itemx --version
8698 @cindex @code{--version}
8699 Print the version of the @code{make} program plus a copyright, a list
8700 of authors, and a notice that there is no warranty; then exit.
8702 @item -w
8703 @cindex @code{-w}
8704 @itemx --print-directory
8705 @cindex @code{--print-directory}
8706 Print a message containing the working directory both before and after
8707 executing the makefile.  This may be useful for tracking down errors
8708 from complicated nests of recursive @code{make} commands.
8709 @xref{Recursion, ,Recursive Use of @code{make}}.  (In practice, you
8710 rarely need to specify this option since @samp{make} does it for you;
8711 see @ref{-w Option, ,The @samp{--print-directory} Option}.)
8713 @itemx --no-print-directory
8714 @cindex @code{--no-print-directory}
8715 Disable printing of the working directory under @code{-w}.
8716 This option is useful when @code{-w} is turned on automatically,
8717 but you do not want to see the extra messages.
8718 @xref{-w Option, ,The @samp{--print-directory} Option}.
8720 @item -W @var{file}
8721 @cindex @code{-W}
8722 @itemx --what-if=@var{file}
8723 @cindex @code{--what-if}
8724 @itemx --new-file=@var{file}
8725 @cindex @code{--new-file}
8726 @itemx --assume-new=@var{file}
8727 @cindex @code{--assume-new}
8728 Pretend that the target @var{file} has just been modified.  When used
8729 with the @samp{-n} flag, this shows you what would happen if you were
8730 to modify that file.  Without @samp{-n}, it is almost the same as
8731 running a @code{touch} command on the given file before running
8732 @code{make}, except that the modification time is changed only in the
8733 imagination of @code{make}.
8734 @xref{Instead of Execution, ,Instead of Executing Recipes}.
8736 @item --warn-undefined-variables
8737 @cindex @code{--warn-undefined-variables}
8738 @cindex variables, warning for undefined
8739 @cindex undefined variables, warning message
8740 Issue a warning message whenever @code{make} sees a reference to an
8741 undefined variable.  This can be helpful when you are trying to debug
8742 makefiles which use variables in complex ways.
8743 @end table
8745 @node Implicit Rules, Archives, Running, Top
8746 @chapter Using Implicit Rules
8747 @cindex implicit rule
8748 @cindex rule, implicit
8750 Certain standard ways of remaking target files are used very often.  For
8751 example, one customary way to make an object file is from a C source file
8752 using the C compiler, @code{cc}.
8754 @dfn{Implicit rules} tell @code{make} how to use customary techniques so
8755 that you do not have to specify them in detail when you want to use
8756 them.  For example, there is an implicit rule for C compilation.  File
8757 names determine which implicit rules are run.  For example, C
8758 compilation typically takes a @file{.c} file and makes a @file{.o} file.
8759 So @code{make} applies the implicit rule for C compilation when it sees
8760 this combination of file name endings.@refill
8762 A chain of implicit rules can apply in sequence; for example, @code{make}
8763 will remake a @file{.o} file from a @file{.y} file by way of a @file{.c} file.
8764 @iftex
8765 @xref{Chained Rules, ,Chains of Implicit Rules}.
8766 @end iftex
8768 The built-in implicit rules use several variables in their recipes so
8769 that, by changing the values of the variables, you can change the way the
8770 implicit rule works.  For example, the variable @code{CFLAGS} controls the
8771 flags given to the C compiler by the implicit rule for C compilation.
8772 @iftex
8773 @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
8774 @end iftex
8776 You can define your own implicit rules by writing @dfn{pattern rules}.
8777 @iftex
8778 @xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.
8779 @end iftex
8781 @dfn{Suffix rules} are a more limited way to define implicit rules.
8782 Pattern rules are more general and clearer, but suffix rules are
8783 retained for compatibility.
8784 @iftex
8785 @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
8786 @end iftex
8788 @menu
8789 * Using Implicit::              How to use an existing implicit rule
8790                                   to get the recipes for updating a file.
8791 * Catalogue of Rules::          A list of built-in implicit rules.
8792 * Implicit Variables::          How to change what predefined rules do.
8793 * Chained Rules::               How to use a chain of implicit rules.
8794 * Pattern Rules::               How to define new implicit rules.
8795 * Last Resort::                 How to define a recipe for rules which
8796                                   cannot find any.
8797 * Suffix Rules::                The old-fashioned style of implicit rule.
8798 * Implicit Rule Search::        The precise algorithm for applying
8799                                   implicit rules.
8800 @end menu
8802 @node Using Implicit, Catalogue of Rules, Implicit Rules, Implicit Rules
8803 @section Using Implicit Rules
8804 @cindex implicit rule, how to use
8805 @cindex rule, implicit, how to use
8807 To allow @code{make} to find a customary method for updating a target
8808 file, all you have to do is refrain from specifying recipes yourself.
8809 Either write a rule with no recipe, or don't write a rule at all.
8810 Then @code{make} will figure out which implicit rule to use based on
8811 which kind of source file exists or can be made.
8813 For example, suppose the makefile looks like this:
8815 @example
8816 foo : foo.o bar.o
8817         cc -o foo foo.o bar.o $(CFLAGS) $(LDFLAGS)
8818 @end example
8820 @noindent
8821 Because you mention @file{foo.o} but do not give a rule for it, @code{make}
8822 will automatically look for an implicit rule that tells how to update it.
8823 This happens whether or not the file @file{foo.o} currently exists.
8825 If an implicit rule is found, it can supply both a recipe and one or
8826 more prerequisites (the source files).  You would want to write a rule
8827 for @file{foo.o} with no recipe if you need to specify additional
8828 prerequisites, such as header files, that the implicit rule cannot
8829 supply.
8831 Each implicit rule has a target pattern and prerequisite patterns.  There may
8832 be many implicit rules with the same target pattern.  For example, numerous
8833 rules make @samp{.o} files: one, from a @samp{.c} file with the C compiler;
8834 another, from a @samp{.p} file with the Pascal compiler; and so on.  The rule
8835 that actually applies is the one whose prerequisites exist or can be made.
8836 So, if you have a file @file{foo.c}, @code{make} will run the C compiler;
8837 otherwise, if you have a file @file{foo.p}, @code{make} will run the Pascal
8838 compiler; and so on.
8840 Of course, when you write the makefile, you know which implicit rule you
8841 want @code{make} to use, and you know it will choose that one because you
8842 know which possible prerequisite files are supposed to exist.
8843 @xref{Catalogue of Rules, ,Catalogue of Implicit Rules},
8844 for a catalogue of all the predefined implicit rules.
8846 Above, we said an implicit rule applies if the required prerequisites ``exist
8847 or can be made''.  A file ``can be made'' if it is mentioned explicitly in
8848 the makefile as a target or a prerequisite, or if an implicit rule can be
8849 recursively found for how to make it.  When an implicit prerequisite is the
8850 result of another implicit rule, we say that @dfn{chaining} is occurring.
8851 @xref{Chained Rules, ,Chains of Implicit Rules}.
8853 In general, @code{make} searches for an implicit rule for each target, and
8854 for each double-colon rule, that has no recipe.  A file that is mentioned
8855 only as a prerequisite is considered a target whose rule specifies nothing,
8856 so implicit rule search happens for it.  @xref{Implicit Rule Search, ,Implicit Rule Search Algorithm}, for the
8857 details of how the search is done.
8859 Note that explicit prerequisites do not influence implicit rule search.
8860 For example, consider this explicit rule:
8862 @example
8863 foo.o: foo.p
8864 @end example
8866 @noindent
8867 The prerequisite on @file{foo.p} does not necessarily mean that
8868 @code{make} will remake @file{foo.o} according to the implicit rule to
8869 make an object file, a @file{.o} file, from a Pascal source file, a
8870 @file{.p} file.  For example, if @file{foo.c} also exists, the implicit
8871 rule to make an object file from a C source file is used instead,
8872 because it appears before the Pascal rule in the list of predefined
8873 implicit rules (@pxref{Catalogue of Rules, , Catalogue of Implicit
8874 Rules}).
8876 If you do not want an implicit rule to be used for a target that has no
8877 recipe, you can give that target an empty recipe by writing a semicolon
8878 (@pxref{Empty Recipes, ,Defining Empty Recipes}).
8880 @node Catalogue of Rules, Implicit Variables, Using Implicit, Implicit Rules
8881 @section Catalogue of Implicit Rules
8882 @cindex implicit rule, predefined
8883 @cindex rule, implicit, predefined
8885 Here is a catalogue of predefined implicit rules which are always
8886 available unless the makefile explicitly overrides or cancels them.
8887 @xref{Canceling Rules, ,Canceling Implicit Rules}, for information on
8888 canceling or overriding an implicit rule.  The @samp{-r} or
8889 @samp{--no-builtin-rules} option cancels all predefined rules.
8891 This manual only documents the default rules available on POSIX-based
8892 operating systems.  Other operating systems, such as VMS, Windows,
8893 OS/2, etc. may have different sets of default rules.  To see the full
8894 list of default rules and variables available in your version of GNU
8895 @code{make}, run @samp{make -p} in a directory with no makefile.
8897 Not all of these rules will always be defined, even when the @samp{-r}
8898 option is not given.  Many of the predefined implicit rules are
8899 implemented in @code{make} as suffix rules, so which ones will be
8900 defined depends on the @dfn{suffix list} (the list of prerequisites of
8901 the special target @code{.SUFFIXES}).  The default suffix list is:
8902 @code{.out}, @code{.a}, @code{.ln}, @code{.o}, @code{.c}, @code{.cc},
8903 @code{.C}, @code{.cpp}, @code{.p}, @code{.f}, @code{.F}, @code{.m},
8904 @code{.r}, @code{.y}, @code{.l}, @code{.ym}, @code{.lm}, @code{.s},
8905 @code{.S}, @code{.mod}, @code{.sym}, @code{.def}, @code{.h},
8906 @code{.info}, @code{.dvi}, @code{.tex}, @code{.texinfo}, @code{.texi},
8907 @code{.txinfo}, @code{.w}, @code{.ch} @code{.web}, @code{.sh},
8908 @code{.elc}, @code{.el}.  All of the implicit rules described below
8909 whose prerequisites have one of these suffixes are actually suffix
8910 rules.  If you modify the suffix list, the only predefined suffix
8911 rules in effect will be those named by one or two of the suffixes that
8912 are on the list you specify; rules whose suffixes fail to be on the
8913 list are disabled.  @xref{Suffix Rules, ,Old-Fashioned Suffix Rules},
8914 for full details on suffix rules.
8916 @table @asis
8917 @item Compiling C programs
8918 @cindex C, rule to compile
8919 @pindex cc
8920 @pindex gcc
8921 @pindex .o
8922 @pindex .c
8923 @file{@var{n}.o} is made automatically from @file{@var{n}.c} with
8924 a recipe of the form @samp{$(CC) $(CPPFLAGS) $(CFLAGS) -c}.@refill
8926 @item Compiling C++ programs
8927 @cindex C++, rule to compile
8928 @pindex g++
8929 @pindex .cc
8930 @pindex .cpp
8931 @pindex .C
8932 @file{@var{n}.o} is made automatically from @file{@var{n}.cc},
8933 @file{@var{n}.cpp}, or @file{@var{n}.C} with a recipe of the form
8934 @samp{$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c}.  We encourage you to use the
8935 suffix @samp{.cc} for C++ source files instead of @samp{.C}.@refill
8937 @item Compiling Pascal programs
8938 @cindex Pascal, rule to compile
8939 @pindex pc
8940 @pindex .p
8941 @file{@var{n}.o} is made automatically from @file{@var{n}.p}
8942 with the recipe @samp{$(PC) $(PFLAGS) -c}.@refill
8944 @item Compiling Fortran and Ratfor programs
8945 @cindex Fortran, rule to compile
8946 @cindex Ratfor, rule to compile
8947 @pindex f77
8948 @pindex .f
8949 @pindex .r
8950 @pindex .F
8951 @file{@var{n}.o} is made automatically from @file{@var{n}.r},
8952 @file{@var{n}.F} or @file{@var{n}.f} by running the
8953 Fortran compiler.  The precise recipe used is as follows:@refill
8955 @table @samp
8956 @item .f
8957 @samp{$(FC) $(FFLAGS) -c}.
8958 @item .F
8959 @samp{$(FC) $(FFLAGS) $(CPPFLAGS) -c}.
8960 @item .r
8961 @samp{$(FC) $(FFLAGS) $(RFLAGS) -c}.
8962 @end table
8964 @item Preprocessing Fortran and Ratfor programs
8965 @file{@var{n}.f} is made automatically from @file{@var{n}.r} or
8966 @file{@var{n}.F}.  This rule runs just the preprocessor to convert a
8967 Ratfor or preprocessable Fortran program into a strict Fortran
8968 program.  The precise recipe used is as follows:@refill
8970 @table @samp
8971 @item .F
8972 @samp{$(FC) $(CPPFLAGS) $(FFLAGS) -F}.
8973 @item .r
8974 @samp{$(FC) $(FFLAGS) $(RFLAGS) -F}.
8975 @end table
8977 @item Compiling Modula-2 programs
8978 @cindex Modula-2, rule to compile
8979 @pindex m2c
8980 @pindex .sym
8981 @pindex .def
8982 @pindex .mod
8983 @file{@var{n}.sym} is made from @file{@var{n}.def} with a recipe
8984 of the form @samp{$(M2C) $(M2FLAGS) $(DEFFLAGS)}.  @file{@var{n}.o}
8985 is made from @file{@var{n}.mod}; the form is:
8986 @w{@samp{$(M2C) $(M2FLAGS) $(MODFLAGS)}}.@refill
8988 @need 1200
8989 @item Assembling and preprocessing assembler programs
8990 @cindex assembly, rule to compile
8991 @pindex as
8992 @pindex .s
8993 @file{@var{n}.o} is made automatically from @file{@var{n}.s} by
8994 running the assembler, @code{as}.  The precise recipe is
8995 @samp{$(AS) $(ASFLAGS)}.@refill
8997 @pindex .S
8998 @file{@var{n}.s} is made automatically from @file{@var{n}.S} by
8999 running the C preprocessor, @code{cpp}.  The precise recipe is
9000 @w{@samp{$(CPP) $(CPPFLAGS)}}.
9002 @item Linking a single object file
9003 @cindex linking, predefined rule for
9004 @pindex ld
9005 @pindex .o
9006 @file{@var{n}} is made automatically from @file{@var{n}.o} by running
9007 the linker (usually called @code{ld}) via the C compiler.  The precise
9008 recipe used is @w{@samp{$(CC) $(LDFLAGS) @var{n}.o $(LOADLIBES) $(LDLIBS)}}.
9010 This rule does the right thing for a simple program with only one
9011 source file.  It will also do the right thing if there are multiple
9012 object files (presumably coming from various other source files), one
9013 of which has a name matching that of the executable file.  Thus,
9015 @example
9016 x: y.o z.o
9017 @end example
9019 @noindent
9020 when @file{x.c}, @file{y.c} and @file{z.c} all exist will execute:
9022 @example
9023 @group
9024 cc -c x.c -o x.o
9025 cc -c y.c -o y.o
9026 cc -c z.c -o z.o
9027 cc x.o y.o z.o -o x
9028 rm -f x.o
9029 rm -f y.o
9030 rm -f z.o
9031 @end group
9032 @end example
9034 @noindent
9035 In more complicated cases, such as when there is no object file whose
9036 name derives from the executable file name, you must write an explicit
9037 recipe for linking.
9039 Each kind of file automatically made into @samp{.o} object files will
9040 be automatically linked by using the compiler (@samp{$(CC)},
9041 @samp{$(FC)} or @samp{$(PC)}; the C compiler @samp{$(CC)} is used to
9042 assemble @samp{.s} files) without the @samp{-c} option.  This could be
9043 done by using the @samp{.o} object files as intermediates, but it is
9044 faster to do the compiling and linking in one step, so that's how it's
9045 done.@refill
9047 @item Yacc for C programs
9048 @pindex yacc
9049 @cindex Yacc, rule to run
9050 @pindex .y
9051 @file{@var{n}.c} is made automatically from @file{@var{n}.y} by
9052 running Yacc with the recipe @samp{$(YACC) $(YFLAGS)}.
9054 @item Lex for C programs
9055 @pindex lex
9056 @cindex Lex, rule to run
9057 @pindex .l
9058 @file{@var{n}.c} is made automatically from @file{@var{n}.l} by
9059 running Lex.  The actual recipe is @samp{$(LEX) $(LFLAGS)}.
9061 @item Lex for Ratfor programs
9062 @file{@var{n}.r} is made automatically from @file{@var{n}.l} by
9063 running Lex.  The actual recipe is @samp{$(LEX) $(LFLAGS)}.
9065 The convention of using the same suffix @samp{.l} for all Lex files
9066 regardless of whether they produce C code or Ratfor code makes it
9067 impossible for @code{make} to determine automatically which of the two
9068 languages you are using in any particular case.  If @code{make} is
9069 called upon to remake an object file from a @samp{.l} file, it must
9070 guess which compiler to use.  It will guess the C compiler, because
9071 that is more common.  If you are using Ratfor, make sure @code{make}
9072 knows this by mentioning @file{@var{n}.r} in the makefile.  Or, if you
9073 are using Ratfor exclusively, with no C files, remove @samp{.c} from
9074 the list of implicit rule suffixes with:@refill
9076 @example
9077 @group
9078 .SUFFIXES:
9079 .SUFFIXES: .o .r .f .l @dots{}
9080 @end group
9081 @end example
9083 @item Making Lint Libraries from C, Yacc, or Lex programs
9084 @pindex lint
9085 @cindex @code{lint}, rule to run
9086 @pindex .ln
9087 @file{@var{n}.ln} is made from @file{@var{n}.c} by running @code{lint}.
9088 The precise recipe is @w{@samp{$(LINT) $(LINTFLAGS) $(CPPFLAGS) -i}}.
9089 The same recipe is used on the C code produced from
9090 @file{@var{n}.y} or @file{@var{n}.l}.@refill
9092 @item @TeX{} and Web
9093 @cindex @TeX{}, rule to run
9094 @cindex Web, rule to run
9095 @pindex tex
9096 @pindex cweave
9097 @pindex weave
9098 @pindex tangle
9099 @pindex ctangle
9100 @pindex .dvi
9101 @pindex .tex
9102 @pindex .web
9103 @pindex .w
9104 @pindex .ch
9105 @file{@var{n}.dvi} is made from @file{@var{n}.tex} with the recipe
9106 @samp{$(TEX)}.  @file{@var{n}.tex} is made from @file{@var{n}.web} with
9107 @samp{$(WEAVE)}, or from @file{@var{n}.w} (and from @file{@var{n}.ch} if
9108 it exists or can be made) with @samp{$(CWEAVE)}.  @file{@var{n}.p} is
9109 made from @file{@var{n}.web} with @samp{$(TANGLE)} and @file{@var{n}.c}
9110 is made from @file{@var{n}.w} (and from @file{@var{n}.ch} if it exists
9111 or can be made) with @samp{$(CTANGLE)}.@refill
9113 @item Texinfo and Info
9114 @cindex Texinfo, rule to format
9115 @cindex Info, rule to format
9116 @pindex texi2dvi
9117 @pindex makeinfo
9118 @pindex .texinfo
9119 @pindex .info
9120 @pindex .texi
9121 @pindex .txinfo
9122 @file{@var{n}.dvi} is made from @file{@var{n}.texinfo},
9123 @file{@var{n}.texi}, or @file{@var{n}.txinfo}, with the recipe
9124 @w{@samp{$(TEXI2DVI) $(TEXI2DVI_FLAGS)}}.  @file{@var{n}.info} is made from
9125 @file{@var{n}.texinfo}, @file{@var{n}.texi}, or @file{@var{n}.txinfo}, with
9126 the recipe @w{@samp{$(MAKEINFO) $(MAKEINFO_FLAGS)}}.
9128 @item RCS
9129 @cindex RCS, rule to extract from
9130 @pindex co
9131 @pindex ,v @r{(RCS file extension)}
9132 Any file @file{@var{n}} is extracted if necessary from an RCS file
9133 named either @file{@var{n},v} or @file{RCS/@var{n},v}.  The precise
9134 recipe used is @w{@samp{$(CO) $(COFLAGS)}}.  @file{@var{n}} will not be
9135 extracted from RCS if it already exists, even if the RCS file is
9136 newer.  The rules for RCS are terminal
9137 (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}),
9138 so RCS files cannot be generated from another source; they must
9139 actually exist.@refill
9141 @item SCCS
9142 @cindex SCCS, rule to extract from
9143 @pindex get
9144 @pindex s. @r{(SCCS file prefix)}
9145 Any file @file{@var{n}} is extracted if necessary from an SCCS file
9146 named either @file{s.@var{n}} or @file{SCCS/s.@var{n}}.  The precise
9147 recipe used is @w{@samp{$(GET) $(GFLAGS)}}.  The rules for SCCS are
9148 terminal (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}),
9149 so SCCS files cannot be generated from another source; they must
9150 actually exist.@refill
9152 @pindex .sh
9153 For the benefit of SCCS, a file @file{@var{n}} is copied from
9154 @file{@var{n}.sh} and made executable (by everyone).  This is for
9155 shell scripts that are checked into SCCS.  Since RCS preserves the
9156 execution permission of a file, you do not need to use this feature
9157 with RCS.@refill
9159 We recommend that you avoid using of SCCS.  RCS is widely held to be
9160 superior, and is also free.  By choosing free software in place of
9161 comparable (or inferior) proprietary software, you support the free
9162 software movement.
9163 @end table
9165 Usually, you want to change only the variables listed in the table
9166 above, which are documented in the following section.
9168 However, the recipes in built-in implicit rules actually use
9169 variables such as @code{COMPILE.c}, @code{LINK.p}, and
9170 @code{PREPROCESS.S}, whose values contain the recipes listed above.
9172 @code{make} follows the convention that the rule to compile a
9173 @file{.@var{x}} source file uses the variable @code{COMPILE.@var{x}}.
9174 Similarly, the rule to produce an executable from a @file{.@var{x}}
9175 file uses @code{LINK.@var{x}}; and the rule to preprocess a
9176 @file{.@var{x}} file uses @code{PREPROCESS.@var{x}}.
9178 @vindex OUTPUT_OPTION
9179 Every rule that produces an object file uses the variable
9180 @code{OUTPUT_OPTION}.  @code{make} defines this variable either to
9181 contain @samp{-o $@@}, or to be empty, depending on a compile-time
9182 option.  You need the @samp{-o} option to ensure that the output goes
9183 into the right file when the source file is in a different directory,
9184 as when using @code{VPATH} (@pxref{Directory Search}).  However,
9185 compilers on some systems do not accept a @samp{-o} switch for object
9186 files.  If you use such a system, and use @code{VPATH}, some
9187 compilations will put their output in the wrong place.
9188 A possible workaround for this problem is to give @code{OUTPUT_OPTION}
9189 the value @w{@samp{; mv $*.o $@@}}.
9191 @node Implicit Variables, Chained Rules, Catalogue of Rules, Implicit Rules
9192 @section Variables Used by Implicit Rules
9193 @cindex flags for compilers
9195 The recipes in built-in implicit rules make liberal use of certain
9196 predefined variables.  You can alter the values of these variables in
9197 the makefile, with arguments to @code{make}, or in the environment to
9198 alter how the implicit rules work without redefining the rules
9199 themselves.  You can cancel all variables used by implicit rules with
9200 the @samp{-R} or @samp{--no-builtin-variables} option.
9202 For example, the recipe used to compile a C source file actually says
9203 @samp{$(CC) -c $(CFLAGS) $(CPPFLAGS)}.  The default values of the variables
9204 used are @samp{cc} and nothing, resulting in the command @samp{cc -c}.  By
9205 redefining @samp{CC} to @samp{ncc}, you could cause @samp{ncc} to be
9206 used for all C compilations performed by the implicit rule.  By redefining
9207 @samp{CFLAGS} to be @samp{-g}, you could pass the @samp{-g} option to
9208 each compilation.  @emph{All} implicit rules that do C compilation use
9209 @samp{$(CC)} to get the program name for the compiler and @emph{all}
9210 include @samp{$(CFLAGS)} among the arguments given to the compiler.@refill
9212 The variables used in implicit rules fall into two classes: those that are
9213 names of programs (like @code{CC}) and those that contain arguments for the
9214 programs (like @code{CFLAGS}).  (The ``name of a program'' may also contain
9215 some command arguments, but it must start with an actual executable program
9216 name.)  If a variable value contains more than one argument, separate them
9217 with spaces.
9219 The following tables describe of some of the more commonly-used predefined
9220 variables.  This list is not exhaustive, and the default values shown here may
9221 not be what @code{make} selects for your environment.  To see the
9222 complete list of predefined variables for your instance of GNU @code{make} you
9223 can run @samp{make -p} in a directory with no makefiles.
9225 Here is a table of some of the more common variables used as names of
9226 programs in built-in rules:
9228 @table @code
9229 @item AR
9230 @vindex AR
9231 Archive-maintaining program; default @samp{ar}.
9232 @pindex ar
9234 @item AS
9235 @vindex AS
9236 Program for compiling assembly files; default @samp{as}.
9237 @pindex as
9239 @item CC
9240 @vindex CC
9241 Program for compiling C programs; default @samp{cc}.
9242 @pindex cc
9244 @item CXX
9245 @vindex CXX
9246 Program for compiling C++ programs; default @samp{g++}.
9247 @pindex g++
9249 @item CPP
9250 @vindex CPP
9251 Program for running the C preprocessor, with results to standard output;
9252 default @samp{$(CC) -E}.
9254 @item FC
9255 @vindex FC
9256 Program for compiling or preprocessing Fortran and Ratfor programs;
9257 default @samp{f77}.
9258 @pindex f77
9260 @item M2C
9261 @vindex M2C
9262 Program to use to compile Modula-2 source code; default @samp{m2c}.
9263 @pindex m2c
9265 @item PC
9266 @vindex PC
9267 Program for compiling Pascal programs; default @samp{pc}.
9268 @pindex pc
9270 @item CO
9271 @vindex CO
9272 Program for extracting a file from RCS; default @samp{co}.
9273 @pindex co
9275 @item GET
9276 @vindex GET
9277 Program for extracting a file from SCCS; default @samp{get}.
9278 @pindex get
9280 @item LEX
9281 @vindex LEX
9282 Program to use to turn Lex grammars into source code; default @samp{lex}.
9283 @pindex lex
9285 @item YACC
9286 @vindex YACC
9287 Program to use to turn Yacc grammars into source code; default @samp{yacc}.
9288 @pindex yacc
9290 @item LINT
9291 @vindex LINT
9292 Program to use to run lint on source code; default @samp{lint}.
9293 @pindex lint
9295 @item MAKEINFO
9296 @vindex MAKEINFO
9297 Program to convert a Texinfo source file into an Info file; default
9298 @samp{makeinfo}.
9299 @pindex makeinfo
9301 @item TEX
9302 @vindex TEX
9303 Program to make @TeX{} @sc{dvi} files from @TeX{} source;
9304 default @samp{tex}.
9305 @pindex tex
9307 @item TEXI2DVI
9308 @vindex TEXI2DVI
9309 Program to make @TeX{} @sc{dvi} files from Texinfo source;
9310 default @samp{texi2dvi}.
9311 @pindex texi2dvi
9313 @item WEAVE
9314 @vindex WEAVE
9315 Program to translate Web into @TeX{}; default @samp{weave}.
9316 @pindex weave
9318 @item CWEAVE
9319 @vindex CWEAVE
9320 Program to translate C Web into @TeX{}; default @samp{cweave}.
9321 @pindex cweave
9323 @item TANGLE
9324 @vindex TANGLE
9325 Program to translate Web into Pascal; default @samp{tangle}.
9326 @pindex tangle
9328 @item CTANGLE
9329 @vindex CTANGLE
9330 Program to translate C Web into C; default @samp{ctangle}.
9331 @pindex ctangle
9333 @item RM
9334 @vindex RM
9335 Command to remove a file; default @samp{rm -f}.
9336 @pindex rm
9337 @end table
9339 Here is a table of variables whose values are additional arguments for the
9340 programs above.  The default values for all of these is the empty
9341 string, unless otherwise noted.
9343 @table @code
9344 @item ARFLAGS
9345 @vindex ARFLAGS
9346 Flags to give the archive-maintaining program; default @samp{rv}.
9348 @item ASFLAGS
9349 @vindex ASFLAGS
9350 Extra flags to give to the assembler (when explicitly
9351 invoked on a @samp{.s} or @samp{.S} file).
9353 @item CFLAGS
9354 @vindex CFLAGS
9355 Extra flags to give to the C compiler.
9357 @item CXXFLAGS
9358 @vindex CXXFLAGS
9359 Extra flags to give to the C++ compiler.
9361 @item COFLAGS
9362 @vindex COFLAGS
9363 Extra flags to give to the RCS @code{co} program.
9365 @item CPPFLAGS
9366 @vindex CPPFLAGS
9367 Extra flags to give to the C preprocessor and programs
9368 that use it (the C and Fortran compilers).
9370 @item FFLAGS
9371 @vindex FFLAGS
9372 Extra flags to give to the Fortran compiler.
9374 @item GFLAGS
9375 @vindex GFLAGS
9376 Extra flags to give to the SCCS @code{get} program.
9378 @item LDFLAGS
9379 @vindex LDFLAGS
9380 Extra flags to give to compilers when they are supposed to invoke the linker,
9381 @samp{ld}.
9383 @item LDLIBS
9384 @vindex LDLIBS
9385 @vindex LOADLIBES
9386 Library flags or names given to compilers when they are supposed to
9387 invoke the linker, @samp{ld}.  @code{LOADLIBES} is a deprecated (but
9388 still supported) alternative to @code{LDLIBS}.
9390 @item LFLAGS
9391 @vindex LFLAGS
9392 Extra flags to give to Lex.
9394 @item YFLAGS
9395 @vindex YFLAGS
9396 Extra flags to give to Yacc.
9398 @item PFLAGS
9399 @vindex PFLAGS
9400 Extra flags to give to the Pascal compiler.
9402 @item RFLAGS
9403 @vindex RFLAGS
9404 Extra flags to give to the Fortran compiler for Ratfor programs.
9406 @item LINTFLAGS
9407 @vindex LINTFLAGS
9408 Extra flags to give to lint.
9409 @end table
9411 @node Chained Rules, Pattern Rules, Implicit Variables, Implicit Rules
9412 @section Chains of Implicit Rules
9414 @cindex chains of rules
9415 @cindex rule, implicit, chains of
9416 Sometimes a file can be made by a sequence of implicit rules.  For example,
9417 a file @file{@var{n}.o} could be made from @file{@var{n}.y} by running
9418 first Yacc and then @code{cc}.  Such a sequence is called a @dfn{chain}.
9420 If the file @file{@var{n}.c} exists, or is mentioned in the makefile, no
9421 special searching is required: @code{make} finds that the object file can
9422 be made by C compilation from @file{@var{n}.c}; later on, when considering
9423 how to make @file{@var{n}.c}, the rule for running Yacc is
9424 used.  Ultimately both @file{@var{n}.c} and @file{@var{n}.o} are
9425 updated.@refill
9427 @cindex intermediate files
9428 @cindex files, intermediate
9429 However, even if @file{@var{n}.c} does not exist and is not mentioned,
9430 @code{make} knows how to envision it as the missing link between
9431 @file{@var{n}.o} and @file{@var{n}.y}!  In this case, @file{@var{n}.c} is
9432 called an @dfn{intermediate file}.  Once @code{make} has decided to use the
9433 intermediate file, it is entered in the data base as if it had been
9434 mentioned in the makefile, along with the implicit rule that says how to
9435 create it.@refill
9437 Intermediate files are remade using their rules just like all other
9438 files.  But intermediate files are treated differently in two ways.
9440 The first difference is what happens if the intermediate file does not
9441 exist.  If an ordinary file @var{b} does not exist, and @code{make}
9442 considers a target that depends on @var{b}, it invariably creates
9443 @var{b} and then updates the target from @var{b}.  But if @var{b} is an
9444 intermediate file, then @code{make} can leave well enough alone.  It
9445 won't bother updating @var{b}, or the ultimate target, unless some
9446 prerequisite of @var{b} is newer than that target or there is some other
9447 reason to update that target.
9449 The second difference is that if @code{make} @emph{does} create @var{b}
9450 in order to update something else, it deletes @var{b} later on after it
9451 is no longer needed.  Therefore, an intermediate file which did not
9452 exist before @code{make} also does not exist after @code{make}.
9453 @code{make} reports the deletion to you by printing a @samp{rm -f}
9454 command showing which file it is deleting.
9456 Ordinarily, a file cannot be intermediate if it is mentioned in the
9457 makefile as a target or prerequisite.  However, you can explicitly mark a
9458 file as intermediate by listing it as a prerequisite of the special target
9459 @code{.INTERMEDIATE}.  This takes effect even if the file is mentioned
9460 explicitly in some other way.
9462 @cindex intermediate files, preserving
9463 @cindex preserving intermediate files
9464 @cindex secondary files
9465 You can prevent automatic deletion of an intermediate file by marking it
9466 as a @dfn{secondary} file.  To do this, list it as a prerequisite of the
9467 special target @code{.SECONDARY}.  When a file is secondary, @code{make}
9468 will not create the file merely because it does not already exist, but
9469 @code{make} does not automatically delete the file.  Marking a file as
9470 secondary also marks it as intermediate.
9472 You can list the target pattern of an implicit rule (such as @samp{%.o})
9473 as a prerequisite of the special target @code{.PRECIOUS} to preserve
9474 intermediate files made by implicit rules whose target patterns match
9475 that file's name; see @ref{Interrupts}.@refill
9476 @cindex preserving with @code{.PRECIOUS}
9477 @cindex @code{.PRECIOUS} intermediate files
9479 A chain can involve more than two implicit rules.  For example, it is
9480 possible to make a file @file{foo} from @file{RCS/foo.y,v} by running RCS,
9481 Yacc and @code{cc}.  Then both @file{foo.y} and @file{foo.c} are
9482 intermediate files that are deleted at the end.@refill
9484 No single implicit rule can appear more than once in a chain.  This means
9485 that @code{make} will not even consider such a ridiculous thing as making
9486 @file{foo} from @file{foo.o.o} by running the linker twice.  This
9487 constraint has the added benefit of preventing any infinite loop in the
9488 search for an implicit rule chain.
9490 There are some special implicit rules to optimize certain cases that would
9491 otherwise be handled by rule chains.  For example, making @file{foo} from
9492 @file{foo.c} could be handled by compiling and linking with separate
9493 chained rules, using @file{foo.o} as an intermediate file.  But what
9494 actually happens is that a special rule for this case does the compilation
9495 and linking with a single @code{cc} command.  The optimized rule is used in
9496 preference to the step-by-step chain because it comes earlier in the
9497 ordering of rules.
9499 @node Pattern Rules, Last Resort, Chained Rules, Implicit Rules
9500 @section Defining and Redefining Pattern Rules
9502 You define an implicit rule by writing a @dfn{pattern rule}.  A pattern
9503 rule looks like an ordinary rule, except that its target contains the
9504 character @samp{%} (exactly one of them).  The target is considered a
9505 pattern for matching file names; the @samp{%} can match any nonempty
9506 substring, while other characters match only themselves.  The prerequisites
9507 likewise use @samp{%} to show how their names relate to the target name.
9509 Thus, a pattern rule @samp{%.o : %.c} says how to make any file
9510 @file{@var{stem}.o} from another file @file{@var{stem}.c}.@refill
9512 Note that expansion using @samp{%} in pattern rules occurs
9513 @strong{after} any variable or function expansions, which take place
9514 when the makefile is read.  @xref{Using Variables, , How to Use
9515 Variables}, and @ref{Functions, ,Functions for Transforming Text}.
9517 @menu
9518 * Pattern Intro::               An introduction to pattern rules.
9519 * Pattern Examples::            Examples of pattern rules.
9520 * Automatic Variables::         How to use automatic variables in the
9521                                   recipe of implicit rules.
9522 * Pattern Match::               How patterns match.
9523 * Match-Anything Rules::        Precautions you should take prior to
9524                                   defining rules that can match any
9525                                   target file whatever.
9526 * Canceling Rules::             How to override or cancel built-in rules.
9527 @end menu
9529 @node Pattern Intro, Pattern Examples, Pattern Rules, Pattern Rules
9530 @subsection Introduction to Pattern Rules
9531 @cindex pattern rule
9532 @cindex rule, pattern
9534 A pattern rule contains the character @samp{%} (exactly one of them)
9535 in the target; otherwise, it looks exactly like an ordinary rule.  The
9536 target is a pattern for matching file names; the @samp{%} matches any
9537 nonempty substring, while other characters match only themselves.
9538 @cindex target pattern, implicit
9539 @cindex @code{%}, in pattern rules
9541 For example, @samp{%.c} as a pattern matches any file name that ends in
9542 @samp{.c}.  @samp{s.%.c} as a pattern matches any file name that starts
9543 with @samp{s.}, ends in @samp{.c} and is at least five characters long.
9544 (There must be at least one character to match the @samp{%}.)  The substring
9545 that the @samp{%} matches is called the @dfn{stem}.@refill
9547 @samp{%} in a prerequisite of a pattern rule stands for the same stem
9548 that was matched by the @samp{%} in the target.  In order for the
9549 pattern rule to apply, its target pattern must match the file name
9550 under consideration and all of its prerequisites (after pattern
9551 substitution) must name files that exist or can be made.  These files
9552 become prerequisites of the target.
9553 @cindex prerequisite pattern, implicit
9555 Thus, a rule of the form
9557 @example
9558 %.o : %.c ; @var{recipe}@dots{}
9559 @end example
9561 @noindent
9562 specifies how to make a file @file{@var{n}.o}, with another file
9563 @file{@var{n}.c} as its prerequisite, provided that @file{@var{n}.c}
9564 exists or can be made.
9566 There may also be prerequisites that do not use @samp{%}; such a prerequisite
9567 attaches to every file made by this pattern rule.  These unvarying
9568 prerequisites are useful occasionally.
9570 A pattern rule need not have any prerequisites that contain @samp{%}, or
9571 in fact any prerequisites at all.  Such a rule is effectively a general
9572 wildcard.  It provides a way to make any file that matches the target
9573 pattern.  @xref{Last Resort}.
9575 More than one pattern rule may match a target.  In this case
9576 @code{make} will choose the ``best fit'' rule.  @xref{Pattern Match,
9577 ,How Patterns Match}.
9579 @c !!! The end of of this paragraph should be rewritten.  --bob
9580 Pattern rules may have more than one target.  Unlike normal rules,
9581 this does not act as many different rules with the same prerequisites
9582 and recipe.  If a pattern rule has multiple targets, @code{make} knows
9583 that the rule's recipe is responsible for making all of the targets.
9584 The recipe is executed only once to make all the targets.  When
9585 searching for a pattern rule to match a target, the target patterns of
9586 a rule other than the one that matches the target in need of a rule
9587 are incidental: @code{make} worries only about giving a recipe and
9588 prerequisites to the file presently in question.  However, when this
9589 file's recipe is run, the other targets are marked as having been
9590 updated themselves.
9591 @cindex multiple targets, in pattern rule
9592 @cindex target, multiple in pattern rule
9594 @node Pattern Examples, Automatic Variables, Pattern Intro, Pattern Rules
9595 @subsection Pattern Rule Examples
9597 Here are some examples of pattern rules actually predefined in
9598 @code{make}.  First, the rule that compiles @samp{.c} files into @samp{.o}
9599 files:@refill
9601 @example
9602 %.o : %.c
9603         $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@@
9604 @end example
9606 @noindent
9607 defines a rule that can make any file @file{@var{x}.o} from
9608 @file{@var{x}.c}.  The recipe uses the automatic variables @samp{$@@} and
9609 @samp{$<} to substitute the names of the target file and the source file
9610 in each case where the rule applies (@pxref{Automatic Variables}).@refill
9612 Here is a second built-in rule:
9614 @example
9615 % :: RCS/%,v
9616         $(CO) $(COFLAGS) $<
9617 @end example
9619 @noindent
9620 defines a rule that can make any file @file{@var{x}} whatsoever from a
9621 corresponding file @file{@var{x},v} in the sub-directory @file{RCS}.  Since
9622 the target is @samp{%}, this rule will apply to any file whatever, provided
9623 the appropriate prerequisite file exists.  The double colon makes the rule
9624 @dfn{terminal}, which means that its prerequisite may not be an intermediate
9625 file (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}).@refill
9627 @need 500
9628 This pattern rule has two targets:
9630 @example
9631 @group
9632 %.tab.c %.tab.h: %.y
9633         bison -d $<
9634 @end group
9635 @end example
9637 @noindent
9638 @c The following paragraph is rewritten to avoid overfull hboxes
9639 This tells @code{make} that the recipe @samp{bison -d @var{x}.y} will
9640 make both @file{@var{x}.tab.c} and @file{@var{x}.tab.h}.  If the file
9641 @file{foo} depends on the files @file{parse.tab.o} and @file{scan.o}
9642 and the file @file{scan.o} depends on the file @file{parse.tab.h},
9643 when @file{parse.y} is changed, the recipe @samp{bison -d parse.y}
9644 will be executed only once, and the prerequisites of both
9645 @file{parse.tab.o} and @file{scan.o} will be satisfied.  (Presumably
9646 the file @file{parse.tab.o} will be recompiled from @file{parse.tab.c}
9647 and the file @file{scan.o} from @file{scan.c}, while @file{foo} is
9648 linked from @file{parse.tab.o}, @file{scan.o}, and its other
9649 prerequisites, and it will execute happily ever after.)@refill
9651 @node Automatic Variables, Pattern Match, Pattern Examples, Pattern Rules
9652 @subsection Automatic Variables
9653 @cindex automatic variables
9654 @cindex variables, automatic
9655 @cindex variables, and implicit rule
9657 Suppose you are writing a pattern rule to compile a @samp{.c} file into a
9658 @samp{.o} file: how do you write the @samp{cc} command so that it operates
9659 on the right source file name?  You cannot write the name in the recipe,
9660 because the name is different each time the implicit rule is applied.
9662 What you do is use a special feature of @code{make}, the @dfn{automatic
9663 variables}.  These variables have values computed afresh for each rule that
9664 is executed, based on the target and prerequisites of the rule.  In this
9665 example, you would use @samp{$@@} for the object file name and @samp{$<}
9666 for the source file name.
9668 @cindex automatic variables in prerequisites
9669 @cindex prerequisites, and automatic variables
9670 It's very important that you recognize the limited scope in which
9671 automatic variable values are available: they only have values within
9672 the recipe.  In particular, you cannot use them anywhere
9673 within the target list of a rule; they have no value there and will
9674 expand to the empty string.  Also, they cannot be accessed directly
9675 within the prerequisite list of a rule.  A common mistake is
9676 attempting to use @code{$@@} within the prerequisites list; this will
9677 not work.  However, there is a special feature of GNU @code{make},
9678 secondary expansion (@pxref{Secondary Expansion}), which will allow
9679 automatic variable values to be used in prerequisite lists.
9681 Here is a table of automatic variables:
9683 @table @code
9684 @vindex $@@
9685 @vindex @@ @r{(automatic variable)}
9686 @item $@@
9687 The file name of the target of the rule.  If the target is an archive
9688 member, then @samp{$@@} is the name of the archive file.  In a pattern
9689 rule that has multiple targets (@pxref{Pattern Intro, ,Introduction to
9690 Pattern Rules}), @samp{$@@} is the name of whichever target caused the
9691 rule's recipe to be run.
9693 @vindex $%
9694 @vindex % @r{(automatic variable)}
9695 @item $%
9696 The target member name, when the target is an archive member.
9697 @xref{Archives}.  For example, if the target is @file{foo.a(bar.o)} then
9698 @samp{$%} is @file{bar.o} and @samp{$@@} is @file{foo.a}.  @samp{$%} is
9699 empty when the target is not an archive member.
9701 @vindex $<
9702 @vindex < @r{(automatic variable)}
9703 @item $<
9704 The name of the first prerequisite.  If the target got its recipe from
9705 an implicit rule, this will be the first prerequisite added by the
9706 implicit rule (@pxref{Implicit Rules}).
9708 @vindex $?
9709 @vindex ? @r{(automatic variable)}
9710 @item $?
9711 The names of all the prerequisites that are newer than the target, with
9712 spaces between them.  For prerequisites which are archive members, only
9713 the named member is used (@pxref{Archives}).
9714 @cindex prerequisites, list of changed
9715 @cindex list of changed prerequisites
9717 @vindex $^
9718 @vindex ^ @r{(automatic variable)}
9719 @item $^
9720 The names of all the prerequisites, with spaces between them.  For
9721 prerequisites which are archive members, only the named member is used
9722 (@pxref{Archives}).  A target has only one prerequisite on each other file
9723 it depends on, no matter how many times each file is listed as a
9724 prerequisite.  So if you list a prerequisite more than once for a target,
9725 the value of @code{$^} contains just one copy of the name.  This list
9726 does @strong{not} contain any of the order-only prerequisites; for those
9727 see the @samp{$|} variable, below.
9728 @cindex prerequisites, list of all
9729 @cindex list of all prerequisites
9731 @vindex $+
9732 @vindex + @r{(automatic variable)}
9733 @item $+
9734 This is like @samp{$^}, but prerequisites listed more than once are
9735 duplicated in the order they were listed in the makefile.  This is
9736 primarily useful for use in linking commands where it is meaningful to
9737 repeat library file names in a particular order.
9739 @vindex $|
9740 @vindex | @r{(automatic variable)}
9741 @item $|
9742 The names of all the order-only prerequisites, with spaces between
9743 them.
9745 @vindex $*
9746 @vindex * @r{(automatic variable)}
9747 @item $*
9748 The stem with which an implicit rule matches (@pxref{Pattern Match, ,How
9749 Patterns Match}).  If the target is @file{dir/a.foo.b} and the target
9750 pattern is @file{a.%.b} then the stem is @file{dir/foo}.  The stem is
9751 useful for constructing names of related files.@refill
9752 @cindex stem, variable for
9754 In a static pattern rule, the stem is part of the file name that matched
9755 the @samp{%} in the target pattern.
9757 In an explicit rule, there is no stem; so @samp{$*} cannot be determined
9758 in that way.  Instead, if the target name ends with a recognized suffix
9759 (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}), @samp{$*} is set to
9760 the target name minus the suffix.  For example, if the target name is
9761 @samp{foo.c}, then @samp{$*} is set to @samp{foo}, since @samp{.c} is a
9762 suffix.  GNU @code{make} does this bizarre thing only for compatibility
9763 with other implementations of @code{make}.  You should generally avoid
9764 using @samp{$*} except in implicit rules or static pattern rules.@refill
9766 If the target name in an explicit rule does not end with a recognized
9767 suffix, @samp{$*} is set to the empty string for that rule.
9768 @end table
9770 @samp{$?} is useful even in explicit rules when you wish to operate on only
9771 the prerequisites that have changed.  For example, suppose that an archive
9772 named @file{lib} is supposed to contain copies of several object files.
9773 This rule copies just the changed object files into the archive:
9775 @example
9776 @group
9777 lib: foo.o bar.o lose.o win.o
9778         ar r lib $?
9779 @end group
9780 @end example
9782 Of the variables listed above, four have values that are single file
9783 names, and three have values that are lists of file names.  These seven
9784 have variants that get just the file's directory name or just the file
9785 name within the directory.  The variant variables' names are formed by
9786 appending @samp{D} or @samp{F}, respectively.  These variants are
9787 semi-obsolete in GNU @code{make} since the functions @code{dir} and
9788 @code{notdir} can be used to get a similar effect (@pxref{File Name
9789 Functions, , Functions for File Names}).  Note, however, that the
9790 @samp{D} variants all omit the trailing slash which always appears in
9791 the output of the @code{dir} function.  Here is a table of the variants:
9793 @table @samp
9794 @vindex $(@@D)
9795 @vindex @@D @r{(automatic variable)}
9796 @item $(@@D)
9797 The directory part of the file name of the target, with the trailing
9798 slash removed.  If the value of @samp{$@@} is @file{dir/foo.o} then
9799 @samp{$(@@D)} is @file{dir}.  This value is @file{.} if @samp{$@@} does
9800 not contain a slash.
9802 @vindex $(@@F)
9803 @vindex @@F @r{(automatic variable)}
9804 @item $(@@F)
9805 The file-within-directory part of the file name of the target.  If the
9806 value of @samp{$@@} is @file{dir/foo.o} then @samp{$(@@F)} is
9807 @file{foo.o}.  @samp{$(@@F)} is equivalent to @samp{$(notdir $@@)}.
9809 @vindex $(*D)
9810 @vindex *D @r{(automatic variable)}
9811 @item $(*D)
9812 @vindex $(*F)
9813 @vindex *F @r{(automatic variable)}
9814 @itemx $(*F)
9815 The directory part and the file-within-directory
9816 part of the stem; @file{dir} and @file{foo} in this example.
9818 @vindex $(%D)
9819 @vindex %D @r{(automatic variable)}
9820 @item $(%D)
9821 @vindex $(%F)
9822 @vindex %F @r{(automatic variable)}
9823 @itemx $(%F)
9824 The directory part and the file-within-directory part of the target
9825 archive member name.  This makes sense only for archive member targets
9826 of the form @file{@var{archive}(@var{member})} and is useful only when
9827 @var{member} may contain a directory name.  (@xref{Archive Members,
9828 ,Archive Members as Targets}.)
9830 @vindex $(<D)
9831 @vindex <D @r{(automatic variable)}
9832 @item $(<D)
9833 @vindex $(<F)
9834 @vindex <F @r{(automatic variable)}
9835 @itemx $(<F)
9836 The directory part and the file-within-directory
9837 part of the first prerequisite.
9839 @vindex $(^D)
9840 @vindex ^D @r{(automatic variable)}
9841 @item $(^D)
9842 @vindex $(^F)
9843 @vindex ^F @r{(automatic variable)}
9844 @itemx $(^F)
9845 Lists of the directory parts and the file-within-directory
9846 parts of all prerequisites.
9848 @vindex $(+D)
9849 @vindex +D @r{(automatic variable)}
9850 @item $(+D)
9851 @vindex $(+F)
9852 @vindex +F @r{(automatic variable)}
9853 @itemx $(+F)
9854 Lists of the directory parts and the file-within-directory
9855 parts of all prerequisites, including multiple instances of duplicated
9856 prerequisites.
9858 @vindex $(?D)
9859 @vindex ?D @r{(automatic variable)}
9860 @item $(?D)
9861 @vindex $(?F)
9862 @vindex ?F @r{(automatic variable)}
9863 @itemx $(?F)
9864 Lists of the directory parts and the file-within-directory parts of
9865 all prerequisites that are newer than the target.
9866 @end table
9868 Note that we use a special stylistic convention when we talk about these
9869 automatic variables; we write ``the value of @samp{$<}'', rather than
9870 @w{``the variable @code{<}''} as we would write for ordinary variables
9871 such as @code{objects} and @code{CFLAGS}.  We think this convention
9872 looks more natural in this special case.  Please do not assume it has a
9873 deep significance; @samp{$<} refers to the variable named @code{<} just
9874 as @samp{$(CFLAGS)} refers to the variable named @code{CFLAGS}.
9875 You could just as well use @samp{$(<)} in place of @samp{$<}.
9877 @node Pattern Match, Match-Anything Rules, Automatic Variables, Pattern Rules
9878 @subsection How Patterns Match
9880 @cindex stem
9881 A target pattern is composed of a @samp{%} between a prefix and a suffix,
9882 either or both of which may be empty.  The pattern matches a file name only
9883 if the file name starts with the prefix and ends with the suffix, without
9884 overlap.  The text between the prefix and the suffix is called the
9885 @dfn{stem}.  Thus, when the pattern @samp{%.o} matches the file name
9886 @file{test.o}, the stem is @samp{test}.  The pattern rule prerequisites are
9887 turned into actual file names by substituting the stem for the character
9888 @samp{%}.  Thus, if in the same example one of the prerequisites is written
9889 as @samp{%.c}, it expands to @samp{test.c}.@refill
9891 When the target pattern does not contain a slash (and it usually does
9892 not), directory names in the file names are removed from the file name
9893 before it is compared with the target prefix and suffix.  After the
9894 comparison of the file name to the target pattern, the directory
9895 names, along with the slash that ends them, are added on to the
9896 prerequisite file names generated from the pattern rule's prerequisite
9897 patterns and the file name.  The directories are ignored only for the
9898 purpose of finding an implicit rule to use, not in the application of
9899 that rule.  Thus, @samp{e%t} matches the file name @file{src/eat},
9900 with @samp{src/a} as the stem.  When prerequisites are turned into file
9901 names, the directories from the stem are added at the front, while the
9902 rest of the stem is substituted for the @samp{%}.  The stem
9903 @samp{src/a} with a prerequisite pattern @samp{c%r} gives the file name
9904 @file{src/car}.@refill
9906 @cindex pattern rules, order of
9907 @cindex order of pattern rules
9908 A pattern rule can be used to build a given file only if there is a
9909 target pattern that matches the file name, @emph{and} all
9910 prerequisites in that rule either exist or can be built.  The rules
9911 you write take precedence over those that are built in. Note however,
9912 that a rule whose prerequisites actually exist or are mentioned always
9913 takes priority over a rule with prerequisites that must be made by
9914 chaining other implicit rules.
9916 @cindex stem, shortest
9917 It is possible that more than one pattern rule will meet these
9918 criteria.  In that case, @code{make} will choose the rule with the
9919 shortest stem (that is, the pattern that matches most specifically).
9920 If more than one pattern rule has the shortest stem, @code{make} will
9921 choose the first one found in the makefile.
9923 This algorithm results in more specific rules being preferred over
9924 more generic ones; for example:
9926 @example
9927 %.o: %.c
9928         $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@@
9930 %.o : %.f
9931         $(COMPILE.F) $(OUTPUT_OPTION) $<
9933 lib/%.o: lib/%.c
9934         $(CC) -fPIC -c $(CFLAGS) $(CPPFLAGS) $< -o $@@
9935 @end example
9937 Given these rules and asked to build @file{bar.o} where both
9938 @file{bar.c} and @file{bar.f} exist, @code{make} will choose the first
9939 rule and compile @file{bar.c} into @file{bar.o}.  In the same
9940 situation where @file{bar.c} does not exist, then @code{make} will
9941 choose the second rule and compile @file{bar.f} into @file{bar.o}.
9943 If @code{make} is asked to build @file{lib/bar.o} and both
9944 @file{lib/bar.c} and @file{lib/bar.f} exist, then the third rule will
9945 be chosen since the stem for this rule (@samp{bar}) is shorter than
9946 the stem for the first rule (@samp{lib/bar}).  If @file{lib/bar.c}
9947 does not exist then the third rule is not eligible and the second rule
9948 will be used, even though the stem is longer.
9950 @node Match-Anything Rules, Canceling Rules, Pattern Match, Pattern Rules
9951 @subsection Match-Anything Pattern Rules
9953 @cindex match-anything rule
9954 @cindex terminal rule
9955 When a pattern rule's target is just @samp{%}, it matches any file name
9956 whatever.  We call these rules @dfn{match-anything} rules.  They are very
9957 useful, but it can take a lot of time for @code{make} to think about them,
9958 because it must consider every such rule for each file name listed either
9959 as a target or as a prerequisite.
9961 Suppose the makefile mentions @file{foo.c}.  For this target, @code{make}
9962 would have to consider making it by linking an object file @file{foo.c.o},
9963 or by C compilation-and-linking in one step from @file{foo.c.c}, or by
9964 Pascal compilation-and-linking from @file{foo.c.p}, and many other
9965 possibilities.
9967 We know these possibilities are ridiculous since @file{foo.c} is a C source
9968 file, not an executable.  If @code{make} did consider these possibilities,
9969 it would ultimately reject them, because files such as @file{foo.c.o} and
9970 @file{foo.c.p} would not exist.  But these possibilities are so
9971 numerous that @code{make} would run very slowly if it had to consider
9972 them.@refill
9974 To gain speed, we have put various constraints on the way @code{make}
9975 considers match-anything rules.  There are two different constraints that
9976 can be applied, and each time you define a match-anything rule you must
9977 choose one or the other for that rule.
9979 One choice is to mark the match-anything rule as @dfn{terminal} by defining
9980 it with a double colon.  When a rule is terminal, it does not apply unless
9981 its prerequisites actually exist.  Prerequisites that could be made with
9982 other implicit rules are not good enough.  In other words, no further
9983 chaining is allowed beyond a terminal rule.
9985 For example, the built-in implicit rules for extracting sources from RCS
9986 and SCCS files are terminal; as a result, if the file @file{foo.c,v} does
9987 not exist, @code{make} will not even consider trying to make it as an
9988 intermediate file from @file{foo.c,v.o} or from @file{RCS/SCCS/s.foo.c,v}.
9989 RCS and SCCS files are generally ultimate source files, which should not be
9990 remade from any other files; therefore, @code{make} can save time by not
9991 looking for ways to remake them.@refill
9993 If you do not mark the match-anything rule as terminal, then it is
9994 non-terminal.  A non-terminal match-anything rule cannot apply to a file name
9995 that indicates a specific type of data.  A file name indicates a specific
9996 type of data if some non-match-anything implicit rule target matches it.
9998 For example, the file name @file{foo.c} matches the target for the pattern
9999 rule @samp{%.c : %.y} (the rule to run Yacc).  Regardless of whether this
10000 rule is actually applicable (which happens only if there is a file
10001 @file{foo.y}), the fact that its target matches is enough to prevent
10002 consideration of any non-terminal match-anything rules for the file
10003 @file{foo.c}.  Thus, @code{make} will not even consider trying to make
10004 @file{foo.c} as an executable file from @file{foo.c.o}, @file{foo.c.c},
10005 @file{foo.c.p}, etc.@refill
10007 The motivation for this constraint is that non-terminal match-anything
10008 rules are used for making files containing specific types of data (such as
10009 executable files) and a file name with a recognized suffix indicates some
10010 other specific type of data (such as a C source file).
10012 Special built-in dummy pattern rules are provided solely to recognize
10013 certain file names so that non-terminal match-anything rules will not be
10014 considered.  These dummy rules have no prerequisites and no recipes, and
10015 they are ignored for all other purposes.  For example, the built-in
10016 implicit rule
10018 @example
10019 %.p :
10020 @end example
10022 @noindent
10023 exists to make sure that Pascal source files such as @file{foo.p} match a
10024 specific target pattern and thereby prevent time from being wasted looking
10025 for @file{foo.p.o} or @file{foo.p.c}.
10027 Dummy pattern rules such as the one for @samp{%.p} are made for every
10028 suffix listed as valid for use in suffix rules (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}).
10030 @node Canceling Rules,  , Match-Anything Rules, Pattern Rules
10031 @subsection Canceling Implicit Rules
10033 You can override a built-in implicit rule (or one you have defined
10034 yourself) by defining a new pattern rule with the same target and
10035 prerequisites, but a different recipe.  When the new rule is defined, the
10036 built-in one is replaced.  The new rule's position in the sequence of
10037 implicit rules is determined by where you write the new rule.
10039 You can cancel a built-in implicit rule by defining a pattern rule with the
10040 same target and prerequisites, but no recipe.  For example, the following
10041 would cancel the rule that runs the assembler:
10043 @example
10044 %.o : %.s
10045 @end example
10047 @node Last Resort, Suffix Rules, Pattern Rules, Implicit Rules
10048 @section Defining Last-Resort Default Rules
10049 @cindex last-resort default rules
10050 @cindex default rules, last-resort
10052 You can define a last-resort implicit rule by writing a terminal
10053 match-anything pattern rule with no prerequisites (@pxref{Match-Anything
10054 Rules}).  This is just like any other pattern rule; the only thing
10055 special about it is that it will match any target.  So such a rule's
10056 recipe is used for all targets and prerequisites that have no recipe
10057 of their own and for which no other implicit rule applies.
10059 For example, when testing a makefile, you might not care if the source
10060 files contain real data, only that they exist.  Then you might do this:
10062 @example
10064         touch $@@
10065 @end example
10067 @noindent
10068 to cause all the source files needed (as prerequisites) to be created
10069 automatically.
10071 @findex .DEFAULT
10072 You can instead define a recipe to be used for targets for which there
10073 are no rules at all, even ones which don't specify recipes.  You do
10074 this by writing a rule for the target @code{.DEFAULT}.  Such a rule's
10075 recipe is used for all prerequisites which do not appear as targets in
10076 any explicit rule, and for which no implicit rule applies.  Naturally,
10077 there is no @code{.DEFAULT} rule unless you write one.
10079 If you use @code{.DEFAULT} with no recipe or prerequisites:
10081 @example
10082 .DEFAULT:
10083 @end example
10085 @noindent
10086 the recipe previously stored for @code{.DEFAULT} is cleared.  Then
10087 @code{make} acts as if you had never defined @code{.DEFAULT} at all.
10089 If you do not want a target to get the recipe from a match-anything
10090 pattern rule or @code{.DEFAULT}, but you also do not want any recipe
10091 to be run for the target, you can give it an empty recipe
10092 (@pxref{Empty Recipes, ,Defining Empty Recipes}).@refill
10094 You can use a last-resort rule to override part of another makefile.
10095 @xref{Overriding Makefiles, , Overriding Part of Another Makefile}.
10097 @node Suffix Rules, Implicit Rule Search, Last Resort, Implicit Rules
10098 @section Old-Fashioned Suffix Rules
10099 @cindex old-fashioned suffix rules
10100 @cindex suffix rule
10102 @dfn{Suffix rules} are the old-fashioned way of defining implicit rules for
10103 @code{make}.  Suffix rules are obsolete because pattern rules are more
10104 general and clearer.  They are supported in GNU @code{make} for
10105 compatibility with old makefiles.  They come in two kinds:
10106 @dfn{double-suffix} and @dfn{single-suffix}.@refill
10108 A double-suffix rule is defined by a pair of suffixes: the target suffix
10109 and the source suffix.  It matches any file whose name ends with the
10110 target suffix.  The corresponding implicit prerequisite is made by
10111 replacing the target suffix with the source suffix in the file name.  A
10112 two-suffix rule whose target and source suffixes are @samp{.o} and
10113 @samp{.c} is equivalent to the pattern rule @samp{%.o : %.c}.
10115 A single-suffix rule is defined by a single suffix, which is the source
10116 suffix.  It matches any file name, and the corresponding implicit
10117 prerequisite name is made by appending the source suffix.  A single-suffix
10118 rule whose source suffix is @samp{.c} is equivalent to the pattern rule
10119 @samp{% : %.c}.
10121 Suffix rule definitions are recognized by comparing each rule's target
10122 against a defined list of known suffixes.  When @code{make} sees a rule
10123 whose target is a known suffix, this rule is considered a single-suffix
10124 rule.  When @code{make} sees a rule whose target is two known suffixes
10125 concatenated, this rule is taken as a double-suffix rule.
10127 For example, @samp{.c} and @samp{.o} are both on the default list of
10128 known suffixes.  Therefore, if you define a rule whose target is
10129 @samp{.c.o}, @code{make} takes it to be a double-suffix rule with source
10130 suffix @samp{.c} and target suffix @samp{.o}.  Here is the old-fashioned
10131 way to define the rule for compiling a C source file:@refill
10133 @example
10134 .c.o:
10135         $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
10136 @end example
10138 Suffix rules cannot have any prerequisites of their own.  If they have any,
10139 they are treated as normal files with funny names, not as suffix rules.
10140 Thus, the rule:
10142 @example
10143 .c.o: foo.h
10144         $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
10145 @end example
10147 @noindent
10148 tells how to make the file @file{.c.o} from the prerequisite file
10149 @file{foo.h}, and is not at all like the pattern rule:
10151 @example
10152 %.o: %.c foo.h
10153         $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
10154 @end example
10156 @noindent
10157 which tells how to make @samp{.o} files from @samp{.c} files, and makes all
10158 @samp{.o} files using this pattern rule also depend on @file{foo.h}.
10160 Suffix rules with no recipe are also meaningless.  They do not remove
10161 previous rules as do pattern rules with no recipe (@pxref{Canceling
10162 Rules, , Canceling Implicit Rules}).  They simply enter the suffix or
10163 pair of suffixes concatenated as a target in the data base.@refill
10165 @findex .SUFFIXES
10166 The known suffixes are simply the names of the prerequisites of the special
10167 target @code{.SUFFIXES}.  You can add your own suffixes by writing a rule
10168 for @code{.SUFFIXES} that adds more prerequisites, as in:
10170 @example
10171 .SUFFIXES: .hack .win
10172 @end example
10174 @noindent
10175 which adds @samp{.hack} and @samp{.win} to the end of the list of suffixes.
10177 If you wish to eliminate the default known suffixes instead of just adding
10178 to them, write a rule for @code{.SUFFIXES} with no prerequisites.  By
10179 special dispensation, this eliminates all existing prerequisites of
10180 @code{.SUFFIXES}.  You can then write another rule to add the suffixes you
10181 want.  For example,
10183 @example
10184 @group
10185 .SUFFIXES:            # @r{Delete the default suffixes}
10186 .SUFFIXES: .c .o .h   # @r{Define our suffix list}
10187 @end group
10188 @end example
10190 The @samp{-r} or @samp{--no-builtin-rules} flag causes the default
10191 list of suffixes to be empty.
10193 @vindex SUFFIXES
10194 The variable @code{SUFFIXES} is defined to the default list of suffixes
10195 before @code{make} reads any makefiles.  You can change the list of suffixes
10196 with a rule for the special target @code{.SUFFIXES}, but that does not alter
10197 this variable.
10199 @node Implicit Rule Search,  , Suffix Rules, Implicit Rules
10200 @section Implicit Rule Search Algorithm
10201 @cindex implicit rule, search algorithm
10202 @cindex search algorithm, implicit rule
10204 Here is the procedure @code{make} uses for searching for an implicit rule
10205 for a target @var{t}.  This procedure is followed for each double-colon
10206 rule with no recipe, for each target of ordinary rules none of which have
10207 a recipe, and for each prerequisite that is not the target of any rule.  It
10208 is also followed recursively for prerequisites that come from implicit
10209 rules, in the search for a chain of rules.
10211 Suffix rules are not mentioned in this algorithm because suffix rules are
10212 converted to equivalent pattern rules once the makefiles have been read in.
10214 For an archive member target of the form
10215 @samp{@var{archive}(@var{member})}, the following algorithm is run
10216 twice, first using the entire target name @var{t}, and second using
10217 @samp{(@var{member})} as the target @var{t} if the first run found no
10218 rule.@refill
10220 @enumerate
10221 @item
10222 Split @var{t} into a directory part, called @var{d}, and the rest,
10223 called @var{n}.  For example, if @var{t} is @samp{src/foo.o}, then
10224 @var{d} is @samp{src/} and @var{n} is @samp{foo.o}.@refill
10226 @item
10227 Make a list of all the pattern rules one of whose targets matches
10228 @var{t} or @var{n}.  If the target pattern contains a slash, it is
10229 matched against @var{t}; otherwise, against @var{n}.
10231 @item
10232 If any rule in that list is @emph{not} a match-anything rule, then
10233 remove all non-terminal match-anything rules from the list.
10235 @item
10236 Remove from the list all rules with no recipe.
10238 @item
10239 For each pattern rule in the list:
10241 @enumerate a
10242 @item
10243 Find the stem @var{s}, which is the nonempty part of @var{t} or @var{n}
10244 matched by the @samp{%} in the target pattern.@refill
10246 @item
10247 Compute the prerequisite names by substituting @var{s} for @samp{%}; if
10248 the target pattern does not contain a slash, append @var{d} to
10249 the front of each prerequisite name.@refill
10251 @item
10252 Test whether all the prerequisites exist or ought to exist.  (If a
10253 file name is mentioned in the makefile as a target or as an explicit
10254 prerequisite, then we say it ought to exist.)
10256 If all prerequisites exist or ought to exist, or there are no prerequisites,
10257 then this rule applies.
10258 @end enumerate
10260 @item
10261 If no pattern rule has been found so far, try harder.
10262 For each pattern rule in the list:
10264 @enumerate a
10265 @item
10266 If the rule is terminal, ignore it and go on to the next rule.
10268 @item
10269 Compute the prerequisite names as before.
10271 @item
10272 Test whether all the prerequisites exist or ought to exist.
10274 @item
10275 For each prerequisite that does not exist, follow this algorithm
10276 recursively to see if the prerequisite can be made by an implicit
10277 rule.
10279 @item
10280 If all prerequisites exist, ought to exist, or can be
10281 made by implicit rules, then this rule applies.
10282 @end enumerate
10284 @item
10285 If no implicit rule applies, the rule for @code{.DEFAULT}, if any,
10286 applies.  In that case, give @var{t} the same recipe that
10287 @code{.DEFAULT} has.  Otherwise, there is no recipe for @var{t}.
10288 @end enumerate
10290 Once a rule that applies has been found, for each target pattern of
10291 the rule other than the one that matched @var{t} or @var{n}, the
10292 @samp{%} in the pattern is replaced with @var{s} and the resultant
10293 file name is stored until the recipe to remake the target file @var{t}
10294 is executed.  After the recipe is executed, each of these stored file
10295 names are entered into the data base and marked as having been updated
10296 and having the same update status as the file @var{t}.
10298 When the recipe of a pattern rule is executed for @var{t}, the
10299 automatic variables are set corresponding to the target and
10300 prerequisites.  @xref{Automatic Variables}.
10302 @node Archives, Extending make, Implicit Rules, Top
10303 @chapter Using @code{make} to Update Archive Files
10304 @cindex archive
10306 @dfn{Archive files} are files containing named sub-files called
10307 @dfn{members}; they are maintained with the program @code{ar} and their
10308 main use is as subroutine libraries for linking.
10310 @menu
10311 * Archive Members::             Archive members as targets.
10312 * Archive Update::              The implicit rule for archive member targets.
10313 * Archive Pitfalls::            Dangers to watch out for when using archives.
10314 * Archive Suffix Rules::        You can write a special kind of suffix rule
10315                                   for updating archives.
10316 @end menu
10318 @node Archive Members, Archive Update, Archives, Archives
10319 @section Archive Members as Targets
10320 @cindex archive member targets
10322 An individual member of an archive file can be used as a target or
10323 prerequisite in @code{make}.  You specify the member named @var{member} in
10324 archive file @var{archive} as follows:
10326 @example
10327 @var{archive}(@var{member})
10328 @end example
10330 @noindent
10331 This construct is available only in targets and prerequisites, not in
10332 recipes!  Most programs that you might use in recipes do not support
10333 this syntax and cannot act directly on archive members.  Only
10334 @code{ar} and other programs specifically designed to operate on
10335 archives can do so.  Therefore, valid recipes to update an archive
10336 member target probably must use @code{ar}.  For example, this rule
10337 says to create a member @file{hack.o} in archive @file{foolib} by
10338 copying the file @file{hack.o}:
10340 @example
10341 foolib(hack.o) : hack.o
10342         ar cr foolib hack.o
10343 @end example
10345 In fact, nearly all archive member targets are updated in just this way
10346 and there is an implicit rule to do it for you.  @strong{Please note:} The
10347 @samp{c} flag to @code{ar} is required if the archive file does not
10348 already exist.
10350 To specify several members in the same archive, you can write all the
10351 member names together between the parentheses.  For example:
10353 @example
10354 foolib(hack.o kludge.o)
10355 @end example
10357 @noindent
10358 is equivalent to:
10360 @example
10361 foolib(hack.o) foolib(kludge.o)
10362 @end example
10364 @cindex wildcard, in archive member
10365 You can also use shell-style wildcards in an archive member reference.
10366 @xref{Wildcards, ,Using Wildcard Characters in File Names}.  For
10367 example, @w{@samp{foolib(*.o)}} expands to all existing members of the
10368 @file{foolib} archive whose names end in @samp{.o}; perhaps
10369 @samp{@w{foolib(hack.o)} @w{foolib(kludge.o)}}.
10371 @node Archive Update, Archive Pitfalls, Archive Members, Archives
10372 @section Implicit Rule for Archive Member Targets
10374 Recall that a target that looks like @file{@var{a}(@var{m})} stands for the
10375 member named @var{m} in the archive file @var{a}.
10377 When @code{make} looks for an implicit rule for such a target, as a special
10378 feature it considers implicit rules that match @file{(@var{m})}, as well as
10379 those that match the actual target @file{@var{a}(@var{m})}.
10381 This causes one special rule whose target is @file{(%)} to match.  This
10382 rule updates the target @file{@var{a}(@var{m})} by copying the file @var{m}
10383 into the archive.  For example, it will update the archive member target
10384 @file{foo.a(bar.o)} by copying the @emph{file} @file{bar.o} into the
10385 archive @file{foo.a} as a @emph{member} named @file{bar.o}.
10387 When this rule is chained with others, the result is very powerful.
10388 Thus, @samp{make "foo.a(bar.o)"} (the quotes are needed to protect the
10389 @samp{(} and @samp{)} from being interpreted specially by the shell) in
10390 the presence of a file @file{bar.c} is enough to cause the following
10391 recipe to be run, even without a makefile:
10393 @example
10394 cc -c bar.c -o bar.o
10395 ar r foo.a bar.o
10396 rm -f bar.o
10397 @end example
10399 @noindent
10400 Here @code{make} has envisioned the file @file{bar.o} as an intermediate
10401 file.  @xref{Chained Rules, ,Chains of Implicit Rules}.
10403 Implicit rules such as this one are written using the automatic variable
10404 @samp{$%}.  @xref{Automatic Variables}.
10406 An archive member name in an archive cannot contain a directory name, but
10407 it may be useful in a makefile to pretend that it does.  If you write an
10408 archive member target @file{foo.a(dir/file.o)}, @code{make} will perform
10409 automatic updating with this recipe:
10411 @example
10412 ar r foo.a dir/file.o
10413 @end example
10415 @noindent
10416 which has the effect of copying the file @file{dir/file.o} into a member
10417 named @file{file.o}.  In connection with such usage, the automatic variables
10418 @code{%D} and @code{%F} may be useful.
10420 @menu
10421 * Archive Symbols::             How to update archive symbol directories.
10422 @end menu
10424 @node Archive Symbols,  , Archive Update, Archive Update
10425 @subsection Updating Archive Symbol Directories
10426 @cindex @code{__.SYMDEF}
10427 @cindex updating archive symbol directories
10428 @cindex archive symbol directory updating
10429 @cindex symbol directories, updating archive
10430 @cindex directories, updating archive symbol
10432 An archive file that is used as a library usually contains a special member
10433 named @file{__.SYMDEF} that contains a directory of the external symbol
10434 names defined by all the other members.  After you update any other
10435 members, you need to update @file{__.SYMDEF} so that it will summarize the
10436 other members properly.  This is done by running the @code{ranlib} program:
10438 @example
10439 ranlib @var{archivefile}
10440 @end example
10442 Normally you would put this command in the rule for the archive file,
10443 and make all the members of the archive file prerequisites of that rule.
10444 For example,
10446 @example
10447 libfoo.a: libfoo.a(x.o) libfoo.a(y.o) @dots{}
10448         ranlib libfoo.a
10449 @end example
10451 @noindent
10452 The effect of this is to update archive members @file{x.o}, @file{y.o},
10453 etc., and then update the symbol directory member @file{__.SYMDEF} by
10454 running @code{ranlib}.  The rules for updating the members are not shown
10455 here; most likely you can omit them and use the implicit rule which copies
10456 files into the archive, as described in the preceding section.
10458 This is not necessary when using the GNU @code{ar} program, which
10459 updates the @file{__.SYMDEF} member automatically.
10461 @node Archive Pitfalls, Archive Suffix Rules, Archive Update, Archives
10462 @section Dangers When Using Archives
10463 @cindex archive, and parallel execution
10464 @cindex parallel execution, and archive update
10465 @cindex archive, and @code{-j}
10466 @cindex @code{-j}, and archive update
10468 It is important to be careful when using parallel execution (the
10469 @code{-j} switch; @pxref{Parallel, ,Parallel Execution}) and archives.
10470 If multiple @code{ar} commands run at the same time on the same archive
10471 file, they will not know about each other and can corrupt the file.
10473 Possibly a future version of @code{make} will provide a mechanism to
10474 circumvent this problem by serializing all recipes that operate on the
10475 same archive file.  But for the time being, you must either write your
10476 makefiles to avoid this problem in some other way, or not use @code{-j}.
10478 @node Archive Suffix Rules,  , Archive Pitfalls, Archives
10479 @section Suffix Rules for Archive Files
10480 @cindex suffix rule, for archive
10481 @cindex archive, suffix rule for
10482 @cindex library archive, suffix rule for
10483 @cindex @code{.a} (archives)
10485 You can write a special kind of suffix rule for dealing with archive
10486 files.  @xref{Suffix Rules}, for a full explanation of suffix rules.
10487 Archive suffix rules are obsolete in GNU @code{make}, because pattern
10488 rules for archives are a more general mechanism (@pxref{Archive
10489 Update}).  But they are retained for compatibility with other
10490 @code{make}s.
10492 To write a suffix rule for archives, you simply write a suffix rule
10493 using the target suffix @samp{.a} (the usual suffix for archive files).
10494 For example, here is the old-fashioned suffix rule to update a library
10495 archive from C source files:
10497 @example
10498 @group
10499 .c.a:
10500         $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o
10501         $(AR) r $@@ $*.o
10502         $(RM) $*.o
10503 @end group
10504 @end example
10506 @noindent
10507 This works just as if you had written the pattern rule:
10509 @example
10510 @group
10511 (%.o): %.c
10512         $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o
10513         $(AR) r $@@ $*.o
10514         $(RM) $*.o
10515 @end group
10516 @end example
10518 In fact, this is just what @code{make} does when it sees a suffix rule
10519 with @samp{.a} as the target suffix.  Any double-suffix rule
10520 @w{@samp{.@var{x}.a}} is converted to a pattern rule with the target
10521 pattern @samp{(%.o)} and a prerequisite pattern of @samp{%.@var{x}}.
10523 Since you might want to use @samp{.a} as the suffix for some other kind
10524 of file, @code{make} also converts archive suffix rules to pattern rules
10525 in the normal way (@pxref{Suffix Rules}).  Thus a double-suffix rule
10526 @w{@samp{.@var{x}.a}} produces two pattern rules: @samp{@w{(%.o):}
10527 @w{%.@var{x}}} and @samp{@w{%.a}: @w{%.@var{x}}}.@refill
10529 @node Extending make, Features, Archives, Top
10530 @chapter Extending GNU @code{make}
10531 @cindex make extensions
10533 GNU @code{make} provides many advanced capabilities, including many
10534 useful functions.  However, it does not contain a complete programming
10535 language and so it has limitations.  Sometimes these limitations can be
10536 overcome through use of the @code{shell} function to invoke a separate
10537 program, although this can be inefficient.
10539 In cases where the built-in capabilities of GNU @code{make} are
10540 insufficient to your requirements there are two options for extending
10541 @code{make}.  On systems where it's provided, you can utilize GNU
10542 Guile as an embedded scripting language (@pxref{Guile Integration,
10543 ,GNU Guile Integration}).  On systems which support dynamically
10544 loadable objects, you can write your own extension in any language
10545 (which can be compiled into such an object) and load it to provide
10546 extended capabilities (@pxref{load Directive, ,The @code{load} Directive}).
10548 @menu
10549 * Guile Integration::           Using Guile as an embedded scripting language.
10550 * Loading Objects::             Loading dynamic objects as extensions.
10551 @end menu
10553 @node Guile Integration, Loading Objects, Extending make, Extending make
10554 @section GNU Guile Integration
10555 @cindex Guile
10556 @cindex extensions, Guile
10558 GNU @code{make} may be built with support for GNU Guile as an embedded
10559 extension language.  Guile implements the Scheme language.  A review
10560 of GNU Guile and the Scheme language and its features is beyond the
10561 scope of this manual: see the documentation for GNU Guile and Scheme.
10563 You can determine if @code{make} contains support for Guile by
10564 examining the @code{.FEATURES} variable; it will contain the word
10565 @var{guile} if Guile support is available.
10567 The Guile integration provides one new @code{make} function: @code{guile}.
10568 The @code{guile} function takes one argument which is first expanded
10569 by @code{make} in the normal fashion, then passed to the GNU Guile
10570 evaluator.  The result of the evaluator is converted into a string and
10571 used as the expansion of the @code{guile} function in the makefile.
10573 In addition, GNU @code{make} exposes Guile procedures for use in Guile
10574 scripts.
10576 @menu
10577 * Guile Types::                 Converting Guile types to @code{make} strings.
10578 * Guile Interface::             Invoking @code{make} functions from Guile.
10579 * Guile Example::               Example using Guile in @code{make}.
10580 @end menu
10582 @node Guile Types, Guile Interface, Guile Integration, Guile Integration
10583 @subsection Conversion of Guile Types
10584 @cindex convert guile types
10585 @cindex guile, conversion of types
10586 @cindex types, conversion of
10588 There is only one ``data type'' in @code{make}: a string.  GNU Guile,
10589 on the other hand, provides a rich variety of different data types.
10590 An important aspect of the interface between @code{make} and GNU Guile
10591 is the conversion of Guile data types into @code{make} strings.
10593 This conversion is relevant in two places: when a makefile invokes the
10594 @code{guile} function to evaluate a Guile expression, the result of
10595 that evaluation must be converted into a make string so it can be
10596 further evaluated by @code{make}.  And secondly, when a Guile script
10597 invokes one of the procedures exported by @code{make} the argument
10598 provided to the procedure must be converted into a string.
10600 The conversion of Guile types into @code{make} strings is as below:
10602 @table @code
10603 @item #f
10604 False is converted into the empty string: in @code{make} conditionals
10605 the empty string is considered false.
10607 @item #t
10608 True is converted to the string @samp{#t}: in @code{make} conditionals
10609 any non-empty string is considered true.
10611 @item symbol
10612 @item number
10613 A symbol or number is converted into the string representation of that
10614 symbol or number.
10616 @item character
10617 A printable character is converted to the same character.
10619 @item string
10620 A string containing only printable characters is converted to the same
10621 string.
10623 @item list
10624 A list is converted recursively according to the above rules.  This
10625 implies that any structured list will be flattened (that is, a result
10626 of @samp{'(a b (c d) e)} will be converted to the @code{make} string
10627 @samp{a b c d e}).
10629 @item other
10630 Any other Guile type results in an error.  In future versions of
10631 @code{make}, other Guile types may be converted.
10633 @end table
10635 The translation of @samp{#f} (to the empty string) and @samp{#t} (to
10636 the non-empty string @samp{#t}) is designed to allow you to use Guile
10637 boolean results directly as @code{make} boolean conditions.  For
10638 example:
10640 @example
10641 $(if $(guile (access? "myfile" R_OK)),$(info myfile exists))
10642 @end example
10644 As a consequence of these conversion rules you must consider the
10645 result of your Guile script, as that result will be converted into a
10646 string and parsed by @code{make}.  If there is no natural result for
10647 the script (that is, the script exists solely for its side-effects),
10648 you should add @samp{#f} as the final expression in order to avoid
10649 syntax errors in your makefile.
10651 @node Guile Interface, Guile Example, Guile Types, Guile Integration
10652 @subsection Interfaces from Guile to @code{make}
10653 @cindex make interface to guile
10654 @cindex make procedures in guile
10656 In addition to the @code{guile} function available in makefiles,
10657 @code{make} exposes some procedures for use in your Guile scripts.  At
10658 startup @code{make} creates a new Guile module, @code{gnu make}, and
10659 exports these procedures as public interfaces from that module:
10661 @table @code
10662 @item gmk-expand
10663 This procedure takes a single argument which is converted into a
10664 string.  The string is expanded by @code{make} using normal
10665 @code{make} expansion rules.  The result of the expansion is converted
10666 into a Guile string and provided as the result of the procedure.
10668 @item gmk-eval
10669 This procedure takes a single argument which is converted into a
10670 string.  The string is evaluated by @code{make} as if it were a
10671 makefile.  This is the same capability available via the @code{eval}
10672 function (@pxref{Eval Function}).  The result of the @code{gmk-eval}
10673 procedure is always the empty string.
10675 @item gmk-var
10676 This procedure takes a single argument which is converted into a
10677 string.  The string is assumed to be the name of a @code{make}
10678 variable, which is then expanded.  The expansion is converted into a
10679 string and provided as the result of the procedure.
10681 @end table
10683 @node Guile Example,  , Guile Interface, Guile Integration
10684 @subsection Example Using Guile in @code{make}
10685 @cindex Guile example
10686 @cindex example using Guile
10688 Here is a very simple example using GNU Guile to manage writing to a
10689 file.  These Guile procedures simply open a file, allow writing to the
10690 file (one string per line), and close the file.  Note that because we
10691 cannot store complex values such as Guile ports in @code{make}
10692 variables, we'll keep the port as a global variable in the Guile
10693 interpreter.
10695 You can create Guile functions easily using @code{define}/@code{endef}
10696 to create a Guile script, then use the @code{guile} function to
10697 internalize it:
10699 @example
10700 @group
10701 define GUILEIO
10702 ;; A simple Guile IO library for GNU make
10704 (define MKPORT #f)
10706 (define (mkopen name mode)
10707   (set! MKPORT (open-file name mode))
10708   #f)
10710 (define (mkwrite s)
10711   (display s MKPORT)
10712   (newline MKPORT)
10713   #f)
10715 (define (mkclose)
10716   (close-port MKPORT)
10717   #f)
10720 endef
10722 # Internalize the Guile IO functions
10723 $(guile $(GUILEIO))
10724 @end group
10725 @end example
10727 If you have a significant amount of Guile support code, you might
10728 consider keeping it in a different file (e.g., @file{guileio.scm}) and
10729 then loading it in your makefile using the @code{guile} function:
10731 @example
10732 $(guile (load "guileio.scm"))
10733 @end example
10735 An advantage to this method is that when editing @file{guileio.scm},
10736 your editor will understand that this file contains Scheme syntax
10737 rather than makefile syntax.
10739 Now you can use these Guile functions to create files.  Suppose you
10740 need to operate on a very large list, which cannot fit on the command
10741 line, but the utility you're using accepts the list as input as well:
10743 @example
10744 @group
10745 prog: $(PREREQS)
10746         @@$(guile (mkopen "tmp.out" "w")) \
10747          $(foreach X,$^,$(guile (mkwrite "$(X)"))) \
10748          $(guile (mkclose))
10749         $(LINK) < tmp.out
10750 @end group
10751 @end example
10753 A more comprehensive suite of file manipulation procedures is possible
10754 of course.  You could, for example, maintain multiple output files at
10755 the same time by choosing a symbol for each one and using it as the
10756 key to a hash table, where the value is a port, then returning the
10757 symbol to be stored in a @code{make} variable.
10759 @node Loading Objects,  , Guile Integration, Extending make
10760 @section Loading Dynamic Objects
10761 @cindex loading objects
10762 @cindex objects, loading
10763 @cindex extensions, loading
10765 @cartouche
10766 @quotation Warning
10767 The @code{load} directive and extension capability is considered a
10768 ``technology preview'' in this release of GNU make.  We encourage you
10769 to experiment with this feature and we appreciate any feedback on it.
10770 However we cannot guarantee to maintain backward-compatibility in the
10771 next release.
10773 In particular, for this feature to be useful your extensions will need
10774 to invoke various functions internal to GNU @code{make}.  In this
10775 release there is no stable programming interface defined for
10776 @code{make}: any internal function may change or even disappear in
10777 future releases.
10778 @end quotation
10779 @end cartouche
10781 Many operating systems provide a facility for dynamically loading
10782 compiled objects.  If your system provides this facility, GNU
10783 @code{make} can make use of it to load dynamic objects at runtime,
10784 providing new capabilities which may then be invoked by your makefile.
10786 The @code{load} directive is used to load a dynamic object.  Once the
10787 object is loaded, a ``setup'' function will be invoked to allow the
10788 object to initialize itself and register new facilities with GNU
10789 @code{make}.  Typically a dynamic object would create new functions,
10790 for example, and the ``setup'' function would register them with GNU
10791 @code{make}'s function handling system.
10793 @menu
10794 * load Directive::              Loading dynamic objects as extensions.
10795 @end menu
10797 @node load Directive,  , Loading Objects, Loading Objects
10798 @subsection The @code{load} Directive
10799 @cindex load directive
10800 @cindex extensions, load directive
10802 Objects are loaded into GNU @code{make} by placing the @code{load}
10803 directive into your makefile.  The syntax of the @code{load} directive
10804 is as follows:
10806 @findex load
10807 @example
10808 load @var{object-file} @dots{}
10809 @end example
10813 @example
10814 load @var{object-file}(@var{symbol-name}) @dots{}
10815 @end example
10817 In the first form, the file @var{object-file} is dynamically loaded by
10818 GNU @code{make}.  On failure, @code{make} will print a message and
10819 exit.  If the load succeeds @code{make} will invoke an initializing
10820 function whose name is created by taking the base file name of
10821 @var{object-file}, up to the first character which is not a valid
10822 symbol name character (alphanumerics and underscores are valid symbol
10823 name characters).  To this prefix will be appended the suffix
10824 @code{_gmake_setup}, then this symbol will be invoked.
10826 In the second form, the function @var{symbol-name} will be invoked.
10828 More than one object file may be loaded with a single @code{load}
10829 directive, and both forms of @code{load} arguments may be used in the
10830 same directive.
10832 For example:
10834 @example
10835 load ../mk_funcs.so
10836 @end example
10838 will load the dynamic object @file{../mk_funcs.so}.  After the object
10839 is loaded, @code{make} will invoke the function (assumed to be defined
10840 by the shared object) @code{mk_funcs_gmake_setup}.
10842 On the other hand:
10844 @example
10845 load ../mk_funcs.so(init_mk_func)
10846 @end example
10848 will load the dynamic object @file{../mk_funcs.so}.  After the object
10849 is loaded, @code{make} will invoke the function @code{init_mk_func}.
10851 Regardless of how many times an object file appears in a @code{load}
10852 directive, it will only be loaded (and it's setup function will only
10853 be invoked) once.
10855 @vindex .LOADED
10856 After an object has been successfully loaded, its file name is
10857 appended to the @code{.LOADED} variable.
10859 @findex -load
10860 If you would prefer that failure to load a dynamic object not be
10861 reported as an error, you can use the @code{-load} directive instead
10862 of @code{load}.  GNU @code{make} will not fail and no message will be
10863 generated if an object fails to load.  The failed object is not added
10864 to the @code{.LOADED} variable, which can then be consulted to
10865 determine if the load was successful.
10867 @node Features, Missing, Extending make, Top
10868 @chapter Features of GNU @code{make}
10869 @cindex features of GNU @code{make}
10870 @cindex portability
10871 @cindex compatibility
10873 Here is a summary of the features of GNU @code{make}, for comparison
10874 with and credit to other versions of @code{make}.  We consider the
10875 features of @code{make} in 4.2 BSD systems as a baseline.  If you are
10876 concerned with writing portable makefiles, you should not use the
10877 features of @code{make} listed here, nor the ones in @ref{Missing}.
10879 Many features come from the version of @code{make} in System V.
10881 @itemize @bullet
10882 @item
10883 The @code{VPATH} variable and its special meaning.
10884 @xref{Directory Search, , Searching Directories for Prerequisites}.
10885 This feature exists in System V @code{make}, but is undocumented.
10886 It is documented in 4.3 BSD @code{make} (which says it mimics System V's
10887 @code{VPATH} feature).@refill
10889 @item
10890 Included makefiles.  @xref{Include, ,Including Other Makefiles}.
10891 Allowing multiple files to be included with a single directive is a GNU
10892 extension.
10894 @item
10895 Variables are read from and communicated via the environment.
10896 @xref{Environment, ,Variables from the Environment}.
10898 @item
10899 Options passed through the variable @code{MAKEFLAGS} to recursive
10900 invocations of @code{make}.
10901 @xref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
10903 @item
10904 The automatic variable @code{$%} is set to the member name
10905 in an archive reference.  @xref{Automatic Variables}.
10907 @item
10908 The automatic variables @code{$@@}, @code{$*}, @code{$<}, @code{$%},
10909 and @code{$?} have corresponding forms like @code{$(@@F)} and
10910 @code{$(@@D)}.  We have generalized this to @code{$^} as an obvious
10911 extension.  @xref{Automatic Variables}.@refill
10913 @item
10914 Substitution variable references.
10915 @xref{Reference, ,Basics of Variable References}.
10917 @item
10918 The command line options @samp{-b} and @samp{-m}, accepted and
10919 ignored.  In System V @code{make}, these options actually do something.
10921 @item
10922 Execution of recursive commands to run @code{make} via the variable
10923 @code{MAKE} even if @samp{-n}, @samp{-q} or @samp{-t} is specified.
10924 @xref{Recursion, ,Recursive Use of @code{make}}.
10926 @item
10927 Support for suffix @samp{.a} in suffix rules.  @xref{Archive Suffix
10928 Rules}.  This feature is obsolete in GNU @code{make}, because the
10929 general feature of rule chaining (@pxref{Chained Rules, ,Chains of
10930 Implicit Rules}) allows one pattern rule for installing members in an
10931 archive (@pxref{Archive Update}) to be sufficient.
10933 @item
10934 The arrangement of lines and backslash/newline combinations in
10935 recipes is retained when the recipes are printed, so they appear as
10936 they do in the makefile, except for the stripping of initial
10937 whitespace.
10938 @end itemize
10940 The following features were inspired by various other versions of
10941 @code{make}.  In some cases it is unclear exactly which versions inspired
10942 which others.
10944 @itemize @bullet
10945 @item
10946 Pattern rules using @samp{%}.
10947 This has been implemented in several versions of @code{make}.
10948 We're not sure who invented it first, but it's been spread around a bit.
10949 @xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.@refill
10951 @item
10952 Rule chaining and implicit intermediate files.
10953 This was implemented by Stu Feldman in his version of @code{make}
10954 for AT&T Eighth Edition Research Unix, and later by Andrew Hume of
10955 AT&T Bell Labs in his @code{mk} program (where he terms it
10956 ``transitive closure'').  We do not really know if
10957 we got this from either of them or thought it up ourselves at the
10958 same time.  @xref{Chained Rules, ,Chains of Implicit Rules}.
10960 @item
10961 The automatic variable @code{$^} containing a list of all prerequisites
10962 of the current target.  We did not invent this, but we have no idea who
10963 did.  @xref{Automatic Variables}.  The automatic variable
10964 @code{$+} is a simple extension of @code{$^}.
10966 @item
10967 The ``what if'' flag (@samp{-W} in GNU @code{make}) was (as far as we know)
10968 invented by Andrew Hume in @code{mk}.
10969 @xref{Instead of Execution, ,Instead of Executing Recipes}.
10971 @item
10972 The concept of doing several things at once (parallelism) exists in
10973 many incarnations of @code{make} and similar programs, though not in the
10974 System V or BSD implementations.  @xref{Execution, ,Recipe Execution}.
10976 @item
10977 Modified variable references using pattern substitution come from
10978 SunOS 4.  @xref{Reference, ,Basics of Variable References}.
10979 This functionality was provided in GNU @code{make} by the
10980 @code{patsubst} function before the alternate syntax was implemented
10981 for compatibility with SunOS 4.  It is not altogether clear who
10982 inspired whom, since GNU @code{make} had @code{patsubst} before SunOS
10983 4 was released.@refill
10985 @item
10986 The special significance of @samp{+} characters preceding recipe lines
10987 (@pxref{Instead of Execution, ,Instead of Executing Recipes}) is
10988 mandated by @cite{IEEE Standard 1003.2-1992} (POSIX.2).
10990 @item
10991 The @samp{+=} syntax to append to the value of a variable comes from SunOS
10992 4 @code{make}.  @xref{Appending, , Appending More Text to Variables}.
10994 @item
10995 The syntax @w{@samp{@var{archive}(@var{mem1} @var{mem2}@dots{})}} to list
10996 multiple members in a single archive file comes from SunOS 4 @code{make}.
10997 @xref{Archive Members}.
10999 @item
11000 The @code{-include} directive to include makefiles with no error for a
11001 nonexistent file comes from SunOS 4 @code{make}.  (But note that SunOS 4
11002 @code{make} does not allow multiple makefiles to be specified in one
11003 @code{-include} directive.)  The same feature appears with the name
11004 @code{sinclude} in SGI @code{make} and perhaps others.
11006 @item
11007 The @code{!=} shell assignment operator exists in many BSD of
11008 @code{make} and is purposefully implemented here to behave identically
11009 to those implementations.
11010 @end itemize
11012 The remaining features are inventions new in GNU @code{make}:
11014 @itemize @bullet
11015 @item
11016 Use the @samp{-v} or @samp{--version} option to print version and
11017 copyright information.
11019 @item
11020 Use the @samp{-h} or @samp{--help} option to summarize the options to
11021 @code{make}.
11023 @item
11024 Simply-expanded variables.  @xref{Flavors, ,The Two Flavors of Variables}.
11026 @item
11027 Pass command line variable assignments automatically through the
11028 variable @code{MAKE} to recursive @code{make} invocations.
11029 @xref{Recursion, ,Recursive Use of @code{make}}.
11031 @item
11032 Use the @samp{-C} or @samp{--directory} command option to change
11033 directory.  @xref{Options Summary, ,Summary of Options}.
11035 @item
11036 Make verbatim variable definitions with @code{define}.
11037 @xref{Multi-Line, ,Defining Multi-Line Variables}.
11039 @item
11040 Declare phony targets with the special target @code{.PHONY}.
11042 Andrew Hume of AT&T Bell Labs implemented a similar feature with a
11043 different syntax in his @code{mk} program.  This seems to be a case of
11044 parallel discovery.  @xref{Phony Targets, ,Phony Targets}.
11046 @item
11047 Manipulate text by calling functions.
11048 @xref{Functions, ,Functions for Transforming Text}.
11050 @item
11051 Use the @samp{-o} or @samp{--old-file}
11052 option to pretend a file's modification-time is old.
11053 @xref{Avoiding Compilation, ,Avoiding Recompilation of Some Files}.
11055 @item
11056 Conditional execution.
11058 This feature has been implemented numerous times in various versions
11059 of @code{make}; it seems a natural extension derived from the features
11060 of the C preprocessor and similar macro languages and is not a
11061 revolutionary concept.  @xref{Conditionals, ,Conditional Parts of Makefiles}.
11063 @item
11064 Specify a search path for included makefiles.
11065 @xref{Include, ,Including Other Makefiles}.
11067 @item
11068 Specify extra makefiles to read with an environment variable.
11069 @xref{MAKEFILES Variable, ,The Variable @code{MAKEFILES}}.
11071 @item
11072 Strip leading sequences of @samp{./} from file names, so that
11073 @file{./@var{file}} and @file{@var{file}} are considered to be the
11074 same file.@refill
11076 @item
11077 Use a special search method for library prerequisites written in the
11078 form @samp{-l@var{name}}.
11079 @xref{Libraries/Search, ,Directory Search for Link Libraries}.
11081 @item
11082 Allow suffixes for suffix rules
11083 (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}) to contain any
11084 characters.  In other versions of @code{make}, they must begin with
11085 @samp{.} and not contain any @samp{/} characters.
11087 @item
11088 Keep track of the current level of @code{make} recursion using the
11089 variable @code{MAKELEVEL}.  @xref{Recursion, ,Recursive Use of @code{make}}.
11091 @item
11092 Provide any goals given on the command line in the variable
11093 @code{MAKECMDGOALS}.  @xref{Goals, ,Arguments to Specify the Goals}.
11095 @item
11096 Specify static pattern rules.  @xref{Static Pattern, ,Static Pattern Rules}.
11098 @item
11099 Provide selective @code{vpath} search.
11100 @xref{Directory Search, ,Searching Directories for Prerequisites}.
11102 @item
11103 Provide computed variable references.
11104 @xref{Reference, ,Basics of Variable References}.
11106 @item
11107 Update makefiles.  @xref{Remaking Makefiles, ,How Makefiles Are Remade}.
11108 System V @code{make} has a very, very limited form of this
11109 functionality in that it will check out SCCS files for makefiles.
11111 @item
11112 Various new built-in implicit rules.
11113 @xref{Catalogue of Rules, ,Catalogue of Implicit Rules}.
11115 @item
11116 The built-in variable @samp{MAKE_VERSION} gives the version number of
11117 @code{make}.
11118 @vindex MAKE_VERSION
11119 @end itemize
11121 @node Missing, Makefile Conventions, Features, Top
11122 @chapter Incompatibilities and Missing Features
11123 @cindex incompatibilities
11124 @cindex missing features
11125 @cindex features, missing
11127 The @code{make} programs in various other systems support a few features
11128 that are not implemented in GNU @code{make}.  The POSIX.2 standard
11129 (@cite{IEEE Standard 1003.2-1992}) which specifies @code{make} does not
11130 require any of these features.@refill
11132 @itemize @bullet
11133 @item
11134 A target of the form @samp{@var{file}((@var{entry}))} stands for a member
11135 of archive file @var{file}.  The member is chosen, not by name, but by
11136 being an object file which defines the linker symbol @var{entry}.@refill
11138 This feature was not put into GNU @code{make} because of the
11139 non-modularity of putting knowledge into @code{make} of the internal
11140 format of archive file symbol tables.
11141 @xref{Archive Symbols, ,Updating Archive Symbol Directories}.
11143 @item
11144 Suffixes (used in suffix rules) that end with the character @samp{~}
11145 have a special meaning to System V @code{make};
11146 they refer to the SCCS file that corresponds
11147 to the file one would get without the @samp{~}.  For example, the
11148 suffix rule @samp{.c~.o} would make the file @file{@var{n}.o} from
11149 the SCCS file @file{s.@var{n}.c}.  For complete coverage, a whole
11150 series of such suffix rules is required.
11151 @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
11153 In GNU @code{make}, this entire series of cases is handled by two
11154 pattern rules for extraction from SCCS, in combination with the
11155 general feature of rule chaining.
11156 @xref{Chained Rules, ,Chains of Implicit Rules}.
11158 @item
11159 In System V and 4.3 BSD @code{make}, files found by @code{VPATH}
11160 search (@pxref{Directory Search, ,Searching Directories for
11161 Prerequisites}) have their names changed inside recipes.  We feel it
11162 is much cleaner to always use automatic variables and thus make this
11163 feature obsolete.@refill
11165 @item
11166 In some Unix @code{make}s, the automatic variable @code{$*} appearing in
11167 the prerequisites of a rule has the amazingly strange ``feature'' of
11168 expanding to the full name of the @emph{target of that rule}.  We cannot
11169 imagine what went on in the minds of Unix @code{make} developers to do
11170 this; it is utterly inconsistent with the normal definition of @code{$*}.
11171 @vindex * @r{(automatic variable), unsupported bizarre usage}
11173 @item
11174 In some Unix @code{make}s, implicit rule search (@pxref{Implicit
11175 Rules, ,Using Implicit Rules}) is apparently done for @emph{all}
11176 targets, not just those without recipes.  This means you can
11177 do:@refill
11179 @example
11180 @group
11181 foo.o:
11182         cc -c foo.c
11183 @end group
11184 @end example
11186 @noindent
11187 and Unix @code{make} will intuit that @file{foo.o} depends on
11188 @file{foo.c}.@refill
11190 We feel that such usage is broken.  The prerequisite properties of
11191 @code{make} are well-defined (for GNU @code{make}, at least),
11192 and doing such a thing simply does not fit the model.@refill
11194 @item
11195 GNU @code{make} does not include any built-in implicit rules for
11196 compiling or preprocessing EFL programs.  If we hear of anyone who is
11197 using EFL, we will gladly add them.
11199 @item
11200 It appears that in SVR4 @code{make}, a suffix rule can be specified
11201 with no recipe, and it is treated as if it had an empty recipe
11202 (@pxref{Empty Recipes}).  For example:
11204 @example
11205 .c.a:
11206 @end example
11208 @noindent
11209 will override the built-in @file{.c.a} suffix rule.
11211 We feel that it is cleaner for a rule without a recipe to always simply
11212 add to the prerequisite list for the target.  The above example can be
11213 easily rewritten to get the desired behavior in GNU @code{make}:
11215 @example
11216 .c.a: ;
11217 @end example
11219 @item
11220 Some versions of @code{make} invoke the shell with the @samp{-e} flag,
11221 except under @samp{-k} (@pxref{Testing, ,Testing the Compilation of a
11222 Program}).  The @samp{-e} flag tells the shell to exit as soon as any
11223 program it runs returns a nonzero status.  We feel it is cleaner to
11224 write each line of the recipe to stand on its own and not require this
11225 special treatment.
11226 @end itemize
11228 @comment The makefile standards are in a separate file that is also
11229 @comment included by standards.texi.
11230 @include make-stds.texi
11232 @node Quick Reference, Error Messages, Makefile Conventions, Top
11233 @appendix Quick Reference
11235 This appendix summarizes the directives, text manipulation functions,
11236 and special variables which GNU @code{make} understands.
11237 @xref{Special Targets}, @ref{Catalogue of Rules, ,Catalogue of Implicit Rules},
11238 and @ref{Options Summary, ,Summary of Options},
11239 for other summaries.
11241 Here is a summary of the directives GNU @code{make} recognizes:
11243 @table @code
11244 @item define @var{variable}
11245 @itemx define @var{variable} =
11246 @itemx define @var{variable} :=
11247 @itemx define @var{variable} ::=
11248 @itemx define @var{variable} +=
11249 @itemx define @var{variable} ?=
11250 @itemx endef
11251 Define multi-line variables.@*
11252 @xref{Multi-Line}.
11254 @item undefine @var{variable}
11255 Undefining variables.@*
11256 @xref{Undefine Directive}.
11258 @item ifdef @var{variable}
11259 @itemx ifndef @var{variable}
11260 @itemx ifeq (@var{a},@var{b})
11261 @itemx ifeq "@var{a}" "@var{b}"
11262 @itemx ifeq '@var{a}' '@var{b}'
11263 @itemx ifneq (@var{a},@var{b})
11264 @itemx ifneq "@var{a}" "@var{b}"
11265 @itemx ifneq '@var{a}' '@var{b}'
11266 @itemx else
11267 @itemx endif
11268 Conditionally evaluate part of the makefile.@*
11269 @xref{Conditionals}.
11271 @item include @var{file}
11272 @itemx -include @var{file}
11273 @itemx sinclude @var{file}
11274 Include another makefile.@*
11275 @xref{Include, ,Including Other Makefiles}.
11277 @item override @var{variable-assignment}
11278 Define a variable, overriding any previous definition, even one from
11279 the command line.@*
11280 @xref{Override Directive, ,The @code{override} Directive}.
11282 @item export
11283 Tell @code{make} to export all variables to child processes by default.@*
11284 @xref{Variables/Recursion, , Communicating Variables to a Sub-@code{make}}.
11286 @item export @var{variable}
11287 @itemx export @var{variable-assignment}
11288 @itemx unexport @var{variable}
11289 Tell @code{make} whether or not to export a particular variable to child
11290 processes.@*
11291 @xref{Variables/Recursion, , Communicating Variables to a Sub-@code{make}}.
11293 @item private @var{variable-assignment}
11294 Do not allow this variable assignment to be inherited by prerequisites.@*
11295 @xref{Suppressing Inheritance}.
11297 @item vpath @var{pattern} @var{path}
11298 Specify a search path for files matching a @samp{%} pattern.@*
11299 @xref{Selective Search, , The @code{vpath} Directive}.
11301 @item vpath @var{pattern}
11302 Remove all search paths previously specified for @var{pattern}.
11304 @item vpath
11305 Remove all search paths previously specified in any @code{vpath}
11306 directive.
11307 @end table
11309 Here is a summary of the built-in functions (@pxref{Functions}):
11311 @table @code
11312 @item $(subst @var{from},@var{to},@var{text})
11313 Replace @var{from} with @var{to} in @var{text}.@*
11314 @xref{Text Functions, , Functions for String Substitution and Analysis}.
11316 @item $(patsubst @var{pattern},@var{replacement},@var{text})
11317 Replace words matching @var{pattern} with @var{replacement} in @var{text}.@*
11318 @xref{Text Functions, , Functions for String Substitution and Analysis}.
11320 @item $(strip @var{string})
11321 Remove excess whitespace characters from @var{string}.@*
11322 @xref{Text Functions, , Functions for String Substitution and Analysis}.
11324 @item $(findstring @var{find},@var{text})
11325 Locate @var{find} in @var{text}.@*
11326 @xref{Text Functions, , Functions for String Substitution and Analysis}.
11328 @item $(filter @var{pattern}@dots{},@var{text})
11329 Select words in @var{text} that match one of the @var{pattern} words.@*
11330 @xref{Text Functions, , Functions for String Substitution and Analysis}.
11332 @item $(filter-out @var{pattern}@dots{},@var{text})
11333 Select words in @var{text} that @emph{do not} match any of the @var{pattern} words.@*
11334 @xref{Text Functions, , Functions for String Substitution and Analysis}.
11336 @item $(sort @var{list})
11337 Sort the words in @var{list} lexicographically, removing duplicates.@*
11338 @xref{Text Functions, , Functions for String Substitution and Analysis}.
11340 @item $(word @var{n},@var{text})
11341 Extract the @var{n}th word (one-origin) of @var{text}.@*
11342 @xref{Text Functions, , Functions for String Substitution and Analysis}.
11344 @item $(words @var{text})
11345 Count the number of words in @var{text}.@*
11346 @xref{Text Functions, , Functions for String Substitution and Analysis}.
11348 @item $(wordlist @var{s},@var{e},@var{text})
11349 Returns the list of words in @var{text} from @var{s} to @var{e}.@*
11350 @xref{Text Functions, , Functions for String Substitution and Analysis}.
11352 @item $(firstword @var{names}@dots{})
11353 Extract the first word of @var{names}.@*
11354 @xref{Text Functions, , Functions for String Substitution and Analysis}.
11356 @item $(lastword @var{names}@dots{})
11357 Extract the last word of @var{names}.@*
11358 @xref{Text Functions, , Functions for String Substitution and Analysis}.
11360 @item $(dir @var{names}@dots{})
11361 Extract the directory part of each file name.@*
11362 @xref{File Name Functions, ,Functions for File Names}.
11364 @item $(notdir @var{names}@dots{})
11365 Extract the non-directory part of each file name.@*
11366 @xref{File Name Functions, ,Functions for File Names}.
11368 @item $(suffix @var{names}@dots{})
11369 Extract the suffix (the last @samp{.} and following characters) of each file name.@*
11370 @xref{File Name Functions, ,Functions for File Names}.
11372 @item $(basename @var{names}@dots{})
11373 Extract the base name (name without suffix) of each file name.@*
11374 @xref{File Name Functions, ,Functions for File Names}.
11376 @item $(addsuffix @var{suffix},@var{names}@dots{})
11377 Append @var{suffix} to each word in @var{names}.@*
11378 @xref{File Name Functions, ,Functions for File Names}.
11380 @item $(addprefix @var{prefix},@var{names}@dots{})
11381 Prepend @var{prefix} to each word in @var{names}.@*
11382 @xref{File Name Functions, ,Functions for File Names}.
11384 @item $(join @var{list1},@var{list2})
11385 Join two parallel lists of words.@*
11386 @xref{File Name Functions, ,Functions for File Names}.
11388 @item $(wildcard @var{pattern}@dots{})
11389 Find file names matching a shell file name pattern (@emph{not} a
11390 @samp{%} pattern).@*
11391 @xref{Wildcard Function, ,The Function @code{wildcard}}.
11393 @item $(realpath @var{names}@dots{})
11394 For each file name in @var{names}, expand to an absolute name that
11395 does not contain any @code{.}, @code{..}, nor symlinks.@*
11396 @xref{File Name Functions, ,Functions for File Names}.
11398 @item $(abspath @var{names}@dots{})
11399 For each file name in @var{names}, expand to an absolute name that
11400 does not contain any @code{.} or @code{..} components, but preserves
11401 symlinks.@*
11402 @xref{File Name Functions, ,Functions for File Names}.
11404 @item $(error @var{text}@dots{})
11405 When this function is evaluated, @code{make} generates a fatal error
11406 with the message @var{text}.@*
11407 @xref{Make Control Functions, ,Functions That Control Make}.
11409 @item $(warning @var{text}@dots{})
11410 When this function is evaluated, @code{make} generates a warning with
11411 the message @var{text}.@*
11412 @xref{Make Control Functions, ,Functions That Control Make}.
11414 @item $(shell @var{command})
11415 Execute a shell command and return its output.@*
11416 @xref{Shell Function, , The @code{shell} Function}.
11418 @item $(origin @var{variable})
11419 Return a string describing how the @code{make} variable @var{variable} was
11420 defined.@*
11421 @xref{Origin Function, , The @code{origin} Function}.
11423 @item $(flavor @var{variable})
11424 Return a string describing the flavor of the @code{make} variable
11425 @var{variable}.@*
11426 @xref{Flavor Function, , The @code{flavor} Function}.
11428 @item $(foreach @var{var},@var{words},@var{text})
11429 Evaluate @var{text} with @var{var} bound to each word in @var{words},
11430 and concatenate the results.@*
11431 @xref{Foreach Function, ,The @code{foreach} Function}.
11433 @item $(if @var{condition},@var{then-part}[,@var{else-part}])
11434 Evaluate the condition @var{condition}; if it's non-empty substitute
11435 the expansion of the @var{then-part} otherwise substitute the
11436 expansion of the @var{else-part}.@*
11437 @xref{Conditional Functions, ,Functions for Conditionals}.
11439 @item $(or @var{condition1}[,@var{condition2}[,@var{condition3}@dots{}]])
11440 Evaluate each condition @var{conditionN} one at a time; substitute the
11441 first non-empty expansion.  If all expansions are empty, substitute
11442 the empty string.@*
11443 @xref{Conditional Functions, ,Functions for Conditionals}.
11445 @item $(and @var{condition1}[,@var{condition2}[,@var{condition3}@dots{}]])
11446 Evaluate each condition @var{conditionN} one at a time; if any
11447 expansion results in the empty string substitute the empty string.  If
11448 all expansions result in a non-empty string, substitute the expansion
11449 of the last @var{condition}.@*
11450 @xref{Conditional Functions, ,Functions for Conditionals}.
11452 @item $(call @var{var},@var{param},@dots{})
11453 Evaluate the variable @var{var} replacing any references to @code{$(1)},
11454 @code{$(2)} with the first, second, etc.@: @var{param} values.@*
11455 @xref{Call Function, ,The @code{call} Function}.
11457 @item $(eval @var{text})
11458 Evaluate @var{text} then read the results as makefile commands.
11459 Expands to the empty string.@*
11460 @xref{Eval Function, ,The @code{eval} Function}.
11462 @item $(value @var{var})
11463 Evaluates to the contents of the variable @var{var}, with no expansion
11464 performed on it.@*
11465 @xref{Value Function, ,The @code{value} Function}.
11466 @end table
11468 Here is a summary of the automatic variables.
11469 @xref{Automatic Variables},
11470 for full information.
11472 @table @code
11473 @item $@@
11474 The file name of the target.
11476 @item $%
11477 The target member name, when the target is an archive member.
11479 @item $<
11480 The name of the first prerequisite.
11482 @item $?
11483 The names of all the prerequisites that are
11484 newer than the target, with spaces between them.
11485 For prerequisites which are archive members, only
11486 the named member is used (@pxref{Archives}).
11488 @item $^
11489 @itemx $+
11490 The names of all the prerequisites, with spaces between them.  For
11491 prerequisites which are archive members, only the named member is used
11492 (@pxref{Archives}).  The value of @code{$^} omits duplicate
11493 prerequisites, while @code{$+} retains them and preserves their order.
11495 @item $*
11496 The stem with which an implicit rule matches
11497 (@pxref{Pattern Match, ,How Patterns Match}).
11499 @item $(@@D)
11500 @itemx $(@@F)
11501 The directory part and the file-within-directory part of @code{$@@}.
11503 @item $(*D)
11504 @itemx $(*F)
11505 The directory part and the file-within-directory part of @code{$*}.
11507 @item $(%D)
11508 @itemx $(%F)
11509 The directory part and the file-within-directory part of @code{$%}.
11511 @item $(<D)
11512 @itemx $(<F)
11513 The directory part and the file-within-directory part of @code{$<}.
11515 @item $(^D)
11516 @itemx $(^F)
11517 The directory part and the file-within-directory part of @code{$^}.
11519 @item $(+D)
11520 @itemx $(+F)
11521 The directory part and the file-within-directory part of @code{$+}.
11523 @item $(?D)
11524 @itemx $(?F)
11525 The directory part and the file-within-directory part of @code{$?}.
11526 @end table
11528 These variables are used specially by GNU @code{make}:
11530 @table @code
11531 @item MAKEFILES
11533 Makefiles to be read on every invocation of @code{make}.@*
11534 @xref{MAKEFILES Variable, ,The Variable @code{MAKEFILES}}.
11536 @item VPATH
11538 Directory search path for files not found in the current directory.@*
11539 @xref{General Search, , @code{VPATH} Search Path for All Prerequisites}.
11541 @item SHELL
11543 The name of the system default command interpreter, usually @file{/bin/sh}.
11544 You can set @code{SHELL} in the makefile to change the shell used to run
11545 recipes.  @xref{Execution, ,Recipe Execution}.  The @code{SHELL}
11546 variable is handled specially when importing from and exporting to the
11547 environment.  @xref{Choosing the Shell}.
11549 @item MAKESHELL
11551 On MS-DOS only, the name of the command interpreter that is to be used
11552 by @code{make}.  This value takes precedence over the value of
11553 @code{SHELL}.  @xref{Execution, ,MAKESHELL variable}.
11555 @item MAKE
11557 The name with which @code{make} was invoked.  Using this variable in
11558 recipes has special meaning.  @xref{MAKE Variable, ,How the
11559 @code{MAKE} Variable Works}.
11561 @item MAKELEVEL
11563 The number of levels of recursion (sub-@code{make}s).@*
11564 @xref{Variables/Recursion}.
11566 @item MAKEFLAGS
11568 The flags given to @code{make}.  You can set this in the environment or
11569 a makefile to set flags.@*
11570 @xref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
11572 It is @emph{never} appropriate to use @code{MAKEFLAGS} directly in a
11573 recipe line: its contents may not be quoted correctly for use in the
11574 shell.  Always allow recursive @code{make}'s to obtain these values
11575 through the environment from its parent.
11577 @item MAKECMDGOALS
11579 The targets given to @code{make} on the command line.  Setting this
11580 variable has no effect on the operation of @code{make}.@*
11581 @xref{Goals, ,Arguments to Specify the Goals}.
11583 @item CURDIR
11585 Set to the pathname of the current working directory (after all
11586 @code{-C} options are processed, if any).  Setting this variable has no
11587 effect on the operation of @code{make}.@*
11588 @xref{Recursion, ,Recursive Use of @code{make}}.
11590 @item SUFFIXES
11592 The default list of suffixes before @code{make} reads any makefiles.
11594 @item .LIBPATTERNS
11595 Defines the naming of the libraries @code{make} searches for, and their
11596 order.@*
11597 @xref{Libraries/Search, ,Directory Search for Link Libraries}.
11598 @end table
11600 @node Error Messages, Complex Makefile, Quick Reference, Top
11601 @comment  node-name,  next,  previous,  up
11602 @appendix Errors Generated by Make
11604 Here is a list of the more common errors you might see generated by
11605 @code{make}, and some information about what they mean and how to fix
11606 them.
11608 Sometimes @code{make} errors are not fatal, especially in the presence
11609 of a @code{-} prefix on a recipe line, or the @code{-k} command line
11610 option.  Errors that are fatal are prefixed with the string
11611 @code{***}.
11613 Error messages are all either prefixed with the name of the program
11614 (usually @samp{make}), or, if the error is found in a makefile, the name
11615 of the file and line number containing the problem.
11617 In the table below, these common prefixes are left off.
11619 @table @samp
11621 @item [@var{foo}] Error @var{NN}
11622 @itemx [@var{foo}] @var{signal description}
11623 These errors are not really @code{make} errors at all.  They mean that a
11624 program that @code{make} invoked as part of a recipe returned a
11625 non-0 error code (@samp{Error @var{NN}}), which @code{make} interprets
11626 as failure, or it exited in some other abnormal fashion (with a
11627 signal of some type).  @xref{Errors, ,Errors in Recipes}.
11629 If no @code{***} is attached to the message, then the sub-process failed
11630 but the rule in the makefile was prefixed with the @code{-} special
11631 character, so @code{make} ignored the error.
11633 @item missing separator.  Stop.
11634 @itemx missing separator (did you mean TAB instead of 8 spaces?).  Stop.
11635 This means that @code{make} could not understand much of anything
11636 about the makefile line it just read.  GNU @code{make} looks for
11637 various separators (@code{:}, @code{=}, recipe prefix characters,
11638 etc.) to indicate what kind of line it's parsing.  This message means
11639 it couldn't find a valid one.
11641 One of the most common reasons for this message is that you (or
11642 perhaps your oh-so-helpful editor, as is the case with many MS-Windows
11643 editors) have attempted to indent your recipe lines with spaces
11644 instead of a tab character.  In this case, @code{make} will use the
11645 second form of the error above.  Remember that every line in the
11646 recipe must begin with a tab character (unless you set
11647 @code{.RECIPEPREFIX}; @pxref{Special Variables}).  Eight spaces do not
11648 count.  @xref{Rule Syntax}.
11650 @item recipe commences before first target.  Stop.
11651 @itemx missing rule before recipe.  Stop.
11652 This means the first thing in the makefile seems to be part of a
11653 recipe: it begins with a recipe prefix character and doesn't appear to
11654 be a legal @code{make} directive (such as a variable assignment).
11655 Recipes must always be associated with a target.
11657 The second form is generated if the line has a semicolon as the first
11658 non-whitespace character; @code{make} interprets this to mean you left
11659 out the "target: prerequisite" section of a rule.  @xref{Rule Syntax}.
11661 @item No rule to make target `@var{xxx}'.
11662 @itemx No rule to make target `@var{xxx}', needed by `@var{yyy}'.
11663 This means that @code{make} decided it needed to build a target, but
11664 then couldn't find any instructions in the makefile on how to do that,
11665 either explicit or implicit (including in the default rules database).
11667 If you want that file to be built, you will need to add a rule to your
11668 makefile describing how that target can be built.  Other possible
11669 sources of this problem are typos in the makefile (if that file name is
11670 wrong) or a corrupted source tree (if that file is not supposed to be
11671 built, but rather only a prerequisite).
11673 @item No targets specified and no makefile found.  Stop.
11674 @itemx No targets.  Stop.
11675 The former means that you didn't provide any targets to be built on the
11676 command line, and @code{make} couldn't find any makefiles to read in.
11677 The latter means that some makefile was found, but it didn't contain any
11678 default goal and none was given on the command line.  GNU @code{make}
11679 has nothing to do in these situations.
11680 @xref{Makefile Arguments, ,Arguments to Specify the Makefile}.@refill
11682 @item Makefile `@var{xxx}' was not found.
11683 @itemx Included makefile `@var{xxx}' was not found.
11684 A makefile specified on the command line (first form) or included
11685 (second form) was not found.
11687 @item warning: overriding recipe for target `@var{xxx}'
11688 @itemx warning: ignoring old recipe for target `@var{xxx}'
11689 GNU @code{make} allows only one recipe to be specified per target
11690 (except for double-colon rules).  If you give a recipe for a target
11691 which already has been defined to have one, this warning is issued and
11692 the second recipe will overwrite the first.  @xref{Multiple Rules,
11693 ,Multiple Rules for One Target}.
11695 @item Circular @var{xxx} <- @var{yyy} dependency dropped.
11696 This means that @code{make} detected a loop in the dependency graph:
11697 after tracing the prerequisite @var{yyy} of target @var{xxx}, and its
11698 prerequisites, etc., one of them depended on @var{xxx} again.
11700 @item Recursive variable `@var{xxx}' references itself (eventually).  Stop.
11701 This means you've defined a normal (recursive) @code{make} variable
11702 @var{xxx} that, when it's expanded, will refer to itself (@var{xxx}).
11703 This is not allowed; either use simply-expanded variables (@samp{:=}
11704 or @samp{::=}) or use the append operator (@samp{+=}).  @xref{Using
11705 Variables, ,How to Use Variables}.
11707 @item Unterminated variable reference.  Stop.
11708 This means you forgot to provide the proper closing parenthesis
11709 or brace in your variable or function reference.
11711 @item insufficient arguments to function `@var{xxx}'.  Stop.
11712 This means you haven't provided the requisite number of arguments for
11713 this function.  See the documentation of the function for a description
11714 of its arguments.  @xref{Functions, ,Functions for Transforming Text}.
11716 @item missing target pattern.  Stop.
11717 @itemx multiple target patterns.  Stop.
11718 @itemx target pattern contains no `%'.  Stop.
11719 @itemx mixed implicit and static pattern rules.  Stop.
11720 These are generated for malformed static pattern rules.  The first means
11721 there's no pattern in the target section of the rule; the second means
11722 there are multiple patterns in the target section; the third means
11723 the target doesn't contain a pattern character (@code{%}); and the
11724 fourth means that all three parts of the static pattern rule contain
11725 pattern characters (@code{%})--only the first two parts should.
11726 @xref{Static Usage, ,Syntax of Static Pattern Rules}.
11728 @item warning: -jN forced in submake: disabling jobserver mode.
11729 This warning and the next are generated if @code{make} detects error
11730 conditions related to parallel processing on systems where
11731 sub-@code{make}s can communicate (@pxref{Options/Recursion,
11732 ,Communicating Options to a Sub-@code{make}}).  This warning is
11733 generated if a recursive invocation of a @code{make} process is forced
11734 to have @samp{-j@var{N}} in its argument list (where @var{N} is greater
11735 than one).  This could happen, for example, if you set the @code{MAKE}
11736 environment variable to @samp{make -j2}.  In this case, the
11737 sub-@code{make} doesn't communicate with other @code{make} processes and
11738 will simply pretend it has two jobs of its own.
11740 @item warning: jobserver unavailable: using -j1.  Add `+' to parent make rule.
11741 In order for @code{make} processes to communicate, the parent will pass
11742 information to the child.  Since this could result in problems if the
11743 child process isn't actually a @code{make}, the parent will only do this
11744 if it thinks the child is a @code{make}.  The parent uses the normal
11745 algorithms to determine this (@pxref{MAKE Variable, ,How the @code{MAKE}
11746 Variable Works}).  If the makefile is constructed such that the parent
11747 doesn't know the child is a @code{make} process, then the child will
11748 receive only part of the information necessary.  In this case, the child
11749 will generate this warning message and proceed with its build in a
11750 sequential manner.
11752 @end table
11754 @node Complex Makefile, GNU Free Documentation License, Error Messages, Top
11755 @appendix Complex Makefile Example
11757 Here is the makefile for the GNU @code{tar} program.  This is a
11758 moderately complex makefile.  The first line uses a @code{#!} setting
11759 to allow the makefile to be executed directly.
11761 Because it is the first target, the default goal is @samp{all}.  An
11762 interesting feature of this makefile is that @file{testpad.h} is a
11763 source file automatically created by the @code{testpad} program,
11764 itself compiled from @file{testpad.c}.
11766 If you type @samp{make} or @samp{make all}, then @code{make} creates
11767 the @file{tar} executable, the @file{rmt} daemon that provides
11768 remote tape access, and the @file{tar.info} Info file.
11770 If you type @samp{make install}, then @code{make} not only creates
11771 @file{tar}, @file{rmt}, and @file{tar.info}, but also installs
11772 them.
11774 If you type @samp{make clean}, then @code{make} removes the @samp{.o}
11775 files, and the @file{tar}, @file{rmt}, @file{testpad},
11776 @file{testpad.h}, and @file{core} files.
11778 If you type @samp{make distclean}, then @code{make} not only removes
11779 the same files as does @samp{make clean} but also the
11780 @file{TAGS}, @file{Makefile}, and @file{config.status} files.
11781 (Although it is not evident, this makefile (and
11782 @file{config.status}) is generated by the user with the
11783 @code{configure} program, which is provided in the @code{tar}
11784 distribution, but is not shown here.)
11786 If you type @samp{make realclean}, then @code{make} removes the same
11787 files as does @samp{make distclean} and also removes the Info files
11788 generated from @file{tar.texinfo}.
11790 In addition, there are targets @code{shar} and @code{dist} that create
11791 distribution kits.
11793 @example
11794 @group
11795 #!/usr/bin/make -f
11796 # Generated automatically from Makefile.in by configure.
11797 # Un*x Makefile for GNU tar program.
11798 # Copyright (C) 1991 Free Software Foundation, Inc.
11799 @end group
11801 @group
11802 # This program is free software; you can redistribute
11803 # it and/or modify it under the terms of the GNU
11804 # General Public License @dots{}
11805 @dots{}
11806 @dots{}
11807 @end group
11809 SHELL = /bin/sh
11811 #### Start of system configuration section. ####
11813 srcdir = .
11815 @group
11816 # If you use gcc, you should either run the
11817 # fixincludes script that comes with it or else use
11818 # gcc with the -traditional option.  Otherwise ioctl
11819 # calls will be compiled incorrectly on some systems.
11820 CC = gcc -O
11821 YACC = bison -y
11822 INSTALL = /usr/local/bin/install -c
11823 INSTALLDATA = /usr/local/bin/install -c -m 644
11824 @end group
11826 # Things you might add to DEFS:
11827 # -DSTDC_HEADERS        If you have ANSI C headers and
11828 #                       libraries.
11829 # -DPOSIX               If you have POSIX.1 headers and
11830 #                       libraries.
11831 # -DBSD42               If you have sys/dir.h (unless
11832 #                       you use -DPOSIX), sys/file.h,
11833 #                       and st_blocks in `struct stat'.
11834 # -DUSG                 If you have System V/ANSI C
11835 #                       string and memory functions
11836 #                       and headers, sys/sysmacros.h,
11837 #                       fcntl.h, getcwd, no valloc,
11838 #                       and ndir.h (unless
11839 #                       you use -DDIRENT).
11840 # -DNO_MEMORY_H         If USG or STDC_HEADERS but do not
11841 #                       include memory.h.
11842 # -DDIRENT              If USG and you have dirent.h
11843 #                       instead of ndir.h.
11844 # -DSIGTYPE=int         If your signal handlers
11845 #                       return int, not void.
11846 # -DNO_MTIO             If you lack sys/mtio.h
11847 #                       (magtape ioctls).
11848 # -DNO_REMOTE           If you do not have a remote shell
11849 #                       or rexec.
11850 # -DUSE_REXEC           To use rexec for remote tape
11851 #                       operations instead of
11852 #                       forking rsh or remsh.
11853 # -DVPRINTF_MISSING     If you lack vprintf function
11854 #                       (but have _doprnt).
11855 # -DDOPRNT_MISSING      If you lack _doprnt function.
11856 #                       Also need to define
11857 #                       -DVPRINTF_MISSING.
11858 # -DFTIME_MISSING       If you lack ftime system call.
11859 # -DSTRSTR_MISSING      If you lack strstr function.
11860 # -DVALLOC_MISSING      If you lack valloc function.
11861 # -DMKDIR_MISSING       If you lack mkdir and
11862 #                       rmdir system calls.
11863 # -DRENAME_MISSING      If you lack rename system call.
11864 # -DFTRUNCATE_MISSING   If you lack ftruncate
11865 #                       system call.
11866 # -DV7                  On Version 7 Unix (not
11867 #                       tested in a long time).
11868 # -DEMUL_OPEN3          If you lack a 3-argument version
11869 #                       of open, and want to emulate it
11870 #                       with system calls you do have.
11871 # -DNO_OPEN3            If you lack the 3-argument open
11872 #                       and want to disable the tar -k
11873 #                       option instead of emulating open.
11874 # -DXENIX               If you have sys/inode.h
11875 #                       and need it 94 to be included.
11877 DEFS =  -DSIGTYPE=int -DDIRENT -DSTRSTR_MISSING \
11878         -DVPRINTF_MISSING -DBSD42
11879 # Set this to rtapelib.o unless you defined NO_REMOTE,
11880 # in which case make it empty.
11881 RTAPELIB = rtapelib.o
11882 LIBS =
11883 DEF_AR_FILE = /dev/rmt8
11884 DEFBLOCKING = 20
11886 @group
11887 CDEBUG = -g
11888 CFLAGS = $(CDEBUG) -I. -I$(srcdir) $(DEFS) \
11889         -DDEF_AR_FILE=\"$(DEF_AR_FILE)\" \
11890         -DDEFBLOCKING=$(DEFBLOCKING)
11891 LDFLAGS = -g
11892 @end group
11894 @group
11895 prefix = /usr/local
11896 # Prefix for each installed program,
11897 # normally empty or `g'.
11898 binprefix =
11900 # The directory to install tar in.
11901 bindir = $(prefix)/bin
11903 # The directory to install the info files in.
11904 infodir = $(prefix)/info
11905 @end group
11907 #### End of system configuration section. ####
11909 @group
11910 SRCS_C  = tar.c create.c extract.c buffer.c   \
11911           getoldopt.c update.c gnu.c mangle.c \
11912           version.c list.c names.c diffarch.c \
11913           port.c wildmat.c getopt.c getopt1.c \
11914           regex.c
11915 SRCS_Y  = getdate.y
11916 SRCS    = $(SRCS_C) $(SRCS_Y)
11917 OBJS    = $(SRCS_C:.c=.o) $(SRCS_Y:.y=.o) $(RTAPELIB)
11918 @end group
11919 @group
11920 AUX =   README COPYING ChangeLog Makefile.in  \
11921         makefile.pc configure configure.in \
11922         tar.texinfo tar.info* texinfo.tex \
11923         tar.h port.h open3.h getopt.h regex.h \
11924         rmt.h rmt.c rtapelib.c alloca.c \
11925         msd_dir.h msd_dir.c tcexparg.c \
11926         level-0 level-1 backup-specs testpad.c
11927 @end group
11929 .PHONY: all
11930 all:    tar rmt tar.info
11932 @group
11933 tar:    $(OBJS)
11934         $(CC) $(LDFLAGS) -o $@@ $(OBJS) $(LIBS)
11935 @end group
11937 @group
11938 rmt:    rmt.c
11939         $(CC) $(CFLAGS) $(LDFLAGS) -o $@@ rmt.c
11940 @end group
11942 @group
11943 tar.info: tar.texinfo
11944         makeinfo tar.texinfo
11945 @end group
11947 @group
11948 .PHONY: install
11949 install: all
11950         $(INSTALL) tar $(bindir)/$(binprefix)tar
11951         -test ! -f rmt || $(INSTALL) rmt /etc/rmt
11952         $(INSTALLDATA) $(srcdir)/tar.info* $(infodir)
11953 @end group
11955 @group
11956 $(OBJS): tar.h port.h testpad.h
11957 regex.o buffer.o tar.o: regex.h
11958 # getdate.y has 8 shift/reduce conflicts.
11959 @end group
11961 @group
11962 testpad.h: testpad
11963         ./testpad
11964 @end group
11966 @group
11967 testpad: testpad.o
11968         $(CC) -o $@@ testpad.o
11969 @end group
11971 @group
11972 TAGS:   $(SRCS)
11973         etags $(SRCS)
11974 @end group
11976 @group
11977 .PHONY: clean
11978 clean:
11979         rm -f *.o tar rmt testpad testpad.h core
11980 @end group
11982 @group
11983 .PHONY: distclean
11984 distclean: clean
11985         rm -f TAGS Makefile config.status
11986 @end group
11988 @group
11989 .PHONY: realclean
11990 realclean: distclean
11991         rm -f tar.info*
11992 @end group
11994 @group
11995 .PHONY: shar
11996 shar: $(SRCS) $(AUX)
11997         shar $(SRCS) $(AUX) | compress \
11998           > tar-`sed -e '/version_string/!d' \
11999                      -e 's/[^0-9.]*\([0-9.]*\).*/\1/' \
12000                      -e q
12001                      version.c`.shar.Z
12002 @end group
12004 @group
12005 .PHONY: dist
12006 dist: $(SRCS) $(AUX)
12007         echo tar-`sed \
12008              -e '/version_string/!d' \
12009              -e 's/[^0-9.]*\([0-9.]*\).*/\1/' \
12010              -e q
12011              version.c` > .fname
12012         -rm -rf `cat .fname`
12013         mkdir `cat .fname`
12014         ln $(SRCS) $(AUX) `cat .fname`
12015         tar chZf `cat .fname`.tar.Z `cat .fname`
12016         -rm -rf `cat .fname` .fname
12017 @end group
12019 @group
12020 tar.zoo: $(SRCS) $(AUX)
12021         -rm -rf tmp.dir
12022         -mkdir tmp.dir
12023         -rm tar.zoo
12024         for X in $(SRCS) $(AUX) ; do \
12025             echo $$X ; \
12026             sed 's/$$/^M/' $$X \
12027             > tmp.dir/$$X ; done
12028         cd tmp.dir ; zoo aM ../tar.zoo *
12029         -rm -rf tmp.dir
12030 @end group
12031 @end example
12033 @node GNU Free Documentation License, Concept Index, Complex Makefile, Top
12034 @appendixsec GNU Free Documentation License
12035 @cindex FDL, GNU Free Documentation License
12036 @include fdl.texi
12038 @node Concept Index, Name Index, GNU Free Documentation License, Top
12039 @unnumbered Index of Concepts
12041 @printindex cp
12043 @node Name Index,  , Concept Index, Top
12044 @unnumbered Index of Functions, Variables, & Directives
12046 @printindex fn
12048 @bye