1 \input texinfo @c -*- Texinfo -*-
4 @settitle GNU @code{make}
8 @c FSF publishers: format makebook.texi instead of using this file directly.
10 @set RCSID $Id: make.texi,v 1.44 2006/02/14 15:42:18 psmith Exp $
13 @set UPDATED 5 Feb 2006
14 @set UPDATE-MONTH Feb 2006
15 @c ISBN provided by Lisa M. Opus Goldstein <opus@gnu.org>, 5 May 2004
16 @set ISBN 1-882114-83-5
20 @c ISPELL CHECK: done, 10 June 1993 --roland
21 @c ISPELL CHECK: done, 2000-06-25 --Martin Buchholz
23 @c Combine the variable and function indices:
25 @c Combine the program and concept indices:
28 @dircategory GNU Packages
30 * Make: (make). Remake files automatically.
34 This file documents the GNU Make utility, which determines
35 automatically which pieces of a large program need to be recompiled,
36 and issues the commands to recompile them.
38 This is Edition @value{EDITION}, last updated @value{UPDATED},
39 of @cite{The GNU Make Manual}, for @code{make}, Version @value{VERSION}.
41 Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
42 1998, 1999, 2000, 2002, 2003, 2004, 2005, 2006
43 Free Software Foundation, Inc.
45 Permission is granted to copy, distribute and/or modify this document
46 under the terms of the GNU Free Documentation License, Version 1.1 or
47 any later version published by the Free Software Foundation; with no
48 Invariant Sections, with no Front-Cover Texts, and with no Back-Cover
49 Texts. A copy of the license is included in the section entitled
50 ``GNU Free Documentation License''.
54 @shorttitlepage GNU Make
58 @subtitle A Program for Directing Recompilation
59 @subtitle GNU @code{make} Version @value{VERSION}
60 @subtitle @value{UPDATE-MONTH}
61 @author Richard M. Stallman, Roland McGrath, Paul D. Smith
63 @vskip 0pt plus 1filll
64 Copyright @copyright{} 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
65 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
66 Free Software Foundation, Inc.
68 Published by the Free Software Foundation @*
69 51 Franklin St. -- Fifth Floor @*
70 Boston, MA 02110-1301 USA @*
73 Permission is granted to copy, distribute and/or modify this document
74 under the terms of the GNU Free Documentation License, Version 1.1 or
75 any later version published by the Free Software Foundation; with the
76 Invariant Sections being ``GNU General Public License'', the Front-Cover
77 Texts being ``A GNU Manual'', and with the Back-Cover Texts being as in
78 (a) below. A copy of the license is included in the section entitled
79 ``GNU Free Documentation License''.
81 (a) The FSF's Back-Cover Text is:
84 You have freedom to copy and modify this GNU Manual, like GNU
85 software. Copies published by the Free Software Foundation raise
86 funds for GNU development.
89 Cover art by Etienne Suvasa.
94 @node Top, Overview, (dir), (dir)
97 The GNU @code{make} utility automatically determines which pieces of a
98 large program need to be recompiled, and issues the commands to
99 recompile them.@refill
101 This edition of the @cite{GNU Make Manual},
102 last updated @value{UPDATED},
103 documents GNU @code{make} Version @value{VERSION}.@refill
105 This manual describes @code{make} and contains the following chapters:@refill
109 * Overview:: Overview of @code{make}.
110 * Introduction:: An introduction to @code{make}.
111 * Makefiles:: Makefiles tell @code{make} what to do.
112 * Rules:: Rules describe when a file must be remade.
113 * Commands:: Commands say how to remake a file.
114 * Using Variables:: You can use variables to avoid repetition.
115 * Conditionals:: Use or ignore parts of the makefile based
116 on the values of variables.
117 * Functions:: Many powerful ways to manipulate text.
118 * Invoking make: Running. How to invoke @code{make} on the command line.
119 * Implicit Rules:: Use implicit rules to treat many files alike,
120 based on their file names.
121 * Archives:: How @code{make} can update library archives.
122 * Features:: Features GNU @code{make} has over other @code{make}s.
123 * Missing:: What GNU @code{make} lacks from other @code{make}s.
124 * Makefile Conventions:: Conventions for writing makefiles for
126 * Quick Reference:: A quick reference for experienced users.
127 * Error Messages:: A list of common errors generated by @code{make}.
128 * Complex Makefile:: A real example of a straightforward,
129 but nontrivial, makefile.
131 * GNU Free Documentation License:: License for copying this manual
132 * Concept Index:: Index of Concepts
133 * Name Index:: Index of Functions, Variables, & Directives
136 --- The Detailed Node Listing ---
138 Overview of @code{make}
140 * Preparing:: Preparing and Running Make
141 * Reading:: On Reading this Text
142 * Bugs:: Problems and Bugs
144 An Introduction to Makefiles
146 * Rule Introduction:: What a rule looks like.
147 * Simple Makefile:: A Simple Makefile
148 * How Make Works:: How @code{make} Processes This Makefile
149 * Variables Simplify:: Variables Make Makefiles Simpler
150 * make Deduces:: Letting @code{make} Deduce the Commands
151 * Combine By Prerequisite:: Another Style of Makefile
152 * Cleanup:: Rules for Cleaning the Directory
156 * Makefile Contents:: What makefiles contain.
157 * Makefile Names:: How to name your makefile.
158 * Include:: How one makefile can use another makefile.
159 * MAKEFILES Variable:: The environment can specify extra makefiles.
160 * MAKEFILE_LIST Variable:: Discover which makefiles have been read.
161 * Special Variables:: Other special variables.
162 * Remaking Makefiles:: How makefiles get remade.
163 * Overriding Makefiles:: How to override part of one makefile
164 with another makefile.
165 * Reading Makefiles:: How makefiles are parsed.
166 * Secondary Expansion:: How and when secondary expansion is performed.
170 * Rule Example:: An example explained.
171 * Rule Syntax:: General syntax explained.
172 * Prerequisite Types:: There are two types of prerequisites.
173 * Wildcards:: Using wildcard characters such as `*'.
174 * Directory Search:: Searching other directories for source files.
175 * Phony Targets:: Using a target that is not a real file's name.
176 * Force Targets:: You can use a target without commands
177 or prerequisites to mark other targets
179 * Empty Targets:: When only the date matters and the
181 * Special Targets:: Targets with special built-in meanings.
182 * Multiple Targets:: When to make use of several targets in a rule.
183 * Multiple Rules:: How to use several rules with the same target.
184 * Static Pattern:: Static pattern rules apply to multiple targets
185 and can vary the prerequisites according to
187 * Double-Colon:: How to use a special kind of rule to allow
188 several independent rules for one target.
189 * Automatic Prerequisites:: How to automatically generate rules giving
190 prerequisites from source files themselves.
192 Using Wildcard Characters in File Names
194 * Wildcard Examples:: Several examples
195 * Wildcard Pitfall:: Problems to avoid.
196 * Wildcard Function:: How to cause wildcard expansion where
197 it does not normally take place.
199 Searching Directories for Prerequisites
201 * General Search:: Specifying a search path that applies
202 to every prerequisite.
203 * Selective Search:: Specifying a search path
204 for a specified class of names.
205 * Search Algorithm:: When and how search paths are applied.
206 * Commands/Search:: How to write shell commands that work together
208 * Implicit/Search:: How search paths affect implicit rules.
209 * Libraries/Search:: Directory search for link libraries.
213 * Static Usage:: The syntax of static pattern rules.
214 * Static versus Implicit:: When are they better than implicit rules?
216 Writing the Commands in Rules
218 * Command Syntax:: Command syntax features and pitfalls.
219 * Echoing:: How to control when commands are echoed.
220 * Execution:: How commands are executed.
221 * Parallel:: How commands can be executed in parallel.
222 * Errors:: What happens after a command execution error.
223 * Interrupts:: What happens when a command is interrupted.
224 * Recursion:: Invoking @code{make} from makefiles.
225 * Sequences:: Defining canned sequences of commands.
226 * Empty Commands:: Defining useful, do-nothing commands.
230 * Splitting Lines:: Breaking long command lines for readability.
231 * Variables in Commands:: Using @code{make} variables in commands.
235 * Choosing the Shell:: How @code{make} chooses the shell used
238 Recursive Use of @code{make}
240 * MAKE Variable:: The special effects of using @samp{$(MAKE)}.
241 * Variables/Recursion:: How to communicate variables to a sub-@code{make}.
242 * Options/Recursion:: How to communicate options to a sub-@code{make}.
243 * -w Option:: How the @samp{-w} or @samp{--print-directory} option
244 helps debug use of recursive @code{make} commands.
248 * Reference:: How to use the value of a variable.
249 * Flavors:: Variables come in two flavors.
250 * Advanced:: Advanced features for referencing a variable.
251 * Values:: All the ways variables get their values.
252 * Setting:: How to set a variable in the makefile.
253 * Appending:: How to append more text to the old value
255 * Override Directive:: How to set a variable in the makefile even if
256 the user has set it with a command argument.
257 * Defining:: An alternate way to set a variable
258 to a verbatim string.
259 * Environment:: Variable values can come from the environment.
260 * Target-specific:: Variable values can be defined on a per-target
262 * Pattern-specific:: Target-specific variable values can be applied
263 to a group of targets that match a pattern.
265 Advanced Features for Reference to Variables
267 * Substitution Refs:: Referencing a variable with
268 substitutions on the value.
269 * Computed Names:: Computing the name of the variable to refer to.
271 Conditional Parts of Makefiles
273 * Conditional Example:: Example of a conditional
274 * Conditional Syntax:: The syntax of conditionals.
275 * Testing Flags:: Conditionals that test flags.
277 Functions for Transforming Text
279 * Syntax of Functions:: How to write a function call.
280 * Text Functions:: General-purpose text manipulation functions.
281 * File Name Functions:: Functions for manipulating file names.
282 * Conditional Functions:: Functions that implement conditions.
283 * Foreach Function:: Repeat some text with controlled variation.
284 * Call Function:: Expand a user-defined function.
285 * Value Function:: Return the un-expanded value of a variable.
286 * Eval Function:: Evaluate the arguments as makefile syntax.
287 * Origin Function:: Find where a variable got its value.
288 * Flavor Function:: Find out the flavor of a variable.
289 * Shell Function:: Substitute the output of a shell command.
290 * Make Control Functions:: Functions that control how make runs.
292 How to Run @code{make}
294 * Makefile Arguments:: How to specify which makefile to use.
295 * Goals:: How to use goal arguments to specify which
296 parts of the makefile to use.
297 * Instead of Execution:: How to use mode flags to specify what
298 kind of thing to do with the commands
299 in the makefile other than simply
301 * Avoiding Compilation:: How to avoid recompiling certain files.
302 * Overriding:: How to override a variable to specify
303 an alternate compiler and other things.
304 * Testing:: How to proceed past some errors, to
306 * Options Summary:: Summary of Options
310 * Using Implicit:: How to use an existing implicit rule
311 to get the commands for updating a file.
312 * Catalogue of Rules:: A list of built-in implicit rules.
313 * Implicit Variables:: How to change what predefined rules do.
314 * Chained Rules:: How to use a chain of implicit rules.
315 * Pattern Rules:: How to define new implicit rules.
316 * Last Resort:: How to define commands for rules which
318 * Suffix Rules:: The old-fashioned style of implicit rule.
319 * Implicit Rule Search:: The precise algorithm for applying
322 Defining and Redefining Pattern Rules
324 * Pattern Intro:: An introduction to pattern rules.
325 * Pattern Examples:: Examples of pattern rules.
326 * Automatic Variables:: How to use automatic variables in the
327 commands of implicit rules.
328 * Pattern Match:: How patterns match.
329 * Match-Anything Rules:: Precautions you should take prior to
330 defining rules that can match any
331 target file whatever.
332 * Canceling Rules:: How to override or cancel built-in rules.
334 Using @code{make} to Update Archive Files
336 * Archive Members:: Archive members as targets.
337 * Archive Update:: The implicit rule for archive member targets.
338 * Archive Pitfalls:: Dangers to watch out for when using archives.
339 * Archive Suffix Rules:: You can write a special kind of suffix rule
340 for updating archives.
342 Implicit Rule for Archive Member Targets
344 * Archive Symbols:: How to update archive symbol directories.
349 @node Overview, Introduction, Top, Top
350 @comment node-name, next, previous, up
351 @chapter Overview of @code{make}
353 The @code{make} utility automatically determines which pieces of a large
354 program need to be recompiled, and issues commands to recompile them.
355 This manual describes GNU @code{make}, which was implemented by Richard
356 Stallman and Roland McGrath. Development since Version 3.76 has been
357 handled by Paul D. Smith.
359 GNU @code{make} conforms to section 6.2 of @cite{IEEE Standard
360 1003.2-1992} (POSIX.2).
362 @cindex IEEE Standard 1003.2
363 @cindex standards conformance
365 Our examples show C programs, since they are most common, but you can use
366 @code{make} with any programming language whose compiler can be run with a
367 shell command. Indeed, @code{make} is not limited to programs. You can
368 use it to describe any task where some files must be updated automatically
369 from others whenever the others change.
372 * Preparing:: Preparing and Running Make
373 * Reading:: On Reading this Text
374 * Bugs:: Problems and Bugs
377 @node Preparing, Reading, Overview, Overview
379 @heading Preparing and Running Make
382 To prepare to use @code{make}, you must write a file called
383 the @dfn{makefile} that describes the relationships among files
384 in your program and provides commands for updating each file.
385 In a program, typically, the executable file is updated from object
386 files, which are in turn made by compiling source files.@refill
388 Once a suitable makefile exists, each time you change some source files,
389 this simple shell command:
396 suffices to perform all necessary recompilations. The @code{make} program
397 uses the makefile data base and the last-modification times of the files to
398 decide which of the files need to be updated. For each of those files, it
399 issues the commands recorded in the data base.
401 You can provide command line arguments to @code{make} to control which
402 files should be recompiled, or how. @xref{Running, ,How to Run
405 @node Reading, Bugs, Preparing, Overview
406 @section How to Read This Manual
408 If you are new to @code{make}, or are looking for a general
409 introduction, read the first few sections of each chapter, skipping the
410 later sections. In each chapter, the first few sections contain
411 introductory or general information and the later sections contain
412 specialized or technical information.
414 The exception is the second chapter, @ref{Introduction, ,An
415 Introduction to Makefiles}, all of which is introductory.
418 The exception is @ref{Introduction, ,An Introduction to Makefiles},
419 all of which is introductory.
422 If you are familiar with other @code{make} programs, see @ref{Features,
423 ,Features of GNU @code{make}}, which lists the enhancements GNU
424 @code{make} has, and @ref{Missing, ,Incompatibilities and Missing
425 Features}, which explains the few things GNU @code{make} lacks that
428 For a quick summary, see @ref{Options Summary}, @ref{Quick Reference},
429 and @ref{Special Targets}.
431 @node Bugs, , Reading, Overview
432 @section Problems and Bugs
433 @cindex reporting bugs
434 @cindex bugs, reporting
435 @cindex problems and bugs, reporting
437 If you have problems with GNU @code{make} or think you've found a bug,
438 please report it to the developers; we cannot promise to do anything but
439 we might well want to fix it.
441 Before reporting a bug, make sure you've actually found a real bug.
442 Carefully reread the documentation and see if it really says you can do
443 what you're trying to do. If it's not clear whether you should be able
444 to do something or not, report that too; it's a bug in the
447 Before reporting a bug or trying to fix it yourself, try to isolate it
448 to the smallest possible makefile that reproduces the problem. Then
449 send us the makefile and the exact results @code{make} gave you,
450 including any error or warning messages. Please don't paraphrase
451 these messages: it's best to cut and paste them into your report.
452 When generating this small makefile, be sure to not use any non-free
453 or unusual tools in your commands: you can almost always emulate what
454 such a tool would do with simple shell commands. Finally, be sure to
455 explain what you expected to occur; this will help us decide whether
456 the problem was really in the documentation.
458 Once you have a precise problem you can report it in one of two ways.
459 Either send electronic mail to:
466 or use our Web-based project management tool, at:
469 http://savannah.gnu.org/projects/make/
473 In addition to the information above, please be careful to include the
474 version number of @code{make} you are using. You can get this
475 information with the command @samp{make --version}. Be sure also to
476 include the type of machine and operating system you are using. One
477 way to obtain this information is by looking at the final lines of
478 output from the command @samp{make --help}.
480 @node Introduction, Makefiles, Overview, Top
481 @comment node-name, next, previous, up
482 @chapter An Introduction to Makefiles
484 You need a file called a @dfn{makefile} to tell @code{make} what to do.
485 Most often, the makefile tells @code{make} how to compile and link a
489 In this chapter, we will discuss a simple makefile that describes how to
490 compile and link a text editor which consists of eight C source files
491 and three header files. The makefile can also tell @code{make} how to
492 run miscellaneous commands when explicitly asked (for example, to remove
493 certain files as a clean-up operation). To see a more complex example
494 of a makefile, see @ref{Complex Makefile}.
496 When @code{make} recompiles the editor, each changed C source file
497 must be recompiled. If a header file has changed, each C source file
498 that includes the header file must be recompiled to be safe. Each
499 compilation produces an object file corresponding to the source file.
500 Finally, if any source file has been recompiled, all the object files,
501 whether newly made or saved from previous compilations, must be linked
502 together to produce the new executable editor.
503 @cindex recompilation
507 * Rule Introduction:: What a rule looks like.
508 * Simple Makefile:: A Simple Makefile
509 * How Make Works:: How @code{make} Processes This Makefile
510 * Variables Simplify:: Variables Make Makefiles Simpler
511 * make Deduces:: Letting @code{make} Deduce the Commands
512 * Combine By Prerequisite:: Another Style of Makefile
513 * Cleanup:: Rules for Cleaning the Directory
516 @node Rule Introduction, Simple Makefile, Introduction, Introduction
517 @comment node-name, next, previous, up
518 @section What a Rule Looks Like
519 @cindex rule, introduction to
520 @cindex makefile rule parts
521 @cindex parts of makefile rule
523 A simple makefile consists of ``rules'' with the following shape:
525 @cindex targets, introduction to
526 @cindex prerequisites, introduction to
527 @cindex commands, introduction to
530 @var{target} @dots{} : @var{prerequisites} @dots{}
537 A @dfn{target} is usually the name of a file that is generated by a
538 program; examples of targets are executable or object files. A target
539 can also be the name of an action to carry out, such as @samp{clean}
540 (@pxref{Phony Targets}).
542 A @dfn{prerequisite} is a file that is used as input to create the
543 target. A target often depends on several files.
545 @cindex tabs in rules
546 A @dfn{command} is an action that @code{make} carries out.
547 A rule may have more than one command, each on its own line.
548 @strong{Please note:} you need to put a tab character at the beginning of
549 every command line! This is an obscurity that catches the unwary.
551 Usually a command is in a rule with prerequisites and serves to create a
552 target file if any of the prerequisites change. However, the rule that
553 specifies commands for the target need not have prerequisites. For
554 example, the rule containing the delete command associated with the
555 target @samp{clean} does not have prerequisites.
557 A @dfn{rule}, then, explains how and when to remake certain files
558 which are the targets of the particular rule. @code{make} carries out
559 the commands on the prerequisites to create or update the target. A
560 rule can also explain how and when to carry out an action.
561 @xref{Rules, , Writing Rules}.
563 A makefile may contain other text besides rules, but a simple makefile
564 need only contain rules. Rules may look somewhat more complicated
565 than shown in this template, but all fit the pattern more or less.
567 @node Simple Makefile, How Make Works, Rule Introduction, Introduction
568 @section A Simple Makefile
569 @cindex simple makefile
570 @cindex makefile, simple
572 Here is a straightforward makefile that describes the way an
573 executable file called @code{edit} depends on eight object files
574 which, in turn, depend on eight C source and three header files.
576 In this example, all the C files include @file{defs.h}, but only those
577 defining editing commands include @file{command.h}, and only low
578 level files that change the editor buffer include @file{buffer.h}.
582 edit : main.o kbd.o command.o display.o \
583 insert.o search.o files.o utils.o
584 cc -o edit main.o kbd.o command.o display.o \
585 insert.o search.o files.o utils.o
587 main.o : main.c defs.h
589 kbd.o : kbd.c defs.h command.h
591 command.o : command.c defs.h command.h
593 display.o : display.c defs.h buffer.h
595 insert.o : insert.c defs.h buffer.h
597 search.o : search.c defs.h buffer.h
599 files.o : files.c defs.h buffer.h command.h
601 utils.o : utils.c defs.h
604 rm edit main.o kbd.o command.o display.o \
605 insert.o search.o files.o utils.o
610 We split each long line into two lines using backslash-newline; this is
611 like using one long line, but is easier to read.
612 @cindex continuation lines
613 @cindex @code{\} (backslash), for continuation lines
614 @cindex backslash (@code{\}), for continuation lines
615 @cindex quoting newline, in makefile
616 @cindex newline, quoting, in makefile
618 To use this makefile to create the executable file called @file{edit},
625 To use this makefile to delete the executable file and all the object
626 files from the directory, type:
632 In the example makefile, the targets include the executable file
633 @samp{edit}, and the object files @samp{main.o} and @samp{kbd.o}. The
634 prerequisites are files such as @samp{main.c} and @samp{defs.h}.
635 In fact, each @samp{.o} file is both a target and a prerequisite.
636 Commands include @w{@samp{cc -c main.c}} and @w{@samp{cc -c kbd.c}}.
638 When a target is a file, it needs to be recompiled or relinked if any
639 of its prerequisites change. In addition, any prerequisites that are
640 themselves automatically generated should be updated first. In this
641 example, @file{edit} depends on each of the eight object files; the
642 object file @file{main.o} depends on the source file @file{main.c} and
643 on the header file @file{defs.h}.
645 A shell command follows each line that contains a target and
646 prerequisites. These shell commands say how to update the target file.
647 A tab character must come at the beginning of every command line to
648 distinguish command lines from other lines in the makefile. (Bear in
649 mind that @code{make} does not know anything about how the commands
650 work. It is up to you to supply commands that will update the target
651 file properly. All @code{make} does is execute the commands in the rule
652 you have specified when the target file needs to be updated.)
653 @cindex shell command
655 The target @samp{clean} is not a file, but merely the name of an
658 do not want to carry out the actions in this rule, @samp{clean} is not a prerequisite of any other rule.
659 Consequently, @code{make} never does anything with it unless you tell
660 it specifically. Note that this rule not only is not a prerequisite, it
661 also does not have any prerequisites, so the only purpose of the rule
662 is to run the specified commands. Targets that do not refer to files
663 but are just actions are called @dfn{phony targets}. @xref{Phony
664 Targets}, for information about this kind of target. @xref{Errors, ,
665 Errors in Commands}, to see how to cause @code{make} to ignore errors
666 from @code{rm} or any other command.
667 @cindex @code{clean} target
668 @cindex @code{rm} (shell command)
670 @node How Make Works, Variables Simplify, Simple Makefile, Introduction
671 @comment node-name, next, previous, up
672 @section How @code{make} Processes a Makefile
673 @cindex processing a makefile
674 @cindex makefile, how @code{make} processes
676 By default, @code{make} starts with the first target (not targets whose
677 names start with @samp{.}). This is called the @dfn{default goal}.
678 (@dfn{Goals} are the targets that @code{make} strives ultimately to
679 update. You can override this behavior using the command line
680 (@pxref{Goals, , Arguments to Specify the Goals}) or with the
681 @code{.DEFAULT_GOAL} special variable (@pxref{Special Variables, ,
682 Other Special Variables}).
684 @cindex goal, default
687 In the simple example of the previous section, the default goal is to
688 update the executable program @file{edit}; therefore, we put that rule
691 Thus, when you give the command:
698 @code{make} reads the makefile in the current directory and begins by
699 processing the first rule. In the example, this rule is for relinking
700 @file{edit}; but before @code{make} can fully process this rule, it
701 must process the rules for the files that @file{edit} depends on,
702 which in this case are the object files. Each of these files is
703 processed according to its own rule. These rules say to update each
704 @samp{.o} file by compiling its source file. The recompilation must
705 be done if the source file, or any of the header files named as
706 prerequisites, is more recent than the object file, or if the object
709 The other rules are processed because their targets appear as
710 prerequisites of the goal. If some other rule is not depended on by the
711 goal (or anything it depends on, etc.), that rule is not processed,
712 unless you tell @code{make} to do so (with a command such as
713 @w{@code{make clean}}).
715 Before recompiling an object file, @code{make} considers updating its
716 prerequisites, the source file and header files. This makefile does not
717 specify anything to be done for them---the @samp{.c} and @samp{.h} files
718 are not the targets of any rules---so @code{make} does nothing for these
719 files. But @code{make} would update automatically generated C programs,
720 such as those made by Bison or Yacc, by their own rules at this time.
722 After recompiling whichever object files need it, @code{make} decides
723 whether to relink @file{edit}. This must be done if the file
724 @file{edit} does not exist, or if any of the object files are newer than
725 it. If an object file was just recompiled, it is now newer than
726 @file{edit}, so @file{edit} is relinked.
729 Thus, if we change the file @file{insert.c} and run @code{make},
730 @code{make} will compile that file to update @file{insert.o}, and then
731 link @file{edit}. If we change the file @file{command.h} and run
732 @code{make}, @code{make} will recompile the object files @file{kbd.o},
733 @file{command.o} and @file{files.o} and then link the file @file{edit}.
735 @node Variables Simplify, make Deduces, How Make Works, Introduction
736 @section Variables Make Makefiles Simpler
738 @cindex simplifying with variables
740 In our example, we had to list all the object files twice in the rule for
741 @file{edit} (repeated here):
745 edit : main.o kbd.o command.o display.o \
746 insert.o search.o files.o utils.o
747 cc -o edit main.o kbd.o command.o display.o \
748 insert.o search.o files.o utils.o
752 @cindex @code{objects}
753 Such duplication is error-prone; if a new object file is added to the
754 system, we might add it to one list and forget the other. We can eliminate
755 the risk and simplify the makefile by using a variable. @dfn{Variables}
756 allow a text string to be defined once and substituted in multiple places
757 later (@pxref{Using Variables, ,How to Use Variables}).
759 @cindex @code{OBJECTS}
764 It is standard practice for every makefile to have a variable named
765 @code{objects}, @code{OBJECTS}, @code{objs}, @code{OBJS}, @code{obj},
766 or @code{OBJ} which is a list of all object file names. We would
767 define such a variable @code{objects} with a line like this in the
772 objects = main.o kbd.o command.o display.o \
773 insert.o search.o files.o utils.o
778 Then, each place we want to put a list of the object file names, we can
779 substitute the variable's value by writing @samp{$(objects)}
780 (@pxref{Using Variables, ,How to Use Variables}).
782 Here is how the complete simple makefile looks when you use a variable
783 for the object files:
787 objects = main.o kbd.o command.o display.o \
788 insert.o search.o files.o utils.o
791 cc -o edit $(objects)
792 main.o : main.c defs.h
794 kbd.o : kbd.c defs.h command.h
796 command.o : command.c defs.h command.h
798 display.o : display.c defs.h buffer.h
800 insert.o : insert.c defs.h buffer.h
802 search.o : search.c defs.h buffer.h
804 files.o : files.c defs.h buffer.h command.h
806 utils.o : utils.c defs.h
813 @node make Deduces, Combine By Prerequisite, Variables Simplify, Introduction
814 @section Letting @code{make} Deduce the Commands
815 @cindex deducing commands (implicit rules)
816 @cindex implicit rule, introduction to
817 @cindex rule, implicit, introduction to
819 It is not necessary to spell out the commands for compiling the individual
820 C source files, because @code{make} can figure them out: it has an
821 @dfn{implicit rule} for updating a @samp{.o} file from a correspondingly
822 named @samp{.c} file using a @samp{cc -c} command. For example, it will
823 use the command @samp{cc -c main.c -o main.o} to compile @file{main.c} into
824 @file{main.o}. We can therefore omit the commands from the rules for the
825 object files. @xref{Implicit Rules, ,Using Implicit Rules}.@refill
827 When a @samp{.c} file is used automatically in this way, it is also
828 automatically added to the list of prerequisites. We can therefore omit
829 the @samp{.c} files from the prerequisites, provided we omit the commands.
831 Here is the entire example, with both of these changes, and a variable
832 @code{objects} as suggested above:
836 objects = main.o kbd.o command.o display.o \
837 insert.o search.o files.o utils.o
840 cc -o edit $(objects)
843 kbd.o : defs.h command.h
844 command.o : defs.h command.h
845 display.o : defs.h buffer.h
846 insert.o : defs.h buffer.h
847 search.o : defs.h buffer.h
848 files.o : defs.h buffer.h command.h
858 This is how we would write the makefile in actual practice. (The
859 complications associated with @samp{clean} are described elsewhere.
860 See @ref{Phony Targets}, and @ref{Errors, ,Errors in Commands}.)
862 Because implicit rules are so convenient, they are important. You
863 will see them used frequently.@refill
865 @node Combine By Prerequisite, Cleanup, make Deduces, Introduction
866 @section Another Style of Makefile
867 @cindex combining rules by prerequisite
869 When the objects of a makefile are created only by implicit rules, an
870 alternative style of makefile is possible. In this style of makefile,
871 you group entries by their prerequisites instead of by their targets.
872 Here is what one looks like:
876 objects = main.o kbd.o command.o display.o \
877 insert.o search.o files.o utils.o
880 cc -o edit $(objects)
883 kbd.o command.o files.o : command.h
884 display.o insert.o search.o files.o : buffer.h
889 Here @file{defs.h} is given as a prerequisite of all the object files;
890 @file{command.h} and @file{buffer.h} are prerequisites of the specific
891 object files listed for them.
893 Whether this is better is a matter of taste: it is more compact, but some
894 people dislike it because they find it clearer to put all the information
895 about each target in one place.
897 @node Cleanup, , Combine By Prerequisite, Introduction
898 @section Rules for Cleaning the Directory
900 @cindex removing, to clean up
902 Compiling a program is not the only thing you might want to write rules
903 for. Makefiles commonly tell how to do a few other things besides
904 compiling a program: for example, how to delete all the object files
905 and executables so that the directory is @samp{clean}.
907 @cindex @code{clean} target
909 could write a @code{make} rule for cleaning our example editor:
918 In practice, we might want to write the rule in a somewhat more
919 complicated manner to handle unanticipated situations. We would do this:
930 This prevents @code{make} from getting confused by an actual file
931 called @file{clean} and causes it to continue in spite of errors from
932 @code{rm}. (See @ref{Phony Targets}, and @ref{Errors, ,Errors in
936 A rule such as this should not be placed at the beginning of the
937 makefile, because we do not want it to run by default! Thus, in the
938 example makefile, we want the rule for @code{edit}, which recompiles
939 the editor, to remain the default goal.
941 Since @code{clean} is not a prerequisite of @code{edit}, this rule will not
942 run at all if we give the command @samp{make} with no arguments. In
943 order to make the rule run, we have to type @samp{make clean}.
944 @xref{Running, ,How to Run @code{make}}.
946 @node Makefiles, Rules, Introduction, Top
947 @chapter Writing Makefiles
949 @cindex makefile, how to write
950 The information that tells @code{make} how to recompile a system comes from
951 reading a data base called the @dfn{makefile}.
954 * Makefile Contents:: What makefiles contain.
955 * Makefile Names:: How to name your makefile.
956 * Include:: How one makefile can use another makefile.
957 * MAKEFILES Variable:: The environment can specify extra makefiles.
958 * MAKEFILE_LIST Variable:: Discover which makefiles have been read.
959 * Special Variables:: Other special variables.
960 * Remaking Makefiles:: How makefiles get remade.
961 * Overriding Makefiles:: How to override part of one makefile
962 with another makefile.
963 * Reading Makefiles:: How makefiles are parsed.
964 * Secondary Expansion:: How and when secondary expansion is performed.
967 @node Makefile Contents, Makefile Names, Makefiles, Makefiles
968 @section What Makefiles Contain
970 Makefiles contain five kinds of things: @dfn{explicit rules},
971 @dfn{implicit rules}, @dfn{variable definitions}, @dfn{directives},
972 and @dfn{comments}. Rules, variables, and directives are described at
973 length in later chapters.@refill
976 @cindex rule, explicit, definition of
977 @cindex explicit rule, definition of
979 An @dfn{explicit rule} says when and how to remake one or more files,
980 called the rule's @dfn{targets}. It lists the other files that the
981 targets depend on, called the @dfn{prerequisites} of the target, and
982 may also give commands to use to create or update the targets.
983 @xref{Rules, ,Writing Rules}.
985 @cindex rule, implicit, definition of
986 @cindex implicit rule, definition of
988 An @dfn{implicit rule} says when and how to remake a class of files
989 based on their names. It describes how a target may depend on a file
990 with a name similar to the target and gives commands to create or
991 update such a target. @xref{Implicit Rules, ,Using Implicit Rules}.
993 @cindex variable definition
995 A @dfn{variable definition} is a line that specifies a text string
996 value for a variable that can be substituted into the text later. The
997 simple makefile example shows a variable definition for @code{objects}
998 as a list of all object files (@pxref{Variables Simplify, , Variables
999 Make Makefiles Simpler}).
1003 A @dfn{directive} is a command for @code{make} to do something special while
1004 reading the makefile. These include:
1008 Reading another makefile (@pxref{Include, ,Including Other Makefiles}).
1011 Deciding (based on the values of variables) whether to use or
1012 ignore a part of the makefile (@pxref{Conditionals, ,Conditional Parts of Makefiles}).
1015 Defining a variable from a verbatim string containing multiple lines
1016 (@pxref{Defining, ,Defining Variables Verbatim}).
1019 @cindex comments, in makefile
1020 @cindex @code{#} (comments), in makefile
1022 @samp{#} in a line of a makefile starts a @dfn{comment}. It and the
1023 rest of the line are ignored, except that a trailing backslash not
1024 escaped by another backslash will continue the comment across multiple
1025 lines. A line containing just a comment (with perhaps spaces before
1026 it) is effectively blank, and is ignored. If you want a literal
1027 @code{#}, escape it with a backslash (e.g., @code{\#}). Comments may
1028 appear on any line in the makefile, although they are treated
1029 specially in certain situations.
1031 Within a command script (if the line begins with a TAB character) the
1032 entire line is passed to the shell, just as with any other line that
1033 begins with a TAB. The shell decides how to interpret the text:
1034 whether or not this is a comment is up to the shell.
1036 Within a @code{define} directive, comments are not ignored during the
1037 definition of the variable, but rather kept intact in the value of the
1038 variable. When the variable is expanded they will either be treated
1039 as @code{make} comments or as command script text, depending on the
1040 context in which the variable is evaluated.
1043 @node Makefile Names, Include, Makefile Contents, Makefiles
1044 @section What Name to Give Your Makefile
1045 @cindex makefile name
1046 @cindex name of makefile
1047 @cindex default makefile name
1048 @cindex file name of makefile
1050 @c following paragraph rewritten to avoid overfull hbox
1051 By default, when @code{make} looks for the makefile, it tries the
1052 following names, in order: @file{GNUmakefile}, @file{makefile}
1053 and @file{Makefile}.@refill
1058 @cindex @code{README}
1059 Normally you should call your makefile either @file{makefile} or
1060 @file{Makefile}. (We recommend @file{Makefile} because it appears
1061 prominently near the beginning of a directory listing, right near other
1062 important files such as @file{README}.) The first name checked,
1063 @file{GNUmakefile}, is not recommended for most makefiles. You should
1064 use this name if you have a makefile that is specific to GNU
1065 @code{make}, and will not be understood by other versions of
1066 @code{make}. Other @code{make} programs look for @file{makefile} and
1067 @file{Makefile}, but not @file{GNUmakefile}.
1069 If @code{make} finds none of these names, it does not use any makefile.
1070 Then you must specify a goal with a command argument, and @code{make}
1071 will attempt to figure out how to remake it using only its built-in
1072 implicit rules. @xref{Implicit Rules, ,Using Implicit Rules}.
1075 @cindex @code{--file}
1076 @cindex @code{--makefile}
1077 If you want to use a nonstandard name for your makefile, you can specify
1078 the makefile name with the @samp{-f} or @samp{--file} option. The
1079 arguments @w{@samp{-f @var{name}}} or @w{@samp{--file=@var{name}}} tell
1080 @code{make} to read the file @var{name} as the makefile. If you use
1081 more than one @samp{-f} or @samp{--file} option, you can specify several
1082 makefiles. All the makefiles are effectively concatenated in the order
1083 specified. The default makefile names @file{GNUmakefile},
1084 @file{makefile} and @file{Makefile} are not checked automatically if you
1085 specify @samp{-f} or @samp{--file}.@refill
1086 @cindex specifying makefile name
1087 @cindex makefile name, how to specify
1088 @cindex name of makefile, how to specify
1089 @cindex file name of makefile, how to specify
1091 @node Include, MAKEFILES Variable, Makefile Names, Makefiles
1092 @section Including Other Makefiles
1093 @cindex including other makefiles
1094 @cindex makefile, including
1097 The @code{include} directive tells @code{make} to suspend reading the
1098 current makefile and read one or more other makefiles before continuing.
1099 The directive is a line in the makefile that looks like this:
1102 include @var{filenames}@dots{}
1106 @var{filenames} can contain shell file name patterns. If
1107 @var{filenames} is empty, nothing is included and no error is printed.
1108 @cindex shell file name pattern (in @code{include})
1109 @cindex shell wildcards (in @code{include})
1110 @cindex wildcard, in @code{include}
1112 Extra spaces are allowed and ignored at the beginning of the line, but
1113 a tab is not allowed. (If the line begins with a tab, it will be
1114 considered a command line.) Whitespace is required between
1115 @code{include} and the file names, and between file names; extra
1116 whitespace is ignored there and at the end of the directive. A
1117 comment starting with @samp{#} is allowed at the end of the line. If
1118 the file names contain any variable or function references, they are
1119 expanded. @xref{Using Variables, ,How to Use Variables}.
1121 For example, if you have three @file{.mk} files, @file{a.mk},
1122 @file{b.mk}, and @file{c.mk}, and @code{$(bar)} expands to
1123 @code{bish bash}, then the following expression
1126 include foo *.mk $(bar)
1132 include foo a.mk b.mk c.mk bish bash
1135 When @code{make} processes an @code{include} directive, it suspends
1136 reading of the containing makefile and reads from each listed file in
1137 turn. When that is finished, @code{make} resumes reading the
1138 makefile in which the directive appears.
1140 One occasion for using @code{include} directives is when several programs,
1141 handled by individual makefiles in various directories, need to use a
1142 common set of variable definitions
1143 (@pxref{Setting, ,Setting Variables}) or pattern rules
1144 (@pxref{Pattern Rules, ,Defining and Redefining Pattern Rules}).
1146 Another such occasion is when you want to generate prerequisites from
1147 source files automatically; the prerequisites can be put in a file that
1148 is included by the main makefile. This practice is generally cleaner
1149 than that of somehow appending the prerequisites to the end of the main
1150 makefile as has been traditionally done with other versions of
1151 @code{make}. @xref{Automatic Prerequisites}.
1152 @cindex prerequisites, automatic generation
1153 @cindex automatic generation of prerequisites
1154 @cindex generating prerequisites automatically
1157 @cindex @code{--include-dir}
1158 @cindex included makefiles, default directories
1159 @cindex default directories for included makefiles
1160 @findex /usr/gnu/include
1161 @findex /usr/local/include
1162 @findex /usr/include
1163 If the specified name does not start with a slash, and the file is not
1164 found in the current directory, several other directories are searched.
1165 First, any directories you have specified with the @samp{-I} or
1166 @samp{--include-dir} option are searched
1167 (@pxref{Options Summary, ,Summary of Options}).
1168 Then the following directories (if they exist)
1169 are searched, in this order:
1170 @file{@var{prefix}/include} (normally @file{/usr/local/include}
1171 @footnote{GNU Make compiled for MS-DOS and MS-Windows behaves as if
1172 @var{prefix} has been defined to be the root of the DJGPP tree
1174 @file{/usr/gnu/include},
1175 @file{/usr/local/include}, @file{/usr/include}.
1177 If an included makefile cannot be found in any of these directories, a
1178 warning message is generated, but it is not an immediately fatal error;
1179 processing of the makefile containing the @code{include} continues.
1180 Once it has finished reading makefiles, @code{make} will try to remake
1181 any that are out of date or don't exist.
1182 @xref{Remaking Makefiles, ,How Makefiles Are Remade}.
1183 Only after it has tried to find a way to remake a makefile and failed,
1184 will @code{make} diagnose the missing makefile as a fatal error.
1186 If you want @code{make} to simply ignore a makefile which does not exist
1187 and cannot be remade, with no error message, use the @w{@code{-include}}
1188 directive instead of @code{include}, like this:
1191 -include @var{filenames}@dots{}
1194 This acts like @code{include} in every way except that there is no
1195 error (not even a warning) if any of the @var{filenames} do not exist.
1196 For compatibility with some other @code{make} implementations,
1197 @code{sinclude} is another name for @w{@code{-include}}.
1199 @node MAKEFILES Variable, MAKEFILE_LIST Variable, Include, Makefiles
1200 @section The Variable @code{MAKEFILES}
1201 @cindex makefile, and @code{MAKEFILES} variable
1202 @cindex including (@code{MAKEFILES} variable)
1205 If the environment variable @code{MAKEFILES} is defined, @code{make}
1206 considers its value as a list of names (separated by whitespace) of
1207 additional makefiles to be read before the others. This works much like
1208 the @code{include} directive: various directories are searched for those
1209 files (@pxref{Include, ,Including Other Makefiles}). In addition, the
1210 default goal is never taken from one of these makefiles and it is not an
1211 error if the files listed in @code{MAKEFILES} are not found.@refill
1213 @cindex recursion, and @code{MAKEFILES} variable
1214 The main use of @code{MAKEFILES} is in communication between recursive
1215 invocations of @code{make} (@pxref{Recursion, ,Recursive Use of
1216 @code{make}}). It usually is not desirable to set the environment
1217 variable before a top-level invocation of @code{make}, because it is
1218 usually better not to mess with a makefile from outside. However, if
1219 you are running @code{make} without a specific makefile, a makefile in
1220 @code{MAKEFILES} can do useful things to help the built-in implicit
1221 rules work better, such as defining search paths (@pxref{Directory Search}).
1223 Some users are tempted to set @code{MAKEFILES} in the environment
1224 automatically on login, and program makefiles to expect this to be done.
1225 This is a very bad idea, because such makefiles will fail to work if run by
1226 anyone else. It is much better to write explicit @code{include} directives
1227 in the makefiles. @xref{Include, , Including Other Makefiles}.
1229 @node MAKEFILE_LIST Variable, Special Variables, MAKEFILES Variable, Makefiles
1230 @comment node-name, next, previous, up
1231 @section The Variable @code{MAKEFILE_LIST}
1232 @cindex makefiles, and @code{MAKEFILE_LIST} variable
1233 @cindex including (@code{MAKEFILE_LIST} variable)
1234 @vindex MAKEFILE_LIST
1236 As @code{make} reads various makefiles, including any obtained from the
1237 @code{MAKEFILES} variable, the command line, the default files, or
1238 from @code{include} directives, their names will be automatically
1239 appended to the @code{MAKEFILE_LIST} variable. They are added right
1240 before @code{make} begins to parse them.
1242 This means that if the first thing a makefile does is examine the last
1243 word in this variable, it will be the name of the current makefile.
1244 Once the current makefile has used @code{include}, however, the last
1245 word will be the just-included makefile.
1247 If a makefile named @code{Makefile} has this content:
1251 name1 := $(lastword $(MAKEFILE_LIST))
1255 name2 := $(lastword $(MAKEFILE_LIST))
1258 @@echo name1 = $(name1)
1259 @@echo name2 = $(name2)
1264 then you would expect to see this output:
1273 @xref{Text Functions}, for more information on the @code{word} and
1274 @code{words} functions used above. @xref{Flavors, The Two Flavors of
1275 Variables}, for more information on simply-expanded (@code{:=})
1276 variable definitions.
1278 @node Special Variables, Remaking Makefiles, MAKEFILE_LIST Variable, Makefiles
1279 @comment node-name, next, previous, up
1280 @section Other Special Variables
1281 @cindex makefiles, and special variables
1282 @cindex special variables
1284 GNU @code{make} also supports other special variables. Unless
1285 otherwise documented here, these values lose their special properties
1286 if they are set by a makefile or on the command line.
1290 @vindex .DEFAULT_GOAL @r{(define default goal)}
1292 Sets the default goal to be used if no targets were specified on the
1293 command line (@pxref{Goals, , Arguments to Specify the Goals}). The
1294 @code{.DEFAULT_GOAL} variable allows you to discover the current
1295 default goal, restart the default goal selection algorithm by clearing
1296 its value, or to explicitly set the default goal. The following
1297 example illustrates these cases:
1301 # Query the default goal.
1302 ifeq ($(.DEFAULT_GOAL),)
1303 $(warning no default goal is set)
1309 $(warning default goal is $(.DEFAULT_GOAL))
1311 # Reset the default goal.
1317 $(warning default goal is $(.DEFAULT_GOAL))
1320 .DEFAULT_GOAL := foo
1324 This makefile prints:
1328 no default goal is set
1335 Note that assigning more than one target name to @code{.DEFAULT_GOAL} is
1336 illegal and will result in an error.
1338 @vindex MAKE_RESTARTS @r{(number of times @code{make} has restarted)}
1340 This variable is set only if this instance of @code{make} has
1341 restarted (@pxref{Remaking Makefiles, , How Makefiles Are Remade}): it
1342 will contain the number of times this instance has restarted. Note
1343 this is not the same as recursion (counted by the @code{MAKELEVEL}
1344 variable). You should not set, modify, or export this variable.
1346 @vindex .VARIABLES @r{(list of variables)}
1348 Expands to a list of the @emph{names} of all global variables defined
1349 so far. This includes variables which have empty values, as well as
1350 built-in variables (@pxref{Implicit Variables, , Variables Used by
1351 Implicit Rules}), but does not include any variables which are only
1352 defined in a target-specific context. Note that any value you assign
1353 to this variable will be ignored; it will always return its special
1356 @c @vindex .TARGETS @r{(list of targets)}
1358 @c The second special variable is @code{.TARGETS}. When expanded, the
1359 @c value consists of a list of all targets defined in all makefiles read
1360 @c up until that point. Note it's not enough for a file to be simply
1361 @c mentioned in the makefile to be listed in this variable, even if it
1362 @c would match an implicit rule and become an ``implicit target''. The
1363 @c file must appear as a target, on the left-hand side of a ``:'', to be
1364 @c considered a target for the purposes of this variable.
1366 @vindex .FEATURES @r{(list of supported features)}
1368 Expands to a list of special features supported by this version of
1369 @code{make}. Possible values include:
1374 Supports @code{ar} (archive) files using special filename syntax.
1375 @xref{Archives, ,Using @code{make} to Update Archive Files}.
1378 Supports the @code{-L} (@code{--check-symlink-times}) flag.
1379 @xref{Options Summary, ,Summary of Options}.
1382 Supports ``else if'' non-nested conditionals. @xref{Conditional
1383 Syntax, ,Syntax of Conditionals}.
1386 Supports ``job server'' enhanced parallel builds. @xref{Parallel,
1387 ,Parallel Execution}.
1389 @item second-expansion
1390 Supports secondary expansion of prerequisite lists.
1393 Supports order-only prerequisites. @xref{Prerequisite Types, ,Types
1396 @item target-specific
1397 Supports target-specific and pattern-specific variable assignments.
1398 @xref{Target-specific, ,Target-specific Variable Values}.
1402 @vindex .INCLUDE_DIRS @r{(list of include directories)}
1404 Expands to a list of directories that @code{make} searches for
1405 included makefiles (@pxref{Include, , Including Other Makefiles}).
1409 @node Remaking Makefiles, Overriding Makefiles, Special Variables, Makefiles
1410 @section How Makefiles Are Remade
1412 @cindex updating makefiles
1413 @cindex remaking makefiles
1414 @cindex makefile, remaking of
1415 Sometimes makefiles can be remade from other files, such as RCS or SCCS
1416 files. If a makefile can be remade from other files, you probably want
1417 @code{make} to get an up-to-date version of the makefile to read in.
1419 To this end, after reading in all makefiles, @code{make} will consider
1420 each as a goal target and attempt to update it. If a makefile has a
1421 rule which says how to update it (found either in that very makefile or
1422 in another one) or if an implicit rule applies to it (@pxref{Implicit
1423 Rules, ,Using Implicit Rules}), it will be updated if necessary. After
1424 all makefiles have been checked, if any have actually been changed,
1425 @code{make} starts with a clean slate and reads all the makefiles over
1426 again. (It will also attempt to update each of them over again, but
1427 normally this will not change them again, since they are already up to
1430 If you know that one or more of your makefiles cannot be remade and you
1431 want to keep @code{make} from performing an implicit rule search on
1432 them, perhaps for efficiency reasons, you can use any normal method of
1433 preventing implicit rule lookup to do so. For example, you can write an
1434 explicit rule with the makefile as the target, and an empty command
1435 string (@pxref{Empty Commands, ,Using Empty Commands}).
1437 If the makefiles specify a double-colon rule to remake a file with
1438 commands but no prerequisites, that file will always be remade
1439 (@pxref{Double-Colon}). In the case of makefiles, a makefile that has a
1440 double-colon rule with commands but no prerequisites will be remade every
1441 time @code{make} is run, and then again after @code{make} starts over
1442 and reads the makefiles in again. This would cause an infinite loop:
1443 @code{make} would constantly remake the makefile, and never do anything
1444 else. So, to avoid this, @code{make} will @strong{not} attempt to
1445 remake makefiles which are specified as targets of a double-colon rule
1446 with commands but no prerequisites.@refill
1448 If you do not specify any makefiles to be read with @samp{-f} or
1449 @samp{--file} options, @code{make} will try the default makefile names;
1450 @pxref{Makefile Names, ,What Name to Give Your Makefile}. Unlike
1451 makefiles explicitly requested with @samp{-f} or @samp{--file} options,
1452 @code{make} is not certain that these makefiles should exist. However,
1453 if a default makefile does not exist but can be created by running
1454 @code{make} rules, you probably want the rules to be run so that the
1455 makefile can be used.
1457 Therefore, if none of the default makefiles exists, @code{make} will try
1458 to make each of them in the same order in which they are searched for
1459 (@pxref{Makefile Names, ,What Name to Give Your Makefile})
1460 until it succeeds in making one, or it runs out of names to try. Note
1461 that it is not an error if @code{make} cannot find or make any makefile;
1462 a makefile is not always necessary.@refill
1464 When you use the @samp{-t} or @samp{--touch} option
1465 (@pxref{Instead of Execution, ,Instead of Executing the Commands}),
1466 you would not want to use an out-of-date makefile to decide which
1467 targets to touch. So the @samp{-t} option has no effect on updating
1468 makefiles; they are really updated even if @samp{-t} is specified.
1469 Likewise, @samp{-q} (or @samp{--question}) and @samp{-n} (or
1470 @samp{--just-print}) do not prevent updating of makefiles, because an
1471 out-of-date makefile would result in the wrong output for other targets.
1472 Thus, @samp{make -f mfile -n foo} will update @file{mfile}, read it in,
1473 and then print the commands to update @file{foo} and its prerequisites
1474 without running them. The commands printed for @file{foo} will be those
1475 specified in the updated contents of @file{mfile}.
1477 However, on occasion you might actually wish to prevent updating of even
1478 the makefiles. You can do this by specifying the makefiles as goals in
1479 the command line as well as specifying them as makefiles. When the
1480 makefile name is specified explicitly as a goal, the options @samp{-t}
1481 and so on do apply to them.
1483 Thus, @samp{make -f mfile -n mfile foo} would read the makefile
1484 @file{mfile}, print the commands needed to update it without actually
1485 running them, and then print the commands needed to update @file{foo}
1486 without running them. The commands for @file{foo} will be those
1487 specified by the existing contents of @file{mfile}.
1489 @node Overriding Makefiles, Reading Makefiles, Remaking Makefiles, Makefiles
1490 @section Overriding Part of Another Makefile
1492 @cindex overriding makefiles
1493 @cindex makefile, overriding
1494 Sometimes it is useful to have a makefile that is mostly just like
1495 another makefile. You can often use the @samp{include} directive to
1496 include one in the other, and add more targets or variable definitions.
1497 However, if the two makefiles give different commands for the same
1498 target, @code{make} will not let you just do this. But there is another way.
1500 @cindex match-anything rule, used to override
1501 In the containing makefile (the one that wants to include the other),
1502 you can use a match-anything pattern rule to say that to remake any
1503 target that cannot be made from the information in the containing
1504 makefile, @code{make} should look in another makefile.
1505 @xref{Pattern Rules}, for more information on pattern rules.
1507 For example, if you have a makefile called @file{Makefile} that says how
1508 to make the target @samp{foo} (and other targets), you can write a
1509 makefile called @file{GNUmakefile} that contains:
1516 @@$(MAKE) -f Makefile $@@
1520 If you say @samp{make foo}, @code{make} will find @file{GNUmakefile},
1521 read it, and see that to make @file{foo}, it needs to run the command
1522 @samp{frobnicate > foo}. If you say @samp{make bar}, @code{make} will
1523 find no way to make @file{bar} in @file{GNUmakefile}, so it will use the
1524 commands from the pattern rule: @samp{make -f Makefile bar}. If
1525 @file{Makefile} provides a rule for updating @file{bar}, @code{make}
1526 will apply the rule. And likewise for any other target that
1527 @file{GNUmakefile} does not say how to make.
1529 The way this works is that the pattern rule has a pattern of just
1530 @samp{%}, so it matches any target whatever. The rule specifies a
1531 prerequisite @file{force}, to guarantee that the commands will be run even
1532 if the target file already exists. We give @file{force} target empty
1533 commands to prevent @code{make} from searching for an implicit rule to
1534 build it---otherwise it would apply the same match-anything rule to
1535 @file{force} itself and create a prerequisite loop!
1537 @node Reading Makefiles, Secondary Expansion, Overriding Makefiles, Makefiles
1538 @section How @code{make} Reads a Makefile
1539 @cindex reading makefiles
1540 @cindex makefile, parsing
1542 GNU @code{make} does its work in two distinct phases. During the first
1543 phase it reads all the makefiles, included makefiles, etc. and
1544 internalizes all the variables and their values, implicit and explicit
1545 rules, and constructs a dependency graph of all the targets and their
1546 prerequisites. During the second phase, @code{make} uses these internal
1547 structures to determine what targets will need to be rebuilt and to
1548 invoke the rules necessary to do so.
1550 It's important to understand this two-phase approach because it has a
1551 direct impact on how variable and function expansion happens; this is
1552 often a source of some confusion when writing makefiles. Here we will
1553 present a summary of the phases in which expansion happens for different
1554 constructs within the makefile. We say that expansion is
1555 @dfn{immediate} if it happens during the first phase: in this case
1556 @code{make} will expand any variables or functions in that section of a
1557 construct as the makefile is parsed. We say that expansion is
1558 @dfn{deferred} if expansion is not performed immediately. Expansion of
1559 deferred construct is not performed until either the construct appears
1560 later in an immediate context, or until the second phase.
1562 You may not be familiar with some of these constructs yet. You can
1563 reference this section as you become familiar with them, in later
1566 @subheading Variable Assignment
1567 @cindex +=, expansion
1568 @cindex =, expansion
1569 @cindex ?=, expansion
1570 @cindex +=, expansion
1571 @cindex define, expansion
1573 Variable definitions are parsed as follows:
1576 @var{immediate} = @var{deferred}
1577 @var{immediate} ?= @var{deferred}
1578 @var{immediate} := @var{immediate}
1579 @var{immediate} += @var{deferred} or @var{immediate}
1581 define @var{immediate}
1586 For the append operator, @samp{+=}, the right-hand side is considered
1587 immediate if the variable was previously set as a simple variable
1588 (@samp{:=}), and deferred otherwise.
1590 @subheading Conditional Statements
1591 @cindex ifdef, expansion
1592 @cindex ifeq, expansion
1593 @cindex ifndef, expansion
1594 @cindex ifneq, expansion
1596 All instances of conditional syntax are parsed immediately, in their
1597 entirety; this includes the @code{ifdef}, @code{ifeq}, @code{ifndef},
1598 and @code{ifneq} forms. Of course this means that automatic variables
1599 cannot be used in conditional statements, as automatic variables are
1600 not set until the command script for that rule is invoked. If you
1601 need to use automatic variables in a conditional you @emph{must} use
1602 shell conditional syntax, in your command script proper, for these
1603 tests, not @code{make} conditionals.
1605 @subheading Rule Definition
1606 @cindex target, expansion
1607 @cindex prerequisite, expansion
1608 @cindex implicit rule, expansion
1609 @cindex pattern rule, expansion
1610 @cindex explicit rule, expansion
1612 A rule is always expanded the same way, regardless of the form:
1615 @var{immediate} : @var{immediate} ; @var{deferred}
1619 That is, the target and prerequisite sections are expanded immediately,
1620 and the commands used to construct the target are always deferred. This
1621 general rule is true for explicit rules, pattern rules, suffix rules,
1622 static pattern rules, and simple prerequisite definitions.
1624 @node Secondary Expansion, , Reading Makefiles, Makefiles
1625 @section Secondary Expansion
1626 @cindex secondary expansion
1627 @cindex expansion, secondary
1629 @findex .SECONDEXPANSION
1630 In the previous section we learned that GNU @code{make} works in two
1631 distinct phases: a read-in phase and a target-update phase
1632 (@pxref{Reading Makefiles, , How @code{make} Reads a Makefile}). GNU
1633 make also has the ability to enable a @emph{second expansion} of the
1634 prerequisites (only) for some or all targets defined in the makefile.
1635 In order for this second expansion to occur, the special target
1636 @code{.SECONDEXPANSION} must be defined before the first prerequisite
1637 list that makes use of this feature.
1639 If that special target is defined then in between the two phases
1640 mentioned above, right at the end of the read-in phase, all the
1641 prerequisites of the targets defined after the special target are
1642 expanded a @emph{second time}. In most circumstances this secondary
1643 expansion will have no effect, since all variable and function
1644 references will have been expanded during the initial parsing of the
1645 makefiles. In order to take advantage of the secondary expansion
1646 phase of the parser, then, it's necessary to @emph{escape} the
1647 variable or function reference in the makefile. In this case the
1648 first expansion merely un-escapes the reference but doesn't expand it,
1649 and expansion is left to the secondary expansion phase. For example,
1650 consider this makefile:
1656 myfile: $(ONEVAR) $$(TWOVAR)
1659 After the first expansion phase the prerequisites list of the
1660 @file{myfile} target will be @code{onefile} and @code{$(TWOVAR)}; the
1661 first (unescaped) variable reference to @var{ONEVAR} is expanded,
1662 while the second (escaped) variable reference is simply unescaped,
1663 without being recognized as a variable reference. Now during the
1664 secondary expansion the first word is expanded again but since it
1665 contains no variable or function references it remains the static
1666 value @file{onefile}, while the second word is now a normal reference
1667 to the variable @var{TWOVAR}, which is expanded to the value
1668 @file{twofile}. The final result is that there are two prerequisites,
1669 @file{onefile} and @file{twofile}.
1671 Obviously, this is not a very interesting case since the same result
1672 could more easily have been achieved simply by having both variables
1673 appear, unescaped, in the prerequisites list. One difference becomes
1674 apparent if the variables are reset; consider this example:
1684 Here the prerequisite of @file{onefile} will be expanded immediately,
1685 and resolve to the value @file{top}, while the prerequisite of
1686 @file{twofile} will not be full expanded until the secondary expansion
1687 and yield a value of @file{bottom}.
1689 This is marginally more exciting, but the true power of this feature
1690 only becomes apparent when you discover that secondary expansions
1691 always take place within the scope of the automatic variables for that
1692 target. This means that you can use variables such as @code{$@@},
1693 @code{$*}, etc. during the second expansion and they will have their
1694 expected values, just as in the command script. All you have to do is
1695 defer the expansion by escaping the @code{$}. Also, secondary
1696 expansion occurs for both explicit and implicit (pattern) rules.
1697 Knowing this, the possible uses for this feature increase
1698 dramatically. For example:
1702 main_OBJS := main.o try.o test.o
1703 lib_OBJS := lib.o api.o
1705 main lib: $$($$@@_OBJS)
1708 Here, after the initial expansion the prerequisites of both the
1709 @file{main} and @file{lib} targets will be @code{$($@@_OBJS)}. During
1710 the secondary expansion, the @code{$@@} variable is set to the name of
1711 the target and so the expansion for the @file{main} target will yield
1712 @code{$(main_OBJS)}, or @code{main.o try.o test.o}, while the
1713 secondary expansion for the @file{lib} target will yield
1714 @code{$(lib_OBJS)}, or @code{lib.o api.o}.
1716 You can also mix functions here, as long as they are properly escaped:
1719 main_SRCS := main.c try.c test.c
1720 lib_SRCS := lib.c api.c
1723 main lib: $$(patsubst %.c,%.o,$$($$@@_SRCS))
1726 This version allows users to specify source files rather than object
1727 files, but gives the same resulting prerequisites list as the previous
1730 Evaluation of automatic variables during the secondary expansion
1731 phase, especially of the target name variable @code{$$@@}, behaves
1732 similarly to evaluation within command scripts. However, there are
1733 some subtle differences and ``corner cases'' which come into play for
1734 the different types of rule definitions that @code{make} understands.
1735 The subtleties of using the different automatic variables are
1738 @subheading Secondary Expansion of Explicit Rules
1739 @cindex secondary expansion and explicit rules
1740 @cindex explicit rules, secondary expansion of
1742 During the secondary expansion of explicit rules, @code{$$@@} and
1743 @code{$$%} evaluate, respectively, to the file name of the target and,
1744 when the target is an archive member, the target member name. The
1745 @code{$$<} variable evaluates to the first prerequisite in the first
1746 rule for this target. @code{$$^} and @code{$$+} evaluate to the list
1747 of all prerequisites of rules @emph{that have already appeared} for
1748 the same target (@code{$$+} with repetitions and @code{$$^}
1749 without). The following example will help illustrate these behaviors:
1754 foo: foo.1 bar.1 $$< $$^ $$+ # line #1
1756 foo: foo.2 bar.2 $$< $$^ $$+ # line #2
1758 foo: foo.3 bar.3 $$< $$^ $$+ # line #3
1761 In the first prerequisite list, all three variables (@code{$$<},
1762 @code{$$^}, and @code{$$+}) expand to the empty string. In the
1763 second, they will have values @code{foo.1}, @code{foo.1 bar.1}, and
1764 @code{foo.1 bar.1} respectively. In the third they will have values
1765 @code{foo.1}, @code{foo.1 bar.1 foo.2 bar.2}, and @code{foo.1 bar.1
1766 foo.2 bar.2} respectively.
1768 Rules undergo secondary expansion in makefile order, except that
1769 the rule with the command script is always evaluated last.
1771 The variables @code{$$?} and @code{$$*} are not available and expand
1772 to the empty string.
1774 @subheading Secondary Expansion of Static Pattern Rules
1775 @cindex secondary expansion and static pattern rules
1776 @cindex static pattern rules, secondary expansion of
1778 Rules for secondary expansion of static pattern rules are identical to
1779 those for explicit rules, above, with one exception: for static
1780 pattern rules the @code{$$*} variable is set to the pattern stem. As
1781 with explicit rules, @code{$$?} is not available and expands to the
1784 @subheading Secondary Expansion of Implicit Rules
1785 @cindex secondary expansion and implicit rules
1786 @cindex implicit rules, secondary expansion of
1788 As @code{make} searches for an implicit rule, it substitutes the stem
1789 and then performs secondary expansion for every rule with a matching
1790 target pattern. The value of the automatic variables is derived in
1791 the same fashion as for static pattern rules. As an example:
1800 %oo: $$< $$^ $$+ $$*
1803 When the implicit rule is tried for target @file{foo}, @code{$$<}
1804 expands to @file{bar}, @code{$$^} expands to @file{bar boo},
1805 @code{$$+} also expands to @file{bar boo}, and @code{$$*} expands to
1808 Note that the directory prefix (D), as described in @ref{Implicit Rule
1809 Search, ,Implicit Rule Search Algorithm}, is appended (after
1810 expansion) to all the patterns in the prerequisites list. As an
1818 %.o: $$(addsuffix /%.c,foo bar) foo.h
1821 The prerequisite list after the secondary expansion and directory
1822 prefix reconstruction will be @file{/tmp/foo/foo.c /tmp/var/bar/foo.c
1823 foo.h}. If you are not interested in this reconstruction, you can use
1824 @code{$$*} instead of @code{%} in the prerequisites list.
1826 @node Rules, Commands, Makefiles, Top
1827 @chapter Writing Rules
1828 @cindex writing rules
1829 @cindex rule, how to write
1831 @cindex prerequisite
1833 A @dfn{rule} appears in the makefile and says when and how to remake
1834 certain files, called the rule's @dfn{targets} (most often only one per rule).
1835 It lists the other files that are the @dfn{prerequisites} of the target, and
1836 @dfn{commands} to use to create or update the target.
1838 @cindex default goal
1839 @cindex goal, default
1840 The order of rules is not significant, except for determining the
1841 @dfn{default goal}: the target for @code{make} to consider, if you do
1842 not otherwise specify one. The default goal is the target of the first
1843 rule in the first makefile. If the first rule has multiple targets,
1844 only the first target is taken as the default. There are two
1845 exceptions: a target starting with a period is not a default unless it
1846 contains one or more slashes, @samp{/}, as well; and, a target that
1847 defines a pattern rule has no effect on the default goal.
1848 (@xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.)
1850 Therefore, we usually write the makefile so that the first rule is the
1851 one for compiling the entire program or all the programs described by
1852 the makefile (often with a target called @samp{all}).
1853 @xref{Goals, ,Arguments to Specify the Goals}.
1856 * Rule Example:: An example explained.
1857 * Rule Syntax:: General syntax explained.
1858 * Prerequisite Types:: There are two types of prerequisites.
1859 * Wildcards:: Using wildcard characters such as `*'.
1860 * Directory Search:: Searching other directories for source files.
1861 * Phony Targets:: Using a target that is not a real file's name.
1862 * Force Targets:: You can use a target without commands
1863 or prerequisites to mark other targets
1865 * Empty Targets:: When only the date matters and the
1867 * Special Targets:: Targets with special built-in meanings.
1868 * Multiple Targets:: When to make use of several targets in a rule.
1869 * Multiple Rules:: How to use several rules with the same target.
1870 * Static Pattern:: Static pattern rules apply to multiple targets
1871 and can vary the prerequisites according to
1873 * Double-Colon:: How to use a special kind of rule to allow
1874 several independent rules for one target.
1875 * Automatic Prerequisites:: How to automatically generate rules giving
1876 prerequisites from source files themselves.
1880 @node Rule Example, Rule Syntax, Rules, Rules
1881 @section Rule Example
1883 Here is an example of a rule:
1886 foo.o : foo.c defs.h # module for twiddling the frobs
1890 Its target is @file{foo.o} and its prerequisites are @file{foo.c} and
1891 @file{defs.h}. It has one command, which is @samp{cc -c -g foo.c}.
1892 The command line starts with a tab to identify it as a command.
1894 This rule says two things:
1898 How to decide whether @file{foo.o} is out of date: it is out of date
1899 if it does not exist, or if either @file{foo.c} or @file{defs.h} is
1900 more recent than it.
1903 How to update the file @file{foo.o}: by running @code{cc} as stated.
1904 The command does not explicitly mention @file{defs.h}, but we presume
1905 that @file{foo.c} includes it, and that that is why @file{defs.h} was
1906 added to the prerequisites.
1910 @node Rule Syntax, Prerequisite Types, Rule Example, Rules
1911 @section Rule Syntax
1914 @cindex syntax of rules
1915 In general, a rule looks like this:
1918 @var{targets} : @var{prerequisites}
1927 @var{targets} : @var{prerequisites} ; @var{command}
1933 @cindex rule targets
1934 The @var{targets} are file names, separated by spaces. Wildcard
1935 characters may be used (@pxref{Wildcards, ,Using Wildcard Characters
1936 in File Names}) and a name of the form @file{@var{a}(@var{m})}
1937 represents member @var{m} in archive file @var{a}
1938 (@pxref{Archive Members, ,Archive Members as Targets}).
1939 Usually there is only one
1940 target per rule, but occasionally there is a reason to have more
1941 (@pxref{Multiple Targets, , Multiple Targets in a Rule}).@refill
1944 @cindex tab character (in commands)
1945 The @var{command} lines start with a tab character. The first command may
1946 appear on the line after the prerequisites, with a tab character, or may
1947 appear on the same line, with a semicolon. Either way, the effect is the
1948 same. There are other differences in the syntax of command lines.
1949 @xref{Commands, ,Writing the Commands in Rules}.
1951 @cindex dollar sign (@code{$}), in rules
1952 @cindex @code{$}, in rules
1953 @cindex rules, and @code{$}
1954 Because dollar signs are used to start @code{make} variable
1955 references, if you really want a dollar sign in a target or
1956 prerequisite you must write two of them, @samp{$$} (@pxref{Using
1957 Variables, ,How to Use Variables}). If you have enabled secondary
1958 expansion (@pxref{Secondary Expansion}) and you want a literal dollar
1959 sign in the prerequisites lise, you must actually write @emph{four}
1960 dollar signs (@samp{$$$$}).
1962 You may split a long line by inserting a backslash followed by a
1963 newline, but this is not required, as @code{make} places no limit on
1964 the length of a line in a makefile.
1966 A rule tells @code{make} two things: when the targets are out of date,
1967 and how to update them when necessary.
1969 @cindex prerequisites
1970 @cindex rule prerequisites
1971 The criterion for being out of date is specified in terms of the
1972 @var{prerequisites}, which consist of file names separated by spaces.
1973 (Wildcards and archive members (@pxref{Archives}) are allowed here too.)
1974 A target is out of date if it does not exist or if it is older than any
1975 of the prerequisites (by comparison of last-modification times). The
1976 idea is that the contents of the target file are computed based on
1977 information in the prerequisites, so if any of the prerequisites changes,
1978 the contents of the existing target file are no longer necessarily
1981 How to update is specified by @var{commands}. These are lines to be
1982 executed by the shell (normally @samp{sh}), but with some extra features
1983 (@pxref{Commands, ,Writing the Commands in Rules}).
1985 @node Prerequisite Types, Wildcards, Rule Syntax, Rules
1986 @comment node-name, next, previous, up
1987 @section Types of Prerequisites
1988 @cindex prerequisite types
1989 @cindex types of prerequisites
1991 @cindex prerequisites, normal
1992 @cindex normal prerequisites
1993 @cindex prerequisites, order-only
1994 @cindex order-only prerequisites
1995 There are actually two different types of prerequisites understood by
1996 GNU @code{make}: normal prerequisites such as described in the
1997 previous section, and @dfn{order-only} prerequisites. A normal
1998 prerequisite makes two statements: first, it imposes an order of
1999 execution of build commands: any commands necessary to build any of a
2000 target's prerequisites will be fully executed before any commands
2001 necessary to build the target. Second, it imposes a dependency
2002 relationship: if any prerequisite is newer than the target, then the
2003 target is considered out-of-date and must be rebuilt.
2005 Normally, this is exactly what you want: if a target's prerequisite is
2006 updated, then the target should also be updated.
2008 Occasionally, however, you have a situation where you want to impose a
2009 specific ordering on the rules to be invoked @emph{without} forcing
2010 the target to be updated if one of those rules is executed. In that
2011 case, you want to define @dfn{order-only} prerequisites. Order-only
2012 prerequisites can be specified by placing a pipe symbol (@code{|})
2013 in the prerequisites list: any prerequisites to the left of the pipe
2014 symbol are normal; any prerequisites to the right are order-only:
2017 @var{targets} : @var{normal-prerequisites} | @var{order-only-prerequisites}
2020 The normal prerequisites section may of course be empty. Also, you
2021 may still declare multiple lines of prerequisites for the same target:
2022 they are appended appropriately. Note that if you declare the same
2023 file to be both a normal and an order-only prerequisite, the normal
2024 prerequisite takes precedence (since they are a strict superset of the
2025 behavior of an order-only prerequisite).
2027 @node Wildcards, Directory Search, Prerequisite Types, Rules
2028 @section Using Wildcard Characters in File Names
2030 @cindex file name with wildcards
2031 @cindex globbing (wildcards)
2033 @cindex @code{*} (wildcard character)
2034 @cindex @code{?} (wildcard character)
2035 @cindex @code{[@dots{}]} (wildcard characters)
2036 A single file name can specify many files using @dfn{wildcard characters}.
2037 The wildcard characters in @code{make} are @samp{*}, @samp{?} and
2038 @samp{[@dots{}]}, the same as in the Bourne shell. For example, @file{*.c}
2039 specifies a list of all the files (in the working directory) whose names
2040 end in @samp{.c}.@refill
2042 @cindex @code{~} (tilde)
2043 @cindex tilde (@code{~})
2044 @cindex home directory
2045 The character @samp{~} at the beginning of a file name also has special
2046 significance. If alone, or followed by a slash, it represents your home
2047 directory. For example @file{~/bin} expands to @file{/home/you/bin}.
2048 If the @samp{~} is followed by a word, the string represents the home
2049 directory of the user named by that word. For example @file{~john/bin}
2050 expands to @file{/home/john/bin}. On systems which don't have a home
2051 directory for each user (such as MS-DOS or MS-Windows), this
2052 functionality can be simulated by setting the environment variable
2055 Wildcard expansion is performed by @code{make} automatically in
2056 targets and in prerequisites. In commands the shell is responsible
2057 for wildcard expansion. In other contexts, wildcard expansion happens
2058 only if you request it explicitly with the @code{wildcard} function.
2060 The special significance of a wildcard character can be turned off by
2061 preceding it with a backslash. Thus, @file{foo\*bar} would refer to a
2062 specific file whose name consists of @samp{foo}, an asterisk, and
2066 * Wildcard Examples:: Several examples
2067 * Wildcard Pitfall:: Problems to avoid.
2068 * Wildcard Function:: How to cause wildcard expansion where
2069 it does not normally take place.
2072 @node Wildcard Examples, Wildcard Pitfall, Wildcards, Wildcards
2073 @subsection Wildcard Examples
2075 Wildcards can be used in the commands of a rule, where they are expanded
2076 by the shell. For example, here is a rule to delete all the object files:
2084 @cindex @code{rm} (shell command)
2086 Wildcards are also useful in the prerequisites of a rule. With the
2087 following rule in the makefile, @samp{make print} will print all the
2088 @samp{.c} files that have changed since the last time you printed them:
2096 @cindex @code{print} target
2097 @cindex @code{lpr} (shell command)
2098 @cindex @code{touch} (shell command)
2100 This rule uses @file{print} as an empty target file; see @ref{Empty
2101 Targets, ,Empty Target Files to Record Events}. (The automatic variable
2102 @samp{$?} is used to print only those files that have changed; see
2103 @ref{Automatic Variables}.)@refill
2105 Wildcard expansion does not happen when you define a variable. Thus, if
2113 then the value of the variable @code{objects} is the actual string
2114 @samp{*.o}. However, if you use the value of @code{objects} in a target,
2115 prerequisite or command, wildcard expansion will take place at that time.
2116 To set @code{objects} to the expansion, instead use:
2119 objects := $(wildcard *.o)
2123 @xref{Wildcard Function}.
2125 @node Wildcard Pitfall, Wildcard Function, Wildcard Examples, Wildcards
2126 @subsection Pitfalls of Using Wildcards
2127 @cindex wildcard pitfalls
2128 @cindex pitfalls of wildcards
2129 @cindex mistakes with wildcards
2130 @cindex errors with wildcards
2131 @cindex problems with wildcards
2133 Now here is an example of a naive way of using wildcard expansion, that
2134 does not do what you would intend. Suppose you would like to say that the
2135 executable file @file{foo} is made from all the object files in the
2136 directory, and you write this:
2142 cc -o foo $(CFLAGS) $(objects)
2146 The value of @code{objects} is the actual string @samp{*.o}. Wildcard
2147 expansion happens in the rule for @file{foo}, so that each @emph{existing}
2148 @samp{.o} file becomes a prerequisite of @file{foo} and will be recompiled if
2151 But what if you delete all the @samp{.o} files? When a wildcard matches
2152 no files, it is left as it is, so then @file{foo} will depend on the
2153 oddly-named file @file{*.o}. Since no such file is likely to exist,
2154 @code{make} will give you an error saying it cannot figure out how to
2155 make @file{*.o}. This is not what you want!
2157 Actually it is possible to obtain the desired result with wildcard
2158 expansion, but you need more sophisticated techniques, including the
2159 @code{wildcard} function and string substitution.
2161 @xref{Wildcard Function, ,The Function @code{wildcard}}.
2164 These are described in the following section.
2167 @cindex wildcards and MS-DOS/MS-Windows backslashes
2168 @cindex backslashes in pathnames and wildcard expansion
2170 Microsoft operating systems (MS-DOS and MS-Windows) use backslashes to
2171 separate directories in pathnames, like so:
2177 This is equivalent to the Unix-style @file{c:/foo/bar/baz.c} (the
2178 @file{c:} part is the so-called drive letter). When @code{make} runs on
2179 these systems, it supports backslashes as well as the Unix-style forward
2180 slashes in pathnames. However, this support does @emph{not} include the
2181 wildcard expansion, where backslash is a quote character. Therefore,
2182 you @emph{must} use Unix-style slashes in these cases.
2185 @node Wildcard Function, , Wildcard Pitfall, Wildcards
2186 @subsection The Function @code{wildcard}
2189 Wildcard expansion happens automatically in rules. But wildcard expansion
2190 does not normally take place when a variable is set, or inside the
2191 arguments of a function. If you want to do wildcard expansion in such
2192 places, you need to use the @code{wildcard} function, like this:
2195 $(wildcard @var{pattern}@dots{})
2199 This string, used anywhere in a makefile, is replaced by a
2200 space-separated list of names of existing files that match one of the
2201 given file name patterns. If no existing file name matches a pattern,
2202 then that pattern is omitted from the output of the @code{wildcard}
2203 function. Note that this is different from how unmatched wildcards
2204 behave in rules, where they are used verbatim rather than ignored
2205 (@pxref{Wildcard Pitfall}).
2207 One use of the @code{wildcard} function is to get a list of all the C source
2208 files in a directory, like this:
2214 We can change the list of C source files into a list of object files by
2215 replacing the @samp{.c} suffix with @samp{.o} in the result, like this:
2218 $(patsubst %.c,%.o,$(wildcard *.c))
2222 (Here we have used another function, @code{patsubst}.
2223 @xref{Text Functions, ,Functions for String Substitution and Analysis}.)@refill
2225 Thus, a makefile to compile all C source files in the directory and then
2226 link them together could be written as follows:
2229 objects := $(patsubst %.c,%.o,$(wildcard *.c))
2232 cc -o foo $(objects)
2236 (This takes advantage of the implicit rule for compiling C programs, so
2237 there is no need to write explicit rules for compiling the files.
2238 @xref{Flavors, ,The Two Flavors of Variables}, for an explanation of
2239 @samp{:=}, which is a variant of @samp{=}.)
2241 @node Directory Search, Phony Targets, Wildcards, Rules
2242 @section Searching Directories for Prerequisites
2246 @cindex search path for prerequisites (@code{VPATH})
2247 @cindex directory search (@code{VPATH})
2249 For large systems, it is often desirable to put sources in a separate
2250 directory from the binaries. The @dfn{directory search} features of
2251 @code{make} facilitate this by searching several directories
2252 automatically to find a prerequisite. When you redistribute the files
2253 among directories, you do not need to change the individual rules,
2254 just the search paths.
2257 * General Search:: Specifying a search path that applies
2258 to every prerequisite.
2259 * Selective Search:: Specifying a search path
2260 for a specified class of names.
2261 * Search Algorithm:: When and how search paths are applied.
2262 * Commands/Search:: How to write shell commands that work together
2264 * Implicit/Search:: How search paths affect implicit rules.
2265 * Libraries/Search:: Directory search for link libraries.
2268 @node General Search, Selective Search, Directory Search, Directory Search
2269 @subsection @code{VPATH}: Search Path for All Prerequisites
2272 The value of the @code{make} variable @code{VPATH} specifies a list of
2273 directories that @code{make} should search. Most often, the
2274 directories are expected to contain prerequisite files that are not in the
2275 current directory; however, @code{make} uses @code{VPATH} as a search
2276 list for both prerequisites and targets of rules.
2278 Thus, if a file that is listed as a target or prerequisite does not exist
2279 in the current directory, @code{make} searches the directories listed in
2280 @code{VPATH} for a file with that name. If a file is found in one of
2281 them, that file may become the prerequisite (see below). Rules may then
2282 specify the names of files in the prerequisite list as if they all
2283 existed in the current directory. @xref{Commands/Search, ,Writing Shell
2284 Commands with Directory Search}.
2286 In the @code{VPATH} variable, directory names are separated by colons or
2287 blanks. The order in which directories are listed is the order followed
2288 by @code{make} in its search. (On MS-DOS and MS-Windows, semi-colons
2289 are used as separators of directory names in @code{VPATH}, since the
2290 colon can be used in the pathname itself, after the drive letter.)
2295 VPATH = src:../headers
2299 specifies a path containing two directories, @file{src} and
2300 @file{../headers}, which @code{make} searches in that order.
2302 With this value of @code{VPATH}, the following rule,
2309 is interpreted as if it were written like this:
2316 assuming the file @file{foo.c} does not exist in the current directory but
2317 is found in the directory @file{src}.
2319 @node Selective Search, Search Algorithm, General Search, Directory Search
2320 @subsection The @code{vpath} Directive
2323 Similar to the @code{VPATH} variable, but more selective, is the
2324 @code{vpath} directive (note lower case), which allows you to specify a
2325 search path for a particular class of file names: those that match a
2326 particular pattern. Thus you can supply certain search directories for
2327 one class of file names and other directories (or none) for other file
2330 There are three forms of the @code{vpath} directive:
2333 @item vpath @var{pattern} @var{directories}
2334 Specify the search path @var{directories} for file names that match
2337 The search path, @var{directories}, is a list of directories to be
2338 searched, separated by colons (semi-colons on MS-DOS and MS-Windows) or
2339 blanks, just like the search path used in the @code{VPATH} variable.
2341 @item vpath @var{pattern}
2342 Clear out the search path associated with @var{pattern}.
2344 @c Extra blank line makes sure this gets two lines.
2347 Clear all search paths previously specified with @code{vpath} directives.
2350 A @code{vpath} pattern is a string containing a @samp{%} character. The
2351 string must match the file name of a prerequisite that is being searched
2352 for, the @samp{%} character matching any sequence of zero or more
2353 characters (as in pattern rules; @pxref{Pattern Rules, ,Defining and
2354 Redefining Pattern Rules}). For example, @code{%.h} matches files that
2355 end in @code{.h}. (If there is no @samp{%}, the pattern must match the
2356 prerequisite exactly, which is not useful very often.)
2358 @cindex @code{%}, quoting in @code{vpath}
2359 @cindex @code{%}, quoting with @code{\} (backslash)
2360 @cindex @code{\} (backslash), to quote @code{%}
2361 @cindex backslash (@code{\}), to quote @code{%}
2362 @cindex quoting @code{%}, in @code{vpath}
2363 @samp{%} characters in a @code{vpath} directive's pattern can be quoted
2364 with preceding backslashes (@samp{\}). Backslashes that would otherwise
2365 quote @samp{%} characters can be quoted with more backslashes.
2366 Backslashes that quote @samp{%} characters or other backslashes are
2367 removed from the pattern before it is compared to file names. Backslashes
2368 that are not in danger of quoting @samp{%} characters go unmolested.@refill
2370 When a prerequisite fails to exist in the current directory, if the
2371 @var{pattern} in a @code{vpath} directive matches the name of the
2372 prerequisite file, then the @var{directories} in that directive are searched
2373 just like (and before) the directories in the @code{VPATH} variable.
2378 vpath %.h ../headers
2382 tells @code{make} to look for any prerequisite whose name ends in @file{.h}
2383 in the directory @file{../headers} if the file is not found in the current
2386 If several @code{vpath} patterns match the prerequisite file's name, then
2387 @code{make} processes each matching @code{vpath} directive one by one,
2388 searching all the directories mentioned in each directive. @code{make}
2389 handles multiple @code{vpath} directives in the order in which they
2390 appear in the makefile; multiple directives with the same pattern are
2391 independent of each other.
2405 will look for a file ending in @samp{.c} in @file{foo}, then
2406 @file{blish}, then @file{bar}, while
2416 will look for a file ending in @samp{.c} in @file{foo}, then
2417 @file{bar}, then @file{blish}.
2419 @node Search Algorithm, Commands/Search, Selective Search, Directory Search
2420 @subsection How Directory Searches are Performed
2421 @cindex algorithm for directory search
2422 @cindex directory search algorithm
2424 When a prerequisite is found through directory search, regardless of type
2425 (general or selective), the pathname located may not be the one that
2426 @code{make} actually provides you in the prerequisite list. Sometimes
2427 the path discovered through directory search is thrown away.
2429 The algorithm @code{make} uses to decide whether to keep or abandon a
2430 path found via directory search is as follows:
2434 If a target file does not exist at the path specified in the makefile,
2435 directory search is performed.
2438 If the directory search is successful, that path is kept and this file
2439 is tentatively stored as the target.
2442 All prerequisites of this target are examined using this same method.
2445 After processing the prerequisites, the target may or may not need to be
2450 If the target does @emph{not} need to be rebuilt, the path to the file
2451 found during directory search is used for any prerequisite lists which
2452 contain this target. In short, if @code{make} doesn't need to rebuild
2453 the target then you use the path found via directory search.
2456 If the target @emph{does} need to be rebuilt (is out-of-date), the
2457 pathname found during directory search is @emph{thrown away}, and the
2458 target is rebuilt using the file name specified in the makefile. In
2459 short, if @code{make} must rebuild, then the target is rebuilt locally,
2460 not in the directory found via directory search.
2464 This algorithm may seem complex, but in practice it is quite often
2465 exactly what you want.
2467 @cindex traditional directory search (GPATH)
2468 @cindex directory search, traditional (GPATH)
2469 Other versions of @code{make} use a simpler algorithm: if the file does
2470 not exist, and it is found via directory search, then that pathname is
2471 always used whether or not the target needs to be built. Thus, if the
2472 target is rebuilt it is created at the pathname discovered during
2476 If, in fact, this is the behavior you want for some or all of your
2477 directories, you can use the @code{GPATH} variable to indicate this to
2480 @code{GPATH} has the same syntax and format as @code{VPATH} (that is, a
2481 space- or colon-delimited list of pathnames). If an out-of-date target
2482 is found by directory search in a directory that also appears in
2483 @code{GPATH}, then that pathname is not thrown away. The target is
2484 rebuilt using the expanded path.
2486 @node Commands/Search, Implicit/Search, Search Algorithm, Directory Search
2487 @subsection Writing Shell Commands with Directory Search
2488 @cindex shell command, and directory search
2489 @cindex directory search (@code{VPATH}), and shell commands
2491 When a prerequisite is found in another directory through directory search,
2492 this cannot change the commands of the rule; they will execute as written.
2493 Therefore, you must write the commands with care so that they will look for
2494 the prerequisite in the directory where @code{make} finds it.
2496 This is done with the @dfn{automatic variables} such as @samp{$^}
2497 (@pxref{Automatic Variables}).
2498 For instance, the value of @samp{$^} is a
2499 list of all the prerequisites of the rule, including the names of
2500 the directories in which they were found, and the value of
2501 @samp{$@@} is the target. Thus:@refill
2505 cc -c $(CFLAGS) $^ -o $@@
2509 (The variable @code{CFLAGS} exists so you can specify flags for C
2510 compilation by implicit rules; we use it here for consistency so it will
2511 affect all C compilations uniformly;
2512 @pxref{Implicit Variables, ,Variables Used by Implicit Rules}.)
2514 Often the prerequisites include header files as well, which you do not
2515 want to mention in the commands. The automatic variable @samp{$<} is
2516 just the first prerequisite:
2519 VPATH = src:../headers
2520 foo.o : foo.c defs.h hack.h
2521 cc -c $(CFLAGS) $< -o $@@
2524 @node Implicit/Search, Libraries/Search, Commands/Search, Directory Search
2525 @subsection Directory Search and Implicit Rules
2526 @cindex @code{VPATH}, and implicit rules
2527 @cindex directory search (@code{VPATH}), and implicit rules
2528 @cindex search path for prerequisites (@code{VPATH}), and implicit rules
2529 @cindex implicit rule, and directory search
2530 @cindex implicit rule, and @code{VPATH}
2531 @cindex rule, implicit, and directory search
2532 @cindex rule, implicit, and @code{VPATH}
2534 The search through the directories specified in @code{VPATH} or with
2535 @code{vpath} also happens during consideration of implicit rules
2536 (@pxref{Implicit Rules, ,Using Implicit Rules}).
2538 For example, when a file @file{foo.o} has no explicit rule, @code{make}
2539 considers implicit rules, such as the built-in rule to compile
2540 @file{foo.c} if that file exists. If such a file is lacking in the
2541 current directory, the appropriate directories are searched for it. If
2542 @file{foo.c} exists (or is mentioned in the makefile) in any of the
2543 directories, the implicit rule for C compilation is applied.
2545 The commands of implicit rules normally use automatic variables as a
2546 matter of necessity; consequently they will use the file names found by
2547 directory search with no extra effort.
2549 @node Libraries/Search, , Implicit/Search, Directory Search
2550 @subsection Directory Search for Link Libraries
2551 @cindex link libraries, and directory search
2552 @cindex libraries for linking, directory search
2553 @cindex directory search (@code{VPATH}), and link libraries
2554 @cindex @code{VPATH}, and link libraries
2555 @cindex search path for prerequisites (@code{VPATH}), and link libraries
2556 @cindex @code{-l} (library search)
2557 @cindex link libraries, patterns matching
2558 @cindex @code{.LIBPATTERNS}, and link libraries
2559 @vindex .LIBPATTERNS
2561 Directory search applies in a special way to libraries used with the
2562 linker. This special feature comes into play when you write a prerequisite
2563 whose name is of the form @samp{-l@var{name}}. (You can tell something
2564 strange is going on here because the prerequisite is normally the name of a
2565 file, and the @emph{file name} of a library generally looks like
2566 @file{lib@var{name}.a}, not like @samp{-l@var{name}}.)@refill
2568 When a prerequisite's name has the form @samp{-l@var{name}}, @code{make}
2569 handles it specially by searching for the file @file{lib@var{name}.so} in
2570 the current directory, in directories specified by matching @code{vpath}
2571 search paths and the @code{VPATH} search path, and then in the
2572 directories @file{/lib}, @file{/usr/lib}, and @file{@var{prefix}/lib}
2573 (normally @file{/usr/local/lib}, but MS-DOS/MS-Windows versions of
2574 @code{make} behave as if @var{prefix} is defined to be the root of the
2575 DJGPP installation tree).
2577 If that file is not found, then the file @file{lib@var{name}.a} is
2578 searched for, in the same directories as above.
2580 For example, if there is a @file{/usr/lib/libcurses.a} library on your
2581 system (and no @file{/usr/lib/libcurses.so} file), then
2585 foo : foo.c -lcurses
2591 would cause the command @samp{cc foo.c /usr/lib/libcurses.a -o foo} to
2592 be executed when @file{foo} is older than @file{foo.c} or than
2593 @file{/usr/lib/libcurses.a}.@refill
2595 Although the default set of files to be searched for is
2596 @file{lib@var{name}.so} and @file{lib@var{name}.a}, this is customizable
2597 via the @code{.LIBPATTERNS} variable. Each word in the value of this
2598 variable is a pattern string. When a prerequisite like
2599 @samp{-l@var{name}} is seen, @code{make} will replace the percent in
2600 each pattern in the list with @var{name} and perform the above directory
2601 searches using that library filename. If no library is found, the next
2602 word in the list will be used.
2604 The default value for @code{.LIBPATTERNS} is @samp{lib%.so lib%.a},
2605 which provides the default behavior described above.
2607 You can turn off link library expansion completely by setting this
2608 variable to an empty value.
2610 @node Phony Targets, Force Targets, Directory Search, Rules
2611 @section Phony Targets
2612 @cindex phony targets
2613 @cindex targets, phony
2614 @cindex targets without a file
2616 A phony target is one that is not really the name of a file. It is just a
2617 name for some commands to be executed when you make an explicit request.
2618 There are two reasons to use a phony target: to avoid a conflict with
2619 a file of the same name, and to improve performance.
2621 If you write a rule whose commands will not create the target file, the
2622 commands will be executed every time the target comes up for remaking.
2633 Because the @code{rm} command does not create a file named @file{clean},
2634 probably no such file will ever exist. Therefore, the @code{rm} command
2635 will be executed every time you say @samp{make clean}.
2636 @cindex @code{rm} (shell command)
2639 The phony target will cease to work if anything ever does create a file
2640 named @file{clean} in this directory. Since it has no prerequisites, the
2641 file @file{clean} would inevitably be considered up to date, and its
2642 commands would not be executed. To avoid this problem, you can explicitly
2643 declare the target to be phony, using the special target @code{.PHONY}
2644 (@pxref{Special Targets, ,Special Built-in Target Names}) as follows:
2651 Once this is done, @samp{make clean} will run the commands regardless of
2652 whether there is a file named @file{clean}.
2654 Since it knows that phony targets do not name actual files that could be
2655 remade from other files, @code{make} skips the implicit rule search for
2656 phony targets (@pxref{Implicit Rules}). This is why declaring a target
2657 phony is good for performance, even if you are not worried about the
2658 actual file existing.
2660 Thus, you first write the line that states that @code{clean} is a
2661 phony target, then you write the rule, like this:
2671 Another example of the usefulness of phony targets is in conjunction
2672 with recursive invocations of @code{make} (for more information, see
2673 @ref{Recursion, ,Recursive Use of @code{make}}). In this case the
2674 makefile will often contain a variable which lists a number of
2675 subdirectories to be built. One way to handle this is with one rule
2676 whose command is a shell loop over the subdirectories, like this:
2680 SUBDIRS = foo bar baz
2683 for dir in $(SUBDIRS); do \
2689 There are a few problems with this method, however. First, any error
2690 detected in a submake is not noted by this rule, so it will continue to
2691 build the rest of the directories even when one fails. This can be
2692 overcome by adding shell commands to note the error and exit, but then
2693 it will do so even if @code{make} is invoked with the @code{-k} option,
2694 which is unfortunate. Second, and perhaps more importantly, you cannot
2695 take advantage of @code{make}'s ability to build targets in parallel
2696 (@pxref{Parallel, ,Parallel Execution}), since there is only one rule.
2698 By declaring the subdirectories as phony targets (you must do this as
2699 the subdirectory obviously always exists; otherwise it won't be built)
2700 you can remove these problems:
2704 SUBDIRS = foo bar baz
2706 .PHONY: subdirs $(SUBDIRS)
2717 Here we've also declared that the @file{foo} subdirectory cannot be
2718 built until after the @file{baz} subdirectory is complete; this kind of
2719 relationship declaration is particularly important when attempting
2722 A phony target should not be a prerequisite of a real target file; if it
2723 is, its commands are run every time @code{make} goes to update that
2724 file. As long as a phony target is never a prerequisite of a real
2725 target, the phony target commands will be executed only when the phony
2726 target is a specified goal (@pxref{Goals, ,Arguments to Specify the
2729 Phony targets can have prerequisites. When one directory contains multiple
2730 programs, it is most convenient to describe all of the programs in one
2731 makefile @file{./Makefile}. Since the target remade by default will be the
2732 first one in the makefile, it is common to make this a phony target named
2733 @samp{all} and give it, as prerequisites, all the individual programs. For
2737 all : prog1 prog2 prog3
2740 prog1 : prog1.o utils.o
2741 cc -o prog1 prog1.o utils.o
2746 prog3 : prog3.o sort.o utils.o
2747 cc -o prog3 prog3.o sort.o utils.o
2751 Now you can say just @samp{make} to remake all three programs, or
2752 specify as arguments the ones to remake (as in @samp{make prog1
2753 prog3}). Phoniness is not inherited: the prerequisites of a phony
2754 target are not themselves phony, unless explicitly declared to be so.
2756 When one phony target is a prerequisite of another, it serves as a subroutine
2757 of the other. For example, here @samp{make cleanall} will delete the
2758 object files, the difference files, and the file @file{program}:
2761 .PHONY: cleanall cleanobj cleandiff
2763 cleanall : cleanobj cleandiff
2773 @node Force Targets, Empty Targets, Phony Targets, Rules
2774 @section Rules without Commands or Prerequisites
2775 @cindex force targets
2776 @cindex targets, force
2777 @cindex @code{FORCE}
2778 @cindex rule, no commands or prerequisites
2780 If a rule has no prerequisites or commands, and the target of the rule
2781 is a nonexistent file, then @code{make} imagines this target to have
2782 been updated whenever its rule is run. This implies that all targets
2783 depending on this one will always have their commands run.
2785 An example will illustrate this:
2795 Here the target @samp{FORCE} satisfies the special conditions, so the
2796 target @file{clean} that depends on it is forced to run its commands.
2797 There is nothing special about the name @samp{FORCE}, but that is one name
2798 commonly used this way.
2800 As you can see, using @samp{FORCE} this way has the same results as using
2801 @samp{.PHONY: clean}.
2803 Using @samp{.PHONY} is more explicit and more efficient. However,
2804 other versions of @code{make} do not support @samp{.PHONY}; thus
2805 @samp{FORCE} appears in many makefiles. @xref{Phony Targets}.
2807 @node Empty Targets, Special Targets, Force Targets, Rules
2808 @section Empty Target Files to Record Events
2809 @cindex empty targets
2810 @cindex targets, empty
2811 @cindex recording events with empty targets
2813 The @dfn{empty target} is a variant of the phony target; it is used to hold
2814 commands for an action that you request explicitly from time to time.
2815 Unlike a phony target, this target file can really exist; but the file's
2816 contents do not matter, and usually are empty.
2818 The purpose of the empty target file is to record, with its
2819 last-modification time, when the rule's commands were last executed. It
2820 does so because one of the commands is a @code{touch} command to update the
2823 The empty target file should have some prerequisites (otherwise it
2824 doesn't make sense). When you ask to remake the empty target, the
2825 commands are executed if any prerequisite is more recent than the target;
2826 in other words, if a prerequisite has changed since the last time you
2827 remade the target. Here is an example:
2834 @cindex @code{print} target
2835 @cindex @code{lpr} (shell command)
2836 @cindex @code{touch} (shell command)
2839 With this rule, @samp{make print} will execute the @code{lpr} command if
2840 either source file has changed since the last @samp{make print}. The
2841 automatic variable @samp{$?} is used to print only those files that have
2842 changed (@pxref{Automatic Variables}).
2844 @node Special Targets, Multiple Targets, Empty Targets, Rules
2845 @section Special Built-in Target Names
2846 @cindex special targets
2847 @cindex built-in special targets
2848 @cindex targets, built-in special
2850 Certain names have special meanings if they appear as targets.
2856 The prerequisites of the special target @code{.PHONY} are considered to
2857 be phony targets. When it is time to consider such a target,
2858 @code{make} will run its commands unconditionally, regardless of
2859 whether a file with that name exists or what its last-modification
2860 time is. @xref{Phony Targets, ,Phony Targets}.
2865 The prerequisites of the special target @code{.SUFFIXES} are the list
2866 of suffixes to be used in checking for suffix rules.
2867 @xref{Suffix Rules, , Old-Fashioned Suffix Rules}.
2872 The commands specified for @code{.DEFAULT} are used for any target for
2873 which no rules are found (either explicit rules or implicit rules).
2874 @xref{Last Resort}. If @code{.DEFAULT} commands are specified, every
2875 file mentioned as a prerequisite, but not as a target in a rule, will have
2876 these commands executed on its behalf. @xref{Implicit Rule Search,
2877 ,Implicit Rule Search Algorithm}.
2881 @cindex precious targets
2882 @cindex preserving with @code{.PRECIOUS}
2884 The targets which @code{.PRECIOUS} depends on are given the following
2885 special treatment: if @code{make} is killed or interrupted during the
2886 execution of their commands, the target is not deleted.
2887 @xref{Interrupts, ,Interrupting or Killing @code{make}}. Also, if the
2888 target is an intermediate file, it will not be deleted after it is no
2889 longer needed, as is normally done. @xref{Chained Rules, ,Chains of
2890 Implicit Rules}. In this latter respect it overlaps with the
2891 @code{.SECONDARY} special target.
2893 You can also list the target pattern of an implicit rule (such as
2894 @samp{%.o}) as a prerequisite file of the special target @code{.PRECIOUS}
2895 to preserve intermediate files created by rules whose target patterns
2896 match that file's name.
2898 @findex .INTERMEDIATE
2900 @cindex intermediate targets, explicit
2902 The targets which @code{.INTERMEDIATE} depends on are treated as
2903 intermediate files. @xref{Chained Rules, ,Chains of Implicit Rules}.
2904 @code{.INTERMEDIATE} with no prerequisites has no effect.
2908 @cindex secondary targets
2909 @cindex preserving with @code{.SECONDARY}
2911 The targets which @code{.SECONDARY} depends on are treated as
2912 intermediate files, except that they are never automatically deleted.
2913 @xref{Chained Rules, ,Chains of Implicit Rules}.
2915 @code{.SECONDARY} with no prerequisites causes all targets to be treated
2916 as secondary (i.e., no target is removed because it is considered
2919 @findex .SECONDEXPANSION
2920 @item .SECONDEXPANSION
2922 If @code{.SECONDEXPANSION} is mentioned as a target anywhere in the
2923 makefile, then all prerequisite lists defined @emph{after} it appears
2924 will be expanded a second time after all makefiles have been read in.
2925 @xref{Secondary Expansion, ,Secondary Expansion}.
2927 The prerequisites of the special target @code{.SUFFIXES} are the list
2928 of suffixes to be used in checking for suffix rules.
2929 @xref{Suffix Rules, , Old-Fashioned Suffix Rules}.
2931 @findex .DELETE_ON_ERROR
2932 @item .DELETE_ON_ERROR
2933 @cindex removing targets on failure
2935 If @code{.DELETE_ON_ERROR} is mentioned as a target anywhere in the
2936 makefile, then @code{make} will delete the target of a rule if it has
2937 changed and its commands exit with a nonzero exit status, just as it
2938 does when it receives a signal. @xref{Errors, ,Errors in Commands}.
2943 If you specify prerequisites for @code{.IGNORE}, then @code{make} will
2944 ignore errors in execution of the commands run for those particular
2945 files. The commands for @code{.IGNORE} are not meaningful.
2947 If mentioned as a target with no prerequisites, @code{.IGNORE} says to
2948 ignore errors in execution of commands for all files. This usage of
2949 @samp{.IGNORE} is supported only for historical compatibility. Since
2950 this affects every command in the makefile, it is not very useful; we
2951 recommend you use the more selective ways to ignore errors in specific
2952 commands. @xref{Errors, ,Errors in Commands}.
2954 @findex .LOW_RESOLUTION_TIME
2955 @item .LOW_RESOLUTION_TIME
2957 If you specify prerequisites for @code{.LOW_RESOLUTION_TIME},
2958 @command{make} assumes that these files are created by commands that
2959 generate low resolution time stamps. The commands for
2960 @code{.LOW_RESOLUTION_TIME} are not meaningful.
2962 The high resolution file time stamps of many modern hosts lessen the
2963 chance of @command{make} incorrectly concluding that a file is up to
2964 date. Unfortunately, these hosts provide no way to set a high
2965 resolution file time stamp, so commands like @samp{cp -p} that
2966 explicitly set a file's time stamp must discard its subsecond part. If
2967 a file is created by such a command, you should list it as a
2968 prerequisite of @code{.LOW_RESOLUTION_TIME} so that @command{make} does
2969 not mistakenly conclude that the file is out of date. For example:
2973 .LOW_RESOLUTION_TIME: dst
2979 Since @samp{cp -p} discards the subsecond part of @file{src}'s time
2980 stamp, @file{dst} is typically slightly older than @file{src} even when
2981 it is up to date. The @code{.LOW_RESOLUTION_TIME} line causes
2982 @command{make} to consider @file{dst} to be up to date if its time stamp
2983 is at the start of the same second that @file{src}'s time stamp is in.
2985 Due to a limitation of the archive format, archive member time stamps
2986 are always low resolution. You need not list archive members as
2987 prerequisites of @code{.LOW_RESOLUTION_TIME}, as @command{make} does this
2993 If you specify prerequisites for @code{.SILENT}, then @code{make} will
2994 not print the commands to remake those particular files before executing
2995 them. The commands for @code{.SILENT} are not meaningful.
2997 If mentioned as a target with no prerequisites, @code{.SILENT} says not
2998 to print any commands before executing them. This usage of
2999 @samp{.SILENT} is supported only for historical compatibility. We
3000 recommend you use the more selective ways to silence specific commands.
3001 @xref{Echoing, ,Command Echoing}. If you want to silence all commands
3002 for a particular run of @code{make}, use the @samp{-s} or
3003 @w{@samp{--silent}} option (@pxref{Options Summary}).
3005 @findex .EXPORT_ALL_VARIABLES
3006 @item .EXPORT_ALL_VARIABLES
3008 Simply by being mentioned as a target, this tells @code{make} to
3009 export all variables to child processes by default.
3010 @xref{Variables/Recursion, ,Communicating Variables to a
3013 @findex .NOTPARALLEL
3015 @cindex parallel execution, overriding
3017 If @code{.NOTPARALLEL} is mentioned as a target, then this invocation of
3018 @code{make} will be run serially, even if the @samp{-j} option is
3019 given. Any recursively invoked @code{make} command will still be run in
3020 parallel (unless its makefile contains this target). Any prerequisites
3021 on this target are ignored.
3024 Any defined implicit rule suffix also counts as a special target if it
3025 appears as a target, and so does the concatenation of two suffixes, such
3026 as @samp{.c.o}. These targets are suffix rules, an obsolete way of
3027 defining implicit rules (but a way still widely used). In principle, any
3028 target name could be special in this way if you break it in two and add
3029 both pieces to the suffix list. In practice, suffixes normally begin with
3030 @samp{.}, so these special target names also begin with @samp{.}.
3031 @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
3033 @node Multiple Targets, Multiple Rules, Special Targets, Rules
3034 @section Multiple Targets in a Rule
3035 @cindex multiple targets
3036 @cindex several targets in a rule
3037 @cindex targets, multiple
3038 @cindex rule, with multiple targets
3040 A rule with multiple targets is equivalent to writing many rules, each with
3041 one target, and all identical aside from that. The same commands apply to
3042 all the targets, but their effects may vary because you can substitute the
3043 actual target name into the command using @samp{$@@}. The rule contributes
3044 the same prerequisites to all the targets also.
3046 This is useful in two cases.
3050 You want just prerequisites, no commands. For example:
3053 kbd.o command.o files.o: command.h
3057 gives an additional prerequisite to each of the three object files
3061 Similar commands work for all the targets. The commands do not need
3062 to be absolutely identical, since the automatic variable @samp{$@@}
3063 can be used to substitute the particular target to be remade into the
3064 commands (@pxref{Automatic Variables}). For example:
3068 bigoutput littleoutput : text.g
3069 generate text.g -$(subst output,,$@@) > $@@
3079 generate text.g -big > bigoutput
3080 littleoutput : text.g
3081 generate text.g -little > littleoutput
3085 Here we assume the hypothetical program @code{generate} makes two
3086 types of output, one if given @samp{-big} and one if given
3088 @xref{Text Functions, ,Functions for String Substitution and Analysis},
3089 for an explanation of the @code{subst} function.
3092 Suppose you would like to vary the prerequisites according to the target,
3093 much as the variable @samp{$@@} allows you to vary the commands.
3094 You cannot do this with multiple targets in an ordinary rule, but you can
3095 do it with a @dfn{static pattern rule}.
3096 @xref{Static Pattern, ,Static Pattern Rules}.
3098 @node Multiple Rules, Static Pattern, Multiple Targets, Rules
3099 @section Multiple Rules for One Target
3100 @cindex multiple rules for one target
3101 @cindex several rules for one target
3102 @cindex rule, multiple for one target
3103 @cindex target, multiple rules for one
3105 One file can be the target of several rules. All the prerequisites
3106 mentioned in all the rules are merged into one list of prerequisites for
3107 the target. If the target is older than any prerequisite from any rule,
3108 the commands are executed.
3110 There can only be one set of commands to be executed for a file. If
3111 more than one rule gives commands for the same file, @code{make} uses
3112 the last set given and prints an error message. (As a special case,
3113 if the file's name begins with a dot, no error message is printed.
3114 This odd behavior is only for compatibility with other implementations
3115 of @code{make}... you should avoid using it). Occasionally it is
3116 useful to have the same target invoke multiple commands which are
3117 defined in different parts of your makefile; you can use
3118 @dfn{double-colon rules} (@pxref{Double-Colon}) for this.
3120 An extra rule with just prerequisites can be used to give a few extra
3121 prerequisites to many files at once. For example, makefiles often
3122 have a variable, such as @code{objects}, containing a list of all the
3123 compiler output files in the system being made. An easy way to say
3124 that all of them must be recompiled if @file{config.h} changes is to
3125 write the following:
3128 objects = foo.o bar.o
3130 bar.o : defs.h test.h
3131 $(objects) : config.h
3134 This could be inserted or taken out without changing the rules that really
3135 specify how to make the object files, making it a convenient form to use if
3136 you wish to add the additional prerequisite intermittently.
3138 Another wrinkle is that the additional prerequisites could be specified with
3139 a variable that you set with a command argument to @code{make}
3140 (@pxref{Overriding, ,Overriding Variables}). For example,
3145 $(objects) : $(extradeps)
3150 means that the command @samp{make extradeps=foo.h} will consider
3151 @file{foo.h} as a prerequisite of each object file, but plain @samp{make}
3154 If none of the explicit rules for a target has commands, then @code{make}
3155 searches for an applicable implicit rule to find some commands
3156 @pxref{Implicit Rules, ,Using Implicit Rules}).
3158 @node Static Pattern, Double-Colon, Multiple Rules, Rules
3159 @section Static Pattern Rules
3160 @cindex static pattern rule
3161 @cindex rule, static pattern
3162 @cindex pattern rules, static (not implicit)
3163 @cindex varying prerequisites
3164 @cindex prerequisites, varying (static pattern)
3166 @dfn{Static pattern rules} are rules which specify multiple targets and
3167 construct the prerequisite names for each target based on the target name.
3168 They are more general than ordinary rules with multiple targets because the
3169 targets do not have to have identical prerequisites. Their prerequisites must
3170 be @emph{analogous}, but not necessarily @emph{identical}.
3173 * Static Usage:: The syntax of static pattern rules.
3174 * Static versus Implicit:: When are they better than implicit rules?
3177 @node Static Usage, Static versus Implicit, Static Pattern, Static Pattern
3178 @subsection Syntax of Static Pattern Rules
3179 @cindex static pattern rule, syntax of
3180 @cindex pattern rules, static, syntax of
3182 Here is the syntax of a static pattern rule:
3185 @var{targets} @dots{}: @var{target-pattern}: @var{prereq-patterns} @dots{}
3191 The @var{targets} list specifies the targets that the rule applies to.
3192 The targets can contain wildcard characters, just like the targets of
3193 ordinary rules (@pxref{Wildcards, ,Using Wildcard Characters in File
3196 @cindex target pattern, static (not implicit)
3198 The @var{target-pattern} and @var{prereq-patterns} say how to compute the
3199 prerequisites of each target. Each target is matched against the
3200 @var{target-pattern} to extract a part of the target name, called the
3201 @dfn{stem}. This stem is substituted into each of the @var{prereq-patterns}
3202 to make the prerequisite names (one from each @var{prereq-pattern}).
3204 Each pattern normally contains the character @samp{%} just once. When the
3205 @var{target-pattern} matches a target, the @samp{%} can match any part of
3206 the target name; this part is called the @dfn{stem}. The rest of the
3207 pattern must match exactly. For example, the target @file{foo.o} matches
3208 the pattern @samp{%.o}, with @samp{foo} as the stem. The targets
3209 @file{foo.c} and @file{foo.out} do not match that pattern.@refill
3211 @cindex prerequisite pattern, static (not implicit)
3212 The prerequisite names for each target are made by substituting the stem
3213 for the @samp{%} in each prerequisite pattern. For example, if one
3214 prerequisite pattern is @file{%.c}, then substitution of the stem
3215 @samp{foo} gives the prerequisite name @file{foo.c}. It is legitimate
3216 to write a prerequisite pattern that does not contain @samp{%}; then this
3217 prerequisite is the same for all targets.
3219 @cindex @code{%}, quoting in static pattern
3220 @cindex @code{%}, quoting with @code{\} (backslash)
3221 @cindex @code{\} (backslash), to quote @code{%}
3222 @cindex backslash (@code{\}), to quote @code{%}
3223 @cindex quoting @code{%}, in static pattern
3224 @samp{%} characters in pattern rules can be quoted with preceding
3225 backslashes (@samp{\}). Backslashes that would otherwise quote @samp{%}
3226 characters can be quoted with more backslashes. Backslashes that quote
3227 @samp{%} characters or other backslashes are removed from the pattern
3228 before it is compared to file names or has a stem substituted into it.
3229 Backslashes that are not in danger of quoting @samp{%} characters go
3230 unmolested. For example, the pattern @file{the\%weird\\%pattern\\} has
3231 @samp{the%weird\} preceding the operative @samp{%} character, and
3232 @samp{pattern\\} following it. The final two backslashes are left alone
3233 because they cannot affect any @samp{%} character.@refill
3235 Here is an example, which compiles each of @file{foo.o} and @file{bar.o}
3236 from the corresponding @file{.c} file:
3240 objects = foo.o bar.o
3244 $(objects): %.o: %.c
3245 $(CC) -c $(CFLAGS) $< -o $@@
3250 Here @samp{$<} is the automatic variable that holds the name of the
3251 prerequisite and @samp{$@@} is the automatic variable that holds the name
3252 of the target; see @ref{Automatic Variables}.
3254 Each target specified must match the target pattern; a warning is issued
3255 for each target that does not. If you have a list of files, only some of
3256 which will match the pattern, you can use the @code{filter} function to
3257 remove nonmatching file names (@pxref{Text Functions, ,Functions for String Substitution and Analysis}):
3260 files = foo.elc bar.o lose.o
3262 $(filter %.o,$(files)): %.o: %.c
3263 $(CC) -c $(CFLAGS) $< -o $@@
3264 $(filter %.elc,$(files)): %.elc: %.el
3265 emacs -f batch-byte-compile $<
3269 In this example the result of @samp{$(filter %.o,$(files))} is
3270 @file{bar.o lose.o}, and the first static pattern rule causes each of
3271 these object files to be updated by compiling the corresponding C source
3272 file. The result of @w{@samp{$(filter %.elc,$(files))}} is
3273 @file{foo.elc}, so that file is made from @file{foo.el}.@refill
3275 Another example shows how to use @code{$*} in static pattern rules:
3276 @vindex $*@r{, and static pattern}
3280 bigoutput littleoutput : %output : text.g
3281 generate text.g -$* > $@@
3286 When the @code{generate} command is run, @code{$*} will expand to the
3287 stem, either @samp{big} or @samp{little}.
3289 @node Static versus Implicit, , Static Usage, Static Pattern
3290 @subsection Static Pattern Rules versus Implicit Rules
3291 @cindex rule, static pattern versus implicit
3292 @cindex static pattern rule, versus implicit
3294 A static pattern rule has much in common with an implicit rule defined as a
3295 pattern rule (@pxref{Pattern Rules, ,Defining and Redefining Pattern Rules}).
3296 Both have a pattern for the target and patterns for constructing the
3297 names of prerequisites. The difference is in how @code{make} decides
3298 @emph{when} the rule applies.
3300 An implicit rule @emph{can} apply to any target that matches its pattern,
3301 but it @emph{does} apply only when the target has no commands otherwise
3302 specified, and only when the prerequisites can be found. If more than one
3303 implicit rule appears applicable, only one applies; the choice depends on
3306 By contrast, a static pattern rule applies to the precise list of targets
3307 that you specify in the rule. It cannot apply to any other target and it
3308 invariably does apply to each of the targets specified. If two conflicting
3309 rules apply, and both have commands, that's an error.
3311 The static pattern rule can be better than an implicit rule for these
3316 You may wish to override the usual implicit rule for a few
3317 files whose names cannot be categorized syntactically but
3318 can be given in an explicit list.
3321 If you cannot be sure of the precise contents of the directories
3322 you are using, you may not be sure which other irrelevant files
3323 might lead @code{make} to use the wrong implicit rule. The choice
3324 might depend on the order in which the implicit rule search is done.
3325 With static pattern rules, there is no uncertainty: each rule applies
3326 to precisely the targets specified.
3329 @node Double-Colon, Automatic Prerequisites, Static Pattern, Rules
3330 @section Double-Colon Rules
3331 @cindex double-colon rules
3332 @cindex rule, double-colon (@code{::})
3333 @cindex multiple rules for one target (@code{::})
3334 @cindex @code{::} rules (double-colon)
3336 @dfn{Double-colon} rules are rules written with @samp{::} instead of
3337 @samp{:} after the target names. They are handled differently from
3338 ordinary rules when the same target appears in more than one rule.
3340 When a target appears in multiple rules, all the rules must be the same
3341 type: all ordinary, or all double-colon. If they are double-colon, each
3342 of them is independent of the others. Each double-colon rule's commands
3343 are executed if the target is older than any prerequisites of that rule.
3344 If there are no prerequisites for that rule, its commands are always
3345 executed (even if the target already exists). This can result in
3346 executing none, any, or all of the double-colon rules.
3348 Double-colon rules with the same target are in fact completely separate
3349 from one another. Each double-colon rule is processed individually, just
3350 as rules with different targets are processed.
3352 The double-colon rules for a target are executed in the order they appear
3353 in the makefile. However, the cases where double-colon rules really make
3354 sense are those where the order of executing the commands would not matter.
3356 Double-colon rules are somewhat obscure and not often very useful; they
3357 provide a mechanism for cases in which the method used to update a target
3358 differs depending on which prerequisite files caused the update, and such
3361 Each double-colon rule should specify commands; if it does not, an
3362 implicit rule will be used if one applies.
3363 @xref{Implicit Rules, ,Using Implicit Rules}.
3365 @node Automatic Prerequisites, , Double-Colon, Rules
3366 @section Generating Prerequisites Automatically
3367 @cindex prerequisites, automatic generation
3368 @cindex automatic generation of prerequisites
3369 @cindex generating prerequisites automatically
3371 In the makefile for a program, many of the rules you need to write often
3372 say only that some object file depends on some header
3373 file. For example, if @file{main.c} uses @file{defs.h} via an
3374 @code{#include}, you would write:
3381 You need this rule so that @code{make} knows that it must remake
3382 @file{main.o} whenever @file{defs.h} changes. You can see that for a
3383 large program you would have to write dozens of such rules in your
3384 makefile. And, you must always be very careful to update the makefile
3385 every time you add or remove an @code{#include}.
3386 @cindex @code{#include}
3388 @cindex @code{-M} (to compiler)
3389 To avoid this hassle, most modern C compilers can write these rules for
3390 you, by looking at the @code{#include} lines in the source files.
3391 Usually this is done with the @samp{-M} option to the compiler.
3392 For example, the command:
3399 generates the output:
3402 main.o : main.c defs.h
3406 Thus you no longer have to write all those rules yourself.
3407 The compiler will do it for you.
3409 Note that such a prerequisite constitutes mentioning @file{main.o} in a
3410 makefile, so it can never be considered an intermediate file by implicit
3411 rule search. This means that @code{make} won't ever remove the file
3412 after using it; @pxref{Chained Rules, ,Chains of Implicit Rules}.
3414 @cindex @code{make depend}
3415 With old @code{make} programs, it was traditional practice to use this
3416 compiler feature to generate prerequisites on demand with a command like
3417 @samp{make depend}. That command would create a file @file{depend}
3418 containing all the automatically-generated prerequisites; then the
3419 makefile could use @code{include} to read them in (@pxref{Include}).
3421 In GNU @code{make}, the feature of remaking makefiles makes this
3422 practice obsolete---you need never tell @code{make} explicitly to
3423 regenerate the prerequisites, because it always regenerates any makefile
3424 that is out of date. @xref{Remaking Makefiles}.
3426 The practice we recommend for automatic prerequisite generation is to have
3427 one makefile corresponding to each source file. For each source file
3428 @file{@var{name}.c} there is a makefile @file{@var{name}.d} which lists
3429 what files the object file @file{@var{name}.o} depends on. That way
3430 only the source files that have changed need to be rescanned to produce
3431 the new prerequisites.
3433 Here is the pattern rule to generate a file of prerequisites (i.e., a makefile)
3434 called @file{@var{name}.d} from a C source file called @file{@var{name}.c}:
3439 @@set -e; rm -f $@@; \
3440 $(CC) -M $(CPPFLAGS) $< > $@@.$$$$; \
3441 sed 's,\($*\)\.o[ :]*,\1.o $@@ : ,g' < $@@.$$$$ > $@@; \
3447 @xref{Pattern Rules}, for information on defining pattern rules. The
3448 @samp{-e} flag to the shell causes it to exit immediately if the
3449 @code{$(CC)} command (or any other command) fails (exits with a
3451 @cindex @code{-e} (shell flag)
3453 @cindex @code{-MM} (to GNU compiler)
3454 With the GNU C compiler, you may wish to use the @samp{-MM} flag instead
3455 of @samp{-M}. This omits prerequisites on system header files.
3456 @xref{Preprocessor Options, , Options Controlling the Preprocessor,
3457 gcc.info, Using GNU CC}, for details.
3459 @cindex @code{sed} (shell command)
3460 The purpose of the @code{sed} command is to translate (for example):
3463 main.o : main.c defs.h
3470 main.o main.d : main.c defs.h
3475 This makes each @samp{.d} file depend on all the source and header files
3476 that the corresponding @samp{.o} file depends on. @code{make} then
3477 knows it must regenerate the prerequisites whenever any of the source or
3478 header files changes.
3480 Once you've defined the rule to remake the @samp{.d} files,
3481 you then use the @code{include} directive to read them all in.
3482 @xref{Include}. For example:
3486 sources = foo.c bar.c
3488 include $(sources:.c=.d)
3493 (This example uses a substitution variable reference to translate the
3494 list of source files @samp{foo.c bar.c} into a list of prerequisite
3495 makefiles, @samp{foo.d bar.d}. @xref{Substitution Refs}, for full
3496 information on substitution references.) Since the @samp{.d} files are
3497 makefiles like any others, @code{make} will remake them as necessary
3498 with no further work from you. @xref{Remaking Makefiles}.
3500 Note that the @samp{.d} files contain target definitions; you should
3501 be sure to place the @code{include} directive @emph{after} the first,
3502 default goal in your makefiles or run the risk of having a random
3503 object file become the default goal.
3504 @xref{How Make Works}.
3506 @node Commands, Using Variables, Rules, Top
3507 @chapter Writing the Commands in Rules
3508 @cindex commands, how to write
3509 @cindex rule commands
3510 @cindex writing rule commands
3512 The commands of a rule consist of one or more shell command lines to
3513 be executed, one at a time, in the order they appear. Typically, the
3514 result of executing these commands is that the target of the rule is
3517 Users use many different shell programs, but commands in makefiles are
3518 always interpreted by @file{/bin/sh} unless the makefile specifies
3519 otherwise. @xref{Execution, ,Command Execution}.
3522 * Command Syntax:: Command syntax features and pitfalls.
3523 * Echoing:: How to control when commands are echoed.
3524 * Execution:: How commands are executed.
3525 * Parallel:: How commands can be executed in parallel.
3526 * Errors:: What happens after a command execution error.
3527 * Interrupts:: What happens when a command is interrupted.
3528 * Recursion:: Invoking @code{make} from makefiles.
3529 * Sequences:: Defining canned sequences of commands.
3530 * Empty Commands:: Defining useful, do-nothing commands.
3533 @node Command Syntax, Echoing, Commands, Commands
3534 @section Command Syntax
3535 @cindex command syntax
3536 @cindex syntax of commands
3538 Makefiles have the unusual property that there are really two distinct
3539 syntaxes in one file. Most of the makefile uses @code{make} syntax
3540 (@pxref{Makefiles, ,Writing Makefiles}). However, commands are meant to be
3541 interpreted by the shell and so they are written using shell syntax.
3542 The @code{make} program does not try to understand shell syntax: it
3543 performs only a very few specific translations on the content of the
3544 command before handing it to the shell.
3546 Each command line must start with a tab, except that the first command
3547 line may be attached to the target-and-prerequisites line with a
3548 semicolon in between. @emph{Any} line in the makefile that begins
3549 with a tab and appears in a ``rule context'' (that is, after a rule
3550 has been started until another rule or variable definition) will be
3551 considered a command line for that rule. Blank lines and lines of
3552 just comments may appear among the command lines; they are ignored.
3554 Some consequences of these rules include:
3558 A blank line that begins with a tab is not blank: it's an empty
3559 command (@pxref{Empty Commands}).
3561 @cindex comments, in commands
3562 @cindex commands, comments in
3563 @cindex @code{#} (comments), in commands
3565 A comment in a command line is not a @code{make} comment; it will be
3566 passed to the shell as-is. Whether the shell treats it as a comment
3567 or not depends on your shell.
3570 A variable definition in a ``rule context'' which is indented by a tab
3571 as the first character on the line, will be considered a command line,
3572 not a @code{make} variable definition, and passed to the shell.
3575 A conditional expression (@code{ifdef}, @code{ifeq},
3576 etc. @pxref{Conditional Syntax, ,Syntax of Conditionals}) in a ``rule
3577 context'' which is indented by a tab as the first character on the
3578 line, will be considered a command line and be passed to the shell.
3583 * Splitting Lines:: Breaking long command lines for readability.
3584 * Variables in Commands:: Using @code{make} variables in commands.
3587 @node Splitting Lines, Variables in Commands, Command Syntax, Command Syntax
3588 @subsection Splitting Command Lines
3589 @cindex commands, splitting
3590 @cindex splitting commands
3591 @cindex commands, backslash (@code{\}) in
3592 @cindex commands, quoting newlines in
3593 @cindex backslash (@code{\}), in commands
3594 @cindex @code{\} (backslash), in commands
3595 @cindex quoting newline, in commands
3596 @cindex newline, quoting, in commands
3598 One of the few ways in which @code{make} does interpret command lines
3599 is checking for a backslash just before the newline. As in normal
3600 makefile syntax, a single command can be split into multiple lines in
3601 the makefile by placing a backslash before each newline. A sequence
3602 of lines like this is considered a single command, and one instance of
3603 the shell will be invoked to run it.
3605 However, in contrast to how they are treated in other places in a
3606 makefile, backslash-newline pairs are @emph{not} removed from the
3607 command. Both the backslash and the newline characters are preserved
3608 and passed to the shell. How the backslash-newline is interpreted
3609 depends on your shell. If the first character of the next line
3610 after the backslash-newline is a tab, then that tab (and only that
3611 tab) is removed. Whitespace is never added to the command.
3613 For example, this makefile:
3630 consists of four separate shell commands where the output is:
3641 As a more complex example, this makefile:
3645 all : ; @@echo 'hello \
3646 world' ; echo "hello \
3652 will run one shell with a command script of:
3657 world' ; echo "hello \
3663 which, according to shell quoting rules, will yield the following output:
3674 Notice how the backslash/newline pair was removed inside the string quoted
3675 with double quotes (@code{"..."}), but not from the string quoted with single
3676 quotes (@code{'...'}). This is the way the default shell (@file{/bin/sh})
3677 handles backslash/newline pairs. If you specify a different shell in your
3678 makefiles it may treat them differently.
3680 Sometimes you want to split a long line inside of single quotes, but
3681 you don't want the backslash-newline to appear in the quoted content.
3682 This is often the case when passing scripts to languages such as Perl,
3683 where extraneous backslashes inside the script can change its meaning
3684 or even be a syntax error. One simple way of handling this is to
3685 place the quoted string, or even the entire command, into a
3686 @code{make} variable then use the variable in the command. In this
3687 situation the newline quoting rules for makefiles will be used, and
3688 the backslash-newline will be removed. If we rewrite our example
3689 above using this method:
3696 all : ; @@echo $(HELLO)
3701 we will get output like this:
3709 If you like, you can also use target-specific variables
3710 (@pxref{Target-specific, ,Target-specific Variable Values}) to obtain
3711 a tighter correspondence between the variable and the command that
3714 @node Variables in Commands, , Splitting Lines, Command Syntax
3715 @subsection Using Variables in Commands
3716 @cindex variable references in commands
3717 @cindex commands, using variables in
3719 The other way in which @code{make} processes commands is by expanding
3720 any variable references in them (@pxref{Reference,Basics of Variable
3721 References}). This occurs after make has finished reading all the
3722 makefiles and the target is determined to be out of date; so, the
3723 commands for targets which are not rebuilt are never expanded.
3725 Variable and function references in commands have identical syntax and
3726 semantics to references elsewhere in the makefile. They also have the
3727 same quoting rules: if you want a dollar sign to appear in your
3728 command, you must double it (@samp{$$}). For shells like the default
3729 shell, that use dollar signs to introduce variables, it's important to
3730 keep clear in your mind whether the variable you want to reference is
3731 a @code{make} variable (use a single dollar sign) or a shell variable
3732 (use two dollar signs). For example:
3736 LIST = one two three
3738 for i in $(LIST); do \
3745 results in the following command being passed to the shell:
3749 for i in one two three; do \
3756 which generates the expected result:
3766 @node Echoing, Execution, Command Syntax, Commands
3767 @section Command Echoing
3768 @cindex echoing of commands
3769 @cindex silent operation
3770 @cindex @code{@@} (in commands)
3771 @cindex commands, echoing
3772 @cindex printing of commands
3774 Normally @code{make} prints each command line before it is executed.
3775 We call this @dfn{echoing} because it gives the appearance that you
3776 are typing the commands yourself.
3778 When a line starts with @samp{@@}, the echoing of that line is suppressed.
3779 The @samp{@@} is discarded before the command is passed to the shell.
3780 Typically you would use this for a command whose only effect is to print
3781 something, such as an @code{echo} command to indicate progress through
3785 @@echo About to make distribution files
3789 @cindex @code{--just-print}
3790 @cindex @code{--dry-run}
3791 @cindex @code{--recon}
3792 When @code{make} is given the flag @samp{-n} or @samp{--just-print}
3793 it only echoes commands, it won't execute them. @xref{Options Summary,
3794 ,Summary of Options}. In this case and only this case, even the
3795 commands starting with @samp{@@} are printed. This flag is useful for
3796 finding out which commands @code{make} thinks are necessary without
3797 actually doing them.
3800 @cindex @code{--silent}
3801 @cindex @code{--quiet}
3803 The @samp{-s} or @samp{--silent}
3804 flag to @code{make} prevents all echoing, as if all commands
3805 started with @samp{@@}. A rule in the makefile for the special target
3806 @code{.SILENT} without prerequisites has the same effect
3807 (@pxref{Special Targets, ,Special Built-in Target Names}).
3808 @code{.SILENT} is essentially obsolete since @samp{@@} is more flexible.@refill
3810 @node Execution, Parallel, Echoing, Commands
3811 @section Command Execution
3812 @cindex commands, execution
3813 @cindex execution, of commands
3814 @cindex shell command, execution
3815 @vindex @code{SHELL} @r{(command execution)}
3817 When it is time to execute commands to update a target, they are
3818 executed by invoking a new subshell for each command line. (In
3819 practice, @code{make} may take shortcuts that do not affect the
3822 @cindex @code{cd} (shell command)
3823 @cindex shell variables, setting in commands
3824 @cindex commands setting shell variables
3825 @strong{Please note:} this implies that setting shell variables and
3826 invoking shell commands such as @code{cd} that set a context local to
3827 each process will not affect the following command lines.@footnote{On
3828 MS-DOS, the value of current working directory is @strong{global}, so
3829 changing it @emph{will} affect the following command lines on those
3830 systems.} If you want to use @code{cd} to affect the next statement,
3831 put both statements in a single command line. Then @code{make} will
3832 invoke one shell to run the entire line, and the shell will execute
3833 the statements in sequence. For example:
3837 cd $(@@D) && gobble $(@@F) > ../$@@
3841 Here we use the shell AND operator (@code{&&}) so that if the
3842 @code{cd} command fails, the script will fail without trying to invoke
3843 the @code{gobble} command in the wrong directory, which could cause
3844 problems (in this case it would certainly cause @file{../foo} to be
3845 truncated, at least).
3848 * Choosing the Shell:: How @code{make} chooses the shell used
3852 @node Choosing the Shell, , Execution, Execution
3853 @subsection Choosing the Shell
3854 @cindex shell, choosing the
3855 @cindex @code{SHELL}, value of
3858 The program used as the shell is taken from the variable @code{SHELL}.
3859 If this variable is not set in your makefile, the program
3860 @file{/bin/sh} is used as the shell.
3862 @cindex environment, @code{SHELL} in
3863 Unlike most variables, the variable @code{SHELL} is never set from the
3864 environment. This is because the @code{SHELL} environment variable is
3865 used to specify your personal choice of shell program for interactive
3866 use. It would be very bad for personal choices like this to affect the
3867 functioning of makefiles. @xref{Environment, ,Variables from the
3870 Furthermore, when you do set @code{SHELL} in your makefile that value
3871 is @emph{not} exported in the environment to commands that @code{make}
3872 invokes. Instead, the value inherited from the user's environment, if
3873 any, is exported. You can override this behavior by explicitly
3874 exporting @code{SHELL} (@pxref{Variables/Recursion, ,Communicating
3875 Variables to a Sub-@code{make}}), forcing it to be passed in the
3876 environment to commands.
3878 @vindex @code{MAKESHELL} @r{(MS-DOS alternative to @code{SHELL})}
3879 However, on MS-DOS and MS-Windows the value of @code{SHELL} in the
3880 environment @strong{is} used, since on those systems most users do not
3881 set this variable, and therefore it is most likely set specifically to
3882 be used by @code{make}. On MS-DOS, if the setting of @code{SHELL} is
3883 not suitable for @code{make}, you can set the variable
3884 @code{MAKESHELL} to the shell that @code{make} should use; if set it
3885 will be used as the shell instead of the value of @code{SHELL}.
3887 @subsubheading Choosing a Shell in DOS and Windows
3888 @cindex shell, in DOS and Windows
3889 @cindex DOS, choosing a shell in
3890 @cindex Windows, choosing a shell in
3892 Choosing a shell in MS-DOS and MS-Windows is much more complex than on
3896 On MS-DOS, if @code{SHELL} is not set, the value of the variable
3897 @code{COMSPEC} (which is always set) is used instead.
3899 @cindex @code{SHELL}, MS-DOS specifics
3900 The processing of lines that set the variable @code{SHELL} in Makefiles
3901 is different on MS-DOS. The stock shell, @file{command.com}, is
3902 ridiculously limited in its functionality and many users of @code{make}
3903 tend to install a replacement shell. Therefore, on MS-DOS, @code{make}
3904 examines the value of @code{SHELL}, and changes its behavior based on
3905 whether it points to a Unix-style or DOS-style shell. This allows
3906 reasonable functionality even if @code{SHELL} points to
3909 If @code{SHELL} points to a Unix-style shell, @code{make} on MS-DOS
3910 additionally checks whether that shell can indeed be found; if not, it
3911 ignores the line that sets @code{SHELL}. In MS-DOS, GNU @code{make}
3912 searches for the shell in the following places:
3916 In the precise place pointed to by the value of @code{SHELL}. For
3917 example, if the makefile specifies @samp{SHELL = /bin/sh}, @code{make}
3918 will look in the directory @file{/bin} on the current drive.
3921 In the current directory.
3924 In each of the directories in the @code{PATH} variable, in order.
3928 In every directory it examines, @code{make} will first look for the
3929 specific file (@file{sh} in the example above). If this is not found,
3930 it will also look in that directory for that file with one of the known
3931 extensions which identify executable files. For example @file{.exe},
3932 @file{.com}, @file{.bat}, @file{.btm}, @file{.sh}, and some others.
3934 If any of these attempts is successful, the value of @code{SHELL} will
3935 be set to the full pathname of the shell as found. However, if none of
3936 these is found, the value of @code{SHELL} will not be changed, and thus
3937 the line that sets it will be effectively ignored. This is so
3938 @code{make} will only support features specific to a Unix-style shell if
3939 such a shell is actually installed on the system where @code{make} runs.
3941 Note that this extended search for the shell is limited to the cases
3942 where @code{SHELL} is set from the Makefile; if it is set in the
3943 environment or command line, you are expected to set it to the full
3944 pathname of the shell, exactly as things are on Unix.
3946 The effect of the above DOS-specific processing is that a Makefile that
3947 contains @samp{SHELL = /bin/sh} (as many Unix makefiles do), will work
3948 on MS-DOS unaltered if you have e.g.@: @file{sh.exe} installed in some
3949 directory along your @code{PATH}.
3951 @node Parallel, Errors, Execution, Commands
3952 @section Parallel Execution
3953 @cindex commands, execution in parallel
3954 @cindex parallel execution
3955 @cindex execution, in parallel
3958 @cindex @code{--jobs}
3960 GNU @code{make} knows how to execute several commands at once.
3961 Normally, @code{make} will execute only one command at a time, waiting
3962 for it to finish before executing the next. However, the @samp{-j} or
3963 @samp{--jobs} option tells @code{make} to execute many commands
3964 simultaneously.@refill
3966 On MS-DOS, the @samp{-j} option has no effect, since that system doesn't
3967 support multi-processing.
3969 If the @samp{-j} option is followed by an integer, this is the number of
3970 commands to execute at once; this is called the number of @dfn{job slots}.
3971 If there is nothing looking like an integer after the @samp{-j} option,
3972 there is no limit on the number of job slots. The default number of job
3973 slots is one, which means serial execution (one thing at a time).
3975 One unpleasant consequence of running several commands simultaneously is
3976 that output generated by the commands appears whenever each command
3977 sends it, so messages from different commands may be interspersed.
3979 Another problem is that two processes cannot both take input from the
3980 same device; so to make sure that only one command tries to take input
3981 from the terminal at once, @code{make} will invalidate the standard
3982 input streams of all but one running command. This means that
3983 attempting to read from standard input will usually be a fatal error (a
3984 @samp{Broken pipe} signal) for most child processes if there are
3987 @cindex standard input
3989 It is unpredictable which command will have a valid standard input stream
3990 (which will come from the terminal, or wherever you redirect the standard
3991 input of @code{make}). The first command run will always get it first, and
3992 the first command started after that one finishes will get it next, and so
3995 We will change how this aspect of @code{make} works if we find a better
3996 alternative. In the mean time, you should not rely on any command using
3997 standard input at all if you are using the parallel execution feature; but
3998 if you are not using this feature, then standard input works normally in
4001 Finally, handling recursive @code{make} invocations raises issues. For
4002 more information on this, see
4003 @ref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
4005 If a command fails (is killed by a signal or exits with a nonzero
4006 status), and errors are not ignored for that command
4007 (@pxref{Errors, ,Errors in Commands}),
4008 the remaining command lines to remake the same target will not be run.
4009 If a command fails and the @samp{-k} or @samp{--keep-going}
4010 option was not given
4011 (@pxref{Options Summary, ,Summary of Options}),
4012 @code{make} aborts execution. If make
4013 terminates for any reason (including a signal) with child processes
4014 running, it waits for them to finish before actually exiting.@refill
4016 @cindex load average
4017 @cindex limiting jobs based on load
4018 @cindex jobs, limiting based on load
4019 @cindex @code{-l} (load average)
4020 @cindex @code{--max-load}
4021 @cindex @code{--load-average}
4022 When the system is heavily loaded, you will probably want to run fewer jobs
4023 than when it is lightly loaded. You can use the @samp{-l} option to tell
4024 @code{make} to limit the number of jobs to run at once, based on the load
4025 average. The @samp{-l} or @samp{--max-load}
4026 option is followed by a floating-point number. For
4034 will not let @code{make} start more than one job if the load average is
4035 above 2.5. The @samp{-l} option with no following number removes the
4036 load limit, if one was given with a previous @samp{-l} option.@refill
4038 More precisely, when @code{make} goes to start up a job, and it already has
4039 at least one job running, it checks the current load average; if it is not
4040 lower than the limit given with @samp{-l}, @code{make} waits until the load
4041 average goes below that limit, or until all the other jobs finish.
4043 By default, there is no load limit.
4045 @node Errors, Interrupts, Parallel, Commands
4046 @section Errors in Commands
4047 @cindex errors (in commands)
4048 @cindex commands, errors in
4049 @cindex exit status (errors)
4051 After each shell command returns, @code{make} looks at its exit status.
4052 If the command completed successfully, the next command line is executed
4053 in a new shell; after the last command line is finished, the rule is
4056 If there is an error (the exit status is nonzero), @code{make} gives up on
4057 the current rule, and perhaps on all rules.
4059 Sometimes the failure of a certain command does not indicate a problem.
4060 For example, you may use the @code{mkdir} command to ensure that a
4061 directory exists. If the directory already exists, @code{mkdir} will
4062 report an error, but you probably want @code{make} to continue regardless.
4064 @cindex @code{-} (in commands)
4065 To ignore errors in a command line, write a @samp{-} at the beginning of
4066 the line's text (after the initial tab). The @samp{-} is discarded before
4067 the command is passed to the shell for execution.
4077 @cindex @code{rm} (shell command)
4080 This causes @code{rm} to continue even if it is unable to remove a file.
4083 @cindex @code{--ignore-errors}
4085 When you run @code{make} with the @samp{-i} or @samp{--ignore-errors}
4086 flag, errors are ignored in all commands of all rules. A rule in the
4087 makefile for the special target @code{.IGNORE} has the same effect, if
4088 there are no prerequisites. These ways of ignoring errors are obsolete
4089 because @samp{-} is more flexible.
4091 When errors are to be ignored, because of either a @samp{-} or the
4092 @samp{-i} flag, @code{make} treats an error return just like success,
4093 except that it prints out a message that tells you the status code
4094 the command exited with, and says that the error has been ignored.
4096 When an error happens that @code{make} has not been told to ignore,
4097 it implies that the current target cannot be correctly remade, and neither
4098 can any other that depends on it either directly or indirectly. No further
4099 commands will be executed for these targets, since their preconditions
4100 have not been achieved.
4104 @cindex @code{--keep-going}
4105 Normally @code{make} gives up immediately in this circumstance, returning a
4106 nonzero status. However, if the @samp{-k} or @samp{--keep-going}
4107 flag is specified, @code{make}
4108 continues to consider the other prerequisites of the pending targets,
4109 remaking them if necessary, before it gives up and returns nonzero status.
4110 For example, after an error in compiling one object file, @samp{make -k}
4111 will continue compiling other object files even though it already knows
4112 that linking them will be impossible. @xref{Options Summary, ,Summary of Options}.
4114 The usual behavior assumes that your purpose is to get the specified
4115 targets up to date; once @code{make} learns that this is impossible, it
4116 might as well report the failure immediately. The @samp{-k} option says
4117 that the real purpose is to test as many of the changes made in the
4118 program as possible, perhaps to find several independent problems so
4119 that you can correct them all before the next attempt to compile. This
4120 is why Emacs' @code{compile} command passes the @samp{-k} flag by
4122 @cindex Emacs (@code{M-x compile})
4124 @findex .DELETE_ON_ERROR
4125 @cindex deletion of target files
4126 @cindex removal of target files
4127 @cindex target, deleting on error
4128 Usually when a command fails, if it has changed the target file at all,
4129 the file is corrupted and cannot be used---or at least it is not
4130 completely updated. Yet the file's time stamp says that it is now up to
4131 date, so the next time @code{make} runs, it will not try to update that
4132 file. The situation is just the same as when the command is killed by a
4133 signal; @pxref{Interrupts}. So generally the right thing to do is to
4134 delete the target file if the command fails after beginning to change
4135 the file. @code{make} will do this if @code{.DELETE_ON_ERROR} appears
4136 as a target. This is almost always what you want @code{make} to do, but
4137 it is not historical practice; so for compatibility, you must explicitly
4140 @node Interrupts, Recursion, Errors, Commands
4141 @section Interrupting or Killing @code{make}
4144 @cindex deletion of target files
4145 @cindex removal of target files
4146 @cindex target, deleting on interrupt
4147 @cindex killing (interruption)
4149 If @code{make} gets a fatal signal while a command is executing, it may
4150 delete the target file that the command was supposed to update. This is
4151 done if the target file's last-modification time has changed since
4152 @code{make} first checked it.
4154 The purpose of deleting the target is to make sure that it is remade from
4155 scratch when @code{make} is next run. Why is this? Suppose you type
4156 @kbd{Ctrl-c} while a compiler is running, and it has begun to write an
4157 object file @file{foo.o}. The @kbd{Ctrl-c} kills the compiler, resulting
4158 in an incomplete file whose last-modification time is newer than the source
4159 file @file{foo.c}. But @code{make} also receives the @kbd{Ctrl-c} signal
4160 and deletes this incomplete file. If @code{make} did not do this, the next
4161 invocation of @code{make} would think that @file{foo.o} did not require
4162 updating---resulting in a strange error message from the linker when it
4163 tries to link an object file half of which is missing.
4166 You can prevent the deletion of a target file in this way by making the
4167 special target @code{.PRECIOUS} depend on it. Before remaking a target,
4168 @code{make} checks to see whether it appears on the prerequisites of
4169 @code{.PRECIOUS}, and thereby decides whether the target should be deleted
4170 if a signal happens. Some reasons why you might do this are that the
4171 target is updated in some atomic fashion, or exists only to record a
4172 modification-time (its contents do not matter), or must exist at all
4173 times to prevent other sorts of trouble.
4175 @node Recursion, Sequences, Interrupts, Commands
4176 @section Recursive Use of @code{make}
4178 @cindex subdirectories, recursion for
4180 Recursive use of @code{make} means using @code{make} as a command in a
4181 makefile. This technique is useful when you want separate makefiles for
4182 various subsystems that compose a larger system. For example, suppose you
4183 have a subdirectory @file{subdir} which has its own makefile, and you would
4184 like the containing directory's makefile to run @code{make} on the
4185 subdirectory. You can do it by writing this:
4189 cd subdir && $(MAKE)
4193 or, equivalently, this (@pxref{Options Summary, ,Summary of Options}):
4200 @cindex @code{--directory}
4202 You can write recursive @code{make} commands just by copying this example,
4203 but there are many things to know about how they work and why, and about
4204 how the sub-@code{make} relates to the top-level @code{make}. You may
4205 also find it useful to declare targets that invoke recursive
4206 @code{make} commands as @samp{.PHONY} (for more discussion on when
4207 this is useful, see @ref{Phony Targets}).
4209 @vindex @code{CURDIR}
4210 For your convenience, when GNU @code{make} starts (after it has
4211 processed any @code{-C} options) it sets the variable @code{CURDIR} to
4212 the pathname of the current working directory. This value is never
4213 touched by @code{make} again: in particular note that if you include
4214 files from other directories the value of @code{CURDIR} does not
4215 change. The value has the same precedence it would have if it were
4216 set in the makefile (by default, an environment variable @code{CURDIR}
4217 will not override this value). Note that setting this variable has no
4218 impact on the operation of @code{make} (it does not cause @code{make}
4219 to change its working directory, for example).
4222 * MAKE Variable:: The special effects of using @samp{$(MAKE)}.
4223 * Variables/Recursion:: How to communicate variables to a sub-@code{make}.
4224 * Options/Recursion:: How to communicate options to a sub-@code{make}.
4225 * -w Option:: How the @samp{-w} or @samp{--print-directory} option
4226 helps debug use of recursive @code{make} commands.
4229 @node MAKE Variable, Variables/Recursion, Recursion, Recursion
4230 @subsection How the @code{MAKE} Variable Works
4232 @cindex recursion, and @code{MAKE} variable
4234 Recursive @code{make} commands should always use the variable @code{MAKE},
4235 not the explicit command name @samp{make}, as shown here:
4240 cd subdir && $(MAKE)
4244 The value of this variable is the file name with which @code{make} was
4245 invoked. If this file name was @file{/bin/make}, then the command executed
4246 is @samp{cd subdir && /bin/make}. If you use a special version of
4247 @code{make} to run the top-level makefile, the same special version will be
4248 executed for recursive invocations.
4249 @cindex @code{cd} (shell command)
4251 @cindex +, and commands
4252 As a special feature, using the variable @code{MAKE} in the commands of
4253 a rule alters the effects of the @samp{-t} (@samp{--touch}), @samp{-n}
4254 (@samp{--just-print}), or @samp{-q} (@w{@samp{--question}}) option.
4255 Using the @code{MAKE} variable has the same effect as using a @samp{+}
4256 character at the beginning of the command line. @xref{Instead of
4257 Execution, ,Instead of Executing the Commands}. This special feature
4258 is only enabled if the @code{MAKE} variable appears directly in the
4259 command script: it does not apply if the @code{MAKE} variable is
4260 referenced through expansion of another variable. In the latter case
4261 you must use the @samp{+} token to get these special effects.@refill
4263 Consider the command @samp{make -t} in the above example. (The
4264 @samp{-t} option marks targets as up to date without actually running
4265 any commands; see @ref{Instead of Execution}.) Following the usual
4266 definition of @samp{-t}, a @samp{make -t} command in the example would
4267 create a file named @file{subsystem} and do nothing else. What you
4268 really want it to do is run @samp{@w{cd subdir &&} @w{make -t}}; but that would
4269 require executing the command, and @samp{-t} says not to execute
4271 @cindex @code{-t}, and recursion
4272 @cindex recursion, and @code{-t}
4273 @cindex @code{--touch}, and recursion
4275 The special feature makes this do what you want: whenever a command
4276 line of a rule contains the variable @code{MAKE}, the flags @samp{-t},
4277 @samp{-n} and @samp{-q} do not apply to that line. Command lines
4278 containing @code{MAKE} are executed normally despite the presence of a
4279 flag that causes most commands not to be run. The usual
4280 @code{MAKEFLAGS} mechanism passes the flags to the sub-@code{make}
4281 (@pxref{Options/Recursion, ,Communicating Options to a
4282 Sub-@code{make}}), so your request to touch the files, or print the
4283 commands, is propagated to the subsystem.@refill
4285 @node Variables/Recursion, Options/Recursion, MAKE Variable, Recursion
4286 @subsection Communicating Variables to a Sub-@code{make}
4287 @cindex sub-@code{make}
4288 @cindex environment, and recursion
4289 @cindex exporting variables
4290 @cindex variables, environment
4291 @cindex variables, exporting
4292 @cindex recursion, and environment
4293 @cindex recursion, and variables
4295 Variable values of the top-level @code{make} can be passed to the
4296 sub-@code{make} through the environment by explicit request. These
4297 variables are defined in the sub-@code{make} as defaults, but do not
4298 override what is specified in the makefile used by the sub-@code{make}
4299 makefile unless you use the @samp{-e} switch (@pxref{Options Summary,
4300 ,Summary of Options}).@refill
4302 To pass down, or @dfn{export}, a variable, @code{make} adds the variable
4303 and its value to the environment for running each command. The
4304 sub-@code{make}, in turn, uses the environment to initialize its table
4305 of variable values. @xref{Environment, ,Variables from the
4308 Except by explicit request, @code{make} exports a variable only if it
4309 is either defined in the environment initially or set on the command
4310 line, and if its name consists only of letters, numbers, and underscores.
4311 Some shells cannot cope with environment variable names consisting of
4312 characters other than letters, numbers, and underscores.
4314 @cindex SHELL, exported value
4315 The value of the @code{make} variable @code{SHELL} is not exported.
4316 Instead, the value of the @code{SHELL} variable from the invoking
4317 environment is passed to the sub-@code{make}. You can force
4318 @code{make} to export its value for @code{SHELL} by using the
4319 @code{export} directive, described below. @xref{Choosing the Shell}.
4321 The special variable @code{MAKEFLAGS} is always exported (unless you
4322 unexport it). @code{MAKEFILES} is exported if you set it to anything.
4324 @code{make} automatically passes down variable values that were defined
4325 on the command line, by putting them in the @code{MAKEFLAGS} variable.
4327 See the next section.
4330 @xref{Options/Recursion}.
4333 Variables are @emph{not} normally passed down if they were created by
4334 default by @code{make} (@pxref{Implicit Variables, ,Variables Used by
4335 Implicit Rules}). The sub-@code{make} will define these for
4339 If you want to export specific variables to a sub-@code{make}, use the
4340 @code{export} directive, like this:
4343 export @var{variable} @dots{}
4348 If you want to @emph{prevent} a variable from being exported, use the
4349 @code{unexport} directive, like this:
4352 unexport @var{variable} @dots{}
4356 In both of these forms, the arguments to @code{export} and
4357 @code{unexport} are expanded, and so could be variables or functions
4358 which expand to a (list of) variable names to be (un)exported.
4360 As a convenience, you can define a variable and export it at the same
4364 export @var{variable} = value
4368 has the same result as:
4371 @var{variable} = value
4372 export @var{variable}
4379 export @var{variable} := value
4383 has the same result as:
4386 @var{variable} := value
4387 export @var{variable}
4393 export @var{variable} += value
4400 @var{variable} += value
4401 export @var{variable}
4405 @xref{Appending, ,Appending More Text to Variables}.
4407 You may notice that the @code{export} and @code{unexport} directives
4408 work in @code{make} in the same way they work in the shell, @code{sh}.
4410 If you want all variables to be exported by default, you can use
4411 @code{export} by itself:
4418 This tells @code{make} that variables which are not explicitly mentioned
4419 in an @code{export} or @code{unexport} directive should be exported.
4420 Any variable given in an @code{unexport} directive will still @emph{not}
4421 be exported. If you use @code{export} by itself to export variables by
4422 default, variables whose names contain characters other than
4423 alphanumerics and underscores will not be exported unless specifically
4424 mentioned in an @code{export} directive.@refill
4426 @findex .EXPORT_ALL_VARIABLES
4427 The behavior elicited by an @code{export} directive by itself was the
4428 default in older versions of GNU @code{make}. If your makefiles depend
4429 on this behavior and you want to be compatible with old versions of
4430 @code{make}, you can write a rule for the special target
4431 @code{.EXPORT_ALL_VARIABLES} instead of using the @code{export} directive.
4432 This will be ignored by old @code{make}s, while the @code{export}
4433 directive will cause a syntax error.@refill
4434 @cindex compatibility in exporting
4436 Likewise, you can use @code{unexport} by itself to tell @code{make}
4437 @emph{not} to export variables by default. Since this is the default
4438 behavior, you would only need to do this if @code{export} had been used
4439 by itself earlier (in an included makefile, perhaps). You
4440 @strong{cannot} use @code{export} and @code{unexport} by themselves to
4441 have variables exported for some commands and not for others. The last
4442 @code{export} or @code{unexport} directive that appears by itself
4443 determines the behavior for the entire run of @code{make}.@refill
4446 @cindex recursion, level of
4447 As a special feature, the variable @code{MAKELEVEL} is changed when it
4448 is passed down from level to level. This variable's value is a string
4449 which is the depth of the level as a decimal number. The value is
4450 @samp{0} for the top-level @code{make}; @samp{1} for a sub-@code{make},
4451 @samp{2} for a sub-sub-@code{make}, and so on. The incrementation
4452 happens when @code{make} sets up the environment for a command.@refill
4454 The main use of @code{MAKELEVEL} is to test it in a conditional
4455 directive (@pxref{Conditionals, ,Conditional Parts of Makefiles}); this
4456 way you can write a makefile that behaves one way if run recursively and
4457 another way if run directly by you.@refill
4460 You can use the variable @code{MAKEFILES} to cause all sub-@code{make}
4461 commands to use additional makefiles. The value of @code{MAKEFILES} is
4462 a whitespace-separated list of file names. This variable, if defined in
4463 the outer-level makefile, is passed down through the environment; then
4464 it serves as a list of extra makefiles for the sub-@code{make} to read
4465 before the usual or specified ones. @xref{MAKEFILES Variable, ,The
4466 Variable @code{MAKEFILES}}.@refill
4468 @node Options/Recursion, -w Option, Variables/Recursion, Recursion
4469 @subsection Communicating Options to a Sub-@code{make}
4470 @cindex options, and recursion
4471 @cindex recursion, and options
4474 Flags such as @samp{-s} and @samp{-k} are passed automatically to the
4475 sub-@code{make} through the variable @code{MAKEFLAGS}. This variable is
4476 set up automatically by @code{make} to contain the flag letters that
4477 @code{make} received. Thus, if you do @w{@samp{make -ks}} then
4478 @code{MAKEFLAGS} gets the value @samp{ks}.@refill
4480 As a consequence, every sub-@code{make} gets a value for @code{MAKEFLAGS}
4481 in its environment. In response, it takes the flags from that value and
4482 processes them as if they had been given as arguments.
4483 @xref{Options Summary, ,Summary of Options}.
4485 @cindex command line variable definitions, and recursion
4486 @cindex variables, command line, and recursion
4487 @cindex recursion, and command line variable definitions
4488 Likewise variables defined on the command line are passed to the
4489 sub-@code{make} through @code{MAKEFLAGS}. Words in the value of
4490 @code{MAKEFLAGS} that contain @samp{=}, @code{make} treats as variable
4491 definitions just as if they appeared on the command line.
4492 @xref{Overriding, ,Overriding Variables}.
4494 @cindex @code{-C}, and recursion
4495 @cindex @code{-f}, and recursion
4496 @cindex @code{-o}, and recursion
4497 @cindex @code{-W}, and recursion
4498 @cindex @code{--directory}, and recursion
4499 @cindex @code{--file}, and recursion
4500 @cindex @code{--old-file}, and recursion
4501 @cindex @code{--assume-old}, and recursion
4502 @cindex @code{--assume-new}, and recursion
4503 @cindex @code{--new-file}, and recursion
4504 @cindex recursion, and @code{-C}
4505 @cindex recursion, and @code{-f}
4506 @cindex recursion, and @code{-o}
4507 @cindex recursion, and @code{-W}
4508 The options @samp{-C}, @samp{-f}, @samp{-o}, and @samp{-W} are not put
4509 into @code{MAKEFLAGS}; these options are not passed down.@refill
4511 @cindex @code{-j}, and recursion
4512 @cindex @code{--jobs}, and recursion
4513 @cindex recursion, and @code{-j}
4514 @cindex job slots, and recursion
4515 The @samp{-j} option is a special case (@pxref{Parallel, ,Parallel Execution}).
4516 If you set it to some numeric value @samp{N} and your operating system
4517 supports it (most any UNIX system will; others typically won't), the
4518 parent @code{make} and all the sub-@code{make}s will communicate to
4519 ensure that there are only @samp{N} jobs running at the same time
4520 between them all. Note that any job that is marked recursive
4521 (@pxref{Instead of Execution, ,Instead of Executing the Commands})
4522 doesn't count against the total jobs (otherwise we could get @samp{N}
4523 sub-@code{make}s running and have no slots left over for any real work!)
4525 If your operating system doesn't support the above communication, then
4526 @samp{-j 1} is always put into @code{MAKEFLAGS} instead of the value you
4527 specified. This is because if the @w{@samp{-j}} option were passed down
4528 to sub-@code{make}s, you would get many more jobs running in parallel
4529 than you asked for. If you give @samp{-j} with no numeric argument,
4530 meaning to run as many jobs as possible in parallel, this is passed
4531 down, since multiple infinities are no more than one.@refill
4533 If you do not want to pass the other flags down, you must change the
4534 value of @code{MAKEFLAGS}, like this:
4538 cd subdir && $(MAKE) MAKEFLAGS=
4541 @vindex MAKEOVERRIDES
4542 The command line variable definitions really appear in the variable
4543 @code{MAKEOVERRIDES}, and @code{MAKEFLAGS} contains a reference to this
4544 variable. If you do want to pass flags down normally, but don't want to
4545 pass down the command line variable definitions, you can reset
4546 @code{MAKEOVERRIDES} to empty, like this:
4553 @cindex Arg list too long
4555 This is not usually useful to do. However, some systems have a small
4556 fixed limit on the size of the environment, and putting so much
4557 information into the value of @code{MAKEFLAGS} can exceed it. If you
4558 see the error message @samp{Arg list too long}, this may be the problem.
4561 (For strict compliance with POSIX.2, changing @code{MAKEOVERRIDES} does
4562 not affect @code{MAKEFLAGS} if the special target @samp{.POSIX} appears
4563 in the makefile. You probably do not care about this.)
4566 A similar variable @code{MFLAGS} exists also, for historical
4567 compatibility. It has the same value as @code{MAKEFLAGS} except that it
4568 does not contain the command line variable definitions, and it always
4569 begins with a hyphen unless it is empty (@code{MAKEFLAGS} begins with a
4570 hyphen only when it begins with an option that has no single-letter
4571 version, such as @samp{--warn-undefined-variables}). @code{MFLAGS} was
4572 traditionally used explicitly in the recursive @code{make} command, like
4577 cd subdir && $(MAKE) $(MFLAGS)
4581 but now @code{MAKEFLAGS} makes this usage redundant. If you want your
4582 makefiles to be compatible with old @code{make} programs, use this
4583 technique; it will work fine with more modern @code{make} versions too.
4585 @cindex setting options from environment
4586 @cindex options, setting from environment
4587 @cindex setting options in makefiles
4588 @cindex options, setting in makefiles
4589 The @code{MAKEFLAGS} variable can also be useful if you want to have
4590 certain options, such as @samp{-k} (@pxref{Options Summary, ,Summary of
4591 Options}), set each time you run @code{make}. You simply put a value for
4592 @code{MAKEFLAGS} in your environment. You can also set @code{MAKEFLAGS} in
4593 a makefile, to specify additional flags that should also be in effect for
4594 that makefile. (Note that you cannot use @code{MFLAGS} this way. That
4595 variable is set only for compatibility; @code{make} does not interpret a
4596 value you set for it in any way.)
4598 When @code{make} interprets the value of @code{MAKEFLAGS} (either from the
4599 environment or from a makefile), it first prepends a hyphen if the value
4600 does not already begin with one. Then it chops the value into words
4601 separated by blanks, and parses these words as if they were options given
4602 on the command line (except that @samp{-C}, @samp{-f}, @samp{-h},
4603 @samp{-o}, @samp{-W}, and their long-named versions are ignored; and there
4604 is no error for an invalid option).
4606 If you do put @code{MAKEFLAGS} in your environment, you should be sure not
4607 to include any options that will drastically affect the actions of
4608 @code{make} and undermine the purpose of makefiles and of @code{make}
4609 itself. For instance, the @samp{-t}, @samp{-n}, and @samp{-q} options, if
4610 put in one of these variables, could have disastrous consequences and would
4611 certainly have at least surprising and probably annoying effects.@refill
4613 @node -w Option, , Options/Recursion, Recursion
4614 @subsection The @samp{--print-directory} Option
4615 @cindex directories, printing them
4616 @cindex printing directories
4617 @cindex recursion, and printing directories
4619 If you use several levels of recursive @code{make} invocations, the
4620 @samp{-w} or @w{@samp{--print-directory}} option can make the output a
4621 lot easier to understand by showing each directory as @code{make}
4622 starts processing it and as @code{make} finishes processing it. For
4623 example, if @samp{make -w} is run in the directory @file{/u/gnu/make},
4624 @code{make} will print a line of the form:@refill
4627 make: Entering directory `/u/gnu/make'.
4631 before doing anything else, and a line of the form:
4634 make: Leaving directory `/u/gnu/make'.
4638 when processing is completed.
4640 @cindex @code{-C}, and @code{-w}
4641 @cindex @code{--directory}, and @code{--print-directory}
4642 @cindex recursion, and @code{-w}
4643 @cindex @code{-w}, and @code{-C}
4644 @cindex @code{-w}, and recursion
4645 @cindex @code{--print-directory}, and @code{--directory}
4646 @cindex @code{--print-directory}, and recursion
4647 @cindex @code{--no-print-directory}
4648 @cindex @code{--print-directory}, disabling
4649 @cindex @code{-w}, disabling
4650 Normally, you do not need to specify this option because @samp{make}
4651 does it for you: @samp{-w} is turned on automatically when you use the
4652 @samp{-C} option, and in sub-@code{make}s. @code{make} will not
4653 automatically turn on @samp{-w} if you also use @samp{-s}, which says to
4654 be silent, or if you use @samp{--no-print-directory} to explicitly
4657 @node Sequences, Empty Commands, Recursion, Commands
4658 @section Defining Canned Command Sequences
4659 @cindex sequences of commands
4660 @cindex commands, sequences of
4662 When the same sequence of commands is useful in making various targets, you
4663 can define it as a canned sequence with the @code{define} directive, and
4664 refer to the canned sequence from the rules for those targets. The canned
4665 sequence is actually a variable, so the name must not conflict with other
4668 Here is an example of defining a canned sequence of commands:
4672 yacc $(firstword $^)
4679 Here @code{run-yacc} is the name of the variable being defined;
4680 @code{endef} marks the end of the definition; the lines in between are the
4681 commands. The @code{define} directive does not expand variable references
4682 and function calls in the canned sequence; the @samp{$} characters,
4683 parentheses, variable names, and so on, all become part of the value of the
4684 variable you are defining.
4685 @xref{Defining, ,Defining Variables Verbatim},
4686 for a complete explanation of @code{define}.
4688 The first command in this example runs Yacc on the first prerequisite of
4689 whichever rule uses the canned sequence. The output file from Yacc is
4690 always named @file{y.tab.c}. The second command moves the output to the
4691 rule's target file name.
4693 To use the canned sequence, substitute the variable into the commands of a
4694 rule. You can substitute it like any other variable
4695 (@pxref{Reference, ,Basics of Variable References}).
4696 Because variables defined by @code{define} are recursively expanded
4697 variables, all the variable references you wrote inside the @code{define}
4698 are expanded now. For example:
4706 @samp{foo.y} will be substituted for the variable @samp{$^} when it occurs in
4707 @code{run-yacc}'s value, and @samp{foo.c} for @samp{$@@}.@refill
4709 This is a realistic example, but this particular one is not needed in
4710 practice because @code{make} has an implicit rule to figure out these
4711 commands based on the file names involved
4712 (@pxref{Implicit Rules, ,Using Implicit Rules}).
4714 @cindex @@, and @code{define}
4715 @cindex -, and @code{define}
4716 @cindex +, and @code{define}
4717 In command execution, each line of a canned sequence is treated just as
4718 if the line appeared on its own in the rule, preceded by a tab. In
4719 particular, @code{make} invokes a separate subshell for each line. You
4720 can use the special prefix characters that affect command lines
4721 (@samp{@@}, @samp{-}, and @samp{+}) on each line of a canned sequence.
4722 @xref{Commands, ,Writing the Commands in Rules}.
4723 For example, using this canned sequence:
4727 @@echo "frobnicating target $@@"
4728 frob-step-1 $< -o $@@-step-1
4729 frob-step-2 $@@-step-1 -o $@@
4734 @code{make} will not echo the first line, the @code{echo} command.
4735 But it @emph{will} echo the following two command lines.
4737 On the other hand, prefix characters on the command line that refers to
4738 a canned sequence apply to every line in the sequence. So the rule:
4746 does not echo @emph{any} commands.
4747 (@xref{Echoing, ,Command Echoing}, for a full explanation of @samp{@@}.)
4749 @node Empty Commands, , Sequences, Commands
4750 @section Using Empty Commands
4751 @cindex empty commands
4752 @cindex commands, empty
4754 It is sometimes useful to define commands which do nothing. This is done
4755 simply by giving a command that consists of nothing but whitespace. For
4763 defines an empty command string for @file{target}. You could also use a
4764 line beginning with a tab character to define an empty command string,
4765 but this would be confusing because such a line looks empty.
4767 @findex .DEFAULT@r{, and empty commands}
4768 You may be wondering why you would want to define a command string that
4769 does nothing. The only reason this is useful is to prevent a target
4770 from getting implicit commands (from implicit rules or the
4771 @code{.DEFAULT} special target; @pxref{Implicit Rules} and
4772 @pxref{Last Resort, ,Defining Last-Resort Default Rules}).@refill
4774 @c !!! another reason is for canonical stamp files:
4779 create foo frm foo.in
4784 You may be inclined to define empty command strings for targets that are
4785 not actual files, but only exist so that their prerequisites can be
4786 remade. However, this is not the best way to do that, because the
4787 prerequisites may not be remade properly if the target file actually does exist.
4788 @xref{Phony Targets, ,Phony Targets}, for a better way to do this.
4790 @node Using Variables, Conditionals, Commands, Top
4791 @chapter How to Use Variables
4794 @cindex recursive variable expansion
4795 @cindex simple variable expansion
4797 A @dfn{variable} is a name defined in a makefile to represent a string
4798 of text, called the variable's @dfn{value}. These values are
4799 substituted by explicit request into targets, prerequisites, commands,
4800 and other parts of the makefile. (In some other versions of @code{make},
4801 variables are called @dfn{macros}.)
4804 Variables and functions in all parts of a makefile are expanded when
4805 read, except for the shell commands in rules, the right-hand sides of
4806 variable definitions using @samp{=}, and the bodies of variable
4807 definitions using the @code{define} directive.@refill
4809 Variables can represent lists of file names, options to pass to compilers,
4810 programs to run, directories to look in for source files, directories to
4811 write output in, or anything else you can imagine.
4813 A variable name may be any sequence of characters not containing @samp{:},
4814 @samp{#}, @samp{=}, or leading or trailing whitespace. However,
4815 variable names containing characters other than letters, numbers, and
4816 underscores should be avoided, as they may be given special meanings in the
4817 future, and with some shells they cannot be passed through the environment to a
4819 (@pxref{Variables/Recursion, ,Communicating Variables to a Sub-@code{make}}).
4821 Variable names are case-sensitive. The names @samp{foo}, @samp{FOO},
4822 and @samp{Foo} all refer to different variables.
4824 It is traditional to use upper case letters in variable names, but we
4825 recommend using lower case letters for variable names that serve internal
4826 purposes in the makefile, and reserving upper case for parameters that
4827 control implicit rules or for parameters that the user should override with
4828 command options (@pxref{Overriding, ,Overriding Variables}).
4830 A few variables have names that are a single punctuation character or
4831 just a few characters. These are the @dfn{automatic variables}, and
4832 they have particular specialized uses. @xref{Automatic Variables}.
4835 * Reference:: How to use the value of a variable.
4836 * Flavors:: Variables come in two flavors.
4837 * Advanced:: Advanced features for referencing a variable.
4838 * Values:: All the ways variables get their values.
4839 * Setting:: How to set a variable in the makefile.
4840 * Appending:: How to append more text to the old value
4842 * Override Directive:: How to set a variable in the makefile even if
4843 the user has set it with a command argument.
4844 * Defining:: An alternate way to set a variable
4845 to a verbatim string.
4846 * Environment:: Variable values can come from the environment.
4847 * Target-specific:: Variable values can be defined on a per-target
4849 * Pattern-specific:: Target-specific variable values can be applied
4850 to a group of targets that match a pattern.
4853 @node Reference, Flavors, Using Variables, Using Variables
4854 @section Basics of Variable References
4855 @cindex variables, how to reference
4856 @cindex reference to variables
4857 @cindex @code{$}, in variable reference
4858 @cindex dollar sign (@code{$}), in variable reference
4860 To substitute a variable's value, write a dollar sign followed by the name
4861 of the variable in parentheses or braces: either @samp{$(foo)} or
4862 @samp{$@{foo@}} is a valid reference to the variable @code{foo}. This
4863 special significance of @samp{$} is why you must write @samp{$$} to have
4864 the effect of a single dollar sign in a file name or command.
4866 Variable references can be used in any context: targets, prerequisites,
4867 commands, most directives, and new variable values. Here is an
4868 example of a common case, where a variable holds the names of all the
4869 object files in a program:
4873 objects = program.o foo.o utils.o
4874 program : $(objects)
4875 cc -o program $(objects)
4881 Variable references work by strict textual substitution. Thus, the rule
4886 prog.o : prog.$(foo)
4887 $(foo)$(foo) -$(foo) prog.$(foo)
4892 could be used to compile a C program @file{prog.c}. Since spaces before
4893 the variable value are ignored in variable assignments, the value of
4894 @code{foo} is precisely @samp{c}. (Don't actually write your makefiles
4897 A dollar sign followed by a character other than a dollar sign,
4898 open-parenthesis or open-brace treats that single character as the
4899 variable name. Thus, you could reference the variable @code{x} with
4900 @samp{$x}. However, this practice is strongly discouraged, except in
4901 the case of the automatic variables (@pxref{Automatic Variables}).
4903 @node Flavors, Advanced, Reference, Using Variables
4904 @section The Two Flavors of Variables
4905 @cindex flavors of variables
4906 @cindex recursive variable expansion
4907 @cindex variables, flavors
4908 @cindex recursively expanded variables
4909 @cindex variables, recursively expanded
4911 There are two ways that a variable in GNU @code{make} can have a value;
4912 we call them the two @dfn{flavors} of variables. The two flavors are
4913 distinguished in how they are defined and in what they do when expanded.
4916 The first flavor of variable is a @dfn{recursively expanded} variable.
4917 Variables of this sort are defined by lines using @samp{=}
4918 (@pxref{Setting, ,Setting Variables}) or by the @code{define} directive
4919 (@pxref{Defining, ,Defining Variables Verbatim}). The value you specify
4920 is installed verbatim; if it contains references to other variables,
4921 these references are expanded whenever this variable is substituted (in
4922 the course of expanding some other string). When this happens, it is
4923 called @dfn{recursive expansion}.@refill
4936 will echo @samp{Huh?}: @samp{$(foo)} expands to @samp{$(bar)} which
4937 expands to @samp{$(ugh)} which finally expands to @samp{Huh?}.@refill
4939 This flavor of variable is the only sort supported by other versions of
4940 @code{make}. It has its advantages and its disadvantages. An advantage
4941 (most would say) is that:
4944 CFLAGS = $(include_dirs) -O
4945 include_dirs = -Ifoo -Ibar
4949 will do what was intended: when @samp{CFLAGS} is expanded in a command,
4950 it will expand to @samp{-Ifoo -Ibar -O}. A major disadvantage is that you
4951 cannot append something on the end of a variable, as in
4954 CFLAGS = $(CFLAGS) -O
4958 because it will cause an infinite loop in the variable expansion.
4959 (Actually @code{make} detects the infinite loop and reports an error.)
4960 @cindex loops in variable expansion
4961 @cindex variables, loops in expansion
4963 Another disadvantage is that any functions
4964 (@pxref{Functions, ,Functions for Transforming Text})
4965 referenced in the definition will be executed every time the variable is
4966 expanded. This makes @code{make} run slower; worse, it causes the
4967 @code{wildcard} and @code{shell} functions to give unpredictable results
4968 because you cannot easily control when they are called, or even how many
4971 To avoid all the problems and inconveniences of recursively expanded
4972 variables, there is another flavor: simply expanded variables.
4974 @cindex simply expanded variables
4975 @cindex variables, simply expanded
4977 @dfn{Simply expanded variables} are defined by lines using @samp{:=}
4978 (@pxref{Setting, ,Setting Variables}).
4979 The value of a simply expanded variable is scanned
4980 once and for all, expanding any references to other variables and
4981 functions, when the variable is defined. The actual value of the simply
4982 expanded variable is the result of expanding the text that you write.
4983 It does not contain any references to other variables; it contains their
4984 values @emph{as of the time this variable was defined}. Therefore,
5000 When a simply expanded variable is referenced, its value is substituted
5003 Here is a somewhat more complicated example, illustrating the use of
5004 @samp{:=} in conjunction with the @code{shell} function.
5005 (@xref{Shell Function, , The @code{shell} Function}.) This example
5006 also shows use of the variable @code{MAKELEVEL}, which is changed
5007 when it is passed down from level to level.
5008 (@xref{Variables/Recursion, , Communicating Variables to a
5009 Sub-@code{make}}, for information about @code{MAKELEVEL}.)
5015 ifeq (0,$@{MAKELEVEL@})
5016 whoami := $(shell whoami)
5017 host-type := $(shell arch)
5018 MAKE := $@{MAKE@} host-type=$@{host-type@} whoami=$@{whoami@}
5024 An advantage of this use of @samp{:=} is that a typical
5025 `descend into a directory' command then looks like this:
5030 $@{MAKE@} -C $@@ all
5034 Simply expanded variables generally make complicated makefile programming
5035 more predictable because they work like variables in most programming
5036 languages. They allow you to redefine a variable using its own value (or
5037 its value processed in some way by one of the expansion functions) and to
5038 use the expansion functions much more efficiently
5039 (@pxref{Functions, ,Functions for Transforming Text}).
5041 @cindex spaces, in variable values
5042 @cindex whitespace, in variable values
5043 @cindex variables, spaces in values
5044 You can also use them to introduce controlled leading whitespace into
5045 variable values. Leading whitespace characters are discarded from your
5046 input before substitution of variable references and function calls;
5047 this means you can include leading spaces in a variable value by
5048 protecting them with variable references, like this:
5052 space := $(nullstring) # end of the line
5056 Here the value of the variable @code{space} is precisely one space. The
5057 comment @w{@samp{# end of the line}} is included here just for clarity.
5058 Since trailing space characters are @emph{not} stripped from variable
5059 values, just a space at the end of the line would have the same effect
5060 (but be rather hard to read). If you put whitespace at the end of a
5061 variable value, it is a good idea to put a comment like that at the end
5062 of the line to make your intent clear. Conversely, if you do @emph{not}
5063 want any whitespace characters at the end of your variable value, you
5064 must remember not to put a random comment on the end of the line after
5065 some whitespace, such as this:
5068 dir := /foo/bar # directory to put the frobs in
5072 Here the value of the variable @code{dir} is @w{@samp{/foo/bar }}
5073 (with four trailing spaces), which was probably not the intention.
5074 (Imagine something like @w{@samp{$(dir)/file}} with this definition!)
5076 @cindex conditional variable assignment
5077 @cindex variables, conditional assignment
5079 There is another assignment operator for variables, @samp{?=}. This
5080 is called a conditional variable assignment operator, because it only
5081 has an effect if the variable is not yet defined. This statement:
5088 is exactly equivalent to this
5089 (@pxref{Origin Function, ,The @code{origin} Function}):
5092 ifeq ($(origin FOO), undefined)
5097 Note that a variable set to an empty value is still defined, so
5098 @samp{?=} will not set that variable.
5100 @node Advanced, Values, Flavors, Using Variables
5101 @section Advanced Features for Reference to Variables
5102 @cindex reference to variables
5104 This section describes some advanced features you can use to reference
5105 variables in more flexible ways.
5108 * Substitution Refs:: Referencing a variable with
5109 substitutions on the value.
5110 * Computed Names:: Computing the name of the variable to refer to.
5113 @node Substitution Refs, Computed Names, Advanced, Advanced
5114 @subsection Substitution References
5115 @cindex modified variable reference
5116 @cindex substitution variable reference
5117 @cindex variables, modified reference
5118 @cindex variables, substitution reference
5120 @cindex variables, substituting suffix in
5121 @cindex suffix, substituting in variables
5122 A @dfn{substitution reference} substitutes the value of a variable with
5123 alterations that you specify. It has the form
5124 @samp{$(@var{var}:@var{a}=@var{b})} (or
5125 @samp{$@{@var{var}:@var{a}=@var{b}@}}) and its meaning is to take the value
5126 of the variable @var{var}, replace every @var{a} at the end of a word with
5127 @var{b} in that value, and substitute the resulting string.
5129 When we say ``at the end of a word'', we mean that @var{a} must appear
5130 either followed by whitespace or at the end of the value in order to be
5131 replaced; other occurrences of @var{a} in the value are unaltered. For
5140 sets @samp{bar} to @samp{a.c b.c c.c}. @xref{Setting, ,Setting Variables}.
5142 A substitution reference is actually an abbreviation for use of the
5143 @code{patsubst} expansion function (@pxref{Text Functions, ,Functions for String Substitution and Analysis}). We provide
5144 substitution references as well as @code{patsubst} for compatibility with
5145 other implementations of @code{make}.
5148 Another type of substitution reference lets you use the full power of
5149 the @code{patsubst} function. It has the same form
5150 @samp{$(@var{var}:@var{a}=@var{b})} described above, except that now
5151 @var{a} must contain a single @samp{%} character. This case is
5152 equivalent to @samp{$(patsubst @var{a},@var{b},$(@var{var}))}.
5153 @xref{Text Functions, ,Functions for String Substitution and Analysis},
5154 for a description of the @code{patsubst} function.@refill
5158 @exdent For example:
5161 bar := $(foo:%.o=%.c)
5166 sets @samp{bar} to @samp{a.c b.c c.c}.
5168 @node Computed Names, , Substitution Refs, Advanced
5169 @subsection Computed Variable Names
5170 @cindex nested variable reference
5171 @cindex computed variable name
5172 @cindex variables, computed names
5173 @cindex variables, nested references
5174 @cindex variables, @samp{$} in name
5175 @cindex @code{$}, in variable name
5176 @cindex dollar sign (@code{$}), in variable name
5178 Computed variable names are a complicated concept needed only for
5179 sophisticated makefile programming. For most purposes you need not
5180 consider them, except to know that making a variable with a dollar sign
5181 in its name might have strange results. However, if you are the type
5182 that wants to understand everything, or you are actually interested in
5183 what they do, read on.
5185 Variables may be referenced inside the name of a variable. This is
5186 called a @dfn{computed variable name} or a @dfn{nested variable
5187 reference}. For example,
5196 defines @code{a} as @samp{z}: the @samp{$(x)} inside @samp{$($(x))} expands
5197 to @samp{y}, so @samp{$($(x))} expands to @samp{$(y)} which in turn expands
5198 to @samp{z}. Here the name of the variable to reference is not stated
5199 explicitly; it is computed by expansion of @samp{$(x)}. The reference
5200 @samp{$(x)} here is nested within the outer variable reference.
5202 The previous example shows two levels of nesting, but any number of levels
5203 is possible. For example, here are three levels:
5213 Here the innermost @samp{$(x)} expands to @samp{y}, so @samp{$($(x))}
5214 expands to @samp{$(y)} which in turn expands to @samp{z}; now we have
5215 @samp{$(z)}, which becomes @samp{u}.
5217 References to recursively-expanded variables within a variable name are
5218 reexpanded in the usual fashion. For example:
5228 defines @code{a} as @samp{Hello}: @samp{$($(x))} becomes @samp{$($(y))}
5229 which becomes @samp{$(z)} which becomes @samp{Hello}.
5231 Nested variable references can also contain modified references and
5232 function invocations (@pxref{Functions, ,Functions for Transforming Text}),
5233 just like any other reference.
5234 For example, using the @code{subst} function
5235 (@pxref{Text Functions, ,Functions for String Substitution and Analysis}):
5241 y = $(subst 1,2,$(x))
5248 eventually defines @code{a} as @samp{Hello}. It is doubtful that anyone
5249 would ever want to write a nested reference as convoluted as this one, but
5250 it works: @samp{$($($(z)))} expands to @samp{$($(y))} which becomes
5251 @samp{$($(subst 1,2,$(x)))}. This gets the value @samp{variable1} from
5252 @code{x} and changes it by substitution to @samp{variable2}, so that the
5253 entire string becomes @samp{$(variable2)}, a simple variable reference
5254 whose value is @samp{Hello}.@refill
5256 A computed variable name need not consist entirely of a single variable
5257 reference. It can contain several variable references, as well as some
5258 invariant text. For example,
5267 a_files := filea fileb
5268 1_files := file1 file2
5272 ifeq "$(use_a)" "yes"
5280 ifeq "$(use_dirs)" "yes"
5286 dirs := $($(a1)_$(df))
5291 will give @code{dirs} the same value as @code{a_dirs}, @code{1_dirs},
5292 @code{a_files} or @code{1_files} depending on the settings of @code{use_a}
5293 and @code{use_dirs}.@refill
5295 Computed variable names can also be used in substitution references:
5299 a_objects := a.o b.o c.o
5300 1_objects := 1.o 2.o 3.o
5302 sources := $($(a1)_objects:.o=.c)
5307 defines @code{sources} as either @samp{a.c b.c c.c} or @samp{1.c 2.c 3.c},
5308 depending on the value of @code{a1}.
5310 The only restriction on this sort of use of nested variable references
5311 is that they cannot specify part of the name of a function to be called.
5312 This is because the test for a recognized function name is done before
5313 the expansion of nested references. For example,
5329 foo := $($(func) $(bar))
5334 attempts to give @samp{foo} the value of the variable @samp{sort a d b g
5335 q c} or @samp{strip a d b g q c}, rather than giving @samp{a d b g q c}
5336 as the argument to either the @code{sort} or the @code{strip} function.
5337 This restriction could be removed in the future if that change is shown
5340 You can also use computed variable names in the left-hand side of a
5341 variable assignment, or in a @code{define} directive, as in:
5345 $(dir)_sources := $(wildcard $(dir)/*.c)
5347 lpr $($(dir)_sources)
5352 This example defines the variables @samp{dir}, @samp{foo_sources}, and
5355 Note that @dfn{nested variable references} are quite different from
5356 @dfn{recursively expanded variables}
5357 (@pxref{Flavors, ,The Two Flavors of Variables}), though both are
5358 used together in complex ways when doing makefile programming.@refill
5360 @node Values, Setting, Advanced, Using Variables
5361 @section How Variables Get Their Values
5362 @cindex variables, how they get their values
5363 @cindex value, how a variable gets it
5365 Variables can get values in several different ways:
5369 You can specify an overriding value when you run @code{make}.
5370 @xref{Overriding, ,Overriding Variables}.
5373 You can specify a value in the makefile, either
5374 with an assignment (@pxref{Setting, ,Setting Variables}) or with a
5375 verbatim definition (@pxref{Defining, ,Defining Variables Verbatim}).@refill
5378 Variables in the environment become @code{make} variables.
5379 @xref{Environment, ,Variables from the Environment}.
5382 Several @dfn{automatic} variables are given new values for each rule.
5383 Each of these has a single conventional use.
5384 @xref{Automatic Variables}.
5387 Several variables have constant initial values.
5388 @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
5391 @node Setting, Appending, Values, Using Variables
5392 @section Setting Variables
5393 @cindex setting variables
5394 @cindex variables, setting
5399 To set a variable from the makefile, write a line starting with the
5400 variable name followed by @samp{=} or @samp{:=}. Whatever follows the
5401 @samp{=} or @samp{:=} on the line becomes the value. For example,
5404 objects = main.o foo.o bar.o utils.o
5408 defines a variable named @code{objects}. Whitespace around the variable
5409 name and immediately after the @samp{=} is ignored.
5411 Variables defined with @samp{=} are @dfn{recursively expanded} variables.
5412 Variables defined with @samp{:=} are @dfn{simply expanded} variables; these
5413 definitions can contain variable references which will be expanded before
5414 the definition is made. @xref{Flavors, ,The Two Flavors of Variables}.
5416 The variable name may contain function and variable references, which
5417 are expanded when the line is read to find the actual variable name to use.
5419 There is no limit on the length of the value of a variable except the
5420 amount of swapping space on the computer. When a variable definition is
5421 long, it is a good idea to break it into several lines by inserting
5422 backslash-newline at convenient places in the definition. This will not
5423 affect the functioning of @code{make}, but it will make the makefile easier
5426 Most variable names are considered to have the empty string as a value if
5427 you have never set them. Several variables have built-in initial values
5428 that are not empty, but you can set them in the usual ways
5429 (@pxref{Implicit Variables, ,Variables Used by Implicit Rules}).
5430 Several special variables are set
5431 automatically to a new value for each rule; these are called the
5432 @dfn{automatic} variables (@pxref{Automatic Variables}).
5434 If you'd like a variable to be set to a value only if it's not already
5435 set, then you can use the shorthand operator @samp{?=} instead of
5436 @samp{=}. These two settings of the variable @samp{FOO} are identical
5437 (@pxref{Origin Function, ,The @code{origin} Function}):
5447 ifeq ($(origin FOO), undefined)
5452 @node Appending, Override Directive, Setting, Using Variables
5453 @section Appending More Text to Variables
5455 @cindex appending to variables
5456 @cindex variables, appending to
5458 Often it is useful to add more text to the value of a variable already defined.
5459 You do this with a line containing @samp{+=}, like this:
5462 objects += another.o
5466 This takes the value of the variable @code{objects}, and adds the text
5467 @samp{another.o} to it (preceded by a single space). Thus:
5470 objects = main.o foo.o bar.o utils.o
5471 objects += another.o
5475 sets @code{objects} to @samp{main.o foo.o bar.o utils.o another.o}.
5477 Using @samp{+=} is similar to:
5480 objects = main.o foo.o bar.o utils.o
5481 objects := $(objects) another.o
5485 but differs in ways that become important when you use more complex values.
5487 When the variable in question has not been defined before, @samp{+=}
5488 acts just like normal @samp{=}: it defines a recursively-expanded
5489 variable. However, when there @emph{is} a previous definition, exactly
5490 what @samp{+=} does depends on what flavor of variable you defined
5491 originally. @xref{Flavors, ,The Two Flavors of Variables}, for an
5492 explanation of the two flavors of variables.
5494 When you add to a variable's value with @samp{+=}, @code{make} acts
5495 essentially as if you had included the extra text in the initial
5496 definition of the variable. If you defined it first with @samp{:=},
5497 making it a simply-expanded variable, @samp{+=} adds to that
5498 simply-expanded definition, and expands the new text before appending it
5499 to the old value just as @samp{:=} does
5500 (see @ref{Setting, ,Setting Variables}, for a full explanation of @samp{:=}).
5509 is exactly equivalent to:
5514 variable := $(variable) more
5517 On the other hand, when you use @samp{+=} with a variable that you defined
5518 first to be recursively-expanded using plain @samp{=}, @code{make} does
5519 something a bit different. Recall that when you define a
5520 recursively-expanded variable, @code{make} does not expand the value you set
5521 for variable and function references immediately. Instead it stores the text
5522 verbatim, and saves these variable and function references to be expanded
5523 later, when you refer to the new variable (@pxref{Flavors, ,The Two Flavors
5524 of Variables}). When you use @samp{+=} on a recursively-expanded variable,
5525 it is this unexpanded text to which @code{make} appends the new text you
5536 is roughly equivalent to:
5541 variable = $(temp) more
5546 except that of course it never defines a variable called @code{temp}.
5547 The importance of this comes when the variable's old value contains
5548 variable references. Take this common example:
5551 CFLAGS = $(includes) -O
5553 CFLAGS += -pg # enable profiling
5557 The first line defines the @code{CFLAGS} variable with a reference to another
5558 variable, @code{includes}. (@code{CFLAGS} is used by the rules for C
5559 compilation; @pxref{Catalogue of Rules, ,Catalogue of Implicit Rules}.)
5560 Using @samp{=} for the definition makes @code{CFLAGS} a recursively-expanded
5561 variable, meaning @w{@samp{$(includes) -O}} is @emph{not} expanded when
5562 @code{make} processes the definition of @code{CFLAGS}. Thus, @code{includes}
5563 need not be defined yet for its value to take effect. It only has to be
5564 defined before any reference to @code{CFLAGS}. If we tried to append to the
5565 value of @code{CFLAGS} without using @samp{+=}, we might do it like this:
5568 CFLAGS := $(CFLAGS) -pg # enable profiling
5572 This is pretty close, but not quite what we want. Using @samp{:=}
5573 redefines @code{CFLAGS} as a simply-expanded variable; this means
5574 @code{make} expands the text @w{@samp{$(CFLAGS) -pg}} before setting the
5575 variable. If @code{includes} is not yet defined, we get @w{@samp{ -O
5576 -pg}}, and a later definition of @code{includes} will have no effect.
5577 Conversely, by using @samp{+=} we set @code{CFLAGS} to the
5578 @emph{unexpanded} value @w{@samp{$(includes) -O -pg}}. Thus we preserve
5579 the reference to @code{includes}, so if that variable gets defined at
5580 any later point, a reference like @samp{$(CFLAGS)} still uses its
5583 @node Override Directive, Defining, Appending, Using Variables
5584 @section The @code{override} Directive
5586 @cindex overriding with @code{override}
5587 @cindex variables, overriding
5589 If a variable has been set with a command argument
5590 (@pxref{Overriding, ,Overriding Variables}),
5591 then ordinary assignments in the makefile are ignored. If you want to set
5592 the variable in the makefile even though it was set with a command
5593 argument, you can use an @code{override} directive, which is a line that
5594 looks like this:@refill
5597 override @var{variable} = @var{value}
5604 override @var{variable} := @var{value}
5607 To append more text to a variable defined on the command line, use:
5610 override @var{variable} += @var{more text}
5614 @xref{Appending, ,Appending More Text to Variables}.
5616 The @code{override} directive was not invented for escalation in the war
5617 between makefiles and command arguments. It was invented so you can alter
5618 and add to values that the user specifies with command arguments.
5620 For example, suppose you always want the @samp{-g} switch when you run the
5621 C compiler, but you would like to allow the user to specify the other
5622 switches with a command argument just as usual. You could use this
5623 @code{override} directive:
5626 override CFLAGS += -g
5629 You can also use @code{override} directives with @code{define} directives.
5630 This is done as you might expect:
5640 See the next section for information about @code{define}.
5643 @xref{Defining, ,Defining Variables Verbatim}.
5646 @node Defining, Environment, Override Directive, Using Variables
5647 @section Defining Variables Verbatim
5650 @cindex verbatim variable definition
5651 @cindex defining variables verbatim
5652 @cindex variables, defining verbatim
5654 Another way to set the value of a variable is to use the @code{define}
5655 directive. This directive has an unusual syntax which allows newline
5656 characters to be included in the value, which is convenient for defining
5657 both canned sequences of commands
5658 (@pxref{Sequences, ,Defining Canned Command Sequences}), and also
5659 sections of makefile syntax to use with @code{eval} (@pxref{Eval Function}).
5661 The @code{define} directive is followed on the same line by the name of the
5662 variable and nothing more. The value to give the variable appears on the
5663 following lines. The end of the value is marked by a line containing just
5664 the word @code{endef}. Aside from this difference in syntax, @code{define}
5665 works just like @samp{=}: it creates a recursively-expanded variable
5666 (@pxref{Flavors, ,The Two Flavors of Variables}).
5667 The variable name may contain function and variable references, which
5668 are expanded when the directive is read to find the actual variable name
5671 You may nest @code{define} directives: @code{make} will keep track of
5672 nested directives and report an error if they are not all properly
5673 closed with @code{endef}. Note that lines beginning with tab
5674 characters are considered part of a command script, so any
5675 @code{define} or @code{endef} strings appearing on such a line will
5676 not be considered @code{make} operators.
5685 The value in an ordinary assignment cannot contain a newline; but the
5686 newlines that separate the lines of the value in a @code{define} become
5687 part of the variable's value (except for the final newline which precedes
5688 the @code{endef} and is not considered part of the value).@refill
5691 When used in a command script, the previous example is functionally
5695 two-lines = echo foo; echo $(bar)
5699 since two commands separated by semicolon behave much like two separate
5700 shell commands. However, note that using two separate lines means
5701 @code{make} will invoke the shell twice, running an independent subshell
5702 for each line. @xref{Execution, ,Command Execution}.
5704 If you want variable definitions made with @code{define} to take
5705 precedence over command-line variable definitions, you can use the
5706 @code{override} directive together with @code{define}:
5709 override define two-lines
5716 @xref{Override Directive, ,The @code{override} Directive}.
5718 @node Environment, Target-specific, Defining, Using Variables
5719 @section Variables from the Environment
5721 @cindex variables, environment
5723 Variables in @code{make} can come from the environment in which
5724 @code{make} is run. Every environment variable that @code{make} sees
5725 when it starts up is transformed into a @code{make} variable with the
5726 same name and value. However, an explicit assignment in the makefile,
5727 or with a command argument, overrides the environment. (If the
5728 @samp{-e} flag is specified, then values from the environment override
5729 assignments in the makefile. @xref{Options Summary, ,Summary of
5730 Options}. But this is not recommended practice.)
5732 Thus, by setting the variable @code{CFLAGS} in your environment, you can
5733 cause all C compilations in most makefiles to use the compiler switches you
5734 prefer. This is safe for variables with standard or conventional meanings
5735 because you know that no makefile will use them for other things. (Note
5736 this is not totally reliable; some makefiles set @code{CFLAGS} explicitly
5737 and therefore are not affected by the value in the environment.)
5739 When @code{make} runs a command script, variables defined in the
5740 makefile are placed into the environment of that command. This allows
5741 you to pass values to sub-@code{make} invocations (@pxref{Recursion,
5742 ,Recursive Use of @code{make}}). By default, only variables that came
5743 from the environment or the command line are passed to recursive
5744 invocations. You can use the @code{export} directive to pass other
5745 variables. @xref{Variables/Recursion, , Communicating Variables to a
5746 Sub-@code{make}}, for full details.
5748 Other use of variables from the environment is not recommended. It is not
5749 wise for makefiles to depend for their functioning on environment variables
5750 set up outside their control, since this would cause different users to get
5751 different results from the same makefile. This is against the whole
5752 purpose of most makefiles.
5754 @cindex SHELL, import from environment
5755 Such problems would be especially likely with the variable
5756 @code{SHELL}, which is normally present in the environment to specify
5757 the user's choice of interactive shell. It would be very undesirable
5758 for this choice to affect @code{make}; so, @code{make} handles the
5759 @code{SHELL} environment variable in a special way; see @ref{Choosing
5762 @node Target-specific, Pattern-specific, Environment, Using Variables
5763 @section Target-specific Variable Values
5764 @cindex target-specific variables
5765 @cindex variables, target-specific
5767 Variable values in @code{make} are usually global; that is, they are the
5768 same regardless of where they are evaluated (unless they're reset, of
5769 course). One exception to that is automatic variables
5770 (@pxref{Automatic Variables}).
5772 The other exception is @dfn{target-specific variable values}. This
5773 feature allows you to define different values for the same variable,
5774 based on the target that @code{make} is currently building. As with
5775 automatic variables, these values are only available within the context
5776 of a target's command script (and in other target-specific assignments).
5778 Set a target-specific variable value like this:
5781 @var{target} @dots{} : @var{variable-assignment}
5788 @var{target} @dots{} : override @var{variable-assignment}
5795 @var{target} @dots{} : export @var{variable-assignment}
5798 Multiple @var{target} values create a target-specific variable value for
5799 each member of the target list individually.
5801 The @var{variable-assignment} can be any valid form of assignment;
5802 recursive (@samp{=}), static (@samp{:=}), appending (@samp{+=}), or
5803 conditional (@samp{?=}). All variables that appear within the
5804 @var{variable-assignment} are evaluated within the context of the
5805 target: thus, any previously-defined target-specific variable values
5806 will be in effect. Note that this variable is actually distinct from
5807 any ``global'' value: the two variables do not have to have the same
5808 flavor (recursive vs.@: static).
5810 Target-specific variables have the same priority as any other makefile
5811 variable. Variables provided on the command-line (and in the
5812 environment if the @samp{-e} option is in force) will take precedence.
5813 Specifying the @code{override} directive will allow the target-specific
5814 variable value to be preferred.
5816 There is one more special feature of target-specific variables: when
5817 you define a target-specific variable that variable value is also in
5818 effect for all prerequisites of this target, and all their
5819 prerequisites, etc.@: (unless those prerequisites override that variable
5820 with their own target-specific variable value). So, for example, a
5821 statement like this:
5825 prog : prog.o foo.o bar.o
5829 will set @code{CFLAGS} to @samp{-g} in the command script for
5830 @file{prog}, but it will also set @code{CFLAGS} to @samp{-g} in the
5831 command scripts that create @file{prog.o}, @file{foo.o}, and
5832 @file{bar.o}, and any command scripts which create their
5835 Be aware that a given prerequisite will only be built once per
5836 invocation of make, at most. If the same file is a prerequisite of
5837 multiple targets, and each of those targets has a different value for
5838 the same target-specific variable, then the first target to be built
5839 will cause that prerequisite to be built and the prerequisite will
5840 inherit the target-specific value from the first target. It will
5841 ignore the target-specific values from any other targets.
5843 @node Pattern-specific, , Target-specific, Using Variables
5844 @section Pattern-specific Variable Values
5845 @cindex pattern-specific variables
5846 @cindex variables, pattern-specific
5848 In addition to target-specific variable values
5849 (@pxref{Target-specific, ,Target-specific Variable Values}), GNU
5850 @code{make} supports pattern-specific variable values. In this form,
5851 the variable is defined for any target that matches the pattern
5852 specified. If a target matches more than one pattern, all the
5853 matching pattern-specific variables are interpreted in the order in
5854 which they were defined in the makefile, and collected together into
5855 one set. Variables defined in this way are searched after any
5856 target-specific variables defined explicitly for that target, and
5857 before target-specific variables defined for the parent target.
5859 Set a pattern-specific variable value like this:
5862 @var{pattern} @dots{} : @var{variable-assignment}
5869 @var{pattern} @dots{} : override @var{variable-assignment}
5873 where @var{pattern} is a %-pattern. As with target-specific variable
5874 values, multiple @var{pattern} values create a pattern-specific variable
5875 value for each pattern individually. The @var{variable-assignment} can
5876 be any valid form of assignment. Any command-line variable setting will
5877 take precedence, unless @code{override} is specified.
5886 will assign @code{CFLAGS} the value of @samp{-O} for all targets
5887 matching the pattern @code{%.o}.
5889 @node Conditionals, Functions, Using Variables, Top
5890 @chapter Conditional Parts of Makefiles
5892 @cindex conditionals
5893 A @dfn{conditional} causes part of a makefile to be obeyed or ignored
5894 depending on the values of variables. Conditionals can compare the
5895 value of one variable to another, or the value of a variable to
5896 a constant string. Conditionals control what @code{make} actually
5897 ``sees'' in the makefile, so they @emph{cannot} be used to control shell
5898 commands at the time of execution.@refill
5901 * Conditional Example:: Example of a conditional
5902 * Conditional Syntax:: The syntax of conditionals.
5903 * Testing Flags:: Conditionals that test flags.
5906 @node Conditional Example, Conditional Syntax, Conditionals, Conditionals
5907 @section Example of a Conditional
5909 The following example of a conditional tells @code{make} to use one set
5910 of libraries if the @code{CC} variable is @samp{gcc}, and a different
5911 set of libraries otherwise. It works by controlling which of two
5912 command lines will be used as the command for a rule. The result is
5913 that @samp{CC=gcc} as an argument to @code{make} changes not only which
5914 compiler is used but also which libraries are linked.
5917 libs_for_gcc = -lgnu
5922 $(CC) -o foo $(objects) $(libs_for_gcc)
5924 $(CC) -o foo $(objects) $(normal_libs)
5928 This conditional uses three directives: one @code{ifeq}, one @code{else}
5929 and one @code{endif}.
5931 The @code{ifeq} directive begins the conditional, and specifies the
5932 condition. It contains two arguments, separated by a comma and surrounded
5933 by parentheses. Variable substitution is performed on both arguments and
5934 then they are compared. The lines of the makefile following the
5935 @code{ifeq} are obeyed if the two arguments match; otherwise they are
5938 The @code{else} directive causes the following lines to be obeyed if the
5939 previous conditional failed. In the example above, this means that the
5940 second alternative linking command is used whenever the first alternative
5941 is not used. It is optional to have an @code{else} in a conditional.
5943 The @code{endif} directive ends the conditional. Every conditional must
5944 end with an @code{endif}. Unconditional makefile text follows.
5946 As this example illustrates, conditionals work at the textual level:
5947 the lines of the conditional are treated as part of the makefile, or
5948 ignored, according to the condition. This is why the larger syntactic
5949 units of the makefile, such as rules, may cross the beginning or the
5950 end of the conditional.
5952 When the variable @code{CC} has the value @samp{gcc}, the above example has
5957 $(CC) -o foo $(objects) $(libs_for_gcc)
5961 When the variable @code{CC} has any other value, the effect is this:
5965 $(CC) -o foo $(objects) $(normal_libs)
5968 Equivalent results can be obtained in another way by conditionalizing a
5969 variable assignment and then using the variable unconditionally:
5972 libs_for_gcc = -lgnu
5976 libs=$(libs_for_gcc)
5982 $(CC) -o foo $(objects) $(libs)
5985 @node Conditional Syntax, Testing Flags, Conditional Example, Conditionals
5986 @section Syntax of Conditionals
5994 The syntax of a simple conditional with no @code{else} is as follows:
5997 @var{conditional-directive}
6003 The @var{text-if-true} may be any lines of text, to be considered as part
6004 of the makefile if the condition is true. If the condition is false, no
6005 text is used instead.
6007 The syntax of a complex conditional is as follows:
6010 @var{conditional-directive}
6020 @var{conditional-directive}
6021 @var{text-if-one-is-true}
6022 else @var{conditional-directive}
6030 There can be as many ``@code{else} @var{conditional-directive}''
6031 clauses as necessary. Once a given condition is true,
6032 @var{text-if-true} is used and no other clause is used; if no
6033 condition is true then @var{text-if-false} is used. The
6034 @var{text-if-true} and @var{text-if-false} can be any number of lines
6037 The syntax of the @var{conditional-directive} is the same whether the
6038 conditional is simple or complex; after an @code{else} or not. There
6039 are four different directives that test different conditions. Here is
6043 @item ifeq (@var{arg1}, @var{arg2})
6044 @itemx ifeq '@var{arg1}' '@var{arg2}'
6045 @itemx ifeq "@var{arg1}" "@var{arg2}"
6046 @itemx ifeq "@var{arg1}" '@var{arg2}'
6047 @itemx ifeq '@var{arg1}' "@var{arg2}"
6048 Expand all variable references in @var{arg1} and @var{arg2} and
6049 compare them. If they are identical, the @var{text-if-true} is
6050 effective; otherwise, the @var{text-if-false}, if any, is effective.
6052 Often you want to test if a variable has a non-empty value. When the
6053 value results from complex expansions of variables and functions,
6054 expansions you would consider empty may actually contain whitespace
6055 characters and thus are not seen as empty. However, you can use the
6056 @code{strip} function (@pxref{Text Functions}) to avoid interpreting
6057 whitespace as a non-empty value. For example:
6061 ifeq ($(strip $(foo)),)
6068 will evaluate @var{text-if-empty} even if the expansion of
6069 @code{$(foo)} contains whitespace characters.
6071 @item ifneq (@var{arg1}, @var{arg2})
6072 @itemx ifneq '@var{arg1}' '@var{arg2}'
6073 @itemx ifneq "@var{arg1}" "@var{arg2}"
6074 @itemx ifneq "@var{arg1}" '@var{arg2}'
6075 @itemx ifneq '@var{arg1}' "@var{arg2}"
6076 Expand all variable references in @var{arg1} and @var{arg2} and
6077 compare them. If they are different, the @var{text-if-true} is
6078 effective; otherwise, the @var{text-if-false}, if any, is effective.
6080 @item ifdef @var{variable-name}
6081 The @code{ifdef} form takes the @emph{name} of a variable as its
6082 argument, not a reference to a variable. The value of that variable
6083 has a non-empty value, the @var{text-if-true} is effective; otherwise,
6084 the @var{text-if-false}, if any, is effective. Variables that have
6085 never been defined have an empty value. The text @var{variable-name}
6086 is expanded, so it could be a variable or function that expands
6087 to the name of a variable. For example:
6097 The variable reference @code{$(foo)} is expanded, yielding @code{bar},
6098 which is considered to be the name of a variable. The variable
6099 @code{bar} is not expanded, but its value is examined to determine if
6102 Note that @code{ifdef} only tests whether a variable has a value. It
6103 does not expand the variable to see if that value is nonempty.
6104 Consequently, tests using @code{ifdef} return true for all definitions
6105 except those like @code{foo =}. To test for an empty value, use
6106 @w{@code{ifeq ($(foo),)}}. For example,
6119 sets @samp{frobozz} to @samp{yes}, while:
6131 sets @samp{frobozz} to @samp{no}.
6133 @item ifndef @var{variable-name}
6134 If the variable @var{variable-name} has an empty value, the
6135 @var{text-if-true} is effective; otherwise, the @var{text-if-false},
6136 if any, is effective. The rules for expansion and testing of
6137 @var{variable-name} are identical to the @code{ifdef} directive.
6140 Extra spaces are allowed and ignored at the beginning of the conditional
6141 directive line, but a tab is not allowed. (If the line begins with a tab,
6142 it will be considered a command for a rule.) Aside from this, extra spaces
6143 or tabs may be inserted with no effect anywhere except within the directive
6144 name or within an argument. A comment starting with @samp{#} may appear at
6145 the end of the line.
6147 The other two directives that play a part in a conditional are @code{else}
6148 and @code{endif}. Each of these directives is written as one word, with no
6149 arguments. Extra spaces are allowed and ignored at the beginning of the
6150 line, and spaces or tabs at the end. A comment starting with @samp{#} may
6151 appear at the end of the line.
6153 Conditionals affect which lines of the makefile @code{make} uses. If
6154 the condition is true, @code{make} reads the lines of the
6155 @var{text-if-true} as part of the makefile; if the condition is false,
6156 @code{make} ignores those lines completely. It follows that syntactic
6157 units of the makefile, such as rules, may safely be split across the
6158 beginning or the end of the conditional.@refill
6160 @code{make} evaluates conditionals when it reads a makefile.
6161 Consequently, you cannot use automatic variables in the tests of
6162 conditionals because they are not defined until commands are run
6163 (@pxref{Automatic Variables}).
6165 To prevent intolerable confusion, it is not permitted to start a
6166 conditional in one makefile and end it in another. However, you may
6167 write an @code{include} directive within a conditional, provided you do
6168 not attempt to terminate the conditional inside the included file.
6170 @node Testing Flags, , Conditional Syntax, Conditionals
6171 @section Conditionals that Test Flags
6173 You can write a conditional that tests @code{make} command flags such as
6174 @samp{-t} by using the variable @code{MAKEFLAGS} together with the
6175 @code{findstring} function
6176 (@pxref{Text Functions, , Functions for String Substitution and Analysis}).
6177 This is useful when @code{touch} is not enough to make a file appear up
6180 The @code{findstring} function determines whether one string appears as a
6181 substring of another. If you want to test for the @samp{-t} flag,
6182 use @samp{t} as the first string and the value of @code{MAKEFLAGS} as
6185 For example, here is how to arrange to use @samp{ranlib -t} to finish
6186 marking an archive file up to date:
6190 ifneq (,$(findstring t,$(MAKEFLAGS)))
6192 +ranlib -t archive.a
6199 The @samp{+} prefix marks those command lines as ``recursive'' so
6200 that they will be executed despite use of the @samp{-t} flag.
6201 @xref{Recursion, ,Recursive Use of @code{make}}.
6203 @node Functions, Running, Conditionals, Top
6204 @chapter Functions for Transforming Text
6207 @dfn{Functions} allow you to do text processing in the makefile to compute
6208 the files to operate on or the commands to use. You use a function in a
6209 @dfn{function call}, where you give the name of the function and some text
6210 (the @dfn{arguments}) for the function to operate on. The result of the
6211 function's processing is substituted into the makefile at the point of the
6212 call, just as a variable might be substituted.
6215 * Syntax of Functions:: How to write a function call.
6216 * Text Functions:: General-purpose text manipulation functions.
6217 * File Name Functions:: Functions for manipulating file names.
6218 * Conditional Functions:: Functions that implement conditions.
6219 * Foreach Function:: Repeat some text with controlled variation.
6220 * Call Function:: Expand a user-defined function.
6221 * Value Function:: Return the un-expanded value of a variable.
6222 * Eval Function:: Evaluate the arguments as makefile syntax.
6223 * Origin Function:: Find where a variable got its value.
6224 * Flavor Function:: Find out the flavor of a variable.
6225 * Shell Function:: Substitute the output of a shell command.
6226 * Make Control Functions:: Functions that control how make runs.
6229 @node Syntax of Functions, Text Functions, Functions, Functions
6230 @section Function Call Syntax
6231 @cindex @code{$}, in function call
6232 @cindex dollar sign (@code{$}), in function call
6233 @cindex arguments of functions
6234 @cindex functions, syntax of
6236 A function call resembles a variable reference. It looks like this:
6239 $(@var{function} @var{arguments})
6246 $@{@var{function} @var{arguments}@}
6249 Here @var{function} is a function name; one of a short list of names
6250 that are part of @code{make}. You can also essentially create your own
6251 functions by using the @code{call} builtin function.
6253 The @var{arguments} are the arguments of the function. They are
6254 separated from the function name by one or more spaces or tabs, and if
6255 there is more than one argument, then they are separated by commas.
6256 Such whitespace and commas are not part of an argument's value. The
6257 delimiters which you use to surround the function call, whether
6258 parentheses or braces, can appear in an argument only in matching pairs;
6259 the other kind of delimiters may appear singly. If the arguments
6260 themselves contain other function calls or variable references, it is
6261 wisest to use the same kind of delimiters for all the references; write
6262 @w{@samp{$(subst a,b,$(x))}}, not @w{@samp{$(subst a,b,$@{x@})}}. This
6263 is because it is clearer, and because only one type of delimiter is
6264 matched to find the end of the reference.
6266 The text written for each argument is processed by substitution of
6267 variables and function calls to produce the argument value, which
6268 is the text on which the function acts. The substitution is done in the
6269 order in which the arguments appear.
6271 Commas and unmatched parentheses or braces cannot appear in the text of an
6272 argument as written; leading spaces cannot appear in the text of the first
6273 argument as written. These characters can be put into the argument value
6274 by variable substitution. First define variables @code{comma} and
6275 @code{space} whose values are isolated comma and space characters, then
6276 substitute these variables where such characters are wanted, like this:
6282 space:= $(empty) $(empty)
6284 bar:= $(subst $(space),$(comma),$(foo))
6285 # @r{bar is now `a,b,c'.}
6290 Here the @code{subst} function replaces each space with a comma, through
6291 the value of @code{foo}, and substitutes the result.
6293 @node Text Functions, File Name Functions, Syntax of Functions, Functions
6294 @section Functions for String Substitution and Analysis
6295 @cindex functions, for text
6297 Here are some functions that operate on strings:
6300 @item $(subst @var{from},@var{to},@var{text})
6302 Performs a textual replacement on the text @var{text}: each occurrence
6303 of @var{from} is replaced by @var{to}. The result is substituted for
6304 the function call. For example,
6307 $(subst ee,EE,feet on the street)
6310 substitutes the string @samp{fEEt on the strEEt}.
6312 @item $(patsubst @var{pattern},@var{replacement},@var{text})
6314 Finds whitespace-separated words in @var{text} that match
6315 @var{pattern} and replaces them with @var{replacement}. Here
6316 @var{pattern} may contain a @samp{%} which acts as a wildcard,
6317 matching any number of any characters within a word. If
6318 @var{replacement} also contains a @samp{%}, the @samp{%} is replaced
6319 by the text that matched the @samp{%} in @var{pattern}. Only the first
6320 @samp{%} in the @var{pattern} and @var{replacement} is treated this
6321 way; any subsequent @samp{%} is unchanged.@refill
6323 @cindex @code{%}, quoting in @code{patsubst}
6324 @cindex @code{%}, quoting with @code{\} (backslash)
6325 @cindex @code{\} (backslash), to quote @code{%}
6326 @cindex backslash (@code{\}), to quote @code{%}
6327 @cindex quoting @code{%}, in @code{patsubst}
6328 @samp{%} characters in @code{patsubst} function invocations can be
6329 quoted with preceding backslashes (@samp{\}). Backslashes that would
6330 otherwise quote @samp{%} characters can be quoted with more backslashes.
6331 Backslashes that quote @samp{%} characters or other backslashes are
6332 removed from the pattern before it is compared file names or has a stem
6333 substituted into it. Backslashes that are not in danger of quoting
6334 @samp{%} characters go unmolested. For example, the pattern
6335 @file{the\%weird\\%pattern\\} has @samp{the%weird\} preceding the
6336 operative @samp{%} character, and @samp{pattern\\} following it. The
6337 final two backslashes are left alone because they cannot affect any
6338 @samp{%} character.@refill
6340 Whitespace between words is folded into single space characters;
6341 leading and trailing whitespace is discarded.
6346 $(patsubst %.c,%.o,x.c.c bar.c)
6350 produces the value @samp{x.c.o bar.o}.
6352 Substitution references (@pxref{Substitution Refs, ,Substitution
6353 References}) are a simpler way to get the effect of the @code{patsubst}
6357 $(@var{var}:@var{pattern}=@var{replacement})
6364 $(patsubst @var{pattern},@var{replacement},$(@var{var}))
6367 The second shorthand simplifies one of the most common uses of
6368 @code{patsubst}: replacing the suffix at the end of file names.
6371 $(@var{var}:@var{suffix}=@var{replacement})
6378 $(patsubst %@var{suffix},%@var{replacement},$(@var{var}))
6382 For example, you might have a list of object files:
6385 objects = foo.o bar.o baz.o
6389 To get the list of corresponding source files, you could simply write:
6396 instead of using the general form:
6399 $(patsubst %.o,%.c,$(objects))
6402 @item $(strip @var{string})
6403 @cindex stripping whitespace
6404 @cindex whitespace, stripping
6405 @cindex spaces, stripping
6407 Removes leading and trailing whitespace from @var{string} and replaces
6408 each internal sequence of one or more whitespace characters with a
6409 single space. Thus, @samp{$(strip a b c )} results in @w{@samp{a b c}}.
6411 The function @code{strip} can be very useful when used in conjunction
6412 with conditionals. When comparing something with the empty string
6413 @samp{} using @code{ifeq} or @code{ifneq}, you usually want a string of
6414 just whitespace to match the empty string (@pxref{Conditionals}).
6416 Thus, the following may fail to have the desired results:
6420 ifneq "$(needs_made)" ""
6423 all:;@@echo 'Nothing to make!'
6428 Replacing the variable reference @w{@samp{$(needs_made)}} with the
6429 function call @w{@samp{$(strip $(needs_made))}} in the @code{ifneq}
6430 directive would make it more robust.@refill
6432 @item $(findstring @var{find},@var{in})
6434 @cindex searching for strings
6435 @cindex finding strings
6436 @cindex strings, searching for
6437 Searches @var{in} for an occurrence of @var{find}. If it occurs, the
6438 value is @var{find}; otherwise, the value is empty. You can use this
6439 function in a conditional to test for the presence of a specific
6440 substring in a given string. Thus, the two examples,
6443 $(findstring a,a b c)
6448 produce the values @samp{a} and @samp{} (the empty string),
6449 respectively. @xref{Testing Flags}, for a practical application of
6450 @code{findstring}.@refill
6454 @cindex filtering words
6455 @cindex words, filtering
6456 @item $(filter @var{pattern}@dots{},@var{text})
6457 Returns all whitespace-separated words in @var{text} that @emph{do} match
6458 any of the @var{pattern} words, removing any words that @emph{do not}
6459 match. The patterns are written using @samp{%}, just like the patterns
6460 used in the @code{patsubst} function above.@refill
6462 The @code{filter} function can be used to separate out different types
6463 of strings (such as file names) in a variable. For example:
6466 sources := foo.c bar.c baz.s ugh.h
6468 cc $(filter %.c %.s,$(sources)) -o foo
6472 says that @file{foo} depends of @file{foo.c}, @file{bar.c},
6473 @file{baz.s} and @file{ugh.h} but only @file{foo.c}, @file{bar.c} and
6474 @file{baz.s} should be specified in the command to the
6477 @item $(filter-out @var{pattern}@dots{},@var{text})
6479 @cindex filtering out words
6480 @cindex words, filtering out
6481 Returns all whitespace-separated words in @var{text} that @emph{do not}
6482 match any of the @var{pattern} words, removing the words that @emph{do}
6483 match one or more. This is the exact opposite of the @code{filter}
6490 objects=main1.o foo.o main2.o bar.o
6491 mains=main1.o main2.o
6496 the following generates a list which contains all the object files not
6500 $(filter-out $(mains),$(objects))
6505 @cindex sorting words
6506 @item $(sort @var{list})
6507 Sorts the words of @var{list} in lexical order, removing duplicate
6508 words. The output is a list of words separated by single spaces.
6512 $(sort foo bar lose)
6516 returns the value @samp{bar foo lose}.
6518 @cindex removing duplicate words
6519 @cindex duplicate words, removing
6520 @cindex words, removing duplicates
6521 Incidentally, since @code{sort} removes duplicate words, you can use
6522 it for this purpose even if you don't care about the sort order.
6524 @item $(word @var{n},@var{text})
6526 @cindex word, selecting a
6527 @cindex selecting a word
6528 Returns the @var{n}th word of @var{text}. The legitimate values of
6529 @var{n} start from 1. If @var{n} is bigger than the number of words
6530 in @var{text}, the value is empty. For example,
6533 $(word 2, foo bar baz)
6539 @item $(wordlist @var{s},@var{e},@var{text})
6541 @cindex words, selecting lists of
6542 @cindex selecting word lists
6543 Returns the list of words in @var{text} starting with word @var{s} and
6544 ending with word @var{e} (inclusive). The legitimate values of @var{s}
6545 start from 1; @var{e} may start from 0. If @var{s} is bigger than the
6546 number of words in @var{text}, the value is empty. If @var{e} is
6547 bigger than the number of words in @var{text}, words up to the end of
6548 @var{text} are returned. If @var{s} is greater than @var{e}, nothing
6549 is returned. For example,
6552 $(wordlist 2, 3, foo bar baz)
6556 returns @samp{bar baz}.
6558 @c Following item phrased to prevent overfull hbox. --RJC 17 Jul 92
6559 @item $(words @var{text})
6561 @cindex words, finding number
6562 Returns the number of words in @var{text}.
6563 Thus, the last word of @var{text} is
6564 @w{@code{$(word $(words @var{text}),@var{text})}}.@refill
6566 @item $(firstword @var{names}@dots{})
6568 @cindex words, extracting first
6569 The argument @var{names} is regarded as a series of names, separated
6570 by whitespace. The value is the first name in the series. The rest
6571 of the names are ignored.
6576 $(firstword foo bar)
6580 produces the result @samp{foo}. Although @code{$(firstword
6581 @var{text})} is the same as @code{$(word 1,@var{text})}, the
6582 @code{firstword} function is retained for its simplicity.@refill
6585 @item $(lastword @var{names}@dots{})
6587 @cindex words, extracting last
6588 The argument @var{names} is regarded as a series of names, separated
6589 by whitespace. The value is the last name in the series.
6598 produces the result @samp{bar}. Although @code{$(lastword
6599 @var{text})} is the same as @code{$(word $(words @var{text}),@var{text})},
6600 the @code{lastword} function was added for its simplicity and better
6605 Here is a realistic example of the use of @code{subst} and
6606 @code{patsubst}. Suppose that a makefile uses the @code{VPATH} variable
6607 to specify a list of directories that @code{make} should search for
6609 (@pxref{General Search, , @code{VPATH} Search Path for All Prerequisites}).
6610 This example shows how to
6611 tell the C compiler to search for header files in the same list of
6614 The value of @code{VPATH} is a list of directories separated by colons,
6615 such as @samp{src:../headers}. First, the @code{subst} function is used to
6616 change the colons to spaces:
6619 $(subst :, ,$(VPATH))
6623 This produces @samp{src ../headers}. Then @code{patsubst} is used to turn
6624 each directory name into a @samp{-I} flag. These can be added to the
6625 value of the variable @code{CFLAGS}, which is passed automatically to the C
6626 compiler, like this:
6629 override CFLAGS += $(patsubst %,-I%,$(subst :, ,$(VPATH)))
6633 The effect is to append the text @samp{-Isrc -I../headers} to the
6634 previously given value of @code{CFLAGS}. The @code{override} directive is
6635 used so that the new value is assigned even if the previous value of
6636 @code{CFLAGS} was specified with a command argument (@pxref{Override
6637 Directive, , The @code{override} Directive}).
6639 @node File Name Functions, Conditional Functions, Text Functions, Functions
6640 @section Functions for File Names
6641 @cindex functions, for file names
6642 @cindex file name functions
6644 Several of the built-in expansion functions relate specifically to
6645 taking apart file names or lists of file names.
6647 Each of the following functions performs a specific transformation on a
6648 file name. The argument of the function is regarded as a series of file
6649 names, separated by whitespace. (Leading and trailing whitespace is
6650 ignored.) Each file name in the series is transformed in the same way and
6651 the results are concatenated with single spaces between them.
6654 @item $(dir @var{names}@dots{})
6656 @cindex directory part
6657 @cindex file name, directory part
6658 Extracts the directory-part of each file name in @var{names}. The
6659 directory-part of the file name is everything up through (and
6660 including) the last slash in it. If the file name contains no slash,
6661 the directory part is the string @samp{./}. For example,
6664 $(dir src/foo.c hacks)
6668 produces the result @samp{src/ ./}.
6670 @item $(notdir @var{names}@dots{})
6672 @cindex file name, nondirectory part
6673 @cindex nondirectory part
6674 Extracts all but the directory-part of each file name in @var{names}.
6675 If the file name contains no slash, it is left unchanged. Otherwise,
6676 everything through the last slash is removed from it.
6678 A file name that ends with a slash becomes an empty string. This is
6679 unfortunate, because it means that the result does not always have the
6680 same number of whitespace-separated file names as the argument had;
6681 but we do not see any other valid alternative.
6686 $(notdir src/foo.c hacks)
6690 produces the result @samp{foo.c hacks}.
6692 @item $(suffix @var{names}@dots{})
6694 @cindex suffix, function to find
6695 @cindex file name suffix
6696 Extracts the suffix of each file name in @var{names}. If the file name
6697 contains a period, the suffix is everything starting with the last
6698 period. Otherwise, the suffix is the empty string. This frequently
6699 means that the result will be empty when @var{names} is not, and if
6700 @var{names} contains multiple file names, the result may contain fewer
6706 $(suffix src/foo.c src-1.0/bar.c hacks)
6710 produces the result @samp{.c .c}.
6712 @item $(basename @var{names}@dots{})
6715 @cindex file name, basename of
6716 Extracts all but the suffix of each file name in @var{names}. If the
6717 file name contains a period, the basename is everything starting up to
6718 (and not including) the last period. Periods in the directory part are
6719 ignored. If there is no period, the basename is the entire file name.
6723 $(basename src/foo.c src-1.0/bar hacks)
6727 produces the result @samp{src/foo src-1.0/bar hacks}.
6729 @c plural convention with dots (be consistent)
6730 @item $(addsuffix @var{suffix},@var{names}@dots{})
6732 @cindex suffix, adding
6733 @cindex file name suffix, adding
6734 The argument @var{names} is regarded as a series of names, separated
6735 by whitespace; @var{suffix} is used as a unit. The value of
6736 @var{suffix} is appended to the end of each individual name and the
6737 resulting larger names are concatenated with single spaces between
6741 $(addsuffix .c,foo bar)
6745 produces the result @samp{foo.c bar.c}.
6747 @item $(addprefix @var{prefix},@var{names}@dots{})
6749 @cindex prefix, adding
6750 @cindex file name prefix, adding
6751 The argument @var{names} is regarded as a series of names, separated
6752 by whitespace; @var{prefix} is used as a unit. The value of
6753 @var{prefix} is prepended to the front of each individual name and the
6754 resulting larger names are concatenated with single spaces between
6758 $(addprefix src/,foo bar)
6762 produces the result @samp{src/foo src/bar}.
6764 @item $(join @var{list1},@var{list2})
6766 @cindex joining lists of words
6767 @cindex words, joining lists
6768 Concatenates the two arguments word by word: the two first words (one
6769 from each argument) concatenated form the first word of the result, the
6770 two second words form the second word of the result, and so on. So the
6771 @var{n}th word of the result comes from the @var{n}th word of each
6772 argument. If one argument has more words that the other, the extra
6773 words are copied unchanged into the result.
6775 For example, @samp{$(join a b,.c .o)} produces @samp{a.c b.o}.
6777 Whitespace between the words in the lists is not preserved; it is
6778 replaced with a single space.
6780 This function can merge the results of the @code{dir} and
6781 @code{notdir} functions, to produce the original list of files which
6782 was given to those two functions.@refill
6784 @item $(wildcard @var{pattern})
6786 @cindex wildcard, function
6787 The argument @var{pattern} is a file name pattern, typically containing
6788 wildcard characters (as in shell file name patterns). The result of
6789 @code{wildcard} is a space-separated list of the names of existing files
6790 that match the pattern.
6791 @xref{Wildcards, ,Using Wildcard Characters in File Names}.
6793 @item $(realpath @var{names}@dots{})
6796 @cindex file name, realpath of
6797 For each file name in @var{names} return the canonical absolute name.
6798 A canonical name does not contain any @code{.} or @code{..} components,
6799 nor any repeated path separators (@code{/}) or symlinks. In case of a
6800 failure the empty string is returned. Consult the @code{realpath(3)}
6801 documentation for a list of possible failure causes.
6803 @item $(abspath @var{names}@dots{})
6806 @cindex file name, abspath of
6807 For each file name in @var{names} return an absolute name that does
6808 not contain any @code{.} or @code{..} components, nor any repeated path
6809 separators (@code{/}). Note that, in contrast to @code{realpath}
6810 function, @code{abspath} does not resolve symlinks and does not require
6811 the file names to refer to an existing file or directory. Use the
6812 @code{wildcard} function to test for existence.
6815 @node Conditional Functions, Foreach Function, File Name Functions, Functions
6816 @section Functions for Conditionals
6818 @cindex conditional expansion
6819 There are three functions that provide conditional expansion. A key
6820 aspect of these functions is that not all of the arguments are
6821 expanded initially. Only those arguments which need to be expanded,
6825 @item $(if @var{condition},@var{then-part}[,@var{else-part}])
6827 The @code{if} function provides support for conditional expansion in a
6828 functional context (as opposed to the GNU @code{make} makefile
6829 conditionals such as @code{ifeq} (@pxref{Conditional Syntax, ,Syntax of
6832 The first argument, @var{condition}, first has all preceding and
6833 trailing whitespace stripped, then is expanded. If it expands to any
6834 non-empty string, then the condition is considered to be true. If it
6835 expands to an empty string, the condition is considered to be false.
6837 If the condition is true then the second argument, @var{then-part}, is
6838 evaluated and this is used as the result of the evaluation of the entire
6841 If the condition is false then the third argument, @var{else-part}, is
6842 evaluated and this is the result of the @code{if} function. If there is
6843 no third argument, the @code{if} function evaluates to nothing (the
6846 Note that only one of the @var{then-part} or the @var{else-part} will be
6847 evaluated, never both. Thus, either can contain side-effects (such as
6848 @code{shell} function calls, etc.)
6850 @item $(or @var{condition1}[,@var{condition2}[,@var{condition3}@dots{}]])
6852 The @code{or} function provides a ``short-circuiting'' OR operation.
6853 Each argument is expanded, in order. If an argument expands to a
6854 non-empty string the processing stops and the result of the expansion
6855 is that string. If, after all arguments are expanded, all of them are
6856 false (empty), then the result of the expansion is the empty string.
6858 @item $(and @var{condition1}[,@var{condition2}[,@var{condition3}@dots{}]])
6860 The @code{and} function provides a ``short-circuiting'' AND operation.
6861 Each argument is expanded, in order. If an argument expands to an
6862 empty string the processing stops and the result of the expansion is
6863 the empty string. If all arguments expand to a non-empty string then
6864 the result of the expansion is the expansion of the last argument.
6868 @node Foreach Function, Call Function, Conditional Functions, Functions
6869 @section The @code{foreach} Function
6871 @cindex words, iterating over
6873 The @code{foreach} function is very different from other functions. It
6874 causes one piece of text to be used repeatedly, each time with a different
6875 substitution performed on it. It resembles the @code{for} command in the
6876 shell @code{sh} and the @code{foreach} command in the C-shell @code{csh}.
6878 The syntax of the @code{foreach} function is:
6881 $(foreach @var{var},@var{list},@var{text})
6885 The first two arguments, @var{var} and @var{list}, are expanded before
6886 anything else is done; note that the last argument, @var{text}, is
6887 @strong{not} expanded at the same time. Then for each word of the expanded
6888 value of @var{list}, the variable named by the expanded value of @var{var}
6889 is set to that word, and @var{text} is expanded. Presumably @var{text}
6890 contains references to that variable, so its expansion will be different
6893 The result is that @var{text} is expanded as many times as there are
6894 whitespace-separated words in @var{list}. The multiple expansions of
6895 @var{text} are concatenated, with spaces between them, to make the result
6898 This simple example sets the variable @samp{files} to the list of all files
6899 in the directories in the list @samp{dirs}:
6903 files := $(foreach dir,$(dirs),$(wildcard $(dir)/*))
6906 Here @var{text} is @samp{$(wildcard $(dir)/*)}. The first repetition
6907 finds the value @samp{a} for @code{dir}, so it produces the same result
6908 as @samp{$(wildcard a/*)}; the second repetition produces the result
6909 of @samp{$(wildcard b/*)}; and the third, that of @samp{$(wildcard c/*)}.
6911 This example has the same result (except for setting @samp{dirs}) as
6912 the following example:
6915 files := $(wildcard a/* b/* c/* d/*)
6918 When @var{text} is complicated, you can improve readability by giving it
6919 a name, with an additional variable:
6922 find_files = $(wildcard $(dir)/*)
6924 files := $(foreach dir,$(dirs),$(find_files))
6928 Here we use the variable @code{find_files} this way. We use plain @samp{=}
6929 to define a recursively-expanding variable, so that its value contains an
6930 actual function call to be reexpanded under the control of @code{foreach};
6931 a simply-expanded variable would not do, since @code{wildcard} would be
6932 called only once at the time of defining @code{find_files}.
6934 The @code{foreach} function has no permanent effect on the variable
6935 @var{var}; its value and flavor after the @code{foreach} function call are
6936 the same as they were beforehand. The other values which are taken from
6937 @var{list} are in effect only temporarily, during the execution of
6938 @code{foreach}. The variable @var{var} is a simply-expanded variable
6939 during the execution of @code{foreach}. If @var{var} was undefined
6940 before the @code{foreach} function call, it is undefined after the call.
6941 @xref{Flavors, ,The Two Flavors of Variables}.@refill
6943 You must take care when using complex variable expressions that result in
6944 variable names because many strange things are valid variable names, but
6945 are probably not what you intended. For example,
6948 files := $(foreach Esta escrito en espanol!,b c ch,$(find_files))
6952 might be useful if the value of @code{find_files} references the variable
6953 whose name is @samp{Esta escrito en espanol!} (es un nombre bastante largo,
6954 no?), but it is more likely to be a mistake.
6956 @node Call Function, Value Function, Foreach Function, Functions
6957 @section The @code{call} Function
6959 @cindex functions, user defined
6960 @cindex user defined functions
6962 The @code{call} function is unique in that it can be used to create new
6963 parameterized functions. You can write a complex expression as the
6964 value of a variable, then use @code{call} to expand it with different
6967 The syntax of the @code{call} function is:
6970 $(call @var{variable},@var{param},@var{param},@dots{})
6973 When @code{make} expands this function, it assigns each @var{param} to
6974 temporary variables @code{$(1)}, @code{$(2)}, etc. The variable
6975 @code{$(0)} will contain @var{variable}. There is no maximum number of
6976 parameter arguments. There is no minimum, either, but it doesn't make
6977 sense to use @code{call} with no parameters.
6979 Then @var{variable} is expanded as a @code{make} variable in the context
6980 of these temporary assignments. Thus, any reference to @code{$(1)} in
6981 the value of @var{variable} will resolve to the first @var{param} in the
6982 invocation of @code{call}.
6984 Note that @var{variable} is the @emph{name} of a variable, not a
6985 @emph{reference} to that variable. Therefore you would not normally use
6986 a @samp{$} or parentheses when writing it. (You can, however, use a
6987 variable reference in the name if you want the name not to be a
6990 If @var{variable} is the name of a builtin function, the builtin function
6991 is always invoked (even if a @code{make} variable by that name also
6994 The @code{call} function expands the @var{param} arguments before
6995 assigning them to temporary variables. This means that @var{variable}
6996 values containing references to builtin functions that have special
6997 expansion rules, like @code{foreach} or @code{if}, may not work as you
7000 Some examples may make this clearer.
7002 This macro simply reverses its arguments:
7007 foo = $(call reverse,a,b)
7011 Here @var{foo} will contain @samp{b a}.
7013 This one is slightly more interesting: it defines a macro to search for
7014 the first instance of a program in @code{PATH}:
7017 pathsearch = $(firstword $(wildcard $(addsuffix /$(1),$(subst :, ,$(PATH)))))
7019 LS := $(call pathsearch,ls)
7023 Now the variable LS contains @code{/bin/ls} or similar.
7025 The @code{call} function can be nested. Each recursive invocation gets
7026 its own local values for @code{$(1)}, etc.@: that mask the values of
7027 higher-level @code{call}. For example, here is an implementation of a
7031 map = $(foreach a,$(2),$(call $(1),$(a)))
7034 Now you can @var{map} a function that normally takes only one argument,
7035 such as @code{origin}, to multiple values in one step:
7038 o = $(call map,origin,o map MAKE)
7041 and end up with @var{o} containing something like @samp{file file default}.
7043 A final caution: be careful when adding whitespace to the arguments to
7044 @code{call}. As with other functions, any whitespace contained in the
7045 second and subsequent arguments is kept; this can cause strange
7046 effects. It's generally safest to remove all extraneous whitespace when
7047 providing parameters to @code{call}.
7049 @node Value Function, Eval Function, Call Function, Functions
7050 @comment node-name, next, previous, up
7051 @section The @code{value} Function
7053 @cindex variables, unexpanded value
7055 The @code{value} function provides a way for you to use the value of a
7056 variable @emph{without} having it expanded. Please note that this
7057 does not undo expansions which have already occurred; for example if
7058 you create a simply expanded variable its value is expanded during the
7059 definition; in that case the @code{value} function will return the
7060 same result as using the variable directly.
7062 The syntax of the @code{value} function is:
7065 $(value @var{variable})
7068 Note that @var{variable} is the @emph{name} of a variable; not a
7069 @emph{reference} to that variable. Therefore you would not normally
7070 use a @samp{$} or parentheses when writing it. (You can, however, use
7071 a variable reference in the name if you want the name not to be a
7074 The result of this function is a string containing the value of
7075 @var{variable}, without any expansion occurring. For example, in this
7089 The first output line would be @code{ATH}, since the ``$P'' would be
7090 expanded as a @code{make} variable, while the second output line would
7091 be the current value of your @code{$PATH} environment variable, since
7092 the @code{value} function avoided the expansion.
7094 The @code{value} function is most often used in conjunction with the
7095 @code{eval} function (@pxref{Eval Function}).
7097 @node Eval Function, Origin Function, Value Function, Functions
7098 @comment node-name, next, previous, up
7099 @section The @code{eval} Function
7101 @cindex evaluating makefile syntax
7102 @cindex makefile syntax, evaluating
7104 The @code{eval} function is very special: it allows you to define new
7105 makefile constructs that are not constant; which are the result of
7106 evaluating other variables and functions. The argument to the
7107 @code{eval} function is expanded, then the results of that expansion
7108 are parsed as makefile syntax. The expanded results can define new
7109 @code{make} variables, targets, implicit or explicit rules, etc.
7111 The result of the @code{eval} function is always the empty string;
7112 thus, it can be placed virtually anywhere in a makefile without
7113 causing syntax errors.
7115 It's important to realize that the @code{eval} argument is expanded
7116 @emph{twice}; first by the @code{eval} function, then the results of
7117 that expansion are expanded again when they are parsed as makefile
7118 syntax. This means you may need to provide extra levels of escaping
7119 for ``$'' characters when using @code{eval}. The @code{value}
7120 function (@pxref{Value Function}) can sometimes be useful in these
7121 situations, to circumvent unwanted expansions.
7123 Here is an example of how @code{eval} can be used; this example
7124 combines a number of concepts and other functions. Although it might
7125 seem overly complex to use @code{eval} in this example, rather than
7126 just writing out the rules, consider two things: first, the template
7127 definition (in @code{PROGRAM_template}) could need to be much more
7128 complex than it is here; and second, you might put the complex,
7129 ``generic'' part of this example into another makefile, then include
7130 it in all the individual makefiles. Now your individual makefiles are
7131 quite straightforward.
7135 PROGRAMS = server client
7137 server_OBJS = server.o server_priv.o server_access.o
7138 server_LIBS = priv protocol
7140 client_OBJS = client.o client_api.o client_mem.o
7141 client_LIBS = protocol
7143 # Everything after this is generic
7148 define PROGRAM_template
7149 $(1): $$($(1)_OBJS) $$($(1)_LIBS:%=-l%)
7150 ALL_OBJS += $$($(1)_OBJS)
7153 $(foreach prog,$(PROGRAMS),$(eval $(call PROGRAM_template,$(prog))))
7156 $(LINK.o) $^ $(LDLIBS) -o $@@
7159 rm -f $(ALL_OBJS) $(PROGRAMS)
7163 @node Origin Function, Flavor Function, Eval Function, Functions
7164 @section The @code{origin} Function
7166 @cindex variables, origin of
7167 @cindex origin of variable
7169 The @code{origin} function is unlike most other functions in that it does
7170 not operate on the values of variables; it tells you something @emph{about}
7171 a variable. Specifically, it tells you where it came from.
7173 The syntax of the @code{origin} function is:
7176 $(origin @var{variable})
7179 Note that @var{variable} is the @emph{name} of a variable to inquire about;
7180 not a @emph{reference} to that variable. Therefore you would not normally
7181 use a @samp{$} or parentheses when writing it. (You can, however, use a
7182 variable reference in the name if you want the name not to be a constant.)
7184 The result of this function is a string telling you how the variable
7185 @var{variable} was defined:
7190 if @var{variable} was never defined.
7194 if @var{variable} has a default definition, as is usual with @code{CC}
7195 and so on. @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
7196 Note that if you have redefined a default variable, the @code{origin}
7197 function will return the origin of the later definition.
7201 if @var{variable} was defined as an environment variable and the
7202 @samp{-e} option is @emph{not} turned on (@pxref{Options Summary, ,Summary of Options}).
7204 @item environment override
7206 if @var{variable} was defined as an environment variable and the
7207 @w{@samp{-e}} option @emph{is} turned on (@pxref{Options Summary,
7208 ,Summary of Options}).@refill
7212 if @var{variable} was defined in a makefile.
7216 if @var{variable} was defined on the command line.
7220 if @var{variable} was defined with an @code{override} directive in a
7221 makefile (@pxref{Override Directive, ,The @code{override} Directive}).
7225 if @var{variable} is an automatic variable defined for the
7226 execution of the commands for each rule
7227 (@pxref{Automatic Variables}).
7230 This information is primarily useful (other than for your curiosity) to
7231 determine if you want to believe the value of a variable. For example,
7232 suppose you have a makefile @file{foo} that includes another makefile
7233 @file{bar}. You want a variable @code{bletch} to be defined in @file{bar}
7234 if you run the command @w{@samp{make -f bar}}, even if the environment contains
7235 a definition of @code{bletch}. However, if @file{foo} defined
7236 @code{bletch} before including @file{bar}, you do not want to override that
7237 definition. This could be done by using an @code{override} directive in
7238 @file{foo}, giving that definition precedence over the later definition in
7239 @file{bar}; unfortunately, the @code{override} directive would also
7240 override any command line definitions. So, @file{bar} could
7246 ifeq "$(origin bletch)" "environment"
7247 bletch = barf, gag, etc.
7254 If @code{bletch} has been defined from the environment, this will redefine
7257 If you want to override a previous definition of @code{bletch} if it came
7258 from the environment, even under @samp{-e}, you could instead write:
7262 ifneq "$(findstring environment,$(origin bletch))" ""
7263 bletch = barf, gag, etc.
7268 Here the redefinition takes place if @samp{$(origin bletch)} returns either
7269 @samp{environment} or @samp{environment override}.
7270 @xref{Text Functions, , Functions for String Substitution and Analysis}.
7272 @node Flavor Function, Shell Function, Origin Function, Functions
7273 @section The @code{flavor} Function
7275 @cindex variables, flavor of
7276 @cindex flavor of variable
7278 The @code{flavor} function is unlike most other functions (and like
7279 @code{origin} function) in that it does not operate on the values of
7280 variables; it tells you something @emph{about} a variable.
7281 Specifically, it tells you the flavor of a variable (@pxref{Flavors,
7282 ,The Two Flavors of Variables}).
7284 The syntax of the @code{flavor} function is:
7287 $(flavor @var{variable})
7290 Note that @var{variable} is the @emph{name} of a variable to inquire about;
7291 not a @emph{reference} to that variable. Therefore you would not normally
7292 use a @samp{$} or parentheses when writing it. (You can, however, use a
7293 variable reference in the name if you want the name not to be a constant.)
7295 The result of this function is a string that identifies the flavor of the
7296 variable @var{variable}:
7301 if @var{variable} was never defined.
7305 if @var{variable} is a recursively expanded variable.
7309 if @var{variable} is a simply expanded variable.
7314 @node Shell Function, Make Control Functions, Flavor Function, Functions
7315 @section The @code{shell} Function
7317 @cindex commands, expansion
7319 @cindex shell command, function for
7321 The @code{shell} function is unlike any other function other than the
7322 @code{wildcard} function
7323 (@pxref{Wildcard Function, ,The Function @code{wildcard}}) in that it
7324 communicates with the world outside of @code{make}.
7326 The @code{shell} function performs the same function that backquotes
7327 (@samp{`}) perform in most shells: it does @dfn{command expansion}.
7328 This means that it takes as an argument a shell command and evaluates
7329 to the output of the command. The only processing @code{make} does on
7330 the result is to convert each newline (or carriage-return / newline
7331 pair) to a single space. If there is a trailing (carriage-return
7332 and) newline it will simply be removed.@refill
7334 The commands run by calls to the @code{shell} function are run when the
7335 function calls are expanded (@pxref{Reading Makefiles, , How
7336 @code{make} Reads a Makefile}). Because this function involves
7337 spawning a new shell, you should carefully consider the performance
7338 implications of using the @code{shell} function within recursively
7339 expanded variables vs.@: simply expanded variables (@pxref{Flavors, ,The
7340 Two Flavors of Variables}).
7342 Here are some examples of the use of the @code{shell} function:
7345 contents := $(shell cat foo)
7349 sets @code{contents} to the contents of the file @file{foo}, with a space
7350 (rather than a newline) separating each line.
7353 files := $(shell echo *.c)
7357 sets @code{files} to the expansion of @samp{*.c}. Unless @code{make} is
7358 using a very strange shell, this has the same result as
7359 @w{@samp{$(wildcard *.c)}} (as long as at least one @samp{.c} file
7362 @node Make Control Functions, , Shell Function, Functions
7363 @section Functions That Control Make
7364 @cindex functions, for controlling make
7365 @cindex controlling make
7367 These functions control the way make runs. Generally, they are used to
7368 provide information to the user of the makefile or to cause make to stop
7369 if some sort of environmental error is detected.
7372 @item $(error @var{text}@dots{})
7374 @cindex error, stopping on
7375 @cindex stopping make
7376 Generates a fatal error where the message is @var{text}. Note that the
7377 error is generated whenever this function is evaluated. So, if you put
7378 it inside a command script or on the right side of a recursive variable
7379 assignment, it won't be evaluated until later. The @var{text} will be
7380 expanded before the error is generated.
7386 $(error error is $(ERROR1))
7391 will generate a fatal error during the read of the makefile if the
7392 @code{make} variable @code{ERROR1} is defined. Or,
7395 ERR = $(error found an error!)
7402 will generate a fatal error while @code{make} is running, if the
7403 @code{err} target is invoked.
7405 @item $(warning @var{text}@dots{})
7407 @cindex warnings, printing
7408 @cindex printing user warnings
7409 This function works similarly to the @code{error} function, above,
7410 except that @code{make} doesn't exit. Instead, @var{text} is expanded
7411 and the resulting message is displayed, but processing of the makefile
7414 The result of the expansion of this function is the empty string.
7416 @item $(info @var{text}@dots{})
7418 @cindex printing messages
7419 This function does nothing more than print its (expanded) argument(s)
7420 to standard output. No makefile name or line number is added. The
7421 result of the expansion of this function is the empty string.
7424 @node Running, Implicit Rules, Functions, Top
7425 @chapter How to Run @code{make}
7427 A makefile that says how to recompile a program can be used in more
7428 than one way. The simplest use is to recompile every file that is out
7429 of date. Usually, makefiles are written so that if you run
7430 @code{make} with no arguments, it does just that.
7432 But you might want to update only some of the files; you might want to use
7433 a different compiler or different compiler options; you might want just to
7434 find out which files are out of date without changing them.
7436 By giving arguments when you run @code{make}, you can do any of these
7437 things and many others.
7439 @cindex exit status of make
7440 The exit status of @code{make} is always one of three values:
7443 The exit status is zero if @code{make} is successful.
7445 The exit status is two if @code{make} encounters any errors.
7446 It will print messages describing the particular errors.
7448 The exit status is one if you use the @samp{-q} flag and @code{make}
7449 determines that some target is not already up to date.
7450 @xref{Instead of Execution, ,Instead of Executing the Commands}.
7454 * Makefile Arguments:: How to specify which makefile to use.
7455 * Goals:: How to use goal arguments to specify which
7456 parts of the makefile to use.
7457 * Instead of Execution:: How to use mode flags to specify what
7458 kind of thing to do with the commands
7459 in the makefile other than simply
7461 * Avoiding Compilation:: How to avoid recompiling certain files.
7462 * Overriding:: How to override a variable to specify
7463 an alternate compiler and other things.
7464 * Testing:: How to proceed past some errors, to
7466 * Options Summary:: Summary of Options
7469 @node Makefile Arguments, Goals, Running, Running
7470 @section Arguments to Specify the Makefile
7471 @cindex @code{--file}
7472 @cindex @code{--makefile}
7475 The way to specify the name of the makefile is with the @samp{-f} or
7476 @samp{--file} option (@samp{--makefile} also works). For example,
7477 @samp{-f altmake} says to use the file @file{altmake} as the makefile.
7479 If you use the @samp{-f} flag several times and follow each @samp{-f}
7480 with an argument, all the specified files are used jointly as
7483 If you do not use the @samp{-f} or @samp{--file} flag, the default is
7484 to try @file{GNUmakefile}, @file{makefile}, and @file{Makefile}, in
7485 that order, and use the first of these three which exists or can be made
7486 (@pxref{Makefiles, ,Writing Makefiles}).@refill
7488 @node Goals, Instead of Execution, Makefile Arguments, Running
7489 @section Arguments to Specify the Goals
7490 @cindex goal, how to specify
7492 The @dfn{goals} are the targets that @code{make} should strive ultimately
7493 to update. Other targets are updated as well if they appear as
7494 prerequisites of goals, or prerequisites of prerequisites of goals, etc.
7496 By default, the goal is the first target in the makefile (not counting
7497 targets that start with a period). Therefore, makefiles are usually
7498 written so that the first target is for compiling the entire program or
7499 programs they describe. If the first rule in the makefile has several
7500 targets, only the first target in the rule becomes the default goal, not
7501 the whole list. You can manage the selection of the default goal from
7502 within your makefile using the @code{.DEFAULT_GOAL} variable
7503 (@pxref{Special Variables, , Other Special Variables}).
7505 You can also specify a different goal or goals with command-line
7506 arguments to @code{make}. Use the name of the goal as an argument.
7507 If you specify several goals, @code{make} processes each of them in
7508 turn, in the order you name them.
7510 Any target in the makefile may be specified as a goal (unless it
7511 starts with @samp{-} or contains an @samp{=}, in which case it will be
7512 parsed as a switch or variable definition, respectively). Even
7513 targets not in the makefile may be specified, if @code{make} can find
7514 implicit rules that say how to make them.
7516 @vindex MAKECMDGOALS
7517 @code{Make} will set the special variable @code{MAKECMDGOALS} to the
7518 list of goals you specified on the command line. If no goals were given
7519 on the command line, this variable is empty. Note that this variable
7520 should be used only in special circumstances.
7522 An example of appropriate use is to avoid including @file{.d} files
7523 during @code{clean} rules (@pxref{Automatic Prerequisites}), so
7524 @code{make} won't create them only to immediately remove them
7529 sources = foo.c bar.c
7531 ifneq ($(MAKECMDGOALS),clean)
7532 include $(sources:.c=.d)
7537 One use of specifying a goal is if you want to compile only a part of
7538 the program, or only one of several programs. Specify as a goal each
7539 file that you wish to remake. For example, consider a directory containing
7540 several programs, with a makefile that starts like this:
7544 all: size nm ld ar as
7547 If you are working on the program @code{size}, you might want to say
7548 @w{@samp{make size}} so that only the files of that program are recompiled.
7550 Another use of specifying a goal is to make files that are not normally
7551 made. For example, there may be a file of debugging output, or a
7552 version of the program that is compiled specially for testing, which has
7553 a rule in the makefile but is not a prerequisite of the default goal.
7555 Another use of specifying a goal is to run the commands associated with
7556 a phony target (@pxref{Phony Targets}) or empty target (@pxref{Empty
7557 Targets, ,Empty Target Files to Record Events}). Many makefiles contain
7558 a phony target named @file{clean} which deletes everything except source
7559 files. Naturally, this is done only if you request it explicitly with
7560 @w{@samp{make clean}}. Following is a list of typical phony and empty
7561 target names. @xref{Standard Targets}, for a detailed list of all the
7562 standard target names which GNU software packages use.
7566 @cindex @code{all} @r{(standard target)}
7567 Make all the top-level targets the makefile knows about.
7570 @cindex @code{clean} @r{(standard target)}
7571 Delete all files that are normally created by running @code{make}.
7574 @cindex @code{mostlyclean} @r{(standard target)}
7575 Like @samp{clean}, but may refrain from deleting a few files that people
7576 normally don't want to recompile. For example, the @samp{mostlyclean}
7577 target for GCC does not delete @file{libgcc.a}, because recompiling it
7578 is rarely necessary and takes a lot of time.
7581 @cindex @code{distclean} @r{(standard target)}
7583 @cindex @code{realclean} @r{(standard target)}
7585 @cindex @code{clobber} @r{(standard target)}
7586 Any of these targets might be defined to delete @emph{more} files than
7587 @samp{clean} does. For example, this would delete configuration files
7588 or links that you would normally create as preparation for compilation,
7589 even if the makefile itself cannot create these files.
7592 @cindex @code{install} @r{(standard target)}
7593 Copy the executable file into a directory that users typically search
7594 for commands; copy any auxiliary files that the executable uses into
7595 the directories where it will look for them.
7598 @cindex @code{print} @r{(standard target)}
7599 Print listings of the source files that have changed.
7602 @cindex @code{tar} @r{(standard target)}
7603 Create a tar file of the source files.
7606 @cindex @code{shar} @r{(standard target)}
7607 Create a shell archive (shar file) of the source files.
7610 @cindex @code{dist} @r{(standard target)}
7611 Create a distribution file of the source files. This might
7612 be a tar file, or a shar file, or a compressed version of one of the
7613 above, or even more than one of the above.
7616 @cindex @code{TAGS} @r{(standard target)}
7617 Update a tags table for this program.
7620 @cindex @code{check} @r{(standard target)}
7622 @cindex @code{test} @r{(standard target)}
7623 Perform self tests on the program this makefile builds.
7626 @node Instead of Execution, Avoiding Compilation, Goals, Running
7627 @section Instead of Executing the Commands
7628 @cindex execution, instead of
7629 @cindex commands, instead of executing
7631 The makefile tells @code{make} how to tell whether a target is up to date,
7632 and how to update each target. But updating the targets is not always
7633 what you want. Certain options specify other activities for @code{make}.
7635 @comment Extra blank lines make it print better.
7641 @cindex @code{--just-print}
7642 @cindex @code{--dry-run}
7643 @cindex @code{--recon}
7646 ``No-op''. The activity is to print what commands would be used to make
7647 the targets up to date, but not actually execute them.
7651 @cindex @code{--touch}
7652 @cindex touching files
7653 @cindex target, touching
7656 ``Touch''. The activity is to mark the targets as up to date without
7657 actually changing them. In other words, @code{make} pretends to compile
7658 the targets but does not really change their contents.
7662 @cindex @code{--question}
7664 @cindex question mode
7666 ``Question''. The activity is to find out silently whether the targets
7667 are up to date already; but execute no commands in either case. In other
7668 words, neither compilation nor output will occur.
7671 @itemx --what-if=@var{file}
7672 @itemx --assume-new=@var{file}
7673 @itemx --new-file=@var{file}
7674 @cindex @code{--what-if}
7676 @cindex @code{--assume-new}
7677 @cindex @code{--new-file}
7679 @cindex files, assuming new
7681 ``What if''. Each @samp{-W} flag is followed by a file name. The given
7682 files' modification times are recorded by @code{make} as being the present
7683 time, although the actual modification times remain the same.
7684 You can use the @samp{-W} flag in conjunction with the @samp{-n} flag
7685 to see what would happen if you were to modify specific files.@refill
7688 With the @samp{-n} flag, @code{make} prints the commands that it would
7689 normally execute but does not execute them.
7691 With the @samp{-t} flag, @code{make} ignores the commands in the rules
7692 and uses (in effect) the command @code{touch} for each target that needs to
7693 be remade. The @code{touch} command is also printed, unless @samp{-s} or
7694 @code{.SILENT} is used. For speed, @code{make} does not actually invoke
7695 the program @code{touch}. It does the work directly.
7697 With the @samp{-q} flag, @code{make} prints nothing and executes no
7698 commands, but the exit status code it returns is zero if and only if the
7699 targets to be considered are already up to date. If the exit status is
7700 one, then some updating needs to be done. If @code{make} encounters an
7701 error, the exit status is two, so you can distinguish an error from a
7702 target that is not up to date.
7704 It is an error to use more than one of these three flags in the same
7705 invocation of @code{make}.
7707 @cindex +, and command execution
7708 The @samp{-n}, @samp{-t}, and @samp{-q} options do not affect command
7709 lines that begin with @samp{+} characters or contain the strings
7710 @samp{$(MAKE)} or @samp{$@{MAKE@}}. Note that only the line containing
7711 the @samp{+} character or the strings @samp{$(MAKE)} or @samp{$@{MAKE@}}
7712 is run regardless of these options. Other lines in the same rule are
7713 not run unless they too begin with @samp{+} or contain @samp{$(MAKE)} or
7714 @samp{$@{MAKE@}} (@xref{MAKE Variable, ,How the @code{MAKE} Variable Works}.)
7716 The @samp{-W} flag provides two features:
7720 If you also use the @samp{-n} or @samp{-q} flag, you can see what
7721 @code{make} would do if you were to modify some files.
7724 Without the @samp{-n} or @samp{-q} flag, when @code{make} is actually
7725 executing commands, the @samp{-W} flag can direct @code{make} to act
7726 as if some files had been modified, without actually modifying the
7730 Note that the options @samp{-p} and @samp{-v} allow you to obtain other
7731 information about @code{make} or about the makefiles in use
7732 (@pxref{Options Summary, ,Summary of Options}).@refill
7734 @node Avoiding Compilation, Overriding, Instead of Execution, Running
7735 @section Avoiding Recompilation of Some Files
7737 @cindex @code{--old-file}
7738 @cindex @code{--assume-old}
7739 @cindex files, assuming old
7740 @cindex files, avoiding recompilation of
7741 @cindex recompilation, avoiding
7743 Sometimes you may have changed a source file but you do not want to
7744 recompile all the files that depend on it. For example, suppose you add
7745 a macro or a declaration to a header file that many other files depend
7746 on. Being conservative, @code{make} assumes that any change in the
7747 header file requires recompilation of all dependent files, but you know
7748 that they do not need to be recompiled and you would rather not waste
7749 the time waiting for them to compile.
7751 If you anticipate the problem before changing the header file, you can
7752 use the @samp{-t} flag. This flag tells @code{make} not to run the
7753 commands in the rules, but rather to mark the target up to date by
7754 changing its last-modification date. You would follow this procedure:
7758 Use the command @samp{make} to recompile the source files that really
7759 need recompilation, ensuring that the object files are up-to-date
7763 Make the changes in the header files.
7766 Use the command @samp{make -t} to mark all the object files as
7767 up to date. The next time you run @code{make}, the changes in the
7768 header files will not cause any recompilation.
7771 If you have already changed the header file at a time when some files
7772 do need recompilation, it is too late to do this. Instead, you can
7773 use the @w{@samp{-o @var{file}}} flag, which marks a specified file as
7774 ``old'' (@pxref{Options Summary, ,Summary of Options}). This means
7775 that the file itself will not be remade, and nothing else will be
7776 remade on its account. Follow this procedure:
7780 Recompile the source files that need compilation for reasons independent
7781 of the particular header file, with @samp{make -o @var{headerfile}}.
7782 If several header files are involved, use a separate @samp{-o} option
7783 for each header file.
7786 Touch all the object files with @samp{make -t}.
7789 @node Overriding, Testing, Avoiding Compilation, Running
7790 @section Overriding Variables
7791 @cindex overriding variables with arguments
7792 @cindex variables, overriding with arguments
7793 @cindex command line variables
7794 @cindex variables, command line
7796 An argument that contains @samp{=} specifies the value of a variable:
7797 @samp{@var{v}=@var{x}} sets the value of the variable @var{v} to @var{x}.
7798 If you specify a value in this way, all ordinary assignments of the same
7799 variable in the makefile are ignored; we say they have been
7800 @dfn{overridden} by the command line argument.
7802 The most common way to use this facility is to pass extra flags to
7803 compilers. For example, in a properly written makefile, the variable
7804 @code{CFLAGS} is included in each command that runs the C compiler, so a
7805 file @file{foo.c} would be compiled something like this:
7808 cc -c $(CFLAGS) foo.c
7811 Thus, whatever value you set for @code{CFLAGS} affects each compilation
7812 that occurs. The makefile probably specifies the usual value for
7813 @code{CFLAGS}, like this:
7819 Each time you run @code{make}, you can override this value if you
7820 wish. For example, if you say @samp{make CFLAGS='-g -O'}, each C
7821 compilation will be done with @samp{cc -c -g -O}. (This also
7822 illustrates how you can use quoting in the shell to enclose spaces and
7823 other special characters in the value of a variable when you override
7826 The variable @code{CFLAGS} is only one of many standard variables that
7827 exist just so that you can change them this way. @xref{Implicit
7828 Variables, , Variables Used by Implicit Rules}, for a complete list.
7830 You can also program the makefile to look at additional variables of your
7831 own, giving the user the ability to control other aspects of how the
7832 makefile works by changing the variables.
7834 When you override a variable with a command argument, you can define either
7835 a recursively-expanded variable or a simply-expanded variable. The
7836 examples shown above make a recursively-expanded variable; to make a
7837 simply-expanded variable, write @samp{:=} instead of @samp{=}. But, unless
7838 you want to include a variable reference or function call in the
7839 @emph{value} that you specify, it makes no difference which kind of
7840 variable you create.
7842 There is one way that the makefile can change a variable that you have
7843 overridden. This is to use the @code{override} directive, which is a line
7844 that looks like this: @samp{override @var{variable} = @var{value}}
7845 (@pxref{Override Directive, ,The @code{override} Directive}).
7847 @node Testing, Options Summary, Overriding, Running
7848 @section Testing the Compilation of a Program
7849 @cindex testing compilation
7850 @cindex compilation, testing
7852 Normally, when an error happens in executing a shell command, @code{make}
7853 gives up immediately, returning a nonzero status. No further commands are
7854 executed for any target. The error implies that the goal cannot be
7855 correctly remade, and @code{make} reports this as soon as it knows.
7857 When you are compiling a program that you have just changed, this is not
7858 what you want. Instead, you would rather that @code{make} try compiling
7859 every file that can be tried, to show you as many compilation errors
7863 @cindex @code{--keep-going}
7864 On these occasions, you should use the @samp{-k} or
7865 @samp{--keep-going} flag. This tells @code{make} to continue to
7866 consider the other prerequisites of the pending targets, remaking them
7867 if necessary, before it gives up and returns nonzero status. For
7868 example, after an error in compiling one object file, @samp{make -k}
7869 will continue compiling other object files even though it already
7870 knows that linking them will be impossible. In addition to continuing
7871 after failed shell commands, @samp{make -k} will continue as much as
7872 possible after discovering that it does not know how to make a target
7873 or prerequisite file. This will always cause an error message, but
7874 without @samp{-k}, it is a fatal error (@pxref{Options Summary,
7875 ,Summary of Options}).@refill
7877 The usual behavior of @code{make} assumes that your purpose is to get the
7878 goals up to date; once @code{make} learns that this is impossible, it might
7879 as well report the failure immediately. The @samp{-k} flag says that the
7880 real purpose is to test as much as possible of the changes made in the
7881 program, perhaps to find several independent problems so that you can
7882 correct them all before the next attempt to compile. This is why Emacs'
7883 @kbd{M-x compile} command passes the @samp{-k} flag by default.
7885 @node Options Summary, , Testing, Running
7886 @section Summary of Options
7891 Here is a table of all the options @code{make} understands:
7898 These options are ignored for compatibility with other versions of @code{make}.
7902 @itemx --always-make
7903 @cindex @code{--always-make}
7904 Consider all targets out-of-date. GNU @code{make} proceeds to
7905 consider targets and their prerequisites using the normal algorithms;
7906 however, all targets so considered are always remade regardless of the
7907 status of their prerequisites. To avoid infinite recursion, if
7908 @code{MAKE_RESTARTS} (@pxref{Special Variables, , Other Special
7909 Variables}) is set to a number greater than 0 this option is disabled
7910 when considering whether to remake makefiles (@pxref{Remaking
7911 Makefiles, , How Makefiles Are Remade}).
7915 @itemx --directory=@var{dir}
7916 @cindex @code{--directory}
7917 Change to directory @var{dir} before reading the makefiles. If multiple
7918 @samp{-C} options are specified, each is interpreted relative to the
7919 previous one: @samp{-C / -C etc} is equivalent to @samp{-C /etc}.
7920 This is typically used with recursive invocations of @code{make}
7921 (@pxref{Recursion, ,Recursive Use of @code{make}}).
7925 @c Extra blank line here makes the table look better.
7927 Print debugging information in addition to normal processing. The
7928 debugging information says which files are being considered for
7929 remaking, which file-times are being compared and with what results,
7930 which files actually need to be remade, which implicit rules are
7931 considered and which are applied---everything interesting about how
7932 @code{make} decides what to do. The @code{-d} option is equivalent to
7933 @samp{--debug=a} (see below).
7935 @item --debug[=@var{options}]
7936 @cindex @code{--debug}
7937 @c Extra blank line here makes the table look better.
7939 Print debugging information in addition to normal processing. Various
7940 levels and types of output can be chosen. With no arguments, print the
7941 ``basic'' level of debugging. Possible arguments are below; only the
7942 first character is considered, and values must be comma- or
7947 All types of debugging output are enabled. This is equivalent to using
7951 Basic debugging prints each target that was found to be out-of-date, and
7952 whether the build was successful or not.
7954 @item v (@i{verbose})
7955 A level above @samp{basic}; includes messages about which makefiles were
7956 parsed, prerequisites that did not need to be rebuilt, etc. This option
7957 also enables @samp{basic} messages.
7959 @item i (@i{implicit})
7960 Prints messages describing the implicit rule searches for each target.
7961 This option also enables @samp{basic} messages.
7964 Prints messages giving details on the invocation of specific subcommands.
7966 @item m (@i{makefile})
7967 By default, the above messages are not enabled while trying to remake
7968 the makefiles. This option enables messages while rebuilding makefiles,
7969 too. Note that the @samp{all} option does enable this option. This
7970 option also enables @samp{basic} messages.
7975 @itemx --environment-overrides
7976 @cindex @code{--environment-overrides}
7977 Give variables taken from the environment precedence
7978 over variables from makefiles.
7979 @xref{Environment, ,Variables from the Environment}.
7983 @itemx --file=@var{file}
7984 @cindex @code{--file}
7985 @itemx --makefile=@var{file}
7986 @cindex @code{--makefile}
7987 Read the file named @var{file} as a makefile.
7988 @xref{Makefiles, ,Writing Makefiles}.
7993 @cindex @code{--help}
7994 @c Extra blank line here makes the table look better.
7996 Remind you of the options that @code{make} understands and then exit.
8000 @itemx --ignore-errors
8001 @cindex @code{--ignore-errors}
8002 Ignore all errors in commands executed to remake files.
8003 @xref{Errors, ,Errors in Commands}.
8007 @itemx --include-dir=@var{dir}
8008 @cindex @code{--include-dir}
8009 Specifies a directory @var{dir} to search for included makefiles.
8010 @xref{Include, ,Including Other Makefiles}. If several @samp{-I}
8011 options are used to specify several directories, the directories are
8012 searched in the order specified.
8014 @item -j [@var{jobs}]
8016 @itemx --jobs[=@var{jobs}]
8017 @cindex @code{--jobs}
8018 Specifies the number of jobs (commands) to run simultaneously. With no
8019 argument, @code{make} runs as many jobs simultaneously as possible. If
8020 there is more than one @samp{-j} option, the last one is effective.
8021 @xref{Parallel, ,Parallel Execution},
8022 for more information on how commands are run.
8023 Note that this option is ignored on MS-DOS.
8028 @cindex @code{--keep-going}
8029 Continue as much as possible after an error. While the target that
8030 failed, and those that depend on it, cannot be remade, the other
8031 prerequisites of these targets can be processed all the same.
8032 @xref{Testing, ,Testing the Compilation of a Program}.
8034 @item -l [@var{load}]
8036 @itemx --load-average[=@var{load}]
8037 @cindex @code{--load-average}
8038 @itemx --max-load[=@var{load}]
8039 @cindex @code{--max-load}
8040 Specifies that no new jobs (commands) should be started if there are
8041 other jobs running and the load average is at least @var{load} (a
8042 floating-point number). With no argument, removes a previous load
8043 limit. @xref{Parallel, ,Parallel Execution}.
8047 @itemx --check-symlink-times
8048 @cindex @code{--check-symlink-times}
8049 On systems that support symbolic links, this option causes @code{make}
8050 to consider the timestamps on any symbolic links in addition to the
8051 timestamp on the file referenced by those links. When this option is
8052 provided, the most recent timestamp among the file and the symbolic
8053 links is taken as the modification time for this target file.
8058 @cindex @code{--just-print}
8060 @cindex @code{--dry-run}
8062 @cindex @code{--recon}
8063 @c Extra blank line here makes the table look better.
8065 Print the commands that would be executed, but do not execute them.
8066 @xref{Instead of Execution, ,Instead of Executing the Commands}.
8070 @itemx --old-file=@var{file}
8071 @cindex @code{--old-file}
8072 @itemx --assume-old=@var{file}
8073 @cindex @code{--assume-old}
8074 Do not remake the file @var{file} even if it is older than its
8075 prerequisites, and do not remake anything on account of changes in
8076 @var{file}. Essentially the file is treated as very old and its rules
8077 are ignored. @xref{Avoiding Compilation, ,Avoiding Recompilation of
8082 @itemx --print-data-base
8083 @cindex @code{--print-data-base}
8084 @cindex data base of @code{make} rules
8085 @cindex predefined rules and variables, printing
8086 Print the data base (rules and variable values) that results from
8087 reading the makefiles; then execute as usual or as otherwise specified.
8088 This also prints the version information given by the @samp{-v} switch
8089 (see below). To print the data base without trying to remake any files,
8090 use @w{@samp{make -qp}}. To print the data base of predefined rules and
8091 variables, use @w{@samp{make -p -f /dev/null}}. The data base output
8092 contains filename and linenumber information for command and variable
8093 definitions, so it can be a useful debugging tool in complex environments.
8098 @cindex @code{--question}
8099 ``Question mode''. Do not run any commands, or print anything; just
8100 return an exit status that is zero if the specified targets are already
8101 up to date, one if any remaking is required, or two if an error is
8102 encountered. @xref{Instead of Execution, ,Instead of Executing the
8107 @itemx --no-builtin-rules
8108 @cindex @code{--no-builtin-rules}
8109 Eliminate use of the built-in implicit rules (@pxref{Implicit Rules,
8110 ,Using Implicit Rules}). You can still define your own by writing
8111 pattern rules (@pxref{Pattern Rules, ,Defining and Redefining Pattern
8112 Rules}). The @samp{-r} option also clears out the default list of
8113 suffixes for suffix rules (@pxref{Suffix Rules, ,Old-Fashioned Suffix
8114 Rules}). But you can still define your own suffixes with a rule for
8115 @code{.SUFFIXES}, and then define your own suffix rules. Note that only
8116 @emph{rules} are affected by the @code{-r} option; default variables
8117 remain in effect (@pxref{Implicit Variables, ,Variables Used by Implicit
8118 Rules}); see the @samp{-R} option below.
8122 @itemx --no-builtin-variables
8123 @cindex @code{--no-builtin-variables}
8124 Eliminate use of the built-in rule-specific variables (@pxref{Implicit
8125 Variables, ,Variables Used by Implicit Rules}). You can still define
8126 your own, of course. The @samp{-R} option also automatically enables
8127 the @samp{-r} option (see above), since it doesn't make sense to have
8128 implicit rules without any definitions for the variables that they use.
8133 @cindex @code{--silent}
8135 @cindex @code{--quiet}
8136 @c Extra blank line here makes the table look better.
8138 Silent operation; do not print the commands as they are executed.
8139 @xref{Echoing, ,Command Echoing}.
8143 @itemx --no-keep-going
8144 @cindex @code{--no-keep-going}
8146 @cindex @code{--stop}
8147 @c Extra blank line here makes the table look better.
8149 Cancel the effect of the @samp{-k} option. This is never necessary
8150 except in a recursive @code{make} where @samp{-k} might be inherited
8151 from the top-level @code{make} via @code{MAKEFLAGS}
8152 (@pxref{Recursion, ,Recursive Use of @code{make}})
8153 or if you set @samp{-k} in @code{MAKEFLAGS} in your environment.@refill
8158 @cindex @code{--touch}
8159 @c Extra blank line here makes the table look better.
8161 Touch files (mark them up to date without really changing them)
8162 instead of running their commands. This is used to pretend that the
8163 commands were done, in order to fool future invocations of
8164 @code{make}. @xref{Instead of Execution, ,Instead of Executing the Commands}.
8169 @cindex @code{--version}
8170 Print the version of the @code{make} program plus a copyright, a list
8171 of authors, and a notice that there is no warranty; then exit.
8175 @itemx --print-directory
8176 @cindex @code{--print-directory}
8177 Print a message containing the working directory both before and after
8178 executing the makefile. This may be useful for tracking down errors
8179 from complicated nests of recursive @code{make} commands.
8180 @xref{Recursion, ,Recursive Use of @code{make}}. (In practice, you
8181 rarely need to specify this option since @samp{make} does it for you;
8182 see @ref{-w Option, ,The @samp{--print-directory} Option}.)
8184 @itemx --no-print-directory
8185 @cindex @code{--no-print-directory}
8186 Disable printing of the working directory under @code{-w}.
8187 This option is useful when @code{-w} is turned on automatically,
8188 but you do not want to see the extra messages.
8189 @xref{-w Option, ,The @samp{--print-directory} Option}.
8193 @itemx --what-if=@var{file}
8194 @cindex @code{--what-if}
8195 @itemx --new-file=@var{file}
8196 @cindex @code{--new-file}
8197 @itemx --assume-new=@var{file}
8198 @cindex @code{--assume-new}
8199 Pretend that the target @var{file} has just been modified. When used
8200 with the @samp{-n} flag, this shows you what would happen if you were
8201 to modify that file. Without @samp{-n}, it is almost the same as
8202 running a @code{touch} command on the given file before running
8203 @code{make}, except that the modification time is changed only in the
8204 imagination of @code{make}.
8205 @xref{Instead of Execution, ,Instead of Executing the Commands}.
8207 @item --warn-undefined-variables
8208 @cindex @code{--warn-undefined-variables}
8209 @cindex variables, warning for undefined
8210 @cindex undefined variables, warning message
8211 Issue a warning message whenever @code{make} sees a reference to an
8212 undefined variable. This can be helpful when you are trying to debug
8213 makefiles which use variables in complex ways.
8216 @node Implicit Rules, Archives, Running, Top
8217 @chapter Using Implicit Rules
8218 @cindex implicit rule
8219 @cindex rule, implicit
8221 Certain standard ways of remaking target files are used very often. For
8222 example, one customary way to make an object file is from a C source file
8223 using the C compiler, @code{cc}.
8225 @dfn{Implicit rules} tell @code{make} how to use customary techniques so
8226 that you do not have to specify them in detail when you want to use
8227 them. For example, there is an implicit rule for C compilation. File
8228 names determine which implicit rules are run. For example, C
8229 compilation typically takes a @file{.c} file and makes a @file{.o} file.
8230 So @code{make} applies the implicit rule for C compilation when it sees
8231 this combination of file name endings.@refill
8233 A chain of implicit rules can apply in sequence; for example, @code{make}
8234 will remake a @file{.o} file from a @file{.y} file by way of a @file{.c} file.
8236 @xref{Chained Rules, ,Chains of Implicit Rules}.
8239 The built-in implicit rules use several variables in their commands so
8240 that, by changing the values of the variables, you can change the way the
8241 implicit rule works. For example, the variable @code{CFLAGS} controls the
8242 flags given to the C compiler by the implicit rule for C compilation.
8244 @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
8247 You can define your own implicit rules by writing @dfn{pattern rules}.
8249 @xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.
8252 @dfn{Suffix rules} are a more limited way to define implicit rules.
8253 Pattern rules are more general and clearer, but suffix rules are
8254 retained for compatibility.
8256 @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
8260 * Using Implicit:: How to use an existing implicit rule
8261 to get the commands for updating a file.
8262 * Catalogue of Rules:: A list of built-in implicit rules.
8263 * Implicit Variables:: How to change what predefined rules do.
8264 * Chained Rules:: How to use a chain of implicit rules.
8265 * Pattern Rules:: How to define new implicit rules.
8266 * Last Resort:: How to define commands for rules which
8268 * Suffix Rules:: The old-fashioned style of implicit rule.
8269 * Implicit Rule Search:: The precise algorithm for applying
8273 @node Using Implicit, Catalogue of Rules, Implicit Rules, Implicit Rules
8274 @section Using Implicit Rules
8275 @cindex implicit rule, how to use
8276 @cindex rule, implicit, how to use
8278 To allow @code{make} to find a customary method for updating a target file,
8279 all you have to do is refrain from specifying commands yourself. Either
8280 write a rule with no command lines, or don't write a rule at all. Then
8281 @code{make} will figure out which implicit rule to use based on which
8282 kind of source file exists or can be made.
8284 For example, suppose the makefile looks like this:
8288 cc -o foo foo.o bar.o $(CFLAGS) $(LDFLAGS)
8292 Because you mention @file{foo.o} but do not give a rule for it, @code{make}
8293 will automatically look for an implicit rule that tells how to update it.
8294 This happens whether or not the file @file{foo.o} currently exists.
8296 If an implicit rule is found, it can supply both commands and one or
8297 more prerequisites (the source files). You would want to write a rule
8298 for @file{foo.o} with no command lines if you need to specify additional
8299 prerequisites, such as header files, that the implicit rule cannot
8302 Each implicit rule has a target pattern and prerequisite patterns. There may
8303 be many implicit rules with the same target pattern. For example, numerous
8304 rules make @samp{.o} files: one, from a @samp{.c} file with the C compiler;
8305 another, from a @samp{.p} file with the Pascal compiler; and so on. The rule
8306 that actually applies is the one whose prerequisites exist or can be made.
8307 So, if you have a file @file{foo.c}, @code{make} will run the C compiler;
8308 otherwise, if you have a file @file{foo.p}, @code{make} will run the Pascal
8309 compiler; and so on.
8311 Of course, when you write the makefile, you know which implicit rule you
8312 want @code{make} to use, and you know it will choose that one because you
8313 know which possible prerequisite files are supposed to exist.
8314 @xref{Catalogue of Rules, ,Catalogue of Implicit Rules},
8315 for a catalogue of all the predefined implicit rules.
8317 Above, we said an implicit rule applies if the required prerequisites ``exist
8318 or can be made''. A file ``can be made'' if it is mentioned explicitly in
8319 the makefile as a target or a prerequisite, or if an implicit rule can be
8320 recursively found for how to make it. When an implicit prerequisite is the
8321 result of another implicit rule, we say that @dfn{chaining} is occurring.
8322 @xref{Chained Rules, ,Chains of Implicit Rules}.
8324 In general, @code{make} searches for an implicit rule for each target, and
8325 for each double-colon rule, that has no commands. A file that is mentioned
8326 only as a prerequisite is considered a target whose rule specifies nothing,
8327 so implicit rule search happens for it. @xref{Implicit Rule Search, ,Implicit Rule Search Algorithm}, for the
8328 details of how the search is done.
8330 Note that explicit prerequisites do not influence implicit rule search.
8331 For example, consider this explicit rule:
8338 The prerequisite on @file{foo.p} does not necessarily mean that
8339 @code{make} will remake @file{foo.o} according to the implicit rule to
8340 make an object file, a @file{.o} file, from a Pascal source file, a
8341 @file{.p} file. For example, if @file{foo.c} also exists, the implicit
8342 rule to make an object file from a C source file is used instead,
8343 because it appears before the Pascal rule in the list of predefined
8344 implicit rules (@pxref{Catalogue of Rules, , Catalogue of Implicit
8347 If you do not want an implicit rule to be used for a target that has no
8348 commands, you can give that target empty commands by writing a semicolon
8349 (@pxref{Empty Commands, ,Defining Empty Commands}).
8351 @node Catalogue of Rules, Implicit Variables, Using Implicit, Implicit Rules
8352 @section Catalogue of Implicit Rules
8353 @cindex implicit rule, predefined
8354 @cindex rule, implicit, predefined
8356 Here is a catalogue of predefined implicit rules which are always
8357 available unless the makefile explicitly overrides or cancels them.
8358 @xref{Canceling Rules, ,Canceling Implicit Rules}, for information on
8359 canceling or overriding an implicit rule. The @samp{-r} or
8360 @samp{--no-builtin-rules} option cancels all predefined rules.
8362 This manual only documents the default rules available on POSIX-based
8363 operating systems. Other operating systems, such as VMS, Windows,
8364 OS/2, etc. may have different sets of default rules. To see the full
8365 list of default rules and variables available in your version of GNU
8366 @code{make}, run @samp{make -p} in a directory with no makefile.
8368 Not all of these rules will always be defined, even when the @samp{-r}
8369 option is not given. Many of the predefined implicit rules are
8370 implemented in @code{make} as suffix rules, so which ones will be
8371 defined depends on the @dfn{suffix list} (the list of prerequisites of
8372 the special target @code{.SUFFIXES}). The default suffix list is:
8373 @code{.out}, @code{.a}, @code{.ln}, @code{.o}, @code{.c}, @code{.cc},
8374 @code{.C}, @code{.cpp}, @code{.p}, @code{.f}, @code{.F}, @code{.r}, @code{.y},
8375 @code{.l}, @code{.s}, @code{.S}, @code{.mod}, @code{.sym}, @code{.def},
8376 @code{.h}, @code{.info}, @code{.dvi}, @code{.tex}, @code{.texinfo},
8377 @code{.texi}, @code{.txinfo}, @code{.w}, @code{.ch} @code{.web},
8378 @code{.sh}, @code{.elc}, @code{.el}. All of the implicit rules
8379 described below whose prerequisites have one of these suffixes are
8380 actually suffix rules. If you modify the suffix list, the only
8381 predefined suffix rules in effect will be those named by one or two of
8382 the suffixes that are on the list you specify; rules whose suffixes fail
8383 to be on the list are disabled. @xref{Suffix Rules, ,Old-Fashioned
8384 Suffix Rules}, for full details on suffix rules.
8387 @item Compiling C programs
8388 @cindex C, rule to compile
8393 @file{@var{n}.o} is made automatically from @file{@var{n}.c} with
8394 a command of the form @samp{$(CC) -c $(CPPFLAGS) $(CFLAGS)}.@refill
8396 @item Compiling C++ programs
8397 @cindex C++, rule to compile
8402 @file{@var{n}.o} is made automatically from @file{@var{n}.cc},
8403 @file{@var{n}.cpp}, or @file{@var{n}.C} with a command of the form
8404 @samp{$(CXX) -c $(CPPFLAGS) $(CXXFLAGS)}. We encourage you to use the
8405 suffix @samp{.cc} for C++ source files instead of @samp{.C}.@refill
8407 @item Compiling Pascal programs
8408 @cindex Pascal, rule to compile
8411 @file{@var{n}.o} is made automatically from @file{@var{n}.p}
8412 with the command @samp{$(PC) -c $(PFLAGS)}.@refill
8414 @item Compiling Fortran and Ratfor programs
8415 @cindex Fortran, rule to compile
8416 @cindex Ratfor, rule to compile
8421 @file{@var{n}.o} is made automatically from @file{@var{n}.r},
8422 @file{@var{n}.F} or @file{@var{n}.f} by running the
8423 Fortran compiler. The precise command used is as follows:@refill
8427 @samp{$(FC) -c $(FFLAGS)}.
8429 @samp{$(FC) -c $(FFLAGS) $(CPPFLAGS)}.
8431 @samp{$(FC) -c $(FFLAGS) $(RFLAGS)}.
8434 @item Preprocessing Fortran and Ratfor programs
8435 @file{@var{n}.f} is made automatically from @file{@var{n}.r} or
8436 @file{@var{n}.F}. This rule runs just the preprocessor to convert a
8437 Ratfor or preprocessable Fortran program into a strict Fortran
8438 program. The precise command used is as follows:@refill
8442 @samp{$(FC) -F $(CPPFLAGS) $(FFLAGS)}.
8444 @samp{$(FC) -F $(FFLAGS) $(RFLAGS)}.
8447 @item Compiling Modula-2 programs
8448 @cindex Modula-2, rule to compile
8453 @file{@var{n}.sym} is made from @file{@var{n}.def} with a command
8454 of the form @samp{$(M2C) $(M2FLAGS) $(DEFFLAGS)}. @file{@var{n}.o}
8455 is made from @file{@var{n}.mod}; the form is:
8456 @w{@samp{$(M2C) $(M2FLAGS) $(MODFLAGS)}}.@refill
8459 @item Assembling and preprocessing assembler programs
8460 @cindex assembly, rule to compile
8463 @file{@var{n}.o} is made automatically from @file{@var{n}.s} by
8464 running the assembler, @code{as}. The precise command is
8465 @samp{$(AS) $(ASFLAGS)}.@refill
8468 @file{@var{n}.s} is made automatically from @file{@var{n}.S} by
8469 running the C preprocessor, @code{cpp}. The precise command is
8470 @w{@samp{$(CPP) $(CPPFLAGS)}}.
8472 @item Linking a single object file
8473 @cindex linking, predefined rule for
8476 @file{@var{n}} is made automatically from @file{@var{n}.o} by running
8477 the linker (usually called @code{ld}) via the C compiler. The precise
8478 command used is @w{@samp{$(CC) $(LDFLAGS) @var{n}.o $(LOADLIBES) $(LDLIBS)}}.
8480 This rule does the right thing for a simple program with only one
8481 source file. It will also do the right thing if there are multiple
8482 object files (presumably coming from various other source files), one
8483 of which has a name matching that of the executable file. Thus,
8490 when @file{x.c}, @file{y.c} and @file{z.c} all exist will execute:
8505 In more complicated cases, such as when there is no object file whose
8506 name derives from the executable file name, you must write an explicit
8507 command for linking.
8509 Each kind of file automatically made into @samp{.o} object files will
8510 be automatically linked by using the compiler (@samp{$(CC)},
8511 @samp{$(FC)} or @samp{$(PC)}; the C compiler @samp{$(CC)} is used to
8512 assemble @samp{.s} files) without the @samp{-c} option. This could be
8513 done by using the @samp{.o} object files as intermediates, but it is
8514 faster to do the compiling and linking in one step, so that's how it's
8517 @item Yacc for C programs
8519 @cindex Yacc, rule to run
8521 @file{@var{n}.c} is made automatically from @file{@var{n}.y} by
8522 running Yacc with the command @samp{$(YACC) $(YFLAGS)}.
8524 @item Lex for C programs
8526 @cindex Lex, rule to run
8528 @file{@var{n}.c} is made automatically from @file{@var{n}.l} by
8529 running Lex. The actual command is @samp{$(LEX) $(LFLAGS)}.
8531 @item Lex for Ratfor programs
8532 @file{@var{n}.r} is made automatically from @file{@var{n}.l} by
8533 running Lex. The actual command is @samp{$(LEX) $(LFLAGS)}.
8535 The convention of using the same suffix @samp{.l} for all Lex files
8536 regardless of whether they produce C code or Ratfor code makes it
8537 impossible for @code{make} to determine automatically which of the two
8538 languages you are using in any particular case. If @code{make} is
8539 called upon to remake an object file from a @samp{.l} file, it must
8540 guess which compiler to use. It will guess the C compiler, because
8541 that is more common. If you are using Ratfor, make sure @code{make}
8542 knows this by mentioning @file{@var{n}.r} in the makefile. Or, if you
8543 are using Ratfor exclusively, with no C files, remove @samp{.c} from
8544 the list of implicit rule suffixes with:@refill
8549 .SUFFIXES: .o .r .f .l @dots{}
8553 @item Making Lint Libraries from C, Yacc, or Lex programs
8555 @cindex @code{lint}, rule to run
8557 @file{@var{n}.ln} is made from @file{@var{n}.c} by running @code{lint}.
8558 The precise command is @w{@samp{$(LINT) $(LINTFLAGS) $(CPPFLAGS) -i}}.
8559 The same command is used on the C code produced from
8560 @file{@var{n}.y} or @file{@var{n}.l}.@refill
8562 @item @TeX{} and Web
8563 @cindex @TeX{}, rule to run
8564 @cindex Web, rule to run
8575 @file{@var{n}.dvi} is made from @file{@var{n}.tex} with the command
8576 @samp{$(TEX)}. @file{@var{n}.tex} is made from @file{@var{n}.web} with
8577 @samp{$(WEAVE)}, or from @file{@var{n}.w} (and from @file{@var{n}.ch} if
8578 it exists or can be made) with @samp{$(CWEAVE)}. @file{@var{n}.p} is
8579 made from @file{@var{n}.web} with @samp{$(TANGLE)} and @file{@var{n}.c}
8580 is made from @file{@var{n}.w} (and from @file{@var{n}.ch} if it exists
8581 or can be made) with @samp{$(CTANGLE)}.@refill
8583 @item Texinfo and Info
8584 @cindex Texinfo, rule to format
8585 @cindex Info, rule to format
8592 @file{@var{n}.dvi} is made from @file{@var{n}.texinfo},
8593 @file{@var{n}.texi}, or @file{@var{n}.txinfo}, with the command
8594 @w{@samp{$(TEXI2DVI) $(TEXI2DVI_FLAGS)}}. @file{@var{n}.info} is made from
8595 @file{@var{n}.texinfo}, @file{@var{n}.texi}, or @file{@var{n}.txinfo}, with
8596 the command @w{@samp{$(MAKEINFO) $(MAKEINFO_FLAGS)}}.
8599 @cindex RCS, rule to extract from
8601 @pindex ,v @r{(RCS file extension)}
8602 Any file @file{@var{n}} is extracted if necessary from an RCS file
8603 named either @file{@var{n},v} or @file{RCS/@var{n},v}. The precise
8604 command used is @w{@samp{$(CO) $(COFLAGS)}}. @file{@var{n}} will not be
8605 extracted from RCS if it already exists, even if the RCS file is
8606 newer. The rules for RCS are terminal
8607 (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}),
8608 so RCS files cannot be generated from another source; they must
8609 actually exist.@refill
8612 @cindex SCCS, rule to extract from
8614 @pindex s. @r{(SCCS file prefix)}
8615 Any file @file{@var{n}} is extracted if necessary from an SCCS file
8616 named either @file{s.@var{n}} or @file{SCCS/s.@var{n}}. The precise
8617 command used is @w{@samp{$(GET) $(GFLAGS)}}. The rules for SCCS are
8618 terminal (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}),
8619 so SCCS files cannot be generated from another source; they must
8620 actually exist.@refill
8623 For the benefit of SCCS, a file @file{@var{n}} is copied from
8624 @file{@var{n}.sh} and made executable (by everyone). This is for
8625 shell scripts that are checked into SCCS. Since RCS preserves the
8626 execution permission of a file, you do not need to use this feature
8629 We recommend that you avoid using of SCCS. RCS is widely held to be
8630 superior, and is also free. By choosing free software in place of
8631 comparable (or inferior) proprietary software, you support the free
8635 Usually, you want to change only the variables listed in the table
8636 above, which are documented in the following section.
8638 However, the commands in built-in implicit rules actually use
8639 variables such as @code{COMPILE.c}, @code{LINK.p}, and
8640 @code{PREPROCESS.S}, whose values contain the commands listed above.
8642 @code{make} follows the convention that the rule to compile a
8643 @file{.@var{x}} source file uses the variable @code{COMPILE.@var{x}}.
8644 Similarly, the rule to produce an executable from a @file{.@var{x}}
8645 file uses @code{LINK.@var{x}}; and the rule to preprocess a
8646 @file{.@var{x}} file uses @code{PREPROCESS.@var{x}}.
8648 @vindex OUTPUT_OPTION
8649 Every rule that produces an object file uses the variable
8650 @code{OUTPUT_OPTION}. @code{make} defines this variable either to
8651 contain @samp{-o $@@}, or to be empty, depending on a compile-time
8652 option. You need the @samp{-o} option to ensure that the output goes
8653 into the right file when the source file is in a different directory,
8654 as when using @code{VPATH} (@pxref{Directory Search}). However,
8655 compilers on some systems do not accept a @samp{-o} switch for object
8656 files. If you use such a system, and use @code{VPATH}, some
8657 compilations will put their output in the wrong place.
8658 A possible workaround for this problem is to give @code{OUTPUT_OPTION}
8659 the value @w{@samp{; mv $*.o $@@}}.
8661 @node Implicit Variables, Chained Rules, Catalogue of Rules, Implicit Rules
8662 @section Variables Used by Implicit Rules
8663 @cindex flags for compilers
8665 The commands in built-in implicit rules make liberal use of certain
8666 predefined variables. You can alter the values of these variables in
8667 the makefile, with arguments to @code{make}, or in the environment to
8668 alter how the implicit rules work without redefining the rules
8669 themselves. You can cancel all variables used by implicit rules with
8670 the @samp{-R} or @samp{--no-builtin-variables} option.
8672 For example, the command used to compile a C source file actually says
8673 @samp{$(CC) -c $(CFLAGS) $(CPPFLAGS)}. The default values of the variables
8674 used are @samp{cc} and nothing, resulting in the command @samp{cc -c}. By
8675 redefining @samp{CC} to @samp{ncc}, you could cause @samp{ncc} to be
8676 used for all C compilations performed by the implicit rule. By redefining
8677 @samp{CFLAGS} to be @samp{-g}, you could pass the @samp{-g} option to
8678 each compilation. @emph{All} implicit rules that do C compilation use
8679 @samp{$(CC)} to get the program name for the compiler and @emph{all}
8680 include @samp{$(CFLAGS)} among the arguments given to the compiler.@refill
8682 The variables used in implicit rules fall into two classes: those that are
8683 names of programs (like @code{CC}) and those that contain arguments for the
8684 programs (like @code{CFLAGS}). (The ``name of a program'' may also contain
8685 some command arguments, but it must start with an actual executable program
8686 name.) If a variable value contains more than one argument, separate them
8689 The following tables describe of some of the more commonly-used predefined
8690 variables. This list is not exhaustive, and the default values shown here may
8691 not be what are selected by @code{make} for your environment. To see the
8692 complete list of predefined variables for your instance of GNU @code{make} you
8693 can run @samp{make -p} in a directory with no makefiles.
8695 Here is a table of some of the more common variables used as names of
8696 programs in built-in rules:
8702 Archive-maintaining program; default @samp{ar}.
8707 Program for compiling assembly files; default @samp{as}.
8712 Program for compiling C programs; default @samp{cc}.
8717 Program for checking out files from RCS; default @samp{co}.
8722 Program for compiling C++ programs; default @samp{g++}.
8727 Program for extracting a file from RCS; default @samp{co}.
8732 Program for running the C preprocessor, with results to standard output;
8733 default @samp{$(CC) -E}.
8737 Program for compiling or preprocessing Fortran and Ratfor programs;
8743 Program for extracting a file from SCCS; default @samp{get}.
8748 Program to use to turn Lex grammars into source code; default @samp{lex}.
8753 Program to use to turn Yacc grammars into source code; default @samp{yacc}.
8758 Program to use to run lint on source code; default @samp{lint}.
8763 Program to use to compile Modula-2 source code; default @samp{m2c}.
8768 Program for compiling Pascal programs; default @samp{pc}.
8773 Program to convert a Texinfo source file into an Info file; default
8779 Program to make @TeX{} @sc{dvi} files from @TeX{} source;
8785 Program to make @TeX{} @sc{dvi} files from Texinfo source;
8786 default @samp{texi2dvi}.
8791 Program to translate Web into @TeX{}; default @samp{weave}.
8796 Program to translate C Web into @TeX{}; default @samp{cweave}.
8801 Program to translate Web into Pascal; default @samp{tangle}.
8806 Program to translate C Web into C; default @samp{ctangle}.
8811 Command to remove a file; default @samp{rm -f}.
8815 Here is a table of variables whose values are additional arguments for the
8816 programs above. The default values for all of these is the empty
8817 string, unless otherwise noted.
8822 Flags to give the archive-maintaining program; default @samp{rv}.
8826 Extra flags to give to the assembler (when explicitly
8827 invoked on a @samp{.s} or @samp{.S} file).
8831 Extra flags to give to the C compiler.
8835 Extra flags to give to the C++ compiler.
8839 Extra flags to give to the RCS @code{co} program.
8843 Extra flags to give to the C preprocessor and programs
8844 that use it (the C and Fortran compilers).
8848 Extra flags to give to the Fortran compiler.
8852 Extra flags to give to the SCCS @code{get} program.
8856 Extra flags to give to compilers when they are supposed to invoke the linker,
8861 Extra flags to give to Lex.
8865 Extra flags to give to Yacc.
8869 Extra flags to give to the Pascal compiler.
8873 Extra flags to give to the Fortran compiler for Ratfor programs.
8877 Extra flags to give to lint.
8880 @node Chained Rules, Pattern Rules, Implicit Variables, Implicit Rules
8881 @section Chains of Implicit Rules
8883 @cindex chains of rules
8884 @cindex rule, implicit, chains of
8885 Sometimes a file can be made by a sequence of implicit rules. For example,
8886 a file @file{@var{n}.o} could be made from @file{@var{n}.y} by running
8887 first Yacc and then @code{cc}. Such a sequence is called a @dfn{chain}.
8889 If the file @file{@var{n}.c} exists, or is mentioned in the makefile, no
8890 special searching is required: @code{make} finds that the object file can
8891 be made by C compilation from @file{@var{n}.c}; later on, when considering
8892 how to make @file{@var{n}.c}, the rule for running Yacc is
8893 used. Ultimately both @file{@var{n}.c} and @file{@var{n}.o} are
8896 @cindex intermediate files
8897 @cindex files, intermediate
8898 However, even if @file{@var{n}.c} does not exist and is not mentioned,
8899 @code{make} knows how to envision it as the missing link between
8900 @file{@var{n}.o} and @file{@var{n}.y}! In this case, @file{@var{n}.c} is
8901 called an @dfn{intermediate file}. Once @code{make} has decided to use the
8902 intermediate file, it is entered in the data base as if it had been
8903 mentioned in the makefile, along with the implicit rule that says how to
8906 Intermediate files are remade using their rules just like all other
8907 files. But intermediate files are treated differently in two ways.
8909 The first difference is what happens if the intermediate file does not
8910 exist. If an ordinary file @var{b} does not exist, and @code{make}
8911 considers a target that depends on @var{b}, it invariably creates
8912 @var{b} and then updates the target from @var{b}. But if @var{b} is an
8913 intermediate file, then @code{make} can leave well enough alone. It
8914 won't bother updating @var{b}, or the ultimate target, unless some
8915 prerequisite of @var{b} is newer than that target or there is some other
8916 reason to update that target.
8918 The second difference is that if @code{make} @emph{does} create @var{b}
8919 in order to update something else, it deletes @var{b} later on after it
8920 is no longer needed. Therefore, an intermediate file which did not
8921 exist before @code{make} also does not exist after @code{make}.
8922 @code{make} reports the deletion to you by printing a @samp{rm -f}
8923 command showing which file it is deleting.
8925 Ordinarily, a file cannot be intermediate if it is mentioned in the
8926 makefile as a target or prerequisite. However, you can explicitly mark a
8927 file as intermediate by listing it as a prerequisite of the special target
8928 @code{.INTERMEDIATE}. This takes effect even if the file is mentioned
8929 explicitly in some other way.
8931 @cindex intermediate files, preserving
8932 @cindex preserving intermediate files
8933 @cindex secondary files
8934 You can prevent automatic deletion of an intermediate file by marking it
8935 as a @dfn{secondary} file. To do this, list it as a prerequisite of the
8936 special target @code{.SECONDARY}. When a file is secondary, @code{make}
8937 will not create the file merely because it does not already exist, but
8938 @code{make} does not automatically delete the file. Marking a file as
8939 secondary also marks it as intermediate.
8941 You can list the target pattern of an implicit rule (such as @samp{%.o})
8942 as a prerequisite of the special target @code{.PRECIOUS} to preserve
8943 intermediate files made by implicit rules whose target patterns match
8944 that file's name; see @ref{Interrupts}.@refill
8945 @cindex preserving with @code{.PRECIOUS}
8946 @cindex @code{.PRECIOUS} intermediate files
8948 A chain can involve more than two implicit rules. For example, it is
8949 possible to make a file @file{foo} from @file{RCS/foo.y,v} by running RCS,
8950 Yacc and @code{cc}. Then both @file{foo.y} and @file{foo.c} are
8951 intermediate files that are deleted at the end.@refill
8953 No single implicit rule can appear more than once in a chain. This means
8954 that @code{make} will not even consider such a ridiculous thing as making
8955 @file{foo} from @file{foo.o.o} by running the linker twice. This
8956 constraint has the added benefit of preventing any infinite loop in the
8957 search for an implicit rule chain.
8959 There are some special implicit rules to optimize certain cases that would
8960 otherwise be handled by rule chains. For example, making @file{foo} from
8961 @file{foo.c} could be handled by compiling and linking with separate
8962 chained rules, using @file{foo.o} as an intermediate file. But what
8963 actually happens is that a special rule for this case does the compilation
8964 and linking with a single @code{cc} command. The optimized rule is used in
8965 preference to the step-by-step chain because it comes earlier in the
8968 @node Pattern Rules, Last Resort, Chained Rules, Implicit Rules
8969 @section Defining and Redefining Pattern Rules
8971 You define an implicit rule by writing a @dfn{pattern rule}. A pattern
8972 rule looks like an ordinary rule, except that its target contains the
8973 character @samp{%} (exactly one of them). The target is considered a
8974 pattern for matching file names; the @samp{%} can match any nonempty
8975 substring, while other characters match only themselves. The prerequisites
8976 likewise use @samp{%} to show how their names relate to the target name.
8978 Thus, a pattern rule @samp{%.o : %.c} says how to make any file
8979 @file{@var{stem}.o} from another file @file{@var{stem}.c}.@refill
8981 Note that expansion using @samp{%} in pattern rules occurs
8982 @strong{after} any variable or function expansions, which take place
8983 when the makefile is read. @xref{Using Variables, , How to Use
8984 Variables}, and @ref{Functions, ,Functions for Transforming Text}.
8987 * Pattern Intro:: An introduction to pattern rules.
8988 * Pattern Examples:: Examples of pattern rules.
8989 * Automatic Variables:: How to use automatic variables in the
8990 commands of implicit rules.
8991 * Pattern Match:: How patterns match.
8992 * Match-Anything Rules:: Precautions you should take prior to
8993 defining rules that can match any
8994 target file whatever.
8995 * Canceling Rules:: How to override or cancel built-in rules.
8998 @node Pattern Intro, Pattern Examples, Pattern Rules, Pattern Rules
8999 @subsection Introduction to Pattern Rules
9000 @cindex pattern rule
9001 @cindex rule, pattern
9003 A pattern rule contains the character @samp{%} (exactly one of them)
9004 in the target; otherwise, it looks exactly like an ordinary rule. The
9005 target is a pattern for matching file names; the @samp{%} matches any
9006 nonempty substring, while other characters match only themselves.
9007 @cindex target pattern, implicit
9008 @cindex @code{%}, in pattern rules
9010 For example, @samp{%.c} as a pattern matches any file name that ends in
9011 @samp{.c}. @samp{s.%.c} as a pattern matches any file name that starts
9012 with @samp{s.}, ends in @samp{.c} and is at least five characters long.
9013 (There must be at least one character to match the @samp{%}.) The substring
9014 that the @samp{%} matches is called the @dfn{stem}.@refill
9016 @samp{%} in a prerequisite of a pattern rule stands for the same stem
9017 that was matched by the @samp{%} in the target. In order for the
9018 pattern rule to apply, its target pattern must match the file name
9019 under consideration and all of its prerequisites (after pattern
9020 substitution) must name files that exist or can be made. These files
9021 become prerequisites of the target.
9022 @cindex prerequisite pattern, implicit
9024 Thus, a rule of the form
9027 %.o : %.c ; @var{command}@dots{}
9031 specifies how to make a file @file{@var{n}.o}, with another file
9032 @file{@var{n}.c} as its prerequisite, provided that @file{@var{n}.c}
9033 exists or can be made.
9035 There may also be prerequisites that do not use @samp{%}; such a prerequisite
9036 attaches to every file made by this pattern rule. These unvarying
9037 prerequisites are useful occasionally.
9039 A pattern rule need not have any prerequisites that contain @samp{%}, or
9040 in fact any prerequisites at all. Such a rule is effectively a general
9041 wildcard. It provides a way to make any file that matches the target
9042 pattern. @xref{Last Resort}.
9044 @c !!! The end of of this paragraph should be rewritten. --bob
9045 Pattern rules may have more than one target. Unlike normal rules, this
9046 does not act as many different rules with the same prerequisites and
9047 commands. If a pattern rule has multiple targets, @code{make} knows that
9048 the rule's commands are responsible for making all of the targets. The
9049 commands are executed only once to make all the targets. When searching
9050 for a pattern rule to match a target, the target patterns of a rule other
9051 than the one that matches the target in need of a rule are incidental:
9052 @code{make} worries only about giving commands and prerequisites to the file
9053 presently in question. However, when this file's commands are run, the
9054 other targets are marked as having been updated themselves.
9055 @cindex multiple targets, in pattern rule
9056 @cindex target, multiple in pattern rule
9058 The order in which pattern rules appear in the makefile is important
9059 since this is the order in which they are considered.
9060 Of equally applicable
9061 rules, only the first one found is used. The rules you write take precedence
9062 over those that are built in. Note however, that a rule whose
9063 prerequisites actually exist or are mentioned always takes priority over a
9064 rule with prerequisites that must be made by chaining other implicit rules.
9065 @cindex pattern rules, order of
9066 @cindex order of pattern rules
9068 @node Pattern Examples, Automatic Variables, Pattern Intro, Pattern Rules
9069 @subsection Pattern Rule Examples
9071 Here are some examples of pattern rules actually predefined in
9072 @code{make}. First, the rule that compiles @samp{.c} files into @samp{.o}
9077 $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@@
9081 defines a rule that can make any file @file{@var{x}.o} from
9082 @file{@var{x}.c}. The command uses the automatic variables @samp{$@@} and
9083 @samp{$<} to substitute the names of the target file and the source file
9084 in each case where the rule applies (@pxref{Automatic Variables}).@refill
9086 Here is a second built-in rule:
9094 defines a rule that can make any file @file{@var{x}} whatsoever from a
9095 corresponding file @file{@var{x},v} in the subdirectory @file{RCS}. Since
9096 the target is @samp{%}, this rule will apply to any file whatever, provided
9097 the appropriate prerequisite file exists. The double colon makes the rule
9098 @dfn{terminal}, which means that its prerequisite may not be an intermediate
9099 file (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}).@refill
9102 This pattern rule has two targets:
9106 %.tab.c %.tab.h: %.y
9112 @c The following paragraph is rewritten to avoid overfull hboxes
9113 This tells @code{make} that the command @samp{bison -d @var{x}.y} will
9114 make both @file{@var{x}.tab.c} and @file{@var{x}.tab.h}. If the file
9115 @file{foo} depends on the files @file{parse.tab.o} and @file{scan.o}
9116 and the file @file{scan.o} depends on the file @file{parse.tab.h},
9117 when @file{parse.y} is changed, the command @samp{bison -d parse.y}
9118 will be executed only once, and the prerequisites of both
9119 @file{parse.tab.o} and @file{scan.o} will be satisfied. (Presumably
9120 the file @file{parse.tab.o} will be recompiled from @file{parse.tab.c}
9121 and the file @file{scan.o} from @file{scan.c}, while @file{foo} is
9122 linked from @file{parse.tab.o}, @file{scan.o}, and its other
9123 prerequisites, and it will execute happily ever after.)@refill
9125 @node Automatic Variables, Pattern Match, Pattern Examples, Pattern Rules
9126 @subsection Automatic Variables
9127 @cindex automatic variables
9128 @cindex variables, automatic
9129 @cindex variables, and implicit rule
9131 Suppose you are writing a pattern rule to compile a @samp{.c} file into a
9132 @samp{.o} file: how do you write the @samp{cc} command so that it operates
9133 on the right source file name? You cannot write the name in the command,
9134 because the name is different each time the implicit rule is applied.
9136 What you do is use a special feature of @code{make}, the @dfn{automatic
9137 variables}. These variables have values computed afresh for each rule that
9138 is executed, based on the target and prerequisites of the rule. In this
9139 example, you would use @samp{$@@} for the object file name and @samp{$<}
9140 for the source file name.
9142 @cindex automatic variables in prerequisites
9143 @cindex prerequisites, and automatic variables
9144 It's very important that you recognize the limited scope in which
9145 automatic variable values are available: they only have values within
9146 the command script. In particular, you cannot use them anywhere
9147 within the target list of a rule; they have no value there and will
9148 expand to the empty string. Also, they cannot be accessed directly
9149 within the prerequisite list of a rule. A common mistake is
9150 attempting to use @code{$@@} within the prerequisites list; this will
9151 not work. However, there is a special feature of GNU @code{make},
9152 secondary expansion (@pxref{Secondary Expansion}), which will allow
9153 automatic variable values to be used in prerequisite lists.
9155 Here is a table of automatic variables:
9159 @vindex @@ @r{(automatic variable)}
9161 The file name of the target of the rule. If the target is an archive
9162 member, then @samp{$@@} is the name of the archive file. In a pattern
9163 rule that has multiple targets (@pxref{Pattern Intro, ,Introduction to
9164 Pattern Rules}), @samp{$@@} is the name of whichever target caused the
9165 rule's commands to be run.
9168 @vindex % @r{(automatic variable)}
9170 The target member name, when the target is an archive member.
9171 @xref{Archives}. For example, if the target is @file{foo.a(bar.o)} then
9172 @samp{$%} is @file{bar.o} and @samp{$@@} is @file{foo.a}. @samp{$%} is
9173 empty when the target is not an archive member.
9176 @vindex < @r{(automatic variable)}
9178 The name of the first prerequisite. If the target got its commands from
9179 an implicit rule, this will be the first prerequisite added by the
9180 implicit rule (@pxref{Implicit Rules}).
9183 @vindex ? @r{(automatic variable)}
9185 The names of all the prerequisites that are newer than the target, with
9186 spaces between them. For prerequisites which are archive members, only
9187 the member named is used (@pxref{Archives}).
9188 @cindex prerequisites, list of changed
9189 @cindex list of changed prerequisites
9192 @vindex ^ @r{(automatic variable)}
9194 The names of all the prerequisites, with spaces between them. For
9195 prerequisites which are archive members, only the member named is used
9196 (@pxref{Archives}). A target has only one prerequisite on each other file
9197 it depends on, no matter how many times each file is listed as a
9198 prerequisite. So if you list a prerequisite more than once for a target,
9199 the value of @code{$^} contains just one copy of the name. This list
9200 does @strong{not} contain any of the order-only prerequisites; for those
9201 see the @samp{$|} variable, below.
9202 @cindex prerequisites, list of all
9203 @cindex list of all prerequisites
9206 @vindex + @r{(automatic variable)}
9208 This is like @samp{$^}, but prerequisites listed more than once are
9209 duplicated in the order they were listed in the makefile. This is
9210 primarily useful for use in linking commands where it is meaningful to
9211 repeat library file names in a particular order.
9214 @vindex | @r{(automatic variable)}
9216 The names of all the order-only prerequisites, with spaces between
9220 @vindex * @r{(automatic variable)}
9222 The stem with which an implicit rule matches (@pxref{Pattern Match, ,How
9223 Patterns Match}). If the target is @file{dir/a.foo.b} and the target
9224 pattern is @file{a.%.b} then the stem is @file{dir/foo}. The stem is
9225 useful for constructing names of related files.@refill
9226 @cindex stem, variable for
9228 In a static pattern rule, the stem is part of the file name that matched
9229 the @samp{%} in the target pattern.
9231 In an explicit rule, there is no stem; so @samp{$*} cannot be determined
9232 in that way. Instead, if the target name ends with a recognized suffix
9233 (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}), @samp{$*} is set to
9234 the target name minus the suffix. For example, if the target name is
9235 @samp{foo.c}, then @samp{$*} is set to @samp{foo}, since @samp{.c} is a
9236 suffix. GNU @code{make} does this bizarre thing only for compatibility
9237 with other implementations of @code{make}. You should generally avoid
9238 using @samp{$*} except in implicit rules or static pattern rules.@refill
9240 If the target name in an explicit rule does not end with a recognized
9241 suffix, @samp{$*} is set to the empty string for that rule.
9244 @samp{$?} is useful even in explicit rules when you wish to operate on only
9245 the prerequisites that have changed. For example, suppose that an archive
9246 named @file{lib} is supposed to contain copies of several object files.
9247 This rule copies just the changed object files into the archive:
9251 lib: foo.o bar.o lose.o win.o
9256 Of the variables listed above, four have values that are single file
9257 names, and three have values that are lists of file names. These seven
9258 have variants that get just the file's directory name or just the file
9259 name within the directory. The variant variables' names are formed by
9260 appending @samp{D} or @samp{F}, respectively. These variants are
9261 semi-obsolete in GNU @code{make} since the functions @code{dir} and
9262 @code{notdir} can be used to get a similar effect (@pxref{File Name
9263 Functions, , Functions for File Names}). Note, however, that the
9264 @samp{D} variants all omit the trailing slash which always appears in
9265 the output of the @code{dir} function. Here is a table of the variants:
9269 @vindex @@D @r{(automatic variable)}
9271 The directory part of the file name of the target, with the trailing
9272 slash removed. If the value of @samp{$@@} is @file{dir/foo.o} then
9273 @samp{$(@@D)} is @file{dir}. This value is @file{.} if @samp{$@@} does
9274 not contain a slash.
9277 @vindex @@F @r{(automatic variable)}
9279 The file-within-directory part of the file name of the target. If the
9280 value of @samp{$@@} is @file{dir/foo.o} then @samp{$(@@F)} is
9281 @file{foo.o}. @samp{$(@@F)} is equivalent to @samp{$(notdir $@@)}.
9284 @vindex *D @r{(automatic variable)}
9287 @vindex *F @r{(automatic variable)}
9289 The directory part and the file-within-directory
9290 part of the stem; @file{dir} and @file{foo} in this example.
9293 @vindex %D @r{(automatic variable)}
9296 @vindex %F @r{(automatic variable)}
9298 The directory part and the file-within-directory part of the target
9299 archive member name. This makes sense only for archive member targets
9300 of the form @file{@var{archive}(@var{member})} and is useful only when
9301 @var{member} may contain a directory name. (@xref{Archive Members,
9302 ,Archive Members as Targets}.)
9305 @vindex <D @r{(automatic variable)}
9308 @vindex <F @r{(automatic variable)}
9310 The directory part and the file-within-directory
9311 part of the first prerequisite.
9314 @vindex ^D @r{(automatic variable)}
9317 @vindex ^F @r{(automatic variable)}
9319 Lists of the directory parts and the file-within-directory
9320 parts of all prerequisites.
9323 @vindex +D @r{(automatic variable)}
9326 @vindex +F @r{(automatic variable)}
9328 Lists of the directory parts and the file-within-directory
9329 parts of all prerequisites, including multiple instances of duplicated
9333 @vindex ?D @r{(automatic variable)}
9336 @vindex ?F @r{(automatic variable)}
9338 Lists of the directory parts and the file-within-directory parts of
9339 all prerequisites that are newer than the target.
9342 Note that we use a special stylistic convention when we talk about these
9343 automatic variables; we write ``the value of @samp{$<}'', rather than
9344 @w{``the variable @code{<}''} as we would write for ordinary variables
9345 such as @code{objects} and @code{CFLAGS}. We think this convention
9346 looks more natural in this special case. Please do not assume it has a
9347 deep significance; @samp{$<} refers to the variable named @code{<} just
9348 as @samp{$(CFLAGS)} refers to the variable named @code{CFLAGS}.
9349 You could just as well use @samp{$(<)} in place of @samp{$<}.
9351 @node Pattern Match, Match-Anything Rules, Automatic Variables, Pattern Rules
9352 @subsection How Patterns Match
9355 A target pattern is composed of a @samp{%} between a prefix and a suffix,
9356 either or both of which may be empty. The pattern matches a file name only
9357 if the file name starts with the prefix and ends with the suffix, without
9358 overlap. The text between the prefix and the suffix is called the
9359 @dfn{stem}. Thus, when the pattern @samp{%.o} matches the file name
9360 @file{test.o}, the stem is @samp{test}. The pattern rule prerequisites are
9361 turned into actual file names by substituting the stem for the character
9362 @samp{%}. Thus, if in the same example one of the prerequisites is written
9363 as @samp{%.c}, it expands to @samp{test.c}.@refill
9365 When the target pattern does not contain a slash (and it usually does
9366 not), directory names in the file names are removed from the file name
9367 before it is compared with the target prefix and suffix. After the
9368 comparison of the file name to the target pattern, the directory
9369 names, along with the slash that ends them, are added on to the
9370 prerequisite file names generated from the pattern rule's prerequisite
9371 patterns and the file name. The directories are ignored only for the
9372 purpose of finding an implicit rule to use, not in the application of
9373 that rule. Thus, @samp{e%t} matches the file name @file{src/eat},
9374 with @samp{src/a} as the stem. When prerequisites are turned into file
9375 names, the directories from the stem are added at the front, while the
9376 rest of the stem is substituted for the @samp{%}. The stem
9377 @samp{src/a} with a prerequisite pattern @samp{c%r} gives the file name
9378 @file{src/car}.@refill
9380 @node Match-Anything Rules, Canceling Rules, Pattern Match, Pattern Rules
9381 @subsection Match-Anything Pattern Rules
9383 @cindex match-anything rule
9384 @cindex terminal rule
9385 When a pattern rule's target is just @samp{%}, it matches any file name
9386 whatever. We call these rules @dfn{match-anything} rules. They are very
9387 useful, but it can take a lot of time for @code{make} to think about them,
9388 because it must consider every such rule for each file name listed either
9389 as a target or as a prerequisite.
9391 Suppose the makefile mentions @file{foo.c}. For this target, @code{make}
9392 would have to consider making it by linking an object file @file{foo.c.o},
9393 or by C compilation-and-linking in one step from @file{foo.c.c}, or by
9394 Pascal compilation-and-linking from @file{foo.c.p}, and many other
9397 We know these possibilities are ridiculous since @file{foo.c} is a C source
9398 file, not an executable. If @code{make} did consider these possibilities,
9399 it would ultimately reject them, because files such as @file{foo.c.o} and
9400 @file{foo.c.p} would not exist. But these possibilities are so
9401 numerous that @code{make} would run very slowly if it had to consider
9404 To gain speed, we have put various constraints on the way @code{make}
9405 considers match-anything rules. There are two different constraints that
9406 can be applied, and each time you define a match-anything rule you must
9407 choose one or the other for that rule.
9409 One choice is to mark the match-anything rule as @dfn{terminal} by defining
9410 it with a double colon. When a rule is terminal, it does not apply unless
9411 its prerequisites actually exist. Prerequisites that could be made with
9412 other implicit rules are not good enough. In other words, no further
9413 chaining is allowed beyond a terminal rule.
9415 For example, the built-in implicit rules for extracting sources from RCS
9416 and SCCS files are terminal; as a result, if the file @file{foo.c,v} does
9417 not exist, @code{make} will not even consider trying to make it as an
9418 intermediate file from @file{foo.c,v.o} or from @file{RCS/SCCS/s.foo.c,v}.
9419 RCS and SCCS files are generally ultimate source files, which should not be
9420 remade from any other files; therefore, @code{make} can save time by not
9421 looking for ways to remake them.@refill
9423 If you do not mark the match-anything rule as terminal, then it is
9424 nonterminal. A nonterminal match-anything rule cannot apply to a file name
9425 that indicates a specific type of data. A file name indicates a specific
9426 type of data if some non-match-anything implicit rule target matches it.
9428 For example, the file name @file{foo.c} matches the target for the pattern
9429 rule @samp{%.c : %.y} (the rule to run Yacc). Regardless of whether this
9430 rule is actually applicable (which happens only if there is a file
9431 @file{foo.y}), the fact that its target matches is enough to prevent
9432 consideration of any nonterminal match-anything rules for the file
9433 @file{foo.c}. Thus, @code{make} will not even consider trying to make
9434 @file{foo.c} as an executable file from @file{foo.c.o}, @file{foo.c.c},
9435 @file{foo.c.p}, etc.@refill
9437 The motivation for this constraint is that nonterminal match-anything
9438 rules are used for making files containing specific types of data (such as
9439 executable files) and a file name with a recognized suffix indicates some
9440 other specific type of data (such as a C source file).
9442 Special built-in dummy pattern rules are provided solely to recognize
9443 certain file names so that nonterminal match-anything rules will not be
9444 considered. These dummy rules have no prerequisites and no commands, and
9445 they are ignored for all other purposes. For example, the built-in
9453 exists to make sure that Pascal source files such as @file{foo.p} match a
9454 specific target pattern and thereby prevent time from being wasted looking
9455 for @file{foo.p.o} or @file{foo.p.c}.
9457 Dummy pattern rules such as the one for @samp{%.p} are made for every
9458 suffix listed as valid for use in suffix rules (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}).
9460 @node Canceling Rules, , Match-Anything Rules, Pattern Rules
9461 @subsection Canceling Implicit Rules
9463 You can override a built-in implicit rule (or one you have defined
9464 yourself) by defining a new pattern rule with the same target and
9465 prerequisites, but different commands. When the new rule is defined, the
9466 built-in one is replaced. The new rule's position in the sequence of
9467 implicit rules is determined by where you write the new rule.
9469 You can cancel a built-in implicit rule by defining a pattern rule with the
9470 same target and prerequisites, but no commands. For example, the following
9471 would cancel the rule that runs the assembler:
9477 @node Last Resort, Suffix Rules, Pattern Rules, Implicit Rules
9478 @section Defining Last-Resort Default Rules
9479 @cindex last-resort default rules
9480 @cindex default rules, last-resort
9482 You can define a last-resort implicit rule by writing a terminal
9483 match-anything pattern rule with no prerequisites (@pxref{Match-Anything
9484 Rules}). This is just like any other pattern rule; the only thing
9485 special about it is that it will match any target. So such a rule's
9486 commands are used for all targets and prerequisites that have no commands
9487 of their own and for which no other implicit rule applies.
9489 For example, when testing a makefile, you might not care if the source
9490 files contain real data, only that they exist. Then you might do this:
9498 to cause all the source files needed (as prerequisites) to be created
9502 You can instead define commands to be used for targets for which there
9503 are no rules at all, even ones which don't specify commands. You do
9504 this by writing a rule for the target @code{.DEFAULT}. Such a rule's
9505 commands are used for all prerequisites which do not appear as targets in
9506 any explicit rule, and for which no implicit rule applies. Naturally,
9507 there is no @code{.DEFAULT} rule unless you write one.
9509 If you use @code{.DEFAULT} with no commands or prerequisites:
9516 the commands previously stored for @code{.DEFAULT} are cleared.
9517 Then @code{make} acts as if you had never defined @code{.DEFAULT} at all.
9519 If you do not want a target to get the commands from a match-anything
9520 pattern rule or @code{.DEFAULT}, but you also do not want any commands
9521 to be run for the target, you can give it empty commands (@pxref{Empty
9522 Commands, ,Defining Empty Commands}).@refill
9524 You can use a last-resort rule to override part of another makefile.
9525 @xref{Overriding Makefiles, , Overriding Part of Another Makefile}.
9527 @node Suffix Rules, Implicit Rule Search, Last Resort, Implicit Rules
9528 @section Old-Fashioned Suffix Rules
9529 @cindex old-fashioned suffix rules
9532 @dfn{Suffix rules} are the old-fashioned way of defining implicit rules for
9533 @code{make}. Suffix rules are obsolete because pattern rules are more
9534 general and clearer. They are supported in GNU @code{make} for
9535 compatibility with old makefiles. They come in two kinds:
9536 @dfn{double-suffix} and @dfn{single-suffix}.@refill
9538 A double-suffix rule is defined by a pair of suffixes: the target suffix
9539 and the source suffix. It matches any file whose name ends with the
9540 target suffix. The corresponding implicit prerequisite is made by
9541 replacing the target suffix with the source suffix in the file name. A
9542 two-suffix rule whose target and source suffixes are @samp{.o} and
9543 @samp{.c} is equivalent to the pattern rule @samp{%.o : %.c}.
9545 A single-suffix rule is defined by a single suffix, which is the source
9546 suffix. It matches any file name, and the corresponding implicit
9547 prerequisite name is made by appending the source suffix. A single-suffix
9548 rule whose source suffix is @samp{.c} is equivalent to the pattern rule
9551 Suffix rule definitions are recognized by comparing each rule's target
9552 against a defined list of known suffixes. When @code{make} sees a rule
9553 whose target is a known suffix, this rule is considered a single-suffix
9554 rule. When @code{make} sees a rule whose target is two known suffixes
9555 concatenated, this rule is taken as a double-suffix rule.
9557 For example, @samp{.c} and @samp{.o} are both on the default list of
9558 known suffixes. Therefore, if you define a rule whose target is
9559 @samp{.c.o}, @code{make} takes it to be a double-suffix rule with source
9560 suffix @samp{.c} and target suffix @samp{.o}. Here is the old-fashioned
9561 way to define the rule for compiling a C source file:@refill
9565 $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
9568 Suffix rules cannot have any prerequisites of their own. If they have any,
9569 they are treated as normal files with funny names, not as suffix rules.
9574 $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
9578 tells how to make the file @file{.c.o} from the prerequisite file
9579 @file{foo.h}, and is not at all like the pattern rule:
9583 $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
9587 which tells how to make @samp{.o} files from @samp{.c} files, and makes all
9588 @samp{.o} files using this pattern rule also depend on @file{foo.h}.
9590 Suffix rules with no commands are also meaningless. They do not remove
9591 previous rules as do pattern rules with no commands (@pxref{Canceling
9592 Rules, , Canceling Implicit Rules}). They simply enter the suffix or pair of suffixes concatenated as
9593 a target in the data base.@refill
9596 The known suffixes are simply the names of the prerequisites of the special
9597 target @code{.SUFFIXES}. You can add your own suffixes by writing a rule
9598 for @code{.SUFFIXES} that adds more prerequisites, as in:
9601 .SUFFIXES: .hack .win
9605 which adds @samp{.hack} and @samp{.win} to the end of the list of suffixes.
9607 If you wish to eliminate the default known suffixes instead of just adding
9608 to them, write a rule for @code{.SUFFIXES} with no prerequisites. By
9609 special dispensation, this eliminates all existing prerequisites of
9610 @code{.SUFFIXES}. You can then write another rule to add the suffixes you
9615 .SUFFIXES: # @r{Delete the default suffixes}
9616 .SUFFIXES: .c .o .h # @r{Define our suffix list}
9620 The @samp{-r} or @samp{--no-builtin-rules} flag causes the default
9621 list of suffixes to be empty.
9624 The variable @code{SUFFIXES} is defined to the default list of suffixes
9625 before @code{make} reads any makefiles. You can change the list of suffixes
9626 with a rule for the special target @code{.SUFFIXES}, but that does not alter
9629 @node Implicit Rule Search, , Suffix Rules, Implicit Rules
9630 @section Implicit Rule Search Algorithm
9631 @cindex implicit rule, search algorithm
9632 @cindex search algorithm, implicit rule
9634 Here is the procedure @code{make} uses for searching for an implicit rule
9635 for a target @var{t}. This procedure is followed for each double-colon
9636 rule with no commands, for each target of ordinary rules none of which have
9637 commands, and for each prerequisite that is not the target of any rule. It
9638 is also followed recursively for prerequisites that come from implicit
9639 rules, in the search for a chain of rules.
9641 Suffix rules are not mentioned in this algorithm because suffix rules are
9642 converted to equivalent pattern rules once the makefiles have been read in.
9644 For an archive member target of the form
9645 @samp{@var{archive}(@var{member})}, the following algorithm is run
9646 twice, first using the entire target name @var{t}, and second using
9647 @samp{(@var{member})} as the target @var{t} if the first run found no
9652 Split @var{t} into a directory part, called @var{d}, and the rest,
9653 called @var{n}. For example, if @var{t} is @samp{src/foo.o}, then
9654 @var{d} is @samp{src/} and @var{n} is @samp{foo.o}.@refill
9657 Make a list of all the pattern rules one of whose targets matches
9658 @var{t} or @var{n}. If the target pattern contains a slash, it is
9659 matched against @var{t}; otherwise, against @var{n}.
9662 If any rule in that list is @emph{not} a match-anything rule, then
9663 remove all nonterminal match-anything rules from the list.
9666 Remove from the list all rules with no commands.
9669 For each pattern rule in the list:
9673 Find the stem @var{s}, which is the nonempty part of @var{t} or @var{n}
9674 matched by the @samp{%} in the target pattern.@refill
9677 Compute the prerequisite names by substituting @var{s} for @samp{%}; if
9678 the target pattern does not contain a slash, append @var{d} to
9679 the front of each prerequisite name.@refill
9682 Test whether all the prerequisites exist or ought to exist. (If a
9683 file name is mentioned in the makefile as a target or as an explicit
9684 prerequisite, then we say it ought to exist.)
9686 If all prerequisites exist or ought to exist, or there are no prerequisites,
9687 then this rule applies.
9691 If no pattern rule has been found so far, try harder.
9692 For each pattern rule in the list:
9696 If the rule is terminal, ignore it and go on to the next rule.
9699 Compute the prerequisite names as before.
9702 Test whether all the prerequisites exist or ought to exist.
9705 For each prerequisite that does not exist, follow this algorithm
9706 recursively to see if the prerequisite can be made by an implicit
9710 If all prerequisites exist, ought to exist, or can be
9711 made by implicit rules, then this rule applies.
9715 If no implicit rule applies, the rule for @code{.DEFAULT}, if any,
9716 applies. In that case, give @var{t} the same commands that
9717 @code{.DEFAULT} has. Otherwise, there are no commands for @var{t}.
9720 Once a rule that applies has been found, for each target pattern of the
9721 rule other than the one that matched @var{t} or @var{n}, the @samp{%} in
9722 the pattern is replaced with @var{s} and the resultant file name is stored
9723 until the commands to remake the target file @var{t} are executed. After
9724 these commands are executed, each of these stored file names are entered
9725 into the data base and marked as having been updated and having the same
9726 update status as the file @var{t}.
9728 When the commands of a pattern rule are executed for @var{t}, the automatic
9729 variables are set corresponding to the target and prerequisites.
9730 @xref{Automatic Variables}.
9732 @node Archives, Features, Implicit Rules, Top
9733 @chapter Using @code{make} to Update Archive Files
9736 @dfn{Archive files} are files containing named subfiles called
9737 @dfn{members}; they are maintained with the program @code{ar} and their
9738 main use is as subroutine libraries for linking.
9741 * Archive Members:: Archive members as targets.
9742 * Archive Update:: The implicit rule for archive member targets.
9743 * Archive Pitfalls:: Dangers to watch out for when using archives.
9744 * Archive Suffix Rules:: You can write a special kind of suffix rule
9745 for updating archives.
9748 @node Archive Members, Archive Update, Archives, Archives
9749 @section Archive Members as Targets
9750 @cindex archive member targets
9752 An individual member of an archive file can be used as a target or
9753 prerequisite in @code{make}. You specify the member named @var{member} in
9754 archive file @var{archive} as follows:
9757 @var{archive}(@var{member})
9761 This construct is available only in targets and prerequisites, not in
9762 commands! Most programs that you might use in commands do not support this
9763 syntax and cannot act directly on archive members. Only @code{ar} and
9764 other programs specifically designed to operate on archives can do so.
9765 Therefore, valid commands to update an archive member target probably must
9766 use @code{ar}. For example, this rule says to create a member
9767 @file{hack.o} in archive @file{foolib} by copying the file @file{hack.o}:
9770 foolib(hack.o) : hack.o
9774 In fact, nearly all archive member targets are updated in just this way
9775 and there is an implicit rule to do it for you. @strong{Please note:} The
9776 @samp{c} flag to @code{ar} is required if the archive file does not
9779 To specify several members in the same archive, you can write all the
9780 member names together between the parentheses. For example:
9783 foolib(hack.o kludge.o)
9790 foolib(hack.o) foolib(kludge.o)
9793 @cindex wildcard, in archive member
9794 You can also use shell-style wildcards in an archive member reference.
9795 @xref{Wildcards, ,Using Wildcard Characters in File Names}. For
9796 example, @w{@samp{foolib(*.o)}} expands to all existing members of the
9797 @file{foolib} archive whose names end in @samp{.o}; perhaps
9798 @samp{@w{foolib(hack.o)} @w{foolib(kludge.o)}}.
9800 @node Archive Update, Archive Pitfalls, Archive Members, Archives
9801 @section Implicit Rule for Archive Member Targets
9803 Recall that a target that looks like @file{@var{a}(@var{m})} stands for the
9804 member named @var{m} in the archive file @var{a}.
9806 When @code{make} looks for an implicit rule for such a target, as a special
9807 feature it considers implicit rules that match @file{(@var{m})}, as well as
9808 those that match the actual target @file{@var{a}(@var{m})}.
9810 This causes one special rule whose target is @file{(%)} to match. This
9811 rule updates the target @file{@var{a}(@var{m})} by copying the file @var{m}
9812 into the archive. For example, it will update the archive member target
9813 @file{foo.a(bar.o)} by copying the @emph{file} @file{bar.o} into the
9814 archive @file{foo.a} as a @emph{member} named @file{bar.o}.
9816 When this rule is chained with others, the result is very powerful.
9817 Thus, @samp{make "foo.a(bar.o)"} (the quotes are needed to protect the
9818 @samp{(} and @samp{)} from being interpreted specially by the shell) in
9819 the presence of a file @file{bar.c} is enough to cause the following
9820 commands to be run, even without a makefile:
9823 cc -c bar.c -o bar.o
9829 Here @code{make} has envisioned the file @file{bar.o} as an intermediate
9830 file. @xref{Chained Rules, ,Chains of Implicit Rules}.
9832 Implicit rules such as this one are written using the automatic variable
9833 @samp{$%}. @xref{Automatic Variables}.
9835 An archive member name in an archive cannot contain a directory name, but
9836 it may be useful in a makefile to pretend that it does. If you write an
9837 archive member target @file{foo.a(dir/file.o)}, @code{make} will perform
9838 automatic updating with this command:
9841 ar r foo.a dir/file.o
9845 which has the effect of copying the file @file{dir/file.o} into a member
9846 named @file{file.o}. In connection with such usage, the automatic variables
9847 @code{%D} and @code{%F} may be useful.
9850 * Archive Symbols:: How to update archive symbol directories.
9853 @node Archive Symbols, , Archive Update, Archive Update
9854 @subsection Updating Archive Symbol Directories
9855 @cindex @code{__.SYMDEF}
9856 @cindex updating archive symbol directories
9857 @cindex archive symbol directory updating
9858 @cindex symbol directories, updating archive
9859 @cindex directories, updating archive symbol
9861 An archive file that is used as a library usually contains a special member
9862 named @file{__.SYMDEF} that contains a directory of the external symbol
9863 names defined by all the other members. After you update any other
9864 members, you need to update @file{__.SYMDEF} so that it will summarize the
9865 other members properly. This is done by running the @code{ranlib} program:
9868 ranlib @var{archivefile}
9871 Normally you would put this command in the rule for the archive file,
9872 and make all the members of the archive file prerequisites of that rule.
9876 libfoo.a: libfoo.a(x.o) libfoo.a(y.o) @dots{}
9881 The effect of this is to update archive members @file{x.o}, @file{y.o},
9882 etc., and then update the symbol directory member @file{__.SYMDEF} by
9883 running @code{ranlib}. The rules for updating the members are not shown
9884 here; most likely you can omit them and use the implicit rule which copies
9885 files into the archive, as described in the preceding section.
9887 This is not necessary when using the GNU @code{ar} program, which
9888 updates the @file{__.SYMDEF} member automatically.
9890 @node Archive Pitfalls, Archive Suffix Rules, Archive Update, Archives
9891 @section Dangers When Using Archives
9892 @cindex archive, and parallel execution
9893 @cindex parallel execution, and archive update
9894 @cindex archive, and @code{-j}
9895 @cindex @code{-j}, and archive update
9897 It is important to be careful when using parallel execution (the
9898 @code{-j} switch; @pxref{Parallel, ,Parallel Execution}) and archives.
9899 If multiple @code{ar} commands run at the same time on the same archive
9900 file, they will not know about each other and can corrupt the file.
9902 Possibly a future version of @code{make} will provide a mechanism to
9903 circumvent this problem by serializing all commands that operate on the
9904 same archive file. But for the time being, you must either write your
9905 makefiles to avoid this problem in some other way, or not use @code{-j}.
9907 @node Archive Suffix Rules, , Archive Pitfalls, Archives
9908 @section Suffix Rules for Archive Files
9909 @cindex suffix rule, for archive
9910 @cindex archive, suffix rule for
9911 @cindex library archive, suffix rule for
9912 @cindex @code{.a} (archives)
9914 You can write a special kind of suffix rule for dealing with archive
9915 files. @xref{Suffix Rules}, for a full explanation of suffix rules.
9916 Archive suffix rules are obsolete in GNU @code{make}, because pattern
9917 rules for archives are a more general mechanism (@pxref{Archive
9918 Update}). But they are retained for compatibility with other
9921 To write a suffix rule for archives, you simply write a suffix rule
9922 using the target suffix @samp{.a} (the usual suffix for archive files).
9923 For example, here is the old-fashioned suffix rule to update a library
9924 archive from C source files:
9929 $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o
9936 This works just as if you had written the pattern rule:
9941 $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o
9947 In fact, this is just what @code{make} does when it sees a suffix rule
9948 with @samp{.a} as the target suffix. Any double-suffix rule
9949 @w{@samp{.@var{x}.a}} is converted to a pattern rule with the target
9950 pattern @samp{(%.o)} and a prerequisite pattern of @samp{%.@var{x}}.
9952 Since you might want to use @samp{.a} as the suffix for some other kind
9953 of file, @code{make} also converts archive suffix rules to pattern rules
9954 in the normal way (@pxref{Suffix Rules}). Thus a double-suffix rule
9955 @w{@samp{.@var{x}.a}} produces two pattern rules: @samp{@w{(%.o):}
9956 @w{%.@var{x}}} and @samp{@w{%.a}: @w{%.@var{x}}}.@refill
9958 @node Features, Missing, Archives, Top
9959 @chapter Features of GNU @code{make}
9960 @cindex features of GNU @code{make}
9962 @cindex compatibility
9964 Here is a summary of the features of GNU @code{make}, for comparison
9965 with and credit to other versions of @code{make}. We consider the
9966 features of @code{make} in 4.2 BSD systems as a baseline. If you are
9967 concerned with writing portable makefiles, you should not use the
9968 features of @code{make} listed here, nor the ones in @ref{Missing}.
9970 Many features come from the version of @code{make} in System V.
9974 The @code{VPATH} variable and its special meaning.
9975 @xref{Directory Search, , Searching Directories for Prerequisites}.
9976 This feature exists in System V @code{make}, but is undocumented.
9977 It is documented in 4.3 BSD @code{make} (which says it mimics System V's
9978 @code{VPATH} feature).@refill
9981 Included makefiles. @xref{Include, ,Including Other Makefiles}.
9982 Allowing multiple files to be included with a single directive is a GNU
9986 Variables are read from and communicated via the environment.
9987 @xref{Environment, ,Variables from the Environment}.
9990 Options passed through the variable @code{MAKEFLAGS} to recursive
9991 invocations of @code{make}.
9992 @xref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
9995 The automatic variable @code{$%} is set to the member name
9996 in an archive reference. @xref{Automatic Variables}.
9999 The automatic variables @code{$@@}, @code{$*}, @code{$<}, @code{$%},
10000 and @code{$?} have corresponding forms like @code{$(@@F)} and
10001 @code{$(@@D)}. We have generalized this to @code{$^} as an obvious
10002 extension. @xref{Automatic Variables}.@refill
10005 Substitution variable references.
10006 @xref{Reference, ,Basics of Variable References}.
10009 The command-line options @samp{-b} and @samp{-m}, accepted and
10010 ignored. In System V @code{make}, these options actually do something.
10013 Execution of recursive commands to run @code{make} via the variable
10014 @code{MAKE} even if @samp{-n}, @samp{-q} or @samp{-t} is specified.
10015 @xref{Recursion, ,Recursive Use of @code{make}}.
10018 Support for suffix @samp{.a} in suffix rules. @xref{Archive Suffix
10019 Rules}. This feature is obsolete in GNU @code{make}, because the
10020 general feature of rule chaining (@pxref{Chained Rules, ,Chains of
10021 Implicit Rules}) allows one pattern rule for installing members in an
10022 archive (@pxref{Archive Update}) to be sufficient.
10025 The arrangement of lines and backslash-newline combinations in
10026 commands is retained when the commands are printed, so they appear as
10027 they do in the makefile, except for the stripping of initial
10031 The following features were inspired by various other versions of
10032 @code{make}. In some cases it is unclear exactly which versions inspired
10037 Pattern rules using @samp{%}.
10038 This has been implemented in several versions of @code{make}.
10039 We're not sure who invented it first, but it's been spread around a bit.
10040 @xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.@refill
10043 Rule chaining and implicit intermediate files.
10044 This was implemented by Stu Feldman in his version of @code{make}
10045 for AT&T Eighth Edition Research Unix, and later by Andrew Hume of
10046 AT&T Bell Labs in his @code{mk} program (where he terms it
10047 ``transitive closure''). We do not really know if
10048 we got this from either of them or thought it up ourselves at the
10049 same time. @xref{Chained Rules, ,Chains of Implicit Rules}.
10052 The automatic variable @code{$^} containing a list of all prerequisites
10053 of the current target. We did not invent this, but we have no idea who
10054 did. @xref{Automatic Variables}. The automatic variable
10055 @code{$+} is a simple extension of @code{$^}.
10058 The ``what if'' flag (@samp{-W} in GNU @code{make}) was (as far as we know)
10059 invented by Andrew Hume in @code{mk}.
10060 @xref{Instead of Execution, ,Instead of Executing the Commands}.
10063 The concept of doing several things at once (parallelism) exists in
10064 many incarnations of @code{make} and similar programs, though not in the
10065 System V or BSD implementations. @xref{Execution, ,Command Execution}.
10068 Modified variable references using pattern substitution come from
10069 SunOS 4. @xref{Reference, ,Basics of Variable References}.
10070 This functionality was provided in GNU @code{make} by the
10071 @code{patsubst} function before the alternate syntax was implemented
10072 for compatibility with SunOS 4. It is not altogether clear who
10073 inspired whom, since GNU @code{make} had @code{patsubst} before SunOS
10074 4 was released.@refill
10077 The special significance of @samp{+} characters preceding command lines
10078 (@pxref{Instead of Execution, ,Instead of Executing the Commands}) is
10080 @cite{IEEE Standard 1003.2-1992} (POSIX.2).
10083 The @samp{+=} syntax to append to the value of a variable comes from SunOS
10084 4 @code{make}. @xref{Appending, , Appending More Text to Variables}.
10087 The syntax @w{@samp{@var{archive}(@var{mem1} @var{mem2}@dots{})}} to list
10088 multiple members in a single archive file comes from SunOS 4 @code{make}.
10089 @xref{Archive Members}.
10092 The @code{-include} directive to include makefiles with no error for a
10093 nonexistent file comes from SunOS 4 @code{make}. (But note that SunOS 4
10094 @code{make} does not allow multiple makefiles to be specified in one
10095 @code{-include} directive.) The same feature appears with the name
10096 @code{sinclude} in SGI @code{make} and perhaps others.
10099 The remaining features are inventions new in GNU @code{make}:
10103 Use the @samp{-v} or @samp{--version} option to print version and
10104 copyright information.
10107 Use the @samp{-h} or @samp{--help} option to summarize the options to
10111 Simply-expanded variables. @xref{Flavors, ,The Two Flavors of Variables}.
10114 Pass command-line variable assignments automatically through the
10115 variable @code{MAKE} to recursive @code{make} invocations.
10116 @xref{Recursion, ,Recursive Use of @code{make}}.
10119 Use the @samp{-C} or @samp{--directory} command option to change
10120 directory. @xref{Options Summary, ,Summary of Options}.
10123 Make verbatim variable definitions with @code{define}.
10124 @xref{Defining, ,Defining Variables Verbatim}.
10127 Declare phony targets with the special target @code{.PHONY}.
10129 Andrew Hume of AT&T Bell Labs implemented a similar feature with a
10130 different syntax in his @code{mk} program. This seems to be a case of
10131 parallel discovery. @xref{Phony Targets, ,Phony Targets}.
10134 Manipulate text by calling functions.
10135 @xref{Functions, ,Functions for Transforming Text}.
10138 Use the @samp{-o} or @samp{--old-file}
10139 option to pretend a file's modification-time is old.
10140 @xref{Avoiding Compilation, ,Avoiding Recompilation of Some Files}.
10143 Conditional execution.
10145 This feature has been implemented numerous times in various versions
10146 of @code{make}; it seems a natural extension derived from the features
10147 of the C preprocessor and similar macro languages and is not a
10148 revolutionary concept. @xref{Conditionals, ,Conditional Parts of Makefiles}.
10151 Specify a search path for included makefiles.
10152 @xref{Include, ,Including Other Makefiles}.
10155 Specify extra makefiles to read with an environment variable.
10156 @xref{MAKEFILES Variable, ,The Variable @code{MAKEFILES}}.
10159 Strip leading sequences of @samp{./} from file names, so that
10160 @file{./@var{file}} and @file{@var{file}} are considered to be the
10164 Use a special search method for library prerequisites written in the
10165 form @samp{-l@var{name}}.
10166 @xref{Libraries/Search, ,Directory Search for Link Libraries}.
10169 Allow suffixes for suffix rules
10170 (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}) to contain any
10171 characters. In other versions of @code{make}, they must begin with
10172 @samp{.} and not contain any @samp{/} characters.
10175 Keep track of the current level of @code{make} recursion using the
10176 variable @code{MAKELEVEL}. @xref{Recursion, ,Recursive Use of @code{make}}.
10179 Provide any goals given on the command line in the variable
10180 @code{MAKECMDGOALS}. @xref{Goals, ,Arguments to Specify the Goals}.
10183 Specify static pattern rules. @xref{Static Pattern, ,Static Pattern Rules}.
10186 Provide selective @code{vpath} search.
10187 @xref{Directory Search, ,Searching Directories for Prerequisites}.
10190 Provide computed variable references.
10191 @xref{Reference, ,Basics of Variable References}.
10194 Update makefiles. @xref{Remaking Makefiles, ,How Makefiles Are Remade}.
10195 System V @code{make} has a very, very limited form of this
10196 functionality in that it will check out SCCS files for makefiles.
10199 Various new built-in implicit rules.
10200 @xref{Catalogue of Rules, ,Catalogue of Implicit Rules}.
10203 The built-in variable @samp{MAKE_VERSION} gives the version number of
10205 @vindex MAKE_VERSION
10208 @node Missing, Makefile Conventions, Features, Top
10209 @chapter Incompatibilities and Missing Features
10210 @cindex incompatibilities
10211 @cindex missing features
10212 @cindex features, missing
10214 The @code{make} programs in various other systems support a few features
10215 that are not implemented in GNU @code{make}. The POSIX.2 standard
10216 (@cite{IEEE Standard 1003.2-1992}) which specifies @code{make} does not
10217 require any of these features.@refill
10221 A target of the form @samp{@var{file}((@var{entry}))} stands for a member
10222 of archive file @var{file}. The member is chosen, not by name, but by
10223 being an object file which defines the linker symbol @var{entry}.@refill
10225 This feature was not put into GNU @code{make} because of the
10226 nonmodularity of putting knowledge into @code{make} of the internal
10227 format of archive file symbol tables.
10228 @xref{Archive Symbols, ,Updating Archive Symbol Directories}.
10231 Suffixes (used in suffix rules) that end with the character @samp{~}
10232 have a special meaning to System V @code{make};
10233 they refer to the SCCS file that corresponds
10234 to the file one would get without the @samp{~}. For example, the
10235 suffix rule @samp{.c~.o} would make the file @file{@var{n}.o} from
10236 the SCCS file @file{s.@var{n}.c}. For complete coverage, a whole
10237 series of such suffix rules is required.
10238 @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
10240 In GNU @code{make}, this entire series of cases is handled by two
10241 pattern rules for extraction from SCCS, in combination with the
10242 general feature of rule chaining.
10243 @xref{Chained Rules, ,Chains of Implicit Rules}.
10246 In System V and 4.3 BSD @code{make}, files found by @code{VPATH} search
10247 (@pxref{Directory Search, ,Searching Directories for Prerequisites}) have their names changed inside command
10248 strings. We feel it is much cleaner to always use automatic variables
10249 and thus make this feature obsolete.@refill
10252 In some Unix @code{make}s, the automatic variable @code{$*} appearing in
10253 the prerequisites of a rule has the amazingly strange ``feature'' of
10254 expanding to the full name of the @emph{target of that rule}. We cannot
10255 imagine what went on in the minds of Unix @code{make} developers to do
10256 this; it is utterly inconsistent with the normal definition of @code{$*}.
10257 @vindex * @r{(automatic variable), unsupported bizarre usage}
10260 In some Unix @code{make}s, implicit rule search
10261 (@pxref{Implicit Rules, ,Using Implicit Rules}) is apparently done for
10262 @emph{all} targets, not just those without commands. This means you can
10273 and Unix @code{make} will intuit that @file{foo.o} depends on
10274 @file{foo.c}.@refill
10276 We feel that such usage is broken. The prerequisite properties of
10277 @code{make} are well-defined (for GNU @code{make}, at least),
10278 and doing such a thing simply does not fit the model.@refill
10281 GNU @code{make} does not include any built-in implicit rules for
10282 compiling or preprocessing EFL programs. If we hear of anyone who is
10283 using EFL, we will gladly add them.
10286 It appears that in SVR4 @code{make}, a suffix rule can be specified with
10287 no commands, and it is treated as if it had empty commands
10288 (@pxref{Empty Commands}). For example:
10295 will override the built-in @file{.c.a} suffix rule.
10297 We feel that it is cleaner for a rule without commands to always simply
10298 add to the prerequisite list for the target. The above example can be
10299 easily rewritten to get the desired behavior in GNU @code{make}:
10306 Some versions of @code{make} invoke the shell with the @samp{-e} flag,
10307 except under @samp{-k} (@pxref{Testing, ,Testing the Compilation of a
10308 Program}). The @samp{-e} flag tells the shell to exit as soon as any
10309 program it runs returns a nonzero status. We feel it is cleaner to
10310 write each shell command line to stand on its own and not require this
10314 @comment The makefile standards are in a separate file that is also
10315 @comment included by standards.texi.
10316 @include make-stds.texi
10318 @node Quick Reference, Error Messages, Makefile Conventions, Top
10319 @appendix Quick Reference
10321 This appendix summarizes the directives, text manipulation functions,
10322 and special variables which GNU @code{make} understands.
10323 @xref{Special Targets}, @ref{Catalogue of Rules, ,Catalogue of Implicit Rules},
10324 and @ref{Options Summary, ,Summary of Options},
10325 for other summaries.
10327 Here is a summary of the directives GNU @code{make} recognizes:
10330 @item define @var{variable}
10333 Define a multi-line, recursively-expanded variable.@*
10336 @item ifdef @var{variable}
10337 @itemx ifndef @var{variable}
10338 @itemx ifeq (@var{a},@var{b})
10339 @itemx ifeq "@var{a}" "@var{b}"
10340 @itemx ifeq '@var{a}' '@var{b}'
10341 @itemx ifneq (@var{a},@var{b})
10342 @itemx ifneq "@var{a}" "@var{b}"
10343 @itemx ifneq '@var{a}' '@var{b}'
10347 Conditionally evaluate part of the makefile.@*
10348 @xref{Conditionals}.
10350 @item include @var{file}
10351 @itemx -include @var{file}
10352 @itemx sinclude @var{file}
10354 Include another makefile.@*
10355 @xref{Include, ,Including Other Makefiles}.
10357 @item override @var{variable} = @var{value}
10358 @itemx override @var{variable} := @var{value}
10359 @itemx override @var{variable} += @var{value}
10360 @itemx override @var{variable} ?= @var{value}
10361 @itemx override define @var{variable}
10364 Define a variable, overriding any previous definition, even one from
10365 the command line.@*
10366 @xref{Override Directive, ,The @code{override} Directive}.
10370 Tell @code{make} to export all variables to child processes by default.@*
10371 @xref{Variables/Recursion, , Communicating Variables to a Sub-@code{make}}.
10373 @item export @var{variable}
10374 @itemx export @var{variable} = @var{value}
10375 @itemx export @var{variable} := @var{value}
10376 @itemx export @var{variable} += @var{value}
10377 @itemx export @var{variable} ?= @var{value}
10378 @itemx unexport @var{variable}
10379 Tell @code{make} whether or not to export a particular variable to child
10381 @xref{Variables/Recursion, , Communicating Variables to a Sub-@code{make}}.
10383 @item vpath @var{pattern} @var{path}
10384 Specify a search path for files matching a @samp{%} pattern.@*
10385 @xref{Selective Search, , The @code{vpath} Directive}.
10387 @item vpath @var{pattern}
10388 Remove all search paths previously specified for @var{pattern}.
10391 Remove all search paths previously specified in any @code{vpath}
10395 Here is a summary of the built-in functions (@pxref{Functions}):
10398 @item $(subst @var{from},@var{to},@var{text})
10399 Replace @var{from} with @var{to} in @var{text}.@*
10400 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10402 @item $(patsubst @var{pattern},@var{replacement},@var{text})
10403 Replace words matching @var{pattern} with @var{replacement} in @var{text}.@*
10404 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10406 @item $(strip @var{string})
10407 Remove excess whitespace characters from @var{string}.@*
10408 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10410 @item $(findstring @var{find},@var{text})
10411 Locate @var{find} in @var{text}.@*
10412 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10414 @item $(filter @var{pattern}@dots{},@var{text})
10415 Select words in @var{text} that match one of the @var{pattern} words.@*
10416 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10418 @item $(filter-out @var{pattern}@dots{},@var{text})
10419 Select words in @var{text} that @emph{do not} match any of the @var{pattern} words.@*
10420 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10422 @item $(sort @var{list})
10423 Sort the words in @var{list} lexicographically, removing duplicates.@*
10424 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10426 @item $(word @var{n},@var{text})
10427 Extract the @var{n}th word (one-origin) of @var{text}.@*
10428 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10430 @item $(words @var{text})
10431 Count the number of words in @var{text}.@*
10432 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10434 @item $(wordlist @var{s},@var{e},@var{text})
10435 Returns the list of words in @var{text} from @var{s} to @var{e}.@*
10436 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10438 @item $(firstword @var{names}@dots{})
10439 Extract the first word of @var{names}.@*
10440 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10442 @item $(lastword @var{names}@dots{})
10443 Extract the last word of @var{names}.@*
10444 @xref{Text Functions, , Functions for String Substitution and Analysis}.
10446 @item $(dir @var{names}@dots{})
10447 Extract the directory part of each file name.@*
10448 @xref{File Name Functions, ,Functions for File Names}.
10450 @item $(notdir @var{names}@dots{})
10451 Extract the non-directory part of each file name.@*
10452 @xref{File Name Functions, ,Functions for File Names}.
10454 @item $(suffix @var{names}@dots{})
10455 Extract the suffix (the last @samp{.} and following characters) of each file name.@*
10456 @xref{File Name Functions, ,Functions for File Names}.
10458 @item $(basename @var{names}@dots{})
10459 Extract the base name (name without suffix) of each file name.@*
10460 @xref{File Name Functions, ,Functions for File Names}.
10462 @item $(addsuffix @var{suffix},@var{names}@dots{})
10463 Append @var{suffix} to each word in @var{names}.@*
10464 @xref{File Name Functions, ,Functions for File Names}.
10466 @item $(addprefix @var{prefix},@var{names}@dots{})
10467 Prepend @var{prefix} to each word in @var{names}.@*
10468 @xref{File Name Functions, ,Functions for File Names}.
10470 @item $(join @var{list1},@var{list2})
10471 Join two parallel lists of words.@*
10472 @xref{File Name Functions, ,Functions for File Names}.
10474 @item $(wildcard @var{pattern}@dots{})
10475 Find file names matching a shell file name pattern (@emph{not} a
10476 @samp{%} pattern).@*
10477 @xref{Wildcard Function, ,The Function @code{wildcard}}.
10479 @item $(realpath @var{names}@dots{})
10480 For each file name in @var{names}, expand to an absolute name that
10481 does not contain any @code{.}, @code{..}, nor symlinks.@*
10482 @xref{File Name Functions, ,Functions for File Names}.
10484 @item $(abspath @var{names}@dots{})
10485 For each file name in @var{names}, expand to an absolute name that
10486 does not contain any @code{.} or @code{..} components, but preserves
10488 @xref{File Name Functions, ,Functions for File Names}.
10490 @item $(error @var{text}@dots{})
10492 When this function is evaluated, @code{make} generates a fatal error
10493 with the message @var{text}.@*
10494 @xref{Make Control Functions, ,Functions That Control Make}.
10496 @item $(warning @var{text}@dots{})
10498 When this function is evaluated, @code{make} generates a warning with
10499 the message @var{text}.@*
10500 @xref{Make Control Functions, ,Functions That Control Make}.
10502 @item $(shell @var{command})
10504 Execute a shell command and return its output.@*
10505 @xref{Shell Function, , The @code{shell} Function}.
10507 @item $(origin @var{variable})
10509 Return a string describing how the @code{make} variable @var{variable} was
10511 @xref{Origin Function, , The @code{origin} Function}.
10513 @item $(flavor @var{variable})
10515 Return a string describing the flavor of the @code{make} variable
10517 @xref{Flavor Function, , The @code{flavor} Function}.
10519 @item $(foreach @var{var},@var{words},@var{text})
10521 Evaluate @var{text} with @var{var} bound to each word in @var{words},
10522 and concatenate the results.@*
10523 @xref{Foreach Function, ,The @code{foreach} Function}.
10525 @item $(call @var{var},@var{param},@dots{})
10527 Evaluate the variable @var{var} replacing any references to @code{$(1)},
10528 @code{$(2)} with the first, second, etc.@: @var{param} values.@*
10529 @xref{Call Function, ,The @code{call} Function}.
10531 @item $(eval @var{text})
10533 Evaluate @var{text} then read the results as makefile commands.
10534 Expands to the empty string.@*
10535 @xref{Eval Function, ,The @code{eval} Function}.
10537 @item $(value @var{var})
10539 Evaluates to the contents of the variable @var{var}, with no expansion
10541 @xref{Value Function, ,The @code{value} Function}.
10544 Here is a summary of the automatic variables.
10545 @xref{Automatic Variables},
10546 for full information.
10550 The file name of the target.
10553 The target member name, when the target is an archive member.
10556 The name of the first prerequisite.
10559 The names of all the prerequisites that are
10560 newer than the target, with spaces between them.
10561 For prerequisites which are archive members, only
10562 the member named is used (@pxref{Archives}).
10566 The names of all the prerequisites, with spaces between them. For
10567 prerequisites which are archive members, only the member named is used
10568 (@pxref{Archives}). The value of @code{$^} omits duplicate
10569 prerequisites, while @code{$+} retains them and preserves their order.
10572 The stem with which an implicit rule matches
10573 (@pxref{Pattern Match, ,How Patterns Match}).
10577 The directory part and the file-within-directory part of @code{$@@}.
10581 The directory part and the file-within-directory part of @code{$*}.
10585 The directory part and the file-within-directory part of @code{$%}.
10589 The directory part and the file-within-directory part of @code{$<}.
10593 The directory part and the file-within-directory part of @code{$^}.
10597 The directory part and the file-within-directory part of @code{$+}.
10601 The directory part and the file-within-directory part of @code{$?}.
10604 These variables are used specially by GNU @code{make}:
10609 Makefiles to be read on every invocation of @code{make}.@*
10610 @xref{MAKEFILES Variable, ,The Variable @code{MAKEFILES}}.
10614 Directory search path for files not found in the current directory.@*
10615 @xref{General Search, , @code{VPATH} Search Path for All Prerequisites}.
10619 The name of the system default command interpreter, usually @file{/bin/sh}.
10620 You can set @code{SHELL} in the makefile to change the shell used to run
10621 commands. @xref{Execution, ,Command Execution}. The @code{SHELL}
10622 variable is handled specially when importing from and exporting to the
10623 environment. @xref{Choosing the Shell}.
10627 On MS-DOS only, the name of the command interpreter that is to be used
10628 by @code{make}. This value takes precedence over the value of
10629 @code{SHELL}. @xref{Execution, ,MAKESHELL variable}.
10633 The name with which @code{make} was invoked.
10634 Using this variable in commands has special meaning.
10635 @xref{MAKE Variable, ,How the @code{MAKE} Variable Works}.
10639 The number of levels of recursion (sub-@code{make}s).@*
10640 @xref{Variables/Recursion}.
10644 The flags given to @code{make}. You can set this in the environment or
10645 a makefile to set flags.@*
10646 @xref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
10648 It is @emph{never} appropriate to use @code{MAKEFLAGS} directly on a
10649 command line: its contents may not be quoted correctly for use in the
10650 shell. Always allow recursive @code{make}'s to obtain these values
10651 through the environment from its parent.
10655 The targets given to @code{make} on the command line. Setting this
10656 variable has no effect on the operation of @code{make}.@*
10657 @xref{Goals, ,Arguments to Specify the Goals}.
10661 Set to the pathname of the current working directory (after all
10662 @code{-C} options are processed, if any). Setting this variable has no
10663 effect on the operation of @code{make}.@*
10664 @xref{Recursion, ,Recursive Use of @code{make}}.
10668 The default list of suffixes before @code{make} reads any makefiles.
10671 Defines the naming of the libraries @code{make} searches for, and their
10673 @xref{Libraries/Search, ,Directory Search for Link Libraries}.
10676 @node Error Messages, Complex Makefile, Quick Reference, Top
10677 @comment node-name, next, previous, up
10678 @appendix Errors Generated by Make
10680 Here is a list of the more common errors you might see generated by
10681 @code{make}, and some information about what they mean and how to fix
10684 Sometimes @code{make} errors are not fatal, especially in the presence
10685 of a @code{-} prefix on a command script line, or the @code{-k} command
10686 line option. Errors that are fatal are prefixed with the string
10689 Error messages are all either prefixed with the name of the program
10690 (usually @samp{make}), or, if the error is found in a makefile, the name
10691 of the file and linenumber containing the problem.
10693 In the table below, these common prefixes are left off.
10697 @item [@var{foo}] Error @var{NN}
10698 @itemx [@var{foo}] @var{signal description}
10699 These errors are not really @code{make} errors at all. They mean that a
10700 program that @code{make} invoked as part of a command script returned a
10701 non-0 error code (@samp{Error @var{NN}}), which @code{make} interprets
10702 as failure, or it exited in some other abnormal fashion (with a
10703 signal of some type). @xref{Errors, ,Errors in Commands}.
10705 If no @code{***} is attached to the message, then the subprocess failed
10706 but the rule in the makefile was prefixed with the @code{-} special
10707 character, so @code{make} ignored the error.
10709 @item missing separator. Stop.
10710 @itemx missing separator (did you mean TAB instead of 8 spaces?). Stop.
10711 This means that @code{make} could not understand much of anything about
10712 the command line it just read. GNU @code{make} looks for various kinds
10713 of separators (@code{:}, @code{=}, TAB characters, etc.) to help it
10714 decide what kind of commandline it's seeing. This means it couldn't
10717 One of the most common reasons for this message is that you (or perhaps
10718 your oh-so-helpful editor, as is the case with many MS-Windows editors)
10719 have attempted to indent your command scripts with spaces instead of a
10720 TAB character. In this case, @code{make} will use the second form of
10721 the error above. Remember that every line in the command script must
10722 begin with a TAB character. Eight spaces do not count. @xref{Rule
10725 @item commands commence before first target. Stop.
10726 @itemx missing rule before commands. Stop.
10727 This means the first thing in the makefile seems to be part of a command
10728 script: it begins with a TAB character and doesn't appear to be a legal
10729 @code{make} command (such as a variable assignment). Command scripts
10730 must always be associated with a target.
10732 The second form is generated if the line has a semicolon as the first
10733 non-whitespace character; @code{make} interprets this to mean you left
10734 out the "target: prerequisite" section of a rule. @xref{Rule Syntax}.
10736 @item No rule to make target `@var{xxx}'.
10737 @itemx No rule to make target `@var{xxx}', needed by `@var{yyy}'.
10738 This means that @code{make} decided it needed to build a target, but
10739 then couldn't find any instructions in the makefile on how to do that,
10740 either explicit or implicit (including in the default rules database).
10742 If you want that file to be built, you will need to add a rule to your
10743 makefile describing how that target can be built. Other possible
10744 sources of this problem are typos in the makefile (if that filename is
10745 wrong) or a corrupted source tree (if that file is not supposed to be
10746 built, but rather only a prerequisite).
10748 @item No targets specified and no makefile found. Stop.
10749 @itemx No targets. Stop.
10750 The former means that you didn't provide any targets to be built on the
10751 command line, and @code{make} couldn't find any makefiles to read in.
10752 The latter means that some makefile was found, but it didn't contain any
10753 default goal and none was given on the command line. GNU @code{make}
10754 has nothing to do in these situations.
10755 @xref{Makefile Arguments, ,Arguments to Specify the Makefile}.@refill
10757 @item Makefile `@var{xxx}' was not found.
10758 @itemx Included makefile `@var{xxx}' was not found.
10759 A makefile specified on the command line (first form) or included
10760 (second form) was not found.
10762 @item warning: overriding commands for target `@var{xxx}'
10763 @itemx warning: ignoring old commands for target `@var{xxx}'
10764 GNU @code{make} allows commands to be specified only once per target
10765 (except for double-colon rules). If you give commands for a target
10766 which already has been defined to have commands, this warning is issued
10767 and the second set of commands will overwrite the first set.
10768 @xref{Multiple Rules, ,Multiple Rules for One Target}.
10770 @item Circular @var{xxx} <- @var{yyy} dependency dropped.
10771 This means that @code{make} detected a loop in the dependency graph:
10772 after tracing the prerequisite @var{yyy} of target @var{xxx}, and its
10773 prerequisites, etc., one of them depended on @var{xxx} again.
10775 @item Recursive variable `@var{xxx}' references itself (eventually). Stop.
10776 This means you've defined a normal (recursive) @code{make} variable
10777 @var{xxx} that, when it's expanded, will refer to itself (@var{xxx}).
10778 This is not allowed; either use simply-expanded variables (@code{:=}) or
10779 use the append operator (@code{+=}). @xref{Using Variables, ,How to Use
10782 @item Unterminated variable reference. Stop.
10783 This means you forgot to provide the proper closing parenthesis
10784 or brace in your variable or function reference.
10786 @item insufficient arguments to function `@var{xxx}'. Stop.
10787 This means you haven't provided the requisite number of arguments for
10788 this function. See the documentation of the function for a description
10789 of its arguments. @xref{Functions, ,Functions for Transforming Text}.
10791 @item missing target pattern. Stop.
10792 @itemx multiple target patterns. Stop.
10793 @itemx target pattern contains no `%'. Stop.
10794 @itemx mixed implicit and static pattern rules. Stop.
10795 These are generated for malformed static pattern rules. The first means
10796 there's no pattern in the target section of the rule; the second means
10797 there are multiple patterns in the target section; the third means
10798 the target doesn't contain a pattern character (@code{%}); and the
10799 fourth means that all three parts of the static pattern rule contain
10800 pattern characters (@code{%})--only the first two parts should.
10801 @xref{Static Usage, ,Syntax of Static Pattern Rules}.
10803 @item warning: -jN forced in submake: disabling jobserver mode.
10804 This warning and the next are generated if @code{make} detects error
10805 conditions related to parallel processing on systems where
10806 sub-@code{make}s can communicate (@pxref{Options/Recursion,
10807 ,Communicating Options to a Sub-@code{make}}). This warning is
10808 generated if a recursive invocation of a @code{make} process is forced
10809 to have @samp{-j@var{N}} in its argument list (where @var{N} is greater
10810 than one). This could happen, for example, if you set the @code{MAKE}
10811 environment variable to @samp{make -j2}. In this case, the
10812 sub-@code{make} doesn't communicate with other @code{make} processes and
10813 will simply pretend it has two jobs of its own.
10815 @item warning: jobserver unavailable: using -j1. Add `+' to parent make rule.
10816 In order for @code{make} processes to communicate, the parent will pass
10817 information to the child. Since this could result in problems if the
10818 child process isn't actually a @code{make}, the parent will only do this
10819 if it thinks the child is a @code{make}. The parent uses the normal
10820 algorithms to determine this (@pxref{MAKE Variable, ,How the @code{MAKE}
10821 Variable Works}). If the makefile is constructed such that the parent
10822 doesn't know the child is a @code{make} process, then the child will
10823 receive only part of the information necessary. In this case, the child
10824 will generate this warning message and proceed with its build in a
10829 @node Complex Makefile, GNU Free Documentation License, Error Messages, Top
10830 @appendix Complex Makefile Example
10832 Here is the makefile for the GNU @code{tar} program. This is a
10833 moderately complex makefile.
10835 Because it is the first target, the default goal is @samp{all}. An
10836 interesting feature of this makefile is that @file{testpad.h} is a
10837 source file automatically created by the @code{testpad} program,
10838 itself compiled from @file{testpad.c}.
10840 If you type @samp{make} or @samp{make all}, then @code{make} creates
10841 the @file{tar} executable, the @file{rmt} daemon that provides
10842 remote tape access, and the @file{tar.info} Info file.
10844 If you type @samp{make install}, then @code{make} not only creates
10845 @file{tar}, @file{rmt}, and @file{tar.info}, but also installs
10848 If you type @samp{make clean}, then @code{make} removes the @samp{.o}
10849 files, and the @file{tar}, @file{rmt}, @file{testpad},
10850 @file{testpad.h}, and @file{core} files.
10852 If you type @samp{make distclean}, then @code{make} not only removes
10853 the same files as does @samp{make clean} but also the
10854 @file{TAGS}, @file{Makefile}, and @file{config.status} files.
10855 (Although it is not evident, this makefile (and
10856 @file{config.status}) is generated by the user with the
10857 @code{configure} program, which is provided in the @code{tar}
10858 distribution, but is not shown here.)
10860 If you type @samp{make realclean}, then @code{make} removes the same
10861 files as does @samp{make distclean} and also removes the Info files
10862 generated from @file{tar.texinfo}.
10864 In addition, there are targets @code{shar} and @code{dist} that create
10869 # Generated automatically from Makefile.in by configure.
10870 # Un*x Makefile for GNU tar program.
10871 # Copyright (C) 1991 Free Software Foundation, Inc.
10875 # This program is free software; you can redistribute
10876 # it and/or modify it under the terms of the GNU
10877 # General Public License @dots{}
10884 #### Start of system configuration section. ####
10889 # If you use gcc, you should either run the
10890 # fixincludes script that comes with it or else use
10891 # gcc with the -traditional option. Otherwise ioctl
10892 # calls will be compiled incorrectly on some systems.
10895 INSTALL = /usr/local/bin/install -c
10896 INSTALLDATA = /usr/local/bin/install -c -m 644
10899 # Things you might add to DEFS:
10900 # -DSTDC_HEADERS If you have ANSI C headers and
10902 # -DPOSIX If you have POSIX.1 headers and
10904 # -DBSD42 If you have sys/dir.h (unless
10905 # you use -DPOSIX), sys/file.h,
10906 # and st_blocks in `struct stat'.
10907 # -DUSG If you have System V/ANSI C
10908 # string and memory functions
10909 # and headers, sys/sysmacros.h,
10910 # fcntl.h, getcwd, no valloc,
10911 # and ndir.h (unless
10912 # you use -DDIRENT).
10913 # -DNO_MEMORY_H If USG or STDC_HEADERS but do not
10914 # include memory.h.
10915 # -DDIRENT If USG and you have dirent.h
10916 # instead of ndir.h.
10917 # -DSIGTYPE=int If your signal handlers
10918 # return int, not void.
10919 # -DNO_MTIO If you lack sys/mtio.h
10920 # (magtape ioctls).
10921 # -DNO_REMOTE If you do not have a remote shell
10923 # -DUSE_REXEC To use rexec for remote tape
10924 # operations instead of
10925 # forking rsh or remsh.
10926 # -DVPRINTF_MISSING If you lack vprintf function
10927 # (but have _doprnt).
10928 # -DDOPRNT_MISSING If you lack _doprnt function.
10929 # Also need to define
10930 # -DVPRINTF_MISSING.
10931 # -DFTIME_MISSING If you lack ftime system call.
10932 # -DSTRSTR_MISSING If you lack strstr function.
10933 # -DVALLOC_MISSING If you lack valloc function.
10934 # -DMKDIR_MISSING If you lack mkdir and
10935 # rmdir system calls.
10936 # -DRENAME_MISSING If you lack rename system call.
10937 # -DFTRUNCATE_MISSING If you lack ftruncate
10939 # -DV7 On Version 7 Unix (not
10940 # tested in a long time).
10941 # -DEMUL_OPEN3 If you lack a 3-argument version
10942 # of open, and want to emulate it
10943 # with system calls you do have.
10944 # -DNO_OPEN3 If you lack the 3-argument open
10945 # and want to disable the tar -k
10946 # option instead of emulating open.
10947 # -DXENIX If you have sys/inode.h
10948 # and need it 94 to be included.
10950 DEFS = -DSIGTYPE=int -DDIRENT -DSTRSTR_MISSING \
10951 -DVPRINTF_MISSING -DBSD42
10952 # Set this to rtapelib.o unless you defined NO_REMOTE,
10953 # in which case make it empty.
10954 RTAPELIB = rtapelib.o
10956 DEF_AR_FILE = /dev/rmt8
10961 CFLAGS = $(CDEBUG) -I. -I$(srcdir) $(DEFS) \
10962 -DDEF_AR_FILE=\"$(DEF_AR_FILE)\" \
10963 -DDEFBLOCKING=$(DEFBLOCKING)
10968 prefix = /usr/local
10969 # Prefix for each installed program,
10970 # normally empty or `g'.
10973 # The directory to install tar in.
10974 bindir = $(prefix)/bin
10976 # The directory to install the info files in.
10977 infodir = $(prefix)/info
10980 #### End of system configuration section. ####
10982 SRC1 = tar.c create.c extract.c buffer.c \
10983 getoldopt.c update.c gnu.c mangle.c
10984 SRC2 = version.c list.c names.c diffarch.c \
10985 port.c wildmat.c getopt.c
10986 SRC3 = getopt1.c regex.c getdate.y
10987 SRCS = $(SRC1) $(SRC2) $(SRC3)
10988 OBJ1 = tar.o create.o extract.o buffer.o \
10989 getoldopt.o update.o gnu.o mangle.o
10990 OBJ2 = version.o list.o names.o diffarch.o \
10991 port.o wildmat.o getopt.o
10992 OBJ3 = getopt1.o regex.o getdate.o $(RTAPELIB)
10993 OBJS = $(OBJ1) $(OBJ2) $(OBJ3)
10995 AUX = README COPYING ChangeLog Makefile.in \
10996 makefile.pc configure configure.in \
10997 tar.texinfo tar.info* texinfo.tex \
10998 tar.h port.h open3.h getopt.h regex.h \
10999 rmt.h rmt.c rtapelib.c alloca.c \
11000 msd_dir.h msd_dir.c tcexparg.c \
11001 level-0 level-1 backup-specs testpad.c
11005 all: tar rmt tar.info
11010 $(CC) $(LDFLAGS) -o $@@ $(OBJS) $(LIBS)
11015 $(CC) $(CFLAGS) $(LDFLAGS) -o $@@ rmt.c
11019 tar.info: tar.texinfo
11020 makeinfo tar.texinfo
11026 $(INSTALL) tar $(bindir)/$(binprefix)tar
11027 -test ! -f rmt || $(INSTALL) rmt /etc/rmt
11028 $(INSTALLDATA) $(srcdir)/tar.info* $(infodir)
11032 $(OBJS): tar.h port.h testpad.h
11033 regex.o buffer.o tar.o: regex.h
11034 # getdate.y has 8 shift/reduce conflicts.
11044 $(CC) -o $@@ testpad.o
11055 rm -f *.o tar rmt testpad testpad.h core
11061 rm -f TAGS Makefile config.status
11066 realclean: distclean
11072 shar: $(SRCS) $(AUX)
11073 shar $(SRCS) $(AUX) | compress \
11074 > tar-`sed -e '/version_string/!d' \
11075 -e 's/[^0-9.]*\([0-9.]*\).*/\1/' \
11082 dist: $(SRCS) $(AUX)
11084 -e '/version_string/!d' \
11085 -e 's/[^0-9.]*\([0-9.]*\).*/\1/' \
11087 version.c` > .fname
11088 -rm -rf `cat .fname`
11090 ln $(SRCS) $(AUX) `cat .fname`
11091 tar chZf `cat .fname`.tar.Z `cat .fname`
11092 -rm -rf `cat .fname` .fname
11096 tar.zoo: $(SRCS) $(AUX)
11100 for X in $(SRCS) $(AUX) ; do \
11102 sed 's/$$/^M/' $$X \
11103 > tmp.dir/$$X ; done
11104 cd tmp.dir ; zoo aM ../tar.zoo *
11113 @node Concept Index, Name Index, GNU Free Documentation License, Top
11114 @unnumbered Index of Concepts
11118 @node Name Index, , Concept Index, Top
11119 @unnumbered Index of Functions, Variables, & Directives