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.2 2002/07/09 06:35:56 psmith Exp $
13 @set UPDATED 08 July 2002
14 @set UPDATE-MONTH July 2002
15 @comment The ISBN number might need to change on next publication.
16 @set ISBN 1-882114-81-7 @c From Brian Youmans <3diff@gnu.org>, 25 Apr 2000
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, 1998, 1999, 2000, 2002
42 Free Software Foundation, Inc.
44 Permission is granted to copy, distribute and/or modify this document
45 under the terms of the GNU Free Documentation License, Version 1.1 or
46 any later version published by the Free Software Foundation; with no
47 Invariant Sections, with no Front-Cover Texts, and with no Back-Cover
48 Texts. A copy of the license is included in the section entitled
49 ``GNU Free Documentation License''.
53 @shorttitlepage GNU Make
57 @subtitle A Program for Directing Recompilation
58 @subtitle GNU @code{make} Version @value{VERSION}
59 @subtitle @value{UPDATE-MONTH}
60 @author Richard M. Stallman, Roland McGrath, Paul Smith
62 @vskip 0pt plus 1filll
63 Copyright @copyright{} 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
64 1996, 1997, 1998, 1999, 2000, 2002 Free Software Foundation, Inc.
66 Published by the Free Software Foundation @*
67 59 Temple Place -- Suite 330, @*
68 Boston, MA 02111-1307 USA @*
71 Maintenance and updates since Version 3.76 by Paul D. Smith.
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 no
76 Invariant Sections, with no Front-Cover Texts, and with no Back-Cover
77 Texts. A copy of the license is included in the section entitled
78 ``GNU Free Documentation License''.
80 Cover art by Etienne Suvasa.
85 @node Top, Overview, (dir), (dir)
88 The GNU @code{make} utility automatically determines which pieces of a
89 large program need to be recompiled, and issues the commands to
90 recompile them.@refill
92 This edition of the @cite{GNU Make Manual},
93 last updated @value{UPDATED},
94 documents GNU @code{make} Version @value{VERSION}.@refill
96 This manual describes @code{make} and contains the following chapters:@refill
100 * Overview:: Overview of @code{make}.
101 * Introduction:: An introduction to @code{make}.
102 * Makefiles:: Makefiles tell @code{make} what to do.
103 * Rules:: Rules describe when a file must be remade.
104 * Commands:: Commands say how to remake a file.
105 * Using Variables:: You can use variables to avoid repetition.
106 * Conditionals:: Use or ignore parts of the makefile based
107 on the values of variables.
108 * Functions:: Many powerful ways to manipulate text.
109 * Invoking make: Running. How to invoke @code{make} on the command line.
110 * Implicit Rules:: Use implicit rules to treat many files alike,
111 based on their file names.
112 * Archives:: How @code{make} can update library archives.
113 * Features:: Features GNU @code{make} has over other @code{make}s.
114 * Missing:: What GNU @code{make} lacks from other @code{make}s.
115 * Makefile Conventions:: Conventions for writing makefiles for
117 * Quick Reference:: A quick reference for experienced users.
118 * Error Messages:: A list of common errors generated by @code{make}.
119 * Complex Makefile:: A real example of a straightforward,
120 but nontrivial, makefile.
122 * GNU Free Documentation License:: License for copying this manual
123 * Concept Index:: Index of Concepts
124 * Name Index:: Index of Functions, Variables, & Directives
127 --- The Detailed Node Listing ---
129 Overview of @code{make}
131 * Preparing:: Preparing and Running Make
132 * Reading:: On Reading this Text
133 * Bugs:: Problems and Bugs
135 An Introduction to Makefiles
137 * Rule Introduction:: What a rule looks like.
138 * Simple Makefile:: A Simple Makefile
139 * How Make Works:: How @code{make} Processes This Makefile
140 * Variables Simplify:: Variables Make Makefiles Simpler
141 * make Deduces:: Letting @code{make} Deduce the Commands
142 * Combine By Prerequisite:: Another Style of Makefile
143 * Cleanup:: Rules for Cleaning the Directory
147 * Makefile Contents:: What makefiles contain.
148 * Makefile Names:: How to name your makefile.
149 * Include:: How one makefile can use another makefile.
150 * MAKEFILES Variable:: The environment can specify extra makefiles.
151 * MAKEFILE_LIST Variable:: Discover which makefiles have been read.
152 * Remaking Makefiles:: How makefiles get remade.
153 * Overriding Makefiles:: How to override part of one makefile
154 with another makefile.
155 * Reading Makefiles:: How makefiles are parsed.
159 * Rule Example:: An example explained.
160 * Rule Syntax:: General syntax explained.
161 * Prerequisite Types:: There are two types of prerequisites.
162 * Wildcards:: Using wildcard characters such as `*'.
163 * Directory Search:: Searching other directories for source files.
164 * Phony Targets:: Using a target that is not a real file's name.
165 * Force Targets:: You can use a target without commands
166 or prerequisites to mark other
168 * Empty Targets:: When only the date matters and the
170 * Special Targets:: Targets with special built-in meanings.
171 * Multiple Targets:: When to make use of several targets in a rule.
172 * Multiple Rules:: How to use several rules with the same target.
173 * Static Pattern:: Static pattern rules apply to multiple targets
174 and can vary the prerequisites according to
176 * Double-Colon:: How to use a special kind of rule to allow
177 several independent rules for one target.
178 * Automatic Prerequisites:: How to automatically generate rules giving
179 prerequisites from source files themselves.
181 Using Wildcard Characters in File Names
183 * Wildcard Examples:: Several examples
184 * Wildcard Pitfall:: Problems to avoid.
185 * Wildcard Function:: How to cause wildcard expansion where
186 it does not normally take place.
188 Searching Directories for Prerequisites
190 * General Search:: Specifying a search path that applies
191 to every prerequisite.
192 * Selective Search:: Specifying a search path
193 for a specified class of names.
194 * Search Algorithm:: When and how search paths are applied.
195 * Commands/Search:: How to write shell commands that work together
197 * Implicit/Search:: How search paths affect implicit rules.
198 * Libraries/Search:: Directory search for link libraries.
202 * Static Usage:: The syntax of static pattern rules.
203 * Static versus Implicit:: When are they better than implicit rules?
205 Writing the Commands in Rules
207 * Echoing:: How to control when commands are echoed.
208 * Execution:: How commands are executed.
209 * Parallel:: How commands can be executed in parallel.
210 * Errors:: What happens after a command execution error.
211 * Interrupts:: What happens when a command is interrupted.
212 * Recursion:: Invoking @code{make} from makefiles.
213 * Sequences:: Defining canned sequences of commands.
214 * Empty Commands:: Defining useful, do-nothing commands.
216 Recursive Use of @code{make}
218 * MAKE Variable:: The special effects of using @samp{$(MAKE)}.
219 * Variables/Recursion:: How to communicate variables to a sub-@code{make}.
220 * Options/Recursion:: How to communicate options to a sub-@code{make}.
221 * -w Option:: How the @samp{-w} or @samp{--print-directory} option
222 helps debug use of recursive @code{make} commands.
226 * Reference:: How to use the value of a variable.
227 * Flavors:: Variables come in two flavors.
228 * Advanced:: Advanced features for referencing a variable.
229 * Values:: All the ways variables get their values.
230 * Setting:: How to set a variable in the makefile.
231 * Appending:: How to append more text to the old value
233 * Override Directive:: How to set a variable in the makefile even if
234 the user has set it with a command argument.
235 * Defining:: An alternate way to set a variable
236 to a verbatim string.
237 * Environment:: Variable values can come from the environment.
238 * Target-specific:: Variable values can be defined on a per-target
240 * Pattern-specific:: Target-specific variable values can be applied
241 to a group of targets that match a pattern.
243 Advanced Features for Reference to Variables
245 * Substitution Refs:: Referencing a variable with
246 substitutions on the value.
247 * Computed Names:: Computing the name of the variable to refer to.
249 Conditional Parts of Makefiles
251 * Conditional Example:: Example of a conditional
252 * Conditional Syntax:: The syntax of conditionals.
253 * Testing Flags:: Conditionals that test flags.
255 Functions for Transforming Text
257 * Syntax of Functions:: How to write a function call.
258 * Text Functions:: General-purpose text manipulation functions.
259 * File Name Functions:: Functions for manipulating file names.
260 * Foreach Function:: Repeat some text with controlled variation.
261 * If Function:: Conditionally expand a value.
262 * Call Function:: Expand a user-defined function.
263 * Value Function:: Return the un-expanded value of a variable.
264 * Eval Function:: Evaluate the arguments as makefile syntax.
265 * Origin Function:: Find where a variable got its value.
266 * Shell Function:: Substitute the output of a shell command.
267 * Make Control Functions:: Functions that control how make runs.
269 How to Run @code{make}
271 * Makefile Arguments:: How to specify which makefile to use.
272 * Goals:: How to use goal arguments to specify which
273 parts of the makefile to use.
274 * Instead of Execution:: How to use mode flags to specify what
275 kind of thing to do with the commands
276 in the makefile other than simply
278 * Avoiding Compilation:: How to avoid recompiling certain files.
279 * Overriding:: How to override a variable to specify
280 an alternate compiler and other things.
281 * Testing:: How to proceed past some errors, to
283 * Options Summary:: Summary of Options
287 * Using Implicit:: How to use an existing implicit rule
288 to get the commands for updating a file.
289 * Catalogue of Rules:: A list of built-in implicit rules.
290 * Implicit Variables:: How to change what predefined rules do.
291 * Chained Rules:: How to use a chain of implicit rules.
292 * Pattern Rules:: How to define new implicit rules.
293 * Last Resort:: How to defining commands for rules
294 which cannot find any.
295 * Suffix Rules:: The old-fashioned style of implicit rule.
296 * Implicit Rule Search:: The precise algorithm for applying
299 Defining and Redefining Pattern Rules
301 * Pattern Intro:: An introduction to pattern rules.
302 * Pattern Examples:: Examples of pattern rules.
303 * Automatic:: How to use automatic variables in the
304 commands of implicit rules.
305 * Pattern Match:: How patterns match.
306 * Match-Anything Rules:: Precautions you should take prior to
307 defining rules that can match any
308 target file whatever.
309 * Canceling Rules:: How to override or cancel built-in rules.
311 Using @code{make} to Update Archive Files
313 * Archive Members:: Archive members as targets.
314 * Archive Update:: The implicit rule for archive member targets.
315 * Archive Pitfalls:: Dangers to watch out for when using archives.
316 * Archive Suffix Rules:: You can write a special kind of suffix rule
317 for updating archives.
319 Implicit Rule for Archive Member Targets
321 * Archive Symbols:: How to update archive symbol directories.
325 * Makefile Basics:: General Conventions for Makefiles
326 * Utilities in Makefiles:: Utilities in Makefiles
327 * Command Variables:: Variables for Specifying Commands
328 * Directory Variables:: Variables for Installation Directories
329 * Standard Targets:: Standard Targets for Users
330 * Install Command Categories:: Three categories of commands in the `install'
337 @node Overview, Introduction, Top, Top
338 @comment node-name, next, previous, up
339 @chapter Overview of @code{make}
341 The @code{make} utility automatically determines which pieces of a large
342 program need to be recompiled, and issues commands to recompile them.
343 This manual describes GNU @code{make}, which was implemented by Richard
344 Stallman and Roland McGrath. Development since Version 3.76 has been
345 handled by Paul Smith.
347 GNU @code{make} conforms to section 6.2 of @cite{IEEE Standard
348 1003.2-1992} (POSIX.2).
350 @cindex IEEE Standard 1003.2
351 @cindex standards conformance
353 Our examples show C programs, since they are most common, but you can use
354 @code{make} with any programming language whose compiler can be run with a
355 shell command. Indeed, @code{make} is not limited to programs. You can
356 use it to describe any task where some files must be updated automatically
357 from others whenever the others change.
360 * Preparing:: Preparing and Running Make
361 * Reading:: On Reading this Text
362 * Bugs:: Problems and Bugs
365 @node Preparing, Reading, Overview, Overview
367 @heading Preparing and Running Make
370 To prepare to use @code{make}, you must write a file called
371 the @dfn{makefile} that describes the relationships among files
372 in your program and provides commands for updating each file.
373 In a program, typically, the executable file is updated from object
374 files, which are in turn made by compiling source files.@refill
376 Once a suitable makefile exists, each time you change some source files,
377 this simple shell command:
384 suffices to perform all necessary recompilations. The @code{make} program
385 uses the makefile data base and the last-modification times of the files to
386 decide which of the files need to be updated. For each of those files, it
387 issues the commands recorded in the data base.
389 You can provide command line arguments to @code{make} to control which
390 files should be recompiled, or how. @xref{Running, ,How to Run
393 @node Reading, Bugs, Preparing, Overview
394 @section How to Read This Manual
396 If you are new to @code{make}, or are looking for a general
397 introduction, read the first few sections of each chapter, skipping the
398 later sections. In each chapter, the first few sections contain
399 introductory or general information and the later sections contain
400 specialized or technical information.
402 The exception is the second chapter, @ref{Introduction, ,An
403 Introduction to Makefiles}, all of which is introductory.
406 The exception is @ref{Introduction, ,An Introduction to Makefiles},
407 all of which is introductory.
410 If you are familiar with other @code{make} programs, see @ref{Features,
411 ,Features of GNU @code{make}}, which lists the enhancements GNU
412 @code{make} has, and @ref{Missing, ,Incompatibilities and Missing
413 Features}, which explains the few things GNU @code{make} lacks that
416 For a quick summary, see @ref{Options Summary}, @ref{Quick Reference},
417 and @ref{Special Targets}.
419 @node Bugs, , Reading, Overview
420 @section Problems and Bugs
421 @cindex reporting bugs
422 @cindex bugs, reporting
423 @cindex problems and bugs, reporting
425 If you have problems with GNU @code{make} or think you've found a bug,
426 please report it to the developers; we cannot promise to do anything but
427 we might well want to fix it.
429 Before reporting a bug, make sure you've actually found a real bug.
430 Carefully reread the documentation and see if it really says you can do
431 what you're trying to do. If it's not clear whether you should be able
432 to do something or not, report that too; it's a bug in the
435 Before reporting a bug or trying to fix it yourself, try to isolate it
436 to the smallest possible makefile that reproduces the problem. Then
437 send us the makefile and the exact results @code{make} gave you. When
438 generating this small makefile, be sure to not use any non-free or
439 unusual tools in your commands: you can almost always emulate what
440 such a tool would do with simple shell commands. Finally, be sure to
441 explain what you expected to occur; this will help us decide whether
442 the problem was really in the documentation.
444 Once you've got a precise problem, please send electronic mail to:
451 Please include the version number of @code{make} you are using. You can
452 get this information with the command @samp{make --version}.
453 Be sure also to include the type of machine and operating system you are
456 @node Introduction, Makefiles, Overview, Top
457 @comment node-name, next, previous, up
458 @chapter An Introduction to Makefiles
460 You need a file called a @dfn{makefile} to tell @code{make} what to do.
461 Most often, the makefile tells @code{make} how to compile and link a
465 In this chapter, we will discuss a simple makefile that describes how to
466 compile and link a text editor which consists of eight C source files
467 and three header files. The makefile can also tell @code{make} how to
468 run miscellaneous commands when explicitly asked (for example, to remove
469 certain files as a clean-up operation). To see a more complex example
470 of a makefile, see @ref{Complex Makefile}.
472 When @code{make} recompiles the editor, each changed C source file
473 must be recompiled. If a header file has changed, each C source file
474 that includes the header file must be recompiled to be safe. Each
475 compilation produces an object file corresponding to the source file.
476 Finally, if any source file has been recompiled, all the object files,
477 whether newly made or saved from previous compilations, must be linked
478 together to produce the new executable editor.
479 @cindex recompilation
483 * Rule Introduction:: What a rule looks like.
484 * Simple Makefile:: A Simple Makefile
485 * How Make Works:: How @code{make} Processes This Makefile
486 * Variables Simplify:: Variables Make Makefiles Simpler
487 * make Deduces:: Letting @code{make} Deduce the Commands
488 * Combine By Prerequisite:: Another Style of Makefile
489 * Cleanup:: Rules for Cleaning the Directory
492 @node Rule Introduction, Simple Makefile, Introduction, Introduction
493 @comment node-name, next, previous, up
494 @section What a Rule Looks Like
495 @cindex rule, introduction to
496 @cindex makefile rule parts
497 @cindex parts of makefile rule
499 A simple makefile consists of ``rules'' with the following shape:
501 @cindex targets, introduction to
502 @cindex prerequisites, introduction to
503 @cindex commands, introduction to
506 @var{target} @dots{} : @var{prerequisites} @dots{}
513 A @dfn{target} is usually the name of a file that is generated by a
514 program; examples of targets are executable or object files. A target
515 can also be the name of an action to carry out, such as @samp{clean}
516 (@pxref{Phony Targets}).
518 A @dfn{prerequisite} is a file that is used as input to create the
519 target. A target often depends on several files.
521 @cindex tabs in rules
522 A @dfn{command} is an action that @code{make} carries out.
523 A rule may have more than one command, each on its own line.
524 @strong{Please note:} you need to put a tab character at the beginning of
525 every command line! This is an obscurity that catches the unwary.
527 Usually a command is in a rule with prerequisites and serves to create a
528 target file if any of the prerequisites change. However, the rule that
529 specifies commands for the target need not have prerequisites. For
530 example, the rule containing the delete command associated with the
531 target @samp{clean} does not have prerequisites.
533 A @dfn{rule}, then, explains how and when to remake certain files
534 which are the targets of the particular rule. @code{make} carries out
535 the commands on the prerequisites to create or update the target. A
536 rule can also explain how and when to carry out an action.
537 @xref{Rules, , Writing Rules}.
539 A makefile may contain other text besides rules, but a simple makefile
540 need only contain rules. Rules may look somewhat more complicated
541 than shown in this template, but all fit the pattern more or less.
543 @node Simple Makefile, How Make Works, Rule Introduction, Introduction
544 @section A Simple Makefile
545 @cindex simple makefile
546 @cindex makefile, simple
548 Here is a straightforward makefile that describes the way an
549 executable file called @code{edit} depends on eight object files
550 which, in turn, depend on eight C source and three header files.
552 In this example, all the C files include @file{defs.h}, but only those
553 defining editing commands include @file{command.h}, and only low
554 level files that change the editor buffer include @file{buffer.h}.
558 edit : main.o kbd.o command.o display.o \
559 insert.o search.o files.o utils.o
560 cc -o edit main.o kbd.o command.o display.o \
561 insert.o search.o files.o utils.o
563 main.o : main.c defs.h
565 kbd.o : kbd.c defs.h command.h
567 command.o : command.c defs.h command.h
569 display.o : display.c defs.h buffer.h
571 insert.o : insert.c defs.h buffer.h
573 search.o : search.c defs.h buffer.h
575 files.o : files.c defs.h buffer.h command.h
577 utils.o : utils.c defs.h
580 rm edit main.o kbd.o command.o display.o \
581 insert.o search.o files.o utils.o
586 We split each long line into two lines using backslash-newline; this is
587 like using one long line, but is easier to read.
588 @cindex continuation lines
589 @cindex @code{\} (backslash), for continuation lines
590 @cindex backslash (@code{\}), for continuation lines
591 @cindex quoting newline, in makefile
592 @cindex newline, quoting, in makefile
594 To use this makefile to create the executable file called @file{edit},
601 To use this makefile to delete the executable file and all the object
602 files from the directory, type:
608 In the example makefile, the targets include the executable file
609 @samp{edit}, and the object files @samp{main.o} and @samp{kbd.o}. The
610 prerequisites are files such as @samp{main.c} and @samp{defs.h}.
611 In fact, each @samp{.o} file is both a target and a prerequisite.
612 Commands include @w{@samp{cc -c main.c}} and @w{@samp{cc -c kbd.c}}.
614 When a target is a file, it needs to be recompiled or relinked if any
615 of its prerequisites change. In addition, any prerequisites that are
616 themselves automatically generated should be updated first. In this
617 example, @file{edit} depends on each of the eight object files; the
618 object file @file{main.o} depends on the source file @file{main.c} and
619 on the header file @file{defs.h}.
621 A shell command follows each line that contains a target and
622 prerequisites. These shell commands say how to update the target file.
623 A tab character must come at the beginning of every command line to
624 distinguish commands lines from other lines in the makefile. (Bear in
625 mind that @code{make} does not know anything about how the commands
626 work. It is up to you to supply commands that will update the target
627 file properly. All @code{make} does is execute the commands in the rule
628 you have specified when the target file needs to be updated.)
629 @cindex shell command
631 The target @samp{clean} is not a file, but merely the name of an
634 do not want to carry out the actions in this rule, @samp{clean} is not a prerequisite of any other rule.
635 Consequently, @code{make} never does anything with it unless you tell
636 it specifically. Note that this rule not only is not a prerequisite, it
637 also does not have any prerequisites, so the only purpose of the rule
638 is to run the specified commands. Targets that do not refer to files
639 but are just actions are called @dfn{phony targets}. @xref{Phony
640 Targets}, for information about this kind of target. @xref{Errors, ,
641 Errors in Commands}, to see how to cause @code{make} to ignore errors
642 from @code{rm} or any other command.
643 @cindex @code{clean} target
644 @cindex @code{rm} (shell command)
646 @node How Make Works, Variables Simplify, Simple Makefile, Introduction
647 @comment node-name, next, previous, up
648 @section How @code{make} Processes a Makefile
649 @cindex processing a makefile
650 @cindex makefile, how @code{make} processes
652 By default, @code{make} starts with the first target (not targets whose
653 names start with @samp{.}). This is called the @dfn{default goal}.
654 (@dfn{Goals} are the targets that @code{make} strives ultimately to
655 update. @xref{Goals, , Arguments to Specify the Goals}.)
657 @cindex goal, default
660 In the simple example of the previous section, the default goal is to
661 update the executable program @file{edit}; therefore, we put that rule
664 Thus, when you give the command:
671 @code{make} reads the makefile in the current directory and begins by
672 processing the first rule. In the example, this rule is for relinking
673 @file{edit}; but before @code{make} can fully process this rule, it
674 must process the rules for the files that @file{edit} depends on,
675 which in this case are the object files. Each of these files is
676 processed according to its own rule. These rules say to update each
677 @samp{.o} file by compiling its source file. The recompilation must
678 be done if the source file, or any of the header files named as
679 prerequisites, is more recent than the object file, or if the object
682 The other rules are processed because their targets appear as
683 prerequisites of the goal. If some other rule is not depended on by the
684 goal (or anything it depends on, etc.), that rule is not processed,
685 unless you tell @code{make} to do so (with a command such as
686 @w{@code{make clean}}).
688 Before recompiling an object file, @code{make} considers updating its
689 prerequisites, the source file and header files. This makefile does not
690 specify anything to be done for them---the @samp{.c} and @samp{.h} files
691 are not the targets of any rules---so @code{make} does nothing for these
692 files. But @code{make} would update automatically generated C programs,
693 such as those made by Bison or Yacc, by their own rules at this time.
695 After recompiling whichever object files need it, @code{make} decides
696 whether to relink @file{edit}. This must be done if the file
697 @file{edit} does not exist, or if any of the object files are newer than
698 it. If an object file was just recompiled, it is now newer than
699 @file{edit}, so @file{edit} is relinked.
702 Thus, if we change the file @file{insert.c} and run @code{make},
703 @code{make} will compile that file to update @file{insert.o}, and then
704 link @file{edit}. If we change the file @file{command.h} and run
705 @code{make}, @code{make} will recompile the object files @file{kbd.o},
706 @file{command.o} and @file{files.o} and then link the file @file{edit}.
708 @node Variables Simplify, make Deduces, How Make Works, Introduction
709 @section Variables Make Makefiles Simpler
711 @cindex simplifying with variables
713 In our example, we had to list all the object files twice in the rule for
714 @file{edit} (repeated here):
718 edit : main.o kbd.o command.o display.o \
719 insert.o search.o files.o utils.o
720 cc -o edit main.o kbd.o command.o display.o \
721 insert.o search.o files.o utils.o
725 @cindex @code{objects}
726 Such duplication is error-prone; if a new object file is added to the
727 system, we might add it to one list and forget the other. We can eliminate
728 the risk and simplify the makefile by using a variable. @dfn{Variables}
729 allow a text string to be defined once and substituted in multiple places
730 later (@pxref{Using Variables, ,How to Use Variables}).
732 @cindex @code{OBJECTS}
737 It is standard practice for every makefile to have a variable named
738 @code{objects}, @code{OBJECTS}, @code{objs}, @code{OBJS}, @code{obj},
739 or @code{OBJ} which is a list of all object file names. We would
740 define such a variable @code{objects} with a line like this in the
745 objects = main.o kbd.o command.o display.o \
746 insert.o search.o files.o utils.o
751 Then, each place we want to put a list of the object file names, we can
752 substitute the variable's value by writing @samp{$(objects)}
753 (@pxref{Using Variables, ,How to Use Variables}).
755 Here is how the complete simple makefile looks when you use a variable
756 for the object files:
760 objects = main.o kbd.o command.o display.o \
761 insert.o search.o files.o utils.o
764 cc -o edit $(objects)
765 main.o : main.c defs.h
767 kbd.o : kbd.c defs.h command.h
769 command.o : command.c defs.h command.h
771 display.o : display.c defs.h buffer.h
773 insert.o : insert.c defs.h buffer.h
775 search.o : search.c defs.h buffer.h
777 files.o : files.c defs.h buffer.h command.h
779 utils.o : utils.c defs.h
786 @node make Deduces, Combine By Prerequisite, Variables Simplify, Introduction
787 @section Letting @code{make} Deduce the Commands
788 @cindex deducing commands (implicit rules)
789 @cindex implicit rule, introduction to
790 @cindex rule, implicit, introduction to
792 It is not necessary to spell out the commands for compiling the individual
793 C source files, because @code{make} can figure them out: it has an
794 @dfn{implicit rule} for updating a @samp{.o} file from a correspondingly
795 named @samp{.c} file using a @samp{cc -c} command. For example, it will
796 use the command @samp{cc -c main.c -o main.o} to compile @file{main.c} into
797 @file{main.o}. We can therefore omit the commands from the rules for the
798 object files. @xref{Implicit Rules, ,Using Implicit Rules}.@refill
800 When a @samp{.c} file is used automatically in this way, it is also
801 automatically added to the list of prerequisites. We can therefore omit
802 the @samp{.c} files from the prerequisites, provided we omit the commands.
804 Here is the entire example, with both of these changes, and a variable
805 @code{objects} as suggested above:
809 objects = main.o kbd.o command.o display.o \
810 insert.o search.o files.o utils.o
813 cc -o edit $(objects)
816 kbd.o : defs.h command.h
817 command.o : defs.h command.h
818 display.o : defs.h buffer.h
819 insert.o : defs.h buffer.h
820 search.o : defs.h buffer.h
821 files.o : defs.h buffer.h command.h
831 This is how we would write the makefile in actual practice. (The
832 complications associated with @samp{clean} are described elsewhere.
833 See @ref{Phony Targets}, and @ref{Errors, ,Errors in Commands}.)
835 Because implicit rules are so convenient, they are important. You
836 will see them used frequently.@refill
838 @node Combine By Prerequisite, Cleanup, make Deduces, Introduction
839 @section Another Style of Makefile
840 @cindex combining rules by prerequisite
842 When the objects of a makefile are created only by implicit rules, an
843 alternative style of makefile is possible. In this style of makefile,
844 you group entries by their prerequisites instead of by their targets.
845 Here is what one looks like:
849 objects = main.o kbd.o command.o display.o \
850 insert.o search.o files.o utils.o
853 cc -o edit $(objects)
856 kbd.o command.o files.o : command.h
857 display.o insert.o search.o files.o : buffer.h
862 Here @file{defs.h} is given as a prerequisite of all the object files;
863 @file{command.h} and @file{buffer.h} are prerequisites of the specific
864 object files listed for them.
866 Whether this is better is a matter of taste: it is more compact, but some
867 people dislike it because they find it clearer to put all the information
868 about each target in one place.
870 @node Cleanup, , Combine By Prerequisite, Introduction
871 @section Rules for Cleaning the Directory
873 @cindex removing, to clean up
875 Compiling a program is not the only thing you might want to write rules
876 for. Makefiles commonly tell how to do a few other things besides
877 compiling a program: for example, how to delete all the object files
878 and executables so that the directory is @samp{clean}.
880 @cindex @code{clean} target
882 could write a @code{make} rule for cleaning our example editor:
891 In practice, we might want to write the rule in a somewhat more
892 complicated manner to handle unanticipated situations. We would do this:
903 This prevents @code{make} from getting confused by an actual file
904 called @file{clean} and causes it to continue in spite of errors from
905 @code{rm}. (See @ref{Phony Targets}, and @ref{Errors, ,Errors in
909 A rule such as this should not be placed at the beginning of the
910 makefile, because we do not want it to run by default! Thus, in the
911 example makefile, we want the rule for @code{edit}, which recompiles
912 the editor, to remain the default goal.
914 Since @code{clean} is not a prerequisite of @code{edit}, this rule will not
915 run at all if we give the command @samp{make} with no arguments. In
916 order to make the rule run, we have to type @samp{make clean}.
917 @xref{Running, ,How to Run @code{make}}.
919 @node Makefiles, Rules, Introduction, Top
920 @chapter Writing Makefiles
922 @cindex makefile, how to write
923 The information that tells @code{make} how to recompile a system comes from
924 reading a data base called the @dfn{makefile}.
927 * Makefile Contents:: What makefiles contain.
928 * Makefile Names:: How to name your makefile.
929 * Include:: How one makefile can use another makefile.
930 * MAKEFILES Variable:: The environment can specify extra makefiles.
931 * MAKEFILE_LIST Variable:: Discover which makefiles have been read.
932 * Remaking Makefiles:: How makefiles get remade.
933 * Overriding Makefiles:: How to override part of one makefile
934 with another makefile.
935 * Reading Makefiles:: How makefiles are parsed.
938 @node Makefile Contents, Makefile Names, Makefiles, Makefiles
939 @section What Makefiles Contain
941 Makefiles contain five kinds of things: @dfn{explicit rules},
942 @dfn{implicit rules}, @dfn{variable definitions}, @dfn{directives},
943 and @dfn{comments}. Rules, variables, and directives are described at
944 length in later chapters.@refill
947 @cindex rule, explicit, definition of
948 @cindex explicit rule, definition of
950 An @dfn{explicit rule} says when and how to remake one or more files,
951 called the rule's targets. It lists the other files that the targets
952 depend on, call the @dfn{prerequisites} of the target, and may also give
953 commands to use to create or update the targets. @xref{Rules, ,Writing
956 @cindex rule, implicit, definition of
957 @cindex implicit rule, definition of
959 An @dfn{implicit rule} says when and how to remake a class of files
960 based on their names. It describes how a target may depend on a file
961 with a name similar to the target and gives commands to create or
962 update such a target. @xref{Implicit Rules, ,Using Implicit Rules}.
964 @cindex variable definition
966 A @dfn{variable definition} is a line that specifies a text string
967 value for a variable that can be substituted into the text later. The
968 simple makefile example shows a variable definition for @code{objects}
969 as a list of all object files (@pxref{Variables Simplify, , Variables
970 Make Makefiles Simpler}).
974 A @dfn{directive} is a command for @code{make} to do something special while
975 reading the makefile. These include:
979 Reading another makefile (@pxref{Include, ,Including Other Makefiles}).
982 Deciding (based on the values of variables) whether to use or
983 ignore a part of the makefile (@pxref{Conditionals, ,Conditional Parts of Makefiles}).
986 Defining a variable from a verbatim string containing multiple lines
987 (@pxref{Defining, ,Defining Variables Verbatim}).
990 @cindex comments, in makefile
991 @cindex @code{#} (comments), in makefile
993 @samp{#} in a line of a makefile starts a @dfn{comment}. It and the rest of
994 the line are ignored, except that a trailing backslash not escaped by
995 another backslash will continue the comment across multiple lines.
996 Comments may appear on any of the lines in the makefile, except within a
997 @code{define} directive, and perhaps within commands (where the shell
998 decides what is a comment). A line containing just a comment (with
999 perhaps spaces before it) is effectively blank, and is ignored.@refill
1002 @node Makefile Names, Include, Makefile Contents, Makefiles
1003 @section What Name to Give Your Makefile
1004 @cindex makefile name
1005 @cindex name of makefile
1006 @cindex default makefile name
1007 @cindex file name of makefile
1009 @c following paragraph rewritten to avoid overfull hbox
1010 By default, when @code{make} looks for the makefile, it tries the
1011 following names, in order: @file{GNUmakefile}, @file{makefile}
1012 and @file{Makefile}.@refill
1017 @cindex @code{README}
1018 Normally you should call your makefile either @file{makefile} or
1019 @file{Makefile}. (We recommend @file{Makefile} because it appears
1020 prominently near the beginning of a directory listing, right near other
1021 important files such as @file{README}.) The first name checked,
1022 @file{GNUmakefile}, is not recommended for most makefiles. You should
1023 use this name if you have a makefile that is specific to GNU
1024 @code{make}, and will not be understood by other versions of
1025 @code{make}. Other @code{make} programs look for @file{makefile} and
1026 @file{Makefile}, but not @file{GNUmakefile}.
1028 If @code{make} finds none of these names, it does not use any makefile.
1029 Then you must specify a goal with a command argument, and @code{make}
1030 will attempt to figure out how to remake it using only its built-in
1031 implicit rules. @xref{Implicit Rules, ,Using Implicit Rules}.
1034 @cindex @code{--file}
1035 @cindex @code{--makefile}
1036 If you want to use a nonstandard name for your makefile, you can specify
1037 the makefile name with the @samp{-f} or @samp{--file} option. The
1038 arguments @w{@samp{-f @var{name}}} or @w{@samp{--file=@var{name}}} tell
1039 @code{make} to read the file @var{name} as the makefile. If you use
1040 more than one @samp{-f} or @samp{--file} option, you can specify several
1041 makefiles. All the makefiles are effectively concatenated in the order
1042 specified. The default makefile names @file{GNUmakefile},
1043 @file{makefile} and @file{Makefile} are not checked automatically if you
1044 specify @samp{-f} or @samp{--file}.@refill
1045 @cindex specifying makefile name
1046 @cindex makefile name, how to specify
1047 @cindex name of makefile, how to specify
1048 @cindex file name of makefile, how to specify
1050 @node Include, MAKEFILES Variable, Makefile Names, Makefiles
1051 @section Including Other Makefiles
1052 @cindex including other makefiles
1053 @cindex makefile, including
1056 The @code{include} directive tells @code{make} to suspend reading the
1057 current makefile and read one or more other makefiles before continuing.
1058 The directive is a line in the makefile that looks like this:
1061 include @var{filenames}@dots{}
1065 @var{filenames} can contain shell file name patterns.
1066 @cindex shell file name pattern (in @code{include})
1067 @cindex shell wildcards (in @code{include})
1068 @cindex wildcard, in @code{include}
1070 Extra spaces are allowed and ignored at the beginning of the line, but
1071 a tab is not allowed. (If the line begins with a tab, it will be
1072 considered a command line.) Whitespace is required between
1073 @code{include} and the file names, and between file names; extra
1074 whitespace is ignored there and at the end of the directive. A
1075 comment starting with @samp{#} is allowed at the end of the line. If
1076 the file names contain any variable or function references, they are
1077 expanded. @xref{Using Variables, ,How to Use Variables}.
1079 For example, if you have three @file{.mk} files, @file{a.mk},
1080 @file{b.mk}, and @file{c.mk}, and @code{$(bar)} expands to
1081 @code{bish bash}, then the following expression
1084 include foo *.mk $(bar)
1090 include foo a.mk b.mk c.mk bish bash
1093 When @code{make} processes an @code{include} directive, it suspends
1094 reading of the containing makefile and reads from each listed file in
1095 turn. When that is finished, @code{make} resumes reading the
1096 makefile in which the directive appears.
1098 One occasion for using @code{include} directives is when several programs,
1099 handled by individual makefiles in various directories, need to use a
1100 common set of variable definitions
1101 (@pxref{Setting, ,Setting Variables}) or pattern rules
1102 (@pxref{Pattern Rules, ,Defining and Redefining Pattern Rules}).
1104 Another such occasion is when you want to generate prerequisites from
1105 source files automatically; the prerequisites can be put in a file that
1106 is included by the main makefile. This practice is generally cleaner
1107 than that of somehow appending the prerequisites to the end of the main
1108 makefile as has been traditionally done with other versions of
1109 @code{make}. @xref{Automatic Prerequisites}.
1110 @cindex prerequisites, automatic generation
1111 @cindex automatic generation of prerequisites
1112 @cindex generating prerequisites automatically
1115 @cindex @code{--include-dir}
1116 @cindex included makefiles, default directories
1117 @cindex default directories for included makefiles
1118 @findex /usr/gnu/include
1119 @findex /usr/local/include
1120 @findex /usr/include
1121 If the specified name does not start with a slash, and the file is not
1122 found in the current directory, several other directories are searched.
1123 First, any directories you have specified with the @samp{-I} or
1124 @samp{--include-dir} option are searched
1125 (@pxref{Options Summary, ,Summary of Options}).
1126 Then the following directories (if they exist)
1127 are searched, in this order:
1128 @file{@var{prefix}/include} (normally @file{/usr/local/include}
1129 @footnote{GNU Make compiled for MS-DOS and MS-Windows behaves as if
1130 @var{prefix} has been defined to be the root of the DJGPP tree
1132 @file{/usr/gnu/include},
1133 @file{/usr/local/include}, @file{/usr/include}.
1135 If an included makefile cannot be found in any of these directories, a
1136 warning message is generated, but it is not an immediately fatal error;
1137 processing of the makefile containing the @code{include} continues.
1138 Once it has finished reading makefiles, @code{make} will try to remake
1139 any that are out of date or don't exist.
1140 @xref{Remaking Makefiles, ,How Makefiles Are Remade}.
1141 Only after it has tried to find a way to remake a makefile and failed,
1142 will @code{make} diagnose the missing makefile as a fatal error.
1144 If you want @code{make} to simply ignore a makefile which does not exist
1145 and cannot be remade, with no error message, use the @w{@code{-include}}
1146 directive instead of @code{include}, like this:
1149 -include @var{filenames}@dots{}
1152 This acts like @code{include} in every way except that there is no
1153 error (not even a warning) if any of the @var{filenames} do not exist.
1154 For compatibility with some other @code{make} implementations,
1155 @code{sinclude} is another name for @w{@code{-include}}.
1157 @node MAKEFILES Variable, MAKEFILE_LIST Variable, Include, Makefiles
1158 @section The Variable @code{MAKEFILES}
1159 @cindex makefile, and @code{MAKEFILES} variable
1160 @cindex including (@code{MAKEFILES} variable)
1163 If the environment variable @code{MAKEFILES} is defined, @code{make}
1164 considers its value as a list of names (separated by whitespace) of
1165 additional makefiles to be read before the others. This works much like
1166 the @code{include} directive: various directories are searched for those
1167 files (@pxref{Include, ,Including Other Makefiles}). In addition, the
1168 default goal is never taken from one of these makefiles and it is not an
1169 error if the files listed in @code{MAKEFILES} are not found.@refill
1171 @cindex recursion, and @code{MAKEFILES} variable
1172 The main use of @code{MAKEFILES} is in communication between recursive
1173 invocations of @code{make} (@pxref{Recursion, ,Recursive Use of
1174 @code{make}}). It usually is not desirable to set the environment
1175 variable before a top-level invocation of @code{make}, because it is
1176 usually better not to mess with a makefile from outside. However, if
1177 you are running @code{make} without a specific makefile, a makefile in
1178 @code{MAKEFILES} can do useful things to help the built-in implicit
1179 rules work better, such as defining search paths (@pxref{Directory Search}).
1181 Some users are tempted to set @code{MAKEFILES} in the environment
1182 automatically on login, and program makefiles to expect this to be done.
1183 This is a very bad idea, because such makefiles will fail to work if run by
1184 anyone else. It is much better to write explicit @code{include} directives
1185 in the makefiles. @xref{Include, , Including Other Makefiles}.
1187 @node MAKEFILE_LIST Variable, Remaking Makefiles, MAKEFILES Variable, Makefiles
1188 @comment node-name, next, previous, up
1189 @section The Variable @code{MAKEFILE_LIST}
1190 @cindex makefiles, and @code{MAKEFILE_LIST} variable
1191 @cindex including (@code{MAKEFILE_LIST} variable)
1193 As @code{make} reads various makefiles, including any obtained from the
1194 @code{MAKEFILES} variable, the command line, the default files, or
1195 from @code{include} directives, their names will be automatically
1196 appended to the @code{MAKEFILE_LIST} variable. They are added right
1197 before @code{make} begins to parse them.
1199 This means that if the first thing a makefile does is examine the last
1200 word in this variable, it will be the name of the current makefile.
1201 Once the current makefile has used @code{include}, however, the last
1202 word will be the just-included makefile.
1204 If a makefile named @code{Makefile} has this content:
1208 name1 := $(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST))
1212 name2 := $(word $(words $(MAKEFILE_LIST)),$(MAKEFILE_LIST))
1215 @@echo name1 = $(name1)
1216 @@echo name2 = $(name2)
1221 then you would expect to see this output:
1230 @xref{Text Functions}, for more information on the @code{word} and
1231 @code{words} functions used above. @xref{Flavors, The Two Flavors of
1232 Variables}, for more information on simply-expanded (@code{:=})
1233 variable definitions.
1235 @node Remaking Makefiles, Overriding Makefiles, MAKEFILE_LIST Variable, Makefiles
1236 @section How Makefiles Are Remade
1238 @cindex updating makefiles
1239 @cindex remaking makefiles
1240 @cindex makefile, remaking of
1241 Sometimes makefiles can be remade from other files, such as RCS or SCCS
1242 files. If a makefile can be remade from other files, you probably want
1243 @code{make} to get an up-to-date version of the makefile to read in.
1245 To this end, after reading in all makefiles, @code{make} will consider
1246 each as a goal target and attempt to update it. If a makefile has a
1247 rule which says how to update it (found either in that very makefile or
1248 in another one) or if an implicit rule applies to it (@pxref{Implicit
1249 Rules, ,Using Implicit Rules}), it will be updated if necessary. After
1250 all makefiles have been checked, if any have actually been changed,
1251 @code{make} starts with a clean slate and reads all the makefiles over
1252 again. (It will also attempt to update each of them over again, but
1253 normally this will not change them again, since they are already up to
1256 If you know that one or more of your makefiles cannot be remade and you
1257 want to keep @code{make} from performing an implicit rule search on
1258 them, perhaps for efficiency reasons, you can use any normal method of
1259 preventing implicit rule lookup to do so. For example, you can write an
1260 explicit rule with the makefile as the target, and an empty command
1261 string (@pxref{Empty Commands, ,Using Empty Commands}).
1263 If the makefiles specify a double-colon rule to remake a file with
1264 commands but no prerequisites, that file will always be remade
1265 (@pxref{Double-Colon}). In the case of makefiles, a makefile that has a
1266 double-colon rule with commands but no prerequisites will be remade every
1267 time @code{make} is run, and then again after @code{make} starts over
1268 and reads the makefiles in again. This would cause an infinite loop:
1269 @code{make} would constantly remake the makefile, and never do anything
1270 else. So, to avoid this, @code{make} will @strong{not} attempt to
1271 remake makefiles which are specified as targets of a double-colon rule
1272 with commands but no prerequisites.@refill
1274 If you do not specify any makefiles to be read with @samp{-f} or
1275 @samp{--file} options, @code{make} will try the default makefile names;
1276 @pxref{Makefile Names, ,What Name to Give Your Makefile}. Unlike
1277 makefiles explicitly requested with @samp{-f} or @samp{--file} options,
1278 @code{make} is not certain that these makefiles should exist. However,
1279 if a default makefile does not exist but can be created by running
1280 @code{make} rules, you probably want the rules to be run so that the
1281 makefile can be used.
1283 Therefore, if none of the default makefiles exists, @code{make} will try
1284 to make each of them in the same order in which they are searched for
1285 (@pxref{Makefile Names, ,What Name to Give Your Makefile})
1286 until it succeeds in making one, or it runs out of names to try. Note
1287 that it is not an error if @code{make} cannot find or make any makefile;
1288 a makefile is not always necessary.@refill
1290 When you use the @samp{-t} or @samp{--touch} option
1291 (@pxref{Instead of Execution, ,Instead of Executing the Commands}),
1292 you would not want to use an out-of-date makefile to decide which
1293 targets to touch. So the @samp{-t} option has no effect on updating
1294 makefiles; they are really updated even if @samp{-t} is specified.
1295 Likewise, @samp{-q} (or @samp{--question}) and @samp{-n} (or
1296 @samp{--just-print}) do not prevent updating of makefiles, because an
1297 out-of-date makefile would result in the wrong output for other targets.
1298 Thus, @samp{make -f mfile -n foo} will update @file{mfile}, read it in,
1299 and then print the commands to update @file{foo} and its prerequisites
1300 without running them. The commands printed for @file{foo} will be those
1301 specified in the updated contents of @file{mfile}.
1303 However, on occasion you might actually wish to prevent updating of even
1304 the makefiles. You can do this by specifying the makefiles as goals in
1305 the command line as well as specifying them as makefiles. When the
1306 makefile name is specified explicitly as a goal, the options @samp{-t}
1307 and so on do apply to them.
1309 Thus, @samp{make -f mfile -n mfile foo} would read the makefile
1310 @file{mfile}, print the commands needed to update it without actually
1311 running them, and then print the commands needed to update @file{foo}
1312 without running them. The commands for @file{foo} will be those
1313 specified by the existing contents of @file{mfile}.
1315 @node Overriding Makefiles, Reading Makefiles, Remaking Makefiles, Makefiles
1316 @section Overriding Part of Another Makefile
1318 @cindex overriding makefiles
1319 @cindex makefile, overriding
1320 Sometimes it is useful to have a makefile that is mostly just like
1321 another makefile. You can often use the @samp{include} directive to
1322 include one in the other, and add more targets or variable definitions.
1323 However, if the two makefiles give different commands for the same
1324 target, @code{make} will not let you just do this. But there is another way.
1326 @cindex match-anything rule, used to override
1327 In the containing makefile (the one that wants to include the other),
1328 you can use a match-anything pattern rule to say that to remake any
1329 target that cannot be made from the information in the containing
1330 makefile, @code{make} should look in another makefile.
1331 @xref{Pattern Rules}, for more information on pattern rules.
1333 For example, if you have a makefile called @file{Makefile} that says how
1334 to make the target @samp{foo} (and other targets), you can write a
1335 makefile called @file{GNUmakefile} that contains:
1342 @@$(MAKE) -f Makefile $@@
1346 If you say @samp{make foo}, @code{make} will find @file{GNUmakefile},
1347 read it, and see that to make @file{foo}, it needs to run the command
1348 @samp{frobnicate > foo}. If you say @samp{make bar}, @code{make} will
1349 find no way to make @file{bar} in @file{GNUmakefile}, so it will use the
1350 commands from the pattern rule: @samp{make -f Makefile bar}. If
1351 @file{Makefile} provides a rule for updating @file{bar}, @code{make}
1352 will apply the rule. And likewise for any other target that
1353 @file{GNUmakefile} does not say how to make.
1355 The way this works is that the pattern rule has a pattern of just
1356 @samp{%}, so it matches any target whatever. The rule specifies a
1357 prerequisite @file{force}, to guarantee that the commands will be run even
1358 if the target file already exists. We give @file{force} target empty
1359 commands to prevent @code{make} from searching for an implicit rule to
1360 build it---otherwise it would apply the same match-anything rule to
1361 @file{force} itself and create a prerequisite loop!
1363 @node Reading Makefiles, , Overriding Makefiles, Makefiles
1364 @section How @code{make} Reads a Makefile
1365 @cindex reading makefiles
1366 @cindex makefile, parsing
1368 GNU @code{make} does its work in two distinct phases. During the first
1369 phase it reads all the makefiles, included makefiles, etc. and
1370 internalizes all the variables and their values, implicit and explicit
1371 rules, and constructs a dependency graph of all the targets and their
1372 prerequisites. During the second phase, @code{make} uses these internal
1373 structures to determine what targets will need to be rebuilt and to
1374 invoke the rules necessary to do so.
1376 It's important to understand this two-phase approach because it has a
1377 direct impact on how variable and function expansion happens; this is
1378 often a source of some confusion when writing makefiles. Here we will
1379 present a summary of the phases in which expansion happens for different
1380 constructs within the makefile. We say that expansion is
1381 @dfn{immediate} if it happens during the first phase: in this case
1382 @code{make} will expand any variables or functions in that section of a
1383 construct as the makefile is parsed. We say that expansion is
1384 @dfn{deferred} if expansion is not performed immediately. Expansion of
1385 deferred construct is not performed until either the construct appears
1386 later in an immediate context, or until the second phase.
1388 You may not be familiar with some of these constructs yet. You can
1389 reference this section as you become familiar with them, in later
1392 @subheading Variable Assignment
1393 @cindex +=, expansion
1394 @cindex =, expansion
1395 @cindex ?=, expansion
1396 @cindex +=, expansion
1397 @cindex define, expansion
1399 Variable definitions are parsed as follows:
1402 @var{immediate} = @var{deferred}
1403 @var{immediate} ?= @var{deferred}
1404 @var{immediate} := @var{immediate}
1405 @var{immediate} += @var{deferred} or @var{immediate}
1407 define @var{immediate}
1412 For the append operator, @samp{+=}, the right-hand side is considered
1413 immediate if the variable was previously set as a simple variable
1414 (@samp{:=}), and deferred otherwise.
1416 @subheading Conditional Statements
1417 @cindex ifdef, expansion
1418 @cindex ifeq, expansion
1419 @cindex ifndef, expansion
1420 @cindex ifneq, expansion
1422 All instances of conditional syntax are parsed immediately, in their
1423 entirety; this includes the @code{ifdef}, @code{ifeq}, @code{ifndef},
1424 and @code{ifneq} forms.
1426 @subheading Rule Definition
1427 @cindex target, expansion
1428 @cindex prerequisite, expansion
1429 @cindex implicit rule, expansion
1430 @cindex pattern rule, expansion
1431 @cindex explicit rule, expansion
1433 A rule is always expanded the same way, regardless of the form:
1436 @var{immediate} : @var{immediate} ; @var{deferred}
1440 That is, the target and prerequisite sections are expanded immediately,
1441 and the commands used to construct the target are always deferred. This
1442 general rule is true for explicit rules, pattern rules, suffix rules,
1443 static pattern rules, and simple prerequisite definitions.
1445 @node Rules, Commands, Makefiles, Top
1446 @chapter Writing Rules
1447 @cindex writing rules
1448 @cindex rule, how to write
1450 @cindex prerequisite
1452 A @dfn{rule} appears in the makefile and says when and how to remake
1453 certain files, called the rule's @dfn{targets} (most often only one per rule).
1454 It lists the other files that are the @dfn{prerequisites} of the target, and
1455 @dfn{commands} to use to create or update the target.
1457 @cindex default goal
1458 @cindex goal, default
1459 The order of rules is not significant, except for determining the
1460 @dfn{default goal}: the target for @code{make} to consider, if you do
1461 not otherwise specify one. The default goal is the target of the first
1462 rule in the first makefile. If the first rule has multiple targets,
1463 only the first target is taken as the default. There are two
1464 exceptions: a target starting with a period is not a default unless it
1465 contains one or more slashes, @samp{/}, as well; and, a target that
1466 defines a pattern rule has no effect on the default goal.
1467 (@xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.)
1469 Therefore, we usually write the makefile so that the first rule is the
1470 one for compiling the entire program or all the programs described by
1471 the makefile (often with a target called @samp{all}).
1472 @xref{Goals, ,Arguments to Specify the Goals}.
1475 * Rule Example:: An example explained.
1476 * Rule Syntax:: General syntax explained.
1477 * Prerequisite Types:: There are two types of prerequisites.
1478 * Wildcards:: Using wildcard characters such as `*'.
1479 * Directory Search:: Searching other directories for source files.
1480 * Phony Targets:: Using a target that is not a real file's name.
1481 * Force Targets:: You can use a target without commands
1482 or prerequisites to mark other
1484 * Empty Targets:: When only the date matters and the
1486 * Special Targets:: Targets with special built-in meanings.
1487 * Multiple Targets:: When to make use of several targets in a rule.
1488 * Multiple Rules:: How to use several rules with the same target.
1489 * Static Pattern:: Static pattern rules apply to multiple targets
1490 and can vary the prerequisites according to
1492 * Double-Colon:: How to use a special kind of rule to allow
1493 several independent rules for one target.
1494 * Automatic Prerequisites:: How to automatically generate rules giving
1495 prerequisites from source files themselves.
1499 @node Rule Example, Rule Syntax, Rules, Rules
1500 @section Rule Example
1502 Here is an example of a rule:
1505 foo.o : foo.c defs.h # module for twiddling the frobs
1509 Its target is @file{foo.o} and its prerequisites are @file{foo.c} and
1510 @file{defs.h}. It has one command, which is @samp{cc -c -g foo.c}.
1511 The command line starts with a tab to identify it as a command.
1513 This rule says two things:
1517 How to decide whether @file{foo.o} is out of date: it is out of date
1518 if it does not exist, or if either @file{foo.c} or @file{defs.h} is
1519 more recent than it.
1522 How to update the file @file{foo.o}: by running @code{cc} as stated.
1523 The command does not explicitly mention @file{defs.h}, but we presume
1524 that @file{foo.c} includes it, and that that is why @file{defs.h} was
1525 added to the prerequisites.
1529 @node Rule Syntax, Prerequisite Types, Rule Example, Rules
1530 @section Rule Syntax
1533 @cindex syntax of rules
1534 In general, a rule looks like this:
1537 @var{targets} : @var{prerequisites}
1546 @var{targets} : @var{prerequisites} ; @var{command}
1552 @cindex rule targets
1553 The @var{targets} are file names, separated by spaces. Wildcard
1554 characters may be used (@pxref{Wildcards, ,Using Wildcard Characters
1555 in File Names}) and a name of the form @file{@var{a}(@var{m})}
1556 represents member @var{m} in archive file @var{a}
1557 (@pxref{Archive Members, ,Archive Members as Targets}).
1558 Usually there is only one
1559 target per rule, but occasionally there is a reason to have more
1560 (@pxref{Multiple Targets, , Multiple Targets in a Rule}).@refill
1563 @cindex tab character (in commands)
1564 The @var{command} lines start with a tab character. The first command may
1565 appear on the line after the prerequisites, with a tab character, or may
1566 appear on the same line, with a semicolon. Either way, the effect is the
1567 same. @xref{Commands, ,Writing the Commands in Rules}.
1569 @cindex dollar sign (@code{$}), in rules
1570 @cindex @code{$}, in rules
1571 @cindex rule, and @code{$}
1572 Because dollar signs are used to start variable references, if you really
1573 want a dollar sign in a rule you must write two of them, @samp{$$}
1574 (@pxref{Using Variables, ,How to Use Variables}).
1575 You may split a long line by inserting a backslash
1576 followed by a newline, but this is not required, as @code{make} places no
1577 limit on the length of a line in a makefile.
1579 A rule tells @code{make} two things: when the targets are out of date,
1580 and how to update them when necessary.
1582 @cindex prerequisites
1583 @cindex rule prerequisites
1584 The criterion for being out of date is specified in terms of the
1585 @var{prerequisites}, which consist of file names separated by spaces.
1586 (Wildcards and archive members (@pxref{Archives}) are allowed here too.)
1587 A target is out of date if it does not exist or if it is older than any
1588 of the prerequisites (by comparison of last-modification times). The
1589 idea is that the contents of the target file are computed based on
1590 information in the prerequisites, so if any of the prerequisites changes,
1591 the contents of the existing target file are no longer necessarily
1594 How to update is specified by @var{commands}. These are lines to be
1595 executed by the shell (normally @samp{sh}), but with some extra features
1596 (@pxref{Commands, ,Writing the Commands in Rules}).
1598 @node Prerequisite Types, Wildcards, Rule Syntax, Rules
1599 @comment node-name, next, previous, up
1600 @section Types of Prerequisites
1601 @cindex prerequisite types
1602 @cindex types of prerequisites
1604 @cindex prerequisites, normal
1605 @cindex normal prerequisites
1606 @cindex prerequisites, order-only
1607 @cindex order-only prerequisites
1608 There are actually two different types of prerequisites understood by
1609 GNU @code{make}: normal prerequisites such as described in the
1610 previous section, and @dfn{order-only} prerequisites. A normal
1611 prerequisite actually makes two statements: first, it imposes an order
1612 of execution of build commands: any commands necessary to build any of
1613 a target's prerequisites will be fully executed before any commands
1614 necessary to build the target. Second, it imposes a dependency
1615 relationship: if any prerequisite is newer than the target, then the
1616 target is considered out-of-date and must be rebuilt.
1618 Normally, this is exactly what you want: if a target's prerequisite is
1619 updated, then the target should also be updated.
1621 Occasionally, however, you have a situation where you want to impose a
1622 specific ordering on the rules to be invoked @emph{without} forcing
1623 the target to be updated if one of those rules is executed. In that
1624 case, you want to define @dfn{order-only} prerequisites. Order-only
1625 prerequisites can be specified by placing a pipe symbol (@code{|})
1626 in the prerequisites list: any prerequisites to the left of the pipe
1627 symbol are normal; any prerequisites to the right are order-only:
1630 @var{targets} : @var{normal-prerequisites} | @var{order-only-prerequisites}
1633 The normal prerequisites section may of course be empty. Also, you
1634 may still declare multiple lines of prerequisites for the same target:
1635 they are appended appropriately. Note that if you declare the same
1636 file to be both a normal and an order-only prerequisite, the normal
1637 prerequisite takes precedence (since they are a strict superset of the
1638 behavior of an order-only prerequisite).
1640 @node Wildcards, Directory Search, Prerequisite Types, Rules
1641 @section Using Wildcard Characters in File Names
1643 @cindex file name with wildcards
1644 @cindex globbing (wildcards)
1646 @cindex @code{*} (wildcard character)
1647 @cindex @code{?} (wildcard character)
1648 @cindex @code{[@dots{}]} (wildcard characters)
1649 A single file name can specify many files using @dfn{wildcard characters}.
1650 The wildcard characters in @code{make} are @samp{*}, @samp{?} and
1651 @samp{[@dots{}]}, the same as in the Bourne shell. For example, @file{*.c}
1652 specifies a list of all the files (in the working directory) whose names
1653 end in @samp{.c}.@refill
1655 @cindex @code{~} (tilde)
1656 @cindex tilde (@code{~})
1657 @cindex home directory
1658 The character @samp{~} at the beginning of a file name also has special
1659 significance. If alone, or followed by a slash, it represents your home
1660 directory. For example @file{~/bin} expands to @file{/home/you/bin}.
1661 If the @samp{~} is followed by a word, the string represents the home
1662 directory of the user named by that word. For example @file{~john/bin}
1663 expands to @file{/home/john/bin}. On systems which don't have a home
1664 directory for each user (such as MS-DOS or MS-Windows), this
1665 functionality can be simulated by setting the environment variable
1668 Wildcard expansion happens automatically in targets, in prerequisites,
1669 and in commands (where the shell does the expansion). In other
1670 contexts, wildcard expansion happens only if you request it explicitly
1671 with the @code{wildcard} function.
1673 The special significance of a wildcard character can be turned off by
1674 preceding it with a backslash. Thus, @file{foo\*bar} would refer to a
1675 specific file whose name consists of @samp{foo}, an asterisk, and
1679 * Wildcard Examples:: Several examples
1680 * Wildcard Pitfall:: Problems to avoid.
1681 * Wildcard Function:: How to cause wildcard expansion where
1682 it does not normally take place.
1685 @node Wildcard Examples, Wildcard Pitfall, Wildcards, Wildcards
1686 @subsection Wildcard Examples
1688 Wildcards can be used in the commands of a rule, where they are expanded
1689 by the shell. For example, here is a rule to delete all the object files:
1697 @cindex @code{rm} (shell command)
1699 Wildcards are also useful in the prerequisites of a rule. With the
1700 following rule in the makefile, @samp{make print} will print all the
1701 @samp{.c} files that have changed since the last time you printed them:
1709 @cindex @code{print} target
1710 @cindex @code{lpr} (shell command)
1711 @cindex @code{touch} (shell command)
1713 This rule uses @file{print} as an empty target file; see @ref{Empty
1714 Targets, ,Empty Target Files to Record Events}. (The automatic variable
1715 @samp{$?} is used to print only those files that have changed; see
1716 @ref{Automatic, ,Automatic Variables}.)@refill
1718 Wildcard expansion does not happen when you define a variable. Thus, if
1726 then the value of the variable @code{objects} is the actual string
1727 @samp{*.o}. However, if you use the value of @code{objects} in a target,
1728 prerequisite or command, wildcard expansion will take place at that time.
1729 To set @code{objects} to the expansion, instead use:
1732 objects := $(wildcard *.o)
1736 @xref{Wildcard Function}.
1738 @node Wildcard Pitfall, Wildcard Function, Wildcard Examples, Wildcards
1739 @subsection Pitfalls of Using Wildcards
1740 @cindex wildcard pitfalls
1741 @cindex pitfalls of wildcards
1742 @cindex mistakes with wildcards
1743 @cindex errors with wildcards
1744 @cindex problems with wildcards
1746 Now here is an example of a naive way of using wildcard expansion, that
1747 does not do what you would intend. Suppose you would like to say that the
1748 executable file @file{foo} is made from all the object files in the
1749 directory, and you write this:
1755 cc -o foo $(CFLAGS) $(objects)
1759 The value of @code{objects} is the actual string @samp{*.o}. Wildcard
1760 expansion happens in the rule for @file{foo}, so that each @emph{existing}
1761 @samp{.o} file becomes a prerequisite of @file{foo} and will be recompiled if
1764 But what if you delete all the @samp{.o} files? When a wildcard matches
1765 no files, it is left as it is, so then @file{foo} will depend on the
1766 oddly-named file @file{*.o}. Since no such file is likely to exist,
1767 @code{make} will give you an error saying it cannot figure out how to
1768 make @file{*.o}. This is not what you want!
1770 Actually it is possible to obtain the desired result with wildcard
1771 expansion, but you need more sophisticated techniques, including the
1772 @code{wildcard} function and string substitution.
1774 @xref{Wildcard Function, ,The Function @code{wildcard}}.
1777 These are described in the following section.
1780 @cindex wildcards and MS-DOS/MS-Windows backslashes
1781 @cindex backslashes in pathnames and wildcard expansion
1783 Microsoft operating systems (MS-DOS and MS-Windows) use backslashes to
1784 separate directories in pathnames, like so:
1790 This is equivalent to the Unix-style @file{c:/foo/bar/baz.c} (the
1791 @file{c:} part is the so-called drive letter). When @code{make} runs on
1792 these systems, it supports backslashes as well as the Unix-style forward
1793 slashes in pathnames. However, this support does @emph{not} include the
1794 wildcard expansion, where backslash is a quote character. Therefore,
1795 you @emph{must} use Unix-style slashes in these cases.
1798 @node Wildcard Function, , Wildcard Pitfall, Wildcards
1799 @subsection The Function @code{wildcard}
1802 Wildcard expansion happens automatically in rules. But wildcard expansion
1803 does not normally take place when a variable is set, or inside the
1804 arguments of a function. If you want to do wildcard expansion in such
1805 places, you need to use the @code{wildcard} function, like this:
1808 $(wildcard @var{pattern}@dots{})
1812 This string, used anywhere in a makefile, is replaced by a
1813 space-separated list of names of existing files that match one of the
1814 given file name patterns. If no existing file name matches a pattern,
1815 then that pattern is omitted from the output of the @code{wildcard}
1816 function. Note that this is different from how unmatched wildcards
1817 behave in rules, where they are used verbatim rather than ignored
1818 (@pxref{Wildcard Pitfall}).
1820 One use of the @code{wildcard} function is to get a list of all the C source
1821 files in a directory, like this:
1827 We can change the list of C source files into a list of object files by
1828 replacing the @samp{.c} suffix with @samp{.o} in the result, like this:
1831 $(patsubst %.c,%.o,$(wildcard *.c))
1835 (Here we have used another function, @code{patsubst}.
1836 @xref{Text Functions, ,Functions for String Substitution and Analysis}.)@refill
1838 Thus, a makefile to compile all C source files in the directory and then
1839 link them together could be written as follows:
1842 objects := $(patsubst %.c,%.o,$(wildcard *.c))
1845 cc -o foo $(objects)
1849 (This takes advantage of the implicit rule for compiling C programs, so
1850 there is no need to write explicit rules for compiling the files.
1851 @xref{Flavors, ,The Two Flavors of Variables}, for an explanation of
1852 @samp{:=}, which is a variant of @samp{=}.)
1854 @node Directory Search, Phony Targets, Wildcards, Rules
1855 @section Searching Directories for Prerequisites
1859 @cindex search path for prerequisites (@code{VPATH})
1860 @cindex directory search (@code{VPATH})
1862 For large systems, it is often desirable to put sources in a separate
1863 directory from the binaries. The @dfn{directory search} features of
1864 @code{make} facilitate this by searching several directories
1865 automatically to find a prerequisite. When you redistribute the files
1866 among directories, you do not need to change the individual rules,
1867 just the search paths.
1870 * General Search:: Specifying a search path that applies
1871 to every prerequisite.
1872 * Selective Search:: Specifying a search path
1873 for a specified class of names.
1874 * Search Algorithm:: When and how search paths are applied.
1875 * Commands/Search:: How to write shell commands that work together
1877 * Implicit/Search:: How search paths affect implicit rules.
1878 * Libraries/Search:: Directory search for link libraries.
1881 @node General Search, Selective Search, Directory Search, Directory Search
1882 @subsection @code{VPATH}: Search Path for All Prerequisites
1885 The value of the @code{make} variable @code{VPATH} specifies a list of
1886 directories that @code{make} should search. Most often, the
1887 directories are expected to contain prerequisite files that are not in the
1888 current directory; however, @code{VPATH} specifies a search list that
1889 @code{make} applies for all files, including files which are targets of
1892 Thus, if a file that is listed as a target or prerequisite does not exist
1893 in the current directory, @code{make} searches the directories listed in
1894 @code{VPATH} for a file with that name. If a file is found in one of
1895 them, that file may become the prerequisite (see below). Rules may then
1896 specify the names of files in the prerequisite list as if they all
1897 existed in the current directory. @xref{Commands/Search, ,Writing Shell
1898 Commands with Directory Search}.
1900 In the @code{VPATH} variable, directory names are separated by colons or
1901 blanks. The order in which directories are listed is the order followed
1902 by @code{make} in its search. (On MS-DOS and MS-Windows, semi-colons
1903 are used as separators of directory names in @code{VPATH}, since the
1904 colon can be used in the pathname itself, after the drive letter.)
1909 VPATH = src:../headers
1913 specifies a path containing two directories, @file{src} and
1914 @file{../headers}, which @code{make} searches in that order.
1916 With this value of @code{VPATH}, the following rule,
1923 is interpreted as if it were written like this:
1930 assuming the file @file{foo.c} does not exist in the current directory but
1931 is found in the directory @file{src}.
1933 @node Selective Search, Search Algorithm, General Search, Directory Search
1934 @subsection The @code{vpath} Directive
1937 Similar to the @code{VPATH} variable, but more selective, is the
1938 @code{vpath} directive (note lower case), which allows you to specify a
1939 search path for a particular class of file names: those that match a
1940 particular pattern. Thus you can supply certain search directories for
1941 one class of file names and other directories (or none) for other file
1944 There are three forms of the @code{vpath} directive:
1947 @item vpath @var{pattern} @var{directories}
1948 Specify the search path @var{directories} for file names that match
1951 The search path, @var{directories}, is a list of directories to be
1952 searched, separated by colons (semi-colons on MS-DOS and MS-Windows) or
1953 blanks, just like the search path used in the @code{VPATH} variable.
1955 @item vpath @var{pattern}
1956 Clear out the search path associated with @var{pattern}.
1958 @c Extra blank line makes sure this gets two lines.
1961 Clear all search paths previously specified with @code{vpath} directives.
1964 A @code{vpath} pattern is a string containing a @samp{%} character. The
1965 string must match the file name of a prerequisite that is being searched
1966 for, the @samp{%} character matching any sequence of zero or more
1967 characters (as in pattern rules; @pxref{Pattern Rules, ,Defining and
1968 Redefining Pattern Rules}). For example, @code{%.h} matches files that
1969 end in @code{.h}. (If there is no @samp{%}, the pattern must match the
1970 prerequisite exactly, which is not useful very often.)
1972 @cindex @code{%}, quoting in @code{vpath}
1973 @cindex @code{%}, quoting with @code{\} (backslash)
1974 @cindex @code{\} (backslash), to quote @code{%}
1975 @cindex backslash (@code{\}), to quote @code{%}
1976 @cindex quoting @code{%}, in @code{vpath}
1977 @samp{%} characters in a @code{vpath} directive's pattern can be quoted
1978 with preceding backslashes (@samp{\}). Backslashes that would otherwise
1979 quote @samp{%} characters can be quoted with more backslashes.
1980 Backslashes that quote @samp{%} characters or other backslashes are
1981 removed from the pattern before it is compared to file names. Backslashes
1982 that are not in danger of quoting @samp{%} characters go unmolested.@refill
1984 When a prerequisite fails to exist in the current directory, if the
1985 @var{pattern} in a @code{vpath} directive matches the name of the
1986 prerequisite file, then the @var{directories} in that directive are searched
1987 just like (and before) the directories in the @code{VPATH} variable.
1992 vpath %.h ../headers
1996 tells @code{make} to look for any prerequisite whose name ends in @file{.h}
1997 in the directory @file{../headers} if the file is not found in the current
2000 If several @code{vpath} patterns match the prerequisite file's name, then
2001 @code{make} processes each matching @code{vpath} directive one by one,
2002 searching all the directories mentioned in each directive. @code{make}
2003 handles multiple @code{vpath} directives in the order in which they
2004 appear in the makefile; multiple directives with the same pattern are
2005 independent of each other.
2019 will look for a file ending in @samp{.c} in @file{foo}, then
2020 @file{blish}, then @file{bar}, while
2030 will look for a file ending in @samp{.c} in @file{foo}, then
2031 @file{bar}, then @file{blish}.
2033 @node Search Algorithm, Commands/Search, Selective Search, Directory Search
2034 @subsection How Directory Searches are Performed
2035 @cindex algorithm for directory search
2036 @cindex directory search algorithm
2038 When a prerequisite is found through directory search, regardless of type
2039 (general or selective), the pathname located may not be the one that
2040 @code{make} actually provides you in the prerequisite list. Sometimes
2041 the path discovered through directory search is thrown away.
2043 The algorithm @code{make} uses to decide whether to keep or abandon a
2044 path found via directory search is as follows:
2048 If a target file does not exist at the path specified in the makefile,
2049 directory search is performed.
2052 If the directory search is successful, that path is kept and this file
2053 is tentatively stored as the target.
2056 All prerequisites of this target are examined using this same method.
2059 After processing the prerequisites, the target may or may not need to be
2064 If the target does @emph{not} need to be rebuilt, the path to the file
2065 found during directory search is used for any prerequisite lists which
2066 contain this target. In short, if @code{make} doesn't need to rebuild
2067 the target then you use the path found via directory search.
2070 If the target @emph{does} need to be rebuilt (is out-of-date), the
2071 pathname found during directory search is @emph{thrown away}, and the
2072 target is rebuilt using the file name specified in the makefile. In
2073 short, if @code{make} must rebuild, then the target is rebuilt locally,
2074 not in the directory found via directory search.
2078 This algorithm may seem complex, but in practice it is quite often
2079 exactly what you want.
2081 @cindex traditional directory search (GPATH)
2082 @cindex directory search, traditional (GPATH)
2083 Other versions of @code{make} use a simpler algorithm: if the file does
2084 not exist, and it is found via directory search, then that pathname is
2085 always used whether or not the target needs to be built. Thus, if the
2086 target is rebuilt it is created at the pathname discovered during
2090 If, in fact, this is the behavior you want for some or all of your
2091 directories, you can use the @code{GPATH} variable to indicate this to
2094 @code{GPATH} has the same syntax and format as @code{VPATH} (that is, a
2095 space- or colon-delimited list of pathnames). If an out-of-date target
2096 is found by directory search in a directory that also appears in
2097 @code{GPATH}, then that pathname is not thrown away. The target is
2098 rebuilt using the expanded path.
2100 @node Commands/Search, Implicit/Search, Search Algorithm, Directory Search
2101 @subsection Writing Shell Commands with Directory Search
2102 @cindex shell command, and directory search
2103 @cindex directory search (@code{VPATH}), and shell commands
2105 When a prerequisite is found in another directory through directory search,
2106 this cannot change the commands of the rule; they will execute as written.
2107 Therefore, you must write the commands with care so that they will look for
2108 the prerequisite in the directory where @code{make} finds it.
2110 This is done with the @dfn{automatic variables} such as @samp{$^}
2111 (@pxref{Automatic, ,Automatic Variables}).
2112 For instance, the value of @samp{$^} is a
2113 list of all the prerequisites of the rule, including the names of
2114 the directories in which they were found, and the value of
2115 @samp{$@@} is the target. Thus:@refill
2119 cc -c $(CFLAGS) $^ -o $@@
2123 (The variable @code{CFLAGS} exists so you can specify flags for C
2124 compilation by implicit rules; we use it here for consistency so it will
2125 affect all C compilations uniformly;
2126 @pxref{Implicit Variables, ,Variables Used by Implicit Rules}.)
2128 Often the prerequisites include header files as well, which you do not
2129 want to mention in the commands. The automatic variable @samp{$<} is
2130 just the first prerequisite:
2133 VPATH = src:../headers
2134 foo.o : foo.c defs.h hack.h
2135 cc -c $(CFLAGS) $< -o $@@
2138 @node Implicit/Search, Libraries/Search, Commands/Search, Directory Search
2139 @subsection Directory Search and Implicit Rules
2140 @cindex @code{VPATH}, and implicit rules
2141 @cindex directory search (@code{VPATH}), and implicit rules
2142 @cindex search path for prerequisites (@code{VPATH}), and implicit rules
2143 @cindex implicit rule, and directory search
2144 @cindex implicit rule, and @code{VPATH}
2145 @cindex rule, implicit, and directory search
2146 @cindex rule, implicit, and @code{VPATH}
2148 The search through the directories specified in @code{VPATH} or with
2149 @code{vpath} also happens during consideration of implicit rules
2150 (@pxref{Implicit Rules, ,Using Implicit Rules}).
2152 For example, when a file @file{foo.o} has no explicit rule, @code{make}
2153 considers implicit rules, such as the built-in rule to compile
2154 @file{foo.c} if that file exists. If such a file is lacking in the
2155 current directory, the appropriate directories are searched for it. If
2156 @file{foo.c} exists (or is mentioned in the makefile) in any of the
2157 directories, the implicit rule for C compilation is applied.
2159 The commands of implicit rules normally use automatic variables as a
2160 matter of necessity; consequently they will use the file names found by
2161 directory search with no extra effort.
2163 @node Libraries/Search, , Implicit/Search, Directory Search
2164 @subsection Directory Search for Link Libraries
2165 @cindex link libraries, and directory search
2166 @cindex libraries for linking, directory search
2167 @cindex directory search (@code{VPATH}), and link libraries
2168 @cindex @code{VPATH}, and link libraries
2169 @cindex search path for prerequisites (@code{VPATH}), and link libraries
2170 @cindex @code{-l} (library search)
2171 @cindex link libraries, patterns matching
2172 @cindex @code{.LIBPATTERNS}, and link libraries
2173 @vindex .LIBPATTERNS
2175 Directory search applies in a special way to libraries used with the
2176 linker. This special feature comes into play when you write a prerequisite
2177 whose name is of the form @samp{-l@var{name}}. (You can tell something
2178 strange is going on here because the prerequisite is normally the name of a
2179 file, and the @emph{file name} of a library generally looks like
2180 @file{lib@var{name}.a}, not like @samp{-l@var{name}}.)@refill
2182 When a prerequisite's name has the form @samp{-l@var{name}}, @code{make}
2183 handles it specially by searching for the file @file{lib@var{name}.so} in
2184 the current directory, in directories specified by matching @code{vpath}
2185 search paths and the @code{VPATH} search path, and then in the
2186 directories @file{/lib}, @file{/usr/lib}, and @file{@var{prefix}/lib}
2187 (normally @file{/usr/local/lib}, but MS-DOS/MS-Windows versions of
2188 @code{make} behave as if @var{prefix} is defined to be the root of the
2189 DJGPP installation tree).
2191 If that file is not found, then the file @file{lib@var{name}.a} is
2192 searched for, in the same directories as above.
2194 For example, if there is a @file{/usr/lib/libcurses.a} library on your
2195 system (and no @file{/usr/lib/libcurses.so} file), then
2199 foo : foo.c -lcurses
2205 would cause the command @samp{cc foo.c /usr/lib/libcurses.a -o foo} to
2206 be executed when @file{foo} is older than @file{foo.c} or than
2207 @file{/usr/lib/libcurses.a}.@refill
2209 Although the default set of files to be searched for is
2210 @file{lib@var{name}.so} and @file{lib@var{name}.a}, this is customizable
2211 via the @code{.LIBPATTERNS} variable. Each word in the value of this
2212 variable is a pattern string. When a prerequisite like
2213 @samp{-l@var{name}} is seen, @code{make} will replace the percent in
2214 each pattern in the list with @var{name} and perform the above directory
2215 searches using that library filename. If no library is found, the next
2216 word in the list will be used.
2218 The default value for @code{.LIBPATTERNS} is ``@samp{lib%.so lib%.a}'',
2219 which provides the default behavior described above.
2221 You can turn off link library expansion completely by setting this
2222 variable to an empty value.
2224 @node Phony Targets, Force Targets, Directory Search, Rules
2225 @section Phony Targets
2226 @cindex phony targets
2227 @cindex targets, phony
2228 @cindex targets without a file
2230 A phony target is one that is not really the name of a file. It is just a
2231 name for some commands to be executed when you make an explicit request.
2232 There are two reasons to use a phony target: to avoid a conflict with
2233 a file of the same name, and to improve performance.
2235 If you write a rule whose commands will not create the target file, the
2236 commands will be executed every time the target comes up for remaking.
2247 Because the @code{rm} command does not create a file named @file{clean},
2248 probably no such file will ever exist. Therefore, the @code{rm} command
2249 will be executed every time you say @samp{make clean}.
2250 @cindex @code{rm} (shell command)
2253 The phony target will cease to work if anything ever does create a file
2254 named @file{clean} in this directory. Since it has no prerequisites, the
2255 file @file{clean} would inevitably be considered up to date, and its
2256 commands would not be executed. To avoid this problem, you can explicitly
2257 declare the target to be phony, using the special target @code{.PHONY}
2258 (@pxref{Special Targets, ,Special Built-in Target Names}) as follows:
2265 Once this is done, @samp{make clean} will run the commands regardless of
2266 whether there is a file named @file{clean}.
2268 Since it knows that phony targets do not name actual files that could be
2269 remade from other files, @code{make} skips the implicit rule search for
2270 phony targets (@pxref{Implicit Rules}). This is why declaring a target
2271 phony is good for performance, even if you are not worried about the
2272 actual file existing.
2274 Thus, you first write the line that states that @code{clean} is a
2275 phony target, then you write the rule, like this:
2285 Another example of the usefulness of phony targets is in conjunction
2286 with recursive invocations of @code{make}. In this case the makefile
2287 will often contain a variable which lists a number of subdirectories to
2288 be built. One way to handle this is with one rule whose command is a
2289 shell loop over the subdirectories, like this:
2293 SUBDIRS = foo bar baz
2296 for dir in $(SUBDIRS); do \
2302 There are a few problems with this method, however. First, any error
2303 detected in a submake is not noted by this rule, so it will continue to
2304 build the rest of the directories even when one fails. This can be
2305 overcome by adding shell commands to note the error and exit, but then
2306 it will do so even if @code{make} is invoked with the @code{-k} option,
2307 which is unfortunate. Second, and perhaps more importantly, you cannot
2308 take advantage of the parallel build capabilities of make using this
2309 method, since there is only one rule.
2311 By declaring the subdirectories as phony targets (you must do this as
2312 the subdirectory obviously always exists; otherwise it won't be built)
2313 you can remove these problems:
2317 SUBDIRS = foo bar baz
2319 .PHONY: subdirs $(SUBDIRS)
2330 Here we've also declared that the @file{foo} subdirectory cannot be
2331 built until after the @file{baz} subdirectory is complete; this kind of
2332 relationship declaration is particularly important when attempting
2335 A phony target should not be a prerequisite of a real target file; if it
2336 is, its commands are run every time @code{make} goes to update that
2337 file. As long as a phony target is never a prerequisite of a real
2338 target, the phony target commands will be executed only when the phony
2339 target is a specified goal (@pxref{Goals, ,Arguments to Specify the
2342 Phony targets can have prerequisites. When one directory contains multiple
2343 programs, it is most convenient to describe all of the programs in one
2344 makefile @file{./Makefile}. Since the target remade by default will be the
2345 first one in the makefile, it is common to make this a phony target named
2346 @samp{all} and give it, as prerequisites, all the individual programs. For
2350 all : prog1 prog2 prog3
2353 prog1 : prog1.o utils.o
2354 cc -o prog1 prog1.o utils.o
2359 prog3 : prog3.o sort.o utils.o
2360 cc -o prog3 prog3.o sort.o utils.o
2364 Now you can say just @samp{make} to remake all three programs, or specify
2365 as arguments the ones to remake (as in @samp{make prog1 prog3}).
2367 When one phony target is a prerequisite of another, it serves as a subroutine
2368 of the other. For example, here @samp{make cleanall} will delete the
2369 object files, the difference files, and the file @file{program}:
2372 .PHONY: cleanall cleanobj cleandiff
2374 cleanall : cleanobj cleandiff
2384 @node Force Targets, Empty Targets, Phony Targets, Rules
2385 @section Rules without Commands or Prerequisites
2386 @cindex force targets
2387 @cindex targets, force
2388 @cindex @code{FORCE}
2389 @cindex rule, no commands or prerequisites
2391 If a rule has no prerequisites or commands, and the target of the rule
2392 is a nonexistent file, then @code{make} imagines this target to have
2393 been updated whenever its rule is run. This implies that all targets
2394 depending on this one will always have their commands run.
2396 An example will illustrate this:
2406 Here the target @samp{FORCE} satisfies the special conditions, so the
2407 target @file{clean} that depends on it is forced to run its commands.
2408 There is nothing special about the name @samp{FORCE}, but that is one name
2409 commonly used this way.
2411 As you can see, using @samp{FORCE} this way has the same results as using
2412 @samp{.PHONY: clean}.
2414 Using @samp{.PHONY} is more explicit and more efficient. However,
2415 other versions of @code{make} do not support @samp{.PHONY}; thus
2416 @samp{FORCE} appears in many makefiles. @xref{Phony Targets}.
2418 @node Empty Targets, Special Targets, Force Targets, Rules
2419 @section Empty Target Files to Record Events
2420 @cindex empty targets
2421 @cindex targets, empty
2422 @cindex recording events with empty targets
2424 The @dfn{empty target} is a variant of the phony target; it is used to hold
2425 commands for an action that you request explicitly from time to time.
2426 Unlike a phony target, this target file can really exist; but the file's
2427 contents do not matter, and usually are empty.
2429 The purpose of the empty target file is to record, with its
2430 last-modification time, when the rule's commands were last executed. It
2431 does so because one of the commands is a @code{touch} command to update the
2434 The empty target file should have some prerequisites (otherwise it
2435 doesn't make sense). When you ask to remake the empty target, the
2436 commands are executed if any prerequisite is more recent than the target;
2437 in other words, if a prerequisite has changed since the last time you
2438 remade the target. Here is an example:
2445 @cindex @code{print} target
2446 @cindex @code{lpr} (shell command)
2447 @cindex @code{touch} (shell command)
2450 With this rule, @samp{make print} will execute the @code{lpr} command if
2451 either source file has changed since the last @samp{make print}. The
2452 automatic variable @samp{$?} is used to print only those files that have
2453 changed (@pxref{Automatic, ,Automatic Variables}).
2455 @node Special Targets, Multiple Targets, Empty Targets, Rules
2456 @section Special Built-in Target Names
2457 @cindex special targets
2458 @cindex built-in special targets
2459 @cindex targets, built-in special
2461 Certain names have special meanings if they appear as targets.
2467 The prerequisites of the special target @code{.PHONY} are considered to
2468 be phony targets. When it is time to consider such a target,
2469 @code{make} will run its commands unconditionally, regardless of
2470 whether a file with that name exists or what its last-modification
2471 time is. @xref{Phony Targets, ,Phony Targets}.
2476 The prerequisites of the special target @code{.SUFFIXES} are the list
2477 of suffixes to be used in checking for suffix rules.
2478 @xref{Suffix Rules, , Old-Fashioned Suffix Rules}.
2483 The commands specified for @code{.DEFAULT} are used for any target for
2484 which no rules are found (either explicit rules or implicit rules).
2485 @xref{Last Resort}. If @code{.DEFAULT} commands are specified, every
2486 file mentioned as a prerequisite, but not as a target in a rule, will have
2487 these commands executed on its behalf. @xref{Implicit Rule Search,
2488 ,Implicit Rule Search Algorithm}.
2492 @cindex precious targets
2493 @cindex preserving with @code{.PRECIOUS}
2495 The targets which @code{.PRECIOUS} depends on are given the following
2496 special treatment: if @code{make} is killed or interrupted during the
2497 execution of their commands, the target is not deleted.
2498 @xref{Interrupts, ,Interrupting or Killing @code{make}}. Also, if the
2499 target is an intermediate file, it will not be deleted after it is no
2500 longer needed, as is normally done. @xref{Chained Rules, ,Chains of
2501 Implicit Rules}. In this latter respect it overlaps with the
2502 @code{.SECONDARY} special target.
2504 You can also list the target pattern of an implicit rule (such as
2505 @samp{%.o}) as a prerequisite file of the special target @code{.PRECIOUS}
2506 to preserve intermediate files created by rules whose target patterns
2507 match that file's name.
2509 @findex .INTERMEDIATE
2511 @cindex intermediate targets, explicit
2513 The targets which @code{.INTERMEDIATE} depends on are treated as
2514 intermediate files. @xref{Chained Rules, ,Chains of Implicit Rules}.
2515 @code{.INTERMEDIATE} with no prerequisites has no effect.
2519 @cindex secondary targets
2520 @cindex preserving with @code{.SECONDARY}
2522 The targets which @code{.SECONDARY} depends on are treated as
2523 intermediate files, except that they are never automatically deleted.
2524 @xref{Chained Rules, ,Chains of Implicit Rules}.
2526 @code{.SECONDARY} with no prerequisites causes all targets to be treated
2527 as secondary (i.e., no target is removed because it is considered
2530 @findex .DELETE_ON_ERROR
2531 @item .DELETE_ON_ERROR
2532 @cindex removing targets on failure
2534 If @code{.DELETE_ON_ERROR} is mentioned as a target anywhere in the
2535 makefile, then @code{make} will delete the target of a rule if it has
2536 changed and its commands exit with a nonzero exit status, just as it
2537 does when it receives a signal. @xref{Errors, ,Errors in Commands}.
2542 If you specify prerequisites for @code{.IGNORE}, then @code{make} will
2543 ignore errors in execution of the commands run for those particular
2544 files. The commands for @code{.IGNORE} are not meaningful.
2546 If mentioned as a target with no prerequisites, @code{.IGNORE} says to
2547 ignore errors in execution of commands for all files. This usage of
2548 @samp{.IGNORE} is supported only for historical compatibility. Since
2549 this affects every command in the makefile, it is not very useful; we
2550 recommend you use the more selective ways to ignore errors in specific
2551 commands. @xref{Errors, ,Errors in Commands}.
2553 @findex .LOW_RESOLUTION_TIME
2554 @item .LOW_RESOLUTION_TIME
2556 If you specify prerequisites for @code{.LOW_RESOLUTION_TIME},
2557 @command{make} assumes that these files are created by commands that
2558 generate low resolution time stamps. The commands for
2559 @code{.LOW_RESOLUTION_TIME} are not meaningful.
2561 The high resolution file time stamps of many modern hosts lessen the
2562 chance of @command{make} incorrectly concluding that a file is up to
2563 date. Unfortunately, these hosts provide no way to set a high
2564 resolution file time stamp, so commands like @samp{cp -p} that
2565 explicitly set a file's time stamp must discard its subsecond part. If
2566 a file is created by such a command, you should list it as a
2567 prerequisite of @code{.LOW_RESOLUTION_TIME} so that @command{make} does
2568 not mistakenly conclude that the file is out of date. For example:
2572 .LOW_RESOLUTION_TIME: dst
2578 Since @samp{cp -p} discards the subsecond part of @file{src}'s time
2579 stamp, @file{dst} is typically slightly older than @file{src} even when
2580 it is up to date. The @code{.LOW_RESOLUTION_TIME} line causes
2581 @command{make} to consider @file{dst} to be up to date if its time stamp
2582 is at the start of the same second that @file{src}'s time stamp is in.
2584 Due to a limitation of the archive format, archive member time stamps
2585 are always low resolution. You need not list archive members as
2586 prerequisites of @code{.LOW_RESOLUTION_TIME}, as @command{make} does this
2592 If you specify prerequisites for @code{.SILENT}, then @code{make} will
2593 not print the commands to remake those particular files before executing
2594 them. The commands for @code{.SILENT} are not meaningful.
2596 If mentioned as a target with no prerequisites, @code{.SILENT} says not
2597 to print any commands before executing them. This usage of
2598 @samp{.SILENT} is supported only for historical compatibility. We
2599 recommend you use the more selective ways to silence specific commands.
2600 @xref{Echoing, ,Command Echoing}. If you want to silence all commands
2601 for a particular run of @code{make}, use the @samp{-s} or
2602 @w{@samp{--silent}} option (@pxref{Options Summary}).
2604 @findex .EXPORT_ALL_VARIABLES
2605 @item .EXPORT_ALL_VARIABLES
2607 Simply by being mentioned as a target, this tells @code{make} to
2608 export all variables to child processes by default.
2609 @xref{Variables/Recursion, ,Communicating Variables to a
2612 @findex .NOTPARALLEL
2614 @cindex parallel execution, overriding
2616 If @code{.NOTPARALLEL} is mentioned as a target, then this invocation of
2617 @code{make} will be run serially, even if the @samp{-j} option is
2618 given. Any recursively invoked @code{make} command will still be run in
2619 parallel (unless its makefile contains this target). Any prerequisites
2620 on this target are ignored.
2623 Any defined implicit rule suffix also counts as a special target if it
2624 appears as a target, and so does the concatenation of two suffixes, such
2625 as @samp{.c.o}. These targets are suffix rules, an obsolete way of
2626 defining implicit rules (but a way still widely used). In principle, any
2627 target name could be special in this way if you break it in two and add
2628 both pieces to the suffix list. In practice, suffixes normally begin with
2629 @samp{.}, so these special target names also begin with @samp{.}.
2630 @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
2632 @node Multiple Targets, Multiple Rules, Special Targets, Rules
2633 @section Multiple Targets in a Rule
2634 @cindex multiple targets
2635 @cindex several targets in a rule
2636 @cindex targets, multiple
2637 @cindex rule, with multiple targets
2639 A rule with multiple targets is equivalent to writing many rules, each with
2640 one target, and all identical aside from that. The same commands apply to
2641 all the targets, but their effects may vary because you can substitute the
2642 actual target name into the command using @samp{$@@}. The rule contributes
2643 the same prerequisites to all the targets also.
2645 This is useful in two cases.
2649 You want just prerequisites, no commands. For example:
2652 kbd.o command.o files.o: command.h
2656 gives an additional prerequisite to each of the three object files
2660 Similar commands work for all the targets. The commands do not need
2661 to be absolutely identical, since the automatic variable @samp{$@@}
2662 can be used to substitute the particular target to be remade into the
2663 commands (@pxref{Automatic, ,Automatic Variables}). For example:
2667 bigoutput littleoutput : text.g
2668 generate text.g -$(subst output,,$@@) > $@@
2678 generate text.g -big > bigoutput
2679 littleoutput : text.g
2680 generate text.g -little > littleoutput
2684 Here we assume the hypothetical program @code{generate} makes two
2685 types of output, one if given @samp{-big} and one if given
2687 @xref{Text Functions, ,Functions for String Substitution and Analysis},
2688 for an explanation of the @code{subst} function.
2691 Suppose you would like to vary the prerequisites according to the target,
2692 much as the variable @samp{$@@} allows you to vary the commands.
2693 You cannot do this with multiple targets in an ordinary rule, but you can
2694 do it with a @dfn{static pattern rule}.
2695 @xref{Static Pattern, ,Static Pattern Rules}.
2697 @node Multiple Rules, Static Pattern, Multiple Targets, Rules
2698 @section Multiple Rules for One Target
2699 @cindex multiple rules for one target
2700 @cindex several rules for one target
2701 @cindex rule, multiple for one target
2702 @cindex target, multiple rules for one
2704 One file can be the target of several rules. All the prerequisites
2705 mentioned in all the rules are merged into one list of prerequisites for
2706 the target. If the target is older than any prerequisite from any rule,
2707 the commands are executed.
2709 There can only be one set of commands to be executed for a file. If
2710 more than one rule gives commands for the same file, @code{make} uses
2711 the last set given and prints an error message. (As a special case,
2712 if the file's name begins with a dot, no error message is printed.
2713 This odd behavior is only for compatibility with other implementations
2714 of @code{make}... you should avoid using it). Occasionally it is
2715 useful to have the same target invoke multiple commands which are
2716 defined in different parts of your makefile; you can use
2717 @dfn{double-colon rules} (@pxref{Double-Colon}) for this.
2719 An extra rule with just prerequisites can be used to give a few extra
2720 prerequisites to many files at once. For example, makefiles often
2721 have a variable, such as @code{objects}, containing a list of all the
2722 compiler output files in the system being made. An easy way to say
2723 that all of them must be recompiled if @file{config.h} changes is to
2724 write the following:
2727 objects = foo.o bar.o
2729 bar.o : defs.h test.h
2730 $(objects) : config.h
2733 This could be inserted or taken out without changing the rules that really
2734 specify how to make the object files, making it a convenient form to use if
2735 you wish to add the additional prerequisite intermittently.
2737 Another wrinkle is that the additional prerequisites could be specified with
2738 a variable that you set with a command argument to @code{make}
2739 (@pxref{Overriding, ,Overriding Variables}). For example,
2744 $(objects) : $(extradeps)
2749 means that the command @samp{make extradeps=foo.h} will consider
2750 @file{foo.h} as a prerequisite of each object file, but plain @samp{make}
2753 If none of the explicit rules for a target has commands, then @code{make}
2754 searches for an applicable implicit rule to find some commands
2755 @pxref{Implicit Rules, ,Using Implicit Rules}).
2757 @node Static Pattern, Double-Colon, Multiple Rules, Rules
2758 @section Static Pattern Rules
2759 @cindex static pattern rule
2760 @cindex rule, static pattern
2761 @cindex pattern rules, static (not implicit)
2762 @cindex varying prerequisites
2763 @cindex prerequisites, varying (static pattern)
2765 @dfn{Static pattern rules} are rules which specify multiple targets and
2766 construct the prerequisite names for each target based on the target name.
2767 They are more general than ordinary rules with multiple targets because the
2768 targets do not have to have identical prerequisites. Their prerequisites must
2769 be @emph{analogous}, but not necessarily @emph{identical}.
2772 * Static Usage:: The syntax of static pattern rules.
2773 * Static versus Implicit:: When are they better than implicit rules?
2776 @node Static Usage, Static versus Implicit, Static Pattern, Static Pattern
2777 @subsection Syntax of Static Pattern Rules
2778 @cindex static pattern rule, syntax of
2779 @cindex pattern rules, static, syntax of
2781 Here is the syntax of a static pattern rule:
2784 @var{targets} @dots{}: @var{target-pattern}: @var{prereq-patterns} @dots{}
2790 The @var{targets} list specifies the targets that the rule applies to.
2791 The targets can contain wildcard characters, just like the targets of
2792 ordinary rules (@pxref{Wildcards, ,Using Wildcard Characters in File
2795 @cindex target pattern, static (not implicit)
2797 The @var{target-pattern} and @var{prereq-patterns} say how to compute the
2798 prerequisites of each target. Each target is matched against the
2799 @var{target-pattern} to extract a part of the target name, called the
2800 @dfn{stem}. This stem is substituted into each of the @var{prereq-patterns}
2801 to make the prerequisite names (one from each @var{prereq-pattern}).
2803 Each pattern normally contains the character @samp{%} just once. When the
2804 @var{target-pattern} matches a target, the @samp{%} can match any part of
2805 the target name; this part is called the @dfn{stem}. The rest of the
2806 pattern must match exactly. For example, the target @file{foo.o} matches
2807 the pattern @samp{%.o}, with @samp{foo} as the stem. The targets
2808 @file{foo.c} and @file{foo.out} do not match that pattern.@refill
2810 @cindex prerequisite pattern, static (not implicit)
2811 The prerequisite names for each target are made by substituting the stem
2812 for the @samp{%} in each prerequisite pattern. For example, if one
2813 prerequisite pattern is @file{%.c}, then substitution of the stem
2814 @samp{foo} gives the prerequisite name @file{foo.c}. It is legitimate
2815 to write a prerequisite pattern that does not contain @samp{%}; then this
2816 prerequisite is the same for all targets.
2818 @cindex @code{%}, quoting in static pattern
2819 @cindex @code{%}, quoting with @code{\} (backslash)
2820 @cindex @code{\} (backslash), to quote @code{%}
2821 @cindex backslash (@code{\}), to quote @code{%}
2822 @cindex quoting @code{%}, in static pattern
2823 @samp{%} characters in pattern rules can be quoted with preceding
2824 backslashes (@samp{\}). Backslashes that would otherwise quote @samp{%}
2825 characters can be quoted with more backslashes. Backslashes that quote
2826 @samp{%} characters or other backslashes are removed from the pattern
2827 before it is compared to file names or has a stem substituted into it.
2828 Backslashes that are not in danger of quoting @samp{%} characters go
2829 unmolested. For example, the pattern @file{the\%weird\\%pattern\\} has
2830 @samp{the%weird\} preceding the operative @samp{%} character, and
2831 @samp{pattern\\} following it. The final two backslashes are left alone
2832 because they cannot affect any @samp{%} character.@refill
2834 Here is an example, which compiles each of @file{foo.o} and @file{bar.o}
2835 from the corresponding @file{.c} file:
2839 objects = foo.o bar.o
2843 $(objects): %.o: %.c
2844 $(CC) -c $(CFLAGS) $< -o $@@
2849 Here @samp{$<} is the automatic variable that holds the name of the
2850 prerequisite and @samp{$@@} is the automatic variable that holds the name
2851 of the target; see @ref{Automatic, , Automatic Variables}.
2853 Each target specified must match the target pattern; a warning is issued
2854 for each target that does not. If you have a list of files, only some of
2855 which will match the pattern, you can use the @code{filter} function to
2856 remove nonmatching file names (@pxref{Text Functions, ,Functions for String Substitution and Analysis}):
2859 files = foo.elc bar.o lose.o
2861 $(filter %.o,$(files)): %.o: %.c
2862 $(CC) -c $(CFLAGS) $< -o $@@
2863 $(filter %.elc,$(files)): %.elc: %.el
2864 emacs -f batch-byte-compile $<
2868 In this example the result of @samp{$(filter %.o,$(files))} is
2869 @file{bar.o lose.o}, and the first static pattern rule causes each of
2870 these object files to be updated by compiling the corresponding C source
2871 file. The result of @w{@samp{$(filter %.elc,$(files))}} is
2872 @file{foo.elc}, so that file is made from @file{foo.el}.@refill
2874 Another example shows how to use @code{$*} in static pattern rules:
2875 @vindex $*@r{, and static pattern}
2879 bigoutput littleoutput : %output : text.g
2880 generate text.g -$* > $@@
2885 When the @code{generate} command is run, @code{$*} will expand to the
2886 stem, either @samp{big} or @samp{little}.
2888 @node Static versus Implicit, , Static Usage, Static Pattern
2889 @subsection Static Pattern Rules versus Implicit Rules
2890 @cindex rule, static pattern versus implicit
2891 @cindex static pattern rule, versus implicit
2893 A static pattern rule has much in common with an implicit rule defined as a
2894 pattern rule (@pxref{Pattern Rules, ,Defining and Redefining Pattern Rules}).
2895 Both have a pattern for the target and patterns for constructing the
2896 names of prerequisites. The difference is in how @code{make} decides
2897 @emph{when} the rule applies.
2899 An implicit rule @emph{can} apply to any target that matches its pattern,
2900 but it @emph{does} apply only when the target has no commands otherwise
2901 specified, and only when the prerequisites can be found. If more than one
2902 implicit rule appears applicable, only one applies; the choice depends on
2905 By contrast, a static pattern rule applies to the precise list of targets
2906 that you specify in the rule. It cannot apply to any other target and it
2907 invariably does apply to each of the targets specified. If two conflicting
2908 rules apply, and both have commands, that's an error.
2910 The static pattern rule can be better than an implicit rule for these
2915 You may wish to override the usual implicit rule for a few
2916 files whose names cannot be categorized syntactically but
2917 can be given in an explicit list.
2920 If you cannot be sure of the precise contents of the directories
2921 you are using, you may not be sure which other irrelevant files
2922 might lead @code{make} to use the wrong implicit rule. The choice
2923 might depend on the order in which the implicit rule search is done.
2924 With static pattern rules, there is no uncertainty: each rule applies
2925 to precisely the targets specified.
2928 @node Double-Colon, Automatic Prerequisites, Static Pattern, Rules
2929 @section Double-Colon Rules
2930 @cindex double-colon rules
2931 @cindex rule, double-colon (@code{::})
2932 @cindex multiple rules for one target (@code{::})
2933 @cindex @code{::} rules (double-colon)
2935 @dfn{Double-colon} rules are rules written with @samp{::} instead of
2936 @samp{:} after the target names. They are handled differently from
2937 ordinary rules when the same target appears in more than one rule.
2939 When a target appears in multiple rules, all the rules must be the same
2940 type: all ordinary, or all double-colon. If they are double-colon, each
2941 of them is independent of the others. Each double-colon rule's commands
2942 are executed if the target is older than any prerequisites of that rule.
2943 If there are no prerequisites for that rule, its commands are always
2944 executed (even if the target already exists). This can result in
2945 executing none, any, or all of the double-colon rules.
2947 Double-colon rules with the same target are in fact completely separate
2948 from one another. Each double-colon rule is processed individually, just
2949 as rules with different targets are processed.
2951 The double-colon rules for a target are executed in the order they appear
2952 in the makefile. However, the cases where double-colon rules really make
2953 sense are those where the order of executing the commands would not matter.
2955 Double-colon rules are somewhat obscure and not often very useful; they
2956 provide a mechanism for cases in which the method used to update a target
2957 differs depending on which prerequisite files caused the update, and such
2960 Each double-colon rule should specify commands; if it does not, an
2961 implicit rule will be used if one applies.
2962 @xref{Implicit Rules, ,Using Implicit Rules}.
2964 @node Automatic Prerequisites, , Double-Colon, Rules
2965 @section Generating Prerequisites Automatically
2966 @cindex prerequisites, automatic generation
2967 @cindex automatic generation of prerequisites
2968 @cindex generating prerequisites automatically
2970 In the makefile for a program, many of the rules you need to write often
2971 say only that some object file depends on some header
2972 file. For example, if @file{main.c} uses @file{defs.h} via an
2973 @code{#include}, you would write:
2980 You need this rule so that @code{make} knows that it must remake
2981 @file{main.o} whenever @file{defs.h} changes. You can see that for a
2982 large program you would have to write dozens of such rules in your
2983 makefile. And, you must always be very careful to update the makefile
2984 every time you add or remove an @code{#include}.
2985 @cindex @code{#include}
2987 @cindex @code{-M} (to compiler)
2988 To avoid this hassle, most modern C compilers can write these rules for
2989 you, by looking at the @code{#include} lines in the source files.
2990 Usually this is done with the @samp{-M} option to the compiler.
2991 For example, the command:
2998 generates the output:
3001 main.o : main.c defs.h
3005 Thus you no longer have to write all those rules yourself.
3006 The compiler will do it for you.
3008 Note that such a prerequisite constitutes mentioning @file{main.o} in a
3009 makefile, so it can never be considered an intermediate file by implicit
3010 rule search. This means that @code{make} won't ever remove the file
3011 after using it; @pxref{Chained Rules, ,Chains of Implicit Rules}.
3013 @cindex @code{make depend}
3014 With old @code{make} programs, it was traditional practice to use this
3015 compiler feature to generate prerequisites on demand with a command like
3016 @samp{make depend}. That command would create a file @file{depend}
3017 containing all the automatically-generated prerequisites; then the
3018 makefile could use @code{include} to read them in (@pxref{Include}).
3020 In GNU @code{make}, the feature of remaking makefiles makes this
3021 practice obsolete---you need never tell @code{make} explicitly to
3022 regenerate the prerequisites, because it always regenerates any makefile
3023 that is out of date. @xref{Remaking Makefiles}.
3025 The practice we recommend for automatic prerequisite generation is to have
3026 one makefile corresponding to each source file. For each source file
3027 @file{@var{name}.c} there is a makefile @file{@var{name}.d} which lists
3028 what files the object file @file{@var{name}.o} depends on. That way
3029 only the source files that have changed need to be rescanned to produce
3030 the new prerequisites.
3032 Here is the pattern rule to generate a file of prerequisites (i.e., a makefile)
3033 called @file{@var{name}.d} from a C source file called @file{@var{name}.c}:
3038 @set -e; rm -f $@@; \
3039 $(CC) -M $(CPPFLAGS) $< > $@@.$$$$; \
3040 sed 's,\($*\)\.o[ :]*,\1.o $@@ : ,g' < $@@.$$$$ > $@@; \
3046 @xref{Pattern Rules}, for information on defining pattern rules. The
3047 @samp{-e} flag to the shell causes it to exit immediately if the
3048 @code{$(CC)} command (or any other command) fails (exits with a
3050 @cindex @code{-e} (shell flag)
3052 @cindex @code{-MM} (to GNU compiler)
3053 With the GNU C compiler, you may wish to use the @samp{-MM} flag instead
3054 of @samp{-M}. This omits prerequisites on system header files.
3055 @xref{Preprocessor Options, , Options Controlling the Preprocessor,
3056 gcc.info, Using GNU CC}, for details.
3058 @cindex @code{sed} (shell command)
3059 The purpose of the @code{sed} command is to translate (for example):
3062 main.o : main.c defs.h
3069 main.o main.d : main.c defs.h
3074 This makes each @samp{.d} file depend on all the source and header files
3075 that the corresponding @samp{.o} file depends on. @code{make} then
3076 knows it must regenerate the prerequisites whenever any of the source or
3077 header files changes.
3079 Once you've defined the rule to remake the @samp{.d} files,
3080 you then use the @code{include} directive to read them all in.
3081 @xref{Include}. For example:
3085 sources = foo.c bar.c
3087 include $(sources:.c=.d)
3092 (This example uses a substitution variable reference to translate the
3093 list of source files @samp{foo.c bar.c} into a list of prerequisite
3094 makefiles, @samp{foo.d bar.d}. @xref{Substitution Refs}, for full
3095 information on substitution references.) Since the @samp{.d} files are
3096 makefiles like any others, @code{make} will remake them as necessary
3097 with no further work from you. @xref{Remaking Makefiles}.
3099 Note that the @samp{.d} files contain target definitions; you should
3100 be sure to place the @code{include} directive @emph{after} the first,
3101 default target in your makefiles or run the risk of having a random
3102 object file become the default target.
3103 @xref{How Make Works}.
3105 @node Commands, Using Variables, Rules, Top
3106 @chapter Writing the Commands in Rules
3107 @cindex commands, how to write
3108 @cindex rule commands
3109 @cindex writing rule commands
3111 The commands of a rule consist of shell command lines to be executed one
3112 by one. Each command line must start with a tab, except that the first
3113 command line may be attached to the target-and-prerequisites line with a
3114 semicolon in between. Blank lines and lines of just comments may appear
3115 among the command lines; they are ignored. (But beware, an apparently
3116 ``blank'' line that begins with a tab is @emph{not} blank! It is an
3117 empty command; @pxref{Empty Commands}.)
3119 Users use many different shell programs, but commands in makefiles are
3120 always interpreted by @file{/bin/sh} unless the makefile specifies
3121 otherwise. @xref{Execution, ,Command Execution}.
3123 @cindex comments, in commands
3124 @cindex commands, comments in
3125 @cindex @code{#} (comments), in commands
3126 The shell that is in use determines whether comments can be written on
3127 command lines, and what syntax they use. When the shell is
3128 @file{/bin/sh}, a @samp{#} starts a comment that extends to the end of
3129 the line. The @samp{#} does not have to be at the beginning of a line.
3130 Text on a line before a @samp{#} is not part of the comment.
3133 * Echoing:: How to control when commands are echoed.
3134 * Execution:: How commands are executed.
3135 * Parallel:: How commands can be executed in parallel.
3136 * Errors:: What happens after a command execution error.
3137 * Interrupts:: What happens when a command is interrupted.
3138 * Recursion:: Invoking @code{make} from makefiles.
3139 * Sequences:: Defining canned sequences of commands.
3140 * Empty Commands:: Defining useful, do-nothing commands.
3143 @node Echoing, Execution, Commands, Commands
3144 @section Command Echoing
3145 @cindex echoing of commands
3146 @cindex silent operation
3147 @cindex @code{@@} (in commands)
3148 @cindex commands, echoing
3149 @cindex printing of commands
3151 Normally @code{make} prints each command line before it is executed.
3152 We call this @dfn{echoing} because it gives the appearance that you
3153 are typing the commands yourself.
3155 When a line starts with @samp{@@}, the echoing of that line is suppressed.
3156 The @samp{@@} is discarded before the command is passed to the shell.
3157 Typically you would use this for a command whose only effect is to print
3158 something, such as an @code{echo} command to indicate progress through
3162 @@echo About to make distribution files
3166 @cindex @code{--just-print}
3167 @cindex @code{--dry-run}
3168 @cindex @code{--recon}
3169 When @code{make} is given the flag @samp{-n} or @samp{--just-print}
3170 it only echoes commands, it won't execute them. @xref{Options Summary,
3171 ,Summary of Options}. In this case and only this case, even the
3172 commands starting with @samp{@@} are printed. This flag is useful for
3173 finding out which commands @code{make} thinks are necessary without
3174 actually doing them.
3177 @cindex @code{--silent}
3178 @cindex @code{--quiet}
3180 The @samp{-s} or @samp{--silent}
3181 flag to @code{make} prevents all echoing, as if all commands
3182 started with @samp{@@}. A rule in the makefile for the special target
3183 @code{.SILENT} without prerequisites has the same effect
3184 (@pxref{Special Targets, ,Special Built-in Target Names}).
3185 @code{.SILENT} is essentially obsolete since @samp{@@} is more flexible.@refill
3187 @node Execution, Parallel, Echoing, Commands
3188 @section Command Execution
3189 @cindex commands, execution
3190 @cindex execution, of commands
3191 @cindex shell command, execution
3192 @vindex SHELL @r{(command execution)}
3194 When it is time to execute commands to update a target, they are executed
3195 by making a new subshell for each line. (In practice, @code{make} may
3196 take shortcuts that do not affect the results.)
3198 @cindex @code{cd} (shell command)
3199 @strong{Please note:} this implies that shell commands such as @code{cd}
3200 that set variables local to each process will not affect the following
3201 command lines. @footnote{On MS-DOS, the value of current working
3202 directory is @strong{global}, so changing it @emph{will} affect the
3203 following command lines on those systems.} If you want to use @code{cd}
3204 to affect the next command, put the two on a single line with a
3205 semicolon between them. Then @code{make} will consider them a single
3206 command and pass them, together, to a shell which will execute them in
3207 sequence. For example:
3211 cd bar; gobble lose > ../foo
3214 @cindex commands, backslash (@code{\}) in
3215 @cindex commands, quoting newlines in
3216 @cindex backslash (@code{\}), in commands
3217 @cindex @code{\} (backslash), in commands
3218 @cindex quoting newline, in commands
3219 @cindex newline, quoting, in commands
3220 If you would like to split a single shell command into multiple lines of
3221 text, you must use a backslash at the end of all but the last subline.
3222 Such a sequence of lines is combined into a single line, by deleting the
3223 backslash-newline sequences, before passing it to the shell. Thus, the
3224 following is equivalent to the preceding example:
3230 gobble lose > ../foo
3235 The program used as the shell is taken from the variable @code{SHELL}.
3236 By default, the program @file{/bin/sh} is used.
3239 On MS-DOS, if @code{SHELL} is not set, the value of the variable
3240 @code{COMSPEC} (which is always set) is used instead.
3242 @cindex @code{SHELL}, MS-DOS specifics
3243 The processing of lines that set the variable @code{SHELL} in Makefiles
3244 is different on MS-DOS. The stock shell, @file{command.com}, is
3245 ridiculously limited in its functionality and many users of @code{make}
3246 tend to install a replacement shell. Therefore, on MS-DOS, @code{make}
3247 examines the value of @code{SHELL}, and changes its behavior based on
3248 whether it points to a Unix-style or DOS-style shell. This allows
3249 reasonable functionality even if @code{SHELL} points to
3252 If @code{SHELL} points to a Unix-style shell, @code{make} on MS-DOS
3253 additionally checks whether that shell can indeed be found; if not, it
3254 ignores the line that sets @code{SHELL}. In MS-DOS, GNU @code{make}
3255 searches for the shell in the following places:
3259 In the precise place pointed to by the value of @code{SHELL}. For
3260 example, if the makefile specifies @samp{SHELL = /bin/sh}, @code{make}
3261 will look in the directory @file{/bin} on the current drive.
3264 In the current directory.
3267 In each of the directories in the @code{PATH} variable, in order.
3271 In every directory it examines, @code{make} will first look for the
3272 specific file (@file{sh} in the example above). If this is not found,
3273 it will also look in that directory for that file with one of the known
3274 extensions which identify executable files. For example @file{.exe},
3275 @file{.com}, @file{.bat}, @file{.btm}, @file{.sh}, and some others.
3277 If any of these attempts is successful, the value of @code{SHELL} will
3278 be set to the full pathname of the shell as found. However, if none of
3279 these is found, the value of @code{SHELL} will not be changed, and thus
3280 the line that sets it will be effectively ignored. This is so
3281 @code{make} will only support features specific to a Unix-style shell if
3282 such a shell is actually installed on the system where @code{make} runs.
3284 Note that this extended search for the shell is limited to the cases
3285 where @code{SHELL} is set from the Makefile; if it is set in the
3286 environment or command line, you are expected to set it to the full
3287 pathname of the shell, exactly as things are on Unix.
3289 The effect of the above DOS-specific processing is that a Makefile that
3290 says @samp{SHELL = /bin/sh} (as many Unix makefiles do), will work
3291 on MS-DOS unaltered if you have e.g. @file{sh.exe} installed in some
3292 directory along your @code{PATH}.
3294 @cindex environment, @code{SHELL} in
3295 Unlike most variables, the variable @code{SHELL} is never set from the
3296 environment. This is because the @code{SHELL} environment variable is
3297 used to specify your personal choice of shell program for interactive
3298 use. It would be very bad for personal choices like this to affect the
3299 functioning of makefiles. @xref{Environment, ,Variables from the
3300 Environment}. However, on MS-DOS and MS-Windows the value of
3301 @code{SHELL} in the environment @strong{is} used, since on those systems
3302 most users do not set this variable, and therefore it is most likely set
3303 specifically to be used by @code{make}. On MS-DOS, if the setting of
3304 @code{SHELL} is not suitable for @code{make}, you can set the variable
3305 @code{MAKESHELL} to the shell that @code{make} should use; this will
3306 override the value of @code{SHELL}.
3308 @node Parallel, Errors, Execution, Commands
3309 @section Parallel Execution
3310 @cindex commands, execution in parallel
3311 @cindex parallel execution
3312 @cindex execution, in parallel
3315 @cindex @code{--jobs}
3317 GNU @code{make} knows how to execute several commands at once.
3318 Normally, @code{make} will execute only one command at a time, waiting
3319 for it to finish before executing the next. However, the @samp{-j} or
3320 @samp{--jobs} option tells @code{make} to execute many commands
3321 simultaneously.@refill
3323 On MS-DOS, the @samp{-j} option has no effect, since that system doesn't
3324 support multi-processing.
3326 If the @samp{-j} option is followed by an integer, this is the number of
3327 commands to execute at once; this is called the number of @dfn{job slots}.
3328 If there is nothing looking like an integer after the @samp{-j} option,
3329 there is no limit on the number of job slots. The default number of job
3330 slots is one, which means serial execution (one thing at a time).
3332 One unpleasant consequence of running several commands simultaneously is
3333 that output generated by the commands appears whenever each command
3334 sends it, so messages from different commands may be interspersed.
3336 Another problem is that two processes cannot both take input from the
3337 same device; so to make sure that only one command tries to take input
3338 from the terminal at once, @code{make} will invalidate the standard
3339 input streams of all but one running command. This means that
3340 attempting to read from standard input will usually be a fatal error (a
3341 @samp{Broken pipe} signal) for most child processes if there are
3344 @cindex standard input
3346 It is unpredictable which command will have a valid standard input stream
3347 (which will come from the terminal, or wherever you redirect the standard
3348 input of @code{make}). The first command run will always get it first, and
3349 the first command started after that one finishes will get it next, and so
3352 We will change how this aspect of @code{make} works if we find a better
3353 alternative. In the mean time, you should not rely on any command using
3354 standard input at all if you are using the parallel execution feature; but
3355 if you are not using this feature, then standard input works normally in
3358 Finally, handling recursive @code{make} invocations raises issues. For
3359 more information on this, see
3360 @ref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
3362 If a command fails (is killed by a signal or exits with a nonzero
3363 status), and errors are not ignored for that command
3364 (@pxref{Errors, ,Errors in Commands}),
3365 the remaining command lines to remake the same target will not be run.
3366 If a command fails and the @samp{-k} or @samp{--keep-going}
3367 option was not given
3368 (@pxref{Options Summary, ,Summary of Options}),
3369 @code{make} aborts execution. If make
3370 terminates for any reason (including a signal) with child processes
3371 running, it waits for them to finish before actually exiting.@refill
3373 @cindex load average
3374 @cindex limiting jobs based on load
3375 @cindex jobs, limiting based on load
3376 @cindex @code{-l} (load average)
3377 @cindex @code{--max-load}
3378 @cindex @code{--load-average}
3379 When the system is heavily loaded, you will probably want to run fewer jobs
3380 than when it is lightly loaded. You can use the @samp{-l} option to tell
3381 @code{make} to limit the number of jobs to run at once, based on the load
3382 average. The @samp{-l} or @samp{--max-load}
3383 option is followed by a floating-point number. For
3391 will not let @code{make} start more than one job if the load average is
3392 above 2.5. The @samp{-l} option with no following number removes the
3393 load limit, if one was given with a previous @samp{-l} option.@refill
3395 More precisely, when @code{make} goes to start up a job, and it already has
3396 at least one job running, it checks the current load average; if it is not
3397 lower than the limit given with @samp{-l}, @code{make} waits until the load
3398 average goes below that limit, or until all the other jobs finish.
3400 By default, there is no load limit.
3402 @node Errors, Interrupts, Parallel, Commands
3403 @section Errors in Commands
3404 @cindex errors (in commands)
3405 @cindex commands, errors in
3406 @cindex exit status (errors)
3408 After each shell command returns, @code{make} looks at its exit status.
3409 If the command completed successfully, the next command line is executed
3410 in a new shell; after the last command line is finished, the rule is
3413 If there is an error (the exit status is nonzero), @code{make} gives up on
3414 the current rule, and perhaps on all rules.
3416 Sometimes the failure of a certain command does not indicate a problem.
3417 For example, you may use the @code{mkdir} command to ensure that a
3418 directory exists. If the directory already exists, @code{mkdir} will
3419 report an error, but you probably want @code{make} to continue regardless.
3421 @cindex @code{-} (in commands)
3422 To ignore errors in a command line, write a @samp{-} at the beginning of
3423 the line's text (after the initial tab). The @samp{-} is discarded before
3424 the command is passed to the shell for execution.
3434 @cindex @code{rm} (shell command)
3437 This causes @code{rm} to continue even if it is unable to remove a file.
3440 @cindex @code{--ignore-errors}
3442 When you run @code{make} with the @samp{-i} or @samp{--ignore-errors}
3443 flag, errors are ignored in all commands of all rules. A rule in the
3444 makefile for the special target @code{.IGNORE} has the same effect, if
3445 there are no prerequisites. These ways of ignoring errors are obsolete
3446 because @samp{-} is more flexible.
3448 When errors are to be ignored, because of either a @samp{-} or the
3449 @samp{-i} flag, @code{make} treats an error return just like success,
3450 except that it prints out a message that tells you the status code
3451 the command exited with, and says that the error has been ignored.
3453 When an error happens that @code{make} has not been told to ignore,
3454 it implies that the current target cannot be correctly remade, and neither
3455 can any other that depends on it either directly or indirectly. No further
3456 commands will be executed for these targets, since their preconditions
3457 have not been achieved.
3461 @cindex @code{--keep-going}
3462 Normally @code{make} gives up immediately in this circumstance, returning a
3463 nonzero status. However, if the @samp{-k} or @samp{--keep-going}
3464 flag is specified, @code{make}
3465 continues to consider the other prerequisites of the pending targets,
3466 remaking them if necessary, before it gives up and returns nonzero status.
3467 For example, after an error in compiling one object file, @samp{make -k}
3468 will continue compiling other object files even though it already knows
3469 that linking them will be impossible. @xref{Options Summary, ,Summary of Options}.
3471 The usual behavior assumes that your purpose is to get the specified
3472 targets up to date; once @code{make} learns that this is impossible, it
3473 might as well report the failure immediately. The @samp{-k} option says
3474 that the real purpose is to test as many of the changes made in the
3475 program as possible, perhaps to find several independent problems so
3476 that you can correct them all before the next attempt to compile. This
3477 is why Emacs' @code{compile} command passes the @samp{-k} flag by
3479 @cindex Emacs (@code{M-x compile})
3481 @findex .DELETE_ON_ERROR
3482 @cindex deletion of target files
3483 @cindex removal of target files
3484 @cindex target, deleting on error
3485 Usually when a command fails, if it has changed the target file at all,
3486 the file is corrupted and cannot be used---or at least it is not
3487 completely updated. Yet the file's time stamp says that it is now up to
3488 date, so the next time @code{make} runs, it will not try to update that
3489 file. The situation is just the same as when the command is killed by a
3490 signal; @pxref{Interrupts}. So generally the right thing to do is to
3491 delete the target file if the command fails after beginning to change
3492 the file. @code{make} will do this if @code{.DELETE_ON_ERROR} appears
3493 as a target. This is almost always what you want @code{make} to do, but
3494 it is not historical practice; so for compatibility, you must explicitly
3497 @node Interrupts, Recursion, Errors, Commands
3498 @section Interrupting or Killing @code{make}
3501 @cindex deletion of target files
3502 @cindex removal of target files
3503 @cindex target, deleting on interrupt
3504 @cindex killing (interruption)
3506 If @code{make} gets a fatal signal while a command is executing, it may
3507 delete the target file that the command was supposed to update. This is
3508 done if the target file's last-modification time has changed since
3509 @code{make} first checked it.
3511 The purpose of deleting the target is to make sure that it is remade from
3512 scratch when @code{make} is next run. Why is this? Suppose you type
3513 @kbd{Ctrl-c} while a compiler is running, and it has begun to write an
3514 object file @file{foo.o}. The @kbd{Ctrl-c} kills the compiler, resulting
3515 in an incomplete file whose last-modification time is newer than the source
3516 file @file{foo.c}. But @code{make} also receives the @kbd{Ctrl-c} signal
3517 and deletes this incomplete file. If @code{make} did not do this, the next
3518 invocation of @code{make} would think that @file{foo.o} did not require
3519 updating---resulting in a strange error message from the linker when it
3520 tries to link an object file half of which is missing.
3523 You can prevent the deletion of a target file in this way by making the
3524 special target @code{.PRECIOUS} depend on it. Before remaking a target,
3525 @code{make} checks to see whether it appears on the prerequisites of
3526 @code{.PRECIOUS}, and thereby decides whether the target should be deleted
3527 if a signal happens. Some reasons why you might do this are that the
3528 target is updated in some atomic fashion, or exists only to record a
3529 modification-time (its contents do not matter), or must exist at all
3530 times to prevent other sorts of trouble.
3532 @node Recursion, Sequences, Interrupts, Commands
3533 @section Recursive Use of @code{make}
3535 @cindex subdirectories, recursion for
3537 Recursive use of @code{make} means using @code{make} as a command in a
3538 makefile. This technique is useful when you want separate makefiles for
3539 various subsystems that compose a larger system. For example, suppose you
3540 have a subdirectory @file{subdir} which has its own makefile, and you would
3541 like the containing directory's makefile to run @code{make} on the
3542 subdirectory. You can do it by writing this:
3546 cd subdir && $(MAKE)
3550 or, equivalently, this (@pxref{Options Summary, ,Summary of Options}):
3557 @cindex @code{--directory}
3559 You can write recursive @code{make} commands just by copying this example,
3560 but there are many things to know about how they work and why, and about
3561 how the sub-@code{make} relates to the top-level @code{make}.
3563 For your convenience, GNU @code{make} sets the variable @code{CURDIR} to
3564 the pathname of the current working directory for you. If @code{-C} is
3565 in effect, it will contain the path of the new directory, not the
3566 original. The value has the same precedence it would have if it were
3567 set in the makefile (by default, an environment variable @code{CURDIR}
3568 will not override this value). Note that setting this variable has no
3569 effect on the operation of @code{make}
3572 * MAKE Variable:: The special effects of using @samp{$(MAKE)}.
3573 * Variables/Recursion:: How to communicate variables to a sub-@code{make}.
3574 * Options/Recursion:: How to communicate options to a sub-@code{make}.
3575 * -w Option:: How the @samp{-w} or @samp{--print-directory} option
3576 helps debug use of recursive @code{make} commands.
3579 @node MAKE Variable, Variables/Recursion, Recursion, Recursion
3580 @subsection How the @code{MAKE} Variable Works
3582 @cindex recursion, and @code{MAKE} variable
3584 Recursive @code{make} commands should always use the variable @code{MAKE},
3585 not the explicit command name @samp{make}, as shown here:
3590 cd subdir && $(MAKE)
3594 The value of this variable is the file name with which @code{make} was
3595 invoked. If this file name was @file{/bin/make}, then the command executed
3596 is @samp{cd subdir && /bin/make}. If you use a special version of
3597 @code{make} to run the top-level makefile, the same special version will be
3598 executed for recursive invocations.
3599 @cindex @code{cd} (shell command)
3601 As a special feature, using the variable @code{MAKE} in the commands of
3602 a rule alters the effects of the @samp{-t} (@samp{--touch}), @samp{-n}
3603 (@samp{--just-print}), or @samp{-q} (@w{@samp{--question}}) option.
3604 Using the @code{MAKE} variable has the same effect as using a @samp{+}
3605 character at the beginning of the command line. @xref{Instead of
3606 Execution, ,Instead of Executing the Commands}.@refill
3608 Consider the command @samp{make -t} in the above example. (The
3609 @samp{-t} option marks targets as up to date without actually running
3610 any commands; see @ref{Instead of Execution}.) Following the usual
3611 definition of @samp{-t}, a @samp{make -t} command in the example would
3612 create a file named @file{subsystem} and do nothing else. What you
3613 really want it to do is run @samp{@w{cd subdir &&} @w{make -t}}; but that would
3614 require executing the command, and @samp{-t} says not to execute
3616 @cindex @code{-t}, and recursion
3617 @cindex recursion, and @code{-t}
3618 @cindex @code{--touch}, and recursion
3620 The special feature makes this do what you want: whenever a command
3621 line of a rule contains the variable @code{MAKE}, the flags @samp{-t},
3622 @samp{-n} and @samp{-q} do not apply to that line. Command lines
3623 containing @code{MAKE} are executed normally despite the presence of a
3624 flag that causes most commands not to be run. The usual
3625 @code{MAKEFLAGS} mechanism passes the flags to the sub-@code{make}
3626 (@pxref{Options/Recursion, ,Communicating Options to a
3627 Sub-@code{make}}), so your request to touch the files, or print the
3628 commands, is propagated to the subsystem.@refill
3630 @node Variables/Recursion, Options/Recursion, MAKE Variable, Recursion
3631 @subsection Communicating Variables to a Sub-@code{make}
3632 @cindex sub-@code{make}
3633 @cindex environment, and recursion
3634 @cindex exporting variables
3635 @cindex variables, environment
3636 @cindex variables, exporting
3637 @cindex recursion, and environment
3638 @cindex recursion, and variables
3640 Variable values of the top-level @code{make} can be passed to the
3641 sub-@code{make} through the environment by explicit request. These
3642 variables are defined in the sub-@code{make} as defaults, but do not
3643 override what is specified in the makefile used by the sub-@code{make}
3644 makefile unless you use the @samp{-e} switch (@pxref{Options Summary,
3645 ,Summary of Options}).@refill
3647 To pass down, or @dfn{export}, a variable, @code{make} adds the variable
3648 and its value to the environment for running each command. The
3649 sub-@code{make}, in turn, uses the environment to initialize its table
3650 of variable values. @xref{Environment, ,Variables from the
3653 Except by explicit request, @code{make} exports a variable only if it
3654 is either defined in the environment initially or set on the command
3655 line, and if its name consists only of letters, numbers, and underscores.
3656 Some shells cannot cope with environment variable names consisting of
3657 characters other than letters, numbers, and underscores.
3659 The special variables @code{SHELL} and @code{MAKEFLAGS} are always
3660 exported (unless you unexport them).
3661 @code{MAKEFILES} is exported if you set it to anything.
3663 @code{make} automatically passes down variable values that were defined
3664 on the command line, by putting them in the @code{MAKEFLAGS} variable.
3666 See the next section.
3669 @xref{Options/Recursion}.
3672 Variables are @emph{not} normally passed down if they were created by
3673 default by @code{make} (@pxref{Implicit Variables, ,Variables Used by
3674 Implicit Rules}). The sub-@code{make} will define these for
3678 If you want to export specific variables to a sub-@code{make}, use the
3679 @code{export} directive, like this:
3682 export @var{variable} @dots{}
3687 If you want to @emph{prevent} a variable from being exported, use the
3688 @code{unexport} directive, like this:
3691 unexport @var{variable} @dots{}
3695 As a convenience, you can define a variable and export it at the same
3699 export @var{variable} = value
3703 has the same result as:
3706 @var{variable} = value
3707 export @var{variable}
3714 export @var{variable} := value
3718 has the same result as:
3721 @var{variable} := value
3722 export @var{variable}
3728 export @var{variable} += value
3735 @var{variable} += value
3736 export @var{variable}
3740 @xref{Appending, ,Appending More Text to Variables}.
3742 You may notice that the @code{export} and @code{unexport} directives
3743 work in @code{make} in the same way they work in the shell, @code{sh}.
3745 If you want all variables to be exported by default, you can use
3746 @code{export} by itself:
3753 This tells @code{make} that variables which are not explicitly mentioned
3754 in an @code{export} or @code{unexport} directive should be exported.
3755 Any variable given in an @code{unexport} directive will still @emph{not}
3756 be exported. If you use @code{export} by itself to export variables by
3757 default, variables whose names contain characters other than
3758 alphanumerics and underscores will not be exported unless specifically
3759 mentioned in an @code{export} directive.@refill
3761 @findex .EXPORT_ALL_VARIABLES
3762 The behavior elicited by an @code{export} directive by itself was the
3763 default in older versions of GNU @code{make}. If your makefiles depend
3764 on this behavior and you want to be compatible with old versions of
3765 @code{make}, you can write a rule for the special target
3766 @code{.EXPORT_ALL_VARIABLES} instead of using the @code{export} directive.
3767 This will be ignored by old @code{make}s, while the @code{export}
3768 directive will cause a syntax error.@refill
3769 @cindex compatibility in exporting
3771 Likewise, you can use @code{unexport} by itself to tell @code{make}
3772 @emph{not} to export variables by default. Since this is the default
3773 behavior, you would only need to do this if @code{export} had been used
3774 by itself earlier (in an included makefile, perhaps). You
3775 @strong{cannot} use @code{export} and @code{unexport} by themselves to
3776 have variables exported for some commands and not for others. The last
3777 @code{export} or @code{unexport} directive that appears by itself
3778 determines the behavior for the entire run of @code{make}.@refill
3781 @cindex recursion, level of
3782 As a special feature, the variable @code{MAKELEVEL} is changed when it
3783 is passed down from level to level. This variable's value is a string
3784 which is the depth of the level as a decimal number. The value is
3785 @samp{0} for the top-level @code{make}; @samp{1} for a sub-@code{make},
3786 @samp{2} for a sub-sub-@code{make}, and so on. The incrementation
3787 happens when @code{make} sets up the environment for a command.@refill
3789 The main use of @code{MAKELEVEL} is to test it in a conditional
3790 directive (@pxref{Conditionals, ,Conditional Parts of Makefiles}); this
3791 way you can write a makefile that behaves one way if run recursively and
3792 another way if run directly by you.@refill
3795 You can use the variable @code{MAKEFILES} to cause all sub-@code{make}
3796 commands to use additional makefiles. The value of @code{MAKEFILES} is
3797 a whitespace-separated list of file names. This variable, if defined in
3798 the outer-level makefile, is passed down through the environment; then
3799 it serves as a list of extra makefiles for the sub-@code{make} to read
3800 before the usual or specified ones. @xref{MAKEFILES Variable, ,The
3801 Variable @code{MAKEFILES}}.@refill
3803 @node Options/Recursion, -w Option, Variables/Recursion, Recursion
3804 @subsection Communicating Options to a Sub-@code{make}
3805 @cindex options, and recursion
3806 @cindex recursion, and options
3809 Flags such as @samp{-s} and @samp{-k} are passed automatically to the
3810 sub-@code{make} through the variable @code{MAKEFLAGS}. This variable is
3811 set up automatically by @code{make} to contain the flag letters that
3812 @code{make} received. Thus, if you do @w{@samp{make -ks}} then
3813 @code{MAKEFLAGS} gets the value @samp{ks}.@refill
3815 As a consequence, every sub-@code{make} gets a value for @code{MAKEFLAGS}
3816 in its environment. In response, it takes the flags from that value and
3817 processes them as if they had been given as arguments.
3818 @xref{Options Summary, ,Summary of Options}.
3820 @cindex command line variable definitions, and recursion
3821 @cindex variables, command line, and recursion
3822 @cindex recursion, and command line variable definitions
3823 Likewise variables defined on the command line are passed to the
3824 sub-@code{make} through @code{MAKEFLAGS}. Words in the value of
3825 @code{MAKEFLAGS} that contain @samp{=}, @code{make} treats as variable
3826 definitions just as if they appeared on the command line.
3827 @xref{Overriding, ,Overriding Variables}.
3829 @cindex @code{-C}, and recursion
3830 @cindex @code{-f}, and recursion
3831 @cindex @code{-o}, and recursion
3832 @cindex @code{-W}, and recursion
3833 @cindex @code{--directory}, and recursion
3834 @cindex @code{--file}, and recursion
3835 @cindex @code{--old-file}, and recursion
3836 @cindex @code{--assume-old}, and recursion
3837 @cindex @code{--assume-new}, and recursion
3838 @cindex @code{--new-file}, and recursion
3839 @cindex recursion, and @code{-C}
3840 @cindex recursion, and @code{-f}
3841 @cindex recursion, and @code{-o}
3842 @cindex recursion, and @code{-W}
3843 The options @samp{-C}, @samp{-f}, @samp{-o}, and @samp{-W} are not put
3844 into @code{MAKEFLAGS}; these options are not passed down.@refill
3846 @cindex @code{-j}, and recursion
3847 @cindex @code{--jobs}, and recursion
3848 @cindex recursion, and @code{-j}
3849 @cindex job slots, and recursion
3850 The @samp{-j} option is a special case (@pxref{Parallel, ,Parallel Execution}).
3851 If you set it to some numeric value @samp{N} and your operating system
3852 supports it (most any UNIX system will; others typically won't), the
3853 parent @code{make} and all the sub-@code{make}s will communicate to
3854 ensure that there are only @samp{N} jobs running at the same time
3855 between them all. Note that any job that is marked recursive
3856 (@pxref{Instead of Execution, ,Instead of Executing the Commands})
3857 doesn't count against the total jobs (otherwise we could get @samp{N}
3858 sub-@code{make}s running and have no slots left over for any real work!)
3860 If your operating system doesn't support the above communication, then
3861 @samp{-j 1} is always put into @code{MAKEFLAGS} instead of the value you
3862 specified. This is because if the @w{@samp{-j}} option were passed down
3863 to sub-@code{make}s, you would get many more jobs running in parallel
3864 than you asked for. If you give @samp{-j} with no numeric argument,
3865 meaning to run as many jobs as possible in parallel, this is passed
3866 down, since multiple infinities are no more than one.@refill
3868 If you do not want to pass the other flags down, you must change the
3869 value of @code{MAKEFLAGS}, like this:
3873 cd subdir && $(MAKE) MAKEFLAGS=
3876 @vindex MAKEOVERRIDES
3877 The command line variable definitions really appear in the variable
3878 @code{MAKEOVERRIDES}, and @code{MAKEFLAGS} contains a reference to this
3879 variable. If you do want to pass flags down normally, but don't want to
3880 pass down the command line variable definitions, you can reset
3881 @code{MAKEOVERRIDES} to empty, like this:
3888 @cindex Arg list too long
3890 This is not usually useful to do. However, some systems have a small
3891 fixed limit on the size of the environment, and putting so much
3892 information into the value of @code{MAKEFLAGS} can exceed it. If you
3893 see the error message @samp{Arg list too long}, this may be the problem.
3896 (For strict compliance with POSIX.2, changing @code{MAKEOVERRIDES} does
3897 not affect @code{MAKEFLAGS} if the special target @samp{.POSIX} appears
3898 in the makefile. You probably do not care about this.)
3901 A similar variable @code{MFLAGS} exists also, for historical
3902 compatibility. It has the same value as @code{MAKEFLAGS} except that it
3903 does not contain the command line variable definitions, and it always
3904 begins with a hyphen unless it is empty (@code{MAKEFLAGS} begins with a
3905 hyphen only when it begins with an option that has no single-letter
3906 version, such as @samp{--warn-undefined-variables}). @code{MFLAGS} was
3907 traditionally used explicitly in the recursive @code{make} command, like
3912 cd subdir && $(MAKE) $(MFLAGS)
3916 but now @code{MAKEFLAGS} makes this usage redundant. If you want your
3917 makefiles to be compatible with old @code{make} programs, use this
3918 technique; it will work fine with more modern @code{make} versions too.
3920 @cindex setting options from environment
3921 @cindex options, setting from environment
3922 @cindex setting options in makefiles
3923 @cindex options, setting in makefiles
3924 The @code{MAKEFLAGS} variable can also be useful if you want to have
3925 certain options, such as @samp{-k} (@pxref{Options Summary, ,Summary of
3926 Options}), set each time you run @code{make}. You simply put a value for
3927 @code{MAKEFLAGS} in your environment. You can also set @code{MAKEFLAGS} in
3928 a makefile, to specify additional flags that should also be in effect for
3929 that makefile. (Note that you cannot use @code{MFLAGS} this way. That
3930 variable is set only for compatibility; @code{make} does not interpret a
3931 value you set for it in any way.)
3933 When @code{make} interprets the value of @code{MAKEFLAGS} (either from the
3934 environment or from a makefile), it first prepends a hyphen if the value
3935 does not already begin with one. Then it chops the value into words
3936 separated by blanks, and parses these words as if they were options given
3937 on the command line (except that @samp{-C}, @samp{-f}, @samp{-h},
3938 @samp{-o}, @samp{-W}, and their long-named versions are ignored; and there
3939 is no error for an invalid option).
3941 If you do put @code{MAKEFLAGS} in your environment, you should be sure not
3942 to include any options that will drastically affect the actions of
3943 @code{make} and undermine the purpose of makefiles and of @code{make}
3944 itself. For instance, the @samp{-t}, @samp{-n}, and @samp{-q} options, if
3945 put in one of these variables, could have disastrous consequences and would
3946 certainly have at least surprising and probably annoying effects.@refill
3948 @node -w Option, , Options/Recursion, Recursion
3949 @subsection The @samp{--print-directory} Option
3950 @cindex directories, printing them
3951 @cindex printing directories
3952 @cindex recursion, and printing directories
3954 If you use several levels of recursive @code{make} invocations, the
3955 @samp{-w} or @w{@samp{--print-directory}} option can make the output a
3956 lot easier to understand by showing each directory as @code{make}
3957 starts processing it and as @code{make} finishes processing it. For
3958 example, if @samp{make -w} is run in the directory @file{/u/gnu/make},
3959 @code{make} will print a line of the form:@refill
3962 make: Entering directory `/u/gnu/make'.
3966 before doing anything else, and a line of the form:
3969 make: Leaving directory `/u/gnu/make'.
3973 when processing is completed.
3975 @cindex @code{-C}, and @code{-w}
3976 @cindex @code{--directory}, and @code{--print-directory}
3977 @cindex recursion, and @code{-w}
3978 @cindex @code{-w}, and @code{-C}
3979 @cindex @code{-w}, and recursion
3980 @cindex @code{--print-directory}, and @code{--directory}
3981 @cindex @code{--print-directory}, and recursion
3982 @cindex @code{--no-print-directory}
3983 @cindex @code{--print-directory}, disabling
3984 @cindex @code{-w}, disabling
3985 Normally, you do not need to specify this option because @samp{make}
3986 does it for you: @samp{-w} is turned on automatically when you use the
3987 @samp{-C} option, and in sub-@code{make}s. @code{make} will not
3988 automatically turn on @samp{-w} if you also use @samp{-s}, which says to
3989 be silent, or if you use @samp{--no-print-directory} to explicitly
3992 @node Sequences, Empty Commands, Recursion, Commands
3993 @section Defining Canned Command Sequences
3994 @cindex sequences of commands
3995 @cindex commands, sequences of
3997 When the same sequence of commands is useful in making various targets, you
3998 can define it as a canned sequence with the @code{define} directive, and
3999 refer to the canned sequence from the rules for those targets. The canned
4000 sequence is actually a variable, so the name must not conflict with other
4003 Here is an example of defining a canned sequence of commands:
4007 yacc $(firstword $^)
4014 Here @code{run-yacc} is the name of the variable being defined;
4015 @code{endef} marks the end of the definition; the lines in between are the
4016 commands. The @code{define} directive does not expand variable references
4017 and function calls in the canned sequence; the @samp{$} characters,
4018 parentheses, variable names, and so on, all become part of the value of the
4019 variable you are defining.
4020 @xref{Defining, ,Defining Variables Verbatim},
4021 for a complete explanation of @code{define}.
4023 The first command in this example runs Yacc on the first prerequisite of
4024 whichever rule uses the canned sequence. The output file from Yacc is
4025 always named @file{y.tab.c}. The second command moves the output to the
4026 rule's target file name.
4028 To use the canned sequence, substitute the variable into the commands of a
4029 rule. You can substitute it like any other variable
4030 (@pxref{Reference, ,Basics of Variable References}).
4031 Because variables defined by @code{define} are recursively expanded
4032 variables, all the variable references you wrote inside the @code{define}
4033 are expanded now. For example:
4041 @samp{foo.y} will be substituted for the variable @samp{$^} when it occurs in
4042 @code{run-yacc}'s value, and @samp{foo.c} for @samp{$@@}.@refill
4044 This is a realistic example, but this particular one is not needed in
4045 practice because @code{make} has an implicit rule to figure out these
4046 commands based on the file names involved
4047 (@pxref{Implicit Rules, ,Using Implicit Rules}).
4049 @cindex @@, and @code{define}
4050 @cindex -, and @code{define}
4051 @cindex +, and @code{define}
4052 In command execution, each line of a canned sequence is treated just as
4053 if the line appeared on its own in the rule, preceded by a tab. In
4054 particular, @code{make} invokes a separate subshell for each line. You
4055 can use the special prefix characters that affect command lines
4056 (@samp{@@}, @samp{-}, and @samp{+}) on each line of a canned sequence.
4057 @xref{Commands, ,Writing the Commands in Rules}.
4058 For example, using this canned sequence:
4062 @@echo "frobnicating target $@@"
4063 frob-step-1 $< -o $@@-step-1
4064 frob-step-2 $@@-step-1 -o $@@
4069 @code{make} will not echo the first line, the @code{echo} command.
4070 But it @emph{will} echo the following two command lines.
4072 On the other hand, prefix characters on the command line that refers to
4073 a canned sequence apply to every line in the sequence. So the rule:
4081 does not echo @emph{any} commands.
4082 (@xref{Echoing, ,Command Echoing}, for a full explanation of @samp{@@}.)
4084 @node Empty Commands, , Sequences, Commands
4085 @section Using Empty Commands
4086 @cindex empty commands
4087 @cindex commands, empty
4089 It is sometimes useful to define commands which do nothing. This is done
4090 simply by giving a command that consists of nothing but whitespace. For
4098 defines an empty command string for @file{target}. You could also use a
4099 line beginning with a tab character to define an empty command string,
4100 but this would be confusing because such a line looks empty.
4102 @findex .DEFAULT@r{, and empty commands}
4103 You may be wondering why you would want to define a command string that
4104 does nothing. The only reason this is useful is to prevent a target
4105 from getting implicit commands (from implicit rules or the
4106 @code{.DEFAULT} special target; @pxref{Implicit Rules} and
4107 @pxref{Last Resort, ,Defining Last-Resort Default Rules}).@refill
4109 @c !!! another reason is for canonical stamp files:
4113 create foo frm foo.in
4117 You may be inclined to define empty command strings for targets that are
4118 not actual files, but only exist so that their prerequisites can be
4119 remade. However, this is not the best way to do that, because the
4120 prerequisites may not be remade properly if the target file actually does exist.
4121 @xref{Phony Targets, ,Phony Targets}, for a better way to do this.
4123 @node Using Variables, Conditionals, Commands, Top
4124 @chapter How to Use Variables
4127 @cindex recursive variable expansion
4128 @cindex simple variable expansion
4130 A @dfn{variable} is a name defined in a makefile to represent a string
4131 of text, called the variable's @dfn{value}. These values are
4132 substituted by explicit request into targets, prerequisites, commands,
4133 and other parts of the makefile. (In some other versions of @code{make},
4134 variables are called @dfn{macros}.)
4137 Variables and functions in all parts of a makefile are expanded when
4138 read, except for the shell commands in rules, the right-hand sides of
4139 variable definitions using @samp{=}, and the bodies of variable
4140 definitions using the @code{define} directive.@refill
4142 Variables can represent lists of file names, options to pass to compilers,
4143 programs to run, directories to look in for source files, directories to
4144 write output in, or anything else you can imagine.
4146 A variable name may be any sequence of characters not containing @samp{:},
4147 @samp{#}, @samp{=}, or leading or trailing whitespace. However,
4148 variable names containing characters other than letters, numbers, and
4149 underscores should be avoided, as they may be given special meanings in the
4150 future, and with some shells they cannot be passed through the environment to a
4152 (@pxref{Variables/Recursion, ,Communicating Variables to a Sub-@code{make}}).
4154 Variable names are case-sensitive. The names @samp{foo}, @samp{FOO},
4155 and @samp{Foo} all refer to different variables.
4157 It is traditional to use upper case letters in variable names, but we
4158 recommend using lower case letters for variable names that serve internal
4159 purposes in the makefile, and reserving upper case for parameters that
4160 control implicit rules or for parameters that the user should override with
4161 command options (@pxref{Overriding, ,Overriding Variables}).
4163 A few variables have names that are a single punctuation character or
4164 just a few characters. These are the @dfn{automatic variables}, and
4165 they have particular specialized uses. @xref{Automatic, ,Automatic Variables}.
4168 * Reference:: How to use the value of a variable.
4169 * Flavors:: Variables come in two flavors.
4170 * Advanced:: Advanced features for referencing a variable.
4171 * Values:: All the ways variables get their values.
4172 * Setting:: How to set a variable in the makefile.
4173 * Appending:: How to append more text to the old value
4175 * Override Directive:: How to set a variable in the makefile even if
4176 the user has set it with a command argument.
4177 * Defining:: An alternate way to set a variable
4178 to a verbatim string.
4179 * Environment:: Variable values can come from the environment.
4180 * Target-specific:: Variable values can be defined on a per-target
4182 * Pattern-specific:: Target-specific variable values can be applied
4183 to a group of targets that match a pattern.
4186 @node Reference, Flavors, Using Variables, Using Variables
4187 @section Basics of Variable References
4188 @cindex variables, how to reference
4189 @cindex reference to variables
4190 @cindex @code{$}, in variable reference
4191 @cindex dollar sign (@code{$}), in variable reference
4193 To substitute a variable's value, write a dollar sign followed by the name
4194 of the variable in parentheses or braces: either @samp{$(foo)} or
4195 @samp{$@{foo@}} is a valid reference to the variable @code{foo}. This
4196 special significance of @samp{$} is why you must write @samp{$$} to have
4197 the effect of a single dollar sign in a file name or command.
4199 Variable references can be used in any context: targets, prerequisites,
4200 commands, most directives, and new variable values. Here is an
4201 example of a common case, where a variable holds the names of all the
4202 object files in a program:
4206 objects = program.o foo.o utils.o
4207 program : $(objects)
4208 cc -o program $(objects)
4214 Variable references work by strict textual substitution. Thus, the rule
4219 prog.o : prog.$(foo)
4220 $(foo)$(foo) -$(foo) prog.$(foo)
4225 could be used to compile a C program @file{prog.c}. Since spaces before
4226 the variable value are ignored in variable assignments, the value of
4227 @code{foo} is precisely @samp{c}. (Don't actually write your makefiles
4230 A dollar sign followed by a character other than a dollar sign,
4231 open-parenthesis or open-brace treats that single character as the
4232 variable name. Thus, you could reference the variable @code{x} with
4233 @samp{$x}. However, this practice is strongly discouraged, except in
4234 the case of the automatic variables (@pxref{Automatic, ,Automatic Variables}).
4236 @node Flavors, Advanced, Reference, Using Variables
4237 @section The Two Flavors of Variables
4238 @cindex flavors of variables
4239 @cindex recursive variable expansion
4240 @cindex variables, flavors
4241 @cindex recursively expanded variables
4242 @cindex variables, recursively expanded
4244 There are two ways that a variable in GNU @code{make} can have a value;
4245 we call them the two @dfn{flavors} of variables. The two flavors are
4246 distinguished in how they are defined and in what they do when expanded.
4249 The first flavor of variable is a @dfn{recursively expanded} variable.
4250 Variables of this sort are defined by lines using @samp{=}
4251 (@pxref{Setting, ,Setting Variables}) or by the @code{define} directive
4252 (@pxref{Defining, ,Defining Variables Verbatim}). The value you specify
4253 is installed verbatim; if it contains references to other variables,
4254 these references are expanded whenever this variable is substituted (in
4255 the course of expanding some other string). When this happens, it is
4256 called @dfn{recursive expansion}.@refill
4269 will echo @samp{Huh?}: @samp{$(foo)} expands to @samp{$(bar)} which
4270 expands to @samp{$(ugh)} which finally expands to @samp{Huh?}.@refill
4272 This flavor of variable is the only sort supported by other versions of
4273 @code{make}. It has its advantages and its disadvantages. An advantage
4274 (most would say) is that:
4277 CFLAGS = $(include_dirs) -O
4278 include_dirs = -Ifoo -Ibar
4282 will do what was intended: when @samp{CFLAGS} is expanded in a command,
4283 it will expand to @samp{-Ifoo -Ibar -O}. A major disadvantage is that you
4284 cannot append something on the end of a variable, as in
4287 CFLAGS = $(CFLAGS) -O
4291 because it will cause an infinite loop in the variable expansion.
4292 (Actually @code{make} detects the infinite loop and reports an error.)
4293 @cindex loops in variable expansion
4294 @cindex variables, loops in expansion
4296 Another disadvantage is that any functions
4297 (@pxref{Functions, ,Functions for Transforming Text})
4298 referenced in the definition will be executed every time the variable is
4299 expanded. This makes @code{make} run slower; worse, it causes the
4300 @code{wildcard} and @code{shell} functions to give unpredictable results
4301 because you cannot easily control when they are called, or even how many
4304 To avoid all the problems and inconveniences of recursively expanded
4305 variables, there is another flavor: simply expanded variables.
4307 @cindex simply expanded variables
4308 @cindex variables, simply expanded
4310 @dfn{Simply expanded variables} are defined by lines using @samp{:=}
4311 (@pxref{Setting, ,Setting Variables}).
4312 The value of a simply expanded variable is scanned
4313 once and for all, expanding any references to other variables and
4314 functions, when the variable is defined. The actual value of the simply
4315 expanded variable is the result of expanding the text that you write.
4316 It does not contain any references to other variables; it contains their
4317 values @emph{as of the time this variable was defined}. Therefore,
4333 When a simply expanded variable is referenced, its value is substituted
4336 Here is a somewhat more complicated example, illustrating the use of
4337 @samp{:=} in conjunction with the @code{shell} function.
4338 (@xref{Shell Function, , The @code{shell} Function}.) This example
4339 also shows use of the variable @code{MAKELEVEL}, which is changed
4340 when it is passed down from level to level.
4341 (@xref{Variables/Recursion, , Communicating Variables to a
4342 Sub-@code{make}}, for information about @code{MAKELEVEL}.)
4348 ifeq (0,$@{MAKELEVEL@})
4349 cur-dir := $(shell pwd)
4350 whoami := $(shell whoami)
4351 host-type := $(shell arch)
4352 MAKE := $@{MAKE@} host-type=$@{host-type@} whoami=$@{whoami@}
4358 An advantage of this use of @samp{:=} is that a typical
4359 `descend into a directory' command then looks like this:
4364 $@{MAKE@} cur-dir=$@{cur-dir@}/$@@ -C $@@ all
4368 Simply expanded variables generally make complicated makefile programming
4369 more predictable because they work like variables in most programming
4370 languages. They allow you to redefine a variable using its own value (or
4371 its value processed in some way by one of the expansion functions) and to
4372 use the expansion functions much more efficiently
4373 (@pxref{Functions, ,Functions for Transforming Text}).
4375 @cindex spaces, in variable values
4376 @cindex whitespace, in variable values
4377 @cindex variables, spaces in values
4378 You can also use them to introduce controlled leading whitespace into
4379 variable values. Leading whitespace characters are discarded from your
4380 input before substitution of variable references and function calls;
4381 this means you can include leading spaces in a variable value by
4382 protecting them with variable references, like this:
4386 space := $(nullstring) # end of the line
4390 Here the value of the variable @code{space} is precisely one space. The
4391 comment @w{@samp{# end of the line}} is included here just for clarity.
4392 Since trailing space characters are @emph{not} stripped from variable
4393 values, just a space at the end of the line would have the same effect
4394 (but be rather hard to read). If you put whitespace at the end of a
4395 variable value, it is a good idea to put a comment like that at the end
4396 of the line to make your intent clear. Conversely, if you do @emph{not}
4397 want any whitespace characters at the end of your variable value, you
4398 must remember not to put a random comment on the end of the line after
4399 some whitespace, such as this:
4402 dir := /foo/bar # directory to put the frobs in
4406 Here the value of the variable @code{dir} is @w{@samp{/foo/bar }}
4407 (with four trailing spaces), which was probably not the intention.
4408 (Imagine something like @w{@samp{$(dir)/file}} with this definition!)
4410 @cindex conditional variable assignment
4411 @cindex variables, conditional assignment
4413 There is another assignment operator for variables, @samp{?=}. This
4414 is called a conditional variable assignment operator, because it only
4415 has an effect if the variable is not yet defined. This statement:
4422 is exactly equivalent to this
4423 (@pxref{Origin Function, ,The @code{origin} Function}):
4426 ifeq ($(origin FOO), undefined)
4431 Note that a variable set to an empty value is still defined, so
4432 @samp{?=} will not set that variable.
4434 @node Advanced, Values, Flavors, Using Variables
4435 @section Advanced Features for Reference to Variables
4436 @cindex reference to variables
4438 This section describes some advanced features you can use to reference
4439 variables in more flexible ways.
4442 * Substitution Refs:: Referencing a variable with
4443 substitutions on the value.
4444 * Computed Names:: Computing the name of the variable to refer to.
4447 @node Substitution Refs, Computed Names, Advanced, Advanced
4448 @subsection Substitution References
4449 @cindex modified variable reference
4450 @cindex substitution variable reference
4451 @cindex variables, modified reference
4452 @cindex variables, substitution reference
4454 @cindex variables, substituting suffix in
4455 @cindex suffix, substituting in variables
4456 A @dfn{substitution reference} substitutes the value of a variable with
4457 alterations that you specify. It has the form
4458 @samp{$(@var{var}:@var{a}=@var{b})} (or
4459 @samp{$@{@var{var}:@var{a}=@var{b}@}}) and its meaning is to take the value
4460 of the variable @var{var}, replace every @var{a} at the end of a word with
4461 @var{b} in that value, and substitute the resulting string.
4463 When we say ``at the end of a word'', we mean that @var{a} must appear
4464 either followed by whitespace or at the end of the value in order to be
4465 replaced; other occurrences of @var{a} in the value are unaltered. For
4474 sets @samp{bar} to @samp{a.c b.c c.c}. @xref{Setting, ,Setting Variables}.
4476 A substitution reference is actually an abbreviation for use of the
4477 @code{patsubst} expansion function (@pxref{Text Functions, ,Functions for String Substitution and Analysis}). We provide
4478 substitution references as well as @code{patsubst} for compatibility with
4479 other implementations of @code{make}.
4482 Another type of substitution reference lets you use the full power of
4483 the @code{patsubst} function. It has the same form
4484 @samp{$(@var{var}:@var{a}=@var{b})} described above, except that now
4485 @var{a} must contain a single @samp{%} character. This case is
4486 equivalent to @samp{$(patsubst @var{a},@var{b},$(@var{var}))}.
4487 @xref{Text Functions, ,Functions for String Substitution and Analysis},
4488 for a description of the @code{patsubst} function.@refill
4492 @exdent For example:
4495 bar := $(foo:%.o=%.c)
4500 sets @samp{bar} to @samp{a.c b.c c.c}.
4502 @node Computed Names, , Substitution Refs, Advanced
4503 @subsection Computed Variable Names
4504 @cindex nested variable reference
4505 @cindex computed variable name
4506 @cindex variables, computed names
4507 @cindex variables, nested references
4508 @cindex variables, @samp{$} in name
4509 @cindex @code{$}, in variable name
4510 @cindex dollar sign (@code{$}), in variable name
4512 Computed variable names are a complicated concept needed only for
4513 sophisticated makefile programming. For most purposes you need not
4514 consider them, except to know that making a variable with a dollar sign
4515 in its name might have strange results. However, if you are the type
4516 that wants to understand everything, or you are actually interested in
4517 what they do, read on.
4519 Variables may be referenced inside the name of a variable. This is
4520 called a @dfn{computed variable name} or a @dfn{nested variable
4521 reference}. For example,
4530 defines @code{a} as @samp{z}: the @samp{$(x)} inside @samp{$($(x))} expands
4531 to @samp{y}, so @samp{$($(x))} expands to @samp{$(y)} which in turn expands
4532 to @samp{z}. Here the name of the variable to reference is not stated
4533 explicitly; it is computed by expansion of @samp{$(x)}. The reference
4534 @samp{$(x)} here is nested within the outer variable reference.
4536 The previous example shows two levels of nesting, but any number of levels
4537 is possible. For example, here are three levels:
4547 Here the innermost @samp{$(x)} expands to @samp{y}, so @samp{$($(x))}
4548 expands to @samp{$(y)} which in turn expands to @samp{z}; now we have
4549 @samp{$(z)}, which becomes @samp{u}.
4551 References to recursively-expanded variables within a variable name are
4552 reexpanded in the usual fashion. For example:
4562 defines @code{a} as @samp{Hello}: @samp{$($(x))} becomes @samp{$($(y))}
4563 which becomes @samp{$(z)} which becomes @samp{Hello}.
4565 Nested variable references can also contain modified references and
4566 function invocations (@pxref{Functions, ,Functions for Transforming Text}),
4567 just like any other reference.
4568 For example, using the @code{subst} function
4569 (@pxref{Text Functions, ,Functions for String Substitution and Analysis}):
4575 y = $(subst 1,2,$(x))
4582 eventually defines @code{a} as @samp{Hello}. It is doubtful that anyone
4583 would ever want to write a nested reference as convoluted as this one, but
4584 it works: @samp{$($($(z)))} expands to @samp{$($(y))} which becomes
4585 @samp{$($(subst 1,2,$(x)))}. This gets the value @samp{variable1} from
4586 @code{x} and changes it by substitution to @samp{variable2}, so that the
4587 entire string becomes @samp{$(variable2)}, a simple variable reference
4588 whose value is @samp{Hello}.@refill
4590 A computed variable name need not consist entirely of a single variable
4591 reference. It can contain several variable references, as well as some
4592 invariant text. For example,
4601 a_files := filea fileb
4602 1_files := file1 file2
4606 ifeq "$(use_a)" "yes"
4614 ifeq "$(use_dirs)" "yes"
4620 dirs := $($(a1)_$(df))
4625 will give @code{dirs} the same value as @code{a_dirs}, @code{1_dirs},
4626 @code{a_files} or @code{1_files} depending on the settings of @code{use_a}
4627 and @code{use_dirs}.@refill
4629 Computed variable names can also be used in substitution references:
4633 a_objects := a.o b.o c.o
4634 1_objects := 1.o 2.o 3.o
4636 sources := $($(a1)_objects:.o=.c)
4641 defines @code{sources} as either @samp{a.c b.c c.c} or @samp{1.c 2.c 3.c},
4642 depending on the value of @code{a1}.
4644 The only restriction on this sort of use of nested variable references
4645 is that they cannot specify part of the name of a function to be called.
4646 This is because the test for a recognized function name is done before
4647 the expansion of nested references. For example,
4663 foo := $($(func) $(bar))
4668 attempts to give @samp{foo} the value of the variable @samp{sort a d b g
4669 q c} or @samp{strip a d b g q c}, rather than giving @samp{a d b g q c}
4670 as the argument to either the @code{sort} or the @code{strip} function.
4671 This restriction could be removed in the future if that change is shown
4674 You can also use computed variable names in the left-hand side of a
4675 variable assignment, or in a @code{define} directive, as in:
4679 $(dir)_sources := $(wildcard $(dir)/*.c)
4681 lpr $($(dir)_sources)
4686 This example defines the variables @samp{dir}, @samp{foo_sources}, and
4689 Note that @dfn{nested variable references} are quite different from
4690 @dfn{recursively expanded variables}
4691 (@pxref{Flavors, ,The Two Flavors of Variables}), though both are
4692 used together in complex ways when doing makefile programming.@refill
4694 @node Values, Setting, Advanced, Using Variables
4695 @section How Variables Get Their Values
4696 @cindex variables, how they get their values
4697 @cindex value, how a variable gets it
4699 Variables can get values in several different ways:
4703 You can specify an overriding value when you run @code{make}.
4704 @xref{Overriding, ,Overriding Variables}.
4707 You can specify a value in the makefile, either
4708 with an assignment (@pxref{Setting, ,Setting Variables}) or with a
4709 verbatim definition (@pxref{Defining, ,Defining Variables Verbatim}).@refill
4712 Variables in the environment become @code{make} variables.
4713 @xref{Environment, ,Variables from the Environment}.
4716 Several @dfn{automatic} variables are given new values for each rule.
4717 Each of these has a single conventional use.
4718 @xref{Automatic, ,Automatic Variables}.
4721 Several variables have constant initial values.
4722 @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
4725 @node Setting, Appending, Values, Using Variables
4726 @section Setting Variables
4727 @cindex setting variables
4728 @cindex variables, setting
4733 To set a variable from the makefile, write a line starting with the
4734 variable name followed by @samp{=} or @samp{:=}. Whatever follows the
4735 @samp{=} or @samp{:=} on the line becomes the value. For example,
4738 objects = main.o foo.o bar.o utils.o
4742 defines a variable named @code{objects}. Whitespace around the variable
4743 name and immediately after the @samp{=} is ignored.
4745 Variables defined with @samp{=} are @dfn{recursively expanded} variables.
4746 Variables defined with @samp{:=} are @dfn{simply expanded} variables; these
4747 definitions can contain variable references which will be expanded before
4748 the definition is made. @xref{Flavors, ,The Two Flavors of Variables}.
4750 The variable name may contain function and variable references, which
4751 are expanded when the line is read to find the actual variable name to use.
4753 There is no limit on the length of the value of a variable except the
4754 amount of swapping space on the computer. When a variable definition is
4755 long, it is a good idea to break it into several lines by inserting
4756 backslash-newline at convenient places in the definition. This will not
4757 affect the functioning of @code{make}, but it will make the makefile easier
4760 Most variable names are considered to have the empty string as a value if
4761 you have never set them. Several variables have built-in initial values
4762 that are not empty, but you can set them in the usual ways
4763 (@pxref{Implicit Variables, ,Variables Used by Implicit Rules}).
4764 Several special variables are set
4765 automatically to a new value for each rule; these are called the
4766 @dfn{automatic} variables (@pxref{Automatic, ,Automatic Variables}).
4768 If you'd like a variable to be set to a value only if it's not already
4769 set, then you can use the shorthand operator @samp{?=} instead of
4770 @samp{=}. These two settings of the variable @samp{FOO} are identical
4771 (@pxref{Origin Function, ,The @code{origin} Function}):
4781 ifeq ($(origin FOO), undefined)
4786 @node Appending, Override Directive, Setting, Using Variables
4787 @section Appending More Text to Variables
4789 @cindex appending to variables
4790 @cindex variables, appending to
4792 Often it is useful to add more text to the value of a variable already defined.
4793 You do this with a line containing @samp{+=}, like this:
4796 objects += another.o
4800 This takes the value of the variable @code{objects}, and adds the text
4801 @samp{another.o} to it (preceded by a single space). Thus:
4804 objects = main.o foo.o bar.o utils.o
4805 objects += another.o
4809 sets @code{objects} to @samp{main.o foo.o bar.o utils.o another.o}.
4811 Using @samp{+=} is similar to:
4814 objects = main.o foo.o bar.o utils.o
4815 objects := $(objects) another.o
4819 but differs in ways that become important when you use more complex values.
4821 When the variable in question has not been defined before, @samp{+=}
4822 acts just like normal @samp{=}: it defines a recursively-expanded
4823 variable. However, when there @emph{is} a previous definition, exactly
4824 what @samp{+=} does depends on what flavor of variable you defined
4825 originally. @xref{Flavors, ,The Two Flavors of Variables}, for an
4826 explanation of the two flavors of variables.
4828 When you add to a variable's value with @samp{+=}, @code{make} acts
4829 essentially as if you had included the extra text in the initial
4830 definition of the variable. If you defined it first with @samp{:=},
4831 making it a simply-expanded variable, @samp{+=} adds to that
4832 simply-expanded definition, and expands the new text before appending it
4833 to the old value just as @samp{:=} does
4834 (@pxref{Setting, ,Setting Variables}, for a full explanation of @samp{:=}).
4843 is exactly equivalent to:
4848 variable := $(variable) more
4851 On the other hand, when you use @samp{+=} with a variable that you defined
4852 first to be recursively-expanded using plain @samp{=}, @code{make} does
4853 something a bit different. Recall that when you define a
4854 recursively-expanded variable, @code{make} does not expand the value you set
4855 for variable and function references immediately. Instead it stores the text
4856 verbatim, and saves these variable and function references to be expanded
4857 later, when you refer to the new variable (@pxref{Flavors, ,The Two Flavors
4858 of Variables}). When you use @samp{+=} on a recursively-expanded variable,
4859 it is this unexpanded text to which @code{make} appends the new text you
4870 is roughly equivalent to:
4875 variable = $(temp) more
4880 except that of course it never defines a variable called @code{temp}.
4881 The importance of this comes when the variable's old value contains
4882 variable references. Take this common example:
4885 CFLAGS = $(includes) -O
4887 CFLAGS += -pg # enable profiling
4891 The first line defines the @code{CFLAGS} variable with a reference to another
4892 variable, @code{includes}. (@code{CFLAGS} is used by the rules for C
4893 compilation; @pxref{Catalogue of Rules, ,Catalogue of Implicit Rules}.)
4894 Using @samp{=} for the definition makes @code{CFLAGS} a recursively-expanded
4895 variable, meaning @w{@samp{$(includes) -O}} is @emph{not} expanded when
4896 @code{make} processes the definition of @code{CFLAGS}. Thus, @code{includes}
4897 need not be defined yet for its value to take effect. It only has to be
4898 defined before any reference to @code{CFLAGS}. If we tried to append to the
4899 value of @code{CFLAGS} without using @samp{+=}, we might do it like this:
4902 CFLAGS := $(CFLAGS) -pg # enable profiling
4906 This is pretty close, but not quite what we want. Using @samp{:=}
4907 redefines @code{CFLAGS} as a simply-expanded variable; this means
4908 @code{make} expands the text @w{@samp{$(CFLAGS) -pg}} before setting the
4909 variable. If @code{includes} is not yet defined, we get @w{@samp{ -O
4910 -pg}}, and a later definition of @code{includes} will have no effect.
4911 Conversely, by using @samp{+=} we set @code{CFLAGS} to the
4912 @emph{unexpanded} value @w{@samp{$(includes) -O -pg}}. Thus we preserve
4913 the reference to @code{includes}, so if that variable gets defined at
4914 any later point, a reference like @samp{$(CFLAGS)} still uses its
4917 @node Override Directive, Defining, Appending, Using Variables
4918 @section The @code{override} Directive
4920 @cindex overriding with @code{override}
4921 @cindex variables, overriding
4923 If a variable has been set with a command argument
4924 (@pxref{Overriding, ,Overriding Variables}),
4925 then ordinary assignments in the makefile are ignored. If you want to set
4926 the variable in the makefile even though it was set with a command
4927 argument, you can use an @code{override} directive, which is a line that
4928 looks like this:@refill
4931 override @var{variable} = @var{value}
4938 override @var{variable} := @var{value}
4941 To append more text to a variable defined on the command line, use:
4944 override @var{variable} += @var{more text}
4948 @xref{Appending, ,Appending More Text to Variables}.
4950 The @code{override} directive was not invented for escalation in the war
4951 between makefiles and command arguments. It was invented so you can alter
4952 and add to values that the user specifies with command arguments.
4954 For example, suppose you always want the @samp{-g} switch when you run the
4955 C compiler, but you would like to allow the user to specify the other
4956 switches with a command argument just as usual. You could use this
4957 @code{override} directive:
4960 override CFLAGS += -g
4963 You can also use @code{override} directives with @code{define} directives.
4964 This is done as you might expect:
4974 See the next section for information about @code{define}.
4977 @xref{Defining, ,Defining Variables Verbatim}.
4980 @node Defining, Environment, Override Directive, Using Variables
4981 @section Defining Variables Verbatim
4984 @cindex verbatim variable definition
4985 @cindex defining variables verbatim
4986 @cindex variables, defining verbatim
4988 Another way to set the value of a variable is to use the @code{define}
4989 directive. This directive has an unusual syntax which allows newline
4990 characters to be included in the value, which is convenient for defining
4991 both canned sequences of commands
4992 (@pxref{Sequences, ,Defining Canned Command Sequences}), and also
4993 sections of makefile syntax to use with @code{eval} (@pxref{Eval Function}).
4995 The @code{define} directive is followed on the same line by the name of the
4996 variable and nothing more. The value to give the variable appears on the
4997 following lines. The end of the value is marked by a line containing just
4998 the word @code{endef}. Aside from this difference in syntax, @code{define}
4999 works just like @samp{=}: it creates a recursively-expanded variable
5000 (@pxref{Flavors, ,The Two Flavors of Variables}).
5001 The variable name may contain function and variable references, which
5002 are expanded when the directive is read to find the actual variable name
5005 You may nest @code{define} directives: @code{make} will keep track of
5006 nested directives and report an error if they are not all properly
5007 closed with @code{endef}. Note that lines beginning with tab
5008 characters are considered part of a command script, so any
5009 @code{define} or @code{endef} strings appearing on such a line will
5010 not be considered @code{make} operators.
5019 The value in an ordinary assignment cannot contain a newline; but the
5020 newlines that separate the lines of the value in a @code{define} become
5021 part of the variable's value (except for the final newline which precedes
5022 the @code{endef} and is not considered part of the value).@refill
5025 When used in a command script, the previous example is functionally
5029 two-lines = echo foo; echo $(bar)
5033 since two commands separated by semicolon behave much like two separate
5034 shell commands. However, note that using two separate lines means
5035 @code{make} will invoke the shell twice, running an independent subshell
5036 for each line. @xref{Execution, ,Command Execution}.
5038 If you want variable definitions made with @code{define} to take
5039 precedence over command-line variable definitions, you can use the
5040 @code{override} directive together with @code{define}:
5043 override define two-lines
5050 @xref{Override Directive, ,The @code{override} Directive}.
5052 @node Environment, Target-specific, Defining, Using Variables
5053 @section Variables from the Environment
5055 @cindex variables, environment
5057 Variables in @code{make} can come from the environment in which
5058 @code{make} is run. Every environment variable that @code{make} sees when
5059 it starts up is transformed into a @code{make} variable with the same name
5060 and value. But an explicit assignment in the makefile, or with a command
5061 argument, overrides the environment. (If the @samp{-e} flag is specified,
5062 then values from the environment override assignments in the makefile.
5063 @xref{Options Summary, ,Summary of Options}.
5064 But this is not recommended practice.)
5066 Thus, by setting the variable @code{CFLAGS} in your environment, you can
5067 cause all C compilations in most makefiles to use the compiler switches you
5068 prefer. This is safe for variables with standard or conventional meanings
5069 because you know that no makefile will use them for other things. (But
5070 this is not totally reliable; some makefiles set @code{CFLAGS} explicitly
5071 and therefore are not affected by the value in the environment.)
5073 When @code{make} is invoked recursively, variables defined in the
5074 outer invocation can be passed to inner invocations through the
5075 environment (@pxref{Recursion, ,Recursive Use of @code{make}}). By
5076 default, only variables that came from the environment or the command
5077 line are passed to recursive invocations. You can use the
5078 @code{export} directive to pass other variables.
5079 @xref{Variables/Recursion, , Communicating Variables to a
5080 Sub-@code{make}}, for full details.
5082 Other use of variables from the environment is not recommended. It is not
5083 wise for makefiles to depend for their functioning on environment variables
5084 set up outside their control, since this would cause different users to get
5085 different results from the same makefile. This is against the whole
5086 purpose of most makefiles.
5088 Such problems would be especially likely with the variable @code{SHELL},
5089 which is normally present in the environment to specify the user's choice
5090 of interactive shell. It would be very undesirable for this choice to
5091 affect @code{make}. So @code{make} ignores the environment value of
5092 @code{SHELL} (except on MS-DOS and MS-Windows, where @code{SHELL} is
5093 usually not set. @xref{Execution, ,Special handling of SHELL on
5096 @node Target-specific, Pattern-specific, Environment, Using Variables
5097 @section Target-specific Variable Values
5098 @cindex target-specific variables
5099 @cindex variables, target-specific
5101 Variable values in @code{make} are usually global; that is, they are the
5102 same regardless of where they are evaluated (unless they're reset, of
5103 course). One exception to that is automatic variables
5104 (@pxref{Automatic, ,Automatic Variables}).
5106 The other exception is @dfn{target-specific variable values}. This
5107 feature allows you to define different values for the same variable,
5108 based on the target that @code{make} is currently building. As with
5109 automatic variables, these values are only available within the context
5110 of a target's command script (and in other target-specific assignments).
5112 Set a target-specific variable value like this:
5115 @var{target} @dots{} : @var{variable-assignment}
5122 @var{target} @dots{} : override @var{variable-assignment}
5125 Multiple @var{target} values create a target-specific variable value for
5126 each member of the target list individually.
5128 The @var{variable-assignment} can be any valid form of assignment;
5129 recursive (@samp{=}), static (@samp{:=}), appending (@samp{+=}), or
5130 conditional (@samp{?=}). All variables that appear within the
5131 @var{variable-assignment} are evaluated within the context of the
5132 target: thus, any previously-defined target-specific variable values
5133 will be in effect. Note that this variable is actually distinct from
5134 any ``global'' value: the two variables do not have to have the same
5135 flavor (recursive vs. static).
5137 Target-specific variables have the same priority as any other makefile
5138 variable. Variables provided on the command-line (and in the
5139 environment if the @samp{-e} option is in force) will take precedence.
5140 Specifying the @code{override} directive will allow the target-specific
5141 variable value to be preferred.
5143 There is one more special feature of target-specific variables: when you
5144 define a target-specific variable, that variable value is also in effect
5145 for all prerequisites of this target (unless those prerequisites override
5146 it with their own target-specific variable value). So, for example, a
5147 statement like this:
5151 prog : prog.o foo.o bar.o
5155 will set @code{CFLAGS} to @samp{-g} in the command script for
5156 @file{prog}, but it will also set @code{CFLAGS} to @samp{-g} in the
5157 command scripts that create @file{prog.o}, @file{foo.o}, and
5158 @file{bar.o}, and any command scripts which create their prerequisites.
5160 @node Pattern-specific, , Target-specific, Using Variables
5161 @section Pattern-specific Variable Values
5162 @cindex pattern-specific variables
5163 @cindex variables, pattern-specific
5165 In addition to target-specific variable values (@pxref{Target-specific,
5166 ,Target-specific Variable Values}), GNU @code{make} supports
5167 pattern-specific variable values. In this form, a variable is defined
5168 for any target that matches the pattern specified. Variables defined in
5169 this way are searched after any target-specific variables defined
5170 explicitly for that target, and before target-specific variables defined
5171 for the parent target.
5173 Set a pattern-specific variable value like this:
5176 @var{pattern} @dots{} : @var{variable-assignment}
5183 @var{pattern} @dots{} : override @var{variable-assignment}
5187 where @var{pattern} is a %-pattern. As with target-specific variable
5188 values, multiple @var{pattern} values create a pattern-specific variable
5189 value for each pattern individually. The @var{variable-assignment} can
5190 be any valid form of assignment. Any command-line variable setting will
5191 take precedence, unless @code{override} is specified.
5200 will assign @code{CFLAGS} the value of @samp{-O} for all targets
5201 matching the pattern @code{%.o}.
5203 @node Conditionals, Functions, Using Variables, Top
5204 @chapter Conditional Parts of Makefiles
5206 @cindex conditionals
5207 A @dfn{conditional} causes part of a makefile to be obeyed or ignored
5208 depending on the values of variables. Conditionals can compare the
5209 value of one variable to another, or the value of a variable to
5210 a constant string. Conditionals control what @code{make} actually
5211 ``sees'' in the makefile, so they @emph{cannot} be used to control shell
5212 commands at the time of execution.@refill
5215 * Conditional Example:: Example of a conditional
5216 * Conditional Syntax:: The syntax of conditionals.
5217 * Testing Flags:: Conditionals that test flags.
5220 @node Conditional Example, Conditional Syntax, Conditionals, Conditionals
5221 @section Example of a Conditional
5223 The following example of a conditional tells @code{make} to use one set
5224 of libraries if the @code{CC} variable is @samp{gcc}, and a different
5225 set of libraries otherwise. It works by controlling which of two
5226 command lines will be used as the command for a rule. The result is
5227 that @samp{CC=gcc} as an argument to @code{make} changes not only which
5228 compiler is used but also which libraries are linked.
5231 libs_for_gcc = -lgnu
5236 $(CC) -o foo $(objects) $(libs_for_gcc)
5238 $(CC) -o foo $(objects) $(normal_libs)
5242 This conditional uses three directives: one @code{ifeq}, one @code{else}
5243 and one @code{endif}.
5245 The @code{ifeq} directive begins the conditional, and specifies the
5246 condition. It contains two arguments, separated by a comma and surrounded
5247 by parentheses. Variable substitution is performed on both arguments and
5248 then they are compared. The lines of the makefile following the
5249 @code{ifeq} are obeyed if the two arguments match; otherwise they are
5252 The @code{else} directive causes the following lines to be obeyed if the
5253 previous conditional failed. In the example above, this means that the
5254 second alternative linking command is used whenever the first alternative
5255 is not used. It is optional to have an @code{else} in a conditional.
5257 The @code{endif} directive ends the conditional. Every conditional must
5258 end with an @code{endif}. Unconditional makefile text follows.
5260 As this example illustrates, conditionals work at the textual level:
5261 the lines of the conditional are treated as part of the makefile, or
5262 ignored, according to the condition. This is why the larger syntactic
5263 units of the makefile, such as rules, may cross the beginning or the
5264 end of the conditional.
5266 When the variable @code{CC} has the value @samp{gcc}, the above example has
5271 $(CC) -o foo $(objects) $(libs_for_gcc)
5275 When the variable @code{CC} has any other value, the effect is this:
5279 $(CC) -o foo $(objects) $(normal_libs)
5282 Equivalent results can be obtained in another way by conditionalizing a
5283 variable assignment and then using the variable unconditionally:
5286 libs_for_gcc = -lgnu
5290 libs=$(libs_for_gcc)
5296 $(CC) -o foo $(objects) $(libs)
5299 @node Conditional Syntax, Testing Flags, Conditional Example, Conditionals
5300 @section Syntax of Conditionals
5308 The syntax of a simple conditional with no @code{else} is as follows:
5311 @var{conditional-directive}
5317 The @var{text-if-true} may be any lines of text, to be considered as part
5318 of the makefile if the condition is true. If the condition is false, no
5319 text is used instead.
5321 The syntax of a complex conditional is as follows:
5324 @var{conditional-directive}
5332 If the condition is true, @var{text-if-true} is used; otherwise,
5333 @var{text-if-false} is used instead. The @var{text-if-false} can be any
5334 number of lines of text.
5336 The syntax of the @var{conditional-directive} is the same whether the
5337 conditional is simple or complex. There are four different directives that
5338 test different conditions. Here is a table of them:
5341 @item ifeq (@var{arg1}, @var{arg2})
5342 @itemx ifeq '@var{arg1}' '@var{arg2}'
5343 @itemx ifeq "@var{arg1}" "@var{arg2}"
5344 @itemx ifeq "@var{arg1}" '@var{arg2}'
5345 @itemx ifeq '@var{arg1}' "@var{arg2}"
5346 Expand all variable references in @var{arg1} and @var{arg2} and
5347 compare them. If they are identical, the @var{text-if-true} is
5348 effective; otherwise, the @var{text-if-false}, if any, is effective.
5350 Often you want to test if a variable has a non-empty value. When the
5351 value results from complex expansions of variables and functions,
5352 expansions you would consider empty may actually contain whitespace
5353 characters and thus are not seen as empty. However, you can use the
5354 @code{strip} function (@pxref{Text Functions}) to avoid interpreting
5355 whitespace as a non-empty value. For example:
5359 ifeq ($(strip $(foo)),)
5366 will evaluate @var{text-if-empty} even if the expansion of
5367 @code{$(foo)} contains whitespace characters.
5369 @item ifneq (@var{arg1}, @var{arg2})
5370 @itemx ifneq '@var{arg1}' '@var{arg2}'
5371 @itemx ifneq "@var{arg1}" "@var{arg2}"
5372 @itemx ifneq "@var{arg1}" '@var{arg2}'
5373 @itemx ifneq '@var{arg1}' "@var{arg2}"
5374 Expand all variable references in @var{arg1} and @var{arg2} and
5375 compare them. If they are different, the @var{text-if-true} is
5376 effective; otherwise, the @var{text-if-false}, if any, is effective.
5378 @item ifdef @var{variable-name}
5379 If the variable @var{variable-name} has a non-empty value, the
5380 @var{text-if-true} is effective; otherwise, the @var{text-if-false},
5381 if any, is effective. Variables that have never been defined have an
5384 Note that @code{ifdef} only tests whether a variable has a value. It
5385 does not expand the variable to see if that value is nonempty.
5386 Consequently, tests using @code{ifdef} return true for all definitions
5387 except those like @code{foo =}. To test for an empty value, use
5388 @w{@code{ifeq ($(foo),)}}. For example,
5401 sets @samp{frobozz} to @samp{yes}, while:
5413 sets @samp{frobozz} to @samp{no}.
5415 @item ifndef @var{variable-name}
5416 If the variable @var{variable-name} has an empty value, the
5417 @var{text-if-true} is effective; otherwise, the @var{text-if-false},
5418 if any, is effective.
5421 Extra spaces are allowed and ignored at the beginning of the conditional
5422 directive line, but a tab is not allowed. (If the line begins with a tab,
5423 it will be considered a command for a rule.) Aside from this, extra spaces
5424 or tabs may be inserted with no effect anywhere except within the directive
5425 name or within an argument. A comment starting with @samp{#} may appear at
5426 the end of the line.
5428 The other two directives that play a part in a conditional are @code{else}
5429 and @code{endif}. Each of these directives is written as one word, with no
5430 arguments. Extra spaces are allowed and ignored at the beginning of the
5431 line, and spaces or tabs at the end. A comment starting with @samp{#} may
5432 appear at the end of the line.
5434 Conditionals affect which lines of the makefile @code{make} uses. If
5435 the condition is true, @code{make} reads the lines of the
5436 @var{text-if-true} as part of the makefile; if the condition is false,
5437 @code{make} ignores those lines completely. It follows that syntactic
5438 units of the makefile, such as rules, may safely be split across the
5439 beginning or the end of the conditional.@refill
5441 @code{make} evaluates conditionals when it reads a makefile.
5442 Consequently, you cannot use automatic variables in the tests of
5443 conditionals because they are not defined until commands are run
5444 (@pxref{Automatic, , Automatic Variables}).
5446 To prevent intolerable confusion, it is not permitted to start a
5447 conditional in one makefile and end it in another. However, you may
5448 write an @code{include} directive within a conditional, provided you do
5449 not attempt to terminate the conditional inside the included file.
5451 @node Testing Flags, , Conditional Syntax, Conditionals
5452 @section Conditionals that Test Flags
5454 You can write a conditional that tests @code{make} command flags such as
5455 @samp{-t} by using the variable @code{MAKEFLAGS} together with the
5456 @code{findstring} function
5457 (@pxref{Text Functions, , Functions for String Substitution and Analysis}).
5458 This is useful when @code{touch} is not enough to make a file appear up
5461 The @code{findstring} function determines whether one string appears as a
5462 substring of another. If you want to test for the @samp{-t} flag,
5463 use @samp{t} as the first string and the value of @code{MAKEFLAGS} as
5466 For example, here is how to arrange to use @samp{ranlib -t} to finish
5467 marking an archive file up to date:
5471 ifneq (,$(findstring t,$(MAKEFLAGS)))
5473 +ranlib -t archive.a
5480 The @samp{+} prefix marks those command lines as ``recursive'' so
5481 that they will be executed despite use of the @samp{-t} flag.
5482 @xref{Recursion, ,Recursive Use of @code{make}}.
5484 @node Functions, Running, Conditionals, Top
5485 @chapter Functions for Transforming Text
5488 @dfn{Functions} allow you to do text processing in the makefile to compute
5489 the files to operate on or the commands to use. You use a function in a
5490 @dfn{function call}, where you give the name of the function and some text
5491 (the @dfn{arguments}) for the function to operate on. The result of the
5492 function's processing is substituted into the makefile at the point of the
5493 call, just as a variable might be substituted.
5496 * Syntax of Functions:: How to write a function call.
5497 * Text Functions:: General-purpose text manipulation functions.
5498 * File Name Functions:: Functions for manipulating file names.
5499 * Foreach Function:: Repeat some text with controlled variation.
5500 * If Function:: Conditionally expand a value.
5501 * Call Function:: Expand a user-defined function.
5502 * Value Function:: Return the un-expanded value of a variable.
5503 * Eval Function:: Evaluate the arguments as makefile syntax.
5504 * Origin Function:: Find where a variable got its value.
5505 * Shell Function:: Substitute the output of a shell command.
5506 * Make Control Functions:: Functions that control how make runs.
5509 @node Syntax of Functions, Text Functions, Functions, Functions
5510 @section Function Call Syntax
5511 @cindex @code{$}, in function call
5512 @cindex dollar sign (@code{$}), in function call
5513 @cindex arguments of functions
5514 @cindex functions, syntax of
5516 A function call resembles a variable reference. It looks like this:
5519 $(@var{function} @var{arguments})
5526 $@{@var{function} @var{arguments}@}
5529 Here @var{function} is a function name; one of a short list of names
5530 that are part of @code{make}. You can also essentially create your own
5531 functions by using the @code{call} builtin function.
5533 The @var{arguments} are the arguments of the function. They are
5534 separated from the function name by one or more spaces or tabs, and if
5535 there is more than one argument, then they are separated by commas.
5536 Such whitespace and commas are not part of an argument's value. The
5537 delimiters which you use to surround the function call, whether
5538 parentheses or braces, can appear in an argument only in matching pairs;
5539 the other kind of delimiters may appear singly. If the arguments
5540 themselves contain other function calls or variable references, it is
5541 wisest to use the same kind of delimiters for all the references; write
5542 @w{@samp{$(subst a,b,$(x))}}, not @w{@samp{$(subst a,b,$@{x@})}}. This
5543 is because it is clearer, and because only one type of delimiter is
5544 matched to find the end of the reference.
5546 The text written for each argument is processed by substitution of
5547 variables and function calls to produce the argument value, which
5548 is the text on which the function acts. The substitution is done in the
5549 order in which the arguments appear.
5551 Commas and unmatched parentheses or braces cannot appear in the text of an
5552 argument as written; leading spaces cannot appear in the text of the first
5553 argument as written. These characters can be put into the argument value
5554 by variable substitution. First define variables @code{comma} and
5555 @code{space} whose values are isolated comma and space characters, then
5556 substitute these variables where such characters are wanted, like this:
5562 space:= $(empty) $(empty)
5564 bar:= $(subst $(space),$(comma),$(foo))
5565 # @r{bar is now `a,b,c'.}
5570 Here the @code{subst} function replaces each space with a comma, through
5571 the value of @code{foo}, and substitutes the result.
5573 @node Text Functions, File Name Functions, Syntax of Functions, Functions
5574 @section Functions for String Substitution and Analysis
5575 @cindex functions, for text
5577 Here are some functions that operate on strings:
5580 @item $(subst @var{from},@var{to},@var{text})
5582 Performs a textual replacement on the text @var{text}: each occurrence
5583 of @var{from} is replaced by @var{to}. The result is substituted for
5584 the function call. For example,
5587 $(subst ee,EE,feet on the street)
5590 substitutes the string @samp{fEEt on the strEEt}.
5592 @item $(patsubst @var{pattern},@var{replacement},@var{text})
5594 Finds whitespace-separated words in @var{text} that match
5595 @var{pattern} and replaces them with @var{replacement}. Here
5596 @var{pattern} may contain a @samp{%} which acts as a wildcard,
5597 matching any number of any characters within a word. If
5598 @var{replacement} also contains a @samp{%}, the @samp{%} is replaced
5599 by the text that matched the @samp{%} in @var{pattern}. Only the first
5600 @samp{%} in the @var{pattern} and @var{replacement} is treated this
5601 way; any subsequent @samp{%} is unchanged.@refill
5603 @cindex @code{%}, quoting in @code{patsubst}
5604 @cindex @code{%}, quoting with @code{\} (backslash)
5605 @cindex @code{\} (backslash), to quote @code{%}
5606 @cindex backslash (@code{\}), to quote @code{%}
5607 @cindex quoting @code{%}, in @code{patsubst}
5608 @samp{%} characters in @code{patsubst} function invocations can be
5609 quoted with preceding backslashes (@samp{\}). Backslashes that would
5610 otherwise quote @samp{%} characters can be quoted with more backslashes.
5611 Backslashes that quote @samp{%} characters or other backslashes are
5612 removed from the pattern before it is compared file names or has a stem
5613 substituted into it. Backslashes that are not in danger of quoting
5614 @samp{%} characters go unmolested. For example, the pattern
5615 @file{the\%weird\\%pattern\\} has @samp{the%weird\} preceding the
5616 operative @samp{%} character, and @samp{pattern\\} following it. The
5617 final two backslashes are left alone because they cannot affect any
5618 @samp{%} character.@refill
5620 Whitespace between words is folded into single space characters;
5621 leading and trailing whitespace is discarded.
5626 $(patsubst %.c,%.o,x.c.c bar.c)
5630 produces the value @samp{x.c.o bar.o}.
5632 Substitution references (@pxref{Substitution Refs, ,Substitution
5633 References}) are a simpler way to get the effect of the @code{patsubst}
5637 $(@var{var}:@var{pattern}=@var{replacement})
5644 $(patsubst @var{pattern},@var{replacement},$(@var{var}))
5647 The second shorthand simplifies one of the most common uses of
5648 @code{patsubst}: replacing the suffix at the end of file names.
5651 $(@var{var}:@var{suffix}=@var{replacement})
5658 $(patsubst %@var{suffix},%@var{replacement},$(@var{var}))
5662 For example, you might have a list of object files:
5665 objects = foo.o bar.o baz.o
5669 To get the list of corresponding source files, you could simply write:
5676 instead of using the general form:
5679 $(patsubst %.o,%.c,$(objects))
5682 @item $(strip @var{string})
5683 @cindex stripping whitespace
5684 @cindex whitespace, stripping
5685 @cindex spaces, stripping
5687 Removes leading and trailing whitespace from @var{string} and replaces
5688 each internal sequence of one or more whitespace characters with a
5689 single space. Thus, @samp{$(strip a b c )} results in @w{@samp{a b c}}.
5691 The function @code{strip} can be very useful when used in conjunction
5692 with conditionals. When comparing something with the empty string
5693 @samp{} using @code{ifeq} or @code{ifneq}, you usually want a string of
5694 just whitespace to match the empty string (@pxref{Conditionals}).
5696 Thus, the following may fail to have the desired results:
5700 ifneq "$(needs_made)" ""
5703 all:;@@echo 'Nothing to make!'
5708 Replacing the variable reference @w{@samp{$(needs_made)}} with the
5709 function call @w{@samp{$(strip $(needs_made))}} in the @code{ifneq}
5710 directive would make it more robust.@refill
5712 @item $(findstring @var{find},@var{in})
5714 @cindex searching for strings
5715 @cindex finding strings
5716 @cindex strings, searching for
5717 Searches @var{in} for an occurrence of @var{find}. If it occurs, the
5718 value is @var{find}; otherwise, the value is empty. You can use this
5719 function in a conditional to test for the presence of a specific
5720 substring in a given string. Thus, the two examples,
5723 $(findstring a,a b c)
5728 produce the values @samp{a} and @samp{} (the empty string),
5729 respectively. @xref{Testing Flags}, for a practical application of
5730 @code{findstring}.@refill
5734 @cindex filtering words
5735 @cindex words, filtering
5736 @item $(filter @var{pattern}@dots{},@var{text})
5737 Returns all whitespace-separated words in @var{text} that @emph{do} match
5738 any of the @var{pattern} words, removing any words that @emph{do not}
5739 match. The patterns are written using @samp{%}, just like the patterns
5740 used in the @code{patsubst} function above.@refill
5742 The @code{filter} function can be used to separate out different types
5743 of strings (such as file names) in a variable. For example:
5746 sources := foo.c bar.c baz.s ugh.h
5748 cc $(filter %.c %.s,$(sources)) -o foo
5752 says that @file{foo} depends of @file{foo.c}, @file{bar.c},
5753 @file{baz.s} and @file{ugh.h} but only @file{foo.c}, @file{bar.c} and
5754 @file{baz.s} should be specified in the command to the
5757 @item $(filter-out @var{pattern}@dots{},@var{text})
5759 @cindex filtering out words
5760 @cindex words, filtering out
5761 Returns all whitespace-separated words in @var{text} that @emph{do not}
5762 match any of the @var{pattern} words, removing the words that @emph{do}
5763 match one or more. This is the exact opposite of the @code{filter}
5770 objects=main1.o foo.o main2.o bar.o
5771 mains=main1.o main2.o
5776 the following generates a list which contains all the object files not
5780 $(filter-out $(mains),$(objects))
5785 @cindex sorting words
5786 @item $(sort @var{list})
5787 Sorts the words of @var{list} in lexical order, removing duplicate
5788 words. The output is a list of words separated by single spaces.
5792 $(sort foo bar lose)
5796 returns the value @samp{bar foo lose}.
5798 @cindex removing duplicate words
5799 @cindex duplicate words, removing
5800 @cindex words, removing duplicates
5801 Incidentally, since @code{sort} removes duplicate words, you can use
5802 it for this purpose even if you don't care about the sort order.
5804 @item $(word @var{n},@var{text})
5806 @cindex word, selecting a
5807 @cindex selecting a word
5808 Returns the @var{n}th word of @var{text}. The legitimate values of
5809 @var{n} start from 1. If @var{n} is bigger than the number of words
5810 in @var{text}, the value is empty. For example,
5813 $(word 2, foo bar baz)
5819 @item $(wordlist @var{s},@var{e},@var{text})
5821 @cindex words, selecting lists of
5822 @cindex selecting word lists
5823 Returns the list of words in @var{text} starting with word @var{s} and
5824 ending with word @var{e} (inclusive). The legitimate values of @var{s}
5825 and @var{e} start from 1. If @var{s} is bigger than the number of words
5826 in @var{text}, the value is empty. If @var{e} is bigger than the number
5827 of words in @var{text}, words up to the end of @var{text} are returned.
5828 If @var{s} is greater than @var{e}, nothing is returned. For example,
5831 $(wordlist 2, 3, foo bar baz)
5835 returns @samp{bar baz}.
5837 @c Following item phrased to prevent overfull hbox. --RJC 17 Jul 92
5838 @item $(words @var{text})
5840 @cindex words, finding number
5841 Returns the number of words in @var{text}.
5842 Thus, the last word of @var{text} is
5843 @w{@code{$(word $(words @var{text}),@var{text})}}.@refill
5845 @item $(firstword @var{names}@dots{})
5847 @cindex words, extracting first
5848 The argument @var{names} is regarded as a series of names, separated
5849 by whitespace. The value is the first name in the series. The rest
5850 of the names are ignored.
5855 $(firstword foo bar)
5859 produces the result @samp{foo}. Although @code{$(firstword
5860 @var{text})} is the same as @code{$(word 1,@var{text})}, the
5861 @code{firstword} function is retained for its simplicity.@refill
5864 Here is a realistic example of the use of @code{subst} and
5865 @code{patsubst}. Suppose that a makefile uses the @code{VPATH} variable
5866 to specify a list of directories that @code{make} should search for
5868 (@pxref{General Search, , @code{VPATH} Search Path for All Prerequisites}).
5869 This example shows how to
5870 tell the C compiler to search for header files in the same list of
5873 The value of @code{VPATH} is a list of directories separated by colons,
5874 such as @samp{src:../headers}. First, the @code{subst} function is used to
5875 change the colons to spaces:
5878 $(subst :, ,$(VPATH))
5882 This produces @samp{src ../headers}. Then @code{patsubst} is used to turn
5883 each directory name into a @samp{-I} flag. These can be added to the
5884 value of the variable @code{CFLAGS}, which is passed automatically to the C
5885 compiler, like this:
5888 override CFLAGS += $(patsubst %,-I%,$(subst :, ,$(VPATH)))
5892 The effect is to append the text @samp{-Isrc -I../headers} to the
5893 previously given value of @code{CFLAGS}. The @code{override} directive is
5894 used so that the new value is assigned even if the previous value of
5895 @code{CFLAGS} was specified with a command argument (@pxref{Override
5896 Directive, , The @code{override} Directive}).
5898 @node File Name Functions, Foreach Function, Text Functions, Functions
5899 @section Functions for File Names
5900 @cindex functions, for file names
5901 @cindex file name functions
5903 Several of the built-in expansion functions relate specifically to
5904 taking apart file names or lists of file names.
5906 Each of the following functions performs a specific transformation on a
5907 file name. The argument of the function is regarded as a series of file
5908 names, separated by whitespace. (Leading and trailing whitespace is
5909 ignored.) Each file name in the series is transformed in the same way and
5910 the results are concatenated with single spaces between them.
5913 @item $(dir @var{names}@dots{})
5915 @cindex directory part
5916 @cindex file name, directory part
5917 Extracts the directory-part of each file name in @var{names}. The
5918 directory-part of the file name is everything up through (and
5919 including) the last slash in it. If the file name contains no slash,
5920 the directory part is the string @samp{./}. For example,
5923 $(dir src/foo.c hacks)
5927 produces the result @samp{src/ ./}.
5929 @item $(notdir @var{names}@dots{})
5931 @cindex file name, nondirectory part
5932 @cindex nondirectory part
5933 Extracts all but the directory-part of each file name in @var{names}.
5934 If the file name contains no slash, it is left unchanged. Otherwise,
5935 everything through the last slash is removed from it.
5937 A file name that ends with a slash becomes an empty string. This is
5938 unfortunate, because it means that the result does not always have the
5939 same number of whitespace-separated file names as the argument had;
5940 but we do not see any other valid alternative.
5945 $(notdir src/foo.c hacks)
5949 produces the result @samp{foo.c hacks}.
5951 @item $(suffix @var{names}@dots{})
5953 @cindex suffix, function to find
5954 @cindex file name suffix
5955 Extracts the suffix of each file name in @var{names}. If the file name
5956 contains a period, the suffix is everything starting with the last
5957 period. Otherwise, the suffix is the empty string. This frequently
5958 means that the result will be empty when @var{names} is not, and if
5959 @var{names} contains multiple file names, the result may contain fewer
5965 $(suffix src/foo.c src-1.0/bar.c hacks)
5969 produces the result @samp{.c .c}.
5971 @item $(basename @var{names}@dots{})
5974 @cindex file name, basename of
5975 Extracts all but the suffix of each file name in @var{names}. If the
5976 file name contains a period, the basename is everything starting up to
5977 (and not including) the last period. Periods in the directory part are
5978 ignored. If there is no period, the basename is the entire file name.
5982 $(basename src/foo.c src-1.0/bar hacks)
5986 produces the result @samp{src/foo src-1.0/bar hacks}.
5988 @c plural convention with dots (be consistent)
5989 @item $(addsuffix @var{suffix},@var{names}@dots{})
5991 @cindex suffix, adding
5992 @cindex file name suffix, adding
5993 The argument @var{names} is regarded as a series of names, separated
5994 by whitespace; @var{suffix} is used as a unit. The value of
5995 @var{suffix} is appended to the end of each individual name and the
5996 resulting larger names are concatenated with single spaces between
6000 $(addsuffix .c,foo bar)
6004 produces the result @samp{foo.c bar.c}.
6006 @item $(addprefix @var{prefix},@var{names}@dots{})
6008 @cindex prefix, adding
6009 @cindex file name prefix, adding
6010 The argument @var{names} is regarded as a series of names, separated
6011 by whitespace; @var{prefix} is used as a unit. The value of
6012 @var{prefix} is prepended to the front of each individual name and the
6013 resulting larger names are concatenated with single spaces between
6017 $(addprefix src/,foo bar)
6021 produces the result @samp{src/foo src/bar}.
6023 @item $(join @var{list1},@var{list2})
6025 @cindex joining lists of words
6026 @cindex words, joining lists
6027 Concatenates the two arguments word by word: the two first words (one
6028 from each argument) concatenated form the first word of the result, the
6029 two second words form the second word of the result, and so on. So the
6030 @var{n}th word of the result comes from the @var{n}th word of each
6031 argument. If one argument has more words that the other, the extra
6032 words are copied unchanged into the result.
6034 For example, @samp{$(join a b,.c .o)} produces @samp{a.c b.o}.
6036 Whitespace between the words in the lists is not preserved; it is
6037 replaced with a single space.
6039 This function can merge the results of the @code{dir} and
6040 @code{notdir} functions, to produce the original list of files which
6041 was given to those two functions.@refill
6043 @item $(wildcard @var{pattern})
6045 @cindex wildcard, function
6046 The argument @var{pattern} is a file name pattern, typically containing
6047 wildcard characters (as in shell file name patterns). The result of
6048 @code{wildcard} is a space-separated list of the names of existing files
6049 that match the pattern.
6050 @xref{Wildcards, ,Using Wildcard Characters in File Names}.
6053 @node Foreach Function, If Function, File Name Functions, Functions
6054 @section The @code{foreach} Function
6056 @cindex words, iterating over
6058 The @code{foreach} function is very different from other functions. It
6059 causes one piece of text to be used repeatedly, each time with a different
6060 substitution performed on it. It resembles the @code{for} command in the
6061 shell @code{sh} and the @code{foreach} command in the C-shell @code{csh}.
6063 The syntax of the @code{foreach} function is:
6066 $(foreach @var{var},@var{list},@var{text})
6070 The first two arguments, @var{var} and @var{list}, are expanded before
6071 anything else is done; note that the last argument, @var{text}, is
6072 @strong{not} expanded at the same time. Then for each word of the expanded
6073 value of @var{list}, the variable named by the expanded value of @var{var}
6074 is set to that word, and @var{text} is expanded. Presumably @var{text}
6075 contains references to that variable, so its expansion will be different
6078 The result is that @var{text} is expanded as many times as there are
6079 whitespace-separated words in @var{list}. The multiple expansions of
6080 @var{text} are concatenated, with spaces between them, to make the result
6083 This simple example sets the variable @samp{files} to the list of all files
6084 in the directories in the list @samp{dirs}:
6088 files := $(foreach dir,$(dirs),$(wildcard $(dir)/*))
6091 Here @var{text} is @samp{$(wildcard $(dir)/*)}. The first repetition
6092 finds the value @samp{a} for @code{dir}, so it produces the same result
6093 as @samp{$(wildcard a/*)}; the second repetition produces the result
6094 of @samp{$(wildcard b/*)}; and the third, that of @samp{$(wildcard c/*)}.
6096 This example has the same result (except for setting @samp{dirs}) as
6097 the following example:
6100 files := $(wildcard a/* b/* c/* d/*)
6103 When @var{text} is complicated, you can improve readability by giving it
6104 a name, with an additional variable:
6107 find_files = $(wildcard $(dir)/*)
6109 files := $(foreach dir,$(dirs),$(find_files))
6113 Here we use the variable @code{find_files} this way. We use plain @samp{=}
6114 to define a recursively-expanding variable, so that its value contains an
6115 actual function call to be reexpanded under the control of @code{foreach};
6116 a simply-expanded variable would not do, since @code{wildcard} would be
6117 called only once at the time of defining @code{find_files}.
6119 The @code{foreach} function has no permanent effect on the variable
6120 @var{var}; its value and flavor after the @code{foreach} function call are
6121 the same as they were beforehand. The other values which are taken from
6122 @var{list} are in effect only temporarily, during the execution of
6123 @code{foreach}. The variable @var{var} is a simply-expanded variable
6124 during the execution of @code{foreach}. If @var{var} was undefined
6125 before the @code{foreach} function call, it is undefined after the call.
6126 @xref{Flavors, ,The Two Flavors of Variables}.@refill
6128 You must take care when using complex variable expressions that result in
6129 variable names because many strange things are valid variable names, but
6130 are probably not what you intended. For example,
6133 files := $(foreach Esta escrito en espanol!,b c ch,$(find_files))
6137 might be useful if the value of @code{find_files} references the variable
6138 whose name is @samp{Esta escrito en espanol!} (es un nombre bastante largo,
6139 no?), but it is more likely to be a mistake.
6141 @node If Function, Call Function, Foreach Function, Functions
6142 @section The @code{if} Function
6144 @cindex conditional expansion
6146 The @code{if} function provides support for conditional expansion in a
6147 functional context (as opposed to the GNU @code{make} makefile
6148 conditionals such as @code{ifeq} (@pxref{Conditional Syntax, ,Syntax of
6151 An @code{if} function call can contain either two or three arguments:
6154 $(if @var{condition},@var{then-part}[,@var{else-part}])
6157 The first argument, @var{condition}, first has all preceding and
6158 trailing whitespace stripped, then is expanded. If it expands to any
6159 non-empty string, then the condition is considered to be true. If it
6160 expands to an empty string, the condition is considered to be false.
6162 If the condition is true then the second argument, @var{then-part}, is
6163 evaluated and this is used as the result of the evaluation of the entire
6166 If the condition is false then the third argument, @var{else-part}, is
6167 evaluated and this is the result of the @code{if} function. If there is
6168 no third argument, the @code{if} function evaluates to nothing (the
6171 Note that only one of the @var{then-part} or the @var{else-part} will be
6172 evaluated, never both. Thus, either can contain side-effects (such as
6173 @code{shell} function calls, etc.)
6175 @node Call Function, Value Function, If Function, Functions
6176 @section The @code{call} Function
6178 @cindex functions, user defined
6179 @cindex user defined functions
6181 The @code{call} function is unique in that it can be used to create new
6182 parameterized functions. You can write a complex expression as the
6183 value of a variable, then use @code{call} to expand it with different
6186 The syntax of the @code{call} function is:
6189 $(call @var{variable},@var{param},@var{param},@dots{})
6192 When @code{make} expands this function, it assigns each @var{param} to
6193 temporary variables @code{$(1)}, @code{$(2)}, etc. The variable
6194 @code{$(0)} will contain @var{variable}. There is no maximum number of
6195 parameter arguments. There is no minimum, either, but it doesn't make
6196 sense to use @code{call} with no parameters.
6198 Then @var{variable} is expanded as a @code{make} variable in the context
6199 of these temporary assignments. Thus, any reference to @code{$(1)} in
6200 the value of @var{variable} will resolve to the first @var{param} in the
6201 invocation of @code{call}.
6203 Note that @var{variable} is the @emph{name} of a variable, not a
6204 @emph{reference} to that variable. Therefore you would not normally use
6205 a @samp{$} or parentheses when writing it. (You can, however, use a
6206 variable reference in the name if you want the name not to be a
6209 If @var{variable} is the name of a builtin function, the builtin function
6210 is always invoked (even if a @code{make} variable by that name also
6213 The @code{call} function expands the @var{param} arguments before
6214 assigning them to temporary variables. This means that @var{variable}
6215 values containing references to builtin functions that have special
6216 expansion rules, like @code{foreach} or @code{if}, may not work as you
6219 Some examples may make this clearer.
6221 This macro simply reverses its arguments:
6226 foo = $(call reverse,a,b)
6230 Here @var{foo} will contain @samp{b a}.
6232 This one is slightly more interesting: it defines a macro to search for
6233 the first instance of a program in @code{PATH}:
6236 pathsearch = $(firstword $(wildcard $(addsuffix /$(1),$(subst :, ,$(PATH)))))
6238 LS := $(call pathsearch,ls)
6242 Now the variable LS contains @code{/bin/ls} or similar.
6244 The @code{call} function can be nested. Each recursive invocation gets
6245 its own local values for @code{$(1)}, etc. that mask the values of
6246 higher-level @code{call}. For example, here is an implementation of a
6250 map = $(foreach a,$(2),$(call $(1),$(a)))
6253 Now you can @var{map} a function that normally takes only one argument,
6254 such as @code{origin}, to multiple values in one step:
6257 o = $(call map,origin,o map MAKE)
6260 and end up with @var{o} containing something like @samp{file file default}.
6262 A final caution: be careful when adding whitespace to the arguments to
6263 @code{call}. As with other functions, any whitespace contained in the
6264 second and subsequent arguments is kept; this can cause strange
6265 effects. It's generally safest to remove all extraneous whitespace when
6266 providing parameters to @code{call}.
6268 @node Value Function, Eval Function, Call Function, Functions
6269 @comment node-name, next, previous, up
6270 @section The @code{value} Function
6272 @cindex variables, unexpanded value
6274 The @code{value} function provides a way for you to use the value of a
6275 variable @emph{without} having it expanded. Please note that this
6276 does not undo expansions which have already occurred; for example if
6277 you create a simply expanded variable its value is expanded during the
6278 definition; in that case the @code{value} function will return the
6279 same result as using the variable directly.
6281 The syntax of the @code{value} function is:
6284 $(value @var{variable})
6287 Note that @var{variable} is the @emph{name} of a variable; not a
6288 @emph{reference} to that variable. Therefore you would not normally
6289 use a @samp{$} or parentheses when writing it. (You can, however, use
6290 a variable reference in the name if you want the name not to be a
6293 The result of this function is a string containing the value of
6294 @var{variable}, without any expansion occurring. For example, in this
6308 The first output line would be @code{ATH}, since the ``$P'' would be
6309 expanded as a @code{make} variable, while the second output line would
6310 be the current value of your @code{$PATH} environment variable, since
6311 the @code{value} function avoided the expansion.
6313 The @code{value} function is most often used in conjunction with the
6314 @code{eval} function (@pxref{Eval Function}).
6316 @node Eval Function, Origin Function, Value Function, Functions
6317 @comment node-name, next, previous, up
6318 @section The @code{eval} Function
6320 @cindex evaluating makefile syntax
6321 @cindex makefile syntax, evaluating
6323 The @code{eval} function is very special: it allows you to define new
6324 makefile constructs that are not constant; which are the result of
6325 evaluating other variables and functions. The argument to the
6326 @code{eval} function is expanded, then the results of that expansion
6327 are parsed as makefile syntax. The expanded results can define new
6328 @code{make} variables, targets, implicit or explicit rules, etc.
6330 The result of the @code{eval} function is always the empty string;
6331 thus, it can be placed virtually anywhere in a makefile without
6332 causing syntax errors.
6334 It's important to realize that the @code{eval} argument is expanded
6335 @emph{twice}; first by the @code{eval} function, then the results of
6336 that expansion are expanded again when they are parsed as makefile
6337 syntax. This means you may need to provide extra levels of escaping
6338 for ``$'' characters when using @code{eval}. The @code{value}
6339 function (@pxref{Value Function}) can sometimes be useful in these
6340 situations, to circumvent unwanted expansions.
6342 Here is an example of how @code{eval} can be used; this example
6343 combines a number of concepts and other functions. Although it might
6344 seem overly complex to use @code{eval} in this example, rather than
6345 just writing out the rules, consider two things: first, the template
6346 definition (in @code{PROGRAM_template}) could need to be much more
6347 complex than it is here; and second, you might put the complex,
6348 ``generic'' part of this example into another makefile, then include
6349 it in all the individual makefiles. Now your individual makefiles are
6350 quite straightforward.
6354 PROGRAMS = server client
6356 server_OBJS = server.o server_priv.o server_access.o
6357 server_LIBS = priv protocol
6359 client_OBJS = client.o client_api.o client_mem.o
6360 client_LIBS = protocol
6362 # Everything after this is generic
6367 define PROGRAM_template
6368 $(1): $$($(1)_OBJ) $$($(1)_LIBS:%=-l%)
6369 ALL_OBJS += $$($(1)_OBJS)
6372 $(foreach prog,$(PROGRAMS),$(eval $(call PROGRAM_template,$(prog))))
6375 $(LINK.o) $^ $(LDLIBS) -o $@@
6378 rm -f $(ALL_OBJS) $(PROGRAMS)
6382 @node Origin Function, Shell Function, Eval Function, Functions
6383 @section The @code{origin} Function
6385 @cindex variables, origin of
6386 @cindex origin of variable
6388 The @code{origin} function is unlike most other functions in that it does
6389 not operate on the values of variables; it tells you something @emph{about}
6390 a variable. Specifically, it tells you where it came from.
6392 The syntax of the @code{origin} function is:
6395 $(origin @var{variable})
6398 Note that @var{variable} is the @emph{name} of a variable to inquire about;
6399 not a @emph{reference} to that variable. Therefore you would not normally
6400 use a @samp{$} or parentheses when writing it. (You can, however, use a
6401 variable reference in the name if you want the name not to be a constant.)
6403 The result of this function is a string telling you how the variable
6404 @var{variable} was defined:
6409 if @var{variable} was never defined.
6413 if @var{variable} has a default definition, as is usual with @code{CC}
6414 and so on. @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
6415 Note that if you have redefined a default variable, the @code{origin}
6416 function will return the origin of the later definition.
6420 if @var{variable} was defined as an environment variable and the
6421 @samp{-e} option is @emph{not} turned on (@pxref{Options Summary, ,Summary of Options}).
6423 @item environment override
6425 if @var{variable} was defined as an environment variable and the
6426 @w{@samp{-e}} option @emph{is} turned on (@pxref{Options Summary,
6427 ,Summary of Options}).@refill
6431 if @var{variable} was defined in a makefile.
6435 if @var{variable} was defined on the command line.
6439 if @var{variable} was defined with an @code{override} directive in a
6440 makefile (@pxref{Override Directive, ,The @code{override} Directive}).
6444 if @var{variable} is an automatic variable defined for the
6445 execution of the commands for each rule
6446 (@pxref{Automatic, , Automatic Variables}).
6449 This information is primarily useful (other than for your curiosity) to
6450 determine if you want to believe the value of a variable. For example,
6451 suppose you have a makefile @file{foo} that includes another makefile
6452 @file{bar}. You want a variable @code{bletch} to be defined in @file{bar}
6453 if you run the command @w{@samp{make -f bar}}, even if the environment contains
6454 a definition of @code{bletch}. However, if @file{foo} defined
6455 @code{bletch} before including @file{bar}, you do not want to override that
6456 definition. This could be done by using an @code{override} directive in
6457 @file{foo}, giving that definition precedence over the later definition in
6458 @file{bar}; unfortunately, the @code{override} directive would also
6459 override any command line definitions. So, @file{bar} could
6465 ifeq "$(origin bletch)" "environment"
6466 bletch = barf, gag, etc.
6473 If @code{bletch} has been defined from the environment, this will redefine
6476 If you want to override a previous definition of @code{bletch} if it came
6477 from the environment, even under @samp{-e}, you could instead write:
6481 ifneq "$(findstring environment,$(origin bletch))" ""
6482 bletch = barf, gag, etc.
6487 Here the redefinition takes place if @samp{$(origin bletch)} returns either
6488 @samp{environment} or @samp{environment override}.
6489 @xref{Text Functions, , Functions for String Substitution and Analysis}.
6491 @node Shell Function, Make Control Functions, Origin Function, Functions
6492 @section The @code{shell} Function
6494 @cindex commands, expansion
6496 @cindex shell command, function for
6498 The @code{shell} function is unlike any other function except the
6499 @code{wildcard} function
6500 (@pxref{Wildcard Function, ,The Function @code{wildcard}}) in that it
6501 communicates with the world outside of @code{make}.
6503 The @code{shell} function performs the same function that backquotes
6504 (@samp{`}) perform in most shells: it does @dfn{command expansion}. This
6505 means that it takes an argument that is a shell command and returns the
6506 output of the command. The only processing @code{make} does on the result,
6507 before substituting it into the surrounding text, is to convert each
6508 newline or carriage-return / newline pair to a single space. It also
6509 removes the trailing (carriage-return and) newline, if it's the last
6510 thing in the result.@refill
6512 The commands run by calls to the @code{shell} function are run when the
6513 function calls are expanded. In most cases, this is when the makefile is
6514 read in. The exception is that function calls in the commands of the rules
6515 are expanded when the commands are run, and this applies to @code{shell}
6516 function calls like all others.
6518 Here are some examples of the use of the @code{shell} function:
6521 contents := $(shell cat foo)
6525 sets @code{contents} to the contents of the file @file{foo}, with a space
6526 (rather than a newline) separating each line.
6529 files := $(shell echo *.c)
6533 sets @code{files} to the expansion of @samp{*.c}. Unless @code{make} is
6534 using a very strange shell, this has the same result as
6535 @w{@samp{$(wildcard *.c)}}.@refill
6537 @node Make Control Functions, , Shell Function, Functions
6538 @section Functions That Control Make
6539 @cindex functions, for controlling make
6540 @cindex controlling make
6542 These functions control the way make runs. Generally, they are used to
6543 provide information to the user of the makefile or to cause make to stop
6544 if some sort of environmental error is detected.
6547 @item $(error @var{text}@dots{})
6549 @cindex error, stopping on
6550 @cindex stopping make
6551 Generates a fatal error where the message is @var{text}. Note that the
6552 error is generated whenever this function is evaluated. So, if you put
6553 it inside a command script or on the right side of a recursive variable
6554 assignment, it won't be evaluated until later. The @var{text} will be
6555 expanded before the error is generated.
6561 $(error error is $(ERROR1))
6566 will generate a fatal error during the read of the makefile if the
6567 @code{make} variable @code{ERROR1} is defined. Or,
6570 ERR = $(error found an error!)
6577 will generate a fatal error while @code{make} is running, if the
6578 @code{err} target is invoked.
6580 @item $(warning @var{text}@dots{})
6582 @cindex warnings, printing
6583 @cindex printing user warnings
6584 This function works similarly to the @code{error} function, above,
6585 except that @code{make} doesn't exit. Instead, @var{text} is expanded
6586 and the resulting message is displayed, but processing of the makefile
6589 The result of the expansion of this function is the empty string.
6592 @node Running, Implicit Rules, Functions, Top
6593 @chapter How to Run @code{make}
6595 A makefile that says how to recompile a program can be used in more
6596 than one way. The simplest use is to recompile every file that is out
6597 of date. Usually, makefiles are written so that if you run
6598 @code{make} with no arguments, it does just that.
6600 But you might want to update only some of the files; you might want to use
6601 a different compiler or different compiler options; you might want just to
6602 find out which files are out of date without changing them.
6604 By giving arguments when you run @code{make}, you can do any of these
6605 things and many others.
6607 The exit status of @code{make} is always one of three values:
6610 The exit status is zero if @code{make} is successful.
6612 The exit status is two if @code{make} encounters any errors.
6613 It will print messages describing the particular errors.
6615 The exit status is one if you use the @samp{-q} flag and @code{make}
6616 determines that some target is not already up to date.
6617 @xref{Instead of Execution, ,Instead of Executing the Commands}.
6621 * Makefile Arguments:: How to specify which makefile to use.
6622 * Goals:: How to use goal arguments to specify which
6623 parts of the makefile to use.
6624 * Instead of Execution:: How to use mode flags to specify what
6625 kind of thing to do with the commands
6626 in the makefile other than simply
6628 * Avoiding Compilation:: How to avoid recompiling certain files.
6629 * Overriding:: How to override a variable to specify
6630 an alternate compiler and other things.
6631 * Testing:: How to proceed past some errors, to
6633 * Options Summary:: Summary of Options
6636 @node Makefile Arguments, Goals, Running, Running
6637 @section Arguments to Specify the Makefile
6638 @cindex @code{--file}
6639 @cindex @code{--makefile}
6642 The way to specify the name of the makefile is with the @samp{-f} or
6643 @samp{--file} option (@samp{--makefile} also works). For example,
6644 @samp{-f altmake} says to use the file @file{altmake} as the makefile.
6646 If you use the @samp{-f} flag several times and follow each @samp{-f}
6647 with an argument, all the specified files are used jointly as
6650 If you do not use the @samp{-f} or @samp{--file} flag, the default is
6651 to try @file{GNUmakefile}, @file{makefile}, and @file{Makefile}, in
6652 that order, and use the first of these three which exists or can be made
6653 (@pxref{Makefiles, ,Writing Makefiles}).@refill
6655 @node Goals, Instead of Execution, Makefile Arguments, Running
6656 @section Arguments to Specify the Goals
6657 @cindex goal, how to specify
6659 The @dfn{goals} are the targets that @code{make} should strive ultimately
6660 to update. Other targets are updated as well if they appear as
6661 prerequisites of goals, or prerequisites of prerequisites of goals, etc.
6663 By default, the goal is the first target in the makefile (not counting
6664 targets that start with a period). Therefore, makefiles are usually
6665 written so that the first target is for compiling the entire program or
6666 programs they describe. If the first rule in the makefile has several
6667 targets, only the first target in the rule becomes the default goal, not
6670 You can specify a different goal or goals with arguments to @code{make}.
6671 Use the name of the goal as an argument. If you specify several goals,
6672 @code{make} processes each of them in turn, in the order you name them.
6674 Any target in the makefile may be specified as a goal (unless it
6675 starts with @samp{-} or contains an @samp{=}, in which case it will be
6676 parsed as a switch or variable definition, respectively). Even
6677 targets not in the makefile may be specified, if @code{make} can find
6678 implicit rules that say how to make them.
6680 @cindex @code{MAKECMDGOALS}
6681 @vindex MAKECMDGOALS
6682 @code{Make} will set the special variable @code{MAKECMDGOALS} to the
6683 list of goals you specified on the command line. If no goals were given
6684 on the command line, this variable is empty. Note that this variable
6685 should be used only in special circumstances.
6687 An example of appropriate use is to avoid including @file{.d} files
6688 during @code{clean} rules (@pxref{Automatic Prerequisites}), so
6689 @code{make} won't create them only to immediately remove them
6694 sources = foo.c bar.c
6696 ifneq ($(MAKECMDGOALS),clean)
6697 include $(sources:.c=.d)
6702 One use of specifying a goal is if you want to compile only a part of
6703 the program, or only one of several programs. Specify as a goal each
6704 file that you wish to remake. For example, consider a directory containing
6705 several programs, with a makefile that starts like this:
6709 all: size nm ld ar as
6712 If you are working on the program @code{size}, you might want to say
6713 @w{@samp{make size}} so that only the files of that program are recompiled.
6715 Another use of specifying a goal is to make files that are not normally
6716 made. For example, there may be a file of debugging output, or a
6717 version of the program that is compiled specially for testing, which has
6718 a rule in the makefile but is not a prerequisite of the default goal.
6720 Another use of specifying a goal is to run the commands associated with
6721 a phony target (@pxref{Phony Targets}) or empty target (@pxref{Empty
6722 Targets, ,Empty Target Files to Record Events}). Many makefiles contain
6723 a phony target named @file{clean} which deletes everything except source
6724 files. Naturally, this is done only if you request it explicitly with
6725 @w{@samp{make clean}}. Following is a list of typical phony and empty
6726 target names. @xref{Standard Targets}, for a detailed list of all the
6727 standard target names which GNU software packages use.
6731 @cindex @code{all} @r{(standard target)}
6732 Make all the top-level targets the makefile knows about.
6735 @cindex @code{clean} @r{(standard target)}
6736 Delete all files that are normally created by running @code{make}.
6739 @cindex @code{mostlyclean} @r{(standard target)}
6740 Like @samp{clean}, but may refrain from deleting a few files that people
6741 normally don't want to recompile. For example, the @samp{mostlyclean}
6742 target for GCC does not delete @file{libgcc.a}, because recompiling it
6743 is rarely necessary and takes a lot of time.
6746 @cindex @code{distclean} @r{(standard target)}
6748 @cindex @code{realclean} @r{(standard target)}
6750 @cindex @code{clobber} @r{(standard target)}
6751 Any of these targets might be defined to delete @emph{more} files than
6752 @samp{clean} does. For example, this would delete configuration files
6753 or links that you would normally create as preparation for compilation,
6754 even if the makefile itself cannot create these files.
6757 @cindex @code{install} @r{(standard target)}
6758 Copy the executable file into a directory that users typically search
6759 for commands; copy any auxiliary files that the executable uses into
6760 the directories where it will look for them.
6763 @cindex @code{print} @r{(standard target)}
6764 Print listings of the source files that have changed.
6767 @cindex @code{tar} @r{(standard target)}
6768 Create a tar file of the source files.
6771 @cindex @code{shar} @r{(standard target)}
6772 Create a shell archive (shar file) of the source files.
6775 @cindex @code{dist} @r{(standard target)}
6776 Create a distribution file of the source files. This might
6777 be a tar file, or a shar file, or a compressed version of one of the
6778 above, or even more than one of the above.
6781 @cindex @code{TAGS} @r{(standard target)}
6782 Update a tags table for this program.
6785 @cindex @code{check} @r{(standard target)}
6787 @cindex @code{test} @r{(standard target)}
6788 Perform self tests on the program this makefile builds.
6791 @node Instead of Execution, Avoiding Compilation, Goals, Running
6792 @section Instead of Executing the Commands
6793 @cindex execution, instead of
6794 @cindex commands, instead of executing
6796 The makefile tells @code{make} how to tell whether a target is up to date,
6797 and how to update each target. But updating the targets is not always
6798 what you want. Certain options specify other activities for @code{make}.
6800 @comment Extra blank lines make it print better.
6806 @cindex @code{--just-print}
6807 @cindex @code{--dry-run}
6808 @cindex @code{--recon}
6811 ``No-op''. The activity is to print what commands would be used to make
6812 the targets up to date, but not actually execute them.
6816 @cindex @code{--touch}
6817 @cindex touching files
6818 @cindex target, touching
6821 ``Touch''. The activity is to mark the targets as up to date without
6822 actually changing them. In other words, @code{make} pretends to compile
6823 the targets but does not really change their contents.
6827 @cindex @code{--question}
6829 @cindex question mode
6831 ``Question''. The activity is to find out silently whether the targets
6832 are up to date already; but execute no commands in either case. In other
6833 words, neither compilation nor output will occur.
6836 @itemx --what-if=@var{file}
6837 @itemx --assume-new=@var{file}
6838 @itemx --new-file=@var{file}
6839 @cindex @code{--what-if}
6841 @cindex @code{--assume-new}
6842 @cindex @code{--new-file}
6844 @cindex files, assuming new
6846 ``What if''. Each @samp{-W} flag is followed by a file name. The given
6847 files' modification times are recorded by @code{make} as being the present
6848 time, although the actual modification times remain the same.
6849 You can use the @samp{-W} flag in conjunction with the @samp{-n} flag
6850 to see what would happen if you were to modify specific files.@refill
6853 With the @samp{-n} flag, @code{make} prints the commands that it would
6854 normally execute but does not execute them.
6856 With the @samp{-t} flag, @code{make} ignores the commands in the rules
6857 and uses (in effect) the command @code{touch} for each target that needs to
6858 be remade. The @code{touch} command is also printed, unless @samp{-s} or
6859 @code{.SILENT} is used. For speed, @code{make} does not actually invoke
6860 the program @code{touch}. It does the work directly.
6862 With the @samp{-q} flag, @code{make} prints nothing and executes no
6863 commands, but the exit status code it returns is zero if and only if the
6864 targets to be considered are already up to date. If the exit status is
6865 one, then some updating needs to be done. If @code{make} encounters an
6866 error, the exit status is two, so you can distinguish an error from a
6867 target that is not up to date.
6869 It is an error to use more than one of these three flags in the same
6870 invocation of @code{make}.
6872 The @samp{-n}, @samp{-t}, and @samp{-q} options do not affect command
6873 lines that begin with @samp{+} characters or contain the strings
6874 @samp{$(MAKE)} or @samp{$@{MAKE@}}. Note that only the line containing
6875 the @samp{+} character or the strings @samp{$(MAKE)} or @samp{$@{MAKE@}}
6876 is run regardless of these options. Other lines in the same rule are
6877 not run unless they too begin with @samp{+} or contain @samp{$(MAKE)} or
6878 @samp{$@{MAKE@}} (@xref{MAKE Variable, ,How the @code{MAKE} Variable Works}.)
6880 The @samp{-W} flag provides two features:
6884 If you also use the @samp{-n} or @samp{-q} flag, you can see what
6885 @code{make} would do if you were to modify some files.
6888 Without the @samp{-n} or @samp{-q} flag, when @code{make} is actually
6889 executing commands, the @samp{-W} flag can direct @code{make} to act
6890 as if some files had been modified, without actually modifying the
6894 Note that the options @samp{-p} and @samp{-v} allow you to obtain other
6895 information about @code{make} or about the makefiles in use
6896 (@pxref{Options Summary, ,Summary of Options}).@refill
6898 @node Avoiding Compilation, Overriding, Instead of Execution, Running
6899 @section Avoiding Recompilation of Some Files
6901 @cindex @code{--old-file}
6902 @cindex @code{--assume-old}
6903 @cindex files, assuming old
6904 @cindex files, avoiding recompilation of
6905 @cindex recompilation, avoiding
6907 Sometimes you may have changed a source file but you do not want to
6908 recompile all the files that depend on it. For example, suppose you add
6909 a macro or a declaration to a header file that many other files depend
6910 on. Being conservative, @code{make} assumes that any change in the
6911 header file requires recompilation of all dependent files, but you know
6912 that they do not need to be recompiled and you would rather not waste
6913 the time waiting for them to compile.
6915 If you anticipate the problem before changing the header file, you can
6916 use the @samp{-t} flag. This flag tells @code{make} not to run the
6917 commands in the rules, but rather to mark the target up to date by
6918 changing its last-modification date. You would follow this procedure:
6922 Use the command @samp{make} to recompile the source files that really
6923 need recompilation, ensuring that the object files are up-to-date
6927 Make the changes in the header files.
6930 Use the command @samp{make -t} to mark all the object files as
6931 up to date. The next time you run @code{make}, the changes in the
6932 header files will not cause any recompilation.
6935 If you have already changed the header file at a time when some files
6936 do need recompilation, it is too late to do this. Instead, you can
6937 use the @w{@samp{-o @var{file}}} flag, which marks a specified file as
6938 ``old'' (@pxref{Options Summary, ,Summary of Options}). This means
6939 that the file itself will not be remade, and nothing else will be
6940 remade on its account. Follow this procedure:
6944 Recompile the source files that need compilation for reasons independent
6945 of the particular header file, with @samp{make -o @var{headerfile}}.
6946 If several header files are involved, use a separate @samp{-o} option
6947 for each header file.
6950 Touch all the object files with @samp{make -t}.
6953 @node Overriding, Testing, Avoiding Compilation, Running
6954 @section Overriding Variables
6955 @cindex overriding variables with arguments
6956 @cindex variables, overriding with arguments
6957 @cindex command line variables
6958 @cindex variables, command line
6960 An argument that contains @samp{=} specifies the value of a variable:
6961 @samp{@var{v}=@var{x}} sets the value of the variable @var{v} to @var{x}.
6962 If you specify a value in this way, all ordinary assignments of the same
6963 variable in the makefile are ignored; we say they have been
6964 @dfn{overridden} by the command line argument.
6966 The most common way to use this facility is to pass extra flags to
6967 compilers. For example, in a properly written makefile, the variable
6968 @code{CFLAGS} is included in each command that runs the C compiler, so a
6969 file @file{foo.c} would be compiled something like this:
6972 cc -c $(CFLAGS) foo.c
6975 Thus, whatever value you set for @code{CFLAGS} affects each compilation
6976 that occurs. The makefile probably specifies the usual value for
6977 @code{CFLAGS}, like this:
6983 Each time you run @code{make}, you can override this value if you
6984 wish. For example, if you say @samp{make CFLAGS='-g -O'}, each C
6985 compilation will be done with @samp{cc -c -g -O}. (This also
6986 illustrates how you can use quoting in the shell to enclose spaces and
6987 other special characters in the value of a variable when you override
6990 The variable @code{CFLAGS} is only one of many standard variables that
6991 exist just so that you can change them this way. @xref{Implicit
6992 Variables, , Variables Used by Implicit Rules}, for a complete list.
6994 You can also program the makefile to look at additional variables of your
6995 own, giving the user the ability to control other aspects of how the
6996 makefile works by changing the variables.
6998 When you override a variable with a command argument, you can define either
6999 a recursively-expanded variable or a simply-expanded variable. The
7000 examples shown above make a recursively-expanded variable; to make a
7001 simply-expanded variable, write @samp{:=} instead of @samp{=}. But, unless
7002 you want to include a variable reference or function call in the
7003 @emph{value} that you specify, it makes no difference which kind of
7004 variable you create.
7006 There is one way that the makefile can change a variable that you have
7007 overridden. This is to use the @code{override} directive, which is a line
7008 that looks like this: @samp{override @var{variable} = @var{value}}
7009 (@pxref{Override Directive, ,The @code{override} Directive}).
7011 @node Testing, Options Summary, Overriding, Running
7012 @section Testing the Compilation of a Program
7013 @cindex testing compilation
7014 @cindex compilation, testing
7016 Normally, when an error happens in executing a shell command, @code{make}
7017 gives up immediately, returning a nonzero status. No further commands are
7018 executed for any target. The error implies that the goal cannot be
7019 correctly remade, and @code{make} reports this as soon as it knows.
7021 When you are compiling a program that you have just changed, this is not
7022 what you want. Instead, you would rather that @code{make} try compiling
7023 every file that can be tried, to show you as many compilation errors
7027 @cindex @code{--keep-going}
7028 On these occasions, you should use the @samp{-k} or
7029 @samp{--keep-going} flag. This tells @code{make} to continue to
7030 consider the other prerequisites of the pending targets, remaking them
7031 if necessary, before it gives up and returns nonzero status. For
7032 example, after an error in compiling one object file, @samp{make -k}
7033 will continue compiling other object files even though it already
7034 knows that linking them will be impossible. In addition to continuing
7035 after failed shell commands, @samp{make -k} will continue as much as
7036 possible after discovering that it does not know how to make a target
7037 or prerequisite file. This will always cause an error message, but
7038 without @samp{-k}, it is a fatal error (@pxref{Options Summary,
7039 ,Summary of Options}).@refill
7041 The usual behavior of @code{make} assumes that your purpose is to get the
7042 goals up to date; once @code{make} learns that this is impossible, it might
7043 as well report the failure immediately. The @samp{-k} flag says that the
7044 real purpose is to test as much as possible of the changes made in the
7045 program, perhaps to find several independent problems so that you can
7046 correct them all before the next attempt to compile. This is why Emacs'
7047 @kbd{M-x compile} command passes the @samp{-k} flag by default.
7049 @node Options Summary, , Testing, Running
7050 @section Summary of Options
7055 Here is a table of all the options @code{make} understands:
7062 These options are ignored for compatibility with other versions of @code{make}.
7066 @itemx --directory=@var{dir}
7067 @cindex @code{--directory}
7068 Change to directory @var{dir} before reading the makefiles. If multiple
7069 @samp{-C} options are specified, each is interpreted relative to the
7070 previous one: @samp{-C / -C etc} is equivalent to @samp{-C /etc}.
7071 This is typically used with recursive invocations of @code{make}
7072 (@pxref{Recursion, ,Recursive Use of @code{make}}).
7076 @c Extra blank line here makes the table look better.
7078 Print debugging information in addition to normal processing. The
7079 debugging information says which files are being considered for
7080 remaking, which file-times are being compared and with what results,
7081 which files actually need to be remade, which implicit rules are
7082 considered and which are applied---everything interesting about how
7083 @code{make} decides what to do. The @code{-d} option is equivalent to
7084 @samp{--debug=a} (see below).
7086 @item --debug[=@var{options}]
7087 @cindex @code{--debug}
7088 @c Extra blank line here makes the table look better.
7090 Print debugging information in addition to normal processing. Various
7091 levels and types of output can be chosen. With no arguments, print the
7092 ``basic'' level of debugging. Possible arguments are below; only the
7093 first character is considered, and values must be comma- or
7098 All types of debugging output are enabled. This is equivalent to using
7102 Basic debugging prints each target that was found to be out-of-date, and
7103 whether the build was successful or not.
7105 @item v (@i{verbose})
7106 A level above @samp{basic}; includes messages about which makefiles were
7107 parsed, prerequisites that did not need to be rebuilt, etc. This option
7108 also enables @samp{basic} messages.
7110 @item i (@i{implicit})
7111 Prints messages describing the implicit rule searches for each target.
7112 This option also enables @samp{basic} messages.
7115 Prints messages giving details on the invocation of specific subcommands.
7117 @item m (@i{makefile})
7118 By default, the above messages are not enabled while trying to remake
7119 the makefiles. This option enables messages while rebuilding makefiles,
7120 too. Note that the @samp{all} option does enable this option. This
7121 option also enables @samp{basic} messages.
7126 @itemx --environment-overrides
7127 @cindex @code{--environment-overrides}
7128 Give variables taken from the environment precedence
7129 over variables from makefiles.
7130 @xref{Environment, ,Variables from the Environment}.
7134 @itemx --file=@var{file}
7135 @cindex @code{--file}
7136 @itemx --makefile=@var{file}
7137 @cindex @code{--makefile}
7138 Read the file named @var{file} as a makefile.
7139 @xref{Makefiles, ,Writing Makefiles}.
7144 @cindex @code{--help}
7145 @c Extra blank line here makes the table look better.
7147 Remind you of the options that @code{make} understands and then exit.
7151 @itemx --ignore-errors
7152 @cindex @code{--ignore-errors}
7153 Ignore all errors in commands executed to remake files.
7154 @xref{Errors, ,Errors in Commands}.
7158 @itemx --include-dir=@var{dir}
7159 @cindex @code{--include-dir}
7160 Specifies a directory @var{dir} to search for included makefiles.
7161 @xref{Include, ,Including Other Makefiles}. If several @samp{-I}
7162 options are used to specify several directories, the directories are
7163 searched in the order specified.
7165 @item -j [@var{jobs}]
7167 @itemx --jobs[=@var{jobs}]
7168 @cindex @code{--jobs}
7169 Specifies the number of jobs (commands) to run simultaneously. With no
7170 argument, @code{make} runs as many jobs simultaneously as possible. If
7171 there is more than one @samp{-j} option, the last one is effective.
7172 @xref{Parallel, ,Parallel Execution},
7173 for more information on how commands are run.
7174 Note that this option is ignored on MS-DOS.
7179 @cindex @code{--keep-going}
7180 Continue as much as possible after an error. While the target that
7181 failed, and those that depend on it, cannot be remade, the other
7182 prerequisites of these targets can be processed all the same.
7183 @xref{Testing, ,Testing the Compilation of a Program}.
7185 @item -l [@var{load}]
7187 @itemx --load-average[=@var{load}]
7188 @cindex @code{--load-average}
7189 @itemx --max-load[=@var{load}]
7190 @cindex @code{--max-load}
7191 Specifies that no new jobs (commands) should be started if there are
7192 other jobs running and the load average is at least @var{load} (a
7193 floating-point number). With no argument, removes a previous load
7194 limit. @xref{Parallel, ,Parallel Execution}.
7199 @cindex @code{--just-print}
7201 @cindex @code{--dry-run}
7203 @cindex @code{--recon}
7204 @c Extra blank line here makes the table look better.
7206 Print the commands that would be executed, but do not execute them.
7207 @xref{Instead of Execution, ,Instead of Executing the Commands}.
7211 @itemx --old-file=@var{file}
7212 @cindex @code{--old-file}
7213 @itemx --assume-old=@var{file}
7214 @cindex @code{--assume-old}
7215 Do not remake the file @var{file} even if it is older than its
7216 prerequisites, and do not remake anything on account of changes in
7217 @var{file}. Essentially the file is treated as very old and its rules
7218 are ignored. @xref{Avoiding Compilation, ,Avoiding Recompilation of
7223 @itemx --print-data-base
7224 @cindex @code{--print-data-base}
7225 @cindex data base of @code{make} rules
7226 @cindex predefined rules and variables, printing
7227 Print the data base (rules and variable values) that results from
7228 reading the makefiles; then execute as usual or as otherwise specified.
7229 This also prints the version information given by the @samp{-v} switch
7230 (see below). To print the data base without trying to remake any files,
7231 use @w{@samp{make -qp}}. To print the data base of predefined rules and
7232 variables, use @w{@samp{make -p -f /dev/null}}. The data base output
7233 contains filename and linenumber information for command and variable
7234 definitions, so it can be a useful debugging tool in complex environments.
7239 @cindex @code{--question}
7240 ``Question mode''. Do not run any commands, or print anything; just
7241 return an exit status that is zero if the specified targets are already
7242 up to date, one if any remaking is required, or two if an error is
7243 encountered. @xref{Instead of Execution, ,Instead of Executing the
7248 @itemx --no-builtin-rules
7249 @cindex @code{--no-builtin-rules}
7250 Eliminate use of the built-in implicit rules (@pxref{Implicit Rules,
7251 ,Using Implicit Rules}). You can still define your own by writing
7252 pattern rules (@pxref{Pattern Rules, ,Defining and Redefining Pattern
7253 Rules}). The @samp{-r} option also clears out the default list of
7254 suffixes for suffix rules (@pxref{Suffix Rules, ,Old-Fashioned Suffix
7255 Rules}). But you can still define your own suffixes with a rule for
7256 @code{.SUFFIXES}, and then define your own suffix rules. Note that only
7257 @emph{rules} are affected by the @code{-r} option; default variables
7258 remain in effect (@pxref{Implicit Variables, ,Variables Used by Implicit
7259 Rules}); see the @samp{-R} option below.
7263 @itemx --no-builtin-variables
7264 @cindex @code{--no-builtin-variables}
7265 Eliminate use of the built-in rule-specific variables (@pxref{Implicit
7266 Variables, ,Variables Used by Implicit Rules}). You can still define
7267 your own, of course. The @samp{-R} option also automatically enables
7268 the @samp{-r} option (see above), since it doesn't make sense to have
7269 implicit rules without any definitions for the variables that they use.
7274 @cindex @code{--silent}
7276 @cindex @code{--quiet}
7277 @c Extra blank line here makes the table look better.
7279 Silent operation; do not print the commands as they are executed.
7280 @xref{Echoing, ,Command Echoing}.
7284 @itemx --no-keep-going
7285 @cindex @code{--no-keep-going}
7287 @cindex @code{--stop}
7288 @c Extra blank line here makes the table look better.
7290 Cancel the effect of the @samp{-k} option. This is never necessary
7291 except in a recursive @code{make} where @samp{-k} might be inherited
7292 from the top-level @code{make} via @code{MAKEFLAGS}
7293 (@pxref{Recursion, ,Recursive Use of @code{make}})
7294 or if you set @samp{-k} in @code{MAKEFLAGS} in your environment.@refill
7299 @cindex @code{--touch}
7300 @c Extra blank line here makes the table look better.
7302 Touch files (mark them up to date without really changing them)
7303 instead of running their commands. This is used to pretend that the
7304 commands were done, in order to fool future invocations of
7305 @code{make}. @xref{Instead of Execution, ,Instead of Executing the Commands}.
7310 @cindex @code{--version}
7311 Print the version of the @code{make} program plus a copyright, a list
7312 of authors, and a notice that there is no warranty; then exit.
7316 @itemx --print-directory
7317 @cindex @code{--print-directory}
7318 Print a message containing the working directory both before and after
7319 executing the makefile. This may be useful for tracking down errors
7320 from complicated nests of recursive @code{make} commands.
7321 @xref{Recursion, ,Recursive Use of @code{make}}. (In practice, you
7322 rarely need to specify this option since @samp{make} does it for you;
7323 see @ref{-w Option, ,The @samp{--print-directory} Option}.)
7325 @itemx --no-print-directory
7326 @cindex @code{--no-print-directory}
7327 Disable printing of the working directory under @code{-w}.
7328 This option is useful when @code{-w} is turned on automatically,
7329 but you do not want to see the extra messages.
7330 @xref{-w Option, ,The @samp{--print-directory} Option}.
7334 @itemx --what-if=@var{file}
7335 @cindex @code{--what-if}
7336 @itemx --new-file=@var{file}
7337 @cindex @code{--new-file}
7338 @itemx --assume-new=@var{file}
7339 @cindex @code{--assume-new}
7340 Pretend that the target @var{file} has just been modified. When used
7341 with the @samp{-n} flag, this shows you what would happen if you were
7342 to modify that file. Without @samp{-n}, it is almost the same as
7343 running a @code{touch} command on the given file before running
7344 @code{make}, except that the modification time is changed only in the
7345 imagination of @code{make}.
7346 @xref{Instead of Execution, ,Instead of Executing the Commands}.
7348 @item --warn-undefined-variables
7349 @cindex @code{--warn-undefined-variables}
7350 @cindex variables, warning for undefined
7351 @cindex undefined variables, warning message
7352 Issue a warning message whenever @code{make} sees a reference to an
7353 undefined variable. This can be helpful when you are trying to debug
7354 makefiles which use variables in complex ways.
7357 @node Implicit Rules, Archives, Running, Top
7358 @chapter Using Implicit Rules
7359 @cindex implicit rule
7360 @cindex rule, implicit
7362 Certain standard ways of remaking target files are used very often. For
7363 example, one customary way to make an object file is from a C source file
7364 using the C compiler, @code{cc}.
7366 @dfn{Implicit rules} tell @code{make} how to use customary techniques so
7367 that you do not have to specify them in detail when you want to use
7368 them. For example, there is an implicit rule for C compilation. File
7369 names determine which implicit rules are run. For example, C
7370 compilation typically takes a @file{.c} file and makes a @file{.o} file.
7371 So @code{make} applies the implicit rule for C compilation when it sees
7372 this combination of file name endings.@refill
7374 A chain of implicit rules can apply in sequence; for example, @code{make}
7375 will remake a @file{.o} file from a @file{.y} file by way of a @file{.c} file.
7377 @xref{Chained Rules, ,Chains of Implicit Rules}.
7380 The built-in implicit rules use several variables in their commands so
7381 that, by changing the values of the variables, you can change the way the
7382 implicit rule works. For example, the variable @code{CFLAGS} controls the
7383 flags given to the C compiler by the implicit rule for C compilation.
7385 @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
7388 You can define your own implicit rules by writing @dfn{pattern rules}.
7390 @xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.
7393 @dfn{Suffix rules} are a more limited way to define implicit rules.
7394 Pattern rules are more general and clearer, but suffix rules are
7395 retained for compatibility.
7397 @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
7401 * Using Implicit:: How to use an existing implicit rule
7402 to get the commands for updating a file.
7403 * Catalogue of Rules:: A list of built-in implicit rules.
7404 * Implicit Variables:: How to change what predefined rules do.
7405 * Chained Rules:: How to use a chain of implicit rules.
7406 * Pattern Rules:: How to define new implicit rules.
7407 * Last Resort:: How to defining commands for rules
7408 which cannot find any.
7409 * Suffix Rules:: The old-fashioned style of implicit rule.
7410 * Implicit Rule Search:: The precise algorithm for applying
7414 @node Using Implicit, Catalogue of Rules, Implicit Rules, Implicit Rules
7415 @section Using Implicit Rules
7416 @cindex implicit rule, how to use
7417 @cindex rule, implicit, how to use
7419 To allow @code{make} to find a customary method for updating a target file,
7420 all you have to do is refrain from specifying commands yourself. Either
7421 write a rule with no command lines, or don't write a rule at all. Then
7422 @code{make} will figure out which implicit rule to use based on which
7423 kind of source file exists or can be made.
7425 For example, suppose the makefile looks like this:
7429 cc -o foo foo.o bar.o $(CFLAGS) $(LDFLAGS)
7433 Because you mention @file{foo.o} but do not give a rule for it, @code{make}
7434 will automatically look for an implicit rule that tells how to update it.
7435 This happens whether or not the file @file{foo.o} currently exists.
7437 If an implicit rule is found, it can supply both commands and one or
7438 more prerequisites (the source files). You would want to write a rule
7439 for @file{foo.o} with no command lines if you need to specify additional
7440 prerequisites, such as header files, that the implicit rule cannot
7443 Each implicit rule has a target pattern and prerequisite patterns. There may
7444 be many implicit rules with the same target pattern. For example, numerous
7445 rules make @samp{.o} files: one, from a @samp{.c} file with the C compiler;
7446 another, from a @samp{.p} file with the Pascal compiler; and so on. The rule
7447 that actually applies is the one whose prerequisites exist or can be made.
7448 So, if you have a file @file{foo.c}, @code{make} will run the C compiler;
7449 otherwise, if you have a file @file{foo.p}, @code{make} will run the Pascal
7450 compiler; and so on.
7452 Of course, when you write the makefile, you know which implicit rule you
7453 want @code{make} to use, and you know it will choose that one because you
7454 know which possible prerequisite files are supposed to exist.
7455 @xref{Catalogue of Rules, ,Catalogue of Implicit Rules},
7456 for a catalogue of all the predefined implicit rules.
7458 Above, we said an implicit rule applies if the required prerequisites ``exist
7459 or can be made''. A file ``can be made'' if it is mentioned explicitly in
7460 the makefile as a target or a prerequisite, or if an implicit rule can be
7461 recursively found for how to make it. When an implicit prerequisite is the
7462 result of another implicit rule, we say that @dfn{chaining} is occurring.
7463 @xref{Chained Rules, ,Chains of Implicit Rules}.
7465 In general, @code{make} searches for an implicit rule for each target, and
7466 for each double-colon rule, that has no commands. A file that is mentioned
7467 only as a prerequisite is considered a target whose rule specifies nothing,
7468 so implicit rule search happens for it. @xref{Implicit Rule Search, ,Implicit Rule Search Algorithm}, for the
7469 details of how the search is done.
7471 Note that explicit prerequisites do not influence implicit rule search.
7472 For example, consider this explicit rule:
7479 The prerequisite on @file{foo.p} does not necessarily mean that
7480 @code{make} will remake @file{foo.o} according to the implicit rule to
7481 make an object file, a @file{.o} file, from a Pascal source file, a
7482 @file{.p} file. For example, if @file{foo.c} also exists, the implicit
7483 rule to make an object file from a C source file is used instead,
7484 because it appears before the Pascal rule in the list of predefined
7485 implicit rules (@pxref{Catalogue of Rules, , Catalogue of Implicit
7488 If you do not want an implicit rule to be used for a target that has no
7489 commands, you can give that target empty commands by writing a semicolon
7490 (@pxref{Empty Commands, ,Defining Empty Commands}).
7492 @node Catalogue of Rules, Implicit Variables, Using Implicit, Implicit Rules
7493 @section Catalogue of Implicit Rules
7494 @cindex implicit rule, predefined
7495 @cindex rule, implicit, predefined
7497 Here is a catalogue of predefined implicit rules which are always
7498 available unless the makefile explicitly overrides or cancels them.
7499 @xref{Canceling Rules, ,Canceling Implicit Rules}, for information on
7500 canceling or overriding an implicit rule. The @samp{-r} or
7501 @samp{--no-builtin-rules} option cancels all predefined rules.
7503 Not all of these rules will always be defined, even when the @samp{-r}
7504 option is not given. Many of the predefined implicit rules are
7505 implemented in @code{make} as suffix rules, so which ones will be
7506 defined depends on the @dfn{suffix list} (the list of prerequisites of
7507 the special target @code{.SUFFIXES}). The default suffix list is:
7508 @code{.out}, @code{.a}, @code{.ln}, @code{.o}, @code{.c}, @code{.cc},
7509 @code{.C}, @code{.p}, @code{.f}, @code{.F}, @code{.r}, @code{.y},
7510 @code{.l}, @code{.s}, @code{.S}, @code{.mod}, @code{.sym}, @code{.def},
7511 @code{.h}, @code{.info}, @code{.dvi}, @code{.tex}, @code{.texinfo},
7512 @code{.texi}, @code{.txinfo}, @code{.w}, @code{.ch} @code{.web},
7513 @code{.sh}, @code{.elc}, @code{.el}. All of the implicit rules
7514 described below whose prerequisites have one of these suffixes are
7515 actually suffix rules. If you modify the suffix list, the only
7516 predefined suffix rules in effect will be those named by one or two of
7517 the suffixes that are on the list you specify; rules whose suffixes fail
7518 to be on the list are disabled. @xref{Suffix Rules, ,Old-Fashioned
7519 Suffix Rules}, for full details on suffix rules.
7522 @item Compiling C programs
7523 @cindex C, rule to compile
7528 @file{@var{n}.o} is made automatically from @file{@var{n}.c} with
7529 a command of the form @samp{$(CC) -c $(CPPFLAGS) $(CFLAGS)}.@refill
7531 @item Compiling C++ programs
7532 @cindex C++, rule to compile
7536 @file{@var{n}.o} is made automatically from @file{@var{n}.cc} or
7537 @file{@var{n}.C} with a command of the form @samp{$(CXX) -c $(CPPFLAGS)
7538 $(CXXFLAGS)}. We encourage you to use the suffix @samp{.cc} for C++
7539 source files instead of @samp{.C}.@refill
7541 @item Compiling Pascal programs
7542 @cindex Pascal, rule to compile
7545 @file{@var{n}.o} is made automatically from @file{@var{n}.p}
7546 with the command @samp{$(PC) -c $(PFLAGS)}.@refill
7548 @item Compiling Fortran and Ratfor programs
7549 @cindex Fortran, rule to compile
7550 @cindex Ratfor, rule to compile
7555 @file{@var{n}.o} is made automatically from @file{@var{n}.r},
7556 @file{@var{n}.F} or @file{@var{n}.f} by running the
7557 Fortran compiler. The precise command used is as follows:@refill
7561 @samp{$(FC) -c $(FFLAGS)}.
7563 @samp{$(FC) -c $(FFLAGS) $(CPPFLAGS)}.
7565 @samp{$(FC) -c $(FFLAGS) $(RFLAGS)}.
7568 @item Preprocessing Fortran and Ratfor programs
7569 @file{@var{n}.f} is made automatically from @file{@var{n}.r} or
7570 @file{@var{n}.F}. This rule runs just the preprocessor to convert a
7571 Ratfor or preprocessable Fortran program into a strict Fortran
7572 program. The precise command used is as follows:@refill
7576 @samp{$(FC) -F $(CPPFLAGS) $(FFLAGS)}.
7578 @samp{$(FC) -F $(FFLAGS) $(RFLAGS)}.
7581 @item Compiling Modula-2 programs
7582 @cindex Modula-2, rule to compile
7587 @file{@var{n}.sym} is made from @file{@var{n}.def} with a command
7588 of the form @samp{$(M2C) $(M2FLAGS) $(DEFFLAGS)}. @file{@var{n}.o}
7589 is made from @file{@var{n}.mod}; the form is:
7590 @w{@samp{$(M2C) $(M2FLAGS) $(MODFLAGS)}}.@refill
7593 @item Assembling and preprocessing assembler programs
7594 @cindex assembly, rule to compile
7597 @file{@var{n}.o} is made automatically from @file{@var{n}.s} by
7598 running the assembler, @code{as}. The precise command is
7599 @samp{$(AS) $(ASFLAGS)}.@refill
7602 @file{@var{n}.s} is made automatically from @file{@var{n}.S} by
7603 running the C preprocessor, @code{cpp}. The precise command is
7604 @w{@samp{$(CPP) $(CPPFLAGS)}}.
7606 @item Linking a single object file
7607 @cindex linking, predefined rule for
7610 @file{@var{n}} is made automatically from @file{@var{n}.o} by running
7611 the linker (usually called @code{ld}) via the C compiler. The precise
7612 command used is @w{@samp{$(CC) $(LDFLAGS) @var{n}.o $(LOADLIBES) $(LDLIBS)}}.
7614 This rule does the right thing for a simple program with only one
7615 source file. It will also do the right thing if there are multiple
7616 object files (presumably coming from various other source files), one
7617 of which has a name matching that of the executable file. Thus,
7624 when @file{x.c}, @file{y.c} and @file{z.c} all exist will execute:
7639 In more complicated cases, such as when there is no object file whose
7640 name derives from the executable file name, you must write an explicit
7641 command for linking.
7643 Each kind of file automatically made into @samp{.o} object files will
7644 be automatically linked by using the compiler (@samp{$(CC)},
7645 @samp{$(FC)} or @samp{$(PC)}; the C compiler @samp{$(CC)} is used to
7646 assemble @samp{.s} files) without the @samp{-c} option. This could be
7647 done by using the @samp{.o} object files as intermediates, but it is
7648 faster to do the compiling and linking in one step, so that's how it's
7651 @item Yacc for C programs
7653 @cindex Yacc, rule to run
7655 @file{@var{n}.c} is made automatically from @file{@var{n}.y} by
7656 running Yacc with the command @samp{$(YACC) $(YFLAGS)}.
7658 @item Lex for C programs
7660 @cindex Lex, rule to run
7662 @file{@var{n}.c} is made automatically from @file{@var{n}.l} by
7663 running Lex. The actual command is @samp{$(LEX) $(LFLAGS)}.
7665 @item Lex for Ratfor programs
7666 @file{@var{n}.r} is made automatically from @file{@var{n}.l} by
7667 running Lex. The actual command is @samp{$(LEX) $(LFLAGS)}.
7669 The convention of using the same suffix @samp{.l} for all Lex files
7670 regardless of whether they produce C code or Ratfor code makes it
7671 impossible for @code{make} to determine automatically which of the two
7672 languages you are using in any particular case. If @code{make} is
7673 called upon to remake an object file from a @samp{.l} file, it must
7674 guess which compiler to use. It will guess the C compiler, because
7675 that is more common. If you are using Ratfor, make sure @code{make}
7676 knows this by mentioning @file{@var{n}.r} in the makefile. Or, if you
7677 are using Ratfor exclusively, with no C files, remove @samp{.c} from
7678 the list of implicit rule suffixes with:@refill
7683 .SUFFIXES: .o .r .f .l @dots{}
7687 @item Making Lint Libraries from C, Yacc, or Lex programs
7689 @cindex @code{lint}, rule to run
7691 @file{@var{n}.ln} is made from @file{@var{n}.c} by running @code{lint}.
7692 The precise command is @w{@samp{$(LINT) $(LINTFLAGS) $(CPPFLAGS) -i}}.
7693 The same command is used on the C code produced from
7694 @file{@var{n}.y} or @file{@var{n}.l}.@refill
7696 @item @TeX{} and Web
7697 @cindex @TeX{}, rule to run
7698 @cindex Web, rule to run
7709 @file{@var{n}.dvi} is made from @file{@var{n}.tex} with the command
7710 @samp{$(TEX)}. @file{@var{n}.tex} is made from @file{@var{n}.web} with
7711 @samp{$(WEAVE)}, or from @file{@var{n}.w} (and from @file{@var{n}.ch} if
7712 it exists or can be made) with @samp{$(CWEAVE)}. @file{@var{n}.p} is
7713 made from @file{@var{n}.web} with @samp{$(TANGLE)} and @file{@var{n}.c}
7714 is made from @file{@var{n}.w} (and from @file{@var{n}.ch} if it exists
7715 or can be made) with @samp{$(CTANGLE)}.@refill
7717 @item Texinfo and Info
7718 @cindex Texinfo, rule to format
7719 @cindex Info, rule to format
7726 @file{@var{n}.dvi} is made from @file{@var{n}.texinfo},
7727 @file{@var{n}.texi}, or @file{@var{n}.txinfo}, with the command
7728 @w{@samp{$(TEXI2DVI) $(TEXI2DVI_FLAGS)}}. @file{@var{n}.info} is made from
7729 @file{@var{n}.texinfo}, @file{@var{n}.texi}, or @file{@var{n}.txinfo}, with
7730 the command @w{@samp{$(MAKEINFO) $(MAKEINFO_FLAGS)}}.
7733 @cindex RCS, rule to extract from
7735 @pindex ,v @r{(RCS file extension)}
7736 Any file @file{@var{n}} is extracted if necessary from an RCS file
7737 named either @file{@var{n},v} or @file{RCS/@var{n},v}. The precise
7738 command used is @w{@samp{$(CO) $(COFLAGS)}}. @file{@var{n}} will not be
7739 extracted from RCS if it already exists, even if the RCS file is
7740 newer. The rules for RCS are terminal
7741 (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}),
7742 so RCS files cannot be generated from another source; they must
7743 actually exist.@refill
7746 @cindex SCCS, rule to extract from
7748 @pindex s. @r{(SCCS file prefix)}
7749 Any file @file{@var{n}} is extracted if necessary from an SCCS file
7750 named either @file{s.@var{n}} or @file{SCCS/s.@var{n}}. The precise
7751 command used is @w{@samp{$(GET) $(GFLAGS)}}. The rules for SCCS are
7752 terminal (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}),
7753 so SCCS files cannot be generated from another source; they must
7754 actually exist.@refill
7757 For the benefit of SCCS, a file @file{@var{n}} is copied from
7758 @file{@var{n}.sh} and made executable (by everyone). This is for
7759 shell scripts that are checked into SCCS. Since RCS preserves the
7760 execution permission of a file, you do not need to use this feature
7763 We recommend that you avoid using of SCCS. RCS is widely held to be
7764 superior, and is also free. By choosing free software in place of
7765 comparable (or inferior) proprietary software, you support the free
7769 Usually, you want to change only the variables listed in the table
7770 above, which are documented in the following section.
7772 However, the commands in built-in implicit rules actually use
7773 variables such as @code{COMPILE.c}, @code{LINK.p}, and
7774 @code{PREPROCESS.S}, whose values contain the commands listed above.
7776 @code{make} follows the convention that the rule to compile a
7777 @file{.@var{x}} source file uses the variable @code{COMPILE.@var{x}}.
7778 Similarly, the rule to produce an executable from a @file{.@var{x}}
7779 file uses @code{LINK.@var{x}}; and the rule to preprocess a
7780 @file{.@var{x}} file uses @code{PREPROCESS.@var{x}}.
7782 @vindex OUTPUT_OPTION
7783 Every rule that produces an object file uses the variable
7784 @code{OUTPUT_OPTION}. @code{make} defines this variable either to
7785 contain @samp{-o $@@}, or to be empty, depending on a compile-time
7786 option. You need the @samp{-o} option to ensure that the output goes
7787 into the right file when the source file is in a different directory,
7788 as when using @code{VPATH} (@pxref{Directory Search}). However,
7789 compilers on some systems do not accept a @samp{-o} switch for object
7790 files. If you use such a system, and use @code{VPATH}, some
7791 compilations will put their output in the wrong place.
7792 A possible workaround for this problem is to give @code{OUTPUT_OPTION}
7793 the value @w{@samp{; mv $*.o $@@}}.
7795 @node Implicit Variables, Chained Rules, Catalogue of Rules, Implicit Rules
7796 @section Variables Used by Implicit Rules
7797 @cindex flags for compilers
7799 The commands in built-in implicit rules make liberal use of certain
7800 predefined variables. You can alter these variables in the makefile,
7801 with arguments to @code{make}, or in the environment to alter how the
7802 implicit rules work without redefining the rules themselves. You can
7803 cancel all variables used by implicit rules with the @samp{-R} or
7804 @samp{--no-builtin-variables} option.
7806 For example, the command used to compile a C source file actually says
7807 @samp{$(CC) -c $(CFLAGS) $(CPPFLAGS)}. The default values of the variables
7808 used are @samp{cc} and nothing, resulting in the command @samp{cc -c}. By
7809 redefining @samp{CC} to @samp{ncc}, you could cause @samp{ncc} to be
7810 used for all C compilations performed by the implicit rule. By redefining
7811 @samp{CFLAGS} to be @samp{-g}, you could pass the @samp{-g} option to
7812 each compilation. @emph{All} implicit rules that do C compilation use
7813 @samp{$(CC)} to get the program name for the compiler and @emph{all}
7814 include @samp{$(CFLAGS)} among the arguments given to the compiler.@refill
7816 The variables used in implicit rules fall into two classes: those that are
7817 names of programs (like @code{CC}) and those that contain arguments for the
7818 programs (like @code{CFLAGS}). (The ``name of a program'' may also contain
7819 some command arguments, but it must start with an actual executable program
7820 name.) If a variable value contains more than one argument, separate them
7823 Here is a table of variables used as names of programs in built-in rules:
7828 Archive-maintaining program; default @samp{ar}.
7833 Program for doing assembly; default @samp{as}.
7838 Program for compiling C programs; default @samp{cc}.
7843 Program for compiling C++ programs; default @samp{g++}.
7848 Program for extracting a file from RCS; default @samp{co}.
7853 Program for running the C preprocessor, with results to standard output;
7854 default @samp{$(CC) -E}.
7858 Program for compiling or preprocessing Fortran and Ratfor programs;
7864 Program for extracting a file from SCCS; default @samp{get}.
7869 Program to use to turn Lex grammars into C programs or Ratfor programs;
7875 Program for compiling Pascal programs; default @samp{pc}.
7880 Program to use to turn Yacc grammars into C programs; default @samp{yacc}.
7885 Program to use to turn Yacc grammars into Ratfor
7886 programs; default @samp{yacc -r}.
7890 Program to convert a Texinfo source file into an Info file; default
7896 Program to make @TeX{} @sc{dvi} files from @TeX{} source;
7902 Program to make @TeX{} @sc{dvi} files from Texinfo source;
7903 default @samp{texi2dvi}.
7908 Program to translate Web into @TeX{}; default @samp{weave}.
7913 Program to translate C Web into @TeX{}; default @samp{cweave}.
7918 Program to translate Web into Pascal; default @samp{tangle}.
7923 Program to translate C Web into C; default @samp{ctangle}.
7928 Command to remove a file; default @samp{rm -f}.
7932 Here is a table of variables whose values are additional arguments for the
7933 programs above. The default values for all of these is the empty
7934 string, unless otherwise noted.
7939 Flags to give the archive-maintaining program; default @samp{rv}.
7943 Extra flags to give to the assembler (when explicitly
7944 invoked on a @samp{.s} or @samp{.S} file).
7948 Extra flags to give to the C compiler.
7952 Extra flags to give to the C++ compiler.
7956 Extra flags to give to the RCS @code{co} program.
7960 Extra flags to give to the C preprocessor and programs
7961 that use it (the C and Fortran compilers).
7965 Extra flags to give to the Fortran compiler.
7969 Extra flags to give to the SCCS @code{get} program.
7973 Extra flags to give to compilers when they are
7974 supposed to invoke the linker, @samp{ld}.
7978 Extra flags to give to Lex.
7982 Extra flags to give to the Pascal compiler.
7986 Extra flags to give to the Fortran compiler for Ratfor programs.
7990 Extra flags to give to Yacc.
7993 @node Chained Rules, Pattern Rules, Implicit Variables, Implicit Rules
7994 @section Chains of Implicit Rules
7996 @cindex chains of rules
7997 @cindex rule, implicit, chains of
7998 Sometimes a file can be made by a sequence of implicit rules. For example,
7999 a file @file{@var{n}.o} could be made from @file{@var{n}.y} by running
8000 first Yacc and then @code{cc}. Such a sequence is called a @dfn{chain}.
8002 If the file @file{@var{n}.c} exists, or is mentioned in the makefile, no
8003 special searching is required: @code{make} finds that the object file can
8004 be made by C compilation from @file{@var{n}.c}; later on, when considering
8005 how to make @file{@var{n}.c}, the rule for running Yacc is
8006 used. Ultimately both @file{@var{n}.c} and @file{@var{n}.o} are
8009 @cindex intermediate files
8010 @cindex files, intermediate
8011 However, even if @file{@var{n}.c} does not exist and is not mentioned,
8012 @code{make} knows how to envision it as the missing link between
8013 @file{@var{n}.o} and @file{@var{n}.y}! In this case, @file{@var{n}.c} is
8014 called an @dfn{intermediate file}. Once @code{make} has decided to use the
8015 intermediate file, it is entered in the data base as if it had been
8016 mentioned in the makefile, along with the implicit rule that says how to
8019 Intermediate files are remade using their rules just like all other
8020 files. But intermediate files are treated differently in two ways.
8022 The first difference is what happens if the intermediate file does not
8023 exist. If an ordinary file @var{b} does not exist, and @code{make}
8024 considers a target that depends on @var{b}, it invariably creates
8025 @var{b} and then updates the target from @var{b}. But if @var{b} is an
8026 intermediate file, then @code{make} can leave well enough alone. It
8027 won't bother updating @var{b}, or the ultimate target, unless some
8028 prerequisite of @var{b} is newer than that target or there is some other
8029 reason to update that target.
8031 The second difference is that if @code{make} @emph{does} create @var{b}
8032 in order to update something else, it deletes @var{b} later on after it
8033 is no longer needed. Therefore, an intermediate file which did not
8034 exist before @code{make} also does not exist after @code{make}.
8035 @code{make} reports the deletion to you by printing a @samp{rm -f}
8036 command showing which file it is deleting.
8038 Ordinarily, a file cannot be intermediate if it is mentioned in the
8039 makefile as a target or prerequisite. However, you can explicitly mark a
8040 file as intermediate by listing it as a prerequisite of the special target
8041 @code{.INTERMEDIATE}. This takes effect even if the file is mentioned
8042 explicitly in some other way.
8044 @cindex intermediate files, preserving
8045 @cindex preserving intermediate files
8046 @cindex secondary files
8047 You can prevent automatic deletion of an intermediate file by marking it
8048 as a @dfn{secondary} file. To do this, list it as a prerequisite of the
8049 special target @code{.SECONDARY}. When a file is secondary, @code{make}
8050 will not create the file merely because it does not already exist, but
8051 @code{make} does not automatically delete the file. Marking a file as
8052 secondary also marks it as intermediate.
8054 You can list the target pattern of an implicit rule (such as @samp{%.o})
8055 as a prerequisite of the special target @code{.PRECIOUS} to preserve
8056 intermediate files made by implicit rules whose target patterns match
8057 that file's name; see @ref{Interrupts}.@refill
8058 @cindex preserving with @code{.PRECIOUS}
8059 @cindex @code{.PRECIOUS} intermediate files
8061 A chain can involve more than two implicit rules. For example, it is
8062 possible to make a file @file{foo} from @file{RCS/foo.y,v} by running RCS,
8063 Yacc and @code{cc}. Then both @file{foo.y} and @file{foo.c} are
8064 intermediate files that are deleted at the end.@refill
8066 No single implicit rule can appear more than once in a chain. This means
8067 that @code{make} will not even consider such a ridiculous thing as making
8068 @file{foo} from @file{foo.o.o} by running the linker twice. This
8069 constraint has the added benefit of preventing any infinite loop in the
8070 search for an implicit rule chain.
8072 There are some special implicit rules to optimize certain cases that would
8073 otherwise be handled by rule chains. For example, making @file{foo} from
8074 @file{foo.c} could be handled by compiling and linking with separate
8075 chained rules, using @file{foo.o} as an intermediate file. But what
8076 actually happens is that a special rule for this case does the compilation
8077 and linking with a single @code{cc} command. The optimized rule is used in
8078 preference to the step-by-step chain because it comes earlier in the
8081 @node Pattern Rules, Last Resort, Chained Rules, Implicit Rules
8082 @section Defining and Redefining Pattern Rules
8084 You define an implicit rule by writing a @dfn{pattern rule}. A pattern
8085 rule looks like an ordinary rule, except that its target contains the
8086 character @samp{%} (exactly one of them). The target is considered a
8087 pattern for matching file names; the @samp{%} can match any nonempty
8088 substring, while other characters match only themselves. The prerequisites
8089 likewise use @samp{%} to show how their names relate to the target name.
8091 Thus, a pattern rule @samp{%.o : %.c} says how to make any file
8092 @file{@var{stem}.o} from another file @file{@var{stem}.c}.@refill
8094 Note that expansion using @samp{%} in pattern rules occurs
8095 @strong{after} any variable or function expansions, which take place
8096 when the makefile is read. @xref{Using Variables, , How to Use
8097 Variables}, and @ref{Functions, ,Functions for Transforming Text}.
8100 * Pattern Intro:: An introduction to pattern rules.
8101 * Pattern Examples:: Examples of pattern rules.
8102 * Automatic:: How to use automatic variables in the
8103 commands of implicit rules.
8104 * Pattern Match:: How patterns match.
8105 * Match-Anything Rules:: Precautions you should take prior to
8106 defining rules that can match any
8107 target file whatever.
8108 * Canceling Rules:: How to override or cancel built-in rules.
8111 @node Pattern Intro, Pattern Examples, Pattern Rules, Pattern Rules
8112 @subsection Introduction to Pattern Rules
8113 @cindex pattern rule
8114 @cindex rule, pattern
8116 A pattern rule contains the character @samp{%} (exactly one of them)
8117 in the target; otherwise, it looks exactly like an ordinary rule. The
8118 target is a pattern for matching file names; the @samp{%} matches any
8119 nonempty substring, while other characters match only themselves.
8120 @cindex target pattern, implicit
8121 @cindex @code{%}, in pattern rules
8123 For example, @samp{%.c} as a pattern matches any file name that ends in
8124 @samp{.c}. @samp{s.%.c} as a pattern matches any file name that starts
8125 with @samp{s.}, ends in @samp{.c} and is at least five characters long.
8126 (There must be at least one character to match the @samp{%}.) The substring
8127 that the @samp{%} matches is called the @dfn{stem}.@refill
8129 @samp{%} in a prerequisite of a pattern rule stands for the same stem
8130 that was matched by the @samp{%} in the target. In order for
8131 the pattern rule to apply, its target pattern must match the file name
8132 under consideration, and its prerequisite patterns must name files that
8133 exist or can be made. These files become prerequisites of the target.
8134 @cindex prerequisite pattern, implicit
8136 Thus, a rule of the form
8139 %.o : %.c ; @var{command}@dots{}
8143 specifies how to make a file @file{@var{n}.o}, with another file
8144 @file{@var{n}.c} as its prerequisite, provided that @file{@var{n}.c}
8145 exists or can be made.
8147 There may also be prerequisites that do not use @samp{%}; such a prerequisite
8148 attaches to every file made by this pattern rule. These unvarying
8149 prerequisites are useful occasionally.
8151 A pattern rule need not have any prerequisites that contain @samp{%}, or
8152 in fact any prerequisites at all. Such a rule is effectively a general
8153 wildcard. It provides a way to make any file that matches the target
8154 pattern. @xref{Last Resort}.
8156 @c !!! The end of of this paragraph should be rewritten. --bob
8157 Pattern rules may have more than one target. Unlike normal rules, this
8158 does not act as many different rules with the same prerequisites and
8159 commands. If a pattern rule has multiple targets, @code{make} knows that
8160 the rule's commands are responsible for making all of the targets. The
8161 commands are executed only once to make all the targets. When searching
8162 for a pattern rule to match a target, the target patterns of a rule other
8163 than the one that matches the target in need of a rule are incidental:
8164 @code{make} worries only about giving commands and prerequisites to the file
8165 presently in question. However, when this file's commands are run, the
8166 other targets are marked as having been updated themselves.
8167 @cindex multiple targets, in pattern rule
8168 @cindex target, multiple in pattern rule
8170 The order in which pattern rules appear in the makefile is important
8171 since this is the order in which they are considered.
8172 Of equally applicable
8173 rules, only the first one found is used. The rules you write take precedence
8174 over those that are built in. Note however, that a rule whose
8175 prerequisites actually exist or are mentioned always takes priority over a
8176 rule with prerequisites that must be made by chaining other implicit rules.
8177 @cindex pattern rules, order of
8178 @cindex order of pattern rules
8180 @node Pattern Examples, Automatic, Pattern Intro, Pattern Rules
8181 @subsection Pattern Rule Examples
8183 Here are some examples of pattern rules actually predefined in
8184 @code{make}. First, the rule that compiles @samp{.c} files into @samp{.o}
8189 $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@@
8193 defines a rule that can make any file @file{@var{x}.o} from
8194 @file{@var{x}.c}. The command uses the automatic variables @samp{$@@} and
8195 @samp{$<} to substitute the names of the target file and the source file
8196 in each case where the rule applies (@pxref{Automatic, ,Automatic Variables}).@refill
8198 Here is a second built-in rule:
8206 defines a rule that can make any file @file{@var{x}} whatsoever from a
8207 corresponding file @file{@var{x},v} in the subdirectory @file{RCS}. Since
8208 the target is @samp{%}, this rule will apply to any file whatever, provided
8209 the appropriate prerequisite file exists. The double colon makes the rule
8210 @dfn{terminal}, which means that its prerequisite may not be an intermediate
8211 file (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}).@refill
8214 This pattern rule has two targets:
8218 %.tab.c %.tab.h: %.y
8224 @c The following paragraph is rewritten to avoid overfull hboxes
8225 This tells @code{make} that the command @samp{bison -d @var{x}.y} will
8226 make both @file{@var{x}.tab.c} and @file{@var{x}.tab.h}. If the file
8227 @file{foo} depends on the files @file{parse.tab.o} and @file{scan.o}
8228 and the file @file{scan.o} depends on the file @file{parse.tab.h},
8229 when @file{parse.y} is changed, the command @samp{bison -d parse.y}
8230 will be executed only once, and the prerequisites of both
8231 @file{parse.tab.o} and @file{scan.o} will be satisfied. (Presumably
8232 the file @file{parse.tab.o} will be recompiled from @file{parse.tab.c}
8233 and the file @file{scan.o} from @file{scan.c}, while @file{foo} is
8234 linked from @file{parse.tab.o}, @file{scan.o}, and its other
8235 prerequisites, and it will execute happily ever after.)@refill
8237 @node Automatic, Pattern Match, Pattern Examples, Pattern Rules
8238 @subsection Automatic Variables
8239 @cindex automatic variables
8240 @cindex variables, automatic
8241 @cindex variables, and implicit rule
8243 Suppose you are writing a pattern rule to compile a @samp{.c} file into a
8244 @samp{.o} file: how do you write the @samp{cc} command so that it operates
8245 on the right source file name? You cannot write the name in the command,
8246 because the name is different each time the implicit rule is applied.
8248 What you do is use a special feature of @code{make}, the @dfn{automatic
8249 variables}. These variables have values computed afresh for each rule that
8250 is executed, based on the target and prerequisites of the rule. In this
8251 example, you would use @samp{$@@} for the object file name and @samp{$<}
8252 for the source file name.
8254 Here is a table of automatic variables:
8258 @vindex @@ @r{(automatic variable)}
8260 The file name of the target of the rule. If the target is an archive
8261 member, then @samp{$@@} is the name of the archive file. In a pattern
8262 rule that has multiple targets (@pxref{Pattern Intro, ,Introduction to
8263 Pattern Rules}), @samp{$@@} is the name of whichever target caused the
8264 rule's commands to be run.
8267 @vindex % @r{(automatic variable)}
8269 The target member name, when the target is an archive member.
8270 @xref{Archives}. For example, if the target is @file{foo.a(bar.o)} then
8271 @samp{$%} is @file{bar.o} and @samp{$@@} is @file{foo.a}. @samp{$%} is
8272 empty when the target is not an archive member.
8275 @vindex < @r{(automatic variable)}
8277 The name of the first prerequisite. If the target got its commands from
8278 an implicit rule, this will be the first prerequisite added by the
8279 implicit rule (@pxref{Implicit Rules}).
8282 @vindex ? @r{(automatic variable)}
8284 The names of all the prerequisites that are newer than the target, with
8285 spaces between them. For prerequisites which are archive members, only
8286 the member named is used (@pxref{Archives}).
8287 @cindex prerequisites, list of changed
8288 @cindex list of changed prerequisites
8291 @vindex ^ @r{(automatic variable)}
8293 The names of all the prerequisites, with spaces between them. For
8294 prerequisites which are archive members, only the member named is used
8295 (@pxref{Archives}). A target has only one prerequisite on each other file
8296 it depends on, no matter how many times each file is listed as a
8297 prerequisite. So if you list a prerequisite more than once for a target,
8298 the value of @code{$^} contains just one copy of the name.
8299 @cindex prerequisites, list of all
8300 @cindex list of all prerequisites
8303 @vindex + @r{(automatic variable)}
8305 This is like @samp{$^}, but prerequisites listed more than once are
8306 duplicated in the order they were listed in the makefile. This is
8307 primarily useful for use in linking commands where it is meaningful to
8308 repeat library file names in a particular order.
8311 @vindex * @r{(automatic variable)}
8313 The stem with which an implicit rule matches (@pxref{Pattern Match, ,How
8314 Patterns Match}). If the target is @file{dir/a.foo.b} and the target
8315 pattern is @file{a.%.b} then the stem is @file{dir/foo}. The stem is
8316 useful for constructing names of related files.@refill
8317 @cindex stem, variable for
8319 In a static pattern rule, the stem is part of the file name that matched
8320 the @samp{%} in the target pattern.
8322 In an explicit rule, there is no stem; so @samp{$*} cannot be determined
8323 in that way. Instead, if the target name ends with a recognized suffix
8324 (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}), @samp{$*} is set to
8325 the target name minus the suffix. For example, if the target name is
8326 @samp{foo.c}, then @samp{$*} is set to @samp{foo}, since @samp{.c} is a
8327 suffix. GNU @code{make} does this bizarre thing only for compatibility
8328 with other implementations of @code{make}. You should generally avoid
8329 using @samp{$*} except in implicit rules or static pattern rules.@refill
8331 If the target name in an explicit rule does not end with a recognized
8332 suffix, @samp{$*} is set to the empty string for that rule.
8335 @samp{$?} is useful even in explicit rules when you wish to operate on only
8336 the prerequisites that have changed. For example, suppose that an archive
8337 named @file{lib} is supposed to contain copies of several object files.
8338 This rule copies just the changed object files into the archive:
8342 lib: foo.o bar.o lose.o win.o
8347 Of the variables listed above, four have values that are single file
8348 names, and three have values that are lists of file names. These seven
8349 have variants that get just the file's directory name or just the file
8350 name within the directory. The variant variables' names are formed by
8351 appending @samp{D} or @samp{F}, respectively. These variants are
8352 semi-obsolete in GNU @code{make} since the functions @code{dir} and
8353 @code{notdir} can be used to get a similar effect (@pxref{File Name
8354 Functions, , Functions for File Names}). Note, however, that the
8355 @samp{D} variants all omit the trailing slash which always appears in
8356 the output of the @code{dir} function. Here is a table of the variants:
8360 @vindex @@D @r{(automatic variable)}
8362 The directory part of the file name of the target, with the trailing
8363 slash removed. If the value of @samp{$@@} is @file{dir/foo.o} then
8364 @samp{$(@@D)} is @file{dir}. This value is @file{.} if @samp{$@@} does
8365 not contain a slash.
8368 @vindex @@F @r{(automatic variable)}
8370 The file-within-directory part of the file name of the target. If the
8371 value of @samp{$@@} is @file{dir/foo.o} then @samp{$(@@F)} is
8372 @file{foo.o}. @samp{$(@@F)} is equivalent to @samp{$(notdir $@@)}.
8375 @vindex *D @r{(automatic variable)}
8378 @vindex *F @r{(automatic variable)}
8380 The directory part and the file-within-directory
8381 part of the stem; @file{dir} and @file{foo} in this example.
8384 @vindex %D @r{(automatic variable)}
8387 @vindex %F @r{(automatic variable)}
8389 The directory part and the file-within-directory part of the target
8390 archive member name. This makes sense only for archive member targets
8391 of the form @file{@var{archive}(@var{member})} and is useful only when
8392 @var{member} may contain a directory name. (@xref{Archive Members,
8393 ,Archive Members as Targets}.)
8396 @vindex <D @r{(automatic variable)}
8399 @vindex <F @r{(automatic variable)}
8401 The directory part and the file-within-directory
8402 part of the first prerequisite.
8405 @vindex ^D @r{(automatic variable)}
8408 @vindex ^F @r{(automatic variable)}
8410 Lists of the directory parts and the file-within-directory
8411 parts of all prerequisites.
8414 @vindex ?D @r{(automatic variable)}
8417 @vindex ?F @r{(automatic variable)}
8419 Lists of the directory parts and the file-within-directory parts of
8420 all prerequisites that are newer than the target.
8423 Note that we use a special stylistic convention when we talk about these
8424 automatic variables; we write ``the value of @samp{$<}'', rather than
8425 @w{``the variable @code{<}''} as we would write for ordinary variables
8426 such as @code{objects} and @code{CFLAGS}. We think this convention
8427 looks more natural in this special case. Please do not assume it has a
8428 deep significance; @samp{$<} refers to the variable named @code{<} just
8429 as @samp{$(CFLAGS)} refers to the variable named @code{CFLAGS}.
8430 You could just as well use @samp{$(<)} in place of @samp{$<}.
8432 @node Pattern Match, Match-Anything Rules, Automatic, Pattern Rules
8433 @subsection How Patterns Match
8436 A target pattern is composed of a @samp{%} between a prefix and a suffix,
8437 either or both of which may be empty. The pattern matches a file name only
8438 if the file name starts with the prefix and ends with the suffix, without
8439 overlap. The text between the prefix and the suffix is called the
8440 @dfn{stem}. Thus, when the pattern @samp{%.o} matches the file name
8441 @file{test.o}, the stem is @samp{test}. The pattern rule prerequisites are
8442 turned into actual file names by substituting the stem for the character
8443 @samp{%}. Thus, if in the same example one of the prerequisites is written
8444 as @samp{%.c}, it expands to @samp{test.c}.@refill
8446 When the target pattern does not contain a slash (and it usually does
8447 not), directory names in the file names are removed from the file name
8448 before it is compared with the target prefix and suffix. After the
8449 comparison of the file name to the target pattern, the directory
8450 names, along with the slash that ends them, are added on to the
8451 prerequisite file names generated from the pattern rule's prerequisite
8452 patterns and the file name. The directories are ignored only for the
8453 purpose of finding an implicit rule to use, not in the application of
8454 that rule. Thus, @samp{e%t} matches the file name @file{src/eat},
8455 with @samp{src/a} as the stem. When prerequisites are turned into file
8456 names, the directories from the stem are added at the front, while the
8457 rest of the stem is substituted for the @samp{%}. The stem
8458 @samp{src/a} with a prerequisite pattern @samp{c%r} gives the file name
8459 @file{src/car}.@refill
8461 @node Match-Anything Rules, Canceling Rules, Pattern Match, Pattern Rules
8462 @subsection Match-Anything Pattern Rules
8464 @cindex match-anything rule
8465 @cindex terminal rule
8466 When a pattern rule's target is just @samp{%}, it matches any file name
8467 whatever. We call these rules @dfn{match-anything} rules. They are very
8468 useful, but it can take a lot of time for @code{make} to think about them,
8469 because it must consider every such rule for each file name listed either
8470 as a target or as a prerequisite.
8472 Suppose the makefile mentions @file{foo.c}. For this target, @code{make}
8473 would have to consider making it by linking an object file @file{foo.c.o},
8474 or by C compilation-and-linking in one step from @file{foo.c.c}, or by
8475 Pascal compilation-and-linking from @file{foo.c.p}, and many other
8478 We know these possibilities are ridiculous since @file{foo.c} is a C source
8479 file, not an executable. If @code{make} did consider these possibilities,
8480 it would ultimately reject them, because files such as @file{foo.c.o} and
8481 @file{foo.c.p} would not exist. But these possibilities are so
8482 numerous that @code{make} would run very slowly if it had to consider
8485 To gain speed, we have put various constraints on the way @code{make}
8486 considers match-anything rules. There are two different constraints that
8487 can be applied, and each time you define a match-anything rule you must
8488 choose one or the other for that rule.
8490 One choice is to mark the match-anything rule as @dfn{terminal} by defining
8491 it with a double colon. When a rule is terminal, it does not apply unless
8492 its prerequisites actually exist. Prerequisites that could be made with
8493 other implicit rules are not good enough. In other words, no further
8494 chaining is allowed beyond a terminal rule.
8496 For example, the built-in implicit rules for extracting sources from RCS
8497 and SCCS files are terminal; as a result, if the file @file{foo.c,v} does
8498 not exist, @code{make} will not even consider trying to make it as an
8499 intermediate file from @file{foo.c,v.o} or from @file{RCS/SCCS/s.foo.c,v}.
8500 RCS and SCCS files are generally ultimate source files, which should not be
8501 remade from any other files; therefore, @code{make} can save time by not
8502 looking for ways to remake them.@refill
8504 If you do not mark the match-anything rule as terminal, then it is
8505 nonterminal. A nonterminal match-anything rule cannot apply to a file name
8506 that indicates a specific type of data. A file name indicates a specific
8507 type of data if some non-match-anything implicit rule target matches it.
8509 For example, the file name @file{foo.c} matches the target for the pattern
8510 rule @samp{%.c : %.y} (the rule to run Yacc). Regardless of whether this
8511 rule is actually applicable (which happens only if there is a file
8512 @file{foo.y}), the fact that its target matches is enough to prevent
8513 consideration of any nonterminal match-anything rules for the file
8514 @file{foo.c}. Thus, @code{make} will not even consider trying to make
8515 @file{foo.c} as an executable file from @file{foo.c.o}, @file{foo.c.c},
8516 @file{foo.c.p}, etc.@refill
8518 The motivation for this constraint is that nonterminal match-anything
8519 rules are used for making files containing specific types of data (such as
8520 executable files) and a file name with a recognized suffix indicates some
8521 other specific type of data (such as a C source file).
8523 Special built-in dummy pattern rules are provided solely to recognize
8524 certain file names so that nonterminal match-anything rules will not be
8525 considered. These dummy rules have no prerequisites and no commands, and
8526 they are ignored for all other purposes. For example, the built-in
8534 exists to make sure that Pascal source files such as @file{foo.p} match a
8535 specific target pattern and thereby prevent time from being wasted looking
8536 for @file{foo.p.o} or @file{foo.p.c}.
8538 Dummy pattern rules such as the one for @samp{%.p} are made for every
8539 suffix listed as valid for use in suffix rules (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}).
8541 @node Canceling Rules, , Match-Anything Rules, Pattern Rules
8542 @subsection Canceling Implicit Rules
8544 You can override a built-in implicit rule (or one you have defined
8545 yourself) by defining a new pattern rule with the same target and
8546 prerequisites, but different commands. When the new rule is defined, the
8547 built-in one is replaced. The new rule's position in the sequence of
8548 implicit rules is determined by where you write the new rule.
8550 You can cancel a built-in implicit rule by defining a pattern rule with the
8551 same target and prerequisites, but no commands. For example, the following
8552 would cancel the rule that runs the assembler:
8558 @node Last Resort, Suffix Rules, Pattern Rules, Implicit Rules
8559 @section Defining Last-Resort Default Rules
8560 @cindex last-resort default rules
8561 @cindex default rules, last-resort
8563 You can define a last-resort implicit rule by writing a terminal
8564 match-anything pattern rule with no prerequisites (@pxref{Match-Anything
8565 Rules}). This is just like any other pattern rule; the only thing
8566 special about it is that it will match any target. So such a rule's
8567 commands are used for all targets and prerequisites that have no commands
8568 of their own and for which no other implicit rule applies.
8570 For example, when testing a makefile, you might not care if the source
8571 files contain real data, only that they exist. Then you might do this:
8579 to cause all the source files needed (as prerequisites) to be created
8583 You can instead define commands to be used for targets for which there
8584 are no rules at all, even ones which don't specify commands. You do
8585 this by writing a rule for the target @code{.DEFAULT}. Such a rule's
8586 commands are used for all prerequisites which do not appear as targets in
8587 any explicit rule, and for which no implicit rule applies. Naturally,
8588 there is no @code{.DEFAULT} rule unless you write one.
8590 If you use @code{.DEFAULT} with no commands or prerequisites:
8597 the commands previously stored for @code{.DEFAULT} are cleared.
8598 Then @code{make} acts as if you had never defined @code{.DEFAULT} at all.
8600 If you do not want a target to get the commands from a match-anything
8601 pattern rule or @code{.DEFAULT}, but you also do not want any commands
8602 to be run for the target, you can give it empty commands (@pxref{Empty
8603 Commands, ,Defining Empty Commands}).@refill
8605 You can use a last-resort rule to override part of another makefile.
8606 @xref{Overriding Makefiles, , Overriding Part of Another Makefile}.
8608 @node Suffix Rules, Implicit Rule Search, Last Resort, Implicit Rules
8609 @section Old-Fashioned Suffix Rules
8610 @cindex old-fashioned suffix rules
8613 @dfn{Suffix rules} are the old-fashioned way of defining implicit rules for
8614 @code{make}. Suffix rules are obsolete because pattern rules are more
8615 general and clearer. They are supported in GNU @code{make} for
8616 compatibility with old makefiles. They come in two kinds:
8617 @dfn{double-suffix} and @dfn{single-suffix}.@refill
8619 A double-suffix rule is defined by a pair of suffixes: the target suffix
8620 and the source suffix. It matches any file whose name ends with the
8621 target suffix. The corresponding implicit prerequisite is made by
8622 replacing the target suffix with the source suffix in the file name. A
8623 two-suffix rule whose target and source suffixes are @samp{.o} and
8624 @samp{.c} is equivalent to the pattern rule @samp{%.o : %.c}.
8626 A single-suffix rule is defined by a single suffix, which is the source
8627 suffix. It matches any file name, and the corresponding implicit
8628 prerequisite name is made by appending the source suffix. A single-suffix
8629 rule whose source suffix is @samp{.c} is equivalent to the pattern rule
8632 Suffix rule definitions are recognized by comparing each rule's target
8633 against a defined list of known suffixes. When @code{make} sees a rule
8634 whose target is a known suffix, this rule is considered a single-suffix
8635 rule. When @code{make} sees a rule whose target is two known suffixes
8636 concatenated, this rule is taken as a double-suffix rule.
8638 For example, @samp{.c} and @samp{.o} are both on the default list of
8639 known suffixes. Therefore, if you define a rule whose target is
8640 @samp{.c.o}, @code{make} takes it to be a double-suffix rule with source
8641 suffix @samp{.c} and target suffix @samp{.o}. Here is the old-fashioned
8642 way to define the rule for compiling a C source file:@refill
8646 $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
8649 Suffix rules cannot have any prerequisites of their own. If they have any,
8650 they are treated as normal files with funny names, not as suffix rules.
8655 $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
8659 tells how to make the file @file{.c.o} from the prerequisite file
8660 @file{foo.h}, and is not at all like the pattern rule:
8664 $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
8668 which tells how to make @samp{.o} files from @samp{.c} files, and makes all
8669 @samp{.o} files using this pattern rule also depend on @file{foo.h}.
8671 Suffix rules with no commands are also meaningless. They do not remove
8672 previous rules as do pattern rules with no commands (@pxref{Canceling
8673 Rules, , Canceling Implicit Rules}). They simply enter the suffix or pair of suffixes concatenated as
8674 a target in the data base.@refill
8677 The known suffixes are simply the names of the prerequisites of the special
8678 target @code{.SUFFIXES}. You can add your own suffixes by writing a rule
8679 for @code{.SUFFIXES} that adds more prerequisites, as in:
8682 .SUFFIXES: .hack .win
8686 which adds @samp{.hack} and @samp{.win} to the end of the list of suffixes.
8688 If you wish to eliminate the default known suffixes instead of just adding
8689 to them, write a rule for @code{.SUFFIXES} with no prerequisites. By
8690 special dispensation, this eliminates all existing prerequisites of
8691 @code{.SUFFIXES}. You can then write another rule to add the suffixes you
8696 .SUFFIXES: # @r{Delete the default suffixes}
8697 .SUFFIXES: .c .o .h # @r{Define our suffix list}
8701 The @samp{-r} or @samp{--no-builtin-rules} flag causes the default
8702 list of suffixes to be empty.
8705 The variable @code{SUFFIXES} is defined to the default list of suffixes
8706 before @code{make} reads any makefiles. You can change the list of suffixes
8707 with a rule for the special target @code{.SUFFIXES}, but that does not alter
8710 @node Implicit Rule Search, , Suffix Rules, Implicit Rules
8711 @section Implicit Rule Search Algorithm
8712 @cindex implicit rule, search algorithm
8713 @cindex search algorithm, implicit rule
8715 Here is the procedure @code{make} uses for searching for an implicit rule
8716 for a target @var{t}. This procedure is followed for each double-colon
8717 rule with no commands, for each target of ordinary rules none of which have
8718 commands, and for each prerequisite that is not the target of any rule. It
8719 is also followed recursively for prerequisites that come from implicit
8720 rules, in the search for a chain of rules.
8722 Suffix rules are not mentioned in this algorithm because suffix rules are
8723 converted to equivalent pattern rules once the makefiles have been read in.
8725 For an archive member target of the form
8726 @samp{@var{archive}(@var{member})}, the following algorithm is run
8727 twice, first using the entire target name @var{t}, and second using
8728 @samp{(@var{member})} as the target @var{t} if the first run found no
8733 Split @var{t} into a directory part, called @var{d}, and the rest,
8734 called @var{n}. For example, if @var{t} is @samp{src/foo.o}, then
8735 @var{d} is @samp{src/} and @var{n} is @samp{foo.o}.@refill
8738 Make a list of all the pattern rules one of whose targets matches
8739 @var{t} or @var{n}. If the target pattern contains a slash, it is
8740 matched against @var{t}; otherwise, against @var{n}.
8743 If any rule in that list is @emph{not} a match-anything rule, then
8744 remove all nonterminal match-anything rules from the list.
8747 Remove from the list all rules with no commands.
8750 For each pattern rule in the list:
8754 Find the stem @var{s}, which is the nonempty part of @var{t} or @var{n}
8755 matched by the @samp{%} in the target pattern.@refill
8758 Compute the prerequisite names by substituting @var{s} for @samp{%}; if
8759 the target pattern does not contain a slash, append @var{d} to
8760 the front of each prerequisite name.@refill
8763 Test whether all the prerequisites exist or ought to exist. (If a
8764 file name is mentioned in the makefile as a target or as an explicit
8765 prerequisite, then we say it ought to exist.)
8767 If all prerequisites exist or ought to exist, or there are no prerequisites,
8768 then this rule applies.
8772 If no pattern rule has been found so far, try harder.
8773 For each pattern rule in the list:
8777 If the rule is terminal, ignore it and go on to the next rule.
8780 Compute the prerequisite names as before.
8783 Test whether all the prerequisites exist or ought to exist.
8786 For each prerequisite that does not exist, follow this algorithm
8787 recursively to see if the prerequisite can be made by an implicit
8791 If all prerequisites exist, ought to exist, or can be
8792 made by implicit rules, then this rule applies.
8796 If no implicit rule applies, the rule for @code{.DEFAULT}, if any,
8797 applies. In that case, give @var{t} the same commands that
8798 @code{.DEFAULT} has. Otherwise, there are no commands for @var{t}.
8801 Once a rule that applies has been found, for each target pattern of the
8802 rule other than the one that matched @var{t} or @var{n}, the @samp{%} in
8803 the pattern is replaced with @var{s} and the resultant file name is stored
8804 until the commands to remake the target file @var{t} are executed. After
8805 these commands are executed, each of these stored file names are entered
8806 into the data base and marked as having been updated and having the same
8807 update status as the file @var{t}.
8809 When the commands of a pattern rule are executed for @var{t}, the automatic
8810 variables are set corresponding to the target and prerequisites.
8811 @xref{Automatic, ,Automatic Variables}.
8813 @node Archives, Features, Implicit Rules, Top
8814 @chapter Using @code{make} to Update Archive Files
8817 @dfn{Archive files} are files containing named subfiles called
8818 @dfn{members}; they are maintained with the program @code{ar} and their
8819 main use is as subroutine libraries for linking.
8822 * Archive Members:: Archive members as targets.
8823 * Archive Update:: The implicit rule for archive member targets.
8824 * Archive Pitfalls:: Dangers to watch out for when using archives.
8825 * Archive Suffix Rules:: You can write a special kind of suffix rule
8826 for updating archives.
8829 @node Archive Members, Archive Update, Archives, Archives
8830 @section Archive Members as Targets
8831 @cindex archive member targets
8833 An individual member of an archive file can be used as a target or
8834 prerequisite in @code{make}. You specify the member named @var{member} in
8835 archive file @var{archive} as follows:
8838 @var{archive}(@var{member})
8842 This construct is available only in targets and prerequisites, not in
8843 commands! Most programs that you might use in commands do not support this
8844 syntax and cannot act directly on archive members. Only @code{ar} and
8845 other programs specifically designed to operate on archives can do so.
8846 Therefore, valid commands to update an archive member target probably must
8847 use @code{ar}. For example, this rule says to create a member
8848 @file{hack.o} in archive @file{foolib} by copying the file @file{hack.o}:
8851 foolib(hack.o) : hack.o
8855 In fact, nearly all archive member targets are updated in just this way
8856 and there is an implicit rule to do it for you. @strong{Note:} The
8857 @samp{c} flag to @code{ar} is required if the archive file does not
8860 To specify several members in the same archive, you can write all the
8861 member names together between the parentheses. For example:
8864 foolib(hack.o kludge.o)
8871 foolib(hack.o) foolib(kludge.o)
8874 @cindex wildcard, in archive member
8875 You can also use shell-style wildcards in an archive member reference.
8876 @xref{Wildcards, ,Using Wildcard Characters in File Names}. For
8877 example, @w{@samp{foolib(*.o)}} expands to all existing members of the
8878 @file{foolib} archive whose names end in @samp{.o}; perhaps
8879 @samp{@w{foolib(hack.o)} @w{foolib(kludge.o)}}.
8881 @node Archive Update, Archive Pitfalls, Archive Members, Archives
8882 @section Implicit Rule for Archive Member Targets
8884 Recall that a target that looks like @file{@var{a}(@var{m})} stands for the
8885 member named @var{m} in the archive file @var{a}.
8887 When @code{make} looks for an implicit rule for such a target, as a special
8888 feature it considers implicit rules that match @file{(@var{m})}, as well as
8889 those that match the actual target @file{@var{a}(@var{m})}.
8891 This causes one special rule whose target is @file{(%)} to match. This
8892 rule updates the target @file{@var{a}(@var{m})} by copying the file @var{m}
8893 into the archive. For example, it will update the archive member target
8894 @file{foo.a(bar.o)} by copying the @emph{file} @file{bar.o} into the
8895 archive @file{foo.a} as a @emph{member} named @file{bar.o}.
8897 When this rule is chained with others, the result is very powerful.
8898 Thus, @samp{make "foo.a(bar.o)"} (the quotes are needed to protect the
8899 @samp{(} and @samp{)} from being interpreted specially by the shell) in
8900 the presence of a file @file{bar.c} is enough to cause the following
8901 commands to be run, even without a makefile:
8904 cc -c bar.c -o bar.o
8910 Here @code{make} has envisioned the file @file{bar.o} as an intermediate
8911 file. @xref{Chained Rules, ,Chains of Implicit Rules}.
8913 Implicit rules such as this one are written using the automatic variable
8914 @samp{$%}. @xref{Automatic, ,Automatic Variables}.
8916 An archive member name in an archive cannot contain a directory name, but
8917 it may be useful in a makefile to pretend that it does. If you write an
8918 archive member target @file{foo.a(dir/file.o)}, @code{make} will perform
8919 automatic updating with this command:
8922 ar r foo.a dir/file.o
8926 which has the effect of copying the file @file{dir/file.o} into a member
8927 named @file{file.o}. In connection with such usage, the automatic variables
8928 @code{%D} and @code{%F} may be useful.
8931 * Archive Symbols:: How to update archive symbol directories.
8934 @node Archive Symbols, , Archive Update, Archive Update
8935 @subsection Updating Archive Symbol Directories
8936 @cindex @code{__.SYMDEF}
8937 @cindex updating archive symbol directories
8938 @cindex archive symbol directory updating
8939 @cindex symbol directories, updating archive
8940 @cindex directories, updating archive symbol
8942 An archive file that is used as a library usually contains a special member
8943 named @file{__.SYMDEF} that contains a directory of the external symbol
8944 names defined by all the other members. After you update any other
8945 members, you need to update @file{__.SYMDEF} so that it will summarize the
8946 other members properly. This is done by running the @code{ranlib} program:
8949 ranlib @var{archivefile}
8952 Normally you would put this command in the rule for the archive file,
8953 and make all the members of the archive file prerequisites of that rule.
8957 libfoo.a: libfoo.a(x.o) libfoo.a(y.o) @dots{}
8962 The effect of this is to update archive members @file{x.o}, @file{y.o},
8963 etc., and then update the symbol directory member @file{__.SYMDEF} by
8964 running @code{ranlib}. The rules for updating the members are not shown
8965 here; most likely you can omit them and use the implicit rule which copies
8966 files into the archive, as described in the preceding section.
8968 This is not necessary when using the GNU @code{ar} program, which
8969 updates the @file{__.SYMDEF} member automatically.
8971 @node Archive Pitfalls, Archive Suffix Rules, Archive Update, Archives
8972 @section Dangers When Using Archives
8973 @cindex archive, and parallel execution
8974 @cindex parallel execution, and archive update
8975 @cindex archive, and @code{-j}
8976 @cindex @code{-j}, and archive update
8978 It is important to be careful when using parallel execution (the
8979 @code{-j} switch; @pxref{Parallel, ,Parallel Execution}) and archives.
8980 If multiple @code{ar} commands run at the same time on the same archive
8981 file, they will not know about each other and can corrupt the file.
8983 Possibly a future version of @code{make} will provide a mechanism to
8984 circumvent this problem by serializing all commands that operate on the
8985 same archive file. But for the time being, you must either write your
8986 makefiles to avoid this problem in some other way, or not use @code{-j}.
8988 @node Archive Suffix Rules, , Archive Pitfalls, Archives
8989 @section Suffix Rules for Archive Files
8990 @cindex suffix rule, for archive
8991 @cindex archive, suffix rule for
8992 @cindex library archive, suffix rule for
8993 @cindex @code{.a} (archives)
8995 You can write a special kind of suffix rule for dealing with archive
8996 files. @xref{Suffix Rules}, for a full explanation of suffix rules.
8997 Archive suffix rules are obsolete in GNU @code{make}, because pattern
8998 rules for archives are a more general mechanism (@pxref{Archive
8999 Update}). But they are retained for compatibility with other
9002 To write a suffix rule for archives, you simply write a suffix rule
9003 using the target suffix @samp{.a} (the usual suffix for archive files).
9004 For example, here is the old-fashioned suffix rule to update a library
9005 archive from C source files:
9010 $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o
9017 This works just as if you had written the pattern rule:
9022 $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o
9028 In fact, this is just what @code{make} does when it sees a suffix rule
9029 with @samp{.a} as the target suffix. Any double-suffix rule
9030 @w{@samp{.@var{x}.a}} is converted to a pattern rule with the target
9031 pattern @samp{(%.o)} and a prerequisite pattern of @samp{%.@var{x}}.
9033 Since you might want to use @samp{.a} as the suffix for some other kind
9034 of file, @code{make} also converts archive suffix rules to pattern rules
9035 in the normal way (@pxref{Suffix Rules}). Thus a double-suffix rule
9036 @w{@samp{.@var{x}.a}} produces two pattern rules: @samp{@w{(%.o):}
9037 @w{%.@var{x}}} and @samp{@w{%.a}: @w{%.@var{x}}}.@refill
9039 @node Features, Missing, Archives, Top
9040 @chapter Features of GNU @code{make}
9041 @cindex features of GNU @code{make}
9043 @cindex compatibility
9045 Here is a summary of the features of GNU @code{make}, for comparison
9046 with and credit to other versions of @code{make}. We consider the
9047 features of @code{make} in 4.2 BSD systems as a baseline. If you are
9048 concerned with writing portable makefiles, you should not use the
9049 features of @code{make} listed here, nor the ones in @ref{Missing}.
9051 Many features come from the version of @code{make} in System V.
9055 The @code{VPATH} variable and its special meaning.
9056 @xref{Directory Search, , Searching Directories for Prerequisites}.
9057 This feature exists in System V @code{make}, but is undocumented.
9058 It is documented in 4.3 BSD @code{make} (which says it mimics System V's
9059 @code{VPATH} feature).@refill
9062 Included makefiles. @xref{Include, ,Including Other Makefiles}.
9063 Allowing multiple files to be included with a single directive is a GNU
9067 Variables are read from and communicated via the environment.
9068 @xref{Environment, ,Variables from the Environment}.
9071 Options passed through the variable @code{MAKEFLAGS} to recursive
9072 invocations of @code{make}.
9073 @xref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
9076 The automatic variable @code{$%} is set to the member name
9077 in an archive reference. @xref{Automatic, ,Automatic Variables}.
9080 The automatic variables @code{$@@}, @code{$*}, @code{$<}, @code{$%},
9081 and @code{$?} have corresponding forms like @code{$(@@F)} and
9082 @code{$(@@D)}. We have generalized this to @code{$^} as an obvious
9083 extension. @xref{Automatic, ,Automatic Variables}.@refill
9086 Substitution variable references.
9087 @xref{Reference, ,Basics of Variable References}.
9090 The command-line options @samp{-b} and @samp{-m}, accepted and
9091 ignored. In System V @code{make}, these options actually do something.
9094 Execution of recursive commands to run @code{make} via the variable
9095 @code{MAKE} even if @samp{-n}, @samp{-q} or @samp{-t} is specified.
9096 @xref{Recursion, ,Recursive Use of @code{make}}.
9099 Support for suffix @samp{.a} in suffix rules. @xref{Archive Suffix
9100 Rules}. This feature is obsolete in GNU @code{make}, because the
9101 general feature of rule chaining (@pxref{Chained Rules, ,Chains of
9102 Implicit Rules}) allows one pattern rule for installing members in an
9103 archive (@pxref{Archive Update}) to be sufficient.
9106 The arrangement of lines and backslash-newline combinations in
9107 commands is retained when the commands are printed, so they appear as
9108 they do in the makefile, except for the stripping of initial
9112 The following features were inspired by various other versions of
9113 @code{make}. In some cases it is unclear exactly which versions inspired
9118 Pattern rules using @samp{%}.
9119 This has been implemented in several versions of @code{make}.
9120 We're not sure who invented it first, but it's been spread around a bit.
9121 @xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.@refill
9124 Rule chaining and implicit intermediate files.
9125 This was implemented by Stu Feldman in his version of @code{make}
9126 for AT&T Eighth Edition Research Unix, and later by Andrew Hume of
9127 AT&T Bell Labs in his @code{mk} program (where he terms it
9128 ``transitive closure''). We do not really know if
9129 we got this from either of them or thought it up ourselves at the
9130 same time. @xref{Chained Rules, ,Chains of Implicit Rules}.
9133 The automatic variable @code{$^} containing a list of all prerequisites
9134 of the current target. We did not invent this, but we have no idea who
9135 did. @xref{Automatic, ,Automatic Variables}. The automatic variable
9136 @code{$+} is a simple extension of @code{$^}.
9139 The ``what if'' flag (@samp{-W} in GNU @code{make}) was (as far as we know)
9140 invented by Andrew Hume in @code{mk}.
9141 @xref{Instead of Execution, ,Instead of Executing the Commands}.
9144 The concept of doing several things at once (parallelism) exists in
9145 many incarnations of @code{make} and similar programs, though not in the
9146 System V or BSD implementations. @xref{Execution, ,Command Execution}.
9149 Modified variable references using pattern substitution come from
9150 SunOS 4. @xref{Reference, ,Basics of Variable References}.
9151 This functionality was provided in GNU @code{make} by the
9152 @code{patsubst} function before the alternate syntax was implemented
9153 for compatibility with SunOS 4. It is not altogether clear who
9154 inspired whom, since GNU @code{make} had @code{patsubst} before SunOS
9155 4 was released.@refill
9158 The special significance of @samp{+} characters preceding command lines
9159 (@pxref{Instead of Execution, ,Instead of Executing the Commands}) is
9161 @cite{IEEE Standard 1003.2-1992} (POSIX.2).
9164 The @samp{+=} syntax to append to the value of a variable comes from SunOS
9165 4 @code{make}. @xref{Appending, , Appending More Text to Variables}.
9168 The syntax @w{@samp{@var{archive}(@var{mem1} @var{mem2}@dots{})}} to list
9169 multiple members in a single archive file comes from SunOS 4 @code{make}.
9170 @xref{Archive Members}.
9173 The @code{-include} directive to include makefiles with no error for a
9174 nonexistent file comes from SunOS 4 @code{make}. (But note that SunOS 4
9175 @code{make} does not allow multiple makefiles to be specified in one
9176 @code{-include} directive.) The same feature appears with the name
9177 @code{sinclude} in SGI @code{make} and perhaps others.
9180 The remaining features are inventions new in GNU @code{make}:
9184 Use the @samp{-v} or @samp{--version} option to print version and
9185 copyright information.
9188 Use the @samp{-h} or @samp{--help} option to summarize the options to
9192 Simply-expanded variables. @xref{Flavors, ,The Two Flavors of Variables}.
9195 Pass command-line variable assignments automatically through the
9196 variable @code{MAKE} to recursive @code{make} invocations.
9197 @xref{Recursion, ,Recursive Use of @code{make}}.
9200 Use the @samp{-C} or @samp{--directory} command option to change
9201 directory. @xref{Options Summary, ,Summary of Options}.
9204 Make verbatim variable definitions with @code{define}.
9205 @xref{Defining, ,Defining Variables Verbatim}.
9208 Declare phony targets with the special target @code{.PHONY}.
9210 Andrew Hume of AT&T Bell Labs implemented a similar feature with a
9211 different syntax in his @code{mk} program. This seems to be a case of
9212 parallel discovery. @xref{Phony Targets, ,Phony Targets}.
9215 Manipulate text by calling functions.
9216 @xref{Functions, ,Functions for Transforming Text}.
9219 Use the @samp{-o} or @samp{--old-file}
9220 option to pretend a file's modification-time is old.
9221 @xref{Avoiding Compilation, ,Avoiding Recompilation of Some Files}.
9224 Conditional execution.
9226 This feature has been implemented numerous times in various versions
9227 of @code{make}; it seems a natural extension derived from the features
9228 of the C preprocessor and similar macro languages and is not a
9229 revolutionary concept. @xref{Conditionals, ,Conditional Parts of Makefiles}.
9232 Specify a search path for included makefiles.
9233 @xref{Include, ,Including Other Makefiles}.
9236 Specify extra makefiles to read with an environment variable.
9237 @xref{MAKEFILES Variable, ,The Variable @code{MAKEFILES}}.
9240 Strip leading sequences of @samp{./} from file names, so that
9241 @file{./@var{file}} and @file{@var{file}} are considered to be the
9245 Use a special search method for library prerequisites written in the
9246 form @samp{-l@var{name}}.
9247 @xref{Libraries/Search, ,Directory Search for Link Libraries}.
9250 Allow suffixes for suffix rules
9251 (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}) to contain any
9252 characters. In other versions of @code{make}, they must begin with
9253 @samp{.} and not contain any @samp{/} characters.
9256 Keep track of the current level of @code{make} recursion using the
9257 variable @code{MAKELEVEL}. @xref{Recursion, ,Recursive Use of @code{make}}.
9260 Provide any goals given on the command line in the variable
9261 @code{MAKECMDGOALS}. @xref{Goals, ,Arguments to Specify the Goals}.
9264 Specify static pattern rules. @xref{Static Pattern, ,Static Pattern Rules}.
9267 Provide selective @code{vpath} search.
9268 @xref{Directory Search, ,Searching Directories for Prerequisites}.
9271 Provide computed variable references.
9272 @xref{Reference, ,Basics of Variable References}.
9275 Update makefiles. @xref{Remaking Makefiles, ,How Makefiles Are Remade}.
9276 System V @code{make} has a very, very limited form of this
9277 functionality in that it will check out SCCS files for makefiles.
9280 Various new built-in implicit rules.
9281 @xref{Catalogue of Rules, ,Catalogue of Implicit Rules}.
9284 The built-in variable @samp{MAKE_VERSION} gives the version number of
9288 @node Missing, Makefile Conventions, Features, Top
9289 @chapter Incompatibilities and Missing Features
9290 @cindex incompatibilities
9291 @cindex missing features
9292 @cindex features, missing
9294 The @code{make} programs in various other systems support a few features
9295 that are not implemented in GNU @code{make}. The POSIX.2 standard
9296 (@cite{IEEE Standard 1003.2-1992}) which specifies @code{make} does not
9297 require any of these features.@refill
9301 A target of the form @samp{@var{file}((@var{entry}))} stands for a member
9302 of archive file @var{file}. The member is chosen, not by name, but by
9303 being an object file which defines the linker symbol @var{entry}.@refill
9305 This feature was not put into GNU @code{make} because of the
9306 nonmodularity of putting knowledge into @code{make} of the internal
9307 format of archive file symbol tables.
9308 @xref{Archive Symbols, ,Updating Archive Symbol Directories}.
9311 Suffixes (used in suffix rules) that end with the character @samp{~}
9312 have a special meaning to System V @code{make};
9313 they refer to the SCCS file that corresponds
9314 to the file one would get without the @samp{~}. For example, the
9315 suffix rule @samp{.c~.o} would make the file @file{@var{n}.o} from
9316 the SCCS file @file{s.@var{n}.c}. For complete coverage, a whole
9317 series of such suffix rules is required.
9318 @xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
9320 In GNU @code{make}, this entire series of cases is handled by two
9321 pattern rules for extraction from SCCS, in combination with the
9322 general feature of rule chaining.
9323 @xref{Chained Rules, ,Chains of Implicit Rules}.
9326 In System V @code{make}, the string @samp{$$@@} has the strange meaning
9327 that, in the prerequisites of a rule with multiple targets, it stands
9328 for the particular target that is being processed.
9330 This is not defined in GNU @code{make} because @samp{$$} should always
9331 stand for an ordinary @samp{$}.
9333 It is possible to get portions of this functionality through the use of
9334 static pattern rules (@pxref{Static Pattern, ,Static Pattern Rules}).
9335 The System V @code{make} rule:
9338 $(targets): $$@@.o lib.a
9342 can be replaced with the GNU @code{make} static pattern rule:
9345 $(targets): %: %.o lib.a
9349 In System V and 4.3 BSD @code{make}, files found by @code{VPATH} search
9350 (@pxref{Directory Search, ,Searching Directories for Prerequisites}) have their names changed inside command
9351 strings. We feel it is much cleaner to always use automatic variables
9352 and thus make this feature obsolete.@refill
9355 In some Unix @code{make}s, the automatic variable @code{$*} appearing in
9356 the prerequisites of a rule has the amazingly strange ``feature'' of
9357 expanding to the full name of the @emph{target of that rule}. We cannot
9358 imagine what went on in the minds of Unix @code{make} developers to do
9359 this; it is utterly inconsistent with the normal definition of @code{$*}.
9360 @vindex * @r{(automatic variable), unsupported bizarre usage}
9363 In some Unix @code{make}s, implicit rule search
9364 (@pxref{Implicit Rules, ,Using Implicit Rules}) is apparently done for
9365 @emph{all} targets, not just those without commands. This means you can
9376 and Unix @code{make} will intuit that @file{foo.o} depends on
9377 @file{foo.c}.@refill
9379 We feel that such usage is broken. The prerequisite properties of
9380 @code{make} are well-defined (for GNU @code{make}, at least),
9381 and doing such a thing simply does not fit the model.@refill
9384 GNU @code{make} does not include any built-in implicit rules for
9385 compiling or preprocessing EFL programs. If we hear of anyone who is
9386 using EFL, we will gladly add them.
9389 It appears that in SVR4 @code{make}, a suffix rule can be specified with
9390 no commands, and it is treated as if it had empty commands
9391 (@pxref{Empty Commands}). For example:
9398 will override the built-in @file{.c.a} suffix rule.
9400 We feel that it is cleaner for a rule without commands to always simply
9401 add to the prerequisite list for the target. The above example can be
9402 easily rewritten to get the desired behavior in GNU @code{make}:
9409 Some versions of @code{make} invoke the shell with the @samp{-e} flag,
9410 except under @samp{-k} (@pxref{Testing, ,Testing the Compilation of a
9411 Program}). The @samp{-e} flag tells the shell to exit as soon as any
9412 program it runs returns a nonzero status. We feel it is cleaner to
9413 write each shell command line to stand on its own and not require this
9417 @comment The makefile standards are in a separate file that is also
9418 @comment included by standards.texi.
9419 @include make-stds.texi
9421 @node Quick Reference, Error Messages, Makefile Conventions, Top
9422 @appendix Quick Reference
9424 This appendix summarizes the directives, text manipulation functions,
9425 and special variables which GNU @code{make} understands.
9426 @xref{Special Targets}, @ref{Catalogue of Rules, ,Catalogue of Implicit Rules},
9427 and @ref{Options Summary, ,Summary of Options},
9428 for other summaries.
9430 Here is a summary of the directives GNU @code{make} recognizes:
9433 @item define @var{variable}
9436 Define a multi-line, recursively-expanded variable.@*
9439 @item ifdef @var{variable}
9440 @itemx ifndef @var{variable}
9441 @itemx ifeq (@var{a},@var{b})
9442 @itemx ifeq "@var{a}" "@var{b}"
9443 @itemx ifeq '@var{a}' '@var{b}'
9444 @itemx ifneq (@var{a},@var{b})
9445 @itemx ifneq "@var{a}" "@var{b}"
9446 @itemx ifneq '@var{a}' '@var{b}'
9450 Conditionally evaluate part of the makefile.@*
9451 @xref{Conditionals}.
9453 @item include @var{file}
9454 @itemx -include @var{file}
9455 @itemx sinclude @var{file}
9457 Include another makefile.@*
9458 @xref{Include, ,Including Other Makefiles}.
9460 @item override @var{variable} = @var{value}
9461 @itemx override @var{variable} := @var{value}
9462 @itemx override @var{variable} += @var{value}
9463 @itemx override @var{variable} ?= @var{value}
9464 @itemx override define @var{variable}
9467 Define a variable, overriding any previous definition, even one from
9469 @xref{Override Directive, ,The @code{override} Directive}.
9473 Tell @code{make} to export all variables to child processes by default.@*
9474 @xref{Variables/Recursion, , Communicating Variables to a Sub-@code{make}}.
9476 @item export @var{variable}
9477 @itemx export @var{variable} = @var{value}
9478 @itemx export @var{variable} := @var{value}
9479 @itemx export @var{variable} += @var{value}
9480 @itemx export @var{variable} ?= @var{value}
9481 @itemx unexport @var{variable}
9482 Tell @code{make} whether or not to export a particular variable to child
9484 @xref{Variables/Recursion, , Communicating Variables to a Sub-@code{make}}.
9486 @item vpath @var{pattern} @var{path}
9487 Specify a search path for files matching a @samp{%} pattern.@*
9488 @xref{Selective Search, , The @code{vpath} Directive}.
9490 @item vpath @var{pattern}
9491 Remove all search paths previously specified for @var{pattern}.
9494 Remove all search paths previously specified in any @code{vpath}
9498 Here is a summary of the text manipulation functions (@pxref{Functions}):
9501 @item $(subst @var{from},@var{to},@var{text})
9502 Replace @var{from} with @var{to} in @var{text}.@*
9503 @xref{Text Functions, , Functions for String Substitution and Analysis}.
9505 @item $(patsubst @var{pattern},@var{replacement},@var{text})
9506 Replace words matching @var{pattern} with @var{replacement} in @var{text}.@*
9507 @xref{Text Functions, , Functions for String Substitution and Analysis}.
9509 @item $(strip @var{string})
9510 Remove excess whitespace characters from @var{string}.@*
9511 @xref{Text Functions, , Functions for String Substitution and Analysis}.
9513 @item $(findstring @var{find},@var{text})
9514 Locate @var{find} in @var{text}.@*
9515 @xref{Text Functions, , Functions for String Substitution and Analysis}.
9517 @item $(filter @var{pattern}@dots{},@var{text})
9518 Select words in @var{text} that match one of the @var{pattern} words.@*
9519 @xref{Text Functions, , Functions for String Substitution and Analysis}.
9521 @item $(filter-out @var{pattern}@dots{},@var{text})
9522 Select words in @var{text} that @emph{do not} match any of the @var{pattern} words.@*
9523 @xref{Text Functions, , Functions for String Substitution and Analysis}.
9525 @item $(sort @var{list})
9526 Sort the words in @var{list} lexicographically, removing duplicates.@*
9527 @xref{Text Functions, , Functions for String Substitution and Analysis}.
9529 @item $(dir @var{names}@dots{})
9530 Extract the directory part of each file name.@*
9531 @xref{File Name Functions, ,Functions for File Names}.
9533 @item $(notdir @var{names}@dots{})
9534 Extract the non-directory part of each file name.@*
9535 @xref{File Name Functions, ,Functions for File Names}.
9537 @item $(suffix @var{names}@dots{})
9538 Extract the suffix (the last @samp{.} and following characters) of each file name.@*
9539 @xref{File Name Functions, ,Functions for File Names}.
9541 @item $(basename @var{names}@dots{})
9542 Extract the base name (name without suffix) of each file name.@*
9543 @xref{File Name Functions, ,Functions for File Names}.
9545 @item $(addsuffix @var{suffix},@var{names}@dots{})
9546 Append @var{suffix} to each word in @var{names}.@*
9547 @xref{File Name Functions, ,Functions for File Names}.
9549 @item $(addprefix @var{prefix},@var{names}@dots{})
9550 Prepend @var{prefix} to each word in @var{names}.@*
9551 @xref{File Name Functions, ,Functions for File Names}.
9553 @item $(join @var{list1},@var{list2})
9554 Join two parallel lists of words.@*
9555 @xref{File Name Functions, ,Functions for File Names}.
9557 @item $(word @var{n},@var{text})
9558 Extract the @var{n}th word (one-origin) of @var{text}.@*
9559 @xref{File Name Functions, ,Functions for File Names}.
9561 @item $(words @var{text})
9562 Count the number of words in @var{text}.@*
9563 @xref{File Name Functions, ,Functions for File Names}.
9565 @item $(wordlist @var{s},@var{e},@var{text})
9566 Returns the list of words in @var{text} from @var{s} to @var{e}.@*
9567 @xref{File Name Functions, ,Functions for File Names}.
9569 @item $(firstword @var{names}@dots{})
9570 Extract the first word of @var{names}.@*
9571 @xref{File Name Functions, ,Functions for File Names}.
9573 @item $(wildcard @var{pattern}@dots{})
9574 Find file names matching a shell file name pattern (@emph{not} a
9575 @samp{%} pattern).@*
9576 @xref{Wildcard Function, ,The Function @code{wildcard}}.
9578 @item $(error @var{text}@dots{})
9580 When this function is evaluated, @code{make} generates a fatal error
9581 with the message @var{text}.@*
9582 @xref{Make Control Functions, ,Functions That Control Make}.
9584 @item $(warning @var{text}@dots{})
9586 When this function is evaluated, @code{make} generates a warning with
9587 the message @var{text}.@*
9588 @xref{Make Control Functions, ,Functions That Control Make}.
9590 @item $(shell @var{command})
9592 Execute a shell command and return its output.@*
9593 @xref{Shell Function, , The @code{shell} Function}.
9595 @item $(origin @var{variable})
9597 Return a string describing how the @code{make} variable @var{variable} was
9599 @xref{Origin Function, , The @code{origin} Function}.
9601 @item $(foreach @var{var},@var{words},@var{text})
9603 Evaluate @var{text} with @var{var} bound to each word in @var{words},
9604 and concatenate the results.@*
9605 @xref{Foreach Function, ,The @code{foreach} Function}.
9607 @item $(call @var{var},@var{param},@dots{})
9609 Evaluate the variable @var{var} replacing any references to @code{$(1)},
9610 @code{$(2)} with the first, second, etc. @var{param} values.@*
9611 @xref{Call Function, ,The @code{call} Function}.
9614 Here is a summary of the automatic variables.
9615 @xref{Automatic, ,Automatic Variables},
9616 for full information.
9620 The file name of the target.
9623 The target member name, when the target is an archive member.
9626 The name of the first prerequisite.
9629 The names of all the prerequisites that are
9630 newer than the target, with spaces between them.
9631 For prerequisites which are archive members, only
9632 the member named is used (@pxref{Archives}).
9636 The names of all the prerequisites, with spaces between them. For
9637 prerequisites which are archive members, only the member named is used
9638 (@pxref{Archives}). The value of @code{$^} omits duplicate
9639 prerequisites, while @code{$+} retains them and preserves their order.
9642 The stem with which an implicit rule matches
9643 (@pxref{Pattern Match, ,How Patterns Match}).
9647 The directory part and the file-within-directory part of @code{$@@}.
9651 The directory part and the file-within-directory part of @code{$*}.
9655 The directory part and the file-within-directory part of @code{$%}.
9659 The directory part and the file-within-directory part of @code{$<}.
9663 The directory part and the file-within-directory part of @code{$^}.
9667 The directory part and the file-within-directory part of @code{$+}.
9671 The directory part and the file-within-directory part of @code{$?}.
9674 These variables are used specially by GNU @code{make}:
9679 Makefiles to be read on every invocation of @code{make}.@*
9680 @xref{MAKEFILES Variable, ,The Variable @code{MAKEFILES}}.
9684 Directory search path for files not found in the current directory.@*
9685 @xref{General Search, , @code{VPATH} Search Path for All Prerequisites}.
9689 The name of the system default command interpreter, usually @file{/bin/sh}.
9690 You can set @code{SHELL} in the makefile to change the shell used to run
9691 commands. @xref{Execution, ,Command Execution}.
9695 On MS-DOS only, the name of the command interpreter that is to be used
9696 by @code{make}. This value takes precedence over the value of
9697 @code{SHELL}. @xref{Execution, ,MAKESHELL variable}.
9701 The name with which @code{make} was invoked.
9702 Using this variable in commands has special meaning.
9703 @xref{MAKE Variable, ,How the @code{MAKE} Variable Works}.
9707 The number of levels of recursion (sub-@code{make}s).@*
9708 @xref{Variables/Recursion}.
9712 The flags given to @code{make}. You can set this in the environment or
9713 a makefile to set flags.@*
9714 @xref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
9716 It is @emph{never} appropriate to use @code{MAKEFLAGS} directly on a
9717 command line: its contents may not be quoted correctly for use in the
9718 shell. Always allow recursive @code{make}'s to obtain these values
9719 through the environment from its parent.
9723 The targets given to @code{make} on the command line. Setting this
9724 variable has no effect on the operation of @code{make}.@*
9725 @xref{Goals, ,Arguments to Specify the Goals}.
9729 Set to the pathname of the current working directory (after all
9730 @code{-C} options are processed, if any). Setting this variable has no
9731 effect on the operation of @code{make}.@*
9732 @xref{Recursion, ,Recursive Use of @code{make}}.
9736 The default list of suffixes before @code{make} reads any makefiles.
9739 Defines the naming of the libraries @code{make} searches for, and their
9741 @xref{Libraries/Search, ,Directory Search for Link Libraries}.
9744 @node Error Messages, Complex Makefile, Quick Reference, Top
9745 @comment node-name, next, previous, up
9746 @appendix Errors Generated by Make
9748 Here is a list of the more common errors you might see generated by
9749 @code{make}, and some information about what they mean and how to fix
9752 Sometimes @code{make} errors are not fatal, especially in the presence
9753 of a @code{-} prefix on a command script line, or the @code{-k} command
9754 line option. Errors that are fatal are prefixed with the string
9757 Error messages are all either prefixed with the name of the program
9758 (usually @samp{make}), or, if the error is found in a makefile, the name
9759 of the file and linenumber containing the problem.
9761 In the table below, these common prefixes are left off.
9765 @item [@var{foo}] Error @var{NN}
9766 @itemx [@var{foo}] @var{signal description}
9767 These errors are not really @code{make} errors at all. They mean that a
9768 program that @code{make} invoked as part of a command script returned a
9769 non-0 error code (@samp{Error @var{NN}}), which @code{make} interprets
9770 as failure, or it exited in some other abnormal fashion (with a
9771 signal of some type). @xref{Errors, ,Errors in Commands}.
9773 If no @code{***} is attached to the message, then the subprocess failed
9774 but the rule in the makefile was prefixed with the @code{-} special
9775 character, so @code{make} ignored the error.
9777 @item missing separator. Stop.
9778 @itemx missing separator (did you mean TAB instead of 8 spaces?). Stop.
9779 This means that @code{make} could not understand much of anything about
9780 the command line it just read. GNU @code{make} looks for various kinds
9781 of separators (@code{:}, @code{=}, TAB characters, etc.) to help it
9782 decide what kind of commandline it's seeing. This means it couldn't
9785 One of the most common reasons for this message is that you (or perhaps
9786 your oh-so-helpful editor, as is the case with many MS-Windows editors)
9787 have attempted to indent your command scripts with spaces instead of a
9788 TAB character. In this case, @code{make} will use the second form of
9789 the error above. Remember that every line in the command script must
9790 begin with a TAB character. Eight spaces do not count. @xref{Rule
9793 @item commands commence before first target. Stop.
9794 @itemx missing rule before commands. Stop.
9795 This means the first thing in the makefile seems to be part of a command
9796 script: it begins with a TAB character and doesn't appear to be a legal
9797 @code{make} command (such as a variable assignment). Command scripts
9798 must always be associated with a target.
9800 The second form is generated if the line has a semicolon as the first
9801 non-whitespace character; @code{make} interprets this to mean you left
9802 out the "target: prerequisite" section of a rule. @xref{Rule Syntax}.
9804 @item No rule to make target `@var{xxx}'.
9805 @itemx No rule to make target `@var{xxx}', needed by `@var{yyy}'.
9806 This means that @code{make} decided it needed to build a target, but
9807 then couldn't find any instructions in the makefile on how to do that,
9808 either explicit or implicit (including in the default rules database).
9810 If you want that file to be built, you will need to add a rule to your
9811 makefile describing how that target can be built. Other possible
9812 sources of this problem are typos in the makefile (if that filename is
9813 wrong) or a corrupted source tree (if that file is not supposed to be
9814 built, but rather only a prerequisite).
9816 @item No targets specified and no makefile found. Stop.
9817 @itemx No targets. Stop.
9818 The former means that you didn't provide any targets to be built on the
9819 command line, and @code{make} couldn't find any makefiles to read in.
9820 The latter means that some makefile was found, but it didn't contain any
9821 default target and none was given on the command line. GNU @code{make}
9822 has nothing to do in these situations.
9823 @xref{Makefile Arguments, ,Arguments to Specify the Makefile}.@refill
9825 @item Makefile `@var{xxx}' was not found.
9826 @itemx Included makefile `@var{xxx}' was not found.
9827 A makefile specified on the command line (first form) or included
9828 (second form) was not found.
9830 @item warning: overriding commands for target `@var{xxx}'
9831 @itemx warning: ignoring old commands for target `@var{xxx}'
9832 GNU @code{make} allows commands to be specified only once per target
9833 (except for double-colon rules). If you give commands for a target
9834 which already has been defined to have commands, this warning is issued
9835 and the second set of commands will overwrite the first set.
9836 @xref{Multiple Rules, ,Multiple Rules for One Target}.
9838 @item Circular @var{xxx} <- @var{yyy} dependency dropped.
9839 This means that @code{make} detected a loop in the dependency graph:
9840 after tracing the prerequisite @var{yyy} of target @var{xxx}, and its
9841 prerequisites, etc., one of them depended on @var{xxx} again.
9843 @item Recursive variable `@var{xxx}' references itself (eventually). Stop.
9844 This means you've defined a normal (recursive) @code{make} variable
9845 @var{xxx} that, when it's expanded, will refer to itself (@var{xxx}).
9846 This is not allowed; either use simply-expanded variables (@code{:=}) or
9847 use the append operator (@code{+=}). @xref{Using Variables, ,How to Use
9850 @item Unterminated variable reference. Stop.
9851 This means you forgot to provide the proper closing parenthesis
9852 or brace in your variable or function reference.
9854 @item insufficient arguments to function `@var{xxx}'. Stop.
9855 This means you haven't provided the requisite number of arguments for
9856 this function. See the documentation of the function for a description
9857 of its arguments. @xref{Functions, ,Functions for Transforming Text}.
9859 @item missing target pattern. Stop.
9860 @itemx multiple target patterns. Stop.
9861 @itemx target pattern contains no `%'. Stop.
9862 @itemx mixed implicit and static pattern rules. Stop.
9863 These are generated for malformed static pattern rules. The first means
9864 there's no pattern in the target section of the rule; the second means
9865 there are multiple patterns in the target section; the third means
9866 the target doesn't contain a pattern character (@code{%}); and the
9867 fourth means that all three parts of the static pattern rule contain
9868 pattern characters (@code{%})--only the first two parts should.
9869 @xref{Static Usage, ,Syntax of Static Pattern Rules}.
9871 @item warning: -jN forced in submake: disabling jobserver mode.
9872 This warning and the next are generated if @code{make} detects error
9873 conditions related to parallel processing on systems where
9874 sub-@code{make}s can communicate (@pxref{Options/Recursion,
9875 ,Communicating Options to a Sub-@code{make}}). This warning is
9876 generated if a recursive invocation of a @code{make} process is forced
9877 to have @samp{-j@var{N}} in its argument list (where @var{N} is greater
9878 than one). This could happen, for example, if you set the @code{MAKE}
9879 environment variable to @samp{make -j2}. In this case, the
9880 sub-@code{make} doesn't communicate with other @code{make} processes and
9881 will simply pretend it has two jobs of its own.
9883 @item warning: jobserver unavailable: using -j1. Add `+' to parent make rule.
9884 In order for @code{make} processes to communicate, the parent will pass
9885 information to the child. Since this could result in problems if the
9886 child process isn't actually a @code{make}, the parent will only do this
9887 if it thinks the child is a @code{make}. The parent uses the normal
9888 algorithms to determine this (@pxref{MAKE Variable, ,How the @code{MAKE}
9889 Variable Works}). If the makefile is constructed such that the parent
9890 doesn't know the child is a @code{make} process, then the child will
9891 receive only part of the information necessary. In this case, the child
9892 will generate this warning message and proceed with its build in a
9897 @node Complex Makefile, GNU Free Documentation License, Error Messages, Top
9898 @appendix Complex Makefile Example
9900 Here is the makefile for the GNU @code{tar} program. This is a
9901 moderately complex makefile.
9903 Because it is the first target, the default goal is @samp{all}. An
9904 interesting feature of this makefile is that @file{testpad.h} is a
9905 source file automatically created by the @code{testpad} program,
9906 itself compiled from @file{testpad.c}.
9908 If you type @samp{make} or @samp{make all}, then @code{make} creates
9909 the @file{tar} executable, the @file{rmt} daemon that provides
9910 remote tape access, and the @file{tar.info} Info file.
9912 If you type @samp{make install}, then @code{make} not only creates
9913 @file{tar}, @file{rmt}, and @file{tar.info}, but also installs
9916 If you type @samp{make clean}, then @code{make} removes the @samp{.o}
9917 files, and the @file{tar}, @file{rmt}, @file{testpad},
9918 @file{testpad.h}, and @file{core} files.
9920 If you type @samp{make distclean}, then @code{make} not only removes
9921 the same files as does @samp{make clean} but also the
9922 @file{TAGS}, @file{Makefile}, and @file{config.status} files.
9923 (Although it is not evident, this makefile (and
9924 @file{config.status}) is generated by the user with the
9925 @code{configure} program, which is provided in the @code{tar}
9926 distribution, but is not shown here.)
9928 If you type @samp{make realclean}, then @code{make} removes the same
9929 files as does @samp{make distclean} and also removes the Info files
9930 generated from @file{tar.texinfo}.
9932 In addition, there are targets @code{shar} and @code{dist} that create
9937 # Generated automatically from Makefile.in by configure.
9938 # Un*x Makefile for GNU tar program.
9939 # Copyright (C) 1991 Free Software Foundation, Inc.
9943 # This program is free software; you can redistribute
9944 # it and/or modify it under the terms of the GNU
9945 # General Public License @dots{}
9952 #### Start of system configuration section. ####
9957 # If you use gcc, you should either run the
9958 # fixincludes script that comes with it or else use
9959 # gcc with the -traditional option. Otherwise ioctl
9960 # calls will be compiled incorrectly on some systems.
9963 INSTALL = /usr/local/bin/install -c
9964 INSTALLDATA = /usr/local/bin/install -c -m 644
9967 # Things you might add to DEFS:
9968 # -DSTDC_HEADERS If you have ANSI C headers and
9970 # -DPOSIX If you have POSIX.1 headers and
9972 # -DBSD42 If you have sys/dir.h (unless
9973 # you use -DPOSIX), sys/file.h,
9974 # and st_blocks in `struct stat'.
9975 # -DUSG If you have System V/ANSI C
9976 # string and memory functions
9977 # and headers, sys/sysmacros.h,
9978 # fcntl.h, getcwd, no valloc,
9979 # and ndir.h (unless
9980 # you use -DDIRENT).
9981 # -DNO_MEMORY_H If USG or STDC_HEADERS but do not
9983 # -DDIRENT If USG and you have dirent.h
9984 # instead of ndir.h.
9985 # -DSIGTYPE=int If your signal handlers
9986 # return int, not void.
9987 # -DNO_MTIO If you lack sys/mtio.h
9989 # -DNO_REMOTE If you do not have a remote shell
9991 # -DUSE_REXEC To use rexec for remote tape
9992 # operations instead of
9993 # forking rsh or remsh.
9994 # -DVPRINTF_MISSING If you lack vprintf function
9995 # (but have _doprnt).
9996 # -DDOPRNT_MISSING If you lack _doprnt function.
9997 # Also need to define
9998 # -DVPRINTF_MISSING.
9999 # -DFTIME_MISSING If you lack ftime system call.
10000 # -DSTRSTR_MISSING If you lack strstr function.
10001 # -DVALLOC_MISSING If you lack valloc function.
10002 # -DMKDIR_MISSING If you lack mkdir and
10003 # rmdir system calls.
10004 # -DRENAME_MISSING If you lack rename system call.
10005 # -DFTRUNCATE_MISSING If you lack ftruncate
10007 # -DV7 On Version 7 Unix (not
10008 # tested in a long time).
10009 # -DEMUL_OPEN3 If you lack a 3-argument version
10010 # of open, and want to emulate it
10011 # with system calls you do have.
10012 # -DNO_OPEN3 If you lack the 3-argument open
10013 # and want to disable the tar -k
10014 # option instead of emulating open.
10015 # -DXENIX If you have sys/inode.h
10016 # and need it 94 to be included.
10018 DEFS = -DSIGTYPE=int -DDIRENT -DSTRSTR_MISSING \
10019 -DVPRINTF_MISSING -DBSD42
10020 # Set this to rtapelib.o unless you defined NO_REMOTE,
10021 # in which case make it empty.
10022 RTAPELIB = rtapelib.o
10024 DEF_AR_FILE = /dev/rmt8
10029 CFLAGS = $(CDEBUG) -I. -I$(srcdir) $(DEFS) \
10030 -DDEF_AR_FILE=\"$(DEF_AR_FILE)\" \
10031 -DDEFBLOCKING=$(DEFBLOCKING)
10036 prefix = /usr/local
10037 # Prefix for each installed program,
10038 # normally empty or `g'.
10041 # The directory to install tar in.
10042 bindir = $(prefix)/bin
10044 # The directory to install the info files in.
10045 infodir = $(prefix)/info
10048 #### End of system configuration section. ####
10050 SRC1 = tar.c create.c extract.c buffer.c \
10051 getoldopt.c update.c gnu.c mangle.c
10052 SRC2 = version.c list.c names.c diffarch.c \
10053 port.c wildmat.c getopt.c
10054 SRC3 = getopt1.c regex.c getdate.y
10055 SRCS = $(SRC1) $(SRC2) $(SRC3)
10056 OBJ1 = tar.o create.o extract.o buffer.o \
10057 getoldopt.o update.o gnu.o mangle.o
10058 OBJ2 = version.o list.o names.o diffarch.o \
10059 port.o wildmat.o getopt.o
10060 OBJ3 = getopt1.o regex.o getdate.o $(RTAPELIB)
10061 OBJS = $(OBJ1) $(OBJ2) $(OBJ3)
10063 AUX = README COPYING ChangeLog Makefile.in \
10064 makefile.pc configure configure.in \
10065 tar.texinfo tar.info* texinfo.tex \
10066 tar.h port.h open3.h getopt.h regex.h \
10067 rmt.h rmt.c rtapelib.c alloca.c \
10068 msd_dir.h msd_dir.c tcexparg.c \
10069 level-0 level-1 backup-specs testpad.c
10072 all: tar rmt tar.info
10076 $(CC) $(LDFLAGS) -o $@@ $(OBJS) $(LIBS)
10081 $(CC) $(CFLAGS) $(LDFLAGS) -o $@@ rmt.c
10085 tar.info: tar.texinfo
10086 makeinfo tar.texinfo
10091 $(INSTALL) tar $(bindir)/$(binprefix)tar
10092 -test ! -f rmt || $(INSTALL) rmt /etc/rmt
10093 $(INSTALLDATA) $(srcdir)/tar.info* $(infodir)
10097 $(OBJS): tar.h port.h testpad.h
10098 regex.o buffer.o tar.o: regex.h
10099 # getdate.y has 8 shift/reduce conflicts.
10109 $(CC) -o $@@ testpad.o
10119 rm -f *.o tar rmt testpad testpad.h core
10124 rm -f TAGS Makefile config.status
10128 realclean: distclean
10133 shar: $(SRCS) $(AUX)
10134 shar $(SRCS) $(AUX) | compress \
10135 > tar-`sed -e '/version_string/!d' \
10136 -e 's/[^0-9.]*\([0-9.]*\).*/\1/' \
10142 dist: $(SRCS) $(AUX)
10144 -e '/version_string/!d' \
10145 -e 's/[^0-9.]*\([0-9.]*\).*/\1/' \
10147 version.c` > .fname
10148 -rm -rf `cat .fname`
10150 ln $(SRCS) $(AUX) `cat .fname`
10151 tar chZf `cat .fname`.tar.Z `cat .fname`
10152 -rm -rf `cat .fname` .fname
10156 tar.zoo: $(SRCS) $(AUX)
10160 for X in $(SRCS) $(AUX) ; do \
10162 sed 's/$$/^M/' $$X \
10163 > tmp.dir/$$X ; done
10164 cd tmp.dir ; zoo aM ../tar.zoo *
10173 @node Concept Index, Name Index, GNU Free Documentation License, Top
10174 @unnumbered Index of Concepts
10178 @node Name Index, , Concept Index, Top
10179 @unnumbered Index of Functions, Variables, & Directives