VirtualBox

source: kBuild/vendor/gnumake/2005-05-16/doc/make.texi

Last change on this file was 280, checked in by bird, 20 years ago

Current make snaphot, 2005-05-16.

  • Property svn:eol-style set to native
File size: 399.0 KB
Line 
1\input texinfo @c -*- Texinfo -*-
2@c %**start of header
3@setfilename make.info
4@settitle GNU @code{make}
5@setchapternewpage odd
6@c %**end of header
7
8@c FSF publishers: format makebook.texi instead of using this file directly.
9
10@set RCSID $Id: make.texi,v 1.30 2005/05/13 12:45:31 psmith Exp $
11@set EDITION 0.70
12@set VERSION 3.81
13@set UPDATED 07 May 2005
14@set UPDATE-MONTH May 2005
15@c ISBN provided by Lisa M. Opus Goldstein <opus@gnu.org>, 5 May 2004
16@set ISBN 1-882114-83-5
17
18@c finalout
19
20@c ISPELL CHECK: done, 10 June 1993 --roland
21@c ISPELL CHECK: done, 2000-06-25 --Martin Buchholz
22
23@c Combine the variable and function indices:
24@syncodeindex vr fn
25@c Combine the program and concept indices:
26@syncodeindex pg cp
27
28@dircategory GNU Packages
29@direntry
30* Make: (make). Remake files automatically.
31@end direntry
32
33@ifnottex
34This file documents the GNU Make utility, which determines
35automatically which pieces of a large program need to be recompiled,
36and issues the commands to recompile them.
37
38This is Edition @value{EDITION}, last updated @value{UPDATED},
39of @cite{The GNU Make Manual}, for @code{make}, Version @value{VERSION}.
40
41Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
421998, 1999, 2000, 2002, 2003, 2004, 2005
43Free Software Foundation, Inc.
44
45Permission is granted to copy, distribute and/or modify this document
46under the terms of the GNU Free Documentation License, Version 1.1 or
47any later version published by the Free Software Foundation; with no
48Invariant Sections, with no Front-Cover Texts, and with no Back-Cover
49Texts. A copy of the license is included in the section entitled
50``GNU Free Documentation License''.
51@end ifnottex
52
53@iftex
54@shorttitlepage GNU Make
55@end iftex
56@titlepage
57@title GNU Make
58@subtitle A Program for Directing Recompilation
59@subtitle GNU @code{make} Version @value{VERSION}
60@subtitle @value{UPDATE-MONTH}
61@author Richard M. Stallman, Roland McGrath, Paul D. Smith
62@page
63@vskip 0pt plus 1filll
64Copyright @copyright{} 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
651996, 1997, 1998, 1999, 2000, 2002, 2003, 2004 Free Software Foundation, Inc.
66@sp 2
67Published by the Free Software Foundation @*
6859 Temple Place -- Suite 330, @*
69Boston, MA 02111-1307 USA @*
70ISBN @value{ISBN} @*
71
72Permission is granted to copy, distribute and/or modify this document
73under the terms of the GNU Free Documentation License, Version 1.1 or
74any later version published by the Free Software Foundation; with the
75Invariant Sections being ``GNU General Public License'', the Front-Cover
76Texts being ``A GNU Manual'', and with the Back-Cover Texts being as in
77(a) below. A copy of the license is included in the section entitled
78``GNU Free Documentation License''.
79
80(a) The FSF's Back-Cover Text is:
81
82@quotation
83 You have freedom to copy and modify this GNU Manual, like GNU
84 software. Copies published by the Free Software Foundation raise
85 funds for GNU development.
86@end quotation
87@sp 2
88Cover art by Etienne Suvasa.
89@end titlepage
90@page
91
92@ifnottex
93@node Top, Overview, (dir), (dir)
94@top Make
95
96The GNU @code{make} utility automatically determines which pieces of a
97large program need to be recompiled, and issues the commands to
98recompile them.@refill
99
100This edition of the @cite{GNU Make Manual},
101last updated @value{UPDATED},
102documents GNU @code{make} Version @value{VERSION}.@refill
103
104This manual describes @code{make} and contains the following chapters:@refill
105@end ifnottex
106
107@menu
108* Overview:: Overview of @code{make}.
109* Introduction:: An introduction to @code{make}.
110* Makefiles:: Makefiles tell @code{make} what to do.
111* Rules:: Rules describe when a file must be remade.
112* Commands:: Commands say how to remake a file.
113* Using Variables:: You can use variables to avoid repetition.
114* Conditionals:: Use or ignore parts of the makefile based
115 on the values of variables.
116* Functions:: Many powerful ways to manipulate text.
117* Invoking make: Running. How to invoke @code{make} on the command line.
118* Implicit Rules:: Use implicit rules to treat many files alike,
119 based on their file names.
120* Archives:: How @code{make} can update library archives.
121* Features:: Features GNU @code{make} has over other @code{make}s.
122* Missing:: What GNU @code{make} lacks from other @code{make}s.
123* Makefile Conventions:: Conventions for writing makefiles for
124 GNU programs.
125* Quick Reference:: A quick reference for experienced users.
126* Error Messages:: A list of common errors generated by @code{make}.
127* Complex Makefile:: A real example of a straightforward,
128 but nontrivial, makefile.
129
130* GNU Free Documentation License:: License for copying this manual
131* Concept Index:: Index of Concepts
132* Name Index:: Index of Functions, Variables, & Directives
133
134@detailmenu
135 --- The Detailed Node Listing ---
136
137Overview of @code{make}
138
139* Preparing:: Preparing and Running Make
140* Reading:: On Reading this Text
141* Bugs:: Problems and Bugs
142
143An Introduction to Makefiles
144
145* Rule Introduction:: What a rule looks like.
146* Simple Makefile:: A Simple Makefile
147* How Make Works:: How @code{make} Processes This Makefile
148* Variables Simplify:: Variables Make Makefiles Simpler
149* make Deduces:: Letting @code{make} Deduce the Commands
150* Combine By Prerequisite:: Another Style of Makefile
151* Cleanup:: Rules for Cleaning the Directory
152
153Writing Makefiles
154
155* Makefile Contents:: What makefiles contain.
156* Makefile Names:: How to name your makefile.
157* Include:: How one makefile can use another makefile.
158* MAKEFILES Variable:: The environment can specify extra makefiles.
159* MAKEFILE_LIST Variable:: Discover which makefiles have been read.
160* Special Variables:: Other special variables.
161* Remaking Makefiles:: How makefiles get remade.
162* Overriding Makefiles:: How to override part of one makefile
163 with another makefile.
164* Reading Makefiles:: How makefiles are parsed.
165* Secondary Expansion:: How and when secondary expansion is performed.
166
167Writing Rules
168
169* Rule Example:: An example explained.
170* Rule Syntax:: General syntax explained.
171* Prerequisite Types:: There are two types of prerequisites.
172* Wildcards:: Using wildcard characters such as `*'.
173* Directory Search:: Searching other directories for source files.
174* Phony Targets:: Using a target that is not a real file's name.
175* Force Targets:: You can use a target without commands
176 or prerequisites to mark other
177 targets as phony.
178* Empty Targets:: When only the date matters and the
179 files are empty.
180* Special Targets:: Targets with special built-in meanings.
181* Multiple Targets:: When to make use of several targets in a rule.
182* Multiple Rules:: How to use several rules with the same target.
183* Static Pattern:: Static pattern rules apply to multiple targets
184 and can vary the prerequisites according to
185 the target name.
186* Double-Colon:: How to use a special kind of rule to allow
187 several independent rules for one target.
188* Automatic Prerequisites:: How to automatically generate rules giving
189 prerequisites from source files themselves.
190
191Using Wildcard Characters in File Names
192
193* Wildcard Examples:: Several examples
194* Wildcard Pitfall:: Problems to avoid.
195* Wildcard Function:: How to cause wildcard expansion where
196 it does not normally take place.
197
198Searching Directories for Prerequisites
199
200* General Search:: Specifying a search path that applies
201 to every prerequisite.
202* Selective Search:: Specifying a search path
203 for a specified class of names.
204* Search Algorithm:: When and how search paths are applied.
205* Commands/Search:: How to write shell commands that work together
206 with search paths.
207* Implicit/Search:: How search paths affect implicit rules.
208* Libraries/Search:: Directory search for link libraries.
209
210Static Pattern Rules
211
212* Static Usage:: The syntax of static pattern rules.
213* Static versus Implicit:: When are they better than implicit rules?
214
215Writing the Commands in Rules
216
217* Echoing:: How to control when commands are echoed.
218* Execution:: How commands are executed.
219* Parallel:: How commands can be executed in parallel.
220* Errors:: What happens after a command execution error.
221* Interrupts:: What happens when a command is interrupted.
222* Recursion:: Invoking @code{make} from makefiles.
223* Sequences:: Defining canned sequences of commands.
224* Empty Commands:: Defining useful, do-nothing commands.
225
226Recursive Use of @code{make}
227
228* MAKE Variable:: The special effects of using @samp{$(MAKE)}.
229* Variables/Recursion:: How to communicate variables to a sub-@code{make}.
230* Options/Recursion:: How to communicate options to a sub-@code{make}.
231* -w Option:: How the @samp{-w} or @samp{--print-directory} option
232 helps debug use of recursive @code{make} commands.
233
234How to Use Variables
235
236* Reference:: How to use the value of a variable.
237* Flavors:: Variables come in two flavors.
238* Advanced:: Advanced features for referencing a variable.
239* Values:: All the ways variables get their values.
240* Setting:: How to set a variable in the makefile.
241* Appending:: How to append more text to the old value
242 of a variable.
243* Override Directive:: How to set a variable in the makefile even if
244 the user has set it with a command argument.
245* Defining:: An alternate way to set a variable
246 to a verbatim string.
247* Environment:: Variable values can come from the environment.
248* Target-specific:: Variable values can be defined on a per-target
249 basis.
250* Pattern-specific:: Target-specific variable values can be applied
251 to a group of targets that match a pattern.
252
253Advanced Features for Reference to Variables
254
255* Substitution Refs:: Referencing a variable with
256 substitutions on the value.
257* Computed Names:: Computing the name of the variable to refer to.
258
259Conditional Parts of Makefiles
260
261* Conditional Example:: Example of a conditional
262* Conditional Syntax:: The syntax of conditionals.
263* Testing Flags:: Conditionals that test flags.
264
265Functions for Transforming Text
266
267* Syntax of Functions:: How to write a function call.
268* Text Functions:: General-purpose text manipulation functions.
269* File Name Functions:: Functions for manipulating file names.
270* Foreach Function:: Repeat some text with controlled variation.
271* If Function:: Conditionally expand a value.
272* Call Function:: Expand a user-defined function.
273* Value Function:: Return the un-expanded value of a variable.
274* Eval Function:: Evaluate the arguments as makefile syntax.
275* Origin Function:: Find where a variable got its value.
276* Shell Function:: Substitute the output of a shell command.
277* Make Control Functions:: Functions that control how make runs.
278
279How to Run @code{make}
280
281* Makefile Arguments:: How to specify which makefile to use.
282* Goals:: How to use goal arguments to specify which
283 parts of the makefile to use.
284* Instead of Execution:: How to use mode flags to specify what
285 kind of thing to do with the commands
286 in the makefile other than simply
287 execute them.
288* Avoiding Compilation:: How to avoid recompiling certain files.
289* Overriding:: How to override a variable to specify
290 an alternate compiler and other things.
291* Testing:: How to proceed past some errors, to
292 test compilation.
293* Options Summary:: Summary of Options
294
295Using Implicit Rules
296
297* Using Implicit:: How to use an existing implicit rule
298 to get the commands for updating a file.
299* Catalogue of Rules:: A list of built-in implicit rules.
300* Implicit Variables:: How to change what predefined rules do.
301* Chained Rules:: How to use a chain of implicit rules.
302* Pattern Rules:: How to define new implicit rules.
303* Last Resort:: How to defining commands for rules
304 which cannot find any.
305* Suffix Rules:: The old-fashioned style of implicit rule.
306* Implicit Rule Search:: The precise algorithm for applying
307 implicit rules.
308
309Defining and Redefining Pattern Rules
310
311* Pattern Intro:: An introduction to pattern rules.
312* Pattern Examples:: Examples of pattern rules.
313* Automatic Variables:: How to use automatic variables in the
314 commands of implicit rules.
315* Pattern Match:: How patterns match.
316* Match-Anything Rules:: Precautions you should take prior to
317 defining rules that can match any
318 target file whatever.
319* Canceling Rules:: How to override or cancel built-in rules.
320
321Using @code{make} to Update Archive Files
322
323* Archive Members:: Archive members as targets.
324* Archive Update:: The implicit rule for archive member targets.
325* Archive Pitfalls:: Dangers to watch out for when using archives.
326* Archive Suffix Rules:: You can write a special kind of suffix rule
327 for updating archives.
328
329Implicit Rule for Archive Member Targets
330
331* Archive Symbols:: How to update archive symbol directories.
332
333Makefile Conventions
334
335* Makefile Basics:: General Conventions for Makefiles
336* Utilities in Makefiles:: Utilities in Makefiles
337* Command Variables:: Variables for Specifying Commands
338* Directory Variables:: Variables for Installation Directories
339* Standard Targets:: Standard Targets for Users
340* Install Command Categories:: Three categories of commands in the `install'
341
342@end detailmenu
343@end menu
344
345@node Overview, Introduction, Top, Top
346@comment node-name, next, previous, up
347@chapter Overview of @code{make}
348
349The @code{make} utility automatically determines which pieces of a large
350program need to be recompiled, and issues commands to recompile them.
351This manual describes GNU @code{make}, which was implemented by Richard
352Stallman and Roland McGrath. Development since Version 3.76 has been
353handled by Paul D. Smith.
354
355GNU @code{make} conforms to section 6.2 of @cite{IEEE Standard
3561003.2-1992} (POSIX.2).
357@cindex POSIX
358@cindex IEEE Standard 1003.2
359@cindex standards conformance
360
361Our examples show C programs, since they are most common, but you can use
362@code{make} with any programming language whose compiler can be run with a
363shell command. Indeed, @code{make} is not limited to programs. You can
364use it to describe any task where some files must be updated automatically
365from others whenever the others change.
366
367@menu
368* Preparing:: Preparing and Running Make
369* Reading:: On Reading this Text
370* Bugs:: Problems and Bugs
371@end menu
372
373@node Preparing, Reading, Overview, Overview
374@ifnottex
375@heading Preparing and Running Make
376@end ifnottex
377
378To prepare to use @code{make}, you must write a file called
379the @dfn{makefile} that describes the relationships among files
380in your program and provides commands for updating each file.
381In a program, typically, the executable file is updated from object
382files, which are in turn made by compiling source files.@refill
383
384Once a suitable makefile exists, each time you change some source files,
385this simple shell command:
386
387@example
388make
389@end example
390
391@noindent
392suffices to perform all necessary recompilations. The @code{make} program
393uses the makefile data base and the last-modification times of the files to
394decide which of the files need to be updated. For each of those files, it
395issues the commands recorded in the data base.
396
397You can provide command line arguments to @code{make} to control which
398files should be recompiled, or how. @xref{Running, ,How to Run
399@code{make}}.
400
401@node Reading, Bugs, Preparing, Overview
402@section How to Read This Manual
403
404If you are new to @code{make}, or are looking for a general
405introduction, read the first few sections of each chapter, skipping the
406later sections. In each chapter, the first few sections contain
407introductory or general information and the later sections contain
408specialized or technical information.
409@ifnottex
410The exception is the second chapter, @ref{Introduction, ,An
411Introduction to Makefiles}, all of which is introductory.
412@end ifnottex
413@iftex
414The exception is @ref{Introduction, ,An Introduction to Makefiles},
415all of which is introductory.
416@end iftex
417
418If you are familiar with other @code{make} programs, see @ref{Features,
419,Features of GNU @code{make}}, which lists the enhancements GNU
420@code{make} has, and @ref{Missing, ,Incompatibilities and Missing
421Features}, which explains the few things GNU @code{make} lacks that
422others have.
423
424For a quick summary, see @ref{Options Summary}, @ref{Quick Reference},
425and @ref{Special Targets}.
426
427@node Bugs, , Reading, Overview
428@section Problems and Bugs
429@cindex reporting bugs
430@cindex bugs, reporting
431@cindex problems and bugs, reporting
432
433If you have problems with GNU @code{make} or think you've found a bug,
434please report it to the developers; we cannot promise to do anything but
435we might well want to fix it.
436
437Before reporting a bug, make sure you've actually found a real bug.
438Carefully reread the documentation and see if it really says you can do
439what you're trying to do. If it's not clear whether you should be able
440to do something or not, report that too; it's a bug in the
441documentation!
442
443Before reporting a bug or trying to fix it yourself, try to isolate it
444to the smallest possible makefile that reproduces the problem. Then
445send us the makefile and the exact results @code{make} gave you,
446including any error or warning messages. Please don't paraphrase
447these messages: it's best to cut and paste them into your report.
448When generating this small makefile, be sure to not use any non-free
449or unusual tools in your commands: you can almost always emulate what
450such a tool would do with simple shell commands. Finally, be sure to
451explain what you expected to occur; this will help us decide whether
452the problem was really in the documentation.
453
454Once you have a precise problem you can report it in one of two ways.
455Either send electronic mail to:
456
457@example
458 bug-make@@gnu.org
459@end example
460
461@noindent
462or use our Web-based project management tool, at:
463
464@example
465 http://savannah.gnu.org/projects/make/
466@end example
467
468@noindent
469In addition to the information above, please be careful to include the
470version number of @code{make} you are using. You can get this
471information with the command @samp{make --version}. Be sure also to
472include the type of machine and operating system you are using. One
473way to obtain this information is by looking at the final lines of
474output from the command @samp{make --help}.
475
476@node Introduction, Makefiles, Overview, Top
477@comment node-name, next, previous, up
478@chapter An Introduction to Makefiles
479
480You need a file called a @dfn{makefile} to tell @code{make} what to do.
481Most often, the makefile tells @code{make} how to compile and link a
482program.
483@cindex makefile
484
485In this chapter, we will discuss a simple makefile that describes how to
486compile and link a text editor which consists of eight C source files
487and three header files. The makefile can also tell @code{make} how to
488run miscellaneous commands when explicitly asked (for example, to remove
489certain files as a clean-up operation). To see a more complex example
490of a makefile, see @ref{Complex Makefile}.
491
492When @code{make} recompiles the editor, each changed C source file
493must be recompiled. If a header file has changed, each C source file
494that includes the header file must be recompiled to be safe. Each
495compilation produces an object file corresponding to the source file.
496Finally, if any source file has been recompiled, all the object files,
497whether newly made or saved from previous compilations, must be linked
498together to produce the new executable editor.
499@cindex recompilation
500@cindex editor
501
502@menu
503* Rule Introduction:: What a rule looks like.
504* Simple Makefile:: A Simple Makefile
505* How Make Works:: How @code{make} Processes This Makefile
506* Variables Simplify:: Variables Make Makefiles Simpler
507* make Deduces:: Letting @code{make} Deduce the Commands
508* Combine By Prerequisite:: Another Style of Makefile
509* Cleanup:: Rules for Cleaning the Directory
510@end menu
511
512@node Rule Introduction, Simple Makefile, Introduction, Introduction
513@comment node-name, next, previous, up
514@section What a Rule Looks Like
515@cindex rule, introduction to
516@cindex makefile rule parts
517@cindex parts of makefile rule
518
519A simple makefile consists of ``rules'' with the following shape:
520
521@cindex targets, introduction to
522@cindex prerequisites, introduction to
523@cindex commands, introduction to
524@example
525@group
526@var{target} @dots{} : @var{prerequisites} @dots{}
527 @var{command}
528 @dots{}
529 @dots{}
530@end group
531@end example
532
533A @dfn{target} is usually the name of a file that is generated by a
534program; examples of targets are executable or object files. A target
535can also be the name of an action to carry out, such as @samp{clean}
536(@pxref{Phony Targets}).
537
538A @dfn{prerequisite} is a file that is used as input to create the
539target. A target often depends on several files.
540
541@cindex tabs in rules
542A @dfn{command} is an action that @code{make} carries out.
543A rule may have more than one command, each on its own line.
544@strong{Please note:} you need to put a tab character at the beginning of
545every command line! This is an obscurity that catches the unwary.
546
547Usually a command is in a rule with prerequisites and serves to create a
548target file if any of the prerequisites change. However, the rule that
549specifies commands for the target need not have prerequisites. For
550example, the rule containing the delete command associated with the
551target @samp{clean} does not have prerequisites.
552
553A @dfn{rule}, then, explains how and when to remake certain files
554which are the targets of the particular rule. @code{make} carries out
555the commands on the prerequisites to create or update the target. A
556rule can also explain how and when to carry out an action.
557@xref{Rules, , Writing Rules}.
558
559A makefile may contain other text besides rules, but a simple makefile
560need only contain rules. Rules may look somewhat more complicated
561than shown in this template, but all fit the pattern more or less.
562
563@node Simple Makefile, How Make Works, Rule Introduction, Introduction
564@section A Simple Makefile
565@cindex simple makefile
566@cindex makefile, simple
567
568Here is a straightforward makefile that describes the way an
569executable file called @code{edit} depends on eight object files
570which, in turn, depend on eight C source and three header files.
571
572In this example, all the C files include @file{defs.h}, but only those
573defining editing commands include @file{command.h}, and only low
574level files that change the editor buffer include @file{buffer.h}.
575
576@example
577@group
578edit : main.o kbd.o command.o display.o \
579 insert.o search.o files.o utils.o
580 cc -o edit main.o kbd.o command.o display.o \
581 insert.o search.o files.o utils.o
582
583main.o : main.c defs.h
584 cc -c main.c
585kbd.o : kbd.c defs.h command.h
586 cc -c kbd.c
587command.o : command.c defs.h command.h
588 cc -c command.c
589display.o : display.c defs.h buffer.h
590 cc -c display.c
591insert.o : insert.c defs.h buffer.h
592 cc -c insert.c
593search.o : search.c defs.h buffer.h
594 cc -c search.c
595files.o : files.c defs.h buffer.h command.h
596 cc -c files.c
597utils.o : utils.c defs.h
598 cc -c utils.c
599clean :
600 rm edit main.o kbd.o command.o display.o \
601 insert.o search.o files.o utils.o
602@end group
603@end example
604
605@noindent
606We split each long line into two lines using backslash-newline; this is
607like using one long line, but is easier to read.
608@cindex continuation lines
609@cindex @code{\} (backslash), for continuation lines
610@cindex backslash (@code{\}), for continuation lines
611@cindex quoting newline, in makefile
612@cindex newline, quoting, in makefile
613
614To use this makefile to create the executable file called @file{edit},
615type:
616
617@example
618make
619@end example
620
621To use this makefile to delete the executable file and all the object
622files from the directory, type:
623
624@example
625make clean
626@end example
627
628In the example makefile, the targets include the executable file
629@samp{edit}, and the object files @samp{main.o} and @samp{kbd.o}. The
630prerequisites are files such as @samp{main.c} and @samp{defs.h}.
631In fact, each @samp{.o} file is both a target and a prerequisite.
632Commands include @w{@samp{cc -c main.c}} and @w{@samp{cc -c kbd.c}}.
633
634When a target is a file, it needs to be recompiled or relinked if any
635of its prerequisites change. In addition, any prerequisites that are
636themselves automatically generated should be updated first. In this
637example, @file{edit} depends on each of the eight object files; the
638object file @file{main.o} depends on the source file @file{main.c} and
639on the header file @file{defs.h}.
640
641A shell command follows each line that contains a target and
642prerequisites. These shell commands say how to update the target file.
643A tab character must come at the beginning of every command line to
644distinguish command lines from other lines in the makefile. (Bear in
645mind that @code{make} does not know anything about how the commands
646work. It is up to you to supply commands that will update the target
647file properly. All @code{make} does is execute the commands in the rule
648you have specified when the target file needs to be updated.)
649@cindex shell command
650
651The target @samp{clean} is not a file, but merely the name of an
652action. Since you
653normally
654do not want to carry out the actions in this rule, @samp{clean} is not a prerequisite of any other rule.
655Consequently, @code{make} never does anything with it unless you tell
656it specifically. Note that this rule not only is not a prerequisite, it
657also does not have any prerequisites, so the only purpose of the rule
658is to run the specified commands. Targets that do not refer to files
659but are just actions are called @dfn{phony targets}. @xref{Phony
660Targets}, for information about this kind of target. @xref{Errors, ,
661Errors in Commands}, to see how to cause @code{make} to ignore errors
662from @code{rm} or any other command.
663@cindex @code{clean} target
664@cindex @code{rm} (shell command)
665
666@node How Make Works, Variables Simplify, Simple Makefile, Introduction
667@comment node-name, next, previous, up
668@section How @code{make} Processes a Makefile
669@cindex processing a makefile
670@cindex makefile, how @code{make} processes
671
672By default, @code{make} starts with the first target (not targets whose
673names start with @samp{.}). This is called the @dfn{default goal}.
674(@dfn{Goals} are the targets that @code{make} strives ultimately to
675update. You can override this behavior using the command line
676(@pxref{Goals, , Arguments to Specify the Goals}) or with the
677@code{.DEFAULT_GOAL} special variable (@pxref{Special Variables, ,
678Other Special Variables}).
679@cindex default goal
680@cindex goal, default
681@cindex goal
682
683In the simple example of the previous section, the default goal is to
684update the executable program @file{edit}; therefore, we put that rule
685first.
686
687Thus, when you give the command:
688
689@example
690make
691@end example
692
693@noindent
694@code{make} reads the makefile in the current directory and begins by
695processing the first rule. In the example, this rule is for relinking
696@file{edit}; but before @code{make} can fully process this rule, it
697must process the rules for the files that @file{edit} depends on,
698which in this case are the object files. Each of these files is
699processed according to its own rule. These rules say to update each
700@samp{.o} file by compiling its source file. The recompilation must
701be done if the source file, or any of the header files named as
702prerequisites, is more recent than the object file, or if the object
703file does not exist.
704
705The other rules are processed because their targets appear as
706prerequisites of the goal. If some other rule is not depended on by the
707goal (or anything it depends on, etc.), that rule is not processed,
708unless you tell @code{make} to do so (with a command such as
709@w{@code{make clean}}).
710
711Before recompiling an object file, @code{make} considers updating its
712prerequisites, the source file and header files. This makefile does not
713specify anything to be done for them---the @samp{.c} and @samp{.h} files
714are not the targets of any rules---so @code{make} does nothing for these
715files. But @code{make} would update automatically generated C programs,
716such as those made by Bison or Yacc, by their own rules at this time.
717
718After recompiling whichever object files need it, @code{make} decides
719whether to relink @file{edit}. This must be done if the file
720@file{edit} does not exist, or if any of the object files are newer than
721it. If an object file was just recompiled, it is now newer than
722@file{edit}, so @file{edit} is relinked.
723@cindex relinking
724
725Thus, if we change the file @file{insert.c} and run @code{make},
726@code{make} will compile that file to update @file{insert.o}, and then
727link @file{edit}. If we change the file @file{command.h} and run
728@code{make}, @code{make} will recompile the object files @file{kbd.o},
729@file{command.o} and @file{files.o} and then link the file @file{edit}.
730
731@node Variables Simplify, make Deduces, How Make Works, Introduction
732@section Variables Make Makefiles Simpler
733@cindex variables
734@cindex simplifying with variables
735
736In our example, we had to list all the object files twice in the rule for
737@file{edit} (repeated here):
738
739@example
740@group
741edit : main.o kbd.o command.o display.o \
742 insert.o search.o files.o utils.o
743 cc -o edit main.o kbd.o command.o display.o \
744 insert.o search.o files.o utils.o
745@end group
746@end example
747
748@cindex @code{objects}
749Such duplication is error-prone; if a new object file is added to the
750system, we might add it to one list and forget the other. We can eliminate
751the risk and simplify the makefile by using a variable. @dfn{Variables}
752allow a text string to be defined once and substituted in multiple places
753later (@pxref{Using Variables, ,How to Use Variables}).
754
755@cindex @code{OBJECTS}
756@cindex @code{objs}
757@cindex @code{OBJS}
758@cindex @code{obj}
759@cindex @code{OBJ}
760It is standard practice for every makefile to have a variable named
761@code{objects}, @code{OBJECTS}, @code{objs}, @code{OBJS}, @code{obj},
762or @code{OBJ} which is a list of all object file names. We would
763define such a variable @code{objects} with a line like this in the
764makefile:@refill
765
766@example
767@group
768objects = main.o kbd.o command.o display.o \
769 insert.o search.o files.o utils.o
770@end group
771@end example
772
773@noindent
774Then, each place we want to put a list of the object file names, we can
775substitute the variable's value by writing @samp{$(objects)}
776(@pxref{Using Variables, ,How to Use Variables}).
777
778Here is how the complete simple makefile looks when you use a variable
779for the object files:
780
781@example
782@group
783objects = main.o kbd.o command.o display.o \
784 insert.o search.o files.o utils.o
785
786edit : $(objects)
787 cc -o edit $(objects)
788main.o : main.c defs.h
789 cc -c main.c
790kbd.o : kbd.c defs.h command.h
791 cc -c kbd.c
792command.o : command.c defs.h command.h
793 cc -c command.c
794display.o : display.c defs.h buffer.h
795 cc -c display.c
796insert.o : insert.c defs.h buffer.h
797 cc -c insert.c
798search.o : search.c defs.h buffer.h
799 cc -c search.c
800files.o : files.c defs.h buffer.h command.h
801 cc -c files.c
802utils.o : utils.c defs.h
803 cc -c utils.c
804clean :
805 rm edit $(objects)
806@end group
807@end example
808
809@node make Deduces, Combine By Prerequisite, Variables Simplify, Introduction
810@section Letting @code{make} Deduce the Commands
811@cindex deducing commands (implicit rules)
812@cindex implicit rule, introduction to
813@cindex rule, implicit, introduction to
814
815It is not necessary to spell out the commands for compiling the individual
816C source files, because @code{make} can figure them out: it has an
817@dfn{implicit rule} for updating a @samp{.o} file from a correspondingly
818named @samp{.c} file using a @samp{cc -c} command. For example, it will
819use the command @samp{cc -c main.c -o main.o} to compile @file{main.c} into
820@file{main.o}. We can therefore omit the commands from the rules for the
821object files. @xref{Implicit Rules, ,Using Implicit Rules}.@refill
822
823When a @samp{.c} file is used automatically in this way, it is also
824automatically added to the list of prerequisites. We can therefore omit
825the @samp{.c} files from the prerequisites, provided we omit the commands.
826
827Here is the entire example, with both of these changes, and a variable
828@code{objects} as suggested above:
829
830@example
831@group
832objects = main.o kbd.o command.o display.o \
833 insert.o search.o files.o utils.o
834
835edit : $(objects)
836 cc -o edit $(objects)
837
838main.o : defs.h
839kbd.o : defs.h command.h
840command.o : defs.h command.h
841display.o : defs.h buffer.h
842insert.o : defs.h buffer.h
843search.o : defs.h buffer.h
844files.o : defs.h buffer.h command.h
845utils.o : defs.h
846
847.PHONY : clean
848clean :
849 rm edit $(objects)
850@end group
851@end example
852
853@noindent
854This is how we would write the makefile in actual practice. (The
855complications associated with @samp{clean} are described elsewhere.
856See @ref{Phony Targets}, and @ref{Errors, ,Errors in Commands}.)
857
858Because implicit rules are so convenient, they are important. You
859will see them used frequently.@refill
860
861@node Combine By Prerequisite, Cleanup, make Deduces, Introduction
862@section Another Style of Makefile
863@cindex combining rules by prerequisite
864
865When the objects of a makefile are created only by implicit rules, an
866alternative style of makefile is possible. In this style of makefile,
867you group entries by their prerequisites instead of by their targets.
868Here is what one looks like:
869
870@example
871@group
872objects = main.o kbd.o command.o display.o \
873 insert.o search.o files.o utils.o
874
875edit : $(objects)
876 cc -o edit $(objects)
877
878$(objects) : defs.h
879kbd.o command.o files.o : command.h
880display.o insert.o search.o files.o : buffer.h
881@end group
882@end example
883
884@noindent
885Here @file{defs.h} is given as a prerequisite of all the object files;
886@file{command.h} and @file{buffer.h} are prerequisites of the specific
887object files listed for them.
888
889Whether this is better is a matter of taste: it is more compact, but some
890people dislike it because they find it clearer to put all the information
891about each target in one place.
892
893@node Cleanup, , Combine By Prerequisite, Introduction
894@section Rules for Cleaning the Directory
895@cindex cleaning up
896@cindex removing, to clean up
897
898Compiling a program is not the only thing you might want to write rules
899for. Makefiles commonly tell how to do a few other things besides
900compiling a program: for example, how to delete all the object files
901and executables so that the directory is @samp{clean}.
902
903@cindex @code{clean} target
904Here is how we
905could write a @code{make} rule for cleaning our example editor:
906
907@example
908@group
909clean:
910 rm edit $(objects)
911@end group
912@end example
913
914In practice, we might want to write the rule in a somewhat more
915complicated manner to handle unanticipated situations. We would do this:
916
917@example
918@group
919.PHONY : clean
920clean :
921 -rm edit $(objects)
922@end group
923@end example
924
925@noindent
926This prevents @code{make} from getting confused by an actual file
927called @file{clean} and causes it to continue in spite of errors from
928@code{rm}. (See @ref{Phony Targets}, and @ref{Errors, ,Errors in
929Commands}.)
930
931@noindent
932A rule such as this should not be placed at the beginning of the
933makefile, because we do not want it to run by default! Thus, in the
934example makefile, we want the rule for @code{edit}, which recompiles
935the editor, to remain the default goal.
936
937Since @code{clean} is not a prerequisite of @code{edit}, this rule will not
938run at all if we give the command @samp{make} with no arguments. In
939order to make the rule run, we have to type @samp{make clean}.
940@xref{Running, ,How to Run @code{make}}.
941
942@node Makefiles, Rules, Introduction, Top
943@chapter Writing Makefiles
944
945@cindex makefile, how to write
946The information that tells @code{make} how to recompile a system comes from
947reading a data base called the @dfn{makefile}.
948
949@menu
950* Makefile Contents:: What makefiles contain.
951* Makefile Names:: How to name your makefile.
952* Include:: How one makefile can use another makefile.
953* MAKEFILES Variable:: The environment can specify extra makefiles.
954* MAKEFILE_LIST Variable:: Discover which makefiles have been read.
955* Special Variables:: Other special variables.
956* Remaking Makefiles:: How makefiles get remade.
957* Overriding Makefiles:: How to override part of one makefile
958 with another makefile.
959* Reading Makefiles:: How makefiles are parsed.
960* Secondary Expansion:: How and when secondary expansion is performed.
961@end menu
962
963@node Makefile Contents, Makefile Names, Makefiles, Makefiles
964@section What Makefiles Contain
965
966Makefiles contain five kinds of things: @dfn{explicit rules},
967@dfn{implicit rules}, @dfn{variable definitions}, @dfn{directives},
968and @dfn{comments}. Rules, variables, and directives are described at
969length in later chapters.@refill
970
971@itemize @bullet
972@cindex rule, explicit, definition of
973@cindex explicit rule, definition of
974@item
975An @dfn{explicit rule} says when and how to remake one or more files,
976called the rule's @dfn{targets}. It lists the other files that the
977targets depend on, called the @dfn{prerequisites} of the target, and
978may also give commands to use to create or update the targets.
979@xref{Rules, ,Writing Rules}.
980
981@cindex rule, implicit, definition of
982@cindex implicit rule, definition of
983@item
984An @dfn{implicit rule} says when and how to remake a class of files
985based on their names. It describes how a target may depend on a file
986with a name similar to the target and gives commands to create or
987update such a target. @xref{Implicit Rules, ,Using Implicit Rules}.
988
989@cindex variable definition
990@item
991A @dfn{variable definition} is a line that specifies a text string
992value for a variable that can be substituted into the text later. The
993simple makefile example shows a variable definition for @code{objects}
994as a list of all object files (@pxref{Variables Simplify, , Variables
995Make Makefiles Simpler}).
996
997@cindex directive
998@item
999A @dfn{directive} is a command for @code{make} to do something special while
1000reading the makefile. These include:
1001
1002@itemize @bullet
1003@item
1004Reading another makefile (@pxref{Include, ,Including Other Makefiles}).
1005
1006@item
1007Deciding (based on the values of variables) whether to use or
1008ignore a part of the makefile (@pxref{Conditionals, ,Conditional Parts of Makefiles}).
1009
1010@item
1011Defining a variable from a verbatim string containing multiple lines
1012(@pxref{Defining, ,Defining Variables Verbatim}).
1013@end itemize
1014
1015@cindex comments, in makefile
1016@cindex @code{#} (comments), in makefile
1017@item
1018@samp{#} in a line of a makefile starts a @dfn{comment}. It and the
1019rest of the line are ignored, except that a trailing backslash not
1020escaped by another backslash will continue the comment across multiple
1021lines. A line containing just a comment (with perhaps spaces before
1022it) is effectively blank, and is ignored. If you want a literal
1023@code{#}, escape it with a backslash (e.g., @code{\#}). Comments may
1024appear on any line in the makefile, although they are treated
1025specially in certain situations.
1026
1027Within a command script (if the line begins with a TAB character) the
1028entire line is passed to the shell, just as with any other line that
1029begins with a TAB. The shell decides how to interpret the text:
1030whether or not this is a comment is up to the shell.
1031
1032Within a @code{define} directive, comments are not ignored during the
1033definition of the variable, but rather kept intact in the value of the
1034variable. When the variable is expanded they will either be treated
1035as @code{make} comments or as command script text, depending on the
1036context in which the variable is evaluated.
1037@end itemize
1038
1039@node Makefile Names, Include, Makefile Contents, Makefiles
1040@section What Name to Give Your Makefile
1041@cindex makefile name
1042@cindex name of makefile
1043@cindex default makefile name
1044@cindex file name of makefile
1045
1046@c following paragraph rewritten to avoid overfull hbox
1047By default, when @code{make} looks for the makefile, it tries the
1048following names, in order: @file{GNUmakefile}, @file{makefile}
1049and @file{Makefile}.@refill
1050@findex Makefile
1051@findex GNUmakefile
1052@findex makefile
1053
1054@cindex @code{README}
1055Normally you should call your makefile either @file{makefile} or
1056@file{Makefile}. (We recommend @file{Makefile} because it appears
1057prominently near the beginning of a directory listing, right near other
1058important files such as @file{README}.) The first name checked,
1059@file{GNUmakefile}, is not recommended for most makefiles. You should
1060use this name if you have a makefile that is specific to GNU
1061@code{make}, and will not be understood by other versions of
1062@code{make}. Other @code{make} programs look for @file{makefile} and
1063@file{Makefile}, but not @file{GNUmakefile}.
1064
1065If @code{make} finds none of these names, it does not use any makefile.
1066Then you must specify a goal with a command argument, and @code{make}
1067will attempt to figure out how to remake it using only its built-in
1068implicit rules. @xref{Implicit Rules, ,Using Implicit Rules}.
1069
1070@cindex @code{-f}
1071@cindex @code{--file}
1072@cindex @code{--makefile}
1073If you want to use a nonstandard name for your makefile, you can specify
1074the makefile name with the @samp{-f} or @samp{--file} option. The
1075arguments @w{@samp{-f @var{name}}} or @w{@samp{--file=@var{name}}} tell
1076@code{make} to read the file @var{name} as the makefile. If you use
1077more than one @samp{-f} or @samp{--file} option, you can specify several
1078makefiles. All the makefiles are effectively concatenated in the order
1079specified. The default makefile names @file{GNUmakefile},
1080@file{makefile} and @file{Makefile} are not checked automatically if you
1081specify @samp{-f} or @samp{--file}.@refill
1082@cindex specifying makefile name
1083@cindex makefile name, how to specify
1084@cindex name of makefile, how to specify
1085@cindex file name of makefile, how to specify
1086
1087@node Include, MAKEFILES Variable, Makefile Names, Makefiles
1088@section Including Other Makefiles
1089@cindex including other makefiles
1090@cindex makefile, including
1091
1092@findex include
1093The @code{include} directive tells @code{make} to suspend reading the
1094current makefile and read one or more other makefiles before continuing.
1095The directive is a line in the makefile that looks like this:
1096
1097@example
1098include @var{filenames}@dots{}
1099@end example
1100
1101@noindent
1102@var{filenames} can contain shell file name patterns.
1103@cindex shell file name pattern (in @code{include})
1104@cindex shell wildcards (in @code{include})
1105@cindex wildcard, in @code{include}
1106
1107Extra spaces are allowed and ignored at the beginning of the line, but
1108a tab is not allowed. (If the line begins with a tab, it will be
1109considered a command line.) Whitespace is required between
1110@code{include} and the file names, and between file names; extra
1111whitespace is ignored there and at the end of the directive. A
1112comment starting with @samp{#} is allowed at the end of the line. If
1113the file names contain any variable or function references, they are
1114expanded. @xref{Using Variables, ,How to Use Variables}.
1115
1116For example, if you have three @file{.mk} files, @file{a.mk},
1117@file{b.mk}, and @file{c.mk}, and @code{$(bar)} expands to
1118@code{bish bash}, then the following expression
1119
1120@example
1121include foo *.mk $(bar)
1122@end example
1123
1124is equivalent to
1125
1126@example
1127include foo a.mk b.mk c.mk bish bash
1128@end example
1129
1130When @code{make} processes an @code{include} directive, it suspends
1131reading of the containing makefile and reads from each listed file in
1132turn. When that is finished, @code{make} resumes reading the
1133makefile in which the directive appears.
1134
1135One occasion for using @code{include} directives is when several programs,
1136handled by individual makefiles in various directories, need to use a
1137common set of variable definitions
1138(@pxref{Setting, ,Setting Variables}) or pattern rules
1139(@pxref{Pattern Rules, ,Defining and Redefining Pattern Rules}).
1140
1141Another such occasion is when you want to generate prerequisites from
1142source files automatically; the prerequisites can be put in a file that
1143is included by the main makefile. This practice is generally cleaner
1144than that of somehow appending the prerequisites to the end of the main
1145makefile as has been traditionally done with other versions of
1146@code{make}. @xref{Automatic Prerequisites}.
1147@cindex prerequisites, automatic generation
1148@cindex automatic generation of prerequisites
1149@cindex generating prerequisites automatically
1150
1151@cindex @code{-I}
1152@cindex @code{--include-dir}
1153@cindex included makefiles, default directories
1154@cindex default directories for included makefiles
1155@findex /usr/gnu/include
1156@findex /usr/local/include
1157@findex /usr/include
1158If the specified name does not start with a slash, and the file is not
1159found in the current directory, several other directories are searched.
1160First, any directories you have specified with the @samp{-I} or
1161@samp{--include-dir} option are searched
1162(@pxref{Options Summary, ,Summary of Options}).
1163Then the following directories (if they exist)
1164are searched, in this order:
1165@file{@var{prefix}/include} (normally @file{/usr/local/include}
1166@footnote{GNU Make compiled for MS-DOS and MS-Windows behaves as if
1167@var{prefix} has been defined to be the root of the DJGPP tree
1168hierarchy.})
1169@file{/usr/gnu/include},
1170@file{/usr/local/include}, @file{/usr/include}.
1171
1172If an included makefile cannot be found in any of these directories, a
1173warning message is generated, but it is not an immediately fatal error;
1174processing of the makefile containing the @code{include} continues.
1175Once it has finished reading makefiles, @code{make} will try to remake
1176any that are out of date or don't exist.
1177@xref{Remaking Makefiles, ,How Makefiles Are Remade}.
1178Only after it has tried to find a way to remake a makefile and failed,
1179will @code{make} diagnose the missing makefile as a fatal error.
1180
1181If you want @code{make} to simply ignore a makefile which does not exist
1182and cannot be remade, with no error message, use the @w{@code{-include}}
1183directive instead of @code{include}, like this:
1184
1185@example
1186-include @var{filenames}@dots{}
1187@end example
1188
1189This acts like @code{include} in every way except that there is no
1190error (not even a warning) if any of the @var{filenames} do not exist.
1191For compatibility with some other @code{make} implementations,
1192@code{sinclude} is another name for @w{@code{-include}}.
1193
1194@node MAKEFILES Variable, MAKEFILE_LIST Variable, Include, Makefiles
1195@section The Variable @code{MAKEFILES}
1196@cindex makefile, and @code{MAKEFILES} variable
1197@cindex including (@code{MAKEFILES} variable)
1198
1199@vindex MAKEFILES
1200If the environment variable @code{MAKEFILES} is defined, @code{make}
1201considers its value as a list of names (separated by whitespace) of
1202additional makefiles to be read before the others. This works much like
1203the @code{include} directive: various directories are searched for those
1204files (@pxref{Include, ,Including Other Makefiles}). In addition, the
1205default goal is never taken from one of these makefiles and it is not an
1206error if the files listed in @code{MAKEFILES} are not found.@refill
1207
1208@cindex recursion, and @code{MAKEFILES} variable
1209The main use of @code{MAKEFILES} is in communication between recursive
1210invocations of @code{make} (@pxref{Recursion, ,Recursive Use of
1211@code{make}}). It usually is not desirable to set the environment
1212variable before a top-level invocation of @code{make}, because it is
1213usually better not to mess with a makefile from outside. However, if
1214you are running @code{make} without a specific makefile, a makefile in
1215@code{MAKEFILES} can do useful things to help the built-in implicit
1216rules work better, such as defining search paths (@pxref{Directory Search}).
1217
1218Some users are tempted to set @code{MAKEFILES} in the environment
1219automatically on login, and program makefiles to expect this to be done.
1220This is a very bad idea, because such makefiles will fail to work if run by
1221anyone else. It is much better to write explicit @code{include} directives
1222in the makefiles. @xref{Include, , Including Other Makefiles}.
1223
1224@node MAKEFILE_LIST Variable, Special Variables, MAKEFILES Variable, Makefiles
1225@comment node-name, next, previous, up
1226@section The Variable @code{MAKEFILE_LIST}
1227@cindex makefiles, and @code{MAKEFILE_LIST} variable
1228@cindex including (@code{MAKEFILE_LIST} variable)
1229@vindex MAKEFILE_LIST
1230
1231As @code{make} reads various makefiles, including any obtained from the
1232@code{MAKEFILES} variable, the command line, the default files, or
1233from @code{include} directives, their names will be automatically
1234appended to the @code{MAKEFILE_LIST} variable. They are added right
1235before @code{make} begins to parse them.
1236
1237This means that if the first thing a makefile does is examine the last
1238word in this variable, it will be the name of the current makefile.
1239Once the current makefile has used @code{include}, however, the last
1240word will be the just-included makefile.
1241
1242If a makefile named @code{Makefile} has this content:
1243
1244@example
1245@group
1246name1 := $(lastword $(MAKEFILE_LIST))
1247
1248include inc.mk
1249
1250name2 := $(lastword $(MAKEFILE_LIST))
1251
1252all:
1253 @@echo name1 = $(name1)
1254 @@echo name2 = $(name2)
1255@end group
1256@end example
1257
1258@noindent
1259then you would expect to see this output:
1260
1261@example
1262@group
1263name1 = Makefile
1264name2 = inc.mk
1265@end group
1266@end example
1267
1268@xref{Text Functions}, for more information on the @code{word} and
1269@code{words} functions used above. @xref{Flavors, The Two Flavors of
1270Variables}, for more information on simply-expanded (@code{:=})
1271variable definitions.
1272
1273@node Special Variables, Remaking Makefiles, MAKEFILE_LIST Variable, Makefiles
1274@comment node-name, next, previous, up
1275@section Other Special Variables
1276@cindex makefiles, and special variables
1277@cindex special variables
1278
1279GNU @code{make} also supports other special variables. Unless
1280otherwise documented here, these values lose their special properties
1281if they are set by a makefile or on the command line.
1282
1283@table @code
1284
1285@vindex .DEFAULT_GOAL @r{(define default goal)}
1286@item .DEFAULT_GOAL
1287Sets the default goal to be used if no targets were specified on the
1288command line (@pxref{Goals, , Arguments to Specify the Goals}). The
1289@code{.DEFAULT_GOAL} variable allows you to discover the current
1290default goal, restart the default goal selection algorithm by clearing
1291its value, or to explicitly set the default goal. The following
1292example illustrates these cases:
1293
1294@example
1295@group
1296# Query the default goal.
1297ifeq ($(.DEFAULT_GOAL),)
1298 $(warning no default goal is set)
1299endif
1300
1301.PHONY: foo
1302foo: ; @@echo $@@
1303
1304$(warning default goal is $(.DEFAULT_GOAL))
1305
1306# Reset the default goal.
1307.DEFAULT_GOAL :=
1308
1309.PHONY: bar
1310bar: ; @@echo $@@
1311
1312$(warning default goal is $(.DEFAULT_GOAL))
1313
1314# Set our own.
1315.DEFAULT_GOAL := foo
1316@end group
1317@end example
1318
1319This makefile prints:
1320
1321@example
1322@group
1323no default goal is set
1324default goal is foo
1325default goal is bar
1326foo
1327@end group
1328@end example
1329
1330Note that assigning more than one target name to @code{.DEFAULT_GOAL} is
1331illegal and will result in an error.
1332
1333@vindex .VARIABLES @r{(list of variables)}
1334@item .VARIABLES
1335Expands to a list of the @emph{names} of all global variables defined
1336so far. This includes variables which have empty values, as well as
1337built-in variables (@pxref{Implicit Variables, , Variables Used by
1338Implicit Rules}), but does not include any variables which are only
1339defined in a target-specific context. Note that any value you assign
1340to this variable will be ignored; it will always return its special
1341value.
1342
1343@c @vindex .TARGETS @r{(list of targets)}
1344@c @item .TARGETS
1345@c The second special variable is @code{.TARGETS}. When expanded, the
1346@c value consists of a list of all targets defined in all makefiles read
1347@c up until that point. Note it's not enough for a file to be simply
1348@c mentioned in the makefile to be listed in this variable, even if it
1349@c would match an implicit rule and become an ``implicit target''. The
1350@c file must appear as a target, on the left-hand side of a ``:'', to be
1351@c considered a target for the purposes of this variable.
1352
1353@vindex .FEATURES @r{(list of supported features)}
1354@item .FEATURES
1355Expands to a list of special features supported by this version of
1356@code{make}. Possible values include:
1357
1358@table @samp
1359@item target-specific
1360Supports target-specific and pattern-specific variable assignments.
1361@xref{Target-specific, ,Target-specific Variable Values}.
1362
1363@item order-only
1364Supports order-only prerequisites. @xref{Prerequisite Types, ,Types
1365of Prerequisites}.
1366
1367@item second-expansion
1368Supports secondary expansion of prerequisite lists.
1369
1370@item jobserver
1371Supports ``job server'' enhanced parallel builds. @xref{Parallel,
1372,Parallel Execution}.
1373
1374@item check-symlink
1375Supports the @code{-L} (@code{--check-symlink-times}) flag.
1376@xref{Options Summary, ,Summary of Options}.
1377
1378@end table
1379
1380@end table
1381
1382@node Remaking Makefiles, Overriding Makefiles, Special Variables, Makefiles
1383@section How Makefiles Are Remade
1384
1385@cindex updating makefiles
1386@cindex remaking makefiles
1387@cindex makefile, remaking of
1388Sometimes makefiles can be remade from other files, such as RCS or SCCS
1389files. If a makefile can be remade from other files, you probably want
1390@code{make} to get an up-to-date version of the makefile to read in.
1391
1392To this end, after reading in all makefiles, @code{make} will consider
1393each as a goal target and attempt to update it. If a makefile has a
1394rule which says how to update it (found either in that very makefile or
1395in another one) or if an implicit rule applies to it (@pxref{Implicit
1396Rules, ,Using Implicit Rules}), it will be updated if necessary. After
1397all makefiles have been checked, if any have actually been changed,
1398@code{make} starts with a clean slate and reads all the makefiles over
1399again. (It will also attempt to update each of them over again, but
1400normally this will not change them again, since they are already up to
1401date.)@refill
1402
1403If you know that one or more of your makefiles cannot be remade and you
1404want to keep @code{make} from performing an implicit rule search on
1405them, perhaps for efficiency reasons, you can use any normal method of
1406preventing implicit rule lookup to do so. For example, you can write an
1407explicit rule with the makefile as the target, and an empty command
1408string (@pxref{Empty Commands, ,Using Empty Commands}).
1409
1410If the makefiles specify a double-colon rule to remake a file with
1411commands but no prerequisites, that file will always be remade
1412(@pxref{Double-Colon}). In the case of makefiles, a makefile that has a
1413double-colon rule with commands but no prerequisites will be remade every
1414time @code{make} is run, and then again after @code{make} starts over
1415and reads the makefiles in again. This would cause an infinite loop:
1416@code{make} would constantly remake the makefile, and never do anything
1417else. So, to avoid this, @code{make} will @strong{not} attempt to
1418remake makefiles which are specified as targets of a double-colon rule
1419with commands but no prerequisites.@refill
1420
1421If you do not specify any makefiles to be read with @samp{-f} or
1422@samp{--file} options, @code{make} will try the default makefile names;
1423@pxref{Makefile Names, ,What Name to Give Your Makefile}. Unlike
1424makefiles explicitly requested with @samp{-f} or @samp{--file} options,
1425@code{make} is not certain that these makefiles should exist. However,
1426if a default makefile does not exist but can be created by running
1427@code{make} rules, you probably want the rules to be run so that the
1428makefile can be used.
1429
1430Therefore, if none of the default makefiles exists, @code{make} will try
1431to make each of them in the same order in which they are searched for
1432(@pxref{Makefile Names, ,What Name to Give Your Makefile})
1433until it succeeds in making one, or it runs out of names to try. Note
1434that it is not an error if @code{make} cannot find or make any makefile;
1435a makefile is not always necessary.@refill
1436
1437When you use the @samp{-t} or @samp{--touch} option
1438(@pxref{Instead of Execution, ,Instead of Executing the Commands}),
1439you would not want to use an out-of-date makefile to decide which
1440targets to touch. So the @samp{-t} option has no effect on updating
1441makefiles; they are really updated even if @samp{-t} is specified.
1442Likewise, @samp{-q} (or @samp{--question}) and @samp{-n} (or
1443@samp{--just-print}) do not prevent updating of makefiles, because an
1444out-of-date makefile would result in the wrong output for other targets.
1445Thus, @samp{make -f mfile -n foo} will update @file{mfile}, read it in,
1446and then print the commands to update @file{foo} and its prerequisites
1447without running them. The commands printed for @file{foo} will be those
1448specified in the updated contents of @file{mfile}.
1449
1450However, on occasion you might actually wish to prevent updating of even
1451the makefiles. You can do this by specifying the makefiles as goals in
1452the command line as well as specifying them as makefiles. When the
1453makefile name is specified explicitly as a goal, the options @samp{-t}
1454and so on do apply to them.
1455
1456Thus, @samp{make -f mfile -n mfile foo} would read the makefile
1457@file{mfile}, print the commands needed to update it without actually
1458running them, and then print the commands needed to update @file{foo}
1459without running them. The commands for @file{foo} will be those
1460specified by the existing contents of @file{mfile}.
1461
1462@node Overriding Makefiles, Reading Makefiles, Remaking Makefiles, Makefiles
1463@section Overriding Part of Another Makefile
1464
1465@cindex overriding makefiles
1466@cindex makefile, overriding
1467Sometimes it is useful to have a makefile that is mostly just like
1468another makefile. You can often use the @samp{include} directive to
1469include one in the other, and add more targets or variable definitions.
1470However, if the two makefiles give different commands for the same
1471target, @code{make} will not let you just do this. But there is another way.
1472
1473@cindex match-anything rule, used to override
1474In the containing makefile (the one that wants to include the other),
1475you can use a match-anything pattern rule to say that to remake any
1476target that cannot be made from the information in the containing
1477makefile, @code{make} should look in another makefile.
1478@xref{Pattern Rules}, for more information on pattern rules.
1479
1480For example, if you have a makefile called @file{Makefile} that says how
1481to make the target @samp{foo} (and other targets), you can write a
1482makefile called @file{GNUmakefile} that contains:
1483
1484@example
1485foo:
1486 frobnicate > foo
1487
1488%: force
1489 @@$(MAKE) -f Makefile $@@
1490force: ;
1491@end example
1492
1493If you say @samp{make foo}, @code{make} will find @file{GNUmakefile},
1494read it, and see that to make @file{foo}, it needs to run the command
1495@samp{frobnicate > foo}. If you say @samp{make bar}, @code{make} will
1496find no way to make @file{bar} in @file{GNUmakefile}, so it will use the
1497commands from the pattern rule: @samp{make -f Makefile bar}. If
1498@file{Makefile} provides a rule for updating @file{bar}, @code{make}
1499will apply the rule. And likewise for any other target that
1500@file{GNUmakefile} does not say how to make.
1501
1502The way this works is that the pattern rule has a pattern of just
1503@samp{%}, so it matches any target whatever. The rule specifies a
1504prerequisite @file{force}, to guarantee that the commands will be run even
1505if the target file already exists. We give @file{force} target empty
1506commands to prevent @code{make} from searching for an implicit rule to
1507build it---otherwise it would apply the same match-anything rule to
1508@file{force} itself and create a prerequisite loop!
1509
1510@node Reading Makefiles, Secondary Expansion, Overriding Makefiles, Makefiles
1511@section How @code{make} Reads a Makefile
1512@cindex reading makefiles
1513@cindex makefile, parsing
1514
1515GNU @code{make} does its work in two distinct phases. During the first
1516phase it reads all the makefiles, included makefiles, etc. and
1517internalizes all the variables and their values, implicit and explicit
1518rules, and constructs a dependency graph of all the targets and their
1519prerequisites. During the second phase, @code{make} uses these internal
1520structures to determine what targets will need to be rebuilt and to
1521invoke the rules necessary to do so.
1522
1523It's important to understand this two-phase approach because it has a
1524direct impact on how variable and function expansion happens; this is
1525often a source of some confusion when writing makefiles. Here we will
1526present a summary of the phases in which expansion happens for different
1527constructs within the makefile. We say that expansion is
1528@dfn{immediate} if it happens during the first phase: in this case
1529@code{make} will expand any variables or functions in that section of a
1530construct as the makefile is parsed. We say that expansion is
1531@dfn{deferred} if expansion is not performed immediately. Expansion of
1532deferred construct is not performed until either the construct appears
1533later in an immediate context, or until the second phase.
1534
1535You may not be familiar with some of these constructs yet. You can
1536reference this section as you become familiar with them, in later
1537chapters.
1538
1539@subheading Variable Assignment
1540@cindex +=, expansion
1541@cindex =, expansion
1542@cindex ?=, expansion
1543@cindex +=, expansion
1544@cindex define, expansion
1545
1546Variable definitions are parsed as follows:
1547
1548@example
1549@var{immediate} = @var{deferred}
1550@var{immediate} ?= @var{deferred}
1551@var{immediate} := @var{immediate}
1552@var{immediate} += @var{deferred} or @var{immediate}
1553
1554define @var{immediate}
1555 @var{deferred}
1556endef
1557@end example
1558
1559For the append operator, @samp{+=}, the right-hand side is considered
1560immediate if the variable was previously set as a simple variable
1561(@samp{:=}), and deferred otherwise.
1562
1563@subheading Conditional Statements
1564@cindex ifdef, expansion
1565@cindex ifeq, expansion
1566@cindex ifndef, expansion
1567@cindex ifneq, expansion
1568
1569All instances of conditional syntax are parsed immediately, in their
1570entirety; this includes the @code{ifdef}, @code{ifeq}, @code{ifndef},
1571and @code{ifneq} forms. Of course this means that automatic variables
1572cannot be used in conditional statements, as automatic variables are
1573not set until the command script for that rule is invoked. If you
1574need to use automatic variables in a conditional you @emph{must} use
1575shell conditional syntax, in your command script proper, for these
1576tests, not @code{make} conditionals.
1577
1578@subheading Rule Definition
1579@cindex target, expansion
1580@cindex prerequisite, expansion
1581@cindex implicit rule, expansion
1582@cindex pattern rule, expansion
1583@cindex explicit rule, expansion
1584
1585A rule is always expanded the same way, regardless of the form:
1586
1587@example
1588@var{immediate} : @var{immediate} ; @var{deferred}
1589 @var{deferred}
1590@end example
1591
1592That is, the target and prerequisite sections are expanded immediately,
1593and the commands used to construct the target are always deferred. This
1594general rule is true for explicit rules, pattern rules, suffix rules,
1595static pattern rules, and simple prerequisite definitions.
1596
1597@node Secondary Expansion, , Reading Makefiles, Makefiles
1598@section Secondary Expansion
1599@cindex secondary expansion
1600@cindex expansion, secondary
1601
1602In the previous section we learned that GNU @code{make} works in two
1603distinct phases: a read-in phase and a target-update phase
1604(@pxref{Reading Makefiles, , How @code{make} Reads a Makefile}).
1605There is an extra wrinkle that comes in between those two phases,
1606right at the end of the read-in phase: at that time, all the
1607prerequisites of all of the targets are expanded a @emph{second time}.
1608In most circumstances this secondary expansion will have no effect,
1609since all variable and function references will have been expanded
1610during the initial parsing of the makefiles. In order to take
1611advantage of the secondary expansion phase of the parser, then, it's
1612necessary to @emph{escape} the variable or function reference in the
1613makefile. In this case the first expansion merely un-escapes the
1614reference but doesn't expand it, and expansion is left to the
1615secondary expansion phase. For example, consider this makefile:
1616
1617@example
1618ONEVAR = onefile
1619TWOVAR = twofile
1620myfile: $(ONEVAR) $$(TWOVAR)
1621@end example
1622
1623After the first expansion phase the prerequisites list of the
1624@file{myfile} target will be @code{onefile} and @code{$(TWOVAR)}; the
1625first (unescaped) variable reference to @var{ONEVAR} is expanded,
1626while the second (escaped) variable reference is simply unescaped,
1627without being recognized as a variable reference. Now during the
1628secondary expansion the first word is expanded again but since it
1629contains no variable or function references it remains the static
1630value @file{onefile}, while the second word is now a normal reference
1631to the variable @var{TWOVAR}, which is expanded to the value
1632@file{twofile}. The final result is that there are two prerequisites,
1633@file{onefile} and @file{twofile}.
1634
1635Obviously, this is not a very interesting case since the same result
1636could more easily have been achieved simply by having both variables
1637appear, unescaped, in the prerequisites list. One difference becomes
1638apparent if the variables are reset; consider this example:
1639
1640@example
1641AVAR = top
1642onefile: $(AVAR)
1643twofile: $$(AVAR)
1644AVAR = bottom
1645@end example
1646
1647Here the prerequisite of @file{onefile} will be expanded immediately,
1648and resolve to the value @file{top}, while the prerequisite of
1649@file{twofile} will not be full expanded until the secondary expansion
1650and yield a value of @file{bottom}.
1651
1652This is marginally more exciting, but the true power of this feature
1653only becomes apparent when you discover that secondary expansions
1654always take place within the scope of the automatic variables for that
1655target. This means that you can use variables such as @code{$@@},
1656@code{$*}, etc. during the second expansion and they will have their
1657expected values, just as in the command script. All you have to do is
1658defer the expansion by escaping the @code{$}. Also, secondary
1659expansion occurs for both explicit and implicit (pattern) rules.
1660Knowing this, the possible uses for this feature are almost endless.
1661For example:
1662
1663@example
1664main_OBJS := main.o try.o test.o
1665lib_OBJS := lib.o api.o
1666
1667main lib: $$($$@@_OBJS)
1668@end example
1669
1670Here, after the initial expansion the prerequisites of both the
1671@file{main} and @file{lib} targets will be @code{$($@@_OBJS)}. During
1672the secondary expansion, the @code{$@@} variable is set to the name of
1673the target and so the expansion for the @file{main} target will yield
1674@code{$(main_OBJS)}, or @code{main.o try.o test.o}, while the
1675secondary expansion for the @file{lib} target will yield
1676@code{$(lib_OBJS)}, or @code{lib.o api.o}.
1677
1678You can also mix functions here, as long as they are properly escaped:
1679
1680@example
1681main_SRCS := main.c try.c test.c
1682lib_SRCS := lib.c api.c
1683
1684main lib: $$(patsubst %.c,%.o,$$($$@@_SRCS))
1685@end example
1686
1687This version allows users to specify source files rather than object
1688files, but gives the same resulting prerequisites list as the previous
1689example.
1690
1691Evaluation of automatic variables during the secondary expansion
1692phase, especially of the target name variable @code{$$@@}, behaves
1693similarly to evaluation within command scripts. However, there are
1694some subtle differences and ``corner cases'' which come into play for
1695the different types of rule definitions that @code{make} understands.
1696The subtleties of using the different automatic variables are
1697described below.
1698
1699@subheading Secondary Expansion of Explicit Rules
1700@cindex secondary expansion and explicit rules
1701@cindex explicit rules, secondary expansion of
1702
1703During the secondary expansion of explicit rules, @code{$$@@} and
1704@code{$$%} evaluate, respectively, to the file name of the target and,
1705when the target is an archive member, the target member name. The
1706@code{$$<} variable evaluates to the first prerequisite in the first
1707rule for this target. @code{$$^} and @code{$$+} evaluate to the list
1708of all prerequisites of rules @emph{that have already appeared} for
1709the same target (@code{$$+} with repetitions and @code{$$^}
1710without). The following example will help illustrate these behaviors:
1711
1712@example
1713foo: foo.1 bar.1 $$< $$^ $$+ # line #1
1714
1715foo: foo.2 bar.2 $$< $$^ $$+ # line #2
1716
1717foo: foo.3 bar.3 $$< $$^ $$+ # line #3
1718@end example
1719
1720For the first line, all three variables (@code{$$<}, @code{$$^}, and
1721@code{$$+}) expand to the empty string. For the second line, they will
1722have values @code{foo.1}, @code{foo.1 bar.1}, and @code{foo.1 bar.1}
1723respectively. For the third they will have values @code{foo.1},
1724@code{foo.1 bar.1 foo.2 bar.2}, and @code{foo.1 bar.1 foo.2 bar.2}
1725respectively.
1726
1727Rules undergo secondary expansion in makefile order, except that
1728the rule with the command script is always evaluated last.
1729
1730The variables @code{$$?} and @code{$$*} are not available and expand
1731to the empty string.
1732
1733@subheading Secondary Expansion of Static Pattern Rules
1734@cindex secondary expansion and static pattern rules
1735@cindex static pattern rules, secondary expansion of
1736
1737Rules for secondary expansion of static pattern rules are identical to
1738those for explicit rules, above, with one exception: for static
1739pattern rules the @code{$$*} variable is set to the pattern stem. As
1740with explicit rules, @code{$$?} is not available and expands to the
1741empty string.
1742
1743@subheading Secondary Expansion of Implicit Rules
1744@cindex secondary expansion and implicit rules
1745@cindex implicit rules, secondary expansion of
1746
1747As @code{make} searches for an implicit rule, it substitutes the stem
1748and then performs secondary expansion for every rule with a matching
1749target pattern. The value of the automatic variables is derived in
1750the same fashion as for static pattern rules. As an example:
1751
1752@example
1753foo: bar
1754
1755foo foz: fo%: bo%
1756
1757%oo: $$< $$^ $$+ $$*
1758@end example
1759
1760When the implicit rule is tried for target @file{foo}, @code{$$<}
1761expands to @file{bar}, @code{$$^} expands to @file{bar boo},
1762@code{$$+} also expands to @file{bar boo}, and @code{$$*} expands to
1763@file{f}.
1764
1765Note that the directory prefix (D), as described in @ref{Implicit Rule
1766Search, ,Implicit Rule Search Algorithm}, is appended (after
1767expansion) to all the patterns in the prerequisites list. As an
1768example:
1769
1770@example
1771/tmp/foo.o:
1772
1773%.o: $$(addsuffix /%.c,foo bar) foo.h
1774@end example
1775
1776The prerequisite list after the secondary expansion and directory
1777prefix reconstruction will be @file{/tmp/foo/foo.c /tmp/var/bar/foo.c
1778foo.h}. If you are not interested in this reconstruction, you can use
1779@code{$$*} instead of @code{%} in the prerequisites list.
1780
1781@node Rules, Commands, Makefiles, Top
1782@chapter Writing Rules
1783@cindex writing rules
1784@cindex rule, how to write
1785@cindex target
1786@cindex prerequisite
1787
1788A @dfn{rule} appears in the makefile and says when and how to remake
1789certain files, called the rule's @dfn{targets} (most often only one per rule).
1790It lists the other files that are the @dfn{prerequisites} of the target, and
1791@dfn{commands} to use to create or update the target.
1792
1793@cindex default goal
1794@cindex goal, default
1795The order of rules is not significant, except for determining the
1796@dfn{default goal}: the target for @code{make} to consider, if you do
1797not otherwise specify one. The default goal is the target of the first
1798rule in the first makefile. If the first rule has multiple targets,
1799only the first target is taken as the default. There are two
1800exceptions: a target starting with a period is not a default unless it
1801contains one or more slashes, @samp{/}, as well; and, a target that
1802defines a pattern rule has no effect on the default goal.
1803(@xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.)
1804
1805Therefore, we usually write the makefile so that the first rule is the
1806one for compiling the entire program or all the programs described by
1807the makefile (often with a target called @samp{all}).
1808@xref{Goals, ,Arguments to Specify the Goals}.
1809
1810@menu
1811* Rule Example:: An example explained.
1812* Rule Syntax:: General syntax explained.
1813* Prerequisite Types:: There are two types of prerequisites.
1814* Wildcards:: Using wildcard characters such as `*'.
1815* Directory Search:: Searching other directories for source files.
1816* Phony Targets:: Using a target that is not a real file's name.
1817* Force Targets:: You can use a target without commands
1818 or prerequisites to mark other
1819 targets as phony.
1820* Empty Targets:: When only the date matters and the
1821 files are empty.
1822* Special Targets:: Targets with special built-in meanings.
1823* Multiple Targets:: When to make use of several targets in a rule.
1824* Multiple Rules:: How to use several rules with the same target.
1825* Static Pattern:: Static pattern rules apply to multiple targets
1826 and can vary the prerequisites according to
1827 the target name.
1828* Double-Colon:: How to use a special kind of rule to allow
1829 several independent rules for one target.
1830* Automatic Prerequisites:: How to automatically generate rules giving
1831 prerequisites from source files themselves.
1832@end menu
1833
1834@ifnottex
1835@node Rule Example, Rule Syntax, Rules, Rules
1836@section Rule Example
1837
1838Here is an example of a rule:
1839
1840@example
1841foo.o : foo.c defs.h # module for twiddling the frobs
1842 cc -c -g foo.c
1843@end example
1844
1845Its target is @file{foo.o} and its prerequisites are @file{foo.c} and
1846@file{defs.h}. It has one command, which is @samp{cc -c -g foo.c}.
1847The command line starts with a tab to identify it as a command.
1848
1849This rule says two things:
1850
1851@itemize @bullet
1852@item
1853How to decide whether @file{foo.o} is out of date: it is out of date
1854if it does not exist, or if either @file{foo.c} or @file{defs.h} is
1855more recent than it.
1856
1857@item
1858How to update the file @file{foo.o}: by running @code{cc} as stated.
1859The command does not explicitly mention @file{defs.h}, but we presume
1860that @file{foo.c} includes it, and that that is why @file{defs.h} was
1861added to the prerequisites.
1862@end itemize
1863@end ifnottex
1864
1865@node Rule Syntax, Prerequisite Types, Rule Example, Rules
1866@section Rule Syntax
1867
1868@cindex rule syntax
1869@cindex syntax of rules
1870In general, a rule looks like this:
1871
1872@example
1873@var{targets} : @var{prerequisites}
1874 @var{command}
1875 @dots{}
1876@end example
1877
1878@noindent
1879or like this:
1880
1881@example
1882@var{targets} : @var{prerequisites} ; @var{command}
1883 @var{command}
1884 @dots{}
1885@end example
1886
1887@cindex targets
1888@cindex rule targets
1889The @var{targets} are file names, separated by spaces. Wildcard
1890characters may be used (@pxref{Wildcards, ,Using Wildcard Characters
1891in File Names}) and a name of the form @file{@var{a}(@var{m})}
1892represents member @var{m} in archive file @var{a}
1893(@pxref{Archive Members, ,Archive Members as Targets}).
1894Usually there is only one
1895target per rule, but occasionally there is a reason to have more
1896(@pxref{Multiple Targets, , Multiple Targets in a Rule}).@refill
1897
1898@cindex commands
1899@cindex tab character (in commands)
1900The @var{command} lines start with a tab character. The first command may
1901appear on the line after the prerequisites, with a tab character, or may
1902appear on the same line, with a semicolon. Either way, the effect is the
1903same. @xref{Commands, ,Writing the Commands in Rules}.
1904
1905@cindex dollar sign (@code{$}), in rules
1906@cindex @code{$}, in rules
1907@cindex rule, and @code{$}
1908Because dollar signs are used to start variable references, if you really
1909want a dollar sign in a rule you must write two of them, @samp{$$}
1910(@pxref{Using Variables, ,How to Use Variables}). In prerequisite
1911lists you must actually write @emph{four} dollar signs (@samp{$$$$}),
1912due to secondary expansion (@pxref{Secondary Expansion}).
1913You may split a long line by inserting a backslash
1914followed by a newline, but this is not required, as @code{make} places no
1915limit on the length of a line in a makefile.
1916
1917A rule tells @code{make} two things: when the targets are out of date,
1918and how to update them when necessary.
1919
1920@cindex prerequisites
1921@cindex rule prerequisites
1922The criterion for being out of date is specified in terms of the
1923@var{prerequisites}, which consist of file names separated by spaces.
1924(Wildcards and archive members (@pxref{Archives}) are allowed here too.)
1925A target is out of date if it does not exist or if it is older than any
1926of the prerequisites (by comparison of last-modification times). The
1927idea is that the contents of the target file are computed based on
1928information in the prerequisites, so if any of the prerequisites changes,
1929the contents of the existing target file are no longer necessarily
1930valid.
1931
1932How to update is specified by @var{commands}. These are lines to be
1933executed by the shell (normally @samp{sh}), but with some extra features
1934(@pxref{Commands, ,Writing the Commands in Rules}).
1935
1936@node Prerequisite Types, Wildcards, Rule Syntax, Rules
1937@comment node-name, next, previous, up
1938@section Types of Prerequisites
1939@cindex prerequisite types
1940@cindex types of prerequisites
1941
1942@cindex prerequisites, normal
1943@cindex normal prerequisites
1944@cindex prerequisites, order-only
1945@cindex order-only prerequisites
1946There are actually two different types of prerequisites understood by
1947GNU @code{make}: normal prerequisites such as described in the
1948previous section, and @dfn{order-only} prerequisites. A normal
1949prerequisite makes two statements: first, it imposes an order of
1950execution of build commands: any commands necessary to build any of a
1951target's prerequisites will be fully executed before any commands
1952necessary to build the target. Second, it imposes a dependency
1953relationship: if any prerequisite is newer than the target, then the
1954target is considered out-of-date and must be rebuilt.
1955
1956Normally, this is exactly what you want: if a target's prerequisite is
1957updated, then the target should also be updated.
1958
1959Occasionally, however, you have a situation where you want to impose a
1960specific ordering on the rules to be invoked @emph{without} forcing
1961the target to be updated if one of those rules is executed. In that
1962case, you want to define @dfn{order-only} prerequisites. Order-only
1963prerequisites can be specified by placing a pipe symbol (@code{|})
1964in the prerequisites list: any prerequisites to the left of the pipe
1965symbol are normal; any prerequisites to the right are order-only:
1966
1967@example
1968@var{targets} : @var{normal-prerequisites} | @var{order-only-prerequisites}
1969@end example
1970
1971The normal prerequisites section may of course be empty. Also, you
1972may still declare multiple lines of prerequisites for the same target:
1973they are appended appropriately. Note that if you declare the same
1974file to be both a normal and an order-only prerequisite, the normal
1975prerequisite takes precedence (since they are a strict superset of the
1976behavior of an order-only prerequisite).
1977
1978@node Wildcards, Directory Search, Prerequisite Types, Rules
1979@section Using Wildcard Characters in File Names
1980@cindex wildcard
1981@cindex file name with wildcards
1982@cindex globbing (wildcards)
1983
1984@cindex @code{*} (wildcard character)
1985@cindex @code{?} (wildcard character)
1986@cindex @code{[@dots{}]} (wildcard characters)
1987A single file name can specify many files using @dfn{wildcard characters}.
1988The wildcard characters in @code{make} are @samp{*}, @samp{?} and
1989@samp{[@dots{}]}, the same as in the Bourne shell. For example, @file{*.c}
1990specifies a list of all the files (in the working directory) whose names
1991end in @samp{.c}.@refill
1992
1993@cindex @code{~} (tilde)
1994@cindex tilde (@code{~})
1995@cindex home directory
1996The character @samp{~} at the beginning of a file name also has special
1997significance. If alone, or followed by a slash, it represents your home
1998directory. For example @file{~/bin} expands to @file{/home/you/bin}.
1999If the @samp{~} is followed by a word, the string represents the home
2000directory of the user named by that word. For example @file{~john/bin}
2001expands to @file{/home/john/bin}. On systems which don't have a home
2002directory for each user (such as MS-DOS or MS-Windows), this
2003functionality can be simulated by setting the environment variable
2004@var{HOME}.@refill
2005
2006Wildcard expansion happens automatically in targets, in prerequisites,
2007and in commands (where the shell does the expansion). In other
2008contexts, wildcard expansion happens only if you request it explicitly
2009with the @code{wildcard} function.
2010
2011The special significance of a wildcard character can be turned off by
2012preceding it with a backslash. Thus, @file{foo\*bar} would refer to a
2013specific file whose name consists of @samp{foo}, an asterisk, and
2014@samp{bar}.@refill
2015
2016@menu
2017* Wildcard Examples:: Several examples
2018* Wildcard Pitfall:: Problems to avoid.
2019* Wildcard Function:: How to cause wildcard expansion where
2020 it does not normally take place.
2021@end menu
2022
2023@node Wildcard Examples, Wildcard Pitfall, Wildcards, Wildcards
2024@subsection Wildcard Examples
2025
2026Wildcards can be used in the commands of a rule, where they are expanded
2027by the shell. For example, here is a rule to delete all the object files:
2028
2029@example
2030@group
2031clean:
2032 rm -f *.o
2033@end group
2034@end example
2035@cindex @code{rm} (shell command)
2036
2037Wildcards are also useful in the prerequisites of a rule. With the
2038following rule in the makefile, @samp{make print} will print all the
2039@samp{.c} files that have changed since the last time you printed them:
2040
2041@example
2042print: *.c
2043 lpr -p $?
2044 touch print
2045@end example
2046
2047@cindex @code{print} target
2048@cindex @code{lpr} (shell command)
2049@cindex @code{touch} (shell command)
2050@noindent
2051This rule uses @file{print} as an empty target file; see @ref{Empty
2052Targets, ,Empty Target Files to Record Events}. (The automatic variable
2053@samp{$?} is used to print only those files that have changed; see
2054@ref{Automatic Variables}.)@refill
2055
2056Wildcard expansion does not happen when you define a variable. Thus, if
2057you write this:
2058
2059@example
2060objects = *.o
2061@end example
2062
2063@noindent
2064then the value of the variable @code{objects} is the actual string
2065@samp{*.o}. However, if you use the value of @code{objects} in a target,
2066prerequisite or command, wildcard expansion will take place at that time.
2067To set @code{objects} to the expansion, instead use:
2068
2069@example
2070objects := $(wildcard *.o)
2071@end example
2072
2073@noindent
2074@xref{Wildcard Function}.
2075
2076@node Wildcard Pitfall, Wildcard Function, Wildcard Examples, Wildcards
2077@subsection Pitfalls of Using Wildcards
2078@cindex wildcard pitfalls
2079@cindex pitfalls of wildcards
2080@cindex mistakes with wildcards
2081@cindex errors with wildcards
2082@cindex problems with wildcards
2083
2084Now here is an example of a naive way of using wildcard expansion, that
2085does not do what you would intend. Suppose you would like to say that the
2086executable file @file{foo} is made from all the object files in the
2087directory, and you write this:
2088
2089@example
2090objects = *.o
2091
2092foo : $(objects)
2093 cc -o foo $(CFLAGS) $(objects)
2094@end example
2095
2096@noindent
2097The value of @code{objects} is the actual string @samp{*.o}. Wildcard
2098expansion happens in the rule for @file{foo}, so that each @emph{existing}
2099@samp{.o} file becomes a prerequisite of @file{foo} and will be recompiled if
2100necessary.
2101
2102But what if you delete all the @samp{.o} files? When a wildcard matches
2103no files, it is left as it is, so then @file{foo} will depend on the
2104oddly-named file @file{*.o}. Since no such file is likely to exist,
2105@code{make} will give you an error saying it cannot figure out how to
2106make @file{*.o}. This is not what you want!
2107
2108Actually it is possible to obtain the desired result with wildcard
2109expansion, but you need more sophisticated techniques, including the
2110@code{wildcard} function and string substitution.
2111@ifnottex
2112@xref{Wildcard Function, ,The Function @code{wildcard}}.
2113@end ifnottex
2114@iftex
2115These are described in the following section.
2116@end iftex
2117
2118@cindex wildcards and MS-DOS/MS-Windows backslashes
2119@cindex backslashes in pathnames and wildcard expansion
2120
2121Microsoft operating systems (MS-DOS and MS-Windows) use backslashes to
2122separate directories in pathnames, like so:
2123
2124@example
2125 c:\foo\bar\baz.c
2126@end example
2127
2128This is equivalent to the Unix-style @file{c:/foo/bar/baz.c} (the
2129@file{c:} part is the so-called drive letter). When @code{make} runs on
2130these systems, it supports backslashes as well as the Unix-style forward
2131slashes in pathnames. However, this support does @emph{not} include the
2132wildcard expansion, where backslash is a quote character. Therefore,
2133you @emph{must} use Unix-style slashes in these cases.
2134
2135
2136@node Wildcard Function, , Wildcard Pitfall, Wildcards
2137@subsection The Function @code{wildcard}
2138@findex wildcard
2139
2140Wildcard expansion happens automatically in rules. But wildcard expansion
2141does not normally take place when a variable is set, or inside the
2142arguments of a function. If you want to do wildcard expansion in such
2143places, you need to use the @code{wildcard} function, like this:
2144
2145@example
2146$(wildcard @var{pattern}@dots{})
2147@end example
2148
2149@noindent
2150This string, used anywhere in a makefile, is replaced by a
2151space-separated list of names of existing files that match one of the
2152given file name patterns. If no existing file name matches a pattern,
2153then that pattern is omitted from the output of the @code{wildcard}
2154function. Note that this is different from how unmatched wildcards
2155behave in rules, where they are used verbatim rather than ignored
2156(@pxref{Wildcard Pitfall}).
2157
2158One use of the @code{wildcard} function is to get a list of all the C source
2159files in a directory, like this:
2160
2161@example
2162$(wildcard *.c)
2163@end example
2164
2165We can change the list of C source files into a list of object files by
2166replacing the @samp{.c} suffix with @samp{.o} in the result, like this:
2167
2168@example
2169$(patsubst %.c,%.o,$(wildcard *.c))
2170@end example
2171
2172@noindent
2173(Here we have used another function, @code{patsubst}.
2174@xref{Text Functions, ,Functions for String Substitution and Analysis}.)@refill
2175
2176Thus, a makefile to compile all C source files in the directory and then
2177link them together could be written as follows:
2178
2179@example
2180objects := $(patsubst %.c,%.o,$(wildcard *.c))
2181
2182foo : $(objects)
2183 cc -o foo $(objects)
2184@end example
2185
2186@noindent
2187(This takes advantage of the implicit rule for compiling C programs, so
2188there is no need to write explicit rules for compiling the files.
2189@xref{Flavors, ,The Two Flavors of Variables}, for an explanation of
2190@samp{:=}, which is a variant of @samp{=}.)
2191
2192@node Directory Search, Phony Targets, Wildcards, Rules
2193@section Searching Directories for Prerequisites
2194@vindex VPATH
2195@findex vpath
2196@cindex vpath
2197@cindex search path for prerequisites (@code{VPATH})
2198@cindex directory search (@code{VPATH})
2199
2200For large systems, it is often desirable to put sources in a separate
2201directory from the binaries. The @dfn{directory search} features of
2202@code{make} facilitate this by searching several directories
2203automatically to find a prerequisite. When you redistribute the files
2204among directories, you do not need to change the individual rules,
2205just the search paths.
2206
2207@menu
2208* General Search:: Specifying a search path that applies
2209 to every prerequisite.
2210* Selective Search:: Specifying a search path
2211 for a specified class of names.
2212* Search Algorithm:: When and how search paths are applied.
2213* Commands/Search:: How to write shell commands that work together
2214 with search paths.
2215* Implicit/Search:: How search paths affect implicit rules.
2216* Libraries/Search:: Directory search for link libraries.
2217@end menu
2218
2219@node General Search, Selective Search, Directory Search, Directory Search
2220@subsection @code{VPATH}: Search Path for All Prerequisites
2221@vindex VPATH
2222
2223The value of the @code{make} variable @code{VPATH} specifies a list of
2224directories that @code{make} should search. Most often, the
2225directories are expected to contain prerequisite files that are not in the
2226current directory; however, @code{make} uses @code{VPATH} as a search
2227list for both prerequisites and targets of rules.
2228
2229Thus, if a file that is listed as a target or prerequisite does not exist
2230in the current directory, @code{make} searches the directories listed in
2231@code{VPATH} for a file with that name. If a file is found in one of
2232them, that file may become the prerequisite (see below). Rules may then
2233specify the names of files in the prerequisite list as if they all
2234existed in the current directory. @xref{Commands/Search, ,Writing Shell
2235Commands with Directory Search}.
2236
2237In the @code{VPATH} variable, directory names are separated by colons or
2238blanks. The order in which directories are listed is the order followed
2239by @code{make} in its search. (On MS-DOS and MS-Windows, semi-colons
2240are used as separators of directory names in @code{VPATH}, since the
2241colon can be used in the pathname itself, after the drive letter.)
2242
2243For example,
2244
2245@example
2246VPATH = src:../headers
2247@end example
2248
2249@noindent
2250specifies a path containing two directories, @file{src} and
2251@file{../headers}, which @code{make} searches in that order.
2252
2253With this value of @code{VPATH}, the following rule,
2254
2255@example
2256foo.o : foo.c
2257@end example
2258
2259@noindent
2260is interpreted as if it were written like this:
2261
2262@example
2263foo.o : src/foo.c
2264@end example
2265
2266@noindent
2267assuming the file @file{foo.c} does not exist in the current directory but
2268is found in the directory @file{src}.
2269
2270@node Selective Search, Search Algorithm, General Search, Directory Search
2271@subsection The @code{vpath} Directive
2272@findex vpath
2273
2274Similar to the @code{VPATH} variable, but more selective, is the
2275@code{vpath} directive (note lower case), which allows you to specify a
2276search path for a particular class of file names: those that match a
2277particular pattern. Thus you can supply certain search directories for
2278one class of file names and other directories (or none) for other file
2279names.
2280
2281There are three forms of the @code{vpath} directive:
2282
2283@table @code
2284@item vpath @var{pattern} @var{directories}
2285Specify the search path @var{directories} for file names that match
2286@var{pattern}.
2287
2288The search path, @var{directories}, is a list of directories to be
2289searched, separated by colons (semi-colons on MS-DOS and MS-Windows) or
2290blanks, just like the search path used in the @code{VPATH} variable.
2291
2292@item vpath @var{pattern}
2293Clear out the search path associated with @var{pattern}.
2294
2295@c Extra blank line makes sure this gets two lines.
2296@item vpath
2297
2298Clear all search paths previously specified with @code{vpath} directives.
2299@end table
2300
2301A @code{vpath} pattern is a string containing a @samp{%} character. The
2302string must match the file name of a prerequisite that is being searched
2303for, the @samp{%} character matching any sequence of zero or more
2304characters (as in pattern rules; @pxref{Pattern Rules, ,Defining and
2305Redefining Pattern Rules}). For example, @code{%.h} matches files that
2306end in @code{.h}. (If there is no @samp{%}, the pattern must match the
2307prerequisite exactly, which is not useful very often.)
2308
2309@cindex @code{%}, quoting in @code{vpath}
2310@cindex @code{%}, quoting with @code{\} (backslash)
2311@cindex @code{\} (backslash), to quote @code{%}
2312@cindex backslash (@code{\}), to quote @code{%}
2313@cindex quoting @code{%}, in @code{vpath}
2314@samp{%} characters in a @code{vpath} directive's pattern can be quoted
2315with preceding backslashes (@samp{\}). Backslashes that would otherwise
2316quote @samp{%} characters can be quoted with more backslashes.
2317Backslashes that quote @samp{%} characters or other backslashes are
2318removed from the pattern before it is compared to file names. Backslashes
2319that are not in danger of quoting @samp{%} characters go unmolested.@refill
2320
2321When a prerequisite fails to exist in the current directory, if the
2322@var{pattern} in a @code{vpath} directive matches the name of the
2323prerequisite file, then the @var{directories} in that directive are searched
2324just like (and before) the directories in the @code{VPATH} variable.
2325
2326For example,
2327
2328@example
2329vpath %.h ../headers
2330@end example
2331
2332@noindent
2333tells @code{make} to look for any prerequisite whose name ends in @file{.h}
2334in the directory @file{../headers} if the file is not found in the current
2335directory.
2336
2337If several @code{vpath} patterns match the prerequisite file's name, then
2338@code{make} processes each matching @code{vpath} directive one by one,
2339searching all the directories mentioned in each directive. @code{make}
2340handles multiple @code{vpath} directives in the order in which they
2341appear in the makefile; multiple directives with the same pattern are
2342independent of each other.
2343
2344@need 750
2345Thus,
2346
2347@example
2348@group
2349vpath %.c foo
2350vpath % blish
2351vpath %.c bar
2352@end group
2353@end example
2354
2355@noindent
2356will look for a file ending in @samp{.c} in @file{foo}, then
2357@file{blish}, then @file{bar}, while
2358
2359@example
2360@group
2361vpath %.c foo:bar
2362vpath % blish
2363@end group
2364@end example
2365
2366@noindent
2367will look for a file ending in @samp{.c} in @file{foo}, then
2368@file{bar}, then @file{blish}.
2369
2370@node Search Algorithm, Commands/Search, Selective Search, Directory Search
2371@subsection How Directory Searches are Performed
2372@cindex algorithm for directory search
2373@cindex directory search algorithm
2374
2375When a prerequisite is found through directory search, regardless of type
2376(general or selective), the pathname located may not be the one that
2377@code{make} actually provides you in the prerequisite list. Sometimes
2378the path discovered through directory search is thrown away.
2379
2380The algorithm @code{make} uses to decide whether to keep or abandon a
2381path found via directory search is as follows:
2382
2383@enumerate
2384@item
2385If a target file does not exist at the path specified in the makefile,
2386directory search is performed.
2387
2388@item
2389If the directory search is successful, that path is kept and this file
2390is tentatively stored as the target.
2391
2392@item
2393All prerequisites of this target are examined using this same method.
2394
2395@item
2396After processing the prerequisites, the target may or may not need to be
2397rebuilt:
2398
2399@enumerate a
2400@item
2401If the target does @emph{not} need to be rebuilt, the path to the file
2402found during directory search is used for any prerequisite lists which
2403contain this target. In short, if @code{make} doesn't need to rebuild
2404the target then you use the path found via directory search.
2405
2406@item
2407If the target @emph{does} need to be rebuilt (is out-of-date), the
2408pathname found during directory search is @emph{thrown away}, and the
2409target is rebuilt using the file name specified in the makefile. In
2410short, if @code{make} must rebuild, then the target is rebuilt locally,
2411not in the directory found via directory search.
2412@end enumerate
2413@end enumerate
2414
2415This algorithm may seem complex, but in practice it is quite often
2416exactly what you want.
2417
2418@cindex traditional directory search (GPATH)
2419@cindex directory search, traditional (GPATH)
2420Other versions of @code{make} use a simpler algorithm: if the file does
2421not exist, and it is found via directory search, then that pathname is
2422always used whether or not the target needs to be built. Thus, if the
2423target is rebuilt it is created at the pathname discovered during
2424directory search.
2425
2426@vindex GPATH
2427If, in fact, this is the behavior you want for some or all of your
2428directories, you can use the @code{GPATH} variable to indicate this to
2429@code{make}.
2430
2431@code{GPATH} has the same syntax and format as @code{VPATH} (that is, a
2432space- or colon-delimited list of pathnames). If an out-of-date target
2433is found by directory search in a directory that also appears in
2434@code{GPATH}, then that pathname is not thrown away. The target is
2435rebuilt using the expanded path.
2436
2437@node Commands/Search, Implicit/Search, Search Algorithm, Directory Search
2438@subsection Writing Shell Commands with Directory Search
2439@cindex shell command, and directory search
2440@cindex directory search (@code{VPATH}), and shell commands
2441
2442When a prerequisite is found in another directory through directory search,
2443this cannot change the commands of the rule; they will execute as written.
2444Therefore, you must write the commands with care so that they will look for
2445the prerequisite in the directory where @code{make} finds it.
2446
2447This is done with the @dfn{automatic variables} such as @samp{$^}
2448(@pxref{Automatic Variables}).
2449For instance, the value of @samp{$^} is a
2450list of all the prerequisites of the rule, including the names of
2451the directories in which they were found, and the value of
2452@samp{$@@} is the target. Thus:@refill
2453
2454@example
2455foo.o : foo.c
2456 cc -c $(CFLAGS) $^ -o $@@
2457@end example
2458
2459@noindent
2460(The variable @code{CFLAGS} exists so you can specify flags for C
2461compilation by implicit rules; we use it here for consistency so it will
2462affect all C compilations uniformly;
2463@pxref{Implicit Variables, ,Variables Used by Implicit Rules}.)
2464
2465Often the prerequisites include header files as well, which you do not
2466want to mention in the commands. The automatic variable @samp{$<} is
2467just the first prerequisite:
2468
2469@example
2470VPATH = src:../headers
2471foo.o : foo.c defs.h hack.h
2472 cc -c $(CFLAGS) $< -o $@@
2473@end example
2474
2475@node Implicit/Search, Libraries/Search, Commands/Search, Directory Search
2476@subsection Directory Search and Implicit Rules
2477@cindex @code{VPATH}, and implicit rules
2478@cindex directory search (@code{VPATH}), and implicit rules
2479@cindex search path for prerequisites (@code{VPATH}), and implicit rules
2480@cindex implicit rule, and directory search
2481@cindex implicit rule, and @code{VPATH}
2482@cindex rule, implicit, and directory search
2483@cindex rule, implicit, and @code{VPATH}
2484
2485The search through the directories specified in @code{VPATH} or with
2486@code{vpath} also happens during consideration of implicit rules
2487(@pxref{Implicit Rules, ,Using Implicit Rules}).
2488
2489For example, when a file @file{foo.o} has no explicit rule, @code{make}
2490considers implicit rules, such as the built-in rule to compile
2491@file{foo.c} if that file exists. If such a file is lacking in the
2492current directory, the appropriate directories are searched for it. If
2493@file{foo.c} exists (or is mentioned in the makefile) in any of the
2494directories, the implicit rule for C compilation is applied.
2495
2496The commands of implicit rules normally use automatic variables as a
2497matter of necessity; consequently they will use the file names found by
2498directory search with no extra effort.
2499
2500@node Libraries/Search, , Implicit/Search, Directory Search
2501@subsection Directory Search for Link Libraries
2502@cindex link libraries, and directory search
2503@cindex libraries for linking, directory search
2504@cindex directory search (@code{VPATH}), and link libraries
2505@cindex @code{VPATH}, and link libraries
2506@cindex search path for prerequisites (@code{VPATH}), and link libraries
2507@cindex @code{-l} (library search)
2508@cindex link libraries, patterns matching
2509@cindex @code{.LIBPATTERNS}, and link libraries
2510@vindex .LIBPATTERNS
2511
2512Directory search applies in a special way to libraries used with the
2513linker. This special feature comes into play when you write a prerequisite
2514whose name is of the form @samp{-l@var{name}}. (You can tell something
2515strange is going on here because the prerequisite is normally the name of a
2516file, and the @emph{file name} of a library generally looks like
2517@file{lib@var{name}.a}, not like @samp{-l@var{name}}.)@refill
2518
2519When a prerequisite's name has the form @samp{-l@var{name}}, @code{make}
2520handles it specially by searching for the file @file{lib@var{name}.so} in
2521the current directory, in directories specified by matching @code{vpath}
2522search paths and the @code{VPATH} search path, and then in the
2523directories @file{/lib}, @file{/usr/lib}, and @file{@var{prefix}/lib}
2524(normally @file{/usr/local/lib}, but MS-DOS/MS-Windows versions of
2525@code{make} behave as if @var{prefix} is defined to be the root of the
2526DJGPP installation tree).
2527
2528If that file is not found, then the file @file{lib@var{name}.a} is
2529searched for, in the same directories as above.
2530
2531For example, if there is a @file{/usr/lib/libcurses.a} library on your
2532system (and no @file{/usr/lib/libcurses.so} file), then
2533
2534@example
2535@group
2536foo : foo.c -lcurses
2537 cc $^ -o $@@
2538@end group
2539@end example
2540
2541@noindent
2542would cause the command @samp{cc foo.c /usr/lib/libcurses.a -o foo} to
2543be executed when @file{foo} is older than @file{foo.c} or than
2544@file{/usr/lib/libcurses.a}.@refill
2545
2546Although the default set of files to be searched for is
2547@file{lib@var{name}.so} and @file{lib@var{name}.a}, this is customizable
2548via the @code{.LIBPATTERNS} variable. Each word in the value of this
2549variable is a pattern string. When a prerequisite like
2550@samp{-l@var{name}} is seen, @code{make} will replace the percent in
2551each pattern in the list with @var{name} and perform the above directory
2552searches using that library filename. If no library is found, the next
2553word in the list will be used.
2554
2555The default value for @code{.LIBPATTERNS} is ``@samp{lib%.so lib%.a}'',
2556which provides the default behavior described above.
2557
2558You can turn off link library expansion completely by setting this
2559variable to an empty value.
2560
2561@node Phony Targets, Force Targets, Directory Search, Rules
2562@section Phony Targets
2563@cindex phony targets
2564@cindex targets, phony
2565@cindex targets without a file
2566
2567A phony target is one that is not really the name of a file. It is just a
2568name for some commands to be executed when you make an explicit request.
2569There are two reasons to use a phony target: to avoid a conflict with
2570a file of the same name, and to improve performance.
2571
2572If you write a rule whose commands will not create the target file, the
2573commands will be executed every time the target comes up for remaking.
2574Here is an example:
2575
2576@example
2577@group
2578clean:
2579 rm *.o temp
2580@end group
2581@end example
2582
2583@noindent
2584Because the @code{rm} command does not create a file named @file{clean},
2585probably no such file will ever exist. Therefore, the @code{rm} command
2586will be executed every time you say @samp{make clean}.
2587@cindex @code{rm} (shell command)
2588
2589@findex .PHONY
2590The phony target will cease to work if anything ever does create a file
2591named @file{clean} in this directory. Since it has no prerequisites, the
2592file @file{clean} would inevitably be considered up to date, and its
2593commands would not be executed. To avoid this problem, you can explicitly
2594declare the target to be phony, using the special target @code{.PHONY}
2595(@pxref{Special Targets, ,Special Built-in Target Names}) as follows:
2596
2597@example
2598.PHONY : clean
2599@end example
2600
2601@noindent
2602Once this is done, @samp{make clean} will run the commands regardless of
2603whether there is a file named @file{clean}.
2604
2605Since it knows that phony targets do not name actual files that could be
2606remade from other files, @code{make} skips the implicit rule search for
2607phony targets (@pxref{Implicit Rules}). This is why declaring a target
2608phony is good for performance, even if you are not worried about the
2609actual file existing.
2610
2611Thus, you first write the line that states that @code{clean} is a
2612phony target, then you write the rule, like this:
2613
2614@example
2615@group
2616.PHONY: clean
2617clean:
2618 rm *.o temp
2619@end group
2620@end example
2621
2622Another example of the usefulness of phony targets is in conjunction
2623with recursive invocations of @code{make} (for more information, see
2624@ref{Recursion, ,Recursive Use of @code{make}}). In this case the
2625makefile will often contain a variable which lists a number of
2626subdirectories to be built. One way to handle this is with one rule
2627whose command is a shell loop over the subdirectories, like this:
2628
2629@example
2630@group
2631SUBDIRS = foo bar baz
2632
2633subdirs:
2634 for dir in $(SUBDIRS); do \
2635 $(MAKE) -C $$dir; \
2636 done
2637@end group
2638@end example
2639
2640There are a few problems with this method, however. First, any error
2641detected in a submake is not noted by this rule, so it will continue to
2642build the rest of the directories even when one fails. This can be
2643overcome by adding shell commands to note the error and exit, but then
2644it will do so even if @code{make} is invoked with the @code{-k} option,
2645which is unfortunate. Second, and perhaps more importantly, you cannot
2646take advantage of the parallel build capabilities of make using this
2647method, since there is only one rule.
2648
2649By declaring the subdirectories as phony targets (you must do this as
2650the subdirectory obviously always exists; otherwise it won't be built)
2651you can remove these problems:
2652
2653@example
2654@group
2655SUBDIRS = foo bar baz
2656
2657.PHONY: subdirs $(SUBDIRS)
2658
2659subdirs: $(SUBDIRS)
2660
2661$(SUBDIRS):
2662 $(MAKE) -C $@@
2663
2664foo: baz
2665@end group
2666@end example
2667
2668Here we've also declared that the @file{foo} subdirectory cannot be
2669built until after the @file{baz} subdirectory is complete; this kind of
2670relationship declaration is particularly important when attempting
2671parallel builds.
2672
2673A phony target should not be a prerequisite of a real target file; if it
2674is, its commands are run every time @code{make} goes to update that
2675file. As long as a phony target is never a prerequisite of a real
2676target, the phony target commands will be executed only when the phony
2677target is a specified goal (@pxref{Goals, ,Arguments to Specify the
2678Goals}).
2679
2680Phony targets can have prerequisites. When one directory contains multiple
2681programs, it is most convenient to describe all of the programs in one
2682makefile @file{./Makefile}. Since the target remade by default will be the
2683first one in the makefile, it is common to make this a phony target named
2684@samp{all} and give it, as prerequisites, all the individual programs. For
2685example:
2686
2687@example
2688all : prog1 prog2 prog3
2689.PHONY : all
2690
2691prog1 : prog1.o utils.o
2692 cc -o prog1 prog1.o utils.o
2693
2694prog2 : prog2.o
2695 cc -o prog2 prog2.o
2696
2697prog3 : prog3.o sort.o utils.o
2698 cc -o prog3 prog3.o sort.o utils.o
2699@end example
2700
2701@noindent
2702Now you can say just @samp{make} to remake all three programs, or
2703specify as arguments the ones to remake (as in @samp{make prog1
2704prog3}). Phoniness is not inherited: the prerequisites of a phony
2705target are not themselves phony, unless explicitly declared to be so.
2706
2707When one phony target is a prerequisite of another, it serves as a subroutine
2708of the other. For example, here @samp{make cleanall} will delete the
2709object files, the difference files, and the file @file{program}:
2710
2711@example
2712.PHONY: cleanall cleanobj cleandiff
2713
2714cleanall : cleanobj cleandiff
2715 rm program
2716
2717cleanobj :
2718 rm *.o
2719
2720cleandiff :
2721 rm *.diff
2722@end example
2723
2724@node Force Targets, Empty Targets, Phony Targets, Rules
2725@section Rules without Commands or Prerequisites
2726@cindex force targets
2727@cindex targets, force
2728@cindex @code{FORCE}
2729@cindex rule, no commands or prerequisites
2730
2731If a rule has no prerequisites or commands, and the target of the rule
2732is a nonexistent file, then @code{make} imagines this target to have
2733been updated whenever its rule is run. This implies that all targets
2734depending on this one will always have their commands run.
2735
2736An example will illustrate this:
2737
2738@example
2739@group
2740clean: FORCE
2741 rm $(objects)
2742FORCE:
2743@end group
2744@end example
2745
2746Here the target @samp{FORCE} satisfies the special conditions, so the
2747target @file{clean} that depends on it is forced to run its commands.
2748There is nothing special about the name @samp{FORCE}, but that is one name
2749commonly used this way.
2750
2751As you can see, using @samp{FORCE} this way has the same results as using
2752@samp{.PHONY: clean}.
2753
2754Using @samp{.PHONY} is more explicit and more efficient. However,
2755other versions of @code{make} do not support @samp{.PHONY}; thus
2756@samp{FORCE} appears in many makefiles. @xref{Phony Targets}.
2757
2758@node Empty Targets, Special Targets, Force Targets, Rules
2759@section Empty Target Files to Record Events
2760@cindex empty targets
2761@cindex targets, empty
2762@cindex recording events with empty targets
2763
2764The @dfn{empty target} is a variant of the phony target; it is used to hold
2765commands for an action that you request explicitly from time to time.
2766Unlike a phony target, this target file can really exist; but the file's
2767contents do not matter, and usually are empty.
2768
2769The purpose of the empty target file is to record, with its
2770last-modification time, when the rule's commands were last executed. It
2771does so because one of the commands is a @code{touch} command to update the
2772target file.
2773
2774The empty target file should have some prerequisites (otherwise it
2775doesn't make sense). When you ask to remake the empty target, the
2776commands are executed if any prerequisite is more recent than the target;
2777in other words, if a prerequisite has changed since the last time you
2778remade the target. Here is an example:
2779
2780@example
2781print: foo.c bar.c
2782 lpr -p $?
2783 touch print
2784@end example
2785@cindex @code{print} target
2786@cindex @code{lpr} (shell command)
2787@cindex @code{touch} (shell command)
2788
2789@noindent
2790With this rule, @samp{make print} will execute the @code{lpr} command if
2791either source file has changed since the last @samp{make print}. The
2792automatic variable @samp{$?} is used to print only those files that have
2793changed (@pxref{Automatic Variables}).
2794
2795@node Special Targets, Multiple Targets, Empty Targets, Rules
2796@section Special Built-in Target Names
2797@cindex special targets
2798@cindex built-in special targets
2799@cindex targets, built-in special
2800
2801Certain names have special meanings if they appear as targets.
2802
2803@table @code
2804@findex .PHONY
2805@item .PHONY
2806
2807The prerequisites of the special target @code{.PHONY} are considered to
2808be phony targets. When it is time to consider such a target,
2809@code{make} will run its commands unconditionally, regardless of
2810whether a file with that name exists or what its last-modification
2811time is. @xref{Phony Targets, ,Phony Targets}.
2812
2813@findex .SUFFIXES
2814@item .SUFFIXES
2815
2816The prerequisites of the special target @code{.SUFFIXES} are the list
2817of suffixes to be used in checking for suffix rules.
2818@xref{Suffix Rules, , Old-Fashioned Suffix Rules}.
2819
2820@findex .DEFAULT
2821@item .DEFAULT
2822
2823The commands specified for @code{.DEFAULT} are used for any target for
2824which no rules are found (either explicit rules or implicit rules).
2825@xref{Last Resort}. If @code{.DEFAULT} commands are specified, every
2826file mentioned as a prerequisite, but not as a target in a rule, will have
2827these commands executed on its behalf. @xref{Implicit Rule Search,
2828,Implicit Rule Search Algorithm}.
2829
2830@findex .PRECIOUS
2831@item .PRECIOUS
2832@cindex precious targets
2833@cindex preserving with @code{.PRECIOUS}
2834
2835The targets which @code{.PRECIOUS} depends on are given the following
2836special treatment: if @code{make} is killed or interrupted during the
2837execution of their commands, the target is not deleted.
2838@xref{Interrupts, ,Interrupting or Killing @code{make}}. Also, if the
2839target is an intermediate file, it will not be deleted after it is no
2840longer needed, as is normally done. @xref{Chained Rules, ,Chains of
2841Implicit Rules}. In this latter respect it overlaps with the
2842@code{.SECONDARY} special target.
2843
2844You can also list the target pattern of an implicit rule (such as
2845@samp{%.o}) as a prerequisite file of the special target @code{.PRECIOUS}
2846to preserve intermediate files created by rules whose target patterns
2847match that file's name.
2848
2849@findex .INTERMEDIATE
2850@item .INTERMEDIATE
2851@cindex intermediate targets, explicit
2852
2853The targets which @code{.INTERMEDIATE} depends on are treated as
2854intermediate files. @xref{Chained Rules, ,Chains of Implicit Rules}.
2855@code{.INTERMEDIATE} with no prerequisites has no effect.
2856
2857@findex .SECONDARY
2858@item .SECONDARY
2859@cindex secondary targets
2860@cindex preserving with @code{.SECONDARY}
2861
2862The targets which @code{.SECONDARY} depends on are treated as
2863intermediate files, except that they are never automatically deleted.
2864@xref{Chained Rules, ,Chains of Implicit Rules}.
2865
2866@code{.SECONDARY} with no prerequisites causes all targets to be treated
2867as secondary (i.e., no target is removed because it is considered
2868intermediate).
2869
2870@findex .DELETE_ON_ERROR
2871@item .DELETE_ON_ERROR
2872@cindex removing targets on failure
2873
2874If @code{.DELETE_ON_ERROR} is mentioned as a target anywhere in the
2875makefile, then @code{make} will delete the target of a rule if it has
2876changed and its commands exit with a nonzero exit status, just as it
2877does when it receives a signal. @xref{Errors, ,Errors in Commands}.
2878
2879@findex .IGNORE
2880@item .IGNORE
2881
2882If you specify prerequisites for @code{.IGNORE}, then @code{make} will
2883ignore errors in execution of the commands run for those particular
2884files. The commands for @code{.IGNORE} are not meaningful.
2885
2886If mentioned as a target with no prerequisites, @code{.IGNORE} says to
2887ignore errors in execution of commands for all files. This usage of
2888@samp{.IGNORE} is supported only for historical compatibility. Since
2889this affects every command in the makefile, it is not very useful; we
2890recommend you use the more selective ways to ignore errors in specific
2891commands. @xref{Errors, ,Errors in Commands}.
2892
2893@findex .LOW_RESOLUTION_TIME
2894@item .LOW_RESOLUTION_TIME
2895
2896If you specify prerequisites for @code{.LOW_RESOLUTION_TIME},
2897@command{make} assumes that these files are created by commands that
2898generate low resolution time stamps. The commands for
2899@code{.LOW_RESOLUTION_TIME} are not meaningful.
2900
2901The high resolution file time stamps of many modern hosts lessen the
2902chance of @command{make} incorrectly concluding that a file is up to
2903date. Unfortunately, these hosts provide no way to set a high
2904resolution file time stamp, so commands like @samp{cp -p} that
2905explicitly set a file's time stamp must discard its subsecond part. If
2906a file is created by such a command, you should list it as a
2907prerequisite of @code{.LOW_RESOLUTION_TIME} so that @command{make} does
2908not mistakenly conclude that the file is out of date. For example:
2909
2910@example
2911@group
2912.LOW_RESOLUTION_TIME: dst
2913dst: src
2914 cp -p src dst
2915@end group
2916@end example
2917
2918Since @samp{cp -p} discards the subsecond part of @file{src}'s time
2919stamp, @file{dst} is typically slightly older than @file{src} even when
2920it is up to date. The @code{.LOW_RESOLUTION_TIME} line causes
2921@command{make} to consider @file{dst} to be up to date if its time stamp
2922is at the start of the same second that @file{src}'s time stamp is in.
2923
2924Due to a limitation of the archive format, archive member time stamps
2925are always low resolution. You need not list archive members as
2926prerequisites of @code{.LOW_RESOLUTION_TIME}, as @command{make} does this
2927automatically.
2928
2929@findex .SILENT
2930@item .SILENT
2931
2932If you specify prerequisites for @code{.SILENT}, then @code{make} will
2933not print the commands to remake those particular files before executing
2934them. The commands for @code{.SILENT} are not meaningful.
2935
2936If mentioned as a target with no prerequisites, @code{.SILENT} says not
2937to print any commands before executing them. This usage of
2938@samp{.SILENT} is supported only for historical compatibility. We
2939recommend you use the more selective ways to silence specific commands.
2940@xref{Echoing, ,Command Echoing}. If you want to silence all commands
2941for a particular run of @code{make}, use the @samp{-s} or
2942@w{@samp{--silent}} option (@pxref{Options Summary}).
2943
2944@findex .EXPORT_ALL_VARIABLES
2945@item .EXPORT_ALL_VARIABLES
2946
2947Simply by being mentioned as a target, this tells @code{make} to
2948export all variables to child processes by default.
2949@xref{Variables/Recursion, ,Communicating Variables to a
2950Sub-@code{make}}.
2951
2952@findex .NOTPARALLEL
2953@item .NOTPARALLEL
2954@cindex parallel execution, overriding
2955
2956If @code{.NOTPARALLEL} is mentioned as a target, then this invocation of
2957@code{make} will be run serially, even if the @samp{-j} option is
2958given. Any recursively invoked @code{make} command will still be run in
2959parallel (unless its makefile contains this target). Any prerequisites
2960on this target are ignored.
2961@end table
2962
2963Any defined implicit rule suffix also counts as a special target if it
2964appears as a target, and so does the concatenation of two suffixes, such
2965as @samp{.c.o}. These targets are suffix rules, an obsolete way of
2966defining implicit rules (but a way still widely used). In principle, any
2967target name could be special in this way if you break it in two and add
2968both pieces to the suffix list. In practice, suffixes normally begin with
2969@samp{.}, so these special target names also begin with @samp{.}.
2970@xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
2971
2972@node Multiple Targets, Multiple Rules, Special Targets, Rules
2973@section Multiple Targets in a Rule
2974@cindex multiple targets
2975@cindex several targets in a rule
2976@cindex targets, multiple
2977@cindex rule, with multiple targets
2978
2979A rule with multiple targets is equivalent to writing many rules, each with
2980one target, and all identical aside from that. The same commands apply to
2981all the targets, but their effects may vary because you can substitute the
2982actual target name into the command using @samp{$@@}. The rule contributes
2983the same prerequisites to all the targets also.
2984
2985This is useful in two cases.
2986
2987@itemize @bullet
2988@item
2989You want just prerequisites, no commands. For example:
2990
2991@example
2992kbd.o command.o files.o: command.h
2993@end example
2994
2995@noindent
2996gives an additional prerequisite to each of the three object files
2997mentioned.
2998
2999@item
3000Similar commands work for all the targets. The commands do not need
3001to be absolutely identical, since the automatic variable @samp{$@@}
3002can be used to substitute the particular target to be remade into the
3003commands (@pxref{Automatic Variables}). For example:
3004
3005@example
3006@group
3007bigoutput littleoutput : text.g
3008 generate text.g -$(subst output,,$@@) > $@@
3009@end group
3010@end example
3011@findex subst
3012
3013@noindent
3014is equivalent to
3015
3016@example
3017bigoutput : text.g
3018 generate text.g -big > bigoutput
3019littleoutput : text.g
3020 generate text.g -little > littleoutput
3021@end example
3022
3023@noindent
3024Here we assume the hypothetical program @code{generate} makes two
3025types of output, one if given @samp{-big} and one if given
3026@samp{-little}.
3027@xref{Text Functions, ,Functions for String Substitution and Analysis},
3028for an explanation of the @code{subst} function.
3029@end itemize
3030
3031Suppose you would like to vary the prerequisites according to the target,
3032much as the variable @samp{$@@} allows you to vary the commands.
3033You cannot do this with multiple targets in an ordinary rule, but you can
3034do it with a @dfn{static pattern rule}.
3035@xref{Static Pattern, ,Static Pattern Rules}.
3036
3037@node Multiple Rules, Static Pattern, Multiple Targets, Rules
3038@section Multiple Rules for One Target
3039@cindex multiple rules for one target
3040@cindex several rules for one target
3041@cindex rule, multiple for one target
3042@cindex target, multiple rules for one
3043
3044One file can be the target of several rules. All the prerequisites
3045mentioned in all the rules are merged into one list of prerequisites for
3046the target. If the target is older than any prerequisite from any rule,
3047the commands are executed.
3048
3049There can only be one set of commands to be executed for a file. If
3050more than one rule gives commands for the same file, @code{make} uses
3051the last set given and prints an error message. (As a special case,
3052if the file's name begins with a dot, no error message is printed.
3053This odd behavior is only for compatibility with other implementations
3054of @code{make}... you should avoid using it). Occasionally it is
3055useful to have the same target invoke multiple commands which are
3056defined in different parts of your makefile; you can use
3057@dfn{double-colon rules} (@pxref{Double-Colon}) for this.
3058
3059An extra rule with just prerequisites can be used to give a few extra
3060prerequisites to many files at once. For example, makefiles often
3061have a variable, such as @code{objects}, containing a list of all the
3062compiler output files in the system being made. An easy way to say
3063that all of them must be recompiled if @file{config.h} changes is to
3064write the following:
3065
3066@example
3067objects = foo.o bar.o
3068foo.o : defs.h
3069bar.o : defs.h test.h
3070$(objects) : config.h
3071@end example
3072
3073This could be inserted or taken out without changing the rules that really
3074specify how to make the object files, making it a convenient form to use if
3075you wish to add the additional prerequisite intermittently.
3076
3077Another wrinkle is that the additional prerequisites could be specified with
3078a variable that you set with a command argument to @code{make}
3079(@pxref{Overriding, ,Overriding Variables}). For example,
3080
3081@example
3082@group
3083extradeps=
3084$(objects) : $(extradeps)
3085@end group
3086@end example
3087
3088@noindent
3089means that the command @samp{make extradeps=foo.h} will consider
3090@file{foo.h} as a prerequisite of each object file, but plain @samp{make}
3091will not.
3092
3093If none of the explicit rules for a target has commands, then @code{make}
3094searches for an applicable implicit rule to find some commands
3095@pxref{Implicit Rules, ,Using Implicit Rules}).
3096
3097@node Static Pattern, Double-Colon, Multiple Rules, Rules
3098@section Static Pattern Rules
3099@cindex static pattern rule
3100@cindex rule, static pattern
3101@cindex pattern rules, static (not implicit)
3102@cindex varying prerequisites
3103@cindex prerequisites, varying (static pattern)
3104
3105@dfn{Static pattern rules} are rules which specify multiple targets and
3106construct the prerequisite names for each target based on the target name.
3107They are more general than ordinary rules with multiple targets because the
3108targets do not have to have identical prerequisites. Their prerequisites must
3109be @emph{analogous}, but not necessarily @emph{identical}.
3110
3111@menu
3112* Static Usage:: The syntax of static pattern rules.
3113* Static versus Implicit:: When are they better than implicit rules?
3114@end menu
3115
3116@node Static Usage, Static versus Implicit, Static Pattern, Static Pattern
3117@subsection Syntax of Static Pattern Rules
3118@cindex static pattern rule, syntax of
3119@cindex pattern rules, static, syntax of
3120
3121Here is the syntax of a static pattern rule:
3122
3123@example
3124@var{targets} @dots{}: @var{target-pattern}: @var{prereq-patterns} @dots{}
3125 @var{commands}
3126 @dots{}
3127@end example
3128
3129@noindent
3130The @var{targets} list specifies the targets that the rule applies to.
3131The targets can contain wildcard characters, just like the targets of
3132ordinary rules (@pxref{Wildcards, ,Using Wildcard Characters in File
3133Names}).
3134
3135@cindex target pattern, static (not implicit)
3136@cindex stem
3137The @var{target-pattern} and @var{prereq-patterns} say how to compute the
3138prerequisites of each target. Each target is matched against the
3139@var{target-pattern} to extract a part of the target name, called the
3140@dfn{stem}. This stem is substituted into each of the @var{prereq-patterns}
3141to make the prerequisite names (one from each @var{prereq-pattern}).
3142
3143Each pattern normally contains the character @samp{%} just once. When the
3144@var{target-pattern} matches a target, the @samp{%} can match any part of
3145the target name; this part is called the @dfn{stem}. The rest of the
3146pattern must match exactly. For example, the target @file{foo.o} matches
3147the pattern @samp{%.o}, with @samp{foo} as the stem. The targets
3148@file{foo.c} and @file{foo.out} do not match that pattern.@refill
3149
3150@cindex prerequisite pattern, static (not implicit)
3151The prerequisite names for each target are made by substituting the stem
3152for the @samp{%} in each prerequisite pattern. For example, if one
3153prerequisite pattern is @file{%.c}, then substitution of the stem
3154@samp{foo} gives the prerequisite name @file{foo.c}. It is legitimate
3155to write a prerequisite pattern that does not contain @samp{%}; then this
3156prerequisite is the same for all targets.
3157
3158@cindex @code{%}, quoting in static pattern
3159@cindex @code{%}, quoting with @code{\} (backslash)
3160@cindex @code{\} (backslash), to quote @code{%}
3161@cindex backslash (@code{\}), to quote @code{%}
3162@cindex quoting @code{%}, in static pattern
3163@samp{%} characters in pattern rules can be quoted with preceding
3164backslashes (@samp{\}). Backslashes that would otherwise quote @samp{%}
3165characters can be quoted with more backslashes. Backslashes that quote
3166@samp{%} characters or other backslashes are removed from the pattern
3167before it is compared to file names or has a stem substituted into it.
3168Backslashes that are not in danger of quoting @samp{%} characters go
3169unmolested. For example, the pattern @file{the\%weird\\%pattern\\} has
3170@samp{the%weird\} preceding the operative @samp{%} character, and
3171@samp{pattern\\} following it. The final two backslashes are left alone
3172because they cannot affect any @samp{%} character.@refill
3173
3174Here is an example, which compiles each of @file{foo.o} and @file{bar.o}
3175from the corresponding @file{.c} file:
3176
3177@example
3178@group
3179objects = foo.o bar.o
3180
3181all: $(objects)
3182
3183$(objects): %.o: %.c
3184 $(CC) -c $(CFLAGS) $< -o $@@
3185@end group
3186@end example
3187
3188@noindent
3189Here @samp{$<} is the automatic variable that holds the name of the
3190prerequisite and @samp{$@@} is the automatic variable that holds the name
3191of the target; see @ref{Automatic Variables}.
3192
3193Each target specified must match the target pattern; a warning is issued
3194for each target that does not. If you have a list of files, only some of
3195which will match the pattern, you can use the @code{filter} function to
3196remove nonmatching file names (@pxref{Text Functions, ,Functions for String Substitution and Analysis}):
3197
3198@example
3199files = foo.elc bar.o lose.o
3200
3201$(filter %.o,$(files)): %.o: %.c
3202 $(CC) -c $(CFLAGS) $< -o $@@
3203$(filter %.elc,$(files)): %.elc: %.el
3204 emacs -f batch-byte-compile $<
3205@end example
3206
3207@noindent
3208In this example the result of @samp{$(filter %.o,$(files))} is
3209@file{bar.o lose.o}, and the first static pattern rule causes each of
3210these object files to be updated by compiling the corresponding C source
3211file. The result of @w{@samp{$(filter %.elc,$(files))}} is
3212@file{foo.elc}, so that file is made from @file{foo.el}.@refill
3213
3214Another example shows how to use @code{$*} in static pattern rules:
3215@vindex $*@r{, and static pattern}
3216
3217@example
3218@group
3219bigoutput littleoutput : %output : text.g
3220 generate text.g -$* > $@@
3221@end group
3222@end example
3223
3224@noindent
3225When the @code{generate} command is run, @code{$*} will expand to the
3226stem, either @samp{big} or @samp{little}.
3227
3228@node Static versus Implicit, , Static Usage, Static Pattern
3229@subsection Static Pattern Rules versus Implicit Rules
3230@cindex rule, static pattern versus implicit
3231@cindex static pattern rule, versus implicit
3232
3233A static pattern rule has much in common with an implicit rule defined as a
3234pattern rule (@pxref{Pattern Rules, ,Defining and Redefining Pattern Rules}).
3235Both have a pattern for the target and patterns for constructing the
3236names of prerequisites. The difference is in how @code{make} decides
3237@emph{when} the rule applies.
3238
3239An implicit rule @emph{can} apply to any target that matches its pattern,
3240but it @emph{does} apply only when the target has no commands otherwise
3241specified, and only when the prerequisites can be found. If more than one
3242implicit rule appears applicable, only one applies; the choice depends on
3243the order of rules.
3244
3245By contrast, a static pattern rule applies to the precise list of targets
3246that you specify in the rule. It cannot apply to any other target and it
3247invariably does apply to each of the targets specified. If two conflicting
3248rules apply, and both have commands, that's an error.
3249
3250The static pattern rule can be better than an implicit rule for these
3251reasons:
3252
3253@itemize @bullet
3254@item
3255You may wish to override the usual implicit rule for a few
3256files whose names cannot be categorized syntactically but
3257can be given in an explicit list.
3258
3259@item
3260If you cannot be sure of the precise contents of the directories
3261you are using, you may not be sure which other irrelevant files
3262might lead @code{make} to use the wrong implicit rule. The choice
3263might depend on the order in which the implicit rule search is done.
3264With static pattern rules, there is no uncertainty: each rule applies
3265to precisely the targets specified.
3266@end itemize
3267
3268@node Double-Colon, Automatic Prerequisites, Static Pattern, Rules
3269@section Double-Colon Rules
3270@cindex double-colon rules
3271@cindex rule, double-colon (@code{::})
3272@cindex multiple rules for one target (@code{::})
3273@cindex @code{::} rules (double-colon)
3274
3275@dfn{Double-colon} rules are rules written with @samp{::} instead of
3276@samp{:} after the target names. They are handled differently from
3277ordinary rules when the same target appears in more than one rule.
3278
3279When a target appears in multiple rules, all the rules must be the same
3280type: all ordinary, or all double-colon. If they are double-colon, each
3281of them is independent of the others. Each double-colon rule's commands
3282are executed if the target is older than any prerequisites of that rule.
3283If there are no prerequisites for that rule, its commands are always
3284executed (even if the target already exists). This can result in
3285executing none, any, or all of the double-colon rules.
3286
3287Double-colon rules with the same target are in fact completely separate
3288from one another. Each double-colon rule is processed individually, just
3289as rules with different targets are processed.
3290
3291The double-colon rules for a target are executed in the order they appear
3292in the makefile. However, the cases where double-colon rules really make
3293sense are those where the order of executing the commands would not matter.
3294
3295Double-colon rules are somewhat obscure and not often very useful; they
3296provide a mechanism for cases in which the method used to update a target
3297differs depending on which prerequisite files caused the update, and such
3298cases are rare.
3299
3300Each double-colon rule should specify commands; if it does not, an
3301implicit rule will be used if one applies.
3302@xref{Implicit Rules, ,Using Implicit Rules}.
3303
3304@node Automatic Prerequisites, , Double-Colon, Rules
3305@section Generating Prerequisites Automatically
3306@cindex prerequisites, automatic generation
3307@cindex automatic generation of prerequisites
3308@cindex generating prerequisites automatically
3309
3310In the makefile for a program, many of the rules you need to write often
3311say only that some object file depends on some header
3312file. For example, if @file{main.c} uses @file{defs.h} via an
3313@code{#include}, you would write:
3314
3315@example
3316main.o: defs.h
3317@end example
3318
3319@noindent
3320You need this rule so that @code{make} knows that it must remake
3321@file{main.o} whenever @file{defs.h} changes. You can see that for a
3322large program you would have to write dozens of such rules in your
3323makefile. And, you must always be very careful to update the makefile
3324every time you add or remove an @code{#include}.
3325@cindex @code{#include}
3326
3327@cindex @code{-M} (to compiler)
3328To avoid this hassle, most modern C compilers can write these rules for
3329you, by looking at the @code{#include} lines in the source files.
3330Usually this is done with the @samp{-M} option to the compiler.
3331For example, the command:
3332
3333@example
3334cc -M main.c
3335@end example
3336
3337@noindent
3338generates the output:
3339
3340@example
3341main.o : main.c defs.h
3342@end example
3343
3344@noindent
3345Thus you no longer have to write all those rules yourself.
3346The compiler will do it for you.
3347
3348Note that such a prerequisite constitutes mentioning @file{main.o} in a
3349makefile, so it can never be considered an intermediate file by implicit
3350rule search. This means that @code{make} won't ever remove the file
3351after using it; @pxref{Chained Rules, ,Chains of Implicit Rules}.
3352
3353@cindex @code{make depend}
3354With old @code{make} programs, it was traditional practice to use this
3355compiler feature to generate prerequisites on demand with a command like
3356@samp{make depend}. That command would create a file @file{depend}
3357containing all the automatically-generated prerequisites; then the
3358makefile could use @code{include} to read them in (@pxref{Include}).
3359
3360In GNU @code{make}, the feature of remaking makefiles makes this
3361practice obsolete---you need never tell @code{make} explicitly to
3362regenerate the prerequisites, because it always regenerates any makefile
3363that is out of date. @xref{Remaking Makefiles}.
3364
3365The practice we recommend for automatic prerequisite generation is to have
3366one makefile corresponding to each source file. For each source file
3367@file{@var{name}.c} there is a makefile @file{@var{name}.d} which lists
3368what files the object file @file{@var{name}.o} depends on. That way
3369only the source files that have changed need to be rescanned to produce
3370the new prerequisites.
3371
3372Here is the pattern rule to generate a file of prerequisites (i.e., a makefile)
3373called @file{@var{name}.d} from a C source file called @file{@var{name}.c}:
3374
3375@smallexample
3376@group
3377%.d: %.c
3378 @set -e; rm -f $@@; \
3379 $(CC) -M $(CPPFLAGS) $< > $@@.$$$$; \
3380 sed 's,\($*\)\.o[ :]*,\1.o $@@ : ,g' < $@@.$$$$ > $@@; \
3381 rm -f $@@.$$$$
3382@end group
3383@end smallexample
3384
3385@noindent
3386@xref{Pattern Rules}, for information on defining pattern rules. The
3387@samp{-e} flag to the shell causes it to exit immediately if the
3388@code{$(CC)} command (or any other command) fails (exits with a
3389nonzero status).
3390@cindex @code{-e} (shell flag)
3391
3392@cindex @code{-MM} (to GNU compiler)
3393With the GNU C compiler, you may wish to use the @samp{-MM} flag instead
3394of @samp{-M}. This omits prerequisites on system header files.
3395@xref{Preprocessor Options, , Options Controlling the Preprocessor,
3396gcc.info, Using GNU CC}, for details.
3397
3398@cindex @code{sed} (shell command)
3399The purpose of the @code{sed} command is to translate (for example):
3400
3401@example
3402main.o : main.c defs.h
3403@end example
3404
3405@noindent
3406into:
3407
3408@example
3409main.o main.d : main.c defs.h
3410@end example
3411
3412@noindent
3413@cindex @code{.d}
3414This makes each @samp{.d} file depend on all the source and header files
3415that the corresponding @samp{.o} file depends on. @code{make} then
3416knows it must regenerate the prerequisites whenever any of the source or
3417header files changes.
3418
3419Once you've defined the rule to remake the @samp{.d} files,
3420you then use the @code{include} directive to read them all in.
3421@xref{Include}. For example:
3422
3423@example
3424@group
3425sources = foo.c bar.c
3426
3427include $(sources:.c=.d)
3428@end group
3429@end example
3430
3431@noindent
3432(This example uses a substitution variable reference to translate the
3433list of source files @samp{foo.c bar.c} into a list of prerequisite
3434makefiles, @samp{foo.d bar.d}. @xref{Substitution Refs}, for full
3435information on substitution references.) Since the @samp{.d} files are
3436makefiles like any others, @code{make} will remake them as necessary
3437with no further work from you. @xref{Remaking Makefiles}.
3438
3439Note that the @samp{.d} files contain target definitions; you should
3440be sure to place the @code{include} directive @emph{after} the first,
3441default goal in your makefiles or run the risk of having a random
3442object file become the default goal.
3443@xref{How Make Works}.
3444
3445@node Commands, Using Variables, Rules, Top
3446@chapter Writing the Commands in Rules
3447@cindex commands, how to write
3448@cindex rule commands
3449@cindex writing rule commands
3450
3451The commands of a rule consist of shell command lines to be executed one
3452by one. Each command line must start with a tab, except that the first
3453command line may be attached to the target-and-prerequisites line with a
3454semicolon in between. Blank lines and lines of just comments may appear
3455among the command lines; they are ignored. (But beware, an apparently
3456``blank'' line that begins with a tab is @emph{not} blank! It is an
3457empty command; @pxref{Empty Commands}.)
3458
3459Users use many different shell programs, but commands in makefiles are
3460always interpreted by @file{/bin/sh} unless the makefile specifies
3461otherwise. @xref{Execution, ,Command Execution}.
3462
3463@cindex comments, in commands
3464@cindex commands, comments in
3465@cindex @code{#} (comments), in commands
3466The shell that is in use determines whether comments can be written on
3467command lines, and what syntax they use. When the shell is
3468@file{/bin/sh}, a @samp{#} starts a comment that extends to the end of
3469the line. The @samp{#} does not have to be at the beginning of a line.
3470Text on a line before a @samp{#} is not part of the comment.
3471
3472@menu
3473* Echoing:: How to control when commands are echoed.
3474* Execution:: How commands are executed.
3475* Parallel:: How commands can be executed in parallel.
3476* Errors:: What happens after a command execution error.
3477* Interrupts:: What happens when a command is interrupted.
3478* Recursion:: Invoking @code{make} from makefiles.
3479* Sequences:: Defining canned sequences of commands.
3480* Empty Commands:: Defining useful, do-nothing commands.
3481@end menu
3482
3483@node Echoing, Execution, Commands, Commands
3484@section Command Echoing
3485@cindex echoing of commands
3486@cindex silent operation
3487@cindex @code{@@} (in commands)
3488@cindex commands, echoing
3489@cindex printing of commands
3490
3491Normally @code{make} prints each command line before it is executed.
3492We call this @dfn{echoing} because it gives the appearance that you
3493are typing the commands yourself.
3494
3495When a line starts with @samp{@@}, the echoing of that line is suppressed.
3496The @samp{@@} is discarded before the command is passed to the shell.
3497Typically you would use this for a command whose only effect is to print
3498something, such as an @code{echo} command to indicate progress through
3499the makefile:
3500
3501@example
3502@@echo About to make distribution files
3503@end example
3504
3505@cindex @code{-n}
3506@cindex @code{--just-print}
3507@cindex @code{--dry-run}
3508@cindex @code{--recon}
3509When @code{make} is given the flag @samp{-n} or @samp{--just-print}
3510it only echoes commands, it won't execute them. @xref{Options Summary,
3511,Summary of Options}. In this case and only this case, even the
3512commands starting with @samp{@@} are printed. This flag is useful for
3513finding out which commands @code{make} thinks are necessary without
3514actually doing them.
3515
3516@cindex @code{-s}
3517@cindex @code{--silent}
3518@cindex @code{--quiet}
3519@findex .SILENT
3520The @samp{-s} or @samp{--silent}
3521flag to @code{make} prevents all echoing, as if all commands
3522started with @samp{@@}. A rule in the makefile for the special target
3523@code{.SILENT} without prerequisites has the same effect
3524(@pxref{Special Targets, ,Special Built-in Target Names}).
3525@code{.SILENT} is essentially obsolete since @samp{@@} is more flexible.@refill
3526
3527@node Execution, Parallel, Echoing, Commands
3528@section Command Execution
3529@cindex commands, execution
3530@cindex execution, of commands
3531@cindex shell command, execution
3532@vindex SHELL @r{(command execution)}
3533
3534When it is time to execute commands to update a target, they are executed
3535by making a new subshell for each line. (In practice, @code{make} may
3536take shortcuts that do not affect the results.)
3537
3538@cindex @code{cd} (shell command)
3539@strong{Please note:} this implies that shell commands such as @code{cd}
3540that set variables local to each process will not affect the following
3541command lines. @footnote{On MS-DOS, the value of current working
3542directory is @strong{global}, so changing it @emph{will} affect the
3543following command lines on those systems.} If you want to use @code{cd}
3544to affect the next command, put the two on a single line with a
3545semicolon between them. Then @code{make} will consider them a single
3546command and pass them, together, to a shell which will execute them in
3547sequence. For example:
3548
3549@example
3550foo : bar/lose
3551 cd bar; gobble lose > ../foo
3552@end example
3553
3554@cindex commands, backslash (@code{\}) in
3555@cindex commands, quoting newlines in
3556@cindex backslash (@code{\}), in commands
3557@cindex @code{\} (backslash), in commands
3558@cindex quoting newline, in commands
3559@cindex newline, quoting, in commands
3560If you would like to split a single shell command into multiple lines of
3561text, you must use a backslash at the end of all but the last subline.
3562Such a sequence of lines is combined into a single line, by deleting the
3563backslash-newline sequences, before passing it to the shell. Thus, the
3564following is equivalent to the preceding example:
3565
3566@example
3567@group
3568foo : bar/lose
3569 cd bar; \
3570 gobble lose > ../foo
3571@end group
3572@end example
3573
3574@vindex SHELL
3575The program used as the shell is taken from the variable @code{SHELL}.
3576By default, the program @file{/bin/sh} is used.
3577
3578@vindex COMSPEC
3579On MS-DOS, if @code{SHELL} is not set, the value of the variable
3580@code{COMSPEC} (which is always set) is used instead.
3581
3582@cindex @code{SHELL}, MS-DOS specifics
3583The processing of lines that set the variable @code{SHELL} in Makefiles
3584is different on MS-DOS. The stock shell, @file{command.com}, is
3585ridiculously limited in its functionality and many users of @code{make}
3586tend to install a replacement shell. Therefore, on MS-DOS, @code{make}
3587examines the value of @code{SHELL}, and changes its behavior based on
3588whether it points to a Unix-style or DOS-style shell. This allows
3589reasonable functionality even if @code{SHELL} points to
3590@file{command.com}.
3591
3592If @code{SHELL} points to a Unix-style shell, @code{make} on MS-DOS
3593additionally checks whether that shell can indeed be found; if not, it
3594ignores the line that sets @code{SHELL}. In MS-DOS, GNU @code{make}
3595searches for the shell in the following places:
3596
3597@enumerate
3598@item
3599In the precise place pointed to by the value of @code{SHELL}. For
3600example, if the makefile specifies @samp{SHELL = /bin/sh}, @code{make}
3601will look in the directory @file{/bin} on the current drive.
3602
3603@item
3604In the current directory.
3605
3606@item
3607In each of the directories in the @code{PATH} variable, in order.
3608
3609@end enumerate
3610
3611In every directory it examines, @code{make} will first look for the
3612specific file (@file{sh} in the example above). If this is not found,
3613it will also look in that directory for that file with one of the known
3614extensions which identify executable files. For example @file{.exe},
3615@file{.com}, @file{.bat}, @file{.btm}, @file{.sh}, and some others.
3616
3617If any of these attempts is successful, the value of @code{SHELL} will
3618be set to the full pathname of the shell as found. However, if none of
3619these is found, the value of @code{SHELL} will not be changed, and thus
3620the line that sets it will be effectively ignored. This is so
3621@code{make} will only support features specific to a Unix-style shell if
3622such a shell is actually installed on the system where @code{make} runs.
3623
3624Note that this extended search for the shell is limited to the cases
3625where @code{SHELL} is set from the Makefile; if it is set in the
3626environment or command line, you are expected to set it to the full
3627pathname of the shell, exactly as things are on Unix.
3628
3629The effect of the above DOS-specific processing is that a Makefile that
3630says @samp{SHELL = /bin/sh} (as many Unix makefiles do), will work
3631on MS-DOS unaltered if you have e.g. @file{sh.exe} installed in some
3632directory along your @code{PATH}.
3633
3634@cindex environment, @code{SHELL} in
3635@vindex MAKESHELL @r{(MS-DOS alternative to @code{SHELL})}
3636Unlike most variables, the variable @code{SHELL} is never set from the
3637environment. This is because the @code{SHELL} environment variable is
3638used to specify your personal choice of shell program for interactive
3639use. It would be very bad for personal choices like this to affect the
3640functioning of makefiles. @xref{Environment, ,Variables from the
3641Environment}. However, on MS-DOS and MS-Windows the value of
3642@code{SHELL} in the environment @strong{is} used, since on those systems
3643most users do not set this variable, and therefore it is most likely set
3644specifically to be used by @code{make}. On MS-DOS, if the setting of
3645@code{SHELL} is not suitable for @code{make}, you can set the variable
3646@code{MAKESHELL} to the shell that @code{make} should use; this will
3647override the value of @code{SHELL}.
3648
3649@node Parallel, Errors, Execution, Commands
3650@section Parallel Execution
3651@cindex commands, execution in parallel
3652@cindex parallel execution
3653@cindex execution, in parallel
3654@cindex job slots
3655@cindex @code{-j}
3656@cindex @code{--jobs}
3657
3658GNU @code{make} knows how to execute several commands at once.
3659Normally, @code{make} will execute only one command at a time, waiting
3660for it to finish before executing the next. However, the @samp{-j} or
3661@samp{--jobs} option tells @code{make} to execute many commands
3662simultaneously.@refill
3663
3664On MS-DOS, the @samp{-j} option has no effect, since that system doesn't
3665support multi-processing.
3666
3667If the @samp{-j} option is followed by an integer, this is the number of
3668commands to execute at once; this is called the number of @dfn{job slots}.
3669If there is nothing looking like an integer after the @samp{-j} option,
3670there is no limit on the number of job slots. The default number of job
3671slots is one, which means serial execution (one thing at a time).
3672
3673One unpleasant consequence of running several commands simultaneously is
3674that output generated by the commands appears whenever each command
3675sends it, so messages from different commands may be interspersed.
3676
3677Another problem is that two processes cannot both take input from the
3678same device; so to make sure that only one command tries to take input
3679from the terminal at once, @code{make} will invalidate the standard
3680input streams of all but one running command. This means that
3681attempting to read from standard input will usually be a fatal error (a
3682@samp{Broken pipe} signal) for most child processes if there are
3683several.
3684@cindex broken pipe
3685@cindex standard input
3686
3687It is unpredictable which command will have a valid standard input stream
3688(which will come from the terminal, or wherever you redirect the standard
3689input of @code{make}). The first command run will always get it first, and
3690the first command started after that one finishes will get it next, and so
3691on.
3692
3693We will change how this aspect of @code{make} works if we find a better
3694alternative. In the mean time, you should not rely on any command using
3695standard input at all if you are using the parallel execution feature; but
3696if you are not using this feature, then standard input works normally in
3697all commands.
3698
3699Finally, handling recursive @code{make} invocations raises issues. For
3700more information on this, see
3701@ref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
3702
3703If a command fails (is killed by a signal or exits with a nonzero
3704status), and errors are not ignored for that command
3705(@pxref{Errors, ,Errors in Commands}),
3706the remaining command lines to remake the same target will not be run.
3707If a command fails and the @samp{-k} or @samp{--keep-going}
3708option was not given
3709(@pxref{Options Summary, ,Summary of Options}),
3710@code{make} aborts execution. If make
3711terminates for any reason (including a signal) with child processes
3712running, it waits for them to finish before actually exiting.@refill
3713
3714@cindex load average
3715@cindex limiting jobs based on load
3716@cindex jobs, limiting based on load
3717@cindex @code{-l} (load average)
3718@cindex @code{--max-load}
3719@cindex @code{--load-average}
3720When the system is heavily loaded, you will probably want to run fewer jobs
3721than when it is lightly loaded. You can use the @samp{-l} option to tell
3722@code{make} to limit the number of jobs to run at once, based on the load
3723average. The @samp{-l} or @samp{--max-load}
3724option is followed by a floating-point number. For
3725example,
3726
3727@example
3728-l 2.5
3729@end example
3730
3731@noindent
3732will not let @code{make} start more than one job if the load average is
3733above 2.5. The @samp{-l} option with no following number removes the
3734load limit, if one was given with a previous @samp{-l} option.@refill
3735
3736More precisely, when @code{make} goes to start up a job, and it already has
3737at least one job running, it checks the current load average; if it is not
3738lower than the limit given with @samp{-l}, @code{make} waits until the load
3739average goes below that limit, or until all the other jobs finish.
3740
3741By default, there is no load limit.
3742
3743@node Errors, Interrupts, Parallel, Commands
3744@section Errors in Commands
3745@cindex errors (in commands)
3746@cindex commands, errors in
3747@cindex exit status (errors)
3748
3749After each shell command returns, @code{make} looks at its exit status.
3750If the command completed successfully, the next command line is executed
3751in a new shell; after the last command line is finished, the rule is
3752finished.
3753
3754If there is an error (the exit status is nonzero), @code{make} gives up on
3755the current rule, and perhaps on all rules.
3756
3757Sometimes the failure of a certain command does not indicate a problem.
3758For example, you may use the @code{mkdir} command to ensure that a
3759directory exists. If the directory already exists, @code{mkdir} will
3760report an error, but you probably want @code{make} to continue regardless.
3761
3762@cindex @code{-} (in commands)
3763To ignore errors in a command line, write a @samp{-} at the beginning of
3764the line's text (after the initial tab). The @samp{-} is discarded before
3765the command is passed to the shell for execution.
3766
3767For example,
3768
3769@example
3770@group
3771clean:
3772 -rm -f *.o
3773@end group
3774@end example
3775@cindex @code{rm} (shell command)
3776
3777@noindent
3778This causes @code{rm} to continue even if it is unable to remove a file.
3779
3780@cindex @code{-i}
3781@cindex @code{--ignore-errors}
3782@findex .IGNORE
3783When you run @code{make} with the @samp{-i} or @samp{--ignore-errors}
3784flag, errors are ignored in all commands of all rules. A rule in the
3785makefile for the special target @code{.IGNORE} has the same effect, if
3786there are no prerequisites. These ways of ignoring errors are obsolete
3787because @samp{-} is more flexible.
3788
3789When errors are to be ignored, because of either a @samp{-} or the
3790@samp{-i} flag, @code{make} treats an error return just like success,
3791except that it prints out a message that tells you the status code
3792the command exited with, and says that the error has been ignored.
3793
3794When an error happens that @code{make} has not been told to ignore,
3795it implies that the current target cannot be correctly remade, and neither
3796can any other that depends on it either directly or indirectly. No further
3797commands will be executed for these targets, since their preconditions
3798have not been achieved.
3799
3800
3801@cindex @code{-k}
3802@cindex @code{--keep-going}
3803Normally @code{make} gives up immediately in this circumstance, returning a
3804nonzero status. However, if the @samp{-k} or @samp{--keep-going}
3805flag is specified, @code{make}
3806continues to consider the other prerequisites of the pending targets,
3807remaking them if necessary, before it gives up and returns nonzero status.
3808For example, after an error in compiling one object file, @samp{make -k}
3809will continue compiling other object files even though it already knows
3810that linking them will be impossible. @xref{Options Summary, ,Summary of Options}.
3811
3812The usual behavior assumes that your purpose is to get the specified
3813targets up to date; once @code{make} learns that this is impossible, it
3814might as well report the failure immediately. The @samp{-k} option says
3815that the real purpose is to test as many of the changes made in the
3816program as possible, perhaps to find several independent problems so
3817that you can correct them all before the next attempt to compile. This
3818is why Emacs' @code{compile} command passes the @samp{-k} flag by
3819default.
3820@cindex Emacs (@code{M-x compile})
3821
3822@findex .DELETE_ON_ERROR
3823@cindex deletion of target files
3824@cindex removal of target files
3825@cindex target, deleting on error
3826Usually when a command fails, if it has changed the target file at all,
3827the file is corrupted and cannot be used---or at least it is not
3828completely updated. Yet the file's time stamp says that it is now up to
3829date, so the next time @code{make} runs, it will not try to update that
3830file. The situation is just the same as when the command is killed by a
3831signal; @pxref{Interrupts}. So generally the right thing to do is to
3832delete the target file if the command fails after beginning to change
3833the file. @code{make} will do this if @code{.DELETE_ON_ERROR} appears
3834as a target. This is almost always what you want @code{make} to do, but
3835it is not historical practice; so for compatibility, you must explicitly
3836request it.
3837
3838@node Interrupts, Recursion, Errors, Commands
3839@section Interrupting or Killing @code{make}
3840@cindex interrupt
3841@cindex signal
3842@cindex deletion of target files
3843@cindex removal of target files
3844@cindex target, deleting on interrupt
3845@cindex killing (interruption)
3846
3847If @code{make} gets a fatal signal while a command is executing, it may
3848delete the target file that the command was supposed to update. This is
3849done if the target file's last-modification time has changed since
3850@code{make} first checked it.
3851
3852The purpose of deleting the target is to make sure that it is remade from
3853scratch when @code{make} is next run. Why is this? Suppose you type
3854@kbd{Ctrl-c} while a compiler is running, and it has begun to write an
3855object file @file{foo.o}. The @kbd{Ctrl-c} kills the compiler, resulting
3856in an incomplete file whose last-modification time is newer than the source
3857file @file{foo.c}. But @code{make} also receives the @kbd{Ctrl-c} signal
3858and deletes this incomplete file. If @code{make} did not do this, the next
3859invocation of @code{make} would think that @file{foo.o} did not require
3860updating---resulting in a strange error message from the linker when it
3861tries to link an object file half of which is missing.
3862
3863@findex .PRECIOUS
3864You can prevent the deletion of a target file in this way by making the
3865special target @code{.PRECIOUS} depend on it. Before remaking a target,
3866@code{make} checks to see whether it appears on the prerequisites of
3867@code{.PRECIOUS}, and thereby decides whether the target should be deleted
3868if a signal happens. Some reasons why you might do this are that the
3869target is updated in some atomic fashion, or exists only to record a
3870modification-time (its contents do not matter), or must exist at all
3871times to prevent other sorts of trouble.
3872
3873@node Recursion, Sequences, Interrupts, Commands
3874@section Recursive Use of @code{make}
3875@cindex recursion
3876@cindex subdirectories, recursion for
3877
3878Recursive use of @code{make} means using @code{make} as a command in a
3879makefile. This technique is useful when you want separate makefiles for
3880various subsystems that compose a larger system. For example, suppose you
3881have a subdirectory @file{subdir} which has its own makefile, and you would
3882like the containing directory's makefile to run @code{make} on the
3883subdirectory. You can do it by writing this:
3884
3885@example
3886subsystem:
3887 cd subdir && $(MAKE)
3888@end example
3889
3890@noindent
3891or, equivalently, this (@pxref{Options Summary, ,Summary of Options}):
3892
3893@example
3894subsystem:
3895 $(MAKE) -C subdir
3896@end example
3897@cindex @code{-C}
3898@cindex @code{--directory}
3899
3900You can write recursive @code{make} commands just by copying this example,
3901but there are many things to know about how they work and why, and about
3902how the sub-@code{make} relates to the top-level @code{make}. You may
3903also find it useful to declare targets that invoke recursive
3904@code{make} commands as @samp{.PHONY} (for more discussion on when
3905this is useful, see @ref{Phony Targets}).
3906
3907@vindex @code{CURDIR}
3908For your convenience, when GNU @code{make} starts (after it has
3909processed any @code{-C} options) it sets the variable @code{CURDIR} to
3910the pathname of the current working directory. This value is never
3911touched by @code{make} again: in particular note that if you include
3912files from other directories the value of @code{CURDIR} does not
3913change. The value has the same precedence it would have if it were
3914set in the makefile (by default, an environment variable @code{CURDIR}
3915will not override this value). Note that setting this variable has no
3916impact on the operation of @code{make} (it does not cause @code{make}
3917to change its working directory, for example).
3918
3919@menu
3920* MAKE Variable:: The special effects of using @samp{$(MAKE)}.
3921* Variables/Recursion:: How to communicate variables to a sub-@code{make}.
3922* Options/Recursion:: How to communicate options to a sub-@code{make}.
3923* -w Option:: How the @samp{-w} or @samp{--print-directory} option
3924 helps debug use of recursive @code{make} commands.
3925@end menu
3926
3927@node MAKE Variable, Variables/Recursion, Recursion, Recursion
3928@subsection How the @code{MAKE} Variable Works
3929@vindex MAKE
3930@cindex recursion, and @code{MAKE} variable
3931
3932Recursive @code{make} commands should always use the variable @code{MAKE},
3933not the explicit command name @samp{make}, as shown here:
3934
3935@example
3936@group
3937subsystem:
3938 cd subdir && $(MAKE)
3939@end group
3940@end example
3941
3942The value of this variable is the file name with which @code{make} was
3943invoked. If this file name was @file{/bin/make}, then the command executed
3944is @samp{cd subdir && /bin/make}. If you use a special version of
3945@code{make} to run the top-level makefile, the same special version will be
3946executed for recursive invocations.
3947@cindex @code{cd} (shell command)
3948
3949@cindex +, and commands
3950As a special feature, using the variable @code{MAKE} in the commands of
3951a rule alters the effects of the @samp{-t} (@samp{--touch}), @samp{-n}
3952(@samp{--just-print}), or @samp{-q} (@w{@samp{--question}}) option.
3953Using the @code{MAKE} variable has the same effect as using a @samp{+}
3954character at the beginning of the command line. @xref{Instead of
3955Execution, ,Instead of Executing the Commands}. This special feature
3956is only enabled if the @code{MAKE} variable appears directly in the
3957command script: it does not apply if the @code{MAKE} variable is
3958referenced through expansion of another variable. In the latter case
3959you must use the @samp{+} token to get these special effects.@refill
3960
3961Consider the command @samp{make -t} in the above example. (The
3962@samp{-t} option marks targets as up to date without actually running
3963any commands; see @ref{Instead of Execution}.) Following the usual
3964definition of @samp{-t}, a @samp{make -t} command in the example would
3965create a file named @file{subsystem} and do nothing else. What you
3966really want it to do is run @samp{@w{cd subdir &&} @w{make -t}}; but that would
3967require executing the command, and @samp{-t} says not to execute
3968commands.@refill
3969@cindex @code{-t}, and recursion
3970@cindex recursion, and @code{-t}
3971@cindex @code{--touch}, and recursion
3972
3973The special feature makes this do what you want: whenever a command
3974line of a rule contains the variable @code{MAKE}, the flags @samp{-t},
3975@samp{-n} and @samp{-q} do not apply to that line. Command lines
3976containing @code{MAKE} are executed normally despite the presence of a
3977flag that causes most commands not to be run. The usual
3978@code{MAKEFLAGS} mechanism passes the flags to the sub-@code{make}
3979(@pxref{Options/Recursion, ,Communicating Options to a
3980Sub-@code{make}}), so your request to touch the files, or print the
3981commands, is propagated to the subsystem.@refill
3982
3983@node Variables/Recursion, Options/Recursion, MAKE Variable, Recursion
3984@subsection Communicating Variables to a Sub-@code{make}
3985@cindex sub-@code{make}
3986@cindex environment, and recursion
3987@cindex exporting variables
3988@cindex variables, environment
3989@cindex variables, exporting
3990@cindex recursion, and environment
3991@cindex recursion, and variables
3992
3993Variable values of the top-level @code{make} can be passed to the
3994sub-@code{make} through the environment by explicit request. These
3995variables are defined in the sub-@code{make} as defaults, but do not
3996override what is specified in the makefile used by the sub-@code{make}
3997makefile unless you use the @samp{-e} switch (@pxref{Options Summary,
3998,Summary of Options}).@refill
3999
4000To pass down, or @dfn{export}, a variable, @code{make} adds the variable
4001and its value to the environment for running each command. The
4002sub-@code{make}, in turn, uses the environment to initialize its table
4003of variable values. @xref{Environment, ,Variables from the
4004Environment}.
4005
4006Except by explicit request, @code{make} exports a variable only if it
4007is either defined in the environment initially or set on the command
4008line, and if its name consists only of letters, numbers, and underscores.
4009Some shells cannot cope with environment variable names consisting of
4010characters other than letters, numbers, and underscores.
4011
4012@cindex SHELL, exported value
4013The value of the @code{make} variable @code{SHELL} is not exported.
4014Instead, the value of the @code{SHELL} variable from the invoking
4015environment is passed to the sub-@code{make}. You can force
4016@code{make} to export its value for @code{SHELL} by using the
4017@code{export} directive, described below.
4018
4019The special variable @code{MAKEFLAGS} is always exported (unless you
4020unexport it). @code{MAKEFILES} is exported if you set it to anything.
4021
4022@code{make} automatically passes down variable values that were defined
4023on the command line, by putting them in the @code{MAKEFLAGS} variable.
4024@iftex
4025See the next section.
4026@end iftex
4027@ifnottex
4028@xref{Options/Recursion}.
4029@end ifnottex
4030
4031Variables are @emph{not} normally passed down if they were created by
4032default by @code{make} (@pxref{Implicit Variables, ,Variables Used by
4033Implicit Rules}). The sub-@code{make} will define these for
4034itself.@refill
4035
4036@findex export
4037If you want to export specific variables to a sub-@code{make}, use the
4038@code{export} directive, like this:
4039
4040@example
4041export @var{variable} @dots{}
4042@end example
4043
4044@noindent
4045@findex unexport
4046If you want to @emph{prevent} a variable from being exported, use the
4047@code{unexport} directive, like this:
4048
4049@example
4050unexport @var{variable} @dots{}
4051@end example
4052
4053@noindent
4054In both of these forms, the arguments to @code{export} and
4055@code{unexport} are expanded, and so could be variables or functions
4056which expand to a (list of) variable names to be (un)exported.
4057
4058As a convenience, you can define a variable and export it at the same
4059time by doing:
4060
4061@example
4062export @var{variable} = value
4063@end example
4064
4065@noindent
4066has the same result as:
4067
4068@example
4069@var{variable} = value
4070export @var{variable}
4071@end example
4072
4073@noindent
4074and
4075
4076@example
4077export @var{variable} := value
4078@end example
4079
4080@noindent
4081has the same result as:
4082
4083@example
4084@var{variable} := value
4085export @var{variable}
4086@end example
4087
4088Likewise,
4089
4090@example
4091export @var{variable} += value
4092@end example
4093
4094@noindent
4095is just like:
4096
4097@example
4098@var{variable} += value
4099export @var{variable}
4100@end example
4101
4102@noindent
4103@xref{Appending, ,Appending More Text to Variables}.
4104
4105You may notice that the @code{export} and @code{unexport} directives
4106work in @code{make} in the same way they work in the shell, @code{sh}.
4107
4108If you want all variables to be exported by default, you can use
4109@code{export} by itself:
4110
4111@example
4112export
4113@end example
4114
4115@noindent
4116This tells @code{make} that variables which are not explicitly mentioned
4117in an @code{export} or @code{unexport} directive should be exported.
4118Any variable given in an @code{unexport} directive will still @emph{not}
4119be exported. If you use @code{export} by itself to export variables by
4120default, variables whose names contain characters other than
4121alphanumerics and underscores will not be exported unless specifically
4122mentioned in an @code{export} directive.@refill
4123
4124@findex .EXPORT_ALL_VARIABLES
4125The behavior elicited by an @code{export} directive by itself was the
4126default in older versions of GNU @code{make}. If your makefiles depend
4127on this behavior and you want to be compatible with old versions of
4128@code{make}, you can write a rule for the special target
4129@code{.EXPORT_ALL_VARIABLES} instead of using the @code{export} directive.
4130This will be ignored by old @code{make}s, while the @code{export}
4131directive will cause a syntax error.@refill
4132@cindex compatibility in exporting
4133
4134Likewise, you can use @code{unexport} by itself to tell @code{make}
4135@emph{not} to export variables by default. Since this is the default
4136behavior, you would only need to do this if @code{export} had been used
4137by itself earlier (in an included makefile, perhaps). You
4138@strong{cannot} use @code{export} and @code{unexport} by themselves to
4139have variables exported for some commands and not for others. The last
4140@code{export} or @code{unexport} directive that appears by itself
4141determines the behavior for the entire run of @code{make}.@refill
4142
4143@vindex MAKELEVEL
4144@cindex recursion, level of
4145As a special feature, the variable @code{MAKELEVEL} is changed when it
4146is passed down from level to level. This variable's value is a string
4147which is the depth of the level as a decimal number. The value is
4148@samp{0} for the top-level @code{make}; @samp{1} for a sub-@code{make},
4149@samp{2} for a sub-sub-@code{make}, and so on. The incrementation
4150happens when @code{make} sets up the environment for a command.@refill
4151
4152The main use of @code{MAKELEVEL} is to test it in a conditional
4153directive (@pxref{Conditionals, ,Conditional Parts of Makefiles}); this
4154way you can write a makefile that behaves one way if run recursively and
4155another way if run directly by you.@refill
4156
4157@vindex MAKEFILES
4158You can use the variable @code{MAKEFILES} to cause all sub-@code{make}
4159commands to use additional makefiles. The value of @code{MAKEFILES} is
4160a whitespace-separated list of file names. This variable, if defined in
4161the outer-level makefile, is passed down through the environment; then
4162it serves as a list of extra makefiles for the sub-@code{make} to read
4163before the usual or specified ones. @xref{MAKEFILES Variable, ,The
4164Variable @code{MAKEFILES}}.@refill
4165
4166@node Options/Recursion, -w Option, Variables/Recursion, Recursion
4167@subsection Communicating Options to a Sub-@code{make}
4168@cindex options, and recursion
4169@cindex recursion, and options
4170
4171@vindex MAKEFLAGS
4172Flags such as @samp{-s} and @samp{-k} are passed automatically to the
4173sub-@code{make} through the variable @code{MAKEFLAGS}. This variable is
4174set up automatically by @code{make} to contain the flag letters that
4175@code{make} received. Thus, if you do @w{@samp{make -ks}} then
4176@code{MAKEFLAGS} gets the value @samp{ks}.@refill
4177
4178As a consequence, every sub-@code{make} gets a value for @code{MAKEFLAGS}
4179in its environment. In response, it takes the flags from that value and
4180processes them as if they had been given as arguments.
4181@xref{Options Summary, ,Summary of Options}.
4182
4183@cindex command line variable definitions, and recursion
4184@cindex variables, command line, and recursion
4185@cindex recursion, and command line variable definitions
4186Likewise variables defined on the command line are passed to the
4187sub-@code{make} through @code{MAKEFLAGS}. Words in the value of
4188@code{MAKEFLAGS} that contain @samp{=}, @code{make} treats as variable
4189definitions just as if they appeared on the command line.
4190@xref{Overriding, ,Overriding Variables}.
4191
4192@cindex @code{-C}, and recursion
4193@cindex @code{-f}, and recursion
4194@cindex @code{-o}, and recursion
4195@cindex @code{-W}, and recursion
4196@cindex @code{--directory}, and recursion
4197@cindex @code{--file}, and recursion
4198@cindex @code{--old-file}, and recursion
4199@cindex @code{--assume-old}, and recursion
4200@cindex @code{--assume-new}, and recursion
4201@cindex @code{--new-file}, and recursion
4202@cindex recursion, and @code{-C}
4203@cindex recursion, and @code{-f}
4204@cindex recursion, and @code{-o}
4205@cindex recursion, and @code{-W}
4206The options @samp{-C}, @samp{-f}, @samp{-o}, and @samp{-W} are not put
4207into @code{MAKEFLAGS}; these options are not passed down.@refill
4208
4209@cindex @code{-j}, and recursion
4210@cindex @code{--jobs}, and recursion
4211@cindex recursion, and @code{-j}
4212@cindex job slots, and recursion
4213The @samp{-j} option is a special case (@pxref{Parallel, ,Parallel Execution}).
4214If you set it to some numeric value @samp{N} and your operating system
4215supports it (most any UNIX system will; others typically won't), the
4216parent @code{make} and all the sub-@code{make}s will communicate to
4217ensure that there are only @samp{N} jobs running at the same time
4218between them all. Note that any job that is marked recursive
4219(@pxref{Instead of Execution, ,Instead of Executing the Commands})
4220doesn't count against the total jobs (otherwise we could get @samp{N}
4221sub-@code{make}s running and have no slots left over for any real work!)
4222
4223If your operating system doesn't support the above communication, then
4224@samp{-j 1} is always put into @code{MAKEFLAGS} instead of the value you
4225specified. This is because if the @w{@samp{-j}} option were passed down
4226to sub-@code{make}s, you would get many more jobs running in parallel
4227than you asked for. If you give @samp{-j} with no numeric argument,
4228meaning to run as many jobs as possible in parallel, this is passed
4229down, since multiple infinities are no more than one.@refill
4230
4231If you do not want to pass the other flags down, you must change the
4232value of @code{MAKEFLAGS}, like this:
4233
4234@example
4235subsystem:
4236 cd subdir && $(MAKE) MAKEFLAGS=
4237@end example
4238
4239@vindex MAKEOVERRIDES
4240The command line variable definitions really appear in the variable
4241@code{MAKEOVERRIDES}, and @code{MAKEFLAGS} contains a reference to this
4242variable. If you do want to pass flags down normally, but don't want to
4243pass down the command line variable definitions, you can reset
4244@code{MAKEOVERRIDES} to empty, like this:
4245
4246@example
4247MAKEOVERRIDES =
4248@end example
4249
4250@noindent
4251@cindex Arg list too long
4252@cindex E2BIG
4253This is not usually useful to do. However, some systems have a small
4254fixed limit on the size of the environment, and putting so much
4255information into the value of @code{MAKEFLAGS} can exceed it. If you
4256see the error message @samp{Arg list too long}, this may be the problem.
4257@findex .POSIX
4258@cindex POSIX.2
4259(For strict compliance with POSIX.2, changing @code{MAKEOVERRIDES} does
4260not affect @code{MAKEFLAGS} if the special target @samp{.POSIX} appears
4261in the makefile. You probably do not care about this.)
4262
4263@vindex MFLAGS
4264A similar variable @code{MFLAGS} exists also, for historical
4265compatibility. It has the same value as @code{MAKEFLAGS} except that it
4266does not contain the command line variable definitions, and it always
4267begins with a hyphen unless it is empty (@code{MAKEFLAGS} begins with a
4268hyphen only when it begins with an option that has no single-letter
4269version, such as @samp{--warn-undefined-variables}). @code{MFLAGS} was
4270traditionally used explicitly in the recursive @code{make} command, like
4271this:
4272
4273@example
4274subsystem:
4275 cd subdir && $(MAKE) $(MFLAGS)
4276@end example
4277
4278@noindent
4279but now @code{MAKEFLAGS} makes this usage redundant. If you want your
4280makefiles to be compatible with old @code{make} programs, use this
4281technique; it will work fine with more modern @code{make} versions too.
4282
4283@cindex setting options from environment
4284@cindex options, setting from environment
4285@cindex setting options in makefiles
4286@cindex options, setting in makefiles
4287The @code{MAKEFLAGS} variable can also be useful if you want to have
4288certain options, such as @samp{-k} (@pxref{Options Summary, ,Summary of
4289Options}), set each time you run @code{make}. You simply put a value for
4290@code{MAKEFLAGS} in your environment. You can also set @code{MAKEFLAGS} in
4291a makefile, to specify additional flags that should also be in effect for
4292that makefile. (Note that you cannot use @code{MFLAGS} this way. That
4293variable is set only for compatibility; @code{make} does not interpret a
4294value you set for it in any way.)
4295
4296When @code{make} interprets the value of @code{MAKEFLAGS} (either from the
4297environment or from a makefile), it first prepends a hyphen if the value
4298does not already begin with one. Then it chops the value into words
4299separated by blanks, and parses these words as if they were options given
4300on the command line (except that @samp{-C}, @samp{-f}, @samp{-h},
4301@samp{-o}, @samp{-W}, and their long-named versions are ignored; and there
4302is no error for an invalid option).
4303
4304If you do put @code{MAKEFLAGS} in your environment, you should be sure not
4305to include any options that will drastically affect the actions of
4306@code{make} and undermine the purpose of makefiles and of @code{make}
4307itself. For instance, the @samp{-t}, @samp{-n}, and @samp{-q} options, if
4308put in one of these variables, could have disastrous consequences and would
4309certainly have at least surprising and probably annoying effects.@refill
4310
4311@node -w Option, , Options/Recursion, Recursion
4312@subsection The @samp{--print-directory} Option
4313@cindex directories, printing them
4314@cindex printing directories
4315@cindex recursion, and printing directories
4316
4317If you use several levels of recursive @code{make} invocations, the
4318@samp{-w} or @w{@samp{--print-directory}} option can make the output a
4319lot easier to understand by showing each directory as @code{make}
4320starts processing it and as @code{make} finishes processing it. For
4321example, if @samp{make -w} is run in the directory @file{/u/gnu/make},
4322@code{make} will print a line of the form:@refill
4323
4324@example
4325make: Entering directory `/u/gnu/make'.
4326@end example
4327
4328@noindent
4329before doing anything else, and a line of the form:
4330
4331@example
4332make: Leaving directory `/u/gnu/make'.
4333@end example
4334
4335@noindent
4336when processing is completed.
4337
4338@cindex @code{-C}, and @code{-w}
4339@cindex @code{--directory}, and @code{--print-directory}
4340@cindex recursion, and @code{-w}
4341@cindex @code{-w}, and @code{-C}
4342@cindex @code{-w}, and recursion
4343@cindex @code{--print-directory}, and @code{--directory}
4344@cindex @code{--print-directory}, and recursion
4345@cindex @code{--no-print-directory}
4346@cindex @code{--print-directory}, disabling
4347@cindex @code{-w}, disabling
4348Normally, you do not need to specify this option because @samp{make}
4349does it for you: @samp{-w} is turned on automatically when you use the
4350@samp{-C} option, and in sub-@code{make}s. @code{make} will not
4351automatically turn on @samp{-w} if you also use @samp{-s}, which says to
4352be silent, or if you use @samp{--no-print-directory} to explicitly
4353disable it.
4354
4355@node Sequences, Empty Commands, Recursion, Commands
4356@section Defining Canned Command Sequences
4357@cindex sequences of commands
4358@cindex commands, sequences of
4359
4360When the same sequence of commands is useful in making various targets, you
4361can define it as a canned sequence with the @code{define} directive, and
4362refer to the canned sequence from the rules for those targets. The canned
4363sequence is actually a variable, so the name must not conflict with other
4364variable names.
4365
4366Here is an example of defining a canned sequence of commands:
4367
4368@example
4369define run-yacc
4370yacc $(firstword $^)
4371mv y.tab.c $@@
4372endef
4373@end example
4374@cindex @code{yacc}
4375
4376@noindent
4377Here @code{run-yacc} is the name of the variable being defined;
4378@code{endef} marks the end of the definition; the lines in between are the
4379commands. The @code{define} directive does not expand variable references
4380and function calls in the canned sequence; the @samp{$} characters,
4381parentheses, variable names, and so on, all become part of the value of the
4382variable you are defining.
4383@xref{Defining, ,Defining Variables Verbatim},
4384for a complete explanation of @code{define}.
4385
4386The first command in this example runs Yacc on the first prerequisite of
4387whichever rule uses the canned sequence. The output file from Yacc is
4388always named @file{y.tab.c}. The second command moves the output to the
4389rule's target file name.
4390
4391To use the canned sequence, substitute the variable into the commands of a
4392rule. You can substitute it like any other variable
4393(@pxref{Reference, ,Basics of Variable References}).
4394Because variables defined by @code{define} are recursively expanded
4395variables, all the variable references you wrote inside the @code{define}
4396are expanded now. For example:
4397
4398@example
4399foo.c : foo.y
4400 $(run-yacc)
4401@end example
4402
4403@noindent
4404@samp{foo.y} will be substituted for the variable @samp{$^} when it occurs in
4405@code{run-yacc}'s value, and @samp{foo.c} for @samp{$@@}.@refill
4406
4407This is a realistic example, but this particular one is not needed in
4408practice because @code{make} has an implicit rule to figure out these
4409commands based on the file names involved
4410(@pxref{Implicit Rules, ,Using Implicit Rules}).
4411
4412@cindex @@, and @code{define}
4413@cindex -, and @code{define}
4414@cindex +, and @code{define}
4415In command execution, each line of a canned sequence is treated just as
4416if the line appeared on its own in the rule, preceded by a tab. In
4417particular, @code{make} invokes a separate subshell for each line. You
4418can use the special prefix characters that affect command lines
4419(@samp{@@}, @samp{-}, and @samp{+}) on each line of a canned sequence.
4420@xref{Commands, ,Writing the Commands in Rules}.
4421For example, using this canned sequence:
4422
4423@example
4424define frobnicate
4425@@echo "frobnicating target $@@"
4426frob-step-1 $< -o $@@-step-1
4427frob-step-2 $@@-step-1 -o $@@
4428endef
4429@end example
4430
4431@noindent
4432@code{make} will not echo the first line, the @code{echo} command.
4433But it @emph{will} echo the following two command lines.
4434
4435On the other hand, prefix characters on the command line that refers to
4436a canned sequence apply to every line in the sequence. So the rule:
4437
4438@example
4439frob.out: frob.in
4440 @@$(frobnicate)
4441@end example
4442
4443@noindent
4444does not echo @emph{any} commands.
4445(@xref{Echoing, ,Command Echoing}, for a full explanation of @samp{@@}.)
4446
4447@node Empty Commands, , Sequences, Commands
4448@section Using Empty Commands
4449@cindex empty commands
4450@cindex commands, empty
4451
4452It is sometimes useful to define commands which do nothing. This is done
4453simply by giving a command that consists of nothing but whitespace. For
4454example:
4455
4456@example
4457target: ;
4458@end example
4459
4460@noindent
4461defines an empty command string for @file{target}. You could also use a
4462line beginning with a tab character to define an empty command string,
4463but this would be confusing because such a line looks empty.
4464
4465@findex .DEFAULT@r{, and empty commands}
4466You may be wondering why you would want to define a command string that
4467does nothing. The only reason this is useful is to prevent a target
4468from getting implicit commands (from implicit rules or the
4469@code{.DEFAULT} special target; @pxref{Implicit Rules} and
4470@pxref{Last Resort, ,Defining Last-Resort Default Rules}).@refill
4471
4472@c !!! another reason is for canonical stamp files:
4473@ignore
4474@example
4475foo: stamp-foo ;
4476stamp-foo: foo.in
4477 create foo frm foo.in
4478 touch $@
4479@end example
4480@end ignore
4481
4482You may be inclined to define empty command strings for targets that are
4483not actual files, but only exist so that their prerequisites can be
4484remade. However, this is not the best way to do that, because the
4485prerequisites may not be remade properly if the target file actually does exist.
4486@xref{Phony Targets, ,Phony Targets}, for a better way to do this.
4487
4488@node Using Variables, Conditionals, Commands, Top
4489@chapter How to Use Variables
4490@cindex variable
4491@cindex value
4492@cindex recursive variable expansion
4493@cindex simple variable expansion
4494
4495A @dfn{variable} is a name defined in a makefile to represent a string
4496of text, called the variable's @dfn{value}. These values are
4497substituted by explicit request into targets, prerequisites, commands,
4498and other parts of the makefile. (In some other versions of @code{make},
4499variables are called @dfn{macros}.)
4500@cindex macro
4501
4502Variables and functions in all parts of a makefile are expanded when
4503read, except for the shell commands in rules, the right-hand sides of
4504variable definitions using @samp{=}, and the bodies of variable
4505definitions using the @code{define} directive.@refill
4506
4507Variables can represent lists of file names, options to pass to compilers,
4508programs to run, directories to look in for source files, directories to
4509write output in, or anything else you can imagine.
4510
4511A variable name may be any sequence of characters not containing @samp{:},
4512@samp{#}, @samp{=}, or leading or trailing whitespace. However,
4513variable names containing characters other than letters, numbers, and
4514underscores should be avoided, as they may be given special meanings in the
4515future, and with some shells they cannot be passed through the environment to a
4516sub-@code{make}
4517(@pxref{Variables/Recursion, ,Communicating Variables to a Sub-@code{make}}).
4518
4519Variable names are case-sensitive. The names @samp{foo}, @samp{FOO},
4520and @samp{Foo} all refer to different variables.
4521
4522It is traditional to use upper case letters in variable names, but we
4523recommend using lower case letters for variable names that serve internal
4524purposes in the makefile, and reserving upper case for parameters that
4525control implicit rules or for parameters that the user should override with
4526command options (@pxref{Overriding, ,Overriding Variables}).
4527
4528A few variables have names that are a single punctuation character or
4529just a few characters. These are the @dfn{automatic variables}, and
4530they have particular specialized uses. @xref{Automatic Variables}.
4531
4532@menu
4533* Reference:: How to use the value of a variable.
4534* Flavors:: Variables come in two flavors.
4535* Advanced:: Advanced features for referencing a variable.
4536* Values:: All the ways variables get their values.
4537* Setting:: How to set a variable in the makefile.
4538* Appending:: How to append more text to the old value
4539 of a variable.
4540* Override Directive:: How to set a variable in the makefile even if
4541 the user has set it with a command argument.
4542* Defining:: An alternate way to set a variable
4543 to a verbatim string.
4544* Environment:: Variable values can come from the environment.
4545* Target-specific:: Variable values can be defined on a per-target
4546 basis.
4547* Pattern-specific:: Target-specific variable values can be applied
4548 to a group of targets that match a pattern.
4549@end menu
4550
4551@node Reference, Flavors, Using Variables, Using Variables
4552@section Basics of Variable References
4553@cindex variables, how to reference
4554@cindex reference to variables
4555@cindex @code{$}, in variable reference
4556@cindex dollar sign (@code{$}), in variable reference
4557
4558To substitute a variable's value, write a dollar sign followed by the name
4559of the variable in parentheses or braces: either @samp{$(foo)} or
4560@samp{$@{foo@}} is a valid reference to the variable @code{foo}. This
4561special significance of @samp{$} is why you must write @samp{$$} to have
4562the effect of a single dollar sign in a file name or command.
4563
4564Variable references can be used in any context: targets, prerequisites,
4565commands, most directives, and new variable values. Here is an
4566example of a common case, where a variable holds the names of all the
4567object files in a program:
4568
4569@example
4570@group
4571objects = program.o foo.o utils.o
4572program : $(objects)
4573 cc -o program $(objects)
4574
4575$(objects) : defs.h
4576@end group
4577@end example
4578
4579Variable references work by strict textual substitution. Thus, the rule
4580
4581@example
4582@group
4583foo = c
4584prog.o : prog.$(foo)
4585 $(foo)$(foo) -$(foo) prog.$(foo)
4586@end group
4587@end example
4588
4589@noindent
4590could be used to compile a C program @file{prog.c}. Since spaces before
4591the variable value are ignored in variable assignments, the value of
4592@code{foo} is precisely @samp{c}. (Don't actually write your makefiles
4593this way!)
4594
4595A dollar sign followed by a character other than a dollar sign,
4596open-parenthesis or open-brace treats that single character as the
4597variable name. Thus, you could reference the variable @code{x} with
4598@samp{$x}. However, this practice is strongly discouraged, except in
4599the case of the automatic variables (@pxref{Automatic Variables}).
4600
4601@node Flavors, Advanced, Reference, Using Variables
4602@section The Two Flavors of Variables
4603@cindex flavors of variables
4604@cindex recursive variable expansion
4605@cindex variables, flavors
4606@cindex recursively expanded variables
4607@cindex variables, recursively expanded
4608
4609There are two ways that a variable in GNU @code{make} can have a value;
4610we call them the two @dfn{flavors} of variables. The two flavors are
4611distinguished in how they are defined and in what they do when expanded.
4612
4613@cindex =
4614The first flavor of variable is a @dfn{recursively expanded} variable.
4615Variables of this sort are defined by lines using @samp{=}
4616(@pxref{Setting, ,Setting Variables}) or by the @code{define} directive
4617(@pxref{Defining, ,Defining Variables Verbatim}). The value you specify
4618is installed verbatim; if it contains references to other variables,
4619these references are expanded whenever this variable is substituted (in
4620the course of expanding some other string). When this happens, it is
4621called @dfn{recursive expansion}.@refill
4622
4623For example,
4624
4625@example
4626foo = $(bar)
4627bar = $(ugh)
4628ugh = Huh?
4629
4630all:;echo $(foo)
4631@end example
4632
4633@noindent
4634will echo @samp{Huh?}: @samp{$(foo)} expands to @samp{$(bar)} which
4635expands to @samp{$(ugh)} which finally expands to @samp{Huh?}.@refill
4636
4637This flavor of variable is the only sort supported by other versions of
4638@code{make}. It has its advantages and its disadvantages. An advantage
4639(most would say) is that:
4640
4641@example
4642CFLAGS = $(include_dirs) -O
4643include_dirs = -Ifoo -Ibar
4644@end example
4645
4646@noindent
4647will do what was intended: when @samp{CFLAGS} is expanded in a command,
4648it will expand to @samp{-Ifoo -Ibar -O}. A major disadvantage is that you
4649cannot append something on the end of a variable, as in
4650
4651@example
4652CFLAGS = $(CFLAGS) -O
4653@end example
4654
4655@noindent
4656because it will cause an infinite loop in the variable expansion.
4657(Actually @code{make} detects the infinite loop and reports an error.)
4658@cindex loops in variable expansion
4659@cindex variables, loops in expansion
4660
4661Another disadvantage is that any functions
4662(@pxref{Functions, ,Functions for Transforming Text})
4663referenced in the definition will be executed every time the variable is
4664expanded. This makes @code{make} run slower; worse, it causes the
4665@code{wildcard} and @code{shell} functions to give unpredictable results
4666because you cannot easily control when they are called, or even how many
4667times.
4668
4669To avoid all the problems and inconveniences of recursively expanded
4670variables, there is another flavor: simply expanded variables.
4671
4672@cindex simply expanded variables
4673@cindex variables, simply expanded
4674@cindex :=
4675@dfn{Simply expanded variables} are defined by lines using @samp{:=}
4676(@pxref{Setting, ,Setting Variables}).
4677The value of a simply expanded variable is scanned
4678once and for all, expanding any references to other variables and
4679functions, when the variable is defined. The actual value of the simply
4680expanded variable is the result of expanding the text that you write.
4681It does not contain any references to other variables; it contains their
4682values @emph{as of the time this variable was defined}. Therefore,
4683
4684@example
4685x := foo
4686y := $(x) bar
4687x := later
4688@end example
4689
4690@noindent
4691is equivalent to
4692
4693@example
4694y := foo bar
4695x := later
4696@end example
4697
4698When a simply expanded variable is referenced, its value is substituted
4699verbatim.
4700
4701Here is a somewhat more complicated example, illustrating the use of
4702@samp{:=} in conjunction with the @code{shell} function.
4703(@xref{Shell Function, , The @code{shell} Function}.) This example
4704also shows use of the variable @code{MAKELEVEL}, which is changed
4705when it is passed down from level to level.
4706(@xref{Variables/Recursion, , Communicating Variables to a
4707Sub-@code{make}}, for information about @code{MAKELEVEL}.)
4708
4709@vindex MAKELEVEL
4710@vindex MAKE
4711@example
4712@group
4713ifeq (0,$@{MAKELEVEL@})
4714cur-dir := $(shell pwd)
4715whoami := $(shell whoami)
4716host-type := $(shell arch)
4717MAKE := $@{MAKE@} host-type=$@{host-type@} whoami=$@{whoami@}
4718endif
4719@end group
4720@end example
4721
4722@noindent
4723An advantage of this use of @samp{:=} is that a typical
4724`descend into a directory' command then looks like this:
4725
4726@example
4727@group
4728$@{subdirs@}:
4729 $@{MAKE@} cur-dir=$@{cur-dir@}/$@@ -C $@@ all
4730@end group
4731@end example
4732
4733Simply expanded variables generally make complicated makefile programming
4734more predictable because they work like variables in most programming
4735languages. They allow you to redefine a variable using its own value (or
4736its value processed in some way by one of the expansion functions) and to
4737use the expansion functions much more efficiently
4738(@pxref{Functions, ,Functions for Transforming Text}).
4739
4740@cindex spaces, in variable values
4741@cindex whitespace, in variable values
4742@cindex variables, spaces in values
4743You can also use them to introduce controlled leading whitespace into
4744variable values. Leading whitespace characters are discarded from your
4745input before substitution of variable references and function calls;
4746this means you can include leading spaces in a variable value by
4747protecting them with variable references, like this:
4748
4749@example
4750nullstring :=
4751space := $(nullstring) # end of the line
4752@end example
4753
4754@noindent
4755Here the value of the variable @code{space} is precisely one space. The
4756comment @w{@samp{# end of the line}} is included here just for clarity.
4757Since trailing space characters are @emph{not} stripped from variable
4758values, just a space at the end of the line would have the same effect
4759(but be rather hard to read). If you put whitespace at the end of a
4760variable value, it is a good idea to put a comment like that at the end
4761of the line to make your intent clear. Conversely, if you do @emph{not}
4762want any whitespace characters at the end of your variable value, you
4763must remember not to put a random comment on the end of the line after
4764some whitespace, such as this:
4765
4766@example
4767dir := /foo/bar # directory to put the frobs in
4768@end example
4769
4770@noindent
4771Here the value of the variable @code{dir} is @w{@samp{/foo/bar }}
4772(with four trailing spaces), which was probably not the intention.
4773(Imagine something like @w{@samp{$(dir)/file}} with this definition!)
4774
4775@cindex conditional variable assignment
4776@cindex variables, conditional assignment
4777@cindex ?=
4778There is another assignment operator for variables, @samp{?=}. This
4779is called a conditional variable assignment operator, because it only
4780has an effect if the variable is not yet defined. This statement:
4781
4782@example
4783FOO ?= bar
4784@end example
4785
4786@noindent
4787is exactly equivalent to this
4788(@pxref{Origin Function, ,The @code{origin} Function}):
4789
4790@example
4791ifeq ($(origin FOO), undefined)
4792 FOO = bar
4793endif
4794@end example
4795
4796Note that a variable set to an empty value is still defined, so
4797@samp{?=} will not set that variable.
4798
4799@node Advanced, Values, Flavors, Using Variables
4800@section Advanced Features for Reference to Variables
4801@cindex reference to variables
4802
4803This section describes some advanced features you can use to reference
4804variables in more flexible ways.
4805
4806@menu
4807* Substitution Refs:: Referencing a variable with
4808 substitutions on the value.
4809* Computed Names:: Computing the name of the variable to refer to.
4810@end menu
4811
4812@node Substitution Refs, Computed Names, Advanced, Advanced
4813@subsection Substitution References
4814@cindex modified variable reference
4815@cindex substitution variable reference
4816@cindex variables, modified reference
4817@cindex variables, substitution reference
4818
4819@cindex variables, substituting suffix in
4820@cindex suffix, substituting in variables
4821A @dfn{substitution reference} substitutes the value of a variable with
4822alterations that you specify. It has the form
4823@samp{$(@var{var}:@var{a}=@var{b})} (or
4824@samp{$@{@var{var}:@var{a}=@var{b}@}}) and its meaning is to take the value
4825of the variable @var{var}, replace every @var{a} at the end of a word with
4826@var{b} in that value, and substitute the resulting string.
4827
4828When we say ``at the end of a word'', we mean that @var{a} must appear
4829either followed by whitespace or at the end of the value in order to be
4830replaced; other occurrences of @var{a} in the value are unaltered. For
4831example:@refill
4832
4833@example
4834foo := a.o b.o c.o
4835bar := $(foo:.o=.c)
4836@end example
4837
4838@noindent
4839sets @samp{bar} to @samp{a.c b.c c.c}. @xref{Setting, ,Setting Variables}.
4840
4841A substitution reference is actually an abbreviation for use of the
4842@code{patsubst} expansion function (@pxref{Text Functions, ,Functions for String Substitution and Analysis}). We provide
4843substitution references as well as @code{patsubst} for compatibility with
4844other implementations of @code{make}.
4845
4846@findex patsubst
4847Another type of substitution reference lets you use the full power of
4848the @code{patsubst} function. It has the same form
4849@samp{$(@var{var}:@var{a}=@var{b})} described above, except that now
4850@var{a} must contain a single @samp{%} character. This case is
4851equivalent to @samp{$(patsubst @var{a},@var{b},$(@var{var}))}.
4852@xref{Text Functions, ,Functions for String Substitution and Analysis},
4853for a description of the @code{patsubst} function.@refill
4854
4855@example
4856@group
4857@exdent For example:
4858
4859foo := a.o b.o c.o
4860bar := $(foo:%.o=%.c)
4861@end group
4862@end example
4863
4864@noindent
4865sets @samp{bar} to @samp{a.c b.c c.c}.
4866
4867@node Computed Names, , Substitution Refs, Advanced
4868@subsection Computed Variable Names
4869@cindex nested variable reference
4870@cindex computed variable name
4871@cindex variables, computed names
4872@cindex variables, nested references
4873@cindex variables, @samp{$} in name
4874@cindex @code{$}, in variable name
4875@cindex dollar sign (@code{$}), in variable name
4876
4877Computed variable names are a complicated concept needed only for
4878sophisticated makefile programming. For most purposes you need not
4879consider them, except to know that making a variable with a dollar sign
4880in its name might have strange results. However, if you are the type
4881that wants to understand everything, or you are actually interested in
4882what they do, read on.
4883
4884Variables may be referenced inside the name of a variable. This is
4885called a @dfn{computed variable name} or a @dfn{nested variable
4886reference}. For example,
4887
4888@example
4889x = y
4890y = z
4891a := $($(x))
4892@end example
4893
4894@noindent
4895defines @code{a} as @samp{z}: the @samp{$(x)} inside @samp{$($(x))} expands
4896to @samp{y}, so @samp{$($(x))} expands to @samp{$(y)} which in turn expands
4897to @samp{z}. Here the name of the variable to reference is not stated
4898explicitly; it is computed by expansion of @samp{$(x)}. The reference
4899@samp{$(x)} here is nested within the outer variable reference.
4900
4901The previous example shows two levels of nesting, but any number of levels
4902is possible. For example, here are three levels:
4903
4904@example
4905x = y
4906y = z
4907z = u
4908a := $($($(x)))
4909@end example
4910
4911@noindent
4912Here the innermost @samp{$(x)} expands to @samp{y}, so @samp{$($(x))}
4913expands to @samp{$(y)} which in turn expands to @samp{z}; now we have
4914@samp{$(z)}, which becomes @samp{u}.
4915
4916References to recursively-expanded variables within a variable name are
4917reexpanded in the usual fashion. For example:
4918
4919@example
4920x = $(y)
4921y = z
4922z = Hello
4923a := $($(x))
4924@end example
4925
4926@noindent
4927defines @code{a} as @samp{Hello}: @samp{$($(x))} becomes @samp{$($(y))}
4928which becomes @samp{$(z)} which becomes @samp{Hello}.
4929
4930Nested variable references can also contain modified references and
4931function invocations (@pxref{Functions, ,Functions for Transforming Text}),
4932just like any other reference.
4933For example, using the @code{subst} function
4934(@pxref{Text Functions, ,Functions for String Substitution and Analysis}):
4935
4936@example
4937@group
4938x = variable1
4939variable2 := Hello
4940y = $(subst 1,2,$(x))
4941z = y
4942a := $($($(z)))
4943@end group
4944@end example
4945
4946@noindent
4947eventually defines @code{a} as @samp{Hello}. It is doubtful that anyone
4948would ever want to write a nested reference as convoluted as this one, but
4949it works: @samp{$($($(z)))} expands to @samp{$($(y))} which becomes
4950@samp{$($(subst 1,2,$(x)))}. This gets the value @samp{variable1} from
4951@code{x} and changes it by substitution to @samp{variable2}, so that the
4952entire string becomes @samp{$(variable2)}, a simple variable reference
4953whose value is @samp{Hello}.@refill
4954
4955A computed variable name need not consist entirely of a single variable
4956reference. It can contain several variable references, as well as some
4957invariant text. For example,
4958
4959@example
4960@group
4961a_dirs := dira dirb
49621_dirs := dir1 dir2
4963@end group
4964
4965@group
4966a_files := filea fileb
49671_files := file1 file2
4968@end group
4969
4970@group
4971ifeq "$(use_a)" "yes"
4972a1 := a
4973else
4974a1 := 1
4975endif
4976@end group
4977
4978@group
4979ifeq "$(use_dirs)" "yes"
4980df := dirs
4981else
4982df := files
4983endif
4984
4985dirs := $($(a1)_$(df))
4986@end group
4987@end example
4988
4989@noindent
4990will give @code{dirs} the same value as @code{a_dirs}, @code{1_dirs},
4991@code{a_files} or @code{1_files} depending on the settings of @code{use_a}
4992and @code{use_dirs}.@refill
4993
4994Computed variable names can also be used in substitution references:
4995
4996@example
4997@group
4998a_objects := a.o b.o c.o
49991_objects := 1.o 2.o 3.o
5000
5001sources := $($(a1)_objects:.o=.c)
5002@end group
5003@end example
5004
5005@noindent
5006defines @code{sources} as either @samp{a.c b.c c.c} or @samp{1.c 2.c 3.c},
5007depending on the value of @code{a1}.
5008
5009The only restriction on this sort of use of nested variable references
5010is that they cannot specify part of the name of a function to be called.
5011This is because the test for a recognized function name is done before
5012the expansion of nested references. For example,
5013
5014@example
5015@group
5016ifdef do_sort
5017func := sort
5018else
5019func := strip
5020endif
5021@end group
5022
5023@group
5024bar := a d b g q c
5025@end group
5026
5027@group
5028foo := $($(func) $(bar))
5029@end group
5030@end example
5031
5032@noindent
5033attempts to give @samp{foo} the value of the variable @samp{sort a d b g
5034q c} or @samp{strip a d b g q c}, rather than giving @samp{a d b g q c}
5035as the argument to either the @code{sort} or the @code{strip} function.
5036This restriction could be removed in the future if that change is shown
5037to be a good idea.
5038
5039You can also use computed variable names in the left-hand side of a
5040variable assignment, or in a @code{define} directive, as in:
5041
5042@example
5043dir = foo
5044$(dir)_sources := $(wildcard $(dir)/*.c)
5045define $(dir)_print
5046lpr $($(dir)_sources)
5047endef
5048@end example
5049
5050@noindent
5051This example defines the variables @samp{dir}, @samp{foo_sources}, and
5052@samp{foo_print}.
5053
5054Note that @dfn{nested variable references} are quite different from
5055@dfn{recursively expanded variables}
5056(@pxref{Flavors, ,The Two Flavors of Variables}), though both are
5057used together in complex ways when doing makefile programming.@refill
5058
5059@node Values, Setting, Advanced, Using Variables
5060@section How Variables Get Their Values
5061@cindex variables, how they get their values
5062@cindex value, how a variable gets it
5063
5064Variables can get values in several different ways:
5065
5066@itemize @bullet
5067@item
5068You can specify an overriding value when you run @code{make}.
5069@xref{Overriding, ,Overriding Variables}.
5070
5071@item
5072You can specify a value in the makefile, either
5073with an assignment (@pxref{Setting, ,Setting Variables}) or with a
5074verbatim definition (@pxref{Defining, ,Defining Variables Verbatim}).@refill
5075
5076@item
5077Variables in the environment become @code{make} variables.
5078@xref{Environment, ,Variables from the Environment}.
5079
5080@item
5081Several @dfn{automatic} variables are given new values for each rule.
5082Each of these has a single conventional use.
5083@xref{Automatic Variables}.
5084
5085@item
5086Several variables have constant initial values.
5087@xref{Implicit Variables, ,Variables Used by Implicit Rules}.
5088@end itemize
5089
5090@node Setting, Appending, Values, Using Variables
5091@section Setting Variables
5092@cindex setting variables
5093@cindex variables, setting
5094@cindex =
5095@cindex :=
5096@cindex ?=
5097
5098To set a variable from the makefile, write a line starting with the
5099variable name followed by @samp{=} or @samp{:=}. Whatever follows the
5100@samp{=} or @samp{:=} on the line becomes the value. For example,
5101
5102@example
5103objects = main.o foo.o bar.o utils.o
5104@end example
5105
5106@noindent
5107defines a variable named @code{objects}. Whitespace around the variable
5108name and immediately after the @samp{=} is ignored.
5109
5110Variables defined with @samp{=} are @dfn{recursively expanded} variables.
5111Variables defined with @samp{:=} are @dfn{simply expanded} variables; these
5112definitions can contain variable references which will be expanded before
5113the definition is made. @xref{Flavors, ,The Two Flavors of Variables}.
5114
5115The variable name may contain function and variable references, which
5116are expanded when the line is read to find the actual variable name to use.
5117
5118There is no limit on the length of the value of a variable except the
5119amount of swapping space on the computer. When a variable definition is
5120long, it is a good idea to break it into several lines by inserting
5121backslash-newline at convenient places in the definition. This will not
5122affect the functioning of @code{make}, but it will make the makefile easier
5123to read.
5124
5125Most variable names are considered to have the empty string as a value if
5126you have never set them. Several variables have built-in initial values
5127that are not empty, but you can set them in the usual ways
5128(@pxref{Implicit Variables, ,Variables Used by Implicit Rules}).
5129Several special variables are set
5130automatically to a new value for each rule; these are called the
5131@dfn{automatic} variables (@pxref{Automatic Variables}).
5132
5133If you'd like a variable to be set to a value only if it's not already
5134set, then you can use the shorthand operator @samp{?=} instead of
5135@samp{=}. These two settings of the variable @samp{FOO} are identical
5136(@pxref{Origin Function, ,The @code{origin} Function}):
5137
5138@example
5139FOO ?= bar
5140@end example
5141
5142@noindent
5143and
5144
5145@example
5146ifeq ($(origin FOO), undefined)
5147FOO = bar
5148endif
5149@end example
5150
5151@node Appending, Override Directive, Setting, Using Variables
5152@section Appending More Text to Variables
5153@cindex +=
5154@cindex appending to variables
5155@cindex variables, appending to
5156
5157Often it is useful to add more text to the value of a variable already defined.
5158You do this with a line containing @samp{+=}, like this:
5159
5160@example
5161objects += another.o
5162@end example
5163
5164@noindent
5165This takes the value of the variable @code{objects}, and adds the text
5166@samp{another.o} to it (preceded by a single space). Thus:
5167
5168@example
5169objects = main.o foo.o bar.o utils.o
5170objects += another.o
5171@end example
5172
5173@noindent
5174sets @code{objects} to @samp{main.o foo.o bar.o utils.o another.o}.
5175
5176Using @samp{+=} is similar to:
5177
5178@example
5179objects = main.o foo.o bar.o utils.o
5180objects := $(objects) another.o
5181@end example
5182
5183@noindent
5184but differs in ways that become important when you use more complex values.
5185
5186When the variable in question has not been defined before, @samp{+=}
5187acts just like normal @samp{=}: it defines a recursively-expanded
5188variable. However, when there @emph{is} a previous definition, exactly
5189what @samp{+=} does depends on what flavor of variable you defined
5190originally. @xref{Flavors, ,The Two Flavors of Variables}, for an
5191explanation of the two flavors of variables.
5192
5193When you add to a variable's value with @samp{+=}, @code{make} acts
5194essentially as if you had included the extra text in the initial
5195definition of the variable. If you defined it first with @samp{:=},
5196making it a simply-expanded variable, @samp{+=} adds to that
5197simply-expanded definition, and expands the new text before appending it
5198to the old value just as @samp{:=} does
5199(@pxref{Setting, ,Setting Variables}, for a full explanation of @samp{:=}).
5200In fact,
5201
5202@example
5203variable := value
5204variable += more
5205@end example
5206
5207@noindent
5208is exactly equivalent to:
5209
5210@noindent
5211@example
5212variable := value
5213variable := $(variable) more
5214@end example
5215
5216On the other hand, when you use @samp{+=} with a variable that you defined
5217first to be recursively-expanded using plain @samp{=}, @code{make} does
5218something a bit different. Recall that when you define a
5219recursively-expanded variable, @code{make} does not expand the value you set
5220for variable and function references immediately. Instead it stores the text
5221verbatim, and saves these variable and function references to be expanded
5222later, when you refer to the new variable (@pxref{Flavors, ,The Two Flavors
5223of Variables}). When you use @samp{+=} on a recursively-expanded variable,
5224it is this unexpanded text to which @code{make} appends the new text you
5225specify.
5226
5227@example
5228@group
5229variable = value
5230variable += more
5231@end group
5232@end example
5233
5234@noindent
5235is roughly equivalent to:
5236
5237@example
5238@group
5239temp = value
5240variable = $(temp) more
5241@end group
5242@end example
5243
5244@noindent
5245except that of course it never defines a variable called @code{temp}.
5246The importance of this comes when the variable's old value contains
5247variable references. Take this common example:
5248
5249@example
5250CFLAGS = $(includes) -O
5251@dots{}
5252CFLAGS += -pg # enable profiling
5253@end example
5254
5255@noindent
5256The first line defines the @code{CFLAGS} variable with a reference to another
5257variable, @code{includes}. (@code{CFLAGS} is used by the rules for C
5258compilation; @pxref{Catalogue of Rules, ,Catalogue of Implicit Rules}.)
5259Using @samp{=} for the definition makes @code{CFLAGS} a recursively-expanded
5260variable, meaning @w{@samp{$(includes) -O}} is @emph{not} expanded when
5261@code{make} processes the definition of @code{CFLAGS}. Thus, @code{includes}
5262need not be defined yet for its value to take effect. It only has to be
5263defined before any reference to @code{CFLAGS}. If we tried to append to the
5264value of @code{CFLAGS} without using @samp{+=}, we might do it like this:
5265
5266@example
5267CFLAGS := $(CFLAGS) -pg # enable profiling
5268@end example
5269
5270@noindent
5271This is pretty close, but not quite what we want. Using @samp{:=}
5272redefines @code{CFLAGS} as a simply-expanded variable; this means
5273@code{make} expands the text @w{@samp{$(CFLAGS) -pg}} before setting the
5274variable. If @code{includes} is not yet defined, we get @w{@samp{ -O
5275-pg}}, and a later definition of @code{includes} will have no effect.
5276Conversely, by using @samp{+=} we set @code{CFLAGS} to the
5277@emph{unexpanded} value @w{@samp{$(includes) -O -pg}}. Thus we preserve
5278the reference to @code{includes}, so if that variable gets defined at
5279any later point, a reference like @samp{$(CFLAGS)} still uses its
5280value.
5281
5282@node Override Directive, Defining, Appending, Using Variables
5283@section The @code{override} Directive
5284@findex override
5285@cindex overriding with @code{override}
5286@cindex variables, overriding
5287
5288If a variable has been set with a command argument
5289(@pxref{Overriding, ,Overriding Variables}),
5290then ordinary assignments in the makefile are ignored. If you want to set
5291the variable in the makefile even though it was set with a command
5292argument, you can use an @code{override} directive, which is a line that
5293looks like this:@refill
5294
5295@example
5296override @var{variable} = @var{value}
5297@end example
5298
5299@noindent
5300or
5301
5302@example
5303override @var{variable} := @var{value}
5304@end example
5305
5306To append more text to a variable defined on the command line, use:
5307
5308@example
5309override @var{variable} += @var{more text}
5310@end example
5311
5312@noindent
5313@xref{Appending, ,Appending More Text to Variables}.
5314
5315The @code{override} directive was not invented for escalation in the war
5316between makefiles and command arguments. It was invented so you can alter
5317and add to values that the user specifies with command arguments.
5318
5319For example, suppose you always want the @samp{-g} switch when you run the
5320C compiler, but you would like to allow the user to specify the other
5321switches with a command argument just as usual. You could use this
5322@code{override} directive:
5323
5324@example
5325override CFLAGS += -g
5326@end example
5327
5328You can also use @code{override} directives with @code{define} directives.
5329This is done as you might expect:
5330
5331@example
5332override define foo
5333bar
5334endef
5335@end example
5336
5337@noindent
5338@iftex
5339See the next section for information about @code{define}.
5340@end iftex
5341@ifnottex
5342@xref{Defining, ,Defining Variables Verbatim}.
5343@end ifnottex
5344
5345@node Defining, Environment, Override Directive, Using Variables
5346@section Defining Variables Verbatim
5347@findex define
5348@findex endef
5349@cindex verbatim variable definition
5350@cindex defining variables verbatim
5351@cindex variables, defining verbatim
5352
5353Another way to set the value of a variable is to use the @code{define}
5354directive. This directive has an unusual syntax which allows newline
5355characters to be included in the value, which is convenient for defining
5356both canned sequences of commands
5357(@pxref{Sequences, ,Defining Canned Command Sequences}), and also
5358sections of makefile syntax to use with @code{eval} (@pxref{Eval Function}).
5359
5360The @code{define} directive is followed on the same line by the name of the
5361variable and nothing more. The value to give the variable appears on the
5362following lines. The end of the value is marked by a line containing just
5363the word @code{endef}. Aside from this difference in syntax, @code{define}
5364works just like @samp{=}: it creates a recursively-expanded variable
5365(@pxref{Flavors, ,The Two Flavors of Variables}).
5366The variable name may contain function and variable references, which
5367are expanded when the directive is read to find the actual variable name
5368to use.
5369
5370You may nest @code{define} directives: @code{make} will keep track of
5371nested directives and report an error if they are not all properly
5372closed with @code{endef}. Note that lines beginning with tab
5373characters are considered part of a command script, so any
5374@code{define} or @code{endef} strings appearing on such a line will
5375not be considered @code{make} operators.
5376
5377@example
5378define two-lines
5379echo foo
5380echo $(bar)
5381endef
5382@end example
5383
5384The value in an ordinary assignment cannot contain a newline; but the
5385newlines that separate the lines of the value in a @code{define} become
5386part of the variable's value (except for the final newline which precedes
5387the @code{endef} and is not considered part of the value).@refill
5388
5389@need 800
5390When used in a command script, the previous example is functionally
5391equivalent to this:
5392
5393@example
5394two-lines = echo foo; echo $(bar)
5395@end example
5396
5397@noindent
5398since two commands separated by semicolon behave much like two separate
5399shell commands. However, note that using two separate lines means
5400@code{make} will invoke the shell twice, running an independent subshell
5401for each line. @xref{Execution, ,Command Execution}.
5402
5403If you want variable definitions made with @code{define} to take
5404precedence over command-line variable definitions, you can use the
5405@code{override} directive together with @code{define}:
5406
5407@example
5408override define two-lines
5409foo
5410$(bar)
5411endef
5412@end example
5413
5414@noindent
5415@xref{Override Directive, ,The @code{override} Directive}.
5416
5417@node Environment, Target-specific, Defining, Using Variables
5418@section Variables from the Environment
5419
5420@cindex variables, environment
5421@cindex environment
5422Variables in @code{make} can come from the environment in which
5423@code{make} is run. Every environment variable that @code{make} sees
5424when it starts up is transformed into a @code{make} variable with the
5425same name and value. However, an explicit assignment in the makefile,
5426or with a command argument, overrides the environment. (If the
5427@samp{-e} flag is specified, then values from the environment override
5428assignments in the makefile. @xref{Options Summary, ,Summary of
5429Options}. But this is not recommended practice.)
5430
5431Thus, by setting the variable @code{CFLAGS} in your environment, you can
5432cause all C compilations in most makefiles to use the compiler switches you
5433prefer. This is safe for variables with standard or conventional meanings
5434because you know that no makefile will use them for other things. (Note
5435this is not totally reliable; some makefiles set @code{CFLAGS} explicitly
5436and therefore are not affected by the value in the environment.)
5437
5438When @code{make} runs a command script, variables defined in the
5439makefile are placed into the environment of that command. This allows
5440you to pass values to sub-@code{make} invocations. (@pxref{Recursion,
5441,Recursive Use of @code{make}}). By default, only variables that came
5442from the environment or the command line are passed to recursive
5443invocations. You can use the @code{export} directive to pass other
5444variables. @xref{Variables/Recursion, , Communicating Variables to a
5445Sub-@code{make}}, for full details.
5446
5447Other use of variables from the environment is not recommended. It is not
5448wise for makefiles to depend for their functioning on environment variables
5449set up outside their control, since this would cause different users to get
5450different results from the same makefile. This is against the whole
5451purpose of most makefiles.
5452
5453@cindex SHELL, import from environment
5454Such problems would be especially likely with the variable @code{SHELL},
5455which is normally present in the environment to specify the user's choice
5456of interactive shell. It would be very undesirable for this choice to
5457affect @code{make}. So @code{make} ignores the environment value of
5458@code{SHELL} (except on MS-DOS and MS-Windows, where @code{SHELL} is
5459usually not set. @xref{Execution, ,Special handling of SHELL on
5460MS-DOS}.)@refill
5461
5462@cindex SHELL, export to environment
5463The @code{SHELL} variable is special in another way: just as the value
5464of the @code{make} variable @code{SHELL} is not taken from the
5465environment, so also it is not placed into the environment of commands
5466that @code{make} invokes. Instead, the value of @code{SHELL} from the
5467invoking environment is provided to the command. You can use
5468@code{export SHELL} to force the value of the @code{make} variable
5469@code{SHELL} to be placed in the environment of commands.
5470
5471@node Target-specific, Pattern-specific, Environment, Using Variables
5472@section Target-specific Variable Values
5473@cindex target-specific variables
5474@cindex variables, target-specific
5475
5476Variable values in @code{make} are usually global; that is, they are the
5477same regardless of where they are evaluated (unless they're reset, of
5478course). One exception to that is automatic variables
5479(@pxref{Automatic Variables}).
5480
5481The other exception is @dfn{target-specific variable values}. This
5482feature allows you to define different values for the same variable,
5483based on the target that @code{make} is currently building. As with
5484automatic variables, these values are only available within the context
5485of a target's command script (and in other target-specific assignments).
5486
5487Set a target-specific variable value like this:
5488
5489@example
5490@var{target} @dots{} : @var{variable-assignment}
5491@end example
5492
5493@noindent
5494or like this:
5495
5496@example
5497@var{target} @dots{} : override @var{variable-assignment}
5498@end example
5499
5500@noindent
5501or like this:
5502
5503@example
5504@var{target} @dots{} : export @var{variable-assignment}
5505@end example
5506
5507Multiple @var{target} values create a target-specific variable value for
5508each member of the target list individually.
5509
5510The @var{variable-assignment} can be any valid form of assignment;
5511recursive (@samp{=}), static (@samp{:=}), appending (@samp{+=}), or
5512conditional (@samp{?=}). All variables that appear within the
5513@var{variable-assignment} are evaluated within the context of the
5514target: thus, any previously-defined target-specific variable values
5515will be in effect. Note that this variable is actually distinct from
5516any ``global'' value: the two variables do not have to have the same
5517flavor (recursive vs. static).
5518
5519Target-specific variables have the same priority as any other makefile
5520variable. Variables provided on the command-line (and in the
5521environment if the @samp{-e} option is in force) will take precedence.
5522Specifying the @code{override} directive will allow the target-specific
5523variable value to be preferred.
5524
5525There is one more special feature of target-specific variables: when
5526you define a target-specific variable that variable value is also in
5527effect for all prerequisites of this target, and all their
5528prerequisites, etc. (unless those prerequisites override that variable
5529with their own target-specific variable value). So, for example, a
5530statement like this:
5531
5532@example
5533prog : CFLAGS = -g
5534prog : prog.o foo.o bar.o
5535@end example
5536
5537@noindent
5538will set @code{CFLAGS} to @samp{-g} in the command script for
5539@file{prog}, but it will also set @code{CFLAGS} to @samp{-g} in the
5540command scripts that create @file{prog.o}, @file{foo.o}, and
5541@file{bar.o}, and any command scripts which create their
5542prerequisites.
5543
5544Be aware that a given prerequisite will only be built once per
5545invocation of make, at most. If the same file is a prerequisite of
5546multiple targets, and each of those targets has a different value for
5547the same target-specific variable, then the first target to be built
5548will cause that prerequisite to be built and the prerequisite will
5549inherit the target-specific value from the first target. It will
5550ignore the target-specific values from any other targets.
5551
5552@node Pattern-specific, , Target-specific, Using Variables
5553@section Pattern-specific Variable Values
5554@cindex pattern-specific variables
5555@cindex variables, pattern-specific
5556
5557In addition to target-specific variable values
5558(@pxref{Target-specific, ,Target-specific Variable Values}), GNU
5559@code{make} supports pattern-specific variable values. In this form,
5560the variable is defined for any target that matches the pattern
5561specified. If a target matches more than one pattern, all the
5562matching pattern-specific variables are interpreted in the order in
5563which they were defined in the makefile, and collected together into
5564one set. Variables defined in this way are searched after any
5565target-specific variables defined explicitly for that target, and
5566before target-specific variables defined for the parent target.
5567
5568Set a pattern-specific variable value like this:
5569
5570@example
5571@var{pattern} @dots{} : @var{variable-assignment}
5572@end example
5573
5574@noindent
5575or like this:
5576
5577@example
5578@var{pattern} @dots{} : override @var{variable-assignment}
5579@end example
5580
5581@noindent
5582where @var{pattern} is a %-pattern. As with target-specific variable
5583values, multiple @var{pattern} values create a pattern-specific variable
5584value for each pattern individually. The @var{variable-assignment} can
5585be any valid form of assignment. Any command-line variable setting will
5586take precedence, unless @code{override} is specified.
5587
5588For example:
5589
5590@example
5591%.o : CFLAGS = -O
5592@end example
5593
5594@noindent
5595will assign @code{CFLAGS} the value of @samp{-O} for all targets
5596matching the pattern @code{%.o}.
5597
5598@node Conditionals, Functions, Using Variables, Top
5599@chapter Conditional Parts of Makefiles
5600
5601@cindex conditionals
5602A @dfn{conditional} causes part of a makefile to be obeyed or ignored
5603depending on the values of variables. Conditionals can compare the
5604value of one variable to another, or the value of a variable to
5605a constant string. Conditionals control what @code{make} actually
5606``sees'' in the makefile, so they @emph{cannot} be used to control shell
5607commands at the time of execution.@refill
5608
5609@menu
5610* Conditional Example:: Example of a conditional
5611* Conditional Syntax:: The syntax of conditionals.
5612* Testing Flags:: Conditionals that test flags.
5613@end menu
5614
5615@node Conditional Example, Conditional Syntax, Conditionals, Conditionals
5616@section Example of a Conditional
5617
5618The following example of a conditional tells @code{make} to use one set
5619of libraries if the @code{CC} variable is @samp{gcc}, and a different
5620set of libraries otherwise. It works by controlling which of two
5621command lines will be used as the command for a rule. The result is
5622that @samp{CC=gcc} as an argument to @code{make} changes not only which
5623compiler is used but also which libraries are linked.
5624
5625@example
5626libs_for_gcc = -lgnu
5627normal_libs =
5628
5629foo: $(objects)
5630ifeq ($(CC),gcc)
5631 $(CC) -o foo $(objects) $(libs_for_gcc)
5632else
5633 $(CC) -o foo $(objects) $(normal_libs)
5634endif
5635@end example
5636
5637This conditional uses three directives: one @code{ifeq}, one @code{else}
5638and one @code{endif}.
5639
5640The @code{ifeq} directive begins the conditional, and specifies the
5641condition. It contains two arguments, separated by a comma and surrounded
5642by parentheses. Variable substitution is performed on both arguments and
5643then they are compared. The lines of the makefile following the
5644@code{ifeq} are obeyed if the two arguments match; otherwise they are
5645ignored.
5646
5647The @code{else} directive causes the following lines to be obeyed if the
5648previous conditional failed. In the example above, this means that the
5649second alternative linking command is used whenever the first alternative
5650is not used. It is optional to have an @code{else} in a conditional.
5651
5652The @code{endif} directive ends the conditional. Every conditional must
5653end with an @code{endif}. Unconditional makefile text follows.
5654
5655As this example illustrates, conditionals work at the textual level:
5656the lines of the conditional are treated as part of the makefile, or
5657ignored, according to the condition. This is why the larger syntactic
5658units of the makefile, such as rules, may cross the beginning or the
5659end of the conditional.
5660
5661When the variable @code{CC} has the value @samp{gcc}, the above example has
5662this effect:
5663
5664@example
5665foo: $(objects)
5666 $(CC) -o foo $(objects) $(libs_for_gcc)
5667@end example
5668
5669@noindent
5670When the variable @code{CC} has any other value, the effect is this:
5671
5672@example
5673foo: $(objects)
5674 $(CC) -o foo $(objects) $(normal_libs)
5675@end example
5676
5677Equivalent results can be obtained in another way by conditionalizing a
5678variable assignment and then using the variable unconditionally:
5679
5680@example
5681libs_for_gcc = -lgnu
5682normal_libs =
5683
5684ifeq ($(CC),gcc)
5685 libs=$(libs_for_gcc)
5686else
5687 libs=$(normal_libs)
5688endif
5689
5690foo: $(objects)
5691 $(CC) -o foo $(objects) $(libs)
5692@end example
5693
5694@node Conditional Syntax, Testing Flags, Conditional Example, Conditionals
5695@section Syntax of Conditionals
5696@findex ifdef
5697@findex ifeq
5698@findex ifndef
5699@findex ifneq
5700@findex else
5701@findex endif
5702
5703The syntax of a simple conditional with no @code{else} is as follows:
5704
5705@example
5706@var{conditional-directive}
5707@var{text-if-true}
5708endif
5709@end example
5710
5711@noindent
5712The @var{text-if-true} may be any lines of text, to be considered as part
5713of the makefile if the condition is true. If the condition is false, no
5714text is used instead.
5715
5716The syntax of a complex conditional is as follows:
5717
5718@example
5719@var{conditional-directive}
5720@var{text-if-true}
5721else
5722@var{text-if-false}
5723endif
5724@end example
5725
5726or:
5727
5728@example
5729@var{conditional-directive}
5730@var{text-if-one-is-true}
5731else @var{conditional-directive}
5732@var{text-if-true}
5733else
5734@var{text-if-false}
5735endif
5736@end example
5737
5738@noindent
5739There can be as many ``@code{else} @var{conditional-directive}''
5740clauses as necessary. Once a given condition is true,
5741@var{text-if-true} is used and no other clause is used; if no
5742condition is true then @var{text-if-false} is used. The
5743@var{text-if-true} and @var{text-if-false} can be any number of lines
5744of text.
5745
5746The syntax of the @var{conditional-directive} is the same whether the
5747conditional is simple or complex; after an @code{else} or not. There
5748are four different directives that test different conditions. Here is
5749a table of them:
5750
5751@table @code
5752@item ifeq (@var{arg1}, @var{arg2})
5753@itemx ifeq '@var{arg1}' '@var{arg2}'
5754@itemx ifeq "@var{arg1}" "@var{arg2}"
5755@itemx ifeq "@var{arg1}" '@var{arg2}'
5756@itemx ifeq '@var{arg1}' "@var{arg2}"
5757Expand all variable references in @var{arg1} and @var{arg2} and
5758compare them. If they are identical, the @var{text-if-true} is
5759effective; otherwise, the @var{text-if-false}, if any, is effective.
5760
5761Often you want to test if a variable has a non-empty value. When the
5762value results from complex expansions of variables and functions,
5763expansions you would consider empty may actually contain whitespace
5764characters and thus are not seen as empty. However, you can use the
5765@code{strip} function (@pxref{Text Functions}) to avoid interpreting
5766whitespace as a non-empty value. For example:
5767
5768@example
5769@group
5770ifeq ($(strip $(foo)),)
5771@var{text-if-empty}
5772endif
5773@end group
5774@end example
5775
5776@noindent
5777will evaluate @var{text-if-empty} even if the expansion of
5778@code{$(foo)} contains whitespace characters.
5779
5780@item ifneq (@var{arg1}, @var{arg2})
5781@itemx ifneq '@var{arg1}' '@var{arg2}'
5782@itemx ifneq "@var{arg1}" "@var{arg2}"
5783@itemx ifneq "@var{arg1}" '@var{arg2}'
5784@itemx ifneq '@var{arg1}' "@var{arg2}"
5785Expand all variable references in @var{arg1} and @var{arg2} and
5786compare them. If they are different, the @var{text-if-true} is
5787effective; otherwise, the @var{text-if-false}, if any, is effective.
5788
5789@item ifdef @var{variable-name}
5790The @code{ifdef} form takes the @emph{name} of a variable as its
5791argument, not a reference to a variable. The value of that variable
5792has a non-empty value, the @var{text-if-true} is effective; otherwise,
5793the @var{text-if-false}, if any, is effective. Variables that have
5794never been defined have an empty value. The text @var{variable-name}
5795is expanded, so it could be a variable or function that expands
5796to the name of a variable. For example:
5797
5798@example
5799bar = true
5800foo = bar
5801ifdef $(foo)
5802frobozz = yes
5803endif
5804@end example
5805
5806The variable reference @code{$(foo)} is expanded, yielding @code{bar},
5807which is considered to be the name of a variable. The variable
5808@code{bar} is not expanded, but its value is examined to determine if
5809it is non-empty.
5810
5811Note that @code{ifdef} only tests whether a variable has a value. It
5812does not expand the variable to see if that value is nonempty.
5813Consequently, tests using @code{ifdef} return true for all definitions
5814except those like @code{foo =}. To test for an empty value, use
5815@w{@code{ifeq ($(foo),)}}. For example,
5816
5817@example
5818bar =
5819foo = $(bar)
5820ifdef foo
5821frobozz = yes
5822else
5823frobozz = no
5824endif
5825@end example
5826
5827@noindent
5828sets @samp{frobozz} to @samp{yes}, while:
5829
5830@example
5831foo =
5832ifdef foo
5833frobozz = yes
5834else
5835frobozz = no
5836endif
5837@end example
5838
5839@noindent
5840sets @samp{frobozz} to @samp{no}.
5841
5842@item ifndef @var{variable-name}
5843If the variable @var{variable-name} has an empty value, the
5844@var{text-if-true} is effective; otherwise, the @var{text-if-false},
5845if any, is effective. The rules for expansion and testing of
5846@var{variable-name} are identical to the @code{ifdef} directive.
5847@end table
5848
5849Extra spaces are allowed and ignored at the beginning of the conditional
5850directive line, but a tab is not allowed. (If the line begins with a tab,
5851it will be considered a command for a rule.) Aside from this, extra spaces
5852or tabs may be inserted with no effect anywhere except within the directive
5853name or within an argument. A comment starting with @samp{#} may appear at
5854the end of the line.
5855
5856The other two directives that play a part in a conditional are @code{else}
5857and @code{endif}. Each of these directives is written as one word, with no
5858arguments. Extra spaces are allowed and ignored at the beginning of the
5859line, and spaces or tabs at the end. A comment starting with @samp{#} may
5860appear at the end of the line.
5861
5862Conditionals affect which lines of the makefile @code{make} uses. If
5863the condition is true, @code{make} reads the lines of the
5864@var{text-if-true} as part of the makefile; if the condition is false,
5865@code{make} ignores those lines completely. It follows that syntactic
5866units of the makefile, such as rules, may safely be split across the
5867beginning or the end of the conditional.@refill
5868
5869@code{make} evaluates conditionals when it reads a makefile.
5870Consequently, you cannot use automatic variables in the tests of
5871conditionals because they are not defined until commands are run
5872(@pxref{Automatic Variables}).
5873
5874To prevent intolerable confusion, it is not permitted to start a
5875conditional in one makefile and end it in another. However, you may
5876write an @code{include} directive within a conditional, provided you do
5877not attempt to terminate the conditional inside the included file.
5878
5879@node Testing Flags, , Conditional Syntax, Conditionals
5880@section Conditionals that Test Flags
5881
5882You can write a conditional that tests @code{make} command flags such as
5883@samp{-t} by using the variable @code{MAKEFLAGS} together with the
5884@code{findstring} function
5885(@pxref{Text Functions, , Functions for String Substitution and Analysis}).
5886This is useful when @code{touch} is not enough to make a file appear up
5887to date.
5888
5889The @code{findstring} function determines whether one string appears as a
5890substring of another. If you want to test for the @samp{-t} flag,
5891use @samp{t} as the first string and the value of @code{MAKEFLAGS} as
5892the other.
5893
5894For example, here is how to arrange to use @samp{ranlib -t} to finish
5895marking an archive file up to date:
5896
5897@example
5898archive.a: @dots{}
5899ifneq (,$(findstring t,$(MAKEFLAGS)))
5900 +touch archive.a
5901 +ranlib -t archive.a
5902else
5903 ranlib archive.a
5904endif
5905@end example
5906
5907@noindent
5908The @samp{+} prefix marks those command lines as ``recursive'' so
5909that they will be executed despite use of the @samp{-t} flag.
5910@xref{Recursion, ,Recursive Use of @code{make}}.
5911
5912@node Functions, Running, Conditionals, Top
5913@chapter Functions for Transforming Text
5914@cindex functions
5915
5916@dfn{Functions} allow you to do text processing in the makefile to compute
5917the files to operate on or the commands to use. You use a function in a
5918@dfn{function call}, where you give the name of the function and some text
5919(the @dfn{arguments}) for the function to operate on. The result of the
5920function's processing is substituted into the makefile at the point of the
5921call, just as a variable might be substituted.
5922
5923@menu
5924* Syntax of Functions:: How to write a function call.
5925* Text Functions:: General-purpose text manipulation functions.
5926* File Name Functions:: Functions for manipulating file names.
5927* Foreach Function:: Repeat some text with controlled variation.
5928* If Function:: Conditionally expand a value.
5929* Call Function:: Expand a user-defined function.
5930* Value Function:: Return the un-expanded value of a variable.
5931* Eval Function:: Evaluate the arguments as makefile syntax.
5932* Origin Function:: Find where a variable got its value.
5933* Shell Function:: Substitute the output of a shell command.
5934* Make Control Functions:: Functions that control how make runs.
5935@end menu
5936
5937@node Syntax of Functions, Text Functions, Functions, Functions
5938@section Function Call Syntax
5939@cindex @code{$}, in function call
5940@cindex dollar sign (@code{$}), in function call
5941@cindex arguments of functions
5942@cindex functions, syntax of
5943
5944A function call resembles a variable reference. It looks like this:
5945
5946@example
5947$(@var{function} @var{arguments})
5948@end example
5949
5950@noindent
5951or like this:
5952
5953@example
5954$@{@var{function} @var{arguments}@}
5955@end example
5956
5957Here @var{function} is a function name; one of a short list of names
5958that are part of @code{make}. You can also essentially create your own
5959functions by using the @code{call} builtin function.
5960
5961The @var{arguments} are the arguments of the function. They are
5962separated from the function name by one or more spaces or tabs, and if
5963there is more than one argument, then they are separated by commas.
5964Such whitespace and commas are not part of an argument's value. The
5965delimiters which you use to surround the function call, whether
5966parentheses or braces, can appear in an argument only in matching pairs;
5967the other kind of delimiters may appear singly. If the arguments
5968themselves contain other function calls or variable references, it is
5969wisest to use the same kind of delimiters for all the references; write
5970@w{@samp{$(subst a,b,$(x))}}, not @w{@samp{$(subst a,b,$@{x@})}}. This
5971is because it is clearer, and because only one type of delimiter is
5972matched to find the end of the reference.
5973
5974The text written for each argument is processed by substitution of
5975variables and function calls to produce the argument value, which
5976is the text on which the function acts. The substitution is done in the
5977order in which the arguments appear.
5978
5979Commas and unmatched parentheses or braces cannot appear in the text of an
5980argument as written; leading spaces cannot appear in the text of the first
5981argument as written. These characters can be put into the argument value
5982by variable substitution. First define variables @code{comma} and
5983@code{space} whose values are isolated comma and space characters, then
5984substitute these variables where such characters are wanted, like this:
5985
5986@example
5987@group
5988comma:= ,
5989empty:=
5990space:= $(empty) $(empty)
5991foo:= a b c
5992bar:= $(subst $(space),$(comma),$(foo))
5993# @r{bar is now `a,b,c'.}
5994@end group
5995@end example
5996
5997@noindent
5998Here the @code{subst} function replaces each space with a comma, through
5999the value of @code{foo}, and substitutes the result.
6000
6001@node Text Functions, File Name Functions, Syntax of Functions, Functions
6002@section Functions for String Substitution and Analysis
6003@cindex functions, for text
6004
6005Here are some functions that operate on strings:
6006
6007@table @code
6008@item $(subst @var{from},@var{to},@var{text})
6009@findex subst
6010Performs a textual replacement on the text @var{text}: each occurrence
6011of @var{from} is replaced by @var{to}. The result is substituted for
6012the function call. For example,
6013
6014@example
6015$(subst ee,EE,feet on the street)
6016@end example
6017
6018substitutes the string @samp{fEEt on the strEEt}.
6019
6020@item $(patsubst @var{pattern},@var{replacement},@var{text})
6021@findex patsubst
6022Finds whitespace-separated words in @var{text} that match
6023@var{pattern} and replaces them with @var{replacement}. Here
6024@var{pattern} may contain a @samp{%} which acts as a wildcard,
6025matching any number of any characters within a word. If
6026@var{replacement} also contains a @samp{%}, the @samp{%} is replaced
6027by the text that matched the @samp{%} in @var{pattern}. Only the first
6028@samp{%} in the @var{pattern} and @var{replacement} is treated this
6029way; any subsequent @samp{%} is unchanged.@refill
6030
6031@cindex @code{%}, quoting in @code{patsubst}
6032@cindex @code{%}, quoting with @code{\} (backslash)
6033@cindex @code{\} (backslash), to quote @code{%}
6034@cindex backslash (@code{\}), to quote @code{%}
6035@cindex quoting @code{%}, in @code{patsubst}
6036@samp{%} characters in @code{patsubst} function invocations can be
6037quoted with preceding backslashes (@samp{\}). Backslashes that would
6038otherwise quote @samp{%} characters can be quoted with more backslashes.
6039Backslashes that quote @samp{%} characters or other backslashes are
6040removed from the pattern before it is compared file names or has a stem
6041substituted into it. Backslashes that are not in danger of quoting
6042@samp{%} characters go unmolested. For example, the pattern
6043@file{the\%weird\\%pattern\\} has @samp{the%weird\} preceding the
6044operative @samp{%} character, and @samp{pattern\\} following it. The
6045final two backslashes are left alone because they cannot affect any
6046@samp{%} character.@refill
6047
6048Whitespace between words is folded into single space characters;
6049leading and trailing whitespace is discarded.
6050
6051For example,
6052
6053@example
6054$(patsubst %.c,%.o,x.c.c bar.c)
6055@end example
6056
6057@noindent
6058produces the value @samp{x.c.o bar.o}.
6059
6060Substitution references (@pxref{Substitution Refs, ,Substitution
6061References}) are a simpler way to get the effect of the @code{patsubst}
6062function:
6063
6064@example
6065$(@var{var}:@var{pattern}=@var{replacement})
6066@end example
6067
6068@noindent
6069is equivalent to
6070
6071@example
6072$(patsubst @var{pattern},@var{replacement},$(@var{var}))
6073@end example
6074
6075The second shorthand simplifies one of the most common uses of
6076@code{patsubst}: replacing the suffix at the end of file names.
6077
6078@example
6079$(@var{var}:@var{suffix}=@var{replacement})
6080@end example
6081
6082@noindent
6083is equivalent to
6084
6085@example
6086$(patsubst %@var{suffix},%@var{replacement},$(@var{var}))
6087@end example
6088
6089@noindent
6090For example, you might have a list of object files:
6091
6092@example
6093objects = foo.o bar.o baz.o
6094@end example
6095
6096@noindent
6097To get the list of corresponding source files, you could simply write:
6098
6099@example
6100$(objects:.o=.c)
6101@end example
6102
6103@noindent
6104instead of using the general form:
6105
6106@example
6107$(patsubst %.o,%.c,$(objects))
6108@end example
6109
6110@item $(strip @var{string})
6111@cindex stripping whitespace
6112@cindex whitespace, stripping
6113@cindex spaces, stripping
6114@findex strip
6115Removes leading and trailing whitespace from @var{string} and replaces
6116each internal sequence of one or more whitespace characters with a
6117single space. Thus, @samp{$(strip a b c )} results in @w{@samp{a b c}}.
6118
6119The function @code{strip} can be very useful when used in conjunction
6120with conditionals. When comparing something with the empty string
6121@samp{} using @code{ifeq} or @code{ifneq}, you usually want a string of
6122just whitespace to match the empty string (@pxref{Conditionals}).
6123
6124Thus, the following may fail to have the desired results:
6125
6126@example
6127.PHONY: all
6128ifneq "$(needs_made)" ""
6129all: $(needs_made)
6130else
6131all:;@@echo 'Nothing to make!'
6132endif
6133@end example
6134
6135@noindent
6136Replacing the variable reference @w{@samp{$(needs_made)}} with the
6137function call @w{@samp{$(strip $(needs_made))}} in the @code{ifneq}
6138directive would make it more robust.@refill
6139
6140@item $(findstring @var{find},@var{in})
6141@findex findstring
6142@cindex searching for strings
6143@cindex finding strings
6144@cindex strings, searching for
6145Searches @var{in} for an occurrence of @var{find}. If it occurs, the
6146value is @var{find}; otherwise, the value is empty. You can use this
6147function in a conditional to test for the presence of a specific
6148substring in a given string. Thus, the two examples,
6149
6150@example
6151$(findstring a,a b c)
6152$(findstring a,b c)
6153@end example
6154
6155@noindent
6156produce the values @samp{a} and @samp{} (the empty string),
6157respectively. @xref{Testing Flags}, for a practical application of
6158@code{findstring}.@refill
6159
6160@need 750
6161@findex filter
6162@cindex filtering words
6163@cindex words, filtering
6164@item $(filter @var{pattern}@dots{},@var{text})
6165Returns all whitespace-separated words in @var{text} that @emph{do} match
6166any of the @var{pattern} words, removing any words that @emph{do not}
6167match. The patterns are written using @samp{%}, just like the patterns
6168used in the @code{patsubst} function above.@refill
6169
6170The @code{filter} function can be used to separate out different types
6171of strings (such as file names) in a variable. For example:
6172
6173@example
6174sources := foo.c bar.c baz.s ugh.h
6175foo: $(sources)
6176 cc $(filter %.c %.s,$(sources)) -o foo
6177@end example
6178
6179@noindent
6180says that @file{foo} depends of @file{foo.c}, @file{bar.c},
6181@file{baz.s} and @file{ugh.h} but only @file{foo.c}, @file{bar.c} and
6182@file{baz.s} should be specified in the command to the
6183compiler.@refill
6184
6185@item $(filter-out @var{pattern}@dots{},@var{text})
6186@findex filter-out
6187@cindex filtering out words
6188@cindex words, filtering out
6189Returns all whitespace-separated words in @var{text} that @emph{do not}
6190match any of the @var{pattern} words, removing the words that @emph{do}
6191match one or more. This is the exact opposite of the @code{filter}
6192function.@refill
6193
6194For example, given:
6195
6196@example
6197@group
6198objects=main1.o foo.o main2.o bar.o
6199mains=main1.o main2.o
6200@end group
6201@end example
6202
6203@noindent
6204the following generates a list which contains all the object files not
6205in @samp{mains}:
6206
6207@example
6208$(filter-out $(mains),$(objects))
6209@end example
6210
6211@need 1500
6212@findex sort
6213@cindex sorting words
6214@item $(sort @var{list})
6215Sorts the words of @var{list} in lexical order, removing duplicate
6216words. The output is a list of words separated by single spaces.
6217Thus,
6218
6219@example
6220$(sort foo bar lose)
6221@end example
6222
6223@noindent
6224returns the value @samp{bar foo lose}.
6225
6226@cindex removing duplicate words
6227@cindex duplicate words, removing
6228@cindex words, removing duplicates
6229Incidentally, since @code{sort} removes duplicate words, you can use
6230it for this purpose even if you don't care about the sort order.
6231
6232@item $(word @var{n},@var{text})
6233@findex word
6234@cindex word, selecting a
6235@cindex selecting a word
6236Returns the @var{n}th word of @var{text}. The legitimate values of
6237@var{n} start from 1. If @var{n} is bigger than the number of words
6238in @var{text}, the value is empty. For example,
6239
6240@example
6241$(word 2, foo bar baz)
6242@end example
6243
6244@noindent
6245returns @samp{bar}.
6246
6247@item $(wordlist @var{s},@var{e},@var{text})
6248@findex wordlist
6249@cindex words, selecting lists of
6250@cindex selecting word lists
6251Returns the list of words in @var{text} starting with word @var{s} and
6252ending with word @var{e} (inclusive). The legitimate values of @var{s}
6253start from 1; @var{e} may start from 0. If @var{s} is bigger than the
6254number of words in @var{text}, the value is empty. If @var{e} is
6255bigger than the number of words in @var{text}, words up to the end of
6256@var{text} are returned. If @var{s} is greater than @var{e}, nothing
6257is returned. For example,
6258
6259@example
6260$(wordlist 2, 3, foo bar baz)
6261@end example
6262
6263@noindent
6264returns @samp{bar baz}.
6265
6266@c Following item phrased to prevent overfull hbox. --RJC 17 Jul 92
6267@item $(words @var{text})
6268@findex words
6269@cindex words, finding number
6270Returns the number of words in @var{text}.
6271Thus, the last word of @var{text} is
6272@w{@code{$(word $(words @var{text}),@var{text})}}.@refill
6273
6274@item $(firstword @var{names}@dots{})
6275@findex firstword
6276@cindex words, extracting first
6277The argument @var{names} is regarded as a series of names, separated
6278by whitespace. The value is the first name in the series. The rest
6279of the names are ignored.
6280
6281For example,
6282
6283@example
6284$(firstword foo bar)
6285@end example
6286
6287@noindent
6288produces the result @samp{foo}. Although @code{$(firstword
6289@var{text})} is the same as @code{$(word 1,@var{text})}, the
6290@code{firstword} function is retained for its simplicity.@refill
6291
6292
6293@item $(lastword @var{names}@dots{})
6294@findex lastword
6295@cindex words, extracting last
6296The argument @var{names} is regarded as a series of names, separated
6297by whitespace. The value is the last name in the series.
6298
6299For example,
6300
6301@example
6302$(lastword foo bar)
6303@end example
6304
6305@noindent
6306produces the result @samp{bar}. Although @code{$(lastword
6307@var{text})} is the same as @code{$(word $(words @var{text}),@var{text})},
6308the @code{lastword} function was added for its simplicity and better
6309performance.@refill
6310@end table
6311
6312
6313Here is a realistic example of the use of @code{subst} and
6314@code{patsubst}. Suppose that a makefile uses the @code{VPATH} variable
6315to specify a list of directories that @code{make} should search for
6316prerequisite files
6317(@pxref{General Search, , @code{VPATH} Search Path for All Prerequisites}).
6318This example shows how to
6319tell the C compiler to search for header files in the same list of
6320directories.@refill
6321
6322The value of @code{VPATH} is a list of directories separated by colons,
6323such as @samp{src:../headers}. First, the @code{subst} function is used to
6324change the colons to spaces:
6325
6326@example
6327$(subst :, ,$(VPATH))
6328@end example
6329
6330@noindent
6331This produces @samp{src ../headers}. Then @code{patsubst} is used to turn
6332each directory name into a @samp{-I} flag. These can be added to the
6333value of the variable @code{CFLAGS}, which is passed automatically to the C
6334compiler, like this:
6335
6336@example
6337override CFLAGS += $(patsubst %,-I%,$(subst :, ,$(VPATH)))
6338@end example
6339
6340@noindent
6341The effect is to append the text @samp{-Isrc -I../headers} to the
6342previously given value of @code{CFLAGS}. The @code{override} directive is
6343used so that the new value is assigned even if the previous value of
6344@code{CFLAGS} was specified with a command argument (@pxref{Override
6345Directive, , The @code{override} Directive}).
6346
6347@node File Name Functions, Foreach Function, Text Functions, Functions
6348@section Functions for File Names
6349@cindex functions, for file names
6350@cindex file name functions
6351
6352Several of the built-in expansion functions relate specifically to
6353taking apart file names or lists of file names.
6354
6355Each of the following functions performs a specific transformation on a
6356file name. The argument of the function is regarded as a series of file
6357names, separated by whitespace. (Leading and trailing whitespace is
6358ignored.) Each file name in the series is transformed in the same way and
6359the results are concatenated with single spaces between them.
6360
6361@table @code
6362@item $(dir @var{names}@dots{})
6363@findex dir
6364@cindex directory part
6365@cindex file name, directory part
6366Extracts the directory-part of each file name in @var{names}. The
6367directory-part of the file name is everything up through (and
6368including) the last slash in it. If the file name contains no slash,
6369the directory part is the string @samp{./}. For example,
6370
6371@example
6372$(dir src/foo.c hacks)
6373@end example
6374
6375@noindent
6376produces the result @samp{src/ ./}.
6377
6378@item $(notdir @var{names}@dots{})
6379@findex notdir
6380@cindex file name, nondirectory part
6381@cindex nondirectory part
6382Extracts all but the directory-part of each file name in @var{names}.
6383If the file name contains no slash, it is left unchanged. Otherwise,
6384everything through the last slash is removed from it.
6385
6386A file name that ends with a slash becomes an empty string. This is
6387unfortunate, because it means that the result does not always have the
6388same number of whitespace-separated file names as the argument had;
6389but we do not see any other valid alternative.
6390
6391For example,
6392
6393@example
6394$(notdir src/foo.c hacks)
6395@end example
6396
6397@noindent
6398produces the result @samp{foo.c hacks}.
6399
6400@item $(suffix @var{names}@dots{})
6401@findex suffix
6402@cindex suffix, function to find
6403@cindex file name suffix
6404Extracts the suffix of each file name in @var{names}. If the file name
6405contains a period, the suffix is everything starting with the last
6406period. Otherwise, the suffix is the empty string. This frequently
6407means that the result will be empty when @var{names} is not, and if
6408@var{names} contains multiple file names, the result may contain fewer
6409file names.
6410
6411For example,
6412
6413@example
6414$(suffix src/foo.c src-1.0/bar.c hacks)
6415@end example
6416
6417@noindent
6418produces the result @samp{.c .c}.
6419
6420@item $(basename @var{names}@dots{})
6421@findex basename
6422@cindex basename
6423@cindex file name, basename of
6424Extracts all but the suffix of each file name in @var{names}. If the
6425file name contains a period, the basename is everything starting up to
6426(and not including) the last period. Periods in the directory part are
6427ignored. If there is no period, the basename is the entire file name.
6428For example,
6429
6430@example
6431$(basename src/foo.c src-1.0/bar hacks)
6432@end example
6433
6434@noindent
6435produces the result @samp{src/foo src-1.0/bar hacks}.
6436
6437@c plural convention with dots (be consistent)
6438@item $(addsuffix @var{suffix},@var{names}@dots{})
6439@findex addsuffix
6440@cindex suffix, adding
6441@cindex file name suffix, adding
6442The argument @var{names} is regarded as a series of names, separated
6443by whitespace; @var{suffix} is used as a unit. The value of
6444@var{suffix} is appended to the end of each individual name and the
6445resulting larger names are concatenated with single spaces between
6446them. For example,
6447
6448@example
6449$(addsuffix .c,foo bar)
6450@end example
6451
6452@noindent
6453produces the result @samp{foo.c bar.c}.
6454
6455@item $(addprefix @var{prefix},@var{names}@dots{})
6456@findex addprefix
6457@cindex prefix, adding
6458@cindex file name prefix, adding
6459The argument @var{names} is regarded as a series of names, separated
6460by whitespace; @var{prefix} is used as a unit. The value of
6461@var{prefix} is prepended to the front of each individual name and the
6462resulting larger names are concatenated with single spaces between
6463them. For example,
6464
6465@example
6466$(addprefix src/,foo bar)
6467@end example
6468
6469@noindent
6470produces the result @samp{src/foo src/bar}.
6471
6472@item $(join @var{list1},@var{list2})
6473@findex join
6474@cindex joining lists of words
6475@cindex words, joining lists
6476Concatenates the two arguments word by word: the two first words (one
6477from each argument) concatenated form the first word of the result, the
6478two second words form the second word of the result, and so on. So the
6479@var{n}th word of the result comes from the @var{n}th word of each
6480argument. If one argument has more words that the other, the extra
6481words are copied unchanged into the result.
6482
6483For example, @samp{$(join a b,.c .o)} produces @samp{a.c b.o}.
6484
6485Whitespace between the words in the lists is not preserved; it is
6486replaced with a single space.
6487
6488This function can merge the results of the @code{dir} and
6489@code{notdir} functions, to produce the original list of files which
6490was given to those two functions.@refill
6491
6492@item $(wildcard @var{pattern})
6493@findex wildcard
6494@cindex wildcard, function
6495The argument @var{pattern} is a file name pattern, typically containing
6496wildcard characters (as in shell file name patterns). The result of
6497@code{wildcard} is a space-separated list of the names of existing files
6498that match the pattern.
6499@xref{Wildcards, ,Using Wildcard Characters in File Names}.
6500
6501@item $(realpath @var{names}@dots{})
6502@findex realpath
6503@cindex realpath
6504@cindex file name, realpath of
6505For each file name in @var{names} return the canonical absolute name.
6506A canonical name does not contain any @code{.} or @code{..} components,
6507nor any repeated path separators (@code{/}) or symlinks. In case of a
6508failure the empty string is returned. Consult the @code{realpath(3)}
6509documentation for a list of possible failure causes.
6510
6511@item $(abspath @var{names}@dots{})
6512@findex abspath
6513@cindex abspath
6514@cindex file name, abspath of
6515For each file name in @var{names} return an absolute name that does
6516not contain any @code{.} or @code{..} components, nor any repeated path
6517separators (@code{/}). Note that in contrast to @code{realpath}
6518function, @code{abspath} does not resolve symlinks and does not require
6519the file names to refer to an existing file or directory. Use the
6520@code{wildcard} function to test for existence.
6521@end table
6522
6523@node Foreach Function, If Function, File Name Functions, Functions
6524@section The @code{foreach} Function
6525@findex foreach
6526@cindex words, iterating over
6527
6528The @code{foreach} function is very different from other functions. It
6529causes one piece of text to be used repeatedly, each time with a different
6530substitution performed on it. It resembles the @code{for} command in the
6531shell @code{sh} and the @code{foreach} command in the C-shell @code{csh}.
6532
6533The syntax of the @code{foreach} function is:
6534
6535@example
6536$(foreach @var{var},@var{list},@var{text})
6537@end example
6538
6539@noindent
6540The first two arguments, @var{var} and @var{list}, are expanded before
6541anything else is done; note that the last argument, @var{text}, is
6542@strong{not} expanded at the same time. Then for each word of the expanded
6543value of @var{list}, the variable named by the expanded value of @var{var}
6544is set to that word, and @var{text} is expanded. Presumably @var{text}
6545contains references to that variable, so its expansion will be different
6546each time.
6547
6548The result is that @var{text} is expanded as many times as there are
6549whitespace-separated words in @var{list}. The multiple expansions of
6550@var{text} are concatenated, with spaces between them, to make the result
6551of @code{foreach}.
6552
6553This simple example sets the variable @samp{files} to the list of all files
6554in the directories in the list @samp{dirs}:
6555
6556@example
6557dirs := a b c d
6558files := $(foreach dir,$(dirs),$(wildcard $(dir)/*))
6559@end example
6560
6561Here @var{text} is @samp{$(wildcard $(dir)/*)}. The first repetition
6562finds the value @samp{a} for @code{dir}, so it produces the same result
6563as @samp{$(wildcard a/*)}; the second repetition produces the result
6564of @samp{$(wildcard b/*)}; and the third, that of @samp{$(wildcard c/*)}.
6565
6566This example has the same result (except for setting @samp{dirs}) as
6567the following example:
6568
6569@example
6570files := $(wildcard a/* b/* c/* d/*)
6571@end example
6572
6573When @var{text} is complicated, you can improve readability by giving it
6574a name, with an additional variable:
6575
6576@example
6577find_files = $(wildcard $(dir)/*)
6578dirs := a b c d
6579files := $(foreach dir,$(dirs),$(find_files))
6580@end example
6581
6582@noindent
6583Here we use the variable @code{find_files} this way. We use plain @samp{=}
6584to define a recursively-expanding variable, so that its value contains an
6585actual function call to be reexpanded under the control of @code{foreach};
6586a simply-expanded variable would not do, since @code{wildcard} would be
6587called only once at the time of defining @code{find_files}.
6588
6589The @code{foreach} function has no permanent effect on the variable
6590@var{var}; its value and flavor after the @code{foreach} function call are
6591the same as they were beforehand. The other values which are taken from
6592@var{list} are in effect only temporarily, during the execution of
6593@code{foreach}. The variable @var{var} is a simply-expanded variable
6594during the execution of @code{foreach}. If @var{var} was undefined
6595before the @code{foreach} function call, it is undefined after the call.
6596@xref{Flavors, ,The Two Flavors of Variables}.@refill
6597
6598You must take care when using complex variable expressions that result in
6599variable names because many strange things are valid variable names, but
6600are probably not what you intended. For example,
6601
6602@smallexample
6603files := $(foreach Esta escrito en espanol!,b c ch,$(find_files))
6604@end smallexample
6605
6606@noindent
6607might be useful if the value of @code{find_files} references the variable
6608whose name is @samp{Esta escrito en espanol!} (es un nombre bastante largo,
6609no?), but it is more likely to be a mistake.
6610
6611@node If Function, Call Function, Foreach Function, Functions
6612@section The @code{if} Function
6613@findex if
6614@cindex conditional expansion
6615
6616The @code{if} function provides support for conditional expansion in a
6617functional context (as opposed to the GNU @code{make} makefile
6618conditionals such as @code{ifeq} (@pxref{Conditional Syntax, ,Syntax of
6619Conditionals}).
6620
6621An @code{if} function call can contain either two or three arguments:
6622
6623@example
6624$(if @var{condition},@var{then-part}[,@var{else-part}])
6625@end example
6626
6627The first argument, @var{condition}, first has all preceding and
6628trailing whitespace stripped, then is expanded. If it expands to any
6629non-empty string, then the condition is considered to be true. If it
6630expands to an empty string, the condition is considered to be false.
6631
6632If the condition is true then the second argument, @var{then-part}, is
6633evaluated and this is used as the result of the evaluation of the entire
6634@code{if} function.
6635
6636If the condition is false then the third argument, @var{else-part}, is
6637evaluated and this is the result of the @code{if} function. If there is
6638no third argument, the @code{if} function evaluates to nothing (the
6639empty string).
6640
6641Note that only one of the @var{then-part} or the @var{else-part} will be
6642evaluated, never both. Thus, either can contain side-effects (such as
6643@code{shell} function calls, etc.)
6644
6645@node Call Function, Value Function, If Function, Functions
6646@section The @code{call} Function
6647@findex call
6648@cindex functions, user defined
6649@cindex user defined functions
6650
6651The @code{call} function is unique in that it can be used to create new
6652parameterized functions. You can write a complex expression as the
6653value of a variable, then use @code{call} to expand it with different
6654values.
6655
6656The syntax of the @code{call} function is:
6657
6658@example
6659$(call @var{variable},@var{param},@var{param},@dots{})
6660@end example
6661
6662When @code{make} expands this function, it assigns each @var{param} to
6663temporary variables @code{$(1)}, @code{$(2)}, etc. The variable
6664@code{$(0)} will contain @var{variable}. There is no maximum number of
6665parameter arguments. There is no minimum, either, but it doesn't make
6666sense to use @code{call} with no parameters.
6667
6668Then @var{variable} is expanded as a @code{make} variable in the context
6669of these temporary assignments. Thus, any reference to @code{$(1)} in
6670the value of @var{variable} will resolve to the first @var{param} in the
6671invocation of @code{call}.
6672
6673Note that @var{variable} is the @emph{name} of a variable, not a
6674@emph{reference} to that variable. Therefore you would not normally use
6675a @samp{$} or parentheses when writing it. (You can, however, use a
6676variable reference in the name if you want the name not to be a
6677constant.)
6678
6679If @var{variable} is the name of a builtin function, the builtin function
6680is always invoked (even if a @code{make} variable by that name also
6681exists).
6682
6683The @code{call} function expands the @var{param} arguments before
6684assigning them to temporary variables. This means that @var{variable}
6685values containing references to builtin functions that have special
6686expansion rules, like @code{foreach} or @code{if}, may not work as you
6687expect.
6688
6689Some examples may make this clearer.
6690
6691This macro simply reverses its arguments:
6692
6693@smallexample
6694reverse = $(2) $(1)
6695
6696foo = $(call reverse,a,b)
6697@end smallexample
6698
6699@noindent
6700Here @var{foo} will contain @samp{b a}.
6701
6702This one is slightly more interesting: it defines a macro to search for
6703the first instance of a program in @code{PATH}:
6704
6705@smallexample
6706pathsearch = $(firstword $(wildcard $(addsuffix /$(1),$(subst :, ,$(PATH)))))
6707
6708LS := $(call pathsearch,ls)
6709@end smallexample
6710
6711@noindent
6712Now the variable LS contains @code{/bin/ls} or similar.
6713
6714The @code{call} function can be nested. Each recursive invocation gets
6715its own local values for @code{$(1)}, etc. that mask the values of
6716higher-level @code{call}. For example, here is an implementation of a
6717@dfn{map} function:
6718
6719@smallexample
6720map = $(foreach a,$(2),$(call $(1),$(a)))
6721@end smallexample
6722
6723Now you can @var{map} a function that normally takes only one argument,
6724such as @code{origin}, to multiple values in one step:
6725
6726@smallexample
6727o = $(call map,origin,o map MAKE)
6728@end smallexample
6729
6730and end up with @var{o} containing something like @samp{file file default}.
6731
6732A final caution: be careful when adding whitespace to the arguments to
6733@code{call}. As with other functions, any whitespace contained in the
6734second and subsequent arguments is kept; this can cause strange
6735effects. It's generally safest to remove all extraneous whitespace when
6736providing parameters to @code{call}.
6737
6738@node Value Function, Eval Function, Call Function, Functions
6739@comment node-name, next, previous, up
6740@section The @code{value} Function
6741@findex value
6742@cindex variables, unexpanded value
6743
6744The @code{value} function provides a way for you to use the value of a
6745variable @emph{without} having it expanded. Please note that this
6746does not undo expansions which have already occurred; for example if
6747you create a simply expanded variable its value is expanded during the
6748definition; in that case the @code{value} function will return the
6749same result as using the variable directly.
6750
6751The syntax of the @code{value} function is:
6752
6753@example
6754$(value @var{variable})
6755@end example
6756
6757Note that @var{variable} is the @emph{name} of a variable; not a
6758@emph{reference} to that variable. Therefore you would not normally
6759use a @samp{$} or parentheses when writing it. (You can, however, use
6760a variable reference in the name if you want the name not to be a
6761constant.)
6762
6763The result of this function is a string containing the value of
6764@var{variable}, without any expansion occurring. For example, in this
6765makefile:
6766
6767@example
6768@group
6769FOO = $PATH
6770
6771all:
6772 @@echo $(FOO)
6773 @@echo $(value FOO)
6774@end group
6775@end example
6776
6777@noindent
6778The first output line would be @code{ATH}, since the ``$P'' would be
6779expanded as a @code{make} variable, while the second output line would
6780be the current value of your @code{$PATH} environment variable, since
6781the @code{value} function avoided the expansion.
6782
6783The @code{value} function is most often used in conjunction with the
6784@code{eval} function (@pxref{Eval Function}).
6785
6786@node Eval Function, Origin Function, Value Function, Functions
6787@comment node-name, next, previous, up
6788@section The @code{eval} Function
6789@findex eval
6790@cindex evaluating makefile syntax
6791@cindex makefile syntax, evaluating
6792
6793The @code{eval} function is very special: it allows you to define new
6794makefile constructs that are not constant; which are the result of
6795evaluating other variables and functions. The argument to the
6796@code{eval} function is expanded, then the results of that expansion
6797are parsed as makefile syntax. The expanded results can define new
6798@code{make} variables, targets, implicit or explicit rules, etc.
6799
6800The result of the @code{eval} function is always the empty string;
6801thus, it can be placed virtually anywhere in a makefile without
6802causing syntax errors.
6803
6804It's important to realize that the @code{eval} argument is expanded
6805@emph{twice}; first by the @code{eval} function, then the results of
6806that expansion are expanded again when they are parsed as makefile
6807syntax. This means you may need to provide extra levels of escaping
6808for ``$'' characters when using @code{eval}. The @code{value}
6809function (@pxref{Value Function}) can sometimes be useful in these
6810situations, to circumvent unwanted expansions.
6811
6812Here is an example of how @code{eval} can be used; this example
6813combines a number of concepts and other functions. Although it might
6814seem overly complex to use @code{eval} in this example, rather than
6815just writing out the rules, consider two things: first, the template
6816definition (in @code{PROGRAM_template}) could need to be much more
6817complex than it is here; and second, you might put the complex,
6818``generic'' part of this example into another makefile, then include
6819it in all the individual makefiles. Now your individual makefiles are
6820quite straightforward.
6821
6822@example
6823@group
6824PROGRAMS = server client
6825
6826server_OBJS = server.o server_priv.o server_access.o
6827server_LIBS = priv protocol
6828
6829client_OBJS = client.o client_api.o client_mem.o
6830client_LIBS = protocol
6831
6832# Everything after this is generic
6833
6834.PHONY: all
6835all: $(PROGRAMS)
6836
6837define PROGRAM_template
6838 $(1): $$($(1)_OBJS) $$($(1)_LIBS:%=-l%)
6839 ALL_OBJS += $$($(1)_OBJS)
6840endef
6841
6842$(foreach prog,$(PROGRAMS),$(eval $(call PROGRAM_template,$(prog))))
6843
6844$(PROGRAMS):
6845 $(LINK.o) $^ $(LDLIBS) -o $@@
6846
6847clean:
6848 rm -f $(ALL_OBJS) $(PROGRAMS)
6849@end group
6850@end example
6851
6852@node Origin Function, Shell Function, Eval Function, Functions
6853@section The @code{origin} Function
6854@findex origin
6855@cindex variables, origin of
6856@cindex origin of variable
6857
6858The @code{origin} function is unlike most other functions in that it does
6859not operate on the values of variables; it tells you something @emph{about}
6860a variable. Specifically, it tells you where it came from.
6861
6862The syntax of the @code{origin} function is:
6863
6864@example
6865$(origin @var{variable})
6866@end example
6867
6868Note that @var{variable} is the @emph{name} of a variable to inquire about;
6869not a @emph{reference} to that variable. Therefore you would not normally
6870use a @samp{$} or parentheses when writing it. (You can, however, use a
6871variable reference in the name if you want the name not to be a constant.)
6872
6873The result of this function is a string telling you how the variable
6874@var{variable} was defined:
6875
6876@table @samp
6877@item undefined
6878
6879if @var{variable} was never defined.
6880
6881@item default
6882
6883if @var{variable} has a default definition, as is usual with @code{CC}
6884and so on. @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
6885Note that if you have redefined a default variable, the @code{origin}
6886function will return the origin of the later definition.
6887
6888@item environment
6889
6890if @var{variable} was defined as an environment variable and the
6891@samp{-e} option is @emph{not} turned on (@pxref{Options Summary, ,Summary of Options}).
6892
6893@item environment override
6894
6895if @var{variable} was defined as an environment variable and the
6896@w{@samp{-e}} option @emph{is} turned on (@pxref{Options Summary,
6897,Summary of Options}).@refill
6898
6899@item file
6900
6901if @var{variable} was defined in a makefile.
6902
6903@item command line
6904
6905if @var{variable} was defined on the command line.
6906
6907@item override
6908
6909if @var{variable} was defined with an @code{override} directive in a
6910makefile (@pxref{Override Directive, ,The @code{override} Directive}).
6911
6912@item automatic
6913
6914if @var{variable} is an automatic variable defined for the
6915execution of the commands for each rule
6916(@pxref{Automatic Variables}).
6917@end table
6918
6919This information is primarily useful (other than for your curiosity) to
6920determine if you want to believe the value of a variable. For example,
6921suppose you have a makefile @file{foo} that includes another makefile
6922@file{bar}. You want a variable @code{bletch} to be defined in @file{bar}
6923if you run the command @w{@samp{make -f bar}}, even if the environment contains
6924a definition of @code{bletch}. However, if @file{foo} defined
6925@code{bletch} before including @file{bar}, you do not want to override that
6926definition. This could be done by using an @code{override} directive in
6927@file{foo}, giving that definition precedence over the later definition in
6928@file{bar}; unfortunately, the @code{override} directive would also
6929override any command line definitions. So, @file{bar} could
6930include:@refill
6931
6932@example
6933@group
6934ifdef bletch
6935ifeq "$(origin bletch)" "environment"
6936bletch = barf, gag, etc.
6937endif
6938endif
6939@end group
6940@end example
6941
6942@noindent
6943If @code{bletch} has been defined from the environment, this will redefine
6944it.
6945
6946If you want to override a previous definition of @code{bletch} if it came
6947from the environment, even under @samp{-e}, you could instead write:
6948
6949@example
6950@group
6951ifneq "$(findstring environment,$(origin bletch))" ""
6952bletch = barf, gag, etc.
6953endif
6954@end group
6955@end example
6956
6957Here the redefinition takes place if @samp{$(origin bletch)} returns either
6958@samp{environment} or @samp{environment override}.
6959@xref{Text Functions, , Functions for String Substitution and Analysis}.
6960
6961@node Shell Function, Make Control Functions, Origin Function, Functions
6962@section The @code{shell} Function
6963@findex shell
6964@cindex commands, expansion
6965@cindex backquotes
6966@cindex shell command, function for
6967
6968The @code{shell} function is unlike any other function other than the
6969@code{wildcard} function
6970(@pxref{Wildcard Function, ,The Function @code{wildcard}}) in that it
6971communicates with the world outside of @code{make}.
6972
6973The @code{shell} function performs the same function that backquotes
6974(@samp{`}) perform in most shells: it does @dfn{command expansion}.
6975This means that it takes as an argument a shell command and evaluates
6976to the output of the command. The only processing @code{make} does on
6977the result is to convert each newline (or carriage-return / newline
6978pair) to a single space. If there is a trailing (carriage-return
6979and) newline it will simply be removed.@refill
6980
6981The commands run by calls to the @code{shell} function are run when the
6982function calls are expanded (@pxref{Reading Makefiles, , How
6983@code{make} Reads a Makefile}). Because this function involves
6984spawning a new shell, you should carefully consider the performance
6985implications of using the @code{shell} function within recursively
6986expanded variables vs. simply expanded variables (@pxref{Flavors, ,The
6987Two Flavors of Variables}).
6988
6989Here are some examples of the use of the @code{shell} function:
6990
6991@example
6992contents := $(shell cat foo)
6993@end example
6994
6995@noindent
6996sets @code{contents} to the contents of the file @file{foo}, with a space
6997(rather than a newline) separating each line.
6998
6999@example
7000files := $(shell echo *.c)
7001@end example
7002
7003@noindent
7004sets @code{files} to the expansion of @samp{*.c}. Unless @code{make} is
7005using a very strange shell, this has the same result as
7006@w{@samp{$(wildcard *.c)}} (as long as at least one @samp{.c} file
7007exists).@refill
7008
7009@node Make Control Functions, , Shell Function, Functions
7010@section Functions That Control Make
7011@cindex functions, for controlling make
7012@cindex controlling make
7013
7014These functions control the way make runs. Generally, they are used to
7015provide information to the user of the makefile or to cause make to stop
7016if some sort of environmental error is detected.
7017
7018@table @code
7019@item $(error @var{text}@dots{})
7020@findex error
7021@cindex error, stopping on
7022@cindex stopping make
7023Generates a fatal error where the message is @var{text}. Note that the
7024error is generated whenever this function is evaluated. So, if you put
7025it inside a command script or on the right side of a recursive variable
7026assignment, it won't be evaluated until later. The @var{text} will be
7027expanded before the error is generated.
7028
7029For example,
7030
7031@example
7032ifdef ERROR1
7033$(error error is $(ERROR1))
7034endif
7035@end example
7036
7037@noindent
7038will generate a fatal error during the read of the makefile if the
7039@code{make} variable @code{ERROR1} is defined. Or,
7040
7041@example
7042ERR = $(error found an error!)
7043
7044.PHONY: err
7045err: ; $(ERR)
7046@end example
7047
7048@noindent
7049will generate a fatal error while @code{make} is running, if the
7050@code{err} target is invoked.
7051
7052@item $(warning @var{text}@dots{})
7053@findex warning
7054@cindex warnings, printing
7055@cindex printing user warnings
7056This function works similarly to the @code{error} function, above,
7057except that @code{make} doesn't exit. Instead, @var{text} is expanded
7058and the resulting message is displayed, but processing of the makefile
7059continues.
7060
7061The result of the expansion of this function is the empty string.
7062
7063@item $(info @var{text}@dots{})
7064@findex info
7065@cindex printing messages
7066This function does nothing more than print its (expanded) argument(s)
7067to standard output. No makefile name or line number is added. The
7068result of the expansion of this function is the empty string.
7069@end table
7070
7071@node Running, Implicit Rules, Functions, Top
7072@chapter How to Run @code{make}
7073
7074A makefile that says how to recompile a program can be used in more
7075than one way. The simplest use is to recompile every file that is out
7076of date. Usually, makefiles are written so that if you run
7077@code{make} with no arguments, it does just that.
7078
7079But you might want to update only some of the files; you might want to use
7080a different compiler or different compiler options; you might want just to
7081find out which files are out of date without changing them.
7082
7083By giving arguments when you run @code{make}, you can do any of these
7084things and many others.
7085
7086The exit status of @code{make} is always one of three values:
7087@table @code
7088@item 0
7089The exit status is zero if @code{make} is successful.
7090@item 2
7091The exit status is two if @code{make} encounters any errors.
7092It will print messages describing the particular errors.
7093@item 1
7094The exit status is one if you use the @samp{-q} flag and @code{make}
7095determines that some target is not already up to date.
7096@xref{Instead of Execution, ,Instead of Executing the Commands}.
7097@end table
7098
7099@menu
7100* Makefile Arguments:: How to specify which makefile to use.
7101* Goals:: How to use goal arguments to specify which
7102 parts of the makefile to use.
7103* Instead of Execution:: How to use mode flags to specify what
7104 kind of thing to do with the commands
7105 in the makefile other than simply
7106 execute them.
7107* Avoiding Compilation:: How to avoid recompiling certain files.
7108* Overriding:: How to override a variable to specify
7109 an alternate compiler and other things.
7110* Testing:: How to proceed past some errors, to
7111 test compilation.
7112* Options Summary:: Summary of Options
7113@end menu
7114
7115@node Makefile Arguments, Goals, Running, Running
7116@section Arguments to Specify the Makefile
7117@cindex @code{--file}
7118@cindex @code{--makefile}
7119@cindex @code{-f}
7120
7121The way to specify the name of the makefile is with the @samp{-f} or
7122@samp{--file} option (@samp{--makefile} also works). For example,
7123@samp{-f altmake} says to use the file @file{altmake} as the makefile.
7124
7125If you use the @samp{-f} flag several times and follow each @samp{-f}
7126with an argument, all the specified files are used jointly as
7127makefiles.
7128
7129If you do not use the @samp{-f} or @samp{--file} flag, the default is
7130to try @file{GNUmakefile}, @file{makefile}, and @file{Makefile}, in
7131that order, and use the first of these three which exists or can be made
7132(@pxref{Makefiles, ,Writing Makefiles}).@refill
7133
7134@node Goals, Instead of Execution, Makefile Arguments, Running
7135@section Arguments to Specify the Goals
7136@cindex goal, how to specify
7137
7138The @dfn{goals} are the targets that @code{make} should strive ultimately
7139to update. Other targets are updated as well if they appear as
7140prerequisites of goals, or prerequisites of prerequisites of goals, etc.
7141
7142By default, the goal is the first target in the makefile (not counting
7143targets that start with a period). Therefore, makefiles are usually
7144written so that the first target is for compiling the entire program or
7145programs they describe. If the first rule in the makefile has several
7146targets, only the first target in the rule becomes the default goal, not
7147the whole list. You can manage the selection of the default goal from
7148within your makefile using the @code{.DEFAULT_GOAL} variable
7149(@pxref{Special Variables, , Other Special Variables}).
7150
7151You can also specify a different goal or goals with command-line
7152arguments to @code{make}. Use the name of the goal as an argument.
7153If you specify several goals, @code{make} processes each of them in
7154turn, in the order you name them.
7155
7156Any target in the makefile may be specified as a goal (unless it
7157starts with @samp{-} or contains an @samp{=}, in which case it will be
7158parsed as a switch or variable definition, respectively). Even
7159targets not in the makefile may be specified, if @code{make} can find
7160implicit rules that say how to make them.
7161
7162@vindex MAKECMDGOALS
7163@code{Make} will set the special variable @code{MAKECMDGOALS} to the
7164list of goals you specified on the command line. If no goals were given
7165on the command line, this variable is empty. Note that this variable
7166should be used only in special circumstances.
7167
7168An example of appropriate use is to avoid including @file{.d} files
7169during @code{clean} rules (@pxref{Automatic Prerequisites}), so
7170@code{make} won't create them only to immediately remove them
7171again:@refill
7172
7173@example
7174@group
7175sources = foo.c bar.c
7176
7177ifneq ($(MAKECMDGOALS),clean)
7178include $(sources:.c=.d)
7179endif
7180@end group
7181@end example
7182
7183One use of specifying a goal is if you want to compile only a part of
7184the program, or only one of several programs. Specify as a goal each
7185file that you wish to remake. For example, consider a directory containing
7186several programs, with a makefile that starts like this:
7187
7188@example
7189.PHONY: all
7190all: size nm ld ar as
7191@end example
7192
7193If you are working on the program @code{size}, you might want to say
7194@w{@samp{make size}} so that only the files of that program are recompiled.
7195
7196Another use of specifying a goal is to make files that are not normally
7197made. For example, there may be a file of debugging output, or a
7198version of the program that is compiled specially for testing, which has
7199a rule in the makefile but is not a prerequisite of the default goal.
7200
7201Another use of specifying a goal is to run the commands associated with
7202a phony target (@pxref{Phony Targets}) or empty target (@pxref{Empty
7203Targets, ,Empty Target Files to Record Events}). Many makefiles contain
7204a phony target named @file{clean} which deletes everything except source
7205files. Naturally, this is done only if you request it explicitly with
7206@w{@samp{make clean}}. Following is a list of typical phony and empty
7207target names. @xref{Standard Targets}, for a detailed list of all the
7208standard target names which GNU software packages use.
7209
7210@table @file
7211@item all
7212@cindex @code{all} @r{(standard target)}
7213Make all the top-level targets the makefile knows about.
7214
7215@item clean
7216@cindex @code{clean} @r{(standard target)}
7217Delete all files that are normally created by running @code{make}.
7218
7219@item mostlyclean
7220@cindex @code{mostlyclean} @r{(standard target)}
7221Like @samp{clean}, but may refrain from deleting a few files that people
7222normally don't want to recompile. For example, the @samp{mostlyclean}
7223target for GCC does not delete @file{libgcc.a}, because recompiling it
7224is rarely necessary and takes a lot of time.
7225
7226@item distclean
7227@cindex @code{distclean} @r{(standard target)}
7228@itemx realclean
7229@cindex @code{realclean} @r{(standard target)}
7230@itemx clobber
7231@cindex @code{clobber} @r{(standard target)}
7232Any of these targets might be defined to delete @emph{more} files than
7233@samp{clean} does. For example, this would delete configuration files
7234or links that you would normally create as preparation for compilation,
7235even if the makefile itself cannot create these files.
7236
7237@item install
7238@cindex @code{install} @r{(standard target)}
7239Copy the executable file into a directory that users typically search
7240for commands; copy any auxiliary files that the executable uses into
7241the directories where it will look for them.
7242
7243@item print
7244@cindex @code{print} @r{(standard target)}
7245Print listings of the source files that have changed.
7246
7247@item tar
7248@cindex @code{tar} @r{(standard target)}
7249Create a tar file of the source files.
7250
7251@item shar
7252@cindex @code{shar} @r{(standard target)}
7253Create a shell archive (shar file) of the source files.
7254
7255@item dist
7256@cindex @code{dist} @r{(standard target)}
7257Create a distribution file of the source files. This might
7258be a tar file, or a shar file, or a compressed version of one of the
7259above, or even more than one of the above.
7260
7261@item TAGS
7262@cindex @code{TAGS} @r{(standard target)}
7263Update a tags table for this program.
7264
7265@item check
7266@cindex @code{check} @r{(standard target)}
7267@itemx test
7268@cindex @code{test} @r{(standard target)}
7269Perform self tests on the program this makefile builds.
7270@end table
7271
7272@node Instead of Execution, Avoiding Compilation, Goals, Running
7273@section Instead of Executing the Commands
7274@cindex execution, instead of
7275@cindex commands, instead of executing
7276
7277The makefile tells @code{make} how to tell whether a target is up to date,
7278and how to update each target. But updating the targets is not always
7279what you want. Certain options specify other activities for @code{make}.
7280
7281@comment Extra blank lines make it print better.
7282@table @samp
7283@item -n
7284@itemx --just-print
7285@itemx --dry-run
7286@itemx --recon
7287@cindex @code{--just-print}
7288@cindex @code{--dry-run}
7289@cindex @code{--recon}
7290@cindex @code{-n}
7291
7292``No-op''. The activity is to print what commands would be used to make
7293the targets up to date, but not actually execute them.
7294
7295@item -t
7296@itemx --touch
7297@cindex @code{--touch}
7298@cindex touching files
7299@cindex target, touching
7300@cindex @code{-t}
7301
7302``Touch''. The activity is to mark the targets as up to date without
7303actually changing them. In other words, @code{make} pretends to compile
7304the targets but does not really change their contents.
7305
7306@item -q
7307@itemx --question
7308@cindex @code{--question}
7309@cindex @code{-q}
7310@cindex question mode
7311
7312``Question''. The activity is to find out silently whether the targets
7313are up to date already; but execute no commands in either case. In other
7314words, neither compilation nor output will occur.
7315
7316@item -W @var{file}
7317@itemx --what-if=@var{file}
7318@itemx --assume-new=@var{file}
7319@itemx --new-file=@var{file}
7320@cindex @code{--what-if}
7321@cindex @code{-W}
7322@cindex @code{--assume-new}
7323@cindex @code{--new-file}
7324@cindex what if
7325@cindex files, assuming new
7326
7327``What if''. Each @samp{-W} flag is followed by a file name. The given
7328files' modification times are recorded by @code{make} as being the present
7329time, although the actual modification times remain the same.
7330You can use the @samp{-W} flag in conjunction with the @samp{-n} flag
7331to see what would happen if you were to modify specific files.@refill
7332@end table
7333
7334With the @samp{-n} flag, @code{make} prints the commands that it would
7335normally execute but does not execute them.
7336
7337With the @samp{-t} flag, @code{make} ignores the commands in the rules
7338and uses (in effect) the command @code{touch} for each target that needs to
7339be remade. The @code{touch} command is also printed, unless @samp{-s} or
7340@code{.SILENT} is used. For speed, @code{make} does not actually invoke
7341the program @code{touch}. It does the work directly.
7342
7343With the @samp{-q} flag, @code{make} prints nothing and executes no
7344commands, but the exit status code it returns is zero if and only if the
7345targets to be considered are already up to date. If the exit status is
7346one, then some updating needs to be done. If @code{make} encounters an
7347error, the exit status is two, so you can distinguish an error from a
7348target that is not up to date.
7349
7350It is an error to use more than one of these three flags in the same
7351invocation of @code{make}.
7352
7353@cindex +, and command execution
7354The @samp{-n}, @samp{-t}, and @samp{-q} options do not affect command
7355lines that begin with @samp{+} characters or contain the strings
7356@samp{$(MAKE)} or @samp{$@{MAKE@}}. Note that only the line containing
7357the @samp{+} character or the strings @samp{$(MAKE)} or @samp{$@{MAKE@}}
7358is run regardless of these options. Other lines in the same rule are
7359not run unless they too begin with @samp{+} or contain @samp{$(MAKE)} or
7360@samp{$@{MAKE@}} (@xref{MAKE Variable, ,How the @code{MAKE} Variable Works}.)
7361
7362The @samp{-W} flag provides two features:
7363
7364@itemize @bullet
7365@item
7366If you also use the @samp{-n} or @samp{-q} flag, you can see what
7367@code{make} would do if you were to modify some files.
7368
7369@item
7370Without the @samp{-n} or @samp{-q} flag, when @code{make} is actually
7371executing commands, the @samp{-W} flag can direct @code{make} to act
7372as if some files had been modified, without actually modifying the
7373files.@refill
7374@end itemize
7375
7376Note that the options @samp{-p} and @samp{-v} allow you to obtain other
7377information about @code{make} or about the makefiles in use
7378(@pxref{Options Summary, ,Summary of Options}).@refill
7379
7380@node Avoiding Compilation, Overriding, Instead of Execution, Running
7381@section Avoiding Recompilation of Some Files
7382@cindex @code{-o}
7383@cindex @code{--old-file}
7384@cindex @code{--assume-old}
7385@cindex files, assuming old
7386@cindex files, avoiding recompilation of
7387@cindex recompilation, avoiding
7388
7389Sometimes you may have changed a source file but you do not want to
7390recompile all the files that depend on it. For example, suppose you add
7391a macro or a declaration to a header file that many other files depend
7392on. Being conservative, @code{make} assumes that any change in the
7393header file requires recompilation of all dependent files, but you know
7394that they do not need to be recompiled and you would rather not waste
7395the time waiting for them to compile.
7396
7397If you anticipate the problem before changing the header file, you can
7398use the @samp{-t} flag. This flag tells @code{make} not to run the
7399commands in the rules, but rather to mark the target up to date by
7400changing its last-modification date. You would follow this procedure:
7401
7402@enumerate
7403@item
7404Use the command @samp{make} to recompile the source files that really
7405need recompilation, ensuring that the object files are up-to-date
7406before you begin.
7407
7408@item
7409Make the changes in the header files.
7410
7411@item
7412Use the command @samp{make -t} to mark all the object files as
7413up to date. The next time you run @code{make}, the changes in the
7414header files will not cause any recompilation.
7415@end enumerate
7416
7417If you have already changed the header file at a time when some files
7418do need recompilation, it is too late to do this. Instead, you can
7419use the @w{@samp{-o @var{file}}} flag, which marks a specified file as
7420``old'' (@pxref{Options Summary, ,Summary of Options}). This means
7421that the file itself will not be remade, and nothing else will be
7422remade on its account. Follow this procedure:
7423
7424@enumerate
7425@item
7426Recompile the source files that need compilation for reasons independent
7427of the particular header file, with @samp{make -o @var{headerfile}}.
7428If several header files are involved, use a separate @samp{-o} option
7429for each header file.
7430
7431@item
7432Touch all the object files with @samp{make -t}.
7433@end enumerate
7434
7435@node Overriding, Testing, Avoiding Compilation, Running
7436@section Overriding Variables
7437@cindex overriding variables with arguments
7438@cindex variables, overriding with arguments
7439@cindex command line variables
7440@cindex variables, command line
7441
7442An argument that contains @samp{=} specifies the value of a variable:
7443@samp{@var{v}=@var{x}} sets the value of the variable @var{v} to @var{x}.
7444If you specify a value in this way, all ordinary assignments of the same
7445variable in the makefile are ignored; we say they have been
7446@dfn{overridden} by the command line argument.
7447
7448The most common way to use this facility is to pass extra flags to
7449compilers. For example, in a properly written makefile, the variable
7450@code{CFLAGS} is included in each command that runs the C compiler, so a
7451file @file{foo.c} would be compiled something like this:
7452
7453@example
7454cc -c $(CFLAGS) foo.c
7455@end example
7456
7457Thus, whatever value you set for @code{CFLAGS} affects each compilation
7458that occurs. The makefile probably specifies the usual value for
7459@code{CFLAGS}, like this:
7460
7461@example
7462CFLAGS=-g
7463@end example
7464
7465Each time you run @code{make}, you can override this value if you
7466wish. For example, if you say @samp{make CFLAGS='-g -O'}, each C
7467compilation will be done with @samp{cc -c -g -O}. (This also
7468illustrates how you can use quoting in the shell to enclose spaces and
7469other special characters in the value of a variable when you override
7470it.)
7471
7472The variable @code{CFLAGS} is only one of many standard variables that
7473exist just so that you can change them this way. @xref{Implicit
7474Variables, , Variables Used by Implicit Rules}, for a complete list.
7475
7476You can also program the makefile to look at additional variables of your
7477own, giving the user the ability to control other aspects of how the
7478makefile works by changing the variables.
7479
7480When you override a variable with a command argument, you can define either
7481a recursively-expanded variable or a simply-expanded variable. The
7482examples shown above make a recursively-expanded variable; to make a
7483simply-expanded variable, write @samp{:=} instead of @samp{=}. But, unless
7484you want to include a variable reference or function call in the
7485@emph{value} that you specify, it makes no difference which kind of
7486variable you create.
7487
7488There is one way that the makefile can change a variable that you have
7489overridden. This is to use the @code{override} directive, which is a line
7490that looks like this: @samp{override @var{variable} = @var{value}}
7491(@pxref{Override Directive, ,The @code{override} Directive}).
7492
7493@node Testing, Options Summary, Overriding, Running
7494@section Testing the Compilation of a Program
7495@cindex testing compilation
7496@cindex compilation, testing
7497
7498Normally, when an error happens in executing a shell command, @code{make}
7499gives up immediately, returning a nonzero status. No further commands are
7500executed for any target. The error implies that the goal cannot be
7501correctly remade, and @code{make} reports this as soon as it knows.
7502
7503When you are compiling a program that you have just changed, this is not
7504what you want. Instead, you would rather that @code{make} try compiling
7505every file that can be tried, to show you as many compilation errors
7506as possible.
7507
7508@cindex @code{-k}
7509@cindex @code{--keep-going}
7510On these occasions, you should use the @samp{-k} or
7511@samp{--keep-going} flag. This tells @code{make} to continue to
7512consider the other prerequisites of the pending targets, remaking them
7513if necessary, before it gives up and returns nonzero status. For
7514example, after an error in compiling one object file, @samp{make -k}
7515will continue compiling other object files even though it already
7516knows that linking them will be impossible. In addition to continuing
7517after failed shell commands, @samp{make -k} will continue as much as
7518possible after discovering that it does not know how to make a target
7519or prerequisite file. This will always cause an error message, but
7520without @samp{-k}, it is a fatal error (@pxref{Options Summary,
7521,Summary of Options}).@refill
7522
7523The usual behavior of @code{make} assumes that your purpose is to get the
7524goals up to date; once @code{make} learns that this is impossible, it might
7525as well report the failure immediately. The @samp{-k} flag says that the
7526real purpose is to test as much as possible of the changes made in the
7527program, perhaps to find several independent problems so that you can
7528correct them all before the next attempt to compile. This is why Emacs'
7529@kbd{M-x compile} command passes the @samp{-k} flag by default.
7530
7531@node Options Summary, , Testing, Running
7532@section Summary of Options
7533@cindex options
7534@cindex flags
7535@cindex switches
7536
7537Here is a table of all the options @code{make} understands:
7538
7539@table @samp
7540@item -b
7541@cindex @code{-b}
7542@itemx -m
7543@cindex @code{-m}
7544These options are ignored for compatibility with other versions of @code{make}.
7545
7546@item -B
7547@cindex @code{-B}
7548@itemx --always-make
7549@cindex @code{--always-make}
7550Consider all targets out-of-date. GNU @code{make} proceeds to
7551consider targets and their prerequisites using the normal algorithms;
7552however, all these targets are remade, regardless of the status of
7553their prerequisites.
7554
7555@item -C @var{dir}
7556@cindex @code{-C}
7557@itemx --directory=@var{dir}
7558@cindex @code{--directory}
7559Change to directory @var{dir} before reading the makefiles. If multiple
7560@samp{-C} options are specified, each is interpreted relative to the
7561previous one: @samp{-C / -C etc} is equivalent to @samp{-C /etc}.
7562This is typically used with recursive invocations of @code{make}
7563(@pxref{Recursion, ,Recursive Use of @code{make}}).
7564
7565@item -d
7566@cindex @code{-d}
7567@c Extra blank line here makes the table look better.
7568
7569Print debugging information in addition to normal processing. The
7570debugging information says which files are being considered for
7571remaking, which file-times are being compared and with what results,
7572which files actually need to be remade, which implicit rules are
7573considered and which are applied---everything interesting about how
7574@code{make} decides what to do. The @code{-d} option is equivalent to
7575@samp{--debug=a} (see below).
7576
7577@item --debug[=@var{options}]
7578@cindex @code{--debug}
7579@c Extra blank line here makes the table look better.
7580
7581Print debugging information in addition to normal processing. Various
7582levels and types of output can be chosen. With no arguments, print the
7583``basic'' level of debugging. Possible arguments are below; only the
7584first character is considered, and values must be comma- or
7585space-separated.
7586
7587@table @code
7588@item a (@i{all})
7589All types of debugging output are enabled. This is equivalent to using
7590@samp{-d}.
7591
7592@item b (@i{basic})
7593Basic debugging prints each target that was found to be out-of-date, and
7594whether the build was successful or not.
7595
7596@item v (@i{verbose})
7597A level above @samp{basic}; includes messages about which makefiles were
7598parsed, prerequisites that did not need to be rebuilt, etc. This option
7599also enables @samp{basic} messages.
7600
7601@item i (@i{implicit})
7602Prints messages describing the implicit rule searches for each target.
7603This option also enables @samp{basic} messages.
7604
7605@item j (@i{jobs})
7606Prints messages giving details on the invocation of specific subcommands.
7607
7608@item m (@i{makefile})
7609By default, the above messages are not enabled while trying to remake
7610the makefiles. This option enables messages while rebuilding makefiles,
7611too. Note that the @samp{all} option does enable this option. This
7612option also enables @samp{basic} messages.
7613@end table
7614
7615@item -e
7616@cindex @code{-e}
7617@itemx --environment-overrides
7618@cindex @code{--environment-overrides}
7619Give variables taken from the environment precedence
7620over variables from makefiles.
7621@xref{Environment, ,Variables from the Environment}.
7622
7623@item -f @var{file}
7624@cindex @code{-f}
7625@itemx --file=@var{file}
7626@cindex @code{--file}
7627@itemx --makefile=@var{file}
7628@cindex @code{--makefile}
7629Read the file named @var{file} as a makefile.
7630@xref{Makefiles, ,Writing Makefiles}.
7631
7632@item -h
7633@cindex @code{-h}
7634@itemx --help
7635@cindex @code{--help}
7636@c Extra blank line here makes the table look better.
7637
7638Remind you of the options that @code{make} understands and then exit.
7639
7640@item -i
7641@cindex @code{-i}
7642@itemx --ignore-errors
7643@cindex @code{--ignore-errors}
7644Ignore all errors in commands executed to remake files.
7645@xref{Errors, ,Errors in Commands}.
7646
7647@item -I @var{dir}
7648@cindex @code{-I}
7649@itemx --include-dir=@var{dir}
7650@cindex @code{--include-dir}
7651Specifies a directory @var{dir} to search for included makefiles.
7652@xref{Include, ,Including Other Makefiles}. If several @samp{-I}
7653options are used to specify several directories, the directories are
7654searched in the order specified.
7655
7656@item -j [@var{jobs}]
7657@cindex @code{-j}
7658@itemx --jobs[=@var{jobs}]
7659@cindex @code{--jobs}
7660Specifies the number of jobs (commands) to run simultaneously. With no
7661argument, @code{make} runs as many jobs simultaneously as possible. If
7662there is more than one @samp{-j} option, the last one is effective.
7663@xref{Parallel, ,Parallel Execution},
7664for more information on how commands are run.
7665Note that this option is ignored on MS-DOS.
7666
7667@item -k
7668@cindex @code{-k}
7669@itemx --keep-going
7670@cindex @code{--keep-going}
7671Continue as much as possible after an error. While the target that
7672failed, and those that depend on it, cannot be remade, the other
7673prerequisites of these targets can be processed all the same.
7674@xref{Testing, ,Testing the Compilation of a Program}.
7675
7676@item -l [@var{load}]
7677@cindex @code{-l}
7678@itemx --load-average[=@var{load}]
7679@cindex @code{--load-average}
7680@itemx --max-load[=@var{load}]
7681@cindex @code{--max-load}
7682Specifies that no new jobs (commands) should be started if there are
7683other jobs running and the load average is at least @var{load} (a
7684floating-point number). With no argument, removes a previous load
7685limit. @xref{Parallel, ,Parallel Execution}.
7686
7687@item -L
7688@cindex @code{-L}
7689@itemx --check-symlink-times
7690@cindex @code{--check-symlink-times}
7691On systems that support symbolic links, this option causes @code{make}
7692to consider the timestamps on any symbolic links in addition to the
7693timestamp on the file referenced by those links. When this option is
7694provided, the most recent timestamp among the file and the symbolic
7695links is taken as the modification time for this target file.
7696
7697@item -n
7698@cindex @code{-n}
7699@itemx --just-print
7700@cindex @code{--just-print}
7701@itemx --dry-run
7702@cindex @code{--dry-run}
7703@itemx --recon
7704@cindex @code{--recon}
7705@c Extra blank line here makes the table look better.
7706
7707Print the commands that would be executed, but do not execute them.
7708@xref{Instead of Execution, ,Instead of Executing the Commands}.
7709
7710@item -o @var{file}
7711@cindex @code{-o}
7712@itemx --old-file=@var{file}
7713@cindex @code{--old-file}
7714@itemx --assume-old=@var{file}
7715@cindex @code{--assume-old}
7716Do not remake the file @var{file} even if it is older than its
7717prerequisites, and do not remake anything on account of changes in
7718@var{file}. Essentially the file is treated as very old and its rules
7719are ignored. @xref{Avoiding Compilation, ,Avoiding Recompilation of
7720Some Files}.@refill
7721
7722@item -p
7723@cindex @code{-p}
7724@itemx --print-data-base
7725@cindex @code{--print-data-base}
7726@cindex data base of @code{make} rules
7727@cindex predefined rules and variables, printing
7728Print the data base (rules and variable values) that results from
7729reading the makefiles; then execute as usual or as otherwise specified.
7730This also prints the version information given by the @samp{-v} switch
7731(see below). To print the data base without trying to remake any files,
7732use @w{@samp{make -qp}}. To print the data base of predefined rules and
7733variables, use @w{@samp{make -p -f /dev/null}}. The data base output
7734contains filename and linenumber information for command and variable
7735definitions, so it can be a useful debugging tool in complex environments.
7736
7737@item -q
7738@cindex @code{-q}
7739@itemx --question
7740@cindex @code{--question}
7741``Question mode''. Do not run any commands, or print anything; just
7742return an exit status that is zero if the specified targets are already
7743up to date, one if any remaking is required, or two if an error is
7744encountered. @xref{Instead of Execution, ,Instead of Executing the
7745Commands}.@refill
7746
7747@item -r
7748@cindex @code{-r}
7749@itemx --no-builtin-rules
7750@cindex @code{--no-builtin-rules}
7751Eliminate use of the built-in implicit rules (@pxref{Implicit Rules,
7752,Using Implicit Rules}). You can still define your own by writing
7753pattern rules (@pxref{Pattern Rules, ,Defining and Redefining Pattern
7754Rules}). The @samp{-r} option also clears out the default list of
7755suffixes for suffix rules (@pxref{Suffix Rules, ,Old-Fashioned Suffix
7756Rules}). But you can still define your own suffixes with a rule for
7757@code{.SUFFIXES}, and then define your own suffix rules. Note that only
7758@emph{rules} are affected by the @code{-r} option; default variables
7759remain in effect (@pxref{Implicit Variables, ,Variables Used by Implicit
7760Rules}); see the @samp{-R} option below.
7761
7762@item -R
7763@cindex @code{-R}
7764@itemx --no-builtin-variables
7765@cindex @code{--no-builtin-variables}
7766Eliminate use of the built-in rule-specific variables (@pxref{Implicit
7767Variables, ,Variables Used by Implicit Rules}). You can still define
7768your own, of course. The @samp{-R} option also automatically enables
7769the @samp{-r} option (see above), since it doesn't make sense to have
7770implicit rules without any definitions for the variables that they use.
7771
7772@item -s
7773@cindex @code{-s}
7774@itemx --silent
7775@cindex @code{--silent}
7776@itemx --quiet
7777@cindex @code{--quiet}
7778@c Extra blank line here makes the table look better.
7779
7780Silent operation; do not print the commands as they are executed.
7781@xref{Echoing, ,Command Echoing}.
7782
7783@item -S
7784@cindex @code{-S}
7785@itemx --no-keep-going
7786@cindex @code{--no-keep-going}
7787@itemx --stop
7788@cindex @code{--stop}
7789@c Extra blank line here makes the table look better.
7790
7791Cancel the effect of the @samp{-k} option. This is never necessary
7792except in a recursive @code{make} where @samp{-k} might be inherited
7793from the top-level @code{make} via @code{MAKEFLAGS}
7794(@pxref{Recursion, ,Recursive Use of @code{make}})
7795or if you set @samp{-k} in @code{MAKEFLAGS} in your environment.@refill
7796
7797@item -t
7798@cindex @code{-t}
7799@itemx --touch
7800@cindex @code{--touch}
7801@c Extra blank line here makes the table look better.
7802
7803Touch files (mark them up to date without really changing them)
7804instead of running their commands. This is used to pretend that the
7805commands were done, in order to fool future invocations of
7806@code{make}. @xref{Instead of Execution, ,Instead of Executing the Commands}.
7807
7808@item -v
7809@cindex @code{-v}
7810@itemx --version
7811@cindex @code{--version}
7812Print the version of the @code{make} program plus a copyright, a list
7813of authors, and a notice that there is no warranty; then exit.
7814
7815@item -w
7816@cindex @code{-w}
7817@itemx --print-directory
7818@cindex @code{--print-directory}
7819Print a message containing the working directory both before and after
7820executing the makefile. This may be useful for tracking down errors
7821from complicated nests of recursive @code{make} commands.
7822@xref{Recursion, ,Recursive Use of @code{make}}. (In practice, you
7823rarely need to specify this option since @samp{make} does it for you;
7824see @ref{-w Option, ,The @samp{--print-directory} Option}.)
7825
7826@itemx --no-print-directory
7827@cindex @code{--no-print-directory}
7828Disable printing of the working directory under @code{-w}.
7829This option is useful when @code{-w} is turned on automatically,
7830but you do not want to see the extra messages.
7831@xref{-w Option, ,The @samp{--print-directory} Option}.
7832
7833@item -W @var{file}
7834@cindex @code{-W}
7835@itemx --what-if=@var{file}
7836@cindex @code{--what-if}
7837@itemx --new-file=@var{file}
7838@cindex @code{--new-file}
7839@itemx --assume-new=@var{file}
7840@cindex @code{--assume-new}
7841Pretend that the target @var{file} has just been modified. When used
7842with the @samp{-n} flag, this shows you what would happen if you were
7843to modify that file. Without @samp{-n}, it is almost the same as
7844running a @code{touch} command on the given file before running
7845@code{make}, except that the modification time is changed only in the
7846imagination of @code{make}.
7847@xref{Instead of Execution, ,Instead of Executing the Commands}.
7848
7849@item --warn-undefined-variables
7850@cindex @code{--warn-undefined-variables}
7851@cindex variables, warning for undefined
7852@cindex undefined variables, warning message
7853Issue a warning message whenever @code{make} sees a reference to an
7854undefined variable. This can be helpful when you are trying to debug
7855makefiles which use variables in complex ways.
7856@end table
7857
7858@node Implicit Rules, Archives, Running, Top
7859@chapter Using Implicit Rules
7860@cindex implicit rule
7861@cindex rule, implicit
7862
7863Certain standard ways of remaking target files are used very often. For
7864example, one customary way to make an object file is from a C source file
7865using the C compiler, @code{cc}.
7866
7867@dfn{Implicit rules} tell @code{make} how to use customary techniques so
7868that you do not have to specify them in detail when you want to use
7869them. For example, there is an implicit rule for C compilation. File
7870names determine which implicit rules are run. For example, C
7871compilation typically takes a @file{.c} file and makes a @file{.o} file.
7872So @code{make} applies the implicit rule for C compilation when it sees
7873this combination of file name endings.@refill
7874
7875A chain of implicit rules can apply in sequence; for example, @code{make}
7876will remake a @file{.o} file from a @file{.y} file by way of a @file{.c} file.
7877@iftex
7878@xref{Chained Rules, ,Chains of Implicit Rules}.
7879@end iftex
7880
7881The built-in implicit rules use several variables in their commands so
7882that, by changing the values of the variables, you can change the way the
7883implicit rule works. For example, the variable @code{CFLAGS} controls the
7884flags given to the C compiler by the implicit rule for C compilation.
7885@iftex
7886@xref{Implicit Variables, ,Variables Used by Implicit Rules}.
7887@end iftex
7888
7889You can define your own implicit rules by writing @dfn{pattern rules}.
7890@iftex
7891@xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.
7892@end iftex
7893
7894@dfn{Suffix rules} are a more limited way to define implicit rules.
7895Pattern rules are more general and clearer, but suffix rules are
7896retained for compatibility.
7897@iftex
7898@xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
7899@end iftex
7900
7901@menu
7902* Using Implicit:: How to use an existing implicit rule
7903 to get the commands for updating a file.
7904* Catalogue of Rules:: A list of built-in implicit rules.
7905* Implicit Variables:: How to change what predefined rules do.
7906* Chained Rules:: How to use a chain of implicit rules.
7907* Pattern Rules:: How to define new implicit rules.
7908* Last Resort:: How to define commands for rules which
7909 cannot find any.
7910* Suffix Rules:: The old-fashioned style of implicit rule.
7911* Implicit Rule Search:: The precise algorithm for applying
7912 implicit rules.
7913@end menu
7914
7915@node Using Implicit, Catalogue of Rules, Implicit Rules, Implicit Rules
7916@section Using Implicit Rules
7917@cindex implicit rule, how to use
7918@cindex rule, implicit, how to use
7919
7920To allow @code{make} to find a customary method for updating a target file,
7921all you have to do is refrain from specifying commands yourself. Either
7922write a rule with no command lines, or don't write a rule at all. Then
7923@code{make} will figure out which implicit rule to use based on which
7924kind of source file exists or can be made.
7925
7926For example, suppose the makefile looks like this:
7927
7928@example
7929foo : foo.o bar.o
7930 cc -o foo foo.o bar.o $(CFLAGS) $(LDFLAGS)
7931@end example
7932
7933@noindent
7934Because you mention @file{foo.o} but do not give a rule for it, @code{make}
7935will automatically look for an implicit rule that tells how to update it.
7936This happens whether or not the file @file{foo.o} currently exists.
7937
7938If an implicit rule is found, it can supply both commands and one or
7939more prerequisites (the source files). You would want to write a rule
7940for @file{foo.o} with no command lines if you need to specify additional
7941prerequisites, such as header files, that the implicit rule cannot
7942supply.
7943
7944Each implicit rule has a target pattern and prerequisite patterns. There may
7945be many implicit rules with the same target pattern. For example, numerous
7946rules make @samp{.o} files: one, from a @samp{.c} file with the C compiler;
7947another, from a @samp{.p} file with the Pascal compiler; and so on. The rule
7948that actually applies is the one whose prerequisites exist or can be made.
7949So, if you have a file @file{foo.c}, @code{make} will run the C compiler;
7950otherwise, if you have a file @file{foo.p}, @code{make} will run the Pascal
7951compiler; and so on.
7952
7953Of course, when you write the makefile, you know which implicit rule you
7954want @code{make} to use, and you know it will choose that one because you
7955know which possible prerequisite files are supposed to exist.
7956@xref{Catalogue of Rules, ,Catalogue of Implicit Rules},
7957for a catalogue of all the predefined implicit rules.
7958
7959Above, we said an implicit rule applies if the required prerequisites ``exist
7960or can be made''. A file ``can be made'' if it is mentioned explicitly in
7961the makefile as a target or a prerequisite, or if an implicit rule can be
7962recursively found for how to make it. When an implicit prerequisite is the
7963result of another implicit rule, we say that @dfn{chaining} is occurring.
7964@xref{Chained Rules, ,Chains of Implicit Rules}.
7965
7966In general, @code{make} searches for an implicit rule for each target, and
7967for each double-colon rule, that has no commands. A file that is mentioned
7968only as a prerequisite is considered a target whose rule specifies nothing,
7969so implicit rule search happens for it. @xref{Implicit Rule Search, ,Implicit Rule Search Algorithm}, for the
7970details of how the search is done.
7971
7972Note that explicit prerequisites do not influence implicit rule search.
7973For example, consider this explicit rule:
7974
7975@example
7976foo.o: foo.p
7977@end example
7978
7979@noindent
7980The prerequisite on @file{foo.p} does not necessarily mean that
7981@code{make} will remake @file{foo.o} according to the implicit rule to
7982make an object file, a @file{.o} file, from a Pascal source file, a
7983@file{.p} file. For example, if @file{foo.c} also exists, the implicit
7984rule to make an object file from a C source file is used instead,
7985because it appears before the Pascal rule in the list of predefined
7986implicit rules (@pxref{Catalogue of Rules, , Catalogue of Implicit
7987Rules}).
7988
7989If you do not want an implicit rule to be used for a target that has no
7990commands, you can give that target empty commands by writing a semicolon
7991(@pxref{Empty Commands, ,Defining Empty Commands}).
7992
7993@node Catalogue of Rules, Implicit Variables, Using Implicit, Implicit Rules
7994@section Catalogue of Implicit Rules
7995@cindex implicit rule, predefined
7996@cindex rule, implicit, predefined
7997
7998Here is a catalogue of predefined implicit rules which are always
7999available unless the makefile explicitly overrides or cancels them.
8000@xref{Canceling Rules, ,Canceling Implicit Rules}, for information on
8001canceling or overriding an implicit rule. The @samp{-r} or
8002@samp{--no-builtin-rules} option cancels all predefined rules.
8003
8004Not all of these rules will always be defined, even when the @samp{-r}
8005option is not given. Many of the predefined implicit rules are
8006implemented in @code{make} as suffix rules, so which ones will be
8007defined depends on the @dfn{suffix list} (the list of prerequisites of
8008the special target @code{.SUFFIXES}). The default suffix list is:
8009@code{.out}, @code{.a}, @code{.ln}, @code{.o}, @code{.c}, @code{.cc},
8010@code{.C}, @code{.p}, @code{.f}, @code{.F}, @code{.r}, @code{.y},
8011@code{.l}, @code{.s}, @code{.S}, @code{.mod}, @code{.sym}, @code{.def},
8012@code{.h}, @code{.info}, @code{.dvi}, @code{.tex}, @code{.texinfo},
8013@code{.texi}, @code{.txinfo}, @code{.w}, @code{.ch} @code{.web},
8014@code{.sh}, @code{.elc}, @code{.el}. All of the implicit rules
8015described below whose prerequisites have one of these suffixes are
8016actually suffix rules. If you modify the suffix list, the only
8017predefined suffix rules in effect will be those named by one or two of
8018the suffixes that are on the list you specify; rules whose suffixes fail
8019to be on the list are disabled. @xref{Suffix Rules, ,Old-Fashioned
8020Suffix Rules}, for full details on suffix rules.
8021
8022@table @asis
8023@item Compiling C programs
8024@cindex C, rule to compile
8025@pindex cc
8026@pindex gcc
8027@pindex .o
8028@pindex .c
8029@file{@var{n}.o} is made automatically from @file{@var{n}.c} with
8030a command of the form @samp{$(CC) -c $(CPPFLAGS) $(CFLAGS)}.@refill
8031
8032@item Compiling C++ programs
8033@cindex C++, rule to compile
8034@pindex g++
8035@pindex .C
8036@pindex .cc
8037@file{@var{n}.o} is made automatically from @file{@var{n}.cc} or
8038@file{@var{n}.C} with a command of the form @samp{$(CXX) -c $(CPPFLAGS)
8039$(CXXFLAGS)}. We encourage you to use the suffix @samp{.cc} for C++
8040source files instead of @samp{.C}.@refill
8041
8042@item Compiling Pascal programs
8043@cindex Pascal, rule to compile
8044@pindex pc
8045@pindex .p
8046@file{@var{n}.o} is made automatically from @file{@var{n}.p}
8047with the command @samp{$(PC) -c $(PFLAGS)}.@refill
8048
8049@item Compiling Fortran and Ratfor programs
8050@cindex Fortran, rule to compile
8051@cindex Ratfor, rule to compile
8052@pindex f77
8053@pindex .f
8054@pindex .r
8055@pindex .F
8056@file{@var{n}.o} is made automatically from @file{@var{n}.r},
8057@file{@var{n}.F} or @file{@var{n}.f} by running the
8058Fortran compiler. The precise command used is as follows:@refill
8059
8060@table @samp
8061@item .f
8062@samp{$(FC) -c $(FFLAGS)}.
8063@item .F
8064@samp{$(FC) -c $(FFLAGS) $(CPPFLAGS)}.
8065@item .r
8066@samp{$(FC) -c $(FFLAGS) $(RFLAGS)}.
8067@end table
8068
8069@item Preprocessing Fortran and Ratfor programs
8070@file{@var{n}.f} is made automatically from @file{@var{n}.r} or
8071@file{@var{n}.F}. This rule runs just the preprocessor to convert a
8072Ratfor or preprocessable Fortran program into a strict Fortran
8073program. The precise command used is as follows:@refill
8074
8075@table @samp
8076@item .F
8077@samp{$(FC) -F $(CPPFLAGS) $(FFLAGS)}.
8078@item .r
8079@samp{$(FC) -F $(FFLAGS) $(RFLAGS)}.
8080@end table
8081
8082@item Compiling Modula-2 programs
8083@cindex Modula-2, rule to compile
8084@pindex m2c
8085@pindex .sym
8086@pindex .def
8087@pindex .mod
8088@file{@var{n}.sym} is made from @file{@var{n}.def} with a command
8089of the form @samp{$(M2C) $(M2FLAGS) $(DEFFLAGS)}. @file{@var{n}.o}
8090is made from @file{@var{n}.mod}; the form is:
8091@w{@samp{$(M2C) $(M2FLAGS) $(MODFLAGS)}}.@refill
8092
8093@need 1200
8094@item Assembling and preprocessing assembler programs
8095@cindex assembly, rule to compile
8096@pindex as
8097@pindex .s
8098@file{@var{n}.o} is made automatically from @file{@var{n}.s} by
8099running the assembler, @code{as}. The precise command is
8100@samp{$(AS) $(ASFLAGS)}.@refill
8101
8102@pindex .S
8103@file{@var{n}.s} is made automatically from @file{@var{n}.S} by
8104running the C preprocessor, @code{cpp}. The precise command is
8105@w{@samp{$(CPP) $(CPPFLAGS)}}.
8106
8107@item Linking a single object file
8108@cindex linking, predefined rule for
8109@pindex ld
8110@pindex .o
8111@file{@var{n}} is made automatically from @file{@var{n}.o} by running
8112the linker (usually called @code{ld}) via the C compiler. The precise
8113command used is @w{@samp{$(CC) $(LDFLAGS) @var{n}.o $(LOADLIBES) $(LDLIBS)}}.
8114
8115This rule does the right thing for a simple program with only one
8116source file. It will also do the right thing if there are multiple
8117object files (presumably coming from various other source files), one
8118of which has a name matching that of the executable file. Thus,
8119
8120@example
8121x: y.o z.o
8122@end example
8123
8124@noindent
8125when @file{x.c}, @file{y.c} and @file{z.c} all exist will execute:
8126
8127@example
8128@group
8129cc -c x.c -o x.o
8130cc -c y.c -o y.o
8131cc -c z.c -o z.o
8132cc x.o y.o z.o -o x
8133rm -f x.o
8134rm -f y.o
8135rm -f z.o
8136@end group
8137@end example
8138
8139@noindent
8140In more complicated cases, such as when there is no object file whose
8141name derives from the executable file name, you must write an explicit
8142command for linking.
8143
8144Each kind of file automatically made into @samp{.o} object files will
8145be automatically linked by using the compiler (@samp{$(CC)},
8146@samp{$(FC)} or @samp{$(PC)}; the C compiler @samp{$(CC)} is used to
8147assemble @samp{.s} files) without the @samp{-c} option. This could be
8148done by using the @samp{.o} object files as intermediates, but it is
8149faster to do the compiling and linking in one step, so that's how it's
8150done.@refill
8151
8152@item Yacc for C programs
8153@pindex yacc
8154@cindex Yacc, rule to run
8155@pindex .y
8156@file{@var{n}.c} is made automatically from @file{@var{n}.y} by
8157running Yacc with the command @samp{$(YACC) $(YFLAGS)}.
8158
8159@item Lex for C programs
8160@pindex lex
8161@cindex Lex, rule to run
8162@pindex .l
8163@file{@var{n}.c} is made automatically from @file{@var{n}.l} by
8164running Lex. The actual command is @samp{$(LEX) $(LFLAGS)}.
8165
8166@item Lex for Ratfor programs
8167@file{@var{n}.r} is made automatically from @file{@var{n}.l} by
8168running Lex. The actual command is @samp{$(LEX) $(LFLAGS)}.
8169
8170The convention of using the same suffix @samp{.l} for all Lex files
8171regardless of whether they produce C code or Ratfor code makes it
8172impossible for @code{make} to determine automatically which of the two
8173languages you are using in any particular case. If @code{make} is
8174called upon to remake an object file from a @samp{.l} file, it must
8175guess which compiler to use. It will guess the C compiler, because
8176that is more common. If you are using Ratfor, make sure @code{make}
8177knows this by mentioning @file{@var{n}.r} in the makefile. Or, if you
8178are using Ratfor exclusively, with no C files, remove @samp{.c} from
8179the list of implicit rule suffixes with:@refill
8180
8181@example
8182@group
8183.SUFFIXES:
8184.SUFFIXES: .o .r .f .l @dots{}
8185@end group
8186@end example
8187
8188@item Making Lint Libraries from C, Yacc, or Lex programs
8189@pindex lint
8190@cindex @code{lint}, rule to run
8191@pindex .ln
8192@file{@var{n}.ln} is made from @file{@var{n}.c} by running @code{lint}.
8193The precise command is @w{@samp{$(LINT) $(LINTFLAGS) $(CPPFLAGS) -i}}.
8194The same command is used on the C code produced from
8195@file{@var{n}.y} or @file{@var{n}.l}.@refill
8196
8197@item @TeX{} and Web
8198@cindex @TeX{}, rule to run
8199@cindex Web, rule to run
8200@pindex tex
8201@pindex cweave
8202@pindex weave
8203@pindex tangle
8204@pindex ctangle
8205@pindex .dvi
8206@pindex .tex
8207@pindex .web
8208@pindex .w
8209@pindex .ch
8210@file{@var{n}.dvi} is made from @file{@var{n}.tex} with the command
8211@samp{$(TEX)}. @file{@var{n}.tex} is made from @file{@var{n}.web} with
8212@samp{$(WEAVE)}, or from @file{@var{n}.w} (and from @file{@var{n}.ch} if
8213it exists or can be made) with @samp{$(CWEAVE)}. @file{@var{n}.p} is
8214made from @file{@var{n}.web} with @samp{$(TANGLE)} and @file{@var{n}.c}
8215is made from @file{@var{n}.w} (and from @file{@var{n}.ch} if it exists
8216or can be made) with @samp{$(CTANGLE)}.@refill
8217
8218@item Texinfo and Info
8219@cindex Texinfo, rule to format
8220@cindex Info, rule to format
8221@pindex texi2dvi
8222@pindex makeinfo
8223@pindex .texinfo
8224@pindex .info
8225@pindex .texi
8226@pindex .txinfo
8227@file{@var{n}.dvi} is made from @file{@var{n}.texinfo},
8228@file{@var{n}.texi}, or @file{@var{n}.txinfo}, with the command
8229@w{@samp{$(TEXI2DVI) $(TEXI2DVI_FLAGS)}}. @file{@var{n}.info} is made from
8230@file{@var{n}.texinfo}, @file{@var{n}.texi}, or @file{@var{n}.txinfo}, with
8231the command @w{@samp{$(MAKEINFO) $(MAKEINFO_FLAGS)}}.
8232
8233@item RCS
8234@cindex RCS, rule to extract from
8235@pindex co
8236@pindex ,v @r{(RCS file extension)}
8237Any file @file{@var{n}} is extracted if necessary from an RCS file
8238named either @file{@var{n},v} or @file{RCS/@var{n},v}. The precise
8239command used is @w{@samp{$(CO) $(COFLAGS)}}. @file{@var{n}} will not be
8240extracted from RCS if it already exists, even if the RCS file is
8241newer. The rules for RCS are terminal
8242(@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}),
8243so RCS files cannot be generated from another source; they must
8244actually exist.@refill
8245
8246@item SCCS
8247@cindex SCCS, rule to extract from
8248@pindex get
8249@pindex s. @r{(SCCS file prefix)}
8250Any file @file{@var{n}} is extracted if necessary from an SCCS file
8251named either @file{s.@var{n}} or @file{SCCS/s.@var{n}}. The precise
8252command used is @w{@samp{$(GET) $(GFLAGS)}}. The rules for SCCS are
8253terminal (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}),
8254so SCCS files cannot be generated from another source; they must
8255actually exist.@refill
8256
8257@pindex .sh
8258For the benefit of SCCS, a file @file{@var{n}} is copied from
8259@file{@var{n}.sh} and made executable (by everyone). This is for
8260shell scripts that are checked into SCCS. Since RCS preserves the
8261execution permission of a file, you do not need to use this feature
8262with RCS.@refill
8263
8264We recommend that you avoid using of SCCS. RCS is widely held to be
8265superior, and is also free. By choosing free software in place of
8266comparable (or inferior) proprietary software, you support the free
8267software movement.
8268@end table
8269
8270Usually, you want to change only the variables listed in the table
8271above, which are documented in the following section.
8272
8273However, the commands in built-in implicit rules actually use
8274variables such as @code{COMPILE.c}, @code{LINK.p}, and
8275@code{PREPROCESS.S}, whose values contain the commands listed above.
8276
8277@code{make} follows the convention that the rule to compile a
8278@file{.@var{x}} source file uses the variable @code{COMPILE.@var{x}}.
8279Similarly, the rule to produce an executable from a @file{.@var{x}}
8280file uses @code{LINK.@var{x}}; and the rule to preprocess a
8281@file{.@var{x}} file uses @code{PREPROCESS.@var{x}}.
8282
8283@vindex OUTPUT_OPTION
8284Every rule that produces an object file uses the variable
8285@code{OUTPUT_OPTION}. @code{make} defines this variable either to
8286contain @samp{-o $@@}, or to be empty, depending on a compile-time
8287option. You need the @samp{-o} option to ensure that the output goes
8288into the right file when the source file is in a different directory,
8289as when using @code{VPATH} (@pxref{Directory Search}). However,
8290compilers on some systems do not accept a @samp{-o} switch for object
8291files. If you use such a system, and use @code{VPATH}, some
8292compilations will put their output in the wrong place.
8293A possible workaround for this problem is to give @code{OUTPUT_OPTION}
8294the value @w{@samp{; mv $*.o $@@}}.
8295
8296@node Implicit Variables, Chained Rules, Catalogue of Rules, Implicit Rules
8297@section Variables Used by Implicit Rules
8298@cindex flags for compilers
8299
8300The commands in built-in implicit rules make liberal use of certain
8301predefined variables. You can alter these variables in the makefile,
8302with arguments to @code{make}, or in the environment to alter how the
8303implicit rules work without redefining the rules themselves. You can
8304cancel all variables used by implicit rules with the @samp{-R} or
8305@samp{--no-builtin-variables} option.
8306
8307For example, the command used to compile a C source file actually says
8308@samp{$(CC) -c $(CFLAGS) $(CPPFLAGS)}. The default values of the variables
8309used are @samp{cc} and nothing, resulting in the command @samp{cc -c}. By
8310redefining @samp{CC} to @samp{ncc}, you could cause @samp{ncc} to be
8311used for all C compilations performed by the implicit rule. By redefining
8312@samp{CFLAGS} to be @samp{-g}, you could pass the @samp{-g} option to
8313each compilation. @emph{All} implicit rules that do C compilation use
8314@samp{$(CC)} to get the program name for the compiler and @emph{all}
8315include @samp{$(CFLAGS)} among the arguments given to the compiler.@refill
8316
8317The variables used in implicit rules fall into two classes: those that are
8318names of programs (like @code{CC}) and those that contain arguments for the
8319programs (like @code{CFLAGS}). (The ``name of a program'' may also contain
8320some command arguments, but it must start with an actual executable program
8321name.) If a variable value contains more than one argument, separate them
8322with spaces.
8323
8324Here is a table of variables used as names of programs in built-in rules:
8325
8326@table @code
8327@item AR
8328@vindex AR
8329Archive-maintaining program; default @samp{ar}.
8330@pindex ar
8331
8332@item AS
8333@vindex AS
8334Program for doing assembly; default @samp{as}.
8335@pindex as
8336
8337@item CC
8338@vindex CC
8339Program for compiling C programs; default @samp{cc}.
8340@pindex cc
8341
8342@item CXX
8343@vindex CXX
8344Program for compiling C++ programs; default @samp{g++}.
8345@pindex g++
8346
8347@item CO
8348@vindex CO
8349Program for extracting a file from RCS; default @samp{co}.
8350@pindex co
8351
8352@item CPP
8353@vindex CPP
8354Program for running the C preprocessor, with results to standard output;
8355default @samp{$(CC) -E}.
8356
8357@item FC
8358@vindex FC
8359Program for compiling or preprocessing Fortran and Ratfor programs;
8360default @samp{f77}.
8361@pindex f77
8362
8363@item GET
8364@vindex GET
8365Program for extracting a file from SCCS; default @samp{get}.
8366@pindex get
8367
8368@item LEX
8369@vindex LEX
8370Program to use to turn Lex grammars into C programs or Ratfor programs;
8371default @samp{lex}.
8372@pindex lex
8373
8374@item PC
8375@vindex PC
8376Program for compiling Pascal programs; default @samp{pc}.
8377@pindex pc
8378
8379@item YACC
8380@vindex YACC
8381Program to use to turn Yacc grammars into C programs; default @samp{yacc}.
8382@pindex yacc
8383
8384@item YACCR
8385@vindex YACCR
8386Program to use to turn Yacc grammars into Ratfor
8387programs; default @samp{yacc -r}.
8388
8389@item MAKEINFO
8390@vindex MAKEINFO
8391Program to convert a Texinfo source file into an Info file; default
8392@samp{makeinfo}.
8393@pindex makeinfo
8394
8395@item TEX
8396@vindex TEX
8397Program to make @TeX{} @sc{dvi} files from @TeX{} source;
8398default @samp{tex}.
8399@pindex tex
8400
8401@item TEXI2DVI
8402@vindex TEXI2DVI
8403Program to make @TeX{} @sc{dvi} files from Texinfo source;
8404default @samp{texi2dvi}.
8405@pindex texi2dvi
8406
8407@item WEAVE
8408@vindex WEAVE
8409Program to translate Web into @TeX{}; default @samp{weave}.
8410@pindex weave
8411
8412@item CWEAVE
8413@vindex CWEAVE
8414Program to translate C Web into @TeX{}; default @samp{cweave}.
8415@pindex cweave
8416
8417@item TANGLE
8418@vindex TANGLE
8419Program to translate Web into Pascal; default @samp{tangle}.
8420@pindex tangle
8421
8422@item CTANGLE
8423@vindex CTANGLE
8424Program to translate C Web into C; default @samp{ctangle}.
8425@pindex ctangle
8426
8427@item RM
8428@vindex RM
8429Command to remove a file; default @samp{rm -f}.
8430@pindex rm
8431@end table
8432
8433Here is a table of variables whose values are additional arguments for the
8434programs above. The default values for all of these is the empty
8435string, unless otherwise noted.
8436
8437@table @code
8438@item ARFLAGS
8439@vindex ARFLAGS
8440Flags to give the archive-maintaining program; default @samp{rv}.
8441
8442@item ASFLAGS
8443@vindex ASFLAGS
8444Extra flags to give to the assembler (when explicitly
8445invoked on a @samp{.s} or @samp{.S} file).
8446
8447@item CFLAGS
8448@vindex CFLAGS
8449Extra flags to give to the C compiler.
8450
8451@item CXXFLAGS
8452@vindex CXXFLAGS
8453Extra flags to give to the C++ compiler.
8454
8455@item COFLAGS
8456@vindex COFLAGS
8457Extra flags to give to the RCS @code{co} program.
8458
8459@item CPPFLAGS
8460@vindex CPPFLAGS
8461Extra flags to give to the C preprocessor and programs
8462that use it (the C and Fortran compilers).
8463
8464@item FFLAGS
8465@vindex FFLAGS
8466Extra flags to give to the Fortran compiler.
8467
8468@item GFLAGS
8469@vindex GFLAGS
8470Extra flags to give to the SCCS @code{get} program.
8471
8472@item LDFLAGS
8473@vindex LDFLAGS
8474Extra flags to give to compilers when they are
8475supposed to invoke the linker, @samp{ld}.
8476
8477@item LFLAGS
8478@vindex LFLAGS
8479Extra flags to give to Lex.
8480
8481@item PFLAGS
8482@vindex PFLAGS
8483Extra flags to give to the Pascal compiler.
8484
8485@item RFLAGS
8486@vindex RFLAGS
8487Extra flags to give to the Fortran compiler for Ratfor programs.
8488
8489@item YFLAGS
8490@vindex YFLAGS
8491Extra flags to give to Yacc.
8492@end table
8493
8494@node Chained Rules, Pattern Rules, Implicit Variables, Implicit Rules
8495@section Chains of Implicit Rules
8496
8497@cindex chains of rules
8498@cindex rule, implicit, chains of
8499Sometimes a file can be made by a sequence of implicit rules. For example,
8500a file @file{@var{n}.o} could be made from @file{@var{n}.y} by running
8501first Yacc and then @code{cc}. Such a sequence is called a @dfn{chain}.
8502
8503If the file @file{@var{n}.c} exists, or is mentioned in the makefile, no
8504special searching is required: @code{make} finds that the object file can
8505be made by C compilation from @file{@var{n}.c}; later on, when considering
8506how to make @file{@var{n}.c}, the rule for running Yacc is
8507used. Ultimately both @file{@var{n}.c} and @file{@var{n}.o} are
8508updated.@refill
8509
8510@cindex intermediate files
8511@cindex files, intermediate
8512However, even if @file{@var{n}.c} does not exist and is not mentioned,
8513@code{make} knows how to envision it as the missing link between
8514@file{@var{n}.o} and @file{@var{n}.y}! In this case, @file{@var{n}.c} is
8515called an @dfn{intermediate file}. Once @code{make} has decided to use the
8516intermediate file, it is entered in the data base as if it had been
8517mentioned in the makefile, along with the implicit rule that says how to
8518create it.@refill
8519
8520Intermediate files are remade using their rules just like all other
8521files. But intermediate files are treated differently in two ways.
8522
8523The first difference is what happens if the intermediate file does not
8524exist. If an ordinary file @var{b} does not exist, and @code{make}
8525considers a target that depends on @var{b}, it invariably creates
8526@var{b} and then updates the target from @var{b}. But if @var{b} is an
8527intermediate file, then @code{make} can leave well enough alone. It
8528won't bother updating @var{b}, or the ultimate target, unless some
8529prerequisite of @var{b} is newer than that target or there is some other
8530reason to update that target.
8531
8532The second difference is that if @code{make} @emph{does} create @var{b}
8533in order to update something else, it deletes @var{b} later on after it
8534is no longer needed. Therefore, an intermediate file which did not
8535exist before @code{make} also does not exist after @code{make}.
8536@code{make} reports the deletion to you by printing a @samp{rm -f}
8537command showing which file it is deleting.
8538
8539Ordinarily, a file cannot be intermediate if it is mentioned in the
8540makefile as a target or prerequisite. However, you can explicitly mark a
8541file as intermediate by listing it as a prerequisite of the special target
8542@code{.INTERMEDIATE}. This takes effect even if the file is mentioned
8543explicitly in some other way.
8544
8545@cindex intermediate files, preserving
8546@cindex preserving intermediate files
8547@cindex secondary files
8548You can prevent automatic deletion of an intermediate file by marking it
8549as a @dfn{secondary} file. To do this, list it as a prerequisite of the
8550special target @code{.SECONDARY}. When a file is secondary, @code{make}
8551will not create the file merely because it does not already exist, but
8552@code{make} does not automatically delete the file. Marking a file as
8553secondary also marks it as intermediate.
8554
8555You can list the target pattern of an implicit rule (such as @samp{%.o})
8556as a prerequisite of the special target @code{.PRECIOUS} to preserve
8557intermediate files made by implicit rules whose target patterns match
8558that file's name; see @ref{Interrupts}.@refill
8559@cindex preserving with @code{.PRECIOUS}
8560@cindex @code{.PRECIOUS} intermediate files
8561
8562A chain can involve more than two implicit rules. For example, it is
8563possible to make a file @file{foo} from @file{RCS/foo.y,v} by running RCS,
8564Yacc and @code{cc}. Then both @file{foo.y} and @file{foo.c} are
8565intermediate files that are deleted at the end.@refill
8566
8567No single implicit rule can appear more than once in a chain. This means
8568that @code{make} will not even consider such a ridiculous thing as making
8569@file{foo} from @file{foo.o.o} by running the linker twice. This
8570constraint has the added benefit of preventing any infinite loop in the
8571search for an implicit rule chain.
8572
8573There are some special implicit rules to optimize certain cases that would
8574otherwise be handled by rule chains. For example, making @file{foo} from
8575@file{foo.c} could be handled by compiling and linking with separate
8576chained rules, using @file{foo.o} as an intermediate file. But what
8577actually happens is that a special rule for this case does the compilation
8578and linking with a single @code{cc} command. The optimized rule is used in
8579preference to the step-by-step chain because it comes earlier in the
8580ordering of rules.
8581
8582@node Pattern Rules, Last Resort, Chained Rules, Implicit Rules
8583@section Defining and Redefining Pattern Rules
8584
8585You define an implicit rule by writing a @dfn{pattern rule}. A pattern
8586rule looks like an ordinary rule, except that its target contains the
8587character @samp{%} (exactly one of them). The target is considered a
8588pattern for matching file names; the @samp{%} can match any nonempty
8589substring, while other characters match only themselves. The prerequisites
8590likewise use @samp{%} to show how their names relate to the target name.
8591
8592Thus, a pattern rule @samp{%.o : %.c} says how to make any file
8593@file{@var{stem}.o} from another file @file{@var{stem}.c}.@refill
8594
8595Note that expansion using @samp{%} in pattern rules occurs
8596@strong{after} any variable or function expansions, which take place
8597when the makefile is read. @xref{Using Variables, , How to Use
8598Variables}, and @ref{Functions, ,Functions for Transforming Text}.
8599
8600@menu
8601* Pattern Intro:: An introduction to pattern rules.
8602* Pattern Examples:: Examples of pattern rules.
8603* Automatic Variables:: How to use automatic variables in the
8604 commands of implicit rules.
8605* Pattern Match:: How patterns match.
8606* Match-Anything Rules:: Precautions you should take prior to
8607 defining rules that can match any
8608 target file whatever.
8609* Canceling Rules:: How to override or cancel built-in rules.
8610@end menu
8611
8612@node Pattern Intro, Pattern Examples, Pattern Rules, Pattern Rules
8613@subsection Introduction to Pattern Rules
8614@cindex pattern rule
8615@cindex rule, pattern
8616
8617A pattern rule contains the character @samp{%} (exactly one of them)
8618in the target; otherwise, it looks exactly like an ordinary rule. The
8619target is a pattern for matching file names; the @samp{%} matches any
8620nonempty substring, while other characters match only themselves.
8621@cindex target pattern, implicit
8622@cindex @code{%}, in pattern rules
8623
8624For example, @samp{%.c} as a pattern matches any file name that ends in
8625@samp{.c}. @samp{s.%.c} as a pattern matches any file name that starts
8626with @samp{s.}, ends in @samp{.c} and is at least five characters long.
8627(There must be at least one character to match the @samp{%}.) The substring
8628that the @samp{%} matches is called the @dfn{stem}.@refill
8629
8630@samp{%} in a prerequisite of a pattern rule stands for the same stem
8631that was matched by the @samp{%} in the target. In order for
8632the pattern rule to apply, its target pattern must match the file name
8633under consideration, and its prerequisite patterns must name files that
8634exist or can be made. These files become prerequisites of the target.
8635@cindex prerequisite pattern, implicit
8636
8637Thus, a rule of the form
8638
8639@example
8640%.o : %.c ; @var{command}@dots{}
8641@end example
8642
8643@noindent
8644specifies how to make a file @file{@var{n}.o}, with another file
8645@file{@var{n}.c} as its prerequisite, provided that @file{@var{n}.c}
8646exists or can be made.
8647
8648There may also be prerequisites that do not use @samp{%}; such a prerequisite
8649attaches to every file made by this pattern rule. These unvarying
8650prerequisites are useful occasionally.
8651
8652A pattern rule need not have any prerequisites that contain @samp{%}, or
8653in fact any prerequisites at all. Such a rule is effectively a general
8654wildcard. It provides a way to make any file that matches the target
8655pattern. @xref{Last Resort}.
8656
8657@c !!! The end of of this paragraph should be rewritten. --bob
8658Pattern rules may have more than one target. Unlike normal rules, this
8659does not act as many different rules with the same prerequisites and
8660commands. If a pattern rule has multiple targets, @code{make} knows that
8661the rule's commands are responsible for making all of the targets. The
8662commands are executed only once to make all the targets. When searching
8663for a pattern rule to match a target, the target patterns of a rule other
8664than the one that matches the target in need of a rule are incidental:
8665@code{make} worries only about giving commands and prerequisites to the file
8666presently in question. However, when this file's commands are run, the
8667other targets are marked as having been updated themselves.
8668@cindex multiple targets, in pattern rule
8669@cindex target, multiple in pattern rule
8670
8671The order in which pattern rules appear in the makefile is important
8672since this is the order in which they are considered.
8673Of equally applicable
8674rules, only the first one found is used. The rules you write take precedence
8675over those that are built in. Note however, that a rule whose
8676prerequisites actually exist or are mentioned always takes priority over a
8677rule with prerequisites that must be made by chaining other implicit rules.
8678@cindex pattern rules, order of
8679@cindex order of pattern rules
8680
8681@node Pattern Examples, Automatic Variables, Pattern Intro, Pattern Rules
8682@subsection Pattern Rule Examples
8683
8684Here are some examples of pattern rules actually predefined in
8685@code{make}. First, the rule that compiles @samp{.c} files into @samp{.o}
8686files:@refill
8687
8688@example
8689%.o : %.c
8690 $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@@
8691@end example
8692
8693@noindent
8694defines a rule that can make any file @file{@var{x}.o} from
8695@file{@var{x}.c}. The command uses the automatic variables @samp{$@@} and
8696@samp{$<} to substitute the names of the target file and the source file
8697in each case where the rule applies (@pxref{Automatic Variables}).@refill
8698
8699Here is a second built-in rule:
8700
8701@example
8702% :: RCS/%,v
8703 $(CO) $(COFLAGS) $<
8704@end example
8705
8706@noindent
8707defines a rule that can make any file @file{@var{x}} whatsoever from a
8708corresponding file @file{@var{x},v} in the subdirectory @file{RCS}. Since
8709the target is @samp{%}, this rule will apply to any file whatever, provided
8710the appropriate prerequisite file exists. The double colon makes the rule
8711@dfn{terminal}, which means that its prerequisite may not be an intermediate
8712file (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}).@refill
8713
8714@need 500
8715This pattern rule has two targets:
8716
8717@example
8718@group
8719%.tab.c %.tab.h: %.y
8720 bison -d $<
8721@end group
8722@end example
8723
8724@noindent
8725@c The following paragraph is rewritten to avoid overfull hboxes
8726This tells @code{make} that the command @samp{bison -d @var{x}.y} will
8727make both @file{@var{x}.tab.c} and @file{@var{x}.tab.h}. If the file
8728@file{foo} depends on the files @file{parse.tab.o} and @file{scan.o}
8729and the file @file{scan.o} depends on the file @file{parse.tab.h},
8730when @file{parse.y} is changed, the command @samp{bison -d parse.y}
8731will be executed only once, and the prerequisites of both
8732@file{parse.tab.o} and @file{scan.o} will be satisfied. (Presumably
8733the file @file{parse.tab.o} will be recompiled from @file{parse.tab.c}
8734and the file @file{scan.o} from @file{scan.c}, while @file{foo} is
8735linked from @file{parse.tab.o}, @file{scan.o}, and its other
8736prerequisites, and it will execute happily ever after.)@refill
8737
8738@node Automatic Variables, Pattern Match, Pattern Examples, Pattern Rules
8739@subsection Automatic Variables
8740@cindex automatic variables
8741@cindex variables, automatic
8742@cindex variables, and implicit rule
8743
8744Suppose you are writing a pattern rule to compile a @samp{.c} file into a
8745@samp{.o} file: how do you write the @samp{cc} command so that it operates
8746on the right source file name? You cannot write the name in the command,
8747because the name is different each time the implicit rule is applied.
8748
8749What you do is use a special feature of @code{make}, the @dfn{automatic
8750variables}. These variables have values computed afresh for each rule that
8751is executed, based on the target and prerequisites of the rule. In this
8752example, you would use @samp{$@@} for the object file name and @samp{$<}
8753for the source file name.
8754
8755@cindex automatic variables in prerequisites
8756@cindex prerequisites, and automatic variables
8757It's very important that you recognize the limited scope in which
8758automatic variable values are available: they only have values within
8759the command script. In particular, you cannot use them anywhere
8760within the target list of a rule; they have no value there and will
8761expand to the empty string. Also, they cannot be accessed directly
8762within the prerequisite list of a rule. A common mistake is
8763attempting to use @code{$@@} within the prerequisites list; this will
8764not work. However, there is a special feature of GNU @code{make},
8765secondary expansion (@pxref{Secondary Expansion}), which will allow
8766automatic variable values to be used in prerequisite lists.
8767
8768Here is a table of automatic variables:
8769
8770@table @code
8771@vindex $@@
8772@vindex @@ @r{(automatic variable)}
8773@item $@@
8774The file name of the target of the rule. If the target is an archive
8775member, then @samp{$@@} is the name of the archive file. In a pattern
8776rule that has multiple targets (@pxref{Pattern Intro, ,Introduction to
8777Pattern Rules}), @samp{$@@} is the name of whichever target caused the
8778rule's commands to be run.
8779
8780@vindex $%
8781@vindex % @r{(automatic variable)}
8782@item $%
8783The target member name, when the target is an archive member.
8784@xref{Archives}. For example, if the target is @file{foo.a(bar.o)} then
8785@samp{$%} is @file{bar.o} and @samp{$@@} is @file{foo.a}. @samp{$%} is
8786empty when the target is not an archive member.
8787
8788@vindex $<
8789@vindex < @r{(automatic variable)}
8790@item $<
8791The name of the first prerequisite. If the target got its commands from
8792an implicit rule, this will be the first prerequisite added by the
8793implicit rule (@pxref{Implicit Rules}).
8794
8795@vindex $?
8796@vindex ? @r{(automatic variable)}
8797@item $?
8798The names of all the prerequisites that are newer than the target, with
8799spaces between them. For prerequisites which are archive members, only
8800the member named is used (@pxref{Archives}).
8801@cindex prerequisites, list of changed
8802@cindex list of changed prerequisites
8803
8804@vindex $^
8805@vindex ^ @r{(automatic variable)}
8806@item $^
8807The names of all the prerequisites, with spaces between them. For
8808prerequisites which are archive members, only the member named is used
8809(@pxref{Archives}). A target has only one prerequisite on each other file
8810it depends on, no matter how many times each file is listed as a
8811prerequisite. So if you list a prerequisite more than once for a target,
8812the value of @code{$^} contains just one copy of the name. This list
8813does @strong{not} contain any of the order-only prerequisites; for those
8814see the @samp{$|} variable, below.
8815@cindex prerequisites, list of all
8816@cindex list of all prerequisites
8817
8818@vindex $+
8819@vindex + @r{(automatic variable)}
8820@item $+
8821This is like @samp{$^}, but prerequisites listed more than once are
8822duplicated in the order they were listed in the makefile. This is
8823primarily useful for use in linking commands where it is meaningful to
8824repeat library file names in a particular order.
8825
8826@vindex $|
8827@vindex | @r{(automatic variable)}
8828@item $|
8829The names of all the order-only prerequisites, with spaces between
8830them.
8831
8832@vindex $*
8833@vindex * @r{(automatic variable)}
8834@item $*
8835The stem with which an implicit rule matches (@pxref{Pattern Match, ,How
8836Patterns Match}). If the target is @file{dir/a.foo.b} and the target
8837pattern is @file{a.%.b} then the stem is @file{dir/foo}. The stem is
8838useful for constructing names of related files.@refill
8839@cindex stem, variable for
8840
8841In a static pattern rule, the stem is part of the file name that matched
8842the @samp{%} in the target pattern.
8843
8844In an explicit rule, there is no stem; so @samp{$*} cannot be determined
8845in that way. Instead, if the target name ends with a recognized suffix
8846(@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}), @samp{$*} is set to
8847the target name minus the suffix. For example, if the target name is
8848@samp{foo.c}, then @samp{$*} is set to @samp{foo}, since @samp{.c} is a
8849suffix. GNU @code{make} does this bizarre thing only for compatibility
8850with other implementations of @code{make}. You should generally avoid
8851using @samp{$*} except in implicit rules or static pattern rules.@refill
8852
8853If the target name in an explicit rule does not end with a recognized
8854suffix, @samp{$*} is set to the empty string for that rule.
8855@end table
8856
8857@samp{$?} is useful even in explicit rules when you wish to operate on only
8858the prerequisites that have changed. For example, suppose that an archive
8859named @file{lib} is supposed to contain copies of several object files.
8860This rule copies just the changed object files into the archive:
8861
8862@example
8863@group
8864lib: foo.o bar.o lose.o win.o
8865 ar r lib $?
8866@end group
8867@end example
8868
8869Of the variables listed above, four have values that are single file
8870names, and three have values that are lists of file names. These seven
8871have variants that get just the file's directory name or just the file
8872name within the directory. The variant variables' names are formed by
8873appending @samp{D} or @samp{F}, respectively. These variants are
8874semi-obsolete in GNU @code{make} since the functions @code{dir} and
8875@code{notdir} can be used to get a similar effect (@pxref{File Name
8876Functions, , Functions for File Names}). Note, however, that the
8877@samp{D} variants all omit the trailing slash which always appears in
8878the output of the @code{dir} function. Here is a table of the variants:
8879
8880@table @samp
8881@vindex $(@@D)
8882@vindex @@D @r{(automatic variable)}
8883@item $(@@D)
8884The directory part of the file name of the target, with the trailing
8885slash removed. If the value of @samp{$@@} is @file{dir/foo.o} then
8886@samp{$(@@D)} is @file{dir}. This value is @file{.} if @samp{$@@} does
8887not contain a slash.
8888
8889@vindex $(@@F)
8890@vindex @@F @r{(automatic variable)}
8891@item $(@@F)
8892The file-within-directory part of the file name of the target. If the
8893value of @samp{$@@} is @file{dir/foo.o} then @samp{$(@@F)} is
8894@file{foo.o}. @samp{$(@@F)} is equivalent to @samp{$(notdir $@@)}.
8895
8896@vindex $(*D)
8897@vindex *D @r{(automatic variable)}
8898@item $(*D)
8899@vindex $(*F)
8900@vindex *F @r{(automatic variable)}
8901@itemx $(*F)
8902The directory part and the file-within-directory
8903part of the stem; @file{dir} and @file{foo} in this example.
8904
8905@vindex $(%D)
8906@vindex %D @r{(automatic variable)}
8907@item $(%D)
8908@vindex $(%F)
8909@vindex %F @r{(automatic variable)}
8910@itemx $(%F)
8911The directory part and the file-within-directory part of the target
8912archive member name. This makes sense only for archive member targets
8913of the form @file{@var{archive}(@var{member})} and is useful only when
8914@var{member} may contain a directory name. (@xref{Archive Members,
8915,Archive Members as Targets}.)
8916
8917@vindex $(<D)
8918@vindex <D @r{(automatic variable)}
8919@item $(<D)
8920@vindex $(<F)
8921@vindex <F @r{(automatic variable)}
8922@itemx $(<F)
8923The directory part and the file-within-directory
8924part of the first prerequisite.
8925
8926@vindex $(^D)
8927@vindex ^D @r{(automatic variable)}
8928@item $(^D)
8929@vindex $(^F)
8930@vindex ^F @r{(automatic variable)}
8931@itemx $(^F)
8932Lists of the directory parts and the file-within-directory
8933parts of all prerequisites.
8934
8935@vindex $(+D)
8936@vindex +D @r{(automatic variable)}
8937@item $(+D)
8938@vindex $(+F)
8939@vindex +F @r{(automatic variable)}
8940@itemx $(+F)
8941Lists of the directory parts and the file-within-directory
8942parts of all prerequisites, including multiple instances of duplicated
8943prerequisites.
8944
8945@vindex $(?D)
8946@vindex ?D @r{(automatic variable)}
8947@item $(?D)
8948@vindex $(?F)
8949@vindex ?F @r{(automatic variable)}
8950@itemx $(?F)
8951Lists of the directory parts and the file-within-directory parts of
8952all prerequisites that are newer than the target.
8953@end table
8954
8955Note that we use a special stylistic convention when we talk about these
8956automatic variables; we write ``the value of @samp{$<}'', rather than
8957@w{``the variable @code{<}''} as we would write for ordinary variables
8958such as @code{objects} and @code{CFLAGS}. We think this convention
8959looks more natural in this special case. Please do not assume it has a
8960deep significance; @samp{$<} refers to the variable named @code{<} just
8961as @samp{$(CFLAGS)} refers to the variable named @code{CFLAGS}.
8962You could just as well use @samp{$(<)} in place of @samp{$<}.
8963
8964@node Pattern Match, Match-Anything Rules, Automatic Variables, Pattern Rules
8965@subsection How Patterns Match
8966
8967@cindex stem
8968A target pattern is composed of a @samp{%} between a prefix and a suffix,
8969either or both of which may be empty. The pattern matches a file name only
8970if the file name starts with the prefix and ends with the suffix, without
8971overlap. The text between the prefix and the suffix is called the
8972@dfn{stem}. Thus, when the pattern @samp{%.o} matches the file name
8973@file{test.o}, the stem is @samp{test}. The pattern rule prerequisites are
8974turned into actual file names by substituting the stem for the character
8975@samp{%}. Thus, if in the same example one of the prerequisites is written
8976as @samp{%.c}, it expands to @samp{test.c}.@refill
8977
8978When the target pattern does not contain a slash (and it usually does
8979not), directory names in the file names are removed from the file name
8980before it is compared with the target prefix and suffix. After the
8981comparison of the file name to the target pattern, the directory
8982names, along with the slash that ends them, are added on to the
8983prerequisite file names generated from the pattern rule's prerequisite
8984patterns and the file name. The directories are ignored only for the
8985purpose of finding an implicit rule to use, not in the application of
8986that rule. Thus, @samp{e%t} matches the file name @file{src/eat},
8987with @samp{src/a} as the stem. When prerequisites are turned into file
8988names, the directories from the stem are added at the front, while the
8989rest of the stem is substituted for the @samp{%}. The stem
8990@samp{src/a} with a prerequisite pattern @samp{c%r} gives the file name
8991@file{src/car}.@refill
8992
8993@node Match-Anything Rules, Canceling Rules, Pattern Match, Pattern Rules
8994@subsection Match-Anything Pattern Rules
8995
8996@cindex match-anything rule
8997@cindex terminal rule
8998When a pattern rule's target is just @samp{%}, it matches any file name
8999whatever. We call these rules @dfn{match-anything} rules. They are very
9000useful, but it can take a lot of time for @code{make} to think about them,
9001because it must consider every such rule for each file name listed either
9002as a target or as a prerequisite.
9003
9004Suppose the makefile mentions @file{foo.c}. For this target, @code{make}
9005would have to consider making it by linking an object file @file{foo.c.o},
9006or by C compilation-and-linking in one step from @file{foo.c.c}, or by
9007Pascal compilation-and-linking from @file{foo.c.p}, and many other
9008possibilities.
9009
9010We know these possibilities are ridiculous since @file{foo.c} is a C source
9011file, not an executable. If @code{make} did consider these possibilities,
9012it would ultimately reject them, because files such as @file{foo.c.o} and
9013@file{foo.c.p} would not exist. But these possibilities are so
9014numerous that @code{make} would run very slowly if it had to consider
9015them.@refill
9016
9017To gain speed, we have put various constraints on the way @code{make}
9018considers match-anything rules. There are two different constraints that
9019can be applied, and each time you define a match-anything rule you must
9020choose one or the other for that rule.
9021
9022One choice is to mark the match-anything rule as @dfn{terminal} by defining
9023it with a double colon. When a rule is terminal, it does not apply unless
9024its prerequisites actually exist. Prerequisites that could be made with
9025other implicit rules are not good enough. In other words, no further
9026chaining is allowed beyond a terminal rule.
9027
9028For example, the built-in implicit rules for extracting sources from RCS
9029and SCCS files are terminal; as a result, if the file @file{foo.c,v} does
9030not exist, @code{make} will not even consider trying to make it as an
9031intermediate file from @file{foo.c,v.o} or from @file{RCS/SCCS/s.foo.c,v}.
9032RCS and SCCS files are generally ultimate source files, which should not be
9033remade from any other files; therefore, @code{make} can save time by not
9034looking for ways to remake them.@refill
9035
9036If you do not mark the match-anything rule as terminal, then it is
9037nonterminal. A nonterminal match-anything rule cannot apply to a file name
9038that indicates a specific type of data. A file name indicates a specific
9039type of data if some non-match-anything implicit rule target matches it.
9040
9041For example, the file name @file{foo.c} matches the target for the pattern
9042rule @samp{%.c : %.y} (the rule to run Yacc). Regardless of whether this
9043rule is actually applicable (which happens only if there is a file
9044@file{foo.y}), the fact that its target matches is enough to prevent
9045consideration of any nonterminal match-anything rules for the file
9046@file{foo.c}. Thus, @code{make} will not even consider trying to make
9047@file{foo.c} as an executable file from @file{foo.c.o}, @file{foo.c.c},
9048@file{foo.c.p}, etc.@refill
9049
9050The motivation for this constraint is that nonterminal match-anything
9051rules are used for making files containing specific types of data (such as
9052executable files) and a file name with a recognized suffix indicates some
9053other specific type of data (such as a C source file).
9054
9055Special built-in dummy pattern rules are provided solely to recognize
9056certain file names so that nonterminal match-anything rules will not be
9057considered. These dummy rules have no prerequisites and no commands, and
9058they are ignored for all other purposes. For example, the built-in
9059implicit rule
9060
9061@example
9062%.p :
9063@end example
9064
9065@noindent
9066exists to make sure that Pascal source files such as @file{foo.p} match a
9067specific target pattern and thereby prevent time from being wasted looking
9068for @file{foo.p.o} or @file{foo.p.c}.
9069
9070Dummy pattern rules such as the one for @samp{%.p} are made for every
9071suffix listed as valid for use in suffix rules (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}).
9072
9073@node Canceling Rules, , Match-Anything Rules, Pattern Rules
9074@subsection Canceling Implicit Rules
9075
9076You can override a built-in implicit rule (or one you have defined
9077yourself) by defining a new pattern rule with the same target and
9078prerequisites, but different commands. When the new rule is defined, the
9079built-in one is replaced. The new rule's position in the sequence of
9080implicit rules is determined by where you write the new rule.
9081
9082You can cancel a built-in implicit rule by defining a pattern rule with the
9083same target and prerequisites, but no commands. For example, the following
9084would cancel the rule that runs the assembler:
9085
9086@example
9087%.o : %.s
9088@end example
9089
9090@node Last Resort, Suffix Rules, Pattern Rules, Implicit Rules
9091@section Defining Last-Resort Default Rules
9092@cindex last-resort default rules
9093@cindex default rules, last-resort
9094
9095You can define a last-resort implicit rule by writing a terminal
9096match-anything pattern rule with no prerequisites (@pxref{Match-Anything
9097Rules}). This is just like any other pattern rule; the only thing
9098special about it is that it will match any target. So such a rule's
9099commands are used for all targets and prerequisites that have no commands
9100of their own and for which no other implicit rule applies.
9101
9102For example, when testing a makefile, you might not care if the source
9103files contain real data, only that they exist. Then you might do this:
9104
9105@example
9106%::
9107 touch $@@
9108@end example
9109
9110@noindent
9111to cause all the source files needed (as prerequisites) to be created
9112automatically.
9113
9114@findex .DEFAULT
9115You can instead define commands to be used for targets for which there
9116are no rules at all, even ones which don't specify commands. You do
9117this by writing a rule for the target @code{.DEFAULT}. Such a rule's
9118commands are used for all prerequisites which do not appear as targets in
9119any explicit rule, and for which no implicit rule applies. Naturally,
9120there is no @code{.DEFAULT} rule unless you write one.
9121
9122If you use @code{.DEFAULT} with no commands or prerequisites:
9123
9124@example
9125.DEFAULT:
9126@end example
9127
9128@noindent
9129the commands previously stored for @code{.DEFAULT} are cleared.
9130Then @code{make} acts as if you had never defined @code{.DEFAULT} at all.
9131
9132If you do not want a target to get the commands from a match-anything
9133pattern rule or @code{.DEFAULT}, but you also do not want any commands
9134to be run for the target, you can give it empty commands (@pxref{Empty
9135Commands, ,Defining Empty Commands}).@refill
9136
9137You can use a last-resort rule to override part of another makefile.
9138@xref{Overriding Makefiles, , Overriding Part of Another Makefile}.
9139
9140@node Suffix Rules, Implicit Rule Search, Last Resort, Implicit Rules
9141@section Old-Fashioned Suffix Rules
9142@cindex old-fashioned suffix rules
9143@cindex suffix rule
9144
9145@dfn{Suffix rules} are the old-fashioned way of defining implicit rules for
9146@code{make}. Suffix rules are obsolete because pattern rules are more
9147general and clearer. They are supported in GNU @code{make} for
9148compatibility with old makefiles. They come in two kinds:
9149@dfn{double-suffix} and @dfn{single-suffix}.@refill
9150
9151A double-suffix rule is defined by a pair of suffixes: the target suffix
9152and the source suffix. It matches any file whose name ends with the
9153target suffix. The corresponding implicit prerequisite is made by
9154replacing the target suffix with the source suffix in the file name. A
9155two-suffix rule whose target and source suffixes are @samp{.o} and
9156@samp{.c} is equivalent to the pattern rule @samp{%.o : %.c}.
9157
9158A single-suffix rule is defined by a single suffix, which is the source
9159suffix. It matches any file name, and the corresponding implicit
9160prerequisite name is made by appending the source suffix. A single-suffix
9161rule whose source suffix is @samp{.c} is equivalent to the pattern rule
9162@samp{% : %.c}.
9163
9164Suffix rule definitions are recognized by comparing each rule's target
9165against a defined list of known suffixes. When @code{make} sees a rule
9166whose target is a known suffix, this rule is considered a single-suffix
9167rule. When @code{make} sees a rule whose target is two known suffixes
9168concatenated, this rule is taken as a double-suffix rule.
9169
9170For example, @samp{.c} and @samp{.o} are both on the default list of
9171known suffixes. Therefore, if you define a rule whose target is
9172@samp{.c.o}, @code{make} takes it to be a double-suffix rule with source
9173suffix @samp{.c} and target suffix @samp{.o}. Here is the old-fashioned
9174way to define the rule for compiling a C source file:@refill
9175
9176@example
9177.c.o:
9178 $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
9179@end example
9180
9181Suffix rules cannot have any prerequisites of their own. If they have any,
9182they are treated as normal files with funny names, not as suffix rules.
9183Thus, the rule:
9184
9185@example
9186.c.o: foo.h
9187 $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
9188@end example
9189
9190@noindent
9191tells how to make the file @file{.c.o} from the prerequisite file
9192@file{foo.h}, and is not at all like the pattern rule:
9193
9194@example
9195%.o: %.c foo.h
9196 $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
9197@end example
9198
9199@noindent
9200which tells how to make @samp{.o} files from @samp{.c} files, and makes all
9201@samp{.o} files using this pattern rule also depend on @file{foo.h}.
9202
9203Suffix rules with no commands are also meaningless. They do not remove
9204previous rules as do pattern rules with no commands (@pxref{Canceling
9205Rules, , Canceling Implicit Rules}). They simply enter the suffix or pair of suffixes concatenated as
9206a target in the data base.@refill
9207
9208@findex .SUFFIXES
9209The known suffixes are simply the names of the prerequisites of the special
9210target @code{.SUFFIXES}. You can add your own suffixes by writing a rule
9211for @code{.SUFFIXES} that adds more prerequisites, as in:
9212
9213@example
9214.SUFFIXES: .hack .win
9215@end example
9216
9217@noindent
9218which adds @samp{.hack} and @samp{.win} to the end of the list of suffixes.
9219
9220If you wish to eliminate the default known suffixes instead of just adding
9221to them, write a rule for @code{.SUFFIXES} with no prerequisites. By
9222special dispensation, this eliminates all existing prerequisites of
9223@code{.SUFFIXES}. You can then write another rule to add the suffixes you
9224want. For example,
9225
9226@example
9227@group
9228.SUFFIXES: # @r{Delete the default suffixes}
9229.SUFFIXES: .c .o .h # @r{Define our suffix list}
9230@end group
9231@end example
9232
9233The @samp{-r} or @samp{--no-builtin-rules} flag causes the default
9234list of suffixes to be empty.
9235
9236@vindex SUFFIXES
9237The variable @code{SUFFIXES} is defined to the default list of suffixes
9238before @code{make} reads any makefiles. You can change the list of suffixes
9239with a rule for the special target @code{.SUFFIXES}, but that does not alter
9240this variable.
9241
9242@node Implicit Rule Search, , Suffix Rules, Implicit Rules
9243@section Implicit Rule Search Algorithm
9244@cindex implicit rule, search algorithm
9245@cindex search algorithm, implicit rule
9246
9247Here is the procedure @code{make} uses for searching for an implicit rule
9248for a target @var{t}. This procedure is followed for each double-colon
9249rule with no commands, for each target of ordinary rules none of which have
9250commands, and for each prerequisite that is not the target of any rule. It
9251is also followed recursively for prerequisites that come from implicit
9252rules, in the search for a chain of rules.
9253
9254Suffix rules are not mentioned in this algorithm because suffix rules are
9255converted to equivalent pattern rules once the makefiles have been read in.
9256
9257For an archive member target of the form
9258@samp{@var{archive}(@var{member})}, the following algorithm is run
9259twice, first using the entire target name @var{t}, and second using
9260@samp{(@var{member})} as the target @var{t} if the first run found no
9261rule.@refill
9262
9263@enumerate
9264@item
9265Split @var{t} into a directory part, called @var{d}, and the rest,
9266called @var{n}. For example, if @var{t} is @samp{src/foo.o}, then
9267@var{d} is @samp{src/} and @var{n} is @samp{foo.o}.@refill
9268
9269@item
9270Make a list of all the pattern rules one of whose targets matches
9271@var{t} or @var{n}. If the target pattern contains a slash, it is
9272matched against @var{t}; otherwise, against @var{n}.
9273
9274@item
9275If any rule in that list is @emph{not} a match-anything rule, then
9276remove all nonterminal match-anything rules from the list.
9277
9278@item
9279Remove from the list all rules with no commands.
9280
9281@item
9282For each pattern rule in the list:
9283
9284@enumerate a
9285@item
9286Find the stem @var{s}, which is the nonempty part of @var{t} or @var{n}
9287matched by the @samp{%} in the target pattern.@refill
9288
9289@item
9290Compute the prerequisite names by substituting @var{s} for @samp{%}; if
9291the target pattern does not contain a slash, append @var{d} to
9292the front of each prerequisite name.@refill
9293
9294@item
9295Test whether all the prerequisites exist or ought to exist. (If a
9296file name is mentioned in the makefile as a target or as an explicit
9297prerequisite, then we say it ought to exist.)
9298
9299If all prerequisites exist or ought to exist, or there are no prerequisites,
9300then this rule applies.
9301@end enumerate
9302
9303@item
9304If no pattern rule has been found so far, try harder.
9305For each pattern rule in the list:
9306
9307@enumerate a
9308@item
9309If the rule is terminal, ignore it and go on to the next rule.
9310
9311@item
9312Compute the prerequisite names as before.
9313
9314@item
9315Test whether all the prerequisites exist or ought to exist.
9316
9317@item
9318For each prerequisite that does not exist, follow this algorithm
9319recursively to see if the prerequisite can be made by an implicit
9320rule.
9321
9322@item
9323If all prerequisites exist, ought to exist, or can be
9324made by implicit rules, then this rule applies.
9325@end enumerate
9326
9327@item
9328If no implicit rule applies, the rule for @code{.DEFAULT}, if any,
9329applies. In that case, give @var{t} the same commands that
9330@code{.DEFAULT} has. Otherwise, there are no commands for @var{t}.
9331@end enumerate
9332
9333Once a rule that applies has been found, for each target pattern of the
9334rule other than the one that matched @var{t} or @var{n}, the @samp{%} in
9335the pattern is replaced with @var{s} and the resultant file name is stored
9336until the commands to remake the target file @var{t} are executed. After
9337these commands are executed, each of these stored file names are entered
9338into the data base and marked as having been updated and having the same
9339update status as the file @var{t}.
9340
9341When the commands of a pattern rule are executed for @var{t}, the automatic
9342variables are set corresponding to the target and prerequisites.
9343@xref{Automatic Variables}.
9344
9345@node Archives, Features, Implicit Rules, Top
9346@chapter Using @code{make} to Update Archive Files
9347@cindex archive
9348
9349@dfn{Archive files} are files containing named subfiles called
9350@dfn{members}; they are maintained with the program @code{ar} and their
9351main use is as subroutine libraries for linking.
9352
9353@menu
9354* Archive Members:: Archive members as targets.
9355* Archive Update:: The implicit rule for archive member targets.
9356* Archive Pitfalls:: Dangers to watch out for when using archives.
9357* Archive Suffix Rules:: You can write a special kind of suffix rule
9358 for updating archives.
9359@end menu
9360
9361@node Archive Members, Archive Update, Archives, Archives
9362@section Archive Members as Targets
9363@cindex archive member targets
9364
9365An individual member of an archive file can be used as a target or
9366prerequisite in @code{make}. You specify the member named @var{member} in
9367archive file @var{archive} as follows:
9368
9369@example
9370@var{archive}(@var{member})
9371@end example
9372
9373@noindent
9374This construct is available only in targets and prerequisites, not in
9375commands! Most programs that you might use in commands do not support this
9376syntax and cannot act directly on archive members. Only @code{ar} and
9377other programs specifically designed to operate on archives can do so.
9378Therefore, valid commands to update an archive member target probably must
9379use @code{ar}. For example, this rule says to create a member
9380@file{hack.o} in archive @file{foolib} by copying the file @file{hack.o}:
9381
9382@example
9383foolib(hack.o) : hack.o
9384 ar cr foolib hack.o
9385@end example
9386
9387In fact, nearly all archive member targets are updated in just this way
9388and there is an implicit rule to do it for you. @strong{Note:} The
9389@samp{c} flag to @code{ar} is required if the archive file does not
9390already exist.
9391
9392To specify several members in the same archive, you can write all the
9393member names together between the parentheses. For example:
9394
9395@example
9396foolib(hack.o kludge.o)
9397@end example
9398
9399@noindent
9400is equivalent to:
9401
9402@example
9403foolib(hack.o) foolib(kludge.o)
9404@end example
9405
9406@cindex wildcard, in archive member
9407You can also use shell-style wildcards in an archive member reference.
9408@xref{Wildcards, ,Using Wildcard Characters in File Names}. For
9409example, @w{@samp{foolib(*.o)}} expands to all existing members of the
9410@file{foolib} archive whose names end in @samp{.o}; perhaps
9411@samp{@w{foolib(hack.o)} @w{foolib(kludge.o)}}.
9412
9413@node Archive Update, Archive Pitfalls, Archive Members, Archives
9414@section Implicit Rule for Archive Member Targets
9415
9416Recall that a target that looks like @file{@var{a}(@var{m})} stands for the
9417member named @var{m} in the archive file @var{a}.
9418
9419When @code{make} looks for an implicit rule for such a target, as a special
9420feature it considers implicit rules that match @file{(@var{m})}, as well as
9421those that match the actual target @file{@var{a}(@var{m})}.
9422
9423This causes one special rule whose target is @file{(%)} to match. This
9424rule updates the target @file{@var{a}(@var{m})} by copying the file @var{m}
9425into the archive. For example, it will update the archive member target
9426@file{foo.a(bar.o)} by copying the @emph{file} @file{bar.o} into the
9427archive @file{foo.a} as a @emph{member} named @file{bar.o}.
9428
9429When this rule is chained with others, the result is very powerful.
9430Thus, @samp{make "foo.a(bar.o)"} (the quotes are needed to protect the
9431@samp{(} and @samp{)} from being interpreted specially by the shell) in
9432the presence of a file @file{bar.c} is enough to cause the following
9433commands to be run, even without a makefile:
9434
9435@example
9436cc -c bar.c -o bar.o
9437ar r foo.a bar.o
9438rm -f bar.o
9439@end example
9440
9441@noindent
9442Here @code{make} has envisioned the file @file{bar.o} as an intermediate
9443file. @xref{Chained Rules, ,Chains of Implicit Rules}.
9444
9445Implicit rules such as this one are written using the automatic variable
9446@samp{$%}. @xref{Automatic Variables}.
9447
9448An archive member name in an archive cannot contain a directory name, but
9449it may be useful in a makefile to pretend that it does. If you write an
9450archive member target @file{foo.a(dir/file.o)}, @code{make} will perform
9451automatic updating with this command:
9452
9453@example
9454ar r foo.a dir/file.o
9455@end example
9456
9457@noindent
9458which has the effect of copying the file @file{dir/file.o} into a member
9459named @file{file.o}. In connection with such usage, the automatic variables
9460@code{%D} and @code{%F} may be useful.
9461
9462@menu
9463* Archive Symbols:: How to update archive symbol directories.
9464@end menu
9465
9466@node Archive Symbols, , Archive Update, Archive Update
9467@subsection Updating Archive Symbol Directories
9468@cindex @code{__.SYMDEF}
9469@cindex updating archive symbol directories
9470@cindex archive symbol directory updating
9471@cindex symbol directories, updating archive
9472@cindex directories, updating archive symbol
9473
9474An archive file that is used as a library usually contains a special member
9475named @file{__.SYMDEF} that contains a directory of the external symbol
9476names defined by all the other members. After you update any other
9477members, you need to update @file{__.SYMDEF} so that it will summarize the
9478other members properly. This is done by running the @code{ranlib} program:
9479
9480@example
9481ranlib @var{archivefile}
9482@end example
9483
9484Normally you would put this command in the rule for the archive file,
9485and make all the members of the archive file prerequisites of that rule.
9486For example,
9487
9488@example
9489libfoo.a: libfoo.a(x.o) libfoo.a(y.o) @dots{}
9490 ranlib libfoo.a
9491@end example
9492
9493@noindent
9494The effect of this is to update archive members @file{x.o}, @file{y.o},
9495etc., and then update the symbol directory member @file{__.SYMDEF} by
9496running @code{ranlib}. The rules for updating the members are not shown
9497here; most likely you can omit them and use the implicit rule which copies
9498files into the archive, as described in the preceding section.
9499
9500This is not necessary when using the GNU @code{ar} program, which
9501updates the @file{__.SYMDEF} member automatically.
9502
9503@node Archive Pitfalls, Archive Suffix Rules, Archive Update, Archives
9504@section Dangers When Using Archives
9505@cindex archive, and parallel execution
9506@cindex parallel execution, and archive update
9507@cindex archive, and @code{-j}
9508@cindex @code{-j}, and archive update
9509
9510It is important to be careful when using parallel execution (the
9511@code{-j} switch; @pxref{Parallel, ,Parallel Execution}) and archives.
9512If multiple @code{ar} commands run at the same time on the same archive
9513file, they will not know about each other and can corrupt the file.
9514
9515Possibly a future version of @code{make} will provide a mechanism to
9516circumvent this problem by serializing all commands that operate on the
9517same archive file. But for the time being, you must either write your
9518makefiles to avoid this problem in some other way, or not use @code{-j}.
9519
9520@node Archive Suffix Rules, , Archive Pitfalls, Archives
9521@section Suffix Rules for Archive Files
9522@cindex suffix rule, for archive
9523@cindex archive, suffix rule for
9524@cindex library archive, suffix rule for
9525@cindex @code{.a} (archives)
9526
9527You can write a special kind of suffix rule for dealing with archive
9528files. @xref{Suffix Rules}, for a full explanation of suffix rules.
9529Archive suffix rules are obsolete in GNU @code{make}, because pattern
9530rules for archives are a more general mechanism (@pxref{Archive
9531Update}). But they are retained for compatibility with other
9532@code{make}s.
9533
9534To write a suffix rule for archives, you simply write a suffix rule
9535using the target suffix @samp{.a} (the usual suffix for archive files).
9536For example, here is the old-fashioned suffix rule to update a library
9537archive from C source files:
9538
9539@example
9540@group
9541.c.a:
9542 $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o
9543 $(AR) r $@@ $*.o
9544 $(RM) $*.o
9545@end group
9546@end example
9547
9548@noindent
9549This works just as if you had written the pattern rule:
9550
9551@example
9552@group
9553(%.o): %.c
9554 $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o
9555 $(AR) r $@@ $*.o
9556 $(RM) $*.o
9557@end group
9558@end example
9559
9560In fact, this is just what @code{make} does when it sees a suffix rule
9561with @samp{.a} as the target suffix. Any double-suffix rule
9562@w{@samp{.@var{x}.a}} is converted to a pattern rule with the target
9563pattern @samp{(%.o)} and a prerequisite pattern of @samp{%.@var{x}}.
9564
9565Since you might want to use @samp{.a} as the suffix for some other kind
9566of file, @code{make} also converts archive suffix rules to pattern rules
9567in the normal way (@pxref{Suffix Rules}). Thus a double-suffix rule
9568@w{@samp{.@var{x}.a}} produces two pattern rules: @samp{@w{(%.o):}
9569@w{%.@var{x}}} and @samp{@w{%.a}: @w{%.@var{x}}}.@refill
9570
9571@node Features, Missing, Archives, Top
9572@chapter Features of GNU @code{make}
9573@cindex features of GNU @code{make}
9574@cindex portability
9575@cindex compatibility
9576
9577Here is a summary of the features of GNU @code{make}, for comparison
9578with and credit to other versions of @code{make}. We consider the
9579features of @code{make} in 4.2 BSD systems as a baseline. If you are
9580concerned with writing portable makefiles, you should not use the
9581features of @code{make} listed here, nor the ones in @ref{Missing}.
9582
9583Many features come from the version of @code{make} in System V.
9584
9585@itemize @bullet
9586@item
9587The @code{VPATH} variable and its special meaning.
9588@xref{Directory Search, , Searching Directories for Prerequisites}.
9589This feature exists in System V @code{make}, but is undocumented.
9590It is documented in 4.3 BSD @code{make} (which says it mimics System V's
9591@code{VPATH} feature).@refill
9592
9593@item
9594Included makefiles. @xref{Include, ,Including Other Makefiles}.
9595Allowing multiple files to be included with a single directive is a GNU
9596extension.
9597
9598@item
9599Variables are read from and communicated via the environment.
9600@xref{Environment, ,Variables from the Environment}.
9601
9602@item
9603Options passed through the variable @code{MAKEFLAGS} to recursive
9604invocations of @code{make}.
9605@xref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
9606
9607@item
9608The automatic variable @code{$%} is set to the member name
9609in an archive reference. @xref{Automatic Variables}.
9610
9611@item
9612The automatic variables @code{$@@}, @code{$*}, @code{$<}, @code{$%},
9613and @code{$?} have corresponding forms like @code{$(@@F)} and
9614@code{$(@@D)}. We have generalized this to @code{$^} as an obvious
9615extension. @xref{Automatic Variables}.@refill
9616
9617@item
9618Substitution variable references.
9619@xref{Reference, ,Basics of Variable References}.
9620
9621@item
9622The command-line options @samp{-b} and @samp{-m}, accepted and
9623ignored. In System V @code{make}, these options actually do something.
9624
9625@item
9626Execution of recursive commands to run @code{make} via the variable
9627@code{MAKE} even if @samp{-n}, @samp{-q} or @samp{-t} is specified.
9628@xref{Recursion, ,Recursive Use of @code{make}}.
9629
9630@item
9631Support for suffix @samp{.a} in suffix rules. @xref{Archive Suffix
9632Rules}. This feature is obsolete in GNU @code{make}, because the
9633general feature of rule chaining (@pxref{Chained Rules, ,Chains of
9634Implicit Rules}) allows one pattern rule for installing members in an
9635archive (@pxref{Archive Update}) to be sufficient.
9636
9637@item
9638The arrangement of lines and backslash-newline combinations in
9639commands is retained when the commands are printed, so they appear as
9640they do in the makefile, except for the stripping of initial
9641whitespace.
9642@end itemize
9643
9644The following features were inspired by various other versions of
9645@code{make}. In some cases it is unclear exactly which versions inspired
9646which others.
9647
9648@itemize @bullet
9649@item
9650Pattern rules using @samp{%}.
9651This has been implemented in several versions of @code{make}.
9652We're not sure who invented it first, but it's been spread around a bit.
9653@xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.@refill
9654
9655@item
9656Rule chaining and implicit intermediate files.
9657This was implemented by Stu Feldman in his version of @code{make}
9658for AT&T Eighth Edition Research Unix, and later by Andrew Hume of
9659AT&T Bell Labs in his @code{mk} program (where he terms it
9660``transitive closure''). We do not really know if
9661we got this from either of them or thought it up ourselves at the
9662same time. @xref{Chained Rules, ,Chains of Implicit Rules}.
9663
9664@item
9665The automatic variable @code{$^} containing a list of all prerequisites
9666of the current target. We did not invent this, but we have no idea who
9667did. @xref{Automatic Variables}. The automatic variable
9668@code{$+} is a simple extension of @code{$^}.
9669
9670@item
9671The ``what if'' flag (@samp{-W} in GNU @code{make}) was (as far as we know)
9672invented by Andrew Hume in @code{mk}.
9673@xref{Instead of Execution, ,Instead of Executing the Commands}.
9674
9675@item
9676The concept of doing several things at once (parallelism) exists in
9677many incarnations of @code{make} and similar programs, though not in the
9678System V or BSD implementations. @xref{Execution, ,Command Execution}.
9679
9680@item
9681Modified variable references using pattern substitution come from
9682SunOS 4. @xref{Reference, ,Basics of Variable References}.
9683This functionality was provided in GNU @code{make} by the
9684@code{patsubst} function before the alternate syntax was implemented
9685for compatibility with SunOS 4. It is not altogether clear who
9686inspired whom, since GNU @code{make} had @code{patsubst} before SunOS
96874 was released.@refill
9688
9689@item
9690The special significance of @samp{+} characters preceding command lines
9691(@pxref{Instead of Execution, ,Instead of Executing the Commands}) is
9692mandated by
9693@cite{IEEE Standard 1003.2-1992} (POSIX.2).
9694
9695@item
9696The @samp{+=} syntax to append to the value of a variable comes from SunOS
96974 @code{make}. @xref{Appending, , Appending More Text to Variables}.
9698
9699@item
9700The syntax @w{@samp{@var{archive}(@var{mem1} @var{mem2}@dots{})}} to list
9701multiple members in a single archive file comes from SunOS 4 @code{make}.
9702@xref{Archive Members}.
9703
9704@item
9705The @code{-include} directive to include makefiles with no error for a
9706nonexistent file comes from SunOS 4 @code{make}. (But note that SunOS 4
9707@code{make} does not allow multiple makefiles to be specified in one
9708@code{-include} directive.) The same feature appears with the name
9709@code{sinclude} in SGI @code{make} and perhaps others.
9710@end itemize
9711
9712The remaining features are inventions new in GNU @code{make}:
9713
9714@itemize @bullet
9715@item
9716Use the @samp{-v} or @samp{--version} option to print version and
9717copyright information.
9718
9719@item
9720Use the @samp{-h} or @samp{--help} option to summarize the options to
9721@code{make}.
9722
9723@item
9724Simply-expanded variables. @xref{Flavors, ,The Two Flavors of Variables}.
9725
9726@item
9727Pass command-line variable assignments automatically through the
9728variable @code{MAKE} to recursive @code{make} invocations.
9729@xref{Recursion, ,Recursive Use of @code{make}}.
9730
9731@item
9732Use the @samp{-C} or @samp{--directory} command option to change
9733directory. @xref{Options Summary, ,Summary of Options}.
9734
9735@item
9736Make verbatim variable definitions with @code{define}.
9737@xref{Defining, ,Defining Variables Verbatim}.
9738
9739@item
9740Declare phony targets with the special target @code{.PHONY}.
9741
9742Andrew Hume of AT&T Bell Labs implemented a similar feature with a
9743different syntax in his @code{mk} program. This seems to be a case of
9744parallel discovery. @xref{Phony Targets, ,Phony Targets}.
9745
9746@item
9747Manipulate text by calling functions.
9748@xref{Functions, ,Functions for Transforming Text}.
9749
9750@item
9751Use the @samp{-o} or @samp{--old-file}
9752option to pretend a file's modification-time is old.
9753@xref{Avoiding Compilation, ,Avoiding Recompilation of Some Files}.
9754
9755@item
9756Conditional execution.
9757
9758This feature has been implemented numerous times in various versions
9759of @code{make}; it seems a natural extension derived from the features
9760of the C preprocessor and similar macro languages and is not a
9761revolutionary concept. @xref{Conditionals, ,Conditional Parts of Makefiles}.
9762
9763@item
9764Specify a search path for included makefiles.
9765@xref{Include, ,Including Other Makefiles}.
9766
9767@item
9768Specify extra makefiles to read with an environment variable.
9769@xref{MAKEFILES Variable, ,The Variable @code{MAKEFILES}}.
9770
9771@item
9772Strip leading sequences of @samp{./} from file names, so that
9773@file{./@var{file}} and @file{@var{file}} are considered to be the
9774same file.@refill
9775
9776@item
9777Use a special search method for library prerequisites written in the
9778form @samp{-l@var{name}}.
9779@xref{Libraries/Search, ,Directory Search for Link Libraries}.
9780
9781@item
9782Allow suffixes for suffix rules
9783(@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}) to contain any
9784characters. In other versions of @code{make}, they must begin with
9785@samp{.} and not contain any @samp{/} characters.
9786
9787@item
9788Keep track of the current level of @code{make} recursion using the
9789variable @code{MAKELEVEL}. @xref{Recursion, ,Recursive Use of @code{make}}.
9790
9791@item
9792Provide any goals given on the command line in the variable
9793@code{MAKECMDGOALS}. @xref{Goals, ,Arguments to Specify the Goals}.
9794
9795@item
9796Specify static pattern rules. @xref{Static Pattern, ,Static Pattern Rules}.
9797
9798@item
9799Provide selective @code{vpath} search.
9800@xref{Directory Search, ,Searching Directories for Prerequisites}.
9801
9802@item
9803Provide computed variable references.
9804@xref{Reference, ,Basics of Variable References}.
9805
9806@item
9807Update makefiles. @xref{Remaking Makefiles, ,How Makefiles Are Remade}.
9808System V @code{make} has a very, very limited form of this
9809functionality in that it will check out SCCS files for makefiles.
9810
9811@item
9812Various new built-in implicit rules.
9813@xref{Catalogue of Rules, ,Catalogue of Implicit Rules}.
9814
9815@item
9816The built-in variable @samp{MAKE_VERSION} gives the version number of
9817@code{make}.
9818@vindex MAKE_VERSION
9819@end itemize
9820
9821@node Missing, Makefile Conventions, Features, Top
9822@chapter Incompatibilities and Missing Features
9823@cindex incompatibilities
9824@cindex missing features
9825@cindex features, missing
9826
9827The @code{make} programs in various other systems support a few features
9828that are not implemented in GNU @code{make}. The POSIX.2 standard
9829(@cite{IEEE Standard 1003.2-1992}) which specifies @code{make} does not
9830require any of these features.@refill
9831
9832@itemize @bullet
9833@item
9834A target of the form @samp{@var{file}((@var{entry}))} stands for a member
9835of archive file @var{file}. The member is chosen, not by name, but by
9836being an object file which defines the linker symbol @var{entry}.@refill
9837
9838This feature was not put into GNU @code{make} because of the
9839nonmodularity of putting knowledge into @code{make} of the internal
9840format of archive file symbol tables.
9841@xref{Archive Symbols, ,Updating Archive Symbol Directories}.
9842
9843@item
9844Suffixes (used in suffix rules) that end with the character @samp{~}
9845have a special meaning to System V @code{make};
9846they refer to the SCCS file that corresponds
9847to the file one would get without the @samp{~}. For example, the
9848suffix rule @samp{.c~.o} would make the file @file{@var{n}.o} from
9849the SCCS file @file{s.@var{n}.c}. For complete coverage, a whole
9850series of such suffix rules is required.
9851@xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
9852
9853In GNU @code{make}, this entire series of cases is handled by two
9854pattern rules for extraction from SCCS, in combination with the
9855general feature of rule chaining.
9856@xref{Chained Rules, ,Chains of Implicit Rules}.
9857
9858@item
9859In System V and 4.3 BSD @code{make}, files found by @code{VPATH} search
9860(@pxref{Directory Search, ,Searching Directories for Prerequisites}) have their names changed inside command
9861strings. We feel it is much cleaner to always use automatic variables
9862and thus make this feature obsolete.@refill
9863
9864@item
9865In some Unix @code{make}s, the automatic variable @code{$*} appearing in
9866the prerequisites of a rule has the amazingly strange ``feature'' of
9867expanding to the full name of the @emph{target of that rule}. We cannot
9868imagine what went on in the minds of Unix @code{make} developers to do
9869this; it is utterly inconsistent with the normal definition of @code{$*}.
9870@vindex * @r{(automatic variable), unsupported bizarre usage}
9871
9872@item
9873In some Unix @code{make}s, implicit rule search
9874(@pxref{Implicit Rules, ,Using Implicit Rules}) is apparently done for
9875@emph{all} targets, not just those without commands. This means you can
9876do:@refill
9877
9878@example
9879@group
9880foo.o:
9881 cc -c foo.c
9882@end group
9883@end example
9884
9885@noindent
9886and Unix @code{make} will intuit that @file{foo.o} depends on
9887@file{foo.c}.@refill
9888
9889We feel that such usage is broken. The prerequisite properties of
9890@code{make} are well-defined (for GNU @code{make}, at least),
9891and doing such a thing simply does not fit the model.@refill
9892
9893@item
9894GNU @code{make} does not include any built-in implicit rules for
9895compiling or preprocessing EFL programs. If we hear of anyone who is
9896using EFL, we will gladly add them.
9897
9898@item
9899It appears that in SVR4 @code{make}, a suffix rule can be specified with
9900no commands, and it is treated as if it had empty commands
9901(@pxref{Empty Commands}). For example:
9902
9903@example
9904.c.a:
9905@end example
9906
9907@noindent
9908will override the built-in @file{.c.a} suffix rule.
9909
9910We feel that it is cleaner for a rule without commands to always simply
9911add to the prerequisite list for the target. The above example can be
9912easily rewritten to get the desired behavior in GNU @code{make}:
9913
9914@example
9915.c.a: ;
9916@end example
9917
9918@item
9919Some versions of @code{make} invoke the shell with the @samp{-e} flag,
9920except under @samp{-k} (@pxref{Testing, ,Testing the Compilation of a
9921Program}). The @samp{-e} flag tells the shell to exit as soon as any
9922program it runs returns a nonzero status. We feel it is cleaner to
9923write each shell command line to stand on its own and not require this
9924special treatment.
9925@end itemize
9926
9927@comment The makefile standards are in a separate file that is also
9928@comment included by standards.texi.
9929@include make-stds.texi
9930
9931@node Quick Reference, Error Messages, Makefile Conventions, Top
9932@appendix Quick Reference
9933
9934This appendix summarizes the directives, text manipulation functions,
9935and special variables which GNU @code{make} understands.
9936@xref{Special Targets}, @ref{Catalogue of Rules, ,Catalogue of Implicit Rules},
9937and @ref{Options Summary, ,Summary of Options},
9938for other summaries.
9939
9940Here is a summary of the directives GNU @code{make} recognizes:
9941
9942@table @code
9943@item define @var{variable}
9944@itemx endef
9945
9946Define a multi-line, recursively-expanded variable.@*
9947@xref{Sequences}.
9948
9949@item ifdef @var{variable}
9950@itemx ifndef @var{variable}
9951@itemx ifeq (@var{a},@var{b})
9952@itemx ifeq "@var{a}" "@var{b}"
9953@itemx ifeq '@var{a}' '@var{b}'
9954@itemx ifneq (@var{a},@var{b})
9955@itemx ifneq "@var{a}" "@var{b}"
9956@itemx ifneq '@var{a}' '@var{b}'
9957@itemx else
9958@itemx endif
9959
9960Conditionally evaluate part of the makefile.@*
9961@xref{Conditionals}.
9962
9963@item include @var{file}
9964@itemx -include @var{file}
9965@itemx sinclude @var{file}
9966
9967Include another makefile.@*
9968@xref{Include, ,Including Other Makefiles}.
9969
9970@item override @var{variable} = @var{value}
9971@itemx override @var{variable} := @var{value}
9972@itemx override @var{variable} += @var{value}
9973@itemx override @var{variable} ?= @var{value}
9974@itemx override define @var{variable}
9975@itemx endef
9976
9977Define a variable, overriding any previous definition, even one from
9978the command line.@*
9979@xref{Override Directive, ,The @code{override} Directive}.
9980
9981@item export
9982
9983Tell @code{make} to export all variables to child processes by default.@*
9984@xref{Variables/Recursion, , Communicating Variables to a Sub-@code{make}}.
9985
9986@item export @var{variable}
9987@itemx export @var{variable} = @var{value}
9988@itemx export @var{variable} := @var{value}
9989@itemx export @var{variable} += @var{value}
9990@itemx export @var{variable} ?= @var{value}
9991@itemx unexport @var{variable}
9992Tell @code{make} whether or not to export a particular variable to child
9993processes.@*
9994@xref{Variables/Recursion, , Communicating Variables to a Sub-@code{make}}.
9995
9996@item vpath @var{pattern} @var{path}
9997Specify a search path for files matching a @samp{%} pattern.@*
9998@xref{Selective Search, , The @code{vpath} Directive}.
9999
10000@item vpath @var{pattern}
10001Remove all search paths previously specified for @var{pattern}.
10002
10003@item vpath
10004Remove all search paths previously specified in any @code{vpath}
10005directive.
10006@end table
10007
10008Here is a summary of the built-in functions (@pxref{Functions}):
10009
10010@table @code
10011@item $(subst @var{from},@var{to},@var{text})
10012Replace @var{from} with @var{to} in @var{text}.@*
10013@xref{Text Functions, , Functions for String Substitution and Analysis}.
10014
10015@item $(patsubst @var{pattern},@var{replacement},@var{text})
10016Replace words matching @var{pattern} with @var{replacement} in @var{text}.@*
10017@xref{Text Functions, , Functions for String Substitution and Analysis}.
10018
10019@item $(strip @var{string})
10020Remove excess whitespace characters from @var{string}.@*
10021@xref{Text Functions, , Functions for String Substitution and Analysis}.
10022
10023@item $(findstring @var{find},@var{text})
10024Locate @var{find} in @var{text}.@*
10025@xref{Text Functions, , Functions for String Substitution and Analysis}.
10026
10027@item $(filter @var{pattern}@dots{},@var{text})
10028Select words in @var{text} that match one of the @var{pattern} words.@*
10029@xref{Text Functions, , Functions for String Substitution and Analysis}.
10030
10031@item $(filter-out @var{pattern}@dots{},@var{text})
10032Select words in @var{text} that @emph{do not} match any of the @var{pattern} words.@*
10033@xref{Text Functions, , Functions for String Substitution and Analysis}.
10034
10035@item $(sort @var{list})
10036Sort the words in @var{list} lexicographically, removing duplicates.@*
10037@xref{Text Functions, , Functions for String Substitution and Analysis}.
10038
10039@item $(word @var{n},@var{text})
10040Extract the @var{n}th word (one-origin) of @var{text}.@*
10041@xref{Text Functions, , Functions for String Substitution and Analysis}.
10042
10043@item $(words @var{text})
10044Count the number of words in @var{text}.@*
10045@xref{Text Functions, , Functions for String Substitution and Analysis}.
10046
10047@item $(wordlist @var{s},@var{e},@var{text})
10048Returns the list of words in @var{text} from @var{s} to @var{e}.@*
10049@xref{Text Functions, , Functions for String Substitution and Analysis}.
10050
10051@item $(firstword @var{names}@dots{})
10052Extract the first word of @var{names}.@*
10053@xref{Text Functions, , Functions for String Substitution and Analysis}.
10054
10055@item $(lastword @var{names}@dots{})
10056Extract the last word of @var{names}.@*
10057@xref{Text Functions, , Functions for String Substitution and Analysis}.
10058
10059@item $(dir @var{names}@dots{})
10060Extract the directory part of each file name.@*
10061@xref{File Name Functions, ,Functions for File Names}.
10062
10063@item $(notdir @var{names}@dots{})
10064Extract the non-directory part of each file name.@*
10065@xref{File Name Functions, ,Functions for File Names}.
10066
10067@item $(suffix @var{names}@dots{})
10068Extract the suffix (the last @samp{.} and following characters) of each file name.@*
10069@xref{File Name Functions, ,Functions for File Names}.
10070
10071@item $(basename @var{names}@dots{})
10072Extract the base name (name without suffix) of each file name.@*
10073@xref{File Name Functions, ,Functions for File Names}.
10074
10075@item $(addsuffix @var{suffix},@var{names}@dots{})
10076Append @var{suffix} to each word in @var{names}.@*
10077@xref{File Name Functions, ,Functions for File Names}.
10078
10079@item $(addprefix @var{prefix},@var{names}@dots{})
10080Prepend @var{prefix} to each word in @var{names}.@*
10081@xref{File Name Functions, ,Functions for File Names}.
10082
10083@item $(join @var{list1},@var{list2})
10084Join two parallel lists of words.@*
10085@xref{File Name Functions, ,Functions for File Names}.
10086
10087@item $(wildcard @var{pattern}@dots{})
10088Find file names matching a shell file name pattern (@emph{not} a
10089@samp{%} pattern).@*
10090@xref{Wildcard Function, ,The Function @code{wildcard}}.
10091
10092@item $(realpath @var{names}@dots{})
10093For each file name in @var{names}, expand to an absolute name that
10094does not contain any @code{.}, @code{..}, nor symlinks.@*
10095@xref{File Name Functions, ,Functions for File Names}.
10096
10097@item $(abspath @var{names}@dots{})
10098For each file name in @var{names}, expand to an absolute name that
10099does not contain any @code{.} or @code{..} components, but preserves
10100symlinks.@*
10101@xref{File Name Functions, ,Functions for File Names}.
10102
10103@item $(error @var{text}@dots{})
10104
10105When this function is evaluated, @code{make} generates a fatal error
10106with the message @var{text}.@*
10107@xref{Make Control Functions, ,Functions That Control Make}.
10108
10109@item $(warning @var{text}@dots{})
10110
10111When this function is evaluated, @code{make} generates a warning with
10112the message @var{text}.@*
10113@xref{Make Control Functions, ,Functions That Control Make}.
10114
10115@item $(shell @var{command})
10116
10117Execute a shell command and return its output.@*
10118@xref{Shell Function, , The @code{shell} Function}.
10119
10120@item $(origin @var{variable})
10121
10122Return a string describing how the @code{make} variable @var{variable} was
10123defined.@*
10124@xref{Origin Function, , The @code{origin} Function}.
10125
10126@item $(foreach @var{var},@var{words},@var{text})
10127
10128Evaluate @var{text} with @var{var} bound to each word in @var{words},
10129and concatenate the results.@*
10130@xref{Foreach Function, ,The @code{foreach} Function}.
10131
10132@item $(call @var{var},@var{param},@dots{})
10133
10134Evaluate the variable @var{var} replacing any references to @code{$(1)},
10135@code{$(2)} with the first, second, etc. @var{param} values.@*
10136@xref{Call Function, ,The @code{call} Function}.
10137
10138@item $(eval @var{text})
10139
10140Evaluate @var{text} then read the results as makefile commands.
10141Expands to the empty string.@*
10142@xref{Eval Function, ,The @code{eval} Function}.
10143
10144@item $(value @var{var})
10145
10146Evaluates to the contents of the variable @var{var}, with no expansion
10147performed on it.@*
10148@xref{Value Function, ,The @code{value} Function}.
10149@end table
10150
10151Here is a summary of the automatic variables.
10152@xref{Automatic Variables},
10153for full information.
10154
10155@table @code
10156@item $@@
10157The file name of the target.
10158
10159@item $%
10160The target member name, when the target is an archive member.
10161
10162@item $<
10163The name of the first prerequisite.
10164
10165@item $?
10166The names of all the prerequisites that are
10167newer than the target, with spaces between them.
10168For prerequisites which are archive members, only
10169the member named is used (@pxref{Archives}).
10170
10171@item $^
10172@itemx $+
10173The names of all the prerequisites, with spaces between them. For
10174prerequisites which are archive members, only the member named is used
10175(@pxref{Archives}). The value of @code{$^} omits duplicate
10176prerequisites, while @code{$+} retains them and preserves their order.
10177
10178@item $*
10179The stem with which an implicit rule matches
10180(@pxref{Pattern Match, ,How Patterns Match}).
10181
10182@item $(@@D)
10183@itemx $(@@F)
10184The directory part and the file-within-directory part of @code{$@@}.
10185
10186@item $(*D)
10187@itemx $(*F)
10188The directory part and the file-within-directory part of @code{$*}.
10189
10190@item $(%D)
10191@itemx $(%F)
10192The directory part and the file-within-directory part of @code{$%}.
10193
10194@item $(<D)
10195@itemx $(<F)
10196The directory part and the file-within-directory part of @code{$<}.
10197
10198@item $(^D)
10199@itemx $(^F)
10200The directory part and the file-within-directory part of @code{$^}.
10201
10202@item $(+D)
10203@itemx $(+F)
10204The directory part and the file-within-directory part of @code{$+}.
10205
10206@item $(?D)
10207@itemx $(?F)
10208The directory part and the file-within-directory part of @code{$?}.
10209@end table
10210
10211These variables are used specially by GNU @code{make}:
10212
10213@table @code
10214@item MAKEFILES
10215
10216Makefiles to be read on every invocation of @code{make}.@*
10217@xref{MAKEFILES Variable, ,The Variable @code{MAKEFILES}}.
10218
10219@item VPATH
10220
10221Directory search path for files not found in the current directory.@*
10222@xref{General Search, , @code{VPATH} Search Path for All Prerequisites}.
10223
10224@item SHELL
10225
10226The name of the system default command interpreter, usually @file{/bin/sh}.
10227You can set @code{SHELL} in the makefile to change the shell used to run
10228commands. @xref{Execution, ,Command Execution}. The @code{SHELL}
10229variable is handled specially when importing from and exporting to the
10230environment. @xref{Environment, ,Using Variable from the Environment}.
10231
10232@item MAKESHELL
10233
10234On MS-DOS only, the name of the command interpreter that is to be used
10235by @code{make}. This value takes precedence over the value of
10236@code{SHELL}. @xref{Execution, ,MAKESHELL variable}.
10237
10238@item MAKE
10239
10240The name with which @code{make} was invoked.
10241Using this variable in commands has special meaning.
10242@xref{MAKE Variable, ,How the @code{MAKE} Variable Works}.
10243
10244@item MAKELEVEL
10245
10246The number of levels of recursion (sub-@code{make}s).@*
10247@xref{Variables/Recursion}.
10248
10249@item MAKEFLAGS
10250
10251The flags given to @code{make}. You can set this in the environment or
10252a makefile to set flags.@*
10253@xref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
10254
10255It is @emph{never} appropriate to use @code{MAKEFLAGS} directly on a
10256command line: its contents may not be quoted correctly for use in the
10257shell. Always allow recursive @code{make}'s to obtain these values
10258through the environment from its parent.
10259
10260@item MAKECMDGOALS
10261
10262The targets given to @code{make} on the command line. Setting this
10263variable has no effect on the operation of @code{make}.@*
10264@xref{Goals, ,Arguments to Specify the Goals}.
10265
10266@item CURDIR
10267
10268Set to the pathname of the current working directory (after all
10269@code{-C} options are processed, if any). Setting this variable has no
10270effect on the operation of @code{make}.@*
10271@xref{Recursion, ,Recursive Use of @code{make}}.
10272
10273@item SUFFIXES
10274
10275The default list of suffixes before @code{make} reads any makefiles.
10276
10277@item .LIBPATTERNS
10278Defines the naming of the libraries @code{make} searches for, and their
10279order.@*
10280@xref{Libraries/Search, ,Directory Search for Link Libraries}.
10281@end table
10282
10283@node Error Messages, Complex Makefile, Quick Reference, Top
10284@comment node-name, next, previous, up
10285@appendix Errors Generated by Make
10286
10287Here is a list of the more common errors you might see generated by
10288@code{make}, and some information about what they mean and how to fix
10289them.
10290
10291Sometimes @code{make} errors are not fatal, especially in the presence
10292of a @code{-} prefix on a command script line, or the @code{-k} command
10293line option. Errors that are fatal are prefixed with the string
10294@code{***}.
10295
10296Error messages are all either prefixed with the name of the program
10297(usually @samp{make}), or, if the error is found in a makefile, the name
10298of the file and linenumber containing the problem.
10299
10300In the table below, these common prefixes are left off.
10301
10302@table @samp
10303
10304@item [@var{foo}] Error @var{NN}
10305@itemx [@var{foo}] @var{signal description}
10306These errors are not really @code{make} errors at all. They mean that a
10307program that @code{make} invoked as part of a command script returned a
10308non-0 error code (@samp{Error @var{NN}}), which @code{make} interprets
10309as failure, or it exited in some other abnormal fashion (with a
10310signal of some type). @xref{Errors, ,Errors in Commands}.
10311
10312If no @code{***} is attached to the message, then the subprocess failed
10313but the rule in the makefile was prefixed with the @code{-} special
10314character, so @code{make} ignored the error.
10315
10316@item missing separator. Stop.
10317@itemx missing separator (did you mean TAB instead of 8 spaces?). Stop.
10318This means that @code{make} could not understand much of anything about
10319the command line it just read. GNU @code{make} looks for various kinds
10320of separators (@code{:}, @code{=}, TAB characters, etc.) to help it
10321decide what kind of commandline it's seeing. This means it couldn't
10322find a valid one.
10323
10324One of the most common reasons for this message is that you (or perhaps
10325your oh-so-helpful editor, as is the case with many MS-Windows editors)
10326have attempted to indent your command scripts with spaces instead of a
10327TAB character. In this case, @code{make} will use the second form of
10328the error above. Remember that every line in the command script must
10329begin with a TAB character. Eight spaces do not count. @xref{Rule
10330Syntax}.
10331
10332@item commands commence before first target. Stop.
10333@itemx missing rule before commands. Stop.
10334This means the first thing in the makefile seems to be part of a command
10335script: it begins with a TAB character and doesn't appear to be a legal
10336@code{make} command (such as a variable assignment). Command scripts
10337must always be associated with a target.
10338
10339The second form is generated if the line has a semicolon as the first
10340non-whitespace character; @code{make} interprets this to mean you left
10341out the "target: prerequisite" section of a rule. @xref{Rule Syntax}.
10342
10343@item No rule to make target `@var{xxx}'.
10344@itemx No rule to make target `@var{xxx}', needed by `@var{yyy}'.
10345This means that @code{make} decided it needed to build a target, but
10346then couldn't find any instructions in the makefile on how to do that,
10347either explicit or implicit (including in the default rules database).
10348
10349If you want that file to be built, you will need to add a rule to your
10350makefile describing how that target can be built. Other possible
10351sources of this problem are typos in the makefile (if that filename is
10352wrong) or a corrupted source tree (if that file is not supposed to be
10353built, but rather only a prerequisite).
10354
10355@item No targets specified and no makefile found. Stop.
10356@itemx No targets. Stop.
10357The former means that you didn't provide any targets to be built on the
10358command line, and @code{make} couldn't find any makefiles to read in.
10359The latter means that some makefile was found, but it didn't contain any
10360default goal and none was given on the command line. GNU @code{make}
10361has nothing to do in these situations.
10362@xref{Makefile Arguments, ,Arguments to Specify the Makefile}.@refill
10363
10364@item Makefile `@var{xxx}' was not found.
10365@itemx Included makefile `@var{xxx}' was not found.
10366A makefile specified on the command line (first form) or included
10367(second form) was not found.
10368
10369@item warning: overriding commands for target `@var{xxx}'
10370@itemx warning: ignoring old commands for target `@var{xxx}'
10371GNU @code{make} allows commands to be specified only once per target
10372(except for double-colon rules). If you give commands for a target
10373which already has been defined to have commands, this warning is issued
10374and the second set of commands will overwrite the first set.
10375@xref{Multiple Rules, ,Multiple Rules for One Target}.
10376
10377@item Circular @var{xxx} <- @var{yyy} dependency dropped.
10378This means that @code{make} detected a loop in the dependency graph:
10379after tracing the prerequisite @var{yyy} of target @var{xxx}, and its
10380prerequisites, etc., one of them depended on @var{xxx} again.
10381
10382@item Recursive variable `@var{xxx}' references itself (eventually). Stop.
10383This means you've defined a normal (recursive) @code{make} variable
10384@var{xxx} that, when it's expanded, will refer to itself (@var{xxx}).
10385This is not allowed; either use simply-expanded variables (@code{:=}) or
10386use the append operator (@code{+=}). @xref{Using Variables, ,How to Use
10387Variables}.
10388
10389@item Unterminated variable reference. Stop.
10390This means you forgot to provide the proper closing parenthesis
10391or brace in your variable or function reference.
10392
10393@item insufficient arguments to function `@var{xxx}'. Stop.
10394This means you haven't provided the requisite number of arguments for
10395this function. See the documentation of the function for a description
10396of its arguments. @xref{Functions, ,Functions for Transforming Text}.
10397
10398@item missing target pattern. Stop.
10399@itemx multiple target patterns. Stop.
10400@itemx target pattern contains no `%'. Stop.
10401@itemx mixed implicit and static pattern rules. Stop.
10402These are generated for malformed static pattern rules. The first means
10403there's no pattern in the target section of the rule; the second means
10404there are multiple patterns in the target section; the third means
10405the target doesn't contain a pattern character (@code{%}); and the
10406fourth means that all three parts of the static pattern rule contain
10407pattern characters (@code{%})--only the first two parts should.
10408@xref{Static Usage, ,Syntax of Static Pattern Rules}.
10409
10410@item warning: -jN forced in submake: disabling jobserver mode.
10411This warning and the next are generated if @code{make} detects error
10412conditions related to parallel processing on systems where
10413sub-@code{make}s can communicate (@pxref{Options/Recursion,
10414,Communicating Options to a Sub-@code{make}}). This warning is
10415generated if a recursive invocation of a @code{make} process is forced
10416to have @samp{-j@var{N}} in its argument list (where @var{N} is greater
10417than one). This could happen, for example, if you set the @code{MAKE}
10418environment variable to @samp{make -j2}. In this case, the
10419sub-@code{make} doesn't communicate with other @code{make} processes and
10420will simply pretend it has two jobs of its own.
10421
10422@item warning: jobserver unavailable: using -j1. Add `+' to parent make rule.
10423In order for @code{make} processes to communicate, the parent will pass
10424information to the child. Since this could result in problems if the
10425child process isn't actually a @code{make}, the parent will only do this
10426if it thinks the child is a @code{make}. The parent uses the normal
10427algorithms to determine this (@pxref{MAKE Variable, ,How the @code{MAKE}
10428Variable Works}). If the makefile is constructed such that the parent
10429doesn't know the child is a @code{make} process, then the child will
10430receive only part of the information necessary. In this case, the child
10431will generate this warning message and proceed with its build in a
10432sequential manner.
10433
10434@end table
10435
10436@node Complex Makefile, GNU Free Documentation License, Error Messages, Top
10437@appendix Complex Makefile Example
10438
10439Here is the makefile for the GNU @code{tar} program. This is a
10440moderately complex makefile.
10441
10442Because it is the first target, the default goal is @samp{all}. An
10443interesting feature of this makefile is that @file{testpad.h} is a
10444source file automatically created by the @code{testpad} program,
10445itself compiled from @file{testpad.c}.
10446
10447If you type @samp{make} or @samp{make all}, then @code{make} creates
10448the @file{tar} executable, the @file{rmt} daemon that provides
10449remote tape access, and the @file{tar.info} Info file.
10450
10451If you type @samp{make install}, then @code{make} not only creates
10452@file{tar}, @file{rmt}, and @file{tar.info}, but also installs
10453them.
10454
10455If you type @samp{make clean}, then @code{make} removes the @samp{.o}
10456files, and the @file{tar}, @file{rmt}, @file{testpad},
10457@file{testpad.h}, and @file{core} files.
10458
10459If you type @samp{make distclean}, then @code{make} not only removes
10460the same files as does @samp{make clean} but also the
10461@file{TAGS}, @file{Makefile}, and @file{config.status} files.
10462(Although it is not evident, this makefile (and
10463@file{config.status}) is generated by the user with the
10464@code{configure} program, which is provided in the @code{tar}
10465distribution, but is not shown here.)
10466
10467If you type @samp{make realclean}, then @code{make} removes the same
10468files as does @samp{make distclean} and also removes the Info files
10469generated from @file{tar.texinfo}.
10470
10471In addition, there are targets @code{shar} and @code{dist} that create
10472distribution kits.
10473
10474@example
10475@group
10476# Generated automatically from Makefile.in by configure.
10477# Un*x Makefile for GNU tar program.
10478# Copyright (C) 1991 Free Software Foundation, Inc.
10479@end group
10480
10481@group
10482# This program is free software; you can redistribute
10483# it and/or modify it under the terms of the GNU
10484# General Public License @dots{}
10485@dots{}
10486@dots{}
10487@end group
10488
10489SHELL = /bin/sh
10490
10491#### Start of system configuration section. ####
10492
10493srcdir = .
10494
10495@group
10496# If you use gcc, you should either run the
10497# fixincludes script that comes with it or else use
10498# gcc with the -traditional option. Otherwise ioctl
10499# calls will be compiled incorrectly on some systems.
10500CC = gcc -O
10501YACC = bison -y
10502INSTALL = /usr/local/bin/install -c
10503INSTALLDATA = /usr/local/bin/install -c -m 644
10504@end group
10505
10506# Things you might add to DEFS:
10507# -DSTDC_HEADERS If you have ANSI C headers and
10508# libraries.
10509# -DPOSIX If you have POSIX.1 headers and
10510# libraries.
10511# -DBSD42 If you have sys/dir.h (unless
10512# you use -DPOSIX), sys/file.h,
10513# and st_blocks in `struct stat'.
10514# -DUSG If you have System V/ANSI C
10515# string and memory functions
10516# and headers, sys/sysmacros.h,
10517# fcntl.h, getcwd, no valloc,
10518# and ndir.h (unless
10519# you use -DDIRENT).
10520# -DNO_MEMORY_H If USG or STDC_HEADERS but do not
10521# include memory.h.
10522# -DDIRENT If USG and you have dirent.h
10523# instead of ndir.h.
10524# -DSIGTYPE=int If your signal handlers
10525# return int, not void.
10526# -DNO_MTIO If you lack sys/mtio.h
10527# (magtape ioctls).
10528# -DNO_REMOTE If you do not have a remote shell
10529# or rexec.
10530# -DUSE_REXEC To use rexec for remote tape
10531# operations instead of
10532# forking rsh or remsh.
10533# -DVPRINTF_MISSING If you lack vprintf function
10534# (but have _doprnt).
10535# -DDOPRNT_MISSING If you lack _doprnt function.
10536# Also need to define
10537# -DVPRINTF_MISSING.
10538# -DFTIME_MISSING If you lack ftime system call.
10539# -DSTRSTR_MISSING If you lack strstr function.
10540# -DVALLOC_MISSING If you lack valloc function.
10541# -DMKDIR_MISSING If you lack mkdir and
10542# rmdir system calls.
10543# -DRENAME_MISSING If you lack rename system call.
10544# -DFTRUNCATE_MISSING If you lack ftruncate
10545# system call.
10546# -DV7 On Version 7 Unix (not
10547# tested in a long time).
10548# -DEMUL_OPEN3 If you lack a 3-argument version
10549# of open, and want to emulate it
10550# with system calls you do have.
10551# -DNO_OPEN3 If you lack the 3-argument open
10552# and want to disable the tar -k
10553# option instead of emulating open.
10554# -DXENIX If you have sys/inode.h
10555# and need it 94 to be included.
10556
10557DEFS = -DSIGTYPE=int -DDIRENT -DSTRSTR_MISSING \
10558 -DVPRINTF_MISSING -DBSD42
10559# Set this to rtapelib.o unless you defined NO_REMOTE,
10560# in which case make it empty.
10561RTAPELIB = rtapelib.o
10562LIBS =
10563DEF_AR_FILE = /dev/rmt8
10564DEFBLOCKING = 20
10565
10566@group
10567CDEBUG = -g
10568CFLAGS = $(CDEBUG) -I. -I$(srcdir) $(DEFS) \
10569 -DDEF_AR_FILE=\"$(DEF_AR_FILE)\" \
10570 -DDEFBLOCKING=$(DEFBLOCKING)
10571LDFLAGS = -g
10572@end group
10573
10574@group
10575prefix = /usr/local
10576# Prefix for each installed program,
10577# normally empty or `g'.
10578binprefix =
10579
10580# The directory to install tar in.
10581bindir = $(prefix)/bin
10582
10583# The directory to install the info files in.
10584infodir = $(prefix)/info
10585@end group
10586
10587#### End of system configuration section. ####
10588
10589SRC1 = tar.c create.c extract.c buffer.c \
10590 getoldopt.c update.c gnu.c mangle.c
10591SRC2 = version.c list.c names.c diffarch.c \
10592 port.c wildmat.c getopt.c
10593SRC3 = getopt1.c regex.c getdate.y
10594SRCS = $(SRC1) $(SRC2) $(SRC3)
10595OBJ1 = tar.o create.o extract.o buffer.o \
10596 getoldopt.o update.o gnu.o mangle.o
10597OBJ2 = version.o list.o names.o diffarch.o \
10598 port.o wildmat.o getopt.o
10599OBJ3 = getopt1.o regex.o getdate.o $(RTAPELIB)
10600OBJS = $(OBJ1) $(OBJ2) $(OBJ3)
10601@group
10602AUX = README COPYING ChangeLog Makefile.in \
10603 makefile.pc configure configure.in \
10604 tar.texinfo tar.info* texinfo.tex \
10605 tar.h port.h open3.h getopt.h regex.h \
10606 rmt.h rmt.c rtapelib.c alloca.c \
10607 msd_dir.h msd_dir.c tcexparg.c \
10608 level-0 level-1 backup-specs testpad.c
10609@end group
10610
10611.PHONY: all
10612all: tar rmt tar.info
10613
10614@group
10615.PHONY: tar
10616tar: $(OBJS)
10617 $(CC) $(LDFLAGS) -o $@@ $(OBJS) $(LIBS)
10618@end group
10619
10620@group
10621rmt: rmt.c
10622 $(CC) $(CFLAGS) $(LDFLAGS) -o $@@ rmt.c
10623@end group
10624
10625@group
10626tar.info: tar.texinfo
10627 makeinfo tar.texinfo
10628@end group
10629
10630@group
10631.PHONY: install
10632install: all
10633 $(INSTALL) tar $(bindir)/$(binprefix)tar
10634 -test ! -f rmt || $(INSTALL) rmt /etc/rmt
10635 $(INSTALLDATA) $(srcdir)/tar.info* $(infodir)
10636@end group
10637
10638@group
10639$(OBJS): tar.h port.h testpad.h
10640regex.o buffer.o tar.o: regex.h
10641# getdate.y has 8 shift/reduce conflicts.
10642@end group
10643
10644@group
10645testpad.h: testpad
10646 ./testpad
10647@end group
10648
10649@group
10650testpad: testpad.o
10651 $(CC) -o $@@ testpad.o
10652@end group
10653
10654@group
10655TAGS: $(SRCS)
10656 etags $(SRCS)
10657@end group
10658
10659@group
10660.PHONY: clean
10661clean:
10662 rm -f *.o tar rmt testpad testpad.h core
10663@end group
10664
10665@group
10666.PHONY: distclean
10667distclean: clean
10668 rm -f TAGS Makefile config.status
10669@end group
10670
10671@group
10672.PHONY: realclean
10673realclean: distclean
10674 rm -f tar.info*
10675@end group
10676
10677@group
10678.PHONY: shar
10679shar: $(SRCS) $(AUX)
10680 shar $(SRCS) $(AUX) | compress \
10681 > tar-`sed -e '/version_string/!d' \
10682 -e 's/[^0-9.]*\([0-9.]*\).*/\1/' \
10683 -e q
10684 version.c`.shar.Z
10685@end group
10686
10687@group
10688.PHONY: dist
10689dist: $(SRCS) $(AUX)
10690 echo tar-`sed \
10691 -e '/version_string/!d' \
10692 -e 's/[^0-9.]*\([0-9.]*\).*/\1/' \
10693 -e q
10694 version.c` > .fname
10695 -rm -rf `cat .fname`
10696 mkdir `cat .fname`
10697 ln $(SRCS) $(AUX) `cat .fname`
10698 tar chZf `cat .fname`.tar.Z `cat .fname`
10699 -rm -rf `cat .fname` .fname
10700@end group
10701
10702@group
10703tar.zoo: $(SRCS) $(AUX)
10704 -rm -rf tmp.dir
10705 -mkdir tmp.dir
10706 -rm tar.zoo
10707 for X in $(SRCS) $(AUX) ; do \
10708 echo $$X ; \
10709 sed 's/$$/^M/' $$X \
10710 > tmp.dir/$$X ; done
10711 cd tmp.dir ; zoo aM ../tar.zoo *
10712 -rm -rf tmp.dir
10713@end group
10714@end example
10715
10716@raisesections
10717@include fdl.texi
10718@lowersections
10719
10720@node Concept Index, Name Index, GNU Free Documentation License, Top
10721@unnumbered Index of Concepts
10722
10723@printindex cp
10724
10725@node Name Index, , Concept Index, Top
10726@unnumbered Index of Functions, Variables, & Directives
10727
10728@printindex fn
10729
10730@summarycontents
10731@contents
10732@bye
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette