VirtualBox

source: kBuild/vendor/gnumake/3.82-cvs/doc/make.texi@ 2581

Last change on this file since 2581 was 2581, checked in by bird, 12 years ago

Sync'ed svn:eol-style with trunk.

  • Property svn:eol-style set to native
File size: 423.5 KB
Line 
1\input texinfo @c -*- Texinfo -*-
2@c %**start of header
3@setfilename make.info
4
5@include version.texi
6@set EDITION 0.71
7@set RCSID $Id: make.texi,v 1.66 2010/07/19 07:10:54 psmith Exp $
8
9@settitle GNU @code{make}
10@setchapternewpage odd
11@c Combine the variable and function indices:
12@syncodeindex vr fn
13@c Combine the program and concept indices:
14@syncodeindex pg cp
15@c FSF publishers: format makebook.texi instead of using this file directly.
16@c ISBN confirmed by Jasimin Huang <jasimin@fsf.org> on 25 Mar 2009
17@set ISBN 1-882114-83-3
18@c %**end of header
19
20@copying
21This file documents the GNU @code{make} utility, which determines
22automatically which pieces of a large program need to be recompiled,
23and issues the commands to recompile them.
24
25This is Edition @value{EDITION}, last updated @value{UPDATED},
26of @cite{The GNU Make Manual}, for GNU @code{make} version @value{VERSION}.
27
28Copyright @copyright{} 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
291996, 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005, 2006, 2007,
302008, 2009, 2010 Free Software Foundation, Inc.
31
32@quotation
33Permission is granted to copy, distribute and/or modify this document
34under the terms of the GNU Free Documentation License, Version 1.2 or
35any later version published by the Free Software Foundation; with no
36Invariant Sections, with the Front-Cover Texts being ``A GNU Manual,''
37and with the Back-Cover Texts as in (a) below. A copy of the
38license is included in the section entitled ``GNU Free Documentation
39License.''
40
41(a) The FSF's Back-Cover Text is: ``You have the freedom to copy and
42modify this GNU manual. Buying copies from the FSF supports it in
43developing GNU and promoting software freedom.''
44@end quotation
45@end copying
46
47@c finalout
48
49@c ISPELL CHECK: done, 10 June 1993 --roland
50@c ISPELL CHECK: done, 2000-06-25 --Martin Buchholz
51
52
53@dircategory Software development
54@direntry
55* Make: (make). Remake files automatically.
56@end direntry
57
58@iftex
59@shorttitlepage GNU Make
60@end iftex
61@titlepage
62@title GNU Make
63@subtitle A Program for Directing Recompilation
64@subtitle GNU @code{make} Version @value{VERSION}
65@subtitle @value{UPDATED-MONTH}
66@author Richard M. Stallman, Roland McGrath, Paul D. Smith
67@page
68@vskip 0pt plus 1filll
69@insertcopying
70@sp 2
71Published by the Free Software Foundation @*
7251 Franklin St. -- Fifth Floor @*
73Boston, MA 02110-1301 USA @*
74ISBN @value{ISBN} @*
75@sp 2
76Cover art by Etienne Suvasa.
77@end titlepage
78
79@summarycontents
80@contents
81
82@ifnottex
83@node Top, Overview, (dir), (dir)
84@top GNU @code{make}
85
86@insertcopying
87@end ifnottex
88
89@menu
90* Overview:: Overview of @code{make}.
91* Introduction:: An introduction to @code{make}.
92* Makefiles:: Makefiles tell @code{make} what to do.
93* Rules:: Rules describe when a file must be remade.
94* Recipes:: Recipes say how to remake a file.
95* Using Variables:: You can use variables to avoid repetition.
96* Conditionals:: Use or ignore parts of the makefile based
97 on the values of variables.
98* Functions:: Many powerful ways to manipulate text.
99* Invoking make: Running. How to invoke @code{make} on the command line.
100* Implicit Rules:: Use implicit rules to treat many files alike,
101 based on their file names.
102* Archives:: How @code{make} can update library archives.
103* Features:: Features GNU @code{make} has over other @code{make}s.
104* Missing:: What GNU @code{make} lacks from other @code{make}s.
105* Makefile Conventions:: Conventions for writing makefiles for
106 GNU programs.
107* Quick Reference:: A quick reference for experienced users.
108* Error Messages:: A list of common errors generated by @code{make}.
109* Complex Makefile:: A real example of a straightforward,
110 but nontrivial, makefile.
111
112* GNU Free Documentation License:: License for copying this manual
113* Concept Index:: Index of Concepts
114* Name Index:: Index of Functions, Variables, & Directives
115
116@detailmenu
117 --- The Detailed Node Listing ---
118
119Overview of @code{make}
120
121* Preparing:: Preparing and running make
122* Reading:: On reading this text
123* Bugs:: Problems and bugs
124
125An Introduction to Makefiles
126
127* Rule Introduction:: What a rule looks like.
128* Simple Makefile:: A simple makefile
129* How Make Works:: How @code{make} processes this makefile
130* Variables Simplify:: Variables make makefiles simpler
131* make Deduces:: Letting @code{make} deduce the recipe
132* Combine By Prerequisite:: Another style of makefile
133* Cleanup:: Rules for cleaning the directory
134
135Writing Makefiles
136
137* Makefile Contents:: What makefiles contain.
138* Makefile Names:: How to name your makefile.
139* Include:: How one makefile can use another makefile.
140* MAKEFILES Variable:: The environment can specify extra makefiles.
141* Remaking Makefiles:: How makefiles get remade.
142* Overriding Makefiles:: How to override part of one makefile
143 with another makefile.
144* Reading Makefiles:: How makefiles are parsed.
145* Secondary Expansion:: How and when secondary expansion is performed.
146
147Writing Rules
148
149* Rule Example:: An example explained.
150* Rule Syntax:: General syntax explained.
151* Prerequisite Types:: There are two types of prerequisites.
152* Wildcards:: Using wildcard characters such as `*'.
153* Directory Search:: Searching other directories for source files.
154* Phony Targets:: Using a target that is not a real file's name.
155* Force Targets:: You can use a target without a recipe
156 or prerequisites to mark other targets
157 as phony.
158* Empty Targets:: When only the date matters and the
159 files are empty.
160* Special Targets:: Targets with special built-in meanings.
161* Multiple Targets:: When to make use of several targets in a rule.
162* Multiple Rules:: How to use several rules with the same target.
163* Static Pattern:: Static pattern rules apply to multiple targets
164 and can vary the prerequisites according to
165 the target name.
166* Double-Colon:: How to use a special kind of rule to allow
167 several independent rules for one target.
168* Automatic Prerequisites:: How to automatically generate rules giving
169 prerequisites from source files themselves.
170
171Using Wildcard Characters in File Names
172
173* Wildcard Examples:: Several examples
174* Wildcard Pitfall:: Problems to avoid.
175* Wildcard Function:: How to cause wildcard expansion where
176 it does not normally take place.
177
178Searching Directories for Prerequisites
179
180* General Search:: Specifying a search path that applies
181 to every prerequisite.
182* Selective Search:: Specifying a search path
183 for a specified class of names.
184* Search Algorithm:: When and how search paths are applied.
185* Recipes/Search:: How to write recipes that work together
186 with search paths.
187* Implicit/Search:: How search paths affect implicit rules.
188* Libraries/Search:: Directory search for link libraries.
189
190Static Pattern Rules
191
192* Static Usage:: The syntax of static pattern rules.
193* Static versus Implicit:: When are they better than implicit rules?
194
195Writing Recipes in Rules
196
197* Recipe Syntax:: Recipe syntax features and pitfalls.
198* Echoing:: How to control when recipes are echoed.
199* Execution:: How recipes are executed.
200* Parallel:: How recipes can be executed in parallel.
201* Errors:: What happens after a recipe execution error.
202* Interrupts:: What happens when a recipe is interrupted.
203* Recursion:: Invoking @code{make} from makefiles.
204* Canned Recipes:: Defining canned recipes.
205* Empty Recipes:: Defining useful, do-nothing recipes.
206
207Recipe Syntax
208
209* Splitting Lines:: Breaking long recipe lines for readability.
210* Variables in Recipes:: Using @code{make} variables in recipes.
211
212Recipe Execution
213
214* Choosing the Shell:: How @code{make} chooses the shell used
215 to run recipes.
216
217Recursive Use of @code{make}
218
219* MAKE Variable:: The special effects of using @samp{$(MAKE)}.
220* Variables/Recursion:: How to communicate variables to a sub-@code{make}.
221* Options/Recursion:: How to communicate options to a sub-@code{make}.
222* -w Option:: How the @samp{-w} or @samp{--print-directory} option
223 helps debug use of recursive @code{make} commands.
224
225How to Use Variables
226
227* Reference:: How to use the value of a variable.
228* Flavors:: Variables come in two flavors.
229* Advanced:: Advanced features for referencing a variable.
230* Values:: All the ways variables get their values.
231* Setting:: How to set a variable in the makefile.
232* Appending:: How to append more text to the old value
233 of a variable.
234* Override Directive:: How to set a variable in the makefile even if
235 the user has set it with a command argument.
236* Multi-Line:: An alternate way to set a variable
237 to a multi-line string.
238* Environment:: Variable values can come from the environment.
239* Target-specific:: Variable values can be defined on a per-target
240 basis.
241* Pattern-specific:: Target-specific variable values can be applied
242 to a group of targets that match a pattern.
243* Suppressing Inheritance:: Suppress inheritance of variables.
244* Special Variables:: Variables with special meaning or behavior.
245
246Advanced Features for Reference to Variables
247
248* Substitution Refs:: Referencing a variable with
249 substitutions on the value.
250* Computed Names:: Computing the name of the variable to refer to.
251
252Conditional Parts of Makefiles
253
254* Conditional Example:: Example of a conditional
255* Conditional Syntax:: The syntax of conditionals.
256* Testing Flags:: Conditionals that test flags.
257
258Functions for Transforming Text
259
260* Syntax of Functions:: How to write a function call.
261* Text Functions:: General-purpose text manipulation functions.
262* File Name Functions:: Functions for manipulating file names.
263* Conditional Functions:: Functions that implement conditions.
264* Foreach Function:: Repeat some text with controlled variation.
265* Call Function:: Expand a user-defined function.
266* Value Function:: Return the un-expanded value of a variable.
267* Eval Function:: Evaluate the arguments as makefile syntax.
268* Origin Function:: Find where a variable got its value.
269* Flavor Function:: Find out the flavor of a variable.
270* Shell Function:: Substitute the output of a shell command.
271* Make Control Functions:: Functions that control how make runs.
272
273How to Run @code{make}
274
275* Makefile Arguments:: How to specify which makefile to use.
276* Goals:: How to use goal arguments to specify which
277 parts of the makefile to use.
278* Instead of Execution:: How to use mode flags to specify what
279 kind of thing to do with the recipes
280 in the makefile other than simply
281 execute them.
282* Avoiding Compilation:: How to avoid recompiling certain files.
283* Overriding:: How to override a variable to specify
284 an alternate compiler and other things.
285* Testing:: How to proceed past some errors, to
286 test compilation.
287* Options Summary:: Summary of Options
288
289Using Implicit Rules
290
291* Using Implicit:: How to use an existing implicit rule
292 to get the recipe for updating a file.
293* Catalogue of Rules:: A list of built-in implicit rules.
294* Implicit Variables:: How to change what predefined rules do.
295* Chained Rules:: How to use a chain of implicit rules.
296* Pattern Rules:: How to define new implicit rules.
297* Last Resort:: How to define a recipe for rules which
298 cannot find any.
299* Suffix Rules:: The old-fashioned style of implicit rule.
300* Implicit Rule Search:: The precise algorithm for applying
301 implicit rules.
302
303Defining and Redefining Pattern Rules
304
305* Pattern Intro:: An introduction to pattern rules.
306* Pattern Examples:: Examples of pattern rules.
307* Automatic Variables:: How to use automatic variables in the
308 recipe of implicit rules.
309* Pattern Match:: How patterns match.
310* Match-Anything Rules:: Precautions you should take prior to
311 defining rules that can match any
312 target file whatever.
313* Canceling Rules:: How to override or cancel built-in rules.
314
315Using @code{make} to Update Archive Files
316
317* Archive Members:: Archive members as targets.
318* Archive Update:: The implicit rule for archive member targets.
319* Archive Pitfalls:: Dangers to watch out for when using archives.
320* Archive Suffix Rules:: You can write a special kind of suffix rule
321 for updating archives.
322
323Implicit Rule for Archive Member Targets
324
325* Archive Symbols:: How to update archive symbol directories.
326
327@end detailmenu
328@end menu
329
330@node Overview, Introduction, Top, Top
331@comment node-name, next, previous, up
332@chapter Overview of @code{make}
333
334The @code{make} utility automatically determines which pieces of a large
335program need to be recompiled, and issues commands to recompile them.
336This manual describes GNU @code{make}, which was implemented by Richard
337Stallman and Roland McGrath. Development since Version 3.76 has been
338handled by Paul D. Smith.
339
340GNU @code{make} conforms to section 6.2 of @cite{IEEE Standard
3411003.2-1992} (POSIX.2).
342@cindex POSIX
343@cindex IEEE Standard 1003.2
344@cindex standards conformance
345
346Our examples show C programs, since they are most common, but you can use
347@code{make} with any programming language whose compiler can be run with a
348shell command. Indeed, @code{make} is not limited to programs. You can
349use it to describe any task where some files must be updated automatically
350from others whenever the others change.
351
352@menu
353* Preparing:: Preparing and Running Make
354* Reading:: On Reading this Text
355* Bugs:: Problems and Bugs
356@end menu
357
358@node Preparing, Reading, Overview, Overview
359@ifnottex
360@heading Preparing and Running Make
361@end ifnottex
362
363To prepare to use @code{make}, you must write a file called
364the @dfn{makefile} that describes the relationships among files
365in your program and provides commands for updating each file.
366In a program, typically, the executable file is updated from object
367files, which are in turn made by compiling source files.@refill
368
369Once a suitable makefile exists, each time you change some source files,
370this simple shell command:
371
372@example
373make
374@end example
375
376@noindent
377suffices to perform all necessary recompilations. The @code{make} program
378uses the makefile data base and the last-modification times of the files to
379decide which of the files need to be updated. For each of those files, it
380issues the recipes recorded in the data base.
381
382You can provide command line arguments to @code{make} to control which
383files should be recompiled, or how. @xref{Running, ,How to Run
384@code{make}}.
385
386@node Reading, Bugs, Preparing, Overview
387@section How to Read This Manual
388
389If you are new to @code{make}, or are looking for a general
390introduction, read the first few sections of each chapter, skipping the
391later sections. In each chapter, the first few sections contain
392introductory or general information and the later sections contain
393specialized or technical information.
394@ifnottex
395The exception is the second chapter, @ref{Introduction, ,An
396Introduction to Makefiles}, all of which is introductory.
397@end ifnottex
398@iftex
399The exception is @ref{Introduction, ,An Introduction to Makefiles},
400all of which is introductory.
401@end iftex
402
403If you are familiar with other @code{make} programs, see @ref{Features,
404,Features of GNU @code{make}}, which lists the enhancements GNU
405@code{make} has, and @ref{Missing, ,Incompatibilities and Missing
406Features}, which explains the few things GNU @code{make} lacks that
407others have.
408
409For a quick summary, see @ref{Options Summary}, @ref{Quick Reference},
410and @ref{Special Targets}.
411
412@node Bugs, , Reading, Overview
413@section Problems and Bugs
414@cindex reporting bugs
415@cindex bugs, reporting
416@cindex problems and bugs, reporting
417
418If you have problems with GNU @code{make} or think you've found a bug,
419please report it to the developers; we cannot promise to do anything but
420we might well want to fix it.
421
422Before reporting a bug, make sure you've actually found a real bug.
423Carefully reread the documentation and see if it really says you can do
424what you're trying to do. If it's not clear whether you should be able
425to do something or not, report that too; it's a bug in the
426documentation!
427
428Before reporting a bug or trying to fix it yourself, try to isolate it
429to the smallest possible makefile that reproduces the problem. Then
430send us the makefile and the exact results @code{make} gave you,
431including any error or warning messages. Please don't paraphrase
432these messages: it's best to cut and paste them into your report.
433When generating this small makefile, be sure to not use any non-free
434or unusual tools in your recipes: you can almost always emulate what
435such a tool would do with simple shell commands. Finally, be sure to
436explain what you expected to occur; this will help us decide whether
437the problem was really in the documentation.
438
439Once you have a precise problem you can report it in one of two ways.
440Either send electronic mail to:
441
442@example
443 bug-make@@gnu.org
444@end example
445
446@noindent
447or use our Web-based project management tool, at:
448
449@example
450 http://savannah.gnu.org/projects/make/
451@end example
452
453@noindent
454In addition to the information above, please be careful to include the
455version number of @code{make} you are using. You can get this
456information with the command @samp{make --version}. Be sure also to
457include the type of machine and operating system you are using. One
458way to obtain this information is by looking at the final lines of
459output from the command @samp{make --help}.
460
461@node Introduction, Makefiles, Overview, Top
462@comment node-name, next, previous, up
463@chapter An Introduction to Makefiles
464
465You need a file called a @dfn{makefile} to tell @code{make} what to do.
466Most often, the makefile tells @code{make} how to compile and link a
467program.
468@cindex makefile
469
470In this chapter, we will discuss a simple makefile that describes how to
471compile and link a text editor which consists of eight C source files
472and three header files. The makefile can also tell @code{make} how to
473run miscellaneous commands when explicitly asked (for example, to remove
474certain files as a clean-up operation). To see a more complex example
475of a makefile, see @ref{Complex Makefile}.
476
477When @code{make} recompiles the editor, each changed C source file
478must be recompiled. If a header file has changed, each C source file
479that includes the header file must be recompiled to be safe. Each
480compilation produces an object file corresponding to the source file.
481Finally, if any source file has been recompiled, all the object files,
482whether newly made or saved from previous compilations, must be linked
483together to produce the new executable editor.
484@cindex recompilation
485@cindex editor
486
487@menu
488* Rule Introduction:: What a rule looks like.
489* Simple Makefile:: A Simple Makefile
490* How Make Works:: How @code{make} Processes This Makefile
491* Variables Simplify:: Variables Make Makefiles Simpler
492* make Deduces:: Letting @code{make} Deduce the Recipes
493* Combine By Prerequisite:: Another Style of Makefile
494* Cleanup:: Rules for Cleaning the Directory
495@end menu
496
497@node Rule Introduction, Simple Makefile, Introduction, Introduction
498@comment node-name, next, previous, up
499@section What a Rule Looks Like
500@cindex rule, introduction to
501@cindex makefile rule parts
502@cindex parts of makefile rule
503
504A simple makefile consists of ``rules'' with the following shape:
505
506@cindex targets, introduction to
507@cindex prerequisites, introduction to
508@cindex recipes, introduction to
509@example
510@group
511@var{target} @dots{} : @var{prerequisites} @dots{}
512 @var{recipe}
513 @dots{}
514 @dots{}
515@end group
516@end example
517
518A @dfn{target} is usually the name of a file that is generated by a
519program; examples of targets are executable or object files. A target
520can also be the name of an action to carry out, such as @samp{clean}
521(@pxref{Phony Targets}).
522
523A @dfn{prerequisite} is a file that is used as input to create the
524target. A target often depends on several files.
525
526@cindex tabs in rules
527A @dfn{recipe} is an action that @code{make} carries out. A recipe
528may have more than one command, either on the same line or each on its
529own line. @strong{Please note:} you need to put a tab character at
530the beginning of every recipe line! This is an obscurity that catches
531the unwary. If you prefer to prefix your recipes with a character
532other than tab, you can set the @code{.RECIPEPREFIX} variable to an
533alternate character (@pxref{Special Variables}).
534
535Usually a recipe is in a rule with prerequisites and serves to create a
536target file if any of the prerequisites change. However, the rule that
537specifies a recipe for the target need not have prerequisites. For
538example, the rule containing the delete command associated with the
539target @samp{clean} does not have prerequisites.
540
541A @dfn{rule}, then, explains how and when to remake certain files
542which are the targets of the particular rule. @code{make} carries out
543the recipe on the prerequisites to create or update the target. A
544rule can also explain how and when to carry out an action.
545@xref{Rules, , Writing Rules}.
546
547A makefile may contain other text besides rules, but a simple makefile
548need only contain rules. Rules may look somewhat more complicated
549than shown in this template, but all fit the pattern more or less.
550
551@node Simple Makefile, How Make Works, Rule Introduction, Introduction
552@section A Simple Makefile
553@cindex simple makefile
554@cindex makefile, simple
555
556Here is a straightforward makefile that describes the way an
557executable file called @code{edit} depends on eight object files
558which, in turn, depend on eight C source and three header files.
559
560In this example, all the C files include @file{defs.h}, but only those
561defining editing commands include @file{command.h}, and only low
562level files that change the editor buffer include @file{buffer.h}.
563
564@example
565@group
566edit : main.o kbd.o command.o display.o \
567 insert.o search.o files.o utils.o
568 cc -o edit main.o kbd.o command.o display.o \
569 insert.o search.o files.o utils.o
570
571main.o : main.c defs.h
572 cc -c main.c
573kbd.o : kbd.c defs.h command.h
574 cc -c kbd.c
575command.o : command.c defs.h command.h
576 cc -c command.c
577display.o : display.c defs.h buffer.h
578 cc -c display.c
579insert.o : insert.c defs.h buffer.h
580 cc -c insert.c
581search.o : search.c defs.h buffer.h
582 cc -c search.c
583files.o : files.c defs.h buffer.h command.h
584 cc -c files.c
585utils.o : utils.c defs.h
586 cc -c utils.c
587clean :
588 rm edit main.o kbd.o command.o display.o \
589 insert.o search.o files.o utils.o
590@end group
591@end example
592
593@noindent
594We split each long line into two lines using backslash-newline; this is
595like using one long line, but is easier to read.
596@cindex continuation lines
597@cindex @code{\} (backslash), for continuation lines
598@cindex backslash (@code{\}), for continuation lines
599@cindex quoting newline, in makefile
600@cindex newline, quoting, in makefile
601
602To use this makefile to create the executable file called @file{edit},
603type:
604
605@example
606make
607@end example
608
609To use this makefile to delete the executable file and all the object
610files from the directory, type:
611
612@example
613make clean
614@end example
615
616In the example makefile, the targets include the executable file
617@samp{edit}, and the object files @samp{main.o} and @samp{kbd.o}. The
618prerequisites are files such as @samp{main.c} and @samp{defs.h}.
619In fact, each @samp{.o} file is both a target and a prerequisite.
620Recipes include @w{@samp{cc -c main.c}} and @w{@samp{cc -c kbd.c}}.
621
622When a target is a file, it needs to be recompiled or relinked if any
623of its prerequisites change. In addition, any prerequisites that are
624themselves automatically generated should be updated first. In this
625example, @file{edit} depends on each of the eight object files; the
626object file @file{main.o} depends on the source file @file{main.c} and
627on the header file @file{defs.h}.
628
629A recipe may follow each line that contains a target and
630prerequisites. These recipes say how to update the target file. A
631tab character (or whatever character is specified by the
632@code{.RECIPEPREFIX} variable; @pxref{Special Variables}) must come at
633the beginning of every line in the recipe to distinguish recipes from
634other lines in the makefile. (Bear in mind that @code{make} does not
635know anything about how the recipes work. It is up to you to supply
636recipes that will update the target file properly. All @code{make}
637does is execute the recipe you have specified when the target file
638needs to be updated.)@refill
639@cindex recipe
640
641The target @samp{clean} is not a file, but merely the name of an
642action. Since you normally do not want to carry out the actions in
643this rule, @samp{clean} is not a prerequisite of any other rule.
644Consequently, @code{make} never does anything with it unless you tell
645it specifically. Note that this rule not only is not a prerequisite,
646it also does not have any prerequisites, so the only purpose of the
647rule is to run the specified recipe. Targets that do not refer to
648files but are just actions are called @dfn{phony targets}.
649@xref{Phony Targets}, for information about this kind of target.
650@xref{Errors, , Errors in Recipes}, to see how to cause @code{make}
651to ignore errors from @code{rm} or any other command.
652@cindex @code{clean} target
653@cindex @code{rm} (shell command)
654
655@node How Make Works, Variables Simplify, Simple Makefile, Introduction
656@comment node-name, next, previous, up
657@section How @code{make} Processes a Makefile
658@cindex processing a makefile
659@cindex makefile, how @code{make} processes
660
661By default, @code{make} starts with the first target (not targets whose
662names start with @samp{.}). This is called the @dfn{default goal}.
663(@dfn{Goals} are the targets that @code{make} strives ultimately to
664update. You can override this behavior using the command line
665(@pxref{Goals, , Arguments to Specify the Goals}) or with the
666@code{.DEFAULT_GOAL} special variable (@pxref{Special Variables, ,
667Other Special Variables}).
668@cindex default goal
669@cindex goal, default
670@cindex goal
671
672In the simple example of the previous section, the default goal is to
673update the executable program @file{edit}; therefore, we put that rule
674first.
675
676Thus, when you give the command:
677
678@example
679make
680@end example
681
682@noindent
683@code{make} reads the makefile in the current directory and begins by
684processing the first rule. In the example, this rule is for relinking
685@file{edit}; but before @code{make} can fully process this rule, it
686must process the rules for the files that @file{edit} depends on,
687which in this case are the object files. Each of these files is
688processed according to its own rule. These rules say to update each
689@samp{.o} file by compiling its source file. The recompilation must
690be done if the source file, or any of the header files named as
691prerequisites, is more recent than the object file, or if the object
692file does not exist.
693
694The other rules are processed because their targets appear as
695prerequisites of the goal. If some other rule is not depended on by the
696goal (or anything it depends on, etc.), that rule is not processed,
697unless you tell @code{make} to do so (with a command such as
698@w{@code{make clean}}).
699
700Before recompiling an object file, @code{make} considers updating its
701prerequisites, the source file and header files. This makefile does not
702specify anything to be done for them---the @samp{.c} and @samp{.h} files
703are not the targets of any rules---so @code{make} does nothing for these
704files. But @code{make} would update automatically generated C programs,
705such as those made by Bison or Yacc, by their own rules at this time.
706
707After recompiling whichever object files need it, @code{make} decides
708whether to relink @file{edit}. This must be done if the file
709@file{edit} does not exist, or if any of the object files are newer than
710it. If an object file was just recompiled, it is now newer than
711@file{edit}, so @file{edit} is relinked.
712@cindex relinking
713
714Thus, if we change the file @file{insert.c} and run @code{make},
715@code{make} will compile that file to update @file{insert.o}, and then
716link @file{edit}. If we change the file @file{command.h} and run
717@code{make}, @code{make} will recompile the object files @file{kbd.o},
718@file{command.o} and @file{files.o} and then link the file @file{edit}.
719
720@node Variables Simplify, make Deduces, How Make Works, Introduction
721@section Variables Make Makefiles Simpler
722@cindex variables
723@cindex simplifying with variables
724
725In our example, we had to list all the object files twice in the rule for
726@file{edit} (repeated here):
727
728@example
729@group
730edit : main.o kbd.o command.o display.o \
731 insert.o search.o files.o utils.o
732 cc -o edit main.o kbd.o command.o display.o \
733 insert.o search.o files.o utils.o
734@end group
735@end example
736
737@cindex @code{objects}
738Such duplication is error-prone; if a new object file is added to the
739system, we might add it to one list and forget the other. We can eliminate
740the risk and simplify the makefile by using a variable. @dfn{Variables}
741allow a text string to be defined once and substituted in multiple places
742later (@pxref{Using Variables, ,How to Use Variables}).
743
744@cindex @code{OBJECTS}
745@cindex @code{objs}
746@cindex @code{OBJS}
747@cindex @code{obj}
748@cindex @code{OBJ}
749It is standard practice for every makefile to have a variable named
750@code{objects}, @code{OBJECTS}, @code{objs}, @code{OBJS}, @code{obj},
751or @code{OBJ} which is a list of all object file names. We would
752define such a variable @code{objects} with a line like this in the
753makefile:@refill
754
755@example
756@group
757objects = main.o kbd.o command.o display.o \
758 insert.o search.o files.o utils.o
759@end group
760@end example
761
762@noindent
763Then, each place we want to put a list of the object file names, we can
764substitute the variable's value by writing @samp{$(objects)}
765(@pxref{Using Variables, ,How to Use Variables}).
766
767Here is how the complete simple makefile looks when you use a variable
768for the object files:
769
770@example
771@group
772objects = main.o kbd.o command.o display.o \
773 insert.o search.o files.o utils.o
774
775edit : $(objects)
776 cc -o edit $(objects)
777main.o : main.c defs.h
778 cc -c main.c
779kbd.o : kbd.c defs.h command.h
780 cc -c kbd.c
781command.o : command.c defs.h command.h
782 cc -c command.c
783display.o : display.c defs.h buffer.h
784 cc -c display.c
785insert.o : insert.c defs.h buffer.h
786 cc -c insert.c
787search.o : search.c defs.h buffer.h
788 cc -c search.c
789files.o : files.c defs.h buffer.h command.h
790 cc -c files.c
791utils.o : utils.c defs.h
792 cc -c utils.c
793clean :
794 rm edit $(objects)
795@end group
796@end example
797
798@node make Deduces, Combine By Prerequisite, Variables Simplify, Introduction
799@section Letting @code{make} Deduce the Recipes
800@cindex deducing recipes (implicit rules)
801@cindex implicit rule, introduction to
802@cindex rule, implicit, introduction to
803
804It is not necessary to spell out the recipes for compiling the individual
805C source files, because @code{make} can figure them out: it has an
806@dfn{implicit rule} for updating a @samp{.o} file from a correspondingly
807named @samp{.c} file using a @samp{cc -c} command. For example, it will
808use the recipe @samp{cc -c main.c -o main.o} to compile @file{main.c} into
809@file{main.o}. We can therefore omit the recipes from the rules for the
810object files. @xref{Implicit Rules, ,Using Implicit Rules}.@refill
811
812When a @samp{.c} file is used automatically in this way, it is also
813automatically added to the list of prerequisites. We can therefore omit
814the @samp{.c} files from the prerequisites, provided we omit the recipe.
815
816Here is the entire example, with both of these changes, and a variable
817@code{objects} as suggested above:
818
819@example
820@group
821objects = main.o kbd.o command.o display.o \
822 insert.o search.o files.o utils.o
823
824edit : $(objects)
825 cc -o edit $(objects)
826
827main.o : defs.h
828kbd.o : defs.h command.h
829command.o : defs.h command.h
830display.o : defs.h buffer.h
831insert.o : defs.h buffer.h
832search.o : defs.h buffer.h
833files.o : defs.h buffer.h command.h
834utils.o : defs.h
835
836.PHONY : clean
837clean :
838 rm edit $(objects)
839@end group
840@end example
841
842@noindent
843This is how we would write the makefile in actual practice. (The
844complications associated with @samp{clean} are described elsewhere.
845See @ref{Phony Targets}, and @ref{Errors, ,Errors in Recipes}.)
846
847Because implicit rules are so convenient, they are important. You
848will see them used frequently.@refill
849
850@node Combine By Prerequisite, Cleanup, make Deduces, Introduction
851@section Another Style of Makefile
852@cindex combining rules by prerequisite
853
854When the objects of a makefile are created only by implicit rules, an
855alternative style of makefile is possible. In this style of makefile,
856you group entries by their prerequisites instead of by their targets.
857Here is what one looks like:
858
859@example
860@group
861objects = main.o kbd.o command.o display.o \
862 insert.o search.o files.o utils.o
863
864edit : $(objects)
865 cc -o edit $(objects)
866
867$(objects) : defs.h
868kbd.o command.o files.o : command.h
869display.o insert.o search.o files.o : buffer.h
870@end group
871@end example
872
873@noindent
874Here @file{defs.h} is given as a prerequisite of all the object files;
875@file{command.h} and @file{buffer.h} are prerequisites of the specific
876object files listed for them.
877
878Whether this is better is a matter of taste: it is more compact, but some
879people dislike it because they find it clearer to put all the information
880about each target in one place.
881
882@node Cleanup, , Combine By Prerequisite, Introduction
883@section Rules for Cleaning the Directory
884@cindex cleaning up
885@cindex removing, to clean up
886
887Compiling a program is not the only thing you might want to write rules
888for. Makefiles commonly tell how to do a few other things besides
889compiling a program: for example, how to delete all the object files
890and executables so that the directory is @samp{clean}.
891
892@cindex @code{clean} target
893Here is how we
894could write a @code{make} rule for cleaning our example editor:
895
896@example
897@group
898clean:
899 rm edit $(objects)
900@end group
901@end example
902
903In practice, we might want to write the rule in a somewhat more
904complicated manner to handle unanticipated situations. We would do this:
905
906@example
907@group
908.PHONY : clean
909clean :
910 -rm edit $(objects)
911@end group
912@end example
913
914@noindent
915This prevents @code{make} from getting confused by an actual file
916called @file{clean} and causes it to continue in spite of errors from
917@code{rm}. (See @ref{Phony Targets}, and @ref{Errors, ,Errors in
918Recipes}.)
919
920@noindent
921A rule such as this should not be placed at the beginning of the
922makefile, because we do not want it to run by default! Thus, in the
923example makefile, we want the rule for @code{edit}, which recompiles
924the editor, to remain the default goal.
925
926Since @code{clean} is not a prerequisite of @code{edit}, this rule will not
927run at all if we give the command @samp{make} with no arguments. In
928order to make the rule run, we have to type @samp{make clean}.
929@xref{Running, ,How to Run @code{make}}.
930
931@node Makefiles, Rules, Introduction, Top
932@chapter Writing Makefiles
933
934@cindex makefile, how to write
935The information that tells @code{make} how to recompile a system comes from
936reading a data base called the @dfn{makefile}.
937
938@menu
939* Makefile Contents:: What makefiles contain.
940* Makefile Names:: How to name your makefile.
941* Include:: How one makefile can use another makefile.
942* MAKEFILES Variable:: The environment can specify extra makefiles.
943* Remaking Makefiles:: How makefiles get remade.
944* Overriding Makefiles:: How to override part of one makefile
945 with another makefile.
946* Reading Makefiles:: How makefiles are parsed.
947* Secondary Expansion:: How and when secondary expansion is performed.
948@end menu
949
950@node Makefile Contents, Makefile Names, Makefiles, Makefiles
951@section What Makefiles Contain
952
953Makefiles contain five kinds of things: @dfn{explicit rules},
954@dfn{implicit rules}, @dfn{variable definitions}, @dfn{directives},
955and @dfn{comments}. Rules, variables, and directives are described at
956length in later chapters.@refill
957
958@itemize @bullet
959@cindex rule, explicit, definition of
960@cindex explicit rule, definition of
961@item
962An @dfn{explicit rule} says when and how to remake one or more files,
963called the rule's @dfn{targets}. It lists the other files that the
964targets depend on, called the @dfn{prerequisites} of the target, and
965may also give a recipe to use to create or update the targets.
966@xref{Rules, ,Writing Rules}.
967
968@cindex rule, implicit, definition of
969@cindex implicit rule, definition of
970@item
971An @dfn{implicit rule} says when and how to remake a class of files
972based on their names. It describes how a target may depend on a file
973with a name similar to the target and gives a recipe to create or
974update such a target. @xref{Implicit Rules, ,Using Implicit Rules}.
975
976@cindex variable definition
977@item
978A @dfn{variable definition} is a line that specifies a text string
979value for a variable that can be substituted into the text later. The
980simple makefile example shows a variable definition for @code{objects}
981as a list of all object files (@pxref{Variables Simplify, , Variables
982Make Makefiles Simpler}).
983
984@cindex directive
985@item
986A @dfn{directive} is an instruction for @code{make} to do something
987special while reading the makefile. These include:
988
989@itemize @bullet
990@item
991Reading another makefile (@pxref{Include, ,Including Other Makefiles}).
992
993@item
994Deciding (based on the values of variables) whether to use or
995ignore a part of the makefile (@pxref{Conditionals, ,Conditional Parts of Makefiles}).
996
997@item
998Defining a variable from a verbatim string containing multiple lines
999(@pxref{Multi-Line, ,Defining Multi-Line Variables}).
1000@end itemize
1001
1002@cindex comments, in makefile
1003@cindex @code{#} (comments), in makefile
1004@item
1005@samp{#} in a line of a makefile starts a @dfn{comment}. It and the
1006rest of the line are ignored, except that a trailing backslash not
1007escaped by another backslash will continue the comment across multiple
1008lines. A line containing just a comment (with perhaps spaces before
1009it) is effectively blank, and is ignored. If you want a literal
1010@code{#}, escape it with a backslash (e.g., @code{\#}). Comments may
1011appear on any line in the makefile, although they are treated
1012specially in certain situations.
1013
1014You cannot use comments within variable references or function calls:
1015any instance of @code{#} will be treated literally (rather than as the
1016start of a comment) inside a variable reference or function call.
1017
1018Comments within a recipe are passed to the shell, just as with any
1019other recipe text. The shell decides how to interpret it: whether or
1020not this is a comment is up to the shell.
1021
1022Within a @code{define} directive, comments are not ignored during the
1023definition of the variable, but rather kept intact in the value of the
1024variable. When the variable is expanded they will either be treated
1025as @code{make} comments or as recipe text, depending on the context in
1026which the variable is evaluated.
1027@end itemize
1028
1029@node Makefile Names, Include, Makefile Contents, Makefiles
1030@section What Name to Give Your Makefile
1031@cindex makefile name
1032@cindex name of makefile
1033@cindex default makefile name
1034@cindex file name of makefile
1035
1036@c following paragraph rewritten to avoid overfull hbox
1037By default, when @code{make} looks for the makefile, it tries the
1038following names, in order: @file{GNUmakefile}, @file{makefile}
1039and @file{Makefile}.@refill
1040@findex Makefile
1041@findex GNUmakefile
1042@findex makefile
1043
1044@cindex @code{README}
1045Normally you should call your makefile either @file{makefile} or
1046@file{Makefile}. (We recommend @file{Makefile} because it appears
1047prominently near the beginning of a directory listing, right near other
1048important files such as @file{README}.) The first name checked,
1049@file{GNUmakefile}, is not recommended for most makefiles. You should
1050use this name if you have a makefile that is specific to GNU
1051@code{make}, and will not be understood by other versions of
1052@code{make}. Other @code{make} programs look for @file{makefile} and
1053@file{Makefile}, but not @file{GNUmakefile}.
1054
1055If @code{make} finds none of these names, it does not use any makefile.
1056Then you must specify a goal with a command argument, and @code{make}
1057will attempt to figure out how to remake it using only its built-in
1058implicit rules. @xref{Implicit Rules, ,Using Implicit Rules}.
1059
1060@cindex @code{-f}
1061@cindex @code{--file}
1062@cindex @code{--makefile}
1063If you want to use a nonstandard name for your makefile, you can specify
1064the makefile name with the @samp{-f} or @samp{--file} option. The
1065arguments @w{@samp{-f @var{name}}} or @w{@samp{--file=@var{name}}} tell
1066@code{make} to read the file @var{name} as the makefile. If you use
1067more than one @samp{-f} or @samp{--file} option, you can specify several
1068makefiles. All the makefiles are effectively concatenated in the order
1069specified. The default makefile names @file{GNUmakefile},
1070@file{makefile} and @file{Makefile} are not checked automatically if you
1071specify @samp{-f} or @samp{--file}.@refill
1072@cindex specifying makefile name
1073@cindex makefile name, how to specify
1074@cindex name of makefile, how to specify
1075@cindex file name of makefile, how to specify
1076
1077@node Include, MAKEFILES Variable, Makefile Names, Makefiles
1078@section Including Other Makefiles
1079@cindex including other makefiles
1080@cindex makefile, including
1081
1082@findex include
1083The @code{include} directive tells @code{make} to suspend reading the
1084current makefile and read one or more other makefiles before continuing.
1085The directive is a line in the makefile that looks like this:
1086
1087@example
1088include @var{filenames}@dots{}
1089@end example
1090
1091@noindent
1092@var{filenames} can contain shell file name patterns. If
1093@var{filenames} is empty, nothing is included and no error is printed.
1094@cindex shell file name pattern (in @code{include})
1095@cindex shell wildcards (in @code{include})
1096@cindex wildcard, in @code{include}
1097
1098Extra spaces are allowed and ignored at the beginning of the line, but
1099the first character must not be a tab (or the value of
1100@code{.RECIPEPREFIX})---if the line begins with a tab, it will be
1101considered a recipe line. Whitespace is required between
1102@code{include} and the file names, and between file names; extra
1103whitespace is ignored there and at the end of the directive. A
1104comment starting with @samp{#} is allowed at the end of the line. If
1105the file names contain any variable or function references, they are
1106expanded. @xref{Using Variables, ,How to Use Variables}.
1107
1108For example, if you have three @file{.mk} files, @file{a.mk},
1109@file{b.mk}, and @file{c.mk}, and @code{$(bar)} expands to
1110@code{bish bash}, then the following expression
1111
1112@example
1113include foo *.mk $(bar)
1114@end example
1115
1116is equivalent to
1117
1118@example
1119include foo a.mk b.mk c.mk bish bash
1120@end example
1121
1122When @code{make} processes an @code{include} directive, it suspends
1123reading of the containing makefile and reads from each listed file in
1124turn. When that is finished, @code{make} resumes reading the
1125makefile in which the directive appears.
1126
1127One occasion for using @code{include} directives is when several programs,
1128handled by individual makefiles in various directories, need to use a
1129common set of variable definitions
1130(@pxref{Setting, ,Setting Variables}) or pattern rules
1131(@pxref{Pattern Rules, ,Defining and Redefining Pattern Rules}).
1132
1133Another such occasion is when you want to generate prerequisites from
1134source files automatically; the prerequisites can be put in a file that
1135is included by the main makefile. This practice is generally cleaner
1136than that of somehow appending the prerequisites to the end of the main
1137makefile as has been traditionally done with other versions of
1138@code{make}. @xref{Automatic Prerequisites}.
1139@cindex prerequisites, automatic generation
1140@cindex automatic generation of prerequisites
1141@cindex generating prerequisites automatically
1142
1143@cindex @code{-I}
1144@cindex @code{--include-dir}
1145@cindex included makefiles, default directories
1146@cindex default directories for included makefiles
1147@findex /usr/gnu/include
1148@findex /usr/local/include
1149@findex /usr/include
1150If the specified name does not start with a slash, and the file is not
1151found in the current directory, several other directories are searched.
1152First, any directories you have specified with the @samp{-I} or
1153@samp{--include-dir} option are searched
1154(@pxref{Options Summary, ,Summary of Options}).
1155Then the following directories (if they exist)
1156are searched, in this order:
1157@file{@var{prefix}/include} (normally @file{/usr/local/include}
1158@footnote{GNU Make compiled for MS-DOS and MS-Windows behaves as if
1159@var{prefix} has been defined to be the root of the DJGPP tree
1160hierarchy.})
1161@file{/usr/gnu/include},
1162@file{/usr/local/include}, @file{/usr/include}.
1163
1164If an included makefile cannot be found in any of these directories, a
1165warning message is generated, but it is not an immediately fatal error;
1166processing of the makefile containing the @code{include} continues.
1167Once it has finished reading makefiles, @code{make} will try to remake
1168any that are out of date or don't exist.
1169@xref{Remaking Makefiles, ,How Makefiles Are Remade}.
1170Only after it has tried to find a way to remake a makefile and failed,
1171will @code{make} diagnose the missing makefile as a fatal error.
1172
1173If you want @code{make} to simply ignore a makefile which does not exist
1174or cannot be remade, with no error message, use the @w{@code{-include}}
1175directive instead of @code{include}, like this:
1176
1177@example
1178-include @var{filenames}@dots{}
1179@end example
1180
1181This acts like @code{include} in every way except that there is no
1182error (not even a warning) if any of the @var{filenames} (or any
1183prerequisites of any of the @var{filenames}) do not exist or cannot be
1184remade.
1185
1186For compatibility with some other @code{make} implementations,
1187@code{sinclude} is another name for @w{@code{-include}}.
1188
1189@node MAKEFILES Variable, Remaking Makefiles, Include, Makefiles
1190@section The Variable @code{MAKEFILES}
1191@cindex makefile, and @code{MAKEFILES} variable
1192@cindex including (@code{MAKEFILES} variable)
1193
1194@vindex MAKEFILES
1195If the environment variable @code{MAKEFILES} is defined, @code{make}
1196considers its value as a list of names (separated by whitespace) of
1197additional makefiles to be read before the others. This works much
1198like the @code{include} directive: various directories are searched
1199for those files (@pxref{Include, ,Including Other Makefiles}). In
1200addition, the default goal is never taken from one of these makefiles
1201(or any makefile included by them) and it is not an error if the files
1202listed in @code{MAKEFILES} are not found.@refill
1203
1204@cindex recursion, and @code{MAKEFILES} variable
1205The main use of @code{MAKEFILES} is in communication between recursive
1206invocations of @code{make} (@pxref{Recursion, ,Recursive Use of
1207@code{make}}). It usually is not desirable to set the environment
1208variable before a top-level invocation of @code{make}, because it is
1209usually better not to mess with a makefile from outside. However, if
1210you are running @code{make} without a specific makefile, a makefile in
1211@code{MAKEFILES} can do useful things to help the built-in implicit
1212rules work better, such as defining search paths (@pxref{Directory Search}).
1213
1214Some users are tempted to set @code{MAKEFILES} in the environment
1215automatically on login, and program makefiles to expect this to be done.
1216This is a very bad idea, because such makefiles will fail to work if run by
1217anyone else. It is much better to write explicit @code{include} directives
1218in the makefiles. @xref{Include, , Including Other Makefiles}.
1219
1220@node Remaking Makefiles, Overriding Makefiles, MAKEFILES Variable, Makefiles
1221@section How Makefiles Are Remade
1222
1223@cindex updating makefiles
1224@cindex remaking makefiles
1225@cindex makefile, remaking of
1226Sometimes makefiles can be remade from other files, such as RCS or SCCS
1227files. If a makefile can be remade from other files, you probably want
1228@code{make} to get an up-to-date version of the makefile to read in.
1229
1230To this end, after reading in all makefiles, @code{make} will consider
1231each as a goal target and attempt to update it. If a makefile has a
1232rule which says how to update it (found either in that very makefile or
1233in another one) or if an implicit rule applies to it (@pxref{Implicit
1234Rules, ,Using Implicit Rules}), it will be updated if necessary. After
1235all makefiles have been checked, if any have actually been changed,
1236@code{make} starts with a clean slate and reads all the makefiles over
1237again. (It will also attempt to update each of them over again, but
1238normally this will not change them again, since they are already up to
1239date.)@refill
1240
1241If you know that one or more of your makefiles cannot be remade and
1242you want to keep @code{make} from performing an implicit rule search
1243on them, perhaps for efficiency reasons, you can use any normal method
1244of preventing implicit rule lookup to do so. For example, you can
1245write an explicit rule with the makefile as the target, and an empty
1246recipe (@pxref{Empty Recipes, ,Using Empty Recipes}).
1247
1248If the makefiles specify a double-colon rule to remake a file with
1249a recipe but no prerequisites, that file will always be remade
1250(@pxref{Double-Colon}). In the case of makefiles, a makefile that has a
1251double-colon rule with a recipe but no prerequisites will be remade every
1252time @code{make} is run, and then again after @code{make} starts over
1253and reads the makefiles in again. This would cause an infinite loop:
1254@code{make} would constantly remake the makefile, and never do anything
1255else. So, to avoid this, @code{make} will @strong{not} attempt to
1256remake makefiles which are specified as targets of a double-colon rule
1257with a recipe but no prerequisites.@refill
1258
1259If you do not specify any makefiles to be read with @samp{-f} or
1260@samp{--file} options, @code{make} will try the default makefile names;
1261@pxref{Makefile Names, ,What Name to Give Your Makefile}. Unlike
1262makefiles explicitly requested with @samp{-f} or @samp{--file} options,
1263@code{make} is not certain that these makefiles should exist. However,
1264if a default makefile does not exist but can be created by running
1265@code{make} rules, you probably want the rules to be run so that the
1266makefile can be used.
1267
1268Therefore, if none of the default makefiles exists, @code{make} will try
1269to make each of them in the same order in which they are searched for
1270(@pxref{Makefile Names, ,What Name to Give Your Makefile})
1271until it succeeds in making one, or it runs out of names to try. Note
1272that it is not an error if @code{make} cannot find or make any makefile;
1273a makefile is not always necessary.@refill
1274
1275When you use the @samp{-t} or @samp{--touch} option
1276(@pxref{Instead of Execution, ,Instead of Executing Recipes}),
1277you would not want to use an out-of-date makefile to decide which
1278targets to touch. So the @samp{-t} option has no effect on updating
1279makefiles; they are really updated even if @samp{-t} is specified.
1280Likewise, @samp{-q} (or @samp{--question}) and @samp{-n} (or
1281@samp{--just-print}) do not prevent updating of makefiles, because an
1282out-of-date makefile would result in the wrong output for other targets.
1283Thus, @samp{make -f mfile -n foo} will update @file{mfile}, read it in,
1284and then print the recipe to update @file{foo} and its prerequisites
1285without running it. The recipe printed for @file{foo} will be the one
1286specified in the updated contents of @file{mfile}.
1287
1288However, on occasion you might actually wish to prevent updating of even
1289the makefiles. You can do this by specifying the makefiles as goals in
1290the command line as well as specifying them as makefiles. When the
1291makefile name is specified explicitly as a goal, the options @samp{-t}
1292and so on do apply to them.
1293
1294Thus, @samp{make -f mfile -n mfile foo} would read the makefile
1295@file{mfile}, print the recipe needed to update it without actually
1296running it, and then print the recipe needed to update @file{foo}
1297without running that. The recipe for @file{foo} will be the one
1298specified by the existing contents of @file{mfile}.
1299
1300@node Overriding Makefiles, Reading Makefiles, Remaking Makefiles, Makefiles
1301@section Overriding Part of Another Makefile
1302
1303@cindex overriding makefiles
1304@cindex makefile, overriding
1305Sometimes it is useful to have a makefile that is mostly just like
1306another makefile. You can often use the @samp{include} directive to
1307include one in the other, and add more targets or variable definitions.
1308However, it is illegal for two makefiles to give different recipes for
1309the same target. But there is another way.
1310
1311@cindex match-anything rule, used to override
1312In the containing makefile (the one that wants to include the other),
1313you can use a match-anything pattern rule to say that to remake any
1314target that cannot be made from the information in the containing
1315makefile, @code{make} should look in another makefile.
1316@xref{Pattern Rules}, for more information on pattern rules.
1317
1318For example, if you have a makefile called @file{Makefile} that says how
1319to make the target @samp{foo} (and other targets), you can write a
1320makefile called @file{GNUmakefile} that contains:
1321
1322@example
1323foo:
1324 frobnicate > foo
1325
1326%: force
1327 @@$(MAKE) -f Makefile $@@
1328force: ;
1329@end example
1330
1331If you say @samp{make foo}, @code{make} will find @file{GNUmakefile},
1332read it, and see that to make @file{foo}, it needs to run the recipe
1333@samp{frobnicate > foo}. If you say @samp{make bar}, @code{make} will
1334find no way to make @file{bar} in @file{GNUmakefile}, so it will use the
1335recipe from the pattern rule: @samp{make -f Makefile bar}. If
1336@file{Makefile} provides a rule for updating @file{bar}, @code{make}
1337will apply the rule. And likewise for any other target that
1338@file{GNUmakefile} does not say how to make.
1339
1340The way this works is that the pattern rule has a pattern of just
1341@samp{%}, so it matches any target whatever. The rule specifies a
1342prerequisite @file{force}, to guarantee that the recipe will be run even
1343if the target file already exists. We give the @file{force} target an
1344empty recipe to prevent @code{make} from searching for an implicit rule to
1345build it---otherwise it would apply the same match-anything rule to
1346@file{force} itself and create a prerequisite loop!
1347
1348@node Reading Makefiles, Secondary Expansion, Overriding Makefiles, Makefiles
1349@section How @code{make} Reads a Makefile
1350@cindex reading makefiles
1351@cindex makefile, parsing
1352
1353GNU @code{make} does its work in two distinct phases. During the first
1354phase it reads all the makefiles, included makefiles, etc. and
1355internalizes all the variables and their values, implicit and explicit
1356rules, and constructs a dependency graph of all the targets and their
1357prerequisites. During the second phase, @code{make} uses these internal
1358structures to determine what targets will need to be rebuilt and to
1359invoke the rules necessary to do so.
1360
1361It's important to understand this two-phase approach because it has a
1362direct impact on how variable and function expansion happens; this is
1363often a source of some confusion when writing makefiles. Here we will
1364present a summary of the phases in which expansion happens for different
1365constructs within the makefile. We say that expansion is
1366@dfn{immediate} if it happens during the first phase: in this case
1367@code{make} will expand any variables or functions in that section of a
1368construct as the makefile is parsed. We say that expansion is
1369@dfn{deferred} if expansion is not performed immediately. Expansion of
1370a deferred construct is not performed until either the construct appears
1371later in an immediate context, or until the second phase.
1372
1373You may not be familiar with some of these constructs yet. You can
1374reference this section as you become familiar with them, in later
1375chapters.
1376
1377@subheading Variable Assignment
1378@cindex +=, expansion
1379@cindex =, expansion
1380@cindex ?=, expansion
1381@cindex +=, expansion
1382@cindex define, expansion
1383
1384Variable definitions are parsed as follows:
1385
1386@example
1387@var{immediate} = @var{deferred}
1388@var{immediate} ?= @var{deferred}
1389@var{immediate} := @var{immediate}
1390@var{immediate} += @var{deferred} or @var{immediate}
1391
1392define @var{immediate}
1393 @var{deferred}
1394endef
1395
1396define @var{immediate} =
1397 @var{deferred}
1398endef
1399
1400define @var{immediate} ?=
1401 @var{deferred}
1402endef
1403
1404define @var{immediate} :=
1405 @var{immediate}
1406endef
1407
1408define @var{immediate} +=
1409 @var{deferred} or @var{immediate}
1410endef
1411@end example
1412
1413For the append operator, @samp{+=}, the right-hand side is considered
1414immediate if the variable was previously set as a simple variable
1415(@samp{:=}), and deferred otherwise.
1416
1417@subheading Conditional Directives
1418@cindex ifdef, expansion
1419@cindex ifeq, expansion
1420@cindex ifndef, expansion
1421@cindex ifneq, expansion
1422
1423Conditional directives are parsed immediately. This means, for
1424example, that automatic variables cannot be used in conditional
1425directives, as automatic variables are not set until the recipe for
1426that rule is invoked. If you need to use automatic variables in a
1427conditional directive you @emph{must} move the condition into the
1428recipe and use shell conditional syntax instead.
1429
1430@subheading Rule Definition
1431@cindex target, expansion
1432@cindex prerequisite, expansion
1433@cindex implicit rule, expansion
1434@cindex pattern rule, expansion
1435@cindex explicit rule, expansion
1436
1437A rule is always expanded the same way, regardless of the form:
1438
1439@example
1440@var{immediate} : @var{immediate} ; @var{deferred}
1441 @var{deferred}
1442@end example
1443
1444That is, the target and prerequisite sections are expanded immediately,
1445and the recipe used to construct the target is always deferred. This
1446general rule is true for explicit rules, pattern rules, suffix rules,
1447static pattern rules, and simple prerequisite definitions.
1448
1449@node Secondary Expansion, , Reading Makefiles, Makefiles
1450@section Secondary Expansion
1451@cindex secondary expansion
1452@cindex expansion, secondary
1453
1454@findex .SECONDEXPANSION
1455In the previous section we learned that GNU @code{make} works in two
1456distinct phases: a read-in phase and a target-update phase
1457(@pxref{Reading Makefiles, , How @code{make} Reads a Makefile}). GNU
1458make also has the ability to enable a @emph{second expansion} of the
1459prerequisites (only) for some or all targets defined in the makefile.
1460In order for this second expansion to occur, the special target
1461@code{.SECONDEXPANSION} must be defined before the first prerequisite
1462list that makes use of this feature.
1463
1464If that special target is defined then in between the two phases
1465mentioned above, right at the end of the read-in phase, all the
1466prerequisites of the targets defined after the special target are
1467expanded a @emph{second time}. In most circumstances this secondary
1468expansion will have no effect, since all variable and function
1469references will have been expanded during the initial parsing of the
1470makefiles. In order to take advantage of the secondary expansion
1471phase of the parser, then, it's necessary to @emph{escape} the
1472variable or function reference in the makefile. In this case the
1473first expansion merely un-escapes the reference but doesn't expand it,
1474and expansion is left to the secondary expansion phase. For example,
1475consider this makefile:
1476
1477@example
1478.SECONDEXPANSION:
1479ONEVAR = onefile
1480TWOVAR = twofile
1481myfile: $(ONEVAR) $$(TWOVAR)
1482@end example
1483
1484After the first expansion phase the prerequisites list of the
1485@file{myfile} target will be @code{onefile} and @code{$(TWOVAR)}; the
1486first (unescaped) variable reference to @var{ONEVAR} is expanded,
1487while the second (escaped) variable reference is simply unescaped,
1488without being recognized as a variable reference. Now during the
1489secondary expansion the first word is expanded again but since it
1490contains no variable or function references it remains the static
1491value @file{onefile}, while the second word is now a normal reference
1492to the variable @var{TWOVAR}, which is expanded to the value
1493@file{twofile}. The final result is that there are two prerequisites,
1494@file{onefile} and @file{twofile}.
1495
1496Obviously, this is not a very interesting case since the same result
1497could more easily have been achieved simply by having both variables
1498appear, unescaped, in the prerequisites list. One difference becomes
1499apparent if the variables are reset; consider this example:
1500
1501@example
1502.SECONDEXPANSION:
1503AVAR = top
1504onefile: $(AVAR)
1505twofile: $$(AVAR)
1506AVAR = bottom
1507@end example
1508
1509Here the prerequisite of @file{onefile} will be expanded immediately,
1510and resolve to the value @file{top}, while the prerequisite of
1511@file{twofile} will not be full expanded until the secondary expansion
1512and yield a value of @file{bottom}.
1513
1514This is marginally more exciting, but the true power of this feature
1515only becomes apparent when you discover that secondary expansions
1516always take place within the scope of the automatic variables for that
1517target. This means that you can use variables such as @code{$@@},
1518@code{$*}, etc. during the second expansion and they will have their
1519expected values, just as in the recipe. All you have to do is defer
1520the expansion by escaping the @code{$}. Also, secondary expansion
1521occurs for both explicit and implicit (pattern) rules. Knowing this,
1522the possible uses for this feature increase dramatically. For
1523example:
1524
1525@example
1526.SECONDEXPANSION:
1527main_OBJS := main.o try.o test.o
1528lib_OBJS := lib.o api.o
1529
1530main lib: $$($$@@_OBJS)
1531@end example
1532
1533Here, after the initial expansion the prerequisites of both the
1534@file{main} and @file{lib} targets will be @code{$($@@_OBJS)}. During
1535the secondary expansion, the @code{$@@} variable is set to the name of
1536the target and so the expansion for the @file{main} target will yield
1537@code{$(main_OBJS)}, or @code{main.o try.o test.o}, while the
1538secondary expansion for the @file{lib} target will yield
1539@code{$(lib_OBJS)}, or @code{lib.o api.o}.
1540
1541You can also mix in functions here, as long as they are properly escaped:
1542
1543@example
1544main_SRCS := main.c try.c test.c
1545lib_SRCS := lib.c api.c
1546
1547.SECONDEXPANSION:
1548main lib: $$(patsubst %.c,%.o,$$($$@@_SRCS))
1549@end example
1550
1551This version allows users to specify source files rather than object
1552files, but gives the same resulting prerequisites list as the previous
1553example.
1554
1555Evaluation of automatic variables during the secondary expansion
1556phase, especially of the target name variable @code{$$@@}, behaves
1557similarly to evaluation within recipes. However, there are some
1558subtle differences and ``corner cases'' which come into play for the
1559different types of rule definitions that @code{make} understands. The
1560subtleties of using the different automatic variables are described
1561below.
1562
1563@subheading Secondary Expansion of Explicit Rules
1564@cindex secondary expansion and explicit rules
1565@cindex explicit rules, secondary expansion of
1566
1567During the secondary expansion of explicit rules, @code{$$@@} and
1568@code{$$%} evaluate, respectively, to the file name of the target and,
1569when the target is an archive member, the target member name. The
1570@code{$$<} variable evaluates to the first prerequisite in the first
1571rule for this target. @code{$$^} and @code{$$+} evaluate to the list
1572of all prerequisites of rules @emph{that have already appeared} for
1573the same target (@code{$$+} with repetitions and @code{$$^}
1574without). The following example will help illustrate these behaviors:
1575
1576@example
1577.SECONDEXPANSION:
1578
1579foo: foo.1 bar.1 $$< $$^ $$+ # line #1
1580
1581foo: foo.2 bar.2 $$< $$^ $$+ # line #2
1582
1583foo: foo.3 bar.3 $$< $$^ $$+ # line #3
1584@end example
1585
1586In the first prerequisite list, all three variables (@code{$$<},
1587@code{$$^}, and @code{$$+}) expand to the empty string. In the
1588second, they will have values @code{foo.1}, @code{foo.1 bar.1}, and
1589@code{foo.1 bar.1} respectively. In the third they will have values
1590@code{foo.1}, @code{foo.1 bar.1 foo.2 bar.2}, and @code{foo.1 bar.1
1591foo.2 bar.2 foo.1 foo.1 bar.1 foo.1 bar.1} respectively.
1592
1593Rules undergo secondary expansion in makefile order, except that
1594the rule with the recipe is always evaluated last.
1595
1596The variables @code{$$?} and @code{$$*} are not available and expand
1597to the empty string.
1598
1599@subheading Secondary Expansion of Static Pattern Rules
1600@cindex secondary expansion and static pattern rules
1601@cindex static pattern rules, secondary expansion of
1602
1603Rules for secondary expansion of static pattern rules are identical to
1604those for explicit rules, above, with one exception: for static
1605pattern rules the @code{$$*} variable is set to the pattern stem. As
1606with explicit rules, @code{$$?} is not available and expands to the
1607empty string.
1608
1609@subheading Secondary Expansion of Implicit Rules
1610@cindex secondary expansion and implicit rules
1611@cindex implicit rules, secondary expansion of
1612
1613As @code{make} searches for an implicit rule, it substitutes the stem
1614and then performs secondary expansion for every rule with a matching
1615target pattern. The value of the automatic variables is derived in
1616the same fashion as for static pattern rules. As an example:
1617
1618@example
1619.SECONDEXPANSION:
1620
1621foo: bar
1622
1623foo foz: fo%: bo%
1624
1625%oo: $$< $$^ $$+ $$*
1626@end example
1627
1628When the implicit rule is tried for target @file{foo}, @code{$$<}
1629expands to @file{bar}, @code{$$^} expands to @file{bar boo},
1630@code{$$+} also expands to @file{bar boo}, and @code{$$*} expands to
1631@file{f}.
1632
1633Note that the directory prefix (D), as described in @ref{Implicit Rule
1634Search, ,Implicit Rule Search Algorithm}, is appended (after
1635expansion) to all the patterns in the prerequisites list. As an
1636example:
1637
1638@example
1639.SECONDEXPANSION:
1640
1641/tmp/foo.o:
1642
1643%.o: $$(addsuffix /%.c,foo bar) foo.h
1644@end example
1645
1646The prerequisite list after the secondary expansion and directory
1647prefix reconstruction will be @file{/tmp/foo/foo.c /tmp/var/bar/foo.c
1648foo.h}. If you are not interested in this reconstruction, you can use
1649@code{$$*} instead of @code{%} in the prerequisites list.
1650
1651@node Rules, Recipes, Makefiles, Top
1652@chapter Writing Rules
1653@cindex writing rules
1654@cindex rule, how to write
1655@cindex target
1656@cindex prerequisite
1657
1658A @dfn{rule} appears in the makefile and says when and how to remake
1659certain files, called the rule's @dfn{targets} (most often only one per rule).
1660It lists the other files that are the @dfn{prerequisites} of the target, and
1661the @dfn{recipe} to use to create or update the target.
1662
1663@cindex default goal
1664@cindex goal, default
1665The order of rules is not significant, except for determining the
1666@dfn{default goal}: the target for @code{make} to consider, if you do
1667not otherwise specify one. The default goal is the target of the first
1668rule in the first makefile. If the first rule has multiple targets,
1669only the first target is taken as the default. There are two
1670exceptions: a target starting with a period is not a default unless it
1671contains one or more slashes, @samp{/}, as well; and, a target that
1672defines a pattern rule has no effect on the default goal.
1673(@xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.)
1674
1675Therefore, we usually write the makefile so that the first rule is the
1676one for compiling the entire program or all the programs described by
1677the makefile (often with a target called @samp{all}).
1678@xref{Goals, ,Arguments to Specify the Goals}.
1679
1680@menu
1681* Rule Example:: An example explained.
1682* Rule Syntax:: General syntax explained.
1683* Prerequisite Types:: There are two types of prerequisites.
1684* Wildcards:: Using wildcard characters such as `*'.
1685* Directory Search:: Searching other directories for source files.
1686* Phony Targets:: Using a target that is not a real file's name.
1687* Force Targets:: You can use a target without recipes
1688 or prerequisites to mark other targets
1689 as phony.
1690* Empty Targets:: When only the date matters and the
1691 files are empty.
1692* Special Targets:: Targets with special built-in meanings.
1693* Multiple Targets:: When to make use of several targets in a rule.
1694* Multiple Rules:: How to use several rules with the same target.
1695* Static Pattern:: Static pattern rules apply to multiple targets
1696 and can vary the prerequisites according to
1697 the target name.
1698* Double-Colon:: How to use a special kind of rule to allow
1699 several independent rules for one target.
1700* Automatic Prerequisites:: How to automatically generate rules giving
1701 prerequisites from source files themselves.
1702@end menu
1703
1704@ifnottex
1705@node Rule Example, Rule Syntax, Rules, Rules
1706@section Rule Example
1707
1708Here is an example of a rule:
1709
1710@example
1711foo.o : foo.c defs.h # module for twiddling the frobs
1712 cc -c -g foo.c
1713@end example
1714
1715Its target is @file{foo.o} and its prerequisites are @file{foo.c} and
1716@file{defs.h}. It has one command in the recipe: @samp{cc -c -g foo.c}.
1717The recipe starts with a tab to identify it as a recipe.
1718
1719This rule says two things:
1720
1721@itemize @bullet
1722@item
1723How to decide whether @file{foo.o} is out of date: it is out of date
1724if it does not exist, or if either @file{foo.c} or @file{defs.h} is
1725more recent than it.
1726
1727@item
1728How to update the file @file{foo.o}: by running @code{cc} as stated.
1729The recipe does not explicitly mention @file{defs.h}, but we presume
1730that @file{foo.c} includes it, and that that is why @file{defs.h} was
1731added to the prerequisites.
1732@end itemize
1733@end ifnottex
1734
1735@node Rule Syntax, Prerequisite Types, Rule Example, Rules
1736@section Rule Syntax
1737
1738@cindex rule syntax
1739@cindex syntax of rules
1740In general, a rule looks like this:
1741
1742@example
1743@var{targets} : @var{prerequisites}
1744 @var{recipe}
1745 @dots{}
1746@end example
1747
1748@noindent
1749or like this:
1750
1751@example
1752@var{targets} : @var{prerequisites} ; @var{recipe}
1753 @var{recipe}
1754 @dots{}
1755@end example
1756
1757@cindex targets
1758@cindex rule targets
1759The @var{targets} are file names, separated by spaces. Wildcard
1760characters may be used (@pxref{Wildcards, ,Using Wildcard Characters
1761in File Names}) and a name of the form @file{@var{a}(@var{m})}
1762represents member @var{m} in archive file @var{a}
1763(@pxref{Archive Members, ,Archive Members as Targets}).
1764Usually there is only one
1765target per rule, but occasionally there is a reason to have more
1766(@pxref{Multiple Targets, , Multiple Targets in a Rule}).@refill
1767
1768@cindex recipes
1769@cindex tab character (in commands)
1770The @var{recipe} lines start with a tab character (or the first
1771character in the value of the @code{.RECIPEPREFIX} variable;
1772@pxref{Special Variables}). The first recipe line may appear on the line
1773after the prerequisites, with a tab character, or may appear on the
1774same line, with a semicolon. Either way, the effect is the same.
1775There are other differences in the syntax of recipes.
1776@xref{Recipes, ,Writing Recipes in Rules}.
1777
1778@cindex dollar sign (@code{$}), in rules
1779@cindex @code{$}, in rules
1780@cindex rules, and @code{$}
1781Because dollar signs are used to start @code{make} variable
1782references, if you really want a dollar sign in a target or
1783prerequisite you must write two of them, @samp{$$} (@pxref{Using
1784Variables, ,How to Use Variables}). If you have enabled secondary
1785expansion (@pxref{Secondary Expansion}) and you want a literal dollar
1786sign in the prerequisites list, you must actually write @emph{four}
1787dollar signs (@samp{$$$$}).
1788
1789You may split a long line by inserting a backslash followed by a
1790newline, but this is not required, as @code{make} places no limit on
1791the length of a line in a makefile.
1792
1793A rule tells @code{make} two things: when the targets are out of date,
1794and how to update them when necessary.
1795
1796@cindex prerequisites
1797@cindex rule prerequisites
1798The criterion for being out of date is specified in terms of the
1799@var{prerequisites}, which consist of file names separated by spaces.
1800(Wildcards and archive members (@pxref{Archives}) are allowed here too.)
1801A target is out of date if it does not exist or if it is older than any
1802of the prerequisites (by comparison of last-modification times). The
1803idea is that the contents of the target file are computed based on
1804information in the prerequisites, so if any of the prerequisites changes,
1805the contents of the existing target file are no longer necessarily
1806valid.
1807
1808How to update is specified by a @var{recipe}. This is one or more
1809lines to be executed by the shell (normally @samp{sh}), but with some
1810extra features (@pxref{Recipes, ,Writing Recipes in Rules}).
1811
1812@node Prerequisite Types, Wildcards, Rule Syntax, Rules
1813@comment node-name, next, previous, up
1814@section Types of Prerequisites
1815@cindex prerequisite types
1816@cindex types of prerequisites
1817
1818@cindex prerequisites, normal
1819@cindex normal prerequisites
1820@cindex prerequisites, order-only
1821@cindex order-only prerequisites
1822There are actually two different types of prerequisites understood by
1823GNU @code{make}: normal prerequisites such as described in the
1824previous section, and @dfn{order-only} prerequisites. A normal
1825prerequisite makes two statements: first, it imposes an order in which
1826recipes will be invoked: the recipes for all prerequisites of a target
1827will be completed before the recipe for the target is run. Second, it
1828imposes a dependency relationship: if any prerequisite is newer than
1829the target, then the target is considered out-of-date and must be
1830rebuilt.
1831
1832Normally, this is exactly what you want: if a target's prerequisite is
1833updated, then the target should also be updated.
1834
1835Occasionally, however, you have a situation where you want to impose a
1836specific ordering on the rules to be invoked @emph{without} forcing
1837the target to be updated if one of those rules is executed. In that
1838case, you want to define @dfn{order-only} prerequisites. Order-only
1839prerequisites can be specified by placing a pipe symbol (@code{|})
1840in the prerequisites list: any prerequisites to the left of the pipe
1841symbol are normal; any prerequisites to the right are order-only:
1842
1843@example
1844@var{targets} : @var{normal-prerequisites} | @var{order-only-prerequisites}
1845@end example
1846
1847The normal prerequisites section may of course be empty. Also, you
1848may still declare multiple lines of prerequisites for the same target:
1849they are appended appropriately (normal prerequisites are appended to
1850the list of normal prerequisites; order-only prerequisites are
1851appended to the list of order-only prerequisites). Note that if you
1852declare the same file to be both a normal and an order-only
1853prerequisite, the normal prerequisite takes precedence (since they
1854have a strict superset of the behavior of an order-only prerequisite).
1855
1856Consider an example where your targets are to be placed in a separate
1857directory, and that directory might not exist before @code{make} is
1858run. In this situation, you want the directory to be created before
1859any targets are placed into it but, because the timestamps on
1860directories change whenever a file is added, removed, or renamed, we
1861certainly don't want to rebuild all the targets whenever the
1862directory's timestamp changes. One way to manage this is with
1863order-only prerequisites: make the directory an order-only
1864prerequisite on all the targets:
1865
1866@example
1867OBJDIR := objdir
1868OBJS := $(addprefix $(OBJDIR)/,foo.o bar.o baz.o)
1869
1870$(OBJDIR)/%.o : %.c
1871 $(COMPILE.c) $(OUTPUT_OPTION) $<
1872
1873all: $(OBJS)
1874
1875$(OBJS): | $(OBJDIR)
1876
1877$(OBJDIR):
1878 mkdir $(OBJDIR)
1879@end example
1880
1881Now the rule to create the @file{objdir} directory will be run, if
1882needed, before any @samp{.o} is built, but no @samp{.o} will be built
1883because the @file{objdir} directory timestamp changed.
1884
1885@node Wildcards, Directory Search, Prerequisite Types, Rules
1886@section Using Wildcard Characters in File Names
1887@cindex wildcard
1888@cindex file name with wildcards
1889@cindex globbing (wildcards)
1890
1891@cindex @code{*} (wildcard character)
1892@cindex @code{?} (wildcard character)
1893@cindex @code{[@dots{}]} (wildcard characters)
1894A single file name can specify many files using @dfn{wildcard characters}.
1895The wildcard characters in @code{make} are @samp{*}, @samp{?} and
1896@samp{[@dots{}]}, the same as in the Bourne shell. For example, @file{*.c}
1897specifies a list of all the files (in the working directory) whose names
1898end in @samp{.c}.@refill
1899
1900@cindex @code{~} (tilde)
1901@cindex tilde (@code{~})
1902@cindex home directory
1903The character @samp{~} at the beginning of a file name also has special
1904significance. If alone, or followed by a slash, it represents your home
1905directory. For example @file{~/bin} expands to @file{/home/you/bin}.
1906If the @samp{~} is followed by a word, the string represents the home
1907directory of the user named by that word. For example @file{~john/bin}
1908expands to @file{/home/john/bin}. On systems which don't have a home
1909directory for each user (such as MS-DOS or MS-Windows), this
1910functionality can be simulated by setting the environment variable
1911@var{HOME}.@refill
1912
1913Wildcard expansion is performed by @code{make} automatically in
1914targets and in prerequisites. In recipes, the shell is responsible
1915for wildcard expansion. In other contexts, wildcard expansion happens
1916only if you request it explicitly with the @code{wildcard} function.
1917
1918The special significance of a wildcard character can be turned off by
1919preceding it with a backslash. Thus, @file{foo\*bar} would refer to a
1920specific file whose name consists of @samp{foo}, an asterisk, and
1921@samp{bar}.@refill
1922
1923@menu
1924* Wildcard Examples:: Several examples
1925* Wildcard Pitfall:: Problems to avoid.
1926* Wildcard Function:: How to cause wildcard expansion where
1927 it does not normally take place.
1928@end menu
1929
1930@node Wildcard Examples, Wildcard Pitfall, Wildcards, Wildcards
1931@subsection Wildcard Examples
1932
1933Wildcards can be used in the recipe of a rule, where they are expanded
1934by the shell. For example, here is a rule to delete all the object files:
1935
1936@example
1937@group
1938clean:
1939 rm -f *.o
1940@end group
1941@end example
1942@cindex @code{rm} (shell command)
1943
1944Wildcards are also useful in the prerequisites of a rule. With the
1945following rule in the makefile, @samp{make print} will print all the
1946@samp{.c} files that have changed since the last time you printed them:
1947
1948@example
1949print: *.c
1950 lpr -p $?
1951 touch print
1952@end example
1953
1954@cindex @code{print} target
1955@cindex @code{lpr} (shell command)
1956@cindex @code{touch} (shell command)
1957@noindent
1958This rule uses @file{print} as an empty target file; see @ref{Empty
1959Targets, ,Empty Target Files to Record Events}. (The automatic variable
1960@samp{$?} is used to print only those files that have changed; see
1961@ref{Automatic Variables}.)@refill
1962
1963Wildcard expansion does not happen when you define a variable. Thus, if
1964you write this:
1965
1966@example
1967objects = *.o
1968@end example
1969
1970@noindent
1971then the value of the variable @code{objects} is the actual string
1972@samp{*.o}. However, if you use the value of @code{objects} in a
1973target or prerequisite, wildcard expansion will take place there. If
1974you use the value of @code{objects} in a recipe, the shell may perform
1975wildcard expansion when the recipe runs. To set @code{objects} to the
1976expansion, instead use:
1977
1978@example
1979objects := $(wildcard *.o)
1980@end example
1981
1982@noindent
1983@xref{Wildcard Function}.
1984
1985@node Wildcard Pitfall, Wildcard Function, Wildcard Examples, Wildcards
1986@subsection Pitfalls of Using Wildcards
1987@cindex wildcard pitfalls
1988@cindex pitfalls of wildcards
1989@cindex mistakes with wildcards
1990@cindex errors with wildcards
1991@cindex problems with wildcards
1992
1993Now here is an example of a naive way of using wildcard expansion, that
1994does not do what you would intend. Suppose you would like to say that the
1995executable file @file{foo} is made from all the object files in the
1996directory, and you write this:
1997
1998@example
1999objects = *.o
2000
2001foo : $(objects)
2002 cc -o foo $(CFLAGS) $(objects)
2003@end example
2004
2005@noindent
2006The value of @code{objects} is the actual string @samp{*.o}. Wildcard
2007expansion happens in the rule for @file{foo}, so that each @emph{existing}
2008@samp{.o} file becomes a prerequisite of @file{foo} and will be recompiled if
2009necessary.
2010
2011But what if you delete all the @samp{.o} files? When a wildcard matches
2012no files, it is left as it is, so then @file{foo} will depend on the
2013oddly-named file @file{*.o}. Since no such file is likely to exist,
2014@code{make} will give you an error saying it cannot figure out how to
2015make @file{*.o}. This is not what you want!
2016
2017Actually it is possible to obtain the desired result with wildcard
2018expansion, but you need more sophisticated techniques, including the
2019@code{wildcard} function and string substitution.
2020@ifnottex
2021@xref{Wildcard Function, ,The Function @code{wildcard}}.
2022@end ifnottex
2023@iftex
2024These are described in the following section.
2025@end iftex
2026
2027@cindex wildcards and MS-DOS/MS-Windows backslashes
2028@cindex backslashes in pathnames and wildcard expansion
2029
2030Microsoft operating systems (MS-DOS and MS-Windows) use backslashes to
2031separate directories in pathnames, like so:
2032
2033@example
2034 c:\foo\bar\baz.c
2035@end example
2036
2037This is equivalent to the Unix-style @file{c:/foo/bar/baz.c} (the
2038@file{c:} part is the so-called drive letter). When @code{make} runs on
2039these systems, it supports backslashes as well as the Unix-style forward
2040slashes in pathnames. However, this support does @emph{not} include the
2041wildcard expansion, where backslash is a quote character. Therefore,
2042you @emph{must} use Unix-style slashes in these cases.
2043
2044
2045@node Wildcard Function, , Wildcard Pitfall, Wildcards
2046@subsection The Function @code{wildcard}
2047@findex wildcard
2048
2049Wildcard expansion happens automatically in rules. But wildcard expansion
2050does not normally take place when a variable is set, or inside the
2051arguments of a function. If you want to do wildcard expansion in such
2052places, you need to use the @code{wildcard} function, like this:
2053
2054@example
2055$(wildcard @var{pattern}@dots{})
2056@end example
2057
2058@noindent
2059This string, used anywhere in a makefile, is replaced by a
2060space-separated list of names of existing files that match one of the
2061given file name patterns. If no existing file name matches a pattern,
2062then that pattern is omitted from the output of the @code{wildcard}
2063function. Note that this is different from how unmatched wildcards
2064behave in rules, where they are used verbatim rather than ignored
2065(@pxref{Wildcard Pitfall}).
2066
2067One use of the @code{wildcard} function is to get a list of all the C source
2068files in a directory, like this:
2069
2070@example
2071$(wildcard *.c)
2072@end example
2073
2074We can change the list of C source files into a list of object files by
2075replacing the @samp{.c} suffix with @samp{.o} in the result, like this:
2076
2077@example
2078$(patsubst %.c,%.o,$(wildcard *.c))
2079@end example
2080
2081@noindent
2082(Here we have used another function, @code{patsubst}.
2083@xref{Text Functions, ,Functions for String Substitution and Analysis}.)@refill
2084
2085Thus, a makefile to compile all C source files in the directory and then
2086link them together could be written as follows:
2087
2088@example
2089objects := $(patsubst %.c,%.o,$(wildcard *.c))
2090
2091foo : $(objects)
2092 cc -o foo $(objects)
2093@end example
2094
2095@noindent
2096(This takes advantage of the implicit rule for compiling C programs, so
2097there is no need to write explicit rules for compiling the files.
2098@xref{Flavors, ,The Two Flavors of Variables}, for an explanation of
2099@samp{:=}, which is a variant of @samp{=}.)
2100
2101@node Directory Search, Phony Targets, Wildcards, Rules
2102@section Searching Directories for Prerequisites
2103@vindex VPATH
2104@findex vpath
2105@cindex vpath
2106@cindex search path for prerequisites (@code{VPATH})
2107@cindex directory search (@code{VPATH})
2108
2109For large systems, it is often desirable to put sources in a separate
2110directory from the binaries. The @dfn{directory search} features of
2111@code{make} facilitate this by searching several directories
2112automatically to find a prerequisite. When you redistribute the files
2113among directories, you do not need to change the individual rules,
2114just the search paths.
2115
2116@menu
2117* General Search:: Specifying a search path that applies
2118 to every prerequisite.
2119* Selective Search:: Specifying a search path
2120 for a specified class of names.
2121* Search Algorithm:: When and how search paths are applied.
2122* Recipes/Search:: How to write recipes that work together
2123 with search paths.
2124* Implicit/Search:: How search paths affect implicit rules.
2125* Libraries/Search:: Directory search for link libraries.
2126@end menu
2127
2128@node General Search, Selective Search, Directory Search, Directory Search
2129@subsection @code{VPATH}: Search Path for All Prerequisites
2130@vindex VPATH
2131
2132The value of the @code{make} variable @code{VPATH} specifies a list of
2133directories that @code{make} should search. Most often, the
2134directories are expected to contain prerequisite files that are not in the
2135current directory; however, @code{make} uses @code{VPATH} as a search
2136list for both prerequisites and targets of rules.
2137
2138Thus, if a file that is listed as a target or prerequisite does not exist
2139in the current directory, @code{make} searches the directories listed in
2140@code{VPATH} for a file with that name. If a file is found in one of
2141them, that file may become the prerequisite (see below). Rules may then
2142specify the names of files in the prerequisite list as if they all
2143existed in the current directory. @xref{Recipes/Search, ,Writing Recipes with Directory Search}.
2144
2145In the @code{VPATH} variable, directory names are separated by colons or
2146blanks. The order in which directories are listed is the order followed
2147by @code{make} in its search. (On MS-DOS and MS-Windows, semi-colons
2148are used as separators of directory names in @code{VPATH}, since the
2149colon can be used in the pathname itself, after the drive letter.)
2150
2151For example,
2152
2153@example
2154VPATH = src:../headers
2155@end example
2156
2157@noindent
2158specifies a path containing two directories, @file{src} and
2159@file{../headers}, which @code{make} searches in that order.
2160
2161With this value of @code{VPATH}, the following rule,
2162
2163@example
2164foo.o : foo.c
2165@end example
2166
2167@noindent
2168is interpreted as if it were written like this:
2169
2170@example
2171foo.o : src/foo.c
2172@end example
2173
2174@noindent
2175assuming the file @file{foo.c} does not exist in the current directory but
2176is found in the directory @file{src}.
2177
2178@node Selective Search, Search Algorithm, General Search, Directory Search
2179@subsection The @code{vpath} Directive
2180@findex vpath
2181
2182Similar to the @code{VPATH} variable, but more selective, is the
2183@code{vpath} directive (note lower case), which allows you to specify a
2184search path for a particular class of file names: those that match a
2185particular pattern. Thus you can supply certain search directories for
2186one class of file names and other directories (or none) for other file
2187names.
2188
2189There are three forms of the @code{vpath} directive:
2190
2191@table @code
2192@item vpath @var{pattern} @var{directories}
2193Specify the search path @var{directories} for file names that match
2194@var{pattern}.
2195
2196The search path, @var{directories}, is a list of directories to be
2197searched, separated by colons (semi-colons on MS-DOS and MS-Windows) or
2198blanks, just like the search path used in the @code{VPATH} variable.
2199
2200@item vpath @var{pattern}
2201Clear out the search path associated with @var{pattern}.
2202
2203@c Extra blank line makes sure this gets two lines.
2204@item vpath
2205
2206Clear all search paths previously specified with @code{vpath} directives.
2207@end table
2208
2209A @code{vpath} pattern is a string containing a @samp{%} character. The
2210string must match the file name of a prerequisite that is being searched
2211for, the @samp{%} character matching any sequence of zero or more
2212characters (as in pattern rules; @pxref{Pattern Rules, ,Defining and
2213Redefining Pattern Rules}). For example, @code{%.h} matches files that
2214end in @code{.h}. (If there is no @samp{%}, the pattern must match the
2215prerequisite exactly, which is not useful very often.)
2216
2217@cindex @code{%}, quoting in @code{vpath}
2218@cindex @code{%}, quoting with @code{\} (backslash)
2219@cindex @code{\} (backslash), to quote @code{%}
2220@cindex backslash (@code{\}), to quote @code{%}
2221@cindex quoting @code{%}, in @code{vpath}
2222@samp{%} characters in a @code{vpath} directive's pattern can be quoted
2223with preceding backslashes (@samp{\}). Backslashes that would otherwise
2224quote @samp{%} characters can be quoted with more backslashes.
2225Backslashes that quote @samp{%} characters or other backslashes are
2226removed from the pattern before it is compared to file names. Backslashes
2227that are not in danger of quoting @samp{%} characters go unmolested.@refill
2228
2229When a prerequisite fails to exist in the current directory, if the
2230@var{pattern} in a @code{vpath} directive matches the name of the
2231prerequisite file, then the @var{directories} in that directive are searched
2232just like (and before) the directories in the @code{VPATH} variable.
2233
2234For example,
2235
2236@example
2237vpath %.h ../headers
2238@end example
2239
2240@noindent
2241tells @code{make} to look for any prerequisite whose name ends in @file{.h}
2242in the directory @file{../headers} if the file is not found in the current
2243directory.
2244
2245If several @code{vpath} patterns match the prerequisite file's name, then
2246@code{make} processes each matching @code{vpath} directive one by one,
2247searching all the directories mentioned in each directive. @code{make}
2248handles multiple @code{vpath} directives in the order in which they
2249appear in the makefile; multiple directives with the same pattern are
2250independent of each other.
2251
2252@need 750
2253Thus,
2254
2255@example
2256@group
2257vpath %.c foo
2258vpath % blish
2259vpath %.c bar
2260@end group
2261@end example
2262
2263@noindent
2264will look for a file ending in @samp{.c} in @file{foo}, then
2265@file{blish}, then @file{bar}, while
2266
2267@example
2268@group
2269vpath %.c foo:bar
2270vpath % blish
2271@end group
2272@end example
2273
2274@noindent
2275will look for a file ending in @samp{.c} in @file{foo}, then
2276@file{bar}, then @file{blish}.
2277
2278@node Search Algorithm, Recipes/Search, Selective Search, Directory Search
2279@subsection How Directory Searches are Performed
2280@cindex algorithm for directory search
2281@cindex directory search algorithm
2282
2283When a prerequisite is found through directory search, regardless of type
2284(general or selective), the pathname located may not be the one that
2285@code{make} actually provides you in the prerequisite list. Sometimes
2286the path discovered through directory search is thrown away.
2287
2288The algorithm @code{make} uses to decide whether to keep or abandon a
2289path found via directory search is as follows:
2290
2291@enumerate
2292@item
2293If a target file does not exist at the path specified in the makefile,
2294directory search is performed.
2295
2296@item
2297If the directory search is successful, that path is kept and this file
2298is tentatively stored as the target.
2299
2300@item
2301All prerequisites of this target are examined using this same method.
2302
2303@item
2304After processing the prerequisites, the target may or may not need to be
2305rebuilt:
2306
2307@enumerate a
2308@item
2309If the target does @emph{not} need to be rebuilt, the path to the file
2310found during directory search is used for any prerequisite lists which
2311contain this target. In short, if @code{make} doesn't need to rebuild
2312the target then you use the path found via directory search.
2313
2314@item
2315If the target @emph{does} need to be rebuilt (is out-of-date), the
2316pathname found during directory search is @emph{thrown away}, and the
2317target is rebuilt using the file name specified in the makefile. In
2318short, if @code{make} must rebuild, then the target is rebuilt locally,
2319not in the directory found via directory search.
2320@end enumerate
2321@end enumerate
2322
2323This algorithm may seem complex, but in practice it is quite often
2324exactly what you want.
2325
2326@cindex traditional directory search (GPATH)
2327@cindex directory search, traditional (GPATH)
2328Other versions of @code{make} use a simpler algorithm: if the file does
2329not exist, and it is found via directory search, then that pathname is
2330always used whether or not the target needs to be built. Thus, if the
2331target is rebuilt it is created at the pathname discovered during
2332directory search.
2333
2334@vindex GPATH
2335If, in fact, this is the behavior you want for some or all of your
2336directories, you can use the @code{GPATH} variable to indicate this to
2337@code{make}.
2338
2339@code{GPATH} has the same syntax and format as @code{VPATH} (that is, a
2340space- or colon-delimited list of pathnames). If an out-of-date target
2341is found by directory search in a directory that also appears in
2342@code{GPATH}, then that pathname is not thrown away. The target is
2343rebuilt using the expanded path.
2344
2345@node Recipes/Search, Implicit/Search, Search Algorithm, Directory Search
2346@subsection Writing Recipes with Directory Search
2347@cindex recipes, and directory search
2348@cindex directory search (@code{VPATH}), and recipes
2349
2350When a prerequisite is found in another directory through directory search,
2351this cannot change the recipe of the rule; they will execute as written.
2352Therefore, you must write the recipe with care so that it will look for
2353the prerequisite in the directory where @code{make} finds it.
2354
2355This is done with the @dfn{automatic variables} such as @samp{$^}
2356(@pxref{Automatic Variables}).
2357For instance, the value of @samp{$^} is a
2358list of all the prerequisites of the rule, including the names of
2359the directories in which they were found, and the value of
2360@samp{$@@} is the target. Thus:@refill
2361
2362@example
2363foo.o : foo.c
2364 cc -c $(CFLAGS) $^ -o $@@
2365@end example
2366
2367@noindent
2368(The variable @code{CFLAGS} exists so you can specify flags for C
2369compilation by implicit rules; we use it here for consistency so it will
2370affect all C compilations uniformly;
2371@pxref{Implicit Variables, ,Variables Used by Implicit Rules}.)
2372
2373Often the prerequisites include header files as well, which you do not
2374want to mention in the recipe. The automatic variable @samp{$<} is
2375just the first prerequisite:
2376
2377@example
2378VPATH = src:../headers
2379foo.o : foo.c defs.h hack.h
2380 cc -c $(CFLAGS) $< -o $@@
2381@end example
2382
2383@node Implicit/Search, Libraries/Search, Recipes/Search, Directory Search
2384@subsection Directory Search and Implicit Rules
2385@cindex @code{VPATH}, and implicit rules
2386@cindex directory search (@code{VPATH}), and implicit rules
2387@cindex search path for prerequisites (@code{VPATH}), and implicit rules
2388@cindex implicit rule, and directory search
2389@cindex implicit rule, and @code{VPATH}
2390@cindex rule, implicit, and directory search
2391@cindex rule, implicit, and @code{VPATH}
2392
2393The search through the directories specified in @code{VPATH} or with
2394@code{vpath} also happens during consideration of implicit rules
2395(@pxref{Implicit Rules, ,Using Implicit Rules}).
2396
2397For example, when a file @file{foo.o} has no explicit rule, @code{make}
2398considers implicit rules, such as the built-in rule to compile
2399@file{foo.c} if that file exists. If such a file is lacking in the
2400current directory, the appropriate directories are searched for it. If
2401@file{foo.c} exists (or is mentioned in the makefile) in any of the
2402directories, the implicit rule for C compilation is applied.
2403
2404The recipes of implicit rules normally use automatic variables as a
2405matter of necessity; consequently they will use the file names found by
2406directory search with no extra effort.
2407
2408@node Libraries/Search, , Implicit/Search, Directory Search
2409@subsection Directory Search for Link Libraries
2410@cindex link libraries, and directory search
2411@cindex libraries for linking, directory search
2412@cindex directory search (@code{VPATH}), and link libraries
2413@cindex @code{VPATH}, and link libraries
2414@cindex search path for prerequisites (@code{VPATH}), and link libraries
2415@cindex @code{-l} (library search)
2416@cindex link libraries, patterns matching
2417@cindex @code{.LIBPATTERNS}, and link libraries
2418@vindex .LIBPATTERNS
2419
2420Directory search applies in a special way to libraries used with the
2421linker. This special feature comes into play when you write a prerequisite
2422whose name is of the form @samp{-l@var{name}}. (You can tell something
2423strange is going on here because the prerequisite is normally the name of a
2424file, and the @emph{file name} of a library generally looks like
2425@file{lib@var{name}.a}, not like @samp{-l@var{name}}.)@refill
2426
2427When a prerequisite's name has the form @samp{-l@var{name}}, @code{make}
2428handles it specially by searching for the file @file{lib@var{name}.so},
2429and, if it is not found, for the file @file{lib@var{name}.a} in the current
2430directory, in directories specified by matching @code{vpath}
2431search paths and the @code{VPATH} search path, and then in the
2432directories @file{/lib}, @file{/usr/lib}, and @file{@var{prefix}/lib}
2433(normally @file{/usr/local/lib}, but MS-DOS/MS-Windows versions of
2434@code{make} behave as if @var{prefix} is defined to be the root of the
2435DJGPP installation tree).
2436
2437For example, if there is a @file{/usr/lib/libcurses.a} library on your
2438system (and no @file{/usr/lib/libcurses.so} file), then
2439
2440@example
2441@group
2442foo : foo.c -lcurses
2443 cc $^ -o $@@
2444@end group
2445@end example
2446
2447@noindent
2448would cause the command @samp{cc foo.c /usr/lib/libcurses.a -o foo} to
2449be executed when @file{foo} is older than @file{foo.c} or than
2450@file{/usr/lib/libcurses.a}.@refill
2451
2452Although the default set of files to be searched for is
2453@file{lib@var{name}.so} and @file{lib@var{name}.a}, this is customizable
2454via the @code{.LIBPATTERNS} variable. Each word in the value of this
2455variable is a pattern string. When a prerequisite like
2456@samp{-l@var{name}} is seen, @code{make} will replace the percent in
2457each pattern in the list with @var{name} and perform the above directory
2458searches using each library filename.
2459
2460The default value for @code{.LIBPATTERNS} is @samp{lib%.so lib%.a},
2461which provides the default behavior described above.
2462
2463You can turn off link library expansion completely by setting this
2464variable to an empty value.
2465
2466@node Phony Targets, Force Targets, Directory Search, Rules
2467@section Phony Targets
2468@cindex phony targets
2469@cindex targets, phony
2470@cindex targets without a file
2471
2472A phony target is one that is not really the name of a file; rather it
2473is just a name for a recipe to be executed when you make an explicit
2474request. There are two reasons to use a phony target: to avoid a
2475conflict with a file of the same name, and to improve performance.
2476
2477If you write a rule whose recipe will not create the target file, the
2478recipe will be executed every time the target comes up for remaking.
2479Here is an example:
2480
2481@example
2482@group
2483clean:
2484 rm *.o temp
2485@end group
2486@end example
2487
2488@noindent
2489Because the @code{rm} command does not create a file named @file{clean},
2490probably no such file will ever exist. Therefore, the @code{rm} command
2491will be executed every time you say @samp{make clean}.
2492@cindex @code{rm} (shell command)
2493
2494@findex .PHONY
2495The phony target will cease to work if anything ever does create a file
2496named @file{clean} in this directory. Since it has no prerequisites, the
2497file @file{clean} would inevitably be considered up to date, and its
2498recipe would not be executed. To avoid this problem, you can explicitly
2499declare the target to be phony, using the special target @code{.PHONY}
2500(@pxref{Special Targets, ,Special Built-in Target Names}) as follows:
2501
2502@example
2503.PHONY : clean
2504@end example
2505
2506@noindent
2507Once this is done, @samp{make clean} will run the recipe regardless of
2508whether there is a file named @file{clean}.
2509
2510Since it knows that phony targets do not name actual files that could be
2511remade from other files, @code{make} skips the implicit rule search for
2512phony targets (@pxref{Implicit Rules}). This is why declaring a target
2513phony is good for performance, even if you are not worried about the
2514actual file existing.
2515
2516Thus, you first write the line that states that @code{clean} is a
2517phony target, then you write the rule, like this:
2518
2519@example
2520@group
2521.PHONY: clean
2522clean:
2523 rm *.o temp
2524@end group
2525@end example
2526
2527Another example of the usefulness of phony targets is in conjunction
2528with recursive invocations of @code{make} (for more information, see
2529@ref{Recursion, ,Recursive Use of @code{make}}). In this case the
2530makefile will often contain a variable which lists a number of
2531subdirectories to be built. One way to handle this is with one rule
2532whose recipe is a shell loop over the subdirectories, like this:
2533
2534@example
2535@group
2536SUBDIRS = foo bar baz
2537
2538subdirs:
2539 for dir in $(SUBDIRS); do \
2540 $(MAKE) -C $$dir; \
2541 done
2542@end group
2543@end example
2544
2545There are problems with this method, however. First, any error
2546detected in a submake is ignored by this rule, so it will continue
2547to build the rest of the directories even when one fails. This can be
2548overcome by adding shell commands to note the error and exit, but then
2549it will do so even if @code{make} is invoked with the @code{-k}
2550option, which is unfortunate. Second, and perhaps more importantly,
2551you cannot take advantage of @code{make}'s ability to build targets in
2552parallel (@pxref{Parallel, ,Parallel Execution}), since there is only
2553one rule.
2554
2555By declaring the subdirectories as phony targets (you must do this as
2556the subdirectory obviously always exists; otherwise it won't be built)
2557you can remove these problems:
2558
2559@example
2560@group
2561SUBDIRS = foo bar baz
2562
2563.PHONY: subdirs $(SUBDIRS)
2564
2565subdirs: $(SUBDIRS)
2566
2567$(SUBDIRS):
2568 $(MAKE) -C $@@
2569
2570foo: baz
2571@end group
2572@end example
2573
2574Here we've also declared that the @file{foo} subdirectory cannot be
2575built until after the @file{baz} subdirectory is complete; this kind of
2576relationship declaration is particularly important when attempting
2577parallel builds.
2578
2579A phony target should not be a prerequisite of a real target file; if it
2580is, its recipe will be run every time @code{make} goes to update that
2581file. As long as a phony target is never a prerequisite of a real
2582target, the phony target recipe will be executed only when the phony
2583target is a specified goal (@pxref{Goals, ,Arguments to Specify the
2584Goals}).
2585
2586Phony targets can have prerequisites. When one directory contains multiple
2587programs, it is most convenient to describe all of the programs in one
2588makefile @file{./Makefile}. Since the target remade by default will be the
2589first one in the makefile, it is common to make this a phony target named
2590@samp{all} and give it, as prerequisites, all the individual programs. For
2591example:
2592
2593@example
2594all : prog1 prog2 prog3
2595.PHONY : all
2596
2597prog1 : prog1.o utils.o
2598 cc -o prog1 prog1.o utils.o
2599
2600prog2 : prog2.o
2601 cc -o prog2 prog2.o
2602
2603prog3 : prog3.o sort.o utils.o
2604 cc -o prog3 prog3.o sort.o utils.o
2605@end example
2606
2607@noindent
2608Now you can say just @samp{make} to remake all three programs, or
2609specify as arguments the ones to remake (as in @samp{make prog1
2610prog3}). Phoniness is not inherited: the prerequisites of a phony
2611target are not themselves phony, unless explicitly declared to be so.
2612
2613When one phony target is a prerequisite of another, it serves as a subroutine
2614of the other. For example, here @samp{make cleanall} will delete the
2615object files, the difference files, and the file @file{program}:
2616
2617@example
2618.PHONY: cleanall cleanobj cleandiff
2619
2620cleanall : cleanobj cleandiff
2621 rm program
2622
2623cleanobj :
2624 rm *.o
2625
2626cleandiff :
2627 rm *.diff
2628@end example
2629
2630@node Force Targets, Empty Targets, Phony Targets, Rules
2631@section Rules without Recipes or Prerequisites
2632@cindex force targets
2633@cindex targets, force
2634@cindex @code{FORCE}
2635@cindex rule, no recipe or prerequisites
2636
2637If a rule has no prerequisites or recipe, and the target of the rule
2638is a nonexistent file, then @code{make} imagines this target to have
2639been updated whenever its rule is run. This implies that all targets
2640depending on this one will always have their recipe run.
2641
2642An example will illustrate this:
2643
2644@example
2645@group
2646clean: FORCE
2647 rm $(objects)
2648FORCE:
2649@end group
2650@end example
2651
2652Here the target @samp{FORCE} satisfies the special conditions, so the
2653target @file{clean} that depends on it is forced to run its recipe.
2654There is nothing special about the name @samp{FORCE}, but that is one
2655name commonly used this way.
2656
2657As you can see, using @samp{FORCE} this way has the same results as using
2658@samp{.PHONY: clean}.
2659
2660Using @samp{.PHONY} is more explicit and more efficient. However,
2661other versions of @code{make} do not support @samp{.PHONY}; thus
2662@samp{FORCE} appears in many makefiles. @xref{Phony Targets}.
2663
2664@node Empty Targets, Special Targets, Force Targets, Rules
2665@section Empty Target Files to Record Events
2666@cindex empty targets
2667@cindex targets, empty
2668@cindex recording events with empty targets
2669
2670The @dfn{empty target} is a variant of the phony target; it is used to hold
2671recipes for an action that you request explicitly from time to time.
2672Unlike a phony target, this target file can really exist; but the file's
2673contents do not matter, and usually are empty.
2674
2675The purpose of the empty target file is to record, with its
2676last-modification time, when the rule's recipe was last executed. It
2677does so because one of the commands in the recipe is a @code{touch}
2678command to update the target file.
2679
2680The empty target file should have some prerequisites (otherwise it
2681doesn't make sense). When you ask to remake the empty target, the
2682recipe is executed if any prerequisite is more recent than the target;
2683in other words, if a prerequisite has changed since the last time you
2684remade the target. Here is an example:
2685
2686@example
2687print: foo.c bar.c
2688 lpr -p $?
2689 touch print
2690@end example
2691@cindex @code{print} target
2692@cindex @code{lpr} (shell command)
2693@cindex @code{touch} (shell command)
2694
2695@noindent
2696With this rule, @samp{make print} will execute the @code{lpr} command if
2697either source file has changed since the last @samp{make print}. The
2698automatic variable @samp{$?} is used to print only those files that have
2699changed (@pxref{Automatic Variables}).
2700
2701@node Special Targets, Multiple Targets, Empty Targets, Rules
2702@section Special Built-in Target Names
2703@cindex special targets
2704@cindex built-in special targets
2705@cindex targets, built-in special
2706
2707Certain names have special meanings if they appear as targets.
2708
2709@table @code
2710@findex .PHONY
2711@item .PHONY
2712
2713The prerequisites of the special target @code{.PHONY} are considered to
2714be phony targets. When it is time to consider such a target,
2715@code{make} will run its recipe unconditionally, regardless of
2716whether a file with that name exists or what its last-modification
2717time is. @xref{Phony Targets, ,Phony Targets}.
2718
2719@findex .SUFFIXES
2720@item .SUFFIXES
2721
2722The prerequisites of the special target @code{.SUFFIXES} are the list
2723of suffixes to be used in checking for suffix rules.
2724@xref{Suffix Rules, , Old-Fashioned Suffix Rules}.
2725
2726@findex .DEFAULT
2727@item .DEFAULT
2728
2729The recipe specified for @code{.DEFAULT} is used for any target for
2730which no rules are found (either explicit rules or implicit rules).
2731@xref{Last Resort}. If a @code{.DEFAULT} recipe is specified, every
2732file mentioned as a prerequisite, but not as a target in a rule, will have
2733that recipe executed on its behalf. @xref{Implicit Rule Search,
2734,Implicit Rule Search Algorithm}.
2735
2736@findex .PRECIOUS
2737@item .PRECIOUS
2738@cindex precious targets
2739@cindex preserving with @code{.PRECIOUS}
2740
2741The targets which @code{.PRECIOUS} depends on are given the following
2742special treatment: if @code{make} is killed or interrupted during the
2743execution of their recipes, the target is not deleted.
2744@xref{Interrupts, ,Interrupting or Killing @code{make}}. Also, if the
2745target is an intermediate file, it will not be deleted after it is no
2746longer needed, as is normally done. @xref{Chained Rules, ,Chains of
2747Implicit Rules}. In this latter respect it overlaps with the
2748@code{.SECONDARY} special target.
2749
2750You can also list the target pattern of an implicit rule (such as
2751@samp{%.o}) as a prerequisite file of the special target @code{.PRECIOUS}
2752to preserve intermediate files created by rules whose target patterns
2753match that file's name.
2754
2755@findex .INTERMEDIATE
2756@item .INTERMEDIATE
2757@cindex intermediate targets, explicit
2758
2759The targets which @code{.INTERMEDIATE} depends on are treated as
2760intermediate files. @xref{Chained Rules, ,Chains of Implicit Rules}.
2761@code{.INTERMEDIATE} with no prerequisites has no effect.
2762
2763@findex .SECONDARY
2764@item .SECONDARY
2765@cindex secondary targets
2766@cindex preserving with @code{.SECONDARY}
2767
2768The targets which @code{.SECONDARY} depends on are treated as
2769intermediate files, except that they are never automatically deleted.
2770@xref{Chained Rules, ,Chains of Implicit Rules}.
2771
2772@code{.SECONDARY} with no prerequisites causes all targets to be treated
2773as secondary (i.e., no target is removed because it is considered
2774intermediate).
2775
2776@findex .SECONDEXPANSION
2777@item .SECONDEXPANSION
2778
2779If @code{.SECONDEXPANSION} is mentioned as a target anywhere in the
2780makefile, then all prerequisite lists defined @emph{after} it appears
2781will be expanded a second time after all makefiles have been read in.
2782@xref{Secondary Expansion, ,Secondary Expansion}.
2783
2784@findex .DELETE_ON_ERROR
2785@item .DELETE_ON_ERROR
2786@cindex removing targets on failure
2787
2788If @code{.DELETE_ON_ERROR} is mentioned as a target anywhere in the
2789makefile, then @code{make} will delete the target of a rule if it has
2790changed and its recipe exits with a nonzero exit status, just as it
2791does when it receives a signal. @xref{Errors, ,Errors in Recipes}.
2792
2793@findex .IGNORE
2794@item .IGNORE
2795
2796If you specify prerequisites for @code{.IGNORE}, then @code{make} will
2797ignore errors in execution of the recipe for those particular files.
2798The recipe for @code{.IGNORE} (if any) is ignored.
2799
2800If mentioned as a target with no prerequisites, @code{.IGNORE} says to
2801ignore errors in execution of recipes for all files. This usage of
2802@samp{.IGNORE} is supported only for historical compatibility. Since
2803this affects every recipe in the makefile, it is not very useful; we
2804recommend you use the more selective ways to ignore errors in specific
2805recipes. @xref{Errors, ,Errors in Recipes}.
2806
2807@findex .LOW_RESOLUTION_TIME
2808@item .LOW_RESOLUTION_TIME
2809
2810If you specify prerequisites for @code{.LOW_RESOLUTION_TIME},
2811@command{make} assumes that these files are created by commands that
2812generate low resolution time stamps. The recipe for the
2813@code{.LOW_RESOLUTION_TIME} target are ignored.
2814
2815The high resolution file time stamps of many modern file systems
2816lessen the chance of @command{make} incorrectly concluding that a file
2817is up to date. Unfortunately, some hosts do not provide a way to set a
2818high resolution file time stamp, so commands like @samp{cp -p} that
2819explicitly set a file's time stamp must discard its subsecond part.
2820If a file is created by such a command, you should list it as a
2821prerequisite of @code{.LOW_RESOLUTION_TIME} so that @command{make}
2822does not mistakenly conclude that the file is out of date. For
2823example:
2824
2825@example
2826@group
2827.LOW_RESOLUTION_TIME: dst
2828dst: src
2829 cp -p src dst
2830@end group
2831@end example
2832
2833Since @samp{cp -p} discards the subsecond part of @file{src}'s time
2834stamp, @file{dst} is typically slightly older than @file{src} even when
2835it is up to date. The @code{.LOW_RESOLUTION_TIME} line causes
2836@command{make} to consider @file{dst} to be up to date if its time stamp
2837is at the start of the same second that @file{src}'s time stamp is in.
2838
2839Due to a limitation of the archive format, archive member time stamps
2840are always low resolution. You need not list archive members as
2841prerequisites of @code{.LOW_RESOLUTION_TIME}, as @command{make} does this
2842automatically.
2843
2844@findex .SILENT
2845@item .SILENT
2846
2847If you specify prerequisites for @code{.SILENT}, then @code{make} will
2848not print the recipe used to remake those particular files before
2849executing them. The recipe for @code{.SILENT} is ignored.
2850
2851If mentioned as a target with no prerequisites, @code{.SILENT} says not
2852to print any recipes before executing them. This usage of
2853@samp{.SILENT} is supported only for historical compatibility. We
2854recommend you use the more selective ways to silence specific recipes.
2855@xref{Echoing, ,Recipe Echoing}. If you want to silence all recipes
2856for a particular run of @code{make}, use the @samp{-s} or
2857@w{@samp{--silent}} option (@pxref{Options Summary}).
2858
2859@findex .EXPORT_ALL_VARIABLES
2860@item .EXPORT_ALL_VARIABLES
2861
2862Simply by being mentioned as a target, this tells @code{make} to
2863export all variables to child processes by default.
2864@xref{Variables/Recursion, ,Communicating Variables to a
2865Sub-@code{make}}.
2866
2867@findex .NOTPARALLEL
2868@item .NOTPARALLEL
2869@cindex parallel execution, overriding
2870
2871If @code{.NOTPARALLEL} is mentioned as a target, then this invocation
2872of @code{make} will be run serially, even if the @samp{-j} option is
2873given. Any recursively invoked @code{make} command will still run
2874recipes in parallel (unless its makefile also contains this target).
2875Any prerequisites on this target are ignored.
2876
2877@findex .ONESHELL
2878@item .ONESHELL
2879@cindex recipe execution, single invocation
2880
2881If @code{.ONESHELL} is mentioned as a target, then when a target is
2882built all lines of the recipe will be given to a single invocation of
2883the shell rather than each line being invoked separately
2884(@pxref{Execution, ,Recipe Execution}).
2885
2886@findex .POSIX
2887@item .POSIX
2888@cindex POSIX-conforming mode, setting
2889
2890If @code{.POSIX} is mentioned as a target, then the makefile will be
2891parsed and run in POSIX-conforming mode. This does @emph{not} mean
2892that only POSIX-conforming makefiles will be accepted: all advanced
2893GNU @code{make} features are still available. Rather, this target
2894causes @code{make} to behave as required by POSIX in those areas
2895where @code{make}'s default behavior differs.
2896
2897In particular, if this target is mentioned then recipes will be
2898invoked as if the shell had been passed the @code{-e} flag: the first
2899failing command in a recipe will cause the recipe to fail immediately.
2900@end table
2901
2902Any defined implicit rule suffix also counts as a special target if it
2903appears as a target, and so does the concatenation of two suffixes, such
2904as @samp{.c.o}. These targets are suffix rules, an obsolete way of
2905defining implicit rules (but a way still widely used). In principle, any
2906target name could be special in this way if you break it in two and add
2907both pieces to the suffix list. In practice, suffixes normally begin with
2908@samp{.}, so these special target names also begin with @samp{.}.
2909@xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
2910
2911@node Multiple Targets, Multiple Rules, Special Targets, Rules
2912@section Multiple Targets in a Rule
2913@cindex multiple targets
2914@cindex several targets in a rule
2915@cindex targets, multiple
2916@cindex rule, with multiple targets
2917
2918A rule with multiple targets is equivalent to writing many rules, each with
2919one target, and all identical aside from that. The same recipe applies to
2920all the targets, but its effect may vary because you can substitute the
2921actual target name into the recipe using @samp{$@@}. The rule contributes
2922the same prerequisites to all the targets also.
2923
2924This is useful in two cases.
2925
2926@itemize @bullet
2927@item
2928You want just prerequisites, no recipe. For example:
2929
2930@example
2931kbd.o command.o files.o: command.h
2932@end example
2933
2934@noindent
2935gives an additional prerequisite to each of the three object files
2936mentioned.
2937
2938@item
2939Similar recipes work for all the targets. The recipes do not need
2940to be absolutely identical, since the automatic variable @samp{$@@}
2941can be used to substitute the particular target to be remade into the
2942commands (@pxref{Automatic Variables}). For example:
2943
2944@example
2945@group
2946bigoutput littleoutput : text.g
2947 generate text.g -$(subst output,,$@@) > $@@
2948@end group
2949@end example
2950@findex subst
2951
2952@noindent
2953is equivalent to
2954
2955@example
2956bigoutput : text.g
2957 generate text.g -big > bigoutput
2958littleoutput : text.g
2959 generate text.g -little > littleoutput
2960@end example
2961
2962@noindent
2963Here we assume the hypothetical program @code{generate} makes two
2964types of output, one if given @samp{-big} and one if given
2965@samp{-little}.
2966@xref{Text Functions, ,Functions for String Substitution and Analysis},
2967for an explanation of the @code{subst} function.
2968@end itemize
2969
2970Suppose you would like to vary the prerequisites according to the
2971target, much as the variable @samp{$@@} allows you to vary the recipe.
2972You cannot do this with multiple targets in an ordinary rule, but you
2973can do it with a @dfn{static pattern rule}. @xref{Static Pattern,
2974,Static Pattern Rules}.
2975
2976@node Multiple Rules, Static Pattern, Multiple Targets, Rules
2977@section Multiple Rules for One Target
2978@cindex multiple rules for one target
2979@cindex several rules for one target
2980@cindex rule, multiple for one target
2981@cindex target, multiple rules for one
2982
2983One file can be the target of several rules. All the prerequisites
2984mentioned in all the rules are merged into one list of prerequisites for
2985the target. If the target is older than any prerequisite from any rule,
2986the recipe is executed.
2987
2988There can only be one recipe to be executed for a file. If more than
2989one rule gives a recipe for the same file, @code{make} uses the last
2990one given and prints an error message. (As a special case, if the
2991file's name begins with a dot, no error message is printed. This odd
2992behavior is only for compatibility with other implementations of
2993@code{make}@dots{} you should avoid using it). Occasionally it is
2994useful to have the same target invoke multiple recipes which are
2995defined in different parts of your makefile; you can use
2996@dfn{double-colon rules} (@pxref{Double-Colon}) for this.
2997
2998An extra rule with just prerequisites can be used to give a few extra
2999prerequisites to many files at once. For example, makefiles often
3000have a variable, such as @code{objects}, containing a list of all the
3001compiler output files in the system being made. An easy way to say
3002that all of them must be recompiled if @file{config.h} changes is to
3003write the following:
3004
3005@example
3006objects = foo.o bar.o
3007foo.o : defs.h
3008bar.o : defs.h test.h
3009$(objects) : config.h
3010@end example
3011
3012This could be inserted or taken out without changing the rules that really
3013specify how to make the object files, making it a convenient form to use if
3014you wish to add the additional prerequisite intermittently.
3015
3016Another wrinkle is that the additional prerequisites could be
3017specified with a variable that you set with a command line argument to
3018@code{make} (@pxref{Overriding, ,Overriding Variables}). For example,
3019
3020@example
3021@group
3022extradeps=
3023$(objects) : $(extradeps)
3024@end group
3025@end example
3026
3027@noindent
3028means that the command @samp{make extradeps=foo.h} will consider
3029@file{foo.h} as a prerequisite of each object file, but plain @samp{make}
3030will not.
3031
3032If none of the explicit rules for a target has a recipe, then @code{make}
3033searches for an applicable implicit rule to find one
3034@pxref{Implicit Rules, ,Using Implicit Rules}).
3035
3036@node Static Pattern, Double-Colon, Multiple Rules, Rules
3037@section Static Pattern Rules
3038@cindex static pattern rule
3039@cindex rule, static pattern
3040@cindex pattern rules, static (not implicit)
3041@cindex varying prerequisites
3042@cindex prerequisites, varying (static pattern)
3043
3044@dfn{Static pattern rules} are rules which specify multiple targets and
3045construct the prerequisite names for each target based on the target name.
3046They are more general than ordinary rules with multiple targets because the
3047targets do not have to have identical prerequisites. Their prerequisites must
3048be @emph{analogous}, but not necessarily @emph{identical}.
3049
3050@menu
3051* Static Usage:: The syntax of static pattern rules.
3052* Static versus Implicit:: When are they better than implicit rules?
3053@end menu
3054
3055@node Static Usage, Static versus Implicit, Static Pattern, Static Pattern
3056@subsection Syntax of Static Pattern Rules
3057@cindex static pattern rule, syntax of
3058@cindex pattern rules, static, syntax of
3059
3060Here is the syntax of a static pattern rule:
3061
3062@example
3063@var{targets} @dots{}: @var{target-pattern}: @var{prereq-patterns} @dots{}
3064 @var{recipe}
3065 @dots{}
3066@end example
3067
3068@noindent
3069The @var{targets} list specifies the targets that the rule applies to.
3070The targets can contain wildcard characters, just like the targets of
3071ordinary rules (@pxref{Wildcards, ,Using Wildcard Characters in File
3072Names}).
3073
3074@cindex target pattern, static (not implicit)
3075@cindex stem
3076The @var{target-pattern} and @var{prereq-patterns} say how to compute the
3077prerequisites of each target. Each target is matched against the
3078@var{target-pattern} to extract a part of the target name, called the
3079@dfn{stem}. This stem is substituted into each of the @var{prereq-patterns}
3080to make the prerequisite names (one from each @var{prereq-pattern}).
3081
3082Each pattern normally contains the character @samp{%} just once. When the
3083@var{target-pattern} matches a target, the @samp{%} can match any part of
3084the target name; this part is called the @dfn{stem}. The rest of the
3085pattern must match exactly. For example, the target @file{foo.o} matches
3086the pattern @samp{%.o}, with @samp{foo} as the stem. The targets
3087@file{foo.c} and @file{foo.out} do not match that pattern.@refill
3088
3089@cindex prerequisite pattern, static (not implicit)
3090The prerequisite names for each target are made by substituting the stem
3091for the @samp{%} in each prerequisite pattern. For example, if one
3092prerequisite pattern is @file{%.c}, then substitution of the stem
3093@samp{foo} gives the prerequisite name @file{foo.c}. It is legitimate
3094to write a prerequisite pattern that does not contain @samp{%}; then this
3095prerequisite is the same for all targets.
3096
3097@cindex @code{%}, quoting in static pattern
3098@cindex @code{%}, quoting with @code{\} (backslash)
3099@cindex @code{\} (backslash), to quote @code{%}
3100@cindex backslash (@code{\}), to quote @code{%}
3101@cindex quoting @code{%}, in static pattern
3102@samp{%} characters in pattern rules can be quoted with preceding
3103backslashes (@samp{\}). Backslashes that would otherwise quote @samp{%}
3104characters can be quoted with more backslashes. Backslashes that quote
3105@samp{%} characters or other backslashes are removed from the pattern
3106before it is compared to file names or has a stem substituted into it.
3107Backslashes that are not in danger of quoting @samp{%} characters go
3108unmolested. For example, the pattern @file{the\%weird\\%pattern\\} has
3109@samp{the%weird\} preceding the operative @samp{%} character, and
3110@samp{pattern\\} following it. The final two backslashes are left alone
3111because they cannot affect any @samp{%} character.@refill
3112
3113Here is an example, which compiles each of @file{foo.o} and @file{bar.o}
3114from the corresponding @file{.c} file:
3115
3116@example
3117@group
3118objects = foo.o bar.o
3119
3120all: $(objects)
3121
3122$(objects): %.o: %.c
3123 $(CC) -c $(CFLAGS) $< -o $@@
3124@end group
3125@end example
3126
3127@noindent
3128Here @samp{$<} is the automatic variable that holds the name of the
3129prerequisite and @samp{$@@} is the automatic variable that holds the name
3130of the target; see @ref{Automatic Variables}.
3131
3132Each target specified must match the target pattern; a warning is issued
3133for each target that does not. If you have a list of files, only some of
3134which will match the pattern, you can use the @code{filter} function to
3135remove nonmatching file names (@pxref{Text Functions, ,Functions for String Substitution and Analysis}):
3136
3137@example
3138files = foo.elc bar.o lose.o
3139
3140$(filter %.o,$(files)): %.o: %.c
3141 $(CC) -c $(CFLAGS) $< -o $@@
3142$(filter %.elc,$(files)): %.elc: %.el
3143 emacs -f batch-byte-compile $<
3144@end example
3145
3146@noindent
3147In this example the result of @samp{$(filter %.o,$(files))} is
3148@file{bar.o lose.o}, and the first static pattern rule causes each of
3149these object files to be updated by compiling the corresponding C source
3150file. The result of @w{@samp{$(filter %.elc,$(files))}} is
3151@file{foo.elc}, so that file is made from @file{foo.el}.@refill
3152
3153Another example shows how to use @code{$*} in static pattern rules:
3154@vindex $*@r{, and static pattern}
3155
3156@example
3157@group
3158bigoutput littleoutput : %output : text.g
3159 generate text.g -$* > $@@
3160@end group
3161@end example
3162
3163@noindent
3164When the @code{generate} command is run, @code{$*} will expand to the
3165stem, either @samp{big} or @samp{little}.
3166
3167@node Static versus Implicit, , Static Usage, Static Pattern
3168@subsection Static Pattern Rules versus Implicit Rules
3169@cindex rule, static pattern versus implicit
3170@cindex static pattern rule, versus implicit
3171
3172A static pattern rule has much in common with an implicit rule defined as a
3173pattern rule (@pxref{Pattern Rules, ,Defining and Redefining Pattern Rules}).
3174Both have a pattern for the target and patterns for constructing the
3175names of prerequisites. The difference is in how @code{make} decides
3176@emph{when} the rule applies.
3177
3178An implicit rule @emph{can} apply to any target that matches its pattern,
3179but it @emph{does} apply only when the target has no recipe otherwise
3180specified, and only when the prerequisites can be found. If more than one
3181implicit rule appears applicable, only one applies; the choice depends on
3182the order of rules.
3183
3184By contrast, a static pattern rule applies to the precise list of targets
3185that you specify in the rule. It cannot apply to any other target and it
3186invariably does apply to each of the targets specified. If two conflicting
3187rules apply, and both have recipes, that's an error.
3188
3189The static pattern rule can be better than an implicit rule for these
3190reasons:
3191
3192@itemize @bullet
3193@item
3194You may wish to override the usual implicit rule for a few
3195files whose names cannot be categorized syntactically but
3196can be given in an explicit list.
3197
3198@item
3199If you cannot be sure of the precise contents of the directories
3200you are using, you may not be sure which other irrelevant files
3201might lead @code{make} to use the wrong implicit rule. The choice
3202might depend on the order in which the implicit rule search is done.
3203With static pattern rules, there is no uncertainty: each rule applies
3204to precisely the targets specified.
3205@end itemize
3206
3207@node Double-Colon, Automatic Prerequisites, Static Pattern, Rules
3208@section Double-Colon Rules
3209@cindex double-colon rules
3210@cindex rule, double-colon (@code{::})
3211@cindex multiple rules for one target (@code{::})
3212@cindex @code{::} rules (double-colon)
3213
3214@dfn{Double-colon} rules are explicit rules written with @samp{::}
3215instead of @samp{:} after the target names. They are handled
3216differently from ordinary rules when the same target appears in more
3217than one rule. Pattern rules with double-colons have an entirely
3218different meaning (@pxref{Match-Anything Rules}).
3219
3220When a target appears in multiple rules, all the rules must be the same
3221type: all ordinary, or all double-colon. If they are double-colon, each
3222of them is independent of the others. Each double-colon rule's recipe
3223is executed if the target is older than any prerequisites of that rule.
3224If there are no prerequisites for that rule, its recipe is always
3225executed (even if the target already exists). This can result in
3226executing none, any, or all of the double-colon rules.
3227
3228Double-colon rules with the same target are in fact completely separate
3229from one another. Each double-colon rule is processed individually, just
3230as rules with different targets are processed.
3231
3232The double-colon rules for a target are executed in the order they appear
3233in the makefile. However, the cases where double-colon rules really make
3234sense are those where the order of executing the recipes would not matter.
3235
3236Double-colon rules are somewhat obscure and not often very useful; they
3237provide a mechanism for cases in which the method used to update a target
3238differs depending on which prerequisite files caused the update, and such
3239cases are rare.
3240
3241Each double-colon rule should specify a recipe; if it does not, an
3242implicit rule will be used if one applies.
3243@xref{Implicit Rules, ,Using Implicit Rules}.
3244
3245@node Automatic Prerequisites, , Double-Colon, Rules
3246@section Generating Prerequisites Automatically
3247@cindex prerequisites, automatic generation
3248@cindex automatic generation of prerequisites
3249@cindex generating prerequisites automatically
3250
3251In the makefile for a program, many of the rules you need to write often
3252say only that some object file depends on some header
3253file. For example, if @file{main.c} uses @file{defs.h} via an
3254@code{#include}, you would write:
3255
3256@example
3257main.o: defs.h
3258@end example
3259
3260@noindent
3261You need this rule so that @code{make} knows that it must remake
3262@file{main.o} whenever @file{defs.h} changes. You can see that for a
3263large program you would have to write dozens of such rules in your
3264makefile. And, you must always be very careful to update the makefile
3265every time you add or remove an @code{#include}.
3266@cindex @code{#include}
3267
3268@cindex @code{-M} (to compiler)
3269To avoid this hassle, most modern C compilers can write these rules for
3270you, by looking at the @code{#include} lines in the source files.
3271Usually this is done with the @samp{-M} option to the compiler.
3272For example, the command:
3273
3274@example
3275cc -M main.c
3276@end example
3277
3278@noindent
3279generates the output:
3280
3281@example
3282main.o : main.c defs.h
3283@end example
3284
3285@noindent
3286Thus you no longer have to write all those rules yourself.
3287The compiler will do it for you.
3288
3289Note that such a prerequisite constitutes mentioning @file{main.o} in a
3290makefile, so it can never be considered an intermediate file by implicit
3291rule search. This means that @code{make} won't ever remove the file
3292after using it; @pxref{Chained Rules, ,Chains of Implicit Rules}.
3293
3294@cindex @code{make depend}
3295With old @code{make} programs, it was traditional practice to use this
3296compiler feature to generate prerequisites on demand with a command like
3297@samp{make depend}. That command would create a file @file{depend}
3298containing all the automatically-generated prerequisites; then the
3299makefile could use @code{include} to read them in (@pxref{Include}).
3300
3301In GNU @code{make}, the feature of remaking makefiles makes this
3302practice obsolete---you need never tell @code{make} explicitly to
3303regenerate the prerequisites, because it always regenerates any makefile
3304that is out of date. @xref{Remaking Makefiles}.
3305
3306The practice we recommend for automatic prerequisite generation is to have
3307one makefile corresponding to each source file. For each source file
3308@file{@var{name}.c} there is a makefile @file{@var{name}.d} which lists
3309what files the object file @file{@var{name}.o} depends on. That way
3310only the source files that have changed need to be rescanned to produce
3311the new prerequisites.
3312
3313Here is the pattern rule to generate a file of prerequisites (i.e., a makefile)
3314called @file{@var{name}.d} from a C source file called @file{@var{name}.c}:
3315
3316@smallexample
3317@group
3318%.d: %.c
3319 @@set -e; rm -f $@@; \
3320 $(CC) -M $(CPPFLAGS) $< > $@@.$$$$; \
3321 sed 's,\($*\)\.o[ :]*,\1.o $@@ : ,g' < $@@.$$$$ > $@@; \
3322 rm -f $@@.$$$$
3323@end group
3324@end smallexample
3325
3326@noindent
3327@xref{Pattern Rules}, for information on defining pattern rules. The
3328@samp{-e} flag to the shell causes it to exit immediately if the
3329@code{$(CC)} command (or any other command) fails (exits with a
3330nonzero status).
3331@cindex @code{-e} (shell flag)
3332
3333@cindex @code{-MM} (to GNU compiler)
3334With the GNU C compiler, you may wish to use the @samp{-MM} flag instead
3335of @samp{-M}. This omits prerequisites on system header files.
3336@xref{Preprocessor Options, , Options Controlling the Preprocessor,
3337gcc.info, Using GNU CC}, for details.
3338
3339@cindex @code{sed} (shell command)
3340The purpose of the @code{sed} command is to translate (for example):
3341
3342@example
3343main.o : main.c defs.h
3344@end example
3345
3346@noindent
3347into:
3348
3349@example
3350main.o main.d : main.c defs.h
3351@end example
3352
3353@noindent
3354@cindex @code{.d}
3355This makes each @samp{.d} file depend on all the source and header files
3356that the corresponding @samp{.o} file depends on. @code{make} then
3357knows it must regenerate the prerequisites whenever any of the source or
3358header files changes.
3359
3360Once you've defined the rule to remake the @samp{.d} files,
3361you then use the @code{include} directive to read them all in.
3362@xref{Include}. For example:
3363
3364@example
3365@group
3366sources = foo.c bar.c
3367
3368include $(sources:.c=.d)
3369@end group
3370@end example
3371
3372@noindent
3373(This example uses a substitution variable reference to translate the
3374list of source files @samp{foo.c bar.c} into a list of prerequisite
3375makefiles, @samp{foo.d bar.d}. @xref{Substitution Refs}, for full
3376information on substitution references.) Since the @samp{.d} files are
3377makefiles like any others, @code{make} will remake them as necessary
3378with no further work from you. @xref{Remaking Makefiles}.
3379
3380Note that the @samp{.d} files contain target definitions; you should
3381be sure to place the @code{include} directive @emph{after} the first,
3382default goal in your makefiles or run the risk of having a random
3383object file become the default goal.
3384@xref{How Make Works}.
3385
3386@node Recipes, Using Variables, Rules, Top
3387@chapter Writing Recipes in Rules
3388@cindex recipes
3389@cindex recipes, how to write
3390@cindex writing recipes
3391
3392The recipe of a rule consists of one or more shell command lines to
3393be executed, one at a time, in the order they appear. Typically, the
3394result of executing these commands is that the target of the rule is
3395brought up to date.
3396
3397Users use many different shell programs, but recipes in makefiles are
3398always interpreted by @file{/bin/sh} unless the makefile specifies
3399otherwise. @xref{Execution, ,Recipe Execution}.
3400
3401@menu
3402* Recipe Syntax:: Recipe syntax features and pitfalls.
3403* Echoing:: How to control when recipes are echoed.
3404* Execution:: How recipes are executed.
3405* Parallel:: How recipes can be executed in parallel.
3406* Errors:: What happens after a recipe execution error.
3407* Interrupts:: What happens when a recipe is interrupted.
3408* Recursion:: Invoking @code{make} from makefiles.
3409* Canned Recipes:: Defining canned recipes.
3410* Empty Recipes:: Defining useful, do-nothing recipes.
3411@end menu
3412
3413@node Recipe Syntax, Echoing, Recipes, Recipes
3414@section Recipe Syntax
3415@cindex recipe syntax
3416@cindex syntax of recipe
3417
3418Makefiles have the unusual property that there are really two distinct
3419syntaxes in one file. Most of the makefile uses @code{make} syntax
3420(@pxref{Makefiles, ,Writing Makefiles}). However, recipes are meant
3421to be interpreted by the shell and so they are written using shell
3422syntax. The @code{make} program does not try to understand shell
3423syntax: it performs only a very few specific translations on the
3424content of the recipe before handing it to the shell.
3425
3426Each line in the recipe must start with a tab (or the first character
3427in the value of the @code{.RECIPEPREFIX} variable; @pxref{Special
3428Variables}), except that the first recipe line may be attached to the
3429target-and-prerequisites line with a semicolon in between. @emph{Any}
3430line in the makefile that begins with a tab and appears in a ``rule
3431context'' (that is, after a rule has been started until another rule
3432or variable definition) will be considered part of a recipe for that
3433rule. Blank lines and lines of just comments may appear among the
3434recipe lines; they are ignored.
3435
3436Some consequences of these rules include:
3437
3438@itemize @bullet
3439@item
3440A blank line that begins with a tab is not blank: it's an empty
3441recipe (@pxref{Empty Recipes}).
3442
3443@cindex comments, in recipes
3444@cindex recipes, comments in
3445@cindex @code{#} (comments), in recipes
3446@item
3447A comment in a recipe is not a @code{make} comment; it will be
3448passed to the shell as-is. Whether the shell treats it as a comment
3449or not depends on your shell.
3450
3451@item
3452A variable definition in a ``rule context'' which is indented by a tab
3453as the first character on the line, will be considered part of a
3454recipe, not a @code{make} variable definition, and passed to the
3455shell.
3456
3457@item
3458A conditional expression (@code{ifdef}, @code{ifeq},
3459etc. @pxref{Conditional Syntax, ,Syntax of Conditionals}) in a ``rule
3460context'' which is indented by a tab as the first character on the
3461line, will be considered part of a recipe and be passed to the shell.
3462
3463@end itemize
3464
3465@menu
3466* Splitting Lines:: Breaking long recipe lines for readability.
3467* Variables in Recipes:: Using @code{make} variables in recipes.
3468@end menu
3469
3470@node Splitting Lines, Variables in Recipes, Recipe Syntax, Recipe Syntax
3471@subsection Splitting Recipe Lines
3472@cindex recipes, splitting
3473@cindex splitting recipes
3474@cindex recipes, backslash (@code{\}) in
3475@cindex recipes, quoting newlines in
3476@cindex backslash (@code{\}), in recipes
3477@cindex @code{\} (backslash), in recipes
3478@cindex quoting newline, in recipes
3479@cindex newline, quoting, in recipes
3480
3481One of the few ways in which @code{make} does interpret recipes is
3482checking for a backslash just before the newline. As in normal
3483makefile syntax, a single logical recipe line can be split into
3484multiple physical lines in the makefile by placing a backslash before
3485each newline. A sequence of lines like this is considered a single
3486recipe line, and one instance of the shell will be invoked to run it.
3487
3488However, in contrast to how they are treated in other places in a
3489makefile, backslash-newline pairs are @emph{not} removed from the
3490recipe. Both the backslash and the newline characters are preserved
3491and passed to the shell. How the backslash-newline is interpreted
3492depends on your shell. If the first character of the next line after
3493the backslash-newline is the recipe prefix character (a tab by
3494default; @pxref{Special Variables}), then that character (and only
3495that character) is removed. Whitespace is never added to the recipe.
3496
3497For example, the recipe for the all target in this makefile:
3498
3499@example
3500@group
3501all :
3502 @@echo no\
3503space
3504 @@echo no\
3505 space
3506 @@echo one \
3507 space
3508 @@echo one\
3509 space
3510@end group
3511@end example
3512
3513@noindent
3514consists of four separate shell commands where the output is:
3515
3516@example
3517@group
3518nospace
3519nospace
3520one space
3521one space
3522@end group
3523@end example
3524
3525As a more complex example, this makefile:
3526
3527@example
3528@group
3529all : ; @@echo 'hello \
3530 world' ; echo "hello \
3531 world"
3532@end group
3533@end example
3534
3535@noindent
3536will invoke one shell with a command of:
3537
3538@example
3539@group
3540echo 'hello \
3541world' ; echo "hello \
3542 world"
3543@end group
3544@end example
3545
3546@noindent
3547which, according to shell quoting rules, will yield the following output:
3548
3549@example
3550@group
3551hello \
3552world
3553hello world
3554@end group
3555@end example
3556
3557@noindent
3558Notice how the backslash/newline pair was removed inside the string
3559quoted with double quotes (@code{"@dots{}"}), but not from the string
3560quoted with single quotes (@code{'@dots{}'}). This is the way the
3561default shell (@file{/bin/sh}) handles backslash/newline pairs. If
3562you specify a different shell in your makefiles it may treat them
3563differently.
3564
3565Sometimes you want to split a long line inside of single quotes, but
3566you don't want the backslash-newline to appear in the quoted content.
3567This is often the case when passing scripts to languages such as Perl,
3568where extraneous backslashes inside the script can change its meaning
3569or even be a syntax error. One simple way of handling this is to
3570place the quoted string, or even the entire command, into a
3571@code{make} variable then use the variable in the recipe. In this
3572situation the newline quoting rules for makefiles will be used, and
3573the backslash-newline will be removed. If we rewrite our example
3574above using this method:
3575
3576@example
3577@group
3578HELLO = 'hello \
3579world'
3580
3581all : ; @@echo $(HELLO)
3582@end group
3583@end example
3584
3585@noindent
3586we will get output like this:
3587
3588@example
3589@group
3590hello world
3591@end group
3592@end example
3593
3594If you like, you can also use target-specific variables
3595(@pxref{Target-specific, ,Target-specific Variable Values}) to obtain
3596a tighter correspondence between the variable and the recipe that
3597uses it.
3598
3599@node Variables in Recipes, , Splitting Lines, Recipe Syntax
3600@subsection Using Variables in Recipes
3601@cindex variable references in recipes
3602@cindex recipes, using variables in
3603
3604The other way in which @code{make} processes recipes is by expanding
3605any variable references in them (@pxref{Reference,Basics of Variable
3606References}). This occurs after make has finished reading all the
3607makefiles and the target is determined to be out of date; so, the
3608recipes for targets which are not rebuilt are never expanded.
3609
3610Variable and function references in recipes have identical syntax and
3611semantics to references elsewhere in the makefile. They also have the
3612same quoting rules: if you want a dollar sign to appear in your
3613recipe, you must double it (@samp{$$}). For shells like the default
3614shell, that use dollar signs to introduce variables, it's important to
3615keep clear in your mind whether the variable you want to reference is
3616a @code{make} variable (use a single dollar sign) or a shell variable
3617(use two dollar signs). For example:
3618
3619@example
3620@group
3621LIST = one two three
3622all:
3623 for i in $(LIST); do \
3624 echo $$i; \
3625 done
3626@end group
3627@end example
3628
3629@noindent
3630results in the following command being passed to the shell:
3631
3632@example
3633@group
3634for i in one two three; do \
3635 echo $i; \
3636done
3637@end group
3638@end example
3639
3640@noindent
3641which generates the expected result:
3642
3643@example
3644@group
3645one
3646two
3647three
3648@end group
3649@end example
3650
3651@node Echoing, Execution, Recipe Syntax, Recipes
3652@section Recipe Echoing
3653@cindex echoing of recipes
3654@cindex silent operation
3655@cindex @code{@@} (in recipes)
3656@cindex recipes, echoing
3657@cindex printing of recipes
3658
3659Normally @code{make} prints each line of the recipe before it is
3660executed. We call this @dfn{echoing} because it gives the appearance
3661that you are typing the lines yourself.
3662
3663When a line starts with @samp{@@}, the echoing of that line is suppressed.
3664The @samp{@@} is discarded before the line is passed to the shell.
3665Typically you would use this for a command whose only effect is to print
3666something, such as an @code{echo} command to indicate progress through
3667the makefile:
3668
3669@example
3670@@echo About to make distribution files
3671@end example
3672
3673@cindex @code{-n}
3674@cindex @code{--just-print}
3675@cindex @code{--dry-run}
3676@cindex @code{--recon}
3677When @code{make} is given the flag @samp{-n} or @samp{--just-print} it
3678only echoes most recipes, without executing them. @xref{Options
3679Summary, ,Summary of Options}. In this case even the recipe lines
3680starting with @samp{@@} are printed. This flag is useful for finding
3681out which recipes @code{make} thinks are necessary without actually
3682doing them.
3683
3684@cindex @code{-s}
3685@cindex @code{--silent}
3686@cindex @code{--quiet}
3687@findex .SILENT
3688The @samp{-s} or @samp{--silent}
3689flag to @code{make} prevents all echoing, as if all recipes
3690started with @samp{@@}. A rule in the makefile for the special target
3691@code{.SILENT} without prerequisites has the same effect
3692(@pxref{Special Targets, ,Special Built-in Target Names}).
3693@code{.SILENT} is essentially obsolete since @samp{@@} is more flexible.@refill
3694
3695@node Execution, Parallel, Echoing, Recipes
3696@section Recipe Execution
3697@cindex recipe, execution
3698@cindex execution, of recipes
3699@vindex @code{SHELL} @r{(recipe execution)}
3700
3701When it is time to execute recipes to update a target, they are
3702executed by invoking a new subshell for each line of the recipe,
3703unless the @code{.ONESHELL} special target is in effect
3704(@pxref{One Shell, ,Using One Shell}) (In practice, @code{make} may
3705take shortcuts that do not affect the results.)
3706
3707@cindex @code{cd} (shell command)
3708@cindex shell variables, setting in recipes
3709@cindex recipes setting shell variables
3710@strong{Please note:} this implies that setting shell variables and
3711invoking shell commands such as @code{cd} that set a context local to
3712each process will not affect the following lines in the recipe.@footnote{On
3713MS-DOS, the value of current working directory is @strong{global}, so
3714changing it @emph{will} affect the following recipe lines on those
3715systems.} If you want to use @code{cd} to affect the next statement,
3716put both statements in a single recipe line. Then @code{make} will
3717invoke one shell to run the entire line, and the shell will execute
3718the statements in sequence. For example:
3719
3720@example
3721foo : bar/lose
3722 cd $(@@D) && gobble $(@@F) > ../$@@
3723@end example
3724
3725@noindent
3726Here we use the shell AND operator (@code{&&}) so that if the
3727@code{cd} command fails, the script will fail without trying to invoke
3728the @code{gobble} command in the wrong directory, which could cause
3729problems (in this case it would certainly cause @file{../foo} to be
3730truncated, at least).
3731
3732@menu
3733* One Shell:: One shell for all lines in a recipe
3734* Choosing the Shell:: How @code{make} chooses the shell used
3735 to run recipes.
3736@end menu
3737
3738@node One Shell, Choosing the Shell, Execution, Execution
3739@subsection Using One Shell
3740@cindex recipe lines, single shell
3741@cindex @code{.ONESHELL}, use of
3742@findex .ONESHELL
3743
3744Sometimes you would prefer that all the lines in the recipe be passed
3745to a single invocation of the shell. There are generally two
3746situations where this is useful: first, it can improve performance in
3747makefiles where recipes consist of many command lines, by avoiding
3748extra processes. Second, you might want newlines to be included in
3749your recipe command (for example perhaps you are using a very
3750different interpreter as your @code{SHELL}). If the @code{.ONESHELL}
3751special target appears anywhere in the makefile then @emph{all}
3752recipe lines for each target will be provided to a single invocation
3753of the shell. Newlines between recipe lines will be preserved. For
3754example:
3755
3756@example
3757.ONESHELL:
3758foo : bar/lose
3759 cd $(@@D)
3760 gobble $(@@F) > ../$@@
3761@end example
3762
3763@noindent
3764would now work as expected even though the commands are on different
3765recipe lines.
3766
3767If @code{.ONESHELL} is provided, then only the first line of the
3768recipe will be checked for the special prefix characters (@samp{@@},
3769@samp{-}, and @samp{+}). Subsequent lines will include the special
3770characters in the recipe line when the @code{SHELL} is invoked. If
3771you want your recipe to start with one of these special characters
3772you'll need to arrange for them to not be the first characters on the
3773first line, perhaps by adding a comment or similar. For example, this
3774would be a syntax error in Perl because the first @samp{@@} is removed
3775by make:
3776
3777@example
3778.ONESHELL:
3779SHELL = /usr/bin/perl
3780.SHELLFLAGS = -e
3781show :
3782 @@f = qw(a b c);
3783 print "@@f\n";
3784@end example
3785
3786@noindent
3787However, either of these alternatives would work properly:
3788
3789@example
3790.ONESHELL:
3791SHELL = /usr/bin/perl
3792.SHELLFLAGS = -e
3793show :
3794 # Make sure "@@" is not the first character on the first line
3795 @@f = qw(a b c);
3796 print "@@f\n";
3797@end example
3798
3799@noindent
3800or
3801
3802@example
3803.ONESHELL:
3804SHELL = /usr/bin/perl
3805.SHELLFLAGS = -e
3806show :
3807 my @@f = qw(a b c);
3808 print "@@f\n";
3809@end example
3810
3811As a special feature, if @code{SHELL} is determined to be a
3812POSIX-style shell, the special prefix characters in ``internal''
3813recipe lines will @emph{removed} before the recipe is processed. This
3814feature is intended to allow existing makefiles to add the
3815@code{.ONESHELL} special target and still run properly without
3816extensive modifications. Since the special prefix characters are not
3817legal at the beginning of a line in a POSIX shell script this is not a
3818loss in functionality. For example, this works as expected:
3819
3820@example
3821.ONESHELL:
3822foo : bar/lose
3823 @@cd $(@@D)
3824 @@gobble $(@@F) > ../$@@
3825@end example
3826
3827Even with this special feature, however, makefiles with
3828@code{.ONESHELL} will behave differently in ways that could be
3829noticeable. For example, normally if any line in the recipe fails,
3830that causes the rule to fail and no more recipe lines are processed.
3831Under @code{.ONESHELL} a failure of any but the final recipe line will
3832not be noticed by @code{make}. You can modify @code{.SHELLFLAGS} to
3833add the @code{-e} option to the shell which will cause any failure
3834anywhere in the command line to cause the shell to fail, but this
3835could itself cause your recipe to behave differently. Ultimately you
3836may need to harden your recipe lines to allow them to work with
3837@code{.ONESHELL}.
3838
3839@node Choosing the Shell, , One Shell, Execution
3840@subsection Choosing the Shell
3841@cindex shell, choosing the
3842@cindex @code{SHELL}, value of
3843@cindex @code{.SHELLFLAGS}, value of
3844
3845@vindex SHELL
3846@vindex .SHELLFLAGS
3847The program used as the shell is taken from the variable @code{SHELL}.
3848If this variable is not set in your makefile, the program
3849@file{/bin/sh} is used as the shell. The argument(s) passed to the
3850shell are taken from the variable @code{.SHELLFLAGS}. The default
3851value of @code{.SHELLFLAGS} is @code{-c} normally, or @code{-ec} in
3852POSIX-conforming mode.
3853
3854@cindex environment, @code{SHELL} in
3855Unlike most variables, the variable @code{SHELL} is never set from the
3856environment. This is because the @code{SHELL} environment variable is
3857used to specify your personal choice of shell program for interactive
3858use. It would be very bad for personal choices like this to affect the
3859functioning of makefiles. @xref{Environment, ,Variables from the
3860Environment}.
3861
3862Furthermore, when you do set @code{SHELL} in your makefile that value
3863is @emph{not} exported in the environment to recipe lines that
3864@code{make} invokes. Instead, the value inherited from the user's
3865environment, if any, is exported. You can override this behavior by
3866explicitly exporting @code{SHELL} (@pxref{Variables/Recursion,
3867,Communicating Variables to a Sub-@code{make}}), forcing it to be
3868passed in the environment to recipe lines.
3869
3870@vindex @code{MAKESHELL} @r{(MS-DOS alternative to @code{SHELL})}
3871However, on MS-DOS and MS-Windows the value of @code{SHELL} in the
3872environment @strong{is} used, since on those systems most users do not
3873set this variable, and therefore it is most likely set specifically to
3874be used by @code{make}. On MS-DOS, if the setting of @code{SHELL} is
3875not suitable for @code{make}, you can set the variable
3876@code{MAKESHELL} to the shell that @code{make} should use; if set it
3877will be used as the shell instead of the value of @code{SHELL}.
3878
3879@subsubheading Choosing a Shell in DOS and Windows
3880@cindex shell, in DOS and Windows
3881@cindex DOS, choosing a shell in
3882@cindex Windows, choosing a shell in
3883
3884Choosing a shell in MS-DOS and MS-Windows is much more complex than on
3885other systems.
3886
3887@vindex COMSPEC
3888On MS-DOS, if @code{SHELL} is not set, the value of the variable
3889@code{COMSPEC} (which is always set) is used instead.
3890
3891@cindex @code{SHELL}, MS-DOS specifics
3892The processing of lines that set the variable @code{SHELL} in Makefiles
3893is different on MS-DOS. The stock shell, @file{command.com}, is
3894ridiculously limited in its functionality and many users of @code{make}
3895tend to install a replacement shell. Therefore, on MS-DOS, @code{make}
3896examines the value of @code{SHELL}, and changes its behavior based on
3897whether it points to a Unix-style or DOS-style shell. This allows
3898reasonable functionality even if @code{SHELL} points to
3899@file{command.com}.
3900
3901If @code{SHELL} points to a Unix-style shell, @code{make} on MS-DOS
3902additionally checks whether that shell can indeed be found; if not, it
3903ignores the line that sets @code{SHELL}. In MS-DOS, GNU @code{make}
3904searches for the shell in the following places:
3905
3906@enumerate
3907@item
3908In the precise place pointed to by the value of @code{SHELL}. For
3909example, if the makefile specifies @samp{SHELL = /bin/sh}, @code{make}
3910will look in the directory @file{/bin} on the current drive.
3911
3912@item
3913In the current directory.
3914
3915@item
3916In each of the directories in the @code{PATH} variable, in order.
3917
3918@end enumerate
3919
3920In every directory it examines, @code{make} will first look for the
3921specific file (@file{sh} in the example above). If this is not found,
3922it will also look in that directory for that file with one of the known
3923extensions which identify executable files. For example @file{.exe},
3924@file{.com}, @file{.bat}, @file{.btm}, @file{.sh}, and some others.
3925
3926If any of these attempts is successful, the value of @code{SHELL} will
3927be set to the full pathname of the shell as found. However, if none of
3928these is found, the value of @code{SHELL} will not be changed, and thus
3929the line that sets it will be effectively ignored. This is so
3930@code{make} will only support features specific to a Unix-style shell if
3931such a shell is actually installed on the system where @code{make} runs.
3932
3933Note that this extended search for the shell is limited to the cases
3934where @code{SHELL} is set from the Makefile; if it is set in the
3935environment or command line, you are expected to set it to the full
3936pathname of the shell, exactly as things are on Unix.
3937
3938The effect of the above DOS-specific processing is that a Makefile that
3939contains @samp{SHELL = /bin/sh} (as many Unix makefiles do), will work
3940on MS-DOS unaltered if you have e.g.@: @file{sh.exe} installed in some
3941directory along your @code{PATH}.
3942
3943@vindex SHELL
3944@vindex .SHELLFLAGS
3945
3946@node Parallel, Errors, Execution, Recipes
3947@section Parallel Execution
3948@cindex recipes, execution in parallel
3949@cindex parallel execution
3950@cindex execution, in parallel
3951@cindex job slots
3952@cindex @code{-j}
3953@cindex @code{--jobs}
3954
3955GNU @code{make} knows how to execute several recipes at once.
3956Normally, @code{make} will execute only one recipe at a time, waiting
3957for it to finish before executing the next. However, the @samp{-j} or
3958@samp{--jobs} option tells @code{make} to execute many recipes
3959simultaneously. You can inhibit parallelism in a particular makefile
3960with the @code{.NOTPARALLEL} pseudo-target (@pxref{Special
3961Targets,Special Built-in Target Names}).@refill
3962
3963On MS-DOS, the @samp{-j} option has no effect, since that system doesn't
3964support multi-processing.
3965
3966If the @samp{-j} option is followed by an integer, this is the number of
3967recipes to execute at once; this is called the number of @dfn{job slots}.
3968If there is nothing looking like an integer after the @samp{-j} option,
3969there is no limit on the number of job slots. The default number of job
3970slots is one, which means serial execution (one thing at a time).
3971
3972One unpleasant consequence of running several recipes simultaneously is
3973that output generated by the recipes appears whenever each recipe
3974sends it, so messages from different recipes may be interspersed.
3975
3976Another problem is that two processes cannot both take input from the
3977same device; so to make sure that only one recipe tries to take input
3978from the terminal at once, @code{make} will invalidate the standard
3979input streams of all but one running recipe. This means that
3980attempting to read from standard input will usually be a fatal error (a
3981@samp{Broken pipe} signal) for most child processes if there are
3982several.
3983@cindex broken pipe
3984@cindex standard input
3985
3986It is unpredictable which recipe will have a valid standard input stream
3987(which will come from the terminal, or wherever you redirect the standard
3988input of @code{make}). The first recipe run will always get it first, and
3989the first recipe started after that one finishes will get it next, and so
3990on.
3991
3992We will change how this aspect of @code{make} works if we find a better
3993alternative. In the mean time, you should not rely on any recipe using
3994standard input at all if you are using the parallel execution feature; but
3995if you are not using this feature, then standard input works normally in
3996all recipes.
3997
3998Finally, handling recursive @code{make} invocations raises issues. For
3999more information on this, see
4000@ref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
4001
4002If a recipe fails (is killed by a signal or exits with a nonzero
4003status), and errors are not ignored for that recipe
4004(@pxref{Errors, ,Errors in Recipes}),
4005the remaining recipe lines to remake the same target will not be run.
4006If a recipe fails and the @samp{-k} or @samp{--keep-going}
4007option was not given
4008(@pxref{Options Summary, ,Summary of Options}),
4009@code{make} aborts execution. If make
4010terminates for any reason (including a signal) with child processes
4011running, it waits for them to finish before actually exiting.@refill
4012
4013@cindex load average
4014@cindex limiting jobs based on load
4015@cindex jobs, limiting based on load
4016@cindex @code{-l} (load average)
4017@cindex @code{--max-load}
4018@cindex @code{--load-average}
4019When the system is heavily loaded, you will probably want to run fewer jobs
4020than when it is lightly loaded. You can use the @samp{-l} option to tell
4021@code{make} to limit the number of jobs to run at once, based on the load
4022average. The @samp{-l} or @samp{--max-load}
4023option is followed by a floating-point number. For
4024example,
4025
4026@example
4027-l 2.5
4028@end example
4029
4030@noindent
4031will not let @code{make} start more than one job if the load average is
4032above 2.5. The @samp{-l} option with no following number removes the
4033load limit, if one was given with a previous @samp{-l} option.@refill
4034
4035More precisely, when @code{make} goes to start up a job, and it already has
4036at least one job running, it checks the current load average; if it is not
4037lower than the limit given with @samp{-l}, @code{make} waits until the load
4038average goes below that limit, or until all the other jobs finish.
4039
4040By default, there is no load limit.
4041
4042@node Errors, Interrupts, Parallel, Recipes
4043@section Errors in Recipes
4044@cindex errors (in recipes)
4045@cindex recipes, errors in
4046@cindex exit status (errors)
4047
4048After each shell invocation returns, @code{make} looks at its exit
4049status. If the shell completed successfully (the exit status is
4050zero), the next line in the recipe is executed in a new shell; after
4051the last line is finished, the rule is finished.
4052
4053If there is an error (the exit status is nonzero), @code{make} gives up on
4054the current rule, and perhaps on all rules.
4055
4056Sometimes the failure of a certain recipe line does not indicate a problem.
4057For example, you may use the @code{mkdir} command to ensure that a
4058directory exists. If the directory already exists, @code{mkdir} will
4059report an error, but you probably want @code{make} to continue regardless.
4060
4061@cindex @code{-} (in recipes)
4062To ignore errors in a recipe line, write a @samp{-} at the beginning
4063of the line's text (after the initial tab). The @samp{-} is discarded
4064before the line is passed to the shell for execution.
4065
4066For example,
4067
4068@example
4069@group
4070clean:
4071 -rm -f *.o
4072@end group
4073@end example
4074@cindex @code{rm} (shell command)
4075
4076@noindent
4077This causes @code{make} to continue even if @code{rm} is unable to
4078remove a file.
4079
4080@cindex @code{-i}
4081@cindex @code{--ignore-errors}
4082@findex .IGNORE
4083When you run @code{make} with the @samp{-i} or @samp{--ignore-errors}
4084flag, errors are ignored in all recipes of all rules. A rule in the
4085makefile for the special target @code{.IGNORE} has the same effect, if
4086there are no prerequisites. These ways of ignoring errors are obsolete
4087because @samp{-} is more flexible.
4088
4089When errors are to be ignored, because of either a @samp{-} or the
4090@samp{-i} flag, @code{make} treats an error return just like success,
4091except that it prints out a message that tells you the status code
4092the shell exited with, and says that the error has been ignored.
4093
4094When an error happens that @code{make} has not been told to ignore,
4095it implies that the current target cannot be correctly remade, and neither
4096can any other that depends on it either directly or indirectly. No further
4097recipes will be executed for these targets, since their preconditions
4098have not been achieved.
4099
4100
4101@cindex @code{-k}
4102@cindex @code{--keep-going}
4103Normally @code{make} gives up immediately in this circumstance, returning a
4104nonzero status. However, if the @samp{-k} or @samp{--keep-going}
4105flag is specified, @code{make}
4106continues to consider the other prerequisites of the pending targets,
4107remaking them if necessary, before it gives up and returns nonzero status.
4108For example, after an error in compiling one object file, @samp{make -k}
4109will continue compiling other object files even though it already knows
4110that linking them will be impossible. @xref{Options Summary, ,Summary of Options}.
4111
4112The usual behavior assumes that your purpose is to get the specified
4113targets up to date; once @code{make} learns that this is impossible, it
4114might as well report the failure immediately. The @samp{-k} option says
4115that the real purpose is to test as many of the changes made in the
4116program as possible, perhaps to find several independent problems so
4117that you can correct them all before the next attempt to compile. This
4118is why Emacs' @code{compile} command passes the @samp{-k} flag by
4119default.
4120@cindex Emacs (@code{M-x compile})
4121
4122@findex .DELETE_ON_ERROR
4123@cindex deletion of target files
4124@cindex removal of target files
4125@cindex target, deleting on error
4126Usually when a recipe line fails, if it has changed the target file at all,
4127the file is corrupted and cannot be used---or at least it is not
4128completely updated. Yet the file's time stamp says that it is now up to
4129date, so the next time @code{make} runs, it will not try to update that
4130file. The situation is just the same as when the shell is killed by a
4131signal; @pxref{Interrupts}. So generally the right thing to do is to
4132delete the target file if the recipe fails after beginning to change
4133the file. @code{make} will do this if @code{.DELETE_ON_ERROR} appears
4134as a target. This is almost always what you want @code{make} to do, but
4135it is not historical practice; so for compatibility, you must explicitly
4136request it.
4137
4138@node Interrupts, Recursion, Errors, Recipes
4139@section Interrupting or Killing @code{make}
4140@cindex interrupt
4141@cindex signal
4142@cindex deletion of target files
4143@cindex removal of target files
4144@cindex target, deleting on interrupt
4145@cindex killing (interruption)
4146
4147If @code{make} gets a fatal signal while a shell is executing, it may
4148delete the target file that the recipe was supposed to update. This is
4149done if the target file's last-modification time has changed since
4150@code{make} first checked it.
4151
4152The purpose of deleting the target is to make sure that it is remade from
4153scratch when @code{make} is next run. Why is this? Suppose you type
4154@kbd{Ctrl-c} while a compiler is running, and it has begun to write an
4155object file @file{foo.o}. The @kbd{Ctrl-c} kills the compiler, resulting
4156in an incomplete file whose last-modification time is newer than the source
4157file @file{foo.c}. But @code{make} also receives the @kbd{Ctrl-c} signal
4158and deletes this incomplete file. If @code{make} did not do this, the next
4159invocation of @code{make} would think that @file{foo.o} did not require
4160updating---resulting in a strange error message from the linker when it
4161tries to link an object file half of which is missing.
4162
4163@findex .PRECIOUS
4164You can prevent the deletion of a target file in this way by making the
4165special target @code{.PRECIOUS} depend on it. Before remaking a target,
4166@code{make} checks to see whether it appears on the prerequisites of
4167@code{.PRECIOUS}, and thereby decides whether the target should be deleted
4168if a signal happens. Some reasons why you might do this are that the
4169target is updated in some atomic fashion, or exists only to record a
4170modification-time (its contents do not matter), or must exist at all
4171times to prevent other sorts of trouble.
4172
4173@node Recursion, Canned Recipes, Interrupts, Recipes
4174@section Recursive Use of @code{make}
4175@cindex recursion
4176@cindex subdirectories, recursion for
4177
4178Recursive use of @code{make} means using @code{make} as a command in a
4179makefile. This technique is useful when you want separate makefiles for
4180various subsystems that compose a larger system. For example, suppose you
4181have a subdirectory @file{subdir} which has its own makefile, and you would
4182like the containing directory's makefile to run @code{make} on the
4183subdirectory. You can do it by writing this:
4184
4185@example
4186subsystem:
4187 cd subdir && $(MAKE)
4188@end example
4189
4190@noindent
4191or, equivalently, this (@pxref{Options Summary, ,Summary of Options}):
4192
4193@example
4194subsystem:
4195 $(MAKE) -C subdir
4196@end example
4197@cindex @code{-C}
4198@cindex @code{--directory}
4199
4200You can write recursive @code{make} commands just by copying this example,
4201but there are many things to know about how they work and why, and about
4202how the sub-@code{make} relates to the top-level @code{make}. You may
4203also find it useful to declare targets that invoke recursive
4204@code{make} commands as @samp{.PHONY} (for more discussion on when
4205this is useful, see @ref{Phony Targets}).
4206
4207@vindex @code{CURDIR}
4208For your convenience, when GNU @code{make} starts (after it has
4209processed any @code{-C} options) it sets the variable @code{CURDIR} to
4210the pathname of the current working directory. This value is never
4211touched by @code{make} again: in particular note that if you include
4212files from other directories the value of @code{CURDIR} does not
4213change. The value has the same precedence it would have if it were
4214set in the makefile (by default, an environment variable @code{CURDIR}
4215will not override this value). Note that setting this variable has no
4216impact on the operation of @code{make} (it does not cause @code{make}
4217to change its working directory, for example).
4218
4219@menu
4220* MAKE Variable:: The special effects of using @samp{$(MAKE)}.
4221* Variables/Recursion:: How to communicate variables to a sub-@code{make}.
4222* Options/Recursion:: How to communicate options to a sub-@code{make}.
4223* -w Option:: How the @samp{-w} or @samp{--print-directory} option
4224 helps debug use of recursive @code{make} commands.
4225@end menu
4226
4227@node MAKE Variable, Variables/Recursion, Recursion, Recursion
4228@subsection How the @code{MAKE} Variable Works
4229@vindex MAKE
4230@cindex recursion, and @code{MAKE} variable
4231
4232Recursive @code{make} commands should always use the variable @code{MAKE},
4233not the explicit command name @samp{make}, as shown here:
4234
4235@example
4236@group
4237subsystem:
4238 cd subdir && $(MAKE)
4239@end group
4240@end example
4241
4242The value of this variable is the file name with which @code{make} was
4243invoked. If this file name was @file{/bin/make}, then the recipe executed
4244is @samp{cd subdir && /bin/make}. If you use a special version of
4245@code{make} to run the top-level makefile, the same special version will be
4246executed for recursive invocations.
4247@cindex @code{cd} (shell command)
4248
4249@cindex +, and recipes
4250As a special feature, using the variable @code{MAKE} in the recipe of
4251a rule alters the effects of the @samp{-t} (@samp{--touch}), @samp{-n}
4252(@samp{--just-print}), or @samp{-q} (@w{@samp{--question}}) option.
4253Using the @code{MAKE} variable has the same effect as using a @samp{+}
4254character at the beginning of the recipe line. @xref{Instead of
4255Execution, ,Instead of Executing the Recipes}. This special feature
4256is only enabled if the @code{MAKE} variable appears directly in the
4257recipe: it does not apply if the @code{MAKE} variable is referenced
4258through expansion of another variable. In the latter case you must
4259use the @samp{+} token to get these special effects.@refill
4260
4261Consider the command @samp{make -t} in the above example. (The
4262@samp{-t} option marks targets as up to date without actually running
4263any recipes; see @ref{Instead of Execution}.) Following the usual
4264definition of @samp{-t}, a @samp{make -t} command in the example would
4265create a file named @file{subsystem} and do nothing else. What you
4266really want it to do is run @samp{@w{cd subdir &&} @w{make -t}}; but
4267that would require executing the recipe, and @samp{-t} says not to
4268execute recipes.@refill
4269@cindex @code{-t}, and recursion
4270@cindex recursion, and @code{-t}
4271@cindex @code{--touch}, and recursion
4272
4273The special feature makes this do what you want: whenever a recipe
4274line of a rule contains the variable @code{MAKE}, the flags @samp{-t},
4275@samp{-n} and @samp{-q} do not apply to that line. Recipe lines
4276containing @code{MAKE} are executed normally despite the presence of a
4277flag that causes most recipes not to be run. The usual
4278@code{MAKEFLAGS} mechanism passes the flags to the sub-@code{make}
4279(@pxref{Options/Recursion, ,Communicating Options to a
4280Sub-@code{make}}), so your request to touch the files, or print the
4281recipes, is propagated to the subsystem.@refill
4282
4283@node Variables/Recursion, Options/Recursion, MAKE Variable, Recursion
4284@subsection Communicating Variables to a Sub-@code{make}
4285@cindex sub-@code{make}
4286@cindex environment, and recursion
4287@cindex exporting variables
4288@cindex variables, environment
4289@cindex variables, exporting
4290@cindex recursion, and environment
4291@cindex recursion, and variables
4292
4293Variable values of the top-level @code{make} can be passed to the
4294sub-@code{make} through the environment by explicit request. These
4295variables are defined in the sub-@code{make} as defaults, but do not
4296override what is specified in the makefile used by the sub-@code{make}
4297makefile unless you use the @samp{-e} switch (@pxref{Options Summary,
4298,Summary of Options}).@refill
4299
4300To pass down, or @dfn{export}, a variable, @code{make} adds the
4301variable and its value to the environment for running each line of the
4302recipe. The sub-@code{make}, in turn, uses the environment to
4303initialize its table of variable values. @xref{Environment,
4304,Variables from the Environment}.
4305
4306Except by explicit request, @code{make} exports a variable only if it
4307is either defined in the environment initially or set on the command
4308line, and if its name consists only of letters, numbers, and underscores.
4309Some shells cannot cope with environment variable names consisting of
4310characters other than letters, numbers, and underscores.
4311
4312@cindex SHELL, exported value
4313The value of the @code{make} variable @code{SHELL} is not exported.
4314Instead, the value of the @code{SHELL} variable from the invoking
4315environment is passed to the sub-@code{make}. You can force
4316@code{make} to export its value for @code{SHELL} by using the
4317@code{export} directive, described below. @xref{Choosing the Shell}.
4318
4319The special variable @code{MAKEFLAGS} is always exported (unless you
4320unexport it). @code{MAKEFILES} is exported if you set it to anything.
4321
4322@code{make} automatically passes down variable values that were defined
4323on the command line, by putting them in the @code{MAKEFLAGS} variable.
4324@iftex
4325See the next section.
4326@end iftex
4327@ifnottex
4328@xref{Options/Recursion}.
4329@end ifnottex
4330
4331Variables are @emph{not} normally passed down if they were created by
4332default by @code{make} (@pxref{Implicit Variables, ,Variables Used by
4333Implicit Rules}). The sub-@code{make} will define these for
4334itself.@refill
4335
4336@findex export
4337If you want to export specific variables to a sub-@code{make}, use the
4338@code{export} directive, like this:
4339
4340@example
4341export @var{variable} @dots{}
4342@end example
4343
4344@noindent
4345@findex unexport
4346If you want to @emph{prevent} a variable from being exported, use the
4347@code{unexport} directive, like this:
4348
4349@example
4350unexport @var{variable} @dots{}
4351@end example
4352
4353@noindent
4354In both of these forms, the arguments to @code{export} and
4355@code{unexport} are expanded, and so could be variables or functions
4356which expand to a (list of) variable names to be (un)exported.
4357
4358As a convenience, you can define a variable and export it at the same
4359time by doing:
4360
4361@example
4362export @var{variable} = value
4363@end example
4364
4365@noindent
4366has the same result as:
4367
4368@example
4369@var{variable} = value
4370export @var{variable}
4371@end example
4372
4373@noindent
4374and
4375
4376@example
4377export @var{variable} := value
4378@end example
4379
4380@noindent
4381has the same result as:
4382
4383@example
4384@var{variable} := value
4385export @var{variable}
4386@end example
4387
4388Likewise,
4389
4390@example
4391export @var{variable} += value
4392@end example
4393
4394@noindent
4395is just like:
4396
4397@example
4398@var{variable} += value
4399export @var{variable}
4400@end example
4401
4402@noindent
4403@xref{Appending, ,Appending More Text to Variables}.
4404
4405You may notice that the @code{export} and @code{unexport} directives
4406work in @code{make} in the same way they work in the shell, @code{sh}.
4407
4408If you want all variables to be exported by default, you can use
4409@code{export} by itself:
4410
4411@example
4412export
4413@end example
4414
4415@noindent
4416This tells @code{make} that variables which are not explicitly mentioned
4417in an @code{export} or @code{unexport} directive should be exported.
4418Any variable given in an @code{unexport} directive will still @emph{not}
4419be exported. If you use @code{export} by itself to export variables by
4420default, variables whose names contain characters other than
4421alphanumerics and underscores will not be exported unless specifically
4422mentioned in an @code{export} directive.@refill
4423
4424@findex .EXPORT_ALL_VARIABLES
4425The behavior elicited by an @code{export} directive by itself was the
4426default in older versions of GNU @code{make}. If your makefiles depend
4427on this behavior and you want to be compatible with old versions of
4428@code{make}, you can write a rule for the special target
4429@code{.EXPORT_ALL_VARIABLES} instead of using the @code{export} directive.
4430This will be ignored by old @code{make}s, while the @code{export}
4431directive will cause a syntax error.@refill
4432@cindex compatibility in exporting
4433
4434Likewise, you can use @code{unexport} by itself to tell @code{make}
4435@emph{not} to export variables by default. Since this is the default
4436behavior, you would only need to do this if @code{export} had been used
4437by itself earlier (in an included makefile, perhaps). You
4438@strong{cannot} use @code{export} and @code{unexport} by themselves to
4439have variables exported for some recipes and not for others. The last
4440@code{export} or @code{unexport} directive that appears by itself
4441determines the behavior for the entire run of @code{make}.@refill
4442
4443@vindex MAKELEVEL
4444@cindex recursion, level of
4445As a special feature, the variable @code{MAKELEVEL} is changed when it
4446is passed down from level to level. This variable's value is a string
4447which is the depth of the level as a decimal number. The value is
4448@samp{0} for the top-level @code{make}; @samp{1} for a sub-@code{make},
4449@samp{2} for a sub-sub-@code{make}, and so on. The incrementation
4450happens when @code{make} sets up the environment for a recipe.@refill
4451
4452The main use of @code{MAKELEVEL} is to test it in a conditional
4453directive (@pxref{Conditionals, ,Conditional Parts of Makefiles}); this
4454way you can write a makefile that behaves one way if run recursively and
4455another way if run directly by you.@refill
4456
4457@vindex MAKEFILES
4458You can use the variable @code{MAKEFILES} to cause all sub-@code{make}
4459commands to use additional makefiles. The value of @code{MAKEFILES} is
4460a whitespace-separated list of file names. This variable, if defined in
4461the outer-level makefile, is passed down through the environment; then
4462it serves as a list of extra makefiles for the sub-@code{make} to read
4463before the usual or specified ones. @xref{MAKEFILES Variable, ,The
4464Variable @code{MAKEFILES}}.@refill
4465
4466@node Options/Recursion, -w Option, Variables/Recursion, Recursion
4467@subsection Communicating Options to a Sub-@code{make}
4468@cindex options, and recursion
4469@cindex recursion, and options
4470
4471@vindex MAKEFLAGS
4472Flags such as @samp{-s} and @samp{-k} are passed automatically to the
4473sub-@code{make} through the variable @code{MAKEFLAGS}. This variable is
4474set up automatically by @code{make} to contain the flag letters that
4475@code{make} received. Thus, if you do @w{@samp{make -ks}} then
4476@code{MAKEFLAGS} gets the value @samp{ks}.@refill
4477
4478As a consequence, every sub-@code{make} gets a value for @code{MAKEFLAGS}
4479in its environment. In response, it takes the flags from that value and
4480processes them as if they had been given as arguments.
4481@xref{Options Summary, ,Summary of Options}.
4482
4483@cindex command line variable definitions, and recursion
4484@cindex variables, command line, and recursion
4485@cindex recursion, and command line variable definitions
4486Likewise variables defined on the command line are passed to the
4487sub-@code{make} through @code{MAKEFLAGS}. Words in the value of
4488@code{MAKEFLAGS} that contain @samp{=}, @code{make} treats as variable
4489definitions just as if they appeared on the command line.
4490@xref{Overriding, ,Overriding Variables}.
4491
4492@cindex @code{-C}, and recursion
4493@cindex @code{-f}, and recursion
4494@cindex @code{-o}, and recursion
4495@cindex @code{-W}, and recursion
4496@cindex @code{--directory}, and recursion
4497@cindex @code{--file}, and recursion
4498@cindex @code{--old-file}, and recursion
4499@cindex @code{--assume-old}, and recursion
4500@cindex @code{--assume-new}, and recursion
4501@cindex @code{--new-file}, and recursion
4502@cindex recursion, and @code{-C}
4503@cindex recursion, and @code{-f}
4504@cindex recursion, and @code{-o}
4505@cindex recursion, and @code{-W}
4506The options @samp{-C}, @samp{-f}, @samp{-o}, and @samp{-W} are not put
4507into @code{MAKEFLAGS}; these options are not passed down.@refill
4508
4509@cindex @code{-j}, and recursion
4510@cindex @code{--jobs}, and recursion
4511@cindex recursion, and @code{-j}
4512@cindex job slots, and recursion
4513The @samp{-j} option is a special case (@pxref{Parallel, ,Parallel Execution}).
4514If you set it to some numeric value @samp{N} and your operating system
4515supports it (most any UNIX system will; others typically won't), the
4516parent @code{make} and all the sub-@code{make}s will communicate to
4517ensure that there are only @samp{N} jobs running at the same time
4518between them all. Note that any job that is marked recursive
4519(@pxref{Instead of Execution, ,Instead of Executing Recipes})
4520doesn't count against the total jobs (otherwise we could get @samp{N}
4521sub-@code{make}s running and have no slots left over for any real work!)
4522
4523If your operating system doesn't support the above communication, then
4524@samp{-j 1} is always put into @code{MAKEFLAGS} instead of the value you
4525specified. This is because if the @w{@samp{-j}} option were passed down
4526to sub-@code{make}s, you would get many more jobs running in parallel
4527than you asked for. If you give @samp{-j} with no numeric argument,
4528meaning to run as many jobs as possible in parallel, this is passed
4529down, since multiple infinities are no more than one.@refill
4530
4531If you do not want to pass the other flags down, you must change the
4532value of @code{MAKEFLAGS}, like this:
4533
4534@example
4535subsystem:
4536 cd subdir && $(MAKE) MAKEFLAGS=
4537@end example
4538
4539@vindex MAKEOVERRIDES
4540The command line variable definitions really appear in the variable
4541@code{MAKEOVERRIDES}, and @code{MAKEFLAGS} contains a reference to this
4542variable. If you do want to pass flags down normally, but don't want to
4543pass down the command line variable definitions, you can reset
4544@code{MAKEOVERRIDES} to empty, like this:
4545
4546@example
4547MAKEOVERRIDES =
4548@end example
4549
4550@noindent
4551@cindex Arg list too long
4552@cindex E2BIG
4553This is not usually useful to do. However, some systems have a small
4554fixed limit on the size of the environment, and putting so much
4555information into the value of @code{MAKEFLAGS} can exceed it. If you
4556see the error message @samp{Arg list too long}, this may be the problem.
4557@findex .POSIX
4558@cindex POSIX.2
4559(For strict compliance with POSIX.2, changing @code{MAKEOVERRIDES} does
4560not affect @code{MAKEFLAGS} if the special target @samp{.POSIX} appears
4561in the makefile. You probably do not care about this.)
4562
4563@vindex MFLAGS
4564A similar variable @code{MFLAGS} exists also, for historical
4565compatibility. It has the same value as @code{MAKEFLAGS} except that it
4566does not contain the command line variable definitions, and it always
4567begins with a hyphen unless it is empty (@code{MAKEFLAGS} begins with a
4568hyphen only when it begins with an option that has no single-letter
4569version, such as @samp{--warn-undefined-variables}). @code{MFLAGS} was
4570traditionally used explicitly in the recursive @code{make} command, like
4571this:
4572
4573@example
4574subsystem:
4575 cd subdir && $(MAKE) $(MFLAGS)
4576@end example
4577
4578@noindent
4579but now @code{MAKEFLAGS} makes this usage redundant. If you want your
4580makefiles to be compatible with old @code{make} programs, use this
4581technique; it will work fine with more modern @code{make} versions too.
4582
4583@cindex setting options from environment
4584@cindex options, setting from environment
4585@cindex setting options in makefiles
4586@cindex options, setting in makefiles
4587The @code{MAKEFLAGS} variable can also be useful if you want to have
4588certain options, such as @samp{-k} (@pxref{Options Summary, ,Summary of
4589Options}), set each time you run @code{make}. You simply put a value for
4590@code{MAKEFLAGS} in your environment. You can also set @code{MAKEFLAGS} in
4591a makefile, to specify additional flags that should also be in effect for
4592that makefile. (Note that you cannot use @code{MFLAGS} this way. That
4593variable is set only for compatibility; @code{make} does not interpret a
4594value you set for it in any way.)
4595
4596When @code{make} interprets the value of @code{MAKEFLAGS} (either from the
4597environment or from a makefile), it first prepends a hyphen if the value
4598does not already begin with one. Then it chops the value into words
4599separated by blanks, and parses these words as if they were options given
4600on the command line (except that @samp{-C}, @samp{-f}, @samp{-h},
4601@samp{-o}, @samp{-W}, and their long-named versions are ignored; and there
4602is no error for an invalid option).
4603
4604If you do put @code{MAKEFLAGS} in your environment, you should be sure not
4605to include any options that will drastically affect the actions of
4606@code{make} and undermine the purpose of makefiles and of @code{make}
4607itself. For instance, the @samp{-t}, @samp{-n}, and @samp{-q} options, if
4608put in one of these variables, could have disastrous consequences and would
4609certainly have at least surprising and probably annoying effects.@refill
4610
4611@node -w Option, , Options/Recursion, Recursion
4612@subsection The @samp{--print-directory} Option
4613@cindex directories, printing them
4614@cindex printing directories
4615@cindex recursion, and printing directories
4616
4617If you use several levels of recursive @code{make} invocations, the
4618@samp{-w} or @w{@samp{--print-directory}} option can make the output a
4619lot easier to understand by showing each directory as @code{make}
4620starts processing it and as @code{make} finishes processing it. For
4621example, if @samp{make -w} is run in the directory @file{/u/gnu/make},
4622@code{make} will print a line of the form:@refill
4623
4624@example
4625make: Entering directory `/u/gnu/make'.
4626@end example
4627
4628@noindent
4629before doing anything else, and a line of the form:
4630
4631@example
4632make: Leaving directory `/u/gnu/make'.
4633@end example
4634
4635@noindent
4636when processing is completed.
4637
4638@cindex @code{-C}, and @code{-w}
4639@cindex @code{--directory}, and @code{--print-directory}
4640@cindex recursion, and @code{-w}
4641@cindex @code{-w}, and @code{-C}
4642@cindex @code{-w}, and recursion
4643@cindex @code{--print-directory}, and @code{--directory}
4644@cindex @code{--print-directory}, and recursion
4645@cindex @code{--no-print-directory}
4646@cindex @code{--print-directory}, disabling
4647@cindex @code{-w}, disabling
4648Normally, you do not need to specify this option because @samp{make}
4649does it for you: @samp{-w} is turned on automatically when you use the
4650@samp{-C} option, and in sub-@code{make}s. @code{make} will not
4651automatically turn on @samp{-w} if you also use @samp{-s}, which says to
4652be silent, or if you use @samp{--no-print-directory} to explicitly
4653disable it.
4654
4655@node Canned Recipes, Empty Recipes, Recursion, Recipes
4656@section Defining Canned Recipes
4657@cindex canned recipes
4658@cindex recipes, canned
4659@cindex sequences of commands
4660@cindex commands, sequences of
4661
4662When the same sequence of commands is useful in making various
4663targets, you can define it as a canned sequence with the @code{define}
4664directive, and refer to the canned sequence from the recipes for those
4665targets. The canned sequence is actually a variable, so the name must
4666not conflict with other variable names.
4667
4668Here is an example of defining a canned recipe:
4669
4670@example
4671define run-yacc =
4672yacc $(firstword $^)
4673mv y.tab.c $@@
4674endef
4675@end example
4676@cindex @code{yacc}
4677
4678@noindent
4679Here @code{run-yacc} is the name of the variable being defined;
4680@code{endef} marks the end of the definition; the lines in between are the
4681commands. The @code{define} directive does not expand variable references
4682and function calls in the canned sequence; the @samp{$} characters,
4683parentheses, variable names, and so on, all become part of the value of the
4684variable you are defining.
4685@xref{Multi-Line, ,Defining Multi-Line Variables},
4686for a complete explanation of @code{define}.
4687
4688The first command in this example runs Yacc on the first prerequisite of
4689whichever rule uses the canned sequence. The output file from Yacc is
4690always named @file{y.tab.c}. The second command moves the output to the
4691rule's target file name.
4692
4693To use the canned sequence, substitute the variable into the recipe of a
4694rule. You can substitute it like any other variable
4695(@pxref{Reference, ,Basics of Variable References}).
4696Because variables defined by @code{define} are recursively expanded
4697variables, all the variable references you wrote inside the @code{define}
4698are expanded now. For example:
4699
4700@example
4701foo.c : foo.y
4702 $(run-yacc)
4703@end example
4704
4705@noindent
4706@samp{foo.y} will be substituted for the variable @samp{$^} when it occurs in
4707@code{run-yacc}'s value, and @samp{foo.c} for @samp{$@@}.@refill
4708
4709This is a realistic example, but this particular one is not needed in
4710practice because @code{make} has an implicit rule to figure out these
4711commands based on the file names involved
4712(@pxref{Implicit Rules, ,Using Implicit Rules}).
4713
4714@cindex @@, and @code{define}
4715@cindex -, and @code{define}
4716@cindex +, and @code{define}
4717In recipe execution, each line of a canned sequence is treated just as
4718if the line appeared on its own in the rule, preceded by a tab. In
4719particular, @code{make} invokes a separate subshell for each line. You
4720can use the special prefix characters that affect command lines
4721(@samp{@@}, @samp{-}, and @samp{+}) on each line of a canned sequence.
4722@xref{Recipes, ,Writing Recipes in Rules}.
4723For example, using this canned sequence:
4724
4725@example
4726define frobnicate =
4727@@echo "frobnicating target $@@"
4728frob-step-1 $< -o $@@-step-1
4729frob-step-2 $@@-step-1 -o $@@
4730endef
4731@end example
4732
4733@noindent
4734@code{make} will not echo the first line, the @code{echo} command.
4735But it @emph{will} echo the following two recipe lines.
4736
4737On the other hand, prefix characters on the recipe line that refers to
4738a canned sequence apply to every line in the sequence. So the rule:
4739
4740@example
4741frob.out: frob.in
4742 @@$(frobnicate)
4743@end example
4744
4745@noindent
4746does not echo @emph{any} recipe lines.
4747(@xref{Echoing, ,Recipe Echoing}, for a full explanation of @samp{@@}.)
4748
4749@node Empty Recipes, , Canned Recipes, Recipes
4750@section Using Empty Recipes
4751@cindex empty recipes
4752@cindex recipes, empty
4753
4754It is sometimes useful to define recipes which do nothing. This is done
4755simply by giving a recipe that consists of nothing but whitespace. For
4756example:
4757
4758@example
4759target: ;
4760@end example
4761
4762@noindent
4763defines an empty recipe for @file{target}. You could also use a line
4764beginning with a recipe prefix character to define an empty recipe,
4765but this would be confusing because such a line looks empty.
4766
4767@findex .DEFAULT@r{, and empty recipes}
4768You may be wondering why you would want to define a recipe that
4769does nothing. The only reason this is useful is to prevent a target
4770from getting implicit recipes (from implicit rules or the
4771@code{.DEFAULT} special target; @pxref{Implicit Rules} and
4772@pxref{Last Resort, ,Defining Last-Resort Default Rules}).@refill
4773
4774@c !!! another reason is for canonical stamp files:
4775@ignore
4776@example
4777foo: stamp-foo ;
4778stamp-foo: foo.in
4779 create foo frm foo.in
4780 touch $@
4781@end example
4782@end ignore
4783
4784You may be inclined to define empty recipes for targets that are
4785not actual files, but only exist so that their prerequisites can be
4786remade. However, this is not the best way to do that, because the
4787prerequisites may not be remade properly if the target file actually does exist.
4788@xref{Phony Targets, ,Phony Targets}, for a better way to do this.
4789
4790@node Using Variables, Conditionals, Recipes, Top
4791@chapter How to Use Variables
4792@cindex variable
4793@cindex value
4794@cindex recursive variable expansion
4795@cindex simple variable expansion
4796
4797A @dfn{variable} is a name defined in a makefile to represent a string
4798of text, called the variable's @dfn{value}. These values are
4799substituted by explicit request into targets, prerequisites, recipes,
4800and other parts of the makefile. (In some other versions of @code{make},
4801variables are called @dfn{macros}.)
4802@cindex macro
4803
4804Variables and functions in all parts of a makefile are expanded when
4805read, except for in recipes, the right-hand sides of variable
4806definitions using @samp{=}, and the bodies of variable definitions
4807using the @code{define} directive.@refill
4808
4809Variables can represent lists of file names, options to pass to compilers,
4810programs to run, directories to look in for source files, directories to
4811write output in, or anything else you can imagine.
4812
4813A variable name may be any sequence of characters not containing @samp{:},
4814@samp{#}, @samp{=}, or leading or trailing whitespace. However,
4815variable names containing characters other than letters, numbers, and
4816underscores should be avoided, as they may be given special meanings in the
4817future, and with some shells they cannot be passed through the environment to a
4818sub-@code{make}
4819(@pxref{Variables/Recursion, ,Communicating Variables to a Sub-@code{make}}).
4820
4821Variable names are case-sensitive. The names @samp{foo}, @samp{FOO},
4822and @samp{Foo} all refer to different variables.
4823
4824It is traditional to use upper case letters in variable names, but we
4825recommend using lower case letters for variable names that serve internal
4826purposes in the makefile, and reserving upper case for parameters that
4827control implicit rules or for parameters that the user should override with
4828command options (@pxref{Overriding, ,Overriding Variables}).
4829
4830A few variables have names that are a single punctuation character or
4831just a few characters. These are the @dfn{automatic variables}, and
4832they have particular specialized uses. @xref{Automatic Variables}.
4833
4834@menu
4835* Reference:: How to use the value of a variable.
4836* Flavors:: Variables come in two flavors.
4837* Advanced:: Advanced features for referencing a variable.
4838* Values:: All the ways variables get their values.
4839* Setting:: How to set a variable in the makefile.
4840* Appending:: How to append more text to the old value
4841 of a variable.
4842* Override Directive:: How to set a variable in the makefile even if
4843 the user has set it with a command argument.
4844* Multi-Line:: An alternate way to set a variable
4845 to a multi-line string.
4846* Undefine Directive:: How to undefine a variable so that it appears
4847 as if it was never set.
4848* Environment:: Variable values can come from the environment.
4849* Target-specific:: Variable values can be defined on a per-target
4850 basis.
4851* Pattern-specific:: Target-specific variable values can be applied
4852 to a group of targets that match a pattern.
4853* Suppressing Inheritance:: Suppress inheritance of variables.
4854* Special Variables:: Variables with special meaning or behavior.
4855@end menu
4856
4857@node Reference, Flavors, Using Variables, Using Variables
4858@section Basics of Variable References
4859@cindex variables, how to reference
4860@cindex reference to variables
4861@cindex @code{$}, in variable reference
4862@cindex dollar sign (@code{$}), in variable reference
4863
4864To substitute a variable's value, write a dollar sign followed by the name
4865of the variable in parentheses or braces: either @samp{$(foo)} or
4866@samp{$@{foo@}} is a valid reference to the variable @code{foo}. This
4867special significance of @samp{$} is why you must write @samp{$$} to have
4868the effect of a single dollar sign in a file name or recipe.
4869
4870Variable references can be used in any context: targets, prerequisites,
4871recipes, most directives, and new variable values. Here is an
4872example of a common case, where a variable holds the names of all the
4873object files in a program:
4874
4875@example
4876@group
4877objects = program.o foo.o utils.o
4878program : $(objects)
4879 cc -o program $(objects)
4880
4881$(objects) : defs.h
4882@end group
4883@end example
4884
4885Variable references work by strict textual substitution. Thus, the rule
4886
4887@example
4888@group
4889foo = c
4890prog.o : prog.$(foo)
4891 $(foo)$(foo) -$(foo) prog.$(foo)
4892@end group
4893@end example
4894
4895@noindent
4896could be used to compile a C program @file{prog.c}. Since spaces before
4897the variable value are ignored in variable assignments, the value of
4898@code{foo} is precisely @samp{c}. (Don't actually write your makefiles
4899this way!)
4900
4901A dollar sign followed by a character other than a dollar sign,
4902open-parenthesis or open-brace treats that single character as the
4903variable name. Thus, you could reference the variable @code{x} with
4904@samp{$x}. However, this practice is strongly discouraged, except in
4905the case of the automatic variables (@pxref{Automatic Variables}).
4906
4907@node Flavors, Advanced, Reference, Using Variables
4908@section The Two Flavors of Variables
4909@cindex flavors of variables
4910@cindex recursive variable expansion
4911@cindex variables, flavors
4912@cindex recursively expanded variables
4913@cindex variables, recursively expanded
4914
4915There are two ways that a variable in GNU @code{make} can have a value;
4916we call them the two @dfn{flavors} of variables. The two flavors are
4917distinguished in how they are defined and in what they do when expanded.
4918
4919@cindex =
4920The first flavor of variable is a @dfn{recursively expanded} variable.
4921Variables of this sort are defined by lines using @samp{=}
4922(@pxref{Setting, ,Setting Variables}) or by the @code{define} directive
4923(@pxref{Multi-Line, ,Defining Multi-Line Variables}). The value you specify
4924is installed verbatim; if it contains references to other variables,
4925these references are expanded whenever this variable is substituted (in
4926the course of expanding some other string). When this happens, it is
4927called @dfn{recursive expansion}.@refill
4928
4929For example,
4930
4931@example
4932foo = $(bar)
4933bar = $(ugh)
4934ugh = Huh?
4935
4936all:;echo $(foo)
4937@end example
4938
4939@noindent
4940will echo @samp{Huh?}: @samp{$(foo)} expands to @samp{$(bar)} which
4941expands to @samp{$(ugh)} which finally expands to @samp{Huh?}.@refill
4942
4943This flavor of variable is the only sort supported by other versions of
4944@code{make}. It has its advantages and its disadvantages. An advantage
4945(most would say) is that:
4946
4947@example
4948CFLAGS = $(include_dirs) -O
4949include_dirs = -Ifoo -Ibar
4950@end example
4951
4952@noindent
4953will do what was intended: when @samp{CFLAGS} is expanded in a recipe,
4954it will expand to @samp{-Ifoo -Ibar -O}. A major disadvantage is that you
4955cannot append something on the end of a variable, as in
4956
4957@example
4958CFLAGS = $(CFLAGS) -O
4959@end example
4960
4961@noindent
4962because it will cause an infinite loop in the variable expansion.
4963(Actually @code{make} detects the infinite loop and reports an error.)
4964@cindex loops in variable expansion
4965@cindex variables, loops in expansion
4966
4967Another disadvantage is that any functions
4968(@pxref{Functions, ,Functions for Transforming Text})
4969referenced in the definition will be executed every time the variable is
4970expanded. This makes @code{make} run slower; worse, it causes the
4971@code{wildcard} and @code{shell} functions to give unpredictable results
4972because you cannot easily control when they are called, or even how many
4973times.
4974
4975To avoid all the problems and inconveniences of recursively expanded
4976variables, there is another flavor: simply expanded variables.
4977
4978@cindex simply expanded variables
4979@cindex variables, simply expanded
4980@cindex :=
4981@dfn{Simply expanded variables} are defined by lines using @samp{:=}
4982(@pxref{Setting, ,Setting Variables}).
4983The value of a simply expanded variable is scanned
4984once and for all, expanding any references to other variables and
4985functions, when the variable is defined. The actual value of the simply
4986expanded variable is the result of expanding the text that you write.
4987It does not contain any references to other variables; it contains their
4988values @emph{as of the time this variable was defined}. Therefore,
4989
4990@example
4991x := foo
4992y := $(x) bar
4993x := later
4994@end example
4995
4996@noindent
4997is equivalent to
4998
4999@example
5000y := foo bar
5001x := later
5002@end example
5003
5004When a simply expanded variable is referenced, its value is substituted
5005verbatim.
5006
5007Here is a somewhat more complicated example, illustrating the use of
5008@samp{:=} in conjunction with the @code{shell} function.
5009(@xref{Shell Function, , The @code{shell} Function}.) This example
5010also shows use of the variable @code{MAKELEVEL}, which is changed
5011when it is passed down from level to level.
5012(@xref{Variables/Recursion, , Communicating Variables to a
5013Sub-@code{make}}, for information about @code{MAKELEVEL}.)
5014
5015@vindex MAKELEVEL
5016@vindex MAKE
5017@example
5018@group
5019ifeq (0,$@{MAKELEVEL@})
5020whoami := $(shell whoami)
5021host-type := $(shell arch)
5022MAKE := $@{MAKE@} host-type=$@{host-type@} whoami=$@{whoami@}
5023endif
5024@end group
5025@end example
5026
5027@noindent
5028An advantage of this use of @samp{:=} is that a typical
5029`descend into a directory' recipe then looks like this:
5030
5031@example
5032@group
5033$@{subdirs@}:
5034 $@{MAKE@} -C $@@ all
5035@end group
5036@end example
5037
5038Simply expanded variables generally make complicated makefile programming
5039more predictable because they work like variables in most programming
5040languages. They allow you to redefine a variable using its own value (or
5041its value processed in some way by one of the expansion functions) and to
5042use the expansion functions much more efficiently
5043(@pxref{Functions, ,Functions for Transforming Text}).
5044
5045@cindex spaces, in variable values
5046@cindex whitespace, in variable values
5047@cindex variables, spaces in values
5048You can also use them to introduce controlled leading whitespace into
5049variable values. Leading whitespace characters are discarded from your
5050input before substitution of variable references and function calls;
5051this means you can include leading spaces in a variable value by
5052protecting them with variable references, like this:
5053
5054@example
5055nullstring :=
5056space := $(nullstring) # end of the line
5057@end example
5058
5059@noindent
5060Here the value of the variable @code{space} is precisely one space. The
5061comment @w{@samp{# end of the line}} is included here just for clarity.
5062Since trailing space characters are @emph{not} stripped from variable
5063values, just a space at the end of the line would have the same effect
5064(but be rather hard to read). If you put whitespace at the end of a
5065variable value, it is a good idea to put a comment like that at the end
5066of the line to make your intent clear. Conversely, if you do @emph{not}
5067want any whitespace characters at the end of your variable value, you
5068must remember not to put a random comment on the end of the line after
5069some whitespace, such as this:
5070
5071@example
5072dir := /foo/bar # directory to put the frobs in
5073@end example
5074
5075@noindent
5076Here the value of the variable @code{dir} is @w{@samp{/foo/bar }}
5077(with four trailing spaces), which was probably not the intention.
5078(Imagine something like @w{@samp{$(dir)/file}} with this definition!)
5079
5080@cindex conditional variable assignment
5081@cindex variables, conditional assignment
5082@cindex ?=
5083There is another assignment operator for variables, @samp{?=}. This
5084is called a conditional variable assignment operator, because it only
5085has an effect if the variable is not yet defined. This statement:
5086
5087@example
5088FOO ?= bar
5089@end example
5090
5091@noindent
5092is exactly equivalent to this
5093(@pxref{Origin Function, ,The @code{origin} Function}):
5094
5095@example
5096ifeq ($(origin FOO), undefined)
5097 FOO = bar
5098endif
5099@end example
5100
5101Note that a variable set to an empty value is still defined, so
5102@samp{?=} will not set that variable.
5103
5104@node Advanced, Values, Flavors, Using Variables
5105@section Advanced Features for Reference to Variables
5106@cindex reference to variables
5107
5108This section describes some advanced features you can use to reference
5109variables in more flexible ways.
5110
5111@menu
5112* Substitution Refs:: Referencing a variable with
5113 substitutions on the value.
5114* Computed Names:: Computing the name of the variable to refer to.
5115@end menu
5116
5117@node Substitution Refs, Computed Names, Advanced, Advanced
5118@subsection Substitution References
5119@cindex modified variable reference
5120@cindex substitution variable reference
5121@cindex variables, modified reference
5122@cindex variables, substitution reference
5123
5124@cindex variables, substituting suffix in
5125@cindex suffix, substituting in variables
5126A @dfn{substitution reference} substitutes the value of a variable with
5127alterations that you specify. It has the form
5128@samp{$(@var{var}:@var{a}=@var{b})} (or
5129@samp{$@{@var{var}:@var{a}=@var{b}@}}) and its meaning is to take the value
5130of the variable @var{var}, replace every @var{a} at the end of a word with
5131@var{b} in that value, and substitute the resulting string.
5132
5133When we say ``at the end of a word'', we mean that @var{a} must appear
5134either followed by whitespace or at the end of the value in order to be
5135replaced; other occurrences of @var{a} in the value are unaltered. For
5136example:@refill
5137
5138@example
5139foo := a.o b.o c.o
5140bar := $(foo:.o=.c)
5141@end example
5142
5143@noindent
5144sets @samp{bar} to @samp{a.c b.c c.c}. @xref{Setting, ,Setting Variables}.
5145
5146A substitution reference is actually an abbreviation for use of the
5147@code{patsubst} expansion function (@pxref{Text Functions, ,Functions for String Substitution and Analysis}). We provide
5148substitution references as well as @code{patsubst} for compatibility with
5149other implementations of @code{make}.
5150
5151@findex patsubst
5152Another type of substitution reference lets you use the full power of
5153the @code{patsubst} function. It has the same form
5154@samp{$(@var{var}:@var{a}=@var{b})} described above, except that now
5155@var{a} must contain a single @samp{%} character. This case is
5156equivalent to @samp{$(patsubst @var{a},@var{b},$(@var{var}))}.
5157@xref{Text Functions, ,Functions for String Substitution and Analysis},
5158for a description of the @code{patsubst} function.@refill
5159
5160@example
5161@group
5162@exdent For example:
5163
5164foo := a.o b.o c.o
5165bar := $(foo:%.o=%.c)
5166@end group
5167@end example
5168
5169@noindent
5170sets @samp{bar} to @samp{a.c b.c c.c}.
5171
5172@node Computed Names, , Substitution Refs, Advanced
5173@subsection Computed Variable Names
5174@cindex nested variable reference
5175@cindex computed variable name
5176@cindex variables, computed names
5177@cindex variables, nested references
5178@cindex variables, @samp{$} in name
5179@cindex @code{$}, in variable name
5180@cindex dollar sign (@code{$}), in variable name
5181
5182Computed variable names are a complicated concept needed only for
5183sophisticated makefile programming. For most purposes you need not
5184consider them, except to know that making a variable with a dollar sign
5185in its name might have strange results. However, if you are the type
5186that wants to understand everything, or you are actually interested in
5187what they do, read on.
5188
5189Variables may be referenced inside the name of a variable. This is
5190called a @dfn{computed variable name} or a @dfn{nested variable
5191reference}. For example,
5192
5193@example
5194x = y
5195y = z
5196a := $($(x))
5197@end example
5198
5199@noindent
5200defines @code{a} as @samp{z}: the @samp{$(x)} inside @samp{$($(x))} expands
5201to @samp{y}, so @samp{$($(x))} expands to @samp{$(y)} which in turn expands
5202to @samp{z}. Here the name of the variable to reference is not stated
5203explicitly; it is computed by expansion of @samp{$(x)}. The reference
5204@samp{$(x)} here is nested within the outer variable reference.
5205
5206The previous example shows two levels of nesting, but any number of levels
5207is possible. For example, here are three levels:
5208
5209@example
5210x = y
5211y = z
5212z = u
5213a := $($($(x)))
5214@end example
5215
5216@noindent
5217Here the innermost @samp{$(x)} expands to @samp{y}, so @samp{$($(x))}
5218expands to @samp{$(y)} which in turn expands to @samp{z}; now we have
5219@samp{$(z)}, which becomes @samp{u}.
5220
5221References to recursively-expanded variables within a variable name are
5222reexpanded in the usual fashion. For example:
5223
5224@example
5225x = $(y)
5226y = z
5227z = Hello
5228a := $($(x))
5229@end example
5230
5231@noindent
5232defines @code{a} as @samp{Hello}: @samp{$($(x))} becomes @samp{$($(y))}
5233which becomes @samp{$(z)} which becomes @samp{Hello}.
5234
5235Nested variable references can also contain modified references and
5236function invocations (@pxref{Functions, ,Functions for Transforming Text}),
5237just like any other reference.
5238For example, using the @code{subst} function
5239(@pxref{Text Functions, ,Functions for String Substitution and Analysis}):
5240
5241@example
5242@group
5243x = variable1
5244variable2 := Hello
5245y = $(subst 1,2,$(x))
5246z = y
5247a := $($($(z)))
5248@end group
5249@end example
5250
5251@noindent
5252eventually defines @code{a} as @samp{Hello}. It is doubtful that anyone
5253would ever want to write a nested reference as convoluted as this one, but
5254it works: @samp{$($($(z)))} expands to @samp{$($(y))} which becomes
5255@samp{$($(subst 1,2,$(x)))}. This gets the value @samp{variable1} from
5256@code{x} and changes it by substitution to @samp{variable2}, so that the
5257entire string becomes @samp{$(variable2)}, a simple variable reference
5258whose value is @samp{Hello}.@refill
5259
5260A computed variable name need not consist entirely of a single variable
5261reference. It can contain several variable references, as well as some
5262invariant text. For example,
5263
5264@example
5265@group
5266a_dirs := dira dirb
52671_dirs := dir1 dir2
5268@end group
5269
5270@group
5271a_files := filea fileb
52721_files := file1 file2
5273@end group
5274
5275@group
5276ifeq "$(use_a)" "yes"
5277a1 := a
5278else
5279a1 := 1
5280endif
5281@end group
5282
5283@group
5284ifeq "$(use_dirs)" "yes"
5285df := dirs
5286else
5287df := files
5288endif
5289
5290dirs := $($(a1)_$(df))
5291@end group
5292@end example
5293
5294@noindent
5295will give @code{dirs} the same value as @code{a_dirs}, @code{1_dirs},
5296@code{a_files} or @code{1_files} depending on the settings of @code{use_a}
5297and @code{use_dirs}.@refill
5298
5299Computed variable names can also be used in substitution references:
5300
5301@example
5302@group
5303a_objects := a.o b.o c.o
53041_objects := 1.o 2.o 3.o
5305
5306sources := $($(a1)_objects:.o=.c)
5307@end group
5308@end example
5309
5310@noindent
5311defines @code{sources} as either @samp{a.c b.c c.c} or @samp{1.c 2.c 3.c},
5312depending on the value of @code{a1}.
5313
5314The only restriction on this sort of use of nested variable references
5315is that they cannot specify part of the name of a function to be called.
5316This is because the test for a recognized function name is done before
5317the expansion of nested references. For example,
5318
5319@example
5320@group
5321ifdef do_sort
5322func := sort
5323else
5324func := strip
5325endif
5326@end group
5327
5328@group
5329bar := a d b g q c
5330@end group
5331
5332@group
5333foo := $($(func) $(bar))
5334@end group
5335@end example
5336
5337@noindent
5338attempts to give @samp{foo} the value of the variable @samp{sort a d b g
5339q c} or @samp{strip a d b g q c}, rather than giving @samp{a d b g q c}
5340as the argument to either the @code{sort} or the @code{strip} function.
5341This restriction could be removed in the future if that change is shown
5342to be a good idea.
5343
5344You can also use computed variable names in the left-hand side of a
5345variable assignment, or in a @code{define} directive, as in:
5346
5347@example
5348dir = foo
5349$(dir)_sources := $(wildcard $(dir)/*.c)
5350define $(dir)_print =
5351lpr $($(dir)_sources)
5352endef
5353@end example
5354
5355@noindent
5356This example defines the variables @samp{dir}, @samp{foo_sources}, and
5357@samp{foo_print}.
5358
5359Note that @dfn{nested variable references} are quite different from
5360@dfn{recursively expanded variables}
5361(@pxref{Flavors, ,The Two Flavors of Variables}), though both are
5362used together in complex ways when doing makefile programming.@refill
5363
5364@node Values, Setting, Advanced, Using Variables
5365@section How Variables Get Their Values
5366@cindex variables, how they get their values
5367@cindex value, how a variable gets it
5368
5369Variables can get values in several different ways:
5370
5371@itemize @bullet
5372@item
5373You can specify an overriding value when you run @code{make}.
5374@xref{Overriding, ,Overriding Variables}.
5375
5376@item
5377You can specify a value in the makefile, either
5378with an assignment (@pxref{Setting, ,Setting Variables}) or with a
5379verbatim definition (@pxref{Multi-Line, ,Defining Multi-Line Variables}).@refill
5380
5381@item
5382Variables in the environment become @code{make} variables.
5383@xref{Environment, ,Variables from the Environment}.
5384
5385@item
5386Several @dfn{automatic} variables are given new values for each rule.
5387Each of these has a single conventional use.
5388@xref{Automatic Variables}.
5389
5390@item
5391Several variables have constant initial values.
5392@xref{Implicit Variables, ,Variables Used by Implicit Rules}.
5393@end itemize
5394
5395@node Setting, Appending, Values, Using Variables
5396@section Setting Variables
5397@cindex setting variables
5398@cindex variables, setting
5399@cindex =
5400@cindex :=
5401@cindex ?=
5402
5403To set a variable from the makefile, write a line starting with the
5404variable name followed by @samp{=} or @samp{:=}. Whatever follows the
5405@samp{=} or @samp{:=} on the line becomes the value. For example,
5406
5407@example
5408objects = main.o foo.o bar.o utils.o
5409@end example
5410
5411@noindent
5412defines a variable named @code{objects}. Whitespace around the variable
5413name and immediately after the @samp{=} is ignored.
5414
5415Variables defined with @samp{=} are @dfn{recursively expanded} variables.
5416Variables defined with @samp{:=} are @dfn{simply expanded} variables; these
5417definitions can contain variable references which will be expanded before
5418the definition is made. @xref{Flavors, ,The Two Flavors of Variables}.
5419
5420The variable name may contain function and variable references, which
5421are expanded when the line is read to find the actual variable name to use.
5422
5423There is no limit on the length of the value of a variable except the
5424amount of swapping space on the computer. When a variable definition is
5425long, it is a good idea to break it into several lines by inserting
5426backslash-newline at convenient places in the definition. This will not
5427affect the functioning of @code{make}, but it will make the makefile easier
5428to read.
5429
5430Most variable names are considered to have the empty string as a value if
5431you have never set them. Several variables have built-in initial values
5432that are not empty, but you can set them in the usual ways
5433(@pxref{Implicit Variables, ,Variables Used by Implicit Rules}).
5434Several special variables are set
5435automatically to a new value for each rule; these are called the
5436@dfn{automatic} variables (@pxref{Automatic Variables}).
5437
5438If you'd like a variable to be set to a value only if it's not already
5439set, then you can use the shorthand operator @samp{?=} instead of
5440@samp{=}. These two settings of the variable @samp{FOO} are identical
5441(@pxref{Origin Function, ,The @code{origin} Function}):
5442
5443@example
5444FOO ?= bar
5445@end example
5446
5447@noindent
5448and
5449
5450@example
5451ifeq ($(origin FOO), undefined)
5452FOO = bar
5453endif
5454@end example
5455
5456@node Appending, Override Directive, Setting, Using Variables
5457@section Appending More Text to Variables
5458@cindex +=
5459@cindex appending to variables
5460@cindex variables, appending to
5461
5462Often it is useful to add more text to the value of a variable already defined.
5463You do this with a line containing @samp{+=}, like this:
5464
5465@example
5466objects += another.o
5467@end example
5468
5469@noindent
5470This takes the value of the variable @code{objects}, and adds the text
5471@samp{another.o} to it (preceded by a single space). Thus:
5472
5473@example
5474objects = main.o foo.o bar.o utils.o
5475objects += another.o
5476@end example
5477
5478@noindent
5479sets @code{objects} to @samp{main.o foo.o bar.o utils.o another.o}.
5480
5481Using @samp{+=} is similar to:
5482
5483@example
5484objects = main.o foo.o bar.o utils.o
5485objects := $(objects) another.o
5486@end example
5487
5488@noindent
5489but differs in ways that become important when you use more complex values.
5490
5491When the variable in question has not been defined before, @samp{+=}
5492acts just like normal @samp{=}: it defines a recursively-expanded
5493variable. However, when there @emph{is} a previous definition, exactly
5494what @samp{+=} does depends on what flavor of variable you defined
5495originally. @xref{Flavors, ,The Two Flavors of Variables}, for an
5496explanation of the two flavors of variables.
5497
5498When you add to a variable's value with @samp{+=}, @code{make} acts
5499essentially as if you had included the extra text in the initial
5500definition of the variable. If you defined it first with @samp{:=},
5501making it a simply-expanded variable, @samp{+=} adds to that
5502simply-expanded definition, and expands the new text before appending it
5503to the old value just as @samp{:=} does
5504(see @ref{Setting, ,Setting Variables}, for a full explanation of @samp{:=}).
5505In fact,
5506
5507@example
5508variable := value
5509variable += more
5510@end example
5511
5512@noindent
5513is exactly equivalent to:
5514
5515@noindent
5516@example
5517variable := value
5518variable := $(variable) more
5519@end example
5520
5521On the other hand, when you use @samp{+=} with a variable that you defined
5522first to be recursively-expanded using plain @samp{=}, @code{make} does
5523something a bit different. Recall that when you define a
5524recursively-expanded variable, @code{make} does not expand the value you set
5525for variable and function references immediately. Instead it stores the text
5526verbatim, and saves these variable and function references to be expanded
5527later, when you refer to the new variable (@pxref{Flavors, ,The Two Flavors
5528of Variables}). When you use @samp{+=} on a recursively-expanded variable,
5529it is this unexpanded text to which @code{make} appends the new text you
5530specify.
5531
5532@example
5533@group
5534variable = value
5535variable += more
5536@end group
5537@end example
5538
5539@noindent
5540is roughly equivalent to:
5541
5542@example
5543@group
5544temp = value
5545variable = $(temp) more
5546@end group
5547@end example
5548
5549@noindent
5550except that of course it never defines a variable called @code{temp}.
5551The importance of this comes when the variable's old value contains
5552variable references. Take this common example:
5553
5554@example
5555CFLAGS = $(includes) -O
5556@dots{}
5557CFLAGS += -pg # enable profiling
5558@end example
5559
5560@noindent
5561The first line defines the @code{CFLAGS} variable with a reference to another
5562variable, @code{includes}. (@code{CFLAGS} is used by the rules for C
5563compilation; @pxref{Catalogue of Rules, ,Catalogue of Implicit Rules}.)
5564Using @samp{=} for the definition makes @code{CFLAGS} a recursively-expanded
5565variable, meaning @w{@samp{$(includes) -O}} is @emph{not} expanded when
5566@code{make} processes the definition of @code{CFLAGS}. Thus, @code{includes}
5567need not be defined yet for its value to take effect. It only has to be
5568defined before any reference to @code{CFLAGS}. If we tried to append to the
5569value of @code{CFLAGS} without using @samp{+=}, we might do it like this:
5570
5571@example
5572CFLAGS := $(CFLAGS) -pg # enable profiling
5573@end example
5574
5575@noindent
5576This is pretty close, but not quite what we want. Using @samp{:=}
5577redefines @code{CFLAGS} as a simply-expanded variable; this means
5578@code{make} expands the text @w{@samp{$(CFLAGS) -pg}} before setting the
5579variable. If @code{includes} is not yet defined, we get @w{@samp{ -O
5580-pg}}, and a later definition of @code{includes} will have no effect.
5581Conversely, by using @samp{+=} we set @code{CFLAGS} to the
5582@emph{unexpanded} value @w{@samp{$(includes) -O -pg}}. Thus we preserve
5583the reference to @code{includes}, so if that variable gets defined at
5584any later point, a reference like @samp{$(CFLAGS)} still uses its
5585value.
5586
5587@node Override Directive, Multi-Line, Appending, Using Variables
5588@section The @code{override} Directive
5589@findex override
5590@cindex overriding with @code{override}
5591@cindex variables, overriding
5592
5593If a variable has been set with a command argument
5594(@pxref{Overriding, ,Overriding Variables}),
5595then ordinary assignments in the makefile are ignored. If you want to set
5596the variable in the makefile even though it was set with a command
5597argument, you can use an @code{override} directive, which is a line that
5598looks like this:@refill
5599
5600@example
5601override @var{variable} = @var{value}
5602@end example
5603
5604@noindent
5605or
5606
5607@example
5608override @var{variable} := @var{value}
5609@end example
5610
5611To append more text to a variable defined on the command line, use:
5612
5613@example
5614override @var{variable} += @var{more text}
5615@end example
5616
5617@noindent
5618@xref{Appending, ,Appending More Text to Variables}.
5619
5620Variable assignments marked with the @code{override} flag have a
5621higher priority than all other assignments, except another
5622@code{override}. Subsequent assignments or appends to this variable
5623which are not marked @code{override} will be ignored.
5624
5625The @code{override} directive was not invented for escalation in the war
5626between makefiles and command arguments. It was invented so you can alter
5627and add to values that the user specifies with command arguments.
5628
5629For example, suppose you always want the @samp{-g} switch when you run the
5630C compiler, but you would like to allow the user to specify the other
5631switches with a command argument just as usual. You could use this
5632@code{override} directive:
5633
5634@example
5635override CFLAGS += -g
5636@end example
5637
5638You can also use @code{override} directives with @code{define} directives.
5639This is done as you might expect:
5640
5641@example
5642override define foo =
5643bar
5644endef
5645@end example
5646
5647@noindent
5648@iftex
5649See the next section for information about @code{define}.
5650@end iftex
5651@ifnottex
5652@xref{Multi-Line, ,Defining Multi-Line Variables}.
5653@end ifnottex
5654
5655@node Multi-Line, Undefine Directive, Override Directive, Using Variables
5656@section Defining Multi-Line Variables
5657@findex define
5658@findex endef
5659@cindex multi-line variable definition
5660@cindex variables, multi-line
5661@cindex verbatim variable definition
5662@cindex defining variables verbatim
5663@cindex variables, defining verbatim
5664
5665Another way to set the value of a variable is to use the @code{define}
5666directive. This directive has an unusual syntax which allows newline
5667characters to be included in the value, which is convenient for
5668defining both canned sequences of commands (@pxref{Canned Recipes,
5669,Defining Canned Recipes}), and also sections of makefile syntax to
5670use with @code{eval} (@pxref{Eval Function}).@refill
5671
5672The @code{define} directive is followed on the same line by the name
5673of the variable being defined and an (optional) assignment operator,
5674and nothing more. The value to give the variable appears on the
5675following lines. The end of the value is marked by a line containing
5676just the word @code{endef}. Aside from this difference in syntax,
5677@code{define} works just like any other variable definition. The
5678variable name may contain function and variable references, which are
5679expanded when the directive is read to find the actual variable name
5680to use.
5681
5682You may omit the variable assignment operator if you prefer. If
5683omitted, @code{make} assumes it to be @samp{=} and creates a
5684recursively-expanded variable (@pxref{Flavors, ,The Two Flavors of Variables}).
5685When using a @samp{+=} operator, the value is appended to the previous
5686value as with any other append operation: with a single space
5687separating the old and new values.
5688
5689You may nest @code{define} directives: @code{make} will keep track of
5690nested directives and report an error if they are not all properly
5691closed with @code{endef}. Note that lines beginning with the recipe
5692prefix character are considered part of a recipe, so any @code{define}
5693or @code{endef} strings appearing on such a line will not be
5694considered @code{make} directives.
5695
5696@example
5697define two-lines =
5698echo foo
5699echo $(bar)
5700endef
5701@end example
5702
5703The value in an ordinary assignment cannot contain a newline; but the
5704newlines that separate the lines of the value in a @code{define} become
5705part of the variable's value (except for the final newline which precedes
5706the @code{endef} and is not considered part of the value).@refill
5707
5708@need 800
5709When used in a recipe, the previous example is functionally equivalent
5710to this:
5711
5712@example
5713two-lines = echo foo; echo $(bar)
5714@end example
5715
5716@noindent
5717since two commands separated by semicolon behave much like two separate
5718shell commands. However, note that using two separate lines means
5719@code{make} will invoke the shell twice, running an independent subshell
5720for each line. @xref{Execution, ,Recipe Execution}.
5721
5722If you want variable definitions made with @code{define} to take
5723precedence over command-line variable definitions, you can use the
5724@code{override} directive together with @code{define}:
5725
5726@example
5727override define two-lines =
5728foo
5729$(bar)
5730endef
5731@end example
5732
5733@noindent
5734@xref{Override Directive, ,The @code{override} Directive}.
5735
5736@node Undefine Directive, Environment, Multi-Line, Using Variables
5737@section Undefining Variables
5738@findex undefine
5739@cindex undefining variable
5740
5741If you want to clear a variable, setting its value to empty is usually
5742sufficient. Expanding such a variable will yield the same result (empty
5743string) regardless of whether it was set or not. However, if you are
5744using the @code{flavor} (@pxref{Flavor Function}) and
5745@code{origin} (@pxref{Origin Function}) functions, there is a difference
5746between a variable that was never set and a variable with an empty value.
5747In such situations you may want to use the @code{undefine} directive to
5748make a variable appear as if it was never set. For example:
5749
5750@example
5751foo := foo
5752bar = bar
5753
5754undefine foo
5755undefine bar
5756
5757$(info $(origin foo))
5758$(info $(flavor bar))
5759@end example
5760
5761This example will print ``undefined'' for both variables.
5762
5763If you want to undefine a command-line variable definition, you can use
5764the @code{override} directive together with @code{undefine}, similar to
5765how this is done for variable definitions:
5766
5767@example
5768override undefine CFLAGS
5769@end example
5770
5771@node Environment, Target-specific, Undefine Directive, Using Variables
5772@section Variables from the Environment
5773
5774@cindex variables, environment
5775@cindex environment
5776Variables in @code{make} can come from the environment in which
5777@code{make} is run. Every environment variable that @code{make} sees
5778when it starts up is transformed into a @code{make} variable with the
5779same name and value. However, an explicit assignment in the makefile,
5780or with a command argument, overrides the environment. (If the
5781@samp{-e} flag is specified, then values from the environment override
5782assignments in the makefile. @xref{Options Summary, ,Summary of
5783Options}. But this is not recommended practice.)
5784
5785Thus, by setting the variable @code{CFLAGS} in your environment, you can
5786cause all C compilations in most makefiles to use the compiler switches you
5787prefer. This is safe for variables with standard or conventional meanings
5788because you know that no makefile will use them for other things. (Note
5789this is not totally reliable; some makefiles set @code{CFLAGS} explicitly
5790and therefore are not affected by the value in the environment.)
5791
5792When @code{make} runs a recipe, variables defined in the
5793makefile are placed into the environment of each shell. This allows
5794you to pass values to sub-@code{make} invocations (@pxref{Recursion,
5795,Recursive Use of @code{make}}). By default, only variables that came
5796from the environment or the command line are passed to recursive
5797invocations. You can use the @code{export} directive to pass other
5798variables. @xref{Variables/Recursion, , Communicating Variables to a
5799Sub-@code{make}}, for full details.
5800
5801Other use of variables from the environment is not recommended. It is not
5802wise for makefiles to depend for their functioning on environment variables
5803set up outside their control, since this would cause different users to get
5804different results from the same makefile. This is against the whole
5805purpose of most makefiles.
5806
5807@cindex SHELL, import from environment
5808Such problems would be especially likely with the variable
5809@code{SHELL}, which is normally present in the environment to specify
5810the user's choice of interactive shell. It would be very undesirable
5811for this choice to affect @code{make}; so, @code{make} handles the
5812@code{SHELL} environment variable in a special way; see @ref{Choosing
5813the Shell}.@refill
5814
5815@node Target-specific, Pattern-specific, Environment, Using Variables
5816@section Target-specific Variable Values
5817@cindex target-specific variables
5818@cindex variables, target-specific
5819
5820Variable values in @code{make} are usually global; that is, they are the
5821same regardless of where they are evaluated (unless they're reset, of
5822course). One exception to that is automatic variables
5823(@pxref{Automatic Variables}).
5824
5825The other exception is @dfn{target-specific variable values}. This
5826feature allows you to define different values for the same variable,
5827based on the target that @code{make} is currently building. As with
5828automatic variables, these values are only available within the context
5829of a target's recipe (and in other target-specific assignments).
5830
5831Set a target-specific variable value like this:
5832
5833@example
5834@var{target} @dots{} : @var{variable-assignment}
5835@end example
5836
5837Target-specific variable assignments can be prefixed with any or all of the
5838special keywords @code{export}, @code{override}, or @code{private};
5839these apply their normal behavior to this instance of the variable only.
5840
5841Multiple @var{target} values create a target-specific variable value for
5842each member of the target list individually.
5843
5844The @var{variable-assignment} can be any valid form of assignment;
5845recursive (@samp{=}), static (@samp{:=}), appending (@samp{+=}), or
5846conditional (@samp{?=}). All variables that appear within the
5847@var{variable-assignment} are evaluated within the context of the
5848target: thus, any previously-defined target-specific variable values
5849will be in effect. Note that this variable is actually distinct from
5850any ``global'' value: the two variables do not have to have the same
5851flavor (recursive vs.@: static).
5852
5853Target-specific variables have the same priority as any other makefile
5854variable. Variables provided on the command line (and in the
5855environment if the @samp{-e} option is in force) will take precedence.
5856Specifying the @code{override} directive will allow the target-specific
5857variable value to be preferred.
5858
5859There is one more special feature of target-specific variables: when
5860you define a target-specific variable that variable value is also in
5861effect for all prerequisites of this target, and all their
5862prerequisites, etc.@: (unless those prerequisites override that variable
5863with their own target-specific variable value). So, for example, a
5864statement like this:
5865
5866@example
5867prog : CFLAGS = -g
5868prog : prog.o foo.o bar.o
5869@end example
5870
5871@noindent
5872will set @code{CFLAGS} to @samp{-g} in the recipe for @file{prog}, but
5873it will also set @code{CFLAGS} to @samp{-g} in the recipes that create
5874@file{prog.o}, @file{foo.o}, and @file{bar.o}, and any recipes which
5875create their prerequisites.
5876
5877Be aware that a given prerequisite will only be built once per
5878invocation of make, at most. If the same file is a prerequisite of
5879multiple targets, and each of those targets has a different value for
5880the same target-specific variable, then the first target to be built
5881will cause that prerequisite to be built and the prerequisite will
5882inherit the target-specific value from the first target. It will
5883ignore the target-specific values from any other targets.
5884
5885@node Pattern-specific, Suppressing Inheritance, Target-specific, Using Variables
5886@section Pattern-specific Variable Values
5887@cindex pattern-specific variables
5888@cindex variables, pattern-specific
5889
5890In addition to target-specific variable values
5891(@pxref{Target-specific, ,Target-specific Variable Values}), GNU
5892@code{make} supports pattern-specific variable values. In this form,
5893the variable is defined for any target that matches the pattern
5894specified.
5895
5896Set a pattern-specific variable value like this:
5897
5898@example
5899@var{pattern} @dots{} : @var{variable-assignment}
5900@end example
5901where @var{pattern} is a %-pattern. As with target-specific variable
5902values, multiple @var{pattern} values create a pattern-specific variable
5903value for each pattern individually. The @var{variable-assignment} can
5904be any valid form of assignment. Any command line variable setting will
5905take precedence, unless @code{override} is specified.
5906
5907For example:
5908
5909@example
5910%.o : CFLAGS = -O
5911@end example
5912
5913@noindent
5914will assign @code{CFLAGS} the value of @samp{-O} for all targets
5915matching the pattern @code{%.o}.
5916
5917If a target matches more than one pattern, the matching pattern-specific
5918variables with longer stems are interpreted first. This results in more
5919specific variables taking precedence over the more generic ones, for
5920example:
5921
5922@example
5923%.o: %.c
5924 $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@@
5925
5926lib/%.o: CFLAGS := -fPIC -g
5927%.o: CFLAGS := -g
5928
5929all: foo.o lib/bar.o
5930@end example
5931
5932In this example the first definition of the @code{CFLAGS} variable
5933will be used to update @file{lib/bar.o} even though the second one
5934also applies to this target. Pattern-specific variables which result
5935in the same stem length are considered in the order in which they
5936were defined in the makefile.
5937
5938Pattern-specific variables are searched after any target-specific
5939variables defined explicitly for that target, and before target-specific
5940variables defined for the parent target.
5941
5942@node Suppressing Inheritance, Special Variables, Pattern-specific, Using Variables
5943@section Suppressing Inheritance
5944@findex private
5945@cindex suppressing inheritance
5946@cindex inheritance, suppressing
5947
5948As described in previous sections, @code{make} variables are inherited
5949by prerequisites. This capability allows you to modify the behavior
5950of a prerequisite based on which targets caused it to be rebuilt. For
5951example, you might set a target-specific variable on a @code{debug}
5952target, then running @samp{make debug} will cause that variable to be
5953inherited by all prerequisites of @code{debug}, while just running
5954@samp{make all} (for example) would not have that assignment.
5955
5956Sometimes, however, you may not want a variable to be inherited. For
5957these situations, @code{make} provides the @code{private} modifier.
5958Although this modifier can be used with any variable assignment, it
5959makes the most sense with target- and pattern-specific variables. Any
5960variable marked @code{private} will be visible to its local target but
5961will not be inherited by prerequisites of that target. A global
5962variable marked @code{private} will be visible in the global scope but
5963will not be inherited by any target, and hence will not be visible
5964in any recipe.
5965
5966As an example, consider this makefile:
5967@example
5968EXTRA_CFLAGS =
5969
5970prog: private EXTRA_CFLAGS = -L/usr/local/lib
5971prog: a.o b.o
5972@end example
5973
5974Due to the @code{private} modifier, @code{a.o} and @code{b.o} will not
5975inherit the @code{EXTRA_CFLAGS} variable assignment from the
5976@code{progs} target.
5977
5978@node Special Variables, , Suppressing Inheritance, Using Variables
5979@comment node-name, next, previous, up
5980@section Other Special Variables
5981@cindex makefiles, and special variables
5982@cindex special variables
5983
5984GNU @code{make} supports some variables that have special properties.
5985
5986@table @code
5987
5988@vindex MAKEFILE_LIST @r{(list of parsed makefiles)}
5989@cindex makefiles, and @code{MAKEFILE_LIST} variable
5990@cindex including (@code{MAKEFILE_LIST} variable)
5991@item MAKEFILE_LIST
5992Contains the name of each makefile that is parsed by @code{make}, in
5993the order in which it was parsed. The name is appended just
5994before @code{make} begins to parse the makefile. Thus, if the first
5995thing a makefile does is examine the last word in this variable, it
5996will be the name of the current makefile. Once the current makefile
5997has used @code{include}, however, the last word will be the
5998just-included makefile.
5999
6000If a makefile named @code{Makefile} has this content:
6001
6002@example
6003@group
6004name1 := $(lastword $(MAKEFILE_LIST))
6005
6006include inc.mk
6007
6008name2 := $(lastword $(MAKEFILE_LIST))
6009
6010all:
6011 @@echo name1 = $(name1)
6012 @@echo name2 = $(name2)
6013@end group
6014@end example
6015
6016@noindent
6017then you would expect to see this output:
6018
6019@example
6020@group
6021name1 = Makefile
6022name2 = inc.mk
6023@end group
6024@end example
6025
6026@vindex .DEFAULT_GOAL @r{(define default goal)}
6027@item .DEFAULT_GOAL
6028Sets the default goal to be used if no targets were specified on the
6029command line (@pxref{Goals, , Arguments to Specify the Goals}). The
6030@code{.DEFAULT_GOAL} variable allows you to discover the current
6031default goal, restart the default goal selection algorithm by clearing
6032its value, or to explicitly set the default goal. The following
6033example illustrates these cases:
6034
6035@example
6036@group
6037# Query the default goal.
6038ifeq ($(.DEFAULT_GOAL),)
6039 $(warning no default goal is set)
6040endif
6041
6042.PHONY: foo
6043foo: ; @@echo $@@
6044
6045$(warning default goal is $(.DEFAULT_GOAL))
6046
6047# Reset the default goal.
6048.DEFAULT_GOAL :=
6049
6050.PHONY: bar
6051bar: ; @@echo $@@
6052
6053$(warning default goal is $(.DEFAULT_GOAL))
6054
6055# Set our own.
6056.DEFAULT_GOAL := foo
6057@end group
6058@end example
6059
6060This makefile prints:
6061
6062@example
6063@group
6064no default goal is set
6065default goal is foo
6066default goal is bar
6067foo
6068@end group
6069@end example
6070
6071Note that assigning more than one target name to @code{.DEFAULT_GOAL} is
6072illegal and will result in an error.
6073
6074@vindex MAKE_RESTARTS @r{(number of times @code{make} has restarted)}
6075@item MAKE_RESTARTS
6076This variable is set only if this instance of @code{make} has
6077restarted (@pxref{Remaking Makefiles, , How Makefiles Are Remade}): it
6078will contain the number of times this instance has restarted. Note
6079this is not the same as recursion (counted by the @code{MAKELEVEL}
6080variable). You should not set, modify, or export this variable.
6081
6082@vindex .RECIPEPREFIX @r{(change the recipe prefix character)}
6083@item .RECIPEPREFIX
6084The first character of the value of this variable is used as the
6085character make assumes is introducing a recipe line. If the variable
6086is empty (as it is by default) that character is the standard tab
6087character. For example, this is a valid makefile:
6088
6089@example
6090@group
6091.RECIPEPREFIX = >
6092all:
6093> @@echo Hello, world
6094@end group
6095@end example
6096
6097The value of @code{.RECIPEPREFIX} can be changed multiple times; once set
6098it stays in effect for all rules parsed until it is modified.
6099
6100@vindex .VARIABLES @r{(list of variables)}
6101@item .VARIABLES
6102Expands to a list of the @emph{names} of all global variables defined
6103so far. This includes variables which have empty values, as well as
6104built-in variables (@pxref{Implicit Variables, , Variables Used by
6105Implicit Rules}), but does not include any variables which are only
6106defined in a target-specific context. Note that any value you assign
6107to this variable will be ignored; it will always return its special
6108value.
6109
6110@c @vindex .TARGETS @r{(list of targets)}
6111@c @item .TARGETS
6112@c The second special variable is @code{.TARGETS}. When expanded, the
6113@c value consists of a list of all targets defined in all makefiles read
6114@c up until that point. Note it's not enough for a file to be simply
6115@c mentioned in the makefile to be listed in this variable, even if it
6116@c would match an implicit rule and become an ``implicit target''. The
6117@c file must appear as a target, on the left-hand side of a ``:'', to be
6118@c considered a target for the purposes of this variable.
6119
6120@vindex .FEATURES @r{(list of supported features)}
6121@item .FEATURES
6122Expands to a list of special features supported by this version of
6123@code{make}. Possible values include:
6124
6125@table @samp
6126
6127@item archives
6128Supports @code{ar} (archive) files using special filename syntax.
6129@xref{Archives, ,Using @code{make} to Update Archive Files}.
6130
6131@item check-symlink
6132Supports the @code{-L} (@code{--check-symlink-times}) flag.
6133@xref{Options Summary, ,Summary of Options}.
6134
6135@item else-if
6136Supports ``else if'' non-nested conditionals. @xref{Conditional
6137Syntax, ,Syntax of Conditionals}.
6138
6139@item jobserver
6140Supports ``job server'' enhanced parallel builds. @xref{Parallel,
6141,Parallel Execution}.
6142
6143@item second-expansion
6144Supports secondary expansion of prerequisite lists.
6145
6146@item order-only
6147Supports order-only prerequisites. @xref{Prerequisite Types, ,Types
6148of Prerequisites}.
6149
6150@item target-specific
6151Supports target-specific and pattern-specific variable assignments.
6152@xref{Target-specific, ,Target-specific Variable Values}.
6153
6154@end table
6155
6156@vindex .INCLUDE_DIRS @r{(list of include directories)}
6157@item .INCLUDE_DIRS
6158Expands to a list of directories that @code{make} searches for
6159included makefiles (@pxref{Include, , Including Other Makefiles}).
6160
6161@end table
6162
6163@node Conditionals, Functions, Using Variables, Top
6164@chapter Conditional Parts of Makefiles
6165
6166@cindex conditionals
6167A @dfn{conditional} directive causes part of a makefile to be obeyed
6168or ignored depending on the values of variables. Conditionals can
6169compare the value of one variable to another, or the value of a
6170variable to a constant string. Conditionals control what @code{make}
6171actually ``sees'' in the makefile, so they @emph{cannot} be used to
6172control recipes at the time of execution.@refill
6173
6174@menu
6175* Conditional Example:: Example of a conditional
6176* Conditional Syntax:: The syntax of conditionals.
6177* Testing Flags:: Conditionals that test flags.
6178@end menu
6179
6180@node Conditional Example, Conditional Syntax, Conditionals, Conditionals
6181@section Example of a Conditional
6182
6183The following example of a conditional tells @code{make} to use one
6184set of libraries if the @code{CC} variable is @samp{gcc}, and a
6185different set of libraries otherwise. It works by controlling which
6186of two recipe lines will be used for the rule. The result is that
6187@samp{CC=gcc} as an argument to @code{make} changes not only which
6188compiler is used but also which libraries are linked.
6189
6190@example
6191libs_for_gcc = -lgnu
6192normal_libs =
6193
6194foo: $(objects)
6195ifeq ($(CC),gcc)
6196 $(CC) -o foo $(objects) $(libs_for_gcc)
6197else
6198 $(CC) -o foo $(objects) $(normal_libs)
6199endif
6200@end example
6201
6202This conditional uses three directives: one @code{ifeq}, one @code{else}
6203and one @code{endif}.
6204
6205The @code{ifeq} directive begins the conditional, and specifies the
6206condition. It contains two arguments, separated by a comma and surrounded
6207by parentheses. Variable substitution is performed on both arguments and
6208then they are compared. The lines of the makefile following the
6209@code{ifeq} are obeyed if the two arguments match; otherwise they are
6210ignored.
6211
6212The @code{else} directive causes the following lines to be obeyed if the
6213previous conditional failed. In the example above, this means that the
6214second alternative linking command is used whenever the first alternative
6215is not used. It is optional to have an @code{else} in a conditional.
6216
6217The @code{endif} directive ends the conditional. Every conditional must
6218end with an @code{endif}. Unconditional makefile text follows.
6219
6220As this example illustrates, conditionals work at the textual level:
6221the lines of the conditional are treated as part of the makefile, or
6222ignored, according to the condition. This is why the larger syntactic
6223units of the makefile, such as rules, may cross the beginning or the
6224end of the conditional.
6225
6226When the variable @code{CC} has the value @samp{gcc}, the above example has
6227this effect:
6228
6229@example
6230foo: $(objects)
6231 $(CC) -o foo $(objects) $(libs_for_gcc)
6232@end example
6233
6234@noindent
6235When the variable @code{CC} has any other value, the effect is this:
6236
6237@example
6238foo: $(objects)
6239 $(CC) -o foo $(objects) $(normal_libs)
6240@end example
6241
6242Equivalent results can be obtained in another way by conditionalizing a
6243variable assignment and then using the variable unconditionally:
6244
6245@example
6246libs_for_gcc = -lgnu
6247normal_libs =
6248
6249ifeq ($(CC),gcc)
6250 libs=$(libs_for_gcc)
6251else
6252 libs=$(normal_libs)
6253endif
6254
6255foo: $(objects)
6256 $(CC) -o foo $(objects) $(libs)
6257@end example
6258
6259@node Conditional Syntax, Testing Flags, Conditional Example, Conditionals
6260@section Syntax of Conditionals
6261@findex ifdef
6262@findex ifeq
6263@findex ifndef
6264@findex ifneq
6265@findex else
6266@findex endif
6267
6268The syntax of a simple conditional with no @code{else} is as follows:
6269
6270@example
6271@var{conditional-directive}
6272@var{text-if-true}
6273endif
6274@end example
6275
6276@noindent
6277The @var{text-if-true} may be any lines of text, to be considered as part
6278of the makefile if the condition is true. If the condition is false, no
6279text is used instead.
6280
6281The syntax of a complex conditional is as follows:
6282
6283@example
6284@var{conditional-directive}
6285@var{text-if-true}
6286else
6287@var{text-if-false}
6288endif
6289@end example
6290
6291or:
6292
6293@example
6294@var{conditional-directive}
6295@var{text-if-one-is-true}
6296else @var{conditional-directive}
6297@var{text-if-true}
6298else
6299@var{text-if-false}
6300endif
6301@end example
6302
6303@noindent
6304There can be as many ``@code{else} @var{conditional-directive}''
6305clauses as necessary. Once a given condition is true,
6306@var{text-if-true} is used and no other clause is used; if no
6307condition is true then @var{text-if-false} is used. The
6308@var{text-if-true} and @var{text-if-false} can be any number of lines
6309of text.
6310
6311The syntax of the @var{conditional-directive} is the same whether the
6312conditional is simple or complex; after an @code{else} or not. There
6313are four different directives that test different conditions. Here is
6314a table of them:
6315
6316@table @code
6317@item ifeq (@var{arg1}, @var{arg2})
6318@itemx ifeq '@var{arg1}' '@var{arg2}'
6319@itemx ifeq "@var{arg1}" "@var{arg2}"
6320@itemx ifeq "@var{arg1}" '@var{arg2}'
6321@itemx ifeq '@var{arg1}' "@var{arg2}"
6322Expand all variable references in @var{arg1} and @var{arg2} and
6323compare them. If they are identical, the @var{text-if-true} is
6324effective; otherwise, the @var{text-if-false}, if any, is effective.
6325
6326Often you want to test if a variable has a non-empty value. When the
6327value results from complex expansions of variables and functions,
6328expansions you would consider empty may actually contain whitespace
6329characters and thus are not seen as empty. However, you can use the
6330@code{strip} function (@pxref{Text Functions}) to avoid interpreting
6331whitespace as a non-empty value. For example:
6332
6333@example
6334@group
6335ifeq ($(strip $(foo)),)
6336@var{text-if-empty}
6337endif
6338@end group
6339@end example
6340
6341@noindent
6342will evaluate @var{text-if-empty} even if the expansion of
6343@code{$(foo)} contains whitespace characters.
6344
6345@item ifneq (@var{arg1}, @var{arg2})
6346@itemx ifneq '@var{arg1}' '@var{arg2}'
6347@itemx ifneq "@var{arg1}" "@var{arg2}"
6348@itemx ifneq "@var{arg1}" '@var{arg2}'
6349@itemx ifneq '@var{arg1}' "@var{arg2}"
6350Expand all variable references in @var{arg1} and @var{arg2} and
6351compare them. If they are different, the @var{text-if-true} is
6352effective; otherwise, the @var{text-if-false}, if any, is effective.
6353
6354@item ifdef @var{variable-name}
6355The @code{ifdef} form takes the @emph{name} of a variable as its
6356argument, not a reference to a variable. The value of that variable
6357has a non-empty value, the @var{text-if-true} is effective; otherwise,
6358the @var{text-if-false}, if any, is effective. Variables that have
6359never been defined have an empty value. The text @var{variable-name}
6360is expanded, so it could be a variable or function that expands
6361to the name of a variable. For example:
6362
6363@example
6364bar = true
6365foo = bar
6366ifdef $(foo)
6367frobozz = yes
6368endif
6369@end example
6370
6371The variable reference @code{$(foo)} is expanded, yielding @code{bar},
6372which is considered to be the name of a variable. The variable
6373@code{bar} is not expanded, but its value is examined to determine if
6374it is non-empty.
6375
6376Note that @code{ifdef} only tests whether a variable has a value. It
6377does not expand the variable to see if that value is nonempty.
6378Consequently, tests using @code{ifdef} return true for all definitions
6379except those like @code{foo =}. To test for an empty value, use
6380@w{@code{ifeq ($(foo),)}}. For example,
6381
6382@example
6383bar =
6384foo = $(bar)
6385ifdef foo
6386frobozz = yes
6387else
6388frobozz = no
6389endif
6390@end example
6391
6392@noindent
6393sets @samp{frobozz} to @samp{yes}, while:
6394
6395@example
6396foo =
6397ifdef foo
6398frobozz = yes
6399else
6400frobozz = no
6401endif
6402@end example
6403
6404@noindent
6405sets @samp{frobozz} to @samp{no}.
6406
6407@item ifndef @var{variable-name}
6408If the variable @var{variable-name} has an empty value, the
6409@var{text-if-true} is effective; otherwise, the @var{text-if-false},
6410if any, is effective. The rules for expansion and testing of
6411@var{variable-name} are identical to the @code{ifdef} directive.
6412@end table
6413
6414Extra spaces are allowed and ignored at the beginning of the
6415conditional directive line, but a tab is not allowed. (If the line
6416begins with a tab, it will be considered part of a recipe for a rule.)
6417Aside from this, extra spaces or tabs may be inserted with no effect
6418anywhere except within the directive name or within an argument. A
6419comment starting with @samp{#} may appear at the end of the line.
6420
6421The other two directives that play a part in a conditional are @code{else}
6422and @code{endif}. Each of these directives is written as one word, with no
6423arguments. Extra spaces are allowed and ignored at the beginning of the
6424line, and spaces or tabs at the end. A comment starting with @samp{#} may
6425appear at the end of the line.
6426
6427Conditionals affect which lines of the makefile @code{make} uses. If
6428the condition is true, @code{make} reads the lines of the
6429@var{text-if-true} as part of the makefile; if the condition is false,
6430@code{make} ignores those lines completely. It follows that syntactic
6431units of the makefile, such as rules, may safely be split across the
6432beginning or the end of the conditional.@refill
6433
6434@code{make} evaluates conditionals when it reads a makefile.
6435Consequently, you cannot use automatic variables in the tests of
6436conditionals because they are not defined until recipes are run
6437(@pxref{Automatic Variables}).
6438
6439To prevent intolerable confusion, it is not permitted to start a
6440conditional in one makefile and end it in another. However, you may
6441write an @code{include} directive within a conditional, provided you do
6442not attempt to terminate the conditional inside the included file.
6443
6444@node Testing Flags, , Conditional Syntax, Conditionals
6445@section Conditionals that Test Flags
6446
6447You can write a conditional that tests @code{make} command flags such as
6448@samp{-t} by using the variable @code{MAKEFLAGS} together with the
6449@code{findstring} function
6450(@pxref{Text Functions, , Functions for String Substitution and Analysis}).
6451This is useful when @code{touch} is not enough to make a file appear up
6452to date.
6453
6454The @code{findstring} function determines whether one string appears as a
6455substring of another. If you want to test for the @samp{-t} flag,
6456use @samp{t} as the first string and the value of @code{MAKEFLAGS} as
6457the other.
6458
6459For example, here is how to arrange to use @samp{ranlib -t} to finish
6460marking an archive file up to date:
6461
6462@example
6463archive.a: @dots{}
6464ifneq (,$(findstring t,$(MAKEFLAGS)))
6465 +touch archive.a
6466 +ranlib -t archive.a
6467else
6468 ranlib archive.a
6469endif
6470@end example
6471
6472@noindent
6473The @samp{+} prefix marks those recipe lines as ``recursive'' so that
6474they will be executed despite use of the @samp{-t} flag.
6475@xref{Recursion, ,Recursive Use of @code{make}}.
6476
6477@node Functions, Running, Conditionals, Top
6478@chapter Functions for Transforming Text
6479@cindex functions
6480
6481@dfn{Functions} allow you to do text processing in the makefile to
6482compute the files to operate on or the commands to use in recipes.
6483You use a function in a @dfn{function call}, where you give the name
6484of the function and some text (the @dfn{arguments}) for the function
6485to operate on. The result of the function's processing is substituted
6486into the makefile at the point of the call, just as a variable might
6487be substituted.
6488
6489@menu
6490* Syntax of Functions:: How to write a function call.
6491* Text Functions:: General-purpose text manipulation functions.
6492* File Name Functions:: Functions for manipulating file names.
6493* Conditional Functions:: Functions that implement conditions.
6494* Foreach Function:: Repeat some text with controlled variation.
6495* Call Function:: Expand a user-defined function.
6496* Value Function:: Return the un-expanded value of a variable.
6497* Eval Function:: Evaluate the arguments as makefile syntax.
6498* Origin Function:: Find where a variable got its value.
6499* Flavor Function:: Find out the flavor of a variable.
6500* Shell Function:: Substitute the output of a shell command.
6501* Make Control Functions:: Functions that control how make runs.
6502@end menu
6503
6504@node Syntax of Functions, Text Functions, Functions, Functions
6505@section Function Call Syntax
6506@cindex @code{$}, in function call
6507@cindex dollar sign (@code{$}), in function call
6508@cindex arguments of functions
6509@cindex functions, syntax of
6510
6511A function call resembles a variable reference. It looks like this:
6512
6513@example
6514$(@var{function} @var{arguments})
6515@end example
6516
6517@noindent
6518or like this:
6519
6520@example
6521$@{@var{function} @var{arguments}@}
6522@end example
6523
6524Here @var{function} is a function name; one of a short list of names
6525that are part of @code{make}. You can also essentially create your own
6526functions by using the @code{call} builtin function.
6527
6528The @var{arguments} are the arguments of the function. They are
6529separated from the function name by one or more spaces or tabs, and if
6530there is more than one argument, then they are separated by commas.
6531Such whitespace and commas are not part of an argument's value. The
6532delimiters which you use to surround the function call, whether
6533parentheses or braces, can appear in an argument only in matching pairs;
6534the other kind of delimiters may appear singly. If the arguments
6535themselves contain other function calls or variable references, it is
6536wisest to use the same kind of delimiters for all the references; write
6537@w{@samp{$(subst a,b,$(x))}}, not @w{@samp{$(subst a,b,$@{x@})}}. This
6538is because it is clearer, and because only one type of delimiter is
6539matched to find the end of the reference.
6540
6541The text written for each argument is processed by substitution of
6542variables and function calls to produce the argument value, which
6543is the text on which the function acts. The substitution is done in the
6544order in which the arguments appear.
6545
6546Commas and unmatched parentheses or braces cannot appear in the text of an
6547argument as written; leading spaces cannot appear in the text of the first
6548argument as written. These characters can be put into the argument value
6549by variable substitution. First define variables @code{comma} and
6550@code{space} whose values are isolated comma and space characters, then
6551substitute these variables where such characters are wanted, like this:
6552
6553@example
6554@group
6555comma:= ,
6556empty:=
6557space:= $(empty) $(empty)
6558foo:= a b c
6559bar:= $(subst $(space),$(comma),$(foo))
6560# @r{bar is now `a,b,c'.}
6561@end group
6562@end example
6563
6564@noindent
6565Here the @code{subst} function replaces each space with a comma, through
6566the value of @code{foo}, and substitutes the result.
6567
6568@node Text Functions, File Name Functions, Syntax of Functions, Functions
6569@section Functions for String Substitution and Analysis
6570@cindex functions, for text
6571
6572Here are some functions that operate on strings:
6573
6574@table @code
6575@item $(subst @var{from},@var{to},@var{text})
6576@findex subst
6577Performs a textual replacement on the text @var{text}: each occurrence
6578of @var{from} is replaced by @var{to}. The result is substituted for
6579the function call. For example,
6580
6581@example
6582$(subst ee,EE,feet on the street)
6583@end example
6584
6585substitutes the string @samp{fEEt on the strEEt}.
6586
6587@item $(patsubst @var{pattern},@var{replacement},@var{text})
6588@findex patsubst
6589Finds whitespace-separated words in @var{text} that match
6590@var{pattern} and replaces them with @var{replacement}. Here
6591@var{pattern} may contain a @samp{%} which acts as a wildcard,
6592matching any number of any characters within a word. If
6593@var{replacement} also contains a @samp{%}, the @samp{%} is replaced
6594by the text that matched the @samp{%} in @var{pattern}. Only the first
6595@samp{%} in the @var{pattern} and @var{replacement} is treated this
6596way; any subsequent @samp{%} is unchanged.@refill
6597
6598@cindex @code{%}, quoting in @code{patsubst}
6599@cindex @code{%}, quoting with @code{\} (backslash)
6600@cindex @code{\} (backslash), to quote @code{%}
6601@cindex backslash (@code{\}), to quote @code{%}
6602@cindex quoting @code{%}, in @code{patsubst}
6603@samp{%} characters in @code{patsubst} function invocations can be
6604quoted with preceding backslashes (@samp{\}). Backslashes that would
6605otherwise quote @samp{%} characters can be quoted with more backslashes.
6606Backslashes that quote @samp{%} characters or other backslashes are
6607removed from the pattern before it is compared file names or has a stem
6608substituted into it. Backslashes that are not in danger of quoting
6609@samp{%} characters go unmolested. For example, the pattern
6610@file{the\%weird\\%pattern\\} has @samp{the%weird\} preceding the
6611operative @samp{%} character, and @samp{pattern\\} following it. The
6612final two backslashes are left alone because they cannot affect any
6613@samp{%} character.@refill
6614
6615Whitespace between words is folded into single space characters;
6616leading and trailing whitespace is discarded.
6617
6618For example,
6619
6620@example
6621$(patsubst %.c,%.o,x.c.c bar.c)
6622@end example
6623
6624@noindent
6625produces the value @samp{x.c.o bar.o}.
6626
6627Substitution references (@pxref{Substitution Refs, ,Substitution
6628References}) are a simpler way to get the effect of the @code{patsubst}
6629function:
6630
6631@example
6632$(@var{var}:@var{pattern}=@var{replacement})
6633@end example
6634
6635@noindent
6636is equivalent to
6637
6638@example
6639$(patsubst @var{pattern},@var{replacement},$(@var{var}))
6640@end example
6641
6642The second shorthand simplifies one of the most common uses of
6643@code{patsubst}: replacing the suffix at the end of file names.
6644
6645@example
6646$(@var{var}:@var{suffix}=@var{replacement})
6647@end example
6648
6649@noindent
6650is equivalent to
6651
6652@example
6653$(patsubst %@var{suffix},%@var{replacement},$(@var{var}))
6654@end example
6655
6656@noindent
6657For example, you might have a list of object files:
6658
6659@example
6660objects = foo.o bar.o baz.o
6661@end example
6662
6663@noindent
6664To get the list of corresponding source files, you could simply write:
6665
6666@example
6667$(objects:.o=.c)
6668@end example
6669
6670@noindent
6671instead of using the general form:
6672
6673@example
6674$(patsubst %.o,%.c,$(objects))
6675@end example
6676
6677@item $(strip @var{string})
6678@cindex stripping whitespace
6679@cindex whitespace, stripping
6680@cindex spaces, stripping
6681@findex strip
6682Removes leading and trailing whitespace from @var{string} and replaces
6683each internal sequence of one or more whitespace characters with a
6684single space. Thus, @samp{$(strip a b c )} results in @w{@samp{a b c}}.
6685
6686The function @code{strip} can be very useful when used in conjunction
6687with conditionals. When comparing something with the empty string
6688@samp{} using @code{ifeq} or @code{ifneq}, you usually want a string of
6689just whitespace to match the empty string (@pxref{Conditionals}).
6690
6691Thus, the following may fail to have the desired results:
6692
6693@example
6694.PHONY: all
6695ifneq "$(needs_made)" ""
6696all: $(needs_made)
6697else
6698all:;@@echo 'Nothing to make!'
6699endif
6700@end example
6701
6702@noindent
6703Replacing the variable reference @w{@samp{$(needs_made)}} with the
6704function call @w{@samp{$(strip $(needs_made))}} in the @code{ifneq}
6705directive would make it more robust.@refill
6706
6707@item $(findstring @var{find},@var{in})
6708@findex findstring
6709@cindex searching for strings
6710@cindex finding strings
6711@cindex strings, searching for
6712Searches @var{in} for an occurrence of @var{find}. If it occurs, the
6713value is @var{find}; otherwise, the value is empty. You can use this
6714function in a conditional to test for the presence of a specific
6715substring in a given string. Thus, the two examples,
6716
6717@example
6718$(findstring a,a b c)
6719$(findstring a,b c)
6720@end example
6721
6722@noindent
6723produce the values @samp{a} and @samp{} (the empty string),
6724respectively. @xref{Testing Flags}, for a practical application of
6725@code{findstring}.@refill
6726
6727@need 750
6728@findex filter
6729@cindex filtering words
6730@cindex words, filtering
6731@item $(filter @var{pattern}@dots{},@var{text})
6732Returns all whitespace-separated words in @var{text} that @emph{do} match
6733any of the @var{pattern} words, removing any words that @emph{do not}
6734match. The patterns are written using @samp{%}, just like the patterns
6735used in the @code{patsubst} function above.@refill
6736
6737The @code{filter} function can be used to separate out different types
6738of strings (such as file names) in a variable. For example:
6739
6740@example
6741sources := foo.c bar.c baz.s ugh.h
6742foo: $(sources)
6743 cc $(filter %.c %.s,$(sources)) -o foo
6744@end example
6745
6746@noindent
6747says that @file{foo} depends of @file{foo.c}, @file{bar.c},
6748@file{baz.s} and @file{ugh.h} but only @file{foo.c}, @file{bar.c} and
6749@file{baz.s} should be specified in the command to the
6750compiler.@refill
6751
6752@item $(filter-out @var{pattern}@dots{},@var{text})
6753@findex filter-out
6754@cindex filtering out words
6755@cindex words, filtering out
6756Returns all whitespace-separated words in @var{text} that @emph{do not}
6757match any of the @var{pattern} words, removing the words that @emph{do}
6758match one or more. This is the exact opposite of the @code{filter}
6759function.@refill
6760
6761For example, given:
6762
6763@example
6764@group
6765objects=main1.o foo.o main2.o bar.o
6766mains=main1.o main2.o
6767@end group
6768@end example
6769
6770@noindent
6771the following generates a list which contains all the object files not
6772in @samp{mains}:
6773
6774@example
6775$(filter-out $(mains),$(objects))
6776@end example
6777
6778@need 1500
6779@findex sort
6780@cindex sorting words
6781@item $(sort @var{list})
6782Sorts the words of @var{list} in lexical order, removing duplicate
6783words. The output is a list of words separated by single spaces.
6784Thus,
6785
6786@example
6787$(sort foo bar lose)
6788@end example
6789
6790@noindent
6791returns the value @samp{bar foo lose}.
6792
6793@cindex removing duplicate words
6794@cindex duplicate words, removing
6795@cindex words, removing duplicates
6796Incidentally, since @code{sort} removes duplicate words, you can use
6797it for this purpose even if you don't care about the sort order.
6798
6799@item $(word @var{n},@var{text})
6800@findex word
6801@cindex word, selecting a
6802@cindex selecting a word
6803Returns the @var{n}th word of @var{text}. The legitimate values of
6804@var{n} start from 1. If @var{n} is bigger than the number of words
6805in @var{text}, the value is empty. For example,
6806
6807@example
6808$(word 2, foo bar baz)
6809@end example
6810
6811@noindent
6812returns @samp{bar}.
6813
6814@item $(wordlist @var{s},@var{e},@var{text})
6815@findex wordlist
6816@cindex words, selecting lists of
6817@cindex selecting word lists
6818Returns the list of words in @var{text} starting with word @var{s} and
6819ending with word @var{e} (inclusive). The legitimate values of @var{s}
6820start from 1; @var{e} may start from 0. If @var{s} is bigger than the
6821number of words in @var{text}, the value is empty. If @var{e} is
6822bigger than the number of words in @var{text}, words up to the end of
6823@var{text} are returned. If @var{s} is greater than @var{e}, nothing
6824is returned. For example,
6825
6826@example
6827$(wordlist 2, 3, foo bar baz)
6828@end example
6829
6830@noindent
6831returns @samp{bar baz}.
6832
6833@c Following item phrased to prevent overfull hbox. --RJC 17 Jul 92
6834@item $(words @var{text})
6835@findex words
6836@cindex words, finding number
6837Returns the number of words in @var{text}.
6838Thus, the last word of @var{text} is
6839@w{@code{$(word $(words @var{text}),@var{text})}}.@refill
6840
6841@item $(firstword @var{names}@dots{})
6842@findex firstword
6843@cindex words, extracting first
6844The argument @var{names} is regarded as a series of names, separated
6845by whitespace. The value is the first name in the series. The rest
6846of the names are ignored.
6847
6848For example,
6849
6850@example
6851$(firstword foo bar)
6852@end example
6853
6854@noindent
6855produces the result @samp{foo}. Although @code{$(firstword
6856@var{text})} is the same as @code{$(word 1,@var{text})}, the
6857@code{firstword} function is retained for its simplicity.@refill
6858
6859
6860@item $(lastword @var{names}@dots{})
6861@findex lastword
6862@cindex words, extracting last
6863The argument @var{names} is regarded as a series of names, separated
6864by whitespace. The value is the last name in the series.
6865
6866For example,
6867
6868@example
6869$(lastword foo bar)
6870@end example
6871
6872@noindent
6873produces the result @samp{bar}. Although @code{$(lastword
6874@var{text})} is the same as @code{$(word $(words @var{text}),@var{text})},
6875the @code{lastword} function was added for its simplicity and better
6876performance.@refill
6877@end table
6878
6879
6880Here is a realistic example of the use of @code{subst} and
6881@code{patsubst}. Suppose that a makefile uses the @code{VPATH} variable
6882to specify a list of directories that @code{make} should search for
6883prerequisite files
6884(@pxref{General Search, , @code{VPATH} Search Path for All Prerequisites}).
6885This example shows how to
6886tell the C compiler to search for header files in the same list of
6887directories.@refill
6888
6889The value of @code{VPATH} is a list of directories separated by colons,
6890such as @samp{src:../headers}. First, the @code{subst} function is used to
6891change the colons to spaces:
6892
6893@example
6894$(subst :, ,$(VPATH))
6895@end example
6896
6897@noindent
6898This produces @samp{src ../headers}. Then @code{patsubst} is used to turn
6899each directory name into a @samp{-I} flag. These can be added to the
6900value of the variable @code{CFLAGS}, which is passed automatically to the C
6901compiler, like this:
6902
6903@example
6904override CFLAGS += $(patsubst %,-I%,$(subst :, ,$(VPATH)))
6905@end example
6906
6907@noindent
6908The effect is to append the text @samp{-Isrc -I../headers} to the
6909previously given value of @code{CFLAGS}. The @code{override} directive is
6910used so that the new value is assigned even if the previous value of
6911@code{CFLAGS} was specified with a command argument (@pxref{Override
6912Directive, , The @code{override} Directive}).
6913
6914@node File Name Functions, Conditional Functions, Text Functions, Functions
6915@section Functions for File Names
6916@cindex functions, for file names
6917@cindex file name functions
6918
6919Several of the built-in expansion functions relate specifically to
6920taking apart file names or lists of file names.
6921
6922Each of the following functions performs a specific transformation on a
6923file name. The argument of the function is regarded as a series of file
6924names, separated by whitespace. (Leading and trailing whitespace is
6925ignored.) Each file name in the series is transformed in the same way and
6926the results are concatenated with single spaces between them.
6927
6928@table @code
6929@item $(dir @var{names}@dots{})
6930@findex dir
6931@cindex directory part
6932@cindex file name, directory part
6933Extracts the directory-part of each file name in @var{names}. The
6934directory-part of the file name is everything up through (and
6935including) the last slash in it. If the file name contains no slash,
6936the directory part is the string @samp{./}. For example,
6937
6938@example
6939$(dir src/foo.c hacks)
6940@end example
6941
6942@noindent
6943produces the result @samp{src/ ./}.
6944
6945@item $(notdir @var{names}@dots{})
6946@findex notdir
6947@cindex file name, nondirectory part
6948@cindex nondirectory part
6949Extracts all but the directory-part of each file name in @var{names}.
6950If the file name contains no slash, it is left unchanged. Otherwise,
6951everything through the last slash is removed from it.
6952
6953A file name that ends with a slash becomes an empty string. This is
6954unfortunate, because it means that the result does not always have the
6955same number of whitespace-separated file names as the argument had;
6956but we do not see any other valid alternative.
6957
6958For example,
6959
6960@example
6961$(notdir src/foo.c hacks)
6962@end example
6963
6964@noindent
6965produces the result @samp{foo.c hacks}.
6966
6967@item $(suffix @var{names}@dots{})
6968@findex suffix
6969@cindex suffix, function to find
6970@cindex file name suffix
6971Extracts the suffix of each file name in @var{names}. If the file name
6972contains a period, the suffix is everything starting with the last
6973period. Otherwise, the suffix is the empty string. This frequently
6974means that the result will be empty when @var{names} is not, and if
6975@var{names} contains multiple file names, the result may contain fewer
6976file names.
6977
6978For example,
6979
6980@example
6981$(suffix src/foo.c src-1.0/bar.c hacks)
6982@end example
6983
6984@noindent
6985produces the result @samp{.c .c}.
6986
6987@item $(basename @var{names}@dots{})
6988@findex basename
6989@cindex basename
6990@cindex file name, basename of
6991Extracts all but the suffix of each file name in @var{names}. If the
6992file name contains a period, the basename is everything starting up to
6993(and not including) the last period. Periods in the directory part are
6994ignored. If there is no period, the basename is the entire file name.
6995For example,
6996
6997@example
6998$(basename src/foo.c src-1.0/bar hacks)
6999@end example
7000
7001@noindent
7002produces the result @samp{src/foo src-1.0/bar hacks}.
7003
7004@c plural convention with dots (be consistent)
7005@item $(addsuffix @var{suffix},@var{names}@dots{})
7006@findex addsuffix
7007@cindex suffix, adding
7008@cindex file name suffix, adding
7009The argument @var{names} is regarded as a series of names, separated
7010by whitespace; @var{suffix} is used as a unit. The value of
7011@var{suffix} is appended to the end of each individual name and the
7012resulting larger names are concatenated with single spaces between
7013them. For example,
7014
7015@example
7016$(addsuffix .c,foo bar)
7017@end example
7018
7019@noindent
7020produces the result @samp{foo.c bar.c}.
7021
7022@item $(addprefix @var{prefix},@var{names}@dots{})
7023@findex addprefix
7024@cindex prefix, adding
7025@cindex file name prefix, adding
7026The argument @var{names} is regarded as a series of names, separated
7027by whitespace; @var{prefix} is used as a unit. The value of
7028@var{prefix} is prepended to the front of each individual name and the
7029resulting larger names are concatenated with single spaces between
7030them. For example,
7031
7032@example
7033$(addprefix src/,foo bar)
7034@end example
7035
7036@noindent
7037produces the result @samp{src/foo src/bar}.
7038
7039@item $(join @var{list1},@var{list2})
7040@findex join
7041@cindex joining lists of words
7042@cindex words, joining lists
7043Concatenates the two arguments word by word: the two first words (one
7044from each argument) concatenated form the first word of the result, the
7045two second words form the second word of the result, and so on. So the
7046@var{n}th word of the result comes from the @var{n}th word of each
7047argument. If one argument has more words that the other, the extra
7048words are copied unchanged into the result.
7049
7050For example, @samp{$(join a b,.c .o)} produces @samp{a.c b.o}.
7051
7052Whitespace between the words in the lists is not preserved; it is
7053replaced with a single space.
7054
7055This function can merge the results of the @code{dir} and
7056@code{notdir} functions, to produce the original list of files which
7057was given to those two functions.@refill
7058
7059@item $(wildcard @var{pattern})
7060@findex wildcard
7061@cindex wildcard, function
7062The argument @var{pattern} is a file name pattern, typically containing
7063wildcard characters (as in shell file name patterns). The result of
7064@code{wildcard} is a space-separated list of the names of existing files
7065that match the pattern.
7066@xref{Wildcards, ,Using Wildcard Characters in File Names}.
7067
7068@item $(realpath @var{names}@dots{})
7069@findex realpath
7070@cindex realpath
7071@cindex file name, realpath of
7072For each file name in @var{names} return the canonical absolute name.
7073A canonical name does not contain any @code{.} or @code{..} components,
7074nor any repeated path separators (@code{/}) or symlinks. In case of a
7075failure the empty string is returned. Consult the @code{realpath(3)}
7076documentation for a list of possible failure causes.
7077
7078@item $(abspath @var{names}@dots{})
7079@findex abspath
7080@cindex abspath
7081@cindex file name, abspath of
7082For each file name in @var{names} return an absolute name that does
7083not contain any @code{.} or @code{..} components, nor any repeated path
7084separators (@code{/}). Note that, in contrast to @code{realpath}
7085function, @code{abspath} does not resolve symlinks and does not require
7086the file names to refer to an existing file or directory. Use the
7087@code{wildcard} function to test for existence.
7088@end table
7089
7090@node Conditional Functions, Foreach Function, File Name Functions, Functions
7091@section Functions for Conditionals
7092@findex if
7093@cindex conditional expansion
7094There are three functions that provide conditional expansion. A key
7095aspect of these functions is that not all of the arguments are
7096expanded initially. Only those arguments which need to be expanded,
7097will be expanded.
7098
7099@table @code
7100@item $(if @var{condition},@var{then-part}[,@var{else-part}])
7101@findex if
7102The @code{if} function provides support for conditional expansion in a
7103functional context (as opposed to the GNU @code{make} makefile
7104conditionals such as @code{ifeq} (@pxref{Conditional Syntax, ,Syntax of
7105Conditionals}).
7106
7107The first argument, @var{condition}, first has all preceding and
7108trailing whitespace stripped, then is expanded. If it expands to any
7109non-empty string, then the condition is considered to be true. If it
7110expands to an empty string, the condition is considered to be false.
7111
7112If the condition is true then the second argument, @var{then-part}, is
7113evaluated and this is used as the result of the evaluation of the entire
7114@code{if} function.
7115
7116If the condition is false then the third argument, @var{else-part}, is
7117evaluated and this is the result of the @code{if} function. If there is
7118no third argument, the @code{if} function evaluates to nothing (the
7119empty string).
7120
7121Note that only one of the @var{then-part} or the @var{else-part} will be
7122evaluated, never both. Thus, either can contain side-effects (such as
7123@code{shell} function calls, etc.)
7124
7125@item $(or @var{condition1}[,@var{condition2}[,@var{condition3}@dots{}]])
7126@findex or
7127The @code{or} function provides a ``short-circuiting'' OR operation.
7128Each argument is expanded, in order. If an argument expands to a
7129non-empty string the processing stops and the result of the expansion
7130is that string. If, after all arguments are expanded, all of them are
7131false (empty), then the result of the expansion is the empty string.
7132
7133@item $(and @var{condition1}[,@var{condition2}[,@var{condition3}@dots{}]])
7134@findex and
7135The @code{and} function provides a ``short-circuiting'' AND operation.
7136Each argument is expanded, in order. If an argument expands to an
7137empty string the processing stops and the result of the expansion is
7138the empty string. If all arguments expand to a non-empty string then
7139the result of the expansion is the expansion of the last argument.
7140
7141@end table
7142
7143@node Foreach Function, Call Function, Conditional Functions, Functions
7144@section The @code{foreach} Function
7145@findex foreach
7146@cindex words, iterating over
7147
7148The @code{foreach} function is very different from other functions. It
7149causes one piece of text to be used repeatedly, each time with a different
7150substitution performed on it. It resembles the @code{for} command in the
7151shell @code{sh} and the @code{foreach} command in the C-shell @code{csh}.
7152
7153The syntax of the @code{foreach} function is:
7154
7155@example
7156$(foreach @var{var},@var{list},@var{text})
7157@end example
7158
7159@noindent
7160The first two arguments, @var{var} and @var{list}, are expanded before
7161anything else is done; note that the last argument, @var{text}, is
7162@strong{not} expanded at the same time. Then for each word of the expanded
7163value of @var{list}, the variable named by the expanded value of @var{var}
7164is set to that word, and @var{text} is expanded. Presumably @var{text}
7165contains references to that variable, so its expansion will be different
7166each time.
7167
7168The result is that @var{text} is expanded as many times as there are
7169whitespace-separated words in @var{list}. The multiple expansions of
7170@var{text} are concatenated, with spaces between them, to make the result
7171of @code{foreach}.
7172
7173This simple example sets the variable @samp{files} to the list of all files
7174in the directories in the list @samp{dirs}:
7175
7176@example
7177dirs := a b c d
7178files := $(foreach dir,$(dirs),$(wildcard $(dir)/*))
7179@end example
7180
7181Here @var{text} is @samp{$(wildcard $(dir)/*)}. The first repetition
7182finds the value @samp{a} for @code{dir}, so it produces the same result
7183as @samp{$(wildcard a/*)}; the second repetition produces the result
7184of @samp{$(wildcard b/*)}; and the third, that of @samp{$(wildcard c/*)}.
7185
7186This example has the same result (except for setting @samp{dirs}) as
7187the following example:
7188
7189@example
7190files := $(wildcard a/* b/* c/* d/*)
7191@end example
7192
7193When @var{text} is complicated, you can improve readability by giving it
7194a name, with an additional variable:
7195
7196@example
7197find_files = $(wildcard $(dir)/*)
7198dirs := a b c d
7199files := $(foreach dir,$(dirs),$(find_files))
7200@end example
7201
7202@noindent
7203Here we use the variable @code{find_files} this way. We use plain @samp{=}
7204to define a recursively-expanding variable, so that its value contains an
7205actual function call to be reexpanded under the control of @code{foreach};
7206a simply-expanded variable would not do, since @code{wildcard} would be
7207called only once at the time of defining @code{find_files}.
7208
7209The @code{foreach} function has no permanent effect on the variable
7210@var{var}; its value and flavor after the @code{foreach} function call are
7211the same as they were beforehand. The other values which are taken from
7212@var{list} are in effect only temporarily, during the execution of
7213@code{foreach}. The variable @var{var} is a simply-expanded variable
7214during the execution of @code{foreach}. If @var{var} was undefined
7215before the @code{foreach} function call, it is undefined after the call.
7216@xref{Flavors, ,The Two Flavors of Variables}.@refill
7217
7218You must take care when using complex variable expressions that result in
7219variable names because many strange things are valid variable names, but
7220are probably not what you intended. For example,
7221
7222@smallexample
7223files := $(foreach Esta escrito en espanol!,b c ch,$(find_files))
7224@end smallexample
7225
7226@noindent
7227might be useful if the value of @code{find_files} references the variable
7228whose name is @samp{Esta escrito en espanol!} (es un nombre bastante largo,
7229no?), but it is more likely to be a mistake.
7230
7231@node Call Function, Value Function, Foreach Function, Functions
7232@section The @code{call} Function
7233@findex call
7234@cindex functions, user defined
7235@cindex user defined functions
7236
7237The @code{call} function is unique in that it can be used to create new
7238parameterized functions. You can write a complex expression as the
7239value of a variable, then use @code{call} to expand it with different
7240values.
7241
7242The syntax of the @code{call} function is:
7243
7244@example
7245$(call @var{variable},@var{param},@var{param},@dots{})
7246@end example
7247
7248When @code{make} expands this function, it assigns each @var{param} to
7249temporary variables @code{$(1)}, @code{$(2)}, etc. The variable
7250@code{$(0)} will contain @var{variable}. There is no maximum number of
7251parameter arguments. There is no minimum, either, but it doesn't make
7252sense to use @code{call} with no parameters.
7253
7254Then @var{variable} is expanded as a @code{make} variable in the context
7255of these temporary assignments. Thus, any reference to @code{$(1)} in
7256the value of @var{variable} will resolve to the first @var{param} in the
7257invocation of @code{call}.
7258
7259Note that @var{variable} is the @emph{name} of a variable, not a
7260@emph{reference} to that variable. Therefore you would not normally use
7261a @samp{$} or parentheses when writing it. (You can, however, use a
7262variable reference in the name if you want the name not to be a
7263constant.)
7264
7265If @var{variable} is the name of a builtin function, the builtin function
7266is always invoked (even if a @code{make} variable by that name also
7267exists).
7268
7269The @code{call} function expands the @var{param} arguments before
7270assigning them to temporary variables. This means that @var{variable}
7271values containing references to builtin functions that have special
7272expansion rules, like @code{foreach} or @code{if}, may not work as you
7273expect.
7274
7275Some examples may make this clearer.
7276
7277This macro simply reverses its arguments:
7278
7279@smallexample
7280reverse = $(2) $(1)
7281
7282foo = $(call reverse,a,b)
7283@end smallexample
7284
7285@noindent
7286Here @var{foo} will contain @samp{b a}.
7287
7288This one is slightly more interesting: it defines a macro to search for
7289the first instance of a program in @code{PATH}:
7290
7291@smallexample
7292pathsearch = $(firstword $(wildcard $(addsuffix /$(1),$(subst :, ,$(PATH)))))
7293
7294LS := $(call pathsearch,ls)
7295@end smallexample
7296
7297@noindent
7298Now the variable LS contains @code{/bin/ls} or similar.
7299
7300The @code{call} function can be nested. Each recursive invocation gets
7301its own local values for @code{$(1)}, etc.@: that mask the values of
7302higher-level @code{call}. For example, here is an implementation of a
7303@dfn{map} function:
7304
7305@smallexample
7306map = $(foreach a,$(2),$(call $(1),$(a)))
7307@end smallexample
7308
7309Now you can @var{map} a function that normally takes only one argument,
7310such as @code{origin}, to multiple values in one step:
7311
7312@smallexample
7313o = $(call map,origin,o map MAKE)
7314@end smallexample
7315
7316and end up with @var{o} containing something like @samp{file file default}.
7317
7318A final caution: be careful when adding whitespace to the arguments to
7319@code{call}. As with other functions, any whitespace contained in the
7320second and subsequent arguments is kept; this can cause strange
7321effects. It's generally safest to remove all extraneous whitespace when
7322providing parameters to @code{call}.
7323
7324@node Value Function, Eval Function, Call Function, Functions
7325@comment node-name, next, previous, up
7326@section The @code{value} Function
7327@findex value
7328@cindex variables, unexpanded value
7329
7330The @code{value} function provides a way for you to use the value of a
7331variable @emph{without} having it expanded. Please note that this
7332does not undo expansions which have already occurred; for example if
7333you create a simply expanded variable its value is expanded during the
7334definition; in that case the @code{value} function will return the
7335same result as using the variable directly.
7336
7337The syntax of the @code{value} function is:
7338
7339@example
7340$(value @var{variable})
7341@end example
7342
7343Note that @var{variable} is the @emph{name} of a variable; not a
7344@emph{reference} to that variable. Therefore you would not normally
7345use a @samp{$} or parentheses when writing it. (You can, however, use
7346a variable reference in the name if you want the name not to be a
7347constant.)
7348
7349The result of this function is a string containing the value of
7350@var{variable}, without any expansion occurring. For example, in this
7351makefile:
7352
7353@example
7354@group
7355FOO = $PATH
7356
7357all:
7358 @@echo $(FOO)
7359 @@echo $(value FOO)
7360@end group
7361@end example
7362
7363@noindent
7364The first output line would be @code{ATH}, since the ``$P'' would be
7365expanded as a @code{make} variable, while the second output line would
7366be the current value of your @code{$PATH} environment variable, since
7367the @code{value} function avoided the expansion.
7368
7369The @code{value} function is most often used in conjunction with the
7370@code{eval} function (@pxref{Eval Function}).
7371
7372@node Eval Function, Origin Function, Value Function, Functions
7373@comment node-name, next, previous, up
7374@section The @code{eval} Function
7375@findex eval
7376@cindex evaluating makefile syntax
7377@cindex makefile syntax, evaluating
7378
7379The @code{eval} function is very special: it allows you to define new
7380makefile constructs that are not constant; which are the result of
7381evaluating other variables and functions. The argument to the
7382@code{eval} function is expanded, then the results of that expansion
7383are parsed as makefile syntax. The expanded results can define new
7384@code{make} variables, targets, implicit or explicit rules, etc.
7385
7386The result of the @code{eval} function is always the empty string;
7387thus, it can be placed virtually anywhere in a makefile without
7388causing syntax errors.
7389
7390It's important to realize that the @code{eval} argument is expanded
7391@emph{twice}; first by the @code{eval} function, then the results of
7392that expansion are expanded again when they are parsed as makefile
7393syntax. This means you may need to provide extra levels of escaping
7394for ``$'' characters when using @code{eval}. The @code{value}
7395function (@pxref{Value Function}) can sometimes be useful in these
7396situations, to circumvent unwanted expansions.
7397
7398Here is an example of how @code{eval} can be used; this example
7399combines a number of concepts and other functions. Although it might
7400seem overly complex to use @code{eval} in this example, rather than
7401just writing out the rules, consider two things: first, the template
7402definition (in @code{PROGRAM_template}) could need to be much more
7403complex than it is here; and second, you might put the complex,
7404``generic'' part of this example into another makefile, then include
7405it in all the individual makefiles. Now your individual makefiles are
7406quite straightforward.
7407
7408@example
7409@group
7410PROGRAMS = server client
7411
7412server_OBJS = server.o server_priv.o server_access.o
7413server_LIBS = priv protocol
7414
7415client_OBJS = client.o client_api.o client_mem.o
7416client_LIBS = protocol
7417
7418# Everything after this is generic
7419
7420.PHONY: all
7421all: $(PROGRAMS)
7422
7423define PROGRAM_template =
7424 $(1): $$($(1)_OBJS) $$($(1)_LIBS:%=-l%)
7425 ALL_OBJS += $$($(1)_OBJS)
7426endef
7427
7428$(foreach prog,$(PROGRAMS),$(eval $(call PROGRAM_template,$(prog))))
7429
7430$(PROGRAMS):
7431 $(LINK.o) $^ $(LDLIBS) -o $@@
7432
7433clean:
7434 rm -f $(ALL_OBJS) $(PROGRAMS)
7435@end group
7436@end example
7437
7438@node Origin Function, Flavor Function, Eval Function, Functions
7439@section The @code{origin} Function
7440@findex origin
7441@cindex variables, origin of
7442@cindex origin of variable
7443
7444The @code{origin} function is unlike most other functions in that it does
7445not operate on the values of variables; it tells you something @emph{about}
7446a variable. Specifically, it tells you where it came from.
7447
7448The syntax of the @code{origin} function is:
7449
7450@example
7451$(origin @var{variable})
7452@end example
7453
7454Note that @var{variable} is the @emph{name} of a variable to inquire about;
7455not a @emph{reference} to that variable. Therefore you would not normally
7456use a @samp{$} or parentheses when writing it. (You can, however, use a
7457variable reference in the name if you want the name not to be a constant.)
7458
7459The result of this function is a string telling you how the variable
7460@var{variable} was defined:
7461
7462@table @samp
7463@item undefined
7464
7465if @var{variable} was never defined.
7466
7467@item default
7468
7469if @var{variable} has a default definition, as is usual with @code{CC}
7470and so on. @xref{Implicit Variables, ,Variables Used by Implicit Rules}.
7471Note that if you have redefined a default variable, the @code{origin}
7472function will return the origin of the later definition.
7473
7474@item environment
7475
7476if @var{variable} was inherited from the environment provided to
7477@code{make}.
7478
7479@item environment override
7480
7481if @var{variable} was inherited from the environment provided to
7482@code{make}, and is overriding a setting for @var{variable} in the
7483makefile as a result of the @w{@samp{-e}} option (@pxref{Options
7484Summary, ,Summary of Options}).@refill
7485
7486@item file
7487
7488if @var{variable} was defined in a makefile.
7489
7490@item command line
7491
7492if @var{variable} was defined on the command line.
7493
7494@item override
7495
7496if @var{variable} was defined with an @code{override} directive in a
7497makefile (@pxref{Override Directive, ,The @code{override} Directive}).
7498
7499@item automatic
7500
7501if @var{variable} is an automatic variable defined for the execution
7502of the recipe for each rule (@pxref{Automatic Variables}).
7503@end table
7504
7505This information is primarily useful (other than for your curiosity) to
7506determine if you want to believe the value of a variable. For example,
7507suppose you have a makefile @file{foo} that includes another makefile
7508@file{bar}. You want a variable @code{bletch} to be defined in @file{bar}
7509if you run the command @w{@samp{make -f bar}}, even if the environment contains
7510a definition of @code{bletch}. However, if @file{foo} defined
7511@code{bletch} before including @file{bar}, you do not want to override that
7512definition. This could be done by using an @code{override} directive in
7513@file{foo}, giving that definition precedence over the later definition in
7514@file{bar}; unfortunately, the @code{override} directive would also
7515override any command line definitions. So, @file{bar} could
7516include:@refill
7517
7518@example
7519@group
7520ifdef bletch
7521ifeq "$(origin bletch)" "environment"
7522bletch = barf, gag, etc.
7523endif
7524endif
7525@end group
7526@end example
7527
7528@noindent
7529If @code{bletch} has been defined from the environment, this will redefine
7530it.
7531
7532If you want to override a previous definition of @code{bletch} if it came
7533from the environment, even under @samp{-e}, you could instead write:
7534
7535@example
7536@group
7537ifneq "$(findstring environment,$(origin bletch))" ""
7538bletch = barf, gag, etc.
7539endif
7540@end group
7541@end example
7542
7543Here the redefinition takes place if @samp{$(origin bletch)} returns either
7544@samp{environment} or @samp{environment override}.
7545@xref{Text Functions, , Functions for String Substitution and Analysis}.
7546
7547@node Flavor Function, Shell Function, Origin Function, Functions
7548@section The @code{flavor} Function
7549@findex flavor
7550@cindex variables, flavor of
7551@cindex flavor of variable
7552
7553The @code{flavor} function is unlike most other functions (and like
7554@code{origin} function) in that it does not operate on the values of
7555variables; it tells you something @emph{about} a variable.
7556Specifically, it tells you the flavor of a variable (@pxref{Flavors,
7557,The Two Flavors of Variables}).
7558
7559The syntax of the @code{flavor} function is:
7560
7561@example
7562$(flavor @var{variable})
7563@end example
7564
7565Note that @var{variable} is the @emph{name} of a variable to inquire about;
7566not a @emph{reference} to that variable. Therefore you would not normally
7567use a @samp{$} or parentheses when writing it. (You can, however, use a
7568variable reference in the name if you want the name not to be a constant.)
7569
7570The result of this function is a string that identifies the flavor of the
7571variable @var{variable}:
7572
7573@table @samp
7574@item undefined
7575
7576if @var{variable} was never defined.
7577
7578@item recursive
7579
7580if @var{variable} is a recursively expanded variable.
7581
7582@item simple
7583
7584if @var{variable} is a simply expanded variable.
7585
7586@end table
7587
7588
7589@node Shell Function, Make Control Functions, Flavor Function, Functions
7590@section The @code{shell} Function
7591@findex shell
7592@cindex command expansion
7593@cindex backquotes
7594@cindex shell command, function for
7595
7596The @code{shell} function is unlike any other function other than the
7597@code{wildcard} function
7598(@pxref{Wildcard Function, ,The Function @code{wildcard}}) in that it
7599communicates with the world outside of @code{make}.
7600
7601The @code{shell} function performs the same function that backquotes
7602(@samp{`}) perform in most shells: it does @dfn{command expansion}.
7603This means that it takes as an argument a shell command and evaluates
7604to the output of the command. The only processing @code{make} does on
7605the result is to convert each newline (or carriage-return / newline
7606pair) to a single space. If there is a trailing (carriage-return
7607and) newline it will simply be removed.@refill
7608
7609The commands run by calls to the @code{shell} function are run when the
7610function calls are expanded (@pxref{Reading Makefiles, , How
7611@code{make} Reads a Makefile}). Because this function involves
7612spawning a new shell, you should carefully consider the performance
7613implications of using the @code{shell} function within recursively
7614expanded variables vs.@: simply expanded variables (@pxref{Flavors, ,The
7615Two Flavors of Variables}).
7616
7617Here are some examples of the use of the @code{shell} function:
7618
7619@example
7620contents := $(shell cat foo)
7621@end example
7622
7623@noindent
7624sets @code{contents} to the contents of the file @file{foo}, with a space
7625(rather than a newline) separating each line.
7626
7627@example
7628files := $(shell echo *.c)
7629@end example
7630
7631@noindent
7632sets @code{files} to the expansion of @samp{*.c}. Unless @code{make} is
7633using a very strange shell, this has the same result as
7634@w{@samp{$(wildcard *.c)}} (as long as at least one @samp{.c} file
7635exists).@refill
7636
7637@node Make Control Functions, , Shell Function, Functions
7638@section Functions That Control Make
7639@cindex functions, for controlling make
7640@cindex controlling make
7641
7642These functions control the way make runs. Generally, they are used to
7643provide information to the user of the makefile or to cause make to stop
7644if some sort of environmental error is detected.
7645
7646@table @code
7647@item $(error @var{text}@dots{})
7648@findex error
7649@cindex error, stopping on
7650@cindex stopping make
7651Generates a fatal error where the message is @var{text}. Note that
7652the error is generated whenever this function is evaluated. So, if
7653you put it inside a recipe or on the right side of a recursive
7654variable assignment, it won't be evaluated until later. The
7655@var{text} will be expanded before the error is generated.
7656
7657For example,
7658
7659@example
7660ifdef ERROR1
7661$(error error is $(ERROR1))
7662endif
7663@end example
7664
7665@noindent
7666will generate a fatal error during the read of the makefile if the
7667@code{make} variable @code{ERROR1} is defined. Or,
7668
7669@example
7670ERR = $(error found an error!)
7671
7672.PHONY: err
7673err: ; $(ERR)
7674@end example
7675
7676@noindent
7677will generate a fatal error while @code{make} is running, if the
7678@code{err} target is invoked.
7679
7680@item $(warning @var{text}@dots{})
7681@findex warning
7682@cindex warnings, printing
7683@cindex printing user warnings
7684This function works similarly to the @code{error} function, above,
7685except that @code{make} doesn't exit. Instead, @var{text} is expanded
7686and the resulting message is displayed, but processing of the makefile
7687continues.
7688
7689The result of the expansion of this function is the empty string.
7690
7691@item $(info @var{text}@dots{})
7692@findex info
7693@cindex printing messages
7694This function does nothing more than print its (expanded) argument(s)
7695to standard output. No makefile name or line number is added. The
7696result of the expansion of this function is the empty string.
7697@end table
7698
7699@node Running, Implicit Rules, Functions, Top
7700@chapter How to Run @code{make}
7701
7702A makefile that says how to recompile a program can be used in more
7703than one way. The simplest use is to recompile every file that is out
7704of date. Usually, makefiles are written so that if you run
7705@code{make} with no arguments, it does just that.
7706
7707But you might want to update only some of the files; you might want to use
7708a different compiler or different compiler options; you might want just to
7709find out which files are out of date without changing them.
7710
7711By giving arguments when you run @code{make}, you can do any of these
7712things and many others.
7713
7714@cindex exit status of make
7715The exit status of @code{make} is always one of three values:
7716@table @code
7717@item 0
7718The exit status is zero if @code{make} is successful.
7719@item 2
7720The exit status is two if @code{make} encounters any errors.
7721It will print messages describing the particular errors.
7722@item 1
7723The exit status is one if you use the @samp{-q} flag and @code{make}
7724determines that some target is not already up to date.
7725@xref{Instead of Execution, ,Instead of Executing Recipes}.
7726@end table
7727
7728@menu
7729* Makefile Arguments:: How to specify which makefile to use.
7730* Goals:: How to use goal arguments to specify which
7731 parts of the makefile to use.
7732* Instead of Execution:: How to use mode flags to specify what
7733 kind of thing to do with the recipes
7734 in the makefile other than simply
7735 execute them.
7736* Avoiding Compilation:: How to avoid recompiling certain files.
7737* Overriding:: How to override a variable to specify
7738 an alternate compiler and other things.
7739* Testing:: How to proceed past some errors, to
7740 test compilation.
7741* Options Summary:: Summary of Options
7742@end menu
7743
7744@node Makefile Arguments, Goals, Running, Running
7745@section Arguments to Specify the Makefile
7746@cindex @code{--file}
7747@cindex @code{--makefile}
7748@cindex @code{-f}
7749
7750The way to specify the name of the makefile is with the @samp{-f} or
7751@samp{--file} option (@samp{--makefile} also works). For example,
7752@samp{-f altmake} says to use the file @file{altmake} as the makefile.
7753
7754If you use the @samp{-f} flag several times and follow each @samp{-f}
7755with an argument, all the specified files are used jointly as
7756makefiles.
7757
7758If you do not use the @samp{-f} or @samp{--file} flag, the default is
7759to try @file{GNUmakefile}, @file{makefile}, and @file{Makefile}, in
7760that order, and use the first of these three which exists or can be made
7761(@pxref{Makefiles, ,Writing Makefiles}).@refill
7762
7763@node Goals, Instead of Execution, Makefile Arguments, Running
7764@section Arguments to Specify the Goals
7765@cindex goal, how to specify
7766
7767The @dfn{goals} are the targets that @code{make} should strive ultimately
7768to update. Other targets are updated as well if they appear as
7769prerequisites of goals, or prerequisites of prerequisites of goals, etc.
7770
7771By default, the goal is the first target in the makefile (not counting
7772targets that start with a period). Therefore, makefiles are usually
7773written so that the first target is for compiling the entire program or
7774programs they describe. If the first rule in the makefile has several
7775targets, only the first target in the rule becomes the default goal, not
7776the whole list. You can manage the selection of the default goal from
7777within your makefile using the @code{.DEFAULT_GOAL} variable
7778(@pxref{Special Variables, , Other Special Variables}).
7779
7780You can also specify a different goal or goals with command line
7781arguments to @code{make}. Use the name of the goal as an argument.
7782If you specify several goals, @code{make} processes each of them in
7783turn, in the order you name them.
7784
7785Any target in the makefile may be specified as a goal (unless it
7786starts with @samp{-} or contains an @samp{=}, in which case it will be
7787parsed as a switch or variable definition, respectively). Even
7788targets not in the makefile may be specified, if @code{make} can find
7789implicit rules that say how to make them.
7790
7791@vindex MAKECMDGOALS
7792@code{Make} will set the special variable @code{MAKECMDGOALS} to the
7793list of goals you specified on the command line. If no goals were given
7794on the command line, this variable is empty. Note that this variable
7795should be used only in special circumstances.
7796
7797An example of appropriate use is to avoid including @file{.d} files
7798during @code{clean} rules (@pxref{Automatic Prerequisites}), so
7799@code{make} won't create them only to immediately remove them
7800again:@refill
7801
7802@example
7803@group
7804sources = foo.c bar.c
7805
7806ifneq ($(MAKECMDGOALS),clean)
7807include $(sources:.c=.d)
7808endif
7809@end group
7810@end example
7811
7812One use of specifying a goal is if you want to compile only a part of
7813the program, or only one of several programs. Specify as a goal each
7814file that you wish to remake. For example, consider a directory containing
7815several programs, with a makefile that starts like this:
7816
7817@example
7818.PHONY: all
7819all: size nm ld ar as
7820@end example
7821
7822If you are working on the program @code{size}, you might want to say
7823@w{@samp{make size}} so that only the files of that program are recompiled.
7824
7825Another use of specifying a goal is to make files that are not normally
7826made. For example, there may be a file of debugging output, or a
7827version of the program that is compiled specially for testing, which has
7828a rule in the makefile but is not a prerequisite of the default goal.
7829
7830Another use of specifying a goal is to run the recipe associated with
7831a phony target (@pxref{Phony Targets}) or empty target (@pxref{Empty
7832Targets, ,Empty Target Files to Record Events}). Many makefiles contain
7833a phony target named @file{clean} which deletes everything except source
7834files. Naturally, this is done only if you request it explicitly with
7835@w{@samp{make clean}}. Following is a list of typical phony and empty
7836target names. @xref{Standard Targets}, for a detailed list of all the
7837standard target names which GNU software packages use.
7838
7839@table @file
7840@item all
7841@cindex @code{all} @r{(standard target)}
7842Make all the top-level targets the makefile knows about.
7843
7844@item clean
7845@cindex @code{clean} @r{(standard target)}
7846Delete all files that are normally created by running @code{make}.
7847
7848@item mostlyclean
7849@cindex @code{mostlyclean} @r{(standard target)}
7850Like @samp{clean}, but may refrain from deleting a few files that people
7851normally don't want to recompile. For example, the @samp{mostlyclean}
7852target for GCC does not delete @file{libgcc.a}, because recompiling it
7853is rarely necessary and takes a lot of time.
7854
7855@item distclean
7856@cindex @code{distclean} @r{(standard target)}
7857@itemx realclean
7858@cindex @code{realclean} @r{(standard target)}
7859@itemx clobber
7860@cindex @code{clobber} @r{(standard target)}
7861Any of these targets might be defined to delete @emph{more} files than
7862@samp{clean} does. For example, this would delete configuration files
7863or links that you would normally create as preparation for compilation,
7864even if the makefile itself cannot create these files.
7865
7866@item install
7867@cindex @code{install} @r{(standard target)}
7868Copy the executable file into a directory that users typically search
7869for commands; copy any auxiliary files that the executable uses into
7870the directories where it will look for them.
7871
7872@item print
7873@cindex @code{print} @r{(standard target)}
7874Print listings of the source files that have changed.
7875
7876@item tar
7877@cindex @code{tar} @r{(standard target)}
7878Create a tar file of the source files.
7879
7880@item shar
7881@cindex @code{shar} @r{(standard target)}
7882Create a shell archive (shar file) of the source files.
7883
7884@item dist
7885@cindex @code{dist} @r{(standard target)}
7886Create a distribution file of the source files. This might
7887be a tar file, or a shar file, or a compressed version of one of the
7888above, or even more than one of the above.
7889
7890@item TAGS
7891@cindex @code{TAGS} @r{(standard target)}
7892Update a tags table for this program.
7893
7894@item check
7895@cindex @code{check} @r{(standard target)}
7896@itemx test
7897@cindex @code{test} @r{(standard target)}
7898Perform self tests on the program this makefile builds.
7899@end table
7900
7901@node Instead of Execution, Avoiding Compilation, Goals, Running
7902@section Instead of Executing Recipes
7903@cindex execution, instead of
7904@cindex recipes, instead of executing
7905
7906The makefile tells @code{make} how to tell whether a target is up to date,
7907and how to update each target. But updating the targets is not always
7908what you want. Certain options specify other activities for @code{make}.
7909
7910@comment Extra blank lines make it print better.
7911@table @samp
7912@item -n
7913@itemx --just-print
7914@itemx --dry-run
7915@itemx --recon
7916@cindex @code{--just-print}
7917@cindex @code{--dry-run}
7918@cindex @code{--recon}
7919@cindex @code{-n}
7920
7921``No-op''. The activity is to print what recipe would be used to make
7922the targets up to date, but not actually execute it. Some recipes are
7923still executed, even with this flag (@pxref{MAKE Variable, ,How the @code{MAKE} Variable Works}).
7924
7925@item -t
7926@itemx --touch
7927@cindex @code{--touch}
7928@cindex touching files
7929@cindex target, touching
7930@cindex @code{-t}
7931
7932``Touch''. The activity is to mark the targets as up to date without
7933actually changing them. In other words, @code{make} pretends to compile
7934the targets but does not really change their contents.
7935
7936@item -q
7937@itemx --question
7938@cindex @code{--question}
7939@cindex @code{-q}
7940@cindex question mode
7941
7942``Question''. The activity is to find out silently whether the targets
7943are up to date already; but execute no recipe in either case. In other
7944words, neither compilation nor output will occur.
7945
7946@item -W @var{file}
7947@itemx --what-if=@var{file}
7948@itemx --assume-new=@var{file}
7949@itemx --new-file=@var{file}
7950@cindex @code{--what-if}
7951@cindex @code{-W}
7952@cindex @code{--assume-new}
7953@cindex @code{--new-file}
7954@cindex what if
7955@cindex files, assuming new
7956
7957``What if''. Each @samp{-W} flag is followed by a file name. The given
7958files' modification times are recorded by @code{make} as being the present
7959time, although the actual modification times remain the same.
7960You can use the @samp{-W} flag in conjunction with the @samp{-n} flag
7961to see what would happen if you were to modify specific files.@refill
7962@end table
7963
7964With the @samp{-n} flag, @code{make} prints the recipe that it would
7965normally execute but usually does not execute it.
7966
7967With the @samp{-t} flag, @code{make} ignores the recipes in the rules
7968and uses (in effect) the command @code{touch} for each target that needs to
7969be remade. The @code{touch} command is also printed, unless @samp{-s} or
7970@code{.SILENT} is used. For speed, @code{make} does not actually invoke
7971the program @code{touch}. It does the work directly.
7972
7973With the @samp{-q} flag, @code{make} prints nothing and executes no
7974recipes, but the exit status code it returns is zero if and only if the
7975targets to be considered are already up to date. If the exit status is
7976one, then some updating needs to be done. If @code{make} encounters an
7977error, the exit status is two, so you can distinguish an error from a
7978target that is not up to date.
7979
7980It is an error to use more than one of these three flags in the same
7981invocation of @code{make}.
7982
7983@cindex +, and recipe execution
7984The @samp{-n}, @samp{-t}, and @samp{-q} options do not affect recipe
7985lines that begin with @samp{+} characters or contain the strings
7986@samp{$(MAKE)} or @samp{$@{MAKE@}}. Note that only the line containing
7987the @samp{+} character or the strings @samp{$(MAKE)} or @samp{$@{MAKE@}}
7988is run regardless of these options. Other lines in the same rule are
7989not run unless they too begin with @samp{+} or contain @samp{$(MAKE)} or
7990@samp{$@{MAKE@}} (@xref{MAKE Variable, ,How the @code{MAKE} Variable Works}.)
7991
7992@cindex phony targets and recipe execution
7993The @samp{-t} flag prevents phony targets (@pxref{Phony Targets}) from
7994being updated, unless there are recipe lines beginning with @samp{+}
7995or containing @samp{$(MAKE)} or @samp{$@{MAKE@}}.
7996
7997The @samp{-W} flag provides two features:
7998
7999@itemize @bullet
8000@item
8001If you also use the @samp{-n} or @samp{-q} flag, you can see what
8002@code{make} would do if you were to modify some files.
8003
8004@item
8005Without the @samp{-n} or @samp{-q} flag, when @code{make} is actually
8006executing recipes, the @samp{-W} flag can direct @code{make} to act as
8007if some files had been modified, without actually running the recipes
8008for those files.@refill
8009@end itemize
8010
8011Note that the options @samp{-p} and @samp{-v} allow you to obtain other
8012information about @code{make} or about the makefiles in use
8013(@pxref{Options Summary, ,Summary of Options}).@refill
8014
8015@node Avoiding Compilation, Overriding, Instead of Execution, Running
8016@section Avoiding Recompilation of Some Files
8017@cindex @code{-o}
8018@cindex @code{--old-file}
8019@cindex @code{--assume-old}
8020@cindex files, assuming old
8021@cindex files, avoiding recompilation of
8022@cindex recompilation, avoiding
8023
8024Sometimes you may have changed a source file but you do not want to
8025recompile all the files that depend on it. For example, suppose you add
8026a macro or a declaration to a header file that many other files depend
8027on. Being conservative, @code{make} assumes that any change in the
8028header file requires recompilation of all dependent files, but you know
8029that they do not need to be recompiled and you would rather not waste
8030the time waiting for them to compile.
8031
8032If you anticipate the problem before changing the header file, you can
8033use the @samp{-t} flag. This flag tells @code{make} not to run the
8034recipes in the rules, but rather to mark the target up to date by
8035changing its last-modification date. You would follow this procedure:
8036
8037@enumerate
8038@item
8039Use the command @samp{make} to recompile the source files that really
8040need recompilation, ensuring that the object files are up-to-date
8041before you begin.
8042
8043@item
8044Make the changes in the header files.
8045
8046@item
8047Use the command @samp{make -t} to mark all the object files as
8048up to date. The next time you run @code{make}, the changes in the
8049header files will not cause any recompilation.
8050@end enumerate
8051
8052If you have already changed the header file at a time when some files
8053do need recompilation, it is too late to do this. Instead, you can
8054use the @w{@samp{-o @var{file}}} flag, which marks a specified file as
8055``old'' (@pxref{Options Summary, ,Summary of Options}). This means
8056that the file itself will not be remade, and nothing else will be
8057remade on its account. Follow this procedure:
8058
8059@enumerate
8060@item
8061Recompile the source files that need compilation for reasons independent
8062of the particular header file, with @samp{make -o @var{headerfile}}.
8063If several header files are involved, use a separate @samp{-o} option
8064for each header file.
8065
8066@item
8067Touch all the object files with @samp{make -t}.
8068@end enumerate
8069
8070@node Overriding, Testing, Avoiding Compilation, Running
8071@section Overriding Variables
8072@cindex overriding variables with arguments
8073@cindex variables, overriding with arguments
8074@cindex command line variables
8075@cindex variables, command line
8076
8077An argument that contains @samp{=} specifies the value of a variable:
8078@samp{@var{v}=@var{x}} sets the value of the variable @var{v} to @var{x}.
8079If you specify a value in this way, all ordinary assignments of the same
8080variable in the makefile are ignored; we say they have been
8081@dfn{overridden} by the command line argument.
8082
8083The most common way to use this facility is to pass extra flags to
8084compilers. For example, in a properly written makefile, the variable
8085@code{CFLAGS} is included in each recipe that runs the C compiler, so a
8086file @file{foo.c} would be compiled something like this:
8087
8088@example
8089cc -c $(CFLAGS) foo.c
8090@end example
8091
8092Thus, whatever value you set for @code{CFLAGS} affects each compilation
8093that occurs. The makefile probably specifies the usual value for
8094@code{CFLAGS}, like this:
8095
8096@example
8097CFLAGS=-g
8098@end example
8099
8100Each time you run @code{make}, you can override this value if you
8101wish. For example, if you say @samp{make CFLAGS='-g -O'}, each C
8102compilation will be done with @samp{cc -c -g -O}. (This also
8103illustrates how you can use quoting in the shell to enclose spaces and
8104other special characters in the value of a variable when you override
8105it.)
8106
8107The variable @code{CFLAGS} is only one of many standard variables that
8108exist just so that you can change them this way. @xref{Implicit
8109Variables, , Variables Used by Implicit Rules}, for a complete list.
8110
8111You can also program the makefile to look at additional variables of your
8112own, giving the user the ability to control other aspects of how the
8113makefile works by changing the variables.
8114
8115When you override a variable with a command line argument, you can
8116define either a recursively-expanded variable or a simply-expanded
8117variable. The examples shown above make a recursively-expanded
8118variable; to make a simply-expanded variable, write @samp{:=} instead
8119of @samp{=}. But, unless you want to include a variable reference or
8120function call in the @emph{value} that you specify, it makes no
8121difference which kind of variable you create.
8122
8123There is one way that the makefile can change a variable that you have
8124overridden. This is to use the @code{override} directive, which is a line
8125that looks like this: @samp{override @var{variable} = @var{value}}
8126(@pxref{Override Directive, ,The @code{override} Directive}).
8127
8128@node Testing, Options Summary, Overriding, Running
8129@section Testing the Compilation of a Program
8130@cindex testing compilation
8131@cindex compilation, testing
8132
8133Normally, when an error happens in executing a shell command, @code{make}
8134gives up immediately, returning a nonzero status. No further recipes are
8135executed for any target. The error implies that the goal cannot be
8136correctly remade, and @code{make} reports this as soon as it knows.
8137
8138When you are compiling a program that you have just changed, this is not
8139what you want. Instead, you would rather that @code{make} try compiling
8140every file that can be tried, to show you as many compilation errors
8141as possible.
8142
8143@cindex @code{-k}
8144@cindex @code{--keep-going}
8145On these occasions, you should use the @samp{-k} or
8146@samp{--keep-going} flag. This tells @code{make} to continue to
8147consider the other prerequisites of the pending targets, remaking them
8148if necessary, before it gives up and returns nonzero status. For
8149example, after an error in compiling one object file, @samp{make -k}
8150will continue compiling other object files even though it already
8151knows that linking them will be impossible. In addition to continuing
8152after failed shell commands, @samp{make -k} will continue as much as
8153possible after discovering that it does not know how to make a target
8154or prerequisite file. This will always cause an error message, but
8155without @samp{-k}, it is a fatal error (@pxref{Options Summary,
8156,Summary of Options}).@refill
8157
8158The usual behavior of @code{make} assumes that your purpose is to get the
8159goals up to date; once @code{make} learns that this is impossible, it might
8160as well report the failure immediately. The @samp{-k} flag says that the
8161real purpose is to test as much as possible of the changes made in the
8162program, perhaps to find several independent problems so that you can
8163correct them all before the next attempt to compile. This is why Emacs'
8164@kbd{M-x compile} command passes the @samp{-k} flag by default.
8165
8166@node Options Summary, , Testing, Running
8167@section Summary of Options
8168@cindex options
8169@cindex flags
8170@cindex switches
8171
8172Here is a table of all the options @code{make} understands:
8173
8174@table @samp
8175@item -b
8176@cindex @code{-b}
8177@itemx -m
8178@cindex @code{-m}
8179These options are ignored for compatibility with other versions of @code{make}.
8180
8181@item -B
8182@cindex @code{-B}
8183@itemx --always-make
8184@cindex @code{--always-make}
8185Consider all targets out-of-date. GNU @code{make} proceeds to
8186consider targets and their prerequisites using the normal algorithms;
8187however, all targets so considered are always remade regardless of the
8188status of their prerequisites. To avoid infinite recursion, if
8189@code{MAKE_RESTARTS} (@pxref{Special Variables, , Other Special
8190Variables}) is set to a number greater than 0 this option is disabled
8191when considering whether to remake makefiles (@pxref{Remaking
8192Makefiles, , How Makefiles Are Remade}).
8193
8194@item -C @var{dir}
8195@cindex @code{-C}
8196@itemx --directory=@var{dir}
8197@cindex @code{--directory}
8198Change to directory @var{dir} before reading the makefiles. If multiple
8199@samp{-C} options are specified, each is interpreted relative to the
8200previous one: @samp{-C / -C etc} is equivalent to @samp{-C /etc}.
8201This is typically used with recursive invocations of @code{make}
8202(@pxref{Recursion, ,Recursive Use of @code{make}}).
8203
8204@item -d
8205@cindex @code{-d}
8206@c Extra blank line here makes the table look better.
8207
8208Print debugging information in addition to normal processing. The
8209debugging information says which files are being considered for
8210remaking, which file-times are being compared and with what results,
8211which files actually need to be remade, which implicit rules are
8212considered and which are applied---everything interesting about how
8213@code{make} decides what to do. The @code{-d} option is equivalent to
8214@samp{--debug=a} (see below).
8215
8216@item --debug[=@var{options}]
8217@cindex @code{--debug}
8218@c Extra blank line here makes the table look better.
8219
8220Print debugging information in addition to normal processing. Various
8221levels and types of output can be chosen. With no arguments, print the
8222``basic'' level of debugging. Possible arguments are below; only the
8223first character is considered, and values must be comma- or
8224space-separated.
8225
8226@table @code
8227@item a (@i{all})
8228All types of debugging output are enabled. This is equivalent to using
8229@samp{-d}.
8230
8231@item b (@i{basic})
8232Basic debugging prints each target that was found to be out-of-date, and
8233whether the build was successful or not.
8234
8235@item v (@i{verbose})
8236A level above @samp{basic}; includes messages about which makefiles were
8237parsed, prerequisites that did not need to be rebuilt, etc. This option
8238also enables @samp{basic} messages.
8239
8240@item i (@i{implicit})
8241Prints messages describing the implicit rule searches for each target.
8242This option also enables @samp{basic} messages.
8243
8244@item j (@i{jobs})
8245Prints messages giving details on the invocation of specific subcommands.
8246
8247@item m (@i{makefile})
8248By default, the above messages are not enabled while trying to remake
8249the makefiles. This option enables messages while rebuilding makefiles,
8250too. Note that the @samp{all} option does enable this option. This
8251option also enables @samp{basic} messages.
8252@end table
8253
8254@item -e
8255@cindex @code{-e}
8256@itemx --environment-overrides
8257@cindex @code{--environment-overrides}
8258Give variables taken from the environment precedence
8259over variables from makefiles.
8260@xref{Environment, ,Variables from the Environment}.
8261
8262@item --eval=@var{string}
8263@cindex @code{--eval}
8264@c Extra blank line here makes the table look better.
8265
8266Evaluate @var{string} as makefile syntax. This is a command-line
8267version of the @code{eval} function (@pxref{Eval Function}). The
8268evaluation is performed after the default rules and variables have
8269been defined, but before any makefiles are read.
8270
8271@item -f @var{file}
8272@cindex @code{-f}
8273@itemx --file=@var{file}
8274@cindex @code{--file}
8275@itemx --makefile=@var{file}
8276@cindex @code{--makefile}
8277Read the file named @var{file} as a makefile.
8278@xref{Makefiles, ,Writing Makefiles}.
8279
8280@item -h
8281@cindex @code{-h}
8282@itemx --help
8283@cindex @code{--help}
8284@c Extra blank line here makes the table look better.
8285
8286Remind you of the options that @code{make} understands and then exit.
8287
8288@item -i
8289@cindex @code{-i}
8290@itemx --ignore-errors
8291@cindex @code{--ignore-errors}
8292Ignore all errors in recipes executed to remake files.
8293@xref{Errors, ,Errors in Recipes}.
8294
8295@item -I @var{dir}
8296@cindex @code{-I}
8297@itemx --include-dir=@var{dir}
8298@cindex @code{--include-dir}
8299Specifies a directory @var{dir} to search for included makefiles.
8300@xref{Include, ,Including Other Makefiles}. If several @samp{-I}
8301options are used to specify several directories, the directories are
8302searched in the order specified.
8303
8304@item -j [@var{jobs}]
8305@cindex @code{-j}
8306@itemx --jobs[=@var{jobs}]
8307@cindex @code{--jobs}
8308Specifies the number of recipes (jobs) to run simultaneously. With no
8309argument, @code{make} runs as many recipes simultaneously as possible.
8310If there is more than one @samp{-j} option, the last one is effective.
8311@xref{Parallel, ,Parallel Execution}, for more information on how
8312recipes are run. Note that this option is ignored on MS-DOS.
8313
8314@item -k
8315@cindex @code{-k}
8316@itemx --keep-going
8317@cindex @code{--keep-going}
8318Continue as much as possible after an error. While the target that
8319failed, and those that depend on it, cannot be remade, the other
8320prerequisites of these targets can be processed all the same.
8321@xref{Testing, ,Testing the Compilation of a Program}.
8322
8323@item -l [@var{load}]
8324@cindex @code{-l}
8325@itemx --load-average[=@var{load}]
8326@cindex @code{--load-average}
8327@itemx --max-load[=@var{load}]
8328@cindex @code{--max-load}
8329Specifies that no new recipes should be started if there are other
8330recipes running and the load average is at least @var{load} (a
8331floating-point number). With no argument, removes a previous load
8332limit. @xref{Parallel, ,Parallel Execution}.
8333
8334@item -L
8335@cindex @code{-L}
8336@itemx --check-symlink-times
8337@cindex @code{--check-symlink-times}
8338On systems that support symbolic links, this option causes @code{make}
8339to consider the timestamps on any symbolic links in addition to the
8340timestamp on the file referenced by those links. When this option is
8341provided, the most recent timestamp among the file and the symbolic
8342links is taken as the modification time for this target file.
8343
8344@item -n
8345@cindex @code{-n}
8346@itemx --just-print
8347@cindex @code{--just-print}
8348@itemx --dry-run
8349@cindex @code{--dry-run}
8350@itemx --recon
8351@cindex @code{--recon}
8352@c Extra blank line here makes the table look better.
8353
8354Print the recipe that would be executed, but do not execute it (except
8355in certain circumstances).
8356@xref{Instead of Execution, ,Instead of Executing Recipes}.
8357
8358@item -o @var{file}
8359@cindex @code{-o}
8360@itemx --old-file=@var{file}
8361@cindex @code{--old-file}
8362@itemx --assume-old=@var{file}
8363@cindex @code{--assume-old}
8364Do not remake the file @var{file} even if it is older than its
8365prerequisites, and do not remake anything on account of changes in
8366@var{file}. Essentially the file is treated as very old and its rules
8367are ignored. @xref{Avoiding Compilation, ,Avoiding Recompilation of
8368Some Files}.@refill
8369
8370@item -p
8371@cindex @code{-p}
8372@itemx --print-data-base
8373@cindex @code{--print-data-base}
8374@cindex data base of @code{make} rules
8375@cindex predefined rules and variables, printing
8376Print the data base (rules and variable values) that results from
8377reading the makefiles; then execute as usual or as otherwise
8378specified. This also prints the version information given by the
8379@samp{-v} switch (see below). To print the data base without trying
8380to remake any files, use @w{@samp{make -qp}}. To print the data base
8381of predefined rules and variables, use @w{@samp{make -p -f /dev/null}}.
8382The data base output contains filename and linenumber information for
8383recipe and variable definitions, so it can be a useful debugging tool
8384in complex environments.
8385
8386@item -q
8387@cindex @code{-q}
8388@itemx --question
8389@cindex @code{--question}
8390``Question mode''. Do not run any recipes, or print anything; just
8391return an exit status that is zero if the specified targets are already
8392up to date, one if any remaking is required, or two if an error is
8393encountered. @xref{Instead of Execution, ,Instead of Executing
8394Recipes}.@refill
8395
8396@item -r
8397@cindex @code{-r}
8398@itemx --no-builtin-rules
8399@cindex @code{--no-builtin-rules}
8400Eliminate use of the built-in implicit rules (@pxref{Implicit Rules,
8401,Using Implicit Rules}). You can still define your own by writing
8402pattern rules (@pxref{Pattern Rules, ,Defining and Redefining Pattern
8403Rules}). The @samp{-r} option also clears out the default list of
8404suffixes for suffix rules (@pxref{Suffix Rules, ,Old-Fashioned Suffix
8405Rules}). But you can still define your own suffixes with a rule for
8406@code{.SUFFIXES}, and then define your own suffix rules. Note that only
8407@emph{rules} are affected by the @code{-r} option; default variables
8408remain in effect (@pxref{Implicit Variables, ,Variables Used by Implicit
8409Rules}); see the @samp{-R} option below.
8410
8411@item -R
8412@cindex @code{-R}
8413@itemx --no-builtin-variables
8414@cindex @code{--no-builtin-variables}
8415Eliminate use of the built-in rule-specific variables (@pxref{Implicit
8416Variables, ,Variables Used by Implicit Rules}). You can still define
8417your own, of course. The @samp{-R} option also automatically enables
8418the @samp{-r} option (see above), since it doesn't make sense to have
8419implicit rules without any definitions for the variables that they use.
8420
8421@item -s
8422@cindex @code{-s}
8423@itemx --silent
8424@cindex @code{--silent}
8425@itemx --quiet
8426@cindex @code{--quiet}
8427@c Extra blank line here makes the table look better.
8428
8429Silent operation; do not print the recipes as they are executed.
8430@xref{Echoing, ,Recipe Echoing}.
8431
8432@item -S
8433@cindex @code{-S}
8434@itemx --no-keep-going
8435@cindex @code{--no-keep-going}
8436@itemx --stop
8437@cindex @code{--stop}
8438@c Extra blank line here makes the table look better.
8439
8440Cancel the effect of the @samp{-k} option. This is never necessary
8441except in a recursive @code{make} where @samp{-k} might be inherited
8442from the top-level @code{make} via @code{MAKEFLAGS}
8443(@pxref{Recursion, ,Recursive Use of @code{make}})
8444or if you set @samp{-k} in @code{MAKEFLAGS} in your environment.@refill
8445
8446@item -t
8447@cindex @code{-t}
8448@itemx --touch
8449@cindex @code{--touch}
8450@c Extra blank line here makes the table look better.
8451
8452Touch files (mark them up to date without really changing them)
8453instead of running their recipes. This is used to pretend that the
8454recipes were done, in order to fool future invocations of
8455@code{make}. @xref{Instead of Execution, ,Instead of Executing Recipes}.
8456
8457@item -v
8458@cindex @code{-v}
8459@itemx --version
8460@cindex @code{--version}
8461Print the version of the @code{make} program plus a copyright, a list
8462of authors, and a notice that there is no warranty; then exit.
8463
8464@item -w
8465@cindex @code{-w}
8466@itemx --print-directory
8467@cindex @code{--print-directory}
8468Print a message containing the working directory both before and after
8469executing the makefile. This may be useful for tracking down errors
8470from complicated nests of recursive @code{make} commands.
8471@xref{Recursion, ,Recursive Use of @code{make}}. (In practice, you
8472rarely need to specify this option since @samp{make} does it for you;
8473see @ref{-w Option, ,The @samp{--print-directory} Option}.)
8474
8475@itemx --no-print-directory
8476@cindex @code{--no-print-directory}
8477Disable printing of the working directory under @code{-w}.
8478This option is useful when @code{-w} is turned on automatically,
8479but you do not want to see the extra messages.
8480@xref{-w Option, ,The @samp{--print-directory} Option}.
8481
8482@item -W @var{file}
8483@cindex @code{-W}
8484@itemx --what-if=@var{file}
8485@cindex @code{--what-if}
8486@itemx --new-file=@var{file}
8487@cindex @code{--new-file}
8488@itemx --assume-new=@var{file}
8489@cindex @code{--assume-new}
8490Pretend that the target @var{file} has just been modified. When used
8491with the @samp{-n} flag, this shows you what would happen if you were
8492to modify that file. Without @samp{-n}, it is almost the same as
8493running a @code{touch} command on the given file before running
8494@code{make}, except that the modification time is changed only in the
8495imagination of @code{make}.
8496@xref{Instead of Execution, ,Instead of Executing Recipes}.
8497
8498@item --warn-undefined-variables
8499@cindex @code{--warn-undefined-variables}
8500@cindex variables, warning for undefined
8501@cindex undefined variables, warning message
8502Issue a warning message whenever @code{make} sees a reference to an
8503undefined variable. This can be helpful when you are trying to debug
8504makefiles which use variables in complex ways.
8505@end table
8506
8507@node Implicit Rules, Archives, Running, Top
8508@chapter Using Implicit Rules
8509@cindex implicit rule
8510@cindex rule, implicit
8511
8512Certain standard ways of remaking target files are used very often. For
8513example, one customary way to make an object file is from a C source file
8514using the C compiler, @code{cc}.
8515
8516@dfn{Implicit rules} tell @code{make} how to use customary techniques so
8517that you do not have to specify them in detail when you want to use
8518them. For example, there is an implicit rule for C compilation. File
8519names determine which implicit rules are run. For example, C
8520compilation typically takes a @file{.c} file and makes a @file{.o} file.
8521So @code{make} applies the implicit rule for C compilation when it sees
8522this combination of file name endings.@refill
8523
8524A chain of implicit rules can apply in sequence; for example, @code{make}
8525will remake a @file{.o} file from a @file{.y} file by way of a @file{.c} file.
8526@iftex
8527@xref{Chained Rules, ,Chains of Implicit Rules}.
8528@end iftex
8529
8530The built-in implicit rules use several variables in their recipes so
8531that, by changing the values of the variables, you can change the way the
8532implicit rule works. For example, the variable @code{CFLAGS} controls the
8533flags given to the C compiler by the implicit rule for C compilation.
8534@iftex
8535@xref{Implicit Variables, ,Variables Used by Implicit Rules}.
8536@end iftex
8537
8538You can define your own implicit rules by writing @dfn{pattern rules}.
8539@iftex
8540@xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.
8541@end iftex
8542
8543@dfn{Suffix rules} are a more limited way to define implicit rules.
8544Pattern rules are more general and clearer, but suffix rules are
8545retained for compatibility.
8546@iftex
8547@xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
8548@end iftex
8549
8550@menu
8551* Using Implicit:: How to use an existing implicit rule
8552 to get the recipes for updating a file.
8553* Catalogue of Rules:: A list of built-in implicit rules.
8554* Implicit Variables:: How to change what predefined rules do.
8555* Chained Rules:: How to use a chain of implicit rules.
8556* Pattern Rules:: How to define new implicit rules.
8557* Last Resort:: How to define recipes for rules which
8558 cannot find any.
8559* Suffix Rules:: The old-fashioned style of implicit rule.
8560* Implicit Rule Search:: The precise algorithm for applying
8561 implicit rules.
8562@end menu
8563
8564@node Using Implicit, Catalogue of Rules, Implicit Rules, Implicit Rules
8565@section Using Implicit Rules
8566@cindex implicit rule, how to use
8567@cindex rule, implicit, how to use
8568
8569To allow @code{make} to find a customary method for updating a target
8570file, all you have to do is refrain from specifying recipes yourself.
8571Either write a rule with no recipe, or don't write a rule at all.
8572Then @code{make} will figure out which implicit rule to use based on
8573which kind of source file exists or can be made.
8574
8575For example, suppose the makefile looks like this:
8576
8577@example
8578foo : foo.o bar.o
8579 cc -o foo foo.o bar.o $(CFLAGS) $(LDFLAGS)
8580@end example
8581
8582@noindent
8583Because you mention @file{foo.o} but do not give a rule for it, @code{make}
8584will automatically look for an implicit rule that tells how to update it.
8585This happens whether or not the file @file{foo.o} currently exists.
8586
8587If an implicit rule is found, it can supply both a recipe and one or
8588more prerequisites (the source files). You would want to write a rule
8589for @file{foo.o} with no recipe if you need to specify additional
8590prerequisites, such as header files, that the implicit rule cannot
8591supply.
8592
8593Each implicit rule has a target pattern and prerequisite patterns. There may
8594be many implicit rules with the same target pattern. For example, numerous
8595rules make @samp{.o} files: one, from a @samp{.c} file with the C compiler;
8596another, from a @samp{.p} file with the Pascal compiler; and so on. The rule
8597that actually applies is the one whose prerequisites exist or can be made.
8598So, if you have a file @file{foo.c}, @code{make} will run the C compiler;
8599otherwise, if you have a file @file{foo.p}, @code{make} will run the Pascal
8600compiler; and so on.
8601
8602Of course, when you write the makefile, you know which implicit rule you
8603want @code{make} to use, and you know it will choose that one because you
8604know which possible prerequisite files are supposed to exist.
8605@xref{Catalogue of Rules, ,Catalogue of Implicit Rules},
8606for a catalogue of all the predefined implicit rules.
8607
8608Above, we said an implicit rule applies if the required prerequisites ``exist
8609or can be made''. A file ``can be made'' if it is mentioned explicitly in
8610the makefile as a target or a prerequisite, or if an implicit rule can be
8611recursively found for how to make it. When an implicit prerequisite is the
8612result of another implicit rule, we say that @dfn{chaining} is occurring.
8613@xref{Chained Rules, ,Chains of Implicit Rules}.
8614
8615In general, @code{make} searches for an implicit rule for each target, and
8616for each double-colon rule, that has no recipe. A file that is mentioned
8617only as a prerequisite is considered a target whose rule specifies nothing,
8618so implicit rule search happens for it. @xref{Implicit Rule Search, ,Implicit Rule Search Algorithm}, for the
8619details of how the search is done.
8620
8621Note that explicit prerequisites do not influence implicit rule search.
8622For example, consider this explicit rule:
8623
8624@example
8625foo.o: foo.p
8626@end example
8627
8628@noindent
8629The prerequisite on @file{foo.p} does not necessarily mean that
8630@code{make} will remake @file{foo.o} according to the implicit rule to
8631make an object file, a @file{.o} file, from a Pascal source file, a
8632@file{.p} file. For example, if @file{foo.c} also exists, the implicit
8633rule to make an object file from a C source file is used instead,
8634because it appears before the Pascal rule in the list of predefined
8635implicit rules (@pxref{Catalogue of Rules, , Catalogue of Implicit
8636Rules}).
8637
8638If you do not want an implicit rule to be used for a target that has no
8639recipe, you can give that target an empty recipe by writing a semicolon
8640(@pxref{Empty Recipes, ,Defining Empty Recipes}).
8641
8642@node Catalogue of Rules, Implicit Variables, Using Implicit, Implicit Rules
8643@section Catalogue of Implicit Rules
8644@cindex implicit rule, predefined
8645@cindex rule, implicit, predefined
8646
8647Here is a catalogue of predefined implicit rules which are always
8648available unless the makefile explicitly overrides or cancels them.
8649@xref{Canceling Rules, ,Canceling Implicit Rules}, for information on
8650canceling or overriding an implicit rule. The @samp{-r} or
8651@samp{--no-builtin-rules} option cancels all predefined rules.
8652
8653This manual only documents the default rules available on POSIX-based
8654operating systems. Other operating systems, such as VMS, Windows,
8655OS/2, etc. may have different sets of default rules. To see the full
8656list of default rules and variables available in your version of GNU
8657@code{make}, run @samp{make -p} in a directory with no makefile.
8658
8659Not all of these rules will always be defined, even when the @samp{-r}
8660option is not given. Many of the predefined implicit rules are
8661implemented in @code{make} as suffix rules, so which ones will be
8662defined depends on the @dfn{suffix list} (the list of prerequisites of
8663the special target @code{.SUFFIXES}). The default suffix list is:
8664@code{.out}, @code{.a}, @code{.ln}, @code{.o}, @code{.c}, @code{.cc},
8665@code{.C}, @code{.cpp}, @code{.p}, @code{.f}, @code{.F}, @code{.m},
8666@code{.r}, @code{.y}, @code{.l}, @code{.ym}, @code{.lm}, @code{.s},
8667@code{.S}, @code{.mod}, @code{.sym}, @code{.def}, @code{.h},
8668@code{.info}, @code{.dvi}, @code{.tex}, @code{.texinfo}, @code{.texi},
8669@code{.txinfo}, @code{.w}, @code{.ch} @code{.web}, @code{.sh},
8670@code{.elc}, @code{.el}. All of the implicit rules described below
8671whose prerequisites have one of these suffixes are actually suffix
8672rules. If you modify the suffix list, the only predefined suffix
8673rules in effect will be those named by one or two of the suffixes that
8674are on the list you specify; rules whose suffixes fail to be on the
8675list are disabled. @xref{Suffix Rules, ,Old-Fashioned Suffix Rules},
8676for full details on suffix rules.
8677
8678@table @asis
8679@item Compiling C programs
8680@cindex C, rule to compile
8681@pindex cc
8682@pindex gcc
8683@pindex .o
8684@pindex .c
8685@file{@var{n}.o} is made automatically from @file{@var{n}.c} with
8686a recipe of the form @samp{$(CC) $(CPPFLAGS) $(CFLAGS) -c}.@refill
8687
8688@item Compiling C++ programs
8689@cindex C++, rule to compile
8690@pindex g++
8691@pindex .cc
8692@pindex .cpp
8693@pindex .C
8694@file{@var{n}.o} is made automatically from @file{@var{n}.cc},
8695@file{@var{n}.cpp}, or @file{@var{n}.C} with a recipe of the form
8696@samp{$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c}. We encourage you to use the
8697suffix @samp{.cc} for C++ source files instead of @samp{.C}.@refill
8698
8699@item Compiling Pascal programs
8700@cindex Pascal, rule to compile
8701@pindex pc
8702@pindex .p
8703@file{@var{n}.o} is made automatically from @file{@var{n}.p}
8704with the recipe @samp{$(PC) $(PFLAGS) -c}.@refill
8705
8706@item Compiling Fortran and Ratfor programs
8707@cindex Fortran, rule to compile
8708@cindex Ratfor, rule to compile
8709@pindex f77
8710@pindex .f
8711@pindex .r
8712@pindex .F
8713@file{@var{n}.o} is made automatically from @file{@var{n}.r},
8714@file{@var{n}.F} or @file{@var{n}.f} by running the
8715Fortran compiler. The precise recipe used is as follows:@refill
8716
8717@table @samp
8718@item .f
8719@samp{$(FC) $(FFLAGS) -c}.
8720@item .F
8721@samp{$(FC) $(FFLAGS) $(CPPFLAGS) -c}.
8722@item .r
8723@samp{$(FC) $(FFLAGS) $(RFLAGS) -c}.
8724@end table
8725
8726@item Preprocessing Fortran and Ratfor programs
8727@file{@var{n}.f} is made automatically from @file{@var{n}.r} or
8728@file{@var{n}.F}. This rule runs just the preprocessor to convert a
8729Ratfor or preprocessable Fortran program into a strict Fortran
8730program. The precise recipe used is as follows:@refill
8731
8732@table @samp
8733@item .F
8734@samp{$(FC) $(CPPFLAGS) $(FFLAGS) -F}.
8735@item .r
8736@samp{$(FC) $(FFLAGS) $(RFLAGS) -F}.
8737@end table
8738
8739@item Compiling Modula-2 programs
8740@cindex Modula-2, rule to compile
8741@pindex m2c
8742@pindex .sym
8743@pindex .def
8744@pindex .mod
8745@file{@var{n}.sym} is made from @file{@var{n}.def} with a recipe
8746of the form @samp{$(M2C) $(M2FLAGS) $(DEFFLAGS)}. @file{@var{n}.o}
8747is made from @file{@var{n}.mod}; the form is:
8748@w{@samp{$(M2C) $(M2FLAGS) $(MODFLAGS)}}.@refill
8749
8750@need 1200
8751@item Assembling and preprocessing assembler programs
8752@cindex assembly, rule to compile
8753@pindex as
8754@pindex .s
8755@file{@var{n}.o} is made automatically from @file{@var{n}.s} by
8756running the assembler, @code{as}. The precise recipe is
8757@samp{$(AS) $(ASFLAGS)}.@refill
8758
8759@pindex .S
8760@file{@var{n}.s} is made automatically from @file{@var{n}.S} by
8761running the C preprocessor, @code{cpp}. The precise recipe is
8762@w{@samp{$(CPP) $(CPPFLAGS)}}.
8763
8764@item Linking a single object file
8765@cindex linking, predefined rule for
8766@pindex ld
8767@pindex .o
8768@file{@var{n}} is made automatically from @file{@var{n}.o} by running
8769the linker (usually called @code{ld}) via the C compiler. The precise
8770recipe used is @w{@samp{$(CC) $(LDFLAGS) @var{n}.o $(LOADLIBES) $(LDLIBS)}}.
8771
8772This rule does the right thing for a simple program with only one
8773source file. It will also do the right thing if there are multiple
8774object files (presumably coming from various other source files), one
8775of which has a name matching that of the executable file. Thus,
8776
8777@example
8778x: y.o z.o
8779@end example
8780
8781@noindent
8782when @file{x.c}, @file{y.c} and @file{z.c} all exist will execute:
8783
8784@example
8785@group
8786cc -c x.c -o x.o
8787cc -c y.c -o y.o
8788cc -c z.c -o z.o
8789cc x.o y.o z.o -o x
8790rm -f x.o
8791rm -f y.o
8792rm -f z.o
8793@end group
8794@end example
8795
8796@noindent
8797In more complicated cases, such as when there is no object file whose
8798name derives from the executable file name, you must write an explicit
8799recipe for linking.
8800
8801Each kind of file automatically made into @samp{.o} object files will
8802be automatically linked by using the compiler (@samp{$(CC)},
8803@samp{$(FC)} or @samp{$(PC)}; the C compiler @samp{$(CC)} is used to
8804assemble @samp{.s} files) without the @samp{-c} option. This could be
8805done by using the @samp{.o} object files as intermediates, but it is
8806faster to do the compiling and linking in one step, so that's how it's
8807done.@refill
8808
8809@item Yacc for C programs
8810@pindex yacc
8811@cindex Yacc, rule to run
8812@pindex .y
8813@file{@var{n}.c} is made automatically from @file{@var{n}.y} by
8814running Yacc with the recipe @samp{$(YACC) $(YFLAGS)}.
8815
8816@item Lex for C programs
8817@pindex lex
8818@cindex Lex, rule to run
8819@pindex .l
8820@file{@var{n}.c} is made automatically from @file{@var{n}.l} by
8821running Lex. The actual recipe is @samp{$(LEX) $(LFLAGS)}.
8822
8823@item Lex for Ratfor programs
8824@file{@var{n}.r} is made automatically from @file{@var{n}.l} by
8825running Lex. The actual recipe is @samp{$(LEX) $(LFLAGS)}.
8826
8827The convention of using the same suffix @samp{.l} for all Lex files
8828regardless of whether they produce C code or Ratfor code makes it
8829impossible for @code{make} to determine automatically which of the two
8830languages you are using in any particular case. If @code{make} is
8831called upon to remake an object file from a @samp{.l} file, it must
8832guess which compiler to use. It will guess the C compiler, because
8833that is more common. If you are using Ratfor, make sure @code{make}
8834knows this by mentioning @file{@var{n}.r} in the makefile. Or, if you
8835are using Ratfor exclusively, with no C files, remove @samp{.c} from
8836the list of implicit rule suffixes with:@refill
8837
8838@example
8839@group
8840.SUFFIXES:
8841.SUFFIXES: .o .r .f .l @dots{}
8842@end group
8843@end example
8844
8845@item Making Lint Libraries from C, Yacc, or Lex programs
8846@pindex lint
8847@cindex @code{lint}, rule to run
8848@pindex .ln
8849@file{@var{n}.ln} is made from @file{@var{n}.c} by running @code{lint}.
8850The precise recipe is @w{@samp{$(LINT) $(LINTFLAGS) $(CPPFLAGS) -i}}.
8851The same recipe is used on the C code produced from
8852@file{@var{n}.y} or @file{@var{n}.l}.@refill
8853
8854@item @TeX{} and Web
8855@cindex @TeX{}, rule to run
8856@cindex Web, rule to run
8857@pindex tex
8858@pindex cweave
8859@pindex weave
8860@pindex tangle
8861@pindex ctangle
8862@pindex .dvi
8863@pindex .tex
8864@pindex .web
8865@pindex .w
8866@pindex .ch
8867@file{@var{n}.dvi} is made from @file{@var{n}.tex} with the recipe
8868@samp{$(TEX)}. @file{@var{n}.tex} is made from @file{@var{n}.web} with
8869@samp{$(WEAVE)}, or from @file{@var{n}.w} (and from @file{@var{n}.ch} if
8870it exists or can be made) with @samp{$(CWEAVE)}. @file{@var{n}.p} is
8871made from @file{@var{n}.web} with @samp{$(TANGLE)} and @file{@var{n}.c}
8872is made from @file{@var{n}.w} (and from @file{@var{n}.ch} if it exists
8873or can be made) with @samp{$(CTANGLE)}.@refill
8874
8875@item Texinfo and Info
8876@cindex Texinfo, rule to format
8877@cindex Info, rule to format
8878@pindex texi2dvi
8879@pindex makeinfo
8880@pindex .texinfo
8881@pindex .info
8882@pindex .texi
8883@pindex .txinfo
8884@file{@var{n}.dvi} is made from @file{@var{n}.texinfo},
8885@file{@var{n}.texi}, or @file{@var{n}.txinfo}, with the recipe
8886@w{@samp{$(TEXI2DVI) $(TEXI2DVI_FLAGS)}}. @file{@var{n}.info} is made from
8887@file{@var{n}.texinfo}, @file{@var{n}.texi}, or @file{@var{n}.txinfo}, with
8888the recipe @w{@samp{$(MAKEINFO) $(MAKEINFO_FLAGS)}}.
8889
8890@item RCS
8891@cindex RCS, rule to extract from
8892@pindex co
8893@pindex ,v @r{(RCS file extension)}
8894Any file @file{@var{n}} is extracted if necessary from an RCS file
8895named either @file{@var{n},v} or @file{RCS/@var{n},v}. The precise
8896recipe used is @w{@samp{$(CO) $(COFLAGS)}}. @file{@var{n}} will not be
8897extracted from RCS if it already exists, even if the RCS file is
8898newer. The rules for RCS are terminal
8899(@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}),
8900so RCS files cannot be generated from another source; they must
8901actually exist.@refill
8902
8903@item SCCS
8904@cindex SCCS, rule to extract from
8905@pindex get
8906@pindex s. @r{(SCCS file prefix)}
8907Any file @file{@var{n}} is extracted if necessary from an SCCS file
8908named either @file{s.@var{n}} or @file{SCCS/s.@var{n}}. The precise
8909recipe used is @w{@samp{$(GET) $(GFLAGS)}}. The rules for SCCS are
8910terminal (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}),
8911so SCCS files cannot be generated from another source; they must
8912actually exist.@refill
8913
8914@pindex .sh
8915For the benefit of SCCS, a file @file{@var{n}} is copied from
8916@file{@var{n}.sh} and made executable (by everyone). This is for
8917shell scripts that are checked into SCCS. Since RCS preserves the
8918execution permission of a file, you do not need to use this feature
8919with RCS.@refill
8920
8921We recommend that you avoid using of SCCS. RCS is widely held to be
8922superior, and is also free. By choosing free software in place of
8923comparable (or inferior) proprietary software, you support the free
8924software movement.
8925@end table
8926
8927Usually, you want to change only the variables listed in the table
8928above, which are documented in the following section.
8929
8930However, the recipes in built-in implicit rules actually use
8931variables such as @code{COMPILE.c}, @code{LINK.p}, and
8932@code{PREPROCESS.S}, whose values contain the recipes listed above.
8933
8934@code{make} follows the convention that the rule to compile a
8935@file{.@var{x}} source file uses the variable @code{COMPILE.@var{x}}.
8936Similarly, the rule to produce an executable from a @file{.@var{x}}
8937file uses @code{LINK.@var{x}}; and the rule to preprocess a
8938@file{.@var{x}} file uses @code{PREPROCESS.@var{x}}.
8939
8940@vindex OUTPUT_OPTION
8941Every rule that produces an object file uses the variable
8942@code{OUTPUT_OPTION}. @code{make} defines this variable either to
8943contain @samp{-o $@@}, or to be empty, depending on a compile-time
8944option. You need the @samp{-o} option to ensure that the output goes
8945into the right file when the source file is in a different directory,
8946as when using @code{VPATH} (@pxref{Directory Search}). However,
8947compilers on some systems do not accept a @samp{-o} switch for object
8948files. If you use such a system, and use @code{VPATH}, some
8949compilations will put their output in the wrong place.
8950A possible workaround for this problem is to give @code{OUTPUT_OPTION}
8951the value @w{@samp{; mv $*.o $@@}}.
8952
8953@node Implicit Variables, Chained Rules, Catalogue of Rules, Implicit Rules
8954@section Variables Used by Implicit Rules
8955@cindex flags for compilers
8956
8957The recipes in built-in implicit rules make liberal use of certain
8958predefined variables. You can alter the values of these variables in
8959the makefile, with arguments to @code{make}, or in the environment to
8960alter how the implicit rules work without redefining the rules
8961themselves. You can cancel all variables used by implicit rules with
8962the @samp{-R} or @samp{--no-builtin-variables} option.
8963
8964For example, the recipe used to compile a C source file actually says
8965@samp{$(CC) -c $(CFLAGS) $(CPPFLAGS)}. The default values of the variables
8966used are @samp{cc} and nothing, resulting in the command @samp{cc -c}. By
8967redefining @samp{CC} to @samp{ncc}, you could cause @samp{ncc} to be
8968used for all C compilations performed by the implicit rule. By redefining
8969@samp{CFLAGS} to be @samp{-g}, you could pass the @samp{-g} option to
8970each compilation. @emph{All} implicit rules that do C compilation use
8971@samp{$(CC)} to get the program name for the compiler and @emph{all}
8972include @samp{$(CFLAGS)} among the arguments given to the compiler.@refill
8973
8974The variables used in implicit rules fall into two classes: those that are
8975names of programs (like @code{CC}) and those that contain arguments for the
8976programs (like @code{CFLAGS}). (The ``name of a program'' may also contain
8977some command arguments, but it must start with an actual executable program
8978name.) If a variable value contains more than one argument, separate them
8979with spaces.
8980
8981The following tables describe of some of the more commonly-used predefined
8982variables. This list is not exhaustive, and the default values shown here may
8983not be what @code{make} selects for your environment. To see the
8984complete list of predefined variables for your instance of GNU @code{make} you
8985can run @samp{make -p} in a directory with no makefiles.
8986
8987Here is a table of some of the more common variables used as names of
8988programs in built-in rules:
8989makefiles.
8990
8991@table @code
8992@item AR
8993@vindex AR
8994Archive-maintaining program; default @samp{ar}.
8995@pindex ar
8996
8997@item AS
8998@vindex AS
8999Program for compiling assembly files; default @samp{as}.
9000@pindex as
9001
9002@item CC
9003@vindex CC
9004Program for compiling C programs; default @samp{cc}.
9005@pindex cc
9006
9007@item CXX
9008@vindex CXX
9009Program for compiling C++ programs; default @samp{g++}.
9010@pindex g++
9011
9012@item CPP
9013@vindex CPP
9014Program for running the C preprocessor, with results to standard output;
9015default @samp{$(CC) -E}.
9016
9017@item FC
9018@vindex FC
9019Program for compiling or preprocessing Fortran and Ratfor programs;
9020default @samp{f77}.
9021@pindex f77
9022
9023@item M2C
9024@vindex M2C
9025Program to use to compile Modula-2 source code; default @samp{m2c}.
9026@pindex m2c
9027
9028@item PC
9029@vindex PC
9030Program for compiling Pascal programs; default @samp{pc}.
9031@pindex pc
9032
9033@item CO
9034@vindex CO
9035Program for extracting a file from RCS; default @samp{co}.
9036@pindex co
9037
9038@item GET
9039@vindex GET
9040Program for extracting a file from SCCS; default @samp{get}.
9041@pindex get
9042
9043@item LEX
9044@vindex LEX
9045Program to use to turn Lex grammars into source code; default @samp{lex}.
9046@pindex lex
9047
9048@item YACC
9049@vindex YACC
9050Program to use to turn Yacc grammars into source code; default @samp{yacc}.
9051@pindex yacc
9052
9053@item LINT
9054@vindex LINT
9055Program to use to run lint on source code; default @samp{lint}.
9056@pindex lint
9057
9058@item MAKEINFO
9059@vindex MAKEINFO
9060Program to convert a Texinfo source file into an Info file; default
9061@samp{makeinfo}.
9062@pindex makeinfo
9063
9064@item TEX
9065@vindex TEX
9066Program to make @TeX{} @sc{dvi} files from @TeX{} source;
9067default @samp{tex}.
9068@pindex tex
9069
9070@item TEXI2DVI
9071@vindex TEXI2DVI
9072Program to make @TeX{} @sc{dvi} files from Texinfo source;
9073default @samp{texi2dvi}.
9074@pindex texi2dvi
9075
9076@item WEAVE
9077@vindex WEAVE
9078Program to translate Web into @TeX{}; default @samp{weave}.
9079@pindex weave
9080
9081@item CWEAVE
9082@vindex CWEAVE
9083Program to translate C Web into @TeX{}; default @samp{cweave}.
9084@pindex cweave
9085
9086@item TANGLE
9087@vindex TANGLE
9088Program to translate Web into Pascal; default @samp{tangle}.
9089@pindex tangle
9090
9091@item CTANGLE
9092@vindex CTANGLE
9093Program to translate C Web into C; default @samp{ctangle}.
9094@pindex ctangle
9095
9096@item RM
9097@vindex RM
9098Command to remove a file; default @samp{rm -f}.
9099@pindex rm
9100@end table
9101
9102Here is a table of variables whose values are additional arguments for the
9103programs above. The default values for all of these is the empty
9104string, unless otherwise noted.
9105
9106@table @code
9107@item ARFLAGS
9108@vindex ARFLAGS
9109Flags to give the archive-maintaining program; default @samp{rv}.
9110
9111@item ASFLAGS
9112@vindex ASFLAGS
9113Extra flags to give to the assembler (when explicitly
9114invoked on a @samp{.s} or @samp{.S} file).
9115
9116@item CFLAGS
9117@vindex CFLAGS
9118Extra flags to give to the C compiler.
9119
9120@item CXXFLAGS
9121@vindex CXXFLAGS
9122Extra flags to give to the C++ compiler.
9123
9124@item COFLAGS
9125@vindex COFLAGS
9126Extra flags to give to the RCS @code{co} program.
9127
9128@item CPPFLAGS
9129@vindex CPPFLAGS
9130Extra flags to give to the C preprocessor and programs
9131that use it (the C and Fortran compilers).
9132
9133@item FFLAGS
9134@vindex FFLAGS
9135Extra flags to give to the Fortran compiler.
9136
9137@item GFLAGS
9138@vindex GFLAGS
9139Extra flags to give to the SCCS @code{get} program.
9140
9141@item LDFLAGS
9142@vindex LDFLAGS
9143Extra flags to give to compilers when they are supposed to invoke the linker,
9144@samp{ld}.
9145
9146@item LFLAGS
9147@vindex LFLAGS
9148Extra flags to give to Lex.
9149
9150@item YFLAGS
9151@vindex YFLAGS
9152Extra flags to give to Yacc.
9153
9154@item PFLAGS
9155@vindex PFLAGS
9156Extra flags to give to the Pascal compiler.
9157
9158@item RFLAGS
9159@vindex RFLAGS
9160Extra flags to give to the Fortran compiler for Ratfor programs.
9161
9162@item LINTFLAGS
9163@vindex LINTFLAGS
9164Extra flags to give to lint.
9165@end table
9166
9167@node Chained Rules, Pattern Rules, Implicit Variables, Implicit Rules
9168@section Chains of Implicit Rules
9169
9170@cindex chains of rules
9171@cindex rule, implicit, chains of
9172Sometimes a file can be made by a sequence of implicit rules. For example,
9173a file @file{@var{n}.o} could be made from @file{@var{n}.y} by running
9174first Yacc and then @code{cc}. Such a sequence is called a @dfn{chain}.
9175
9176If the file @file{@var{n}.c} exists, or is mentioned in the makefile, no
9177special searching is required: @code{make} finds that the object file can
9178be made by C compilation from @file{@var{n}.c}; later on, when considering
9179how to make @file{@var{n}.c}, the rule for running Yacc is
9180used. Ultimately both @file{@var{n}.c} and @file{@var{n}.o} are
9181updated.@refill
9182
9183@cindex intermediate files
9184@cindex files, intermediate
9185However, even if @file{@var{n}.c} does not exist and is not mentioned,
9186@code{make} knows how to envision it as the missing link between
9187@file{@var{n}.o} and @file{@var{n}.y}! In this case, @file{@var{n}.c} is
9188called an @dfn{intermediate file}. Once @code{make} has decided to use the
9189intermediate file, it is entered in the data base as if it had been
9190mentioned in the makefile, along with the implicit rule that says how to
9191create it.@refill
9192
9193Intermediate files are remade using their rules just like all other
9194files. But intermediate files are treated differently in two ways.
9195
9196The first difference is what happens if the intermediate file does not
9197exist. If an ordinary file @var{b} does not exist, and @code{make}
9198considers a target that depends on @var{b}, it invariably creates
9199@var{b} and then updates the target from @var{b}. But if @var{b} is an
9200intermediate file, then @code{make} can leave well enough alone. It
9201won't bother updating @var{b}, or the ultimate target, unless some
9202prerequisite of @var{b} is newer than that target or there is some other
9203reason to update that target.
9204
9205The second difference is that if @code{make} @emph{does} create @var{b}
9206in order to update something else, it deletes @var{b} later on after it
9207is no longer needed. Therefore, an intermediate file which did not
9208exist before @code{make} also does not exist after @code{make}.
9209@code{make} reports the deletion to you by printing a @samp{rm -f}
9210command showing which file it is deleting.
9211
9212Ordinarily, a file cannot be intermediate if it is mentioned in the
9213makefile as a target or prerequisite. However, you can explicitly mark a
9214file as intermediate by listing it as a prerequisite of the special target
9215@code{.INTERMEDIATE}. This takes effect even if the file is mentioned
9216explicitly in some other way.
9217
9218@cindex intermediate files, preserving
9219@cindex preserving intermediate files
9220@cindex secondary files
9221You can prevent automatic deletion of an intermediate file by marking it
9222as a @dfn{secondary} file. To do this, list it as a prerequisite of the
9223special target @code{.SECONDARY}. When a file is secondary, @code{make}
9224will not create the file merely because it does not already exist, but
9225@code{make} does not automatically delete the file. Marking a file as
9226secondary also marks it as intermediate.
9227
9228You can list the target pattern of an implicit rule (such as @samp{%.o})
9229as a prerequisite of the special target @code{.PRECIOUS} to preserve
9230intermediate files made by implicit rules whose target patterns match
9231that file's name; see @ref{Interrupts}.@refill
9232@cindex preserving with @code{.PRECIOUS}
9233@cindex @code{.PRECIOUS} intermediate files
9234
9235A chain can involve more than two implicit rules. For example, it is
9236possible to make a file @file{foo} from @file{RCS/foo.y,v} by running RCS,
9237Yacc and @code{cc}. Then both @file{foo.y} and @file{foo.c} are
9238intermediate files that are deleted at the end.@refill
9239
9240No single implicit rule can appear more than once in a chain. This means
9241that @code{make} will not even consider such a ridiculous thing as making
9242@file{foo} from @file{foo.o.o} by running the linker twice. This
9243constraint has the added benefit of preventing any infinite loop in the
9244search for an implicit rule chain.
9245
9246There are some special implicit rules to optimize certain cases that would
9247otherwise be handled by rule chains. For example, making @file{foo} from
9248@file{foo.c} could be handled by compiling and linking with separate
9249chained rules, using @file{foo.o} as an intermediate file. But what
9250actually happens is that a special rule for this case does the compilation
9251and linking with a single @code{cc} command. The optimized rule is used in
9252preference to the step-by-step chain because it comes earlier in the
9253ordering of rules.
9254
9255@node Pattern Rules, Last Resort, Chained Rules, Implicit Rules
9256@section Defining and Redefining Pattern Rules
9257
9258You define an implicit rule by writing a @dfn{pattern rule}. A pattern
9259rule looks like an ordinary rule, except that its target contains the
9260character @samp{%} (exactly one of them). The target is considered a
9261pattern for matching file names; the @samp{%} can match any nonempty
9262substring, while other characters match only themselves. The prerequisites
9263likewise use @samp{%} to show how their names relate to the target name.
9264
9265Thus, a pattern rule @samp{%.o : %.c} says how to make any file
9266@file{@var{stem}.o} from another file @file{@var{stem}.c}.@refill
9267
9268Note that expansion using @samp{%} in pattern rules occurs
9269@strong{after} any variable or function expansions, which take place
9270when the makefile is read. @xref{Using Variables, , How to Use
9271Variables}, and @ref{Functions, ,Functions for Transforming Text}.
9272
9273@menu
9274* Pattern Intro:: An introduction to pattern rules.
9275* Pattern Examples:: Examples of pattern rules.
9276* Automatic Variables:: How to use automatic variables in the
9277 recipes of implicit rules.
9278* Pattern Match:: How patterns match.
9279* Match-Anything Rules:: Precautions you should take prior to
9280 defining rules that can match any
9281 target file whatever.
9282* Canceling Rules:: How to override or cancel built-in rules.
9283@end menu
9284
9285@node Pattern Intro, Pattern Examples, Pattern Rules, Pattern Rules
9286@subsection Introduction to Pattern Rules
9287@cindex pattern rule
9288@cindex rule, pattern
9289
9290A pattern rule contains the character @samp{%} (exactly one of them)
9291in the target; otherwise, it looks exactly like an ordinary rule. The
9292target is a pattern for matching file names; the @samp{%} matches any
9293nonempty substring, while other characters match only themselves.
9294@cindex target pattern, implicit
9295@cindex @code{%}, in pattern rules
9296
9297For example, @samp{%.c} as a pattern matches any file name that ends in
9298@samp{.c}. @samp{s.%.c} as a pattern matches any file name that starts
9299with @samp{s.}, ends in @samp{.c} and is at least five characters long.
9300(There must be at least one character to match the @samp{%}.) The substring
9301that the @samp{%} matches is called the @dfn{stem}.@refill
9302
9303@samp{%} in a prerequisite of a pattern rule stands for the same stem
9304that was matched by the @samp{%} in the target. In order for the
9305pattern rule to apply, its target pattern must match the file name
9306under consideration and all of its prerequisites (after pattern
9307substitution) must name files that exist or can be made. These files
9308become prerequisites of the target.
9309@cindex prerequisite pattern, implicit
9310
9311Thus, a rule of the form
9312
9313@example
9314%.o : %.c ; @var{recipe}@dots{}
9315@end example
9316
9317@noindent
9318specifies how to make a file @file{@var{n}.o}, with another file
9319@file{@var{n}.c} as its prerequisite, provided that @file{@var{n}.c}
9320exists or can be made.
9321
9322There may also be prerequisites that do not use @samp{%}; such a prerequisite
9323attaches to every file made by this pattern rule. These unvarying
9324prerequisites are useful occasionally.
9325
9326A pattern rule need not have any prerequisites that contain @samp{%}, or
9327in fact any prerequisites at all. Such a rule is effectively a general
9328wildcard. It provides a way to make any file that matches the target
9329pattern. @xref{Last Resort}.
9330
9331More than one pattern rule may match a target. In this case
9332@code{make} will choose the ``best fit'' rule. @xref{Pattern Match,
9333,How Patterns Match}.
9334
9335@c !!! The end of of this paragraph should be rewritten. --bob
9336Pattern rules may have more than one target. Unlike normal rules,
9337this does not act as many different rules with the same prerequisites
9338and recipe. If a pattern rule has multiple targets, @code{make} knows
9339that the rule's recipe is responsible for making all of the targets.
9340The recipe is executed only once to make all the targets. When
9341searching for a pattern rule to match a target, the target patterns of
9342a rule other than the one that matches the target in need of a rule
9343are incidental: @code{make} worries only about giving a recipe and
9344prerequisites to the file presently in question. However, when this
9345file's recipe is run, the other targets are marked as having been
9346updated themselves.
9347@cindex multiple targets, in pattern rule
9348@cindex target, multiple in pattern rule
9349
9350@node Pattern Examples, Automatic Variables, Pattern Intro, Pattern Rules
9351@subsection Pattern Rule Examples
9352
9353Here are some examples of pattern rules actually predefined in
9354@code{make}. First, the rule that compiles @samp{.c} files into @samp{.o}
9355files:@refill
9356
9357@example
9358%.o : %.c
9359 $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@@
9360@end example
9361
9362@noindent
9363defines a rule that can make any file @file{@var{x}.o} from
9364@file{@var{x}.c}. The recipe uses the automatic variables @samp{$@@} and
9365@samp{$<} to substitute the names of the target file and the source file
9366in each case where the rule applies (@pxref{Automatic Variables}).@refill
9367
9368Here is a second built-in rule:
9369
9370@example
9371% :: RCS/%,v
9372 $(CO) $(COFLAGS) $<
9373@end example
9374
9375@noindent
9376defines a rule that can make any file @file{@var{x}} whatsoever from a
9377corresponding file @file{@var{x},v} in the subdirectory @file{RCS}. Since
9378the target is @samp{%}, this rule will apply to any file whatever, provided
9379the appropriate prerequisite file exists. The double colon makes the rule
9380@dfn{terminal}, which means that its prerequisite may not be an intermediate
9381file (@pxref{Match-Anything Rules, ,Match-Anything Pattern Rules}).@refill
9382
9383@need 500
9384This pattern rule has two targets:
9385
9386@example
9387@group
9388%.tab.c %.tab.h: %.y
9389 bison -d $<
9390@end group
9391@end example
9392
9393@noindent
9394@c The following paragraph is rewritten to avoid overfull hboxes
9395This tells @code{make} that the recipe @samp{bison -d @var{x}.y} will
9396make both @file{@var{x}.tab.c} and @file{@var{x}.tab.h}. If the file
9397@file{foo} depends on the files @file{parse.tab.o} and @file{scan.o}
9398and the file @file{scan.o} depends on the file @file{parse.tab.h},
9399when @file{parse.y} is changed, the recipe @samp{bison -d parse.y}
9400will be executed only once, and the prerequisites of both
9401@file{parse.tab.o} and @file{scan.o} will be satisfied. (Presumably
9402the file @file{parse.tab.o} will be recompiled from @file{parse.tab.c}
9403and the file @file{scan.o} from @file{scan.c}, while @file{foo} is
9404linked from @file{parse.tab.o}, @file{scan.o}, and its other
9405prerequisites, and it will execute happily ever after.)@refill
9406
9407@node Automatic Variables, Pattern Match, Pattern Examples, Pattern Rules
9408@subsection Automatic Variables
9409@cindex automatic variables
9410@cindex variables, automatic
9411@cindex variables, and implicit rule
9412
9413Suppose you are writing a pattern rule to compile a @samp{.c} file into a
9414@samp{.o} file: how do you write the @samp{cc} command so that it operates
9415on the right source file name? You cannot write the name in the recipe,
9416because the name is different each time the implicit rule is applied.
9417
9418What you do is use a special feature of @code{make}, the @dfn{automatic
9419variables}. These variables have values computed afresh for each rule that
9420is executed, based on the target and prerequisites of the rule. In this
9421example, you would use @samp{$@@} for the object file name and @samp{$<}
9422for the source file name.
9423
9424@cindex automatic variables in prerequisites
9425@cindex prerequisites, and automatic variables
9426It's very important that you recognize the limited scope in which
9427automatic variable values are available: they only have values within
9428the recipe. In particular, you cannot use them anywhere
9429within the target list of a rule; they have no value there and will
9430expand to the empty string. Also, they cannot be accessed directly
9431within the prerequisite list of a rule. A common mistake is
9432attempting to use @code{$@@} within the prerequisites list; this will
9433not work. However, there is a special feature of GNU @code{make},
9434secondary expansion (@pxref{Secondary Expansion}), which will allow
9435automatic variable values to be used in prerequisite lists.
9436
9437Here is a table of automatic variables:
9438
9439@table @code
9440@vindex $@@
9441@vindex @@ @r{(automatic variable)}
9442@item $@@
9443The file name of the target of the rule. If the target is an archive
9444member, then @samp{$@@} is the name of the archive file. In a pattern
9445rule that has multiple targets (@pxref{Pattern Intro, ,Introduction to
9446Pattern Rules}), @samp{$@@} is the name of whichever target caused the
9447rule's recipe to be run.
9448
9449@vindex $%
9450@vindex % @r{(automatic variable)}
9451@item $%
9452The target member name, when the target is an archive member.
9453@xref{Archives}. For example, if the target is @file{foo.a(bar.o)} then
9454@samp{$%} is @file{bar.o} and @samp{$@@} is @file{foo.a}. @samp{$%} is
9455empty when the target is not an archive member.
9456
9457@vindex $<
9458@vindex < @r{(automatic variable)}
9459@item $<
9460The name of the first prerequisite. If the target got its recipe from
9461an implicit rule, this will be the first prerequisite added by the
9462implicit rule (@pxref{Implicit Rules}).
9463
9464@vindex $?
9465@vindex ? @r{(automatic variable)}
9466@item $?
9467The names of all the prerequisites that are newer than the target, with
9468spaces between them. For prerequisites which are archive members, only
9469the named member is used (@pxref{Archives}).
9470@cindex prerequisites, list of changed
9471@cindex list of changed prerequisites
9472
9473@vindex $^
9474@vindex ^ @r{(automatic variable)}
9475@item $^
9476The names of all the prerequisites, with spaces between them. For
9477prerequisites which are archive members, only the named member is used
9478(@pxref{Archives}). A target has only one prerequisite on each other file
9479it depends on, no matter how many times each file is listed as a
9480prerequisite. So if you list a prerequisite more than once for a target,
9481the value of @code{$^} contains just one copy of the name. This list
9482does @strong{not} contain any of the order-only prerequisites; for those
9483see the @samp{$|} variable, below.
9484@cindex prerequisites, list of all
9485@cindex list of all prerequisites
9486
9487@vindex $+
9488@vindex + @r{(automatic variable)}
9489@item $+
9490This is like @samp{$^}, but prerequisites listed more than once are
9491duplicated in the order they were listed in the makefile. This is
9492primarily useful for use in linking commands where it is meaningful to
9493repeat library file names in a particular order.
9494
9495@vindex $|
9496@vindex | @r{(automatic variable)}
9497@item $|
9498The names of all the order-only prerequisites, with spaces between
9499them.
9500
9501@vindex $*
9502@vindex * @r{(automatic variable)}
9503@item $*
9504The stem with which an implicit rule matches (@pxref{Pattern Match, ,How
9505Patterns Match}). If the target is @file{dir/a.foo.b} and the target
9506pattern is @file{a.%.b} then the stem is @file{dir/foo}. The stem is
9507useful for constructing names of related files.@refill
9508@cindex stem, variable for
9509
9510In a static pattern rule, the stem is part of the file name that matched
9511the @samp{%} in the target pattern.
9512
9513In an explicit rule, there is no stem; so @samp{$*} cannot be determined
9514in that way. Instead, if the target name ends with a recognized suffix
9515(@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}), @samp{$*} is set to
9516the target name minus the suffix. For example, if the target name is
9517@samp{foo.c}, then @samp{$*} is set to @samp{foo}, since @samp{.c} is a
9518suffix. GNU @code{make} does this bizarre thing only for compatibility
9519with other implementations of @code{make}. You should generally avoid
9520using @samp{$*} except in implicit rules or static pattern rules.@refill
9521
9522If the target name in an explicit rule does not end with a recognized
9523suffix, @samp{$*} is set to the empty string for that rule.
9524@end table
9525
9526@samp{$?} is useful even in explicit rules when you wish to operate on only
9527the prerequisites that have changed. For example, suppose that an archive
9528named @file{lib} is supposed to contain copies of several object files.
9529This rule copies just the changed object files into the archive:
9530
9531@example
9532@group
9533lib: foo.o bar.o lose.o win.o
9534 ar r lib $?
9535@end group
9536@end example
9537
9538Of the variables listed above, four have values that are single file
9539names, and three have values that are lists of file names. These seven
9540have variants that get just the file's directory name or just the file
9541name within the directory. The variant variables' names are formed by
9542appending @samp{D} or @samp{F}, respectively. These variants are
9543semi-obsolete in GNU @code{make} since the functions @code{dir} and
9544@code{notdir} can be used to get a similar effect (@pxref{File Name
9545Functions, , Functions for File Names}). Note, however, that the
9546@samp{D} variants all omit the trailing slash which always appears in
9547the output of the @code{dir} function. Here is a table of the variants:
9548
9549@table @samp
9550@vindex $(@@D)
9551@vindex @@D @r{(automatic variable)}
9552@item $(@@D)
9553The directory part of the file name of the target, with the trailing
9554slash removed. If the value of @samp{$@@} is @file{dir/foo.o} then
9555@samp{$(@@D)} is @file{dir}. This value is @file{.} if @samp{$@@} does
9556not contain a slash.
9557
9558@vindex $(@@F)
9559@vindex @@F @r{(automatic variable)}
9560@item $(@@F)
9561The file-within-directory part of the file name of the target. If the
9562value of @samp{$@@} is @file{dir/foo.o} then @samp{$(@@F)} is
9563@file{foo.o}. @samp{$(@@F)} is equivalent to @samp{$(notdir $@@)}.
9564
9565@vindex $(*D)
9566@vindex *D @r{(automatic variable)}
9567@item $(*D)
9568@vindex $(*F)
9569@vindex *F @r{(automatic variable)}
9570@itemx $(*F)
9571The directory part and the file-within-directory
9572part of the stem; @file{dir} and @file{foo} in this example.
9573
9574@vindex $(%D)
9575@vindex %D @r{(automatic variable)}
9576@item $(%D)
9577@vindex $(%F)
9578@vindex %F @r{(automatic variable)}
9579@itemx $(%F)
9580The directory part and the file-within-directory part of the target
9581archive member name. This makes sense only for archive member targets
9582of the form @file{@var{archive}(@var{member})} and is useful only when
9583@var{member} may contain a directory name. (@xref{Archive Members,
9584,Archive Members as Targets}.)
9585
9586@vindex $(<D)
9587@vindex <D @r{(automatic variable)}
9588@item $(<D)
9589@vindex $(<F)
9590@vindex <F @r{(automatic variable)}
9591@itemx $(<F)
9592The directory part and the file-within-directory
9593part of the first prerequisite.
9594
9595@vindex $(^D)
9596@vindex ^D @r{(automatic variable)}
9597@item $(^D)
9598@vindex $(^F)
9599@vindex ^F @r{(automatic variable)}
9600@itemx $(^F)
9601Lists of the directory parts and the file-within-directory
9602parts of all prerequisites.
9603
9604@vindex $(+D)
9605@vindex +D @r{(automatic variable)}
9606@item $(+D)
9607@vindex $(+F)
9608@vindex +F @r{(automatic variable)}
9609@itemx $(+F)
9610Lists of the directory parts and the file-within-directory
9611parts of all prerequisites, including multiple instances of duplicated
9612prerequisites.
9613
9614@vindex $(?D)
9615@vindex ?D @r{(automatic variable)}
9616@item $(?D)
9617@vindex $(?F)
9618@vindex ?F @r{(automatic variable)}
9619@itemx $(?F)
9620Lists of the directory parts and the file-within-directory parts of
9621all prerequisites that are newer than the target.
9622@end table
9623
9624Note that we use a special stylistic convention when we talk about these
9625automatic variables; we write ``the value of @samp{$<}'', rather than
9626@w{``the variable @code{<}''} as we would write for ordinary variables
9627such as @code{objects} and @code{CFLAGS}. We think this convention
9628looks more natural in this special case. Please do not assume it has a
9629deep significance; @samp{$<} refers to the variable named @code{<} just
9630as @samp{$(CFLAGS)} refers to the variable named @code{CFLAGS}.
9631You could just as well use @samp{$(<)} in place of @samp{$<}.
9632
9633@node Pattern Match, Match-Anything Rules, Automatic Variables, Pattern Rules
9634@subsection How Patterns Match
9635
9636@cindex stem
9637A target pattern is composed of a @samp{%} between a prefix and a suffix,
9638either or both of which may be empty. The pattern matches a file name only
9639if the file name starts with the prefix and ends with the suffix, without
9640overlap. The text between the prefix and the suffix is called the
9641@dfn{stem}. Thus, when the pattern @samp{%.o} matches the file name
9642@file{test.o}, the stem is @samp{test}. The pattern rule prerequisites are
9643turned into actual file names by substituting the stem for the character
9644@samp{%}. Thus, if in the same example one of the prerequisites is written
9645as @samp{%.c}, it expands to @samp{test.c}.@refill
9646
9647When the target pattern does not contain a slash (and it usually does
9648not), directory names in the file names are removed from the file name
9649before it is compared with the target prefix and suffix. After the
9650comparison of the file name to the target pattern, the directory
9651names, along with the slash that ends them, are added on to the
9652prerequisite file names generated from the pattern rule's prerequisite
9653patterns and the file name. The directories are ignored only for the
9654purpose of finding an implicit rule to use, not in the application of
9655that rule. Thus, @samp{e%t} matches the file name @file{src/eat},
9656with @samp{src/a} as the stem. When prerequisites are turned into file
9657names, the directories from the stem are added at the front, while the
9658rest of the stem is substituted for the @samp{%}. The stem
9659@samp{src/a} with a prerequisite pattern @samp{c%r} gives the file name
9660@file{src/car}.@refill
9661
9662@cindex pattern rules, order of
9663@cindex order of pattern rules
9664A pattern rule can be used to build a given file only if there is a
9665target pattern that matches the file name, @emph{and} all
9666prerequisites in that rule either exist or can be built. The rules
9667you write take precedence over those that are built in. Note however,
9668that a rule whose prerequisites actually exist or are mentioned always
9669takes priority over a rule with prerequisites that must be made by
9670chaining other implicit rules.
9671
9672@cindex stem, shortest
9673It is possible that more than one pattern rule will meet these
9674criteria. In that case, @code{make} will choose the rule with the
9675shortest stem (that is, the pattern that matches most specifically).
9676If more than one pattern rule has the shortest stem, @code{make} will
9677choose the first one found in the makefile.
9678
9679This algorithm results in more specific rules being preferred over
9680more generic ones; for example:
9681
9682@example
9683%.o: %.c
9684 $(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@@
9685
9686%.o : %.f
9687 $(COMPILE.F) $(OUTPUT_OPTION) $<
9688
9689lib/%.o: lib/%.c
9690 $(CC) -fPIC -c $(CFLAGS) $(CPPFLAGS) $< -o $@@
9691@end example
9692
9693Given these rules and asked to build @file{bar.o} where both
9694@file{bar.c} and @file{bar.f} exist, @code{make} will choose the first
9695rule and compile @file{bar.c} into @file{bar.o}. In the same
9696situation where @file{bar.c} does not exist, then @code{make} will
9697choose the second rule and compile @file{bar.f} into @file{bar.o}.
9698
9699If @code{make} is asked to build @file{lib/bar.o} and both
9700@file{lib/bar.c} and @file{lib/bar.f} exist, then the third rule will
9701be chosen since the stem for this rule (@samp{bar}) is shorter than
9702the stem for the first rule (@samp{lib/bar}). If @file{lib/bar.c}
9703does not exist then the third rule is not eligible and the second rule
9704will be used, even though the stem is longer.
9705
9706@node Match-Anything Rules, Canceling Rules, Pattern Match, Pattern Rules
9707@subsection Match-Anything Pattern Rules
9708
9709@cindex match-anything rule
9710@cindex terminal rule
9711When a pattern rule's target is just @samp{%}, it matches any file name
9712whatever. We call these rules @dfn{match-anything} rules. They are very
9713useful, but it can take a lot of time for @code{make} to think about them,
9714because it must consider every such rule for each file name listed either
9715as a target or as a prerequisite.
9716
9717Suppose the makefile mentions @file{foo.c}. For this target, @code{make}
9718would have to consider making it by linking an object file @file{foo.c.o},
9719or by C compilation-and-linking in one step from @file{foo.c.c}, or by
9720Pascal compilation-and-linking from @file{foo.c.p}, and many other
9721possibilities.
9722
9723We know these possibilities are ridiculous since @file{foo.c} is a C source
9724file, not an executable. If @code{make} did consider these possibilities,
9725it would ultimately reject them, because files such as @file{foo.c.o} and
9726@file{foo.c.p} would not exist. But these possibilities are so
9727numerous that @code{make} would run very slowly if it had to consider
9728them.@refill
9729
9730To gain speed, we have put various constraints on the way @code{make}
9731considers match-anything rules. There are two different constraints that
9732can be applied, and each time you define a match-anything rule you must
9733choose one or the other for that rule.
9734
9735One choice is to mark the match-anything rule as @dfn{terminal} by defining
9736it with a double colon. When a rule is terminal, it does not apply unless
9737its prerequisites actually exist. Prerequisites that could be made with
9738other implicit rules are not good enough. In other words, no further
9739chaining is allowed beyond a terminal rule.
9740
9741For example, the built-in implicit rules for extracting sources from RCS
9742and SCCS files are terminal; as a result, if the file @file{foo.c,v} does
9743not exist, @code{make} will not even consider trying to make it as an
9744intermediate file from @file{foo.c,v.o} or from @file{RCS/SCCS/s.foo.c,v}.
9745RCS and SCCS files are generally ultimate source files, which should not be
9746remade from any other files; therefore, @code{make} can save time by not
9747looking for ways to remake them.@refill
9748
9749If you do not mark the match-anything rule as terminal, then it is
9750nonterminal. A nonterminal match-anything rule cannot apply to a file name
9751that indicates a specific type of data. A file name indicates a specific
9752type of data if some non-match-anything implicit rule target matches it.
9753
9754For example, the file name @file{foo.c} matches the target for the pattern
9755rule @samp{%.c : %.y} (the rule to run Yacc). Regardless of whether this
9756rule is actually applicable (which happens only if there is a file
9757@file{foo.y}), the fact that its target matches is enough to prevent
9758consideration of any nonterminal match-anything rules for the file
9759@file{foo.c}. Thus, @code{make} will not even consider trying to make
9760@file{foo.c} as an executable file from @file{foo.c.o}, @file{foo.c.c},
9761@file{foo.c.p}, etc.@refill
9762
9763The motivation for this constraint is that nonterminal match-anything
9764rules are used for making files containing specific types of data (such as
9765executable files) and a file name with a recognized suffix indicates some
9766other specific type of data (such as a C source file).
9767
9768Special built-in dummy pattern rules are provided solely to recognize
9769certain file names so that nonterminal match-anything rules will not be
9770considered. These dummy rules have no prerequisites and no recipes, and
9771they are ignored for all other purposes. For example, the built-in
9772implicit rule
9773
9774@example
9775%.p :
9776@end example
9777
9778@noindent
9779exists to make sure that Pascal source files such as @file{foo.p} match a
9780specific target pattern and thereby prevent time from being wasted looking
9781for @file{foo.p.o} or @file{foo.p.c}.
9782
9783Dummy pattern rules such as the one for @samp{%.p} are made for every
9784suffix listed as valid for use in suffix rules (@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}).
9785
9786@node Canceling Rules, , Match-Anything Rules, Pattern Rules
9787@subsection Canceling Implicit Rules
9788
9789You can override a built-in implicit rule (or one you have defined
9790yourself) by defining a new pattern rule with the same target and
9791prerequisites, but a different recipe. When the new rule is defined, the
9792built-in one is replaced. The new rule's position in the sequence of
9793implicit rules is determined by where you write the new rule.
9794
9795You can cancel a built-in implicit rule by defining a pattern rule with the
9796same target and prerequisites, but no recipe. For example, the following
9797would cancel the rule that runs the assembler:
9798
9799@example
9800%.o : %.s
9801@end example
9802
9803@node Last Resort, Suffix Rules, Pattern Rules, Implicit Rules
9804@section Defining Last-Resort Default Rules
9805@cindex last-resort default rules
9806@cindex default rules, last-resort
9807
9808You can define a last-resort implicit rule by writing a terminal
9809match-anything pattern rule with no prerequisites (@pxref{Match-Anything
9810Rules}). This is just like any other pattern rule; the only thing
9811special about it is that it will match any target. So such a rule's
9812recipe is used for all targets and prerequisites that have no recipe
9813of their own and for which no other implicit rule applies.
9814
9815For example, when testing a makefile, you might not care if the source
9816files contain real data, only that they exist. Then you might do this:
9817
9818@example
9819%::
9820 touch $@@
9821@end example
9822
9823@noindent
9824to cause all the source files needed (as prerequisites) to be created
9825automatically.
9826
9827@findex .DEFAULT
9828You can instead define a recipe to be used for targets for which there
9829are no rules at all, even ones which don't specify recipes. You do
9830this by writing a rule for the target @code{.DEFAULT}. Such a rule's
9831recipe is used for all prerequisites which do not appear as targets in
9832any explicit rule, and for which no implicit rule applies. Naturally,
9833there is no @code{.DEFAULT} rule unless you write one.
9834
9835If you use @code{.DEFAULT} with no recipe or prerequisites:
9836
9837@example
9838.DEFAULT:
9839@end example
9840
9841@noindent
9842the recipe previously stored for @code{.DEFAULT} is cleared. Then
9843@code{make} acts as if you had never defined @code{.DEFAULT} at all.
9844
9845If you do not want a target to get the recipe from a match-anything
9846pattern rule or @code{.DEFAULT}, but you also do not want any recipe
9847to be run for the target, you can give it an empty recipe
9848(@pxref{Empty Recipes, ,Defining Empty Recipes}).@refill
9849
9850You can use a last-resort rule to override part of another makefile.
9851@xref{Overriding Makefiles, , Overriding Part of Another Makefile}.
9852
9853@node Suffix Rules, Implicit Rule Search, Last Resort, Implicit Rules
9854@section Old-Fashioned Suffix Rules
9855@cindex old-fashioned suffix rules
9856@cindex suffix rule
9857
9858@dfn{Suffix rules} are the old-fashioned way of defining implicit rules for
9859@code{make}. Suffix rules are obsolete because pattern rules are more
9860general and clearer. They are supported in GNU @code{make} for
9861compatibility with old makefiles. They come in two kinds:
9862@dfn{double-suffix} and @dfn{single-suffix}.@refill
9863
9864A double-suffix rule is defined by a pair of suffixes: the target suffix
9865and the source suffix. It matches any file whose name ends with the
9866target suffix. The corresponding implicit prerequisite is made by
9867replacing the target suffix with the source suffix in the file name. A
9868two-suffix rule whose target and source suffixes are @samp{.o} and
9869@samp{.c} is equivalent to the pattern rule @samp{%.o : %.c}.
9870
9871A single-suffix rule is defined by a single suffix, which is the source
9872suffix. It matches any file name, and the corresponding implicit
9873prerequisite name is made by appending the source suffix. A single-suffix
9874rule whose source suffix is @samp{.c} is equivalent to the pattern rule
9875@samp{% : %.c}.
9876
9877Suffix rule definitions are recognized by comparing each rule's target
9878against a defined list of known suffixes. When @code{make} sees a rule
9879whose target is a known suffix, this rule is considered a single-suffix
9880rule. When @code{make} sees a rule whose target is two known suffixes
9881concatenated, this rule is taken as a double-suffix rule.
9882
9883For example, @samp{.c} and @samp{.o} are both on the default list of
9884known suffixes. Therefore, if you define a rule whose target is
9885@samp{.c.o}, @code{make} takes it to be a double-suffix rule with source
9886suffix @samp{.c} and target suffix @samp{.o}. Here is the old-fashioned
9887way to define the rule for compiling a C source file:@refill
9888
9889@example
9890.c.o:
9891 $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
9892@end example
9893
9894Suffix rules cannot have any prerequisites of their own. If they have any,
9895they are treated as normal files with funny names, not as suffix rules.
9896Thus, the rule:
9897
9898@example
9899.c.o: foo.h
9900 $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
9901@end example
9902
9903@noindent
9904tells how to make the file @file{.c.o} from the prerequisite file
9905@file{foo.h}, and is not at all like the pattern rule:
9906
9907@example
9908%.o: %.c foo.h
9909 $(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@@ $<
9910@end example
9911
9912@noindent
9913which tells how to make @samp{.o} files from @samp{.c} files, and makes all
9914@samp{.o} files using this pattern rule also depend on @file{foo.h}.
9915
9916Suffix rules with no recipe are also meaningless. They do not remove
9917previous rules as do pattern rules with no recipe (@pxref{Canceling
9918Rules, , Canceling Implicit Rules}). They simply enter the suffix or
9919pair of suffixes concatenated as a target in the data base.@refill
9920
9921@findex .SUFFIXES
9922The known suffixes are simply the names of the prerequisites of the special
9923target @code{.SUFFIXES}. You can add your own suffixes by writing a rule
9924for @code{.SUFFIXES} that adds more prerequisites, as in:
9925
9926@example
9927.SUFFIXES: .hack .win
9928@end example
9929
9930@noindent
9931which adds @samp{.hack} and @samp{.win} to the end of the list of suffixes.
9932
9933If you wish to eliminate the default known suffixes instead of just adding
9934to them, write a rule for @code{.SUFFIXES} with no prerequisites. By
9935special dispensation, this eliminates all existing prerequisites of
9936@code{.SUFFIXES}. You can then write another rule to add the suffixes you
9937want. For example,
9938
9939@example
9940@group
9941.SUFFIXES: # @r{Delete the default suffixes}
9942.SUFFIXES: .c .o .h # @r{Define our suffix list}
9943@end group
9944@end example
9945
9946The @samp{-r} or @samp{--no-builtin-rules} flag causes the default
9947list of suffixes to be empty.
9948
9949@vindex SUFFIXES
9950The variable @code{SUFFIXES} is defined to the default list of suffixes
9951before @code{make} reads any makefiles. You can change the list of suffixes
9952with a rule for the special target @code{.SUFFIXES}, but that does not alter
9953this variable.
9954
9955@node Implicit Rule Search, , Suffix Rules, Implicit Rules
9956@section Implicit Rule Search Algorithm
9957@cindex implicit rule, search algorithm
9958@cindex search algorithm, implicit rule
9959
9960Here is the procedure @code{make} uses for searching for an implicit rule
9961for a target @var{t}. This procedure is followed for each double-colon
9962rule with no recipe, for each target of ordinary rules none of which have
9963a recipe, and for each prerequisite that is not the target of any rule. It
9964is also followed recursively for prerequisites that come from implicit
9965rules, in the search for a chain of rules.
9966
9967Suffix rules are not mentioned in this algorithm because suffix rules are
9968converted to equivalent pattern rules once the makefiles have been read in.
9969
9970For an archive member target of the form
9971@samp{@var{archive}(@var{member})}, the following algorithm is run
9972twice, first using the entire target name @var{t}, and second using
9973@samp{(@var{member})} as the target @var{t} if the first run found no
9974rule.@refill
9975
9976@enumerate
9977@item
9978Split @var{t} into a directory part, called @var{d}, and the rest,
9979called @var{n}. For example, if @var{t} is @samp{src/foo.o}, then
9980@var{d} is @samp{src/} and @var{n} is @samp{foo.o}.@refill
9981
9982@item
9983Make a list of all the pattern rules one of whose targets matches
9984@var{t} or @var{n}. If the target pattern contains a slash, it is
9985matched against @var{t}; otherwise, against @var{n}.
9986
9987@item
9988If any rule in that list is @emph{not} a match-anything rule, then
9989remove all nonterminal match-anything rules from the list.
9990
9991@item
9992Remove from the list all rules with no recipe.
9993
9994@item
9995For each pattern rule in the list:
9996
9997@enumerate a
9998@item
9999Find the stem @var{s}, which is the nonempty part of @var{t} or @var{n}
10000matched by the @samp{%} in the target pattern.@refill
10001
10002@item
10003Compute the prerequisite names by substituting @var{s} for @samp{%}; if
10004the target pattern does not contain a slash, append @var{d} to
10005the front of each prerequisite name.@refill
10006
10007@item
10008Test whether all the prerequisites exist or ought to exist. (If a
10009file name is mentioned in the makefile as a target or as an explicit
10010prerequisite, then we say it ought to exist.)
10011
10012If all prerequisites exist or ought to exist, or there are no prerequisites,
10013then this rule applies.
10014@end enumerate
10015
10016@item
10017If no pattern rule has been found so far, try harder.
10018For each pattern rule in the list:
10019
10020@enumerate a
10021@item
10022If the rule is terminal, ignore it and go on to the next rule.
10023
10024@item
10025Compute the prerequisite names as before.
10026
10027@item
10028Test whether all the prerequisites exist or ought to exist.
10029
10030@item
10031For each prerequisite that does not exist, follow this algorithm
10032recursively to see if the prerequisite can be made by an implicit
10033rule.
10034
10035@item
10036If all prerequisites exist, ought to exist, or can be
10037made by implicit rules, then this rule applies.
10038@end enumerate
10039
10040@item
10041If no implicit rule applies, the rule for @code{.DEFAULT}, if any,
10042applies. In that case, give @var{t} the same recipe that
10043@code{.DEFAULT} has. Otherwise, there is no recipe for @var{t}.
10044@end enumerate
10045
10046Once a rule that applies has been found, for each target pattern of
10047the rule other than the one that matched @var{t} or @var{n}, the
10048@samp{%} in the pattern is replaced with @var{s} and the resultant
10049file name is stored until the recipe to remake the target file @var{t}
10050is executed. After the recipe is executed, each of these stored file
10051names are entered into the data base and marked as having been updated
10052and having the same update status as the file @var{t}.
10053
10054When the recipe of a pattern rule is executed for @var{t}, the
10055automatic variables are set corresponding to the target and
10056prerequisites. @xref{Automatic Variables}.
10057
10058@node Archives, Features, Implicit Rules, Top
10059@chapter Using @code{make} to Update Archive Files
10060@cindex archive
10061
10062@dfn{Archive files} are files containing named subfiles called
10063@dfn{members}; they are maintained with the program @code{ar} and their
10064main use is as subroutine libraries for linking.
10065
10066@menu
10067* Archive Members:: Archive members as targets.
10068* Archive Update:: The implicit rule for archive member targets.
10069* Archive Pitfalls:: Dangers to watch out for when using archives.
10070* Archive Suffix Rules:: You can write a special kind of suffix rule
10071 for updating archives.
10072@end menu
10073
10074@node Archive Members, Archive Update, Archives, Archives
10075@section Archive Members as Targets
10076@cindex archive member targets
10077
10078An individual member of an archive file can be used as a target or
10079prerequisite in @code{make}. You specify the member named @var{member} in
10080archive file @var{archive} as follows:
10081
10082@example
10083@var{archive}(@var{member})
10084@end example
10085
10086@noindent
10087This construct is available only in targets and prerequisites, not in
10088recipes! Most programs that you might use in recipes do not support
10089this syntax and cannot act directly on archive members. Only
10090@code{ar} and other programs specifically designed to operate on
10091archives can do so. Therefore, valid recipes to update an archive
10092member target probably must use @code{ar}. For example, this rule
10093says to create a member @file{hack.o} in archive @file{foolib} by
10094copying the file @file{hack.o}:
10095
10096@example
10097foolib(hack.o) : hack.o
10098 ar cr foolib hack.o
10099@end example
10100
10101In fact, nearly all archive member targets are updated in just this way
10102and there is an implicit rule to do it for you. @strong{Please note:} The
10103@samp{c} flag to @code{ar} is required if the archive file does not
10104already exist.
10105
10106To specify several members in the same archive, you can write all the
10107member names together between the parentheses. For example:
10108
10109@example
10110foolib(hack.o kludge.o)
10111@end example
10112
10113@noindent
10114is equivalent to:
10115
10116@example
10117foolib(hack.o) foolib(kludge.o)
10118@end example
10119
10120@cindex wildcard, in archive member
10121You can also use shell-style wildcards in an archive member reference.
10122@xref{Wildcards, ,Using Wildcard Characters in File Names}. For
10123example, @w{@samp{foolib(*.o)}} expands to all existing members of the
10124@file{foolib} archive whose names end in @samp{.o}; perhaps
10125@samp{@w{foolib(hack.o)} @w{foolib(kludge.o)}}.
10126
10127@node Archive Update, Archive Pitfalls, Archive Members, Archives
10128@section Implicit Rule for Archive Member Targets
10129
10130Recall that a target that looks like @file{@var{a}(@var{m})} stands for the
10131member named @var{m} in the archive file @var{a}.
10132
10133When @code{make} looks for an implicit rule for such a target, as a special
10134feature it considers implicit rules that match @file{(@var{m})}, as well as
10135those that match the actual target @file{@var{a}(@var{m})}.
10136
10137This causes one special rule whose target is @file{(%)} to match. This
10138rule updates the target @file{@var{a}(@var{m})} by copying the file @var{m}
10139into the archive. For example, it will update the archive member target
10140@file{foo.a(bar.o)} by copying the @emph{file} @file{bar.o} into the
10141archive @file{foo.a} as a @emph{member} named @file{bar.o}.
10142
10143When this rule is chained with others, the result is very powerful.
10144Thus, @samp{make "foo.a(bar.o)"} (the quotes are needed to protect the
10145@samp{(} and @samp{)} from being interpreted specially by the shell) in
10146the presence of a file @file{bar.c} is enough to cause the following
10147recipe to be run, even without a makefile:
10148
10149@example
10150cc -c bar.c -o bar.o
10151ar r foo.a bar.o
10152rm -f bar.o
10153@end example
10154
10155@noindent
10156Here @code{make} has envisioned the file @file{bar.o} as an intermediate
10157file. @xref{Chained Rules, ,Chains of Implicit Rules}.
10158
10159Implicit rules such as this one are written using the automatic variable
10160@samp{$%}. @xref{Automatic Variables}.
10161
10162An archive member name in an archive cannot contain a directory name, but
10163it may be useful in a makefile to pretend that it does. If you write an
10164archive member target @file{foo.a(dir/file.o)}, @code{make} will perform
10165automatic updating with this recipe:
10166
10167@example
10168ar r foo.a dir/file.o
10169@end example
10170
10171@noindent
10172which has the effect of copying the file @file{dir/file.o} into a member
10173named @file{file.o}. In connection with such usage, the automatic variables
10174@code{%D} and @code{%F} may be useful.
10175
10176@menu
10177* Archive Symbols:: How to update archive symbol directories.
10178@end menu
10179
10180@node Archive Symbols, , Archive Update, Archive Update
10181@subsection Updating Archive Symbol Directories
10182@cindex @code{__.SYMDEF}
10183@cindex updating archive symbol directories
10184@cindex archive symbol directory updating
10185@cindex symbol directories, updating archive
10186@cindex directories, updating archive symbol
10187
10188An archive file that is used as a library usually contains a special member
10189named @file{__.SYMDEF} that contains a directory of the external symbol
10190names defined by all the other members. After you update any other
10191members, you need to update @file{__.SYMDEF} so that it will summarize the
10192other members properly. This is done by running the @code{ranlib} program:
10193
10194@example
10195ranlib @var{archivefile}
10196@end example
10197
10198Normally you would put this command in the rule for the archive file,
10199and make all the members of the archive file prerequisites of that rule.
10200For example,
10201
10202@example
10203libfoo.a: libfoo.a(x.o) libfoo.a(y.o) @dots{}
10204 ranlib libfoo.a
10205@end example
10206
10207@noindent
10208The effect of this is to update archive members @file{x.o}, @file{y.o},
10209etc., and then update the symbol directory member @file{__.SYMDEF} by
10210running @code{ranlib}. The rules for updating the members are not shown
10211here; most likely you can omit them and use the implicit rule which copies
10212files into the archive, as described in the preceding section.
10213
10214This is not necessary when using the GNU @code{ar} program, which
10215updates the @file{__.SYMDEF} member automatically.
10216
10217@node Archive Pitfalls, Archive Suffix Rules, Archive Update, Archives
10218@section Dangers When Using Archives
10219@cindex archive, and parallel execution
10220@cindex parallel execution, and archive update
10221@cindex archive, and @code{-j}
10222@cindex @code{-j}, and archive update
10223
10224It is important to be careful when using parallel execution (the
10225@code{-j} switch; @pxref{Parallel, ,Parallel Execution}) and archives.
10226If multiple @code{ar} commands run at the same time on the same archive
10227file, they will not know about each other and can corrupt the file.
10228
10229Possibly a future version of @code{make} will provide a mechanism to
10230circumvent this problem by serializing all recipes that operate on the
10231same archive file. But for the time being, you must either write your
10232makefiles to avoid this problem in some other way, or not use @code{-j}.
10233
10234@node Archive Suffix Rules, , Archive Pitfalls, Archives
10235@section Suffix Rules for Archive Files
10236@cindex suffix rule, for archive
10237@cindex archive, suffix rule for
10238@cindex library archive, suffix rule for
10239@cindex @code{.a} (archives)
10240
10241You can write a special kind of suffix rule for dealing with archive
10242files. @xref{Suffix Rules}, for a full explanation of suffix rules.
10243Archive suffix rules are obsolete in GNU @code{make}, because pattern
10244rules for archives are a more general mechanism (@pxref{Archive
10245Update}). But they are retained for compatibility with other
10246@code{make}s.
10247
10248To write a suffix rule for archives, you simply write a suffix rule
10249using the target suffix @samp{.a} (the usual suffix for archive files).
10250For example, here is the old-fashioned suffix rule to update a library
10251archive from C source files:
10252
10253@example
10254@group
10255.c.a:
10256 $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o
10257 $(AR) r $@@ $*.o
10258 $(RM) $*.o
10259@end group
10260@end example
10261
10262@noindent
10263This works just as if you had written the pattern rule:
10264
10265@example
10266@group
10267(%.o): %.c
10268 $(CC) $(CFLAGS) $(CPPFLAGS) -c $< -o $*.o
10269 $(AR) r $@@ $*.o
10270 $(RM) $*.o
10271@end group
10272@end example
10273
10274In fact, this is just what @code{make} does when it sees a suffix rule
10275with @samp{.a} as the target suffix. Any double-suffix rule
10276@w{@samp{.@var{x}.a}} is converted to a pattern rule with the target
10277pattern @samp{(%.o)} and a prerequisite pattern of @samp{%.@var{x}}.
10278
10279Since you might want to use @samp{.a} as the suffix for some other kind
10280of file, @code{make} also converts archive suffix rules to pattern rules
10281in the normal way (@pxref{Suffix Rules}). Thus a double-suffix rule
10282@w{@samp{.@var{x}.a}} produces two pattern rules: @samp{@w{(%.o):}
10283@w{%.@var{x}}} and @samp{@w{%.a}: @w{%.@var{x}}}.@refill
10284
10285@node Features, Missing, Archives, Top
10286@chapter Features of GNU @code{make}
10287@cindex features of GNU @code{make}
10288@cindex portability
10289@cindex compatibility
10290
10291Here is a summary of the features of GNU @code{make}, for comparison
10292with and credit to other versions of @code{make}. We consider the
10293features of @code{make} in 4.2 BSD systems as a baseline. If you are
10294concerned with writing portable makefiles, you should not use the
10295features of @code{make} listed here, nor the ones in @ref{Missing}.
10296
10297Many features come from the version of @code{make} in System V.
10298
10299@itemize @bullet
10300@item
10301The @code{VPATH} variable and its special meaning.
10302@xref{Directory Search, , Searching Directories for Prerequisites}.
10303This feature exists in System V @code{make}, but is undocumented.
10304It is documented in 4.3 BSD @code{make} (which says it mimics System V's
10305@code{VPATH} feature).@refill
10306
10307@item
10308Included makefiles. @xref{Include, ,Including Other Makefiles}.
10309Allowing multiple files to be included with a single directive is a GNU
10310extension.
10311
10312@item
10313Variables are read from and communicated via the environment.
10314@xref{Environment, ,Variables from the Environment}.
10315
10316@item
10317Options passed through the variable @code{MAKEFLAGS} to recursive
10318invocations of @code{make}.
10319@xref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
10320
10321@item
10322The automatic variable @code{$%} is set to the member name
10323in an archive reference. @xref{Automatic Variables}.
10324
10325@item
10326The automatic variables @code{$@@}, @code{$*}, @code{$<}, @code{$%},
10327and @code{$?} have corresponding forms like @code{$(@@F)} and
10328@code{$(@@D)}. We have generalized this to @code{$^} as an obvious
10329extension. @xref{Automatic Variables}.@refill
10330
10331@item
10332Substitution variable references.
10333@xref{Reference, ,Basics of Variable References}.
10334
10335@item
10336The command line options @samp{-b} and @samp{-m}, accepted and
10337ignored. In System V @code{make}, these options actually do something.
10338
10339@item
10340Execution of recursive commands to run @code{make} via the variable
10341@code{MAKE} even if @samp{-n}, @samp{-q} or @samp{-t} is specified.
10342@xref{Recursion, ,Recursive Use of @code{make}}.
10343
10344@item
10345Support for suffix @samp{.a} in suffix rules. @xref{Archive Suffix
10346Rules}. This feature is obsolete in GNU @code{make}, because the
10347general feature of rule chaining (@pxref{Chained Rules, ,Chains of
10348Implicit Rules}) allows one pattern rule for installing members in an
10349archive (@pxref{Archive Update}) to be sufficient.
10350
10351@item
10352The arrangement of lines and backslash-newline combinations in
10353recipes is retained when the recipes are printed, so they appear as
10354they do in the makefile, except for the stripping of initial
10355whitespace.
10356@end itemize
10357
10358The following features were inspired by various other versions of
10359@code{make}. In some cases it is unclear exactly which versions inspired
10360which others.
10361
10362@itemize @bullet
10363@item
10364Pattern rules using @samp{%}.
10365This has been implemented in several versions of @code{make}.
10366We're not sure who invented it first, but it's been spread around a bit.
10367@xref{Pattern Rules, ,Defining and Redefining Pattern Rules}.@refill
10368
10369@item
10370Rule chaining and implicit intermediate files.
10371This was implemented by Stu Feldman in his version of @code{make}
10372for AT&T Eighth Edition Research Unix, and later by Andrew Hume of
10373AT&T Bell Labs in his @code{mk} program (where he terms it
10374``transitive closure''). We do not really know if
10375we got this from either of them or thought it up ourselves at the
10376same time. @xref{Chained Rules, ,Chains of Implicit Rules}.
10377
10378@item
10379The automatic variable @code{$^} containing a list of all prerequisites
10380of the current target. We did not invent this, but we have no idea who
10381did. @xref{Automatic Variables}. The automatic variable
10382@code{$+} is a simple extension of @code{$^}.
10383
10384@item
10385The ``what if'' flag (@samp{-W} in GNU @code{make}) was (as far as we know)
10386invented by Andrew Hume in @code{mk}.
10387@xref{Instead of Execution, ,Instead of Executing Recipes}.
10388
10389@item
10390The concept of doing several things at once (parallelism) exists in
10391many incarnations of @code{make} and similar programs, though not in the
10392System V or BSD implementations. @xref{Execution, ,Recipe Execution}.
10393
10394@item
10395Modified variable references using pattern substitution come from
10396SunOS 4. @xref{Reference, ,Basics of Variable References}.
10397This functionality was provided in GNU @code{make} by the
10398@code{patsubst} function before the alternate syntax was implemented
10399for compatibility with SunOS 4. It is not altogether clear who
10400inspired whom, since GNU @code{make} had @code{patsubst} before SunOS
104014 was released.@refill
10402
10403@item
10404The special significance of @samp{+} characters preceding recipe lines
10405(@pxref{Instead of Execution, ,Instead of Executing Recipes}) is
10406mandated by @cite{IEEE Standard 1003.2-1992} (POSIX.2).
10407
10408@item
10409The @samp{+=} syntax to append to the value of a variable comes from SunOS
104104 @code{make}. @xref{Appending, , Appending More Text to Variables}.
10411
10412@item
10413The syntax @w{@samp{@var{archive}(@var{mem1} @var{mem2}@dots{})}} to list
10414multiple members in a single archive file comes from SunOS 4 @code{make}.
10415@xref{Archive Members}.
10416
10417@item
10418The @code{-include} directive to include makefiles with no error for a
10419nonexistent file comes from SunOS 4 @code{make}. (But note that SunOS 4
10420@code{make} does not allow multiple makefiles to be specified in one
10421@code{-include} directive.) The same feature appears with the name
10422@code{sinclude} in SGI @code{make} and perhaps others.
10423@end itemize
10424
10425The remaining features are inventions new in GNU @code{make}:
10426
10427@itemize @bullet
10428@item
10429Use the @samp{-v} or @samp{--version} option to print version and
10430copyright information.
10431
10432@item
10433Use the @samp{-h} or @samp{--help} option to summarize the options to
10434@code{make}.
10435
10436@item
10437Simply-expanded variables. @xref{Flavors, ,The Two Flavors of Variables}.
10438
10439@item
10440Pass command line variable assignments automatically through the
10441variable @code{MAKE} to recursive @code{make} invocations.
10442@xref{Recursion, ,Recursive Use of @code{make}}.
10443
10444@item
10445Use the @samp{-C} or @samp{--directory} command option to change
10446directory. @xref{Options Summary, ,Summary of Options}.
10447
10448@item
10449Make verbatim variable definitions with @code{define}.
10450@xref{Multi-Line, ,Defining Multi-Line Variables}.
10451
10452@item
10453Declare phony targets with the special target @code{.PHONY}.
10454
10455Andrew Hume of AT&T Bell Labs implemented a similar feature with a
10456different syntax in his @code{mk} program. This seems to be a case of
10457parallel discovery. @xref{Phony Targets, ,Phony Targets}.
10458
10459@item
10460Manipulate text by calling functions.
10461@xref{Functions, ,Functions for Transforming Text}.
10462
10463@item
10464Use the @samp{-o} or @samp{--old-file}
10465option to pretend a file's modification-time is old.
10466@xref{Avoiding Compilation, ,Avoiding Recompilation of Some Files}.
10467
10468@item
10469Conditional execution.
10470
10471This feature has been implemented numerous times in various versions
10472of @code{make}; it seems a natural extension derived from the features
10473of the C preprocessor and similar macro languages and is not a
10474revolutionary concept. @xref{Conditionals, ,Conditional Parts of Makefiles}.
10475
10476@item
10477Specify a search path for included makefiles.
10478@xref{Include, ,Including Other Makefiles}.
10479
10480@item
10481Specify extra makefiles to read with an environment variable.
10482@xref{MAKEFILES Variable, ,The Variable @code{MAKEFILES}}.
10483
10484@item
10485Strip leading sequences of @samp{./} from file names, so that
10486@file{./@var{file}} and @file{@var{file}} are considered to be the
10487same file.@refill
10488
10489@item
10490Use a special search method for library prerequisites written in the
10491form @samp{-l@var{name}}.
10492@xref{Libraries/Search, ,Directory Search for Link Libraries}.
10493
10494@item
10495Allow suffixes for suffix rules
10496(@pxref{Suffix Rules, ,Old-Fashioned Suffix Rules}) to contain any
10497characters. In other versions of @code{make}, they must begin with
10498@samp{.} and not contain any @samp{/} characters.
10499
10500@item
10501Keep track of the current level of @code{make} recursion using the
10502variable @code{MAKELEVEL}. @xref{Recursion, ,Recursive Use of @code{make}}.
10503
10504@item
10505Provide any goals given on the command line in the variable
10506@code{MAKECMDGOALS}. @xref{Goals, ,Arguments to Specify the Goals}.
10507
10508@item
10509Specify static pattern rules. @xref{Static Pattern, ,Static Pattern Rules}.
10510
10511@item
10512Provide selective @code{vpath} search.
10513@xref{Directory Search, ,Searching Directories for Prerequisites}.
10514
10515@item
10516Provide computed variable references.
10517@xref{Reference, ,Basics of Variable References}.
10518
10519@item
10520Update makefiles. @xref{Remaking Makefiles, ,How Makefiles Are Remade}.
10521System V @code{make} has a very, very limited form of this
10522functionality in that it will check out SCCS files for makefiles.
10523
10524@item
10525Various new built-in implicit rules.
10526@xref{Catalogue of Rules, ,Catalogue of Implicit Rules}.
10527
10528@item
10529The built-in variable @samp{MAKE_VERSION} gives the version number of
10530@code{make}.
10531@vindex MAKE_VERSION
10532@end itemize
10533
10534@node Missing, Makefile Conventions, Features, Top
10535@chapter Incompatibilities and Missing Features
10536@cindex incompatibilities
10537@cindex missing features
10538@cindex features, missing
10539
10540The @code{make} programs in various other systems support a few features
10541that are not implemented in GNU @code{make}. The POSIX.2 standard
10542(@cite{IEEE Standard 1003.2-1992}) which specifies @code{make} does not
10543require any of these features.@refill
10544
10545@itemize @bullet
10546@item
10547A target of the form @samp{@var{file}((@var{entry}))} stands for a member
10548of archive file @var{file}. The member is chosen, not by name, but by
10549being an object file which defines the linker symbol @var{entry}.@refill
10550
10551This feature was not put into GNU @code{make} because of the
10552nonmodularity of putting knowledge into @code{make} of the internal
10553format of archive file symbol tables.
10554@xref{Archive Symbols, ,Updating Archive Symbol Directories}.
10555
10556@item
10557Suffixes (used in suffix rules) that end with the character @samp{~}
10558have a special meaning to System V @code{make};
10559they refer to the SCCS file that corresponds
10560to the file one would get without the @samp{~}. For example, the
10561suffix rule @samp{.c~.o} would make the file @file{@var{n}.o} from
10562the SCCS file @file{s.@var{n}.c}. For complete coverage, a whole
10563series of such suffix rules is required.
10564@xref{Suffix Rules, ,Old-Fashioned Suffix Rules}.
10565
10566In GNU @code{make}, this entire series of cases is handled by two
10567pattern rules for extraction from SCCS, in combination with the
10568general feature of rule chaining.
10569@xref{Chained Rules, ,Chains of Implicit Rules}.
10570
10571@item
10572In System V and 4.3 BSD @code{make}, files found by @code{VPATH}
10573search (@pxref{Directory Search, ,Searching Directories for
10574Prerequisites}) have their names changed inside recipes. We feel it
10575is much cleaner to always use automatic variables and thus make this
10576feature obsolete.@refill
10577
10578@item
10579In some Unix @code{make}s, the automatic variable @code{$*} appearing in
10580the prerequisites of a rule has the amazingly strange ``feature'' of
10581expanding to the full name of the @emph{target of that rule}. We cannot
10582imagine what went on in the minds of Unix @code{make} developers to do
10583this; it is utterly inconsistent with the normal definition of @code{$*}.
10584@vindex * @r{(automatic variable), unsupported bizarre usage}
10585
10586@item
10587In some Unix @code{make}s, implicit rule search (@pxref{Implicit
10588Rules, ,Using Implicit Rules}) is apparently done for @emph{all}
10589targets, not just those without recipes. This means you can
10590do:@refill
10591
10592@example
10593@group
10594foo.o:
10595 cc -c foo.c
10596@end group
10597@end example
10598
10599@noindent
10600and Unix @code{make} will intuit that @file{foo.o} depends on
10601@file{foo.c}.@refill
10602
10603We feel that such usage is broken. The prerequisite properties of
10604@code{make} are well-defined (for GNU @code{make}, at least),
10605and doing such a thing simply does not fit the model.@refill
10606
10607@item
10608GNU @code{make} does not include any built-in implicit rules for
10609compiling or preprocessing EFL programs. If we hear of anyone who is
10610using EFL, we will gladly add them.
10611
10612@item
10613It appears that in SVR4 @code{make}, a suffix rule can be specified
10614with no recipe, and it is treated as if it had an empty recipe
10615(@pxref{Empty Recipes}). For example:
10616
10617@example
10618.c.a:
10619@end example
10620
10621@noindent
10622will override the built-in @file{.c.a} suffix rule.
10623
10624We feel that it is cleaner for a rule without a recipe to always simply
10625add to the prerequisite list for the target. The above example can be
10626easily rewritten to get the desired behavior in GNU @code{make}:
10627
10628@example
10629.c.a: ;
10630@end example
10631
10632@item
10633Some versions of @code{make} invoke the shell with the @samp{-e} flag,
10634except under @samp{-k} (@pxref{Testing, ,Testing the Compilation of a
10635Program}). The @samp{-e} flag tells the shell to exit as soon as any
10636program it runs returns a nonzero status. We feel it is cleaner to
10637write each line of the recipe to stand on its own and not require this
10638special treatment.
10639@end itemize
10640
10641@comment The makefile standards are in a separate file that is also
10642@comment included by standards.texi.
10643@include make-stds.texi
10644
10645@node Quick Reference, Error Messages, Makefile Conventions, Top
10646@appendix Quick Reference
10647
10648This appendix summarizes the directives, text manipulation functions,
10649and special variables which GNU @code{make} understands.
10650@xref{Special Targets}, @ref{Catalogue of Rules, ,Catalogue of Implicit Rules},
10651and @ref{Options Summary, ,Summary of Options},
10652for other summaries.
10653
10654Here is a summary of the directives GNU @code{make} recognizes:
10655
10656@table @code
10657@item define @var{variable}
10658@itemx define @var{variable} =
10659@itemx define @var{variable} :=
10660@itemx define @var{variable} +=
10661@itemx define @var{variable} ?=
10662@itemx endef
10663Define multi-line variables.@*
10664@xref{Multi-Line}.
10665
10666@item undefine @var{variable}
10667Undefining variables.@*
10668@xref{Undefine Directive}.
10669
10670@item ifdef @var{variable}
10671@itemx ifndef @var{variable}
10672@itemx ifeq (@var{a},@var{b})
10673@itemx ifeq "@var{a}" "@var{b}"
10674@itemx ifeq '@var{a}' '@var{b}'
10675@itemx ifneq (@var{a},@var{b})
10676@itemx ifneq "@var{a}" "@var{b}"
10677@itemx ifneq '@var{a}' '@var{b}'
10678@itemx else
10679@itemx endif
10680Conditionally evaluate part of the makefile.@*
10681@xref{Conditionals}.
10682
10683@item include @var{file}
10684@itemx -include @var{file}
10685@itemx sinclude @var{file}
10686Include another makefile.@*
10687@xref{Include, ,Including Other Makefiles}.
10688
10689@item override @var{variable-assignment}
10690Define a variable, overriding any previous definition, even one from
10691the command line.@*
10692@xref{Override Directive, ,The @code{override} Directive}.
10693
10694@item export
10695Tell @code{make} to export all variables to child processes by default.@*
10696@xref{Variables/Recursion, , Communicating Variables to a Sub-@code{make}}.
10697
10698@item export @var{variable}
10699@itemx export @var{variable-assignment}
10700@itemx unexport @var{variable}
10701Tell @code{make} whether or not to export a particular variable to child
10702processes.@*
10703@xref{Variables/Recursion, , Communicating Variables to a Sub-@code{make}}.
10704
10705@item private @var{variable-assignment}
10706Do not allow this variable assignment to be inherited by prerequisites.@*
10707@xref{Suppressing Inheritance}.
10708
10709@item vpath @var{pattern} @var{path}
10710Specify a search path for files matching a @samp{%} pattern.@*
10711@xref{Selective Search, , The @code{vpath} Directive}.
10712
10713@item vpath @var{pattern}
10714Remove all search paths previously specified for @var{pattern}.
10715
10716@item vpath
10717Remove all search paths previously specified in any @code{vpath}
10718directive.
10719@end table
10720
10721Here is a summary of the built-in functions (@pxref{Functions}):
10722
10723@table @code
10724@item $(subst @var{from},@var{to},@var{text})
10725Replace @var{from} with @var{to} in @var{text}.@*
10726@xref{Text Functions, , Functions for String Substitution and Analysis}.
10727
10728@item $(patsubst @var{pattern},@var{replacement},@var{text})
10729Replace words matching @var{pattern} with @var{replacement} in @var{text}.@*
10730@xref{Text Functions, , Functions for String Substitution and Analysis}.
10731
10732@item $(strip @var{string})
10733Remove excess whitespace characters from @var{string}.@*
10734@xref{Text Functions, , Functions for String Substitution and Analysis}.
10735
10736@item $(findstring @var{find},@var{text})
10737Locate @var{find} in @var{text}.@*
10738@xref{Text Functions, , Functions for String Substitution and Analysis}.
10739
10740@item $(filter @var{pattern}@dots{},@var{text})
10741Select words in @var{text} that match one of the @var{pattern} words.@*
10742@xref{Text Functions, , Functions for String Substitution and Analysis}.
10743
10744@item $(filter-out @var{pattern}@dots{},@var{text})
10745Select words in @var{text} that @emph{do not} match any of the @var{pattern} words.@*
10746@xref{Text Functions, , Functions for String Substitution and Analysis}.
10747
10748@item $(sort @var{list})
10749Sort the words in @var{list} lexicographically, removing duplicates.@*
10750@xref{Text Functions, , Functions for String Substitution and Analysis}.
10751
10752@item $(word @var{n},@var{text})
10753Extract the @var{n}th word (one-origin) of @var{text}.@*
10754@xref{Text Functions, , Functions for String Substitution and Analysis}.
10755
10756@item $(words @var{text})
10757Count the number of words in @var{text}.@*
10758@xref{Text Functions, , Functions for String Substitution and Analysis}.
10759
10760@item $(wordlist @var{s},@var{e},@var{text})
10761Returns the list of words in @var{text} from @var{s} to @var{e}.@*
10762@xref{Text Functions, , Functions for String Substitution and Analysis}.
10763
10764@item $(firstword @var{names}@dots{})
10765Extract the first word of @var{names}.@*
10766@xref{Text Functions, , Functions for String Substitution and Analysis}.
10767
10768@item $(lastword @var{names}@dots{})
10769Extract the last word of @var{names}.@*
10770@xref{Text Functions, , Functions for String Substitution and Analysis}.
10771
10772@item $(dir @var{names}@dots{})
10773Extract the directory part of each file name.@*
10774@xref{File Name Functions, ,Functions for File Names}.
10775
10776@item $(notdir @var{names}@dots{})
10777Extract the non-directory part of each file name.@*
10778@xref{File Name Functions, ,Functions for File Names}.
10779
10780@item $(suffix @var{names}@dots{})
10781Extract the suffix (the last @samp{.} and following characters) of each file name.@*
10782@xref{File Name Functions, ,Functions for File Names}.
10783
10784@item $(basename @var{names}@dots{})
10785Extract the base name (name without suffix) of each file name.@*
10786@xref{File Name Functions, ,Functions for File Names}.
10787
10788@item $(addsuffix @var{suffix},@var{names}@dots{})
10789Append @var{suffix} to each word in @var{names}.@*
10790@xref{File Name Functions, ,Functions for File Names}.
10791
10792@item $(addprefix @var{prefix},@var{names}@dots{})
10793Prepend @var{prefix} to each word in @var{names}.@*
10794@xref{File Name Functions, ,Functions for File Names}.
10795
10796@item $(join @var{list1},@var{list2})
10797Join two parallel lists of words.@*
10798@xref{File Name Functions, ,Functions for File Names}.
10799
10800@item $(wildcard @var{pattern}@dots{})
10801Find file names matching a shell file name pattern (@emph{not} a
10802@samp{%} pattern).@*
10803@xref{Wildcard Function, ,The Function @code{wildcard}}.
10804
10805@item $(realpath @var{names}@dots{})
10806For each file name in @var{names}, expand to an absolute name that
10807does not contain any @code{.}, @code{..}, nor symlinks.@*
10808@xref{File Name Functions, ,Functions for File Names}.
10809
10810@item $(abspath @var{names}@dots{})
10811For each file name in @var{names}, expand to an absolute name that
10812does not contain any @code{.} or @code{..} components, but preserves
10813symlinks.@*
10814@xref{File Name Functions, ,Functions for File Names}.
10815
10816@item $(error @var{text}@dots{})
10817When this function is evaluated, @code{make} generates a fatal error
10818with the message @var{text}.@*
10819@xref{Make Control Functions, ,Functions That Control Make}.
10820
10821@item $(warning @var{text}@dots{})
10822When this function is evaluated, @code{make} generates a warning with
10823the message @var{text}.@*
10824@xref{Make Control Functions, ,Functions That Control Make}.
10825
10826@item $(shell @var{command})
10827Execute a shell command and return its output.@*
10828@xref{Shell Function, , The @code{shell} Function}.
10829
10830@item $(origin @var{variable})
10831Return a string describing how the @code{make} variable @var{variable} was
10832defined.@*
10833@xref{Origin Function, , The @code{origin} Function}.
10834
10835@item $(flavor @var{variable})
10836Return a string describing the flavor of the @code{make} variable
10837@var{variable}.@*
10838@xref{Flavor Function, , The @code{flavor} Function}.
10839
10840@item $(foreach @var{var},@var{words},@var{text})
10841Evaluate @var{text} with @var{var} bound to each word in @var{words},
10842and concatenate the results.@*
10843@xref{Foreach Function, ,The @code{foreach} Function}.
10844
10845@item $(if @var{condition},@var{then-part}[,@var{else-part}])
10846Evaluate the condition @var{condition}; if it's non-empty substitute
10847the expansion of the @var{then-part} otherwise substitute the
10848expansion of the @var{else-part}.@*
10849@xref{Conditional Functions, ,Functions for Conditionals}.
10850
10851@item $(or @var{condition1}[,@var{condition2}[,@var{condition3}@dots{}]])
10852Evaluate each condition @var{conditionN} one at a time; substitute the
10853first non-empty expansion. If all expansions are empty, substitute
10854the empty string.@*
10855@xref{Conditional Functions, ,Functions for Conditionals}.
10856
10857@item $(and @var{condition1}[,@var{condition2}[,@var{condition3}@dots{}]])
10858Evaluate each condition @var{conditionN} one at a time; if any
10859expansion results in the empty string substitute the empty string. If
10860all expansions result in a non-empty string, substitute the expansion
10861of the last @var{condition}.@*
10862@xref{Conditional Functions, ,Functions for Conditionals}.
10863
10864@item $(call @var{var},@var{param},@dots{})
10865Evaluate the variable @var{var} replacing any references to @code{$(1)},
10866@code{$(2)} with the first, second, etc.@: @var{param} values.@*
10867@xref{Call Function, ,The @code{call} Function}.
10868
10869@item $(eval @var{text})
10870Evaluate @var{text} then read the results as makefile commands.
10871Expands to the empty string.@*
10872@xref{Eval Function, ,The @code{eval} Function}.
10873
10874@item $(value @var{var})
10875Evaluates to the contents of the variable @var{var}, with no expansion
10876performed on it.@*
10877@xref{Value Function, ,The @code{value} Function}.
10878@end table
10879
10880Here is a summary of the automatic variables.
10881@xref{Automatic Variables},
10882for full information.
10883
10884@table @code
10885@item $@@
10886The file name of the target.
10887
10888@item $%
10889The target member name, when the target is an archive member.
10890
10891@item $<
10892The name of the first prerequisite.
10893
10894@item $?
10895The names of all the prerequisites that are
10896newer than the target, with spaces between them.
10897For prerequisites which are archive members, only
10898the named member is used (@pxref{Archives}).
10899
10900@item $^
10901@itemx $+
10902The names of all the prerequisites, with spaces between them. For
10903prerequisites which are archive members, only the named member is used
10904(@pxref{Archives}). The value of @code{$^} omits duplicate
10905prerequisites, while @code{$+} retains them and preserves their order.
10906
10907@item $*
10908The stem with which an implicit rule matches
10909(@pxref{Pattern Match, ,How Patterns Match}).
10910
10911@item $(@@D)
10912@itemx $(@@F)
10913The directory part and the file-within-directory part of @code{$@@}.
10914
10915@item $(*D)
10916@itemx $(*F)
10917The directory part and the file-within-directory part of @code{$*}.
10918
10919@item $(%D)
10920@itemx $(%F)
10921The directory part and the file-within-directory part of @code{$%}.
10922
10923@item $(<D)
10924@itemx $(<F)
10925The directory part and the file-within-directory part of @code{$<}.
10926
10927@item $(^D)
10928@itemx $(^F)
10929The directory part and the file-within-directory part of @code{$^}.
10930
10931@item $(+D)
10932@itemx $(+F)
10933The directory part and the file-within-directory part of @code{$+}.
10934
10935@item $(?D)
10936@itemx $(?F)
10937The directory part and the file-within-directory part of @code{$?}.
10938@end table
10939
10940These variables are used specially by GNU @code{make}:
10941
10942@table @code
10943@item MAKEFILES
10944
10945Makefiles to be read on every invocation of @code{make}.@*
10946@xref{MAKEFILES Variable, ,The Variable @code{MAKEFILES}}.
10947
10948@item VPATH
10949
10950Directory search path for files not found in the current directory.@*
10951@xref{General Search, , @code{VPATH} Search Path for All Prerequisites}.
10952
10953@item SHELL
10954
10955The name of the system default command interpreter, usually @file{/bin/sh}.
10956You can set @code{SHELL} in the makefile to change the shell used to run
10957recipes. @xref{Execution, ,Recipe Execution}. The @code{SHELL}
10958variable is handled specially when importing from and exporting to the
10959environment. @xref{Choosing the Shell}.
10960
10961@item MAKESHELL
10962
10963On MS-DOS only, the name of the command interpreter that is to be used
10964by @code{make}. This value takes precedence over the value of
10965@code{SHELL}. @xref{Execution, ,MAKESHELL variable}.
10966
10967@item MAKE
10968
10969The name with which @code{make} was invoked. Using this variable in
10970recipes has special meaning. @xref{MAKE Variable, ,How the
10971@code{MAKE} Variable Works}.
10972
10973@item MAKELEVEL
10974
10975The number of levels of recursion (sub-@code{make}s).@*
10976@xref{Variables/Recursion}.
10977
10978@item MAKEFLAGS
10979
10980The flags given to @code{make}. You can set this in the environment or
10981a makefile to set flags.@*
10982@xref{Options/Recursion, ,Communicating Options to a Sub-@code{make}}.
10983
10984It is @emph{never} appropriate to use @code{MAKEFLAGS} directly in a
10985recipe line: its contents may not be quoted correctly for use in the
10986shell. Always allow recursive @code{make}'s to obtain these values
10987through the environment from its parent.
10988
10989@item MAKECMDGOALS
10990
10991The targets given to @code{make} on the command line. Setting this
10992variable has no effect on the operation of @code{make}.@*
10993@xref{Goals, ,Arguments to Specify the Goals}.
10994
10995@item CURDIR
10996
10997Set to the pathname of the current working directory (after all
10998@code{-C} options are processed, if any). Setting this variable has no
10999effect on the operation of @code{make}.@*
11000@xref{Recursion, ,Recursive Use of @code{make}}.
11001
11002@item SUFFIXES
11003
11004The default list of suffixes before @code{make} reads any makefiles.
11005
11006@item .LIBPATTERNS
11007Defines the naming of the libraries @code{make} searches for, and their
11008order.@*
11009@xref{Libraries/Search, ,Directory Search for Link Libraries}.
11010@end table
11011
11012@node Error Messages, Complex Makefile, Quick Reference, Top
11013@comment node-name, next, previous, up
11014@appendix Errors Generated by Make
11015
11016Here is a list of the more common errors you might see generated by
11017@code{make}, and some information about what they mean and how to fix
11018them.
11019
11020Sometimes @code{make} errors are not fatal, especially in the presence
11021of a @code{-} prefix on a recipe line, or the @code{-k} command line
11022option. Errors that are fatal are prefixed with the string
11023@code{***}.
11024
11025Error messages are all either prefixed with the name of the program
11026(usually @samp{make}), or, if the error is found in a makefile, the name
11027of the file and linenumber containing the problem.
11028
11029In the table below, these common prefixes are left off.
11030
11031@table @samp
11032
11033@item [@var{foo}] Error @var{NN}
11034@itemx [@var{foo}] @var{signal description}
11035These errors are not really @code{make} errors at all. They mean that a
11036program that @code{make} invoked as part of a recipe returned a
11037non-0 error code (@samp{Error @var{NN}}), which @code{make} interprets
11038as failure, or it exited in some other abnormal fashion (with a
11039signal of some type). @xref{Errors, ,Errors in Recipes}.
11040
11041If no @code{***} is attached to the message, then the subprocess failed
11042but the rule in the makefile was prefixed with the @code{-} special
11043character, so @code{make} ignored the error.
11044
11045@item missing separator. Stop.
11046@itemx missing separator (did you mean TAB instead of 8 spaces?). Stop.
11047This means that @code{make} could not understand much of anything
11048about the makefile line it just read. GNU @code{make} looks for
11049various separators (@code{:}, @code{=}, recipe prefix characters,
11050etc.) to indicate what kind of line it's parsing. This message means
11051it couldn't find a valid one.
11052
11053One of the most common reasons for this message is that you (or
11054perhaps your oh-so-helpful editor, as is the case with many MS-Windows
11055editors) have attempted to indent your recipe lines with spaces
11056instead of a tab character. In this case, @code{make} will use the
11057second form of the error above. Remember that every line in the
11058recipe must begin with a tab character (unless you set
11059@code{.RECIPEPREFIX}; @pxref{Special Variables}). Eight spaces do not
11060count. @xref{Rule Syntax}.
11061
11062@item recipe commences before first target. Stop.
11063@itemx missing rule before recipe. Stop.
11064This means the first thing in the makefile seems to be part of a
11065recipe: it begins with a recipe prefix character and doesn't appear to
11066be a legal @code{make} directive (such as a variable assignment).
11067Recipes must always be associated with a target.
11068
11069The second form is generated if the line has a semicolon as the first
11070non-whitespace character; @code{make} interprets this to mean you left
11071out the "target: prerequisite" section of a rule. @xref{Rule Syntax}.
11072
11073@item No rule to make target `@var{xxx}'.
11074@itemx No rule to make target `@var{xxx}', needed by `@var{yyy}'.
11075This means that @code{make} decided it needed to build a target, but
11076then couldn't find any instructions in the makefile on how to do that,
11077either explicit or implicit (including in the default rules database).
11078
11079If you want that file to be built, you will need to add a rule to your
11080makefile describing how that target can be built. Other possible
11081sources of this problem are typos in the makefile (if that filename is
11082wrong) or a corrupted source tree (if that file is not supposed to be
11083built, but rather only a prerequisite).
11084
11085@item No targets specified and no makefile found. Stop.
11086@itemx No targets. Stop.
11087The former means that you didn't provide any targets to be built on the
11088command line, and @code{make} couldn't find any makefiles to read in.
11089The latter means that some makefile was found, but it didn't contain any
11090default goal and none was given on the command line. GNU @code{make}
11091has nothing to do in these situations.
11092@xref{Makefile Arguments, ,Arguments to Specify the Makefile}.@refill
11093
11094@item Makefile `@var{xxx}' was not found.
11095@itemx Included makefile `@var{xxx}' was not found.
11096A makefile specified on the command line (first form) or included
11097(second form) was not found.
11098
11099@item warning: overriding recipe for target `@var{xxx}'
11100@itemx warning: ignoring old recipe for target `@var{xxx}'
11101GNU @code{make} allows only one recipe to be specified per target
11102(except for double-colon rules). If you give a recipe for a target
11103which already has been defined to have one, this warning is issued and
11104the second recipe will overwrite the first. @xref{Multiple Rules,
11105,Multiple Rules for One Target}.
11106
11107@item Circular @var{xxx} <- @var{yyy} dependency dropped.
11108This means that @code{make} detected a loop in the dependency graph:
11109after tracing the prerequisite @var{yyy} of target @var{xxx}, and its
11110prerequisites, etc., one of them depended on @var{xxx} again.
11111
11112@item Recursive variable `@var{xxx}' references itself (eventually). Stop.
11113This means you've defined a normal (recursive) @code{make} variable
11114@var{xxx} that, when it's expanded, will refer to itself (@var{xxx}).
11115This is not allowed; either use simply-expanded variables (@code{:=}) or
11116use the append operator (@code{+=}). @xref{Using Variables, ,How to Use
11117Variables}.
11118
11119@item Unterminated variable reference. Stop.
11120This means you forgot to provide the proper closing parenthesis
11121or brace in your variable or function reference.
11122
11123@item insufficient arguments to function `@var{xxx}'. Stop.
11124This means you haven't provided the requisite number of arguments for
11125this function. See the documentation of the function for a description
11126of its arguments. @xref{Functions, ,Functions for Transforming Text}.
11127
11128@item missing target pattern. Stop.
11129@itemx multiple target patterns. Stop.
11130@itemx target pattern contains no `%'. Stop.
11131@itemx mixed implicit and static pattern rules. Stop.
11132These are generated for malformed static pattern rules. The first means
11133there's no pattern in the target section of the rule; the second means
11134there are multiple patterns in the target section; the third means
11135the target doesn't contain a pattern character (@code{%}); and the
11136fourth means that all three parts of the static pattern rule contain
11137pattern characters (@code{%})--only the first two parts should.
11138@xref{Static Usage, ,Syntax of Static Pattern Rules}.
11139
11140@item warning: -jN forced in submake: disabling jobserver mode.
11141This warning and the next are generated if @code{make} detects error
11142conditions related to parallel processing on systems where
11143sub-@code{make}s can communicate (@pxref{Options/Recursion,
11144,Communicating Options to a Sub-@code{make}}). This warning is
11145generated if a recursive invocation of a @code{make} process is forced
11146to have @samp{-j@var{N}} in its argument list (where @var{N} is greater
11147than one). This could happen, for example, if you set the @code{MAKE}
11148environment variable to @samp{make -j2}. In this case, the
11149sub-@code{make} doesn't communicate with other @code{make} processes and
11150will simply pretend it has two jobs of its own.
11151
11152@item warning: jobserver unavailable: using -j1. Add `+' to parent make rule.
11153In order for @code{make} processes to communicate, the parent will pass
11154information to the child. Since this could result in problems if the
11155child process isn't actually a @code{make}, the parent will only do this
11156if it thinks the child is a @code{make}. The parent uses the normal
11157algorithms to determine this (@pxref{MAKE Variable, ,How the @code{MAKE}
11158Variable Works}). If the makefile is constructed such that the parent
11159doesn't know the child is a @code{make} process, then the child will
11160receive only part of the information necessary. In this case, the child
11161will generate this warning message and proceed with its build in a
11162sequential manner.
11163
11164@end table
11165
11166@node Complex Makefile, GNU Free Documentation License, Error Messages, Top
11167@appendix Complex Makefile Example
11168
11169Here is the makefile for the GNU @code{tar} program. This is a
11170moderately complex makefile.
11171
11172Because it is the first target, the default goal is @samp{all}. An
11173interesting feature of this makefile is that @file{testpad.h} is a
11174source file automatically created by the @code{testpad} program,
11175itself compiled from @file{testpad.c}.
11176
11177If you type @samp{make} or @samp{make all}, then @code{make} creates
11178the @file{tar} executable, the @file{rmt} daemon that provides
11179remote tape access, and the @file{tar.info} Info file.
11180
11181If you type @samp{make install}, then @code{make} not only creates
11182@file{tar}, @file{rmt}, and @file{tar.info}, but also installs
11183them.
11184
11185If you type @samp{make clean}, then @code{make} removes the @samp{.o}
11186files, and the @file{tar}, @file{rmt}, @file{testpad},
11187@file{testpad.h}, and @file{core} files.
11188
11189If you type @samp{make distclean}, then @code{make} not only removes
11190the same files as does @samp{make clean} but also the
11191@file{TAGS}, @file{Makefile}, and @file{config.status} files.
11192(Although it is not evident, this makefile (and
11193@file{config.status}) is generated by the user with the
11194@code{configure} program, which is provided in the @code{tar}
11195distribution, but is not shown here.)
11196
11197If you type @samp{make realclean}, then @code{make} removes the same
11198files as does @samp{make distclean} and also removes the Info files
11199generated from @file{tar.texinfo}.
11200
11201In addition, there are targets @code{shar} and @code{dist} that create
11202distribution kits.
11203
11204@example
11205@group
11206# Generated automatically from Makefile.in by configure.
11207# Un*x Makefile for GNU tar program.
11208# Copyright (C) 1991 Free Software Foundation, Inc.
11209@end group
11210
11211@group
11212# This program is free software; you can redistribute
11213# it and/or modify it under the terms of the GNU
11214# General Public License @dots{}
11215@dots{}
11216@dots{}
11217@end group
11218
11219SHELL = /bin/sh
11220
11221#### Start of system configuration section. ####
11222
11223srcdir = .
11224
11225@group
11226# If you use gcc, you should either run the
11227# fixincludes script that comes with it or else use
11228# gcc with the -traditional option. Otherwise ioctl
11229# calls will be compiled incorrectly on some systems.
11230CC = gcc -O
11231YACC = bison -y
11232INSTALL = /usr/local/bin/install -c
11233INSTALLDATA = /usr/local/bin/install -c -m 644
11234@end group
11235
11236# Things you might add to DEFS:
11237# -DSTDC_HEADERS If you have ANSI C headers and
11238# libraries.
11239# -DPOSIX If you have POSIX.1 headers and
11240# libraries.
11241# -DBSD42 If you have sys/dir.h (unless
11242# you use -DPOSIX), sys/file.h,
11243# and st_blocks in `struct stat'.
11244# -DUSG If you have System V/ANSI C
11245# string and memory functions
11246# and headers, sys/sysmacros.h,
11247# fcntl.h, getcwd, no valloc,
11248# and ndir.h (unless
11249# you use -DDIRENT).
11250# -DNO_MEMORY_H If USG or STDC_HEADERS but do not
11251# include memory.h.
11252# -DDIRENT If USG and you have dirent.h
11253# instead of ndir.h.
11254# -DSIGTYPE=int If your signal handlers
11255# return int, not void.
11256# -DNO_MTIO If you lack sys/mtio.h
11257# (magtape ioctls).
11258# -DNO_REMOTE If you do not have a remote shell
11259# or rexec.
11260# -DUSE_REXEC To use rexec for remote tape
11261# operations instead of
11262# forking rsh or remsh.
11263# -DVPRINTF_MISSING If you lack vprintf function
11264# (but have _doprnt).
11265# -DDOPRNT_MISSING If you lack _doprnt function.
11266# Also need to define
11267# -DVPRINTF_MISSING.
11268# -DFTIME_MISSING If you lack ftime system call.
11269# -DSTRSTR_MISSING If you lack strstr function.
11270# -DVALLOC_MISSING If you lack valloc function.
11271# -DMKDIR_MISSING If you lack mkdir and
11272# rmdir system calls.
11273# -DRENAME_MISSING If you lack rename system call.
11274# -DFTRUNCATE_MISSING If you lack ftruncate
11275# system call.
11276# -DV7 On Version 7 Unix (not
11277# tested in a long time).
11278# -DEMUL_OPEN3 If you lack a 3-argument version
11279# of open, and want to emulate it
11280# with system calls you do have.
11281# -DNO_OPEN3 If you lack the 3-argument open
11282# and want to disable the tar -k
11283# option instead of emulating open.
11284# -DXENIX If you have sys/inode.h
11285# and need it 94 to be included.
11286
11287DEFS = -DSIGTYPE=int -DDIRENT -DSTRSTR_MISSING \
11288 -DVPRINTF_MISSING -DBSD42
11289# Set this to rtapelib.o unless you defined NO_REMOTE,
11290# in which case make it empty.
11291RTAPELIB = rtapelib.o
11292LIBS =
11293DEF_AR_FILE = /dev/rmt8
11294DEFBLOCKING = 20
11295
11296@group
11297CDEBUG = -g
11298CFLAGS = $(CDEBUG) -I. -I$(srcdir) $(DEFS) \
11299 -DDEF_AR_FILE=\"$(DEF_AR_FILE)\" \
11300 -DDEFBLOCKING=$(DEFBLOCKING)
11301LDFLAGS = -g
11302@end group
11303
11304@group
11305prefix = /usr/local
11306# Prefix for each installed program,
11307# normally empty or `g'.
11308binprefix =
11309
11310# The directory to install tar in.
11311bindir = $(prefix)/bin
11312
11313# The directory to install the info files in.
11314infodir = $(prefix)/info
11315@end group
11316
11317#### End of system configuration section. ####
11318
11319@group
11320SRCS_C = tar.c create.c extract.c buffer.c \
11321 getoldopt.c update.c gnu.c mangle.c \
11322 version.c list.c names.c diffarch.c \
11323 port.c wildmat.c getopt.c getopt1.c \
11324 regex.c
11325SRCS_Y = getdate.y
11326SRCS = $(SRCS_C) $(SRCS_Y)
11327OBJS = $(SRCS_C:.c=.o) $(SRCS_Y:.y=.o) $(RTAPELIB)
11328@end group
11329@group
11330AUX = README COPYING ChangeLog Makefile.in \
11331 makefile.pc configure configure.in \
11332 tar.texinfo tar.info* texinfo.tex \
11333 tar.h port.h open3.h getopt.h regex.h \
11334 rmt.h rmt.c rtapelib.c alloca.c \
11335 msd_dir.h msd_dir.c tcexparg.c \
11336 level-0 level-1 backup-specs testpad.c
11337@end group
11338
11339.PHONY: all
11340all: tar rmt tar.info
11341
11342@group
11343tar: $(OBJS)
11344 $(CC) $(LDFLAGS) -o $@@ $(OBJS) $(LIBS)
11345@end group
11346
11347@group
11348rmt: rmt.c
11349 $(CC) $(CFLAGS) $(LDFLAGS) -o $@@ rmt.c
11350@end group
11351
11352@group
11353tar.info: tar.texinfo
11354 makeinfo tar.texinfo
11355@end group
11356
11357@group
11358.PHONY: install
11359install: all
11360 $(INSTALL) tar $(bindir)/$(binprefix)tar
11361 -test ! -f rmt || $(INSTALL) rmt /etc/rmt
11362 $(INSTALLDATA) $(srcdir)/tar.info* $(infodir)
11363@end group
11364
11365@group
11366$(OBJS): tar.h port.h testpad.h
11367regex.o buffer.o tar.o: regex.h
11368# getdate.y has 8 shift/reduce conflicts.
11369@end group
11370
11371@group
11372testpad.h: testpad
11373 ./testpad
11374@end group
11375
11376@group
11377testpad: testpad.o
11378 $(CC) -o $@@ testpad.o
11379@end group
11380
11381@group
11382TAGS: $(SRCS)
11383 etags $(SRCS)
11384@end group
11385
11386@group
11387.PHONY: clean
11388clean:
11389 rm -f *.o tar rmt testpad testpad.h core
11390@end group
11391
11392@group
11393.PHONY: distclean
11394distclean: clean
11395 rm -f TAGS Makefile config.status
11396@end group
11397
11398@group
11399.PHONY: realclean
11400realclean: distclean
11401 rm -f tar.info*
11402@end group
11403
11404@group
11405.PHONY: shar
11406shar: $(SRCS) $(AUX)
11407 shar $(SRCS) $(AUX) | compress \
11408 > tar-`sed -e '/version_string/!d' \
11409 -e 's/[^0-9.]*\([0-9.]*\).*/\1/' \
11410 -e q
11411 version.c`.shar.Z
11412@end group
11413
11414@group
11415.PHONY: dist
11416dist: $(SRCS) $(AUX)
11417 echo tar-`sed \
11418 -e '/version_string/!d' \
11419 -e 's/[^0-9.]*\([0-9.]*\).*/\1/' \
11420 -e q
11421 version.c` > .fname
11422 -rm -rf `cat .fname`
11423 mkdir `cat .fname`
11424 ln $(SRCS) $(AUX) `cat .fname`
11425 tar chZf `cat .fname`.tar.Z `cat .fname`
11426 -rm -rf `cat .fname` .fname
11427@end group
11428
11429@group
11430tar.zoo: $(SRCS) $(AUX)
11431 -rm -rf tmp.dir
11432 -mkdir tmp.dir
11433 -rm tar.zoo
11434 for X in $(SRCS) $(AUX) ; do \
11435 echo $$X ; \
11436 sed 's/$$/^M/' $$X \
11437 > tmp.dir/$$X ; done
11438 cd tmp.dir ; zoo aM ../tar.zoo *
11439 -rm -rf tmp.dir
11440@end group
11441@end example
11442
11443@node GNU Free Documentation License, Concept Index, Complex Makefile, Top
11444@appendixsec GNU Free Documentation License
11445@cindex FDL, GNU Free Documentation License
11446@include fdl.texi
11447
11448@node Concept Index, Name Index, GNU Free Documentation License, Top
11449@unnumbered Index of Concepts
11450
11451@printindex cp
11452
11453@node Name Index, , Concept Index, Top
11454@unnumbered Index of Functions, Variables, & Directives
11455
11456@printindex fn
11457
11458@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